@triflux/remote 10.0.0-alpha.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/hub/pipe.mjs +579 -0
- package/hub/public/dashboard.html +355 -0
- package/hub/public/tray-icon.ico +0 -0
- package/hub/public/tray-icon.png +0 -0
- package/hub/server.mjs +1124 -0
- package/hub/store-adapter.mjs +851 -0
- package/hub/store.mjs +897 -0
- package/hub/team/agent-map.json +11 -0
- package/hub/team/ansi.mjs +379 -0
- package/hub/team/backend.mjs +90 -0
- package/hub/team/cli/commands/attach.mjs +37 -0
- package/hub/team/cli/commands/control.mjs +43 -0
- package/hub/team/cli/commands/debug.mjs +74 -0
- package/hub/team/cli/commands/focus.mjs +53 -0
- package/hub/team/cli/commands/interrupt.mjs +36 -0
- package/hub/team/cli/commands/kill.mjs +37 -0
- package/hub/team/cli/commands/list.mjs +24 -0
- package/hub/team/cli/commands/send.mjs +37 -0
- package/hub/team/cli/commands/start/index.mjs +106 -0
- package/hub/team/cli/commands/start/parse-args.mjs +130 -0
- package/hub/team/cli/commands/start/start-headless.mjs +109 -0
- package/hub/team/cli/commands/start/start-in-process.mjs +40 -0
- package/hub/team/cli/commands/start/start-mux.mjs +73 -0
- package/hub/team/cli/commands/start/start-wt.mjs +69 -0
- package/hub/team/cli/commands/status.mjs +87 -0
- package/hub/team/cli/commands/stop.mjs +31 -0
- package/hub/team/cli/commands/task.mjs +30 -0
- package/hub/team/cli/commands/tasks.mjs +13 -0
- package/hub/team/cli/help.mjs +42 -0
- package/hub/team/cli/index.mjs +41 -0
- package/hub/team/cli/manifest.mjs +29 -0
- package/hub/team/cli/render.mjs +30 -0
- package/hub/team/cli/services/attach-fallback.mjs +54 -0
- package/hub/team/cli/services/hub-client.mjs +208 -0
- package/hub/team/cli/services/member-selector.mjs +30 -0
- package/hub/team/cli/services/native-control.mjs +117 -0
- package/hub/team/cli/services/runtime-mode.mjs +62 -0
- package/hub/team/cli/services/state-store.mjs +48 -0
- package/hub/team/cli/services/task-model.mjs +30 -0
- package/hub/team/dashboard-anchor.mjs +14 -0
- package/hub/team/dashboard-layout.mjs +33 -0
- package/hub/team/dashboard-open.mjs +153 -0
- package/hub/team/dashboard.mjs +274 -0
- package/hub/team/handoff.mjs +303 -0
- package/hub/team/headless.mjs +1149 -0
- package/hub/team/native-supervisor.mjs +392 -0
- package/hub/team/native.mjs +649 -0
- package/hub/team/nativeProxy.mjs +681 -0
- package/hub/team/orchestrator.mjs +161 -0
- package/hub/team/pane.mjs +153 -0
- package/hub/team/psmux.mjs +1354 -0
- package/hub/team/routing.mjs +223 -0
- package/hub/team/session.mjs +611 -0
- package/hub/team/shared.mjs +13 -0
- package/hub/team/staleState.mjs +361 -0
- package/hub/team/tui-lite.mjs +380 -0
- package/hub/team/tui-viewer.mjs +463 -0
- package/hub/team/tui.mjs +1245 -0
- package/hub/tools.mjs +554 -0
- package/hub/tray.mjs +376 -0
- package/hub/workers/claude-worker.mjs +475 -0
- package/hub/workers/codex-mcp.mjs +504 -0
- package/hub/workers/delegator-mcp.mjs +1076 -0
- package/hub/workers/factory.mjs +21 -0
- package/hub/workers/gemini-worker.mjs +373 -0
- package/hub/workers/interface.mjs +52 -0
- package/hub/workers/worker-utils.mjs +104 -0
- package/package.json +31 -0
|
@@ -0,0 +1,504 @@
|
|
|
1
|
+
// hub/workers/codex-mcp.mjs — Codex MCP 서버 래퍼
|
|
2
|
+
import process from 'node:process';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
|
|
5
|
+
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
6
|
+
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
7
|
+
|
|
8
|
+
import { withRetry } from './worker-utils.mjs';
|
|
9
|
+
|
|
10
|
+
const REQUIRED_TOOLS = ['codex', 'codex-reply'];
|
|
11
|
+
|
|
12
|
+
export const CODEX_MCP_TRANSPORT_EXIT_CODE = 70;
|
|
13
|
+
export const CODEX_MCP_EXECUTION_EXIT_CODE = 1;
|
|
14
|
+
export const DEFAULT_CODEX_MCP_TIMEOUT_MS = 10 * 60 * 1000;
|
|
15
|
+
export const DEFAULT_CODEX_MCP_BOOTSTRAP_TIMEOUT_MS = 10 * 1000;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Codex MCP transport/bootstrap 계층 오류
|
|
19
|
+
*/
|
|
20
|
+
export class CodexMcpTransportError extends Error {
|
|
21
|
+
/**
|
|
22
|
+
* @param {string} message
|
|
23
|
+
* @param {object} [options]
|
|
24
|
+
* @param {unknown} [options.cause]
|
|
25
|
+
* @param {string} [options.stderr]
|
|
26
|
+
*/
|
|
27
|
+
constructor(message, options = {}) {
|
|
28
|
+
super(message, { cause: options.cause });
|
|
29
|
+
this.name = 'CodexMcpTransportError';
|
|
30
|
+
this.stderr = options.stderr || '';
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function cloneEnv(env = process.env) {
|
|
35
|
+
return Object.fromEntries(
|
|
36
|
+
Object.entries(env).filter(([, value]) => typeof value === 'string'),
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function collectTextContent(content = []) {
|
|
41
|
+
return content
|
|
42
|
+
.filter((item) => item?.type === 'text' && typeof item.text === 'string')
|
|
43
|
+
.map((item) => item.text)
|
|
44
|
+
.join('\n')
|
|
45
|
+
.trim();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function normalizeStructuredContent(structuredContent, fallbackText = '') {
|
|
49
|
+
if (!structuredContent || typeof structuredContent !== 'object') {
|
|
50
|
+
return { threadId: null, content: fallbackText };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const threadId = typeof structuredContent.threadId === 'string'
|
|
54
|
+
? structuredContent.threadId
|
|
55
|
+
: null;
|
|
56
|
+
const content = typeof structuredContent.content === 'string'
|
|
57
|
+
? structuredContent.content
|
|
58
|
+
: fallbackText;
|
|
59
|
+
|
|
60
|
+
return { threadId, content };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function buildCodexArguments(prompt, opts = {}) {
|
|
64
|
+
const args = { prompt };
|
|
65
|
+
|
|
66
|
+
if (typeof opts.cwd === 'string' && opts.cwd) args.cwd = opts.cwd;
|
|
67
|
+
if (typeof opts.model === 'string' && opts.model) args.model = opts.model;
|
|
68
|
+
if (typeof opts.profile === 'string' && opts.profile) args.profile = opts.profile;
|
|
69
|
+
if (typeof opts.approvalPolicy === 'string' && opts.approvalPolicy) {
|
|
70
|
+
args['approval-policy'] = opts.approvalPolicy;
|
|
71
|
+
}
|
|
72
|
+
if (typeof opts.sandbox === 'string' && opts.sandbox) args.sandbox = opts.sandbox;
|
|
73
|
+
if (opts.config && typeof opts.config === 'object') args.config = opts.config;
|
|
74
|
+
if (typeof opts.baseInstructions === 'string' && opts.baseInstructions) {
|
|
75
|
+
args['base-instructions'] = opts.baseInstructions;
|
|
76
|
+
}
|
|
77
|
+
if (typeof opts.developerInstructions === 'string' && opts.developerInstructions) {
|
|
78
|
+
args['developer-instructions'] = opts.developerInstructions;
|
|
79
|
+
}
|
|
80
|
+
if (typeof opts.compactPrompt === 'string' && opts.compactPrompt) {
|
|
81
|
+
args['compact-prompt'] = opts.compactPrompt;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return args;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function pickToolName(threadId) {
|
|
88
|
+
return threadId ? 'codex-reply' : 'codex';
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function withTimeout(promise, timeoutMs, message) {
|
|
92
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
|
|
93
|
+
return promise;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
let timer;
|
|
97
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
98
|
+
timer = setTimeout(() => {
|
|
99
|
+
reject(new Error(message));
|
|
100
|
+
}, timeoutMs);
|
|
101
|
+
timer.unref?.();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
return Promise.race([promise, timeoutPromise]).finally(() => {
|
|
105
|
+
clearTimeout(timer);
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function normalizeRetryOptions(retryOptions) {
|
|
110
|
+
if (!retryOptions || typeof retryOptions !== 'object') {
|
|
111
|
+
return Object.freeze({});
|
|
112
|
+
}
|
|
113
|
+
return Object.freeze({ ...retryOptions });
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function isCodexRetryable(error) {
|
|
117
|
+
return error instanceof CodexMcpTransportError
|
|
118
|
+
|| error?.code === 'ETIMEDOUT'
|
|
119
|
+
|| error?.cause?.code === 'ETIMEDOUT';
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function detectWorkerCategory(error, fallbackCategory = 'transient') {
|
|
123
|
+
const combined = `${error?.message || ''}\n${error?.stderr || ''}`.toLowerCase();
|
|
124
|
+
|
|
125
|
+
if (error?.code === 'INVALID_INPUT') return 'input';
|
|
126
|
+
if (/(unauthorized|forbidden|auth|login|token|credential|apikey|api key)/i.test(combined)) {
|
|
127
|
+
return 'auth';
|
|
128
|
+
}
|
|
129
|
+
if (/(config|unknown option|invalid option|missing|필수 mcp 도구 누락)/i.test(combined)) {
|
|
130
|
+
return 'config';
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return fallbackCategory;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function buildCodexErrorInfo(error, attempts) {
|
|
137
|
+
const retryable = isCodexRetryable(error);
|
|
138
|
+
const code = error instanceof CodexMcpTransportError
|
|
139
|
+
? 'CODEX_TRANSPORT_ERROR'
|
|
140
|
+
: (error?.code || 'CODEX_EXECUTION_ERROR');
|
|
141
|
+
const category = detectWorkerCategory(error, retryable ? 'transient' : 'config');
|
|
142
|
+
|
|
143
|
+
let recovery = 'Review the Codex worker error output and retry after correcting the issue.';
|
|
144
|
+
if (code === 'INVALID_INPUT') {
|
|
145
|
+
recovery = 'Provide a non-empty prompt before invoking the Codex worker.';
|
|
146
|
+
} else if (retryable) {
|
|
147
|
+
recovery = 'Retry after reconnecting the Codex MCP transport.';
|
|
148
|
+
} else if (category === 'auth') {
|
|
149
|
+
recovery = 'Refresh the Codex authentication state and retry.';
|
|
150
|
+
} else if (category === 'config') {
|
|
151
|
+
recovery = 'Check the Codex MCP configuration and available tools.';
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return Object.freeze({
|
|
155
|
+
code,
|
|
156
|
+
retryable,
|
|
157
|
+
attempts,
|
|
158
|
+
category,
|
|
159
|
+
recovery,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Codex MCP 워커
|
|
165
|
+
*/
|
|
166
|
+
export class CodexMcpWorker {
|
|
167
|
+
type = 'codex';
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* @param {object} [options]
|
|
171
|
+
* @param {string} [options.command]
|
|
172
|
+
* @param {string[]} [options.args]
|
|
173
|
+
* @param {string} [options.cwd]
|
|
174
|
+
* @param {Record<string, string>} [options.env]
|
|
175
|
+
* @param {{ name: string, version: string }} [options.clientInfo]
|
|
176
|
+
* @param {number} [options.bootstrapTimeoutMs]
|
|
177
|
+
*/
|
|
178
|
+
constructor(options = {}) {
|
|
179
|
+
this.command = options.command || process.env.CODEX_BIN || 'codex';
|
|
180
|
+
this.args = Array.isArray(options.args) && options.args.length
|
|
181
|
+
? [...options.args]
|
|
182
|
+
: ['mcp-server'];
|
|
183
|
+
this.cwd = options.cwd || process.cwd();
|
|
184
|
+
this.env = cloneEnv({ ...cloneEnv(process.env), ...cloneEnv(options.env) });
|
|
185
|
+
this.clientInfo = options.clientInfo || { name: 'triflux-codex-mcp', version: '1.0.0' };
|
|
186
|
+
this.bootstrapTimeoutMs = Number.isFinite(options.bootstrapTimeoutMs)
|
|
187
|
+
? options.bootstrapTimeoutMs
|
|
188
|
+
: DEFAULT_CODEX_MCP_BOOTSTRAP_TIMEOUT_MS;
|
|
189
|
+
this.retryOptions = normalizeRetryOptions(options.retryOptions);
|
|
190
|
+
|
|
191
|
+
this.client = null;
|
|
192
|
+
this.transport = null;
|
|
193
|
+
this.ready = false;
|
|
194
|
+
this.availableTools = new Set();
|
|
195
|
+
this.threadIds = new Map();
|
|
196
|
+
this.serverStderr = '';
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
isReady() {
|
|
200
|
+
return this.ready;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
getThreadId(sessionKey) {
|
|
204
|
+
return this.threadIds.get(sessionKey) || null;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
setThreadId(sessionKey, threadId) {
|
|
208
|
+
if (!sessionKey || !threadId) return;
|
|
209
|
+
this.threadIds.set(sessionKey, threadId);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
clearThread(sessionKey) {
|
|
213
|
+
if (!sessionKey) return;
|
|
214
|
+
this.threadIds.delete(sessionKey);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async start() {
|
|
218
|
+
if (this.ready && this.client && this.transport) return;
|
|
219
|
+
|
|
220
|
+
await this.stop();
|
|
221
|
+
|
|
222
|
+
const transport = new StdioClientTransport({
|
|
223
|
+
command: this.command,
|
|
224
|
+
args: this.args,
|
|
225
|
+
cwd: this.cwd,
|
|
226
|
+
env: this.env,
|
|
227
|
+
stderr: 'pipe',
|
|
228
|
+
});
|
|
229
|
+
const client = new Client(this.clientInfo, { capabilities: {} });
|
|
230
|
+
|
|
231
|
+
this.serverStderr = '';
|
|
232
|
+
transport.stderr?.on('data', (chunk) => {
|
|
233
|
+
this.serverStderr += String(chunk);
|
|
234
|
+
if (this.serverStderr.length > 16000) {
|
|
235
|
+
this.serverStderr = this.serverStderr.slice(-16000);
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
try {
|
|
240
|
+
await withTimeout((async () => {
|
|
241
|
+
await client.connect(transport);
|
|
242
|
+
const tools = await client.listTools(undefined, { timeout: this.bootstrapTimeoutMs });
|
|
243
|
+
this.availableTools = new Set(tools.tools.map((tool) => tool.name));
|
|
244
|
+
|
|
245
|
+
for (const requiredTool of REQUIRED_TOOLS) {
|
|
246
|
+
if (!this.availableTools.has(requiredTool)) {
|
|
247
|
+
throw new Error(`필수 MCP 도구 누락: ${requiredTool}`);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
})(), this.bootstrapTimeoutMs, `Codex MCP bootstrap timeout (${this.bootstrapTimeoutMs}ms)`);
|
|
251
|
+
} catch (error) {
|
|
252
|
+
await client.close().catch(() => {});
|
|
253
|
+
transport.stderr?.destroy?.();
|
|
254
|
+
throw new CodexMcpTransportError(
|
|
255
|
+
`Codex MCP 연결 실패: ${error instanceof Error ? error.message : String(error)}`,
|
|
256
|
+
{ cause: error, stderr: this.serverStderr.trim() },
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
this.client = client;
|
|
261
|
+
this.transport = transport;
|
|
262
|
+
this.ready = true;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
async stop() {
|
|
266
|
+
this.ready = false;
|
|
267
|
+
this.availableTools.clear();
|
|
268
|
+
|
|
269
|
+
const client = this.client;
|
|
270
|
+
const transport = this.transport;
|
|
271
|
+
this.transport = null;
|
|
272
|
+
this.client = null;
|
|
273
|
+
|
|
274
|
+
if (client) {
|
|
275
|
+
await client.close().catch(() => {});
|
|
276
|
+
} else if (transport) {
|
|
277
|
+
await transport.close().catch(() => {});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
transport?.stderr?.destroy?.();
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* @param {string} prompt
|
|
285
|
+
* @param {import('./interface.mjs').WorkerExecuteOptions} [opts]
|
|
286
|
+
* @returns {Promise<import('./interface.mjs').WorkerResult>}
|
|
287
|
+
*/
|
|
288
|
+
async execute(prompt, opts = {}) {
|
|
289
|
+
if (typeof prompt !== 'string' || !prompt.trim()) {
|
|
290
|
+
return {
|
|
291
|
+
output: 'prompt는 비어 있을 수 없습니다.',
|
|
292
|
+
exitCode: CODEX_MCP_EXECUTION_EXIT_CODE,
|
|
293
|
+
threadId: null,
|
|
294
|
+
sessionKey: opts.sessionKey || null,
|
|
295
|
+
error: buildCodexErrorInfo({ code: 'INVALID_INPUT', message: 'prompt는 비어 있을 수 없습니다.' }, 0),
|
|
296
|
+
raw: null,
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const sessionKey = typeof opts.sessionKey === 'string' && opts.sessionKey
|
|
301
|
+
? opts.sessionKey
|
|
302
|
+
: null;
|
|
303
|
+
|
|
304
|
+
if (opts.resetSession && sessionKey) {
|
|
305
|
+
this.clearThread(sessionKey);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const threadId = typeof opts.threadId === 'string' && opts.threadId
|
|
309
|
+
? opts.threadId
|
|
310
|
+
: (sessionKey ? this.getThreadId(sessionKey) : null);
|
|
311
|
+
const timeoutMs = Number.isFinite(opts.timeoutMs) ? opts.timeoutMs : DEFAULT_CODEX_MCP_TIMEOUT_MS;
|
|
312
|
+
let attempts = 0;
|
|
313
|
+
let activeThreadId = threadId;
|
|
314
|
+
|
|
315
|
+
try {
|
|
316
|
+
const { rawResult, normalized } = await withRetry(async () => {
|
|
317
|
+
attempts += 1;
|
|
318
|
+
if (attempts === 1) {
|
|
319
|
+
await this.start();
|
|
320
|
+
} else {
|
|
321
|
+
await this.stop();
|
|
322
|
+
await this.start();
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const toolName = pickToolName(activeThreadId);
|
|
326
|
+
const toolArguments = toolName === 'codex-reply'
|
|
327
|
+
? { prompt, threadId: activeThreadId }
|
|
328
|
+
: buildCodexArguments(prompt, opts);
|
|
329
|
+
|
|
330
|
+
const nextRawResult = await this.client.callTool(
|
|
331
|
+
{ name: toolName, arguments: toolArguments },
|
|
332
|
+
undefined,
|
|
333
|
+
{ timeout: timeoutMs },
|
|
334
|
+
);
|
|
335
|
+
|
|
336
|
+
const textContent = collectTextContent(nextRawResult.content);
|
|
337
|
+
const nextNormalized = normalizeStructuredContent(nextRawResult.structuredContent, textContent);
|
|
338
|
+
activeThreadId = nextNormalized.threadId || activeThreadId;
|
|
339
|
+
|
|
340
|
+
return { rawResult: nextRawResult, normalized: nextNormalized };
|
|
341
|
+
}, {
|
|
342
|
+
...this.retryOptions,
|
|
343
|
+
shouldRetry: (error) => isCodexRetryable(error),
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
if (sessionKey && normalized.threadId) {
|
|
347
|
+
this.setThreadId(sessionKey, normalized.threadId);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
if (rawResult.isError) {
|
|
351
|
+
return {
|
|
352
|
+
output: normalized.content,
|
|
353
|
+
exitCode: CODEX_MCP_EXECUTION_EXIT_CODE,
|
|
354
|
+
threadId: normalized.threadId,
|
|
355
|
+
sessionKey,
|
|
356
|
+
error: buildCodexErrorInfo(
|
|
357
|
+
{ code: 'CODEX_TOOL_ERROR', message: normalized.content },
|
|
358
|
+
attempts,
|
|
359
|
+
),
|
|
360
|
+
raw: rawResult,
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
return {
|
|
365
|
+
output: normalized.content,
|
|
366
|
+
exitCode: 0,
|
|
367
|
+
threadId: normalized.threadId,
|
|
368
|
+
sessionKey,
|
|
369
|
+
raw: rawResult,
|
|
370
|
+
};
|
|
371
|
+
} catch (error) {
|
|
372
|
+
await this.stop().catch(() => {});
|
|
373
|
+
return {
|
|
374
|
+
output: error instanceof Error ? error.message : String(error),
|
|
375
|
+
exitCode: CODEX_MCP_EXECUTION_EXIT_CODE,
|
|
376
|
+
threadId: activeThreadId,
|
|
377
|
+
sessionKey,
|
|
378
|
+
error: buildCodexErrorInfo(error, attempts || 1),
|
|
379
|
+
raw: null,
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
export function createCodexMcpWorker(options = {}) {
|
|
386
|
+
return new CodexMcpWorker(options);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function parseCliArgs(argv) {
|
|
390
|
+
const options = {
|
|
391
|
+
command: process.env.CODEX_BIN || 'codex',
|
|
392
|
+
cwd: process.cwd(),
|
|
393
|
+
timeoutMs: DEFAULT_CODEX_MCP_TIMEOUT_MS,
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
397
|
+
const token = argv[i];
|
|
398
|
+
const next = () => {
|
|
399
|
+
const value = argv[i + 1];
|
|
400
|
+
if (value === undefined) {
|
|
401
|
+
throw new Error(`${token} 값이 필요합니다.`);
|
|
402
|
+
}
|
|
403
|
+
i += 1;
|
|
404
|
+
return value;
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
switch (token) {
|
|
408
|
+
case '--prompt':
|
|
409
|
+
options.prompt = next();
|
|
410
|
+
break;
|
|
411
|
+
case '--thread-id':
|
|
412
|
+
options.threadId = next();
|
|
413
|
+
break;
|
|
414
|
+
case '--session-key':
|
|
415
|
+
options.sessionKey = next();
|
|
416
|
+
break;
|
|
417
|
+
case '--cwd':
|
|
418
|
+
options.cwd = next();
|
|
419
|
+
break;
|
|
420
|
+
case '--profile':
|
|
421
|
+
options.profile = next();
|
|
422
|
+
break;
|
|
423
|
+
case '--model':
|
|
424
|
+
options.model = next();
|
|
425
|
+
break;
|
|
426
|
+
case '--approval-policy':
|
|
427
|
+
options.approvalPolicy = next();
|
|
428
|
+
break;
|
|
429
|
+
case '--sandbox':
|
|
430
|
+
options.sandbox = next();
|
|
431
|
+
break;
|
|
432
|
+
case '--base-instructions':
|
|
433
|
+
options.baseInstructions = next();
|
|
434
|
+
break;
|
|
435
|
+
case '--developer-instructions':
|
|
436
|
+
options.developerInstructions = next();
|
|
437
|
+
break;
|
|
438
|
+
case '--compact-prompt':
|
|
439
|
+
options.compactPrompt = next();
|
|
440
|
+
break;
|
|
441
|
+
case '--timeout-ms':
|
|
442
|
+
options.timeoutMs = Number.parseInt(next(), 10);
|
|
443
|
+
break;
|
|
444
|
+
case '--config-json':
|
|
445
|
+
options.config = JSON.parse(next());
|
|
446
|
+
break;
|
|
447
|
+
case '--codex-command':
|
|
448
|
+
options.command = next();
|
|
449
|
+
break;
|
|
450
|
+
case '--reset-session':
|
|
451
|
+
options.resetSession = true;
|
|
452
|
+
break;
|
|
453
|
+
default:
|
|
454
|
+
throw new Error(`알 수 없는 옵션: ${token}`);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
if (typeof options.prompt !== 'string' || !options.prompt) {
|
|
459
|
+
throw new Error('--prompt는 필수입니다.');
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
return options;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
export async function runCodexMcpCli(argv = process.argv.slice(2)) {
|
|
466
|
+
let options;
|
|
467
|
+
try {
|
|
468
|
+
options = parseCliArgs(argv);
|
|
469
|
+
} catch (error) {
|
|
470
|
+
console.error(`[codex-mcp] ${error instanceof Error ? error.message : String(error)}`);
|
|
471
|
+
process.exitCode = 64;
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
const worker = new CodexMcpWorker({
|
|
476
|
+
command: options.command,
|
|
477
|
+
cwd: options.cwd,
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
try {
|
|
481
|
+
const result = await worker.execute(options.prompt, options);
|
|
482
|
+
if (result.output) {
|
|
483
|
+
process.stdout.write(result.output);
|
|
484
|
+
if (!result.output.endsWith('\n')) {
|
|
485
|
+
process.stdout.write('\n');
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
process.exitCode = result.exitCode;
|
|
489
|
+
} catch (error) {
|
|
490
|
+
const lines = [error instanceof Error ? error.message : String(error)];
|
|
491
|
+
if (error instanceof CodexMcpTransportError && error.stderr) {
|
|
492
|
+
lines.push(error.stderr);
|
|
493
|
+
}
|
|
494
|
+
console.error(`[codex-mcp] ${lines.join('\n')}`);
|
|
495
|
+
process.exitCode = CODEX_MCP_TRANSPORT_EXIT_CODE;
|
|
496
|
+
} finally {
|
|
497
|
+
await worker.stop();
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
|
|
502
|
+
await runCodexMcpCli();
|
|
503
|
+
process.exit(process.exitCode ?? 0);
|
|
504
|
+
}
|