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): preserve workflow voice responses across Durable Objects

Ezra Elletted9c66f9bb414
diff --git a/packages/ai-gateway/src/index.ts b/packages/ai-gateway/src/index.ts
index dc52b1ecd..d7c9b99b4 100644
--- a/packages/ai-gateway/src/index.ts
+++ b/packages/ai-gateway/src/index.ts
@@ -362,7 +362,9 @@ export async function handleRequest(request: Request, env: Env, ctx: ExecutionCo
const owner = env.RATE_LIMITER.idFromName(`workflow-voice:${authResult.userId}`);
const headers = new Headers(request.headers);
headers.set('x-voice-account', authResult.userId);
- const response = await env.RATE_LIMITER.get(owner).fetch(new Request('https://internal/workflow-voice', { method: request.method, headers, body: request.body }));
+ const upstream = await env.RATE_LIMITER.get(owner).fetch(new Request('https://internal/workflow-voice', { method: request.method, headers, body: request.body }));
+ // Fetch responses have immutable headers in the Workers runtime.
+ const response = new Response(upstream.body, upstream);
response.headers.set('Cache-Control', 'no-store');
return addCorsHeaders(response);
}
diff --git a/packages/ai-gateway/src/test/local-gateway-harness.integration.test.ts b/packages/ai-gateway/src/test/local-gateway-harness.integration.test.ts
index 034c49b36..d85672991 100644
--- a/packages/ai-gateway/src/test/local-gateway-harness.integration.test.ts
+++ b/packages/ai-gateway/src/test/local-gateway-harness.integration.test.ts
@@ -20,6 +20,45 @@ afterEach(async () => {
});
describe('local AI gateway harness', () => {
+ test('preserves voice responses across the real Durable Object fetch boundary', async () => {
+ const harness = await startHarness({
+ outboundResponse: async (request) => {
+ if (request.url === 'https://screenpipe.com/api/user') {
+ return Response.json({ success: true, user: {
+ id: 'synthetic-voice-user', cloud_subscribed: true, subscription_plan: 'pro', app_entitled: true,
+ entitlement: { active: true, plan: 'pro', features: { app: true, cloud: true } },
+ } });
+ }
+ if (request.url === 'https://api.openai.com/v1/live/sessions') {
+ return Response.json({ session: { id: 'synthetic-call' },
+ transport: { type: 'webrtc', sdp: 'v=0\r\nsynthetic-answer' } });
+ }
+ if (request.url === 'https://api.openai.com/v1/live/sessions/synthetic-call/hangup') {
+ return new Response(null, { status: 204 });
+ }
+ },
+ });
+ const voice = (body: unknown, method = 'POST') => harness.fetch('/workflow-voice', {
+ method, headers: { Authorization: 'Bearer eyJ.synthetic-voice-token', 'Content-Type': 'application/json' },
+ body: JSON.stringify(body),
+ });
+ const invalid = await voice({});
+ expect(invalid.status).toBe(400);
+ expect(await invalid.json()).toEqual({ error: 'Invalid voice request.' });
+ const connected = await voice({ sdp: 'v=0\r\nsynthetic-offer', title: 'Synthetic workflow', questions: [] });
+ expect(connected.status).toBe(201);
+ const call = await connected.json() as { call_token: string; sdp: string };
+ expect(call.sdp).toBe('v=0\r\nsynthetic-answer');
+ const closed = await voice({ call_token: call.call_token }, 'DELETE');
+ expect(closed.status).toBe(204);
+ expect(await closed.text()).toBe('');
+ for (const response of [invalid, connected, closed]) {
+ expect(response.headers.get('Cache-Control')).toBe('no-store');
+ expect(response.headers.get('Access-Control-Allow-Origin')).toBe('*');
+ }
+ harness.assertNoUnexpectedOutboundRequests();
+ });
+
test('retains Auto decisions and monotonic fallback in the real Durable Object runtime', async () => {
const harness = await startHarness({ routerMode: 'heuristic' });
const object = await harness.rateLimiterObject('synthetic-auto-turn');
diff --git a/packages/ai-gateway/src/test/local-gateway-harness.ts b/packages/ai-gateway/src/test/local-gateway-harness.ts
index f398b3676..292a43614 100644
--- a/packages/ai-gateway/src/test/local-gateway-harness.ts
+++ b/packages/ai-gateway/src/test/local-gateway-harness.ts
@@ -194,6 +194,7 @@ export class LocalGatewayHarness {
...jsonBindings(options.privateCostControls),
...(options.ttsStatus !== undefined ? { TTS_ENABLED: 'true', ELEVENLABS_VOICE_ID: 'fictionalVoice123', ELEVENLABS_USD_PER_CHARACTER: '0.0001' } : {}),
OPENAI_API_KEY: 'screenpipe-local-e2e-only',
+ ADMIN_SECRET: 'screenpipe-local-e2e-admin-only',
AI_GATEWAY_SERVICE_TOKEN: LOCAL_GATEWAY_SERVICE_TOKEN,
MODEL_GATING_ENABLED: 'true',
PIPE_FRONTIER_POLICY: 'reject',