UNPKG

@nomyx/assistant

Version:

A powerful assistant library and cli for your AI projects. works with Vertex AI (Claude and Gemini)

114 lines (113 loc) 4.54 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AnthropicProviderError = exports.AnthropicProvider = void 0; const cache_1 = require("./cache"); const chat_1 = require("./chat"); const streaming_1 = require("./streaming"); const config_1 = require("./config"); const errors_1 = require("./errors"); const tools_1 = require("./tools"); class AnthropicProvider { constructor(config, logger) { this.middleware = []; this.config = new config_1.AnthropicConfig(config, logger); this.cache = new cache_1.AnthropicCache(); } async chat(messages, options, tools) { try { const cacheKey = { messages, options, tools }; if (this.cache.has(cacheKey)) { return this.cache.get(cacheKey); } let processedMessages = messages; for (const mw of this.middleware) { processedMessages = await mw.preProcess(processedMessages); } let response = await (0, chat_1.chat)(this.config, processedMessages, options, tools); for (const mw of this.middleware) { response = await mw.postProcess(response); } if (response.toolCalls && response.toolCalls.length > 0) { for (const mw of this.middleware) { if (mw.processToolCalls) { response.toolCalls = await mw.processToolCalls(response.toolCalls); } } } this.cache.set(cacheKey, response); return response; } catch (error) { throw (0, errors_1.handleAnthropicError)(error); } } async *streamChat(messages, options, tools) { try { let processedMessages = messages; for (const mw of this.middleware) { processedMessages = await mw.preProcess(processedMessages); } const stream = (0, streaming_1.streamChat)(this.config, processedMessages, options, tools); for await (const chunk of stream) { let processedChunk = chunk; for (const mw of this.middleware) { processedChunk = await mw.postProcess(processedChunk); } if (processedChunk.toolCalls && processedChunk.toolCalls.length > 0) { for (const mw of this.middleware) { if (mw.processToolCalls) { processedChunk.toolCalls = await mw.processToolCalls(processedChunk.toolCalls); } } } yield processedChunk; } } catch (error) { throw (0, errors_1.handleAnthropicError)(error); } } getCapabilities() { return { maxTokens: 100000, // Adjust based on Anthropic's actual limits supportsFunctionCalling: true, supportsStreaming: true, supportedModels: [this.config.model], maxSimultaneousCalls: 1, // Adjust based on Anthropic's rate limits supportsSemanticCaching: false, }; } use(middleware) { this.middleware.push(middleware); } registerPlugin(plugin) { plugin.initialize(this); } convertToolSchema(schema) { return (0, tools_1.convertToolSchema)(schema); } convertToolCall(call) { return (0, tools_1.convertToolCall)(call); } clearCache() { this.cache.clear(); } } exports.AnthropicProvider = AnthropicProvider; __exportStar(require("./types"), exports); var errors_2 = require("./errors"); Object.defineProperty(exports, "AnthropicProviderError", { enumerable: true, get: function () { return errors_2.AnthropicProviderError; } });