Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

XAiAcpExtension.test.ts

332 lines9.6 KB

1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodePath from "node:path";
3import * as NodeURL from "node:url";
4​
5import * as NodeServices from "@effect/platform-node/NodeServices";
6import { it } from "@effect/vitest";
7import * as Effect from "effect/Effect";
8import * as Schema from "effect/Schema";
9import { describe, expect } from "vite-plus/test";
10​
11import {
12 extractXAiAskUserQuestions,
13 makeXAiAskUserQuestionCancelledResponse,
14 makeXAiAskUserQuestionResponse,
15 makeXAiPromptCompletionRuntime,
16 XAiAskUserQuestionRequest,
17} from "./XAiAcpExtension.ts";
18import * as AcpSessionRuntime from "./AcpSessionRuntime.ts";
19​
20const __dirname = NodePath.dirname(NodeURL.fileURLToPath(import.meta.url));
21const mockAgentPath = NodePath.join(__dirname, "../../../scripts/acp-mock-agent.ts");
22​
23const makePromptCompletionRuntime = (env: NodeJS.ProcessEnv) =>
24 Effect.gen(function* () {
25 const runtime = yield* AcpSessionRuntime.make({
26 spawn: {
27 command: process.execPath,
28 args: [mockAgentPath],
29 env,
30 },
31 cwd: process.cwd(),
32 clientInfo: { name: "t3-test", version: "0.0.0" },
33 authMethodId: "test",
34 });
35 return yield* makeXAiPromptCompletionRuntime(runtime);
36 });
37​
38const decodeXAiAskUserQuestionRequest = Schema.decodeUnknownSync(XAiAskUserQuestionRequest);
39​
40describe("XAiAcpExtension", () => {
41 it("extracts questions from the real xAI ask_user_question payload shape", () => {
42 const questions = extractXAiAskUserQuestions({
43 sessionId: "session-1",
44 toolCallId: "tool-call-1",
45 mode: "default",
46 questions: [
47 {
48 id: "scope",
49 question: "Which scope should Grok use?",
50 options: [
51 { label: "Workspace", description: "Use the current workspace" },
52 { label: "Session", description: "Only use this session" },
53 ],
54 },
55 ],
56 });
57​
58 expect(questions).toEqual([
59 {
60 id: "scope",
61 header: "Question",
62 question: "Which scope should Grok use?",
63 multiSelect: false,
64 options: [
65 { label: "Workspace", description: "Use the current workspace" },
66 { label: "Session", description: "Only use this session" },
67 ],
68 },
69 ]);
70 });
71​
72 it("extracts questions from wrapped _x.ai extension payloads", () => {
73 const payload = {
74 method: "_x.ai/ask_user_question",
75 params: {
76 sessionId: "session-1",
77 toolCallId: "tool-call-1",
78 mode: "plan",
79 questions: [
80 {
81 question: "Which changes should be included?",
82 multiSelect: true,
83 options: [{ label: "Tests" }, { label: "Docs" }],
84 },
85 ],
86 },
87 };
88 const decoded = decodeXAiAskUserQuestionRequest(payload);
89 const questions = extractXAiAskUserQuestions(decoded);
90​
91 expect(questions).toEqual([
92 {
93 id: "Which changes should be included?",
94 header: "Question",
95 question: "Which changes should be included?",
96 multiSelect: true,
97 options: [
98 { label: "Tests", description: "Tests" },
99 { label: "Docs", description: "Docs" },
100 ],
101 },
102 ]);
103 });
104​
105 it("treats nullable multiSelect from Grok as single-select", () => {
106 const questions = extractXAiAskUserQuestions({
107 sessionId: "session-1",
108 toolCallId: "tool-call-1",
109 mode: "default",
110 questions: [
111 {
112 question: "Which label should Grok use?",
113 multiSelect: null,
114 options: [
115 { label: "Alpha", description: "Use the Alpha label" },
116 { label: "Beta", description: "Use the Beta label" },
117 { label: "Other", description: "Use the Other label" },
118 ],
119 },
120 ],
121 });
122​
123 expect(questions).toEqual([
124 {
125 id: "Which label should Grok use?",
126 header: "Question",
127 question: "Which label should Grok use?",
128 multiSelect: false,
129 options: [
130 { label: "Alpha", description: "Use the Alpha label" },
131 { label: "Beta", description: "Use the Beta label" },
132 { label: "Other", description: "Use the Other label" },
133 ],
134 },
135 ]);
136 });
137​
138 it("maps UI question ids back to xAI question text in accepted responses", () => {
139 const response = makeXAiAskUserQuestionResponse(
140 {
141 sessionId: "session-1",
142 toolCallId: "tool-call-1",
143 mode: "default",
144 questions: [
145 {
146 id: "scope",
147 question: "Which scope should Grok use?",
148 options: [
149 { label: "workspace", description: "Use the current workspace" },
150 { label: "session", description: "Only use this session" },
151 ],
152 },
153 ],
154 },
155 { scope: "workspace" },
156 );
157​
158 expect(response).toEqual({
159 outcome: "accepted",
160 answers: {
161 "Which scope should Grok use?": ["workspace"],
162 },
163 });
164 });
165​
166 it("orders accepted answers by the original xAI question order", () => {
167 const response = makeXAiAskUserQuestionResponse(
168 {
169 sessionId: "session-1",
170 toolCallId: "tool-call-1",
171 mode: "default",
172 questions: [
173 {
174 id: "first",
175 question: "First question?",
176 options: [{ label: "A", description: "A" }],
177 },
178 {
179 id: "second",
180 question: "Second question?",
181 options: [{ label: "B", description: "B" }],
182 },
183 ],
184 },
185 {
186 second: "B",
187 first: "A",
188 },
189 );
190​
191 expect(Object.keys(response.answers)).toEqual(["First question?", "Second question?"]);
192 expect(response).toMatchObject({
193 outcome: "accepted",
194 answers: {
195 "First question?": ["A"],
196 "Second question?": ["B"],
197 },
198 });
199 });
200​
201 it("encodes typed custom answers as xAI Other annotations", () => {
202 const response = makeXAiAskUserQuestionResponse(
203 {
204 method: "x.ai/ask_user_question",
205 params: {
206 sessionId: "session-1",
207 toolCallId: "tool-call-1",
208 mode: "default",
209 questions: [
210 {
211 question: "Which ice cream flavor?",
212 options: [
213 { label: "vanilla", description: "Vanilla flavor" },
214 { label: "chocolate", description: "Chocolate flavor" },
215 ],
216 },
217 ],
218 },
219 },
220 { "Which ice cream flavor?": "pistachio" },
221 );
222​
223 expect(response).toEqual({
224 outcome: "accepted",
225 answers: {
226 "Which ice cream flavor?": ["Other"],
227 },
228 annotations: {
229 "Which ice cream flavor?": {
230 notes: "pistachio",
231 },
232 },
233 });
234 });
235​
236 it("encodes interrupted dialogs as xAI cancelled responses", () => {
237 expect(makeXAiAskUserQuestionCancelledResponse()).toEqual({
238 outcome: "cancelled",
239 });
240 });
241​
242 it("does not echo preview annotations for multi-select answers", () => {
243 const response = makeXAiAskUserQuestionResponse(
244 {
245 sessionId: "session-1",
246 toolCallId: "tool-call-1",
247 mode: "default",
248 questions: [
249 {
250 question: "Which files should Grok touch?",
251 multiSelect: true,
252 options: [
253 {
254 label: "Tests",
255 description: "Update tests",
256 preview: "test preview",
257 },
258 {
259 label: "Docs",
260 description: "Update docs",
261 preview: "docs preview",
262 },
263 ],
264 },
265 ],
266 },
267 { "Which files should Grok touch?": ["Tests", "Docs"] },
268 );
269​
270 expect(response).toEqual({
271 outcome: "accepted",
272 answers: {
273 "Which files should Grok touch?": ["Tests", "Docs"],
274 },
275 });
276 });
277​
278 it.effect("resolves a hung standard prompt from xAI prompt completion", () =>
279 Effect.gen(function* () {
280 const runtime = yield* makePromptCompletionRuntime({
281 T3_ACP_EMIT_XAI_PROMPT_COMPLETE_THEN_HANG: "1",
282 });
283 yield* runtime.start();
284​
285 const promptResult = yield* runtime.prompt({
286 prompt: [{ type: "text", text: "hi" }],
287 });
288 const promptId = promptResult._meta?.promptId;
289​
290 expect(typeof promptId).toBe("string");
291 expect(promptResult).toMatchObject({
292 stopReason: "end_turn",
293 _meta: {
294 sessionId: "mock-session-1",
295 promptId,
296 requestId: promptId,
297 },
298 });
299 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
300 );
301​
302 it.effect("ignores stale xAI completion from an already settled prompt", () =>
303 Effect.gen(function* () {
304 const runtime = yield* makePromptCompletionRuntime({
305 T3_ACP_EMIT_STALE_XAI_PROMPT_COMPLETE_BEFORE_SECOND_HANG: "1",
306 });
307 yield* runtime.start();
308​
309 const firstPromptResult = yield* runtime.prompt({
310 prompt: [{ type: "text", text: "first" }],
311 });
312 expect(firstPromptResult).toMatchObject({
313 stopReason: "end_turn",
314 _meta: { promptId: "mock-stale-xai-prompt-1" },
315 });
316​
317 const secondPromptResult = yield* runtime.prompt({
318 prompt: [{ type: "text", text: "second" }],
319 });
320 const secondPromptId = secondPromptResult._meta?.promptId;
321 expect(typeof secondPromptId).toBe("string");
322 expect(secondPromptId).not.toBe("mock-stale-xai-prompt-1");
323 expect(secondPromptResult).toMatchObject({
324 stopReason: "end_turn",
325 _meta: {
326 promptId: secondPromptId,
327 requestId: secondPromptId,
328 },
329 });
330 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
331 );
332});