@evandu/super-pi-server 0.0.0-stage → 0.1.259
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 +248 -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 +392 -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 +448 -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,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The nine methods Pi's `RpcExtensionUIRequest` union defines (rpc-types.ts:252-287), split by whether
|
|
3
|
+
* Pi waits for a reply. `blocking` is the only field the transport needs in order to decide, and it
|
|
4
|
+
* is derived here rather than at each call site so the two halves can never drift apart.
|
|
5
|
+
*/
|
|
6
|
+
export const EXTENSION_UI_METHODS = [
|
|
7
|
+
"select", "confirm", "input", "editor",
|
|
8
|
+
"notify", "setStatus", "setWidget", "setTitle", "set_editor_text",
|
|
9
|
+
];
|
|
10
|
+
/** Pi registers exactly these four in `pendingExtensionRequests`; the other five are fire-and-forget. */
|
|
11
|
+
export const BLOCKING_UI_METHODS = new Set(["select", "confirm", "input", "editor"]);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type UpdateOperation, type UpdateStatus } from "../shared/update-status.js";
|
|
2
|
+
/** Keeps the last operation available to the service that comes up after a restart. */
|
|
3
|
+
export declare class UpdateTracker {
|
|
4
|
+
private readonly file;
|
|
5
|
+
private readonly root;
|
|
6
|
+
private operation;
|
|
7
|
+
constructor(file: string, root: string);
|
|
8
|
+
private save;
|
|
9
|
+
snapshot(): UpdateStatus;
|
|
10
|
+
begin(action: UpdateOperation["action"], targetVersion?: string): string;
|
|
11
|
+
private change;
|
|
12
|
+
restarting(id: string, targetVersion: string): void;
|
|
13
|
+
fail(id: string, step: "installing" | "restarting", message: string, code?: number): void;
|
|
14
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { updateIsActive } from "../shared/update-status.js";
|
|
4
|
+
import { checkDiskVersion } from "./version.js";
|
|
5
|
+
/** Keeps the last operation available to the service that comes up after a restart. */
|
|
6
|
+
export class UpdateTracker {
|
|
7
|
+
file;
|
|
8
|
+
root;
|
|
9
|
+
operation = null;
|
|
10
|
+
constructor(file, root) {
|
|
11
|
+
this.file = file;
|
|
12
|
+
this.root = root;
|
|
13
|
+
try {
|
|
14
|
+
const value = JSON.parse(readFileSync(file, "utf8"));
|
|
15
|
+
if (value?.id && ["installing", "restarting", "completed", "failed"].includes(value.phase))
|
|
16
|
+
this.operation = value;
|
|
17
|
+
}
|
|
18
|
+
catch { /* A new service has no update history. */ }
|
|
19
|
+
const previous = this.operation;
|
|
20
|
+
if (!previous || !updateIsActive(previous))
|
|
21
|
+
return;
|
|
22
|
+
const versions = checkDiskVersion(this.root);
|
|
23
|
+
if (previous.phase === "restarting" && previous.targetVersion === versions.runningVersion && !versions.needsRestart) {
|
|
24
|
+
this.change(previous.id, { phase: "completed" });
|
|
25
|
+
}
|
|
26
|
+
else if (previous.phase === "restarting") {
|
|
27
|
+
this.fail(previous.id, "restarting", `服务已重新启动,当前运行 v${versions.runningVersion}。请核对已安装版本。`);
|
|
28
|
+
}
|
|
29
|
+
else
|
|
30
|
+
this.fail(previous.id, "installing", "上一次安装期间服务退出。请检查已安装版本后重试。");
|
|
31
|
+
}
|
|
32
|
+
save(operation) {
|
|
33
|
+
const temporary = `${this.file}.${process.pid}.tmp`;
|
|
34
|
+
writeFileSync(temporary, JSON.stringify(operation), { encoding: "utf8", mode: 0o600 });
|
|
35
|
+
renameSync(temporary, this.file);
|
|
36
|
+
}
|
|
37
|
+
snapshot() { return { ...checkDiskVersion(this.root), operation: this.operation }; }
|
|
38
|
+
begin(action, targetVersion) {
|
|
39
|
+
const { runningVersion } = checkDiskVersion(this.root);
|
|
40
|
+
const startedAt = new Date().toISOString();
|
|
41
|
+
const operation = { id: randomUUID(), action, phase: action === "update" ? "installing" : "restarting",
|
|
42
|
+
fromVersion: runningVersion, targetVersion, startedAt, changedAt: startedAt };
|
|
43
|
+
this.save(operation);
|
|
44
|
+
this.operation = operation;
|
|
45
|
+
return operation.id;
|
|
46
|
+
}
|
|
47
|
+
change(id, change) {
|
|
48
|
+
if (this.operation?.id !== id || !updateIsActive(this.operation))
|
|
49
|
+
return;
|
|
50
|
+
const operation = { ...this.operation, ...change, changedAt: new Date().toISOString() };
|
|
51
|
+
this.operation = operation;
|
|
52
|
+
try {
|
|
53
|
+
this.save(operation);
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
console.warn("Super Pi: could not save update status:", error);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
restarting(id, targetVersion) {
|
|
60
|
+
if (this.operation?.id !== id || this.operation.phase !== "installing")
|
|
61
|
+
return;
|
|
62
|
+
const operation = { ...this.operation, phase: "restarting", targetVersion, changedAt: new Date().toISOString() };
|
|
63
|
+
this.save(operation);
|
|
64
|
+
this.operation = operation;
|
|
65
|
+
}
|
|
66
|
+
fail(id, step, message, code) {
|
|
67
|
+
this.change(id, { phase: "failed", failure: { step, message, code } });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Quote an argument for a shell on Windows.
|
|
3
|
+
*
|
|
4
|
+
* WHY THIS EXISTS. Node runs `spawn("npm.cmd", args, { shell: true })` by CONCATENATING the
|
|
5
|
+
* arguments into one command line - it does not escape them (Node's own DEP0190 warning says
|
|
6
|
+
* exactly that). So an argument with a space in it arrives at npm in pieces: measured, a
|
|
7
|
+
* `--prefix "C:\\Users\\Some Name\\npm"` reached npm as `C:\\Users\\Some Name` and npm
|
|
8
|
+
* answered `ENOENT ... lstat ... path C:\\Users\\Some`.
|
|
9
|
+
*
|
|
10
|
+
* The npm prefix is wherever the user's npm lives, so on a machine whose home directory has a
|
|
11
|
+
* space in it the update - and the CLI's update - would fail with a path that looks truncated.
|
|
12
|
+
* Quoting here is what makes the argument survive.
|
|
13
|
+
*/
|
|
14
|
+
export declare function quoteForShell(value: string): string;
|
|
15
|
+
/** The arguments to hand to npm, quoted for the shell when the shell is what runs them. */
|
|
16
|
+
export declare function shellArgs(args: string[]): string[];
|
|
17
|
+
export declare function installedPackageName(rootDir?: string): "@evandu/super-pi" | "@evandu/super-pi-server";
|
|
18
|
+
/** Check the release registry with a package-sized timeout and bounded retries for transient failures. */
|
|
19
|
+
export declare function fetchLatestPackageVersion(): Promise<string>;
|
|
20
|
+
/**
|
|
21
|
+
* Return npm's install arguments for the package that is actually running.
|
|
22
|
+
*
|
|
23
|
+
* Pi can keep Super Pi in its own npm prefix, separate from the user's normal
|
|
24
|
+
* global npm prefix. A bare `npm install -g` therefore may update a different
|
|
25
|
+
* copy while this process keeps serving the old one. Installed package roots
|
|
26
|
+
* contain `node_modules/@evandu/super-pi`; walking back to the prefix makes the
|
|
27
|
+
* update target unambiguous. Source checkouts retain the global fallback.
|
|
28
|
+
*
|
|
29
|
+
* `-g` is not optional. Without it npm reads `--prefix <dir>` as a PROJECT
|
|
30
|
+
* directory, recomputes `<dir>/node_modules` as that project's dependency
|
|
31
|
+
* tree, and every package in there that is unrelated to this one counts as
|
|
32
|
+
* extraneous - so npm removes it. A global prefix holds the user's global
|
|
33
|
+
* CLIs (npm itself included), so one update would wipe them all. With `-g`
|
|
34
|
+
* npm adds the package to the global tree and leaves everything else alone.
|
|
35
|
+
*
|
|
36
|
+
* The fetch options are there because the tarball is ~32 MB: npm's default
|
|
37
|
+
* 5 minute timeout fails on a slow line and the update looks broken.
|
|
38
|
+
*/
|
|
39
|
+
export declare function packageUpdateArgs(rootDir: string, version?: string, platform?: NodeJS.Platform): {
|
|
40
|
+
args: string[];
|
|
41
|
+
prefix?: string;
|
|
42
|
+
};
|
|
43
|
+
/** Claims the update slot. False when an update is already running. */
|
|
44
|
+
export declare function claimUpdateSlot(): boolean;
|
|
45
|
+
/** Releases the update slot, so the next update can start. */
|
|
46
|
+
export declare function releaseUpdateSlot(): void;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
import { npmInstallPrefix } from "../../bin/npm-prefix.js";
|
|
4
|
+
/**
|
|
5
|
+
* Quote an argument for a shell on Windows.
|
|
6
|
+
*
|
|
7
|
+
* WHY THIS EXISTS. Node runs `spawn("npm.cmd", args, { shell: true })` by CONCATENATING the
|
|
8
|
+
* arguments into one command line - it does not escape them (Node's own DEP0190 warning says
|
|
9
|
+
* exactly that). So an argument with a space in it arrives at npm in pieces: measured, a
|
|
10
|
+
* `--prefix "C:\\Users\\Some Name\\npm"` reached npm as `C:\\Users\\Some Name` and npm
|
|
11
|
+
* answered `ENOENT ... lstat ... path C:\\Users\\Some`.
|
|
12
|
+
*
|
|
13
|
+
* The npm prefix is wherever the user's npm lives, so on a machine whose home directory has a
|
|
14
|
+
* space in it the update - and the CLI's update - would fail with a path that looks truncated.
|
|
15
|
+
* Quoting here is what makes the argument survive.
|
|
16
|
+
*/
|
|
17
|
+
export function quoteForShell(value) {
|
|
18
|
+
return `"${String(value).replace(/"/g, '""')}"`;
|
|
19
|
+
}
|
|
20
|
+
/** The arguments to hand to npm, quoted for the shell when the shell is what runs them. */
|
|
21
|
+
export function shellArgs(args) {
|
|
22
|
+
return process.platform === "win32" ? args.map(quoteForShell) : args;
|
|
23
|
+
}
|
|
24
|
+
export function installedPackageName(rootDir = resolve(import.meta.dirname, "../..")) {
|
|
25
|
+
try {
|
|
26
|
+
if (JSON.parse(readFileSync(join(rootDir, "package.json"), "utf8")).name === "@evandu/super-pi-server")
|
|
27
|
+
return "@evandu/super-pi-server";
|
|
28
|
+
}
|
|
29
|
+
catch { }
|
|
30
|
+
return /[\\/]super-pi-server[\\/]?$/.test(rootDir) ? "@evandu/super-pi-server" : "@evandu/super-pi";
|
|
31
|
+
}
|
|
32
|
+
/** Check the release registry with a package-sized timeout and bounded retries for transient failures. */
|
|
33
|
+
export async function fetchLatestPackageVersion() {
|
|
34
|
+
let lastError;
|
|
35
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
36
|
+
try {
|
|
37
|
+
const response = await fetch(`https://registry.npmjs.org/${installedPackageName()}/latest`, { signal: AbortSignal.timeout(20_000) });
|
|
38
|
+
if (!response.ok)
|
|
39
|
+
throw new Error(`npm registry HTTP ${response.status}`);
|
|
40
|
+
const payload = await response.json();
|
|
41
|
+
if (typeof payload.version !== "string" || !/^\d+\.\d+\.\d+(?:-[a-z0-9.-]+)?(?:\+[a-z0-9.-]+)?$/iu.test(payload.version)) {
|
|
42
|
+
throw new Error("npm registry did not report a valid version");
|
|
43
|
+
}
|
|
44
|
+
return payload.version;
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
lastError = error;
|
|
48
|
+
if (attempt < 2)
|
|
49
|
+
await new Promise((resolve) => setTimeout(resolve, 750 * (attempt + 1)));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
throw lastError instanceof Error ? lastError : new Error("npm registry request failed");
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Return npm's install arguments for the package that is actually running.
|
|
56
|
+
*
|
|
57
|
+
* Pi can keep Super Pi in its own npm prefix, separate from the user's normal
|
|
58
|
+
* global npm prefix. A bare `npm install -g` therefore may update a different
|
|
59
|
+
* copy while this process keeps serving the old one. Installed package roots
|
|
60
|
+
* contain `node_modules/@evandu/super-pi`; walking back to the prefix makes the
|
|
61
|
+
* update target unambiguous. Source checkouts retain the global fallback.
|
|
62
|
+
*
|
|
63
|
+
* `-g` is not optional. Without it npm reads `--prefix <dir>` as a PROJECT
|
|
64
|
+
* directory, recomputes `<dir>/node_modules` as that project's dependency
|
|
65
|
+
* tree, and every package in there that is unrelated to this one counts as
|
|
66
|
+
* extraneous - so npm removes it. A global prefix holds the user's global
|
|
67
|
+
* CLIs (npm itself included), so one update would wipe them all. With `-g`
|
|
68
|
+
* npm adds the package to the global tree and leaves everything else alone.
|
|
69
|
+
*
|
|
70
|
+
* The fetch options are there because the tarball is ~32 MB: npm's default
|
|
71
|
+
* 5 minute timeout fails on a slow line and the update looks broken.
|
|
72
|
+
*/
|
|
73
|
+
export function packageUpdateArgs(rootDir, version, platform = process.platform) {
|
|
74
|
+
const prefix = npmInstallPrefix(rootDir, platform);
|
|
75
|
+
// THE VERSION THE USER CONFIRMED. The check reads the registry directly while npm resolves
|
|
76
|
+
// "latest" from its own cache, so the two can disagree: the dialog promises vX and the
|
|
77
|
+
// install silently lands on vY. Pinning the version the dialog showed removes that gap.
|
|
78
|
+
const name = installedPackageName(rootDir);
|
|
79
|
+
const target = version ? `${name}@${version}` : name;
|
|
80
|
+
if (prefix !== undefined) {
|
|
81
|
+
return { prefix, args: ["install", "-g", "--prefix", prefix, "--fetch-timeout=900000", "--fetch-retries=8", "--fetch-retry-maxtimeout=120000", target] };
|
|
82
|
+
}
|
|
83
|
+
return { args: ["install", "-g", "--fetch-timeout=900000", "--fetch-retries=8", "--fetch-retry-maxtimeout=120000", target] };
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* ONE UPDATE AT A TIME.
|
|
87
|
+
*
|
|
88
|
+
* Two `npm install` runs against the same global prefix at once fight each other over the same
|
|
89
|
+
* files, and the second one can report a failure that belongs to the first. The slot is claimed
|
|
90
|
+
* before the install starts and released when it ends, so a second request is refused instead of
|
|
91
|
+
* racing the first.
|
|
92
|
+
*/
|
|
93
|
+
let updateInFlight = false;
|
|
94
|
+
/** Claims the update slot. False when an update is already running. */
|
|
95
|
+
export function claimUpdateSlot() {
|
|
96
|
+
if (updateInFlight)
|
|
97
|
+
return false;
|
|
98
|
+
updateInFlight = true;
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
/** Releases the update slot, so the next update can start. */
|
|
102
|
+
export function releaseUpdateSlot() {
|
|
103
|
+
updateInFlight = false;
|
|
104
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { MessageImage, PiImageContent } from "./types.js";
|
|
2
|
+
/** 5MB maximum decoded image payload, matching the source-file size ceiling in files.ts. */
|
|
3
|
+
export declare const MAX_UPLOAD_BYTES: number;
|
|
4
|
+
/** 10MB maximum HTTP request body to safely handle base64 overhead before JSON parsing. */
|
|
5
|
+
export declare const MAX_UPLOAD_BODY_BYTES: number;
|
|
6
|
+
/**
|
|
7
|
+
* Strict image MIME type allowlist.
|
|
8
|
+
* SVGs are explicitly excluded because rendered SVGs can execute arbitrary JavaScript (XSS).
|
|
9
|
+
*/
|
|
10
|
+
export declare const ALLOWED_IMAGE_MIMES: Set<string>;
|
|
11
|
+
export declare function extForMime(mime: string): string;
|
|
12
|
+
export declare function mimeForExt(ext: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* Sniffs magic bytes to verify that buffer contents match the declared image MIME type.
|
|
15
|
+
* This prevents masquerading non-image files or dangerous content under image extensions.
|
|
16
|
+
*/
|
|
17
|
+
export declare function validateImageMagic(buffer: Buffer, mimeType: string): boolean;
|
|
18
|
+
export type UploadRecord = {
|
|
19
|
+
id: string;
|
|
20
|
+
conversationId: string;
|
|
21
|
+
name: string;
|
|
22
|
+
mimeType: string;
|
|
23
|
+
bytes: number;
|
|
24
|
+
url: string;
|
|
25
|
+
};
|
|
26
|
+
export declare function saveUpload(uploadsDir: string, conversationId: string, clientName: string | undefined, mimeType: string, base64Data: string): Promise<UploadRecord>;
|
|
27
|
+
export declare function findUploadFile(uploadsDir: string, conversationId: string | undefined, filenameOrId: string): Promise<{
|
|
28
|
+
filePath: string;
|
|
29
|
+
mimeType: string;
|
|
30
|
+
size: number;
|
|
31
|
+
} | null>;
|
|
32
|
+
export declare function deleteConversationUploads(uploadsDir: string, conversationId: string): Promise<void>;
|
|
33
|
+
/** Gives each fork its own copy of the local images referenced by its history. */
|
|
34
|
+
export declare function copyForkUploads(uploadsDir: string, conversationId: string, forkedId: string, images: MessageImage[]): Promise<void>;
|
|
35
|
+
export declare function loadConversationImages(uploadsDir: string, conversationId: string, images: MessageImage[]): Promise<PiImageContent[]>;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { copyFile, mkdir, readdir, readFile, realpath, rm, stat, writeFile } from "node:fs/promises";
|
|
3
|
+
import { basename, extname, isAbsolute, join, relative, sep } from "node:path";
|
|
4
|
+
/** 5MB maximum decoded image payload, matching the source-file size ceiling in files.ts. */
|
|
5
|
+
export const MAX_UPLOAD_BYTES = 5 * 1024 * 1024;
|
|
6
|
+
/** 10MB maximum HTTP request body to safely handle base64 overhead before JSON parsing. */
|
|
7
|
+
export const MAX_UPLOAD_BODY_BYTES = 10 * 1024 * 1024;
|
|
8
|
+
/**
|
|
9
|
+
* Strict image MIME type allowlist.
|
|
10
|
+
* SVGs are explicitly excluded because rendered SVGs can execute arbitrary JavaScript (XSS).
|
|
11
|
+
*/
|
|
12
|
+
export const ALLOWED_IMAGE_MIMES = new Set([
|
|
13
|
+
"image/png",
|
|
14
|
+
"image/jpeg",
|
|
15
|
+
"image/gif",
|
|
16
|
+
"image/webp",
|
|
17
|
+
]);
|
|
18
|
+
export function extForMime(mime) {
|
|
19
|
+
switch (mime) {
|
|
20
|
+
case "image/png": return ".png";
|
|
21
|
+
case "image/jpeg": return ".jpg";
|
|
22
|
+
case "image/gif": return ".gif";
|
|
23
|
+
case "image/webp": return ".webp";
|
|
24
|
+
default: return "";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export function mimeForExt(ext) {
|
|
28
|
+
switch (ext.toLowerCase()) {
|
|
29
|
+
case ".png": return "image/png";
|
|
30
|
+
case ".jpg":
|
|
31
|
+
case ".jpeg": return "image/jpeg";
|
|
32
|
+
case ".gif": return "image/gif";
|
|
33
|
+
case ".webp": return "image/webp";
|
|
34
|
+
default: return "application/octet-stream";
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Sniffs magic bytes to verify that buffer contents match the declared image MIME type.
|
|
39
|
+
* This prevents masquerading non-image files or dangerous content under image extensions.
|
|
40
|
+
*/
|
|
41
|
+
export function validateImageMagic(buffer, mimeType) {
|
|
42
|
+
if (buffer.length < 8)
|
|
43
|
+
return false;
|
|
44
|
+
switch (mimeType) {
|
|
45
|
+
case "image/png":
|
|
46
|
+
return buffer[0] === 0x89 && buffer[1] === 0x50 && buffer[2] === 0x4e && buffer[3] === 0x47 &&
|
|
47
|
+
buffer[4] === 0x0d && buffer[5] === 0x0a && buffer[6] === 0x1a && buffer[7] === 0x0a;
|
|
48
|
+
case "image/jpeg":
|
|
49
|
+
return buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff;
|
|
50
|
+
case "image/gif":
|
|
51
|
+
return buffer[0] === 0x47 && buffer[1] === 0x49 && buffer[2] === 0x46 && buffer[3] === 0x38 &&
|
|
52
|
+
(buffer[4] === 0x37 || buffer[4] === 0x39) && buffer[5] === 0x61;
|
|
53
|
+
case "image/webp":
|
|
54
|
+
return buffer.length >= 12 &&
|
|
55
|
+
buffer.toString("ascii", 0, 4) === "RIFF" &&
|
|
56
|
+
buffer.toString("ascii", 8, 12) === "WEBP";
|
|
57
|
+
default:
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export async function saveUpload(uploadsDir, conversationId, clientName, mimeType, base64Data) {
|
|
62
|
+
if (!ALLOWED_IMAGE_MIMES.has(mimeType)) {
|
|
63
|
+
throw new Error(`不支持的图片格式: ${mimeType}。仅支持 PNG、JPEG、GIF 和 WebP`);
|
|
64
|
+
}
|
|
65
|
+
const buffer = Buffer.from(base64Data, "base64");
|
|
66
|
+
if (buffer.length === 0) {
|
|
67
|
+
throw new Error("上传的图片数据为空");
|
|
68
|
+
}
|
|
69
|
+
if (buffer.length > MAX_UPLOAD_BYTES) {
|
|
70
|
+
throw new Error(`图片大小(${(buffer.length / 1024 / 1024).toFixed(1)}MB)超过 5MB 上限`);
|
|
71
|
+
}
|
|
72
|
+
if (!validateImageMagic(buffer, mimeType)) {
|
|
73
|
+
throw new Error("图片数据与声明的 MIME 类型不符或文件损坏");
|
|
74
|
+
}
|
|
75
|
+
const id = crypto.randomUUID();
|
|
76
|
+
const ext = extForMime(mimeType);
|
|
77
|
+
const filename = `${id}${ext}`;
|
|
78
|
+
const conversationDir = join(uploadsDir, conversationId);
|
|
79
|
+
await mkdir(conversationDir, { recursive: true });
|
|
80
|
+
const targetPath = join(conversationDir, filename);
|
|
81
|
+
await writeFile(targetPath, buffer);
|
|
82
|
+
const cleanName = (clientName?.trim() || filename).slice(0, 120);
|
|
83
|
+
const meta = {
|
|
84
|
+
id,
|
|
85
|
+
conversationId,
|
|
86
|
+
name: cleanName,
|
|
87
|
+
mimeType,
|
|
88
|
+
bytes: buffer.length,
|
|
89
|
+
createdAt: new Date().toISOString(),
|
|
90
|
+
};
|
|
91
|
+
await writeFile(join(conversationDir, `${id}.meta.json`), JSON.stringify(meta), "utf8");
|
|
92
|
+
return {
|
|
93
|
+
id,
|
|
94
|
+
conversationId,
|
|
95
|
+
name: cleanName,
|
|
96
|
+
mimeType,
|
|
97
|
+
bytes: buffer.length,
|
|
98
|
+
url: `/api/upload/${conversationId}/${filename}`,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
export async function findUploadFile(uploadsDir, conversationId, filenameOrId) {
|
|
102
|
+
if (!existsSync(uploadsDir))
|
|
103
|
+
return null;
|
|
104
|
+
// Syntactic safety check: reject path traversal
|
|
105
|
+
if (filenameOrId.includes("..") || filenameOrId.includes("/") || filenameOrId.includes("\\"))
|
|
106
|
+
return null;
|
|
107
|
+
if (conversationId) {
|
|
108
|
+
if (conversationId.includes("..") || conversationId.includes("/") || conversationId.includes("\\"))
|
|
109
|
+
return null;
|
|
110
|
+
const directPath = join(uploadsDir, conversationId, filenameOrId);
|
|
111
|
+
if (existsSync(directPath)) {
|
|
112
|
+
const s = await stat(directPath).catch(() => null);
|
|
113
|
+
if (s?.isFile()) {
|
|
114
|
+
const mime = mimeForExt(extname(directPath));
|
|
115
|
+
return { filePath: directPath, mimeType: mime, size: s.size };
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const dir = join(uploadsDir, conversationId);
|
|
119
|
+
if (existsSync(dir)) {
|
|
120
|
+
const entries = await readdir(dir).catch(() => []);
|
|
121
|
+
for (const entry of entries) {
|
|
122
|
+
if (!entry.endsWith(".meta.json") && (entry === filenameOrId || entry.startsWith(`${filenameOrId}.`))) {
|
|
123
|
+
const fp = join(dir, entry);
|
|
124
|
+
// A file listed by readdir can be gone by the time stat runs; that is a miss, not a failure.
|
|
125
|
+
const s = await stat(fp).catch(() => null);
|
|
126
|
+
if (s?.isFile()) {
|
|
127
|
+
return { filePath: fp, mimeType: mimeForExt(extname(fp)), size: s.size };
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
// Deliberately NO cross-conversation fallback here: an upload URL carries its own conversation id, and
|
|
134
|
+
// serving one conversation's request from another conversation's directory would leak images across
|
|
135
|
+
// conversations. A miss is the caller's 404, not a wider search.
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
export async function deleteConversationUploads(uploadsDir, conversationId) {
|
|
139
|
+
if (conversationId.includes("..") || conversationId.includes("/") || conversationId.includes("\\"))
|
|
140
|
+
return;
|
|
141
|
+
const dir = join(uploadsDir, conversationId);
|
|
142
|
+
await rm(dir, { recursive: true, force: true }).catch(() => { });
|
|
143
|
+
}
|
|
144
|
+
/** Gives each fork its own copy of the local images referenced by its history. */
|
|
145
|
+
export async function copyForkUploads(uploadsDir, conversationId, forkedId, images) {
|
|
146
|
+
const root = await realpath(uploadsDir);
|
|
147
|
+
const target = join(root, forkedId);
|
|
148
|
+
await mkdir(target);
|
|
149
|
+
for (const id of new Set(images.map((image) => image.id))) {
|
|
150
|
+
const file = await findUploadFile(root, conversationId, id);
|
|
151
|
+
if (!file)
|
|
152
|
+
throw new Error("无法找到历史消息的附件,分叉未保存");
|
|
153
|
+
const source = await realpath(file.filePath);
|
|
154
|
+
const scoped = relative(join(root, conversationId), source);
|
|
155
|
+
if (!scoped || scoped === ".." || scoped.startsWith(`..${sep}`) || isAbsolute(scoped)) {
|
|
156
|
+
throw new Error("历史附件越出了上传目录,分叉未保存");
|
|
157
|
+
}
|
|
158
|
+
await copyFile(source, join(target, basename(file.filePath)));
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
export async function loadConversationImages(uploadsDir, conversationId, images) {
|
|
162
|
+
const result = [];
|
|
163
|
+
for (const img of images) {
|
|
164
|
+
const found = await findUploadFile(uploadsDir, conversationId, img.id);
|
|
165
|
+
if (found) {
|
|
166
|
+
const buf = await readFile(found.filePath);
|
|
167
|
+
if (buf.length > 0) {
|
|
168
|
+
result.push({
|
|
169
|
+
type: "image",
|
|
170
|
+
data: buf.toString("base64"),
|
|
171
|
+
mimeType: img.mimeType || found.mimeType,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return result;
|
|
177
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ServerVersionInfo } from "./types.js";
|
|
2
|
+
export declare const UNKNOWN_VERSION = "0.0.0-unknown";
|
|
3
|
+
/**
|
|
4
|
+
* Reads and validates the version string from a package.json located in the given directory.
|
|
5
|
+
*/
|
|
6
|
+
export declare function readPackageVersion(dir?: string): string | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* Resolves the version of Super Pi itself by searching relative to this module's location.
|
|
9
|
+
* In both development (`src/server/`) and production build (`dist/server/`), two levels up
|
|
10
|
+
* is the repository / package root containing package.json.
|
|
11
|
+
*/
|
|
12
|
+
export declare function resolveSelfVersion(fromDir?: string): string | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* Initializes the version captured when this server process booted up.
|
|
15
|
+
* If the provided rootDir fails to yield a valid package.json version,
|
|
16
|
+
* attempts to self-heal using the module's own location before falling back to UNKNOWN_VERSION.
|
|
17
|
+
*/
|
|
18
|
+
export declare function initRunningVersion(rootDir: string, selfFromDir?: string): string;
|
|
19
|
+
/**
|
|
20
|
+
* Returns the currently running version captured in memory.
|
|
21
|
+
*/
|
|
22
|
+
export declare function getRunningVersion(): string;
|
|
23
|
+
/**
|
|
24
|
+
* Resets the in-memory running version, primarily for testing purposes.
|
|
25
|
+
*/
|
|
26
|
+
export declare function resetRunningVersion(toVersion?: string): string;
|
|
27
|
+
/**
|
|
28
|
+
* Checks whether the package.json version on disk has been updated beyond
|
|
29
|
+
* the version currently executing in this process.
|
|
30
|
+
* If rootDir fails to yield a valid package.json version, attempts to self-heal
|
|
31
|
+
* using the module's own location.
|
|
32
|
+
*/
|
|
33
|
+
export declare function checkDiskVersion(rootDir: string, selfFromDir?: string): ServerVersionInfo;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join, resolve } from "node:path";
|
|
3
|
+
export const UNKNOWN_VERSION = "0.0.0-unknown";
|
|
4
|
+
/**
|
|
5
|
+
* Reads and validates the version string from a package.json located in the given directory.
|
|
6
|
+
*/
|
|
7
|
+
export function readPackageVersion(dir) {
|
|
8
|
+
if (!dir || typeof dir !== "string")
|
|
9
|
+
return undefined;
|
|
10
|
+
try {
|
|
11
|
+
const pkgPath = join(dir, "package.json");
|
|
12
|
+
if (!existsSync(pkgPath))
|
|
13
|
+
return undefined;
|
|
14
|
+
const raw = readFileSync(pkgPath, "utf8");
|
|
15
|
+
const pkg = JSON.parse(raw);
|
|
16
|
+
if (pkg && typeof pkg.version === "string" && pkg.version.trim().length > 0) {
|
|
17
|
+
return pkg.version.trim();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
// Read or JSON parse error - return undefined to allow self-healing / fallback
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Resolves the version of Super Pi itself by searching relative to this module's location.
|
|
27
|
+
* In both development (`src/server/`) and production build (`dist/server/`), two levels up
|
|
28
|
+
* is the repository / package root containing package.json.
|
|
29
|
+
*/
|
|
30
|
+
export function resolveSelfVersion(fromDir = import.meta.dirname) {
|
|
31
|
+
if (!fromDir || typeof fromDir !== "string")
|
|
32
|
+
return undefined;
|
|
33
|
+
// Primary path: two levels up from server module directory (src/server or dist/server)
|
|
34
|
+
const canonicalRoot = resolve(fromDir, "../..");
|
|
35
|
+
const canonicalVersion = readPackageVersion(canonicalRoot);
|
|
36
|
+
if (canonicalVersion)
|
|
37
|
+
return canonicalVersion;
|
|
38
|
+
// Secondary path: walk upwards to locate the nearest directory containing Super Pi's package.json
|
|
39
|
+
let current = resolve(fromDir);
|
|
40
|
+
while (true) {
|
|
41
|
+
try {
|
|
42
|
+
const pkgPath = join(current, "package.json");
|
|
43
|
+
if (existsSync(pkgPath)) {
|
|
44
|
+
const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
|
|
45
|
+
if (pkg && pkg.name === "@evandu/super-pi" && typeof pkg.version === "string" && pkg.version.trim()) {
|
|
46
|
+
return pkg.version.trim();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
// Continue searching upwards
|
|
52
|
+
}
|
|
53
|
+
const parent = dirname(current);
|
|
54
|
+
if (parent === current)
|
|
55
|
+
break;
|
|
56
|
+
current = parent;
|
|
57
|
+
}
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
let runningVersion = resolveSelfVersion() ?? UNKNOWN_VERSION;
|
|
61
|
+
/**
|
|
62
|
+
* Initializes the version captured when this server process booted up.
|
|
63
|
+
* If the provided rootDir fails to yield a valid package.json version,
|
|
64
|
+
* attempts to self-heal using the module's own location before falling back to UNKNOWN_VERSION.
|
|
65
|
+
*/
|
|
66
|
+
export function initRunningVersion(rootDir, selfFromDir = import.meta.dirname) {
|
|
67
|
+
const direct = readPackageVersion(rootDir);
|
|
68
|
+
if (direct) {
|
|
69
|
+
runningVersion = direct;
|
|
70
|
+
return runningVersion;
|
|
71
|
+
}
|
|
72
|
+
const self = resolveSelfVersion(selfFromDir);
|
|
73
|
+
if (self) {
|
|
74
|
+
runningVersion = self;
|
|
75
|
+
return runningVersion;
|
|
76
|
+
}
|
|
77
|
+
runningVersion = UNKNOWN_VERSION;
|
|
78
|
+
return runningVersion;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Returns the currently running version captured in memory.
|
|
82
|
+
*/
|
|
83
|
+
export function getRunningVersion() {
|
|
84
|
+
return runningVersion;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Resets the in-memory running version, primarily for testing purposes.
|
|
88
|
+
*/
|
|
89
|
+
export function resetRunningVersion(toVersion) {
|
|
90
|
+
runningVersion = toVersion ?? resolveSelfVersion() ?? UNKNOWN_VERSION;
|
|
91
|
+
return runningVersion;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Checks whether the package.json version on disk has been updated beyond
|
|
95
|
+
* the version currently executing in this process.
|
|
96
|
+
* If rootDir fails to yield a valid package.json version, attempts to self-heal
|
|
97
|
+
* using the module's own location.
|
|
98
|
+
*/
|
|
99
|
+
export function checkDiskVersion(rootDir, selfFromDir = import.meta.dirname) {
|
|
100
|
+
const diskVersion = readPackageVersion(rootDir) ??
|
|
101
|
+
resolveSelfVersion(selfFromDir) ??
|
|
102
|
+
runningVersion;
|
|
103
|
+
const needsRestart = Boolean(diskVersion !== runningVersion &&
|
|
104
|
+
runningVersion !== UNKNOWN_VERSION &&
|
|
105
|
+
diskVersion !== UNKNOWN_VERSION);
|
|
106
|
+
return {
|
|
107
|
+
runningVersion,
|
|
108
|
+
diskVersion,
|
|
109
|
+
needsRestart,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { TavilyService } from "./tavily.js";
|
|
2
|
+
export declare const WEB_SEARCH_TOOL_NAME = "web_search";
|
|
3
|
+
export declare const WEB_FETCH_TOOL_NAME = "web_fetch";
|
|
4
|
+
export declare const MAX_WEB_REQUEST_BYTES: number;
|
|
5
|
+
export type WebSearchResult = {
|
|
6
|
+
title: string;
|
|
7
|
+
url: string;
|
|
8
|
+
snippet: string;
|
|
9
|
+
};
|
|
10
|
+
export type WebFetchResult = {
|
|
11
|
+
title: string;
|
|
12
|
+
url: string;
|
|
13
|
+
text: string;
|
|
14
|
+
truncated: boolean;
|
|
15
|
+
contentType: string;
|
|
16
|
+
};
|
|
17
|
+
/** Parse DuckDuckGo Lite result rows and unwrap their destination URLs. */
|
|
18
|
+
export declare function parseDuckDuckGoResults(html: string, limit?: number): WebSearchResult[];
|
|
19
|
+
export declare class WebService {
|
|
20
|
+
private readonly tavily?;
|
|
21
|
+
constructor(tavily?: TavilyService | undefined);
|
|
22
|
+
search(query: string, limit?: number, signal?: AbortSignal): Promise<{
|
|
23
|
+
query: string;
|
|
24
|
+
results: WebSearchResult[];
|
|
25
|
+
}>;
|
|
26
|
+
fetch(url: string, signal?: AbortSignal): Promise<WebFetchResult>;
|
|
27
|
+
}
|