@swarmclawai/swarmclaw 0.8.4 → 0.8.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -9
- package/bin/swarmclaw.js +5 -1
- package/bin/worker-cmd.js +73 -0
- package/package.json +2 -1
- package/src/app/api/agents/[id]/route.ts +17 -7
- package/src/app/api/agents/route.ts +21 -8
- package/src/app/api/approvals/route.test.ts +6 -6
- package/src/app/api/approvals/route.ts +2 -1
- package/src/app/api/auth/route.ts +2 -3
- package/src/app/api/chatrooms/[id]/chat/route.test.ts +299 -0
- package/src/app/api/chatrooms/[id]/chat/route.ts +3 -2
- package/src/app/api/chatrooms/[id]/route.ts +7 -6
- package/src/app/api/chats/[id]/chat/route.test.ts +496 -0
- package/src/app/api/chats/[id]/chat/route.ts +7 -3
- package/src/app/api/chats/[id]/clear/route.ts +9 -9
- package/src/app/api/chats/[id]/devserver/route.ts +2 -1
- package/src/app/api/chats/[id]/edit-resend/route.ts +3 -4
- package/src/app/api/chats/[id]/fork/route.ts +3 -5
- package/src/app/api/chats/[id]/restore/route.ts +6 -7
- package/src/app/api/chats/[id]/retry/route.ts +3 -4
- package/src/app/api/chats/[id]/route.ts +61 -62
- package/src/app/api/chats/route.ts +7 -1
- package/src/app/api/connectors/[id]/route.ts +7 -8
- package/src/app/api/connectors/route.ts +5 -4
- package/src/app/api/eval/run/route.ts +2 -1
- package/src/app/api/eval/suite/route.ts +2 -1
- package/src/app/api/external-agents/route.test.ts +1 -1
- package/src/app/api/external-agents/route.ts +2 -2
- package/src/app/api/files/serve/route.ts +1 -1
- package/src/app/api/gateways/[id]/route.ts +7 -5
- package/src/app/api/gateways/route.ts +1 -1
- package/src/app/api/knowledge/upload/route.ts +1 -1
- package/src/app/api/logs/route.ts +5 -7
- package/src/app/api/memory-images/[filename]/route.ts +2 -3
- package/src/app/api/openclaw/agent-files/route.ts +4 -3
- package/src/app/api/openclaw/approvals/route.ts +3 -4
- package/src/app/api/openclaw/config-sync/route.ts +3 -2
- package/src/app/api/openclaw/cron/route.ts +3 -2
- package/src/app/api/openclaw/dotenv-keys/route.ts +2 -1
- package/src/app/api/openclaw/exec-config/route.ts +3 -2
- package/src/app/api/openclaw/gateway/route.ts +5 -4
- package/src/app/api/openclaw/history/route.ts +3 -2
- package/src/app/api/openclaw/media/route.ts +2 -1
- package/src/app/api/openclaw/permissions/route.ts +3 -2
- package/src/app/api/openclaw/sandbox-env/route.ts +3 -2
- package/src/app/api/openclaw/skills/install/route.ts +2 -1
- package/src/app/api/openclaw/skills/remove/route.ts +2 -1
- package/src/app/api/openclaw/skills/route.ts +3 -2
- package/src/app/api/orchestrator/run/route.ts +5 -14
- package/src/app/api/perf/route.ts +43 -0
- package/src/app/api/plugins/dependencies/route.ts +2 -1
- package/src/app/api/plugins/install/route.ts +2 -1
- package/src/app/api/plugins/marketplace/route.ts +3 -2
- package/src/app/api/plugins/settings/route.ts +2 -1
- package/src/app/api/preview-server/route.ts +11 -10
- package/src/app/api/projects/[id]/route.ts +1 -1
- package/src/app/api/schedules/[id]/route.test.ts +128 -0
- package/src/app/api/schedules/[id]/route.ts +43 -43
- package/src/app/api/schedules/[id]/run/route.ts +11 -62
- package/src/app/api/schedules/route.ts +21 -87
- package/src/app/api/settings/route.ts +2 -0
- package/src/app/api/setup/doctor/route.ts +9 -8
- package/src/app/api/tasks/[id]/approve/route.ts +33 -30
- package/src/app/api/tasks/[id]/route.ts +12 -35
- package/src/app/api/tasks/import/github/route.ts +2 -1
- package/src/app/api/tasks/route.ts +79 -91
- package/src/app/api/wallets/[id]/approve/route.ts +2 -1
- package/src/app/api/wallets/[id]/route.ts +13 -19
- package/src/app/api/wallets/[id]/send/route.ts +2 -1
- package/src/app/api/wallets/route.ts +2 -1
- package/src/app/api/webhooks/[id]/route.ts +2 -1
- package/src/app/api/webhooks/route.test.ts +3 -1
- package/src/app/page.tsx +23 -331
- package/src/cli/index.js +19 -0
- package/src/cli/index.ts +38 -7
- package/src/cli/spec.js +9 -0
- package/src/components/activity/activity-feed.tsx +7 -4
- package/src/components/agents/agent-card.tsx +32 -6
- package/src/components/agents/agent-chat-list.tsx +55 -22
- package/src/components/agents/agent-files-editor.tsx +3 -2
- package/src/components/agents/agent-sheet.tsx +123 -22
- package/src/components/agents/inspector-panel.tsx +1 -1
- package/src/components/agents/openclaw-skills-panel.tsx +2 -1
- package/src/components/agents/trash-list.tsx +1 -1
- package/src/components/auth/access-key-gate.tsx +8 -2
- package/src/components/auth/setup-wizard.tsx +10 -9
- package/src/components/auth/user-picker.tsx +3 -2
- package/src/components/chat/chat-area.tsx +20 -1
- package/src/components/chat/chat-card.tsx +18 -3
- package/src/components/chat/chat-header.tsx +24 -4
- package/src/components/chat/chat-list.tsx +2 -11
- package/src/components/chat/heartbeat-history-panel.tsx +2 -1
- package/src/components/chat/message-bubble.tsx +45 -6
- package/src/components/chat/message-list.tsx +280 -145
- package/src/components/chat/streaming-bubble.tsx +217 -60
- package/src/components/chat/swarm-panel.test.ts +274 -0
- package/src/components/chat/swarm-panel.tsx +410 -0
- package/src/components/chat/swarm-status-card.tsx +346 -0
- package/src/components/chat/tool-call-bubble.tsx +48 -23
- package/src/components/chatrooms/chatroom-list.tsx +8 -5
- package/src/components/chatrooms/chatroom-message.tsx +10 -7
- package/src/components/chatrooms/chatroom-view.tsx +12 -9
- package/src/components/connectors/connector-health.tsx +6 -4
- package/src/components/connectors/connector-list.tsx +16 -11
- package/src/components/connectors/connector-sheet.tsx +12 -6
- package/src/components/home/home-view.tsx +38 -24
- package/src/components/input/chat-input.tsx +10 -1
- package/src/components/layout/app-layout.tsx +2 -38
- package/src/components/layout/sheet-layer.tsx +50 -0
- package/src/components/mcp-servers/mcp-server-list.tsx +37 -5
- package/src/components/mcp-servers/mcp-server-sheet.tsx +12 -2
- package/src/components/plugins/plugin-list.tsx +8 -4
- package/src/components/plugins/plugin-sheet.tsx +2 -1
- package/src/components/providers/provider-list.tsx +3 -2
- package/src/components/providers/provider-sheet.tsx +2 -1
- package/src/components/runs/run-list.tsx +11 -7
- package/src/components/schedules/schedule-card.tsx +5 -3
- package/src/components/shared/agent-switch-dialog.tsx +1 -1
- package/src/components/shared/attachment-chip.tsx +19 -3
- package/src/components/shared/notification-center.tsx +6 -3
- package/src/components/shared/settings/plugin-manager.tsx +3 -2
- package/src/components/shared/settings/section-embedding.tsx +2 -1
- package/src/components/shared/settings/section-orchestrator.tsx +2 -1
- package/src/components/shared/settings/section-user-preferences.tsx +107 -0
- package/src/components/shared/settings/settings-page.tsx +13 -9
- package/src/components/skills/clawhub-browser.tsx +15 -4
- package/src/components/skills/skill-list.tsx +15 -4
- package/src/components/tasks/approvals-panel.tsx +2 -1
- package/src/components/tasks/task-board.tsx +35 -37
- package/src/components/tasks/task-sheet.tsx +4 -3
- package/src/components/ui/full-screen-loader.tsx +164 -0
- package/src/components/wallets/wallet-approval-dialog.tsx +2 -1
- package/src/components/wallets/wallet-panel.tsx +6 -5
- package/src/components/wallets/wallet-section.tsx +3 -2
- package/src/components/webhooks/webhook-list.tsx +4 -5
- package/src/components/webhooks/webhook-sheet.tsx +6 -6
- package/src/hooks/use-app-bootstrap.ts +202 -0
- package/src/hooks/use-mounted-ref.ts +14 -0
- package/src/hooks/use-now.ts +31 -0
- package/src/hooks/use-openclaw-gateway.ts +2 -1
- package/src/instrumentation.ts +20 -8
- package/src/lib/agent-default-tools.test.ts +52 -0
- package/src/lib/agent-default-tools.ts +40 -0
- package/src/lib/api-client.test.ts +21 -0
- package/src/lib/api-client.ts +6 -11
- package/src/lib/canvas-content.test.ts +360 -0
- package/src/lib/chat-streaming-state.test.ts +49 -2
- package/src/lib/chat-streaming-state.ts +26 -10
- package/src/lib/fetch-timeout.test.ts +54 -0
- package/src/lib/fetch-timeout.ts +60 -3
- package/src/lib/live-tool-events.test.ts +77 -0
- package/src/lib/live-tool-events.ts +73 -0
- package/src/lib/local-observability.test.ts +2 -2
- package/src/lib/openclaw-endpoint.test.ts +1 -1
- package/src/lib/providers/anthropic.ts +12 -16
- package/src/lib/providers/index.ts +4 -2
- package/src/lib/providers/ollama.ts +9 -6
- package/src/lib/providers/openai.ts +11 -14
- package/src/lib/runtime-env.test.ts +8 -8
- package/src/lib/schedule-dedupe-advanced.test.ts +2 -2
- package/src/lib/schedule-dedupe.test.ts +1 -1
- package/src/lib/schedule-dedupe.ts +3 -2
- package/src/lib/server/agent-thread-session.test.ts +6 -6
- package/src/lib/server/agent-thread-session.ts +6 -9
- package/src/lib/server/alert-dispatch.ts +2 -1
- package/src/lib/server/api-routes.test.ts +6 -6
- package/src/lib/server/approval-connector-notify.test.ts +4 -4
- package/src/lib/server/approvals-auto-approve.test.ts +29 -29
- package/src/lib/server/approvals.test.ts +317 -0
- package/src/lib/server/approvals.ts +5 -4
- package/src/lib/server/autonomy-runtime.test.ts +11 -11
- package/src/lib/server/browser-state.ts +2 -2
- package/src/lib/server/capability-router.test.ts +1 -1
- package/src/lib/server/capability-router.ts +3 -2
- package/src/lib/server/chat-execution-advanced.test.ts +15 -2
- package/src/lib/server/chat-execution-connector-delivery.ts +67 -0
- package/src/lib/server/chat-execution-disabled.test.ts +3 -3
- package/src/lib/server/chat-execution-eval-history.test.ts +3 -3
- package/src/lib/server/chat-execution-heartbeat.test.ts +42 -1
- package/src/lib/server/chat-execution-session-sync.test.ts +119 -0
- package/src/lib/server/chat-execution-tool-events.ts +116 -0
- package/src/lib/server/chat-execution-utils.test.ts +479 -0
- package/src/lib/server/chat-execution-utils.ts +533 -0
- package/src/lib/server/chat-execution.ts +153 -748
- package/src/lib/server/chat-streaming-utils.ts +174 -0
- package/src/lib/server/chat-turn-tool-routing.ts +310 -0
- package/src/lib/server/chatroom-session-persistence.test.ts +2 -2
- package/src/lib/server/clawhub-client.ts +2 -1
- package/src/lib/server/collection-helpers.test.ts +92 -0
- package/src/lib/server/collection-helpers.ts +25 -3
- package/src/lib/server/connectors/access.ts +146 -0
- package/src/lib/server/connectors/bluebubbles.test.ts +1 -1
- package/src/lib/server/connectors/bluebubbles.ts +4 -4
- package/src/lib/server/connectors/commands.ts +367 -0
- package/src/lib/server/connectors/connector-routing.test.ts +4 -4
- package/src/lib/server/connectors/delivery.ts +142 -0
- package/src/lib/server/connectors/discord.ts +37 -40
- package/src/lib/server/connectors/email.ts +11 -10
- package/src/lib/server/connectors/googlechat.ts +4 -4
- package/src/lib/server/connectors/inbound-audio-transcription.ts +2 -1
- package/src/lib/server/connectors/ingress-delivery.ts +23 -0
- package/src/lib/server/connectors/manager-roundtrip.test.ts +300 -0
- package/src/lib/server/connectors/manager.test.ts +352 -77
- package/src/lib/server/connectors/manager.ts +134 -673
- package/src/lib/server/connectors/matrix.ts +4 -4
- package/src/lib/server/connectors/message-sentinel.ts +7 -0
- package/src/lib/server/connectors/openclaw.test.ts +1 -1
- package/src/lib/server/connectors/openclaw.ts +8 -10
- package/src/lib/server/connectors/outbox.test.ts +192 -0
- package/src/lib/server/connectors/outbox.ts +369 -0
- package/src/lib/server/connectors/pairing.test.ts +18 -1
- package/src/lib/server/connectors/pairing.ts +49 -4
- package/src/lib/server/connectors/policy.ts +9 -3
- package/src/lib/server/connectors/reconnect-state.ts +71 -0
- package/src/lib/server/connectors/response-media.ts +256 -0
- package/src/lib/server/connectors/runtime-state.ts +67 -0
- package/src/lib/server/connectors/session.test.ts +357 -0
- package/src/lib/server/connectors/session.ts +422 -0
- package/src/lib/server/connectors/signal.ts +7 -7
- package/src/lib/server/connectors/slack.ts +43 -43
- package/src/lib/server/connectors/teams.ts +4 -4
- package/src/lib/server/connectors/telegram.ts +37 -43
- package/src/lib/server/connectors/types.ts +31 -1
- package/src/lib/server/connectors/whatsapp.test.ts +108 -0
- package/src/lib/server/connectors/whatsapp.ts +106 -34
- package/src/lib/server/context-manager.test.ts +409 -0
- package/src/lib/server/cost.test.ts +1 -1
- package/src/lib/server/daemon-policy.ts +78 -0
- package/src/lib/server/daemon-state-connectors.test.ts +167 -0
- package/src/lib/server/daemon-state.test.ts +283 -55
- package/src/lib/server/daemon-state.ts +106 -109
- package/src/lib/server/data-dir.test.ts +5 -5
- package/src/lib/server/data-dir.ts +4 -0
- package/src/lib/server/delegation-jobs-advanced.test.ts +1 -1
- package/src/lib/server/delegation-jobs.test.ts +87 -0
- package/src/lib/server/delegation-jobs.ts +42 -48
- package/src/lib/server/devserver-launch.ts +1 -1
- package/src/lib/server/document-utils.ts +7 -9
- package/src/lib/server/elevenlabs.ts +2 -1
- package/src/lib/server/embeddings.test.ts +105 -0
- package/src/lib/server/ethereum.ts +3 -2
- package/src/lib/server/eval/agent-regression.ts +3 -2
- package/src/lib/server/eval/runner.ts +2 -1
- package/src/lib/server/eval/scorer.ts +2 -1
- package/src/lib/server/evm-swap.ts +2 -1
- package/src/lib/server/gateway/protocol.test.ts +1 -1
- package/src/lib/server/guardian.ts +2 -1
- package/src/lib/server/heartbeat-blocked-suppression.test.ts +151 -0
- package/src/lib/server/heartbeat-service-timer.test.ts +6 -6
- package/src/lib/server/heartbeat-service.test.ts +406 -0
- package/src/lib/server/heartbeat-service.ts +54 -7
- package/src/lib/server/heartbeat-wake.test.ts +19 -0
- package/src/lib/server/heartbeat-wake.ts +17 -16
- package/src/lib/server/integrity-monitor.test.ts +149 -0
- package/src/lib/server/json-utils.ts +22 -0
- package/src/lib/server/knowledge-db.test.ts +13 -13
- package/src/lib/server/link-understanding.ts +2 -1
- package/src/lib/server/llm-response-cache.test.ts +1 -1
- package/src/lib/server/main-agent-loop-advanced.test.ts +65 -3
- package/src/lib/server/main-agent-loop.test.ts +6 -6
- package/src/lib/server/main-agent-loop.ts +21 -7
- package/src/lib/server/mcp-client.test.ts +1 -1
- package/src/lib/server/mcp-conformance.test.ts +1 -1
- package/src/lib/server/mcp-conformance.ts +3 -2
- package/src/lib/server/memory-consolidation.ts +2 -1
- package/src/lib/server/memory-db.test.ts +485 -0
- package/src/lib/server/memory-db.ts +39 -26
- package/src/lib/server/memory-graph.test.ts +2 -2
- package/src/lib/server/memory-policy.test.ts +7 -7
- package/src/lib/server/memory-retrieval.test.ts +1 -1
- package/src/lib/server/openclaw-config-sync.ts +2 -1
- package/src/lib/server/openclaw-deploy.test.ts +1 -1
- package/src/lib/server/openclaw-deploy.ts +8 -12
- package/src/lib/server/openclaw-exec-config.ts +2 -1
- package/src/lib/server/openclaw-gateway.ts +6 -7
- package/src/lib/server/openclaw-skills-normalize.ts +2 -1
- package/src/lib/server/openclaw-sync.ts +7 -5
- package/src/lib/server/orchestrator-lg-structure.test.ts +17 -0
- package/src/lib/server/orchestrator-lg.ts +199 -327
- package/src/lib/server/path-utils.ts +31 -0
- package/src/lib/server/perf.ts +161 -0
- package/src/lib/server/plugins-approval-guidance.ts +115 -0
- package/src/lib/server/plugins.test.ts +1 -1
- package/src/lib/server/plugins.ts +22 -132
- package/src/lib/server/process-manager.ts +5 -8
- package/src/lib/server/provider-health.test.ts +137 -0
- package/src/lib/server/provider-health.ts +3 -3
- package/src/lib/server/provider-model-discovery.ts +3 -12
- package/src/lib/server/queue-followups.test.ts +9 -9
- package/src/lib/server/queue-reconcile.test.ts +2 -2
- package/src/lib/server/queue-recovery.test.ts +269 -0
- package/src/lib/server/queue.test.ts +570 -0
- package/src/lib/server/queue.ts +62 -455
- package/src/lib/server/resolve-image.ts +30 -0
- package/src/lib/server/runtime-settings.test.ts +4 -4
- package/src/lib/server/runtime-storage-write-paths.test.ts +60 -0
- package/src/lib/server/schedule-normalization.test.ts +279 -0
- package/src/lib/server/schedule-service.ts +263 -0
- package/src/lib/server/scheduler.ts +17 -74
- package/src/lib/server/session-mailbox.test.ts +191 -0
- package/src/lib/server/session-run-manager.test.ts +640 -0
- package/src/lib/server/session-run-manager.ts +59 -15
- package/src/lib/server/session-tools/autonomy-tools.test.ts +20 -20
- package/src/lib/server/session-tools/calendar.ts +2 -1
- package/src/lib/server/session-tools/canvas.ts +2 -1
- package/src/lib/server/session-tools/chatroom.ts +2 -1
- package/src/lib/server/session-tools/connector.ts +26 -28
- package/src/lib/server/session-tools/context-mgmt.ts +3 -2
- package/src/lib/server/session-tools/crawl.ts +4 -3
- package/src/lib/server/session-tools/crud.ts +105 -324
- package/src/lib/server/session-tools/delegate-fallback.test.ts +9 -9
- package/src/lib/server/session-tools/delegate.ts +6 -8
- package/src/lib/server/session-tools/discovery-approvals.test.ts +15 -15
- package/src/lib/server/session-tools/discovery.ts +4 -3
- package/src/lib/server/session-tools/document.ts +2 -1
- package/src/lib/server/session-tools/email.ts +2 -1
- package/src/lib/server/session-tools/extract.ts +2 -1
- package/src/lib/server/session-tools/file.ts +4 -3
- package/src/lib/server/session-tools/http.ts +2 -1
- package/src/lib/server/session-tools/human-loop.ts +2 -1
- package/src/lib/server/session-tools/image-gen.ts +4 -3
- package/src/lib/server/session-tools/index.ts +26 -30
- package/src/lib/server/session-tools/mailbox.ts +2 -1
- package/src/lib/server/session-tools/manage-connectors.test.ts +4 -4
- package/src/lib/server/session-tools/manage-schedules.test.ts +12 -12
- package/src/lib/server/session-tools/manage-tasks-advanced.test.ts +5 -5
- package/src/lib/server/session-tools/manage-tasks.test.ts +2 -2
- package/src/lib/server/session-tools/monitor.ts +2 -1
- package/src/lib/server/session-tools/platform.ts +2 -1
- package/src/lib/server/session-tools/plugin-creator.ts +2 -1
- package/src/lib/server/session-tools/replicate.ts +3 -2
- package/src/lib/server/session-tools/session-tools-wiring.test.ts +6 -6
- package/src/lib/server/session-tools/shell.ts +4 -9
- package/src/lib/server/session-tools/subagent.ts +322 -170
- package/src/lib/server/session-tools/table.ts +6 -5
- package/src/lib/server/session-tools/wallet-tool.test.ts +3 -3
- package/src/lib/server/session-tools/wallet.ts +7 -6
- package/src/lib/server/session-tools/web-browser-config.test.ts +1 -0
- package/src/lib/server/session-tools/web-utils.ts +317 -0
- package/src/lib/server/session-tools/web.ts +62 -328
- package/src/lib/server/skill-prompt-budget.test.ts +1 -1
- package/src/lib/server/skills-normalize.ts +2 -1
- package/src/lib/server/storage-item-access.test.ts +302 -0
- package/src/lib/server/storage.ts +366 -314
- package/src/lib/server/stream-agent-chat.test.ts +82 -3
- package/src/lib/server/stream-agent-chat.ts +146 -510
- package/src/lib/server/stream-continuation.ts +412 -0
- package/src/lib/server/subagent-lineage.test.ts +647 -0
- package/src/lib/server/subagent-lineage.ts +435 -0
- package/src/lib/server/subagent-runtime.test.ts +484 -0
- package/src/lib/server/subagent-runtime.ts +419 -0
- package/src/lib/server/subagent-swarm.test.ts +391 -0
- package/src/lib/server/subagent-swarm.ts +564 -0
- package/src/lib/server/system-events.ts +3 -3
- package/src/lib/server/task-followups.test.ts +491 -0
- package/src/lib/server/task-followups.ts +391 -0
- package/src/lib/server/task-lifecycle.test.ts +205 -0
- package/src/lib/server/task-lifecycle.ts +200 -0
- package/src/lib/server/task-quality-gate.test.ts +1 -1
- package/src/lib/server/task-resume.ts +208 -0
- package/src/lib/server/task-service.test.ts +108 -0
- package/src/lib/server/task-service.ts +264 -0
- package/src/lib/server/task-validation.test.ts +1 -1
- package/src/lib/server/test-utils/run-with-temp-data-dir.ts +42 -0
- package/src/lib/server/tool-capability-policy.test.ts +2 -2
- package/src/lib/server/tool-capability-policy.ts +3 -2
- package/src/lib/server/tool-planning.ts +2 -1
- package/src/lib/server/tool-retry.ts +2 -3
- package/src/lib/server/wake-dispatcher.test.ts +303 -0
- package/src/lib/server/wake-dispatcher.ts +318 -0
- package/src/lib/server/wake-mode.test.ts +161 -0
- package/src/lib/server/wake-mode.ts +174 -0
- package/src/lib/server/wallet-service.ts +8 -9
- package/src/lib/server/watch-jobs.ts +2 -1
- package/src/lib/server/workspace-context.ts +2 -2
- package/src/lib/shared-utils.test.ts +142 -0
- package/src/lib/shared-utils.ts +62 -0
- package/src/lib/tool-event-summary.ts +2 -1
- package/src/lib/view-routes.test.ts +100 -0
- package/src/lib/wallet.test.ts +322 -6
- package/src/proxy.test.ts +4 -4
- package/src/proxy.ts +2 -3
- package/src/stores/set-if-changed.ts +40 -0
- package/src/stores/slices/agent-slice.ts +111 -0
- package/src/stores/slices/auth-slice.ts +25 -0
- package/src/stores/slices/data-slice.ts +301 -0
- package/src/stores/slices/index.ts +7 -0
- package/src/stores/slices/session-slice.ts +112 -0
- package/src/stores/slices/task-slice.ts +63 -0
- package/src/stores/slices/ui-slice.ts +192 -0
- package/src/stores/use-app-store.ts +17 -822
- package/src/stores/use-approval-store.ts +2 -1
- package/src/stores/use-chat-store.ts +8 -1
- package/src/types/index.ts +10 -0
package/src/lib/wallet.test.ts
CHANGED
|
@@ -1,17 +1,333 @@
|
|
|
1
|
-
import assert from 'node:assert/strict'
|
|
2
1
|
import { describe, it } from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import {
|
|
4
|
+
normalizeWalletChainInput,
|
|
5
|
+
getWalletChainOrDefault,
|
|
6
|
+
normalizeAtomicString,
|
|
7
|
+
parseDisplayAmountToAtomic,
|
|
8
|
+
formatAtomicAmount,
|
|
9
|
+
formatWalletAmount,
|
|
10
|
+
getWalletChainMeta,
|
|
11
|
+
getWalletDefaultLimitAtomic,
|
|
12
|
+
getWalletExplorerUrl,
|
|
13
|
+
getWalletAssetSymbol,
|
|
14
|
+
getWalletAtomicAmount,
|
|
15
|
+
getWalletFeeAtomicAmount,
|
|
16
|
+
getWalletLimitAtomic,
|
|
17
|
+
getWalletBalanceAtomic,
|
|
18
|
+
} from './wallet'
|
|
19
|
+
|
|
20
|
+
describe('normalizeWalletChainInput', () => {
|
|
21
|
+
it('normalizes solana variants', () => {
|
|
22
|
+
assert.equal(normalizeWalletChainInput('solana'), 'solana')
|
|
23
|
+
assert.equal(normalizeWalletChainInput('Solana'), 'solana')
|
|
24
|
+
assert.equal(normalizeWalletChainInput('SOL'), 'solana')
|
|
25
|
+
assert.equal(normalizeWalletChainInput('sol'), 'solana')
|
|
26
|
+
assert.equal(normalizeWalletChainInput(' sol '), 'solana')
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('normalizes ethereum variants', () => {
|
|
30
|
+
assert.equal(normalizeWalletChainInput('ethereum'), 'ethereum')
|
|
31
|
+
assert.equal(normalizeWalletChainInput('Ethereum'), 'ethereum')
|
|
32
|
+
assert.equal(normalizeWalletChainInput('ETH'), 'ethereum')
|
|
33
|
+
assert.equal(normalizeWalletChainInput('eth'), 'ethereum')
|
|
34
|
+
assert.equal(normalizeWalletChainInput('evm'), 'ethereum')
|
|
35
|
+
assert.equal(normalizeWalletChainInput('EVM'), 'ethereum')
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('returns null for unsupported or empty input', () => {
|
|
39
|
+
assert.equal(normalizeWalletChainInput('bitcoin'), null)
|
|
40
|
+
assert.equal(normalizeWalletChainInput(''), null)
|
|
41
|
+
assert.equal(normalizeWalletChainInput(null), null)
|
|
42
|
+
assert.equal(normalizeWalletChainInput(undefined), null)
|
|
43
|
+
assert.equal(normalizeWalletChainInput(0), null)
|
|
44
|
+
assert.equal(normalizeWalletChainInput(false), null)
|
|
45
|
+
})
|
|
46
|
+
})
|
|
3
47
|
|
|
4
|
-
|
|
48
|
+
describe('getWalletChainOrDefault', () => {
|
|
49
|
+
it('returns recognized chain', () => {
|
|
50
|
+
assert.equal(getWalletChainOrDefault('sol'), 'solana')
|
|
51
|
+
assert.equal(getWalletChainOrDefault('eth'), 'ethereum')
|
|
52
|
+
})
|
|
5
53
|
|
|
6
|
-
|
|
7
|
-
|
|
54
|
+
it('returns fallback for empty/null/undefined', () => {
|
|
55
|
+
assert.equal(getWalletChainOrDefault(''), 'solana')
|
|
56
|
+
assert.equal(getWalletChainOrDefault(null), 'solana')
|
|
57
|
+
assert.equal(getWalletChainOrDefault(undefined), 'solana')
|
|
58
|
+
assert.equal(getWalletChainOrDefault(undefined, 'ethereum'), 'ethereum')
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it('throws for unrecognized non-empty value', () => {
|
|
62
|
+
assert.throws(() => getWalletChainOrDefault('bitcoin'), /Unsupported wallet chain/)
|
|
8
63
|
assert.throws(() => getWalletChainOrDefault('base'), /Unsupported wallet chain or provider: base/)
|
|
9
64
|
assert.throws(() => getWalletChainOrDefault('ethereun'), /Unsupported wallet chain or provider: ethereun/)
|
|
10
|
-
|
|
65
|
+
})
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
describe('normalizeAtomicString', () => {
|
|
69
|
+
it('handles string integers', () => {
|
|
70
|
+
assert.equal(normalizeAtomicString('12345'), '12345')
|
|
71
|
+
assert.equal(normalizeAtomicString('0'), '0')
|
|
72
|
+
assert.equal(normalizeAtomicString('007'), '7')
|
|
73
|
+
assert.equal(normalizeAtomicString('0000'), '0')
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('handles bigint', () => {
|
|
77
|
+
assert.equal(normalizeAtomicString(BigInt(999)), '999')
|
|
78
|
+
assert.equal(normalizeAtomicString(BigInt(0)), '0')
|
|
79
|
+
assert.equal(normalizeAtomicString(BigInt(-5)), '0')
|
|
80
|
+
assert.equal(normalizeAtomicString(BigInt(-1), 'fallback'), 'fallback')
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
it('handles number', () => {
|
|
84
|
+
assert.equal(normalizeAtomicString(100), '100')
|
|
85
|
+
assert.equal(normalizeAtomicString(3.9), '3')
|
|
86
|
+
assert.equal(normalizeAtomicString(0), '0')
|
|
11
87
|
})
|
|
12
88
|
|
|
13
|
-
it('
|
|
89
|
+
it('returns fallback for invalid input', () => {
|
|
90
|
+
assert.equal(normalizeAtomicString('abc'), '0')
|
|
91
|
+
assert.equal(normalizeAtomicString('12.5'), '0')
|
|
92
|
+
assert.equal(normalizeAtomicString(-1), '0')
|
|
93
|
+
assert.equal(normalizeAtomicString(NaN), '0')
|
|
94
|
+
assert.equal(normalizeAtomicString(Infinity), '0')
|
|
95
|
+
assert.equal(normalizeAtomicString(null), '0')
|
|
96
|
+
assert.equal(normalizeAtomicString(undefined), '0')
|
|
97
|
+
assert.equal(normalizeAtomicString(' '), '0')
|
|
98
|
+
assert.equal(normalizeAtomicString(null, 'custom'), 'custom')
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('strips leading zeros from strings', () => {
|
|
102
|
+
assert.equal(normalizeAtomicString('00100'), '100')
|
|
103
|
+
assert.equal(normalizeAtomicString('00'), '0')
|
|
104
|
+
})
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
describe('parseDisplayAmountToAtomic', () => {
|
|
108
|
+
it('converts whole numbers (SOL, 9 decimals)', () => {
|
|
109
|
+
assert.equal(parseDisplayAmountToAtomic('1', 9), '1000000000')
|
|
110
|
+
assert.equal(parseDisplayAmountToAtomic('0', 9), '0')
|
|
111
|
+
assert.equal(parseDisplayAmountToAtomic('100', 9), '100000000000')
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
it('converts fractional numbers (SOL)', () => {
|
|
115
|
+
assert.equal(parseDisplayAmountToAtomic('0.1', 9), '100000000')
|
|
116
|
+
assert.equal(parseDisplayAmountToAtomic('1.5', 9), '1500000000')
|
|
117
|
+
assert.equal(parseDisplayAmountToAtomic('0.000000001', 9), '1')
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
it('converts with ETH decimals (18)', () => {
|
|
121
|
+
assert.equal(parseDisplayAmountToAtomic('1', 18), '1000000000000000000')
|
|
122
|
+
assert.equal(parseDisplayAmountToAtomic('0.01', 18), '10000000000000000')
|
|
14
123
|
assert.equal(parseDisplayAmountToAtomic('0.000000000000000001', 18), '1')
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('accepts number input', () => {
|
|
127
|
+
assert.equal(parseDisplayAmountToAtomic(1.5, 9), '1500000000')
|
|
128
|
+
assert.equal(parseDisplayAmountToAtomic(0, 9), '0')
|
|
15
129
|
assert.equal(parseDisplayAmountToAtomic(0.000000000000000001, 18), '1')
|
|
16
130
|
})
|
|
131
|
+
|
|
132
|
+
it('throws for empty amount', () => {
|
|
133
|
+
assert.throws(() => parseDisplayAmountToAtomic('', 9), /Amount is required/)
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
it('throws for non-numeric strings', () => {
|
|
137
|
+
assert.throws(() => parseDisplayAmountToAtomic('abc', 9), /Amount must be a positive decimal/)
|
|
138
|
+
assert.throws(() => parseDisplayAmountToAtomic('-1', 9), /Amount must be a positive decimal/)
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
it('throws for too many decimal places', () => {
|
|
142
|
+
assert.throws(() => parseDisplayAmountToAtomic('1.0000000001', 9), /up to 9 decimal places/)
|
|
143
|
+
})
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
describe('formatAtomicAmount', () => {
|
|
147
|
+
it('formats SOL (9 decimals)', () => {
|
|
148
|
+
assert.equal(formatAtomicAmount('1000000000', 9), '1')
|
|
149
|
+
assert.equal(formatAtomicAmount('1500000000', 9), '1.5')
|
|
150
|
+
assert.equal(formatAtomicAmount('100000000', 9), '0.1')
|
|
151
|
+
assert.equal(formatAtomicAmount('1', 9), '0.000000001')
|
|
152
|
+
assert.equal(formatAtomicAmount('0', 9), '0')
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
it('formats ETH (18 decimals)', () => {
|
|
156
|
+
assert.equal(formatAtomicAmount('1000000000000000000', 18), '1')
|
|
157
|
+
assert.equal(formatAtomicAmount('10000000000000000', 18), '0.01')
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
it('respects minFractionDigits', () => {
|
|
161
|
+
assert.equal(formatAtomicAmount('1000000000', 9, { minFractionDigits: 2 }), '1.00')
|
|
162
|
+
assert.equal(formatAtomicAmount('1500000000', 9, { minFractionDigits: 4 }), '1.5000')
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
it('respects maxFractionDigits', () => {
|
|
166
|
+
assert.equal(formatAtomicAmount('1', 9, { maxFractionDigits: 2 }), '0')
|
|
167
|
+
assert.equal(formatAtomicAmount('1000000000', 9, { maxFractionDigits: 0 }), '1')
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
it('handles bigint input', () => {
|
|
171
|
+
assert.equal(formatAtomicAmount(BigInt('1000000000'), 9), '1')
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
it('handles number input', () => {
|
|
175
|
+
assert.equal(formatAtomicAmount(1000000000, 9), '1')
|
|
176
|
+
})
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
describe('formatWalletAmount', () => {
|
|
180
|
+
it('uses chain-specific decimals', () => {
|
|
181
|
+
assert.equal(formatWalletAmount('solana', '1000000000'), '1')
|
|
182
|
+
assert.equal(formatWalletAmount('ethereum', '1000000000000000000'), '1')
|
|
183
|
+
})
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
describe('getWalletChainMeta', () => {
|
|
187
|
+
it('returns solana metadata', () => {
|
|
188
|
+
const meta = getWalletChainMeta('solana')
|
|
189
|
+
assert.equal(meta.symbol, 'SOL')
|
|
190
|
+
assert.equal(meta.decimals, 9)
|
|
191
|
+
assert.equal(meta.chain, 'solana')
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
it('returns ethereum metadata', () => {
|
|
195
|
+
const meta = getWalletChainMeta('ethereum')
|
|
196
|
+
assert.equal(meta.symbol, 'ETH')
|
|
197
|
+
assert.equal(meta.decimals, 18)
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
it('falls back to solana for unknown chain', () => {
|
|
201
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
202
|
+
const meta = getWalletChainMeta('unknown' as any)
|
|
203
|
+
assert.equal(meta.chain, 'solana')
|
|
204
|
+
})
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
describe('getWalletDefaultLimitAtomic', () => {
|
|
208
|
+
it('returns perTx and daily defaults', () => {
|
|
209
|
+
assert.equal(getWalletDefaultLimitAtomic('solana', 'perTx'), '100000000')
|
|
210
|
+
assert.equal(getWalletDefaultLimitAtomic('solana', 'daily'), '1000000000')
|
|
211
|
+
assert.equal(getWalletDefaultLimitAtomic('ethereum', 'perTx'), '10000000000000000')
|
|
212
|
+
assert.equal(getWalletDefaultLimitAtomic('ethereum', 'daily'), '50000000000000000')
|
|
213
|
+
})
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
describe('getWalletExplorerUrl', () => {
|
|
217
|
+
it('builds address explorer URLs', () => {
|
|
218
|
+
assert.equal(
|
|
219
|
+
getWalletExplorerUrl('solana', 'address', 'abc123'),
|
|
220
|
+
'https://solscan.io/account/abc123',
|
|
221
|
+
)
|
|
222
|
+
assert.equal(
|
|
223
|
+
getWalletExplorerUrl('ethereum', 'address', '0xdef'),
|
|
224
|
+
'https://etherscan.io/address/0xdef',
|
|
225
|
+
)
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
it('builds transaction explorer URLs', () => {
|
|
229
|
+
assert.equal(
|
|
230
|
+
getWalletExplorerUrl('solana', 'transaction', 'tx1'),
|
|
231
|
+
'https://solscan.io/tx/tx1',
|
|
232
|
+
)
|
|
233
|
+
assert.equal(
|
|
234
|
+
getWalletExplorerUrl('ethereum', 'transaction', '0xtx'),
|
|
235
|
+
'https://etherscan.io/tx/0xtx',
|
|
236
|
+
)
|
|
237
|
+
})
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
describe('getWalletAssetSymbol', () => {
|
|
241
|
+
it('returns chain symbols', () => {
|
|
242
|
+
assert.equal(getWalletAssetSymbol('solana'), 'SOL')
|
|
243
|
+
assert.equal(getWalletAssetSymbol('ethereum'), 'ETH')
|
|
244
|
+
})
|
|
245
|
+
})
|
|
246
|
+
|
|
247
|
+
describe('getWalletAtomicAmount', () => {
|
|
248
|
+
it('prefers amountAtomic', () => {
|
|
249
|
+
assert.equal(getWalletAtomicAmount({ amountAtomic: '500' }), '500')
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
it('falls back to amountLamports', () => {
|
|
253
|
+
assert.equal(getWalletAtomicAmount({ amountLamports: 300 }), '300')
|
|
254
|
+
})
|
|
255
|
+
|
|
256
|
+
it('returns 0 for null/undefined', () => {
|
|
257
|
+
assert.equal(getWalletAtomicAmount(null), '0')
|
|
258
|
+
assert.equal(getWalletAtomicAmount(undefined), '0')
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
it('returns 0 for empty object', () => {
|
|
262
|
+
assert.equal(getWalletAtomicAmount({}), '0')
|
|
263
|
+
})
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
describe('getWalletFeeAtomicAmount', () => {
|
|
267
|
+
it('prefers feeAtomic', () => {
|
|
268
|
+
assert.equal(getWalletFeeAtomicAmount({ feeAtomic: '5000' }), '5000')
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
it('falls back to feeLamports', () => {
|
|
272
|
+
assert.equal(getWalletFeeAtomicAmount({ feeLamports: 5000 }), '5000')
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
it('returns 0 for null/undefined', () => {
|
|
276
|
+
assert.equal(getWalletFeeAtomicAmount(null), '0')
|
|
277
|
+
assert.equal(getWalletFeeAtomicAmount(undefined), '0')
|
|
278
|
+
})
|
|
279
|
+
})
|
|
280
|
+
|
|
281
|
+
describe('getWalletLimitAtomic', () => {
|
|
282
|
+
it('uses spendingLimitAtomic for perTx', () => {
|
|
283
|
+
assert.equal(
|
|
284
|
+
getWalletLimitAtomic({ chain: 'solana', spendingLimitAtomic: '50000' }, 'perTx'),
|
|
285
|
+
'50000',
|
|
286
|
+
)
|
|
287
|
+
})
|
|
288
|
+
|
|
289
|
+
it('falls back to spendingLimitLamports then default for perTx', () => {
|
|
290
|
+
assert.equal(
|
|
291
|
+
getWalletLimitAtomic({ chain: 'solana', spendingLimitLamports: 25000 }, 'perTx'),
|
|
292
|
+
'25000',
|
|
293
|
+
)
|
|
294
|
+
assert.equal(
|
|
295
|
+
getWalletLimitAtomic({ chain: 'solana' }, 'perTx'),
|
|
296
|
+
'100000000',
|
|
297
|
+
)
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
it('uses dailyLimitAtomic for daily', () => {
|
|
301
|
+
assert.equal(
|
|
302
|
+
getWalletLimitAtomic({ chain: 'ethereum', dailyLimitAtomic: '99999' }, 'daily'),
|
|
303
|
+
'99999',
|
|
304
|
+
)
|
|
305
|
+
})
|
|
306
|
+
|
|
307
|
+
it('falls back for daily', () => {
|
|
308
|
+
assert.equal(
|
|
309
|
+
getWalletLimitAtomic({ chain: 'ethereum' }, 'daily'),
|
|
310
|
+
'50000000000000000',
|
|
311
|
+
)
|
|
312
|
+
})
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
describe('getWalletBalanceAtomic', () => {
|
|
316
|
+
it('prefers balanceAtomic', () => {
|
|
317
|
+
assert.equal(getWalletBalanceAtomic({ balanceAtomic: '12345' }), '12345')
|
|
318
|
+
})
|
|
319
|
+
|
|
320
|
+
it('falls back to balanceLamports', () => {
|
|
321
|
+
assert.equal(getWalletBalanceAtomic({ balanceLamports: 9999 }), '9999')
|
|
322
|
+
})
|
|
323
|
+
|
|
324
|
+
it('returns 0 for null/undefined/empty', () => {
|
|
325
|
+
assert.equal(getWalletBalanceAtomic(null), '0')
|
|
326
|
+
assert.equal(getWalletBalanceAtomic(undefined), '0')
|
|
327
|
+
assert.equal(getWalletBalanceAtomic({}), '0')
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
it('handles null balanceLamports', () => {
|
|
331
|
+
assert.equal(getWalletBalanceAtomic({ balanceLamports: null }), '0')
|
|
332
|
+
})
|
|
17
333
|
})
|
package/src/proxy.test.ts
CHANGED
|
@@ -45,8 +45,8 @@ describe('proxy', () => {
|
|
|
45
45
|
|
|
46
46
|
it('does not lock out invalid requests in development', () => {
|
|
47
47
|
process.env.ACCESS_KEY = 'top-secret'
|
|
48
|
-
const originalNodeEnv = process.env.NODE_ENV
|
|
49
|
-
process.env.NODE_ENV = 'development'
|
|
48
|
+
const originalNodeEnv = process.env.NODE_ENV;
|
|
49
|
+
(process.env as any).NODE_ENV = 'development'
|
|
50
50
|
|
|
51
51
|
try {
|
|
52
52
|
for (let i = 0; i < 6; i++) {
|
|
@@ -64,8 +64,8 @@ describe('proxy', () => {
|
|
|
64
64
|
}))
|
|
65
65
|
assert.equal(finalResponse.status, 401)
|
|
66
66
|
} finally {
|
|
67
|
-
if (originalNodeEnv === undefined) delete process.env.NODE_ENV
|
|
68
|
-
else process.env.NODE_ENV = originalNodeEnv
|
|
67
|
+
if (originalNodeEnv === undefined) delete (process.env as any).NODE_ENV
|
|
68
|
+
else (process.env as any).NODE_ENV = originalNodeEnv
|
|
69
69
|
}
|
|
70
70
|
})
|
|
71
71
|
})
|
package/src/proxy.ts
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
resolvePluginInstallCorsOrigin,
|
|
8
8
|
} from '@/lib/plugin-install-cors'
|
|
9
9
|
import { isProductionRuntime } from '@/lib/runtime-env'
|
|
10
|
+
import { hmrSingleton } from '@/lib/shared-utils'
|
|
10
11
|
|
|
11
12
|
/* ------------------------------------------------------------------ */
|
|
12
13
|
/* Rate-limit state — HMR-safe via globalThis */
|
|
@@ -17,9 +18,7 @@ interface RateLimitEntry {
|
|
|
17
18
|
lockedUntil: number
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
const rateLimitMap = (
|
|
21
|
-
(globalThis as Record<string, unknown>).__swarmclaw_rate_limit__ ??= new Map()
|
|
22
|
-
) as Map<string, RateLimitEntry>
|
|
21
|
+
const rateLimitMap = hmrSingleton('__swarmclaw_rate_limit__', () => new Map<string, RateLimitEntry>())
|
|
23
22
|
|
|
24
23
|
const MAX_ATTEMPTS = 5
|
|
25
24
|
const LOCKOUT_MS = 15 * 60 * 1000 // 15 minutes
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memoisation layer for Zustand store loaders.
|
|
3
|
+
*
|
|
4
|
+
* Every async loader (loadSessions, loadTasks, loadProviders, …) fetches data
|
|
5
|
+
* from the API and calls `set()`. Because the API always returns fresh object
|
|
6
|
+
* references, `set()` unconditionally replaces the store value, which triggers
|
|
7
|
+
* re-renders in *every* component that subscribes to that slice — even when the
|
|
8
|
+
* data hasn't changed.
|
|
9
|
+
*
|
|
10
|
+
* `setIfChanged` keeps a lightweight JSON fingerprint of the last value written
|
|
11
|
+
* for each store key. If the new value produces the same fingerprint, the
|
|
12
|
+
* `set()` call is skipped entirely, preventing the render cascade.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const fingerprints = new Map<string, string>()
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Call Zustand `set()` only when the value for `key` has actually changed.
|
|
19
|
+
* Returns `true` if `set()` was called, `false` if skipped.
|
|
20
|
+
*/
|
|
21
|
+
export function setIfChanged<S>(
|
|
22
|
+
set: (partial: Partial<S>) => void,
|
|
23
|
+
key: keyof S & string,
|
|
24
|
+
value: S[keyof S],
|
|
25
|
+
): boolean {
|
|
26
|
+
const json = JSON.stringify(value)
|
|
27
|
+
if (fingerprints.get(key) === json) return false
|
|
28
|
+
fingerprints.set(key, json)
|
|
29
|
+
set({ [key]: value } as Partial<S>)
|
|
30
|
+
return true
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Invalidate the fingerprint for a key so the next `setIfChanged` call
|
|
35
|
+
* for that key will always write. Useful after local mutations (optimistic
|
|
36
|
+
* updates, removes) that change the store without going through the loader.
|
|
37
|
+
*/
|
|
38
|
+
export function invalidateFingerprint(key: string): void {
|
|
39
|
+
fingerprints.delete(key)
|
|
40
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
const inflightAgentThreadLoads = new Map<string, Promise<void>>()
|
|
2
|
+
import { StateCreator } from 'zustand'
|
|
3
|
+
import type { AppState } from '../use-app-store'
|
|
4
|
+
import type { Session, Agent, ExternalAgentRuntime } from '../../types'
|
|
5
|
+
import { fetchAgents } from '../../lib/agents'
|
|
6
|
+
import { findLatestObservablePlatformSession, isLocalhostBrowser } from '../../lib/local-observability'
|
|
7
|
+
import { api } from '../../lib/api-client'
|
|
8
|
+
import { safeStorageRemove, safeStorageSet } from '../../lib/safe-storage'
|
|
9
|
+
import { setIfChanged, invalidateFingerprint } from '../set-if-changed'
|
|
10
|
+
|
|
11
|
+
export interface AgentSlice {
|
|
12
|
+
currentAgentId: string | null
|
|
13
|
+
setCurrentAgent: (id: string | null) => Promise<void>
|
|
14
|
+
agents: Record<string, Agent>
|
|
15
|
+
loadAgents: () => Promise<void>
|
|
16
|
+
togglePinAgent: (id: string) => void
|
|
17
|
+
trashedAgents: Record<string, Agent>
|
|
18
|
+
loadTrashedAgents: () => Promise<void>
|
|
19
|
+
externalAgents: ExternalAgentRuntime[]
|
|
20
|
+
loadExternalAgents: () => Promise<void>
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const createAgentSlice: StateCreator<AppState, [], [], AgentSlice> = (set, get) => ({
|
|
24
|
+
currentAgentId: null,
|
|
25
|
+
setCurrentAgent: async (id) => {
|
|
26
|
+
if (!id) {
|
|
27
|
+
set({ currentAgentId: null })
|
|
28
|
+
safeStorageRemove('sc_agent')
|
|
29
|
+
return
|
|
30
|
+
}
|
|
31
|
+
const currentState = get()
|
|
32
|
+
const currentSession = currentState.currentSessionId ? currentState.sessions[currentState.currentSessionId] : null
|
|
33
|
+
if (currentState.currentAgentId === id && currentSession?.agentId === id) {
|
|
34
|
+
return
|
|
35
|
+
}
|
|
36
|
+
set({ currentAgentId: id })
|
|
37
|
+
safeStorageSet('sc_agent', id)
|
|
38
|
+
if (isLocalhostBrowser()) {
|
|
39
|
+
const livePlatformSession = findLatestObservablePlatformSession(get().sessions, id)
|
|
40
|
+
if (livePlatformSession?.id) {
|
|
41
|
+
set({ currentSessionId: livePlatformSession.id })
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const existingLoad = inflightAgentThreadLoads.get(id)
|
|
47
|
+
if (existingLoad) {
|
|
48
|
+
await existingLoad
|
|
49
|
+
return
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const loadPromise = (async () => {
|
|
53
|
+
try {
|
|
54
|
+
const user = get().currentUser || 'default'
|
|
55
|
+
const session = await api<Session>('POST', `/agents/${id}/thread`, { user })
|
|
56
|
+
if (session?.id) {
|
|
57
|
+
const sessions = { ...get().sessions, [session.id]: session }
|
|
58
|
+
invalidateFingerprint('sessions')
|
|
59
|
+
set({ sessions, currentSessionId: session.id })
|
|
60
|
+
}
|
|
61
|
+
} catch {
|
|
62
|
+
// ignore — thread creation failed
|
|
63
|
+
}
|
|
64
|
+
})()
|
|
65
|
+
|
|
66
|
+
inflightAgentThreadLoads.set(id, loadPromise)
|
|
67
|
+
try {
|
|
68
|
+
await loadPromise
|
|
69
|
+
} finally {
|
|
70
|
+
if (inflightAgentThreadLoads.get(id) === loadPromise) {
|
|
71
|
+
inflightAgentThreadLoads.delete(id)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
agents: {},
|
|
76
|
+
loadAgents: async () => {
|
|
77
|
+
try {
|
|
78
|
+
const agents = await fetchAgents()
|
|
79
|
+
setIfChanged<AppState>(set, 'agents', agents)
|
|
80
|
+
} catch (err) {
|
|
81
|
+
console.warn('Store error:', err)
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
togglePinAgent: (id) => {
|
|
85
|
+
const agents = { ...get().agents }
|
|
86
|
+
if (agents[id]) {
|
|
87
|
+
agents[id] = { ...agents[id], pinned: !agents[id].pinned }
|
|
88
|
+
invalidateFingerprint('agents')
|
|
89
|
+
set({ agents })
|
|
90
|
+
void api('PUT', `/agents/${id}`, { pinned: agents[id].pinned })
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
trashedAgents: {},
|
|
94
|
+
loadTrashedAgents: async () => {
|
|
95
|
+
try {
|
|
96
|
+
const trashedAgents = await api<Record<string, Agent>>('GET', '/agents/trash')
|
|
97
|
+
setIfChanged<AppState>(set, 'trashedAgents', trashedAgents)
|
|
98
|
+
} catch (err) {
|
|
99
|
+
console.warn('Store error:', err)
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
externalAgents: [],
|
|
103
|
+
loadExternalAgents: async () => {
|
|
104
|
+
try {
|
|
105
|
+
const externalAgents = await api<ExternalAgentRuntime[]>('GET', '/external-agents')
|
|
106
|
+
setIfChanged<AppState>(set, 'externalAgents', externalAgents)
|
|
107
|
+
} catch (err) {
|
|
108
|
+
console.warn('Store error:', err)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
})
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { StateCreator } from 'zustand'
|
|
2
|
+
import type { AppState } from '../use-app-store'
|
|
3
|
+
import { safeStorageGet, safeStorageRemove, safeStorageSet } from '../../lib/safe-storage'
|
|
4
|
+
|
|
5
|
+
export interface AuthSlice {
|
|
6
|
+
currentUser: string | null
|
|
7
|
+
_hydrated: boolean
|
|
8
|
+
hydrate: () => void
|
|
9
|
+
setUser: (user: string | null) => void
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const createAuthSlice: StateCreator<AppState, [], [], AuthSlice> = (set, get) => ({
|
|
13
|
+
currentUser: null,
|
|
14
|
+
_hydrated: false,
|
|
15
|
+
hydrate: () => {
|
|
16
|
+
const user = safeStorageGet('sc_user')
|
|
17
|
+
const savedAgentId = safeStorageGet('sc_agent')
|
|
18
|
+
set({ currentUser: user, currentAgentId: savedAgentId, _hydrated: true })
|
|
19
|
+
},
|
|
20
|
+
setUser: (user) => {
|
|
21
|
+
if (user) safeStorageSet('sc_user', user)
|
|
22
|
+
else safeStorageRemove('sc_user')
|
|
23
|
+
set({ currentUser: user })
|
|
24
|
+
}
|
|
25
|
+
})
|