@tangle-network/agent-app 0.60.119 → 0.60.121
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/dist/chat-routes/index.js +104 -114
- package/dist/chat-routes/index.js.map +1 -1
- package/dist/chat-routes/protected-runtime-producer.d.ts +8 -4
- package/dist/chunk-2HGAMEOY.js +222 -0
- package/dist/chunk-2HGAMEOY.js.map +1 -0
- package/dist/{chunk-2YEQGNIU.js → chunk-4U2CA7N2.js} +105 -33
- package/dist/chunk-4U2CA7N2.js.map +1 -0
- package/dist/eval/index.js +1 -1
- package/dist/eval/index.js.map +1 -1
- package/dist/public-consultation/index.d.ts +1 -1
- package/dist/runtime/index.d.ts +1 -0
- package/dist/runtime/index.js +7 -3
- package/dist/runtime/index.js.map +1 -1
- package/dist/runtime/openai-stream.d.ts +8 -0
- package/dist/runtime/protected-router-turn.d.ts +57 -0
- package/dist/runtime/router-turn.d.ts +34 -1
- package/dist/runtime/router-turn.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-2YEQGNIU.js.map +0 -1
- package/dist/chunk-RN2N75EI.js +0 -119
- package/dist/chunk-RN2N75EI.js.map +0 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import { type
|
|
1
|
+
import { type AgentProfile } from '@tangle-network/agent-interface';
|
|
2
|
+
import { type RouterTurnGrant } from '../runtime/protected-router-turn';
|
|
3
3
|
import type { McpToolDefinition } from '../tools/mcp-rpc';
|
|
4
4
|
import type { ChatTurnRouteProducer } from './turn-routes';
|
|
5
5
|
export interface ProtectedRuntimeChatOptions {
|
|
@@ -10,7 +10,7 @@ export interface ProtectedRuntimeChatOptions {
|
|
|
10
10
|
content: string;
|
|
11
11
|
}>;
|
|
12
12
|
/** Runtime owns reservation, activation, revocation and authoritative settlement. */
|
|
13
|
-
grant:
|
|
13
|
+
grant: RouterTurnGrant;
|
|
14
14
|
/** Only tools authorized for this execution. Each tool validates its arguments and effects. */
|
|
15
15
|
tools: readonly McpToolDefinition<{
|
|
16
16
|
signal: AbortSignal;
|
|
@@ -18,5 +18,9 @@ export interface ProtectedRuntimeChatOptions {
|
|
|
18
18
|
maxToolCalls: number;
|
|
19
19
|
signal?: AbortSignal;
|
|
20
20
|
}
|
|
21
|
-
/**
|
|
21
|
+
/**
|
|
22
|
+
* Run a profile as one Router turn under a protected model grant
|
|
23
|
+
* (`runProtectedRouterTurn`), projected through the shared chat projection. The
|
|
24
|
+
* usage is published only from the settled grant.
|
|
25
|
+
*/
|
|
22
26
|
export declare function createProtectedRuntimeChatProducer(options: ProtectedRuntimeChatOptions): ChatTurnRouteProducer;
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import {
|
|
2
|
+
runRouterTurn
|
|
3
|
+
} from "./chunk-4U2CA7N2.js";
|
|
4
|
+
|
|
5
|
+
// src/runtime/protected-model.ts
|
|
6
|
+
import {
|
|
7
|
+
createProtectedAgentCandidateModelPort
|
|
8
|
+
} from "@tangle-network/agent-runtime/candidate-execution";
|
|
9
|
+
var GATEWAY_ORIGIN = "https://candidate-router.tangle.tools";
|
|
10
|
+
var CONTROL_ORIGIN = "https://router.tangle.tools/v1/candidate-model-grants";
|
|
11
|
+
var ACTIVATION_NAMES = [
|
|
12
|
+
"MODEL_GATEWAY_TOKEN",
|
|
13
|
+
"MODEL_GATEWAY_BASE_URL",
|
|
14
|
+
"OPENAI_API_KEY",
|
|
15
|
+
"OPENAI_BASE_URL",
|
|
16
|
+
"ANTHROPIC_API_KEY",
|
|
17
|
+
"ANTHROPIC_AUTH_TOKEN",
|
|
18
|
+
"ANTHROPIC_BASE_URL"
|
|
19
|
+
];
|
|
20
|
+
var ProtectedModelSettlementError = class extends Error {
|
|
21
|
+
constructor(settlement, cause) {
|
|
22
|
+
super("Protected model settled, but its audit record could not be saved", { cause });
|
|
23
|
+
this.settlement = settlement;
|
|
24
|
+
this.name = "ProtectedModelSettlementError";
|
|
25
|
+
}
|
|
26
|
+
settlement;
|
|
27
|
+
};
|
|
28
|
+
function createRouterProtectedModelPort(options) {
|
|
29
|
+
if (!options.apiKey || !Number.isFinite(options.maxCostUsd) || options.maxCostUsd <= 0 || !Number.isSafeInteger(Math.round(options.maxCostUsd * 1e9))) {
|
|
30
|
+
throw new Error("Protected Router transport requires a parent key and finite positive cap");
|
|
31
|
+
}
|
|
32
|
+
const receipts = /* @__PURE__ */ new Map();
|
|
33
|
+
async function request(operation, body, settling = false) {
|
|
34
|
+
const timeout = AbortSignal.timeout(settling ? 3e4 : 15e3);
|
|
35
|
+
const signal = !settling && options.signal ? AbortSignal.any([timeout, options.signal]) : timeout;
|
|
36
|
+
let response;
|
|
37
|
+
try {
|
|
38
|
+
response = await fetch(`${CONTROL_ORIGIN}/${operation}`, {
|
|
39
|
+
// Workerd supports manual redirects; non-2xx responses below still fail closed.
|
|
40
|
+
method: "POST",
|
|
41
|
+
redirect: "manual",
|
|
42
|
+
signal,
|
|
43
|
+
headers: {
|
|
44
|
+
Authorization: `Bearer ${options.apiKey}`,
|
|
45
|
+
"Content-Type": "application/json",
|
|
46
|
+
"X-Tangle-Client": options.clientName ?? "agent-app"
|
|
47
|
+
},
|
|
48
|
+
body: JSON.stringify(body)
|
|
49
|
+
});
|
|
50
|
+
} catch {
|
|
51
|
+
if (!settling && options.signal?.aborted) throw options.signal.reason;
|
|
52
|
+
throw new Error(`Protected model ${operation} transport failed`);
|
|
53
|
+
}
|
|
54
|
+
if (!response.ok) {
|
|
55
|
+
const body2 = await response.json().catch(() => null);
|
|
56
|
+
const code = body2?.error?.code;
|
|
57
|
+
const safeCode = typeof code === "string" && /^candidate_[a-z_]{1,80}$/.test(code) ? code : "request_failed";
|
|
58
|
+
throw new Error(`Protected model ${operation}: ${safeCode} (HTTP ${response.status})`);
|
|
59
|
+
}
|
|
60
|
+
return await response.json();
|
|
61
|
+
}
|
|
62
|
+
const client = {
|
|
63
|
+
reserve: (value) => request("reserve", value),
|
|
64
|
+
activate: async (value) => {
|
|
65
|
+
const activation = await request("activate", value);
|
|
66
|
+
const env = activation?.env;
|
|
67
|
+
const token = env?.MODEL_GATEWAY_TOKEN;
|
|
68
|
+
if (typeof token !== "string" || !/^sk-tgr-[A-Za-z0-9_-]{32,}$/.test(token) || env.MODEL_GATEWAY_BASE_URL !== `${GATEWAY_ORIGIN}/v1` || env.OPENAI_BASE_URL !== `${GATEWAY_ORIGIN}/v1` || env.ANTHROPIC_BASE_URL !== GATEWAY_ORIGIN || [env.OPENAI_API_KEY, env.ANTHROPIC_API_KEY, env.ANTHROPIC_AUTH_TOKEN].some((value2) => value2 !== token)) {
|
|
69
|
+
throw new Error("Protected model activation changed the gateway or credential binding");
|
|
70
|
+
}
|
|
71
|
+
return activation;
|
|
72
|
+
},
|
|
73
|
+
settle: async (value) => {
|
|
74
|
+
const settlement = await request("settle", value, true);
|
|
75
|
+
assertWithinCap(settlement, options.maxCostUsd);
|
|
76
|
+
receipts.set(value.preparationId, settlement);
|
|
77
|
+
return settlement;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const port = createProtectedAgentCandidateModelPort({
|
|
81
|
+
client,
|
|
82
|
+
resolveModel: (value) => request("resolve", value),
|
|
83
|
+
gatewayDomain: "candidate-router.tangle.tools",
|
|
84
|
+
activationEnvNames: ACTIVATION_NAMES
|
|
85
|
+
});
|
|
86
|
+
return {
|
|
87
|
+
...port,
|
|
88
|
+
settleGrant: async (value) => {
|
|
89
|
+
const settlement = await port.settleGrant(value);
|
|
90
|
+
const receipt = receipts.get(value.preparationId);
|
|
91
|
+
if (receipt) {
|
|
92
|
+
try {
|
|
93
|
+
await options.onSettlement?.(structuredClone(receipt));
|
|
94
|
+
} catch (error) {
|
|
95
|
+
throw new ProtectedModelSettlementError(settlement, error);
|
|
96
|
+
} finally {
|
|
97
|
+
receipts.delete(value.preparationId);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return settlement;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function assertWithinCap(settlement, capUsd) {
|
|
105
|
+
if (!Array.isArray(settlement?.calls)) throw new Error("Protected model settlement has no call ledger");
|
|
106
|
+
const capNanos = Math.round(capUsd * 1e9);
|
|
107
|
+
let cost = 0;
|
|
108
|
+
for (const call of settlement.calls) {
|
|
109
|
+
if (!Number.isSafeInteger(call?.costUsdNanos) || call.costUsdNanos < 0) {
|
|
110
|
+
throw new Error("Protected model call cost is invalid");
|
|
111
|
+
}
|
|
112
|
+
cost += call.costUsdNanos;
|
|
113
|
+
if (!Number.isSafeInteger(cost) || cost > capNanos) {
|
|
114
|
+
throw new Error("Protected model settlement exceeds its cost cap");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// src/runtime/protected-router-turn.ts
|
|
120
|
+
import {
|
|
121
|
+
runProtectedAgentCandidateModelGrant
|
|
122
|
+
} from "@tangle-network/agent-runtime/candidate-execution";
|
|
123
|
+
var GRANT_PROVIDERS = /* @__PURE__ */ new Set(["anthropic", "openai"]);
|
|
124
|
+
var GrantRefusal = class extends Error {
|
|
125
|
+
constructor(failure) {
|
|
126
|
+
super(failure.message);
|
|
127
|
+
this.failure = failure;
|
|
128
|
+
this.name = "GrantRefusal";
|
|
129
|
+
}
|
|
130
|
+
failure;
|
|
131
|
+
};
|
|
132
|
+
var refuse = (code, message) => new GrantRefusal({ code, message });
|
|
133
|
+
function failureOf(error) {
|
|
134
|
+
if (error instanceof GrantRefusal) return error.failure;
|
|
135
|
+
if (error instanceof ProtectedModelSettlementError) return { code: "settlement_audit_failed", message: error.message };
|
|
136
|
+
if (error instanceof AggregateError) return { code: "grant_unsettled", message: error.message };
|
|
137
|
+
return { code: "grant_failed", message: error instanceof Error ? error.message : "The model grant failed." };
|
|
138
|
+
}
|
|
139
|
+
async function runProtectedRouterTurn(options) {
|
|
140
|
+
const { grant, ...turnOptions } = options;
|
|
141
|
+
let turn;
|
|
142
|
+
let settlement;
|
|
143
|
+
let resolvedModel;
|
|
144
|
+
const result = (failure) => {
|
|
145
|
+
const base = turn ?? { text: "", servedModel: null, toolCalls: [], usage: { promptTokens: 0, completionTokens: 0, modelCalls: 0 }, toolEvents: [] };
|
|
146
|
+
const { failure: _turnFailure, ...rest } = base;
|
|
147
|
+
return { ...rest, ...settlement ? { settlement } : {}, ...failure ? { failure } : {} };
|
|
148
|
+
};
|
|
149
|
+
if (options.signal?.aborted) return result({ code: "turn_aborted", message: "The turn was cancelled." });
|
|
150
|
+
if (Date.now() >= grant.deadlineAtMs) return result({ code: "grant_deadline", message: "Protected execution deadline expired" });
|
|
151
|
+
try {
|
|
152
|
+
await runProtectedAgentCandidateModelGrant({
|
|
153
|
+
...grant,
|
|
154
|
+
port: {
|
|
155
|
+
resolve: async (input) => {
|
|
156
|
+
const resolved = await grant.port.resolve(input);
|
|
157
|
+
resolvedModel = resolved.model;
|
|
158
|
+
return resolved;
|
|
159
|
+
},
|
|
160
|
+
reserveGrant: (input) => grant.port.reserveGrant(input),
|
|
161
|
+
activateGrant: (input) => grant.port.activateGrant(input),
|
|
162
|
+
// Runtime settles failed executions before rethrowing. Retain that receipt too.
|
|
163
|
+
settleGrant: async (input) => {
|
|
164
|
+
let auditError;
|
|
165
|
+
const receipt = await grant.port.settleGrant(input).catch((error) => {
|
|
166
|
+
if (!(error instanceof ProtectedModelSettlementError)) throw error;
|
|
167
|
+
auditError = error;
|
|
168
|
+
return error.settlement;
|
|
169
|
+
});
|
|
170
|
+
const usage = {
|
|
171
|
+
inputTokens: receipt.calls.reduce((sum, call) => sum + call.accountedInputTokens, 0),
|
|
172
|
+
outputTokens: receipt.calls.reduce((sum, call) => sum + call.outputTokens, 0),
|
|
173
|
+
reasoningTokens: receipt.calls.reduce((sum, call) => sum + call.reasoningTokens, 0),
|
|
174
|
+
costUsd: receipt.calls.reduce((sum, call) => sum + call.costUsdNanos, 0) / 1e9
|
|
175
|
+
};
|
|
176
|
+
const unauthorizedReasoning = usage.reasoningTokens > 0 && grant.resolve.reasoningEffort === "none";
|
|
177
|
+
const budgetEnforced = receipt.usageWithinLimits && !unauthorizedReasoning;
|
|
178
|
+
settlement = { receipt, ...resolvedModel ? { model: resolvedModel } : {}, usage, budgetEnforced };
|
|
179
|
+
if (unauthorizedReasoning) {
|
|
180
|
+
throw refuse("settlement_unauthorized_reasoning", "Provider reported reasoning tokens although reasoning was disabled");
|
|
181
|
+
}
|
|
182
|
+
if (!budgetEnforced) throw refuse("settlement_over_limit", "Provider settlement exceeded execution limits");
|
|
183
|
+
if (auditError) throw auditError;
|
|
184
|
+
return receipt;
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
execute: async ({ activation, resolved }) => {
|
|
188
|
+
if (options.signal?.aborted) throw refuse("turn_aborted", "The turn was cancelled.");
|
|
189
|
+
if (Date.now() >= grant.deadlineAtMs) throw refuse("grant_deadline", "Protected execution deadline expired");
|
|
190
|
+
if (!GRANT_PROVIDERS.has(resolved.provider) || resolved.reasoningEffort !== "none") {
|
|
191
|
+
throw refuse("grant_model_unsupported", "Protected chat currently requires an Anthropic or OpenAI model without reasoning");
|
|
192
|
+
}
|
|
193
|
+
const apiKey = activation.env.OPENAI_API_KEY;
|
|
194
|
+
const baseUrl = activation.env.OPENAI_BASE_URL;
|
|
195
|
+
if (!apiKey || !baseUrl) throw refuse("grant_activation_missing", "Protected Router activation is missing");
|
|
196
|
+
const remainingMs = Math.max(1, grant.deadlineAtMs - Date.now());
|
|
197
|
+
turn = await runRouterTurn({
|
|
198
|
+
...turnOptions,
|
|
199
|
+
apiKey,
|
|
200
|
+
baseUrl,
|
|
201
|
+
repeat: false,
|
|
202
|
+
budgetMs: Math.min(options.budgetMs ?? remainingMs, remainingMs),
|
|
203
|
+
signal: AbortSignal.any([AbortSignal.timeout(remainingMs), ...options.signal ? [options.signal] : []])
|
|
204
|
+
});
|
|
205
|
+
if (turn.failure?.code === "turn_aborted" && Date.now() >= grant.deadlineAtMs) {
|
|
206
|
+
throw refuse("turn_deadline", "Protected execution deadline expired");
|
|
207
|
+
}
|
|
208
|
+
if (turn.failure) throw new GrantRefusal(turn.failure);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
} catch (error) {
|
|
212
|
+
return result(failureOf(error));
|
|
213
|
+
}
|
|
214
|
+
return result();
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export {
|
|
218
|
+
ProtectedModelSettlementError,
|
|
219
|
+
createRouterProtectedModelPort,
|
|
220
|
+
runProtectedRouterTurn
|
|
221
|
+
};
|
|
222
|
+
//# sourceMappingURL=chunk-2HGAMEOY.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/runtime/protected-model.ts","../src/runtime/protected-router-turn.ts"],"sourcesContent":["import {\n createProtectedAgentCandidateModelPort,\n type AgentCandidateModelGrantClient,\n type AgentCandidateModelPort,\n} from '@tangle-network/agent-runtime/candidate-execution'\n\nconst GATEWAY_ORIGIN = 'https://candidate-router.tangle.tools'\nconst CONTROL_ORIGIN = 'https://router.tangle.tools/v1/candidate-model-grants'\nconst ACTIVATION_NAMES = [\n 'MODEL_GATEWAY_TOKEN', 'MODEL_GATEWAY_BASE_URL', 'OPENAI_API_KEY', 'OPENAI_BASE_URL',\n 'ANTHROPIC_API_KEY', 'ANTHROPIC_AUTH_TOKEN', 'ANTHROPIC_BASE_URL',\n] as const\n\nexport interface RouterProtectedModelPortOptions {\n apiKey: string\n maxCostUsd: number\n clientName?: string\n signal?: AbortSignal\n onSettlement?(receipt: RouterProtectedModelSettlement): Promise<void> | void\n}\n\n/** A validated charge remains authoritative when the product cannot save its audit record. */\nexport class ProtectedModelSettlementError extends Error {\n constructor(readonly settlement: Awaited<ReturnType<AgentCandidateModelPort['settleGrant']>>, cause: unknown) {\n super('Protected model settled, but its audit record could not be saved', { cause })\n this.name = 'ProtectedModelSettlementError'\n }\n}\n\n/** Bind one turn to Router's atomic grant ledger without exposing its parent key. */\nexport function createRouterProtectedModelPort(options: RouterProtectedModelPortOptions): AgentCandidateModelPort {\n if (!options.apiKey || !Number.isFinite(options.maxCostUsd) || options.maxCostUsd <= 0\n || !Number.isSafeInteger(Math.round(options.maxCostUsd * 1_000_000_000))) {\n throw new Error('Protected Router transport requires a parent key and finite positive cap')\n }\n const receipts = new Map<string, RouterProtectedModelSettlement>()\n async function request<T>(operation: string, body: unknown, settling = false): Promise<T> {\n const timeout = AbortSignal.timeout(settling ? 30_000 : 15_000)\n const signal = !settling && options.signal ? AbortSignal.any([timeout, options.signal]) : timeout\n let response: Response\n try {\n response = await fetch(`${CONTROL_ORIGIN}/${operation}`, {\n // Workerd supports manual redirects; non-2xx responses below still fail closed.\n method: 'POST', redirect: 'manual', signal,\n headers: {\n Authorization: `Bearer ${options.apiKey}`,\n 'Content-Type': 'application/json', 'X-Tangle-Client': options.clientName ?? 'agent-app',\n },\n body: JSON.stringify(body),\n })\n } catch {\n if (!settling && options.signal?.aborted) throw options.signal.reason\n throw new Error(`Protected model ${operation} transport failed`)\n }\n if (!response.ok) {\n const body = await response.json().catch(() => null) as { error?: { code?: unknown } } | null\n const code = body?.error?.code\n // Preserve Runtime's draining retry contract without echoing server text or credentials.\n const safeCode = typeof code === 'string' && /^candidate_[a-z_]{1,80}$/.test(code) ? code : 'request_failed'\n throw new Error(`Protected model ${operation}: ${safeCode} (HTTP ${response.status})`)\n }\n return await response.json() as T\n }\n\n const client: AgentCandidateModelGrantClient = {\n reserve: (value) => request('reserve', value),\n activate: async (value) => {\n const activation = await request<Awaited<ReturnType<AgentCandidateModelGrantClient['activate']>>>('activate', value)\n const env = activation?.env\n const token = env?.MODEL_GATEWAY_TOKEN\n if (typeof token !== 'string' || !/^sk-tgr-[A-Za-z0-9_-]{32,}$/.test(token)\n || env.MODEL_GATEWAY_BASE_URL !== `${GATEWAY_ORIGIN}/v1`\n || env.OPENAI_BASE_URL !== `${GATEWAY_ORIGIN}/v1`\n || env.ANTHROPIC_BASE_URL !== GATEWAY_ORIGIN\n || [env.OPENAI_API_KEY, env.ANTHROPIC_API_KEY, env.ANTHROPIC_AUTH_TOKEN].some((value) => value !== token)) {\n throw new Error('Protected model activation changed the gateway or credential binding')\n }\n return activation\n },\n settle: async (value) => {\n const settlement = await request<RouterProtectedModelSettlement>('settle', value, true)\n assertWithinCap(settlement, options.maxCostUsd)\n receipts.set(value.preparationId, settlement)\n return settlement\n },\n }\n const port = createProtectedAgentCandidateModelPort({\n client, resolveModel: (value) => request('resolve', value),\n gatewayDomain: 'candidate-router.tangle.tools', activationEnvNames: ACTIVATION_NAMES,\n })\n return {\n ...port,\n settleGrant: async (value) => {\n const settlement = await port.settleGrant(value)\n const receipt = receipts.get(value.preparationId)\n if (receipt) {\n try {\n await options.onSettlement?.(structuredClone(receipt))\n } catch (error) {\n throw new ProtectedModelSettlementError(settlement, error)\n } finally {\n receipts.delete(value.preparationId)\n }\n }\n return settlement\n },\n }\n}\n\n/**\n * Router's settle response is Runtime's exact settlement ledger. Router keeps\n * the billing hold and charge behind it and refuses a settlement outside the\n * grant's frozen limits; Runtime rejects unknown or missing ledger fields.\n */\nexport type RouterProtectedModelSettlement = Awaited<ReturnType<AgentCandidateModelGrantClient['settle']>>\n\n/** Bound the ledger's cumulative cost by this turn's cap before anything retains it. */\nfunction assertWithinCap(settlement: RouterProtectedModelSettlement, capUsd: number): void {\n if (!Array.isArray(settlement?.calls)) throw new Error('Protected model settlement has no call ledger')\n const capNanos = Math.round(capUsd * 1_000_000_000)\n let cost = 0\n for (const call of settlement.calls) {\n if (!Number.isSafeInteger(call?.costUsdNanos) || call.costUsdNanos < 0) {\n throw new Error('Protected model call cost is invalid')\n }\n cost += call.costUsdNanos\n if (!Number.isSafeInteger(cost) || cost > capNanos) {\n throw new Error('Protected model settlement exceeds its cost cap')\n }\n }\n}\n","/**\n * A Router turn under a protected model grant.\n *\n * Runtime resolves, reserves, activates and settles the grant around exactly\n * one `runRouterTurn` (`runProtectedAgentCandidateModelGrant`); the turn's model\n * calls use the grant's scoped key and base URL; the settled ledger is the\n * turn's only usage. GTM's bounded lane and Legal's paid consultations run here\n * through `createProtectedRuntimeChatProducer`, on the same engine as every\n * other Router turn. This module stays off the `./router-turn` entry: the grant\n * client pulls agent-runtime chunks a Worker bundle cannot build\n * (tests/router-turn-entry.test.ts).\n *\n * Every refusal and failure is a typed outcome. Codes:\n * - `grant_deadline`, `grant_model_unsupported`, `grant_activation_missing`:\n * refused before any model call.\n * - `grant_failed`: the grant could not be resolved, reserved or activated.\n * - `grant_unsettled`: the turn failed and its grant did not settle either.\n * - `settlement_over_limit`, `settlement_unauthorized_reasoning`,\n * `settlement_audit_failed`: the turn answered, but its settled ledger failed\n * a check; the answer stands and the turn still fails.\n * - `turn_deadline`: the grant's deadline ended the turn.\n * - Anything else is the turn's own failure (`turn_aborted`, `turn_tool_limit`,\n * `model_http_<status>`, ...), settled as `failed`.\n */\nimport {\n runProtectedAgentCandidateModelGrant,\n type RunProtectedAgentCandidateModelGrantOptions,\n} from '@tangle-network/agent-runtime/candidate-execution'\nimport { ProtectedModelSettlementError } from './protected-model'\nimport { runRouterTurn, type RouterTurnFailure, type RouterTurnOptions, type RouterTurnOutcome } from './router-turn'\n\n/** Runtime's grant inputs for one turn: the port, the model request, the reservation and its deadline. */\nexport type RouterTurnGrant = Omit<RunProtectedAgentCandidateModelGrantOptions<void>, 'execute'>\n\ntype GrantSettlement = Awaited<ReturnType<RouterTurnGrant['port']['settleGrant']>>\n\n/** A turn under a grant. The grant supplies the key and base URL; one grant never repeats a turn.\n * Placement is decided before a grant is reserved, so it is not an option here. */\nexport type ProtectedRouterTurnOptions = Omit<RouterTurnOptions, 'apiKey' | 'baseUrl' | 'repeat' | 'placement'> & {\n grant: RouterTurnGrant\n}\n\nexport interface ProtectedRouterTurnOutcome extends RouterTurnOutcome {\n /** What the grant settled, present whenever it settled, including for a failed turn. */\n settlement?: RouterTurnSettlement\n}\n\n/** What the grant settled for the turn. */\nexport interface RouterTurnSettlement {\n /** Runtime's settled ledger, retained even when a check on it fails. */\n receipt: GrantSettlement\n /** The model the grant resolved and reserved. */\n model?: string\n usage: { inputTokens: number; outputTokens: number; reasoningTokens: number; costUsd: number }\n /** Router certified the usage within the grant's limits, and no call reasoned under a grant\n * that disabled reasoning. */\n budgetEnforced: boolean\n}\n\n/**\n * Providers whose client function tools are verified over Chat Completions with\n * reasoning disabled. Router refuses provider-hosted tools and cache markers on\n * candidate grants, so tool-result text is billed as input and accounted input\n * covers OpenAI's automatic cached prefix.\n */\nconst GRANT_PROVIDERS: ReadonlySet<string> = new Set(['anthropic', 'openai'])\n\nclass GrantRefusal extends Error {\n constructor(readonly failure: RouterTurnFailure) {\n super(failure.message)\n this.name = 'GrantRefusal'\n }\n}\n\nconst refuse = (code: string, message: string) => new GrantRefusal({ code, message })\n\nfunction failureOf(error: unknown): RouterTurnFailure {\n if (error instanceof GrantRefusal) return error.failure\n if (error instanceof ProtectedModelSettlementError) return { code: 'settlement_audit_failed', message: error.message }\n if (error instanceof AggregateError) return { code: 'grant_unsettled', message: error.message }\n return { code: 'grant_failed', message: error instanceof Error ? error.message : 'The model grant failed.' }\n}\n\n/** Run one `runRouterTurn` under `options.grant`. */\nexport async function runProtectedRouterTurn(options: ProtectedRouterTurnOptions): Promise<ProtectedRouterTurnOutcome> {\n const { grant, ...turnOptions } = options\n let turn: RouterTurnOutcome | undefined\n let settlement: RouterTurnSettlement | undefined\n let resolvedModel: string | undefined\n const result = (failure?: RouterTurnFailure): ProtectedRouterTurnOutcome => {\n const base: RouterTurnOutcome = turn\n ?? { text: '', servedModel: null, toolCalls: [], usage: { promptTokens: 0, completionTokens: 0, modelCalls: 0 }, toolEvents: [] }\n // The grant's verdict replaces the turn's own failure.\n const { failure: _turnFailure, ...rest } = base\n return { ...rest, ...(settlement ? { settlement } : {}), ...(failure ? { failure } : {}) }\n }\n if (options.signal?.aborted) return result({ code: 'turn_aborted', message: 'The turn was cancelled.' })\n if (Date.now() >= grant.deadlineAtMs) return result({ code: 'grant_deadline', message: 'Protected execution deadline expired' })\n try {\n await runProtectedAgentCandidateModelGrant({\n ...grant,\n port: {\n resolve: async input => {\n const resolved = await grant.port.resolve(input)\n resolvedModel = resolved.model\n return resolved\n },\n reserveGrant: input => grant.port.reserveGrant(input),\n activateGrant: input => grant.port.activateGrant(input),\n // Runtime settles failed executions before rethrowing. Retain that receipt too.\n settleGrant: async input => {\n let auditError: ProtectedModelSettlementError | undefined\n const receipt = await grant.port.settleGrant(input).catch(error => {\n if (!(error instanceof ProtectedModelSettlementError)) throw error\n auditError = error\n return error.settlement\n })\n const usage = {\n inputTokens: receipt.calls.reduce((sum, call) => sum + call.accountedInputTokens, 0),\n outputTokens: receipt.calls.reduce((sum, call) => sum + call.outputTokens, 0),\n reasoningTokens: receipt.calls.reduce((sum, call) => sum + call.reasoningTokens, 0),\n costUsd: receipt.calls.reduce((sum, call) => sum + call.costUsdNanos, 0) / 1e9,\n }\n // Execution limits carry no reasoning allowance, so a provider that\n // reasons under a disabled-reasoning grant exceeded what was authorized.\n const unauthorizedReasoning = usage.reasoningTokens > 0 && grant.resolve.reasoningEffort === 'none'\n const budgetEnforced = receipt.usageWithinLimits && !unauthorizedReasoning\n settlement = { receipt, ...(resolvedModel ? { model: resolvedModel } : {}), usage, budgetEnforced }\n if (unauthorizedReasoning) {\n throw refuse('settlement_unauthorized_reasoning', 'Provider reported reasoning tokens although reasoning was disabled')\n }\n if (!budgetEnforced) throw refuse('settlement_over_limit', 'Provider settlement exceeded execution limits')\n if (auditError) throw auditError\n return receipt\n },\n },\n execute: async ({ activation, resolved }) => {\n if (options.signal?.aborted) throw refuse('turn_aborted', 'The turn was cancelled.')\n if (Date.now() >= grant.deadlineAtMs) throw refuse('grant_deadline', 'Protected execution deadline expired')\n if (!GRANT_PROVIDERS.has(resolved.provider) || resolved.reasoningEffort !== 'none') {\n throw refuse('grant_model_unsupported', 'Protected chat currently requires an Anthropic or OpenAI model without reasoning')\n }\n const apiKey = activation.env.OPENAI_API_KEY\n const baseUrl = activation.env.OPENAI_BASE_URL\n if (!apiKey || !baseUrl) throw refuse('grant_activation_missing', 'Protected Router activation is missing')\n const remainingMs = Math.max(1, grant.deadlineAtMs - Date.now())\n // One grant is one bounded unit: no automatic second billable call, and the\n // grant's deadline ends the model call in flight.\n turn = await runRouterTurn({\n ...turnOptions, apiKey, baseUrl, repeat: false,\n budgetMs: Math.min(options.budgetMs ?? remainingMs, remainingMs),\n signal: AbortSignal.any([AbortSignal.timeout(remainingMs), ...(options.signal ? [options.signal] : [])]),\n })\n // The grant's timeout aborts the call in flight; that is the deadline, not a cancellation.\n // A turn past its deadline fails here: finishing it would need a second grant.\n if (turn.failure?.code === 'turn_aborted' && Date.now() >= grant.deadlineAtMs) {\n throw refuse('turn_deadline', 'Protected execution deadline expired')\n }\n if (turn.failure) throw new GrantRefusal(turn.failure)\n },\n })\n } catch (error) {\n return result(failureOf(error))\n }\n return result()\n}\n"],"mappings":";;;;;AAAA;AAAA,EACE;AAAA,OAGK;AAEP,IAAM,iBAAiB;AACvB,IAAM,iBAAiB;AACvB,IAAM,mBAAmB;AAAA,EACvB;AAAA,EAAuB;AAAA,EAA0B;AAAA,EAAkB;AAAA,EACnE;AAAA,EAAqB;AAAA,EAAwB;AAC/C;AAWO,IAAM,gCAAN,cAA4C,MAAM;AAAA,EACvD,YAAqB,YAAyE,OAAgB;AAC5G,UAAM,oEAAoE,EAAE,MAAM,CAAC;AADhE;AAEnB,SAAK,OAAO;AAAA,EACd;AAAA,EAHqB;AAIvB;AAGO,SAAS,+BAA+B,SAAmE;AAChH,MAAI,CAAC,QAAQ,UAAU,CAAC,OAAO,SAAS,QAAQ,UAAU,KAAK,QAAQ,cAAc,KAChF,CAAC,OAAO,cAAc,KAAK,MAAM,QAAQ,aAAa,GAAa,CAAC,GAAG;AAC1E,UAAM,IAAI,MAAM,0EAA0E;AAAA,EAC5F;AACA,QAAM,WAAW,oBAAI,IAA4C;AACjE,iBAAe,QAAW,WAAmB,MAAe,WAAW,OAAmB;AACxF,UAAM,UAAU,YAAY,QAAQ,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@@ function createOpenAICompatStreamTurn(opts) {
|
|
|
94
94
|
}, opts.onServedModel ? { requestedModel: opts.model, report: opts.onServedModel } : void 0, opts.headers)
|
|
95
95
|
);
|
|
96
96
|
}
|
|
97
|
+
function createOpenAICompatCompletionTurn(opts) {
|
|
98
|
+
const base = opts.baseUrl.replace(/\/+$/, "");
|
|
99
|
+
const doFetch = opts.fetchImpl ?? fetch;
|
|
100
|
+
return (messages) => toLoopEvents(
|
|
101
|
+
completionAsChunk(doFetch, `${base}/chat/completions`, opts.apiKey, {
|
|
102
|
+
model: opts.model,
|
|
103
|
+
messages,
|
|
104
|
+
...opts.tools && opts.tools.length > 0 ? { tools: opts.tools } : {},
|
|
105
|
+
...opts.temperature != null ? { temperature: opts.temperature } : {},
|
|
106
|
+
...opts.extraBody
|
|
107
|
+
}, opts.onServedModel ? { requestedModel: opts.model, report: opts.onServedModel } : void 0, opts.headers)
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
async function* completionAsChunk(doFetch, url, apiKey, body, attribution, headers) {
|
|
111
|
+
const res = await doFetch(url, {
|
|
112
|
+
method: "POST",
|
|
113
|
+
headers: { ...headers, Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json", Accept: "application/json" },
|
|
114
|
+
body: JSON.stringify(body)
|
|
115
|
+
});
|
|
116
|
+
if (!res.ok) throw await httpFailure(res, "completion");
|
|
117
|
+
const completion = await res.json();
|
|
118
|
+
if (attribution) {
|
|
119
|
+
const served = servedFromHeader(res, attribution.requestedModel) ?? (completion.model ? servedFromBody(completion.model, attribution.requestedModel) : void 0);
|
|
120
|
+
if (served) attribution.report(served);
|
|
121
|
+
}
|
|
122
|
+
const choice = completion.choices?.[0];
|
|
123
|
+
const message = choice?.message;
|
|
124
|
+
yield {
|
|
125
|
+
...completion.model ? { model: completion.model } : {},
|
|
126
|
+
choices: [{
|
|
127
|
+
delta: {
|
|
128
|
+
content: message?.content ?? null,
|
|
129
|
+
reasoning_content: message?.reasoning_content ?? null,
|
|
130
|
+
tool_calls: (message?.tool_calls ?? []).map((call, index) => ({
|
|
131
|
+
index,
|
|
132
|
+
...call.id ? { id: call.id } : {},
|
|
133
|
+
function: { name: call.function?.name ?? "", arguments: call.function?.arguments ?? "" },
|
|
134
|
+
...call.extra_content !== void 0 ? { extra_content: call.extra_content } : {}
|
|
135
|
+
}))
|
|
136
|
+
},
|
|
137
|
+
finish_reason: choice?.finish_reason ?? null
|
|
138
|
+
}],
|
|
139
|
+
...completion.usage ? { usage: completion.usage } : {}
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
async function httpFailure(res, kind) {
|
|
143
|
+
const text = res.body ? await res.text().catch(() => "") : "";
|
|
144
|
+
const error = new Error(`OpenAI-compat ${kind} failed (HTTP ${res.status})${text ? `: ${text.slice(0, 200)}` : ""}`);
|
|
145
|
+
Object.assign(error, { status: res.status });
|
|
146
|
+
return error;
|
|
147
|
+
}
|
|
148
|
+
function servedFromHeader(res, requestedModel) {
|
|
149
|
+
const servedHeader = res.headers.get("x-tangle-served-model");
|
|
150
|
+
if (!servedHeader) return void 0;
|
|
151
|
+
return {
|
|
152
|
+
requestedModel,
|
|
153
|
+
servedModel: servedHeader,
|
|
154
|
+
source: "router_header",
|
|
155
|
+
substituted: normalizeModelId(servedHeader) !== normalizeModelId(requestedModel),
|
|
156
|
+
...parseFailoverHeader(res.headers.get("x-tangle-failover"))
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
function servedFromBody(model, requestedModel) {
|
|
160
|
+
return {
|
|
161
|
+
requestedModel,
|
|
162
|
+
servedModel: model,
|
|
163
|
+
source: "response_body",
|
|
164
|
+
substituted: normalizeModelId(model) !== normalizeModelId(requestedModel)
|
|
165
|
+
};
|
|
166
|
+
}
|
|
97
167
|
async function* streamChatCompletions(doFetch, url, apiKey, body, attribution, headers) {
|
|
98
168
|
const res = await doFetch(url, {
|
|
99
169
|
method: "POST",
|
|
100
170
|
headers: { ...headers, Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json", Accept: "text/event-stream" },
|
|
101
171
|
body: JSON.stringify(body)
|
|
102
172
|
});
|
|
103
|
-
if (!res.ok || !res.body)
|
|
104
|
-
const text = res.body ? await res.text().catch(() => "") : "";
|
|
105
|
-
const error = new Error(`OpenAI-compat stream failed (HTTP ${res.status})${text ? `: ${text.slice(0, 200)}` : ""}`);
|
|
106
|
-
Object.assign(error, { status: res.status });
|
|
107
|
-
throw error;
|
|
108
|
-
}
|
|
173
|
+
if (!res.ok || !res.body) throw await httpFailure(res, "stream");
|
|
109
174
|
let reported = false;
|
|
110
175
|
const report = (served) => {
|
|
111
176
|
if (reported || !attribution) return;
|
|
@@ -113,16 +178,8 @@ async function* streamChatCompletions(doFetch, url, apiKey, body, attribution, h
|
|
|
113
178
|
attribution.report(served);
|
|
114
179
|
};
|
|
115
180
|
if (attribution) {
|
|
116
|
-
const
|
|
117
|
-
if (
|
|
118
|
-
report({
|
|
119
|
-
requestedModel: attribution.requestedModel,
|
|
120
|
-
servedModel: servedHeader,
|
|
121
|
-
source: "router_header",
|
|
122
|
-
substituted: normalizeModelId(servedHeader) !== normalizeModelId(attribution.requestedModel),
|
|
123
|
-
...parseFailoverHeader(res.headers.get("x-tangle-failover"))
|
|
124
|
-
});
|
|
125
|
-
}
|
|
181
|
+
const served = servedFromHeader(res, attribution.requestedModel);
|
|
182
|
+
if (served) report(served);
|
|
126
183
|
}
|
|
127
184
|
const reader = res.body.getReader();
|
|
128
185
|
const decoder = new TextDecoder();
|
|
@@ -144,14 +201,7 @@ async function* streamChatCompletions(doFetch, url, apiKey, body, attribution, h
|
|
|
144
201
|
} catch {
|
|
145
202
|
continue;
|
|
146
203
|
}
|
|
147
|
-
if (attribution && !reported && chunk.model)
|
|
148
|
-
report({
|
|
149
|
-
requestedModel: attribution.requestedModel,
|
|
150
|
-
servedModel: chunk.model,
|
|
151
|
-
source: "response_body",
|
|
152
|
-
substituted: normalizeModelId(chunk.model) !== normalizeModelId(attribution.requestedModel)
|
|
153
|
-
});
|
|
154
|
-
}
|
|
204
|
+
if (attribution && !reported && chunk.model) report(servedFromBody(chunk.model, attribution.requestedModel));
|
|
155
205
|
yield chunk;
|
|
156
206
|
}
|
|
157
207
|
}
|
|
@@ -246,7 +296,8 @@ async function runRouterTurn(options) {
|
|
|
246
296
|
failure: { code: "placement_sandbox", message: placement.reason }
|
|
247
297
|
};
|
|
248
298
|
}
|
|
249
|
-
|
|
299
|
+
const { apiKey } = options;
|
|
300
|
+
if (!apiKey) {
|
|
250
301
|
return {
|
|
251
302
|
text: "",
|
|
252
303
|
servedModel: null,
|
|
@@ -258,23 +309,33 @@ async function runRouterTurn(options) {
|
|
|
258
309
|
}
|
|
259
310
|
const emit = options.onEvent ?? (() => {
|
|
260
311
|
});
|
|
312
|
+
const { signal } = options;
|
|
313
|
+
const fetchImpl = signal ? async (input, init) => {
|
|
314
|
+
signal.throwIfAborted();
|
|
315
|
+
return (options.fetchImpl ?? fetch)(input, { ...init, signal });
|
|
316
|
+
} : options.fetchImpl;
|
|
317
|
+
const extraBody = {
|
|
318
|
+
...options.maxTokens ? { max_tokens: options.maxTokens } : {},
|
|
319
|
+
...options.maxCompletionTokens ? { max_completion_tokens: options.maxCompletionTokens } : {},
|
|
320
|
+
...options.reasoningEffort ? { reasoning_effort: options.reasoningEffort } : {}
|
|
321
|
+
};
|
|
261
322
|
const byFunction = /* @__PURE__ */ new Map();
|
|
262
323
|
for (const tool of options.tools ?? []) {
|
|
263
324
|
const fn = routerFunctionName(tool.name);
|
|
264
325
|
if (!byFunction.has(fn)) byFunction.set(fn, tool);
|
|
265
326
|
}
|
|
266
327
|
let servedModel = null;
|
|
267
|
-
const streamTurn = createOpenAICompatStreamTurn({
|
|
328
|
+
const streamTurn = (options.stream === false ? createOpenAICompatCompletionTurn : createOpenAICompatStreamTurn)({
|
|
268
329
|
baseUrl: options.baseUrl ?? DEFAULT_TANGLE_ROUTER_BASE_URL,
|
|
269
|
-
apiKey
|
|
330
|
+
apiKey,
|
|
270
331
|
model: options.model,
|
|
271
332
|
tools: [...byFunction.entries()].map(([name, tool]) => ({
|
|
272
333
|
type: "function",
|
|
273
334
|
function: { name, description: tool.description, parameters: tool.inputSchema }
|
|
274
335
|
})),
|
|
275
|
-
...
|
|
336
|
+
...Object.keys(extraBody).length ? { extraBody } : {},
|
|
276
337
|
...options.traceId ? { headers: { "x-tangle-trace-id": options.traceId } } : {},
|
|
277
|
-
...
|
|
338
|
+
...fetchImpl ? { fetchImpl } : {},
|
|
278
339
|
onServedModel: (served) => {
|
|
279
340
|
servedModel = served.servedModel;
|
|
280
341
|
}
|
|
@@ -286,6 +347,7 @@ async function runRouterTurn(options) {
|
|
|
286
347
|
const started = /* @__PURE__ */ new Map();
|
|
287
348
|
let segmentOpen = false;
|
|
288
349
|
let toolsRan = 0;
|
|
350
|
+
let toolLimitReached = false;
|
|
289
351
|
const deadline = Date.now() + (options.budgetMs ?? DEFAULT_BUDGET_MS);
|
|
290
352
|
const callIdOf = (id, toolName) => id ?? `${toolName}-${started.size}`;
|
|
291
353
|
const run = async () => {
|
|
@@ -299,10 +361,16 @@ async function runRouterTurn(options) {
|
|
|
299
361
|
isExecutableTool: (name) => byFunction.has(name),
|
|
300
362
|
deadlineMs: deadline,
|
|
301
363
|
maxToolTurns: options.maxToolTurns ?? 12,
|
|
302
|
-
|
|
364
|
+
labelFor: (call) => byFunction.get(call.toolName)?.name ?? call.toolName,
|
|
365
|
+
renderResult: (label, outcome2) => options.renderToolResult ? options.renderToolResult(label, outcome2) : renderResult(outcome2),
|
|
303
366
|
executeToolCall: async (call, context) => {
|
|
304
367
|
const tool = byFunction.get(call.toolName);
|
|
305
368
|
if (!tool) return { ok: false, code: "unknown_tool", message: `No tool named ${call.toolName}` };
|
|
369
|
+
if (signal?.aborted) return { ok: false, code: "aborted", message: "The turn was cancelled before this tool ran." };
|
|
370
|
+
if (options.maxToolCalls !== void 0 && toolsRan >= options.maxToolCalls) {
|
|
371
|
+
toolLimitReached = true;
|
|
372
|
+
return { ok: false, code: "tool_limit", message: "The turn reached its tool-call limit." };
|
|
373
|
+
}
|
|
306
374
|
toolsRan++;
|
|
307
375
|
const emitEvent = (kind, data) => context?.progress({ data: { [TOOL_EVENT]: kind, data } });
|
|
308
376
|
try {
|
|
@@ -371,6 +439,7 @@ async function runRouterTurn(options) {
|
|
|
371
439
|
endedAt: Date.now(),
|
|
372
440
|
error: { code: step.outcome.code, message: step.outcome.message }
|
|
373
441
|
});
|
|
442
|
+
if (toolLimitReached) return "tool_limit";
|
|
374
443
|
} else if (step.kind === "capped") {
|
|
375
444
|
return step.stopReason;
|
|
376
445
|
}
|
|
@@ -386,21 +455,23 @@ async function runRouterTurn(options) {
|
|
|
386
455
|
...placement ? { placement } : {},
|
|
387
456
|
...failure ? { failure } : {}
|
|
388
457
|
});
|
|
458
|
+
const repeat = options.repeat !== false;
|
|
389
459
|
let stop;
|
|
390
460
|
try {
|
|
391
461
|
try {
|
|
392
462
|
stop = await run();
|
|
393
463
|
} catch (error) {
|
|
394
|
-
if (toolsRan > 0 || segments.length > 0) throw error;
|
|
464
|
+
if (!repeat || signal?.aborted || toolsRan > 0 || segments.length > 0) throw error;
|
|
395
465
|
stop = await run();
|
|
396
466
|
}
|
|
397
467
|
} catch (error) {
|
|
468
|
+
if (signal?.aborted) return outcome({ code: "turn_aborted", message: "The turn was cancelled." });
|
|
398
469
|
const status = error.status;
|
|
399
470
|
return outcome(typeof status === "number" ? { code: `model_http_${status}`, status, message: "The model service did not answer." } : { code: "model_unavailable", message: "The model service did not answer." });
|
|
400
471
|
}
|
|
401
472
|
if (stop !== "completed") return outcome({ code: `turn_${stop}`, message: "The reply was stopped before it finished." });
|
|
402
473
|
if (!segments.some((part) => part.trim())) {
|
|
403
|
-
if (toolsRan === 0) {
|
|
474
|
+
if (toolsRan === 0 && repeat) {
|
|
404
475
|
try {
|
|
405
476
|
stop = await run();
|
|
406
477
|
} catch {
|
|
@@ -418,6 +489,7 @@ async function runRouterTurn(options) {
|
|
|
418
489
|
export {
|
|
419
490
|
toLoopEvents,
|
|
420
491
|
createOpenAICompatStreamTurn,
|
|
492
|
+
createOpenAICompatCompletionTurn,
|
|
421
493
|
runToolLoop,
|
|
422
494
|
streamToolLoop,
|
|
423
495
|
budgetTurnKnowledge,
|
|
@@ -428,4 +500,4 @@ export {
|
|
|
428
500
|
composeRouterTurnPrompt,
|
|
429
501
|
runRouterTurn
|
|
430
502
|
};
|
|
431
|
-
//# sourceMappingURL=chunk-
|
|
503
|
+
//# sourceMappingURL=chunk-4U2CA7N2.js.map
|