UNPKG

assistan-ts

Version:

A typesafe and code-first library to define and run OpenAI assistants

68 lines 3.24 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.functionsToPayload = exports.toolsToPayload = exports.toPayload = exports.definition = exports.METADATA_KEY = void 0; const link_1 = require("./link"); const typebox_1 = require("./lib/typebox"); exports.METADATA_KEY = "__key__"; // Identity function to assist with type inference const definition = (definition) => { return Object.assign(Object.assign({}, definition), { /** Find the matching assistant and sync the definition with openai */ link: (0, link_1.link)(definition), /* Links the definition without calling OpenAI to find assistant */ link__unsafe: (props) => { return Object.assign(Object.assign({}, definition), { openai: props.openai, id: props.assistantId }); } }); }; exports.definition = definition; const toPayload = (assistant) => { var _a; const { key, functionTools, codeInterpreter, retrieval, metadata: userMetadata, files } = assistant, rest = __rest(assistant, ["key", "functionTools", "codeInterpreter", "retrieval", "metadata", "files"]); const tools = (0, exports.toolsToPayload)(assistant); const metadata = Object.assign({ [exports.METADATA_KEY]: assistant.key }, (userMetadata !== null && userMetadata !== void 0 ? userMetadata : {})); /* Does not include "resolved" files, as those must be dynamical matched using API */ const file_ids = (_a = files === null || files === void 0 ? void 0 : files.file_ids) !== null && _a !== void 0 ? _a : undefined; return Object.assign(Object.assign({}, rest), { tools, metadata, file_ids }); }; exports.toPayload = toPayload; const toolsToPayload = (def) => { const { functionTools, codeInterpreter = false, retrieval = false } = def; const functions = (0, exports.functionsToPayload)(functionTools); const tools = [ ...functions, ...(codeInterpreter ? [{ type: "code_interpreter" }] : []), ...(retrieval ? [{ type: "retrieval" }] : []), ]; return tools; }; exports.toolsToPayload = toolsToPayload; const functionsToPayload = (functionTools) => { if (!functionTools) return []; return Object.keys(functionTools).map((toolKey) => { // TODO: fix hack to deal with undefined type complexity. // (currently mutates def which is problematic) if ((0, typebox_1.isNullType)(functionTools[toolKey].parameters)) { functionTools[toolKey]["parameters"] = null; } return { type: "function", function: Object.assign({ name: toolKey }, functionTools[toolKey]), }; }); }; exports.functionsToPayload = functionsToPayload; //# sourceMappingURL=definition.js.map