@qvac/core 1.0.0-rc.0 → 1.0.0-rc.10
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 +1203 -1
- package/README.md +56 -45
- package/dist-lib/index.d.ts +3 -2
- package/dist-lib/index.js +1 -1
- package/dist-lib/lib/api/agent-folders.d.ts +22 -0
- package/dist-lib/lib/api/agent-folders.js +115 -0
- package/dist-lib/lib/api/agents.d.ts +20 -2
- package/dist-lib/lib/api/agents.js +128 -24
- package/dist-lib/lib/api/app-config.d.ts +7 -1
- package/dist-lib/lib/api/app-config.js +37 -6
- package/dist-lib/lib/api/approval-rules.d.ts +1 -1
- package/dist-lib/lib/api/approval-rules.js +12 -5
- package/dist-lib/lib/api/attachments.d.ts +1 -1
- package/dist-lib/lib/api/attachments.js +14 -2
- package/dist-lib/lib/api/chat-move.d.ts +4 -0
- package/dist-lib/lib/api/chat-move.js +7 -0
- package/dist-lib/lib/api/chat.d.ts +12 -2
- package/dist-lib/lib/api/chat.js +100 -9
- package/dist-lib/lib/api/chunk-delta.d.ts +12 -0
- package/dist-lib/lib/api/chunk-delta.js +115 -0
- package/dist-lib/lib/api/chunks.d.ts +19 -2
- package/dist-lib/lib/api/chunks.js +73 -13
- package/dist-lib/lib/api/config.d.ts +11 -3
- package/dist-lib/lib/api/config.js +51 -5
- package/dist-lib/lib/api/current-mesh.d.ts +13 -5
- package/dist-lib/lib/api/current-mesh.js +15 -11
- package/dist-lib/lib/api/custom-models.d.ts +7 -1
- package/dist-lib/lib/api/custom-models.js +12 -12
- package/dist-lib/lib/api/device-routes.d.ts +14 -0
- package/dist-lib/lib/api/device-routes.js +144 -0
- package/dist-lib/lib/api/devices.d.ts +3 -2
- package/dist-lib/lib/api/devices.js +28 -28
- package/dist-lib/lib/api/diagnostics.d.ts +2 -1
- package/dist-lib/lib/api/diagnostics.js +2 -2
- package/dist-lib/lib/api/execution.d.ts +16 -1
- package/dist-lib/lib/api/execution.js +72 -16
- package/dist-lib/lib/api/files.d.ts +1 -1
- package/dist-lib/lib/api/files.js +65 -8
- package/dist-lib/lib/api/host-run-log.d.ts +2 -41
- package/dist-lib/lib/api/host-run-log.js +35 -83
- package/dist-lib/lib/api/index.d.ts +39 -7
- package/dist-lib/lib/api/index.js +116 -36
- package/dist-lib/lib/api/instrument.d.ts +2 -2
- package/dist-lib/lib/api/instrument.js +24 -48
- package/dist-lib/lib/api/knowledge.d.ts +4 -1
- package/dist-lib/lib/api/knowledge.js +25 -7
- package/dist-lib/lib/api/local-files.d.ts +5 -1
- package/dist-lib/lib/api/local-files.js +9 -5
- package/dist-lib/lib/api/logs.js +3 -3
- package/dist-lib/lib/api/members.d.ts +10 -1
- package/dist-lib/lib/api/members.js +69 -44
- package/dist-lib/lib/api/mesh.d.ts +15 -6
- package/dist-lib/lib/api/mesh.js +13 -9
- package/dist-lib/lib/api/model-download.d.ts +2 -1
- package/dist-lib/lib/api/model-download.js +2 -2
- package/dist-lib/lib/api/network.d.ts +10 -2
- package/dist-lib/lib/api/network.js +8 -2
- package/dist-lib/lib/api/notifications.d.ts +3 -1
- package/dist-lib/lib/api/notifications.js +14 -9
- package/dist-lib/lib/api/projects.d.ts +20 -0
- package/dist-lib/lib/api/projects.js +172 -0
- package/dist-lib/lib/api/rag-plugin.d.ts +17 -0
- package/dist-lib/lib/api/rag-plugin.js +44 -0
- package/dist-lib/lib/api/rag.d.ts +0 -1
- package/dist-lib/lib/api/rag.js +106 -33
- package/dist-lib/lib/api/served.d.ts +17 -0
- package/dist-lib/lib/api/served.js +65 -0
- package/dist-lib/lib/api/sync.d.ts +2 -1
- package/dist-lib/lib/api/sync.js +13 -3
- package/dist-lib/lib/api/tool-activity.d.ts +2 -1
- package/dist-lib/lib/api/tool-activity.js +4 -3
- package/dist-lib/lib/api/tool-configs.d.ts +2 -1
- package/dist-lib/lib/api/tool-configs.js +6 -9
- package/dist-lib/lib/api/upload-frames.d.ts +0 -1
- package/dist-lib/lib/api/upload-frames.js +0 -1
- package/dist-lib/lib/api/watchable.d.ts +3 -2
- package/dist-lib/lib/api/watchable.js +2 -3
- package/dist-lib/lib/api/workspace.d.ts +12 -0
- package/dist-lib/lib/api/workspace.js +61 -0
- package/dist-lib/lib/assistant/fake-models.d.ts +20 -0
- package/dist-lib/lib/assistant/fake-models.js +112 -0
- package/dist-lib/lib/assistant/harness.d.ts +48 -31
- package/dist-lib/lib/assistant/harness.js +51 -10
- package/dist-lib/lib/assistant/history.d.ts +37 -17
- package/dist-lib/lib/assistant/history.js +212 -87
- package/dist-lib/lib/assistant/host-run-log.d.ts +6 -1
- package/dist-lib/lib/assistant/host-run-log.js +24 -36
- package/dist-lib/lib/assistant/media-round.d.ts +61 -0
- package/dist-lib/lib/assistant/media-round.js +111 -0
- package/dist-lib/lib/assistant/message.d.ts +28 -1
- package/dist-lib/lib/assistant/message.js +65 -2
- package/dist-lib/lib/assistant/model-round.d.ts +118 -0
- package/dist-lib/lib/assistant/model-round.js +706 -0
- package/dist-lib/lib/assistant/pdf-budget.d.ts +11 -0
- package/dist-lib/lib/assistant/pdf-budget.js +12 -0
- package/dist-lib/lib/assistant/pdf-pages.d.ts +25 -0
- package/dist-lib/lib/assistant/pdf-pages.js +75 -0
- package/dist-lib/lib/assistant/round-host.d.ts +65 -0
- package/dist-lib/lib/assistant/round-host.js +332 -0
- package/dist-lib/lib/assistant/skill-encode.d.ts +2 -1
- package/dist-lib/lib/assistant/skill-encode.js +2 -1
- package/dist-lib/lib/assistant/upload.d.ts +12 -0
- package/dist-lib/lib/assistant/upload.js +36 -0
- package/dist-lib/lib/assistant/video-budget.d.ts +13 -0
- package/dist-lib/lib/assistant/video-budget.js +29 -0
- package/dist-lib/lib/assistant-client.d.ts +5 -1
- package/dist-lib/lib/assistant-client.js +54 -14
- package/dist-lib/lib/assistant-tree.d.ts +14 -3
- package/dist-lib/lib/assistant-tree.js +113 -21
- package/dist-lib/lib/assistant.d.ts +115 -25
- package/dist-lib/lib/assistant.js +2409 -1245
- package/dist-lib/lib/audio-format.js +1 -1
- package/dist-lib/lib/chat-suggestion.d.ts +3 -0
- package/dist-lib/lib/chat-suggestion.js +47 -0
- package/dist-lib/lib/client.d.ts +3 -2
- package/dist-lib/lib/client.js +18 -3
- package/dist-lib/lib/compaction.d.ts +3 -1
- package/dist-lib/lib/compaction.js +18 -4
- package/dist-lib/lib/constants.d.ts +10 -0
- package/dist-lib/lib/constants.js +10 -0
- package/dist-lib/lib/core/agent-copies.d.ts +9 -0
- package/dist-lib/lib/core/agent-copies.js +47 -0
- package/dist-lib/lib/core/agent-hosting.d.ts +25 -0
- package/dist-lib/lib/core/agent-hosting.js +76 -0
- package/dist-lib/lib/core/agent.d.ts +9 -1
- package/dist-lib/lib/core/agent.js +58 -63
- package/dist-lib/lib/core/approval-rules.d.ts +5 -1
- package/dist-lib/lib/core/approval-rules.js +17 -9
- package/dist-lib/lib/core/archived-mesh.d.ts +14 -7
- package/dist-lib/lib/core/archived-mesh.js +75 -59
- package/dist-lib/lib/core/async-iterable.js +1 -1
- package/dist-lib/lib/core/blind-peers.d.ts +1 -0
- package/dist-lib/lib/core/blind-peers.js +1 -0
- package/dist-lib/lib/core/chat-activity.d.ts +1 -1
- package/dist-lib/lib/core/chat-activity.js +45 -34
- package/dist-lib/lib/core/chat-copy.d.ts +42 -0
- package/dist-lib/lib/core/chat-copy.js +404 -0
- package/dist-lib/lib/core/chat-fork.d.ts +16 -0
- package/dist-lib/lib/core/chat-fork.js +246 -0
- package/dist-lib/lib/core/chat-group.js +10 -22
- package/dist-lib/lib/core/chat-move.d.ts +77 -0
- package/dist-lib/lib/core/chat-move.js +601 -0
- package/dist-lib/lib/core/chat-order.d.ts +2 -1
- package/dist-lib/lib/core/chat-order.js +28 -19
- package/dist-lib/lib/core/chat-read.d.ts +4 -0
- package/dist-lib/lib/core/chat-read.js +49 -0
- package/dist-lib/lib/core/chat-search.js +14 -12
- package/dist-lib/lib/core/chat.d.ts +32 -4
- package/dist-lib/lib/core/chat.js +135 -61
- package/dist-lib/lib/core/chunk.d.ts +35 -7
- package/dist-lib/lib/core/chunk.js +271 -72
- package/dist-lib/lib/core/collections.d.ts +41 -1
- package/dist-lib/lib/core/collections.js +41 -1
- package/dist-lib/lib/core/config-value.d.ts +7 -0
- package/dist-lib/lib/core/config-value.js +15 -0
- package/dist-lib/lib/core/contract/activity.d.ts +7 -2
- package/dist-lib/lib/core/contract/activity.js +71 -23
- package/dist-lib/lib/core/contract/agent-join.d.ts +10 -0
- package/dist-lib/lib/core/contract/agent-join.js +104 -0
- package/dist-lib/lib/core/contract/agents.d.ts +5 -0
- package/dist-lib/lib/core/contract/agents.js +142 -3
- package/dist-lib/lib/core/contract/chat.d.ts +4 -0
- package/dist-lib/lib/core/contract/chat.js +53 -3
- package/dist-lib/lib/core/contract/chunks.d.ts +7 -0
- package/dist-lib/lib/core/contract/chunks.js +125 -38
- package/dist-lib/lib/core/contract/devices.js +6 -4
- package/dist-lib/lib/core/contract/index.d.ts +2 -3
- package/dist-lib/lib/core/contract/index.js +38 -11
- package/dist-lib/lib/core/contract/indexing.d.ts +1 -0
- package/dist-lib/lib/core/contract/indexing.js +51 -7
- package/dist-lib/lib/core/contract/invites.d.ts +3 -14
- package/dist-lib/lib/core/contract/invites.js +26 -36
- package/dist-lib/lib/core/contract/joined-meshes.js +4 -1
- package/dist-lib/lib/core/contract/knowledge.d.ts +1 -0
- package/dist-lib/lib/core/contract/knowledge.js +57 -6
- package/dist-lib/lib/core/contract/members.d.ts +3 -1
- package/dist-lib/lib/core/contract/members.js +48 -12
- package/dist-lib/lib/core/contract/mesh-kind.d.ts +3 -3
- package/dist-lib/lib/core/contract/mesh-kind.js +20 -16
- package/dist-lib/lib/core/contract/mesh-name.d.ts +3 -0
- package/dist-lib/lib/core/contract/mesh-name.js +31 -0
- package/dist-lib/lib/core/contract/mesh-rules.d.ts +8 -0
- package/dist-lib/lib/core/contract/mesh-rules.js +35 -0
- package/dist-lib/lib/core/contract/mesh-seeds.d.ts +2 -0
- package/dist-lib/lib/core/contract/mesh-seeds.js +26 -0
- package/dist-lib/lib/core/contract/model-requests.d.ts +26 -0
- package/dist-lib/lib/core/contract/model-requests.js +280 -0
- package/dist-lib/lib/core/contract/oldest-version.d.ts +9 -0
- package/dist-lib/lib/core/contract/oldest-version.js +20 -0
- package/dist-lib/lib/core/contract/open-join.d.ts +2 -0
- package/dist-lib/lib/core/contract/open-join.js +77 -0
- package/dist-lib/lib/core/contract/project-device-keys.d.ts +9 -0
- package/dist-lib/lib/core/contract/project-device-keys.js +38 -0
- package/dist-lib/lib/core/contract/project-host-keys.d.ts +9 -0
- package/dist-lib/lib/core/contract/project-host-keys.js +52 -0
- package/dist-lib/lib/core/contract/project-order.d.ts +2 -0
- package/dist-lib/lib/core/contract/project-order.js +25 -0
- package/dist-lib/lib/core/contract/projects.d.ts +23 -0
- package/dist-lib/lib/core/contract/projects.js +339 -0
- package/dist-lib/lib/core/contract/public-mesh.d.ts +3 -0
- package/dist-lib/lib/core/contract/public-mesh.js +31 -0
- package/dist-lib/lib/core/contract/public-seeds.d.ts +9 -0
- package/dist-lib/lib/core/contract/public-seeds.js +43 -0
- package/dist-lib/lib/core/contract/roles.d.ts +10 -0
- package/dist-lib/lib/core/contract/roles.js +123 -0
- package/dist-lib/lib/core/contract/run-requests.d.ts +11 -0
- package/dist-lib/lib/core/contract/run-requests.js +114 -0
- package/dist-lib/lib/core/contract/seed-defaults.d.ts +3 -1
- package/dist-lib/lib/core/contract/seed-defaults.js +17 -4
- package/dist-lib/lib/core/contract/shipped-rows.d.ts +2 -0
- package/dist-lib/lib/core/contract/shipped-rows.js +22 -0
- package/dist-lib/lib/core/contract/skill-secrets.d.ts +3 -0
- package/dist-lib/lib/core/contract/skill-secrets.js +32 -0
- package/dist-lib/lib/core/contract/tool-activity.js +12 -1
- package/dist-lib/lib/core/contract/tool-configs.js +2 -1
- package/dist-lib/lib/core/contract/types.d.ts +2 -2
- package/dist-lib/lib/core/contract/workspace.d.ts +31 -0
- package/dist-lib/lib/core/contract/workspace.js +311 -0
- package/dist-lib/lib/core/contract/writers.js +100 -16
- package/dist-lib/lib/core/deleted-agent-runs.d.ts +8 -0
- package/dist-lib/lib/core/deleted-agent-runs.js +57 -0
- package/dist-lib/lib/core/departures.d.ts +18 -5
- package/dist-lib/lib/core/departures.js +82 -6
- package/dist-lib/lib/core/device-control.d.ts +21 -0
- package/dist-lib/lib/core/device-control.js +91 -0
- package/dist-lib/lib/core/device-seal.d.ts +4 -0
- package/dist-lib/lib/core/device-seal.js +22 -0
- package/dist-lib/lib/core/devices.d.ts +2 -1
- package/dist-lib/lib/core/devices.js +6 -11
- package/dist-lib/lib/core/document-attachment.d.ts +10 -0
- package/dist-lib/lib/core/document-attachment.js +14 -0
- package/dist-lib/lib/core/files/attachments.d.ts +2 -0
- package/dist-lib/lib/core/files/attachments.js +23 -7
- package/dist-lib/lib/core/files/index.d.ts +28 -15
- package/dist-lib/lib/core/files/index.js +222 -43
- package/dist-lib/lib/core/files/knowledge.d.ts +10 -1
- package/dist-lib/lib/core/files/knowledge.js +77 -5
- package/dist-lib/lib/core/files/preview/index.js +6 -2
- package/dist-lib/lib/core/files/processing/claims.d.ts +7 -0
- package/dist-lib/lib/core/files/processing/claims.js +33 -7
- package/dist-lib/lib/core/files/processing/document.d.ts +8 -0
- package/dist-lib/lib/core/files/processing/document.js +26 -2
- package/dist-lib/lib/core/files/processing/index.d.ts +12 -3
- package/dist-lib/lib/core/files/processing/index.js +122 -73
- package/dist-lib/lib/core/files/processing/protocol.d.ts +7 -1
- package/dist-lib/lib/core/files/processing/protocol.js +27 -37
- package/dist-lib/lib/core/files/processing/source.d.ts +4 -0
- package/dist-lib/lib/core/files/processing/source.js +12 -4
- package/dist-lib/lib/core/files/processing/state.d.ts +5 -1
- package/dist-lib/lib/core/files/processing/state.js +9 -1
- package/dist-lib/lib/core/files/rag/build.d.ts +15 -2
- package/dist-lib/lib/core/files/rag/build.js +119 -21
- package/dist-lib/lib/core/files/rag/failure.d.ts +20 -0
- package/dist-lib/lib/core/files/rag/failure.js +58 -0
- package/dist-lib/lib/core/files/rag/index.d.ts +21 -5
- package/dist-lib/lib/core/files/rag/index.js +38 -9
- package/dist-lib/lib/core/files/rag/joined-index.d.ts +6 -0
- package/dist-lib/lib/core/files/rag/joined-index.js +35 -0
- package/dist-lib/lib/core/files/rag/limits.d.ts +1 -0
- package/dist-lib/lib/core/files/rag/limits.js +1 -0
- package/dist-lib/lib/core/files/rag/load.d.ts +3 -3
- package/dist-lib/lib/core/files/rag/load.js +6 -3
- package/dist-lib/lib/core/files/rag/plugin-index.d.ts +32 -0
- package/dist-lib/lib/core/files/rag/plugin-index.js +120 -0
- package/dist-lib/lib/core/files/rag/policy.d.ts +10 -1
- package/dist-lib/lib/core/files/rag/policy.js +23 -2
- package/dist-lib/lib/core/files/rag/produce.d.ts +3 -4
- package/dist-lib/lib/core/files/rag/produce.js +31 -18
- package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +2 -0
- package/dist-lib/lib/core/files/rag/retrieval/full-text.js +8 -2
- package/dist-lib/lib/core/files/rag/similarity.d.ts +2 -2
- package/dist-lib/lib/core/files/rag/similarity.js +2 -3
- package/dist-lib/lib/core/files/records/chunks.js +22 -1
- package/dist-lib/lib/core/files/records/index.d.ts +1 -1
- package/dist-lib/lib/core/files/records/index.js +1 -1
- package/dist-lib/lib/core/files/records/knowledge.d.ts +12 -4
- package/dist-lib/lib/core/files/records/knowledge.js +34 -30
- package/dist-lib/lib/core/files/records/purge.js +13 -8
- package/dist-lib/lib/core/files/records/reclaim.js +21 -55
- package/dist-lib/lib/core/files/records/routing.d.ts +1 -1
- package/dist-lib/lib/core/files/records/routing.js +6 -2
- package/dist-lib/lib/core/files/records/source.js +3 -5
- package/dist-lib/lib/core/files/records/sweep.d.ts +3 -0
- package/dist-lib/lib/core/files/records/sweep.js +9 -3
- package/dist-lib/lib/core/files/store/blob.d.ts +2 -0
- package/dist-lib/lib/core/files/store/blob.js +2 -2
- package/dist-lib/lib/core/files/store/index.d.ts +3 -1
- package/dist-lib/lib/core/files/store/index.js +4 -1
- package/dist-lib/lib/core/files/store/reference.d.ts +3 -0
- package/dist-lib/lib/core/files/store/reference.js +12 -2
- package/dist-lib/lib/core/files/store/sweep.js +3 -1
- package/dist-lib/lib/core/files/upload.d.ts +5 -1
- package/dist-lib/lib/core/files/upload.js +117 -51
- package/dist-lib/lib/core/first-event.d.ts +6 -0
- package/dist-lib/lib/core/first-event.js +13 -0
- package/dist-lib/lib/core/fs-drives.d.ts +6 -3
- package/dist-lib/lib/core/fs-drives.js +35 -35
- package/dist-lib/lib/core/helpers/index.d.ts +1 -1
- package/dist-lib/lib/core/helpers/index.js +2 -2
- package/dist-lib/lib/core/id.d.ts +1 -0
- package/dist-lib/lib/core/id.js +3 -0
- package/dist-lib/lib/core/identity.d.ts +3 -1
- package/dist-lib/lib/core/identity.js +25 -4
- package/dist-lib/lib/core/invite.d.ts +6 -2
- package/dist-lib/lib/core/invite.js +22 -8
- package/dist-lib/lib/core/local/files.d.ts +2 -1
- package/dist-lib/lib/core/local/files.js +5 -7
- package/dist-lib/lib/core/local/index.d.ts +93 -8
- package/dist-lib/lib/core/local/index.js +327 -28
- package/dist-lib/lib/core/local/run-logs.d.ts +38 -0
- package/dist-lib/lib/core/local/run-logs.js +132 -0
- package/dist-lib/lib/core/member-key.d.ts +7 -0
- package/dist-lib/lib/core/member-key.js +24 -0
- package/dist-lib/lib/core/membership.d.ts +3 -14
- package/dist-lib/lib/core/membership.js +3 -96
- package/dist-lib/lib/core/mesh.d.ts +132 -29
- package/dist-lib/lib/core/mesh.js +775 -215
- package/dist-lib/lib/core/migrations/agent-catalog.js +2 -8
- package/dist-lib/lib/core/migrations/agent-tools-scope.d.ts +2 -0
- package/dist-lib/lib/core/migrations/agent-tools-scope.js +18 -0
- package/dist-lib/lib/core/migrations/chat-chunks.d.ts +4 -0
- package/dist-lib/lib/core/migrations/chat-chunks.js +47 -0
- package/dist-lib/lib/core/migrations/file-index.d.ts +1 -1
- package/dist-lib/lib/core/migrations/file-index.js +8 -6
- package/dist-lib/lib/core/migrations/index.d.ts +2 -1
- package/dist-lib/lib/core/migrations/index.js +69 -8
- package/dist-lib/lib/core/migrations/writer-links.d.ts +3 -0
- package/dist-lib/lib/core/migrations/writer-links.js +30 -0
- package/dist-lib/lib/core/model-requests.d.ts +13 -0
- package/dist-lib/lib/core/model-requests.js +65 -0
- package/dist-lib/lib/core/next-frame.js +2 -1
- package/dist-lib/lib/core/notification.js +100 -25
- package/dist-lib/lib/core/pdf/figure.d.ts +22 -0
- package/dist-lib/lib/core/pdf/figure.js +64 -0
- package/dist-lib/lib/core/pdf/rasterize.d.ts +12 -3
- package/dist-lib/lib/core/pdf/rasterize.js +77 -28
- package/dist-lib/lib/core/peer-duplex.d.ts +34 -0
- package/dist-lib/lib/core/{harness-loan.js → peer-duplex.js} +104 -45
- package/dist-lib/lib/core/project-keys.d.ts +16 -0
- package/dist-lib/lib/core/project-keys.js +65 -0
- package/dist-lib/lib/core/prompt-history.js +4 -4
- package/dist-lib/lib/core/protocol-pairing.d.ts +11 -0
- package/dist-lib/lib/core/protocol-pairing.js +22 -0
- package/dist-lib/lib/core/public-mesh.d.ts +25 -0
- package/dist-lib/lib/core/public-mesh.js +81 -0
- package/dist-lib/lib/core/purge.d.ts +3 -0
- package/dist-lib/lib/core/purge.js +7 -0
- package/dist-lib/lib/core/run-driver.d.ts +17 -0
- package/dist-lib/lib/core/run-driver.js +20 -0
- package/dist-lib/lib/core/run-requests.d.ts +14 -0
- package/dist-lib/lib/core/run-requests.js +78 -0
- package/dist-lib/lib/core/send.d.ts +2 -0
- package/dist-lib/lib/core/send.js +17 -4
- package/dist-lib/lib/core/senders.d.ts +19 -0
- package/dist-lib/lib/core/senders.js +45 -0
- package/dist-lib/lib/core/shared-agents.d.ts +40 -0
- package/dist-lib/lib/core/shared-agents.js +283 -0
- package/dist-lib/lib/core/skill-availability.d.ts +33 -0
- package/dist-lib/lib/core/skill-availability.js +67 -0
- package/dist-lib/lib/core/skill-secrets.d.ts +13 -18
- package/dist-lib/lib/core/skill-secrets.js +93 -72
- package/dist-lib/lib/core/sync/background.d.ts +34 -0
- package/dist-lib/lib/core/sync/background.js +86 -0
- package/dist-lib/lib/core/sync/changed.d.ts +20 -0
- package/dist-lib/lib/core/sync/changed.js +7 -0
- package/dist-lib/lib/core/sync/devices.js +6 -3
- package/dist-lib/lib/core/sync/files.d.ts +1 -0
- package/dist-lib/lib/core/sync/files.js +50 -72
- package/dist-lib/lib/core/sync/index.d.ts +1 -1
- package/dist-lib/lib/core/sync/index.js +193 -81
- package/dist-lib/lib/core/sync/view-updates.d.ts +39 -0
- package/dist-lib/lib/core/sync/view-updates.js +124 -0
- package/dist-lib/lib/core/sync-status.js +99 -43
- package/dist-lib/lib/core/system-info.d.ts +2 -2
- package/dist-lib/lib/core/system-info.js +10 -11
- package/dist-lib/lib/core/tool-activity.js +6 -12
- package/dist-lib/lib/core/tool-configs.d.ts +5 -3
- package/dist-lib/lib/core/tool-configs.js +27 -39
- package/dist-lib/lib/core/transcript-visibility.d.ts +5 -5
- package/dist-lib/lib/core/transcript-visibility.js +27 -31
- package/dist-lib/lib/core/upgrade-agents.d.ts +11 -0
- package/dist-lib/lib/core/upgrade-agents.js +78 -0
- package/dist-lib/lib/core/upgrade-summary.d.ts +19 -0
- package/dist-lib/lib/core/upgrade-summary.js +67 -0
- package/dist-lib/lib/core/vault-managed-skills.d.ts +18 -0
- package/dist-lib/lib/core/vault-managed-skills.js +105 -0
- package/dist-lib/lib/core/video/attachment.d.ts +1 -0
- package/dist-lib/lib/core/video/attachment.js +7 -0
- package/dist-lib/lib/core/video/digest.d.ts +16 -0
- package/dist-lib/lib/core/video/digest.js +57 -0
- package/dist-lib/lib/core/video/frames.d.ts +32 -0
- package/dist-lib/lib/core/video/frames.js +224 -0
- package/dist-lib/lib/core/view-read.d.ts +4 -0
- package/dist-lib/lib/core/view-read.js +23 -0
- package/dist-lib/lib/core/workspace-upgrade.d.ts +23 -0
- package/dist-lib/lib/core/workspace-upgrade.js +51 -0
- package/dist-lib/lib/core.d.ts +345 -88
- package/dist-lib/lib/core.js +4275 -1513
- package/dist-lib/lib/credential-pusher.d.ts +17 -7
- package/dist-lib/lib/credential-pusher.js +111 -62
- package/dist-lib/lib/custom-model-pusher.d.ts +2 -4
- package/dist-lib/lib/custom-model-task.d.ts +2 -1
- package/dist-lib/lib/custom-model-task.js +2 -2
- package/dist-lib/lib/device-tree.d.ts +18 -0
- package/dist-lib/lib/device-tree.js +32 -0
- package/dist-lib/lib/diagnostics.d.ts +7 -0
- package/dist-lib/lib/diagnostics.js +22 -0
- package/dist-lib/lib/doctor/checks.d.ts +15 -0
- package/dist-lib/lib/doctor/checks.js +272 -0
- package/dist-lib/lib/doctor/redact.d.ts +4 -0
- package/dist-lib/lib/doctor/redact.js +31 -0
- package/dist-lib/lib/doctor/render.d.ts +8 -0
- package/dist-lib/lib/doctor/render.js +83 -0
- package/dist-lib/lib/doctor/tree.d.ts +5 -0
- package/dist-lib/lib/doctor/tree.js +167 -0
- package/dist-lib/lib/downloads-allowed-pusher.d.ts +8 -0
- package/dist-lib/lib/downloads-allowed-pusher.js +37 -0
- package/dist-lib/lib/errors.d.ts +814 -0
- package/dist-lib/lib/errors.js +748 -0
- package/dist-lib/lib/fs-drive-bridge.d.ts +8 -1
- package/dist-lib/lib/fs-drive-bridge.js +312 -31
- package/dist-lib/lib/guards.d.ts +1 -0
- package/dist-lib/lib/guards.js +4 -0
- package/dist-lib/lib/inference-calls.d.ts +1 -2
- package/dist-lib/lib/inference-calls.js +2 -10
- package/dist-lib/lib/integrations/setup-routes.js +7 -5
- package/dist-lib/lib/knowledge-search.d.ts +1 -1
- package/dist-lib/lib/knowledge-search.js +5 -51
- package/dist-lib/lib/kv-cache-ledger.d.ts +22 -0
- package/dist-lib/lib/kv-cache-ledger.js +72 -0
- package/dist-lib/lib/kv-cache-reclaim.d.ts +2 -1
- package/dist-lib/lib/kv-cache-reclaim.js +30 -0
- package/dist-lib/lib/kv-cache-retention.d.ts +36 -0
- package/dist-lib/lib/kv-cache-retention.js +101 -0
- package/dist-lib/lib/latest-frame.d.ts +10 -0
- package/dist-lib/lib/latest-frame.js +48 -0
- package/dist-lib/lib/llm-model-names.d.ts +1 -1
- package/dist-lib/lib/llm-model-names.js +1 -1
- package/dist-lib/lib/log.d.ts +7 -6
- package/dist-lib/lib/log.js +14 -7
- package/dist-lib/lib/logger-type.d.ts +2 -5
- package/dist-lib/lib/mesh-scoped-engine.js +47 -2
- package/dist-lib/lib/model-context.js +1 -2
- package/dist-lib/lib/model-download-status.d.ts +3 -3
- package/dist-lib/lib/model-download.d.ts +22 -13
- package/dist-lib/lib/model-download.js +106 -37
- package/dist-lib/lib/model-idle-pusher.d.ts +2 -4
- package/dist-lib/lib/model-memory-cap.d.ts +30 -0
- package/dist-lib/lib/model-memory-cap.js +78 -0
- package/dist-lib/lib/models-watch.d.ts +1 -1
- package/dist-lib/lib/models-watch.js +4 -4
- package/dist-lib/lib/oauth/browser-connect.js +3 -2
- package/dist-lib/lib/oauth/flow-core.js +5 -3
- package/dist-lib/lib/oauth/flow.js +1 -1
- package/dist-lib/lib/oauth/loopback.d.ts +1 -0
- package/dist-lib/lib/oauth/loopback.js +57 -12
- package/dist-lib/lib/oauth/providers.js +4 -2
- package/dist-lib/lib/oauth/redirect-types.d.ts +1 -0
- package/dist-lib/lib/oauth/register.js +35 -19
- package/dist-lib/lib/oauth/request.d.ts +10 -0
- package/dist-lib/lib/oauth/request.js +22 -0
- package/dist-lib/lib/oauth/resolve.js +42 -13
- package/dist-lib/lib/oauth/token-exchange-core.js +40 -31
- package/dist-lib/lib/oauth/types.d.ts +3 -0
- package/dist-lib/lib/oauth.d.ts +2 -1
- package/dist-lib/lib/oauth.js +1 -0
- package/dist-lib/lib/persistent-dir.d.ts +1 -0
- package/dist-lib/lib/persistent-dir.js +9 -0
- package/dist-lib/lib/probe-rounds.d.ts +8 -0
- package/dist-lib/lib/probe-rounds.js +10 -0
- package/dist-lib/lib/reconnecting-watch.js +19 -11
- package/dist-lib/lib/run-input.d.ts +14 -0
- package/dist-lib/lib/run-input.js +33 -0
- package/dist-lib/lib/run-notices.d.ts +5 -0
- package/dist-lib/lib/run-notices.js +13 -2
- package/dist-lib/lib/run-replay.d.ts +45 -0
- package/dist-lib/lib/run-replay.js +169 -0
- package/dist-lib/lib/run-transcript.d.ts +13 -12
- package/dist-lib/lib/run-transcript.js +20 -19
- package/dist-lib/lib/suspend-lifecycle.js +15 -5
- package/dist-lib/lib/task.js +6 -4
- package/dist-lib/lib/test-env.d.ts +1 -0
- package/dist-lib/lib/test-env.js +4 -0
- package/dist-lib/schema/build.js +66 -55
- package/dist-lib/schema/config.d.ts +1 -1
- package/dist-lib/schema/config.js +1 -1
- package/dist-lib/schema/content.d.ts +5 -1
- package/dist-lib/schema/content.js +60 -12
- package/dist-lib/schema/engine.d.ts +1 -0
- package/dist-lib/schema/engine.js +941 -152
- package/dist-lib/schema/field-order.js +8 -1
- package/dist-lib/schema/indexes.d.ts +10 -0
- package/dist-lib/schema/indexes.js +53 -3
- package/dist-lib/schema/local.js +83 -5
- package/dist-lib/schema/qvac.d.ts +4 -1
- package/dist-lib/schema/qvac.js +558 -16
- package/dist-lib/spec/bind.js +261 -48
- package/dist-lib/spec/calls.js +56 -5
- package/dist-lib/spec/capabilities.d.ts +129 -20
- package/dist-lib/spec/config/hyperconf/index.js +1 -1
- package/dist-lib/spec/hrpc/hrpc.json +870 -207
- package/dist-lib/spec/hrpc/index.js +969 -347
- package/dist-lib/spec/hrpc/messages.js +7590 -4896
- package/dist-lib/spec/hyperschema/index.js +7590 -4896
- package/dist-lib/spec/hyperschema/schema.json +5316 -3224
- package/dist-lib/spec/hyperschema/types.d.ts +576 -62
- package/dist-lib/spec/local/hyperdb/db.json +95 -1
- package/dist-lib/spec/local/hyperdb/index.js +459 -2
- package/dist-lib/spec/local/hyperdb/messages.js +1588 -488
- package/dist-lib/spec/local/hyperschema/index.js +584 -16
- package/dist-lib/spec/local/hyperschema/schema.json +369 -1
- package/dist-lib/spec/local/hyperschema/types.d.ts +88 -2
- package/dist-lib/spec/mesh/hyperdb/db.json +711 -9
- package/dist-lib/spec/mesh/hyperdb/index.js +2769 -7
- package/dist-lib/spec/mesh/hyperdb/messages.js +7140 -1298
- package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +205 -1
- package/dist-lib/spec/mesh/hyperdispatch/index.js +579 -1
- package/dist-lib/spec/mesh/hyperdispatch/messages.js +4017 -267
- package/dist-lib/spec/mesh/hyperschema/index.js +4017 -267
- package/dist-lib/spec/mesh/hyperschema/schema.json +2193 -19
- package/dist-lib/spec/mesh/hyperschema/types.d.ts +550 -6
- package/package.json +136 -90
- package/tui/README.md +119 -20
- package/tui/app.mjs +2000 -621
- package/tui/boot.mjs +74 -9
- package/tui/cli.mjs +93 -0
- package/tui/index.mjs +2 -51
- package/tui/lib/connect-sheet.mjs +350 -0
- package/tui/lib/device-target.mjs +64 -0
- package/tui/lib/folders-command.mjs +84 -0
- package/tui/lib/fork-command.mjs +43 -0
- package/tui/lib/knowledge-import.mjs +3 -21
- package/tui/lib/local-path.mjs +21 -0
- package/tui/lib/move-command.mjs +17 -0
- package/tui/lib/render.mjs +30 -1
- package/tui/lib/setup.mjs +59 -25
- package/tui/lib/skill-connect.mjs +145 -0
- package/tui/lib/updater.mjs +86 -0
- package/tui/main.mjs +44 -18
- package/tui/plugins.mjs +158 -0
- package/worker/assistant-sidecar.mjs +28 -4
- package/worker/stow-entry.mjs +3 -50
- package/dist-lib/lib/core/contract/compute.d.ts +0 -11
- package/dist-lib/lib/core/contract/compute.js +0 -48
- package/dist-lib/lib/core/device-invites.d.ts +0 -11
- package/dist-lib/lib/core/device-invites.js +0 -63
- package/dist-lib/lib/core/files/processing/settle.d.ts +0 -13
- package/dist-lib/lib/core/files/processing/settle.js +0 -86
- package/dist-lib/lib/core/grant-expiry.d.ts +0 -11
- package/dist-lib/lib/core/grant-expiry.js +0 -66
- package/dist-lib/lib/core/handoff-channel.d.ts +0 -47
- package/dist-lib/lib/core/handoff-channel.js +0 -123
- package/dist-lib/lib/core/harness-loan.d.ts +0 -29
- package/dist-lib/lib/core/host-run-log-store.d.ts +0 -114
- package/dist-lib/lib/core/host-run-log-store.js +0 -976
- package/dist-lib/lib/core/hosted-agents.d.ts +0 -38
- package/dist-lib/lib/core/hosted-agents.js +0 -140
- package/dist-lib/lib/core/inference-calls-handoff.d.ts +0 -20
- package/dist-lib/lib/core/inference-calls-handoff.js +0 -37
- package/dist-lib/lib/core/inference-calls-receiver.d.ts +0 -14
- package/dist-lib/lib/core/inference-calls-receiver.js +0 -42
- package/dist-lib/lib/core/mesh-seed-handoff.d.ts +0 -25
- package/dist-lib/lib/core/mesh-seed-handoff.js +0 -52
- package/dist-lib/lib/core/migrations/device-indexes.d.ts +0 -2
- package/dist-lib/lib/core/migrations/device-indexes.js +0 -7
- package/dist-lib/lib/core/secret-handoff.d.ts +0 -17
- package/dist-lib/lib/core/secret-handoff.js +0 -29
- package/dist-lib/lib/core/skill-overlay-handoff.d.ts +0 -25
- package/dist-lib/lib/core/skill-overlay-handoff.js +0 -50
- package/dist-lib/lib/core/skill-overlay-receiver.d.ts +0 -8
- package/dist-lib/lib/core/skill-overlay-receiver.js +0 -23
- package/dist-lib/lib/image-cache-dir.d.ts +0 -1
- package/dist-lib/lib/image-cache-dir.js +0 -5
- package/dist-lib/lib/log-levels.d.ts +0 -7
- package/dist-lib/lib/log-levels.js +0 -1
- package/dist-lib/lib/logger.d.ts +0 -5
- package/dist-lib/lib/logger.js +0 -18
- package/dist-lib/lib/oauth/loopback.stub.d.ts +0 -3
- package/dist-lib/lib/oauth/loopback.stub.js +0 -6
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
import b4a from 'b4a';
|
|
2
|
+
import { PENDING_BLOB_REFERENCE, isPendingBlobReference, parseBlobReference } from './files/store/index.js';
|
|
3
|
+
import { CoreError } from '../errors.js';
|
|
4
|
+
import { inlineReference } from './files/store/inline.js';
|
|
5
|
+
import { TERMINAL_RUN_STATES } from './contract/run-requests.js';
|
|
6
|
+
import { appendChunk, ownedByUs, updateOp } from './chunk.js';
|
|
7
|
+
import { runRequestOf } from './contract/chunks.js';
|
|
8
|
+
import { ACTIVE_AGENTS, AGENTS, CHATS, CHAT_GROUPS, CHUNKS, CHUNKS_BY_CHAT, DEVICES, KNOWLEDGE, KNOWLEDGE_ARTIFACTS, KNOWLEDGE_BY_CHAT, PURGED_CHATS, TOOL_ACTIVITY } from './collections.js';
|
|
9
|
+
const UNCOPIED = new Set(['context', 'model-request']);
|
|
10
|
+
export async function copyChunks(snap, chatId, { from, into }) {
|
|
11
|
+
const batch = into.batch();
|
|
12
|
+
for (const chunk of await chunksOf(snap, chatId)) {
|
|
13
|
+
if (await into.view.get(CHUNKS, { id: chunk.id }))
|
|
14
|
+
continue;
|
|
15
|
+
const { seq, updatedAt, attachment, runnerDeviceId, ...op } = stripped(chunk);
|
|
16
|
+
if (!attachment) {
|
|
17
|
+
batch.dispatch('append-chunk', op);
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
const blobId = await copyBlob(from, into, attachment.blobId);
|
|
21
|
+
if (!blobId) {
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
batch.dispatch('append-chunk', { ...op, attachment: { ...attachment, blobId } });
|
|
25
|
+
await batch.flush();
|
|
26
|
+
await into.fileIndex?.record([{ ...chunk, attachment: { ...attachment, blobId } }]);
|
|
27
|
+
}
|
|
28
|
+
await batch.flush();
|
|
29
|
+
}
|
|
30
|
+
export async function copyMovedChunks(snap, chatId, copy) {
|
|
31
|
+
const { into, originDeviceId } = copy;
|
|
32
|
+
const result = { copied: 0, open: new Set(), withheld: 0 };
|
|
33
|
+
const batch = into.batch();
|
|
34
|
+
const open = new Set();
|
|
35
|
+
const files = [];
|
|
36
|
+
const chunks = await chunksOf(snap, chatId);
|
|
37
|
+
const runs = runsOf(chunks);
|
|
38
|
+
for (const chunk of chunks) {
|
|
39
|
+
if (await heldInTarget(into, { collection: CHUNKS, id: chunk.id, chatId: copy.chatId })) {
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (chunk.runId && runs.open.has(chunk.runId)) {
|
|
43
|
+
result.open.add(chunk.runId);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (chunk.runId && !runs.requested.has(chunk.runId) && into.role === 'project') {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (await othersRun(into, { ...chunk, chatId: copy.chatId })) {
|
|
50
|
+
result.withheld++;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
const { seq, updatedAt, attachment, runnerDeviceId, ...op } = stripped(chunk);
|
|
54
|
+
const at = { ...op, chatId: copy.chatId, senderMemberId: sentBy(chunk) };
|
|
55
|
+
result.copied++;
|
|
56
|
+
trackOpen(open, chunk);
|
|
57
|
+
if (!attachment) {
|
|
58
|
+
batch.dispatch('append-chunk', at);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
const blobId = attachment.deletedAt
|
|
62
|
+
? removedReference(attachment)
|
|
63
|
+
: await movedBlob(copy, attachment);
|
|
64
|
+
const moved = { ...at, originDeviceId, attachment: { ...attachment, blobId } };
|
|
65
|
+
batch.dispatch('append-chunk', moved);
|
|
66
|
+
if (!isPendingBlobReference(blobId)) {
|
|
67
|
+
files.push({ ...chunk, ...moved });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (files.length > 0 && open.size === 0) {
|
|
71
|
+
await batch.flush();
|
|
72
|
+
await into.fileIndex?.record(files.splice(0));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
await batch.flush();
|
|
76
|
+
if (files.length > 0) {
|
|
77
|
+
await into.fileIndex?.record(files);
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
function runsOf(chunks) {
|
|
82
|
+
const requested = new Set();
|
|
83
|
+
const settled = new Set();
|
|
84
|
+
for (const chunk of chunks) {
|
|
85
|
+
if (!chunk.runId)
|
|
86
|
+
continue;
|
|
87
|
+
if (chunk.type === 'run-request') {
|
|
88
|
+
requested.add(chunk.runId);
|
|
89
|
+
}
|
|
90
|
+
else if (TERMINAL_RUN_STATES.has(chunk.runStatus?.state ?? '')) {
|
|
91
|
+
settled.add(chunk.runId);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return { requested, open: new Set([...requested].filter((runId) => !settled.has(runId))) };
|
|
95
|
+
}
|
|
96
|
+
function trackOpen(open, chunk) {
|
|
97
|
+
if (!chunk.runId)
|
|
98
|
+
return;
|
|
99
|
+
if (chunk.type === 'run-request') {
|
|
100
|
+
open.add(chunk.runId);
|
|
101
|
+
}
|
|
102
|
+
else if (TERMINAL_RUN_STATES.has(chunk.runStatus?.state ?? '')) {
|
|
103
|
+
open.delete(chunk.runId);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async function othersRun(into, { chatId, runId }) {
|
|
107
|
+
const request = runId ? await runRequestOf(into.view, chatId, runId) : null;
|
|
108
|
+
return !!request?.requesterMemberId && !(await ownedByUs(into, request));
|
|
109
|
+
}
|
|
110
|
+
export function sentBy(chunk) {
|
|
111
|
+
const writer = chunk.type === 'user-message' ? chunk.requesterMemberId : undefined;
|
|
112
|
+
return chunk.senderMemberId ?? writer ?? undefined;
|
|
113
|
+
}
|
|
114
|
+
export async function copyMovedKnowledge(snap, chatId, copy) {
|
|
115
|
+
const batch = copy.into.batch();
|
|
116
|
+
const inChat = { gte: { chatId }, lte: { chatId } };
|
|
117
|
+
for (const row of await snap.find(KNOWLEDGE_BY_CHAT, inChat).toArray()) {
|
|
118
|
+
if (await heldInTarget(copy.into, { collection: KNOWLEDGE, id: row.id, chatId: copy.chatId })) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
const blobId = row.deletedAt ? removedReference(row) : await movedBlob(copy, row);
|
|
122
|
+
if (isPendingBlobReference(blobId)) {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
batch.dispatch('add-knowledge', {
|
|
126
|
+
knowledge: { ...stripped(row), chatId: copy.chatId, blobId }
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
await batch.flush();
|
|
130
|
+
}
|
|
131
|
+
export async function movedBlob({ from, into, references }, { blobId }) {
|
|
132
|
+
const known = references.get(blobId);
|
|
133
|
+
if (known) {
|
|
134
|
+
return known;
|
|
135
|
+
}
|
|
136
|
+
const moved = (await copyBlob(from, into, blobId)) ?? PENDING_BLOB_REFERENCE;
|
|
137
|
+
if (!isPendingBlobReference(moved)) {
|
|
138
|
+
references.set(blobId, moved);
|
|
139
|
+
}
|
|
140
|
+
return moved;
|
|
141
|
+
}
|
|
142
|
+
export function removedReference({ mimeType }) {
|
|
143
|
+
return inlineReference(b4a.alloc(0), mimeType);
|
|
144
|
+
}
|
|
145
|
+
async function heldInTarget(into, { collection, id, chatId }) {
|
|
146
|
+
const existing = await into.view.get(collection, { id });
|
|
147
|
+
if (existing && existing.chatId !== chatId) {
|
|
148
|
+
throw new CoreError('CHAT_MOVE_ID_CONFLICT');
|
|
149
|
+
}
|
|
150
|
+
return !!existing;
|
|
151
|
+
}
|
|
152
|
+
export async function chunksOf(snap, chatId) {
|
|
153
|
+
const all = await snap
|
|
154
|
+
.find(CHUNKS_BY_CHAT, { gte: { chatId }, lte: { chatId } })
|
|
155
|
+
.toArray();
|
|
156
|
+
const rounds = new Set(all.filter((chunk) => UNCOPIED.has(chunk.type)).map(({ id }) => id));
|
|
157
|
+
return all
|
|
158
|
+
.filter((chunk) => !rounds.has(chunk.id))
|
|
159
|
+
.map((chunk) => chunk.parentId && rounds.has(chunk.parentId) ? { ...chunk, parentId: null } : chunk);
|
|
160
|
+
}
|
|
161
|
+
export async function copyBlob(from, into, reference) {
|
|
162
|
+
let bytes;
|
|
163
|
+
try {
|
|
164
|
+
if (parseBlobReference(reference).inline) {
|
|
165
|
+
return reference;
|
|
166
|
+
}
|
|
167
|
+
bytes = await from.files.readBytes(reference, { wait: false });
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
return (await into.files.put(bytes)).reference;
|
|
173
|
+
}
|
|
174
|
+
export function stripped(row) {
|
|
175
|
+
const { version: _version, ...rest } = row;
|
|
176
|
+
return rest;
|
|
177
|
+
}
|
|
178
|
+
export async function copyContent(copy) {
|
|
179
|
+
await copy.into.autobee.update();
|
|
180
|
+
await copy.from.autobee.update();
|
|
181
|
+
const snap = copy.from.view.snapshot();
|
|
182
|
+
try {
|
|
183
|
+
const remap = await defaultAgentRemap(snap, copy.into);
|
|
184
|
+
const chats = await copyRows(snap, copy, remap);
|
|
185
|
+
const references = new Map();
|
|
186
|
+
for (const chat of chats) {
|
|
187
|
+
await copyChatChunks(snap, chat.id, { ...copy, references });
|
|
188
|
+
}
|
|
189
|
+
await copyFiles(snap, { ...copy, references });
|
|
190
|
+
await copyToolActivity(snap, copy);
|
|
191
|
+
}
|
|
192
|
+
finally {
|
|
193
|
+
await snap.close();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
async function defaultAgentRemap(snap, into) {
|
|
197
|
+
const remap = new Map();
|
|
198
|
+
const target = (await into.view.find(ACTIVE_AGENTS).toArray()).find((a) => a.isDefault);
|
|
199
|
+
if (!target) {
|
|
200
|
+
return remap;
|
|
201
|
+
}
|
|
202
|
+
for (const agent of await snap.find(ACTIVE_AGENTS).toArray()) {
|
|
203
|
+
if (agent.isDefault && agent.id !== target.id) {
|
|
204
|
+
remap.set(agent.id, { id: target.id, seededAt: agent.createdAt });
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return remap;
|
|
208
|
+
}
|
|
209
|
+
async function copyRows(snap, copy, remap) {
|
|
210
|
+
const { into } = copy;
|
|
211
|
+
const batch = into.batch();
|
|
212
|
+
const chats = await copiedChats(snap, copy);
|
|
213
|
+
await copyAgentsAndGroups(snap, { into, batch, remap, chats, ungroup: !!copy.chats });
|
|
214
|
+
const live = [];
|
|
215
|
+
for (const chat of chats) {
|
|
216
|
+
const agent = remap.get(chat.agentId);
|
|
217
|
+
if (agent && (await seededChat(snap, chat, agent))) {
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
if (!chat.deletedAt) {
|
|
221
|
+
live.push(chat);
|
|
222
|
+
}
|
|
223
|
+
if (await newer(into, CHATS, chat)) {
|
|
224
|
+
const groupId = copy.chats ? undefined : chat.groupId;
|
|
225
|
+
batch.dispatch('put-chat', {
|
|
226
|
+
chat: { ...stripped(chat), agentId: agent?.id ?? chat.agentId, groupId }
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
const purged = copy.chats ? [] : await snap.find(PURGED_CHATS).toArray();
|
|
231
|
+
for (const row of purged) {
|
|
232
|
+
if (!(await into.view.get(PURGED_CHATS, { chatId: row.chatId }))) {
|
|
233
|
+
batch.dispatch('purge-chat', { chatId: row.chatId, at: row.at });
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
await batch.flush();
|
|
237
|
+
return live;
|
|
238
|
+
}
|
|
239
|
+
async function copiedChats(snap, { chats }) {
|
|
240
|
+
const all = await snap.find(CHATS).toArray();
|
|
241
|
+
return chats ? all.filter((chat) => chats.has(chat.id) && !chat.deletedAt) : all;
|
|
242
|
+
}
|
|
243
|
+
async function copyAgentsAndGroups(snap, { into, batch, remap, chats, ungroup }) {
|
|
244
|
+
const used = new Set(chats.map((chat) => chat.agentId));
|
|
245
|
+
for (const agent of await snap.find(AGENTS).toArray()) {
|
|
246
|
+
if (used.has(agent.id) && !remap.has(agent.id) && (await newer(into, AGENTS, agent))) {
|
|
247
|
+
batch.dispatch('put-agent', { agent: stripped(agent) });
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
const groups = ungroup ? [] : await snap.find(CHAT_GROUPS).toArray();
|
|
251
|
+
for (const group of groups) {
|
|
252
|
+
if (await newer(into, CHAT_GROUPS, group)) {
|
|
253
|
+
batch.dispatch('put-chat-group', { group: stripped(group) });
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
async function seededChat(snap, chat, agent) {
|
|
258
|
+
return chat.createdAt === agent.seededAt && (await chunksOf(snap, chat.id)).length === 0;
|
|
259
|
+
}
|
|
260
|
+
async function newer(into, collection, row) {
|
|
261
|
+
const held = await into.view.get(collection, { id: row.id });
|
|
262
|
+
return !held || (held.updatedAt ?? 0) < (row.updatedAt ?? 0);
|
|
263
|
+
}
|
|
264
|
+
async function copyChatChunks(snap, chatId, copy) {
|
|
265
|
+
const { into } = copy;
|
|
266
|
+
const batch = into.batch();
|
|
267
|
+
const chunks = await chunksOf(snap, chatId);
|
|
268
|
+
const open = copy.endsOpenRuns ? runsOf(chunks).open : new Set();
|
|
269
|
+
for (const chunk of chunks) {
|
|
270
|
+
const held = await into.view.get(CHUNKS, { id: chunk.id });
|
|
271
|
+
if (held) {
|
|
272
|
+
await batch.flush();
|
|
273
|
+
await updateHeldChunk(chunk, held, copy);
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
const { seq, updatedAt, attachment, runnerDeviceId, ...rest } = stripped(chunk);
|
|
277
|
+
const sent = copy.chats ? { ...rest, senderMemberId: await senderOf(snap, chunk) } : rest;
|
|
278
|
+
const op = deniedWhileOpen(sent, open);
|
|
279
|
+
if (!attachment) {
|
|
280
|
+
batch.dispatch('append-chunk', op);
|
|
281
|
+
if (chunk.type === 'run-request' && chunk.runId && open.has(chunk.runId)) {
|
|
282
|
+
await endUpgradedRun(chunk, copy, batch);
|
|
283
|
+
}
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
const blobId = attachment.deletedAt
|
|
287
|
+
? removedReference(attachment)
|
|
288
|
+
: await movedBlob(moveOf(copy), attachment);
|
|
289
|
+
const copied = { ...op, attachment: { ...attachment, blobId } };
|
|
290
|
+
batch.dispatch('append-chunk', copied);
|
|
291
|
+
await batch.flush();
|
|
292
|
+
if (!isPendingBlobReference(blobId)) {
|
|
293
|
+
await into.fileIndex?.record([{ ...chunk, ...copied }]);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
await batch.flush();
|
|
297
|
+
}
|
|
298
|
+
function deniedWhileOpen(op, open) {
|
|
299
|
+
const ask = op.approvalRequest;
|
|
300
|
+
if (op.type !== 'approval-request' ||
|
|
301
|
+
ask?.status !== 'pending' ||
|
|
302
|
+
!op.runId ||
|
|
303
|
+
!open.has(op.runId)) {
|
|
304
|
+
return op;
|
|
305
|
+
}
|
|
306
|
+
return { ...op, approvalRequest: { ...ask, status: 'denied' } };
|
|
307
|
+
}
|
|
308
|
+
export const UPGRADED_RUN_REASON = 'the app was updated before this was answered, send it again';
|
|
309
|
+
async function endUpgradedRun(request, copy, batch) {
|
|
310
|
+
await appendChunk(copy.into, {
|
|
311
|
+
chatId: request.chatId,
|
|
312
|
+
runId: request.runId,
|
|
313
|
+
type: 'run-status',
|
|
314
|
+
runStatus: { state: 'failed', reason: UPGRADED_RUN_REASON },
|
|
315
|
+
originDeviceId: copy.deviceId
|
|
316
|
+
}, batch);
|
|
317
|
+
}
|
|
318
|
+
async function senderOf(snap, chunk) {
|
|
319
|
+
if (chunk.type !== 'user-message') {
|
|
320
|
+
return undefined;
|
|
321
|
+
}
|
|
322
|
+
const device = await snap.get(DEVICES, { id: chunk.originDeviceId });
|
|
323
|
+
return sentBy(chunk) ?? device?.memberId ?? undefined;
|
|
324
|
+
}
|
|
325
|
+
async function updateHeldChunk(chunk, held, copy) {
|
|
326
|
+
if (!b4a.equals(chunk.originDeviceId, copy.deviceId))
|
|
327
|
+
return;
|
|
328
|
+
const { into } = copy;
|
|
329
|
+
if (chunk.runStatus) {
|
|
330
|
+
if (newerEdit(chunk, held)) {
|
|
331
|
+
const op = updateOp(chunk, { id: chunk.id, runStatus: chunk.runStatus });
|
|
332
|
+
await into.dispatch('update-chunk', op);
|
|
333
|
+
}
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
const attachment = await repointed(chunk, held, copy);
|
|
337
|
+
if (attachment) {
|
|
338
|
+
await into.dispatch('update-chunk', updateOp(chunk, { id: chunk.id, attachment }));
|
|
339
|
+
await into.fileIndex?.record([{ ...chunk, attachment }]);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
async function repointed(chunk, held, copy) {
|
|
343
|
+
const attachment = chunk.attachment;
|
|
344
|
+
if (!attachment || !held.attachment || held.attachment.deletedAt) {
|
|
345
|
+
return null;
|
|
346
|
+
}
|
|
347
|
+
if (attachment.deletedAt) {
|
|
348
|
+
return newerEdit(chunk, held) ? { ...attachment, blobId: removedReference(attachment) } : null;
|
|
349
|
+
}
|
|
350
|
+
if (isPendingBlobReference(held.attachment.blobId)) {
|
|
351
|
+
const blobId = await movedBlob(moveOf(copy), attachment);
|
|
352
|
+
return isPendingBlobReference(blobId) ? null : { ...attachment, blobId };
|
|
353
|
+
}
|
|
354
|
+
return newerEdit(chunk, held) ? { ...attachment, blobId: held.attachment.blobId } : null;
|
|
355
|
+
}
|
|
356
|
+
function newerEdit(chunk, held) {
|
|
357
|
+
return (chunk.updatedAt ?? 0) > (held.updatedAt ?? 0);
|
|
358
|
+
}
|
|
359
|
+
function moveOf({ from, into, deviceId, references }) {
|
|
360
|
+
return { from, into, chatId: '', originDeviceId: deviceId, references };
|
|
361
|
+
}
|
|
362
|
+
async function copyFiles(snap, copy) {
|
|
363
|
+
const { from, into, chats } = copy;
|
|
364
|
+
const batch = into.batch();
|
|
365
|
+
const rows = await snap.find(KNOWLEDGE).toArray();
|
|
366
|
+
const copied = chats ? rows.filter((row) => !!row.chatId && chats.has(row.chatId)) : rows;
|
|
367
|
+
const ids = new Set(copied.map((row) => row.id));
|
|
368
|
+
for (const row of copied) {
|
|
369
|
+
if (await into.view.get(KNOWLEDGE, { id: row.id })) {
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
const blobId = row.deletedAt ? removedReference(row) : await movedBlob(moveOf(copy), row);
|
|
373
|
+
if (!isPendingBlobReference(blobId)) {
|
|
374
|
+
batch.dispatch('add-knowledge', { knowledge: { ...stripped(row), blobId } });
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
await batch.flush();
|
|
378
|
+
for (const row of await snap.find(KNOWLEDGE_ARTIFACTS).toArray()) {
|
|
379
|
+
if (chats && !ids.has(row.knowledgeId)) {
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
if (await into.view.get(KNOWLEDGE_ARTIFACTS, { id: row.id })) {
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
const artifactBlobId = await copyBlob(from, into, row.artifactBlobId);
|
|
386
|
+
if (artifactBlobId) {
|
|
387
|
+
batch.dispatch('add-knowledge-artifact', { artifact: { ...stripped(row), artifactBlobId } });
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
await batch.flush();
|
|
391
|
+
}
|
|
392
|
+
async function copyToolActivity(snap, { into, chats }) {
|
|
393
|
+
const batch = into.batch();
|
|
394
|
+
for (const row of await snap.find(TOOL_ACTIVITY).toArray()) {
|
|
395
|
+
if (chats && !(row.chatId && chats.has(row.chatId))) {
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
if (await into.view.get(TOOL_ACTIVITY, { id: row.id })) {
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
batch.dispatch('append-tool-activity', { activity: { ...stripped(row), seq: 0 } });
|
|
402
|
+
}
|
|
403
|
+
await batch.flush();
|
|
404
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type ChatListing } from './chat.ts';
|
|
2
|
+
import type { Mesh } from './mesh.ts';
|
|
3
|
+
export interface ForkMeshes {
|
|
4
|
+
readonly from: Mesh;
|
|
5
|
+
readonly into: Mesh;
|
|
6
|
+
readonly fromId?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface ForkChatInput {
|
|
9
|
+
readonly chatId: string;
|
|
10
|
+
readonly runId?: string | null;
|
|
11
|
+
readonly exclusive?: boolean;
|
|
12
|
+
readonly agentId?: string | null;
|
|
13
|
+
readonly title?: string | null;
|
|
14
|
+
readonly originDeviceId: Buffer;
|
|
15
|
+
}
|
|
16
|
+
export declare function forkChat(meshes: ForkMeshes, input: ForkChatInput): Promise<ChatListing>;
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import b4a from 'b4a';
|
|
2
|
+
import { CoreError } from '../errors.js';
|
|
3
|
+
import { landingAgentId, mustGetActiveAgent } from './agent.js';
|
|
4
|
+
import { enrichChat, listSidebar } from './chat.js';
|
|
5
|
+
import { TERMINAL_RUN_STATES } from './contract/run-requests.js';
|
|
6
|
+
import { chunksOf, copyBlob, removedReference, sentBy, stripped } from './chat-copy.js';
|
|
7
|
+
import { chatSortKey } from './chat-order.js';
|
|
8
|
+
import { isPendingBlobReference } from './files/index.js';
|
|
9
|
+
import { groupAttachmentsByMessage } from './files/records/linkage.js';
|
|
10
|
+
import { derivedId, randomId } from './id.js';
|
|
11
|
+
import { rankOnTop } from './rank.js';
|
|
12
|
+
import { CHATS, KNOWLEDGE_BY_CHAT } from './collections.js';
|
|
13
|
+
export async function forkChat(meshes, input) {
|
|
14
|
+
const { into } = meshes;
|
|
15
|
+
if (!into.writable) {
|
|
16
|
+
throw new CoreError('CHAT_TARGET_NOT_WRITER');
|
|
17
|
+
}
|
|
18
|
+
const plan = await forkPlan(meshes, input);
|
|
19
|
+
const batch = into.batch();
|
|
20
|
+
batch.dispatch('put-chat', { chat: plan.chat });
|
|
21
|
+
for (const chunk of plan.chunks) {
|
|
22
|
+
batch.dispatch('append-chunk', chunk);
|
|
23
|
+
}
|
|
24
|
+
const memberId = into.ourMemberId;
|
|
25
|
+
if (memberId && plan.chunks.length > 0) {
|
|
26
|
+
batch.dispatch('mark-chat-read', {
|
|
27
|
+
chatId: plan.chat.id,
|
|
28
|
+
memberId,
|
|
29
|
+
readThrough: plan.chunks.length
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
for (const knowledge of plan.knowledge) {
|
|
33
|
+
batch.dispatch('add-knowledge', { knowledge });
|
|
34
|
+
}
|
|
35
|
+
await batch.flush();
|
|
36
|
+
await into.fileIndex?.record(plan.chunks.filter((chunk) => chunk.attachment));
|
|
37
|
+
return enrichChat(into, plan.chat);
|
|
38
|
+
}
|
|
39
|
+
async function forkPlan(meshes, input) {
|
|
40
|
+
const snap = meshes.from.view.snapshot();
|
|
41
|
+
try {
|
|
42
|
+
const source = await snap.get(CHATS, { id: input.chatId });
|
|
43
|
+
if (!source || source.deletedAt) {
|
|
44
|
+
throw new CoreError('CHAT_NOT_FOUND', [input.chatId]);
|
|
45
|
+
}
|
|
46
|
+
const all = await chunksOf(snap, source.id);
|
|
47
|
+
const cut = forkCut(all, input);
|
|
48
|
+
const documents = await documentsOf(snap, { source, all, cut });
|
|
49
|
+
const ctx = contextOf(meshes, { cut, documents, originDeviceId: input.originDeviceId });
|
|
50
|
+
const agentId = await forkAgentId(ctx, { source, agentId: input.agentId });
|
|
51
|
+
const chunks = await forkedChunks(ctx, cut.chunks);
|
|
52
|
+
const knowledge = await forkedDocuments(ctx, documents);
|
|
53
|
+
const chat = await forkedChat(ctx, { source, cut, agentId, title: input.title });
|
|
54
|
+
return { chat, chunks, knowledge };
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
await snap.close();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function forkCut(chunks, input) {
|
|
61
|
+
const requests = chunks.filter((chunk) => chunk.type === 'run-request' && chunk.runId);
|
|
62
|
+
const finished = finishedRuns(chunks);
|
|
63
|
+
const end = input.runId ? endAt(requests, input) : settledEnd(requests, finished);
|
|
64
|
+
const included = requests.slice(0, end);
|
|
65
|
+
if (included.some((request) => !finished.has(request.runId ?? ''))) {
|
|
66
|
+
throw new CoreError('CHAT_FORK_TURN_RUNNING');
|
|
67
|
+
}
|
|
68
|
+
const turns = new Set(requests.map((request) => request.runId));
|
|
69
|
+
const runs = new Set(included.map((request) => request.runId));
|
|
70
|
+
const next = requests[end];
|
|
71
|
+
const from = next ? turnStart(chunks, { next, included }) : Infinity;
|
|
72
|
+
const kept = [];
|
|
73
|
+
let before = Infinity;
|
|
74
|
+
for (const chunk of chunks) {
|
|
75
|
+
if (turns.has(chunk.runId) ? runs.has(chunk.runId) : chunk.seq < from) {
|
|
76
|
+
kept.push(chunk);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
before = Math.min(before, chunk.createdAt);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return { chunks: kept, before, runId: included.at(-1)?.runId ?? undefined };
|
|
83
|
+
}
|
|
84
|
+
function endAt(requests, { runId, exclusive }) {
|
|
85
|
+
const at = requests.findIndex((request) => request.runId === runId);
|
|
86
|
+
if (at === -1) {
|
|
87
|
+
throw new CoreError('CHAT_FORK_NO_SUCH_TURN');
|
|
88
|
+
}
|
|
89
|
+
const end = exclusive ? at : at + 1;
|
|
90
|
+
if (end === 0) {
|
|
91
|
+
throw new CoreError('CHAT_FORK_NOTHING_BEFORE');
|
|
92
|
+
}
|
|
93
|
+
return end;
|
|
94
|
+
}
|
|
95
|
+
function settledEnd(requests, finished) {
|
|
96
|
+
const running = requests.findIndex((request) => !finished.has(request.runId ?? ''));
|
|
97
|
+
if (running === 0) {
|
|
98
|
+
throw new CoreError('CHAT_FORK_TURN_RUNNING');
|
|
99
|
+
}
|
|
100
|
+
return running === -1 ? requests.length : running;
|
|
101
|
+
}
|
|
102
|
+
function finishedRuns(chunks) {
|
|
103
|
+
const runs = new Set();
|
|
104
|
+
for (const chunk of chunks) {
|
|
105
|
+
if (chunk.runId && TERMINAL_RUN_STATES.has(chunk.runStatus?.state ?? '')) {
|
|
106
|
+
runs.add(chunk.runId);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return runs;
|
|
110
|
+
}
|
|
111
|
+
function turnStart(chunks, { next, included }) {
|
|
112
|
+
const messages = chunks.filter((chunk) => chunk.type === 'user-message' && !chunk.runId);
|
|
113
|
+
const message = messages.findLast((chunk) => chunk.seq < next.seq);
|
|
114
|
+
const answered = (included.at(-1)?.seq ?? -1) > (message?.seq ?? -1);
|
|
115
|
+
if (!message || answered) {
|
|
116
|
+
return next.seq;
|
|
117
|
+
}
|
|
118
|
+
const attachments = chunks.filter((chunk) => chunk.type === 'attachment');
|
|
119
|
+
const bound = groupAttachmentsByMessage(attachments, messages).get(message.id) ?? [];
|
|
120
|
+
return Math.min(message.seq, ...bound.map((chunk) => chunk.seq));
|
|
121
|
+
}
|
|
122
|
+
async function documentsOf(snap, { source, all, cut }) {
|
|
123
|
+
const inChat = { gte: { chatId: source.id }, lte: { chatId: source.id } };
|
|
124
|
+
const rows = await snap.find(KNOWLEDGE_BY_CHAT, inChat).toArray();
|
|
125
|
+
const attached = new Set(all.map(fileIdOf));
|
|
126
|
+
const carried = new Set(cut.chunks.map(fileIdOf));
|
|
127
|
+
return rows.filter((row) => attached.has(row.id) ? carried.has(row.id) : row.createdAt < cut.before);
|
|
128
|
+
}
|
|
129
|
+
function fileIdOf(chunk) {
|
|
130
|
+
return chunk.attachment?.id;
|
|
131
|
+
}
|
|
132
|
+
function contextOf(meshes, { cut, documents, originDeviceId }) {
|
|
133
|
+
const files = cut.chunks.map(fileIdOf).filter((id) => id !== undefined);
|
|
134
|
+
return {
|
|
135
|
+
...meshes,
|
|
136
|
+
same: b4a.equals(meshes.from.key, meshes.into.key),
|
|
137
|
+
chatId: randomId(),
|
|
138
|
+
kept: new Set(cut.chunks.map((chunk) => chunk.id)),
|
|
139
|
+
files: new Set([...files, ...documents.map((row) => row.id)]),
|
|
140
|
+
originDeviceId,
|
|
141
|
+
references: new Map()
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
async function forkAgentId(ctx, { source, agentId }) {
|
|
145
|
+
if (agentId) {
|
|
146
|
+
return (await mustGetActiveAgent(ctx.into, agentId)).id;
|
|
147
|
+
}
|
|
148
|
+
return ctx.same ? source.agentId : landingAgentId(ctx.into, source.agentId);
|
|
149
|
+
}
|
|
150
|
+
function forkedId(ctx, id) {
|
|
151
|
+
return derivedId(id, ctx.chatId);
|
|
152
|
+
}
|
|
153
|
+
async function forkedChunks(ctx, chunks) {
|
|
154
|
+
const forked = [];
|
|
155
|
+
for (const chunk of chunks) {
|
|
156
|
+
const through = chunk.compaction?.throughId;
|
|
157
|
+
if (through && !ctx.kept.has(through)) {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
forked.push(await forkedChunk(ctx, chunk));
|
|
161
|
+
}
|
|
162
|
+
return forked;
|
|
163
|
+
}
|
|
164
|
+
async function forkedChunk(ctx, chunk) {
|
|
165
|
+
const { seq: _seq, updatedAt: _updatedAt, ...op } = stripped(chunk);
|
|
166
|
+
const forked = {
|
|
167
|
+
...op,
|
|
168
|
+
id: forkedId(ctx, chunk.id),
|
|
169
|
+
chatId: ctx.chatId,
|
|
170
|
+
senderMemberId: sentBy(chunk),
|
|
171
|
+
runId: chunk.runId && forkedId(ctx, chunk.runId),
|
|
172
|
+
parentId: chunk.parentId && ctx.kept.has(chunk.parentId) ? forkedId(ctx, chunk.parentId) : null,
|
|
173
|
+
sourceId: chunk.sourceId && forkedId(ctx, chunk.sourceId),
|
|
174
|
+
compaction: chunk.compaction && {
|
|
175
|
+
...chunk.compaction,
|
|
176
|
+
throughId: forkedId(ctx, chunk.compaction.throughId)
|
|
177
|
+
},
|
|
178
|
+
sources: chunk.sources && {
|
|
179
|
+
...chunk.sources,
|
|
180
|
+
sources: chunk.sources.sources.map((source) => forkedSource(ctx, source))
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
if (!chunk.attachment) {
|
|
184
|
+
return forked;
|
|
185
|
+
}
|
|
186
|
+
const blobId = await forkedReference(ctx, chunk.attachment);
|
|
187
|
+
const attachment = { ...chunk.attachment, id: forkedId(ctx, chunk.attachment.id), blobId };
|
|
188
|
+
return { ...forked, attachment, originDeviceId: ctx.originDeviceId };
|
|
189
|
+
}
|
|
190
|
+
function forkedSource(ctx, source) {
|
|
191
|
+
const file = (id) => (id && ctx.files.has(id) ? forkedId(ctx, id) : id);
|
|
192
|
+
return { ...source, documentId: file(source.documentId), attachmentId: file(source.attachmentId) };
|
|
193
|
+
}
|
|
194
|
+
async function forkedDocuments(ctx, rows) {
|
|
195
|
+
const forked = [];
|
|
196
|
+
for (const row of rows) {
|
|
197
|
+
const blobId = await forkedReference(ctx, row);
|
|
198
|
+
forked.push({ ...stripped(row), id: forkedId(ctx, row.id), chatId: ctx.chatId, blobId });
|
|
199
|
+
}
|
|
200
|
+
return forked;
|
|
201
|
+
}
|
|
202
|
+
async function forkedReference(ctx, file) {
|
|
203
|
+
if (file.deletedAt) {
|
|
204
|
+
return removedReference(file);
|
|
205
|
+
}
|
|
206
|
+
if (isPendingBlobReference(file.blobId)) {
|
|
207
|
+
throw new CoreError('CHAT_FORK_FILE_ARRIVING');
|
|
208
|
+
}
|
|
209
|
+
const known = ctx.references.get(file.blobId);
|
|
210
|
+
if (known) {
|
|
211
|
+
return known;
|
|
212
|
+
}
|
|
213
|
+
const copied = await copyBlob(ctx.from, ctx.into, file.blobId);
|
|
214
|
+
if (!copied) {
|
|
215
|
+
throw new CoreError('CHAT_FORK_FILE_NOT_HERE');
|
|
216
|
+
}
|
|
217
|
+
ctx.references.set(file.blobId, copied);
|
|
218
|
+
return copied;
|
|
219
|
+
}
|
|
220
|
+
async function forkedChat(ctx, input) {
|
|
221
|
+
const { source, cut } = input;
|
|
222
|
+
const now = Date.now();
|
|
223
|
+
const groupId = ctx.same ? source.groupId : undefined;
|
|
224
|
+
const { chats } = await listSidebar(ctx.into);
|
|
225
|
+
const siblings = chats.filter((chat) => (chat.groupId || undefined) === (groupId || undefined));
|
|
226
|
+
const top = siblings.length > 0 ? Math.max(...siblings.map(chatSortKey)) : undefined;
|
|
227
|
+
return {
|
|
228
|
+
...stripped(source),
|
|
229
|
+
id: ctx.chatId,
|
|
230
|
+
agentId: input.agentId,
|
|
231
|
+
title: input.title || `${source.title} (2)`,
|
|
232
|
+
createdAt: now,
|
|
233
|
+
updatedAt: now,
|
|
234
|
+
deletedAt: undefined,
|
|
235
|
+
groupId,
|
|
236
|
+
projectId: ctx.same ? source.projectId : undefined,
|
|
237
|
+
rank: rankOnTop(top, now),
|
|
238
|
+
rankedAt: now,
|
|
239
|
+
readThrough: undefined,
|
|
240
|
+
forkedFrom: {
|
|
241
|
+
meshId: ctx.fromId ?? b4a.toHex(ctx.from.key),
|
|
242
|
+
chatId: source.id,
|
|
243
|
+
runId: cut.runId
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
}
|