pi-tsgw 0.0.5 → 0.0.7
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 +5 -0
- package/extensions/index.ts +108 -12
- package/extensions/models/gateway-catalog.ts +26 -10
- package/extensions/models/operations.ts +0 -2
- package/extensions/models/vendors/anthropic.ts +7 -149
- package/extensions/models/vendors/deepseek.ts +43 -39
- package/extensions/models/vendors/openai.ts +21 -154
- package/extensions/models/web-search.ts +11 -5
- package/extensions/ts-search/ts-search.ts +183 -50
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -85,6 +85,11 @@ global whitelist: models matching neither list remain available. To register
|
|
|
85
85
|
only selected models, use `"excludeModels": ["*"]` and pull them back through
|
|
86
86
|
`includeModels`.
|
|
87
87
|
|
|
88
|
+
Opening `/model` refreshes the gateway catalog in the background, with a
|
|
89
|
+
five-minute cache to avoid repeated requests. Run `/tsgw-refresh` to bypass the
|
|
90
|
+
cache and force an immediate refresh without restarting or reloading Pi. If a
|
|
91
|
+
refresh fails, the last successful model list remains active.
|
|
92
|
+
|
|
88
93
|
## Development
|
|
89
94
|
|
|
90
95
|
```bash
|
package/extensions/index.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import {
|
|
3
|
-
CONFIG_DIR_NAME,
|
|
4
3
|
getAgentDir,
|
|
5
4
|
readStoredCredential,
|
|
6
5
|
} from "@earendil-works/pi-coding-agent";
|
|
7
6
|
import { readFileSync } from "node:fs";
|
|
8
|
-
import { homedir } from "node:os";
|
|
9
7
|
import { join } from "node:path";
|
|
10
8
|
import { randomUUID } from "node:crypto";
|
|
11
9
|
import {
|
|
@@ -124,9 +122,21 @@ function gatewayHasModel(gatewayIds: ReadonlySet<string>, id: string): boolean {
|
|
|
124
122
|
return false;
|
|
125
123
|
}
|
|
126
124
|
|
|
127
|
-
/**
|
|
125
|
+
/** 模型缓存文件:<agent-dir>/tsgw/models-cache.json。 */
|
|
128
126
|
function gatewayModelCachePath(): string {
|
|
129
|
-
return join(
|
|
127
|
+
return join(getAgentDir(), "tsgw", "models-cache.json");
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function effectiveModelsForGateway(
|
|
131
|
+
root: string,
|
|
132
|
+
filter: ModelFilter | undefined,
|
|
133
|
+
gatewayIds: ReadonlySet<string> | undefined,
|
|
134
|
+
): ReturnType<typeof modelsForRoot> {
|
|
135
|
+
const staticModels = modelsForRoot(root);
|
|
136
|
+
const intersected = gatewayIds
|
|
137
|
+
? staticModels.filter((model) => gatewayHasModel(gatewayIds, model.id))
|
|
138
|
+
: staticModels;
|
|
139
|
+
return filterModels(intersected, filter);
|
|
130
140
|
}
|
|
131
141
|
|
|
132
142
|
/**
|
|
@@ -136,12 +146,11 @@ function gatewayModelCachePath(): string {
|
|
|
136
146
|
async function resolveEffectiveModels(
|
|
137
147
|
root: string,
|
|
138
148
|
filter: ModelFilter | undefined,
|
|
149
|
+
cacheFilePath: string,
|
|
139
150
|
): Promise<{
|
|
140
151
|
models: ReturnType<typeof modelsForRoot>;
|
|
141
152
|
gatewayIds: ReadonlySet<string> | undefined;
|
|
142
153
|
}> {
|
|
143
|
-
const staticModels = modelsForRoot(root);
|
|
144
|
-
|
|
145
154
|
// 凭据解析失败不阻塞:无凭据时直接跳过网关层。
|
|
146
155
|
let apiKey = "";
|
|
147
156
|
try {
|
|
@@ -155,24 +164,24 @@ async function resolveEffectiveModels(
|
|
|
155
164
|
if (apiKey) {
|
|
156
165
|
try {
|
|
157
166
|
const gateway = await getGatewayModelIds(root, apiKey, {
|
|
158
|
-
cacheFilePath
|
|
167
|
+
cacheFilePath,
|
|
159
168
|
});
|
|
160
169
|
if (gateway.ok) gatewayIds = new Set(gateway.ids);
|
|
161
170
|
else
|
|
162
171
|
console.warn(
|
|
163
172
|
`TSGW: gateway model list unavailable (${gateway.reason ?? "unknown"}); using static catalog.`,
|
|
164
173
|
);
|
|
165
|
-
} catch
|
|
174
|
+
} catch {
|
|
166
175
|
console.warn(
|
|
167
176
|
`TSGW: gateway model list load failed; using static catalog.`,
|
|
168
177
|
);
|
|
169
178
|
}
|
|
170
179
|
}
|
|
171
180
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
181
|
+
return {
|
|
182
|
+
models: effectiveModelsForGateway(root, filter, gatewayIds),
|
|
183
|
+
gatewayIds,
|
|
184
|
+
};
|
|
176
185
|
}
|
|
177
186
|
|
|
178
187
|
export default async function registerTsgw(pi: ExtensionAPI): Promise<void> {
|
|
@@ -187,6 +196,7 @@ export default async function registerTsgw(pi: ExtensionAPI): Promise<void> {
|
|
|
187
196
|
include: settings.includeModels,
|
|
188
197
|
exclude: settings.excludeModels,
|
|
189
198
|
};
|
|
199
|
+
const cacheFilePath = gatewayModelCachePath();
|
|
190
200
|
let requestState: RequestState | undefined;
|
|
191
201
|
|
|
192
202
|
const refreshRequestState = (ctx: {
|
|
@@ -200,13 +210,99 @@ export default async function registerTsgw(pi: ExtensionAPI): Promise<void> {
|
|
|
200
210
|
const { models, gatewayIds } = await resolveEffectiveModels(
|
|
201
211
|
root,
|
|
202
212
|
modelFilter,
|
|
213
|
+
cacheFilePath,
|
|
203
214
|
);
|
|
215
|
+
let latestModels = models;
|
|
204
216
|
pi.registerProvider(PROVIDER_ID, {
|
|
205
217
|
name: "TSGW",
|
|
206
218
|
baseUrl: `${root}/v1`,
|
|
207
219
|
api: "openai-completions",
|
|
208
220
|
apiKey: "$TSGW_API_KEY",
|
|
209
221
|
models,
|
|
222
|
+
async refreshModels(context) {
|
|
223
|
+
if (!context.allowNetwork) return latestModels;
|
|
224
|
+
context.signal.throwIfAborted();
|
|
225
|
+
const credential = context.credential;
|
|
226
|
+
if (credential?.type !== "api_key" || !credential.key)
|
|
227
|
+
throw new Error("TSGW API key is unavailable for model refresh.");
|
|
228
|
+
|
|
229
|
+
const gateway = await getGatewayModelIds(root, credential.key, {
|
|
230
|
+
cacheFilePath,
|
|
231
|
+
force: context.force === true,
|
|
232
|
+
signal: context.signal,
|
|
233
|
+
});
|
|
234
|
+
context.signal.throwIfAborted();
|
|
235
|
+
if (!gateway.ok)
|
|
236
|
+
throw new Error(
|
|
237
|
+
`TSGW gateway model refresh failed (${gateway.reason}).`,
|
|
238
|
+
);
|
|
239
|
+
if (gateway.stale)
|
|
240
|
+
throw new Error(
|
|
241
|
+
`TSGW gateway model refresh failed (${gateway.fallbackReason ?? "unknown"}); keeping the last successful catalog.`,
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
latestModels = effectiveModelsForGateway(
|
|
245
|
+
root,
|
|
246
|
+
modelFilter,
|
|
247
|
+
new Set(gateway.ids),
|
|
248
|
+
);
|
|
249
|
+
return latestModels;
|
|
250
|
+
},
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
pi.registerCommand("tsgw-refresh", {
|
|
254
|
+
description: "Force-refresh the TSGW gateway model catalog",
|
|
255
|
+
handler: async (_args, ctx) => {
|
|
256
|
+
await ctx.waitForIdle();
|
|
257
|
+
if (!ctx.modelRegistry.getProviderAuthStatus(PROVIDER_ID).configured) {
|
|
258
|
+
ctx.ui.notify(
|
|
259
|
+
"TSGW model refresh skipped: configure an API key with /login first.",
|
|
260
|
+
"warning",
|
|
261
|
+
);
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
const count = (): number =>
|
|
265
|
+
ctx.modelRegistry
|
|
266
|
+
.getAvailable()
|
|
267
|
+
.filter((model) => model.provider === PROVIDER_ID).length;
|
|
268
|
+
const before = count();
|
|
269
|
+
try {
|
|
270
|
+
// Pi 0.84+ returns cancellation/errors; older supported Pi versions
|
|
271
|
+
// resolve void, so treat missing result metadata as success.
|
|
272
|
+
const result = (await ctx.modelRegistry.refresh({
|
|
273
|
+
providers: [PROVIDER_ID],
|
|
274
|
+
force: true,
|
|
275
|
+
})) as
|
|
276
|
+
| {
|
|
277
|
+
aborted?: boolean;
|
|
278
|
+
errors?: ReadonlyMap<string, Error>;
|
|
279
|
+
}
|
|
280
|
+
| undefined;
|
|
281
|
+
const after = count();
|
|
282
|
+
const error = result?.errors?.get(PROVIDER_ID);
|
|
283
|
+
if (result?.aborted) {
|
|
284
|
+
ctx.ui.notify(
|
|
285
|
+
`TSGW model refresh cancelled (${before} → ${after}).`,
|
|
286
|
+
"warning",
|
|
287
|
+
);
|
|
288
|
+
} else if (error) {
|
|
289
|
+
ctx.ui.notify(
|
|
290
|
+
`TSGW model refresh failed (${before} → ${after}): ${error.message}`,
|
|
291
|
+
"error",
|
|
292
|
+
);
|
|
293
|
+
} else {
|
|
294
|
+
ctx.ui.notify(
|
|
295
|
+
`TSGW models refreshed (${before} → ${after}).`,
|
|
296
|
+
"info",
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
} catch (error) {
|
|
300
|
+
ctx.ui.notify(
|
|
301
|
+
`TSGW model refresh failed (${before} → ${count()}): ${error instanceof Error ? error.message : String(error)}`,
|
|
302
|
+
"error",
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
},
|
|
210
306
|
});
|
|
211
307
|
|
|
212
308
|
// ts_search 条件注册:固定白名单后端与可用模型列表有交集才注册,
|
|
@@ -14,6 +14,7 @@ export const DEFAULT_GATEWAY_MODEL_CACHE_TTL_MS = 5 * 60 * 1_000;
|
|
|
14
14
|
export type GatewayModelFailureReason =
|
|
15
15
|
| "http"
|
|
16
16
|
| "timeout"
|
|
17
|
+
| "aborted"
|
|
17
18
|
| "network"
|
|
18
19
|
| "invalid-json"
|
|
19
20
|
| "invalid-data"
|
|
@@ -27,10 +28,12 @@ export type GatewayFetch = (
|
|
|
27
28
|
export interface FetchGatewayModelIdsOptions {
|
|
28
29
|
timeoutMs?: number;
|
|
29
30
|
fetcher?: GatewayFetch;
|
|
31
|
+
signal?: AbortSignal;
|
|
30
32
|
}
|
|
31
33
|
|
|
32
34
|
export interface GetGatewayModelIdsOptions extends FetchGatewayModelIdsOptions {
|
|
33
35
|
cacheFilePath?: string;
|
|
36
|
+
force?: boolean;
|
|
34
37
|
}
|
|
35
38
|
|
|
36
39
|
export type FetchGatewayModelIdsResult =
|
|
@@ -94,8 +97,12 @@ export async function fetchGatewayModelIds(
|
|
|
94
97
|
): Promise<FetchGatewayModelIdsResult> {
|
|
95
98
|
const timeoutMs = opts.timeoutMs ?? DEFAULT_GATEWAY_MODEL_TIMEOUT_MS;
|
|
96
99
|
const fetcher = opts.fetcher ?? globalThis.fetch;
|
|
100
|
+
if (opts.signal?.aborted) return { ok: false, reason: "aborted" };
|
|
101
|
+
|
|
97
102
|
const controller = new AbortController();
|
|
98
103
|
let timedOut = false;
|
|
104
|
+
const abortFromCaller = (): void => controller.abort(opts.signal?.reason);
|
|
105
|
+
opts.signal?.addEventListener("abort", abortFromCaller, { once: true });
|
|
99
106
|
const timeout = setTimeout(
|
|
100
107
|
() => {
|
|
101
108
|
timedOut = true;
|
|
@@ -113,9 +120,9 @@ export async function fetchGatewayModelIds(
|
|
|
113
120
|
signal: controller.signal,
|
|
114
121
|
});
|
|
115
122
|
} catch (error) {
|
|
116
|
-
return
|
|
117
|
-
|
|
118
|
-
|
|
123
|
+
if (timedOut) return { ok: false, reason: "timeout" };
|
|
124
|
+
if (opts.signal?.aborted) return { ok: false, reason: "aborted" };
|
|
125
|
+
return { ok: false, reason: "network", detail: errorDetail(error) };
|
|
119
126
|
}
|
|
120
127
|
|
|
121
128
|
if (!response.ok) {
|
|
@@ -126,9 +133,9 @@ export async function fetchGatewayModelIds(
|
|
|
126
133
|
try {
|
|
127
134
|
payload = await response.json();
|
|
128
135
|
} catch (error) {
|
|
129
|
-
return
|
|
130
|
-
|
|
131
|
-
|
|
136
|
+
if (timedOut) return { ok: false, reason: "timeout" };
|
|
137
|
+
if (opts.signal?.aborted) return { ok: false, reason: "aborted" };
|
|
138
|
+
return { ok: false, reason: "invalid-json", detail: errorDetail(error) };
|
|
132
139
|
}
|
|
133
140
|
|
|
134
141
|
if (!payload || typeof payload !== "object")
|
|
@@ -140,6 +147,7 @@ export async function fetchGatewayModelIds(
|
|
|
140
147
|
return { ok: true, ids };
|
|
141
148
|
} finally {
|
|
142
149
|
clearTimeout(timeout);
|
|
150
|
+
opts.signal?.removeEventListener("abort", abortFromCaller);
|
|
143
151
|
}
|
|
144
152
|
}
|
|
145
153
|
|
|
@@ -269,7 +277,7 @@ export async function getGatewayModelIds(
|
|
|
269
277
|
): Promise<GetGatewayModelIdsResult> {
|
|
270
278
|
const key = baseUrl.replace(/\/+$/, "");
|
|
271
279
|
const cache = cacheFor(key);
|
|
272
|
-
if (cache.isFresh(now)) {
|
|
280
|
+
if (!opts.force && cache.isFresh(now)) {
|
|
273
281
|
const entry = cache.get();
|
|
274
282
|
if (entry) return { ok: true, ids: entry.ids, cached: true, stale: false };
|
|
275
283
|
}
|
|
@@ -278,6 +286,7 @@ export async function getGatewayModelIds(
|
|
|
278
286
|
? readGatewayModelCacheEntry(opts.cacheFilePath)
|
|
279
287
|
: undefined;
|
|
280
288
|
if (
|
|
289
|
+
!opts.force &&
|
|
281
290
|
diskEntry &&
|
|
282
291
|
isCacheEntryFresh(diskEntry, now, DEFAULT_GATEWAY_MODEL_CACHE_TTL_MS)
|
|
283
292
|
) {
|
|
@@ -290,20 +299,27 @@ export async function getGatewayModelIds(
|
|
|
290
299
|
};
|
|
291
300
|
}
|
|
292
301
|
|
|
293
|
-
|
|
302
|
+
const requestKey = `${key}\0${opts.force ? "force" : "normal"}`;
|
|
303
|
+
// A shared request cannot safely inherit one caller's AbortSignal: Pi may
|
|
304
|
+
// supersede that refresh while a newer caller is still waiting. Only
|
|
305
|
+
// signal-free calls share in-flight work; abortable calls own their fetch.
|
|
306
|
+
const shareRequest = opts.signal === undefined;
|
|
307
|
+
let request = shareRequest ? inFlightRequests.get(requestKey) : undefined;
|
|
294
308
|
if (!request) {
|
|
295
309
|
request = fetchGatewayModelIds(key, apiKey, {
|
|
296
310
|
fetcher: opts.fetcher,
|
|
297
311
|
timeoutMs: opts.timeoutMs,
|
|
312
|
+
signal: opts.signal,
|
|
298
313
|
});
|
|
299
|
-
inFlightRequests.set(
|
|
314
|
+
if (shareRequest) inFlightRequests.set(requestKey, request);
|
|
300
315
|
}
|
|
301
316
|
|
|
302
317
|
let fetched: FetchGatewayModelIdsResult;
|
|
303
318
|
try {
|
|
304
319
|
fetched = await request;
|
|
305
320
|
} finally {
|
|
306
|
-
if (inFlightRequests.get(
|
|
321
|
+
if (shareRequest && inFlightRequests.get(requestKey) === request)
|
|
322
|
+
inFlightRequests.delete(requestKey);
|
|
307
323
|
}
|
|
308
324
|
|
|
309
325
|
if (fetched.ok) {
|
|
@@ -12,7 +12,6 @@ import {
|
|
|
12
12
|
type ThinkingApplier,
|
|
13
13
|
} from "./_tools.ts";
|
|
14
14
|
import { applyBuiltinSearchTool } from "./web-search.ts";
|
|
15
|
-
import { deepseekThinking } from "./vendors/deepseek.ts";
|
|
16
15
|
import { geminiThinking } from "./vendors/gemini.ts";
|
|
17
16
|
import { glmThinking } from "./vendors/glm.ts";
|
|
18
17
|
import { grokThinking } from "./vendors/grok.ts";
|
|
@@ -27,7 +26,6 @@ import { qwenThinking } from "./vendors/qwen.ts";
|
|
|
27
26
|
const TSGW_PROVIDER = "tsgw";
|
|
28
27
|
|
|
29
28
|
const THINKING_STRATEGIES: Record<string, ThinkingApplier> = {
|
|
30
|
-
...deepseekThinking,
|
|
31
29
|
...glmThinking,
|
|
32
30
|
...mimoThinking,
|
|
33
31
|
...minimaxThinking,
|
|
@@ -4,6 +4,7 @@ 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
8
|
*/
|
|
8
9
|
export function anthropicModels(root: string): ProviderModelConfig[] {
|
|
9
10
|
const anthropic = `${root}/anthropic`;
|
|
@@ -27,8 +28,8 @@ export function anthropicModels(root: string): ProviderModelConfig[] {
|
|
|
27
28
|
compat: ANTHROPIC_COMPAT,
|
|
28
29
|
},
|
|
29
30
|
{
|
|
30
|
-
id: "claude-opus-
|
|
31
|
-
name: "Claude Opus
|
|
31
|
+
id: "claude-opus-5",
|
|
32
|
+
name: "Claude Opus 5",
|
|
32
33
|
api: "anthropic-messages",
|
|
33
34
|
baseUrl: anthropic,
|
|
34
35
|
reasoning: true,
|
|
@@ -36,158 +37,15 @@ export function anthropicModels(root: string): ProviderModelConfig[] {
|
|
|
36
37
|
low: "low",
|
|
37
38
|
medium: "medium",
|
|
38
39
|
high: "high",
|
|
40
|
+
xhigh: "xhigh",
|
|
39
41
|
max: "max",
|
|
40
42
|
},
|
|
41
43
|
input: ["text", "image"],
|
|
42
|
-
cost: { input:
|
|
43
|
-
contextWindow: 200000,
|
|
44
|
-
maxTokens: 128000,
|
|
45
|
-
compat: ANTHROPIC_COMPAT,
|
|
46
|
-
},
|
|
47
|
-
{
|
|
48
|
-
id: "claude-sonnet",
|
|
49
|
-
name: "Claude Sonnet 4.6",
|
|
50
|
-
api: "anthropic-messages",
|
|
51
|
-
baseUrl: anthropic,
|
|
52
|
-
reasoning: true,
|
|
53
|
-
thinkingLevelMap: {
|
|
54
|
-
low: "low",
|
|
55
|
-
medium: "medium",
|
|
56
|
-
high: "high",
|
|
57
|
-
max: "max",
|
|
58
|
-
},
|
|
59
|
-
input: ["text", "image"],
|
|
60
|
-
cost: { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 },
|
|
61
|
-
contextWindow: 200000,
|
|
62
|
-
maxTokens: 64000,
|
|
63
|
-
compat: ANTHROPIC_COMPAT,
|
|
64
|
-
},
|
|
65
|
-
{
|
|
66
|
-
id: "claude-opus-4",
|
|
67
|
-
name: "Claude Opus 4",
|
|
68
|
-
api: "anthropic-messages",
|
|
69
|
-
baseUrl: anthropic,
|
|
70
|
-
reasoning: true,
|
|
71
|
-
thinkingLevelMap: {
|
|
72
|
-
low: "low",
|
|
73
|
-
medium: "medium",
|
|
74
|
-
high: "high",
|
|
75
|
-
max: "max",
|
|
76
|
-
},
|
|
77
|
-
input: ["text", "image"],
|
|
78
|
-
cost: { input: 15, output: 75, cacheRead: 1.5, cacheWrite: 18.75 },
|
|
79
|
-
contextWindow: 200000,
|
|
80
|
-
maxTokens: 32000,
|
|
81
|
-
compat: ANTHROPIC_COMPAT,
|
|
82
|
-
},
|
|
83
|
-
{
|
|
84
|
-
id: "claude-opus-4.1",
|
|
85
|
-
name: "Claude Opus 4.1",
|
|
86
|
-
api: "anthropic-messages",
|
|
87
|
-
baseUrl: anthropic,
|
|
88
|
-
reasoning: true,
|
|
89
|
-
thinkingLevelMap: {
|
|
90
|
-
low: "low",
|
|
91
|
-
medium: "medium",
|
|
92
|
-
high: "high",
|
|
93
|
-
max: "max",
|
|
94
|
-
},
|
|
95
|
-
input: ["text", "image"],
|
|
96
|
-
cost: { input: 15, output: 75, cacheRead: 1.5, cacheWrite: 18.75 },
|
|
97
|
-
contextWindow: 200000,
|
|
98
|
-
maxTokens: 32000,
|
|
99
|
-
compat: ANTHROPIC_COMPAT,
|
|
100
|
-
},
|
|
101
|
-
{
|
|
102
|
-
id: "claude-opus-4.5",
|
|
103
|
-
name: "Claude Opus 4.5",
|
|
104
|
-
api: "anthropic-messages",
|
|
105
|
-
baseUrl: anthropic,
|
|
106
|
-
reasoning: true,
|
|
107
|
-
thinkingLevelMap: {
|
|
108
|
-
low: "low",
|
|
109
|
-
medium: "medium",
|
|
110
|
-
high: "high",
|
|
111
|
-
max: "max",
|
|
112
|
-
},
|
|
113
|
-
input: ["text", "image"],
|
|
114
|
-
cost: { input: 5, output: 25, cacheRead: 0.5, cacheWrite: 6.25 },
|
|
115
|
-
contextWindow: 200000,
|
|
116
|
-
maxTokens: 64000,
|
|
117
|
-
compat: ANTHROPIC_COMPAT,
|
|
118
|
-
},
|
|
119
|
-
{
|
|
120
|
-
id: "claude-opus-4.6",
|
|
121
|
-
name: "Claude Opus 4.6",
|
|
122
|
-
api: "anthropic-messages",
|
|
123
|
-
baseUrl: anthropic,
|
|
124
|
-
reasoning: true,
|
|
125
|
-
thinkingLevelMap: {
|
|
126
|
-
low: "low",
|
|
127
|
-
medium: "medium",
|
|
128
|
-
high: "high",
|
|
129
|
-
max: "max",
|
|
130
|
-
},
|
|
131
|
-
input: ["text", "image"],
|
|
132
|
-
cost: { input: 5, output: 25, cacheRead: 0.5, cacheWrite: 6.25 },
|
|
133
|
-
contextWindow: 1000000,
|
|
134
|
-
maxTokens: 128000,
|
|
135
|
-
compat: ANTHROPIC_COMPAT,
|
|
136
|
-
},
|
|
137
|
-
{
|
|
138
|
-
id: "claude-opus-4.7",
|
|
139
|
-
name: "Claude Opus 4.7",
|
|
140
|
-
api: "anthropic-messages",
|
|
141
|
-
baseUrl: anthropic,
|
|
142
|
-
reasoning: true,
|
|
143
|
-
thinkingLevelMap: {
|
|
144
|
-
low: "low",
|
|
145
|
-
medium: "medium",
|
|
146
|
-
high: "high",
|
|
147
|
-
max: "max",
|
|
148
|
-
},
|
|
149
|
-
input: ["text", "image"],
|
|
150
|
-
cost: { input: 5, output: 25, cacheRead: 0.5, cacheWrite: 6.25 },
|
|
44
|
+
cost: { input: 10, output: 50, cacheRead: 1, cacheWrite: 12.5 },
|
|
151
45
|
contextWindow: 1000000,
|
|
152
46
|
maxTokens: 128000,
|
|
153
47
|
compat: ANTHROPIC_COMPAT,
|
|
154
48
|
},
|
|
155
|
-
{
|
|
156
|
-
id: "claude-sonnet-4",
|
|
157
|
-
name: "Claude Sonnet 4",
|
|
158
|
-
api: "anthropic-messages",
|
|
159
|
-
baseUrl: anthropic,
|
|
160
|
-
reasoning: true,
|
|
161
|
-
thinkingLevelMap: {
|
|
162
|
-
low: "low",
|
|
163
|
-
medium: "medium",
|
|
164
|
-
high: "high",
|
|
165
|
-
max: "max",
|
|
166
|
-
},
|
|
167
|
-
input: ["text", "image"],
|
|
168
|
-
cost: { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 },
|
|
169
|
-
contextWindow: 200000,
|
|
170
|
-
maxTokens: 64000,
|
|
171
|
-
compat: ANTHROPIC_COMPAT,
|
|
172
|
-
},
|
|
173
|
-
{
|
|
174
|
-
id: "claude-sonnet-4.5",
|
|
175
|
-
name: "Claude Sonnet 4.5",
|
|
176
|
-
api: "anthropic-messages",
|
|
177
|
-
baseUrl: anthropic,
|
|
178
|
-
reasoning: true,
|
|
179
|
-
thinkingLevelMap: {
|
|
180
|
-
low: "low",
|
|
181
|
-
medium: "medium",
|
|
182
|
-
high: "high",
|
|
183
|
-
max: "max",
|
|
184
|
-
},
|
|
185
|
-
input: ["text", "image"],
|
|
186
|
-
cost: { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 },
|
|
187
|
-
contextWindow: 200000,
|
|
188
|
-
maxTokens: 64000,
|
|
189
|
-
compat: ANTHROPIC_COMPAT,
|
|
190
|
-
},
|
|
191
49
|
{
|
|
192
50
|
id: "claude-sonnet-5",
|
|
193
51
|
name: "Claude Sonnet 5",
|
|
@@ -198,11 +56,11 @@ export function anthropicModels(root: string): ProviderModelConfig[] {
|
|
|
198
56
|
low: "low",
|
|
199
57
|
medium: "medium",
|
|
200
58
|
high: "high",
|
|
59
|
+
xhigh: "xhigh",
|
|
201
60
|
max: "max",
|
|
202
61
|
},
|
|
203
62
|
input: ["text", "image"],
|
|
204
|
-
|
|
205
|
-
cost: { input: 2, output: 10, cacheRead: 0.2, cacheWrite: 2.5 },
|
|
63
|
+
cost: { input: 6, output: 30, cacheRead: 0.6, cacheWrite: 7.5 },
|
|
206
64
|
contextWindow: 1000000,
|
|
207
65
|
maxTokens: 128000,
|
|
208
66
|
compat: ANTHROPIC_COMPAT,
|
|
@@ -1,46 +1,65 @@
|
|
|
1
1
|
import type { ProviderModelConfig } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import { OPENAI_COMPLETIONS_COMPAT } from "./_protocols.ts";
|
|
3
|
-
import { isMaximum, type PayloadWriter, type ThinkingApplier, type ThinkingLevel } from "../_tools.ts";
|
|
4
2
|
|
|
5
|
-
// DeepSeek
|
|
6
|
-
//
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
requiresReasoningContentOnAssistantMessages: true,
|
|
3
|
+
// DeepSeek Responses 将 developer role 当作 user,且不支持 OpenAI 的
|
|
4
|
+
// prompt_cache_retention;其余 reasoning 字段交给 Pi 的 Responses adapter。
|
|
5
|
+
const DEEPSEEK_RESPONSES_COMPAT = {
|
|
6
|
+
supportsDeveloperRole: false,
|
|
7
|
+
supportsLongCacheRetention: false,
|
|
11
8
|
};
|
|
12
9
|
|
|
13
10
|
/**
|
|
14
11
|
* DeepSeek(深度求索)V4 系列。
|
|
15
|
-
* 官方文档:https://api-docs.deepseek.com/
|
|
12
|
+
* 官方文档:https://api-docs.deepseek.com/guides/responses_api
|
|
16
13
|
*/
|
|
17
14
|
export function deepseekModels(root: string): ProviderModelConfig[] {
|
|
18
15
|
const v1 = `${root}/v1`;
|
|
19
16
|
return [
|
|
17
|
+
{
|
|
18
|
+
id: "deepseek-flash",
|
|
19
|
+
name: "DeepSeek V4.1 Flash",
|
|
20
|
+
api: "openai-responses",
|
|
21
|
+
baseUrl: v1,
|
|
22
|
+
reasoning: true,
|
|
23
|
+
input: ["text", "image"],
|
|
24
|
+
cost: { input: 0.1475, output: 0.295, cacheRead: 0.00295, cacheWrite: 0 },
|
|
25
|
+
contextWindow: 1000000,
|
|
26
|
+
maxTokens: 384000,
|
|
27
|
+
compat: DEEPSEEK_RESPONSES_COMPAT,
|
|
28
|
+
thinkingLevelMap: {
|
|
29
|
+
off: "none",
|
|
30
|
+
minimal: "low",
|
|
31
|
+
low: "low",
|
|
32
|
+
medium: "high",
|
|
33
|
+
high: "high",
|
|
34
|
+
xhigh: "high",
|
|
35
|
+
max: "max",
|
|
36
|
+
},
|
|
37
|
+
},
|
|
20
38
|
{
|
|
21
39
|
id: "deepseek-v4-flash",
|
|
22
|
-
name: "DeepSeek V4 Flash",
|
|
23
|
-
api: "openai-
|
|
40
|
+
name: "DeepSeek V4 Flash (Legacy Alias)",
|
|
41
|
+
api: "openai-responses",
|
|
24
42
|
baseUrl: v1,
|
|
25
43
|
reasoning: true,
|
|
26
|
-
input: ["text"],
|
|
44
|
+
input: ["text", "image"],
|
|
27
45
|
cost: { input: 0.1475, output: 0.295, cacheRead: 0.00295, cacheWrite: 0 },
|
|
28
46
|
contextWindow: 1000000,
|
|
29
47
|
maxTokens: 384000,
|
|
30
|
-
compat:
|
|
48
|
+
compat: DEEPSEEK_RESPONSES_COMPAT,
|
|
31
49
|
thinkingLevelMap: {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
50
|
+
off: "none",
|
|
51
|
+
minimal: "low",
|
|
52
|
+
low: "low",
|
|
53
|
+
medium: "high",
|
|
35
54
|
high: "high",
|
|
36
|
-
xhigh: "
|
|
55
|
+
xhigh: "high",
|
|
37
56
|
max: "max",
|
|
38
57
|
},
|
|
39
58
|
},
|
|
40
59
|
{
|
|
41
60
|
id: "deepseek-v4-pro",
|
|
42
61
|
name: "DeepSeek V4 Pro",
|
|
43
|
-
api: "openai-
|
|
62
|
+
api: "openai-responses",
|
|
44
63
|
baseUrl: v1,
|
|
45
64
|
reasoning: true,
|
|
46
65
|
input: ["text"],
|
|
@@ -52,31 +71,16 @@ export function deepseekModels(root: string): ProviderModelConfig[] {
|
|
|
52
71
|
},
|
|
53
72
|
contextWindow: 1000000,
|
|
54
73
|
maxTokens: 384000,
|
|
55
|
-
compat:
|
|
74
|
+
compat: DEEPSEEK_RESPONSES_COMPAT,
|
|
56
75
|
thinkingLevelMap: {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
76
|
+
off: "none",
|
|
77
|
+
minimal: "low",
|
|
78
|
+
low: "low",
|
|
79
|
+
medium: "high",
|
|
60
80
|
high: "high",
|
|
61
|
-
xhigh: "
|
|
81
|
+
xhigh: "high",
|
|
62
82
|
max: "max",
|
|
63
83
|
},
|
|
64
84
|
},
|
|
65
85
|
];
|
|
66
86
|
}
|
|
67
|
-
|
|
68
|
-
// DeepSeek 思维链:`thinking.type` + `reasoning_effort`(high/max 两档)。
|
|
69
|
-
function applyDeepSeek(writer: PayloadWriter, level: ThinkingLevel): void {
|
|
70
|
-
writer.remove("reasoning_effort");
|
|
71
|
-
if (level === "off") {
|
|
72
|
-
writer.setThinking({ type: "disabled" });
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
writer.setThinking({ type: "enabled" });
|
|
76
|
-
writer.set("reasoning_effort", isMaximum(level) ? "max" : "high");
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export const deepseekThinking: Record<string, ThinkingApplier> = {
|
|
80
|
-
"deepseek-v4-flash": (w, c) => applyDeepSeek(w, c.thinkingLevel),
|
|
81
|
-
"deepseek-v4-pro": (w, c) => applyDeepSeek(w, c.thinkingLevel),
|
|
82
|
-
};
|
|
@@ -9,8 +9,8 @@ export function openaiModels(root: string): ProviderModelConfig[] {
|
|
|
9
9
|
const v1 = `${root}/v1`;
|
|
10
10
|
return [
|
|
11
11
|
{
|
|
12
|
-
id: "gpt-
|
|
13
|
-
name: "GPT
|
|
12
|
+
id: "gpt-6-astra",
|
|
13
|
+
name: "GPT-6 Astra",
|
|
14
14
|
api: "openai-responses",
|
|
15
15
|
baseUrl: v1,
|
|
16
16
|
reasoning: true,
|
|
@@ -22,13 +22,13 @@ export function openaiModels(root: string): ProviderModelConfig[] {
|
|
|
22
22
|
max: "max",
|
|
23
23
|
},
|
|
24
24
|
input: ["text", "image"],
|
|
25
|
-
cost: { input:
|
|
26
|
-
contextWindow:
|
|
25
|
+
cost: { input: 10, output: 50, cacheRead: 1, cacheWrite: 12.5 },
|
|
26
|
+
contextWindow: 1050000,
|
|
27
27
|
maxTokens: 128000,
|
|
28
28
|
},
|
|
29
29
|
{
|
|
30
|
-
id: "gpt-5.6-
|
|
31
|
-
name: "GPT 5.6
|
|
30
|
+
id: "gpt-5.6-sol",
|
|
31
|
+
name: "GPT 5.6 Sol",
|
|
32
32
|
api: "openai-responses",
|
|
33
33
|
baseUrl: v1,
|
|
34
34
|
reasoning: true,
|
|
@@ -40,13 +40,13 @@ export function openaiModels(root: string): ProviderModelConfig[] {
|
|
|
40
40
|
max: "max",
|
|
41
41
|
},
|
|
42
42
|
input: ["text", "image"],
|
|
43
|
-
cost: { input:
|
|
43
|
+
cost: { input: 5, output: 30, cacheRead: 0.5, cacheWrite: 6.25 },
|
|
44
44
|
contextWindow: 372000,
|
|
45
45
|
maxTokens: 128000,
|
|
46
46
|
},
|
|
47
47
|
{
|
|
48
|
-
id: "gpt-5.6-
|
|
49
|
-
name: "GPT 5.6
|
|
48
|
+
id: "gpt-5.6-terra",
|
|
49
|
+
name: "GPT 5.6 Terra",
|
|
50
50
|
api: "openai-responses",
|
|
51
51
|
baseUrl: v1,
|
|
52
52
|
reasoning: true,
|
|
@@ -58,138 +58,13 @@ export function openaiModels(root: string): ProviderModelConfig[] {
|
|
|
58
58
|
max: "max",
|
|
59
59
|
},
|
|
60
60
|
input: ["text", "image"],
|
|
61
|
-
cost: { input:
|
|
61
|
+
cost: { input: 2.5, output: 15, cacheRead: 0.25, cacheWrite: 3.125 },
|
|
62
62
|
contextWindow: 372000,
|
|
63
63
|
maxTokens: 128000,
|
|
64
64
|
},
|
|
65
65
|
{
|
|
66
|
-
id: "gpt-5.
|
|
67
|
-
name: "GPT
|
|
68
|
-
api: "openai-responses",
|
|
69
|
-
baseUrl: v1,
|
|
70
|
-
reasoning: true,
|
|
71
|
-
thinkingLevelMap: {
|
|
72
|
-
low: "low",
|
|
73
|
-
medium: "medium",
|
|
74
|
-
high: "high",
|
|
75
|
-
xhigh: "xhigh",
|
|
76
|
-
},
|
|
77
|
-
input: ["text", "image"],
|
|
78
|
-
cost: { input: 5, output: 30, cacheRead: 0.5, cacheWrite: 0 },
|
|
79
|
-
contextWindow: 272000,
|
|
80
|
-
maxTokens: 128000,
|
|
81
|
-
},
|
|
82
|
-
{
|
|
83
|
-
id: "gpt-5.3-codex-spark",
|
|
84
|
-
name: "GPT-5.3 Codex Spark",
|
|
85
|
-
api: "openai-responses",
|
|
86
|
-
baseUrl: v1,
|
|
87
|
-
reasoning: true,
|
|
88
|
-
thinkingLevelMap: {
|
|
89
|
-
low: "low",
|
|
90
|
-
medium: "medium",
|
|
91
|
-
high: "high",
|
|
92
|
-
xhigh: "xhigh",
|
|
93
|
-
},
|
|
94
|
-
input: ["text"],
|
|
95
|
-
cost: { input: 1.75, output: 14, cacheRead: 0.175, cacheWrite: 0 },
|
|
96
|
-
contextWindow: 128000,
|
|
97
|
-
maxTokens: 32000,
|
|
98
|
-
},
|
|
99
|
-
{
|
|
100
|
-
id: "gpt-5.6",
|
|
101
|
-
name: "GPT-5.6",
|
|
102
|
-
api: "openai-responses",
|
|
103
|
-
baseUrl: v1,
|
|
104
|
-
reasoning: true,
|
|
105
|
-
thinkingLevelMap: {
|
|
106
|
-
low: "low",
|
|
107
|
-
medium: "medium",
|
|
108
|
-
high: "high",
|
|
109
|
-
xhigh: "xhigh",
|
|
110
|
-
max: "max",
|
|
111
|
-
},
|
|
112
|
-
input: ["text", "image"],
|
|
113
|
-
cost: { input: 5, output: 30, cacheRead: 0.5, cacheWrite: 6.25 },
|
|
114
|
-
contextWindow: 1050000,
|
|
115
|
-
maxTokens: 128000,
|
|
116
|
-
},
|
|
117
|
-
{
|
|
118
|
-
id: "gpt-5.4",
|
|
119
|
-
name: "GPT-5.4",
|
|
120
|
-
api: "openai-responses",
|
|
121
|
-
baseUrl: v1,
|
|
122
|
-
reasoning: true,
|
|
123
|
-
thinkingLevelMap: {
|
|
124
|
-
low: "low",
|
|
125
|
-
medium: "medium",
|
|
126
|
-
high: "high",
|
|
127
|
-
xhigh: "xhigh",
|
|
128
|
-
max: "max",
|
|
129
|
-
},
|
|
130
|
-
input: ["text", "image"],
|
|
131
|
-
cost: { input: 2.5, output: 15, cacheRead: 0.25, cacheWrite: 0 },
|
|
132
|
-
contextWindow: 1050000,
|
|
133
|
-
maxTokens: 128000,
|
|
134
|
-
},
|
|
135
|
-
{
|
|
136
|
-
id: "gpt-5.4-mini",
|
|
137
|
-
name: "GPT-5.4 mini",
|
|
138
|
-
api: "openai-responses",
|
|
139
|
-
baseUrl: v1,
|
|
140
|
-
reasoning: true,
|
|
141
|
-
thinkingLevelMap: {
|
|
142
|
-
low: "low",
|
|
143
|
-
medium: "medium",
|
|
144
|
-
high: "high",
|
|
145
|
-
xhigh: "xhigh",
|
|
146
|
-
max: "max",
|
|
147
|
-
},
|
|
148
|
-
input: ["text", "image"],
|
|
149
|
-
cost: { input: 0.75, output: 4.5, cacheRead: 0.075, cacheWrite: 0 },
|
|
150
|
-
contextWindow: 400000,
|
|
151
|
-
maxTokens: 128000,
|
|
152
|
-
},
|
|
153
|
-
{
|
|
154
|
-
id: "gpt-5.2",
|
|
155
|
-
name: "GPT-5.2",
|
|
156
|
-
api: "openai-responses",
|
|
157
|
-
baseUrl: v1,
|
|
158
|
-
reasoning: true,
|
|
159
|
-
thinkingLevelMap: {
|
|
160
|
-
low: "low",
|
|
161
|
-
medium: "medium",
|
|
162
|
-
high: "high",
|
|
163
|
-
xhigh: "xhigh",
|
|
164
|
-
max: "max",
|
|
165
|
-
},
|
|
166
|
-
input: ["text", "image"],
|
|
167
|
-
cost: { input: 1.75, output: 14, cacheRead: 0.175, cacheWrite: 0 },
|
|
168
|
-
contextWindow: 400000,
|
|
169
|
-
maxTokens: 128000,
|
|
170
|
-
},
|
|
171
|
-
{
|
|
172
|
-
id: "gpt-5.2-pro",
|
|
173
|
-
name: "GPT-5.2 Pro",
|
|
174
|
-
api: "openai-responses",
|
|
175
|
-
baseUrl: v1,
|
|
176
|
-
reasoning: true,
|
|
177
|
-
thinkingLevelMap: {
|
|
178
|
-
low: "low",
|
|
179
|
-
medium: "medium",
|
|
180
|
-
high: "high",
|
|
181
|
-
xhigh: "xhigh",
|
|
182
|
-
max: "max",
|
|
183
|
-
},
|
|
184
|
-
input: ["text", "image"],
|
|
185
|
-
// 官方未公布缓存价,置 0。
|
|
186
|
-
cost: { input: 21, output: 168, cacheRead: 0, cacheWrite: 0 },
|
|
187
|
-
contextWindow: 400000,
|
|
188
|
-
maxTokens: 128000,
|
|
189
|
-
},
|
|
190
|
-
{
|
|
191
|
-
id: "gpt-5.1",
|
|
192
|
-
name: "GPT-5.1",
|
|
66
|
+
id: "gpt-5.6-luna",
|
|
67
|
+
name: "GPT 5.6 Luna",
|
|
193
68
|
api: "openai-responses",
|
|
194
69
|
baseUrl: v1,
|
|
195
70
|
reasoning: true,
|
|
@@ -201,13 +76,13 @@ export function openaiModels(root: string): ProviderModelConfig[] {
|
|
|
201
76
|
max: "max",
|
|
202
77
|
},
|
|
203
78
|
input: ["text", "image"],
|
|
204
|
-
cost: { input: 1
|
|
205
|
-
contextWindow:
|
|
79
|
+
cost: { input: 1, output: 6, cacheRead: 0.01, cacheWrite: 1.25 },
|
|
80
|
+
contextWindow: 372000,
|
|
206
81
|
maxTokens: 128000,
|
|
207
82
|
},
|
|
208
83
|
{
|
|
209
|
-
id: "gpt-5",
|
|
210
|
-
name: "GPT-5",
|
|
84
|
+
id: "gpt-5.5",
|
|
85
|
+
name: "GPT-5.5",
|
|
211
86
|
api: "openai-responses",
|
|
212
87
|
baseUrl: v1,
|
|
213
88
|
reasoning: true,
|
|
@@ -216,11 +91,10 @@ export function openaiModels(root: string): ProviderModelConfig[] {
|
|
|
216
91
|
medium: "medium",
|
|
217
92
|
high: "high",
|
|
218
93
|
xhigh: "xhigh",
|
|
219
|
-
max: "max",
|
|
220
94
|
},
|
|
221
95
|
input: ["text", "image"],
|
|
222
|
-
cost: { input:
|
|
223
|
-
contextWindow:
|
|
96
|
+
cost: { input: 5, output: 30, cacheRead: 0.5, cacheWrite: 0 },
|
|
97
|
+
contextWindow: 272000,
|
|
224
98
|
maxTokens: 128000,
|
|
225
99
|
},
|
|
226
100
|
];
|
|
@@ -234,17 +108,10 @@ function applyOpenAIResponses(writer: PayloadWriter, modelId: string): void {
|
|
|
234
108
|
writer.setTextVerbosity(verbosity);
|
|
235
109
|
}
|
|
236
110
|
|
|
237
|
-
export const openaiThinking
|
|
111
|
+
export const openaiThinking = {
|
|
112
|
+
"gpt-6-astra": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
238
113
|
"gpt-5.6-sol": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
239
114
|
"gpt-5.6-terra": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
240
115
|
"gpt-5.6-luna": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
241
116
|
"gpt-5.5": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
242
|
-
|
|
243
|
-
"gpt-5.6": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
244
|
-
"gpt-5.4": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
245
|
-
"gpt-5.4-mini": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
246
|
-
"gpt-5.2": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
247
|
-
"gpt-5.2-pro": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
248
|
-
"gpt-5.1": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
249
|
-
"gpt-5": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
250
|
-
};
|
|
117
|
+
} satisfies Record<string, ThinkingApplier>;
|
|
@@ -12,13 +12,19 @@
|
|
|
12
12
|
* 独立的 ts_search 工具(所有模型可用)单独实现,与内置查询注入分离。
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
isPlainObject,
|
|
17
|
+
type Payload,
|
|
18
|
+
PayloadWriter,
|
|
19
|
+
type WebSearchMode,
|
|
20
|
+
} from "./_tools.ts";
|
|
16
21
|
|
|
17
22
|
const OPENAI_RESPONSES = "openai-responses";
|
|
18
23
|
const OPENAI_COMPLETIONS = "openai-completions";
|
|
19
24
|
|
|
20
25
|
// 支持内置查询的模型名单(GPT Responses 系列)。
|
|
21
26
|
const BUILTIN_SEARCH_MODELS = new Set([
|
|
27
|
+
"gpt-6-astra",
|
|
22
28
|
"gpt-5.6-sol",
|
|
23
29
|
"gpt-5.6-terra",
|
|
24
30
|
"gpt-5.6-luna",
|
|
@@ -42,7 +48,7 @@ function isExistingWebSearchTool(tools: readonly unknown[]): boolean {
|
|
|
42
48
|
});
|
|
43
49
|
}
|
|
44
50
|
|
|
45
|
-
function hasGrokSearchFields(payload:
|
|
51
|
+
function hasGrokSearchFields(payload: Payload): boolean {
|
|
46
52
|
return (
|
|
47
53
|
Object.hasOwn(payload, "search_parameters") ||
|
|
48
54
|
Object.hasOwn(payload, "web_search_options")
|
|
@@ -76,12 +82,12 @@ function applyGrokSearch(writer: PayloadWriter): void {
|
|
|
76
82
|
* preserved. Pi drops requested sources, so GPT injection deliberately does
|
|
77
83
|
* not request them.
|
|
78
84
|
*/
|
|
79
|
-
export function applyBuiltinSearchTool(
|
|
80
|
-
payload:
|
|
85
|
+
export function applyBuiltinSearchTool<T>(
|
|
86
|
+
payload: T,
|
|
81
87
|
modelId: string,
|
|
82
88
|
api: string,
|
|
83
89
|
mode: WebSearchMode,
|
|
84
|
-
):
|
|
90
|
+
): T | Payload {
|
|
85
91
|
if (!isPlainObject(payload) || mode === "off") return payload;
|
|
86
92
|
const writer = new PayloadWriter(payload);
|
|
87
93
|
|
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* 独立搜索工具(ts_search
|
|
3
|
-
*
|
|
4
|
-
* 与模型模块的内置查询注入(models/web-search.ts)分离:
|
|
5
|
-
* - 内置查询注入:GPT/Grok 模型请求时自动附加搜索能力
|
|
6
|
-
* - 本工具:任何模型主动调用,内部通过固定的 GPT / Grok 后端联网搜索
|
|
7
|
-
*
|
|
8
|
-
* 工具只接收查询;搜索后端由扩展维护,避免调用方猜测或误选模型。
|
|
2
|
+
* 独立搜索工具(ts_search):任何模型均可通过配置或内置的 GPT / Grok
|
|
3
|
+
* 后端执行联网搜索。模型模块自身的内置查询注入在 models/web-search.ts。
|
|
9
4
|
*/
|
|
10
5
|
|
|
11
6
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
@@ -13,22 +8,24 @@ import { Type } from "typebox";
|
|
|
13
8
|
|
|
14
9
|
export interface TsSearchOptions {
|
|
15
10
|
baseUrl: string;
|
|
16
|
-
/** 凭据 provider(默认 tsgw,走 Pi 凭据机制)。 */
|
|
17
11
|
apiKeyProvider?: string;
|
|
18
|
-
/** @deprecated 固定白名单已内置;仅兼容旧 index.ts,值会被忽略。 */
|
|
19
12
|
searchModels?: readonly string[];
|
|
20
13
|
}
|
|
21
14
|
|
|
22
|
-
export const DEFAULT_SEARCH_MODELS = [
|
|
15
|
+
export const DEFAULT_SEARCH_MODELS = [
|
|
16
|
+
"gpt-5.6-luna",
|
|
17
|
+
"grok-chat-fast",
|
|
18
|
+
] as const;
|
|
23
19
|
const CHAT_COMPLETIONS_PATH = "v1/chat/completions";
|
|
24
|
-
|
|
20
|
+
const MAX_ATTEMPTS = 3;
|
|
21
|
+
const REQUEST_TIMEOUT_MS = 45_000;
|
|
22
|
+
const RETRY_DELAY_MS = 1_000;
|
|
25
23
|
const SEARCH_PROMPT = [
|
|
26
24
|
"Answer the user's question using the latest available information from the web.",
|
|
27
25
|
"Be concise.",
|
|
28
26
|
"If sources are available, include only the most relevant source URLs needed to support the answer; avoid link dumping.",
|
|
29
27
|
"If no live web information is available, reply exactly: NO_LIVE_WEB.",
|
|
30
28
|
].join(" ");
|
|
31
|
-
|
|
32
29
|
type SearchFamily = "gpt" | "grok";
|
|
33
30
|
|
|
34
31
|
export function detectSearchFamily(
|
|
@@ -40,8 +37,28 @@ export function detectSearchFamily(
|
|
|
40
37
|
return "unsupported";
|
|
41
38
|
}
|
|
42
39
|
|
|
43
|
-
export function resolveSearchModels(
|
|
44
|
-
|
|
40
|
+
export function resolveSearchModels(
|
|
41
|
+
options: Pick<TsSearchOptions, "searchModels"> = {},
|
|
42
|
+
): string[] {
|
|
43
|
+
if (!options.searchModels) return [...DEFAULT_SEARCH_MODELS];
|
|
44
|
+
|
|
45
|
+
const models: string[] = [];
|
|
46
|
+
const seen = new Set<string>();
|
|
47
|
+
for (const value of options.searchModels as readonly unknown[]) {
|
|
48
|
+
const model = typeof value === "string" ? value.trim() : "";
|
|
49
|
+
if (!model || detectSearchFamily(model) === "unsupported") {
|
|
50
|
+
console.warn(
|
|
51
|
+
`TSGW: ignoring invalid ts_search model ${JSON.stringify(value)}; expected gpt-* or grok-*`,
|
|
52
|
+
);
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (!seen.has(model)) {
|
|
56
|
+
seen.add(model);
|
|
57
|
+
models.push(model);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return models.length > 0 ? models : [...DEFAULT_SEARCH_MODELS];
|
|
45
62
|
}
|
|
46
63
|
|
|
47
64
|
export function buildSearchUrl(baseUrl: string): string {
|
|
@@ -146,7 +163,7 @@ export function buildSearchPayload(
|
|
|
146
163
|
return { model, messages, search_parameters: { mode: "on" } };
|
|
147
164
|
}
|
|
148
165
|
|
|
149
|
-
interface RouteResult {
|
|
166
|
+
export interface RouteResult {
|
|
150
167
|
ok: boolean;
|
|
151
168
|
model: string;
|
|
152
169
|
family: SearchFamily;
|
|
@@ -156,78 +173,195 @@ interface RouteResult {
|
|
|
156
173
|
error: string;
|
|
157
174
|
}
|
|
158
175
|
|
|
159
|
-
|
|
176
|
+
export interface SearchRetryOptions {
|
|
177
|
+
delayMs?: number;
|
|
178
|
+
requestTimeoutMs?: number;
|
|
179
|
+
fetch?: typeof fetch;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
interface SearchAttemptResult {
|
|
183
|
+
result: RouteResult;
|
|
184
|
+
retryable: boolean;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function failedAttempt(
|
|
188
|
+
model: string,
|
|
189
|
+
family: SearchFamily,
|
|
190
|
+
error: string,
|
|
191
|
+
retryable: boolean,
|
|
192
|
+
requestId = "",
|
|
193
|
+
): SearchAttemptResult {
|
|
194
|
+
return {
|
|
195
|
+
result: { ...buildFailure(model, family, error), requestId },
|
|
196
|
+
retryable,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function requestSignal(
|
|
201
|
+
signal: AbortSignal | undefined,
|
|
202
|
+
timeoutMs: number,
|
|
203
|
+
): AbortSignal {
|
|
204
|
+
const timeout = AbortSignal.timeout(timeoutMs);
|
|
205
|
+
return signal ? AbortSignal.any([signal, timeout]) : timeout;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function waitForRetry(
|
|
209
|
+
delayMs: number,
|
|
210
|
+
signal: AbortSignal | undefined,
|
|
211
|
+
): Promise<void> {
|
|
212
|
+
if (delayMs <= 0) return Promise.resolve();
|
|
213
|
+
if (signal?.aborted) return Promise.reject(signal.reason);
|
|
214
|
+
|
|
215
|
+
return new Promise((resolve, reject) => {
|
|
216
|
+
const onAbort = (): void => {
|
|
217
|
+
clearTimeout(timer);
|
|
218
|
+
reject(signal?.reason);
|
|
219
|
+
};
|
|
220
|
+
const timer = setTimeout(() => {
|
|
221
|
+
signal?.removeEventListener("abort", onAbort);
|
|
222
|
+
resolve();
|
|
223
|
+
}, delayMs);
|
|
224
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async function executeSearchAttempt(
|
|
160
229
|
model: string,
|
|
161
230
|
query: string,
|
|
162
231
|
apiKey: string,
|
|
163
232
|
baseUrl: string,
|
|
164
233
|
signal: AbortSignal | undefined,
|
|
165
|
-
|
|
234
|
+
fetchImpl: typeof fetch,
|
|
235
|
+
requestTimeoutMs: number,
|
|
236
|
+
): Promise<SearchAttemptResult> {
|
|
166
237
|
const family = detectSearchFamily(model);
|
|
167
|
-
if (family === "unsupported")
|
|
168
|
-
return
|
|
169
|
-
|
|
238
|
+
if (family === "unsupported")
|
|
239
|
+
return failedAttempt(model, "gpt", `不支持的模型系列:${model}`, false);
|
|
240
|
+
if (signal?.aborted)
|
|
241
|
+
return failedAttempt(model, family, "请求已取消。", false);
|
|
170
242
|
|
|
171
243
|
let response: Response;
|
|
172
244
|
try {
|
|
173
|
-
response = await
|
|
245
|
+
response = await fetchImpl(buildSearchUrl(baseUrl), {
|
|
174
246
|
method: "POST",
|
|
175
247
|
headers: {
|
|
176
248
|
authorization: `Bearer ${apiKey}`,
|
|
177
249
|
"content-type": "application/json",
|
|
178
250
|
},
|
|
179
251
|
body: JSON.stringify(buildSearchPayload(model, family, query)),
|
|
180
|
-
signal,
|
|
252
|
+
signal: requestSignal(signal, requestTimeoutMs),
|
|
181
253
|
});
|
|
182
254
|
} catch (error) {
|
|
183
|
-
return
|
|
255
|
+
return failedAttempt(
|
|
184
256
|
model,
|
|
185
257
|
family,
|
|
186
|
-
|
|
258
|
+
signal?.aborted
|
|
259
|
+
? "请求已取消。"
|
|
260
|
+
: `请求失败:${error instanceof Error ? error.message : String(error)}`,
|
|
261
|
+
!signal?.aborted,
|
|
187
262
|
);
|
|
188
263
|
}
|
|
189
264
|
|
|
190
265
|
const requestId = response.headers.get("ah-request-id") ?? "";
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
266
|
+
let bodyText: string;
|
|
267
|
+
try {
|
|
268
|
+
bodyText = await response.text();
|
|
269
|
+
} catch (error) {
|
|
270
|
+
return failedAttempt(
|
|
271
|
+
model,
|
|
272
|
+
family,
|
|
273
|
+
signal?.aborted
|
|
274
|
+
? "请求已取消。"
|
|
275
|
+
: `读取网关响应失败:${error instanceof Error ? error.message : String(error)}`,
|
|
276
|
+
!signal?.aborted,
|
|
199
277
|
requestId,
|
|
200
|
-
|
|
278
|
+
);
|
|
201
279
|
}
|
|
280
|
+
if (!response.ok)
|
|
281
|
+
return failedAttempt(
|
|
282
|
+
model,
|
|
283
|
+
family,
|
|
284
|
+
`网关请求失败:HTTP ${response.status} ${response.statusText}\n${bodyText}`,
|
|
285
|
+
response.status === 429 || response.status >= 500,
|
|
286
|
+
requestId,
|
|
287
|
+
);
|
|
202
288
|
|
|
203
289
|
try {
|
|
204
290
|
const parsed: unknown = JSON.parse(bodyText);
|
|
205
291
|
if (parsed && typeof parsed === "object") {
|
|
206
292
|
const record = parsed as Record<string, unknown>;
|
|
207
293
|
return {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
294
|
+
result: {
|
|
295
|
+
ok: true,
|
|
296
|
+
model,
|
|
297
|
+
family,
|
|
298
|
+
answer: extractAnswer(record) || JSON.stringify(record),
|
|
299
|
+
urls: Array.from(collectUrls(record)),
|
|
300
|
+
requestId,
|
|
301
|
+
error: "",
|
|
302
|
+
},
|
|
303
|
+
retryable: false,
|
|
215
304
|
};
|
|
216
305
|
}
|
|
217
306
|
} catch {
|
|
218
307
|
// 非 JSON 响应按纯文本处理。
|
|
219
308
|
}
|
|
220
309
|
return {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
310
|
+
result: {
|
|
311
|
+
ok: true,
|
|
312
|
+
model,
|
|
313
|
+
family,
|
|
314
|
+
answer: bodyText || "网关返回空响应。",
|
|
315
|
+
urls: Array.from(collectUrls(bodyText)),
|
|
316
|
+
requestId,
|
|
317
|
+
error: "",
|
|
318
|
+
},
|
|
319
|
+
retryable: false,
|
|
228
320
|
};
|
|
229
321
|
}
|
|
230
322
|
|
|
323
|
+
export async function retrySearchRoute(
|
|
324
|
+
model: string,
|
|
325
|
+
query: string,
|
|
326
|
+
apiKey: string,
|
|
327
|
+
baseUrl: string,
|
|
328
|
+
signal: AbortSignal | undefined,
|
|
329
|
+
options: SearchRetryOptions = {},
|
|
330
|
+
): Promise<RouteResult> {
|
|
331
|
+
const delayMs = options.delayMs ?? RETRY_DELAY_MS;
|
|
332
|
+
const requestTimeoutMs = options.requestTimeoutMs ?? REQUEST_TIMEOUT_MS;
|
|
333
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
334
|
+
let lastResult = buildFailure(model, "gpt", "搜索请求未执行。");
|
|
335
|
+
|
|
336
|
+
for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt += 1) {
|
|
337
|
+
const current = await executeSearchAttempt(
|
|
338
|
+
model,
|
|
339
|
+
query,
|
|
340
|
+
apiKey,
|
|
341
|
+
baseUrl,
|
|
342
|
+
signal,
|
|
343
|
+
fetchImpl,
|
|
344
|
+
requestTimeoutMs,
|
|
345
|
+
);
|
|
346
|
+
lastResult = current.result;
|
|
347
|
+
if (current.result.ok || !current.retryable || attempt === MAX_ATTEMPTS - 1)
|
|
348
|
+
return current.result;
|
|
349
|
+
|
|
350
|
+
try {
|
|
351
|
+
await waitForRetry(delayMs * 2 ** attempt, signal);
|
|
352
|
+
} catch {
|
|
353
|
+
const family = detectSearchFamily(model);
|
|
354
|
+
return buildFailure(
|
|
355
|
+
model,
|
|
356
|
+
family === "grok" ? "grok" : "gpt",
|
|
357
|
+
"请求已取消。",
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
return lastResult;
|
|
363
|
+
}
|
|
364
|
+
|
|
231
365
|
function buildFailure(
|
|
232
366
|
model: string,
|
|
233
367
|
family: SearchFamily,
|
|
@@ -288,11 +422,11 @@ function renderMergedResult(results: RouteResult[]): string {
|
|
|
288
422
|
return lines.join("\n");
|
|
289
423
|
}
|
|
290
424
|
|
|
291
|
-
/** 注册 ts_search 工具,固定并行请求白名单内的 GPT + Grok 后端。 */
|
|
292
425
|
export function registerTsSearch(
|
|
293
426
|
pi: ExtensionAPI,
|
|
294
427
|
options: TsSearchOptions,
|
|
295
428
|
): void {
|
|
429
|
+
const models = resolveSearchModels(options);
|
|
296
430
|
const argsSchema = Type.Object({
|
|
297
431
|
query: Type.String({ minLength: 1, description: "搜索查询" }),
|
|
298
432
|
});
|
|
@@ -340,10 +474,9 @@ export function registerTsSearch(
|
|
|
340
474
|
};
|
|
341
475
|
}
|
|
342
476
|
|
|
343
|
-
const models = resolveSearchModels();
|
|
344
477
|
const settled = await Promise.allSettled(
|
|
345
478
|
models.map((model) =>
|
|
346
|
-
|
|
479
|
+
retrySearchRoute(model, query, apiKey, options.baseUrl, signal),
|
|
347
480
|
),
|
|
348
481
|
);
|
|
349
482
|
const results = settled.map((item, index) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-tsgw",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
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.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [
|