forked from pwrdrvr/openclaw-codex-app-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpending-input.test.ts
More file actions
365 lines (335 loc) · 11.7 KB
/
pending-input.test.ts
File metadata and controls
365 lines (335 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import { describe, expect, it } from "vitest";
import {
buildPendingQuestionnaireResponse,
buildPendingPromptText,
buildPendingUserInputActions,
createPendingInputState,
formatPendingQuestionnairePrompt,
parseCodexUserInput,
parsePendingQuestionnaire,
questionnaireCurrentQuestionHasAnswer,
questionnaireIsComplete,
requestToken,
stripShellLauncher,
} from "./pending-input.js";
describe("pending-input helpers", () => {
it("parses numeric option replies", () => {
expect(parseCodexUserInput("2", 3)).toEqual({ kind: "option", index: 1 });
expect(parseCodexUserInput("option 1", 3)).toEqual({ kind: "option", index: 0 });
expect(parseCodexUserInput("hello", 3)).toEqual({ kind: "text", text: "hello" });
});
it("builds approval actions from request decisions", () => {
const actions = buildPendingUserInputActions({
method: "turn/requestApproval",
requestParams: {
availableDecisions: ["accept", "acceptForSession", "decline", "cancel"],
},
});
expect(actions.map((action) => action.label)).toEqual([
"Approve Once",
"Approve for Session",
"Decline",
"Cancel",
"Tell Codex What To Do",
]);
});
it("defaults file change approvals to approve and decline actions", () => {
const actions = buildPendingUserInputActions({
method: "item/fileChange/requestApproval",
requestParams: {
threadId: "019cd368-7eda-7863-86ba-6586598bc5a3",
turnId: "turn-1",
itemId: "item-1",
},
});
expect(actions.map((action) => action.label)).toEqual([
"Approve File Changes",
"Decline",
"Tell Codex What To Do",
]);
});
it("does not treat ids as shell commands for file change approvals", () => {
const text = buildPendingPromptText({
method: "item/fileChange/requestApproval",
requestId: "req-file-1",
requestParams: {
threadId: "019cd368-7eda-7863-86ba-6586598bc5a3",
turnId: "turn-1",
itemId: "item-1",
reason: "Codex wants to apply the proposed patch.",
},
options: [],
actions: buildPendingUserInputActions({
method: "item/fileChange/requestApproval",
requestParams: {
threadId: "019cd368-7eda-7863-86ba-6586598bc5a3",
turnId: "turn-1",
itemId: "item-1",
reason: "Codex wants to apply the proposed patch.",
},
}),
expiresAt: Date.now() + 60_000,
});
expect(text).toContain("Codex file change approval requested");
expect(text).toContain("Codex wants to apply the proposed patch.");
expect(text).not.toContain("Command:");
expect(text).not.toContain("019cd368-7eda-7863-86ba-6586598bc5a3");
});
it("lists changed files for file change approvals", () => {
const text = buildPendingPromptText({
method: "item/fileChange/requestApproval",
requestId: "req-file-2",
requestParams: {
threadId: "thread-1",
turnId: "turn-1",
itemId: "item-1",
filePaths: ["src/app.ts", "README.md", "/tmp/outside.txt"],
},
options: [],
actions: buildPendingUserInputActions({
method: "item/fileChange/requestApproval",
requestParams: {
threadId: "thread-1",
turnId: "turn-1",
itemId: "item-1",
},
}),
expiresAt: Date.now() + 60_000,
});
expect(text).toContain("Files:");
expect(text).toContain("`src/app.ts`");
expect(text).toContain("`README.md`");
expect(text).toContain("`/tmp/outside.txt`");
});
it("includes writable-root context for file change approvals that request broader access", () => {
const text = buildPendingPromptText({
method: "item/fileChange/requestApproval",
requestId: "req-file-3",
requestParams: {
threadId: "thread-1",
turnId: "turn-1",
itemId: "item-1",
reason: "Codex needs write access outside the workspace.",
grantRoot: "/Users/huntharo/github/codex",
},
options: [],
actions: buildPendingUserInputActions({
method: "item/fileChange/requestApproval",
requestParams: {
threadId: "thread-1",
turnId: "turn-1",
itemId: "item-1",
},
}),
expiresAt: Date.now() + 60_000,
});
expect(text).toContain("Codex file change approval requested");
expect(text).toContain("Codex needs write access outside the workspace.");
expect(text).toContain("Requested writable root: `/Users/huntharo/github/codex`");
expect(text).not.toContain("Command:");
});
it("uses explicit approval decision labels when command approvals provide them", () => {
const actions = buildPendingUserInputActions({
method: "item/commandExecution/requestApproval",
requestParams: {
availableDecisions: [
{ decision: "accept", label: "Allow once" },
{
decision: "acceptForSession",
label: "Always allow `pnpm typecheck`",
proposedExecpolicyAmendment: { prefix: ["pnpm", "typecheck"] },
},
{ decision: "decline", label: "Deny" },
],
},
});
expect(actions.map((action) => action.label)).toEqual([
"Allow once",
"Always allow `pnpm typecheck`",
"Deny",
"Tell Codex What To Do",
]);
});
it("creates a stable request token", () => {
expect(requestToken("abc")).toBe(requestToken("abc"));
expect(requestToken("abc")).not.toBe(requestToken("def"));
});
it("creates a prompt text for pending input", () => {
const state = createPendingInputState({
method: "item/tool/requestUserInput",
requestId: "req-1",
requestParams: {
question: "Pick one",
},
options: ["A", "B"],
expiresAt: Date.now() + 60_000,
});
expect(state.promptText).toContain("Codex input requested");
expect(state.promptText).toContain("Choices:");
});
it("truncates oversized pending request prompts for chat delivery", () => {
const text = buildPendingPromptText({
method: "item/tool/requestUserInput",
requestId: "req-2",
requestParams: {
prompt: "A".repeat(5000),
},
options: ["A", "B"],
actions: [],
expiresAt: Date.now() + 60_000,
});
expect(text.length).toBeLessThan(2400);
expect(text).toContain("[Request details truncated.");
});
it("parses multi-question plan prompts into a questionnaire state", () => {
const questionnaire = parsePendingQuestionnaire(`
1. What do you want the final artifact to be?
• A Single static binary
• B Normal runtime-managed CLI
Guidance:
• A points toward Go or Rust.
2. What do you care about more: delivery speed or long-term rigor?
• A Fastest rewrite
• B Balanced
`);
expect(questionnaire?.questions).toHaveLength(2);
expect(questionnaire?.questions[0]).toMatchObject({
id: "q1",
prompt: "What do you want the final artifact to be?",
options: [
{ key: "A", label: "Single static binary" },
{ key: "B", label: "Normal runtime-managed CLI" },
],
});
expect(formatPendingQuestionnairePrompt(questionnaire!)).toContain("Codex plan question 1 of 2");
});
it("renders a compact questionnaire reply once all answers are filled in", () => {
const questionnaire = parsePendingQuestionnaire(`
1. What do you want the final artifact to be?
• A Single static binary
• B Normal runtime-managed CLI
2. What do you care about more?
• A Fastest rewrite
• B Balanced
`)!;
questionnaire.answers[0] = {
kind: "option",
optionKey: "A",
optionLabel: "Single static binary",
};
questionnaire.answers[1] = {
kind: "text",
text: "Balanced, but only if we keep the migration simple.",
};
expect(questionnaireIsComplete(questionnaire)).toBe(true);
expect(buildPendingQuestionnaireResponse(questionnaire)).toBe(
"1A 2: Balanced, but only if we keep the migration simple.",
);
});
it("requires an answer before advancing to the next questionnaire question", () => {
const questionnaire = parsePendingQuestionnaire(`
1. What do you want the final artifact to be?
• A Single static binary
• B Normal runtime-managed CLI
2. What do you care about more?
• A Fastest rewrite
• B Balanced
`)!;
expect(questionnaireCurrentQuestionHasAnswer(questionnaire)).toBe(false);
questionnaire.answers[0] = {
kind: "option",
optionKey: "A",
optionLabel: "Single static binary",
};
expect(questionnaireCurrentQuestionHasAnswer(questionnaire)).toBe(true);
});
it("strips shell launcher wrappers from commands for display", () => {
expect(stripShellLauncher("/bin/zsh -lc 'git status'")).toBe("git status");
expect(stripShellLauncher("/bin/bash -lc 'npm install'")).toBe("npm install");
expect(stripShellLauncher("bash -lc 'make build'")).toBe("make build");
expect(
stripShellLauncher('zsh -lc \'git add README.md && git commit -m "docs: update"\''),
).toBe('git add README.md && git commit -m "docs: update"');
expect(stripShellLauncher("/usr/bin/zsh -lc 'cargo test'")).toBe("cargo test");
// Non-launcher commands pass through unchanged
expect(stripShellLauncher("git status")).toBe("git status");
expect(stripShellLauncher("npm install")).toBe("npm install");
});
it("parses structured request_user_input questions into questionnaire state", () => {
const state = createPendingInputState({
method: "item/tool/requestUserInput",
requestId: "req-3",
requestParams: {
questions: [
{
id: "runtime",
header: "Runtime",
question: "Which runtime shape should we optimize for?",
isOther: true,
options: [
{
label: "Long-lived service (Recommended)",
description: "Best fit for stateful flows.",
},
{
label: "Mostly serverless",
description: "Best fit for stateless handlers.",
},
],
},
{
id: "db",
header: "DB",
question: "What kind of database migration do you want from SQLite?",
options: [{ label: "Postgres (Recommended)" }, { label: "Firestore" }],
},
],
},
options: [],
expiresAt: Date.now() + 60_000,
});
expect(state.questionnaire?.questions).toHaveLength(2);
expect(state.questionnaire?.questions[0]).toMatchObject({
id: "runtime",
header: "Runtime",
prompt: "Which runtime shape should we optimize for?",
allowFreeform: true,
options: [
{
key: "A",
label: "Long-lived service (Recommended)",
description: "Best fit for stateful flows.",
recommended: true,
},
{
key: "B",
label: "Mostly serverless",
description: "Best fit for stateless handlers.",
recommended: false,
},
],
});
expect(formatPendingQuestionnairePrompt(state.questionnaire!)).toContain(
"Runtime: Which runtime shape should we optimize for?",
);
expect(formatPendingQuestionnairePrompt(state.questionnaire!)).toContain(
"Other: You can reply with free text.",
);
state.questionnaire!.answers[0] = {
kind: "option",
optionKey: "A",
optionLabel: "Long-lived service (Recommended)",
};
state.questionnaire!.answers[1] = {
kind: "option",
optionKey: "A",
optionLabel: "Postgres (Recommended)",
};
expect(buildPendingQuestionnaireResponse(state.questionnaire!)).toEqual({
answers: {
runtime: { answers: ["Long-lived service (Recommended)"] },
db: { answers: ["Postgres (Recommended)"] },
},
});
});
});