dal-code 1.0.8 → 1.0.10
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 +39 -53
- package/dist/core/keybindings.d.ts +1 -1
- package/dist/core/keybindings.d.ts.map +1 -1
- package/dist/core/keybindings.js +1 -1
- package/dist/core/keybindings.js.map +1 -1
- package/dist/core/model-config.d.ts +85 -0
- package/dist/core/model-config.d.ts.map +1 -1
- package/dist/core/model-config.js +19 -14
- package/dist/core/model-config.js.map +1 -1
- package/dist/core/session-manager.d.ts +7 -0
- package/dist/core/session-manager.d.ts.map +1 -1
- package/dist/core/session-manager.js +28 -0
- package/dist/core/session-manager.js.map +1 -1
- package/dist/extensions/llama/client.d.ts +2 -0
- package/dist/extensions/llama/client.d.ts.map +1 -1
- package/dist/extensions/llama/client.js +7 -3
- package/dist/extensions/llama/client.js.map +1 -1
- package/dist/extensions/llama/provider.d.ts.map +1 -1
- package/dist/extensions/llama/provider.js +20 -5
- package/dist/extensions/llama/provider.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +5 -6
- package/dist/main.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +2 -1
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +31 -21
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/node_modules/@dalcode/dal-bridge/package.json +3 -3
- package/dist/node_modules/@dalcode/dal-gateway/package.json +3 -3
- package/dist/node_modules/@dalcode/dal-guard/package.json +5 -5
- package/dist/node_modules/@dalcode/dal-halo/package.json +2 -2
- package/dist/node_modules/@dalcode/dal-lang/package.json +4 -4
- package/dist/node_modules/@dalcode/dal-lsp/package.json +4 -4
- package/dist/node_modules/@dalcode/dal-mcp/package.json +6 -6
- package/dist/node_modules/@dalcode/dal-office/package.json +3 -3
- package/dist/node_modules/@dalcode/dal-pi-kit/package.json +1 -1
- package/dist/node_modules/@dalcode/dal-skills/package.json +4 -4
- package/dist/node_modules/@dalcode/dal-subagent/package.json +6 -6
- package/dist/node_modules/@dalcode/dal-telemetry/package.json +3 -3
- package/dist/node_modules/@dalcode/deepailab/package.json +4 -4
- package/dist/node_modules/@earendil-works/chord/LICENSE +21 -0
- package/dist/node_modules/@earendil-works/chord/README.md +205 -0
- package/dist/node_modules/@earendil-works/chord/dist/api.d.ts +15 -0
- package/dist/node_modules/@earendil-works/chord/dist/api.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/api.js +72 -0
- package/dist/node_modules/@earendil-works/chord/dist/api.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/bundler.d.ts +6 -0
- package/dist/node_modules/@earendil-works/chord/dist/bundler.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/bundler.js +3 -0
- package/dist/node_modules/@earendil-works/chord/dist/bundler.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/context/index.d.ts +24 -0
- package/dist/node_modules/@earendil-works/chord/dist/context/index.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/context/index.js +97 -0
- package/dist/node_modules/@earendil-works/chord/dist/context/index.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/delta/index.d.ts +119 -0
- package/dist/node_modules/@earendil-works/chord/dist/delta/index.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/delta/index.js +1906 -0
- package/dist/node_modules/@earendil-works/chord/dist/delta/index.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/facets/host.d.ts +12 -0
- package/dist/node_modules/@earendil-works/chord/dist/facets/host.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/facets/host.js +763 -0
- package/dist/node_modules/@earendil-works/chord/dist/facets/host.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/facets/loader.d.ts +3 -0
- package/dist/node_modules/@earendil-works/chord/dist/facets/loader.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/facets/loader.js +5 -0
- package/dist/node_modules/@earendil-works/chord/dist/facets/loader.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/index.d.ts +11 -0
- package/dist/node_modules/@earendil-works/chord/dist/index.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/index.js +10 -0
- package/dist/node_modules/@earendil-works/chord/dist/index.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/json.d.ts +4 -0
- package/dist/node_modules/@earendil-works/chord/dist/json.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/json.js +57 -0
- package/dist/node_modules/@earendil-works/chord/dist/json.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/node/bundle-loader.d.ts +30 -0
- package/dist/node_modules/@earendil-works/chord/dist/node/bundle-loader.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/node/bundle-loader.js +377 -0
- package/dist/node_modules/@earendil-works/chord/dist/node/bundle-loader.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/node/bundle.d.ts +26 -0
- package/dist/node_modules/@earendil-works/chord/dist/node/bundle.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/node/bundle.js +183 -0
- package/dist/node_modules/@earendil-works/chord/dist/node/bundle.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/node/manifest.d.ts +36 -0
- package/dist/node_modules/@earendil-works/chord/dist/node/manifest.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/node/manifest.js +6 -0
- package/dist/node_modules/@earendil-works/chord/dist/node/manifest.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/node/package.d.ts +15 -0
- package/dist/node_modules/@earendil-works/chord/dist/node/package.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/node/package.js +199 -0
- package/dist/node_modules/@earendil-works/chord/dist/node/package.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/node.d.ts +5 -0
- package/dist/node_modules/@earendil-works/chord/dist/node.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/node.js +3 -0
- package/dist/node_modules/@earendil-works/chord/dist/node.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/consumer.d.ts +11 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/consumer.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/consumer.js +537 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/consumer.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/errors.d.ts +8 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/errors.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/errors.js +22 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/errors.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/handle.d.ts +15 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/handle.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/handle.js +99 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/handle.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/instances.d.ts +25 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/instances.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/instances.js +137 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/instances.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/loopback.d.ts +5 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/loopback.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/loopback.js +15 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/loopback.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/provider.d.ts +37 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/provider.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/provider.js +472 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/provider.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/state-codec.d.ts +15 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/state-codec.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/state-codec.js +115 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/state-codec.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/state-internals.d.ts +11 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/state-internals.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/state-internals.js +10 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/state-internals.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/state.d.ts +23 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/state.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/state.js +124 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/state.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/wire.d.ts +60 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/wire.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/wire.js +173 -0
- package/dist/node_modules/@earendil-works/chord/dist/services/wire.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/types.d.ts +203 -0
- package/dist/node_modules/@earendil-works/chord/dist/types.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/chord/dist/types.js +2 -0
- package/dist/node_modules/@earendil-works/chord/dist/types.js.map +1 -0
- package/dist/node_modules/@earendil-works/chord/package.json +62 -0
- package/dist/node_modules/@earendil-works/chord/src/delta/README.md +404 -0
- package/dist/node_modules/@earendil-works/pi-agent-core/package.json +3 -3
- package/dist/node_modules/@earendil-works/pi-ai/README.md +3 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/anthropic-messages.d.ts.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/anthropic-messages.js +15 -6
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/anthropic-messages.js.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/bedrock-converse-stream.d.ts.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/bedrock-converse-stream.js +1 -0
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/bedrock-converse-stream.js.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/google-generative-ai.d.ts.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/google-generative-ai.js +9 -66
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/google-generative-ai.js.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/google-shared.d.ts +12 -3
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/google-shared.d.ts.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/google-shared.js +50 -3
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/google-shared.js.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/google-vertex.d.ts.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/google-vertex.js +13 -58
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/google-vertex.js.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/mistral-conversations.d.ts.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/mistral-conversations.js +5 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/mistral-conversations.js.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/openai-codex-responses.d.ts.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/openai-codex-responses.js +6 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/openai-codex-responses.js.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/openai-completions.d.ts.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/openai-completions.js +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/api/openai-completions.js.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/image-models.generated.d.ts +30 -0
- package/dist/node_modules/@earendil-works/pi-ai/dist/image-models.generated.d.ts.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/image-models.generated.js +34 -4
- package/dist/node_modules/@earendil-works/pi-ai/dist/image-models.generated.js.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/.manifest.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/baseten.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/deepseek.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/fireworks.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/openai-codex.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/opencode-go.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/opencode.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/openrouter.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/qwen-token-plan-cn.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/qwen-token-plan.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/zai-coding-cn.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/zai.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/types.d.ts +4 -2
- package/dist/node_modules/@earendil-works/pi-ai/dist/types.d.ts.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/types.js.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/utils/overflow.d.ts.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/utils/overflow.js +8 -3
- package/dist/node_modules/@earendil-works/pi-ai/dist/utils/overflow.js.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/utils/retry.d.ts.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/utils/retry.js +2 -0
- package/dist/node_modules/@earendil-works/pi-ai/dist/utils/retry.js.map +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/package.json +2 -2
- package/dist/node_modules/@earendil-works/pi-client/package.json +2 -2
- package/dist/node_modules/@earendil-works/pi-protocol/package.json +2 -2
- package/dist/node_modules/@earendil-works/pi-server/CHANGELOG.md +57 -0
- package/dist/node_modules/@earendil-works/pi-server/LICENSE +21 -0
- package/dist/node_modules/@earendil-works/pi-server/README.md +79 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/cli.d.ts +3 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/cli.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/cli.js +136 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/cli.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/config.d.ts +12 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/config.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/config.js +55 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/config.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/connection.d.ts +35 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/connection.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/connection.js +4 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/connection.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/errors.d.ts +25 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/errors.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/errors.js +41 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/errors.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/handler.d.ts +14 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/handler.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/handler.js +105 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/handler.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/index.d.ts +5 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/index.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/index.js +5 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/index.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/ipc/client.d.ts +3 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/ipc/client.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/ipc/client.js +56 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/ipc/client.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/ipc/protocol.d.ts +99 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/ipc/protocol.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/ipc/protocol.js +12 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/ipc/protocol.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/ipc/server.d.ts +20 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/ipc/server.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/ipc/server.js +145 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/ipc/server.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/listener.d.ts +8 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/listener.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/listener.js +2 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/listener.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/protocol.d.ts +22 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/protocol.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/protocol.js +266 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/protocol.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/radius.d.ts +36 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/radius.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/radius.js +360 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/radius.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/rpc-process.d.ts +31 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/rpc-process.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/rpc-process.js +168 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/rpc-process.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/serve.d.ts +2 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/serve.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/serve.js +70 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/serve.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/server.d.ts +46 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/server.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/server.js +549 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/server.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/session-router.d.ts +40 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/session-router.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/session-router.js +264 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/session-router.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/sessions.d.ts +2195 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/sessions.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/sessions.js +332 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/sessions.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/snapshots.d.ts +24 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/snapshots.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/snapshots.js +39 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/snapshots.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/storage.d.ts +10 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/storage.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/storage.js +58 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/storage.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/supervisor.d.ts +34 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/supervisor.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/supervisor.js +290 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/supervisor.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/testing/client.d.ts +35 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/testing/client.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/testing/client.js +138 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/testing/client.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/testing/host.d.ts +57 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/testing/host.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/testing/host.js +189 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/testing/host.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/testing/index.d.ts +6 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/testing/index.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/testing/index.js +4 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/testing/index.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/testing/server.d.ts +13 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/testing/server.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/testing/server.js +17 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/testing/server.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/testing/service.d.ts +66 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/testing/service.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/testing/service.js +254 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/testing/service.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/address.d.ts +3 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/address.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/address.js +9 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/address.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/index.d.ts +5 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/index.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/index.js +4 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/index.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/listener.d.ts +24 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/listener.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/listener.js +421 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/listener.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/preset.d.ts +7 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/preset.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/preset.js +22 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/preset.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/types.d.ts +15 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/types.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/types.js +2 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/transports/unix/types.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/types.d.ts +49 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/types.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/types.js +2 -0
- package/dist/node_modules/@earendil-works/pi-server/dist/types.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-server/package.json +45 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/CHANGELOG.md +46 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/LICENSE +21 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/README.md +33 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/index.d.ts +6 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/index.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/index.js +103 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/index.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/branch-cache.d.ts +6 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/branch-cache.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/branch-cache.js +70 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/branch-cache.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/index.d.ts +5 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/index.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/index.js +4 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/index.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/migrations/001_initial.sql +121 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/migrations.d.ts +3 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/migrations.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/migrations.js +7 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/migrations.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/repo.d.ts +41 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/repo.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/repo.js +408 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/repo.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/search-backend.d.ts +14 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/search-backend.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/search-backend.js +153 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/search-backend.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/branch-entries.d.ts +6 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/branch-entries.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/branch-entries.js +213 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/branch-entries.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/entries.d.ts +24 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/entries.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/entries.js +126 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/entries.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-row.d.ts +23 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-row.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-row.js +69 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-row.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-sequences.d.ts +4 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-sequences.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-sequences.js +13 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-sequences.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-stats.d.ts +6 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-stats.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-stats.js +39 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/session-stats.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/usage-ledger.d.ts +20 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/usage-ledger.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/usage-ledger.js +50 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/usage-ledger.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/values.d.ts +23 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/values.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/values.js +91 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session/values.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session.d.ts +42 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session.js +163 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/session.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/sql.d.ts +19 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/sql.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/sql.js +58 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/sql.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/branch-entries.d.ts +41 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/branch-entries.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/branch-entries.js +112 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/branch-entries.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/branch-tips.d.ts +7 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/branch-tips.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/branch-tips.js +22 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/branch-tips.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/entries.d.ts +34 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/entries.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/entries.js +37 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/entries.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/facts.d.ts +20 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/facts.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/facts.js +38 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/facts.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/lanes.d.ts +35 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/lanes.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/lanes.js +95 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/lanes.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/records.d.ts +38 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/records.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/records.js +49 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/records.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/session-sequences.d.ts +7 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/session-sequences.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/session-sequences.js +22 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/session-sequences.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/session-stats.d.ts +17 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/session-stats.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/session-stats.js +40 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/session-stats.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/sessions.d.ts +26 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/sessions.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/sessions.js +99 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/sessions.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/writer-leases.d.ts +15 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/writer-leases.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/writer-leases.js +31 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage/writer-leases.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage.d.ts +38 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage.js +161 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/storage.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/types.d.ts +29 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/types.d.ts.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/types.js +2 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/dist/sqlite/types.js.map +1 -0
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/package.json +33 -0
- package/dist/node_modules/@earendil-works/pi-telemetry/package.json +1 -1
- package/dist/node_modules/@earendil-works/pi-tui/package.json +1 -1
- package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
- package/examples/extensions/custom-provider-anthropic/package.json +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/gondolin/package-lock.json +2 -2
- package/examples/extensions/gondolin/package.json +1 -1
- package/examples/extensions/official/package.json +1 -1
- package/examples/extensions/sandbox/package-lock.json +2 -2
- package/examples/extensions/sandbox/package.json +1 -1
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/examples/plugins/pi-example-plugin/package.json +1 -1
- package/npm-shrinkwrap.json +3 -14
- package/package.json +2 -2
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { AgentSessionEvent, RpcCommand, RpcExtensionUIRequest, RpcExtensionUIResponse, RpcResponse } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { InstanceRecord } from "./types.ts";
|
|
3
|
+
export declare class ServerSupervisor {
|
|
4
|
+
private readonly liveInstances;
|
|
5
|
+
private setStatus;
|
|
6
|
+
private updateRecord;
|
|
7
|
+
private clearBindings;
|
|
8
|
+
private bindRpcProcess;
|
|
9
|
+
private handleUnexpectedRpcExit;
|
|
10
|
+
private getRpcProcess;
|
|
11
|
+
private syncInstanceRecord;
|
|
12
|
+
private cleanupAcquiredResources;
|
|
13
|
+
private failSpawn;
|
|
14
|
+
updateInstance(instance: InstanceRecord): void;
|
|
15
|
+
openRpcStream(instanceId: string, onEvent: (event: AgentSessionEvent) => void, onUiRequest: (request: RpcExtensionUIRequest) => void): {
|
|
16
|
+
handleRpc(command: RpcCommand): Promise<RpcResponse>;
|
|
17
|
+
handleUiResponse(response: RpcExtensionUIResponse): void;
|
|
18
|
+
close(): void;
|
|
19
|
+
} | undefined;
|
|
20
|
+
getLiveInstance(instanceId: string): InstanceRecord | undefined;
|
|
21
|
+
listLiveInstances(): InstanceRecord[];
|
|
22
|
+
recoverAfterRestart(): Promise<void>;
|
|
23
|
+
listInstances(): InstanceRecord[];
|
|
24
|
+
getInstance(instanceId: string): InstanceRecord | undefined;
|
|
25
|
+
spawnInstance(options: {
|
|
26
|
+
cwd: string;
|
|
27
|
+
label?: string;
|
|
28
|
+
}): Promise<InstanceRecord>;
|
|
29
|
+
stopInstance(instanceId: string): Promise<InstanceRecord | undefined>;
|
|
30
|
+
handleRpc(instanceId: string, command: RpcCommand): Promise<RpcResponse | undefined>;
|
|
31
|
+
shutdown(): Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
export declare const supervisor: ServerSupervisor;
|
|
34
|
+
//# sourceMappingURL=supervisor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"supervisor.d.ts","sourceRoot":"","sources":["../src/supervisor.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACX,iBAAiB,EAEjB,UAAU,EACV,qBAAqB,EACrB,sBAAsB,EACtB,WAAW,EACX,MAAM,iCAAiC,CAAC;AAIzC,OAAO,KAAK,EAAE,cAAc,EAAkB,MAAM,YAAY,CAAC;AAkDjE,qBAAa,gBAAgB;IAC5B,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAmC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CACD;AAED,eAAO,MAAM,UAAU,kBAAyB,CAAC","sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport type {\n\tAgentSessionEvent,\n\tAgentSessionEventListener,\n\tRpcCommand,\n\tRpcExtensionUIRequest,\n\tRpcExtensionUIResponse,\n\tRpcResponse,\n} from \"@earendil-works/pi-coding-agent\";\nimport { radiusPresence } from \"./radius.ts\";\nimport { createRpcProcessInstance, type RpcProcessInstance } from \"./rpc-process.ts\";\nimport { getInstance, loadInstances, removeInstance, saveInstances, upsertInstance } from \"./storage.ts\";\nimport type { InstanceRecord, InstanceStatus } from \"./types.ts\";\n\ninterface LiveInstanceResources {\n\trpcProcess?: RpcProcessInstance;\n\tradiusPiId?: string;\n\tsessionId?: string;\n}\n\ninterface LiveInstance {\n\trecord: InstanceRecord;\n\tresources: LiveInstanceResources;\n\tsubscribers: Set<AgentSessionEventListener>;\n\tonUiRequest?: (request: RpcExtensionUIRequest) => void;\n\tunsubscribeEvents?: () => void;\n\tunsubscribeExit?: () => void;\n}\n\nfunction cloneInstance(record: InstanceRecord): InstanceRecord {\n\treturn { ...record };\n}\n\n// Only refresh persisted session metadata after commands that can plausibly change\n// the instance identity/details we store in instances.json. Most RPCs mutate transient\n// runtime state only, so forcing a follow-up get_state after every command is wasted IO.\n//\n// - new_session / switch_session / fork / clone can change sessionId/sessionFile\n// - set_session_name changes a persisted session detail we may want reflected externally\n// - prompt can materialize or advance persisted session state after the child processes it\nconst SESSION_METADATA_COMMANDS: ReadonlySet<RpcCommand[\"type\"]> = new Set([\n\t\"new_session\",\n\t\"switch_session\",\n\t\"fork\",\n\t\"clone\",\n\t\"set_session_name\",\n\t\"prompt\",\n]);\n\nfunction shouldRefreshSessionMetadata(command: RpcCommand): boolean {\n\treturn SESSION_METADATA_COMMANDS.has(command.type);\n}\n\nfunction isGetStateSuccess(\n\tresponse: RpcResponse,\n): response is Extract<\n\tRpcResponse,\n\t{ success: true; command: \"get_state\"; data: { sessionId: string; sessionFile?: string } }\n> {\n\treturn response.success === true && response.command === \"get_state\" && \"data\" in response;\n}\n\nexport class ServerSupervisor {\n\tprivate readonly liveInstances = new Map<string, LiveInstance>();\n\n\tprivate setStatus(live: LiveInstance, status: InstanceStatus): void {\n\t\tlive.record = {\n\t\t\t...live.record,\n\t\t\tstatus,\n\t\t\tlastSeenAt: new Date().toISOString(),\n\t\t};\n\t\tupsertInstance(live.record);\n\t}\n\n\tprivate updateRecord(live: LiveInstance, updates: Partial<InstanceRecord>): void {\n\t\tlive.record = {\n\t\t\t...live.record,\n\t\t\t...updates,\n\t\t\tlastSeenAt: new Date().toISOString(),\n\t\t};\n\t\tif (updates.radiusPiId !== undefined) {\n\t\t\tlive.resources.radiusPiId = updates.radiusPiId;\n\t\t}\n\t\tif (updates.sessionId !== undefined) {\n\t\t\tlive.resources.sessionId = updates.sessionId;\n\t\t}\n\t\tupsertInstance(live.record);\n\t}\n\n\tprivate clearBindings(live: LiveInstance): void {\n\t\tlive.unsubscribeEvents?.();\n\t\tlive.unsubscribeExit?.();\n\t\tlive.unsubscribeEvents = undefined;\n\t\tlive.unsubscribeExit = undefined;\n\t\tlive.onUiRequest = undefined;\n\t\tlive.resources.rpcProcess?.setUiRequestHandler(undefined);\n\t}\n\n\tprivate bindRpcProcess(live: LiveInstance, rpcProcess: RpcProcessInstance): void {\n\t\tthis.clearBindings(live);\n\t\tlive.resources.rpcProcess = rpcProcess;\n\t\tlive.unsubscribeEvents = rpcProcess.onEvent((event) => {\n\t\t\tfor (const subscriber of live.subscribers) {\n\t\t\t\tsubscriber(event);\n\t\t\t}\n\t\t});\n\t\tlive.unsubscribeExit = rpcProcess.onExit((error) => {\n\t\t\tvoid this.handleUnexpectedRpcExit(live, error);\n\t\t});\n\t\trpcProcess.setUiRequestHandler((request) => {\n\t\t\tlive.onUiRequest?.(request);\n\t\t});\n\t}\n\n\tprivate async handleUnexpectedRpcExit(live: LiveInstance, _error?: Error): Promise<void> {\n\t\tif (this.liveInstances.get(live.record.id) !== live) {\n\t\t\treturn;\n\t\t}\n\t\tif (live.record.status === \"stopping\" || live.record.status === \"stopped\") {\n\t\t\treturn;\n\t\t}\n\t\tthis.setStatus(live, \"error\");\n\t\tthis.clearBindings(live);\n\t\tlive.resources.rpcProcess = undefined;\n\t\tif (live.resources.radiusPiId) {\n\t\t\ttry {\n\t\t\t\tawait radiusPresence.disconnectPi(live.record);\n\t\t\t\tthis.updateRecord(live, { radiusPiId: undefined });\n\t\t\t} catch (error) {\n\t\t\t\tconsole.error(`Failed to disconnect Radius Pi ${live.record.id}: ${String(error)}`);\n\t\t\t}\n\t\t}\n\t\tthis.liveInstances.delete(live.record.id);\n\t}\n\n\tprivate getRpcProcess(live: LiveInstance): RpcProcessInstance | undefined {\n\t\treturn live.resources.rpcProcess;\n\t}\n\n\tprivate async syncInstanceRecord(live: LiveInstance): Promise<void> {\n\t\tconst rpcProcess = this.getRpcProcess(live);\n\t\tif (!rpcProcess) {\n\t\t\tthis.updateRecord(live, {});\n\t\t\treturn;\n\t\t}\n\t\tconst response = await rpcProcess.send({ type: \"get_state\" });\n\t\tif (!isGetStateSuccess(response)) {\n\t\t\tthis.updateRecord(live, {});\n\t\t\treturn;\n\t\t}\n\t\tthis.updateRecord(live, {\n\t\t\tsessionId: response.data.sessionId,\n\t\t\tsessionFile: response.data.sessionFile,\n\t\t});\n\t}\n\n\tprivate async cleanupAcquiredResources(live: LiveInstance): Promise<void> {\n\t\tconst rpcProcess = live.resources.rpcProcess;\n\t\tthis.clearBindings(live);\n\t\tif (live.resources.radiusPiId) {\n\t\t\tawait radiusPresence.disconnectPi(live.record);\n\t\t\tlive.resources.radiusPiId = undefined;\n\t\t\tlive.record = {\n\t\t\t\t...live.record,\n\t\t\t\tradiusPiId: undefined,\n\t\t\t\tlastSeenAt: new Date().toISOString(),\n\t\t\t};\n\t\t}\n\t\tlive.resources.sessionId = undefined;\n\t\tif (rpcProcess) {\n\t\t\tlive.resources.rpcProcess = undefined;\n\t\t\tawait rpcProcess.dispose();\n\t\t}\n\t}\n\n\tprivate async failSpawn(live: LiveInstance, error: unknown): Promise<never> {\n\t\tthis.setStatus(live, \"error\");\n\t\ttry {\n\t\t\tawait this.cleanupAcquiredResources(live);\n\t\t} finally {\n\t\t\tthis.setStatus(live, \"stopped\");\n\t\t\tthis.liveInstances.delete(live.record.id);\n\t\t}\n\t\tthrow error;\n\t}\n\n\tupdateInstance(instance: InstanceRecord): void {\n\t\tconst live = this.liveInstances.get(instance.id);\n\t\tif (live) {\n\t\t\tlive.record = instance;\n\t\t\tlive.resources.radiusPiId = instance.radiusPiId;\n\t\t\tlive.resources.sessionId = instance.sessionId;\n\t\t}\n\t\tupsertInstance(instance);\n\t}\n\n\topenRpcStream(\n\t\tinstanceId: string,\n\t\tonEvent: (event: AgentSessionEvent) => void,\n\t\tonUiRequest: (request: RpcExtensionUIRequest) => void,\n\t):\n\t\t| {\n\t\t\t\thandleRpc(command: RpcCommand): Promise<RpcResponse>;\n\t\t\t\thandleUiResponse(response: RpcExtensionUIResponse): void;\n\t\t\t\tclose(): void;\n\t\t }\n\t\t| undefined {\n\t\tconst live = this.liveInstances.get(instanceId);\n\t\tconst rpcProcess = live ? this.getRpcProcess(live) : undefined;\n\t\tif (!live || !rpcProcess) {\n\t\t\treturn undefined;\n\t\t}\n\t\tlive.subscribers.add(onEvent);\n\t\tlive.onUiRequest = onUiRequest;\n\t\treturn {\n\t\t\thandleRpc: async (command) => {\n\t\t\t\tconst response = await rpcProcess.send(command);\n\t\t\t\tif (shouldRefreshSessionMetadata(command)) {\n\t\t\t\t\tawait this.syncInstanceRecord(live);\n\t\t\t\t}\n\t\t\t\treturn response;\n\t\t\t},\n\t\t\thandleUiResponse: (response) => {\n\t\t\t\trpcProcess.handleUiResponse(response);\n\t\t\t},\n\t\t\tclose: () => {\n\t\t\t\tif (live.onUiRequest === onUiRequest) {\n\t\t\t\t\tlive.onUiRequest = undefined;\n\t\t\t\t}\n\t\t\t\tlive.subscribers.delete(onEvent);\n\t\t\t},\n\t\t};\n\t}\n\n\tgetLiveInstance(instanceId: string): InstanceRecord | undefined {\n\t\tconst live = this.liveInstances.get(instanceId);\n\t\treturn live ? cloneInstance(live.record) : undefined;\n\t}\n\n\tlistLiveInstances(): InstanceRecord[] {\n\t\treturn [...this.liveInstances.values()].map((live) => cloneInstance(live.record));\n\t}\n\n\tasync recoverAfterRestart(): Promise<void> {\n\t\tconst recoveredAt = new Date().toISOString();\n\t\tconst instances = loadInstances().map((instance) => ({\n\t\t\t...instance,\n\t\t\tstatus: instance.status === \"online\" || instance.status === \"starting\" ? \"stopped\" : instance.status,\n\t\t\tlastSeenAt: recoveredAt,\n\t\t}));\n\t\tfor (const instance of instances) {\n\t\t\tawait radiusPresence.disconnectPi(instance);\n\t\t}\n\t\tsaveInstances(instances);\n\t}\n\n\tlistInstances(): InstanceRecord[] {\n\t\treturn loadInstances().map(cloneInstance);\n\t}\n\n\tgetInstance(instanceId: string): InstanceRecord | undefined {\n\t\tconst live = this.liveInstances.get(instanceId);\n\t\tif (live) {\n\t\t\treturn cloneInstance(live.record);\n\t\t}\n\t\tconst stored = getInstance(instanceId);\n\t\treturn stored ? cloneInstance(stored) : undefined;\n\t}\n\n\tasync spawnInstance(options: { cwd: string; label?: string }): Promise<InstanceRecord> {\n\t\tconst now = new Date().toISOString();\n\t\tconst live: LiveInstance = {\n\t\t\trecord: {\n\t\t\t\tid: randomUUID(),\n\t\t\t\tstatus: \"starting\",\n\t\t\t\tcwd: options.cwd,\n\t\t\t\tcreatedAt: now,\n\t\t\t\tlastSeenAt: now,\n\t\t\t\tlabel: options.label,\n\t\t\t},\n\t\t\tresources: {},\n\t\t\tsubscribers: new Set(),\n\t\t};\n\t\tthis.liveInstances.set(live.record.id, live);\n\t\tupsertInstance(live.record);\n\n\t\ttry {\n\t\t\tconst rpcProcess = createRpcProcessInstance({ cwd: options.cwd });\n\t\t\tthis.bindRpcProcess(live, rpcProcess);\n\t\t\tawait this.syncInstanceRecord(live);\n\t\t\tconst registeredRecord = await radiusPresence.registerPi(live.record);\n\t\t\tthis.updateRecord(live, { radiusPiId: registeredRecord.radiusPiId });\n\t\t\tthis.setStatus(live, \"online\");\n\t\t\treturn cloneInstance(live.record);\n\t\t} catch (error) {\n\t\t\treturn await this.failSpawn(live, error);\n\t\t}\n\t}\n\n\tasync stopInstance(instanceId: string): Promise<InstanceRecord | undefined> {\n\t\tconst live = this.liveInstances.get(instanceId);\n\t\tif (!live) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tthis.setStatus(live, \"stopping\");\n\t\ttry {\n\t\t\tawait this.cleanupAcquiredResources(live);\n\t\t} finally {\n\t\t\tlive.record = {\n\t\t\t\t...live.record,\n\t\t\t\tstatus: \"stopped\",\n\t\t\t\tlastSeenAt: new Date().toISOString(),\n\t\t\t};\n\t\t\tthis.liveInstances.delete(instanceId);\n\t\t\tremoveInstance(instanceId);\n\t\t}\n\t\treturn cloneInstance(live.record);\n\t}\n\n\tasync handleRpc(instanceId: string, command: RpcCommand): Promise<RpcResponse | undefined> {\n\t\tconst live = this.liveInstances.get(instanceId);\n\t\tconst rpcProcess = live ? this.getRpcProcess(live) : undefined;\n\t\tif (!live || !rpcProcess) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst response = await rpcProcess.send(command);\n\t\tif (shouldRefreshSessionMetadata(command)) {\n\t\t\tawait this.syncInstanceRecord(live);\n\t\t}\n\t\treturn response;\n\t}\n\n\tasync shutdown(): Promise<void> {\n\t\tfor (const instanceId of [...this.liveInstances.keys()]) {\n\t\t\tawait this.stopInstance(instanceId);\n\t\t}\n\t}\n}\n\nexport const supervisor = new ServerSupervisor();\n\nradiusPresence.setCoordinator({\n\tgetLiveInstance(instanceId) {\n\t\treturn supervisor.getLiveInstance(instanceId);\n\t},\n\tlistLiveInstances() {\n\t\treturn supervisor.listLiveInstances();\n\t},\n\tupdateInstance(instance) {\n\t\tsupervisor.updateInstance(instance);\n\t},\n});\n"]}
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { radiusPresence } from "./radius.js";
|
|
3
|
+
import { createRpcProcessInstance } from "./rpc-process.js";
|
|
4
|
+
import { getInstance, loadInstances, removeInstance, saveInstances, upsertInstance } from "./storage.js";
|
|
5
|
+
function cloneInstance(record) {
|
|
6
|
+
return { ...record };
|
|
7
|
+
}
|
|
8
|
+
// Only refresh persisted session metadata after commands that can plausibly change
|
|
9
|
+
// the instance identity/details we store in instances.json. Most RPCs mutate transient
|
|
10
|
+
// runtime state only, so forcing a follow-up get_state after every command is wasted IO.
|
|
11
|
+
//
|
|
12
|
+
// - new_session / switch_session / fork / clone can change sessionId/sessionFile
|
|
13
|
+
// - set_session_name changes a persisted session detail we may want reflected externally
|
|
14
|
+
// - prompt can materialize or advance persisted session state after the child processes it
|
|
15
|
+
const SESSION_METADATA_COMMANDS = new Set([
|
|
16
|
+
"new_session",
|
|
17
|
+
"switch_session",
|
|
18
|
+
"fork",
|
|
19
|
+
"clone",
|
|
20
|
+
"set_session_name",
|
|
21
|
+
"prompt",
|
|
22
|
+
]);
|
|
23
|
+
function shouldRefreshSessionMetadata(command) {
|
|
24
|
+
return SESSION_METADATA_COMMANDS.has(command.type);
|
|
25
|
+
}
|
|
26
|
+
function isGetStateSuccess(response) {
|
|
27
|
+
return response.success === true && response.command === "get_state" && "data" in response;
|
|
28
|
+
}
|
|
29
|
+
export class ServerSupervisor {
|
|
30
|
+
liveInstances = new Map();
|
|
31
|
+
setStatus(live, status) {
|
|
32
|
+
live.record = {
|
|
33
|
+
...live.record,
|
|
34
|
+
status,
|
|
35
|
+
lastSeenAt: new Date().toISOString(),
|
|
36
|
+
};
|
|
37
|
+
upsertInstance(live.record);
|
|
38
|
+
}
|
|
39
|
+
updateRecord(live, updates) {
|
|
40
|
+
live.record = {
|
|
41
|
+
...live.record,
|
|
42
|
+
...updates,
|
|
43
|
+
lastSeenAt: new Date().toISOString(),
|
|
44
|
+
};
|
|
45
|
+
if (updates.radiusPiId !== undefined) {
|
|
46
|
+
live.resources.radiusPiId = updates.radiusPiId;
|
|
47
|
+
}
|
|
48
|
+
if (updates.sessionId !== undefined) {
|
|
49
|
+
live.resources.sessionId = updates.sessionId;
|
|
50
|
+
}
|
|
51
|
+
upsertInstance(live.record);
|
|
52
|
+
}
|
|
53
|
+
clearBindings(live) {
|
|
54
|
+
live.unsubscribeEvents?.();
|
|
55
|
+
live.unsubscribeExit?.();
|
|
56
|
+
live.unsubscribeEvents = undefined;
|
|
57
|
+
live.unsubscribeExit = undefined;
|
|
58
|
+
live.onUiRequest = undefined;
|
|
59
|
+
live.resources.rpcProcess?.setUiRequestHandler(undefined);
|
|
60
|
+
}
|
|
61
|
+
bindRpcProcess(live, rpcProcess) {
|
|
62
|
+
this.clearBindings(live);
|
|
63
|
+
live.resources.rpcProcess = rpcProcess;
|
|
64
|
+
live.unsubscribeEvents = rpcProcess.onEvent((event) => {
|
|
65
|
+
for (const subscriber of live.subscribers) {
|
|
66
|
+
subscriber(event);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
live.unsubscribeExit = rpcProcess.onExit((error) => {
|
|
70
|
+
void this.handleUnexpectedRpcExit(live, error);
|
|
71
|
+
});
|
|
72
|
+
rpcProcess.setUiRequestHandler((request) => {
|
|
73
|
+
live.onUiRequest?.(request);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
async handleUnexpectedRpcExit(live, _error) {
|
|
77
|
+
if (this.liveInstances.get(live.record.id) !== live) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (live.record.status === "stopping" || live.record.status === "stopped") {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
this.setStatus(live, "error");
|
|
84
|
+
this.clearBindings(live);
|
|
85
|
+
live.resources.rpcProcess = undefined;
|
|
86
|
+
if (live.resources.radiusPiId) {
|
|
87
|
+
try {
|
|
88
|
+
await radiusPresence.disconnectPi(live.record);
|
|
89
|
+
this.updateRecord(live, { radiusPiId: undefined });
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
console.error(`Failed to disconnect Radius Pi ${live.record.id}: ${String(error)}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
this.liveInstances.delete(live.record.id);
|
|
96
|
+
}
|
|
97
|
+
getRpcProcess(live) {
|
|
98
|
+
return live.resources.rpcProcess;
|
|
99
|
+
}
|
|
100
|
+
async syncInstanceRecord(live) {
|
|
101
|
+
const rpcProcess = this.getRpcProcess(live);
|
|
102
|
+
if (!rpcProcess) {
|
|
103
|
+
this.updateRecord(live, {});
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const response = await rpcProcess.send({ type: "get_state" });
|
|
107
|
+
if (!isGetStateSuccess(response)) {
|
|
108
|
+
this.updateRecord(live, {});
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
this.updateRecord(live, {
|
|
112
|
+
sessionId: response.data.sessionId,
|
|
113
|
+
sessionFile: response.data.sessionFile,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
async cleanupAcquiredResources(live) {
|
|
117
|
+
const rpcProcess = live.resources.rpcProcess;
|
|
118
|
+
this.clearBindings(live);
|
|
119
|
+
if (live.resources.radiusPiId) {
|
|
120
|
+
await radiusPresence.disconnectPi(live.record);
|
|
121
|
+
live.resources.radiusPiId = undefined;
|
|
122
|
+
live.record = {
|
|
123
|
+
...live.record,
|
|
124
|
+
radiusPiId: undefined,
|
|
125
|
+
lastSeenAt: new Date().toISOString(),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
live.resources.sessionId = undefined;
|
|
129
|
+
if (rpcProcess) {
|
|
130
|
+
live.resources.rpcProcess = undefined;
|
|
131
|
+
await rpcProcess.dispose();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
async failSpawn(live, error) {
|
|
135
|
+
this.setStatus(live, "error");
|
|
136
|
+
try {
|
|
137
|
+
await this.cleanupAcquiredResources(live);
|
|
138
|
+
}
|
|
139
|
+
finally {
|
|
140
|
+
this.setStatus(live, "stopped");
|
|
141
|
+
this.liveInstances.delete(live.record.id);
|
|
142
|
+
}
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
updateInstance(instance) {
|
|
146
|
+
const live = this.liveInstances.get(instance.id);
|
|
147
|
+
if (live) {
|
|
148
|
+
live.record = instance;
|
|
149
|
+
live.resources.radiusPiId = instance.radiusPiId;
|
|
150
|
+
live.resources.sessionId = instance.sessionId;
|
|
151
|
+
}
|
|
152
|
+
upsertInstance(instance);
|
|
153
|
+
}
|
|
154
|
+
openRpcStream(instanceId, onEvent, onUiRequest) {
|
|
155
|
+
const live = this.liveInstances.get(instanceId);
|
|
156
|
+
const rpcProcess = live ? this.getRpcProcess(live) : undefined;
|
|
157
|
+
if (!live || !rpcProcess) {
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
live.subscribers.add(onEvent);
|
|
161
|
+
live.onUiRequest = onUiRequest;
|
|
162
|
+
return {
|
|
163
|
+
handleRpc: async (command) => {
|
|
164
|
+
const response = await rpcProcess.send(command);
|
|
165
|
+
if (shouldRefreshSessionMetadata(command)) {
|
|
166
|
+
await this.syncInstanceRecord(live);
|
|
167
|
+
}
|
|
168
|
+
return response;
|
|
169
|
+
},
|
|
170
|
+
handleUiResponse: (response) => {
|
|
171
|
+
rpcProcess.handleUiResponse(response);
|
|
172
|
+
},
|
|
173
|
+
close: () => {
|
|
174
|
+
if (live.onUiRequest === onUiRequest) {
|
|
175
|
+
live.onUiRequest = undefined;
|
|
176
|
+
}
|
|
177
|
+
live.subscribers.delete(onEvent);
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
getLiveInstance(instanceId) {
|
|
182
|
+
const live = this.liveInstances.get(instanceId);
|
|
183
|
+
return live ? cloneInstance(live.record) : undefined;
|
|
184
|
+
}
|
|
185
|
+
listLiveInstances() {
|
|
186
|
+
return [...this.liveInstances.values()].map((live) => cloneInstance(live.record));
|
|
187
|
+
}
|
|
188
|
+
async recoverAfterRestart() {
|
|
189
|
+
const recoveredAt = new Date().toISOString();
|
|
190
|
+
const instances = loadInstances().map((instance) => ({
|
|
191
|
+
...instance,
|
|
192
|
+
status: instance.status === "online" || instance.status === "starting" ? "stopped" : instance.status,
|
|
193
|
+
lastSeenAt: recoveredAt,
|
|
194
|
+
}));
|
|
195
|
+
for (const instance of instances) {
|
|
196
|
+
await radiusPresence.disconnectPi(instance);
|
|
197
|
+
}
|
|
198
|
+
saveInstances(instances);
|
|
199
|
+
}
|
|
200
|
+
listInstances() {
|
|
201
|
+
return loadInstances().map(cloneInstance);
|
|
202
|
+
}
|
|
203
|
+
getInstance(instanceId) {
|
|
204
|
+
const live = this.liveInstances.get(instanceId);
|
|
205
|
+
if (live) {
|
|
206
|
+
return cloneInstance(live.record);
|
|
207
|
+
}
|
|
208
|
+
const stored = getInstance(instanceId);
|
|
209
|
+
return stored ? cloneInstance(stored) : undefined;
|
|
210
|
+
}
|
|
211
|
+
async spawnInstance(options) {
|
|
212
|
+
const now = new Date().toISOString();
|
|
213
|
+
const live = {
|
|
214
|
+
record: {
|
|
215
|
+
id: randomUUID(),
|
|
216
|
+
status: "starting",
|
|
217
|
+
cwd: options.cwd,
|
|
218
|
+
createdAt: now,
|
|
219
|
+
lastSeenAt: now,
|
|
220
|
+
label: options.label,
|
|
221
|
+
},
|
|
222
|
+
resources: {},
|
|
223
|
+
subscribers: new Set(),
|
|
224
|
+
};
|
|
225
|
+
this.liveInstances.set(live.record.id, live);
|
|
226
|
+
upsertInstance(live.record);
|
|
227
|
+
try {
|
|
228
|
+
const rpcProcess = createRpcProcessInstance({ cwd: options.cwd });
|
|
229
|
+
this.bindRpcProcess(live, rpcProcess);
|
|
230
|
+
await this.syncInstanceRecord(live);
|
|
231
|
+
const registeredRecord = await radiusPresence.registerPi(live.record);
|
|
232
|
+
this.updateRecord(live, { radiusPiId: registeredRecord.radiusPiId });
|
|
233
|
+
this.setStatus(live, "online");
|
|
234
|
+
return cloneInstance(live.record);
|
|
235
|
+
}
|
|
236
|
+
catch (error) {
|
|
237
|
+
return await this.failSpawn(live, error);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
async stopInstance(instanceId) {
|
|
241
|
+
const live = this.liveInstances.get(instanceId);
|
|
242
|
+
if (!live) {
|
|
243
|
+
return undefined;
|
|
244
|
+
}
|
|
245
|
+
this.setStatus(live, "stopping");
|
|
246
|
+
try {
|
|
247
|
+
await this.cleanupAcquiredResources(live);
|
|
248
|
+
}
|
|
249
|
+
finally {
|
|
250
|
+
live.record = {
|
|
251
|
+
...live.record,
|
|
252
|
+
status: "stopped",
|
|
253
|
+
lastSeenAt: new Date().toISOString(),
|
|
254
|
+
};
|
|
255
|
+
this.liveInstances.delete(instanceId);
|
|
256
|
+
removeInstance(instanceId);
|
|
257
|
+
}
|
|
258
|
+
return cloneInstance(live.record);
|
|
259
|
+
}
|
|
260
|
+
async handleRpc(instanceId, command) {
|
|
261
|
+
const live = this.liveInstances.get(instanceId);
|
|
262
|
+
const rpcProcess = live ? this.getRpcProcess(live) : undefined;
|
|
263
|
+
if (!live || !rpcProcess) {
|
|
264
|
+
return undefined;
|
|
265
|
+
}
|
|
266
|
+
const response = await rpcProcess.send(command);
|
|
267
|
+
if (shouldRefreshSessionMetadata(command)) {
|
|
268
|
+
await this.syncInstanceRecord(live);
|
|
269
|
+
}
|
|
270
|
+
return response;
|
|
271
|
+
}
|
|
272
|
+
async shutdown() {
|
|
273
|
+
for (const instanceId of [...this.liveInstances.keys()]) {
|
|
274
|
+
await this.stopInstance(instanceId);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
export const supervisor = new ServerSupervisor();
|
|
279
|
+
radiusPresence.setCoordinator({
|
|
280
|
+
getLiveInstance(instanceId) {
|
|
281
|
+
return supervisor.getLiveInstance(instanceId);
|
|
282
|
+
},
|
|
283
|
+
listLiveInstances() {
|
|
284
|
+
return supervisor.listLiveInstances();
|
|
285
|
+
},
|
|
286
|
+
updateInstance(instance) {
|
|
287
|
+
supervisor.updateInstance(instance);
|
|
288
|
+
},
|
|
289
|
+
});
|
|
290
|
+
//# sourceMappingURL=supervisor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"supervisor.js","sourceRoot":"","sources":["../src/supervisor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AASzC,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,wBAAwB,EAA2B,MAAM,kBAAkB,CAAC;AACrF,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,cAAc,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAkBzG,SAAS,aAAa,CAAC,MAAsB,EAAkB;IAC9D,OAAO,EAAE,GAAG,MAAM,EAAE,CAAC;AAAA,CACrB;AAED,mFAAmF;AACnF,uFAAuF;AACvF,yFAAyF;AACzF,EAAE;AACF,iFAAiF;AACjF,yFAAyF;AACzF,2FAA2F;AAC3F,MAAM,yBAAyB,GAAoC,IAAI,GAAG,CAAC;IAC1E,aAAa;IACb,gBAAgB;IAChB,MAAM;IACN,OAAO;IACP,kBAAkB;IAClB,QAAQ;CACR,CAAC,CAAC;AAEH,SAAS,4BAA4B,CAAC,OAAmB,EAAW;IACnE,OAAO,yBAAyB,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;AAAA,CACnD;AAED,SAAS,iBAAiB,CACzB,QAAqB,EAIpB;IACD,OAAO,QAAQ,CAAC,OAAO,KAAK,IAAI,IAAI,QAAQ,CAAC,OAAO,KAAK,WAAW,IAAI,MAAM,IAAI,QAAQ,CAAC;AAAA,CAC3F;AAED,MAAM,OAAO,gBAAgB;IACX,aAAa,GAAG,IAAI,GAAG,EAAwB,CAAC;IAEzD,SAAS,CAAC,IAAkB,EAAE,MAAsB,EAAQ;QACnE,IAAI,CAA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AO,CAAC,EAAE,CAAC;YAC3C,MAAM,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC;QACrC,CAAC;QACD,OAAO,QAAQ,CAAC;IAAA,CAChB;IAED,KAAK,CAAC,QAAQ,GAAkB;QAC/B,KAAK,MAAM,UAAU,IAAI,CAAC,GAAG,IAAI,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC;YACzD,MAAM,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,CAAC;QACrC,CAAC;IAAA,CACD;CACD;AAED,MAAM,CAAC,MAAM,UAAU,GAAG,IAAI,gBAAgB,EAAE,CAAC;AAEjD,cAAc,CAAC,cAAc,CAAC;IAC7B,eAAe,CAAC,UAAU,EAAE;QAC3B,OAAO,UAAU,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC;IAAA,CAC9C;IACD,iBAAiB,GAAG;QACnB,OAAO,UAAU,CAAC,iBAAiB,EAAE,CAAC;IAAA,CACtC;IACD,cAAc,CAAC,QAAQ,EAAE;QACxB,UAAU,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;IAAA,CACpC;CACD,CAAC,CAAC","sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport type {\n\tAgentSessionEvent,\n\tAgentSessionEventListener,\n\tRpcCommand,\n\tRpcExtensionUIRequest,\n\tRpcExtensionUIResponse,\n\tRpcResponse,\n} from \"@earendil-works/pi-coding-agent\";\nimport { radiusPresence } from \"./radius.ts\";\nimport { createRpcProcessInstance, type RpcProcessInstance } from \"./rpc-process.ts\";\nimport { getInstance, loadInstances, removeInstance, saveInstances, upsertInstance } from \"./storage.ts\";\nimport type { InstanceRecord, InstanceStatus } from \"./types.ts\";\n\ninterface LiveInstanceResources {\n\trpcProcess?: RpcProcessInstance;\n\tradiusPiId?: string;\n\tsessionId?: string;\n}\n\ninterface LiveInstance {\n\trecord: InstanceRecord;\n\tresources: LiveInstanceResources;\n\tsubscribers: Set<AgentSessionEventListener>;\n\tonUiRequest?: (request: RpcExtensionUIRequest) => void;\n\tunsubscribeEvents?: () => void;\n\tunsubscribeExit?: () => void;\n}\n\nfunction cloneInstance(record: InstanceRecord): InstanceRecord {\n\treturn { ...record };\n}\n\n// Only refresh persisted session metadata after commands that can plausibly change\n// the instance identity/details we store in instances.json. Most RPCs mutate transient\n// runtime state only, so forcing a follow-up get_state after every command is wasted IO.\n//\n// - new_session / switch_session / fork / clone can change sessionId/sessionFile\n// - set_session_name changes a persisted session detail we may want reflected externally\n// - prompt can materialize or advance persisted session state after the child processes it\nconst SESSION_METADATA_COMMANDS: ReadonlySet<RpcCommand[\"type\"]> = new Set([\n\t\"new_session\",\n\t\"switch_session\",\n\t\"fork\",\n\t\"clone\",\n\t\"set_session_name\",\n\t\"prompt\",\n]);\n\nfunction shouldRefreshSessionMetadata(command: RpcCommand): boolean {\n\treturn SESSION_METADATA_COMMANDS.has(command.type);\n}\n\nfunction isGetStateSuccess(\n\tresponse: RpcResponse,\n): response is Extract<\n\tRpcResponse,\n\t{ success: true; command: \"get_state\"; data: { sessionId: string; sessionFile?: string } }\n> {\n\treturn response.success === true && response.command === \"get_state\" && \"data\" in response;\n}\n\nexport class ServerSupervisor {\n\tprivate readonly liveInstances = new Map<string, LiveInstance>();\n\n\tprivate setStatus(live: LiveInstance, status: InstanceStatus): void {\n\t\tlive.record = {\n\t\t\t...live.record,\n\t\t\tstatus,\n\t\t\tlastSeenAt: new Date().toISOString(),\n\t\t};\n\t\tupsertInstance(live.record);\n\t}\n\n\tprivate updateRecord(live: LiveInstance, updates: Partial<InstanceRecord>): void {\n\t\tlive.record = {\n\t\t\t...live.record,\n\t\t\t...updates,\n\t\t\tlastSeenAt: new Date().toISOString(),\n\t\t};\n\t\tif (updates.radiusPiId !== undefined) {\n\t\t\tlive.resources.radiusPiId = updates.radiusPiId;\n\t\t}\n\t\tif (updates.sessionId !== undefined) {\n\t\t\tlive.resources.sessionId = updates.sessionId;\n\t\t}\n\t\tupsertInstance(live.record);\n\t}\n\n\tprivate clearBindings(live: LiveInstance): void {\n\t\tlive.unsubscribeEvents?.();\n\t\tlive.unsubscribeExit?.();\n\t\tlive.unsubscribeEvents = undefined;\n\t\tlive.unsubscribeExit = undefined;\n\t\tlive.onUiRequest = undefined;\n\t\tlive.resources.rpcProcess?.setUiRequestHandler(undefined);\n\t}\n\n\tprivate bindRpcProcess(live: LiveInstance, rpcProcess: RpcProcessInstance): void {\n\t\tthis.clearBindings(live);\n\t\tlive.resources.rpcProcess = rpcProcess;\n\t\tlive.unsubscribeEvents = rpcProcess.onEvent((event) => {\n\t\t\tfor (const subscriber of live.subscribers) {\n\t\t\t\tsubscriber(event);\n\t\t\t}\n\t\t});\n\t\tlive.unsubscribeExit = rpcProcess.onExit((error) => {\n\t\t\tvoid this.handleUnexpectedRpcExit(live, error);\n\t\t});\n\t\trpcProcess.setUiRequestHandler((request) => {\n\t\t\tlive.onUiRequest?.(request);\n\t\t});\n\t}\n\n\tprivate async handleUnexpectedRpcExit(live: LiveInstance, _error?: Error): Promise<void> {\n\t\tif (this.liveInstances.get(live.record.id) !== live) {\n\t\t\treturn;\n\t\t}\n\t\tif (live.record.status === \"stopping\" || live.record.status === \"stopped\") {\n\t\t\treturn;\n\t\t}\n\t\tthis.setStatus(live, \"error\");\n\t\tthis.clearBindings(live);\n\t\tlive.resources.rpcProcess = undefined;\n\t\tif (live.resources.radiusPiId) {\n\t\t\ttry {\n\t\t\t\tawait radiusPresence.disconnectPi(live.record);\n\t\t\t\tthis.updateRecord(live, { radiusPiId: undefined });\n\t\t\t} catch (error) {\n\t\t\t\tconsole.error(`Failed to disconnect Radius Pi ${live.record.id}: ${String(error)}`);\n\t\t\t}\n\t\t}\n\t\tthis.liveInstances.delete(live.record.id);\n\t}\n\n\tprivate getRpcProcess(live: LiveInstance): RpcProcessInstance | undefined {\n\t\treturn live.resources.rpcProcess;\n\t}\n\n\tprivate async syncInstanceRecord(live: LiveInstance): Promise<void> {\n\t\tconst rpcProcess = this.getRpcProcess(live);\n\t\tif (!rpcProcess) {\n\t\t\tthis.updateRecord(live, {});\n\t\t\treturn;\n\t\t}\n\t\tconst response = await rpcProcess.send({ type: \"get_state\" });\n\t\tif (!isGetStateSuccess(response)) {\n\t\t\tthis.updateRecord(live, {});\n\t\t\treturn;\n\t\t}\n\t\tthis.updateRecord(live, {\n\t\t\tsessionId: response.data.sessionId,\n\t\t\tsessionFile: response.data.sessionFile,\n\t\t});\n\t}\n\n\tprivate async cleanupAcquiredResources(live: LiveInstance): Promise<void> {\n\t\tconst rpcProcess = live.resources.rpcProcess;\n\t\tthis.clearBindings(live);\n\t\tif (live.resources.radiusPiId) {\n\t\t\tawait radiusPresence.disconnectPi(live.record);\n\t\t\tlive.resources.radiusPiId = undefined;\n\t\t\tlive.record = {\n\t\t\t\t...live.record,\n\t\t\t\tradiusPiId: undefined,\n\t\t\t\tlastSeenAt: new Date().toISOString(),\n\t\t\t};\n\t\t}\n\t\tlive.resources.sessionId = undefined;\n\t\tif (rpcProcess) {\n\t\t\tlive.resources.rpcProcess = undefined;\n\t\t\tawait rpcProcess.dispose();\n\t\t}\n\t}\n\n\tprivate async failSpawn(live: LiveInstance, error: unknown): Promise<never> {\n\t\tthis.setStatus(live, \"error\");\n\t\ttry {\n\t\t\tawait this.cleanupAcquiredResources(live);\n\t\t} finally {\n\t\t\tthis.setStatus(live, \"stopped\");\n\t\t\tthis.liveInstances.delete(live.record.id);\n\t\t}\n\t\tthrow error;\n\t}\n\n\tupdateInstance(instance: InstanceRecord): void {\n\t\tconst live = this.liveInstances.get(instance.id);\n\t\tif (live) {\n\t\t\tlive.record = instance;\n\t\t\tlive.resources.radiusPiId = instance.radiusPiId;\n\t\t\tlive.resources.sessionId = instance.sessionId;\n\t\t}\n\t\tupsertInstance(instance);\n\t}\n\n\topenRpcStream(\n\t\tinstanceId: string,\n\t\tonEvent: (event: AgentSessionEvent) => void,\n\t\tonUiRequest: (request: RpcExtensionUIRequest) => void,\n\t):\n\t\t| {\n\t\t\t\thandleRpc(command: RpcCommand): Promise<RpcResponse>;\n\t\t\t\thandleUiResponse(response: RpcExtensionUIResponse): void;\n\t\t\t\tclose(): void;\n\t\t }\n\t\t| undefined {\n\t\tconst live = this.liveInstances.get(instanceId);\n\t\tconst rpcProcess = live ? this.getRpcProcess(live) : undefined;\n\t\tif (!live || !rpcProcess) {\n\t\t\treturn undefined;\n\t\t}\n\t\tlive.subscribers.add(onEvent);\n\t\tlive.onUiRequest = onUiRequest;\n\t\treturn {\n\t\t\thandleRpc: async (command) => {\n\t\t\t\tconst response = await rpcProcess.send(command);\n\t\t\t\tif (shouldRefreshSessionMetadata(command)) {\n\t\t\t\t\tawait this.syncInstanceRecord(live);\n\t\t\t\t}\n\t\t\t\treturn response;\n\t\t\t},\n\t\t\thandleUiResponse: (response) => {\n\t\t\t\trpcProcess.handleUiResponse(response);\n\t\t\t},\n\t\t\tclose: () => {\n\t\t\t\tif (live.onUiRequest === onUiRequest) {\n\t\t\t\t\tlive.onUiRequest = undefined;\n\t\t\t\t}\n\t\t\t\tlive.subscribers.delete(onEvent);\n\t\t\t},\n\t\t};\n\t}\n\n\tgetLiveInstance(instanceId: string): InstanceRecord | undefined {\n\t\tconst live = this.liveInstances.get(instanceId);\n\t\treturn live ? cloneInstance(live.record) : undefined;\n\t}\n\n\tlistLiveInstances(): InstanceRecord[] {\n\t\treturn [...this.liveInstances.values()].map((live) => cloneInstance(live.record));\n\t}\n\n\tasync recoverAfterRestart(): Promise<void> {\n\t\tconst recoveredAt = new Date().toISOString();\n\t\tconst instances = loadInstances().map((instance) => ({\n\t\t\t...instance,\n\t\t\tstatus: instance.status === \"online\" || instance.status === \"starting\" ? \"stopped\" : instance.status,\n\t\t\tlastSeenAt: recoveredAt,\n\t\t}));\n\t\tfor (const instance of instances) {\n\t\t\tawait radiusPresence.disconnectPi(instance);\n\t\t}\n\t\tsaveInstances(instances);\n\t}\n\n\tlistInstances(): InstanceRecord[] {\n\t\treturn loadInstances().map(cloneInstance);\n\t}\n\n\tgetInstance(instanceId: string): InstanceRecord | undefined {\n\t\tconst live = this.liveInstances.get(instanceId);\n\t\tif (live) {\n\t\t\treturn cloneInstance(live.record);\n\t\t}\n\t\tconst stored = getInstance(instanceId);\n\t\treturn stored ? cloneInstance(stored) : undefined;\n\t}\n\n\tasync spawnInstance(options: { cwd: string; label?: string }): Promise<InstanceRecord> {\n\t\tconst now = new Date().toISOString();\n\t\tconst live: LiveInstance = {\n\t\t\trecord: {\n\t\t\t\tid: randomUUID(),\n\t\t\t\tstatus: \"starting\",\n\t\t\t\tcwd: options.cwd,\n\t\t\t\tcreatedAt: now,\n\t\t\t\tlastSeenAt: now,\n\t\t\t\tlabel: options.label,\n\t\t\t},\n\t\t\tresources: {},\n\t\t\tsubscribers: new Set(),\n\t\t};\n\t\tthis.liveInstances.set(live.record.id, live);\n\t\tupsertInstance(live.record);\n\n\t\ttry {\n\t\t\tconst rpcProcess = createRpcProcessInstance({ cwd: options.cwd });\n\t\t\tthis.bindRpcProcess(live, rpcProcess);\n\t\t\tawait this.syncInstanceRecord(live);\n\t\t\tconst registeredRecord = await radiusPresence.registerPi(live.record);\n\t\t\tthis.updateRecord(live, { radiusPiId: registeredRecord.radiusPiId });\n\t\t\tthis.setStatus(live, \"online\");\n\t\t\treturn cloneInstance(live.record);\n\t\t} catch (error) {\n\t\t\treturn await this.failSpawn(live, error);\n\t\t}\n\t}\n\n\tasync stopInstance(instanceId: string): Promise<InstanceRecord | undefined> {\n\t\tconst live = this.liveInstances.get(instanceId);\n\t\tif (!live) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tthis.setStatus(live, \"stopping\");\n\t\ttry {\n\t\t\tawait this.cleanupAcquiredResources(live);\n\t\t} finally {\n\t\t\tlive.record = {\n\t\t\t\t...live.record,\n\t\t\t\tstatus: \"stopped\",\n\t\t\t\tlastSeenAt: new Date().toISOString(),\n\t\t\t};\n\t\t\tthis.liveInstances.delete(instanceId);\n\t\t\tremoveInstance(instanceId);\n\t\t}\n\t\treturn cloneInstance(live.record);\n\t}\n\n\tasync handleRpc(instanceId: string, command: RpcCommand): Promise<RpcResponse | undefined> {\n\t\tconst live = this.liveInstances.get(instanceId);\n\t\tconst rpcProcess = live ? this.getRpcProcess(live) : undefined;\n\t\tif (!live || !rpcProcess) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst response = await rpcProcess.send(command);\n\t\tif (shouldRefreshSessionMetadata(command)) {\n\t\t\tawait this.syncInstanceRecord(live);\n\t\t}\n\t\treturn response;\n\t}\n\n\tasync shutdown(): Promise<void> {\n\t\tfor (const instanceId of [...this.liveInstances.keys()]) {\n\t\t\tawait this.stopInstance(instanceId);\n\t\t}\n\t}\n}\n\nexport const supervisor = new ServerSupervisor();\n\nradiusPresence.setCoordinator({\n\tgetLiveInstance(instanceId) {\n\t\treturn supervisor.getLiveInstance(instanceId);\n\t},\n\tlistLiveInstances() {\n\t\treturn supervisor.listLiveInstances();\n\t},\n\tupdateInstance(instance) {\n\t\tsupervisor.updateInstance(instance);\n\t},\n});\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { ServiceCall } from "@earendil-works/chord";
|
|
2
|
+
import { type ClientMessage, type ResponseEnvelope, type RpcTarget, type ServerMessage } from "@earendil-works/pi-protocol";
|
|
3
|
+
export interface WireChannel {
|
|
4
|
+
send(chunk: Uint8Array): Promise<void>;
|
|
5
|
+
sendFragmented(chunk: Uint8Array, splitAt: number): Promise<void>;
|
|
6
|
+
close(): Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
export declare class ProtocolTestClient {
|
|
9
|
+
readonly messages: ServerMessage[];
|
|
10
|
+
private readonly channel;
|
|
11
|
+
private readonly decoder;
|
|
12
|
+
private readonly waiters;
|
|
13
|
+
private readonly closedDeferred;
|
|
14
|
+
private requestSequence;
|
|
15
|
+
private attachment;
|
|
16
|
+
private closedValue;
|
|
17
|
+
constructor(channel: WireChannel);
|
|
18
|
+
get closed(): boolean;
|
|
19
|
+
hello(version?: number): Promise<ServerMessage>;
|
|
20
|
+
requestService(target: RpcTarget, call: ServiceCall, id?: string): Promise<ResponseEnvelope>;
|
|
21
|
+
attach(serverId: string, sessionId: string): Promise<ResponseEnvelope>;
|
|
22
|
+
requestSessionService(serverId: string, sessionId: string, call: ServiceCall, id?: string): Promise<ResponseEnvelope>;
|
|
23
|
+
sendMessage(message: ClientMessage): Promise<void>;
|
|
24
|
+
sendBytes(chunk: Uint8Array): Promise<void>;
|
|
25
|
+
sendFragmentedMessage(message: ClientMessage, splitAt: number): Promise<void>;
|
|
26
|
+
next(predicate: (message: ServerMessage) => boolean): Promise<ServerMessage>;
|
|
27
|
+
nextFrom(index: number, predicate: (message: ServerMessage) => boolean): Promise<ServerMessage>;
|
|
28
|
+
waitForClose(): Promise<void>;
|
|
29
|
+
close(): Promise<void>;
|
|
30
|
+
receive(chunk: Uint8Array): void;
|
|
31
|
+
markClosed(): void;
|
|
32
|
+
fail(error: Error): void;
|
|
33
|
+
}
|
|
34
|
+
export declare function connectUnixTestClient(path: string): Promise<ProtocolTestClient>;
|
|
35
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/testing/client.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAa,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpE,OAAO,EACN,KAAK,aAAa,EAGlB,KAAK,gBAAgB,EACrB,KAAK,SAAS,EACd,KAAK,aAAa,EAElB,MAAM,6BAA6B,CAAC;AASrC,MAAM,WAAW,WAAW;IAC3B,IAAI,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACvC,cAAc,CAAC,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAClE,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACvB;AAED,qBAAa,kBAAkB;IAC9B,QAAQ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AAC,OAAO,EAAE,aAAa,KAAK,OAAO,GAAG,OAAO,CAAC,aAAa,CAAC,CAK9F;IAED,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC,CAE5B;IAED,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAErB;IAED,OAAO,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CAsB/B;IAED,UAAU,IAAI,IAAI,CAKjB;IAED,IAAI,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,CAGvB;CACD;AAED,wBAAsB,qBAAqB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAsBrF","sourcesContent":["import { once } from \"node:events\";\nimport { createConnection, type Socket } from \"node:net\";\nimport type { JsonValue, ServiceCall } from \"@earendil-works/chord\";\nimport {\n\ttype ClientMessage,\n\tencodeClientMessage,\n\tPROTOCOL_VERSION,\n\ttype ResponseEnvelope,\n\ttype RpcTarget,\n\ttype ServerMessage,\n\tServerMessageDecoder,\n} from \"@earendil-works/pi-protocol\";\nimport { Deferred } from \"./host.ts\";\n\ninterface MessageWaiter {\n\tpredicate: (message: ServerMessage) => boolean;\n\tresolve: (message: ServerMessage) => void;\n\treject: (error: Error) => void;\n}\n\nexport interface WireChannel {\n\tsend(chunk: Uint8Array): Promise<void>;\n\tsendFragmented(chunk: Uint8Array, splitAt: number): Promise<void>;\n\tclose(): Promise<void>;\n}\n\nexport class ProtocolTestClient {\n\treadonly messages: ServerMessage[] = [];\n\tprivate readonly channel: WireChannel;\n\tprivate readonly decoder = new ServerMessageDecoder();\n\tprivate readonly waiters = new Set<MessageWaiter>();\n\tprivate readonly closedDeferred = new Deferred<void>();\n\tprivate requestSequence = 0;\n\tprivate attachment: { sessionId: string; attachmentId: string } | undefined;\n\tprivate closedValue = false;\n\n\tconstructor(channel: WireChannel) {\n\t\tthis.channel = channel;\n\t}\n\n\tget closed(): boolean {\n\t\treturn this.closedValue;\n\t}\n\n\thello(version: number = PROTOCOL_VERSION): Promise<ServerMessage> {\n\t\tconst response = this.next((message) => message.type === \"hello\" || message.type === \"hello_error\");\n\t\tvoid this.sendMessage({ type: \"hello\", version });\n\t\treturn response;\n\t}\n\n\tasync requestService(\n\t\ttarget: RpcTarget,\n\t\tcall: ServiceCall,\n\t\tid = `request-${++this.requestSequence}`,\n\t): Promise<ResponseEnvelope> {\n\t\tconst response = this.next(\n\t\t\t(message): message is ResponseEnvelope => message.type === \"response\" && message.id === id,\n\t\t);\n\t\tawait this.sendMessage({ type: \"request\", id, target, call: call as unknown as JsonValue });\n\t\treturn (await response) as ResponseEnvelope;\n\t}\n\n\tattach(serverId: string, sessionId: string): Promise<ResponseEnvelope> {\n\t\treturn this.requestService(\n\t\t\t{ serverId },\n\t\t\t{ serviceId: \"pi.session-management\", member: \"attach\", args: [sessionId] },\n\t\t);\n\t}\n\n\trequestSessionService(\n\t\tserverId: string,\n\t\tsessionId: string,\n\t\tcall: ServiceCall,\n\t\tid?: string,\n\t): Promise<ResponseEnvelope> {\n\t\tconst attachment = this.attachment;\n\t\tconst target: RpcTarget =\n\t\t\tattachment === undefined || attachment.sessionId !== sessionId\n\t\t\t\t? { serverId, sessionId, attachmentId: \"missing-attachment\" }\n\t\t\t\t: { serverId, ...attachment };\n\t\treturn this.requestService(target, call, id);\n\t}\n\n\tsendMessage(message: ClientMessage): Promise<void> {\n\t\treturn this.channel.send(encodeClientMessage(message));\n\t}\n\n\tsendBytes(chunk: Uint8Array): Promise<void> {\n\t\treturn this.channel.send(chunk);\n\t}\n\n\tsendFragmentedMessage(message: ClientMessage, splitAt: number): Promise<void> {\n\t\treturn this.channel.sendFragmented(encodeClientMessage(message), splitAt);\n\t}\n\n\tnext(predicate: (message: ServerMessage) => boolean): Promise<ServerMessage> {\n\t\treturn this.nextFrom(0, predicate);\n\t}\n\n\tnextFrom(index: number, predicate: (message: ServerMessage) => boolean): Promise<ServerMessage> {\n\t\tconst existing = this.messages.slice(index).find(predicate);\n\t\tif (existing) return Promise.resolve(existing);\n\t\tif (this.closedValue) return Promise.reject(new Error(\"Wire client is closed\"));\n\t\treturn new Promise((resolve, reject) => this.waiters.add({ predicate, resolve, reject }));\n\t}\n\n\twaitForClose(): Promise<void> {\n\t\treturn this.closedValue ? Promise.resolve() : this.closedDeferred.promise;\n\t}\n\n\tclose(): Promise<void> {\n\t\treturn this.channel.close();\n\t}\n\n\treceive(chunk: Uint8Array): void {\n\t\ttry {\n\t\t\tfor (const message of this.decoder.push(chunk)) {\n\t\t\t\tif (message.type === \"attachment\") {\n\t\t\t\t\tthis.attachment =\n\t\t\t\t\t\tmessage.attachment === null\n\t\t\t\t\t\t\t? undefined\n\t\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\t\tsessionId: message.attachment.sessionId,\n\t\t\t\t\t\t\t\t\tattachmentId: message.attachment.attachmentId,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\tthis.messages.push(message);\n\t\t\t\tfor (const waiter of this.waiters) {\n\t\t\t\t\tif (!waiter.predicate(message)) continue;\n\t\t\t\t\tthis.waiters.delete(waiter);\n\t\t\t\t\twaiter.resolve(message);\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tthis.fail(error instanceof Error ? error : new Error(String(error)));\n\t\t}\n\t}\n\n\tmarkClosed(): void {\n\t\tif (this.closedValue) return;\n\t\tthis.closedValue = true;\n\t\tthis.closedDeferred.resolve(undefined);\n\t\tthis.fail(new Error(\"Wire connection closed\"));\n\t}\n\n\tfail(error: Error): void {\n\t\tfor (const waiter of this.waiters) waiter.reject(error);\n\t\tthis.waiters.clear();\n\t}\n}\n\nexport async function connectUnixTestClient(path: string): Promise<ProtocolTestClient> {\n\tconst socket = createConnection(path);\n\tawait once(socket, \"connect\");\n\tconst client = new ProtocolTestClient({\n\t\tsend: (chunk) => writeSocket(socket, chunk),\n\t\tasync sendFragmented(chunk, splitAt) {\n\t\t\tawait writeSocket(socket, chunk.subarray(0, splitAt));\n\t\t\tawait writeSocket(socket, chunk.subarray(splitAt));\n\t\t},\n\t\tasync close() {\n\t\t\tif (socket.destroyed) return;\n\t\t\tconst closed = once(socket, \"close\");\n\t\t\tsocket.destroy();\n\t\t\tawait closed;\n\t\t},\n\t});\n\tsocket.on(\"data\", (chunk) => {\n\t\tclient.receive(new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength));\n\t});\n\tsocket.on(\"error\", (error) => client.fail(error));\n\tsocket.once(\"close\", () => client.markClosed());\n\treturn client;\n}\n\nfunction writeSocket(socket: Socket, chunk: Uint8Array): Promise<void> {\n\treturn new Promise<void>((resolve, reject) => {\n\t\tsocket.write(chunk, (error) => {\n\t\t\tif (error) reject(error);\n\t\t\telse resolve();\n\t\t});\n\t});\n}\n"]}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { once } from "node:events";
|
|
2
|
+
import { createConnection } from "node:net";
|
|
3
|
+
import { encodeClientMessage, PROTOCOL_VERSION, ServerMessageDecoder, } from "@earendil-works/pi-protocol";
|
|
4
|
+
import { Deferred } from "./host.js";
|
|
5
|
+
export class ProtocolTestClient {
|
|
6
|
+
messages = [];
|
|
7
|
+
channel;
|
|
8
|
+
decoder = new ServerMessageDecoder();
|
|
9
|
+
waiters = new Set();
|
|
10
|
+
closedDeferred = new Deferred();
|
|
11
|
+
requestSequence = 0;
|
|
12
|
+
attachment;
|
|
13
|
+
closedValue = false;
|
|
14
|
+
constructor(channel) {
|
|
15
|
+
this.channel = channel;
|
|
16
|
+
}
|
|
17
|
+
get closed() {
|
|
18
|
+
return this.closedValue;
|
|
19
|
+
}
|
|
20
|
+
hello(version = PROTOCOL_VERSION) {
|
|
21
|
+
const response = this.next((message) => message.type === "hello" || message.type === "hello_error");
|
|
22
|
+
void this.sendMessage({ type: "hello", version });
|
|
23
|
+
return response;
|
|
24
|
+
}
|
|
25
|
+
async requestService(target, call, id = `request-${++this.requestSequence}`) {
|
|
26
|
+
const response = this.next((message) => message.type === "response" && message.id === id);
|
|
27
|
+
await this.sendMessage({ type: "request", id, target, call: call });
|
|
28
|
+
return (await response);
|
|
29
|
+
}
|
|
30
|
+
attach(serverId, sessionId) {
|
|
31
|
+
return this.requestService({ serverId }, { serviceId: "pi.session-management", member: "attach", args: [sessionId] });
|
|
32
|
+
}
|
|
33
|
+
requestSessionService(serverId, sessionId, call, id) {
|
|
34
|
+
const attachment = this.attachment;
|
|
35
|
+
const target = attachment === undefined || attachment.sessionId !== sessionId
|
|
36
|
+
? { serverId, sessionId, attachmentId: "missing-attachment" }
|
|
37
|
+
: { serverId, ...attachment };
|
|
38
|
+
return this.requestService(target, call, id);
|
|
39
|
+
}
|
|
40
|
+
sendMessage(message) {
|
|
41
|
+
return this.channel.send(encodeClientMessage(message));
|
|
42
|
+
}
|
|
43
|
+
sendBytes(chunk) {
|
|
44
|
+
return this.channel.send(chunk);
|
|
45
|
+
}
|
|
46
|
+
sendFragmentedMessage(message, splitAt) {
|
|
47
|
+
return this.channel.sendFragmented(encodeClientMessage(message), splitAt);
|
|
48
|
+
}
|
|
49
|
+
next(predicate) {
|
|
50
|
+
return this.nextFrom(0, predicate);
|
|
51
|
+
}
|
|
52
|
+
nextFrom(index, predicate) {
|
|
53
|
+
const existing = this.messages.slice(index).find(predicate);
|
|
54
|
+
if (existing)
|
|
55
|
+
return Promise.resolve(existing);
|
|
56
|
+
if (this.closedValue)
|
|
57
|
+
return Promise.reject(new Error("Wire client is closed"));
|
|
58
|
+
return new Promise((resolve, reject) => this.waiters.add({ predicate, resolve, reject }));
|
|
59
|
+
}
|
|
60
|
+
waitForClose() {
|
|
61
|
+
return this.closedValue ? Promise.resolve() : this.closedDeferred.promise;
|
|
62
|
+
}
|
|
63
|
+
close() {
|
|
64
|
+
return this.channel.close();
|
|
65
|
+
}
|
|
66
|
+
receive(chunk) {
|
|
67
|
+
try {
|
|
68
|
+
for (const message of this.decoder.push(chunk)) {
|
|
69
|
+
if (message.type === "attachment") {
|
|
70
|
+
this.attachment =
|
|
71
|
+
message.attachment === null
|
|
72
|
+
? undefined
|
|
73
|
+
: {
|
|
74
|
+
sessionId: message.attachment.sessionId,
|
|
75
|
+
attachmentId: message.attachment.attachmentId,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
this.messages.push(message);
|
|
79
|
+
for (const waiter of this.waiters) {
|
|
80
|
+
if (!waiter.predicate(message))
|
|
81
|
+
continue;
|
|
82
|
+
this.waiters.delete(waiter);
|
|
83
|
+
waiter.resolve(message);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
this.fail(error instanceof Error ? error : new Error(String(error)));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
markClosed() {
|
|
92
|
+
if (this.closedValue)
|
|
93
|
+
return;
|
|
94
|
+
this.closedValue = true;
|
|
95
|
+
this.closedDeferred.resolve(undefined);
|
|
96
|
+
this.fail(new Error("Wire connection closed"));
|
|
97
|
+
}
|
|
98
|
+
fail(error) {
|
|
99
|
+
for (const waiter of this.waiters)
|
|
100
|
+
waiter.reject(error);
|
|
101
|
+
this.waiters.clear();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
export async function connectUnixTestClient(path) {
|
|
105
|
+
const socket = createConnection(path);
|
|
106
|
+
await once(socket, "connect");
|
|
107
|
+
const client = new ProtocolTestClient({
|
|
108
|
+
send: (chunk) => writeSocket(socket, chunk),
|
|
109
|
+
async sendFragmented(chunk, splitAt) {
|
|
110
|
+
await writeSocket(socket, chunk.subarray(0, splitAt));
|
|
111
|
+
await writeSocket(socket, chunk.subarray(splitAt));
|
|
112
|
+
},
|
|
113
|
+
async close() {
|
|
114
|
+
if (socket.destroyed)
|
|
115
|
+
return;
|
|
116
|
+
const closed = once(socket, "close");
|
|
117
|
+
socket.destroy();
|
|
118
|
+
await closed;
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
socket.on("data", (chunk) => {
|
|
122
|
+
client.receive(new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength));
|
|
123
|
+
});
|
|
124
|
+
socket.on("error", (error) => client.fail(error));
|
|
125
|
+
socket.once("close", () => client.markClosed());
|
|
126
|
+
return client;
|
|
127
|
+
}
|
|
128
|
+
function writeSocket(socket, chunk) {
|
|
129
|
+
return new Promise((resolve, reject) => {
|
|
130
|
+
socket.write(chunk, (error) => {
|
|
131
|
+
if (error)
|
|
132
|
+
reject(error);
|
|
133
|
+
else
|
|
134
|
+
resolve();
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/testing/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACnC,OAAO,EAAE,gBAAgB,EAAe,MAAM,UAAU,CAAC;AAEzD,OAAO,EAEN,mBAAmB,EACnB,gBAAgB,EAIhB,oBAAoB,GACpB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAcrC,MAAM,OAAO,kBAAkB;IACrB,QAAQ,GAAoB,EAAE,CAAC;IACvB,OAAO,CAAc;IACrB,OAAO,GAAG,IAAI,oBAAoB,EAAE,CAAC;IACrC,OAAO,GAAG,IAAI,GAAG,EAAiB,CAAC;IACnC,cAAc,GAAG,IAAI,QAAQ,EAAQ,CAAC;IAC/C,eAAe,GAAG,CAAC,CAAC;IACpB,UAAU,CAA0D;IACpE,WAAW,GAAG,KAAK,CAAC;IAE5B,YAAY,OAAoB,EAAE;QACjC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IAAA,CACvB;IAED,IAAI,MAAM,GAAY;QACrB,OAAO,IAAI,CAAC,WAAW,CAAC;IAAA,CACxB;IAED,KAAK,CAAC,OAAO,GAAW,gBAAgB,EAA0B;QACjE,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,OA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AC,MAAM,EAAE,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC;IAAA,CACjF,CAAC,CAAC;IACH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;IAClD,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC,CAAC;IAChD,OAAO,MAAM,CAAC;AAAA,CACd;AAED,SAAS,WAAW,CAAC,MAAc,EAAE,KAAiB,EAAiB;IACtE,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,CAAC;QAC7C,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC;YAC9B,IAAI,KAAK;gBAAE,MAAM,CAAC,KAAK,CAAC,CAAC;;gBACpB,OAAO,EAAE,CAAC;QAAA,CACf,CAAC,CAAC;IAAA,CACH,CAAC,CAAC;AAAA,CACH","sourcesContent":["import { once } from \"node:events\";\nimport { createConnection, type Socket } from \"node:net\";\nimport type { JsonValue, ServiceCall } from \"@earendil-works/chord\";\nimport {\n\ttype ClientMessage,\n\tencodeClientMessage,\n\tPROTOCOL_VERSION,\n\ttype ResponseEnvelope,\n\ttype RpcTarget,\n\ttype ServerMessage,\n\tServerMessageDecoder,\n} from \"@earendil-works/pi-protocol\";\nimport { Deferred } from \"./host.ts\";\n\ninterface MessageWaiter {\n\tpredicate: (message: ServerMessage) => boolean;\n\tresolve: (message: ServerMessage) => void;\n\treject: (error: Error) => void;\n}\n\nexport interface WireChannel {\n\tsend(chunk: Uint8Array): Promise<void>;\n\tsendFragmented(chunk: Uint8Array, splitAt: number): Promise<void>;\n\tclose(): Promise<void>;\n}\n\nexport class ProtocolTestClient {\n\treadonly messages: ServerMessage[] = [];\n\tprivate readonly channel: WireChannel;\n\tprivate readonly decoder = new ServerMessageDecoder();\n\tprivate readonly waiters = new Set<MessageWaiter>();\n\tprivate readonly closedDeferred = new Deferred<void>();\n\tprivate requestSequence = 0;\n\tprivate attachment: { sessionId: string; attachmentId: string } | undefined;\n\tprivate closedValue = false;\n\n\tconstructor(channel: WireChannel) {\n\t\tthis.channel = channel;\n\t}\n\n\tget closed(): boolean {\n\t\treturn this.closedValue;\n\t}\n\n\thello(version: number = PROTOCOL_VERSION): Promise<ServerMessage> {\n\t\tconst response = this.next((message) => message.type === \"hello\" || message.type === \"hello_error\");\n\t\tvoid this.sendMessage({ type: \"hello\", version });\n\t\treturn response;\n\t}\n\n\tasync requestService(\n\t\ttarget: RpcTarget,\n\t\tcall: ServiceCall,\n\t\tid = `request-${++this.requestSequence}`,\n\t): Promise<ResponseEnvelope> {\n\t\tconst response = this.next(\n\t\t\t(message): message is ResponseEnvelope => message.type === \"response\" && message.id === id,\n\t\t);\n\t\tawait this.sendMessage({ type: \"request\", id, target, call: call as unknown as JsonValue });\n\t\treturn (await response) as ResponseEnvelope;\n\t}\n\n\tattach(serverId: string, sessionId: string): Promise<ResponseEnvelope> {\n\t\treturn this.requestService(\n\t\t\t{ serverId },\n\t\t\t{ serviceId: \"pi.session-management\", member: \"attach\", args: [sessionId] },\n\t\t);\n\t}\n\n\trequestSessionService(\n\t\tserverId: string,\n\t\tsessionId: string,\n\t\tcall: ServiceCall,\n\t\tid?: string,\n\t): Promise<ResponseEnvelope> {\n\t\tconst attachment = this.attachment;\n\t\tconst target: RpcTarget =\n\t\t\tattachment === undefined || attachment.sessionId !== sessionId\n\t\t\t\t? { serverId, sessionId, attachmentId: \"missing-attachment\" }\n\t\t\t\t: { serverId, ...attachment };\n\t\treturn this.requestService(target, call, id);\n\t}\n\n\tsendMessage(message: ClientMessage): Promise<void> {\n\t\treturn this.channel.send(encodeClientMessage(message));\n\t}\n\n\tsendBytes(chunk: Uint8Array): Promise<void> {\n\t\treturn this.channel.send(chunk);\n\t}\n\n\tsendFragmentedMessage(message: ClientMessage, splitAt: number): Promise<void> {\n\t\treturn this.channel.sendFragmented(encodeClientMessage(message), splitAt);\n\t}\n\n\tnext(predicate: (message: ServerMessage) => boolean): Promise<ServerMessage> {\n\t\treturn this.nextFrom(0, predicate);\n\t}\n\n\tnextFrom(index: number, predicate: (message: ServerMessage) => boolean): Promise<ServerMessage> {\n\t\tconst existing = this.messages.slice(index).find(predicate);\n\t\tif (existing) return Promise.resolve(existing);\n\t\tif (this.closedValue) return Promise.reject(new Error(\"Wire client is closed\"));\n\t\treturn new Promise((resolve, reject) => this.waiters.add({ predicate, resolve, reject }));\n\t}\n\n\twaitForClose(): Promise<void> {\n\t\treturn this.closedValue ? Promise.resolve() : this.closedDeferred.promise;\n\t}\n\n\tclose(): Promise<void> {\n\t\treturn this.channel.close();\n\t}\n\n\treceive(chunk: Uint8Array): void {\n\t\ttry {\n\t\t\tfor (const message of this.decoder.push(chunk)) {\n\t\t\t\tif (message.type === \"attachment\") {\n\t\t\t\t\tthis.attachment =\n\t\t\t\t\t\tmessage.attachment === null\n\t\t\t\t\t\t\t? undefined\n\t\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\t\tsessionId: message.attachment.sessionId,\n\t\t\t\t\t\t\t\t\tattachmentId: message.attachment.attachmentId,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\tthis.messages.push(message);\n\t\t\t\tfor (const waiter of this.waiters) {\n\t\t\t\t\tif (!waiter.predicate(message)) continue;\n\t\t\t\t\tthis.waiters.delete(waiter);\n\t\t\t\t\twaiter.resolve(message);\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tthis.fail(error instanceof Error ? error : new Error(String(error)));\n\t\t}\n\t}\n\n\tmarkClosed(): void {\n\t\tif (this.closedValue) return;\n\t\tthis.closedValue = true;\n\t\tthis.closedDeferred.resolve(undefined);\n\t\tthis.fail(new Error(\"Wire connection closed\"));\n\t}\n\n\tfail(error: Error): void {\n\t\tfor (const waiter of this.waiters) waiter.reject(error);\n\t\tthis.waiters.clear();\n\t}\n}\n\nexport async function connectUnixTestClient(path: string): Promise<ProtocolTestClient> {\n\tconst socket = createConnection(path);\n\tawait once(socket, \"connect\");\n\tconst client = new ProtocolTestClient({\n\t\tsend: (chunk) => writeSocket(socket, chunk),\n\t\tasync sendFragmented(chunk, splitAt) {\n\t\t\tawait writeSocket(socket, chunk.subarray(0, splitAt));\n\t\t\tawait writeSocket(socket, chunk.subarray(splitAt));\n\t\t},\n\t\tasync close() {\n\t\t\tif (socket.destroyed) return;\n\t\t\tconst closed = once(socket, \"close\");\n\t\t\tsocket.destroy();\n\t\t\tawait closed;\n\t\t},\n\t});\n\tsocket.on(\"data\", (chunk) => {\n\t\tclient.receive(new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength));\n\t});\n\tsocket.on(\"error\", (error) => client.fail(error));\n\tsocket.once(\"close\", () => client.markClosed());\n\treturn client;\n}\n\nfunction writeSocket(socket: Socket, chunk: Uint8Array): Promise<void> {\n\treturn new Promise<void>((resolve, reject) => {\n\t\tsocket.write(chunk, (error) => {\n\t\t\tif (error) reject(error);\n\t\t\telse resolve();\n\t\t});\n\t});\n}\n"]}
|