@dexto/core 1.15.0-beta.2 → 1.16.0
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/agent/DextoAgent.cjs +1 -2
- package/dist/agent/DextoAgent.d.ts.map +1 -1
- package/dist/agent/DextoAgent.js +1 -2
- package/dist/approval/errors.cjs +9 -0
- package/dist/approval/errors.d.ts +8 -0
- package/dist/approval/errors.d.ts.map +1 -1
- package/dist/approval/errors.js +8 -0
- package/dist/approval/index.cjs +2 -4
- package/dist/approval/index.d.ts +3 -3
- package/dist/approval/index.d.ts.map +1 -1
- package/dist/approval/index.js +2 -5
- package/dist/approval/manager.cjs +9 -6
- package/dist/approval/manager.d.ts +2 -2
- package/dist/approval/manager.d.ts.map +1 -1
- package/dist/approval/manager.js +9 -6
- package/dist/approval/types.d.ts +13 -1
- package/dist/approval/types.d.ts.map +1 -1
- package/dist/context/index.cjs +4 -1
- package/dist/context/index.d.ts +1 -0
- package/dist/context/index.d.ts.map +1 -1
- package/dist/context/index.js +3 -1
- package/dist/context/types.cjs +3 -0
- package/dist/context/types.d.ts +2 -0
- package/dist/context/types.d.ts.map +1 -1
- package/dist/context/types.js +2 -0
- package/dist/events/index.cjs +3 -2
- package/dist/events/index.d.ts +23 -6
- package/dist/events/index.d.ts.map +1 -1
- package/dist/events/index.js +3 -2
- package/dist/index.browser.cjs +2 -0
- package/dist/index.browser.d.ts +1 -1
- package/dist/index.browser.d.ts.map +1 -1
- package/dist/index.browser.js +2 -0
- package/dist/llm/error-codes.cjs +1 -0
- package/dist/llm/error-codes.d.ts +1 -0
- package/dist/llm/error-codes.d.ts.map +1 -1
- package/dist/llm/error-codes.js +1 -0
- package/dist/llm/errors.cjs +12 -0
- package/dist/llm/errors.d.ts +7 -0
- package/dist/llm/errors.d.ts.map +1 -1
- package/dist/llm/errors.js +12 -0
- package/dist/llm/executor/index.d.ts +1 -1
- package/dist/llm/executor/index.d.ts.map +1 -1
- package/dist/llm/executor/stream-processor.cjs +46 -2
- package/dist/llm/executor/stream-processor.d.ts +9 -1
- package/dist/llm/executor/stream-processor.d.ts.map +1 -1
- package/dist/llm/executor/stream-processor.js +45 -2
- package/dist/llm/executor/turn-executor.cjs +79 -36
- package/dist/llm/executor/turn-executor.d.ts +15 -2
- package/dist/llm/executor/turn-executor.d.ts.map +1 -1
- package/dist/llm/executor/turn-executor.js +79 -36
- package/dist/llm/formatters/vercel.cjs +56 -3
- package/dist/llm/formatters/vercel.d.ts.map +1 -1
- package/dist/llm/formatters/vercel.js +54 -1
- package/dist/llm/providers/codex-app-server.cjs +3 -3
- package/dist/llm/providers/codex-app-server.js +3 -3
- package/dist/llm/providers/local/errors.cjs +4 -4
- package/dist/llm/providers/local/errors.js +4 -4
- package/dist/logger/logger.cjs +0 -4
- package/dist/logger/logger.d.ts.map +1 -1
- package/dist/logger/logger.js +0 -4
- package/dist/session/session-manager.cjs +0 -2
- package/dist/session/session-manager.d.ts.map +1 -1
- package/dist/session/session-manager.js +0 -2
- package/dist/tools/activity.cjs +16 -0
- package/dist/tools/activity.d.ts +2 -0
- package/dist/tools/activity.d.ts.map +1 -1
- package/dist/tools/activity.js +16 -0
- package/dist/tools/approval/tool-approval-policy.cjs +7 -1
- package/dist/tools/approval/tool-approval-policy.d.ts +3 -0
- package/dist/tools/approval/tool-approval-policy.d.ts.map +1 -1
- package/dist/tools/approval/tool-approval-policy.js +7 -1
- package/dist/tools/tool-call-metadata.cjs +12 -0
- package/dist/tools/tool-call-metadata.d.ts +3 -0
- package/dist/tools/tool-call-metadata.d.ts.map +1 -1
- package/dist/tools/tool-call-metadata.js +12 -0
- package/dist/tools/tool-manager.cjs +116 -14
- package/dist/tools/tool-manager.d.ts +5 -2
- package/dist/tools/tool-manager.d.ts.map +1 -1
- package/dist/tools/tool-manager.js +116 -14
- package/dist/tools/types.d.ts +16 -1
- package/dist/tools/types.d.ts.map +1 -1
- package/dist/utils/env.cjs +5 -0
- package/dist/utils/env.d.ts +1 -0
- package/dist/utils/env.d.ts.map +1 -1
- package/dist/utils/env.js +4 -0
- package/package.json +4 -4
|
@@ -47,11 +47,14 @@ var import_operation_span = require("../../telemetry/operation-span.js");
|
|
|
47
47
|
var import_provider_error = require("./provider-error.js");
|
|
48
48
|
var import_empty_response = require("./empty-response.js");
|
|
49
49
|
var import_errors = require("../errors.js");
|
|
50
|
+
var import_error_codes = require("../error-codes.js");
|
|
51
|
+
var import_DextoRuntimeError = require("../../errors/DextoRuntimeError.js");
|
|
50
52
|
var import_error_conversion = require("../../utils/error-conversion.js");
|
|
51
53
|
var import_codex_base_url = require("../providers/codex-base-url.js");
|
|
52
54
|
var import_tool_definitions = require("./tool-definitions.js");
|
|
53
55
|
var import_model_request_diagnostics = require("./model-request-diagnostics.js");
|
|
54
56
|
var import_manager2 = require("../../approval/manager.js");
|
|
57
|
+
var import_errors2 = require("../../approval/errors.js");
|
|
55
58
|
var import_content_audit = require("../../context/content-audit.js");
|
|
56
59
|
var import_content_clone = require("../../context/content-clone.js");
|
|
57
60
|
const MCP_TOOL_PREFIX = "mcp--";
|
|
@@ -189,6 +192,9 @@ const TurnDriverStateSchema = import_zod.z.discriminatedUnion("phase", [
|
|
|
189
192
|
function parseTurnDriverState(input) {
|
|
190
193
|
return TurnDriverStateSchema.parse(input);
|
|
191
194
|
}
|
|
195
|
+
function startsInBackground(prepared) {
|
|
196
|
+
return prepared.kind === "prepared" && prepared.prepared.kind !== "terminal" && prepared.prepared.call.meta?.runInBackground === true;
|
|
197
|
+
}
|
|
192
198
|
function approvalGateOf(prepared) {
|
|
193
199
|
if (prepared.kind === "terminal") return "terminal";
|
|
194
200
|
return prepared.prepared.kind === "ready" ? prepared.prepared.because : prepared.prepared.kind;
|
|
@@ -288,7 +294,12 @@ class TurnExecutor {
|
|
|
288
294
|
while (!stopped) {
|
|
289
295
|
const modelStep = await driver.runNextModelStep();
|
|
290
296
|
if (modelStep.result.finishReason === "tool-calls") {
|
|
291
|
-
await driver.executeToolCalls();
|
|
297
|
+
const toolCalls = await driver.executeToolCalls();
|
|
298
|
+
if (toolCalls.kind === "deferred") {
|
|
299
|
+
throw new Error(
|
|
300
|
+
"A tool approval was deferred in a turn that cannot pause"
|
|
301
|
+
);
|
|
302
|
+
}
|
|
292
303
|
}
|
|
293
304
|
const nextStep = await driver.decideNextStep();
|
|
294
305
|
if (nextStep.kind === "stop") {
|
|
@@ -408,7 +419,7 @@ class TurnExecutor {
|
|
|
408
419
|
}
|
|
409
420
|
};
|
|
410
421
|
return {
|
|
411
|
-
prepareNextModelStep: async () => {
|
|
422
|
+
prepareNextModelStep: async (stepOptions) => {
|
|
412
423
|
assertCanUseDriver();
|
|
413
424
|
if (stopped) {
|
|
414
425
|
throw new Error("Turn driver has already reached a stop decision");
|
|
@@ -431,6 +442,7 @@ class TurnExecutor {
|
|
|
431
442
|
},
|
|
432
443
|
() => this.prepareNextModelRequest({
|
|
433
444
|
contributorContext,
|
|
445
|
+
reasoning: stepOptions?.reasoning,
|
|
434
446
|
supportsTools: turn.supportsTools,
|
|
435
447
|
stepCount,
|
|
436
448
|
streaming: options.streaming
|
|
@@ -530,28 +542,31 @@ class TurnExecutor {
|
|
|
530
542
|
if (currentToolCallsExecuted) {
|
|
531
543
|
throw new Error("Tool calls for the current model step have already run");
|
|
532
544
|
}
|
|
533
|
-
if (result.finishReason
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
)
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
545
|
+
if (result.finishReason !== "tool-calls") {
|
|
546
|
+
return { kind: "executed" };
|
|
547
|
+
}
|
|
548
|
+
toolCallsRunning = true;
|
|
549
|
+
currentToolCallsExecuted = true;
|
|
550
|
+
try {
|
|
551
|
+
const toolCalls = await (0, import_operation_span.recordOperationSpan)(
|
|
552
|
+
{
|
|
553
|
+
name: "turn.execute_tool_calls",
|
|
554
|
+
componentName: "TurnExecutor",
|
|
555
|
+
attributes: {
|
|
556
|
+
...this.createTurnStepSpanAttributes(stepCount),
|
|
557
|
+
"tool.count": result.toolCalls.length
|
|
558
|
+
}
|
|
559
|
+
},
|
|
560
|
+
() => this.executeModelToolCalls(result.toolCalls),
|
|
561
|
+
this.logger
|
|
562
|
+
);
|
|
563
|
+
currentToolCallsExecuted = toolCalls.kind === "executed";
|
|
564
|
+
return toolCalls;
|
|
565
|
+
} catch (error) {
|
|
566
|
+
currentToolCallsExecuted = false;
|
|
567
|
+
throw error;
|
|
568
|
+
} finally {
|
|
569
|
+
toolCallsRunning = false;
|
|
555
570
|
}
|
|
556
571
|
},
|
|
557
572
|
decideNextStep: async () => {
|
|
@@ -1172,6 +1187,7 @@ class TurnExecutor {
|
|
|
1172
1187
|
);
|
|
1173
1188
|
}
|
|
1174
1189
|
}
|
|
1190
|
+
const stepReasoning = input.reasoning ?? this.config.reasoning;
|
|
1175
1191
|
const providerOptions = await (0, import_operation_span.recordOperationSpan)(
|
|
1176
1192
|
{
|
|
1177
1193
|
name: "llm.request_setup",
|
|
@@ -1184,7 +1200,7 @@ class TurnExecutor {
|
|
|
1184
1200
|
() => (0, import_provider_options.buildProviderOptions)({
|
|
1185
1201
|
provider: this.llmContext.provider,
|
|
1186
1202
|
model: this.llmContext.model,
|
|
1187
|
-
reasoning:
|
|
1203
|
+
reasoning: stepReasoning,
|
|
1188
1204
|
...this.config.llmRegistry !== void 0 && {
|
|
1189
1205
|
llmRegistry: this.config.llmRegistry
|
|
1190
1206
|
}
|
|
@@ -1195,12 +1211,12 @@ class TurnExecutor {
|
|
|
1195
1211
|
provider: this.llmContext.provider,
|
|
1196
1212
|
model: this.llmContext.model,
|
|
1197
1213
|
requestedReasoning: {
|
|
1198
|
-
variant:
|
|
1199
|
-
budgetTokens:
|
|
1214
|
+
variant: stepReasoning?.variant,
|
|
1215
|
+
budgetTokens: stepReasoning?.budgetTokens
|
|
1200
1216
|
},
|
|
1201
1217
|
providerOptions
|
|
1202
1218
|
});
|
|
1203
|
-
const reasoningVariant =
|
|
1219
|
+
const reasoningVariant = stepReasoning?.variant;
|
|
1204
1220
|
const reasoningBudgetTokens = (0, import_provider_options.getEffectiveReasoningBudgetTokens)(providerOptions);
|
|
1205
1221
|
const reasoning = reasoningVariant !== void 0 || reasoningBudgetTokens !== void 0 ? {
|
|
1206
1222
|
...reasoningVariant !== void 0 && { reasoningVariant },
|
|
@@ -1297,12 +1313,12 @@ class TurnExecutor {
|
|
|
1297
1313
|
})
|
|
1298
1314
|
},
|
|
1299
1315
|
() => streamProcessor.process(
|
|
1300
|
-
() => (0, import_ai.streamText)({
|
|
1316
|
+
(abortSignal) => (0, import_ai.streamText)({
|
|
1301
1317
|
model: this.model,
|
|
1302
1318
|
stopWhen: (0, import_ai.stepCountIs)(1),
|
|
1303
1319
|
maxRetries: 0,
|
|
1304
1320
|
tools: request.tools,
|
|
1305
|
-
abortSignal
|
|
1321
|
+
abortSignal,
|
|
1306
1322
|
messages: request.messages,
|
|
1307
1323
|
...this.config.maxOutputTokens !== void 0 && {
|
|
1308
1324
|
maxOutputTokens: this.config.maxOutputTokens
|
|
@@ -1404,6 +1420,7 @@ class TurnExecutor {
|
|
|
1404
1420
|
canRetryModelRequest(error, historyLengthChanged) {
|
|
1405
1421
|
if (historyLengthChanged) return false;
|
|
1406
1422
|
if (this.stepAbortController.signal.aborted) return false;
|
|
1423
|
+
if (error instanceof import_DextoRuntimeError.DextoRuntimeError) return error.code === import_error_codes.LLMErrorCode.STREAM_STALLED;
|
|
1407
1424
|
if (!import_ai.APICallError.isInstance?.(error)) return false;
|
|
1408
1425
|
return error.isRetryable;
|
|
1409
1426
|
}
|
|
@@ -1462,7 +1479,8 @@ class TurnExecutor {
|
|
|
1462
1479
|
)
|
|
1463
1480
|
);
|
|
1464
1481
|
}
|
|
1465
|
-
const
|
|
1482
|
+
const deferrable = !preparedCalls.some(startsInBackground);
|
|
1483
|
+
const settled = await Promise.allSettled(
|
|
1466
1484
|
preparedCalls.map(
|
|
1467
1485
|
(prepared) => (0, import_operation_span.recordOperationSpan)(
|
|
1468
1486
|
{
|
|
@@ -1477,11 +1495,21 @@ class TurnExecutor {
|
|
|
1477
1495
|
"tool.success": this.isToolExecutionSuccessful(result)
|
|
1478
1496
|
})
|
|
1479
1497
|
},
|
|
1480
|
-
() => this.executePreparedModelToolCall(prepared),
|
|
1498
|
+
() => this.executePreparedModelToolCall(prepared, deferrable),
|
|
1481
1499
|
this.logger
|
|
1482
1500
|
)
|
|
1483
1501
|
)
|
|
1484
1502
|
);
|
|
1503
|
+
const deferredApprovalIds = settled.flatMap(
|
|
1504
|
+
(outcome) => outcome.status === "rejected" && outcome.reason instanceof import_errors2.ApprovalDeferredError ? [outcome.reason.approvalId] : []
|
|
1505
|
+
);
|
|
1506
|
+
if (deferredApprovalIds.length > 0) {
|
|
1507
|
+
return { kind: "deferred", approvalIds: deferredApprovalIds };
|
|
1508
|
+
}
|
|
1509
|
+
const executionResults = settled.map((outcome) => {
|
|
1510
|
+
if (outcome.status === "rejected") throw outcome.reason;
|
|
1511
|
+
return outcome.value;
|
|
1512
|
+
});
|
|
1485
1513
|
const sanitizedResults = await Promise.all(
|
|
1486
1514
|
toolCalls.map((toolCall, index) => {
|
|
1487
1515
|
const executionResult = executionResults[index];
|
|
@@ -1513,6 +1541,7 @@ class TurnExecutor {
|
|
|
1513
1541
|
this.logger
|
|
1514
1542
|
);
|
|
1515
1543
|
}
|
|
1544
|
+
return { kind: "executed" };
|
|
1516
1545
|
}
|
|
1517
1546
|
async prepareModelToolCall(toolCall) {
|
|
1518
1547
|
if (this.stepAbortController.signal.aborted) {
|
|
@@ -1526,6 +1555,9 @@ class TurnExecutor {
|
|
|
1526
1555
|
}
|
|
1527
1556
|
let prepared;
|
|
1528
1557
|
try {
|
|
1558
|
+
if (!this.toolManager.isListedForModel(toolCall.toolName)) {
|
|
1559
|
+
throw new Error(`Tool '${toolCall.toolName}' not found`);
|
|
1560
|
+
}
|
|
1529
1561
|
prepared = await this.toolManager.prepareToolCall({
|
|
1530
1562
|
toolName: toolCall.toolName,
|
|
1531
1563
|
input: toolCall.input,
|
|
@@ -1557,7 +1589,7 @@ class TurnExecutor {
|
|
|
1557
1589
|
}
|
|
1558
1590
|
return { kind: "prepared", toolCall, prepared };
|
|
1559
1591
|
}
|
|
1560
|
-
async executePreparedModelToolCall(preparedCall) {
|
|
1592
|
+
async executePreparedModelToolCall(preparedCall, deferrable) {
|
|
1561
1593
|
try {
|
|
1562
1594
|
if (preparedCall.kind === "terminal") {
|
|
1563
1595
|
return preparedCall.modelVisibleResult;
|
|
@@ -1574,7 +1606,7 @@ class TurnExecutor {
|
|
|
1574
1606
|
}
|
|
1575
1607
|
const identity = this.resolveToolApprovalIdentity();
|
|
1576
1608
|
const recorded = await this.toolManager.recordApprovalRequest(prepared, identity);
|
|
1577
|
-
const approval = await this.requestApprovalDecisionWithAbort(recorded);
|
|
1609
|
+
const approval = await this.requestApprovalDecisionWithAbort(recorded, deferrable);
|
|
1578
1610
|
if (approval.kind === "terminal") {
|
|
1579
1611
|
return approval.modelVisibleResult;
|
|
1580
1612
|
}
|
|
@@ -1588,6 +1620,7 @@ class TurnExecutor {
|
|
|
1588
1620
|
this.resolveModelToolExecutionIdentity(preparedCall.toolCall.toolCallId)
|
|
1589
1621
|
);
|
|
1590
1622
|
} catch (error) {
|
|
1623
|
+
if (error instanceof import_errors2.ApprovalDeferredError) throw error;
|
|
1591
1624
|
return this.failedToolResult(
|
|
1592
1625
|
preparedCall.toolCall.toolName,
|
|
1593
1626
|
error,
|
|
@@ -1628,7 +1661,7 @@ class TurnExecutor {
|
|
|
1628
1661
|
}
|
|
1629
1662
|
}
|
|
1630
1663
|
}
|
|
1631
|
-
async requestApprovalDecisionWithAbort(recorded) {
|
|
1664
|
+
async requestApprovalDecisionWithAbort(recorded, deferrable) {
|
|
1632
1665
|
const abortSignal = this.stepAbortController.signal;
|
|
1633
1666
|
if (abortSignal.aborted) {
|
|
1634
1667
|
return {
|
|
@@ -1657,7 +1690,17 @@ class TurnExecutor {
|
|
|
1657
1690
|
});
|
|
1658
1691
|
try {
|
|
1659
1692
|
return await Promise.race([
|
|
1660
|
-
this.toolManager.requestApprovalDecision(recorded
|
|
1693
|
+
this.toolManager.requestApprovalDecision(recorded, { deferrable }).then(
|
|
1694
|
+
(response) => ({ kind: "response", response }),
|
|
1695
|
+
(error) => {
|
|
1696
|
+
if (error instanceof import_errors2.ApprovalDeferredError && error.approvalId !== recorded.request.approvalId) {
|
|
1697
|
+
throw new Error(
|
|
1698
|
+
`Approval ${recorded.request.approvalId} was answered by a deferral of ${error.approvalId}`
|
|
1699
|
+
);
|
|
1700
|
+
}
|
|
1701
|
+
throw error;
|
|
1702
|
+
}
|
|
1703
|
+
),
|
|
1661
1704
|
abortPromise
|
|
1662
1705
|
]);
|
|
1663
1706
|
} finally {
|
|
@@ -227,10 +227,23 @@ export type TurnDriverNextAction = {
|
|
|
227
227
|
stepCount: number;
|
|
228
228
|
finishReason: LLMFinishReason;
|
|
229
229
|
};
|
|
230
|
+
/**
|
|
231
|
+
* `deferred`: an approval handler deferred at least one call's approval. Every other call settled,
|
|
232
|
+
* nothing was persisted, and the tool calls stay unexecuted: a driver resumed from this state runs
|
|
233
|
+
* the batch again, completed calls replaying from the tool execution store.
|
|
234
|
+
*/
|
|
235
|
+
export type TurnDriverToolCalls = {
|
|
236
|
+
kind: 'executed';
|
|
237
|
+
} | {
|
|
238
|
+
kind: 'deferred';
|
|
239
|
+
approvalIds: string[];
|
|
240
|
+
};
|
|
230
241
|
export type TurnDriver = {
|
|
231
|
-
prepareNextModelStep(
|
|
242
|
+
prepareNextModelStep(options?: {
|
|
243
|
+
reasoning?: LLMReasoningConfig;
|
|
244
|
+
}): Promise<TurnDriverPreparedModelStep>;
|
|
232
245
|
runNextModelStep(): Promise<TurnDriverModelStep>;
|
|
233
|
-
executeToolCalls(): Promise<
|
|
246
|
+
executeToolCalls(): Promise<TurnDriverToolCalls>;
|
|
234
247
|
decideNextStep(): Promise<TurnDriverNextAction>;
|
|
235
248
|
finish(): Promise<ExecutorResult>;
|
|
236
249
|
fail(error: unknown): Promise<never>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"turn-executor.d.ts","sourceRoot":"","sources":["../../../src/llm/executor/turn-executor.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,aAAa,EAMb,KAAK,YAAY,EAEpB,MAAM,IAAI,CAAC;AACZ,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAa,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAE3E,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAW1D,OAAO,EACH,WAAW,EAId,MAAM,6BAA6B,CAAC;AACrC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAapD,OAAO,KAAK,EAAE,cAAc,EAAiB,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAEvF,OAAO,KAAK,EACR,UAAU,EACV,kBAAkB,EAClB,UAAU,EAEV,aAAa,EAEhB,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAC;AAEvD,OAAO,KAAK,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAC9E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AAIxE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;
|
|
1
|
+
{"version":3,"file":"turn-executor.d.ts","sourceRoot":"","sources":["../../../src/llm/executor/turn-executor.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,aAAa,E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,OAAO,CAAC,mBAAmB;IAY3B,OAAO,CAAC,gBAAgB;IAexB,OAAO,CAAC,2BAA2B;YAkBrB,uBAAuB;IAuBrC;;;;OAIG;YACW,yBAAyB;YAqCzB,sBAAsB;IAsCpC,OAAO,CAAC,YAAY;IAkBpB,OAAO,CAAC,2BAA2B;IAanC,OAAO,CAAC,iCAAiC;IAUzC,OAAO,CAAC,wBAAwB;IA6BhC,OAAO,CAAC,yBAAyB;IAIjC,OAAO,CAAC,4BAA4B;IAQpC,OAAO,CAAC,wBAAwB;IAMhC,OAAO,CAAC,6BAA6B;IAYrC;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAAU;IACnD,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,0BAA0B,CAAO;IACzD,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,aAAa,CAAU;IAE/C,OAAO,CAAC,qBAAqB;IAS7B;;;;OAIG;IACH,OAAO,CAAC,6BAA6B;IAgBrC;;;;;;;;;;;;OAYG;YACW,mBAAmB;IA2DjC;;;;;;OAMG;IACH,OAAO,CAAC,kBAAkB;IAgB1B;;;OAGG;IACH,OAAO,CAAC,OAAO;IASf;;;;;;OAMG;IACH,OAAO,CAAC,aAAa;IAOrB;;;;;;OAMG;IACH,OAAO,CAAC,uBAAuB;IAO/B;;;;;;;;;;;OAWG;YACW,cAAc;IAwF5B;;OAEG;IACH,OAAO,CAAC,sBAAsB;IAoB9B,OAAO,CAAC,qBAAqB;IAK7B;;OAEG;IACH,OAAO,CAAC,gBAAgB;CAQ3B"}
|
|
@@ -27,6 +27,8 @@ import {
|
|
|
27
27
|
} from "./provider-error.js";
|
|
28
28
|
import { isEmptyModelResponse } from "./empty-response.js";
|
|
29
29
|
import { LLMError } from "../errors.js";
|
|
30
|
+
import { LLMErrorCode } from "../error-codes.js";
|
|
31
|
+
import { DextoRuntimeError } from "../../errors/DextoRuntimeError.js";
|
|
30
32
|
import { toError } from "../../utils/error-conversion.js";
|
|
31
33
|
import { isCodexBaseURL } from "../providers/codex-base-url.js";
|
|
32
34
|
import { createModelToolDefinitions } from "./tool-definitions.js";
|
|
@@ -35,6 +37,7 @@ import {
|
|
|
35
37
|
modelRequestDiagnosticAttributes
|
|
36
38
|
} from "./model-request-diagnostics.js";
|
|
37
39
|
import { approvalResponseToDecisionInput } from "../../approval/manager.js";
|
|
40
|
+
import { ApprovalDeferredError } from "../../approval/errors.js";
|
|
38
41
|
import {
|
|
39
42
|
describeContentPartsForAudit,
|
|
40
43
|
describeInternalMessageTailForAudit
|
|
@@ -175,6 +178,9 @@ const TurnDriverStateSchema = z.discriminatedUnion("phase", [
|
|
|
175
178
|
function parseTurnDriverState(input) {
|
|
176
179
|
return TurnDriverStateSchema.parse(input);
|
|
177
180
|
}
|
|
181
|
+
function startsInBackground(prepared) {
|
|
182
|
+
return prepared.kind === "prepared" && prepared.prepared.kind !== "terminal" && prepared.prepared.call.meta?.runInBackground === true;
|
|
183
|
+
}
|
|
178
184
|
function approvalGateOf(prepared) {
|
|
179
185
|
if (prepared.kind === "terminal") return "terminal";
|
|
180
186
|
return prepared.prepared.kind === "ready" ? prepared.prepared.because : prepared.prepared.kind;
|
|
@@ -274,7 +280,12 @@ class TurnExecutor {
|
|
|
274
280
|
while (!stopped) {
|
|
275
281
|
const modelStep = await driver.runNextModelStep();
|
|
276
282
|
if (modelStep.result.finishReason === "tool-calls") {
|
|
277
|
-
await driver.executeToolCalls();
|
|
283
|
+
const toolCalls = await driver.executeToolCalls();
|
|
284
|
+
if (toolCalls.kind === "deferred") {
|
|
285
|
+
throw new Error(
|
|
286
|
+
"A tool approval was deferred in a turn that cannot pause"
|
|
287
|
+
);
|
|
288
|
+
}
|
|
278
289
|
}
|
|
279
290
|
const nextStep = await driver.decideNextStep();
|
|
280
291
|
if (nextStep.kind === "stop") {
|
|
@@ -394,7 +405,7 @@ class TurnExecutor {
|
|
|
394
405
|
}
|
|
395
406
|
};
|
|
396
407
|
return {
|
|
397
|
-
prepareNextModelStep: async () => {
|
|
408
|
+
prepareNextModelStep: async (stepOptions) => {
|
|
398
409
|
assertCanUseDriver();
|
|
399
410
|
if (stopped) {
|
|
400
411
|
throw new Error("Turn driver has already reached a stop decision");
|
|
@@ -417,6 +428,7 @@ class TurnExecutor {
|
|
|
417
428
|
},
|
|
418
429
|
() => this.prepareNextModelRequest({
|
|
419
430
|
contributorContext,
|
|
431
|
+
reasoning: stepOptions?.reasoning,
|
|
420
432
|
supportsTools: turn.supportsTools,
|
|
421
433
|
stepCount,
|
|
422
434
|
streaming: options.streaming
|
|
@@ -516,28 +528,31 @@ class TurnExecutor {
|
|
|
516
528
|
if (currentToolCallsExecuted) {
|
|
517
529
|
throw new Error("Tool calls for the current model step have already run");
|
|
518
530
|
}
|
|
519
|
-
if (result.finishReason
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
)
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
531
|
+
if (result.finishReason !== "tool-calls") {
|
|
532
|
+
return { kind: "executed" };
|
|
533
|
+
}
|
|
534
|
+
toolCallsRunning = true;
|
|
535
|
+
currentToolCallsExecuted = true;
|
|
536
|
+
try {
|
|
537
|
+
const toolCalls = await recordOperationSpan(
|
|
538
|
+
{
|
|
539
|
+
name: "turn.execute_tool_calls",
|
|
540
|
+
componentName: "TurnExecutor",
|
|
541
|
+
attributes: {
|
|
542
|
+
...this.createTurnStepSpanAttributes(stepCount),
|
|
543
|
+
"tool.count": result.toolCalls.length
|
|
544
|
+
}
|
|
545
|
+
},
|
|
546
|
+
() => this.executeModelToolCalls(result.toolCalls),
|
|
547
|
+
this.logger
|
|
548
|
+
);
|
|
549
|
+
currentToolCallsExecuted = toolCalls.kind === "executed";
|
|
550
|
+
return toolCalls;
|
|
551
|
+
} catch (error) {
|
|
552
|
+
currentToolCallsExecuted = false;
|
|
553
|
+
throw error;
|
|
554
|
+
} finally {
|
|
555
|
+
toolCallsRunning = false;
|
|
541
556
|
}
|
|
542
557
|
},
|
|
543
558
|
decideNextStep: async () => {
|
|
@@ -1158,6 +1173,7 @@ class TurnExecutor {
|
|
|
1158
1173
|
);
|
|
1159
1174
|
}
|
|
1160
1175
|
}
|
|
1176
|
+
const stepReasoning = input.reasoning ?? this.config.reasoning;
|
|
1161
1177
|
const providerOptions = await recordOperationSpan(
|
|
1162
1178
|
{
|
|
1163
1179
|
name: "llm.request_setup",
|
|
@@ -1170,7 +1186,7 @@ class TurnExecutor {
|
|
|
1170
1186
|
() => buildProviderOptions({
|
|
1171
1187
|
provider: this.llmContext.provider,
|
|
1172
1188
|
model: this.llmContext.model,
|
|
1173
|
-
reasoning:
|
|
1189
|
+
reasoning: stepReasoning,
|
|
1174
1190
|
...this.config.llmRegistry !== void 0 && {
|
|
1175
1191
|
llmRegistry: this.config.llmRegistry
|
|
1176
1192
|
}
|
|
@@ -1181,12 +1197,12 @@ class TurnExecutor {
|
|
|
1181
1197
|
provider: this.llmContext.provider,
|
|
1182
1198
|
model: this.llmContext.model,
|
|
1183
1199
|
requestedReasoning: {
|
|
1184
|
-
variant:
|
|
1185
|
-
budgetTokens:
|
|
1200
|
+
variant: stepReasoning?.variant,
|
|
1201
|
+
budgetTokens: stepReasoning?.budgetTokens
|
|
1186
1202
|
},
|
|
1187
1203
|
providerOptions
|
|
1188
1204
|
});
|
|
1189
|
-
const reasoningVariant =
|
|
1205
|
+
const reasoningVariant = stepReasoning?.variant;
|
|
1190
1206
|
const reasoningBudgetTokens = getEffectiveReasoningBudgetTokens(providerOptions);
|
|
1191
1207
|
const reasoning = reasoningVariant !== void 0 || reasoningBudgetTokens !== void 0 ? {
|
|
1192
1208
|
...reasoningVariant !== void 0 && { reasoningVariant },
|
|
@@ -1283,12 +1299,12 @@ class TurnExecutor {
|
|
|
1283
1299
|
})
|
|
1284
1300
|
},
|
|
1285
1301
|
() => streamProcessor.process(
|
|
1286
|
-
() => streamText({
|
|
1302
|
+
(abortSignal) => streamText({
|
|
1287
1303
|
model: this.model,
|
|
1288
1304
|
stopWhen: stepCountIs(1),
|
|
1289
1305
|
maxRetries: 0,
|
|
1290
1306
|
tools: request.tools,
|
|
1291
|
-
abortSignal
|
|
1307
|
+
abortSignal,
|
|
1292
1308
|
messages: request.messages,
|
|
1293
1309
|
...this.config.maxOutputTokens !== void 0 && {
|
|
1294
1310
|
maxOutputTokens: this.config.maxOutputTokens
|
|
@@ -1390,6 +1406,7 @@ class TurnExecutor {
|
|
|
1390
1406
|
canRetryModelRequest(error, historyLengthChanged) {
|
|
1391
1407
|
if (historyLengthChanged) return false;
|
|
1392
1408
|
if (this.stepAbortController.signal.aborted) return false;
|
|
1409
|
+
if (error instanceof DextoRuntimeError) return error.code === LLMErrorCode.STREAM_STALLED;
|
|
1393
1410
|
if (!APICallError.isInstance?.(error)) return false;
|
|
1394
1411
|
return error.isRetryable;
|
|
1395
1412
|
}
|
|
@@ -1448,7 +1465,8 @@ class TurnExecutor {
|
|
|
1448
1465
|
)
|
|
1449
1466
|
);
|
|
1450
1467
|
}
|
|
1451
|
-
const
|
|
1468
|
+
const deferrable = !preparedCalls.some(startsInBackground);
|
|
1469
|
+
const settled = await Promise.allSettled(
|
|
1452
1470
|
preparedCalls.map(
|
|
1453
1471
|
(prepared) => recordOperationSpan(
|
|
1454
1472
|
{
|
|
@@ -1463,11 +1481,21 @@ class TurnExecutor {
|
|
|
1463
1481
|
"tool.success": this.isToolExecutionSuccessful(result)
|
|
1464
1482
|
})
|
|
1465
1483
|
},
|
|
1466
|
-
() => this.executePreparedModelToolCall(prepared),
|
|
1484
|
+
() => this.executePreparedModelToolCall(prepared, deferrable),
|
|
1467
1485
|
this.logger
|
|
1468
1486
|
)
|
|
1469
1487
|
)
|
|
1470
1488
|
);
|
|
1489
|
+
const deferredApprovalIds = settled.flatMap(
|
|
1490
|
+
(outcome) => outcome.status === "rejected" && outcome.reason instanceof ApprovalDeferredError ? [outcome.reason.approvalId] : []
|
|
1491
|
+
);
|
|
1492
|
+
if (deferredApprovalIds.length > 0) {
|
|
1493
|
+
return { kind: "deferred", approvalIds: deferredApprovalIds };
|
|
1494
|
+
}
|
|
1495
|
+
const executionResults = settled.map((outcome) => {
|
|
1496
|
+
if (outcome.status === "rejected") throw outcome.reason;
|
|
1497
|
+
return outcome.value;
|
|
1498
|
+
});
|
|
1471
1499
|
const sanitizedResults = await Promise.all(
|
|
1472
1500
|
toolCalls.map((toolCall, index) => {
|
|
1473
1501
|
const executionResult = executionResults[index];
|
|
@@ -1499,6 +1527,7 @@ class TurnExecutor {
|
|
|
1499
1527
|
this.logger
|
|
1500
1528
|
);
|
|
1501
1529
|
}
|
|
1530
|
+
return { kind: "executed" };
|
|
1502
1531
|
}
|
|
1503
1532
|
async prepareModelToolCall(toolCall) {
|
|
1504
1533
|
if (this.stepAbortController.signal.aborted) {
|
|
@@ -1512,6 +1541,9 @@ class TurnExecutor {
|
|
|
1512
1541
|
}
|
|
1513
1542
|
let prepared;
|
|
1514
1543
|
try {
|
|
1544
|
+
if (!this.toolManager.isListedForModel(toolCall.toolName)) {
|
|
1545
|
+
throw new Error(`Tool '${toolCall.toolName}' not found`);
|
|
1546
|
+
}
|
|
1515
1547
|
prepared = await this.toolManager.prepareToolCall({
|
|
1516
1548
|
toolName: toolCall.toolName,
|
|
1517
1549
|
input: toolCall.input,
|
|
@@ -1543,7 +1575,7 @@ class TurnExecutor {
|
|
|
1543
1575
|
}
|
|
1544
1576
|
return { kind: "prepared", toolCall, prepared };
|
|
1545
1577
|
}
|
|
1546
|
-
async executePreparedModelToolCall(preparedCall) {
|
|
1578
|
+
async executePreparedModelToolCall(preparedCall, deferrable) {
|
|
1547
1579
|
try {
|
|
1548
1580
|
if (preparedCall.kind === "terminal") {
|
|
1549
1581
|
return preparedCall.modelVisibleResult;
|
|
@@ -1560,7 +1592,7 @@ class TurnExecutor {
|
|
|
1560
1592
|
}
|
|
1561
1593
|
const identity = this.resolveToolApprovalIdentity();
|
|
1562
1594
|
const recorded = await this.toolManager.recordApprovalRequest(prepared, identity);
|
|
1563
|
-
const approval = await this.requestApprovalDecisionWithAbort(recorded);
|
|
1595
|
+
const approval = await this.requestApprovalDecisionWithAbort(recorded, deferrable);
|
|
1564
1596
|
if (approval.kind === "terminal") {
|
|
1565
1597
|
return approval.modelVisibleResult;
|
|
1566
1598
|
}
|
|
@@ -1574,6 +1606,7 @@ class TurnExecutor {
|
|
|
1574
1606
|
this.resolveModelToolExecutionIdentity(preparedCall.toolCall.toolCallId)
|
|
1575
1607
|
);
|
|
1576
1608
|
} catch (error) {
|
|
1609
|
+
if (error instanceof ApprovalDeferredError) throw error;
|
|
1577
1610
|
return this.failedToolResult(
|
|
1578
1611
|
preparedCall.toolCall.toolName,
|
|
1579
1612
|
error,
|
|
@@ -1614,7 +1647,7 @@ class TurnExecutor {
|
|
|
1614
1647
|
}
|
|
1615
1648
|
}
|
|
1616
1649
|
}
|
|
1617
|
-
async requestApprovalDecisionWithAbort(recorded) {
|
|
1650
|
+
async requestApprovalDecisionWithAbort(recorded, deferrable) {
|
|
1618
1651
|
const abortSignal = this.stepAbortController.signal;
|
|
1619
1652
|
if (abortSignal.aborted) {
|
|
1620
1653
|
return {
|
|
@@ -1643,7 +1676,17 @@ class TurnExecutor {
|
|
|
1643
1676
|
});
|
|
1644
1677
|
try {
|
|
1645
1678
|
return await Promise.race([
|
|
1646
|
-
this.toolManager.requestApprovalDecision(recorded
|
|
1679
|
+
this.toolManager.requestApprovalDecision(recorded, { deferrable }).then(
|
|
1680
|
+
(response) => ({ kind: "response", response }),
|
|
1681
|
+
(error) => {
|
|
1682
|
+
if (error instanceof ApprovalDeferredError && error.approvalId !== recorded.request.approvalId) {
|
|
1683
|
+
throw new Error(
|
|
1684
|
+
`Approval ${recorded.request.approvalId} was answered by a deferral of ${error.approvalId}`
|
|
1685
|
+
);
|
|
1686
|
+
}
|
|
1687
|
+
throw error;
|
|
1688
|
+
}
|
|
1689
|
+
),
|
|
1647
1690
|
abortPromise
|
|
1648
1691
|
]);
|
|
1649
1692
|
} finally {
|
|
@@ -22,8 +22,61 @@ __export(vercel_exports, {
|
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(vercel_exports);
|
|
24
24
|
var import_llm = require("@dexto/llm");
|
|
25
|
+
var import_types = require("../../context/types.js");
|
|
25
26
|
var import_utils = require("../../context/utils.js");
|
|
26
|
-
var
|
|
27
|
+
var import_types2 = require("../../logger/v2/types.js");
|
|
28
|
+
function normalizeInterleavedToolHistory(history) {
|
|
29
|
+
const normalized = [];
|
|
30
|
+
for (let index = 0; index < history.length; ) {
|
|
31
|
+
const first = history[index];
|
|
32
|
+
if (!first || first.role === "system" || first.metadata?.[import_types.IMPORTED_TRANSCRIPT_METADATA_KEY] !== true) {
|
|
33
|
+
if (first) normalized.push(first);
|
|
34
|
+
index++;
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
let end = index;
|
|
38
|
+
const resultIndexes = /* @__PURE__ */ new Map();
|
|
39
|
+
while (end < history.length) {
|
|
40
|
+
const message = history[end];
|
|
41
|
+
if (!message || message.role === "system" || message.metadata?.[import_types.IMPORTED_TRANSCRIPT_METADATA_KEY] !== true)
|
|
42
|
+
break;
|
|
43
|
+
if (message.role === "tool") {
|
|
44
|
+
const indexes = resultIndexes.get(message.toolCallId) ?? [];
|
|
45
|
+
indexes.push(end);
|
|
46
|
+
resultIndexes.set(message.toolCallId, indexes);
|
|
47
|
+
}
|
|
48
|
+
end++;
|
|
49
|
+
}
|
|
50
|
+
const moved = /* @__PURE__ */ new Set();
|
|
51
|
+
for (let cursor = index; cursor < end; cursor++) {
|
|
52
|
+
if (moved.has(cursor)) continue;
|
|
53
|
+
const message = history[cursor];
|
|
54
|
+
if (!message) continue;
|
|
55
|
+
normalized.push(message);
|
|
56
|
+
if (message.role !== "assistant" || !message.toolCalls?.length) continue;
|
|
57
|
+
const matching = [];
|
|
58
|
+
for (const call of message.toolCalls) {
|
|
59
|
+
const indexes = resultIndexes.get(call.id);
|
|
60
|
+
if (!indexes) continue;
|
|
61
|
+
while (indexes.length > 0) {
|
|
62
|
+
const candidate = indexes[0];
|
|
63
|
+
if (candidate === void 0 || candidate > cursor) break;
|
|
64
|
+
indexes.shift();
|
|
65
|
+
}
|
|
66
|
+
const resultIndex = indexes.shift();
|
|
67
|
+
if (resultIndex !== void 0) matching.push(resultIndex);
|
|
68
|
+
}
|
|
69
|
+
matching.sort((left, right) => left - right);
|
|
70
|
+
for (const resultIndex of matching) {
|
|
71
|
+
const result = history[resultIndex];
|
|
72
|
+
if (result) normalized.push(result);
|
|
73
|
+
moved.add(resultIndex);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
index = end;
|
|
77
|
+
}
|
|
78
|
+
return normalized;
|
|
79
|
+
}
|
|
27
80
|
function toUrlIfString(value) {
|
|
28
81
|
if (typeof value === "string" && /^https?:\/\//i.test(value)) {
|
|
29
82
|
try {
|
|
@@ -110,7 +163,7 @@ class VercelMessageFormatter {
|
|
|
110
163
|
logger;
|
|
111
164
|
llmRegistry;
|
|
112
165
|
constructor(logger, llmRegistry = import_llm.DEFAULT_MODEL_REGISTRY) {
|
|
113
|
-
this.logger = logger.createChild(
|
|
166
|
+
this.logger = logger.createChild(import_types2.DextoLogComponent.LLM);
|
|
114
167
|
this.llmRegistry = llmRegistry;
|
|
115
168
|
}
|
|
116
169
|
/**
|
|
@@ -181,7 +234,7 @@ class VercelMessageFormatter {
|
|
|
181
234
|
pendingRemoteToolMedia = [];
|
|
182
235
|
}
|
|
183
236
|
};
|
|
184
|
-
for (const msg of filteredHistory) {
|
|
237
|
+
for (const msg of normalizeInterleavedToolHistory(filteredHistory)) {
|
|
185
238
|
if (msg.role !== "tool" && pendingToolCalls.size > 0) {
|
|
186
239
|
closePendingToolTurn();
|
|
187
240
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vercel.d.ts","sourceRoot":"","sources":["../../../src/llm/formatters/vercel.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAoC,MAAM,IAAI,CAAC;AACzE,OAAO,EAA0B,KAAK,UAAU,EAAE,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC;AACzF,OAAO,KAAK,EACR,eAAe,EAIlB,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"vercel.d.ts","sourceRoot":"","sources":["../../../src/llm/formatters/vercel.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAoC,MAAM,IAAI,CAAC;AACzE,OAAO,EAA0B,KAAK,UAAU,EAAE,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC;AACzF,OAAO,KAAK,EACR,eAAe,EAIlB,MAAM,wBAAwB,CAAC;AAQhC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAC;AAuLvD;;;;;;;;;;GAUG;AACH,qBAAa,sBAAsB;IAC/B,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAgB;gBAEhC,MAAM,EAAE,MAAM,EAAE,WAAW,GAAE,aAAsC;IAI/E;;;;;;OAMG;IACH,MAAM,CACF,OAAO,EAAE,QAAQ,CAAC,eAAe,EAAE,CAAC,EACpC,OAAO,EAAE,UAAU,EACnB,YAAY,EAAE,MAAM,GAAG,IAAI,GAC5B,YAAY,EAAE;IA0LjB;;;;;;OAMG;IACH,kBAAkB,IAAI,IAAI;IAI1B,OAAO,CAAC,wBAAwB;IAUhC,OAAO,CAAC,sBAAsB;IAmG9B,OAAO,CAAC,iBAAiB;CAmG5B"}
|