@nomyx/assistant
Version:
A powerful assistant library and cli for your AI projects. works with Vertex AI (Claude and Gemini)
139 lines (138 loc) • 6.57 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.streamChat = streamChat;
const sdk_1 = __importDefault(require("@anthropic-ai/sdk"));
const tools_1 = require("./tools");
async function* streamChat(config, messages, options, tools) {
const anthropic = new sdk_1.default({
apiKey: config.apiKey,
});
const convertedMessages = (0, tools_1.convertMessages)(messages);
const requestBody = {
model: config.model,
messages: convertedMessages,
max_tokens: options.maxTokens || 1000,
temperature: options.temperature || 0.7,
stream: true,
system: '',
};
if (convertedMessages.length > 0 && convertedMessages[0].role === 'system') {
requestBody.system = convertedMessages[0].content;
requestBody.messages = requestBody.messages.slice(1);
}
if (tools && tools.length > 0) {
requestBody.tools = tools.map(tool => (0, tools_1.convertToolSchema)(tool.getSchema()));
}
console.log('Sending request to Anthropic API', {
model: requestBody.model,
messageCount: requestBody.messages.length,
maxTokens: requestBody.max_tokens,
temperature: requestBody.temperature,
toolCount: tools?.length
});
try {
const stream = await anthropic.messages.create(requestBody);
console.log('Received response from Anthropic API');
let aggregatedData = '';
let currentToolCall = null;
// Create a basic ActionContext
const actionContext = {
user: { id: 'default-user', name: 'Default User' },
session: { id: 'default-session', timestamp: new Date().toISOString() },
};
for await (const chunk of stream) {
console.log('Received chunk', { chunkSize: JSON.stringify(chunk).length });
aggregatedData += JSON.stringify(chunk);
try {
const parsedData = JSON.parse(aggregatedData);
console.log('Parsed data:', parsedData);
// Note: In OpenAI's format, we would target choices[0].delta here.
// Anthropic's API might structure the response differently, so we're using parsedData.delta directly.
if (parsedData.delta && parsedData.delta.content) {
const content = parsedData.delta.content;
console.log('Content:', content);
yield createProviderResponse({ content, toolCalls: [] });
}
if (parsedData.delta && parsedData.delta.tool_calls) {
const toolCalls = parsedData.delta.tool_calls;
console.log('Tool Calls:', toolCalls);
for (const toolCall of toolCalls) {
if (!currentToolCall || toolCall.index === 0) {
currentToolCall = { ...toolCall, function: { ...toolCall.function } };
}
else {
if (toolCall.function.name) {
currentToolCall.function.name = toolCall.function.name;
}
if (toolCall.function.arguments) {
currentToolCall.function.arguments = (currentToolCall.function.arguments || '') + toolCall.function.arguments;
}
}
}
console.log('Current Tool Call:', currentToolCall);
if (parsedData.delta.type === 'tool_calls' && parsedData.delta.tool_calls[0].type === 'function') {
try {
const fullToolCall = {
name: currentToolCall.function.name,
arguments: JSON.parse(currentToolCall.function.arguments)
};
console.log('Full tool call:', fullToolCall);
// Execute the tool
const tool = tools?.find(t => t.name === fullToolCall.name);
if (tool) {
const result = await tool.execute(fullToolCall.arguments, actionContext);
console.log('Tool execution result:', result);
yield createProviderResponse({ content: `Tool ${fullToolCall.name} executed. Result: ${JSON.stringify(result)}`, toolCalls: [fullToolCall] });
}
else {
console.error(`Tool ${fullToolCall.name} not found`);
yield createProviderResponse({ content: `Error: Tool ${fullToolCall.name} not found`, toolCalls: [fullToolCall] });
}
currentToolCall = null;
}
catch (parseError) {
console.error('Error parsing or executing tool call:', parseError);
console.error('Raw tool call arguments:', currentToolCall.function.arguments);
yield createProviderResponse({ content: `Error executing tool: ${parseError instanceof Error ? parseError.message : String(parseError)}`, toolCalls: [] });
}
}
}
aggregatedData = ''; // Reset aggregated data after successful parsing
}
catch (parseError) {
// If parsing fails, continue aggregating data
console.log('Parsing failed, continuing to aggregate data');
continue;
}
}
}
catch (error) {
handleError(config, error);
throw error;
}
}
function createProviderResponse(data) {
return {
content: data.content,
toolCalls: data.toolCalls,
usage: {
promptTokens: 0,
completionTokens: 0,
totalTokens: 0
},
};
}
function handleError(config, error) {
if (error instanceof sdk_1.default.APIError) {
console.error('AnthropicProvider: API request failed:', {
status: error.status,
message: error.message,
});
}
else {
console.error('AnthropicProvider: Unexpected error:', error);
}
}