UNPKG

openai-tokens-count

Version:

OpenAI tokens calculator with function calls, images, and messages in one call

86 lines 3.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatFunctionDefinitions = void 0; const IDENT_DEFAULT = 0; const IDENT_INCREMENT = 0; function formatFunctionDefinitions(tools) { var _a; const lines = []; lines.push("# Tools"); lines.push(""); lines.push("## functions"); lines.push(""); lines.push("namespace functions {"); lines.push(""); for (const tool of tools) { const func = tool.function; (_a = func === null || func === void 0 ? void 0 : func.description) === null || _a === void 0 ? void 0 : _a.split("\n").forEach((row) => lines.push(`// ${row}`)); const p = JSON.parse(JSON.stringify(func === null || func === void 0 ? void 0 : func.parameters)); const properties = p === null || p === void 0 ? void 0 : p.properties; if (properties && Object.keys(properties).length > 0) { lines.push(`type ${func === null || func === void 0 ? void 0 : func.name} = (_: {`); lines.push(formatObjectProperties(p, IDENT_DEFAULT)); lines.push("}) => any;"); } else { lines.push(`type ${func === null || func === void 0 ? void 0 : func.name} = () => any;`); } lines.push(""); } lines.push("} // namespace functions"); lines.push(""); return lines.join("\n"); } exports.formatFunctionDefinitions = formatFunctionDefinitions; function formatObjectProperties(p, indent) { var _a; const properties = p === null || p === void 0 ? void 0 : p.properties; if (!properties) { return ""; } const requiredParams = (p === null || p === void 0 ? void 0 : p.required) || []; const lines = []; for (const key in properties) { const props = properties[key]; (_a = props === null || props === void 0 ? void 0 : props.description) === null || _a === void 0 ? void 0 : _a.split("\n").forEach((row) => lines.push(`// ${row}`)); const question = requiredParams.includes(key) ? "" : "?"; lines.push(`${" ".repeat(indent)}${key}${question}: ${formatType(props, indent)},`); } return lines.join("\n"); } function formatType(props, indent) { var _a, _b; const type = props === null || props === void 0 ? void 0 : props.type; switch (type) { case "string": if ("enum" in props) { return props.enum.map((item) => `"${item}"`).join(" | "); } return "string"; case "array": if ("items" in props) { if (((_a = props.items) === null || _a === void 0 ? void 0 : _a.type) === "object" || ((_b = props.items) === null || _b === void 0 ? void 0 : _b.type) === "array") { return `Array<\n${formatType(props.items, indent)}\n>`; } else { return `${formatType(props.items, indent)}[]`; } } return "Array<any>"; case "object": return `{\n${formatObjectProperties(props, indent + IDENT_INCREMENT)}\n${" ".repeat(indent)}}`; case "integer": case "number": if ("enum" in props) { return props.enum.map((item) => `"${item}"`).join(" | "); } return "number"; case "boolean": return "boolean"; case "null": return "null"; default: return ""; } } //# sourceMappingURL=function-format.js.map