pi-tsgw 0.4.2 → 0.6.0

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 CHANGED
@@ -89,7 +89,7 @@ pi install npm:pi-tsgw
89
89
  目录由各厂商的分片(`extensions/models/vendors/`)组装而成,涵盖 11 个厂商系列:
90
90
  DeepSeek、GLM(智谱)、MiMo(小米)、MiniMax、Kimi(Moonshot)、Qwen(阿里巴巴)、
91
91
  GPT(OpenAI)、Gemini(Google)、Claude(Anthropic)、LongCat 和 Grok(xAI)。
92
- 当前静态目录包含 36 个聊天模型 ID(含保留的别名);这不是实时可用模型数量。
92
+ 静态目录会持续调整,不代表网关实时可用模型数量。
93
93
  厂商文件包含文档链接和元数据注意事项。别名映射、输出限制和共享思考策略并未全部
94
94
  针对网关端点逐项验证;公开价格估算不构成计费保证。
95
95
  MiMo 使用 `mimo-v2.6-pro` 和 `mimo-v2.6-flash`;不再注册原有 V2.5 ID。
@@ -97,10 +97,33 @@ MiMo 使用 `mimo-v2.6-pro` 和 `mimo-v2.6-flash`;不再注册原有 V2.5 ID
97
97
 
98
98
  精选目录保留 DeepSeek `deepseek-flash` 和 V4 Pro;GLM 5.2+,以及明确例外
99
99
  `glm-4.5-air`;MiniMax M3+;Kimi K3;GPT 6+;以及 Gemini 3.5+ 和无版本号的
100
- `gemini-flash` / `gemini-pro` 别名。Qwen、MiMo、LongCat、Claude 和 Grok 保留现有注册。
101
- 已移除的 ID 及其专用请求策略不再注册;即使网关仍列出这些 ID,纳入规则也无法恢复它们。
100
+ `gemini-flash` / `gemini-pro` 别名。当前裁剪移除了 Qwen `qwen3.7-max`、`qwen3.6-plus`、
101
+ `qwen3.5-plus`,Claude `claude-fable-5`,以及 Grok `grok-4.20`、`grok-4.20-fast`、
102
+ `grok-4.3-low`、`grok-4.3-medium`、`grok-4.3-high`。Qwen 仅保留 `qwen3.7-plus` 及其
103
+ 完整请求策略,作为厂商最后一个接入参考;它仍须命中网关目录才会登记,并非可绕过网关
104
+ 交集的默认模型。Grok 保留 `grok-4.5` 并新增 `grok-4.7`;Claude 当前仅保留 Opus 和 Sonnet。
105
+ 其他已移除的 ID 及其专用请求策略不再注册;即使网关仍列出这些 ID,纳入规则也无法恢复它们。
102
106
  现有显式模型选择若引用了已移除的 ID,需要更新。
103
107
 
108
+ 新增 `grok-4.7`、`gemini-3.5-flash-lite`、`gemini-3.8-flash-high` 和 `longcat-2.5-preview`:
109
+
110
+ | ID | 协议与端点后缀 | 思考映射 | 2026-10-17 最小真实验收 |
111
+ | --- | --- | --- | --- |
112
+ | `grok-4.7` | Responses,`/v1` | low/medium/high/xhigh;不支持 off,max→xhigh | 文本通过;函数调用未通过;原生搜索无事件/来源,故不加入搜索支持名单 |
113
+ | `gemini-3.5-flash-lite` | Completions,`/v1` | minimal/low/medium/high;off→minimal,xhigh/max→high | 文本、函数调用与默认工具选择下的结果往返通过;显式 tool_choice:none 的结果轮曾超时 |
114
+ | `gemini-3.8-flash-high` | Google,`/gemini/v1beta` | low/medium/high;off/minimal→low,xhigh/max→high | 文本、函数调用与工具结果往返通过 |
115
+ | `longcat-2.5-preview` | Completions,`/v1` | off 关闭,其他档开启;无已确认深度映射 | 上游额度耗尽,推理及工具验收阻塞 |
116
+
117
+ 上述验收仅覆盖短文本与最小工具夹具,不代表全部模态、上下文容量或七档已逐项验证。
118
+ 两款 Gemini 的旧 `/gemini` 路径均不可用;仅新型号按实测选择协议与版本路径,不修改旧型号。
119
+ 3.8 的网关 ID 后缀 `high` 不作为已确认的固定档位契约;规格/价格继承官方基础型号。
120
+ 3.8 采用截至 2026-12-31 的介绍价,2027-01-01 起应复核更新;Grok 4.7 采用输入少于
121
+ 200K 的公开价,达到 200K 后输入、缓存与输出均加倍。LongCat 使用国内限时价并按
122
+ 固定 0.1475 USD/CNY 折算;公开价格只用于估算,不代表网关实际计费。
123
+ Grok 4.7 的官方文本输出不设独立上限,目录以 500K 上下文作为输出容量上界,
124
+ 不代表可同时填满输入与输出。LongCat 2.5 输出上限按逐型号参数页采用 256K;
125
+ 官方快速开始仍写 128K,图像更新说明也与纯文本 API schema 存在差异,当前保守登记文本输入。
126
+
104
127
  Kimi 提供 `kimi-k3` 和网关变体 `kimi-k3-256k`,后者显示为
105
128
  **Kimi K3 (256K)**,上下文窗口为 262,144 个 token。其余元数据、价格估算和思考策略
106
129
  继承自 K3;131,072 个 token 的输出设置与上下文大小彼此独立。
@@ -124,9 +147,23 @@ GLM 5.3 仅支持文本。注册并不证明网关支持推理或工具调用。
124
147
  请保留 `MiniMax-*` 的大小写。网关目录可用时,先与静态目录取交集,再应用过滤。
125
148
  纳入规则无法添加未知或不可用的 ID。
126
149
 
127
- 打开 `/model` 会在后台刷新网关目录,并使用五分钟缓存避免重复请求。
128
- 运行 `/tsgw-refresh` 可绕过缓存立即强制刷新,无需重启或重新加载 Pi。
129
- 刷新失败时,继续使用最近一次成功获取的模型列表。
150
+ 插件启动时复用 **7 天**内的缓存;缓存过期才联网刷新。工具调用复用 **24 小时**内的缓存。
151
+ 宿主额外的非强制后台同步沿用已登记目录,不追加联网请求;需要获取新目录时调用工具
152
+ 或强制刷新。所有入口使用同一份缓存,命中缓存和
153
+ 失败回退均不更新原始获取时间。单次联网超时 **5 秒**;仅启动时无有效作用域缓存
154
+ 允许失败后重试一次,其余情况不自动重试。
155
+ 启动有旧缓存时刷新失败会沿用缓存;无缓存且两次失败时正常加载,但 TSGW 登记零个模型,
156
+ 不回退静态目录。启动失败提示通过宿主 `ui.notify` 发出(Pi Web Toast),提醒择机强制刷新;
157
+ 非 UI 模式仍可使用且保留状态诊断。缺少可解析凭据时不联网、不登记模型。
158
+ 运行 `/tsgw-refresh` 可绕过缓存立即强制刷新,无需重启或重新加载 Pi;成功后可恢复目录。
159
+ 动态刷新失败保留最近已登记的列表。
160
+
161
+ 模型可通过 codemode 调用 `tools.tsgw_refresh_models({})` 自动获取,
162
+ 或 `tools.tsgw_refresh_models({ force: true })` 强制刷新。
163
+ 工具返回 `cacheFilePath`、`count` 与 `storedAt`,随后读取该文件的 `ids` 即为完整网关列表,
164
+ 包括尚未静态登记的型号;不会把完整列表重复塞入工具返回值,也不会自动登记未知型号。
165
+ 工具确认文件符合当前缓存窗口;强制刷新时要求本次写入。取消、刷新失败或未确认落盘时
166
+ 不会把旧文件报成本次刷新成功。
130
167
 
131
168
  内存和磁盘缓存通过 SHA-256 摘要限定在网关根地址与凭据对应的作用域内;不存储明文凭据。
132
169
  磁盘缓存仅有一个槽位,位于 `<agent-dir>/tsgw/models-cache.json`:改变作用域可能需要
@@ -9,7 +9,8 @@ import {
9
9
  import { dirname, resolve } from "node:path";
10
10
 
11
11
  export const DEFAULT_GATEWAY_MODEL_TIMEOUT_MS = 5_000;
12
- export const DEFAULT_GATEWAY_MODEL_CACHE_TTL_MS = 5 * 60 * 1_000;
12
+ export const DEFAULT_GATEWAY_MODEL_CACHE_TTL_MS = 24 * 60 * 60 * 1_000;
13
+ export const STARTUP_GATEWAY_MODEL_CACHE_TTL_MS = 7 * DEFAULT_GATEWAY_MODEL_CACHE_TTL_MS;
13
14
 
14
15
  export type GatewayModelFailureReason =
15
16
  | "http"
@@ -34,6 +35,10 @@ export interface FetchGatewayModelIdsOptions {
34
35
  export interface GetGatewayModelIdsOptions extends FetchGatewayModelIdsOptions {
35
36
  cacheFilePath?: string;
36
37
  force?: boolean;
38
+ /** 各入口独立判断新鲜度,但共用原始获取时间与缓存。 */
39
+ ttlMs?: number;
40
+ /** 仅启动无有效作用域缓存时允许失败后重试一次。 */
41
+ retryWithoutCache?: boolean;
37
42
  }
38
43
 
39
44
  export type FetchGatewayModelIdsResult =
@@ -187,10 +192,9 @@ export function gatewayModelCacheScope(baseUrl: string, apiKey: string): string
187
192
  .digest("hex");
188
193
  }
189
194
 
190
- function readGatewayModelCacheEntry(
195
+ function readGatewayModelCacheSnapshot(
191
196
  filePath: string,
192
- scope: string,
193
- ): ModelCacheEntry | undefined {
197
+ ): (ModelCacheEntry & { scope: string }) | undefined {
194
198
  try {
195
199
  const payload: unknown = JSON.parse(readFileSync(filePath, "utf8"));
196
200
  if (!payload || typeof payload !== "object") return undefined;
@@ -200,15 +204,16 @@ function readGatewayModelCacheEntry(
200
204
  ids?: unknown;
201
205
  };
202
206
  if (
203
- !scope || storedScope !== scope ||
207
+ typeof storedScope !== "string" || !storedScope ||
204
208
  typeof storedAt !== "number" ||
205
209
  !Number.isFinite(storedAt) ||
206
210
  storedAt < 0 ||
207
- !Array.isArray(ids) ||
211
+ !Array.isArray(ids) || ids.length === 0 ||
208
212
  !ids.every((id) => typeof id === "string" && id.trim())
209
213
  )
210
214
  return undefined;
211
215
  return {
216
+ scope: storedScope,
212
217
  storedAt,
213
218
  ids: Array.from(new Set(ids.map((id) => id.trim()))),
214
219
  };
@@ -217,6 +222,27 @@ function readGatewayModelCacheEntry(
217
222
  }
218
223
  }
219
224
 
225
+ function readGatewayModelCacheEntry(
226
+ filePath: string,
227
+ scope: string,
228
+ ): ModelCacheEntry | undefined {
229
+ const snapshot = readGatewayModelCacheSnapshot(filePath);
230
+ if (!scope || snapshot?.scope !== scope) return undefined;
231
+ return { storedAt: snapshot.storedAt, ids: snapshot.ids };
232
+ }
233
+
234
+ /** 刷新后的文件摘要;只确认可读性与获取时间,不用于恢复目录或认证作用域。 */
235
+ export function readGatewayModelCacheSummary(
236
+ cacheFilePath: string,
237
+ minimumStoredAt: number,
238
+ ): { cacheFilePath: string; count: number; storedAt: number } | undefined {
239
+ if (!Number.isFinite(minimumStoredAt) || minimumStoredAt < 0) return undefined;
240
+ const snapshot = readGatewayModelCacheSnapshot(cacheFilePath);
241
+ if (!snapshot || !snapshot.ids.length || snapshot.storedAt < minimumStoredAt)
242
+ return undefined;
243
+ return { cacheFilePath, count: snapshot.ids.length, storedAt: snapshot.storedAt };
244
+ }
245
+
220
246
  function isCacheEntryFresh(
221
247
  entry: ModelCacheEntry,
222
248
  now: number,
@@ -289,7 +315,7 @@ function cacheFor(key: string): ModelCache {
289
315
  }
290
316
 
291
317
  /**
292
- * 加载网关模型 ID,使用有效期为五分钟的进程内缓存。
318
+ * 加载网关模型 ID,默认有效期为 24 小时;启动入口可指定 7 天。
293
319
  *
294
320
  * 缓存未过期时直接返回,不进行输入输出。刷新失败时,若有过期缓存则回退使用;
295
321
  * 否则返回获取失败的结果。
@@ -302,9 +328,12 @@ export async function getGatewayModelIds(
302
328
  ): Promise<GetGatewayModelIdsResult> {
303
329
  if (opts.signal?.aborted) return { ok: false, reason: "aborted" };
304
330
  const key = gatewayModelCacheScope(baseUrl, apiKey);
331
+ const ttlMs = opts.ttlMs ?? DEFAULT_GATEWAY_MODEL_CACHE_TTL_MS;
332
+ if (!Number.isFinite(ttlMs) || ttlMs < 0) throw new RangeError("ttlMs 必须是非负有限数值");
305
333
  const cache = cacheFor(key);
306
- if (!opts.force && cache.isFresh(now)) {
307
- const entry = cache.get();
334
+ const memoryEntryBefore = cache.get();
335
+ if (!opts.force && memoryEntryBefore && isCacheEntryFresh(memoryEntryBefore, now, ttlMs)) {
336
+ const entry = memoryEntryBefore;
308
337
  if (entry)
309
338
  return {
310
339
  ok: true,
@@ -321,7 +350,7 @@ export async function getGatewayModelIds(
321
350
  if (
322
351
  !opts.force &&
323
352
  diskEntry &&
324
- isCacheEntryFresh(diskEntry, now, DEFAULT_GATEWAY_MODEL_CACHE_TTL_MS)
353
+ isCacheEntryFresh(diskEntry, now, ttlMs)
325
354
  ) {
326
355
  cache.set(diskEntry.ids, diskEntry.storedAt);
327
356
  return {
@@ -333,7 +362,8 @@ export async function getGatewayModelIds(
333
362
  };
334
363
  }
335
364
 
336
- const requestKey = `${key}\0${opts.force ? "force" : "normal"}`;
365
+ const retry = opts.retryWithoutCache === true && !memoryEntryBefore && !diskEntry;
366
+ const requestKey = `${key}\0${opts.force ? "force" : "normal"}\0${retry}\0${opts.timeoutMs ?? DEFAULT_GATEWAY_MODEL_TIMEOUT_MS}`;
337
367
  // 共享请求无法安全地继承某个调用者的 AbortSignal:较新的调用者仍在等待时,
338
368
  // Pi 可能已替换该次刷新。只有不带信号的调用共享进行中的请求;
339
369
  // 可取消的调用独立发起获取请求。
@@ -345,11 +375,11 @@ export async function getGatewayModelIds(
345
375
  request = {
346
376
  revision: requestRevision,
347
377
  storedAt: now,
348
- promise: fetchGatewayModelIds(baseUrl, apiKey, {
349
- fetcher: opts.fetcher,
350
- timeoutMs: opts.timeoutMs,
351
- signal: opts.signal,
352
- }),
378
+ promise: (async () => {
379
+ const first = await fetchGatewayModelIds(baseUrl, apiKey, opts);
380
+ if (first.ok || !retry || first.reason === "aborted" || opts.signal?.aborted) return first;
381
+ return fetchGatewayModelIds(baseUrl, apiKey, opts);
382
+ })(),
353
383
  };
354
384
  if (shareRequest) inFlightRequests.set(requestKey, request);
355
385
  }
@@ -4,29 +4,11 @@ import { ANTHROPIC_COMPAT } from "./_protocols.ts";
4
4
  /**
5
5
  * Anthropic Claude 系列(anthropic-messages 协议)。
6
6
  * 官方文档:https://docs.claude.com/en/docs/about-claude/models/
7
- * 规格/定价以网关目录为准(fable/opus/sonnet 三档)。
7
+ * 规格/定价以网关目录为准(opus/sonnet 两档)。
8
8
  */
9
9
  export function anthropicModels(root: string): ProviderModelConfig[] {
10
10
  const anthropic = `${root}/anthropic`;
11
11
  return [
12
- {
13
- id: "claude-fable-5",
14
- name: "Claude Fable 5",
15
- api: "anthropic-messages",
16
- baseUrl: anthropic,
17
- reasoning: true,
18
- thinkingLevelMap: {
19
- low: "low",
20
- medium: "medium",
21
- high: "high",
22
- max: "max",
23
- },
24
- input: ["text", "image"],
25
- cost: { input: 10, output: 50, cacheRead: 1, cacheWrite: 12.3 },
26
- contextWindow: 1000000,
27
- maxTokens: 128000,
28
- compat: ANTHROPIC_COMPAT,
29
- },
30
12
  {
31
13
  id: "claude-opus-5",
32
14
  name: "Claude Opus 5",
@@ -1,11 +1,27 @@
1
1
  import type { ChatModelConfig as ProviderModelConfig } from "../_tools.ts";
2
2
  import { type PayloadWriter, type ThinkingApplier, type ThinkingLevel } from "../_tools.ts";
3
+ import { OPENAI_COMPLETIONS_COMPAT } from "./_protocols.ts";
3
4
 
4
5
  /**
5
- * Google Gemini 系列(google-generative-ai 协议)。
6
+ * Google Gemini 系列(原生 Google 协议;3.5 Flash-Lite 使用网关 Completions 路由)。
6
7
  * 官方文档:https://ai.google.dev/gemini-api/docs
7
8
  * GenerateContent 档位/预算:https://docs.cloud.google.com/vertex-ai/generative-ai/docs/thinking
8
9
  * gemini-flash 别名保留预算策略,其他保留型号使用档位策略。网关别名的路由仍需网关侧验证。
10
+ *
11
+ * 新增型号依据(2026-10-17 核查):
12
+ * - gemini-3.5-flash-lite:模型页 input 1,048,576 / output 65,536;思考档
13
+ * minimal/low/medium/high(默认 minimal,无 off),见
14
+ * https://ai.google.dev/gemini-api/docs/models/gemini-3.5-flash-lite 与
15
+ * https://ai.google.dev/gemini-api/docs/thinking 的逐型号表。
16
+ * - gemini-3.8-flash-high:网关精确 ID。官方无该后缀型号,规格(input
17
+ * 1,048,576 / output 65,536;思考 low/medium/high,默认 medium,minimal
18
+ * 会报错)继承基础型号 gemini-3.8-flash,实际别名路由未验证;不登记
19
+ * 裸 ID gemini-3.8-flash,也不把 high 后缀当作固定思考档位。
20
+ * https://ai.google.dev/gemini-api/docs/models/gemini-3.8-flash
21
+ * https://ai.google.dev/gemini-api/docs/whats-new-gemini-3.8
22
+ * - 3.5 Flash-Lite 与 3.8 起废弃采样参数(temperature/top_p/top_k 被忽略,
23
+ * 未来版本报错)并改用 thinking_level 枚举,见
24
+ * https://ai.google.dev/gemini-api/docs/whats-new-gemini-3.6。
9
25
  */
10
26
  export function geminiModels(root: string): ProviderModelConfig[] {
11
27
  const gemini = `${root}/gemini`;
@@ -68,6 +84,40 @@ export function geminiModels(root: string): ProviderModelConfig[] {
68
84
  contextWindow: 1048576,
69
85
  maxTokens: 65536,
70
86
  },
87
+ {
88
+ id: "gemini-3.5-flash-lite",
89
+ name: "Gemini 3.5 Flash-Lite",
90
+ // 2026-10-17:原生 Google 路由 404;此 Completions 路由文本/函数通过,
91
+ // 默认工具选择下结果往返通过;显式 tool_choice:none 的结果轮曾超时。
92
+ api: "openai-completions",
93
+ baseUrl: `${root}/v1`,
94
+ compat: OPENAI_COMPLETIONS_COMPAT,
95
+ thinkingLevelMap: { off: null, minimal: "minimal", low: "low", medium: "medium", high: "high", xhigh: "high", max: "high" },
96
+ reasoning: true,
97
+ input: ["text", "image"],
98
+ // 标准层公开价(2026-10-17):输入 $0.30、输出 $2.50(含思考);
99
+ // 缓存读取 $0.03,存储 $1/1M tokens/小时按小时计费,无法映射为单价,置 0。
100
+ cost: { input: 0.3, output: 2.5, cacheRead: 0.03, cacheWrite: 0 },
101
+ contextWindow: 1048576,
102
+ maxTokens: 65536,
103
+ },
104
+ {
105
+ id: "gemini-3.8-flash-high",
106
+ name: "Gemini 3.8 Flash",
107
+ api: "google-generative-ai",
108
+ // 宿主指定 baseUrl 时不追加版本路径;此路由已通过文本/工具往返。
109
+ baseUrl: `${gemini}/v1beta`,
110
+ thinkingLevelMap: { off: null, minimal: "low", low: "low", medium: "medium", high: "high", xhigh: "high", max: "high" },
111
+ reasoning: true,
112
+ input: ["text", "image"],
113
+ // 网关精确 ID,规格与价格继承官方基础型号 gemini-3.8-flash(别名路由
114
+ // 未验证)。标准层介绍价:输入 $0.75、输出 $3.75(含思考)、缓存读取
115
+ // $0.075,优惠截止 2026-12-31;2027-01-01 起为 $1.50/$7.50/$0.15。
116
+ // 缓存存储 $0.50/1M tokens/小时(介绍价)按小时计费,无法映射为单价,置 0。
117
+ cost: { input: 0.75, output: 3.75, cacheRead: 0.075, cacheWrite: 0 },
118
+ contextWindow: 1048576,
119
+ maxTokens: 65536,
120
+ },
71
121
  ];
72
122
  }
73
123
 
@@ -100,10 +150,26 @@ function applyGemini35Flash(writer: PayloadWriter, level: ThinkingLevel): void {
100
150
  writer.setGoogleThinking({ includeThoughts: level !== "off", thinkingLevel }, ["thinkingBudget"]);
101
151
  }
102
152
 
153
+ // 3.5 Flash-Lite 官方档位同为 minimal/low/medium/high 且默认 minimal,无 off:
154
+ // 网关 Completions 使用 reasoning_effort;off 就近 minimal,xhigh/max 收敛 high。
155
+ function applyGemini35FlashLite(writer: PayloadWriter, level: ThinkingLevel): void {
156
+ writer.remove("thinking");
157
+ const effort = level === "off" ? "minimal" : level === "xhigh" || level === "max" ? "high" : level;
158
+ writer.set("reasoning_effort", effort);
159
+ }
160
+
161
+ // 3.8 Flash 仅支持 low/medium/high(minimal 会报错,无 off);off/minimal
162
+ // 就近落到 LOW,xhigh/max 收敛到 HIGH;后缀 high 不锁定档位。
163
+ function applyGemini38Flash(writer: PayloadWriter, level: ThinkingLevel): void {
164
+ applyGeminiPro(writer, level);
165
+ }
166
+
103
167
  export const geminiThinking: Record<string, ThinkingApplier> = {
104
168
  "gemini-flash": (w, c) => applyGeminiFlash(w, c.thinkingLevel),
105
169
  "gemini-3.5-flash": (w, c) => applyGemini35Flash(w, c.thinkingLevel),
106
170
  "gemini-3.5-flash-low": (w, c) => applyGemini35Flash(w, c.thinkingLevel),
107
171
  "gemini-3.5-flash-extra-low": (w, c) => applyGemini35Flash(w, c.thinkingLevel),
172
+ "gemini-3.5-flash-lite": (w, c) => applyGemini35FlashLite(w, c.thinkingLevel),
108
173
  "gemini-pro": (w, c) => applyGeminiPro(w, c.thinkingLevel),
174
+ "gemini-3.8-flash-high": (w, c) => applyGemini38Flash(w, c.thinkingLevel),
109
175
  };
@@ -1,28 +1,30 @@
1
1
  import type { ChatModelConfig as ProviderModelConfig } from "../_tools.ts";
2
2
  import { OPENAI_COMPLETIONS_COMPAT } from "./_protocols.ts";
3
- import { type PayloadWriter, type ThinkingApplier, type ThinkingLevel } from "../_tools.ts";
3
+ import { isPlainObject, type PayloadWriter, type ThinkingApplier, type ThinkingLevel } from "../_tools.ts";
4
4
 
5
5
  /**
6
- * xAI Grok 系列(openai-completions 协议)。
6
+ * xAI Grok 系列:4.5 使用 Completions,4.7 使用 Responses。
7
7
  * 官方文档:https://docs.x.ai/developers/models
8
8
  * 定价(docs.x.ai/developers/pricing,提示词 <200k 档):
9
- * grok-4.20 / grok-4.3:每百万词元输入 $1.25 / 缓存命中 $0.20 / 输出 $2.50
10
9
  * grok-4.5:每百万词元输入 $2.00 / 缓存命中 $0.30 / 输出 $6.00
11
10
  */
12
11
  export function grokModels(root: string): ProviderModelConfig[] {
13
12
  const v1 = `${root}/v1`;
14
13
  return [
15
14
  {
16
- id: "grok-4.20",
17
- name: "Grok 4.20",
18
- api: "openai-completions",
15
+ id: "grok-4.7",
16
+ name: "Grok 4.7",
17
+ api: "openai-responses",
19
18
  baseUrl: v1,
20
19
  reasoning: true,
21
20
  input: ["text", "image"],
22
- cost: { input: 1.25, output: 2.5, cacheRead: 0.2, cacheWrite: 0 },
23
- contextWindow: 1000000,
24
- maxTokens: 65536,
25
- compat: OPENAI_COMPLETIONS_COMPAT,
21
+ // 2026-10-17:https://docs.x.ai/developers/models/grok-4.7
22
+ // <200K 官方参考价;≥200K 输入/缓存/输出均 ×2,cacheWrite=0 表示未单列。
23
+ cost: { input: 2, output: 6, cacheRead: 0.5, cacheWrite: 0 },
24
+ contextWindow: 500000,
25
+ // 官方不设独立文本输出上限;此值取上下文容量上界,不宣称可同时填满输入与输出。
26
+ maxTokens: 500000,
27
+ thinkingLevelMap: { off: null, minimal: "low", low: "low", medium: "medium", high: "high", xhigh: "xhigh", max: "xhigh" },
26
28
  },
27
29
  {
28
30
  id: "grok-4.5",
@@ -36,65 +38,11 @@ export function grokModels(root: string): ProviderModelConfig[] {
36
38
  maxTokens: 65536,
37
39
  compat: OPENAI_COMPLETIONS_COMPAT,
38
40
  },
39
- {
40
- id: "grok-4.3-low",
41
- name: "Grok 4.3",
42
- api: "openai-completions",
43
- baseUrl: v1,
44
- reasoning: true,
45
- input: ["text", "image"],
46
- // 网关思考档位别名,规格继承 grok-4.3。
47
- cost: { input: 1.25, output: 2.5, cacheRead: 0.2, cacheWrite: 0 },
48
- contextWindow: 1000000,
49
- maxTokens: 65536,
50
- compat: OPENAI_COMPLETIONS_COMPAT,
51
- },
52
- {
53
- id: "grok-4.3-medium",
54
- name: "Grok 4.3",
55
- api: "openai-completions",
56
- baseUrl: v1,
57
- reasoning: true,
58
- input: ["text", "image"],
59
- // 网关思考档位别名,规格继承 grok-4.3。
60
- cost: { input: 1.25, output: 2.5, cacheRead: 0.2, cacheWrite: 0 },
61
- contextWindow: 1000000,
62
- maxTokens: 65536,
63
- compat: OPENAI_COMPLETIONS_COMPAT,
64
- },
65
- {
66
- id: "grok-4.3-high",
67
- name: "Grok 4.3",
68
- api: "openai-completions",
69
- baseUrl: v1,
70
- reasoning: true,
71
- input: ["text", "image"],
72
- // 网关思考档位别名,规格继承 grok-4.3。
73
- cost: { input: 1.25, output: 2.5, cacheRead: 0.2, cacheWrite: 0 },
74
- contextWindow: 1000000,
75
- maxTokens: 65536,
76
- compat: OPENAI_COMPLETIONS_COMPAT,
77
- },
78
- {
79
- id: "grok-4.20-fast",
80
- name: "Grok 4.20 Fast",
81
- api: "openai-completions",
82
- baseUrl: v1,
83
- reasoning: true,
84
- input: ["text", "image"],
85
- // 网关快速档别名,规格继承 grok-4.20。
86
- cost: { input: 1.25, output: 2.5, cacheRead: 0.2, cacheWrite: 0 },
87
- contextWindow: 1000000,
88
- maxTokens: 65536,
89
- compat: OPENAI_COMPLETIONS_COMPAT,
90
- },
91
41
  ];
92
42
  }
93
43
 
94
44
  // 官方:https://docs.x.ai/developers/model-capabilities/text/reasoning
95
45
  // grok-4.5 支持 low/medium/high,不支持禁用 reasoning,off 映射到 low。
96
- // 4.20 / 4.3 网关别名暂保留既有映射;上述官方指南没有确认其思考深度支持,
97
- // 不应将同族回归测试视为这些别名已通过端点验证。
98
46
  function applyGrok(writer: PayloadWriter, level: ThinkingLevel): void {
99
47
  writer.remove("thinking");
100
48
  if (level === "off") {
@@ -110,11 +58,19 @@ function applyGrok(writer: PayloadWriter, level: ThinkingLevel): void {
110
58
  writer.set("reasoning_effort", effort);
111
59
  }
112
60
 
61
+ // Responses 保留宿主的 encrypted reasoning/include 与其他 reasoning 字段,
62
+ // 仅归一化不受支持的档位;off 不关闭思考,遗留会话回退 low。
63
+ function applyGrok47(writer: PayloadWriter, level: ThinkingLevel): void {
64
+ writer.remove("thinking");
65
+ writer.remove("reasoning_effort");
66
+ const current = writer.get("reasoning");
67
+ if (current !== undefined && !isPlainObject(current)) return;
68
+ const reasoning = isPlainObject(current) ? current : {};
69
+ const effort = level === "off" || level === "minimal" ? "low" : level === "max" ? "xhigh" : level;
70
+ if (reasoning.effort !== effort) writer.set("reasoning", { ...reasoning, effort });
71
+ }
72
+
113
73
  export const grokThinking: Record<string, ThinkingApplier> = {
114
- "grok-4.20": (w, c) => applyGrok(w, c.thinkingLevel),
74
+ "grok-4.7": (w, c) => applyGrok47(w, c.thinkingLevel),
115
75
  "grok-4.5": (w, c) => applyGrok(w, c.thinkingLevel),
116
- "grok-4.3-low": (w, c) => applyGrok(w, c.thinkingLevel),
117
- "grok-4.3-medium": (w, c) => applyGrok(w, c.thinkingLevel),
118
- "grok-4.3-high": (w, c) => applyGrok(w, c.thinkingLevel),
119
- "grok-4.20-fast": (w, c) => applyGrok(w, c.thinkingLevel),
120
76
  };
@@ -3,8 +3,10 @@ import { OPENAI_COMPLETIONS_COMPAT } from "./_protocols.ts";
3
3
  import { applyEnabledThinking, type ThinkingApplier } from "../_tools.ts";
4
4
 
5
5
  /**
6
- * LongCat 2.0(龙猫)。
7
- * 官方文档:https://longcat.chat/platform/docs/zh/
6
+ * 美团 LongCat(龙猫)系列。
7
+ * 官方中文文档(2026-10-17 核实):https://longcat.ai/platform/docs/zh/
8
+ * 网关目录 ID 为小写 longcat-*;上游 API 模型名保留大小写
9
+ * (LongCat-2.5-Preview / LongCat-2.0)。
8
10
  */
9
11
  export function longcatModels(root: string): ProviderModelConfig[] {
10
12
  const v1 = `${root}/v1`;
@@ -21,10 +23,41 @@ export function longcatModels(root: string): ProviderModelConfig[] {
21
23
  maxTokens: 131072,
22
24
  compat: OPENAI_COMPLETIONS_COMPAT,
23
25
  },
26
+ {
27
+ // LongCat-2.5-Preview(2026-09-25 上线)。依据官方中文文档:
28
+ // - 输入模态:模型详情 /api/model 示例 input_modalities 为 ["text"]、
29
+ // modality "text->text",OpenAI/Anthropic 两套消息 schema 的 content
30
+ // 均为“纯文本字符串”;更新日志宣称新增图片理解能力,但当前 API
31
+ // 契约未提供图像入参,按 API 契约登记为纯文本输入(冲突见更新日志)。
32
+ // - 定价(国内 chat 版,限时折扣价)/pricing/longcat-2.5:
33
+ // 输入未命中缓存 ¥2、命中缓存 ¥0.04、输出 ¥8 每百万词元,无缓存写计费;
34
+ // 按既有固定 0.1475 USD/CNY 折算为 0.295 / 0.0059 / 1.18 USD。
35
+ // - 参数 /api/model-params:上下文 1,048,576、max_tokens 上限
36
+ // 262,144(256K)。快速开始页仍写最大输出 128K 且两型号相同,
37
+ // 与逐型号对照的参数参考页冲突;以参数参考页为准(thinking_budget
38
+ // 上限同为 262,144 可互证)。
39
+ id: "longcat-2.5-preview",
40
+ name: "LongCat-2.5-Preview",
41
+ api: "openai-completions",
42
+ baseUrl: v1,
43
+ reasoning: true,
44
+ input: ["text"],
45
+ cost: { input: 0.295, output: 1.18, cacheRead: 0.0059, cacheWrite: 0 },
46
+ contextWindow: 1048576,
47
+ maxTokens: 262144,
48
+ // 官方协议字段为 max_tokens,由宿主 compat 映射,不自行改写 token 上限。
49
+ compat: { ...OPENAI_COMPLETIONS_COMPAT, maxTokensField: "max_tokens" },
50
+ },
24
51
  ];
25
52
  }
26
53
 
27
- // OC/网关兼容策略:LongCat 的公开 HTTP thinking 结构定义未验证,用通用开关式。
54
+ // 2.5 的 /api/chat 与 /api/model-params 已确认 thinking.type enabled/disabled;
55
+ // 独立核对后可复用通用开关式,不支持 reasoning_effort。预算没有逐档依据,不臆造映射。
56
+ // 可关闭思考,故不设 off:null;采样温度由服务端按思考开关覆盖。
57
+
58
+ // OC/网关兼容策略:longcat-2.0 的公开 HTTP thinking 结构定义未验证,沿用通用开关式;
59
+ // longcat-2.5-preview 经独立核对后复用相同的开关形状(见上方依据)。
28
60
  export const longcatThinking: Record<string, ThinkingApplier> = {
29
61
  "longcat-2.0": (w, c) => applyEnabledThinking(w, c.thinkingLevel),
62
+ "longcat-2.5-preview": (w, c) => applyEnabledThinking(w, c.thinkingLevel),
30
63
  };
@@ -6,6 +6,7 @@ import { type PayloadWriter, type ThinkingApplier } from "../_tools.ts";
6
6
  * OpenAI GPT 系列(Responses 协议)。
7
7
  * 官方文档:https://platform.openai.com/docs/models
8
8
  * GPT-6.1 Sol:https://developers.openai.com/api/docs/models/gpt-6.1-sol
9
+ * 用户确认网关接入 Codex 对应型号,上下文为 500000;最大输出与价格保持原值。
9
10
  * 标准档 ≤272K:输入 $2 / 缓存命中 $0.1 / 缓存写入 $2.5 / 输出 $10(2026-09-30)。
10
11
  * GPT-6 Sol:https://developers.openai.com/api/docs/models/gpt-6-sol
11
12
  * GPT-6 Luna:https://developers.openai.com/api/docs/models/gpt-6-luna
@@ -53,7 +54,7 @@ export function openaiModels(root: string): ProviderModelConfig[] {
53
54
  },
54
55
  input: ["text", "image"],
55
56
  cost: { input: 2, output: 10, cacheRead: 0.1, cacheWrite: 2.5 },
56
- contextWindow: 1050000,
57
+ contextWindow: 500000,
57
58
  maxTokens: 128000,
58
59
  },
59
60
  {
@@ -4,6 +4,7 @@ import { isAtLeastHigh, type PayloadWriter, type ThinkingApplier, type ThinkingL
4
4
 
5
5
  /**
6
6
  * 阿里云通义(Alibaba Qwen)系列。
7
+ * 当前仅保留 qwen3.7-plus 及策略作为厂商接入参考;实际登记仍须命中网关目录。
7
8
  * 官方文档:https://help.aliyun.com/zh/model-studio/
8
9
  */
9
10
  export function qwenModels(root: string): ProviderModelConfig[] {
@@ -26,44 +27,6 @@ export function qwenModels(root: string): ProviderModelConfig[] {
26
27
  maxTokens: 64000,
27
28
  compat: OPENAI_COMPLETIONS_COMPAT,
28
29
  },
29
- {
30
- id: "qwen3.7-max",
31
- name: "Qwen3.7 Max",
32
- api: "openai-completions",
33
- baseUrl: v1,
34
- reasoning: true,
35
- input: ["text"],
36
- cost: { input: 1.77, output: 5.31, cacheRead: 0.354, cacheWrite: 2.2125 },
37
- contextWindow: 1000000,
38
- maxTokens: 64000,
39
- compat: OPENAI_COMPLETIONS_COMPAT,
40
- },
41
- {
42
- id: "qwen3.6-plus",
43
- name: "Qwen3.6 Plus",
44
- api: "openai-completions",
45
- baseUrl: v1,
46
- reasoning: true,
47
- input: ["text", "image"],
48
- // 阿里云百炼 CNY 定价按 0.1475 USD/CNY 换算(与 qwen3.7 系列一致)。
49
- cost: { input: 0.295, output: 1.77, cacheRead: 0.0295, cacheWrite: 0.36875 },
50
- contextWindow: 1000000,
51
- maxTokens: 65536,
52
- compat: OPENAI_COMPLETIONS_COMPAT,
53
- },
54
- {
55
- id: "qwen3.5-plus",
56
- name: "Qwen3.5 Plus",
57
- api: "openai-completions",
58
- baseUrl: v1,
59
- reasoning: true,
60
- input: ["text", "image"],
61
- // 阿里云百炼 CNY 定价按 0.1475 USD/CNY 换算(与 qwen3.7 系列一致)。
62
- cost: { input: 0.118, output: 0.708, cacheRead: 0.0118, cacheWrite: 0.1475 },
63
- contextWindow: 1000000,
64
- maxTokens: 65536,
65
- compat: OPENAI_COMPLETIONS_COMPAT,
66
- },
67
30
  ];
68
31
  }
69
32
 
@@ -96,7 +59,4 @@ function applyQwen(writer: PayloadWriter, level: ThinkingLevel): void {
96
59
 
97
60
  export const qwenThinking: Record<string, ThinkingApplier> = {
98
61
  "qwen3.7-plus": (w, c) => applyQwen(w, c.thinkingLevel),
99
- "qwen3.7-max": (w, c) => applyQwen(w, c.thinkingLevel),
100
- "qwen3.6-plus": (w, c) => applyQwen(w, c.thinkingLevel),
101
- "qwen3.5-plus": (w, c) => applyQwen(w, c.thinkingLevel),
102
62
  };
@@ -6,7 +6,7 @@
6
6
  * `before_provider_request` 中应用。
7
7
  *
8
8
  * - GPT(openai-responses):追加 `tools: [{ type: "web_search", ... }]`
9
- * - Grok(openai-completions):追加 `search_parameters: { mode: "on" }`
9
+ * - Grok 4.5(Completions):追加 `search_parameters: { mode: "on" }`
10
10
  * (xAI 文档:https://docs.x.ai/developers/tools/web-search)
11
11
  *
12
12
  * 名单外主模型通过 tsgw_search 调用目录内支持模型;注入逻辑保持共用。
@@ -31,14 +31,7 @@ const BUILTIN_SEARCH_MODELS = new Set([
31
31
  ]);
32
32
 
33
33
  // 支持内置查询的 Grok 系列(openai-completions 协议,search_parameters 模式)。
34
- const GROK_SEARCH_MODELS = new Set([
35
- "grok-4.20",
36
- "grok-4.5",
37
- "grok-4.3-low",
38
- "grok-4.3-medium",
39
- "grok-4.3-high",
40
- "grok-4.20-fast",
41
- ]);
34
+ const GROK_SEARCH_MODELS = new Set(["grok-4.5"]);
42
35
 
43
36
  /** 精确判定模型/协议支持性,由请求注入与展示代码共用。 */
44
37
  export function builtinSearchSupport(
@@ -65,11 +58,6 @@ const DELEGATE_PREFERENCE = [
65
58
  "gpt-6-sol",
66
59
  "gpt-6.1-sol",
67
60
  "gpt-6-astra",
68
- "grok-4.20",
69
- "grok-4.20-fast",
70
- "grok-4.3-low",
71
- "grok-4.3-medium",
72
- "grok-4.3-high",
73
61
  "grok-4.5",
74
62
  ] as const;
75
63
 
@@ -19,7 +19,7 @@ import {
19
19
  normalizeRoot,
20
20
  type ModelFilter,
21
21
  } from "../models/catalog.ts";
22
- import { getGatewayModelIds } from "../models/gateway-catalog.ts";
22
+ import { getGatewayModelIds, STARTUP_GATEWAY_MODEL_CACHE_TTL_MS } from "../models/gateway-catalog.ts";
23
23
  import { applyModelOperations } from "../models/operations.ts";
24
24
  import { registerSearch } from "../search/index.ts";
25
25
  import { IMAGE_API, imageModelsForRoot, supportsImageModels } from "../images/models.ts";
@@ -32,6 +32,7 @@ import {
32
32
  } from "./status.ts";
33
33
  import type { ThinkingLevel, WebSearchMode } from "../models/_tools.ts";
34
34
  import type { TsgwSettings } from "./config.ts";
35
+ import { registerRefreshTool } from "./refresh-tool.ts";
35
36
 
36
37
  /**
37
38
  * 在生命周期上下文仍有效时,复制供应商钩子所需的数据。
@@ -121,13 +122,13 @@ function effectiveModelsForGateway(
121
122
  ];
122
123
  const intersected = gatewayIds
123
124
  ? staticModels.filter((model) => gatewayHasModel(gatewayIds, model.id))
124
- : staticModels;
125
+ : [];
125
126
  return filterModels(intersected, filter);
126
127
  }
127
128
 
128
129
  /**
129
130
  * 三层模型过滤:静态目录 ∩ 网关实际列表 ∩ 用户黑白名单。
130
- * 网关列表拉取失败/无凭据时跳过网关层(回退静态目录)。
131
+ * 无网关目录时不登记模型,避免将静态登记误认为网关可用。
131
132
  */
132
133
  async function resolveEffectiveModels(
133
134
  root: string,
@@ -139,7 +140,7 @@ async function resolveEffectiveModels(
139
140
  gatewayIds: ReadonlySet<string> | undefined;
140
141
  catalog: CatalogStatus;
141
142
  }> {
142
- // 凭据解析失败不阻塞:无凭据时直接跳过网关层。
143
+ // 凭据解析失败不阻塞插件加载,但不登记未经网关确认的模型。
143
144
  let apiKey = "";
144
145
  try {
145
146
  const credential = readStoredCredential(PROVIDER_ID);
@@ -149,11 +150,15 @@ async function resolveEffectiveModels(
149
150
  }
150
151
 
151
152
  let gatewayIds: ReadonlySet<string> | undefined;
152
- let catalog: CatalogStatus = { count: 0, source: "static", loading: false };
153
+ let catalog: CatalogStatus = { count: 0, source: "empty", loading: false,
154
+ ttlMs: STARTUP_GATEWAY_MODEL_CACHE_TTL_MS,
155
+ error: "模型目录获取失败,当前无可用模型;请配置凭据后择机强制刷新。" };
153
156
  if (apiKey) {
154
157
  try {
155
158
  const gateway = await getGatewayModelIds(root, apiKey, {
156
159
  cacheFilePath,
160
+ ttlMs: STARTUP_GATEWAY_MODEL_CACHE_TTL_MS,
161
+ retryWithoutCache: true,
157
162
  });
158
163
  if (gateway.ok) {
159
164
  gatewayIds = new Set(gateway.ids);
@@ -162,11 +167,11 @@ async function resolveEffectiveModels(
162
167
  source: gateway.cached ? "cache" : "network",
163
168
  storedAt: gateway.storedAt,
164
169
  error: gateway.stale
165
- ? `刷新失败(${gateway.fallbackReason ?? "unknown"}),沿用旧目录` : undefined,
170
+ ? "模型目录刷新失败,已沿用缓存,请择机强制刷新。" : undefined,
166
171
  };
167
- } else catalog.error = `目录获取失败(${gateway.reason}),使用内置目录`;
172
+ } else catalog.error = "模型目录获取失败,当前无可用模型,请择机强制刷新。";
168
173
  } catch {
169
- catalog.error = "目录获取失败,使用内置目录";
174
+ catalog.error = "模型目录获取失败,当前无可用模型,请择机强制刷新。";
170
175
  }
171
176
  }
172
177
  if (catalog.error) console.warn(`TSGW: ${catalog.error}`);
@@ -189,6 +194,7 @@ export async function registerTsgwProvider(
189
194
  exclude: settings.excludeModels,
190
195
  };
191
196
  const cacheFilePath = gatewayModelCachePath();
197
+ registerRefreshTool(pi, cacheFilePath);
192
198
  let requestState: RequestState | undefined;
193
199
 
194
200
  const refreshRequestState = (ctx: {
@@ -208,6 +214,7 @@ export async function registerTsgwProvider(
208
214
  const { models } = initial;
209
215
  let latestModels = models;
210
216
  let catalog = initial.catalog;
217
+ let startupNotice = initial.catalog.error;
211
218
  let credentialConfigured = false;
212
219
  let publishStatus: (() => void) | undefined;
213
220
  let refreshRevision = 0;
@@ -247,7 +254,9 @@ export async function registerTsgwProvider(
247
254
  models,
248
255
  ...(imagesEnabled ? { images: { [IMAGE_API]: { generateImages } } } : {}),
249
256
  async refreshModels(context) {
250
- if (!context.allowNetwork) return latestModels;
257
+ // 启动获取已完成;宿主的额外自动同步不再追加联网请求。
258
+ // 工具检查 24 小时窗口,必要时与命令一样显式传入 force。
259
+ if (!context.allowNetwork || context.force !== true) return latestModels;
251
260
  context.signal.throwIfAborted();
252
261
  const revision = ++refreshRevision;
253
262
  catalog = { ...catalog, loading: true };
@@ -263,11 +272,10 @@ export async function registerTsgwProvider(
263
272
  signal: context.signal,
264
273
  });
265
274
  context.signal.throwIfAborted();
266
- if (!gateway.ok)
267
- throw new Error(`TSGW 网关模型刷新失败(${gateway.reason})。`);
268
- if (gateway.stale)
275
+ if (!gateway.ok || gateway.stale)
269
276
  throw new Error("TSGW 刷新失败;保留上次成功获取的目录。");
270
277
  if (revision === refreshRevision) {
278
+ startupNotice = undefined;
271
279
  latestModels = effectiveModelsForGateway(root, modelFilter, new Set(gateway.ids), imagesEnabled);
272
280
  search.sync();
273
281
  catalog = {
@@ -370,6 +378,10 @@ export async function registerTsgwProvider(
370
378
  refreshRequestState(ctx);
371
379
  search.sync();
372
380
  bindStatus(ctx);
381
+ if (startupNotice && ctx.hasUI) {
382
+ ctx.ui.notify(startupNotice, "warning");
383
+ startupNotice = undefined;
384
+ }
373
385
  });
374
386
  pi.on("agent_start", (_event, ctx) => {
375
387
  refreshRequestState(ctx);
@@ -0,0 +1,48 @@
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
+ import { Type } from "typebox";
3
+ import { PROVIDER_ID } from "../models/catalog.ts";
4
+ import { DEFAULT_GATEWAY_MODEL_CACHE_TTL_MS, readGatewayModelCacheSummary } from "../models/gateway-catalog.ts";
5
+
6
+ /** 默认检查缓存时间;需要联网时复用宿主 provider 的强制刷新。 */
7
+ export function registerRefreshTool(pi: ExtensionAPI, cacheFilePath: string): void {
8
+ pi.registerTool({
9
+ name: "tsgw_refresh_models",
10
+ label: "刷新 TSGW 模型目录",
11
+ description: "获取 AIH 网关可用模型,默认复用 24 小时内的缓存;force=true 忽略有效期强制联网刷新。单次联网超时 5 秒,不发推理请求、不修改配置、不自动重试。返回缓存文件路径、网关模型数与获取时间;随后读取该文件的 ids 获取完整列表(含插件尚未登记的型号)。不自动登记未知型号。",
12
+ parameters: Type.Object({
13
+ force: Type.Optional(Type.Boolean({ description: "忽略缓存有效期强制联网刷新,默认 false。" })),
14
+ }),
15
+ outputSchema: Type.Object({
16
+ cacheFilePath: Type.String(), count: Type.Integer(), storedAt: Type.Number(),
17
+ }),
18
+ exposure: "codemode",
19
+ annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: true },
20
+ async execute(_id, params, signal, _onUpdate, ctx) {
21
+ const caller = signal && ctx.signal && signal !== ctx.signal
22
+ ? AbortSignal.any([signal, ctx.signal]) : signal ?? ctx.signal;
23
+ const registry = ctx.modelRegistry;
24
+ const startedAt = Date.now();
25
+ let details;
26
+ let refreshed;
27
+ try {
28
+ caller?.throwIfAborted();
29
+ if (!registry.getProviderAuthStatus(PROVIDER_ID).configured) throw new Error("缺少凭据");
30
+ if (params.force !== true)
31
+ details = readGatewayModelCacheSummary(cacheFilePath, startedAt - DEFAULT_GATEWAY_MODEL_CACHE_TTL_MS + 1);
32
+ if (!details) {
33
+ refreshed = await registry.refresh({ providers: [PROVIDER_ID], force: true, signal: caller });
34
+ caller?.throwIfAborted();
35
+ }
36
+ } catch {
37
+ throw new Error(caller?.aborted ? "TSGW 模型刷新已取消。" : "TSGW 模型刷新失败;请检查宿主凭据、网关与网络。未输出原始错误。");
38
+ }
39
+ if (refreshed?.aborted) throw new Error("TSGW 模型刷新已取消。");
40
+ if (refreshed?.errors?.has(PROVIDER_ID))
41
+ throw new Error("TSGW 模型刷新失败,保留原目录;请检查宿主凭据、网关与网络。");
42
+ details ??= readGatewayModelCacheSummary(cacheFilePath, startedAt);
43
+ if (!details)
44
+ throw new Error("未确认本次完整网关目录已写入缓存文件;不能将旧文件当作本次刷新结果。provider 可能已经更新。");
45
+ return { content: [{ type: "text", text: JSON.stringify(details) }], details, structuredContent: details };
46
+ },
47
+ });
48
+ }
@@ -3,7 +3,9 @@ import { DEFAULT_GATEWAY_MODEL_CACHE_TTL_MS } from "../models/gateway-catalog.ts
3
3
 
4
4
  export interface CatalogStatus {
5
5
  count: number;
6
- source: "static" | "cache" | "network";
6
+ source: "static" | "empty" | "cache" | "network";
7
+ /** 当前入口采用的新鲜度窗口;启动为 7 天,运行时为 24 小时。 */
8
+ ttlMs?: number;
7
9
  /** 网关目录真实获取时间;从缓存读取时保留原值,不重置为读取时间。 */
8
10
  storedAt?: number;
9
11
  loading: boolean;
@@ -13,6 +15,7 @@ export interface CatalogStatus {
13
15
 
14
16
  const SOURCE_LABELS = {
15
17
  static: "内置静态",
18
+ empty: "无可用目录",
16
19
  cache: "缓存",
17
20
  network: "网关",
18
21
  } as const;
@@ -20,7 +23,7 @@ const SOURCE_LABELS = {
20
23
  function fetchedAt(state: CatalogStatus): number | undefined {
21
24
  const time = state.storedAt;
22
25
  if (
23
- state.source === "static" ||
26
+ (state.source === "static" || state.source === "empty") ||
24
27
  time === undefined ||
25
28
  !Number.isFinite(time) ||
26
29
  time < 0 ||
@@ -33,7 +36,7 @@ function freshness(state: CatalogStatus, now: number): "新鲜" | "过期" | "
33
36
  const time = fetchedAt(state);
34
37
  if (time === undefined || !Number.isFinite(now)) return "未知";
35
38
  // 与目录缓存一致:时钟回拨不判过期,达到 TTL 则过期。
36
- return Math.max(0, now - time) < DEFAULT_GATEWAY_MODEL_CACHE_TTL_MS
39
+ return Math.max(0, now - time) < (state.ttlMs ?? DEFAULT_GATEWAY_MODEL_CACHE_TTL_MS)
37
40
  ? "新鲜"
38
41
  : "过期";
39
42
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-tsgw",
3
- "version": "0.4.2",
3
+ "version": "0.6.0",
4
4
  "description": "面向 TOSSP AIH gateway 的 Pi 扩展:按厂商维护聊天与图片模型目录及思考策略,支持四种聊天协议与图片生成、编辑工具。",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -24,7 +24,7 @@
24
24
  ]
25
25
  },
26
26
  "scripts": {
27
- "test": "tsc -p tsconfig.test.json && node dist/extensions/provider/index.test.js && node dist/extensions/prompts.test.js && node dist/extensions/provider/status.test.js && node dist/extensions/search/index.test.js && node dist/extensions/search/proxy.test.js && node dist/extensions/search/host-integration.test.js && node dist/extensions/provider/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/images/models.test.js && node dist/extensions/images/parameters.test.js && node dist/extensions/images/input-budget.test.js && node dist/extensions/images/edit-request.test.js && node dist/extensions/images/image-data.test.js && node dist/extensions/images/response.test.js && node dist/extensions/images/stream-response.test.js && node dist/extensions/images/adapter.test.js && node dist/extensions/images/tool-files.test.js && node dist/extensions/images/tools.test.js && node dist/extensions/images/host-integration.test.js && node dist/extensions/images/tools-host.test.js && node dist/index.test.js && node dist/extensions/skills.test.js && node dist/extensions/skill-resources.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",
27
+ "test": "tsc -p tsconfig.test.json && node dist/extensions/provider/index.test.js && node dist/extensions/provider/refresh-tool.test.js && node dist/extensions/provider/startup-catalog.test.js && node dist/extensions/prompts.test.js && node dist/extensions/provider/status.test.js && node dist/extensions/search/index.test.js && node dist/extensions/search/proxy.test.js && node dist/extensions/search/host-integration.test.js && node dist/extensions/provider/status-integration.test.js && node dist/extensions/models/gateway-catalog-metadata.test.js && node dist/extensions/models/gateway-catalog-policy.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/models/grok-new.test.js && node dist/extensions/models/gemini-new.test.js && node dist/extensions/models/longcat-new.test.js && node dist/extensions/images/models.test.js && node dist/extensions/images/parameters.test.js && node dist/extensions/images/input-budget.test.js && node dist/extensions/images/edit-request.test.js && node dist/extensions/images/image-data.test.js && node dist/extensions/images/response.test.js && node dist/extensions/images/stream-response.test.js && node dist/extensions/images/adapter.test.js && node dist/extensions/images/tool-files.test.js && node dist/extensions/images/tools.test.js && node dist/extensions/images/host-integration.test.js && node dist/extensions/images/tools-host.test.js && node dist/index.test.js && node dist/extensions/skills.test.js && node dist/extensions/skill-resources.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
28
  "test:upload:live": "tsc -p tsconfig.test.json && node dist/extensions/upload/live.test.js"
29
29
  },
30
30
  "peerDependencies": {
@@ -36,10 +36,16 @@ compatibility: 需要宿主提供子代理工具及可用的 tsgw 模型。
36
36
 
37
37
  - † MiniMax 的 29 分为模型级参考,Pi high 映射为 adaptive;待评项不借用其他型号或档位的分数。
38
38
  - GLM 5.3 三型号始终思考,实际 effort 为 low/high/max;Pi medium 映射 high,xhigh 映射 max。FlashX 可作未定档备选,评分待评,输入/输出参考价为 0.295/1.0325。
39
+ - `gpt-6.1-sol` 按用户确认的网关 Codex 接入采用 500000 tokens 上下文;最大输出仍为 128000,不与上下文窗口混淆。
39
40
  - GPT 价格对应输入 ≤272K,超过后输入 ×2、输出 ×1.5;MiniMax 输入超过 512K 为 0.60/2.40。
40
41
  - DeepSeek 采用国内价(2026-10-16 核验,Blinko #604):输入 1–2 元、输出 4–8 元/百万 tokens,按固定 0.1475 USD/CNY 折算;区间为低谷/高峰价。静态目录采用区间高端,不自动按时段切换;高峰为北京时间工作日(不含中国法定节假日)9–12、14–18。
41
42
  - GLM 依据 Blinko #929,同样以固定 0.1475 USD/CNY 折算国内人民币标价。其余数值沿用既有选型资料。
42
43
 
44
+ 新增目录候选 `grok-4.7`、`gemini-3.5-flash-lite`、`gemini-3.8-flash-high`、
45
+ `longcat-2.5-preview` 暂不进入上表排序:Grok 工具完整往返尚未通过,
46
+ LongCat 受上游额度阻塞;两款 Gemini 仅通过最小文本/工具往返,尚无本表同口径评分。
47
+ 登记不等于适合长期工程委派,不借用同族评分。
48
+
43
49
  ## 必要边界
44
50
 
45
51
  - **MiniMax-M3** 只承担步骤明确的小任务。参考使用 Pi 的 high;非 off 档位均映射为 adaptive,不代表不同能力档。adaptive 不是 Pi 档位,此映射不适用于 M3.1 Flash Preview。