@evandu/super-pi-server 0.0.0-stage → 0.1.260
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +250 -3
- package/agents/code-auditor.md +64 -0
- package/agents/code-designer.md +64 -0
- package/agents/coder.md +24 -0
- package/agents/critic.md +24 -0
- package/agents/document-organizer.md +48 -0
- package/agents/patcher.md +27 -0
- package/agents/planner.md +30 -0
- package/agents/project-leader.md +28 -0
- package/agents/reviewer.md +27 -0
- package/agents/scout.md +27 -0
- package/agents/solution-designer.md +62 -0
- package/agents/super-pi-documents/index.ts +92 -0
- package/agents/super-pi-feishu/index.ts +88 -0
- package/agents/super-pi-knowledge/index.ts +70 -0
- package/agents/super-pi-managed-model/index.ts +9 -0
- package/agents/super-pi-memory/index.ts +75 -0
- package/agents/super-pi-native-tools/index.ts +1 -0
- package/agents/super-pi-task/core.ts +2262 -0
- package/agents/super-pi-task/index.ts +814 -0
- package/agents/super-pi-task/workflow-schema.ts +34 -0
- package/agents/super-pi-web/index.ts +32 -0
- package/agents/verifier.md +25 -0
- package/assets/fonts/LICENSE +92 -0
- package/assets/fonts/NotoSansSC-Bold.otf +0 -0
- package/assets/fonts/NotoSansSC-Regular.otf +0 -0
- package/assets/knowledge-model/README.md +37 -0
- package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/config.json +31 -0
- package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/onnx/model_quantized.onnx +0 -0
- package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/special_tokens_map.json +7 -0
- package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/tokenizer.json +21278 -0
- package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/tokenizer_config.json +15 -0
- package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/vocab.txt +21128 -0
- package/bin/npm-prefix.d.ts +1 -0
- package/bin/npm-prefix.js +10 -0
- package/bin/super-pi.js +349 -0
- package/dist/client/.super-pi-client-sources.json +82 -0
- package/dist/client/assets/CodeMirrorEditor-mi9rGUce.js +2 -0
- package/dist/client/assets/GitDiffEditor-CpoDDQrA.css +1 -0
- package/dist/client/assets/GitDiffEditor-FQBfGxOY.js +29 -0
- package/dist/client/assets/diff-ChtP43wD.js +1 -0
- package/dist/client/assets/dist-AW5lyI3I.js +1 -0
- package/dist/client/assets/dist-B3-a6uF0.js +7 -0
- package/dist/client/assets/dist-BGxkyh6e.js +1 -0
- package/dist/client/assets/dist-BJxfd7yb.js +11 -0
- package/dist/client/assets/dist-BRVQKlcp.js +1 -0
- package/dist/client/assets/dist-BV64A9Ji.js +1 -0
- package/dist/client/assets/dist-BXAOrsjq.js +1 -0
- package/dist/client/assets/dist-CKF2Jksy.js +1 -0
- package/dist/client/assets/dist-CX_Hm4GS.js +1 -0
- package/dist/client/assets/dist-Cus6_oaF.js +2 -0
- package/dist/client/assets/dist-Cx00EZKR.js +23 -0
- package/dist/client/assets/dist-D71y2EI1.js +1 -0
- package/dist/client/assets/dist-D978o95T.js +5 -0
- package/dist/client/assets/dist-Dc1MBGAG.js +1 -0
- package/dist/client/assets/dist-Dds-qLxY.js +9 -0
- package/dist/client/assets/dist-DeD9copn.js +1 -0
- package/dist/client/assets/dist-DuvGReu-.js +6 -0
- package/dist/client/assets/dist-Dvb0SqcK.js +2 -0
- package/dist/client/assets/dist-J6XWqRpB.js +3 -0
- package/dist/client/assets/dist-QzbOCAU6.js +1 -0
- package/dist/client/assets/dist-moiMRpw_.js +1 -0
- package/dist/client/assets/dist-scBkrwv-.js +2 -0
- package/dist/client/assets/dockerfile-BDePf5PZ.js +1 -0
- package/dist/client/assets/index-Cn7hdrDw.js +350 -0
- package/dist/client/assets/index-UfzqdECL.css +1 -0
- package/dist/client/assets/powershell-D8WpQPaX.js +1 -0
- package/dist/client/assets/properties-C357ku5U.js +1 -0
- package/dist/client/assets/rolldown-runtime-XKyaHtl9.js +1 -0
- package/dist/client/assets/ruby-ChfNwlCY.js +1 -0
- package/dist/client/assets/shell-DwuoZtxw.js +1 -0
- package/dist/client/assets/swift-DKB6_1j6.js +1 -0
- package/dist/client/assets/toml-BPTmHmyx.js +1 -0
- package/dist/client/favicon.ico +0 -0
- package/dist/client/favicon.png +0 -0
- package/dist/client/favicon.svg +10 -0
- package/dist/client/index.html +18 -0
- package/dist/extension.d.ts +15 -0
- package/dist/extension.js +126 -0
- package/dist/server/active-memory.d.ts +247 -0
- package/dist/server/active-memory.js +669 -0
- package/dist/server/agents.d.ts +245 -0
- package/dist/server/agents.js +701 -0
- package/dist/server/application/catalog-service.d.ts +98 -0
- package/dist/server/application/catalog-service.js +245 -0
- package/dist/server/application/conversation-service.d.ts +245 -0
- package/dist/server/application/conversation-service.js +654 -0
- package/dist/server/application/document-service.d.ts +68 -0
- package/dist/server/application/document-service.js +120 -0
- package/dist/server/application/extension-ui-service.d.ts +44 -0
- package/dist/server/application/extension-ui-service.js +135 -0
- package/dist/server/application/feishu-service.d.ts +179 -0
- package/dist/server/application/feishu-service.js +358 -0
- package/dist/server/application/group-chat-service.d.ts +75 -0
- package/dist/server/application/group-chat-service.js +953 -0
- package/dist/server/application/group-execution.d.ts +15 -0
- package/dist/server/application/group-execution.js +77 -0
- package/dist/server/application/project-service.d.ts +464 -0
- package/dist/server/application/project-service.js +2523 -0
- package/dist/server/application/provider-service.d.ts +174 -0
- package/dist/server/application/provider-service.js +510 -0
- package/dist/server/application/run-service.d.ts +547 -0
- package/dist/server/application/run-service.js +2683 -0
- package/dist/server/application/scheduled-task-service.d.ts +184 -0
- package/dist/server/application/scheduled-task-service.js +391 -0
- package/dist/server/application/session-orchestrator-service.d.ts +388 -0
- package/dist/server/application/session-orchestrator-service.js +1146 -0
- package/dist/server/application/session-reminder-service.d.ts +59 -0
- package/dist/server/application/session-reminder-service.js +141 -0
- package/dist/server/application/session-watchdog.d.ts +171 -0
- package/dist/server/application/session-watchdog.js +284 -0
- package/dist/server/application/workspace-service.d.ts +52 -0
- package/dist/server/application/workspace-service.js +186 -0
- package/dist/server/application/worktree-service.d.ts +13 -0
- package/dist/server/application/worktree-service.js +76 -0
- package/dist/server/browser-access.d.ts +34 -0
- package/dist/server/browser-access.js +89 -0
- package/dist/server/capability.d.ts +105 -0
- package/dist/server/capability.js +170 -0
- package/dist/server/command-handler.d.ts +93 -0
- package/dist/server/command-handler.js +540 -0
- package/dist/server/content-search.d.ts +16 -0
- package/dist/server/content-search.js +235 -0
- package/dist/server/conversation-usage.d.ts +22 -0
- package/dist/server/conversation-usage.js +43 -0
- package/dist/server/documents.d.ts +152 -0
- package/dist/server/documents.js +998 -0
- package/dist/server/engineering-learning.d.ts +30 -0
- package/dist/server/engineering-learning.js +106 -0
- package/dist/server/exec-service.d.ts +45 -0
- package/dist/server/exec-service.js +167 -0
- package/dist/server/explicit-file-open.d.ts +34 -0
- package/dist/server/explicit-file-open.js +105 -0
- package/dist/server/failure-text.d.ts +18 -0
- package/dist/server/failure-text.js +44 -0
- package/dist/server/feishu-cli.d.ts +223 -0
- package/dist/server/feishu-cli.js +464 -0
- package/dist/server/file-changes.d.ts +119 -0
- package/dist/server/file-changes.js +280 -0
- package/dist/server/file-diff.d.ts +41 -0
- package/dist/server/file-diff.js +97 -0
- package/dist/server/file-index.d.ts +100 -0
- package/dist/server/file-index.js +296 -0
- package/dist/server/file-name-index.d.ts +160 -0
- package/dist/server/file-name-index.js +411 -0
- package/dist/server/files.d.ts +269 -0
- package/dist/server/files.js +748 -0
- package/dist/server/git.d.ts +245 -0
- package/dist/server/git.js +903 -0
- package/dist/server/http-server.d.ts +45 -0
- package/dist/server/http-server.js +3830 -0
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +485 -0
- package/dist/server/inline-thinking.d.ts +90 -0
- package/dist/server/inline-thinking.js +181 -0
- package/dist/server/json-comments.d.ts +14 -0
- package/dist/server/json-comments.js +71 -0
- package/dist/server/knowledge-embedding-worker.d.ts +1 -0
- package/dist/server/knowledge-embedding-worker.js +27 -0
- package/dist/server/knowledge-vectors.d.ts +150 -0
- package/dist/server/knowledge-vectors.js +385 -0
- package/dist/server/knowledge.d.ts +273 -0
- package/dist/server/knowledge.js +848 -0
- package/dist/server/length-continuation.d.ts +83 -0
- package/dist/server/length-continuation.js +118 -0
- package/dist/server/logging.d.ts +32 -0
- package/dist/server/logging.js +185 -0
- package/dist/server/managed-http-tunnel.d.ts +6 -0
- package/dist/server/managed-http-tunnel.js +85 -0
- package/dist/server/managed-http.d.ts +4 -0
- package/dist/server/managed-http.js +85 -0
- package/dist/server/managed-model-adapter.d.ts +17 -0
- package/dist/server/managed-model-adapter.js +75 -0
- package/dist/server/managed-model-gateway.d.ts +49 -0
- package/dist/server/managed-model-gateway.js +73 -0
- package/dist/server/managed-servers.d.ts +55 -0
- package/dist/server/managed-servers.js +396 -0
- package/dist/server/managed-ssh.d.ts +26 -0
- package/dist/server/managed-ssh.js +73 -0
- package/dist/server/manager-vault.d.ts +11 -0
- package/dist/server/manager-vault.js +67 -0
- package/dist/server/memory-recall.d.ts +25 -0
- package/dist/server/memory-recall.js +45 -0
- package/dist/server/memory.d.ts +67 -0
- package/dist/server/memory.js +216 -0
- package/dist/server/message-media.d.ts +7 -0
- package/dist/server/message-media.js +54 -0
- package/dist/server/model-context-window.d.ts +186 -0
- package/dist/server/model-context-window.js +444 -0
- package/dist/server/model-filter.d.ts +23 -0
- package/dist/server/model-filter.js +48 -0
- package/dist/server/models-store.d.ts +36 -0
- package/dist/server/models-store.js +132 -0
- package/dist/server/native-tool-settings.d.ts +25 -0
- package/dist/server/native-tool-settings.js +41 -0
- package/dist/server/native-tools-extension.d.ts +2 -0
- package/dist/server/native-tools-extension.js +39 -0
- package/dist/server/native-tools-service.d.ts +39 -0
- package/dist/server/native-tools-service.js +240 -0
- package/dist/server/persistence/app-state-repository.d.ts +65 -0
- package/dist/server/persistence/app-state-repository.js +1 -0
- package/dist/server/persistence/json-state-repository.d.ts +14 -0
- package/dist/server/persistence/json-state-repository.js +43 -0
- package/dist/server/persistence/open-state-store.d.ts +3 -0
- package/dist/server/persistence/open-state-store.js +35 -0
- package/dist/server/persistence/rename.d.ts +22 -0
- package/dist/server/persistence/rename.js +44 -0
- package/dist/server/persistence/sqlite-state-repository.d.ts +12 -0
- package/dist/server/persistence/sqlite-state-repository.js +264 -0
- package/dist/server/pi-command.d.ts +94 -0
- package/dist/server/pi-command.js +265 -0
- package/dist/server/pi-native.d.ts +12 -0
- package/dist/server/pi-native.js +16 -0
- package/dist/server/pi-session-events.d.ts +158 -0
- package/dist/server/pi-session-events.js +373 -0
- package/dist/server/pi-session-pool.d.ts +233 -0
- package/dist/server/pi-session-pool.js +382 -0
- package/dist/server/pi-session.d.ts +642 -0
- package/dist/server/pi-session.js +655 -0
- package/dist/server/presentations/schema.d.ts +120 -0
- package/dist/server/presentations/schema.js +128 -0
- package/dist/server/provider-network.d.ts +16 -0
- package/dist/server/provider-network.js +106 -0
- package/dist/server/provider-proxy.d.ts +18 -0
- package/dist/server/provider-proxy.js +200 -0
- package/dist/server/providers.d.ts +333 -0
- package/dist/server/providers.js +989 -0
- package/dist/server/remote-bootstrap.d.ts +17 -0
- package/dist/server/remote-bootstrap.js +119 -0
- package/dist/server/rpc-channel.d.ts +131 -0
- package/dist/server/rpc-channel.js +135 -0
- package/dist/server/runtime.d.ts +1393 -0
- package/dist/server/runtime.js +3286 -0
- package/dist/server/scheduled-tasks.d.ts +101 -0
- package/dist/server/scheduled-tasks.js +138 -0
- package/dist/server/server-context.d.ts +119 -0
- package/dist/server/server-context.js +1 -0
- package/dist/server/session-cwd.d.ts +19 -0
- package/dist/server/session-cwd.js +58 -0
- package/dist/server/session-ledger.d.ts +67 -0
- package/dist/server/session-ledger.js +76 -0
- package/dist/server/session-memory-scope.d.ts +4 -0
- package/dist/server/session-memory-scope.js +30 -0
- package/dist/server/session-repository.d.ts +100 -0
- package/dist/server/session-repository.js +246 -0
- package/dist/server/session-tree-scope.d.ts +125 -0
- package/dist/server/session-tree-scope.js +337 -0
- package/dist/server/skills.d.ts +23 -0
- package/dist/server/skills.js +190 -0
- package/dist/server/store.d.ts +1062 -0
- package/dist/server/store.js +3305 -0
- package/dist/server/subagent-runs.d.ts +185 -0
- package/dist/server/subagent-runs.js +383 -0
- package/dist/server/subagents.d.ts +200 -0
- package/dist/server/subagents.js +619 -0
- package/dist/server/task-tool.d.ts +282 -0
- package/dist/server/task-tool.js +507 -0
- package/dist/server/tavily.d.ts +32 -0
- package/dist/server/tavily.js +136 -0
- package/dist/server/team-context.d.ts +12 -0
- package/dist/server/team-context.js +180 -0
- package/dist/server/terminal.d.ts +163 -0
- package/dist/server/terminal.js +557 -0
- package/dist/server/thinking-level-discovery.d.ts +38 -0
- package/dist/server/thinking-level-discovery.js +183 -0
- package/dist/server/time-zone.d.ts +20 -0
- package/dist/server/time-zone.js +32 -0
- package/dist/server/tool-calls.d.ts +130 -0
- package/dist/server/tool-calls.js +341 -0
- package/dist/server/tool-llm.d.ts +72 -0
- package/dist/server/tool-llm.js +146 -0
- package/dist/server/tool-registry.d.ts +129 -0
- package/dist/server/tool-registry.js +249 -0
- package/dist/server/tool-runner.d.ts +112 -0
- package/dist/server/tool-runner.js +306 -0
- package/dist/server/turn-failure.d.ts +48 -0
- package/dist/server/turn-failure.js +148 -0
- package/dist/server/types.d.ts +2315 -0
- package/dist/server/types.js +11 -0
- package/dist/server/update-status.d.ts +14 -0
- package/dist/server/update-status.js +69 -0
- package/dist/server/update.d.ts +46 -0
- package/dist/server/update.js +104 -0
- package/dist/server/upload.d.ts +35 -0
- package/dist/server/upload.js +177 -0
- package/dist/server/version.d.ts +33 -0
- package/dist/server/version.js +111 -0
- package/dist/server/web.d.ts +27 -0
- package/dist/server/web.js +183 -0
- package/dist/server/workflow-engine.d.ts +47 -0
- package/dist/server/workflow-engine.js +255 -0
- package/dist/server/workflow.d.ts +329 -0
- package/dist/server/workflow.js +469 -0
- package/dist/server/workspace-file-indexes.d.ts +2 -0
- package/dist/server/workspace-file-indexes.js +61 -0
- package/dist/server/workspace-file-operations.d.ts +36 -0
- package/dist/server/workspace-file-operations.js +281 -0
- package/dist/server/workspace-file-response.d.ts +7 -0
- package/dist/server/workspace-file-response.js +79 -0
- package/dist/server/ws-server.d.ts +149 -0
- package/dist/server/ws-server.js +640 -0
- package/dist/shared/agent-order.d.ts +44 -0
- package/dist/shared/agent-order.js +50 -0
- package/dist/shared/archive-decisions.d.ts +44 -0
- package/dist/shared/archive-decisions.js +98 -0
- package/dist/shared/background-task.d.ts +25 -0
- package/dist/shared/background-task.js +183 -0
- package/dist/shared/child-session-lock.d.ts +37 -0
- package/dist/shared/child-session-lock.js +59 -0
- package/dist/shared/content-search.d.ts +22 -0
- package/dist/shared/content-search.js +1 -0
- package/dist/shared/conversation-title.d.ts +4 -0
- package/dist/shared/conversation-title.js +4 -0
- package/dist/shared/file-mentions.d.ts +17 -0
- package/dist/shared/file-mentions.js +91 -0
- package/dist/shared/git-commit.d.ts +11 -0
- package/dist/shared/git-commit.js +1 -0
- package/dist/shared/git-graph.d.ts +42 -0
- package/dist/shared/git-graph.js +30 -0
- package/dist/shared/group-chat.d.ts +132 -0
- package/dist/shared/group-chat.js +6 -0
- package/dist/shared/managed-servers.d.ts +36 -0
- package/dist/shared/managed-servers.js +1 -0
- package/dist/shared/message-media.d.ts +12 -0
- package/dist/shared/message-media.js +27 -0
- package/dist/shared/model-window-label.d.ts +39 -0
- package/dist/shared/model-window-label.js +55 -0
- package/dist/shared/native-tools.d.ts +43 -0
- package/dist/shared/native-tools.js +1 -0
- package/dist/shared/pi-cache-warming.d.ts +9 -0
- package/dist/shared/pi-cache-warming.js +4 -0
- package/dist/shared/project-overview.d.ts +107 -0
- package/dist/shared/project-overview.js +92 -0
- package/dist/shared/project-presentation.d.ts +55 -0
- package/dist/shared/project-presentation.js +111 -0
- package/dist/shared/provider-network.d.ts +30 -0
- package/dist/shared/provider-network.js +1 -0
- package/dist/shared/session-reporting.d.ts +40 -0
- package/dist/shared/session-reporting.js +97 -0
- package/dist/shared/session-search.d.ts +14 -0
- package/dist/shared/session-search.js +1 -0
- package/dist/shared/team-context-view.d.ts +33 -0
- package/dist/shared/team-context-view.js +49 -0
- package/dist/shared/team-context.d.ts +59 -0
- package/dist/shared/team-context.js +3 -0
- package/dist/shared/terminal-protocol.d.ts +71 -0
- package/dist/shared/terminal-protocol.js +1 -0
- package/dist/shared/thinking-levels.d.ts +53 -0
- package/dist/shared/thinking-levels.js +85 -0
- package/dist/shared/tool-glyph.d.ts +7 -0
- package/dist/shared/tool-glyph.js +19 -0
- package/dist/shared/transcript-order.d.ts +7 -0
- package/dist/shared/transcript-order.js +12 -0
- package/dist/shared/update-status.d.ts +28 -0
- package/dist/shared/update-status.js +1 -0
- package/dist/shared/workspace-file-preview.d.ts +4 -0
- package/dist/shared/workspace-file-preview.js +28 -0
- package/package.json +450 -4
- package/scripts/diagnose.mjs +603 -0
- package/scripts/patch-brace-expansion.mjs +260 -0
- package/scripts/restart-server.mjs +104 -0
- package/skills/presentation/SKILL.md +30 -0
- package/skills/presentation/references/data.md +10 -0
- package/skills/presentation/references/quality.md +12 -0
- package/skills/presentation/references/story.md +12 -0
- package/skills/presentation/references/templates-and-editing.md +11 -0
- package/skills/presentation/references/visual-system.md +10 -0
- package/skills/super-pi-agent-development/SKILL.md +25 -0
- package/skills/super-pi-development/SKILL.md +31 -0
- package/skills/super-pi-tool-development/SKILL.md +39 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import type { EventBroadcaster } from "../server-context.js";
|
|
2
|
+
import type { Store } from "../store.js";
|
|
3
|
+
import { describeSchedule, type ScheduledTask, type ScheduleSpec } from "../scheduled-tasks.js";
|
|
4
|
+
import type { ModelSelection } from "../types.js";
|
|
5
|
+
import type { ConversationService } from "./conversation-service.js";
|
|
6
|
+
import type { RunService } from "./run-service.js";
|
|
7
|
+
/** The constructor's last parameter: the only seams this service admits. */
|
|
8
|
+
export type ScheduledTaskServiceOptions = {
|
|
9
|
+
/**
|
|
10
|
+
* The TEST SEAM for the clock: how often the sweep runs. Production never passes this and gets the
|
|
11
|
+
* fifteen-second sweep; a test passes a few milliseconds and waits for real time instead of
|
|
12
|
+
* faking timers - the same role `sessionFactory` plays in `PiRpcRuntime`.
|
|
13
|
+
*/
|
|
14
|
+
sweepIntervalMs?: number;
|
|
15
|
+
};
|
|
16
|
+
/** What a caller wants a task to be. Everything but the id and the bookkeeping is optional. */
|
|
17
|
+
export type ScheduledTaskInput = {
|
|
18
|
+
name?: string;
|
|
19
|
+
prompt?: string;
|
|
20
|
+
schedule?: unknown;
|
|
21
|
+
workspaceId?: string;
|
|
22
|
+
model?: ModelSelection | null;
|
|
23
|
+
agentId?: string | null;
|
|
24
|
+
enabled?: boolean;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* THE SCHEDULED TASKS SERVICE: the list, the clock, and the runs.
|
|
28
|
+
*
|
|
29
|
+
* WHAT LIVES HERE, AND WHY NOT IN THE STORE. The store owns the record and the disk; this owns the
|
|
30
|
+
* clock and the act of running. Keeping the two apart is what lets the store stay a thing that can be
|
|
31
|
+
* opened, repaired and closed without a timer running behind it - and it is why a scheduled task
|
|
32
|
+
* survives a restart as a RECORD while the thing that fires it is built fresh from that record.
|
|
33
|
+
*/
|
|
34
|
+
export declare class ScheduledTaskService {
|
|
35
|
+
private readonly store;
|
|
36
|
+
private readonly runService;
|
|
37
|
+
private readonly conversationService;
|
|
38
|
+
private readonly broadcaster;
|
|
39
|
+
/** The runs currently in flight, by task id. A task in here is not fired again. */
|
|
40
|
+
private readonly inFlight;
|
|
41
|
+
private timer?;
|
|
42
|
+
private stopped;
|
|
43
|
+
constructor(store: Store, runService: RunService, conversationService: ConversationService, broadcaster: EventBroadcaster, options?: ScheduledTaskServiceOptions);
|
|
44
|
+
private readonly sweepIntervalMs;
|
|
45
|
+
/**
|
|
46
|
+
* Starts the clock.
|
|
47
|
+
*
|
|
48
|
+
* `unref()` so the sweep never holds the process open: the shutdown sequence closes the server and
|
|
49
|
+
* the store, and a pending interval would keep the process alive after that, which is the difference
|
|
50
|
+
* between a server that exits and one that has to be killed.
|
|
51
|
+
*/
|
|
52
|
+
start(): void;
|
|
53
|
+
/**
|
|
54
|
+
* Stops the clock and waits for whatever is running.
|
|
55
|
+
*
|
|
56
|
+
* The in-flight runs are NOT aborted: a task that is halfway through its answer is a real answer in
|
|
57
|
+
* a real conversation, and killing it because the server is shutting down would throw that work
|
|
58
|
+
* away. The sweep stops, the runs finish, and the next start picks the schedule up from what is on
|
|
59
|
+
* disk.
|
|
60
|
+
*/
|
|
61
|
+
stop(): Promise<void>;
|
|
62
|
+
/** Every task on record, newest first. */
|
|
63
|
+
list(): ScheduledTask[];
|
|
64
|
+
/** The tasks of ONE workspace, which is what the panel shows. */
|
|
65
|
+
listForWorkspace(workspaceId: string | undefined): ScheduledTask[];
|
|
66
|
+
/**
|
|
67
|
+
* The task, if it belongs to THIS workspace - and a plain "not found" if it does not.
|
|
68
|
+
*
|
|
69
|
+
* WHY THE OWNERSHIP CHECK IS HERE, IN THE SERVICE. A task is a thing that RUNS in a workspace: it
|
|
70
|
+
* opens its conversation there, uses that workspace's defaults and its tools. A caller in another
|
|
71
|
+
* workspace cannot even list these tasks - `listForWorkspace` is the scoped read - so it must not
|
|
72
|
+
* be able to change or delete them either. Before this, `update` and `delete` acted on an id alone:
|
|
73
|
+
* a task created from workspace B could be rewritten and then deleted by a conversation in
|
|
74
|
+
* workspace A, and it STILL RAN in B, so A's prompt executed inside B with B's model and B's tools
|
|
75
|
+
* - an invisible cross-workspace write (measured by scripts/scheduled-task-scope.test.mjs, ledger
|
|
76
|
+
* TEST-026).
|
|
77
|
+
*
|
|
78
|
+
* THE REFUSAL DOES NOT SAY WHERE IT IS. "Not found" is the whole answer: a caller must not be able
|
|
79
|
+
* to learn that a task exists in a workspace it cannot see.
|
|
80
|
+
*/
|
|
81
|
+
requireForWorkspace(id: string, workspaceId: string): ScheduledTask;
|
|
82
|
+
/**
|
|
83
|
+
* Creates a task.
|
|
84
|
+
*
|
|
85
|
+
* `createdBy` is recorded because the two kinds are not the same thing to a reader: a task the user
|
|
86
|
+
* asked for and a task an agent invented look identical in the list, and the difference is exactly
|
|
87
|
+
* what a person wants to know before they delete it.
|
|
88
|
+
*/
|
|
89
|
+
create(input: ScheduledTaskInput, createdBy: "user" | "agent"): Promise<ScheduledTask>;
|
|
90
|
+
/**
|
|
91
|
+
* Changes a task.
|
|
92
|
+
*
|
|
93
|
+
* EVERY CHANGE RECOMPUTES `nextRunAt`, because a task whose prompt was edited an hour before its
|
|
94
|
+
* slot must not fire at the old slot with the old schedule - and a task whose schedule was edited
|
|
95
|
+
* must not keep the old next time. The one exception is a manual trigger, which is not a change at
|
|
96
|
+
* all: it runs the task once and leaves the clock where it was.
|
|
97
|
+
*/
|
|
98
|
+
update(id: string, input: ScheduledTaskInput): Promise<ScheduledTask>;
|
|
99
|
+
/** Turns a task on or off. */
|
|
100
|
+
setEnabled(id: string, enabled: boolean): Promise<ScheduledTask>;
|
|
101
|
+
/**
|
|
102
|
+
* Removes a task. Its conversation is ARCHIVED, not deleted.
|
|
103
|
+
*
|
|
104
|
+
* THE ANSWERS ARE WORTH KEEPING, so the session is not thrown away - and archiving is the keeping
|
|
105
|
+
* that can be undone: the session moves into the sidebar's 已归档 group and can be restored from
|
|
106
|
+
* there. The old behaviour was to leave it in the LIVE list, and that was the defect: a 「定时:xxx」
|
|
107
|
+
* session with no task behind it is an orphan the reader cannot explain (there is no delete-session
|
|
108
|
+
* control at all) and cannot get rid of.
|
|
109
|
+
*
|
|
110
|
+
* THE DELETE ITSELF IS NEVER REFUSED. The task is removed first and the archive is best-effort: a
|
|
111
|
+
* conversation that is still running cannot be archived, and a reader who deletes a task must not be
|
|
112
|
+
* told "no" because a session happens to be busy. The session simply stays where it is, and the
|
|
113
|
+
* reason is in the server log rather than in the reader's face.
|
|
114
|
+
*/
|
|
115
|
+
remove(id: string): Promise<void>;
|
|
116
|
+
/**
|
|
117
|
+
* Runs a task NOW, once.
|
|
118
|
+
*
|
|
119
|
+
* The clock is untouched: a manual run does not make the next scheduled run earlier or later, and it
|
|
120
|
+
* does not count as the task having fired. This is the difference between 「先试一次」 and 「改一下周期」,
|
|
121
|
+
* and conflating them would make 「先试一次」 silently rewrite the user's schedule.
|
|
122
|
+
*
|
|
123
|
+
* THE ACK RETURNS BEFORE THE RUN FINISHES. The run itself takes as long as a model turn takes -
|
|
124
|
+
* seconds, usually, sometimes minutes - and a command that waited for it would hold the socket's
|
|
125
|
+
* whole command chain hostage for that long. The outcome is not lost: it lands in `lastRunStatus`
|
|
126
|
+
* and arrives at the panel through the ordinary state broadcast, where a person reads it.
|
|
127
|
+
*/
|
|
128
|
+
trigger(id: string): Promise<void>;
|
|
129
|
+
/**
|
|
130
|
+
* The sweep: every task whose moment has come runs once.
|
|
131
|
+
*
|
|
132
|
+
* The tasks that are due are collected BEFORE any of them runs, and each one advances its own
|
|
133
|
+
* `nextRunAt` before it runs - so a task that takes three minutes cannot be fired again by the next
|
|
134
|
+
* sweep, and two sweeps can never both pick up the same task.
|
|
135
|
+
*/
|
|
136
|
+
private sweep;
|
|
137
|
+
/**
|
|
138
|
+
* THE ACTUAL RUN, for both the clock and the button.
|
|
139
|
+
*
|
|
140
|
+
* The task gets its OWN conversation, created on the first run, and the prompt is sent into it
|
|
141
|
+
* verbatim as a user message - the same path a person's own message takes. That sameness is the point:
|
|
142
|
+
* a scheduled task has no special powers, so it cannot do anything a message could not, and it gets
|
|
143
|
+
* the same model, the same tools, the same memory.
|
|
144
|
+
*/
|
|
145
|
+
private fire;
|
|
146
|
+
/** The body of a run: get a conversation, put the prompt into it, remember how it went. */
|
|
147
|
+
private runTask;
|
|
148
|
+
/**
|
|
149
|
+
* The conversation the task runs in, created the first time it is needed.
|
|
150
|
+
*
|
|
151
|
+
* LAZY, so that creating a task does not create a conversation nobody has spoken in. The name says
|
|
152
|
+
* what the conversation is, because it appears in the conversation list like any other and the user
|
|
153
|
+
* has to be able to tell it apart from their own work.
|
|
154
|
+
*/
|
|
155
|
+
private ensureConversation;
|
|
156
|
+
/**
|
|
157
|
+
* Puts the task's model and agent on its conversation.
|
|
158
|
+
*
|
|
159
|
+
* EVERY RUN, not only the first: the user can change the model in the panel, and the next run is the
|
|
160
|
+
* one that has to use it. A failure here is the task's failure - a model that Pi no longer offers
|
|
161
|
+
* cannot be silently ignored, because the run would then happen on some other model and the panel
|
|
162
|
+
* would say it succeeded.
|
|
163
|
+
*
|
|
164
|
+
* AND WHEN THERE IS NO OVERRIDE, THE DEFAULTS BEHIND IT GO ON THE CONVERSATION TOO. Clearing the
|
|
165
|
+
* override in the panel means 「跟着工作区默认走」, and the only way to honour that on a conversation
|
|
166
|
+
* that already ran with the override is to PUT THE DEFAULT BACK. The conversation's model is a
|
|
167
|
+
* copy-at-birth value (AGENTS.md: 建会话时把默认值盖到会话字段上,之后永不回查工作区), so "do
|
|
168
|
+
* nothing when there is no override" keeps the WITHDRAWN override pinned for good - and the panel
|
|
169
|
+
* says the run succeeded on a model the user just took away.
|
|
170
|
+
*/
|
|
171
|
+
private applyConfiguration;
|
|
172
|
+
/** Writes down how the run went, and tells the panel. */
|
|
173
|
+
private record;
|
|
174
|
+
/** The task, or a refusal in words the reader can act on. */
|
|
175
|
+
private require;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* The schedule in words, for the text an agent reads back after creating a task.
|
|
179
|
+
*
|
|
180
|
+
* Exported from here rather than from `scheduled-tasks.ts` so that the HTTP layer and the WS layer
|
|
181
|
+
* say the same thing about one schedule.
|
|
182
|
+
*/
|
|
183
|
+
export { describeSchedule };
|
|
184
|
+
export type { ScheduledTask, ScheduleSpec };
|
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { describeSchedule, nextRunAt, normalizeSchedule } from "../scheduled-tasks.js";
|
|
3
|
+
/**
|
|
4
|
+
* HOW OFTEN THE SCHEDULER LOOKS.
|
|
5
|
+
*
|
|
6
|
+
* A sweep every fifteen seconds, rather than one timer per task. A per-task timer would fire closer to
|
|
7
|
+
* the requested minute, and it would need a timer to be created, replaced and destroyed every time a
|
|
8
|
+
* task was edited - a class of bug (a timer left pointing at the old schedule) that a single sweep
|
|
9
|
+
* cannot have. The cost of the sweep is a scan of a list that is bounded at a hundred entries.
|
|
10
|
+
*
|
|
11
|
+
* FIFTEEN SECONDS IS ALSO THE HONEST ANSWER to "how precise is 'every day at 09:00'": a task can fire
|
|
12
|
+
* up to fifteen seconds after the minute it was asked for, and never before it.
|
|
13
|
+
*/
|
|
14
|
+
const SWEEP_INTERVAL_MS = 15_000;
|
|
15
|
+
/**
|
|
16
|
+
* THE SCHEDULED TASKS SERVICE: the list, the clock, and the runs.
|
|
17
|
+
*
|
|
18
|
+
* WHAT LIVES HERE, AND WHY NOT IN THE STORE. The store owns the record and the disk; this owns the
|
|
19
|
+
* clock and the act of running. Keeping the two apart is what lets the store stay a thing that can be
|
|
20
|
+
* opened, repaired and closed without a timer running behind it - and it is why a scheduled task
|
|
21
|
+
* survives a restart as a RECORD while the thing that fires it is built fresh from that record.
|
|
22
|
+
*/
|
|
23
|
+
export class ScheduledTaskService {
|
|
24
|
+
store;
|
|
25
|
+
runService;
|
|
26
|
+
conversationService;
|
|
27
|
+
broadcaster;
|
|
28
|
+
/** The runs currently in flight, by task id. A task in here is not fired again. */
|
|
29
|
+
inFlight = new Map();
|
|
30
|
+
timer;
|
|
31
|
+
stopped = true;
|
|
32
|
+
constructor(store, runService, conversationService, broadcaster, options = {}) {
|
|
33
|
+
this.store = store;
|
|
34
|
+
this.runService = runService;
|
|
35
|
+
this.conversationService = conversationService;
|
|
36
|
+
this.broadcaster = broadcaster;
|
|
37
|
+
this.sweepIntervalMs = options.sweepIntervalMs ?? SWEEP_INTERVAL_MS;
|
|
38
|
+
}
|
|
39
|
+
sweepIntervalMs;
|
|
40
|
+
/**
|
|
41
|
+
* Starts the clock.
|
|
42
|
+
*
|
|
43
|
+
* `unref()` so the sweep never holds the process open: the shutdown sequence closes the server and
|
|
44
|
+
* the store, and a pending interval would keep the process alive after that, which is the difference
|
|
45
|
+
* between a server that exits and one that has to be killed.
|
|
46
|
+
*/
|
|
47
|
+
start() {
|
|
48
|
+
if (this.timer)
|
|
49
|
+
return;
|
|
50
|
+
this.stopped = false;
|
|
51
|
+
this.timer = setInterval(() => { void this.sweep(); }, this.sweepIntervalMs);
|
|
52
|
+
this.timer.unref?.();
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Stops the clock and waits for whatever is running.
|
|
56
|
+
*
|
|
57
|
+
* The in-flight runs are NOT aborted: a task that is halfway through its answer is a real answer in
|
|
58
|
+
* a real conversation, and killing it because the server is shutting down would throw that work
|
|
59
|
+
* away. The sweep stops, the runs finish, and the next start picks the schedule up from what is on
|
|
60
|
+
* disk.
|
|
61
|
+
*/
|
|
62
|
+
async stop() {
|
|
63
|
+
this.stopped = true;
|
|
64
|
+
if (this.timer) {
|
|
65
|
+
clearInterval(this.timer);
|
|
66
|
+
this.timer = undefined;
|
|
67
|
+
}
|
|
68
|
+
await Promise.allSettled([...this.inFlight.values()]);
|
|
69
|
+
}
|
|
70
|
+
/** Every task on record, newest first. */
|
|
71
|
+
list() {
|
|
72
|
+
return [...(this.store.snapshot().scheduledTasks ?? [])].sort((a, b) => b.createdAt - a.createdAt);
|
|
73
|
+
}
|
|
74
|
+
/** The tasks of ONE workspace, which is what the panel shows. */
|
|
75
|
+
listForWorkspace(workspaceId) {
|
|
76
|
+
if (!workspaceId)
|
|
77
|
+
return [];
|
|
78
|
+
return this.list().filter((task) => task.workspaceId === workspaceId);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The task, if it belongs to THIS workspace - and a plain "not found" if it does not.
|
|
82
|
+
*
|
|
83
|
+
* WHY THE OWNERSHIP CHECK IS HERE, IN THE SERVICE. A task is a thing that RUNS in a workspace: it
|
|
84
|
+
* opens its conversation there, uses that workspace's defaults and its tools. A caller in another
|
|
85
|
+
* workspace cannot even list these tasks - `listForWorkspace` is the scoped read - so it must not
|
|
86
|
+
* be able to change or delete them either. Before this, `update` and `delete` acted on an id alone:
|
|
87
|
+
* a task created from workspace B could be rewritten and then deleted by a conversation in
|
|
88
|
+
* workspace A, and it STILL RAN in B, so A's prompt executed inside B with B's model and B's tools
|
|
89
|
+
* - an invisible cross-workspace write (measured by scripts/scheduled-task-scope.test.mjs, ledger
|
|
90
|
+
* TEST-026).
|
|
91
|
+
*
|
|
92
|
+
* THE REFUSAL DOES NOT SAY WHERE IT IS. "Not found" is the whole answer: a caller must not be able
|
|
93
|
+
* to learn that a task exists in a workspace it cannot see.
|
|
94
|
+
*/
|
|
95
|
+
requireForWorkspace(id, workspaceId) {
|
|
96
|
+
const task = this.listForWorkspace(workspaceId).find((item) => item.id === id);
|
|
97
|
+
if (!task)
|
|
98
|
+
throw new Error("找不到这个定时任务。");
|
|
99
|
+
return task;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Creates a task.
|
|
103
|
+
*
|
|
104
|
+
* `createdBy` is recorded because the two kinds are not the same thing to a reader: a task the user
|
|
105
|
+
* asked for and a task an agent invented look identical in the list, and the difference is exactly
|
|
106
|
+
* what a person wants to know before they delete it.
|
|
107
|
+
*/
|
|
108
|
+
async create(input, createdBy) {
|
|
109
|
+
const prompt = (input.prompt ?? "").trim();
|
|
110
|
+
if (!prompt)
|
|
111
|
+
throw new Error("定时任务需要一段提示词:到点时这段话会被当作用户消息发给 agent。");
|
|
112
|
+
const workspaceId = (input.workspaceId ?? "").trim();
|
|
113
|
+
if (!workspaceId)
|
|
114
|
+
throw new Error("定时任务需要属于一个工作区。");
|
|
115
|
+
if (!this.store.snapshot().workspaces.some((workspace) => workspace.id === workspaceId))
|
|
116
|
+
throw new Error("找不到这个工作区。");
|
|
117
|
+
const schedule = normalizeSchedule(input.schedule);
|
|
118
|
+
const now = Date.now();
|
|
119
|
+
const firstRunAt = nextRunAt(schedule, now);
|
|
120
|
+
// A ONE-SHOT TASK WHOSE MOMENT HAS PASSED is not a task, it is a memory of one. Refusing here is
|
|
121
|
+
// what keeps it from being created with no next run and never firing - a task that exists but
|
|
122
|
+
// cannot run is the worst outcome, because nothing on screen explains why.
|
|
123
|
+
if (schedule.kind === "once" && firstRunAt === undefined)
|
|
124
|
+
throw new Error("一次性任务的触发时刻要在将来。");
|
|
125
|
+
const task = {
|
|
126
|
+
id: `scheduled_${randomUUID()}`,
|
|
127
|
+
name: (input.name ?? "").trim() || defaultName(prompt),
|
|
128
|
+
prompt,
|
|
129
|
+
workspaceId,
|
|
130
|
+
schedule,
|
|
131
|
+
enabled: input.enabled === false ? false : true,
|
|
132
|
+
nextRunAt: firstRunAt,
|
|
133
|
+
createdBy,
|
|
134
|
+
createdAt: now,
|
|
135
|
+
updatedAt: now,
|
|
136
|
+
...(input.model ? { model: input.model } : {}),
|
|
137
|
+
...(input.agentId ? { agentId: input.agentId } : {}),
|
|
138
|
+
};
|
|
139
|
+
const stored = await this.store.setScheduledTask(task);
|
|
140
|
+
this.broadcaster.broadcastState();
|
|
141
|
+
return stored;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Changes a task.
|
|
145
|
+
*
|
|
146
|
+
* EVERY CHANGE RECOMPUTES `nextRunAt`, because a task whose prompt was edited an hour before its
|
|
147
|
+
* slot must not fire at the old slot with the old schedule - and a task whose schedule was edited
|
|
148
|
+
* must not keep the old next time. The one exception is a manual trigger, which is not a change at
|
|
149
|
+
* all: it runs the task once and leaves the clock where it was.
|
|
150
|
+
*/
|
|
151
|
+
async update(id, input) {
|
|
152
|
+
const task = this.require(id);
|
|
153
|
+
const now = Date.now();
|
|
154
|
+
const schedule = input.schedule === undefined ? task.schedule : normalizeSchedule(input.schedule);
|
|
155
|
+
const updated = {
|
|
156
|
+
...task,
|
|
157
|
+
name: input.name === undefined ? task.name : (input.name.trim() || task.name),
|
|
158
|
+
prompt: input.prompt === undefined ? task.prompt : (input.prompt.trim() || task.prompt),
|
|
159
|
+
schedule,
|
|
160
|
+
enabled: input.enabled === undefined ? task.enabled : input.enabled,
|
|
161
|
+
updatedAt: now,
|
|
162
|
+
};
|
|
163
|
+
if (input.model !== undefined) {
|
|
164
|
+
if (input.model)
|
|
165
|
+
updated.model = input.model;
|
|
166
|
+
else
|
|
167
|
+
delete updated.model;
|
|
168
|
+
}
|
|
169
|
+
if (input.agentId !== undefined) {
|
|
170
|
+
if (input.agentId)
|
|
171
|
+
updated.agentId = input.agentId;
|
|
172
|
+
else
|
|
173
|
+
delete updated.agentId;
|
|
174
|
+
}
|
|
175
|
+
// A DISABLED TASK HAS NO NEXT TIME, and an enabled one always has one - unless it is a one-shot
|
|
176
|
+
// whose moment has passed, which is a finished task rather than a broken one.
|
|
177
|
+
updated.nextRunAt = updated.enabled ? nextRunAt(schedule, now) : undefined;
|
|
178
|
+
const stored = await this.store.setScheduledTask(updated);
|
|
179
|
+
this.broadcaster.broadcastState();
|
|
180
|
+
return stored;
|
|
181
|
+
}
|
|
182
|
+
/** Turns a task on or off. */
|
|
183
|
+
async setEnabled(id, enabled) {
|
|
184
|
+
const task = this.require(id);
|
|
185
|
+
if (task.enabled === enabled)
|
|
186
|
+
return task;
|
|
187
|
+
return this.update(id, { enabled });
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Removes a task. Its conversation is ARCHIVED, not deleted.
|
|
191
|
+
*
|
|
192
|
+
* THE ANSWERS ARE WORTH KEEPING, so the session is not thrown away - and archiving is the keeping
|
|
193
|
+
* that can be undone: the session moves into the sidebar's 已归档 group and can be restored from
|
|
194
|
+
* there. The old behaviour was to leave it in the LIVE list, and that was the defect: a 「定时:xxx」
|
|
195
|
+
* session with no task behind it is an orphan the reader cannot explain (there is no delete-session
|
|
196
|
+
* control at all) and cannot get rid of.
|
|
197
|
+
*
|
|
198
|
+
* THE DELETE ITSELF IS NEVER REFUSED. The task is removed first and the archive is best-effort: a
|
|
199
|
+
* conversation that is still running cannot be archived, and a reader who deletes a task must not be
|
|
200
|
+
* told "no" because a session happens to be busy. The session simply stays where it is, and the
|
|
201
|
+
* reason is in the server log rather than in the reader's face.
|
|
202
|
+
*/
|
|
203
|
+
async remove(id) {
|
|
204
|
+
const task = this.require(id);
|
|
205
|
+
await this.store.removeScheduledTask(id);
|
|
206
|
+
const conversationId = task.conversationId;
|
|
207
|
+
if (conversationId) {
|
|
208
|
+
const conversation = this.store.conversation(conversationId);
|
|
209
|
+
if (conversation && !conversation.archivedAt) {
|
|
210
|
+
try {
|
|
211
|
+
await this.conversationService.archiveConversation(conversationId);
|
|
212
|
+
}
|
|
213
|
+
catch (error) {
|
|
214
|
+
console.warn(`Super Pi: the deleted task's conversation ${conversationId} could not be archived:`, error instanceof Error ? error.message : String(error));
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
this.broadcaster.broadcastState();
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Runs a task NOW, once.
|
|
222
|
+
*
|
|
223
|
+
* The clock is untouched: a manual run does not make the next scheduled run earlier or later, and it
|
|
224
|
+
* does not count as the task having fired. This is the difference between 「先试一次」 and 「改一下周期」,
|
|
225
|
+
* and conflating them would make 「先试一次」 silently rewrite the user's schedule.
|
|
226
|
+
*
|
|
227
|
+
* THE ACK RETURNS BEFORE THE RUN FINISHES. The run itself takes as long as a model turn takes -
|
|
228
|
+
* seconds, usually, sometimes minutes - and a command that waited for it would hold the socket's
|
|
229
|
+
* whole command chain hostage for that long. The outcome is not lost: it lands in `lastRunStatus`
|
|
230
|
+
* and arrives at the panel through the ordinary state broadcast, where a person reads it.
|
|
231
|
+
*/
|
|
232
|
+
async trigger(id) {
|
|
233
|
+
const task = this.require(id);
|
|
234
|
+
this.fire(task, "manual").catch(() => undefined);
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* The sweep: every task whose moment has come runs once.
|
|
238
|
+
*
|
|
239
|
+
* The tasks that are due are collected BEFORE any of them runs, and each one advances its own
|
|
240
|
+
* `nextRunAt` before it runs - so a task that takes three minutes cannot be fired again by the next
|
|
241
|
+
* sweep, and two sweeps can never both pick up the same task.
|
|
242
|
+
*/
|
|
243
|
+
async sweep() {
|
|
244
|
+
if (this.stopped)
|
|
245
|
+
return;
|
|
246
|
+
const now = Date.now();
|
|
247
|
+
// READ THROUGH THE NARROW READER, NOT THROUGH A CLONE OF THE WHOLE APPLICATION.
|
|
248
|
+
//
|
|
249
|
+
// This sweep runs every fifteen seconds for the whole life of the server. It used to read its
|
|
250
|
+
// tasks through `store.snapshot()` - a deep clone of every workspace, every conversation and every
|
|
251
|
+
// message of every conversation - to find the zero-to-three tasks that are due. On a server
|
|
252
|
+
// holding a long transcript that is a full copy of the app four times a minute, for a list that is
|
|
253
|
+
// usually empty. The narrow reader returns the tasks themselves and nothing else.
|
|
254
|
+
const due = this.store.scheduledTasks().filter((task) => task.enabled && task.nextRunAt !== undefined && task.nextRunAt <= now);
|
|
255
|
+
for (const task of due) {
|
|
256
|
+
if (this.inFlight.has(task.id))
|
|
257
|
+
continue;
|
|
258
|
+
const next = nextRunAt(task.schedule, now);
|
|
259
|
+
await this.store.setScheduledTask({
|
|
260
|
+
...task,
|
|
261
|
+
// A ONE-SHOT TASK IS FINISHED once it has fired. It is disabled rather than deleted, because
|
|
262
|
+
// the answers it produced are worth keeping and the user can see that it ran.
|
|
263
|
+
enabled: next === undefined ? false : task.enabled,
|
|
264
|
+
nextRunAt: next,
|
|
265
|
+
updatedAt: now,
|
|
266
|
+
});
|
|
267
|
+
this.broadcaster.broadcastState();
|
|
268
|
+
void this.fire(task, "schedule");
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* THE ACTUAL RUN, for both the clock and the button.
|
|
273
|
+
*
|
|
274
|
+
* The task gets its OWN conversation, created on the first run, and the prompt is sent into it
|
|
275
|
+
* verbatim as a user message - the same path a person's own message takes. That sameness is the point:
|
|
276
|
+
* a scheduled task has no special powers, so it cannot do anything a message could not, and it gets
|
|
277
|
+
* the same model, the same tools, the same memory.
|
|
278
|
+
*/
|
|
279
|
+
async fire(task, reason) {
|
|
280
|
+
const running = this.inFlight.get(task.id);
|
|
281
|
+
if (running) {
|
|
282
|
+
await running;
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
const promise = this.runTask(task, reason).finally(() => { this.inFlight.delete(task.id); });
|
|
286
|
+
this.inFlight.set(task.id, promise);
|
|
287
|
+
await promise;
|
|
288
|
+
}
|
|
289
|
+
/** The body of a run: get a conversation, put the prompt into it, remember how it went. */
|
|
290
|
+
async runTask(task, reason) {
|
|
291
|
+
const startedAt = Date.now();
|
|
292
|
+
try {
|
|
293
|
+
const conversationId = await this.ensureConversation(task);
|
|
294
|
+
await this.runService.enqueueAndRun(conversationId, task.prompt);
|
|
295
|
+
await this.record(task, { status: "completed", message: reason === "manual" ? "手动触发完成" : "按时触发完成", at: startedAt });
|
|
296
|
+
}
|
|
297
|
+
catch (error) {
|
|
298
|
+
// A FAILED RUN IS A RECORD, not a silence. The task keeps its schedule - a broken model or a
|
|
299
|
+
// missing workspace is worth retrying at the next slot - and the failure is written where the
|
|
300
|
+
// panel can show it.
|
|
301
|
+
await this.record(task, { status: "failed", message: error instanceof Error ? error.message : String(error), at: startedAt });
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* The conversation the task runs in, created the first time it is needed.
|
|
306
|
+
*
|
|
307
|
+
* LAZY, so that creating a task does not create a conversation nobody has spoken in. The name says
|
|
308
|
+
* what the conversation is, because it appears in the conversation list like any other and the user
|
|
309
|
+
* has to be able to tell it apart from their own work.
|
|
310
|
+
*/
|
|
311
|
+
async ensureConversation(task) {
|
|
312
|
+
const state = this.store.snapshot();
|
|
313
|
+
const existing = task.conversationId ? state.conversations.find((conversation) => conversation.id === task.conversationId) : undefined;
|
|
314
|
+
if (existing) {
|
|
315
|
+
await this.applyConfiguration(task, existing.id);
|
|
316
|
+
return existing.id;
|
|
317
|
+
}
|
|
318
|
+
const conversationId = await this.conversationService.createConversation(task.workspaceId);
|
|
319
|
+
await this.conversationService.renameConversation(conversationId, `定时:${task.name}`);
|
|
320
|
+
// THE CURRENT ROW, NOT THE STALE ONE. `task` is the snapshot the caller was holding, and its
|
|
321
|
+
// `nextRunAt` may already have been advanced by a sweep that fired this run - writing it back
|
|
322
|
+
// would move the clock backwards and the next sweep would fire the task a second time. The
|
|
323
|
+
// fresh row is read before the write, so the only field this changes is the one this line
|
|
324
|
+
// means to change.
|
|
325
|
+
const current = this.store.snapshot().scheduledTasks?.find((item) => item.id === task.id);
|
|
326
|
+
await this.store.setScheduledTask({ ...(current ?? task), conversationId, updatedAt: Date.now() });
|
|
327
|
+
await this.applyConfiguration(task, conversationId);
|
|
328
|
+
return conversationId;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Puts the task's model and agent on its conversation.
|
|
332
|
+
*
|
|
333
|
+
* EVERY RUN, not only the first: the user can change the model in the panel, and the next run is the
|
|
334
|
+
* one that has to use it. A failure here is the task's failure - a model that Pi no longer offers
|
|
335
|
+
* cannot be silently ignored, because the run would then happen on some other model and the panel
|
|
336
|
+
* would say it succeeded.
|
|
337
|
+
*
|
|
338
|
+
* AND WHEN THERE IS NO OVERRIDE, THE DEFAULTS BEHIND IT GO ON THE CONVERSATION TOO. Clearing the
|
|
339
|
+
* override in the panel means 「跟着工作区默认走」, and the only way to honour that on a conversation
|
|
340
|
+
* that already ran with the override is to PUT THE DEFAULT BACK. The conversation's model is a
|
|
341
|
+
* copy-at-birth value (AGENTS.md: 建会话时把默认值盖到会话字段上,之后永不回查工作区), so "do
|
|
342
|
+
* nothing when there is no override" keeps the WITHDRAWN override pinned for good - and the panel
|
|
343
|
+
* says the run succeeded on a model the user just took away.
|
|
344
|
+
*/
|
|
345
|
+
async applyConfiguration(task, conversationId) {
|
|
346
|
+
const state = this.store.snapshot();
|
|
347
|
+
const workspace = state.workspaces.find((item) => item.id === task.workspaceId);
|
|
348
|
+
const model = task.model ?? workspace?.defaultModel ?? state.defaultModel;
|
|
349
|
+
if (model) {
|
|
350
|
+
await this.conversationService.setModel(conversationId, model.provider, model.modelId);
|
|
351
|
+
}
|
|
352
|
+
const agentId = task.agentId ?? workspace?.defaultAgentId ?? null;
|
|
353
|
+
await this.conversationService.setAgent(conversationId, agentId);
|
|
354
|
+
}
|
|
355
|
+
/** Writes down how the run went, and tells the panel. */
|
|
356
|
+
async record(task, outcome) {
|
|
357
|
+
const current = this.store.snapshot().scheduledTasks?.find((item) => item.id === task.id);
|
|
358
|
+
if (!current)
|
|
359
|
+
return;
|
|
360
|
+
await this.store.setScheduledTask({
|
|
361
|
+
...current,
|
|
362
|
+
lastRunAt: outcome.at,
|
|
363
|
+
lastRunStatus: outcome.status,
|
|
364
|
+
lastRunMessage: outcome.message,
|
|
365
|
+
updatedAt: Date.now(),
|
|
366
|
+
});
|
|
367
|
+
this.broadcaster.broadcastState();
|
|
368
|
+
}
|
|
369
|
+
/** The task, or a refusal in words the reader can act on. */
|
|
370
|
+
require(id) {
|
|
371
|
+
const task = this.store.snapshot().scheduledTasks?.find((item) => item.id === id);
|
|
372
|
+
if (!task)
|
|
373
|
+
throw new Error("找不到这个定时任务。");
|
|
374
|
+
return task;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* The name a task gets when nobody gave it one: the first line of its prompt, cut to something that
|
|
379
|
+
* fits on a line in the panel.
|
|
380
|
+
*/
|
|
381
|
+
function defaultName(prompt) {
|
|
382
|
+
const firstLine = prompt.split("\n").map((line) => line.trim()).find(Boolean) ?? prompt.trim();
|
|
383
|
+
return firstLine.length > 24 ? `${firstLine.slice(0, 24)}…` : firstLine;
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* The schedule in words, for the text an agent reads back after creating a task.
|
|
387
|
+
*
|
|
388
|
+
* Exported from here rather than from `scheduled-tasks.ts` so that the HTTP layer and the WS layer
|
|
389
|
+
* say the same thing about one schedule.
|
|
390
|
+
*/
|
|
391
|
+
export { describeSchedule };
|