@selesai/code 0.10.2 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/dist/cli/credential-print.js +4 -4
- package/dist/cli.js +0 -0
- package/dist/core/compaction/branch-summarization.js +30 -30
- package/dist/core/compaction/compaction.js +81 -81
- package/dist/core/compaction/utils.js +2 -2
- package/dist/core/export-html/template.css +1066 -1066
- package/dist/core/export-html/template.html +55 -55
- package/dist/core/export-html/template.js +1864 -1864
- package/dist/core/export-html/vendor/highlight.min.js +1212 -1212
- package/dist/core/export-html/vendor/marked.min.js +78 -78
- package/dist/core/messages.js +7 -7
- package/dist/defaults/settings.json +0 -3
- package/dist/extensions/auto-session-name.test.ts +157 -0
- package/dist/extensions/auto-session-name.ts +96 -0
- package/dist/extensions/context-compaction-reminder.test.ts +82 -82
- package/dist/extensions/context-compaction-reminder.ts +28 -28
- package/dist/extensions/cost-reconcile.test.ts +76 -0
- package/dist/extensions/cost-reconcile.ts +278 -0
- package/dist/extensions/enable-readonly-tools.test.ts +11 -0
- package/dist/extensions/enable-readonly-tools.ts +7 -3
- package/dist/extensions/package.json +3 -0
- package/dist/extensions/pi-intercom/LICENSE +21 -21
- package/dist/extensions/pi-intercom/broker/client.test.ts +83 -83
- package/dist/extensions/pi-intercom/broker/framing.test.ts +114 -114
- package/dist/extensions/pi-intercom/broker/paths.ts +134 -134
- package/dist/extensions/pi-intercom/broker/runtime-claim.test.ts +34 -34
- package/dist/extensions/pi-intercom/broker/runtime-claim.ts +21 -21
- package/dist/extensions/pi-intercom/cwd.test.ts +40 -40
- package/dist/extensions/pi-intercom/cwd.ts +31 -31
- package/dist/extensions/pi-intercom/format-context.test.ts +31 -31
- package/dist/extensions/pi-intercom/format-context.ts +32 -32
- package/dist/extensions/pi-intercom/test/overlay-width.test.ts +66 -66
- package/dist/extensions/pi-intercom/ui/compose.ts +143 -143
- package/dist/extensions/pi-intercom/ui/session-list.ts +166 -166
- package/dist/extensions/pi-rewind-hook/README.md +245 -245
- package/dist/extensions/pi-rewind-hook/index.ts +1445 -1445
- package/dist/extensions/pi-rewind-hook/package.json +29 -29
- package/dist/extensions/pi-subagents/install.mjs +0 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/bun/cli.d.ts +3 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/bun/register-bedrock.d.ts +2 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/bun/restore-sandbox-env.d.ts +17 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/cli/args.d.ts +57 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/cli/config-selector.d.ts +16 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/cli/file-processor.d.ts +15 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/cli/initial-message.d.ts +18 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/cli/list-models.d.ts +9 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/cli/project-trust.d.ts +10 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/cli/session-picker.d.ts +10 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/cli/startup-ui.d.ts +20 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/cli.d.ts +3 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/config.d.ts +96 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/agent-session-runtime.d.ts +119 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/agent-session-services.d.ts +85 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/agent-session.d.ts +620 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/auth-guidance.d.ts +5 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/auth-storage.d.ts +56 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/bash-executor.d.ts +32 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/cache-stats.d.ts +49 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/compaction/branch-summarization.d.ts +93 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/compaction/compaction.d.ts +128 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/compaction/index.d.ts +7 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/compaction/utils.d.ts +38 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/defaults.d.ts +3 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/diagnostics.d.ts +15 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/event-bus.d.ts +9 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/exec.d.ts +29 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/experimental.d.ts +2 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/export-html/ansi-to-html.d.ts +22 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/export-html/index.d.ts +37 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/export-html/tool-renderer.d.ts +34 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/extensions/index.d.ts +12 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/extensions/loader.d.ts +23 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/extensions/runner.d.ts +171 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/extensions/types.d.ts +1261 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/extensions/wrapper.d.ts +20 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/footer-data-provider.d.ts +54 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/http-dispatcher.d.ts +22 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/index.d.ts +13 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/keybindings.d.ts +358 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/messages.d.ts +77 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/model-config.d.ts +512 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/model-registry.d.ts +43 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/model-resolver.d.ts +121 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/model-runtime.d.ts +82 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/models-store.d.ts +17 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/output-guard.d.ts +7 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/package-manager.d.ts +210 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/project-trust.d.ts +15 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/prompt-templates.d.ts +54 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/provider-attribution.d.ts +4 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/provider-composer.d.ts +55 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/radius.d.ts +2 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/remote-catalog-provider.d.ts +5 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/resolve-config-value.d.ts +30 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/resource-loader.d.ts +206 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/runtime-credentials.d.ts +15 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/sdk.d.ts +106 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/session-cwd.d.ts +19 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/session-manager.d.ts +356 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/settings-manager.d.ts +297 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/skills.d.ts +60 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/slash-commands.d.ts +15 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/source-info.d.ts +18 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/system-prompt.d.ts +28 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/telemetry.d.ts +3 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/timings.d.ts +10 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/tools/bash.d.ts +68 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/tools/edit-diff.d.ts +106 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/tools/edit.d.ts +51 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/tools/file-mutation-queue.d.ts +6 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/tools/find.d.ts +35 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/tools/grep.d.ts +37 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/tools/index.d.ts +40 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/tools/ls.d.ts +37 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/tools/output-accumulator.d.ts +52 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/tools/path-utils.d.ts +10 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/tools/read.d.ts +35 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/tools/render-utils.d.ts +24 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/tools/tool-definition-wrapper.d.ts +14 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/tools/truncate.d.ts +70 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/tools/write.d.ts +26 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/trust-manager.d.ts +36 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/core/usage-totals.d.ts +19 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/extensions/index.d.ts +3 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/extensions/llama/client.d.ts +61 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/extensions/llama/huggingface.d.ts +23 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/extensions/llama/index.d.ts +3 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/extensions/llama/provider.d.ts +10 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/extensions/llama/ui.d.ts +42 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/index.d.ts +35 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/main.d.ts +12 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/migrations.d.ts +33 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/index.d.ts +9 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/armin.d.ts +34 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/assistant-message.d.ts +22 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/bash-execution.d.ts +34 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/bordered-loader.d.ts +16 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/branch-summary-message.d.ts +16 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/compaction-summary-message.d.ts +16 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/config-selector.d.ts +102 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/countdown-timer.d.ts +14 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/custom-editor.d.ts +21 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/custom-entry.d.ts +19 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/custom-message.d.ts +20 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/daxnuts.d.ts +23 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/diff.d.ts +12 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/dynamic-border.d.ts +15 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/earendil-announcement.d.ts +5 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/extension-editor.d.ts +22 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/extension-input.d.ts +23 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/extension-selector.d.ts +26 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/first-time-setup.d.ts +25 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/footer.d.ts +32 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/index.d.ts +34 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/keybinding-hints.d.ts +13 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/login-dialog.d.ts +52 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/model-selector.d.ts +54 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/oauth-selector.d.ts +33 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/scoped-models-selector.d.ts +42 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/session-selector-search.d.ts +23 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/session-selector.d.ts +95 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/settings-selector.d.ts +77 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/show-images-selector.d.ts +10 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/skill-invocation-message.d.ts +17 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/status-indicator.d.ts +28 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/theme-selector.d.ts +11 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/thinking-selector.d.ts +11 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/tool-execution.d.ts +63 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/tree-selector.d.ts +94 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/trust-selector.d.ts +23 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/user-message-selector.d.ts +30 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/user-message.d.ts +14 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/components/visual-truncate.d.ts +24 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/interactive-mode.d.ts +394 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/model-search.d.ts +12 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/theme/theme-controller.d.ts +29 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/interactive/theme/theme.d.ts +120 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/print-mode.d.ts +28 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/rpc/jsonl.d.ts +17 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/rpc/rpc-client.d.ts +246 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/rpc/rpc-mode.d.ts +20 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/modes/rpc/rpc-types.d.ts +457 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/package-manager-cli.d.ts +8 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/rpc-entry.d.ts +3 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/ansi.d.ts +2 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/changelog.d.ts +22 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/child-process.d.ts +18 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/clipboard-image.d.ts +11 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/clipboard-native.d.ts +11 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/clipboard.d.ts +4 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/deprecation.d.ts +4 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/exif-orientation.d.ts +5 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/frontmatter.d.ts +8 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/fs-watch.d.ts +5 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/git.d.ts +26 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/html.d.ts +7 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/image-convert.d.ts +10 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/image-process.d.ts +18 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/image-resize-core.d.ts +30 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/image-resize-worker.d.ts +2 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/image-resize.d.ts +16 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/json.d.ts +3 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/mime.d.ts +3 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/open-browser.d.ts +9 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/paths.d.ts +31 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/photon.d.ts +21 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/pi-user-agent.d.ts +2 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/shell.d.ts +31 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/sleep.d.ts +5 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/syntax-highlight.d.ts +12 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/tools-manager.d.ts +3 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/version-check.d.ts +15 -0
- package/dist/extensions/pi-subagents/test/fixtures/pi-coding-agent-shim/dist/utils/windows-self-update.d.ts +3 -0
- package/dist/extensions/pi-tool-display/CHANGELOG.md +329 -0
- package/dist/extensions/pi-tool-display/LICENSE +21 -0
- package/dist/extensions/pi-tool-display/README.md +417 -0
- package/dist/extensions/pi-tool-display/config/config.example.json +40 -0
- package/dist/extensions/pi-tool-display/index.ts +3 -0
- package/dist/extensions/pi-tool-display/package-lock.json +2555 -0
- package/dist/extensions/pi-tool-display/package.json +86 -0
- package/dist/extensions/pi-tool-display/src/agent-dir.ts +35 -0
- package/dist/extensions/pi-tool-display/src/ansi-utils.ts +116 -0
- package/dist/extensions/pi-tool-display/src/bash-display.ts +213 -0
- package/dist/extensions/pi-tool-display/src/capabilities.ts +88 -0
- package/dist/extensions/pi-tool-display/src/config-modal.ts +541 -0
- package/dist/extensions/pi-tool-display/src/config-store.ts +304 -0
- package/dist/extensions/pi-tool-display/src/debug-logger.ts +150 -0
- package/dist/extensions/pi-tool-display/src/diff-presentation.ts +72 -0
- package/dist/extensions/pi-tool-display/src/diff-renderer.ts +2517 -0
- package/dist/extensions/pi-tool-display/src/disposable.ts +34 -0
- package/dist/extensions/pi-tool-display/src/extension-lifecycle.ts +14 -0
- package/dist/extensions/pi-tool-display/src/index.ts +109 -0
- package/dist/extensions/pi-tool-display/src/line-width-safety.ts +93 -0
- package/dist/extensions/pi-tool-display/src/modal-icons.ts +57 -0
- package/dist/extensions/pi-tool-display/src/pending-diff-preview.ts +370 -0
- package/dist/extensions/pi-tool-display/src/presets.ts +117 -0
- package/dist/extensions/pi-tool-display/src/render-utils.ts +186 -0
- package/dist/extensions/pi-tool-display/src/settings-inspector-modal.ts +475 -0
- package/dist/extensions/pi-tool-display/src/thinking-label.ts +343 -0
- package/dist/extensions/pi-tool-display/src/tool-call-box.ts +181 -0
- package/dist/extensions/pi-tool-display/src/tool-metadata.ts +139 -0
- package/dist/extensions/pi-tool-display/src/tool-overrides.ts +2168 -0
- package/dist/extensions/pi-tool-display/src/types.ts +109 -0
- package/dist/extensions/pi-tool-display/src/user-message-box-markdown.ts +64 -0
- package/dist/extensions/pi-tool-display/src/user-message-box-native.ts +66 -0
- package/dist/extensions/pi-tool-display/src/user-message-box-patch.ts +72 -0
- package/dist/extensions/pi-tool-display/src/user-message-box-renderer.ts +419 -0
- package/dist/extensions/pi-tool-display/src/user-message-box-utils.ts +106 -0
- package/dist/extensions/pi-tool-display/src/write-display-utils.ts +31 -0
- package/dist/extensions/pi-tool-display/src/zellij-modal.ts +1027 -0
- package/dist/extensions/pi-tool-display/tests/active-backlog-red.test.ts +65 -0
- package/dist/extensions/pi-tool-display/tests/ansi-utils.test.ts +345 -0
- package/dist/extensions/pi-tool-display/tests/bash-display.test.ts +442 -0
- package/dist/extensions/pi-tool-display/tests/capabilities-edge.test.ts +360 -0
- package/dist/extensions/pi-tool-display/tests/capabilities.test.ts +54 -0
- package/dist/extensions/pi-tool-display/tests/config-modal.test.ts +360 -0
- package/dist/extensions/pi-tool-display/tests/config-store.test.ts +87 -0
- package/dist/extensions/pi-tool-display/tests/custom-tool-overrides.test.ts +415 -0
- package/dist/extensions/pi-tool-display/tests/debug-logger-edge.test.ts +630 -0
- package/dist/extensions/pi-tool-display/tests/debug-logger.test.ts +62 -0
- package/dist/extensions/pi-tool-display/tests/diff-renderer-ansi.test.ts +55 -0
- package/dist/extensions/pi-tool-display/tests/diff-renderer-edge.test.ts +886 -0
- package/dist/extensions/pi-tool-display/tests/diff-renderer-numbering.test.ts +135 -0
- package/dist/extensions/pi-tool-display/tests/diff-renderer-width.test.ts +454 -0
- package/dist/extensions/pi-tool-display/tests/diff-renderer-wrap.test.ts +169 -0
- package/dist/extensions/pi-tool-display/tests/index-integration.test.ts +354 -0
- package/dist/extensions/pi-tool-display/tests/pending-diff-preview.test.ts +79 -0
- package/dist/extensions/pi-tool-display/tests/presets-edge.test.ts +229 -0
- package/dist/extensions/pi-tool-display/tests/presets.test.ts +26 -0
- package/dist/extensions/pi-tool-display/tests/reload-behavior.test.ts +1241 -0
- package/dist/extensions/pi-tool-display/tests/render-utils.test.ts +710 -0
- package/dist/extensions/pi-tool-display/tests/settings-inspector.test.ts +450 -0
- package/dist/extensions/pi-tool-display/tests/thinking-label-edge.test.ts +904 -0
- package/dist/extensions/pi-tool-display/tests/thinking-label.test.ts +72 -0
- package/dist/extensions/pi-tool-display/tests/tool-call-box.test.ts +105 -0
- package/dist/extensions/pi-tool-display/tests/tool-overrides-config.test.ts +613 -0
- package/dist/extensions/pi-tool-display/tests/tool-overrides-mcp.test.ts +499 -0
- package/dist/extensions/pi-tool-display/tests/tool-overrides-registration.test.ts +315 -0
- package/dist/extensions/pi-tool-display/tests/tool-overrides-write-meta.test.ts +68 -0
- package/dist/extensions/pi-tool-display/tests/tool-ui-utils.test.ts +413 -0
- package/dist/extensions/pi-tool-display/tests/user-message-box-performance-red.test.ts +176 -0
- package/dist/extensions/pi-tool-display/tests/user-message-box.test.ts +908 -0
- package/dist/extensions/pi-tool-display/tests/zellij-modal.test.ts +848 -0
- package/dist/extensions/pi-tool-display/tool-display-api-consumer.d.ts +30 -0
- package/dist/extensions/pi-tool-display/tool-display-api-consumer.js +40 -0
- package/dist/extensions/pi-web-agent/package.json +31 -31
- package/dist/extensions/pi-web-agent/src/backends/config.ts +205 -205
- package/dist/extensions/pi-web-agent/src/backends/doctor.ts +136 -136
- package/dist/extensions/pi-web-agent/src/backends/factory.ts +152 -152
- package/dist/extensions/pi-web-agent/src/backends/settings-reader.ts +25 -25
- package/dist/extensions/pi-web-agent/src/cache/ttl-cache.ts +28 -28
- package/dist/extensions/pi-web-agent/src/changelog-notice.ts +136 -136
- package/dist/extensions/pi-web-agent/src/commands/web-agent-config.ts +946 -946
- package/dist/extensions/pi-web-agent/src/extension.ts +126 -126
- package/dist/extensions/pi-web-agent/src/extract/readability.ts +118 -118
- package/dist/extensions/pi-web-agent/src/fetch/browser-resolution.ts +199 -199
- package/dist/extensions/pi-web-agent/src/fetch/firecrawl-fetch.ts +100 -100
- package/dist/extensions/pi-web-agent/src/fetch/headless-fetch.ts +117 -117
- package/dist/extensions/pi-web-agent/src/fetch/http-fetch.ts +67 -67
- package/dist/extensions/pi-web-agent/src/orchestration/answer-synthesizer.ts +60 -60
- package/dist/extensions/pi-web-agent/src/orchestration/candidate-selector.ts +50 -50
- package/dist/extensions/pi-web-agent/src/orchestration/direct-url.ts +52 -52
- package/dist/extensions/pi-web-agent/src/orchestration/evidence-quality.ts +105 -105
- package/dist/extensions/pi-web-agent/src/orchestration/evidence-ranker.ts +45 -45
- package/dist/extensions/pi-web-agent/src/orchestration/index.ts +28 -28
- package/dist/extensions/pi-web-agent/src/orchestration/query-planner.ts +47 -47
- package/dist/extensions/pi-web-agent/src/orchestration/research-orchestrator.ts +376 -376
- package/dist/extensions/pi-web-agent/src/orchestration/research-types.ts +64 -64
- package/dist/extensions/pi-web-agent/src/orchestration/research-worker.ts +181 -181
- package/dist/extensions/pi-web-agent/src/orchestration/source-profile.ts +101 -101
- package/dist/extensions/pi-web-agent/src/orchestration/stop-decider.ts +81 -81
- package/dist/extensions/pi-web-agent/src/presentation/config-store.ts +210 -210
- package/dist/extensions/pi-web-agent/src/presentation/config.ts +75 -75
- package/dist/extensions/pi-web-agent/src/presentation/explore-presentation.ts +61 -61
- package/dist/extensions/pi-web-agent/src/presentation/fetch-presentation.ts +54 -54
- package/dist/extensions/pi-web-agent/src/presentation/search-presentation.ts +41 -41
- package/dist/extensions/pi-web-agent/src/presentation/select-view.ts +20 -20
- package/dist/extensions/pi-web-agent/src/presentation/types.ts +63 -63
- package/dist/extensions/pi-web-agent/src/search/brave.ts +114 -114
- package/dist/extensions/pi-web-agent/src/search/duckduckgo.ts +72 -72
- package/dist/extensions/pi-web-agent/src/search/searxng.ts +96 -96
- package/dist/extensions/pi-web-agent/src/tools/web-explore.ts +71 -71
- package/dist/extensions/pi-web-agent/src/tools/web-fetch-headless.ts +31 -31
- package/dist/extensions/pi-web-agent/src/tools/web-fetch.ts +31 -31
- package/dist/extensions/pi-web-agent/src/tools/web-search.ts +157 -157
- package/dist/extensions/pi-web-agent/src/types.ts +88 -88
- package/dist/extensions/pi-zentui/extensions/zentui/format.ts +46 -1
- package/dist/extensions/pi-zentui/extensions/zentui/index.ts +10 -0
- package/dist/extensions/pi-zentui/extensions/zentui/user-message-styles.ts +1 -1
- package/dist/extensions/pi-zentui/test/extension-compliance.test.ts +3 -3
- package/dist/extensions/pi-zentui/test/format.test.ts +50 -2
- package/dist/extensions/ponytail/package.json +8 -8
- package/dist/extensions/question/constants.ts +30 -30
- package/dist/extensions/question/helpers.ts +58 -58
- package/dist/extensions/question/navigation.ts +14 -14
- package/dist/extensions/question/package.json +19 -19
- package/dist/extensions/question/selection-mode.ts +46 -46
- package/dist/extensions/question/shortcuts.ts +44 -44
- package/dist/extensions/question/types.ts +132 -132
- package/dist/extensions/test-resolve-hook-impl.mjs +6 -6
- package/dist/extensions/test-resolve-hook.mjs +6 -6
- package/dist/extensions/web-agent-onboarding.ts +222 -222
- package/dist/modes/interactive/interactive-mode.js +17 -3
- package/dist/package-manager-cli.js +71 -71
- package/dist/rpc-entry.js +0 -0
- package/dist/skills/improve-codebase/REFERENCE.md +78 -78
- package/docs/compaction.md +396 -396
- package/docs/containerization.md +111 -111
- package/docs/development.md +71 -71
- package/docs/docs.json +164 -164
- package/docs/environment-variables.md +86 -86
- package/docs/index.md +83 -83
- package/docs/json.md +82 -82
- package/docs/models.md +502 -502
- package/docs/packages.md +227 -227
- package/docs/plans/subagent-delegation/phase-0-correctness.md +264 -264
- package/docs/plans/subagent-delegation/phase-1-behavioral-contract.md +485 -485
- package/docs/plans/subagent-delegation/phase-2-context-controls.md +281 -281
- package/docs/plans/subagent-delegation/phase-3-advisory-routing.md +361 -361
- package/docs/plans/subagent-delegation/phase-4-optional-enforcement.md +380 -380
- package/docs/prompt-templates.md +95 -95
- package/docs/providers.md +293 -293
- package/docs/security.md +59 -59
- package/docs/session-format.md +414 -414
- package/docs/sessions.md +145 -145
- package/docs/shared-host-extensions.md +109 -109
- package/docs/shell-aliases.md +13 -13
- package/docs/skills.md +231 -231
- package/docs/terminal-setup.md +142 -142
- package/docs/termux.md +127 -127
- package/docs/themes.md +295 -295
- package/docs/tmux.md +63 -63
- package/docs/tui.md +927 -927
- package/docs/windows.md +17 -17
- package/examples/README.md +25 -25
- package/examples/extensions/README.md +211 -211
- package/examples/extensions/auto-commit-on-exit.ts +49 -49
- package/examples/extensions/bash-spawn-hook.ts +30 -30
- package/examples/extensions/bookmark.ts +50 -50
- package/examples/extensions/border-status-editor.ts +150 -150
- package/examples/extensions/built-in-tool-renderer.ts +249 -249
- package/examples/extensions/claude-rules.ts +86 -86
- package/examples/extensions/commands.ts +72 -72
- package/examples/extensions/confirm-destructive.ts +59 -59
- package/examples/extensions/custom-compaction.ts +130 -130
- package/examples/extensions/custom-footer.ts +64 -64
- package/examples/extensions/custom-header.ts +73 -73
- package/examples/extensions/custom-provider-anthropic/index.ts +610 -610
- package/examples/extensions/custom-provider-anthropic/package-lock.json +24 -24
- package/examples/extensions/custom-provider-anthropic/package.json +19 -19
- package/examples/extensions/custom-provider-gitlab-duo/index.ts +404 -404
- package/examples/extensions/custom-provider-gitlab-duo/package.json +16 -16
- package/examples/extensions/custom-provider-gitlab-duo/test.ts +82 -82
- package/examples/extensions/dirty-repo-guard.ts +56 -56
- package/examples/extensions/doom-overlay/README.md +46 -46
- package/examples/extensions/doom-overlay/doom/build/doom.js +21 -21
- package/examples/extensions/doom-overlay/doom/build/doom.wasm +0 -0
- package/examples/extensions/doom-overlay/doom/build.sh +152 -152
- package/examples/extensions/doom-overlay/doom/doomgeneric_pi.c +72 -72
- package/examples/extensions/doom-overlay/doom-component.ts +132 -132
- package/examples/extensions/doom-overlay/doom-engine.ts +173 -173
- package/examples/extensions/doom-overlay/doom-keys.ts +104 -104
- package/examples/extensions/doom-overlay/index.ts +74 -74
- package/examples/extensions/doom-overlay/wad-finder.ts +51 -51
- package/examples/extensions/dynamic-resources/SKILL.md +8 -8
- package/examples/extensions/dynamic-resources/dynamic.json +79 -79
- package/examples/extensions/dynamic-resources/dynamic.md +5 -5
- package/examples/extensions/dynamic-resources/index.ts +15 -15
- package/examples/extensions/dynamic-tools.ts +74 -74
- package/examples/extensions/event-bus.ts +43 -43
- package/examples/extensions/file-trigger.ts +41 -41
- package/examples/extensions/git-checkpoint.ts +53 -53
- package/examples/extensions/git-merge-and-resolve.ts +115 -115
- package/examples/extensions/github-issue-autocomplete.ts +185 -185
- package/examples/extensions/gondolin/index.ts +531 -531
- package/examples/extensions/gondolin/package-lock.json +185 -185
- package/examples/extensions/gondolin/package.json +19 -19
- package/examples/extensions/handoff.ts +199 -199
- package/examples/extensions/hello.ts +26 -26
- package/examples/extensions/hidden-thinking-label.ts +53 -53
- package/examples/extensions/inline-bash.ts +94 -94
- package/examples/extensions/input-transform-streaming.ts +39 -39
- package/examples/extensions/input-transform.ts +43 -43
- package/examples/extensions/interactive-shell.ts +196 -196
- package/examples/extensions/mac-system-theme.ts +47 -47
- package/examples/extensions/message-renderer.ts +59 -59
- package/examples/extensions/minimal-mode.ts +426 -426
- package/examples/extensions/modal-editor.ts +85 -85
- package/examples/extensions/model-status.ts +31 -31
- package/examples/extensions/notify.ts +55 -55
- package/examples/extensions/overlay-qa-tests.ts +1450 -1450
- package/examples/extensions/overlay-test.ts +153 -153
- package/examples/extensions/permission-gate.ts +34 -34
- package/examples/extensions/pirate.ts +47 -47
- package/examples/extensions/plan-mode/README.md +66 -66
- package/examples/extensions/plan-mode/index.ts +390 -390
- package/examples/extensions/plan-mode/utils.ts +168 -168
- package/examples/extensions/preset.ts +436 -436
- package/examples/extensions/project-trust.ts +64 -64
- package/examples/extensions/prompt-customizer.ts +97 -97
- package/examples/extensions/protected-paths.ts +30 -30
- package/examples/extensions/provider-payload.ts +18 -18
- package/examples/extensions/qna.ts +122 -122
- package/examples/extensions/question.ts +285 -285
- package/examples/extensions/questionnaire.ts +448 -448
- package/examples/extensions/rainbow-editor.ts +88 -88
- package/examples/extensions/reload-runtime.ts +37 -37
- package/examples/extensions/rpc-demo.ts +118 -118
- package/examples/extensions/sandbox/index.ts +321 -321
- package/examples/extensions/sandbox/package-lock.json +92 -92
- package/examples/extensions/sandbox/package.json +19 -19
- package/examples/extensions/send-user-message.ts +97 -97
- package/examples/extensions/session-name.ts +27 -27
- package/examples/extensions/shutdown-command.ts +63 -63
- package/examples/extensions/snake.ts +343 -343
- package/examples/extensions/space-invaders.ts +560 -560
- package/examples/extensions/ssh.ts +220 -220
- package/examples/extensions/status-line.ts +32 -32
- package/examples/extensions/structured-output.ts +65 -65
- package/examples/extensions/subagent/README.md +175 -175
- package/examples/extensions/subagent/agents/planner.md +37 -37
- package/examples/extensions/subagent/agents/reviewer.md +35 -35
- package/examples/extensions/subagent/agents/scout.md +50 -50
- package/examples/extensions/subagent/agents/worker.md +24 -24
- package/examples/extensions/subagent/agents.ts +126 -126
- package/examples/extensions/subagent/index.ts +1015 -1015
- package/examples/extensions/subagent/prompts/implement-and-review.md +10 -10
- package/examples/extensions/subagent/prompts/implement.md +10 -10
- package/examples/extensions/subagent/prompts/scout-and-plan.md +9 -9
- package/examples/extensions/summarize.ts +209 -209
- package/examples/extensions/system-prompt-header.ts +17 -17
- package/examples/extensions/tic-tac-toe.ts +1008 -1008
- package/examples/extensions/timed-confirm.ts +70 -70
- package/examples/extensions/titlebar-spinner.ts +58 -58
- package/examples/extensions/todo.ts +297 -297
- package/examples/extensions/tool-override.ts +144 -144
- package/examples/extensions/tools.ts +146 -146
- package/examples/extensions/trigger-compact.ts +50 -50
- package/examples/extensions/truncated-tool.ts +195 -195
- package/examples/extensions/widget-placement.ts +9 -9
- package/examples/extensions/with-deps/index.ts +32 -32
- package/examples/extensions/with-deps/package-lock.json +31 -31
- package/examples/extensions/with-deps/package.json +22 -22
- package/examples/extensions/working-indicator.ts +123 -123
- package/examples/extensions/working-message-test.ts +25 -25
- package/examples/rpc-extension-ui.ts +632 -632
- package/examples/sdk/01-minimal.ts +26 -26
- package/examples/sdk/02-custom-model.ts +53 -53
- package/examples/sdk/03-custom-prompt.ts +75 -75
- package/examples/sdk/04-skills.ts +55 -55
- package/examples/sdk/05-tools.ts +48 -48
- package/examples/sdk/06-extensions.ts +99 -99
- package/examples/sdk/07-context-files.ts +47 -47
- package/examples/sdk/08-prompt-templates.ts +51 -51
- package/examples/sdk/09-api-keys-and-oauth.ts +52 -52
- package/examples/sdk/10-settings.ts +53 -53
- package/examples/sdk/11-sessions.ts +52 -52
- package/examples/sdk/12-full-control.ts +79 -79
- package/examples/sdk/13-session-runtime.ts +67 -67
- package/examples/sdk/README.md +144 -144
- package/package.json +3 -3
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* auto-session-name
|
|
3
|
+
*
|
|
4
|
+
* Names the session on every user message using the currently selected model.
|
|
5
|
+
* Sends only user messages (current prompt plus previous user messages,
|
|
6
|
+
* truncated) for context, capped at 10 output tokens (1-5 word name).
|
|
7
|
+
* Thinking is never enabled. Equivalent to running /name automatically.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { complete } from "@earendil-works/pi-ai/compat";
|
|
11
|
+
import type { ExtensionAPI, SessionEntry } from "@selesai/code";
|
|
12
|
+
|
|
13
|
+
export const NAMING_SYSTEM_PROMPT =
|
|
14
|
+
"Reply with only a short session name (1-5 words) for this conversation based on the user's messages. No punctuation, no quotes, no explanation.";
|
|
15
|
+
|
|
16
|
+
export const NAMING_MAX_TOKENS = 10;
|
|
17
|
+
/** Hard cap on the name itself: at most 5 words (≈5 tokens). */
|
|
18
|
+
export const MAX_NAME_WORDS = 5;
|
|
19
|
+
/** How many previous user messages to include, newest first. */
|
|
20
|
+
export const MAX_PREVIOUS_MESSAGES = 20;
|
|
21
|
+
/** Per-message character cap when truncating. */
|
|
22
|
+
export const MAX_MESSAGE_CHARS = 200;
|
|
23
|
+
|
|
24
|
+
function truncate(text: string): string {
|
|
25
|
+
return text.length > MAX_MESSAGE_CHARS ? `${text.slice(0, MAX_MESSAGE_CHARS)}…` : text;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Collect previous user message texts from the session branch, newest first, truncated. */
|
|
29
|
+
export function previousUserMessages(sessionManager: { getBranch(): SessionEntry[] }): string[] {
|
|
30
|
+
const branch = sessionManager.getBranch();
|
|
31
|
+
const texts: string[] = [];
|
|
32
|
+
for (let i = branch.length - 1; i >= 0 && texts.length < MAX_PREVIOUS_MESSAGES; i--) {
|
|
33
|
+
const entry = branch[i];
|
|
34
|
+
if (entry.type !== "message" || entry.message.role !== "user") continue;
|
|
35
|
+
const content = entry.message.content;
|
|
36
|
+
const text = typeof content === "string" ? content : content
|
|
37
|
+
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
|
38
|
+
.map((c) => c.text)
|
|
39
|
+
.join("\n");
|
|
40
|
+
if (text.trim()) texts.push(truncate(text.trim()));
|
|
41
|
+
}
|
|
42
|
+
return texts;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export default function autoSessionNameExtension(pi: ExtensionAPI): void {
|
|
46
|
+
// Guards run synchronously; the naming call itself is fire-and-forget so it
|
|
47
|
+
// never blocks the user's message from being sent.
|
|
48
|
+
let namingSeq = 0;
|
|
49
|
+
pi.on("input", (event, ctx) => {
|
|
50
|
+
// Only real user messages; skip extension-sourced messages and commands.
|
|
51
|
+
if (event.source === "extension") return;
|
|
52
|
+
const text = event.text.trim();
|
|
53
|
+
if (!text || text.startsWith("/")) return;
|
|
54
|
+
if (!ctx.model) return;
|
|
55
|
+
|
|
56
|
+
const seq = ++namingSeq;
|
|
57
|
+
void (async () => {
|
|
58
|
+
try {
|
|
59
|
+
const auth = await ctx.modelRegistry.getApiKeyAndHeaders(ctx.model);
|
|
60
|
+
if (!auth.ok || !auth.apiKey) return;
|
|
61
|
+
|
|
62
|
+
const previous = previousUserMessages(ctx.sessionManager);
|
|
63
|
+
const conversation = [...previous, text].join("\n");
|
|
64
|
+
|
|
65
|
+
const response = await complete(
|
|
66
|
+
ctx.model,
|
|
67
|
+
{
|
|
68
|
+
systemPrompt: NAMING_SYSTEM_PROMPT,
|
|
69
|
+
messages: [{ role: "user", content: conversation, timestamp: Date.now() }],
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
apiKey: auth.apiKey,
|
|
73
|
+
headers: auth.headers,
|
|
74
|
+
maxTokens: NAMING_MAX_TOKENS,
|
|
75
|
+
cacheRetention: "none",
|
|
76
|
+
// The tokenin gateway ignores `thinking: {type:"disabled"}` and burns the
|
|
77
|
+
// whole budget on reasoning; `reasoning_effort: "none"` is what disables it.
|
|
78
|
+
samplingParams: { reasoning_effort: "none" },
|
|
79
|
+
},
|
|
80
|
+
);
|
|
81
|
+
const name = response.content
|
|
82
|
+
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
|
83
|
+
.map((c) => c.text)
|
|
84
|
+
.join("")
|
|
85
|
+
.trim()
|
|
86
|
+
.split(/\s+/)
|
|
87
|
+
.slice(0, MAX_NAME_WORDS)
|
|
88
|
+
.join(" ");
|
|
89
|
+
// Only the latest request may set the name; slower older responses lose.
|
|
90
|
+
if (name && seq === namingSeq) pi.setSessionName(name);
|
|
91
|
+
} catch {
|
|
92
|
+
// Best-effort naming; never break the user's message flow.
|
|
93
|
+
}
|
|
94
|
+
})();
|
|
95
|
+
});
|
|
96
|
+
}
|
|
@@ -1,82 +1,82 @@
|
|
|
1
|
-
import { describe, expect, it, vi } from "vitest";
|
|
2
|
-
import type { ExtensionAPI, ExtensionContext } from "@selesai/code";
|
|
3
|
-
import contextCompactionReminderExtension, {
|
|
4
|
-
CONTEXT_COMPACTION_REMINDER_MESSAGE,
|
|
5
|
-
} from "./context-compaction-reminder.ts";
|
|
6
|
-
|
|
7
|
-
type Handler = (event: unknown, ctx: ExtensionContext) => void | Promise<void>;
|
|
8
|
-
|
|
9
|
-
function createHarness(tokens: number | null | undefined = undefined) {
|
|
10
|
-
const handlers = new Map<string, Handler>();
|
|
11
|
-
const notify = vi.fn();
|
|
12
|
-
const pi = {
|
|
13
|
-
on: vi.fn((event: string, handler: Handler) => handlers.set(event, handler)),
|
|
14
|
-
} as unknown as ExtensionAPI;
|
|
15
|
-
const ctx = {
|
|
16
|
-
getContextUsage: () =>
|
|
17
|
-
tokens === undefined ? undefined : { tokens, contextWindow: 200_000, percent: tokens === null ? null : tokens / 2_000 },
|
|
18
|
-
ui: { notify },
|
|
19
|
-
} as unknown as ExtensionContext;
|
|
20
|
-
|
|
21
|
-
contextCompactionReminderExtension(pi);
|
|
22
|
-
|
|
23
|
-
return {
|
|
24
|
-
notify,
|
|
25
|
-
setTokens: (value: number | null | undefined) => {
|
|
26
|
-
tokens = value;
|
|
27
|
-
},
|
|
28
|
-
settle: () => handlers.get("agent_settled")!({ type: "agent_settled" }, ctx),
|
|
29
|
-
compact: () => handlers.get("session_compact")!({ type: "session_compact" }, ctx),
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
describe("context-compaction-reminder", () => {
|
|
34
|
-
it("warns once when known context reaches 128k", async () => {
|
|
35
|
-
const { notify, setTokens, settle } = createHarness(127_999);
|
|
36
|
-
|
|
37
|
-
await settle();
|
|
38
|
-
setTokens(128_000);
|
|
39
|
-
await settle();
|
|
40
|
-
setTokens(130_000);
|
|
41
|
-
await settle();
|
|
42
|
-
|
|
43
|
-
expect(notify).toHaveBeenCalledTimes(1);
|
|
44
|
-
expect(notify).toHaveBeenCalledWith(CONTEXT_COMPACTION_REMINDER_MESSAGE, "warning");
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it("does not reset the reminder for unavailable or post-compaction usage", async () => {
|
|
48
|
-
const { notify, setTokens, settle } = createHarness(128_000);
|
|
49
|
-
|
|
50
|
-
await settle();
|
|
51
|
-
setTokens(undefined);
|
|
52
|
-
await settle();
|
|
53
|
-
setTokens(null);
|
|
54
|
-
await settle();
|
|
55
|
-
setTokens(130_000);
|
|
56
|
-
await settle();
|
|
57
|
-
|
|
58
|
-
expect(notify).toHaveBeenCalledTimes(1);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it("allows another warning after successful compaction", async () => {
|
|
62
|
-
const { notify, settle, compact } = createHarness(128_000);
|
|
63
|
-
|
|
64
|
-
await settle();
|
|
65
|
-
await compact();
|
|
66
|
-
await settle();
|
|
67
|
-
|
|
68
|
-
expect(notify).toHaveBeenCalledTimes(2);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it("allows another warning after known usage drops below 128k", async () => {
|
|
72
|
-
const { notify, setTokens, settle } = createHarness(128_000);
|
|
73
|
-
|
|
74
|
-
await settle();
|
|
75
|
-
setTokens(127_999);
|
|
76
|
-
await settle();
|
|
77
|
-
setTokens(128_000);
|
|
78
|
-
await settle();
|
|
79
|
-
|
|
80
|
-
expect(notify).toHaveBeenCalledTimes(2);
|
|
81
|
-
});
|
|
82
|
-
});
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import type { ExtensionAPI, ExtensionContext } from "@selesai/code";
|
|
3
|
+
import contextCompactionReminderExtension, {
|
|
4
|
+
CONTEXT_COMPACTION_REMINDER_MESSAGE,
|
|
5
|
+
} from "./context-compaction-reminder.ts";
|
|
6
|
+
|
|
7
|
+
type Handler = (event: unknown, ctx: ExtensionContext) => void | Promise<void>;
|
|
8
|
+
|
|
9
|
+
function createHarness(tokens: number | null | undefined = undefined) {
|
|
10
|
+
const handlers = new Map<string, Handler>();
|
|
11
|
+
const notify = vi.fn();
|
|
12
|
+
const pi = {
|
|
13
|
+
on: vi.fn((event: string, handler: Handler) => handlers.set(event, handler)),
|
|
14
|
+
} as unknown as ExtensionAPI;
|
|
15
|
+
const ctx = {
|
|
16
|
+
getContextUsage: () =>
|
|
17
|
+
tokens === undefined ? undefined : { tokens, contextWindow: 200_000, percent: tokens === null ? null : tokens / 2_000 },
|
|
18
|
+
ui: { notify },
|
|
19
|
+
} as unknown as ExtensionContext;
|
|
20
|
+
|
|
21
|
+
contextCompactionReminderExtension(pi);
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
notify,
|
|
25
|
+
setTokens: (value: number | null | undefined) => {
|
|
26
|
+
tokens = value;
|
|
27
|
+
},
|
|
28
|
+
settle: () => handlers.get("agent_settled")!({ type: "agent_settled" }, ctx),
|
|
29
|
+
compact: () => handlers.get("session_compact")!({ type: "session_compact" }, ctx),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
describe("context-compaction-reminder", () => {
|
|
34
|
+
it("warns once when known context reaches 128k", async () => {
|
|
35
|
+
const { notify, setTokens, settle } = createHarness(127_999);
|
|
36
|
+
|
|
37
|
+
await settle();
|
|
38
|
+
setTokens(128_000);
|
|
39
|
+
await settle();
|
|
40
|
+
setTokens(130_000);
|
|
41
|
+
await settle();
|
|
42
|
+
|
|
43
|
+
expect(notify).toHaveBeenCalledTimes(1);
|
|
44
|
+
expect(notify).toHaveBeenCalledWith(CONTEXT_COMPACTION_REMINDER_MESSAGE, "warning");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("does not reset the reminder for unavailable or post-compaction usage", async () => {
|
|
48
|
+
const { notify, setTokens, settle } = createHarness(128_000);
|
|
49
|
+
|
|
50
|
+
await settle();
|
|
51
|
+
setTokens(undefined);
|
|
52
|
+
await settle();
|
|
53
|
+
setTokens(null);
|
|
54
|
+
await settle();
|
|
55
|
+
setTokens(130_000);
|
|
56
|
+
await settle();
|
|
57
|
+
|
|
58
|
+
expect(notify).toHaveBeenCalledTimes(1);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("allows another warning after successful compaction", async () => {
|
|
62
|
+
const { notify, settle, compact } = createHarness(128_000);
|
|
63
|
+
|
|
64
|
+
await settle();
|
|
65
|
+
await compact();
|
|
66
|
+
await settle();
|
|
67
|
+
|
|
68
|
+
expect(notify).toHaveBeenCalledTimes(2);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("allows another warning after known usage drops below 128k", async () => {
|
|
72
|
+
const { notify, setTokens, settle } = createHarness(128_000);
|
|
73
|
+
|
|
74
|
+
await settle();
|
|
75
|
+
setTokens(127_999);
|
|
76
|
+
await settle();
|
|
77
|
+
setTokens(128_000);
|
|
78
|
+
await settle();
|
|
79
|
+
|
|
80
|
+
expect(notify).toHaveBeenCalledTimes(2);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import type { ExtensionAPI } from "@selesai/code";
|
|
2
|
-
|
|
3
|
-
export const CONTEXT_COMPACTION_REMINDER_THRESHOLD = 128_000;
|
|
4
|
-
export const CONTEXT_COMPACTION_REMINDER_MESSAGE =
|
|
5
|
-
`Conversation context has reached ${CONTEXT_COMPACTION_REMINDER_THRESHOLD.toLocaleString()} tokens. Run /handoff-new to summarize into a new chat, or enable Auto Handoff in Settings to do this automatically. Bigger context windows eat up tokens quickly and degrade AI quality.`;
|
|
6
|
-
|
|
7
|
-
export default function contextCompactionReminderExtension(pi: ExtensionAPI): void {
|
|
8
|
-
let reminderShown = false;
|
|
9
|
-
|
|
10
|
-
pi.on("session_compact", () => {
|
|
11
|
-
reminderShown = false;
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
pi.on("agent_settled", (_event, ctx) => {
|
|
15
|
-
const tokens = ctx.getContextUsage()?.tokens;
|
|
16
|
-
if (tokens === undefined || tokens === null) return;
|
|
17
|
-
|
|
18
|
-
if (tokens < CONTEXT_COMPACTION_REMINDER_THRESHOLD) {
|
|
19
|
-
reminderShown = false;
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
if (!reminderShown) {
|
|
24
|
-
ctx.ui.notify(CONTEXT_COMPACTION_REMINDER_MESSAGE, "warning");
|
|
25
|
-
reminderShown = true;
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
}
|
|
1
|
+
import type { ExtensionAPI } from "@selesai/code";
|
|
2
|
+
|
|
3
|
+
export const CONTEXT_COMPACTION_REMINDER_THRESHOLD = 128_000;
|
|
4
|
+
export const CONTEXT_COMPACTION_REMINDER_MESSAGE =
|
|
5
|
+
`Conversation context has reached ${CONTEXT_COMPACTION_REMINDER_THRESHOLD.toLocaleString()} tokens. Run /handoff-new to summarize into a new chat, or enable Auto Handoff in Settings to do this automatically. Bigger context windows eat up tokens quickly and degrade AI quality.`;
|
|
6
|
+
|
|
7
|
+
export default function contextCompactionReminderExtension(pi: ExtensionAPI): void {
|
|
8
|
+
let reminderShown = false;
|
|
9
|
+
|
|
10
|
+
pi.on("session_compact", () => {
|
|
11
|
+
reminderShown = false;
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
pi.on("agent_settled", (_event, ctx) => {
|
|
15
|
+
const tokens = ctx.getContextUsage()?.tokens;
|
|
16
|
+
if (tokens === undefined || tokens === null) return;
|
|
17
|
+
|
|
18
|
+
if (tokens < CONTEXT_COMPACTION_REMINDER_THRESHOLD) {
|
|
19
|
+
reminderShown = false;
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (!reminderShown) {
|
|
24
|
+
ctx.ui.notify(CONTEXT_COMPACTION_REMINDER_MESSAGE, "warning");
|
|
25
|
+
reminderShown = true;
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { extractCosts, parseCostHeader } from "./cost-reconcile.ts";
|
|
3
|
+
|
|
4
|
+
describe("parseCostHeader", () => {
|
|
5
|
+
it("parses LiteLLM scientific-notation cost", () => {
|
|
6
|
+
expect(parseCostHeader("4.45284e-06")).toBe(4.45284e-6);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it("parses plain decimal cost", () => {
|
|
10
|
+
expect(parseCostHeader("0.00123")).toBe(0.00123);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("rejects null, garbage, and negatives", () => {
|
|
14
|
+
expect(parseCostHeader(null)).toBeUndefined();
|
|
15
|
+
expect(parseCostHeader("abc")).toBeUndefined();
|
|
16
|
+
expect(parseCostHeader("-1")).toBeUndefined();
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe("extractCosts", () => {
|
|
21
|
+
it("reads OpenRouter chat-completions usage cost from a final SSE chunk", () => {
|
|
22
|
+
const body =
|
|
23
|
+
'Data: {"id":"gen-abc","provider":"OpenAI"}\n\n' +
|
|
24
|
+
'data: {"id":"gen-abc","usage":{"prompt_tokens":10,"cost":0.0123},"choices":[]}\n\n' +
|
|
25
|
+
"data: [DONE]\n\n";
|
|
26
|
+
const { ids, cost } = extractCosts(body);
|
|
27
|
+
expect(ids).toContain("gen-abc");
|
|
28
|
+
expect(cost).toBe(0.0123);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("reads OpenRouter native total_cost from a JSON body", () => {
|
|
32
|
+
const { ids, cost } = extractCosts(
|
|
33
|
+
'{"id":"gen-xyz","total_cost":0.05152,"usage":{"total_tokens":330}}',
|
|
34
|
+
);
|
|
35
|
+
expect(ids).toEqual(["gen-xyz"]);
|
|
36
|
+
expect(cost).toBe(0.05152);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("reads object-shaped cost {total} payloads", () => {
|
|
40
|
+
const { cost } = extractCosts('{"id":"gen-obj","cost":{"input":0.01,"total":0.0456}}');
|
|
41
|
+
expect(cost).toBe(0.0456);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("keeps the last cost when multiple chunks carry usage", () => {
|
|
45
|
+
const body =
|
|
46
|
+
'data: {"id":"gen-1","usage":{"cost":0.01}}\n\ndata: {"id":"gen-1","usage":{"cost":0.02}}\n\n';
|
|
47
|
+
const { cost } = extractCosts(body);
|
|
48
|
+
expect(cost).toBe(0.02);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("returns no cost when the payload has none", () => {
|
|
52
|
+
const { ids, cost } = extractCosts('data: {"id":"resp-1","delta":"hi"}\n\n');
|
|
53
|
+
expect(ids).toContain("resp-1");
|
|
54
|
+
expect(cost).toBeUndefined();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("ignores negative or non-finite costs", () => {
|
|
58
|
+
const { cost } = extractCosts('{"id":"gen-neg","cost":-1}');
|
|
59
|
+
expect(cost).toBeUndefined();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("extracts ids from anthropic-style message_start without cost", () => {
|
|
63
|
+
const body =
|
|
64
|
+
'event: message_start\ndata: {"type":"message_start","message":{"id":"msg_01ABC","usage":{}}}\n\n' +
|
|
65
|
+
'event: message_delta\ndata: {"type":"message_delta","usage":{"output_tokens":15}}\n\n';
|
|
66
|
+
const { ids, cost } = extractCosts(body);
|
|
67
|
+
expect(ids).toContain("msg_01ABC");
|
|
68
|
+
expect(cost).toBeUndefined();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("caps captured ids", () => {
|
|
72
|
+
const body = Array.from({ length: 100 }, (_, i) => `{"id":"id-${i}"}`).join("\n");
|
|
73
|
+
const { ids } = extractCosts(body);
|
|
74
|
+
expect(ids.length).toBe(50);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cost Reconcile — records the real billed cost the provider reports in its
|
|
3
|
+
* own response payload, for any provider that includes one.
|
|
4
|
+
*
|
|
5
|
+
* pi-ai reads token counts from provider payloads but discards any
|
|
6
|
+
* provider-reported `cost` field, recomputing cost from the local rate card.
|
|
7
|
+
* For gateways (OpenRouter etc.) and custom catalog entries that bill with
|
|
8
|
+
* their own tokenizers or dated pricing, the estimate diverges from the bill.
|
|
9
|
+
*
|
|
10
|
+
* Extensions cannot hook the response body through the event API
|
|
11
|
+
* (`after_provider_response` exposes only status/headers), but provider SDKs
|
|
12
|
+
* fall back to `globalThis.fetch`. This extension installs a tee-wrapping
|
|
13
|
+
* fetch once per process: LLM API responses are scanned for a
|
|
14
|
+
* provider-reported cost (LiteLLM `x-litellm-response-cost` header,
|
|
15
|
+
* OpenRouter `usage.cost`, `total_cost`, plus object-shaped `cost.total`)
|
|
16
|
+
* and its response id, then recorded as a custom session entry on
|
|
17
|
+
* `message_end`.
|
|
18
|
+
*
|
|
19
|
+
* Providers whose payloads carry no cost (first-party OpenAI/Anthropic due to
|
|
20
|
+
* their usage APIs) simply record nothing — their rate-card estimate matches
|
|
21
|
+
* their bill anyway when catalog prices are configured.
|
|
22
|
+
*
|
|
23
|
+
* The zentui footer prefers reconciled entries over the rate-card total when
|
|
24
|
+
* both exist for the same response id.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import type { ExtensionAPI } from "@selesai/code";
|
|
28
|
+
|
|
29
|
+
const ENTRY_TYPE = "cost-reconcile";
|
|
30
|
+
const ENTRY_VERSION = 1 as const;
|
|
31
|
+
|
|
32
|
+
// Guard against pathological bodies; real usage chunks arrive well under 1MB.
|
|
33
|
+
const MAX_CAPTURE_BYTES = 4 * 1024 * 1024;
|
|
34
|
+
// Only LLM API paths are tee'd and scanned; unrelated fetches (web fetch tool,
|
|
35
|
+
// model catalog refreshes) pass through untouched.
|
|
36
|
+
const LLM_PATH_RE = /(\/chat\/completions|\/responses|\/messages\b|:streamGenerateContent)/;
|
|
37
|
+
const MAX_CAPTURED_IDS = 50;
|
|
38
|
+
const MAX_PENDING_LOOKUPS = 200;
|
|
39
|
+
const MAX_SESSION_ENTRIES = 2_000;
|
|
40
|
+
|
|
41
|
+
type ReconcileEntry = {
|
|
42
|
+
version: typeof ENTRY_VERSION;
|
|
43
|
+
provider: string;
|
|
44
|
+
model: string;
|
|
45
|
+
responseId: string;
|
|
46
|
+
/** Provider-reported billed cost in USD. */
|
|
47
|
+
cost: number;
|
|
48
|
+
reconciledAt: number;
|
|
49
|
+
source: "payload";
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Scan a captured SSE/JSON body for provider-reported cost and response ids.
|
|
54
|
+
* Exported for tests.
|
|
55
|
+
*
|
|
56
|
+
* In streams, usage lands in the final chunk, so the LAST cost match wins.
|
|
57
|
+
*/
|
|
58
|
+
type MessageInfo = {
|
|
59
|
+
provider: string;
|
|
60
|
+
model: string;
|
|
61
|
+
responseId: string;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// Process-wide capture state. The fetch patch is installed once per process
|
|
65
|
+
// and must not capture session-bound `pi`; per-session message_end handlers
|
|
66
|
+
// read these maps and write entries through their own session's appendEntry.
|
|
67
|
+
const processCaptured = new Map<string, number>();
|
|
68
|
+
const processWaiting = new Map<
|
|
69
|
+
string,
|
|
70
|
+
{ info: MessageInfo; write: (info: MessageInfo, cost: number) => void }
|
|
71
|
+
>();
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Scan a captured SSE/JSON body for provider-reported cost and response ids.
|
|
75
|
+
* Pure function; exported for tests.
|
|
76
|
+
*
|
|
77
|
+
* In streams, usage lands in the final chunk, so the LAST cost match wins.
|
|
78
|
+
*/
|
|
79
|
+
/**
|
|
80
|
+
* Parse a LiteLLM `x-litellm-response-cost` header value (a USD float,
|
|
81
|
+
* possibly in scientific notation). Exported for tests.
|
|
82
|
+
*/
|
|
83
|
+
export function parseCostHeader(value: string | null): number | undefined {
|
|
84
|
+
if (value === null) return undefined;
|
|
85
|
+
const cost = Number(value);
|
|
86
|
+
return Number.isFinite(cost) && cost >= 0 ? cost : undefined;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function extractCosts(
|
|
90
|
+
text: string,
|
|
91
|
+
): { ids: string[]; cost?: number } {
|
|
92
|
+
const ids = new Set<string>();
|
|
93
|
+
for (const match of text.matchAll(/"id"\s*:\s*"([^"]{1,200})"/g)) {
|
|
94
|
+
ids.add(match[1]);
|
|
95
|
+
if (ids.size >= MAX_CAPTURED_IDS) break;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// OpenRouter native API uses `total_cost`; chat-completions streams use
|
|
99
|
+
// `cost` (number or `{total}`); gateways behind the responses API have
|
|
100
|
+
// been observed with object-shaped cost too.
|
|
101
|
+
let cost: number | undefined;
|
|
102
|
+
for (const match of text.matchAll(
|
|
103
|
+
/"total_cost"\s*:\s*(-?[\d.eE+]+)|"cost"\s*:\s*(-?[\d.eE+]+)|"cost"\s*:\s*\{[^}]*?"total"\s*:\s*(-?[\d.eE+]+)/g,
|
|
104
|
+
)) {
|
|
105
|
+
const value = Number(match[1] ?? match[2] ?? match[3]);
|
|
106
|
+
if (Number.isFinite(value) && value >= 0) cost = value;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return { ids: [...ids], cost };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function isLlmUrl(input: unknown): boolean {
|
|
113
|
+
try {
|
|
114
|
+
const url =
|
|
115
|
+
typeof input === "string"
|
|
116
|
+
? input
|
|
117
|
+
: input instanceof URL
|
|
118
|
+
? input.href
|
|
119
|
+
: input instanceof Request
|
|
120
|
+
? input.url
|
|
121
|
+
: undefined;
|
|
122
|
+
if (!url) return false;
|
|
123
|
+
if (!/^https?:/i.test(url)) return false;
|
|
124
|
+
return LLM_PATH_RE.test(url);
|
|
125
|
+
} catch {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Install the capture fetch once per process, across extension reloads. */
|
|
131
|
+
const FETCH_PATCHED = Symbol.for("selesai.cost-reconcile.fetch-patched");
|
|
132
|
+
|
|
133
|
+
export default function costReconcileExtension(pi: ExtensionAPI): void {
|
|
134
|
+
/** Response ids already written to this session's file. */
|
|
135
|
+
const seen = new Set<string>();
|
|
136
|
+
let entriesWritten = 0;
|
|
137
|
+
|
|
138
|
+
const writeEntry = (info: MessageInfo, cost: number): void => {
|
|
139
|
+
if (seen.has(info.responseId) || entriesWritten >= MAX_SESSION_ENTRIES) return;
|
|
140
|
+
seen.add(info.responseId);
|
|
141
|
+
entriesWritten += 1;
|
|
142
|
+
const entry: ReconcileEntry = {
|
|
143
|
+
version: ENTRY_VERSION,
|
|
144
|
+
provider: info.provider,
|
|
145
|
+
model: info.model,
|
|
146
|
+
responseId: info.responseId,
|
|
147
|
+
cost,
|
|
148
|
+
reconciledAt: Date.now(),
|
|
149
|
+
source: "payload",
|
|
150
|
+
};
|
|
151
|
+
try {
|
|
152
|
+
pi.appendEntry(ENTRY_TYPE, entry);
|
|
153
|
+
} catch {
|
|
154
|
+
// transcript persistence failure must not break the session
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const finishCapture = (rawBody: string, headerCost?: number): void => {
|
|
159
|
+
// ponytail: one naive regex pass over the whole body; a streaming
|
|
160
|
+
// parser only matters if bodies grow past the 4MB cap.
|
|
161
|
+
const { ids, cost } = extractCosts(rawBody);
|
|
162
|
+
// LiteLLM's header is the amount the gateway billed; prefer it over
|
|
163
|
+
// any cost the upstream payload reports.
|
|
164
|
+
const effectiveCost = headerCost ?? cost;
|
|
165
|
+
if (effectiveCost === undefined) return;
|
|
166
|
+
for (const responseId of ids) {
|
|
167
|
+
processCaptured.set(responseId, effectiveCost);
|
|
168
|
+
const waiting = processWaiting.get(responseId);
|
|
169
|
+
if (waiting) {
|
|
170
|
+
processWaiting.delete(responseId);
|
|
171
|
+
waiting.write(waiting.info, effectiveCost);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (processCaptured.size > 500) {
|
|
175
|
+
for (const key of [...processCaptured.keys()].slice(0, processCaptured.size - 500))
|
|
176
|
+
processCaptured.delete(key);
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
pi.on("session_start", (_event, ctx) => {
|
|
181
|
+
try {
|
|
182
|
+
const entries =
|
|
183
|
+
(
|
|
184
|
+
ctx.sessionManager as {
|
|
185
|
+
getEntries?: () => readonly { type?: string; customType?: string; data?: ReconcileEntry }[];
|
|
186
|
+
}
|
|
187
|
+
).getEntries?.() ?? [];
|
|
188
|
+
for (const entry of entries) {
|
|
189
|
+
if (entry?.type !== "custom" || entry?.customType !== ENTRY_TYPE) continue;
|
|
190
|
+
const responseId = entry?.data?.responseId;
|
|
191
|
+
if (typeof responseId === "string") seen.add(responseId);
|
|
192
|
+
}
|
|
193
|
+
} catch {
|
|
194
|
+
// best effort only
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const globalFetch = globalThis as typeof globalThis & { [FETCH_PATCHED]?: boolean };
|
|
198
|
+
if (globalFetch[FETCH_PATCHED]) return;
|
|
199
|
+
globalFetch[FETCH_PATCHED] = true;
|
|
200
|
+
const realFetch = globalThis.fetch.bind(globalThis);
|
|
201
|
+
globalThis.fetch = async (input, init) => {
|
|
202
|
+
const response = await realFetch(input as Parameters<typeof realFetch>[0], init);
|
|
203
|
+
try {
|
|
204
|
+
if (!isLlmUrl(input)) return response;
|
|
205
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
206
|
+
if (!contentType.includes("json") && !contentType.includes("event-stream")) return response;
|
|
207
|
+
if (!response.body) return response;
|
|
208
|
+
const headerCost = parseCostHeader(response.headers.get("x-litellm-response-cost"));
|
|
209
|
+
const [main, tee] = response.body.tee();
|
|
210
|
+
void (async () => {
|
|
211
|
+
let raw = "";
|
|
212
|
+
try {
|
|
213
|
+
const reader = tee.getReader();
|
|
214
|
+
const decoder = new TextDecoder();
|
|
215
|
+
for (;;) {
|
|
216
|
+
const { done, value } = await reader.read();
|
|
217
|
+
if (done) break;
|
|
218
|
+
raw += decoder.decode(value, { stream: true });
|
|
219
|
+
if (raw.length > MAX_CAPTURE_BYTES) break;
|
|
220
|
+
}
|
|
221
|
+
} catch {
|
|
222
|
+
// capture is best-effort; the main branch is unaffected
|
|
223
|
+
}
|
|
224
|
+
finishCapture(raw, headerCost);
|
|
225
|
+
})();
|
|
226
|
+
// Preserve response identity as seen by the SDK: body swapped, rest identical.
|
|
227
|
+
return new Response(main, {
|
|
228
|
+
status: response.status,
|
|
229
|
+
statusText: response.statusText,
|
|
230
|
+
headers: response.headers,
|
|
231
|
+
});
|
|
232
|
+
} catch {
|
|
233
|
+
return response;
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
pi.on("message_end", (event) => {
|
|
239
|
+
const message = event.message as
|
|
240
|
+
| {
|
|
241
|
+
role?: string;
|
|
242
|
+
provider?: string;
|
|
243
|
+
model?: string;
|
|
244
|
+
responseId?: string;
|
|
245
|
+
stopReason?: string;
|
|
246
|
+
}
|
|
247
|
+
| undefined;
|
|
248
|
+
if (!message || message.role !== "assistant") return;
|
|
249
|
+
if (message.stopReason === "error" || message.stopReason === "aborted") return;
|
|
250
|
+
const responseId = message.responseId;
|
|
251
|
+
if (typeof responseId !== "string" || responseId.length === 0) return;
|
|
252
|
+
|
|
253
|
+
const info: MessageInfo = {
|
|
254
|
+
provider: message.provider ?? "",
|
|
255
|
+
model: message.responseModel ?? message.model ?? "",
|
|
256
|
+
responseId,
|
|
257
|
+
};
|
|
258
|
+
const cost = processCaptured.get(responseId);
|
|
259
|
+
if (cost !== undefined) {
|
|
260
|
+
writeEntry(info, cost);
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
// The tee branch may finish after message_end; remember the message so
|
|
264
|
+
// the capture can match it on completion and write via this session.
|
|
265
|
+
if (!seen.has(responseId) && !processWaiting.has(responseId)) {
|
|
266
|
+
waitingLimitGuard();
|
|
267
|
+
processWaiting.set(responseId, { info, write: writeEntry });
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// Trim oldest waiting entries so unclaimed captures cannot grow unbounded.
|
|
273
|
+
function waitingLimitGuard(): void {
|
|
274
|
+
if (processWaiting.size >= MAX_PENDING_LOOKUPS) {
|
|
275
|
+
for (const key of [...processWaiting.keys()].slice(0, processWaiting.size - MAX_PENDING_LOOKUPS + 1))
|
|
276
|
+
processWaiting.delete(key);
|
|
277
|
+
}
|
|
278
|
+
}
|