143 lines
5.2 KiB
JavaScript
143 lines
5.2 KiB
JavaScript
import { runFetch } from "./cli.mjs";
|
|
|
|
const EMAIL_REGEX = /^[a-z0-9._%+-]+@oracle\.com$/i;
|
|
const PLACEHOLDER_REGEX = /\{\{([a-zA-Z0-9]+)\}\}/g;
|
|
|
|
export function hasPlaceholders(value) {
|
|
return PLACEHOLDER_REGEX.test(String(value || ""));
|
|
}
|
|
|
|
export function resolvePlaceholders(value, examples = {}, { allowUnresolved = false } = {}) {
|
|
return String(value).replace(PLACEHOLDER_REGEX, (match, key) => {
|
|
if (examples[key]) return examples[key];
|
|
if (allowUnresolved) return match;
|
|
throw new Error(`Missing live example value for {{${key}}}.`);
|
|
});
|
|
}
|
|
|
|
export function resolvePromptCase(item, examples = {}, { allowUnresolved = false } = {}) {
|
|
return {
|
|
...item,
|
|
prompt: resolvePlaceholders(item.prompt, examples, { allowUnresolved }),
|
|
args: item.args.map((arg) => resolvePlaceholders(arg, examples, { allowUnresolved })),
|
|
...(item.liveArgs
|
|
? { liveArgs: item.liveArgs.map((arg) => resolvePlaceholders(arg, examples, { allowUnresolved })) }
|
|
: {}),
|
|
};
|
|
}
|
|
|
|
export async function buildLiveExampleContext(catalogs) {
|
|
const catalogResourceManagers = emailsFromCatalog(catalogs.initialContext);
|
|
const [pipelineSample, presalesSample] = await Promise.all([
|
|
runFetch(["--view", "sa-attach-pipeline-details", "--limit", "500"], { timeoutMs: 180000 }),
|
|
runFetch(["--view", "sa-attach-presales-coverage", "--limit", "500"], { timeoutMs: 180000 }),
|
|
]);
|
|
|
|
const pipelineRecords = pipelineSample.json.records || [];
|
|
const presalesRecords = presalesSample.json.records || [];
|
|
const resourceManagerEmails = uniqueEmails([
|
|
...catalogResourceManagers,
|
|
...valuesFromRecords(presalesRecords, ["seTeamLevel4EmailAddress"]),
|
|
]);
|
|
const resourceMemberEmails = uniqueEmails(valuesFromRecords(presalesRecords, ["seTeamLevel5EmailAddress"]));
|
|
const territoryOwnerEmails = uniqueEmails(
|
|
valuesFromRecords(pipelineRecords, ["level7TerritoryOwnerEmail", "territoryOwnerEmail"])
|
|
);
|
|
const territoryManagerEmails = uniqueEmails(
|
|
valuesFromRecords(pipelineRecords, [
|
|
"level2TerritoryOwnerEmail",
|
|
"level3TerritoryOwnerEmail",
|
|
"level4TerritoryOwnerEmail",
|
|
"level5TerritoryOwnerEmail",
|
|
"level6TerritoryOwnerEmail",
|
|
])
|
|
);
|
|
const [resourceManagerEmailA, resourceManagerEmailB] = pickDistinct(
|
|
resourceManagerEmails,
|
|
2,
|
|
"resource manager emails"
|
|
);
|
|
|
|
return {
|
|
examples: {
|
|
resourceManagerEmail: pickRandom(resourceManagerEmails, "resource manager email"),
|
|
resourceManagerEmailA,
|
|
resourceManagerEmailB,
|
|
resourceManagerEmailList: `${resourceManagerEmailA},${resourceManagerEmailB}`,
|
|
territoryOwnerEmail: pickRandom(territoryOwnerEmails, "territory owner email"),
|
|
territoryManagerEmail: pickRandom(territoryManagerEmails, "territory manager email"),
|
|
resourceMember: pickRandom(resourceMemberEmails, "resource member email"),
|
|
},
|
|
trace: [
|
|
{
|
|
script: "fetch-sales-intelligence.mjs",
|
|
args: pipelineSample.args,
|
|
result: `ok; records=${pipelineRecords.length}; source=${pipelineSample.json.request?.dataSource || "unknown"}`,
|
|
profiles: ["territoryOwnerEmail", "territoryManagerEmail"],
|
|
},
|
|
{
|
|
script: "fetch-sales-intelligence.mjs",
|
|
args: presalesSample.args,
|
|
result: `ok; records=${presalesRecords.length}; source=${presalesSample.json.request?.dataSource || "unknown"}`,
|
|
profiles: ["resourceManagerEmail", "resourceManagerEmailA", "resourceManagerEmailB", "resourceMember"],
|
|
},
|
|
],
|
|
poolSizes: {
|
|
resourceManagerEmails: resourceManagerEmails.length,
|
|
resourceMemberEmails: resourceMemberEmails.length,
|
|
territoryOwnerEmails: territoryOwnerEmails.length,
|
|
territoryManagerEmails: territoryManagerEmails.length,
|
|
},
|
|
};
|
|
}
|
|
|
|
export function placeholderKeysForItem(item) {
|
|
const text = [item.prompt, ...(item.args || []), ...(item.liveArgs || [])].join("\n");
|
|
return Array.from(text.matchAll(PLACEHOLDER_REGEX), (match) => match[1]);
|
|
}
|
|
|
|
function emailsFromCatalog(text) {
|
|
return uniqueEmails(Array.from(String(text || "").matchAll(/[a-z0-9._%+-]+@oracle\.com/gi), (match) => match[0]));
|
|
}
|
|
|
|
function valuesFromRecords(records, fieldNames) {
|
|
const values = [];
|
|
for (const record of records || []) {
|
|
for (const fieldName of fieldNames) {
|
|
if (record?.[fieldName]) values.push(record[fieldName]);
|
|
}
|
|
}
|
|
return values;
|
|
}
|
|
|
|
function uniqueEmails(values) {
|
|
return Array.from(
|
|
new Set(
|
|
values
|
|
.map((value) => String(value || "").trim().toLowerCase())
|
|
.filter((value) => EMAIL_REGEX.test(value))
|
|
.filter((value) => !/^(resource|territory)\./.test(value))
|
|
)
|
|
).sort();
|
|
}
|
|
|
|
function pickRandom(values, label) {
|
|
if (!values.length) {
|
|
throw new Error(`Could not resolve any real ${label} from the skill catalogs or live DV sample.`);
|
|
}
|
|
return values[Math.floor(Math.random() * values.length)];
|
|
}
|
|
|
|
function pickDistinct(values, count, label) {
|
|
if (values.length < count) {
|
|
throw new Error(`Could not resolve ${count} distinct real ${label} from the skill catalogs or live DV sample.`);
|
|
}
|
|
const remaining = [...values];
|
|
const picked = [];
|
|
while (picked.length < count) {
|
|
const index = Math.floor(Math.random() * remaining.length);
|
|
picked.push(remaining.splice(index, 1)[0]);
|
|
}
|
|
return picked;
|
|
}
|