@kodelyth/codex 2026.5.40 → 2026.5.42
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/dist/client-ChMX13_o.js +642 -0
- package/dist/client-factory-D3dIsp4Y.js +9 -0
- package/dist/command-formatters-BRW7_Nu7.js +519 -0
- package/dist/command-handlers-P2IqtXaZ.js +1462 -0
- package/dist/compact-baos5flR.js +329 -0
- package/dist/computer-use-VfLvTMaa.js +367 -0
- package/dist/config-CezENx_E.js +510 -0
- package/dist/doctor-contract-api.js +53 -0
- package/dist/harness.js +51 -0
- package/dist/index.js +1133 -0
- package/dist/media-understanding-provider.js +335 -0
- package/dist/models-B9DhrIwD.js +110 -0
- package/dist/node-cli-sessions-De4_DuFw.js +1216 -0
- package/dist/plugin-activation-BlMuJeXz.js +452 -0
- package/dist/prompt-overlay.js +12 -0
- package/dist/protocol-C9UWI98H.js +9 -0
- package/dist/protocol-validators-BGBspNmF.js +5988 -0
- package/dist/provider-catalog.js +84 -0
- package/dist/provider-discovery.js +33 -0
- package/dist/provider.js +150 -0
- package/dist/rate-limit-cache-CHuacE27.js +24 -0
- package/dist/request-CTQKUxaa.js +89 -0
- package/dist/rolldown-runtime-DUslC3ob.js +14 -0
- package/dist/run-attempt-DqV2OU1R.js +5366 -0
- package/dist/session-binding-3PzU7ZTW.js +222 -0
- package/dist/shared-client-Cnyr9dyT.js +631 -0
- package/dist/side-question-CP5XlA0U.js +667 -0
- package/dist/test-api.js +45 -0
- package/dist/thread-lifecycle-DBJetBuV.js +1561 -0
- package/dist/vision-tools-Cl_5a93K.js +1379 -0
- package/doctor-contract-api.test.ts +44 -0
- package/doctor-contract-api.ts +68 -0
- package/harness.ts +72 -0
- package/index.test.ts +230 -0
- package/index.ts +66 -0
- package/klaw.plugin.json +24 -85
- package/media-understanding-provider.test.ts +486 -0
- package/media-understanding-provider.ts +521 -0
- package/package.json +3 -3
- package/prompt-overlay-runtime-contract.test.ts +48 -0
- package/prompt-overlay.ts +21 -0
- package/provider-catalog.ts +83 -0
- package/provider-discovery.ts +45 -0
- package/provider.test.ts +384 -0
- package/provider.ts +243 -0
- package/src/app-server/app-inventory-cache.test.ts +176 -0
- package/src/app-server/app-inventory-cache.ts +324 -0
- package/src/app-server/approval-bridge.test.ts +1471 -0
- package/src/app-server/approval-bridge.ts +1211 -0
- package/src/app-server/auth-bridge.test.ts +1449 -0
- package/src/app-server/auth-bridge.ts +614 -0
- package/src/app-server/auth-profile-runtime-contract.test.ts +239 -0
- package/src/app-server/capabilities.ts +27 -0
- package/src/app-server/client-factory.ts +24 -0
- package/src/app-server/client.test.ts +563 -0
- package/src/app-server/client.ts +715 -0
- package/src/app-server/compact.test.ts +710 -0
- package/src/app-server/compact.ts +500 -0
- package/src/app-server/computer-use.test.ts +788 -0
- package/src/app-server/computer-use.ts +683 -0
- package/src/app-server/config.test.ts +879 -0
- package/src/app-server/config.ts +1038 -0
- package/src/app-server/context-engine-projection.test.ts +252 -0
- package/src/app-server/context-engine-projection.ts +403 -0
- package/src/app-server/delivery-no-reply-runtime-contract.test.ts +80 -0
- package/src/app-server/dynamic-tool-diagnostics.ts +73 -0
- package/src/app-server/dynamic-tool-profile.ts +69 -0
- package/src/app-server/dynamic-tools.test.ts +1302 -0
- package/src/app-server/dynamic-tools.ts +623 -0
- package/src/app-server/elicitation-bridge.test.ts +1056 -0
- package/src/app-server/elicitation-bridge.ts +783 -0
- package/src/app-server/event-projector.test.ts +2668 -0
- package/src/app-server/event-projector.ts +2057 -0
- package/src/app-server/image-payload-sanitizer.test.ts +49 -0
- package/src/app-server/image-payload-sanitizer.ts +167 -0
- package/src/app-server/klaw-owned-tool-runtime-contract.test.ts +456 -0
- package/src/app-server/local-runtime-attribution.ts +39 -0
- package/src/app-server/managed-binary.test.ts +139 -0
- package/src/app-server/managed-binary.ts +193 -0
- package/src/app-server/models.test.ts +246 -0
- package/src/app-server/models.ts +172 -0
- package/src/app-server/native-hook-relay.test.ts +271 -0
- package/src/app-server/native-hook-relay.ts +150 -0
- package/src/app-server/native-subagent-task-mirror.test.ts +573 -0
- package/src/app-server/native-subagent-task-mirror.ts +497 -0
- package/src/app-server/outcome-fallback-runtime-contract.test.ts +404 -0
- package/src/app-server/plugin-activation.test.ts +336 -0
- package/src/app-server/plugin-activation.ts +283 -0
- package/src/app-server/plugin-app-cache-key.ts +74 -0
- package/src/app-server/plugin-approval-roundtrip.ts +122 -0
- package/src/app-server/plugin-inventory.test.ts +355 -0
- package/src/app-server/plugin-inventory.ts +357 -0
- package/src/app-server/plugin-thread-config.test.ts +865 -0
- package/src/app-server/plugin-thread-config.ts +455 -0
- package/src/app-server/protocol-generated/json/DynamicToolCallParams.json +33 -0
- package/src/app-server/protocol-generated/json/v2/ErrorNotification.json +199 -0
- package/src/app-server/protocol-generated/json/v2/GetAccountResponse.json +102 -0
- package/src/app-server/protocol-generated/json/v2/ModelListResponse.json +227 -0
- package/src/app-server/protocol-generated/json/v2/ThreadResumeResponse.json +2630 -0
- package/src/app-server/protocol-generated/json/v2/ThreadStartResponse.json +2630 -0
- package/src/app-server/protocol-generated/json/v2/TurnCompletedNotification.json +1659 -0
- package/src/app-server/protocol-generated/json/v2/TurnStartResponse.json +1655 -0
- package/src/app-server/protocol-validators.test.ts +75 -0
- package/src/app-server/protocol-validators.ts +203 -0
- package/src/app-server/protocol.ts +520 -0
- package/src/app-server/rate-limit-cache.ts +48 -0
- package/src/app-server/rate-limits.test.ts +202 -0
- package/src/app-server/rate-limits.ts +583 -0
- package/src/app-server/request.ts +73 -0
- package/src/app-server/run-attempt.context-engine.test.ts +1004 -0
- package/src/app-server/run-attempt.test.ts +9477 -0
- package/src/app-server/run-attempt.ts +4683 -0
- package/src/app-server/run-attempt.vision-tools.test.ts +35 -0
- package/src/app-server/schema-normalization-runtime-contract.test.ts +206 -0
- package/src/app-server/session-binding.test.ts +303 -0
- package/src/app-server/session-binding.ts +398 -0
- package/src/app-server/session-history.ts +44 -0
- package/src/app-server/shared-client.test.ts +589 -0
- package/src/app-server/shared-client.ts +289 -0
- package/src/app-server/side-question.test.ts +1175 -0
- package/src/app-server/side-question.ts +1007 -0
- package/src/app-server/test-support.ts +48 -0
- package/src/app-server/thread-lifecycle.test.ts +447 -0
- package/src/app-server/thread-lifecycle.ts +939 -0
- package/src/app-server/thread-lifecycle.user-mcp-servers.test.ts +442 -0
- package/src/app-server/timeout.ts +9 -0
- package/src/app-server/tool-progress-normalization.ts +77 -0
- package/src/app-server/trajectory.test.ts +205 -0
- package/src/app-server/trajectory.ts +365 -0
- package/src/app-server/transcript-mirror.test.ts +524 -0
- package/src/app-server/transcript-mirror.ts +208 -0
- package/src/app-server/transcript-repair-runtime-contract.test.ts +44 -0
- package/src/app-server/transport-stdio.test.ts +171 -0
- package/src/app-server/transport-stdio.ts +107 -0
- package/src/app-server/transport-websocket.test.ts +69 -0
- package/src/app-server/transport-websocket.ts +90 -0
- package/src/app-server/transport.ts +117 -0
- package/src/app-server/user-input-bridge.test.ts +249 -0
- package/src/app-server/user-input-bridge.ts +316 -0
- package/src/app-server/version.ts +4 -0
- package/src/app-server/vision-tools.ts +12 -0
- package/src/command-account.ts +544 -0
- package/src/command-formatters.ts +425 -0
- package/src/command-handlers.ts +2004 -0
- package/src/command-rpc.test.ts +16 -0
- package/src/command-rpc.ts +142 -0
- package/src/commands.test.ts +3312 -0
- package/src/commands.ts +65 -0
- package/src/conversation-binding-data.ts +124 -0
- package/src/conversation-binding.test.ts +599 -0
- package/src/conversation-binding.ts +561 -0
- package/src/conversation-control.test.ts +126 -0
- package/src/conversation-control.ts +303 -0
- package/src/conversation-turn-collector.test.ts +191 -0
- package/src/conversation-turn-collector.ts +186 -0
- package/src/conversation-turn-input.test.ts +141 -0
- package/src/conversation-turn-input.ts +106 -0
- package/src/manifest.test.ts +20 -0
- package/src/migration/apply.ts +501 -0
- package/src/migration/helpers.ts +55 -0
- package/src/migration/plan.ts +461 -0
- package/src/migration/provider.test.ts +1741 -0
- package/src/migration/provider.ts +41 -0
- package/src/migration/source.ts +643 -0
- package/src/migration/targets.ts +25 -0
- package/src/node-cli-sessions.test.ts +180 -0
- package/src/node-cli-sessions.ts +711 -0
- package/test-api.ts +82 -0
- package/tsconfig.json +16 -0
- package/doctor-contract-api.js +0 -7
- package/harness.js +0 -7
- package/index.js +0 -7
- package/media-understanding-provider.js +0 -7
- package/prompt-overlay.js +0 -7
- package/provider-catalog.js +0 -7
- package/provider-discovery.js +0 -7
- package/provider.js +0 -7
- package/test-api.js +0 -7
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
invalidInlineImageText,
|
|
4
|
+
sanitizeCodexHistoryImagePayloads,
|
|
5
|
+
sanitizeInlineImageDataUrl,
|
|
6
|
+
} from "./image-payload-sanitizer.js";
|
|
7
|
+
|
|
8
|
+
const PNG_1X1 =
|
|
9
|
+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR4nGNgYAAAAAMAASsJTYQAAAAASUVORK5CYII=";
|
|
10
|
+
|
|
11
|
+
describe("Codex app-server image payload sanitizer", () => {
|
|
12
|
+
it("drops malformed data URL image payloads", () => {
|
|
13
|
+
expect(sanitizeInlineImageDataUrl("data:image/jpeg;base64,not base64!")).toBeUndefined();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("canonicalizes valid data URL images with sniffed MIME type", () => {
|
|
17
|
+
expect(sanitizeInlineImageDataUrl(`data:image/jpeg;base64,\n${PNG_1X1}`)).toBe(
|
|
18
|
+
`data:image/png;base64,${PNG_1X1}`,
|
|
19
|
+
);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("formats the text replacement used for invalid images", () => {
|
|
23
|
+
expect(invalidInlineImageText("codex user input")).toContain("invalid inline image data");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("scrubs invalid image blocks from mirrored history values", () => {
|
|
27
|
+
expect(
|
|
28
|
+
sanitizeCodexHistoryImagePayloads(
|
|
29
|
+
[
|
|
30
|
+
{
|
|
31
|
+
role: "toolResult",
|
|
32
|
+
content: [{ type: "image", mimeType: "image/jpeg", data: "not base64!" }],
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
"codex mirrored history",
|
|
36
|
+
),
|
|
37
|
+
).toEqual([
|
|
38
|
+
{
|
|
39
|
+
role: "toolResult",
|
|
40
|
+
content: [
|
|
41
|
+
{
|
|
42
|
+
type: "text",
|
|
43
|
+
text: "[codex mirrored history] omitted image payload: invalid inline image data",
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
},
|
|
47
|
+
]);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
const DATA_URL_PREFIX = "data:";
|
|
2
|
+
const IMAGE_OMITTED_TEXT = "omitted image payload: invalid inline image data";
|
|
3
|
+
|
|
4
|
+
function startsWithDataUrl(value: string): boolean {
|
|
5
|
+
return value.slice(0, DATA_URL_PREFIX.length).toLowerCase() === DATA_URL_PREFIX;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function canonicalizeBase64(base64: string): string | undefined {
|
|
9
|
+
let cleaned = "";
|
|
10
|
+
let padding = 0;
|
|
11
|
+
let sawPadding = false;
|
|
12
|
+
for (let i = 0; i < base64.length; i += 1) {
|
|
13
|
+
const code = base64.charCodeAt(i);
|
|
14
|
+
if (code <= 0x20) {
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
if (code === 0x3d) {
|
|
18
|
+
padding += 1;
|
|
19
|
+
if (padding > 2) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
sawPadding = true;
|
|
23
|
+
cleaned += "=";
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
const isBase64DataChar =
|
|
27
|
+
(code >= 0x41 && code <= 0x5a) ||
|
|
28
|
+
(code >= 0x61 && code <= 0x7a) ||
|
|
29
|
+
(code >= 0x30 && code <= 0x39) ||
|
|
30
|
+
code === 0x2b ||
|
|
31
|
+
code === 0x2f;
|
|
32
|
+
if (sawPadding || !isBase64DataChar) {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
cleaned += base64[i];
|
|
36
|
+
}
|
|
37
|
+
if (!cleaned || cleaned.length % 4 !== 0) {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
return cleaned;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function sniffImageMime(buffer: Buffer): string | undefined {
|
|
44
|
+
if (
|
|
45
|
+
buffer.length >= 8 &&
|
|
46
|
+
buffer[0] === 0x89 &&
|
|
47
|
+
buffer[1] === 0x50 &&
|
|
48
|
+
buffer[2] === 0x4e &&
|
|
49
|
+
buffer[3] === 0x47 &&
|
|
50
|
+
buffer[4] === 0x0d &&
|
|
51
|
+
buffer[5] === 0x0a &&
|
|
52
|
+
buffer[6] === 0x1a &&
|
|
53
|
+
buffer[7] === 0x0a
|
|
54
|
+
) {
|
|
55
|
+
return "image/png";
|
|
56
|
+
}
|
|
57
|
+
if (buffer.length >= 3 && buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff) {
|
|
58
|
+
return "image/jpeg";
|
|
59
|
+
}
|
|
60
|
+
if (
|
|
61
|
+
buffer.length >= 12 &&
|
|
62
|
+
buffer.subarray(0, 4).toString("ascii") === "RIFF" &&
|
|
63
|
+
buffer.subarray(8, 12).toString("ascii") === "WEBP"
|
|
64
|
+
) {
|
|
65
|
+
return "image/webp";
|
|
66
|
+
}
|
|
67
|
+
if (
|
|
68
|
+
buffer.length >= 6 &&
|
|
69
|
+
(buffer.subarray(0, 6).toString("ascii") === "GIF87a" ||
|
|
70
|
+
buffer.subarray(0, 6).toString("ascii") === "GIF89a")
|
|
71
|
+
) {
|
|
72
|
+
return "image/gif";
|
|
73
|
+
}
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function sanitizeInlineImageDataUrl(imageUrl: string): string | undefined {
|
|
78
|
+
if (!startsWithDataUrl(imageUrl)) {
|
|
79
|
+
return imageUrl;
|
|
80
|
+
}
|
|
81
|
+
const commaIndex = imageUrl.indexOf(",");
|
|
82
|
+
if (commaIndex < 0) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const metadata = imageUrl.slice(DATA_URL_PREFIX.length, commaIndex);
|
|
87
|
+
const payload = imageUrl.slice(commaIndex + 1);
|
|
88
|
+
const metadataParts = metadata.split(";").map((part) => part.trim());
|
|
89
|
+
const declaredMimeType = metadataParts[0]?.toLowerCase();
|
|
90
|
+
if (!declaredMimeType?.startsWith("image/")) {
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
if (!metadataParts.slice(1).some((part) => part.toLowerCase() === "base64")) {
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const canonicalPayload = canonicalizeBase64(payload);
|
|
98
|
+
if (!canonicalPayload) {
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
const sniffedMimeType = sniffImageMime(Buffer.from(canonicalPayload, "base64"));
|
|
102
|
+
if (!sniffedMimeType) {
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
return `data:${sniffedMimeType};base64,${canonicalPayload}`;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function invalidInlineImageText(label: string): string {
|
|
109
|
+
return `[${label}] ${IMAGE_OMITTED_TEXT}`;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
113
|
+
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function sanitizeImageContentRecord(
|
|
117
|
+
record: Record<string, unknown>,
|
|
118
|
+
label: string,
|
|
119
|
+
): Record<string, unknown> | undefined {
|
|
120
|
+
if (record.type === "image" && typeof record.data === "string") {
|
|
121
|
+
const mimeType = typeof record.mimeType === "string" ? record.mimeType : "image/png";
|
|
122
|
+
const imageUrl = sanitizeInlineImageDataUrl(`data:${mimeType};base64,${record.data}`);
|
|
123
|
+
if (!imageUrl) {
|
|
124
|
+
return { type: "text", text: invalidInlineImageText(label) };
|
|
125
|
+
}
|
|
126
|
+
const commaIndex = imageUrl.indexOf(",");
|
|
127
|
+
const metadata = imageUrl.slice(DATA_URL_PREFIX.length, commaIndex);
|
|
128
|
+
const mime = metadata.split(";")[0] ?? mimeType;
|
|
129
|
+
return { ...record, mimeType: mime, data: imageUrl.slice(commaIndex + 1) };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (record.type === "inputImage" && typeof record.imageUrl === "string") {
|
|
133
|
+
const imageUrl = sanitizeInlineImageDataUrl(record.imageUrl);
|
|
134
|
+
return imageUrl
|
|
135
|
+
? { ...record, imageUrl }
|
|
136
|
+
: { type: "inputText", text: invalidInlineImageText(label) };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (record.type === "input_image" && typeof record.image_url === "string") {
|
|
140
|
+
const imageUrl = sanitizeInlineImageDataUrl(record.image_url);
|
|
141
|
+
return imageUrl
|
|
142
|
+
? { ...record, image_url: imageUrl }
|
|
143
|
+
: { type: "input_text", text: invalidInlineImageText(label) };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function sanitizeCodexHistoryImagePayloads<T>(value: T, label: string): T {
|
|
150
|
+
if (Array.isArray(value)) {
|
|
151
|
+
return value.map((entry) => sanitizeCodexHistoryImagePayloads(entry, label)) as T;
|
|
152
|
+
}
|
|
153
|
+
if (!isRecord(value)) {
|
|
154
|
+
return value;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const imageRecord = sanitizeImageContentRecord(value, label);
|
|
158
|
+
if (imageRecord) {
|
|
159
|
+
return imageRecord as T;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const next: Record<string, unknown> = {};
|
|
163
|
+
for (const [key, child] of Object.entries(value)) {
|
|
164
|
+
next[key] = sanitizeCodexHistoryImagePayloads(child, label);
|
|
165
|
+
}
|
|
166
|
+
return next as T;
|
|
167
|
+
}
|
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
import type { AnyAgentTool } from "klaw/plugin-sdk/agent-harness";
|
|
2
|
+
import { wrapToolWithBeforeToolCallHook } from "klaw/plugin-sdk/agent-harness-runtime";
|
|
3
|
+
import {
|
|
4
|
+
installCodexToolResultMiddleware,
|
|
5
|
+
installKlawOwnedToolHooks,
|
|
6
|
+
mediaToolResult,
|
|
7
|
+
resetKlawOwnedToolHooks,
|
|
8
|
+
textToolResult,
|
|
9
|
+
} from "klaw/plugin-sdk/agent-runtime-test-contracts";
|
|
10
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
11
|
+
import { createCodexDynamicToolBridge } from "./dynamic-tools.js";
|
|
12
|
+
|
|
13
|
+
function createContractTool(overrides: Partial<AnyAgentTool>): AnyAgentTool {
|
|
14
|
+
return {
|
|
15
|
+
name: "exec",
|
|
16
|
+
description: "Run a command.",
|
|
17
|
+
parameters: { type: "object", properties: {} },
|
|
18
|
+
execute: vi.fn(),
|
|
19
|
+
...overrides,
|
|
20
|
+
} as unknown as AnyAgentTool;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function requireRecord(value: unknown, label: string): Record<string, unknown> {
|
|
24
|
+
if (typeof value !== "object" || value === null) {
|
|
25
|
+
throw new Error(`${label} was not an object`);
|
|
26
|
+
}
|
|
27
|
+
return value as Record<string, unknown>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function expectRecordFields(record: Record<string, unknown>, fields: Record<string, unknown>) {
|
|
31
|
+
for (const [key, value] of Object.entries(fields)) {
|
|
32
|
+
expect(record[key]).toEqual(value);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function requireMockCall(mock: unknown, index: number, label: string): unknown[] {
|
|
37
|
+
const calls = (mock as { mock?: { calls?: unknown[][] } }).mock?.calls;
|
|
38
|
+
expect(Array.isArray(calls)).toBe(true);
|
|
39
|
+
if (!Array.isArray(calls)) {
|
|
40
|
+
throw new Error(`${label} did not expose mock calls`);
|
|
41
|
+
}
|
|
42
|
+
const call = calls[index];
|
|
43
|
+
if (!call) {
|
|
44
|
+
throw new Error(`missing ${label} call ${index + 1}`);
|
|
45
|
+
}
|
|
46
|
+
return call;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function expectHookContext(value: unknown, fields: Record<string, unknown>) {
|
|
50
|
+
expectRecordFields(requireRecord(value, "hook context"), fields);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function expectExecuteCall(execute: unknown, callId: string, params: Record<string, unknown>) {
|
|
54
|
+
const call = requireMockCall(execute, 0, "execute");
|
|
55
|
+
expect(call[0]).toBe(callId);
|
|
56
|
+
expect(call[1]).toEqual(params);
|
|
57
|
+
expect(call[2]).toBeInstanceOf(AbortSignal);
|
|
58
|
+
expect(call[3]).toBeUndefined();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function expectBeforeToolCall(
|
|
62
|
+
hooks: { beforeToolCall: unknown },
|
|
63
|
+
eventFields: Record<string, unknown>,
|
|
64
|
+
contextFields: Record<string, unknown>,
|
|
65
|
+
) {
|
|
66
|
+
const call = requireMockCall(hooks.beforeToolCall, 0, "before_tool_call");
|
|
67
|
+
expectRecordFields(requireRecord(call[0], "before_tool_call event"), eventFields);
|
|
68
|
+
expectHookContext(call[1], contextFields);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function expectAfterToolCall(
|
|
72
|
+
hooks: { afterToolCall: unknown },
|
|
73
|
+
eventFields: Record<string, unknown>,
|
|
74
|
+
contextFields: Record<string, unknown>,
|
|
75
|
+
) {
|
|
76
|
+
const call = requireMockCall(hooks.afterToolCall, 0, "after_tool_call");
|
|
77
|
+
expectRecordFields(requireRecord(call[0], "after_tool_call event"), eventFields);
|
|
78
|
+
expectHookContext(call[1], contextFields);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
describe("Klaw-owned tool runtime contract — Codex app-server adapter", () => {
|
|
82
|
+
afterEach(() => {
|
|
83
|
+
resetKlawOwnedToolHooks();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("wraps unwrapped dynamic tools with before/after tool hooks", async () => {
|
|
87
|
+
const adjustedParams = { mode: "safe" };
|
|
88
|
+
const mergedParams = { command: "pwd", mode: "safe" };
|
|
89
|
+
const hooks = installKlawOwnedToolHooks({ adjustedParams });
|
|
90
|
+
const execute = vi.fn(async () => textToolResult("done", { ok: true }));
|
|
91
|
+
const bridge = createCodexDynamicToolBridge({
|
|
92
|
+
tools: [createContractTool({ name: "exec", execute })],
|
|
93
|
+
signal: new AbortController().signal,
|
|
94
|
+
hookContext: {
|
|
95
|
+
agentId: "agent-1",
|
|
96
|
+
sessionId: "session-1",
|
|
97
|
+
sessionKey: "agent:agent-1:session-1",
|
|
98
|
+
runId: "run-contract",
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
const result = await bridge.handleToolCall({
|
|
103
|
+
threadId: "thread-1",
|
|
104
|
+
turnId: "turn-1",
|
|
105
|
+
callId: "call-contract",
|
|
106
|
+
namespace: null,
|
|
107
|
+
tool: "exec",
|
|
108
|
+
arguments: { command: "pwd" },
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
expect(result).toEqual({
|
|
112
|
+
success: true,
|
|
113
|
+
contentItems: [{ type: "inputText", text: "done" }],
|
|
114
|
+
});
|
|
115
|
+
expectBeforeToolCall(
|
|
116
|
+
hooks,
|
|
117
|
+
{
|
|
118
|
+
toolName: "exec",
|
|
119
|
+
toolCallId: "call-contract",
|
|
120
|
+
runId: "run-contract",
|
|
121
|
+
params: { command: "pwd" },
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
agentId: "agent-1",
|
|
125
|
+
sessionId: "session-1",
|
|
126
|
+
sessionKey: "agent:agent-1:session-1",
|
|
127
|
+
runId: "run-contract",
|
|
128
|
+
toolCallId: "call-contract",
|
|
129
|
+
},
|
|
130
|
+
);
|
|
131
|
+
expectExecuteCall(execute, "call-contract", mergedParams);
|
|
132
|
+
await vi.waitFor(() => {
|
|
133
|
+
const call = requireMockCall(hooks.afterToolCall, 0, "after_tool_call");
|
|
134
|
+
const event = requireRecord(call[0], "after_tool_call event");
|
|
135
|
+
expectRecordFields(event, {
|
|
136
|
+
toolName: "exec",
|
|
137
|
+
toolCallId: "call-contract",
|
|
138
|
+
params: mergedParams,
|
|
139
|
+
});
|
|
140
|
+
expectRecordFields(requireRecord(event.result, "after_tool_call result"), {
|
|
141
|
+
content: [{ type: "text", text: "done" }],
|
|
142
|
+
details: { ok: true },
|
|
143
|
+
});
|
|
144
|
+
expectHookContext(call[1], {
|
|
145
|
+
agentId: "agent-1",
|
|
146
|
+
sessionId: "session-1",
|
|
147
|
+
sessionKey: "agent:agent-1:session-1",
|
|
148
|
+
runId: "run-contract",
|
|
149
|
+
toolCallId: "call-contract",
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("runs tool_result middleware before after_tool_call observes the result", async () => {
|
|
155
|
+
const adjustedParams = { mode: "safe" };
|
|
156
|
+
const mergedParams = { command: "status", mode: "safe" };
|
|
157
|
+
const hooks = installKlawOwnedToolHooks({ adjustedParams });
|
|
158
|
+
const middleware = installCodexToolResultMiddleware((event) => {
|
|
159
|
+
const eventRecord = requireRecord(event, "tool_result middleware event");
|
|
160
|
+
expectRecordFields(eventRecord, {
|
|
161
|
+
toolName: "exec",
|
|
162
|
+
toolCallId: "call-middleware",
|
|
163
|
+
args: { command: "status" },
|
|
164
|
+
});
|
|
165
|
+
expectRecordFields(requireRecord(eventRecord.result, "tool_result middleware result"), {
|
|
166
|
+
content: [{ type: "text", text: "raw output" }],
|
|
167
|
+
details: { stage: "execute" },
|
|
168
|
+
});
|
|
169
|
+
return textToolResult("compacted output", { stage: "middleware" });
|
|
170
|
+
});
|
|
171
|
+
const execute = vi.fn(async () => textToolResult("raw output", { stage: "execute" }));
|
|
172
|
+
const bridge = createCodexDynamicToolBridge({
|
|
173
|
+
tools: [createContractTool({ name: "exec", execute })],
|
|
174
|
+
signal: new AbortController().signal,
|
|
175
|
+
hookContext: {
|
|
176
|
+
agentId: "agent-1",
|
|
177
|
+
sessionId: "session-1",
|
|
178
|
+
sessionKey: "agent:agent-1:session-1",
|
|
179
|
+
runId: "run-middleware",
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
const result = await bridge.handleToolCall({
|
|
184
|
+
threadId: "thread-1",
|
|
185
|
+
turnId: "turn-1",
|
|
186
|
+
callId: "call-middleware",
|
|
187
|
+
namespace: null,
|
|
188
|
+
tool: "exec",
|
|
189
|
+
arguments: { command: "status" },
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
expect(result).toEqual({
|
|
193
|
+
success: true,
|
|
194
|
+
contentItems: [{ type: "inputText", text: "compacted output" }],
|
|
195
|
+
});
|
|
196
|
+
expectExecuteCall(execute, "call-middleware", mergedParams);
|
|
197
|
+
expect(middleware.middleware).toHaveBeenCalledTimes(1);
|
|
198
|
+
await vi.waitFor(() => {
|
|
199
|
+
const call = requireMockCall(hooks.afterToolCall, 0, "after_tool_call");
|
|
200
|
+
const event = requireRecord(call[0], "after_tool_call event");
|
|
201
|
+
expectRecordFields(event, {
|
|
202
|
+
toolName: "exec",
|
|
203
|
+
toolCallId: "call-middleware",
|
|
204
|
+
params: mergedParams,
|
|
205
|
+
});
|
|
206
|
+
expectRecordFields(requireRecord(event.result, "after_tool_call result"), {
|
|
207
|
+
content: [{ type: "text", text: "compacted output" }],
|
|
208
|
+
details: { stage: "middleware" },
|
|
209
|
+
});
|
|
210
|
+
expectHookContext(call[1], {
|
|
211
|
+
runId: "run-middleware",
|
|
212
|
+
toolCallId: "call-middleware",
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("fails closed when before_tool_call blocks a dynamic tool", async () => {
|
|
218
|
+
const hooks = installKlawOwnedToolHooks({ blockReason: "blocked by policy" });
|
|
219
|
+
const execute = vi.fn(async () => textToolResult("should not run"));
|
|
220
|
+
const bridge = createCodexDynamicToolBridge({
|
|
221
|
+
tools: [createContractTool({ name: "message", execute })],
|
|
222
|
+
signal: new AbortController().signal,
|
|
223
|
+
hookContext: { runId: "run-blocked" },
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
const result = await bridge.handleToolCall({
|
|
227
|
+
threadId: "thread-1",
|
|
228
|
+
turnId: "turn-1",
|
|
229
|
+
callId: "call-blocked",
|
|
230
|
+
namespace: null,
|
|
231
|
+
tool: "message",
|
|
232
|
+
arguments: {
|
|
233
|
+
action: "send",
|
|
234
|
+
text: "blocked",
|
|
235
|
+
provider: "telegram",
|
|
236
|
+
to: "chat-1",
|
|
237
|
+
},
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
expect(result).toEqual({
|
|
241
|
+
success: false,
|
|
242
|
+
contentItems: [{ type: "inputText", text: "blocked by policy" }],
|
|
243
|
+
});
|
|
244
|
+
expect(execute).not.toHaveBeenCalled();
|
|
245
|
+
expect(bridge.telemetry.didSendViaMessagingTool).toBe(false);
|
|
246
|
+
await vi.waitFor(() => {
|
|
247
|
+
const call = requireMockCall(hooks.afterToolCall, 0, "after_tool_call");
|
|
248
|
+
const event = requireRecord(call[0], "after_tool_call event");
|
|
249
|
+
expectRecordFields(event, {
|
|
250
|
+
toolName: "message",
|
|
251
|
+
toolCallId: "call-blocked",
|
|
252
|
+
params: {
|
|
253
|
+
action: "send",
|
|
254
|
+
text: "blocked",
|
|
255
|
+
provider: "telegram",
|
|
256
|
+
to: "chat-1",
|
|
257
|
+
},
|
|
258
|
+
});
|
|
259
|
+
expectRecordFields(requireRecord(event.result, "after_tool_call result"), {
|
|
260
|
+
content: [{ type: "text", text: "blocked by policy" }],
|
|
261
|
+
details: {
|
|
262
|
+
status: "blocked",
|
|
263
|
+
deniedReason: "plugin-before-tool-call",
|
|
264
|
+
reason: "blocked by policy",
|
|
265
|
+
},
|
|
266
|
+
});
|
|
267
|
+
expectHookContext(call[1], {
|
|
268
|
+
runId: "run-blocked",
|
|
269
|
+
toolCallId: "call-blocked",
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it("reports dynamic tool execution errors through after_tool_call", async () => {
|
|
275
|
+
const adjustedParams = { timeoutSec: 1 };
|
|
276
|
+
const mergedParams = { command: "false", timeoutSec: 1 };
|
|
277
|
+
const hooks = installKlawOwnedToolHooks({ adjustedParams });
|
|
278
|
+
const execute = vi.fn(async () => {
|
|
279
|
+
throw new Error("tool failed");
|
|
280
|
+
});
|
|
281
|
+
const bridge = createCodexDynamicToolBridge({
|
|
282
|
+
tools: [createContractTool({ name: "exec", execute })],
|
|
283
|
+
signal: new AbortController().signal,
|
|
284
|
+
hookContext: { runId: "run-error" },
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
const result = await bridge.handleToolCall({
|
|
288
|
+
threadId: "thread-1",
|
|
289
|
+
turnId: "turn-1",
|
|
290
|
+
callId: "call-error",
|
|
291
|
+
namespace: null,
|
|
292
|
+
tool: "exec",
|
|
293
|
+
arguments: { command: "false" },
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
expect(result).toEqual({
|
|
297
|
+
success: false,
|
|
298
|
+
contentItems: [{ type: "inputText", text: "tool failed" }],
|
|
299
|
+
});
|
|
300
|
+
expectExecuteCall(execute, "call-error", mergedParams);
|
|
301
|
+
await vi.waitFor(() => {
|
|
302
|
+
expectAfterToolCall(
|
|
303
|
+
hooks,
|
|
304
|
+
{
|
|
305
|
+
toolName: "exec",
|
|
306
|
+
toolCallId: "call-error",
|
|
307
|
+
params: mergedParams,
|
|
308
|
+
error: "tool failed",
|
|
309
|
+
},
|
|
310
|
+
{
|
|
311
|
+
runId: "run-error",
|
|
312
|
+
toolCallId: "call-error",
|
|
313
|
+
},
|
|
314
|
+
);
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it("records successful Codex messaging text, media, and target telemetry", async () => {
|
|
319
|
+
const hooks = installKlawOwnedToolHooks();
|
|
320
|
+
const execute = vi.fn(async () => textToolResult("Sent."));
|
|
321
|
+
const bridge = createCodexDynamicToolBridge({
|
|
322
|
+
tools: [createContractTool({ name: "message", execute })],
|
|
323
|
+
signal: new AbortController().signal,
|
|
324
|
+
hookContext: { runId: "run-message" },
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
const result = await bridge.handleToolCall({
|
|
328
|
+
threadId: "thread-1",
|
|
329
|
+
turnId: "turn-1",
|
|
330
|
+
callId: "call-message",
|
|
331
|
+
namespace: null,
|
|
332
|
+
tool: "message",
|
|
333
|
+
arguments: {
|
|
334
|
+
action: "send",
|
|
335
|
+
text: "hello from Codex",
|
|
336
|
+
mediaUrl: "/tmp/codex-reply.png",
|
|
337
|
+
provider: "telegram",
|
|
338
|
+
to: "chat-1",
|
|
339
|
+
threadId: "thread-ts-1",
|
|
340
|
+
},
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
expect(result).toEqual({
|
|
344
|
+
success: true,
|
|
345
|
+
contentItems: [{ type: "inputText", text: "Sent." }],
|
|
346
|
+
});
|
|
347
|
+
expectRecordFields(requireRecord(bridge.telemetry, "bridge telemetry"), {
|
|
348
|
+
didSendViaMessagingTool: true,
|
|
349
|
+
messagingToolSentTexts: ["hello from Codex"],
|
|
350
|
+
messagingToolSentMediaUrls: ["/tmp/codex-reply.png"],
|
|
351
|
+
});
|
|
352
|
+
expect(bridge.telemetry.messagingToolSentTargets).toEqual([
|
|
353
|
+
{
|
|
354
|
+
tool: "message",
|
|
355
|
+
provider: "telegram",
|
|
356
|
+
to: "chat-1",
|
|
357
|
+
threadId: "thread-ts-1",
|
|
358
|
+
text: "hello from Codex",
|
|
359
|
+
mediaUrls: ["/tmp/codex-reply.png"],
|
|
360
|
+
},
|
|
361
|
+
]);
|
|
362
|
+
await vi.waitFor(() => {
|
|
363
|
+
const call = requireMockCall(hooks.afterToolCall, 0, "after_tool_call");
|
|
364
|
+
const event = requireRecord(call[0], "after_tool_call event");
|
|
365
|
+
expectRecordFields(event, {
|
|
366
|
+
toolName: "message",
|
|
367
|
+
toolCallId: "call-message",
|
|
368
|
+
});
|
|
369
|
+
expectRecordFields(requireRecord(event.params, "after_tool_call params"), {
|
|
370
|
+
text: "hello from Codex",
|
|
371
|
+
mediaUrl: "/tmp/codex-reply.png",
|
|
372
|
+
});
|
|
373
|
+
expectHookContext(call[1], {
|
|
374
|
+
runId: "run-message",
|
|
375
|
+
toolCallId: "call-message",
|
|
376
|
+
});
|
|
377
|
+
});
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
it("records successful Codex media artifacts from tool results", async () => {
|
|
381
|
+
const hooks = installKlawOwnedToolHooks();
|
|
382
|
+
const execute = vi.fn(async () =>
|
|
383
|
+
mediaToolResult("Generated media reply.", "/tmp/reply.opus", true),
|
|
384
|
+
);
|
|
385
|
+
const bridge = createCodexDynamicToolBridge({
|
|
386
|
+
tools: [createContractTool({ name: "tts", execute })],
|
|
387
|
+
signal: new AbortController().signal,
|
|
388
|
+
hookContext: { runId: "run-media" },
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
const result = await bridge.handleToolCall({
|
|
392
|
+
threadId: "thread-1",
|
|
393
|
+
turnId: "turn-1",
|
|
394
|
+
callId: "call-media",
|
|
395
|
+
namespace: null,
|
|
396
|
+
tool: "tts",
|
|
397
|
+
arguments: { text: "hello" },
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
expect(result).toEqual({
|
|
401
|
+
success: true,
|
|
402
|
+
contentItems: [{ type: "inputText", text: "Generated media reply." }],
|
|
403
|
+
});
|
|
404
|
+
expect(bridge.telemetry.toolMediaUrls).toEqual(["/tmp/reply.opus"]);
|
|
405
|
+
expect(bridge.telemetry.toolAudioAsVoice).toBe(true);
|
|
406
|
+
await vi.waitFor(() => {
|
|
407
|
+
const call = requireMockCall(hooks.afterToolCall, 0, "after_tool_call");
|
|
408
|
+
const event = requireRecord(call[0], "after_tool_call event");
|
|
409
|
+
expectRecordFields(event, {
|
|
410
|
+
toolName: "tts",
|
|
411
|
+
toolCallId: "call-media",
|
|
412
|
+
});
|
|
413
|
+
const resultRecord = requireRecord(event.result, "after_tool_call result");
|
|
414
|
+
const details = requireRecord(resultRecord.details, "after_tool_call result details");
|
|
415
|
+
expectRecordFields(requireRecord(details.media, "media result details"), {
|
|
416
|
+
mediaUrl: "/tmp/reply.opus",
|
|
417
|
+
audioAsVoice: true,
|
|
418
|
+
});
|
|
419
|
+
expectHookContext(call[1], {
|
|
420
|
+
runId: "run-media",
|
|
421
|
+
toolCallId: "call-media",
|
|
422
|
+
});
|
|
423
|
+
});
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
it("does not double-wrap dynamic tools that already have before_tool_call", async () => {
|
|
427
|
+
const adjustedParams = { mode: "safe" };
|
|
428
|
+
const mergedParams = { command: "pwd", mode: "safe" };
|
|
429
|
+
const hooks = installKlawOwnedToolHooks({ adjustedParams });
|
|
430
|
+
const execute = vi.fn(async () => textToolResult("done"));
|
|
431
|
+
const tool = wrapToolWithBeforeToolCallHook(createContractTool({ name: "exec", execute }), {
|
|
432
|
+
runId: "run-wrapped",
|
|
433
|
+
});
|
|
434
|
+
const bridge = createCodexDynamicToolBridge({
|
|
435
|
+
tools: [tool],
|
|
436
|
+
signal: new AbortController().signal,
|
|
437
|
+
hookContext: { runId: "run-wrapped" },
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
const result = await bridge.handleToolCall({
|
|
441
|
+
threadId: "thread-1",
|
|
442
|
+
turnId: "turn-1",
|
|
443
|
+
callId: "call-wrapped",
|
|
444
|
+
namespace: null,
|
|
445
|
+
tool: "exec",
|
|
446
|
+
arguments: { command: "pwd" },
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
expect(result).toEqual({
|
|
450
|
+
success: true,
|
|
451
|
+
contentItems: [{ type: "inputText", text: "done" }],
|
|
452
|
+
});
|
|
453
|
+
expect(hooks.beforeToolCall).toHaveBeenCalledTimes(1);
|
|
454
|
+
expectExecuteCall(execute, "call-wrapped", mergedParams);
|
|
455
|
+
});
|
|
456
|
+
});
|