@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,14 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Inbound messages: acknowledge and deduplicate raw Stream frames, parse a robot message
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* Inbound messages: acknowledge and deduplicate raw Stream frames, parse a robot message, apply the
|
|
3
|
+
* sender allow-list, download image attachments, and hand the result to the runtime's ingress. The
|
|
4
|
+
* per-channel ingestion queue lives here so an attachment download can never be overtaken by a
|
|
5
|
+
* later message on the same channel.
|
|
6
6
|
*/
|
|
7
7
|
import type { DWClientDownStream } from "dingtalk-stream";
|
|
8
|
-
import type {
|
|
8
|
+
import type { InboundAttachment, Ingress } from "../channel/ingress.js";
|
|
9
9
|
import type { DingTalkConfig } from "./config.js";
|
|
10
10
|
import type { ConversationMetaStore } from "./conversation-meta.js";
|
|
11
|
-
import type { DingTalkEvent, DingTalkHandler } from "./handler.js";
|
|
12
11
|
import { type ParsedInboundMessage } from "./inbound-media.js";
|
|
13
12
|
interface DingTalkIncomingMessage {
|
|
14
13
|
msgId?: string;
|
|
@@ -29,19 +28,19 @@ interface DingTalkIncomingMessage {
|
|
|
29
28
|
downloadCode?: string;
|
|
30
29
|
};
|
|
31
30
|
}
|
|
31
|
+
/** What the DingTalk side needs from the runtime: the one door inbound messages go through. */
|
|
32
|
+
export type DingTalkIngress = Pick<Ingress, "receive">;
|
|
32
33
|
export interface InboundMessagesDeps {
|
|
33
34
|
config: DingTalkConfig;
|
|
34
|
-
|
|
35
|
+
ingress: DingTalkIngress;
|
|
35
36
|
conversations: ConversationMetaStore;
|
|
36
37
|
isStopped: () => boolean;
|
|
37
38
|
/** Acknowledge a downstream frame to DingTalk. */
|
|
38
39
|
ack: (messageId: string) => void;
|
|
39
|
-
|
|
40
|
+
collectAttachments: (parsed: ParsedInboundMessage) => Promise<{
|
|
40
41
|
text: string;
|
|
41
|
-
|
|
42
|
+
attachments: InboundAttachment[];
|
|
42
43
|
}>;
|
|
43
|
-
/** Command / busy / `/new` routing for a built event (the bot's `routeInboundEvent`). */
|
|
44
|
-
routeInboundEvent: (event: DingTalkEvent) => Promise<void>;
|
|
45
44
|
}
|
|
46
45
|
export declare class InboundMessages {
|
|
47
46
|
private deps;
|
package/dist/dingtalk/inbound.js
CHANGED
|
@@ -2,6 +2,7 @@ import * as log from "../log.js";
|
|
|
2
2
|
import { createSerialQueue } from "../shared/serial-queue.js";
|
|
3
3
|
import { errorMessage } from "../shared/text-utils.js";
|
|
4
4
|
import { isRecord } from "../shared/type-guards.js";
|
|
5
|
+
import { dingtalkDmChannelId, dingtalkGroupChannelId } from "./channel-ids.js";
|
|
5
6
|
import { parseInboundMessage } from "./inbound-media.js";
|
|
6
7
|
export class InboundMessages {
|
|
7
8
|
constructor(deps) {
|
|
@@ -64,7 +65,7 @@ export class InboundMessages {
|
|
|
64
65
|
return { status: "SUCCESS", message: "OK" };
|
|
65
66
|
}
|
|
66
67
|
async onStreamMessage(data) {
|
|
67
|
-
const { config,
|
|
68
|
+
const { config, ingress } = this.deps;
|
|
68
69
|
if (this.deps.isStopped()) {
|
|
69
70
|
return;
|
|
70
71
|
}
|
|
@@ -87,16 +88,15 @@ export class InboundMessages {
|
|
|
87
88
|
}
|
|
88
89
|
}
|
|
89
90
|
// Determine channel ID
|
|
90
|
-
const channelId = conversationType === "2" ?
|
|
91
|
-
// Parsing above is free;
|
|
92
|
-
// Serialize the whole "
|
|
93
|
-
//
|
|
94
|
-
//
|
|
95
|
-
// `enqueueStreamMessage` otherwise waits on anything asynchronous.
|
|
91
|
+
const channelId = conversationType === "2" ? dingtalkGroupChannelId(conversationId) : dingtalkDmChannelId(senderId);
|
|
92
|
+
// Parsing above is free; downloading image attachments below is a real network round-trip.
|
|
93
|
+
// Serialize the whole "download attachments → hand over" unit per channel (spec 049 D3):
|
|
94
|
+
// without this, a message needing a download can be overtaken, in delivery order, by a later
|
|
95
|
+
// download-free message on the same channel.
|
|
96
96
|
this.pendingInboundIngestCount++;
|
|
97
97
|
try {
|
|
98
98
|
await this.inboundIngestQueue.run(channelId, async () => {
|
|
99
|
-
const { text: content,
|
|
99
|
+
const { text: content, attachments } = await this.deps.collectAttachments(parsed);
|
|
100
100
|
log.logEvent("info", "runtime.dingtalk.message_received", "Accepted incoming message", {
|
|
101
101
|
ctx: { channelId, userName: senderName },
|
|
102
102
|
fields: { messageType: conversationType === "2" ? "group" : "dm", messageLength: content.length },
|
|
@@ -111,23 +111,16 @@ export class InboundMessages {
|
|
|
111
111
|
conversationType,
|
|
112
112
|
senderId,
|
|
113
113
|
});
|
|
114
|
-
|
|
115
|
-
// the path, so the channel index counts every human message and no synthetic wake.
|
|
116
|
-
handler.noteChannelActivity?.({ channelId, name: channelName, at: Date.now() });
|
|
117
|
-
// Build event
|
|
118
|
-
const event = {
|
|
119
|
-
type: conversationType === "2" ? "group" : "dm",
|
|
114
|
+
const message = {
|
|
120
115
|
channelId,
|
|
121
|
-
ts: Date.now().toString(),
|
|
122
116
|
user: senderId,
|
|
123
117
|
userName: senderName,
|
|
124
118
|
text: content,
|
|
125
|
-
|
|
126
|
-
conversationType,
|
|
119
|
+
ts: Date.now().toString(),
|
|
127
120
|
...(channelName ? { channelName } : {}),
|
|
128
|
-
...(
|
|
121
|
+
...(attachments.length > 0 ? { attachments } : {}),
|
|
129
122
|
};
|
|
130
|
-
await
|
|
123
|
+
await ingress.receive(message);
|
|
131
124
|
});
|
|
132
125
|
}
|
|
133
126
|
finally {
|
package/dist/dingtalk/media.d.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { InboundAttachment } from "../channel/ingress.js";
|
|
2
|
+
import type { MediaSendResult, OutboundMedia } from "../channel/transport.js";
|
|
3
3
|
import { type ConversationMeta } from "./api.js";
|
|
4
4
|
import type { DingTalkConfig } from "./config.js";
|
|
5
|
-
import type { DingTalkHandler } from "./handler.js";
|
|
6
5
|
import { type ParsedInboundMessage } from "./inbound-media.js";
|
|
7
6
|
export interface DingTalkMediaDeps {
|
|
8
7
|
config: DingTalkConfig;
|
|
9
|
-
handler: DingTalkHandler;
|
|
10
8
|
getAccessToken: () => Promise<string | null>;
|
|
11
9
|
getConversationMeta: (channelId: string) => ConversationMeta | null;
|
|
12
10
|
sendRobotMessage: (meta: ConversationMeta, msgKey: string, msgParam: string) => Promise<boolean>;
|
|
@@ -50,17 +48,17 @@ export declare class DingTalkMedia {
|
|
|
50
48
|
data: Buffer;
|
|
51
49
|
} | null>;
|
|
52
50
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
51
|
+
* Download one parsed message's images and say, in the text, what became of the ones that did
|
|
52
|
+
* not make it (spec 049 D8): a failed download, an oversized file, or more images than
|
|
53
|
+
* `MAX_INBOUND_IMAGES_PER_MESSAGE` allows. A failure on one image never drops the rest of the
|
|
54
|
+
* message — only that one `[图片N]` marker changes. The bytes of the others go to the runtime as
|
|
55
|
+
* attachments (it persists them under the bound desk and settles the remaining markers).
|
|
58
56
|
*
|
|
59
57
|
* No-op (no network call at all) when the message carries no image credentials, so a plain
|
|
60
58
|
* text message never pays for this method's existence.
|
|
61
59
|
*/
|
|
62
|
-
|
|
60
|
+
collectInboundAttachments(parsed: ParsedInboundMessage): Promise<{
|
|
63
61
|
text: string;
|
|
64
|
-
|
|
62
|
+
attachments: InboundAttachment[];
|
|
65
63
|
}>;
|
|
66
64
|
}
|
package/dist/dingtalk/media.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Media (image / file) for DingTalk: outbound delivery
|
|
3
|
-
*
|
|
2
|
+
* Media (image / file) for DingTalk: outbound delivery, and the two-step inbound download plus the
|
|
3
|
+
* per-message attachment collection built on it (spec 049).
|
|
4
4
|
*/
|
|
5
5
|
import axios from "axios";
|
|
6
|
+
import { MAX_INBOUND_IMAGE_BYTES, MAX_INBOUND_IMAGES_PER_MESSAGE } from "../channel/attachments.js";
|
|
6
7
|
import * as log from "../log.js";
|
|
7
8
|
import { errorMessage } from "../shared/text-utils.js";
|
|
8
9
|
import { isRecord } from "../shared/type-guards.js";
|
|
9
10
|
import { formatSize } from "../tools/truncate.js";
|
|
10
11
|
import { DINGTALK_API, DINGTALK_OAPI, http } from "./api.js";
|
|
11
|
-
import {
|
|
12
|
+
import { MEDIA_DOWNLOAD_TIMEOUT_MS } from "./inbound-media.js";
|
|
12
13
|
// DingTalk caps robot image messages at 1MB and file messages at 20MB. We reject
|
|
13
14
|
// oversized files before spending an upload round-trip on a request the API rejects.
|
|
14
15
|
const MAX_IMAGE_BYTES = 1024 * 1024;
|
|
@@ -17,12 +18,12 @@ const MAX_FILE_BYTES = 20 * 1024 * 1024;
|
|
|
17
18
|
const MEDIA_UPLOAD_TIMEOUT_MS = 60_000;
|
|
18
19
|
// File extensions the `sampleFile` message type accepts (per DingTalk docs).
|
|
19
20
|
const SUPPORTED_FILE_TYPES = new Set(["xlsx", "pdf", "zip", "rar", "doc", "docx", "ppt", "pptx", "csv", "txt"]);
|
|
20
|
-
/** Lowercased extension without the
|
|
21
|
+
/** Lowercased extension without the desk ("Report.PDF" → "pdf"); "" when none. */
|
|
21
22
|
function extractFileExtension(fileName) {
|
|
22
|
-
const
|
|
23
|
-
if (
|
|
23
|
+
const desk = fileName.lastIndexOf(".");
|
|
24
|
+
if (desk < 0 || desk === fileName.length - 1)
|
|
24
25
|
return "";
|
|
25
|
-
return fileName.slice(
|
|
26
|
+
return fileName.slice(desk + 1).toLowerCase();
|
|
26
27
|
}
|
|
27
28
|
export class DingTalkMedia {
|
|
28
29
|
constructor(deps) {
|
|
@@ -160,21 +161,21 @@ export class DingTalkMedia {
|
|
|
160
161
|
}
|
|
161
162
|
}
|
|
162
163
|
/**
|
|
163
|
-
*
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
*
|
|
167
|
-
*
|
|
164
|
+
* Download one parsed message's images and say, in the text, what became of the ones that did
|
|
165
|
+
* not make it (spec 049 D8): a failed download, an oversized file, or more images than
|
|
166
|
+
* `MAX_INBOUND_IMAGES_PER_MESSAGE` allows. A failure on one image never drops the rest of the
|
|
167
|
+
* message — only that one `[图片N]` marker changes. The bytes of the others go to the runtime as
|
|
168
|
+
* attachments (it persists them under the bound desk and settles the remaining markers).
|
|
168
169
|
*
|
|
169
170
|
* No-op (no network call at all) when the message carries no image credentials, so a plain
|
|
170
171
|
* text message never pays for this method's existence.
|
|
171
172
|
*/
|
|
172
|
-
async
|
|
173
|
+
async collectInboundAttachments(parsed) {
|
|
173
174
|
if (parsed.downloadCodes.length === 0) {
|
|
174
|
-
return { text: parsed.text,
|
|
175
|
+
return { text: parsed.text, attachments: [] };
|
|
175
176
|
}
|
|
176
177
|
let text = parsed.text;
|
|
177
|
-
const
|
|
178
|
+
const attachments = [];
|
|
178
179
|
const replaceMarker = (index, replacement) => {
|
|
179
180
|
text = text.replace(`[图片${index + 1}]`, replacement);
|
|
180
181
|
};
|
|
@@ -193,22 +194,8 @@ export class DingTalkMedia {
|
|
|
193
194
|
replaceMarker(index, `[图片${marker}:文件过大(${formatSize(downloaded.data.byteLength)}),已丢弃]`);
|
|
194
195
|
return;
|
|
195
196
|
}
|
|
196
|
-
|
|
197
|
-
const persisted = await this.deps.handler.persistInboundImage?.(channelId, {
|
|
198
|
-
data: downloaded.data,
|
|
199
|
-
mimeType,
|
|
200
|
-
});
|
|
201
|
-
if (!persisted) {
|
|
202
|
-
replaceMarker(index, `[图片${marker}:保存失败]`);
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
if (!mimeType) {
|
|
206
|
-
// Persisted for the record, but never handed to a model — see the handler
|
|
207
|
-
// contract's doc comment on `persistInboundImage`.
|
|
208
|
-
replaceMarker(index, `[图片${marker}:格式不支持,已保存]`);
|
|
209
|
-
}
|
|
210
|
-
images[index] = persisted;
|
|
197
|
+
attachments[index] = { kind: "image", index: marker, data: downloaded.data };
|
|
211
198
|
}));
|
|
212
|
-
return { text,
|
|
199
|
+
return { text, attachments: attachments.filter((attachment) => !!attachment) };
|
|
213
200
|
}
|
|
214
201
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a turn's events read as a DingTalk progress card: one line per entry, prefixed by what kind
|
|
3
|
+
* of entry it is (`➜` a tool call, `✦` thinking, `✕` an error, plain text for the model's own
|
|
4
|
+
* words).
|
|
5
|
+
*/
|
|
6
|
+
export type ProgressEntryKind = "tool" | "thinking" | "error" | "assistant";
|
|
7
|
+
/** `""` when there is nothing to show. */
|
|
8
|
+
export declare function formatProgressEntry(kind: ProgressEntryKind, text: string): string;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
function sanitizeProgressText(text) {
|
|
2
|
+
return text.replace(//g, "").replace(/\r/g, "").trim();
|
|
3
|
+
}
|
|
4
|
+
/** `""` when there is nothing to show. */
|
|
5
|
+
export function formatProgressEntry(kind, text) {
|
|
6
|
+
const cleaned = sanitizeProgressText(text);
|
|
7
|
+
if (!cleaned)
|
|
8
|
+
return "";
|
|
9
|
+
const normalized = cleaned.replace(/\n+/g, " ").trim();
|
|
10
|
+
switch (kind) {
|
|
11
|
+
case "tool":
|
|
12
|
+
return `➜ ${normalized}`;
|
|
13
|
+
case "thinking":
|
|
14
|
+
return `✦ ${normalized}`;
|
|
15
|
+
case "error":
|
|
16
|
+
return `✕ ${normalized}`;
|
|
17
|
+
case "assistant":
|
|
18
|
+
return normalized;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DingTalk's delivery presets. `channel.json`'s `dingtalk.responseMode` picks how a turn is shown;
|
|
3
|
+
* the two derived settings below are all the delivery code ever branches on.
|
|
4
|
+
*/
|
|
5
|
+
/** How progress is surfaced during a turn. */
|
|
6
|
+
export type ProgressStyle = "full" | "rolling" | "none";
|
|
7
|
+
/** How the final answer is delivered. */
|
|
8
|
+
export type FinalDelivery = "plain" | "card";
|
|
9
|
+
export type ResponseMode = "full_progress_then_plain_final" | "rolling_progress_then_plain_final" | "final_card_only";
|
|
10
|
+
export declare function progressStyleOf(mode: ResponseMode): ProgressStyle;
|
|
11
|
+
export declare function finalDeliveryOf(mode: ResponseMode): FinalDelivery;
|
|
12
|
+
export declare function isResponseModeConfig(value: unknown): value is ResponseMode;
|
|
13
|
+
export declare function normalizeResponseMode(value: unknown): ResponseMode;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DingTalk's delivery presets. `channel.json`'s `dingtalk.responseMode` picks how a turn is shown;
|
|
3
|
+
* the two derived settings below are all the delivery code ever branches on.
|
|
4
|
+
*/
|
|
5
|
+
const RESPONSE_MODE_VALUES = [
|
|
6
|
+
"full_progress_then_plain_final",
|
|
7
|
+
"rolling_progress_then_plain_final",
|
|
8
|
+
"final_card_only",
|
|
9
|
+
];
|
|
10
|
+
export function progressStyleOf(mode) {
|
|
11
|
+
if (mode === "final_card_only")
|
|
12
|
+
return "none";
|
|
13
|
+
if (mode === "rolling_progress_then_plain_final")
|
|
14
|
+
return "rolling";
|
|
15
|
+
return "full";
|
|
16
|
+
}
|
|
17
|
+
export function finalDeliveryOf(mode) {
|
|
18
|
+
return mode === "final_card_only" ? "card" : "plain";
|
|
19
|
+
}
|
|
20
|
+
export function isResponseModeConfig(value) {
|
|
21
|
+
return typeof value === "string" && RESPONSE_MODE_VALUES.includes(value);
|
|
22
|
+
}
|
|
23
|
+
export function normalizeResponseMode(value) {
|
|
24
|
+
if (value === undefined) {
|
|
25
|
+
return "full_progress_then_plain_final";
|
|
26
|
+
}
|
|
27
|
+
if (isResponseModeConfig(value)) {
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
throw new Error('Invalid `responseMode`: expected "full_progress_then_plain_final", "rolling_progress_then_plain_final", or "final_card_only".');
|
|
31
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The DingTalk transport (spec 061, D4/D27): the `Transport` the runtime holds. It composes the
|
|
3
|
+
* Stream bot (connection, inbound, cards, media, plain sends) with the per-turn delivery, and
|
|
4
|
+
* decides nothing about what a message means — that is the runtime's `Ingress`.
|
|
5
|
+
*/
|
|
6
|
+
import type { ChannelContext } from "../channel/channel-context.js";
|
|
7
|
+
import type { Ingress } from "../channel/ingress.js";
|
|
8
|
+
import type { MediaSendResult, OutboundMedia, PushMessage, Transport, TransportPolicy, TurnInfo } from "../channel/transport.js";
|
|
9
|
+
import type { ConversationKind } from "../desk/types.js";
|
|
10
|
+
import { DingTalkBot } from "./bot.js";
|
|
11
|
+
import type { DingTalkConfig } from "./config.js";
|
|
12
|
+
import { type ArchiveBotResponse } from "./delivery.js";
|
|
13
|
+
export interface DingTalkTransportDeps {
|
|
14
|
+
config: DingTalkConfig;
|
|
15
|
+
/** Records a delivered answer in the channel's conversation log (log.jsonl). */
|
|
16
|
+
archiveBotResponse: ArchiveBotResponse;
|
|
17
|
+
/** Tests replace the bot to run the real delivery against a fake DingTalk API. */
|
|
18
|
+
createBot?: (ingress: Pick<Ingress, "receive">, config: DingTalkConfig) => DingTalkBot;
|
|
19
|
+
}
|
|
20
|
+
export declare class DingTalkTransport implements Transport {
|
|
21
|
+
private readonly deps;
|
|
22
|
+
readonly name: "dingtalk";
|
|
23
|
+
readonly policy: TransportPolicy;
|
|
24
|
+
readonly bot: DingTalkBot;
|
|
25
|
+
private ingress;
|
|
26
|
+
private stopping;
|
|
27
|
+
constructor(deps: DingTalkTransportDeps);
|
|
28
|
+
private requireIngress;
|
|
29
|
+
/**
|
|
30
|
+
* Begin the Stream connection. Not awaited: the first connect has its own timeout and keeps
|
|
31
|
+
* retrying in the background, and a daemon whose DingTalk is briefly unreachable still boots.
|
|
32
|
+
*/
|
|
33
|
+
start(ingress: Ingress): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* Inbound stops with the Stream connection. Cards and plain sends go over HTTP and keep working,
|
|
36
|
+
* so there is nothing left for `stop` to close.
|
|
37
|
+
*/
|
|
38
|
+
stopAccepting(): Promise<void>;
|
|
39
|
+
stop(): Promise<void>;
|
|
40
|
+
openTurn(turn: TurnInfo, opts: {
|
|
41
|
+
quiet: boolean;
|
|
42
|
+
}): ChannelContext;
|
|
43
|
+
push(channelId: string, message: PushMessage): Promise<boolean>;
|
|
44
|
+
sendMedia(channelId: string, media: OutboundMedia): Promise<MediaSendResult>;
|
|
45
|
+
describe(channelId: string, knownName?: string): string;
|
|
46
|
+
conversationKindOf(channelId: string): ConversationKind | undefined;
|
|
47
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { DingTalkBot } from "./bot.js";
|
|
2
|
+
import { describeDingTalkChannel, parseDingTalkChannelId } from "./channel-ids.js";
|
|
3
|
+
import { createDingTalkContext } from "./delivery.js";
|
|
4
|
+
export class DingTalkTransport {
|
|
5
|
+
constructor(deps) {
|
|
6
|
+
this.deps = deps;
|
|
7
|
+
this.name = "dingtalk";
|
|
8
|
+
const ingress = {
|
|
9
|
+
receive: (message) => this.requireIngress().receive(message),
|
|
10
|
+
};
|
|
11
|
+
this.bot = deps.createBot ? deps.createBot(ingress, deps.config) : new DingTalkBot(ingress, deps.config);
|
|
12
|
+
this.policy = { whenBusy: this.bot.busyMessageDefault, textControlCommands: true };
|
|
13
|
+
}
|
|
14
|
+
requireIngress() {
|
|
15
|
+
if (!this.ingress)
|
|
16
|
+
throw new Error("DingTalk transport received a message before it was started");
|
|
17
|
+
return this.ingress;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Begin the Stream connection. Not awaited: the first connect has its own timeout and keeps
|
|
21
|
+
* retrying in the background, and a daemon whose DingTalk is briefly unreachable still boots.
|
|
22
|
+
*/
|
|
23
|
+
async start(ingress) {
|
|
24
|
+
this.ingress = ingress;
|
|
25
|
+
void this.bot.start();
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Inbound stops with the Stream connection. Cards and plain sends go over HTTP and keep working,
|
|
29
|
+
* so there is nothing left for `stop` to close.
|
|
30
|
+
*/
|
|
31
|
+
stopAccepting() {
|
|
32
|
+
this.stopping ??= this.bot.stop();
|
|
33
|
+
return this.stopping;
|
|
34
|
+
}
|
|
35
|
+
stop() {
|
|
36
|
+
return this.stopAccepting();
|
|
37
|
+
}
|
|
38
|
+
openTurn(turn, opts) {
|
|
39
|
+
const releaseConnection = this.bot.holdConnection();
|
|
40
|
+
const inner = createDingTalkContext(turn, this.bot, this.deps.archiveBotResponse, opts.quiet);
|
|
41
|
+
return {
|
|
42
|
+
emit: (event) => inner.emit(event),
|
|
43
|
+
finish: async (outcome) => {
|
|
44
|
+
try {
|
|
45
|
+
await inner.finish(outcome);
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
releaseConnection();
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
push(channelId, message) {
|
|
54
|
+
const opts = message.title !== undefined || message.markdown !== undefined
|
|
55
|
+
? {
|
|
56
|
+
...(message.title !== undefined ? { title: message.title } : {}),
|
|
57
|
+
...(message.markdown !== undefined ? { markdown: message.markdown } : {}),
|
|
58
|
+
}
|
|
59
|
+
: undefined;
|
|
60
|
+
return this.bot.sendPlain(channelId, message.text, opts);
|
|
61
|
+
}
|
|
62
|
+
sendMedia(channelId, media) {
|
|
63
|
+
return this.bot.sendMedia(channelId, media);
|
|
64
|
+
}
|
|
65
|
+
describe(channelId, knownName) {
|
|
66
|
+
return describeDingTalkChannel(channelId, knownName);
|
|
67
|
+
}
|
|
68
|
+
conversationKindOf(channelId) {
|
|
69
|
+
return parseDingTalkChannelId(channelId)?.kind;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -1,7 +1,12 @@
|
|
|
1
|
+
import type { ChannelDescription } from "../channel/channel-id.js";
|
|
1
2
|
export interface HandleEventsCommandOptions {
|
|
2
3
|
args: string;
|
|
3
|
-
|
|
4
|
+
/** The asking channel's desk: its `events/` and its history records are the whole scope. */
|
|
5
|
+
desk: string;
|
|
6
|
+
deskDir: string;
|
|
4
7
|
historyPath: string;
|
|
8
|
+
/** Names a delivery channel and says whether it is still bound to this desk. */
|
|
9
|
+
describeChannel: (channelId: string) => ChannelDescription;
|
|
5
10
|
}
|
|
6
11
|
type EventsCommand = {
|
|
7
12
|
action: "list";
|
|
@@ -18,7 +23,7 @@ type EventsCommand = {
|
|
|
18
23
|
/** Exported so `test/commands-subcommands.test.ts` can feed every broadcast example back through it. */
|
|
19
24
|
export declare function parseEventsCommand(args: string): EventsCommand;
|
|
20
25
|
export declare function normalizeEventName(name: string): string;
|
|
21
|
-
export declare function resolveEventPath(
|
|
26
|
+
export declare function resolveEventPath(deskDir: string, name: string): {
|
|
22
27
|
eventName: string;
|
|
23
28
|
eventPath: string;
|
|
24
29
|
};
|
package/dist/events/commands.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `/events`: list, show, delete and history of scheduled events, confined to
|
|
3
|
-
* the event history.
|
|
2
|
+
* `/events`: list, show, delete and history of one desk's scheduled events, confined to
|
|
3
|
+
* `<desk>/events/` and that desk's slice of the event history (spec 058, D14).
|
|
4
4
|
*/
|
|
5
5
|
import { existsSync } from "fs";
|
|
6
6
|
import { readdir, readFile, unlink } from "fs/promises";
|
|
@@ -44,8 +44,8 @@ export function parseEventsCommand(args) {
|
|
|
44
44
|
}
|
|
45
45
|
throw new Error(`未知子命令 \`${action}\`。`);
|
|
46
46
|
}
|
|
47
|
-
function eventsDir(
|
|
48
|
-
return join(
|
|
47
|
+
function eventsDir(deskDir) {
|
|
48
|
+
return join(deskDir, "events");
|
|
49
49
|
}
|
|
50
50
|
const SAFE_ID_OPTIONS = { suffix: ".json", pattern: EVENT_NAME_PATTERN, label: "event name" };
|
|
51
51
|
// English on purpose: shared with the model-facing `event_manage` tool (src/tools/event-manage.ts),
|
|
@@ -53,15 +53,20 @@ const SAFE_ID_OPTIONS = { suffix: ".json", pattern: EVENT_NAME_PATTERN, label: "
|
|
|
53
53
|
export function normalizeEventName(name) {
|
|
54
54
|
return normalizeSafeId(name, SAFE_ID_OPTIONS);
|
|
55
55
|
}
|
|
56
|
-
export function resolveEventPath(
|
|
57
|
-
const { id: eventName, path: eventPath } = resolveSafeIdPath(eventsDir(
|
|
56
|
+
export function resolveEventPath(deskDir, name) {
|
|
57
|
+
const { id: eventName, path: eventPath } = resolveSafeIdPath(eventsDir(deskDir), name, SAFE_ID_OPTIONS);
|
|
58
58
|
return { eventName, eventPath };
|
|
59
59
|
}
|
|
60
60
|
function clipText(text, maxChars = TEXT_PREVIEW_MAX_CHARS) {
|
|
61
61
|
return sharedClipText(text, maxChars, { headRatio: 1, omitHint: "...", collapseWhitespace: true });
|
|
62
62
|
}
|
|
63
|
-
function formatEventSummary(name, event) {
|
|
64
|
-
const
|
|
63
|
+
function formatEventSummary(name, event, describeChannel) {
|
|
64
|
+
const channel = describeChannel(event.channelId);
|
|
65
|
+
const lines = [
|
|
66
|
+
`**${name}**`,
|
|
67
|
+
`- 类型:${event.type}`,
|
|
68
|
+
`- 投递频道(channelId):${channel.label}${channel.boundHere ? "" : "(已暂停:投递频道未绑定到本工位)"}`,
|
|
69
|
+
];
|
|
65
70
|
if (event.type === "one-shot") {
|
|
66
71
|
lines.push(`- 触发时间:${event.at}`);
|
|
67
72
|
}
|
|
@@ -96,8 +101,8 @@ function describeEventProblem(error, name) {
|
|
|
96
101
|
return `定义无效,用 \`/events show ${name}\` 查看`;
|
|
97
102
|
}
|
|
98
103
|
const EMPTY_EVENTS = "暂无定时事件。\n在对话里告诉我要定时做什么,我会创建事件。";
|
|
99
|
-
async function listEvents(
|
|
100
|
-
const dir = eventsDir(
|
|
104
|
+
async function listEvents(options) {
|
|
105
|
+
const dir = eventsDir(options.deskDir);
|
|
101
106
|
if (!existsSync(dir))
|
|
102
107
|
return EMPTY_EVENTS;
|
|
103
108
|
const filenames = (await readdir(dir)).filter((filename) => filename.endsWith(".json")).sort();
|
|
@@ -112,7 +117,7 @@ async function listEvents(workspaceDir) {
|
|
|
112
117
|
// The same definition rules the watcher applies, so "listed as fine" means "will run".
|
|
113
118
|
// The lead-time rule is creation-only (an overdue one-shot is a legitimate recovery).
|
|
114
119
|
validateScheduledEvent(event, { enforceOneShotLead: false });
|
|
115
|
-
blocks.push(formatEventSummary(name, event));
|
|
120
|
+
blocks.push(formatEventSummary(name, event, options.describeChannel));
|
|
116
121
|
}
|
|
117
122
|
catch (error) {
|
|
118
123
|
blocks.push(`**${name}**\n- ⚠️ 无效:${describeEventProblem(error, name)}`);
|
|
@@ -125,8 +130,8 @@ async function listEvents(workspaceDir) {
|
|
|
125
130
|
function eventNotFound(eventName) {
|
|
126
131
|
return `未找到事件 \`${eventName}\`。用 \`/events list\` 查看。`;
|
|
127
132
|
}
|
|
128
|
-
async function showEvent(
|
|
129
|
-
const { eventName, eventPath } = resolveEventPath(
|
|
133
|
+
async function showEvent(options, name) {
|
|
134
|
+
const { eventName, eventPath } = resolveEventPath(options.deskDir, name);
|
|
130
135
|
if (!existsSync(eventPath))
|
|
131
136
|
return eventNotFound(eventName);
|
|
132
137
|
const raw = await readFile(eventPath, "utf-8");
|
|
@@ -138,12 +143,20 @@ async function showEvent(workspaceDir, name) {
|
|
|
138
143
|
// Show the bytes as they are: an unparseable file is exactly the case the user needs to see.
|
|
139
144
|
body = raw;
|
|
140
145
|
}
|
|
141
|
-
|
|
146
|
+
let status = "";
|
|
147
|
+
try {
|
|
148
|
+
const channel = options.describeChannel(parseScheduledEventContent(raw, `${eventName}.json`).channelId);
|
|
149
|
+
status = `- 投递频道:${channel.label}${channel.boundHere ? "" : "(已暂停:投递频道未绑定到本工位)"}\n\n`;
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
// An unparseable file has no channel to describe; the raw body below says why.
|
|
153
|
+
}
|
|
154
|
+
return capReply(`**事件 ${eventName}**\n\n${status}\`\`\`json\n${body}\n\`\`\``, {
|
|
142
155
|
nextStepHint: `完整文件在 \`${eventPath}\``,
|
|
143
156
|
}).text;
|
|
144
157
|
}
|
|
145
|
-
async function deleteEvent(
|
|
146
|
-
const { eventName, eventPath } = resolveEventPath(
|
|
158
|
+
async function deleteEvent(options, name) {
|
|
159
|
+
const { eventName, eventPath } = resolveEventPath(options.deskDir, name);
|
|
147
160
|
if (!existsSync(eventPath))
|
|
148
161
|
return eventNotFound(eventName);
|
|
149
162
|
// Read before deleting so the reply carries what was removed (review 2026-08-24 §3.4):
|
|
@@ -151,7 +164,7 @@ async function deleteEvent(workspaceDir, name) {
|
|
|
151
164
|
let summary;
|
|
152
165
|
try {
|
|
153
166
|
const event = parseScheduledEventContent(await readFile(eventPath, "utf-8"), `${eventName}.json`);
|
|
154
|
-
summary = formatEventSummary(eventName, event);
|
|
167
|
+
summary = formatEventSummary(eventName, event, options.describeChannel);
|
|
155
168
|
}
|
|
156
169
|
catch {
|
|
157
170
|
// Unparseable content is still safe to delete; just skip the summary.
|
|
@@ -203,7 +216,11 @@ function formatHistoryRecord(record) {
|
|
|
203
216
|
bits.push(`详情:\`${clipText(record.reason.replace(/`/g, "'"))}\``);
|
|
204
217
|
return `- ${bits.join(" · ")}`;
|
|
205
218
|
}
|
|
206
|
-
|
|
219
|
+
function belongsToDesk(record, options) {
|
|
220
|
+
return record.desk === options.desk;
|
|
221
|
+
}
|
|
222
|
+
async function showHistory(options, name) {
|
|
223
|
+
const { historyPath } = options;
|
|
207
224
|
const eventName = name ? normalizeEventName(name) : undefined;
|
|
208
225
|
const empty = eventName
|
|
209
226
|
? `暂无事件 \`${eventName}\` 的触发记录。\n事件触发后会记录在这里。`
|
|
@@ -217,6 +234,7 @@ async function showHistory(historyPath, name) {
|
|
|
217
234
|
.filter(Boolean)
|
|
218
235
|
.map(parseHistoryLine)
|
|
219
236
|
.filter((record) => !!record)
|
|
237
|
+
.filter((record) => belongsToDesk(record, options))
|
|
220
238
|
.filter((record) => !eventName || record.eventName === eventName)
|
|
221
239
|
.slice(-DEFAULT_HISTORY_LIMIT)
|
|
222
240
|
.reverse();
|
|
@@ -239,13 +257,13 @@ export async function handleEventsCommand(options) {
|
|
|
239
257
|
try {
|
|
240
258
|
switch (command.action) {
|
|
241
259
|
case "list":
|
|
242
|
-
return await listEvents(options
|
|
260
|
+
return await listEvents(options);
|
|
243
261
|
case "show":
|
|
244
|
-
return await showEvent(options
|
|
262
|
+
return await showEvent(options, command.name);
|
|
245
263
|
case "delete":
|
|
246
|
-
return await deleteEvent(options
|
|
264
|
+
return await deleteEvent(options, command.name);
|
|
247
265
|
case "history":
|
|
248
|
-
return await showHistory(options
|
|
266
|
+
return await showHistory(options, command.name);
|
|
249
267
|
}
|
|
250
268
|
}
|
|
251
269
|
catch (error) {
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Why an event's pre-action gate did not let the occurrence through: it blocked it, or it failed. */
|
|
2
|
+
export declare class EventPreActionError extends Error {
|
|
3
|
+
readonly kind: "blocked" | "failed";
|
|
4
|
+
readonly exitCode?: number;
|
|
5
|
+
readonly durationMs?: number;
|
|
6
|
+
constructor(kind: "blocked" | "failed", message: string, options?: {
|
|
7
|
+
exitCode?: number;
|
|
8
|
+
durationMs?: number;
|
|
9
|
+
});
|
|
10
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Why an event's pre-action gate did not let the occurrence through: it blocked it, or it failed. */
|
|
2
|
+
export class EventPreActionError extends Error {
|
|
3
|
+
constructor(kind, message, options = {}) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = "EventPreActionError";
|
|
6
|
+
this.kind = kind;
|
|
7
|
+
this.exitCode = options.exitCode;
|
|
8
|
+
this.durationMs = options.durationMs;
|
|
9
|
+
}
|
|
10
|
+
}
|