@oyasmi/pipiclaw 0.9.4 → 0.9.5-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +28 -22
- package/README.md +27 -21
- package/dist/agent/builtin-commands.d.ts +7 -2
- package/dist/agent/builtin-commands.js +19 -23
- package/dist/agent/channel-runner.d.ts +13 -13
- package/dist/agent/channel-runner.js +93 -76
- package/dist/agent/maintenance-context.d.ts +6 -5
- package/dist/agent/maintenance-context.js +15 -15
- package/dist/agent/prompt/manifest.js +1 -1
- package/dist/agent/prompt/resources.d.ts +1 -1
- package/dist/agent/prompt/resources.js +1 -1
- package/dist/agent/prompt/sections.js +4 -4
- package/dist/agent/runner-factory.d.ts +6 -6
- package/dist/agent/runner-factory.js +6 -7
- package/dist/agent/session/compaction-input.d.ts +6 -0
- package/dist/agent/session/compaction-input.js +75 -0
- package/dist/agent/session/compaction-snapshot.d.ts +30 -0
- package/dist/agent/session/compaction-snapshot.js +154 -0
- package/dist/agent/session/compaction-summary.d.ts +39 -0
- package/dist/agent/session/compaction-summary.js +158 -0
- package/dist/agent/session/compaction.d.ts +35 -0
- package/dist/agent/session/compaction.js +294 -0
- package/dist/agent/session/context-input.d.ts +15 -0
- package/dist/agent/session/context-input.js +82 -0
- package/dist/agent/session/context-snapshot.d.ts +11 -0
- package/dist/agent/session/context-snapshot.js +72 -0
- package/dist/agent/session/event-guards.d.ts +2 -0
- package/dist/agent/session/event-guards.js +4 -0
- package/dist/agent/session/request-tools.d.ts +3 -0
- package/dist/agent/session/request-tools.js +20 -0
- package/dist/agent/session/session-adapter.d.ts +2 -1
- package/dist/agent/session/session-adapter.js +6 -3
- package/dist/agent/session/session-events.d.ts +5 -4
- package/dist/agent/session/session-events.js +46 -59
- package/dist/agent/session/session-host.d.ts +29 -41
- package/dist/agent/session/session-host.js +86 -69
- package/dist/agent/session/settings-diagnostics.d.ts +5 -0
- package/dist/agent/session/settings-diagnostics.js +21 -0
- package/dist/agent/status-render.js +5 -2
- package/dist/{runtime → agent}/stop-watchdog.d.ts +3 -1
- package/dist/{runtime → agent}/stop-watchdog.js +7 -3
- package/dist/agent/turn/progress-formatter.d.ts +1 -3
- package/dist/agent/turn/progress-formatter.js +3 -22
- package/dist/agent/turn/task-digest.d.ts +4 -2
- package/dist/agent/turn/task-digest.js +7 -4
- package/dist/agent/turn/turn-context.d.ts +11 -7
- package/dist/agent/turn/turn-context.js +26 -18
- package/dist/agent/turn/turn-finish.d.ts +17 -5
- package/dist/agent/turn/turn-finish.js +51 -77
- package/dist/agent/turn/turn-input.d.ts +2 -2
- package/dist/agent/turn/turn-input.js +4 -3
- package/dist/agent/turn/turn-output.d.ts +19 -0
- package/dist/agent/turn/turn-output.js +41 -0
- package/dist/agent/turn/turn-pipeline.d.ts +11 -8
- package/dist/agent/turn/turn-pipeline.js +54 -55
- package/dist/agent/turn/turn-prompt.d.ts +1 -1
- package/dist/agent/turn/turn-recovery.d.ts +11 -10
- package/dist/agent/turn/turn-recovery.js +14 -29
- package/dist/agent/types.d.ts +31 -12
- package/dist/agent/types.js +2 -1
- package/dist/app/auth-command.js +1 -1
- package/dist/app/home.js +31 -15
- package/dist/channel/active-session-store.d.ts +14 -12
- package/dist/channel/active-session-store.js +29 -25
- package/dist/channel/attachments.d.ts +25 -0
- package/dist/channel/attachments.js +56 -0
- package/dist/channel/channel-context.d.ts +87 -96
- package/dist/channel/channel-context.js +1 -66
- package/dist/channel/channel-event.d.ts +51 -41
- package/dist/channel/channel-event.js +6 -11
- package/dist/channel/channel-id.d.ts +22 -0
- package/dist/channel/channel-id.js +13 -0
- package/dist/channel/channel-index.d.ts +17 -34
- package/dist/channel/channel-index.js +41 -80
- package/dist/channel/config.d.ts +10 -0
- package/dist/channel/config.js +55 -0
- package/dist/channel/ingress.d.ts +101 -0
- package/dist/channel/ingress.js +5 -0
- package/dist/channel/store.d.ts +13 -15
- package/dist/channel/store.js +21 -25
- package/dist/channel/transport.d.ts +99 -0
- package/dist/channel/turn-context.d.ts +23 -0
- package/dist/channel/turn-context.js +1 -0
- package/dist/commands/catalog.d.ts +4 -7
- package/dist/commands/catalog.js +24 -31
- package/dist/commands/router.d.ts +23 -12
- package/dist/commands/router.js +22 -24
- package/dist/config/settings.d.ts +5 -4
- package/dist/desk/bindings.d.ts +6 -0
- package/dist/desk/bindings.js +105 -0
- package/dist/desk/commands.d.ts +81 -0
- package/dist/desk/commands.js +145 -0
- package/dist/desk/default.d.ts +4 -0
- package/dist/desk/default.js +11 -0
- package/dist/desk/paths.d.ts +41 -0
- package/dist/desk/paths.js +81 -0
- package/dist/desk/registry.d.ts +17 -0
- package/dist/desk/registry.js +191 -0
- package/dist/desk/startup-error.d.ts +4 -0
- package/dist/desk/startup-error.js +7 -0
- package/dist/desk/types.d.ts +72 -0
- package/dist/desk/types.js +7 -0
- package/dist/dingtalk/bot.d.ts +18 -35
- package/dist/dingtalk/bot.js +21 -204
- package/dist/dingtalk/channel-ids.d.ts +15 -0
- package/dist/dingtalk/channel-ids.js +39 -0
- package/dist/dingtalk/config.d.ts +6 -3
- package/dist/dingtalk/config.js +13 -25
- package/dist/dingtalk/connection.d.ts +1 -0
- package/dist/dingtalk/connection.js +5 -4
- package/dist/dingtalk/conversation-meta.d.ts +10 -5
- package/dist/dingtalk/conversation-meta.js +72 -50
- package/dist/dingtalk/delivery.d.ts +30 -14
- package/dist/dingtalk/delivery.js +189 -62
- package/dist/dingtalk/inbound-media.d.ts +2 -18
- package/dist/dingtalk/inbound-media.js +0 -53
- package/dist/dingtalk/inbound.d.ts +10 -11
- package/dist/dingtalk/inbound.js +12 -19
- package/dist/dingtalk/media.d.ts +9 -11
- package/dist/dingtalk/media.js +18 -31
- package/dist/dingtalk/progress.d.ts +8 -0
- package/dist/dingtalk/progress.js +20 -0
- package/dist/dingtalk/response-mode.d.ts +13 -0
- package/dist/dingtalk/response-mode.js +31 -0
- package/dist/dingtalk/transport.d.ts +47 -0
- package/dist/dingtalk/transport.js +71 -0
- package/dist/events/commands.d.ts +7 -2
- package/dist/events/commands.js +40 -22
- package/dist/events/pre-action-error.d.ts +10 -0
- package/dist/events/pre-action-error.js +10 -0
- package/dist/events/types.d.ts +20 -2
- package/dist/events/watcher.d.ts +14 -7
- package/dist/events/watcher.js +49 -26
- package/dist/index.d.ts +4 -2
- package/dist/jobs/completion-wake.d.ts +14 -0
- package/dist/jobs/completion-wake.js +36 -0
- package/dist/jobs/manager.d.ts +27 -22
- package/dist/jobs/manager.js +58 -91
- package/dist/jobs/record.d.ts +2 -0
- package/dist/jobs/record.js +2 -0
- package/dist/log.d.ts +0 -1
- package/dist/log.js +1 -1
- package/dist/main.js +10 -3
- package/dist/memory/boundary-work.d.ts +17 -0
- package/dist/memory/boundary-work.js +162 -0
- package/dist/memory/commands.d.ts +4 -3
- package/dist/memory/commands.js +16 -16
- package/dist/memory/desk-memory-queue.d.ts +18 -0
- package/dist/memory/desk-memory-queue.js +33 -0
- package/dist/memory/index-budget.d.ts +3 -3
- package/dist/memory/index-budget.js +3 -3
- package/dist/memory/journal.d.ts +6 -6
- package/dist/memory/journal.js +14 -14
- package/dist/memory/lifecycle.d.ts +14 -21
- package/dist/memory/lifecycle.js +73 -103
- package/dist/memory/maintenance-state.d.ts +14 -15
- package/dist/memory/maintenance-state.js +43 -42
- package/dist/memory/reflect-job.d.ts +5 -4
- package/dist/memory/reflect-job.js +21 -15
- package/dist/memory/reflect.d.ts +2 -1
- package/dist/memory/reflect.js +13 -12
- package/dist/memory/render.d.ts +1 -1
- package/dist/memory/render.js +2 -2
- package/dist/memory/review-log.d.ts +4 -4
- package/dist/memory/review-log.js +5 -5
- package/dist/memory/scheduler.d.ts +12 -19
- package/dist/memory/scheduler.js +28 -64
- package/dist/memory/session-corpus.d.ts +4 -1
- package/dist/memory/session-corpus.js +70 -29
- package/dist/memory/session-search.d.ts +11 -3
- package/dist/memory/session-search.js +14 -11
- package/dist/memory/sidecar-worker.d.ts +1 -0
- package/dist/memory/sidecar-worker.js +57 -76
- package/dist/memory/source-window.d.ts +0 -6
- package/dist/memory/source-window.js +0 -20
- package/dist/memory/store.d.ts +12 -12
- package/dist/memory/store.js +44 -44
- package/dist/paths.d.ts +2 -0
- package/dist/paths.js +2 -0
- package/dist/playbooks/agent-delegation.md +16 -16
- package/dist/playbooks/background-jobs.md +5 -3
- package/dist/playbooks/event-scheduling.md +7 -5
- package/dist/playbooks/memory-and-learning.md +16 -12
- package/dist/playbooks/outbound-media.md +1 -1
- package/dist/playbooks/runtime-orientation.md +35 -23
- package/dist/playbooks/task-lead.md +13 -11
- package/dist/runtime/bootstrap.d.ts +36 -13
- package/dist/runtime/bootstrap.js +310 -138
- package/dist/runtime/channel-queue.d.ts +23 -0
- package/dist/{dingtalk → runtime}/channel-queue.js +20 -9
- package/dist/runtime/daemon-lock.d.ts +12 -0
- package/dist/runtime/daemon-lock.js +28 -0
- package/dist/runtime/desk-runtime.d.ts +131 -0
- package/dist/runtime/desk-runtime.js +402 -0
- package/dist/runtime/durable-dispatch.d.ts +11 -5
- package/dist/runtime/durable-dispatch.js +20 -8
- package/dist/runtime/inbound-images.d.ts +32 -10
- package/dist/runtime/inbound-images.js +95 -21
- package/dist/runtime/ingress-busy.d.ts +26 -0
- package/dist/runtime/ingress-busy.js +211 -0
- package/dist/runtime/ingress-commands.d.ts +7 -0
- package/dist/runtime/ingress-commands.js +162 -0
- package/dist/runtime/ingress-deps.d.ts +62 -0
- package/dist/runtime/ingress-deps.js +1 -0
- package/dist/runtime/ingress.d.ts +22 -0
- package/dist/runtime/ingress.js +376 -0
- package/dist/runtime/request-execution.d.ts +19 -0
- package/dist/runtime/request-execution.js +88 -0
- package/dist/runtime/shutdown.d.ts +13 -5
- package/dist/runtime/shutdown.js +10 -4
- package/dist/runtime/task-driver.d.ts +38 -22
- package/dist/runtime/task-driver.js +142 -116
- package/dist/runtime/task-spawn.d.ts +4 -4
- package/dist/runtime/task-spawn.js +6 -5
- package/dist/runtime/task-wake.d.ts +6 -4
- package/dist/runtime/task-wake.js +7 -7
- package/dist/runtime/transports.d.ts +32 -0
- package/dist/runtime/transports.js +101 -0
- package/dist/runtime/turn-run.d.ts +18 -9
- package/dist/runtime/turn-run.js +302 -71
- package/dist/runtime/wake-coalescing.d.ts +11 -3
- package/dist/runtime/wake-coalescing.js +10 -0
- package/dist/security/config.js +5 -10
- package/dist/security/logger.js +1 -1
- package/dist/security/path-guard.js +7 -63
- package/dist/security/types.d.ts +27 -30
- package/dist/security/types.js +0 -1
- package/dist/shared/atomic-file.d.ts +2 -0
- package/dist/shared/atomic-file.js +14 -1
- package/dist/shared/file-lock.d.ts +35 -0
- package/dist/shared/file-lock.js +195 -0
- package/dist/shared/with-timeout.d.ts +2 -0
- package/dist/shared/with-timeout.js +21 -0
- package/dist/skills/loader.d.ts +1 -1
- package/dist/skills/loader.js +2 -2
- package/dist/skills/workspace.js +2 -2
- package/dist/subagents/completion-wake.d.ts +11 -2
- package/dist/subagents/completion-wake.js +22 -9
- package/dist/subagents/discovery.js +1 -1
- package/dist/subagents/dispatch.d.ts +0 -10
- package/dist/subagents/dispatch.js +10 -28
- package/dist/subagents/external/dispatch.js +1 -1
- package/dist/subagents/external/run.d.ts +1 -1
- package/dist/subagents/external/run.js +3 -3
- package/dist/subagents/internal/run.js +1 -1
- package/dist/subagents/internal/worker.js +6 -14
- package/dist/subagents/run-archive.d.ts +11 -0
- package/dist/subagents/run-archive.js +49 -0
- package/dist/subagents/run-record.d.ts +9 -8
- package/dist/subagents/run-record.js +3 -3
- package/dist/subagents/run-registry.d.ts +5 -3
- package/dist/subagents/run-registry.js +22 -45
- package/dist/subagents/runs.d.ts +23 -13
- package/dist/subagents/runs.js +75 -82
- package/dist/subagents/task-envelope.js +9 -11
- package/dist/subagents/types.d.ts +6 -5
- package/dist/tasks/board.d.ts +1 -1
- package/dist/tasks/board.js +2 -2
- package/dist/tasks/bound-work.d.ts +2 -2
- package/dist/tasks/bound-work.js +7 -7
- package/dist/tasks/brief.d.ts +1 -1
- package/dist/tasks/brief.js +9 -9
- package/dist/tasks/commands.d.ts +11 -2
- package/dist/tasks/commands.js +66 -41
- package/dist/tasks/contract.d.ts +0 -2
- package/dist/tasks/contract.js +1 -3
- package/dist/tasks/create.d.ts +3 -1
- package/dist/tasks/create.js +10 -4
- package/dist/tasks/frontmatter.d.ts +6 -2
- package/dist/tasks/frontmatter.js +10 -10
- package/dist/tasks/loop-log.d.ts +7 -13
- package/dist/tasks/loop-log.js +14 -17
- package/dist/tasks/mutation-lock.d.ts +7 -5
- package/dist/tasks/mutation-lock.js +32 -9
- package/dist/tasks/session-path.d.ts +3 -3
- package/dist/tasks/session-path.js +5 -5
- package/dist/tasks/steer.d.ts +5 -5
- package/dist/tasks/steer.js +42 -40
- package/dist/tasks/store.d.ts +11 -11
- package/dist/tasks/store.js +32 -29
- package/dist/tools/bash.d.ts +5 -5
- package/dist/tools/bash.js +19 -18
- package/dist/tools/contract/names.d.ts +7 -0
- package/dist/tools/contract/names.js +22 -0
- package/dist/tools/edit.js +1 -1
- package/dist/tools/event-manage.d.ts +3 -1
- package/dist/tools/event-manage.js +34 -33
- package/dist/tools/glob.js +5 -5
- package/dist/tools/grep.js +2 -2
- package/dist/tools/index.d.ts +9 -8
- package/dist/tools/index.js +12 -16
- package/dist/tools/job.d.ts +4 -2
- package/dist/tools/job.js +11 -8
- package/dist/tools/memory-manage.d.ts +5 -3
- package/dist/tools/memory-manage.js +24 -24
- package/dist/tools/read.js +1 -1
- package/dist/tools/registry.d.ts +12 -8
- package/dist/tools/registry.js +13 -9
- package/dist/tools/send-media.d.ts +2 -2
- package/dist/tools/send-media.js +3 -3
- package/dist/tools/session-search.d.ts +3 -1
- package/dist/tools/session-search.js +8 -6
- package/dist/tools/subagent-manage.d.ts +2 -8
- package/dist/tools/subagent-manage.js +19 -15
- package/dist/tools/subagent.js +1 -1
- package/dist/tools/task-manage/create.js +1 -1
- package/dist/tools/task-manage/factory.js +2 -2
- package/dist/tools/task-manage/lifecycle.js +3 -1
- package/dist/tools/task-manage/log-view.js +2 -2
- package/dist/tools/task-manage/shared.js +3 -3
- package/dist/tools/task-manage/step-end.js +3 -3
- package/dist/tools/task-manage/types.d.ts +8 -3
- package/dist/tools/web-cache.d.ts +3 -3
- package/dist/tools/web-cache.js +12 -12
- package/dist/tools/web-fetch.d.ts +2 -2
- package/dist/tools/web-fetch.js +7 -7
- package/dist/tools/write-content.js +1 -1
- package/dist/tui/cli.d.ts +17 -5
- package/dist/tui/cli.js +90 -27
- package/dist/tui/client.d.ts +26 -0
- package/dist/tui/client.js +157 -0
- package/dist/tui/colors.d.ts +0 -1
- package/dist/tui/colors.js +0 -1
- package/dist/tui/commands.d.ts +10 -51
- package/dist/tui/commands.js +16 -39
- package/dist/tui/frontend.d.ts +2 -2
- package/dist/tui/frontend.js +2 -2
- package/dist/tui/pitui-frontend.d.ts +0 -2
- package/dist/tui/pitui-frontend.js +1 -6
- package/dist/tui/render.d.ts +62 -0
- package/dist/tui/render.js +123 -0
- package/dist/tui/session.d.ts +92 -0
- package/dist/tui/session.js +317 -0
- package/dist/usage/ledger.d.ts +5 -0
- package/dist/usage/ledger.js +2 -0
- package/dist/usage/render.d.ts +6 -1
- package/dist/usage/render.js +18 -13
- package/dist/webapi/auth.d.ts +13 -0
- package/dist/webapi/auth.js +29 -0
- package/dist/webapi/config.d.ts +29 -0
- package/dist/webapi/config.js +100 -0
- package/dist/webapi/errors.d.ts +39 -0
- package/dist/webapi/errors.js +32 -0
- package/dist/webapi/http.d.ts +25 -0
- package/dist/webapi/http.js +4 -0
- package/dist/webapi/ids.d.ts +9 -0
- package/dist/webapi/ids.js +20 -0
- package/dist/webapi/input.d.ts +17 -0
- package/dist/webapi/input.js +229 -0
- package/dist/webapi/items.d.ts +37 -0
- package/dist/webapi/items.js +48 -0
- package/dist/webapi/key-command.d.ts +2 -0
- package/dist/webapi/key-command.js +110 -0
- package/dist/webapi/limits.d.ts +47 -0
- package/dist/webapi/limits.js +48 -0
- package/dist/webapi/request-policy.d.ts +17 -0
- package/dist/webapi/request-policy.js +160 -0
- package/dist/webapi/response-record.d.ts +68 -0
- package/dist/webapi/response-record.js +58 -0
- package/dist/webapi/response-state.d.ts +90 -0
- package/dist/webapi/response-state.js +347 -0
- package/dist/webapi/routes/conversations.d.ts +8 -0
- package/dist/webapi/routes/conversations.js +80 -0
- package/dist/webapi/routes/models.d.ts +3 -0
- package/dist/webapi/routes/models.js +12 -0
- package/dist/webapi/routes/responses.d.ts +8 -0
- package/dist/webapi/routes/responses.js +118 -0
- package/dist/webapi/routes/websocket.d.ts +9 -0
- package/dist/webapi/routes/websocket.js +328 -0
- package/dist/webapi/server.d.ts +24 -0
- package/dist/webapi/server.js +229 -0
- package/dist/webapi/service.d.ts +130 -0
- package/dist/webapi/service.js +588 -0
- package/dist/webapi/store.d.ts +46 -0
- package/dist/webapi/store.js +245 -0
- package/dist/webapi/stream.d.ts +13 -0
- package/dist/webapi/stream.js +76 -0
- package/dist/webapi/transport.d.ts +43 -0
- package/dist/webapi/transport.js +100 -0
- package/package.json +8 -2
- package/dist/channel/channel-paths.d.ts +0 -16
- package/dist/channel/channel-paths.js +0 -53
- package/dist/channel/commands.d.ts +0 -32
- package/dist/channel/commands.js +0 -109
- package/dist/channel/project-scope-store.d.ts +0 -51
- package/dist/channel/project-scope-store.js +0 -112
- package/dist/dingtalk/channel-queue.d.ts +0 -19
- package/dist/dingtalk/handler.d.ts +0 -90
- package/dist/memory/channel-maintenance-queue.d.ts +0 -5
- package/dist/memory/channel-maintenance-queue.js +0 -8
- package/dist/runtime/task-migration.d.ts +0 -30
- package/dist/runtime/task-migration.js +0 -465
- package/dist/runtime/turn-handler.d.ts +0 -47
- package/dist/runtime/turn-handler.js +0 -318
- package/dist/security/project-scope.d.ts +0 -45
- package/dist/security/project-scope.js +0 -105
- package/dist/tui/app.d.ts +0 -16
- package/dist/tui/app.js +0 -166
- package/dist/tui/terminal-context.d.ts +0 -27
- package/dist/tui/terminal-context.js +0 -84
- package/dist/tui/terminal-media-sender.d.ts +0 -9
- package/dist/tui/terminal-media-sender.js +0 -15
- package/dist/tui/turn-controller.d.ts +0 -64
- package/dist/tui/turn-controller.js +0 -311
- /package/dist/{dingtalk/handler.js → channel/transport.js} +0 -0
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ChannelEventSink, WakeEvent } from "../channel/channel-event.js";
|
|
2
2
|
type DispatchStatus = "pending" | "queued" | "running" | "exhausted";
|
|
3
3
|
export interface DurableDispatchRecord {
|
|
4
4
|
version: 1;
|
|
5
5
|
id: string;
|
|
6
6
|
createdAt: string;
|
|
7
7
|
status: DispatchStatus;
|
|
8
|
-
event:
|
|
8
|
+
event: WakeEvent;
|
|
9
9
|
deliveries: number;
|
|
10
10
|
leaseExpiresAt?: string;
|
|
11
11
|
/** Set by `markRetryable` to back off a repeatedly-failing structured wake instead of
|
|
@@ -15,6 +15,12 @@ export interface DurableDispatchRecord {
|
|
|
15
15
|
export interface DurableDispatchOptions {
|
|
16
16
|
stateDir: string;
|
|
17
17
|
bot: ChannelEventSink;
|
|
18
|
+
/**
|
|
19
|
+
* Whether the channel's transport is switched on in this process (spec 061, INV-8). A record for
|
|
20
|
+
* a transport that is off is not delivered and not counted against its delivery limit: it stays
|
|
21
|
+
* on disk until a run in which the transport is back.
|
|
22
|
+
*/
|
|
23
|
+
isReachable?: (channelId: string) => boolean;
|
|
18
24
|
leaseMs?: number;
|
|
19
25
|
intervalMs?: number;
|
|
20
26
|
/** Called once, at most, when a dispatch id hits `MAX_DELIVERIES` without ever completing —
|
|
@@ -66,7 +72,7 @@ export declare class DurableDispatchService {
|
|
|
66
72
|
*/
|
|
67
73
|
private drainSafely;
|
|
68
74
|
stop(): void;
|
|
69
|
-
dispatch(event:
|
|
75
|
+
dispatch(event: WakeEvent): Promise<boolean>;
|
|
70
76
|
/** Reset any in-flight records for a channel so a stop/abort can redeliver on the next tick, not after the lease expires. */
|
|
71
77
|
cancelChannel(channelId: string): Promise<number>;
|
|
72
78
|
/**
|
|
@@ -92,9 +98,9 @@ export declare class DurableDispatchService {
|
|
|
92
98
|
* crash mid-turn redelivers each of them on its own, unchanged. Returned events carry their
|
|
93
99
|
* own redelivery notice, like a normal delivery would.
|
|
94
100
|
*/
|
|
95
|
-
absorbPending(channelId: string, primaryId: string, accept: (event:
|
|
101
|
+
absorbPending(channelId: string, primaryId: string, accept: (event: WakeEvent) => boolean, limit: number,
|
|
96
102
|
/** `stopEpoch` sampled when the caller's admission began; a later `/stop` suppresses the stale-delivery marks. */
|
|
97
|
-
epoch?: number): Promise<
|
|
103
|
+
epoch?: number): Promise<WakeEvent[]>;
|
|
98
104
|
/** Not started anywhere: still waiting for delivery (`pending`) or queued behind the current turn. */
|
|
99
105
|
private isAbsorbable;
|
|
100
106
|
markCompleted(id: string | undefined): Promise<void>;
|
|
@@ -19,12 +19,11 @@ const MAX_DELIVERIES = 8;
|
|
|
19
19
|
const MIN_RETRY_BACKOFF_MS = 30_000;
|
|
20
20
|
const MAX_RETRY_BACKOFF_MS = 10 * 60_000;
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* the on-disk filename changes.
|
|
22
|
+
* A dispatch id is an opaque business id and may contain `/`. Used verbatim as a filename it would
|
|
23
|
+
* turn into a nested path a flat `readdir(stateDir)` scan can never find again (R1): a queue
|
|
24
|
+
* rejection or a restart then makes that record un-drainable and un-cancellable forever, even once
|
|
25
|
+
* capacity returns. Percent-encoding makes it always a flat filename and reversible without
|
|
26
|
+
* collisions, without touching the id itself — only the on-disk filename changes.
|
|
28
27
|
*/
|
|
29
28
|
function encodeDispatchId(id) {
|
|
30
29
|
return encodeURIComponent(id);
|
|
@@ -62,7 +61,7 @@ function withRedeliveryNotice(event, deliveries) {
|
|
|
62
61
|
}
|
|
63
62
|
/**
|
|
64
63
|
* What a wake was for, in the user's terms (spec 053, D2). The wake's own `text` is a prompt for
|
|
65
|
-
* the model and its `
|
|
64
|
+
* the model and its `producer` an internal tag (`TASK_DRIVER`, `SUBAGENT`, …), so neither
|
|
66
65
|
* may reach the chat; every in-process producer prefixes its text with a `[KIND:id…]` tag, which
|
|
67
66
|
* is what this reads instead.
|
|
68
67
|
*/
|
|
@@ -96,7 +95,7 @@ export function renderDispatchExhaustedNotice(record, stateDir) {
|
|
|
96
95
|
}
|
|
97
96
|
function dispatchId(event) {
|
|
98
97
|
return createHash("sha256")
|
|
99
|
-
.update(JSON.stringify([event.channelId, event.
|
|
98
|
+
.update(JSON.stringify([event.channelId, event.producer, event.ts, event.text]))
|
|
100
99
|
.digest("hex");
|
|
101
100
|
}
|
|
102
101
|
function parseRecord(raw) {
|
|
@@ -111,7 +110,9 @@ function parseRecord(raw) {
|
|
|
111
110
|
value.status !== "running" &&
|
|
112
111
|
value.status !== "exhausted") ||
|
|
113
112
|
!isRecord(value.event) ||
|
|
113
|
+
value.event.source !== "wake" ||
|
|
114
114
|
typeof value.event.channelId !== "string" ||
|
|
115
|
+
typeof value.event.producer !== "string" ||
|
|
115
116
|
typeof value.event.text !== "string" ||
|
|
116
117
|
typeof value.deliveries !== "number" ||
|
|
117
118
|
(value.nextAttemptAt !== undefined && typeof value.nextAttemptAt !== "string")) {
|
|
@@ -178,6 +179,15 @@ export class DurableDispatchService {
|
|
|
178
179
|
this.timer = null;
|
|
179
180
|
}
|
|
180
181
|
async dispatch(event) {
|
|
182
|
+
// Spec 058, D7: every in-process producer stamps its desk. An event without one cannot be
|
|
183
|
+
// checked against the channel's binding at admission, so it is refused here, at the door.
|
|
184
|
+
if (!event.desk) {
|
|
185
|
+
log.logWarn("dispatch.event.missing_desk", "Dispatch refused: synthetic event carries no desk", {
|
|
186
|
+
channelId: event.channelId,
|
|
187
|
+
fields: { dispatchId: event.dispatchId, producer: event.producer },
|
|
188
|
+
});
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
181
191
|
const id = event.dispatchId ?? dispatchId(event);
|
|
182
192
|
await this.queue.run(id, async () => {
|
|
183
193
|
const existing = await this.read(id);
|
|
@@ -421,6 +431,8 @@ export class DurableDispatchService {
|
|
|
421
431
|
const nextAttemptMs = record.nextAttemptAt ? new Date(record.nextAttemptAt).getTime() : undefined;
|
|
422
432
|
if (record.status === "pending" && nextAttemptMs && nextAttemptMs > now)
|
|
423
433
|
return;
|
|
434
|
+
if (this.options.isReachable && !this.options.isReachable(record.event.channelId))
|
|
435
|
+
return;
|
|
424
436
|
if (record.deliveries >= MAX_DELIVERIES) {
|
|
425
437
|
record.status = "exhausted";
|
|
426
438
|
record.leaseExpiresAt = undefined;
|
|
@@ -1,18 +1,40 @@
|
|
|
1
1
|
import type { InboundImage } from "../channel/channel-event.js";
|
|
2
|
+
import type { InboundAttachment } from "../channel/ingress.js";
|
|
2
3
|
/**
|
|
3
|
-
* Absolute `InboundImage.path`s → paths relative to the
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* machine other than the one that wrote it, and `~/.pipiclaw` itself can move between backup and
|
|
7
|
-
* restore.
|
|
4
|
+
* Absolute `InboundImage.path`s → paths relative to the desk directory, for `LoggedMessage.images`
|
|
5
|
+
* (spec 049). An absolute path in `log.jsonl` would be dead on any machine other than the one that
|
|
6
|
+
* wrote it, and `~/.pipiclaw` itself can move between backup and restore.
|
|
8
7
|
*/
|
|
9
|
-
export declare function relativeImagePaths(
|
|
8
|
+
export declare function relativeImagePaths(deskDir: string | undefined, images: InboundImage[] | undefined): string[];
|
|
10
9
|
/**
|
|
11
|
-
* Persist one downloaded inbound image under the
|
|
12
|
-
*
|
|
13
|
-
* `
|
|
10
|
+
* Persist one downloaded inbound image under the desk's `inbox/` (spec 049). The runtime owns this,
|
|
11
|
+
* not the transport, because it resolves the binding; an unbound channel never gets here (D4).
|
|
12
|
+
* `mimeType` is `null` when magic-byte sniffing did not recognize a supported image format: the
|
|
13
|
+
* file is still persisted (for the archive) but never handed to a model. Returns `null` on a local
|
|
14
|
+
* I/O failure.
|
|
14
15
|
*/
|
|
15
|
-
export declare function persistInboundImage(
|
|
16
|
+
export declare function persistInboundImage(deskDir: string, channelId: string, image: {
|
|
16
17
|
data: Buffer;
|
|
17
18
|
mimeType: string | null;
|
|
18
19
|
}): Promise<InboundImage | null>;
|
|
20
|
+
/** Persist one inbound file under the desk's `inbox/`; the path, or `null` on a local I/O failure. */
|
|
21
|
+
export declare function persistInboundFile(deskDir: string, channelId: string, file: {
|
|
22
|
+
data: Buffer;
|
|
23
|
+
fileName: string;
|
|
24
|
+
}): Promise<string | null>;
|
|
25
|
+
/**
|
|
26
|
+
* Settle one message's attachments once its channel's desk is known (D5): persist each under the
|
|
27
|
+
* desk's `inbox/` and say in the text what became of it. An image that could not be saved, or is
|
|
28
|
+
* not a recognizable image format, never reaches the model; it only changes its own marker. With
|
|
29
|
+
* no desk (the channel is not bound to an available one) nothing is written and every attachment
|
|
30
|
+
* reads as not saved.
|
|
31
|
+
*/
|
|
32
|
+
export declare function resolveInboundAttachments(input: {
|
|
33
|
+
deskDir: string | undefined;
|
|
34
|
+
channelId: string;
|
|
35
|
+
text: string;
|
|
36
|
+
attachments: readonly InboundAttachment[];
|
|
37
|
+
}): Promise<{
|
|
38
|
+
text: string;
|
|
39
|
+
images: InboundImage[];
|
|
40
|
+
}>;
|
|
@@ -1,39 +1,40 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Inbound
|
|
3
|
-
*
|
|
2
|
+
* Inbound attachments on the runtime side: persist what a transport downloaded under the bound
|
|
3
|
+
* desk's `inbox/`, settle the `[图片N]` / `[文件N]` markers in the message text, and map absolute
|
|
4
|
+
* image paths back to desk-relative ones for the message archive.
|
|
4
5
|
*/
|
|
6
|
+
import { randomBytes } from "node:crypto";
|
|
5
7
|
import { mkdir, writeFile } from "fs/promises";
|
|
6
|
-
import { join, relative } from "path";
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
8
|
+
import { basename, join, relative } from "path";
|
|
9
|
+
import { extensionForMimeType, sniffImageMimeType } from "../channel/attachments.js";
|
|
10
|
+
import { getDeskInboxDir } from "../desk/paths.js";
|
|
9
11
|
import * as log from "../log.js";
|
|
10
12
|
import { localStampForFilename } from "../shared/local-time.js";
|
|
11
13
|
import { errorMessage } from "../shared/text-utils.js";
|
|
12
14
|
/**
|
|
13
|
-
* Absolute `InboundImage.path`s → paths relative to the
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* machine other than the one that wrote it, and `~/.pipiclaw` itself can move between backup and
|
|
17
|
-
* restore.
|
|
15
|
+
* Absolute `InboundImage.path`s → paths relative to the desk directory, for `LoggedMessage.images`
|
|
16
|
+
* (spec 049). An absolute path in `log.jsonl` would be dead on any machine other than the one that
|
|
17
|
+
* wrote it, and `~/.pipiclaw` itself can move between backup and restore.
|
|
18
18
|
*/
|
|
19
|
-
export function relativeImagePaths(
|
|
20
|
-
if (!images || images.length === 0)
|
|
19
|
+
export function relativeImagePaths(deskDir, images) {
|
|
20
|
+
if (!deskDir || !images || images.length === 0)
|
|
21
21
|
return [];
|
|
22
|
-
|
|
23
|
-
return images.map((image) => relative(channelDir, image.path));
|
|
22
|
+
return images.map((image) => relative(deskDir, image.path));
|
|
24
23
|
}
|
|
25
24
|
/**
|
|
26
|
-
* Persist one downloaded inbound image under the
|
|
27
|
-
*
|
|
28
|
-
* `
|
|
25
|
+
* Persist one downloaded inbound image under the desk's `inbox/` (spec 049). The runtime owns this,
|
|
26
|
+
* not the transport, because it resolves the binding; an unbound channel never gets here (D4).
|
|
27
|
+
* `mimeType` is `null` when magic-byte sniffing did not recognize a supported image format: the
|
|
28
|
+
* file is still persisted (for the archive) but never handed to a model. Returns `null` on a local
|
|
29
|
+
* I/O failure.
|
|
29
30
|
*/
|
|
30
|
-
export async function persistInboundImage(
|
|
31
|
+
export async function persistInboundImage(deskDir, channelId, image) {
|
|
31
32
|
try {
|
|
32
|
-
const dir =
|
|
33
|
+
const dir = getDeskInboxDir(deskDir);
|
|
33
34
|
await mkdir(dir, { recursive: true });
|
|
34
35
|
const extension = extensionForMimeType(image.mimeType);
|
|
35
36
|
const path = join(dir, `image-${localStampForFilename()}-${Math.random().toString(36).slice(2, 8)}.${extension}`);
|
|
36
|
-
await writeFile(path, image.data, { mode: 0o600 });
|
|
37
|
+
await writeFile(path, image.data, { mode: 0o600, flag: "wx" });
|
|
37
38
|
return {
|
|
38
39
|
path,
|
|
39
40
|
mimeType: image.mimeType ?? "application/octet-stream",
|
|
@@ -42,10 +43,83 @@ export async function persistInboundImage(workspaceDir, channelId, image) {
|
|
|
42
43
|
};
|
|
43
44
|
}
|
|
44
45
|
catch (err) {
|
|
45
|
-
log.logWarn("
|
|
46
|
+
log.logWarn("runtime.inbound_image.persist_failed", "Inbound image persist failed", {
|
|
46
47
|
channelId: channelId,
|
|
47
48
|
reason: errorMessage(err),
|
|
48
49
|
});
|
|
49
50
|
return null;
|
|
50
51
|
}
|
|
51
52
|
}
|
|
53
|
+
const MAX_INBOUND_FILE_NAME_CHARS = 100;
|
|
54
|
+
/** A transport-supplied file name made safe to use as the tail of a file name inside `inbox/`. */
|
|
55
|
+
function safeInboundFileName(fileName) {
|
|
56
|
+
const base = basename(fileName.replace(/\\/g, "/")).replace(/[^\p{L}\p{N}._-]+/gu, "_");
|
|
57
|
+
const trimmed = base.replace(/^\.+/, "").slice(-MAX_INBOUND_FILE_NAME_CHARS);
|
|
58
|
+
return trimmed || "file";
|
|
59
|
+
}
|
|
60
|
+
/** Persist one inbound file under the desk's `inbox/`; the path, or `null` on a local I/O failure. */
|
|
61
|
+
export async function persistInboundFile(deskDir, channelId, file) {
|
|
62
|
+
try {
|
|
63
|
+
const dir = getDeskInboxDir(deskDir);
|
|
64
|
+
await mkdir(dir, { recursive: true });
|
|
65
|
+
const unique = `${localStampForFilename()}-${randomBytes(4).toString("hex")}`;
|
|
66
|
+
const path = join(dir, `file-${unique}-${safeInboundFileName(file.fileName)}`);
|
|
67
|
+
await writeFile(path, file.data, { mode: 0o600, flag: "wx" });
|
|
68
|
+
return path;
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
log.logWarn("runtime.inbound_file.persist_failed", "Inbound file persist failed", {
|
|
72
|
+
channelId: channelId,
|
|
73
|
+
reason: errorMessage(err),
|
|
74
|
+
});
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Settle one message's attachments once its channel's desk is known (D5): persist each under the
|
|
80
|
+
* desk's `inbox/` and say in the text what became of it. An image that could not be saved, or is
|
|
81
|
+
* not a recognizable image format, never reaches the model; it only changes its own marker. With
|
|
82
|
+
* no desk (the channel is not bound to an available one) nothing is written and every attachment
|
|
83
|
+
* reads as not saved.
|
|
84
|
+
*/
|
|
85
|
+
export async function resolveInboundAttachments(input) {
|
|
86
|
+
const { deskDir, channelId } = input;
|
|
87
|
+
let text = input.text;
|
|
88
|
+
const appended = [];
|
|
89
|
+
const replaceMarker = (marker, replacement) => {
|
|
90
|
+
text = text.replace(marker, replacement);
|
|
91
|
+
};
|
|
92
|
+
const images = [];
|
|
93
|
+
await Promise.all(input.attachments.map(async (attachment) => {
|
|
94
|
+
if (attachment.kind === "image") {
|
|
95
|
+
const marker = `[图片${attachment.index}]`;
|
|
96
|
+
const mimeType = sniffImageMimeType(attachment.data);
|
|
97
|
+
const persisted = deskDir
|
|
98
|
+
? await persistInboundImage(deskDir, channelId, { data: attachment.data, mimeType })
|
|
99
|
+
: null;
|
|
100
|
+
if (!persisted) {
|
|
101
|
+
replaceMarker(marker, `[图片${attachment.index}:保存失败]`);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (!mimeType) {
|
|
105
|
+
// Persisted for the record, but never handed to a model.
|
|
106
|
+
replaceMarker(marker, `[图片${attachment.index}:格式不支持,已保存]`);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
images.push({ index: attachment.index, image: persisted });
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const marker = `[文件${attachment.index}]`;
|
|
113
|
+
const path = deskDir ? await persistInboundFile(deskDir, channelId, attachment) : null;
|
|
114
|
+
if (!path) {
|
|
115
|
+
replaceMarker(marker, `[文件${attachment.index}:保存失败]`);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
appended.push(`[文件${attachment.index}:${attachment.fileName} 已保存到 ${path}]`);
|
|
119
|
+
}));
|
|
120
|
+
appended.sort();
|
|
121
|
+
return {
|
|
122
|
+
text: appended.length > 0 ? `${text}\n${appended.join("\n")}` : text,
|
|
123
|
+
images: images.sort((a, b) => a.index - b.index).map((entry) => entry.image),
|
|
124
|
+
};
|
|
125
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { HumanEvent } from "../channel/channel-event.js";
|
|
2
|
+
import type { StopOutcome } from "../channel/ingress.js";
|
|
3
|
+
import type { IngressDeps } from "./ingress-deps.js";
|
|
4
|
+
/** How a message that met a busy channel ended up. */
|
|
5
|
+
export type BusyOutcome =
|
|
6
|
+
/** It went into the running turn as a steering message. */
|
|
7
|
+
{
|
|
8
|
+
kind: "steered";
|
|
9
|
+
}
|
|
10
|
+
/** The running turn cannot take it: run `text` as a normal turn after it. */
|
|
11
|
+
| {
|
|
12
|
+
kind: "requeue";
|
|
13
|
+
text: string;
|
|
14
|
+
}
|
|
15
|
+
/** Nothing more to do (the user was told, if there was something to tell). */
|
|
16
|
+
| {
|
|
17
|
+
kind: "handled";
|
|
18
|
+
};
|
|
19
|
+
export declare function createBusyHandlers(deps: IngressDeps, queues: {
|
|
20
|
+
clearPending(channelId: string): number;
|
|
21
|
+
reset(channelId: string): number;
|
|
22
|
+
}): {
|
|
23
|
+
handleBusyMessage: (event: HumanEvent, mode: "steer" | "followUp", queueText: string, quiet?: boolean) => Promise<BusyOutcome>;
|
|
24
|
+
handleStop: (channelId: string) => Promise<StopOutcome>;
|
|
25
|
+
cancelRunningTurn: (channelId: string) => boolean;
|
|
26
|
+
};
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the runtime does with a message that arrives while its channel is already running a turn
|
|
3
|
+
* (`/steer`, `/followup`, a plain message under a steering policy), and with `/stop`. These are
|
|
4
|
+
* the branches of `ingress.ts`'s routing that touch the live runner.
|
|
5
|
+
*/
|
|
6
|
+
import { forceEndStuckTurnAfterStop } from "../agent/stop-watchdog.js";
|
|
7
|
+
import { formatMissingQueueMessage, formatSteerQueued } from "../commands/catalog.js";
|
|
8
|
+
import * as log from "../log.js";
|
|
9
|
+
import { errorMessage } from "../shared/text-utils.js";
|
|
10
|
+
import { pauseTask as pauseStoredTask } from "../tasks/store.js";
|
|
11
|
+
import { relativeImagePaths } from "./inbound-images.js";
|
|
12
|
+
import { parseDriverTaskId } from "./task-driver.js";
|
|
13
|
+
function isNoRunningTaskQueueError(err) {
|
|
14
|
+
return err instanceof Error && err.message === "No task is currently running.";
|
|
15
|
+
}
|
|
16
|
+
export function createBusyHandlers(deps, queues) {
|
|
17
|
+
const { options, channelRunners, stopWatchdogs, archiveIncomingMessage, getRunner, isShuttingDown, getDurableDispatch, } = deps;
|
|
18
|
+
/** Tell the channel something outside any turn. */
|
|
19
|
+
const say = (event, text) => deps.pushTo(event.channelId, { text, ...(event.ref !== undefined ? { ref: event.ref } : {}) });
|
|
20
|
+
/**
|
|
21
|
+
* Hand a message to the turn that is running, per `mode`. `quiet` is for a caller that reports
|
|
22
|
+
* the result itself (a Web API steer): no confirmation or explanation is sent to the channel.
|
|
23
|
+
*/
|
|
24
|
+
async function handleBusyMessage(event, mode, queueText, quiet = false) {
|
|
25
|
+
if (isShuttingDown()) {
|
|
26
|
+
return { kind: "handled" };
|
|
27
|
+
}
|
|
28
|
+
// A channel is busy only while it has a runner, which only a bound channel ever gets; a
|
|
29
|
+
// binding that disappeared in between is simply run as a normal (refused) turn.
|
|
30
|
+
const runner = getRunner(event.channelId);
|
|
31
|
+
const trimmedQueueText = queueText.trim();
|
|
32
|
+
if (!runner)
|
|
33
|
+
return { kind: "requeue", text: trimmedQueueText || event.text };
|
|
34
|
+
if (!trimmedQueueText) {
|
|
35
|
+
if (!quiet)
|
|
36
|
+
await say(event, formatMissingQueueMessage(mode === "followUp" ? "followup" : "steer"));
|
|
37
|
+
return { kind: "handled" };
|
|
38
|
+
}
|
|
39
|
+
// Compaction is maintenance, not user work. A new message cancels it and is
|
|
40
|
+
// kept as a normal queued turn instead of trying to steer an agent loop that
|
|
41
|
+
// is currently disconnected for summarization.
|
|
42
|
+
if (runner.interruptCompaction()) {
|
|
43
|
+
log.logInfoEvent("agent.compaction.interrupted", "Compaction interrupted for a new user message", {
|
|
44
|
+
channelId: event.channelId,
|
|
45
|
+
});
|
|
46
|
+
return { kind: "requeue", text: trimmedQueueText };
|
|
47
|
+
}
|
|
48
|
+
// A task step runs in the task's own session and is muted: a steer here would land in
|
|
49
|
+
// the background task's context and its answer would never reach the user, while the
|
|
50
|
+
// message itself is kept out of the chat session. Run it as the next chat turn instead.
|
|
51
|
+
const taskLoop = runner.getTaskLoop();
|
|
52
|
+
if (taskLoop) {
|
|
53
|
+
if (!quiet) {
|
|
54
|
+
await say(event, `后台任务 \`${taskLoop.taskId}\` 正在执行一步,你的消息会在这一步结束后处理;要给该任务补充指示,用 \`/tasks steer ${taskLoop.taskId} <内容>\`。`);
|
|
55
|
+
}
|
|
56
|
+
log.logEvent("info", "agent.turn.followup_queued", "Message deferred past a task step", {
|
|
57
|
+
ctx: { channelId: event.channelId, userName: event.userName },
|
|
58
|
+
fields: { messageLength: trimmedQueueText.length, taskId: taskLoop.taskId },
|
|
59
|
+
});
|
|
60
|
+
return { kind: "requeue", text: trimmedQueueText };
|
|
61
|
+
}
|
|
62
|
+
if (mode === "followUp") {
|
|
63
|
+
log.logEvent("info", "agent.turn.followup_queued", "Follow-up queued", {
|
|
64
|
+
ctx: { channelId: event.channelId, userName: event.userName },
|
|
65
|
+
fields: { messageLength: trimmedQueueText.length },
|
|
66
|
+
});
|
|
67
|
+
return { kind: "requeue", text: trimmedQueueText };
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
await runner.queueSteer(trimmedQueueText, event.userName, event.images);
|
|
71
|
+
await archiveIncomingMessage(event.channelId, {
|
|
72
|
+
date: new Date().toISOString(),
|
|
73
|
+
ts: event.ts,
|
|
74
|
+
user: event.user,
|
|
75
|
+
userName: event.userName,
|
|
76
|
+
text: event.text,
|
|
77
|
+
isBot: false,
|
|
78
|
+
deliveryMode: mode,
|
|
79
|
+
skipContextSync: true,
|
|
80
|
+
images: relativeImagePaths(runner.conversation.deskDir, event.images),
|
|
81
|
+
}, `${mode} message`);
|
|
82
|
+
if (!quiet)
|
|
83
|
+
await say(event, formatSteerQueued(!event.text.trim().startsWith("/")));
|
|
84
|
+
log.logEvent("info", "agent.turn.steer_queued", "Steer queued", {
|
|
85
|
+
ctx: { channelId: event.channelId, userName: event.userName },
|
|
86
|
+
fields: { messageLength: trimmedQueueText.length },
|
|
87
|
+
});
|
|
88
|
+
return { kind: "steered" };
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
const errMsg = errorMessage(err);
|
|
92
|
+
if (isNoRunningTaskQueueError(err)) {
|
|
93
|
+
log.logInfoEvent("agent.turn.requeued", "Busy window closed; message requeued as a normal turn", {
|
|
94
|
+
channelId: event.channelId,
|
|
95
|
+
fields: { mode },
|
|
96
|
+
});
|
|
97
|
+
return { kind: "requeue", text: trimmedQueueText };
|
|
98
|
+
}
|
|
99
|
+
log.logWarn("agent.turn.queue_failed", "Busy message queueing failed", {
|
|
100
|
+
channelId: event.channelId,
|
|
101
|
+
reason: errMsg,
|
|
102
|
+
fields: { mode },
|
|
103
|
+
});
|
|
104
|
+
if (!quiet) {
|
|
105
|
+
await say(event, `这条消息没能排进当前回合:${errMsg}\n等当前回合结束后重发,或用 \`/followup <消息>\` 排到它之后。`);
|
|
106
|
+
}
|
|
107
|
+
return { kind: "handled" };
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Abort `runner`'s turn, with the watchdog that isolates it if the abort never lands. The turn's
|
|
112
|
+
* output is ended at once (the card is closed, late events dropped).
|
|
113
|
+
*/
|
|
114
|
+
function abortTurn(channelId, runner) {
|
|
115
|
+
runner.requestStop();
|
|
116
|
+
void runner.abort().catch((err) => {
|
|
117
|
+
log.logWarn("agent.turn.abort_failed", "Turn abort failed", {
|
|
118
|
+
channelId: channelId,
|
|
119
|
+
reason: errorMessage(err),
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
if (stopWatchdogs.has(channelId))
|
|
123
|
+
return;
|
|
124
|
+
stopWatchdogs.add(channelId);
|
|
125
|
+
void forceEndStuckTurnAfterStop({
|
|
126
|
+
channelId,
|
|
127
|
+
runner,
|
|
128
|
+
graceMs: options.stopForceEndGraceMs,
|
|
129
|
+
isCurrent: () => channelRunners.get(channelId) === runner,
|
|
130
|
+
recover: () => {
|
|
131
|
+
runner.retireForRecovery();
|
|
132
|
+
channelRunners.delete(channelId);
|
|
133
|
+
queues.reset(channelId);
|
|
134
|
+
getDurableDispatch()?.discardQueuedClaims(channelId);
|
|
135
|
+
},
|
|
136
|
+
notify: (text) => deps.pushTo(channelId, { text }),
|
|
137
|
+
})
|
|
138
|
+
.catch((err) => {
|
|
139
|
+
log.logWarn("agent.turn.stop_watchdog_failed", "Stop watchdog failed", {
|
|
140
|
+
channelId: channelId,
|
|
141
|
+
reason: errorMessage(err),
|
|
142
|
+
});
|
|
143
|
+
})
|
|
144
|
+
.finally(() => {
|
|
145
|
+
stopWatchdogs.delete(channelId);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
/** One message's turn is no longer wanted (its caller cancelled or left): abort only that turn. */
|
|
149
|
+
function cancelRunningTurn(channelId) {
|
|
150
|
+
const runner = channelRunners.get(channelId);
|
|
151
|
+
if (!runner?.isBusy())
|
|
152
|
+
return false;
|
|
153
|
+
abortTurn(channelId, runner);
|
|
154
|
+
log.logInfoEvent("agent.turn.cancel_requested", "Turn cancel requested", { channelId: channelId });
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
/** `/stop`: abort the running turn, drop what was queued behind it, and pause the task it was a step of. */
|
|
158
|
+
async function handleStop(channelId) {
|
|
159
|
+
const runner = channelRunners.get(channelId);
|
|
160
|
+
let pausedTaskId;
|
|
161
|
+
if (runner?.isBusy()) {
|
|
162
|
+
// The runner ends the turn's output at once (the card is closed, late events dropped),
|
|
163
|
+
// before the task is paused below.
|
|
164
|
+
runner.requestStop();
|
|
165
|
+
// Stopping a task step stops the task: otherwise the driver would queue the next step
|
|
166
|
+
// straight away. The step may not be bound to its session yet, so fall back to the text.
|
|
167
|
+
const taskId = runner.getTaskLoop()?.taskId ?? parseDriverTaskId(runner.getTurnStatus().taskText);
|
|
168
|
+
if (taskId) {
|
|
169
|
+
await pauseStoredTask(runner.conversation.deskDir, taskId, {
|
|
170
|
+
by: "user",
|
|
171
|
+
reason: "用户 /stop 中止了该任务的当前步骤。",
|
|
172
|
+
});
|
|
173
|
+
pausedTaskId = taskId;
|
|
174
|
+
log.logInfoEvent("tasks.task.paused_on_stop", "Task paused on /stop", {
|
|
175
|
+
channelId: channelId,
|
|
176
|
+
fields: { taskId },
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
// Drop queued-but-not-started messages so a burst does not keep
|
|
180
|
+
// running after the user asked to halt; abort the in-flight one.
|
|
181
|
+
const dropped = queues.clearPending(channelId);
|
|
182
|
+
// Same tick as the clear: record that absorbed wakes' queued closures are gone.
|
|
183
|
+
getDurableDispatch()?.discardQueuedClaims(channelId);
|
|
184
|
+
if (dropped > 0) {
|
|
185
|
+
log.logInfoEvent("agent.turn.queue_dropped", "Queued messages dropped on /stop", {
|
|
186
|
+
channelId: channelId,
|
|
187
|
+
fields: { count: dropped },
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
abortTurn(channelId, runner);
|
|
191
|
+
void getDurableDispatch()
|
|
192
|
+
?.cancelChannel(channelId)
|
|
193
|
+
.then((canceled) => {
|
|
194
|
+
if (canceled)
|
|
195
|
+
log.logInfoEvent("dispatch.cancel.reset", "Durable dispatch leases reset on /stop", {
|
|
196
|
+
channelId: channelId,
|
|
197
|
+
fields: { count: canceled },
|
|
198
|
+
});
|
|
199
|
+
})
|
|
200
|
+
.catch((err) => {
|
|
201
|
+
log.logWarn("dispatch.cancel.failed", "Durable dispatch lease reset on stop failed", {
|
|
202
|
+
channelId: channelId,
|
|
203
|
+
reason: errorMessage(err),
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
log.logInfoEvent("agent.turn.stop_requested", "Stop requested", { channelId: channelId });
|
|
207
|
+
}
|
|
208
|
+
return { pausedTaskId };
|
|
209
|
+
}
|
|
210
|
+
return { handleBusyMessage, handleStop, cancelRunningTurn };
|
|
211
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ChannelEvent, HumanEvent } from "../channel/channel-event.js";
|
|
2
|
+
import { type RuntimeCommandName } from "../commands/catalog.js";
|
|
3
|
+
import type { IngressDeps } from "./ingress-deps.js";
|
|
4
|
+
export declare function createCommandHandlers(deps: IngressDeps, resetChannelQueue: (channelId: string) => number): {
|
|
5
|
+
handleNewSession: (event: HumanEvent) => Promise<void>;
|
|
6
|
+
runRuntimeCommand: (event: ChannelEvent, name: RuntimeCommandName, args: string) => Promise<string>;
|
|
7
|
+
};
|