@lsamien/yaoyao 0.4.75
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 +202 -0
- package/NOTICE +6 -0
- package/README.md +103 -0
- package/THIRD_PARTY_NOTICES.md +16 -0
- package/bin/desktop-service-sync.mjs +43 -0
- package/bin/hermes-yaoyao-updater.mjs +315 -0
- package/bin/hermes-yaoyao.mjs +246 -0
- package/bin/lib/data-home.d.mts +4 -0
- package/bin/lib/data-home.mjs +24 -0
- package/bin/lib/data-migration.d.mts +2 -0
- package/bin/lib/data-migration.mjs +107 -0
- package/bin/lib/desktop-activation.d.mts +4 -0
- package/bin/lib/desktop-activation.mjs +18 -0
- package/bin/lib/managed-data-migration.mjs +45 -0
- package/bin/lib/release-source.d.mts +2 -0
- package/bin/lib/release-source.mjs +23 -0
- package/bin/lib/runtime-release.mjs +78 -0
- package/bin/lib/service-data.mjs +57 -0
- package/bin/lib/service-update.mjs +372 -0
- package/bin/migrate-data-home.mjs +20 -0
- package/build-info.json +657 -0
- package/desktop/main.mjs +679 -0
- package/dist/assets/AboutDialog-BvuJp8Pe.js +2 -0
- package/dist/assets/AboutDialog-BvuJp8Pe.js.map +1 -0
- package/dist/assets/AboutDialog-DC5YWX39.css +1 -0
- package/dist/assets/BotAutomationsView-Ddgs5Hes.js +2 -0
- package/dist/assets/BotAutomationsView-Ddgs5Hes.js.map +1 -0
- package/dist/assets/BotAutomationsView-DeJ-e3y5.css +1 -0
- package/dist/assets/BotPluginsDialog-Db5dTqvg.css +1 -0
- package/dist/assets/BotPluginsDialog-xPqUSyHS.js +2 -0
- package/dist/assets/BotPluginsDialog-xPqUSyHS.js.map +1 -0
- package/dist/assets/BotSettingsDialog-BkjoAZtf.css +1 -0
- package/dist/assets/BotSettingsDialog-wLz_KGzX.js +5 -0
- package/dist/assets/BotSettingsDialog-wLz_KGzX.js.map +1 -0
- package/dist/assets/ChatView-DRPu2nKr.js +2 -0
- package/dist/assets/ChatView-DRPu2nKr.js.map +1 -0
- package/dist/assets/ChatView-Dx8YlcPr.css +1 -0
- package/dist/assets/ChevronForwardOutline-D--GIQK6.js +2 -0
- package/dist/assets/ChevronForwardOutline-D--GIQK6.js.map +1 -0
- package/dist/assets/ComputerPanel-Byr2_yKj.js +2 -0
- package/dist/assets/ComputerPanel-Byr2_yKj.js.map +1 -0
- package/dist/assets/ComputerPanel-CQOv4v8E.css +1 -0
- package/dist/assets/ComputerViewerView-BBAs4bzi.css +1 -0
- package/dist/assets/ComputerViewerView-DJ6hB-5e.js +2 -0
- package/dist/assets/ComputerViewerView-DJ6hB-5e.js.map +1 -0
- package/dist/assets/ConversationsView-C_VZl5g2.css +1 -0
- package/dist/assets/ConversationsView-cu1Yrq68.js +6 -0
- package/dist/assets/ConversationsView-cu1Yrq68.js.map +1 -0
- package/dist/assets/FilesView-DAIzo9Ps.css +1 -0
- package/dist/assets/FilesView-h9oJFmZt.js +2 -0
- package/dist/assets/FilesView-h9oJFmZt.js.map +1 -0
- package/dist/assets/FloatingResourceSearch-BF2XOLd-.css +1 -0
- package/dist/assets/FloatingResourceSearch-TofG1_DV.js +9 -0
- package/dist/assets/FloatingResourceSearch-TofG1_DV.js.map +1 -0
- package/dist/assets/HistoryView-Bh0SU969.js +2 -0
- package/dist/assets/HistoryView-Bh0SU969.js.map +1 -0
- package/dist/assets/HistoryView-DvuCAjlo.css +1 -0
- package/dist/assets/KanbanView-B-2vG4w7.css +1 -0
- package/dist/assets/KanbanView-CcqMOzB7.js +2 -0
- package/dist/assets/KanbanView-CcqMOzB7.js.map +1 -0
- package/dist/assets/LocalVmSettingsPanel-C8PIgMr3.js +2 -0
- package/dist/assets/LocalVmSettingsPanel-C8PIgMr3.js.map +1 -0
- package/dist/assets/LocalVmSettingsPanel-Dedfhqlq.css +1 -0
- package/dist/assets/RunnerSettingsPanel-CfxxQKpV.css +1 -0
- package/dist/assets/RunnerSettingsPanel-CvNnWcdH.js +4 -0
- package/dist/assets/RunnerSettingsPanel-CvNnWcdH.js.map +1 -0
- package/dist/assets/StandaloneDialog-BbNWY6IJ.js +2 -0
- package/dist/assets/StandaloneDialog-BbNWY6IJ.js.map +1 -0
- package/dist/assets/StandaloneDialog-Bg5U9bMJ.css +1 -0
- package/dist/assets/UpdateCheckDialog-C357uDSE.js +2 -0
- package/dist/assets/UpdateCheckDialog-C357uDSE.js.map +1 -0
- package/dist/assets/UpdateCheckDialog-CRlyWnE7.css +1 -0
- package/dist/assets/WorkspaceKnowledgePanel-B6xMyHdp.css +1 -0
- package/dist/assets/WorkspaceKnowledgePanel-CE7qsu2i.js +2 -0
- package/dist/assets/WorkspaceKnowledgePanel-CE7qsu2i.js.map +1 -0
- package/dist/assets/WorkspaceRoutinesPanel-CLsHmBuH.css +1 -0
- package/dist/assets/WorkspaceRoutinesPanel-DybBh6PZ.js +2 -0
- package/dist/assets/WorkspaceRoutinesPanel-DybBh6PZ.js.map +1 -0
- package/dist/assets/WorkspaceShell-7wn8iTne.css +1 -0
- package/dist/assets/WorkspaceShell-kp1ObJTk.js +44 -0
- package/dist/assets/WorkspaceShell-kp1ObJTk.js.map +1 -0
- package/dist/assets/WorkspaceView-CtNI0_AO.js +2 -0
- package/dist/assets/WorkspaceView-CtNI0_AO.js.map +1 -0
- package/dist/assets/_plugin-vue_export-helper-CscYwSBt.js +3 -0
- package/dist/assets/_plugin-vue_export-helper-CscYwSBt.js.map +1 -0
- package/dist/assets/browser-B97PQaMt.js +11 -0
- package/dist/assets/browser-B97PQaMt.js.map +1 -0
- package/dist/assets/docx-preview-YvkiVUyQ.js +22 -0
- package/dist/assets/docx-preview-YvkiVUyQ.js.map +1 -0
- package/dist/assets/index-GQxxBKs2.js +17 -0
- package/dist/assets/index-GQxxBKs2.js.map +1 -0
- package/dist/assets/index-g5POnMkp.css +1 -0
- package/dist/assets/localVm-owThjmhy.js +2 -0
- package/dist/assets/localVm-owThjmhy.js.map +1 -0
- package/dist/assets/pdf-pqGpzpl1.js +55 -0
- package/dist/assets/pdf-pqGpzpl1.js.map +1 -0
- package/dist/assets/pdf.worker.min-iDqQPrd3.mjs +31 -0
- package/dist/assets/sessionOwnership-BTz0Fyp5.css +1 -0
- package/dist/assets/sessionOwnership-bedp16q1.js +6 -0
- package/dist/assets/sessionOwnership-bedp16q1.js.map +1 -0
- package/dist/assets/viewModels-BYFb-v4u.js +38 -0
- package/dist/assets/viewModels-BYFb-v4u.js.map +1 -0
- package/dist/assets/viewModels-CkZuSvKm.css +1 -0
- package/dist/brand/AppIcon-1024.png +0 -0
- package/dist/icons/apple-touch-icon.png +0 -0
- package/dist/icons/favicon-32.png +0 -0
- package/dist/icons/icon-192.png +0 -0
- package/dist/icons/icon-512.png +0 -0
- package/dist/icons/logo-64.png +0 -0
- package/dist/index.html +19 -0
- package/dist/manifest.webmanifest +21 -0
- package/dist/setting-previews/dark.png +0 -0
- package/dist/setting-previews/light.png +0 -0
- package/dist/setting-previews/system.png +0 -0
- package/dist-server/runner/agent.js +563 -0
- package/dist-server/runner/browser/installation.js +180 -0
- package/dist-server/runner/browser/publicProxy.js +157 -0
- package/dist-server/runner/browser/runtime.js +622 -0
- package/dist-server/runner/browser/types.js +8 -0
- package/dist-server/runner/computers/commandProcess.js +127 -0
- package/dist-server/runner/computers/compose.js +139 -0
- package/dist-server/runner/computers/computer-image/Dockerfile +101 -0
- package/dist-server/runner/computers/computer-image/Dockerfile.cursor +27 -0
- package/dist-server/runner/computers/computer-image/Dockerfile.network +9 -0
- package/dist-server/runner/computers/computer-image/compose_bridge.py +329 -0
- package/dist-server/runner/computers/computer-image/cursor-browser.sh +6 -0
- package/dist-server/runner/computers/computer-image/cursor-desktop.py +86 -0
- package/dist-server/runner/computers/computer-image/network_gateway.py +197 -0
- package/dist-server/runner/computers/computer-image/prepare-firefox.sh +11 -0
- package/dist-server/runner/computers/computer-image/skill_bundle.py +111 -0
- package/dist-server/runner/computers/computerFailures.js +30 -0
- package/dist-server/runner/computers/container.js +652 -0
- package/dist-server/runner/computers/dataSnapshot.js +62 -0
- package/dist-server/runner/computers/desktopClose.js +41 -0
- package/dist-server/runner/computers/imageCli.js +42 -0
- package/dist-server/runner/computers/imageManagement.js +321 -0
- package/dist-server/runner/computers/images.js +130 -0
- package/dist-server/runner/computers/localVm.js +270 -0
- package/dist-server/runner/computers/maintenance.js +96 -0
- package/dist-server/runner/computers/maintenanceCli.js +59 -0
- package/dist-server/runner/computers/migrateUserData.js +62 -0
- package/dist-server/runner/computers/pool.js +407 -0
- package/dist-server/runner/computers/third-party/openmausbot/LICENSE +202 -0
- package/dist-server/runner/computers/third-party/openmausbot/NOTICE +4 -0
- package/dist-server/runner/computers/userData.js +128 -0
- package/dist-server/runner/computers/workspaceRecovery.js +120 -0
- package/dist-server/runner/config.js +37 -0
- package/dist-server/runner/hermes-bots-bridge/README.md +102 -0
- package/dist-server/runner/hermes-bots-bridge/__init__.py +59 -0
- package/dist-server/runner/hermes-bots-bridge/bootstrap.py +31 -0
- package/dist-server/runner/hermes-bots-bridge/bridge_runtime.py +1179 -0
- package/dist-server/runner/hermes-bots-bridge/dashboard/bridge.js +2 -0
- package/dist-server/runner/hermes-bots-bridge/dashboard/manifest.json +9 -0
- package/dist-server/runner/hermes-bots-bridge/dashboard/plugin_api.py +132 -0
- package/dist-server/runner/hermes-bots-bridge/file_transfer.py +146 -0
- package/dist-server/runner/hermes-bots-bridge/model_settings.py +136 -0
- package/dist-server/runner/hermes-bots-bridge/plugin.yaml +4 -0
- package/dist-server/runner/hermes-bots-bridge/profile_runtime_repair.py +77 -0
- package/dist-server/runner/index.js +75 -0
- package/dist-server/runner/install-hermes-bridge.py +192 -0
- package/dist-server/runner/managedBrowser.js +266 -0
- package/dist-server/runner/network/computerProxy.js +117 -0
- package/dist-server/runner/network/managedProxy.js +96 -0
- package/dist-server/runner/network/publicDestination.js +47 -0
- package/dist-server/runner/network/socketBroker.js +131 -0
- package/dist-server/runner/network/upstreamProxy.js +118 -0
- package/dist-server/runner/worker/control.js +212 -0
- package/dist-server/runner/worker/fileTransfer.js +9 -0
- package/dist-server/runner/worker/gateway.js +1009 -0
- package/dist-server/runner/worker/guest_proxy.py +116 -0
- package/dist-server/runner/worker/hermes_worker.py +310 -0
- package/dist-server/runner/worker/hostTools.js +115 -0
- package/dist-server/runner/worker/process.js +123 -0
- package/dist-server/runner/worker/profileSession.js +144 -0
- package/dist-server/runner/worker/profile_skills.py +361 -0
- package/dist-server/runner/worker/skill_bundle.py +111 -0
- package/dist-server/runner/worker/skills.js +119 -0
- package/dist-server/runner/worker/taskEnvironment.js +35 -0
- package/dist-server/server/accountPairing.js +72 -0
- package/dist-server/server/allowedHostsConfiguration.js +170 -0
- package/dist-server/server/apns.js +261 -0
- package/dist-server/server/apnsConfiguration.js +298 -0
- package/dist-server/server/app.js +639 -0
- package/dist-server/server/botModelSettings.js +60 -0
- package/dist-server/server/botPlugins/connectedApps.js +213 -0
- package/dist-server/server/botPlugins/httpMcp.js +117 -0
- package/dist-server/server/botPlugins/stdioMcp.js +263 -0
- package/dist-server/server/botPlugins/workspacePlugins.js +353 -0
- package/dist-server/server/browserFiles.js +79 -0
- package/dist-server/server/buildIdentity.js +18 -0
- package/dist-server/server/chatCache.js +1651 -0
- package/dist-server/server/chatEventProjection.js +70 -0
- package/dist-server/server/chatTranscriptApi.js +145 -0
- package/dist-server/server/chatTranscriptStore.js +1021 -0
- package/dist-server/server/chatUnread.js +34 -0
- package/dist-server/server/codexAuthorization.js +149 -0
- package/dist-server/server/composeDesktops.js +69 -0
- package/dist-server/server/computerControls.js +122 -0
- package/dist-server/server/computerNames.js +41 -0
- package/dist-server/server/config.js +176 -0
- package/dist-server/server/dashboardController.js +120 -0
- package/dist-server/server/dashboardSupervisor.js +176 -0
- package/dist-server/server/desktopActivation.js +26 -0
- package/dist-server/server/desktopEnvironments.js +600 -0
- package/dist-server/server/desktopHostProtocol.js +17 -0
- package/dist-server/server/desktopHosts.js +117 -0
- package/dist-server/server/errors.js +12 -0
- package/dist-server/server/executionSettings.js +171 -0
- package/dist-server/server/fcm.js +247 -0
- package/dist-server/server/fcmConfiguration.js +270 -0
- package/dist-server/server/fileAccess.js +103 -0
- package/dist-server/server/fileTransfer.js +98 -0
- package/dist-server/server/githubReleases.js +73 -0
- package/dist-server/server/grokAuth.js +333 -0
- package/dist-server/server/grokCloud.js +458 -0
- package/dist-server/server/hermesBotRelay.js +117 -0
- package/dist-server/server/hermesBridge.js +245 -0
- package/dist-server/server/hostToolSettings.js +60 -0
- package/dist-server/server/imageManagement.js +55 -0
- package/dist-server/server/index.js +132 -0
- package/dist-server/server/leases.js +69 -0
- package/dist-server/server/localAuth.js +737 -0
- package/dist-server/server/localVm.js +358 -0
- package/dist-server/server/loopbackAuthorization.js +58 -0
- package/dist-server/server/managedBrowserTools.js +16 -0
- package/dist-server/server/managedBrowsers.js +735 -0
- package/dist-server/server/notificationText.js +125 -0
- package/dist-server/server/openVikingConfiguration.js +222 -0
- package/dist-server/server/openVikingMemoryProvider.js +279 -0
- package/dist-server/server/openVikingService.js +177 -0
- package/dist-server/server/openVikingSessionSync.js +286 -0
- package/dist-server/server/pairing.js +342 -0
- package/dist-server/server/profileIdentities.js +103 -0
- package/dist-server/server/pushCoordinator.js +1422 -0
- package/dist-server/server/pushEventAdapter.js +31 -0
- package/dist-server/server/pushEvents.js +1218 -0
- package/dist-server/server/realtimeApi.js +330 -0
- package/dist-server/server/realtimeBroker.js +835 -0
- package/dist-server/server/realtimeProtocol.js +353 -0
- package/dist-server/server/realtimeReceipts.js +60 -0
- package/dist-server/server/releases.js +46 -0
- package/dist-server/server/routes.js +2156 -0
- package/dist-server/server/runnerHub.js +687 -0
- package/dist-server/server/security.js +196 -0
- package/dist-server/server/serverIdentity.js +31 -0
- package/dist-server/server/serviceInstance.js +178 -0
- package/dist-server/server/sessionWorkingDirectory.js +39 -0
- package/dist-server/server/sharedComputers.js +221 -0
- package/dist-server/server/subaccountAccess.js +28 -0
- package/dist-server/server/taskCoordinator.js +580 -0
- package/dist-server/server/toolOperationJournal.js +81 -0
- package/dist-server/server/updateManager.js +340 -0
- package/dist-server/server/uploads.js +265 -0
- package/dist-server/server/upstream.js +375 -0
- package/dist-server/server/upstreamReadCache.js +133 -0
- package/dist-server/server/vmComputer.js +96 -0
- package/dist-server/server/websocket.js +753 -0
- package/dist-server/server/workspaceAssets.js +192 -0
- package/dist-server/server/workspaceCollaboration.js +224 -0
- package/dist-server/server/workspaceDiscussion.js +23 -0
- package/dist-server/server/workspaceEnvironment.js +43 -0
- package/dist-server/server/workspaceGateway.js +509 -0
- package/dist-server/server/workspaceInspector.js +47 -0
- package/dist-server/server/workspaceKnowledge.js +557 -0
- package/dist-server/server/workspaceKnowledgeRoutes.js +78 -0
- package/dist-server/server/workspaceKnowledgeTools.js +105 -0
- package/dist-server/server/workspaceMemoryContext.js +34 -0
- package/dist-server/server/workspaceMemorySynthesis.js +201 -0
- package/dist-server/server/workspaceMentions.js +31 -0
- package/dist-server/server/workspacePairedNode.js +166 -0
- package/dist-server/server/workspacePrompt.js +93 -0
- package/dist-server/server/workspaceRelay.js +90 -0
- package/dist-server/server/workspaceRemoteAgents.js +191 -0
- package/dist-server/server/workspaceRoutes.js +703 -0
- package/dist-server/server/workspaceRoutineTools.js +109 -0
- package/dist-server/server/workspaceRoutines.js +163 -0
- package/dist-server/server/workspaceRuntime.js +1363 -0
- package/dist-server/server/workspaceScheduler.js +592 -0
- package/dist-server/server/workspaceStore.js +1220 -0
- package/dist-server/server/workspaceSync.js +129 -0
- package/dist-server/server/workspaceTeamTools.js +312 -0
- package/dist-server/server/workspaceToolLease.js +280 -0
- package/dist-server/shared/agentIdentity.js +90 -0
- package/dist-server/shared/agentTasks.js +1 -0
- package/dist-server/shared/botEnvironment.js +28 -0
- package/dist-server/shared/botModelSettings.js +3 -0
- package/dist-server/shared/chatTools.js +32 -0
- package/dist-server/shared/chatTranscript.js +50 -0
- package/dist-server/shared/composeDesktops.js +1 -0
- package/dist-server/shared/computerControl.js +1 -0
- package/dist-server/shared/desktopEnvironment.js +1 -0
- package/dist-server/shared/desktopHost.js +1 -0
- package/dist-server/shared/executionEnvironment.js +1 -0
- package/dist-server/shared/fileTransfer.js +5 -0
- package/dist-server/shared/fileTransferEndpoint.mjs +133 -0
- package/dist-server/shared/grokAuth.js +1 -0
- package/dist-server/shared/hermesBridge.js +1 -0
- package/dist-server/shared/hostPaths.mjs +22 -0
- package/dist-server/shared/imageManagement.js +1 -0
- package/dist-server/shared/index.js +1 -0
- package/dist-server/shared/localVm.js +9 -0
- package/dist-server/shared/managedBrowser.js +1 -0
- package/dist-server/shared/messageFiles.js +90 -0
- package/dist-server/shared/openVikingSync.js +1 -0
- package/dist-server/shared/runner.js +2 -0
- package/dist-server/shared/serverFiles.js +55 -0
- package/dist-server/shared/serverIdentity.js +1 -0
- package/dist-server/shared/sse.js +47 -0
- package/dist-server/shared/types.js +8 -0
- package/dist-server/shared/workspace.js +61 -0
- package/dist-server/shared/workspaceKnowledge.js +1 -0
- package/dist-server/shared/workspaceLifecycle.js +1 -0
- package/dist-server/shared/workspaceMessagePatch.js +63 -0
- package/dist-server/shared/workspacePanels.js +1 -0
- package/dist-server/shared/workspacePlugins.js +1 -0
- package/licenses/LaoA-GrokBot/LICENSE +21 -0
- package/licenses/OpenMausBot/LICENSE +202 -0
- package/licenses/OpenMausBot/NOTICE +6 -0
- package/package.json +122 -0
- package/release.json +6 -0
|
@@ -0,0 +1,592 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { WorkspaceStore } from './workspaceStore.js';
|
|
3
|
+
import { HttpError } from './errors.js';
|
|
4
|
+
import { discussionOrder } from './workspaceDiscussion.js';
|
|
5
|
+
import { botRelayIntent, relayFingerprint, relayMessageValue, REPEATED_RELAY_NOTICE } from './workspaceRelay.js';
|
|
6
|
+
export const NO_REPLY = '[[YAOYAO_NO_REPLY_V1]]';
|
|
7
|
+
export const RESOURCE_WAIT_CODES = new Set(['computer_busy', 'computer_quota']);
|
|
8
|
+
export const WORKSPACE_CONCURRENCY_LIMIT = 10;
|
|
9
|
+
export const HOST_FALLBACK = '我还不能确定你希望我处理什么,请补充具体目标、范围,或明确需要我协调的机器人。';
|
|
10
|
+
const terminal = (status) => ['complete', 'failed', 'interrupted'].includes(status);
|
|
11
|
+
/** Durable per-member work and cascade ledger; Hermes transport lives in WorkspaceRuntime. */
|
|
12
|
+
export class WorkspaceScheduler {
|
|
13
|
+
store;
|
|
14
|
+
userActive;
|
|
15
|
+
closing = false;
|
|
16
|
+
executing = new Set();
|
|
17
|
+
wakePending = false;
|
|
18
|
+
pumping = false;
|
|
19
|
+
retryTimer;
|
|
20
|
+
recoverAfter = new Map();
|
|
21
|
+
attempts = new Map();
|
|
22
|
+
interrupting = new Set();
|
|
23
|
+
onMessage = async () => { };
|
|
24
|
+
onNotify = () => { };
|
|
25
|
+
onRunSettled = () => { };
|
|
26
|
+
constructor(store, userActive = () => true) {
|
|
27
|
+
this.store = store;
|
|
28
|
+
this.userActive = userActive;
|
|
29
|
+
for (const owner of store.owners()) {
|
|
30
|
+
// Forward the already-admitted v0.3.x turn without replaying its prompt.
|
|
31
|
+
for (const root of store.list(owner, 'run')) {
|
|
32
|
+
if (terminal(root.status) || this.works(owner, root.id).length)
|
|
33
|
+
continue;
|
|
34
|
+
const c = store.require(owner, 'conversation', root.conversationId);
|
|
35
|
+
const trigger = store.require(owner, 'message', root.messageId);
|
|
36
|
+
const continuation = root.queue?.length === 0;
|
|
37
|
+
const ids = continuation ? root.hostReturn ? [c.administratorId] : root.next ?? [] : root.queue ?? [root.activeAgentId ?? c.administratorId];
|
|
38
|
+
if (!ids.length && root.currentMessageId) {
|
|
39
|
+
const agentId = store.get(owner, 'message', root.currentMessageId)?.agentId ?? root.activeAgentId;
|
|
40
|
+
if (agentId)
|
|
41
|
+
ids.push(agentId);
|
|
42
|
+
}
|
|
43
|
+
if (!ids.length) {
|
|
44
|
+
root.status = root.stopRequested ? 'interrupted' : 'complete';
|
|
45
|
+
store.saveRun(owner, root);
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const last = store.messages(owner, c.id).filter(m => m.runId === root.id).at(-1);
|
|
49
|
+
for (const [index, id] of ids.entries()) {
|
|
50
|
+
const work = this.enqueue(owner, root, id, root.round + (continuation && !root.hostReturn ? 1 : 0), root.id, last?.seq ?? trigger.seq, 'mentioned', id === c.administratorId);
|
|
51
|
+
if (index === 0 && root.currentMessageId) {
|
|
52
|
+
Object.assign(work, { currentMessageId: root.currentMessageId, submitted: true, status: 'uncertain', turnConfiguration: root.turnConfiguration });
|
|
53
|
+
store.put(owner, 'turn', work.id, work);
|
|
54
|
+
const key = this.bindingKey(c.id, root.conversationTaskId, id);
|
|
55
|
+
const binding = store.get(owner, 'binding', key);
|
|
56
|
+
if (binding?.runId === root.id && binding.messageId === root.currentMessageId) {
|
|
57
|
+
binding.taskId = work.id;
|
|
58
|
+
store.put(owner, 'binding', key, binding);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
for (const work of this.works(owner)) {
|
|
64
|
+
if (['running', 'waiting'].includes(work.status)) {
|
|
65
|
+
work.status = work.submitted ? 'uncertain' : 'queued';
|
|
66
|
+
work.error = work.submitted ? '服务已重启,正在核对原执行' : undefined;
|
|
67
|
+
store.put(owner, 'turn', work.id, work);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
notify(owner, c, run, message, interaction) {
|
|
73
|
+
try {
|
|
74
|
+
this.onNotify(owner, c, run, message, interaction);
|
|
75
|
+
}
|
|
76
|
+
catch { /* Push cannot change execution. */ }
|
|
77
|
+
}
|
|
78
|
+
bindingKey(conversationId, conversationTaskId, agentId) {
|
|
79
|
+
return conversationTaskId ? `${conversationId}:${conversationTaskId}:${agentId}` : `${conversationId}:${agentId}`;
|
|
80
|
+
}
|
|
81
|
+
works(owner, runId) {
|
|
82
|
+
return this.store.db.prepare(`SELECT data FROM workspace_entities WHERE owner=? AND kind='turn' ${runId ? "AND json_extract(data,'$.runId')=?" : ''}`).all(...(runId ? [owner, runId] : [owner]))
|
|
83
|
+
.map(row => JSON.parse(String(row.data)));
|
|
84
|
+
}
|
|
85
|
+
pendingWorks(owner) {
|
|
86
|
+
return this.store.db.prepare("SELECT data FROM workspace_entities WHERE owner=? AND kind='turn' AND (json_extract(data,'$.status') NOT IN ('complete','failed','interrupted') OR coalesce(json_extract(data,'$.planned'),0)=0 OR coalesce(json_extract(data,'$.cleanupPending'),0)=1)").all(owner)
|
|
87
|
+
.map(row => JSON.parse(String(row.data)));
|
|
88
|
+
}
|
|
89
|
+
getWork(owner, id) { return this.store.require(owner, 'turn', id); }
|
|
90
|
+
saveWork(owner, work) {
|
|
91
|
+
this.store.atomic(() => { this.store.put(owner, 'turn', work.id, work); this.updateRoot(owner, work.runId); });
|
|
92
|
+
}
|
|
93
|
+
resolveInteractions(owner, taskId) {
|
|
94
|
+
for (const interaction of this.store.list(owner, 'interaction')) {
|
|
95
|
+
const binding = this.store.get(owner, 'interaction-binding', interaction.id);
|
|
96
|
+
if (binding?.taskId === taskId && !interaction.resolved) {
|
|
97
|
+
interaction.resolved = true;
|
|
98
|
+
this.store.put(owner, 'interaction', interaction.id, interaction);
|
|
99
|
+
this.store.event(owner, 'interaction.changed', interaction, interaction.conversationId);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
enqueue(owner, root, agentId, depth, batchId, triggerSeq, replyMode, requiredReply, reviewOf) {
|
|
104
|
+
const existing = this.works(owner, root.id).find(w => reviewOf ? w.reviewOf === reviewOf : w.agentId === agentId && w.depth === depth && !w.reviewOf);
|
|
105
|
+
if (existing)
|
|
106
|
+
return existing;
|
|
107
|
+
const work = { id: randomUUID(), runId: root.id, conversationId: root.conversationId, conversationTaskId: root.conversationTaskId, agentId, messageId: root.messageId, depth, batchId, triggerSeq, replyMode, requiredReply, reviewOf, status: 'queued', createdAt: Date.now() };
|
|
108
|
+
this.store.put(owner, 'turn', work.id, work);
|
|
109
|
+
return work;
|
|
110
|
+
}
|
|
111
|
+
admit(owner, root, c, trigger) {
|
|
112
|
+
this.store.put(owner, 'run', root.id, root);
|
|
113
|
+
if (root.discussion) {
|
|
114
|
+
const first = discussionOrder(root.discussion)[0];
|
|
115
|
+
if (first) {
|
|
116
|
+
const work = this.enqueue(owner, root, first.agentId, 0, root.id, trigger.seq, 'mentioned', false);
|
|
117
|
+
work.discussionIndex = 0;
|
|
118
|
+
this.store.put(owner, 'turn', work.id, work);
|
|
119
|
+
}
|
|
120
|
+
this.updateRoot(owner, root.id);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const targeted = root.targetAgentId;
|
|
124
|
+
const ids = targeted ? [root.targetAgentId] : c.kind === 'direct' || c.mode === 'host' ? [c.administratorId]
|
|
125
|
+
: root.mentionIds.length ? root.mentionIds : [...new Set([c.administratorId, ...c.autoReplyIds])];
|
|
126
|
+
for (const id of ids)
|
|
127
|
+
this.enqueue(owner, root, id, 0, root.id, trigger.seq, c.kind === 'direct' || targeted ? 'mentioned' : c.mode === 'host' || !root.mentionIds.length ? 'automatic' : 'mentioned', !!targeted || c.kind === 'group' && id === c.administratorId && (c.mode === 'host' || !root.mentionIds.length));
|
|
128
|
+
this.updateRoot(owner, root.id);
|
|
129
|
+
}
|
|
130
|
+
start() { this.wake(); }
|
|
131
|
+
wake() {
|
|
132
|
+
if (this.closing || this.wakePending)
|
|
133
|
+
return;
|
|
134
|
+
this.wakePending = true;
|
|
135
|
+
queueMicrotask(() => { this.wakePending = false; if (!this.closing)
|
|
136
|
+
this.pump(); });
|
|
137
|
+
}
|
|
138
|
+
retrySoon() {
|
|
139
|
+
if (this.closing || this.retryTimer)
|
|
140
|
+
return;
|
|
141
|
+
this.retryTimer = setTimeout(() => { this.retryTimer = undefined; this.wake(); }, 500);
|
|
142
|
+
this.retryTimer.unref();
|
|
143
|
+
}
|
|
144
|
+
updateRoot(owner, id) {
|
|
145
|
+
const root = this.store.require(owner, 'run', id), tasks = this.works(owner, id);
|
|
146
|
+
const active = tasks.filter(t => !terminal(t.status));
|
|
147
|
+
const done = !active.length && tasks.every(t => t.planned);
|
|
148
|
+
const wasTerminal = terminal(root.status);
|
|
149
|
+
root.round = Math.max(0, ...tasks.map(t => t.depth));
|
|
150
|
+
root.activeAgentId = active.find(t => t.status !== 'queued')?.agentId ?? active[0]?.agentId;
|
|
151
|
+
if (done) {
|
|
152
|
+
const interrupted = root.stopRequested || tasks.some(t => t.requiredReply && t.status === 'interrupted') || (tasks.length > 0 && tasks.every(t => t.status === 'interrupted'));
|
|
153
|
+
const failed = tasks.some(t => t.status === 'failed' && (t.requiredReply || this.store.require(owner, 'conversation', root.conversationId).kind === 'direct'))
|
|
154
|
+
|| !!root.discussion && tasks.length > 0 && tasks.every(t => ['failed', 'interrupted'].includes(t.status));
|
|
155
|
+
root.status = interrupted ? 'interrupted' : failed ? 'failed' : 'complete';
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
root.status = active.some(t => t.status === 'uncertain') ? 'uncertain' : active.some(t => t.status === 'running') ? 'running' : active.some(t => t.status === 'waiting') ? 'waiting' : tasks.some(t => t.status !== 'queued') ? 'running' : 'queued';
|
|
159
|
+
}
|
|
160
|
+
root.error = tasks.find(t => t.status === 'uncertain' || t.status === 'failed')?.error ?? active.find(t => t.error)?.error;
|
|
161
|
+
this.store.saveRun(owner, root);
|
|
162
|
+
if (done && !wasTerminal)
|
|
163
|
+
this.onRunSettled(owner, root);
|
|
164
|
+
if (done && !wasTerminal && root.status !== 'interrupted') {
|
|
165
|
+
const c = this.store.require(owner, 'conversation', root.conversationId);
|
|
166
|
+
if (root.status === 'failed' && c.kind === 'direct')
|
|
167
|
+
this.store.saveMessage(owner, { id: randomUUID(), conversationId: c.id, conversationTaskId: root.conversationTaskId, seq: 0, role: 'system', content: `执行失败:${root.error ?? '运行失败'}`, reasoning: '', status: 'failed', runId: root.id, attachments: [], tools: [], createdAt: Date.now() });
|
|
168
|
+
this.notify(owner, c, root, this.store.messages(owner, c.id, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, false, root.conversationTaskId).filter(m => m.runId === id && m.visible !== false).at(-1));
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
plan(owner, id) {
|
|
172
|
+
this.store.atomic(() => {
|
|
173
|
+
const work = this.getWork(owner, id);
|
|
174
|
+
if (!terminal(work.status) || work.planned)
|
|
175
|
+
return;
|
|
176
|
+
const root = this.store.require(owner, 'run', work.runId), c = this.store.require(owner, 'conversation', work.conversationId);
|
|
177
|
+
work.planned = true;
|
|
178
|
+
this.store.put(owner, 'turn', work.id, work);
|
|
179
|
+
if (root.discussion && !root.stopRequested && !c.archived) {
|
|
180
|
+
const order = discussionOrder(root.discussion);
|
|
181
|
+
let index = (work.discussionIndex ?? 0) + 1;
|
|
182
|
+
while (index < order.length) {
|
|
183
|
+
const next = order[index];
|
|
184
|
+
const agent = this.store.get(owner, 'agent', next.agentId);
|
|
185
|
+
if (agent && !agent.archived && c.memberIds.includes(next.agentId)) {
|
|
186
|
+
const through = Math.max(work.triggerSeq, ...this.works(owner, root.id).map(w => w.currentMessageId ? this.store.get(owner, 'message', w.currentMessageId)?.seq ?? 0 : 0));
|
|
187
|
+
const queued = this.enqueue(owner, root, next.agentId, next.round, root.id, through, 'mentioned', false);
|
|
188
|
+
queued.discussionIndex = index;
|
|
189
|
+
this.store.put(owner, 'turn', queued.id, queued);
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
index++;
|
|
193
|
+
}
|
|
194
|
+
this.updateRoot(owner, root.id);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (root.stopRequested || c.archived || c.kind === 'direct' || root.assignmentId || root.goalId || root.peerMessageId) {
|
|
198
|
+
this.updateRoot(owner, root.id);
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
const config = work.turnConfiguration ?? { mode: c.mode, administratorId: c.administratorId, members: c.memberIds.map(id => this.store.require(owner, 'agent', id)) };
|
|
202
|
+
const message = work.currentMessageId ? this.store.get(owner, 'message', work.currentMessageId) : undefined;
|
|
203
|
+
if (config.mode === 'host' && work.agentId !== config.administratorId) {
|
|
204
|
+
const batch = this.works(owner, root.id).filter(t => t.batchId === work.batchId && !t.reviewOf);
|
|
205
|
+
if (batch.every(t => terminal(t.status))) {
|
|
206
|
+
const through = Math.max(work.triggerSeq, ...batch.map(t => t.currentMessageId ? this.store.get(owner, 'message', t.currentMessageId)?.seq ?? 0 : 0));
|
|
207
|
+
this.enqueue(owner, root, c.administratorId, work.depth, `review:${work.batchId}`, through, 'automatic', true, work.batchId);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
else if (work.status === 'complete' && !work.silent && message) {
|
|
211
|
+
const nextDepth = work.depth + 1;
|
|
212
|
+
const intent = botRelayIntent(message.content, config.members);
|
|
213
|
+
const eligible = (id) => id !== work.agentId && this.store.taskMemberIds(owner, c, work.conversationTaskId).includes(id);
|
|
214
|
+
const mentions = intent.targetIds.filter(eligible);
|
|
215
|
+
// A filtered mention is still an explicit address. Never turn a closing
|
|
216
|
+
// receipt back into a broadcast through the automatic participant list.
|
|
217
|
+
const automatic = config.mode === 'free' ? c.autoReplyIds.filter(eligible) : [];
|
|
218
|
+
const targets = intent.mentionedIds.length ? mentions : intent.acknowledgementOnly ? [] : automatic;
|
|
219
|
+
work.relay = { fingerprints: {}, suppressed: {} };
|
|
220
|
+
for (const target of intent.mentionedIds.filter(eligible)) {
|
|
221
|
+
if (!mentions.includes(target))
|
|
222
|
+
work.relay.suppressed[target] = 'acknowledgement';
|
|
223
|
+
}
|
|
224
|
+
if (intent.acknowledgementOnly && !intent.mentionedIds.length)
|
|
225
|
+
for (const target of automatic)
|
|
226
|
+
work.relay.suppressed[target] = 'acknowledgement';
|
|
227
|
+
if (targets.length && (c.maxReplyRounds === -1 || nextDepth < c.maxReplyRounds)) {
|
|
228
|
+
const works = this.works(owner, root.id).filter(t => t.conversationId === c.id && t.conversationTaskId === work.conversationTaskId);
|
|
229
|
+
const evidence = this.relayEvidence(owner, work, works);
|
|
230
|
+
const previous = works.filter(t => t.id !== work.id && t.agentId === work.agentId && t.status === 'complete' && t.planned);
|
|
231
|
+
const legacyEvidence = new Map();
|
|
232
|
+
for (const target of targets) {
|
|
233
|
+
const fingerprint = evidence === undefined ? undefined : relayFingerprint({ target, evidence });
|
|
234
|
+
if (fingerprint)
|
|
235
|
+
work.relay.fingerprints[target] = fingerprint;
|
|
236
|
+
const repeated = fingerprint && previous.some(t => {
|
|
237
|
+
if (t.relay)
|
|
238
|
+
return t.relay.fingerprints[target] === fingerprint;
|
|
239
|
+
// Pre-upgrade turns have no receipt. Only a persisted child is
|
|
240
|
+
// evidence that this particular edge was actually dispatched.
|
|
241
|
+
if (!works.some(child => child.batchId === t.id && child.agentId === target && !child.reviewOf))
|
|
242
|
+
return false;
|
|
243
|
+
if (!legacyEvidence.has(t.id))
|
|
244
|
+
legacyEvidence.set(t.id, this.relayEvidence(owner, t, works));
|
|
245
|
+
const old = legacyEvidence.get(t.id);
|
|
246
|
+
return old !== undefined && relayFingerprint({ target, evidence: old }) === fingerprint;
|
|
247
|
+
});
|
|
248
|
+
if (repeated) {
|
|
249
|
+
work.relay.suppressed[target] = 'repeated';
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
this.enqueue(owner, root, target, nextDepth, work.id, message.seq, mentions.includes(target) ? 'mentioned' : 'automatic', false);
|
|
253
|
+
}
|
|
254
|
+
if (Object.values(work.relay.suppressed).includes('repeated') && !works.some(t => t.relay?.notice)) {
|
|
255
|
+
work.relay.notice = true;
|
|
256
|
+
this.store.saveMessage(owner, { id: randomUUID(), conversationId: c.id, conversationTaskId: root.conversationTaskId, seq: 0, role: 'system', content: REPEATED_RELAY_NOTICE, reasoning: '', status: 'complete', runId: root.id, attachments: [], tools: [], createdAt: Date.now() });
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
else if (targets.length && !this.store.get(owner, 'limit-notice', root.id)) {
|
|
260
|
+
this.store.put(owner, 'limit-notice', root.id, { id: root.id });
|
|
261
|
+
this.store.saveMessage(owner, { id: randomUUID(), conversationId: c.id, conversationTaskId: root.conversationTaskId, seq: 0, role: 'system', content: '已达到自动协作轮数上限,本轮不再自动分派。', reasoning: '', status: 'complete', runId: root.id, attachments: [], tools: [], createdAt: Date.now() });
|
|
262
|
+
}
|
|
263
|
+
this.store.put(owner, 'turn', work.id, work);
|
|
264
|
+
}
|
|
265
|
+
this.updateRoot(owner, root.id);
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
relayEvidence(owner, work, works) {
|
|
269
|
+
const read = (id) => {
|
|
270
|
+
const message = id ? this.store.get(owner, 'message', id) : undefined;
|
|
271
|
+
return message?.conversationId === work.conversationId && message.conversationTaskId === work.conversationTaskId && message.runId === work.runId ? message : undefined;
|
|
272
|
+
};
|
|
273
|
+
const reply = read(work.currentMessageId);
|
|
274
|
+
if (!reply)
|
|
275
|
+
return undefined;
|
|
276
|
+
const inputs = work.reviewOf ? works.filter(t => t.batchId === work.reviewOf && !t.reviewOf).sort((a, b) => a.agentId.localeCompare(b.agentId))
|
|
277
|
+
: works.filter(t => t.id === work.batchId);
|
|
278
|
+
let input;
|
|
279
|
+
if (inputs.length) {
|
|
280
|
+
input = inputs.map(t => {
|
|
281
|
+
const message = read(t.currentMessageId);
|
|
282
|
+
return { agentId: t.agentId, status: t.status, error: t.error, message: message ? relayMessageValue(message) : null };
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
// Initial turns and the legacy admission path have no parent Work.
|
|
287
|
+
const row = this.store.db.prepare("SELECT data FROM workspace_entities WHERE owner=? AND kind='message' AND json_extract(data,'$.conversationId')=? AND json_extract(data,'$.seq')=?").get(owner, work.conversationId, work.triggerSeq);
|
|
288
|
+
const trigger = row ? read(JSON.parse(String(row.data)).id) : undefined;
|
|
289
|
+
if (!trigger)
|
|
290
|
+
return undefined;
|
|
291
|
+
input = relayMessageValue(trigger);
|
|
292
|
+
}
|
|
293
|
+
const relevant = new Set([work.id, ...inputs.map(t => t.id)]);
|
|
294
|
+
const interactions = this.store.list(owner, 'interaction')
|
|
295
|
+
.filter(i => i.runId === work.runId && i.conversationId === work.conversationId && i.conversationTaskId === work.conversationTaskId && i.answer !== undefined && i.responseState === 'sent'
|
|
296
|
+
&& relevant.has(this.store.get(owner, 'interaction-binding', i.id)?.taskId ?? ''))
|
|
297
|
+
// A fresh human answer is permission to advance, even if the person
|
|
298
|
+
// chooses the same answer again. This is not a provider event ID.
|
|
299
|
+
.map(i => ({ id: i.id, kind: i.kind, question: i.message, answer: i.answer })).sort((a, b) => a.id.localeCompare(b.id));
|
|
300
|
+
return { version: 1, agentId: work.agentId, mode: work.turnConfiguration?.mode, input, reply: relayMessageValue(reply), interactions };
|
|
301
|
+
}
|
|
302
|
+
pump() {
|
|
303
|
+
if (this.pumping || this.closing)
|
|
304
|
+
return;
|
|
305
|
+
this.pumping = true;
|
|
306
|
+
try {
|
|
307
|
+
let all = this.store.owners().flatMap(owner => this.pendingWorks(owner).map(work => ({ owner, work })));
|
|
308
|
+
for (const { owner, work } of all) {
|
|
309
|
+
if (terminal(work.status) && work.cleanupPending)
|
|
310
|
+
this.scheduleInterruptCleanup(owner, work);
|
|
311
|
+
}
|
|
312
|
+
for (const { owner, work } of all) {
|
|
313
|
+
if (['running', 'waiting'].includes(work.status) && !this.executing.has(work.id)) {
|
|
314
|
+
work.status = work.submitted ? 'uncertain' : 'queued';
|
|
315
|
+
this.saveWork(owner, work);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
for (const { owner, work } of all)
|
|
319
|
+
if (terminal(work.status) && !work.planned && !this.executing.has(work.id))
|
|
320
|
+
this.plan(owner, work.id);
|
|
321
|
+
all = this.store.owners().flatMap(owner => this.pendingWorks(owner).map(work => ({ owner, work })));
|
|
322
|
+
all.sort((a, b) => {
|
|
323
|
+
const priority = (entry) => {
|
|
324
|
+
if (Date.now() - entry.work.createdAt > 60_000)
|
|
325
|
+
return 0;
|
|
326
|
+
const run = this.store.get(entry.owner, 'run', entry.work.runId);
|
|
327
|
+
if (run?.triggerKind === 'peer' && run.priority)
|
|
328
|
+
return 0.5;
|
|
329
|
+
return run?.triggerKind === 'assignment' ? 2 : run?.triggerKind ? 1 : 0;
|
|
330
|
+
};
|
|
331
|
+
const difference = priority(a) - priority(b);
|
|
332
|
+
if (difference)
|
|
333
|
+
return difference;
|
|
334
|
+
const am = this.store.require(a.owner, 'message', a.work.messageId), bm = this.store.require(b.owner, 'message', b.work.messageId);
|
|
335
|
+
return am.createdAt - bm.createdAt || (a.work.conversationId === b.work.conversationId && a.work.conversationTaskId === b.work.conversationTaskId ? am.seq - bm.seq : 0) || a.work.createdAt - b.work.createdAt || a.work.id.localeCompare(b.work.id);
|
|
336
|
+
});
|
|
337
|
+
for (const { owner, work: candidate } of all) {
|
|
338
|
+
const work = this.getWork(owner, candidate.id);
|
|
339
|
+
if (work.cancelRequested && work.status === 'uncertain' && this.executing.has(work.id)) {
|
|
340
|
+
if ((this.recoverAfter.get(work.id) ?? 0) > Date.now()) {
|
|
341
|
+
this.retrySoon();
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
if (!this.interrupting.has(work.id)) {
|
|
345
|
+
this.interrupting.add(work.id);
|
|
346
|
+
void this.interruptTurn(owner, work).catch(error => {
|
|
347
|
+
if (this.closing)
|
|
348
|
+
return;
|
|
349
|
+
const current = this.getWork(owner, work.id);
|
|
350
|
+
if (terminal(current.status))
|
|
351
|
+
return;
|
|
352
|
+
current.status = 'uncertain';
|
|
353
|
+
current.error = `停止状态待确认:${error instanceof Error ? error.message : '连接中断'}`;
|
|
354
|
+
this.saveWork(owner, current);
|
|
355
|
+
const attempt = this.attempts.get(work.id) ?? 0;
|
|
356
|
+
this.attempts.set(work.id, attempt + 1);
|
|
357
|
+
this.recoverAfter.set(work.id, Date.now() + Math.min(30_000, 500 * 2 ** Math.min(attempt, 6)));
|
|
358
|
+
this.retrySoon();
|
|
359
|
+
}).finally(() => { this.interrupting.delete(work.id); this.wake(); }).catch(() => this.retrySoon());
|
|
360
|
+
}
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
if (this.executing.size >= WORKSPACE_CONCURRENCY_LIMIT || !['queued', 'uncertain'].includes(work.status) || this.executing.has(work.id))
|
|
364
|
+
continue;
|
|
365
|
+
if (!this.userActive(owner)) {
|
|
366
|
+
this.retrySoon();
|
|
367
|
+
continue;
|
|
368
|
+
}
|
|
369
|
+
const c = this.store.require(owner, 'conversation', work.conversationId), root = this.store.require(owner, 'run', work.runId);
|
|
370
|
+
if (work.status === 'queued' && !root.targetAgentId && !root.discussion && c.mode === 'host' && work.depth === 0 && work.batchId === root.id && work.requiredReply && !work.currentMessageId) {
|
|
371
|
+
work.agentId = c.administratorId;
|
|
372
|
+
}
|
|
373
|
+
if (work.status === 'queued' && (c.archived || root.stopRequested || !this.store.taskMemberIds(owner, c, work.conversationTaskId).includes(work.agentId))) {
|
|
374
|
+
work.status = 'interrupted';
|
|
375
|
+
work.error = '执行前成员已移除或聊天已停止';
|
|
376
|
+
this.saveWork(owner, work);
|
|
377
|
+
this.wake();
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
const occupied = all.map(entry => this.getWork(entry.owner, entry.work.id)).filter(t => (['running', 'waiting', 'uncertain'].includes(t.status) || this.executing.has(t.id)) && t.id !== work.id);
|
|
381
|
+
// Only the conversation/task/Agent binding shares mutable session state.
|
|
382
|
+
// Other sessions may run concurrently; computer leases and tool queues
|
|
383
|
+
// continue to serialize access to their actual shared resources.
|
|
384
|
+
if (occupied.some(t => t.conversationId === c.id && t.conversationTaskId === work.conversationTaskId && t.agentId === work.agentId))
|
|
385
|
+
continue;
|
|
386
|
+
const inTask = occupied.filter(t => t.conversationId === c.id && t.conversationTaskId === work.conversationTaskId);
|
|
387
|
+
if (root.discussion && inTask.length)
|
|
388
|
+
continue;
|
|
389
|
+
if (work.status === 'queued' && (occupied.length >= WORKSPACE_CONCURRENCY_LIMIT || inTask.length >= 3))
|
|
390
|
+
continue;
|
|
391
|
+
if ((this.recoverAfter.get(work.id) ?? 0) > Date.now()) {
|
|
392
|
+
this.retrySoon();
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
if (c.mode === 'host' || c.kind === 'direct') {
|
|
396
|
+
if (inTask.some(t => c.kind === 'direct'
|
|
397
|
+
|| (!root.assignmentId && (t.runId !== work.runId || t.batchId !== work.batchId))
|
|
398
|
+
|| t.agentId === c.administratorId || work.agentId === c.administratorId))
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
this.executing.add(work.id);
|
|
402
|
+
const recovering = work.status === 'uncertain';
|
|
403
|
+
if (!recovering) {
|
|
404
|
+
work.status = 'running';
|
|
405
|
+
work.resourceWait = false;
|
|
406
|
+
if (c.kind === 'group' && c.mode === 'host' && work.agentId === c.administratorId) {
|
|
407
|
+
work.requiredReply = true;
|
|
408
|
+
work.replyMode = 'automatic';
|
|
409
|
+
}
|
|
410
|
+
work.turnConfiguration = { mode: c.mode, administratorId: c.administratorId, members: this.store.taskMemberIds(owner, c, work.conversationTaskId).map(id => { const a = this.store.require(owner, 'agent', id); return { id, name: a.name }; }) };
|
|
411
|
+
this.saveWork(owner, work);
|
|
412
|
+
}
|
|
413
|
+
void this.executeWork(owner, work, recovering).catch(() => this.retrySoon());
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
catch {
|
|
417
|
+
this.retrySoon();
|
|
418
|
+
}
|
|
419
|
+
finally {
|
|
420
|
+
this.pumping = false;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
async executeWork(owner, work, recovering) {
|
|
424
|
+
try {
|
|
425
|
+
if (work.cancelRequested)
|
|
426
|
+
await this.interruptTurn(owner, work);
|
|
427
|
+
else {
|
|
428
|
+
const c = this.store.require(owner, 'conversation', work.conversationId);
|
|
429
|
+
const agent = this.store.require(owner, 'agent', work.agentId);
|
|
430
|
+
const message = await this.performTurn(owner, c, agent, work, recovering);
|
|
431
|
+
if (message.visible !== false)
|
|
432
|
+
await this.onMessage(owner, message).catch(() => { });
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
catch (error) {
|
|
436
|
+
if (this.closing)
|
|
437
|
+
return;
|
|
438
|
+
const current = this.getWork(owner, work.id);
|
|
439
|
+
if (!current.submitted && error instanceof HttpError && RESOURCE_WAIT_CODES.has(error.code ?? '') && current.status !== 'interrupted') {
|
|
440
|
+
current.status = 'queued';
|
|
441
|
+
current.resourceWait = true;
|
|
442
|
+
current.error = '等待执行节点或电脑资源';
|
|
443
|
+
this.saveWork(owner, current);
|
|
444
|
+
}
|
|
445
|
+
else if (!terminal(current.status) && current.status !== 'uncertain') {
|
|
446
|
+
current.status = current.submitted ? 'uncertain' : 'failed';
|
|
447
|
+
current.error = error instanceof Error ? error.message : '执行失败';
|
|
448
|
+
this.saveWork(owner, current);
|
|
449
|
+
}
|
|
450
|
+
if (current.status === 'failed' && !current.currentMessageId) {
|
|
451
|
+
current.currentMessageId = randomUUID();
|
|
452
|
+
this.store.saveMessage(owner, { id: current.currentMessageId, conversationId: current.conversationId, conversationTaskId: current.conversationTaskId, seq: 0, role: 'assistant', agentId: current.agentId, agentName: this.store.get(owner, 'agent', current.agentId)?.name, content: `执行失败:${current.error}`, reasoning: '', status: 'failed', error: current.error, taskId: current.id, runId: current.runId, attachments: [], tools: [], createdAt: Date.now() });
|
|
453
|
+
this.saveWork(owner, current);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
finally {
|
|
457
|
+
this.executing.delete(work.id);
|
|
458
|
+
if (!this.closing) {
|
|
459
|
+
const current = this.getWork(owner, work.id);
|
|
460
|
+
if (current.status === 'queued' && current.resourceWait) {
|
|
461
|
+
this.recoverAfter.set(work.id, Date.now() + 1000);
|
|
462
|
+
this.retrySoon();
|
|
463
|
+
}
|
|
464
|
+
else if (current.status === 'uncertain') {
|
|
465
|
+
const attempt = this.attempts.get(work.id) ?? 0;
|
|
466
|
+
this.attempts.set(work.id, attempt + 1);
|
|
467
|
+
this.recoverAfter.set(work.id, Date.now() + Math.min(30_000, 500 * 2 ** Math.min(attempt, 6)));
|
|
468
|
+
this.retrySoon();
|
|
469
|
+
}
|
|
470
|
+
else {
|
|
471
|
+
this.attempts.delete(work.id);
|
|
472
|
+
this.recoverAfter.delete(work.id);
|
|
473
|
+
}
|
|
474
|
+
this.wake();
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
async reconcile(owner, id) {
|
|
479
|
+
this.store.require(owner, 'run', id);
|
|
480
|
+
for (const w of this.works(owner, id))
|
|
481
|
+
this.recoverAfter.delete(w.id);
|
|
482
|
+
this.wake();
|
|
483
|
+
}
|
|
484
|
+
async stop(owner, id) {
|
|
485
|
+
const root = this.store.require(owner, 'run', id);
|
|
486
|
+
if (terminal(root.status))
|
|
487
|
+
return;
|
|
488
|
+
root.stopRequested = true;
|
|
489
|
+
this.store.saveRun(owner, root);
|
|
490
|
+
await this.cancelWorks(owner, this.works(owner, id));
|
|
491
|
+
}
|
|
492
|
+
async stopAgent(owner, conversationId, agentId, conversationTaskId) {
|
|
493
|
+
const c = this.store.require(owner, 'conversation', conversationId);
|
|
494
|
+
const helper = this.store.get(owner, 'agent', agentId);
|
|
495
|
+
const scopedTaskId = c.kind === 'group' ? this.store.resolveTask(owner, conversationId, conversationTaskId ?? helper?.temporaryGoalId).id : undefined;
|
|
496
|
+
if (!this.store.taskMemberIds(owner, c, scopedTaskId).includes(agentId))
|
|
497
|
+
throw new HttpError(400, '只能停止当前任务成员', 'invalid_members');
|
|
498
|
+
await this.cancelWorks(owner, this.works(owner).filter(w => w.conversationId === conversationId && w.agentId === agentId && w.conversationTaskId === scopedTaskId));
|
|
499
|
+
}
|
|
500
|
+
async stopTask(owner, conversationId, conversationTaskId) {
|
|
501
|
+
this.store.requireTask(owner, conversationId, conversationTaskId);
|
|
502
|
+
const roots = this.store.list(owner, 'run').filter(r => r.conversationId === conversationId && r.conversationTaskId === conversationTaskId && !terminal(r.status));
|
|
503
|
+
this.store.atomic(() => { for (const root of roots) {
|
|
504
|
+
root.stopRequested = true;
|
|
505
|
+
this.store.saveRun(owner, root);
|
|
506
|
+
} });
|
|
507
|
+
await this.cancelWorks(owner, this.works(owner).filter(w => w.conversationId === conversationId && w.conversationTaskId === conversationTaskId));
|
|
508
|
+
}
|
|
509
|
+
async stopConversation(owner, id) {
|
|
510
|
+
this.store.require(owner, 'conversation', id);
|
|
511
|
+
const roots = this.store.list(owner, 'run').filter(r => r.conversationId === id && !terminal(r.status));
|
|
512
|
+
this.store.atomic(() => { for (const root of roots) {
|
|
513
|
+
root.stopRequested = true;
|
|
514
|
+
this.store.saveRun(owner, root);
|
|
515
|
+
} });
|
|
516
|
+
await this.cancelWorks(owner, this.works(owner).filter(w => roots.some(r => r.id === w.runId)));
|
|
517
|
+
}
|
|
518
|
+
async cancelWorks(owner, works) {
|
|
519
|
+
for (const w of works) {
|
|
520
|
+
if (terminal(w.status))
|
|
521
|
+
continue;
|
|
522
|
+
const current = this.getWork(owner, w.id);
|
|
523
|
+
const wasQueued = current.status === 'queued';
|
|
524
|
+
current.cancelRequested = true;
|
|
525
|
+
current.status = 'interrupted';
|
|
526
|
+
current.error = '已停止';
|
|
527
|
+
current.planned = true;
|
|
528
|
+
current.cleanupPending = !wasQueued;
|
|
529
|
+
if (current.currentMessageId) {
|
|
530
|
+
const message = this.store.require(owner, 'message', current.currentMessageId);
|
|
531
|
+
message.status = 'interrupted';
|
|
532
|
+
message.error = '已停止';
|
|
533
|
+
this.store.atomic(() => {
|
|
534
|
+
this.store.saveMessage(owner, message);
|
|
535
|
+
this.saveWork(owner, current);
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
else
|
|
539
|
+
this.saveWork(owner, current);
|
|
540
|
+
this.resolveInteractions(owner, current.id);
|
|
541
|
+
if (current.cleanupPending)
|
|
542
|
+
this.scheduleInterruptCleanup(owner, current);
|
|
543
|
+
}
|
|
544
|
+
this.wake();
|
|
545
|
+
}
|
|
546
|
+
scheduleInterruptCleanup(owner, work) {
|
|
547
|
+
if (this.closing || this.interrupting.has(work.id))
|
|
548
|
+
return;
|
|
549
|
+
if ((this.recoverAfter.get(work.id) ?? 0) > Date.now()) {
|
|
550
|
+
this.retrySoon();
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
this.interrupting.add(work.id);
|
|
554
|
+
let succeeded = false;
|
|
555
|
+
void this.interruptTurn(owner, work)
|
|
556
|
+
.then(() => {
|
|
557
|
+
succeeded = true;
|
|
558
|
+
const current = this.getWork(owner, work.id);
|
|
559
|
+
current.cleanupPending = false;
|
|
560
|
+
current.cleanupError = undefined;
|
|
561
|
+
this.saveWork(owner, current);
|
|
562
|
+
})
|
|
563
|
+
.catch(error => {
|
|
564
|
+
if (this.closing)
|
|
565
|
+
return;
|
|
566
|
+
const current = this.getWork(owner, work.id);
|
|
567
|
+
current.cleanupPending = true;
|
|
568
|
+
current.cleanupError = error instanceof Error ? error.message : '连接中断';
|
|
569
|
+
this.saveWork(owner, current);
|
|
570
|
+
const attempt = this.attempts.get(work.id) ?? 0;
|
|
571
|
+
this.attempts.set(work.id, attempt + 1);
|
|
572
|
+
this.recoverAfter.set(work.id, Date.now() + Math.min(30_000, 500 * 2 ** Math.min(attempt, 6)));
|
|
573
|
+
this.retrySoon();
|
|
574
|
+
})
|
|
575
|
+
.finally(() => {
|
|
576
|
+
this.interrupting.delete(work.id);
|
|
577
|
+
if (succeeded) {
|
|
578
|
+
this.attempts.delete(work.id);
|
|
579
|
+
this.recoverAfter.delete(work.id);
|
|
580
|
+
}
|
|
581
|
+
this.wake();
|
|
582
|
+
})
|
|
583
|
+
.catch(() => this.retrySoon());
|
|
584
|
+
}
|
|
585
|
+
closeScheduler() {
|
|
586
|
+
this.closing = true;
|
|
587
|
+
if (this.retryTimer)
|
|
588
|
+
clearTimeout(this.retryTimer);
|
|
589
|
+
this.recoverAfter.clear();
|
|
590
|
+
this.attempts.clear();
|
|
591
|
+
}
|
|
592
|
+
}
|