@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,510 @@
|
|
|
1
|
+
import { KNOWN_APIS, publicProviders, validateProvider, fetchRemoteModels } from "../providers.js";
|
|
2
|
+
import { readProviderProxyEnv } from "../provider-proxy.js";
|
|
3
|
+
import { piAgentDir } from "../providers.js";
|
|
4
|
+
import { hasStoredCredential } from "../provider-network.js";
|
|
5
|
+
import { thinkingLevelsEndpoint } from "../../shared/thinking-levels.js";
|
|
6
|
+
import { randomUUID } from "node:crypto";
|
|
7
|
+
/**
|
|
8
|
+
* THE PROVIDERS A PERSON ADDED, as a service.
|
|
9
|
+
*
|
|
10
|
+
* The service owns the sequence, and the sequence is the whole point: a provider is not "saved"
|
|
11
|
+
* when it is in Super Pi's registry, it is saved when Pi can use it. So every change goes through
|
|
12
|
+
* the same four steps, in this order:
|
|
13
|
+
*
|
|
14
|
+
* 1. WRITE PI'S FILES. The definitions go into Pi's `models.json` and the key into Pi's
|
|
15
|
+
* `auth.json`, merged into whatever is already there.
|
|
16
|
+
* 2. WRITE SUPER PI'S REGISTRY. It is the truth source, and it is written AFTER Pi's files so
|
|
17
|
+
* that a failure to write them leaves Super Pi claiming nothing it has not done.
|
|
18
|
+
* 3. INVALIDATE THE CATALOG. Pi's model list is cached - the SDK's is remembered for the life of
|
|
19
|
+
* the process and the runtime's has a ten-minute TTL - so a provider added now would not
|
|
20
|
+
* appear in the picker until the cache expired. Ten minutes of a picker that cannot see the
|
|
21
|
+
* model the person just added is exactly the "decorative control" this project refuses to ship.
|
|
22
|
+
* 4. INVALIDATE THE LIVE SESSIONS. A session holds its own `ModelRuntime`, created when the
|
|
23
|
+
* session was built, and that runtime does not know a provider added a minute ago. Pi's
|
|
24
|
+
* refusal to switch to an unknown model is swallowed by the session pool into a warning - the
|
|
25
|
+
* turn then runs on the OLD model while the picker says the new one. So the sessions are made
|
|
26
|
+
* stale instead, and each one is rebuilt at its next turn, which is a moment when throwing the
|
|
27
|
+
* session away costs nothing.
|
|
28
|
+
*/
|
|
29
|
+
export class ProviderService {
|
|
30
|
+
registry;
|
|
31
|
+
runtime;
|
|
32
|
+
broadcaster;
|
|
33
|
+
catalogService;
|
|
34
|
+
store;
|
|
35
|
+
oauthJobs = new Map();
|
|
36
|
+
constructor(registry, runtime, broadcaster, catalogService, store) {
|
|
37
|
+
this.registry = registry;
|
|
38
|
+
this.runtime = runtime;
|
|
39
|
+
this.broadcaster = broadcaster;
|
|
40
|
+
this.catalogService = catalogService;
|
|
41
|
+
this.store = store;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Every provider Pi knows about, as the browser may see it: no keys, only the fact of one.
|
|
45
|
+
*
|
|
46
|
+
* THREE LISTS, ONE VIEW, because this panel is a view of Pi's model configuration and Pi's
|
|
47
|
+
* configuration is in three places:
|
|
48
|
+
* - `models.json` entries (custom providers, whether this app wrote them or the person did);
|
|
49
|
+
* - credentials in `auth.json` with no `models.json` entry (Pi's BUILT-IN providers);
|
|
50
|
+
* - the ones turned off, which are in neither of Pi's files any more but are kept here.
|
|
51
|
+
* A panel built on the first list alone could never answer "turn off my lapsed coding plan",
|
|
52
|
+
* because that provider is a built-in whose only configuration is a credential.
|
|
53
|
+
*/
|
|
54
|
+
async list() {
|
|
55
|
+
const [configured, credentialOnly, disabled] = await Promise.all([
|
|
56
|
+
this.registry.load(),
|
|
57
|
+
this.registry.loadCredentialOnly(),
|
|
58
|
+
this.registry.loadDisabled(),
|
|
59
|
+
]);
|
|
60
|
+
// A built-in's models live in Pi's own catalogue, so the file layer cannot know them. They are
|
|
61
|
+
// filled in from the catalog the panel already receives, which is the only thing that knows what
|
|
62
|
+
// a built-in actually offers - and a provider card with an empty model list would misdescribe a
|
|
63
|
+
// provider that is working perfectly.
|
|
64
|
+
if (credentialOnly.length) {
|
|
65
|
+
try {
|
|
66
|
+
const catalog = await this.runtime.catalog();
|
|
67
|
+
const byProvider = new Map();
|
|
68
|
+
for (const model of catalog.models) {
|
|
69
|
+
const group = byProvider.get(model.provider) ?? [];
|
|
70
|
+
group.push(model);
|
|
71
|
+
byProvider.set(model.provider, group);
|
|
72
|
+
}
|
|
73
|
+
for (const provider of credentialOnly) {
|
|
74
|
+
provider.models = (byProvider.get(provider.id) ?? []).map((model) => ({
|
|
75
|
+
id: model.modelId,
|
|
76
|
+
name: model.label,
|
|
77
|
+
reasoning: model.reasoning,
|
|
78
|
+
imageInput: model.imageInput,
|
|
79
|
+
contextWindow: model.contextWindow,
|
|
80
|
+
}));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
// A catalog that cannot be read leaves a built-in's models unknown, which is the truth - the
|
|
85
|
+
// provider itself is still listed and can still be switched off.
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const offList = disabled.map((entry) => ({
|
|
89
|
+
id: entry.id,
|
|
90
|
+
...(entry.name ? { name: entry.name } : {}),
|
|
91
|
+
baseUrl: typeof entry.modelsEntry?.baseUrl === "string" ? entry.modelsEntry.baseUrl : "",
|
|
92
|
+
api: typeof entry.modelsEntry?.api === "string" ? entry.modelsEntry.api : "",
|
|
93
|
+
hasAuthKey: hasStoredCredential(entry.credential),
|
|
94
|
+
...(entry.modelsEntry ? {} : { builtin: true }),
|
|
95
|
+
disabled: true,
|
|
96
|
+
models: entry.models,
|
|
97
|
+
}));
|
|
98
|
+
const base = publicProviders([...configured, ...credentialOnly, ...offList]);
|
|
99
|
+
const oauth = await this.runtime.oauthProviders?.() ?? [];
|
|
100
|
+
const byId = new Map(base.map((provider) => [provider.id, provider]));
|
|
101
|
+
for (const info of oauth) {
|
|
102
|
+
const provider = byId.get(info.id) ?? {
|
|
103
|
+
id: info.id, name: info.name, baseUrl: "", api: "", hasKey: info.authenticated,
|
|
104
|
+
builtin: true, models: [],
|
|
105
|
+
};
|
|
106
|
+
provider.name = info.name;
|
|
107
|
+
provider.authType = "oauth";
|
|
108
|
+
provider.oauthSubscription = info.isSubscription;
|
|
109
|
+
if (info.loginLabel)
|
|
110
|
+
provider.oauthLoginLabel = info.loginLabel;
|
|
111
|
+
provider.authenticated = info.authenticated;
|
|
112
|
+
provider.hasKey = info.authenticated;
|
|
113
|
+
provider.builtin = true;
|
|
114
|
+
byId.set(info.id, provider);
|
|
115
|
+
}
|
|
116
|
+
return [...byId.values()];
|
|
117
|
+
}
|
|
118
|
+
oauthSnapshot() {
|
|
119
|
+
return [...this.oauthJobs.values()].map(({ answer: _answer, abort: _abort, ...job }) => structuredClone(job));
|
|
120
|
+
}
|
|
121
|
+
async startOAuthLogin(providerId) {
|
|
122
|
+
if (typeof providerId !== "string" || !providerId)
|
|
123
|
+
throw new Error("请选择 Pi 内置的授权供应商。");
|
|
124
|
+
const provider = (await this.runtime.oauthProviders?.() ?? []).find((item) => item.id === providerId);
|
|
125
|
+
if (!provider)
|
|
126
|
+
throw new Error("Pi 当前版本不支持这个供应商的 OAuth 登录。");
|
|
127
|
+
for (const job of this.oauthJobs.values())
|
|
128
|
+
if (job.providerId === providerId && ["starting", "authorizing", "prompt"].includes(job.state))
|
|
129
|
+
this.cancelOAuthLogin(job.id);
|
|
130
|
+
if (this.oauthJobs.size >= 20) {
|
|
131
|
+
for (const [id, job] of this.oauthJobs)
|
|
132
|
+
if (!["starting", "authorizing", "prompt"].includes(job.state)) {
|
|
133
|
+
this.oauthJobs.delete(id);
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
if (this.oauthJobs.size >= 20)
|
|
137
|
+
throw new Error("同时进行的 OAuth 登录过多,请先完成或取消一个登录。");
|
|
138
|
+
}
|
|
139
|
+
const job = { id: randomUUID(), providerId, state: "starting", abort: new AbortController() };
|
|
140
|
+
this.oauthJobs.set(job.id, job);
|
|
141
|
+
void (async () => {
|
|
142
|
+
try {
|
|
143
|
+
await this.runtime.loginOAuth(providerId, {
|
|
144
|
+
signal: job.abort.signal,
|
|
145
|
+
notify: (event) => { job.event = event && typeof event === "object" ? structuredClone(event) : undefined; job.state = event?.type === "auth_url" || event?.type === "device_code" ? "authorizing" : "starting"; },
|
|
146
|
+
prompt: (prompt) => new Promise((resolve, reject) => {
|
|
147
|
+
job.prompt = { type: String(prompt.type ?? "text"), message: String(prompt.message ?? "Pi 需要输入授权信息"),
|
|
148
|
+
...(typeof prompt.placeholder === "string" ? { placeholder: prompt.placeholder } : {}),
|
|
149
|
+
...(Array.isArray(prompt.options) ? { options: prompt.options.map((item) => ({ id: String(item.id), label: String(item.label), ...(item.description ? { description: String(item.description) } : {}) })) } : {}) };
|
|
150
|
+
job.state = "prompt";
|
|
151
|
+
const signal = prompt.signal ?? job.abort.signal;
|
|
152
|
+
const abort = () => { job.answer = undefined; job.prompt = undefined; reject(new Error("登录已取消。")); };
|
|
153
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
154
|
+
job.answer = (value) => { signal.removeEventListener("abort", abort); job.prompt = undefined; job.answer = undefined; resolve(value); };
|
|
155
|
+
if (signal.aborted)
|
|
156
|
+
abort();
|
|
157
|
+
}),
|
|
158
|
+
});
|
|
159
|
+
job.state = "completed";
|
|
160
|
+
job.event = undefined;
|
|
161
|
+
await this.afterChange();
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
job.state = job.abort.signal.aborted ? "cancelled" : "failed";
|
|
165
|
+
job.error = job.abort.signal.aborted ? "登录已取消。" : "Pi OAuth 登录失败,请重试并检查授权页面。";
|
|
166
|
+
job.prompt = undefined;
|
|
167
|
+
job.event = undefined;
|
|
168
|
+
}
|
|
169
|
+
})();
|
|
170
|
+
return job.id;
|
|
171
|
+
}
|
|
172
|
+
answerOAuthPrompt(id, answer) {
|
|
173
|
+
const job = this.oauthJobs.get(String(id));
|
|
174
|
+
if (!job || job.state !== "prompt" || !job.answer || typeof answer !== "string" || answer.length > 8192)
|
|
175
|
+
throw new Error("授权输入已过期,请重新登录。");
|
|
176
|
+
const submit = job.answer;
|
|
177
|
+
submit(answer);
|
|
178
|
+
}
|
|
179
|
+
cancelOAuthLogin(id) {
|
|
180
|
+
const job = this.oauthJobs.get(String(id));
|
|
181
|
+
if (!job || !["starting", "authorizing", "prompt"].includes(job.state))
|
|
182
|
+
return;
|
|
183
|
+
job.state = "cancelled";
|
|
184
|
+
job.abort.abort();
|
|
185
|
+
job.answer?.("");
|
|
186
|
+
job.answer = undefined;
|
|
187
|
+
job.prompt = undefined;
|
|
188
|
+
}
|
|
189
|
+
async logoutOAuth(providerId) {
|
|
190
|
+
if (typeof providerId !== "string" || !(await this.runtime.oauthProviders?.() ?? []).some((item) => item.id === providerId))
|
|
191
|
+
throw new Error("Pi 当前版本不支持这个供应商。");
|
|
192
|
+
await this.runtime.logoutOAuth?.(providerId);
|
|
193
|
+
await this.afterChange([{ provider: providerId }]);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Turns a provider off, then makes every LIVE conversation stop pointing at the models it removed.
|
|
197
|
+
*
|
|
198
|
+
* WHY THE CONVERSATIONS ARE REPAIRED. A conversation's model is copied at birth and never re-read
|
|
199
|
+
* from the workspace - that is the rule. But the model it copied can CEASE TO EXIST, and a
|
|
200
|
+
* conversation holding one does not fail cleanly: Pi runs the turn on its own default, so the
|
|
201
|
+
* picker names one model while another one answers. Measured before this change: removing the
|
|
202
|
+
* model in use left the picker empty (`source=unavailable, models=0`).
|
|
203
|
+
*/
|
|
204
|
+
async disable(providerId) {
|
|
205
|
+
const active = await this.registry.load();
|
|
206
|
+
const known = active.find((provider) => provider.id === providerId);
|
|
207
|
+
let models = known?.models ?? [];
|
|
208
|
+
if (!known) {
|
|
209
|
+
// A built-in has no `models.json` entry, so its list comes from Pi's catalogue - and it has to
|
|
210
|
+
// be captured NOW: once the credential is gone the catalogue no longer offers it, and the
|
|
211
|
+
// "off" card would have nothing to say about what switching it back on brings.
|
|
212
|
+
try {
|
|
213
|
+
const catalog = await this.runtime.catalog();
|
|
214
|
+
if (catalog.source === "pi") {
|
|
215
|
+
models = catalog.models
|
|
216
|
+
.filter((model) => model.provider === providerId)
|
|
217
|
+
.map((model) => ({
|
|
218
|
+
id: model.modelId,
|
|
219
|
+
name: model.label,
|
|
220
|
+
reasoning: model.reasoning,
|
|
221
|
+
imageInput: model.imageInput,
|
|
222
|
+
contextWindow: model.contextWindow,
|
|
223
|
+
}));
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
// Still switchable off with an unknown model list; the record just says less about it.
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
await this.registry.disable(providerId, models);
|
|
231
|
+
await this.afterChange([{ provider: providerId }]);
|
|
232
|
+
return this.list();
|
|
233
|
+
}
|
|
234
|
+
async enable(providerId) {
|
|
235
|
+
await this.registry.enable(providerId);
|
|
236
|
+
await this.afterChange();
|
|
237
|
+
return this.list();
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Repairs Pi's model configuration: the empty fields Pi's schema refuses are dropped, and nothing
|
|
241
|
+
* else in the file is touched.
|
|
242
|
+
*
|
|
243
|
+
* WHY IT IS WORTH A BUTTON. Pi validates the WHOLE `models.json`, so one entry with an empty
|
|
244
|
+
* `baseUrl` takes every custom provider out of every picker - and nothing on screen says so,
|
|
245
|
+
* because the settings panel reads the file leniently and lists them as if they worked. The repair
|
|
246
|
+
* restores the file Pi will accept, which is the difference between "my provider is gone" and
|
|
247
|
+
* "your file had an empty field".
|
|
248
|
+
*/
|
|
249
|
+
async repair() {
|
|
250
|
+
const result = await this.registry.repairModelsConfig();
|
|
251
|
+
if (result.changed)
|
|
252
|
+
await this.afterChange();
|
|
253
|
+
return { fixed: result.fixed, providers: await this.list() };
|
|
254
|
+
}
|
|
255
|
+
async network() { return this.registry.sharedNetwork(await this.list()); }
|
|
256
|
+
async setNetwork(input) {
|
|
257
|
+
const settings = await this.registry.setSharedNetwork(input, await this.list());
|
|
258
|
+
await this.afterChange();
|
|
259
|
+
return settings;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Adds a provider, or adds a model to one, or updates one.
|
|
263
|
+
*
|
|
264
|
+
* ONE CALL, THREE THINGS, because the form collects a provider and a model together and a person
|
|
265
|
+
* saving a second model under the same provider should not have to retype the API address. The
|
|
266
|
+
* merge is by id: the provider's fields are replaced by what the form said, and the model with
|
|
267
|
+
* the same id is replaced while the provider's other models stay.
|
|
268
|
+
*
|
|
269
|
+
* THE KEY IS LEFT ALONE UNLESS THE FORM SAYS OTHERWISE. An empty key field means "I did not type
|
|
270
|
+
* one", not "remove the key": the key is never shown again once it is stored, so an empty field
|
|
271
|
+
* is the normal state of a form for a provider that already has one. `clearKey` is how a person
|
|
272
|
+
* asks for it to go.
|
|
273
|
+
*/
|
|
274
|
+
async set(input) {
|
|
275
|
+
const requested = validateProvider(input);
|
|
276
|
+
const raw = (input ?? {});
|
|
277
|
+
const previous = await this.registry.load();
|
|
278
|
+
const existing = previous.find((provider) => provider.id === requested.id);
|
|
279
|
+
const apiKey = requested.apiKey
|
|
280
|
+
? requested.apiKey
|
|
281
|
+
: (raw.clearKey === true ? undefined : existing?.apiKey);
|
|
282
|
+
// A MODEL WITH THE SAME ID IS REPLACED WHERE IT STANDS, not moved to the end: the list a person
|
|
283
|
+
// sees in the panel is the list they added, and re-ordering it because they edited its first
|
|
284
|
+
// entry would be a change nobody asked for. Models with NEW ids are appended.
|
|
285
|
+
const models = !existing
|
|
286
|
+
? requested.models
|
|
287
|
+
: (() => {
|
|
288
|
+
const endpointChanged = thinkingLevelsEndpoint(existing.baseUrl, existing.api)
|
|
289
|
+
!== thinkingLevelsEndpoint(requested.baseUrl, requested.api);
|
|
290
|
+
const list = existing.models.map((model) => endpointChanged
|
|
291
|
+
? { ...model, thinkingLevelMap: null }
|
|
292
|
+
: model);
|
|
293
|
+
for (const model of requested.models) {
|
|
294
|
+
const inc = endpointChanged && model.thinkingLevelMap === undefined
|
|
295
|
+
? { ...model, thinkingLevelMap: null }
|
|
296
|
+
: model;
|
|
297
|
+
const idx = list.findIndex((m) => m.id === inc.id);
|
|
298
|
+
if (idx < 0) {
|
|
299
|
+
list.push(inc.thinkingLevelMap
|
|
300
|
+
? { ...inc, reasoning: inc.reasoning === undefined ? true : inc.reasoning }
|
|
301
|
+
: inc);
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
const saved = list[idx];
|
|
305
|
+
if (inc.thinkingLevelMap === undefined && saved.thinkingLevelMap !== undefined) {
|
|
306
|
+
// NO NEW LEVELS, AND THERE ARE OLD ONES. The saved levels stay, and everything that
|
|
307
|
+
// goes with them stays: the model's own word about thinking - somebody may have
|
|
308
|
+
// turned it off by hand, and that is not this form's to undo - and the compatibility
|
|
309
|
+
// switches somebody set are not reset by an import that knows nothing about them.
|
|
310
|
+
list[idx] = {
|
|
311
|
+
...inc,
|
|
312
|
+
reasoning: saved.reasoning ?? inc.reasoning,
|
|
313
|
+
thinkingLevelMap: saved.thinkingLevelMap,
|
|
314
|
+
...(saved.supportsDeveloperRole !== undefined ? { supportsDeveloperRole: saved.supportsDeveloperRole } : {}),
|
|
315
|
+
...(saved.supportsReasoningEffort !== undefined ? { supportsReasoningEffort: saved.supportsReasoningEffort } : {}),
|
|
316
|
+
};
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
// A MAP MEANS THE MODEL THINKS - unless somebody already said otherwise, in which case
|
|
320
|
+
// their word wins: the levels are not a licence to overrule it.
|
|
321
|
+
list[idx] = inc.thinkingLevelMap
|
|
322
|
+
? { ...inc, reasoning: inc.reasoning === undefined ? (saved.reasoning ?? true) : inc.reasoning }
|
|
323
|
+
: inc;
|
|
324
|
+
}
|
|
325
|
+
return list;
|
|
326
|
+
})();
|
|
327
|
+
const merged = {
|
|
328
|
+
id: requested.id,
|
|
329
|
+
...(requested.name ? { name: requested.name } : {}),
|
|
330
|
+
baseUrl: requested.baseUrl,
|
|
331
|
+
api: requested.api,
|
|
332
|
+
...(apiKey ? { apiKey } : {}),
|
|
333
|
+
models,
|
|
334
|
+
};
|
|
335
|
+
const next = existing
|
|
336
|
+
? [...previous.filter((provider) => provider.id !== requested.id), merged]
|
|
337
|
+
: [...previous, merged];
|
|
338
|
+
await this.registry.applyToPi(previous, next);
|
|
339
|
+
try {
|
|
340
|
+
await this.registry.save(next);
|
|
341
|
+
}
|
|
342
|
+
catch (error) {
|
|
343
|
+
// The registry is the truth source, so a failure to write it means Super Pi no longer knows
|
|
344
|
+
// what it put into Pi's files. Pi's files are put back the way they were, and the failure is
|
|
345
|
+
// reported rather than swallowed: a registry that disagrees with Pi's files is a state the
|
|
346
|
+
// next write would trip over.
|
|
347
|
+
//
|
|
348
|
+
// THE OLD STATE, EXACTLY. The write that is being undone put the new thinking levels into
|
|
349
|
+
// Pi's file, and the "keep whatever was there" rule would carry them into the rollback - so
|
|
350
|
+
// a model that had NO levels before would keep the ones the failed write gave it. The
|
|
351
|
+
// levels are therefore said out loud (`null` means "this model has none"), which is the same
|
|
352
|
+
// thing the form says when it means it.
|
|
353
|
+
const restore = previous.map((provider) => ({
|
|
354
|
+
...provider,
|
|
355
|
+
models: provider.models.map((model) => ({
|
|
356
|
+
...model,
|
|
357
|
+
thinkingLevelMap: model.thinkingLevelMap ?? null,
|
|
358
|
+
})),
|
|
359
|
+
}));
|
|
360
|
+
await this.registry.applyToPi(next, restore).catch(() => undefined);
|
|
361
|
+
throw error;
|
|
362
|
+
}
|
|
363
|
+
await this.afterChange();
|
|
364
|
+
return this.list();
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Removes a provider for good.
|
|
368
|
+
*
|
|
369
|
+
* THREE KINDS, ONE VERB, because the panel is a view of Pi's model configuration and deleting a
|
|
370
|
+
* provider means the same thing in all three cases:
|
|
371
|
+
* - a provider in `models.json`: its entry and its key go (the original behaviour);
|
|
372
|
+
* - a BUILT-IN with only a credential: its models are Pi's, so the credential is the only thing
|
|
373
|
+
* there is to delete - and deleting it is exactly what makes Pi stop offering it;
|
|
374
|
+
* - one that is already switched off: the remembered copy goes, which is what makes "off"
|
|
375
|
+
* permanent instead of something that would come back months later.
|
|
376
|
+
*/
|
|
377
|
+
async remove(providerId) {
|
|
378
|
+
const previous = await this.registry.load();
|
|
379
|
+
const existing = previous.find((provider) => provider.id === providerId);
|
|
380
|
+
if (!existing) {
|
|
381
|
+
const disabled = (await this.registry.loadDisabled()).some((entry) => entry.id === providerId);
|
|
382
|
+
if (disabled) {
|
|
383
|
+
await this.registry.forgetDisabled(providerId);
|
|
384
|
+
await this.registry.clearNetworkOverride(providerId);
|
|
385
|
+
await this.afterChange();
|
|
386
|
+
return this.list();
|
|
387
|
+
}
|
|
388
|
+
const builtin = (await this.registry.loadCredentialOnly()).some((provider) => provider.id === providerId);
|
|
389
|
+
if (builtin) {
|
|
390
|
+
await this.registry.removeCredential(providerId);
|
|
391
|
+
await this.registry.clearNetworkOverride(providerId);
|
|
392
|
+
await this.afterChange([{ provider: providerId }]);
|
|
393
|
+
return this.list();
|
|
394
|
+
}
|
|
395
|
+
throw new Error(`Pi 的配置里没有供应商「${providerId}」`);
|
|
396
|
+
}
|
|
397
|
+
const next = previous.filter((provider) => provider.id !== providerId);
|
|
398
|
+
await this.registry.applyToPi(previous, next);
|
|
399
|
+
await this.registry.save(next);
|
|
400
|
+
await this.registry.clearNetworkOverride(providerId);
|
|
401
|
+
await this.afterChange([{ provider: providerId }]);
|
|
402
|
+
return this.list();
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Removes ONE model from a provider, leaving the provider and its other models alone.
|
|
406
|
+
*
|
|
407
|
+
* WHY THIS IS NOT JUST A DELETE-AND-RE-ADD. A provider is a group of models sharing one address,
|
|
408
|
+
* one API type and one key, so removing a model must not touch any of those - and the key in
|
|
409
|
+
* particular is not something to re-type. The provider's entry in Pi's file is rewritten with the
|
|
410
|
+
* remaining models, and its credential stays exactly where it was.
|
|
411
|
+
*
|
|
412
|
+
* THE LAST MODEL GOES WITH THE PROVIDER. A provider with no models offers nothing, and Pi's file
|
|
413
|
+
* would keep an entry that can never be selected - so the provider is removed, and with it the
|
|
414
|
+
* credential, because a credential for a provider that no longer exists is a secret left lying in
|
|
415
|
+
* a file for nothing.
|
|
416
|
+
*/
|
|
417
|
+
async removeModel(providerId, modelId) {
|
|
418
|
+
const previous = await this.registry.load();
|
|
419
|
+
const existing = previous.find((provider) => provider.id === providerId);
|
|
420
|
+
if (!existing)
|
|
421
|
+
throw new Error(`Super Pi 没有管理供应商「${providerId}」`);
|
|
422
|
+
const remaining = existing.models.filter((model) => model.id !== modelId);
|
|
423
|
+
if (remaining.length === existing.models.length)
|
|
424
|
+
throw new Error(`供应商「${providerId}」里没有模型「${modelId}」`);
|
|
425
|
+
if (!remaining.length)
|
|
426
|
+
return this.remove(providerId);
|
|
427
|
+
const next = previous.map((provider) => (provider.id === providerId ? { ...provider, models: remaining } : provider));
|
|
428
|
+
await this.registry.applyToPi(previous, next);
|
|
429
|
+
await this.registry.save(next);
|
|
430
|
+
await this.afterChange([{ provider: providerId, modelId }]);
|
|
431
|
+
return this.list();
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* What has to happen after the files are written, for the change to be REAL rather than pending.
|
|
435
|
+
*
|
|
436
|
+
* The catalog is re-read for every live conversation - the same loop the model rules use - rather
|
|
437
|
+
* than broadcast once without a conversation, because a catalog without a conversation is the
|
|
438
|
+
* UNFILTERED one, and handing that to a conversation that has model rules would offer it models
|
|
439
|
+
* those rules forbid.
|
|
440
|
+
*/
|
|
441
|
+
async afterChange(removed = []) {
|
|
442
|
+
this.runtime.invalidateCatalog?.();
|
|
443
|
+
this.runtime.bumpModelEpoch?.();
|
|
444
|
+
await this.repairConversationModels(removed);
|
|
445
|
+
for (const conversation of this.store.snapshot().conversations) {
|
|
446
|
+
if (conversation.archivedAt)
|
|
447
|
+
continue;
|
|
448
|
+
try {
|
|
449
|
+
await this.catalogService.broadcastCatalog(conversation.model, conversation.id);
|
|
450
|
+
}
|
|
451
|
+
catch (error) {
|
|
452
|
+
console.warn(`供应商已保存,刷新会话 ${conversation.id} 的模型目录失败:${String(error)}`);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
// The settings panel's OWN list, which is unfiltered and therefore has to be asked for
|
|
456
|
+
// separately. Without this the person who just added a provider would not see their own model
|
|
457
|
+
// in the default-model dropdown until they closed the window and opened it again.
|
|
458
|
+
await this.catalogService.broadcastAllCatalog?.();
|
|
459
|
+
this.broadcaster.broadcast({ type: "providers.list", providers: await this.list(), apis: [...KNOWN_APIS] });
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Drops a conversation's model when the change removed it.
|
|
463
|
+
*
|
|
464
|
+
* WHY THIS EXISTS AT ALL. A conversation whose model was just deleted is not merely stale - it is
|
|
465
|
+
* WRONG in a way that never surfaces: Pi cannot resolve the model and runs the turn on its own
|
|
466
|
+
* default, so the picker says one thing while another model answers. Clearing the field is the
|
|
467
|
+
* only honest state: the conversation follows Pi's default again, which is what the next turn
|
|
468
|
+
* does anyway.
|
|
469
|
+
*
|
|
470
|
+
* THE LIST IS PASSED IN, NOT DERIVED FROM A CATALOG READ. The caller knows exactly which models it
|
|
471
|
+
* just took away (one model, or a whole provider), so asking Pi what it offers would be a slower
|
|
472
|
+
* and less precise way to learn the same thing - and on a machine where Pi is slow or broken, the
|
|
473
|
+
* broadcast every open tab is waiting for would wait with it.
|
|
474
|
+
*
|
|
475
|
+
* ARCHIVED CONVERSATIONS ARE LEFT ALONE: they are read-only by design, and rewriting a frozen
|
|
476
|
+
* record's stored model would make the archive describe a past that did not happen.
|
|
477
|
+
*/
|
|
478
|
+
async repairConversationModels(removed) {
|
|
479
|
+
if (!removed.length)
|
|
480
|
+
return;
|
|
481
|
+
const gone = new Set(removed.map((item) => `${item.provider}/${item.modelId ?? "*"}`));
|
|
482
|
+
let changed = false;
|
|
483
|
+
for (const conversation of this.store.snapshot().conversations) {
|
|
484
|
+
const model = conversation.model;
|
|
485
|
+
if (conversation.archivedAt || !model)
|
|
486
|
+
continue;
|
|
487
|
+
if (!gone.has(`${model.provider}/${model.modelId}`) && !gone.has(`${model.provider}/*`))
|
|
488
|
+
continue;
|
|
489
|
+
await this.store.clearConversationModel(conversation.id);
|
|
490
|
+
changed = true;
|
|
491
|
+
}
|
|
492
|
+
if (changed)
|
|
493
|
+
this.broadcaster.broadcastState();
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Discovers available models from the remote provider endpoint.
|
|
497
|
+
* If apiKey is omitted but providerId is given, uses the stored key.
|
|
498
|
+
*/
|
|
499
|
+
async fetchModels(input) {
|
|
500
|
+
let key = input.apiKey?.trim();
|
|
501
|
+
if (!key && input.providerId) {
|
|
502
|
+
const all = await this.registry.load();
|
|
503
|
+
const match = all.find((p) => p.id === input.providerId);
|
|
504
|
+
if (match?.apiKey)
|
|
505
|
+
key = match.apiKey;
|
|
506
|
+
}
|
|
507
|
+
const proxyEnv = input.providerId ? await readProviderProxyEnv(piAgentDir(), input.providerId) : undefined;
|
|
508
|
+
return fetchRemoteModels({ baseUrl: input.baseUrl, apiKey: key, api: input.api, proxyEnv });
|
|
509
|
+
}
|
|
510
|
+
}
|