@juspay/neurolink
Version:
The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002
96 lines (95 loc) • 3.56 kB
JavaScript
import { getProviderRules } from "./registry.js";
const TOOL_NAME_PLACEHOLDER = "{toolName}";
/** The named tool-choice table from a rules file; throws when it is missing. */
export function getToolChoiceMap(id, name) {
const table = getProviderRules(id).toolChoices?.[name];
if (!table) {
throw new Error(`Provider rules file "${id}.json" has no toolChoices.${name} table`);
}
return table;
}
const fillToolName = (value, toolName) => {
if (value === TOOL_NAME_PLACEHOLDER) {
return toolName;
}
if (Array.isArray(value)) {
return value.map((item) => fillToolName(item, toolName));
}
if (value && typeof value === "object") {
return Object.fromEntries(Object.entries(value).map(([key, item]) => [
key,
fillToolName(item, toolName),
]));
}
return value;
};
const fromRule = (value) => value === null || value === undefined ? undefined : structuredClone(value);
/**
* Spell a tool choice for one wire (see ProviderToolChoiceMap). `toWireName`
* maps a registered tool name onto the name the wire knows it by. Returns
* undefined when nothing should be sent.
*/
export function translateToolChoice(table, choice, toWireName) {
if (choice === null || choice === undefined) {
return undefined;
}
if (typeof choice === "string") {
if (table.acceptStrings && Object.hasOwn(table.modes, choice)) {
return fromRule(table.modes[choice]);
}
return table.passthroughStrings ? choice : fromRule(table.fallback);
}
if (typeof choice === "object") {
const { type, toolName } = choice;
if (type === "tool" && (!table.toolRequiresName || toolName)) {
const wireName = typeof toolName === "string" && toWireName
? toWireName(toolName)
: toolName;
return fillToolName(table.tool, wireName);
}
if (table.acceptModeObjects &&
typeof type === "string" &&
Object.hasOwn(table.modes, type)) {
return fromRule(table.modes[type]);
}
}
return fromRule(table.fallback);
}
/** A provider's tool-name rules; throws when its rules file has none. */
export function getToolNameRules(id) {
const rules = getProviderRules(id).toolNames;
if (!rules) {
throw new Error(`Provider rules file "${id}.json" has no toolNames block`);
}
return rules;
}
export function compileToolNameRules(rules) {
const valid = new RegExp(`^[${rules.firstChars}][${rules.allowedChars}]{0,${rules.maxLength - 1}}$`);
const disallowed = new RegExp(`[^${rules.allowedChars}]`, "g");
const validFirst = new RegExp(`^[${rules.firstChars}]`);
return {
isValid: (name) => valid.test(name),
sanitize: (name) => {
if (valid.test(name)) {
return name;
}
let sanitized = name.replace(disallowed, "_");
if (!validFirst.test(sanitized)) {
sanitized = `_${sanitized}`;
}
return sanitized.slice(0, rules.maxLength);
},
unique: (base, isTaken) => {
if (!isTaken(base)) {
return base;
}
for (let suffix = 2;; suffix++) {
const tail = `_${suffix}`;
const candidate = `${base.slice(0, rules.maxLength - tail.length)}${tail}`;
if (!isTaken(candidate)) {
return candidate;
}
}
},
};
}