UNPKG

@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

92 lines (91 loc) • 3.42 kB
import { getProviderRules } from "./registry.js"; function required(id, key, value) { if (value === undefined) { throw new Error(`Provider rules file "${id}.json" has no ${key}`); } return value; } /** The provider's request field names; throws when its rules file has none. */ export function getRequestFields(id) { return required(id, "requestFields", getProviderRules(id).requestFields); } /** * Request parameters under the provider's wire names. Unset values and empty * lists are left out, as every request builder did by hand, and so is any * parameter the rules file omits while extended thinking is on. */ export function toWireParams(id, values, context = {}) { const names = getRequestFields(id); const omitted = new Set(context.thinkingOn ? (getProviderRules(id).requestFieldRules?.omitWhileThinking ?? []) : []); const out = {}; for (const key of Object.keys(values)) { const value = values[key]; if (value === undefined || value === null || omitted.has(key)) { continue; } if (Array.isArray(value) && value.length === 0) { continue; } const name = names[key]; if (name === undefined) { throw new Error(`Provider rules file "${id}.json" has no requestFields.${key}`); } out[name] = value; } return out; } /** * The two output budget field names a provider's wire accepts: the ordinary * one and the one some models take in its place. */ export function getOutputBudgetFields(id) { const names = getRequestFields(id); return { maxTokens: names.maxTokens, maxCompletionTokens: required(id, "requestFields.maxCompletionTokens", names.maxCompletionTokens), }; } /** * Moves a request's output budget from the ordinary field to the field some * models take in its place. The value moves to the end of the body; a body * with no budget comes back without either field. */ export function moveToCompletionBudgetField(id, body) { const { maxTokens, maxCompletionTokens } = getOutputBudgetFields(id); const { [maxTokens]: budget, ...rest } = body; return (budget === undefined ? rest : { ...rest, [maxCompletionTokens]: budget }); } function toolSchemaField(id) { return required(id, "requestFields.toolSchema", getRequestFields(id).toolSchema); } /** * A tool declaration under the provider's wire names: the name, the * description when there is one, the schema, then any extra fields. */ export function toolDeclaration(id, parts) { return { name: parts.name, ...(parts.description !== undefined ? { description: parts.description } : {}), [toolSchemaField(id)]: parts.schema, ...parts.extra, }; } /** The JSON Schema a tool declaration carries, read by the wire name. */ export function toolSchemaOf(id, declaration) { const schema = declaration[toolSchemaField(id)]; return schema !== null && typeof schema === "object" ? schema : undefined; } /** How a failed tool call is reported back to the model. */ export function getToolResultRules(id) { return required(id, "toolResults", getProviderRules(id).toolResults); } /** Fixed texts inserted into the conversation. */ export function getContextTexts(id) { return required(id, "contextTexts", getProviderRules(id).contextTexts); }