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.
- package/LICENSE +21 -21
- package/NOTICE +83 -83
- package/README.md +8 -26
- package/README.zh.md +8 -26
- package/config.schema.json +278 -366
- package/docs/configuration.md +294 -310
- package/docs/internals.md +166 -175
- package/package.json +3 -13
- package/src/compact-client.ts +412 -389
- package/src/compaction-errors.ts +96 -0
- package/src/compaction-projection.ts +35 -35
- package/src/config/legacy.ts +519 -717
- package/src/config/migration.ts +103 -100
- package/src/config/notifications.ts +36 -28
- package/src/config/policy.ts +10 -18
- package/src/config/v2.ts +1167 -1380
- package/src/config.ts +310 -319
- package/src/context-management/codex-provider.ts +270 -270
- package/src/context-management/history-notes.ts +357 -357
- package/src/context-management/manual-compact.ts +0 -11
- package/src/context-management/tools.ts +371 -371
- package/src/context-management/types.ts +168 -168
- package/src/context-management/window-manager.ts +801 -801
- package/src/debug.ts +238 -237
- package/src/deferred-tool-carryover.ts +282 -282
- package/src/details-store.ts +151 -151
- package/src/extension-runtime.ts +1662 -1623
- package/src/image-generation/extension.ts +189 -189
- package/src/image-generation/protocol.ts +579 -579
- package/src/image-generation/types.ts +300 -300
- package/src/native-fallback.ts +4 -132
- package/src/payload-rewrite.ts +515 -518
- package/src/pi-context-hook.ts +117 -117
- package/src/remote-v2-client.ts +811 -814
- package/src/request-context-cache.ts +155 -155
- package/src/responses-headers.ts +180 -180
- package/src/runtime.ts +865 -832
- package/src/serializer.ts +882 -882
- package/src/supported-environment.ts +27 -27
- package/src/types.ts +565 -648
- package/src/web-search/alpha-client.ts +876 -876
- package/src/web-search/extension.ts +530 -530
- package/src/web-search/payload.ts +299 -299
- package/src/web-search/prompt.ts +60 -60
- package/src/web-search/types.ts +166 -166
- package/tsconfig.check.json +50 -54
- package/src/auto-mode/classifier.ts +0 -161
- package/src/auto-mode/extension.ts +0 -691
- package/src/auto-mode/model-selection-guard.ts +0 -52
- package/src/auto-mode/policy.ts +0 -146
- package/src/auto-mode/prompt.ts +0 -279
- package/src/auto-mode/reviewer.ts +0 -325
- package/src/auto-mode/tool-review-tui.ts +0 -211
- package/src/auto-mode/transcript.ts +0 -135
- package/src/auto-mode/types.ts +0 -137
|
@@ -1,282 +1,282 @@
|
|
|
1
|
-
import type { ResponsesCompatibleRequestPayload } from "./runtime";
|
|
2
|
-
import { isDeferredToolCarryover, type DeferredToolCarryoverV1 } from "./types";
|
|
3
|
-
import { LOCAL_WEB_SEARCH_TOOL_NAME, WEB_RUN_TOOL_NAME } from "./web-search/types";
|
|
4
|
-
|
|
5
|
-
type JsonObject = Record<string, unknown>;
|
|
6
|
-
|
|
7
|
-
type DeferredToolCompat = {
|
|
8
|
-
supportsAdditionalTools?: boolean;
|
|
9
|
-
supportsToolSearch?: boolean;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export type DeferredToolCarryoverMode = "additional-tools" | "tool-search";
|
|
13
|
-
|
|
14
|
-
export type DeferredToolCarryoverRewrite = {
|
|
15
|
-
payload: ResponsesCompatibleRequestPayload;
|
|
16
|
-
changed: boolean;
|
|
17
|
-
mode?: DeferredToolCarryoverMode;
|
|
18
|
-
movedToolNames: string[];
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
function isRecord(value: unknown): value is JsonObject {
|
|
22
|
-
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function getDeferredToolName(value: unknown): string | undefined {
|
|
26
|
-
if (!isRecord(value) || (value.type !== "function" && value.type !== "custom")) {
|
|
27
|
-
return undefined;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return typeof value.name === "string" && value.name.trim().length > 0 ? value.name : undefined;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function hasOptionalBooleanField(value: JsonObject, field: string): boolean {
|
|
34
|
-
return value[field] === undefined || typeof value[field] === "boolean";
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function hasOptionalBooleanOrNullField(value: JsonObject, field: string): boolean {
|
|
38
|
-
return value[field] === undefined || value[field] === null || typeof value[field] === "boolean";
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function isCustomToolFormat(value: unknown): boolean {
|
|
42
|
-
if (!isRecord(value)) return false;
|
|
43
|
-
if (value.type === "text") return true;
|
|
44
|
-
return (
|
|
45
|
-
value.type === "grammar" &&
|
|
46
|
-
(value.syntax === "lark" || value.syntax === "regex") &&
|
|
47
|
-
typeof value.definition === "string"
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/** Validate only the provider-facing function/custom shapes emitted by official Pi. */
|
|
52
|
-
function isDeferredToolSchema(value: unknown): value is JsonObject {
|
|
53
|
-
const name = getDeferredToolName(value);
|
|
54
|
-
if (!name || !isRecord(value)) return false;
|
|
55
|
-
|
|
56
|
-
if (value.type === "function") {
|
|
57
|
-
return (
|
|
58
|
-
"parameters" in value &&
|
|
59
|
-
(value.parameters === null || isRecord(value.parameters)) &&
|
|
60
|
-
hasOptionalBooleanOrNullField(value, "strict") &&
|
|
61
|
-
hasOptionalBooleanField(value, "defer_loading") &&
|
|
62
|
-
(value.description === undefined || value.description === null || typeof value.description === "string")
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return (
|
|
67
|
-
hasOptionalBooleanField(value, "defer_loading") &&
|
|
68
|
-
(value.description === undefined || typeof value.description === "string") &&
|
|
69
|
-
(value.format === undefined || isCustomToolFormat(value.format))
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function addLoadedToolSchemas(tools: unknown, loaded: Set<string>): boolean {
|
|
74
|
-
if (!Array.isArray(tools)) return false;
|
|
75
|
-
for (const tool of tools) {
|
|
76
|
-
const name = getDeferredToolName(tool);
|
|
77
|
-
if (!name || !isDeferredToolSchema(tool)) return false;
|
|
78
|
-
loaded.add(name);
|
|
79
|
-
}
|
|
80
|
-
return true;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function isCompletedClientToolSearchCall(value: JsonObject): value is JsonObject & { call_id: string } {
|
|
84
|
-
if (
|
|
85
|
-
value.type !== "tool_search_call" ||
|
|
86
|
-
typeof value.call_id !== "string" ||
|
|
87
|
-
value.call_id.length === 0 ||
|
|
88
|
-
value.execution !== "client" ||
|
|
89
|
-
value.status !== "completed" ||
|
|
90
|
-
!isRecord(value.arguments)
|
|
91
|
-
) {
|
|
92
|
-
return false;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return (
|
|
96
|
-
typeof value.arguments.query === "string" &&
|
|
97
|
-
typeof value.arguments.limit === "number" &&
|
|
98
|
-
Number.isInteger(value.arguments.limit) &&
|
|
99
|
-
value.arguments.limit >= 0
|
|
100
|
-
);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function collectLoadedToolNames(input: readonly unknown[], startIndex: number): Set<string> | undefined {
|
|
104
|
-
const loaded = new Set<string>();
|
|
105
|
-
|
|
106
|
-
for (let index = startIndex; index < input.length; index += 1) {
|
|
107
|
-
const item = input[index];
|
|
108
|
-
if (!isRecord(item)) continue;
|
|
109
|
-
|
|
110
|
-
if (item.type === "additional_tools") {
|
|
111
|
-
if (item.role !== "developer" || !addLoadedToolSchemas(item.tools, loaded)) return undefined;
|
|
112
|
-
continue;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (item.type === "tool_search_call") {
|
|
116
|
-
if (!isCompletedClientToolSearchCall(item)) return undefined;
|
|
117
|
-
const output = input[index + 1];
|
|
118
|
-
if (
|
|
119
|
-
!isRecord(output) ||
|
|
120
|
-
output.type !== "tool_search_output" ||
|
|
121
|
-
output.call_id !== item.call_id ||
|
|
122
|
-
output.execution !== "client" ||
|
|
123
|
-
output.status !== "completed" ||
|
|
124
|
-
!addLoadedToolSchemas(output.tools, loaded)
|
|
125
|
-
) {
|
|
126
|
-
return undefined;
|
|
127
|
-
}
|
|
128
|
-
index += 1;
|
|
129
|
-
continue;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if (item.type === "tool_search_output") return undefined;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
return loaded;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function createToolSearchCallId(compactionEntryId: string, names: readonly string[]): string {
|
|
139
|
-
let hash = 2166136261;
|
|
140
|
-
for (const value of `${compactionEntryId}\u0000${names.join("\u0000")}`) {
|
|
141
|
-
hash ^= value.codePointAt(0) ?? 0;
|
|
142
|
-
hash = Math.imul(hash, 16777619);
|
|
143
|
-
}
|
|
144
|
-
return `pi_tool_load_${(hash >>> 0).toString(16).padStart(8, "0")}`;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
function cloneTool(value: JsonObject): JsonObject | undefined {
|
|
148
|
-
try {
|
|
149
|
-
return structuredClone(value);
|
|
150
|
-
} catch {
|
|
151
|
-
return undefined;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
function selectMode(compat: DeferredToolCompat | undefined): DeferredToolCarryoverMode | undefined {
|
|
156
|
-
if (compat?.supportsAdditionalTools === true) return "additional-tools";
|
|
157
|
-
if (compat?.supportsToolSearch === true) return "tool-search";
|
|
158
|
-
return undefined;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
function createLoadPoint(
|
|
162
|
-
mode: DeferredToolCarryoverMode,
|
|
163
|
-
compactionEntryId: string,
|
|
164
|
-
schemas: readonly JsonObject[],
|
|
165
|
-
): JsonObject[] | undefined {
|
|
166
|
-
const clonedSchemas: JsonObject[] = [];
|
|
167
|
-
for (const schema of schemas) {
|
|
168
|
-
const clone = cloneTool(schema);
|
|
169
|
-
if (!clone) return undefined;
|
|
170
|
-
if (mode === "tool-search") clone.defer_loading = true;
|
|
171
|
-
clonedSchemas.push(clone);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
if (mode === "additional-tools") {
|
|
175
|
-
return [{ type: "additional_tools", role: "developer", tools: clonedSchemas }];
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const names = clonedSchemas.map((schema) => getDeferredToolName(schema));
|
|
179
|
-
if (names.some((name): name is undefined => name === undefined)) return undefined;
|
|
180
|
-
const callId = createToolSearchCallId(compactionEntryId, names as string[]);
|
|
181
|
-
return [
|
|
182
|
-
{
|
|
183
|
-
type: "tool_search_call",
|
|
184
|
-
call_id: callId,
|
|
185
|
-
execution: "client",
|
|
186
|
-
status: "completed",
|
|
187
|
-
arguments: { query: (names as string[]).join(" "), limit: names.length },
|
|
188
|
-
},
|
|
189
|
-
{
|
|
190
|
-
type: "tool_search_output",
|
|
191
|
-
call_id: callId,
|
|
192
|
-
execution: "client",
|
|
193
|
-
status: "completed",
|
|
194
|
-
tools: clonedSchemas,
|
|
195
|
-
},
|
|
196
|
-
];
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Restore cache-stack's activated function/custom schemas after an opaque
|
|
201
|
-
* compaction checkpoint. This is deliberately fail-open: malformed state or
|
|
202
|
-
* payloads leave the provider request untouched so Pi keeps its immediate-tool
|
|
203
|
-
* fallback behavior.
|
|
204
|
-
*/
|
|
205
|
-
export function rewritePayloadWithDeferredToolCarryover(args: {
|
|
206
|
-
payload: ResponsesCompatibleRequestPayload;
|
|
207
|
-
carryover?: DeferredToolCarryoverV1;
|
|
208
|
-
compactionEntryId: string;
|
|
209
|
-
checkpointEndIndex: number;
|
|
210
|
-
compat?: DeferredToolCompat;
|
|
211
|
-
}): DeferredToolCarryoverRewrite {
|
|
212
|
-
const unchanged: DeferredToolCarryoverRewrite = {
|
|
213
|
-
payload: args.payload,
|
|
214
|
-
changed: false,
|
|
215
|
-
movedToolNames: [],
|
|
216
|
-
};
|
|
217
|
-
|
|
218
|
-
const mode = selectMode(args.compat);
|
|
219
|
-
if (!mode || !args.carryover || !isDeferredToolCarryover(args.carryover) || !Array.isArray(args.payload.input)) {
|
|
220
|
-
return unchanged;
|
|
221
|
-
}
|
|
222
|
-
if (!Number.isInteger(args.checkpointEndIndex) || args.checkpointEndIndex < 0 || args.checkpointEndIndex > args.payload.input.length) {
|
|
223
|
-
return unchanged;
|
|
224
|
-
}
|
|
225
|
-
if (!Array.isArray(args.payload.tools)) return unchanged;
|
|
226
|
-
|
|
227
|
-
// Search routing needs its local callable at the top level on every request.
|
|
228
|
-
const carryoverNames = new Set(
|
|
229
|
-
args.carryover.toolNames
|
|
230
|
-
.map((name) => name.trim())
|
|
231
|
-
.filter((name) => name.length > 0 && name !== LOCAL_WEB_SEARCH_TOOL_NAME && name !== WEB_RUN_TOOL_NAME),
|
|
232
|
-
);
|
|
233
|
-
if (carryoverNames.size === 0) return unchanged;
|
|
234
|
-
|
|
235
|
-
const schemasByName = new Map<string, JsonObject>();
|
|
236
|
-
const duplicateNames = new Set<string>();
|
|
237
|
-
for (const tool of args.payload.tools) {
|
|
238
|
-
const name = getDeferredToolName(tool);
|
|
239
|
-
if (!name || !carryoverNames.has(name)) continue;
|
|
240
|
-
if (!isDeferredToolSchema(tool)) return unchanged;
|
|
241
|
-
const schema = tool;
|
|
242
|
-
if (schemasByName.has(name)) {
|
|
243
|
-
duplicateNames.add(name);
|
|
244
|
-
continue;
|
|
245
|
-
}
|
|
246
|
-
schemasByName.set(name, schema);
|
|
247
|
-
}
|
|
248
|
-
for (const name of duplicateNames) schemasByName.delete(name);
|
|
249
|
-
if (schemasByName.size === 0) return unchanged;
|
|
250
|
-
|
|
251
|
-
const loadedNames = collectLoadedToolNames(args.payload.input, args.checkpointEndIndex);
|
|
252
|
-
if (!loadedNames) return unchanged;
|
|
253
|
-
const matchingNames = [...schemasByName.keys()];
|
|
254
|
-
const movedNames = matchingNames.filter((name) => !loadedNames.has(name));
|
|
255
|
-
const matchingNameSet = new Set(matchingNames);
|
|
256
|
-
const nextTools = args.payload.tools.filter((tool) => {
|
|
257
|
-
const name = getDeferredToolName(tool);
|
|
258
|
-
return !name || !matchingNameSet.has(name);
|
|
259
|
-
});
|
|
260
|
-
const toolsChanged = nextTools.length !== args.payload.tools.length;
|
|
261
|
-
if (movedNames.length === 0 && !toolsChanged) return unchanged;
|
|
262
|
-
|
|
263
|
-
const nextInput = [...args.payload.input];
|
|
264
|
-
if (movedNames.length > 0) {
|
|
265
|
-
const schemas = movedNames.map((name) => schemasByName.get(name));
|
|
266
|
-
if (schemas.some((schema): schema is undefined => schema === undefined)) return unchanged;
|
|
267
|
-
const loadPoint = createLoadPoint(mode, args.compactionEntryId, schemas as JsonObject[]);
|
|
268
|
-
if (!loadPoint) return unchanged;
|
|
269
|
-
nextInput.splice(args.checkpointEndIndex, 0, ...loadPoint);
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
return {
|
|
273
|
-
payload: {
|
|
274
|
-
...args.payload,
|
|
275
|
-
tools: nextTools,
|
|
276
|
-
input: nextInput,
|
|
277
|
-
},
|
|
278
|
-
changed: true,
|
|
279
|
-
mode,
|
|
280
|
-
movedToolNames: movedNames,
|
|
281
|
-
};
|
|
282
|
-
}
|
|
1
|
+
import type { ResponsesCompatibleRequestPayload } from "./runtime";
|
|
2
|
+
import { isDeferredToolCarryover, type DeferredToolCarryoverV1 } from "./types";
|
|
3
|
+
import { LOCAL_WEB_SEARCH_TOOL_NAME, WEB_RUN_TOOL_NAME } from "./web-search/types";
|
|
4
|
+
|
|
5
|
+
type JsonObject = Record<string, unknown>;
|
|
6
|
+
|
|
7
|
+
type DeferredToolCompat = {
|
|
8
|
+
supportsAdditionalTools?: boolean;
|
|
9
|
+
supportsToolSearch?: boolean;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type DeferredToolCarryoverMode = "additional-tools" | "tool-search";
|
|
13
|
+
|
|
14
|
+
export type DeferredToolCarryoverRewrite = {
|
|
15
|
+
payload: ResponsesCompatibleRequestPayload;
|
|
16
|
+
changed: boolean;
|
|
17
|
+
mode?: DeferredToolCarryoverMode;
|
|
18
|
+
movedToolNames: string[];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
function isRecord(value: unknown): value is JsonObject {
|
|
22
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function getDeferredToolName(value: unknown): string | undefined {
|
|
26
|
+
if (!isRecord(value) || (value.type !== "function" && value.type !== "custom")) {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return typeof value.name === "string" && value.name.trim().length > 0 ? value.name : undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function hasOptionalBooleanField(value: JsonObject, field: string): boolean {
|
|
34
|
+
return value[field] === undefined || typeof value[field] === "boolean";
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function hasOptionalBooleanOrNullField(value: JsonObject, field: string): boolean {
|
|
38
|
+
return value[field] === undefined || value[field] === null || typeof value[field] === "boolean";
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function isCustomToolFormat(value: unknown): boolean {
|
|
42
|
+
if (!isRecord(value)) return false;
|
|
43
|
+
if (value.type === "text") return true;
|
|
44
|
+
return (
|
|
45
|
+
value.type === "grammar" &&
|
|
46
|
+
(value.syntax === "lark" || value.syntax === "regex") &&
|
|
47
|
+
typeof value.definition === "string"
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Validate only the provider-facing function/custom shapes emitted by official Pi. */
|
|
52
|
+
function isDeferredToolSchema(value: unknown): value is JsonObject {
|
|
53
|
+
const name = getDeferredToolName(value);
|
|
54
|
+
if (!name || !isRecord(value)) return false;
|
|
55
|
+
|
|
56
|
+
if (value.type === "function") {
|
|
57
|
+
return (
|
|
58
|
+
"parameters" in value &&
|
|
59
|
+
(value.parameters === null || isRecord(value.parameters)) &&
|
|
60
|
+
hasOptionalBooleanOrNullField(value, "strict") &&
|
|
61
|
+
hasOptionalBooleanField(value, "defer_loading") &&
|
|
62
|
+
(value.description === undefined || value.description === null || typeof value.description === "string")
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
hasOptionalBooleanField(value, "defer_loading") &&
|
|
68
|
+
(value.description === undefined || typeof value.description === "string") &&
|
|
69
|
+
(value.format === undefined || isCustomToolFormat(value.format))
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function addLoadedToolSchemas(tools: unknown, loaded: Set<string>): boolean {
|
|
74
|
+
if (!Array.isArray(tools)) return false;
|
|
75
|
+
for (const tool of tools) {
|
|
76
|
+
const name = getDeferredToolName(tool);
|
|
77
|
+
if (!name || !isDeferredToolSchema(tool)) return false;
|
|
78
|
+
loaded.add(name);
|
|
79
|
+
}
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function isCompletedClientToolSearchCall(value: JsonObject): value is JsonObject & { call_id: string } {
|
|
84
|
+
if (
|
|
85
|
+
value.type !== "tool_search_call" ||
|
|
86
|
+
typeof value.call_id !== "string" ||
|
|
87
|
+
value.call_id.length === 0 ||
|
|
88
|
+
value.execution !== "client" ||
|
|
89
|
+
value.status !== "completed" ||
|
|
90
|
+
!isRecord(value.arguments)
|
|
91
|
+
) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return (
|
|
96
|
+
typeof value.arguments.query === "string" &&
|
|
97
|
+
typeof value.arguments.limit === "number" &&
|
|
98
|
+
Number.isInteger(value.arguments.limit) &&
|
|
99
|
+
value.arguments.limit >= 0
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function collectLoadedToolNames(input: readonly unknown[], startIndex: number): Set<string> | undefined {
|
|
104
|
+
const loaded = new Set<string>();
|
|
105
|
+
|
|
106
|
+
for (let index = startIndex; index < input.length; index += 1) {
|
|
107
|
+
const item = input[index];
|
|
108
|
+
if (!isRecord(item)) continue;
|
|
109
|
+
|
|
110
|
+
if (item.type === "additional_tools") {
|
|
111
|
+
if (item.role !== "developer" || !addLoadedToolSchemas(item.tools, loaded)) return undefined;
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (item.type === "tool_search_call") {
|
|
116
|
+
if (!isCompletedClientToolSearchCall(item)) return undefined;
|
|
117
|
+
const output = input[index + 1];
|
|
118
|
+
if (
|
|
119
|
+
!isRecord(output) ||
|
|
120
|
+
output.type !== "tool_search_output" ||
|
|
121
|
+
output.call_id !== item.call_id ||
|
|
122
|
+
output.execution !== "client" ||
|
|
123
|
+
output.status !== "completed" ||
|
|
124
|
+
!addLoadedToolSchemas(output.tools, loaded)
|
|
125
|
+
) {
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
index += 1;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (item.type === "tool_search_output") return undefined;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return loaded;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function createToolSearchCallId(compactionEntryId: string, names: readonly string[]): string {
|
|
139
|
+
let hash = 2166136261;
|
|
140
|
+
for (const value of `${compactionEntryId}\u0000${names.join("\u0000")}`) {
|
|
141
|
+
hash ^= value.codePointAt(0) ?? 0;
|
|
142
|
+
hash = Math.imul(hash, 16777619);
|
|
143
|
+
}
|
|
144
|
+
return `pi_tool_load_${(hash >>> 0).toString(16).padStart(8, "0")}`;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function cloneTool(value: JsonObject): JsonObject | undefined {
|
|
148
|
+
try {
|
|
149
|
+
return structuredClone(value);
|
|
150
|
+
} catch {
|
|
151
|
+
return undefined;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function selectMode(compat: DeferredToolCompat | undefined): DeferredToolCarryoverMode | undefined {
|
|
156
|
+
if (compat?.supportsAdditionalTools === true) return "additional-tools";
|
|
157
|
+
if (compat?.supportsToolSearch === true) return "tool-search";
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function createLoadPoint(
|
|
162
|
+
mode: DeferredToolCarryoverMode,
|
|
163
|
+
compactionEntryId: string,
|
|
164
|
+
schemas: readonly JsonObject[],
|
|
165
|
+
): JsonObject[] | undefined {
|
|
166
|
+
const clonedSchemas: JsonObject[] = [];
|
|
167
|
+
for (const schema of schemas) {
|
|
168
|
+
const clone = cloneTool(schema);
|
|
169
|
+
if (!clone) return undefined;
|
|
170
|
+
if (mode === "tool-search") clone.defer_loading = true;
|
|
171
|
+
clonedSchemas.push(clone);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (mode === "additional-tools") {
|
|
175
|
+
return [{ type: "additional_tools", role: "developer", tools: clonedSchemas }];
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const names = clonedSchemas.map((schema) => getDeferredToolName(schema));
|
|
179
|
+
if (names.some((name): name is undefined => name === undefined)) return undefined;
|
|
180
|
+
const callId = createToolSearchCallId(compactionEntryId, names as string[]);
|
|
181
|
+
return [
|
|
182
|
+
{
|
|
183
|
+
type: "tool_search_call",
|
|
184
|
+
call_id: callId,
|
|
185
|
+
execution: "client",
|
|
186
|
+
status: "completed",
|
|
187
|
+
arguments: { query: (names as string[]).join(" "), limit: names.length },
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
type: "tool_search_output",
|
|
191
|
+
call_id: callId,
|
|
192
|
+
execution: "client",
|
|
193
|
+
status: "completed",
|
|
194
|
+
tools: clonedSchemas,
|
|
195
|
+
},
|
|
196
|
+
];
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Restore cache-stack's activated function/custom schemas after an opaque
|
|
201
|
+
* compaction checkpoint. This is deliberately fail-open: malformed state or
|
|
202
|
+
* payloads leave the provider request untouched so Pi keeps its immediate-tool
|
|
203
|
+
* fallback behavior.
|
|
204
|
+
*/
|
|
205
|
+
export function rewritePayloadWithDeferredToolCarryover(args: {
|
|
206
|
+
payload: ResponsesCompatibleRequestPayload;
|
|
207
|
+
carryover?: DeferredToolCarryoverV1;
|
|
208
|
+
compactionEntryId: string;
|
|
209
|
+
checkpointEndIndex: number;
|
|
210
|
+
compat?: DeferredToolCompat;
|
|
211
|
+
}): DeferredToolCarryoverRewrite {
|
|
212
|
+
const unchanged: DeferredToolCarryoverRewrite = {
|
|
213
|
+
payload: args.payload,
|
|
214
|
+
changed: false,
|
|
215
|
+
movedToolNames: [],
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const mode = selectMode(args.compat);
|
|
219
|
+
if (!mode || !args.carryover || !isDeferredToolCarryover(args.carryover) || !Array.isArray(args.payload.input)) {
|
|
220
|
+
return unchanged;
|
|
221
|
+
}
|
|
222
|
+
if (!Number.isInteger(args.checkpointEndIndex) || args.checkpointEndIndex < 0 || args.checkpointEndIndex > args.payload.input.length) {
|
|
223
|
+
return unchanged;
|
|
224
|
+
}
|
|
225
|
+
if (!Array.isArray(args.payload.tools)) return unchanged;
|
|
226
|
+
|
|
227
|
+
// Search routing needs its local callable at the top level on every request.
|
|
228
|
+
const carryoverNames = new Set(
|
|
229
|
+
args.carryover.toolNames
|
|
230
|
+
.map((name) => name.trim())
|
|
231
|
+
.filter((name) => name.length > 0 && name !== LOCAL_WEB_SEARCH_TOOL_NAME && name !== WEB_RUN_TOOL_NAME),
|
|
232
|
+
);
|
|
233
|
+
if (carryoverNames.size === 0) return unchanged;
|
|
234
|
+
|
|
235
|
+
const schemasByName = new Map<string, JsonObject>();
|
|
236
|
+
const duplicateNames = new Set<string>();
|
|
237
|
+
for (const tool of args.payload.tools) {
|
|
238
|
+
const name = getDeferredToolName(tool);
|
|
239
|
+
if (!name || !carryoverNames.has(name)) continue;
|
|
240
|
+
if (!isDeferredToolSchema(tool)) return unchanged;
|
|
241
|
+
const schema = tool;
|
|
242
|
+
if (schemasByName.has(name)) {
|
|
243
|
+
duplicateNames.add(name);
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
schemasByName.set(name, schema);
|
|
247
|
+
}
|
|
248
|
+
for (const name of duplicateNames) schemasByName.delete(name);
|
|
249
|
+
if (schemasByName.size === 0) return unchanged;
|
|
250
|
+
|
|
251
|
+
const loadedNames = collectLoadedToolNames(args.payload.input, args.checkpointEndIndex);
|
|
252
|
+
if (!loadedNames) return unchanged;
|
|
253
|
+
const matchingNames = [...schemasByName.keys()];
|
|
254
|
+
const movedNames = matchingNames.filter((name) => !loadedNames.has(name));
|
|
255
|
+
const matchingNameSet = new Set(matchingNames);
|
|
256
|
+
const nextTools = args.payload.tools.filter((tool) => {
|
|
257
|
+
const name = getDeferredToolName(tool);
|
|
258
|
+
return !name || !matchingNameSet.has(name);
|
|
259
|
+
});
|
|
260
|
+
const toolsChanged = nextTools.length !== args.payload.tools.length;
|
|
261
|
+
if (movedNames.length === 0 && !toolsChanged) return unchanged;
|
|
262
|
+
|
|
263
|
+
const nextInput = [...args.payload.input];
|
|
264
|
+
if (movedNames.length > 0) {
|
|
265
|
+
const schemas = movedNames.map((name) => schemasByName.get(name));
|
|
266
|
+
if (schemas.some((schema): schema is undefined => schema === undefined)) return unchanged;
|
|
267
|
+
const loadPoint = createLoadPoint(mode, args.compactionEntryId, schemas as JsonObject[]);
|
|
268
|
+
if (!loadPoint) return unchanged;
|
|
269
|
+
nextInput.splice(args.checkpointEndIndex, 0, ...loadPoint);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return {
|
|
273
|
+
payload: {
|
|
274
|
+
...args.payload,
|
|
275
|
+
tools: nextTools,
|
|
276
|
+
input: nextInput,
|
|
277
|
+
},
|
|
278
|
+
changed: true,
|
|
279
|
+
mode,
|
|
280
|
+
movedToolNames: movedNames,
|
|
281
|
+
};
|
|
282
|
+
}
|