@oyasmi/pipiclaw 0.9.4 → 0.9.5-beta.3
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 +35 -31
- package/README.md +34 -30
- package/dist/agent/builtin-commands.d.ts +7 -2
- package/dist/agent/builtin-commands.js +19 -23
- package/dist/agent/channel-runner.d.ts +23 -35
- package/dist/agent/channel-runner.js +150 -89
- package/dist/agent/maintenance-context.d.ts +8 -5
- package/dist/agent/maintenance-context.js +30 -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 +11 -9
- package/dist/agent/prompt/types.d.ts +1 -1
- package/dist/agent/runner-factory.d.ts +6 -6
- package/dist/agent/runner-factory.js +7 -8
- package/dist/agent/session/command-extension.js +11 -0
- 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 +162 -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/execution-trace.d.ts +14 -0
- package/dist/agent/session/execution-trace.js +57 -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 +4 -8
- package/dist/agent/session/session-events.js +80 -70
- package/dist/agent/session/session-host.d.ts +39 -61
- package/dist/agent/session/session-host.js +122 -124
- 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 +5 -4
- package/dist/agent/turn/task-digest.js +37 -34
- package/dist/agent/turn/turn-context.d.ts +12 -8
- package/dist/agent/turn/turn-context.js +27 -19
- package/dist/agent/turn/turn-finish.d.ts +18 -6
- package/dist/agent/turn/turn-finish.js +52 -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 +15 -10
- package/dist/agent/turn/turn-pipeline.js +86 -59
- 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 +93 -24
- package/dist/agent/types.js +5 -1
- package/dist/app/auth-command.js +1 -1
- package/dist/app/home.js +34 -19
- 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 +49 -47
- 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 +93 -0
- package/dist/channel/ingress.js +5 -0
- package/dist/channel/store.d.ts +14 -16
- 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 +48 -49
- package/dist/commands/router.d.ts +28 -14
- package/dist/commands/router.js +30 -27
- package/dist/config/settings.d.ts +8 -14
- package/dist/config/settings.js +0 -7
- package/dist/config/tools.d.ts +2 -2
- package/dist/config/tools.js +2 -2
- 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 +20 -35
- package/dist/dingtalk/bot.js +25 -204
- package/dist/dingtalk/cards.d.ts +7 -0
- package/dist/dingtalk/cards.js +32 -2
- 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 +34 -14
- package/dist/dingtalk/delivery.js +332 -190
- 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 +57 -0
- package/dist/dingtalk/progress.js +199 -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/file-store.d.ts +4 -0
- package/dist/file-store.js +6 -1
- package/dist/index.d.ts +4 -2
- package/dist/jobs/completion-observer.d.ts +20 -0
- package/dist/jobs/completion-observer.js +107 -0
- package/dist/jobs/completion-signal.d.ts +9 -0
- package/dist/jobs/completion-signal.js +32 -0
- package/dist/jobs/completion-wake.d.ts +14 -0
- package/dist/jobs/completion-wake.js +25 -0
- package/dist/jobs/manager.d.ts +29 -42
- package/dist/jobs/manager.js +130 -236
- package/dist/jobs/probe.d.ts +3 -0
- package/dist/jobs/probe.js +42 -0
- package/dist/jobs/record.d.ts +5 -6
- package/dist/jobs/record.js +2 -0
- package/dist/jobs/runtime.d.ts +28 -0
- package/dist/jobs/runtime.js +69 -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 +19 -0
- package/dist/memory/boundary-work.js +204 -0
- package/dist/memory/commands.d.ts +4 -3
- package/dist/memory/commands.js +20 -17
- 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 +21 -18
- package/dist/memory/journal.d.ts +6 -6
- package/dist/memory/journal.js +16 -15
- package/dist/memory/lifecycle.d.ts +17 -28
- package/dist/memory/lifecycle.js +87 -147
- 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 -10
- package/dist/memory/reflect-job.js +29 -44
- package/dist/memory/reflect.d.ts +6 -1
- package/dist/memory/reflect.js +44 -16
- package/dist/memory/render.d.ts +1 -1
- package/dist/memory/render.js +2 -2
- package/dist/memory/review-log.d.ts +17 -4
- package/dist/memory/review-log.js +54 -5
- package/dist/memory/scheduler.d.ts +12 -19
- package/dist/memory/scheduler.js +28 -64
- package/dist/memory/search.js +7 -4
- 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 +18 -7
- package/dist/memory/source-window.js +59 -29
- package/dist/memory/store.d.ts +14 -12
- package/dist/memory/store.js +89 -57
- package/dist/paths.d.ts +2 -0
- package/dist/paths.js +2 -0
- package/dist/playbooks/agent-delegation.md +23 -17
- package/dist/playbooks/background-jobs.md +7 -5
- package/dist/playbooks/event-scheduling.md +9 -7
- package/dist/playbooks/memory-and-learning.md +18 -14
- package/dist/playbooks/outbound-media.md +3 -3
- package/dist/playbooks/runtime-orientation.md +39 -23
- package/dist/playbooks/task-lead.md +57 -34
- package/dist/runtime/bootstrap.d.ts +45 -35
- package/dist/runtime/bootstrap.js +372 -167
- package/dist/runtime/channel-queue.d.ts +25 -0
- package/dist/{dingtalk → runtime}/channel-queue.js +25 -10
- 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 +18 -5
- package/dist/runtime/durable-dispatch.js +85 -14
- package/dist/runtime/inbound-images.d.ts +32 -10
- package/dist/runtime/inbound-images.js +95 -21
- package/dist/runtime/ingress-busy.d.ts +25 -0
- package/dist/runtime/ingress-busy.js +178 -0
- package/dist/runtime/ingress-commands.d.ts +7 -0
- package/dist/runtime/ingress-commands.js +170 -0
- package/dist/runtime/ingress-deps.d.ts +59 -0
- package/dist/runtime/ingress-deps.js +1 -0
- package/dist/runtime/ingress.d.ts +22 -0
- package/dist/runtime/ingress.js +386 -0
- package/dist/runtime/request-execution.d.ts +19 -0
- package/dist/runtime/request-execution.js +88 -0
- package/dist/runtime/shutdown.d.ts +14 -6
- package/dist/runtime/shutdown.js +11 -5
- package/dist/runtime/task-close.d.ts +4 -0
- package/dist/runtime/task-close.js +102 -0
- package/dist/runtime/task-control.d.ts +34 -0
- package/dist/runtime/task-control.js +225 -0
- package/dist/runtime/task-input.d.ts +13 -0
- package/dist/runtime/task-input.js +129 -0
- package/dist/runtime/task-lane-types.d.ts +123 -0
- package/dist/runtime/task-lane-types.js +14 -0
- package/dist/runtime/task-lane.d.ts +99 -0
- package/dist/runtime/task-lane.js +703 -0
- package/dist/runtime/task-pending.d.ts +8 -0
- package/dist/runtime/task-pending.js +40 -0
- package/dist/runtime/task-spawn.d.ts +8 -8
- package/dist/runtime/task-spawn.js +10 -9
- package/dist/runtime/task-step.d.ts +32 -0
- package/dist/runtime/task-step.js +276 -0
- package/dist/runtime/transports.d.ts +32 -0
- package/dist/runtime/transports.js +101 -0
- package/dist/runtime/turn-run.d.ts +20 -23
- package/dist/runtime/turn-run.js +252 -159
- package/dist/runtime/wake-coalescing.d.ts +12 -6
- package/dist/runtime/wake-coalescing.js +12 -4
- 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/format.d.ts +0 -6
- package/dist/shared/format.js +0 -17
- package/dist/shared/host-process.d.ts +2 -0
- package/dist/shared/host-process.js +16 -1
- package/dist/shared/jsonl-appender.d.ts +5 -0
- package/dist/shared/jsonl-appender.js +25 -2
- package/dist/shared/with-timeout.d.ts +2 -0
- package/dist/shared/with-timeout.js +21 -0
- package/dist/skills/catalog.d.ts +9 -0
- package/dist/skills/catalog.js +34 -0
- package/dist/skills/commands.d.ts +3 -12
- package/dist/skills/commands.js +36 -22
- package/dist/skills/loader.d.ts +2 -31
- package/dist/skills/loader.js +101 -68
- package/dist/skills/roots.d.ts +12 -0
- package/dist/skills/roots.js +18 -0
- package/dist/skills/types.d.ts +24 -0
- package/dist/skills/types.js +1 -0
- package/dist/subagents/completion-wake.d.ts +11 -2
- package/dist/subagents/completion-wake.js +21 -18
- package/dist/subagents/discovery.d.ts +1 -1
- package/dist/subagents/discovery.js +10 -16
- package/dist/subagents/dispatch.d.ts +0 -10
- package/dist/subagents/dispatch.js +18 -44
- package/dist/subagents/external/cursor-cli.d.ts +2 -0
- package/dist/subagents/external/cursor-cli.js +256 -0
- package/dist/subagents/external/dispatch.js +1 -1
- package/dist/subagents/external/harness.d.ts +5 -5
- package/dist/subagents/external/reconciliation.d.ts +7 -0
- package/dist/subagents/external/reconciliation.js +105 -0
- package/dist/subagents/external/recovery.d.ts +20 -0
- package/dist/subagents/external/recovery.js +71 -0
- package/dist/subagents/external/registry.js +2 -0
- package/dist/subagents/external/role-warnings.d.ts +17 -0
- package/dist/subagents/external/role-warnings.js +42 -0
- package/dist/subagents/external/run.d.ts +1 -1
- package/dist/subagents/external/run.js +21 -25
- package/dist/subagents/format.d.ts +1 -1
- package/dist/subagents/format.js +1 -1
- package/dist/subagents/grace.d.ts +2 -2
- package/dist/subagents/grace.js +3 -3
- package/dist/subagents/internal/run.js +8 -6
- package/dist/subagents/internal/worker.js +37 -16
- package/dist/subagents/result.js +2 -3
- package/dist/subagents/run-archive.d.ts +11 -0
- package/dist/subagents/run-archive.js +49 -0
- package/dist/subagents/run-record.d.ts +13 -15
- package/dist/subagents/run-record.js +5 -5
- package/dist/subagents/run-registry.d.ts +5 -3
- package/dist/subagents/run-registry.js +22 -45
- package/dist/subagents/runs.d.ts +51 -54
- package/dist/subagents/runs.js +178 -304
- package/dist/subagents/task-envelope.js +9 -11
- package/dist/subagents/task-settlement.d.ts +3 -0
- package/dist/subagents/task-settlement.js +20 -0
- package/dist/subagents/types.d.ts +7 -7
- package/dist/subagents/workspace-summary.js +5 -1
- package/dist/tasks/archive.d.ts +11 -0
- package/dist/tasks/archive.js +141 -0
- package/dist/tasks/board.d.ts +5 -12
- package/dist/tasks/board.js +4 -30
- package/dist/tasks/bound-work.d.ts +13 -7
- package/dist/tasks/bound-work.js +41 -28
- package/dist/tasks/brief.d.ts +39 -15
- package/dist/tasks/brief.js +131 -73
- package/dist/tasks/budget.d.ts +3 -3
- package/dist/tasks/budget.js +9 -4
- package/dist/tasks/commands.d.ts +24 -10
- package/dist/tasks/commands.js +225 -131
- package/dist/tasks/completion.d.ts +12 -0
- package/dist/tasks/completion.js +35 -0
- package/dist/tasks/contract.d.ts +8 -19
- package/dist/tasks/contract.js +41 -45
- package/dist/tasks/control.d.ts +138 -0
- package/dist/tasks/control.js +1 -0
- package/dist/tasks/create.d.ts +4 -2
- package/dist/tasks/create.js +13 -7
- package/dist/tasks/delivery.d.ts +24 -0
- package/dist/tasks/delivery.js +221 -0
- package/dist/tasks/file-mutation.d.ts +5 -0
- package/dist/tasks/file-mutation.js +38 -0
- package/dist/tasks/frontmatter.d.ts +46 -27
- package/dist/tasks/frontmatter.js +90 -47
- package/dist/tasks/input-receipts.d.ts +7 -0
- package/dist/tasks/input-receipts.js +60 -0
- package/dist/tasks/loop-log.d.ts +150 -37
- package/dist/tasks/loop-log.js +250 -42
- package/dist/tasks/mutation-lock.d.ts +7 -5
- package/dist/tasks/mutation-lock.js +32 -9
- package/dist/tasks/receipts.d.ts +19 -0
- package/dist/tasks/receipts.js +48 -0
- package/dist/tasks/session-path.d.ts +3 -3
- package/dist/tasks/session-path.js +5 -5
- package/dist/tasks/state.d.ts +93 -0
- package/dist/tasks/state.js +156 -0
- package/dist/tasks/store.d.ts +86 -54
- package/dist/tasks/store.js +256 -156
- package/dist/tools/bash.d.ts +8 -10
- package/dist/tools/bash.js +27 -40
- package/dist/tools/contract/details.d.ts +6 -6
- package/dist/tools/contract/details.js +6 -6
- package/dist/tools/contract/execution-trace.d.ts +4 -0
- package/dist/tools/contract/execution-trace.js +23 -0
- package/dist/tools/contract/names.d.ts +7 -0
- package/dist/tools/contract/names.js +22 -0
- package/dist/tools/contract/presentation.js +6 -2
- package/dist/tools/edit.d.ts +1 -0
- package/dist/tools/edit.js +46 -43
- 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 +17 -20
- package/dist/tools/index.js +28 -22
- 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 +32 -25
- package/dist/tools/read.js +1 -1
- package/dist/tools/registry.d.ts +19 -19
- package/dist/tools/registry.js +65 -34
- 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/skill.d.ts +2 -2
- package/dist/tools/skill.js +24 -10
- package/dist/tools/subagent-manage.d.ts +2 -8
- package/dist/tools/subagent-manage.js +26 -19
- package/dist/tools/subagent.d.ts +0 -2
- package/dist/tools/subagent.js +3 -8
- package/dist/tools/task-manage/control-tools.d.ts +4 -0
- package/dist/tools/task-manage/control-tools.js +65 -0
- package/dist/tools/task-manage/create.js +3 -2
- package/dist/tools/task-manage/factory.d.ts +12 -6
- package/dist/tools/task-manage/factory.js +73 -58
- package/dist/tools/task-manage/lifecycle.d.ts +7 -12
- package/dist/tools/task-manage/lifecycle.js +119 -65
- package/dist/tools/task-manage/log-view.js +10 -7
- package/dist/tools/task-manage/schema.d.ts +21 -26
- package/dist/tools/task-manage/schema.js +29 -35
- package/dist/tools/task-manage/shared.d.ts +8 -39
- package/dist/tools/task-manage/shared.js +20 -69
- package/dist/tools/task-manage/step-tools.d.ts +5 -0
- package/dist/tools/task-manage/step-tools.js +103 -0
- package/dist/tools/task-manage/types.d.ts +33 -21
- 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.d.ts +1 -0
- package/dist/tools/write-content.js +7 -2
- package/dist/tools/write.d.ts +1 -0
- package/dist/tools/write.js +1 -0
- 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 +9 -4
- package/dist/usage/ledger.js +2 -0
- package/dist/usage/render.d.ts +6 -1
- package/dist/usage/render.js +20 -15
- package/dist/web/search-providers.js +0 -33
- 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/examples/sub-agents/README.md +23 -16
- package/examples/sub-agents/agents/claude-high.md +1 -1
- package/examples/sub-agents/agents/claude-main.md +1 -1
- package/examples/sub-agents/agents/codex-flash.md +1 -1
- package/examples/sub-agents/agents/codex-high.md +1 -1
- package/examples/sub-agents/agents/codex-main.md +4 -4
- package/examples/sub-agents/agents/cursor-high.md +22 -0
- package/examples/sub-agents/agents/cursor-main.md +22 -0
- package/examples/sub-agents/agents/glm-flash.md +1 -1
- package/examples/sub-agents/agents/glm-high.md +2 -2
- package/examples/sub-agents/agents/scout.md +30 -0
- package/package.json +13 -7
- 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-driver.d.ts +0 -100
- package/dist/runtime/task-driver.js +0 -417
- package/dist/runtime/task-migration.d.ts +0 -30
- package/dist/runtime/task-migration.js +0 -465
- package/dist/runtime/task-wake.d.ts +0 -46
- package/dist/runtime/task-wake.js +0 -32
- 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/shared/wake-claim.d.ts +0 -23
- package/dist/shared/wake-claim.js +0 -26
- package/dist/skills/validation.d.ts +0 -8
- package/dist/skills/validation.js +0 -91
- package/dist/skills/workspace.d.ts +0 -33
- package/dist/skills/workspace.js +0 -116
- package/dist/tasks/steer.d.ts +0 -8
- package/dist/tasks/steer.js +0 -79
- package/dist/tasks/ticket.d.ts +0 -71
- package/dist/tasks/ticket.js +0 -138
- package/dist/tools/task-manage/step-end.d.ts +0 -11
- package/dist/tools/task-manage/step-end.js +0 -117
- 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
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import * as log from "../log.js";
|
|
2
|
+
import { errorMessage } from "../shared/text-utils.js";
|
|
3
|
+
import { newId } from "./ids.js";
|
|
4
|
+
import { MAX_TOOL_OUTPUT_CHARS } from "./limits.js";
|
|
5
|
+
import { isTerminal, nowSeconds, toApiResponse } from "./response-record.js";
|
|
6
|
+
const SERVER_LABEL = "pipiclaw";
|
|
7
|
+
export class LiveResponse {
|
|
8
|
+
constructor(record, hooks,
|
|
9
|
+
/** Created with `background` and `stream`: a client may resume the stream while the log is kept. */
|
|
10
|
+
replayable = false) {
|
|
11
|
+
this.record = record;
|
|
12
|
+
this.hooks = hooks;
|
|
13
|
+
this.replayable = replayable;
|
|
14
|
+
this.log = [];
|
|
15
|
+
/** Events produced before the response was announced; they follow `response.created` in the log. */
|
|
16
|
+
this.deferred = [];
|
|
17
|
+
this.announced = false;
|
|
18
|
+
this.subscribers = new Set();
|
|
19
|
+
this.toolItems = new Map();
|
|
20
|
+
this.persisting = Promise.resolve();
|
|
21
|
+
// ---- internals -------------------------------------------------------------------------
|
|
22
|
+
this.endedFlag = false;
|
|
23
|
+
this.ended = new Promise((resolve) => {
|
|
24
|
+
this.resolveEnded = resolve;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
get id() {
|
|
28
|
+
return this.record.id;
|
|
29
|
+
}
|
|
30
|
+
get status() {
|
|
31
|
+
return this.record.status;
|
|
32
|
+
}
|
|
33
|
+
get terminal() {
|
|
34
|
+
return isTerminal(this.record.status);
|
|
35
|
+
}
|
|
36
|
+
/** Resolves once the response has reached a terminal state. */
|
|
37
|
+
whenEnded() {
|
|
38
|
+
return this.ended;
|
|
39
|
+
}
|
|
40
|
+
/** Resolves once the last snapshot has been written (tests and shutdown). */
|
|
41
|
+
whenPersisted() {
|
|
42
|
+
return this.persisting;
|
|
43
|
+
}
|
|
44
|
+
snapshot() {
|
|
45
|
+
return toApiResponse(this.record);
|
|
46
|
+
}
|
|
47
|
+
// ---- lifecycle -------------------------------------------------------------------------
|
|
48
|
+
/**
|
|
49
|
+
* Make the response visible: `response.created` (and `queued` or `in_progress`) open the log,
|
|
50
|
+
* and whatever the turn already produced follows. Nothing is stored or announced before this.
|
|
51
|
+
*/
|
|
52
|
+
announce(initial) {
|
|
53
|
+
if (this.announced)
|
|
54
|
+
return;
|
|
55
|
+
if (!this.terminal)
|
|
56
|
+
this.record.status = initial;
|
|
57
|
+
this.announced = true;
|
|
58
|
+
const skeleton = toApiResponse(this.record, { skeleton: initial });
|
|
59
|
+
this.commit({ type: "response.created", response: skeleton });
|
|
60
|
+
this.commit(initial === "queued"
|
|
61
|
+
? { type: "response.queued", response: skeleton }
|
|
62
|
+
: { type: "response.in_progress", response: skeleton });
|
|
63
|
+
for (const pending of this.deferred.splice(0))
|
|
64
|
+
this.commit(pending);
|
|
65
|
+
this.save();
|
|
66
|
+
}
|
|
67
|
+
/** A queued response whose turn begins. */
|
|
68
|
+
start() {
|
|
69
|
+
if (this.record.status !== "queued")
|
|
70
|
+
return;
|
|
71
|
+
this.record.status = "in_progress";
|
|
72
|
+
this.emit({ type: "response.in_progress", response: toApiResponse(this.record, { skeleton: "in_progress" }) });
|
|
73
|
+
}
|
|
74
|
+
complete(usage) {
|
|
75
|
+
if (this.terminal)
|
|
76
|
+
return;
|
|
77
|
+
this.closeDanglingCalls();
|
|
78
|
+
this.record.status = "completed";
|
|
79
|
+
this.record.usage = usage;
|
|
80
|
+
this.record.completed_at = nowSeconds();
|
|
81
|
+
this.emit({ type: "response.completed", response: this.snapshot() });
|
|
82
|
+
this.settle();
|
|
83
|
+
}
|
|
84
|
+
fail(message, usage) {
|
|
85
|
+
if (this.terminal)
|
|
86
|
+
return;
|
|
87
|
+
this.closeDanglingCalls();
|
|
88
|
+
this.record.status = "failed";
|
|
89
|
+
this.record.error = { code: "server_error", message };
|
|
90
|
+
if (usage)
|
|
91
|
+
this.record.usage = usage;
|
|
92
|
+
this.record.completed_at = nowSeconds();
|
|
93
|
+
this.emit({ type: "error", code: "server_error", message, param: null });
|
|
94
|
+
this.emit({ type: "response.failed", response: this.snapshot() });
|
|
95
|
+
this.settle();
|
|
96
|
+
}
|
|
97
|
+
/** The response stopped before it was done (steering took over): `incomplete` with the reason. */
|
|
98
|
+
incomplete(reason, usage) {
|
|
99
|
+
if (this.terminal)
|
|
100
|
+
return;
|
|
101
|
+
this.closeDanglingCalls();
|
|
102
|
+
this.record.status = "incomplete";
|
|
103
|
+
this.record.incomplete_details = { reason };
|
|
104
|
+
if (usage)
|
|
105
|
+
this.record.usage = usage;
|
|
106
|
+
this.record.completed_at = nowSeconds();
|
|
107
|
+
this.emit({ type: "response.incomplete", response: this.snapshot() });
|
|
108
|
+
this.settle();
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Cancelled by the caller. The API has no event for it: the stream ends where it is, and the
|
|
112
|
+
* outcome is read from the response object. Returns whether anything changed.
|
|
113
|
+
*/
|
|
114
|
+
cancel() {
|
|
115
|
+
if (this.terminal)
|
|
116
|
+
return false;
|
|
117
|
+
this.markDanglingCallsIncomplete();
|
|
118
|
+
this.record.status = "cancelled";
|
|
119
|
+
this.record.completed_at = null;
|
|
120
|
+
this.settle();
|
|
121
|
+
this.endSubscribers();
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
// ---- items -----------------------------------------------------------------------------
|
|
125
|
+
/** A tool call begins: an `mcp_call` item, in progress, with its arguments. */
|
|
126
|
+
beginToolCall(callId, name, args) {
|
|
127
|
+
if (this.terminal || this.toolItems.has(callId))
|
|
128
|
+
return;
|
|
129
|
+
const item = {
|
|
130
|
+
id: newId("mcp"),
|
|
131
|
+
type: "mcp_call",
|
|
132
|
+
server_label: SERVER_LABEL,
|
|
133
|
+
name,
|
|
134
|
+
arguments: JSON.stringify(args ?? {}) ?? "{}",
|
|
135
|
+
status: "in_progress",
|
|
136
|
+
output: null,
|
|
137
|
+
error: null,
|
|
138
|
+
};
|
|
139
|
+
const index = this.record.output.push(item) - 1;
|
|
140
|
+
this.toolItems.set(callId, index);
|
|
141
|
+
this.emit({ type: "response.output_item.added", output_index: index, item: structuredClone(item) });
|
|
142
|
+
this.emit({
|
|
143
|
+
type: "response.mcp_call_arguments.done",
|
|
144
|
+
output_index: index,
|
|
145
|
+
item_id: item.id,
|
|
146
|
+
arguments: item.arguments,
|
|
147
|
+
});
|
|
148
|
+
this.emit({ type: "response.mcp_call.in_progress", output_index: index, item_id: item.id });
|
|
149
|
+
}
|
|
150
|
+
endToolCall(callId, ok, output, failure) {
|
|
151
|
+
const index = this.toolItems.get(callId);
|
|
152
|
+
if (index === undefined || this.terminal)
|
|
153
|
+
return;
|
|
154
|
+
this.toolItems.delete(callId);
|
|
155
|
+
const item = this.record.output[index];
|
|
156
|
+
const text = output.length > MAX_TOOL_OUTPUT_CHARS ? `${output.slice(0, MAX_TOOL_OUTPUT_CHARS)}…` : output;
|
|
157
|
+
item.output = text;
|
|
158
|
+
item.status = ok ? "completed" : "failed";
|
|
159
|
+
item.error = ok ? null : { type: "mcp_tool_execution_error", content: text };
|
|
160
|
+
if (!ok && failure)
|
|
161
|
+
item.pipiclaw_failure = failure;
|
|
162
|
+
this.emit({
|
|
163
|
+
type: ok ? "response.mcp_call.completed" : "response.mcp_call.failed",
|
|
164
|
+
output_index: index,
|
|
165
|
+
item_id: item.id,
|
|
166
|
+
});
|
|
167
|
+
this.emit({ type: "response.output_item.done", output_index: index, item: structuredClone(item) });
|
|
168
|
+
}
|
|
169
|
+
/** The model's reasoning, as a reasoning item with a one-part summary. */
|
|
170
|
+
addReasoning(text) {
|
|
171
|
+
if (this.terminal || !text.trim())
|
|
172
|
+
return;
|
|
173
|
+
const item = { id: newId("rs"), type: "reasoning", summary: [], status: "in_progress" };
|
|
174
|
+
const index = this.record.output.push(item) - 1;
|
|
175
|
+
this.emit({ type: "response.output_item.added", output_index: index, item: structuredClone(item) });
|
|
176
|
+
this.emit({
|
|
177
|
+
type: "response.reasoning_summary_part.added",
|
|
178
|
+
output_index: index,
|
|
179
|
+
item_id: item.id,
|
|
180
|
+
summary_index: 0,
|
|
181
|
+
part: { type: "summary_text", text: "" },
|
|
182
|
+
});
|
|
183
|
+
this.emit({
|
|
184
|
+
type: "response.reasoning_summary_text.delta",
|
|
185
|
+
output_index: index,
|
|
186
|
+
item_id: item.id,
|
|
187
|
+
summary_index: 0,
|
|
188
|
+
delta: text,
|
|
189
|
+
});
|
|
190
|
+
this.emit({
|
|
191
|
+
type: "response.reasoning_summary_text.done",
|
|
192
|
+
output_index: index,
|
|
193
|
+
item_id: item.id,
|
|
194
|
+
summary_index: 0,
|
|
195
|
+
text,
|
|
196
|
+
});
|
|
197
|
+
const part = { type: "summary_text", text };
|
|
198
|
+
this.emit({
|
|
199
|
+
type: "response.reasoning_summary_part.done",
|
|
200
|
+
output_index: index,
|
|
201
|
+
item_id: item.id,
|
|
202
|
+
summary_index: 0,
|
|
203
|
+
part,
|
|
204
|
+
});
|
|
205
|
+
item.summary = [part];
|
|
206
|
+
item.status = "completed";
|
|
207
|
+
this.emit({ type: "response.output_item.done", output_index: index, item: structuredClone(item) });
|
|
208
|
+
}
|
|
209
|
+
/** A finished assistant message: commentary between tool calls, or the final answer. */
|
|
210
|
+
addMessage(phase, text, level) {
|
|
211
|
+
if (this.terminal || !text.trim())
|
|
212
|
+
return;
|
|
213
|
+
const id = newId("msg");
|
|
214
|
+
const empty = {
|
|
215
|
+
id,
|
|
216
|
+
type: "message",
|
|
217
|
+
role: "assistant",
|
|
218
|
+
status: "in_progress",
|
|
219
|
+
phase,
|
|
220
|
+
content: [],
|
|
221
|
+
...(level ? { pipiclaw_level: level } : {}),
|
|
222
|
+
};
|
|
223
|
+
const index = this.record.output.push(empty) - 1;
|
|
224
|
+
this.emit({ type: "response.output_item.added", output_index: index, item: structuredClone(empty) });
|
|
225
|
+
this.emit({
|
|
226
|
+
type: "response.content_part.added",
|
|
227
|
+
output_index: index,
|
|
228
|
+
item_id: id,
|
|
229
|
+
content_index: 0,
|
|
230
|
+
part: { type: "output_text", text: "", annotations: [] },
|
|
231
|
+
});
|
|
232
|
+
this.emit({
|
|
233
|
+
type: "response.output_text.delta",
|
|
234
|
+
output_index: index,
|
|
235
|
+
item_id: id,
|
|
236
|
+
content_index: 0,
|
|
237
|
+
delta: text,
|
|
238
|
+
logprobs: [],
|
|
239
|
+
});
|
|
240
|
+
this.emit({
|
|
241
|
+
type: "response.output_text.done",
|
|
242
|
+
output_index: index,
|
|
243
|
+
item_id: id,
|
|
244
|
+
content_index: 0,
|
|
245
|
+
text,
|
|
246
|
+
logprobs: [],
|
|
247
|
+
});
|
|
248
|
+
const part = { type: "output_text", text, annotations: [] };
|
|
249
|
+
this.emit({ type: "response.content_part.done", output_index: index, item_id: id, content_index: 0, part });
|
|
250
|
+
empty.content = [part];
|
|
251
|
+
empty.status = "completed";
|
|
252
|
+
this.emit({ type: "response.output_item.done", output_index: index, item: structuredClone(empty) });
|
|
253
|
+
}
|
|
254
|
+
/** Output items produced so far, for a caller that needs to know whether the turn said anything. */
|
|
255
|
+
get hasOutput() {
|
|
256
|
+
return this.record.output.length > 0;
|
|
257
|
+
}
|
|
258
|
+
// ---- streams ---------------------------------------------------------------------------
|
|
259
|
+
/**
|
|
260
|
+
* Replay the log from `fromSequence` (the first event to deliver), then follow it live. Returns
|
|
261
|
+
* the way to stop following. A response that has already ended is replayed and then `onEnd`.
|
|
262
|
+
*/
|
|
263
|
+
subscribe(fromSequence, subscriber) {
|
|
264
|
+
for (const event of this.log.slice(Math.max(0, fromSequence)))
|
|
265
|
+
subscriber.onEvent(event);
|
|
266
|
+
if (this.endedFlag) {
|
|
267
|
+
subscriber.onEnd();
|
|
268
|
+
return () => undefined;
|
|
269
|
+
}
|
|
270
|
+
this.subscribers.add(subscriber);
|
|
271
|
+
return () => {
|
|
272
|
+
this.subscribers.delete(subscriber);
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
/** Whether events from `sequence` on can still be replayed (the log is whole from the start). */
|
|
276
|
+
get logLength() {
|
|
277
|
+
return this.log.length;
|
|
278
|
+
}
|
|
279
|
+
emit(event) {
|
|
280
|
+
if (this.announced)
|
|
281
|
+
this.commit(event);
|
|
282
|
+
else
|
|
283
|
+
this.deferred.push(event);
|
|
284
|
+
}
|
|
285
|
+
commit(event) {
|
|
286
|
+
const logged = { ...event, sequence_number: this.log.length };
|
|
287
|
+
this.log.push(logged);
|
|
288
|
+
for (const subscriber of [...this.subscribers]) {
|
|
289
|
+
try {
|
|
290
|
+
subscriber.onEvent(logged);
|
|
291
|
+
}
|
|
292
|
+
catch (error) {
|
|
293
|
+
log.logWarn("webapi.stream.subscriber_failed", "Stream subscriber failed", {
|
|
294
|
+
reason: errorMessage(error),
|
|
295
|
+
fields: { responseId: this.id },
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
if (isTerminalEvent(logged) && this.terminal)
|
|
300
|
+
this.endSubscribers();
|
|
301
|
+
}
|
|
302
|
+
endSubscribers() {
|
|
303
|
+
if (this.endedFlag)
|
|
304
|
+
return;
|
|
305
|
+
this.endedFlag = true;
|
|
306
|
+
for (const subscriber of [...this.subscribers])
|
|
307
|
+
subscriber.onEnd();
|
|
308
|
+
this.subscribers.clear();
|
|
309
|
+
}
|
|
310
|
+
/** The response is over: store the final record and let waiters go. */
|
|
311
|
+
settle() {
|
|
312
|
+
this.save();
|
|
313
|
+
this.hooks.onTerminal?.(this);
|
|
314
|
+
this.resolveEnded();
|
|
315
|
+
}
|
|
316
|
+
save() {
|
|
317
|
+
// A response that is not yet announced is not stored: a request refused at admission leaves nothing behind.
|
|
318
|
+
if (!this.announced)
|
|
319
|
+
return;
|
|
320
|
+
// Handed over at once, so the store registers the response before anything can ask about it.
|
|
321
|
+
const written = this.hooks.persist(structuredClone(this.record)).catch((error) => {
|
|
322
|
+
log.logWarn("webapi.response.persist_failed", "Response snapshot write failed", {
|
|
323
|
+
reason: errorMessage(error),
|
|
324
|
+
fields: { responseId: this.id },
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
this.persisting = Promise.all([this.persisting, written]).then(() => undefined);
|
|
328
|
+
}
|
|
329
|
+
/** Tool calls still running when the response ends are closed as `incomplete`, with their `done` events. */
|
|
330
|
+
closeDanglingCalls() {
|
|
331
|
+
for (const [callId, index] of [...this.toolItems]) {
|
|
332
|
+
this.toolItems.delete(callId);
|
|
333
|
+
const item = this.record.output[index];
|
|
334
|
+
item.status = "incomplete";
|
|
335
|
+
this.emit({ type: "response.output_item.done", output_index: index, item: structuredClone(item) });
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
markDanglingCallsIncomplete() {
|
|
339
|
+
for (const [callId, index] of [...this.toolItems]) {
|
|
340
|
+
this.toolItems.delete(callId);
|
|
341
|
+
this.record.output[index].status = "incomplete";
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
function isTerminalEvent(event) {
|
|
346
|
+
return (event.type === "response.completed" || event.type === "response.failed" || event.type === "response.incomplete");
|
|
347
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/v1/conversations` (spec 061, D12/D13): create, retrieve, update metadata, list items. A
|
|
3
|
+
* conversation is a channel; its items are what its responses said and were asked, not the model's
|
|
4
|
+
* context. Deleting and editing items are not offered (404).
|
|
5
|
+
*/
|
|
6
|
+
import type { FastifyInstance } from "fastify";
|
|
7
|
+
import { type RouteContext } from "../http.js";
|
|
8
|
+
export declare function registerConversationRoutes(app: FastifyInstance, { service }: RouteContext): void;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { badRequest, notFound } from "../errors.js";
|
|
2
|
+
import { sendError } from "../http.js";
|
|
3
|
+
import { conversationItems, defaultPage, paginate, UnknownCursorError } from "../items.js";
|
|
4
|
+
import { MAX_PAGE_LIMIT } from "../limits.js";
|
|
5
|
+
import { validateMetadataParam } from "../request-policy.js";
|
|
6
|
+
function toApiConversation(conversation) {
|
|
7
|
+
return {
|
|
8
|
+
id: conversation.id,
|
|
9
|
+
object: "conversation",
|
|
10
|
+
created_at: conversation.created_at,
|
|
11
|
+
metadata: conversation.metadata ?? {},
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
const ITEMS_QUERY = {
|
|
15
|
+
type: "object",
|
|
16
|
+
additionalProperties: false,
|
|
17
|
+
properties: {
|
|
18
|
+
after: { type: "string" },
|
|
19
|
+
limit: { type: "integer", minimum: 1, maximum: MAX_PAGE_LIMIT },
|
|
20
|
+
order: { type: "string", enum: ["asc", "desc"] },
|
|
21
|
+
include: { type: "array", maxItems: 0 },
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
export function registerConversationRoutes(app, { service }) {
|
|
25
|
+
app.post("/v1/conversations", async (request, reply) => {
|
|
26
|
+
const body = (request.body ?? {});
|
|
27
|
+
for (const key of Object.keys(body)) {
|
|
28
|
+
if (key !== "metadata" && key !== "items") {
|
|
29
|
+
return sendError(reply, badRequest("unknown_parameter", `Unknown parameter \`${key}\`.`, key));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (Array.isArray(body.items) ? body.items.length > 0 : body.items !== undefined && body.items !== null) {
|
|
33
|
+
return sendError(reply, badRequest("unsupported_parameter", "Seeding a conversation with `items` is not supported.", "items"));
|
|
34
|
+
}
|
|
35
|
+
const conversation = await service.store.createConversation(request.principal.name, validateMetadataParam(body.metadata));
|
|
36
|
+
request.auditConversationId = conversation.id;
|
|
37
|
+
return toApiConversation(conversation);
|
|
38
|
+
});
|
|
39
|
+
app.get("/v1/conversations/:id", async (request, reply) => {
|
|
40
|
+
try {
|
|
41
|
+
const conversation = service.conversationFor(request.principal, request.params.id);
|
|
42
|
+
request.auditConversationId = conversation.id;
|
|
43
|
+
return toApiConversation(conversation);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
return sendError(reply, error);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
app.post("/v1/conversations/:id", async (request, reply) => {
|
|
50
|
+
const conversation = service.conversationFor(request.principal, request.params.id);
|
|
51
|
+
const body = (request.body ?? {});
|
|
52
|
+
for (const key of Object.keys(body)) {
|
|
53
|
+
if (key !== "metadata")
|
|
54
|
+
return sendError(reply, badRequest("unknown_parameter", `Unknown parameter \`${key}\`.`, key));
|
|
55
|
+
}
|
|
56
|
+
if (body.metadata === undefined) {
|
|
57
|
+
return sendError(reply, badRequest("missing_required_parameter", "`metadata` is required.", "metadata"));
|
|
58
|
+
}
|
|
59
|
+
request.auditConversationId = conversation.id;
|
|
60
|
+
return toApiConversation(await service.store.updateConversationMetadata(conversation.id, validateMetadataParam(body.metadata)));
|
|
61
|
+
});
|
|
62
|
+
app.delete("/v1/conversations/:id", async (request) => {
|
|
63
|
+
await service.deleteConversation(request.principal, request.params.id);
|
|
64
|
+
request.auditConversationId = request.params.id;
|
|
65
|
+
return { id: request.params.id, object: "conversation.deleted", deleted: true };
|
|
66
|
+
});
|
|
67
|
+
app.get("/v1/conversations/:id/items", { schema: { querystring: ITEMS_QUERY } }, async (request, reply) => {
|
|
68
|
+
const conversation = service.conversationFor(request.principal, request.params.id);
|
|
69
|
+
request.auditConversationId = conversation.id;
|
|
70
|
+
try {
|
|
71
|
+
return paginate(conversationItems(service.responsesOf(conversation.id)), defaultPage(request.query));
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
if (error instanceof UnknownCursorError) {
|
|
75
|
+
return sendError(reply, notFound(`Item ${error.message}`));
|
|
76
|
+
}
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { notFound } from "../errors.js";
|
|
2
|
+
import { sendError } from "../http.js";
|
|
3
|
+
import { MODEL_NAME } from "../response-record.js";
|
|
4
|
+
const MODEL = { id: MODEL_NAME, object: "model", created: 0, owned_by: "pipiclaw" };
|
|
5
|
+
export function registerModelRoutes(app) {
|
|
6
|
+
app.get("/v1/models", async () => ({ object: "list", data: [MODEL] }));
|
|
7
|
+
app.get("/v1/models/:id", async (request, reply) => {
|
|
8
|
+
if (request.params.id !== MODEL_NAME)
|
|
9
|
+
return sendError(reply, notFound(`Model ${request.params.id}`));
|
|
10
|
+
return MODEL;
|
|
11
|
+
});
|
|
12
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/v1/responses` over HTTP (spec 061, D13/D14/D17/D19/D20). One request is one message and one
|
|
3
|
+
* Response; what the caller gets back — the finished object, an event stream, or the object at once
|
|
4
|
+
* with the work continuing in the background — is the only thing the four modes differ in.
|
|
5
|
+
*/
|
|
6
|
+
import type { FastifyInstance } from "fastify";
|
|
7
|
+
import { type RouteContext } from "../http.js";
|
|
8
|
+
export declare function registerResponseRoutes(app: FastifyInstance, context: RouteContext): void;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { badRequest, notFound } from "../errors.js";
|
|
2
|
+
import { sendError } from "../http.js";
|
|
3
|
+
import { parseInput } from "../input.js";
|
|
4
|
+
import { defaultPage, paginate, UnknownCursorError } from "../items.js";
|
|
5
|
+
import { MAX_PAGE_LIMIT } from "../limits.js";
|
|
6
|
+
import { validateCreateRequest } from "../request-policy.js";
|
|
7
|
+
import { toApiResponse } from "../response-record.js";
|
|
8
|
+
import { openEventStream, pumpEvents } from "../stream.js";
|
|
9
|
+
const EMPTY_INCLUDE = { type: "array", maxItems: 0 };
|
|
10
|
+
const RETRIEVE_QUERY = {
|
|
11
|
+
type: "object",
|
|
12
|
+
additionalProperties: false,
|
|
13
|
+
properties: {
|
|
14
|
+
stream: { type: "boolean" },
|
|
15
|
+
starting_after: { type: "integer", minimum: 0 },
|
|
16
|
+
include: EMPTY_INCLUDE,
|
|
17
|
+
include_obfuscation: { type: "boolean" },
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
const INPUT_ITEMS_QUERY = {
|
|
21
|
+
type: "object",
|
|
22
|
+
additionalProperties: false,
|
|
23
|
+
properties: {
|
|
24
|
+
after: { type: "string" },
|
|
25
|
+
limit: { type: "integer", minimum: 1, maximum: MAX_PAGE_LIMIT },
|
|
26
|
+
order: { type: "string", enum: ["asc", "desc"] },
|
|
27
|
+
include: EMPTY_INCLUDE,
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
export function registerResponseRoutes(app, context) {
|
|
31
|
+
const { service } = context;
|
|
32
|
+
/** Cancel `live` if the caller walks away before it is done (a synchronous or streamed request). */
|
|
33
|
+
function cancelOnDisconnect(request, live) {
|
|
34
|
+
const socket = request.raw.socket;
|
|
35
|
+
const onClose = () => {
|
|
36
|
+
if (!live.terminal)
|
|
37
|
+
void service.cancel(live);
|
|
38
|
+
};
|
|
39
|
+
socket.once("close", onClose);
|
|
40
|
+
return () => socket.off("close", onClose);
|
|
41
|
+
}
|
|
42
|
+
app.post("/v1/responses", async (request, reply) => {
|
|
43
|
+
const create = validateCreateRequest(request.body, "http");
|
|
44
|
+
const input = parseInput(create.input);
|
|
45
|
+
const live = await service.create(request.principal, create, input);
|
|
46
|
+
request.auditConversationId = live.record.conversation_id;
|
|
47
|
+
if (create.stream) {
|
|
48
|
+
const stopWatching = create.background ? () => undefined : cancelOnDisconnect(request, live);
|
|
49
|
+
reply.hijack();
|
|
50
|
+
openEventStream(reply.raw);
|
|
51
|
+
try {
|
|
52
|
+
await pumpEvents(live, 0, reply.raw);
|
|
53
|
+
}
|
|
54
|
+
finally {
|
|
55
|
+
stopWatching();
|
|
56
|
+
reply.raw.end();
|
|
57
|
+
context.auditHijacked(request, 200);
|
|
58
|
+
}
|
|
59
|
+
return reply;
|
|
60
|
+
}
|
|
61
|
+
if (create.background)
|
|
62
|
+
return live.snapshot();
|
|
63
|
+
// Synchronous: the caller waits for the end, and leaving cancels the work.
|
|
64
|
+
const stopWatching = cancelOnDisconnect(request, live);
|
|
65
|
+
try {
|
|
66
|
+
await live.whenEnded();
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
stopWatching();
|
|
70
|
+
}
|
|
71
|
+
return live.snapshot();
|
|
72
|
+
});
|
|
73
|
+
app.get("/v1/responses/:id", { schema: { querystring: RETRIEVE_QUERY } }, async (request, reply) => {
|
|
74
|
+
const record = service.recordFor(request.principal, request.params.id);
|
|
75
|
+
request.auditConversationId = record.conversation_id;
|
|
76
|
+
const live = service.getLive(record.id);
|
|
77
|
+
if (!request.query.stream)
|
|
78
|
+
return live ? live.snapshot() : toApiResponse(record);
|
|
79
|
+
if (!live?.replayable) {
|
|
80
|
+
return sendError(reply, badRequest("stream_not_available", "Only a response created with `background: true` and `stream: true` can be streamed again, and only for a few minutes after it ends.", "stream"));
|
|
81
|
+
}
|
|
82
|
+
const from = request.query.starting_after === undefined ? 0 : request.query.starting_after + 1;
|
|
83
|
+
reply.hijack();
|
|
84
|
+
openEventStream(reply.raw);
|
|
85
|
+
try {
|
|
86
|
+
await pumpEvents(live, from, reply.raw);
|
|
87
|
+
}
|
|
88
|
+
finally {
|
|
89
|
+
reply.raw.end();
|
|
90
|
+
context.auditHijacked(request, 200);
|
|
91
|
+
}
|
|
92
|
+
return reply;
|
|
93
|
+
});
|
|
94
|
+
app.post("/v1/responses/:id/cancel", async (request, reply) => {
|
|
95
|
+
const record = service.recordFor(request.principal, request.params.id);
|
|
96
|
+
request.auditConversationId = record.conversation_id;
|
|
97
|
+
if (!record.background) {
|
|
98
|
+
return sendError(reply, badRequest("invalid_value", "Only a response created with `background: true` can be cancelled. To cancel a synchronous or streamed request, close the connection."));
|
|
99
|
+
}
|
|
100
|
+
const live = service.getLive(record.id);
|
|
101
|
+
if (!live)
|
|
102
|
+
return toApiResponse(record);
|
|
103
|
+
await service.cancel(live);
|
|
104
|
+
return live.snapshot();
|
|
105
|
+
});
|
|
106
|
+
app.get("/v1/responses/:id/input_items", { schema: { querystring: INPUT_ITEMS_QUERY } }, async (request, reply) => {
|
|
107
|
+
const record = service.recordFor(request.principal, request.params.id);
|
|
108
|
+
request.auditConversationId = record.conversation_id;
|
|
109
|
+
try {
|
|
110
|
+
return paginate(record.input_items, defaultPage(request.query));
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
if (error instanceof UnknownCursorError)
|
|
114
|
+
return sendError(reply, notFound(`Item ${error.message}`));
|
|
115
|
+
throw error;
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `GET /v1/responses` upgraded to a WebSocket (spec 061, D14/D18/D19/D22). The connection carries
|
|
3
|
+
* `response.create` (the same request an HTTP call makes, always streamed) and `response.steer`
|
|
4
|
+
* (mid-turn steering). Named `stream_id` lanes run in parallel and each lane runs its responses one
|
|
5
|
+
* at a time; a steered response ends `incomplete` and its successor carries on on the same lane.
|
|
6
|
+
*/
|
|
7
|
+
import type { FastifyInstance } from "fastify";
|
|
8
|
+
import type { RouteContext } from "../http.js";
|
|
9
|
+
export declare function registerWebSocketRoute(app: FastifyInstance, context: RouteContext): void;
|