@tomsun28/pizza 0.1.0 → 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 -14
- package/dist/extension/index.js +0 -374
- package/dist/extension/index.js.map +0 -1
- package/dist/main.js.map +0 -1
- package/themes/pizza.json +0 -87
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session Listing
|
|
3
|
+
*
|
|
4
|
+
* Lists sessions from the new session index (sessions.json) and EventStore.
|
|
5
|
+
* Replaces old SessionManager.list/listAll static methods with a clean,
|
|
6
|
+
* event-sourced implementation.
|
|
7
|
+
*/
|
|
8
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
import { getAgentDir } from "../config.js";
|
|
11
|
+
import { SqliteEventStore } from "./event-store/sqlite-store.js";
|
|
12
|
+
import { deriveWorkspaceId, getEventDatabasePath, getWorkspaceMetaPath } from "./event-store/workspace.js";
|
|
13
|
+
// ============================================================================
|
|
14
|
+
// Helpers
|
|
15
|
+
// ============================================================================
|
|
16
|
+
const SESSION_REF_PREFIX = "event-session:";
|
|
17
|
+
function makeSessionRef(workspaceId, sessionId) {
|
|
18
|
+
return `${SESSION_REF_PREFIX}${workspaceId}:${sessionId}`;
|
|
19
|
+
}
|
|
20
|
+
function extractTextContent(message) {
|
|
21
|
+
const content = message.content;
|
|
22
|
+
if (typeof content === "string")
|
|
23
|
+
return content;
|
|
24
|
+
return content
|
|
25
|
+
.filter((block) => block.type === "text")
|
|
26
|
+
.map((block) => block.text)
|
|
27
|
+
.join(" ");
|
|
28
|
+
}
|
|
29
|
+
function isMessageWithContent(message) {
|
|
30
|
+
return typeof message.role === "string" && "content" in message;
|
|
31
|
+
}
|
|
32
|
+
/** Read session index from a workspace directory */
|
|
33
|
+
function readSessionIndex(indexPath) {
|
|
34
|
+
if (!existsSync(indexPath))
|
|
35
|
+
return undefined;
|
|
36
|
+
try {
|
|
37
|
+
return JSON.parse(readFileSync(indexPath, "utf8"));
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** Read workspace cwd from meta.json */
|
|
44
|
+
function readWorkspaceCwd(workspaceId, agentDir) {
|
|
45
|
+
const metaPath = getWorkspaceMetaPath(workspaceId, agentDir);
|
|
46
|
+
if (!existsSync(metaPath))
|
|
47
|
+
return undefined;
|
|
48
|
+
try {
|
|
49
|
+
const meta = JSON.parse(readFileSync(metaPath, "utf8"));
|
|
50
|
+
return meta.cwd;
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** Build SessionListInfo from a session descriptor by querying EventStore for metadata */
|
|
57
|
+
function buildSessionInfo(descriptor, workspaceId, cwd, store) {
|
|
58
|
+
let messageCount = 0;
|
|
59
|
+
let firstMessage = "";
|
|
60
|
+
const allMessages = [];
|
|
61
|
+
let lastActivity = descriptor.created_at;
|
|
62
|
+
// Query context-relevant events for this session's range
|
|
63
|
+
const events = store.query({
|
|
64
|
+
types: ["USER_MESSAGE", "AGENT_MESSAGE_END"],
|
|
65
|
+
});
|
|
66
|
+
for (const event of events) {
|
|
67
|
+
if (event.timestamp > 0) {
|
|
68
|
+
lastActivity = Math.max(lastActivity, event.timestamp);
|
|
69
|
+
}
|
|
70
|
+
if (event.type === "USER_MESSAGE") {
|
|
71
|
+
const payload = event.payload;
|
|
72
|
+
const text = typeof payload.content === "string"
|
|
73
|
+
? payload.content
|
|
74
|
+
: "";
|
|
75
|
+
messageCount++;
|
|
76
|
+
if (text && !firstMessage) {
|
|
77
|
+
firstMessage = text.slice(0, 200);
|
|
78
|
+
}
|
|
79
|
+
if (text)
|
|
80
|
+
allMessages.push(text);
|
|
81
|
+
}
|
|
82
|
+
else if (event.type === "AGENT_MESSAGE_END") {
|
|
83
|
+
messageCount++;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
path: makeSessionRef(workspaceId, descriptor.session_id),
|
|
88
|
+
id: descriptor.session_id,
|
|
89
|
+
cwd,
|
|
90
|
+
name: descriptor.name,
|
|
91
|
+
parentSessionPath: descriptor.parent_session_id
|
|
92
|
+
? makeSessionRef(workspaceId, descriptor.parent_session_id)
|
|
93
|
+
: undefined,
|
|
94
|
+
created: new Date(descriptor.created_at),
|
|
95
|
+
modified: new Date(lastActivity),
|
|
96
|
+
messageCount,
|
|
97
|
+
firstMessage: firstMessage || "(no messages)",
|
|
98
|
+
allMessagesText: allMessages.join(" "),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
// ============================================================================
|
|
102
|
+
// Public API
|
|
103
|
+
// ============================================================================
|
|
104
|
+
/**
|
|
105
|
+
* List all sessions for a workspace (current cwd).
|
|
106
|
+
* Reads from sessions.json + EventStore for each session.
|
|
107
|
+
*/
|
|
108
|
+
export async function listWorkspaceSessions(cwd, agentDir = getAgentDir(), onProgress) {
|
|
109
|
+
const workspaceId = deriveWorkspaceId(cwd);
|
|
110
|
+
const indexPath = join(agentDir, "workspaces", workspaceId, "sessions.json");
|
|
111
|
+
const index = readSessionIndex(indexPath);
|
|
112
|
+
if (!index || index.sessions.length === 0)
|
|
113
|
+
return [];
|
|
114
|
+
const dbPath = getEventDatabasePath(workspaceId, agentDir);
|
|
115
|
+
if (!existsSync(dbPath))
|
|
116
|
+
return [];
|
|
117
|
+
const store = new SqliteEventStore(workspaceId, dbPath, "session_list");
|
|
118
|
+
try {
|
|
119
|
+
const workspaceCwd = readWorkspaceCwd(workspaceId, agentDir) ?? cwd;
|
|
120
|
+
const results = [];
|
|
121
|
+
for (let i = 0; i < index.sessions.length; i++) {
|
|
122
|
+
const desc = index.sessions[i];
|
|
123
|
+
results.push(buildSessionInfo(desc, workspaceId, workspaceCwd, store));
|
|
124
|
+
onProgress?.(i + 1, index.sessions.length);
|
|
125
|
+
}
|
|
126
|
+
results.sort((a, b) => b.modified.getTime() - a.modified.getTime());
|
|
127
|
+
return results;
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
store.close();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* List all sessions across all workspaces.
|
|
135
|
+
* Scans agentDir/workspaces/* for sessions.json files.
|
|
136
|
+
*/
|
|
137
|
+
export async function listAllSessions(agentDir = getAgentDir(), onProgress) {
|
|
138
|
+
const workspacesDir = join(agentDir, "workspaces");
|
|
139
|
+
if (!existsSync(workspacesDir))
|
|
140
|
+
return [];
|
|
141
|
+
const workspaceEntries = readdirSync(workspacesDir, { withFileTypes: true })
|
|
142
|
+
.filter((entry) => entry.isDirectory())
|
|
143
|
+
.map((entry) => entry.name);
|
|
144
|
+
const results = [];
|
|
145
|
+
for (let i = 0; i < workspaceEntries.length; i++) {
|
|
146
|
+
const workspaceId = workspaceEntries[i];
|
|
147
|
+
const indexPath = join(workspacesDir, workspaceId, "sessions.json");
|
|
148
|
+
const index = readSessionIndex(indexPath);
|
|
149
|
+
if (!index || index.sessions.length === 0)
|
|
150
|
+
continue;
|
|
151
|
+
const dbPath = getEventDatabasePath(workspaceId, agentDir);
|
|
152
|
+
if (!existsSync(dbPath))
|
|
153
|
+
continue;
|
|
154
|
+
const store = new SqliteEventStore(workspaceId, dbPath, "session_list_all");
|
|
155
|
+
try {
|
|
156
|
+
const workspaceCwd = readWorkspaceCwd(workspaceId, agentDir) ?? process.cwd();
|
|
157
|
+
for (const desc of index.sessions) {
|
|
158
|
+
results.push(buildSessionInfo(desc, workspaceId, workspaceCwd, store));
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
finally {
|
|
162
|
+
store.close();
|
|
163
|
+
}
|
|
164
|
+
onProgress?.(i + 1, workspaceEntries.length);
|
|
165
|
+
}
|
|
166
|
+
results.sort((a, b) => b.modified.getTime() - a.modified.getTime());
|
|
167
|
+
return results;
|
|
168
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session Reader
|
|
3
|
+
*
|
|
4
|
+
* Minimal utility for reading session data from EventStore for export.
|
|
5
|
+
* This is the only remaining code that understands the old SESSION_ENTRY_APPENDED
|
|
6
|
+
* event format used to reconstruct entries for HTML export.
|
|
7
|
+
*/
|
|
8
|
+
import type { SessionEntry, SessionHeader } from "./types/session-types.js";
|
|
9
|
+
export interface ReadSessionResult {
|
|
10
|
+
header: SessionHeader | null;
|
|
11
|
+
entries: SessionEntry[];
|
|
12
|
+
leafId: string | null;
|
|
13
|
+
sessionId: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Read session data from EventStore for export.
|
|
17
|
+
* Replaces SessionManager.open() for the export use case.
|
|
18
|
+
*/
|
|
19
|
+
export declare function readSessionForExport(inputPath: string, agentDir?: string): ReadSessionResult;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session Reader
|
|
3
|
+
*
|
|
4
|
+
* Minimal utility for reading session data from EventStore for export.
|
|
5
|
+
* This is the only remaining code that understands the old SESSION_ENTRY_APPENDED
|
|
6
|
+
* event format used to reconstruct entries for HTML export.
|
|
7
|
+
*/
|
|
8
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
9
|
+
import { getAgentDir } from "../config.js";
|
|
10
|
+
import { SqliteEventStore } from "./event-store/sqlite-store.js";
|
|
11
|
+
import { deriveWorkspaceId, getEventDatabasePath, getWorkspaceMetaPath } from "./event-store/workspace.js";
|
|
12
|
+
const SESSION_REF_PREFIX = "event-session:";
|
|
13
|
+
// ============================================================================
|
|
14
|
+
// Helpers
|
|
15
|
+
// ============================================================================
|
|
16
|
+
function parseSessionRef(ref) {
|
|
17
|
+
if (ref.endsWith(".jsonl") || ref.includes("/") || ref.includes("\\")) {
|
|
18
|
+
throw new Error("Legacy JSONL session files are no longer supported; sessions now live in events.sqlite.");
|
|
19
|
+
}
|
|
20
|
+
if (!ref.startsWith(SESSION_REF_PREFIX)) {
|
|
21
|
+
return { sessionId: ref };
|
|
22
|
+
}
|
|
23
|
+
const rest = ref.slice(SESSION_REF_PREFIX.length);
|
|
24
|
+
const [workspaceId, sessionId] = rest.split(":");
|
|
25
|
+
if (!sessionId) {
|
|
26
|
+
throw new Error(`Invalid session reference: ${ref}`);
|
|
27
|
+
}
|
|
28
|
+
return { workspaceId, sessionId };
|
|
29
|
+
}
|
|
30
|
+
function readWorkspaceCwd(workspaceId, agentDir, fallback) {
|
|
31
|
+
const metaPath = getWorkspaceMetaPath(workspaceId, agentDir);
|
|
32
|
+
if (!existsSync(metaPath))
|
|
33
|
+
return fallback;
|
|
34
|
+
try {
|
|
35
|
+
const meta = JSON.parse(readFileSync(metaPath, "utf8"));
|
|
36
|
+
return meta.cwd ?? fallback;
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return fallback;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function entryFromEvent(event) {
|
|
43
|
+
const payload = event.payload;
|
|
44
|
+
return payload.entry;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Read session data from EventStore for export.
|
|
48
|
+
* Replaces SessionManager.open() for the export use case.
|
|
49
|
+
*/
|
|
50
|
+
export function readSessionForExport(inputPath, agentDir = getAgentDir()) {
|
|
51
|
+
const parsed = parseSessionRef(inputPath);
|
|
52
|
+
const workspaceId = parsed.workspaceId ?? deriveWorkspaceId(process.cwd());
|
|
53
|
+
const dbPath = getEventDatabasePath(workspaceId, agentDir);
|
|
54
|
+
if (!existsSync(dbPath)) {
|
|
55
|
+
throw new Error(`Session database not found: ${dbPath}`);
|
|
56
|
+
}
|
|
57
|
+
const store = new SqliteEventStore(workspaceId, dbPath, "export_reader");
|
|
58
|
+
try {
|
|
59
|
+
const sessionId = parsed.sessionId;
|
|
60
|
+
const cwd = readWorkspaceCwd(workspaceId, agentDir, process.cwd());
|
|
61
|
+
// Find the SESSION_CREATED event for this session
|
|
62
|
+
const createdEvents = store.query({ types: ["SESSION_CREATED"] });
|
|
63
|
+
const createdEvent = createdEvents.find((e) => {
|
|
64
|
+
const payload = e.payload;
|
|
65
|
+
return payload.session_id === sessionId;
|
|
66
|
+
});
|
|
67
|
+
let header = null;
|
|
68
|
+
if (createdEvent) {
|
|
69
|
+
const payload = createdEvent.payload;
|
|
70
|
+
header = {
|
|
71
|
+
type: "session",
|
|
72
|
+
version: 3,
|
|
73
|
+
id: sessionId,
|
|
74
|
+
timestamp: payload.timestamp ?? new Date(createdEvent.timestamp).toISOString(),
|
|
75
|
+
cwd: payload.cwd ?? cwd,
|
|
76
|
+
parentSession: payload.parentSession,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
// Rebuild entries from SESSION_ENTRY_APPENDED events
|
|
80
|
+
const appendedEvents = store.query({ types: ["SESSION_ENTRY_APPENDED"] }).filter((event) => {
|
|
81
|
+
const payload = event.payload;
|
|
82
|
+
return payload.session_id === sessionId;
|
|
83
|
+
});
|
|
84
|
+
const entries = [];
|
|
85
|
+
let leafId = null;
|
|
86
|
+
for (const event of appendedEvents) {
|
|
87
|
+
const payload = event.payload;
|
|
88
|
+
if (payload.entry) {
|
|
89
|
+
entries.push(payload.entry);
|
|
90
|
+
leafId = payload.entry.id;
|
|
91
|
+
}
|
|
92
|
+
if (payload.leaf_id !== undefined) {
|
|
93
|
+
leafId = payload.leaf_id;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return { header, entries, leafId, sessionId };
|
|
97
|
+
}
|
|
98
|
+
finally {
|
|
99
|
+
store.close();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const SESSION_REF_PREFIX = "event-session:";
|
|
2
|
+
export declare function makeSessionRef(workspaceId: string, sessionId: string): string;
|
|
3
|
+
export declare function parseSessionRef(ref: string): {
|
|
4
|
+
workspaceId?: string;
|
|
5
|
+
sessionId: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function getAgentDirFromSessionDir(sessionDir?: string): string;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { basename, dirname } from "node:path";
|
|
2
|
+
import { getAgentDir as getDefaultAgentDir } from "../config.js";
|
|
3
|
+
export const SESSION_REF_PREFIX = "event-session:";
|
|
4
|
+
export function makeSessionRef(workspaceId, sessionId) {
|
|
5
|
+
return `${SESSION_REF_PREFIX}${workspaceId}:${sessionId}`;
|
|
6
|
+
}
|
|
7
|
+
export function parseSessionRef(ref) {
|
|
8
|
+
if (ref.endsWith(".jsonl") || ref.includes("/") || ref.includes("\\")) {
|
|
9
|
+
throw new Error("Legacy JSONL session files are no longer supported; sessions now live in events.sqlite.");
|
|
10
|
+
}
|
|
11
|
+
if (!ref.startsWith(SESSION_REF_PREFIX)) {
|
|
12
|
+
return { sessionId: ref };
|
|
13
|
+
}
|
|
14
|
+
const rest = ref.slice(SESSION_REF_PREFIX.length);
|
|
15
|
+
const [workspaceId, sessionId] = rest.split(":");
|
|
16
|
+
if (!sessionId) {
|
|
17
|
+
throw new Error(`Invalid session reference: ${ref}`);
|
|
18
|
+
}
|
|
19
|
+
return { workspaceId, sessionId };
|
|
20
|
+
}
|
|
21
|
+
export function getAgentDirFromSessionDir(sessionDir) {
|
|
22
|
+
if (!sessionDir)
|
|
23
|
+
return getDefaultAgentDir();
|
|
24
|
+
return basename(sessionDir) === "sessions" ? dirname(sessionDir) : sessionDir;
|
|
25
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { AuthStorage } from "./auth-storage.js";
|
|
2
|
+
import { ModelRegistry } from "./model-registry.js";
|
|
3
|
+
import { type DefaultResourceLoaderOptions, type ResourceLoader } from "./resource-loader.js";
|
|
4
|
+
import { SettingsManager } from "./settings-manager.js";
|
|
5
|
+
/**
|
|
6
|
+
* Non-fatal issues collected while creating services or sessions.
|
|
7
|
+
*
|
|
8
|
+
* Runtime creation returns diagnostics to the caller instead of printing or
|
|
9
|
+
* exiting. The app layer decides whether warnings should be shown and whether
|
|
10
|
+
* errors should abort startup.
|
|
11
|
+
*/
|
|
12
|
+
export interface SessionDiagnostic {
|
|
13
|
+
type: "info" | "warning" | "error";
|
|
14
|
+
message: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Inputs for creating cwd-bound runtime services.
|
|
18
|
+
*
|
|
19
|
+
* These services are recreated whenever the effective session cwd changes.
|
|
20
|
+
* CLI-provided resource paths should be resolved to absolute paths before they
|
|
21
|
+
* reach this function, so later cwd switches do not reinterpret them.
|
|
22
|
+
*/
|
|
23
|
+
export interface CreateSessionServicesOptions {
|
|
24
|
+
cwd: string;
|
|
25
|
+
agentDir?: string;
|
|
26
|
+
authStorage?: AuthStorage;
|
|
27
|
+
settingsManager?: SettingsManager;
|
|
28
|
+
modelRegistry?: ModelRegistry;
|
|
29
|
+
extensionFlagValues?: Map<string, boolean | string>;
|
|
30
|
+
resourceLoaderOptions?: Omit<DefaultResourceLoaderOptions, "cwd" | "agentDir" | "settingsManager">;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Coherent cwd-bound runtime services for one effective session cwd.
|
|
34
|
+
*
|
|
35
|
+
* Services are created separately so session options can be resolved against them first.
|
|
36
|
+
*/
|
|
37
|
+
export interface SessionServices {
|
|
38
|
+
cwd: string;
|
|
39
|
+
agentDir: string;
|
|
40
|
+
authStorage: AuthStorage;
|
|
41
|
+
settingsManager: SettingsManager;
|
|
42
|
+
modelRegistry: ModelRegistry;
|
|
43
|
+
resourceLoader: ResourceLoader;
|
|
44
|
+
diagnostics: SessionDiagnostic[];
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Create cwd-bound runtime services.
|
|
48
|
+
*
|
|
49
|
+
* Returns services plus diagnostics. Does not create a session facade.
|
|
50
|
+
*/
|
|
51
|
+
export declare function createSessionServices(options: CreateSessionServicesOptions): Promise<SessionServices>;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { getAgentDir } from "../config.js";
|
|
3
|
+
import { AuthStorage } from "./auth-storage.js";
|
|
4
|
+
import { ModelRegistry } from "./model-registry.js";
|
|
5
|
+
import { DefaultResourceLoader } from "./resource-loader.js";
|
|
6
|
+
import { SettingsManager } from "./settings-manager.js";
|
|
7
|
+
function applyExtensionFlagValues(resourceLoader, extensionFlagValues) {
|
|
8
|
+
if (!extensionFlagValues) {
|
|
9
|
+
return [];
|
|
10
|
+
}
|
|
11
|
+
const diagnostics = [];
|
|
12
|
+
const extensionsResult = resourceLoader.getExtensions();
|
|
13
|
+
const registeredFlags = new Map();
|
|
14
|
+
for (const extension of extensionsResult.extensions) {
|
|
15
|
+
for (const [name, flag] of extension.flags) {
|
|
16
|
+
registeredFlags.set(name, { type: flag.type });
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const unknownFlags = [];
|
|
20
|
+
for (const [name, value] of extensionFlagValues) {
|
|
21
|
+
const flag = registeredFlags.get(name);
|
|
22
|
+
if (!flag) {
|
|
23
|
+
unknownFlags.push(name);
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (flag.type === "boolean") {
|
|
27
|
+
extensionsResult.runtime.flagValues.set(name, true);
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
if (typeof value === "string") {
|
|
31
|
+
extensionsResult.runtime.flagValues.set(name, value);
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
diagnostics.push({
|
|
35
|
+
type: "error",
|
|
36
|
+
message: `Extension flag "--${name}" requires a value`,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
if (unknownFlags.length > 0) {
|
|
40
|
+
diagnostics.push({
|
|
41
|
+
type: "error",
|
|
42
|
+
message: `Unknown option${unknownFlags.length === 1 ? "" : "s"}: ${unknownFlags.map((name) => `--${name}`).join(", ")}`,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
return diagnostics;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Create cwd-bound runtime services.
|
|
49
|
+
*
|
|
50
|
+
* Returns services plus diagnostics. Does not create a session facade.
|
|
51
|
+
*/
|
|
52
|
+
export async function createSessionServices(options) {
|
|
53
|
+
const cwd = options.cwd;
|
|
54
|
+
const agentDir = options.agentDir ?? getAgentDir();
|
|
55
|
+
const authStorage = options.authStorage ?? AuthStorage.create(join(agentDir, "auth.json"));
|
|
56
|
+
const settingsManager = options.settingsManager ?? SettingsManager.create(cwd, agentDir);
|
|
57
|
+
const modelRegistry = options.modelRegistry ?? ModelRegistry.create(authStorage, join(agentDir, "models.json"));
|
|
58
|
+
const resourceLoader = new DefaultResourceLoader({
|
|
59
|
+
...(options.resourceLoaderOptions ?? {}),
|
|
60
|
+
cwd,
|
|
61
|
+
agentDir,
|
|
62
|
+
settingsManager,
|
|
63
|
+
});
|
|
64
|
+
await resourceLoader.reload();
|
|
65
|
+
const diagnostics = [];
|
|
66
|
+
const extensionsResult = resourceLoader.getExtensions();
|
|
67
|
+
for (const { name, config, extensionPath } of extensionsResult.runtime.pendingProviderRegistrations) {
|
|
68
|
+
try {
|
|
69
|
+
modelRegistry.registerProvider(name, config);
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
73
|
+
diagnostics.push({
|
|
74
|
+
type: "error",
|
|
75
|
+
message: `Extension "${extensionPath}" error: ${message}`,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
extensionsResult.runtime.pendingProviderRegistrations = [];
|
|
80
|
+
diagnostics.push(...applyExtensionFlagValues(resourceLoader, options.extensionFlagValues));
|
|
81
|
+
return {
|
|
82
|
+
cwd,
|
|
83
|
+
agentDir,
|
|
84
|
+
authStorage,
|
|
85
|
+
settingsManager,
|
|
86
|
+
modelRegistry,
|
|
87
|
+
resourceLoader,
|
|
88
|
+
diagnostics,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { ContextUsage } from "./extensions/index.js";
|
|
2
|
+
import type { AgentMessage } from "./agent/types.js";
|
|
3
|
+
/** Token + cost + message-count statistics derived from a message list (no identity). */
|
|
4
|
+
export interface MessageStats {
|
|
5
|
+
userMessages: number;
|
|
6
|
+
assistantMessages: number;
|
|
7
|
+
toolCalls: number;
|
|
8
|
+
toolResults: number;
|
|
9
|
+
totalMessages: number;
|
|
10
|
+
tokens: {
|
|
11
|
+
input: number;
|
|
12
|
+
output: number;
|
|
13
|
+
cacheRead: number;
|
|
14
|
+
cacheWrite: number;
|
|
15
|
+
total: number;
|
|
16
|
+
};
|
|
17
|
+
cost: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Compute token/cost/message-count statistics from a list of agent messages.
|
|
21
|
+
* Pure function — no facade, no identity fields. Callers needing sessionFile/
|
|
22
|
+
* sessionId add them on top.
|
|
23
|
+
*/
|
|
24
|
+
export declare function computeMessageStats(messages: AgentMessage[]): MessageStats;
|
|
25
|
+
/** Session statistics for /session command */
|
|
26
|
+
export interface SessionStats {
|
|
27
|
+
sessionFile: string | undefined;
|
|
28
|
+
sessionId: string;
|
|
29
|
+
userMessages: number;
|
|
30
|
+
assistantMessages: number;
|
|
31
|
+
toolCalls: number;
|
|
32
|
+
toolResults: number;
|
|
33
|
+
totalMessages: number;
|
|
34
|
+
tokens: {
|
|
35
|
+
input: number;
|
|
36
|
+
output: number;
|
|
37
|
+
cacheRead: number;
|
|
38
|
+
cacheWrite: number;
|
|
39
|
+
total: number;
|
|
40
|
+
};
|
|
41
|
+
cost: number;
|
|
42
|
+
contextUsage?: ContextUsage;
|
|
43
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compute token/cost/message-count statistics from a list of agent messages.
|
|
3
|
+
* Pure function — no facade, no identity fields. Callers needing sessionFile/
|
|
4
|
+
* sessionId add them on top.
|
|
5
|
+
*/
|
|
6
|
+
export function computeMessageStats(messages) {
|
|
7
|
+
const userMessages = messages.filter((m) => m.role === "user").length;
|
|
8
|
+
const assistantMessages = messages.filter((m) => m.role === "assistant").length;
|
|
9
|
+
const toolResults = messages.filter((m) => m.role === "toolResult").length;
|
|
10
|
+
let toolCalls = 0;
|
|
11
|
+
let totalInput = 0;
|
|
12
|
+
let totalOutput = 0;
|
|
13
|
+
let totalCacheRead = 0;
|
|
14
|
+
let totalCacheWrite = 0;
|
|
15
|
+
let totalCost = 0;
|
|
16
|
+
for (const message of messages) {
|
|
17
|
+
if (message.role !== "assistant")
|
|
18
|
+
continue;
|
|
19
|
+
toolCalls += message.content.filter((block) => block.type === "toolCall").length;
|
|
20
|
+
totalInput += message.usage?.input ?? 0;
|
|
21
|
+
totalOutput += message.usage?.output ?? 0;
|
|
22
|
+
totalCacheRead += message.usage?.cacheRead ?? 0;
|
|
23
|
+
totalCacheWrite += message.usage?.cacheWrite ?? 0;
|
|
24
|
+
totalCost += message.usage?.cost?.total ?? 0;
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
userMessages,
|
|
28
|
+
assistantMessages,
|
|
29
|
+
toolCalls,
|
|
30
|
+
toolResults,
|
|
31
|
+
totalMessages: messages.length,
|
|
32
|
+
tokens: {
|
|
33
|
+
input: totalInput,
|
|
34
|
+
output: totalOutput,
|
|
35
|
+
cacheRead: totalCacheRead,
|
|
36
|
+
cacheWrite: totalCacheWrite,
|
|
37
|
+
total: totalInput + totalOutput + totalCacheRead + totalCacheWrite,
|
|
38
|
+
},
|
|
39
|
+
cost: totalCost,
|
|
40
|
+
};
|
|
41
|
+
}
|