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

5.7 KB
Raw Blame
// screenpipe — AI that knows everything you've seen, said, or heard
// https://screenpipe.com
import { expect, it, spyOn } from 'bun:test';
import { Database } from 'bun:sqlite';
import { handleRequest } from '../index';
import { OpenAIProvider } from '../providers/openai';
import type { Env, RequestBody } from '../types';

const originalFetch = globalThis.fetch;
async function exercise(stream: boolean, authorized = true) {
 const db = new Database(':memory:');
 db.exec(`CREATE TABLE cost_daily (
  date TEXT, tier TEXT, provider TEXT, model TEXT, endpoint TEXT,
  stream INTEGER, router_tier TEXT, requests INTEGER, input_tokens INTEGER,
  output_tokens INTEGER, cache_read_tokens INTEGER, cache_creation_tokens INTEGER,
  estimated_cost_usd REAL, latency_ms_sum REAL, latency_samples INTEGER,
  updated_at TEXT, PRIMARY KEY(date,tier,provider,model,endpoint,stream,router_tier)
 )`);
 const lifetime: Promise<unknown>[] = [], pending: Promise<unknown>[] = [];
 let contextOpen = true;
 let release!: () => void;
 const gate = new Promise<void>(resolve => { release = resolve; });
 const network: string[] = [];
 globalThis.fetch = (async (input: unknown) => {
  network.push(String(input));
  throw Error('unexpected external network');
 }) as typeof fetch;
 const info = spyOn(console, 'info').mockImplementation(() => {});
 const warn = spyOn(console, 'warn').mockImplementation(() => {});
 const tool = {
  id: 'synthetic-call', type: 'function',
  function: { name: 'lookup', arguments: '{"query":"synthetic"}' },
 };
 const usage = {
  prompt_tokens: 100, completion_tokens: 20,
  prompt_tokens_details: { cached_tokens: 40 },
 };
 const reply = async (body: RequestBody) => {
  const json = {
   model: body.model,
   choices: [{ message: { role: 'assistant', content: null, tool_calls: [tool] }, finish_reason: 'tool_calls' }],
   usage,
  };
  const events = [
   { choices: [{ delta: { role: 'assistant', tool_calls: [{ index: 0, ...tool }] }, finish_reason: null }] },
   { choices: [{ delta: {}, finish_reason: 'tool_calls' }] },
   { choices: [], usage },
  ];
  const sse = events.map(event => 'data: ' + JSON.stringify(event) + '\n\n').join('') + 'data: [DONE]\n\n';
  return stream
   ? new Response(sse, { headers: { 'content-type': 'text/event-stream' } })
   : Response.json(json);
 };
 const jsonSpy = spyOn(OpenAIProvider.prototype, 'createCompletion').mockImplementation(reply);
 const streamSpy = spyOn(OpenAIProvider.prototype, 'createStreamingCompletion')
  .mockImplementation(async body => (await reply(body)).body!);
 const env = {
  AI_GATEWAY_SERVICE_TOKEN: 'synthetic-service-token', OPENAI_API_KEY: 'synthetic-openai-key',
  CLOUDFLARE_AI_GATEWAY_ID: 'synthetic', CLOUDFLARE_AI_GATEWAY_TOKEN: 'synthetic-gateway-key',
  CLOUDFLARE_AI_GATEWAY_BASE_URL: 'https://gateway.ai.cloudflare.com/v1/00000000000000000000000000000000/synthetic/compat/chat/completions',
  RATE_LIMITER: {
   idFromName: (name: string) => name,
   get: () => ({ fetch: async () => Response.json({ allowed: true, standing: 'good' }) }),
  },
  DB: {
   prepare: (sql: string) => ({ bind: (...values: unknown[]) => ({
    first: async () => null,
    run: () => {
     const task = (async () => {
      if (/\bcost_daily\b/i.test(sql)) {
       await gate;
       if (contextOpen) db.query(sql).run(...values as any[]);
      }
      return { success: true };
     })();
     pending.push(task);
     return task;
    },
   }) }),
  },
 } as unknown as Env;
 // In this synthetic runtime, DB completion occurs on the next scheduler turn.
 // Synchronous completion, response-body work, and registered background work
 // can all keep the lifetime open. No particular scheduling call is asserted.
 const timer = setImmediate(release);
 try {
  const request = new Request('https://gateway.test/v1/chat/completions', {
   method: 'POST',
   headers: {
    ...(authorized ? { authorization: 'Bearer synthetic-service-token' } : {}),
    'content-type': 'application/json',
   },
   body: JSON.stringify({
    model: 'auto', stream, messages: [{ role: 'user', content: 'Synthetic lookup' }],
    tools: [{ type: 'function', function: { name: 'lookup', parameters: { type: 'object', properties: { query: { type: 'string' } } } } }],
    max_completion_tokens: 64,
   }),
  });
  const response = await handleRequest(request, env, {
   waitUntil: (task: Promise<unknown>) => lifetime.push(task),
   passThroughOnException() {},
  } as unknown as ExecutionContext);
  const content = await response.text();
  await Promise.all(lifetime);
  contextOpen = false;
  release();
  await Promise.all(pending);
  const writes = db.query('SELECT requests, input_tokens, output_tokens, cache_read_tokens FROM cost_daily').all();
  return { status: response.status, content, network, writes };
 } finally {
  clearImmediate(timer);
  release();
  await Promise.allSettled(pending);
  info.mockRestore(); warn.mockRestore(); jsonSpy.mockRestore(); streamSpy.mockRestore();
  globalThis.fetch = originalFetch;
  db.close();
 }
}
for (const stream of [false, true]) {
 it(`retains ${stream ? 'SSE' : 'JSON'} usage through request completion`, async () => {
  const result = await exercise(stream);
  expect(result.writes).toHaveLength(1);
  expect(result.writes[0]).toEqual({ requests: 1, input_tokens: 100, output_tokens: 20, cache_read_tokens: 40 });
 });
 it(`preserves ${stream ? 'SSE' : 'JSON'} successful tool response`, async () => {
  const result = await exercise(stream);
  expect(result.status).toBe(200);
  expect(result.content).toContain('synthetic-call');
  expect(result.network).toEqual([]);
 });
}
it('unauthenticated request does not produce a cost write', async () => {
 const result = await exercise(false, false);
 expect(result.status).toBe(401);
 expect(result.writes).toEqual([]);
 expect(result.network).toEqual([]);
});