MCPcopy Create free account
hub / github.com/Noumena-Network/code / withTokenCountVCR

Function withTokenCountVCR

src/services/vcr.ts:383–407  ·  view source on GitHub ↗
(
  messages: unknown[],
  tools: unknown[],
  f: () => Promise<number | null>,
)

Source from the content-addressed store, hash-verified

381}
382
383export async function withTokenCountVCR(
384 messages: unknown[],
385 tools: unknown[],
386 f: () => Promise<number | null>,
387): Promise<number | null> {
388 // Dehydrate before hashing so fixture keys survive cwd/config-home/tempdir
389 // variation and message UUID/timestamp churn. System prompts embed the
390 // working directory (both raw and as a slash→dash project slug in the
391 // auto-memory path) and messages carry fresh UUIDs per run; without this,
392 // every test run produces a new hash and fixtures never hit in CI.
393 const cwdSlug = getCwd().replace(/[^a-zA-Z0-9]/g, '-')
394 const dehydrated = (
395 dehydrateValue(jsonStringify({ messages, tools })) as string
396 )
397 .replaceAll(cwdSlug, '[CWD_SLUG]')
398 .replace(
399 /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi,
400 '[UUID]',
401 )
402 .replace(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z?/g, '[TIMESTAMP]')
403 const result = await withFixture(dehydrated, 'token-count', async () => ({
404 tokenCount: await f(),
405 }))
406 return result.tokenCount
407}

Callers 1

Calls 5

getCwdFunction · 0.85
dehydrateValueFunction · 0.85
jsonStringifyFunction · 0.85
withFixtureFunction · 0.85
fFunction · 0.85

Tested by

no test coverage detected