pi-openai-toolkit 0.20.7 → 0.21.0

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.
Files changed (55) hide show
  1. package/LICENSE +21 -21
  2. package/NOTICE +83 -83
  3. package/README.md +8 -26
  4. package/README.zh.md +8 -26
  5. package/config.schema.json +278 -366
  6. package/docs/configuration.md +294 -310
  7. package/docs/internals.md +166 -175
  8. package/package.json +3 -13
  9. package/src/compact-client.ts +412 -389
  10. package/src/compaction-errors.ts +96 -0
  11. package/src/compaction-projection.ts +35 -35
  12. package/src/config/legacy.ts +519 -717
  13. package/src/config/migration.ts +103 -100
  14. package/src/config/notifications.ts +36 -28
  15. package/src/config/policy.ts +10 -18
  16. package/src/config/v2.ts +1167 -1380
  17. package/src/config.ts +310 -319
  18. package/src/context-management/codex-provider.ts +270 -270
  19. package/src/context-management/history-notes.ts +357 -357
  20. package/src/context-management/manual-compact.ts +0 -11
  21. package/src/context-management/tools.ts +371 -371
  22. package/src/context-management/types.ts +168 -168
  23. package/src/context-management/window-manager.ts +801 -801
  24. package/src/debug.ts +238 -237
  25. package/src/deferred-tool-carryover.ts +282 -282
  26. package/src/details-store.ts +151 -151
  27. package/src/extension-runtime.ts +1662 -1623
  28. package/src/image-generation/extension.ts +189 -189
  29. package/src/image-generation/protocol.ts +579 -579
  30. package/src/image-generation/types.ts +300 -300
  31. package/src/native-fallback.ts +4 -132
  32. package/src/payload-rewrite.ts +515 -518
  33. package/src/pi-context-hook.ts +117 -117
  34. package/src/remote-v2-client.ts +811 -814
  35. package/src/request-context-cache.ts +155 -155
  36. package/src/responses-headers.ts +180 -180
  37. package/src/runtime.ts +865 -832
  38. package/src/serializer.ts +882 -882
  39. package/src/supported-environment.ts +27 -27
  40. package/src/types.ts +565 -648
  41. package/src/web-search/alpha-client.ts +876 -876
  42. package/src/web-search/extension.ts +530 -530
  43. package/src/web-search/payload.ts +299 -299
  44. package/src/web-search/prompt.ts +60 -60
  45. package/src/web-search/types.ts +166 -166
  46. package/tsconfig.check.json +50 -54
  47. package/src/auto-mode/classifier.ts +0 -161
  48. package/src/auto-mode/extension.ts +0 -691
  49. package/src/auto-mode/model-selection-guard.ts +0 -52
  50. package/src/auto-mode/policy.ts +0 -146
  51. package/src/auto-mode/prompt.ts +0 -279
  52. package/src/auto-mode/reviewer.ts +0 -325
  53. package/src/auto-mode/tool-review-tui.ts +0 -211
  54. package/src/auto-mode/transcript.ts +0 -135
  55. package/src/auto-mode/types.ts +0 -137
@@ -1,299 +1,299 @@
1
- import type { WebSearchConfig } from "../types";
2
- import {
3
- LOCAL_WEB_SEARCH_TOOL_NAME,
4
- resolveWebSearchRoute,
5
- WEB_RUN_TOOL_NAME,
6
- WEB_SEARCH_SOURCE_INCLUDE,
7
- isWebSearchRoute,
8
- type WebSearchModel,
9
- type WebSearchPayloadTransform,
10
- } from "./types";
11
-
12
- type JsonObject = Record<string, unknown>;
13
-
14
- function isRecord(value: unknown): value is JsonObject {
15
- return !!value && typeof value === "object" && !Array.isArray(value);
16
- }
17
-
18
- function isLocalWebSearchFunction(tool: unknown): boolean {
19
- return isRecord(tool) && tool.type === "function" && tool.name === LOCAL_WEB_SEARCH_TOOL_NAME;
20
- }
21
-
22
- function isStandaloneWebRunFunction(tool: unknown): boolean {
23
- return isRecord(tool) && tool.type === "function" && tool.name === WEB_RUN_TOOL_NAME;
24
- }
25
-
26
- function isNativeWebSearchTool(tool: unknown): boolean {
27
- return isRecord(tool) && (tool.type === "web_search" || tool.type === "web_search_preview");
28
- }
29
-
30
- function explicitRouteFailure(resolution: ReturnType<typeof resolveWebSearchRoute>): WebSearchPayloadTransform {
31
- const reason = resolution.route === "none" ? resolution.reason : resolution.reason;
32
- return {
33
- payload: undefined,
34
- outcome: "unavailable-route",
35
- changed: false,
36
- fatal: true,
37
- errorMessage: `Web Search route is unavailable${reason ? ` (${reason})` : ""}; request aborted without fallback.`,
38
- };
39
- }
40
-
41
- function unavailableRouteTransform(
42
- payload: unknown,
43
- resolution: ReturnType<typeof resolveWebSearchRoute>,
44
- ): WebSearchPayloadTransform {
45
- const base = explicitRouteFailure(resolution);
46
- return { ...base, payload };
47
- }
48
-
49
- function transformStandalonePayload(args: {
50
- payload: JsonObject;
51
- resolution: ReturnType<typeof resolveWebSearchRoute>;
52
- standaloneToolExcluded?: boolean;
53
- }): WebSearchPayloadTransform {
54
- const { payload, resolution, standaloneToolExcluded = false } = args;
55
- const toolsValue = payload.tools;
56
- if (toolsValue !== undefined && !Array.isArray(toolsValue)) {
57
- return {
58
- payload,
59
- outcome: "invalid-tools",
60
- changed: false,
61
- fatal: resolution.source !== "legacy",
62
- errorMessage: "Standalone Web Search requires a provider payload tools array.",
63
- };
64
- }
65
- const includeValue = payload.include;
66
- if (includeValue !== undefined && !Array.isArray(includeValue)) {
67
- return {
68
- payload,
69
- outcome: "invalid-include",
70
- changed: false,
71
- fatal: resolution.source !== "legacy",
72
- errorMessage: "Standalone Web Search requires a provider payload include array.",
73
- };
74
- }
75
-
76
- const tools = toolsValue ?? [];
77
- let standaloneToolCount = 0;
78
- const normalizedTools = tools.filter((tool) => {
79
- if (isLocalWebSearchFunction(tool) || isNativeWebSearchTool(tool)) return false;
80
- if (!isStandaloneWebRunFunction(tool)) return true;
81
- if (standaloneToolExcluded) return false;
82
- standaloneToolCount += 1;
83
- return standaloneToolCount === 1;
84
- });
85
- const include = includeValue ?? [];
86
- const normalizedInclude = include.filter((item) => item !== WEB_SEARCH_SOURCE_INCLUDE);
87
- const toolsChanged = normalizedTools.length !== tools.length;
88
- const includeChanged = normalizedInclude.length !== include.length;
89
- if (!standaloneToolExcluded && toolsValue !== undefined && !normalizedTools.some(isStandaloneWebRunFunction)) {
90
- return {
91
- payload,
92
- outcome: "unavailable-route",
93
- changed: false,
94
- fatal: resolution.source !== "legacy",
95
- errorMessage: `Standalone Web Search requires the registered ${WEB_RUN_TOOL_NAME} tool in the provider payload.`,
96
- };
97
- }
98
- if (!toolsChanged && !includeChanged) {
99
- return { payload, outcome: "standalone-route", changed: false };
100
- }
101
-
102
- return {
103
- payload: {
104
- ...payload,
105
- ...(toolsChanged ? { tools: normalizedTools } : {}),
106
- ...(includeChanged ? { include: normalizedInclude } : {}),
107
- },
108
- outcome: "removed-conflicting-tools",
109
- changed: true,
110
- };
111
- }
112
-
113
- function transformLocalPayload(args: {
114
- payload: JsonObject;
115
- resolution: ReturnType<typeof resolveWebSearchRoute>;
116
- }): WebSearchPayloadTransform {
117
- const { payload, resolution } = args;
118
- const toolsValue = payload.tools;
119
- if (toolsValue !== undefined && !Array.isArray(toolsValue)) {
120
- return {
121
- payload,
122
- outcome: "invalid-tools",
123
- changed: false,
124
- fatal: resolution.source !== "legacy",
125
- errorMessage: "Local Web Search requires a provider payload tools array.",
126
- };
127
- }
128
- const includeValue = payload.include;
129
- if (includeValue !== undefined && !Array.isArray(includeValue)) {
130
- return {
131
- payload,
132
- outcome: "invalid-include",
133
- changed: false,
134
- fatal: resolution.source !== "legacy",
135
- errorMessage: "Local Web Search requires a provider payload include array.",
136
- };
137
- }
138
-
139
- const tools = toolsValue ?? [];
140
- const normalizedTools = tools.filter(
141
- (tool) => !isStandaloneWebRunFunction(tool) && !isNativeWebSearchTool(tool),
142
- );
143
- const include = includeValue ?? [];
144
- const normalizedInclude = include.filter((item) => item !== WEB_SEARCH_SOURCE_INCLUDE);
145
- const toolsChanged = normalizedTools.length !== tools.length;
146
- const includeChanged = normalizedInclude.length !== include.length;
147
- if (!toolsChanged && !includeChanged) return { payload, outcome: "local-route", changed: false };
148
-
149
- return {
150
- payload: {
151
- ...payload,
152
- ...(toolsChanged ? { tools: normalizedTools } : {}),
153
- ...(includeChanged ? { include: normalizedInclude } : {}),
154
- },
155
- outcome: "removed-conflicting-tools",
156
- changed: true,
157
- };
158
- }
159
-
160
- export function transformWebSearchPayload(args: {
161
- model: WebSearchModel | undefined;
162
- config: WebSearchConfig;
163
- payload: unknown;
164
- /** The host filtered a successfully registered web_run out of this session's tool catalog. */
165
- standaloneToolExcluded?: boolean;
166
- }): WebSearchPayloadTransform {
167
- const { model, config, payload } = args;
168
- const resolution = resolveWebSearchRoute({ model, config });
169
-
170
- if (resolution.route === "none") {
171
- return {
172
- payload,
173
- outcome: resolution.reason === "disabled" ? "disabled" : "unsupported-model",
174
- changed: false,
175
- };
176
- }
177
- if (!resolution.available) {
178
- return unavailableRouteTransform(payload, resolution);
179
- }
180
- if (resolution.route === "local") {
181
- if (!isRecord(payload)) {
182
- return {
183
- payload,
184
- outcome: "non-object-payload",
185
- changed: false,
186
- fatal: resolution.source !== "legacy",
187
- errorMessage: "Local Web Search requires an object provider payload.",
188
- };
189
- }
190
- return transformLocalPayload({ payload, resolution });
191
- }
192
- if (resolution.route === "standalone-alpha") {
193
- if (!isRecord(payload)) {
194
- return {
195
- payload,
196
- outcome: "non-object-payload",
197
- changed: false,
198
- fatal: resolution.source !== "legacy",
199
- errorMessage: "Standalone Web Search requires an object provider payload.",
200
- };
201
- }
202
- return transformStandalonePayload({ payload, resolution, standaloneToolExcluded: args.standaloneToolExcluded });
203
- }
204
-
205
- if (!isRecord(payload)) {
206
- return {
207
- payload,
208
- outcome: "non-object-payload",
209
- changed: false,
210
- ...(resolution.source !== "legacy"
211
- ? { fatal: true, errorMessage: "Hosted Web Search requires an object provider payload." }
212
- : {}),
213
- };
214
- }
215
-
216
- const toolsValue = payload.tools;
217
- if (toolsValue !== undefined && !Array.isArray(toolsValue)) {
218
- return {
219
- payload,
220
- outcome: "invalid-tools",
221
- changed: false,
222
- ...(resolution.source !== "legacy"
223
- ? { fatal: true, errorMessage: "Hosted Web Search requires a provider payload tools array." }
224
- : {}),
225
- };
226
- }
227
- const tools = toolsValue ?? [];
228
- const includeValue = payload.include;
229
- if (includeValue !== undefined && !Array.isArray(includeValue)) {
230
- return {
231
- payload,
232
- outcome: "invalid-include",
233
- changed: false,
234
- ...(resolution.source !== "legacy"
235
- ? { fatal: true, errorMessage: "Hosted Web Search requires a provider payload include array." }
236
- : {}),
237
- };
238
- }
239
- const include = includeValue ?? [];
240
- const normalizedTools: unknown[] = [];
241
- let nativeToolCount = 0;
242
- let localToolRemoved = false;
243
- let standaloneToolRemoved = false;
244
- for (const tool of tools) {
245
- if (isLocalWebSearchFunction(tool)) {
246
- localToolRemoved = true;
247
- continue;
248
- }
249
- if (isStandaloneWebRunFunction(tool)) {
250
- standaloneToolRemoved = true;
251
- continue;
252
- }
253
- if (isNativeWebSearchTool(tool)) {
254
- nativeToolCount += 1;
255
- if (nativeToolCount > 1) continue;
256
- }
257
- normalizedTools.push(tool);
258
- }
259
- const hasNativeTool = nativeToolCount > 0;
260
- if (!hasNativeTool) {
261
- normalizedTools.push({ type: "web_search" });
262
- }
263
-
264
- const normalizedInclude: unknown[] = [];
265
- let sourceIncludeCount = 0;
266
- for (const item of include) {
267
- if (item === WEB_SEARCH_SOURCE_INCLUDE) {
268
- sourceIncludeCount += 1;
269
- if (sourceIncludeCount > 1) continue;
270
- }
271
- normalizedInclude.push(item);
272
- }
273
- if (sourceIncludeCount === 0) {
274
- normalizedInclude.push(WEB_SEARCH_SOURCE_INCLUDE);
275
- }
276
-
277
- const toolsChanged = localToolRemoved || standaloneToolRemoved || !hasNativeTool || nativeToolCount > 1;
278
- const includeChanged = sourceIncludeCount !== 1;
279
- if (!toolsChanged && !includeChanged) {
280
- return { payload, outcome: "existing-native-tool", changed: false };
281
- }
282
-
283
- return {
284
- payload: {
285
- ...payload,
286
- ...(toolsChanged ? { tools: normalizedTools } : {}),
287
- ...(includeChanged ? { include: normalizedInclude } : {}),
288
- },
289
- outcome: hasNativeTool ? "existing-native-tool" : "injected-native-tool",
290
- changed: true,
291
- };
292
- }
293
-
294
- export const _payloadTest = {
295
- isLocalWebSearchFunction,
296
- isNativeWebSearchTool,
297
- isStandaloneWebRunFunction,
298
- isWebSearchRoute,
299
- };
1
+ import type { WebSearchConfig } from "../types";
2
+ import {
3
+ LOCAL_WEB_SEARCH_TOOL_NAME,
4
+ resolveWebSearchRoute,
5
+ WEB_RUN_TOOL_NAME,
6
+ WEB_SEARCH_SOURCE_INCLUDE,
7
+ isWebSearchRoute,
8
+ type WebSearchModel,
9
+ type WebSearchPayloadTransform,
10
+ } from "./types";
11
+
12
+ type JsonObject = Record<string, unknown>;
13
+
14
+ function isRecord(value: unknown): value is JsonObject {
15
+ return !!value && typeof value === "object" && !Array.isArray(value);
16
+ }
17
+
18
+ function isLocalWebSearchFunction(tool: unknown): boolean {
19
+ return isRecord(tool) && tool.type === "function" && tool.name === LOCAL_WEB_SEARCH_TOOL_NAME;
20
+ }
21
+
22
+ function isStandaloneWebRunFunction(tool: unknown): boolean {
23
+ return isRecord(tool) && tool.type === "function" && tool.name === WEB_RUN_TOOL_NAME;
24
+ }
25
+
26
+ function isNativeWebSearchTool(tool: unknown): boolean {
27
+ return isRecord(tool) && (tool.type === "web_search" || tool.type === "web_search_preview");
28
+ }
29
+
30
+ function explicitRouteFailure(resolution: ReturnType<typeof resolveWebSearchRoute>): WebSearchPayloadTransform {
31
+ const reason = resolution.route === "none" ? resolution.reason : resolution.reason;
32
+ return {
33
+ payload: undefined,
34
+ outcome: "unavailable-route",
35
+ changed: false,
36
+ fatal: true,
37
+ errorMessage: `Web Search route is unavailable${reason ? ` (${reason})` : ""}; request aborted without fallback.`,
38
+ };
39
+ }
40
+
41
+ function unavailableRouteTransform(
42
+ payload: unknown,
43
+ resolution: ReturnType<typeof resolveWebSearchRoute>,
44
+ ): WebSearchPayloadTransform {
45
+ const base = explicitRouteFailure(resolution);
46
+ return { ...base, payload };
47
+ }
48
+
49
+ function transformStandalonePayload(args: {
50
+ payload: JsonObject;
51
+ resolution: ReturnType<typeof resolveWebSearchRoute>;
52
+ standaloneToolExcluded?: boolean;
53
+ }): WebSearchPayloadTransform {
54
+ const { payload, resolution, standaloneToolExcluded = false } = args;
55
+ const toolsValue = payload.tools;
56
+ if (toolsValue !== undefined && !Array.isArray(toolsValue)) {
57
+ return {
58
+ payload,
59
+ outcome: "invalid-tools",
60
+ changed: false,
61
+ fatal: resolution.source !== "legacy",
62
+ errorMessage: "Standalone Web Search requires a provider payload tools array.",
63
+ };
64
+ }
65
+ const includeValue = payload.include;
66
+ if (includeValue !== undefined && !Array.isArray(includeValue)) {
67
+ return {
68
+ payload,
69
+ outcome: "invalid-include",
70
+ changed: false,
71
+ fatal: resolution.source !== "legacy",
72
+ errorMessage: "Standalone Web Search requires a provider payload include array.",
73
+ };
74
+ }
75
+
76
+ const tools = toolsValue ?? [];
77
+ let standaloneToolCount = 0;
78
+ const normalizedTools = tools.filter((tool) => {
79
+ if (isLocalWebSearchFunction(tool) || isNativeWebSearchTool(tool)) return false;
80
+ if (!isStandaloneWebRunFunction(tool)) return true;
81
+ if (standaloneToolExcluded) return false;
82
+ standaloneToolCount += 1;
83
+ return standaloneToolCount === 1;
84
+ });
85
+ const include = includeValue ?? [];
86
+ const normalizedInclude = include.filter((item) => item !== WEB_SEARCH_SOURCE_INCLUDE);
87
+ const toolsChanged = normalizedTools.length !== tools.length;
88
+ const includeChanged = normalizedInclude.length !== include.length;
89
+ if (!standaloneToolExcluded && toolsValue !== undefined && !normalizedTools.some(isStandaloneWebRunFunction)) {
90
+ return {
91
+ payload,
92
+ outcome: "unavailable-route",
93
+ changed: false,
94
+ fatal: resolution.source !== "legacy",
95
+ errorMessage: `Standalone Web Search requires the registered ${WEB_RUN_TOOL_NAME} tool in the provider payload.`,
96
+ };
97
+ }
98
+ if (!toolsChanged && !includeChanged) {
99
+ return { payload, outcome: "standalone-route", changed: false };
100
+ }
101
+
102
+ return {
103
+ payload: {
104
+ ...payload,
105
+ ...(toolsChanged ? { tools: normalizedTools } : {}),
106
+ ...(includeChanged ? { include: normalizedInclude } : {}),
107
+ },
108
+ outcome: "removed-conflicting-tools",
109
+ changed: true,
110
+ };
111
+ }
112
+
113
+ function transformLocalPayload(args: {
114
+ payload: JsonObject;
115
+ resolution: ReturnType<typeof resolveWebSearchRoute>;
116
+ }): WebSearchPayloadTransform {
117
+ const { payload, resolution } = args;
118
+ const toolsValue = payload.tools;
119
+ if (toolsValue !== undefined && !Array.isArray(toolsValue)) {
120
+ return {
121
+ payload,
122
+ outcome: "invalid-tools",
123
+ changed: false,
124
+ fatal: resolution.source !== "legacy",
125
+ errorMessage: "Local Web Search requires a provider payload tools array.",
126
+ };
127
+ }
128
+ const includeValue = payload.include;
129
+ if (includeValue !== undefined && !Array.isArray(includeValue)) {
130
+ return {
131
+ payload,
132
+ outcome: "invalid-include",
133
+ changed: false,
134
+ fatal: resolution.source !== "legacy",
135
+ errorMessage: "Local Web Search requires a provider payload include array.",
136
+ };
137
+ }
138
+
139
+ const tools = toolsValue ?? [];
140
+ const normalizedTools = tools.filter(
141
+ (tool) => !isStandaloneWebRunFunction(tool) && !isNativeWebSearchTool(tool),
142
+ );
143
+ const include = includeValue ?? [];
144
+ const normalizedInclude = include.filter((item) => item !== WEB_SEARCH_SOURCE_INCLUDE);
145
+ const toolsChanged = normalizedTools.length !== tools.length;
146
+ const includeChanged = normalizedInclude.length !== include.length;
147
+ if (!toolsChanged && !includeChanged) return { payload, outcome: "local-route", changed: false };
148
+
149
+ return {
150
+ payload: {
151
+ ...payload,
152
+ ...(toolsChanged ? { tools: normalizedTools } : {}),
153
+ ...(includeChanged ? { include: normalizedInclude } : {}),
154
+ },
155
+ outcome: "removed-conflicting-tools",
156
+ changed: true,
157
+ };
158
+ }
159
+
160
+ export function transformWebSearchPayload(args: {
161
+ model: WebSearchModel | undefined;
162
+ config: WebSearchConfig;
163
+ payload: unknown;
164
+ /** The host filtered a successfully registered web_run out of this session's tool catalog. */
165
+ standaloneToolExcluded?: boolean;
166
+ }): WebSearchPayloadTransform {
167
+ const { model, config, payload } = args;
168
+ const resolution = resolveWebSearchRoute({ model, config });
169
+
170
+ if (resolution.route === "none") {
171
+ return {
172
+ payload,
173
+ outcome: resolution.reason === "disabled" ? "disabled" : "unsupported-model",
174
+ changed: false,
175
+ };
176
+ }
177
+ if (!resolution.available) {
178
+ return unavailableRouteTransform(payload, resolution);
179
+ }
180
+ if (resolution.route === "local") {
181
+ if (!isRecord(payload)) {
182
+ return {
183
+ payload,
184
+ outcome: "non-object-payload",
185
+ changed: false,
186
+ fatal: resolution.source !== "legacy",
187
+ errorMessage: "Local Web Search requires an object provider payload.",
188
+ };
189
+ }
190
+ return transformLocalPayload({ payload, resolution });
191
+ }
192
+ if (resolution.route === "standalone-alpha") {
193
+ if (!isRecord(payload)) {
194
+ return {
195
+ payload,
196
+ outcome: "non-object-payload",
197
+ changed: false,
198
+ fatal: resolution.source !== "legacy",
199
+ errorMessage: "Standalone Web Search requires an object provider payload.",
200
+ };
201
+ }
202
+ return transformStandalonePayload({ payload, resolution, standaloneToolExcluded: args.standaloneToolExcluded });
203
+ }
204
+
205
+ if (!isRecord(payload)) {
206
+ return {
207
+ payload,
208
+ outcome: "non-object-payload",
209
+ changed: false,
210
+ ...(resolution.source !== "legacy"
211
+ ? { fatal: true, errorMessage: "Hosted Web Search requires an object provider payload." }
212
+ : {}),
213
+ };
214
+ }
215
+
216
+ const toolsValue = payload.tools;
217
+ if (toolsValue !== undefined && !Array.isArray(toolsValue)) {
218
+ return {
219
+ payload,
220
+ outcome: "invalid-tools",
221
+ changed: false,
222
+ ...(resolution.source !== "legacy"
223
+ ? { fatal: true, errorMessage: "Hosted Web Search requires a provider payload tools array." }
224
+ : {}),
225
+ };
226
+ }
227
+ const tools = toolsValue ?? [];
228
+ const includeValue = payload.include;
229
+ if (includeValue !== undefined && !Array.isArray(includeValue)) {
230
+ return {
231
+ payload,
232
+ outcome: "invalid-include",
233
+ changed: false,
234
+ ...(resolution.source !== "legacy"
235
+ ? { fatal: true, errorMessage: "Hosted Web Search requires a provider payload include array." }
236
+ : {}),
237
+ };
238
+ }
239
+ const include = includeValue ?? [];
240
+ const normalizedTools: unknown[] = [];
241
+ let nativeToolCount = 0;
242
+ let localToolRemoved = false;
243
+ let standaloneToolRemoved = false;
244
+ for (const tool of tools) {
245
+ if (isLocalWebSearchFunction(tool)) {
246
+ localToolRemoved = true;
247
+ continue;
248
+ }
249
+ if (isStandaloneWebRunFunction(tool)) {
250
+ standaloneToolRemoved = true;
251
+ continue;
252
+ }
253
+ if (isNativeWebSearchTool(tool)) {
254
+ nativeToolCount += 1;
255
+ if (nativeToolCount > 1) continue;
256
+ }
257
+ normalizedTools.push(tool);
258
+ }
259
+ const hasNativeTool = nativeToolCount > 0;
260
+ if (!hasNativeTool) {
261
+ normalizedTools.push({ type: "web_search" });
262
+ }
263
+
264
+ const normalizedInclude: unknown[] = [];
265
+ let sourceIncludeCount = 0;
266
+ for (const item of include) {
267
+ if (item === WEB_SEARCH_SOURCE_INCLUDE) {
268
+ sourceIncludeCount += 1;
269
+ if (sourceIncludeCount > 1) continue;
270
+ }
271
+ normalizedInclude.push(item);
272
+ }
273
+ if (sourceIncludeCount === 0) {
274
+ normalizedInclude.push(WEB_SEARCH_SOURCE_INCLUDE);
275
+ }
276
+
277
+ const toolsChanged = localToolRemoved || standaloneToolRemoved || !hasNativeTool || nativeToolCount > 1;
278
+ const includeChanged = sourceIncludeCount !== 1;
279
+ if (!toolsChanged && !includeChanged) {
280
+ return { payload, outcome: "existing-native-tool", changed: false };
281
+ }
282
+
283
+ return {
284
+ payload: {
285
+ ...payload,
286
+ ...(toolsChanged ? { tools: normalizedTools } : {}),
287
+ ...(includeChanged ? { include: normalizedInclude } : {}),
288
+ },
289
+ outcome: hasNativeTool ? "existing-native-tool" : "injected-native-tool",
290
+ changed: true,
291
+ };
292
+ }
293
+
294
+ export const _payloadTest = {
295
+ isLocalWebSearchFunction,
296
+ isNativeWebSearchTool,
297
+ isStandaloneWebRunFunction,
298
+ isWebSearchRoute,
299
+ };