@poolzin/pool-bot 2026.2.24 → 2026.2.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -0
- package/dist/acp/client.js +207 -18
- package/dist/acp/event-mapper.js +87 -22
- package/dist/acp/meta.js +12 -6
- package/dist/acp/secret-file.js +22 -0
- package/dist/agents/agent-paths.js +8 -9
- package/dist/agents/agent-scope.js +17 -5
- package/dist/agents/auth-profiles/oauth.js +148 -64
- package/dist/agents/auth-profiles/session-override.js +13 -7
- package/dist/agents/bash-process-registry.test-helpers.js +29 -0
- package/dist/agents/bash-tools.exec-approval-request.js +20 -0
- package/dist/agents/bash-tools.exec-host-gateway.js +240 -0
- package/dist/agents/bash-tools.exec-host-node.js +235 -0
- package/dist/agents/bash-tools.exec-runtime.js +2 -25
- package/dist/agents/bash-tools.exec-types.js +1 -0
- package/dist/agents/bash-tools.process.js +224 -218
- package/dist/agents/bedrock-discovery.js +3 -1
- package/dist/agents/byteplus-models.js +97 -0
- package/dist/agents/chutes-oauth.js +1 -0
- package/dist/agents/cli-runner/helpers.js +4 -0
- package/dist/agents/compaction.js +41 -14
- package/dist/agents/content-blocks.js +16 -0
- package/dist/agents/doubao-models.js +121 -0
- package/dist/agents/failover-error.js +2 -0
- package/dist/agents/huggingface-models.js +5 -3
- package/dist/agents/live-model-filter.js +5 -0
- package/dist/agents/minimax-vlm.js +10 -8
- package/dist/agents/model-auth.js +6 -0
- package/dist/agents/model-catalog.js +3 -1
- package/dist/agents/model-fallback.js +96 -101
- package/dist/agents/model-selection.js +7 -1
- package/dist/agents/models-config.providers.js +364 -165
- package/dist/agents/ollama-stream.js +117 -4
- package/dist/agents/opencode-zen-models.js +22 -11
- package/dist/agents/pi-embedded-helpers/errors.js +55 -33
- package/dist/agents/pi-embedded-helpers/messaging-dedupe.js +10 -5
- package/dist/agents/pi-embedded-helpers/thinking.js +10 -5
- package/dist/agents/pi-embedded-helpers.js +1 -1
- package/dist/agents/pi-embedded-payloads.js +1 -0
- package/dist/agents/pi-embedded-runner/compact.js +29 -7
- package/dist/agents/pi-embedded-runner/extensions.js +28 -26
- package/dist/agents/pi-embedded-runner/google.js +20 -8
- package/dist/agents/pi-embedded-runner/run/attempt.js +95 -36
- package/dist/agents/pi-embedded-runner/run.js +71 -12
- package/dist/agents/pi-embedded-runner/run.overflow-compaction.fixture.js +34 -0
- package/dist/agents/pi-embedded-runner/run.overflow-compaction.mocks.shared.js +11 -2
- package/dist/agents/pi-embedded-runner/session-manager-cache.js +11 -7
- package/dist/agents/pi-embedded-runner/system-prompt.js +2 -0
- package/dist/agents/pi-embedded-runner/thinking.js +42 -0
- package/dist/agents/pi-embedded-runner/tool-name-allowlist.js +19 -0
- package/dist/agents/pi-embedded-runner/utils.js +7 -10
- package/dist/agents/pi-embedded-subscribe.handlers.lifecycle.js +45 -56
- package/dist/agents/pi-embedded-subscribe.handlers.tools.js +2 -2
- package/dist/agents/pi-embedded-subscribe.js +9 -4
- package/dist/agents/pi-embedded-subscribe.tools.js +68 -14
- package/dist/agents/pi-embedded-utils.js +3 -0
- package/dist/agents/pi-extensions/compaction-safeguard-runtime.js +4 -20
- package/dist/agents/pi-extensions/compaction-safeguard.js +75 -33
- package/dist/agents/pi-settings.js +40 -0
- package/dist/agents/pi-tools.policy.js +2 -1
- package/dist/agents/provider/config-loader.js +1 -1
- package/dist/agents/sandbox/browser.js +170 -33
- package/dist/agents/sandbox/config-hash.js +14 -27
- package/dist/agents/sandbox/config.js +21 -2
- package/dist/agents/sandbox/constants.js +2 -0
- package/dist/agents/sandbox/docker.js +16 -2
- package/dist/agents/sandbox/novnc-auth.js +62 -0
- package/dist/agents/sandbox/sanitize-env-vars.js +1 -1
- package/dist/agents/sandbox/shared.js +10 -6
- package/dist/agents/sandbox-paths.js +24 -11
- package/dist/agents/schema/clean-for-gemini.js +132 -85
- package/dist/agents/session-slug.js +10 -5
- package/dist/agents/session-tool-result-guard-wrapper.js +1 -0
- package/dist/agents/session-tool-result-guard.js +3 -1
- package/dist/agents/session-transcript-repair.js +40 -6
- package/dist/agents/skills/bundled-dir.js +19 -5
- package/dist/agents/skills/env-overrides.js +124 -43
- package/dist/agents/skills/frontmatter.js +6 -6
- package/dist/agents/skills/plugin-skills.js +14 -7
- package/dist/agents/skills/workspace.js +1 -0
- package/dist/agents/skills.test-helpers.js +13 -0
- package/dist/agents/stable-stringify.js +12 -0
- package/dist/agents/subagent-announce.js +251 -49
- package/dist/agents/subagent-lifecycle-events.js +19 -0
- package/dist/agents/subagent-registry-cleanup.js +31 -0
- package/dist/agents/subagent-registry-completion.js +68 -0
- package/dist/agents/subagent-registry-queries.js +117 -0
- package/dist/agents/subagent-registry-state.js +46 -0
- package/dist/agents/subagent-registry.js +252 -221
- package/dist/agents/subagent-registry.mocks.shared.js +12 -0
- package/dist/agents/subagent-registry.store.js +1 -0
- package/dist/agents/subagent-registry.types.js +1 -0
- package/dist/agents/subagent-spawn.js +195 -7
- package/dist/agents/system-prompt.js +22 -6
- package/dist/agents/test-helpers/assistant-message-fixtures.js +29 -0
- package/dist/agents/test-helpers/fast-coding-tools.js +1 -18
- package/dist/agents/test-helpers/fast-core-tools.js +1 -17
- package/dist/agents/test-helpers/pi-tools-sandbox-context.js +27 -0
- package/dist/agents/timeout.js +18 -6
- package/dist/agents/tool-call-id.js +1 -1
- package/dist/agents/tool-display-common.js +162 -29
- package/dist/agents/tool-images.js +82 -9
- package/dist/agents/tool-policy-shared.js +108 -0
- package/dist/agents/tool-policy.js +51 -26
- package/dist/agents/tools/browser-tool.js +160 -54
- package/dist/agents/tools/canvas-tool.js +27 -1
- package/dist/agents/tools/common.js +45 -0
- package/dist/agents/tools/cron-tool.test-helpers.js +12 -0
- package/dist/agents/tools/discord-actions-guild.js +4 -1
- package/dist/agents/tools/discord-actions-moderation-shared.js +27 -0
- package/dist/agents/tools/gateway-tool.js +3 -1
- package/dist/agents/tools/image-tool.js +214 -99
- package/dist/agents/tools/nodes-utils.js +1 -10
- package/dist/agents/tools/sessions-history-tool.js +140 -108
- package/dist/agents/tools/sessions-send-helpers.js +12 -6
- package/dist/agents/tools/sessions-spawn-tool.js +8 -2
- package/dist/agents/tools/subagents-tool.js +2 -1
- package/dist/agents/tools/whatsapp-actions.js +10 -2
- package/dist/agents/tools/whatsapp-target-auth.js +18 -0
- package/dist/agents/transcript-policy.js +22 -8
- package/dist/agents/venice-models.js +11 -3
- package/dist/agents/workspace.js +222 -46
- package/dist/auto-reply/commands-registry.data.js +51 -0
- package/dist/auto-reply/commands-registry.js +19 -21
- package/dist/auto-reply/fallback-state.js +114 -0
- package/dist/auto-reply/group-activation.js +10 -5
- package/dist/auto-reply/inbound-debounce.js +10 -5
- package/dist/auto-reply/model-runtime.js +68 -0
- package/dist/auto-reply/reply/abort.js +1 -1
- package/dist/auto-reply/reply/agent-runner-execution.js +40 -5
- package/dist/auto-reply/reply/agent-runner.js +165 -39
- package/dist/auto-reply/reply/bash-command.js +41 -39
- package/dist/auto-reply/reply/command-gates.js +25 -0
- package/dist/auto-reply/reply/commands-allowlist.js +111 -72
- package/dist/auto-reply/reply/commands-bash.js +6 -5
- package/dist/auto-reply/reply/commands-config.js +30 -28
- package/dist/auto-reply/reply/commands-core.js +2 -1
- package/dist/auto-reply/reply/commands-info.js +1 -0
- package/dist/auto-reply/reply/commands-models.js +65 -14
- package/dist/auto-reply/reply/commands-session.js +237 -82
- package/dist/auto-reply/reply/commands-setunset-standard.js +13 -0
- package/dist/auto-reply/reply/commands-setunset.js +45 -0
- package/dist/auto-reply/reply/commands-subagents/action-agents.js +44 -0
- package/dist/auto-reply/reply/commands-subagents/action-focus.js +64 -0
- package/dist/auto-reply/reply/commands-subagents/action-help.js +4 -0
- package/dist/auto-reply/reply/commands-subagents/action-info.js +45 -0
- package/dist/auto-reply/reply/commands-subagents/action-kill.js +60 -0
- package/dist/auto-reply/reply/commands-subagents/action-list.js +44 -0
- package/dist/auto-reply/reply/commands-subagents/action-log.js +29 -0
- package/dist/auto-reply/reply/commands-subagents/action-send.js +119 -0
- package/dist/auto-reply/reply/commands-subagents/action-spawn.js +52 -0
- package/dist/auto-reply/reply/commands-subagents/action-unfocus.js +30 -0
- package/dist/auto-reply/reply/commands-subagents/shared.js +303 -0
- package/dist/auto-reply/reply/commands-subagents.js +51 -587
- package/dist/auto-reply/reply/commands-tts.js +10 -5
- package/dist/auto-reply/reply/config-value.js +10 -5
- package/dist/auto-reply/reply/directive-handling.model-picker.js +12 -6
- package/dist/auto-reply/reply/directive-handling.persist.js +9 -21
- package/dist/auto-reply/reply/directive-handling.shared.js +24 -4
- package/dist/auto-reply/reply/followup-runner.js +1 -0
- package/dist/auto-reply/reply/get-reply-directives-utils.js +23 -14
- package/dist/auto-reply/reply/get-reply-directives.js +17 -28
- package/dist/auto-reply/reply/get-reply-inline-actions.js +1 -0
- package/dist/auto-reply/reply/get-reply.js +71 -12
- package/dist/auto-reply/reply/model-selection.js +80 -39
- package/dist/auto-reply/reply/queue/enqueue.js +10 -5
- package/dist/auto-reply/reply/queue/state.js +13 -12
- package/dist/auto-reply/reply/reply-payloads.js +67 -36
- package/dist/auto-reply/reply/reply-reference.js +9 -8
- package/dist/auto-reply/reply/route-reply.js +15 -8
- package/dist/auto-reply/reply/session-reset-prompt.js +1 -1
- package/dist/auto-reply/reply/session.js +22 -6
- package/dist/auto-reply/reply/strip-inbound-meta.js +147 -0
- package/dist/auto-reply/reply/subagents-utils.js +56 -30
- package/dist/auto-reply/reply/typing.js +46 -21
- package/dist/auto-reply/send-policy.js +14 -7
- package/dist/auto-reply/status.js +140 -16
- package/dist/auto-reply/templating.js +10 -5
- package/dist/auto-reply/thinking.js +7 -16
- package/dist/auto-reply/tokens.js +21 -5
- package/dist/browser/bridge-server.js +36 -20
- package/dist/browser/cdp.helpers.js +7 -14
- package/dist/browser/cdp.js +35 -15
- package/dist/browser/chrome.profile-decoration.js +7 -4
- package/dist/browser/config.js +30 -0
- package/dist/browser/extension-relay-auth.js +55 -0
- package/dist/browser/extension-relay.js +74 -29
- package/dist/browser/navigation-guard.js +39 -0
- package/dist/browser/paths.js +77 -0
- package/dist/browser/profiles.js +13 -8
- package/dist/browser/pw-ai-module.js +10 -5
- package/dist/browser/pw-session.js +76 -39
- package/dist/browser/pw-tools-core.interactions.js +14 -7
- package/dist/browser/pw-tools-core.state.js +12 -6
- package/dist/browser/routes/agent.act.js +431 -424
- package/dist/browser/routes/agent.shared.js +47 -3
- package/dist/browser/routes/agent.snapshot.js +122 -116
- package/dist/browser/routes/agent.storage.js +303 -297
- package/dist/browser/routes/tabs.js +154 -100
- package/dist/browser/server-context.js +7 -0
- package/dist/browser/server-lifecycle.js +37 -0
- package/dist/build-info.json +3 -3
- package/dist/channels/allow-from.js +26 -0
- package/dist/channels/allowlists/resolve-utils.js +43 -19
- package/dist/channels/channel-config.js +14 -7
- package/dist/channels/draft-stream-loop.js +7 -0
- package/dist/channels/model-overrides.js +82 -0
- package/dist/channels/plugins/account-action-gate.js +13 -0
- package/dist/channels/plugins/message-actions.js +10 -0
- package/dist/channels/plugins/normalize/imessage.js +14 -7
- package/dist/channels/plugins/normalize/slack.js +10 -5
- package/dist/channels/plugins/normalize/telegram.js +14 -7
- package/dist/channels/plugins/outbound/discord.js +80 -8
- package/dist/channels/plugins/outbound/signal.js +11 -11
- package/dist/channels/plugins/setup-helpers.js +10 -5
- package/dist/channels/sender-label.js +14 -7
- package/dist/channels/session.js +4 -2
- package/dist/channels/status-reactions.js +297 -0
- package/dist/channels/telegram/api.js +18 -0
- package/dist/cli/argv.js +84 -21
- package/dist/cli/banner.js +3 -2
- package/dist/cli/browser-cli-actions-input/register.files-downloads.js +65 -56
- package/dist/cli/cli-name.js +11 -11
- package/dist/cli/cli-utils.js +13 -3
- package/dist/cli/command-format.js +1 -1
- package/dist/cli/config-cli.js +1 -1
- package/dist/cli/daemon-cli/lifecycle-core.js +31 -19
- package/dist/cli/daemon-cli/lifecycle.js +64 -2
- package/dist/cli/daemon-cli/restart-health.js +126 -0
- package/dist/cli/daemon-cli/status.gather.js +9 -13
- package/dist/cli/daemon-cli/status.print.js +2 -10
- package/dist/cli/deps.js +27 -22
- package/dist/cli/exec-approvals-cli.js +92 -124
- package/dist/cli/gateway-cli/run-loop.js +23 -5
- package/dist/cli/memory-cli.js +158 -61
- package/dist/cli/node-cli/register.js +14 -5
- package/dist/cli/nodes-cli/register.push.js +63 -0
- package/dist/cli/nodes-media-utils.js +26 -0
- package/dist/cli/outbound-send-deps.js +2 -9
- package/dist/cli/outbound-send-mapping.js +11 -0
- package/dist/cli/pairing-cli.js +40 -14
- package/dist/cli/plugins-cli.js +250 -73
- package/dist/cli/ports.js +11 -10
- package/dist/cli/program/build-program.js +3 -1
- package/dist/cli/program/command-registry.js +214 -136
- package/dist/cli/program/command-tree.js +16 -0
- package/dist/cli/program/help.js +43 -12
- package/dist/cli/program/preaction.js +13 -9
- package/dist/cli/program/register.configure.js +3 -18
- package/dist/cli/program/register.maintenance.js +2 -2
- package/dist/cli/program/register.onboard.js +2 -0
- package/dist/cli/program/register.status-health-sessions.js +16 -17
- package/dist/cli/program/register.subclis.js +93 -52
- package/dist/cli/route.js +12 -8
- package/dist/cli/system-cli.js +36 -46
- package/dist/cli/test-runtime-capture.js +24 -0
- package/dist/cli/update-cli/shared.js +22 -9
- package/dist/cli/update-cli/update-command.js +89 -14
- package/dist/cli/update-cli/wizard.js +6 -12
- package/dist/commands/agent/run-context.js +18 -5
- package/dist/commands/agent/session-store.js +17 -4
- package/dist/commands/agent.js +185 -89
- package/dist/commands/agents.bindings.js +14 -7
- package/dist/commands/agents.commands.add.js +13 -9
- package/dist/commands/agents.commands.identity.js +12 -6
- package/dist/commands/agents.commands.list.js +11 -6
- package/dist/commands/agents.config.js +8 -10
- package/dist/commands/agents.providers.js +12 -6
- package/dist/commands/auth-choice-options.js +103 -75
- package/dist/commands/auth-choice.apply.byteplus.js +55 -0
- package/dist/commands/auth-choice.apply.js +4 -0
- package/dist/commands/auth-choice.apply.minimax.js +61 -13
- package/dist/commands/auth-choice.apply.openai.js +3 -1
- package/dist/commands/auth-choice.apply.volcengine.js +55 -0
- package/dist/commands/auth-choice.preferred-provider.js +2 -0
- package/dist/commands/channels/remove.js +13 -6
- package/dist/commands/channels/shared.js +4 -14
- package/dist/commands/channels.mock-harness.js +23 -0
- package/dist/commands/configure.commands.js +14 -0
- package/dist/commands/configure.gateway.js +2 -4
- package/dist/commands/configure.js +1 -1
- package/dist/commands/configure.shared.js +11 -0
- package/dist/commands/daemon-install-helpers.js +2 -2
- package/dist/commands/daemon-install-runtime-warning.js +11 -0
- package/dist/commands/dashboard.js +12 -10
- package/dist/commands/docs.js +14 -8
- package/dist/commands/doctor-config-flow.js +11 -9
- package/dist/commands/doctor-legacy-config.js +281 -0
- package/dist/commands/doctor-state-integrity.js +99 -23
- package/dist/commands/doctor-update.js +12 -9
- package/dist/commands/models/list.list-command.js +7 -5
- package/dist/commands/models/set-image.js +2 -21
- package/dist/commands/node-daemon-install-helpers.js +10 -8
- package/dist/commands/onboard-auth.config-minimax.js +54 -80
- package/dist/commands/onboard-auth.config-opencode.js +2 -18
- package/dist/commands/onboard-auth.credentials.js +90 -13
- package/dist/commands/onboard-auth.js +1 -1
- package/dist/commands/onboard-auth.models.js +6 -5
- package/dist/commands/onboard-hooks.js +1 -1
- package/dist/commands/onboard-non-interactive/api-keys.js +14 -7
- package/dist/commands/onboard-non-interactive/local/auth-choice.js +64 -49
- package/dist/commands/onboard-provider-auth-flags.js +14 -0
- package/dist/commands/onboard-remote.js +14 -7
- package/dist/commands/onboard.js +11 -13
- package/dist/commands/sandbox-display.js +6 -5
- package/dist/commands/sessions.test-helpers.js +61 -0
- package/dist/commands/status-all/diagnosis.js +14 -10
- package/dist/commands/status-all/format.js +1 -0
- package/dist/commands/status.gateway-probe.js +1 -16
- package/dist/commands/systemd-linger.js +12 -6
- package/dist/config/agent-limits.js +2 -0
- package/dist/config/commands.js +32 -15
- package/dist/config/config-paths.js +9 -11
- package/dist/config/config.js +1 -1
- package/dist/config/defaults.js +22 -2
- package/dist/config/discord-preview-streaming.js +104 -0
- package/dist/config/env-substitution.js +62 -34
- package/dist/config/env-vars.js +45 -7
- package/dist/config/includes.js +4 -0
- package/dist/config/io.js +656 -171
- package/dist/config/legacy.migrations.part-1.js +189 -78
- package/dist/config/legacy.shared.js +3 -1
- package/dist/config/merge-patch.js +54 -4
- package/dist/config/prototype-keys.js +4 -0
- package/dist/config/redact-snapshot.js +404 -76
- package/dist/config/schema.help.js +44 -7
- package/dist/config/schema.js +58 -570
- package/dist/config/schema.labels.js +38 -6
- package/dist/config/sessions/delivery-info.js +10 -3
- package/dist/config/sessions/main-session.js +10 -5
- package/dist/config/sessions/session-file.js +33 -0
- package/dist/config/sessions/session-key.js +10 -5
- package/dist/config/sessions/store.js +1 -1
- package/dist/config/sessions.js +1 -0
- package/dist/config/validation.js +140 -85
- package/dist/config/zod-schema.agent-runtime.js +11 -0
- package/dist/config/zod-schema.hooks.js +40 -11
- package/dist/config/zod-schema.installs.js +20 -0
- package/dist/config/zod-schema.js +156 -20
- package/dist/config/zod-schema.providers-core.js +78 -4
- package/dist/config/zod-schema.providers.js +6 -1
- package/dist/config/zod-schema.session.js +41 -2
- package/dist/cron/run-log.js +3 -0
- package/dist/cron/schedule.js +21 -10
- package/dist/cron/service/ops.js +35 -21
- package/dist/cron/service/timer.js +116 -16
- package/dist/cron/stagger.js +3 -1
- package/dist/daemon/cmd-argv.js +21 -0
- package/dist/daemon/cmd-set.js +58 -0
- package/dist/daemon/service-types.js +1 -0
- package/dist/discord/api.js +12 -6
- package/dist/discord/draft-chunking.js +22 -0
- package/dist/discord/draft-stream.js +124 -0
- package/dist/discord/monitor/agent-components.js +1 -1
- package/dist/discord/monitor/commands.js +5 -0
- package/dist/discord/monitor/exec-approvals.js +357 -162
- package/dist/discord/monitor/gateway-plugin.js +2 -1
- package/dist/discord/monitor/listeners.js +37 -27
- package/dist/discord/monitor/message-handler.js +4 -1
- package/dist/discord/monitor/message-handler.preflight.js +65 -8
- package/dist/discord/monitor/message-handler.process.js +246 -217
- package/dist/discord/monitor/message-utils.js +143 -6
- package/dist/discord/monitor/model-picker-preferences.js +143 -0
- package/dist/discord/monitor/model-picker.js +651 -0
- package/dist/discord/monitor/native-command.js +573 -16
- package/dist/discord/monitor/provider.allowlist.js +223 -0
- package/dist/discord/monitor/provider.js +275 -347
- package/dist/discord/monitor/provider.lifecycle.js +100 -0
- package/dist/discord/monitor/reply-delivery.js +123 -16
- package/dist/discord/monitor/thread-bindings.discord-api.js +215 -0
- package/dist/discord/monitor/thread-bindings.js +4 -0
- package/dist/discord/monitor/thread-bindings.lifecycle.js +177 -0
- package/dist/discord/monitor/thread-bindings.manager.js +423 -0
- package/dist/discord/monitor/thread-bindings.messages.js +55 -0
- package/dist/discord/monitor/thread-bindings.state.js +358 -0
- package/dist/discord/monitor/thread-bindings.types.js +6 -0
- package/dist/discord/resolve-users.js +33 -21
- package/dist/discord/send.channels.js +15 -0
- package/dist/discord/send.js +3 -2
- package/dist/discord/send.outbound.js +82 -26
- package/dist/discord/send.permissions.js +83 -30
- package/dist/discord/send.reactions.js +8 -4
- package/dist/discord/token.js +10 -5
- package/dist/discord/voice/command.js +263 -0
- package/dist/discord/voice/manager.js +531 -0
- package/dist/gateway/auth.js +72 -13
- package/dist/gateway/call.js +152 -83
- package/dist/gateway/canvas-capability.js +75 -0
- package/dist/gateway/client.js +28 -4
- package/dist/gateway/config-reload.js +3 -4
- package/dist/gateway/control-plane-audit.js +28 -0
- package/dist/gateway/control-plane-rate-limit.js +53 -0
- package/dist/gateway/control-ui.js +219 -96
- package/dist/gateway/events.js +1 -0
- package/dist/gateway/hooks-mapping.js +88 -38
- package/dist/gateway/hooks.js +109 -54
- package/dist/gateway/http-auth-helpers.js +3 -2
- package/dist/gateway/http-common.js +22 -0
- package/dist/gateway/http-endpoint-helpers.js +1 -0
- package/dist/gateway/method-scopes.js +169 -0
- package/dist/gateway/net.js +74 -9
- package/dist/gateway/node-invoke-system-run-approval.js +14 -35
- package/dist/gateway/node-registry.js +10 -5
- package/dist/gateway/openai-http.js +1 -0
- package/dist/gateway/openresponses-http.js +121 -110
- package/dist/gateway/origin-check.js +1 -18
- package/dist/gateway/probe-auth.js +2 -0
- package/dist/gateway/protocol/index.js +4 -2
- package/dist/gateway/protocol/schema/cron.js +1 -0
- package/dist/gateway/protocol/schema/devices.js +1 -0
- package/dist/gateway/protocol/schema/protocol-schemas.js +4 -1
- package/dist/gateway/protocol/schema/push.js +18 -0
- package/dist/gateway/protocol/schema/sessions.js +6 -0
- package/dist/gateway/protocol/schema.js +1 -0
- package/dist/gateway/role-policy.js +17 -0
- package/dist/gateway/server/ws-connection/connect-policy.js +37 -0
- package/dist/gateway/server/ws-connection/message-handler.js +175 -148
- package/dist/gateway/server-chat.js +83 -25
- package/dist/gateway/server-constants.js +10 -9
- package/dist/gateway/server-cron.js +1 -0
- package/dist/gateway/server-http.js +247 -54
- package/dist/gateway/server-maintenance.js +20 -5
- package/dist/gateway/server-methods/agent.js +162 -24
- package/dist/gateway/server-methods/chat.js +465 -130
- package/dist/gateway/server-methods/config.js +193 -152
- package/dist/gateway/server-methods/devices.js +17 -3
- package/dist/gateway/server-methods/models.js +11 -1
- package/dist/gateway/server-methods/nodes.helpers.js +12 -0
- package/dist/gateway/server-methods/nodes.js +251 -69
- package/dist/gateway/server-methods/push.js +53 -0
- package/dist/gateway/server-methods/sessions.js +64 -8
- package/dist/gateway/server-methods/usage.js +162 -75
- package/dist/gateway/server-node-events.js +29 -0
- package/dist/gateway/server-reload-handlers.js +2 -3
- package/dist/gateway/server-runtime-config.js +39 -13
- package/dist/gateway/server-runtime-state.js +2 -0
- package/dist/gateway/server-startup-memory.js +17 -11
- package/dist/gateway/server-ws-runtime.js +1 -0
- package/dist/gateway/server.impl.js +296 -139
- package/dist/gateway/session-preview.test-helpers.js +11 -0
- package/dist/gateway/session-utils.fs.js +32 -34
- package/dist/gateway/sessions-resolve.js +17 -5
- package/dist/gateway/startup-auth.js +126 -0
- package/dist/gateway/test-helpers.agent-results.js +15 -0
- package/dist/gateway/test-helpers.mocks.js +37 -14
- package/dist/gateway/test-helpers.openai-mock.js +14 -7
- package/dist/gateway/test-helpers.server.js +161 -77
- package/dist/gateway/tools-invoke-http.js +21 -10
- package/dist/hooks/bundled/bootstrap-extra-files/handler.js +3 -1
- package/dist/hooks/bundled/command-logger/handler.js +7 -2
- package/dist/hooks/bundled/session-memory/handler.js +170 -38
- package/dist/hooks/frontmatter.js +6 -6
- package/dist/hooks/gmail-watcher-lifecycle.js +23 -0
- package/dist/hooks/gmail-watcher.js +11 -6
- package/dist/hooks/internal-hooks.js +11 -1
- package/dist/hooks/llm-slug-generator.js +4 -1
- package/dist/hooks/workspace.js +47 -17
- package/dist/imessage/accounts.js +9 -20
- package/dist/imessage/monitor/inbound-processing.js +2 -1
- package/dist/infra/archive-path.js +49 -0
- package/dist/infra/archive.js +174 -73
- package/dist/infra/control-ui-assets.js +14 -6
- package/dist/infra/device-pairing.js +204 -144
- package/dist/infra/env.js +10 -5
- package/dist/infra/exec-approvals-allowlist.js +141 -70
- package/dist/infra/exec-approvals-analysis.js +78 -20
- package/dist/infra/exec-approvals.js +5 -17
- package/dist/infra/exec-safe-bin-policy.js +277 -0
- package/dist/infra/fixed-window-rate-limit.js +33 -0
- package/dist/infra/fs-safe.js +71 -39
- package/dist/infra/gateway-lock.js +6 -2
- package/dist/infra/git-root.js +61 -0
- package/dist/infra/heartbeat-active-hours.js +2 -2
- package/dist/infra/heartbeat-reason.js +40 -0
- package/dist/infra/heartbeat-runner.js +72 -32
- package/dist/infra/heartbeat-wake.js +6 -12
- package/dist/infra/host-env-security-policy.json +19 -0
- package/dist/infra/host-env-security.js +66 -0
- package/dist/infra/install-source-utils.js +91 -7
- package/dist/infra/net/ssrf.js +131 -38
- package/dist/infra/node-pairing.js +50 -105
- package/dist/infra/npm-integrity.js +45 -0
- package/dist/infra/npm-pack-install.js +40 -0
- package/dist/infra/outbound/bound-delivery-router.js +88 -0
- package/dist/infra/outbound/channel-adapters.js +20 -7
- package/dist/infra/outbound/channel-selection.js +12 -6
- package/dist/infra/outbound/envelope.js +1 -1
- package/dist/infra/outbound/format.js +12 -6
- package/dist/infra/outbound/message-action-runner.js +107 -327
- package/dist/infra/outbound/message.js +59 -36
- package/dist/infra/outbound/outbound-policy.js +52 -25
- package/dist/infra/outbound/outbound-send-service.js +58 -71
- package/dist/infra/outbound/payloads.js +14 -7
- package/dist/infra/outbound/session-binding-service.js +123 -0
- package/dist/infra/pairing-files.js +10 -0
- package/dist/infra/path-guards.js +25 -0
- package/dist/infra/plain-object.js +9 -0
- package/dist/infra/provider-usage.fetch.codex.js +7 -15
- package/dist/infra/provider-usage.fetch.gemini.js +14 -11
- package/dist/infra/provider-usage.fetch.shared.js +30 -1
- package/dist/infra/provider-usage.fetch.zai.js +10 -9
- package/dist/infra/push-apns.js +365 -0
- package/dist/infra/restart-sentinel.js +16 -1
- package/dist/infra/restart.js +229 -26
- package/dist/infra/retry-policy.js +4 -2
- package/dist/infra/retry.js +9 -5
- package/dist/infra/scp-host.js +54 -0
- package/dist/infra/session-cost-usage.js +107 -59
- package/dist/infra/session-maintenance-warning.js +3 -1
- package/dist/infra/shell-env.js +98 -34
- package/dist/infra/ssh-config.js +12 -6
- package/dist/infra/system-run-command.js +49 -4
- package/dist/infra/update-channels.js +10 -5
- package/dist/infra/update-startup.js +86 -9
- package/dist/line/accounts.js +5 -7
- package/dist/line/bot-access.js +8 -20
- package/dist/line/bot-handlers.js +3 -1
- package/dist/link-understanding/detect.js +15 -7
- package/dist/media/constants.js +15 -6
- package/dist/media/image-ops.js +7 -0
- package/dist/media/inbound-path-policy.js +114 -0
- package/dist/media/input-files.js +16 -0
- package/dist/media/local-roots.js +3 -2
- package/dist/media-understanding/apply.js +4 -1
- package/dist/media-understanding/concurrency.js +8 -20
- package/dist/memory/backend-config.js +45 -6
- package/dist/memory/embeddings.js +10 -4
- package/dist/memory/fs-utils.js +23 -0
- package/dist/memory/manager-search.js +12 -6
- package/dist/memory/manager-sync-ops.js +12 -2
- package/dist/memory/qmd-manager.js +466 -53
- package/dist/memory/query-expansion.js +167 -3
- package/dist/memory/status-format.js +10 -5
- package/dist/memory/sync-memory-files.js +1 -1
- package/dist/memory/test-manager.js +8 -0
- package/dist/node-host/invoke-system-run.js +281 -0
- package/dist/node-host/invoke.js +55 -337
- package/dist/pairing/pairing-store.js +22 -0
- package/dist/plugin-sdk/allow-from.js +1 -1
- package/dist/plugin-sdk/command-auth.js +3 -1
- package/dist/plugin-sdk/index.js +6 -3
- package/dist/plugin-sdk/temp-path.js +47 -0
- package/dist/plugin-sdk/webhook-targets.js +32 -0
- package/dist/plugins/bundled-dir.js +9 -6
- package/dist/plugins/discovery.js +217 -23
- package/dist/plugins/hook-runner-global.js +16 -0
- package/dist/plugins/hooks.js +50 -0
- package/dist/plugins/install.js +28 -16
- package/dist/plugins/loader.js +192 -26
- package/dist/plugins/logger.js +8 -0
- package/dist/plugins/manifest-registry.js +3 -0
- package/dist/plugins/path-safety.js +34 -0
- package/dist/plugins/registry.js +5 -2
- package/dist/plugins/runtime/index.js +271 -206
- package/dist/plugins/runtime.js +3 -17
- package/dist/plugins/update.js +78 -12
- package/dist/process/spawn-utils.js +14 -7
- package/dist/providers/github-copilot-models.js +4 -1
- package/dist/providers/github-copilot-token.js +11 -6
- package/dist/providers/qwen-portal-oauth.js +14 -6
- package/dist/routing/account-id.js +30 -0
- package/dist/routing/resolve-route.js +3 -7
- package/dist/routing/session-key.js +2 -16
- package/dist/security/audit-channel.js +100 -20
- package/dist/security/audit-extra.async.js +505 -179
- package/dist/security/audit-extra.js +12 -2
- package/dist/security/audit-extra.sync.js +421 -35
- package/dist/security/audit-fs.js +31 -13
- package/dist/security/audit.js +180 -370
- package/dist/security/dm-policy-shared.js +68 -0
- package/dist/security/external-content.js +46 -14
- package/dist/security/fix.js +49 -85
- package/dist/security/scan-paths.js +20 -0
- package/dist/security/secret-equal.js +3 -7
- package/dist/security/windows-acl.js +30 -15
- package/dist/shared/entry-status.js +6 -0
- package/dist/shared/frontmatter.js +5 -5
- package/dist/shared/node-list-parse.js +13 -0
- package/dist/shared/node-match.js +11 -4
- package/dist/shared/operator-scope-compat.js +42 -0
- package/dist/shared/text-chunking.js +29 -0
- package/dist/signal/accounts.js +7 -20
- package/dist/signal/monitor/event-handler.js +3 -1
- package/dist/slack/accounts.js +6 -19
- package/dist/slack/actions.js +11 -3
- package/dist/slack/blocks.test-helpers.js +31 -0
- package/dist/slack/monitor/auth.js +1 -1
- package/dist/slack/monitor/message-handler/dispatch.js +50 -29
- package/dist/slack/monitor/mrkdwn.js +8 -0
- package/dist/slack/monitor/replies.js +15 -7
- package/dist/slack/monitor/slash.js +22 -13
- package/dist/slack/resolve-channels.js +10 -5
- package/dist/slack/send.js +102 -12
- package/dist/slack/stream-mode.js +10 -0
- package/dist/slack/streaming.js +4 -2
- package/dist/telegram/accounts.js +19 -14
- package/dist/telegram/bot/helpers.js +3 -5
- package/dist/telegram/bot-access.js +35 -36
- package/dist/telegram/bot-handlers.js +120 -148
- package/dist/telegram/bot-message-context.js +68 -9
- package/dist/telegram/bot-message-dispatch.js +477 -210
- package/dist/telegram/bot-native-commands.js +16 -0
- package/dist/telegram/draft-stream.js +44 -8
- package/dist/telegram/inline-buttons.js +5 -15
- package/dist/telegram/monitor.js +11 -7
- package/dist/telegram/network-config.js +19 -7
- package/dist/telegram/reasoning-lane-coordinator.js +128 -0
- package/dist/telegram/send.js +3 -2
- package/dist/telegram/sent-message-cache.js +5 -6
- package/dist/telegram/status-reaction-variants.js +208 -0
- package/dist/telegram/sticker-cache.js +11 -9
- package/dist/terminal/prompt-select-styled.js +9 -0
- package/dist/terminal/theme.js +12 -12
- package/dist/test-utils/command-runner.js +6 -0
- package/dist/test-utils/internal-hook-event-payload.js +10 -0
- package/dist/test-utils/model-auth-mock.js +12 -0
- package/dist/test-utils/provider-usage-fetch.js +14 -0
- package/dist/test-utils/temp-home.js +33 -0
- package/dist/tts/tts.js +80 -567
- package/dist/tui/components/chat-log.js +50 -8
- package/dist/tui/theme/theme.js +10 -12
- package/dist/tui/tui-command-handlers.js +36 -27
- package/dist/tui/tui-event-handlers.js +122 -32
- package/dist/tui/tui-local-shell.js +16 -6
- package/dist/tui/tui.js +236 -48
- package/dist/utils/account-id.js +2 -4
- package/dist/utils/boolean.js +10 -5
- package/dist/utils/directive-tags.js +11 -0
- package/dist/utils/mask-api-key.js +10 -0
- package/dist/utils/queue-helpers.js +67 -12
- package/dist/utils/run-with-concurrency.js +39 -0
- package/dist/web/auto-reply/deliver-reply.js +8 -4
- package/dist/web/auto-reply/mentions.js +10 -5
- package/dist/web/auto-reply/monitor/group-members.js +14 -7
- package/dist/web/auto-reply/monitor/process-message.js +45 -24
- package/dist/web/inbound/access-control.js +5 -2
- package/dist/web/login-qr.js +12 -6
- package/dist/web/media.js +126 -15
- package/docs/tools/slash-commands.md +5 -1
- package/extensions/bluebubbles/src/monitor-processing.ts +580 -139
- package/extensions/bluebubbles/src/monitor.ts +208 -1950
- package/extensions/feishu/src/external-keys.ts +19 -0
- package/extensions/lobster/src/windows-spawn.ts +193 -0
- package/extensions/matrix/src/matrix/actions/limits.ts +6 -0
- package/extensions/mattermost/src/mattermost/reactions.test-helpers.ts +83 -0
- package/package.json +1 -1
|
@@ -1,34 +1,224 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
1
|
import fs from "node:fs";
|
|
3
2
|
import path from "node:path";
|
|
4
|
-
import { CURRENT_SESSION_VERSION } from "@mariozechner/pi-coding-agent";
|
|
3
|
+
import { CURRENT_SESSION_VERSION, SessionManager } from "@mariozechner/pi-coding-agent";
|
|
5
4
|
import { resolveSessionAgentId } from "../../agents/agent-scope.js";
|
|
6
|
-
import { resolveEffectiveMessagesConfig, resolveIdentityName } from "../../agents/identity.js";
|
|
7
5
|
import { resolveThinkingDefault } from "../../agents/model-selection.js";
|
|
8
6
|
import { resolveAgentTimeoutMs } from "../../agents/timeout.js";
|
|
9
7
|
import { dispatchInboundMessage } from "../../auto-reply/dispatch.js";
|
|
10
8
|
import { createReplyDispatcher } from "../../auto-reply/reply/reply-dispatcher.js";
|
|
11
|
-
import {
|
|
9
|
+
import { createReplyPrefixOptions } from "../../channels/reply-prefix.js";
|
|
10
|
+
import { resolveSessionFilePath } from "../../config/sessions.js";
|
|
12
11
|
import { resolveSendPolicy } from "../../sessions/send-policy.js";
|
|
12
|
+
import { stripInlineDirectiveTagsForDisplay } from "../../utils/directive-tags.js";
|
|
13
13
|
import { INTERNAL_MESSAGE_CHANNEL } from "../../utils/message-channel.js";
|
|
14
14
|
import { abortChatRunById, abortChatRunsForSessionKey, isChatStopCommandText, resolveChatRunExpiresAtMs, } from "../chat-abort.js";
|
|
15
15
|
import { parseMessageWithAttachments } from "../chat-attachments.js";
|
|
16
|
+
import { stripEnvelopeFromMessages } from "../chat-sanitize.js";
|
|
17
|
+
import { GATEWAY_CLIENT_CAPS, hasGatewayClientCap } from "../protocol/client-info.js";
|
|
16
18
|
import { ErrorCodes, errorShape, formatValidationErrors, validateChatAbortParams, validateChatHistoryParams, validateChatInjectParams, validateChatSendParams, } from "../protocol/index.js";
|
|
17
19
|
import { getMaxChatHistoryMessagesBytes } from "../server-constants.js";
|
|
18
20
|
import { capArrayByJsonBytes, loadSessionEntry, readSessionMessages, resolveSessionModelRef, } from "../session-utils.js";
|
|
19
|
-
import { stripEnvelopeFromMessages } from "../chat-sanitize.js";
|
|
20
21
|
import { formatForLog } from "../ws-log.js";
|
|
22
|
+
import { injectTimestamp, timestampOptsFromConfig } from "./agent-timestamp.js";
|
|
23
|
+
import { normalizeRpcAttachmentsToChatAttachments } from "./attachment-normalize.js";
|
|
24
|
+
const CHAT_HISTORY_TEXT_MAX_CHARS = 12_000;
|
|
25
|
+
const CHAT_HISTORY_MAX_SINGLE_MESSAGE_BYTES = 128 * 1024;
|
|
26
|
+
const CHAT_HISTORY_OVERSIZED_PLACEHOLDER = "[chat.history omitted: message too large]";
|
|
27
|
+
let chatHistoryPlaceholderEmitCount = 0;
|
|
28
|
+
function stripDisallowedChatControlChars(message) {
|
|
29
|
+
let output = "";
|
|
30
|
+
for (const char of message) {
|
|
31
|
+
const code = char.charCodeAt(0);
|
|
32
|
+
if (code === 9 || code === 10 || code === 13 || (code >= 32 && code !== 127)) {
|
|
33
|
+
output += char;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return output;
|
|
37
|
+
}
|
|
38
|
+
export function sanitizeChatSendMessageInput(message) {
|
|
39
|
+
const normalized = message.normalize("NFC");
|
|
40
|
+
if (normalized.includes("\u0000")) {
|
|
41
|
+
return { ok: false, error: "message must not contain null bytes" };
|
|
42
|
+
}
|
|
43
|
+
return { ok: true, message: stripDisallowedChatControlChars(normalized) };
|
|
44
|
+
}
|
|
45
|
+
function truncateChatHistoryText(text) {
|
|
46
|
+
if (text.length <= CHAT_HISTORY_TEXT_MAX_CHARS) {
|
|
47
|
+
return { text, truncated: false };
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
text: `${text.slice(0, CHAT_HISTORY_TEXT_MAX_CHARS)}\n...(truncated)...`,
|
|
51
|
+
truncated: true,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function sanitizeChatHistoryContentBlock(block) {
|
|
55
|
+
if (!block || typeof block !== "object") {
|
|
56
|
+
return { block, changed: false };
|
|
57
|
+
}
|
|
58
|
+
const entry = { ...block };
|
|
59
|
+
let changed = false;
|
|
60
|
+
if (typeof entry.text === "string") {
|
|
61
|
+
const stripped = stripInlineDirectiveTagsForDisplay(entry.text);
|
|
62
|
+
const res = truncateChatHistoryText(stripped.text);
|
|
63
|
+
entry.text = res.text;
|
|
64
|
+
changed ||= stripped.changed || res.truncated;
|
|
65
|
+
}
|
|
66
|
+
if (typeof entry.partialJson === "string") {
|
|
67
|
+
const res = truncateChatHistoryText(entry.partialJson);
|
|
68
|
+
entry.partialJson = res.text;
|
|
69
|
+
changed ||= res.truncated;
|
|
70
|
+
}
|
|
71
|
+
if (typeof entry.arguments === "string") {
|
|
72
|
+
const res = truncateChatHistoryText(entry.arguments);
|
|
73
|
+
entry.arguments = res.text;
|
|
74
|
+
changed ||= res.truncated;
|
|
75
|
+
}
|
|
76
|
+
if (typeof entry.thinking === "string") {
|
|
77
|
+
const res = truncateChatHistoryText(entry.thinking);
|
|
78
|
+
entry.thinking = res.text;
|
|
79
|
+
changed ||= res.truncated;
|
|
80
|
+
}
|
|
81
|
+
if ("thinkingSignature" in entry) {
|
|
82
|
+
delete entry.thinkingSignature;
|
|
83
|
+
changed = true;
|
|
84
|
+
}
|
|
85
|
+
const type = typeof entry.type === "string" ? entry.type : "";
|
|
86
|
+
if (type === "image" && typeof entry.data === "string") {
|
|
87
|
+
const bytes = Buffer.byteLength(entry.data, "utf8");
|
|
88
|
+
delete entry.data;
|
|
89
|
+
entry.omitted = true;
|
|
90
|
+
entry.bytes = bytes;
|
|
91
|
+
changed = true;
|
|
92
|
+
}
|
|
93
|
+
return { block: changed ? entry : block, changed };
|
|
94
|
+
}
|
|
95
|
+
function sanitizeChatHistoryMessage(message) {
|
|
96
|
+
if (!message || typeof message !== "object") {
|
|
97
|
+
return { message, changed: false };
|
|
98
|
+
}
|
|
99
|
+
const entry = { ...message };
|
|
100
|
+
let changed = false;
|
|
101
|
+
if ("details" in entry) {
|
|
102
|
+
delete entry.details;
|
|
103
|
+
changed = true;
|
|
104
|
+
}
|
|
105
|
+
if ("usage" in entry) {
|
|
106
|
+
delete entry.usage;
|
|
107
|
+
changed = true;
|
|
108
|
+
}
|
|
109
|
+
if ("cost" in entry) {
|
|
110
|
+
delete entry.cost;
|
|
111
|
+
changed = true;
|
|
112
|
+
}
|
|
113
|
+
if (typeof entry.content === "string") {
|
|
114
|
+
const stripped = stripInlineDirectiveTagsForDisplay(entry.content);
|
|
115
|
+
const res = truncateChatHistoryText(stripped.text);
|
|
116
|
+
entry.content = res.text;
|
|
117
|
+
changed ||= stripped.changed || res.truncated;
|
|
118
|
+
}
|
|
119
|
+
else if (Array.isArray(entry.content)) {
|
|
120
|
+
const updated = entry.content.map((block) => sanitizeChatHistoryContentBlock(block));
|
|
121
|
+
if (updated.some((item) => item.changed)) {
|
|
122
|
+
entry.content = updated.map((item) => item.block);
|
|
123
|
+
changed = true;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (typeof entry.text === "string") {
|
|
127
|
+
const stripped = stripInlineDirectiveTagsForDisplay(entry.text);
|
|
128
|
+
const res = truncateChatHistoryText(stripped.text);
|
|
129
|
+
entry.text = res.text;
|
|
130
|
+
changed ||= stripped.changed || res.truncated;
|
|
131
|
+
}
|
|
132
|
+
return { message: changed ? entry : message, changed };
|
|
133
|
+
}
|
|
134
|
+
function sanitizeChatHistoryMessages(messages) {
|
|
135
|
+
if (messages.length === 0) {
|
|
136
|
+
return messages;
|
|
137
|
+
}
|
|
138
|
+
let changed = false;
|
|
139
|
+
const next = messages.map((message) => {
|
|
140
|
+
const res = sanitizeChatHistoryMessage(message);
|
|
141
|
+
changed ||= res.changed;
|
|
142
|
+
return res.message;
|
|
143
|
+
});
|
|
144
|
+
return changed ? next : messages;
|
|
145
|
+
}
|
|
146
|
+
function jsonUtf8Bytes(value) {
|
|
147
|
+
try {
|
|
148
|
+
return Buffer.byteLength(JSON.stringify(value), "utf8");
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
return Buffer.byteLength(String(value), "utf8");
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function buildOversizedHistoryPlaceholder(message) {
|
|
155
|
+
const role = message &&
|
|
156
|
+
typeof message === "object" &&
|
|
157
|
+
typeof message.role === "string"
|
|
158
|
+
? message.role
|
|
159
|
+
: "assistant";
|
|
160
|
+
const timestamp = message &&
|
|
161
|
+
typeof message === "object" &&
|
|
162
|
+
typeof message.timestamp === "number"
|
|
163
|
+
? message.timestamp
|
|
164
|
+
: Date.now();
|
|
165
|
+
return {
|
|
166
|
+
role,
|
|
167
|
+
timestamp,
|
|
168
|
+
content: [{ type: "text", text: CHAT_HISTORY_OVERSIZED_PLACEHOLDER }],
|
|
169
|
+
__poolbot: { truncated: true, reason: "oversized" },
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
function replaceOversizedChatHistoryMessages(params) {
|
|
173
|
+
const { messages, maxSingleMessageBytes } = params;
|
|
174
|
+
if (messages.length === 0) {
|
|
175
|
+
return { messages, replacedCount: 0 };
|
|
176
|
+
}
|
|
177
|
+
let replacedCount = 0;
|
|
178
|
+
const next = messages.map((message) => {
|
|
179
|
+
if (jsonUtf8Bytes(message) <= maxSingleMessageBytes) {
|
|
180
|
+
return message;
|
|
181
|
+
}
|
|
182
|
+
replacedCount += 1;
|
|
183
|
+
return buildOversizedHistoryPlaceholder(message);
|
|
184
|
+
});
|
|
185
|
+
return { messages: replacedCount > 0 ? next : messages, replacedCount };
|
|
186
|
+
}
|
|
187
|
+
function enforceChatHistoryFinalBudget(params) {
|
|
188
|
+
const { messages, maxBytes } = params;
|
|
189
|
+
if (messages.length === 0) {
|
|
190
|
+
return { messages, placeholderCount: 0 };
|
|
191
|
+
}
|
|
192
|
+
if (jsonUtf8Bytes(messages) <= maxBytes) {
|
|
193
|
+
return { messages, placeholderCount: 0 };
|
|
194
|
+
}
|
|
195
|
+
const last = messages.at(-1);
|
|
196
|
+
if (last && jsonUtf8Bytes([last]) <= maxBytes) {
|
|
197
|
+
return { messages: [last], placeholderCount: 0 };
|
|
198
|
+
}
|
|
199
|
+
const placeholder = buildOversizedHistoryPlaceholder(last);
|
|
200
|
+
if (jsonUtf8Bytes([placeholder]) <= maxBytes) {
|
|
201
|
+
return { messages: [placeholder], placeholderCount: 1 };
|
|
202
|
+
}
|
|
203
|
+
return { messages: [], placeholderCount: 0 };
|
|
204
|
+
}
|
|
21
205
|
function resolveTranscriptPath(params) {
|
|
22
|
-
const { sessionId, storePath, sessionFile } = params;
|
|
23
|
-
if (sessionFile)
|
|
24
|
-
return sessionFile;
|
|
25
|
-
if (!storePath)
|
|
206
|
+
const { sessionId, storePath, sessionFile, agentId } = params;
|
|
207
|
+
if (!storePath && !sessionFile) {
|
|
26
208
|
return null;
|
|
27
|
-
|
|
209
|
+
}
|
|
210
|
+
try {
|
|
211
|
+
const sessionsDir = storePath ? path.dirname(storePath) : undefined;
|
|
212
|
+
return resolveSessionFilePath(sessionId, sessionFile ? { sessionFile } : undefined, sessionsDir || agentId ? { sessionsDir, agentId } : undefined);
|
|
213
|
+
}
|
|
214
|
+
catch {
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
28
217
|
}
|
|
29
218
|
function ensureTranscriptFile(params) {
|
|
30
|
-
if (fs.existsSync(params.transcriptPath))
|
|
219
|
+
if (fs.existsSync(params.transcriptPath)) {
|
|
31
220
|
return { ok: true };
|
|
221
|
+
}
|
|
32
222
|
try {
|
|
33
223
|
fs.mkdirSync(path.dirname(params.transcriptPath), { recursive: true });
|
|
34
224
|
const header = {
|
|
@@ -38,18 +228,40 @@ function ensureTranscriptFile(params) {
|
|
|
38
228
|
timestamp: new Date().toISOString(),
|
|
39
229
|
cwd: process.cwd(),
|
|
40
230
|
};
|
|
41
|
-
fs.writeFileSync(params.transcriptPath, `${JSON.stringify(header)}\n`,
|
|
231
|
+
fs.writeFileSync(params.transcriptPath, `${JSON.stringify(header)}\n`, {
|
|
232
|
+
encoding: "utf-8",
|
|
233
|
+
mode: 0o600,
|
|
234
|
+
});
|
|
42
235
|
return { ok: true };
|
|
43
236
|
}
|
|
44
237
|
catch (err) {
|
|
45
238
|
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
46
239
|
}
|
|
47
240
|
}
|
|
241
|
+
function transcriptHasIdempotencyKey(transcriptPath, idempotencyKey) {
|
|
242
|
+
try {
|
|
243
|
+
const lines = fs.readFileSync(transcriptPath, "utf-8").split(/\r?\n/);
|
|
244
|
+
for (const line of lines) {
|
|
245
|
+
if (!line.trim()) {
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
const parsed = JSON.parse(line);
|
|
249
|
+
if (parsed?.message?.idempotencyKey === idempotencyKey) {
|
|
250
|
+
return true;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
catch {
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
48
259
|
function appendAssistantTranscriptMessage(params) {
|
|
49
260
|
const transcriptPath = resolveTranscriptPath({
|
|
50
261
|
sessionId: params.sessionId,
|
|
51
262
|
storePath: params.storePath,
|
|
52
263
|
sessionFile: params.sessionFile,
|
|
264
|
+
agentId: params.agentId,
|
|
53
265
|
});
|
|
54
266
|
if (!transcriptPath) {
|
|
55
267
|
return { ok: false, error: "transcript path not resolved" };
|
|
@@ -66,29 +278,134 @@ function appendAssistantTranscriptMessage(params) {
|
|
|
66
278
|
return { ok: false, error: ensured.error ?? "failed to create transcript file" };
|
|
67
279
|
}
|
|
68
280
|
}
|
|
281
|
+
if (params.idempotencyKey && transcriptHasIdempotencyKey(transcriptPath, params.idempotencyKey)) {
|
|
282
|
+
return { ok: true };
|
|
283
|
+
}
|
|
69
284
|
const now = Date.now();
|
|
70
|
-
const messageId = randomUUID().slice(0, 8);
|
|
71
285
|
const labelPrefix = params.label ? `[${params.label}]\n\n` : "";
|
|
286
|
+
const usage = {
|
|
287
|
+
input: 0,
|
|
288
|
+
output: 0,
|
|
289
|
+
cacheRead: 0,
|
|
290
|
+
cacheWrite: 0,
|
|
291
|
+
totalTokens: 0,
|
|
292
|
+
cost: {
|
|
293
|
+
input: 0,
|
|
294
|
+
output: 0,
|
|
295
|
+
cacheRead: 0,
|
|
296
|
+
cacheWrite: 0,
|
|
297
|
+
total: 0,
|
|
298
|
+
},
|
|
299
|
+
};
|
|
72
300
|
const messageBody = {
|
|
73
301
|
role: "assistant",
|
|
74
302
|
content: [{ type: "text", text: `${labelPrefix}${params.message}` }],
|
|
75
303
|
timestamp: now,
|
|
76
|
-
stopReason
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
304
|
+
// Pi stopReason is a strict enum; this is not model output, but we still store it as a
|
|
305
|
+
// normal assistant message so it participates in the session parentId chain.
|
|
306
|
+
stopReason: "stop",
|
|
307
|
+
usage,
|
|
308
|
+
// Make these explicit so downstream tooling never treats this as model output.
|
|
309
|
+
api: "openai-responses",
|
|
310
|
+
provider: "poolbot",
|
|
311
|
+
model: "gateway-injected",
|
|
312
|
+
...(params.idempotencyKey ? { idempotencyKey: params.idempotencyKey } : {}),
|
|
313
|
+
...(params.abortMeta
|
|
314
|
+
? {
|
|
315
|
+
poolbotAbort: {
|
|
316
|
+
aborted: true,
|
|
317
|
+
origin: params.abortMeta.origin,
|
|
318
|
+
runId: params.abortMeta.runId,
|
|
319
|
+
},
|
|
320
|
+
}
|
|
321
|
+
: {}),
|
|
84
322
|
};
|
|
85
323
|
try {
|
|
86
|
-
|
|
324
|
+
// IMPORTANT: Use SessionManager so the entry is attached to the current leaf via parentId.
|
|
325
|
+
// Raw jsonl appends break the parent chain and can hide compaction summaries from context.
|
|
326
|
+
const sessionManager = SessionManager.open(transcriptPath);
|
|
327
|
+
const messageId = sessionManager.appendMessage(messageBody);
|
|
328
|
+
return { ok: true, messageId, message: messageBody };
|
|
87
329
|
}
|
|
88
330
|
catch (err) {
|
|
89
331
|
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
90
332
|
}
|
|
91
|
-
|
|
333
|
+
}
|
|
334
|
+
function collectSessionAbortPartials(params) {
|
|
335
|
+
const out = [];
|
|
336
|
+
for (const [runId, active] of params.chatAbortControllers) {
|
|
337
|
+
if (active.sessionKey !== params.sessionKey) {
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
const text = params.chatRunBuffers.get(runId);
|
|
341
|
+
if (!text || !text.trim()) {
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
out.push({
|
|
345
|
+
runId,
|
|
346
|
+
sessionId: active.sessionId,
|
|
347
|
+
text,
|
|
348
|
+
abortOrigin: params.abortOrigin,
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
return out;
|
|
352
|
+
}
|
|
353
|
+
function persistAbortedPartials(params) {
|
|
354
|
+
if (params.snapshots.length === 0) {
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
const { storePath, entry } = loadSessionEntry(params.sessionKey);
|
|
358
|
+
for (const snapshot of params.snapshots) {
|
|
359
|
+
const sessionId = entry?.sessionId ?? snapshot.sessionId ?? snapshot.runId;
|
|
360
|
+
const appended = appendAssistantTranscriptMessage({
|
|
361
|
+
message: snapshot.text,
|
|
362
|
+
sessionId,
|
|
363
|
+
storePath,
|
|
364
|
+
sessionFile: entry?.sessionFile,
|
|
365
|
+
createIfMissing: true,
|
|
366
|
+
idempotencyKey: `${snapshot.runId}:assistant`,
|
|
367
|
+
abortMeta: {
|
|
368
|
+
aborted: true,
|
|
369
|
+
origin: snapshot.abortOrigin,
|
|
370
|
+
runId: snapshot.runId,
|
|
371
|
+
},
|
|
372
|
+
});
|
|
373
|
+
if (!appended.ok) {
|
|
374
|
+
params.context.logGateway.warn(`chat.abort transcript append failed: ${appended.error ?? "unknown error"}`);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
function createChatAbortOps(context) {
|
|
379
|
+
return {
|
|
380
|
+
chatAbortControllers: context.chatAbortControllers,
|
|
381
|
+
chatRunBuffers: context.chatRunBuffers,
|
|
382
|
+
chatDeltaSentAt: context.chatDeltaSentAt,
|
|
383
|
+
chatAbortedRuns: context.chatAbortedRuns,
|
|
384
|
+
removeChatRun: context.removeChatRun,
|
|
385
|
+
agentRunSeq: context.agentRunSeq,
|
|
386
|
+
broadcast: context.broadcast,
|
|
387
|
+
nodeSendToSession: context.nodeSendToSession,
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
function abortChatRunsForSessionKeyWithPartials(params) {
|
|
391
|
+
const snapshots = collectSessionAbortPartials({
|
|
392
|
+
chatAbortControllers: params.context.chatAbortControllers,
|
|
393
|
+
chatRunBuffers: params.context.chatRunBuffers,
|
|
394
|
+
sessionKey: params.sessionKey,
|
|
395
|
+
abortOrigin: params.abortOrigin,
|
|
396
|
+
});
|
|
397
|
+
const res = abortChatRunsForSessionKey(params.ops, {
|
|
398
|
+
sessionKey: params.sessionKey,
|
|
399
|
+
stopReason: params.stopReason,
|
|
400
|
+
});
|
|
401
|
+
if (res.aborted) {
|
|
402
|
+
persistAbortedPartials({
|
|
403
|
+
context: params.context,
|
|
404
|
+
sessionKey: params.sessionKey,
|
|
405
|
+
snapshots,
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
return res;
|
|
92
409
|
}
|
|
93
410
|
function nextChatSeq(context, runId) {
|
|
94
411
|
const next = (context.agentRunSeq.get(runId) ?? 0) + 1;
|
|
@@ -106,6 +423,7 @@ function broadcastChatFinal(params) {
|
|
|
106
423
|
};
|
|
107
424
|
params.context.broadcast("chat", payload);
|
|
108
425
|
params.context.nodeSendToSession(params.sessionKey, "chat", payload);
|
|
426
|
+
params.context.agentRunSeq.delete(params.runId);
|
|
109
427
|
}
|
|
110
428
|
function broadcastChatError(params) {
|
|
111
429
|
const seq = nextChatSeq({ agentRunSeq: params.context.agentRunSeq }, params.runId);
|
|
@@ -118,6 +436,7 @@ function broadcastChatError(params) {
|
|
|
118
436
|
};
|
|
119
437
|
params.context.broadcast("chat", payload);
|
|
120
438
|
params.context.nodeSendToSession(params.sessionKey, "chat", payload);
|
|
439
|
+
params.context.agentRunSeq.delete(params.runId);
|
|
121
440
|
}
|
|
122
441
|
export const chatHandlers = {
|
|
123
442
|
"chat.history": async ({ params, respond, context }) => {
|
|
@@ -135,7 +454,20 @@ export const chatHandlers = {
|
|
|
135
454
|
const max = Math.min(hardMax, requested);
|
|
136
455
|
const sliced = rawMessages.length > max ? rawMessages.slice(-max) : rawMessages;
|
|
137
456
|
const sanitized = stripEnvelopeFromMessages(sliced);
|
|
138
|
-
const
|
|
457
|
+
const normalized = sanitizeChatHistoryMessages(sanitized);
|
|
458
|
+
const maxHistoryBytes = getMaxChatHistoryMessagesBytes();
|
|
459
|
+
const perMessageHardCap = Math.min(CHAT_HISTORY_MAX_SINGLE_MESSAGE_BYTES, maxHistoryBytes);
|
|
460
|
+
const replaced = replaceOversizedChatHistoryMessages({
|
|
461
|
+
messages: normalized,
|
|
462
|
+
maxSingleMessageBytes: perMessageHardCap,
|
|
463
|
+
});
|
|
464
|
+
const capped = capArrayByJsonBytes(replaced.messages, maxHistoryBytes).items;
|
|
465
|
+
const bounded = enforceChatHistoryFinalBudget({ messages: capped, maxBytes: maxHistoryBytes });
|
|
466
|
+
const placeholderCount = replaced.replacedCount + bounded.placeholderCount;
|
|
467
|
+
if (placeholderCount > 0) {
|
|
468
|
+
chatHistoryPlaceholderEmitCount += placeholderCount;
|
|
469
|
+
context.logGateway.debug(`chat.history omitted oversized payloads placeholders=${placeholderCount} total=${chatHistoryPlaceholderEmitCount}`);
|
|
470
|
+
}
|
|
139
471
|
let thinkingLevel = entry?.thinkingLevel;
|
|
140
472
|
if (!thinkingLevel) {
|
|
141
473
|
const configured = cfg.agents?.defaults?.thinkingDefault;
|
|
@@ -143,7 +475,8 @@ export const chatHandlers = {
|
|
|
143
475
|
thinkingLevel = configured;
|
|
144
476
|
}
|
|
145
477
|
else {
|
|
146
|
-
const {
|
|
478
|
+
const sessionAgentId = resolveSessionAgentId({ sessionKey, config: cfg });
|
|
479
|
+
const { provider, model } = resolveSessionModelRef(cfg, entry, sessionAgentId);
|
|
147
480
|
const catalog = await context.loadGatewayModelCatalog();
|
|
148
481
|
thinkingLevel = resolveThinkingDefault({
|
|
149
482
|
cfg,
|
|
@@ -153,11 +486,13 @@ export const chatHandlers = {
|
|
|
153
486
|
});
|
|
154
487
|
}
|
|
155
488
|
}
|
|
489
|
+
const verboseLevel = entry?.verboseLevel ?? cfg.agents?.defaults?.verboseDefault;
|
|
156
490
|
respond(true, {
|
|
157
491
|
sessionKey,
|
|
158
492
|
sessionId,
|
|
159
|
-
messages:
|
|
493
|
+
messages: bounded.messages,
|
|
160
494
|
thinkingLevel,
|
|
495
|
+
verboseLevel,
|
|
161
496
|
});
|
|
162
497
|
},
|
|
163
498
|
"chat.abort": ({ params, respond, context }) => {
|
|
@@ -165,20 +500,14 @@ export const chatHandlers = {
|
|
|
165
500
|
respond(false, undefined, errorShape(ErrorCodes.INVALID_REQUEST, `invalid chat.abort params: ${formatValidationErrors(validateChatAbortParams.errors)}`));
|
|
166
501
|
return;
|
|
167
502
|
}
|
|
168
|
-
const { sessionKey, runId } = params;
|
|
169
|
-
const ops =
|
|
170
|
-
chatAbortControllers: context.chatAbortControllers,
|
|
171
|
-
chatRunBuffers: context.chatRunBuffers,
|
|
172
|
-
chatDeltaSentAt: context.chatDeltaSentAt,
|
|
173
|
-
chatAbortedRuns: context.chatAbortedRuns,
|
|
174
|
-
removeChatRun: context.removeChatRun,
|
|
175
|
-
agentRunSeq: context.agentRunSeq,
|
|
176
|
-
broadcast: context.broadcast,
|
|
177
|
-
nodeSendToSession: context.nodeSendToSession,
|
|
178
|
-
};
|
|
503
|
+
const { sessionKey: rawSessionKey, runId } = params;
|
|
504
|
+
const ops = createChatAbortOps(context);
|
|
179
505
|
if (!runId) {
|
|
180
|
-
const res =
|
|
181
|
-
|
|
506
|
+
const res = abortChatRunsForSessionKeyWithPartials({
|
|
507
|
+
context,
|
|
508
|
+
ops,
|
|
509
|
+
sessionKey: rawSessionKey,
|
|
510
|
+
abortOrigin: "rpc",
|
|
182
511
|
stopReason: "rpc",
|
|
183
512
|
});
|
|
184
513
|
respond(true, { ok: true, aborted: res.aborted, runIds: res.runIds });
|
|
@@ -189,15 +518,30 @@ export const chatHandlers = {
|
|
|
189
518
|
respond(true, { ok: true, aborted: false, runIds: [] });
|
|
190
519
|
return;
|
|
191
520
|
}
|
|
192
|
-
if (active.sessionKey !==
|
|
521
|
+
if (active.sessionKey !== rawSessionKey) {
|
|
193
522
|
respond(false, undefined, errorShape(ErrorCodes.INVALID_REQUEST, "runId does not match sessionKey"));
|
|
194
523
|
return;
|
|
195
524
|
}
|
|
525
|
+
const partialText = context.chatRunBuffers.get(runId);
|
|
196
526
|
const res = abortChatRunById(ops, {
|
|
197
527
|
runId,
|
|
198
|
-
sessionKey,
|
|
528
|
+
sessionKey: rawSessionKey,
|
|
199
529
|
stopReason: "rpc",
|
|
200
530
|
});
|
|
531
|
+
if (res.aborted && partialText && partialText.trim()) {
|
|
532
|
+
persistAbortedPartials({
|
|
533
|
+
context,
|
|
534
|
+
sessionKey: rawSessionKey,
|
|
535
|
+
snapshots: [
|
|
536
|
+
{
|
|
537
|
+
runId,
|
|
538
|
+
sessionId: active.sessionId,
|
|
539
|
+
text: partialText,
|
|
540
|
+
abortOrigin: "rpc",
|
|
541
|
+
},
|
|
542
|
+
],
|
|
543
|
+
});
|
|
544
|
+
}
|
|
201
545
|
respond(true, {
|
|
202
546
|
ok: true,
|
|
203
547
|
aborted: res.aborted,
|
|
@@ -210,29 +554,24 @@ export const chatHandlers = {
|
|
|
210
554
|
return;
|
|
211
555
|
}
|
|
212
556
|
const p = params;
|
|
213
|
-
const
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
? Buffer.from(a.content.buffer, a.content.byteOffset, a.content.byteLength).toString("base64")
|
|
223
|
-
: undefined,
|
|
224
|
-
}))
|
|
225
|
-
.filter((a) => a.content) ?? [];
|
|
226
|
-
const rawMessage = p.message.trim();
|
|
557
|
+
const sanitizedMessageResult = sanitizeChatSendMessageInput(p.message);
|
|
558
|
+
if (!sanitizedMessageResult.ok) {
|
|
559
|
+
respond(false, undefined, errorShape(ErrorCodes.INVALID_REQUEST, sanitizedMessageResult.error));
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
562
|
+
const inboundMessage = sanitizedMessageResult.message;
|
|
563
|
+
const stopCommand = isChatStopCommandText(inboundMessage);
|
|
564
|
+
const normalizedAttachments = normalizeRpcAttachmentsToChatAttachments(p.attachments);
|
|
565
|
+
const rawMessage = inboundMessage.trim();
|
|
227
566
|
if (!rawMessage && normalizedAttachments.length === 0) {
|
|
228
567
|
respond(false, undefined, errorShape(ErrorCodes.INVALID_REQUEST, "message or attachment required"));
|
|
229
568
|
return;
|
|
230
569
|
}
|
|
231
|
-
let parsedMessage =
|
|
570
|
+
let parsedMessage = inboundMessage;
|
|
232
571
|
let parsedImages = [];
|
|
233
572
|
if (normalizedAttachments.length > 0) {
|
|
234
573
|
try {
|
|
235
|
-
const parsed = await parseMessageWithAttachments(
|
|
574
|
+
const parsed = await parseMessageWithAttachments(inboundMessage, normalizedAttachments, {
|
|
236
575
|
maxBytes: 5_000_000,
|
|
237
576
|
log: context.logGateway,
|
|
238
577
|
});
|
|
@@ -244,7 +583,8 @@ export const chatHandlers = {
|
|
|
244
583
|
return;
|
|
245
584
|
}
|
|
246
585
|
}
|
|
247
|
-
const
|
|
586
|
+
const rawSessionKey = p.sessionKey;
|
|
587
|
+
const { cfg, entry, canonicalKey: sessionKey } = loadSessionEntry(rawSessionKey);
|
|
248
588
|
const timeoutMs = resolveAgentTimeoutMs({
|
|
249
589
|
cfg,
|
|
250
590
|
overrideMs: p.timeoutMs,
|
|
@@ -254,7 +594,7 @@ export const chatHandlers = {
|
|
|
254
594
|
const sendPolicy = resolveSendPolicy({
|
|
255
595
|
cfg,
|
|
256
596
|
entry,
|
|
257
|
-
sessionKey
|
|
597
|
+
sessionKey,
|
|
258
598
|
channel: entry?.channel,
|
|
259
599
|
chatType: entry?.chatType,
|
|
260
600
|
});
|
|
@@ -263,16 +603,13 @@ export const chatHandlers = {
|
|
|
263
603
|
return;
|
|
264
604
|
}
|
|
265
605
|
if (stopCommand) {
|
|
266
|
-
const res =
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
broadcast: context.broadcast,
|
|
274
|
-
nodeSendToSession: context.nodeSendToSession,
|
|
275
|
-
}, { sessionKey: p.sessionKey, stopReason: "stop" });
|
|
606
|
+
const res = abortChatRunsForSessionKeyWithPartials({
|
|
607
|
+
context,
|
|
608
|
+
ops: createChatAbortOps(context),
|
|
609
|
+
sessionKey: rawSessionKey,
|
|
610
|
+
abortOrigin: "stop-command",
|
|
611
|
+
stopReason: "stop",
|
|
612
|
+
});
|
|
276
613
|
respond(true, { ok: true, aborted: res.aborted, runIds: res.runIds });
|
|
277
614
|
return;
|
|
278
615
|
}
|
|
@@ -296,7 +633,7 @@ export const chatHandlers = {
|
|
|
296
633
|
context.chatAbortControllers.set(clientRunId, {
|
|
297
634
|
controller: abortController,
|
|
298
635
|
sessionId: entry?.sessionId ?? clientRunId,
|
|
299
|
-
sessionKey:
|
|
636
|
+
sessionKey: rawSessionKey,
|
|
300
637
|
startedAtMs: now,
|
|
301
638
|
expiresAtMs: resolveChatRunExpiresAtMs({ now, timeoutMs }),
|
|
302
639
|
});
|
|
@@ -309,13 +646,17 @@ export const chatHandlers = {
|
|
|
309
646
|
const injectThinking = Boolean(p.thinking && trimmedMessage && !trimmedMessage.startsWith("/"));
|
|
310
647
|
const commandBody = injectThinking ? `/think ${p.thinking} ${parsedMessage}` : parsedMessage;
|
|
311
648
|
const clientInfo = client?.connect?.client;
|
|
649
|
+
// Inject timestamp so agents know the current date/time.
|
|
650
|
+
// Only BodyForAgent gets the timestamp — Body stays raw for UI display.
|
|
651
|
+
// See: https://github.com/moltbot/moltbot/issues/3658
|
|
652
|
+
const stampedMessage = injectTimestamp(parsedMessage, timestampOptsFromConfig(cfg));
|
|
312
653
|
const ctx = {
|
|
313
654
|
Body: parsedMessage,
|
|
314
|
-
BodyForAgent:
|
|
655
|
+
BodyForAgent: stampedMessage,
|
|
315
656
|
BodyForCommands: commandBody,
|
|
316
657
|
RawBody: parsedMessage,
|
|
317
658
|
CommandBody: commandBody,
|
|
318
|
-
SessionKey:
|
|
659
|
+
SessionKey: sessionKey,
|
|
319
660
|
Provider: INTERNAL_MESSAGE_CHANNEL,
|
|
320
661
|
Surface: INTERNAL_MESSAGE_CHANNEL,
|
|
321
662
|
OriginatingChannel: INTERNAL_MESSAGE_CHANNEL,
|
|
@@ -325,27 +666,31 @@ export const chatHandlers = {
|
|
|
325
666
|
SenderId: clientInfo?.id,
|
|
326
667
|
SenderName: clientInfo?.displayName,
|
|
327
668
|
SenderUsername: clientInfo?.displayName,
|
|
669
|
+
GatewayClientScopes: client?.connect?.scopes,
|
|
328
670
|
};
|
|
329
671
|
const agentId = resolveSessionAgentId({
|
|
330
|
-
sessionKey
|
|
672
|
+
sessionKey,
|
|
331
673
|
config: cfg,
|
|
332
674
|
});
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
675
|
+
const { onModelSelected, ...prefixOptions } = createReplyPrefixOptions({
|
|
676
|
+
cfg,
|
|
677
|
+
agentId,
|
|
678
|
+
channel: INTERNAL_MESSAGE_CHANNEL,
|
|
679
|
+
});
|
|
336
680
|
const finalReplyParts = [];
|
|
337
681
|
const dispatcher = createReplyDispatcher({
|
|
338
|
-
|
|
339
|
-
responsePrefixContextProvider: () => prefixContext,
|
|
682
|
+
...prefixOptions,
|
|
340
683
|
onError: (err) => {
|
|
341
684
|
context.logGateway.warn(`webchat dispatch failed: ${formatForLog(err)}`);
|
|
342
685
|
},
|
|
343
686
|
deliver: async (payload, info) => {
|
|
344
|
-
if (info.kind !== "final")
|
|
687
|
+
if (info.kind !== "final") {
|
|
345
688
|
return;
|
|
689
|
+
}
|
|
346
690
|
const text = payload.text?.trim() ?? "";
|
|
347
|
-
if (!text)
|
|
691
|
+
if (!text) {
|
|
348
692
|
return;
|
|
693
|
+
}
|
|
349
694
|
finalReplyParts.push(text);
|
|
350
695
|
},
|
|
351
696
|
});
|
|
@@ -358,16 +703,23 @@ export const chatHandlers = {
|
|
|
358
703
|
runId: clientRunId,
|
|
359
704
|
abortSignal: abortController.signal,
|
|
360
705
|
images: parsedImages.length > 0 ? parsedImages : undefined,
|
|
361
|
-
|
|
362
|
-
onAgentRunStart: () => {
|
|
706
|
+
onAgentRunStart: (runId) => {
|
|
363
707
|
agentRunStarted = true;
|
|
708
|
+
const connId = typeof client?.connId === "string" ? client.connId : undefined;
|
|
709
|
+
const wantsToolEvents = hasGatewayClientCap(client?.connect?.caps, GATEWAY_CLIENT_CAPS.TOOL_EVENTS);
|
|
710
|
+
if (connId && wantsToolEvents) {
|
|
711
|
+
context.registerToolEventRecipient(runId, connId);
|
|
712
|
+
// Register for any other active runs *in the same session* so
|
|
713
|
+
// late-joining clients (e.g. page refresh mid-response) receive
|
|
714
|
+
// in-progress tool events without leaking cross-session data.
|
|
715
|
+
for (const [activeRunId, active] of context.chatAbortControllers) {
|
|
716
|
+
if (activeRunId !== runId && active.sessionKey === p.sessionKey) {
|
|
717
|
+
context.registerToolEventRecipient(activeRunId, connId);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
}
|
|
364
721
|
},
|
|
365
|
-
onModelSelected
|
|
366
|
-
prefixContext.provider = ctx.provider;
|
|
367
|
-
prefixContext.model = extractShortModelName(ctx.model);
|
|
368
|
-
prefixContext.modelFull = `${ctx.provider}/${ctx.model}`;
|
|
369
|
-
prefixContext.thinkingLevel = ctx.thinkLevel ?? "off";
|
|
370
|
-
},
|
|
722
|
+
onModelSelected,
|
|
371
723
|
},
|
|
372
724
|
})
|
|
373
725
|
.then(() => {
|
|
@@ -379,13 +731,14 @@ export const chatHandlers = {
|
|
|
379
731
|
.trim();
|
|
380
732
|
let message;
|
|
381
733
|
if (combinedReply) {
|
|
382
|
-
const { storePath: latestStorePath, entry: latestEntry } = loadSessionEntry(
|
|
734
|
+
const { storePath: latestStorePath, entry: latestEntry } = loadSessionEntry(sessionKey);
|
|
383
735
|
const sessionId = latestEntry?.sessionId ?? entry?.sessionId ?? clientRunId;
|
|
384
736
|
const appended = appendAssistantTranscriptMessage({
|
|
385
737
|
message: combinedReply,
|
|
386
738
|
sessionId,
|
|
387
739
|
storePath: latestStorePath,
|
|
388
740
|
sessionFile: latestEntry?.sessionFile,
|
|
741
|
+
agentId,
|
|
389
742
|
createIfMissing: true,
|
|
390
743
|
});
|
|
391
744
|
if (appended.ok) {
|
|
@@ -398,7 +751,9 @@ export const chatHandlers = {
|
|
|
398
751
|
role: "assistant",
|
|
399
752
|
content: [{ type: "text", text: combinedReply }],
|
|
400
753
|
timestamp: now,
|
|
401
|
-
stopReason
|
|
754
|
+
// Keep this compatible with Pi stopReason enums even though this message isn't
|
|
755
|
+
// persisted to the transcript due to the append failure.
|
|
756
|
+
stopReason: "stop",
|
|
402
757
|
usage: { input: 0, output: 0, totalTokens: 0 },
|
|
403
758
|
};
|
|
404
759
|
}
|
|
@@ -406,7 +761,7 @@ export const chatHandlers = {
|
|
|
406
761
|
broadcastChatFinal({
|
|
407
762
|
context,
|
|
408
763
|
runId: clientRunId,
|
|
409
|
-
sessionKey:
|
|
764
|
+
sessionKey: rawSessionKey,
|
|
410
765
|
message,
|
|
411
766
|
});
|
|
412
767
|
}
|
|
@@ -431,7 +786,7 @@ export const chatHandlers = {
|
|
|
431
786
|
broadcastChatError({
|
|
432
787
|
context,
|
|
433
788
|
runId: clientRunId,
|
|
434
|
-
sessionKey:
|
|
789
|
+
sessionKey: rawSessionKey,
|
|
435
790
|
errorMessage: String(err),
|
|
436
791
|
});
|
|
437
792
|
})
|
|
@@ -465,56 +820,36 @@ export const chatHandlers = {
|
|
|
465
820
|
}
|
|
466
821
|
const p = params;
|
|
467
822
|
// Load session to find transcript file
|
|
468
|
-
const
|
|
823
|
+
const rawSessionKey = p.sessionKey;
|
|
824
|
+
const { cfg, storePath, entry } = loadSessionEntry(rawSessionKey);
|
|
469
825
|
const sessionId = entry?.sessionId;
|
|
470
826
|
if (!sessionId || !storePath) {
|
|
471
827
|
respond(false, undefined, errorShape(ErrorCodes.INVALID_REQUEST, "session not found"));
|
|
472
828
|
return;
|
|
473
829
|
}
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
const labelPrefix = p.label ? `[${p.label}]\n\n` : "";
|
|
486
|
-
const messageBody = {
|
|
487
|
-
role: "assistant",
|
|
488
|
-
content: [{ type: "text", text: `${labelPrefix}${p.message}` }],
|
|
489
|
-
timestamp: now,
|
|
490
|
-
stopReason: "injected",
|
|
491
|
-
usage: { input: 0, output: 0, totalTokens: 0 },
|
|
492
|
-
};
|
|
493
|
-
const transcriptEntry = {
|
|
494
|
-
type: "message",
|
|
495
|
-
id: messageId,
|
|
496
|
-
timestamp: new Date(now).toISOString(),
|
|
497
|
-
message: messageBody,
|
|
498
|
-
};
|
|
499
|
-
// Append to transcript file
|
|
500
|
-
try {
|
|
501
|
-
fs.appendFileSync(transcriptPath, `${JSON.stringify(transcriptEntry)}\n`, "utf-8");
|
|
502
|
-
}
|
|
503
|
-
catch (err) {
|
|
504
|
-
const errMessage = err instanceof Error ? err.message : String(err);
|
|
505
|
-
respond(false, undefined, errorShape(ErrorCodes.UNAVAILABLE, `failed to write transcript: ${errMessage}`));
|
|
830
|
+
const appended = appendAssistantTranscriptMessage({
|
|
831
|
+
message: p.message,
|
|
832
|
+
label: p.label,
|
|
833
|
+
sessionId,
|
|
834
|
+
storePath,
|
|
835
|
+
sessionFile: entry?.sessionFile,
|
|
836
|
+
agentId: resolveSessionAgentId({ sessionKey: rawSessionKey, config: cfg }),
|
|
837
|
+
createIfMissing: false,
|
|
838
|
+
});
|
|
839
|
+
if (!appended.ok || !appended.messageId || !appended.message) {
|
|
840
|
+
respond(false, undefined, errorShape(ErrorCodes.UNAVAILABLE, `failed to write transcript: ${appended.error ?? "unknown error"}`));
|
|
506
841
|
return;
|
|
507
842
|
}
|
|
508
843
|
// Broadcast to webchat for immediate UI update
|
|
509
844
|
const chatPayload = {
|
|
510
|
-
runId: `inject-${messageId}`,
|
|
511
|
-
sessionKey:
|
|
845
|
+
runId: `inject-${appended.messageId}`,
|
|
846
|
+
sessionKey: rawSessionKey,
|
|
512
847
|
seq: 0,
|
|
513
848
|
state: "final",
|
|
514
|
-
message:
|
|
849
|
+
message: appended.message,
|
|
515
850
|
};
|
|
516
851
|
context.broadcast("chat", chatPayload);
|
|
517
|
-
context.nodeSendToSession(
|
|
518
|
-
respond(true, { ok: true, messageId });
|
|
852
|
+
context.nodeSendToSession(rawSessionKey, "chat", chatPayload);
|
|
853
|
+
respond(true, { ok: true, messageId: appended.messageId });
|
|
519
854
|
},
|
|
520
855
|
};
|