Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(ai-proxy): pass tools to Anthropic streaming API and handle tool_use events

Louis Beaumont2786b209d00f
diff --git a/screenpipe-js/ai-proxy/src/providers/anthropic.ts b/screenpipe-js/ai-proxy/src/providers/anthropic.ts
index 8eca11c68..ec1df93cd 100644
--- a/screenpipe-js/ai-proxy/src/providers/anthropic.ts
+++ b/screenpipe-js/ai-proxy/src/providers/anthropic.ts
@@ -61,12 +61,17 @@ export class AnthropicProvider implements AIProvider {
stream: true,
max_tokens: 4096,
temperature: body.temperature,
+ tools: body.tools ? this.formatTools(body.tools) : undefined,
});
return new ReadableStream({
async start(controller) {
try {
+ let currentToolCall: { index: number; id: string; name: string; arguments: string } | null = null;
+ let toolCallIndex = 0;
+
for await (const chunk of stream) {
+ // Handle text content
if (chunk.type === 'content_block_delta' && chunk.delta?.type === 'text_delta') {
controller.enqueue(
new TextEncoder().encode(
@@ -76,6 +81,63 @@ export class AnthropicProvider implements AIProvider {
)
);
}
+
+ // Handle tool use start
+ if (chunk.type === 'content_block_start' && chunk.content_block?.type === 'tool_use') {
+ currentToolCall = {
+ index: toolCallIndex,
+ id: chunk.content_block.id,
+ name: chunk.content_block.name,
+ arguments: '',
+ };
+ // Send tool call start in OpenAI format
+ controller.enqueue(
+ new TextEncoder().encode(
+ `data: ${JSON.stringify({
+ choices: [{
+ delta: {
+ tool_calls: [{
+ index: toolCallIndex,
+ id: chunk.content_block.id,
+ type: 'function',
+ function: {
+ name: chunk.content_block.name,
+ arguments: '',
+ },
+ }],
+ },
+ }],
+ })}\n\n`
+ )
+ );
+ }
+
+ // Handle tool use input delta
+ if (chunk.type === 'content_block_delta' && chunk.delta?.type === 'input_json_delta' && currentToolCall) {
+ currentToolCall.arguments += chunk.delta.partial_json;
+ controller.enqueue(
+ new TextEncoder().encode(
+ `data: ${JSON.stringify({
+ choices: [{
+ delta: {
+ tool_calls: [{
+ index: currentToolCall.index,
+ function: {
+ arguments: chunk.delta.partial_json,
+ },
+ }],
+ },
+ }],
+ })}\n\n`
+ )
+ );
+ }
+
+ // Handle tool use end
+ if (chunk.type === 'content_block_stop' && currentToolCall) {
+ toolCallIndex++;
+ currentToolCall = null;
+ }
}
controller.enqueue(new TextEncoder().encode('data: [DONE]\n\n'));
controller.close();