triflux 3.2.0-dev.1 → 3.2.0-dev.11

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.
Files changed (53) hide show
  1. package/README.ko.md +26 -18
  2. package/README.md +26 -18
  3. package/bin/triflux.mjs +1614 -1084
  4. package/hooks/hooks.json +12 -0
  5. package/hooks/keyword-rules.json +354 -0
  6. package/hub/bridge.mjs +371 -193
  7. package/hub/hitl.mjs +45 -31
  8. package/hub/pipe.mjs +457 -0
  9. package/hub/router.mjs +422 -161
  10. package/hub/server.mjs +429 -344
  11. package/hub/store.mjs +388 -314
  12. package/hub/team/cli-team-common.mjs +348 -0
  13. package/hub/team/cli-team-control.mjs +393 -0
  14. package/hub/team/cli-team-start.mjs +516 -0
  15. package/hub/team/cli-team-status.mjs +269 -0
  16. package/hub/team/cli.mjs +99 -368
  17. package/hub/team/dashboard.mjs +165 -64
  18. package/hub/team/native-supervisor.mjs +300 -0
  19. package/hub/team/native.mjs +62 -0
  20. package/hub/team/nativeProxy.mjs +534 -0
  21. package/hub/team/orchestrator.mjs +90 -31
  22. package/hub/team/pane.mjs +149 -101
  23. package/hub/team/psmux.mjs +297 -0
  24. package/hub/team/session.mjs +608 -186
  25. package/hub/team/shared.mjs +13 -0
  26. package/hub/team/staleState.mjs +299 -0
  27. package/hub/tools.mjs +140 -53
  28. package/hub/workers/claude-worker.mjs +446 -0
  29. package/hub/workers/codex-mcp.mjs +414 -0
  30. package/hub/workers/factory.mjs +18 -0
  31. package/hub/workers/gemini-worker.mjs +349 -0
  32. package/hub/workers/interface.mjs +41 -0
  33. package/hud/hud-qos-status.mjs +1789 -1732
  34. package/package.json +6 -2
  35. package/scripts/__tests__/keyword-detector.test.mjs +234 -0
  36. package/scripts/hub-ensure.mjs +83 -0
  37. package/scripts/keyword-detector.mjs +272 -0
  38. package/scripts/keyword-rules-expander.mjs +521 -0
  39. package/scripts/lib/keyword-rules.mjs +168 -0
  40. package/scripts/psmux-steering-prototype.sh +368 -0
  41. package/scripts/run.cjs +62 -0
  42. package/scripts/setup.mjs +189 -7
  43. package/scripts/test-tfx-route-no-claude-native.mjs +49 -0
  44. package/scripts/tfx-route-worker.mjs +161 -0
  45. package/scripts/tfx-route.sh +943 -508
  46. package/skills/tfx-auto/SKILL.md +90 -564
  47. package/skills/tfx-auto-codex/SKILL.md +77 -0
  48. package/skills/tfx-codex/SKILL.md +1 -4
  49. package/skills/tfx-doctor/SKILL.md +1 -0
  50. package/skills/tfx-gemini/SKILL.md +1 -4
  51. package/skills/tfx-multi/SKILL.md +296 -0
  52. package/skills/tfx-setup/SKILL.md +1 -4
  53. package/skills/tfx-team/SKILL.md +0 -172
@@ -0,0 +1,414 @@
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
+ const REQUIRED_TOOLS = ['codex', 'codex-reply'];
9
+
10
+ export const CODEX_MCP_TRANSPORT_EXIT_CODE = 70;
11
+ export const CODEX_MCP_EXECUTION_EXIT_CODE = 1;
12
+ export const DEFAULT_CODEX_MCP_TIMEOUT_MS = 10 * 60 * 1000;
13
+ export const DEFAULT_CODEX_MCP_BOOTSTRAP_TIMEOUT_MS = 10 * 1000;
14
+
15
+ /**
16
+ * Codex MCP transport/bootstrap 계층 오류
17
+ */
18
+ export class CodexMcpTransportError extends Error {
19
+ /**
20
+ * @param {string} message
21
+ * @param {object} [options]
22
+ * @param {unknown} [options.cause]
23
+ * @param {string} [options.stderr]
24
+ */
25
+ constructor(message, options = {}) {
26
+ super(message, { cause: options.cause });
27
+ this.name = 'CodexMcpTransportError';
28
+ this.stderr = options.stderr || '';
29
+ }
30
+ }
31
+
32
+ function cloneEnv(env = process.env) {
33
+ return Object.fromEntries(
34
+ Object.entries(env).filter(([, value]) => typeof value === 'string'),
35
+ );
36
+ }
37
+
38
+ function collectTextContent(content = []) {
39
+ return content
40
+ .filter((item) => item?.type === 'text' && typeof item.text === 'string')
41
+ .map((item) => item.text)
42
+ .join('\n')
43
+ .trim();
44
+ }
45
+
46
+ function normalizeStructuredContent(structuredContent, fallbackText = '') {
47
+ if (!structuredContent || typeof structuredContent !== 'object') {
48
+ return { threadId: null, content: fallbackText };
49
+ }
50
+
51
+ const threadId = typeof structuredContent.threadId === 'string'
52
+ ? structuredContent.threadId
53
+ : null;
54
+ const content = typeof structuredContent.content === 'string'
55
+ ? structuredContent.content
56
+ : fallbackText;
57
+
58
+ return { threadId, content };
59
+ }
60
+
61
+ function buildCodexArguments(prompt, opts = {}) {
62
+ const args = { prompt };
63
+
64
+ if (typeof opts.cwd === 'string' && opts.cwd) args.cwd = opts.cwd;
65
+ if (typeof opts.model === 'string' && opts.model) args.model = opts.model;
66
+ if (typeof opts.profile === 'string' && opts.profile) args.profile = opts.profile;
67
+ if (typeof opts.approvalPolicy === 'string' && opts.approvalPolicy) {
68
+ args['approval-policy'] = opts.approvalPolicy;
69
+ }
70
+ if (typeof opts.sandbox === 'string' && opts.sandbox) args.sandbox = opts.sandbox;
71
+ if (opts.config && typeof opts.config === 'object') args.config = opts.config;
72
+ if (typeof opts.baseInstructions === 'string' && opts.baseInstructions) {
73
+ args['base-instructions'] = opts.baseInstructions;
74
+ }
75
+ if (typeof opts.developerInstructions === 'string' && opts.developerInstructions) {
76
+ args['developer-instructions'] = opts.developerInstructions;
77
+ }
78
+ if (typeof opts.compactPrompt === 'string' && opts.compactPrompt) {
79
+ args['compact-prompt'] = opts.compactPrompt;
80
+ }
81
+
82
+ return args;
83
+ }
84
+
85
+ function pickToolName(threadId) {
86
+ return threadId ? 'codex-reply' : 'codex';
87
+ }
88
+
89
+ function withTimeout(promise, timeoutMs, message) {
90
+ if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
91
+ return promise;
92
+ }
93
+
94
+ let timer;
95
+ const timeoutPromise = new Promise((_, reject) => {
96
+ timer = setTimeout(() => {
97
+ reject(new Error(message));
98
+ }, timeoutMs);
99
+ timer.unref?.();
100
+ });
101
+
102
+ return Promise.race([promise, timeoutPromise]).finally(() => {
103
+ clearTimeout(timer);
104
+ });
105
+ }
106
+
107
+ /**
108
+ * Codex MCP 워커
109
+ */
110
+ export class CodexMcpWorker {
111
+ type = 'codex';
112
+
113
+ /**
114
+ * @param {object} [options]
115
+ * @param {string} [options.command]
116
+ * @param {string[]} [options.args]
117
+ * @param {string} [options.cwd]
118
+ * @param {Record<string, string>} [options.env]
119
+ * @param {{ name: string, version: string }} [options.clientInfo]
120
+ * @param {number} [options.bootstrapTimeoutMs]
121
+ */
122
+ constructor(options = {}) {
123
+ this.command = options.command || process.env.CODEX_BIN || 'codex';
124
+ this.args = Array.isArray(options.args) && options.args.length
125
+ ? [...options.args]
126
+ : ['mcp-server'];
127
+ this.cwd = options.cwd || process.cwd();
128
+ this.env = cloneEnv({ ...cloneEnv(process.env), ...cloneEnv(options.env) });
129
+ this.clientInfo = options.clientInfo || { name: 'triflux-codex-mcp', version: '1.0.0' };
130
+ this.bootstrapTimeoutMs = Number.isFinite(options.bootstrapTimeoutMs)
131
+ ? options.bootstrapTimeoutMs
132
+ : DEFAULT_CODEX_MCP_BOOTSTRAP_TIMEOUT_MS;
133
+
134
+ this.client = null;
135
+ this.transport = null;
136
+ this.ready = false;
137
+ this.availableTools = new Set();
138
+ this.threadIds = new Map();
139
+ this.serverStderr = '';
140
+ }
141
+
142
+ isReady() {
143
+ return this.ready;
144
+ }
145
+
146
+ getThreadId(sessionKey) {
147
+ return this.threadIds.get(sessionKey) || null;
148
+ }
149
+
150
+ setThreadId(sessionKey, threadId) {
151
+ if (!sessionKey || !threadId) return;
152
+ this.threadIds.set(sessionKey, threadId);
153
+ }
154
+
155
+ clearThread(sessionKey) {
156
+ if (!sessionKey) return;
157
+ this.threadIds.delete(sessionKey);
158
+ }
159
+
160
+ async start() {
161
+ if (this.ready && this.client && this.transport) return;
162
+
163
+ await this.stop();
164
+
165
+ const transport = new StdioClientTransport({
166
+ command: this.command,
167
+ args: this.args,
168
+ cwd: this.cwd,
169
+ env: this.env,
170
+ stderr: 'pipe',
171
+ });
172
+ const client = new Client(this.clientInfo, { capabilities: {} });
173
+
174
+ this.serverStderr = '';
175
+ transport.stderr?.on('data', (chunk) => {
176
+ this.serverStderr += String(chunk);
177
+ if (this.serverStderr.length > 16000) {
178
+ this.serverStderr = this.serverStderr.slice(-16000);
179
+ }
180
+ });
181
+
182
+ try {
183
+ await withTimeout((async () => {
184
+ await client.connect(transport);
185
+ const tools = await client.listTools(undefined, { timeout: this.bootstrapTimeoutMs });
186
+ this.availableTools = new Set(tools.tools.map((tool) => tool.name));
187
+
188
+ for (const requiredTool of REQUIRED_TOOLS) {
189
+ if (!this.availableTools.has(requiredTool)) {
190
+ throw new Error(`필수 MCP 도구 누락: ${requiredTool}`);
191
+ }
192
+ }
193
+ })(), this.bootstrapTimeoutMs, `Codex MCP bootstrap timeout (${this.bootstrapTimeoutMs}ms)`);
194
+ } catch (error) {
195
+ await client.close().catch(() => {});
196
+ transport.stderr?.destroy?.();
197
+ throw new CodexMcpTransportError(
198
+ `Codex MCP 연결 실패: ${error instanceof Error ? error.message : String(error)}`,
199
+ { cause: error, stderr: this.serverStderr.trim() },
200
+ );
201
+ }
202
+
203
+ this.client = client;
204
+ this.transport = transport;
205
+ this.ready = true;
206
+ }
207
+
208
+ async stop() {
209
+ this.ready = false;
210
+ this.availableTools.clear();
211
+
212
+ const client = this.client;
213
+ const transport = this.transport;
214
+ this.transport = null;
215
+ this.client = null;
216
+
217
+ if (client) {
218
+ await client.close().catch(() => {});
219
+ } else if (transport) {
220
+ await transport.close().catch(() => {});
221
+ }
222
+
223
+ transport?.stderr?.destroy?.();
224
+ }
225
+
226
+ /**
227
+ * @param {string} prompt
228
+ * @param {import('./interface.mjs').WorkerExecuteOptions} [opts]
229
+ * @returns {Promise<import('./interface.mjs').WorkerResult>}
230
+ */
231
+ async execute(prompt, opts = {}) {
232
+ if (typeof prompt !== 'string' || !prompt.trim()) {
233
+ return {
234
+ output: 'prompt는 비어 있을 수 없습니다.',
235
+ exitCode: CODEX_MCP_EXECUTION_EXIT_CODE,
236
+ threadId: null,
237
+ sessionKey: opts.sessionKey || null,
238
+ raw: null,
239
+ };
240
+ }
241
+
242
+ await this.start();
243
+
244
+ const sessionKey = typeof opts.sessionKey === 'string' && opts.sessionKey
245
+ ? opts.sessionKey
246
+ : null;
247
+
248
+ if (opts.resetSession && sessionKey) {
249
+ this.clearThread(sessionKey);
250
+ }
251
+
252
+ const threadId = typeof opts.threadId === 'string' && opts.threadId
253
+ ? opts.threadId
254
+ : (sessionKey ? this.getThreadId(sessionKey) : null);
255
+
256
+ const toolName = pickToolName(threadId);
257
+ const toolArguments = toolName === 'codex-reply'
258
+ ? { prompt, threadId }
259
+ : buildCodexArguments(prompt, opts);
260
+
261
+ let rawResult;
262
+ try {
263
+ rawResult = await this.client.callTool(
264
+ { name: toolName, arguments: toolArguments },
265
+ undefined,
266
+ { timeout: Number.isFinite(opts.timeoutMs) ? opts.timeoutMs : DEFAULT_CODEX_MCP_TIMEOUT_MS },
267
+ );
268
+ } catch (error) {
269
+ return {
270
+ output: error instanceof Error ? error.message : String(error),
271
+ exitCode: CODEX_MCP_EXECUTION_EXIT_CODE,
272
+ threadId,
273
+ sessionKey,
274
+ raw: null,
275
+ };
276
+ }
277
+
278
+ const textContent = collectTextContent(rawResult.content);
279
+ const normalized = normalizeStructuredContent(rawResult.structuredContent, textContent);
280
+
281
+ if (sessionKey && normalized.threadId) {
282
+ this.setThreadId(sessionKey, normalized.threadId);
283
+ }
284
+
285
+ return {
286
+ output: normalized.content,
287
+ exitCode: rawResult.isError ? CODEX_MCP_EXECUTION_EXIT_CODE : 0,
288
+ threadId: normalized.threadId,
289
+ sessionKey,
290
+ raw: rawResult,
291
+ };
292
+ }
293
+ }
294
+
295
+ export function createCodexMcpWorker(options = {}) {
296
+ return new CodexMcpWorker(options);
297
+ }
298
+
299
+ function parseCliArgs(argv) {
300
+ const options = {
301
+ command: process.env.CODEX_BIN || 'codex',
302
+ cwd: process.cwd(),
303
+ timeoutMs: DEFAULT_CODEX_MCP_TIMEOUT_MS,
304
+ };
305
+
306
+ for (let i = 0; i < argv.length; i += 1) {
307
+ const token = argv[i];
308
+ const next = () => {
309
+ const value = argv[i + 1];
310
+ if (value === undefined) {
311
+ throw new Error(`${token} 값이 필요합니다.`);
312
+ }
313
+ i += 1;
314
+ return value;
315
+ };
316
+
317
+ switch (token) {
318
+ case '--prompt':
319
+ options.prompt = next();
320
+ break;
321
+ case '--thread-id':
322
+ options.threadId = next();
323
+ break;
324
+ case '--session-key':
325
+ options.sessionKey = next();
326
+ break;
327
+ case '--cwd':
328
+ options.cwd = next();
329
+ break;
330
+ case '--profile':
331
+ options.profile = next();
332
+ break;
333
+ case '--model':
334
+ options.model = next();
335
+ break;
336
+ case '--approval-policy':
337
+ options.approvalPolicy = next();
338
+ break;
339
+ case '--sandbox':
340
+ options.sandbox = next();
341
+ break;
342
+ case '--base-instructions':
343
+ options.baseInstructions = next();
344
+ break;
345
+ case '--developer-instructions':
346
+ options.developerInstructions = next();
347
+ break;
348
+ case '--compact-prompt':
349
+ options.compactPrompt = next();
350
+ break;
351
+ case '--timeout-ms':
352
+ options.timeoutMs = Number.parseInt(next(), 10);
353
+ break;
354
+ case '--config-json':
355
+ options.config = JSON.parse(next());
356
+ break;
357
+ case '--codex-command':
358
+ options.command = next();
359
+ break;
360
+ case '--reset-session':
361
+ options.resetSession = true;
362
+ break;
363
+ default:
364
+ throw new Error(`알 수 없는 옵션: ${token}`);
365
+ }
366
+ }
367
+
368
+ if (typeof options.prompt !== 'string' || !options.prompt) {
369
+ throw new Error('--prompt는 필수입니다.');
370
+ }
371
+
372
+ return options;
373
+ }
374
+
375
+ export async function runCodexMcpCli(argv = process.argv.slice(2)) {
376
+ let options;
377
+ try {
378
+ options = parseCliArgs(argv);
379
+ } catch (error) {
380
+ console.error(`[codex-mcp] ${error instanceof Error ? error.message : String(error)}`);
381
+ process.exitCode = 64;
382
+ return;
383
+ }
384
+
385
+ const worker = new CodexMcpWorker({
386
+ command: options.command,
387
+ cwd: options.cwd,
388
+ });
389
+
390
+ try {
391
+ const result = await worker.execute(options.prompt, options);
392
+ if (result.output) {
393
+ process.stdout.write(result.output);
394
+ if (!result.output.endsWith('\n')) {
395
+ process.stdout.write('\n');
396
+ }
397
+ }
398
+ process.exitCode = result.exitCode;
399
+ } catch (error) {
400
+ const lines = [error instanceof Error ? error.message : String(error)];
401
+ if (error instanceof CodexMcpTransportError && error.stderr) {
402
+ lines.push(error.stderr);
403
+ }
404
+ console.error(`[codex-mcp] ${lines.join('\n')}`);
405
+ process.exitCode = CODEX_MCP_TRANSPORT_EXIT_CODE;
406
+ } finally {
407
+ await worker.stop();
408
+ }
409
+ }
410
+
411
+ if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
412
+ await runCodexMcpCli();
413
+ process.exit(process.exitCode ?? 0);
414
+ }
@@ -0,0 +1,18 @@
1
+ // hub/workers/factory.mjs — Worker 생성 팩토리
2
+
3
+ import { GeminiWorker } from './gemini-worker.mjs';
4
+ import { ClaudeWorker } from './claude-worker.mjs';
5
+ import { CodexMcpWorker } from './codex-mcp.mjs';
6
+
7
+ export function createWorker(type, opts = {}) {
8
+ switch (type) {
9
+ case 'gemini':
10
+ return new GeminiWorker(opts);
11
+ case 'claude':
12
+ return new ClaudeWorker(opts);
13
+ case 'codex':
14
+ return new CodexMcpWorker(opts);
15
+ default:
16
+ throw new Error(`Unknown worker type: ${type}`);
17
+ }
18
+ }