pi-tsgw 0.0.2 → 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.
package/README.md CHANGED
@@ -36,7 +36,7 @@ Example:
36
36
  {
37
37
  "tsgw": {
38
38
  "baseUrl": "https://aih.example.net",
39
- "webSearch": "off",
39
+ "tsSearch": "off",
40
40
  "traceHeaders": false,
41
41
  "includeModels": ["gpt-5.6-*", "claude-*"],
42
42
  "excludeModels": ["gpt-5.3-*"]
@@ -64,7 +64,7 @@ The extension never reads credential files. Pi resolves the key itself:
64
64
  | Setting (`settings.json` `tsgw.*`) | Default | Meaning |
65
65
  | --- | --- | --- |
66
66
  | `baseUrl` | placeholder | Gateway root (the `/v1` suffix is normalized away) |
67
- | `webSearch` | `off` | `off`, `cached`, or `live` — appends the built-in `web_search` tool for models with built-in search (GPT / Grok) |
67
+ | `tsSearch` | `off` | `off`, `cached`, or `live` — appends the built-in `web_search` tool for models with built-in search (GPT / Grok) |
68
68
  | `traceHeaders` | `false` | Adds `AH-Thread-Id` / `AH-Trace-Id` headers for gateway-side tracing |
69
69
  | `includeModels` | unset | Whitelist: only these model ids are registered (exact id or `prefix-*` glob) |
70
70
  | `excludeModels` | unset | Blacklist: these model ids are dropped (exact id or `prefix-*` glob); blacklist wins over whitelist |
@@ -12,7 +12,6 @@ import {
12
12
  } from "./models/catalog.ts";
13
13
  import { applyModelOperations } from "./models/operations.ts";
14
14
  import type { ThinkingLevel, WebSearchMode } from "./models/_tools.ts";
15
- import { applyWebSearchTool } from "./web-search/web-search.ts";
16
15
 
17
16
  /**
18
17
  * The data needed by provider hooks, copied while the lifecycle context is
@@ -40,7 +39,7 @@ interface RequestModel {
40
39
  */
41
40
  interface TsgwSettings {
42
41
  baseUrl?: string;
43
- webSearch?: string;
42
+ tsSearch?: string;
44
43
  traceHeaders?: boolean;
45
44
  includeModels?: string[];
46
45
  excludeModels?: string[];
@@ -108,9 +107,9 @@ function isTsgwTraceTarget(state: RequestState, root: string): boolean {
108
107
  export default async function registerTsgw(pi: ExtensionAPI): Promise<void> {
109
108
  const settings = readTsgwSettings();
110
109
  const root = rootForRuntime(settings.baseUrl);
111
- const webSearchMode: WebSearchMode =
112
- settings.webSearch === "cached" || settings.webSearch === "live"
113
- ? settings.webSearch
110
+ const tsSearchMode: WebSearchMode =
111
+ settings.tsSearch === "cached" || settings.tsSearch === "live"
112
+ ? settings.tsSearch
114
113
  : "off";
115
114
  const traceEnabled = settings.traceHeaders === true;
116
115
  const modelFilter: ModelFilter = {
@@ -159,13 +158,10 @@ export default async function registerTsgw(pi: ExtensionAPI): Promise<void> {
159
158
  modelId: state.modelId,
160
159
  api: state.api,
161
160
  thinkingLevel: state.thinkingLevel,
161
+ tsSearchMode: tsSearchMode,
162
162
  };
163
- // 模型模块:厂商思维链改写 → 网络模块:内置查询工具注入(两模块互不感知)。
164
- return applyWebSearchTool(applyModelOperations(event.payload, context), {
165
- modelId: state.modelId,
166
- api: state.api,
167
- mode: webSearchMode,
168
- });
163
+ // 模型模块统一完成请求改写:厂商思维链策略 + 内置查询工具注入。
164
+ return applyModelOperations(event.payload, context);
169
165
  });
170
166
 
171
167
  if (!traceEnabled) return;
@@ -20,6 +20,7 @@ export interface ModelOperationContext {
20
20
  modelId: string;
21
21
  api: string;
22
22
  thinkingLevel: ThinkingLevel;
23
+ tsSearchMode: WebSearchMode;
23
24
  }
24
25
 
25
26
  export type Payload = Record<string, unknown>;
@@ -3,6 +3,7 @@ import { anthropicModels } from "./vendors/anthropic.ts";
3
3
  import { deepseekModels } from "./vendors/deepseek.ts";
4
4
  import { geminiModels } from "./vendors/gemini.ts";
5
5
  import { glmModels } from "./vendors/glm.ts";
6
+ import { grokModels } from "./vendors/grok.ts";
6
7
  import { kimiModels } from "./vendors/kimi.ts";
7
8
  import { longcatModels } from "./vendors/longcat.ts";
8
9
  import { mimoModels } from "./vendors/mimo.ts";
@@ -95,6 +96,7 @@ export function modelsForRoot(
95
96
  ...openaiModels(root),
96
97
  ...geminiModels(root),
97
98
  ...anthropicModels(root),
99
+ ...grokModels(root),
98
100
  ];
99
101
  return filterModels(models, filter);
100
102
  }
@@ -11,9 +11,11 @@ import {
11
11
  type ModelOperationContext,
12
12
  type ThinkingApplier,
13
13
  } from "./_tools.ts";
14
+ import { applyBuiltinSearchTool } from "./web-search.ts";
14
15
  import { deepseekThinking } from "./vendors/deepseek.ts";
15
16
  import { geminiThinking } from "./vendors/gemini.ts";
16
17
  import { glmThinking } from "./vendors/glm.ts";
18
+ import { grokThinking } from "./vendors/grok.ts";
17
19
  import { kimiThinking } from "./vendors/kimi.ts";
18
20
  import { longcatThinking } from "./vendors/longcat.ts";
19
21
  import { mimoThinking } from "./vendors/mimo.ts";
@@ -34,6 +36,7 @@ const THINKING_STRATEGIES: Record<string, ThinkingApplier> = {
34
36
  ...qwenThinking,
35
37
  ...openaiThinking,
36
38
  ...geminiThinking,
39
+ ...grokThinking,
37
40
  };
38
41
 
39
42
  /**
@@ -50,5 +53,11 @@ export function applyModelOperations(
50
53
  const writer = new PayloadWriter(payload);
51
54
  const applier = THINKING_STRATEGIES[context.modelId];
52
55
  if (applier) applier(writer, context);
53
- return writer.result();
56
+ // 思维链改写后,再为支持内置查询的模型注入 web_search 工具。
57
+ return applyBuiltinSearchTool(
58
+ writer.result(),
59
+ context.modelId,
60
+ context.api,
61
+ context.tsSearchMode,
62
+ );
54
63
  }
@@ -0,0 +1,118 @@
1
+ import type { ProviderModelConfig } from "@earendil-works/pi-coding-agent";
2
+ import { OPENAI_COMPLETIONS_COMPAT } from "./_protocols.ts";
3
+ import { type PayloadWriter, type ThinkingApplier, type ThinkingLevel } from "../_tools.ts";
4
+
5
+ /**
6
+ * xAI Grok 系列(openai-completions 协议)。
7
+ * 官方文档:https://docs.x.ai/developers/models
8
+ * 定价(docs.x.ai/developers/pricing,<200k prompt 档):
9
+ * grok-4.20 / grok-4.3:input $1.25 / cached $0.20 / output $2.50 per 1M
10
+ * grok-4.5:input $2.00 / cached $0.30 / output $6.00 per 1M
11
+ */
12
+ export function grokModels(root: string): ProviderModelConfig[] {
13
+ const v1 = `${root}/v1`;
14
+ return [
15
+ {
16
+ id: "grok-4.20",
17
+ name: "Grok 4.20",
18
+ api: "openai-completions",
19
+ baseUrl: v1,
20
+ reasoning: true,
21
+ input: ["text", "image"],
22
+ cost: { input: 1.25, output: 2.5, cacheRead: 0.2, cacheWrite: 0 },
23
+ contextWindow: 1000000,
24
+ maxTokens: 65536,
25
+ compat: OPENAI_COMPLETIONS_COMPAT,
26
+ },
27
+ {
28
+ id: "grok-4.5",
29
+ name: "Grok 4.5",
30
+ api: "openai-completions",
31
+ baseUrl: v1,
32
+ reasoning: true,
33
+ input: ["text", "image"],
34
+ cost: { input: 2, output: 6, cacheRead: 0.3, cacheWrite: 0 },
35
+ contextWindow: 500000,
36
+ maxTokens: 65536,
37
+ compat: OPENAI_COMPLETIONS_COMPAT,
38
+ },
39
+ {
40
+ id: "grok-4.3-low",
41
+ name: "Grok 4.3",
42
+ api: "openai-completions",
43
+ baseUrl: v1,
44
+ reasoning: true,
45
+ input: ["text", "image"],
46
+ // 网关思考档位别名,规格继承 grok-4.3。
47
+ cost: { input: 1.25, output: 2.5, cacheRead: 0.2, cacheWrite: 0 },
48
+ contextWindow: 1000000,
49
+ maxTokens: 65536,
50
+ compat: OPENAI_COMPLETIONS_COMPAT,
51
+ },
52
+ {
53
+ id: "grok-4.3-medium",
54
+ name: "Grok 4.3",
55
+ api: "openai-completions",
56
+ baseUrl: v1,
57
+ reasoning: true,
58
+ input: ["text", "image"],
59
+ // 网关思考档位别名,规格继承 grok-4.3。
60
+ cost: { input: 1.25, output: 2.5, cacheRead: 0.2, cacheWrite: 0 },
61
+ contextWindow: 1000000,
62
+ maxTokens: 65536,
63
+ compat: OPENAI_COMPLETIONS_COMPAT,
64
+ },
65
+ {
66
+ id: "grok-4.3-high",
67
+ name: "Grok 4.3",
68
+ api: "openai-completions",
69
+ baseUrl: v1,
70
+ reasoning: true,
71
+ input: ["text", "image"],
72
+ // 网关思考档位别名,规格继承 grok-4.3。
73
+ cost: { input: 1.25, output: 2.5, cacheRead: 0.2, cacheWrite: 0 },
74
+ contextWindow: 1000000,
75
+ maxTokens: 65536,
76
+ compat: OPENAI_COMPLETIONS_COMPAT,
77
+ },
78
+ {
79
+ id: "grok-4.20-fast",
80
+ name: "Grok 4.20 Fast",
81
+ api: "openai-completions",
82
+ baseUrl: v1,
83
+ reasoning: true,
84
+ input: ["text", "image"],
85
+ // 网关快速档别名,规格继承 grok-4.20。
86
+ cost: { input: 1.25, output: 2.5, cacheRead: 0.2, cacheWrite: 0 },
87
+ contextWindow: 1000000,
88
+ maxTokens: 65536,
89
+ compat: OPENAI_COMPLETIONS_COMPAT,
90
+ },
91
+ ];
92
+ }
93
+
94
+ // Grok 思维链:`reasoning_effort`(none/low/medium/high)。
95
+ // 注意:grok-4.5 不支持禁用 reasoning(none),off 档映射到最小 effort "low"。
96
+ function applyGrok(writer: PayloadWriter, level: ThinkingLevel): void {
97
+ writer.remove("thinking");
98
+ if (level === "off") {
99
+ writer.set("reasoning_effort", "low");
100
+ return;
101
+ }
102
+ const effort =
103
+ level === "minimal" || level === "low"
104
+ ? "low"
105
+ : level === "medium"
106
+ ? "medium"
107
+ : "high";
108
+ writer.set("reasoning_effort", effort);
109
+ }
110
+
111
+ export const grokThinking: Record<string, ThinkingApplier> = {
112
+ "grok-4.20": (w, c) => applyGrok(w, c.thinkingLevel),
113
+ "grok-4.5": (w, c) => applyGrok(w, c.thinkingLevel),
114
+ "grok-4.3-low": (w, c) => applyGrok(w, c.thinkingLevel),
115
+ "grok-4.3-medium": (w, c) => applyGrok(w, c.thinkingLevel),
116
+ "grok-4.3-high": (w, c) => applyGrok(w, c.thinkingLevel),
117
+ "grok-4.20-fast": (w, c) => applyGrok(w, c.thinkingLevel),
118
+ };
@@ -226,10 +226,11 @@ export function openaiModels(root: string): ProviderModelConfig[] {
226
226
  ];
227
227
  }
228
228
 
229
- // GPT Responses 思维链:保留 Pi 的 `reasoning`,固定 `service_tier=flex`,按别名调文本 verbosity。
229
+ // GPT Responses 思维链:保留 Pi 的 `reasoning`,按别名调文本 verbosity。
230
+ // 不写 service_tier:flex 为 beta 有限支持(受模型/账号限制),会被 OpenAI upstream 以
231
+ // 400 "Unsupported service_tier: flex" 拒绝,让请求走项目默认 tier。
230
232
  function applyOpenAIResponses(writer: PayloadWriter, modelId: string): void {
231
233
  const verbosity = modelId === "gpt-5.6-terra" ? "medium" : "low";
232
- writer.set("service_tier", "flex");
233
234
  writer.setTextVerbosity(verbosity);
234
235
  }
235
236
 
@@ -0,0 +1,97 @@
1
+ /**
2
+ * 内置查询注入(Built-in web search tool injection)。
3
+ *
4
+ * 对原生支持内置查询的模型(GPT Responses / Grok),在请求改写阶段把
5
+ * 搜索参数追加进请求。属于模型请求改写的一部分,与思维链策略一起在
6
+ * `before_provider_request` 中应用。
7
+ *
8
+ * - GPT(openai-responses):追加 `tools: [{ type: "web_search", ... }]`
9
+ * - Grok(openai-completions):追加 `search_parameters: { mode: "on" }`
10
+ * (xAI 文档:https://docs.x.ai/developers/tools/web-search)
11
+ *
12
+ * 独立的 ts_search 工具(所有模型可用)单独实现,与内置查询注入分离。
13
+ */
14
+
15
+ import { isPlainObject, PayloadWriter, type WebSearchMode } from "./_tools.ts";
16
+
17
+ const OPENAI_RESPONSES = "openai-responses";
18
+ const OPENAI_COMPLETIONS = "openai-completions";
19
+
20
+ // 支持内置查询的模型名单(GPT Responses 系列)。
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
+ // 支持内置查询的 Grok 系列(openai-completions 协议,search_parameters 模式)。
29
+ const GROK_SEARCH_MODELS = new Set([
30
+ "grok-4.20",
31
+ "grok-4.5",
32
+ "grok-4.3-low",
33
+ "grok-4.3-medium",
34
+ "grok-4.3-high",
35
+ "grok-4.20-fast",
36
+ ]);
37
+
38
+ function isExistingWebSearchTool(tools: readonly unknown[]): boolean {
39
+ return tools.some((tool) => {
40
+ if (!isPlainObject(tool) || typeof tool.type !== "string") return false;
41
+ return tool.type === "web_search" || tool.type.startsWith("web_search_");
42
+ });
43
+ }
44
+
45
+ function hasGrokSearchFields(payload: Record<string, unknown>): boolean {
46
+ return (
47
+ Object.hasOwn(payload, "search_parameters") ||
48
+ Object.hasOwn(payload, "web_search_options")
49
+ );
50
+ }
51
+
52
+ function applyGptSearch(writer: PayloadWriter, mode: WebSearchMode): void {
53
+ const tools = writer.get("tools");
54
+ if (tools !== undefined && !Array.isArray(tools)) return;
55
+ const currentTools = tools ?? [];
56
+ if (isExistingWebSearchTool(currentTools)) return;
57
+ writer.set("tools", [
58
+ ...currentTools,
59
+ {
60
+ type: "web_search",
61
+ search_context_size: "medium",
62
+ external_web_access: mode === "live",
63
+ },
64
+ ]);
65
+ }
66
+
67
+ function applyGrokSearch(writer: PayloadWriter): void {
68
+ if (hasGrokSearchFields(writer.result())) return;
69
+ writer.set("search_parameters", { mode: "on" });
70
+ }
71
+
72
+ /**
73
+ * Append-only built-in search injection for models with native search.
74
+ * Non-plain payloads, `off` mode, and unsupported models are returned
75
+ * unchanged. Existing function tools, `tool_choice`, and `include` are
76
+ * preserved. Pi drops requested sources, so GPT injection deliberately does
77
+ * not request them.
78
+ */
79
+ export function applyBuiltinSearchTool(
80
+ payload: unknown,
81
+ modelId: string,
82
+ api: string,
83
+ mode: WebSearchMode,
84
+ ): unknown {
85
+ if (!isPlainObject(payload) || mode === "off") return payload;
86
+ const writer = new PayloadWriter(payload);
87
+
88
+ if (api === OPENAI_RESPONSES && BUILTIN_SEARCH_MODELS.has(modelId)) {
89
+ applyGptSearch(writer, mode);
90
+ return writer.result();
91
+ }
92
+ if (api === OPENAI_COMPLETIONS && GROK_SEARCH_MODELS.has(modelId)) {
93
+ applyGrokSearch(writer);
94
+ return writer.result();
95
+ }
96
+ return payload;
97
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-tsgw",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
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": [
@@ -21,13 +21,13 @@
21
21
  ]
22
22
  },
23
23
  "scripts": {
24
- "test": "tsc -p tsconfig.test.json && 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"
24
+ "test": "tsc -p tsconfig.test.json && node dist/extensions/index.test.js && node dist/extensions/models/operations.test.js && node dist/extensions/models/catalog.test.js && node dist/extensions/models/web-search.test.js"
25
25
  },
26
26
  "peerDependencies": {
27
27
  "@earendil-works/pi-coding-agent": ">=0.82.0"
28
28
  },
29
29
  "devDependencies": {
30
- "@earendil-works/pi-coding-agent": "^0.83.0",
30
+ "@earendil-works/pi-coding-agent": "^0.84.1",
31
31
  "typescript": "^7.0.2"
32
32
  }
33
33
  }
@@ -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
- }