@renxqoo/renx-code 0.0.9 → 0.0.12
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 +31 -143
- package/bin/renx.cjs +79 -42
- package/bin/renx.exe +0 -0
- package/package.json +10 -28
- package/src/App.tsx +0 -297
- package/src/agent/runtime/event-format.ts +0 -258
- package/src/agent/runtime/model-types.ts +0 -13
- package/src/agent/runtime/runtime.context-usage.test.ts +0 -192
- package/src/agent/runtime/runtime.error-handling.test.ts +0 -235
- package/src/agent/runtime/runtime.simple.test.ts +0 -16
- package/src/agent/runtime/runtime.test.ts +0 -296
- package/src/agent/runtime/runtime.ts +0 -875
- package/src/agent/runtime/runtime.usage-forwarding.test.ts +0 -228
- package/src/agent/runtime/source-modules.test.ts +0 -38
- package/src/agent/runtime/source-modules.ts +0 -370
- package/src/agent/runtime/tool-call-buffer.test.ts +0 -65
- package/src/agent/runtime/tool-call-buffer.ts +0 -60
- package/src/agent/runtime/tool-confirmation.test.ts +0 -56
- package/src/agent/runtime/tool-confirmation.ts +0 -15
- package/src/agent/runtime/types.ts +0 -99
- package/src/commands/slash-commands.test.ts +0 -216
- package/src/commands/slash-commands.ts +0 -64
- package/src/components/chat/assistant-reply.test.tsx +0 -47
- package/src/components/chat/assistant-reply.tsx +0 -136
- package/src/components/chat/assistant-segment.test.ts +0 -99
- package/src/components/chat/assistant-segment.tsx +0 -125
- package/src/components/chat/assistant-tool-group.tsx +0 -900
- package/src/components/chat/code-block.test.tsx +0 -206
- package/src/components/chat/code-block.tsx +0 -313
- package/src/components/chat/prompt-card.tsx +0 -81
- package/src/components/chat/segment-groups.test.ts +0 -52
- package/src/components/chat/segment-groups.ts +0 -106
- package/src/components/chat/turn-item.tsx +0 -39
- package/src/components/conversation-panel.tsx +0 -43
- package/src/components/file-mention-menu.tsx +0 -77
- package/src/components/file-picker-dialog.tsx +0 -206
- package/src/components/footer-hints.tsx +0 -75
- package/src/components/model-picker-dialog.tsx +0 -248
- package/src/components/prompt.tsx +0 -233
- package/src/components/slash-command-menu.tsx +0 -65
- package/src/components/tool-confirm-dialog-content.test.ts +0 -103
- package/src/components/tool-confirm-dialog-content.ts +0 -186
- package/src/components/tool-confirm-dialog.tsx +0 -187
- package/src/components/tool-display-config.ts +0 -119
- package/src/context-usage-regressions.test.ts +0 -26
- package/src/files/attachment-capabilities.test.ts +0 -30
- package/src/files/attachment-capabilities.ts +0 -50
- package/src/files/attachment-content.ts +0 -153
- package/src/files/file-mention-query.test.ts +0 -34
- package/src/files/file-mention-query.ts +0 -32
- package/src/files/prompt-display.ts +0 -13
- package/src/files/types.ts +0 -5
- package/src/files/workspace-files.ts +0 -61
- package/src/hooks/agent-event-handlers.test.ts +0 -207
- package/src/hooks/agent-event-handlers.ts +0 -196
- package/src/hooks/chat-local-replies.fixed.test.ts +0 -119
- package/src/hooks/chat-local-replies.test.ts +0 -153
- package/src/hooks/chat-local-replies.ts +0 -63
- package/src/hooks/turn-updater.test.ts +0 -70
- package/src/hooks/turn-updater.ts +0 -166
- package/src/hooks/use-agent-chat.context.test.ts +0 -10
- package/src/hooks/use-agent-chat.status.test.ts +0 -14
- package/src/hooks/use-agent-chat.test.ts +0 -80
- package/src/hooks/use-agent-chat.ts +0 -621
- package/src/hooks/use-file-mention-menu.ts +0 -196
- package/src/hooks/use-file-picker.ts +0 -185
- package/src/hooks/use-model-picker.ts +0 -196
- package/src/hooks/use-slash-command-menu.ts +0 -154
- package/src/index.tsx +0 -55
- package/src/runtime/clipboard.test.ts +0 -43
- package/src/runtime/clipboard.ts +0 -89
- package/src/runtime/exit.test.ts +0 -177
- package/src/runtime/exit.ts +0 -98
- package/src/runtime/runtime-support.test.ts +0 -31
- package/src/runtime/terminal-theme.test.ts +0 -55
- package/src/runtime/terminal-theme.ts +0 -196
- package/src/types/chat.ts +0 -32
- package/src/types/message-content.ts +0 -48
- package/src/ui/open-code-theme.ts +0 -176
- package/src/ui/opencode-markdown.ts +0 -211
- package/src/ui/theme.simple.test.ts +0 -52
- package/src/ui/theme.test.ts +0 -151
- package/src/ui/theme.ts +0 -152
- package/src/utils/time.test.ts +0 -144
- package/src/utils/time.ts +0 -7
- package/tsconfig.json +0 -30
- package/vendor/agent-root/src/agent/ENTERPRISE_ACCEPTANCE_CHECKLIST.md +0 -95
- package/vendor/agent-root/src/agent/ENTERPRISE_REALTIME.html +0 -1345
- package/vendor/agent-root/src/agent/ENTERPRISE_REALTIME.md +0 -1353
- package/vendor/agent-root/src/agent/ERROR_CONTRACT.md +0 -60
- package/vendor/agent-root/src/agent/TEST_COVERAGE_ANALYSIS.md +0 -278
- package/vendor/agent-root/src/agent/__test__/error-contract.test.ts +0 -72
- package/vendor/agent-root/src/agent/__test__/types.test.ts +0 -137
- package/vendor/agent-root/src/agent/agent/__test__/abort-runtime.test.ts +0 -83
- package/vendor/agent-root/src/agent/agent/__test__/callback-safety.test.ts +0 -34
- package/vendor/agent-root/src/agent/agent/__test__/compaction.test.ts +0 -323
- package/vendor/agent-root/src/agent/agent/__test__/concurrency.test.ts +0 -290
- package/vendor/agent-root/src/agent/agent/__test__/error-normalizer.test.ts +0 -377
- package/vendor/agent-root/src/agent/agent/__test__/error.test.ts +0 -212
- package/vendor/agent-root/src/agent/agent/__test__/fault-injection.test.ts +0 -295
- package/vendor/agent-root/src/agent/agent/__test__/index.test.ts +0 -3607
- package/vendor/agent-root/src/agent/agent/__test__/logger.test.ts +0 -35
- package/vendor/agent-root/src/agent/agent/__test__/message-utils.test.ts +0 -517
- package/vendor/agent-root/src/agent/agent/__test__/telemetry.test.ts +0 -97
- package/vendor/agent-root/src/agent/agent/__test__/timeout-budget.test.ts +0 -479
- package/vendor/agent-root/src/agent/agent/__test__/tool-call-merge.test.ts +0 -80
- package/vendor/agent-root/src/agent/agent/__test__/tool-execution-ledger.test.ts +0 -76
- package/vendor/agent-root/src/agent/agent/__test__/write-buffer.test.ts +0 -173
- package/vendor/agent-root/src/agent/agent/__test__/write-file-session.test.ts +0 -109
- package/vendor/agent-root/src/agent/agent/abort-runtime.ts +0 -71
- package/vendor/agent-root/src/agent/agent/callback-safety.ts +0 -33
- package/vendor/agent-root/src/agent/agent/compaction.ts +0 -291
- package/vendor/agent-root/src/agent/agent/concurrency.ts +0 -103
- package/vendor/agent-root/src/agent/agent/error-normalizer.ts +0 -190
- package/vendor/agent-root/src/agent/agent/error.ts +0 -198
- package/vendor/agent-root/src/agent/agent/index.ts +0 -1772
- package/vendor/agent-root/src/agent/agent/logger.ts +0 -65
- package/vendor/agent-root/src/agent/agent/message-utils.ts +0 -101
- package/vendor/agent-root/src/agent/agent/stream-events.ts +0 -61
- package/vendor/agent-root/src/agent/agent/telemetry.ts +0 -123
- package/vendor/agent-root/src/agent/agent/timeout-budget.ts +0 -227
- package/vendor/agent-root/src/agent/agent/tool-call-merge.ts +0 -111
- package/vendor/agent-root/src/agent/agent/tool-execution-ledger.ts +0 -164
- package/vendor/agent-root/src/agent/agent/write-buffer.ts +0 -188
- package/vendor/agent-root/src/agent/agent/write-file-session.ts +0 -238
- package/vendor/agent-root/src/agent/app/__test__/agent-app-service.test.ts +0 -1053
- package/vendor/agent-root/src/agent/app/__test__/minimal-agent-application.test.ts +0 -158
- package/vendor/agent-root/src/agent/app/__test__/sqlite-agent-app-store.test.ts +0 -437
- package/vendor/agent-root/src/agent/app/agent-app-service.ts +0 -748
- package/vendor/agent-root/src/agent/app/contracts.ts +0 -109
- package/vendor/agent-root/src/agent/app/index.ts +0 -5
- package/vendor/agent-root/src/agent/app/minimal-agent-application.ts +0 -151
- package/vendor/agent-root/src/agent/app/ports.ts +0 -72
- package/vendor/agent-root/src/agent/app/sqlite-agent-app-store.ts +0 -1182
- package/vendor/agent-root/src/agent/app/sqlite-client.ts +0 -177
- package/vendor/agent-root/src/agent/docs/cli-app-layer/00-README.md +0 -36
- package/vendor/agent-root/src/agent/docs/cli-app-layer/01-scope-and-goals.md +0 -33
- package/vendor/agent-root/src/agent/docs/cli-app-layer/02-architecture-overview.md +0 -40
- package/vendor/agent-root/src/agent/docs/cli-app-layer/03-domain-model-and-contracts.md +0 -91
- package/vendor/agent-root/src/agent/docs/cli-app-layer/04-ports-and-interfaces.md +0 -116
- package/vendor/agent-root/src/agent/docs/cli-app-layer/05-run-orchestration-and-state-machine.md +0 -52
- package/vendor/agent-root/src/agent/docs/cli-app-layer/06-cli-commands-and-ux.md +0 -53
- package/vendor/agent-root/src/agent/docs/cli-app-layer/07-storage-design-local.md +0 -52
- package/vendor/agent-root/src/agent/docs/cli-app-layer/08-error-and-observability.md +0 -40
- package/vendor/agent-root/src/agent/docs/cli-app-layer/09-security-and-policy-boundary.md +0 -19
- package/vendor/agent-root/src/agent/docs/cli-app-layer/10-test-plan-and-acceptance.md +0 -28
- package/vendor/agent-root/src/agent/docs/cli-app-layer/11-implementation-phases.md +0 -26
- package/vendor/agent-root/src/agent/docs/cli-app-layer/12-open-questions-and-risks.md +0 -30
- package/vendor/agent-root/src/agent/docs/cli-app-layer/13-sqlite-schema-fields-and-rationale.md +0 -567
- package/vendor/agent-root/src/agent/docs/cli-app-layer/14-project-flow-mermaid.md +0 -583
- package/vendor/agent-root/src/agent/docs/cli-app-layer/15-openclaw-style-project-blueprint.md +0 -972
- package/vendor/agent-root/src/agent/error-contract.ts +0 -154
- package/vendor/agent-root/src/agent/prompts/system.ts +0 -246
- package/vendor/agent-root/src/agent/prompts/system1.ts +0 -208
- package/vendor/agent-root/src/agent/storage/__test__/file-history-store.test.ts +0 -98
- package/vendor/agent-root/src/agent/storage/file-history-store.ts +0 -313
- package/vendor/agent-root/src/agent/storage/file-storage-config.ts +0 -94
- package/vendor/agent-root/src/agent/storage/file-system.ts +0 -31
- package/vendor/agent-root/src/agent/storage/file-write-service.ts +0 -21
- package/vendor/agent-root/src/agent/tool/__test__/base-tool.test.ts +0 -413
- package/vendor/agent-root/src/agent/tool/__test__/bash-policy.test.ts +0 -356
- package/vendor/agent-root/src/agent/tool/__test__/bash.mocked-coverage.test.ts +0 -375
- package/vendor/agent-root/src/agent/tool/__test__/bash.test.ts +0 -372
- package/vendor/agent-root/src/agent/tool/__test__/error.test.ts +0 -108
- package/vendor/agent-root/src/agent/tool/__test__/file-edit-tool.test.ts +0 -258
- package/vendor/agent-root/src/agent/tool/__test__/file-history-tools.test.ts +0 -121
- package/vendor/agent-root/src/agent/tool/__test__/file-read-tool.test.ts +0 -210
- package/vendor/agent-root/src/agent/tool/__test__/glob.test.ts +0 -139
- package/vendor/agent-root/src/agent/tool/__test__/grep.mocked-coverage.test.ts +0 -456
- package/vendor/agent-root/src/agent/tool/__test__/grep.test.ts +0 -192
- package/vendor/agent-root/src/agent/tool/__test__/lsp.test.ts +0 -300
- package/vendor/agent-root/src/agent/tool/__test__/outside-workspace-confirmation.test.ts +0 -214
- package/vendor/agent-root/src/agent/tool/__test__/path-security.test.ts +0 -336
- package/vendor/agent-root/src/agent/tool/__test__/skill-loader.test.ts +0 -494
- package/vendor/agent-root/src/agent/tool/__test__/skill-parser.test.ts +0 -543
- package/vendor/agent-root/src/agent/tool/__test__/skill-tool.test.ts +0 -172
- package/vendor/agent-root/src/agent/tool/__test__/task-concurrency-and-version.test.ts +0 -116
- package/vendor/agent-root/src/agent/tool/__test__/task-create-get-list-update.test.ts +0 -267
- package/vendor/agent-root/src/agent/tool/__test__/task-create.test.ts +0 -519
- package/vendor/agent-root/src/agent/tool/__test__/task-errors.test.ts +0 -225
- package/vendor/agent-root/src/agent/tool/__test__/task-output-blocking.test.ts +0 -223
- package/vendor/agent-root/src/agent/tool/__test__/task-output.test.ts +0 -184
- package/vendor/agent-root/src/agent/tool/__test__/task-parent-abort.test.ts +0 -287
- package/vendor/agent-root/src/agent/tool/__test__/task-real-runner-adapter.test.ts +0 -190
- package/vendor/agent-root/src/agent/tool/__test__/task-run-lifecycle.test.ts +0 -352
- package/vendor/agent-root/src/agent/tool/__test__/task-store-runner-branches.test.ts +0 -395
- package/vendor/agent-root/src/agent/tool/__test__/task-store.test.ts +0 -391
- package/vendor/agent-root/src/agent/tool/__test__/task-subagent-config-integration.test.ts +0 -176
- package/vendor/agent-root/src/agent/tool/__test__/task-subagent-config.test.ts +0 -68
- package/vendor/agent-root/src/agent/tool/__test__/task-tools-core-edges.test.ts +0 -630
- package/vendor/agent-root/src/agent/tool/__test__/task-tools-runtime-edges.test.ts +0 -732
- package/vendor/agent-root/src/agent/tool/__test__/task-types.test.ts +0 -494
- package/vendor/agent-root/src/agent/tool/__test__/task-utils-branches.test.ts +0 -175
- package/vendor/agent-root/src/agent/tool/__test__/tool-manager.test.ts +0 -505
- package/vendor/agent-root/src/agent/tool/__test__/types.test.ts +0 -55
- package/vendor/agent-root/src/agent/tool/__test__/web-fetch.test.ts +0 -244
- package/vendor/agent-root/src/agent/tool/__test__/web-search.test.ts +0 -290
- package/vendor/agent-root/src/agent/tool/__test__/write-file.test.ts +0 -368
- package/vendor/agent-root/src/agent/tool/base-tool.ts +0 -345
- package/vendor/agent-root/src/agent/tool/bash-policy.ts +0 -636
- package/vendor/agent-root/src/agent/tool/bash.ts +0 -688
- package/vendor/agent-root/src/agent/tool/error.ts +0 -131
- package/vendor/agent-root/src/agent/tool/file-edit-tool.ts +0 -264
- package/vendor/agent-root/src/agent/tool/file-history-list.ts +0 -103
- package/vendor/agent-root/src/agent/tool/file-history-restore.ts +0 -149
- package/vendor/agent-root/src/agent/tool/file-read-tool.ts +0 -211
- package/vendor/agent-root/src/agent/tool/glob.ts +0 -171
- package/vendor/agent-root/src/agent/tool/grep.ts +0 -496
- package/vendor/agent-root/src/agent/tool/lsp.ts +0 -481
- package/vendor/agent-root/src/agent/tool/path-security.ts +0 -117
- package/vendor/agent-root/src/agent/tool/search/common.ts +0 -153
- package/vendor/agent-root/src/agent/tool/skill/index.ts +0 -13
- package/vendor/agent-root/src/agent/tool/skill/loader.ts +0 -229
- package/vendor/agent-root/src/agent/tool/skill/parser.ts +0 -124
- package/vendor/agent-root/src/agent/tool/skill/types.ts +0 -27
- package/vendor/agent-root/src/agent/tool/skill-tool.ts +0 -143
- package/vendor/agent-root/src/agent/tool/task-create.ts +0 -186
- package/vendor/agent-root/src/agent/tool/task-errors.ts +0 -42
- package/vendor/agent-root/src/agent/tool/task-get.ts +0 -116
- package/vendor/agent-root/src/agent/tool/task-graph.ts +0 -78
- package/vendor/agent-root/src/agent/tool/task-list.ts +0 -141
- package/vendor/agent-root/src/agent/tool/task-mock-runner-adapter.ts +0 -232
- package/vendor/agent-root/src/agent/tool/task-output.ts +0 -223
- package/vendor/agent-root/src/agent/tool/task-parent-abort.ts +0 -115
- package/vendor/agent-root/src/agent/tool/task-real-runner-adapter.ts +0 -336
- package/vendor/agent-root/src/agent/tool/task-runner-adapter.ts +0 -55
- package/vendor/agent-root/src/agent/tool/task-stop.ts +0 -187
- package/vendor/agent-root/src/agent/tool/task-store.ts +0 -217
- package/vendor/agent-root/src/agent/tool/task-subagent-config.ts +0 -149
- package/vendor/agent-root/src/agent/tool/task-types.ts +0 -264
- package/vendor/agent-root/src/agent/tool/task-update.ts +0 -315
- package/vendor/agent-root/src/agent/tool/task.ts +0 -209
- package/vendor/agent-root/src/agent/tool/tool-manager.ts +0 -361
- package/vendor/agent-root/src/agent/tool/tool-prompts.ts +0 -242
- package/vendor/agent-root/src/agent/tool/types.ts +0 -116
- package/vendor/agent-root/src/agent/tool/web-fetch.ts +0 -227
- package/vendor/agent-root/src/agent/tool/web-search.ts +0 -208
- package/vendor/agent-root/src/agent/tool/write-file.ts +0 -497
- package/vendor/agent-root/src/agent/types.ts +0 -232
- package/vendor/agent-root/src/agent/utils/__tests__/index.test.ts +0 -18
- package/vendor/agent-root/src/agent/utils/__tests__/message-utils.test.ts +0 -610
- package/vendor/agent-root/src/agent/utils/__tests__/message.test.ts +0 -223
- package/vendor/agent-root/src/agent/utils/__tests__/token.test.ts +0 -42
- package/vendor/agent-root/src/agent/utils/index.ts +0 -16
- package/vendor/agent-root/src/agent/utils/message.ts +0 -171
- package/vendor/agent-root/src/agent/utils/token.ts +0 -28
- package/vendor/agent-root/src/config/__tests__/load-config-to-env.test.ts +0 -238
- package/vendor/agent-root/src/config/__tests__/loader.test.ts +0 -361
- package/vendor/agent-root/src/config/__tests__/runtime.test.ts +0 -88
- package/vendor/agent-root/src/config/index.ts +0 -55
- package/vendor/agent-root/src/config/loader.ts +0 -494
- package/vendor/agent-root/src/config/paths.ts +0 -30
- package/vendor/agent-root/src/config/runtime.ts +0 -163
- package/vendor/agent-root/src/config/types.ts +0 -96
- package/vendor/agent-root/src/logger/index.ts +0 -57
- package/vendor/agent-root/src/logger/logger.ts +0 -819
- package/vendor/agent-root/src/logger/types.ts +0 -150
- package/vendor/agent-root/src/providers/__tests__/errors.test.ts +0 -441
- package/vendor/agent-root/src/providers/__tests__/index.test.ts +0 -16
- package/vendor/agent-root/src/providers/__tests__/openai-compatible.options.test.ts +0 -318
- package/vendor/agent-root/src/providers/__tests__/openai-compatible.test.ts +0 -600
- package/vendor/agent-root/src/providers/__tests__/registry.test.ts +0 -523
- package/vendor/agent-root/src/providers/__tests__/responses-adapter.test.ts +0 -298
- package/vendor/agent-root/src/providers/adapters/__tests__/anthropic.test.ts +0 -354
- package/vendor/agent-root/src/providers/adapters/__tests__/kimi.test.ts +0 -58
- package/vendor/agent-root/src/providers/adapters/__tests__/standard.test.ts +0 -261
- package/vendor/agent-root/src/providers/adapters/anthropic.ts +0 -572
- package/vendor/agent-root/src/providers/adapters/base.ts +0 -131
- package/vendor/agent-root/src/providers/adapters/kimi.ts +0 -48
- package/vendor/agent-root/src/providers/adapters/responses.ts +0 -732
- package/vendor/agent-root/src/providers/adapters/standard.ts +0 -120
- package/vendor/agent-root/src/providers/http/__tests__/client.timeout.test.ts +0 -313
- package/vendor/agent-root/src/providers/http/client.ts +0 -289
- package/vendor/agent-root/src/providers/http/stream-parser.ts +0 -109
- package/vendor/agent-root/src/providers/index.ts +0 -76
- package/vendor/agent-root/src/providers/kimi-headers.ts +0 -177
- package/vendor/agent-root/src/providers/openai-compatible.ts +0 -387
- package/vendor/agent-root/src/providers/registry/model-config.ts +0 -477
- package/vendor/agent-root/src/providers/registry/provider-factory.ts +0 -127
- package/vendor/agent-root/src/providers/registry.ts +0 -135
- package/vendor/agent-root/src/providers/types/api.ts +0 -284
- package/vendor/agent-root/src/providers/types/config.ts +0 -58
- package/vendor/agent-root/src/providers/types/errors.ts +0 -323
- package/vendor/agent-root/src/providers/types/index.ts +0 -72
- package/vendor/agent-root/src/providers/types/provider.ts +0 -45
- package/vendor/agent-root/src/providers/types/registry.ts +0 -68
|
@@ -1,1772 +0,0 @@
|
|
|
1
|
-
import { createHash } from 'node:crypto';
|
|
2
|
-
import {
|
|
3
|
-
Message,
|
|
4
|
-
AgentInput,
|
|
5
|
-
AgentCallbacks,
|
|
6
|
-
AgentContextUsage,
|
|
7
|
-
CompactionInfo,
|
|
8
|
-
StreamEvent,
|
|
9
|
-
ErrorDecision,
|
|
10
|
-
ToolDecision,
|
|
11
|
-
} from '../types';
|
|
12
|
-
import { ToolManager } from '../tool/tool-manager';
|
|
13
|
-
import { LLMProvider, LLMRequestMessage, Tool, ToolCall } from '../../providers';
|
|
14
|
-
import { EventEmitter } from 'events';
|
|
15
|
-
import {
|
|
16
|
-
AgentAbortedError,
|
|
17
|
-
AgentError,
|
|
18
|
-
AgentUpstreamRetryableError,
|
|
19
|
-
MaxRetriesError,
|
|
20
|
-
TimeoutBudgetExceededError,
|
|
21
|
-
UnknownError,
|
|
22
|
-
} from './error';
|
|
23
|
-
import { mergeAgentLoggers, type AgentLogger } from './logger';
|
|
24
|
-
import { compact, estimateMessagesTokens } from './compaction';
|
|
25
|
-
import { LLMTool, ToolConcurrencyPolicy } from '../tool/types';
|
|
26
|
-
import type { BackoffConfig } from '../../providers';
|
|
27
|
-
import {
|
|
28
|
-
convertMessageToLLMMessage as toLLMMessage,
|
|
29
|
-
mergeLLMConfig as mergeLLMRequestConfig,
|
|
30
|
-
shouldSendMessageToLLM,
|
|
31
|
-
} from './message-utils';
|
|
32
|
-
import { processToolCallPairs } from '../utils/message';
|
|
33
|
-
|
|
34
|
-
import {
|
|
35
|
-
createCheckpoint,
|
|
36
|
-
createDoneEvent,
|
|
37
|
-
createErrorEvent,
|
|
38
|
-
createProgressEvent,
|
|
39
|
-
} from './stream-events';
|
|
40
|
-
import {
|
|
41
|
-
buildExecutionWaves as buildToolExecutionWaves,
|
|
42
|
-
runWithConcurrencyAndLock as runTasksWithConcurrencyAndLock,
|
|
43
|
-
} from './concurrency';
|
|
44
|
-
import {
|
|
45
|
-
calculateRetryDelay as calculateRetryDelayWithBackoff,
|
|
46
|
-
isAbortError as isAbortErrorByMessage,
|
|
47
|
-
normalizeError as normalizeAgentError,
|
|
48
|
-
} from './error-normalizer';
|
|
49
|
-
import {
|
|
50
|
-
createExecutionAbortScope as createExecutionBudgetScope,
|
|
51
|
-
createStageAbortScope as createStageBudgetScope,
|
|
52
|
-
createTimeoutBudgetState as createBudgetState,
|
|
53
|
-
type AbortScope,
|
|
54
|
-
type TimeoutBudgetState,
|
|
55
|
-
type TimeoutStage,
|
|
56
|
-
} from './timeout-budget';
|
|
57
|
-
import {
|
|
58
|
-
buildWriteFileSessionKey as createWriteFileSessionKey,
|
|
59
|
-
bufferWriteFileToolCallChunk as bufferWriteFileChunk,
|
|
60
|
-
cleanupWriteFileBufferIfNeeded as cleanupWriteFileBufferSession,
|
|
61
|
-
enrichWriteFileToolError as buildWriteFileToolErrorPayload,
|
|
62
|
-
isWriteFileProtocolOutput as isWriteFileProtocolResponse,
|
|
63
|
-
isWriteFileToolCall as isWriteFileTool,
|
|
64
|
-
shouldEnrichWriteFileFailure as needEnrichWriteFileFailure,
|
|
65
|
-
type WriteBufferRuntime,
|
|
66
|
-
} from './write-file-session';
|
|
67
|
-
import {
|
|
68
|
-
createToolResultMessage as buildToolResultMessage,
|
|
69
|
-
NoopToolExecutionLedger,
|
|
70
|
-
executeToolCallWithLedger as executeWithToolLedger,
|
|
71
|
-
type ToolExecutionLedger,
|
|
72
|
-
type ToolExecutionLedgerRecord,
|
|
73
|
-
} from './tool-execution-ledger';
|
|
74
|
-
import {
|
|
75
|
-
emitMetric as pushMetric,
|
|
76
|
-
emitTrace as pushTrace,
|
|
77
|
-
endSpan as finishSpan,
|
|
78
|
-
extractErrorCode as parseErrorCode,
|
|
79
|
-
logError as writeErrorLog,
|
|
80
|
-
logInfo as writeInfoLog,
|
|
81
|
-
logWarn as writeWarnLog,
|
|
82
|
-
startSpan as beginSpan,
|
|
83
|
-
type SpanRuntime,
|
|
84
|
-
} from './telemetry';
|
|
85
|
-
import {
|
|
86
|
-
safeCallback as invokeSafeCallback,
|
|
87
|
-
safeErrorCallback as invokeSafeErrorCallback,
|
|
88
|
-
} from './callback-safety';
|
|
89
|
-
import { mergeToolCalls as mergeToolCallsWithBuffer } from './tool-call-merge';
|
|
90
|
-
import type { ToolResult } from '../tool/base-tool';
|
|
91
|
-
import {
|
|
92
|
-
normalizeTimeoutBudgetError as normalizeAbortTimeoutBudgetError,
|
|
93
|
-
sleepWithAbort,
|
|
94
|
-
throwIfAborted as assertNotAborted,
|
|
95
|
-
timeoutBudgetErrorFromSignal as timeoutErrorFromAbortSignal,
|
|
96
|
-
} from './abort-runtime';
|
|
97
|
-
|
|
98
|
-
function generateId(prefix: string): string {
|
|
99
|
-
return `${prefix}${Date.now()}_${Math.random().toString(36).substring(2, 11)}`;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function hasNonEmptyText(value: unknown): value is string {
|
|
103
|
-
return typeof value === 'string' && value.length > 0;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
type ContinuationMetadata = {
|
|
107
|
-
responseId?: string;
|
|
108
|
-
llmRequestConfigHash?: string;
|
|
109
|
-
llmRequestInputHash?: string;
|
|
110
|
-
llmRequestInputMessageCount?: number;
|
|
111
|
-
llmResponseMessageHash?: string;
|
|
112
|
-
continuationMode?: 'full' | 'incremental';
|
|
113
|
-
previousResponseIdUsed?: string;
|
|
114
|
-
continuationBaselineMessageCount?: number;
|
|
115
|
-
continuationDeltaMessageCount?: number;
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
type LLMRequestPlan = {
|
|
119
|
-
llmMessages: LLMRequestMessage[];
|
|
120
|
-
requestMessages: LLMRequestMessage[];
|
|
121
|
-
requestConfig: AgentInput['config'];
|
|
122
|
-
requestConfigHash: string;
|
|
123
|
-
requestInputHash: string;
|
|
124
|
-
requestInputMessageCount: number;
|
|
125
|
-
continuationMode: 'full' | 'incremental';
|
|
126
|
-
previousResponseIdUsed?: string;
|
|
127
|
-
continuationBaselineMessageCount?: number;
|
|
128
|
-
continuationDeltaMessageCount: number;
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
function isPlainRecord(value: unknown): value is Record<string, unknown> {
|
|
132
|
-
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
function normalizeValueForHash(value: unknown): unknown {
|
|
136
|
-
if (value === undefined) {
|
|
137
|
-
return null;
|
|
138
|
-
}
|
|
139
|
-
if (
|
|
140
|
-
value === null ||
|
|
141
|
-
typeof value === 'string' ||
|
|
142
|
-
typeof value === 'number' ||
|
|
143
|
-
typeof value === 'boolean'
|
|
144
|
-
) {
|
|
145
|
-
return value;
|
|
146
|
-
}
|
|
147
|
-
if (Array.isArray(value)) {
|
|
148
|
-
return value.map((item) => normalizeValueForHash(item));
|
|
149
|
-
}
|
|
150
|
-
if (typeof value === 'object') {
|
|
151
|
-
return Object.keys(value as Record<string, unknown>)
|
|
152
|
-
.sort()
|
|
153
|
-
.reduce<Record<string, unknown>>((acc, key) => {
|
|
154
|
-
const normalized = normalizeValueForHash((value as Record<string, unknown>)[key]);
|
|
155
|
-
if (normalized !== undefined) {
|
|
156
|
-
acc[key] = normalized;
|
|
157
|
-
}
|
|
158
|
-
return acc;
|
|
159
|
-
}, {});
|
|
160
|
-
}
|
|
161
|
-
return String(value);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function hashValueForContinuation(value: unknown): string {
|
|
165
|
-
return createHash('sha256')
|
|
166
|
-
.update(JSON.stringify(normalizeValueForHash(value)))
|
|
167
|
-
.digest('hex');
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
export interface AgentConfig {
|
|
171
|
-
maxRetryCount?: number;
|
|
172
|
-
enableCompaction?: boolean;
|
|
173
|
-
compactionTriggerRatio?: number;
|
|
174
|
-
compactionKeepMessagesNum?: number;
|
|
175
|
-
enableServerSideContinuation?: boolean;
|
|
176
|
-
backoffConfig?: BackoffConfig;
|
|
177
|
-
maxConcurrentToolCalls?: number;
|
|
178
|
-
toolConcurrencyPolicyResolver?: (toolCall: ToolCall) => ToolConcurrencyPolicy;
|
|
179
|
-
logger?: AgentLogger;
|
|
180
|
-
/**
|
|
181
|
-
* Optional external idempotency ledger.
|
|
182
|
-
* Defaults to Noop to keep the agent stateless across process restarts and scale-out replicas.
|
|
183
|
-
*/
|
|
184
|
-
toolExecutionLedger?: ToolExecutionLedger;
|
|
185
|
-
timeoutBudgetMs?: number;
|
|
186
|
-
llmTimeoutRatio?: number;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
export type { AgentLogger } from './logger';
|
|
190
|
-
|
|
191
|
-
interface InternalAgentConfig {
|
|
192
|
-
maxRetryCount: number;
|
|
193
|
-
enableCompaction: boolean;
|
|
194
|
-
compactionTriggerRatio: number;
|
|
195
|
-
compactionKeepMessagesNum: number;
|
|
196
|
-
enableServerSideContinuation: boolean;
|
|
197
|
-
backoffConfig: BackoffConfig;
|
|
198
|
-
maxConcurrentToolCalls: number;
|
|
199
|
-
toolConcurrencyPolicyResolver?: (toolCall: ToolCall) => ToolConcurrencyPolicy;
|
|
200
|
-
logger: AgentLogger;
|
|
201
|
-
timeoutBudgetMs?: number;
|
|
202
|
-
llmTimeoutRatio: number;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
const DEFAULT_MAX_RETRY_COUNT = 20;
|
|
206
|
-
const DEFAULT_COMPACTION_TRIGGER_RATIO = 0.8;
|
|
207
|
-
const DEFAULT_COMPACTION_KEEP_MESSAGES = 20;
|
|
208
|
-
const DEFAULT_MAX_CONCURRENT_TOOL_CALLS = 1;
|
|
209
|
-
const DEFAULT_LLM_TIMEOUT_RATIO = 0.7;
|
|
210
|
-
const ABORTED_MESSAGE = 'Operation aborted';
|
|
211
|
-
|
|
212
|
-
export type { ToolExecutionLedger, ToolExecutionLedgerRecord } from './tool-execution-ledger';
|
|
213
|
-
|
|
214
|
-
export class StatelessAgent extends EventEmitter {
|
|
215
|
-
private llmProvider: LLMProvider;
|
|
216
|
-
private toolExecutor: ToolManager;
|
|
217
|
-
private config: InternalAgentConfig;
|
|
218
|
-
private logger: AgentLogger;
|
|
219
|
-
private toolExecutionLedger: ToolExecutionLedger;
|
|
220
|
-
constructor(llmProvider: LLMProvider, toolExecutor: ToolManager, config: AgentConfig) {
|
|
221
|
-
super();
|
|
222
|
-
this.llmProvider = llmProvider;
|
|
223
|
-
this.toolExecutor = toolExecutor;
|
|
224
|
-
this.logger = config.logger ?? {};
|
|
225
|
-
this.toolExecutionLedger = config.toolExecutionLedger ?? new NoopToolExecutionLedger();
|
|
226
|
-
const llmTimeoutRatio = Number.isFinite(config.llmTimeoutRatio)
|
|
227
|
-
? Number(config.llmTimeoutRatio)
|
|
228
|
-
: DEFAULT_LLM_TIMEOUT_RATIO;
|
|
229
|
-
const clampedLlmTimeoutRatio = Math.min(0.95, Math.max(0.05, llmTimeoutRatio));
|
|
230
|
-
this.config = {
|
|
231
|
-
maxRetryCount: config.maxRetryCount ?? DEFAULT_MAX_RETRY_COUNT,
|
|
232
|
-
enableCompaction: config.enableCompaction ?? false,
|
|
233
|
-
compactionTriggerRatio: config.compactionTriggerRatio ?? DEFAULT_COMPACTION_TRIGGER_RATIO,
|
|
234
|
-
compactionKeepMessagesNum:
|
|
235
|
-
config.compactionKeepMessagesNum ?? DEFAULT_COMPACTION_KEEP_MESSAGES,
|
|
236
|
-
enableServerSideContinuation: config.enableServerSideContinuation ?? false,
|
|
237
|
-
backoffConfig: config.backoffConfig ?? {},
|
|
238
|
-
maxConcurrentToolCalls: Math.max(
|
|
239
|
-
1,
|
|
240
|
-
Math.floor(config.maxConcurrentToolCalls ?? DEFAULT_MAX_CONCURRENT_TOOL_CALLS)
|
|
241
|
-
),
|
|
242
|
-
toolConcurrencyPolicyResolver: config.toolConcurrencyPolicyResolver,
|
|
243
|
-
logger: this.logger,
|
|
244
|
-
timeoutBudgetMs:
|
|
245
|
-
config.timeoutBudgetMs &&
|
|
246
|
-
Number.isFinite(config.timeoutBudgetMs) &&
|
|
247
|
-
config.timeoutBudgetMs > 0
|
|
248
|
-
? Math.floor(config.timeoutBudgetMs)
|
|
249
|
-
: undefined,
|
|
250
|
-
llmTimeoutRatio: clampedLlmTimeoutRatio,
|
|
251
|
-
};
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
getContextLimitTokens(contextLimitTokens?: number): number {
|
|
255
|
-
if (
|
|
256
|
-
typeof contextLimitTokens === 'number' &&
|
|
257
|
-
Number.isFinite(contextLimitTokens) &&
|
|
258
|
-
contextLimitTokens > 0
|
|
259
|
-
) {
|
|
260
|
-
return Math.max(1, Math.floor(contextLimitTokens));
|
|
261
|
-
}
|
|
262
|
-
const maxTokens = this.llmProvider.getLLMMaxTokens();
|
|
263
|
-
const maxOutputTokens = this.llmProvider.getMaxOutputTokens();
|
|
264
|
-
return Math.max(1, maxTokens - maxOutputTokens);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
estimateContextUsage(
|
|
268
|
-
messages: Message[],
|
|
269
|
-
tools?: Tool[],
|
|
270
|
-
contextLimitTokens?: number
|
|
271
|
-
): Pick<AgentContextUsage, 'contextTokens' | 'contextLimitTokens' | 'contextUsagePercent'> {
|
|
272
|
-
const llmTools = tools as unknown as LLMTool[] | undefined;
|
|
273
|
-
const contextTokens = estimateMessagesTokens(messages, llmTools);
|
|
274
|
-
const resolvedContextLimitTokens = this.getContextLimitTokens(contextLimitTokens);
|
|
275
|
-
return {
|
|
276
|
-
contextTokens,
|
|
277
|
-
contextLimitTokens: resolvedContextLimitTokens,
|
|
278
|
-
contextUsagePercent: (contextTokens / resolvedContextLimitTokens) * 100,
|
|
279
|
-
};
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
attachLogger(logger: AgentLogger): void {
|
|
283
|
-
this.logger = mergeAgentLoggers(this.logger, logger);
|
|
284
|
-
this.config.logger = this.logger;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
private convertMessageToLLMMessage(message: Message) {
|
|
288
|
-
return toLLMMessage(message);
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
private normalizeContinuationConfig(config: AgentInput['config']): Record<string, unknown> {
|
|
292
|
-
if (!config) {
|
|
293
|
-
return {};
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
const { abortSignal, previous_response_id, ...rest } = config as AgentInput['config'] & {
|
|
297
|
-
abortSignal?: AbortSignal;
|
|
298
|
-
previous_response_id?: string;
|
|
299
|
-
};
|
|
300
|
-
void abortSignal;
|
|
301
|
-
void previous_response_id;
|
|
302
|
-
|
|
303
|
-
return normalizeValueForHash(rest) as Record<string, unknown>;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
private readContinuationMetadata(message: Message): ContinuationMetadata | undefined {
|
|
307
|
-
if (!isPlainRecord(message.metadata)) {
|
|
308
|
-
return undefined;
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
const metadata = message.metadata as Record<string, unknown>;
|
|
312
|
-
const responseId =
|
|
313
|
-
typeof metadata.responseId === 'string' && metadata.responseId.trim().length > 0
|
|
314
|
-
? metadata.responseId
|
|
315
|
-
: undefined;
|
|
316
|
-
const llmRequestConfigHash =
|
|
317
|
-
typeof metadata.llmRequestConfigHash === 'string' ? metadata.llmRequestConfigHash : undefined;
|
|
318
|
-
const llmRequestInputHash =
|
|
319
|
-
typeof metadata.llmRequestInputHash === 'string' ? metadata.llmRequestInputHash : undefined;
|
|
320
|
-
const llmRequestInputMessageCount =
|
|
321
|
-
typeof metadata.llmRequestInputMessageCount === 'number'
|
|
322
|
-
? metadata.llmRequestInputMessageCount
|
|
323
|
-
: undefined;
|
|
324
|
-
const llmResponseMessageHash =
|
|
325
|
-
typeof metadata.llmResponseMessageHash === 'string'
|
|
326
|
-
? metadata.llmResponseMessageHash
|
|
327
|
-
: undefined;
|
|
328
|
-
|
|
329
|
-
if (
|
|
330
|
-
!responseId ||
|
|
331
|
-
!llmRequestConfigHash ||
|
|
332
|
-
!llmRequestInputHash ||
|
|
333
|
-
typeof llmRequestInputMessageCount !== 'number' ||
|
|
334
|
-
!Number.isInteger(llmRequestInputMessageCount) ||
|
|
335
|
-
llmRequestInputMessageCount < 0 ||
|
|
336
|
-
!llmResponseMessageHash
|
|
337
|
-
) {
|
|
338
|
-
return undefined;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
const safeInputMessageCount = llmRequestInputMessageCount;
|
|
342
|
-
|
|
343
|
-
return {
|
|
344
|
-
responseId,
|
|
345
|
-
llmRequestConfigHash,
|
|
346
|
-
llmRequestInputHash,
|
|
347
|
-
llmRequestInputMessageCount: safeInputMessageCount,
|
|
348
|
-
llmResponseMessageHash,
|
|
349
|
-
};
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
private buildLLMRequestPlan(messages: Message[], config: AgentInput['config']): LLMRequestPlan {
|
|
353
|
-
const llmSourceMessages = messages.filter((msg) => shouldSendMessageToLLM(msg));
|
|
354
|
-
const llmMessages = llmSourceMessages.map((msg) => this.convertMessageToLLMMessage(msg));
|
|
355
|
-
const requestConfigHash = hashValueForContinuation(this.normalizeContinuationConfig(config));
|
|
356
|
-
const requestInputHash = hashValueForContinuation(llmMessages);
|
|
357
|
-
const requestInputMessageCount = llmMessages.length;
|
|
358
|
-
|
|
359
|
-
const explicitPreviousResponseId =
|
|
360
|
-
typeof config?.previous_response_id === 'string' &&
|
|
361
|
-
config.previous_response_id.trim().length > 0
|
|
362
|
-
? config.previous_response_id
|
|
363
|
-
: undefined;
|
|
364
|
-
|
|
365
|
-
if (explicitPreviousResponseId) {
|
|
366
|
-
return {
|
|
367
|
-
llmMessages,
|
|
368
|
-
requestMessages: llmMessages,
|
|
369
|
-
requestConfig: config,
|
|
370
|
-
requestConfigHash,
|
|
371
|
-
requestInputHash,
|
|
372
|
-
requestInputMessageCount,
|
|
373
|
-
continuationMode: 'full',
|
|
374
|
-
continuationDeltaMessageCount: llmMessages.length,
|
|
375
|
-
};
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
// Keep server-side continuation opt-in. In the current gateway environment
|
|
379
|
-
// full replay + prompt_cache_key is more stable than automatic previous_response_id chaining.
|
|
380
|
-
if (!this.config.enableServerSideContinuation) {
|
|
381
|
-
return {
|
|
382
|
-
llmMessages,
|
|
383
|
-
requestMessages: llmMessages,
|
|
384
|
-
requestConfig: config,
|
|
385
|
-
requestConfigHash,
|
|
386
|
-
requestInputHash,
|
|
387
|
-
requestInputMessageCount,
|
|
388
|
-
continuationMode: 'full',
|
|
389
|
-
continuationDeltaMessageCount: llmMessages.length,
|
|
390
|
-
};
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
for (let index = llmSourceMessages.length - 1; index >= 0; index -= 1) {
|
|
394
|
-
const candidate = llmSourceMessages[index];
|
|
395
|
-
if (candidate.role !== 'assistant') {
|
|
396
|
-
continue;
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
const metadata = this.readContinuationMetadata(candidate);
|
|
400
|
-
if (!metadata) {
|
|
401
|
-
continue;
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
if (metadata.llmRequestConfigHash !== requestConfigHash) {
|
|
405
|
-
break;
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
const prefixMessages = llmMessages.slice(0, index);
|
|
409
|
-
const currentAssistantMessage = llmMessages[index];
|
|
410
|
-
if (!currentAssistantMessage) {
|
|
411
|
-
break;
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
if (prefixMessages.length !== metadata.llmRequestInputMessageCount) {
|
|
415
|
-
break;
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
if (hashValueForContinuation(prefixMessages) !== metadata.llmRequestInputHash) {
|
|
419
|
-
break;
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
if (hashValueForContinuation(currentAssistantMessage) !== metadata.llmResponseMessageHash) {
|
|
423
|
-
break;
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
// If the delta contains a tool result, the paired assistant tool_call must
|
|
427
|
-
// be included as well, otherwise the Responses gateway rejects the request shape.
|
|
428
|
-
const continuationWindow = processToolCallPairs(
|
|
429
|
-
llmSourceMessages.slice(0, index + 1),
|
|
430
|
-
llmSourceMessages.slice(index + 1)
|
|
431
|
-
);
|
|
432
|
-
const deltaSourceMessages = continuationWindow.active;
|
|
433
|
-
if (deltaSourceMessages.length === 0) {
|
|
434
|
-
break;
|
|
435
|
-
}
|
|
436
|
-
const deltaMessages = deltaSourceMessages.map((msg) => this.convertMessageToLLMMessage(msg));
|
|
437
|
-
|
|
438
|
-
return {
|
|
439
|
-
llmMessages,
|
|
440
|
-
requestMessages: deltaMessages,
|
|
441
|
-
requestConfig: {
|
|
442
|
-
...(config || {}),
|
|
443
|
-
previous_response_id: metadata.responseId,
|
|
444
|
-
},
|
|
445
|
-
requestConfigHash,
|
|
446
|
-
requestInputHash,
|
|
447
|
-
requestInputMessageCount,
|
|
448
|
-
continuationMode: 'incremental',
|
|
449
|
-
previousResponseIdUsed: metadata.responseId,
|
|
450
|
-
continuationBaselineMessageCount: continuationWindow.pending.length,
|
|
451
|
-
continuationDeltaMessageCount: deltaMessages.length,
|
|
452
|
-
};
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
return {
|
|
456
|
-
llmMessages,
|
|
457
|
-
requestMessages: llmMessages,
|
|
458
|
-
requestConfig: config,
|
|
459
|
-
requestConfigHash,
|
|
460
|
-
requestInputHash,
|
|
461
|
-
requestInputMessageCount,
|
|
462
|
-
continuationMode: 'full',
|
|
463
|
-
continuationDeltaMessageCount: llmMessages.length,
|
|
464
|
-
};
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
private needsCompaction(
|
|
468
|
-
messages: Message[],
|
|
469
|
-
tools?: Tool[],
|
|
470
|
-
contextLimitTokens?: number
|
|
471
|
-
): boolean {
|
|
472
|
-
if (!this.config.enableCompaction) {
|
|
473
|
-
return false;
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
const usableLimit = this.getContextLimitTokens(contextLimitTokens);
|
|
477
|
-
const threshold = usableLimit * this.config.compactionTriggerRatio;
|
|
478
|
-
|
|
479
|
-
const llmTools = tools as unknown as LLMTool[] | undefined;
|
|
480
|
-
const currentTokens = estimateMessagesTokens(messages, llmTools);
|
|
481
|
-
|
|
482
|
-
return currentTokens >= threshold;
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
private async compactMessagesIfNeeded(
|
|
486
|
-
messages: Message[],
|
|
487
|
-
tools?: Tool[],
|
|
488
|
-
contextLimitTokens?: number
|
|
489
|
-
): Promise<string[]> {
|
|
490
|
-
if (!this.needsCompaction(messages, tools, contextLimitTokens)) {
|
|
491
|
-
return [];
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
try {
|
|
495
|
-
const result = await compact(messages, {
|
|
496
|
-
provider: this.llmProvider,
|
|
497
|
-
keepMessagesNum: this.config.compactionKeepMessagesNum,
|
|
498
|
-
});
|
|
499
|
-
messages.splice(0, messages.length, ...result.messages);
|
|
500
|
-
|
|
501
|
-
return result.removedMessageIds ?? [];
|
|
502
|
-
} catch (error) {
|
|
503
|
-
this.logError('[Agent] Compaction failed:', error);
|
|
504
|
-
return [];
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
async *runStream(
|
|
509
|
-
input: AgentInput,
|
|
510
|
-
callbacks?: AgentCallbacks
|
|
511
|
-
): AsyncGenerator<StreamEvent, void, unknown> {
|
|
512
|
-
const { messages: inputMessages, maxSteps = 100, abortSignal: inputAbortSignal } = input;
|
|
513
|
-
const effectiveTools = this.resolveLLMTools(input.tools);
|
|
514
|
-
const messages = [...inputMessages];
|
|
515
|
-
if (typeof input.systemPrompt === 'string' && input.systemPrompt.trim().length > 0) {
|
|
516
|
-
const hasSystemMessage = messages.some((message) => message.role === 'system');
|
|
517
|
-
if (!hasSystemMessage) {
|
|
518
|
-
messages.unshift({
|
|
519
|
-
messageId: generateId('msg_sys_'),
|
|
520
|
-
type: 'system',
|
|
521
|
-
role: 'system',
|
|
522
|
-
content: input.systemPrompt,
|
|
523
|
-
timestamp: Date.now(),
|
|
524
|
-
});
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
const writeBufferSessions = new Map<string, WriteBufferRuntime>();
|
|
528
|
-
const timeoutBudget = this.createTimeoutBudgetState(input);
|
|
529
|
-
const executionScope = this.createExecutionAbortScope(inputAbortSignal, timeoutBudget);
|
|
530
|
-
const abortSignal = executionScope.signal;
|
|
531
|
-
const traceId = input.executionId || generateId('trace_');
|
|
532
|
-
const runSpan = await this.startSpan(callbacks, traceId, 'agent.run', undefined, {
|
|
533
|
-
executionId: input.executionId,
|
|
534
|
-
conversationId: input.conversationId,
|
|
535
|
-
maxSteps,
|
|
536
|
-
timeoutBudgetMs: timeoutBudget?.totalMs,
|
|
537
|
-
});
|
|
538
|
-
this.logInfo('[Agent] run.start', {
|
|
539
|
-
executionId: input.executionId,
|
|
540
|
-
traceId,
|
|
541
|
-
spanId: runSpan.spanId,
|
|
542
|
-
messageCount: messages.length,
|
|
543
|
-
});
|
|
544
|
-
|
|
545
|
-
let stepIndex = 0;
|
|
546
|
-
let retryCount = 0;
|
|
547
|
-
let runOutcome: 'done' | 'error' | 'aborted' | 'timeout' | 'max_retries' | 'max_steps' = 'done';
|
|
548
|
-
let runErrorCode: string | undefined;
|
|
549
|
-
let terminalDoneEmitted = false;
|
|
550
|
-
|
|
551
|
-
try {
|
|
552
|
-
while (stepIndex < maxSteps) {
|
|
553
|
-
if (abortSignal?.aborted) {
|
|
554
|
-
const timeoutError = this.timeoutBudgetErrorFromSignal(abortSignal);
|
|
555
|
-
if (timeoutError) {
|
|
556
|
-
runOutcome = 'timeout';
|
|
557
|
-
runErrorCode = timeoutError.errorCode;
|
|
558
|
-
yield* this.yieldErrorEvent(timeoutError);
|
|
559
|
-
} else {
|
|
560
|
-
runOutcome = 'aborted';
|
|
561
|
-
runErrorCode = 'AGENT_ABORTED';
|
|
562
|
-
yield* this.yieldErrorEvent(new AgentAbortedError(ABORTED_MESSAGE));
|
|
563
|
-
}
|
|
564
|
-
break;
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
if (retryCount >= this.config.maxRetryCount) {
|
|
568
|
-
runOutcome = 'max_retries';
|
|
569
|
-
runErrorCode = 'AGENT_MAX_RETRIES_REACHED';
|
|
570
|
-
yield* this.yieldMaxRetriesError();
|
|
571
|
-
break;
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
stepIndex++;
|
|
575
|
-
|
|
576
|
-
try {
|
|
577
|
-
this.throwIfAborted(abortSignal);
|
|
578
|
-
const messageCountBeforeCompaction = messages.length;
|
|
579
|
-
const removedMessageIds = await this.compactMessagesIfNeeded(
|
|
580
|
-
messages,
|
|
581
|
-
effectiveTools,
|
|
582
|
-
input.contextLimitTokens
|
|
583
|
-
);
|
|
584
|
-
if (removedMessageIds.length > 0) {
|
|
585
|
-
const compactionInfo: CompactionInfo = {
|
|
586
|
-
executionId: input.executionId,
|
|
587
|
-
stepIndex,
|
|
588
|
-
removedMessageIds,
|
|
589
|
-
messageCountBefore: messageCountBeforeCompaction,
|
|
590
|
-
messageCountAfter: messages.length,
|
|
591
|
-
};
|
|
592
|
-
await this.safeCallback(callbacks?.onCompaction, compactionInfo);
|
|
593
|
-
yield {
|
|
594
|
-
type: 'compaction',
|
|
595
|
-
data: compactionInfo,
|
|
596
|
-
};
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
this.throwIfAborted(abortSignal);
|
|
600
|
-
|
|
601
|
-
const contextUsage = this.estimateContextUsage(
|
|
602
|
-
messages,
|
|
603
|
-
effectiveTools,
|
|
604
|
-
input.contextLimitTokens
|
|
605
|
-
);
|
|
606
|
-
await this.safeCallback(callbacks?.onContextUsage, {
|
|
607
|
-
stepIndex,
|
|
608
|
-
messageCount: messages.length,
|
|
609
|
-
...contextUsage,
|
|
610
|
-
});
|
|
611
|
-
|
|
612
|
-
yield* this.emitProgress(input.executionId, stepIndex, 'llm', messages.length);
|
|
613
|
-
const llmSpan = await this.startSpan(
|
|
614
|
-
callbacks,
|
|
615
|
-
traceId,
|
|
616
|
-
'agent.llm.step',
|
|
617
|
-
runSpan.spanId,
|
|
618
|
-
{
|
|
619
|
-
executionId: input.executionId,
|
|
620
|
-
stepIndex,
|
|
621
|
-
messageCount: messages.length,
|
|
622
|
-
}
|
|
623
|
-
);
|
|
624
|
-
const llmScope = this.createStageAbortScope(abortSignal, timeoutBudget, 'llm');
|
|
625
|
-
let llmResult:
|
|
626
|
-
| {
|
|
627
|
-
assistantMessage: Message;
|
|
628
|
-
toolCalls: ToolCall[];
|
|
629
|
-
}
|
|
630
|
-
| undefined;
|
|
631
|
-
let llmErrorCode: string | undefined;
|
|
632
|
-
let llmSucceeded = false;
|
|
633
|
-
try {
|
|
634
|
-
const llmGen = this.callLLMAndProcessStream(
|
|
635
|
-
messages,
|
|
636
|
-
this.mergeLLMConfig(
|
|
637
|
-
input.config,
|
|
638
|
-
effectiveTools,
|
|
639
|
-
llmScope.signal,
|
|
640
|
-
input.conversationId
|
|
641
|
-
),
|
|
642
|
-
llmScope.signal,
|
|
643
|
-
input.executionId,
|
|
644
|
-
stepIndex,
|
|
645
|
-
writeBufferSessions
|
|
646
|
-
);
|
|
647
|
-
for (;;) {
|
|
648
|
-
const next = await llmGen.next();
|
|
649
|
-
if (next.done) {
|
|
650
|
-
llmResult = next.value;
|
|
651
|
-
break;
|
|
652
|
-
}
|
|
653
|
-
yield next.value as StreamEvent;
|
|
654
|
-
}
|
|
655
|
-
this.throwIfAborted(llmScope.signal);
|
|
656
|
-
llmSucceeded = true;
|
|
657
|
-
} catch (error) {
|
|
658
|
-
llmErrorCode = this.extractErrorCode(error) || 'AGENT_LLM_STAGE_FAILED';
|
|
659
|
-
throw error;
|
|
660
|
-
} finally {
|
|
661
|
-
llmScope.release();
|
|
662
|
-
const llmLatencyMs = Date.now() - llmSpan.startedAt;
|
|
663
|
-
await this.emitMetric(callbacks, {
|
|
664
|
-
name: 'agent.llm.duration_ms',
|
|
665
|
-
value: llmLatencyMs,
|
|
666
|
-
unit: 'ms',
|
|
667
|
-
timestamp: Date.now(),
|
|
668
|
-
tags: {
|
|
669
|
-
executionId: input.executionId,
|
|
670
|
-
stepIndex,
|
|
671
|
-
success: llmSucceeded ? 'true' : 'false',
|
|
672
|
-
},
|
|
673
|
-
});
|
|
674
|
-
await this.endSpan(callbacks, llmSpan, {
|
|
675
|
-
executionId: input.executionId,
|
|
676
|
-
stepIndex,
|
|
677
|
-
latencyMs: llmLatencyMs,
|
|
678
|
-
errorCode: llmErrorCode,
|
|
679
|
-
});
|
|
680
|
-
this.logInfo('[Agent] llm.step', {
|
|
681
|
-
executionId: input.executionId,
|
|
682
|
-
traceId,
|
|
683
|
-
spanId: llmSpan.spanId,
|
|
684
|
-
stepIndex,
|
|
685
|
-
latencyMs: llmLatencyMs,
|
|
686
|
-
errorCode: llmErrorCode,
|
|
687
|
-
messageCount: messages.length,
|
|
688
|
-
});
|
|
689
|
-
}
|
|
690
|
-
|
|
691
|
-
if (!llmResult) {
|
|
692
|
-
throw new UnknownError('LLM stream completed without result');
|
|
693
|
-
}
|
|
694
|
-
const assistantMessage = llmResult.assistantMessage;
|
|
695
|
-
const toolCalls = llmResult.toolCalls;
|
|
696
|
-
|
|
697
|
-
messages.push(assistantMessage);
|
|
698
|
-
await this.safeCallback(callbacks?.onMessage, assistantMessage);
|
|
699
|
-
|
|
700
|
-
if (toolCalls.length > 0) {
|
|
701
|
-
yield* this.emitProgress(input.executionId, stepIndex, 'tool', messages.length);
|
|
702
|
-
const toolStageSpan = await this.startSpan(
|
|
703
|
-
callbacks,
|
|
704
|
-
traceId,
|
|
705
|
-
'agent.tool.stage',
|
|
706
|
-
runSpan.spanId,
|
|
707
|
-
{
|
|
708
|
-
executionId: input.executionId,
|
|
709
|
-
stepIndex,
|
|
710
|
-
toolCalls: toolCalls.length,
|
|
711
|
-
}
|
|
712
|
-
);
|
|
713
|
-
const toolScope = this.createStageAbortScope(abortSignal, timeoutBudget, 'tool');
|
|
714
|
-
let toolResultMessage: Message | undefined;
|
|
715
|
-
let toolStageErrorCode: string | undefined;
|
|
716
|
-
let toolStageSucceeded = false;
|
|
717
|
-
try {
|
|
718
|
-
const toolGen = this.processToolCalls(
|
|
719
|
-
toolCalls,
|
|
720
|
-
messages,
|
|
721
|
-
stepIndex,
|
|
722
|
-
callbacks,
|
|
723
|
-
toolScope.signal,
|
|
724
|
-
input.executionId,
|
|
725
|
-
traceId,
|
|
726
|
-
toolStageSpan.spanId,
|
|
727
|
-
writeBufferSessions
|
|
728
|
-
);
|
|
729
|
-
for (;;) {
|
|
730
|
-
const next = await toolGen.next();
|
|
731
|
-
if (next.done) {
|
|
732
|
-
toolResultMessage = next.value;
|
|
733
|
-
break;
|
|
734
|
-
}
|
|
735
|
-
yield next.value as StreamEvent;
|
|
736
|
-
}
|
|
737
|
-
toolStageSucceeded = true;
|
|
738
|
-
} catch (error) {
|
|
739
|
-
toolStageErrorCode = this.extractErrorCode(error) || 'AGENT_TOOL_STAGE_FAILED';
|
|
740
|
-
throw error;
|
|
741
|
-
} finally {
|
|
742
|
-
toolScope.release();
|
|
743
|
-
const toolStageLatencyMs = Date.now() - toolStageSpan.startedAt;
|
|
744
|
-
await this.emitMetric(callbacks, {
|
|
745
|
-
name: 'agent.tool.stage.duration_ms',
|
|
746
|
-
value: toolStageLatencyMs,
|
|
747
|
-
unit: 'ms',
|
|
748
|
-
timestamp: Date.now(),
|
|
749
|
-
tags: {
|
|
750
|
-
executionId: input.executionId,
|
|
751
|
-
stepIndex,
|
|
752
|
-
success: toolStageSucceeded ? 'true' : 'false',
|
|
753
|
-
},
|
|
754
|
-
});
|
|
755
|
-
await this.endSpan(callbacks, toolStageSpan, {
|
|
756
|
-
executionId: input.executionId,
|
|
757
|
-
stepIndex,
|
|
758
|
-
latencyMs: toolStageLatencyMs,
|
|
759
|
-
errorCode: toolStageErrorCode,
|
|
760
|
-
toolCalls: toolCalls.length,
|
|
761
|
-
});
|
|
762
|
-
this.logInfo('[Agent] tool.stage', {
|
|
763
|
-
executionId: input.executionId,
|
|
764
|
-
traceId,
|
|
765
|
-
spanId: toolStageSpan.spanId,
|
|
766
|
-
stepIndex,
|
|
767
|
-
latencyMs: toolStageLatencyMs,
|
|
768
|
-
errorCode: toolStageErrorCode,
|
|
769
|
-
toolCalls: toolCalls.length,
|
|
770
|
-
});
|
|
771
|
-
}
|
|
772
|
-
|
|
773
|
-
const lastMessage = toolResultMessage;
|
|
774
|
-
yield* this.yieldCheckpoint(input.executionId, stepIndex, lastMessage, callbacks);
|
|
775
|
-
continue;
|
|
776
|
-
}
|
|
777
|
-
|
|
778
|
-
retryCount = 0;
|
|
779
|
-
runOutcome = 'done';
|
|
780
|
-
terminalDoneEmitted = true;
|
|
781
|
-
yield* this.yieldDoneEvent(stepIndex, 'stop');
|
|
782
|
-
break;
|
|
783
|
-
} catch (error) {
|
|
784
|
-
const timeoutError = this.normalizeTimeoutBudgetError(error, abortSignal);
|
|
785
|
-
if (timeoutError) {
|
|
786
|
-
runOutcome = 'timeout';
|
|
787
|
-
runErrorCode = timeoutError.errorCode;
|
|
788
|
-
yield* this.yieldErrorEvent(timeoutError);
|
|
789
|
-
break;
|
|
790
|
-
}
|
|
791
|
-
|
|
792
|
-
if (this.isAbortError(error) || inputAbortSignal?.aborted) {
|
|
793
|
-
runOutcome = 'aborted';
|
|
794
|
-
runErrorCode = 'AGENT_ABORTED';
|
|
795
|
-
yield* this.yieldErrorEvent(new AgentAbortedError(ABORTED_MESSAGE));
|
|
796
|
-
break;
|
|
797
|
-
}
|
|
798
|
-
|
|
799
|
-
const normalizedError = this.normalizeError(error);
|
|
800
|
-
this.logError('[Agent] run.error', normalizedError, {
|
|
801
|
-
executionId: input.executionId,
|
|
802
|
-
traceId,
|
|
803
|
-
stepIndex,
|
|
804
|
-
retryCount,
|
|
805
|
-
errorCode: normalizedError.errorCode,
|
|
806
|
-
category: normalizedError.category,
|
|
807
|
-
});
|
|
808
|
-
runOutcome = 'error';
|
|
809
|
-
runErrorCode = normalizedError.errorCode;
|
|
810
|
-
const decision = await this.safeErrorCallback(callbacks?.onError, normalizedError);
|
|
811
|
-
yield* this.yieldErrorEvent(normalizedError);
|
|
812
|
-
|
|
813
|
-
const shouldRetry = decision?.retry ?? normalizedError.retryable;
|
|
814
|
-
if (!shouldRetry) {
|
|
815
|
-
break;
|
|
816
|
-
}
|
|
817
|
-
|
|
818
|
-
retryCount++;
|
|
819
|
-
this.logWarn('[Agent] retry.scheduled', {
|
|
820
|
-
executionId: input.executionId,
|
|
821
|
-
traceId,
|
|
822
|
-
stepIndex,
|
|
823
|
-
retryCount,
|
|
824
|
-
errorCode: normalizedError.errorCode,
|
|
825
|
-
});
|
|
826
|
-
if (retryCount < this.config.maxRetryCount) {
|
|
827
|
-
const retryDelay = this.calculateRetryDelay(retryCount, error as Error);
|
|
828
|
-
try {
|
|
829
|
-
await this.sleep(retryDelay, abortSignal);
|
|
830
|
-
} catch (sleepError) {
|
|
831
|
-
const sleepTimeoutError = this.normalizeTimeoutBudgetError(sleepError, abortSignal);
|
|
832
|
-
if (sleepTimeoutError) {
|
|
833
|
-
runOutcome = 'timeout';
|
|
834
|
-
runErrorCode = sleepTimeoutError.errorCode;
|
|
835
|
-
yield* this.yieldErrorEvent(sleepTimeoutError);
|
|
836
|
-
break;
|
|
837
|
-
}
|
|
838
|
-
if (this.isAbortError(sleepError) || inputAbortSignal?.aborted) {
|
|
839
|
-
runOutcome = 'aborted';
|
|
840
|
-
runErrorCode = 'AGENT_ABORTED';
|
|
841
|
-
yield* this.yieldErrorEvent(new AgentAbortedError(ABORTED_MESSAGE));
|
|
842
|
-
break;
|
|
843
|
-
}
|
|
844
|
-
throw sleepError;
|
|
845
|
-
}
|
|
846
|
-
}
|
|
847
|
-
}
|
|
848
|
-
}
|
|
849
|
-
|
|
850
|
-
if (!terminalDoneEmitted && runOutcome === 'done' && stepIndex >= maxSteps) {
|
|
851
|
-
runOutcome = 'max_steps';
|
|
852
|
-
terminalDoneEmitted = true;
|
|
853
|
-
yield* this.yieldDoneEvent(stepIndex, 'max_steps');
|
|
854
|
-
}
|
|
855
|
-
} finally {
|
|
856
|
-
const runLatencyMs = Date.now() - runSpan.startedAt;
|
|
857
|
-
await this.emitMetric(callbacks, {
|
|
858
|
-
name: 'agent.run.duration_ms',
|
|
859
|
-
value: runLatencyMs,
|
|
860
|
-
unit: 'ms',
|
|
861
|
-
timestamp: Date.now(),
|
|
862
|
-
tags: {
|
|
863
|
-
executionId: input.executionId,
|
|
864
|
-
outcome: runOutcome,
|
|
865
|
-
},
|
|
866
|
-
});
|
|
867
|
-
await this.emitMetric(callbacks, {
|
|
868
|
-
name: 'agent.retry.count',
|
|
869
|
-
value: retryCount,
|
|
870
|
-
unit: 'count',
|
|
871
|
-
timestamp: Date.now(),
|
|
872
|
-
tags: {
|
|
873
|
-
executionId: input.executionId,
|
|
874
|
-
},
|
|
875
|
-
});
|
|
876
|
-
await this.endSpan(callbacks, runSpan, {
|
|
877
|
-
executionId: input.executionId,
|
|
878
|
-
stepIndex,
|
|
879
|
-
latencyMs: runLatencyMs,
|
|
880
|
-
outcome: runOutcome,
|
|
881
|
-
errorCode: runErrorCode,
|
|
882
|
-
retryCount,
|
|
883
|
-
});
|
|
884
|
-
this.logInfo('[Agent] run.finish', {
|
|
885
|
-
executionId: input.executionId,
|
|
886
|
-
traceId,
|
|
887
|
-
spanId: runSpan.spanId,
|
|
888
|
-
stepIndex,
|
|
889
|
-
latencyMs: runLatencyMs,
|
|
890
|
-
outcome: runOutcome,
|
|
891
|
-
errorCode: runErrorCode,
|
|
892
|
-
retryCount,
|
|
893
|
-
});
|
|
894
|
-
executionScope.release();
|
|
895
|
-
}
|
|
896
|
-
}
|
|
897
|
-
|
|
898
|
-
private async safeCallback<T>(
|
|
899
|
-
callback: ((arg: T) => void | Promise<void>) | undefined,
|
|
900
|
-
arg: T
|
|
901
|
-
): Promise<void> {
|
|
902
|
-
await invokeSafeCallback(callback, arg, (error) =>
|
|
903
|
-
this.logError('[Agent] Callback error:', error)
|
|
904
|
-
);
|
|
905
|
-
}
|
|
906
|
-
|
|
907
|
-
private async safeErrorCallback(
|
|
908
|
-
callback: ((error: Error) => ErrorDecision | void | Promise<ErrorDecision | void>) | undefined,
|
|
909
|
-
error: Error
|
|
910
|
-
): Promise<ErrorDecision | undefined> {
|
|
911
|
-
return invokeSafeErrorCallback(callback, error, (err) =>
|
|
912
|
-
this.logError('[Agent] Error callback error:', err)
|
|
913
|
-
);
|
|
914
|
-
}
|
|
915
|
-
|
|
916
|
-
private async mergeToolCalls(
|
|
917
|
-
existing: ToolCall[],
|
|
918
|
-
newCalls: ToolCall[],
|
|
919
|
-
messageId: string,
|
|
920
|
-
executionId: string | undefined,
|
|
921
|
-
stepIndex: number,
|
|
922
|
-
writeBufferSessions: Map<string, WriteBufferRuntime>
|
|
923
|
-
): Promise<ToolCall[]> {
|
|
924
|
-
return mergeToolCallsWithBuffer({
|
|
925
|
-
existing,
|
|
926
|
-
incoming: newCalls,
|
|
927
|
-
messageId,
|
|
928
|
-
onArgumentsChunk: async (toolCall, argumentsChunk, chunkMessageId) => {
|
|
929
|
-
const sessionKey = createWriteFileSessionKey({
|
|
930
|
-
executionId,
|
|
931
|
-
stepIndex,
|
|
932
|
-
toolCallId: toolCall.id,
|
|
933
|
-
});
|
|
934
|
-
await bufferWriteFileChunk({
|
|
935
|
-
toolCall,
|
|
936
|
-
argumentsChunk,
|
|
937
|
-
messageId: chunkMessageId,
|
|
938
|
-
sessionKey,
|
|
939
|
-
sessions: writeBufferSessions,
|
|
940
|
-
onError: (error) =>
|
|
941
|
-
this.logError('[Agent] Failed to buffer write_file tool chunk:', error),
|
|
942
|
-
});
|
|
943
|
-
},
|
|
944
|
-
});
|
|
945
|
-
}
|
|
946
|
-
|
|
947
|
-
private async *callLLMAndProcessStream(
|
|
948
|
-
messages: Message[],
|
|
949
|
-
config: AgentInput['config'],
|
|
950
|
-
abortSignal?: AbortSignal,
|
|
951
|
-
executionId?: string,
|
|
952
|
-
stepIndex = 0,
|
|
953
|
-
writeBufferSessions: Map<string, WriteBufferRuntime> = new Map()
|
|
954
|
-
): AsyncGenerator<StreamEvent, { assistantMessage: Message; toolCalls: ToolCall[] }, unknown> {
|
|
955
|
-
const requestPlan = this.buildLLMRequestPlan(messages, config);
|
|
956
|
-
const stream = this.llmProvider.generateStream(
|
|
957
|
-
requestPlan.requestMessages,
|
|
958
|
-
requestPlan.requestConfig
|
|
959
|
-
);
|
|
960
|
-
|
|
961
|
-
const assistantMessage: Message = {
|
|
962
|
-
messageId: generateId('msg_'),
|
|
963
|
-
type: 'assistant-text',
|
|
964
|
-
role: 'assistant',
|
|
965
|
-
content: '',
|
|
966
|
-
reasoning_content: '',
|
|
967
|
-
timestamp: Date.now(),
|
|
968
|
-
};
|
|
969
|
-
|
|
970
|
-
let toolCalls: ToolCall[] = [];
|
|
971
|
-
let finished = false;
|
|
972
|
-
|
|
973
|
-
for await (const chunk of stream) {
|
|
974
|
-
this.throwIfAborted(abortSignal);
|
|
975
|
-
const choices = chunk.choices;
|
|
976
|
-
const delta = choices?.[0]?.delta;
|
|
977
|
-
|
|
978
|
-
if (typeof chunk.id === 'string' && chunk.id.trim().length > 0) {
|
|
979
|
-
assistantMessage.metadata = {
|
|
980
|
-
...assistantMessage.metadata,
|
|
981
|
-
responseId: chunk.id,
|
|
982
|
-
};
|
|
983
|
-
}
|
|
984
|
-
|
|
985
|
-
if (chunk.usage) {
|
|
986
|
-
assistantMessage.usage = chunk.usage;
|
|
987
|
-
}
|
|
988
|
-
|
|
989
|
-
if (finished) {
|
|
990
|
-
continue;
|
|
991
|
-
}
|
|
992
|
-
|
|
993
|
-
if (typeof delta?.content === 'string') {
|
|
994
|
-
assistantMessage.content = `${assistantMessage.content}${delta.content}`;
|
|
995
|
-
|
|
996
|
-
yield {
|
|
997
|
-
type: 'chunk',
|
|
998
|
-
data: {
|
|
999
|
-
messageId: assistantMessage.messageId,
|
|
1000
|
-
content: delta.content,
|
|
1001
|
-
delta: true,
|
|
1002
|
-
},
|
|
1003
|
-
};
|
|
1004
|
-
}
|
|
1005
|
-
|
|
1006
|
-
if (typeof delta?.reasoning_content === 'string') {
|
|
1007
|
-
const currentReasoning = assistantMessage.reasoning_content;
|
|
1008
|
-
assistantMessage.reasoning_content = `${currentReasoning || ''}${delta.reasoning_content}`;
|
|
1009
|
-
|
|
1010
|
-
yield {
|
|
1011
|
-
type: 'reasoning_chunk',
|
|
1012
|
-
data: {
|
|
1013
|
-
messageId: assistantMessage.messageId,
|
|
1014
|
-
reasoningContent: delta.reasoning_content,
|
|
1015
|
-
delta: true,
|
|
1016
|
-
},
|
|
1017
|
-
};
|
|
1018
|
-
}
|
|
1019
|
-
|
|
1020
|
-
if (delta?.tool_calls) {
|
|
1021
|
-
toolCalls = await this.mergeToolCalls(
|
|
1022
|
-
toolCalls,
|
|
1023
|
-
delta.tool_calls,
|
|
1024
|
-
assistantMessage.messageId,
|
|
1025
|
-
executionId,
|
|
1026
|
-
stepIndex,
|
|
1027
|
-
writeBufferSessions
|
|
1028
|
-
);
|
|
1029
|
-
|
|
1030
|
-
yield {
|
|
1031
|
-
type: 'tool_call',
|
|
1032
|
-
data: {
|
|
1033
|
-
messageId: assistantMessage.messageId,
|
|
1034
|
-
toolCalls,
|
|
1035
|
-
},
|
|
1036
|
-
};
|
|
1037
|
-
}
|
|
1038
|
-
|
|
1039
|
-
const finishReason =
|
|
1040
|
-
choices?.[0]?.finish_reason ||
|
|
1041
|
-
(delta as { finish_reason?: string } | undefined)?.finish_reason;
|
|
1042
|
-
if (finishReason) {
|
|
1043
|
-
finished = true;
|
|
1044
|
-
}
|
|
1045
|
-
}
|
|
1046
|
-
|
|
1047
|
-
assistantMessage.tool_calls = toolCalls.length > 0 ? toolCalls : undefined;
|
|
1048
|
-
assistantMessage.type = toolCalls.length > 0 ? 'tool-call' : 'assistant-text';
|
|
1049
|
-
assistantMessage.metadata = {
|
|
1050
|
-
...assistantMessage.metadata,
|
|
1051
|
-
llmRequestConfigHash: requestPlan.requestConfigHash,
|
|
1052
|
-
llmRequestInputHash: requestPlan.requestInputHash,
|
|
1053
|
-
llmRequestInputMessageCount: requestPlan.requestInputMessageCount,
|
|
1054
|
-
continuationMode: requestPlan.continuationMode,
|
|
1055
|
-
continuationDeltaMessageCount: requestPlan.continuationDeltaMessageCount,
|
|
1056
|
-
...(requestPlan.previousResponseIdUsed
|
|
1057
|
-
? {
|
|
1058
|
-
previousResponseIdUsed: requestPlan.previousResponseIdUsed,
|
|
1059
|
-
continuationBaselineMessageCount: requestPlan.continuationBaselineMessageCount,
|
|
1060
|
-
}
|
|
1061
|
-
: {}),
|
|
1062
|
-
};
|
|
1063
|
-
assistantMessage.metadata = {
|
|
1064
|
-
...assistantMessage.metadata,
|
|
1065
|
-
llmResponseMessageHash: hashValueForContinuation(
|
|
1066
|
-
this.convertMessageToLLMMessage(assistantMessage)
|
|
1067
|
-
),
|
|
1068
|
-
};
|
|
1069
|
-
|
|
1070
|
-
const hasAssistantText = hasNonEmptyText(assistantMessage.content);
|
|
1071
|
-
const hasReasoningText = hasNonEmptyText(assistantMessage.reasoning_content);
|
|
1072
|
-
if (toolCalls.length === 0 && !hasAssistantText && !hasReasoningText) {
|
|
1073
|
-
throw new AgentUpstreamRetryableError('LLM returned an empty assistant response');
|
|
1074
|
-
}
|
|
1075
|
-
|
|
1076
|
-
return { assistantMessage, toolCalls };
|
|
1077
|
-
}
|
|
1078
|
-
|
|
1079
|
-
private async *executeTool(
|
|
1080
|
-
toolCall: ToolCall,
|
|
1081
|
-
stepIndex: number,
|
|
1082
|
-
callbacks?: AgentCallbacks,
|
|
1083
|
-
abortSignal?: AbortSignal,
|
|
1084
|
-
executionId?: string,
|
|
1085
|
-
traceId?: string,
|
|
1086
|
-
parentSpanId?: string,
|
|
1087
|
-
writeBufferSessions: Map<string, WriteBufferRuntime> = new Map()
|
|
1088
|
-
): AsyncGenerator<StreamEvent, Message, unknown> {
|
|
1089
|
-
this.throwIfAborted(abortSignal);
|
|
1090
|
-
const effectiveTraceId = traceId || executionId || generateId('trace_');
|
|
1091
|
-
const toolSpan = await this.startSpan(
|
|
1092
|
-
callbacks,
|
|
1093
|
-
effectiveTraceId,
|
|
1094
|
-
'agent.tool.execute',
|
|
1095
|
-
parentSpanId,
|
|
1096
|
-
{
|
|
1097
|
-
executionId,
|
|
1098
|
-
stepIndex,
|
|
1099
|
-
toolCallId: toolCall.id,
|
|
1100
|
-
toolName: toolCall.function.name,
|
|
1101
|
-
}
|
|
1102
|
-
);
|
|
1103
|
-
let toolErrorCode: string | undefined;
|
|
1104
|
-
let cachedHit = false;
|
|
1105
|
-
let toolSucceeded = false;
|
|
1106
|
-
try {
|
|
1107
|
-
const writeFileSessionKey = createWriteFileSessionKey({
|
|
1108
|
-
executionId,
|
|
1109
|
-
stepIndex,
|
|
1110
|
-
toolCallId: toolCall.id,
|
|
1111
|
-
});
|
|
1112
|
-
|
|
1113
|
-
const ledgerResult = await executeWithToolLedger({
|
|
1114
|
-
ledger: this.toolExecutionLedger,
|
|
1115
|
-
executionId,
|
|
1116
|
-
toolCallId: toolCall.id,
|
|
1117
|
-
execute: async () => {
|
|
1118
|
-
const toolExecResult = await this.toolExecutor.execute(toolCall, {
|
|
1119
|
-
onChunk: (chunk) => {
|
|
1120
|
-
this.emit('tool_chunk', {
|
|
1121
|
-
toolCallId: toolCall.id,
|
|
1122
|
-
toolName: toolCall.function.name,
|
|
1123
|
-
arguments: toolCall.function.arguments,
|
|
1124
|
-
chunk: chunk.data,
|
|
1125
|
-
chunkType: chunk.type,
|
|
1126
|
-
});
|
|
1127
|
-
},
|
|
1128
|
-
onConfirm: async (info) => {
|
|
1129
|
-
return new Promise((resolve) => {
|
|
1130
|
-
let settled = false;
|
|
1131
|
-
const abortHandler = () => {
|
|
1132
|
-
if (!settled) {
|
|
1133
|
-
settled = true;
|
|
1134
|
-
resolve({ approved: false, message: ABORTED_MESSAGE });
|
|
1135
|
-
}
|
|
1136
|
-
};
|
|
1137
|
-
|
|
1138
|
-
const cleanup = () => {
|
|
1139
|
-
if (!settled) {
|
|
1140
|
-
settled = true;
|
|
1141
|
-
}
|
|
1142
|
-
abortSignal?.removeEventListener('abort', abortHandler);
|
|
1143
|
-
};
|
|
1144
|
-
|
|
1145
|
-
if (abortSignal?.aborted) {
|
|
1146
|
-
abortHandler();
|
|
1147
|
-
return;
|
|
1148
|
-
}
|
|
1149
|
-
abortSignal?.addEventListener('abort', abortHandler, { once: true });
|
|
1150
|
-
|
|
1151
|
-
this.emit('tool_confirm', {
|
|
1152
|
-
...info,
|
|
1153
|
-
resolve: (decision: ToolDecision) => {
|
|
1154
|
-
cleanup();
|
|
1155
|
-
resolve(decision);
|
|
1156
|
-
},
|
|
1157
|
-
});
|
|
1158
|
-
});
|
|
1159
|
-
},
|
|
1160
|
-
onPolicyCheck: callbacks?.onToolPolicy
|
|
1161
|
-
? async (info) => {
|
|
1162
|
-
const decision = await callbacks.onToolPolicy?.(info);
|
|
1163
|
-
return decision || { allowed: true };
|
|
1164
|
-
}
|
|
1165
|
-
: undefined,
|
|
1166
|
-
toolCallId: toolCall.id,
|
|
1167
|
-
loopIndex: stepIndex,
|
|
1168
|
-
agent: this,
|
|
1169
|
-
toolAbortSignal: abortSignal,
|
|
1170
|
-
});
|
|
1171
|
-
|
|
1172
|
-
let toolOutput = '';
|
|
1173
|
-
let toolSummary = '';
|
|
1174
|
-
let errorCode: string | undefined;
|
|
1175
|
-
|
|
1176
|
-
if (toolExecResult.success) {
|
|
1177
|
-
toolOutput = toolExecResult.output || '';
|
|
1178
|
-
await cleanupWriteFileBufferSession(toolCall, writeBufferSessions, writeFileSessionKey);
|
|
1179
|
-
} else {
|
|
1180
|
-
if (isWriteFileTool(toolCall)) {
|
|
1181
|
-
if (isWriteFileProtocolResponse(toolExecResult.output)) {
|
|
1182
|
-
toolOutput = toolExecResult.output;
|
|
1183
|
-
} else if (needEnrichWriteFileFailure(toolExecResult.error, toolExecResult.output)) {
|
|
1184
|
-
const errorContent =
|
|
1185
|
-
toolExecResult.error?.message ||
|
|
1186
|
-
toolExecResult.output ||
|
|
1187
|
-
new UnknownError().message;
|
|
1188
|
-
toolOutput = await buildWriteFileToolErrorPayload(
|
|
1189
|
-
toolCall,
|
|
1190
|
-
errorContent,
|
|
1191
|
-
writeBufferSessions,
|
|
1192
|
-
writeFileSessionKey
|
|
1193
|
-
);
|
|
1194
|
-
} else {
|
|
1195
|
-
toolOutput =
|
|
1196
|
-
toolExecResult.error?.message ||
|
|
1197
|
-
toolExecResult.output ||
|
|
1198
|
-
new UnknownError().message;
|
|
1199
|
-
}
|
|
1200
|
-
} else {
|
|
1201
|
-
toolOutput =
|
|
1202
|
-
toolExecResult.error?.message ||
|
|
1203
|
-
toolExecResult.output ||
|
|
1204
|
-
new UnknownError().message;
|
|
1205
|
-
}
|
|
1206
|
-
errorCode = this.extractErrorCode(toolExecResult.error) || 'TOOL_EXECUTION_FAILED';
|
|
1207
|
-
}
|
|
1208
|
-
|
|
1209
|
-
toolSummary = this.resolveToolResultSummary(toolCall, toolExecResult, toolOutput);
|
|
1210
|
-
|
|
1211
|
-
return {
|
|
1212
|
-
success: toolExecResult.success,
|
|
1213
|
-
output: toolOutput,
|
|
1214
|
-
summary: toolSummary,
|
|
1215
|
-
payload: toolExecResult.payload,
|
|
1216
|
-
metadata: toolExecResult.metadata,
|
|
1217
|
-
errorName: toolExecResult.error?.name,
|
|
1218
|
-
errorMessage: toolExecResult.error?.message,
|
|
1219
|
-
errorCode,
|
|
1220
|
-
recordedAt: Date.now(),
|
|
1221
|
-
};
|
|
1222
|
-
},
|
|
1223
|
-
onError: (error) => {
|
|
1224
|
-
this.logError('[Agent] Failed to execute tool with ledger:', error, {
|
|
1225
|
-
executionId,
|
|
1226
|
-
stepIndex,
|
|
1227
|
-
toolCallId: toolCall.id,
|
|
1228
|
-
});
|
|
1229
|
-
},
|
|
1230
|
-
});
|
|
1231
|
-
|
|
1232
|
-
cachedHit = ledgerResult.fromCache;
|
|
1233
|
-
toolSucceeded = ledgerResult.record.success;
|
|
1234
|
-
toolErrorCode = ledgerResult.record.errorCode;
|
|
1235
|
-
|
|
1236
|
-
const replayResult = this.createToolResultMessageFromLedger(toolCall.id, ledgerResult.record);
|
|
1237
|
-
await this.safeCallback(callbacks?.onMessage, replayResult);
|
|
1238
|
-
|
|
1239
|
-
yield {
|
|
1240
|
-
type: 'tool_result',
|
|
1241
|
-
data: replayResult,
|
|
1242
|
-
};
|
|
1243
|
-
|
|
1244
|
-
return replayResult;
|
|
1245
|
-
} catch (error) {
|
|
1246
|
-
toolErrorCode = this.extractErrorCode(error) || 'TOOL_EXECUTION_FAILED';
|
|
1247
|
-
throw error;
|
|
1248
|
-
} finally {
|
|
1249
|
-
const toolLatencyMs = Date.now() - toolSpan.startedAt;
|
|
1250
|
-
await this.emitMetric(callbacks, {
|
|
1251
|
-
name: 'agent.tool.duration_ms',
|
|
1252
|
-
value: toolLatencyMs,
|
|
1253
|
-
unit: 'ms',
|
|
1254
|
-
timestamp: Date.now(),
|
|
1255
|
-
tags: {
|
|
1256
|
-
executionId: executionId || '',
|
|
1257
|
-
stepIndex,
|
|
1258
|
-
toolCallId: toolCall.id,
|
|
1259
|
-
cached: cachedHit ? 'true' : 'false',
|
|
1260
|
-
success: toolSucceeded ? 'true' : 'false',
|
|
1261
|
-
},
|
|
1262
|
-
});
|
|
1263
|
-
await this.endSpan(callbacks, toolSpan, {
|
|
1264
|
-
executionId,
|
|
1265
|
-
stepIndex,
|
|
1266
|
-
toolCallId: toolCall.id,
|
|
1267
|
-
toolName: toolCall.function.name,
|
|
1268
|
-
latencyMs: toolLatencyMs,
|
|
1269
|
-
cached: cachedHit,
|
|
1270
|
-
errorCode: toolErrorCode,
|
|
1271
|
-
});
|
|
1272
|
-
this.logInfo('[Agent] tool.execute', {
|
|
1273
|
-
executionId,
|
|
1274
|
-
traceId: effectiveTraceId,
|
|
1275
|
-
spanId: toolSpan.spanId,
|
|
1276
|
-
parentSpanId: toolSpan.parentSpanId,
|
|
1277
|
-
stepIndex,
|
|
1278
|
-
toolCallId: toolCall.id,
|
|
1279
|
-
toolName: toolCall.function.name,
|
|
1280
|
-
latencyMs: toolLatencyMs,
|
|
1281
|
-
cached: cachedHit,
|
|
1282
|
-
errorCode: toolErrorCode,
|
|
1283
|
-
});
|
|
1284
|
-
}
|
|
1285
|
-
}
|
|
1286
|
-
|
|
1287
|
-
private async *processToolCalls(
|
|
1288
|
-
toolCalls: ToolCall[],
|
|
1289
|
-
messages: Message[],
|
|
1290
|
-
stepIndex: number,
|
|
1291
|
-
callbacks?: AgentCallbacks,
|
|
1292
|
-
abortSignal?: AbortSignal,
|
|
1293
|
-
executionId?: string,
|
|
1294
|
-
traceId?: string,
|
|
1295
|
-
parentSpanId?: string,
|
|
1296
|
-
writeBufferSessions: Map<string, WriteBufferRuntime> = new Map()
|
|
1297
|
-
): AsyncGenerator<StreamEvent, Message, unknown> {
|
|
1298
|
-
if (this.config.maxConcurrentToolCalls <= 1 || toolCalls.length <= 1) {
|
|
1299
|
-
for (const toolCall of toolCalls) {
|
|
1300
|
-
this.throwIfAborted(abortSignal);
|
|
1301
|
-
yield* this.emitProgress(executionId, stepIndex, 'tool', messages.length);
|
|
1302
|
-
|
|
1303
|
-
const toolGen = this.executeTool(
|
|
1304
|
-
toolCall,
|
|
1305
|
-
stepIndex,
|
|
1306
|
-
callbacks,
|
|
1307
|
-
abortSignal,
|
|
1308
|
-
executionId,
|
|
1309
|
-
traceId,
|
|
1310
|
-
parentSpanId,
|
|
1311
|
-
writeBufferSessions
|
|
1312
|
-
);
|
|
1313
|
-
let resultMessage: Message | undefined;
|
|
1314
|
-
for (;;) {
|
|
1315
|
-
const next = await toolGen.next();
|
|
1316
|
-
if (next.done) {
|
|
1317
|
-
resultMessage = next.value;
|
|
1318
|
-
break;
|
|
1319
|
-
}
|
|
1320
|
-
yield next.value as StreamEvent;
|
|
1321
|
-
}
|
|
1322
|
-
|
|
1323
|
-
if (resultMessage) {
|
|
1324
|
-
messages.push(resultMessage);
|
|
1325
|
-
}
|
|
1326
|
-
}
|
|
1327
|
-
|
|
1328
|
-
const lastMessage = messages[messages.length - 1];
|
|
1329
|
-
return lastMessage;
|
|
1330
|
-
}
|
|
1331
|
-
|
|
1332
|
-
const plans = toolCalls.map((toolCall) => ({
|
|
1333
|
-
toolCall,
|
|
1334
|
-
policy: this.resolveToolConcurrencyPolicy(toolCall),
|
|
1335
|
-
}));
|
|
1336
|
-
|
|
1337
|
-
for (let i = 0; i < plans.length; i++) {
|
|
1338
|
-
this.throwIfAborted(abortSignal);
|
|
1339
|
-
yield* this.emitProgress(executionId, stepIndex, 'tool', messages.length);
|
|
1340
|
-
}
|
|
1341
|
-
|
|
1342
|
-
const waves = this.buildExecutionWaves(plans);
|
|
1343
|
-
const allResults: Array<{ events: StreamEvent[]; message?: Message }> = [];
|
|
1344
|
-
for (const wave of waves) {
|
|
1345
|
-
this.throwIfAborted(abortSignal);
|
|
1346
|
-
if (wave.type === 'exclusive') {
|
|
1347
|
-
allResults.push(
|
|
1348
|
-
await this.executeToolTask(
|
|
1349
|
-
wave.plans[0].toolCall,
|
|
1350
|
-
stepIndex,
|
|
1351
|
-
callbacks,
|
|
1352
|
-
abortSignal,
|
|
1353
|
-
executionId,
|
|
1354
|
-
traceId,
|
|
1355
|
-
parentSpanId,
|
|
1356
|
-
writeBufferSessions
|
|
1357
|
-
)
|
|
1358
|
-
);
|
|
1359
|
-
continue;
|
|
1360
|
-
}
|
|
1361
|
-
|
|
1362
|
-
const parallelResults = await this.runParallelWave(
|
|
1363
|
-
wave.plans,
|
|
1364
|
-
stepIndex,
|
|
1365
|
-
callbacks,
|
|
1366
|
-
abortSignal,
|
|
1367
|
-
executionId,
|
|
1368
|
-
traceId,
|
|
1369
|
-
parentSpanId,
|
|
1370
|
-
writeBufferSessions
|
|
1371
|
-
);
|
|
1372
|
-
allResults.push(...parallelResults);
|
|
1373
|
-
}
|
|
1374
|
-
|
|
1375
|
-
for (const taskResult of allResults) {
|
|
1376
|
-
for (const event of taskResult.events) {
|
|
1377
|
-
yield event;
|
|
1378
|
-
}
|
|
1379
|
-
if (taskResult.message) {
|
|
1380
|
-
messages.push(taskResult.message);
|
|
1381
|
-
}
|
|
1382
|
-
this.throwIfAborted(abortSignal);
|
|
1383
|
-
}
|
|
1384
|
-
|
|
1385
|
-
const lastMessage = messages[messages.length - 1];
|
|
1386
|
-
return lastMessage;
|
|
1387
|
-
}
|
|
1388
|
-
|
|
1389
|
-
private resolveToolConcurrencyPolicy(toolCall: ToolCall): ToolConcurrencyPolicy {
|
|
1390
|
-
if (this.config.toolConcurrencyPolicyResolver) {
|
|
1391
|
-
return this.config.toolConcurrencyPolicyResolver(toolCall);
|
|
1392
|
-
}
|
|
1393
|
-
|
|
1394
|
-
const manager = this.toolExecutor as ToolManager & {
|
|
1395
|
-
getConcurrencyPolicy?: (call: ToolCall) => ToolConcurrencyPolicy;
|
|
1396
|
-
};
|
|
1397
|
-
if (typeof manager.getConcurrencyPolicy === 'function') {
|
|
1398
|
-
return manager.getConcurrencyPolicy(toolCall);
|
|
1399
|
-
}
|
|
1400
|
-
|
|
1401
|
-
return { mode: 'exclusive' };
|
|
1402
|
-
}
|
|
1403
|
-
|
|
1404
|
-
private buildExecutionWaves(
|
|
1405
|
-
plans: Array<{ toolCall: ToolCall; policy: ToolConcurrencyPolicy }>
|
|
1406
|
-
): Array<{
|
|
1407
|
-
type: 'exclusive' | 'parallel';
|
|
1408
|
-
plans: Array<{ toolCall: ToolCall; policy: ToolConcurrencyPolicy }>;
|
|
1409
|
-
}> {
|
|
1410
|
-
return buildToolExecutionWaves(plans);
|
|
1411
|
-
}
|
|
1412
|
-
|
|
1413
|
-
private async runParallelWave(
|
|
1414
|
-
plans: Array<{ toolCall: ToolCall; policy: ToolConcurrencyPolicy }>,
|
|
1415
|
-
stepIndex: number,
|
|
1416
|
-
callbacks?: AgentCallbacks,
|
|
1417
|
-
abortSignal?: AbortSignal,
|
|
1418
|
-
executionId?: string,
|
|
1419
|
-
traceId?: string,
|
|
1420
|
-
parentSpanId?: string,
|
|
1421
|
-
writeBufferSessions: Map<string, WriteBufferRuntime> = new Map()
|
|
1422
|
-
): Promise<Array<{ events: StreamEvent[]; message?: Message }>> {
|
|
1423
|
-
const tasks = plans.map((plan) => ({
|
|
1424
|
-
lockKey: plan.policy.lockKey,
|
|
1425
|
-
run: async () =>
|
|
1426
|
-
this.executeToolTask(
|
|
1427
|
-
plan.toolCall,
|
|
1428
|
-
stepIndex,
|
|
1429
|
-
callbacks,
|
|
1430
|
-
abortSignal,
|
|
1431
|
-
executionId,
|
|
1432
|
-
traceId,
|
|
1433
|
-
parentSpanId,
|
|
1434
|
-
writeBufferSessions
|
|
1435
|
-
),
|
|
1436
|
-
}));
|
|
1437
|
-
return this.runWithConcurrencyAndLock(tasks, this.config.maxConcurrentToolCalls);
|
|
1438
|
-
}
|
|
1439
|
-
|
|
1440
|
-
private async executeToolTask(
|
|
1441
|
-
toolCall: ToolCall,
|
|
1442
|
-
stepIndex: number,
|
|
1443
|
-
callbacks?: AgentCallbacks,
|
|
1444
|
-
abortSignal?: AbortSignal,
|
|
1445
|
-
executionId?: string,
|
|
1446
|
-
traceId?: string,
|
|
1447
|
-
parentSpanId?: string,
|
|
1448
|
-
writeBufferSessions: Map<string, WriteBufferRuntime> = new Map()
|
|
1449
|
-
): Promise<{ events: StreamEvent[]; message?: Message }> {
|
|
1450
|
-
const events: StreamEvent[] = [];
|
|
1451
|
-
const toolGen = this.executeTool(
|
|
1452
|
-
toolCall,
|
|
1453
|
-
stepIndex,
|
|
1454
|
-
callbacks,
|
|
1455
|
-
abortSignal,
|
|
1456
|
-
executionId,
|
|
1457
|
-
traceId,
|
|
1458
|
-
parentSpanId,
|
|
1459
|
-
writeBufferSessions
|
|
1460
|
-
);
|
|
1461
|
-
let resultMessage: Message | undefined;
|
|
1462
|
-
for (;;) {
|
|
1463
|
-
const next = await toolGen.next();
|
|
1464
|
-
if (next.done) {
|
|
1465
|
-
resultMessage = next.value;
|
|
1466
|
-
break;
|
|
1467
|
-
}
|
|
1468
|
-
events.push(next.value as StreamEvent);
|
|
1469
|
-
}
|
|
1470
|
-
return {
|
|
1471
|
-
events,
|
|
1472
|
-
message: resultMessage,
|
|
1473
|
-
};
|
|
1474
|
-
}
|
|
1475
|
-
|
|
1476
|
-
private async runWithConcurrencyAndLock<T>(
|
|
1477
|
-
tasks: Array<{ lockKey?: string; run: () => Promise<T> }>,
|
|
1478
|
-
limit: number
|
|
1479
|
-
): Promise<T[]> {
|
|
1480
|
-
return runTasksWithConcurrencyAndLock(tasks, limit);
|
|
1481
|
-
}
|
|
1482
|
-
|
|
1483
|
-
private async *yieldCheckpoint(
|
|
1484
|
-
executionId: string | undefined,
|
|
1485
|
-
stepIndex: number,
|
|
1486
|
-
lastMessage: Message | undefined,
|
|
1487
|
-
callbacks?: AgentCallbacks
|
|
1488
|
-
): AsyncGenerator<StreamEvent, void, unknown> {
|
|
1489
|
-
const checkpoint = createCheckpoint(executionId, stepIndex, lastMessage?.messageId);
|
|
1490
|
-
await this.safeCallback(callbacks?.onCheckpoint, checkpoint);
|
|
1491
|
-
|
|
1492
|
-
yield {
|
|
1493
|
-
type: 'checkpoint',
|
|
1494
|
-
data: checkpoint,
|
|
1495
|
-
};
|
|
1496
|
-
}
|
|
1497
|
-
|
|
1498
|
-
private *yieldMaxRetriesError(): Generator<StreamEvent> {
|
|
1499
|
-
yield* this.yieldErrorEvent(new MaxRetriesError());
|
|
1500
|
-
}
|
|
1501
|
-
|
|
1502
|
-
private *emitProgress(
|
|
1503
|
-
executionId: string | undefined,
|
|
1504
|
-
stepIndex: number,
|
|
1505
|
-
currentAction: 'llm' | 'tool',
|
|
1506
|
-
messageCount: number
|
|
1507
|
-
): Generator<StreamEvent> {
|
|
1508
|
-
yield createProgressEvent(executionId, stepIndex, currentAction, messageCount);
|
|
1509
|
-
}
|
|
1510
|
-
|
|
1511
|
-
private *yieldErrorEvent(error: AgentError): Generator<StreamEvent> {
|
|
1512
|
-
yield createErrorEvent(error);
|
|
1513
|
-
}
|
|
1514
|
-
|
|
1515
|
-
private *yieldDoneEvent(
|
|
1516
|
-
stepIndex: number,
|
|
1517
|
-
finishReason: 'stop' | 'max_steps' = 'stop'
|
|
1518
|
-
): Generator<StreamEvent> {
|
|
1519
|
-
yield createDoneEvent(stepIndex, finishReason);
|
|
1520
|
-
}
|
|
1521
|
-
|
|
1522
|
-
private mergeLLMConfig(
|
|
1523
|
-
config: AgentInput['config'],
|
|
1524
|
-
tools?: AgentInput['tools'],
|
|
1525
|
-
abortSignal?: AbortSignal,
|
|
1526
|
-
conversationId?: string
|
|
1527
|
-
): AgentInput['config'] {
|
|
1528
|
-
const merged = mergeLLMRequestConfig(config, tools, abortSignal);
|
|
1529
|
-
if (
|
|
1530
|
-
typeof conversationId !== 'string' ||
|
|
1531
|
-
conversationId.trim().length === 0 ||
|
|
1532
|
-
merged?.prompt_cache_key
|
|
1533
|
-
) {
|
|
1534
|
-
return merged;
|
|
1535
|
-
}
|
|
1536
|
-
|
|
1537
|
-
// Use the conversation id as the default sticky cache routing key so
|
|
1538
|
-
// repeated full replays can still hit provider-side prefix caching.
|
|
1539
|
-
return {
|
|
1540
|
-
...(merged || {}),
|
|
1541
|
-
prompt_cache_key: conversationId,
|
|
1542
|
-
};
|
|
1543
|
-
}
|
|
1544
|
-
|
|
1545
|
-
private resolveLLMTools(inputTools?: Tool[]): Tool[] | undefined {
|
|
1546
|
-
if (typeof inputTools !== 'undefined') {
|
|
1547
|
-
return inputTools;
|
|
1548
|
-
}
|
|
1549
|
-
|
|
1550
|
-
const manager = this.toolExecutor as ToolManager & {
|
|
1551
|
-
getTools?: () => Array<{ toToolSchema?: () => unknown }>;
|
|
1552
|
-
};
|
|
1553
|
-
if (typeof manager.getTools !== 'function') {
|
|
1554
|
-
return undefined;
|
|
1555
|
-
}
|
|
1556
|
-
|
|
1557
|
-
const schemas: Tool[] = [];
|
|
1558
|
-
for (const tool of manager.getTools()) {
|
|
1559
|
-
if (typeof tool.toToolSchema !== 'function') {
|
|
1560
|
-
continue;
|
|
1561
|
-
}
|
|
1562
|
-
const schema = tool.toToolSchema();
|
|
1563
|
-
schemas.push({
|
|
1564
|
-
type: schema.type,
|
|
1565
|
-
function: {
|
|
1566
|
-
name: schema.function.name,
|
|
1567
|
-
description: schema.function.description,
|
|
1568
|
-
parameters: (schema.function.parameters as Record<string, unknown> | undefined) || {},
|
|
1569
|
-
},
|
|
1570
|
-
});
|
|
1571
|
-
}
|
|
1572
|
-
|
|
1573
|
-
return schemas.length > 0 ? schemas : undefined;
|
|
1574
|
-
}
|
|
1575
|
-
|
|
1576
|
-
private async emitMetric(
|
|
1577
|
-
callbacks: AgentCallbacks | undefined,
|
|
1578
|
-
metric: Parameters<typeof pushMetric>[1]
|
|
1579
|
-
): Promise<void> {
|
|
1580
|
-
await pushMetric(callbacks, metric, this.safeCallback.bind(this));
|
|
1581
|
-
}
|
|
1582
|
-
|
|
1583
|
-
private async emitTrace(
|
|
1584
|
-
callbacks: AgentCallbacks | undefined,
|
|
1585
|
-
event: Parameters<typeof pushTrace>[1]
|
|
1586
|
-
): Promise<void> {
|
|
1587
|
-
await pushTrace(callbacks, event, this.safeCallback.bind(this));
|
|
1588
|
-
}
|
|
1589
|
-
|
|
1590
|
-
private async startSpan(
|
|
1591
|
-
callbacks: AgentCallbacks | undefined,
|
|
1592
|
-
traceId: string,
|
|
1593
|
-
name: string,
|
|
1594
|
-
parentSpanId?: string,
|
|
1595
|
-
attributes?: Record<string, unknown>
|
|
1596
|
-
): Promise<SpanRuntime> {
|
|
1597
|
-
return beginSpan({
|
|
1598
|
-
callbacks,
|
|
1599
|
-
traceId,
|
|
1600
|
-
name,
|
|
1601
|
-
parentSpanId,
|
|
1602
|
-
attributes,
|
|
1603
|
-
createSpanId: () => generateId('span_'),
|
|
1604
|
-
emitTrace: async (cbs, event) => {
|
|
1605
|
-
await this.emitTrace(cbs, event);
|
|
1606
|
-
},
|
|
1607
|
-
});
|
|
1608
|
-
}
|
|
1609
|
-
|
|
1610
|
-
private async endSpan(
|
|
1611
|
-
callbacks: AgentCallbacks | undefined,
|
|
1612
|
-
span: SpanRuntime,
|
|
1613
|
-
attributes?: Record<string, unknown>
|
|
1614
|
-
): Promise<void> {
|
|
1615
|
-
await finishSpan({
|
|
1616
|
-
callbacks,
|
|
1617
|
-
span,
|
|
1618
|
-
attributes,
|
|
1619
|
-
emitTrace: async (cbs, event) => {
|
|
1620
|
-
await this.emitTrace(cbs, event);
|
|
1621
|
-
},
|
|
1622
|
-
});
|
|
1623
|
-
}
|
|
1624
|
-
|
|
1625
|
-
private extractErrorCode(error: unknown): string | undefined {
|
|
1626
|
-
return parseErrorCode(error);
|
|
1627
|
-
}
|
|
1628
|
-
|
|
1629
|
-
private createTimeoutBudgetState(input: AgentInput): TimeoutBudgetState | undefined {
|
|
1630
|
-
return createBudgetState({
|
|
1631
|
-
inputTimeoutBudgetMs: input.timeoutBudgetMs,
|
|
1632
|
-
configTimeoutBudgetMs: this.config.timeoutBudgetMs,
|
|
1633
|
-
inputLlmTimeoutRatio: input.llmTimeoutRatio,
|
|
1634
|
-
configLlmTimeoutRatio: this.config.llmTimeoutRatio,
|
|
1635
|
-
});
|
|
1636
|
-
}
|
|
1637
|
-
|
|
1638
|
-
private createExecutionAbortScope(
|
|
1639
|
-
inputAbortSignal: AbortSignal | undefined,
|
|
1640
|
-
timeoutBudget: TimeoutBudgetState | undefined
|
|
1641
|
-
): AbortScope {
|
|
1642
|
-
return createExecutionBudgetScope(inputAbortSignal, timeoutBudget);
|
|
1643
|
-
}
|
|
1644
|
-
|
|
1645
|
-
private createStageAbortScope(
|
|
1646
|
-
baseSignal: AbortSignal | undefined,
|
|
1647
|
-
timeoutBudget: TimeoutBudgetState | undefined,
|
|
1648
|
-
stage: TimeoutStage
|
|
1649
|
-
): AbortScope {
|
|
1650
|
-
return createStageBudgetScope(baseSignal, timeoutBudget, stage);
|
|
1651
|
-
}
|
|
1652
|
-
|
|
1653
|
-
private timeoutBudgetErrorFromSignal(
|
|
1654
|
-
signal: AbortSignal | undefined
|
|
1655
|
-
): TimeoutBudgetExceededError | undefined {
|
|
1656
|
-
return timeoutErrorFromAbortSignal(signal);
|
|
1657
|
-
}
|
|
1658
|
-
|
|
1659
|
-
private normalizeTimeoutBudgetError(
|
|
1660
|
-
error: unknown,
|
|
1661
|
-
signal: AbortSignal | undefined
|
|
1662
|
-
): TimeoutBudgetExceededError | undefined {
|
|
1663
|
-
return normalizeAbortTimeoutBudgetError(error, signal);
|
|
1664
|
-
}
|
|
1665
|
-
|
|
1666
|
-
private throwIfAborted(signal?: AbortSignal): void {
|
|
1667
|
-
assertNotAborted(signal, ABORTED_MESSAGE);
|
|
1668
|
-
}
|
|
1669
|
-
|
|
1670
|
-
private isAbortError(error: unknown): boolean {
|
|
1671
|
-
return isAbortErrorByMessage(error, ABORTED_MESSAGE);
|
|
1672
|
-
}
|
|
1673
|
-
|
|
1674
|
-
private normalizeError(error: unknown): AgentError {
|
|
1675
|
-
return normalizeAgentError(error, ABORTED_MESSAGE);
|
|
1676
|
-
}
|
|
1677
|
-
|
|
1678
|
-
private calculateRetryDelay(retryCount: number, error: Error): number {
|
|
1679
|
-
return calculateRetryDelayWithBackoff(retryCount, error, this.config.backoffConfig);
|
|
1680
|
-
}
|
|
1681
|
-
|
|
1682
|
-
private async sleep(ms: number, signal?: AbortSignal): Promise<void> {
|
|
1683
|
-
await sleepWithAbort(ms, signal, ABORTED_MESSAGE);
|
|
1684
|
-
}
|
|
1685
|
-
|
|
1686
|
-
private logError(message: string, error: unknown, context?: Record<string, unknown>): void {
|
|
1687
|
-
writeErrorLog(this.logger, message, error, context);
|
|
1688
|
-
}
|
|
1689
|
-
|
|
1690
|
-
private logInfo(message: string, context?: Record<string, unknown>, data?: unknown): void {
|
|
1691
|
-
writeInfoLog(this.logger, message, context, data);
|
|
1692
|
-
}
|
|
1693
|
-
|
|
1694
|
-
private logWarn(message: string, context?: Record<string, unknown>, data?: unknown): void {
|
|
1695
|
-
writeWarnLog(this.logger, message, context, data);
|
|
1696
|
-
}
|
|
1697
|
-
|
|
1698
|
-
private resolveToolResultSummary(
|
|
1699
|
-
toolCall: ToolCall,
|
|
1700
|
-
toolResult: ToolResult,
|
|
1701
|
-
toolOutput: string
|
|
1702
|
-
): string {
|
|
1703
|
-
if (hasNonEmptyText(toolResult.summary)) {
|
|
1704
|
-
return toolResult.summary;
|
|
1705
|
-
}
|
|
1706
|
-
|
|
1707
|
-
const toolName = toolCall.function.name;
|
|
1708
|
-
const subject = toolName === 'bash' ? 'Command' : toolName;
|
|
1709
|
-
|
|
1710
|
-
if (toolResult.success) {
|
|
1711
|
-
if (hasNonEmptyText(toolOutput)) {
|
|
1712
|
-
return `${subject} completed successfully.`;
|
|
1713
|
-
}
|
|
1714
|
-
return `${subject} completed successfully with no output.`;
|
|
1715
|
-
}
|
|
1716
|
-
|
|
1717
|
-
const errorMessage =
|
|
1718
|
-
toolResult.error?.message || (hasNonEmptyText(toolOutput) ? toolOutput : undefined);
|
|
1719
|
-
if (errorMessage) {
|
|
1720
|
-
return `${subject} failed: ${errorMessage}`;
|
|
1721
|
-
}
|
|
1722
|
-
return `${subject} failed.`;
|
|
1723
|
-
}
|
|
1724
|
-
|
|
1725
|
-
private buildToolResultMetadata(
|
|
1726
|
-
record: ToolExecutionLedgerRecord
|
|
1727
|
-
): Record<string, unknown> | undefined {
|
|
1728
|
-
const error: Record<string, unknown> = {};
|
|
1729
|
-
if (record.errorName) {
|
|
1730
|
-
error.name = record.errorName;
|
|
1731
|
-
}
|
|
1732
|
-
if (record.errorMessage) {
|
|
1733
|
-
error.message = record.errorMessage;
|
|
1734
|
-
}
|
|
1735
|
-
if (record.errorCode) {
|
|
1736
|
-
error.code = record.errorCode;
|
|
1737
|
-
}
|
|
1738
|
-
|
|
1739
|
-
const toolResult: Record<string, unknown> = {
|
|
1740
|
-
success: record.success,
|
|
1741
|
-
summary: record.summary,
|
|
1742
|
-
};
|
|
1743
|
-
if (hasNonEmptyText(record.output)) {
|
|
1744
|
-
toolResult.output = record.output;
|
|
1745
|
-
}
|
|
1746
|
-
if (record.payload !== undefined) {
|
|
1747
|
-
toolResult.payload = record.payload;
|
|
1748
|
-
}
|
|
1749
|
-
if (record.metadata && Object.keys(record.metadata).length > 0) {
|
|
1750
|
-
toolResult.metadata = record.metadata;
|
|
1751
|
-
}
|
|
1752
|
-
if (Object.keys(error).length > 0) {
|
|
1753
|
-
toolResult.error = error;
|
|
1754
|
-
}
|
|
1755
|
-
|
|
1756
|
-
return {
|
|
1757
|
-
toolResult,
|
|
1758
|
-
};
|
|
1759
|
-
}
|
|
1760
|
-
|
|
1761
|
-
private createToolResultMessageFromLedger(
|
|
1762
|
-
toolCallId: string,
|
|
1763
|
-
record: ToolExecutionLedgerRecord
|
|
1764
|
-
): Message {
|
|
1765
|
-
return buildToolResultMessage({
|
|
1766
|
-
toolCallId,
|
|
1767
|
-
content: hasNonEmptyText(record.output) ? record.output : record.summary,
|
|
1768
|
-
metadata: this.buildToolResultMetadata(record),
|
|
1769
|
-
createMessageId: () => generateId('msg_'),
|
|
1770
|
-
});
|
|
1771
|
-
}
|
|
1772
|
-
}
|