pi-tsgw 0.0.1
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/LICENSE +21 -0
- package/README.md +100 -0
- package/extensions/index.test.ts +509 -0
- package/extensions/index.ts +185 -0
- package/extensions/models/_tools.ts +139 -0
- package/extensions/models/catalog.test.ts +137 -0
- package/extensions/models/catalog.ts +100 -0
- package/extensions/models/operations.test.ts +428 -0
- package/extensions/models/operations.ts +54 -0
- package/extensions/models/vendors/_protocols.ts +24 -0
- package/extensions/models/vendors/anthropic.ts +211 -0
- package/extensions/models/vendors/deepseek.ts +82 -0
- package/extensions/models/vendors/gemini.ts +148 -0
- package/extensions/models/vendors/glm.ts +157 -0
- package/extensions/models/vendors/kimi.ts +124 -0
- package/extensions/models/vendors/longcat.ts +30 -0
- package/extensions/models/vendors/mimo.ts +47 -0
- package/extensions/models/vendors/minimax.ts +84 -0
- package/extensions/models/vendors/openai.ts +249 -0
- package/extensions/models/vendors/qwen.ts +102 -0
- package/extensions/web-search/web-search.test.ts +111 -0
- package/extensions/web-search/web-search.ts +73 -0
- package/package.json +36 -0
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import type { ProviderModelConfig } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { type PayloadWriter, type ThinkingApplier } from "../_tools.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* OpenAI GPT 系列(Responses 协议)。
|
|
6
|
+
* 官方文档:https://platform.openai.com/docs/models
|
|
7
|
+
*/
|
|
8
|
+
export function openaiModels(root: string): ProviderModelConfig[] {
|
|
9
|
+
const v1 = `${root}/v1`;
|
|
10
|
+
return [
|
|
11
|
+
{
|
|
12
|
+
id: "gpt-5.6-sol",
|
|
13
|
+
name: "GPT 5.6 Sol",
|
|
14
|
+
api: "openai-responses",
|
|
15
|
+
baseUrl: v1,
|
|
16
|
+
reasoning: true,
|
|
17
|
+
thinkingLevelMap: {
|
|
18
|
+
low: "low",
|
|
19
|
+
medium: "medium",
|
|
20
|
+
high: "high",
|
|
21
|
+
xhigh: "xhigh",
|
|
22
|
+
max: "max",
|
|
23
|
+
},
|
|
24
|
+
input: ["text", "image"],
|
|
25
|
+
cost: { input: 5, output: 30, cacheRead: 0.5, cacheWrite: 6.25 },
|
|
26
|
+
contextWindow: 372000,
|
|
27
|
+
maxTokens: 128000,
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
id: "gpt-5.6-terra",
|
|
31
|
+
name: "GPT 5.6 Terra",
|
|
32
|
+
api: "openai-responses",
|
|
33
|
+
baseUrl: v1,
|
|
34
|
+
reasoning: true,
|
|
35
|
+
thinkingLevelMap: {
|
|
36
|
+
low: "low",
|
|
37
|
+
medium: "medium",
|
|
38
|
+
high: "high",
|
|
39
|
+
xhigh: "xhigh",
|
|
40
|
+
max: "max",
|
|
41
|
+
},
|
|
42
|
+
input: ["text", "image"],
|
|
43
|
+
cost: { input: 2.5, output: 15, cacheRead: 0.25, cacheWrite: 3.125 },
|
|
44
|
+
contextWindow: 372000,
|
|
45
|
+
maxTokens: 128000,
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
id: "gpt-5.6-luna",
|
|
49
|
+
name: "GPT 5.6 Luna",
|
|
50
|
+
api: "openai-responses",
|
|
51
|
+
baseUrl: v1,
|
|
52
|
+
reasoning: true,
|
|
53
|
+
thinkingLevelMap: {
|
|
54
|
+
low: "low",
|
|
55
|
+
medium: "medium",
|
|
56
|
+
high: "high",
|
|
57
|
+
xhigh: "xhigh",
|
|
58
|
+
max: "max",
|
|
59
|
+
},
|
|
60
|
+
input: ["text", "image"],
|
|
61
|
+
cost: { input: 1, output: 6, cacheRead: 0.01, cacheWrite: 1.25 },
|
|
62
|
+
contextWindow: 372000,
|
|
63
|
+
maxTokens: 128000,
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
id: "gpt-5.5",
|
|
67
|
+
name: "GPT-5.5",
|
|
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",
|
|
193
|
+
api: "openai-responses",
|
|
194
|
+
baseUrl: v1,
|
|
195
|
+
reasoning: true,
|
|
196
|
+
thinkingLevelMap: {
|
|
197
|
+
low: "low",
|
|
198
|
+
medium: "medium",
|
|
199
|
+
high: "high",
|
|
200
|
+
xhigh: "xhigh",
|
|
201
|
+
max: "max",
|
|
202
|
+
},
|
|
203
|
+
input: ["text", "image"],
|
|
204
|
+
cost: { input: 1.25, output: 10, cacheRead: 0.125, cacheWrite: 0 },
|
|
205
|
+
contextWindow: 400000,
|
|
206
|
+
maxTokens: 128000,
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
id: "gpt-5",
|
|
210
|
+
name: "GPT-5",
|
|
211
|
+
api: "openai-responses",
|
|
212
|
+
baseUrl: v1,
|
|
213
|
+
reasoning: true,
|
|
214
|
+
thinkingLevelMap: {
|
|
215
|
+
low: "low",
|
|
216
|
+
medium: "medium",
|
|
217
|
+
high: "high",
|
|
218
|
+
xhigh: "xhigh",
|
|
219
|
+
max: "max",
|
|
220
|
+
},
|
|
221
|
+
input: ["text", "image"],
|
|
222
|
+
cost: { input: 1.25, output: 10, cacheRead: 0.125, cacheWrite: 0 },
|
|
223
|
+
contextWindow: 400000,
|
|
224
|
+
maxTokens: 128000,
|
|
225
|
+
},
|
|
226
|
+
];
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// GPT Responses 思维链:保留 Pi 的 `reasoning`,固定 `service_tier=flex`,按别名调文本 verbosity。
|
|
230
|
+
function applyOpenAIResponses(writer: PayloadWriter, modelId: string): void {
|
|
231
|
+
const verbosity = modelId === "gpt-5.6-terra" ? "medium" : "low";
|
|
232
|
+
writer.set("service_tier", "flex");
|
|
233
|
+
writer.setTextVerbosity(verbosity);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export const openaiThinking: Record<string, ThinkingApplier> = {
|
|
237
|
+
"gpt-5.6-sol": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
238
|
+
"gpt-5.6-terra": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
239
|
+
"gpt-5.6-luna": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
240
|
+
"gpt-5.5": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
241
|
+
"gpt-5.3-codex-spark": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
242
|
+
"gpt-5.6": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
243
|
+
"gpt-5.4": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
244
|
+
"gpt-5.4-mini": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
245
|
+
"gpt-5.2": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
246
|
+
"gpt-5.2-pro": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
247
|
+
"gpt-5.1": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
248
|
+
"gpt-5": (w, c) => applyOpenAIResponses(w, c.modelId),
|
|
249
|
+
};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { ProviderModelConfig } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { OPENAI_COMPLETIONS_COMPAT } from "./_protocols.ts";
|
|
3
|
+
import { isAtLeastHigh, type PayloadWriter, type ThinkingApplier, type ThinkingLevel } from "../_tools.ts";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 阿里云通义(Alibaba Qwen)系列。
|
|
7
|
+
* 官方文档:https://help.aliyun.com/zh/model-studio/
|
|
8
|
+
*/
|
|
9
|
+
export function qwenModels(root: string): ProviderModelConfig[] {
|
|
10
|
+
const v1 = `${root}/v1`;
|
|
11
|
+
return [
|
|
12
|
+
{
|
|
13
|
+
id: "qwen3.7-plus",
|
|
14
|
+
name: "Qwen3.7 Plus",
|
|
15
|
+
api: "openai-completions",
|
|
16
|
+
baseUrl: v1,
|
|
17
|
+
reasoning: true,
|
|
18
|
+
input: ["text", "image"],
|
|
19
|
+
cost: {
|
|
20
|
+
input: 0.295,
|
|
21
|
+
output: 1.18,
|
|
22
|
+
cacheRead: 0.059,
|
|
23
|
+
cacheWrite: 0.36875,
|
|
24
|
+
},
|
|
25
|
+
contextWindow: 1000000,
|
|
26
|
+
maxTokens: 64000,
|
|
27
|
+
compat: OPENAI_COMPLETIONS_COMPAT,
|
|
28
|
+
},
|
|
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
|
+
];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Qwen 思维链:`enable_thinking`/`preserve_thinking`,高级档位加预算与搜索/解释器。
|
|
71
|
+
function applyQwen(writer: PayloadWriter, level: ThinkingLevel): void {
|
|
72
|
+
writer.remove("reasoning_effort");
|
|
73
|
+
writer.set("enable_search", true);
|
|
74
|
+
writer.set("parallel_tool_calls", true);
|
|
75
|
+
if (level === "off") {
|
|
76
|
+
writer.set("enable_thinking", false);
|
|
77
|
+
writer.set("preserve_thinking", false);
|
|
78
|
+
writer.remove("thinking_budget");
|
|
79
|
+
writer.remove("search_options");
|
|
80
|
+
writer.remove("enable_code_interpreter");
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
writer.set("enable_thinking", true);
|
|
85
|
+
writer.set("preserve_thinking", true);
|
|
86
|
+
if (isAtLeastHigh(level)) {
|
|
87
|
+
writer.set("thinking_budget", 6000);
|
|
88
|
+
writer.set("search_options", { search_strategy: "agent_max" });
|
|
89
|
+
writer.set("enable_code_interpreter", true);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
writer.remove("thinking_budget");
|
|
93
|
+
writer.remove("search_options");
|
|
94
|
+
writer.remove("enable_code_interpreter");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export const qwenThinking: Record<string, ThinkingApplier> = {
|
|
98
|
+
"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
|
+
};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { deepStrictEqual, equal, strictEqual } from "node:assert";
|
|
2
|
+
import { applyWebSearchTool } from "./web-search.ts";
|
|
3
|
+
import {
|
|
4
|
+
isPlainObject,
|
|
5
|
+
type Payload,
|
|
6
|
+
type WebSearchMode,
|
|
7
|
+
} from "../models/_tools.ts";
|
|
8
|
+
|
|
9
|
+
const OPENAI_RESPONSES = "openai-responses";
|
|
10
|
+
|
|
11
|
+
function apply(
|
|
12
|
+
payload: unknown,
|
|
13
|
+
modelId: string,
|
|
14
|
+
mode: WebSearchMode,
|
|
15
|
+
): Payload {
|
|
16
|
+
const result = applyWebSearchTool(payload, {
|
|
17
|
+
modelId,
|
|
18
|
+
api: OPENAI_RESPONSES,
|
|
19
|
+
mode,
|
|
20
|
+
});
|
|
21
|
+
if (!isPlainObject(result)) throw new Error("expected a plain object result");
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function testBuiltinSearchInjection(): void {
|
|
26
|
+
const functionTool = { type: "function", name: "keep" };
|
|
27
|
+
for (const mode of ["off", "cached", "live"] as const) {
|
|
28
|
+
const input = {
|
|
29
|
+
tools: [functionTool],
|
|
30
|
+
tool_choice: "auto",
|
|
31
|
+
include: ["reasoning.encrypted_content"],
|
|
32
|
+
store: false,
|
|
33
|
+
};
|
|
34
|
+
const result = apply(input, "gpt-5.6-sol", mode);
|
|
35
|
+
const tools = result.tools as unknown[];
|
|
36
|
+
equal(tools.length, mode === "off" ? 1 : 2);
|
|
37
|
+
strictEqual(tools[0], functionTool);
|
|
38
|
+
if (mode !== "off")
|
|
39
|
+
deepStrictEqual(tools[1], {
|
|
40
|
+
type: "web_search",
|
|
41
|
+
search_context_size: "medium",
|
|
42
|
+
external_web_access: mode === "live",
|
|
43
|
+
});
|
|
44
|
+
deepStrictEqual(result.tool_choice, "auto");
|
|
45
|
+
deepStrictEqual(result.include, ["reasoning.encrypted_content"]);
|
|
46
|
+
strictEqual(result.store, false);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
for (const type of [
|
|
50
|
+
"web_search",
|
|
51
|
+
"web_search_preview",
|
|
52
|
+
"web_search_custom",
|
|
53
|
+
]) {
|
|
54
|
+
const existing = { type };
|
|
55
|
+
const result = apply({ tools: [existing] }, "gpt-5.6-terra", "live");
|
|
56
|
+
strictEqual((result.tools as unknown[])[0], existing);
|
|
57
|
+
equal((result.tools as unknown[]).length, 1);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const undefinedTools = apply({}, "gpt-5.6-luna", "cached");
|
|
61
|
+
deepStrictEqual(undefinedTools.tools, [
|
|
62
|
+
{
|
|
63
|
+
type: "web_search",
|
|
64
|
+
search_context_size: "medium",
|
|
65
|
+
external_web_access: false,
|
|
66
|
+
},
|
|
67
|
+
]);
|
|
68
|
+
const invalidTools = apply({ tools: null }, "gpt-5.5", "live");
|
|
69
|
+
strictEqual(invalidTools.tools, null);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function testNoOpsAndScope(): void {
|
|
73
|
+
// 非内置查询名单内的模型不注入。
|
|
74
|
+
const nonSearch = { tools: [functionTool] };
|
|
75
|
+
strictEqual(
|
|
76
|
+
applyWebSearchTool(nonSearch, {
|
|
77
|
+
modelId: "gpt-5.3-codex-spark",
|
|
78
|
+
api: OPENAI_RESPONSES,
|
|
79
|
+
mode: "live",
|
|
80
|
+
}),
|
|
81
|
+
nonSearch,
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
// 非 Responses 协议不注入。
|
|
85
|
+
strictEqual(
|
|
86
|
+
applyWebSearchTool(nonSearch, {
|
|
87
|
+
modelId: "gpt-5.6-sol",
|
|
88
|
+
api: "openai-completions",
|
|
89
|
+
mode: "live",
|
|
90
|
+
}),
|
|
91
|
+
nonSearch,
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
// 非 plain 对象原样返回。
|
|
95
|
+
const nonPlain = [null, [], new Date()] as const;
|
|
96
|
+
for (const payload of nonPlain)
|
|
97
|
+
strictEqual(
|
|
98
|
+
applyWebSearchTool(payload, {
|
|
99
|
+
modelId: "gpt-5.6-sol",
|
|
100
|
+
api: OPENAI_RESPONSES,
|
|
101
|
+
mode: "live",
|
|
102
|
+
}),
|
|
103
|
+
payload,
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const functionTool = { type: "function", name: "keep" };
|
|
108
|
+
|
|
109
|
+
testBuiltinSearchInjection();
|
|
110
|
+
testNoOpsAndScope();
|
|
111
|
+
console.log("web-search.test.ts: all assertions passed");
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 网络模块:内置查询(built-in web search)工具注入。
|
|
3
|
+
*
|
|
4
|
+
* 与模型模块平行、互不依赖:这里只维护"哪些模型支持内置查询"及注入
|
|
5
|
+
* 格式,不读取模型目录数据。支持的模型:GPT 系列(OpenAI Responses
|
|
6
|
+
* 协议)与 Grok 系列(xAI,工具名同为 `web_search`,见
|
|
7
|
+
* https://docs.x.ai/developers/tools/web-search)。Grok 的协议端点与
|
|
8
|
+
* 注入参数待其模型加入目录后按网关实际行为确认。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
isPlainObject,
|
|
13
|
+
PayloadWriter,
|
|
14
|
+
type WebSearchMode,
|
|
15
|
+
} from "../models/_tools.ts";
|
|
16
|
+
|
|
17
|
+
const OPENAI_RESPONSES = "openai-responses";
|
|
18
|
+
|
|
19
|
+
// 支持内置查询的模型名单。Grok 系列工具格式已确认(`web_search`),
|
|
20
|
+
// 待其协议端点确认后加入。
|
|
21
|
+
const BUILTIN_SEARCH_MODELS = new Set([
|
|
22
|
+
"gpt-5.6-sol",
|
|
23
|
+
"gpt-5.6-terra",
|
|
24
|
+
"gpt-5.6-luna",
|
|
25
|
+
"gpt-5.5",
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
function isExistingWebSearchTool(tools: readonly unknown[]): boolean {
|
|
29
|
+
return tools.some((tool) => {
|
|
30
|
+
if (!isPlainObject(tool) || typeof tool.type !== "string") return false;
|
|
31
|
+
return tool.type === "web_search" || tool.type.startsWith("web_search_");
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface WebSearchContext {
|
|
36
|
+
modelId: string;
|
|
37
|
+
api: string;
|
|
38
|
+
mode: WebSearchMode;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Append-only web_search tool injection for models with built-in search.
|
|
43
|
+
* Non-plain payloads and unsupported models are returned unchanged.
|
|
44
|
+
*
|
|
45
|
+
* `cached` uses `external_web_access: false`; `live` uses `true`. Existing
|
|
46
|
+
* function tools, `tool_choice`, and `include` are preserved. Pi drops
|
|
47
|
+
* requested sources, so this operation deliberately does not request them.
|
|
48
|
+
*/
|
|
49
|
+
export function applyWebSearchTool(
|
|
50
|
+
payload: unknown,
|
|
51
|
+
context: WebSearchContext,
|
|
52
|
+
): unknown {
|
|
53
|
+
if (!isPlainObject(payload) || context.mode === "off") return payload;
|
|
54
|
+
if (
|
|
55
|
+
context.api !== OPENAI_RESPONSES ||
|
|
56
|
+
!BUILTIN_SEARCH_MODELS.has(context.modelId)
|
|
57
|
+
)
|
|
58
|
+
return payload;
|
|
59
|
+
const writer = new PayloadWriter(payload);
|
|
60
|
+
const tools = writer.get("tools");
|
|
61
|
+
if (tools !== undefined && !Array.isArray(tools)) return payload;
|
|
62
|
+
const currentTools = tools ?? [];
|
|
63
|
+
if (isExistingWebSearchTool(currentTools)) return payload;
|
|
64
|
+
writer.set("tools", [
|
|
65
|
+
...currentTools,
|
|
66
|
+
{
|
|
67
|
+
type: "web_search",
|
|
68
|
+
search_context_size: "medium",
|
|
69
|
+
external_web_access: context.mode === "live",
|
|
70
|
+
},
|
|
71
|
+
]);
|
|
72
|
+
return writer.result();
|
|
73
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pi-tsgw",
|
|
3
|
+
"version": "0.0.1",
|
|
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
|
+
"type": "module",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"pi",
|
|
8
|
+
"pi-package",
|
|
9
|
+
"tsgw",
|
|
10
|
+
"gateway",
|
|
11
|
+
"models"
|
|
12
|
+
],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/tossp/pi-tsgw.git"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"extensions"
|
|
20
|
+
],
|
|
21
|
+
"pi": {
|
|
22
|
+
"extensions": [
|
|
23
|
+
"./extensions/index.ts"
|
|
24
|
+
]
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"test": "tsc && node dist/extensions/index.test.js && node dist/extensions/models/operations.test.js && node dist/extensions/models/catalog.test.js && node dist/extensions/web-search/web-search.test.js"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"@earendil-works/pi-coding-agent": ">=0.82.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@earendil-works/pi-coding-agent": "^0.83.0",
|
|
34
|
+
"typescript": "^7.0.2"
|
|
35
|
+
}
|
|
36
|
+
}
|