UNPKG

@maximai/maxim-js

Version:

Maxim AI JS SDK. Visit https://getmaxim.ai for more info.

134 lines 4.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ToolCall = void 0; const base_1 = require("./base"); const types_1 = require("./types"); /** * Represents a function or tool call. * * Tool calls capture the invocation of external APIs, internal functions, * or any callable operations made via tool calls in your AI application. * They track the complete lifecycle including arguments, results, timing, and * errors. * * @class ToolCall * @extends BaseContainer * @example * const toolCallArgs = { * userId: '12345', * fields: ['name', 'email', 'preferences'] * }; * * const toolCall = container.toolCall({ * id: 'database-query-001', * name: 'query_user_database', * description: 'Queries the user database for customer information', * args: JSON.stringify(toolCallArgs), * }); * * // Execute and record result * try { * const userData = await query(toolCallArgs); * toolCall.result(JSON.stringify(userData)); * } catch (error) { * toolCall.error({ * message: error.message, * code: 'DB_CONNECTION_ERROR', * type: 'DatabaseError' * }); * } */ class ToolCall extends base_1.BaseContainer { /** * Creates a new tool call log entry. * * @param config - Configuration object defining the tool call * @param writer - Log writer instance for persisting tool call data * @example * const toolCall = container.toolCall({ * id: 'api-call-001', * name: 'get_user_profile', * description: 'Fetches user profile data from the database', * args: JSON.stringify({ userId: '12345', fields: ['name', 'email'] }), * }); */ constructor(config, writer) { super(types_1.Entity.TOOL_CALL, config, writer); this.args = config.args; this.description = config.description; } /** * Records the successful result of this tool call and ends it. * * @param result - The result returned by the tool as a string * @returns void * @example * toolCall.result(JSON.stringify({ * userId: '12345', * name: 'John Doe', * email: 'john@example.com' * })); */ result(result) { this.commit("result", { result }); this.end(); } /** * Static method to record a result for any tool call by ID. * * @param writer - The log writer instance * @param id - The tool call ID * @param result - The result returned by the tool * @returns void */ static result_(writer, id, result) { base_1.BaseContainer.commit_(writer, types_1.Entity.TOOL_CALL, id, "result", { result }); base_1.BaseContainer.end_(writer, types_1.Entity.TOOL_CALL, id); } /** * Records an error that occurred during this tool call and ends it. * * @param error - Error information including message, code, and type * @returns void * @example * toolCall.error({ * message: 'Database connection failed', * code: 'DB_CONNECTION_ERROR', * type: 'DatabaseError' * }); */ error(error) { this.commit("error", { error }); this.end(); } /** * Static method to record an error for any tool call by ID. * * @param writer - The log writer instance * @param id - The tool call ID * @param error - Error information * @returns void */ static error_(writer, id, error) { base_1.BaseContainer.commit_(writer, types_1.Entity.TOOL_CALL, id, "error", { error }); base_1.BaseContainer.end_(writer, types_1.Entity.TOOL_CALL, id); } /** * Returns the complete data representation of this tool call. * * @returns Tool call data. * @example * const toolData = toolCall.data(); */ data() { const baseData = super.data(); return { ...baseData, name: this._name, description: this.description, args: this.args, }; } } exports.ToolCall = ToolCall; //# sourceMappingURL=toolCall.js.map