@tomsun28/pizza 0.0.9 → 0.1.1
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 +20 -143
- package/README.zh-CN.md +36 -0
- package/dist/bun/cli.d.ts +2 -0
- package/dist/bun/cli.js +6 -0
- package/dist/bun/register-bedrock.d.ts +1 -0
- package/dist/bun/register-bedrock.js +3 -0
- package/dist/cli/args.d.ts +48 -0
- package/dist/cli/args.js +330 -0
- package/dist/cli/config-selector.d.ts +13 -0
- package/dist/cli/config-selector.js +30 -0
- package/dist/cli/file-processor.d.ts +14 -0
- package/dist/cli/file-processor.js +82 -0
- package/dist/cli/initial-message.d.ts +17 -0
- package/dist/cli/initial-message.js +21 -0
- package/dist/cli/list-models.d.ts +8 -0
- package/dist/cli/list-models.js +96 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +68 -0
- package/dist/config.d.ts +79 -0
- package/dist/config.js +223 -0
- package/dist/core/agent/index.d.ts +6 -0
- package/dist/core/agent/index.js +6 -0
- package/dist/core/agent/types.d.ts +202 -0
- package/dist/core/agent/types.js +51 -0
- package/dist/core/auth-storage.d.ts +135 -0
- package/dist/core/auth-storage.js +417 -0
- package/dist/core/bash-executor.d.ts +31 -0
- package/dist/core/bash-executor.js +107 -0
- package/dist/core/compaction/branch-summarization.d.ts +87 -0
- package/dist/core/compaction/branch-summarization.js +242 -0
- package/dist/core/compaction/compaction-engine.d.ts +40 -0
- package/dist/core/compaction/compaction-engine.js +292 -0
- package/dist/core/compaction/compaction.d.ts +106 -0
- package/dist/core/compaction/compaction.js +295 -0
- package/dist/core/compaction/index.d.ts +7 -0
- package/dist/core/compaction/index.js +7 -0
- package/dist/core/compaction/utils.d.ts +37 -0
- package/dist/core/compaction/utils.js +152 -0
- package/dist/core/defaults.d.ts +2 -0
- package/dist/core/defaults.js +1 -0
- package/dist/core/diagnostics.d.ts +14 -0
- package/dist/core/diagnostics.js +1 -0
- package/dist/core/event-bus.d.ts +8 -0
- package/dist/core/event-bus.js +24 -0
- package/dist/core/event-store/events.d.ts +465 -0
- package/dist/core/event-store/events.js +6 -0
- package/dist/core/event-store/index.d.ts +10 -0
- package/dist/core/event-store/index.js +10 -0
- package/dist/core/event-store/sqlite-store.d.ts +27 -0
- package/dist/core/event-store/sqlite-store.js +275 -0
- package/dist/core/event-store/store.d.ts +91 -0
- package/dist/core/event-store/store.js +7 -0
- package/dist/core/event-store/thread-scoped-store.d.ts +29 -0
- package/dist/core/event-store/thread-scoped-store.js +50 -0
- package/dist/core/event-store/types.d.ts +70 -0
- package/dist/core/event-store/types.js +7 -0
- package/dist/core/event-store/workspace.d.ts +37 -0
- package/dist/core/event-store/workspace.js +71 -0
- package/dist/core/exec.d.ts +28 -0
- package/dist/core/exec.js +74 -0
- package/dist/core/export-html/ansi-to-html.d.ts +21 -0
- package/dist/core/export-html/ansi-to-html.js +248 -0
- package/dist/core/export-html/index.d.ts +29 -0
- package/dist/core/export-html/index.js +186 -0
- package/dist/core/export-html/template.css +1017 -0
- package/dist/core/export-html/template.html +55 -0
- package/dist/core/export-html/template.js +1742 -0
- package/dist/core/export-html/tool-renderer.d.ts +39 -0
- package/dist/core/export-html/tool-renderer.js +94 -0
- package/dist/core/export-html/vendor/highlight.min.js +1213 -0
- package/dist/core/export-html/vendor/marked.min.js +6 -0
- package/dist/core/extensions/index.d.ts +12 -0
- package/dist/core/extensions/index.js +9 -0
- package/dist/core/extensions/loader.d.ts +24 -0
- package/dist/core/extensions/loader.js +492 -0
- package/dist/core/extensions/runner.d.ts +167 -0
- package/dist/core/extensions/runner.js +898 -0
- package/dist/core/extensions/session-context.d.ts +67 -0
- package/dist/core/extensions/session-context.js +272 -0
- package/dist/core/extensions/types.d.ts +1147 -0
- package/dist/core/extensions/types.js +44 -0
- package/dist/core/extensions/wrapper.d.ts +19 -0
- package/dist/core/extensions/wrapper.js +21 -0
- package/dist/core/footer-data-provider.d.ts +47 -0
- package/dist/core/footer-data-provider.js +321 -0
- package/dist/core/goal/index.d.ts +3 -0
- package/dist/core/goal/index.js +1 -0
- package/dist/core/goal/scheduler.d.ts +83 -0
- package/dist/core/goal/scheduler.js +154 -0
- package/dist/core/goal/types.d.ts +182 -0
- package/dist/core/goal/types.js +11 -0
- package/dist/core/index.d.ts +8 -0
- package/dist/core/index.js +8 -0
- package/dist/core/intent/classifier.d.ts +53 -0
- package/dist/core/intent/classifier.js +353 -0
- package/dist/core/intent/index.d.ts +8 -0
- package/dist/core/intent/index.js +8 -0
- package/dist/core/intent/tool-adapter.d.ts +31 -0
- package/dist/core/intent/tool-adapter.js +168 -0
- package/dist/core/intent/types.d.ts +71 -0
- package/dist/core/intent/types.js +6 -0
- package/dist/core/keybindings.d.ts +297 -0
- package/dist/core/keybindings.js +250 -0
- package/dist/core/messages.d.ts +30 -0
- package/dist/core/messages.js +134 -0
- package/dist/core/model-registry.d.ts +131 -0
- package/dist/core/model-registry.js +634 -0
- package/dist/core/model-resolver.d.ts +109 -0
- package/dist/core/model-resolver.js +486 -0
- package/dist/core/output-guard.d.ts +5 -0
- package/dist/core/output-guard.js +58 -0
- package/dist/core/package-manager.d.ts +196 -0
- package/dist/core/package-manager.js +1873 -0
- package/dist/core/projection/boundary-inferrer.d.ts +58 -0
- package/dist/core/projection/boundary-inferrer.js +122 -0
- package/dist/core/projection/event-to-message.d.ts +35 -0
- package/dist/core/projection/event-to-message.js +253 -0
- package/dist/core/projection/goal-projection.d.ts +67 -0
- package/dist/core/projection/goal-projection.js +339 -0
- package/dist/core/projection/index.d.ts +13 -0
- package/dist/core/projection/index.js +13 -0
- package/dist/core/projection/session-manager.d.ts +100 -0
- package/dist/core/projection/session-manager.js +331 -0
- package/dist/core/projection/session-projection.d.ts +58 -0
- package/dist/core/projection/session-projection.js +257 -0
- package/dist/core/projection/task-history.d.ts +13 -0
- package/dist/core/projection/task-history.js +61 -0
- package/dist/core/projection/timeline-projection.d.ts +82 -0
- package/dist/core/projection/timeline-projection.js +276 -0
- package/dist/core/projection/types.d.ts +69 -0
- package/dist/core/projection/types.js +6 -0
- package/dist/core/prompt-templates.d.ts +51 -0
- package/dist/core/prompt-templates.js +249 -0
- package/dist/core/resolve-config-value.d.ts +22 -0
- package/dist/core/resolve-config-value.js +125 -0
- package/dist/core/resource-loader.d.ts +193 -0
- package/dist/core/resource-loader.js +685 -0
- package/dist/core/runtime/ai-client.d.ts +12 -0
- package/dist/core/runtime/ai-client.js +153 -0
- package/dist/core/runtime/checkpoint.d.ts +12 -0
- package/dist/core/runtime/checkpoint.js +78 -0
- package/dist/core/runtime/index.d.ts +12 -0
- package/dist/core/runtime/index.js +12 -0
- package/dist/core/runtime/llm-types.d.ts +99 -0
- package/dist/core/runtime/llm-types.js +6 -0
- package/dist/core/runtime/local-runtime.d.ts +23 -0
- package/dist/core/runtime/local-runtime.js +59 -0
- package/dist/core/runtime/policies.d.ts +72 -0
- package/dist/core/runtime/policies.js +37 -0
- package/dist/core/runtime/reactor.d.ts +213 -0
- package/dist/core/runtime/reactor.js +992 -0
- package/dist/core/runtime/runtime.d.ts +191 -0
- package/dist/core/runtime/runtime.js +449 -0
- package/dist/core/runtime/types.d.ts +42 -0
- package/dist/core/runtime/types.js +1 -0
- package/dist/core/sdk.d.ts +12 -0
- package/dist/core/sdk.js +10 -0
- package/dist/core/session-context-builder.d.ts +9 -0
- package/dist/core/session-context-builder.js +87 -0
- package/dist/core/session-cwd.d.ts +18 -0
- package/dist/core/session-cwd.js +36 -0
- package/dist/core/session-facade-factory.d.ts +79 -0
- package/dist/core/session-facade-factory.js +460 -0
- package/dist/core/session-facade.d.ts +57 -0
- package/dist/core/session-facade.js +91 -0
- package/dist/core/session-listing.d.ts +30 -0
- package/dist/core/session-listing.js +168 -0
- package/dist/core/session-reader.d.ts +19 -0
- package/dist/core/session-reader.js +101 -0
- package/dist/core/session-ref.d.ts +7 -0
- package/dist/core/session-ref.js +25 -0
- package/dist/core/session-services.d.ts +51 -0
- package/dist/core/session-services.js +90 -0
- package/dist/core/session-stats.d.ts +43 -0
- package/dist/core/session-stats.js +41 -0
- package/dist/core/settings-manager.d.ts +236 -0
- package/dist/core/settings-manager.js +706 -0
- package/dist/core/skill-block-parser.d.ts +12 -0
- package/dist/core/skill-block-parser.js +15 -0
- package/dist/core/skills.d.ts +59 -0
- package/dist/core/skills.js +408 -0
- package/dist/core/slash-commands.d.ts +13 -0
- package/dist/core/slash-commands.js +17 -0
- package/dist/core/source-info.d.ts +17 -0
- package/dist/core/source-info.js +18 -0
- package/dist/core/system-prompt.d.ts +27 -0
- package/dist/core/system-prompt.js +179 -0
- package/dist/core/timings.d.ts +7 -0
- package/dist/core/timings.js +30 -0
- package/dist/core/tools/bash.d.ts +95 -0
- package/dist/core/tools/bash.js +513 -0
- package/dist/core/tools/builtin-commands.d.ts +75 -0
- package/dist/core/tools/builtin-commands.js +503 -0
- package/dist/core/tools/compat-commands.d.ts +12 -0
- package/dist/core/tools/compat-commands.js +1280 -0
- package/dist/core/tools/edit-diff.d.ts +59 -0
- package/dist/core/tools/edit-diff.js +263 -0
- package/dist/core/tools/edit.d.ts +49 -0
- package/dist/core/tools/edit.js +340 -0
- package/dist/core/tools/file-mutation-queue.d.ts +5 -0
- package/dist/core/tools/file-mutation-queue.js +36 -0
- package/dist/core/tools/find.d.ts +34 -0
- package/dist/core/tools/find.js +459 -0
- package/dist/core/tools/grep.d.ts +36 -0
- package/dist/core/tools/grep.js +422 -0
- package/dist/core/tools/index.d.ts +70 -0
- package/dist/core/tools/index.js +129 -0
- package/dist/core/tools/line-anchors.d.ts +33 -0
- package/dist/core/tools/line-anchors.js +151 -0
- package/dist/core/tools/ls.d.ts +36 -0
- package/dist/core/tools/ls.js +268 -0
- package/dist/core/tools/path-utils.d.ts +7 -0
- package/dist/core/tools/path-utils.js +80 -0
- package/dist/core/tools/read.d.ts +35 -0
- package/dist/core/tools/read.js +244 -0
- package/dist/core/tools/render-utils.d.ts +20 -0
- package/dist/core/tools/render-utils.js +48 -0
- package/dist/core/tools/session-split.d.ts +9 -0
- package/dist/core/tools/session-split.js +77 -0
- package/dist/core/tools/tool-definition-wrapper.d.ts +13 -0
- package/dist/core/tools/tool-definition-wrapper.js +32 -0
- package/dist/core/tools/truncate.d.ts +69 -0
- package/dist/core/tools/truncate.js +204 -0
- package/dist/core/tools/write.d.ts +25 -0
- package/dist/core/tools/write.js +211 -0
- package/dist/core/types/session-types.d.ts +123 -0
- package/dist/core/types/session-types.js +8 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +38 -0
- package/dist/main.d.ts +11 -2
- package/dist/main.js +577 -149
- package/dist/modes/event-mapper.d.ts +115 -0
- package/dist/modes/event-mapper.js +200 -0
- package/dist/modes/gui/server.d.ts +14 -0
- package/dist/modes/gui/server.js +1032 -0
- package/dist/modes/index.d.ts +10 -0
- package/dist/modes/index.js +9 -0
- package/dist/modes/interactive/assets/clankolas.png +0 -0
- package/dist/modes/interactive/components/armin.d.ts +33 -0
- package/dist/modes/interactive/components/armin.js +328 -0
- package/dist/modes/interactive/components/assistant-message.d.ts +19 -0
- package/dist/modes/interactive/components/assistant-message.js +115 -0
- package/dist/modes/interactive/components/bash-execution.d.ts +33 -0
- package/dist/modes/interactive/components/bash-execution.js +169 -0
- package/dist/modes/interactive/components/bordered-loader.d.ts +15 -0
- package/dist/modes/interactive/components/bordered-loader.js +50 -0
- package/dist/modes/interactive/components/branch-summary-message.d.ts +15 -0
- package/dist/modes/interactive/components/branch-summary-message.js +41 -0
- package/dist/modes/interactive/components/brand-header.d.ts +33 -0
- package/dist/modes/interactive/components/brand-header.js +235 -0
- package/dist/modes/interactive/components/compaction-summary-message.d.ts +15 -0
- package/dist/modes/interactive/components/compaction-summary-message.js +42 -0
- package/dist/modes/interactive/components/config-selector.d.ts +70 -0
- package/dist/modes/interactive/components/config-selector.js +469 -0
- package/dist/modes/interactive/components/countdown-timer.d.ts +13 -0
- package/dist/modes/interactive/components/countdown-timer.js +27 -0
- package/dist/modes/interactive/components/custom-editor.d.ts +27 -0
- package/dist/modes/interactive/components/custom-editor.js +94 -0
- package/dist/modes/interactive/components/custom-message.d.ts +19 -0
- package/dist/modes/interactive/components/custom-message.js +73 -0
- package/dist/modes/interactive/components/daxnuts.d.ts +22 -0
- package/dist/modes/interactive/components/daxnuts.js +137 -0
- package/dist/modes/interactive/components/diff.d.ts +11 -0
- package/dist/modes/interactive/components/diff.js +132 -0
- package/dist/modes/interactive/components/dynamic-border.d.ts +14 -0
- package/dist/modes/interactive/components/dynamic-border.js +19 -0
- package/dist/modes/interactive/components/earendil-announcement.d.ts +4 -0
- package/dist/modes/interactive/components/earendil-announcement.js +39 -0
- package/dist/modes/interactive/components/extension-editor.d.ts +19 -0
- package/dist/modes/interactive/components/extension-editor.js +105 -0
- package/dist/modes/interactive/components/extension-input.d.ts +22 -0
- package/dist/modes/interactive/components/extension-input.js +54 -0
- package/dist/modes/interactive/components/extension-selector.d.ts +23 -0
- package/dist/modes/interactive/components/extension-selector.js +70 -0
- package/dist/modes/interactive/components/footer.d.ts +55 -0
- package/dist/modes/interactive/components/footer.js +207 -0
- package/dist/modes/interactive/components/index.d.ts +31 -0
- package/dist/modes/interactive/components/index.js +32 -0
- package/dist/modes/interactive/components/keybinding-hints.d.ts +7 -0
- package/dist/modes/interactive/components/keybinding-hints.js +21 -0
- package/dist/modes/interactive/components/login-dialog.d.ts +41 -0
- package/dist/modes/interactive/components/login-dialog.js +138 -0
- package/dist/modes/interactive/components/model-selector.d.ts +32 -0
- package/dist/modes/interactive/components/model-selector.js +209 -0
- package/dist/modes/interactive/components/oauth-selector.d.ts +18 -0
- package/dist/modes/interactive/components/oauth-selector.js +91 -0
- package/dist/modes/interactive/components/settings-selector.d.ts +57 -0
- package/dist/modes/interactive/components/settings-selector.js +299 -0
- package/dist/modes/interactive/components/show-images-selector.d.ts +9 -0
- package/dist/modes/interactive/components/show-images-selector.js +37 -0
- package/dist/modes/interactive/components/skill-invocation-message.d.ts +16 -0
- package/dist/modes/interactive/components/skill-invocation-message.js +44 -0
- package/dist/modes/interactive/components/theme-selector.d.ts +10 -0
- package/dist/modes/interactive/components/theme-selector.js +47 -0
- package/dist/modes/interactive/components/thinking-selector.d.ts +10 -0
- package/dist/modes/interactive/components/thinking-selector.js +50 -0
- package/dist/modes/interactive/components/timeline-view.d.ts +31 -0
- package/dist/modes/interactive/components/timeline-view.js +81 -0
- package/dist/modes/interactive/components/tool-execution.d.ts +64 -0
- package/dist/modes/interactive/components/tool-execution.js +305 -0
- package/dist/modes/interactive/components/tree-selector.d.ts +88 -0
- package/dist/modes/interactive/components/tree-selector.js +1072 -0
- package/dist/modes/interactive/components/user-message-selector.d.ts +29 -0
- package/dist/modes/interactive/components/user-message-selector.js +110 -0
- package/dist/modes/interactive/components/user-message.d.ts +9 -0
- package/dist/modes/interactive/components/user-message.js +27 -0
- package/dist/modes/interactive/components/visual-truncate.d.ts +23 -0
- package/dist/modes/interactive/components/visual-truncate.js +32 -0
- package/dist/modes/interactive/interactive-mode.d.ts +394 -0
- package/dist/modes/interactive/interactive-mode.js +4116 -0
- package/dist/modes/interactive/theme/dark.json +85 -0
- package/dist/modes/interactive/theme/light.json +84 -0
- package/dist/modes/interactive/theme/theme-schema.json +335 -0
- package/dist/modes/interactive/theme/theme.d.ts +80 -0
- package/dist/modes/interactive/theme/theme.js +973 -0
- package/dist/modes/print-mode.d.ts +27 -0
- package/dist/modes/print-mode.js +102 -0
- package/dist/modes/rpc/jsonl.d.ts +16 -0
- package/dist/modes/rpc/jsonl.js +48 -0
- package/dist/modes/rpc/rpc-client.d.ts +219 -0
- package/dist/modes/rpc/rpc-client.js +405 -0
- package/dist/modes/rpc/rpc-mode.d.ts +19 -0
- package/dist/modes/rpc/rpc-mode.js +381 -0
- package/dist/modes/rpc/rpc-types.d.ts +406 -0
- package/dist/modes/rpc/rpc-types.js +7 -0
- package/dist/package-manager-cli.d.ts +3 -0
- package/dist/package-manager-cli.js +233 -0
- package/dist/utils/changelog.d.ts +20 -0
- package/dist/utils/changelog.js +86 -0
- package/dist/utils/child-process.d.ts +10 -0
- package/dist/utils/child-process.js +77 -0
- package/dist/utils/clipboard-image.d.ts +10 -0
- package/dist/utils/clipboard-image.js +244 -0
- package/dist/utils/clipboard-native.d.ts +7 -0
- package/dist/utils/clipboard-native.js +13 -0
- package/dist/utils/clipboard.d.ts +1 -0
- package/dist/utils/clipboard.js +77 -0
- package/dist/utils/exif-orientation.d.ts +4 -0
- package/dist/utils/exif-orientation.js +157 -0
- package/dist/utils/frontmatter.d.ts +7 -0
- package/dist/utils/frontmatter.js +25 -0
- package/dist/utils/git.d.ts +25 -0
- package/dist/utils/git.js +162 -0
- package/dist/utils/image-convert.d.ts +8 -0
- package/dist/utils/image-convert.js +38 -0
- package/dist/utils/image-resize.d.ts +35 -0
- package/dist/utils/image-resize.js +136 -0
- package/dist/utils/mime.d.ts +1 -0
- package/dist/utils/mime.js +25 -0
- package/dist/utils/path-shims.d.ts +1 -0
- package/dist/utils/path-shims.js +90 -0
- package/dist/utils/paths.d.ts +6 -0
- package/dist/utils/paths.js +18 -0
- package/dist/utils/photon.d.ts +20 -0
- package/dist/utils/photon.js +120 -0
- package/dist/utils/shell.d.ts +29 -0
- package/dist/utils/shell.js +189 -0
- package/dist/utils/sleep.d.ts +4 -0
- package/dist/utils/sleep.js +16 -0
- package/dist/utils/tools-manager.d.ts +3 -0
- package/dist/utils/tools-manager.js +101 -0
- package/dist/vendor/bin/darwin-arm64/fd +0 -0
- package/dist/vendor/bin/darwin-arm64/rg +0 -0
- package/dist/vendor/bin/darwin-x64/fd +0 -0
- package/dist/vendor/bin/darwin-x64/rg +0 -0
- package/dist/vendor/bin/linux-arm64/fd +0 -0
- package/dist/vendor/bin/linux-arm64/rg +0 -0
- package/dist/vendor/bin/linux-x64/fd +0 -0
- package/dist/vendor/bin/linux-x64/rg +0 -0
- package/dist/vendor/bin/win32-arm64/fd.exe +0 -0
- package/dist/vendor/bin/win32-arm64/rg.exe +0 -0
- package/dist/vendor/bin/win32-x64/fd.exe +0 -0
- package/dist/vendor/bin/win32-x64/rg.exe +0 -0
- package/package.json +90 -26
- package/dist/extension/core.d.ts +0 -69
- package/dist/extension/core.js +0 -270
- package/dist/extension/core.js.map +0 -1
- package/dist/extension/index.d.ts +0 -25
- package/dist/extension/index.js +0 -303
- package/dist/extension/index.js.map +0 -1
- package/dist/main.js.map +0 -1
- package/themes/pizza.json +0 -87
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
function getStreamToolCall(event) {
|
|
2
|
+
const direct = event.toolCall;
|
|
3
|
+
if (direct?.type === "toolCall")
|
|
4
|
+
return direct;
|
|
5
|
+
const contentIndex = event.contentIndex;
|
|
6
|
+
const partialContent = event.partial?.content;
|
|
7
|
+
const partialBlock = Array.isArray(partialContent) && typeof contentIndex === "number" ? partialContent[contentIndex] : undefined;
|
|
8
|
+
return partialBlock?.type === "toolCall" ? partialBlock : undefined;
|
|
9
|
+
}
|
|
10
|
+
/** Build an EventSourcedRuntime LLMClient around the configured AI stream function. */
|
|
11
|
+
export function buildLlmClientFromStreamFn(model, streamFn, options) {
|
|
12
|
+
return {
|
|
13
|
+
async complete({ messages, systemPrompt, tools, onChunk, signal }) {
|
|
14
|
+
const apiKey = options?.getApiKey ? await options.getApiKey(model.provider) : undefined;
|
|
15
|
+
const streamTools = tools?.map((tool) => ({
|
|
16
|
+
name: tool.name,
|
|
17
|
+
description: tool.description ?? "",
|
|
18
|
+
parameters: tool.input_schema,
|
|
19
|
+
}));
|
|
20
|
+
const stream = await streamFn(model, {
|
|
21
|
+
systemPrompt: systemPrompt ?? "",
|
|
22
|
+
messages: messages.filter((message) => message.role === "user" || message.role === "assistant" || message.role === "toolResult").map((message) => {
|
|
23
|
+
if (message.role !== "toolResult") {
|
|
24
|
+
return message;
|
|
25
|
+
}
|
|
26
|
+
const toolResult = message;
|
|
27
|
+
return {
|
|
28
|
+
role: "toolResult",
|
|
29
|
+
toolCallId: toolResult.toolCallId,
|
|
30
|
+
toolName: toolResult.toolName,
|
|
31
|
+
content: toolResult.content,
|
|
32
|
+
isError: toolResult.isError,
|
|
33
|
+
timestamp: toolResult.timestamp,
|
|
34
|
+
};
|
|
35
|
+
}),
|
|
36
|
+
tools: streamTools,
|
|
37
|
+
}, {
|
|
38
|
+
apiKey,
|
|
39
|
+
signal,
|
|
40
|
+
onPayload: options?.onPayload,
|
|
41
|
+
onResponse: options?.onResponse,
|
|
42
|
+
thinkingBudgets: options?.thinkingBudgets,
|
|
43
|
+
transport: options?.transport,
|
|
44
|
+
});
|
|
45
|
+
let finalMessage;
|
|
46
|
+
for await (const event of stream) {
|
|
47
|
+
switch (event.type) {
|
|
48
|
+
case "start":
|
|
49
|
+
break;
|
|
50
|
+
case "text_start":
|
|
51
|
+
onChunk?.({ kind: "text_start", contentIndex: event.contentIndex });
|
|
52
|
+
break;
|
|
53
|
+
case "text_delta":
|
|
54
|
+
onChunk?.({ kind: "text_delta", contentIndex: event.contentIndex, delta: event.delta });
|
|
55
|
+
break;
|
|
56
|
+
case "text_end":
|
|
57
|
+
onChunk?.({
|
|
58
|
+
kind: "text_end",
|
|
59
|
+
contentIndex: event.contentIndex,
|
|
60
|
+
content: event.content,
|
|
61
|
+
});
|
|
62
|
+
break;
|
|
63
|
+
case "thinking_start":
|
|
64
|
+
onChunk?.({ kind: "thinking_start", contentIndex: event.contentIndex });
|
|
65
|
+
break;
|
|
66
|
+
case "thinking_delta":
|
|
67
|
+
onChunk?.({
|
|
68
|
+
kind: "thinking_delta",
|
|
69
|
+
contentIndex: event.contentIndex,
|
|
70
|
+
delta: event.delta,
|
|
71
|
+
});
|
|
72
|
+
break;
|
|
73
|
+
case "thinking_end":
|
|
74
|
+
onChunk?.({
|
|
75
|
+
kind: "thinking_end",
|
|
76
|
+
contentIndex: event.contentIndex,
|
|
77
|
+
content: event.content,
|
|
78
|
+
});
|
|
79
|
+
break;
|
|
80
|
+
case "toolcall_start": {
|
|
81
|
+
const toolCall = getStreamToolCall(event);
|
|
82
|
+
onChunk?.({
|
|
83
|
+
kind: "toolcall_start",
|
|
84
|
+
contentIndex: event.contentIndex,
|
|
85
|
+
tool_call_id: event.id ?? toolCall?.id ?? "",
|
|
86
|
+
tool_name: event.toolName ?? toolCall?.name ?? "",
|
|
87
|
+
});
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
case "toolcall_delta":
|
|
91
|
+
onChunk?.({ kind: "toolcall_delta", contentIndex: event.contentIndex, delta: event.delta });
|
|
92
|
+
break;
|
|
93
|
+
case "toolcall_end": {
|
|
94
|
+
const toolCall = getStreamToolCall(event);
|
|
95
|
+
onChunk?.({
|
|
96
|
+
kind: "toolcall_end",
|
|
97
|
+
contentIndex: event.contentIndex,
|
|
98
|
+
tool_call_id: event.id ?? toolCall?.id ?? "",
|
|
99
|
+
tool_name: event.toolName ?? toolCall?.name ?? "",
|
|
100
|
+
arguments: toolCall?.arguments ?? {},
|
|
101
|
+
});
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
case "done":
|
|
105
|
+
case "error":
|
|
106
|
+
finalMessage = await stream.result();
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
finalMessage ??= await stream.result();
|
|
111
|
+
if (!finalMessage) {
|
|
112
|
+
throw new Error("Stream ended without a terminal event");
|
|
113
|
+
}
|
|
114
|
+
const content = finalMessage.content.map((block) => {
|
|
115
|
+
if (block.type === "toolCall") {
|
|
116
|
+
return {
|
|
117
|
+
type: "tool_call",
|
|
118
|
+
id: block.id,
|
|
119
|
+
name: block.name,
|
|
120
|
+
arguments: block.arguments,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
return block;
|
|
124
|
+
});
|
|
125
|
+
return {
|
|
126
|
+
content,
|
|
127
|
+
provider: finalMessage.provider,
|
|
128
|
+
model: finalMessage.model,
|
|
129
|
+
usage: {
|
|
130
|
+
input: finalMessage.usage.input,
|
|
131
|
+
output: finalMessage.usage.output,
|
|
132
|
+
cache_read: finalMessage.usage.cacheRead,
|
|
133
|
+
cache_write: finalMessage.usage.cacheWrite,
|
|
134
|
+
total: finalMessage.usage.totalTokens,
|
|
135
|
+
cost: finalMessage.usage.cost.total,
|
|
136
|
+
},
|
|
137
|
+
stopReason: content.some((block) => block.type === "tool_call")
|
|
138
|
+
? "tool_use"
|
|
139
|
+
: finalMessage.stopReason === "toolUse"
|
|
140
|
+
? "tool_use"
|
|
141
|
+
: finalMessage.stopReason,
|
|
142
|
+
errorMessage: finalMessage.errorMessage,
|
|
143
|
+
};
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
export function toModelConfig(model, thinkingLevel) {
|
|
148
|
+
return {
|
|
149
|
+
provider: model.provider,
|
|
150
|
+
model_id: model.id,
|
|
151
|
+
thinking_level: thinkingLevel,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { CheckpointRef, CheckpointRequest } from "./types.js";
|
|
2
|
+
export interface LocalCheckpointServiceOptions {
|
|
3
|
+
workspace_id: string;
|
|
4
|
+
runtime_id: string;
|
|
5
|
+
checkpointDir: string;
|
|
6
|
+
}
|
|
7
|
+
export declare class LocalCheckpointService {
|
|
8
|
+
private options;
|
|
9
|
+
constructor(options: LocalCheckpointServiceOptions);
|
|
10
|
+
create(request: CheckpointRequest): CheckpointRef;
|
|
11
|
+
restore(ref: CheckpointRef): void;
|
|
12
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join, relative, resolve } from "node:path";
|
|
4
|
+
const IGNORED_DIRS = new Set([".git", "node_modules", "dist", ".pizza", ".agents"]);
|
|
5
|
+
export class LocalCheckpointService {
|
|
6
|
+
constructor(options) {
|
|
7
|
+
this.options = options;
|
|
8
|
+
mkdirSync(this.options.checkpointDir, { recursive: true });
|
|
9
|
+
}
|
|
10
|
+
create(request) {
|
|
11
|
+
const createdAt = Date.now();
|
|
12
|
+
const checkpointId = `ckpt_${createdAt.toString(36)}_${randomUUID().slice(0, 8)}`;
|
|
13
|
+
const path = join(this.options.checkpointDir, `${checkpointId}.json`);
|
|
14
|
+
const files = collectFileManifest(request.cwd);
|
|
15
|
+
const workspaceHash = createHash("sha256")
|
|
16
|
+
.update(files.map((file) => `${file.path}:${file.hash}:${file.size}`).join("\n"))
|
|
17
|
+
.digest("hex");
|
|
18
|
+
const manifest = {
|
|
19
|
+
checkpoint_id: checkpointId,
|
|
20
|
+
path,
|
|
21
|
+
created_at: createdAt,
|
|
22
|
+
event_head: request.event_head,
|
|
23
|
+
event_head_sequence: request.event_head_sequence,
|
|
24
|
+
label: request.label,
|
|
25
|
+
workspace_id: this.options.workspace_id,
|
|
26
|
+
runtime_id: this.options.runtime_id,
|
|
27
|
+
cwd: resolve(request.cwd),
|
|
28
|
+
workspace_hash: workspaceHash,
|
|
29
|
+
files,
|
|
30
|
+
};
|
|
31
|
+
writeFileSync(path, `${JSON.stringify(manifest, null, 2)}\n`);
|
|
32
|
+
return {
|
|
33
|
+
checkpoint_id: manifest.checkpoint_id,
|
|
34
|
+
path: manifest.path,
|
|
35
|
+
created_at: manifest.created_at,
|
|
36
|
+
event_head: manifest.event_head,
|
|
37
|
+
event_head_sequence: manifest.event_head_sequence,
|
|
38
|
+
label: manifest.label,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
restore(ref) {
|
|
42
|
+
if (!existsSync(ref.path)) {
|
|
43
|
+
throw new Error(`Checkpoint not found: ${ref.path}`);
|
|
44
|
+
}
|
|
45
|
+
JSON.parse(readFileSync(ref.path, "utf8"));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function collectFileManifest(cwd) {
|
|
49
|
+
const root = resolve(cwd);
|
|
50
|
+
const files = [];
|
|
51
|
+
collect(root, root, files);
|
|
52
|
+
files.sort((a, b) => a.path.localeCompare(b.path));
|
|
53
|
+
return files;
|
|
54
|
+
}
|
|
55
|
+
function collect(root, current, files) {
|
|
56
|
+
for (const entry of readdirSync(current, { withFileTypes: true })) {
|
|
57
|
+
if (entry.name.startsWith(".") && entry.name !== ".env" && entry.name !== ".gitignore") {
|
|
58
|
+
if (entry.isDirectory() || IGNORED_DIRS.has(entry.name))
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (entry.isDirectory() && IGNORED_DIRS.has(entry.name))
|
|
62
|
+
continue;
|
|
63
|
+
const fullPath = join(current, entry.name);
|
|
64
|
+
if (entry.isDirectory()) {
|
|
65
|
+
collect(root, fullPath, files);
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (!entry.isFile())
|
|
69
|
+
continue;
|
|
70
|
+
const stat = statSync(fullPath);
|
|
71
|
+
const content = readFileSync(fullPath);
|
|
72
|
+
files.push({
|
|
73
|
+
path: relative(root, fullPath).replace(/\\/g, "/"),
|
|
74
|
+
hash: createHash("sha256").update(content).digest("hex"),
|
|
75
|
+
size: stat.size,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime Module
|
|
3
|
+
*
|
|
4
|
+
* Event-driven (reactor-based) runtime implementation.
|
|
5
|
+
*/
|
|
6
|
+
export * from "./reactor.js";
|
|
7
|
+
export * from "./policies.js";
|
|
8
|
+
export * from "./llm-types.js";
|
|
9
|
+
export * from "./runtime.js";
|
|
10
|
+
export * from "./types.js";
|
|
11
|
+
export * from "./local-runtime.js";
|
|
12
|
+
export * from "./checkpoint.js";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime Module
|
|
3
|
+
*
|
|
4
|
+
* Event-driven (reactor-based) runtime implementation.
|
|
5
|
+
*/
|
|
6
|
+
export * from "./reactor.js";
|
|
7
|
+
export * from "./policies.js";
|
|
8
|
+
export * from "./llm-types.js";
|
|
9
|
+
export * from "./runtime.js";
|
|
10
|
+
export * from "./types.js";
|
|
11
|
+
export * from "./local-runtime.js";
|
|
12
|
+
export * from "./checkpoint.js";
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM Client Types
|
|
3
|
+
*
|
|
4
|
+
* Shared types for the LLM client interface used by the reactor.
|
|
5
|
+
*/
|
|
6
|
+
import type { ContentBlock } from "../event-store/types.js";
|
|
7
|
+
import type { AgentMessage } from "../agent/types.js";
|
|
8
|
+
/** Model configuration */
|
|
9
|
+
export interface ModelConfig {
|
|
10
|
+
provider: string;
|
|
11
|
+
model_id: string;
|
|
12
|
+
thinking_level?: string;
|
|
13
|
+
}
|
|
14
|
+
/** Tool definition for LLM */
|
|
15
|
+
export interface ToolDefinition {
|
|
16
|
+
name: string;
|
|
17
|
+
description?: string;
|
|
18
|
+
input_schema: Record<string, unknown>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Streaming chunk emitted by an LLM client during a call.
|
|
22
|
+
*
|
|
23
|
+
* Producers map provider-specific streaming events onto this minimal shape.
|
|
24
|
+
* Consumers (the reactor) translate each chunk into AGENT_MESSAGE_CHUNK events
|
|
25
|
+
* appended to the EventStore.
|
|
26
|
+
*/
|
|
27
|
+
export type LLMChunk = {
|
|
28
|
+
kind: "text_start";
|
|
29
|
+
contentIndex: number;
|
|
30
|
+
} | {
|
|
31
|
+
kind: "text_delta";
|
|
32
|
+
contentIndex: number;
|
|
33
|
+
delta: string;
|
|
34
|
+
} | {
|
|
35
|
+
kind: "text_end";
|
|
36
|
+
contentIndex: number;
|
|
37
|
+
content: string;
|
|
38
|
+
} | {
|
|
39
|
+
kind: "thinking_start";
|
|
40
|
+
contentIndex: number;
|
|
41
|
+
} | {
|
|
42
|
+
kind: "thinking_delta";
|
|
43
|
+
contentIndex: number;
|
|
44
|
+
delta: string;
|
|
45
|
+
} | {
|
|
46
|
+
kind: "thinking_end";
|
|
47
|
+
contentIndex: number;
|
|
48
|
+
content: string;
|
|
49
|
+
} | {
|
|
50
|
+
kind: "toolcall_start";
|
|
51
|
+
contentIndex: number;
|
|
52
|
+
tool_call_id: string;
|
|
53
|
+
tool_name: string;
|
|
54
|
+
} | {
|
|
55
|
+
kind: "toolcall_delta";
|
|
56
|
+
contentIndex: number;
|
|
57
|
+
delta: string;
|
|
58
|
+
} | {
|
|
59
|
+
kind: "toolcall_end";
|
|
60
|
+
contentIndex: number;
|
|
61
|
+
tool_call_id: string;
|
|
62
|
+
tool_name: string;
|
|
63
|
+
arguments: unknown;
|
|
64
|
+
};
|
|
65
|
+
/** LLM client interface */
|
|
66
|
+
export interface LLMClient {
|
|
67
|
+
complete(request: {
|
|
68
|
+
messages: AgentMessage[];
|
|
69
|
+
systemPrompt?: string;
|
|
70
|
+
model: ModelConfig;
|
|
71
|
+
tools?: ToolDefinition[];
|
|
72
|
+
/**
|
|
73
|
+
* Optional streaming callback. Fires once per LLM chunk while the call
|
|
74
|
+
* is in flight. Reactors use this to emit AGENT_MESSAGE_CHUNK events to
|
|
75
|
+
* the EventStore for live UI rendering.
|
|
76
|
+
*
|
|
77
|
+
* Clients that cannot stream may simply not call this.
|
|
78
|
+
*/
|
|
79
|
+
onChunk?: (chunk: LLMChunk) => void;
|
|
80
|
+
/** Abort signal for in-flight requests. */
|
|
81
|
+
signal?: AbortSignal;
|
|
82
|
+
}): Promise<LLMResponse>;
|
|
83
|
+
}
|
|
84
|
+
/** LLM response */
|
|
85
|
+
export interface LLMResponse {
|
|
86
|
+
content: ContentBlock[];
|
|
87
|
+
provider: string;
|
|
88
|
+
model: string;
|
|
89
|
+
usage: {
|
|
90
|
+
input: number;
|
|
91
|
+
output: number;
|
|
92
|
+
cache_read: number;
|
|
93
|
+
cache_write: number;
|
|
94
|
+
total: number;
|
|
95
|
+
cost: number;
|
|
96
|
+
};
|
|
97
|
+
stopReason: "stop" | "tool_use" | "length" | "error" | "aborted";
|
|
98
|
+
errorMessage?: string;
|
|
99
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ToolExecutionResult, ToolRegistry } from "../intent/types.js";
|
|
2
|
+
import type { CheckpointRef, CheckpointRequest, RuntimeAdapter, RuntimeKind, RuntimeStatus, ToolExecutionRequest } from "./types.js";
|
|
3
|
+
export interface LocalRuntimeAdapterOptions {
|
|
4
|
+
runtime_id?: string;
|
|
5
|
+
workspace_id: string;
|
|
6
|
+
cwd: string;
|
|
7
|
+
agentDir?: string;
|
|
8
|
+
toolRegistry: ToolRegistry;
|
|
9
|
+
}
|
|
10
|
+
export declare class LocalRuntimeAdapter implements RuntimeAdapter {
|
|
11
|
+
private options;
|
|
12
|
+
readonly runtime_id: string;
|
|
13
|
+
readonly workspace_id: string;
|
|
14
|
+
readonly kind: RuntimeKind;
|
|
15
|
+
private checkpointService;
|
|
16
|
+
private status;
|
|
17
|
+
constructor(options: LocalRuntimeAdapterOptions);
|
|
18
|
+
executeTool(request: ToolExecutionRequest): Promise<ToolExecutionResult>;
|
|
19
|
+
createCheckpoint(request: CheckpointRequest): Promise<CheckpointRef>;
|
|
20
|
+
restoreCheckpoint(ref: CheckpointRef): Promise<void>;
|
|
21
|
+
getStatus(): Promise<RuntimeStatus>;
|
|
22
|
+
private _getCheckpointService;
|
|
23
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { getWorkspaceDir } from "../event-store/workspace.js";
|
|
3
|
+
import { LocalCheckpointService } from "./checkpoint.js";
|
|
4
|
+
export class LocalRuntimeAdapter {
|
|
5
|
+
constructor(options) {
|
|
6
|
+
this.options = options;
|
|
7
|
+
this.kind = "local";
|
|
8
|
+
this.status = "idle";
|
|
9
|
+
this.runtime_id = options.runtime_id ?? "local_runtime";
|
|
10
|
+
this.workspace_id = options.workspace_id;
|
|
11
|
+
}
|
|
12
|
+
async executeTool(request) {
|
|
13
|
+
const tool = this.options.toolRegistry.get(request.tool_name);
|
|
14
|
+
if (!tool) {
|
|
15
|
+
return {
|
|
16
|
+
content: [{ type: "text", text: `Unknown tool: ${request.tool_name}` }],
|
|
17
|
+
is_error: true,
|
|
18
|
+
error_message: `Unknown tool: ${request.tool_name}`,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
this.status = "running";
|
|
22
|
+
try {
|
|
23
|
+
return await tool.execute(request.arguments, {
|
|
24
|
+
tool_call_id: request.tool_call_id,
|
|
25
|
+
signal: request.signal,
|
|
26
|
+
onUpdate: request.onUpdate,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
finally {
|
|
30
|
+
this.status = "idle";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
async createCheckpoint(request) {
|
|
34
|
+
return this._getCheckpointService().create(request);
|
|
35
|
+
}
|
|
36
|
+
async restoreCheckpoint(ref) {
|
|
37
|
+
this._getCheckpointService().restore(ref);
|
|
38
|
+
}
|
|
39
|
+
async getStatus() {
|
|
40
|
+
return {
|
|
41
|
+
runtime_id: this.runtime_id,
|
|
42
|
+
workspace_id: this.workspace_id,
|
|
43
|
+
kind: this.kind,
|
|
44
|
+
cwd: this.options.cwd,
|
|
45
|
+
status: this.status,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
_getCheckpointService() {
|
|
49
|
+
if (!this.checkpointService) {
|
|
50
|
+
const workspaceDir = getWorkspaceDir(this.options.workspace_id, this.options.agentDir);
|
|
51
|
+
this.checkpointService = new LocalCheckpointService({
|
|
52
|
+
workspace_id: this.options.workspace_id,
|
|
53
|
+
runtime_id: this.runtime_id,
|
|
54
|
+
checkpointDir: join(workspaceDir, "checkpoints"),
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return this.checkpointService;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reactor Policies
|
|
3
|
+
*
|
|
4
|
+
* Pluggable policy objects that customize reactor behavior without coupling
|
|
5
|
+
* the reactor to heavy dependencies (SettingsManager, SessionManager, etc).
|
|
6
|
+
*
|
|
7
|
+
* Each policy is a small, testable strategy interface. Default implementations
|
|
8
|
+
* cover the common cases; richer implementations may consult settings/session state.
|
|
9
|
+
*/
|
|
10
|
+
import type { EventBase } from "../event-store/types.js";
|
|
11
|
+
/** Decides whether and how to retry after an LLM call failure. */
|
|
12
|
+
export interface RetryPolicy {
|
|
13
|
+
/** Is this error transient (retryable)? */
|
|
14
|
+
isRetryable(error: {
|
|
15
|
+
message: string;
|
|
16
|
+
statusCode?: number;
|
|
17
|
+
}): boolean;
|
|
18
|
+
/** Compute backoff delay for the given attempt (1-indexed). Return null to give up. */
|
|
19
|
+
nextDelayMs(attempt: number): number | null;
|
|
20
|
+
/** Maximum attempts before giving up. */
|
|
21
|
+
maxAttempts: number;
|
|
22
|
+
}
|
|
23
|
+
/** Built-in retry policy: matches the patterns historically used by the reactor. */
|
|
24
|
+
export declare class DefaultRetryPolicy implements RetryPolicy {
|
|
25
|
+
maxAttempts: number;
|
|
26
|
+
private baseDelayMs;
|
|
27
|
+
private capDelayMs;
|
|
28
|
+
constructor(opts?: {
|
|
29
|
+
maxAttempts?: number;
|
|
30
|
+
baseDelayMs?: number;
|
|
31
|
+
capDelayMs?: number;
|
|
32
|
+
});
|
|
33
|
+
isRetryable(error: {
|
|
34
|
+
message: string;
|
|
35
|
+
statusCode?: number;
|
|
36
|
+
}): boolean;
|
|
37
|
+
nextDelayMs(attempt: number): number | null;
|
|
38
|
+
}
|
|
39
|
+
/** Reason a compaction is being requested. */
|
|
40
|
+
export type CompactionReason = "manual" | "threshold" | "overflow";
|
|
41
|
+
/** A single compaction operation's outcome. */
|
|
42
|
+
export interface CompactionOutcome {
|
|
43
|
+
/** Marker event_id from which the summarized context begins. */
|
|
44
|
+
first_kept_event_id: string;
|
|
45
|
+
/** LLM-produced summary text. */
|
|
46
|
+
summary: string;
|
|
47
|
+
/** Estimated tokens before compaction. */
|
|
48
|
+
tokens_before: number;
|
|
49
|
+
/** Estimated tokens after compaction (post-summary context). */
|
|
50
|
+
tokens_after?: number;
|
|
51
|
+
}
|
|
52
|
+
/** Decides whether to compact, and how to compact when asked. */
|
|
53
|
+
export interface CompactionPolicy {
|
|
54
|
+
/** Estimate current context token usage. Return 0 if unknown. */
|
|
55
|
+
estimateContextTokens(): number;
|
|
56
|
+
/** Context window size for the active model (used for threshold/overflow detection). */
|
|
57
|
+
contextWindow(): number;
|
|
58
|
+
/** Compaction threshold as a fraction of contextWindow (default 0.85 in real settings). */
|
|
59
|
+
threshold(): number;
|
|
60
|
+
/** Run the actual compaction. Should throw on failure. */
|
|
61
|
+
compact(reason: CompactionReason, signal: AbortSignal): Promise<CompactionOutcome>;
|
|
62
|
+
/** Was the just-finished assistant message an overflow signal? */
|
|
63
|
+
isOverflow(lastAssistantMessageEvent: EventBase | undefined): boolean;
|
|
64
|
+
}
|
|
65
|
+
/** No-op compaction policy — useful for tests and the bare reactor. */
|
|
66
|
+
export declare class NoopCompactionPolicy implements CompactionPolicy {
|
|
67
|
+
estimateContextTokens(): number;
|
|
68
|
+
contextWindow(): number;
|
|
69
|
+
threshold(): number;
|
|
70
|
+
compact(): Promise<CompactionOutcome>;
|
|
71
|
+
isOverflow(): boolean;
|
|
72
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reactor Policies
|
|
3
|
+
*
|
|
4
|
+
* Pluggable policy objects that customize reactor behavior without coupling
|
|
5
|
+
* the reactor to heavy dependencies (SettingsManager, SessionManager, etc).
|
|
6
|
+
*
|
|
7
|
+
* Each policy is a small, testable strategy interface. Default implementations
|
|
8
|
+
* cover the common cases; richer implementations may consult settings/session state.
|
|
9
|
+
*/
|
|
10
|
+
/** Built-in retry policy: matches the patterns historically used by the reactor. */
|
|
11
|
+
export class DefaultRetryPolicy {
|
|
12
|
+
constructor(opts) {
|
|
13
|
+
this.maxAttempts = opts?.maxAttempts ?? 3;
|
|
14
|
+
this.baseDelayMs = opts?.baseDelayMs ?? 1000;
|
|
15
|
+
this.capDelayMs = opts?.capDelayMs ?? 30000;
|
|
16
|
+
}
|
|
17
|
+
isRetryable(error) {
|
|
18
|
+
if (error.statusCode && [429, 500, 502, 503, 504].includes(error.statusCode))
|
|
19
|
+
return true;
|
|
20
|
+
return /overloaded|provider.?returned.?error|rate.?limit|too many requests|429|5\d{2}|service.?unavailable|server.?error|internal.?error|network.?error|connection.?(error|refused|lost)|other side closed|fetch failed|upstream.?connect|reset before headers|socket hang up|ended without|timed? out|timeout|terminated|retry delay/i.test(error.message);
|
|
21
|
+
}
|
|
22
|
+
nextDelayMs(attempt) {
|
|
23
|
+
if (attempt > this.maxAttempts)
|
|
24
|
+
return null;
|
|
25
|
+
return Math.min(this.baseDelayMs * Math.pow(2, attempt - 1), this.capDelayMs);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** No-op compaction policy — useful for tests and the bare reactor. */
|
|
29
|
+
export class NoopCompactionPolicy {
|
|
30
|
+
estimateContextTokens() { return 0; }
|
|
31
|
+
contextWindow() { return Number.MAX_SAFE_INTEGER; }
|
|
32
|
+
threshold() { return 1; }
|
|
33
|
+
async compact() {
|
|
34
|
+
throw new Error("Compaction not configured");
|
|
35
|
+
}
|
|
36
|
+
isOverflow() { return false; }
|
|
37
|
+
}
|