Open History to see the last commit
1import * as NodeOS from "node:os";
2
3let nextServerRequestId = 10_000;
4let pendingSkillsListRequestId: number | string | null = null;
5let pendingUserInputRequestId: number | null = null;
6
7const writeMessage = (message: unknown) => {
8 process.stdout.write(`${JSON.stringify(message)}\n`);
9};
10
11const respond = (id: number | string, result: unknown) => {
12 writeMessage({ id, result });
13};
14
15const respondError = (id: number | string, code: number, message: string) => {
16 writeMessage({
17 id,
18 error: {
19 code,
20 message,
21 },
22 });
23};
24
25const sendRequest = (method: string, params: unknown) => {
26 const id = nextServerRequestId++;
27 writeMessage({ id, method, params });
28 return id;
29};
30
31const handleMethod = (message: Record<string, unknown>) => {
32 const method = message.method;
33 if (typeof method !== "string") {
34 return;
35 }
36
37 switch (method) {
38 case "initialize": {
39 // oxlint-disable-next-line t3code/no-global-process-runtime -- Standalone mock peer process has no Effect runtime.
40 const platform = NodeOS.platform();
41 const stderrBytes = Number(process.env.CODEX_APP_SERVER_TEST_STDERR_BYTES ?? 0);
42 if (Number.isFinite(stderrBytes) && stderrBytes > 0) {
43 process.stderr.write("x".repeat(stderrBytes), () => {
44 respond(message.id as number | string, {
45 userAgent: "mock-codex-app-server",
46 codexHome: process.cwd(),
47 platformFamily: platform === "win32" ? "windows" : "unix",
48 platformOs: platform === "darwin" ? "macos" : platform,
49 });
50 });
51 return;
52 }
53 respond(message.id as number | string, {
54 userAgent: "mock-codex-app-server",
55 codexHome: process.cwd(),
56 platformFamily: platform === "win32" ? "windows" : "unix",
57 platformOs: platform === "darwin" ? "macos" : platform,
58 });
59 return;
60 }
61 case "initialized": {
62 writeMessage({
63 method: "item/agentMessage/delta",
64 params: {
65 delta: "Mock server is ready.",
66 itemId: "item-1",
67 threadId: "thread-1",
68 turnId: "turn-1",
69 },
70 });
71 return;
72 }
73 case "account/read": {
74 respond(message.id as number | string, {
75 account: {
76 type: "chatgpt",
77 email: "mock@example.com",
78 planType: "plus",
79 },
80 requiresOpenaiAuth: false,
81 });
82 return;
83 }
84 case "skills/list": {
85 pendingSkillsListRequestId = message.id as number | string;
86 pendingUserInputRequestId = sendRequest("item/tool/requestUserInput", {
87 isBlocking: true,
88 itemId: "item-approval-1",
89 threadId: "thread-1",
90 turnId: "turn-1",
91 questions: [
92 {
93 id: "approved",
94 header: "Approve",
95 question: "Continue with the mock skills request?",
96 options: [
97 {
98 label: "yes",
99 description: "Approve the request",
100 },
101 ],
102 },
103 ],
104 });
105 return;
106 }
107 case "mock/notifyThenRespond": {
108 const params = message.params as { readonly notification: unknown };
109 writeMessage(params.notification);
110 respond(message.id as number | string, { answered: true });
111 return;
112 }
113 default: {
114 if (message.id !== undefined) {
115 respondError(message.id as number | string, -32601, `Unhandled request: ${method}`);
116 }
117 }
118 }
119};
120
121const handleResponse = (message: Record<string, unknown>) => {
122 if (message.id !== pendingUserInputRequestId) {
123 return;
124 }
125
126 pendingUserInputRequestId = null;
127
128 respond(pendingSkillsListRequestId!, {
129 data: [
130 {
131 cwd: process.cwd(),
132 errors: [],
133 skills: [],
134 },
135 ],
136 });
137 pendingSkillsListRequestId = null;
138};
139
140let remainder = "";
141
142process.stdin.setEncoding("utf8");
143process.stdin.on("data", (chunk) => {
144 remainder += chunk;
145 const lines = remainder.split("\n");
146 remainder = lines.pop() ?? "";
147
148 for (const line of lines) {
149 const trimmed = line.trim();
150 if (trimmed.length === 0) {
151 continue;
152 }
153
154 const message = JSON.parse(trimmed) as Record<string, unknown>;
155 if ("method" in message) {
156 handleMethod(message);
157 continue;
158 }
159 if ("id" in message) {
160 handleResponse(message);
161 }
162 }
163});
164
165process.stdin.on("end", () => {
166 process.exit(0);
167});