@chances-ai/engine 24.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agents/discover.d.ts +30 -0
- package/dist/agents/discover.d.ts.map +1 -0
- package/dist/agents/discover.js +183 -0
- package/dist/agents/discover.js.map +1 -0
- package/dist/agents/index.d.ts +20 -0
- package/dist/agents/index.d.ts.map +1 -0
- package/dist/agents/index.js +52 -0
- package/dist/agents/index.js.map +1 -0
- package/dist/agents/parse.d.ts +61 -0
- package/dist/agents/parse.d.ts.map +1 -0
- package/dist/agents/parse.js +527 -0
- package/dist/agents/parse.js.map +1 -0
- package/dist/agents/types.d.ts +52 -0
- package/dist/agents/types.d.ts.map +1 -0
- package/dist/agents/types.js +8 -0
- package/dist/agents/types.js.map +1 -0
- package/dist/ai/adapters/ai-sdk-stream.d.ts +19 -0
- package/dist/ai/adapters/ai-sdk-stream.d.ts.map +1 -0
- package/dist/ai/adapters/ai-sdk-stream.js +125 -0
- package/dist/ai/adapters/ai-sdk-stream.js.map +1 -0
- package/dist/ai/adapters/ai-sdk.d.ts +56 -0
- package/dist/ai/adapters/ai-sdk.d.ts.map +1 -0
- package/dist/ai/adapters/ai-sdk.js +112 -0
- package/dist/ai/adapters/ai-sdk.js.map +1 -0
- package/dist/ai/adapters/mock.d.ts +13 -0
- package/dist/ai/adapters/mock.d.ts.map +1 -0
- package/dist/ai/adapters/mock.js +54 -0
- package/dist/ai/adapters/mock.js.map +1 -0
- package/dist/ai/adapters/openai-compatible.d.ts +23 -0
- package/dist/ai/adapters/openai-compatible.d.ts.map +1 -0
- package/dist/ai/adapters/openai-compatible.js +45 -0
- package/dist/ai/adapters/openai-compatible.js.map +1 -0
- package/dist/ai/cost.d.ts +3 -0
- package/dist/ai/cost.d.ts.map +1 -0
- package/dist/ai/cost.js +5 -0
- package/dist/ai/cost.js.map +1 -0
- package/dist/ai/index.d.ts +12 -0
- package/dist/ai/index.d.ts.map +1 -0
- package/dist/ai/index.js +11 -0
- package/dist/ai/index.js.map +1 -0
- package/dist/ai/known-models.d.ts +20 -0
- package/dist/ai/known-models.d.ts.map +1 -0
- package/dist/ai/known-models.js +129 -0
- package/dist/ai/known-models.js.map +1 -0
- package/dist/ai/registry.d.ts +12 -0
- package/dist/ai/registry.d.ts.map +1 -0
- package/dist/ai/registry.js +24 -0
- package/dist/ai/registry.js.map +1 -0
- package/dist/ai/retry.d.ts +11 -0
- package/dist/ai/retry.d.ts.map +1 -0
- package/dist/ai/retry.js +14 -0
- package/dist/ai/retry.js.map +1 -0
- package/dist/ai/router.d.ts +25 -0
- package/dist/ai/router.d.ts.map +1 -0
- package/dist/ai/router.js +36 -0
- package/dist/ai/router.js.map +1 -0
- package/dist/ai/setup.d.ts +23 -0
- package/dist/ai/setup.d.ts.map +1 -0
- package/dist/ai/setup.js +47 -0
- package/dist/ai/setup.js.map +1 -0
- package/dist/ai/summarizer.d.ts +24 -0
- package/dist/ai/summarizer.d.ts.map +1 -0
- package/dist/ai/summarizer.js +56 -0
- package/dist/ai/summarizer.js.map +1 -0
- package/dist/ai/types.d.ts +83 -0
- package/dist/ai/types.d.ts.map +1 -0
- package/dist/ai/types.js +2 -0
- package/dist/ai/types.js.map +1 -0
- package/dist/core/compaction/circuit-breaker.d.ts +32 -0
- package/dist/core/compaction/circuit-breaker.d.ts.map +1 -0
- package/dist/core/compaction/circuit-breaker.js +42 -0
- package/dist/core/compaction/circuit-breaker.js.map +1 -0
- package/dist/core/compaction/compactor.d.ts +75 -0
- package/dist/core/compaction/compactor.d.ts.map +1 -0
- package/dist/core/compaction/compactor.js +261 -0
- package/dist/core/compaction/compactor.js.map +1 -0
- package/dist/core/compaction/estimate.d.ts +39 -0
- package/dist/core/compaction/estimate.d.ts.map +1 -0
- package/dist/core/compaction/estimate.js +74 -0
- package/dist/core/compaction/estimate.js.map +1 -0
- package/dist/core/compaction/index.d.ts +5 -0
- package/dist/core/compaction/index.d.ts.map +1 -0
- package/dist/core/compaction/index.js +5 -0
- package/dist/core/compaction/index.js.map +1 -0
- package/dist/core/compaction/prune.d.ts +43 -0
- package/dist/core/compaction/prune.d.ts.map +1 -0
- package/dist/core/compaction/prune.js +51 -0
- package/dist/core/compaction/prune.js.map +1 -0
- package/dist/core/engine.d.ts +268 -0
- package/dist/core/engine.d.ts.map +1 -0
- package/dist/core/engine.js +767 -0
- package/dist/core/engine.js.map +1 -0
- package/dist/core/index.d.ts +6 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +6 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/task-tool.d.ts +175 -0
- package/dist/core/task-tool.d.ts.map +1 -0
- package/dist/core/task-tool.js +901 -0
- package/dist/core/task-tool.js.map +1 -0
- package/dist/core/workspace-query.d.ts +83 -0
- package/dist/core/workspace-query.d.ts.map +1 -0
- package/dist/core/workspace-query.js +217 -0
- package/dist/core/workspace-query.js.map +1 -0
- package/dist/core/worktree/active-marker.d.ts +31 -0
- package/dist/core/worktree/active-marker.d.ts.map +1 -0
- package/dist/core/worktree/active-marker.js +109 -0
- package/dist/core/worktree/active-marker.js.map +1 -0
- package/dist/core/worktree/create.d.ts +40 -0
- package/dist/core/worktree/create.d.ts.map +1 -0
- package/dist/core/worktree/create.js +121 -0
- package/dist/core/worktree/create.js.map +1 -0
- package/dist/core/worktree/errors.d.ts +7 -0
- package/dist/core/worktree/errors.d.ts.map +1 -0
- package/dist/core/worktree/errors.js +11 -0
- package/dist/core/worktree/errors.js.map +1 -0
- package/dist/core/worktree/gc.d.ts +39 -0
- package/dist/core/worktree/gc.d.ts.map +1 -0
- package/dist/core/worktree/gc.js +146 -0
- package/dist/core/worktree/gc.js.map +1 -0
- package/dist/core/worktree/git.d.ts +53 -0
- package/dist/core/worktree/git.d.ts.map +1 -0
- package/dist/core/worktree/git.js +166 -0
- package/dist/core/worktree/git.js.map +1 -0
- package/dist/core/worktree/index.d.ts +8 -0
- package/dist/core/worktree/index.d.ts.map +1 -0
- package/dist/core/worktree/index.js +8 -0
- package/dist/core/worktree/index.js.map +1 -0
- package/dist/core/worktree/paths.d.ts +26 -0
- package/dist/core/worktree/paths.d.ts.map +1 -0
- package/dist/core/worktree/paths.js +57 -0
- package/dist/core/worktree/paths.js.map +1 -0
- package/dist/core/worktree/slug.d.ts +6 -0
- package/dist/core/worktree/slug.d.ts.map +1 -0
- package/dist/core/worktree/slug.js +21 -0
- package/dist/core/worktree/slug.js.map +1 -0
- package/dist/local-vault/file-store.d.ts +64 -0
- package/dist/local-vault/file-store.d.ts.map +1 -0
- package/dist/local-vault/file-store.js +225 -0
- package/dist/local-vault/file-store.js.map +1 -0
- package/dist/local-vault/index.d.ts +57 -0
- package/dist/local-vault/index.d.ts.map +1 -0
- package/dist/local-vault/index.js +68 -0
- package/dist/local-vault/index.js.map +1 -0
- package/dist/local-vault/keychain.d.ts +58 -0
- package/dist/local-vault/keychain.d.ts.map +1 -0
- package/dist/local-vault/keychain.js +200 -0
- package/dist/local-vault/keychain.js.map +1 -0
- package/dist/local-vault/mutex.d.ts +20 -0
- package/dist/local-vault/mutex.d.ts.map +1 -0
- package/dist/local-vault/mutex.js +44 -0
- package/dist/local-vault/mutex.js.map +1 -0
- package/dist/local-vault/passphrase.d.ts +30 -0
- package/dist/local-vault/passphrase.d.ts.map +1 -0
- package/dist/local-vault/passphrase.js +72 -0
- package/dist/local-vault/passphrase.js.map +1 -0
- package/dist/lsp/config.d.ts +34 -0
- package/dist/lsp/config.d.ts.map +1 -0
- package/dist/lsp/config.js +68 -0
- package/dist/lsp/config.js.map +1 -0
- package/dist/lsp/detect.d.ts +7 -0
- package/dist/lsp/detect.d.ts.map +1 -0
- package/dist/lsp/detect.js +78 -0
- package/dist/lsp/detect.js.map +1 -0
- package/dist/lsp/errors.d.ts +11 -0
- package/dist/lsp/errors.d.ts.map +1 -0
- package/dist/lsp/errors.js +11 -0
- package/dist/lsp/errors.js.map +1 -0
- package/dist/lsp/formatters.d.ts +147 -0
- package/dist/lsp/formatters.d.ts.map +1 -0
- package/dist/lsp/formatters.js +259 -0
- package/dist/lsp/formatters.js.map +1 -0
- package/dist/lsp/index.d.ts +31 -0
- package/dist/lsp/index.d.ts.map +1 -0
- package/dist/lsp/index.js +31 -0
- package/dist/lsp/index.js.map +1 -0
- package/dist/lsp/instance.d.ts +72 -0
- package/dist/lsp/instance.d.ts.map +1 -0
- package/dist/lsp/instance.js +489 -0
- package/dist/lsp/instance.js.map +1 -0
- package/dist/lsp/lazy-load.d.ts +27 -0
- package/dist/lsp/lazy-load.d.ts.map +1 -0
- package/dist/lsp/lazy-load.js +57 -0
- package/dist/lsp/lazy-load.js.map +1 -0
- package/dist/lsp/manager.d.ts +59 -0
- package/dist/lsp/manager.d.ts.map +1 -0
- package/dist/lsp/manager.js +242 -0
- package/dist/lsp/manager.js.map +1 -0
- package/dist/lsp/ops.d.ts +13 -0
- package/dist/lsp/ops.d.ts.map +1 -0
- package/dist/lsp/ops.js +225 -0
- package/dist/lsp/ops.js.map +1 -0
- package/dist/lsp/rpc.d.ts +47 -0
- package/dist/lsp/rpc.d.ts.map +1 -0
- package/dist/lsp/rpc.js +134 -0
- package/dist/lsp/rpc.js.map +1 -0
- package/dist/lsp/safe-uri.d.ts +18 -0
- package/dist/lsp/safe-uri.d.ts.map +1 -0
- package/dist/lsp/safe-uri.js +96 -0
- package/dist/lsp/safe-uri.js.map +1 -0
- package/dist/lsp/types.d.ts +70 -0
- package/dist/lsp/types.d.ts.map +1 -0
- package/dist/lsp/types.js +16 -0
- package/dist/lsp/types.js.map +1 -0
- package/dist/mcp/bridge.d.ts +57 -0
- package/dist/mcp/bridge.d.ts.map +1 -0
- package/dist/mcp/bridge.js +98 -0
- package/dist/mcp/bridge.js.map +1 -0
- package/dist/mcp/category.d.ts +22 -0
- package/dist/mcp/category.d.ts.map +1 -0
- package/dist/mcp/category.js +11 -0
- package/dist/mcp/category.js.map +1 -0
- package/dist/mcp/client.d.ts +228 -0
- package/dist/mcp/client.d.ts.map +1 -0
- package/dist/mcp/client.js +352 -0
- package/dist/mcp/client.js.map +1 -0
- package/dist/mcp/content.d.ts +86 -0
- package/dist/mcp/content.d.ts.map +1 -0
- package/dist/mcp/content.js +147 -0
- package/dist/mcp/content.js.map +1 -0
- package/dist/mcp/env.d.ts +19 -0
- package/dist/mcp/env.d.ts.map +1 -0
- package/dist/mcp/env.js +50 -0
- package/dist/mcp/env.js.map +1 -0
- package/dist/mcp/host.d.ts +199 -0
- package/dist/mcp/host.d.ts.map +1 -0
- package/dist/mcp/host.js +530 -0
- package/dist/mcp/host.js.map +1 -0
- package/dist/mcp/index.d.ts +18 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +17 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/load-mcp-host.d.ts +32 -0
- package/dist/mcp/load-mcp-host.d.ts.map +1 -0
- package/dist/mcp/load-mcp-host.js +49 -0
- package/dist/mcp/load-mcp-host.js.map +1 -0
- package/dist/mcp/oauth/callback-server.d.ts +73 -0
- package/dist/mcp/oauth/callback-server.d.ts.map +1 -0
- package/dist/mcp/oauth/callback-server.js +280 -0
- package/dist/mcp/oauth/callback-server.js.map +1 -0
- package/dist/mcp/oauth/config-hash.d.ts +24 -0
- package/dist/mcp/oauth/config-hash.d.ts.map +1 -0
- package/dist/mcp/oauth/config-hash.js +55 -0
- package/dist/mcp/oauth/config-hash.js.map +1 -0
- package/dist/mcp/oauth/error-normalize.d.ts +39 -0
- package/dist/mcp/oauth/error-normalize.d.ts.map +1 -0
- package/dist/mcp/oauth/error-normalize.js +91 -0
- package/dist/mcp/oauth/error-normalize.js.map +1 -0
- package/dist/mcp/oauth/provider.d.ts +190 -0
- package/dist/mcp/oauth/provider.d.ts.map +1 -0
- package/dist/mcp/oauth/provider.js +305 -0
- package/dist/mcp/oauth/provider.js.map +1 -0
- package/dist/mcp/oauth/refresh-coalescer.d.ts +46 -0
- package/dist/mcp/oauth/refresh-coalescer.d.ts.map +1 -0
- package/dist/mcp/oauth/refresh-coalescer.js +77 -0
- package/dist/mcp/oauth/refresh-coalescer.js.map +1 -0
- package/dist/mcp/oauth/sdk-shapes.d.ts +77 -0
- package/dist/mcp/oauth/sdk-shapes.d.ts.map +1 -0
- package/dist/mcp/oauth/sdk-shapes.js +21 -0
- package/dist/mcp/oauth/sdk-shapes.js.map +1 -0
- package/dist/mcp/parse.d.ts +28 -0
- package/dist/mcp/parse.d.ts.map +1 -0
- package/dist/mcp/parse.js +209 -0
- package/dist/mcp/parse.js.map +1 -0
- package/dist/mcp/prompts.d.ts +31 -0
- package/dist/mcp/prompts.d.ts.map +1 -0
- package/dist/mcp/prompts.js +71 -0
- package/dist/mcp/prompts.js.map +1 -0
- package/dist/mcp/redact.d.ts +62 -0
- package/dist/mcp/redact.d.ts.map +1 -0
- package/dist/mcp/redact.js +87 -0
- package/dist/mcp/redact.js.map +1 -0
- package/dist/mcp/resources.d.ts +70 -0
- package/dist/mcp/resources.d.ts.map +1 -0
- package/dist/mcp/resources.js +170 -0
- package/dist/mcp/resources.js.map +1 -0
- package/dist/mcp/types.d.ts +123 -0
- package/dist/mcp/types.d.ts.map +1 -0
- package/dist/mcp/types.js +2 -0
- package/dist/mcp/types.js.map +1 -0
- package/dist/memory/frontmatter.d.ts +18 -0
- package/dist/memory/frontmatter.d.ts.map +1 -0
- package/dist/memory/frontmatter.js +81 -0
- package/dist/memory/frontmatter.js.map +1 -0
- package/dist/memory/index.d.ts +5 -0
- package/dist/memory/index.d.ts.map +1 -0
- package/dist/memory/index.js +5 -0
- package/dist/memory/index.js.map +1 -0
- package/dist/memory/store.d.ts +44 -0
- package/dist/memory/store.d.ts.map +1 -0
- package/dist/memory/store.js +237 -0
- package/dist/memory/store.js.map +1 -0
- package/dist/memory/tools.d.ts +11 -0
- package/dist/memory/tools.d.ts.map +1 -0
- package/dist/memory/tools.js +159 -0
- package/dist/memory/tools.js.map +1 -0
- package/dist/memory/types.d.ts +32 -0
- package/dist/memory/types.d.ts.map +1 -0
- package/dist/memory/types.js +20 -0
- package/dist/memory/types.js.map +1 -0
- package/dist/plugin-api/index.d.ts +167 -0
- package/dist/plugin-api/index.d.ts.map +1 -0
- package/dist/plugin-api/index.js +151 -0
- package/dist/plugin-api/index.js.map +1 -0
- package/dist/plugin-logger/index.d.ts +21 -0
- package/dist/plugin-logger/index.d.ts.map +1 -0
- package/dist/plugin-logger/index.js +59 -0
- package/dist/plugin-logger/index.js.map +1 -0
- package/dist/session/index.d.ts +125 -0
- package/dist/session/index.d.ts.map +1 -0
- package/dist/session/index.js +202 -0
- package/dist/session/index.js.map +1 -0
- package/dist/tools/approval.d.ts +33 -0
- package/dist/tools/approval.d.ts.map +1 -0
- package/dist/tools/approval.js +53 -0
- package/dist/tools/approval.js.map +1 -0
- package/dist/tools/builtins/_shared.d.ts +94 -0
- package/dist/tools/builtins/_shared.d.ts.map +1 -0
- package/dist/tools/builtins/_shared.js +246 -0
- package/dist/tools/builtins/_shared.js.map +1 -0
- package/dist/tools/builtins/ask-user-question.d.ts +27 -0
- package/dist/tools/builtins/ask-user-question.d.ts.map +1 -0
- package/dist/tools/builtins/ask-user-question.js +191 -0
- package/dist/tools/builtins/ask-user-question.js.map +1 -0
- package/dist/tools/builtins/bash.d.ts +3 -0
- package/dist/tools/builtins/bash.d.ts.map +1 -0
- package/dist/tools/builtins/bash.js +158 -0
- package/dist/tools/builtins/bash.js.map +1 -0
- package/dist/tools/builtins/diff.d.ts +3 -0
- package/dist/tools/builtins/diff.d.ts.map +1 -0
- package/dist/tools/builtins/diff.js +83 -0
- package/dist/tools/builtins/diff.js.map +1 -0
- package/dist/tools/builtins/edit.d.ts +3 -0
- package/dist/tools/builtins/edit.d.ts.map +1 -0
- package/dist/tools/builtins/edit.js +40 -0
- package/dist/tools/builtins/edit.js.map +1 -0
- package/dist/tools/builtins/glob.d.ts +3 -0
- package/dist/tools/builtins/glob.d.ts.map +1 -0
- package/dist/tools/builtins/glob.js +37 -0
- package/dist/tools/builtins/glob.js.map +1 -0
- package/dist/tools/builtins/grep.d.ts +3 -0
- package/dist/tools/builtins/grep.d.ts.map +1 -0
- package/dist/tools/builtins/grep.js +81 -0
- package/dist/tools/builtins/grep.js.map +1 -0
- package/dist/tools/builtins/lsp.d.ts +3 -0
- package/dist/tools/builtins/lsp.d.ts.map +1 -0
- package/dist/tools/builtins/lsp.js +102 -0
- package/dist/tools/builtins/lsp.js.map +1 -0
- package/dist/tools/builtins/pty.d.ts +64 -0
- package/dist/tools/builtins/pty.d.ts.map +1 -0
- package/dist/tools/builtins/pty.js +536 -0
- package/dist/tools/builtins/pty.js.map +1 -0
- package/dist/tools/builtins/read.d.ts +3 -0
- package/dist/tools/builtins/read.d.ts.map +1 -0
- package/dist/tools/builtins/read.js +18 -0
- package/dist/tools/builtins/read.js.map +1 -0
- package/dist/tools/builtins/web-fetch.d.ts +4 -0
- package/dist/tools/builtins/web-fetch.d.ts.map +1 -0
- package/dist/tools/builtins/web-fetch.js +353 -0
- package/dist/tools/builtins/web-fetch.js.map +1 -0
- package/dist/tools/builtins/write.d.ts +3 -0
- package/dist/tools/builtins/write.d.ts.map +1 -0
- package/dist/tools/builtins/write.js +48 -0
- package/dist/tools/builtins/write.js.map +1 -0
- package/dist/tools/builtins.d.ts +9 -0
- package/dist/tools/builtins.d.ts.map +1 -0
- package/dist/tools/builtins.js +29 -0
- package/dist/tools/builtins.js.map +1 -0
- package/dist/tools/diff.d.ts +18 -0
- package/dist/tools/diff.d.ts.map +1 -0
- package/dist/tools/diff.js +57 -0
- package/dist/tools/diff.js.map +1 -0
- package/dist/tools/index.d.ts +10 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +9 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/permission.d.ts +120 -0
- package/dist/tools/permission.d.ts.map +1 -0
- package/dist/tools/permission.js +208 -0
- package/dist/tools/permission.js.map +1 -0
- package/dist/tools/registry.d.ts +12 -0
- package/dist/tools/registry.d.ts.map +1 -0
- package/dist/tools/registry.js +19 -0
- package/dist/tools/registry.js.map +1 -0
- package/dist/tools/types.d.ts +244 -0
- package/dist/tools/types.d.ts.map +1 -0
- package/dist/tools/types.js +15 -0
- package/dist/tools/types.js.map +1 -0
- package/package.json +109 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import type { EventBus, Logger, JSONValue } from "@chances-ai/runtime";
|
|
2
|
+
import type { Tool, ToolRegistry } from "../tools/index.js";
|
|
3
|
+
/** Re-exported so plugin authors can write `import type { Tool } from
|
|
4
|
+
* "@chances-ai/plugin-api"` and skip a second workspace dep. The types still
|
|
5
|
+
* physically live in `@chances-ai/tools`. */
|
|
6
|
+
export type { Tool, ToolCategory, ToolContext, ToolResult } from "../tools/index.js";
|
|
7
|
+
/**
|
|
8
|
+
* Hooks let plugins observe/mutate the turn lifecycle. Built-in features use the
|
|
9
|
+
* exact same map as third-party plugins — there is no second-class plugin API.
|
|
10
|
+
*/
|
|
11
|
+
export interface HookMap {
|
|
12
|
+
beforePrompt?: (input: {
|
|
13
|
+
prompt: string;
|
|
14
|
+
}) => void | Promise<void>;
|
|
15
|
+
beforeToolCall?: (input: {
|
|
16
|
+
name: string;
|
|
17
|
+
args: JSONValue;
|
|
18
|
+
}) => void | Promise<void>;
|
|
19
|
+
afterToolCall?: (input: {
|
|
20
|
+
name: string;
|
|
21
|
+
ok: boolean;
|
|
22
|
+
output: string;
|
|
23
|
+
}) => void | Promise<void>;
|
|
24
|
+
afterResponse?: (input: {
|
|
25
|
+
text: string;
|
|
26
|
+
}) => void | Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Opens a modal overlay in the host TUI and resolves once the user makes a
|
|
30
|
+
* selection (or cancels with `null`). The `render` factory is called by the
|
|
31
|
+
* host and should return a TUI element (e.g. an Ink JSX element) wired to
|
|
32
|
+
* call `resolve(value)` on user input. The type `unknown` for the return
|
|
33
|
+
* keeps plugin-api free of `react`/`ink` deps — the host is responsible for
|
|
34
|
+
* the actual rendering. When `openModal` is absent on the SlashContext, the
|
|
35
|
+
* dispatcher is running in text mode (`-p` / non-TTY); slash commands must
|
|
36
|
+
* fall back to a non-interactive code path.
|
|
37
|
+
*/
|
|
38
|
+
export type OpenModal = <T>(render: (resolve: (value: T | null) => void) => unknown) => Promise<T | null>;
|
|
39
|
+
/**
|
|
40
|
+
* What a slash command receives at dispatch time. Extends PluginContext with
|
|
41
|
+
* an optional TUI-only `openModal`. Text-mode dispatch (`chances -p "/foo"`)
|
|
42
|
+
* leaves it undefined; the slash command branches on its presence.
|
|
43
|
+
*/
|
|
44
|
+
export interface SlashContext extends PluginContext {
|
|
45
|
+
openModal?: OpenModal;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* (5.4) Richer slash result. A command returns this (instead of a bare string)
|
|
49
|
+
* when it wants to **submit a prompt as a model turn** rather than just display
|
|
50
|
+
* output — the MCP prompt commands (`/<server>:<prompt>`) use `submitPrompt` to
|
|
51
|
+
* feed the server's rendered prompt into `engine.runTurn`. `output` is the
|
|
52
|
+
* display line (same semantics as returning a string). Returning a plain
|
|
53
|
+
* `string` is still the common case and is unchanged.
|
|
54
|
+
*/
|
|
55
|
+
export interface SlashRunResult {
|
|
56
|
+
/** Display line (info, unless the command threw). */
|
|
57
|
+
output?: string;
|
|
58
|
+
/** When set, the entry point runs a model turn with this text AFTER the
|
|
59
|
+
* slash dispatch returns. The text is NOT mention-expanded (it may be
|
|
60
|
+
* untrusted MCP-server output — codex 5.4 R1 M3). */
|
|
61
|
+
submitPrompt?: string;
|
|
62
|
+
}
|
|
63
|
+
export interface SlashCommand {
|
|
64
|
+
name: string;
|
|
65
|
+
description: string;
|
|
66
|
+
/** (5.10 A3) Ghost-text shown after the name in the slash typeahead, e.g.
|
|
67
|
+
* `"<provider> <url>"`. Display-only — does not affect `run`'s `args`. */
|
|
68
|
+
argumentHint?: string;
|
|
69
|
+
/** (5.10 A3) Hide from the typeahead menu (still dispatchable if typed in
|
|
70
|
+
* full). For internal/rarely-typed commands. */
|
|
71
|
+
isHidden?: boolean;
|
|
72
|
+
/** (5.10 A3) Alternate names. Reserved metadata — alias *matching* is not yet
|
|
73
|
+
* wired into the typeahead (additive; lands when a command needs it). */
|
|
74
|
+
aliases?: string[];
|
|
75
|
+
/** Optional return value is displayed by the dispatcher (TUI info line, CLI
|
|
76
|
+
* stdout). Commands that emit their own output via `ctx.bus`/`ctx.logger`
|
|
77
|
+
* can return void. Return a {@link SlashRunResult} to submit a model turn
|
|
78
|
+
* (`submitPrompt`) in addition to / instead of display output. To signal
|
|
79
|
+
* failure, throw — the dispatcher catches it and surfaces the message with
|
|
80
|
+
* `isError: true`. The `ctx.openModal` field is present only when running
|
|
81
|
+
* inside a TUI host; text-mode dispatch leaves it undefined so commands can
|
|
82
|
+
* fall back gracefully. */
|
|
83
|
+
run(args: string[], ctx: SlashContext): string | void | SlashRunResult | Promise<string | void | SlashRunResult>;
|
|
84
|
+
}
|
|
85
|
+
/** Splits a raw input line like `"/echo hi there "` into `{ name, args }`,
|
|
86
|
+
* tolerating leading whitespace after the slash and stray internal/trailing
|
|
87
|
+
* whitespace. An input of just `"/"` produces `{ name: "", args: [] }`; callers
|
|
88
|
+
* should treat an empty name as "unknown command". */
|
|
89
|
+
export declare function parseSlashInput(line: string): {
|
|
90
|
+
name: string;
|
|
91
|
+
args: string[];
|
|
92
|
+
};
|
|
93
|
+
export interface SlashDispatchResult {
|
|
94
|
+
/** True when a command was found and ran (even if it threw or returned an error string). */
|
|
95
|
+
ok: boolean;
|
|
96
|
+
/** Optional display output; undefined when the command emitted via bus/logger only. */
|
|
97
|
+
output?: string;
|
|
98
|
+
/** True when `output` should be styled as an error. */
|
|
99
|
+
isError?: boolean;
|
|
100
|
+
/** (5.4) When set, the entry point should run a model turn with this text
|
|
101
|
+
* (NOT mention-expanded — codex R1 M3). Carried from `SlashRunResult`. */
|
|
102
|
+
submitPrompt?: string;
|
|
103
|
+
}
|
|
104
|
+
/** What a plugin receives at load time. */
|
|
105
|
+
export interface PluginContext {
|
|
106
|
+
bus: EventBus;
|
|
107
|
+
logger: Logger;
|
|
108
|
+
registerHook<K extends keyof HookMap>(name: K, handler: NonNullable<HookMap[K]>): void;
|
|
109
|
+
registerSlashCommand(command: SlashCommand): void;
|
|
110
|
+
/** Registers a tool that the engine can dispatch like a built-in. Names share
|
|
111
|
+
* one namespace with built-ins; the last registration wins. Throws if the
|
|
112
|
+
* PluginHost was constructed without a ToolRegistry. */
|
|
113
|
+
registerTool(tool: Tool): void;
|
|
114
|
+
}
|
|
115
|
+
export interface Plugin {
|
|
116
|
+
name: string;
|
|
117
|
+
version?: string;
|
|
118
|
+
onLoad(ctx: PluginContext): void | Promise<void>;
|
|
119
|
+
onDispose?(): void | Promise<void>;
|
|
120
|
+
}
|
|
121
|
+
/** Identity helper that gives plugin authors type inference. */
|
|
122
|
+
export declare function definePlugin(plugin: Plugin): Plugin;
|
|
123
|
+
/** Collects hooks/commands/tools from plugins and runs hook chains in registration order. */
|
|
124
|
+
export declare class PluginHost {
|
|
125
|
+
private readonly bus;
|
|
126
|
+
private readonly logger;
|
|
127
|
+
private readonly tools?;
|
|
128
|
+
private readonly plugins;
|
|
129
|
+
private readonly hooks;
|
|
130
|
+
private readonly commands;
|
|
131
|
+
/** `tools` is optional so test fixtures don't have to wire one up; callers
|
|
132
|
+
* whose plugins use `registerTool` must supply it. */
|
|
133
|
+
constructor(bus: EventBus, logger: Logger, tools?: ToolRegistry | undefined);
|
|
134
|
+
/** Builds a PluginContext. When an `undos` array is passed, every mutation
|
|
135
|
+
* pushes a rollback into it so `load()` can unwind on a throw; the default
|
|
136
|
+
* empty array means "no rollback" (used by `dispatchSlash`, where slash
|
|
137
|
+
* commands intentionally keep any state they register). */
|
|
138
|
+
private context;
|
|
139
|
+
/**
|
|
140
|
+
* Loads a plugin. If `onLoad` throws, any hooks/commands the plugin managed
|
|
141
|
+
* to register before the failure are rolled back so the host is never left in
|
|
142
|
+
* a partially-initialized state.
|
|
143
|
+
*/
|
|
144
|
+
load(plugin: Plugin): Promise<void>;
|
|
145
|
+
runHook<K extends keyof HookMap>(name: K, input: Parameters<NonNullable<HookMap[K]>>[0]): Promise<void>;
|
|
146
|
+
slashCommand(name: string): SlashCommand | undefined;
|
|
147
|
+
slashCommands(): SlashCommand[];
|
|
148
|
+
/**
|
|
149
|
+
* Runs a registered slash command. Returns `{ ok: false }` when no command
|
|
150
|
+
* matches the name. A command that throws is reported as `{ ok: true, output:
|
|
151
|
+
* "...", isError: true }` so the caller can render the failure inline without
|
|
152
|
+
* the command derailing the host process.
|
|
153
|
+
*
|
|
154
|
+
* `tui` is the TUI host's modal hook — pass it when dispatching from an Ink
|
|
155
|
+
* loop so slash commands can open pickers; omit it for `-p` / non-TTY
|
|
156
|
+
* dispatch and commands will detect text-mode via the missing
|
|
157
|
+
* `ctx.openModal` and fall back accordingly.
|
|
158
|
+
*/
|
|
159
|
+
dispatchSlash(name: string, args: string[], tui?: {
|
|
160
|
+
openModal: OpenModal;
|
|
161
|
+
}): Promise<SlashDispatchResult>;
|
|
162
|
+
/** Disposes plugins in reverse-load order. One plugin's failing `onDispose`
|
|
163
|
+
* must not prevent later plugins from running theirs — we swallow + log each
|
|
164
|
+
* failure individually. */
|
|
165
|
+
dispose(): Promise<void>;
|
|
166
|
+
}
|
|
167
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/plugin-api/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACvE,OAAO,KAAK,EAAE,IAAI,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAE5D;;6CAE6C;AAC7C,YAAY,EAAE,IAAI,EAAE,YAAY,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAErF;;;GAGG;AACH,MAAM,WAAW,OAAO;IACtB,YAAY,CAAC,EA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internal/trailing
|
|
3
|
+
* whitespace. An input of just `"/"` produces `{ name: "", args: [] }`; callers
|
|
4
|
+
* should treat an empty name as "unknown command". */
|
|
5
|
+
export function parseSlashInput(line) {
|
|
6
|
+
const parts = line.replace(/^\//, "").trim().split(/\s+/).filter(Boolean);
|
|
7
|
+
return { name: parts[0] ?? "", args: parts.slice(1) };
|
|
8
|
+
}
|
|
9
|
+
/** Identity helper that gives plugin authors type inference. */
|
|
10
|
+
export function definePlugin(plugin) {
|
|
11
|
+
return plugin;
|
|
12
|
+
}
|
|
13
|
+
/** Collects hooks/commands/tools from plugins and runs hook chains in registration order. */
|
|
14
|
+
export class PluginHost {
|
|
15
|
+
bus;
|
|
16
|
+
logger;
|
|
17
|
+
tools;
|
|
18
|
+
plugins = [];
|
|
19
|
+
hooks = {};
|
|
20
|
+
commands = new Map();
|
|
21
|
+
/** `tools` is optional so test fixtures don't have to wire one up; callers
|
|
22
|
+
* whose plugins use `registerTool` must supply it. */
|
|
23
|
+
constructor(bus, logger, tools) {
|
|
24
|
+
this.bus = bus;
|
|
25
|
+
this.logger = logger;
|
|
26
|
+
this.tools = tools;
|
|
27
|
+
}
|
|
28
|
+
/** Builds a PluginContext. When an `undos` array is passed, every mutation
|
|
29
|
+
* pushes a rollback into it so `load()` can unwind on a throw; the default
|
|
30
|
+
* empty array means "no rollback" (used by `dispatchSlash`, where slash
|
|
31
|
+
* commands intentionally keep any state they register). */
|
|
32
|
+
context(undos = []) {
|
|
33
|
+
return {
|
|
34
|
+
bus: this.bus,
|
|
35
|
+
logger: this.logger,
|
|
36
|
+
registerHook: (name, handler) => {
|
|
37
|
+
const arr = (this.hooks[name] ??= []);
|
|
38
|
+
arr.push(handler);
|
|
39
|
+
undos.push(() => {
|
|
40
|
+
const i = arr.indexOf(handler);
|
|
41
|
+
if (i >= 0)
|
|
42
|
+
arr.splice(i, 1);
|
|
43
|
+
});
|
|
44
|
+
},
|
|
45
|
+
registerSlashCommand: (command) => {
|
|
46
|
+
const prior = this.commands.get(command.name);
|
|
47
|
+
this.commands.set(command.name, command);
|
|
48
|
+
undos.push(() => {
|
|
49
|
+
if (this.commands.get(command.name) === command) {
|
|
50
|
+
if (prior)
|
|
51
|
+
this.commands.set(command.name, prior);
|
|
52
|
+
else
|
|
53
|
+
this.commands.delete(command.name);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
},
|
|
57
|
+
registerTool: (tool) => {
|
|
58
|
+
if (!this.tools) {
|
|
59
|
+
throw new Error("PluginHost was built without a ToolRegistry; cannot register tools");
|
|
60
|
+
}
|
|
61
|
+
const registry = this.tools;
|
|
62
|
+
const prior = registry.get(tool.name);
|
|
63
|
+
registry.register(tool);
|
|
64
|
+
undos.push(() => {
|
|
65
|
+
if (registry.get(tool.name) === tool) {
|
|
66
|
+
if (prior)
|
|
67
|
+
registry.register(prior);
|
|
68
|
+
else
|
|
69
|
+
registry.unregister(tool.name);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Loads a plugin. If `onLoad` throws, any hooks/commands the plugin managed
|
|
77
|
+
* to register before the failure are rolled back so the host is never left in
|
|
78
|
+
* a partially-initialized state.
|
|
79
|
+
*/
|
|
80
|
+
async load(plugin) {
|
|
81
|
+
const undos = [];
|
|
82
|
+
try {
|
|
83
|
+
await plugin.onLoad(this.context(undos));
|
|
84
|
+
this.plugins.push(plugin);
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
for (const undo of undos.reverse())
|
|
88
|
+
undo();
|
|
89
|
+
throw e;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async runHook(name, input) {
|
|
93
|
+
for (const handler of this.hooks[name] ?? []) {
|
|
94
|
+
await handler(input);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
slashCommand(name) {
|
|
98
|
+
return this.commands.get(name);
|
|
99
|
+
}
|
|
100
|
+
slashCommands() {
|
|
101
|
+
return [...this.commands.values()];
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Runs a registered slash command. Returns `{ ok: false }` when no command
|
|
105
|
+
* matches the name. A command that throws is reported as `{ ok: true, output:
|
|
106
|
+
* "...", isError: true }` so the caller can render the failure inline without
|
|
107
|
+
* the command derailing the host process.
|
|
108
|
+
*
|
|
109
|
+
* `tui` is the TUI host's modal hook — pass it when dispatching from an Ink
|
|
110
|
+
* loop so slash commands can open pickers; omit it for `-p` / non-TTY
|
|
111
|
+
* dispatch and commands will detect text-mode via the missing
|
|
112
|
+
* `ctx.openModal` and fall back accordingly.
|
|
113
|
+
*/
|
|
114
|
+
async dispatchSlash(name, args, tui) {
|
|
115
|
+
const cmd = this.commands.get(name);
|
|
116
|
+
if (!cmd)
|
|
117
|
+
return { ok: false };
|
|
118
|
+
try {
|
|
119
|
+
const ctx = { ...this.context(), openModal: tui?.openModal };
|
|
120
|
+
const result = await cmd.run(args, ctx);
|
|
121
|
+
// (5.4) Three return shapes: string (display), void (self-emitted), or
|
|
122
|
+
// SlashRunResult ({output?, submitPrompt?}). Map them onto the dispatch
|
|
123
|
+
// result; legacy string/void callers are unaffected.
|
|
124
|
+
if (typeof result === "string")
|
|
125
|
+
return { ok: true, output: result };
|
|
126
|
+
if (result && typeof result === "object") {
|
|
127
|
+
return { ok: true, output: result.output, submitPrompt: result.submitPrompt };
|
|
128
|
+
}
|
|
129
|
+
return { ok: true, output: undefined };
|
|
130
|
+
}
|
|
131
|
+
catch (e) {
|
|
132
|
+
const message = e instanceof Error ? e.message : String(e);
|
|
133
|
+
this.logger.warn(`slash /${name} failed: ${message}`);
|
|
134
|
+
return { ok: true, output: message, isError: true };
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/** Disposes plugins in reverse-load order. One plugin's failing `onDispose`
|
|
138
|
+
* must not prevent later plugins from running theirs — we swallow + log each
|
|
139
|
+
* failure individually. */
|
|
140
|
+
async dispose() {
|
|
141
|
+
for (const p of [...this.plugins].reverse()) {
|
|
142
|
+
try {
|
|
143
|
+
await p.onDispose?.();
|
|
144
|
+
}
|
|
145
|
+
catch (e) {
|
|
146
|
+
this.logger.warn(`plugin ${p.name} onDispose failed: ${e.message}`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/plugin-api/index.ts"],"names":[],"mappings":"AAqFA;;;sDAGsD;AACtD,MAAM,UAAU,eAAe,CAAC,IAAY;IAC1C,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAC1E,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;AACxD,CAAC;AAiCD,gEAAgE;AAChE,MAAM,UAAU,YAAY,CAAC,MAAc;IACzC,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,6FAA6F;AAC7F,MAAM,OAAO,UAAU;IAQF;IACA;IACA;IATF,OAAO,GAAa,EAAE,CAAC;IACvB,KAAK,GAAwD,EAAE,CAAC;IAChE,QAAQ,GAAG,IAAI,GAAG,EAAwB,CAAC;IAE5D;0DACsD;IACtD,YACmB,GAAa,EACb,MAAc,EACd,KAAoB;QAFpB,QAAG,GAAH,GAAG,CAAU;QACb,WAAM,GAAN,MAAM,CAAQ;QACd,UAAK,GAAL,KAAK,CAAe;IACpC,CAAC;IAEJ;;;+DAG2D;IACnD,OAAO,CAAC,QAA2B,EAAE;QAC3C,OAAO;YACL,GAAG,EAAE,IAAI,CAAC,GAAG;YACb,MAAM,EAAE,IAAI,CAAC,MAAM;Y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M,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;QACtD,CAAC;IACH,CAAC;IAED;;+BAE2B;IAC3B,KAAK,CAAC,OAAO;QACX,KAAK,MAAM,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC;YAC5C,IAAI,CAAC;gBACH,MAAM,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC;YACxB,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACX,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,IAAI,sBAAuB,CAAW,CAAC,OAAO,EAAE,CAAC,CAAC;YACjF,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type Plugin } from "../plugin-api/index.js";
|
|
2
|
+
export interface LoggerPluginOptions {
|
|
3
|
+
/** Absolute path of the transcript file to append to. */
|
|
4
|
+
path: string;
|
|
5
|
+
/** Max chars per logged prompt / tool output / assistant reply. Default 500. */
|
|
6
|
+
previewChars?: number;
|
|
7
|
+
/** DI seam: lets tests capture lines without touching disk. */
|
|
8
|
+
appender?: (line: string) => void;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Reference plugin: appends a human-readable transcript of each turn — prompts,
|
|
12
|
+
* tool calls, tool results, and assistant replies — to a configured file.
|
|
13
|
+
*
|
|
14
|
+
* Exists primarily as a worked example of the plugin API:
|
|
15
|
+
* - exercises every entry in HookMap;
|
|
16
|
+
* - registers a slash command (`/transcript`);
|
|
17
|
+
* - uses dependency injection (the `appender` option) so the hooks can be
|
|
18
|
+
* tested without a real filesystem.
|
|
19
|
+
*/
|
|
20
|
+
export declare function loggerPlugin(options: LoggerPluginOptions): Plugin;
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/plugin-logger/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAgB,KAAK,MAAM,EAAsB,MAAM,wBAAwB,CAAC;AAGvF,MAAM,WAAW,mBAAmB;IAClC,yDAAyD;IACzD,IAAI,EAAE,MAAM,CAAC;IACb,gFAAgF;IAChF,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,+DAA+D;IAC/D,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;CACnC;AAED;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,mBAAmB,GAAG,MAAM,CAkCjE"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { appendFileSync, mkdirSync } from "node:fs";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import { definePlugin } from "../plugin-api/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* Reference plugin: appends a human-readable transcript of each turn — prompts,
|
|
6
|
+
* tool calls, tool results, and assistant replies — to a configured file.
|
|
7
|
+
*
|
|
8
|
+
* Exists primarily as a worked example of the plugin API:
|
|
9
|
+
* - exercises every entry in HookMap;
|
|
10
|
+
* - registers a slash command (`/transcript`);
|
|
11
|
+
* - uses dependency injection (the `appender` option) so the hooks can be
|
|
12
|
+
* tested without a real filesystem.
|
|
13
|
+
*/
|
|
14
|
+
export function loggerPlugin(options) {
|
|
15
|
+
const previewLen = options.previewChars ?? 500;
|
|
16
|
+
const append = options.appender ?? defaultAppender(options.path);
|
|
17
|
+
const stamp = () => new Date().toISOString();
|
|
18
|
+
return definePlugin({
|
|
19
|
+
name: "logger",
|
|
20
|
+
version: "1.0.0",
|
|
21
|
+
onLoad(ctx) {
|
|
22
|
+
append(`\n=== Session opened ${stamp()} ===\n`);
|
|
23
|
+
ctx.registerHook("beforePrompt", ({ prompt }) => {
|
|
24
|
+
append(`[${stamp()}] > ${oneLine(prompt, previewLen)}\n`);
|
|
25
|
+
});
|
|
26
|
+
ctx.registerHook("beforeToolCall", ({ name, args }) => {
|
|
27
|
+
append(`[${stamp()}] tool→ ${name} ${jsonPreview(args)}\n`);
|
|
28
|
+
});
|
|
29
|
+
ctx.registerHook("afterToolCall", ({ name, ok, output }) => {
|
|
30
|
+
append(`[${stamp()}] tool← ${name} ${ok ? "ok" : "fail"}: ${oneLine(output, previewLen)}\n`);
|
|
31
|
+
});
|
|
32
|
+
ctx.registerHook("afterResponse", ({ text }) => {
|
|
33
|
+
append(`[${stamp()}] < ${oneLine(text, previewLen)}\n`);
|
|
34
|
+
});
|
|
35
|
+
ctx.registerSlashCommand({
|
|
36
|
+
name: "transcript",
|
|
37
|
+
description: "Print the transcript file path",
|
|
38
|
+
run: () => `Transcript: ${options.path}`,
|
|
39
|
+
});
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function defaultAppender(path) {
|
|
44
|
+
return (line) => {
|
|
45
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
46
|
+
appendFileSync(path, line);
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function oneLine(text, max) {
|
|
50
|
+
const flat = text.replace(/\s+/g, " ").trim();
|
|
51
|
+
return flat.length > max ? `${flat.slice(0, max - 1)}…` : flat;
|
|
52
|
+
}
|
|
53
|
+
function jsonPreview(value) {
|
|
54
|
+
const s = JSON.stringify(value);
|
|
55
|
+
if (!s)
|
|
56
|
+
return "{}";
|
|
57
|
+
return s.length > 200 ? `${s.slice(0, 199)}…` : s;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/plugin-logger/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,YAAY,EAAmC,MAAM,wBAAwB,CAAC;AAYvF;;;;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type { Message } from "../ai/index.js";
|
|
2
|
+
export interface TurnRecord {
|
|
3
|
+
turnId: string;
|
|
4
|
+
at: string;
|
|
5
|
+
messages: Message[];
|
|
6
|
+
}
|
|
7
|
+
export interface SessionState {
|
|
8
|
+
id: string;
|
|
9
|
+
title: string;
|
|
10
|
+
createdAt: string;
|
|
11
|
+
updatedAt: string;
|
|
12
|
+
turns: TurnRecord[];
|
|
13
|
+
/** Last model the user was on for this session. Restored on resume so the
|
|
14
|
+
* next turn after a `/resume` reuses what the user actually picked, not the
|
|
15
|
+
* global default. Optional so v2.x sessions still load (the field is missing
|
|
16
|
+
* from older files). */
|
|
17
|
+
model?: {
|
|
18
|
+
provider?: string;
|
|
19
|
+
model?: string;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** Memento: a point-in-time snapshot the session can be restored to. */
|
|
23
|
+
export interface Checkpoint {
|
|
24
|
+
turnIndex: number;
|
|
25
|
+
state: SessionState;
|
|
26
|
+
}
|
|
27
|
+
/** Persists sessions as JSON under <workspaceRoot>/.chances/sessions. */
|
|
28
|
+
export declare class SessionStore {
|
|
29
|
+
private readonly dir;
|
|
30
|
+
constructor(workspaceRoot: string);
|
|
31
|
+
save(state: SessionState): void;
|
|
32
|
+
load(id: string): SessionState | undefined;
|
|
33
|
+
list(): SessionState[];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Owns the session lifecycle (state machine): create/resume, append turns,
|
|
37
|
+
* flatten to a provider message list, checkpoint and restore.
|
|
38
|
+
*/
|
|
39
|
+
export declare class SessionManager {
|
|
40
|
+
private readonly store?;
|
|
41
|
+
private state;
|
|
42
|
+
private constructor();
|
|
43
|
+
static create(title?: string, store?: SessionStore): SessionManager;
|
|
44
|
+
static resume(id: string, store: SessionStore): SessionManager | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* (5.5) Creates a NEW session pre-seeded with a deep clone of `parent`'s
|
|
47
|
+
* completed turns — the seam behind the `task` tool's `context: "fork"` mode.
|
|
48
|
+
*
|
|
49
|
+
* Three invariants make this safe:
|
|
50
|
+
* - **New id, no `SessionStore`.** The child is a distinct session that can
|
|
51
|
+
* never `save()` over the parent's `<id>.json` on disk (mirrors
|
|
52
|
+
* `create("subagent")`, which also omits the store). All mutation stays in
|
|
53
|
+
* the child's own in-memory state.
|
|
54
|
+
* - **Whole turns, never a mid-turn slice.** chances-cli persists an
|
|
55
|
+
* assistant message carrying `tool-call` parts and its `tool` results in
|
|
56
|
+
* one atomic turn (see `engine.ts` end-of-turn `appendTurn`), so copying at
|
|
57
|
+
* turn granularity keeps every `tool-call` paired with its result — the
|
|
58
|
+
* provider's no-orphan-`tool_use` invariant holds for free.
|
|
59
|
+
* - **The session `model` field is NOT carried.** The forked child's model is
|
|
60
|
+
* chosen by the caller (persona override or inherited parent selection),
|
|
61
|
+
* not by the parent session's last `/model` choice. `snapshot()` deep-clones
|
|
62
|
+
* the whole state; we keep only `turns`.
|
|
63
|
+
*/
|
|
64
|
+
static forkFrom(parent: SessionManager, title?: string): SessionManager;
|
|
65
|
+
get id(): string;
|
|
66
|
+
/** All messages across turns, ready to send to a provider. */
|
|
67
|
+
messages(): Message[];
|
|
68
|
+
appendTurn(messages: Message[]): TurnRecord;
|
|
69
|
+
/** Records the user's current model choice so `/resume` can restore it.
|
|
70
|
+
* Persisted immediately; not gated behind the next `appendTurn`. */
|
|
71
|
+
setModel(model: {
|
|
72
|
+
provider?: string;
|
|
73
|
+
model?: string;
|
|
74
|
+
} | undefined): void;
|
|
75
|
+
/** Returns the persisted model choice for this session, if any. */
|
|
76
|
+
modelChoice(): {
|
|
77
|
+
provider?: string;
|
|
78
|
+
model?: string;
|
|
79
|
+
} | undefined;
|
|
80
|
+
/**
|
|
81
|
+
* Empties this session's turn list. Used by `/clear` when the user wants a
|
|
82
|
+
* fresh conversation without losing the session metadata (id/title). The
|
|
83
|
+
* underlying file is rewritten so a `/resume` of this id will land on an
|
|
84
|
+
* empty conversation.
|
|
85
|
+
*/
|
|
86
|
+
clearTurns(): void;
|
|
87
|
+
/**
|
|
88
|
+
* (3.5) Applies a pure transform to every turn in the prefix
|
|
89
|
+
* (all but the last `keepRecentTurns` turns). Used by the Stage 1
|
|
90
|
+
* pruner — the transform receives one turn's messages, returns
|
|
91
|
+
* the rewritten messages. The turn's `turnId` / `at` are
|
|
92
|
+
* unchanged so persistence keeps its identity; the synthetic
|
|
93
|
+
* `compact-*` turn (from `compactTurns`) is treated like any
|
|
94
|
+
* other turn for indexing purposes.
|
|
95
|
+
*
|
|
96
|
+
* `keepRecentTurns` is clamped to `[0, turns.length]`. When
|
|
97
|
+
* `keepRecentTurns >= turns.length`, this is a no-op (nothing to
|
|
98
|
+
* prune). When 0, every turn including the most recent is fed to
|
|
99
|
+
* the transform.
|
|
100
|
+
*
|
|
101
|
+
* Idempotent if the transform is idempotent (the pruner's marker
|
|
102
|
+
* replacement is — second pass sees the marker, fails the
|
|
103
|
+
* delta>0 check, leaves it alone).
|
|
104
|
+
*/
|
|
105
|
+
pruneTurns(keepRecentTurns: number, transform: (messages: Message[]) => Message[]): void;
|
|
106
|
+
/**
|
|
107
|
+
* Replaces all but the last `keepLastN` turns with a single synthetic
|
|
108
|
+
* "system context" turn carrying `summary` as a user-role message. Why
|
|
109
|
+
* user-role rather than system-role: providers vary in how they handle
|
|
110
|
+
* mid-conversation system messages, but every provider accepts a long-form
|
|
111
|
+
* user message as historical context. The synthetic turn's `turnId` is
|
|
112
|
+
* tagged `compact-*` so debuggers can spot it.
|
|
113
|
+
*
|
|
114
|
+
* `keepLastN` is clamped to `[0, turns.length]` — passing a larger N keeps
|
|
115
|
+
* everything (compaction is a no-op), passing 0 collapses the whole
|
|
116
|
+
* conversation into the summary. The summary itself must be non-empty;
|
|
117
|
+
* callers should bail before invoking this when the summarizer returned
|
|
118
|
+
* nothing.
|
|
119
|
+
*/
|
|
120
|
+
compactTurns(summary: string, keepLastN: number): void;
|
|
121
|
+
checkpoint(): Checkpoint;
|
|
122
|
+
restore(checkpoint: Checkpoint): void;
|
|
123
|
+
snapshot(): SessionState;
|
|
124
|
+
}
|
|
125
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/session/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAE9C,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,OAAO,EAAE,CAAC;CACrB;AAED,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,UAAU,EAAE,CAAC;IACpB;;;4BAGwB;IACxB,KAAK,CAAC,EAAE;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CAC/C;AAED,wEAAwE;AACxE,MAAM,WAAW,UAAU;IACzB,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,YAAY,CAAC;CACrB;AAED,yEAAyE;AACzE,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAS;gBAEjB,aAAa,EAAE,MAAM;IAKjC,IAAI,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI;IAI/B,IAAI,CAAC,EAAE,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS;IAU1C,IAAI,IAAI,YAAY,EAAE;CAYvB;AAED;;;GAGG;AACH,qBAAa,cAAc;IAGgB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC;IAFhE,OAAO,CAAC,KAAK,CAAe;IAE5B,OAAO;IAIP,MAAM,CAAC,MAAM,CAAC,KAAK,SAAY,EAAE,KAAK,CAAC,EAAE,YAAY,GAA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