@mcp-abap-adt/llm-agent-libs 33.0.1 → 34.0.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/README.md +67 -22
- package/dist/adapters/llm-adapter.d.ts +2 -2
- package/dist/adapters/llm-adapter.d.ts.map +1 -1
- package/dist/adapters/llm-adapter.js +82 -34
- package/dist/adapters/llm-adapter.js.map +1 -1
- package/dist/adapters/llm-provider-bridge.d.ts +1 -4
- package/dist/adapters/llm-provider-bridge.d.ts.map +1 -1
- package/dist/adapters/llm-provider-bridge.js +16 -13
- package/dist/adapters/llm-provider-bridge.js.map +1 -1
- package/dist/agent/rag-helpers.d.ts +4 -6
- package/dist/agent/rag-helpers.d.ts.map +1 -1
- package/dist/agent/rag-helpers.js +3 -41
- package/dist/agent/rag-helpers.js.map +1 -1
- package/dist/agent/rag-orchestrator-types.d.ts +3 -0
- package/dist/agent/rag-orchestrator-types.d.ts.map +1 -1
- package/dist/agent/rag-orchestrator.d.ts +0 -1
- package/dist/agent/rag-orchestrator.d.ts.map +1 -1
- package/dist/agent/rag-orchestrator.js +9 -29
- package/dist/agent/rag-orchestrator.js.map +1 -1
- package/dist/agent.d.ts +11 -2
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +171 -236
- package/dist/agent.js.map +1 -1
- package/dist/builder-types.d.ts +1 -1
- package/dist/builder-types.d.ts.map +1 -1
- package/dist/builder.d.ts +7 -7
- package/dist/builder.d.ts.map +1 -1
- package/dist/builder.js +8 -17
- package/dist/builder.js.map +1 -1
- package/dist/classifier/llm-classifier.d.ts.map +1 -1
- package/dist/classifier/llm-classifier.js +2 -1
- package/dist/classifier/llm-classifier.js.map +1 -1
- package/dist/config/config-watcher.d.ts +3 -0
- package/dist/config/config-watcher.d.ts.map +1 -1
- package/dist/config/config-watcher.js +1 -0
- package/dist/config/config-watcher.js.map +1 -1
- package/dist/context/context-assembler.d.ts.map +1 -1
- package/dist/context/context-assembler.js +23 -23
- package/dist/context/context-assembler.js.map +1 -1
- package/dist/coordinator/adaptive/compose-step-input.d.ts +1 -5
- package/dist/coordinator/adaptive/compose-step-input.d.ts.map +1 -1
- package/dist/coordinator/adaptive/compose-step-input.js +6 -17
- package/dist/coordinator/adaptive/compose-step-input.js.map +1 -1
- package/dist/coordinator/adaptive/index.d.ts +1 -1
- package/dist/coordinator/adaptive/index.d.ts.map +1 -1
- package/dist/coordinator/adaptive/index.js +1 -1
- package/dist/coordinator/adaptive/index.js.map +1 -1
- package/dist/coordinator/adaptive/llm-adaptive-controller.d.ts.map +1 -1
- package/dist/coordinator/adaptive/llm-adaptive-controller.js +3 -9
- package/dist/coordinator/adaptive/llm-adaptive-controller.js.map +1 -1
- package/dist/coordinator/dag/dag-plan-interpreter.js +1 -1
- package/dist/coordinator/dag/dag-plan-interpreter.js.map +1 -1
- package/dist/coordinator/dialogue/dialogue-session.d.ts +0 -3
- package/dist/coordinator/dialogue/dialogue-session.d.ts.map +1 -1
- package/dist/coordinator/dialogue/dialogue-session.js +15 -59
- package/dist/coordinator/dialogue/dialogue-session.js.map +1 -1
- package/dist/coordinator/dialogue/index.d.ts +2 -2
- package/dist/coordinator/dialogue/index.d.ts.map +1 -1
- package/dist/coordinator/dialogue/index.js +2 -2
- package/dist/coordinator/dialogue/index.js.map +1 -1
- package/dist/coordinator/dialogue/planner-context-builder.d.ts +2 -3
- package/dist/coordinator/dialogue/planner-context-builder.d.ts.map +1 -1
- package/dist/coordinator/dialogue/planner-context-builder.js +13 -73
- package/dist/coordinator/dialogue/planner-context-builder.js.map +1 -1
- package/dist/coordinator/dialogue/planner-tools-client.d.ts.map +1 -1
- package/dist/coordinator/dialogue/planner-tools-client.js +7 -19
- package/dist/coordinator/dialogue/planner-tools-client.js.map +1 -1
- package/dist/coordinator/dialogue/structured-plan.d.ts +1 -1
- package/dist/coordinator/dialogue/structured-plan.d.ts.map +1 -1
- package/dist/coordinator/dialogue/structured-plan.js +6 -7
- package/dist/coordinator/dialogue/structured-plan.js.map +1 -1
- package/dist/coordinator/dialogue/submit-report-tool.d.ts +1 -1
- package/dist/coordinator/dialogue/submit-report-tool.d.ts.map +1 -1
- package/dist/coordinator/dialogue/submit-report-tool.js +4 -17
- package/dist/coordinator/dialogue/submit-report-tool.js.map +1 -1
- package/dist/coordinator/dialogue/task-board.d.ts +0 -1
- package/dist/coordinator/dialogue/task-board.d.ts.map +1 -1
- package/dist/coordinator/dialogue/task-board.js +6 -7
- package/dist/coordinator/dialogue/task-board.js.map +1 -1
- package/dist/coordinator/dialogue/task-reference.d.ts +1 -3
- package/dist/coordinator/dialogue/task-reference.d.ts.map +1 -1
- package/dist/coordinator/dialogue/task-reference.js +3 -10
- package/dist/coordinator/dialogue/task-reference.js.map +1 -1
- package/dist/coordinator/dialogue/tool-trace-render.d.ts.map +1 -1
- package/dist/coordinator/dialogue/tool-trace-render.js +9 -29
- package/dist/coordinator/dialogue/tool-trace-render.js.map +1 -1
- package/dist/coordinator/dispatch/self.d.ts.map +1 -1
- package/dist/coordinator/dispatch/self.js +17 -21
- package/dist/coordinator/dispatch/self.js.map +1 -1
- package/dist/coordinator/planning/replan-on-error.d.ts.map +1 -1
- package/dist/coordinator/planning/replan-on-error.js +1 -1
- package/dist/coordinator/planning/replan-on-error.js.map +1 -1
- package/dist/coordinator/stepper/cyclic-react-executor.d.ts.map +1 -1
- package/dist/coordinator/stepper/cyclic-react-executor.js +35 -34
- package/dist/coordinator/stepper/cyclic-react-executor.js.map +1 -1
- package/dist/coordinator/stepper/logging-llm.d.ts.map +1 -1
- package/dist/coordinator/stepper/logging-llm.js +4 -1
- package/dist/coordinator/stepper/logging-llm.js.map +1 -1
- package/dist/coordinator/stepper/stepper-interpreter.d.ts.map +1 -1
- package/dist/coordinator/stepper/stepper-interpreter.js +5 -17
- package/dist/coordinator/stepper/stepper-interpreter.js.map +1 -1
- package/dist/history/history-memory.d.ts +1 -0
- package/dist/history/history-memory.d.ts.map +1 -1
- package/dist/history/history-memory.js +5 -1
- package/dist/history/history-memory.js.map +1 -1
- package/dist/history/history-summarizer.js +1 -1
- package/dist/history/history-summarizer.js.map +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/interfaces/pipeline.d.ts +1 -1
- package/dist/interfaces/pipeline.d.ts.map +1 -1
- package/dist/logger/default-request-logger.d.ts.map +1 -1
- package/dist/logger/default-request-logger.js +3 -0
- package/dist/logger/default-request-logger.js.map +1 -1
- package/dist/logger/session-request-logger.d.ts.map +1 -1
- package/dist/logger/session-request-logger.js +8 -1
- package/dist/logger/session-request-logger.js.map +1 -1
- package/dist/pipeline/context/tool-loop-context/window-context-strategy.d.ts +1 -1
- package/dist/pipeline/context/tool-loop-context/window-context-strategy.d.ts.map +1 -1
- package/dist/pipeline/context/tool-loop-context/window-context-strategy.js +6 -3
- package/dist/pipeline/context/tool-loop-context/window-context-strategy.js.map +1 -1
- package/dist/pipeline/context/tool-loop-control-history.d.ts +15 -0
- package/dist/pipeline/context/tool-loop-control-history.d.ts.map +1 -0
- package/dist/pipeline/context/tool-loop-control-history.js +72 -0
- package/dist/pipeline/context/tool-loop-control-history.js.map +1 -0
- package/dist/pipeline/context.d.ts +5 -2
- package/dist/pipeline/context.d.ts.map +1 -1
- package/dist/pipeline/default-pipeline.d.ts +2 -2
- package/dist/pipeline/default-pipeline.d.ts.map +1 -1
- package/dist/pipeline/default-pipeline.js +7 -7
- package/dist/pipeline/default-pipeline.js.map +1 -1
- package/dist/pipeline/executor.d.ts.map +1 -1
- package/dist/pipeline/executor.js +6 -0
- package/dist/pipeline/executor.js.map +1 -1
- package/dist/pipeline/handlers/adaptive-coordinator.d.ts.map +1 -1
- package/dist/pipeline/handlers/adaptive-coordinator.js +9 -6
- package/dist/pipeline/handlers/adaptive-coordinator.js.map +1 -1
- package/dist/pipeline/handlers/assemble.d.ts +0 -9
- package/dist/pipeline/handlers/assemble.d.ts.map +1 -1
- package/dist/pipeline/handlers/assemble.js +7 -11
- package/dist/pipeline/handlers/assemble.js.map +1 -1
- package/dist/pipeline/handlers/dag-coordinator.d.ts.map +1 -1
- package/dist/pipeline/handlers/dag-coordinator.js +2 -1
- package/dist/pipeline/handlers/dag-coordinator.js.map +1 -1
- package/dist/pipeline/handlers/history-upsert.d.ts +2 -0
- package/dist/pipeline/handlers/history-upsert.d.ts.map +1 -1
- package/dist/pipeline/handlers/history-upsert.js +6 -1
- package/dist/pipeline/handlers/history-upsert.js.map +1 -1
- package/dist/pipeline/handlers/pass-through.d.ts +3 -1
- package/dist/pipeline/handlers/pass-through.d.ts.map +1 -1
- package/dist/pipeline/handlers/pass-through.js +75 -5
- package/dist/pipeline/handlers/pass-through.js.map +1 -1
- package/dist/pipeline/handlers/summarize.d.ts +2 -12
- package/dist/pipeline/handlers/summarize.d.ts.map +1 -1
- package/dist/pipeline/handlers/summarize.js +2 -63
- package/dist/pipeline/handlers/summarize.js.map +1 -1
- package/dist/pipeline/handlers/tool-loop-core.d.ts +15 -9
- package/dist/pipeline/handlers/tool-loop-core.d.ts.map +1 -1
- package/dist/pipeline/handlers/tool-loop-core.js +128 -59
- package/dist/pipeline/handlers/tool-loop-core.js.map +1 -1
- package/dist/pipeline/handlers/tool-loop.d.ts.map +1 -1
- package/dist/pipeline/handlers/tool-loop.js +116 -293
- package/dist/pipeline/handlers/tool-loop.js.map +1 -1
- package/dist/pipeline/handlers/tool-response-round.d.ts +30 -0
- package/dist/pipeline/handlers/tool-response-round.d.ts.map +1 -0
- package/dist/pipeline/handlers/tool-response-round.js +85 -0
- package/dist/pipeline/handlers/tool-response-round.js.map +1 -0
- package/dist/pipeline/handlers/tool-select.d.ts.map +1 -1
- package/dist/pipeline/handlers/tool-select.js +5 -1
- package/dist/pipeline/handlers/tool-select.js.map +1 -1
- package/dist/pipeline/handlers/translate.d.ts.map +1 -1
- package/dist/pipeline/handlers/translate.js +2 -10
- package/dist/pipeline/handlers/translate.js.map +1 -1
- package/dist/policy/caller-identity.d.ts +29 -0
- package/dist/policy/caller-identity.d.ts.map +1 -0
- package/dist/policy/caller-identity.js +79 -0
- package/dist/policy/caller-identity.js.map +1 -0
- package/dist/policy/mixed-tool-call-handler.d.ts +9 -3
- package/dist/policy/mixed-tool-call-handler.d.ts.map +1 -1
- package/dist/policy/mixed-tool-call-handler.js +29 -7
- package/dist/policy/mixed-tool-call-handler.js.map +1 -1
- package/dist/policy/pending-tool-results-registry.d.ts +46 -4
- package/dist/policy/pending-tool-results-registry.d.ts.map +1 -1
- package/dist/policy/pending-tool-results-registry.js +203 -18
- package/dist/policy/pending-tool-results-registry.js.map +1 -1
- package/dist/policy/tool-availability-registry.d.ts +6 -5
- package/dist/policy/tool-availability-registry.d.ts.map +1 -1
- package/dist/policy/tool-availability-registry.js +13 -2
- package/dist/policy/tool-availability-registry.js.map +1 -1
- package/dist/reranker/llm-reranker.d.ts.map +1 -1
- package/dist/reranker/llm-reranker.js +2 -1
- package/dist/reranker/llm-reranker.js.map +1 -1
- package/dist/subagent/direct-llm-subagent.js +1 -1
- package/dist/subagent/direct-llm-subagent.js.map +1 -1
- package/dist/subagent/smart-agent-subagent.d.ts.map +1 -1
- package/dist/subagent/smart-agent-subagent.js +6 -5
- package/dist/subagent/smart-agent-subagent.js.map +1 -1
- package/dist/testing/index.d.ts +3 -1
- package/dist/testing/index.d.ts.map +1 -1
- package/dist/testing/index.js +2 -0
- package/dist/testing/index.js.map +1 -1
- package/package.json +2 -2
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { addCacheTokenUsage, toModelToolMessages, } from '@mcp-abap-adt/llm-agent';
|
|
2
2
|
import { normalizeLoopLimit } from '../../config/normalize-max-iterations.js';
|
|
3
3
|
import { withSubmitReport } from '../../coordinator/dialogue/submit-report-tool.js';
|
|
4
4
|
import { denyRagAccess, ownerBoundQuery } from '../../rag/owner-bound-query.js';
|
|
5
|
-
import {
|
|
5
|
+
import { ToolResponseRound } from './tool-response-round.js';
|
|
6
|
+
import { buildNamespacedTools, defaultToolNamespace, mergeOfferedTools, scopedUsage, toolNameFromRecord, toToolCallDelta, } from '@mcp-abap-adt/llm-agent';
|
|
6
7
|
import { OrchestratorError } from '../../agent.js';
|
|
8
|
+
import { resolveCallerIdentity } from '../../policy/caller-identity.js';
|
|
7
9
|
import { fireInternalToolsAsync } from '../../policy/mixed-tool-call-handler.js';
|
|
8
10
|
import { LegacyAccumulateContextStrategy } from '../context/tool-loop-context/index.js';
|
|
11
|
+
import { ToolLoopControlHistory } from '../context/tool-loop-control-history.js';
|
|
9
12
|
import { normalizeHeartbeatMs } from './normalize-heartbeat-ms.js';
|
|
10
|
-
import {
|
|
13
|
+
import { classifyToolCalls, executeToolBatchWithHeartbeat, filterAvailableTools, guardToolRound, injectPendingResults, injectToolPriority, pendingSourceMessages, readTaskFinish, runOutputValidationReprompt, } from './tool-loop-core.js';
|
|
11
14
|
import { wrapFinalResponse } from './wrap-final-response.js';
|
|
12
15
|
function summarizeIterationMessages(messages) {
|
|
13
16
|
const totalChars = messages.reduce((sum, msg) => {
|
|
@@ -71,6 +74,7 @@ function describeCause(error) {
|
|
|
71
74
|
}
|
|
72
75
|
export class ToolLoopHandler {
|
|
73
76
|
async execute(ctx, config, parentSpan) {
|
|
77
|
+
const identity = ctx.callerResolution ?? resolveCallerIdentity(ctx.options, ctx.sessionId);
|
|
74
78
|
const maxIterations = normalizeLoopLimit('maxIterations' in config
|
|
75
79
|
? config.maxIterations
|
|
76
80
|
: ctx.config.maxIterations, 'maxIterations');
|
|
@@ -103,29 +107,19 @@ export class ToolLoopHandler {
|
|
|
103
107
|
const timingLog = [];
|
|
104
108
|
const loopStart = Date.now();
|
|
105
109
|
let currentTools = ctx.activeTools;
|
|
110
|
+
const localBlocks = new Set();
|
|
106
111
|
messages = injectToolPriority(messages, externalTools);
|
|
107
112
|
// staticPrefix is immutable across all iterations — re-emitted every round.
|
|
108
|
-
const staticPrefix = messages;
|
|
113
|
+
const staticPrefix = await injectPendingResults(messages, ctx.pendingToolResults, identity, ctx.options, ctx.externalResults, ctx.textOrMessages);
|
|
109
114
|
const strategy = (ctx.toolLoopContextStrategyFactory ??
|
|
110
115
|
(() => new LegacyAccumulateContextStrategy()))({ run: undefined });
|
|
111
|
-
//
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const withPending = await injectPendingResults(messages, ctx.pendingToolResults, ctx.sessionId, ctx.options);
|
|
115
|
-
if (withPending.length > staticPrefix.length) {
|
|
116
|
-
// Injected pending results form an assistant+tool group → record as round.
|
|
117
|
-
const injected = withPending.slice(staticPrefix.length);
|
|
118
|
-
const pendingRound = {
|
|
119
|
-
assistant: injected[0],
|
|
120
|
-
results: injected.slice(1),
|
|
121
|
-
};
|
|
122
|
-
await strategy.record(pendingRound);
|
|
123
|
-
}
|
|
124
|
-
messages = (await strategy.form({
|
|
116
|
+
// Preserve validation exchanges between their original tool rounds.
|
|
117
|
+
const controlHistory = new ToolLoopControlHistory(staticPrefix.length);
|
|
118
|
+
messages = controlHistory.restore(await strategy.form({
|
|
125
119
|
prefix: staticPrefix,
|
|
126
120
|
toolTrust: ctx.config.toolTrust,
|
|
127
121
|
queryText: ctx.inputText,
|
|
128
|
-
}))
|
|
122
|
+
}));
|
|
129
123
|
const finalize = (content, finishReason) => {
|
|
130
124
|
ctx.assistantText = content;
|
|
131
125
|
const summary = ctx.requestLogger.getSummary();
|
|
@@ -355,7 +349,7 @@ export class ToolLoopHandler {
|
|
|
355
349
|
}
|
|
356
350
|
currentTools = await withSubmitReport(currentTools, ctx.toolClientMap, ctx.options, externalTools);
|
|
357
351
|
// Filter tools per iteration
|
|
358
|
-
currentTools = filterAvailableTools(ctx.toolAvailabilityRegistry,
|
|
352
|
+
currentTools = filterAvailableTools(ctx.toolAvailabilityRegistry, identity, currentTools, iteration, ctx.options, localBlocks);
|
|
359
353
|
if (maxToolCalls !== undefined && toolCallCount >= maxToolCalls) {
|
|
360
354
|
currentTools = [];
|
|
361
355
|
ctx.yield({
|
|
@@ -368,6 +362,7 @@ export class ToolLoopHandler {
|
|
|
368
362
|
},
|
|
369
363
|
});
|
|
370
364
|
}
|
|
365
|
+
let iterCache = {};
|
|
371
366
|
let iterPromptTokens = 0;
|
|
372
367
|
let iterCompletionTokens = 0;
|
|
373
368
|
let iterTotalTokens = 0;
|
|
@@ -407,7 +402,6 @@ export class ToolLoopHandler {
|
|
|
407
402
|
const toolCallsMap = new Map();
|
|
408
403
|
// Track which streaming indices belong to external tools so that
|
|
409
404
|
// argument-only continuation deltas (no name field) are forwarded too.
|
|
410
|
-
const externalToolIndices = new Set();
|
|
411
405
|
const available = ctx.mcpTools.length + externalTools.length;
|
|
412
406
|
if (currentTools.length !== lastToolsSelected ||
|
|
413
407
|
available !== lastToolsAvailable) {
|
|
@@ -478,7 +472,6 @@ export class ToolLoopHandler {
|
|
|
478
472
|
ctx.onPartial?.({ kind: 'iteration-end', outcome: 'retry' });
|
|
479
473
|
content = '';
|
|
480
474
|
toolCallsMap.clear();
|
|
481
|
-
externalToolIndices.clear();
|
|
482
475
|
finishReason = undefined;
|
|
483
476
|
iterPromptTokens = 0;
|
|
484
477
|
iterCompletionTokens = 0;
|
|
@@ -501,18 +494,7 @@ export class ToolLoopHandler {
|
|
|
501
494
|
}
|
|
502
495
|
}
|
|
503
496
|
if (chunk.toolCalls) {
|
|
504
|
-
//
|
|
505
|
-
// tool-call deltas are BUFFERED (accumulated into toolCallsMap) and
|
|
506
|
-
// NOT yielded live: an external call is surfaced only AFTER the stream
|
|
507
|
-
// settles — and only if it has no resume result (miss). This avoids
|
|
508
|
-
// leaking a call we may instead resolve from `ctx.externalResults`.
|
|
509
|
-
for (const tc of chunk.toolCalls) {
|
|
510
|
-
const name = getStreamToolCallName(tc);
|
|
511
|
-
if (name && externalToolNames.has(name)) {
|
|
512
|
-
const delta = toToolCallDelta(tc, 0);
|
|
513
|
-
externalToolIndices.add(delta.index);
|
|
514
|
-
}
|
|
515
|
-
}
|
|
497
|
+
// Buffer calls until the complete response can be classified atomically.
|
|
516
498
|
for (const [fallbackIndex, rawToolCall,] of chunk.toolCalls.entries()) {
|
|
517
499
|
const tc = toToolCallDelta(rawToolCall, fallbackIndex);
|
|
518
500
|
if (!toolCallsMap.has(tc.index)) {
|
|
@@ -546,9 +528,11 @@ export class ToolLoopHandler {
|
|
|
546
528
|
if (chunk.finishReason)
|
|
547
529
|
finishReason = chunk.finishReason;
|
|
548
530
|
if (chunk.usage) {
|
|
531
|
+
Object.assign(usage, addCacheTokenUsage(usage, chunk.usage));
|
|
549
532
|
usage.promptTokens += chunk.usage.promptTokens;
|
|
550
533
|
usage.completionTokens += chunk.usage.completionTokens;
|
|
551
534
|
usage.totalTokens += chunk.usage.totalTokens;
|
|
535
|
+
iterCache = addCacheTokenUsage(iterCache, chunk.usage);
|
|
552
536
|
iterPromptTokens += chunk.usage.promptTokens;
|
|
553
537
|
iterCompletionTokens += chunk.usage.completionTokens;
|
|
554
538
|
iterTotalTokens += chunk.usage.totalTokens;
|
|
@@ -566,27 +550,21 @@ export class ToolLoopHandler {
|
|
|
566
550
|
ctx.requestLogger.logLlmCall({
|
|
567
551
|
component: 'tool-loop',
|
|
568
552
|
model: ctx.mainLlm.model ?? 'unknown',
|
|
553
|
+
...iterCache,
|
|
569
554
|
promptTokens: iterPromptTokens,
|
|
570
555
|
completionTokens: iterCompletionTokens,
|
|
571
556
|
totalTokens: iterTotalTokens,
|
|
572
557
|
durationMs: llmCallDuration,
|
|
573
558
|
requestId: ctx.options?.trace?.traceId,
|
|
574
559
|
});
|
|
575
|
-
const
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
args = JSON.parse(tc.arguments);
|
|
579
|
-
}
|
|
580
|
-
catch {
|
|
581
|
-
args = {};
|
|
582
|
-
}
|
|
583
|
-
return { id: tc.id, name: tc.name, arguments: args };
|
|
584
|
-
});
|
|
560
|
+
const hasToolCalls = finishReason === 'tool_calls' && toolCallsMap.size > 0;
|
|
561
|
+
const responseRound = new ToolResponseRound(content, toolCallsMap.values(), (name) => externalToolNames.has(name) && !ctx.toolClientMap.has(name));
|
|
562
|
+
const toolCalls = hasToolCalls ? responseRound.executableCalls() : [];
|
|
585
563
|
ctx.options?.sessionLogger?.logStep(`llm_response_iter_${iteration + 1}`, { content, toolCalls, finishReason }, 'llm');
|
|
586
|
-
const val =
|
|
564
|
+
const val = hasToolCalls
|
|
587
565
|
? { reprompt: false, messages }
|
|
588
566
|
: await runOutputValidationReprompt(ctx.outputValidator, content, messages, currentTools, ctx.options, ctx.config.toolTrust);
|
|
589
|
-
const iterationEnd =
|
|
567
|
+
const iterationEnd = hasToolCalls
|
|
590
568
|
? 'tool_calls'
|
|
591
569
|
: val.reprompt
|
|
592
570
|
? 'retry'
|
|
@@ -603,262 +581,117 @@ export class ToolLoopHandler {
|
|
|
603
581
|
ctx.yield({ ok: true, value: { content: '', iterationEnd } });
|
|
604
582
|
ctx.onPartial?.({ kind: 'iteration-end', outcome: iterationEnd });
|
|
605
583
|
// -- No tool calls: validate and finish --------------------------------
|
|
606
|
-
if (
|
|
584
|
+
if (!hasToolCalls) {
|
|
607
585
|
if (val.reprompt) {
|
|
608
|
-
//
|
|
609
|
-
// into the strategy — they are ephemeral and survive only until the
|
|
610
|
-
// next recorded tool round, at which point controlTail is pruned.
|
|
586
|
+
// Anchor the rejected reply and correction before any newer tool rounds.
|
|
611
587
|
const appended = val.messages.slice(messages.length);
|
|
612
|
-
|
|
613
|
-
messages = (await strategy.form({
|
|
588
|
+
controlHistory.append(appended);
|
|
589
|
+
messages = controlHistory.restore(await strategy.form({
|
|
614
590
|
prefix: staticPrefix,
|
|
615
591
|
toolTrust: ctx.config.toolTrust,
|
|
616
592
|
queryText: ctx.inputText,
|
|
617
|
-
}))
|
|
593
|
+
}));
|
|
618
594
|
continue;
|
|
619
595
|
}
|
|
620
596
|
return finalize(content, finishReason);
|
|
621
597
|
}
|
|
622
598
|
// -- Classify tool calls -----------------------------------------------
|
|
623
599
|
const roundRejections = guardToolRound(toolCalls, ctx.options) ?? new Map();
|
|
624
|
-
const { internalCalls, validExternalCalls, blockedCalls, hallucinations, } = classifyToolCalls(toolCalls, ctx.toolClientMap, externalToolNames, ctx.toolAvailabilityRegistry,
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
const rejectedExternal = validExternalCalls.filter((tc) => roundRejections.has(toolCalls.indexOf(tc)));
|
|
628
|
-
if (rejectedExternal.length > 0) {
|
|
629
|
-
await strategy.record({
|
|
630
|
-
assistant: {
|
|
631
|
-
role: 'assistant',
|
|
632
|
-
content: content || null,
|
|
633
|
-
tool_calls: rejectedExternal.map((tc) => ({
|
|
634
|
-
id: tc.id,
|
|
635
|
-
type: 'function',
|
|
636
|
-
function: {
|
|
637
|
-
name: tc.name,
|
|
638
|
-
arguments: JSON.stringify(tc.arguments),
|
|
639
|
-
},
|
|
640
|
-
})),
|
|
641
|
-
},
|
|
642
|
-
results: rejectedExternal.map((tc) => ({
|
|
643
|
-
role: 'tool',
|
|
644
|
-
tool_call_id: tc.id,
|
|
645
|
-
content: `Error: ${roundRejections.get(toolCalls.indexOf(tc))}`,
|
|
646
|
-
})),
|
|
647
|
-
meta: rejectedExternal.map(() => ({ isError: true })),
|
|
648
|
-
});
|
|
649
|
-
for (const tc of rejectedExternal) {
|
|
650
|
-
validExternalCalls.splice(validExternalCalls.indexOf(tc), 1);
|
|
651
|
-
}
|
|
652
|
-
controlTail.length = 0;
|
|
653
|
-
messages = (await strategy.form({
|
|
654
|
-
prefix: staticPrefix,
|
|
655
|
-
toolTrust: ctx.config.toolTrust,
|
|
656
|
-
queryText: ctx.inputText,
|
|
657
|
-
})).concat(controlTail);
|
|
658
|
-
if (validExternalCalls.length === 0 &&
|
|
659
|
-
internalCalls.length === 0 &&
|
|
660
|
-
blockedCalls.length === 0 &&
|
|
661
|
-
hallucinations.length === 0)
|
|
662
|
-
continue;
|
|
600
|
+
const { internalCalls, validExternalCalls, blockedCalls, hallucinations, } = classifyToolCalls(toolCalls, ctx.toolClientMap, externalToolNames, ctx.toolAvailabilityRegistry, identity, localBlocks);
|
|
601
|
+
for (const tc of blockedCalls) {
|
|
602
|
+
responseRound.resolve(tc, `Error: Tool "${tc.name}" is temporarily unavailable in this session.`, true);
|
|
663
603
|
}
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
await strategy.record({
|
|
667
|
-
assistant: group.assistant,
|
|
668
|
-
results: group.results,
|
|
669
|
-
});
|
|
670
|
-
controlTail.length = 0;
|
|
671
|
-
messages = (await strategy.form({
|
|
672
|
-
prefix: staticPrefix,
|
|
673
|
-
toolTrust: ctx.config.toolTrust,
|
|
674
|
-
queryText: ctx.inputText,
|
|
675
|
-
})).concat(controlTail);
|
|
676
|
-
continue;
|
|
604
|
+
for (const tc of hallucinations) {
|
|
605
|
+
responseRound.resolve(tc, `Error: Tool "${tc.name}" not found.`, true);
|
|
677
606
|
}
|
|
607
|
+
const externalCalls = validExternalCalls.filter((tc) => {
|
|
608
|
+
const rejection = roundRejections.get(toolCalls.indexOf(tc));
|
|
609
|
+
if (rejection === undefined)
|
|
610
|
+
return true;
|
|
611
|
+
responseRound.resolve(tc, `Error: ${rejection}`, true);
|
|
612
|
+
return false;
|
|
613
|
+
});
|
|
678
614
|
// Check tool call budget
|
|
679
615
|
const remaining = maxToolCalls !== undefined
|
|
680
616
|
? maxToolCalls - toolCallCount
|
|
681
617
|
: internalCalls.length;
|
|
682
618
|
const batch = internalCalls.slice(0, Math.max(0, remaining));
|
|
683
619
|
const skipped = internalCalls.slice(batch.length);
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
},
|
|
700
|
-
results: unknownCalls.map((tc) => ({
|
|
701
|
-
role: 'tool',
|
|
702
|
-
tool_call_id: tc.id,
|
|
703
|
-
content: `Error: Tool "${tc.name}" not found.`,
|
|
704
|
-
})),
|
|
705
|
-
meta: unknownCalls.map(() => ({ isError: true })),
|
|
620
|
+
if (externalCalls.length > 0) {
|
|
621
|
+
const pending = fireInternalToolsAsync(content, batch, ctx.pendingToolResults, identity, {
|
|
622
|
+
toolClientMap: ctx.toolClientMap,
|
|
623
|
+
toolCache: ctx.toolCache,
|
|
624
|
+
metrics: ctx.metrics,
|
|
625
|
+
options: ctx.options,
|
|
626
|
+
roundCalls: toolCalls,
|
|
627
|
+
roundRejections,
|
|
628
|
+
skippedCalls: skipped,
|
|
629
|
+
maxToolCalls: maxToolCalls,
|
|
630
|
+
assistantMessage: responseRound.assistant,
|
|
631
|
+
resolvedResults: responseRound.resolved(),
|
|
632
|
+
precedingMessages: messages,
|
|
633
|
+
sourceMessages: pendingSourceMessages(ctx.textOrMessages, staticPrefix, messages),
|
|
634
|
+
externalCalls,
|
|
706
635
|
});
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
queryText: ctx.inputText,
|
|
712
|
-
})).concat(controlTail);
|
|
713
|
-
}
|
|
714
|
-
// -- Handle external (client-provided) tool calls (spec D1) ------------
|
|
715
|
-
// Each external call is rewritten to a deterministic content-addressed id
|
|
716
|
-
// (`externalToolCallId`). Two paths:
|
|
717
|
-
// HIT — `ctx.externalResults` already has the result (stateless resume
|
|
718
|
-
// from the client's prior round-trip): inject a MATCHED
|
|
719
|
-
// assistant(tool_calls=[extId]) -> tool(tool_call_id=extId) pair
|
|
720
|
-
// and CONTINUE the loop (no re-surface).
|
|
721
|
-
// MISS — no result yet: surface the call (with extId) to the consumer
|
|
722
|
-
// and END the worker turn (`finish_reason: tool_calls`); never
|
|
723
|
-
// execute it server-side.
|
|
724
|
-
if (validExternalCalls.length > 0) {
|
|
725
|
-
const externalWithId = validExternalCalls.map((tc) => ({
|
|
726
|
-
...tc,
|
|
727
|
-
extId: externalToolCallId(tc.name, tc.arguments),
|
|
728
|
-
}));
|
|
729
|
-
const hits = externalWithId.filter((tc) => ctx.externalResults?.has(tc.extId));
|
|
730
|
-
const misses = externalWithId.filter((tc) => !ctx.externalResults?.has(tc.extId));
|
|
731
|
-
// HIT path: inject matched pairs (assistant tool_call + tool result),
|
|
732
|
-
// each correlated by the SAME extId, and resolve them in-conversation.
|
|
733
|
-
if (hits.length > 0) {
|
|
734
|
-
markExternalTraceUnavailable(ctx.options?.onToolTrace);
|
|
735
|
-
const hitAssistant = {
|
|
736
|
-
role: 'assistant',
|
|
737
|
-
content: null,
|
|
738
|
-
tool_calls: hits.map((tc) => ({
|
|
739
|
-
id: tc.extId,
|
|
740
|
-
type: 'function',
|
|
741
|
-
function: {
|
|
742
|
-
name: tc.name,
|
|
743
|
-
arguments: JSON.stringify(tc.arguments),
|
|
744
|
-
},
|
|
745
|
-
})),
|
|
746
|
-
};
|
|
747
|
-
const hitResults = hits.map((tc) => ({
|
|
748
|
-
role: 'tool',
|
|
749
|
-
content: ctx.externalResults?.get(tc.extId) ?? '',
|
|
750
|
-
tool_call_id: tc.extId,
|
|
751
|
-
}));
|
|
752
|
-
await strategy.record({
|
|
753
|
-
assistant: hitAssistant,
|
|
754
|
-
results: hitResults,
|
|
755
|
-
});
|
|
756
|
-
controlTail.length = 0;
|
|
757
|
-
messages = (await strategy.form({
|
|
758
|
-
prefix: staticPrefix,
|
|
759
|
-
toolTrust: ctx.config.toolTrust,
|
|
760
|
-
queryText: ctx.inputText,
|
|
761
|
-
})).concat(controlTail);
|
|
762
|
-
ctx.options?.sessionLogger?.logStep('external_results_resumed', {
|
|
763
|
-
extIds: hits.map((tc) => tc.extId),
|
|
764
|
-
toolNames: hits.map((tc) => tc.name),
|
|
765
|
-
});
|
|
766
|
-
}
|
|
767
|
-
// MISS path: surface the unresolved external call(s) and end the turn.
|
|
768
|
-
if (misses.length > 0) {
|
|
769
|
-
if (internalCalls.length > 0) {
|
|
770
|
-
fireInternalToolsAsync(content, batch, ctx.pendingToolResults, ctx.sessionId, {
|
|
771
|
-
toolClientMap: ctx.toolClientMap,
|
|
772
|
-
toolCache: ctx.toolCache,
|
|
773
|
-
metrics: ctx.metrics,
|
|
774
|
-
options: ctx.options,
|
|
775
|
-
roundCalls: toolCalls,
|
|
776
|
-
roundRejections,
|
|
777
|
-
skippedCalls: skipped,
|
|
778
|
-
maxToolCalls: maxToolCalls,
|
|
779
|
-
});
|
|
780
|
-
toolCallCount += batch.length;
|
|
781
|
-
if (batch.length > 0) {
|
|
782
|
-
try {
|
|
783
|
-
ctx.options?.onToolCallCount?.(toolCallCount, maxToolCalls);
|
|
784
|
-
}
|
|
785
|
-
catch {
|
|
786
|
-
/* Observers cannot affect execution. */
|
|
787
|
-
}
|
|
788
|
-
}
|
|
789
|
-
if (maxToolCalls !== undefined && toolCallCount >= maxToolCalls) {
|
|
790
|
-
ctx.yield({
|
|
791
|
-
ok: true,
|
|
792
|
-
value: {
|
|
793
|
-
content: '',
|
|
794
|
-
budgetExhausted: true,
|
|
795
|
-
toolCallCount,
|
|
796
|
-
maxToolCalls: maxToolCalls,
|
|
797
|
-
},
|
|
798
|
-
});
|
|
799
|
-
}
|
|
800
|
-
ctx.options?.sessionLogger?.logStep('mixed_tool_calls', {
|
|
801
|
-
internal: internalCalls.map((tc) => tc.name),
|
|
802
|
-
external: misses.map((tc) => tc.name),
|
|
803
|
-
});
|
|
636
|
+
toolCallCount += batch.length;
|
|
637
|
+
if (batch.length > 0) {
|
|
638
|
+
try {
|
|
639
|
+
ctx.options?.onToolCallCount?.(toolCallCount, maxToolCalls);
|
|
804
640
|
}
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
value: {
|
|
811
|
-
content: '',
|
|
812
|
-
toolCalls: misses.map((tc, index) => ({
|
|
813
|
-
index,
|
|
814
|
-
id: tc.extId,
|
|
815
|
-
name: tc.name,
|
|
816
|
-
arguments: JSON.stringify(tc.arguments),
|
|
817
|
-
})),
|
|
818
|
-
},
|
|
819
|
-
});
|
|
820
|
-
timingLog.push({ phase: 'total', duration: Date.now() - loopStart });
|
|
821
|
-
ctx.timing.push(...timingLog);
|
|
641
|
+
catch {
|
|
642
|
+
/* Observers cannot affect execution. */
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
if (maxToolCalls !== undefined && toolCallCount >= maxToolCalls) {
|
|
822
646
|
ctx.yield({
|
|
823
647
|
ok: true,
|
|
824
648
|
value: {
|
|
825
649
|
content: '',
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
models: ctx.requestLogger.getSummary().byModel,
|
|
830
|
-
components: ctx.requestLogger.getSummary().byComponent,
|
|
831
|
-
},
|
|
832
|
-
timing: timingLog,
|
|
650
|
+
budgetExhausted: true,
|
|
651
|
+
toolCallCount,
|
|
652
|
+
maxToolCalls: maxToolCalls,
|
|
833
653
|
},
|
|
834
654
|
});
|
|
835
|
-
return true;
|
|
836
|
-
}
|
|
837
|
-
// Only hits (no misses): if there are no internal calls to execute,
|
|
838
|
-
// continue the loop directly so the worker consumes the injected
|
|
839
|
-
// results. With internal calls present, fall through to execute them.
|
|
840
|
-
if (internalCalls.length === 0) {
|
|
841
|
-
continue;
|
|
842
655
|
}
|
|
656
|
+
ctx.options?.sessionLogger?.logStep('mixed_tool_calls', {
|
|
657
|
+
internal: internalCalls.map((tc) => tc.name),
|
|
658
|
+
external: externalCalls.map((tc) => tc.name),
|
|
659
|
+
});
|
|
660
|
+
// Return this suspension's unique IDs; keep model IDs internal.
|
|
661
|
+
emitProgressFallback();
|
|
662
|
+
ctx.yield({
|
|
663
|
+
ok: true,
|
|
664
|
+
value: {
|
|
665
|
+
content: '',
|
|
666
|
+
toolCalls: externalCalls.map((tc, index) => ({
|
|
667
|
+
index,
|
|
668
|
+
id: pending.externalCalls[index].returnedId,
|
|
669
|
+
name: tc.name,
|
|
670
|
+
arguments: JSON.stringify(tc.arguments),
|
|
671
|
+
})),
|
|
672
|
+
},
|
|
673
|
+
});
|
|
674
|
+
timingLog.push({ phase: 'total', duration: Date.now() - loopStart });
|
|
675
|
+
ctx.timing.push(...timingLog);
|
|
676
|
+
ctx.yield({
|
|
677
|
+
ok: true,
|
|
678
|
+
value: {
|
|
679
|
+
content: '',
|
|
680
|
+
finishReason: 'tool_calls',
|
|
681
|
+
usage: {
|
|
682
|
+
...usage,
|
|
683
|
+
models: ctx.requestLogger.getSummary().byModel,
|
|
684
|
+
components: ctx.requestLogger.getSummary().byComponent,
|
|
685
|
+
},
|
|
686
|
+
timing: timingLog,
|
|
687
|
+
},
|
|
688
|
+
});
|
|
689
|
+
return true;
|
|
843
690
|
}
|
|
844
691
|
// -- Execute internal MCP tool calls -----------------------------------
|
|
845
692
|
// Capture the assistant message now; record the full round (assistant +
|
|
846
693
|
// tool results) atomically after the batch completes so the strategy
|
|
847
694
|
// always sees a complete ToolRound.
|
|
848
|
-
const batchAssistantMsg = {
|
|
849
|
-
role: 'assistant',
|
|
850
|
-
content: content || null,
|
|
851
|
-
tool_calls: toolCalls
|
|
852
|
-
.filter((tc) => internalCalls.includes(tc) || hallucinations.includes(tc))
|
|
853
|
-
.map((tc) => ({
|
|
854
|
-
id: tc.id,
|
|
855
|
-
type: 'function',
|
|
856
|
-
function: {
|
|
857
|
-
name: tc.name,
|
|
858
|
-
arguments: JSON.stringify(tc.arguments),
|
|
859
|
-
},
|
|
860
|
-
})),
|
|
861
|
-
};
|
|
862
695
|
// Yield progress messages
|
|
863
696
|
for (const tc of batch) {
|
|
864
697
|
ctx.yield({
|
|
@@ -871,6 +704,8 @@ export class ToolLoopHandler {
|
|
|
871
704
|
}
|
|
872
705
|
// Execute tool calls concurrently with heartbeat (shared core).
|
|
873
706
|
const batchGen = executeToolBatchWithHeartbeat({
|
|
707
|
+
callerResolution: identity,
|
|
708
|
+
localBlocks,
|
|
874
709
|
batch,
|
|
875
710
|
roundCalls: toolCalls,
|
|
876
711
|
roundRejections,
|
|
@@ -887,6 +722,16 @@ export class ToolLoopHandler {
|
|
|
887
722
|
timingLog,
|
|
888
723
|
heartbeatMs,
|
|
889
724
|
options: ctx.options,
|
|
725
|
+
onBatchComplete: async (outcome) => {
|
|
726
|
+
outcome.toolMessages.forEach((message, index) => {
|
|
727
|
+
responseRound.add(message, outcome.resultMeta[index]);
|
|
728
|
+
});
|
|
729
|
+
for (const tc of skipped) {
|
|
730
|
+
responseRound.resolve(tc, `Error: not executed: tool-call budget of ${maxToolCalls} for this request is exhausted; return what you have and report what is missing`, true);
|
|
731
|
+
}
|
|
732
|
+
const batchRound = responseRound.finish();
|
|
733
|
+
await strategy.record(controlHistory.record(batchRound));
|
|
734
|
+
},
|
|
890
735
|
mcpFailureClassifier: ctx.mcpFailureClassifier,
|
|
891
736
|
maxToolCalls,
|
|
892
737
|
onToolExecuted: (r) => {
|
|
@@ -952,33 +797,11 @@ export class ToolLoopHandler {
|
|
|
952
797
|
},
|
|
953
798
|
});
|
|
954
799
|
}
|
|
955
|
-
|
|
956
|
-
assistant: batchAssistantMsg,
|
|
957
|
-
results: [
|
|
958
|
-
...outcome.toolMessages,
|
|
959
|
-
...hallucinations.map((tc) => ({
|
|
960
|
-
role: 'tool',
|
|
961
|
-
tool_call_id: tc.id,
|
|
962
|
-
content: `Error: Tool "${tc.name}" not found.`,
|
|
963
|
-
})),
|
|
964
|
-
...skipped.map((tc) => ({
|
|
965
|
-
role: 'tool',
|
|
966
|
-
tool_call_id: tc.id,
|
|
967
|
-
content: `Error: not executed: tool-call budget of ${maxToolCalls} for this request is exhausted; return what you have and report what is missing`,
|
|
968
|
-
})),
|
|
969
|
-
],
|
|
970
|
-
meta: [
|
|
971
|
-
...outcome.resultMeta,
|
|
972
|
-
...[...hallucinations, ...skipped].map(() => ({ isError: true })),
|
|
973
|
-
],
|
|
974
|
-
};
|
|
975
|
-
await strategy.record(batchRound);
|
|
976
|
-
controlTail.length = 0;
|
|
977
|
-
messages = (await strategy.form({
|
|
800
|
+
messages = controlHistory.restore(await strategy.form({
|
|
978
801
|
prefix: staticPrefix,
|
|
979
802
|
toolTrust: ctx.config.toolTrust,
|
|
980
803
|
queryText: ctx.inputText,
|
|
981
|
-
}))
|
|
804
|
+
}));
|
|
982
805
|
}
|
|
983
806
|
}
|
|
984
807
|
}
|