@zhigang1992/happy-cli 0.12.1
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/README.md +60 -0
- package/bin/happy-mcp.mjs +32 -0
- package/bin/happy.mjs +35 -0
- package/dist/codex/happyMcpStdioBridge.cjs +80 -0
- package/dist/codex/happyMcpStdioBridge.d.cts +2 -0
- package/dist/codex/happyMcpStdioBridge.d.mts +2 -0
- package/dist/codex/happyMcpStdioBridge.mjs +78 -0
- package/dist/index-BOBrKhX5.cjs +6655 -0
- package/dist/index-DsHtmQqP.mjs +6624 -0
- package/dist/index.cjs +42 -0
- package/dist/index.d.cts +1 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.mjs +39 -0
- package/dist/lib.cjs +31 -0
- package/dist/lib.d.cts +817 -0
- package/dist/lib.d.mts +817 -0
- package/dist/lib.mjs +21 -0
- package/dist/list-BW6QBLa1.cjs +328 -0
- package/dist/list-hET5tyMc.mjs +326 -0
- package/dist/prompt-DXkgjktW.cjs +203 -0
- package/dist/prompt-Dz7G8yGx.mjs +201 -0
- package/dist/runCodex-CLGYMNs2.mjs +1335 -0
- package/dist/runCodex-CylcX5Ug.cjs +1338 -0
- package/dist/types-BsjUgWOx.cjs +2264 -0
- package/dist/types-CGco5Y-r.mjs +2213 -0
- package/package.json +126 -0
- package/scripts/claude_local_launcher.cjs +98 -0
- package/scripts/claude_remote_launcher.cjs +13 -0
- package/scripts/ripgrep_launcher.cjs +33 -0
- package/scripts/unpack-tools.cjs +163 -0
- package/tools/archives/difftastic-LICENSE +21 -0
- package/tools/archives/difftastic-arm64-darwin.tar.gz +0 -0
- package/tools/archives/difftastic-arm64-linux.tar.gz +0 -0
- package/tools/archives/difftastic-x64-darwin.tar.gz +0 -0
- package/tools/archives/difftastic-x64-linux.tar.gz +0 -0
- package/tools/archives/difftastic-x64-win32.tar.gz +0 -0
- package/tools/archives/ripgrep-LICENSE +3 -0
- package/tools/archives/ripgrep-arm64-darwin.tar.gz +0 -0
- package/tools/archives/ripgrep-arm64-linux.tar.gz +0 -0
- package/tools/archives/ripgrep-x64-darwin.tar.gz +0 -0
- package/tools/archives/ripgrep-x64-linux.tar.gz +0 -0
- package/tools/archives/ripgrep-x64-win32.tar.gz +0 -0
- package/tools/licenses/difftastic-LICENSE +21 -0
- package/tools/licenses/ripgrep-LICENSE +3 -0
- package/tools/unpacked/difft +0 -0
- package/tools/unpacked/rg +0 -0
- package/tools/unpacked/ripgrep.node +0 -0
|
@@ -0,0 +1,1335 @@
|
|
|
1
|
+
import { useStdout, useInput, Box, Text, render } from 'ink';
|
|
2
|
+
import React, { useState, useRef, useEffect, useCallback } from 'react';
|
|
3
|
+
import { l as logger, A as ApiClient, r as readSettings, p as projectPath, c as configuration, i as packageJson } from './types-CGco5Y-r.mjs';
|
|
4
|
+
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
5
|
+
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
import { ElicitRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
8
|
+
import { execSync } from 'child_process';
|
|
9
|
+
import { randomUUID } from 'node:crypto';
|
|
10
|
+
import { i as initialMachineMetadata, n as notifyDaemonSessionStarted, M as MessageQueue2, h as hashObject, r as registerKillSessionHandler, a as MessageBuffer, s as startHappyServer, t as trimIdent, b as stopCaffeinate } from './index-DsHtmQqP.mjs';
|
|
11
|
+
import os from 'node:os';
|
|
12
|
+
import { resolve, join } from 'node:path';
|
|
13
|
+
import fs from 'node:fs';
|
|
14
|
+
import 'axios';
|
|
15
|
+
import 'chalk';
|
|
16
|
+
import 'fs';
|
|
17
|
+
import 'node:fs/promises';
|
|
18
|
+
import 'tweetnacl';
|
|
19
|
+
import 'node:events';
|
|
20
|
+
import 'socket.io-client';
|
|
21
|
+
import 'util';
|
|
22
|
+
import 'fs/promises';
|
|
23
|
+
import 'crypto';
|
|
24
|
+
import 'path';
|
|
25
|
+
import 'url';
|
|
26
|
+
import 'os';
|
|
27
|
+
import 'expo-server-sdk';
|
|
28
|
+
import 'node:child_process';
|
|
29
|
+
import 'node:readline';
|
|
30
|
+
import 'node:url';
|
|
31
|
+
import 'ps-list';
|
|
32
|
+
import 'cross-spawn';
|
|
33
|
+
import 'tmp';
|
|
34
|
+
import 'qrcode-terminal';
|
|
35
|
+
import 'open';
|
|
36
|
+
import 'fastify';
|
|
37
|
+
import 'fastify-type-provider-zod';
|
|
38
|
+
import '@modelcontextprotocol/sdk/server/mcp.js';
|
|
39
|
+
import 'node:http';
|
|
40
|
+
import '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
|
41
|
+
import '@stablelib/hex';
|
|
42
|
+
import 'http';
|
|
43
|
+
|
|
44
|
+
const DEFAULT_TIMEOUT = 14 * 24 * 60 * 60 * 1e3;
|
|
45
|
+
function getCodexMcpCommand() {
|
|
46
|
+
try {
|
|
47
|
+
const version = execSync("codex --version", { encoding: "utf8" }).trim();
|
|
48
|
+
const match = version.match(/codex-cli\s+(\d+\.\d+\.\d+(?:-alpha\.\d+)?)/);
|
|
49
|
+
if (!match) return "mcp-server";
|
|
50
|
+
const versionStr = match[1];
|
|
51
|
+
const [major, minor, patch] = versionStr.split(/[-.]/).map(Number);
|
|
52
|
+
if (major > 0 || minor > 43) return "mcp-server";
|
|
53
|
+
if (minor === 43 && patch === 0) {
|
|
54
|
+
if (versionStr.includes("-alpha.")) {
|
|
55
|
+
const alphaNum = parseInt(versionStr.split("-alpha.")[1]);
|
|
56
|
+
return alphaNum >= 5 ? "mcp-server" : "mcp";
|
|
57
|
+
}
|
|
58
|
+
return "mcp-server";
|
|
59
|
+
}
|
|
60
|
+
return "mcp";
|
|
61
|
+
} catch (error) {
|
|
62
|
+
logger.debug("[CodexMCP] Error detecting codex version, defaulting to mcp-server:", error);
|
|
63
|
+
return "mcp-server";
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
class CodexMcpClient {
|
|
67
|
+
client;
|
|
68
|
+
transport = null;
|
|
69
|
+
connected = false;
|
|
70
|
+
sessionId = null;
|
|
71
|
+
conversationId = null;
|
|
72
|
+
handler = null;
|
|
73
|
+
permissionHandler = null;
|
|
74
|
+
constructor() {
|
|
75
|
+
this.client = new Client(
|
|
76
|
+
{ name: "happy-codex-client", version: "1.0.0" },
|
|
77
|
+
{ capabilities: { tools: {}, elicitation: {} } }
|
|
78
|
+
);
|
|
79
|
+
this.client.setNotificationHandler(z.object({
|
|
80
|
+
method: z.literal("codex/event"),
|
|
81
|
+
params: z.object({
|
|
82
|
+
msg: z.any()
|
|
83
|
+
})
|
|
84
|
+
}).passthrough(), (data) => {
|
|
85
|
+
const msg = data.params.msg;
|
|
86
|
+
this.updateIdentifiersFromEvent(msg);
|
|
87
|
+
this.handler?.(msg);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
setHandler(handler) {
|
|
91
|
+
this.handler = handler;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Set the permission handler for tool approval
|
|
95
|
+
*/
|
|
96
|
+
setPermissionHandler(handler) {
|
|
97
|
+
this.permissionHandler = handler;
|
|
98
|
+
}
|
|
99
|
+
async connect() {
|
|
100
|
+
if (this.connected) return;
|
|
101
|
+
const mcpCommand = getCodexMcpCommand();
|
|
102
|
+
logger.debug(`[CodexMCP] Connecting to Codex MCP server using command: codex ${mcpCommand}`);
|
|
103
|
+
this.transport = new StdioClientTransport({
|
|
104
|
+
command: "codex",
|
|
105
|
+
args: [mcpCommand],
|
|
106
|
+
env: Object.keys(process.env).reduce((acc, key) => {
|
|
107
|
+
const value = process.env[key];
|
|
108
|
+
if (typeof value === "string") acc[key] = value;
|
|
109
|
+
return acc;
|
|
110
|
+
}, {})
|
|
111
|
+
});
|
|
112
|
+
this.registerPermissionHandlers();
|
|
113
|
+
await this.client.connect(this.transport);
|
|
114
|
+
this.connected = true;
|
|
115
|
+
logger.debug("[CodexMCP] Connected to Codex");
|
|
116
|
+
}
|
|
117
|
+
registerPermissionHandlers() {
|
|
118
|
+
this.client.setRequestHandler(
|
|
119
|
+
ElicitRequestSchema,
|
|
120
|
+
async (request) => {
|
|
121
|
+
console.log("[CodexMCP] Received elicitation request:", request.params);
|
|
122
|
+
const params = request.params;
|
|
123
|
+
const toolName = "CodexBash";
|
|
124
|
+
if (!this.permissionHandler) {
|
|
125
|
+
logger.debug("[CodexMCP] No permission handler set, denying by default");
|
|
126
|
+
return {
|
|
127
|
+
decision: "denied"
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
try {
|
|
131
|
+
const result = await this.permissionHandler.handleToolCall(
|
|
132
|
+
params.codex_call_id,
|
|
133
|
+
toolName,
|
|
134
|
+
{
|
|
135
|
+
command: params.codex_command,
|
|
136
|
+
cwd: params.codex_cwd
|
|
137
|
+
}
|
|
138
|
+
);
|
|
139
|
+
logger.debug("[CodexMCP] Permission result:", result);
|
|
140
|
+
return {
|
|
141
|
+
decision: result.decision
|
|
142
|
+
};
|
|
143
|
+
} catch (error) {
|
|
144
|
+
logger.debug("[CodexMCP] Error handling permission request:", error);
|
|
145
|
+
return {
|
|
146
|
+
decision: "denied",
|
|
147
|
+
reason: error instanceof Error ? error.message : "Permission request failed"
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
);
|
|
152
|
+
logger.debug("[CodexMCP] Permission handlers registered");
|
|
153
|
+
}
|
|
154
|
+
async startSession(config, options) {
|
|
155
|
+
if (!this.connected) await this.connect();
|
|
156
|
+
logger.debug("[CodexMCP] Starting Codex session:", config);
|
|
157
|
+
const response = await this.client.callTool({
|
|
158
|
+
name: "codex",
|
|
159
|
+
arguments: config
|
|
160
|
+
}, void 0, {
|
|
161
|
+
signal: options?.signal,
|
|
162
|
+
timeout: DEFAULT_TIMEOUT
|
|
163
|
+
// maxTotalTimeout: 10000000000
|
|
164
|
+
});
|
|
165
|
+
logger.debug("[CodexMCP] startSession response:", response);
|
|
166
|
+
this.extractIdentifiers(response);
|
|
167
|
+
return response;
|
|
168
|
+
}
|
|
169
|
+
async continueSession(prompt, options) {
|
|
170
|
+
if (!this.connected) await this.connect();
|
|
171
|
+
if (!this.sessionId) {
|
|
172
|
+
throw new Error("No active session. Call startSession first.");
|
|
173
|
+
}
|
|
174
|
+
if (!this.conversationId) {
|
|
175
|
+
this.conversationId = this.sessionId;
|
|
176
|
+
logger.debug("[CodexMCP] conversationId missing, defaulting to sessionId:", this.conversationId);
|
|
177
|
+
}
|
|
178
|
+
const args = { sessionId: this.sessionId, conversationId: this.conversationId, prompt };
|
|
179
|
+
logger.debug("[CodexMCP] Continuing Codex session:", args);
|
|
180
|
+
const response = await this.client.callTool({
|
|
181
|
+
name: "codex-reply",
|
|
182
|
+
arguments: args
|
|
183
|
+
}, void 0, {
|
|
184
|
+
signal: options?.signal,
|
|
185
|
+
timeout: DEFAULT_TIMEOUT
|
|
186
|
+
});
|
|
187
|
+
logger.debug("[CodexMCP] continueSession response:", response);
|
|
188
|
+
this.extractIdentifiers(response);
|
|
189
|
+
return response;
|
|
190
|
+
}
|
|
191
|
+
updateIdentifiersFromEvent(event) {
|
|
192
|
+
if (!event || typeof event !== "object") {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const candidates = [event];
|
|
196
|
+
if (event.data && typeof event.data === "object") {
|
|
197
|
+
candidates.push(event.data);
|
|
198
|
+
}
|
|
199
|
+
for (const candidate of candidates) {
|
|
200
|
+
const sessionId = candidate.session_id ?? candidate.sessionId;
|
|
201
|
+
if (sessionId) {
|
|
202
|
+
this.sessionId = sessionId;
|
|
203
|
+
logger.debug("[CodexMCP] Session ID extracted from event:", this.sessionId);
|
|
204
|
+
}
|
|
205
|
+
const conversationId = candidate.conversation_id ?? candidate.conversationId;
|
|
206
|
+
if (conversationId) {
|
|
207
|
+
this.conversationId = conversationId;
|
|
208
|
+
logger.debug("[CodexMCP] Conversation ID extracted from event:", this.conversationId);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
extractIdentifiers(response) {
|
|
213
|
+
const meta = response?.meta || {};
|
|
214
|
+
if (meta.sessionId) {
|
|
215
|
+
this.sessionId = meta.sessionId;
|
|
216
|
+
logger.debug("[CodexMCP] Session ID extracted:", this.sessionId);
|
|
217
|
+
} else if (response?.sessionId) {
|
|
218
|
+
this.sessionId = response.sessionId;
|
|
219
|
+
logger.debug("[CodexMCP] Session ID extracted:", this.sessionId);
|
|
220
|
+
}
|
|
221
|
+
if (meta.conversationId) {
|
|
222
|
+
this.conversationId = meta.conversationId;
|
|
223
|
+
logger.debug("[CodexMCP] Conversation ID extracted:", this.conversationId);
|
|
224
|
+
} else if (response?.conversationId) {
|
|
225
|
+
this.conversationId = response.conversationId;
|
|
226
|
+
logger.debug("[CodexMCP] Conversation ID extracted:", this.conversationId);
|
|
227
|
+
}
|
|
228
|
+
const content = response?.content;
|
|
229
|
+
if (Array.isArray(content)) {
|
|
230
|
+
for (const item of content) {
|
|
231
|
+
if (!this.sessionId && item?.sessionId) {
|
|
232
|
+
this.sessionId = item.sessionId;
|
|
233
|
+
logger.debug("[CodexMCP] Session ID extracted from content:", this.sessionId);
|
|
234
|
+
}
|
|
235
|
+
if (!this.conversationId && item && typeof item === "object" && "conversationId" in item && item.conversationId) {
|
|
236
|
+
this.conversationId = item.conversationId;
|
|
237
|
+
logger.debug("[CodexMCP] Conversation ID extracted from content:", this.conversationId);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
getSessionId() {
|
|
243
|
+
return this.sessionId;
|
|
244
|
+
}
|
|
245
|
+
hasActiveSession() {
|
|
246
|
+
return this.sessionId !== null;
|
|
247
|
+
}
|
|
248
|
+
clearSession() {
|
|
249
|
+
const previousSessionId = this.sessionId;
|
|
250
|
+
this.sessionId = null;
|
|
251
|
+
this.conversationId = null;
|
|
252
|
+
logger.debug("[CodexMCP] Session cleared, previous sessionId:", previousSessionId);
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Store the current session ID without clearing it, useful for abort handling
|
|
256
|
+
*/
|
|
257
|
+
storeSessionForResume() {
|
|
258
|
+
logger.debug("[CodexMCP] Storing session for potential resume:", this.sessionId);
|
|
259
|
+
return this.sessionId;
|
|
260
|
+
}
|
|
261
|
+
async disconnect() {
|
|
262
|
+
if (!this.connected) return;
|
|
263
|
+
const pid = this.transport?.pid ?? null;
|
|
264
|
+
logger.debug(`[CodexMCP] Disconnecting; child pid=${pid ?? "none"}`);
|
|
265
|
+
try {
|
|
266
|
+
logger.debug("[CodexMCP] client.close begin");
|
|
267
|
+
await this.client.close();
|
|
268
|
+
logger.debug("[CodexMCP] client.close done");
|
|
269
|
+
} catch (e) {
|
|
270
|
+
logger.debug("[CodexMCP] Error closing client, attempting transport close directly", e);
|
|
271
|
+
try {
|
|
272
|
+
logger.debug("[CodexMCP] transport.close begin");
|
|
273
|
+
await this.transport?.close?.();
|
|
274
|
+
logger.debug("[CodexMCP] transport.close done");
|
|
275
|
+
} catch {
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
if (pid) {
|
|
279
|
+
try {
|
|
280
|
+
process.kill(pid, 0);
|
|
281
|
+
logger.debug("[CodexMCP] Child still alive, sending SIGKILL");
|
|
282
|
+
try {
|
|
283
|
+
process.kill(pid, "SIGKILL");
|
|
284
|
+
} catch {
|
|
285
|
+
}
|
|
286
|
+
} catch {
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
this.transport = null;
|
|
290
|
+
this.connected = false;
|
|
291
|
+
this.sessionId = null;
|
|
292
|
+
this.conversationId = null;
|
|
293
|
+
logger.debug("[CodexMCP] Disconnected");
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
class CodexPermissionHandler {
|
|
298
|
+
pendingRequests = /* @__PURE__ */ new Map();
|
|
299
|
+
session;
|
|
300
|
+
constructor(session) {
|
|
301
|
+
this.session = session;
|
|
302
|
+
this.setupRpcHandler();
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Handle a tool permission request
|
|
306
|
+
* @param toolCallId - The unique ID of the tool call
|
|
307
|
+
* @param toolName - The name of the tool being called
|
|
308
|
+
* @param input - The input parameters for the tool
|
|
309
|
+
* @returns Promise resolving to permission result
|
|
310
|
+
*/
|
|
311
|
+
async handleToolCall(toolCallId, toolName, input) {
|
|
312
|
+
return new Promise((resolve, reject) => {
|
|
313
|
+
this.pendingRequests.set(toolCallId, {
|
|
314
|
+
resolve,
|
|
315
|
+
reject,
|
|
316
|
+
toolName,
|
|
317
|
+
input
|
|
318
|
+
});
|
|
319
|
+
this.session.updateAgentState((currentState) => ({
|
|
320
|
+
...currentState,
|
|
321
|
+
requests: {
|
|
322
|
+
...currentState.requests,
|
|
323
|
+
[toolCallId]: {
|
|
324
|
+
tool: toolName,
|
|
325
|
+
arguments: input,
|
|
326
|
+
createdAt: Date.now()
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}));
|
|
330
|
+
logger.debug(`[Codex] Permission request sent for tool: ${toolName} (${toolCallId})`);
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Setup RPC handler for permission responses
|
|
335
|
+
*/
|
|
336
|
+
setupRpcHandler() {
|
|
337
|
+
this.session.rpcHandlerManager.registerHandler(
|
|
338
|
+
"permission",
|
|
339
|
+
async (response) => {
|
|
340
|
+
const pending = this.pendingRequests.get(response.id);
|
|
341
|
+
if (!pending) {
|
|
342
|
+
logger.debug("[Codex] Permission request not found or already resolved");
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
this.pendingRequests.delete(response.id);
|
|
346
|
+
const result = response.approved ? { decision: response.decision === "approved_for_session" ? "approved_for_session" : "approved" } : { decision: response.decision === "denied" ? "denied" : "abort" };
|
|
347
|
+
pending.resolve(result);
|
|
348
|
+
this.session.updateAgentState((currentState) => {
|
|
349
|
+
const request = currentState.requests?.[response.id];
|
|
350
|
+
if (!request) return currentState;
|
|
351
|
+
const { [response.id]: _, ...remainingRequests } = currentState.requests || {};
|
|
352
|
+
let res = {
|
|
353
|
+
...currentState,
|
|
354
|
+
requests: remainingRequests,
|
|
355
|
+
completedRequests: {
|
|
356
|
+
...currentState.completedRequests,
|
|
357
|
+
[response.id]: {
|
|
358
|
+
...request,
|
|
359
|
+
completedAt: Date.now(),
|
|
360
|
+
status: response.approved ? "approved" : "denied",
|
|
361
|
+
decision: result.decision
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
return res;
|
|
366
|
+
});
|
|
367
|
+
logger.debug(`[Codex] Permission ${response.approved ? "approved" : "denied"} for ${pending.toolName}`);
|
|
368
|
+
}
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Reset state for new sessions
|
|
373
|
+
*/
|
|
374
|
+
reset() {
|
|
375
|
+
for (const [id, pending] of this.pendingRequests.entries()) {
|
|
376
|
+
pending.reject(new Error("Session reset"));
|
|
377
|
+
}
|
|
378
|
+
this.pendingRequests.clear();
|
|
379
|
+
this.session.updateAgentState((currentState) => {
|
|
380
|
+
const pendingRequests = currentState.requests || {};
|
|
381
|
+
const completedRequests = { ...currentState.completedRequests };
|
|
382
|
+
for (const [id, request] of Object.entries(pendingRequests)) {
|
|
383
|
+
completedRequests[id] = {
|
|
384
|
+
...request,
|
|
385
|
+
completedAt: Date.now(),
|
|
386
|
+
status: "canceled",
|
|
387
|
+
reason: "Session reset"
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
return {
|
|
391
|
+
...currentState,
|
|
392
|
+
requests: {},
|
|
393
|
+
completedRequests
|
|
394
|
+
};
|
|
395
|
+
});
|
|
396
|
+
logger.debug("[Codex] Permission handler reset");
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
class ReasoningProcessor {
|
|
401
|
+
accumulator = "";
|
|
402
|
+
inTitleCapture = false;
|
|
403
|
+
titleBuffer = "";
|
|
404
|
+
contentBuffer = "";
|
|
405
|
+
hasTitle = false;
|
|
406
|
+
currentCallId = null;
|
|
407
|
+
toolCallStarted = false;
|
|
408
|
+
currentTitle = null;
|
|
409
|
+
onMessage = null;
|
|
410
|
+
constructor(onMessage) {
|
|
411
|
+
this.onMessage = onMessage || null;
|
|
412
|
+
this.reset();
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Set the message callback for sending messages directly
|
|
416
|
+
*/
|
|
417
|
+
setMessageCallback(callback) {
|
|
418
|
+
this.onMessage = callback;
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Process a reasoning section break - indicates a new reasoning section is starting
|
|
422
|
+
*/
|
|
423
|
+
handleSectionBreak() {
|
|
424
|
+
this.finishCurrentToolCall("canceled");
|
|
425
|
+
this.resetState();
|
|
426
|
+
logger.debug("[ReasoningProcessor] Section break - reset state");
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Process a reasoning delta and accumulate content
|
|
430
|
+
*/
|
|
431
|
+
processDelta(delta) {
|
|
432
|
+
this.accumulator += delta;
|
|
433
|
+
if (!this.inTitleCapture && !this.hasTitle && !this.contentBuffer) {
|
|
434
|
+
if (this.accumulator.startsWith("**")) {
|
|
435
|
+
this.inTitleCapture = true;
|
|
436
|
+
this.titleBuffer = this.accumulator.substring(2);
|
|
437
|
+
logger.debug("[ReasoningProcessor] Started title capture");
|
|
438
|
+
} else if (this.accumulator.length > 0) {
|
|
439
|
+
this.contentBuffer = this.accumulator;
|
|
440
|
+
}
|
|
441
|
+
} else if (this.inTitleCapture) {
|
|
442
|
+
this.titleBuffer = this.accumulator.substring(2);
|
|
443
|
+
const titleEndIndex = this.titleBuffer.indexOf("**");
|
|
444
|
+
if (titleEndIndex !== -1) {
|
|
445
|
+
const title = this.titleBuffer.substring(0, titleEndIndex);
|
|
446
|
+
const afterTitle = this.titleBuffer.substring(titleEndIndex + 2);
|
|
447
|
+
this.hasTitle = true;
|
|
448
|
+
this.inTitleCapture = false;
|
|
449
|
+
this.currentTitle = title;
|
|
450
|
+
this.contentBuffer = afterTitle;
|
|
451
|
+
this.currentCallId = randomUUID();
|
|
452
|
+
logger.debug(`[ReasoningProcessor] Title captured: "${title}"`);
|
|
453
|
+
this.sendToolCallStart(title);
|
|
454
|
+
}
|
|
455
|
+
} else if (this.hasTitle) {
|
|
456
|
+
this.contentBuffer = this.accumulator.substring(
|
|
457
|
+
this.accumulator.indexOf("**") + 2 + this.currentTitle.length + 2
|
|
458
|
+
);
|
|
459
|
+
} else {
|
|
460
|
+
this.contentBuffer = this.accumulator;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* Send the tool call start message
|
|
465
|
+
*/
|
|
466
|
+
sendToolCallStart(title) {
|
|
467
|
+
if (!this.currentCallId || this.toolCallStarted) {
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
const toolCall = {
|
|
471
|
+
type: "tool-call",
|
|
472
|
+
name: "CodexReasoning",
|
|
473
|
+
callId: this.currentCallId,
|
|
474
|
+
input: {
|
|
475
|
+
title
|
|
476
|
+
},
|
|
477
|
+
id: randomUUID()
|
|
478
|
+
};
|
|
479
|
+
logger.debug(`[ReasoningProcessor] Sending tool call start for: "${title}"`);
|
|
480
|
+
this.onMessage?.(toolCall);
|
|
481
|
+
this.toolCallStarted = true;
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Complete the reasoning section with final text
|
|
485
|
+
*/
|
|
486
|
+
complete(fullText) {
|
|
487
|
+
let title;
|
|
488
|
+
let content = fullText;
|
|
489
|
+
if (fullText.startsWith("**")) {
|
|
490
|
+
const titleEndIndex = fullText.indexOf("**", 2);
|
|
491
|
+
if (titleEndIndex !== -1) {
|
|
492
|
+
title = fullText.substring(2, titleEndIndex);
|
|
493
|
+
content = fullText.substring(titleEndIndex + 2).trim();
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
logger.debug(`[ReasoningProcessor] Complete reasoning - Title: "${title}", Has content: ${content.length > 0}`);
|
|
497
|
+
if (title && !this.toolCallStarted) {
|
|
498
|
+
this.currentCallId = this.currentCallId || randomUUID();
|
|
499
|
+
this.sendToolCallStart(title);
|
|
500
|
+
}
|
|
501
|
+
if (this.toolCallStarted && this.currentCallId) {
|
|
502
|
+
const toolResult = {
|
|
503
|
+
type: "tool-call-result",
|
|
504
|
+
callId: this.currentCallId,
|
|
505
|
+
output: {
|
|
506
|
+
content,
|
|
507
|
+
status: "completed"
|
|
508
|
+
},
|
|
509
|
+
id: randomUUID()
|
|
510
|
+
};
|
|
511
|
+
logger.debug("[ReasoningProcessor] Sending tool call result");
|
|
512
|
+
this.onMessage?.(toolResult);
|
|
513
|
+
} else {
|
|
514
|
+
const reasoningMessage = {
|
|
515
|
+
type: "reasoning",
|
|
516
|
+
message: content,
|
|
517
|
+
id: randomUUID()
|
|
518
|
+
};
|
|
519
|
+
logger.debug("[ReasoningProcessor] Sending reasoning message");
|
|
520
|
+
this.onMessage?.(reasoningMessage);
|
|
521
|
+
}
|
|
522
|
+
this.resetState();
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Abort the current reasoning section
|
|
526
|
+
*/
|
|
527
|
+
abort() {
|
|
528
|
+
logger.debug("[ReasoningProcessor] Abort called");
|
|
529
|
+
this.finishCurrentToolCall("canceled");
|
|
530
|
+
this.resetState();
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Reset the processor state
|
|
534
|
+
*/
|
|
535
|
+
reset() {
|
|
536
|
+
this.finishCurrentToolCall("canceled");
|
|
537
|
+
this.resetState();
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* Finish current tool call if one is in progress
|
|
541
|
+
*/
|
|
542
|
+
finishCurrentToolCall(status) {
|
|
543
|
+
if (this.toolCallStarted && this.currentCallId) {
|
|
544
|
+
const toolResult = {
|
|
545
|
+
type: "tool-call-result",
|
|
546
|
+
callId: this.currentCallId,
|
|
547
|
+
output: {
|
|
548
|
+
content: this.contentBuffer || "",
|
|
549
|
+
status
|
|
550
|
+
},
|
|
551
|
+
id: randomUUID()
|
|
552
|
+
};
|
|
553
|
+
logger.debug(`[ReasoningProcessor] Sending tool call result with status: ${status}`);
|
|
554
|
+
this.onMessage?.(toolResult);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* Reset internal state
|
|
559
|
+
*/
|
|
560
|
+
resetState() {
|
|
561
|
+
this.accumulator = "";
|
|
562
|
+
this.inTitleCapture = false;
|
|
563
|
+
this.titleBuffer = "";
|
|
564
|
+
this.contentBuffer = "";
|
|
565
|
+
this.hasTitle = false;
|
|
566
|
+
this.currentCallId = null;
|
|
567
|
+
this.toolCallStarted = false;
|
|
568
|
+
this.currentTitle = null;
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Get the current call ID for tool result matching
|
|
572
|
+
*/
|
|
573
|
+
getCurrentCallId() {
|
|
574
|
+
return this.currentCallId;
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Check if a tool call has been started
|
|
578
|
+
*/
|
|
579
|
+
hasStartedToolCall() {
|
|
580
|
+
return this.toolCallStarted;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
class DiffProcessor {
|
|
585
|
+
previousDiff = null;
|
|
586
|
+
onMessage = null;
|
|
587
|
+
constructor(onMessage) {
|
|
588
|
+
this.onMessage = onMessage || null;
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Process a turn_diff message and check if the unified_diff has changed
|
|
592
|
+
*/
|
|
593
|
+
processDiff(unifiedDiff) {
|
|
594
|
+
if (this.previousDiff !== unifiedDiff) {
|
|
595
|
+
logger.debug("[DiffProcessor] Unified diff changed, sending CodexDiff tool call");
|
|
596
|
+
const callId = randomUUID();
|
|
597
|
+
const toolCall = {
|
|
598
|
+
type: "tool-call",
|
|
599
|
+
name: "CodexDiff",
|
|
600
|
+
callId,
|
|
601
|
+
input: {
|
|
602
|
+
unified_diff: unifiedDiff
|
|
603
|
+
},
|
|
604
|
+
id: randomUUID()
|
|
605
|
+
};
|
|
606
|
+
this.onMessage?.(toolCall);
|
|
607
|
+
const toolResult = {
|
|
608
|
+
type: "tool-call-result",
|
|
609
|
+
callId,
|
|
610
|
+
output: {
|
|
611
|
+
status: "completed"
|
|
612
|
+
},
|
|
613
|
+
id: randomUUID()
|
|
614
|
+
};
|
|
615
|
+
this.onMessage?.(toolResult);
|
|
616
|
+
}
|
|
617
|
+
this.previousDiff = unifiedDiff;
|
|
618
|
+
logger.debug("[DiffProcessor] Updated stored diff");
|
|
619
|
+
}
|
|
620
|
+
/**
|
|
621
|
+
* Reset the processor state (called on task_complete or turn_aborted)
|
|
622
|
+
*/
|
|
623
|
+
reset() {
|
|
624
|
+
logger.debug("[DiffProcessor] Resetting diff state");
|
|
625
|
+
this.previousDiff = null;
|
|
626
|
+
}
|
|
627
|
+
/**
|
|
628
|
+
* Set the message callback for sending messages directly
|
|
629
|
+
*/
|
|
630
|
+
setMessageCallback(callback) {
|
|
631
|
+
this.onMessage = callback;
|
|
632
|
+
}
|
|
633
|
+
/**
|
|
634
|
+
* Get the current diff value
|
|
635
|
+
*/
|
|
636
|
+
getCurrentDiff() {
|
|
637
|
+
return this.previousDiff;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
const CodexDisplay = ({ messageBuffer, logPath, onExit }) => {
|
|
642
|
+
const [messages, setMessages] = useState([]);
|
|
643
|
+
const [confirmationMode, setConfirmationMode] = useState(false);
|
|
644
|
+
const [actionInProgress, setActionInProgress] = useState(false);
|
|
645
|
+
const confirmationTimeoutRef = useRef(null);
|
|
646
|
+
const { stdout } = useStdout();
|
|
647
|
+
const terminalWidth = stdout.columns || 80;
|
|
648
|
+
const terminalHeight = stdout.rows || 24;
|
|
649
|
+
useEffect(() => {
|
|
650
|
+
setMessages(messageBuffer.getMessages());
|
|
651
|
+
const unsubscribe = messageBuffer.onUpdate((newMessages) => {
|
|
652
|
+
setMessages(newMessages);
|
|
653
|
+
});
|
|
654
|
+
return () => {
|
|
655
|
+
unsubscribe();
|
|
656
|
+
if (confirmationTimeoutRef.current) {
|
|
657
|
+
clearTimeout(confirmationTimeoutRef.current);
|
|
658
|
+
}
|
|
659
|
+
};
|
|
660
|
+
}, [messageBuffer]);
|
|
661
|
+
const resetConfirmation = useCallback(() => {
|
|
662
|
+
setConfirmationMode(false);
|
|
663
|
+
if (confirmationTimeoutRef.current) {
|
|
664
|
+
clearTimeout(confirmationTimeoutRef.current);
|
|
665
|
+
confirmationTimeoutRef.current = null;
|
|
666
|
+
}
|
|
667
|
+
}, []);
|
|
668
|
+
const setConfirmationWithTimeout = useCallback(() => {
|
|
669
|
+
setConfirmationMode(true);
|
|
670
|
+
if (confirmationTimeoutRef.current) {
|
|
671
|
+
clearTimeout(confirmationTimeoutRef.current);
|
|
672
|
+
}
|
|
673
|
+
confirmationTimeoutRef.current = setTimeout(() => {
|
|
674
|
+
resetConfirmation();
|
|
675
|
+
}, 15e3);
|
|
676
|
+
}, [resetConfirmation]);
|
|
677
|
+
useInput(useCallback(async (input, key) => {
|
|
678
|
+
if (actionInProgress) return;
|
|
679
|
+
if (key.ctrl && input === "c") {
|
|
680
|
+
if (confirmationMode) {
|
|
681
|
+
resetConfirmation();
|
|
682
|
+
setActionInProgress(true);
|
|
683
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
684
|
+
onExit?.();
|
|
685
|
+
} else {
|
|
686
|
+
setConfirmationWithTimeout();
|
|
687
|
+
}
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
690
|
+
if (confirmationMode) {
|
|
691
|
+
resetConfirmation();
|
|
692
|
+
}
|
|
693
|
+
}, [confirmationMode, actionInProgress, onExit, setConfirmationWithTimeout, resetConfirmation]));
|
|
694
|
+
const getMessageColor = (type) => {
|
|
695
|
+
switch (type) {
|
|
696
|
+
case "user":
|
|
697
|
+
return "magenta";
|
|
698
|
+
case "assistant":
|
|
699
|
+
return "cyan";
|
|
700
|
+
case "system":
|
|
701
|
+
return "blue";
|
|
702
|
+
case "tool":
|
|
703
|
+
return "yellow";
|
|
704
|
+
case "result":
|
|
705
|
+
return "green";
|
|
706
|
+
case "status":
|
|
707
|
+
return "gray";
|
|
708
|
+
default:
|
|
709
|
+
return "white";
|
|
710
|
+
}
|
|
711
|
+
};
|
|
712
|
+
const formatMessage = (msg) => {
|
|
713
|
+
const lines = msg.content.split("\n");
|
|
714
|
+
const maxLineLength = terminalWidth - 10;
|
|
715
|
+
return lines.map((line) => {
|
|
716
|
+
if (line.length <= maxLineLength) return line;
|
|
717
|
+
const chunks = [];
|
|
718
|
+
for (let i = 0; i < line.length; i += maxLineLength) {
|
|
719
|
+
chunks.push(line.slice(i, i + maxLineLength));
|
|
720
|
+
}
|
|
721
|
+
return chunks.join("\n");
|
|
722
|
+
}).join("\n");
|
|
723
|
+
};
|
|
724
|
+
return /* @__PURE__ */ React.createElement(Box, { flexDirection: "column", width: terminalWidth, height: terminalHeight }, /* @__PURE__ */ React.createElement(
|
|
725
|
+
Box,
|
|
726
|
+
{
|
|
727
|
+
flexDirection: "column",
|
|
728
|
+
width: terminalWidth,
|
|
729
|
+
height: terminalHeight - 4,
|
|
730
|
+
borderStyle: "round",
|
|
731
|
+
borderColor: "gray",
|
|
732
|
+
paddingX: 1,
|
|
733
|
+
overflow: "hidden"
|
|
734
|
+
},
|
|
735
|
+
/* @__PURE__ */ React.createElement(Box, { flexDirection: "column", marginBottom: 1 }, /* @__PURE__ */ React.createElement(Text, { color: "gray", bold: true }, "\u{1F916} Codex Agent Messages"), /* @__PURE__ */ React.createElement(Text, { color: "gray", dimColor: true }, "\u2500".repeat(Math.min(terminalWidth - 4, 60)))),
|
|
736
|
+
/* @__PURE__ */ React.createElement(Box, { flexDirection: "column", height: terminalHeight - 10, overflow: "hidden" }, messages.length === 0 ? /* @__PURE__ */ React.createElement(Text, { color: "gray", dimColor: true }, "Waiting for messages...") : (
|
|
737
|
+
// Show only the last messages that fit in the available space
|
|
738
|
+
messages.slice(-Math.max(1, terminalHeight - 10)).map((msg) => /* @__PURE__ */ React.createElement(Box, { key: msg.id, flexDirection: "column", marginBottom: 1 }, /* @__PURE__ */ React.createElement(Text, { color: getMessageColor(msg.type), dimColor: true }, formatMessage(msg))))
|
|
739
|
+
))
|
|
740
|
+
), /* @__PURE__ */ React.createElement(
|
|
741
|
+
Box,
|
|
742
|
+
{
|
|
743
|
+
width: terminalWidth,
|
|
744
|
+
borderStyle: "round",
|
|
745
|
+
borderColor: actionInProgress ? "gray" : confirmationMode ? "red" : "green",
|
|
746
|
+
paddingX: 2,
|
|
747
|
+
justifyContent: "center",
|
|
748
|
+
alignItems: "center",
|
|
749
|
+
flexDirection: "column"
|
|
750
|
+
},
|
|
751
|
+
/* @__PURE__ */ React.createElement(Box, { flexDirection: "column", alignItems: "center" }, actionInProgress ? /* @__PURE__ */ React.createElement(Text, { color: "gray", bold: true }, "Exiting agent...") : confirmationMode ? /* @__PURE__ */ React.createElement(Text, { color: "red", bold: true }, "\u26A0\uFE0F Press Ctrl-C again to exit the agent") : /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Text, { color: "green", bold: true }, "\u{1F916} Codex Agent Running \u2022 Ctrl-C to exit")), process.env.DEBUG && logPath && /* @__PURE__ */ React.createElement(Text, { color: "gray", dimColor: true }, "Debug logs: ", logPath))
|
|
752
|
+
));
|
|
753
|
+
};
|
|
754
|
+
|
|
755
|
+
function emitReadyIfIdle({ pending, queueSize, shouldExit, sendReady, notify }) {
|
|
756
|
+
if (shouldExit) {
|
|
757
|
+
return false;
|
|
758
|
+
}
|
|
759
|
+
if (pending) {
|
|
760
|
+
return false;
|
|
761
|
+
}
|
|
762
|
+
if (queueSize() > 0) {
|
|
763
|
+
return false;
|
|
764
|
+
}
|
|
765
|
+
sendReady();
|
|
766
|
+
notify?.();
|
|
767
|
+
return true;
|
|
768
|
+
}
|
|
769
|
+
async function runCodex(opts) {
|
|
770
|
+
const sessionTag = randomUUID();
|
|
771
|
+
const api = await ApiClient.create(opts.credentials);
|
|
772
|
+
logger.debug(`[codex] Starting with options: startedBy=${opts.startedBy || "terminal"}`);
|
|
773
|
+
const settings = await readSettings();
|
|
774
|
+
let machineId = settings?.machineId;
|
|
775
|
+
if (!machineId) {
|
|
776
|
+
console.error(`[START] No machine ID found in settings, which is unexpected since authAndSetupMachineIfNeeded should have created it. Please report this issue on https://github.com/slopus/happy-cli/issues`);
|
|
777
|
+
process.exit(1);
|
|
778
|
+
}
|
|
779
|
+
logger.debug(`Using machineId: ${machineId}`);
|
|
780
|
+
await api.getOrCreateMachine({
|
|
781
|
+
machineId,
|
|
782
|
+
metadata: initialMachineMetadata
|
|
783
|
+
});
|
|
784
|
+
let state = {
|
|
785
|
+
controlledByUser: false
|
|
786
|
+
};
|
|
787
|
+
let metadata = {
|
|
788
|
+
path: process.cwd(),
|
|
789
|
+
host: os.hostname(),
|
|
790
|
+
version: packageJson.version,
|
|
791
|
+
os: os.platform(),
|
|
792
|
+
machineId,
|
|
793
|
+
homeDir: os.homedir(),
|
|
794
|
+
happyHomeDir: configuration.happyHomeDir,
|
|
795
|
+
happyLibDir: projectPath(),
|
|
796
|
+
happyToolsDir: resolve(projectPath(), "tools", "unpacked"),
|
|
797
|
+
startedFromDaemon: opts.startedBy === "daemon",
|
|
798
|
+
hostPid: process.pid,
|
|
799
|
+
startedBy: opts.startedBy || "terminal",
|
|
800
|
+
// Initialize lifecycle state
|
|
801
|
+
lifecycleState: "running",
|
|
802
|
+
lifecycleStateSince: Date.now(),
|
|
803
|
+
flavor: "codex"
|
|
804
|
+
};
|
|
805
|
+
const response = await api.getOrCreateSession({ tag: sessionTag, metadata, state });
|
|
806
|
+
const session = api.sessionSyncClient(response);
|
|
807
|
+
try {
|
|
808
|
+
logger.debug(`[START] Reporting session ${response.id} to daemon`);
|
|
809
|
+
const result = await notifyDaemonSessionStarted(response.id, metadata);
|
|
810
|
+
if (result.error) {
|
|
811
|
+
logger.debug(`[START] Failed to report to daemon (may not be running):`, result.error);
|
|
812
|
+
} else {
|
|
813
|
+
logger.debug(`[START] Reported session ${response.id} to daemon`);
|
|
814
|
+
}
|
|
815
|
+
} catch (error) {
|
|
816
|
+
logger.debug("[START] Failed to report to daemon (may not be running):", error);
|
|
817
|
+
}
|
|
818
|
+
const messageQueue = new MessageQueue2((mode) => hashObject({
|
|
819
|
+
permissionMode: mode.permissionMode,
|
|
820
|
+
model: mode.model
|
|
821
|
+
}));
|
|
822
|
+
let currentPermissionMode = void 0;
|
|
823
|
+
let currentModel = void 0;
|
|
824
|
+
session.onUserMessage((message) => {
|
|
825
|
+
let messagePermissionMode = currentPermissionMode;
|
|
826
|
+
if (message.meta?.permissionMode) {
|
|
827
|
+
const validModes = ["default", "read-only", "safe-yolo", "yolo"];
|
|
828
|
+
if (validModes.includes(message.meta.permissionMode)) {
|
|
829
|
+
messagePermissionMode = message.meta.permissionMode;
|
|
830
|
+
currentPermissionMode = messagePermissionMode;
|
|
831
|
+
logger.debug(`[Codex] Permission mode updated from user message to: ${currentPermissionMode}`);
|
|
832
|
+
} else {
|
|
833
|
+
logger.debug(`[Codex] Invalid permission mode received: ${message.meta.permissionMode}`);
|
|
834
|
+
}
|
|
835
|
+
} else {
|
|
836
|
+
logger.debug(`[Codex] User message received with no permission mode override, using current: ${currentPermissionMode ?? "default (effective)"}`);
|
|
837
|
+
}
|
|
838
|
+
let messageModel = currentModel;
|
|
839
|
+
if (message.meta?.hasOwnProperty("model")) {
|
|
840
|
+
messageModel = message.meta.model || void 0;
|
|
841
|
+
currentModel = messageModel;
|
|
842
|
+
logger.debug(`[Codex] Model updated from user message: ${messageModel || "reset to default"}`);
|
|
843
|
+
} else {
|
|
844
|
+
logger.debug(`[Codex] User message received with no model override, using current: ${currentModel || "default"}`);
|
|
845
|
+
}
|
|
846
|
+
const enhancedMode = {
|
|
847
|
+
permissionMode: messagePermissionMode || "default",
|
|
848
|
+
model: messageModel
|
|
849
|
+
};
|
|
850
|
+
messageQueue.push(message.content.text, enhancedMode);
|
|
851
|
+
});
|
|
852
|
+
let thinking = false;
|
|
853
|
+
session.keepAlive(thinking, "remote");
|
|
854
|
+
const keepAliveInterval = setInterval(() => {
|
|
855
|
+
session.keepAlive(thinking, "remote");
|
|
856
|
+
}, 2e3);
|
|
857
|
+
const sendReady = () => {
|
|
858
|
+
session.sendSessionEvent({ type: "ready" });
|
|
859
|
+
try {
|
|
860
|
+
api.push().sendToAllDevices(
|
|
861
|
+
"It's ready!",
|
|
862
|
+
"Codex is waiting for your command",
|
|
863
|
+
{ sessionId: session.sessionId }
|
|
864
|
+
);
|
|
865
|
+
} catch (pushError) {
|
|
866
|
+
logger.debug("[Codex] Failed to send ready push", pushError);
|
|
867
|
+
}
|
|
868
|
+
};
|
|
869
|
+
function logActiveHandles(tag) {
|
|
870
|
+
if (!process.env.DEBUG) return;
|
|
871
|
+
const anyProc = process;
|
|
872
|
+
const handles = typeof anyProc._getActiveHandles === "function" ? anyProc._getActiveHandles() : [];
|
|
873
|
+
const requests = typeof anyProc._getActiveRequests === "function" ? anyProc._getActiveRequests() : [];
|
|
874
|
+
logger.debug(`[codex][handles] ${tag}: handles=${handles.length} requests=${requests.length}`);
|
|
875
|
+
try {
|
|
876
|
+
const kinds = handles.map((h) => h && h.constructor ? h.constructor.name : typeof h);
|
|
877
|
+
logger.debug(`[codex][handles] kinds=${JSON.stringify(kinds)}`);
|
|
878
|
+
} catch {
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
let abortController = new AbortController();
|
|
882
|
+
let shouldExit = false;
|
|
883
|
+
let storedSessionIdForResume = null;
|
|
884
|
+
async function handleAbort() {
|
|
885
|
+
logger.debug("[Codex] Abort requested - stopping current task");
|
|
886
|
+
try {
|
|
887
|
+
if (client.hasActiveSession()) {
|
|
888
|
+
storedSessionIdForResume = client.storeSessionForResume();
|
|
889
|
+
logger.debug("[Codex] Stored session for resume:", storedSessionIdForResume);
|
|
890
|
+
}
|
|
891
|
+
abortController.abort();
|
|
892
|
+
messageQueue.reset();
|
|
893
|
+
permissionHandler.reset();
|
|
894
|
+
reasoningProcessor.abort();
|
|
895
|
+
diffProcessor.reset();
|
|
896
|
+
logger.debug("[Codex] Abort completed - session remains active");
|
|
897
|
+
} catch (error) {
|
|
898
|
+
logger.debug("[Codex] Error during abort:", error);
|
|
899
|
+
} finally {
|
|
900
|
+
abortController = new AbortController();
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
const handleKillSession = async () => {
|
|
904
|
+
logger.debug("[Codex] Kill session requested - terminating process");
|
|
905
|
+
await handleAbort();
|
|
906
|
+
logger.debug("[Codex] Abort completed, proceeding with termination");
|
|
907
|
+
try {
|
|
908
|
+
if (session) {
|
|
909
|
+
session.updateMetadata((currentMetadata) => ({
|
|
910
|
+
...currentMetadata,
|
|
911
|
+
lifecycleState: "archived",
|
|
912
|
+
lifecycleStateSince: Date.now(),
|
|
913
|
+
archivedBy: "cli",
|
|
914
|
+
archiveReason: "User terminated"
|
|
915
|
+
}));
|
|
916
|
+
session.sendSessionDeath();
|
|
917
|
+
await session.flush();
|
|
918
|
+
await session.close();
|
|
919
|
+
}
|
|
920
|
+
stopCaffeinate();
|
|
921
|
+
happyServer.stop();
|
|
922
|
+
logger.debug("[Codex] Session termination complete, exiting");
|
|
923
|
+
process.exit(0);
|
|
924
|
+
} catch (error) {
|
|
925
|
+
logger.debug("[Codex] Error during session termination:", error);
|
|
926
|
+
process.exit(1);
|
|
927
|
+
}
|
|
928
|
+
};
|
|
929
|
+
session.rpcHandlerManager.registerHandler("abort", handleAbort);
|
|
930
|
+
registerKillSessionHandler(session.rpcHandlerManager, handleKillSession);
|
|
931
|
+
const messageBuffer = new MessageBuffer();
|
|
932
|
+
const hasTTY = process.stdout.isTTY && process.stdin.isTTY;
|
|
933
|
+
let inkInstance = null;
|
|
934
|
+
if (hasTTY) {
|
|
935
|
+
console.clear();
|
|
936
|
+
inkInstance = render(React.createElement(CodexDisplay, {
|
|
937
|
+
messageBuffer,
|
|
938
|
+
logPath: process.env.DEBUG ? logger.getLogPath() : void 0,
|
|
939
|
+
onExit: async () => {
|
|
940
|
+
logger.debug("[codex]: Exiting agent via Ctrl-C");
|
|
941
|
+
shouldExit = true;
|
|
942
|
+
await handleAbort();
|
|
943
|
+
}
|
|
944
|
+
}), {
|
|
945
|
+
exitOnCtrlC: false,
|
|
946
|
+
patchConsole: false
|
|
947
|
+
});
|
|
948
|
+
}
|
|
949
|
+
if (hasTTY) {
|
|
950
|
+
process.stdin.resume();
|
|
951
|
+
if (process.stdin.isTTY) {
|
|
952
|
+
process.stdin.setRawMode(true);
|
|
953
|
+
}
|
|
954
|
+
process.stdin.setEncoding("utf8");
|
|
955
|
+
}
|
|
956
|
+
const client = new CodexMcpClient();
|
|
957
|
+
function findCodexResumeFile(sessionId) {
|
|
958
|
+
if (!sessionId) return null;
|
|
959
|
+
try {
|
|
960
|
+
let collectFilesRecursive2 = function(dir, acc = []) {
|
|
961
|
+
let entries;
|
|
962
|
+
try {
|
|
963
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
964
|
+
} catch {
|
|
965
|
+
return acc;
|
|
966
|
+
}
|
|
967
|
+
for (const entry of entries) {
|
|
968
|
+
const full = join(dir, entry.name);
|
|
969
|
+
if (entry.isDirectory()) {
|
|
970
|
+
collectFilesRecursive2(full, acc);
|
|
971
|
+
} else if (entry.isFile()) {
|
|
972
|
+
acc.push(full);
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
return acc;
|
|
976
|
+
};
|
|
977
|
+
var collectFilesRecursive = collectFilesRecursive2;
|
|
978
|
+
const codexHomeDir = process.env.CODEX_HOME || join(os.homedir(), ".codex");
|
|
979
|
+
const rootDir = join(codexHomeDir, "sessions");
|
|
980
|
+
const candidates = collectFilesRecursive2(rootDir).filter((full) => full.endsWith(`-${sessionId}.jsonl`)).filter((full) => {
|
|
981
|
+
try {
|
|
982
|
+
return fs.statSync(full).isFile();
|
|
983
|
+
} catch {
|
|
984
|
+
return false;
|
|
985
|
+
}
|
|
986
|
+
}).sort((a, b) => {
|
|
987
|
+
const sa = fs.statSync(a).mtimeMs;
|
|
988
|
+
const sb = fs.statSync(b).mtimeMs;
|
|
989
|
+
return sb - sa;
|
|
990
|
+
});
|
|
991
|
+
return candidates[0] || null;
|
|
992
|
+
} catch {
|
|
993
|
+
return null;
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
const permissionHandler = new CodexPermissionHandler(session);
|
|
997
|
+
const reasoningProcessor = new ReasoningProcessor((message) => {
|
|
998
|
+
session.sendCodexMessage(message);
|
|
999
|
+
});
|
|
1000
|
+
const diffProcessor = new DiffProcessor((message) => {
|
|
1001
|
+
session.sendCodexMessage(message);
|
|
1002
|
+
});
|
|
1003
|
+
client.setPermissionHandler(permissionHandler);
|
|
1004
|
+
client.setHandler((msg) => {
|
|
1005
|
+
logger.debug(`[Codex] MCP message: ${JSON.stringify(msg)}`);
|
|
1006
|
+
if (msg.type === "agent_message") {
|
|
1007
|
+
messageBuffer.addMessage(msg.message, "assistant");
|
|
1008
|
+
} else if (msg.type === "agent_reasoning_delta") ; else if (msg.type === "agent_reasoning") {
|
|
1009
|
+
messageBuffer.addMessage(`[Thinking] ${msg.text.substring(0, 100)}...`, "system");
|
|
1010
|
+
} else if (msg.type === "exec_command_begin") {
|
|
1011
|
+
messageBuffer.addMessage(`Executing: ${msg.command}`, "tool");
|
|
1012
|
+
} else if (msg.type === "exec_command_end") {
|
|
1013
|
+
const output = msg.output || msg.error || "Command completed";
|
|
1014
|
+
const truncatedOutput = output.substring(0, 200);
|
|
1015
|
+
messageBuffer.addMessage(
|
|
1016
|
+
`Result: ${truncatedOutput}${output.length > 200 ? "..." : ""}`,
|
|
1017
|
+
"result"
|
|
1018
|
+
);
|
|
1019
|
+
} else if (msg.type === "task_started") {
|
|
1020
|
+
messageBuffer.addMessage("Starting task...", "status");
|
|
1021
|
+
} else if (msg.type === "task_complete") {
|
|
1022
|
+
messageBuffer.addMessage("Task completed", "status");
|
|
1023
|
+
sendReady();
|
|
1024
|
+
} else if (msg.type === "turn_aborted") {
|
|
1025
|
+
messageBuffer.addMessage("Turn aborted", "status");
|
|
1026
|
+
sendReady();
|
|
1027
|
+
}
|
|
1028
|
+
if (msg.type === "task_started") {
|
|
1029
|
+
if (!thinking) {
|
|
1030
|
+
logger.debug("thinking started");
|
|
1031
|
+
thinking = true;
|
|
1032
|
+
session.keepAlive(thinking, "remote");
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
if (msg.type === "task_complete" || msg.type === "turn_aborted") {
|
|
1036
|
+
if (thinking) {
|
|
1037
|
+
logger.debug("thinking completed");
|
|
1038
|
+
thinking = false;
|
|
1039
|
+
session.keepAlive(thinking, "remote");
|
|
1040
|
+
}
|
|
1041
|
+
diffProcessor.reset();
|
|
1042
|
+
}
|
|
1043
|
+
if (msg.type === "agent_reasoning_section_break") {
|
|
1044
|
+
reasoningProcessor.handleSectionBreak();
|
|
1045
|
+
}
|
|
1046
|
+
if (msg.type === "agent_reasoning_delta") {
|
|
1047
|
+
reasoningProcessor.processDelta(msg.delta);
|
|
1048
|
+
}
|
|
1049
|
+
if (msg.type === "agent_reasoning") {
|
|
1050
|
+
reasoningProcessor.complete(msg.text);
|
|
1051
|
+
}
|
|
1052
|
+
if (msg.type === "agent_message") {
|
|
1053
|
+
session.sendCodexMessage({
|
|
1054
|
+
type: "message",
|
|
1055
|
+
message: msg.message,
|
|
1056
|
+
id: randomUUID()
|
|
1057
|
+
});
|
|
1058
|
+
}
|
|
1059
|
+
if (msg.type === "exec_command_begin" || msg.type === "exec_approval_request") {
|
|
1060
|
+
let { call_id, type, ...inputs } = msg;
|
|
1061
|
+
session.sendCodexMessage({
|
|
1062
|
+
type: "tool-call",
|
|
1063
|
+
name: "CodexBash",
|
|
1064
|
+
callId: call_id,
|
|
1065
|
+
input: inputs,
|
|
1066
|
+
id: randomUUID()
|
|
1067
|
+
});
|
|
1068
|
+
}
|
|
1069
|
+
if (msg.type === "exec_command_end") {
|
|
1070
|
+
let { call_id, type, ...output } = msg;
|
|
1071
|
+
session.sendCodexMessage({
|
|
1072
|
+
type: "tool-call-result",
|
|
1073
|
+
callId: call_id,
|
|
1074
|
+
output,
|
|
1075
|
+
id: randomUUID()
|
|
1076
|
+
});
|
|
1077
|
+
}
|
|
1078
|
+
if (msg.type === "token_count") {
|
|
1079
|
+
session.sendCodexMessage({
|
|
1080
|
+
...msg,
|
|
1081
|
+
id: randomUUID()
|
|
1082
|
+
});
|
|
1083
|
+
}
|
|
1084
|
+
if (msg.type === "patch_apply_begin") {
|
|
1085
|
+
let { call_id, auto_approved, changes } = msg;
|
|
1086
|
+
const changeCount = Object.keys(changes).length;
|
|
1087
|
+
const filesMsg = changeCount === 1 ? "1 file" : `${changeCount} files`;
|
|
1088
|
+
messageBuffer.addMessage(`Modifying ${filesMsg}...`, "tool");
|
|
1089
|
+
session.sendCodexMessage({
|
|
1090
|
+
type: "tool-call",
|
|
1091
|
+
name: "CodexPatch",
|
|
1092
|
+
callId: call_id,
|
|
1093
|
+
input: {
|
|
1094
|
+
auto_approved,
|
|
1095
|
+
changes
|
|
1096
|
+
},
|
|
1097
|
+
id: randomUUID()
|
|
1098
|
+
});
|
|
1099
|
+
}
|
|
1100
|
+
if (msg.type === "patch_apply_end") {
|
|
1101
|
+
let { call_id, stdout, stderr, success } = msg;
|
|
1102
|
+
if (success) {
|
|
1103
|
+
const message = stdout || "Files modified successfully";
|
|
1104
|
+
messageBuffer.addMessage(message.substring(0, 200), "result");
|
|
1105
|
+
} else {
|
|
1106
|
+
const errorMsg = stderr || "Failed to modify files";
|
|
1107
|
+
messageBuffer.addMessage(`Error: ${errorMsg.substring(0, 200)}`, "result");
|
|
1108
|
+
}
|
|
1109
|
+
session.sendCodexMessage({
|
|
1110
|
+
type: "tool-call-result",
|
|
1111
|
+
callId: call_id,
|
|
1112
|
+
output: {
|
|
1113
|
+
stdout,
|
|
1114
|
+
stderr,
|
|
1115
|
+
success
|
|
1116
|
+
},
|
|
1117
|
+
id: randomUUID()
|
|
1118
|
+
});
|
|
1119
|
+
}
|
|
1120
|
+
if (msg.type === "turn_diff") {
|
|
1121
|
+
if (msg.unified_diff) {
|
|
1122
|
+
diffProcessor.processDiff(msg.unified_diff);
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
});
|
|
1126
|
+
const happyServer = await startHappyServer(session);
|
|
1127
|
+
const bridgeCommand = join(projectPath(), "bin", "happy-mcp.mjs");
|
|
1128
|
+
const mcpServers = {
|
|
1129
|
+
happy: {
|
|
1130
|
+
command: bridgeCommand,
|
|
1131
|
+
args: ["--url", happyServer.url]
|
|
1132
|
+
}
|
|
1133
|
+
};
|
|
1134
|
+
let first = true;
|
|
1135
|
+
try {
|
|
1136
|
+
logger.debug("[codex]: client.connect begin");
|
|
1137
|
+
await client.connect();
|
|
1138
|
+
logger.debug("[codex]: client.connect done");
|
|
1139
|
+
let wasCreated = false;
|
|
1140
|
+
let currentModeHash = null;
|
|
1141
|
+
let pending = null;
|
|
1142
|
+
let nextExperimentalResume = null;
|
|
1143
|
+
while (!shouldExit) {
|
|
1144
|
+
logActiveHandles("loop-top");
|
|
1145
|
+
let message = pending;
|
|
1146
|
+
pending = null;
|
|
1147
|
+
if (!message) {
|
|
1148
|
+
const waitSignal = abortController.signal;
|
|
1149
|
+
const batch = await messageQueue.waitForMessagesAndGetAsString(waitSignal);
|
|
1150
|
+
if (!batch) {
|
|
1151
|
+
if (waitSignal.aborted && !shouldExit) {
|
|
1152
|
+
logger.debug("[codex]: Wait aborted while idle; ignoring and continuing");
|
|
1153
|
+
continue;
|
|
1154
|
+
}
|
|
1155
|
+
logger.debug(`[codex]: batch=${!!batch}, shouldExit=${shouldExit}`);
|
|
1156
|
+
break;
|
|
1157
|
+
}
|
|
1158
|
+
message = batch;
|
|
1159
|
+
}
|
|
1160
|
+
if (!message) {
|
|
1161
|
+
break;
|
|
1162
|
+
}
|
|
1163
|
+
if (wasCreated && currentModeHash && message.hash !== currentModeHash) {
|
|
1164
|
+
logger.debug("[Codex] Mode changed \u2013 restarting Codex session");
|
|
1165
|
+
messageBuffer.addMessage("\u2550".repeat(40), "status");
|
|
1166
|
+
messageBuffer.addMessage("Starting new Codex session (mode changed)...", "status");
|
|
1167
|
+
try {
|
|
1168
|
+
const prevSessionId = client.getSessionId();
|
|
1169
|
+
nextExperimentalResume = findCodexResumeFile(prevSessionId);
|
|
1170
|
+
if (nextExperimentalResume) {
|
|
1171
|
+
logger.debug(`[Codex] Found resume file for session ${prevSessionId}: ${nextExperimentalResume}`);
|
|
1172
|
+
messageBuffer.addMessage("Resuming previous context\u2026", "status");
|
|
1173
|
+
} else {
|
|
1174
|
+
logger.debug("[Codex] No resume file found for previous session");
|
|
1175
|
+
}
|
|
1176
|
+
} catch (e) {
|
|
1177
|
+
logger.debug("[Codex] Error while searching resume file", e);
|
|
1178
|
+
}
|
|
1179
|
+
client.clearSession();
|
|
1180
|
+
wasCreated = false;
|
|
1181
|
+
currentModeHash = null;
|
|
1182
|
+
pending = message;
|
|
1183
|
+
permissionHandler.reset();
|
|
1184
|
+
reasoningProcessor.abort();
|
|
1185
|
+
diffProcessor.reset();
|
|
1186
|
+
thinking = false;
|
|
1187
|
+
session.keepAlive(thinking, "remote");
|
|
1188
|
+
continue;
|
|
1189
|
+
}
|
|
1190
|
+
messageBuffer.addMessage(message.message, "user");
|
|
1191
|
+
currentModeHash = message.hash;
|
|
1192
|
+
try {
|
|
1193
|
+
const approvalPolicy = (() => {
|
|
1194
|
+
switch (message.mode.permissionMode) {
|
|
1195
|
+
case "default":
|
|
1196
|
+
return "untrusted";
|
|
1197
|
+
case "read-only":
|
|
1198
|
+
return "never";
|
|
1199
|
+
case "safe-yolo":
|
|
1200
|
+
return "on-failure";
|
|
1201
|
+
case "yolo":
|
|
1202
|
+
return "on-failure";
|
|
1203
|
+
}
|
|
1204
|
+
})();
|
|
1205
|
+
const sandbox = (() => {
|
|
1206
|
+
switch (message.mode.permissionMode) {
|
|
1207
|
+
case "default":
|
|
1208
|
+
return "workspace-write";
|
|
1209
|
+
case "read-only":
|
|
1210
|
+
return "read-only";
|
|
1211
|
+
case "safe-yolo":
|
|
1212
|
+
return "workspace-write";
|
|
1213
|
+
case "yolo":
|
|
1214
|
+
return "danger-full-access";
|
|
1215
|
+
}
|
|
1216
|
+
})();
|
|
1217
|
+
if (!wasCreated) {
|
|
1218
|
+
const startConfig = {
|
|
1219
|
+
prompt: first ? message.message + "\n\n" + trimIdent(`Based on this message, call functions.happy__change_title to change chat session title that would represent the current task. If chat idea would change dramatically - call this function again to update the title.`) : message.message,
|
|
1220
|
+
sandbox,
|
|
1221
|
+
"approval-policy": approvalPolicy,
|
|
1222
|
+
config: { mcp_servers: mcpServers }
|
|
1223
|
+
};
|
|
1224
|
+
if (message.mode.model) {
|
|
1225
|
+
startConfig.model = message.mode.model;
|
|
1226
|
+
}
|
|
1227
|
+
let resumeFile = null;
|
|
1228
|
+
if (nextExperimentalResume) {
|
|
1229
|
+
resumeFile = nextExperimentalResume;
|
|
1230
|
+
nextExperimentalResume = null;
|
|
1231
|
+
logger.debug("[Codex] Using resume file from mode change:", resumeFile);
|
|
1232
|
+
} else if (storedSessionIdForResume) {
|
|
1233
|
+
const abortResumeFile = findCodexResumeFile(storedSessionIdForResume);
|
|
1234
|
+
if (abortResumeFile) {
|
|
1235
|
+
resumeFile = abortResumeFile;
|
|
1236
|
+
logger.debug("[Codex] Using resume file from aborted session:", resumeFile);
|
|
1237
|
+
messageBuffer.addMessage("Resuming from aborted session...", "status");
|
|
1238
|
+
}
|
|
1239
|
+
storedSessionIdForResume = null;
|
|
1240
|
+
}
|
|
1241
|
+
if (resumeFile) {
|
|
1242
|
+
startConfig.config.experimental_resume = resumeFile;
|
|
1243
|
+
}
|
|
1244
|
+
await client.startSession(
|
|
1245
|
+
startConfig,
|
|
1246
|
+
{ signal: abortController.signal }
|
|
1247
|
+
);
|
|
1248
|
+
wasCreated = true;
|
|
1249
|
+
first = false;
|
|
1250
|
+
} else {
|
|
1251
|
+
const response2 = await client.continueSession(
|
|
1252
|
+
message.message,
|
|
1253
|
+
{ signal: abortController.signal }
|
|
1254
|
+
);
|
|
1255
|
+
logger.debug("[Codex] continueSession response:", response2);
|
|
1256
|
+
}
|
|
1257
|
+
} catch (error) {
|
|
1258
|
+
logger.warn("Error in codex session:", error);
|
|
1259
|
+
const isAbortError = error instanceof Error && error.name === "AbortError";
|
|
1260
|
+
if (isAbortError) {
|
|
1261
|
+
messageBuffer.addMessage("Aborted by user", "status");
|
|
1262
|
+
session.sendSessionEvent({ type: "message", message: "Aborted by user" });
|
|
1263
|
+
wasCreated = false;
|
|
1264
|
+
currentModeHash = null;
|
|
1265
|
+
logger.debug("[Codex] Marked session as not created after abort for proper resume");
|
|
1266
|
+
} else {
|
|
1267
|
+
messageBuffer.addMessage("Process exited unexpectedly", "status");
|
|
1268
|
+
session.sendSessionEvent({ type: "message", message: "Process exited unexpectedly" });
|
|
1269
|
+
if (client.hasActiveSession()) {
|
|
1270
|
+
storedSessionIdForResume = client.storeSessionForResume();
|
|
1271
|
+
logger.debug("[Codex] Stored session after unexpected error:", storedSessionIdForResume);
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
} finally {
|
|
1275
|
+
permissionHandler.reset();
|
|
1276
|
+
reasoningProcessor.abort();
|
|
1277
|
+
diffProcessor.reset();
|
|
1278
|
+
thinking = false;
|
|
1279
|
+
session.keepAlive(thinking, "remote");
|
|
1280
|
+
emitReadyIfIdle({
|
|
1281
|
+
pending,
|
|
1282
|
+
queueSize: () => messageQueue.size(),
|
|
1283
|
+
shouldExit,
|
|
1284
|
+
sendReady
|
|
1285
|
+
});
|
|
1286
|
+
logActiveHandles("after-turn");
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
} finally {
|
|
1290
|
+
logger.debug("[codex]: Final cleanup start");
|
|
1291
|
+
logActiveHandles("cleanup-start");
|
|
1292
|
+
try {
|
|
1293
|
+
logger.debug("[codex]: sendSessionDeath");
|
|
1294
|
+
session.sendSessionDeath();
|
|
1295
|
+
logger.debug("[codex]: flush begin");
|
|
1296
|
+
await session.flush();
|
|
1297
|
+
logger.debug("[codex]: flush done");
|
|
1298
|
+
logger.debug("[codex]: session.close begin");
|
|
1299
|
+
await session.close();
|
|
1300
|
+
logger.debug("[codex]: session.close done");
|
|
1301
|
+
} catch (e) {
|
|
1302
|
+
logger.debug("[codex]: Error while closing session", e);
|
|
1303
|
+
}
|
|
1304
|
+
logger.debug("[codex]: client.disconnect begin");
|
|
1305
|
+
await client.disconnect();
|
|
1306
|
+
logger.debug("[codex]: client.disconnect done");
|
|
1307
|
+
logger.debug("[codex]: happyServer.stop");
|
|
1308
|
+
happyServer.stop();
|
|
1309
|
+
if (process.stdin.isTTY) {
|
|
1310
|
+
logger.debug("[codex]: setRawMode(false)");
|
|
1311
|
+
try {
|
|
1312
|
+
process.stdin.setRawMode(false);
|
|
1313
|
+
} catch {
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
if (hasTTY) {
|
|
1317
|
+
logger.debug("[codex]: stdin.pause()");
|
|
1318
|
+
try {
|
|
1319
|
+
process.stdin.pause();
|
|
1320
|
+
} catch {
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
logger.debug("[codex]: clearInterval(keepAlive)");
|
|
1324
|
+
clearInterval(keepAliveInterval);
|
|
1325
|
+
if (inkInstance) {
|
|
1326
|
+
logger.debug("[codex]: inkInstance.unmount()");
|
|
1327
|
+
inkInstance.unmount();
|
|
1328
|
+
}
|
|
1329
|
+
messageBuffer.clear();
|
|
1330
|
+
logActiveHandles("cleanup-end");
|
|
1331
|
+
logger.debug("[codex]: Final cleanup completed");
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
|
|
1335
|
+
export { emitReadyIfIdle, runCodex };
|