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): buffer SSE chunks to handle split events

Louis Beaumont6378fbd40f4b
diff --git a/screenpipe-js/ai-proxy/src/index.ts b/screenpipe-js/ai-proxy/src/index.ts
index 29db533dc..b71162142 100644
--- a/screenpipe-js/ai-proxy/src/index.ts
+++ b/screenpipe-js/ai-proxy/src/index.ts
@@ -107,9 +107,9 @@ export default Sentry.withSentry(
}
return createErrorResponse(404, 'not found');
- } catch (error) {
- console.error('error in fetch:', error);
- return createErrorResponse(500, 'an error occurred');
+ } catch (error: any) {
+ console.error('error in fetch:', error?.message, error?.stack);
+ return createErrorResponse(500, error?.message || 'an error occurred');
} finally {
await langfuse.shutdownAsync();
}
diff --git a/screenpipe-js/ai-proxy/src/providers/vertex.ts b/screenpipe-js/ai-proxy/src/providers/vertex.ts
index 615fb640d..1d38203a8 100644
--- a/screenpipe-js/ai-proxy/src/providers/vertex.ts
+++ b/screenpipe-js/ai-proxy/src/providers/vertex.ts
@@ -231,6 +231,7 @@ export class VertexAIProvider implements AIProvider {
const toolCallsById: Record<string, { index: number; id: string; name: string; arguments: string }> = {};
const toolCallIndexRef = { value: 0 };
+ let buffer = ''; // Buffer for incomplete lines
return new ReadableStream({
async start(controller) {
@@ -243,8 +244,12 @@ export class VertexAIProvider implements AIProvider {
return;
}
- const chunk = decoder.decode(value, { stream: true });
- const lines = chunk.split('\n');
+ // Append chunk to buffer and split by newlines
+ buffer += decoder.decode(value, { stream: true });
+ const lines = buffer.split('\n');
+
+ // Keep the last potentially incomplete line in buffer
+ buffer = lines.pop() || '';
for (const line of lines) {
if (line.startsWith('data: ')) {
@@ -260,7 +265,7 @@ export class VertexAIProvider implements AIProvider {
return;
}
} catch (e) {
- // Skip invalid JSON
+ // Skip invalid JSON (might be incomplete)
}
}
}
diff --git a/screenpipe-js/ai-proxy/src/test/vertex.unit.test.ts b/screenpipe-js/ai-proxy/src/test/vertex.unit.test.ts
index 1757e8f3e..4ee2125d2 100644
--- a/screenpipe-js/ai-proxy/src/test/vertex.unit.test.ts
+++ b/screenpipe-js/ai-proxy/src/test/vertex.unit.test.ts
@@ -502,6 +502,81 @@ describe('Full streaming scenarios', () => {
});
});
+// ============================================================================
+// SSE Buffer Parsing Tests
+// ============================================================================
+describe('SSE buffer parsing', () => {
+ it('should handle events split across chunks', () => {
+ // Simulate SSE events that might be split across network chunks
+ const chunk1 = 'data: {"type":"content_block_delta","delta":{"type":"text_delta","text":"Hel';
+ const chunk2 = 'lo"}}\n\ndata: {"type":"content_block_delta","delta":{"type":"text_delta","text":" World"}}\n\n';
+
+ let buffer = '';
+ const results: any[] = [];
+
+ // Process chunk1
+ buffer += chunk1;
+ let lines = buffer.split('\n');
+ buffer = lines.pop() || '';
+ for (const line of lines) {
+ if (line.startsWith('data: ')) {
+ try {
+ results.push(JSON.parse(line.slice(6)));
+ } catch (e) {
+ // incomplete JSON
+ }
+ }
+ }
+
+ // After chunk1, buffer should have incomplete data
+ expect(results.length).toBe(0);
+ expect(buffer).toContain('Hel');
+
+ // Process chunk2
+ buffer += chunk2;
+ lines = buffer.split('\n');
+ buffer = lines.pop() || '';
+ for (const line of lines) {
+ if (line.startsWith('data: ')) {
+ try {
+ results.push(JSON.parse(line.slice(6)));
+ } catch (e) {
+ // incomplete JSON
+ }
+ }
+ }
+
+ // After chunk2, should have both complete events
+ expect(results.length).toBe(2);
+ expect(results[0].delta.text).toBe('Hello');
+ expect(results[1].delta.text).toBe(' World');
+ });
+
+ it('should handle complete events in single chunk', () => {
+ const chunk = 'data: {"type":"message_start"}\n\ndata: {"type":"ping"}\n\n';
+
+ let buffer = '';
+ const results: any[] = [];
+
+ buffer += chunk;
+ const lines = buffer.split('\n');
+ buffer = lines.pop() || '';
+ for (const line of lines) {
+ if (line.startsWith('data: ')) {
+ try {
+ results.push(JSON.parse(line.slice(6)));
+ } catch (e) {
+ // skip
+ }
+ }
+ }
+
+ expect(results.length).toBe(2);
+ expect(results[0].type).toBe('message_start');
+ expect(results[1].type).toBe('ping');
+ });
+});
+
// ============================================================================
// Request Conversion Tests (convertToAnthropicFormat)
// ============================================================================
diff --git a/screenpipe-js/ai-proxy/wrangler.toml b/screenpipe-js/ai-proxy/wrangler.toml
index 926cedfc1..dfa27f776 100644
--- a/screenpipe-js/ai-proxy/wrangler.toml
+++ b/screenpipe-js/ai-proxy/wrangler.toml
@@ -5,7 +5,7 @@ compatibility_date = "2024-10-11"
compatibility_flags = ["nodejs_compat"]
upload_source_maps = true
-tail_consumers = [{service = "ai-proxy-tail"}]
+# tail_consumers = [{service = "ai-proxy-tail"}]
# Workers Logs
# Docs: https://developers.cloudflare.com/workers/observability/logs/workers-logs/