@xyit/wellspring 0.0.0-stage → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +853 -2
- package/dist/agent/pending-asks.d.ts +20 -0
- package/dist/agent/pending-asks.js +70 -0
- package/dist/agent/pending-asks.js.map +1 -0
- package/dist/agent/task-registry.d.ts +14 -0
- package/dist/agent/task-registry.js +80 -0
- package/dist/agent/task-registry.js.map +1 -0
- package/dist/agent/tool-context.d.ts +30 -0
- package/dist/agent/tool-context.js +10 -0
- package/dist/agent/tool-context.js.map +1 -0
- package/dist/agent/usage.d.ts +40 -0
- package/dist/agent/usage.js +122 -0
- package/dist/agent/usage.js.map +1 -0
- package/dist/api/host/agent.d.ts +165 -0
- package/dist/api/host/agent.js +5 -0
- package/dist/api/host/agent.js.map +1 -0
- package/dist/api/host/events.d.ts +2 -0
- package/dist/api/host/events.js +6 -0
- package/dist/api/host/events.js.map +1 -0
- package/dist/api/host/exec.d.ts +36 -0
- package/dist/api/host/exec.js +10 -0
- package/dist/api/host/exec.js.map +1 -0
- package/dist/api/host/fs.d.ts +24 -0
- package/dist/api/host/fs.js +7 -0
- package/dist/api/host/fs.js.map +1 -0
- package/dist/api/host/hooks.d.ts +210 -0
- package/dist/api/host/hooks.js +401 -0
- package/dist/api/host/hooks.js.map +1 -0
- package/dist/api/host/host.d.ts +36 -0
- package/dist/api/host/host.js +53 -0
- package/dist/api/host/host.js.map +1 -0
- package/dist/api/host/http.d.ts +58 -0
- package/dist/api/host/http.js +6 -0
- package/dist/api/host/http.js.map +1 -0
- package/dist/api/host/index.d.ts +15 -0
- package/dist/api/host/index.js +20 -0
- package/dist/api/host/index.js.map +1 -0
- package/dist/api/host/models.d.ts +65 -0
- package/dist/api/host/models.js +8 -0
- package/dist/api/host/models.js.map +1 -0
- package/dist/api/host/network.d.ts +22 -0
- package/dist/api/host/network.js +7 -0
- package/dist/api/host/network.js.map +1 -0
- package/dist/api/host/plugin.d.ts +138 -0
- package/dist/api/host/plugin.js +6 -0
- package/dist/api/host/plugin.js.map +1 -0
- package/dist/api/host/plugins.d.ts +49 -0
- package/dist/api/host/plugins.js +6 -0
- package/dist/api/host/plugins.js.map +1 -0
- package/dist/api/host/pty.d.ts +55 -0
- package/dist/api/host/pty.js +21 -0
- package/dist/api/host/pty.js.map +1 -0
- package/dist/api/host/session.d.ts +5 -0
- package/dist/api/host/session.js +52 -0
- package/dist/api/host/session.js.map +1 -0
- package/dist/api/host/version.d.ts +2 -0
- package/dist/api/host/version.js +3 -0
- package/dist/api/host/version.js.map +1 -0
- package/dist/api/host/workspace.d.ts +38 -0
- package/dist/api/host/workspace.js +9 -0
- package/dist/api/host/workspace.js.map +1 -0
- package/dist/api/plugin/http-client.d.ts +42 -0
- package/dist/api/plugin/http-client.js +364 -0
- package/dist/api/plugin/http-client.js.map +1 -0
- package/dist/api/plugin/http-protocol.d.ts +152 -0
- package/dist/api/plugin/http-protocol.js +49 -0
- package/dist/api/plugin/http-protocol.js.map +1 -0
- package/dist/api/result/result.d.ts +16 -0
- package/dist/api/result/result.js +43 -0
- package/dist/api/result/result.js.map +1 -0
- package/dist/commands/agent-turn.d.ts +63 -0
- package/dist/commands/agent-turn.js +687 -0
- package/dist/commands/agent-turn.js.map +1 -0
- package/dist/commands/ask-prompt.d.ts +6 -0
- package/dist/commands/ask-prompt.js +49 -0
- package/dist/commands/ask-prompt.js.map +1 -0
- package/dist/commands/config.d.ts +2 -0
- package/dist/commands/config.js +283 -0
- package/dist/commands/config.js.map +1 -0
- package/dist/commands/conversation.d.ts +51 -0
- package/dist/commands/conversation.js +630 -0
- package/dist/commands/conversation.js.map +1 -0
- package/dist/commands/doctor.d.ts +2 -0
- package/dist/commands/doctor.js +77 -0
- package/dist/commands/doctor.js.map +1 -0
- package/dist/commands/exec-approval.d.ts +3 -0
- package/dist/commands/exec-approval.js +80 -0
- package/dist/commands/exec-approval.js.map +1 -0
- package/dist/commands/help.d.ts +2 -0
- package/dist/commands/help.js +35 -0
- package/dist/commands/help.js.map +1 -0
- package/dist/commands/key.d.ts +2 -0
- package/dist/commands/key.js +226 -0
- package/dist/commands/key.js.map +1 -0
- package/dist/commands/plugin.d.ts +2 -0
- package/dist/commands/plugin.js +190 -0
- package/dist/commands/plugin.js.map +1 -0
- package/dist/commands/session.d.ts +2 -0
- package/dist/commands/session.js +167 -0
- package/dist/commands/session.js.map +1 -0
- package/dist/commands/shared.d.ts +35 -0
- package/dist/commands/shared.js +66 -0
- package/dist/commands/shared.js.map +1 -0
- package/dist/commands/start.d.ts +2 -0
- package/dist/commands/start.js +75 -0
- package/dist/commands/start.js.map +1 -0
- package/dist/commands/stop.d.ts +2 -0
- package/dist/commands/stop.js +24 -0
- package/dist/commands/stop.js.map +1 -0
- package/dist/commands/version.d.ts +2 -0
- package/dist/commands/version.js +33 -0
- package/dist/commands/version.js.map +1 -0
- package/dist/config/codes.d.ts +13 -0
- package/dist/config/codes.js +15 -0
- package/dist/config/codes.js.map +1 -0
- package/dist/config/credentials.d.ts +67 -0
- package/dist/config/credentials.js +214 -0
- package/dist/config/credentials.js.map +1 -0
- package/dist/config/defaults.d.ts +5 -0
- package/dist/config/defaults.js +31 -0
- package/dist/config/defaults.js.map +1 -0
- package/dist/config/index.d.ts +5 -0
- package/dist/config/index.js +10 -0
- package/dist/config/index.js.map +1 -0
- package/dist/config/loader.d.ts +10 -0
- package/dist/config/loader.js +68 -0
- package/dist/config/loader.js.map +1 -0
- package/dist/config/providers.d.ts +30 -0
- package/dist/config/providers.js +97 -0
- package/dist/config/providers.js.map +1 -0
- package/dist/config/types.d.ts +75 -0
- package/dist/config/types.js +4 -0
- package/dist/config/types.js.map +1 -0
- package/dist/config/validate.d.ts +4 -0
- package/dist/config/validate.js +391 -0
- package/dist/config/validate.js.map +1 -0
- package/dist/display/markdown.d.ts +7 -0
- package/dist/display/markdown.js +116 -0
- package/dist/display/markdown.js.map +1 -0
- package/dist/display/tool-label.d.ts +6 -0
- package/dist/display/tool-label.js +81 -0
- package/dist/display/tool-label.js.map +1 -0
- package/dist/event/event.d.ts +27 -0
- package/dist/event/event.js +152 -0
- package/dist/event/event.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +308 -0
- package/dist/index.js.map +1 -0
- package/dist/input/codes.d.ts +15 -0
- package/dist/input/codes.js +20 -0
- package/dist/input/codes.js.map +1 -0
- package/dist/input/commands.d.ts +10 -0
- package/dist/input/commands.js +65 -0
- package/dist/input/commands.js.map +1 -0
- package/dist/input/help.d.ts +8 -0
- package/dist/input/help.js +51 -0
- package/dist/input/help.js.map +1 -0
- package/dist/input/index.d.ts +6 -0
- package/dist/input/index.js +10 -0
- package/dist/input/index.js.map +1 -0
- package/dist/input/parse-options.d.ts +11 -0
- package/dist/input/parse-options.js +51 -0
- package/dist/input/parse-options.js.map +1 -0
- package/dist/input/parse.d.ts +3 -0
- package/dist/input/parse.js +119 -0
- package/dist/input/parse.js.map +1 -0
- package/dist/input/types.d.ts +17 -0
- package/dist/input/types.js +2 -0
- package/dist/input/types.js.map +1 -0
- package/dist/logger/elog4js.d.ts +65 -0
- package/dist/logger/elog4js.js +305 -0
- package/dist/logger/elog4js.js.map +1 -0
- package/dist/logger/logger.d.ts +37 -0
- package/dist/logger/logger.js +196 -0
- package/dist/logger/logger.js.map +1 -0
- package/dist/network/index.d.ts +2 -0
- package/dist/network/index.js +6 -0
- package/dist/network/index.js.map +1 -0
- package/dist/network/openai.d.ts +117 -0
- package/dist/network/openai.js +443 -0
- package/dist/network/openai.js.map +1 -0
- package/dist/network/proxy.d.ts +9 -0
- package/dist/network/proxy.js +72 -0
- package/dist/network/proxy.js.map +1 -0
- package/dist/network/web.d.ts +34 -0
- package/dist/network/web.js +152 -0
- package/dist/network/web.js.map +1 -0
- package/dist/platform/clipboard.d.ts +15 -0
- package/dist/platform/clipboard.js +82 -0
- package/dist/platform/clipboard.js.map +1 -0
- package/dist/platform/exec.d.ts +28 -0
- package/dist/platform/exec.js +95 -0
- package/dist/platform/exec.js.map +1 -0
- package/dist/platform/file.d.ts +38 -0
- package/dist/platform/file.js +197 -0
- package/dist/platform/file.js.map +1 -0
- package/dist/platform/index.d.ts +7 -0
- package/dist/platform/index.js +12 -0
- package/dist/platform/index.js.map +1 -0
- package/dist/platform/notification.d.ts +14 -0
- package/dist/platform/notification.js +82 -0
- package/dist/platform/notification.js.map +1 -0
- package/dist/platform/open-external.d.ts +10 -0
- package/dist/platform/open-external.js +105 -0
- package/dist/platform/open-external.js.map +1 -0
- package/dist/platform/os.d.ts +20 -0
- package/dist/platform/os.js +86 -0
- package/dist/platform/os.js.map +1 -0
- package/dist/platform/path.d.ts +32 -0
- package/dist/platform/path.js +154 -0
- package/dist/platform/path.js.map +1 -0
- package/dist/plugin/access-guard.d.ts +1 -0
- package/dist/plugin/access-guard.js +40 -0
- package/dist/plugin/access-guard.js.map +1 -0
- package/dist/plugin/agent-service.d.ts +25 -0
- package/dist/plugin/agent-service.js +809 -0
- package/dist/plugin/agent-service.js.map +1 -0
- package/dist/plugin/agent-tools.d.ts +12 -0
- package/dist/plugin/agent-tools.js +31 -0
- package/dist/plugin/agent-tools.js.map +1 -0
- package/dist/plugin/capabilities.d.ts +26 -0
- package/dist/plugin/capabilities.js +192 -0
- package/dist/plugin/capabilities.js.map +1 -0
- package/dist/plugin/console-guard.d.ts +2 -0
- package/dist/plugin/console-guard.js +52 -0
- package/dist/plugin/console-guard.js.map +1 -0
- package/dist/plugin/data-access.d.ts +15 -0
- package/dist/plugin/data-access.js +57 -0
- package/dist/plugin/data-access.js.map +1 -0
- package/dist/plugin/fs.d.ts +3 -0
- package/dist/plugin/fs.js +93 -0
- package/dist/plugin/fs.js.map +1 -0
- package/dist/plugin/host-alias.d.ts +2 -0
- package/dist/plugin/host-alias.js +57 -0
- package/dist/plugin/host-alias.js.map +1 -0
- package/dist/plugin/index.d.ts +7 -0
- package/dist/plugin/index.js +11 -0
- package/dist/plugin/index.js.map +1 -0
- package/dist/plugin/install.d.ts +17 -0
- package/dist/plugin/install.js +163 -0
- package/dist/plugin/install.js.map +1 -0
- package/dist/plugin/loader.d.ts +45 -0
- package/dist/plugin/loader.js +739 -0
- package/dist/plugin/loader.js.map +1 -0
- package/dist/plugin/manage.d.ts +31 -0
- package/dist/plugin/manage.js +271 -0
- package/dist/plugin/manage.js.map +1 -0
- package/dist/plugin/models-service.d.ts +15 -0
- package/dist/plugin/models-service.js +317 -0
- package/dist/plugin/models-service.js.map +1 -0
- package/dist/plugin/network-service.d.ts +10 -0
- package/dist/plugin/network-service.js +87 -0
- package/dist/plugin/network-service.js.map +1 -0
- package/dist/plugin/package-manager.d.ts +46 -0
- package/dist/plugin/package-manager.js +148 -0
- package/dist/plugin/package-manager.js.map +1 -0
- package/dist/plugin/paths.d.ts +6 -0
- package/dist/plugin/paths.js +15 -0
- package/dist/plugin/paths.js.map +1 -0
- package/dist/plugin/registry.d.ts +17 -0
- package/dist/plugin/registry.js +82 -0
- package/dist/plugin/registry.js.map +1 -0
- package/dist/plugin/runtime.d.ts +9 -0
- package/dist/plugin/runtime.js +43 -0
- package/dist/plugin/runtime.js.map +1 -0
- package/dist/plugin/scaffold.d.ts +13 -0
- package/dist/plugin/scaffold.js +132 -0
- package/dist/plugin/scaffold.js.map +1 -0
- package/dist/plugin/state.d.ts +6 -0
- package/dist/plugin/state.js +38 -0
- package/dist/plugin/state.js.map +1 -0
- package/dist/plugin/zip.d.ts +7 -0
- package/dist/plugin/zip.js +138 -0
- package/dist/plugin/zip.js.map +1 -0
- package/dist/sandbox/audit.d.ts +12 -0
- package/dist/sandbox/audit.js +20 -0
- package/dist/sandbox/audit.js.map +1 -0
- package/dist/sandbox/backends/bwrap.d.ts +28 -0
- package/dist/sandbox/backends/bwrap.js +197 -0
- package/dist/sandbox/backends/bwrap.js.map +1 -0
- package/dist/sandbox/backends/direct.d.ts +7 -0
- package/dist/sandbox/backends/direct.js +32 -0
- package/dist/sandbox/backends/direct.js.map +1 -0
- package/dist/sandbox/bridge-state.d.ts +13 -0
- package/dist/sandbox/bridge-state.js +12 -0
- package/dist/sandbox/bridge-state.js.map +1 -0
- package/dist/sandbox/index.d.ts +5 -0
- package/dist/sandbox/index.js +6 -0
- package/dist/sandbox/index.js.map +1 -0
- package/dist/sandbox/manager.d.ts +8 -0
- package/dist/sandbox/manager.js +325 -0
- package/dist/sandbox/manager.js.map +1 -0
- package/dist/sandbox/memory-limit.d.ts +44 -0
- package/dist/sandbox/memory-limit.js +193 -0
- package/dist/sandbox/memory-limit.js.map +1 -0
- package/dist/sandbox/plugin-client.d.ts +80 -0
- package/dist/sandbox/plugin-client.js +1601 -0
- package/dist/sandbox/plugin-client.js.map +1 -0
- package/dist/sandbox/plugin-host.d.ts +1 -0
- package/dist/sandbox/plugin-host.js +1001 -0
- package/dist/sandbox/plugin-host.js.map +1 -0
- package/dist/sandbox/protocol.d.ts +43 -0
- package/dist/sandbox/protocol.js +96 -0
- package/dist/sandbox/protocol.js.map +1 -0
- package/dist/sandbox/rpc-timeouts.d.ts +28 -0
- package/dist/sandbox/rpc-timeouts.js +15 -0
- package/dist/sandbox/rpc-timeouts.js.map +1 -0
- package/dist/sandbox/supervisor.d.ts +31 -0
- package/dist/sandbox/supervisor.js +114 -0
- package/dist/sandbox/supervisor.js.map +1 -0
- package/dist/sandbox/types.d.ts +124 -0
- package/dist/sandbox/types.js +17 -0
- package/dist/sandbox/types.js.map +1 -0
- package/dist/service/control.d.ts +32 -0
- package/dist/service/control.js +117 -0
- package/dist/service/control.js.map +1 -0
- package/dist/session/compress.d.ts +3 -0
- package/dist/session/compress.js +42 -0
- package/dist/session/compress.js.map +1 -0
- package/dist/session/index.d.ts +4 -0
- package/dist/session/index.js +8 -0
- package/dist/session/index.js.map +1 -0
- package/dist/session/registry.d.ts +4 -0
- package/dist/session/registry.js +12 -0
- package/dist/session/registry.js.map +1 -0
- package/dist/session/replay.d.ts +10 -0
- package/dist/session/replay.js +109 -0
- package/dist/session/replay.js.map +1 -0
- package/dist/session/session.d.ts +40 -0
- package/dist/session/session.js +108 -0
- package/dist/session/session.js.map +1 -0
- package/dist/session/store.d.ts +58 -0
- package/dist/session/store.js +382 -0
- package/dist/session/store.js.map +1 -0
- package/dist/session/types.d.ts +56 -0
- package/dist/session/types.js +3 -0
- package/dist/session/types.js.map +1 -0
- package/dist/workspace/index.d.ts +7 -0
- package/dist/workspace/index.js +17 -0
- package/dist/workspace/index.js.map +1 -0
- package/dist/workspace/registry.d.ts +25 -0
- package/dist/workspace/registry.js +293 -0
- package/dist/workspace/registry.js.map +1 -0
- package/package.json +73 -5
- package/scripts/build-node-pty.mjs +55 -0
- package/scripts/rewrite-dts.mjs +36 -0
- package/scripts/run-tests.mjs +41 -0
- package/scripts/verify-sandbox.ts +98 -0
- package/src/agent/pending-asks.ts +88 -0
- package/src/agent/task-registry.ts +108 -0
- package/src/agent/tool-context.ts +47 -0
- package/src/agent/usage.ts +170 -0
- package/src/api/host/agent.ts +162 -0
- package/src/api/host/events.ts +7 -0
- package/src/api/host/exec.ts +47 -0
- package/src/api/host/fs.ts +33 -0
- package/src/api/host/hooks.ts +635 -0
- package/src/api/host/host.ts +99 -0
- package/src/api/host/http.ts +79 -0
- package/src/api/host/index.ts +20 -0
- package/src/api/host/models.ts +81 -0
- package/src/api/host/network.ts +32 -0
- package/src/api/host/plugin.ts +158 -0
- package/src/api/host/plugins.ts +59 -0
- package/src/api/host/pty.ts +73 -0
- package/src/api/host/session.ts +61 -0
- package/src/api/host/version.ts +2 -0
- package/src/api/host/workspace.ts +51 -0
- package/src/api/plugin/http-client.ts +492 -0
- package/src/api/plugin/http-protocol.ts +231 -0
- package/src/api/result/result.ts +52 -0
- package/src/commands/agent-turn.ts +817 -0
- package/src/commands/ask-prompt.ts +52 -0
- package/src/commands/config.ts +306 -0
- package/src/commands/conversation.ts +777 -0
- package/src/commands/doctor.ts +92 -0
- package/src/commands/exec-approval.ts +91 -0
- package/src/commands/help.ts +41 -0
- package/src/commands/key.ts +281 -0
- package/src/commands/plugin.ts +222 -0
- package/src/commands/session.ts +179 -0
- package/src/commands/shared.ts +103 -0
- package/src/commands/start.ts +88 -0
- package/src/commands/stop.ts +24 -0
- package/src/commands/version.ts +40 -0
- package/src/config/codes.ts +17 -0
- package/src/config/credentials.ts +302 -0
- package/src/config/defaults.ts +32 -0
- package/src/config/index.ts +10 -0
- package/src/config/loader.ts +80 -0
- package/src/config/providers.ts +133 -0
- package/src/config/types.ts +88 -0
- package/src/config/validate.ts +458 -0
- package/src/display/markdown.ts +137 -0
- package/src/display/tool-label.ts +87 -0
- package/src/event/event.ts +171 -0
- package/src/index.ts +344 -0
- package/src/input/codes.ts +21 -0
- package/src/input/commands.ts +76 -0
- package/src/input/help.ts +60 -0
- package/src/input/index.ts +10 -0
- package/src/input/parse-options.ts +66 -0
- package/src/input/parse.ts +138 -0
- package/src/input/types.ts +19 -0
- package/src/logger/elog4js.ts +366 -0
- package/src/logger/logger.ts +251 -0
- package/src/network/index.ts +6 -0
- package/src/network/openai.ts +571 -0
- package/src/network/proxy.ts +80 -0
- package/src/network/web.ts +206 -0
- package/src/platform/clipboard.ts +107 -0
- package/src/platform/exec.ts +128 -0
- package/src/platform/file.ts +235 -0
- package/src/platform/index.ts +12 -0
- package/src/platform/notification.ts +108 -0
- package/src/platform/open-external.ts +132 -0
- package/src/platform/os.ts +101 -0
- package/src/platform/path.ts +214 -0
- package/src/plugin/access-guard.ts +46 -0
- package/src/plugin/agent-service.ts +969 -0
- package/src/plugin/agent-tools.ts +43 -0
- package/src/plugin/capabilities.ts +240 -0
- package/src/plugin/console-guard.ts +66 -0
- package/src/plugin/data-access.ts +70 -0
- package/src/plugin/fs.ts +109 -0
- package/src/plugin/host-alias.ts +61 -0
- package/src/plugin/index.ts +11 -0
- package/src/plugin/install.ts +213 -0
- package/src/plugin/loader.ts +952 -0
- package/src/plugin/manage.ts +338 -0
- package/src/plugin/models-service.ts +383 -0
- package/src/plugin/network-service.ts +102 -0
- package/src/plugin/package-manager.ts +214 -0
- package/src/plugin/paths.ts +19 -0
- package/src/plugin/registry.ts +103 -0
- package/src/plugin/runtime.ts +57 -0
- package/src/plugin/scaffold.ts +158 -0
- package/src/plugin/state.ts +52 -0
- package/src/plugin/zip.ts +156 -0
- package/src/sandbox/audit.ts +38 -0
- package/src/sandbox/backends/bwrap.ts +251 -0
- package/src/sandbox/backends/direct.ts +43 -0
- package/src/sandbox/bridge-state.ts +29 -0
- package/src/sandbox/index.ts +5 -0
- package/src/sandbox/manager.ts +406 -0
- package/src/sandbox/memory-limit.ts +245 -0
- package/src/sandbox/plugin-client.ts +1881 -0
- package/src/sandbox/plugin-host.ts +1282 -0
- package/src/sandbox/protocol.ts +142 -0
- package/src/sandbox/pty-bridge.py +154 -0
- package/src/sandbox/rpc-timeouts.ts +43 -0
- package/src/sandbox/supervisor.ts +172 -0
- package/src/sandbox/types.ts +149 -0
- package/src/service/control.ts +155 -0
- package/src/session/compress.ts +47 -0
- package/src/session/index.ts +8 -0
- package/src/session/registry.ts +17 -0
- package/src/session/replay.ts +132 -0
- package/src/session/session.ts +134 -0
- package/src/session/store.ts +468 -0
- package/src/session/types.ts +61 -0
- package/src/workspace/index.ts +21 -0
- package/src/workspace/registry.ts +356 -0
|
@@ -0,0 +1,635 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* 平台 → 插件的钩子。
|
|
3
|
+
* 插件通过 `wellspring/api/host/index.ts` 导入注册函数;平台在对话生命周期里触发。
|
|
4
|
+
* 注册发生在插件 import / setup / init 期间,用 AsyncLocalStorage 记录归属插件。
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { AsyncLocalStorage } from "node:async_hooks"
|
|
8
|
+
|
|
9
|
+
import { getHostBridge } from "../../sandbox/bridge-state.ts"
|
|
10
|
+
|
|
11
|
+
export type HookKind =
|
|
12
|
+
| "message"
|
|
13
|
+
| "sessionStart"
|
|
14
|
+
| "beforeTurn"
|
|
15
|
+
| "beforeStep"
|
|
16
|
+
| "afterStep"
|
|
17
|
+
| "afterTurn"
|
|
18
|
+
| "agentRunSync"
|
|
19
|
+
| "agentRunAsync"
|
|
20
|
+
| "agentAsyncTaskComplete"
|
|
21
|
+
| "agentCallTool"
|
|
22
|
+
| "agentAsk"
|
|
23
|
+
| "agentRead"
|
|
24
|
+
| "agentQueryTask"
|
|
25
|
+
| "agentSubmit"
|
|
26
|
+
| "agentSetTasks"
|
|
27
|
+
| "agentSetGoal"
|
|
28
|
+
| "agentRequestPermission"
|
|
29
|
+
| "userAnswer"
|
|
30
|
+
| "init"
|
|
31
|
+
| "shutdown"
|
|
32
|
+
|
|
33
|
+
export type HookHandler<Args extends unknown[]> = (...args: Args) => void | Promise<void>
|
|
34
|
+
|
|
35
|
+
export type Unregister = () => void
|
|
36
|
+
|
|
37
|
+
abstract class EventBase {
|
|
38
|
+
constructor(input: Record<string, unknown>) {
|
|
39
|
+
Object.assign(this, input)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export class SessionLifecycleEvent extends EventBase {
|
|
44
|
+
declare readonly sessionId: string
|
|
45
|
+
declare readonly mode: "chat" | "run"
|
|
46
|
+
declare readonly turn: number
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export class SessionMessageEvent extends SessionLifecycleEvent {
|
|
50
|
+
declare readonly role: "user" | "assistant"
|
|
51
|
+
declare readonly phase: "before" | "after"
|
|
52
|
+
declare readonly content: string
|
|
53
|
+
/** before 阶段可追加系统提示;平台会把数组拼进系统消息 */
|
|
54
|
+
declare readonly prompt: string[]
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 会话的第一轮开始前触发(新会话的第一次对话)。
|
|
59
|
+
*
|
|
60
|
+
* 与 `onMessage` 的区别:`onSessionStart` 每个会话只触发一次,适合注入一次性的
|
|
61
|
+
* system prompt(例如插件的角色设定、工作区约定);`onMessage` 的 before 阶段每轮都触发。
|
|
62
|
+
* 可以在 `prompt` 数组里追加字符串,平台会把它们拼进 system 消息。
|
|
63
|
+
*/
|
|
64
|
+
export interface SessionStartTool {
|
|
65
|
+
readonly name: string
|
|
66
|
+
readonly description: string
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export class SessionStartEvent extends SessionLifecycleEvent {
|
|
70
|
+
declare readonly input: string
|
|
71
|
+
declare readonly cwd: string
|
|
72
|
+
/** 第一轮开始的宿主时间戳 */
|
|
73
|
+
declare readonly time: number
|
|
74
|
+
/** 宿主时区(IANA 名称,例如 Asia/Shanghai) */
|
|
75
|
+
declare readonly timeZone: string
|
|
76
|
+
/** 本轮使用的模型 */
|
|
77
|
+
declare readonly model: string
|
|
78
|
+
/** 本轮生效的 sandbox 权限 */
|
|
79
|
+
declare readonly permission: string
|
|
80
|
+
/** 本轮可调用的工具(含平台内置 ask;纯对话模式为空) */
|
|
81
|
+
declare readonly tools: readonly SessionStartTool[]
|
|
82
|
+
/** 可追加系统提示;平台会拼进 system 消息 */
|
|
83
|
+
declare readonly prompt: string[]
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export class BeforeTurnEvent extends SessionLifecycleEvent {
|
|
87
|
+
declare readonly input: string
|
|
88
|
+
declare readonly cwd: string
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export class BeforeStepEvent extends SessionLifecycleEvent {
|
|
92
|
+
declare readonly step: number
|
|
93
|
+
declare readonly input: string
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export class AfterStepEvent extends SessionLifecycleEvent {
|
|
97
|
+
declare readonly step: number
|
|
98
|
+
declare readonly reply: string
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export class AfterTurnEvent extends SessionLifecycleEvent {
|
|
102
|
+
declare readonly reason: "completed" | "failed" | "cancelled"
|
|
103
|
+
declare readonly code: string | null
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export class AgentRunEvent extends EventBase {
|
|
107
|
+
declare readonly sessionId: string
|
|
108
|
+
declare readonly turn: number
|
|
109
|
+
declare readonly input: string
|
|
110
|
+
declare readonly cwd: string
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** onAgentRunAsync:Agent 创建异步任务之前 */
|
|
114
|
+
export class AgentAsyncTaskEvent extends EventBase {
|
|
115
|
+
declare readonly sessionId: string
|
|
116
|
+
declare readonly taskId: string
|
|
117
|
+
declare readonly task: string
|
|
118
|
+
declare readonly cwd: string
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** onAgentAsyncTaskComplete:异步任务结束 */
|
|
122
|
+
export class AgentAsyncTaskCompleteEvent extends EventBase {
|
|
123
|
+
declare readonly sessionId: string
|
|
124
|
+
declare readonly taskId: string
|
|
125
|
+
declare readonly status: "completed" | "failed" | "cancelled"
|
|
126
|
+
declare readonly result: unknown
|
|
127
|
+
declare readonly error: string | null
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export class AgentCallToolEvent extends EventBase {
|
|
131
|
+
declare readonly sessionId: string
|
|
132
|
+
declare readonly turn: number
|
|
133
|
+
declare readonly step: number
|
|
134
|
+
declare readonly callId: string
|
|
135
|
+
declare readonly name: string
|
|
136
|
+
declare readonly arguments: string
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export class AgentAskEvent extends EventBase {
|
|
140
|
+
declare readonly sessionId: string
|
|
141
|
+
/** 本次提问的 id;回答时对应用 UserAnswerEvent.askId */
|
|
142
|
+
declare readonly askId?: string
|
|
143
|
+
declare readonly turn?: number
|
|
144
|
+
declare readonly step?: number
|
|
145
|
+
declare readonly question: string
|
|
146
|
+
declare readonly options?: readonly string[]
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export class AgentReadEvent extends EventBase {
|
|
150
|
+
declare readonly sessionId: string
|
|
151
|
+
declare readonly turn: number
|
|
152
|
+
declare readonly step: number
|
|
153
|
+
declare readonly path: string
|
|
154
|
+
declare readonly content: string | null
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export class AgentQueryTaskEvent extends EventBase {
|
|
158
|
+
declare readonly sessionId: string
|
|
159
|
+
declare readonly query?: string
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export class AgentSubmitEvent extends EventBase {
|
|
163
|
+
declare readonly sessionId: string
|
|
164
|
+
declare readonly result: unknown
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export class AgentSetTasksEvent extends EventBase {
|
|
168
|
+
declare readonly sessionId: string
|
|
169
|
+
declare readonly tasks: readonly string[]
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export class AgentSetGoalEvent extends EventBase {
|
|
173
|
+
declare readonly sessionId: string
|
|
174
|
+
declare readonly goal: string
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export class AgentRequestPermissionEvent extends EventBase {
|
|
178
|
+
declare readonly sessionId: string
|
|
179
|
+
declare readonly action: string
|
|
180
|
+
declare readonly resource?: string
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export class UserAnswerEvent extends EventBase {
|
|
184
|
+
declare readonly sessionId: string
|
|
185
|
+
declare readonly askId?: string
|
|
186
|
+
declare readonly answer: string
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export interface HookError {
|
|
190
|
+
readonly hook: HookKind
|
|
191
|
+
readonly owner: string | null
|
|
192
|
+
readonly error: unknown
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const ownerStorage = new AsyncLocalStorage<{ readonly pluginId: string }>()
|
|
196
|
+
|
|
197
|
+
/** 在插件加载/执行期间设置钩子归属;平台内部使用 */
|
|
198
|
+
export async function withHookOwner<T>(pluginId: string, task: () => T | Promise<T>): Promise<T> {
|
|
199
|
+
return await ownerStorage.run({ pluginId }, async () => await task())
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** 平台内部:当前正在执行的插件(用于访问守卫) */
|
|
203
|
+
export function getHookOwner(): string | null {
|
|
204
|
+
return ownerStorage.getStore()?.pluginId ?? null
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
interface Entry<Args extends unknown[]> {
|
|
208
|
+
readonly owner: string | null
|
|
209
|
+
readonly handler: HookHandler<Args>
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const messageHooks: Entry<[SessionMessageEvent]>[] = []
|
|
213
|
+
const sessionStartHooks: Entry<[SessionStartEvent]>[] = []
|
|
214
|
+
const beforeTurnHooks: Entry<[BeforeTurnEvent]>[] = []
|
|
215
|
+
const beforeStepHooks: Entry<[BeforeStepEvent]>[] = []
|
|
216
|
+
const afterStepHooks: Entry<[AfterStepEvent]>[] = []
|
|
217
|
+
const afterTurnHooks: Entry<[AfterTurnEvent]>[] = []
|
|
218
|
+
const agentRunSyncHooks: Entry<[AgentRunEvent]>[] = []
|
|
219
|
+
const agentRunAsyncHooks: Entry<[AgentAsyncTaskEvent]>[] = []
|
|
220
|
+
const agentAsyncTaskCompleteHooks: Entry<[AgentAsyncTaskCompleteEvent]>[] = []
|
|
221
|
+
const agentCallToolHooks: Entry<[AgentCallToolEvent]>[] = []
|
|
222
|
+
const agentAskHooks: Entry<[AgentAskEvent]>[] = []
|
|
223
|
+
const agentReadHooks: Entry<[AgentReadEvent]>[] = []
|
|
224
|
+
const agentQueryTaskHooks: Entry<[AgentQueryTaskEvent]>[] = []
|
|
225
|
+
const agentSubmitHooks: Entry<[AgentSubmitEvent]>[] = []
|
|
226
|
+
const agentSetTasksHooks: Entry<[AgentSetTasksEvent]>[] = []
|
|
227
|
+
const agentSetGoalHooks: Entry<[AgentSetGoalEvent]>[] = []
|
|
228
|
+
const agentRequestPermissionHooks: Entry<[AgentRequestPermissionEvent]>[] = []
|
|
229
|
+
const userAnswerHooks: Entry<[UserAnswerEvent]>[] = []
|
|
230
|
+
const initHooks: Entry<[]>[] = []
|
|
231
|
+
const shutdownHooks: Entry<[]>[] = []
|
|
232
|
+
|
|
233
|
+
let errorReporter: ((error: HookError) => void) | null = null
|
|
234
|
+
|
|
235
|
+
/** 平台注入错误处理,钩子抛错时不打断主流程 */
|
|
236
|
+
export function setHookErrorHandler(handler: (error: HookError) => void): void {
|
|
237
|
+
errorReporter = handler
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
type HookGuard = (owner: string, task: () => Promise<void>) => Promise<void>
|
|
241
|
+
|
|
242
|
+
let hookGuard: HookGuard | null = null
|
|
243
|
+
|
|
244
|
+
/** 平台内部:在钩子执行期间应用插件级 console 拦截等 */
|
|
245
|
+
export function setHookGuard(guard: HookGuard | null): void {
|
|
246
|
+
hookGuard = guard
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function add<Args extends unknown[]>(
|
|
250
|
+
registry: Entry<Args>[],
|
|
251
|
+
handler: HookHandler<Args>,
|
|
252
|
+
): Unregister {
|
|
253
|
+
const entry: Entry<Args> = { owner: getHookOwner(), handler }
|
|
254
|
+
registry.push(entry)
|
|
255
|
+
return () => {
|
|
256
|
+
const index = registry.indexOf(entry)
|
|
257
|
+
if (index !== -1) {
|
|
258
|
+
registry.splice(index, 1)
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function removeByOwner(registry: Entry<unknown[]>[], owner: string): void {
|
|
264
|
+
for (let index = registry.length - 1; index >= 0; index -= 1) {
|
|
265
|
+
if (registry[index]?.owner === owner) {
|
|
266
|
+
registry.splice(index, 1)
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** 插件加载失败时清掉它已注册的钩子 */
|
|
272
|
+
export function removeHooksByOwner(owner: string): void {
|
|
273
|
+
const registries: Entry<never>[][] = [
|
|
274
|
+
messageHooks,
|
|
275
|
+
sessionStartHooks,
|
|
276
|
+
beforeTurnHooks,
|
|
277
|
+
beforeStepHooks,
|
|
278
|
+
afterStepHooks,
|
|
279
|
+
afterTurnHooks,
|
|
280
|
+
agentRunSyncHooks,
|
|
281
|
+
agentRunAsyncHooks,
|
|
282
|
+
agentAsyncTaskCompleteHooks,
|
|
283
|
+
agentCallToolHooks,
|
|
284
|
+
agentAskHooks,
|
|
285
|
+
agentReadHooks,
|
|
286
|
+
agentQueryTaskHooks,
|
|
287
|
+
agentSubmitHooks,
|
|
288
|
+
agentSetTasksHooks,
|
|
289
|
+
agentSetGoalHooks,
|
|
290
|
+
agentRequestPermissionHooks,
|
|
291
|
+
userAnswerHooks,
|
|
292
|
+
initHooks,
|
|
293
|
+
shutdownHooks,
|
|
294
|
+
] as unknown as Entry<never>[][]
|
|
295
|
+
for (const registry of registries) {
|
|
296
|
+
removeByOwner(registry as Entry<unknown[]>[], owner)
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const ALL_HOOK_KINDS: readonly HookKind[] = [
|
|
301
|
+
"message",
|
|
302
|
+
"sessionStart",
|
|
303
|
+
"beforeTurn",
|
|
304
|
+
"beforeStep",
|
|
305
|
+
"afterStep",
|
|
306
|
+
"afterTurn",
|
|
307
|
+
"agentRunSync",
|
|
308
|
+
"agentRunAsync",
|
|
309
|
+
"agentAsyncTaskComplete",
|
|
310
|
+
"agentCallTool",
|
|
311
|
+
"agentAsk",
|
|
312
|
+
"agentRead",
|
|
313
|
+
"agentQueryTask",
|
|
314
|
+
"agentSubmit",
|
|
315
|
+
"agentSetTasks",
|
|
316
|
+
"agentSetGoal",
|
|
317
|
+
"agentRequestPermission",
|
|
318
|
+
"userAnswer",
|
|
319
|
+
"init",
|
|
320
|
+
"shutdown",
|
|
321
|
+
]
|
|
322
|
+
|
|
323
|
+
function registryFor(kind: HookKind): Entry<unknown[]>[] {
|
|
324
|
+
switch (kind) {
|
|
325
|
+
case "message":
|
|
326
|
+
return messageHooks as Entry<unknown[]>[]
|
|
327
|
+
case "sessionStart":
|
|
328
|
+
return sessionStartHooks as Entry<unknown[]>[]
|
|
329
|
+
case "beforeTurn":
|
|
330
|
+
return beforeTurnHooks as Entry<unknown[]>[]
|
|
331
|
+
case "beforeStep":
|
|
332
|
+
return beforeStepHooks as Entry<unknown[]>[]
|
|
333
|
+
case "afterStep":
|
|
334
|
+
return afterStepHooks as Entry<unknown[]>[]
|
|
335
|
+
case "afterTurn":
|
|
336
|
+
return afterTurnHooks as Entry<unknown[]>[]
|
|
337
|
+
case "agentRunSync":
|
|
338
|
+
return agentRunSyncHooks as Entry<unknown[]>[]
|
|
339
|
+
case "agentRunAsync":
|
|
340
|
+
return agentRunAsyncHooks as Entry<unknown[]>[]
|
|
341
|
+
case "agentAsyncTaskComplete":
|
|
342
|
+
return agentAsyncTaskCompleteHooks as Entry<unknown[]>[]
|
|
343
|
+
case "agentCallTool":
|
|
344
|
+
return agentCallToolHooks as Entry<unknown[]>[]
|
|
345
|
+
case "agentAsk":
|
|
346
|
+
return agentAskHooks as Entry<unknown[]>[]
|
|
347
|
+
case "agentRead":
|
|
348
|
+
return agentReadHooks as Entry<unknown[]>[]
|
|
349
|
+
case "agentQueryTask":
|
|
350
|
+
return agentQueryTaskHooks as Entry<unknown[]>[]
|
|
351
|
+
case "agentSubmit":
|
|
352
|
+
return agentSubmitHooks as Entry<unknown[]>[]
|
|
353
|
+
case "agentSetTasks":
|
|
354
|
+
return agentSetTasksHooks as Entry<unknown[]>[]
|
|
355
|
+
case "agentSetGoal":
|
|
356
|
+
return agentSetGoalHooks as Entry<unknown[]>[]
|
|
357
|
+
case "agentRequestPermission":
|
|
358
|
+
return agentRequestPermissionHooks as Entry<unknown[]>[]
|
|
359
|
+
case "userAnswer":
|
|
360
|
+
return userAnswerHooks as Entry<unknown[]>[]
|
|
361
|
+
case "init":
|
|
362
|
+
return initHooks as Entry<unknown[]>[]
|
|
363
|
+
case "shutdown":
|
|
364
|
+
return shutdownHooks as Entry<unknown[]>[]
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** 宿主内部:为 sandbox 插件注册远程钩子 */
|
|
369
|
+
export function registerExternalHook(
|
|
370
|
+
kind: HookKind,
|
|
371
|
+
owner: string,
|
|
372
|
+
handler: HookHandler<[unknown]>,
|
|
373
|
+
): Unregister {
|
|
374
|
+
const registry = registryFor(kind)
|
|
375
|
+
const entry: Entry<unknown[]> = {
|
|
376
|
+
owner,
|
|
377
|
+
handler: handler as HookHandler<unknown[]>,
|
|
378
|
+
}
|
|
379
|
+
registry.push(entry)
|
|
380
|
+
return () => {
|
|
381
|
+
const index = registry.indexOf(entry)
|
|
382
|
+
if (index !== -1) {
|
|
383
|
+
registry.splice(index, 1)
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** 当前 owner 注册了哪些钩子(sandbox runner 上报用) */
|
|
389
|
+
export function listHookKinds(owner: string): HookKind[] {
|
|
390
|
+
return ALL_HOOK_KINDS.filter((kind) => registryFor(kind).some((entry) => entry.owner === owner))
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
export function onMessage(handler: HookHandler<[SessionMessageEvent]>): Unregister {
|
|
394
|
+
return add(messageHooks, handler)
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** 会话第一轮开始前触发一次;可往 prompt 里追加 system 提示 */
|
|
398
|
+
export function onSessionStart(handler: HookHandler<[SessionStartEvent]>): Unregister {
|
|
399
|
+
return add(sessionStartHooks, handler)
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
export function beforeTurn(handler: HookHandler<[BeforeTurnEvent]>): Unregister {
|
|
403
|
+
return add(beforeTurnHooks, handler)
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
export function beforeStep(handler: HookHandler<[BeforeStepEvent]>): Unregister {
|
|
407
|
+
return add(beforeStepHooks, handler)
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
export function afterStep(handler: HookHandler<[AfterStepEvent]>): Unregister {
|
|
411
|
+
return add(afterStepHooks, handler)
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
export function afterTurn(handler: HookHandler<[AfterTurnEvent]>): Unregister {
|
|
415
|
+
return add(afterTurnHooks, handler)
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/** run 模式开始同步执行前触发 */
|
|
419
|
+
export function onAgentRunSync(handler: HookHandler<[AgentRunEvent]>): Unregister {
|
|
420
|
+
return add(agentRunSyncHooks, handler)
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/** Agent 即将创建异步任务时触发 */
|
|
424
|
+
export function onAgentRunAsync(handler: HookHandler<[AgentAsyncTaskEvent]>): Unregister {
|
|
425
|
+
return add(agentRunAsyncHooks, handler)
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/** Agent 异步任务结束后触发 */
|
|
429
|
+
export function onAgentAsyncTaskComplete(
|
|
430
|
+
handler: HookHandler<[AgentAsyncTaskCompleteEvent]>,
|
|
431
|
+
): Unregister {
|
|
432
|
+
return add(agentAsyncTaskCompleteHooks, handler)
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
export function onAgentCallTool(handler: HookHandler<[AgentCallToolEvent]>): Unregister {
|
|
436
|
+
return add(agentCallToolHooks, handler)
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
export function onAgentAsk(handler: HookHandler<[AgentAskEvent]>): Unregister {
|
|
440
|
+
return add(agentAskHooks, handler)
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
export function onAgentRead(handler: HookHandler<[AgentReadEvent]>): Unregister {
|
|
444
|
+
return add(agentReadHooks, handler)
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
export function onAgentQueryTask(handler: HookHandler<[AgentQueryTaskEvent]>): Unregister {
|
|
448
|
+
return add(agentQueryTaskHooks, handler)
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
export function onAgentSubmit(handler: HookHandler<[AgentSubmitEvent]>): Unregister {
|
|
452
|
+
return add(agentSubmitHooks, handler)
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
export function onAgentSetTasks(handler: HookHandler<[AgentSetTasksEvent]>): Unregister {
|
|
456
|
+
return add(agentSetTasksHooks, handler)
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
export function onAgentSetGoal(handler: HookHandler<[AgentSetGoalEvent]>): Unregister {
|
|
460
|
+
return add(agentSetGoalHooks, handler)
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
export function onAgentRequestPermission(
|
|
464
|
+
handler: HookHandler<[AgentRequestPermissionEvent]>,
|
|
465
|
+
): Unregister {
|
|
466
|
+
return add(agentRequestPermissionHooks, handler)
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
export function onUserAnswer(handler: HookHandler<[UserAnswerEvent]>): Unregister {
|
|
470
|
+
return add(userAnswerHooks, handler)
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/** 服务进程加载完插件后触发 */
|
|
474
|
+
export function onInit(handler: HookHandler<[]>): Unregister {
|
|
475
|
+
return add(initHooks, handler)
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/** 服务进程关闭前触发;Ctrl+C 或 shutdown() 后执行 */
|
|
479
|
+
export function onShutdown(handler: HookHandler<[]>): Unregister {
|
|
480
|
+
return add(shutdownHooks, handler)
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
async function emit<Args extends unknown[]>(
|
|
484
|
+
kind: HookKind,
|
|
485
|
+
registry: Entry<Args>[],
|
|
486
|
+
args: Args,
|
|
487
|
+
): Promise<void> {
|
|
488
|
+
for (const entry of [...registry]) {
|
|
489
|
+
const task = async (): Promise<void> => {
|
|
490
|
+
await entry.handler(...args)
|
|
491
|
+
}
|
|
492
|
+
try {
|
|
493
|
+
if (entry.owner !== null && hookGuard !== null) {
|
|
494
|
+
await hookGuard(entry.owner, task)
|
|
495
|
+
} else {
|
|
496
|
+
await task()
|
|
497
|
+
}
|
|
498
|
+
} catch (error) {
|
|
499
|
+
errorReporter?.({ hook: kind, owner: entry.owner, error })
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function eventOf<T extends object>(input: T, ctor: new (input: Record<string, unknown>) => T): T {
|
|
505
|
+
return input instanceof ctor ? input : new ctor({ ...(input as Record<string, unknown>) })
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
export async function emitOnMessage(payload: SessionMessageEvent): Promise<void> {
|
|
509
|
+
await emit("message", messageHooks, [eventOf(payload, SessionMessageEvent)])
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
export async function emitSessionStart(payload: SessionStartEvent): Promise<void> {
|
|
513
|
+
await emit("sessionStart", sessionStartHooks, [eventOf(payload, SessionStartEvent)])
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
export async function emitBeforeTurn(payload: BeforeTurnEvent): Promise<void> {
|
|
517
|
+
await emit("beforeTurn", beforeTurnHooks, [eventOf(payload, BeforeTurnEvent)])
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
export async function emitBeforeStep(payload: BeforeStepEvent): Promise<void> {
|
|
521
|
+
await emit("beforeStep", beforeStepHooks, [eventOf(payload, BeforeStepEvent)])
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
export async function emitAfterStep(payload: AfterStepEvent): Promise<void> {
|
|
525
|
+
await emit("afterStep", afterStepHooks, [eventOf(payload, AfterStepEvent)])
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
export async function emitAfterTurn(payload: AfterTurnEvent): Promise<void> {
|
|
529
|
+
await emit("afterTurn", afterTurnHooks, [eventOf(payload, AfterTurnEvent)])
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
export async function emitAgentRunSync(payload: AgentRunEvent): Promise<void> {
|
|
533
|
+
await emit("agentRunSync", agentRunSyncHooks, [eventOf(payload, AgentRunEvent)])
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
export async function emitAgentRunAsync(payload: AgentAsyncTaskEvent): Promise<void> {
|
|
537
|
+
await emit("agentRunAsync", agentRunAsyncHooks, [eventOf(payload, AgentAsyncTaskEvent)])
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
export async function emitAgentAsyncTaskComplete(
|
|
541
|
+
payload: AgentAsyncTaskCompleteEvent,
|
|
542
|
+
): Promise<void> {
|
|
543
|
+
await emit("agentAsyncTaskComplete", agentAsyncTaskCompleteHooks, [
|
|
544
|
+
eventOf(payload, AgentAsyncTaskCompleteEvent),
|
|
545
|
+
])
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
export async function emitAgentCallTool(payload: AgentCallToolEvent): Promise<void> {
|
|
549
|
+
await emit("agentCallTool", agentCallToolHooks, [eventOf(payload, AgentCallToolEvent)])
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
export async function emitAgentAsk(payload: AgentAskEvent): Promise<void> {
|
|
553
|
+
await emit("agentAsk", agentAskHooks, [eventOf(payload, AgentAskEvent)])
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
export async function emitAgentRead(payload: AgentReadEvent): Promise<void> {
|
|
557
|
+
await emit("agentRead", agentReadHooks, [eventOf(payload, AgentReadEvent)])
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
export async function emitAgentQueryTask(payload: AgentQueryTaskEvent): Promise<void> {
|
|
561
|
+
await emit("agentQueryTask", agentQueryTaskHooks, [eventOf(payload, AgentQueryTaskEvent)])
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
export async function emitAgentSubmit(payload: AgentSubmitEvent): Promise<void> {
|
|
565
|
+
await emit("agentSubmit", agentSubmitHooks, [eventOf(payload, AgentSubmitEvent)])
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
export async function emitAgentSetTasks(payload: AgentSetTasksEvent): Promise<void> {
|
|
569
|
+
await emit("agentSetTasks", agentSetTasksHooks, [eventOf(payload, AgentSetTasksEvent)])
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
export async function emitAgentSetGoal(payload: AgentSetGoalEvent): Promise<void> {
|
|
573
|
+
await emit("agentSetGoal", agentSetGoalHooks, [eventOf(payload, AgentSetGoalEvent)])
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
export async function emitAgentRequestPermission(
|
|
577
|
+
payload: AgentRequestPermissionEvent,
|
|
578
|
+
): Promise<void> {
|
|
579
|
+
await emit("agentRequestPermission", agentRequestPermissionHooks, [
|
|
580
|
+
eventOf(payload, AgentRequestPermissionEvent),
|
|
581
|
+
])
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
export async function emitUserAnswer(payload: UserAnswerEvent): Promise<void> {
|
|
585
|
+
await emit("userAnswer", userAnswerHooks, [eventOf(payload, UserAnswerEvent)])
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
export async function emitInitHooks(): Promise<void> {
|
|
589
|
+
await emit("init", initHooks, [])
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
export async function emitShutdownHooks(): Promise<void> {
|
|
593
|
+
await emit("shutdown", shutdownHooks, [])
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
/** 只触发某个插件的 init hooks;热启用时使用 */
|
|
597
|
+
export async function emitInitHooksForOwner(owner: string): Promise<void> {
|
|
598
|
+
await emit(
|
|
599
|
+
"init",
|
|
600
|
+
initHooks.filter((entry) => entry.owner === owner),
|
|
601
|
+
[],
|
|
602
|
+
)
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/** 只触发某个插件的 shutdown hooks;热禁用时使用 */
|
|
606
|
+
export async function emitShutdownHooksForOwner(owner: string): Promise<void> {
|
|
607
|
+
await emit(
|
|
608
|
+
"shutdown",
|
|
609
|
+
shutdownHooks.filter((entry) => entry.owner === owner),
|
|
610
|
+
[],
|
|
611
|
+
)
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
let shutdownHandler: (() => void) | null = null
|
|
615
|
+
|
|
616
|
+
/** 平台内部:start 命令注册关闭回调 */
|
|
617
|
+
export function setShutdownHandler(handler: (() => void) | null): void {
|
|
618
|
+
shutdownHandler = handler
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* 请求服务进程优雅关闭。
|
|
623
|
+
* 只能在 `wellspring start` 进程内调用;关闭流程会先执行 onShutdown 钩子。
|
|
624
|
+
*/
|
|
625
|
+
export function shutdown(): void {
|
|
626
|
+
const bridge = getHostBridge()
|
|
627
|
+
if (bridge !== null) {
|
|
628
|
+
bridge.shutdown()
|
|
629
|
+
return
|
|
630
|
+
}
|
|
631
|
+
if (shutdownHandler === null) {
|
|
632
|
+
throw new Error("shutdown() 只能在 wellspring start 进程内调用")
|
|
633
|
+
}
|
|
634
|
+
shutdownHandler()
|
|
635
|
+
}
|