@johpaz/hive-agents 0.0.47 → 1.0.0
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/LICENSE +21 -0
- package/README.md +119 -902
- package/dist/hive.js +68840 -68355
- package/dist/tool-worker.js +220 -925
- package/dist/ui/assets/A2UIPage-D4nUFe5q.js +8 -0
- package/dist/ui/assets/AgentCreateForm-g1mQnIoJ.js +1 -0
- package/dist/ui/assets/AgentDetailPage-C3XkU4Ll.js +1 -0
- package/dist/ui/assets/AgentNewPage-CEkLeA79.js +1 -0
- package/dist/ui/assets/AgentsPage-CNA778Eo.js +10 -0
- package/dist/ui/assets/ApiClientPage-t5DpKhmA.js +3 -0
- package/dist/ui/assets/ChannelsPage-yY6t0YTU.js +8 -0
- package/dist/ui/assets/DashboardPage-BSeNSYg5.js +6 -0
- package/dist/ui/assets/{LoginPage-BTxJdFVG.js → LoginPage-eT5zDRDa.js} +1 -1
- package/dist/ui/assets/{LogsPage-DVV2eHbh.js → LogsPage-Gczzzsdh.js} +1 -1
- package/dist/ui/assets/MeetingPage-Bu4aMMNR.js +1 -0
- package/dist/ui/assets/ModelSelector-nNOrW3te.js +1 -0
- package/dist/ui/assets/{NotFound-BIUDlIXU.js → NotFound-Bpr12V51.js} +1 -1
- package/dist/ui/assets/Office3DPage-B_iXEbyv.js +4614 -0
- package/dist/ui/assets/Office3DPage-C3SIzd2H.css +1 -0
- package/dist/ui/assets/ProvidersPage-BupHs5yO.js +1 -0
- package/dist/ui/assets/{RecoverPage-7YDQysBL.js → RecoverPage-DMqSasKk.js} +1 -1
- package/dist/ui/assets/SettingsPage-B6FTSkOn.js +9 -0
- package/dist/ui/assets/SetupPage-DhcRbGgg.js +1 -0
- package/dist/ui/assets/WebChatPage-CWXjW4Hn.js +29 -0
- package/dist/ui/assets/{activity-c3pNngT_.js → activity-CQuPevhB.js} +1 -1
- package/dist/ui/assets/agentCapabilities-CDC6y91E.js +1 -0
- package/dist/ui/assets/alert-CQlRq6sv.js +1 -0
- package/dist/ui/assets/arrow-left-0EBE7nsF.js +1 -0
- package/dist/ui/assets/arrow-right-5QIaph-z.js +1 -0
- package/dist/ui/assets/arrow-up-right-DQQCY007.js +1 -0
- package/dist/ui/assets/badge-DKGZbVZJ.js +1 -0
- package/dist/ui/assets/{calendar-B-KZ9RQO.js → calendar-Be23hQGc.js} +1 -1
- package/dist/ui/assets/{card-CNf6BS2e.js → card-D3Nw3kh3.js} +1 -1
- package/dist/ui/assets/check-Dp6tzwax.js +1 -0
- package/dist/ui/assets/chevron-down-33N886NY.js +1 -0
- package/dist/ui/assets/chevron-left-C6itJXh3.js +1 -0
- package/dist/ui/assets/chevron-right-CFodBS-b.js +1 -0
- package/dist/ui/assets/chevron-up-BvSmyBEs.js +1 -0
- package/dist/ui/assets/{circle-alert-KuAm2FWh.js → circle-alert-BDVHxmAz.js} +1 -1
- package/dist/ui/assets/circle-check-BJWNbLuF.js +1 -0
- package/dist/ui/assets/{circle-x-DjLkFDO8.js → circle-x-Cb2Yer4s.js} +1 -1
- package/dist/ui/assets/{copy-Bu5d7C-0.js → copy-33tZrzc9.js} +1 -1
- package/dist/ui/assets/{dialog-Jk4bieO4.js → dialog-KOZKWbib.js} +1 -1
- package/dist/ui/assets/dist-CD_qfA30.js +1 -0
- package/dist/ui/assets/{download-Cjbk4Rek.js → download-CiQqqXyz.js} +1 -1
- package/dist/ui/assets/earth-DCvN7ynm.js +1 -0
- package/dist/ui/assets/ellipsis-vertical-BybFZDk2.js +1 -0
- package/dist/ui/assets/{external-link-6sTlRDUR.js → external-link-0iNNBvZm.js} +1 -1
- package/dist/ui/assets/{eye-Df8o0tkC.js → eye-C8sKSSkg.js} +1 -1
- package/dist/ui/assets/{file-text-lnxnjBp0.js → file-text-DO8jqiJK.js} +1 -1
- package/dist/ui/assets/{gateway-url-DwzPmoc8.js → gateway-url-BlnabM5e.js} +1 -1
- package/dist/ui/assets/gauge-BRGwuN8G.js +1 -0
- package/dist/ui/assets/{history-CAF_R34_.js → history-BtTD1kDo.js} +1 -1
- package/dist/ui/assets/index-Bejlp-R1.js +116 -0
- package/dist/ui/assets/index-XoTT41Hr.css +2 -0
- package/dist/ui/assets/{info-WjromB4Y.js → info-mpk1tsNC.js} +1 -1
- package/dist/ui/assets/key-CoQ0qtXt.js +1 -0
- package/dist/ui/assets/label-k0jYxoWW.js +1 -0
- package/dist/ui/assets/loader-circle-BhqRN2nO.js +1 -0
- package/dist/ui/assets/lock-open-Cd2aqwOd.js +1 -0
- package/dist/ui/assets/mail-BG0RYSkL.js +1 -0
- package/dist/ui/assets/{pause-VqeUmp2Z.js → pause-Cme88_9s.js} +1 -1
- package/dist/ui/assets/pencil-f1nJ1xhs.js +1 -0
- package/dist/ui/assets/play-CfYDHD6h.js +1 -0
- package/dist/ui/assets/plus-C5BxJeOX.js +1 -0
- package/dist/ui/assets/progress-DrI2UDu-.js +1 -0
- package/dist/ui/assets/radio-B5SpKJ-J.js +1 -0
- package/dist/ui/assets/{refresh-cw-CCzDCAuz.js → refresh-cw-CroGQY62.js} +1 -1
- package/dist/ui/assets/scroll-area-CfHn94Vb.js +1 -0
- package/dist/ui/assets/search-rfBcmohK.js +1 -0
- package/dist/ui/assets/select-BhbW0stK.js +1 -0
- package/dist/ui/assets/{send-BPk9XbIq.js → send-D6whYpw-.js} +1 -1
- package/dist/ui/assets/{settings-BGfrZ_zM.js → settings-BX6q3MGB.js} +1 -1
- package/dist/ui/assets/{shield-CxhcUT39.js → shield-BnGyu1xf.js} +1 -1
- package/dist/ui/assets/slider-CmE_z1Ii.js +1 -0
- package/dist/ui/assets/{sparkles-BhwlS1pc.js → sparkles-BcaOs_Jk.js} +1 -1
- package/dist/ui/assets/{square-DMNWw4Hi.js → square-ju5H8nrT.js} +1 -1
- package/dist/ui/assets/switch-DXcELPsM.js +1 -0
- package/dist/ui/assets/tabs-CxiybXcw.js +1 -0
- package/dist/ui/assets/terminal-Bjfu-TkS.js +1 -0
- package/dist/ui/assets/{textarea-B6Z1Zc6W.js → textarea-rp6vsOZl.js} +1 -1
- package/dist/ui/assets/{trash-2-xD2o4SgX.js → trash-2-DjQXac_0.js} +1 -1
- package/dist/ui/assets/{triangle-alert-pVIJGjga.js → triangle-alert-B04mgMcg.js} +1 -1
- package/dist/ui/assets/{useHiveAgentsModelLoad-BnC4jLE-.js → useHiveAgentsModelLoad-DT5-4CCj.js} +1 -1
- package/dist/ui/assets/useOfficeModel-Cy7cAyEn.js +1 -0
- package/dist/ui/assets/useProviders-DSX7eVsz.js +1 -0
- package/dist/ui/assets/utils-Dyb7SC2_.js +1 -0
- package/dist/ui/assets/vendor-charts-K3xcRl0X.js +60 -0
- package/dist/ui/assets/{vendor-query-DsWPbQdG.js → vendor-query-Dj8xYhQD.js} +1 -1
- package/dist/ui/assets/vendor-radix-D0s_k6Gp.js +63 -0
- package/dist/ui/assets/vendor-react-CPJEWbIG.js +1 -0
- package/dist/ui/assets/{vendor-router-gqiZ7xhx.js → vendor-router-zpB-76nT.js} +1 -1
- package/dist/ui/assets/volume-x-qnVxkRSM.js +1 -0
- package/dist/ui/assets/wifi-BA1m2dGO.js +1 -0
- package/dist/ui/assets/{zap-B4RaNNO5.js → zap-DT7cM7YS.js} +1 -1
- package/dist/ui/index.html +10 -9
- package/package.json +8 -9
- package/packages/cli/src/adapters/config.ts +1 -80
- package/packages/cli/src/adapters/factory.ts +0 -45
- package/packages/cli/src/adapters/index.ts +0 -8
- package/packages/cli/src/commands/doctor.ts +45 -1
- package/packages/cli/src/commands/gateway.ts +2 -9
- package/packages/cli/src/index.ts +9 -11
- package/packages/core/src/agent/acceptance-verifier.ts +300 -0
- package/packages/core/src/agent/agent-catalog.ts +384 -0
- package/packages/core/src/agent/agent-loop.ts +185 -28
- package/packages/core/src/agent/capability-search.ts +5 -5
- package/packages/core/src/agent/catalog-selector.ts +104 -0
- package/packages/core/src/agent/compaction.ts +36 -41
- package/packages/core/src/agent/context-compiler.ts +182 -62
- package/packages/core/src/agent/conversation-store.ts +56 -3
- package/packages/core/src/agent/curator.ts +129 -34
- package/packages/core/src/agent/delegation-runtime.ts +248 -0
- package/packages/core/src/agent/index.ts +17 -0
- package/packages/core/src/agent/llm-providers/hiveagents.ts +1 -1
- package/packages/core/src/agent/llm-providers/interface.ts +3 -3
- package/packages/core/src/agent/llm-providers/openai-compat-base.ts +1 -1
- package/packages/core/src/agent/minimal-loadout.ts +46 -0
- package/packages/core/src/agent/playbook-selector.ts +1 -1
- package/packages/core/src/agent/prompt-builder.ts +4 -0
- package/packages/core/src/agent/proof-packet.ts +15 -1
- package/packages/core/src/agent/providers/index.ts +8 -3
- package/packages/core/src/agent/routing-intent.ts +22 -0
- package/packages/core/src/agent/run-store.ts +24 -3
- package/packages/core/src/agent/service.ts +2 -2
- package/packages/core/src/agent/skill-selector.ts +12 -31
- package/packages/core/src/agent/tool-selector.ts +36 -35
- package/packages/core/src/agent/tracer.ts +5 -1
- package/packages/core/src/artifacts/store.ts +162 -0
- package/packages/core/src/canvas/canvas-manager.ts +5 -234
- package/packages/core/src/canvas/emitter.ts +165 -69
- package/packages/core/src/canvas/index.ts +0 -2
- package/packages/core/src/channels/base.ts +3 -1
- package/packages/core/src/channels/webchat.ts +4 -8
- package/packages/core/src/config/loader.ts +0 -22
- package/packages/core/src/events/agent-bus.ts +0 -31
- package/packages/core/src/events/narration.ts +82 -0
- package/packages/core/src/gateway/channel-notify.ts +34 -5
- package/packages/core/src/gateway/delegation-groups.ts +245 -0
- package/packages/core/src/gateway/delegation-notify.ts +103 -0
- package/packages/core/src/gateway/durable-queue.ts +33 -2
- package/packages/core/src/gateway/helpers/index.ts +2 -2
- package/packages/core/src/gateway/helpers/narration.ts +12 -7
- package/packages/core/src/gateway/helpers/redact.ts +1 -1
- package/packages/core/src/gateway/initializer.ts +15 -13
- package/packages/core/src/gateway/job-executors.ts +232 -101
- package/packages/core/src/gateway/lane-queue.ts +1 -1
- package/packages/core/src/gateway/notification-inbox.ts +57 -0
- package/packages/core/src/gateway/resolver.ts +1 -23
- package/packages/core/src/gateway/routes/agents.ts +91 -1
- package/packages/core/src/gateway/routes/auth.ts +21 -42
- package/packages/core/src/gateway/routes/chat.ts +6 -6
- package/packages/core/src/gateway/routes/cron-api.ts +0 -4
- package/packages/core/src/gateway/routes/mcp.ts +0 -63
- package/packages/core/src/gateway/routes/meeting.ts +245 -0
- package/packages/core/src/gateway/routes/providers.ts +1 -0
- package/packages/core/src/gateway/routes/setup.ts +29 -24
- package/packages/core/src/gateway/routes/skills.ts +2 -2
- package/packages/core/src/gateway/routes/system.ts +23 -5
- package/packages/core/src/gateway/routes/users.ts +24 -1
- package/packages/core/src/gateway/server.ts +153 -117
- package/packages/core/src/gateway/session.ts +6 -4
- package/packages/core/src/gateway/slash-commands.ts +7 -6
- package/packages/core/src/gateway/webchat-turn.ts +34 -8
- package/packages/core/src/index.ts +3 -0
- package/packages/core/src/mcp/hot-reload.ts +14 -7
- package/packages/core/src/mcp/singleton.ts +0 -3
- package/packages/core/src/mcp/tool-sync.ts +2 -2
- package/packages/core/src/resilience/retry.ts +1 -1
- package/packages/core/src/scheduler/CronScheduler.ts +3 -1
- package/packages/core/src/scheduler/integration.ts +1 -1
- package/packages/core/src/storage/bootstrap.ts +25 -5
- package/packages/core/src/storage/collections.ts +201 -27
- package/packages/core/src/storage/onboarding.ts +147 -32
- package/packages/core/src/storage/reconcile.ts +2 -5
- package/packages/core/src/storage/seed.ts +196 -34
- package/packages/core/src/storage/usage.ts +0 -9
- package/packages/core/src/storage/user-email.ts +11 -0
- package/packages/core/src/tool-runtime/index.ts +69 -23
- package/packages/core/src/tool-runtime/worker-tools.ts +4 -2
- package/packages/core/src/{canvas/a2ui-tools.ts → tools/a2ui/index.ts} +17 -8
- package/packages/core/src/tools/agents/index.ts +392 -83
- package/packages/core/src/tools/core/index.ts +28 -10
- package/packages/core/src/tools/cron/index.ts +1 -6
- package/packages/core/src/tools/index.ts +17 -45
- package/packages/core/src/tools/types.ts +1 -1
- package/packages/core/src/tools/web/artifact-inspect.ts +23 -0
- package/packages/core/src/tools/web/browser-screenshot.ts +26 -5
- package/packages/core/src/tools/web/index.ts +3 -0
- package/packages/core/src/utils/logger.ts +58 -30
- package/packages/core/src/utils/toon.ts +0 -20
- package/packages/mcp/src/manager.ts +8 -7
- package/packages/mcp/src/transports/sse.ts +0 -4
- package/packages/mcp/src/transports/websocket.ts +0 -6
- package/packages/skills/src/bundled/{canvas → a2ui}/a2ui_dashboard/SKILL.md +4 -4
- package/packages/skills/src/bundled/{canvas → a2ui}/a2ui_form/SKILL.md +4 -4
- package/packages/skills/src/bundled/{canvas → a2ui}/a2ui_interactive/SKILL.md +5 -5
- package/packages/skills/src/bundled/agents/acceptance_verification/SKILL.md +26 -0
- package/packages/skills/src/bundled/agents/agent_spawner/SKILL.md +24 -18
- package/packages/skills/src/bundled/agents/task_orchestrator/SKILL.md +47 -148
- package/packages/skills/src/bundled/cli/cli_pipeline/SKILL.md +9 -10
- package/packages/skills/src/bundled/cli/cli_safe_exec/SKILL.md +1 -1
- package/packages/skills/src/bundled/cli/software_engineering/SKILL.md +23 -0
- package/packages/skills/src/bundled/cron_manager/SKILL.md +2 -2
- package/packages/skills/src/bundled/cron_reminder/SKILL.md +7 -7
- package/packages/skills/src/bundled/filesystem/file_read_and_summarize/SKILL.md +11 -10
- package/packages/skills/src/bundled/filesystem/file_writer/SKILL.md +29 -35
- package/packages/skills/src/bundled/filesystem/workspace_file_operator/SKILL.md +22 -0
- package/packages/skills/src/bundled/search_knowledge/capability_discovery/SKILL.md +5 -4
- package/packages/skills/src/bundled-data.generated.ts +690 -1255
- package/dist/ui/assets/AgentCreateForm-DzaIT-dA.js +0 -1
- package/dist/ui/assets/AgentDetailPage-DgqcJceY.js +0 -1
- package/dist/ui/assets/AgentNewPage-CNSpoOSA.js +0 -1
- package/dist/ui/assets/AgentsPage-BMUJzTD6.js +0 -10
- package/dist/ui/assets/ApiClientPage-Cum_a_3j.js +0 -3
- package/dist/ui/assets/CanvasPage-o6oCP6uq.js +0 -33
- package/dist/ui/assets/ChannelsPage-WrB0zwLq.js +0 -8
- package/dist/ui/assets/DashboardPage-BCd6kNw0.js +0 -6
- package/dist/ui/assets/MeetingPage-BULIAWzq.js +0 -1
- package/dist/ui/assets/ModelSelector-BMYOChYb.js +0 -1
- package/dist/ui/assets/ProvidersPage-BVN5UZrI.js +0 -1
- package/dist/ui/assets/SettingsPage-CYmpFfDn.js +0 -9
- package/dist/ui/assets/SetupPage-Dnmb1Lbv.js +0 -1
- package/dist/ui/assets/WebChatPage-DmmdFhIC.js +0 -16
- package/dist/ui/assets/accordion-CnLzKNHK.js +0 -1
- package/dist/ui/assets/alert-B9J6KHmt.js +0 -1
- package/dist/ui/assets/alert-dialog-KcYQWcJ6.js +0 -1
- package/dist/ui/assets/arrow-left-CBcbX5EZ.js +0 -1
- package/dist/ui/assets/badge-88dRb4M8.js +0 -1
- package/dist/ui/assets/bell-8BqRYmzf.js +0 -1
- package/dist/ui/assets/chevron-down-DIosfU_U.js +0 -1
- package/dist/ui/assets/chevron-left-D4U-5A27.js +0 -1
- package/dist/ui/assets/chevron-right-CR4Skrf3.js +0 -1
- package/dist/ui/assets/chevron-up-CI-W21Fy.js +0 -1
- package/dist/ui/assets/circle-check-6Ard1-2z.js +0 -1
- package/dist/ui/assets/dropdown-menu-DDiaHg5y.js +0 -1
- package/dist/ui/assets/es-4I-joeZz.js +0 -1
- package/dist/ui/assets/folder-open-DJBLDFjv.js +0 -1
- package/dist/ui/assets/format-DsLPBxvt.js +0 -1
- package/dist/ui/assets/gauge-B8Tj43rC.js +0 -1
- package/dist/ui/assets/hexagon-6L79pgVK.js +0 -1
- package/dist/ui/assets/index-BmwTAwOF.css +0 -2
- package/dist/ui/assets/index-Cyw7L0Li.js +0 -116
- package/dist/ui/assets/key-DyKOoQh5.js +0 -1
- package/dist/ui/assets/label-B_15zZlR.js +0 -1
- package/dist/ui/assets/loader-circle-BmBOgYze.js +0 -1
- package/dist/ui/assets/lock-BS6OLXPv.js +0 -1
- package/dist/ui/assets/play-zJpWuhrr.js +0 -1
- package/dist/ui/assets/plus-BZQX26Dr.js +0 -1
- package/dist/ui/assets/progress-D5c-Eilm.js +0 -1
- package/dist/ui/assets/scroll-area-CihOx0cb.js +0 -1
- package/dist/ui/assets/search-DzDptO9s.js +0 -1
- package/dist/ui/assets/select-BQCOjM2j.js +0 -1
- package/dist/ui/assets/slider-bcUiUfx0.js +0 -14
- package/dist/ui/assets/switch-BR30E4ej.js +0 -1
- package/dist/ui/assets/table-B3aGEaVp.js +0 -1
- package/dist/ui/assets/tabs-LQidMKRS.js +0 -1
- package/dist/ui/assets/terminal--7G943As.js +0 -1
- package/dist/ui/assets/useProviders-C1xVKLxB.js +0 -1
- package/dist/ui/assets/utils-3pnRFmFe.js +0 -1
- package/dist/ui/assets/vendor-charts-Bu2lyBKP.js +0 -65
- package/dist/ui/assets/vendor-radix-D6rA7xKY.js +0 -63
- package/dist/ui/assets/vendor-react-BU5iQU4f.js +0 -1
- package/dist/ui/assets/volume-2-BekVQl6P.js +0 -1
- package/packages/core/src/agent/context-guard.ts +0 -91
- package/packages/core/src/agent/hooks.ts +0 -166
- package/packages/core/src/agent/providers.ts +0 -1
- package/packages/core/src/auth/auth.ts +0 -111
- package/packages/core/src/auth/index.ts +0 -1
- package/packages/core/src/canvas/canvas-tools.ts +0 -448
- package/packages/core/src/gateway/router.ts +0 -126
- package/packages/core/src/scheduler/dag/AgentExecutor.ts +0 -53
- package/packages/core/src/scheduler/dag/DAGScheduler.ts +0 -250
- package/packages/core/src/scheduler/dag/EventBridge.ts +0 -122
- package/packages/core/src/scheduler/dag/TaskGraph.ts +0 -192
- package/packages/core/src/scheduler/dag/TaskNode.ts +0 -97
- package/packages/core/src/scheduler/dag/TaskResult.ts +0 -22
- package/packages/core/src/scheduler/dag/errors.ts +0 -37
- package/packages/core/src/scheduler/dag/index.ts +0 -26
- package/packages/core/src/scheduler/dag/presets/ResearchPreset.ts +0 -97
- package/packages/core/src/scheduler/dag/strategies/ParallelStrategy.ts +0 -21
- package/packages/core/src/scheduler/dag/strategies/PriorityStrategy.ts +0 -46
- package/packages/core/src/scheduler/index.ts +0 -22
- package/packages/core/src/scheduler/task-driver.ts +0 -216
- package/packages/core/src/tools/canvas/index.ts +0 -420
- package/packages/core/src/tools/meeting/index.ts +0 -336
- package/packages/core/src/tools/projects/index.ts +0 -348
- package/packages/core/src/tools/voice/index.ts +0 -104
- package/packages/core/src/utils/index.ts +0 -5
- package/packages/skills/src/bundled/agents/code_delegator/SKILL.md +0 -156
- package/packages/skills/src/bundled/canvas/canvas_dashboard/SKILL.md +0 -146
- package/packages/skills/src/bundled/canvas/canvas_interact/SKILL.md +0 -148
- package/packages/skills/src/bundled/canvas/canvas_report/SKILL.md +0 -146
- package/packages/skills/src/bundled/meeting/meeting_transcription/SKILL.md +0 -213
- package/packages/skills/src/bundled/voice/voice_assistant/SKILL.md +0 -174
- package/packages/skills/src/bundled/voice/voice_input/SKILL.md +0 -146
- package/packages/skills/src/bundled/voice/voice_output/SKILL.md +0 -151
|
@@ -224,37 +224,6 @@ export async function getUnreadMessagesForWorker(workerId: string, limit: number
|
|
|
224
224
|
}
|
|
225
225
|
}
|
|
226
226
|
|
|
227
|
-
/**
|
|
228
|
-
* Obtiene el historial de mensajes de un proyecto
|
|
229
|
-
*/
|
|
230
|
-
export async function getProjectMessageHistory(projectId: string, limit: number = 100): Promise<AgentBusMessage[]> {
|
|
231
|
-
try {
|
|
232
|
-
// Primero obtenemos los task_ids del proyecto
|
|
233
|
-
const tasksCol = await col<TaskDoc>("tasks");
|
|
234
|
-
const tasks = (await tasksCol.findBy("project_id", projectId)).map(e => e.doc);
|
|
235
|
-
|
|
236
|
-
if (tasks.length === 0) return [];
|
|
237
|
-
|
|
238
|
-
// Obtenemos los agent_ids de las tareas
|
|
239
|
-
const agentIds = new Set(
|
|
240
|
-
tasks.map(t => fromIndexable(t.agent_id)).filter((id): id is string => id !== null)
|
|
241
|
-
);
|
|
242
|
-
|
|
243
|
-
if (agentIds.size === 0) return [];
|
|
244
|
-
|
|
245
|
-
// Obtenemos mensajes relacionados a estos agents
|
|
246
|
-
const messagesCol = await col<AgentBusMessageDoc>("agentBusMessages");
|
|
247
|
-
const entries = (await messagesCol.scan({}))
|
|
248
|
-
.filter(e => agentIds.has(fromIndexable(e.doc.from_worker_id) ?? ""))
|
|
249
|
-
.sort((a, b) => b.doc.created_at - a.doc.created_at)
|
|
250
|
-
.slice(0, limit);
|
|
251
|
-
|
|
252
|
-
return entries.map(e => docToMessage(e.doc));
|
|
253
|
-
} catch (err) {
|
|
254
|
-
log.error(`Failed to get project message history: ${(err as Error).message}`);
|
|
255
|
-
return [];
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
227
|
|
|
259
228
|
// ─── Agent Bus Implementation ────────────────────────────────────────────────
|
|
260
229
|
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { col } from "../storage/hive";
|
|
3
|
+
import type { NarrationEventDoc } from "../storage/collections";
|
|
4
|
+
import { logger } from "../utils/logger";
|
|
5
|
+
|
|
6
|
+
const log = logger.child("narration");
|
|
7
|
+
|
|
8
|
+
export type NarrationDelivery = (event: NarrationEventDoc) => Promise<void>;
|
|
9
|
+
const STATE_KEY = Symbol.for("hive.narration.delivery");
|
|
10
|
+
type NarrationState = { delivery: NarrationDelivery | null };
|
|
11
|
+
|
|
12
|
+
function state(): NarrationState {
|
|
13
|
+
const root = globalThis as any;
|
|
14
|
+
root[STATE_KEY] ??= { delivery: null };
|
|
15
|
+
return root[STATE_KEY];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function setNarrationDelivery(next: NarrationDelivery | null): void {
|
|
19
|
+
state().delivery = next;
|
|
20
|
+
log.info(`Delivery adapter ${next ? "registered" : "cleared"}`);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function eventId(turnId: string, dedupeKey: string): string {
|
|
24
|
+
return createHash("sha256").update(`${turnId}\0${dedupeKey}`).digest("hex");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function publishNarration(input: {
|
|
28
|
+
turnId: string;
|
|
29
|
+
threadId: string;
|
|
30
|
+
channel?: string | null;
|
|
31
|
+
userId?: string | null;
|
|
32
|
+
sessionId?: string | null;
|
|
33
|
+
agentId?: string | null;
|
|
34
|
+
agentName?: string | null;
|
|
35
|
+
kind: NarrationEventDoc["kind"];
|
|
36
|
+
status: NarrationEventDoc["status"];
|
|
37
|
+
label: string;
|
|
38
|
+
detail?: string | null;
|
|
39
|
+
dedupeKey: string;
|
|
40
|
+
}): Promise<NarrationEventDoc> {
|
|
41
|
+
const id = eventId(input.turnId, input.dedupeKey);
|
|
42
|
+
const events = await col<NarrationEventDoc>("narrationEvents");
|
|
43
|
+
const existing = await events.get(id);
|
|
44
|
+
if (existing) return existing.doc;
|
|
45
|
+
|
|
46
|
+
const event: NarrationEventDoc = {
|
|
47
|
+
id,
|
|
48
|
+
turn_id: input.turnId,
|
|
49
|
+
thread_id: input.threadId,
|
|
50
|
+
channel: input.channel ?? "",
|
|
51
|
+
user_id: input.userId ?? "",
|
|
52
|
+
session_id: input.sessionId ?? "",
|
|
53
|
+
agent_id: input.agentId ?? "",
|
|
54
|
+
agent_name: input.agentName ?? "",
|
|
55
|
+
kind: input.kind,
|
|
56
|
+
status: input.status,
|
|
57
|
+
label: input.label,
|
|
58
|
+
detail: input.detail ?? null,
|
|
59
|
+
dedupe_key: input.dedupeKey,
|
|
60
|
+
created_at: Date.now(),
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
await events.put(id, event, { expectedVersion: 0 });
|
|
65
|
+
} catch {
|
|
66
|
+
const raced = await events.get(id);
|
|
67
|
+
if (raced) return raced.doc;
|
|
68
|
+
throw new Error(`Could not persist narration event ${id}`);
|
|
69
|
+
}
|
|
70
|
+
log.info(`Persisted ${event.kind} for turn=${event.turn_id} agent=${event.agent_id || "coordinator"}`);
|
|
71
|
+
|
|
72
|
+
const delivery = state().delivery;
|
|
73
|
+
if (delivery) {
|
|
74
|
+
try {
|
|
75
|
+
await delivery(event);
|
|
76
|
+
log.info(`Delivered ${event.kind} for turn=${event.turn_id} channel=${event.channel || "none"}`);
|
|
77
|
+
} catch (error) {
|
|
78
|
+
log.warn(`Delivery failed for narration ${id}: ${(error as Error).message}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return event;
|
|
82
|
+
}
|
|
@@ -8,10 +8,19 @@
|
|
|
8
8
|
import { logger } from "../utils/logger"
|
|
9
9
|
import { col } from "../storage/hive"
|
|
10
10
|
import type { UserIdentityDoc } from "../storage/collections"
|
|
11
|
+
import {
|
|
12
|
+
createNotification,
|
|
13
|
+
markNotificationDelivered,
|
|
14
|
+
} from "./notification-inbox"
|
|
11
15
|
|
|
12
16
|
const log = logger.child("channel-notify")
|
|
13
17
|
|
|
14
|
-
type SendFn = (
|
|
18
|
+
type SendFn = (
|
|
19
|
+
channel: string,
|
|
20
|
+
sessionId: string,
|
|
21
|
+
message: string,
|
|
22
|
+
metadata?: { notificationId?: string },
|
|
23
|
+
) => Promise<void>
|
|
15
24
|
|
|
16
25
|
let _sendFn: SendFn | null = null
|
|
17
26
|
|
|
@@ -44,9 +53,25 @@ export async function sendToUserChannel(
|
|
|
44
53
|
channel: string,
|
|
45
54
|
userId: string,
|
|
46
55
|
message: string
|
|
47
|
-
): Promise<{ ok: boolean; error?: string }> {
|
|
56
|
+
): Promise<{ ok: boolean; queued?: boolean; notificationId?: string; error?: string }> {
|
|
57
|
+
let notificationId: string | undefined
|
|
58
|
+
if (channel === "webchat") {
|
|
59
|
+
try {
|
|
60
|
+
const notification = await createNotification({ userId, channel, message })
|
|
61
|
+
notificationId = notification.id
|
|
62
|
+
} catch (err) {
|
|
63
|
+
const error = `Could not persist WebChat notification: ${(err as Error).message}`
|
|
64
|
+
log.warn(`[channel-notify] ${error}`)
|
|
65
|
+
return { ok: false, error }
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
48
69
|
if (!_sendFn) {
|
|
49
|
-
|
|
70
|
+
if (notificationId) {
|
|
71
|
+
log.info(`[channel-notify] WebChat offline — notification ${notificationId} queued`)
|
|
72
|
+
return { ok: true, queued: true, notificationId }
|
|
73
|
+
}
|
|
74
|
+
log.warn("[channel-notify] No send function registered")
|
|
50
75
|
return { ok: false, error: "Channel send not initialized" }
|
|
51
76
|
}
|
|
52
77
|
|
|
@@ -54,10 +79,14 @@ export async function sendToUserChannel(
|
|
|
54
79
|
log.info(`[channel-notify] Sending to ${channel}/${sessionId}: ${message.substring(0, 80)}`)
|
|
55
80
|
|
|
56
81
|
try {
|
|
57
|
-
await _sendFn(channel, sessionId, message)
|
|
58
|
-
|
|
82
|
+
await _sendFn(channel, sessionId, message, { notificationId })
|
|
83
|
+
if (notificationId) await markNotificationDelivered(notificationId, userId)
|
|
84
|
+
return { ok: true, queued: false, notificationId }
|
|
59
85
|
} catch (err) {
|
|
60
86
|
log.warn(`[channel-notify] Failed to send: ${(err as Error).message}`)
|
|
87
|
+
if (notificationId) {
|
|
88
|
+
return { ok: true, queued: true, notificationId }
|
|
89
|
+
}
|
|
61
90
|
return { ok: false, error: (err as Error).message }
|
|
62
91
|
}
|
|
63
92
|
}
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { col } from "../storage/hive";
|
|
2
|
+
import type {
|
|
3
|
+
DelegationGroupDoc,
|
|
4
|
+
DelegationGroupOutcome,
|
|
5
|
+
} from "../storage/collections";
|
|
6
|
+
import { logger } from "../utils/logger";
|
|
7
|
+
import { publishNarration } from "../events/narration";
|
|
8
|
+
|
|
9
|
+
const log = logger.child("delegation-groups");
|
|
10
|
+
const MAX_RETRIES = 8;
|
|
11
|
+
|
|
12
|
+
export type DelegationSummaryEnqueuer = (
|
|
13
|
+
group: DelegationGroupDoc,
|
|
14
|
+
content: string,
|
|
15
|
+
idempotencyKey: string,
|
|
16
|
+
) => Promise<{ id: string }>;
|
|
17
|
+
|
|
18
|
+
let summaryEnqueuer: DelegationSummaryEnqueuer | null = null;
|
|
19
|
+
|
|
20
|
+
export function setDelegationSummaryEnqueuer(enqueuer: DelegationSummaryEnqueuer | null): void {
|
|
21
|
+
summaryEnqueuer = enqueuer;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function taskIds(group: DelegationGroupDoc): string[] {
|
|
25
|
+
return JSON.parse(group.task_ids_json || "[]");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function outcomes(group: DelegationGroupDoc): DelegationGroupOutcome[] {
|
|
29
|
+
return JSON.parse(group.outcomes_json || "[]");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function mutate(
|
|
33
|
+
groupId: string,
|
|
34
|
+
fn: (doc: DelegationGroupDoc) => DelegationGroupDoc,
|
|
35
|
+
): Promise<DelegationGroupDoc> {
|
|
36
|
+
const groups = await col<DelegationGroupDoc>("delegationGroups");
|
|
37
|
+
for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {
|
|
38
|
+
const current = await groups.get(groupId);
|
|
39
|
+
if (!current) throw new Error(`Delegation group not found: ${groupId}`);
|
|
40
|
+
const next = fn(current.doc);
|
|
41
|
+
try {
|
|
42
|
+
await groups.put(groupId, next, { expectedVersion: current.version });
|
|
43
|
+
return next;
|
|
44
|
+
} catch {
|
|
45
|
+
// optimistic concurrency conflict
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
throw new Error(`Delegation group contention: ${groupId}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export async function registerDelegatedTask(input: {
|
|
52
|
+
turnId: string;
|
|
53
|
+
taskId: string;
|
|
54
|
+
threadId: string;
|
|
55
|
+
channel?: string | null;
|
|
56
|
+
userId?: string | null;
|
|
57
|
+
sessionId?: string | null;
|
|
58
|
+
coordinatorAgentId?: string | null;
|
|
59
|
+
}): Promise<DelegationGroupDoc> {
|
|
60
|
+
const groups = await col<DelegationGroupDoc>("delegationGroups");
|
|
61
|
+
const existing = await groups.get(input.turnId);
|
|
62
|
+
if (!existing) {
|
|
63
|
+
const now = Date.now();
|
|
64
|
+
const created: DelegationGroupDoc = {
|
|
65
|
+
id: input.turnId,
|
|
66
|
+
turn_id: input.turnId,
|
|
67
|
+
thread_id: input.threadId,
|
|
68
|
+
channel: input.channel ?? "",
|
|
69
|
+
user_id: input.userId ?? "",
|
|
70
|
+
session_id: input.sessionId ?? "",
|
|
71
|
+
coordinator_agent_id: input.coordinatorAgentId ?? "",
|
|
72
|
+
status: "open",
|
|
73
|
+
task_ids_json: JSON.stringify([input.taskId]),
|
|
74
|
+
outcomes_json: "[]",
|
|
75
|
+
summary_job_id: null,
|
|
76
|
+
created_at: now,
|
|
77
|
+
sealed_at: null,
|
|
78
|
+
ready_at: null,
|
|
79
|
+
notified_at: null,
|
|
80
|
+
};
|
|
81
|
+
try {
|
|
82
|
+
await groups.put(input.turnId, created, { expectedVersion: 0 });
|
|
83
|
+
return created;
|
|
84
|
+
} catch {
|
|
85
|
+
// Another parallel task created the group; append below.
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return mutate(input.turnId, (group) => {
|
|
90
|
+
if (group.status !== "open") {
|
|
91
|
+
throw new Error(`Delegation group ${input.turnId} is already sealed`);
|
|
92
|
+
}
|
|
93
|
+
const ids = taskIds(group);
|
|
94
|
+
if (!ids.includes(input.taskId)) ids.push(input.taskId);
|
|
95
|
+
return { ...group, task_ids_json: JSON.stringify(ids) };
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export async function getDelegationGroup(turnId: string): Promise<DelegationGroupDoc | null> {
|
|
100
|
+
return (await (await col<DelegationGroupDoc>("delegationGroups")).get(turnId))?.doc ?? null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function isComplete(group: DelegationGroupDoc): boolean {
|
|
104
|
+
const expected = new Set(taskIds(group));
|
|
105
|
+
const completed = new Set(outcomes(group).map((outcome) => outcome.task_id));
|
|
106
|
+
return expected.size > 0 && [...expected].every((id) => completed.has(id));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function summaryPrompt(group: DelegationGroupDoc): string {
|
|
110
|
+
const factual = outcomes(group).map((outcome) => ({
|
|
111
|
+
task_id: outcome.task_id,
|
|
112
|
+
worker_id: outcome.worker_id,
|
|
113
|
+
task: outcome.task_name,
|
|
114
|
+
ok: outcome.ok,
|
|
115
|
+
result: outcome.result,
|
|
116
|
+
error: outcome.error,
|
|
117
|
+
}));
|
|
118
|
+
// No manual framing here — persisted with source:"delegation_summary" and
|
|
119
|
+
// framed at serialization time by formatInternalEvent (conversation-store.ts).
|
|
120
|
+
return [
|
|
121
|
+
"Todas las tareas delegadas de este turno alcanzaron estado terminal.",
|
|
122
|
+
"Sintetiza UNA sola respuesta final para el usuario basándote exclusivamente en estos resultados y sus verificaciones.",
|
|
123
|
+
"No declares éxito para resultados con ok=false y no inventes trabajo ni evidencia.",
|
|
124
|
+
"Escribí en lenguaje natural para un humano: nunca expongas task_id, worker_id, verification_id, nombres de tools ni JSON crudo.",
|
|
125
|
+
JSON.stringify(factual),
|
|
126
|
+
].join("\n\n");
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async function finalizeIfReady(groupId: string): Promise<DelegationGroupDoc> {
|
|
130
|
+
let group = await getDelegationGroup(groupId);
|
|
131
|
+
if (!group || group.status === "open" || group.status === "notified" || !isComplete(group)) {
|
|
132
|
+
return group!;
|
|
133
|
+
}
|
|
134
|
+
if (group.status === "sealed") {
|
|
135
|
+
group = await mutate(groupId, (current) => {
|
|
136
|
+
if (current.status !== "sealed" || !isComplete(current)) return current;
|
|
137
|
+
return { ...current, status: "ready", ready_at: Date.now() };
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
if (group.status !== "ready") return group;
|
|
141
|
+
if (!summaryEnqueuer) {
|
|
142
|
+
log.warn(`Group ${groupId} is ready but no summary enqueuer is registered`);
|
|
143
|
+
return group;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const groups = await col<DelegationGroupDoc>("delegationGroups");
|
|
147
|
+
let claimed = false;
|
|
148
|
+
for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {
|
|
149
|
+
const current = await groups.get(groupId);
|
|
150
|
+
if (!current) return group;
|
|
151
|
+
if (current.doc.status !== "ready" || current.doc.summary_job_id !== null) {
|
|
152
|
+
return current.doc;
|
|
153
|
+
}
|
|
154
|
+
try {
|
|
155
|
+
await groups.put(groupId, {
|
|
156
|
+
...current.doc,
|
|
157
|
+
summary_job_id: "__claim__",
|
|
158
|
+
}, { expectedVersion: current.version });
|
|
159
|
+
group = { ...current.doc, summary_job_id: "__claim__" };
|
|
160
|
+
claimed = true;
|
|
161
|
+
break;
|
|
162
|
+
} catch {
|
|
163
|
+
// another terminal hook is racing us
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (!claimed) return (await getDelegationGroup(groupId))!;
|
|
167
|
+
|
|
168
|
+
let job: { id: string };
|
|
169
|
+
try {
|
|
170
|
+
job = await summaryEnqueuer(
|
|
171
|
+
group,
|
|
172
|
+
summaryPrompt(group),
|
|
173
|
+
`delegation-summary:${group.id}`,
|
|
174
|
+
);
|
|
175
|
+
} catch (error) {
|
|
176
|
+
await mutate(groupId, (current) => current.summary_job_id === "__claim__"
|
|
177
|
+
? { ...current, summary_job_id: null }
|
|
178
|
+
: current);
|
|
179
|
+
throw error;
|
|
180
|
+
}
|
|
181
|
+
group = await mutate(groupId, (current) => {
|
|
182
|
+
if (current.status === "notified") return current;
|
|
183
|
+
return {
|
|
184
|
+
...current,
|
|
185
|
+
status: "notified",
|
|
186
|
+
summary_job_id: job.id,
|
|
187
|
+
notified_at: Date.now(),
|
|
188
|
+
};
|
|
189
|
+
});
|
|
190
|
+
await publishNarration({
|
|
191
|
+
turnId: group.turn_id,
|
|
192
|
+
threadId: group.thread_id,
|
|
193
|
+
channel: group.channel,
|
|
194
|
+
userId: group.user_id,
|
|
195
|
+
sessionId: group.session_id,
|
|
196
|
+
agentId: group.coordinator_agent_id,
|
|
197
|
+
agentName: "Coordinador",
|
|
198
|
+
kind: "group_ready",
|
|
199
|
+
status: "done",
|
|
200
|
+
label: "Todas las entregas fueron revisadas; el coordinador prepara una única respuesta final",
|
|
201
|
+
dedupeKey: "delegation_group_ready",
|
|
202
|
+
});
|
|
203
|
+
return group;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export async function sealDelegationGroup(turnId: string): Promise<DelegationGroupDoc | null> {
|
|
207
|
+
const existing = await getDelegationGroup(turnId);
|
|
208
|
+
if (!existing) return null;
|
|
209
|
+
if (existing.status === "open") {
|
|
210
|
+
await mutate(turnId, (group) => group.status === "open"
|
|
211
|
+
? { ...group, status: "sealed", sealed_at: Date.now() }
|
|
212
|
+
: group);
|
|
213
|
+
}
|
|
214
|
+
return finalizeIfReady(turnId);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export async function recordDelegationOutcome(input: {
|
|
218
|
+
turnId: string;
|
|
219
|
+
taskId: string;
|
|
220
|
+
jobId: string;
|
|
221
|
+
workerId: string;
|
|
222
|
+
taskName: string;
|
|
223
|
+
ok: boolean;
|
|
224
|
+
result?: unknown;
|
|
225
|
+
error?: string | null;
|
|
226
|
+
}): Promise<DelegationGroupDoc | null> {
|
|
227
|
+
const existing = await getDelegationGroup(input.turnId);
|
|
228
|
+
if (!existing) return null;
|
|
229
|
+
await mutate(input.turnId, (group) => {
|
|
230
|
+
const current = outcomes(group);
|
|
231
|
+
if (current.some((outcome) => outcome.task_id === input.taskId)) return group;
|
|
232
|
+
current.push({
|
|
233
|
+
task_id: input.taskId,
|
|
234
|
+
job_id: input.jobId,
|
|
235
|
+
worker_id: input.workerId,
|
|
236
|
+
task_name: input.taskName,
|
|
237
|
+
ok: input.ok,
|
|
238
|
+
result: input.result ?? null,
|
|
239
|
+
error: input.error ?? null,
|
|
240
|
+
finished_at: Date.now(),
|
|
241
|
+
});
|
|
242
|
+
return { ...group, outcomes_json: JSON.stringify(current) };
|
|
243
|
+
});
|
|
244
|
+
return finalizeIfReady(input.turnId);
|
|
245
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* delegation-notify — closes the loop on `task_delegate(mode:"async")`.
|
|
3
|
+
*
|
|
4
|
+
* Before this, an async-delegated worker_task was fire-and-forget: nothing
|
|
5
|
+
* told the user (or the coordinator) when the delegated agent finished. This
|
|
6
|
+
* registers a terminal hook (durable-queue.ts) that, on completion or final
|
|
7
|
+
* failure, enqueues a synthetic chat turn in the ORIGINATING conversation's
|
|
8
|
+
* lane so the coordinator can relay the outcome — reusing the exact delivery
|
|
9
|
+
* path already used for crash-recovered turns (webchat-turn.ts's
|
|
10
|
+
* `sendToUserChannel` fallback when no live socket is attached).
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { logger } from "../utils/logger";
|
|
14
|
+
import { registerTerminalHook, type JobTerminalOutcome } from "./durable-queue";
|
|
15
|
+
import { enqueueChatTurn } from "./webchat-turn";
|
|
16
|
+
import type { JobDoc } from "../storage/collections";
|
|
17
|
+
import {
|
|
18
|
+
recordDelegationOutcome,
|
|
19
|
+
setDelegationSummaryEnqueuer,
|
|
20
|
+
} from "./delegation-groups";
|
|
21
|
+
|
|
22
|
+
const log = logger.child("delegation-notify");
|
|
23
|
+
|
|
24
|
+
interface WorkerTaskPayload {
|
|
25
|
+
originThreadId?: string | null;
|
|
26
|
+
taskName?: string;
|
|
27
|
+
workerId?: string;
|
|
28
|
+
taskId?: string;
|
|
29
|
+
turnId?: string | null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Inverts enqueueChatTurn's `thread_id = webchat:${sessionId}` (or raw sessionId for "api") mapping (webchat-turn.ts:438). */
|
|
33
|
+
function deriveSessionId(originThreadId: string): string {
|
|
34
|
+
const prefix = "webchat:";
|
|
35
|
+
return originThreadId.startsWith(prefix) ? originThreadId.slice(prefix.length) : originThreadId;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function summarize(value: unknown, maxLen = 500): string {
|
|
39
|
+
const text = typeof value === "string" ? value : JSON.stringify(value);
|
|
40
|
+
return text.length > maxLen ? `${text.slice(0, maxLen)}…` : text;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function notifyWorkerTaskOutcome(job: JobDoc, outcome: JobTerminalOutcome): Promise<void> {
|
|
44
|
+
const payload = JSON.parse(job.payload_json) as WorkerTaskPayload;
|
|
45
|
+
if (payload.turnId && payload.taskId) {
|
|
46
|
+
await recordDelegationOutcome({
|
|
47
|
+
turnId: payload.turnId,
|
|
48
|
+
taskId: payload.taskId,
|
|
49
|
+
jobId: job.id,
|
|
50
|
+
workerId: payload.workerId ?? "",
|
|
51
|
+
taskName: payload.taskName ?? "tarea delegada",
|
|
52
|
+
ok: outcome.ok,
|
|
53
|
+
result: outcome.result,
|
|
54
|
+
error: outcome.error ?? null,
|
|
55
|
+
});
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (!payload.originThreadId) return; // sync delegation or no coordinator conversation to report back to
|
|
59
|
+
|
|
60
|
+
const sessionId = deriveSessionId(payload.originThreadId);
|
|
61
|
+
const taskName = payload.taskName || "la tarea delegada";
|
|
62
|
+
|
|
63
|
+
// No manual framing here — persisted with source:"task_complete" and framed
|
|
64
|
+
// at serialization time by formatInternalEvent (conversation-store.ts).
|
|
65
|
+
const content = outcome.ok
|
|
66
|
+
? `El agente completó la tarea "${taskName}". Resultado: ${summarize(outcome.result)}`
|
|
67
|
+
: `El agente NO pudo completar la tarea "${taskName}". Motivo: ${summarize(outcome.error ?? "error desconocido")}`;
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
await enqueueChatTurn({
|
|
71
|
+
lane: sessionId,
|
|
72
|
+
payload: { source: "task_complete", sessionId, content },
|
|
73
|
+
});
|
|
74
|
+
log.info(`[notifyWorkerTaskOutcome] Enqueued task_complete notice for session ${sessionId} (job ${job.id}, ok=${outcome.ok})`);
|
|
75
|
+
} catch (err) {
|
|
76
|
+
log.warn(`[notifyWorkerTaskOutcome] Failed to enqueue completion notice: ${(err as Error).message}`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
let initialized = false;
|
|
81
|
+
|
|
82
|
+
export function initDelegationNotify(): void {
|
|
83
|
+
if (initialized) return;
|
|
84
|
+
registerTerminalHook("worker_task", notifyWorkerTaskOutcome);
|
|
85
|
+
setDelegationSummaryEnqueuer(async (group, content, idempotencyKey) => {
|
|
86
|
+
const sessionId = group.session_id || deriveSessionId(group.thread_id);
|
|
87
|
+
return enqueueChatTurn({
|
|
88
|
+
lane: sessionId,
|
|
89
|
+
payload: {
|
|
90
|
+
source: "delegation_summary",
|
|
91
|
+
sessionId,
|
|
92
|
+
content,
|
|
93
|
+
rawContent: content,
|
|
94
|
+
userId: group.user_id,
|
|
95
|
+
agentId: group.coordinator_agent_id,
|
|
96
|
+
channel: group.channel,
|
|
97
|
+
},
|
|
98
|
+
idempotencyKey,
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
initialized = true;
|
|
102
|
+
log.info("[initDelegationNotify] Registered worker_task terminal hook");
|
|
103
|
+
}
|
|
@@ -76,6 +76,32 @@ export function registerExecutor(type: JobType, executor: JobExecutor): void {
|
|
|
76
76
|
log.info(`[registerExecutor] Registered executor for type=${type}`);
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
+
export interface JobTerminalOutcome {
|
|
80
|
+
ok: boolean;
|
|
81
|
+
result?: unknown;
|
|
82
|
+
error?: string;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Fires exactly once when a job of `type` reaches a terminal state (completed, or failed with no retries left) — never on a retryable failure that gets requeued. */
|
|
86
|
+
export type JobTerminalHook = (job: JobDoc, outcome: JobTerminalOutcome) => void | Promise<void>;
|
|
87
|
+
|
|
88
|
+
const terminalHooks = new Map<JobType, JobTerminalHook>();
|
|
89
|
+
|
|
90
|
+
/** Register a side-effect to run once a job type finishes for good. Kept decoupled from executors so this module doesn't need to import webchat-turn.ts (circular: webchat-turn.ts already imports durable-queue.ts for enqueueChatTurn). */
|
|
91
|
+
export function registerTerminalHook(type: JobType, hook: JobTerminalHook): void {
|
|
92
|
+
terminalHooks.set(type, hook);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async function runTerminalHook(job: JobDoc, outcome: JobTerminalOutcome): Promise<void> {
|
|
96
|
+
const hook = terminalHooks.get(job.type);
|
|
97
|
+
if (!hook) return;
|
|
98
|
+
try {
|
|
99
|
+
await hook(job, outcome);
|
|
100
|
+
} catch (err) {
|
|
101
|
+
log.warn(`[runTerminalHook] Hook for type=${job.type} failed: ${(err as Error).message}`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
79
105
|
export class DurableLaneQueue {
|
|
80
106
|
private maxGlobalConcurrency: number;
|
|
81
107
|
private taskTimeoutMs: number;
|
|
@@ -280,14 +306,17 @@ export class DurableLaneQueue {
|
|
|
280
306
|
|
|
281
307
|
if (result.ok) {
|
|
282
308
|
await completeJob(job.id, result.result ?? null, this.bootId);
|
|
309
|
+
await runTerminalHook(job, { ok: true, result: result.result });
|
|
283
310
|
} else {
|
|
284
311
|
const error = result.error ?? "Unknown error";
|
|
285
312
|
// chat_turn is user-facing — never auto-retry a logical failure, the
|
|
286
313
|
// user just sees the error and can re-ask.
|
|
287
314
|
if (job.type !== "chat_turn" && result.retryable !== false) {
|
|
288
|
-
await failJobOrRetry(job.id, error, this.bootId, this.jobRetryPolicy);
|
|
315
|
+
const updated = await failJobOrRetry(job.id, error, this.bootId, this.jobRetryPolicy);
|
|
316
|
+
if (updated?.status === "failed") await runTerminalHook(job, { ok: false, error });
|
|
289
317
|
} else {
|
|
290
318
|
await failJob(job.id, error, this.bootId);
|
|
319
|
+
await runTerminalHook(job, { ok: false, error });
|
|
291
320
|
}
|
|
292
321
|
}
|
|
293
322
|
} catch (err) {
|
|
@@ -296,9 +325,11 @@ export class DurableLaneQueue {
|
|
|
296
325
|
} else {
|
|
297
326
|
const error = (err as Error).message;
|
|
298
327
|
if (job.type !== "chat_turn") {
|
|
299
|
-
await failJobOrRetry(job.id, error, this.bootId, this.jobRetryPolicy);
|
|
328
|
+
const updated = await failJobOrRetry(job.id, error, this.bootId, this.jobRetryPolicy);
|
|
329
|
+
if (updated?.status === "failed") await runTerminalHook(job, { ok: false, error });
|
|
300
330
|
} else {
|
|
301
331
|
await failJob(job.id, error, this.bootId);
|
|
332
|
+
await runTerminalHook(job, { ok: false, error });
|
|
302
333
|
}
|
|
303
334
|
}
|
|
304
335
|
} finally {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { getNarration } from "./narration.ts";
|
|
2
2
|
export { expandPath } from "./path.ts";
|
|
3
3
|
export { CORS_ORIGINS, addCorsHeaders } from "./cors.ts";
|
|
4
|
-
export {
|
|
4
|
+
export { redactConfig } from "./redact.ts";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// ─── Tool narration map ───────────────────────────────────────────────────────
|
|
2
2
|
// Maps tool name prefixes/exact names to human-readable Spanish narrations.
|
|
3
3
|
// Shown to the user while the agent executes a tool.
|
|
4
|
-
|
|
4
|
+
const TOOL_NARRATIONS: Record<string, string> = {
|
|
5
5
|
// Web
|
|
6
6
|
web_search: "Buscando en la web...",
|
|
7
7
|
web_fetch: "Leyendo página web...",
|
|
@@ -20,9 +20,17 @@ export const TOOL_NARRATIONS: Record<string, string> = {
|
|
|
20
20
|
"cron.trigger": "Ejecutando tarea ahora...",
|
|
21
21
|
"cron.history": "Consultando historial...",
|
|
22
22
|
// Agents
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
agent_create: "Creando agente worker...",
|
|
24
|
+
agent_find: "Buscando agente disponible...",
|
|
25
|
+
agent_archive: "Archivando agente...",
|
|
26
|
+
get_available_models: "Consultando modelos disponibles...",
|
|
27
|
+
task_delegate: "Delegando tarea a un agente...",
|
|
28
|
+
task_list: "Listando tareas en ejecución...",
|
|
29
|
+
task_status: "Consultando estado de la tarea...",
|
|
30
|
+
bus_publish: "Coordinando con otro agente...",
|
|
31
|
+
bus_read: "Leyendo mensajes de los agentes...",
|
|
32
|
+
// Discovery
|
|
33
|
+
search_knowledge: "Buscando capacidades disponibles...",
|
|
26
34
|
// Memory
|
|
27
35
|
save_note: "Guardando nota...",
|
|
28
36
|
memory_write: "Guardando en memoria...",
|
|
@@ -39,9 +47,6 @@ export const TOOL_NARRATIONS: Record<string, string> = {
|
|
|
39
47
|
// Canvas
|
|
40
48
|
canvas_add_node: "Actualizando canvas...",
|
|
41
49
|
canvas_update: "Actualizando canvas...",
|
|
42
|
-
// Code Bridge
|
|
43
|
-
bridge_send: "Enviando tarea al CLI...",
|
|
44
|
-
bridge_exec: "Ejecutando en el Code Bridge...",
|
|
45
50
|
// Notify
|
|
46
51
|
notify: "Enviando notificación...",
|
|
47
52
|
report_progress: "Reportando progreso...",
|
|
@@ -5,7 +5,7 @@ import type { Config } from "../../config/loader.ts";
|
|
|
5
5
|
* @param value - The value to redact
|
|
6
6
|
* @returns The redacted value or a string of bullets if too short
|
|
7
7
|
*/
|
|
8
|
-
|
|
8
|
+
function redactValue(value: string): string {
|
|
9
9
|
if (!value || value.length < 8) return "••••••••";
|
|
10
10
|
return `${value.slice(0, 4)}••••••••`;
|
|
11
11
|
}
|