@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,120 @@
|
|
|
1
|
+
/** Versioned, renderer-independent contract for structured presentation tools. */
|
|
2
|
+
export declare const PRESENTATION_SPEC_VERSION: 1;
|
|
3
|
+
export declare const PRESENTATION_LIMITS: {
|
|
4
|
+
readonly maxSlides: 200;
|
|
5
|
+
readonly maxElementsPerSlide: 500;
|
|
6
|
+
readonly maxTextCharacters: 100000;
|
|
7
|
+
readonly maxTableCells: 10000;
|
|
8
|
+
readonly maxChartPoints: 20000;
|
|
9
|
+
};
|
|
10
|
+
export type PresentationSpec = {
|
|
11
|
+
version: typeof PRESENTATION_SPEC_VERSION;
|
|
12
|
+
metadata?: {
|
|
13
|
+
title?: string;
|
|
14
|
+
subject?: string;
|
|
15
|
+
author?: string;
|
|
16
|
+
company?: string;
|
|
17
|
+
language?: string;
|
|
18
|
+
};
|
|
19
|
+
canvas: {
|
|
20
|
+
width: number;
|
|
21
|
+
height: number;
|
|
22
|
+
};
|
|
23
|
+
theme?: PresentationTheme;
|
|
24
|
+
slides: PresentationSlide[];
|
|
25
|
+
};
|
|
26
|
+
export type PresentationTheme = {
|
|
27
|
+
colors?: Record<string, string>;
|
|
28
|
+
fonts?: {
|
|
29
|
+
heading?: string;
|
|
30
|
+
body?: string;
|
|
31
|
+
mono?: string;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
export type PresentationSlide = {
|
|
35
|
+
id: string;
|
|
36
|
+
title?: string;
|
|
37
|
+
notes?: string;
|
|
38
|
+
background?: string;
|
|
39
|
+
elements: PresentationElement[];
|
|
40
|
+
};
|
|
41
|
+
type Frame = {
|
|
42
|
+
x: number;
|
|
43
|
+
y: number;
|
|
44
|
+
width: number;
|
|
45
|
+
height: number;
|
|
46
|
+
};
|
|
47
|
+
type BaseElement = Frame & {
|
|
48
|
+
id: string;
|
|
49
|
+
altText?: string;
|
|
50
|
+
name?: string;
|
|
51
|
+
hidden?: boolean;
|
|
52
|
+
rotation?: number;
|
|
53
|
+
};
|
|
54
|
+
export type TextElement = BaseElement & {
|
|
55
|
+
type: "text";
|
|
56
|
+
text: string;
|
|
57
|
+
fontFamily?: string;
|
|
58
|
+
fontSize?: number;
|
|
59
|
+
color?: string;
|
|
60
|
+
bold?: boolean;
|
|
61
|
+
italic?: boolean;
|
|
62
|
+
align?: "left" | "center" | "right" | "justify";
|
|
63
|
+
verticalAlign?: "top" | "middle" | "bottom";
|
|
64
|
+
link?: string;
|
|
65
|
+
};
|
|
66
|
+
export type ShapeElement = BaseElement & {
|
|
67
|
+
type: "shape";
|
|
68
|
+
shape: "rectangle" | "roundRectangle" | "ellipse" | "triangle" | "chevron";
|
|
69
|
+
fill?: string;
|
|
70
|
+
stroke?: string;
|
|
71
|
+
strokeWidth?: number;
|
|
72
|
+
};
|
|
73
|
+
export type LineElement = BaseElement & {
|
|
74
|
+
type: "line";
|
|
75
|
+
stroke?: string;
|
|
76
|
+
strokeWidth?: number;
|
|
77
|
+
dash?: "solid" | "dash" | "dot";
|
|
78
|
+
beginArrow?: boolean;
|
|
79
|
+
endArrow?: boolean;
|
|
80
|
+
};
|
|
81
|
+
export type ImageElement = BaseElement & {
|
|
82
|
+
type: "image";
|
|
83
|
+
source: string;
|
|
84
|
+
fit?: "contain" | "cover" | "stretch";
|
|
85
|
+
link?: string;
|
|
86
|
+
};
|
|
87
|
+
export type TableElement = BaseElement & {
|
|
88
|
+
type: "table";
|
|
89
|
+
rows: Array<Array<string | number | boolean | null>>;
|
|
90
|
+
headerRows?: number;
|
|
91
|
+
};
|
|
92
|
+
export type ChartElement = BaseElement & {
|
|
93
|
+
type: "chart";
|
|
94
|
+
chart: "bar" | "column" | "line" | "area" | "pie" | "doughnut" | "scatter";
|
|
95
|
+
categories?: string[];
|
|
96
|
+
series: Array<{
|
|
97
|
+
name: string;
|
|
98
|
+
values: Array<number | null>;
|
|
99
|
+
xValues?: Array<number | null>;
|
|
100
|
+
}>;
|
|
101
|
+
showLegend?: boolean;
|
|
102
|
+
showValues?: boolean;
|
|
103
|
+
};
|
|
104
|
+
export type PresentationElement = TextElement | ShapeElement | LineElement | ImageElement | TableElement | ChartElement;
|
|
105
|
+
export type PresentationDiagnostic = {
|
|
106
|
+
code: "invalid_type" | "invalid_value" | "unsupported_version" | "duplicate_id" | "limit_exceeded" | "out_of_bounds";
|
|
107
|
+
severity: "error" | "warning";
|
|
108
|
+
path: string;
|
|
109
|
+
message: string;
|
|
110
|
+
};
|
|
111
|
+
export type PresentationValidationResult = {
|
|
112
|
+
valid: true;
|
|
113
|
+
spec: PresentationSpec;
|
|
114
|
+
diagnostics: PresentationDiagnostic[];
|
|
115
|
+
} | {
|
|
116
|
+
valid: false;
|
|
117
|
+
diagnostics: PresentationDiagnostic[];
|
|
118
|
+
};
|
|
119
|
+
export declare function validatePresentationSpec(input: unknown): PresentationValidationResult;
|
|
120
|
+
export {};
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/** Versioned, renderer-independent contract for structured presentation tools. */
|
|
2
|
+
export const PRESENTATION_SPEC_VERSION = 1;
|
|
3
|
+
export const PRESENTATION_LIMITS = {
|
|
4
|
+
maxSlides: 200,
|
|
5
|
+
maxElementsPerSlide: 500,
|
|
6
|
+
maxTextCharacters: 100_000,
|
|
7
|
+
maxTableCells: 10_000,
|
|
8
|
+
maxChartPoints: 20_000,
|
|
9
|
+
};
|
|
10
|
+
const ELEMENT_TYPES = new Set(["text", "shape", "line", "image", "table", "chart"]);
|
|
11
|
+
const ID_PATTERN = /^[A-Za-z][A-Za-z0-9._-]{0,127}$/;
|
|
12
|
+
export function validatePresentationSpec(input) {
|
|
13
|
+
const diagnostics = [];
|
|
14
|
+
const error = (code, path, message) => diagnostics.push({ code, severity: "error", path, message });
|
|
15
|
+
if (!isObject(input)) {
|
|
16
|
+
error("invalid_type", "$", "Presentation spec must be an object.");
|
|
17
|
+
return { valid: false, diagnostics };
|
|
18
|
+
}
|
|
19
|
+
if (input.version !== PRESENTATION_SPEC_VERSION) {
|
|
20
|
+
error("unsupported_version", "$.version", `Only presentation spec version ${PRESENTATION_SPEC_VERSION} is supported.`);
|
|
21
|
+
}
|
|
22
|
+
const canvas = input.canvas;
|
|
23
|
+
if (!isObject(canvas))
|
|
24
|
+
error("invalid_type", "$.canvas", "Canvas must be an object.");
|
|
25
|
+
else {
|
|
26
|
+
positiveNumber(canvas.width, "$.canvas.width", error);
|
|
27
|
+
positiveNumber(canvas.height, "$.canvas.height", error);
|
|
28
|
+
}
|
|
29
|
+
if (!Array.isArray(input.slides)) {
|
|
30
|
+
error("invalid_type", "$.slides", "Slides must be an array.");
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
if (input.slides.length > PRESENTATION_LIMITS.maxSlides) {
|
|
34
|
+
error("limit_exceeded", "$.slides", `A deck may contain at most ${PRESENTATION_LIMITS.maxSlides} slides.`);
|
|
35
|
+
}
|
|
36
|
+
const ids = new Set();
|
|
37
|
+
input.slides.forEach((slide, slideIndex) => validateSlide(slide, slideIndex, canvas, ids, error));
|
|
38
|
+
}
|
|
39
|
+
if (input.theme !== undefined)
|
|
40
|
+
validateTheme(input.theme, error);
|
|
41
|
+
return diagnostics.some((item) => item.severity === "error")
|
|
42
|
+
? { valid: false, diagnostics }
|
|
43
|
+
: { valid: true, spec: input, diagnostics };
|
|
44
|
+
}
|
|
45
|
+
function validateSlide(slide, index, canvas, ids, error) {
|
|
46
|
+
const path = `$.slides[${index}]`;
|
|
47
|
+
if (!isObject(slide))
|
|
48
|
+
return error("invalid_type", path, "Slide must be an object.");
|
|
49
|
+
validateId(slide.id, `${path}.id`, ids, error);
|
|
50
|
+
if (!Array.isArray(slide.elements))
|
|
51
|
+
return error("invalid_type", `${path}.elements`, "Elements must be an array.");
|
|
52
|
+
if (slide.elements.length > PRESENTATION_LIMITS.maxElementsPerSlide) {
|
|
53
|
+
error("limit_exceeded", `${path}.elements`, `A slide may contain at most ${PRESENTATION_LIMITS.maxElementsPerSlide} elements.`);
|
|
54
|
+
}
|
|
55
|
+
slide.elements.forEach((element, elementIndex) => validateElement(element, `${path}.elements[${elementIndex}]`, canvas, ids, error));
|
|
56
|
+
}
|
|
57
|
+
function validateElement(element, path, canvas, ids, error) {
|
|
58
|
+
if (!isObject(element))
|
|
59
|
+
return error("invalid_type", path, "Element must be an object.");
|
|
60
|
+
validateId(element.id, `${path}.id`, ids, error);
|
|
61
|
+
if (typeof element.type !== "string" || !ELEMENT_TYPES.has(element.type)) {
|
|
62
|
+
error("invalid_value", `${path}.type`, "Element type is not supported.");
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
for (const key of ["x", "y", "width", "height"])
|
|
66
|
+
positiveNumber(element[key], `${path}.${key}`, error, key === "x" || key === "y");
|
|
67
|
+
if (isObject(canvas) && finite(element.x) && finite(element.y) && finite(element.width) && finite(element.height)
|
|
68
|
+
&& finite(canvas.width) && finite(canvas.height)
|
|
69
|
+
&& (element.x < 0 || element.y < 0 || element.x + element.width > canvas.width || element.y + element.height > canvas.height)) {
|
|
70
|
+
error("out_of_bounds", path, "Element frame must fit within the canvas.");
|
|
71
|
+
}
|
|
72
|
+
if (element.type === "text") {
|
|
73
|
+
if (typeof element.text !== "string")
|
|
74
|
+
error("invalid_type", `${path}.text`, "Text must be a string.");
|
|
75
|
+
else if (element.text.length > PRESENTATION_LIMITS.maxTextCharacters)
|
|
76
|
+
error("limit_exceeded", `${path}.text`, "Text is too long.");
|
|
77
|
+
}
|
|
78
|
+
else if (element.type === "image") {
|
|
79
|
+
if (typeof element.source !== "string" || !element.source.trim())
|
|
80
|
+
error("invalid_value", `${path}.source`, "Image source is required.");
|
|
81
|
+
if (!element.altText)
|
|
82
|
+
error("invalid_value", `${path}.altText`, "Images require alt text.");
|
|
83
|
+
}
|
|
84
|
+
else if (element.type === "table") {
|
|
85
|
+
if (!Array.isArray(element.rows))
|
|
86
|
+
error("invalid_type", `${path}.rows`, "Table rows must be an array.");
|
|
87
|
+
else if (element.rows.reduce((sum, row) => sum + (Array.isArray(row) ? row.length : 0), 0) > PRESENTATION_LIMITS.maxTableCells)
|
|
88
|
+
error("limit_exceeded", `${path}.rows`, "Table contains too many cells.");
|
|
89
|
+
}
|
|
90
|
+
else if (element.type === "chart")
|
|
91
|
+
validateChart(element, path, error);
|
|
92
|
+
}
|
|
93
|
+
function validateChart(chart, path, error) {
|
|
94
|
+
if (!Array.isArray(chart.series) || chart.series.length === 0)
|
|
95
|
+
return error("invalid_value", `${path}.series`, "Chart requires at least one series.");
|
|
96
|
+
let points = 0;
|
|
97
|
+
for (const [index, series] of chart.series.entries()) {
|
|
98
|
+
if (!isObject(series) || typeof series.name !== "string" || !Array.isArray(series.values)) {
|
|
99
|
+
error("invalid_type", `${path}.series[${index}]`, "Series requires a name and numeric values.");
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (series.values.some((value) => value !== null && !finite(value)))
|
|
103
|
+
error("invalid_value", `${path}.series[${index}].values`, "Chart values must be finite numbers or null.");
|
|
104
|
+
points += series.values.length;
|
|
105
|
+
}
|
|
106
|
+
if (points > PRESENTATION_LIMITS.maxChartPoints)
|
|
107
|
+
error("limit_exceeded", `${path}.series`, "Chart contains too many data points.");
|
|
108
|
+
}
|
|
109
|
+
function validateTheme(theme, error) {
|
|
110
|
+
if (!isObject(theme))
|
|
111
|
+
return error("invalid_type", "$.theme", "Theme must be an object.");
|
|
112
|
+
if (theme.colors !== undefined && (!isObject(theme.colors) || Object.values(theme.colors).some((value) => typeof value !== "string"))) {
|
|
113
|
+
error("invalid_type", "$.theme.colors", "Theme colors must map token names to color strings.");
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
function validateId(value, path, ids, error) {
|
|
117
|
+
if (typeof value !== "string" || !ID_PATTERN.test(value))
|
|
118
|
+
return error("invalid_value", path, "ID must start with a letter and contain only letters, digits, dot, underscore, or hyphen.");
|
|
119
|
+
if (ids.has(value))
|
|
120
|
+
error("duplicate_id", path, `ID '${value}' is already used in this deck.`);
|
|
121
|
+
ids.add(value);
|
|
122
|
+
}
|
|
123
|
+
function positiveNumber(value, path, error, allowZero = false) {
|
|
124
|
+
if (!finite(value) || (allowZero ? value < 0 : value <= 0))
|
|
125
|
+
error("invalid_value", path, allowZero ? "Value must be a non-negative finite number." : "Value must be a positive finite number.");
|
|
126
|
+
}
|
|
127
|
+
function finite(value) { return typeof value === "number" && Number.isFinite(value); }
|
|
128
|
+
function isObject(value) { return typeof value === "object" && value !== null && !Array.isArray(value); }
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ProviderNetworkSettings } from "../shared/provider-network.js";
|
|
2
|
+
type Credential = {
|
|
3
|
+
type?: string;
|
|
4
|
+
env?: Record<string, string>;
|
|
5
|
+
[key: string]: unknown;
|
|
6
|
+
};
|
|
7
|
+
export declare class ProviderNetworkInputError extends Error {
|
|
8
|
+
}
|
|
9
|
+
export declare const hasStoredCredential: (value?: {
|
|
10
|
+
type?: string;
|
|
11
|
+
key?: unknown;
|
|
12
|
+
}) => boolean;
|
|
13
|
+
/** Only connection settings are returned; credentials and proxy userinfo never reach the browser. */
|
|
14
|
+
export declare function publicProviderNetwork(credential?: Credential): ProviderNetworkSettings;
|
|
15
|
+
export declare function updateProviderNetwork(current: Credential | undefined, input: unknown): Credential | undefined;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
export class ProviderNetworkInputError extends Error {
|
|
2
|
+
}
|
|
3
|
+
export const hasStoredCredential = (value) => Boolean(value && (value.type === "oauth" || typeof value.key === "string" && value.key.length > 0));
|
|
4
|
+
const PROXY_KEYS = ["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY"];
|
|
5
|
+
const read = (env, key) => {
|
|
6
|
+
const value = env[key.toLowerCase()] ?? env[key];
|
|
7
|
+
return typeof value === "string" ? value : "";
|
|
8
|
+
};
|
|
9
|
+
function visibleAddress(value) {
|
|
10
|
+
if (!value)
|
|
11
|
+
return { address: "", hasAuth: false };
|
|
12
|
+
try {
|
|
13
|
+
const url = new URL(value);
|
|
14
|
+
const hasAuth = Boolean(url.username || url.password || url.search || url.hash || url.pathname !== "/");
|
|
15
|
+
url.username = "";
|
|
16
|
+
url.password = "";
|
|
17
|
+
url.search = "";
|
|
18
|
+
url.hash = "";
|
|
19
|
+
url.pathname = "/";
|
|
20
|
+
return { address: url.href, hasAuth };
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return { address: "", hasAuth: true };
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/** Only connection settings are returned; credentials and proxy userinfo never reach the browser. */
|
|
27
|
+
export function publicProviderNetwork(credential) {
|
|
28
|
+
const env = credential?.env ?? {};
|
|
29
|
+
const fallback = read(env, "ALL_PROXY");
|
|
30
|
+
const http = visibleAddress(read(env, "HTTP_PROXY") || fallback);
|
|
31
|
+
const https = visibleAddress(read(env, "HTTPS_PROXY") || fallback);
|
|
32
|
+
const noProxy = read(env, "NO_PROXY");
|
|
33
|
+
const httpAddress = read(env, "HTTP_PROXY") || fallback;
|
|
34
|
+
const httpsAddress = read(env, "HTTPS_PROXY") || fallback;
|
|
35
|
+
return { mode: noProxy.trim() === "*" ? "direct" : http.address || https.address || http.hasAuth || https.hasAuth ? "proxy" : "inherit",
|
|
36
|
+
httpProxy: http.address, httpsProxy: https.address, noProxy, httpProxyHasAuth: http.hasAuth, httpsProxyHasAuth: https.hasAuth,
|
|
37
|
+
httpsSameAsHttp: Boolean(httpAddress && (!httpsAddress || httpsAddress === httpAddress)) };
|
|
38
|
+
}
|
|
39
|
+
function address(value) {
|
|
40
|
+
if (value === undefined)
|
|
41
|
+
return undefined;
|
|
42
|
+
if (typeof value !== "string" || value.length > 4096)
|
|
43
|
+
throw new ProviderNetworkInputError("代理地址必须是最多 4096 字符的 HTTP / HTTPS URL");
|
|
44
|
+
const text = value.trim();
|
|
45
|
+
if (!text)
|
|
46
|
+
return "";
|
|
47
|
+
try {
|
|
48
|
+
const parsed = new URL(text);
|
|
49
|
+
if (!["http:", "https:"].includes(parsed.protocol) || !parsed.hostname || parsed.hash || parsed.search || parsed.pathname !== "/")
|
|
50
|
+
throw new Error();
|
|
51
|
+
return parsed.href;
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
throw new ProviderNetworkInputError("代理地址仅支持 HTTP / HTTPS,例如 http://127.0.0.1:7890");
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export function updateProviderNetwork(current, input) {
|
|
58
|
+
const value = input;
|
|
59
|
+
if (!value || !["inherit", "proxy", "direct"].includes(value.mode ?? ""))
|
|
60
|
+
throw new ProviderNetworkInputError("请选择继承环境、单独代理或直连");
|
|
61
|
+
if (value.mode === "proxy" && value.noProxy !== undefined && (typeof value.noProxy !== "string" || value.noProxy.length > 4096 || /[\r\n\u0000]/.test(value.noProxy)))
|
|
62
|
+
throw new ProviderNetworkInputError("绕过代理的地址须为最多 4096 字符的逗号分隔列表");
|
|
63
|
+
const before = current?.env ?? {};
|
|
64
|
+
const env = { ...before };
|
|
65
|
+
for (const key of PROXY_KEYS) {
|
|
66
|
+
delete env[key];
|
|
67
|
+
delete env[key.toLowerCase()];
|
|
68
|
+
}
|
|
69
|
+
if (value.mode === "direct") {
|
|
70
|
+
const keepAddress = (key) => { try {
|
|
71
|
+
return address(read(before, key) || read(before, "ALL_PROXY")) || "";
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return "";
|
|
75
|
+
} };
|
|
76
|
+
// Keep usable addresses for switching back, while explicitly blocking inherited proxy values.
|
|
77
|
+
env.HTTP_PROXY = env.http_proxy = keepAddress("HTTP_PROXY");
|
|
78
|
+
env.HTTPS_PROXY = env.https_proxy = keepAddress("HTTPS_PROXY");
|
|
79
|
+
env.ALL_PROXY = env.all_proxy = "";
|
|
80
|
+
env.NO_PROXY = env.no_proxy = "*";
|
|
81
|
+
}
|
|
82
|
+
if (value.mode === "proxy") {
|
|
83
|
+
const fallback = read(before, "ALL_PROXY");
|
|
84
|
+
const http = address(value.httpProxy ?? (read(before, "HTTP_PROXY") || fallback)) ?? "";
|
|
85
|
+
const https = address(value.httpsProxy ?? (read(before, "HTTPS_PROXY") || fallback)) ?? "";
|
|
86
|
+
if (!http && !https)
|
|
87
|
+
throw new ProviderNetworkInputError("请填写至少一个 HTTP / HTTPS 代理地址");
|
|
88
|
+
env.HTTP_PROXY = env.http_proxy = http || https;
|
|
89
|
+
env.HTTPS_PROXY = env.https_proxy = https || http;
|
|
90
|
+
const previousBypass = read(before, "NO_PROXY");
|
|
91
|
+
const noProxy = value.noProxy?.trim() ?? (previousBypass === "*" ? "" : previousBypass);
|
|
92
|
+
if (noProxy.length > 4096 || /[\r\n\u0000]/.test(noProxy))
|
|
93
|
+
throw new ProviderNetworkInputError("绕过代理的地址格式不正确");
|
|
94
|
+
if (noProxy === "*")
|
|
95
|
+
throw new ProviderNetworkInputError("全部直连请选择“直连”模式");
|
|
96
|
+
env.NO_PROXY = env.no_proxy = noProxy;
|
|
97
|
+
}
|
|
98
|
+
if (!current && !Object.keys(env).length)
|
|
99
|
+
return undefined;
|
|
100
|
+
const next = { ...(current ?? { type: "api_key" }) };
|
|
101
|
+
if (Object.keys(env).length)
|
|
102
|
+
next.env = env;
|
|
103
|
+
else
|
|
104
|
+
delete next.env;
|
|
105
|
+
return next;
|
|
106
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type SharedProviderProxyRecord = {
|
|
2
|
+
env: Record<string, string>;
|
|
3
|
+
providers: string[];
|
|
4
|
+
excludedProviders: string[];
|
|
5
|
+
};
|
|
6
|
+
export declare function readSharedProviderProxy(agentDir: string): Promise<SharedProviderProxyRecord | undefined>;
|
|
7
|
+
/** A shared address is applied at request time, without touching any provider credential. */
|
|
8
|
+
export declare function sharedProviderProxyEnv(shared: SharedProviderProxyRecord | undefined, providerId: string): Record<string, string> | undefined;
|
|
9
|
+
/** Read only proxy keys; provider secrets must never become child process environment variables. */
|
|
10
|
+
export declare function readProviderProxyEnv(agentDir: string, providerId?: string): Promise<Record<string, string>>;
|
|
11
|
+
type Env = Record<string, string | undefined>;
|
|
12
|
+
/** Apply scoped proxy values without changing the server's process environment. */
|
|
13
|
+
export declare function providerProxyFetch(env: Env | undefined, baseFetch?: typeof fetch): typeof fetch | undefined;
|
|
14
|
+
/** Keep Pi's own cross-process credential lock, and preserve scoped env on OAuth rotation/login. */
|
|
15
|
+
export declare function proxyCredentialStore<T extends object>(store: T): T;
|
|
16
|
+
/** Read provider routing independently of Pi's auth result: OAuth resolution omits credential.env. */
|
|
17
|
+
export declare function installProviderProxy(runtime: unknown, agentDir?: string): Promise<void>;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { installManagedModels } from "./managed-model-adapter.js";
|
|
2
|
+
import { EnvHttpProxyAgent } from "undici";
|
|
3
|
+
import { readFile } from "node:fs/promises";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
6
|
+
export async function readSharedProviderProxy(agentDir) {
|
|
7
|
+
let data;
|
|
8
|
+
try {
|
|
9
|
+
data = JSON.parse(await readFile(join(agentDir, "shared-provider-proxy.json"), "utf8"));
|
|
10
|
+
}
|
|
11
|
+
catch (error) {
|
|
12
|
+
if (error.code === "ENOENT")
|
|
13
|
+
return undefined;
|
|
14
|
+
throw new Error("公共代理配置无法读取,请检查格式和权限");
|
|
15
|
+
}
|
|
16
|
+
if (!data || !data.env || typeof data.env !== "object" || Array.isArray(data.env) || Object.entries(data.env).some(([key, value]) => !/^(http_proxy|https_proxy|all_proxy|no_proxy)$/i.test(key) || typeof value !== "string") || !Array.isArray(data.providers) || !data.providers.every(id => typeof id === "string") || !Array.isArray(data.excludedProviders) || !data.excludedProviders.every(id => typeof id === "string"))
|
|
17
|
+
throw new Error("公共代理配置格式不正确");
|
|
18
|
+
return data;
|
|
19
|
+
}
|
|
20
|
+
/** A shared address is applied at request time, without touching any provider credential. */
|
|
21
|
+
export function sharedProviderProxyEnv(shared, providerId) {
|
|
22
|
+
if (!shared || shared.excludedProviders.includes(providerId) || ["google", "google-vertex"].includes(providerId))
|
|
23
|
+
return undefined;
|
|
24
|
+
if (shared.providers.includes(providerId))
|
|
25
|
+
return shared.env;
|
|
26
|
+
return { HTTP_PROXY: "", http_proxy: "", HTTPS_PROXY: "", https_proxy: "", ALL_PROXY: "", all_proxy: "", NO_PROXY: "*", no_proxy: "*" };
|
|
27
|
+
}
|
|
28
|
+
/** Read only proxy keys; provider secrets must never become child process environment variables. */
|
|
29
|
+
export async function readProviderProxyEnv(agentDir, providerId) {
|
|
30
|
+
const read = async (file) => {
|
|
31
|
+
try {
|
|
32
|
+
return JSON.parse(await readFile(join(agentDir, file), "utf8"));
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
if (error.code === "ENOENT")
|
|
36
|
+
return {};
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
const id = providerId ?? (await read("settings.json")).defaultProvider;
|
|
41
|
+
if (!id)
|
|
42
|
+
return {};
|
|
43
|
+
const env = mergeEnv((await read("auth.json"))[id]?.env, sharedProviderProxyEnv(await readSharedProviderProxy(agentDir), id));
|
|
44
|
+
const result = {};
|
|
45
|
+
for (const name of ["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY"]) {
|
|
46
|
+
const value = env[name.toLowerCase()] ?? env[name];
|
|
47
|
+
if (typeof value === "string") {
|
|
48
|
+
result[name] = value;
|
|
49
|
+
result[name.toLowerCase()] = value;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
const agents = new Map();
|
|
55
|
+
/** Apply scoped proxy values without changing the server's process environment. */
|
|
56
|
+
export function providerProxyFetch(env, baseFetch = globalThis.fetch) {
|
|
57
|
+
if (!env || !["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY", "http_proxy", "https_proxy", "all_proxy", "no_proxy"].some(key => env[key] !== undefined))
|
|
58
|
+
return undefined;
|
|
59
|
+
const value = (key) => env[key.toLowerCase()] ?? env[key] ?? process.env[key.toLowerCase()] ?? process.env[key];
|
|
60
|
+
const allProxy = value("ALL_PROXY");
|
|
61
|
+
const httpProxy = value("HTTP_PROXY") ?? allProxy;
|
|
62
|
+
const httpsProxy = value("HTTPS_PROXY") ?? allProxy ?? httpProxy;
|
|
63
|
+
const noProxy = value("NO_PROXY");
|
|
64
|
+
for (const address of [httpProxy, httpsProxy]) {
|
|
65
|
+
if (!address)
|
|
66
|
+
continue;
|
|
67
|
+
let protocol;
|
|
68
|
+
try {
|
|
69
|
+
protocol = new URL(address).protocol;
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
throw new Error("供应商代理地址必须是合法的 HTTP / HTTPS URL");
|
|
73
|
+
}
|
|
74
|
+
if (protocol !== "http:" && protocol !== "https:")
|
|
75
|
+
throw new Error("供应商代理仅支持 HTTP / HTTPS");
|
|
76
|
+
}
|
|
77
|
+
const key = JSON.stringify([httpProxy, httpsProxy, noProxy]);
|
|
78
|
+
let agent = agents.get(key);
|
|
79
|
+
if (!agent) {
|
|
80
|
+
agent = new EnvHttpProxyAgent({ httpProxy, httpsProxy, noProxy });
|
|
81
|
+
if (agents.size >= 32) {
|
|
82
|
+
const oldest = agents.keys().next().value;
|
|
83
|
+
void agents.get(oldest).close().catch(() => { });
|
|
84
|
+
agents.delete(oldest);
|
|
85
|
+
}
|
|
86
|
+
agents.set(key, agent);
|
|
87
|
+
}
|
|
88
|
+
return (input, init) => baseFetch(input, { ...init, dispatcher: agent });
|
|
89
|
+
}
|
|
90
|
+
const authFetchScope = new AsyncLocalStorage();
|
|
91
|
+
let scopedFetch;
|
|
92
|
+
let baseAuthFetch;
|
|
93
|
+
/** OAuth adapters call global fetch. Route only the current async auth operation through its proxy. */
|
|
94
|
+
async function withAuthProxy(env, operation, providerId) {
|
|
95
|
+
const active = authFetchScope.getStore();
|
|
96
|
+
env = mergeEnv(env, providerId && active?.providerId === providerId ? active.env : undefined);
|
|
97
|
+
if (!Object.keys(env).some(key => /^(http_proxy|https_proxy|all_proxy|no_proxy)$/i.test(key))) {
|
|
98
|
+
if (active && providerId && active.providerId !== providerId)
|
|
99
|
+
return authFetchScope.run({ fetch: baseAuthFetch, providerId, env }, operation);
|
|
100
|
+
return operation();
|
|
101
|
+
}
|
|
102
|
+
if (globalThis.fetch !== scopedFetch) {
|
|
103
|
+
const original = globalThis.fetch;
|
|
104
|
+
baseAuthFetch = original;
|
|
105
|
+
scopedFetch = (input, init) => (authFetchScope.getStore()?.fetch ?? original)(input, init);
|
|
106
|
+
globalThis.fetch = scopedFetch;
|
|
107
|
+
}
|
|
108
|
+
const requestFetch = providerProxyFetch(env, baseAuthFetch);
|
|
109
|
+
return authFetchScope.run({ fetch: requestFetch, providerId, env }, operation);
|
|
110
|
+
}
|
|
111
|
+
function mergeEnv(...layers) {
|
|
112
|
+
const merged = {};
|
|
113
|
+
for (const layer of layers) {
|
|
114
|
+
Object.assign(merged, layer);
|
|
115
|
+
for (const key of ["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY"]) {
|
|
116
|
+
const value = layer?.[key.toLowerCase()] ?? layer?.[key];
|
|
117
|
+
if (value !== undefined)
|
|
118
|
+
merged[key] = merged[key.toLowerCase()] = value;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return merged;
|
|
122
|
+
}
|
|
123
|
+
/** Keep Pi's own cross-process credential lock, and preserve scoped env on OAuth rotation/login. */
|
|
124
|
+
export function proxyCredentialStore(store) {
|
|
125
|
+
return new Proxy(store, {
|
|
126
|
+
get(target, property) {
|
|
127
|
+
const value = Reflect.get(target, property);
|
|
128
|
+
if (property === "modify" && typeof value === "function") {
|
|
129
|
+
return (providerId, modify, options) => value.call(target, providerId, (current) => withAuthProxy(current?.env, async () => {
|
|
130
|
+
const next = await modify(current);
|
|
131
|
+
if (next?.type === "oauth" && current?.env)
|
|
132
|
+
return { ...next, env: { ...current.env, ...next.env } };
|
|
133
|
+
return next;
|
|
134
|
+
}, providerId), options);
|
|
135
|
+
}
|
|
136
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/** Read provider routing independently of Pi's auth result: OAuth resolution omits credential.env. */
|
|
141
|
+
export async function installProviderProxy(runtime, agentDir) {
|
|
142
|
+
await installManagedModels(runtime);
|
|
143
|
+
const target = runtime;
|
|
144
|
+
if (typeof target.getAuth !== "function")
|
|
145
|
+
return;
|
|
146
|
+
let stored = {};
|
|
147
|
+
let shared;
|
|
148
|
+
if (agentDir) {
|
|
149
|
+
try {
|
|
150
|
+
stored = JSON.parse(await readFile(join(agentDir, "auth.json"), "utf8"));
|
|
151
|
+
}
|
|
152
|
+
catch (error) {
|
|
153
|
+
if (error.code !== "ENOENT")
|
|
154
|
+
throw error;
|
|
155
|
+
}
|
|
156
|
+
shared = await readSharedProviderProxy(agentDir);
|
|
157
|
+
}
|
|
158
|
+
const providerId = (model) => typeof model === "string" ? model : model?.provider ?? "";
|
|
159
|
+
const providerEnv = (model) => mergeEnv(stored[providerId(model)]?.env, sharedProviderProxyEnv(shared, providerId(model)));
|
|
160
|
+
const getAuth = target.getAuth.bind(target);
|
|
161
|
+
target.getAuth = (model, options) => {
|
|
162
|
+
const env = mergeEnv(providerEnv(model), options?.env);
|
|
163
|
+
return withAuthProxy(env, async () => {
|
|
164
|
+
const auth = await getAuth(model, Object.keys(env).length ? { ...options, env } : options);
|
|
165
|
+
const resolvedEnv = mergeEnv(providerEnv(model), auth?.env, sharedProviderProxyEnv(shared, providerId(model)), options?.env);
|
|
166
|
+
return auth && Object.keys(resolvedEnv).length ? { ...auth, env: resolvedEnv } : auth;
|
|
167
|
+
}, typeof model === "string" ? model : model?.provider);
|
|
168
|
+
};
|
|
169
|
+
const login = target.login;
|
|
170
|
+
if (typeof login === "function") {
|
|
171
|
+
target.login = (providerId, ...args) => withAuthProxy(providerEnv(providerId), () => login.call(target, providerId, ...args), providerId);
|
|
172
|
+
}
|
|
173
|
+
for (const method of ["stream", "streamSimple", "streamDeferred"]) {
|
|
174
|
+
const original = target[method];
|
|
175
|
+
if (typeof original !== "function")
|
|
176
|
+
continue;
|
|
177
|
+
target[method] = function (model, context, options) {
|
|
178
|
+
// These SDK adapters reject injected fetch implementations; preserve their native transport.
|
|
179
|
+
if (["bedrock-converse-stream", "google-generative-ai", "google-vertex"].includes(model?.api ?? ""))
|
|
180
|
+
return original.call(target, model, context, options);
|
|
181
|
+
const env = mergeEnv(providerEnv(model), options?.env);
|
|
182
|
+
const routed = { ...options, ...(Object.keys(env).length ? { env } : {}) };
|
|
183
|
+
const hasProxy = ["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY"].some(key => env[key]);
|
|
184
|
+
if (model?.api === "openai-codex-responses" && hasProxy && env.NO_PROXY !== "*") {
|
|
185
|
+
if (options?.transport && !["auto", "sse"].includes(options.transport))
|
|
186
|
+
throw new Error("Codex 配置代理时当前仅支持 SSE 传输,请将 transport 设为 sse 或 auto");
|
|
187
|
+
routed.transport = "sse";
|
|
188
|
+
}
|
|
189
|
+
if (options?.fetch)
|
|
190
|
+
return original.call(target, model, context, routed);
|
|
191
|
+
const requestFetch = async (input, init) => {
|
|
192
|
+
const auth = await target.getAuth(model, { env });
|
|
193
|
+
// Provider requests select their own routing, even when launched inside another auth callback.
|
|
194
|
+
const transport = globalThis.fetch === scopedFetch ? baseAuthFetch : globalThis.fetch;
|
|
195
|
+
return (providerProxyFetch(mergeEnv(auth?.env, env), transport) ?? transport)(input, init);
|
|
196
|
+
};
|
|
197
|
+
return original.call(target, model, context, { ...routed, fetch: requestFetch });
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
}
|