pi-tsgw 0.0.11 → 0.0.13
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 +96 -30
- package/extensions/index.ts +7 -4
- package/extensions/models/_tools.ts +9 -3
- package/extensions/models/catalog.ts +2 -2
- package/extensions/models/gateway-catalog.ts +66 -20
- package/extensions/models/vendors/_protocols.ts +1 -1
- package/extensions/models/vendors/anthropic.ts +1 -1
- package/extensions/models/vendors/deepseek.ts +2 -23
- package/extensions/models/vendors/gemini.ts +15 -54
- package/extensions/models/vendors/glm.ts +50 -99
- package/extensions/models/vendors/grok.ts +5 -3
- package/extensions/models/vendors/kimi.ts +35 -100
- package/extensions/models/vendors/longcat.ts +1 -1
- package/extensions/models/vendors/mimo.ts +13 -9
- package/extensions/models/vendors/minimax.ts +40 -46
- package/extensions/models/vendors/openai.ts +8 -84
- package/extensions/models/vendors/qwen.ts +1 -1
- package/extensions/models/web-search.ts +4 -8
- package/extensions/status.ts +1 -2
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -6,8 +6,9 @@ openai-responses, anthropic-messages, google-generative-ai) and applies
|
|
|
6
6
|
per-vendor request-body operations so thinking levels, reasoning formats, and
|
|
7
7
|
optional built-in web search behave correctly on the gateway.
|
|
8
8
|
|
|
9
|
-
No API keys
|
|
10
|
-
|
|
9
|
+
No private API keys or gateway endpoints are included. The static catalog
|
|
10
|
+
contains model metadata and public-price estimates; these are not your gateway's
|
|
11
|
+
billing rates.
|
|
11
12
|
|
|
12
13
|
## Install
|
|
13
14
|
|
|
@@ -36,10 +37,10 @@ Example:
|
|
|
36
37
|
{
|
|
37
38
|
"tsgw": {
|
|
38
39
|
"baseUrl": "https://aih.example.net",
|
|
39
|
-
"tsSearch": "
|
|
40
|
+
"tsSearch": "live",
|
|
40
41
|
"traceHeaders": false,
|
|
41
42
|
"excludeModels": ["gpt-*", "claude-*"],
|
|
42
|
-
"includeModels": ["gpt-
|
|
43
|
+
"includeModels": ["gpt-6-*"]
|
|
43
44
|
}
|
|
44
45
|
}
|
|
45
46
|
```
|
|
@@ -52,19 +53,23 @@ placeholder root is used that will not work.
|
|
|
52
53
|
|
|
53
54
|
### API key
|
|
54
55
|
|
|
55
|
-
The extension
|
|
56
|
+
The extension delegates credential reading and resolution to Pi:
|
|
56
57
|
|
|
57
58
|
1. **`/login`** — select the `tsgw` provider and enter the key; pi stores it
|
|
58
59
|
in `~/.pi/agent/auth.json` (preferred)
|
|
59
60
|
2. **`TSGW_API_KEY`** environment variable — Pi's fallback when nothing is
|
|
60
61
|
stored
|
|
61
62
|
|
|
63
|
+
Startup discovery uses Pi's stored-credential API; later catalog refreshes use
|
|
64
|
+
the credential supplied by Pi. With environment-only authentication, the initial
|
|
65
|
+
catalog may fall back to static models until Pi refreshes it.
|
|
66
|
+
|
|
62
67
|
### Options
|
|
63
68
|
|
|
64
69
|
| Setting (`settings.json` `tsgw.*`) | Default | Meaning |
|
|
65
70
|
| --- | --- | --- |
|
|
66
71
|
| `baseUrl` | placeholder | Gateway root (the `/v1` suffix is normalized away) |
|
|
67
|
-
| `tsSearch` | `
|
|
72
|
+
| `tsSearch` | `live` | GPT: `cached` or `live`; unset, legacy `off`, and invalid values fall back to `live`. Supported Grok models always use live search. |
|
|
68
73
|
| `traceHeaders` | `false` | Adds `AH-Thread-Id` / `AH-Trace-Id` headers for gateway-side tracing |
|
|
69
74
|
| `includeModels` | unset | Re-include override: matching model ids are kept even when also matched by `excludeModels` (exact id or `prefix-*` glob) |
|
|
70
75
|
| `excludeModels` | unset | Blacklist: matching model ids are dropped unless pulled back by `includeModels` (exact id or `prefix-*` glob) |
|
|
@@ -72,42 +77,93 @@ The extension never reads credential files. Pi resolves the key itself:
|
|
|
72
77
|
## Model catalog
|
|
73
78
|
|
|
74
79
|
The catalog is assembled from per-vendor slices (`extensions/models/vendors/`),
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
80
|
+
covering 11 vendor families: DeepSeek, GLM (Zhipu), MiMo (Xiaomi), MiniMax,
|
|
81
|
+
Kimi (Moonshot), Qwen (Alibaba), GPT (OpenAI), Gemini (Google), Claude
|
|
82
|
+
(Anthropic), LongCat, and Grok (xAI). The current static catalog contains 36 chat
|
|
83
|
+
model IDs, including retained aliases; this is not a live availability count.
|
|
84
|
+
Vendor files carry documentation links and metadata caveats. Alias mappings,
|
|
85
|
+
output limits, and shared thinking policies are not all individually verified
|
|
86
|
+
against gateway endpoints; public-price estimates do not guarantee billing.
|
|
87
|
+
MiMo uses `mimo-v2.6-pro` and `mimo-v2.6-flash`; the former V2.5 IDs are not
|
|
88
|
+
registered. Update any explicit model selections or filter rules to the new IDs.
|
|
89
|
+
|
|
90
|
+
The curated catalog keeps DeepSeek `deepseek-flash` and V4 Pro; GLM 5.2+,
|
|
91
|
+
plus the explicit `glm-4.5-air` exception; MiniMax M3+; Kimi K3; GPT 6+;
|
|
92
|
+
and Gemini 3.5+ plus the unversioned `gemini-flash` / `gemini-pro` aliases.
|
|
93
|
+
Qwen, MiMo, LongCat, Claude, and Grok retain their existing registrations.
|
|
94
|
+
Removed IDs and their dedicated request policies are no longer registered;
|
|
95
|
+
include rules cannot restore them, even if a gateway still lists them. Existing
|
|
96
|
+
explicit model selections referencing removed IDs need to be updated.
|
|
97
|
+
|
|
98
|
+
Kimi offers `kimi-k3` and the gateway variant `kimi-k3-256k`, displayed as
|
|
99
|
+
**Kimi K3 (256K)** with a 262,144-token context window. Its other metadata,
|
|
100
|
+
price estimates, and thinking policy are inherited from K3; the 131,072-token
|
|
101
|
+
output setting is separate from context size. These inherited values are not
|
|
102
|
+
individual endpoint verification or a guarantee of the variant's billing rates.
|
|
103
|
+
|
|
104
|
+
The catalog also includes `MiniMax-M3.1-Flash-Preview`, `glm-5.3`,
|
|
105
|
+
`glm-5.3-flash`, and `glm-5.3-flashx`. All four require thinking: MiniMax
|
|
106
|
+
uses low/medium/high/xhigh/max; GLM uses low/high/max. Unsupported `off` is
|
|
107
|
+
disabled in model metadata, and stale off/minimal requests map to low rather
|
|
108
|
+
than silently selecting the default max. MiniMax and GLM Flash/FlashX support
|
|
109
|
+
image input; GLM 5.3 is text-only. Registration does not prove gateway
|
|
110
|
+
inference or tool-call support.
|
|
111
|
+
|
|
112
|
+
As checked on 2026-10-16, MiniMax publishes subscription access but no per-token
|
|
113
|
+
price for M3.1 Flash Preview. Its zero cost fields mean **unknown**, not free,
|
|
114
|
+
and usage cost totals therefore omit its actual charges. GLM 5.3 price estimates
|
|
115
|
+
use domestic public rates and the existing fixed USD/CNY conversion; temporary
|
|
116
|
+
free cache storage is recorded as zero, not a permanent billing guarantee.
|
|
81
117
|
|
|
82
118
|
Use broad `excludeModels` rules to drop model families and precise
|
|
83
119
|
`includeModels` rules to pull selected models back. `includeModels` is not a
|
|
84
120
|
global whitelist: models matching neither list remain available. To register
|
|
85
121
|
only selected models, use `"excludeModels": ["*"]` and pull them back through
|
|
86
|
-
`includeModels`.
|
|
122
|
+
`includeModels`. Both lists are case-sensitive and support only exact IDs or
|
|
123
|
+
trailing `*` prefix matching (not general globs); preserve `MiniMax-*` casing.
|
|
124
|
+
When a gateway catalog is available, filtering applies after intersecting it
|
|
125
|
+
with the static catalog. Include rules cannot add unknown or unavailable IDs.
|
|
87
126
|
|
|
88
127
|
Opening `/model` refreshes the gateway catalog in the background, with a
|
|
89
128
|
five-minute cache to avoid repeated requests. Run `/tsgw-refresh` to bypass the
|
|
90
129
|
cache and force an immediate refresh without restarting or reloading Pi. If a
|
|
91
130
|
refresh fails, the last successful model list remains active.
|
|
92
131
|
|
|
132
|
+
Memory and disk caches are scoped to the gateway root and credential using a
|
|
133
|
+
SHA-256 digest; plaintext credentials are not stored. The disk cache is a single
|
|
134
|
+
slot at `<agent-dir>/tsgw/models-cache.json`: changing scope may require a fresh
|
|
135
|
+
fetch. Legacy unscoped cache files are ignored and replaced after a successful
|
|
136
|
+
fetch. In-process request ordering prevents late results from replacing newer
|
|
137
|
+
cache writes; this is not a cross-process locking mechanism.
|
|
138
|
+
|
|
93
139
|
## Status bar and session controls
|
|
94
140
|
|
|
95
|
-
Pi Web
|
|
141
|
+
Pi Web clients supporting `command:/…` statuses render these as clickable
|
|
142
|
+
buttons; the terminal CLI displays status text and accepts the same commands:
|
|
96
143
|
|
|
97
144
|
- **`TSGW · N`** (`/tsgw`): one menu for **refreshing the model catalog** and
|
|
98
145
|
**viewing diagnostics**. The count is the filtered registered catalog, not a
|
|
99
146
|
count of healthy inference endpoints. 🟢 means a fresh directory result;
|
|
100
147
|
🟡 means a static/expired directory or a failed refresh; ⚠️ means missing
|
|
101
148
|
credentials or gateway configuration; ⏳ means a refresh is running.
|
|
102
|
-
- **Built-in search** (`/tsgw-search`):
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
hidden when another provider is selected.
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
across a reload or a new session; `tsgw.tsSearch` remains the startup
|
|
149
|
+
- **Built-in search** (`/tsgw-search`): GPT offers only `cached / live`
|
|
150
|
+
for the current session, defaulting to `live`. Supported Grok models use
|
|
151
|
+
fixed live search: the command only shows a notice, without a picker or
|
|
152
|
+
changing the GPT session mode. Unsupported TSGW models are labeled
|
|
153
|
+
accordingly; this button is hidden when another provider is selected.
|
|
154
|
+
|
|
155
|
+
For GPT, you can also type `/tsgw-search cached` or `/tsgw-search live`;
|
|
156
|
+
`off` is no longer accepted. These controls do not modify `settings.json` or
|
|
157
|
+
persist across a reload or a new session; `tsgw.tsSearch` remains the startup
|
|
158
|
+
default. Existing `cached` configuration is preserved; unset, legacy `off`,
|
|
159
|
+
and invalid configuration now default to `live`.
|
|
160
|
+
|
|
161
|
+
Injection remains limited to the existing model/protocol support list. GPT
|
|
162
|
+
Responses appends a `web_search` tool (`external_web_access` is false for
|
|
163
|
+
`cached`, true for `live`); Grok Completions appends
|
|
164
|
+
`search_parameters: { mode: "on" }`. Existing search tools/fields are never
|
|
165
|
+
overwritten. This setting does not control Qwen's vendor policy, external
|
|
166
|
+
tools, or thinking mode and is not a global network-access switch.
|
|
111
167
|
|
|
112
168
|
The standalone `ts_search` tool is no longer bundled. Use a separately configured
|
|
113
169
|
search tool such as `ts_oht.search` instead. Built-in model search remains
|
|
@@ -126,16 +182,26 @@ statuses until the session runtime has started.
|
|
|
126
182
|
## Development
|
|
127
183
|
|
|
128
184
|
```bash
|
|
129
|
-
npm
|
|
185
|
+
npm ci
|
|
130
186
|
npm test
|
|
187
|
+
npm exec -- tsc -p tsconfig.json
|
|
188
|
+
npm pack --dry-run --json
|
|
131
189
|
```
|
|
132
190
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
191
|
+
Pi is installed as a development dependency; the lockfile defines the test
|
|
192
|
+
baseline. Tests isolate configuration and credentials using temporary agent
|
|
193
|
+
directories. Unit tests use a fake host; host integration tests load the extension
|
|
194
|
+
through the real Pi SDK with simulated HTTP responses, without production
|
|
195
|
+
requests. They do not verify upstream inference, native search results, or Pi
|
|
196
|
+
Web rendering. Run the full suite after upgrading Pi.
|
|
136
197
|
|
|
137
198
|
## Technical details
|
|
138
199
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
[
|
|
200
|
+
- [Vendor profiles](https://github.com/tossp/pi-tsgw/tree/main/extensions/models/vendors): model metadata, compatibility flags, and thinking mappings.
|
|
201
|
+
- [Request operations](https://github.com/tossp/pi-tsgw/blob/main/extensions/models/operations.ts): copy-on-write dispatch; DeepSeek/Claude response handling stays with Pi's adapters.
|
|
202
|
+
- [Built-in search](https://github.com/tossp/pi-tsgw/blob/main/extensions/models/web-search.ts): exact model/protocol support and append-only injection.
|
|
203
|
+
- [Host integration](https://github.com/tossp/pi-tsgw/blob/main/extensions/index.ts): configuration, refreshes, commands, and lifecycle snapshots. Late request callbacks must not access stale session contexts.
|
|
204
|
+
|
|
205
|
+
These source links describe the repository's current main branch; an installed
|
|
206
|
+
release may differ. Development rules in `AGENTS.md` are repository-only and
|
|
207
|
+
are not part of the npm package.
|
package/extensions/index.ts
CHANGED
|
@@ -200,7 +200,7 @@ export default async function registerTsgw(pi: ExtensionAPI): Promise<void> {
|
|
|
200
200
|
const defaultSearchMode: WebSearchMode =
|
|
201
201
|
settings.tsSearch === "cached" || settings.tsSearch === "live"
|
|
202
202
|
? settings.tsSearch
|
|
203
|
-
: "
|
|
203
|
+
: "live";
|
|
204
204
|
let tsSearchMode = defaultSearchMode;
|
|
205
205
|
const traceEnabled = settings.traceHeaders === true;
|
|
206
206
|
const modelFilter: ModelFilter = {
|
|
@@ -383,8 +383,11 @@ export default async function registerTsgw(pi: ExtensionAPI): Promise<void> {
|
|
|
383
383
|
ctx.ui.notify("当前模型不支持插件内置联网。", "info");
|
|
384
384
|
return;
|
|
385
385
|
}
|
|
386
|
-
|
|
387
|
-
|
|
386
|
+
if (support === "grok") {
|
|
387
|
+
ctx.ui.notify("当前 Grok 模型内置联网固定为实时;不修改 GPT 会话模式。", "info");
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
const modes: WebSearchMode[] = ["cached", "live"];
|
|
388
391
|
const labels = modes.map((mode) => searchLabel(mode, support));
|
|
389
392
|
const input = args.trim();
|
|
390
393
|
const choice = input || (ctx.hasUI
|
|
@@ -396,7 +399,7 @@ export default async function registerTsgw(pi: ExtensionAPI): Promise<void> {
|
|
|
396
399
|
}
|
|
397
400
|
const mode = modes.find((mode, index) => mode === choice || labels[index] === choice);
|
|
398
401
|
if (!mode) {
|
|
399
|
-
ctx.ui.notify("不支持的模式;GPT
|
|
402
|
+
ctx.ui.notify("不支持的模式;GPT 仅支持 cached/live,Grok 固定实时。", "warning");
|
|
400
403
|
return;
|
|
401
404
|
}
|
|
402
405
|
tsSearchMode = mode;
|
|
@@ -5,6 +5,12 @@
|
|
|
5
5
|
* 与 operations 调度都从这里引用,避免 vendors ↔ operations 循环依赖。
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
+
import type { ProviderModelConfig } from "@earendil-works/pi-coding-agent";
|
|
9
|
+
|
|
10
|
+
// Pi 1.x also includes image/classifier configs; this catalog is chat-only.
|
|
11
|
+
// Structural extraction keeps compatibility with the older chat-only type.
|
|
12
|
+
export type ChatModelConfig = Extract<ProviderModelConfig, { reasoning: boolean }>;
|
|
13
|
+
|
|
8
14
|
export type ThinkingLevel =
|
|
9
15
|
| "off"
|
|
10
16
|
| "minimal"
|
|
@@ -13,7 +19,7 @@ export type ThinkingLevel =
|
|
|
13
19
|
| "high"
|
|
14
20
|
| "xhigh"
|
|
15
21
|
| "max";
|
|
16
|
-
export type WebSearchMode = "
|
|
22
|
+
export type WebSearchMode = "cached" | "live";
|
|
17
23
|
|
|
18
24
|
export interface ModelOperationContext {
|
|
19
25
|
provider: string;
|
|
@@ -28,7 +34,7 @@ type GoogleThinkingField =
|
|
|
28
34
|
| "includeThoughts"
|
|
29
35
|
| "thinkingLevel"
|
|
30
36
|
| "thinkingBudget";
|
|
31
|
-
type ThinkingUpdate = Partial<Pick<Payload, "type" | "clear_thinking">>;
|
|
37
|
+
type ThinkingUpdate = Partial<Pick<Payload, "type" | "clear_thinking" | "keep">>;
|
|
32
38
|
type GoogleThinkingUpdate = Partial<Pick<Payload, GoogleThinkingField>>;
|
|
33
39
|
|
|
34
40
|
export function isPlainObject(value: unknown): value is Payload {
|
|
@@ -109,7 +115,7 @@ export class PayloadWriter {
|
|
|
109
115
|
}
|
|
110
116
|
}
|
|
111
117
|
|
|
112
|
-
const THINKING_FIELDS = ["type", "clear_thinking"] as const;
|
|
118
|
+
const THINKING_FIELDS = ["type", "clear_thinking", "keep"] as const;
|
|
113
119
|
const GOOGLE_THINKING_FIELDS = [
|
|
114
120
|
"includeThoughts",
|
|
115
121
|
"thinkingLevel",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ProviderModelConfig } from "
|
|
1
|
+
import type { ChatModelConfig as ProviderModelConfig } from "./_tools.ts";
|
|
2
2
|
import { anthropicModels } from "./vendors/anthropic.ts";
|
|
3
3
|
import { deepseekModels } from "./vendors/deepseek.ts";
|
|
4
4
|
import { geminiModels } from "./vendors/gemini.ts";
|
|
@@ -47,7 +47,7 @@ export interface ModelFilter {
|
|
|
47
47
|
exclude?: readonly string[];
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
/** 前缀通配匹配:`gpt-
|
|
50
|
+
/** 前缀通配匹配:`gpt-6-*` 命中该系列全部;不带 `*` 时精确匹配。 */
|
|
51
51
|
function matches(pattern: string, id: string): boolean {
|
|
52
52
|
if (pattern.endsWith("*")) return id.startsWith(pattern.slice(0, -1));
|
|
53
53
|
return pattern === id;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
2
|
import {
|
|
3
3
|
mkdirSync,
|
|
4
4
|
readFileSync,
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
unlinkSync,
|
|
7
7
|
writeFileSync,
|
|
8
8
|
} from "node:fs";
|
|
9
|
-
import { dirname } from "node:path";
|
|
9
|
+
import { dirname, resolve } from "node:path";
|
|
10
10
|
|
|
11
11
|
export const DEFAULT_GATEWAY_MODEL_TIMEOUT_MS = 5_000;
|
|
12
12
|
export const DEFAULT_GATEWAY_MODEL_CACHE_TTL_MS = 5 * 60 * 1_000;
|
|
@@ -126,6 +126,8 @@ export async function fetchGatewayModelIds(
|
|
|
126
126
|
return { ok: false, reason: "network", detail: errorDetail(error) };
|
|
127
127
|
}
|
|
128
128
|
|
|
129
|
+
if (timedOut) return { ok: false, reason: "timeout" };
|
|
130
|
+
if (opts.signal?.aborted) return { ok: false, reason: "aborted" };
|
|
129
131
|
if (!response.ok) {
|
|
130
132
|
return { ok: false, reason: "http", status: response.status };
|
|
131
133
|
}
|
|
@@ -139,6 +141,8 @@ export async function fetchGatewayModelIds(
|
|
|
139
141
|
return { ok: false, reason: "invalid-json", detail: errorDetail(error) };
|
|
140
142
|
}
|
|
141
143
|
|
|
144
|
+
if (timedOut) return { ok: false, reason: "timeout" };
|
|
145
|
+
if (opts.signal?.aborted) return { ok: false, reason: "aborted" };
|
|
142
146
|
if (!payload || typeof payload !== "object")
|
|
143
147
|
return { ok: false, reason: "invalid-data" };
|
|
144
148
|
const data = (payload as { data?: unknown }).data;
|
|
@@ -176,17 +180,27 @@ export function createModelCache(
|
|
|
176
180
|
};
|
|
177
181
|
}
|
|
178
182
|
|
|
183
|
+
/** Hash both identity components; no plaintext credentials in cache keys or files. */
|
|
184
|
+
export function gatewayModelCacheScope(baseUrl: string, apiKey: string): string {
|
|
185
|
+
return createHash("sha256")
|
|
186
|
+
.update(JSON.stringify([baseUrl.replace(/\/+$/, ""), apiKey]))
|
|
187
|
+
.digest("hex");
|
|
188
|
+
}
|
|
189
|
+
|
|
179
190
|
function readGatewayModelCacheEntry(
|
|
180
191
|
filePath: string,
|
|
192
|
+
scope: string,
|
|
181
193
|
): ModelCacheEntry | undefined {
|
|
182
194
|
try {
|
|
183
195
|
const payload: unknown = JSON.parse(readFileSync(filePath, "utf8"));
|
|
184
196
|
if (!payload || typeof payload !== "object") return undefined;
|
|
185
|
-
const { storedAt, ids } = payload as {
|
|
197
|
+
const { storedAt, ids, scope: storedScope } = payload as {
|
|
198
|
+
scope?: unknown;
|
|
186
199
|
storedAt?: unknown;
|
|
187
200
|
ids?: unknown;
|
|
188
201
|
};
|
|
189
202
|
if (
|
|
203
|
+
!scope || storedScope !== scope ||
|
|
190
204
|
typeof storedAt !== "number" ||
|
|
191
205
|
!Number.isFinite(storedAt) ||
|
|
192
206
|
storedAt < 0 ||
|
|
@@ -214,6 +228,7 @@ function isCacheEntryFresh(
|
|
|
214
228
|
/** Atomically persist gateway model IDs. Write failures are non-fatal. */
|
|
215
229
|
export function saveGatewayModelCache(
|
|
216
230
|
filePath: string,
|
|
231
|
+
scope: string,
|
|
217
232
|
ids: readonly string[],
|
|
218
233
|
now = Date.now(),
|
|
219
234
|
): void {
|
|
@@ -222,7 +237,7 @@ export function saveGatewayModelCache(
|
|
|
222
237
|
mkdirSync(dirname(filePath), { recursive: true });
|
|
223
238
|
writeFileSync(
|
|
224
239
|
temporaryPath,
|
|
225
|
-
JSON.stringify({ storedAt: now, ids: [...ids] }),
|
|
240
|
+
JSON.stringify({ scope, storedAt: now, ids: [...ids] }),
|
|
226
241
|
{ encoding: "utf8", mode: 0o600 },
|
|
227
242
|
);
|
|
228
243
|
renameSync(temporaryPath, filePath);
|
|
@@ -238,12 +253,13 @@ export function saveGatewayModelCache(
|
|
|
238
253
|
/** Load a persisted gateway model list. Missing or invalid files are ignored. */
|
|
239
254
|
export function loadGatewayModelCache(
|
|
240
255
|
filePath: string,
|
|
256
|
+
scope: string,
|
|
241
257
|
now = Date.now(),
|
|
242
258
|
ttlMs = DEFAULT_GATEWAY_MODEL_CACHE_TTL_MS,
|
|
243
259
|
): LoadedGatewayModelCache | undefined {
|
|
244
260
|
if (!Number.isFinite(ttlMs) || ttlMs < 0)
|
|
245
261
|
throw new RangeError("ttlMs must be a non-negative finite number");
|
|
246
|
-
const entry = readGatewayModelCacheEntry(filePath);
|
|
262
|
+
const entry = readGatewayModelCacheEntry(filePath, scope);
|
|
247
263
|
if (!entry) return undefined;
|
|
248
264
|
return {
|
|
249
265
|
ids: [...entry.ids],
|
|
@@ -252,10 +268,18 @@ export function loadGatewayModelCache(
|
|
|
252
268
|
}
|
|
253
269
|
|
|
254
270
|
const gatewayCaches = new Map<string, ModelCache>();
|
|
255
|
-
|
|
271
|
+
interface PendingRequest {
|
|
272
|
+
promise: Promise<FetchGatewayModelIdsResult>;
|
|
273
|
+
revision: number;
|
|
274
|
+
storedAt: number;
|
|
275
|
+
}
|
|
276
|
+
const inFlightRequests = new Map<string, PendingRequest>();
|
|
277
|
+
const latestRequests = new Map<string, number>();
|
|
278
|
+
// Disk is single-slot per normalized path, independently of the memory scope.
|
|
279
|
+
const latestDiskRequests = new Map<string, number>();
|
|
280
|
+
let revision = 0;
|
|
256
281
|
|
|
257
|
-
function cacheFor(
|
|
258
|
-
const key = baseUrl.replace(/\/+$/, "");
|
|
282
|
+
function cacheFor(key: string): ModelCache {
|
|
259
283
|
let cache = gatewayCaches.get(key);
|
|
260
284
|
if (!cache) {
|
|
261
285
|
cache = createModelCache();
|
|
@@ -276,7 +300,8 @@ export async function getGatewayModelIds(
|
|
|
276
300
|
opts: GetGatewayModelIdsOptions = {},
|
|
277
301
|
now = Date.now(),
|
|
278
302
|
): Promise<GetGatewayModelIdsResult> {
|
|
279
|
-
|
|
303
|
+
if (opts.signal?.aborted) return { ok: false, reason: "aborted" };
|
|
304
|
+
const key = gatewayModelCacheScope(baseUrl, apiKey);
|
|
280
305
|
const cache = cacheFor(key);
|
|
281
306
|
if (!opts.force && cache.isFresh(now)) {
|
|
282
307
|
const entry = cache.get();
|
|
@@ -291,7 +316,7 @@ export async function getGatewayModelIds(
|
|
|
291
316
|
}
|
|
292
317
|
|
|
293
318
|
const diskEntry = opts.cacheFilePath
|
|
294
|
-
? readGatewayModelCacheEntry(opts.cacheFilePath)
|
|
319
|
+
? readGatewayModelCacheEntry(opts.cacheFilePath, key)
|
|
295
320
|
: undefined;
|
|
296
321
|
if (
|
|
297
322
|
!opts.force &&
|
|
@@ -315,32 +340,53 @@ export async function getGatewayModelIds(
|
|
|
315
340
|
const shareRequest = opts.signal === undefined;
|
|
316
341
|
let request = shareRequest ? inFlightRequests.get(requestKey) : undefined;
|
|
317
342
|
if (!request) {
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
343
|
+
const requestRevision = ++revision;
|
|
344
|
+
latestRequests.set(key, requestRevision);
|
|
345
|
+
request = {
|
|
346
|
+
revision: requestRevision,
|
|
347
|
+
storedAt: now,
|
|
348
|
+
promise: fetchGatewayModelIds(baseUrl, apiKey, {
|
|
349
|
+
fetcher: opts.fetcher,
|
|
350
|
+
timeoutMs: opts.timeoutMs,
|
|
351
|
+
signal: opts.signal,
|
|
352
|
+
}),
|
|
353
|
+
};
|
|
323
354
|
if (shareRequest) inFlightRequests.set(requestKey, request);
|
|
324
355
|
}
|
|
325
356
|
|
|
357
|
+
const diskPath = opts.cacheFilePath ? resolve(opts.cacheFilePath) : undefined;
|
|
358
|
+
if (diskPath) {
|
|
359
|
+
// Register every subscriber's path, including shared requests with different
|
|
360
|
+
// destinations. Joining an older request must not roll back a newer fence.
|
|
361
|
+
latestDiskRequests.set(diskPath, Math.max(
|
|
362
|
+
latestDiskRequests.get(diskPath) ?? 0, request.revision,
|
|
363
|
+
));
|
|
364
|
+
}
|
|
365
|
+
|
|
326
366
|
let fetched: FetchGatewayModelIdsResult;
|
|
327
367
|
try {
|
|
328
|
-
fetched = await request;
|
|
368
|
+
fetched = await request.promise;
|
|
329
369
|
} finally {
|
|
330
370
|
if (shareRequest && inFlightRequests.get(requestKey) === request)
|
|
331
371
|
inFlightRequests.delete(requestKey);
|
|
332
372
|
}
|
|
333
373
|
|
|
374
|
+
// Cancellation never falls back or writes, even if a fetcher ignores its signal.
|
|
375
|
+
if (opts.signal?.aborted) return { ok: false, reason: "aborted" };
|
|
334
376
|
if (fetched.ok) {
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
377
|
+
// Latest-started wins even if the newer request fails or aborts.
|
|
378
|
+
// Superseded successes still return to their callers without cache writes.
|
|
379
|
+
if (latestRequests.get(key) === request.revision) {
|
|
380
|
+
cache.set(fetched.ids, request.storedAt);
|
|
381
|
+
if (diskPath && latestDiskRequests.get(diskPath) === request.revision)
|
|
382
|
+
saveGatewayModelCache(diskPath, key, fetched.ids, request.storedAt);
|
|
383
|
+
}
|
|
338
384
|
return {
|
|
339
385
|
ok: true,
|
|
340
386
|
ids: [...fetched.ids],
|
|
341
387
|
cached: false,
|
|
342
388
|
stale: false,
|
|
343
|
-
storedAt:
|
|
389
|
+
storedAt: request.storedAt,
|
|
344
390
|
};
|
|
345
391
|
}
|
|
346
392
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ProviderModelConfig } from "
|
|
1
|
+
import type { ChatModelConfig as ProviderModelConfig } from "../_tools.ts";
|
|
2
2
|
|
|
3
3
|
// DeepSeek Responses 将 developer role 当作 user,且不支持 OpenAI 的
|
|
4
4
|
// prompt_cache_retention;其余 reasoning 字段交给 Pi 的 Responses adapter。
|
|
@@ -8,7 +8,7 @@ const DEEPSEEK_RESPONSES_COMPAT = {
|
|
|
8
8
|
};
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
* DeepSeek
|
|
11
|
+
* DeepSeek(深度求索)系列:V4.1 Flash 与 V4 Pro。
|
|
12
12
|
* 官方文档:https://api-docs.deepseek.com/guides/responses_api
|
|
13
13
|
*/
|
|
14
14
|
export function deepseekModels(root: string): ProviderModelConfig[] {
|
|
@@ -35,27 +35,6 @@ export function deepseekModels(root: string): ProviderModelConfig[] {
|
|
|
35
35
|
max: "max",
|
|
36
36
|
},
|
|
37
37
|
},
|
|
38
|
-
{
|
|
39
|
-
id: "deepseek-v4-flash",
|
|
40
|
-
name: "DeepSeek V4 Flash (Legacy Alias)",
|
|
41
|
-
api: "openai-responses",
|
|
42
|
-
baseUrl: v1,
|
|
43
|
-
reasoning: true,
|
|
44
|
-
input: ["text", "image"],
|
|
45
|
-
cost: { input: 0.1475, output: 0.295, cacheRead: 0.00295, cacheWrite: 0 },
|
|
46
|
-
contextWindow: 1000000,
|
|
47
|
-
maxTokens: 384000,
|
|
48
|
-
compat: DEEPSEEK_RESPONSES_COMPAT,
|
|
49
|
-
thinkingLevelMap: {
|
|
50
|
-
off: "none",
|
|
51
|
-
minimal: "low",
|
|
52
|
-
low: "low",
|
|
53
|
-
medium: "high",
|
|
54
|
-
high: "high",
|
|
55
|
-
xhigh: "high",
|
|
56
|
-
max: "max",
|
|
57
|
-
},
|
|
58
|
-
},
|
|
59
38
|
{
|
|
60
39
|
id: "deepseek-v4-pro",
|
|
61
40
|
name: "DeepSeek V4 Pro",
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import type { ProviderModelConfig } from "
|
|
2
|
-
import {
|
|
1
|
+
import type { ChatModelConfig as ProviderModelConfig } from "../_tools.ts";
|
|
2
|
+
import { type PayloadWriter, type ThinkingApplier, type ThinkingLevel } from "../_tools.ts";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Google Gemini 系列(google-generative-ai 协议)。
|
|
6
6
|
* 官方文档:https://ai.google.dev/gemini-api/docs
|
|
7
|
+
* GenerateContent 档位/预算:https://docs.cloud.google.com/vertex-ai/generative-ai/docs/thinking
|
|
8
|
+
* gemini-flash 别名保留 budget 策略,其他保留型号使用 level。网关别名的路由仍需网关侧验证。
|
|
7
9
|
*/
|
|
8
10
|
export function geminiModels(root: string): ProviderModelConfig[] {
|
|
9
11
|
const gemini = `${root}/gemini`;
|
|
@@ -66,51 +68,6 @@ export function geminiModels(root: string): ProviderModelConfig[] {
|
|
|
66
68
|
contextWindow: 1048576,
|
|
67
69
|
maxTokens: 65536,
|
|
68
70
|
},
|
|
69
|
-
{
|
|
70
|
-
id: "gemini-3.1-pro-low",
|
|
71
|
-
name: "Gemini 3.1 Pro Preview",
|
|
72
|
-
api: "google-generative-ai",
|
|
73
|
-
baseUrl: gemini,
|
|
74
|
-
reasoning: true,
|
|
75
|
-
input: ["text", "image"],
|
|
76
|
-
// 网关思考档位别名(low),规格继承 gemini-3.1-pro-preview。
|
|
77
|
-
cost: { input: 2, output: 12, cacheRead: 0.2, cacheWrite: 0 },
|
|
78
|
-
contextWindow: 1048576,
|
|
79
|
-
maxTokens: 65536,
|
|
80
|
-
},
|
|
81
|
-
{
|
|
82
|
-
id: "gemini-2.5-pro",
|
|
83
|
-
name: "Gemini 2.5 Pro",
|
|
84
|
-
api: "google-generative-ai",
|
|
85
|
-
baseUrl: gemini,
|
|
86
|
-
reasoning: true,
|
|
87
|
-
input: ["text", "image"],
|
|
88
|
-
cost: { input: 1.25, output: 10, cacheRead: 0.125, cacheWrite: 0 },
|
|
89
|
-
contextWindow: 1048576,
|
|
90
|
-
maxTokens: 65536,
|
|
91
|
-
},
|
|
92
|
-
{
|
|
93
|
-
id: "gemini-2.5-flash",
|
|
94
|
-
name: "Gemini 2.5 Flash",
|
|
95
|
-
api: "google-generative-ai",
|
|
96
|
-
baseUrl: gemini,
|
|
97
|
-
reasoning: true,
|
|
98
|
-
input: ["text", "image"],
|
|
99
|
-
cost: { input: 0.3, output: 2.5, cacheRead: 0.03, cacheWrite: 0 },
|
|
100
|
-
contextWindow: 1048576,
|
|
101
|
-
maxTokens: 65536,
|
|
102
|
-
},
|
|
103
|
-
{
|
|
104
|
-
id: "gemini-2.5-flash-lite",
|
|
105
|
-
name: "Gemini 2.5 Flash-Lite",
|
|
106
|
-
api: "google-generative-ai",
|
|
107
|
-
baseUrl: gemini,
|
|
108
|
-
reasoning: true,
|
|
109
|
-
input: ["text", "image"],
|
|
110
|
-
cost: { input: 0.1, output: 0.4, cacheRead: 0.01, cacheWrite: 0 },
|
|
111
|
-
contextWindow: 1048576,
|
|
112
|
-
maxTokens: 65536,
|
|
113
|
-
},
|
|
114
71
|
];
|
|
115
72
|
}
|
|
116
73
|
|
|
@@ -135,14 +92,18 @@ function applyGeminiPro(writer: PayloadWriter, level: ThinkingLevel): void {
|
|
|
135
92
|
writer.setGoogleThinking({ includeThoughts: level !== "off", thinkingLevel }, ["thinkingBudget"]);
|
|
136
93
|
}
|
|
137
94
|
|
|
95
|
+
// 3.5 Flash 支持 MINIMAL/LOW/MEDIUM/HIGH,不使用 token budget。
|
|
96
|
+
function applyGemini35Flash(writer: PayloadWriter, level: ThinkingLevel): void {
|
|
97
|
+
writer.remove("reasoning_effort");
|
|
98
|
+
const thinkingLevel = level === "off" || level === "minimal" ? "MINIMAL"
|
|
99
|
+
: level === "low" ? "LOW" : level === "medium" ? "MEDIUM" : "HIGH";
|
|
100
|
+
writer.setGoogleThinking({ includeThoughts: level !== "off", thinkingLevel }, ["thinkingBudget"]);
|
|
101
|
+
}
|
|
102
|
+
|
|
138
103
|
export const geminiThinking: Record<string, ThinkingApplier> = {
|
|
139
104
|
"gemini-flash": (w, c) => applyGeminiFlash(w, c.thinkingLevel),
|
|
140
|
-
"gemini-3.5-flash": (w, c) =>
|
|
141
|
-
"gemini-3.5-flash-low": (w, c) =>
|
|
142
|
-
"gemini-3.5-flash-extra-low": (w, c) =>
|
|
143
|
-
"gemini-2.5-flash": (w, c) => applyGeminiFlash(w, c.thinkingLevel),
|
|
144
|
-
"gemini-2.5-flash-lite": (w, c) => applyGeminiFlash(w, c.thinkingLevel),
|
|
105
|
+
"gemini-3.5-flash": (w, c) => applyGemini35Flash(w, c.thinkingLevel),
|
|
106
|
+
"gemini-3.5-flash-low": (w, c) => applyGemini35Flash(w, c.thinkingLevel),
|
|
107
|
+
"gemini-3.5-flash-extra-low": (w, c) => applyGemini35Flash(w, c.thinkingLevel),
|
|
145
108
|
"gemini-pro": (w, c) => applyGeminiPro(w, c.thinkingLevel),
|
|
146
|
-
"gemini-3.1-pro-low": (w, c) => applyGeminiPro(w, c.thinkingLevel),
|
|
147
|
-
"gemini-2.5-pro": (w, c) => applyGeminiPro(w, c.thinkingLevel),
|
|
148
109
|
};
|