@venturekit-pro/ai 0.0.47 → 0.0.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +192 -0
- package/dist/agent-loop/index.d.ts +9 -0
- package/dist/agent-loop/index.d.ts.map +1 -0
- package/dist/agent-loop/index.js +7 -0
- package/dist/agent-loop/index.js.map +1 -0
- package/dist/agent-loop/registry.d.ts +85 -0
- package/dist/agent-loop/registry.d.ts.map +1 -0
- package/dist/agent-loop/registry.js +100 -0
- package/dist/agent-loop/registry.js.map +1 -0
- package/dist/agent-loop/run-agent.d.ts +37 -0
- package/dist/agent-loop/run-agent.d.ts.map +1 -0
- package/dist/agent-loop/run-agent.js +405 -0
- package/dist/agent-loop/run-agent.js.map +1 -0
- package/dist/agent-loop/tools.d.ts +10 -0
- package/dist/agent-loop/tools.d.ts.map +1 -0
- package/dist/agent-loop/tools.js +28 -0
- package/dist/agent-loop/tools.js.map +1 -0
- package/dist/agent-loop/types.d.ts +175 -0
- package/dist/agent-loop/types.d.ts.map +1 -0
- package/dist/agent-loop/types.js +8 -0
- package/dist/agent-loop/types.js.map +1 -0
- package/dist/chat/types.d.ts +4 -2
- package/dist/chat/types.d.ts.map +1 -1
- package/dist/cost/types.d.ts +15 -0
- package/dist/cost/types.d.ts.map +1 -1
- package/dist/cost-ledger/migrations/vk_ai_cost_0004_llm_gateway.sql +69 -0
- package/dist/cost-ledger/migrations/vk_ai_cost_0005_harness.sql +99 -0
- package/dist/cost-ledger/migrations/vk_ai_cost_0006_cache_write_prices.sql +17 -0
- package/dist/cost-ledger/migrations/vk_ai_cost_0007_harness_fan_out.sql +47 -0
- package/dist/gateway/errors.d.ts +24 -0
- package/dist/gateway/errors.d.ts.map +1 -0
- package/dist/gateway/errors.js +29 -0
- package/dist/gateway/errors.js.map +1 -0
- package/dist/gateway/gateway.d.ts +27 -0
- package/dist/gateway/gateway.d.ts.map +1 -0
- package/dist/gateway/gateway.js +503 -0
- package/dist/gateway/gateway.js.map +1 -0
- package/dist/gateway/index.d.ts +13 -0
- package/dist/gateway/index.d.ts.map +1 -0
- package/dist/gateway/index.js +10 -0
- package/dist/gateway/index.js.map +1 -0
- package/dist/gateway/policies.d.ts +32 -0
- package/dist/gateway/policies.d.ts.map +1 -0
- package/dist/gateway/policies.js +45 -0
- package/dist/gateway/policies.js.map +1 -0
- package/dist/gateway/postgres.d.ts +55 -0
- package/dist/gateway/postgres.d.ts.map +1 -0
- package/dist/gateway/postgres.js +127 -0
- package/dist/gateway/postgres.js.map +1 -0
- package/dist/gateway/pricing.d.ts +11 -0
- package/dist/gateway/pricing.d.ts.map +1 -0
- package/dist/gateway/pricing.js +47 -0
- package/dist/gateway/pricing.js.map +1 -0
- package/dist/gateway/routing.d.ts +25 -0
- package/dist/gateway/routing.d.ts.map +1 -0
- package/dist/gateway/routing.js +23 -0
- package/dist/gateway/routing.js.map +1 -0
- package/dist/gateway/types.d.ts +225 -0
- package/dist/gateway/types.d.ts.map +1 -0
- package/dist/gateway/types.js +7 -0
- package/dist/gateway/types.js.map +1 -0
- package/dist/generate/anthropic.d.ts +38 -0
- package/dist/generate/anthropic.d.ts.map +1 -0
- package/dist/generate/anthropic.js +446 -0
- package/dist/generate/anthropic.js.map +1 -0
- package/dist/generate/client.d.ts +9 -0
- package/dist/generate/client.d.ts.map +1 -0
- package/dist/generate/client.js +44 -0
- package/dist/generate/client.js.map +1 -0
- package/dist/generate/google.d.ts +39 -0
- package/dist/generate/google.d.ts.map +1 -0
- package/dist/generate/google.js +372 -0
- package/dist/generate/google.js.map +1 -0
- package/dist/generate/http.d.ts +48 -0
- package/dist/generate/http.d.ts.map +1 -0
- package/dist/generate/http.js +180 -0
- package/dist/generate/http.js.map +1 -0
- package/dist/generate/index.d.ts +13 -0
- package/dist/generate/index.d.ts.map +1 -0
- package/dist/generate/index.js +12 -0
- package/dist/generate/index.js.map +1 -0
- package/dist/generate/openai.d.ts +29 -0
- package/dist/generate/openai.d.ts.map +1 -0
- package/dist/generate/openai.js +314 -0
- package/dist/generate/openai.js.map +1 -0
- package/dist/generate/perplexity.d.ts +27 -0
- package/dist/generate/perplexity.d.ts.map +1 -0
- package/dist/generate/perplexity.js +208 -0
- package/dist/generate/perplexity.js.map +1 -0
- package/dist/generate/types.d.ts +253 -0
- package/dist/generate/types.d.ts.map +1 -0
- package/dist/generate/types.js +19 -0
- package/dist/generate/types.js.map +1 -0
- package/dist/harness/define.d.ts +15 -0
- package/dist/harness/define.d.ts.map +1 -0
- package/dist/harness/define.js +38 -0
- package/dist/harness/define.js.map +1 -0
- package/dist/harness/harness.d.ts +29 -0
- package/dist/harness/harness.d.ts.map +1 -0
- package/dist/harness/harness.js +1287 -0
- package/dist/harness/harness.js.map +1 -0
- package/dist/harness/index.d.ts +14 -0
- package/dist/harness/index.d.ts.map +1 -0
- package/dist/harness/index.js +10 -0
- package/dist/harness/index.js.map +1 -0
- package/dist/harness/memory-store.d.ts +8 -0
- package/dist/harness/memory-store.d.ts.map +1 -0
- package/dist/harness/memory-store.js +270 -0
- package/dist/harness/memory-store.js.map +1 -0
- package/dist/harness/postgres-store.d.ts +12 -0
- package/dist/harness/postgres-store.d.ts.map +1 -0
- package/dist/harness/postgres-store.js +368 -0
- package/dist/harness/postgres-store.js.map +1 -0
- package/dist/harness/scheduler.d.ts +12 -0
- package/dist/harness/scheduler.d.ts.map +1 -0
- package/dist/harness/scheduler.js +23 -0
- package/dist/harness/scheduler.js.map +1 -0
- package/dist/harness/store.d.ts +172 -0
- package/dist/harness/store.d.ts.map +1 -0
- package/dist/harness/store.js +21 -0
- package/dist/harness/store.js.map +1 -0
- package/dist/harness/types.d.ts +312 -0
- package/dist/harness/types.d.ts.map +1 -0
- package/dist/harness/types.js +11 -0
- package/dist/harness/types.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -1
- package/dist/model-catalog/crud.d.ts.map +1 -1
- package/dist/model-catalog/crud.js +33 -2
- package/dist/model-catalog/crud.js.map +1 -1
- package/dist/model-catalog/types.d.ts +20 -0
- package/dist/model-catalog/types.d.ts.map +1 -1
- package/dist/tracing/index.d.ts +68 -0
- package/dist/tracing/index.d.ts.map +1 -0
- package/dist/tracing/index.js +72 -0
- package/dist/tracing/index.js.map +1 -0
- package/dist/tracing/recording.d.ts +23 -0
- package/dist/tracing/recording.d.ts.map +1 -0
- package/dist/tracing/recording.js +49 -0
- package/dist/tracing/recording.js.map +1 -0
- package/package.json +7 -4
- package/src/cost-ledger/migrations/vk_ai_cost_0004_llm_gateway.sql +69 -0
- package/src/cost-ledger/migrations/vk_ai_cost_0005_harness.sql +99 -0
- package/src/cost-ledger/migrations/vk_ai_cost_0006_cache_write_prices.sql +17 -0
- package/src/cost-ledger/migrations/vk_ai_cost_0007_harness_fan_out.sql +47 -0
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `runAgent` — a bounded tool-use loop on the gateway's `generate`.
|
|
3
|
+
*
|
|
4
|
+
* each step: model call (tools + native web search) → run the function
|
|
5
|
+
* tools it asked for, in order → answer them → next step
|
|
6
|
+
*
|
|
7
|
+
* Limits are soft: when steps, tool calls, cost or time run out, the
|
|
8
|
+
* model gets one last call without tool use and is told to answer from
|
|
9
|
+
* what it has (`stopReason: 'budget'`). A hard stop (cancellation, the
|
|
10
|
+
* wall clock passing its limit mid-call, a gateway error) throws an
|
|
11
|
+
* `AgentRunError` carrying the conversation and spend so far.
|
|
12
|
+
*
|
|
13
|
+
* Every step is one gateway call with a stable idempotency key
|
|
14
|
+
* (`<key>:s<step>`); tool errors go back to the model as error results
|
|
15
|
+
* instead of failing the run.
|
|
16
|
+
*/
|
|
17
|
+
import { round8 } from '../chat/cost-util.js';
|
|
18
|
+
import { GenerateAbortedError } from '../generate/http.js';
|
|
19
|
+
import { attributes, inSpan, SPAN_KIND } from '../tracing/index.js';
|
|
20
|
+
import { isToolResult } from './tools.js';
|
|
21
|
+
const DEFAULTS = { maxSteps: 10, maxToolCalls: 20, timeoutMs: 5 * 60_000 };
|
|
22
|
+
const TOOL_TIMEOUT_MS = 60_000;
|
|
23
|
+
const MAX_RESULT_CHARS = 20_000;
|
|
24
|
+
/** Wrap up once this share of the wall-clock budget is gone, leaving time for the final answer. */
|
|
25
|
+
const TIME_WRAP_UP_SHARE = 0.85;
|
|
26
|
+
const WRAP_UP = {
|
|
27
|
+
steps: 'You have used all the steps for this task.',
|
|
28
|
+
tool_calls: 'You have used all the tool calls for this task.',
|
|
29
|
+
cost: 'You have used the budget for this task.',
|
|
30
|
+
time: 'You are out of time for this task.',
|
|
31
|
+
};
|
|
32
|
+
/** A hard stop, with the conversation and spend so far. `name` stays `AbortError` on cancellation. */
|
|
33
|
+
export class AgentRunError extends Error {
|
|
34
|
+
snapshot;
|
|
35
|
+
constructor(message, cause, snapshot) {
|
|
36
|
+
super(message, { cause });
|
|
37
|
+
this.name =
|
|
38
|
+
cause instanceof Error && cause.name === 'AbortError' ? 'AbortError' : 'AgentRunError';
|
|
39
|
+
this.snapshot = snapshot;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export async function runAgent(options) {
|
|
43
|
+
const name = options.name ?? 'agent';
|
|
44
|
+
return inSpan(options.tracer, `invoke_agent ${name}`, {
|
|
45
|
+
kind: SPAN_KIND.INTERNAL,
|
|
46
|
+
attributes: attributes({
|
|
47
|
+
'gen_ai.operation.name': 'invoke_agent',
|
|
48
|
+
'gen_ai.agent.name': name,
|
|
49
|
+
'gen_ai.request.model': options.request.model,
|
|
50
|
+
'vk.ai.tier': options.request.tier,
|
|
51
|
+
}),
|
|
52
|
+
}, async (span) => {
|
|
53
|
+
const result = await loop(options);
|
|
54
|
+
span?.setAttributes(attributes({
|
|
55
|
+
'vk.ai.steps': result.steps,
|
|
56
|
+
'vk.ai.tool_calls': result.toolCalls,
|
|
57
|
+
'vk.ai.stop_reason': result.stopReason,
|
|
58
|
+
'vk.ai.limit_hit': result.limitHit,
|
|
59
|
+
'gen_ai.usage.input_tokens': result.usage.tokensIn,
|
|
60
|
+
'gen_ai.usage.output_tokens': result.usage.tokensOut,
|
|
61
|
+
'vk.ai.cost_usd': result.cost?.costUsd,
|
|
62
|
+
}));
|
|
63
|
+
return result;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
async function loop(options) {
|
|
67
|
+
const startedAt = Date.now();
|
|
68
|
+
const limits = { ...DEFAULTS, ...options.limits };
|
|
69
|
+
const tools = new Map((options.tools ?? []).map((t) => [t.definition.name, t]));
|
|
70
|
+
const toolDefs = (options.tools ?? []).map((t) => t.definition);
|
|
71
|
+
const messages = [...options.messages];
|
|
72
|
+
// The wall clock is a hard stop too: abort the in-flight call when it passes.
|
|
73
|
+
const controller = new AbortController();
|
|
74
|
+
const onParentAbort = () => controller.abort();
|
|
75
|
+
options.signal?.addEventListener('abort', onParentAbort, { once: true });
|
|
76
|
+
if (options.signal?.aborted)
|
|
77
|
+
controller.abort();
|
|
78
|
+
const hardTimer = setTimeout(() => controller.abort(), limits.timeoutMs);
|
|
79
|
+
const usage = {
|
|
80
|
+
tokensIn: null,
|
|
81
|
+
cachedTokensIn: null,
|
|
82
|
+
tokensOut: null,
|
|
83
|
+
webSearches: 0,
|
|
84
|
+
};
|
|
85
|
+
let costUsd = 0;
|
|
86
|
+
let costMicroUsd = 0;
|
|
87
|
+
let priced = false;
|
|
88
|
+
const toolCost = { costUsd: 0, costMicroUsd: 0 };
|
|
89
|
+
let toolPriced = false;
|
|
90
|
+
let steps = 0;
|
|
91
|
+
let toolCallCount = 0;
|
|
92
|
+
const searchQueries = [];
|
|
93
|
+
const searchResults = [];
|
|
94
|
+
const citations = [];
|
|
95
|
+
const callIds = [];
|
|
96
|
+
const emit = async (event) => {
|
|
97
|
+
if (options.onEvent)
|
|
98
|
+
await options.onEvent(event);
|
|
99
|
+
};
|
|
100
|
+
const snapshot = () => ({
|
|
101
|
+
messages: [...messages],
|
|
102
|
+
usage: { ...usage },
|
|
103
|
+
cost: priced ? { costUsd, costMicroUsd } : null,
|
|
104
|
+
toolCost: toolPriced ? { ...toolCost } : null,
|
|
105
|
+
steps,
|
|
106
|
+
toolCalls: toolCallCount,
|
|
107
|
+
});
|
|
108
|
+
/** What a tool spent counts like a model call: usage, cost and the cost limit. */
|
|
109
|
+
const addToolSpend = (outcome) => {
|
|
110
|
+
if (outcome.usage) {
|
|
111
|
+
const u = outcome.usage;
|
|
112
|
+
addUsage(usage, {
|
|
113
|
+
tokensIn: u.tokensIn ?? null,
|
|
114
|
+
cachedTokensIn: u.cachedTokensIn ?? null,
|
|
115
|
+
tokensOut: u.tokensOut ?? null,
|
|
116
|
+
webSearches: u.webSearches ?? 0,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
if (outcome.cost) {
|
|
120
|
+
priced = toolPriced = true;
|
|
121
|
+
costUsd = round8(costUsd + outcome.cost.costUsd);
|
|
122
|
+
costMicroUsd += outcome.cost.costMicroUsd;
|
|
123
|
+
toolCost.costUsd = round8(toolCost.costUsd + outcome.cost.costUsd);
|
|
124
|
+
toolCost.costMicroUsd += outcome.cost.costMicroUsd;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
const fail = (err) => {
|
|
128
|
+
if (err instanceof AgentRunError)
|
|
129
|
+
throw err;
|
|
130
|
+
const timedOut = controller.signal.aborted &&
|
|
131
|
+
!options.signal?.aborted &&
|
|
132
|
+
Date.now() - startedAt >= limits.timeoutMs;
|
|
133
|
+
const message = timedOut
|
|
134
|
+
? `The agent ran past its ${limits.timeoutMs} ms limit`
|
|
135
|
+
: err instanceof Error
|
|
136
|
+
? err.message
|
|
137
|
+
: String(err);
|
|
138
|
+
throw new AgentRunError(message, err, snapshot());
|
|
139
|
+
};
|
|
140
|
+
const limitHit = () => {
|
|
141
|
+
if (steps >= limits.maxSteps)
|
|
142
|
+
return 'steps';
|
|
143
|
+
if (toolCallCount >= limits.maxToolCalls)
|
|
144
|
+
return 'tool_calls';
|
|
145
|
+
if (limits.maxCostUsd !== undefined && costUsd >= limits.maxCostUsd)
|
|
146
|
+
return 'cost';
|
|
147
|
+
if (Date.now() - startedAt >= limits.timeoutMs * TIME_WRAP_UP_SHARE)
|
|
148
|
+
return 'time';
|
|
149
|
+
return null;
|
|
150
|
+
};
|
|
151
|
+
const call = async (wrapUp) => {
|
|
152
|
+
if (controller.signal.aborted)
|
|
153
|
+
throw new GenerateAbortedError();
|
|
154
|
+
steps += 1;
|
|
155
|
+
await emit({ type: 'step.started', step: steps });
|
|
156
|
+
const request = {
|
|
157
|
+
...options.request,
|
|
158
|
+
messages,
|
|
159
|
+
...(toolDefs.length
|
|
160
|
+
? { tools: toolDefs, ...(wrapUp ? { toolChoice: 'none' } : {}) }
|
|
161
|
+
: {}),
|
|
162
|
+
signal: controller.signal,
|
|
163
|
+
...(options.idempotencyKey ? { idempotencyKey: `${options.idempotencyKey}:s${steps}` } : {}),
|
|
164
|
+
};
|
|
165
|
+
let result;
|
|
166
|
+
if (options.stream && options.client.stream) {
|
|
167
|
+
// Cooperative cancellation between chunks, on top of the aborted HTTP call.
|
|
168
|
+
for await (const event of options.client.stream(request)) {
|
|
169
|
+
if (controller.signal.aborted)
|
|
170
|
+
throw new GenerateAbortedError();
|
|
171
|
+
if (event.type === 'text')
|
|
172
|
+
await emit({ type: 'text.delta', step: steps, delta: event.delta });
|
|
173
|
+
else if (event.type === 'done')
|
|
174
|
+
result = event.result;
|
|
175
|
+
}
|
|
176
|
+
if (!result)
|
|
177
|
+
throw new Error('[ai/agent] the stream ended without a result');
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
result = await options.client.generate(request);
|
|
181
|
+
}
|
|
182
|
+
addUsage(usage, result.usage);
|
|
183
|
+
if (result.cost) {
|
|
184
|
+
priced = true;
|
|
185
|
+
costUsd = round8(costUsd + result.cost.costUsd);
|
|
186
|
+
costMicroUsd += result.cost.costMicroUsd;
|
|
187
|
+
}
|
|
188
|
+
if (result.callId)
|
|
189
|
+
callIds.push(result.callId);
|
|
190
|
+
searchQueries.push(...(result.searchQueries ?? []));
|
|
191
|
+
searchResults.push(...(result.searchResults ?? []));
|
|
192
|
+
citations.push(...(result.citations ?? []));
|
|
193
|
+
const stepResult = {
|
|
194
|
+
text: result.text ?? '',
|
|
195
|
+
toolCalls: result.toolCalls ?? [],
|
|
196
|
+
citations: result.citations ?? [],
|
|
197
|
+
searchResults: result.searchResults ?? [],
|
|
198
|
+
searchQueries: result.searchQueries ?? [],
|
|
199
|
+
finishReason: result.finishReason ?? null,
|
|
200
|
+
usage: result.usage,
|
|
201
|
+
cost: result.cost
|
|
202
|
+
? { costUsd: result.cost.costUsd, costMicroUsd: result.cost.costMicroUsd }
|
|
203
|
+
: null,
|
|
204
|
+
...(result.modelKey ? { modelKey: result.modelKey } : {}),
|
|
205
|
+
...(result.callId ? { callId: result.callId } : {}),
|
|
206
|
+
};
|
|
207
|
+
await emit({ type: 'step.completed', step: steps, result: stepResult });
|
|
208
|
+
return result;
|
|
209
|
+
};
|
|
210
|
+
const finish = (text, json, stopReason, hit) => ({
|
|
211
|
+
text,
|
|
212
|
+
...(json !== undefined ? { json } : {}),
|
|
213
|
+
stopReason,
|
|
214
|
+
...(hit ? { limitHit: hit } : {}),
|
|
215
|
+
steps,
|
|
216
|
+
toolCalls: toolCallCount,
|
|
217
|
+
messages,
|
|
218
|
+
searchQueries: [...new Set(searchQueries)],
|
|
219
|
+
searchResults: dedupeByUrl(searchResults),
|
|
220
|
+
citations: dedupeByUrl(citations),
|
|
221
|
+
usage,
|
|
222
|
+
cost: priced ? { costUsd, costMicroUsd } : null,
|
|
223
|
+
toolCost: toolPriced ? { ...toolCost } : null,
|
|
224
|
+
callIds,
|
|
225
|
+
durationMs: Date.now() - startedAt,
|
|
226
|
+
});
|
|
227
|
+
try {
|
|
228
|
+
for (;;) {
|
|
229
|
+
const hit = limitHit();
|
|
230
|
+
if (hit) {
|
|
231
|
+
await emit({ type: 'wrap_up', step: steps, reason: hit });
|
|
232
|
+
messages.push({
|
|
233
|
+
role: 'user',
|
|
234
|
+
content: `${WRAP_UP[hit]} ${options.wrapUpMessage ?? 'Do not call any more tools. Give your final answer now, from what you already have.'}`,
|
|
235
|
+
});
|
|
236
|
+
const last = await call(true);
|
|
237
|
+
messages.push({ role: 'assistant', content: last.text ?? '' });
|
|
238
|
+
return finish(last.text ?? '', last.json, last.finishReason === 'length' ? 'length' : 'budget', hit);
|
|
239
|
+
}
|
|
240
|
+
const result = await call(false);
|
|
241
|
+
const calls = result.toolCalls ?? [];
|
|
242
|
+
if (result.finishReason === 'pause_turn') {
|
|
243
|
+
// A long server-side tool turn (web search): send it back to continue.
|
|
244
|
+
messages.push({
|
|
245
|
+
role: 'assistant',
|
|
246
|
+
content: result.text ?? '',
|
|
247
|
+
...(result.providerState ? { providerState: result.providerState } : {}),
|
|
248
|
+
});
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
if (calls.length === 0) {
|
|
252
|
+
messages.push({ role: 'assistant', content: result.text ?? '' });
|
|
253
|
+
return finish(result.text ?? '', result.json, result.finishReason === 'length' ? 'length' : 'done');
|
|
254
|
+
}
|
|
255
|
+
messages.push({
|
|
256
|
+
role: 'assistant',
|
|
257
|
+
content: result.text ?? '',
|
|
258
|
+
toolCalls: calls,
|
|
259
|
+
...(result.providerState ? { providerState: result.providerState } : {}),
|
|
260
|
+
});
|
|
261
|
+
for (const toolCall of calls) {
|
|
262
|
+
if (toolCallCount >= limits.maxToolCalls) {
|
|
263
|
+
messages.push({
|
|
264
|
+
role: 'tool',
|
|
265
|
+
toolCallId: toolCall.id,
|
|
266
|
+
name: toolCall.name,
|
|
267
|
+
content: 'Not run: the tool-call limit for this task is reached.',
|
|
268
|
+
isError: true,
|
|
269
|
+
});
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
toolCallCount += 1;
|
|
273
|
+
const outcome = await runTool(tools.get(toolCall.name), toolCall, steps, controller.signal, emit, options.tracer);
|
|
274
|
+
addToolSpend(outcome);
|
|
275
|
+
messages.push({
|
|
276
|
+
role: 'tool',
|
|
277
|
+
toolCallId: toolCall.id,
|
|
278
|
+
name: toolCall.name,
|
|
279
|
+
content: outcome.content,
|
|
280
|
+
...(outcome.isError ? { isError: true } : {}),
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
catch (err) {
|
|
286
|
+
return fail(err);
|
|
287
|
+
}
|
|
288
|
+
finally {
|
|
289
|
+
clearTimeout(hardTimer);
|
|
290
|
+
options.signal?.removeEventListener('abort', onParentAbort);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
function runTool(tool, call, step, signal, emit, tracer) {
|
|
294
|
+
return inSpan(tracer, `execute_tool ${call.name}`, {
|
|
295
|
+
kind: SPAN_KIND.INTERNAL,
|
|
296
|
+
attributes: {
|
|
297
|
+
'gen_ai.operation.name': 'execute_tool',
|
|
298
|
+
'gen_ai.tool.name': call.name,
|
|
299
|
+
'gen_ai.tool.call.id': call.id,
|
|
300
|
+
'gen_ai.tool.type': 'function',
|
|
301
|
+
},
|
|
302
|
+
}, async (span) => {
|
|
303
|
+
const result = await executeTool(tool, call, step, signal, emit);
|
|
304
|
+
span?.setAttributes(attributes({
|
|
305
|
+
'vk.ai.tool.is_error': result.isError,
|
|
306
|
+
'vk.ai.tool.duration_ms': result.durationMs,
|
|
307
|
+
'vk.ai.cost_usd': result.cost?.costUsd,
|
|
308
|
+
}));
|
|
309
|
+
return result;
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
async function executeTool(tool, call, step, signal, emit) {
|
|
313
|
+
const started = Date.now();
|
|
314
|
+
await emit({ type: 'tool.started', step, call });
|
|
315
|
+
let content;
|
|
316
|
+
let isError = false;
|
|
317
|
+
let data;
|
|
318
|
+
let spend = {};
|
|
319
|
+
if (!tool) {
|
|
320
|
+
content = `Unknown tool '${call.name}'. Use one of the tools you were given.`;
|
|
321
|
+
isError = true;
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
try {
|
|
325
|
+
const args = tool.parse(call.arguments);
|
|
326
|
+
const output = await withTimeout((s) => tool.execute(args, { signal: s, callId: call.id, step }), tool.timeoutMs ?? TOOL_TIMEOUT_MS, signal);
|
|
327
|
+
if (isToolResult(output)) {
|
|
328
|
+
content = stringify(output.content);
|
|
329
|
+
isError = output.isError === true;
|
|
330
|
+
data = output.data;
|
|
331
|
+
spend = {
|
|
332
|
+
...(output.cost ? { cost: output.cost } : {}),
|
|
333
|
+
...(output.usage ? { usage: output.usage } : {}),
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
content = stringify(output);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
catch (err) {
|
|
341
|
+
if (signal.aborted)
|
|
342
|
+
throw err;
|
|
343
|
+
content = `The tool failed: ${err instanceof Error ? err.message : String(err)}`;
|
|
344
|
+
isError = true;
|
|
345
|
+
}
|
|
346
|
+
const max = tool.maxResultChars ?? MAX_RESULT_CHARS;
|
|
347
|
+
if (content.length > max)
|
|
348
|
+
content = `${content.slice(0, max)}…[truncated]`;
|
|
349
|
+
}
|
|
350
|
+
const result = {
|
|
351
|
+
callId: call.id,
|
|
352
|
+
name: call.name,
|
|
353
|
+
arguments: call.arguments,
|
|
354
|
+
content,
|
|
355
|
+
isError,
|
|
356
|
+
durationMs: Date.now() - started,
|
|
357
|
+
...(data !== undefined ? { data } : {}),
|
|
358
|
+
...spend,
|
|
359
|
+
};
|
|
360
|
+
await emit({ type: 'tool.completed', step, result });
|
|
361
|
+
return result;
|
|
362
|
+
}
|
|
363
|
+
async function withTimeout(run, timeoutMs, parent) {
|
|
364
|
+
const controller = new AbortController();
|
|
365
|
+
const onAbort = () => controller.abort();
|
|
366
|
+
parent.addEventListener('abort', onAbort, { once: true });
|
|
367
|
+
let timer;
|
|
368
|
+
const timeout = new Promise((_, reject) => {
|
|
369
|
+
timer = setTimeout(() => {
|
|
370
|
+
controller.abort();
|
|
371
|
+
reject(new Error(`timed out after ${Math.round(timeoutMs / 1000)} s`));
|
|
372
|
+
}, timeoutMs);
|
|
373
|
+
});
|
|
374
|
+
try {
|
|
375
|
+
return await Promise.race([run(controller.signal), timeout]);
|
|
376
|
+
}
|
|
377
|
+
finally {
|
|
378
|
+
clearTimeout(timer);
|
|
379
|
+
parent.removeEventListener('abort', onAbort);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
function stringify(value) {
|
|
383
|
+
if (typeof value === 'string')
|
|
384
|
+
return value;
|
|
385
|
+
try {
|
|
386
|
+
return JSON.stringify(value ?? null);
|
|
387
|
+
}
|
|
388
|
+
catch {
|
|
389
|
+
return String(value);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
function addUsage(total, add) {
|
|
393
|
+
if (!add)
|
|
394
|
+
return;
|
|
395
|
+
const sum = (a, b) => a === null && b === null ? null : (a ?? 0) + (b ?? 0);
|
|
396
|
+
total.tokensIn = sum(total.tokensIn, add.tokensIn);
|
|
397
|
+
total.cachedTokensIn = sum(total.cachedTokensIn, add.cachedTokensIn);
|
|
398
|
+
total.tokensOut = sum(total.tokensOut, add.tokensOut);
|
|
399
|
+
total.webSearches += add.webSearches ?? 0;
|
|
400
|
+
}
|
|
401
|
+
function dedupeByUrl(items) {
|
|
402
|
+
const seen = new Set();
|
|
403
|
+
return items.filter((i) => (seen.has(i.url) ? false : (seen.add(i.url), true)));
|
|
404
|
+
}
|
|
405
|
+
//# sourceMappingURL=run-agent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run-agent.js","sourceRoot":"","sources":["../../src/agent-loop/run-agent.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAE9C,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAQ3D,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,SAAS,EAA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,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;IACxD,KAAK,CAAC,QAAQ,GAAG,GAAG,CAAC,KAAK,CAAC,QAAQ,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAC;IACnD,KAAK,CAAC,cAAc,GAAG,GAAG,CAAC,KAAK,CAAC,cAAc,EAAE,GAAG,CAAC,cAAc,CAAC,CAAC;IACrE,KAAK,CAAC,SAAS,GAAG,GAAG,CAAC,KAAK,CAAC,SAAS,EAAE,GAAG,CAAC,SAAS,CAAC,CAAC;IACtD,KAAK,CAAC,WAAW,IAAI,GAAG,CAAC,WAAW,IAAI,CAAC,CAAC;AAC5C,CAAC;AAED,SAAS,WAAW,CAA4B,KAAU;IACxD,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAC;IAC/B,OAAO,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;AAClF,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { AgentTool, ToolResult } from './types.js';
|
|
2
|
+
/** Declare a tool. Identity at runtime; keeps the argument type through `parse` → `execute`. */
|
|
3
|
+
export declare function defineAgentTool<A>(tool: AgentTool<A>): AgentTool<A>;
|
|
4
|
+
/**
|
|
5
|
+
* A tool result with an error flag, caller-only `data` or what the tool
|
|
6
|
+
* spent. A tool may also return any plain value, sent to the model as JSON.
|
|
7
|
+
*/
|
|
8
|
+
export declare function toolResult(result: ToolResult): ToolResult;
|
|
9
|
+
export declare function isToolResult(value: unknown): value is ToolResult;
|
|
10
|
+
//# sourceMappingURL=tools.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../../src/agent-loop/tools.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAIxD,gGAAgG;AAChG,wBAAgB,eAAe,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAEnE;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,UAAU,GAAG,UAAU,CAQzD;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,UAAU,CAMhE"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const TOOL_RESULT = Symbol.for('venturekit.ai.toolResult');
|
|
2
|
+
/** Declare a tool. Identity at runtime; keeps the argument type through `parse` → `execute`. */
|
|
3
|
+
export function defineAgentTool(tool) {
|
|
4
|
+
return tool;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* A tool result with an error flag, caller-only `data` or what the tool
|
|
8
|
+
* spent. A tool may also return any plain value, sent to the model as JSON.
|
|
9
|
+
*/
|
|
10
|
+
export function toolResult(result) {
|
|
11
|
+
const out = { content: result.content };
|
|
12
|
+
if (result.isError !== undefined)
|
|
13
|
+
out.isError = result.isError;
|
|
14
|
+
if (result.data !== undefined)
|
|
15
|
+
out.data = result.data;
|
|
16
|
+
if (result.cost)
|
|
17
|
+
out.cost = result.cost;
|
|
18
|
+
if (result.usage)
|
|
19
|
+
out.usage = result.usage;
|
|
20
|
+
Object.defineProperty(out, TOOL_RESULT, { value: true, enumerable: false });
|
|
21
|
+
return out;
|
|
22
|
+
}
|
|
23
|
+
export function isToolResult(value) {
|
|
24
|
+
return (typeof value === 'object' &&
|
|
25
|
+
value !== null &&
|
|
26
|
+
value[TOOL_RESULT] === true);
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=tools.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.js","sourceRoot":"","sources":["../../src/agent-loop/tools.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,GAAG,MAAM,CAAC,GAAG,CAAC,0BAA0B,CAAC,CAAC;AAE3D,gGAAgG;AAChG,MAAM,UAAU,eAAe,CAAI,IAAkB;IACnD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,UAAU,CAAC,MAAkB;IAC3C,MAAM,GAAG,GAAe,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE,CAAC;IACpD,IAAI,MAAM,CAAC,OAAO,KAAK,SAAS;QAAE,GAAG,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;IAC/D,IAAI,MAAM,CAAC,IAAI,KAAK,SAAS;QAAE,GAAG,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;IACtD,IAAI,MAAM,CAAC,IAAI;QAAE,GAAG,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;IACxC,IAAI,MAAM,CAAC,KAAK;QAAE,GAAG,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;IAC3C,MAAM,CAAC,cAAc,CAAC,GAAG,EAAE,WAAW,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;IAC5E,OAAO,GAAG,CAAC;AACb,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,KAAc;IACzC,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACb,KAAiC,CAAC,WAAW,CAAC,KAAK,IAAI,CACzD,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent loop types — a provider-neutral, bounded tool-use loop on top of
|
|
3
|
+
* the gateway's `generate`: the model calls function tools (and the
|
|
4
|
+
* vendor's native web search) until it answers, or until a step, tool
|
|
5
|
+
* call, cost or time limit asks it to wrap up.
|
|
6
|
+
*/
|
|
7
|
+
import type { GatewayClient, GatewayRequest } from '../gateway/types.js';
|
|
8
|
+
import type { Citation, GenerateMessage, GenerateUsage, SearchResult, ToolCall, ToolDefinition } from '../generate/types.js';
|
|
9
|
+
import type { AiTracer } from '../tracing/index.js';
|
|
10
|
+
export interface AgentToolContext {
|
|
11
|
+
/** Aborts when the run is cancelled or the tool times out. */
|
|
12
|
+
signal: AbortSignal;
|
|
13
|
+
callId: string;
|
|
14
|
+
step: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* What a tool spent itself (a model call it made, a paid API). Added to
|
|
18
|
+
* the agent's usage, cost and cost limit, and reported apart as `toolCost`.
|
|
19
|
+
*/
|
|
20
|
+
export interface ToolSpend {
|
|
21
|
+
cost?: {
|
|
22
|
+
costUsd: number;
|
|
23
|
+
costMicroUsd: number;
|
|
24
|
+
} | null;
|
|
25
|
+
usage?: Partial<GenerateUsage>;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* What a tool may return: a plain value (sent to the model as JSON) or a
|
|
29
|
+
* `toolResult(...)` — content for the model, an error flag, `data` kept
|
|
30
|
+
* for the caller (events, audit) but never shown to the model, and what
|
|
31
|
+
* the tool spent.
|
|
32
|
+
*/
|
|
33
|
+
export interface ToolResult extends ToolSpend {
|
|
34
|
+
content: unknown;
|
|
35
|
+
isError?: boolean;
|
|
36
|
+
data?: unknown;
|
|
37
|
+
}
|
|
38
|
+
export interface AgentTool<A = unknown> {
|
|
39
|
+
definition: ToolDefinition;
|
|
40
|
+
/** Validate / normalize the model's arguments; throw to answer the model with the error. */
|
|
41
|
+
parse(args: Record<string, unknown>): A;
|
|
42
|
+
execute(args: A, ctx: AgentToolContext): Promise<unknown>;
|
|
43
|
+
/** Per-call timeout. Default 60 s. */
|
|
44
|
+
timeoutMs?: number;
|
|
45
|
+
/** Longest result sent back to the model. Default 20,000 characters. */
|
|
46
|
+
maxResultChars?: number;
|
|
47
|
+
}
|
|
48
|
+
export interface AgentLimits {
|
|
49
|
+
/** Model calls (steps). Default 10. */
|
|
50
|
+
maxSteps?: number;
|
|
51
|
+
/** Tool calls across the run. Default 20. */
|
|
52
|
+
maxToolCalls?: number;
|
|
53
|
+
/** Wall clock. Default 5 minutes. */
|
|
54
|
+
timeoutMs?: number;
|
|
55
|
+
/** Spend across the run: its model calls plus what its tools report. */
|
|
56
|
+
maxCostUsd?: number;
|
|
57
|
+
}
|
|
58
|
+
/** Which soft limit asked the agent to wrap up. */
|
|
59
|
+
export type AgentLimitHit = 'steps' | 'tool_calls' | 'cost' | 'time';
|
|
60
|
+
/**
|
|
61
|
+
* `done` — the model answered on its own; `budget` — a limit made it wrap
|
|
62
|
+
* up (it still answered); `length` — the final answer hit `maxTokens`.
|
|
63
|
+
*/
|
|
64
|
+
export type AgentStopReason = 'done' | 'budget' | 'length';
|
|
65
|
+
export interface AgentStepResult {
|
|
66
|
+
text: string;
|
|
67
|
+
toolCalls: ToolCall[];
|
|
68
|
+
citations: Citation[];
|
|
69
|
+
searchResults: SearchResult[];
|
|
70
|
+
searchQueries: string[];
|
|
71
|
+
finishReason: string | null;
|
|
72
|
+
usage: GenerateUsage;
|
|
73
|
+
cost: {
|
|
74
|
+
costUsd: number;
|
|
75
|
+
costMicroUsd: number;
|
|
76
|
+
} | null;
|
|
77
|
+
modelKey?: string;
|
|
78
|
+
callId?: string;
|
|
79
|
+
}
|
|
80
|
+
export interface AgentToolCallResult {
|
|
81
|
+
callId: string;
|
|
82
|
+
name: string;
|
|
83
|
+
arguments: Record<string, unknown>;
|
|
84
|
+
/** What the model was sent (JSON or text, truncated to `maxResultChars`). */
|
|
85
|
+
content: string;
|
|
86
|
+
isError: boolean;
|
|
87
|
+
durationMs: number;
|
|
88
|
+
/** `toolResult({ data })` — for the caller only. */
|
|
89
|
+
data?: unknown;
|
|
90
|
+
/** `toolResult({ cost, usage })` — what the tool spent. */
|
|
91
|
+
cost?: {
|
|
92
|
+
costUsd: number;
|
|
93
|
+
costMicroUsd: number;
|
|
94
|
+
} | null;
|
|
95
|
+
usage?: Partial<GenerateUsage>;
|
|
96
|
+
}
|
|
97
|
+
export type AgentEvent = {
|
|
98
|
+
type: 'step.started';
|
|
99
|
+
step: number;
|
|
100
|
+
}
|
|
101
|
+
/** Streamed text of the step (`stream: true`). */
|
|
102
|
+
| {
|
|
103
|
+
type: 'text.delta';
|
|
104
|
+
step: number;
|
|
105
|
+
delta: string;
|
|
106
|
+
} | {
|
|
107
|
+
type: 'step.completed';
|
|
108
|
+
step: number;
|
|
109
|
+
result: AgentStepResult;
|
|
110
|
+
} | {
|
|
111
|
+
type: 'tool.started';
|
|
112
|
+
step: number;
|
|
113
|
+
call: ToolCall;
|
|
114
|
+
} | {
|
|
115
|
+
type: 'tool.completed';
|
|
116
|
+
step: number;
|
|
117
|
+
result: AgentToolCallResult;
|
|
118
|
+
} | {
|
|
119
|
+
type: 'wrap_up';
|
|
120
|
+
step: number;
|
|
121
|
+
reason: AgentLimitHit;
|
|
122
|
+
};
|
|
123
|
+
export interface RunAgentOptions {
|
|
124
|
+
/** The gateway bound to a context (`gateway.client(context)`). */
|
|
125
|
+
client: GatewayClient;
|
|
126
|
+
/** Model and generation settings for every step (`model` is a catalog key). */
|
|
127
|
+
request: Omit<GatewayRequest, 'messages' | 'tools' | 'toolChoice' | 'signal' | 'idempotencyKey'>;
|
|
128
|
+
messages: GenerateMessage[];
|
|
129
|
+
tools?: AgentTool[];
|
|
130
|
+
limits?: AgentLimits;
|
|
131
|
+
/** Awaited: events are serialized, in order. */
|
|
132
|
+
onEvent?(event: AgentEvent): void | Promise<void>;
|
|
133
|
+
signal?: AbortSignal;
|
|
134
|
+
/** Base of the per-step idempotency keys (`<key>:s<step>`). */
|
|
135
|
+
idempotencyKey?: string;
|
|
136
|
+
/** The instruction sent when a limit is hit. */
|
|
137
|
+
wrapUpMessage?: string;
|
|
138
|
+
/** Stream each step and emit `text.delta` events (when the client can stream). */
|
|
139
|
+
stream?: boolean;
|
|
140
|
+
/** Names the agent in traces (`invoke_agent <name>`). */
|
|
141
|
+
name?: string;
|
|
142
|
+
/** OpenTelemetry tracer: an `invoke_agent` span and one `execute_tool` span per tool call. */
|
|
143
|
+
tracer?: AiTracer;
|
|
144
|
+
}
|
|
145
|
+
export interface AgentRunResult {
|
|
146
|
+
/** The final answer. */
|
|
147
|
+
text: string;
|
|
148
|
+
/** Parsed final answer when `request.responseFormat` was set. */
|
|
149
|
+
json?: unknown;
|
|
150
|
+
stopReason: AgentStopReason;
|
|
151
|
+
limitHit?: AgentLimitHit;
|
|
152
|
+
steps: number;
|
|
153
|
+
toolCalls: number;
|
|
154
|
+
/** The whole conversation, final answer included. */
|
|
155
|
+
messages: GenerateMessage[];
|
|
156
|
+
searchQueries: string[];
|
|
157
|
+
searchResults: SearchResult[];
|
|
158
|
+
citations: Citation[];
|
|
159
|
+
/** Model calls plus what the tools reported. */
|
|
160
|
+
usage: GenerateUsage;
|
|
161
|
+
/** Model calls plus what the tools reported. */
|
|
162
|
+
cost: {
|
|
163
|
+
costUsd: number;
|
|
164
|
+
costMicroUsd: number;
|
|
165
|
+
} | null;
|
|
166
|
+
/** The part of `cost` the tools reported (`toolResult({ cost })`). */
|
|
167
|
+
toolCost: {
|
|
168
|
+
costUsd: number;
|
|
169
|
+
costMicroUsd: number;
|
|
170
|
+
} | null;
|
|
171
|
+
/** Gateway call ids, one per step. */
|
|
172
|
+
callIds: string[];
|
|
173
|
+
durationMs: number;
|
|
174
|
+
}
|
|
175
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/agent-loop/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,qBAAqB,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