pi-tsgw 0.0.13 → 0.0.16

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.
@@ -0,0 +1,214 @@
1
+ import { createHash } from "node:crypto";
2
+ import { openLocalFile, type LocalFile } from "./local-file.ts";
3
+ import { putBytes } from "./transport.ts";
4
+ import { checkAbort, UploadError } from "./errors.ts";
5
+ import { decodeReceipt, download, encodeReceipt, mcpData, plan, planIdentity, SINGLE_LIMIT, UPLOAD_TOOL, URL_TOOL,
6
+ type Download, type FileMetadata, type Receipt, type Target } from "./protocol.ts";
7
+
8
+ export interface UploadParams {
9
+ path: string;
10
+ contentType?: string;
11
+ expiresIn?: number;
12
+ disposition?: "inline" | "attachment";
13
+ resume?: string;
14
+ }
15
+ export interface UploadResult {
16
+ uploaded: boolean | null;
17
+ uploadStatus: "not-started" | "incomplete" | "unknown" | "uploaded";
18
+ phase: "local" | "prepare" | "upload" | "complete" | "link";
19
+ linkStatus: "not-ready" | "ready" | "failed";
20
+ key?: string;
21
+ uid?: string;
22
+ url?: string;
23
+ expiresAt?: string;
24
+ expiresIn?: number;
25
+ disposition?: "inline" | "attachment";
26
+ fallback?: Download;
27
+ resume?: string;
28
+ error?: { code: string; message: string };
29
+ }
30
+ export interface Progress { phase: UploadResult["phase"]; completedParts: number; totalParts: number }
31
+ export interface UploadDependencies {
32
+ /** 返回原始 MCP CallToolResult。认证和连接由主机负责。 */
33
+ call(tool: string, args: Record<string, unknown>, signal: AbortSignal): Promise<unknown>;
34
+ put?: (target: Target, body: AsyncIterable<Uint8Array> | Uint8Array, signal: AbortSignal) => Promise<void>;
35
+ open?: typeof openLocalFile;
36
+ remoteTimeoutMs?: number;
37
+ onProgress?: (progress: Progress) => void;
38
+ }
39
+
40
+ // 作用范围与服务基于元数据的身份判定一致,包括别名和硬链接。
41
+ // 延迟返回的主机调用结束前持续持锁;这不是分布式锁。
42
+ const active = new Set<string>();
43
+ const lockKey = (metadata: FileMetadata) => createHash("sha256").update(JSON.stringify(metadata)).digest("hex");
44
+
45
+ export async function uploadLocalFile(params: UploadParams, cwd: string, signal: AbortSignal,
46
+ deps: UploadDependencies): Promise<UploadResult> {
47
+ let file: LocalFile | undefined;
48
+ let receipt: Receipt | undefined;
49
+ let key: string | undefined;
50
+ let fallback: Download | undefined;
51
+ const pending = new Set<Promise<unknown>>();
52
+ const state: UploadResult = { uploaded: false, uploadStatus: "not-started", phase: "local", linkStatus: "not-ready" };
53
+ const seconds = params.expiresIn ?? 86400;
54
+ const disposition = params.disposition ?? "inline";
55
+ const put = deps.put ?? putBytes;
56
+ const open = deps.open ?? openLocalFile;
57
+ const notify = (completedParts = 0, totalParts = 0) => {
58
+ // 渲染或进度观察器绝不能改变上传结果。
59
+ try { deps.onProgress?.({ phase: state.phase, completedParts, totalParts }); } catch { /* 仅影响界面 */ }
60
+ };
61
+ const claim = (metadata: FileMetadata) => {
62
+ const candidate = lockKey(metadata);
63
+ if (active.has(candidate)) throw new UploadError("busy", "此文件仍有上传操作正在进行。请等待其停止后再恢复。");
64
+ active.add(candidate);
65
+ key = candidate;
66
+ };
67
+ const call = async (tool: string, args: Record<string, unknown>): Promise<Record<string, unknown>> => {
68
+ checkAbort(signal);
69
+ const controller = new AbortController();
70
+ const combined = AbortSignal.any([signal, controller.signal]);
71
+ const timer = setTimeout(() => controller.abort(), deps.remoteTimeoutMs ?? 330_000);
72
+ let stop: (() => void) | undefined;
73
+ // 主机钩子或连接等待可能忽略取消。限制等待时间,但继续持锁。
74
+ const work = Promise.resolve().then(() => {
75
+ checkAbort(combined);
76
+ return deps.call(tool, args, combined);
77
+ });
78
+ pending.add(work);
79
+ void work.then(() => pending.delete(work), () => pending.delete(work));
80
+ const stopped = new Promise<never>((_, reject) => {
81
+ stop = () => reject(new UploadError(signal.aborted ? "cancelled" : "timeout",
82
+ "远程操作结果未知。不要自动重复此操作。"));
83
+ combined.addEventListener("abort", stop, { once: true });
84
+ if (combined.aborted) stop();
85
+ });
86
+ try { return mcpData(await Promise.race([work, stopped])); }
87
+ finally {
88
+ clearTimeout(timer);
89
+ if (stop) combined.removeEventListener("abort", stop);
90
+ }
91
+ };
92
+ const acceptLink = (link: Download): UploadResult => ({ uploaded: true, uploadStatus: "uploaded",
93
+ phase: "link", linkStatus: "ready", ...link, disposition, ...(receipt?.uid ? { uid: receipt.uid } : {}) });
94
+ const getLink = async (objectKey: string) => {
95
+ const link = download(await call(URL_TOOL, { key: objectKey, expiresIn: seconds, disposition }), objectKey, seconds);
96
+ if (Date.parse(link.expiresAt) <= Date.now()) throw new UploadError("expired-link", "服务返回了已过期的下载链接;未尝试自动重新签名。");
97
+ return link;
98
+ };
99
+ try {
100
+ if (params.resume !== undefined) {
101
+ receipt = decodeReceipt(params.resume);
102
+ state.uploaded = receipt.phase === "link" ? true : null;
103
+ state.uploadStatus = receipt.phase === "link" ? "uploaded" : "unknown";
104
+ }
105
+ checkAbort(signal);
106
+ if (!Number.isSafeInteger(seconds) || seconds < 1 || seconds > 604800 || !["inline", "attachment"].includes(disposition))
107
+ throw new UploadError("invalid-options", "expiresIn 必须是 1 到 604800 之间的整数;disposition 必须是 inline 或 attachment。");
108
+ if (receipt) {
109
+ claim(receipt.metadata);
110
+ state.phase = "link";
111
+ notify();
112
+ try { return acceptLink(await getLink(receipt.key)); }
113
+ catch (error) {
114
+ // 即使对象已不存在,也绝不重放单次 PUT 或已完成的上传。
115
+ if (!(error instanceof UploadError) || error.code !== "not-found" || receipt.mode === "single" || receipt.phase === "link") throw error;
116
+ }
117
+ state.uploaded = false;
118
+ state.uploadStatus = "incomplete";
119
+ }
120
+ file = await open(params.path, cwd, params.contentType ?? receipt?.metadata.contentType);
121
+ if (receipt && (file.fingerprint !== receipt.fingerprint || JSON.stringify(file.metadata) !== JSON.stringify(receipt.metadata)))
122
+ throw new UploadError("file-changed", "文件已与恢复凭条不匹配。不得复用旧分片。");
123
+ if (!receipt) claim(file.metadata);
124
+ // 单次上传使用固定快照;分片上传使用有界流式读取。
125
+ const snapshot = file.metadata.size <= SINGLE_LIMIT ? await file.snapshot(signal) : undefined;
126
+ await file.assertUnchanged();
127
+ state.phase = "prepare";
128
+ // 准备分片上传也可能完成已有的服务端任务。
129
+ if (file.metadata.size > SINGLE_LIMIT) {
130
+ state.uploaded = null;
131
+ state.uploadStatus = "unknown";
132
+ }
133
+ notify();
134
+ const rawPlan = await call(UPLOAD_TOOL, { ...file.metadata });
135
+ const identity = planIdentity(rawPlan, file.metadata.size, receipt);
136
+ receipt = { version: 1, key: identity.key, uid: identity.uid, mode: identity.mode,
137
+ phase: identity.uploaded ? "link" : "upload", fingerprint: file.fingerprint, metadata: file.metadata };
138
+ if (identity.uploaded) {
139
+ state.uploaded = true;
140
+ state.uploadStatus = "uploaded";
141
+ state.phase = "link";
142
+ }
143
+ const prepared = plan(rawPlan, file.metadata.size, receipt);
144
+ fallback = { key: prepared.key, url: prepared.url, expiresAt: prepared.expiresAt, expiresIn: prepared.expiresIn };
145
+ if (!prepared.uploaded) {
146
+ await file.assertUnchanged();
147
+ state.phase = "upload";
148
+ state.uploadStatus = "incomplete";
149
+ const batch = new AbortController();
150
+ const batchSignal = AbortSignal.any([signal, batch.signal]);
151
+ let cursor = 0;
152
+ let completed = 0;
153
+ let failure: unknown;
154
+ const worker = async () => {
155
+ try {
156
+ while (cursor < prepared.uploads.length) {
157
+ checkAbort(batchSignal);
158
+ const target = prepared.uploads[cursor++]!;
159
+ state.uploaded = null;
160
+ state.uploadStatus = "unknown";
161
+ await put(target, snapshot ?? file!.bytes(target.offset, target.size, batchSignal), batchSignal);
162
+ completed++;
163
+ notify(completed, prepared.uploads.length);
164
+ }
165
+ } catch (error) { if (!failure) failure = error; batch.abort(); }
166
+ };
167
+ await Promise.allSettled(Array.from({ length: Math.min(3, prepared.uploads.length) }, worker));
168
+ if (failure) throw failure;
169
+ if (prepared.mode === "multipart") {
170
+ await file.assertUnchanged();
171
+ checkAbort(signal);
172
+ state.phase = "complete";
173
+ receipt.phase = "complete";
174
+ notify(completed, prepared.uploads.length);
175
+ // 只进行一次状态核对或完成调用;绝不循环调用或自动重发分片。
176
+ const completion = await call(UPLOAD_TOOL, { ...file.metadata });
177
+ const completedIdentity = planIdentity(completion, file.metadata.size, receipt);
178
+ if (!completedIdentity.uploaded) {
179
+ receipt.phase = "upload";
180
+ state.uploaded = false;
181
+ state.uploadStatus = "incomplete";
182
+ throw new UploadError("incomplete-upload", "服务仍报告有缺失分片。请显式恢复;未重试任何分片。");
183
+ }
184
+ state.uploaded = true;
185
+ state.uploadStatus = "uploaded";
186
+ state.phase = "link";
187
+ receipt.phase = "link";
188
+ const completedPlan = plan(completion, file.metadata.size, receipt);
189
+ fallback = { key: completedPlan.key, url: completedPlan.url, expiresAt: completedPlan.expiresAt, expiresIn: completedPlan.expiresIn };
190
+ }
191
+ }
192
+ state.uploaded = true;
193
+ state.uploadStatus = "uploaded";
194
+ state.phase = "link";
195
+ receipt.phase = "link";
196
+ notify();
197
+ if (seconds === 86400 && disposition === "inline" && Date.parse(fallback.expiresAt) > Date.now()) return acceptLink(fallback);
198
+ return acceptLink(await getLink(receipt.key));
199
+ } catch (error) {
200
+ const safe = error instanceof UploadError ? error : new UploadError("operation-failed",
201
+ "上传操作失败。已隐藏原始错误;不要自动重试。");
202
+ return { ...state, linkStatus: state.phase === "link" ? "failed" : "not-ready",
203
+ ...(receipt ? { key: receipt.key, uid: receipt.uid, resume: encodeReceipt(receipt) } : {}),
204
+ ...(state.uploaded === true && fallback && Date.parse(fallback.expiresAt) > Date.now() ? { fallback } : {}),
205
+ error: { code: safe.code, message: safe.message } };
206
+ } finally {
207
+ await file?.close().catch(() => {});
208
+ if (key) {
209
+ const held = key;
210
+ if (pending.size === 0) active.delete(held);
211
+ else void Promise.allSettled([...pending]).then(() => active.delete(held));
212
+ }
213
+ }
214
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pi-tsgw",
3
- "version": "0.0.13",
4
- "description": "Pi extension for the TOSSP AIH gateway: per-vendor model catalog and thinking operations for openai-completions, openai-responses, anthropic-messages, and google-generative-ai wire protocols.",
3
+ "version": "0.0.16",
4
+ "description": "面向 TOSSP AIH gateway 的 Pi 扩展:按厂商维护模型目录与思考策略,支持 openai-completions、openai-responses、anthropic-messages 和 google-generative-ai 协议。",
5
5
  "type": "module",
6
6
  "keywords": [
7
7
  "pi",
@@ -18,18 +18,24 @@
18
18
  "pi": {
19
19
  "extensions": [
20
20
  "./extensions/index.ts"
21
+ ],
22
+ "skills": [
23
+ "./skills"
21
24
  ]
22
25
  },
23
26
  "scripts": {
24
- "test": "tsc -p tsconfig.test.json && node dist/extensions/index.test.js && node dist/extensions/status.test.js && node dist/extensions/status-integration.test.js && node dist/extensions/models/gateway-catalog-metadata.test.js && node dist/extensions/models/operations.test.js && node dist/extensions/models/catalog.test.js && node dist/extensions/models/web-search.test.js && node dist/extensions/models/gateway-catalog.test.js && node dist/extensions/models/gateway-catalog-isolation.test.js && node dist/extensions/models/vendor-strategies.test.js && node dist/extensions/host-integration.test.js"
27
+ "test": "tsc -p tsconfig.test.json && node dist/extensions/index.test.js && node dist/extensions/status.test.js && node dist/extensions/search.test.js && node dist/extensions/search-proxy.test.js && node dist/extensions/search-host-integration.test.js && node dist/extensions/status-integration.test.js && node dist/extensions/models/gateway-catalog-metadata.test.js && node dist/extensions/models/operations.test.js && node dist/extensions/models/catalog.test.js && node dist/extensions/models/web-search.test.js && node dist/extensions/models/gateway-catalog.test.js && node dist/extensions/models/gateway-catalog-isolation.test.js && node dist/extensions/models/vendor-strategies.test.js && node dist/extensions/host-integration.test.js && node dist/extensions/mcp.test.js && node dist/extensions/mcp-host-integration.test.js && node dist/extensions/skills.test.js && node dist/extensions/upload/protocol.test.js && node dist/extensions/upload/schema.test.js && node dist/extensions/upload/uploader.test.js && node dist/extensions/upload/transport.test.js && node dist/extensions/upload/host-integration.test.js && node dist/extensions/upload/readiness-host.test.js",
28
+ "test:upload:live": "tsc -p tsconfig.test.json && node dist/extensions/upload/live.test.js"
25
29
  },
26
30
  "peerDependencies": {
27
31
  "@earendil-works/pi-coding-agent": ">=0.82.0",
28
32
  "typebox": "*"
29
33
  },
30
34
  "devDependencies": {
31
- "@earendil-works/pi-coding-agent": "^1.1.0",
32
- "typebox": "^1.3.11",
35
+ "@earendil-works/pi-ai": "~1.2.0",
36
+ "@earendil-works/pi-coding-agent": "~1.2.0",
37
+ "@earendil-works/pi-mcp": "~1.2.0",
38
+ "typebox": "^1.3.37",
33
39
  "typescript": "^7.0.2"
34
40
  }
35
41
  }
@@ -0,0 +1,50 @@
1
+ ---
2
+ name: delegate-model-selection
3
+ description: 当本技能存在时,应更倾向于把适合委派的任务编排为子代理背景执行。本技能定义编排中的模型与思考深度选择,应尽量遵循;具体任务拆分、并行方式与升级策略由模型结合现场自行判断。
4
+ license: MIT
5
+ compatibility: 需要宿主提供子代理工具及可用的 tsgw 模型。
6
+ ---
7
+
8
+ # 子代理编排与模型选型
9
+
10
+ 当本技能存在时,应更倾向于把适合委派的任务编排为子代理背景执行,并尽量遵循本技能的模型与思考深度定义。具体编排由模型根据任务自行判断。
11
+
12
+ ## 选型参考
13
+
14
+ 结合任务、套餐、速度和费用参考下表,同档顺序不是单纯按评分排序;表格不是白名单或固定派遣路线。
15
+
16
+ 评分为 AA Intelligence Index v4.3.2 原始分;价格单位为美元/百万 tokens,是 API 参考单价而非套餐扣费;思考深度使用 Pi 档位。
17
+
18
+ | 任务等级 | 模型 | 思考深度 | 评分 | 输入价 | 输出价 |
19
+ | --- | --- | --- | ---: | ---: | ---: |
20
+ | 轻量执行 | `MiniMax-M3` | `high` | 29† | 0.30 | 1.20 |
21
+ | 轻量执行 | `gpt-6-luna` | `high` | 33 | 0.10 | 0.50 |
22
+ | 通用工程 | `glm-5.3` | `low` | 34 | 1.18 | 4.13 |
23
+ | 通用工程 | `glm-5.3` | `high` | 待评 | 1.18 | 4.13 |
24
+ | 通用工程 | `glm-5.3` | `max` | 45 | 1.18 | 4.13 |
25
+ | 通用工程 | `gpt-6-luna` | `max` | 38 | 0.10 | 0.50 |
26
+ | 通用工程(低成本备选) | `glm-5.3-flash` | `max` | 42 | 0.118 | 0.413 |
27
+ | 通用工程 | `gpt-6.1-sol` | `medium` | 48 | 2.00 | 10.00 |
28
+ | 按量付费补位 | `deepseek-flash` | `max` | 39 | 0.1475–0.295 | 0.59–1.18 |
29
+ | 复杂任务 | `gpt-6.1-sol` | `high` | 50 | 2.00 | 10.00 |
30
+ | 高难任务 | `gpt-6.1-sol` | `xhigh` | 51 | 2.00 | 10.00 |
31
+ | 高难任务 | `gpt-6-astra` | `high` | 51 | 10.00 | 50.00 |
32
+ | 高难任务 | `gpt-6-astra` | `xhigh` | 52 | 10.00 | 50.00 |
33
+ | 极难任务 | `gpt-6.1-sol` | `max` | 52 | 2.00 | 10.00 |
34
+ | 极难任务 | `gpt-6-astra` | `max` | 53 | 10.00 | 50.00 |
35
+ | 双模型复核专用 | `kimi-k3` | `high` | 待评 | 3.00 | 15.00 |
36
+
37
+ - † MiniMax 的 29 分为模型级参考,Pi high 映射为 adaptive;待评项不借用其他型号或档位的分数。
38
+ - GLM 5.3 三型号始终思考,实际 effort 为 low/high/max;Pi medium 映射 high,xhigh 映射 max。FlashX 可作未定档备选,评分待评,输入/输出参考价为 0.295/1.0325。
39
+ - GPT 价格对应输入 ≤272K,超过后输入 ×2、输出 ×1.5;MiniMax 输入超过 512K 为 0.60/2.40。
40
+ - DeepSeek 采用国内价(2026-10-16 核验,Blinko #604):输入 1–2 元、输出 4–8 元/百万 tokens,按固定 0.1475 USD/CNY 折算;区间为低谷/高峰价。静态目录采用区间高端,不自动按时段切换;高峰为北京时间工作日(不含中国法定节假日)9–12、14–18。
41
+ - GLM 依据 Blinko #929,同样以固定 0.1475 USD/CNY 折算国内人民币标价。其余数值沿用既有选型资料。
42
+
43
+ ## 必要边界
44
+
45
+ - **MiniMax-M3** 只承担步骤明确的小任务。参考使用 Pi 的 high;非 off 档位均映射为 adaptive,不代表不同能力档。adaptive 不是 Pi 档位,此映射不适用于 M3.1 Flash Preview。
46
+ - **deepseek-flash** 作为按量付费补位时使用 max。
47
+ - **Kimi** 不承担长程执行;双模型复核固定为 **Kimi+Astra**,独立审查同一份材料。Kimi 使用 kimi-k3 / high,Astra 深度按任务选择。
48
+ - 用户明确要求、预算、权限及宿主实际能力优先;不可用的模型或档位如实说明。
49
+
50
+ 宿主支持 `Agent.model = provider/modelId:level` 时,例如 `tsgw/MiniMax-M3:high`、`tsgw/glm-5.3:max`;其他宿主使用其实际格式。