pi-tsgw 0.0.1 → 0.0.3

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.
@@ -1,428 +0,0 @@
1
- import { deepStrictEqual, equal, strictEqual } from "node:assert";
2
- import { applyModelOperations } from "./operations.ts";
3
- import {
4
- isPlainObject,
5
- type ModelOperationContext,
6
- type ThinkingLevel,
7
- } from "./_tools.ts";
8
-
9
- type Payload = Record<string, unknown>;
10
-
11
- const levels = [
12
- "off",
13
- "high",
14
- "xhigh",
15
- "max",
16
- ] as const satisfies readonly ThinkingLevel[];
17
-
18
- function deepFreeze(value: unknown): void {
19
- if (Array.isArray(value)) {
20
- for (const item of value) deepFreeze(item);
21
- Object.freeze(value);
22
- return;
23
- }
24
- if (isPlainObject(value)) {
25
- for (const item of Object.values(value)) deepFreeze(item);
26
- Object.freeze(value);
27
- }
28
- }
29
-
30
- function context(
31
- modelId: string,
32
- api = "openai-completions",
33
- thinkingLevel: ThinkingLevel = "high",
34
- ): ModelOperationContext {
35
- return { provider: "tsgw", modelId, api, thinkingLevel };
36
- }
37
-
38
- function apply(
39
- payload: Payload,
40
- modelId: string,
41
- api: string,
42
- thinkingLevel: ThinkingLevel,
43
- ): Payload {
44
- deepFreeze(payload);
45
- const result = applyModelOperations(
46
- payload,
47
- context(modelId, api, thinkingLevel),
48
- );
49
- if (!isPlainObject(result)) throw new Error("expected a plain object result");
50
- return result;
51
- }
52
-
53
- function completionBase(): Payload {
54
- return {
55
- stream: true,
56
- reasoning_effort: "medium",
57
- thinking: { type: "legacy", retained: true },
58
- };
59
- }
60
-
61
- function testCompletionProfiles(): void {
62
- for (const modelId of ["deepseek-v4-flash", "deepseek-v4-pro"]) {
63
- for (const level of levels) {
64
- const result = apply(
65
- completionBase(),
66
- modelId,
67
- "openai-completions",
68
- level,
69
- );
70
- const effort =
71
- level === "off"
72
- ? undefined
73
- : level === "xhigh" || level === "max"
74
- ? "max"
75
- : "high";
76
- deepStrictEqual(
77
- result,
78
- effort === undefined
79
- ? { stream: true, thinking: { type: "disabled", retained: true } }
80
- : {
81
- stream: true,
82
- reasoning_effort: effort,
83
- thinking: { type: "enabled", retained: true },
84
- },
85
- );
86
- }
87
- }
88
- deepStrictEqual(
89
- apply(
90
- completionBase(),
91
- "deepseek-v4-flash",
92
- "openai-completions",
93
- "minimal",
94
- ),
95
- {
96
- stream: true,
97
- reasoning_effort: "high",
98
- thinking: { type: "enabled", retained: true },
99
- },
100
- );
101
-
102
- for (const modelId of ["glm-5.2", "glm-5.1"]) {
103
- for (const level of levels) {
104
- const result = apply(
105
- completionBase(),
106
- modelId,
107
- "openai-completions",
108
- level,
109
- );
110
- const effort =
111
- level === "off"
112
- ? "none"
113
- : level === "xhigh" || level === "max"
114
- ? "max"
115
- : "high";
116
- deepStrictEqual(result, {
117
- stream: true,
118
- tool_stream: true,
119
- reasoning_effort: effort,
120
- thinking:
121
- level === "off"
122
- ? { type: "disabled", retained: true }
123
- : { type: "enabled", retained: true, clear_thinking: false },
124
- });
125
- }
126
- }
127
- const glmNotStreaming = apply(
128
- { stream: false, reasoning_effort: "medium", thinking: { type: "legacy" } },
129
- "glm-5.2",
130
- "openai-completions",
131
- "high",
132
- );
133
- equal(Object.hasOwn(glmNotStreaming, "tool_stream"), false);
134
-
135
- for (const modelId of [
136
- "mimo-v2.5",
137
- "mimo-v2.5-pro",
138
- "kimi-for-coding",
139
- "longcat-2.0",
140
- ]) {
141
- for (const level of levels) {
142
- const result = apply(
143
- completionBase(),
144
- modelId,
145
- "openai-completions",
146
- level,
147
- );
148
- deepStrictEqual(result, {
149
- stream: true,
150
- thinking: {
151
- type: level === "off" ? "disabled" : "enabled",
152
- retained: true,
153
- },
154
- });
155
- }
156
- }
157
-
158
- for (const level of levels) {
159
- const result = apply(
160
- completionBase(),
161
- "minimax-m3",
162
- "openai-completions",
163
- level,
164
- );
165
- deepStrictEqual(result, {
166
- stream: true,
167
- reasoning_split: true,
168
- thinking: {
169
- type: level === "off" ? "disabled" : "adaptive",
170
- retained: true,
171
- },
172
- });
173
- }
174
-
175
- for (const level of [
176
- "off",
177
- "minimal",
178
- "low",
179
- "medium",
180
- "high",
181
- "xhigh",
182
- "max",
183
- ] as const satisfies readonly ThinkingLevel[]) {
184
- const result = apply(
185
- completionBase(),
186
- "kimi-k3",
187
- "openai-completions",
188
- level,
189
- );
190
- const effort =
191
- level === "off"
192
- ? undefined
193
- : level === "minimal" || level === "low"
194
- ? "low"
195
- : level === "max"
196
- ? "max"
197
- : "high";
198
- deepStrictEqual(
199
- result,
200
- effort === undefined
201
- ? { stream: true }
202
- : { stream: true, reasoning_effort: effort },
203
- );
204
- }
205
-
206
- for (const modelId of ["qwen3.7-plus", "qwen3.7-max"]) {
207
- for (const level of levels) {
208
- const result = apply(
209
- {
210
- ...completionBase(),
211
- thinking_budget: 1,
212
- search_options: { old: true },
213
- enable_code_interpreter: false,
214
- },
215
- modelId,
216
- "openai-completions",
217
- level,
218
- );
219
- const expected: Payload = {
220
- stream: true,
221
- enable_search: true,
222
- parallel_tool_calls: true,
223
- enable_thinking: level !== "off",
224
- preserve_thinking: level !== "off",
225
- thinking: { type: "legacy", retained: true },
226
- };
227
- if (level !== "off") {
228
- expected.thinking_budget = 6000;
229
- expected.search_options = { search_strategy: "agent_max" };
230
- expected.enable_code_interpreter = true;
231
- }
232
- deepStrictEqual(result, expected);
233
- }
234
- }
235
- const qwenLow = apply(
236
- {
237
- ...completionBase(),
238
- thinking_budget: 1,
239
- search_options: { old: true },
240
- enable_code_interpreter: false,
241
- },
242
- "qwen3.7-plus",
243
- "openai-completions",
244
- "low",
245
- );
246
- deepStrictEqual(qwenLow, {
247
- stream: true,
248
- thinking: { type: "legacy", retained: true },
249
- enable_search: true,
250
- parallel_tool_calls: true,
251
- enable_thinking: true,
252
- preserve_thinking: true,
253
- });
254
- }
255
-
256
- function testGeminiProfiles(): void {
257
- for (const level of levels) {
258
- const tools = [{ functionDeclarations: [{ name: "keep" }] }];
259
- const signal = new AbortController().signal;
260
- const input = {
261
- reasoning_effort: "medium",
262
- config: {
263
- tools,
264
- abortSignal: signal,
265
- thinkingConfig: { retained: true, thinkingLevel: "HIGH" },
266
- },
267
- };
268
- const result = apply(input, "gemini-flash", "google-generative-ai", level);
269
- const budget = level === "max" ? 24576 : level === "off" ? 0 : 16000;
270
- deepStrictEqual(result, {
271
- config: {
272
- tools,
273
- abortSignal: signal,
274
- thinkingConfig: {
275
- retained: true,
276
- includeThoughts: level !== "off",
277
- thinkingBudget: budget,
278
- },
279
- },
280
- });
281
- strictEqual((result.config as Payload).tools, tools);
282
- strictEqual((result.config as Payload).abortSignal, signal);
283
- }
284
-
285
- const proLevels: readonly ThinkingLevel[] = [
286
- "off",
287
- "minimal",
288
- "low",
289
- "medium",
290
- "high",
291
- "xhigh",
292
- "max",
293
- ];
294
- for (const level of proLevels) {
295
- const tools = [{ functionDeclarations: [{ name: "keep" }] }];
296
- const signal = new AbortController().signal;
297
- const input = {
298
- reasoning_effort: "medium",
299
- config: {
300
- tools,
301
- abortSignal: signal,
302
- thinkingConfig: { retained: true, thinkingBudget: 42 },
303
- },
304
- };
305
- const result = apply(input, "gemini-pro", "google-generative-ai", level);
306
- const thinkingLevel =
307
- level === "off" || level === "minimal" || level === "low"
308
- ? "LOW"
309
- : level === "medium"
310
- ? "MEDIUM"
311
- : "HIGH";
312
- deepStrictEqual(result, {
313
- config: {
314
- tools,
315
- abortSignal: signal,
316
- thinkingConfig: {
317
- retained: true,
318
- includeThoughts: level !== "off",
319
- thinkingLevel,
320
- },
321
- },
322
- });
323
- strictEqual((result.config as Payload).tools, tools);
324
- strictEqual((result.config as Payload).abortSignal, signal);
325
- }
326
- }
327
-
328
- function testOpenAIResponsesProfiles(): void {
329
- const verbosityByModel: Record<string, "low" | "medium"> = {
330
- "gpt-5.6-sol": "low",
331
- "gpt-5.6-terra": "medium",
332
- "gpt-5.6-luna": "low",
333
- "gpt-5.5": "low",
334
- "gpt-5.3-codex-spark": "low",
335
- };
336
- for (const [modelId, verbosity] of Object.entries(verbosityByModel)) {
337
- for (const level of levels) {
338
- const reasoning = { effort: "high", summary: "auto", retained: true };
339
- const input = {
340
- reasoning,
341
- text: { format: "text" },
342
- include: ["reasoning.encrypted_content"],
343
- store: false,
344
- parallel_tool_calls: false,
345
- };
346
- const result = apply(input, modelId, "openai-responses", level);
347
- deepStrictEqual(result, {
348
- reasoning,
349
- text: { format: "text", verbosity },
350
- include: ["reasoning.encrypted_content"],
351
- store: false,
352
- parallel_tool_calls: false,
353
- service_tier: "flex",
354
- });
355
- strictEqual(result.reasoning, reasoning);
356
- }
357
- }
358
- }
359
-
360
- function testNoOpsAndProtectedFields(): void {
361
- const nonPlain = [null, [], new Date()] as const;
362
- for (const payload of nonPlain)
363
- strictEqual(
364
- applyModelOperations(payload, context("deepseek-v4-flash")),
365
- payload,
366
- );
367
- const claude = { model: "keep" };
368
- strictEqual(
369
- applyModelOperations(
370
- claude,
371
- context("claude-fable-5", "anthropic-messages"),
372
- ),
373
- claude,
374
- );
375
- strictEqual(
376
- applyModelOperations(claude, {
377
- ...context("gpt-5.6-sol", "openai-responses"),
378
- provider: "other",
379
- }),
380
- claude,
381
- );
382
-
383
- const input: Payload = {
384
- model: "keep",
385
- messages: [{ role: "user", content: "keep" }],
386
- input: ["keep"],
387
- contents: ["keep"],
388
- system: "keep",
389
- tools: [{ type: "function", name: "keep" }],
390
- tool_choice: "auto",
391
- headers: { keep: "yes" },
392
- stream: true,
393
- include: ["keep"],
394
- store: false,
395
- prompt_cache_key: "keep",
396
- max_output_tokens: 9,
397
- reasoning_effort: "keep",
398
- reasoning: { effort: "high", summary: "auto" },
399
- parallel_tool_calls: false,
400
- };
401
- const result = apply(input, "gpt-5.6-terra", "openai-responses", "high");
402
- for (const key of [
403
- "model",
404
- "messages",
405
- "input",
406
- "contents",
407
- "system",
408
- "tools",
409
- "tool_choice",
410
- "headers",
411
- "stream",
412
- "include",
413
- "store",
414
- "prompt_cache_key",
415
- "max_output_tokens",
416
- "reasoning_effort",
417
- "reasoning",
418
- "parallel_tool_calls",
419
- ]) {
420
- deepStrictEqual(result[key], input[key]);
421
- }
422
- }
423
-
424
- testCompletionProfiles();
425
- testGeminiProfiles();
426
- testOpenAIResponsesProfiles();
427
- testNoOpsAndProtectedFields();
428
- console.log("operations.test.ts: all assertions passed");
@@ -1,111 +0,0 @@
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");
@@ -1,73 +0,0 @@
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
- }