-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathllm-anthropic.test.js
More file actions
719 lines (647 loc) · 23.9 KB
/
Copy pathllm-anthropic.test.js
File metadata and controls
719 lines (647 loc) · 23.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
// Tests for /api/llm/anthropic — we-pay LLM proxy.
// Focus: monthly token budgeting (input+output), embed-policy enforcement
// (origin, surface, brain mode), upstream error sanitization, model allowlist.
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { Readable } from 'node:stream';
process.env.PUBLIC_APP_ORIGIN ||= 'https://app.test';
process.env.JWT_SECRET ||= 'test-secret-llm';
process.env.ANTHROPIC_API_KEY ||= 'sk-ant-test-key';
process.env.UPSTASH_REDIS_REST_URL ||= 'https://redis.test';
process.env.UPSTASH_REDIS_REST_TOKEN ||= 'redis-token';
// ── Mocks ─────────────────────────────────────────────────────────────────
const policyState = { policy: null };
const avatarPolicyState = { policy: null };
const avatarState = { avatar: null };
vi.mock('../../api/_lib/embed-policy.js', () => ({
readEmbedPolicy: vi.fn(async () => policyState.policy),
readEmbedPolicyByAvatarId: vi.fn(async () => avatarPolicyState.policy),
defaultEmbedPolicy: () => JSON.parse(JSON.stringify(DEFAULT_POLICY)),
}));
vi.mock('../../api/_lib/avatars.js', () => ({
getAvatar: vi.fn(async () => avatarState.avatar),
}));
const rlState = {
ip: { success: true },
agent: { success: true },
};
vi.mock('../../api/_lib/rate-limit.js', () => ({
limits: {
embedLlmIp: vi.fn(async () => rlState.ip),
embedLlmAgent: vi.fn(async () => rlState.agent),
},
clientIp: () => '203.0.113.1',
}));
const usageEvents = [];
vi.mock('../../api/_lib/usage.js', () => ({
recordEvent: (evt) => usageEvents.push(evt),
logger: () => ({ info: () => {}, warn: () => {}, error: () => {} }),
}));
// In-memory Redis stub backing the @upstash/redis client. Token + quota
// counters are kept here so tests can prime initial usage and assert deltas.
const redisStore = new Map();
const redisCalls = [];
class FakeRedis {
constructor(_opts) {}
async incr(key) {
const next = (redisStore.get(key) ?? 0) + 1;
redisStore.set(key, next);
redisCalls.push({ op: 'incr', key, value: next });
return next;
}
async incrby(key, delta) {
const next = (redisStore.get(key) ?? 0) + delta;
redisStore.set(key, next);
redisCalls.push({ op: 'incrby', key, delta, value: next });
return next;
}
async get(key) {
return redisStore.get(key) ?? null;
}
async expire(key, ttl) {
redisCalls.push({ op: 'expire', key, ttl });
return 1;
}
}
vi.mock('@upstash/redis', () => ({ Redis: FakeRedis }));
// Default upstream mock — overridden per-test as needed.
const fetchState = {
response: () => upstreamOk({ ok: true, usage: { input_tokens: 10, output_tokens: 20 } }),
calls: [],
};
function upstreamOk(json) {
return {
ok: true,
status: 200,
headers: new Map([['content-type', 'application/json']]),
text: async () => JSON.stringify(json),
};
}
function upstreamErr(status, body) {
return {
ok: false,
status,
headers: new Map([['content-type', 'application/json']]),
text: async () => (typeof body === 'string' ? body : JSON.stringify(body)),
};
}
// Map.get is case-sensitive but the handler reads via headers.get('content-type')
// — patch Headers-like objects to mimic native fetch Headers behavior.
function patchHeaders(map) {
return { get: (k) => map.get(k.toLowerCase()) ?? map.get(k) ?? null };
}
globalThis.fetch = vi.fn(async (url, init) => {
fetchState.calls.push({ url, init });
const r = await fetchState.response();
r.headers = patchHeaders(
r.headers instanceof Map ? r.headers : new Map(Object.entries(r.headers || {})),
);
return r;
});
const { default: handler } = await import('../../api/llm/anthropic.js');
// ── Helpers ───────────────────────────────────────────────────────────────
function makeReq({ url = '/api/llm/anthropic?agent=agent-1', headers = {}, body = null } = {}) {
const base = body ? Readable.from([Buffer.from(JSON.stringify(body))]) : Readable.from([]);
base.method = 'POST';
base.url = url;
base.headers = {
host: 'app.test',
'content-type': 'application/json',
...headers,
};
return base;
}
function makeRes() {
return {
statusCode: 200,
headers: {},
body: '',
writableEnded: false,
setHeader(k, v) {
this.headers[k.toLowerCase()] = v;
},
getHeader(k) {
return this.headers[k.toLowerCase()];
},
end(chunk) {
if (chunk !== undefined) this.body += chunk;
this.writableEnded = true;
},
};
}
async function invoke(opts = {}) {
const req = makeReq(opts);
const res = makeRes();
await handler(req, res);
const json = res.body ? safeJson(res.body) : null;
return { res, status: res.statusCode, body: json };
}
function safeJson(s) {
try {
return JSON.parse(s);
} catch {
return s;
}
}
const VALID_BODY = {
messages: [{ role: 'user', content: 'hello' }],
max_tokens: 100,
};
const WE_PAY_POLICY = {
version: 1,
origins: { mode: 'allowlist', hosts: ['client.test'] },
surfaces: { script: true, iframe: true, widget: true, mcp: false },
brain: {
mode: 'we-pay',
proxy_url: null,
monthly_quota: null,
rate_limit_per_min: null,
model: 'claude-opus-4-6',
cost_limit_cents: null,
},
storage: { primary: 'r2', pinned_ipfs: false, onchain_attested: false },
};
// Mirrors embed-policy.js defaultEmbedPolicy() — the we-pay free-model policy
// granted to bare public-avatar embeds that have no agent_identity row.
const DEFAULT_POLICY = {
version: 1,
origins: { mode: 'allowlist', hosts: [] },
surfaces: { script: true, iframe: true, widget: true, mcp: false },
brain: {
mode: 'we-pay',
proxy_url: null,
monthly_quota: 1000,
rate_limit_per_min: 10,
model: 'meta-llama/llama-3.3-70b-instruct:free',
},
storage: { primary: 'r2', pinned_ipfs: false, onchain_attested: false },
};
// ── Reset between tests ───────────────────────────────────────────────────
beforeEach(() => {
policyState.policy = JSON.parse(JSON.stringify(WE_PAY_POLICY));
avatarPolicyState.policy = null;
avatarState.avatar = null;
rlState.ip = { success: true };
rlState.agent = { success: true };
usageEvents.length = 0;
redisStore.clear();
redisCalls.length = 0;
fetchState.calls.length = 0;
fetchState.response = () =>
upstreamOk({ ok: true, usage: { input_tokens: 10, output_tokens: 20 } });
});
// ── agent + policy gating ─────────────────────────────────────────────────
describe('/api/llm/anthropic — agent + policy gating', () => {
it('rejects when no agent query param supplied', async () => {
const { status, body } = await invoke({ url: '/api/llm/anthropic', body: VALID_BODY });
expect(status).toBe(400);
expect(body.error).toBe('validation_error');
});
it('returns 404 when no agent, avatar-agent, or public avatar matches', async () => {
policyState.policy = null;
avatarPolicyState.policy = null;
avatarState.avatar = null;
const { status, body } = await invoke({ body: VALID_BODY });
expect(status).toBe(404);
expect(body.error).toBe('not_found');
});
it('falls back to the agent_identity linked by avatar_id', async () => {
policyState.policy = null;
avatarPolicyState.policy = JSON.parse(JSON.stringify(WE_PAY_POLICY));
const { status } = await invoke({ body: VALID_BODY });
expect(status).toBe(200);
});
it('grants the default we-pay policy to a bare public avatar embed', async () => {
policyState.policy = null;
avatarPolicyState.policy = null;
avatarState.avatar = { id: 'avatar-uuid', visibility: 'public' };
// The default policy serves the free OpenRouter model.
process.env.OPENROUTER_API_KEY = 'sk-or-test';
try {
const { status } = await invoke({ body: VALID_BODY });
expect(status).toBe(200);
} finally {
delete process.env.OPENROUTER_API_KEY;
}
});
it('does not grant a policy to a private avatar', async () => {
policyState.policy = null;
avatarPolicyState.policy = null;
avatarState.avatar = { id: 'avatar-uuid', visibility: 'private' };
const { status, body } = await invoke({ body: VALID_BODY });
expect(status).toBe(404);
expect(body.error).toBe('not_found');
});
it('returns 402 payment_required when brain.mode is not we-pay', async () => {
policyState.policy.brain.mode = 'wallet-gated';
const { status, body } = await invoke({ body: VALID_BODY });
expect(status).toBe(402);
expect(body.error).toBe('payment_required');
});
it('returns 403 embed_denied_surface when script surface is disabled', async () => {
policyState.policy.surfaces.script = false;
const { status, body } = await invoke({ body: VALID_BODY });
expect(status).toBe(403);
expect(body.error).toBe('embed_denied_surface');
});
});
// ── origin enforcement ───────────────────────────────────────────────────
describe('/api/llm/anthropic — origin / referer policy', () => {
it('rejects an origin not on the allowlist', async () => {
const { status, body } = await invoke({
body: VALID_BODY,
headers: { origin: 'https://attacker.test' },
});
expect(status).toBe(403);
expect(body.error).toBe('embed_denied_origin');
});
it('allows server-to-server (no Origin header) requests', async () => {
const { status } = await invoke({ body: VALID_BODY });
expect(status).toBe(200);
});
it('allows first-party localhost origin without policy entry', async () => {
policyState.policy.origins.hosts = []; // explicitly empty
const { status } = await invoke({
body: VALID_BODY,
headers: { origin: 'http://localhost:3000' },
});
expect(status).toBe(200);
});
});
// ── rate limiting ────────────────────────────────────────────────────────
describe('/api/llm/anthropic — rate limiting', () => {
it('returns 429 on per-IP rate limit', async () => {
rlState.ip = { success: false };
const { status, body } = await invoke({ body: VALID_BODY });
expect(status).toBe(429);
expect(body.error_description).toMatch(/IP/);
});
it('returns 429 on per-agent rate limit when policy declares one', async () => {
policyState.policy.brain.rate_limit_per_min = 5;
rlState.agent = { success: false };
const { status, body } = await invoke({ body: VALID_BODY });
expect(status).toBe(429);
expect(body.error_description).toMatch(/agent rate limit/);
});
});
// ── monthly call-count quota ─────────────────────────────────────────────
describe('/api/llm/anthropic — monthly call quota', () => {
it('returns 429 quota_exceeded once monthly_quota is reached', async () => {
policyState.policy.brain.monthly_quota = 2;
// Pre-seed the call counter so the next incr crosses the quota.
const monthKey = new Date().toISOString().slice(0, 7);
redisStore.set(`llm:quota:agent-1:${monthKey}`, 2);
const { status, body } = await invoke({ body: VALID_BODY });
expect(status).toBe(429);
expect(body.error).toBe('quota_exceeded');
expect(body.error_description).toMatch(/monthly quota of 2/);
});
it('passes through when under monthly_quota', async () => {
policyState.policy.brain.monthly_quota = 100;
const { status } = await invoke({ body: VALID_BODY });
expect(status).toBe(200);
});
});
// ── monthly token budget ────────────────────────────────────────────────
describe('/api/llm/anthropic — monthly token budget', () => {
it('returns 429 quota_exceeded when token budget already met (default budget)', async () => {
const monthKey = new Date().toISOString().slice(0, 7);
// Default budget is 1_000_000 — pre-seed at the cap.
redisStore.set(`llm:tokens:agent-1:${monthKey}`, 1_000_000);
const { status, body } = await invoke({ body: VALID_BODY });
expect(status).toBe(429);
expect(body.error).toBe('quota_exceeded');
expect(body.error_description).toMatch(/monthly token budget/);
});
it('derives a smaller token budget from policy.brain.cost_limit_cents', async () => {
// 15 cents @ 1.5¢/1k tokens = 10_000-token budget.
policyState.policy.brain.cost_limit_cents = 15;
const monthKey = new Date().toISOString().slice(0, 7);
redisStore.set(`llm:tokens:agent-1:${monthKey}`, 10_000);
const { status, body } = await invoke({ body: VALID_BODY });
expect(status).toBe(429);
expect(body.error_description).toMatch(/10000/);
});
it('debits actual token usage to the per-agent counter on success', async () => {
fetchState.response = () =>
upstreamOk({ ok: true, usage: { input_tokens: 123, output_tokens: 456 } });
const { status } = await invoke({ body: VALID_BODY });
expect(status).toBe(200);
const monthKey = new Date().toISOString().slice(0, 7);
expect(redisStore.get(`llm:tokens:agent-1:${monthKey}`)).toBe(579);
});
it('does NOT debit tokens when upstream fails', async () => {
fetchState.response = () => upstreamErr(500, { error: 'overloaded' });
await invoke({ body: VALID_BODY });
const monthKey = new Date().toISOString().slice(0, 7);
expect(redisStore.get(`llm:tokens:agent-1:${monthKey}`)).toBeUndefined();
});
});
// ── request body validation + model allowlist ───────────────────────────
describe('/api/llm/anthropic — request body + model', () => {
it('rejects empty messages array', async () => {
const { status, body } = await invoke({ body: { messages: [] } });
expect(status).toBe(400);
expect(body.error).toBe('validation_error');
});
it('rejects model not on allowlist', async () => {
const { status, body } = await invoke({
body: { ...VALID_BODY, model: 'gpt-4' },
});
expect(status).toBe(400);
expect(body.error_description).toMatch(/not in allowlist/);
});
it('uses the model from the policy when caller omits one', async () => {
policyState.policy.brain.model = 'claude-sonnet-4-6';
await invoke({ body: VALID_BODY });
const sentBody = JSON.parse(fetchState.calls[0].init.body);
expect(sentBody.model).toBe('claude-sonnet-4-6');
});
it('caller-supplied allowlisted model overrides policy default', async () => {
policyState.policy.brain.model = 'claude-opus-4-6';
await invoke({ body: { ...VALID_BODY, model: 'claude-opus-4-7' } });
const sentBody = JSON.parse(fetchState.calls[0].init.body);
expect(sentBody.model).toBe('claude-opus-4-7');
});
});
// ── upstream response handling ──────────────────────────────────────────
describe('/api/llm/anthropic — upstream behaviour', () => {
it('proxies a successful upstream response unchanged', async () => {
const upstreamPayload = {
ok: true,
id: 'msg_123',
content: [{ type: 'text', text: 'hi' }],
usage: { input_tokens: 5, output_tokens: 7 },
};
fetchState.response = () => upstreamOk(upstreamPayload);
const { res, status, body } = await invoke({ body: VALID_BODY });
expect(status).toBe(200);
expect(body).toEqual(upstreamPayload);
expect(res.headers['content-type']).toMatch(/application\/json/);
});
it('sanitizes upstream errors into a generic 502 envelope (no leaking)', async () => {
fetchState.response = () =>
upstreamErr(429, {
error: { type: 'rate_limit', message: 'slow down', secret: 'leaked' },
});
const { status, body } = await invoke({ body: VALID_BODY });
expect(status).toBe(502);
expect(body.error).toBe('upstream_error');
// The original Anthropic body must NOT be forwarded.
expect(JSON.stringify(body)).not.toContain('leaked');
expect(JSON.stringify(body)).not.toContain('rate_limit');
});
it('records a usage event with input/output token counts', async () => {
fetchState.response = () =>
upstreamOk({ ok: true, usage: { input_tokens: 11, output_tokens: 22 } });
await invoke({ body: VALID_BODY });
// recordEvent runs synchronously (queueMicrotask in production code is in
// usage.js; the mock here pushes immediately).
expect(usageEvents).toHaveLength(1);
expect(usageEvents[0]).toMatchObject({
kind: 'llm',
tool: 'anthropic.messages',
agentId: 'agent-1',
status: 'ok',
});
expect(usageEvents[0].meta.input_tokens).toBe(11);
expect(usageEvents[0].meta.output_tokens).toBe(22);
});
});
// ── free-provider routing (Groq / OpenRouter) ──────────────────────────
//
// The browser sends Anthropic-shape bodies for every model; the proxy
// inspects `model`, picks the upstream URL + key, and translates request
// + response shapes both ways. These tests pin that translation contract.
describe('/api/llm/anthropic — free-provider routing', () => {
const SYSTEM = 'you are a helpful agent';
beforeEach(() => {
process.env.OPENROUTER_API_KEY = 'sk-or-test';
process.env.GROQ_API_KEY = 'gsk-test';
// OpenAI-shape response by default; individual tests can override.
fetchState.response = () =>
upstreamOk({
id: 'gen-1',
choices: [
{
message: { role: 'assistant', content: 'hi from free model' },
finish_reason: 'stop',
},
],
usage: { prompt_tokens: 7, completion_tokens: 9 },
});
});
it('routes meta-llama/*:free models to OpenRouter with bearer auth', async () => {
await invoke({
body: {
...VALID_BODY,
system: SYSTEM,
model: 'meta-llama/llama-3.3-70b-instruct:free',
},
});
const call = fetchState.calls[0];
expect(call.url).toBe('https://openrouter.ai/api/v1/chat/completions');
expect(call.init.headers.authorization).toBe('Bearer sk-or-test');
expect(call.init.headers['HTTP-Referer']).toBe('https://three.ws');
});
it('routes Groq model ids to api.groq.com', async () => {
await invoke({
body: { ...VALID_BODY, model: 'llama-3.3-70b-versatile' },
});
const call = fetchState.calls[0];
expect(call.url).toBe('https://api.groq.com/openai/v1/chat/completions');
expect(call.init.headers.authorization).toBe('Bearer gsk-test');
});
it('degrades through the fallback chain when the requested model has no key', async () => {
// The requested OpenRouter lane is unkeyed; the request must NOT 503 —
// it walks the chain and serves from the next configured lane.
delete process.env.OPENROUTER_API_KEY;
const { status } = await invoke({
body: { ...VALID_BODY, model: 'meta-llama/llama-3.3-70b-instruct:free' },
});
expect(status).toBe(200);
// Every attempted call skipped the unkeyed OpenRouter lane.
expect(fetchState.calls.every((c) => !c.url.includes('openrouter.ai'))).toBe(true);
});
it('degrades to a free lane when the keyed upstream rejects with 401 (dead key)', async () => {
// Account-level 4xx (dead/revoked key) must fall through to the next
// lane, not surface as a terminal error.
let first = true;
fetchState.response = () => {
if (first) {
first = false;
return upstreamErr(401, 'invalid x-api-key');
}
return upstreamOk({
id: 'gen-2',
choices: [
{ message: { role: 'assistant', content: 'rescued by free lane' }, finish_reason: 'stop' },
],
usage: { prompt_tokens: 3, completion_tokens: 4 },
});
};
const { status } = await invoke({
body: { ...VALID_BODY, model: 'meta-llama/llama-3.3-70b-instruct:free' },
});
expect(status).toBe(200);
expect(fetchState.calls.length).toBeGreaterThan(1);
});
it('returns 503 only when no lane in the chain is configured', async () => {
delete process.env.OPENROUTER_API_KEY;
delete process.env.GROQ_API_KEY;
delete process.env.NVIDIA_API_KEY;
const savedAnthropic = process.env.ANTHROPIC_API_KEY;
delete process.env.ANTHROPIC_API_KEY;
try {
const { status, body } = await invoke({
body: { ...VALID_BODY, model: 'meta-llama/llama-3.3-70b-instruct:free' },
});
expect(status).toBe(503);
expect(body.error).toBe('provider_unavailable');
expect(fetchState.calls).toHaveLength(0);
} finally {
process.env.ANTHROPIC_API_KEY = savedAnthropic;
}
});
it('translates Anthropic body shape → OpenAI body shape', async () => {
await invoke({
body: {
system: SYSTEM,
model: 'meta-llama/llama-3.3-70b-instruct:free',
max_tokens: 256,
temperature: 0.4,
messages: [
{ role: 'user', content: 'how are you?' },
{
role: 'assistant',
content: [
{ type: 'text', text: 'let me check' },
{
type: 'tool_use',
id: 'toolu_01',
name: 'getTime',
input: { tz: 'UTC' },
},
],
},
{
role: 'user',
content: [
{ type: 'tool_result', tool_use_id: 'toolu_01', content: '12:00' },
],
},
],
tools: [
{
name: 'getTime',
description: 'returns the current time',
input_schema: {
type: 'object',
properties: { tz: { type: 'string' } },
required: ['tz'],
},
},
],
},
});
const sent = JSON.parse(fetchState.calls[0].init.body);
expect(sent.model).toBe('meta-llama/llama-3.3-70b-instruct:free');
expect(sent.max_tokens).toBe(256);
expect(sent.temperature).toBe(0.4);
// System collapses into the first message.
expect(sent.messages[0]).toEqual({ role: 'system', content: SYSTEM });
expect(sent.messages[1]).toEqual({ role: 'user', content: 'how are you?' });
// Assistant turn with a tool_use → OpenAI tool_calls array.
expect(sent.messages[2]).toMatchObject({
role: 'assistant',
content: 'let me check',
tool_calls: [
{
id: 'toolu_01',
type: 'function',
function: { name: 'getTime', arguments: JSON.stringify({ tz: 'UTC' }) },
},
],
});
// tool_result block → role:"tool" message.
expect(sent.messages[3]).toEqual({
role: 'tool',
tool_call_id: 'toolu_01',
content: '12:00',
});
// Tool schema converted to OpenAI function-tool shape.
expect(sent.tools[0]).toEqual({
type: 'function',
function: {
name: 'getTime',
description: 'returns the current time',
parameters: {
type: 'object',
properties: { tz: { type: 'string' } },
required: ['tz'],
},
},
});
expect(sent.tool_choice).toBe('auto');
});
it('translates OpenAI-shape upstream response → Anthropic-shape body', async () => {
const { status, body } = await invoke({
body: { ...VALID_BODY, model: 'llama-3.3-70b-versatile' },
});
expect(status).toBe(200);
expect(body).toMatchObject({
type: 'message',
role: 'assistant',
model: 'llama-3.3-70b-versatile',
content: [{ type: 'text', text: 'hi from free model' }],
stop_reason: 'end_turn',
usage: { input_tokens: 7, output_tokens: 9 },
});
});
it('translates upstream tool_calls into Anthropic tool_use blocks', async () => {
fetchState.response = () =>
upstreamOk({
id: 'gen-2',
choices: [
{
message: {
role: 'assistant',
content: '',
tool_calls: [
{
id: 'call_1',
type: 'function',
function: { name: 'getTime', arguments: '{"tz":"UTC"}' },
},
],
},
finish_reason: 'tool_calls',
},
],
usage: { prompt_tokens: 4, completion_tokens: 5 },
});
const { body } = await invoke({
body: { ...VALID_BODY, model: 'meta-llama/llama-3.3-70b-instruct:free' },
});
expect(body.content).toEqual([
{ type: 'tool_use', id: 'call_1', name: 'getTime', input: { tz: 'UTC' } },
]);
expect(body.stop_reason).toBe('tool_use');
});
it('debits OpenAI-shape token usage (prompt + completion) onto the agent', async () => {
await invoke({
body: { ...VALID_BODY, model: 'meta-llama/llama-3.3-70b-instruct:free' },
});
const monthKey = new Date().toISOString().slice(0, 7);
expect(redisStore.get(`llm:tokens:agent-1:${monthKey}`)).toBe(16);
});
it('records usage with the upstream provider tag', async () => {
await invoke({
body: { ...VALID_BODY, model: 'meta-llama/llama-3.3-70b-instruct:free' },
});
expect(usageEvents[0]).toMatchObject({
kind: 'llm',
tool: 'openrouter.chat',
agentId: 'agent-1',
});
});
});