@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,77 +1,99 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* DingTalk routing metadata per channel (conversation id, DM vs group, sender), so a card or plain
|
|
3
|
+
* message can be routed after a restart. One file for every channel, `state/dingtalk/
|
|
4
|
+
* conversations.json`, keyed by channel id (spec 058, D17): loaded once, kept in memory, and
|
|
5
|
+
* rewritten only when an entry actually changes. Unbound channels are recorded too, so a later
|
|
6
|
+
* binding can push to them.
|
|
5
7
|
*/
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import { getChannelDir } from "../channel/channel-paths.js";
|
|
8
|
+
import { readFileSync } from "fs";
|
|
9
|
+
import { join } from "path";
|
|
9
10
|
import * as log from "../log.js";
|
|
10
11
|
import { writeFileAtomicallySync } from "../shared/atomic-file.js";
|
|
11
12
|
import { errorMessage } from "../shared/text-utils.js";
|
|
13
|
+
import { isPlainObject } from "../shared/type-guards.js";
|
|
14
|
+
function isNodeErrorCode(error, code) {
|
|
15
|
+
return isPlainObject(error) && error.code === code;
|
|
16
|
+
}
|
|
17
|
+
function parseMeta(value) {
|
|
18
|
+
if (!isPlainObject(value))
|
|
19
|
+
return undefined;
|
|
20
|
+
const { conversationId, conversationType, senderId } = value;
|
|
21
|
+
if (typeof conversationId !== "string" || typeof conversationType !== "string" || typeof senderId !== "string") {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
if (!conversationId || !conversationType || !senderId)
|
|
25
|
+
return undefined;
|
|
26
|
+
return { conversationId, conversationType, senderId };
|
|
27
|
+
}
|
|
28
|
+
function sameMeta(a, b) {
|
|
29
|
+
return (a !== undefined &&
|
|
30
|
+
a.conversationId === b.conversationId &&
|
|
31
|
+
a.conversationType === b.conversationType &&
|
|
32
|
+
a.senderId === b.senderId);
|
|
33
|
+
}
|
|
12
34
|
export class ConversationMetaStore {
|
|
13
|
-
constructor(
|
|
35
|
+
constructor(
|
|
36
|
+
/** `stateDir` is the app's `state/` directory; omit it to keep metadata in memory only. */
|
|
37
|
+
config,
|
|
14
38
|
/** Marks the channel as recently used, so the idle-cache sweep leaves it alone. */
|
|
15
39
|
touchChannel) {
|
|
16
40
|
this.config = config;
|
|
17
41
|
this.touchChannel = touchChannel;
|
|
18
|
-
|
|
19
|
-
this.
|
|
42
|
+
/** The last write failed: memory holds entries the file does not, so the next `set` writes even if unchanged. */
|
|
43
|
+
this.unsaved = false;
|
|
20
44
|
}
|
|
21
|
-
get(
|
|
22
|
-
this.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
45
|
+
get path() {
|
|
46
|
+
return this.config.stateDir ? join(this.config.stateDir, "dingtalk", "conversations.json") : undefined;
|
|
47
|
+
}
|
|
48
|
+
load() {
|
|
49
|
+
if (this.entries)
|
|
50
|
+
return this.entries;
|
|
51
|
+
const entries = new Map();
|
|
52
|
+
const path = this.path;
|
|
53
|
+
if (path) {
|
|
54
|
+
try {
|
|
55
|
+
const parsed = JSON.parse(readFileSync(path, "utf-8"));
|
|
56
|
+
for (const [channelId, value] of Object.entries(isPlainObject(parsed) ? parsed : {})) {
|
|
57
|
+
const meta = parseMeta(value);
|
|
58
|
+
if (meta)
|
|
59
|
+
entries.set(channelId, meta);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
if (!isNodeErrorCode(err, "ENOENT")) {
|
|
64
|
+
log.logWarn("dingtalk.metadata.load_failed", "Conversation metadata load failed", {
|
|
65
|
+
reason: errorMessage(err),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
34
68
|
}
|
|
35
|
-
const meta = {
|
|
36
|
-
conversationId: parsed.conversationId,
|
|
37
|
-
conversationType: parsed.conversationType,
|
|
38
|
-
senderId: parsed.senderId,
|
|
39
|
-
};
|
|
40
|
-
this.convMeta.set(channelId, meta);
|
|
41
|
-
return meta;
|
|
42
|
-
}
|
|
43
|
-
catch (err) {
|
|
44
|
-
log.logWarn("dingtalk.metadata.load_failed", "Conversation metadata load failed", {
|
|
45
|
-
channelId: channelId,
|
|
46
|
-
reason: errorMessage(err),
|
|
47
|
-
});
|
|
48
|
-
return null;
|
|
49
69
|
}
|
|
70
|
+
this.entries = entries;
|
|
71
|
+
return entries;
|
|
72
|
+
}
|
|
73
|
+
get(channelId) {
|
|
74
|
+
this.touchChannel(channelId);
|
|
75
|
+
return this.load().get(channelId) ?? null;
|
|
50
76
|
}
|
|
77
|
+
/** Called on every inbound message; writes only when the entry is new or changed. */
|
|
51
78
|
set(channelId, meta) {
|
|
52
79
|
this.touchChannel(channelId);
|
|
53
|
-
this.
|
|
54
|
-
|
|
55
|
-
|
|
80
|
+
const entries = this.load();
|
|
81
|
+
if (sameMeta(entries.get(channelId), meta) && !this.unsaved)
|
|
82
|
+
return;
|
|
83
|
+
entries.set(channelId, meta);
|
|
84
|
+
const path = this.path;
|
|
85
|
+
if (!path)
|
|
56
86
|
return;
|
|
57
87
|
try {
|
|
58
|
-
|
|
59
|
-
|
|
88
|
+
writeFileAtomicallySync(path, `${JSON.stringify(Object.fromEntries(entries), null, 2)}\n`);
|
|
89
|
+
this.unsaved = false;
|
|
60
90
|
}
|
|
61
91
|
catch (err) {
|
|
92
|
+
this.unsaved = true;
|
|
62
93
|
log.logWarn("dingtalk.metadata.persist_failed", "Conversation metadata persist failed", {
|
|
63
94
|
channelId: channelId,
|
|
64
95
|
reason: errorMessage(err),
|
|
65
96
|
});
|
|
66
97
|
}
|
|
67
98
|
}
|
|
68
|
-
/** Drop the in-memory entry only; the persisted file stays and is reloaded on demand. */
|
|
69
|
-
evict(channelId) {
|
|
70
|
-
this.convMeta.delete(channelId);
|
|
71
|
-
}
|
|
72
|
-
getPath(channelId) {
|
|
73
|
-
if (!this.config.stateDir)
|
|
74
|
-
return null;
|
|
75
|
-
return join(getChannelDir(this.config.stateDir, channelId), ".channel-meta.json");
|
|
76
|
-
}
|
|
77
99
|
}
|
|
@@ -1,18 +1,34 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The DingTalk implementation of the
|
|
3
|
-
*
|
|
2
|
+
* The DingTalk implementation of the turn output port (`ChannelContext`): it reads a turn's
|
|
3
|
+
* structured events and renders them as AI-Card progress, plain messages and a final answer,
|
|
4
|
+
* including the "card failed, send it plainly" fallbacks.
|
|
4
5
|
*/
|
|
5
|
-
import type { ChannelContext
|
|
6
|
-
import type {
|
|
7
|
-
import type {
|
|
8
|
-
import type { DingTalkBotPort } from "./handler.js";
|
|
6
|
+
import type { ChannelContext } from "../channel/channel-context.js";
|
|
7
|
+
import type { TurnInfo } from "../channel/transport.js";
|
|
8
|
+
import type { FinalDelivery, ProgressStyle } from "./response-mode.js";
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* `progressStyleOverride` lets the caller quiet a turn the user did not ask for. Background wakes
|
|
13
|
-
* (task driver, finished jobs, scheduled events) pass `"none"`: without it every autonomous step
|
|
14
|
-
* created an AI card in the human's conversation even when the check-in ended `[SILENT]`. The
|
|
15
|
-
* final answer is unaffected; it is delivered by `respondPlain`/`replaceMessage` either way, so a
|
|
16
|
-
* background turn that has something to say still says it.
|
|
10
|
+
* What a turn's delivery needs from the DingTalk side: the card surface and the plain-message
|
|
11
|
+
* send. `DingTalkBot` implements it; the shape stays structural so this file does not import it.
|
|
17
12
|
*/
|
|
18
|
-
export
|
|
13
|
+
export interface DeliveryPort {
|
|
14
|
+
readonly progressStyle: ProgressStyle;
|
|
15
|
+
readonly finalDelivery: FinalDelivery;
|
|
16
|
+
sendPlain(channelId: string, text: string, opts?: {
|
|
17
|
+
title?: string;
|
|
18
|
+
markdown?: boolean;
|
|
19
|
+
}): Promise<boolean>;
|
|
20
|
+
getCardGeneration(channelId: string): symbol;
|
|
21
|
+
isCardGenerationCurrent(channelId: string, generation: symbol): boolean;
|
|
22
|
+
replaceCard(channelId: string, content: string, finalize?: boolean, failed?: boolean, generation?: symbol): Promise<boolean>;
|
|
23
|
+
appendToCard(channelId: string, content: string, finalize?: boolean, failed?: boolean, generation?: symbol): Promise<boolean>;
|
|
24
|
+
finalizeCard(channelId: string, content: string, generation?: symbol): Promise<boolean>;
|
|
25
|
+
finalizeExistingCard(channelId: string, content: string, generation?: symbol): Promise<boolean>;
|
|
26
|
+
discardCard(channelId: string, generation?: symbol): void;
|
|
27
|
+
}
|
|
28
|
+
/** Records a delivered answer in the channel's conversation log. */
|
|
29
|
+
export type ArchiveBotResponse = (channelId: string, text: string) => Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* The output port for one turn. A `quiet` turn (a background wake) draws no progress; its answer is
|
|
32
|
+
* delivered the same way, so a wake that has something to say still says it.
|
|
33
|
+
*/
|
|
34
|
+
export declare function createDingTalkContext(turn: TurnInfo, port: DeliveryPort, archive: ArchiveBotResponse, quiet: boolean): ChannelContext;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import * as log from "../log.js";
|
|
2
2
|
import { formatElapsedZh } from "../shared/format.js";
|
|
3
3
|
import { errorMessage } from "../shared/text-utils.js";
|
|
4
|
+
import { describeToolCall } from "../tools/contract/presentation.js";
|
|
5
|
+
import { formatProgressEntry } from "./progress.js";
|
|
4
6
|
const MIN_UPDATE_INTERVAL_MS = 800;
|
|
5
7
|
const ROLLING_WINDOW_SIZE = 3;
|
|
6
8
|
const WAITING_TEXT = "正在处理…";
|
|
@@ -14,14 +16,15 @@ const PROGRESS_ENTRY_PREFIX = "- ";
|
|
|
14
16
|
// has its own timeout, so the sync loop is already bounded; this only guards against
|
|
15
17
|
// an unforeseen stall so run()'s finally can never hang the channel forever.
|
|
16
18
|
const FLUSH_DEADLINE_MS = 60_000;
|
|
19
|
+
/** How long a turn may stay silent before a "正在处理…" card appears. */
|
|
20
|
+
const CARD_WARMUP_MS = 3500;
|
|
17
21
|
class ChannelDeliveryController {
|
|
18
|
-
constructor(
|
|
19
|
-
/**
|
|
20
|
-
|
|
21
|
-
this.event = event;
|
|
22
|
+
constructor(turn, bot, archive,
|
|
23
|
+
/** A quiet turn (a background wake) shows no progress, whatever the channel is configured for. */
|
|
24
|
+
quiet) {
|
|
22
25
|
this.bot = bot;
|
|
23
|
-
this.
|
|
24
|
-
this.
|
|
26
|
+
this.archive = archive;
|
|
27
|
+
this.quiet = quiet;
|
|
25
28
|
this.progressSegments = [];
|
|
26
29
|
this.cachedProgressText = "";
|
|
27
30
|
this.progressTextDirty = false;
|
|
@@ -45,54 +48,167 @@ class ChannelDeliveryController {
|
|
|
45
48
|
this.sentProgressChars = 0;
|
|
46
49
|
this.replayRequired = false;
|
|
47
50
|
this.finalReplacementText = "";
|
|
48
|
-
|
|
51
|
+
/** Deliveries run one after another in the order the events arrived. */
|
|
52
|
+
this.chain = Promise.resolve();
|
|
53
|
+
this.lastAnswer = "";
|
|
54
|
+
this.event = { channelId: turn.channelId };
|
|
55
|
+
this.cardGeneration = bot.getCardGeneration(turn.channelId);
|
|
49
56
|
}
|
|
50
57
|
get isCurrent() {
|
|
51
58
|
return this.bot.isCardGenerationCurrent(this.event.channelId, this.cardGeneration);
|
|
52
59
|
}
|
|
53
|
-
/** The style this turn actually delivers with
|
|
60
|
+
/** The style this turn actually delivers with. */
|
|
54
61
|
get progressStyle() {
|
|
55
|
-
return this.
|
|
62
|
+
return this.quiet ? "none" : this.bot.progressStyle;
|
|
56
63
|
}
|
|
57
64
|
buildContext() {
|
|
58
65
|
return {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
ts: this.event.ts,
|
|
66
|
-
images: this.event.images,
|
|
67
|
-
},
|
|
68
|
-
// The group title when the transport supplied one; the raw id is the fallback so a
|
|
69
|
-
// synthetic event (which carries no conversation payload) still names its channel.
|
|
70
|
-
channelName: this.event.channelName ?? this.event.channelId,
|
|
71
|
-
respond: async (text, shouldLog = true) => this.appendProgress(text, shouldLog),
|
|
72
|
-
respondPlain: async (text, shouldLog = true, title) => this.sendFinal(text, shouldLog, title),
|
|
73
|
-
replaceMessage: async (text) => this.replaceWithFinal(text),
|
|
74
|
-
respondInThread: async (text) => {
|
|
75
|
-
if (this.closed || !this.isCurrent || !text.trim()) {
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
const delivered = await this.bot.sendPlain(this.event.channelId, text);
|
|
79
|
-
if (!delivered) {
|
|
80
|
-
log.logWarn("delivery.notice.failed", "Light notice delivery failed", {
|
|
81
|
-
channelId: this.event.channelId,
|
|
82
|
-
fields: { text: text.substring(0, 200) },
|
|
83
|
-
});
|
|
66
|
+
emit: (event) => this.enqueue(() => this.deliver(event), event.type),
|
|
67
|
+
finish: (outcome) => {
|
|
68
|
+
// /stop and /new end the turn at once: the card is closed and this turn's later writes
|
|
69
|
+
// are dropped now, not after whatever delivery is still in flight has finished.
|
|
70
|
+
if (outcome.kind === "cancelled") {
|
|
71
|
+
this.bot.discardCard(this.event.channelId, this.cardGeneration);
|
|
84
72
|
}
|
|
73
|
+
this.finished ??= this.enqueueFinish(outcome);
|
|
74
|
+
return this.finished;
|
|
85
75
|
},
|
|
86
|
-
setTyping: async (_isTyping) => { },
|
|
87
|
-
setWorking: async (_working) => { },
|
|
88
|
-
deleteMessage: async () => this.silence(),
|
|
89
|
-
primeCard: (delayMs) => this.primeCard(delayMs),
|
|
90
|
-
flush: async () => this.flush(),
|
|
91
|
-
close: async () => this.close(),
|
|
92
|
-
progressStyle: this.progressStyle,
|
|
93
|
-
finalDelivery: this.bot.finalDelivery,
|
|
94
76
|
};
|
|
95
77
|
}
|
|
78
|
+
enqueue(work, context) {
|
|
79
|
+
this.chain = this.chain.then(async () => {
|
|
80
|
+
try {
|
|
81
|
+
await work();
|
|
82
|
+
}
|
|
83
|
+
catch (err) {
|
|
84
|
+
log.logWarn("delivery.api.failed", "DingTalk API call failed", {
|
|
85
|
+
reason: errorMessage(err),
|
|
86
|
+
fields: { context },
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
async enqueueFinish(outcome) {
|
|
92
|
+
this.enqueue(() => this.conclude(outcome), "finish");
|
|
93
|
+
await this.chain;
|
|
94
|
+
}
|
|
95
|
+
/** Render one event; what the old pipeline did in its `respond*` calls, now chosen here. */
|
|
96
|
+
async deliver(event) {
|
|
97
|
+
switch (event.type) {
|
|
98
|
+
case "tool.start":
|
|
99
|
+
await this.appendProgress(formatProgressEntry("tool", describeToolCall(event.tool, event.args)), false, true);
|
|
100
|
+
return;
|
|
101
|
+
case "tool.update":
|
|
102
|
+
await this.appendProgress(formatProgressEntry("tool", event.text), false);
|
|
103
|
+
return;
|
|
104
|
+
case "tool.end":
|
|
105
|
+
if (event.failure)
|
|
106
|
+
await this.appendProgress(formatProgressEntry("error", event.failure), false);
|
|
107
|
+
return;
|
|
108
|
+
case "reasoning":
|
|
109
|
+
await this.appendProgress(formatProgressEntry("thinking", event.text), false);
|
|
110
|
+
return;
|
|
111
|
+
case "commentary":
|
|
112
|
+
await this.deliverCommentary(event.text, event.level);
|
|
113
|
+
return;
|
|
114
|
+
case "answer":
|
|
115
|
+
await this.deliverAnswer(event.text);
|
|
116
|
+
return;
|
|
117
|
+
case "compaction":
|
|
118
|
+
if (event.phase === "start") {
|
|
119
|
+
await this.appendProgress(formatProgressEntry("assistant", "正在压缩上下文…"), false);
|
|
120
|
+
}
|
|
121
|
+
return;
|
|
122
|
+
case "steer.applied":
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
async deliverCommentary(text, level) {
|
|
127
|
+
switch (level) {
|
|
128
|
+
case undefined:
|
|
129
|
+
await this.appendProgress(formatProgressEntry("assistant", text), false);
|
|
130
|
+
return;
|
|
131
|
+
case "error":
|
|
132
|
+
await this.appendProgress(formatProgressEntry("error", text), false);
|
|
133
|
+
return;
|
|
134
|
+
case "warning":
|
|
135
|
+
if (this.progressStyle !== "none") {
|
|
136
|
+
await this.appendProgress(formatProgressEntry("error", text), false);
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
await this.notice(`⚠️ ${text}`);
|
|
140
|
+
}
|
|
141
|
+
return;
|
|
142
|
+
case "notice":
|
|
143
|
+
await this.notice(text);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
async deliverAnswer(text) {
|
|
148
|
+
this.lastAnswer = text;
|
|
149
|
+
if (this.bot.finalDelivery === "card") {
|
|
150
|
+
await this.replaceWithFinal(text);
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
await this.sendFinal(text, true);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/** A standalone side message: not part of the progress, sent even when progress is off. */
|
|
157
|
+
async notice(text) {
|
|
158
|
+
if (this.closed || !this.isCurrent || !text.trim()) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const delivered = await this.bot.sendPlain(this.event.channelId, text);
|
|
162
|
+
if (!delivered) {
|
|
163
|
+
log.logWarn("delivery.notice.failed", "Light notice delivery failed", {
|
|
164
|
+
channelId: this.event.channelId,
|
|
165
|
+
fields: { text: text.substring(0, 200) },
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* The turn's end: whatever it settled on is made visible (or cleaned up), then the card is
|
|
171
|
+
* closed. An answer that never reached the user gets one more try as a card.
|
|
172
|
+
*/
|
|
173
|
+
async conclude(outcome) {
|
|
174
|
+
try {
|
|
175
|
+
switch (outcome.kind) {
|
|
176
|
+
case "failed":
|
|
177
|
+
try {
|
|
178
|
+
await this.replaceWithFinal(outcome.message ?? "");
|
|
179
|
+
}
|
|
180
|
+
catch (err) {
|
|
181
|
+
log.logWarn("delivery.error_reply.failed", "Error reply failed", {
|
|
182
|
+
channelId: this.event.channelId,
|
|
183
|
+
reason: errorMessage(err),
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
break;
|
|
187
|
+
case "silent":
|
|
188
|
+
await this.silence();
|
|
189
|
+
break;
|
|
190
|
+
case "cancelled":
|
|
191
|
+
break;
|
|
192
|
+
case "completed":
|
|
193
|
+
if (this.lastAnswer && !this.finalResponseDelivered) {
|
|
194
|
+
try {
|
|
195
|
+
await this.replaceWithFinal(this.lastAnswer);
|
|
196
|
+
}
|
|
197
|
+
catch (err) {
|
|
198
|
+
log.logWarn("delivery.final_reply.failed", "Final reply replacement failed", {
|
|
199
|
+
channelId: this.event.channelId,
|
|
200
|
+
reason: errorMessage(err),
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
await this.flush();
|
|
207
|
+
}
|
|
208
|
+
finally {
|
|
209
|
+
await this.close();
|
|
210
|
+
}
|
|
211
|
+
}
|
|
96
212
|
primeCard(delayMs) {
|
|
97
213
|
if (this.closed ||
|
|
98
214
|
this.ending ||
|
|
@@ -120,14 +236,14 @@ class ChannelDeliveryController {
|
|
|
120
236
|
}
|
|
121
237
|
}
|
|
122
238
|
archiveBotResponse(text) {
|
|
123
|
-
void this.
|
|
239
|
+
void this.archive(this.event.channelId, text).catch((err) => {
|
|
124
240
|
log.logWarn("channel.archive.write_failed", "Bot response archive failed", {
|
|
125
241
|
channelId: this.event.channelId,
|
|
126
242
|
reason: errorMessage(err),
|
|
127
243
|
});
|
|
128
244
|
});
|
|
129
245
|
}
|
|
130
|
-
async appendProgress(text, shouldLog) {
|
|
246
|
+
async appendProgress(text, shouldLog, isToolCall = false) {
|
|
131
247
|
if (this.closed || this.ending || !this.isCurrent || !text.trim())
|
|
132
248
|
return;
|
|
133
249
|
// Final-card-only mode shows no progress; ignore any stray progress writes
|
|
@@ -138,7 +254,7 @@ class ChannelDeliveryController {
|
|
|
138
254
|
if (this.progressStartedAt === 0) {
|
|
139
255
|
this.progressStartedAt = Date.now();
|
|
140
256
|
}
|
|
141
|
-
if (
|
|
257
|
+
if (isToolCall) {
|
|
142
258
|
this.toolCallCount++;
|
|
143
259
|
}
|
|
144
260
|
if (this.progressSegments.length > 0) {
|
|
@@ -170,18 +286,29 @@ class ChannelDeliveryController {
|
|
|
170
286
|
if (this.timer)
|
|
171
287
|
clearTimeout(this.timer);
|
|
172
288
|
this.timer = null;
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
//
|
|
176
|
-
|
|
177
|
-
|
|
289
|
+
if (this.cardWarmupTriggered && !this.getProgressText().trim()) {
|
|
290
|
+
// The card only ever said "正在处理…": the answer goes into it rather than into a second
|
|
291
|
+
// message under a card that would just read "已答复。".
|
|
292
|
+
this.finalReplacementText = text;
|
|
293
|
+
this.mode = "finalize-with-fallback";
|
|
294
|
+
this.bumpRevision(true);
|
|
295
|
+
await this.flush();
|
|
178
296
|
}
|
|
297
|
+
else {
|
|
298
|
+
if (!(await this.bot.sendPlain(this.event.channelId, text, title ? { title, markdown: true } : undefined))) {
|
|
299
|
+
return false;
|
|
300
|
+
}
|
|
301
|
+
this.finalResponseDelivered = true;
|
|
302
|
+
this.mode = "finalize-existing";
|
|
303
|
+
this.bumpRevision(true);
|
|
304
|
+
}
|
|
305
|
+
// Do not archive a response we failed to deliver: the conversation log
|
|
306
|
+
// must not claim we answered when the user never received it.
|
|
307
|
+
if (!this.finalResponseDelivered)
|
|
308
|
+
return false;
|
|
179
309
|
if (shouldLog) {
|
|
180
310
|
this.archiveBotResponse(text);
|
|
181
311
|
}
|
|
182
|
-
this.finalResponseDelivered = true;
|
|
183
|
-
this.mode = "finalize-existing";
|
|
184
|
-
this.bumpRevision(true);
|
|
185
312
|
return true;
|
|
186
313
|
}
|
|
187
314
|
async replaceWithFinal(text) {
|
|
@@ -433,14 +560,14 @@ class ChannelDeliveryController {
|
|
|
433
560
|
}
|
|
434
561
|
}
|
|
435
562
|
/**
|
|
436
|
-
*
|
|
437
|
-
*
|
|
438
|
-
* `progressStyleOverride` lets the caller quiet a turn the user did not ask for. Background wakes
|
|
439
|
-
* (task driver, finished jobs, scheduled events) pass `"none"`: without it every autonomous step
|
|
440
|
-
* created an AI card in the human's conversation even when the check-in ended `[SILENT]`. The
|
|
441
|
-
* final answer is unaffected; it is delivered by `respondPlain`/`replaceMessage` either way, so a
|
|
442
|
-
* background turn that has something to say still says it.
|
|
563
|
+
* The output port for one turn. A `quiet` turn (a background wake) draws no progress; its answer is
|
|
564
|
+
* delivered the same way, so a wake that has something to say still says it.
|
|
443
565
|
*/
|
|
444
|
-
export function createDingTalkContext(
|
|
445
|
-
|
|
566
|
+
export function createDingTalkContext(turn, port, archive, quiet) {
|
|
567
|
+
const controller = new ChannelDeliveryController(turn, port, archive, quiet);
|
|
568
|
+
const context = controller.buildContext();
|
|
569
|
+
// Same warm-up as before: a card that says "正在处理…" appears if the first output is slow.
|
|
570
|
+
if (!quiet)
|
|
571
|
+
controller.primeCard(CARD_WARMUP_MS);
|
|
572
|
+
return context;
|
|
446
573
|
}
|
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Pure parsing
|
|
2
|
+
* Pure parsing of DingTalk inbound image attachments (spec 049).
|
|
3
3
|
*
|
|
4
4
|
* Kept separate from `dingtalk.ts` — which owns the network calls (`downloadMessageFile`) and
|
|
5
|
-
* the per-channel serialization of the download — so the message-shape parsing
|
|
6
|
-
* rules can be unit tested without a `DingTalkBot` instance or any I/O.
|
|
5
|
+
* the per-channel serialization of the download — so the message-shape parsing rules can be unit tested without a `DingTalkBot` instance or any I/O.
|
|
7
6
|
*/
|
|
8
|
-
import { MAX_INBOUND_IMAGE_BYTES } from "../channel/channel-event.js";
|
|
9
|
-
/** Re-exported for `dingtalk.ts`'s convenience — see `channel-event.ts` for why the constant
|
|
10
|
-
* itself lives there rather than here. */
|
|
11
|
-
export { MAX_INBOUND_IMAGE_BYTES };
|
|
12
7
|
/** Structural subset of the raw DingTalk callback payload this module reads. */
|
|
13
8
|
export interface InboundMessagePayload {
|
|
14
9
|
msgtype?: string;
|
|
@@ -31,8 +26,6 @@ export interface ParsedInboundMessage {
|
|
|
31
26
|
/** In the same order as the `[图片N]` markers in `text`, one entry per image. */
|
|
32
27
|
downloadCodes: string[];
|
|
33
28
|
}
|
|
34
|
-
/** Defensive hard cap on how many images one inbound message can carry. */
|
|
35
|
-
export declare const MAX_INBOUND_IMAGES_PER_MESSAGE = 9;
|
|
36
29
|
/** Mirrors `MEDIA_UPLOAD_TIMEOUT_MS`: a multi-MB download over a slow link needs more than the
|
|
37
30
|
* 15s default HTTP timeout. */
|
|
38
31
|
export declare const MEDIA_DOWNLOAD_TIMEOUT_MS = 60000;
|
|
@@ -49,12 +42,3 @@ export declare const MEDIA_DOWNLOAD_TIMEOUT_MS = 60000;
|
|
|
49
42
|
* itself appends every image after all text once the turn is actually sent.
|
|
50
43
|
*/
|
|
51
44
|
export declare function parseInboundMessage(data: InboundMessagePayload): ParsedInboundMessage;
|
|
52
|
-
/**
|
|
53
|
-
* Identify a downloaded file's real format from its magic bytes rather than trusting the
|
|
54
|
-
* `Content-Type` DingTalk's CDN happens to answer with (spec 049 D8) — the four formats `read`
|
|
55
|
-
* and outbound `send_media` already treat as images. `null` means "downloaded fine, but not a
|
|
56
|
-
* recognized image format"; the caller still persists the bytes but never hands them to a model.
|
|
57
|
-
*/
|
|
58
|
-
export declare function sniffImageMimeType(data: Buffer): string | null;
|
|
59
|
-
/** File extension to persist a downloaded attachment under, from its sniffed MIME type. */
|
|
60
|
-
export declare function extensionForMimeType(mimeType: string | null): string;
|
|
@@ -1,16 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pure parsing + magic-byte sniffing for DingTalk inbound image attachments (spec 049).
|
|
3
|
-
*
|
|
4
|
-
* Kept separate from `dingtalk.ts` — which owns the network calls (`downloadMessageFile`) and
|
|
5
|
-
* the per-channel serialization of the download — so the message-shape parsing and the sniffing
|
|
6
|
-
* rules can be unit tested without a `DingTalkBot` instance or any I/O.
|
|
7
|
-
*/
|
|
8
|
-
import { MAX_INBOUND_IMAGE_BYTES } from "../channel/channel-event.js";
|
|
9
|
-
/** Re-exported for `dingtalk.ts`'s convenience — see `channel-event.ts` for why the constant
|
|
10
|
-
* itself lives there rather than here. */
|
|
11
|
-
export { MAX_INBOUND_IMAGE_BYTES };
|
|
12
|
-
/** Defensive hard cap on how many images one inbound message can carry. */
|
|
13
|
-
export const MAX_INBOUND_IMAGES_PER_MESSAGE = 9;
|
|
14
1
|
/** Mirrors `MEDIA_UPLOAD_TIMEOUT_MS`: a multi-MB download over a slow link needs more than the
|
|
15
2
|
* 15s default HTTP timeout. */
|
|
16
3
|
export const MEDIA_DOWNLOAD_TIMEOUT_MS = 60_000;
|
|
@@ -53,43 +40,3 @@ export function parseInboundMessage(data) {
|
|
|
53
40
|
}
|
|
54
41
|
return { text: "", downloadCodes: [] };
|
|
55
42
|
}
|
|
56
|
-
/**
|
|
57
|
-
* Identify a downloaded file's real format from its magic bytes rather than trusting the
|
|
58
|
-
* `Content-Type` DingTalk's CDN happens to answer with (spec 049 D8) — the four formats `read`
|
|
59
|
-
* and outbound `send_media` already treat as images. `null` means "downloaded fine, but not a
|
|
60
|
-
* recognized image format"; the caller still persists the bytes but never hands them to a model.
|
|
61
|
-
*/
|
|
62
|
-
export function sniffImageMimeType(data) {
|
|
63
|
-
if (data.length >= 3 && data[0] === 0xff && data[1] === 0xd8 && data[2] === 0xff) {
|
|
64
|
-
return "image/jpeg";
|
|
65
|
-
}
|
|
66
|
-
if (data.length >= 8 && data.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))) {
|
|
67
|
-
return "image/png";
|
|
68
|
-
}
|
|
69
|
-
if (data.length >= 6) {
|
|
70
|
-
const header = data.subarray(0, 6).toString("latin1");
|
|
71
|
-
if (header === "GIF87a" || header === "GIF89a")
|
|
72
|
-
return "image/gif";
|
|
73
|
-
}
|
|
74
|
-
if (data.length >= 12 &&
|
|
75
|
-
data.subarray(0, 4).toString("latin1") === "RIFF" &&
|
|
76
|
-
data.subarray(8, 12).toString("latin1") === "WEBP") {
|
|
77
|
-
return "image/webp";
|
|
78
|
-
}
|
|
79
|
-
return null;
|
|
80
|
-
}
|
|
81
|
-
/** File extension to persist a downloaded attachment under, from its sniffed MIME type. */
|
|
82
|
-
export function extensionForMimeType(mimeType) {
|
|
83
|
-
switch (mimeType) {
|
|
84
|
-
case "image/jpeg":
|
|
85
|
-
return "jpg";
|
|
86
|
-
case "image/png":
|
|
87
|
-
return "png";
|
|
88
|
-
case "image/gif":
|
|
89
|
-
return "gif";
|
|
90
|
-
case "image/webp":
|
|
91
|
-
return "webp";
|
|
92
|
-
default:
|
|
93
|
-
return "bin";
|
|
94
|
-
}
|
|
95
|
-
}
|