@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,28 @@
|
|
|
1
|
+
/** Owner-scoped Bot endpoints. Keep native and administrative surfaces denied. */
|
|
2
|
+
export function isWorkspaceAccountPath(method, path) {
|
|
3
|
+
const read = method === 'GET' || method === 'HEAD';
|
|
4
|
+
if (read && /^\/api\/app\/workspace\/(snapshot|projects|memories|memory-export|memory-jobs|collaboration)$/.test(path))
|
|
5
|
+
return true;
|
|
6
|
+
if (read && /^\/api\/app\/workspace\/memories\/[^/]+\/revisions$/.test(path))
|
|
7
|
+
return true;
|
|
8
|
+
return method === 'POST' && /^\/api\/app\/workspace\/(projects|memories|memories\/forget|memory-jobs\/[^/]+\/retry|collaboration\/[^/]+\/stop)$/.test(path);
|
|
9
|
+
}
|
|
10
|
+
/** Bound memory as well as request frequency; untrusted forwarded IPs are not used. */
|
|
11
|
+
export class RegistrationLimiter {
|
|
12
|
+
attempts = new Map();
|
|
13
|
+
take(address, now = Date.now()) {
|
|
14
|
+
for (const [key, value] of this.attempts)
|
|
15
|
+
if (value.expires <= now)
|
|
16
|
+
this.attempts.delete(key);
|
|
17
|
+
let entry = this.attempts.get(address);
|
|
18
|
+
if (!entry) {
|
|
19
|
+
if (this.attempts.size >= 10_000)
|
|
20
|
+
return 60;
|
|
21
|
+
entry = { count: 0, expires: now + 15 * 60_000 };
|
|
22
|
+
this.attempts.set(address, entry);
|
|
23
|
+
}
|
|
24
|
+
if (++entry.count > 10)
|
|
25
|
+
return Math.ceil((entry.expires - now) / 1000);
|
|
26
|
+
return 0;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { HttpError } from './errors.js';
|
|
4
|
+
import { parse } from './workspaceStore.js';
|
|
5
|
+
const terminalRun = (status) => ['complete', 'failed', 'interrupted'].includes(status);
|
|
6
|
+
const terminalGoal = (status) => ['complete', 'blocked', 'cancelled'].includes(status);
|
|
7
|
+
const criteria = z.array(z.string().trim().min(1).max(1000)).max(12).default([]);
|
|
8
|
+
export const goalCriteriaInput = z.object({
|
|
9
|
+
requestId: z.string().uuid(),
|
|
10
|
+
expectedRevision: z.number().int().positive(),
|
|
11
|
+
acceptanceCriteria: z.array(z.string().trim().min(1).max(1000)).min(1).max(12),
|
|
12
|
+
}).strict();
|
|
13
|
+
export const assignmentInput = z.object({
|
|
14
|
+
requestId: z.string().uuid(), goalId: z.string().uuid(), agentId: z.string().uuid(),
|
|
15
|
+
title: z.string().trim().min(1).max(100), brief: z.string().trim().min(1).max(16000),
|
|
16
|
+
acceptanceCriteria: criteria, dependsOn: z.array(z.string().uuid()).max(12).default([]),
|
|
17
|
+
}).strict();
|
|
18
|
+
export const assignmentReview = z.object({
|
|
19
|
+
requestId: z.string().uuid(), assignmentId: z.string().uuid(),
|
|
20
|
+
decision: z.enum(['accept', 'retry', 'blocked']), review: z.string().trim().min(1).max(4000),
|
|
21
|
+
}).strict();
|
|
22
|
+
export const assignmentUpdate = z.object({
|
|
23
|
+
requestId: z.string().uuid(), assignmentId: z.string().uuid(),
|
|
24
|
+
title: z.string().trim().min(1).max(100).optional(), brief: z.string().trim().min(1).max(16000).optional(),
|
|
25
|
+
agentId: z.string().uuid().optional(),
|
|
26
|
+
acceptanceCriteria: criteria.optional(), dependsOn: z.array(z.string().uuid()).max(12).optional(),
|
|
27
|
+
}).strict();
|
|
28
|
+
export const assignmentCancel = z.object({ requestId: z.string().uuid(), assignmentId: z.string().uuid(), reason: z.string().trim().min(1).max(2000) }).strict();
|
|
29
|
+
export const finishGoalInput = z.object({
|
|
30
|
+
requestId: z.string().uuid(), goalId: z.string().uuid(), status: z.enum(['complete', 'blocked', 'waiting']),
|
|
31
|
+
result: z.string().trim().min(1).max(16000), artifactIds: z.array(z.string().uuid()).max(24).default([]),
|
|
32
|
+
acceptanceRevision: z.number().int().positive().optional(),
|
|
33
|
+
checks: z.array(z.object({ criterion: z.number().int().min(0).max(11), passed: z.boolean(), evidence: z.string().trim().min(1).max(2000) }).strict()).max(12).default([]),
|
|
34
|
+
}).strict();
|
|
35
|
+
export const resumeGoalInput = z.object({ requestId: z.string().uuid(), goalId: z.string().uuid() }).strict();
|
|
36
|
+
/** Stable internal command identity; retries can never create a second wake. */
|
|
37
|
+
function requestId(value) {
|
|
38
|
+
const h = createHash('sha256').update(value).digest('hex');
|
|
39
|
+
return `${h.slice(0, 8)}-${h.slice(8, 12)}-5${h.slice(13, 16)}-a${h.slice(17, 20)}-${h.slice(20, 32)}`;
|
|
40
|
+
}
|
|
41
|
+
export class WorkspaceTaskCoordinator {
|
|
42
|
+
store;
|
|
43
|
+
runtime;
|
|
44
|
+
cleaning = new Set();
|
|
45
|
+
cleanupAfter = new Map();
|
|
46
|
+
pending = false;
|
|
47
|
+
closed = false;
|
|
48
|
+
unobserve;
|
|
49
|
+
timer;
|
|
50
|
+
constructor(store, runtime) {
|
|
51
|
+
this.store = store;
|
|
52
|
+
this.runtime = runtime;
|
|
53
|
+
this.unobserve = store.observe((_owner, event) => {
|
|
54
|
+
if (['run.changed', 'agent.changed', 'conversation.changed', 'task.deleted', 'assignment.changed', 'goal.changed'].includes(event.type))
|
|
55
|
+
this.wake();
|
|
56
|
+
});
|
|
57
|
+
this.timer = setInterval(() => this.wake(), 5000);
|
|
58
|
+
this.timer.unref();
|
|
59
|
+
this.wake();
|
|
60
|
+
}
|
|
61
|
+
begin(owner, task, coordinator, objective, origin, acceptanceCriteria = []) {
|
|
62
|
+
const old = this.store.get(owner, 'goal', task.id);
|
|
63
|
+
if (old)
|
|
64
|
+
throw new HttpError(409, '这个任务已有目标,请继续原任务或新建任务', 'goal_exists');
|
|
65
|
+
const goal = { id: task.id, conversationId: task.conversationId, coordinatorId: coordinator.id,
|
|
66
|
+
authorityRevision: coordinator.teamAuthorizationVersion ?? 0, objective, acceptanceCriteria: acceptanceCriteria.length ? acceptanceCriteria : ['对照原始请求核验交付结果'], acceptanceRevision: 1,
|
|
67
|
+
authorizationVersion: this.runtime.authorizationVersion(owner), activation: 1,
|
|
68
|
+
status: 'running', origin, artifactIds: [], revision: 1, automaticWakes: 0, createdAt: Date.now(), updatedAt: Date.now() };
|
|
69
|
+
this.saveGoal(owner, goal);
|
|
70
|
+
return goal;
|
|
71
|
+
}
|
|
72
|
+
updateCriteria(owner, goalId, input, actorId) {
|
|
73
|
+
const body = parse(goalCriteriaInput, input);
|
|
74
|
+
return this.store.command(owner, body.requestId, { operation: 'goal.criteria', goalId, actorId: actorId ?? 'user', ...body }, () => {
|
|
75
|
+
const goal = actorId ? this.requireCoordinator(owner, goalId, actorId) : this.store.require(owner, 'goal', goalId);
|
|
76
|
+
if (!this.authorized(owner, goal))
|
|
77
|
+
throw new HttpError(403, '目标授权已失效', 'goal_forbidden');
|
|
78
|
+
if (!['running', 'review', 'waiting'].includes(goal.status))
|
|
79
|
+
throw new HttpError(409, '只能调整进行中的目标,请先继续任务', 'goal_not_editable');
|
|
80
|
+
if ((goal.acceptanceRevision ?? 1) !== body.expectedRevision)
|
|
81
|
+
throw new HttpError(409, '验收要求已更新,请重新查看后修改', 'goal_criteria_changed');
|
|
82
|
+
goal.acceptanceCriteria = body.acceptanceCriteria;
|
|
83
|
+
goal.acceptanceRevision = body.expectedRevision + 1;
|
|
84
|
+
goal.checks = undefined;
|
|
85
|
+
goal.revision++;
|
|
86
|
+
this.saveGoal(owner, goal);
|
|
87
|
+
return goal;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
saveGoal(owner, goal) {
|
|
91
|
+
goal.updatedAt = Date.now();
|
|
92
|
+
this.store.put(owner, 'goal', goal.id, goal);
|
|
93
|
+
const task = this.store.get(owner, 'conversation-task', goal.id);
|
|
94
|
+
if (task) {
|
|
95
|
+
task.goal = goal;
|
|
96
|
+
this.store.put(owner, 'conversation-task', task.id, task);
|
|
97
|
+
this.store.event(owner, 'task.changed', task, task.conversationId);
|
|
98
|
+
}
|
|
99
|
+
this.store.event(owner, 'goal.changed', goal, goal.conversationId);
|
|
100
|
+
}
|
|
101
|
+
saveAssignment(owner, assignment) {
|
|
102
|
+
assignment.updatedAt = Date.now();
|
|
103
|
+
this.store.put(owner, 'assignment', assignment.id, assignment);
|
|
104
|
+
this.store.event(owner, 'assignment.changed', assignment, assignment.conversationId);
|
|
105
|
+
}
|
|
106
|
+
assignments(owner, goalId) {
|
|
107
|
+
return this.store.list(owner, 'assignment').filter(a => a.goalId === goalId)
|
|
108
|
+
.sort((a, b) => a.createdAt - b.createdAt || a.id.localeCompare(b.id));
|
|
109
|
+
}
|
|
110
|
+
requireCoordinator(owner, goalId, actorId) {
|
|
111
|
+
const goal = this.store.require(owner, 'goal', goalId);
|
|
112
|
+
const team = this.store.require(owner, 'conversation', goal.conversationId);
|
|
113
|
+
if (!this.authorized(owner, goal) || goal.coordinatorId !== actorId || team.administratorId !== actorId)
|
|
114
|
+
throw new HttpError(403, '当前机器人无权管理这个任务', 'goal_forbidden');
|
|
115
|
+
return goal;
|
|
116
|
+
}
|
|
117
|
+
authorized(owner, goal) {
|
|
118
|
+
try {
|
|
119
|
+
const agent = this.store.require(owner, 'agent', goal.coordinatorId);
|
|
120
|
+
const team = this.store.require(owner, 'conversation', goal.conversationId);
|
|
121
|
+
this.store.requireTask(owner, team.id, goal.id);
|
|
122
|
+
this.runtime.nodes.requireSource(owner, agent);
|
|
123
|
+
return this.runtime.userActive(owner) && !agent.archived && !agent.temporaryGoalId
|
|
124
|
+
&& (goal.authorizationVersion === undefined || goal.authorizationVersion === this.runtime.authorizationVersion(owner))
|
|
125
|
+
&& (agent.teamAuthorizationVersion ?? 0) === goal.authorityRevision
|
|
126
|
+
&& !team.archived && team.administratorId === agent.id && team.memberIds.includes(agent.id);
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
createAssignment(owner, actorId, input, sourceRunId) {
|
|
133
|
+
const { requestId: command, ...body } = parse(assignmentInput, input);
|
|
134
|
+
const goal = this.requireCoordinator(owner, body.goalId, actorId);
|
|
135
|
+
if (terminalGoal(goal.status))
|
|
136
|
+
throw new HttpError(409, '目标已结束', 'goal_terminal');
|
|
137
|
+
const team = this.store.require(owner, 'conversation', goal.conversationId);
|
|
138
|
+
const agent = this.store.require(owner, 'agent', body.agentId);
|
|
139
|
+
this.runtime.nodes.requireSource(owner, agent);
|
|
140
|
+
if (!this.store.taskMemberIds(owner, team, goal.id).includes(agent.id) || agent.archived || agent.id === actorId)
|
|
141
|
+
throw new HttpError(400, '请选择其他有效团队成员;管理员可以直接处理自己的工作。', 'invalid_assignment_agent');
|
|
142
|
+
const previous = this.assignments(owner, goal.id);
|
|
143
|
+
if (body.dependsOn.some(id => !previous.some(a => a.id === id)))
|
|
144
|
+
throw new HttpError(400, '依赖必须属于同一目标中的已有子任务', 'invalid_assignment_dependency');
|
|
145
|
+
return this.store.command(owner, command, { actorId, operation: 'assignment.create', ...body }, () => {
|
|
146
|
+
if (previous.length >= 32)
|
|
147
|
+
throw new HttpError(409, '每个目标最多 32 个子任务', 'assignment_limit');
|
|
148
|
+
const assignment = { ...body, id: randomUUID(), conversationId: goal.conversationId,
|
|
149
|
+
status: 'pending', sourceRunId: sourceRunId ?? goal.origin.runId, attempt: 0, artifactIds: [], createdAt: Date.now(), updatedAt: Date.now() };
|
|
150
|
+
this.saveAssignment(owner, assignment);
|
|
151
|
+
if (goal.status !== 'waiting')
|
|
152
|
+
goal.status = 'running';
|
|
153
|
+
goal.revision++;
|
|
154
|
+
this.saveGoal(owner, goal);
|
|
155
|
+
return assignment;
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
reviewAssignment(owner, actorId, input, sourceRunId) {
|
|
159
|
+
const body = parse(assignmentReview, input);
|
|
160
|
+
const assignment = this.store.require(owner, 'assignment', body.assignmentId);
|
|
161
|
+
const goal = this.requireCoordinator(owner, assignment.goalId, actorId);
|
|
162
|
+
return this.store.command(owner, body.requestId, { actorId, operation: 'assignment.review', ...body }, () => {
|
|
163
|
+
if (terminalGoal(goal.status) || !['review', 'failed', 'blocked'].includes(assignment.status))
|
|
164
|
+
throw new HttpError(409, '子任务尚未进入复核状态', 'assignment_not_reviewable');
|
|
165
|
+
if (body.decision === 'accept' && assignment.status !== 'review')
|
|
166
|
+
throw new HttpError(409, '失败的执行不能直接验收为完成', 'assignment_failed');
|
|
167
|
+
if (body.decision === 'retry' && assignment.attempt >= (assignment.maxAttempts ?? 3))
|
|
168
|
+
throw new HttpError(409, '此子任务已达到 3 次执行上限,请调整方案', 'assignment_retry_limit');
|
|
169
|
+
assignment.review = body.review;
|
|
170
|
+
assignment.status = body.decision === 'accept' ? 'complete' : body.decision === 'retry' ? 'pending' : 'blocked';
|
|
171
|
+
if (body.decision === 'retry') {
|
|
172
|
+
assignment.runId = undefined;
|
|
173
|
+
assignment.result = undefined;
|
|
174
|
+
if (sourceRunId)
|
|
175
|
+
assignment.sourceRunId = sourceRunId;
|
|
176
|
+
}
|
|
177
|
+
this.saveAssignment(owner, assignment);
|
|
178
|
+
if (goal.status !== 'waiting')
|
|
179
|
+
goal.status = 'running';
|
|
180
|
+
goal.revision++;
|
|
181
|
+
this.saveGoal(owner, goal);
|
|
182
|
+
return assignment;
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
updateAssignment(owner, actorId, input, sourceRunId) {
|
|
186
|
+
const { requestId: command, assignmentId, ...patch } = parse(assignmentUpdate, input);
|
|
187
|
+
const assignment = this.store.require(owner, 'assignment', assignmentId);
|
|
188
|
+
const goal = this.requireCoordinator(owner, assignment.goalId, actorId);
|
|
189
|
+
return this.store.command(owner, command, { actorId, operation: 'assignment.update', assignmentId, ...patch }, () => {
|
|
190
|
+
if (terminalGoal(goal.status) || !['pending', 'failed', 'blocked'].includes(assignment.status))
|
|
191
|
+
throw new HttpError(409, '只能调整尚未运行或已失败的子任务', 'assignment_not_editable');
|
|
192
|
+
const all = this.assignments(owner, goal.id), dependencies = patch.dependsOn ?? assignment.dependsOn;
|
|
193
|
+
if (dependencies.some(id => id === assignment.id || !all.some(a => a.id === id)))
|
|
194
|
+
throw new HttpError(400, '子任务依赖无效', 'invalid_assignment_dependency');
|
|
195
|
+
const reaches = (id, seen = new Set()) => {
|
|
196
|
+
if (id === assignment.id)
|
|
197
|
+
return true;
|
|
198
|
+
if (seen.has(id))
|
|
199
|
+
return false;
|
|
200
|
+
seen.add(id);
|
|
201
|
+
return (all.find(a => a.id === id)?.dependsOn ?? []).some(next => reaches(next, seen));
|
|
202
|
+
};
|
|
203
|
+
if (dependencies.some(id => reaches(id)))
|
|
204
|
+
throw new HttpError(400, '子任务依赖不能形成循环', 'assignment_dependency_cycle');
|
|
205
|
+
if (patch.agentId && patch.agentId !== assignment.agentId) {
|
|
206
|
+
const target = this.store.require(owner, 'agent', patch.agentId);
|
|
207
|
+
const team = this.store.require(owner, 'conversation', goal.conversationId);
|
|
208
|
+
this.runtime.nodes.requireSource(owner, target);
|
|
209
|
+
if (target.archived || target.id === actorId || !this.store.taskMemberIds(owner, team, goal.id).includes(target.id))
|
|
210
|
+
throw new HttpError(400, '请选择有效团队成员', 'invalid_assignment_agent');
|
|
211
|
+
assignment.status = 'pending';
|
|
212
|
+
assignment.runId = undefined;
|
|
213
|
+
assignment.result = undefined;
|
|
214
|
+
}
|
|
215
|
+
Object.assign(assignment, patch);
|
|
216
|
+
if (sourceRunId)
|
|
217
|
+
assignment.sourceRunId = sourceRunId;
|
|
218
|
+
this.saveAssignment(owner, assignment);
|
|
219
|
+
goal.revision++;
|
|
220
|
+
this.saveGoal(owner, goal);
|
|
221
|
+
return assignment;
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
cancelAssignment(owner, actorId, input) {
|
|
225
|
+
const body = parse(assignmentCancel, input), assignment = this.store.require(owner, 'assignment', body.assignmentId);
|
|
226
|
+
const goal = this.requireCoordinator(owner, assignment.goalId, actorId);
|
|
227
|
+
return this.store.command(owner, body.requestId, { actorId, operation: 'assignment.cancel', ...body }, () => {
|
|
228
|
+
if (terminalGoal(goal.status) || assignment.status === 'complete')
|
|
229
|
+
throw new HttpError(409, '已完成的工作不能通过取消操作撤销', 'assignment_not_cancellable');
|
|
230
|
+
const run = assignment.runId ? this.store.get(owner, 'run', assignment.runId) : undefined;
|
|
231
|
+
assignment.review = body.reason;
|
|
232
|
+
assignment.status = run && !terminalRun(run.status) ? 'cancelling' : 'cancelled';
|
|
233
|
+
this.saveAssignment(owner, assignment);
|
|
234
|
+
return assignment;
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
finish(owner, actorId, input, reportingRunId) {
|
|
238
|
+
const body = parse(finishGoalInput, input), goal = this.requireCoordinator(owner, body.goalId, actorId);
|
|
239
|
+
return this.store.command(owner, body.requestId, { actorId, operation: 'goal.finish', ...body }, () => {
|
|
240
|
+
if (terminalGoal(goal.status))
|
|
241
|
+
throw new HttpError(409, '目标已结束', 'goal_terminal');
|
|
242
|
+
if (body.status === 'complete' && (body.acceptanceRevision ?? 1) !== (goal.acceptanceRevision ?? 1))
|
|
243
|
+
throw new HttpError(409, '验收要求已修改,请按最新要求重新核验', 'goal_criteria_changed');
|
|
244
|
+
const assignments = this.assignments(owner, goal.id);
|
|
245
|
+
if (body.status === 'complete' && (assignments.some(a => !['complete', 'cancelled'].includes(a.status)) || (assignments.length > 0 && !assignments.some(a => a.status === 'complete'))))
|
|
246
|
+
throw new HttpError(409, '还有未验收的子任务,不能宣布目标完成', 'goal_incomplete');
|
|
247
|
+
if (body.status === 'complete' && goal.acceptanceCriteria.some((_criterion, index) => !body.checks.some(check => check.criterion === index && check.passed && check.evidence.trim())))
|
|
248
|
+
throw new HttpError(409, '需要逐项提供验收依据,才能宣布目标完成', 'goal_checks_missing');
|
|
249
|
+
if (body.status === 'complete' && this.store.list(owner, 'run').some(r => r.conversationId === goal.conversationId && r.conversationTaskId === goal.id && r.id !== reportingRunId && !terminalRun(r.status)))
|
|
250
|
+
throw new HttpError(409, '任务仍有运行中的执行,请先等待或停止', 'goal_still_running');
|
|
251
|
+
for (const id of body.artifactIds) {
|
|
252
|
+
const file = this.store.require(owner, 'file', id);
|
|
253
|
+
if (file.conversationId && file.conversationId !== goal.conversationId)
|
|
254
|
+
throw new HttpError(403, '产物不属于这个团队', 'goal_artifact_forbidden');
|
|
255
|
+
}
|
|
256
|
+
goal.status = body.status;
|
|
257
|
+
goal.result = body.result;
|
|
258
|
+
goal.artifactIds = body.artifactIds;
|
|
259
|
+
goal.revision++;
|
|
260
|
+
goal.reportingRunId = reportingRunId;
|
|
261
|
+
goal.checks = body.checks;
|
|
262
|
+
this.saveGoal(owner, goal);
|
|
263
|
+
return goal;
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
resume(owner, actorId, input, origin) {
|
|
267
|
+
const body = parse(resumeGoalInput, input), goal = this.store.require(owner, 'goal', body.goalId);
|
|
268
|
+
const agent = this.store.require(owner, 'agent', actorId), team = this.store.require(owner, 'conversation', goal.conversationId);
|
|
269
|
+
this.runtime.nodes.requireSource(owner, agent);
|
|
270
|
+
const source = this.store.require(owner, 'run', origin.runId);
|
|
271
|
+
const message = this.store.require(owner, 'message', source.messageId);
|
|
272
|
+
if (!this.runtime.userActive(owner) || agent.temporaryGoalId || agent.archived || team.archived || goal.coordinatorId !== actorId || team.administratorId !== actorId || source.triggerKind || message.role !== 'user')
|
|
273
|
+
throw new HttpError(403, '恢复任务需要当前用户的新指令和有效管理员权限', 'goal_resume_forbidden');
|
|
274
|
+
return this.store.command(owner, body.requestId, { actorId, operation: 'goal.resume', goalId: goal.id, origin }, () => {
|
|
275
|
+
if (goal.status === 'cancelling')
|
|
276
|
+
throw new HttpError(409, '旧执行尚未确认结束,请先核对状态', 'goal_still_running');
|
|
277
|
+
if (!['blocked', 'waiting', 'cancelled'].includes(goal.status))
|
|
278
|
+
throw new HttpError(409, '只能恢复受阻、等待或已停止的目标', 'goal_not_resumable');
|
|
279
|
+
if (this.store.list(owner, 'run').some(r => r.conversationId === goal.conversationId && r.conversationTaskId === goal.id && r.id !== source.id && !terminalRun(r.status)))
|
|
280
|
+
throw new HttpError(409, '旧执行尚未确认结束,请先核对状态', 'goal_still_running');
|
|
281
|
+
for (const assignment of this.assignments(owner, goal.id)) {
|
|
282
|
+
assignment.maxAttempts = assignment.attempt + 3;
|
|
283
|
+
if (assignment.status === 'cancelled') {
|
|
284
|
+
assignment.status = 'pending';
|
|
285
|
+
assignment.runId = undefined;
|
|
286
|
+
}
|
|
287
|
+
this.saveAssignment(owner, assignment);
|
|
288
|
+
}
|
|
289
|
+
Object.assign(goal, { status: 'running', origin, authorityRevision: agent.teamAuthorizationVersion ?? 0,
|
|
290
|
+
authorizationVersion: this.runtime.authorizationVersion(owner), activation: (goal.activation ?? 0) + 1,
|
|
291
|
+
automaticWakes: 0, reviewRunId: source.id, lastReviewedBatch: undefined, reportingRunId: undefined, result: undefined, checks: undefined });
|
|
292
|
+
goal.revision++;
|
|
293
|
+
this.saveGoal(owner, goal);
|
|
294
|
+
return goal;
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
cancel(owner, goalId, reason = '用户已停止任务') {
|
|
298
|
+
const goal = this.store.get(owner, 'goal', goalId);
|
|
299
|
+
if (!goal || terminalGoal(goal.status) || goal.status === 'cancelling')
|
|
300
|
+
return;
|
|
301
|
+
this.store.atomic(() => {
|
|
302
|
+
goal.status = 'cancelling';
|
|
303
|
+
goal.result = reason;
|
|
304
|
+
goal.revision++;
|
|
305
|
+
for (const assignment of this.assignments(owner, goal.id)) {
|
|
306
|
+
if (!['complete', 'cancelled'].includes(assignment.status)) {
|
|
307
|
+
const run = assignment.runId ? this.store.get(owner, 'run', assignment.runId) : undefined;
|
|
308
|
+
assignment.status = run && !terminalRun(run.status) ? 'cancelling' : 'cancelled';
|
|
309
|
+
this.saveAssignment(owner, assignment);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
this.saveGoal(owner, goal);
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
async cancelOrigin(owner, conversationId, taskId, visited = new Set()) {
|
|
316
|
+
const pending = [];
|
|
317
|
+
for (const goal of this.store.list(owner, 'goal')) {
|
|
318
|
+
if (terminalGoal(goal.status))
|
|
319
|
+
continue;
|
|
320
|
+
if (goal.origin.conversationId === conversationId && (!taskId || goal.origin.conversationTaskId === taskId)) {
|
|
321
|
+
this.cancel(owner, goal.id);
|
|
322
|
+
pending.push(this.runtime.stopTask(owner, goal.conversationId, goal.id, visited));
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
await Promise.all(pending);
|
|
326
|
+
}
|
|
327
|
+
wake() {
|
|
328
|
+
if (this.closed || this.pending)
|
|
329
|
+
return;
|
|
330
|
+
this.pending = true;
|
|
331
|
+
queueMicrotask(() => {
|
|
332
|
+
this.pending = false;
|
|
333
|
+
if (this.closed)
|
|
334
|
+
return;
|
|
335
|
+
for (const owner of this.store.owners()) {
|
|
336
|
+
for (const goal of this.store.list(owner, 'goal')) {
|
|
337
|
+
try {
|
|
338
|
+
this.reconcile(owner, goal);
|
|
339
|
+
this.retireHelpers(owner, goal);
|
|
340
|
+
}
|
|
341
|
+
catch { /* The next committed event retries durable state. */ }
|
|
342
|
+
}
|
|
343
|
+
for (const goalId of new Set(this.store.list(owner, 'agent').flatMap(agent => agent.temporaryGoalId ? [agent.temporaryGoalId] : [])))
|
|
344
|
+
if (!this.store.get(owner, 'goal', goalId) || !this.store.get(owner, 'conversation-task', goalId))
|
|
345
|
+
this.retireHelpers(owner, { id: goalId, status: 'cancelled', activation: 0 });
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
retireHelpers(owner, goal) {
|
|
350
|
+
for (const helper of this.store.list(owner, 'agent').filter(agent => agent.temporaryGoalId === goal.id)) {
|
|
351
|
+
if (!helper.archived && !terminalGoal(goal.status) && helper.helperActivation === (goal.activation ?? 1))
|
|
352
|
+
continue;
|
|
353
|
+
if (!helper.archived) {
|
|
354
|
+
helper.archived = true;
|
|
355
|
+
helper.revision++;
|
|
356
|
+
helper.updatedAt = Date.now();
|
|
357
|
+
helper.retiredAt = Date.now();
|
|
358
|
+
helper.cleanupState = 'pending';
|
|
359
|
+
this.store.put(owner, 'agent', helper.id, helper);
|
|
360
|
+
this.store.event(owner, 'agent.changed', helper);
|
|
361
|
+
}
|
|
362
|
+
if (!helper.cleanupState) {
|
|
363
|
+
helper.cleanupState = 'pending';
|
|
364
|
+
helper.retiredAt ??= Date.now();
|
|
365
|
+
this.store.put(owner, 'agent', helper.id, helper);
|
|
366
|
+
}
|
|
367
|
+
const works = this.store.list(owner, 'turn').filter(work => work.agentId === helper.id && !terminalRun(work.status));
|
|
368
|
+
if (works.length) {
|
|
369
|
+
for (const runId of new Set(works.map(work => work.runId)))
|
|
370
|
+
void this.runtime.stop(owner, runId).catch(() => { });
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
if (helper.cleanupState === 'complete')
|
|
374
|
+
continue;
|
|
375
|
+
const key = `${owner}:${helper.id}`;
|
|
376
|
+
if (this.cleaning.has(key) || (this.cleanupAfter.get(key) ?? 0) > Date.now())
|
|
377
|
+
continue;
|
|
378
|
+
this.cleaning.add(key);
|
|
379
|
+
void this.runtime.retireHelper(owner, helper).then(() => {
|
|
380
|
+
if (this.closed)
|
|
381
|
+
return;
|
|
382
|
+
const current = this.store.require(owner, 'agent', helper.id);
|
|
383
|
+
current.cleanupState = 'complete';
|
|
384
|
+
current.retiredAt ??= Date.now();
|
|
385
|
+
this.store.put(owner, 'agent', current.id, current);
|
|
386
|
+
this.store.event(owner, 'agent.changed', current);
|
|
387
|
+
}).catch(() => { this.cleanupAfter.set(key, Date.now() + 5000); }).finally(() => this.cleaning.delete(key));
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
reconcile(owner, goal) {
|
|
391
|
+
if (goal.status === 'cancelling') {
|
|
392
|
+
const all = this.store.list(owner, 'goal'), owned = new Set([goal.id]);
|
|
393
|
+
for (let changed = true; changed;) {
|
|
394
|
+
changed = false;
|
|
395
|
+
for (const child of all)
|
|
396
|
+
if (child.origin.conversationTaskId && owned.has(child.origin.conversationTaskId) && !owned.has(child.id)) {
|
|
397
|
+
owned.add(child.id);
|
|
398
|
+
changed = true;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
const active = this.store.list(owner, 'run').filter(r => r.conversationTaskId && owned.has(r.conversationTaskId) && !terminalRun(r.status));
|
|
402
|
+
if (!active.length) {
|
|
403
|
+
for (const assignment of this.assignments(owner, goal.id))
|
|
404
|
+
if (assignment.status === 'cancelling') {
|
|
405
|
+
assignment.status = 'cancelled';
|
|
406
|
+
this.saveAssignment(owner, assignment);
|
|
407
|
+
}
|
|
408
|
+
goal.status = 'cancelled';
|
|
409
|
+
goal.revision++;
|
|
410
|
+
this.saveGoal(owner, goal);
|
|
411
|
+
}
|
|
412
|
+
else if (active.some(r => !r.stopRequested))
|
|
413
|
+
void this.runtime.stopTask(owner, goal.conversationId, goal.id).catch(() => { });
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
if (!this.authorized(owner, goal) && !terminalGoal(goal.status)) {
|
|
417
|
+
this.cancel(owner, goal.id, '任务授权已失效');
|
|
418
|
+
void this.runtime.stopTask(owner, goal.conversationId, goal.id).catch(() => { });
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
if (goal.status === 'cancelled')
|
|
422
|
+
return;
|
|
423
|
+
if (goal.status === 'blocked') {
|
|
424
|
+
for (const assignment of this.assignments(owner, goal.id))
|
|
425
|
+
if (['pending', 'running', 'cancelling'].includes(assignment.status)) {
|
|
426
|
+
const run = assignment.runId ? this.store.get(owner, 'run', assignment.runId) : undefined;
|
|
427
|
+
const next = run && !terminalRun(run.status) ? 'cancelling' : 'cancelled';
|
|
428
|
+
if (assignment.status !== next) {
|
|
429
|
+
assignment.status = next;
|
|
430
|
+
this.saveAssignment(owner, assignment);
|
|
431
|
+
}
|
|
432
|
+
if (run && !terminalRun(run.status) && !run.stopRequested)
|
|
433
|
+
void this.runtime.stop(owner, run.id).catch(() => { });
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
if (terminalGoal(goal.status) || goal.status === 'waiting') {
|
|
437
|
+
this.deliver(owner, goal, 'terminal', String(goal.revision));
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
const assignments = this.assignments(owner, goal.id);
|
|
441
|
+
for (const assignment of assignments) {
|
|
442
|
+
if (assignment.status === 'cancelling') {
|
|
443
|
+
const run = assignment.runId ? this.store.get(owner, 'run', assignment.runId) : undefined;
|
|
444
|
+
if (!run || terminalRun(run.status)) {
|
|
445
|
+
assignment.status = 'cancelled';
|
|
446
|
+
this.saveAssignment(owner, assignment);
|
|
447
|
+
}
|
|
448
|
+
else
|
|
449
|
+
void this.runtime.stop(owner, run.id).catch(() => { });
|
|
450
|
+
continue;
|
|
451
|
+
}
|
|
452
|
+
if (assignment.status === 'running' && assignment.runId) {
|
|
453
|
+
const run = this.store.get(owner, 'run', assignment.runId);
|
|
454
|
+
if (run && terminalRun(run.status)) {
|
|
455
|
+
const messages = this.store.messages(owner, goal.conversationId, Number.MAX_SAFE_INTEGER, 100, false, goal.id)
|
|
456
|
+
.filter(m => m.runId === run.id && m.role === 'assistant');
|
|
457
|
+
assignment.result = messages.map(m => m.content).join('\n\n').slice(-16000) || run.error || '未产生有效结果';
|
|
458
|
+
assignment.artifactIds = [...new Set(messages.flatMap(m => m.attachments.map(f => f.id)))];
|
|
459
|
+
assignment.status = run.status === 'complete' && messages.some(m => m.content.trim() || m.attachments.length) ? 'review' : 'failed';
|
|
460
|
+
this.saveAssignment(owner, assignment);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
if (assignment.status === 'pending') {
|
|
464
|
+
const dependencies = assignments.filter(a => assignment.dependsOn.includes(a.id));
|
|
465
|
+
if (dependencies.some(a => ['failed', 'blocked', 'cancelled'].includes(a.status)))
|
|
466
|
+
continue;
|
|
467
|
+
if (dependencies.some(a => a.status !== 'complete'))
|
|
468
|
+
continue;
|
|
469
|
+
const target = this.store.get(owner, 'agent', assignment.agentId);
|
|
470
|
+
const team = this.store.require(owner, 'conversation', goal.conversationId);
|
|
471
|
+
if (!target || target.archived || !this.store.taskMemberIds(owner, team, goal.id).includes(target.id)) {
|
|
472
|
+
assignment.status = 'blocked';
|
|
473
|
+
assignment.result = '负责成员已不可用';
|
|
474
|
+
this.saveAssignment(owner, assignment);
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
477
|
+
try {
|
|
478
|
+
this.runtime.nodes.requireSource(owner, target);
|
|
479
|
+
}
|
|
480
|
+
catch {
|
|
481
|
+
assignment.status = 'blocked';
|
|
482
|
+
assignment.result = '负责成员的基础 Profile 已撤权';
|
|
483
|
+
this.saveAssignment(owner, assignment);
|
|
484
|
+
continue;
|
|
485
|
+
}
|
|
486
|
+
this.store.atomic(() => {
|
|
487
|
+
const content = `由管理员分派的子任务:${assignment.title}\n${assignment.brief}\n验收要求:${assignment.acceptanceCriteria.join(';') || '提供实际结果及依据'}\n${assignment.review ? `上次复核意见:${assignment.review}\n` : ''}${dependencies.map(a => `依赖结果 ${a.title}:${a.result?.slice(0, 1500)}`).join('\n')}\n完成后提交结果,由管理员复核,不要自行转交。`;
|
|
488
|
+
const originRun = this.store.get(owner, 'run', goal.origin.runId);
|
|
489
|
+
const assignmentSource = this.store.get(owner, 'run', assignment.sourceRunId ?? goal.origin.runId);
|
|
490
|
+
const inputFiles = originRun ? this.store.get(owner, 'message', originRun.messageId)?.attachments.map(file => file.id) ?? [] : [];
|
|
491
|
+
const fileIds = [...new Set([...inputFiles, ...dependencies.flatMap(item => item.artifactIds ?? [])])].slice(0, 8);
|
|
492
|
+
const run = this.runtime.dispatch(owner, goal.conversationId, { requestId: requestId(`assignment:${assignment.id}:${assignment.attempt}`), taskId: goal.id, content, fileIds }, { agentId: goal.coordinatorId, assignmentId: assignment.id, targetAgentId: assignment.agentId, kind: 'assignment', deviceHost: assignmentSource?.deviceHost });
|
|
493
|
+
assignment.runId = run.id;
|
|
494
|
+
assignment.attempt++;
|
|
495
|
+
assignment.status = 'running';
|
|
496
|
+
this.saveAssignment(owner, assignment);
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
const goalRuns = this.store.list(owner, 'run').filter(r => r.conversationId === goal.conversationId && r.conversationTaskId === goal.id);
|
|
501
|
+
if (!assignments.length && !goalRuns.length)
|
|
502
|
+
return;
|
|
503
|
+
const activeRuns = goalRuns.some(r => !terminalRun(r.status));
|
|
504
|
+
if (activeRuns)
|
|
505
|
+
return;
|
|
506
|
+
const reviewing = goal.reviewRunId && this.store.get(owner, 'run', goal.reviewRunId);
|
|
507
|
+
if (reviewing && !terminalRun(reviewing.status))
|
|
508
|
+
return;
|
|
509
|
+
const assignmentBatch = assignments.map(a => `${a.id}:${a.status}:${a.attempt}`).join('|') || 'initial';
|
|
510
|
+
// A user's changed requirements need a fresh review even when assignments are unchanged.
|
|
511
|
+
const batch = assignmentBatch + ((goal.acceptanceRevision ?? 1) > 1 ? `:criteria:${goal.acceptanceRevision}` : '');
|
|
512
|
+
if (goal.lastReviewedBatch === batch) {
|
|
513
|
+
if (goal.status === 'review')
|
|
514
|
+
this.deliver(owner, goal, 'review', batch);
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
if (!assignments.length || assignments.some(a => ['review', 'failed', 'blocked'].includes(a.status)) || assignments.every(a => ['complete', 'cancelled'].includes(a.status))) {
|
|
518
|
+
goal.status = 'review';
|
|
519
|
+
goal.lastReviewedBatch = batch;
|
|
520
|
+
goal.revision++;
|
|
521
|
+
this.saveGoal(owner, goal);
|
|
522
|
+
this.deliver(owner, goal, 'review', batch);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
deliver(owner, goal, kind, batch) {
|
|
526
|
+
const id = requestId(`delivery:${goal.id}:${goal.activation ?? 0}:${kind}:${batch}`);
|
|
527
|
+
let delivery = this.store.get(owner, 'task-delivery', id);
|
|
528
|
+
if (delivery?.status === 'delivered' || delivery?.status === 'suppressed')
|
|
529
|
+
return;
|
|
530
|
+
const source = this.store.get(owner, 'conversation', goal.origin.conversationId);
|
|
531
|
+
const sourceRun = this.store.get(owner, 'run', goal.origin.runId);
|
|
532
|
+
const canWake = this.authorized(owner, goal) && source && !source.archived && source.memberIds.includes(goal.origin.agentId)
|
|
533
|
+
&& sourceRun && !sourceRun.stopRequested && sourceRun.status !== 'interrupted'
|
|
534
|
+
&& (!goal.origin.conversationTaskId || this.store.get(owner, 'conversation-task', goal.origin.conversationTaskId));
|
|
535
|
+
delivery ??= { id, goalId: goal.id, kind, status: 'pending', targetConversationId: goal.origin.conversationId,
|
|
536
|
+
targetTaskId: goal.origin.conversationTaskId, createdAt: Date.now() };
|
|
537
|
+
if (kind === 'terminal' && goal.reportingRunId) {
|
|
538
|
+
const reported = this.store.get(owner, 'run', goal.reportingRunId);
|
|
539
|
+
if (reported?.conversationId === goal.origin.conversationId && reported.conversationTaskId === goal.origin.conversationTaskId) {
|
|
540
|
+
if (!terminalRun(reported.status))
|
|
541
|
+
return;
|
|
542
|
+
if (reported.status === 'complete' && this.store.messages(owner, reported.conversationId, Number.MAX_SAFE_INTEGER, 20, false, reported.conversationTaskId)
|
|
543
|
+
.some(m => m.runId === reported.id && m.role === 'assistant' && m.content.trim())) {
|
|
544
|
+
delivery.status = 'delivered';
|
|
545
|
+
delivery.runId = reported.id;
|
|
546
|
+
this.store.put(owner, 'task-delivery', id, delivery);
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
if (!canWake || goal.automaticWakes >= 8) {
|
|
552
|
+
delivery.status = 'suppressed';
|
|
553
|
+
this.store.put(owner, 'task-delivery', id, delivery);
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
const assignments = this.assignments(owner, goal.id);
|
|
557
|
+
const taskTitle = this.store.get(owner, 'conversation-task', goal.id)?.title || goal.objective.slice(0, 100);
|
|
558
|
+
const teamName = this.store.get(owner, 'conversation', goal.conversationId)?.name || '团队';
|
|
559
|
+
const statusLabel = { complete: '已完成', blocked: '遇到阻碍', waiting: '需要补充信息', review: '待复核', running: '进行中', cancelling: '正在停止', cancelled: '已停止' }[goal.status];
|
|
560
|
+
const text = kind === 'terminal'
|
|
561
|
+
? `「${teamName}」的任务「${taskTitle}」${statusLabel}。\n${goal.result || ''}\n[打开任务](/conversations/${goal.conversationId}?taskId=${goal.id})`
|
|
562
|
+
: `「${teamName}」的任务「${taskTitle}」已有执行结果,等待管理员复核。\n[打开任务](/conversations/${goal.conversationId}?taskId=${goal.id})`;
|
|
563
|
+
const instruction = `本条来自系统任务投递。团队 ID:${goal.conversationId},目标/任务 ID:${goal.id}。目标:${goal.objective.slice(0, 4000)}\n${assignments.slice(-12).map(a => `${a.title}(${a.id}):${a.status}\n${a.result?.slice(0, 1000) || ''}`).join('\n\n')}\n${kind === 'terminal' ? '向用户汇报实际结果,不要重复启动相同任务。' : '请使用 workspace_get_team_task 读取完整状态,使用 workspace_review_assignment 复核成员结果,必要时调整方案;满足验收条件后使用 workspace_finish_team_task 记录完成。'}`;
|
|
564
|
+
this.store.atomic(() => {
|
|
565
|
+
this.store.put(owner, 'task-delivery', id, delivery);
|
|
566
|
+
const run = this.runtime.dispatch(owner, source.id, { requestId: id, taskId: goal.origin.conversationTaskId, content: text, fileIds: [...new Set(assignments.flatMap(assignment => assignment.artifactIds ?? []))].slice(0, 8) }, { agentId: goal.coordinatorId, kind: kind === 'review' ? 'task_review' : 'task_result', instruction, deviceHost: sourceRun?.deviceHost,
|
|
567
|
+
taskReference: { conversationId: goal.conversationId, taskId: goal.id } });
|
|
568
|
+
delivery.runId = run.id;
|
|
569
|
+
delivery.status = 'delivered';
|
|
570
|
+
this.store.put(owner, 'task-delivery', id, delivery);
|
|
571
|
+
goal.automaticWakes++;
|
|
572
|
+
this.saveGoal(owner, goal);
|
|
573
|
+
if (kind === 'review') {
|
|
574
|
+
goal.reviewRunId = run.id;
|
|
575
|
+
this.saveGoal(owner, goal);
|
|
576
|
+
}
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
close() { this.closed = true; clearInterval(this.timer); this.unobserve(); }
|
|
580
|
+
}
|