@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.
Files changed (68) hide show
  1. package/hub/pipe.mjs +579 -0
  2. package/hub/public/dashboard.html +355 -0
  3. package/hub/public/tray-icon.ico +0 -0
  4. package/hub/public/tray-icon.png +0 -0
  5. package/hub/server.mjs +1124 -0
  6. package/hub/store-adapter.mjs +851 -0
  7. package/hub/store.mjs +897 -0
  8. package/hub/team/agent-map.json +11 -0
  9. package/hub/team/ansi.mjs +379 -0
  10. package/hub/team/backend.mjs +90 -0
  11. package/hub/team/cli/commands/attach.mjs +37 -0
  12. package/hub/team/cli/commands/control.mjs +43 -0
  13. package/hub/team/cli/commands/debug.mjs +74 -0
  14. package/hub/team/cli/commands/focus.mjs +53 -0
  15. package/hub/team/cli/commands/interrupt.mjs +36 -0
  16. package/hub/team/cli/commands/kill.mjs +37 -0
  17. package/hub/team/cli/commands/list.mjs +24 -0
  18. package/hub/team/cli/commands/send.mjs +37 -0
  19. package/hub/team/cli/commands/start/index.mjs +106 -0
  20. package/hub/team/cli/commands/start/parse-args.mjs +130 -0
  21. package/hub/team/cli/commands/start/start-headless.mjs +109 -0
  22. package/hub/team/cli/commands/start/start-in-process.mjs +40 -0
  23. package/hub/team/cli/commands/start/start-mux.mjs +73 -0
  24. package/hub/team/cli/commands/start/start-wt.mjs +69 -0
  25. package/hub/team/cli/commands/status.mjs +87 -0
  26. package/hub/team/cli/commands/stop.mjs +31 -0
  27. package/hub/team/cli/commands/task.mjs +30 -0
  28. package/hub/team/cli/commands/tasks.mjs +13 -0
  29. package/hub/team/cli/help.mjs +42 -0
  30. package/hub/team/cli/index.mjs +41 -0
  31. package/hub/team/cli/manifest.mjs +29 -0
  32. package/hub/team/cli/render.mjs +30 -0
  33. package/hub/team/cli/services/attach-fallback.mjs +54 -0
  34. package/hub/team/cli/services/hub-client.mjs +208 -0
  35. package/hub/team/cli/services/member-selector.mjs +30 -0
  36. package/hub/team/cli/services/native-control.mjs +117 -0
  37. package/hub/team/cli/services/runtime-mode.mjs +62 -0
  38. package/hub/team/cli/services/state-store.mjs +48 -0
  39. package/hub/team/cli/services/task-model.mjs +30 -0
  40. package/hub/team/dashboard-anchor.mjs +14 -0
  41. package/hub/team/dashboard-layout.mjs +33 -0
  42. package/hub/team/dashboard-open.mjs +153 -0
  43. package/hub/team/dashboard.mjs +274 -0
  44. package/hub/team/handoff.mjs +303 -0
  45. package/hub/team/headless.mjs +1149 -0
  46. package/hub/team/native-supervisor.mjs +392 -0
  47. package/hub/team/native.mjs +649 -0
  48. package/hub/team/nativeProxy.mjs +681 -0
  49. package/hub/team/orchestrator.mjs +161 -0
  50. package/hub/team/pane.mjs +153 -0
  51. package/hub/team/psmux.mjs +1354 -0
  52. package/hub/team/routing.mjs +223 -0
  53. package/hub/team/session.mjs +611 -0
  54. package/hub/team/shared.mjs +13 -0
  55. package/hub/team/staleState.mjs +361 -0
  56. package/hub/team/tui-lite.mjs +380 -0
  57. package/hub/team/tui-viewer.mjs +463 -0
  58. package/hub/team/tui.mjs +1245 -0
  59. package/hub/tools.mjs +554 -0
  60. package/hub/tray.mjs +376 -0
  61. package/hub/workers/claude-worker.mjs +475 -0
  62. package/hub/workers/codex-mcp.mjs +504 -0
  63. package/hub/workers/delegator-mcp.mjs +1076 -0
  64. package/hub/workers/factory.mjs +21 -0
  65. package/hub/workers/gemini-worker.mjs +373 -0
  66. package/hub/workers/interface.mjs +52 -0
  67. package/hub/workers/worker-utils.mjs +104 -0
  68. package/package.json +31 -0
package/hub/pipe.mjs ADDED
@@ -0,0 +1,579 @@
1
+ // hub/pipe.mjs — Named Pipe/Unix socket 제어 채널
2
+ // NDJSON 프로토콜로 에이전트 실시간 제어/이벤트 푸시를 처리한다.
3
+
4
+ import net from 'node:net';
5
+ import { existsSync, unlinkSync } from 'node:fs';
6
+ import { randomUUID } from 'node:crypto';
7
+ import {
8
+ teamInfo,
9
+ teamTaskList,
10
+ teamTaskUpdate,
11
+ teamSendMessage,
12
+ } from './team-bridge.mjs';
13
+ import { createPipeline } from './pipeline/index.mjs';
14
+ import {
15
+ ensurePipelineTable,
16
+ initPipelineState,
17
+ listPipelineStates,
18
+ readPipelineState,
19
+ } from './pipeline/state.mjs';
20
+ import { IS_WINDOWS, pipePath } from './platform.mjs';
21
+ import { safeJsonParse } from './workers/worker-utils.mjs';
22
+
23
+ const DEFAULT_HEARTBEAT_TTL_MS = 60000;
24
+
25
+ /** 플랫폼별 pipe 경로 계산 */
26
+ export function getPipePath(sessionId = process.pid) {
27
+ return pipePath('triflux', sessionId);
28
+ }
29
+
30
+ function normalizeTopics(topics) {
31
+ if (!Array.isArray(topics)) return [];
32
+ return topics
33
+ .map((topic) => String(topic || '').trim())
34
+ .filter(Boolean);
35
+ }
36
+
37
+ /**
38
+ * Named Pipe 서버 생성
39
+ * @param {object} opts
40
+ * @param {object} opts.router
41
+ * @param {object} [opts.store]
42
+ * @param {string|number} [opts.sessionId]
43
+ * @param {number} [opts.heartbeatTtlMs]
44
+ */
45
+ export function createPipeServer({
46
+ router,
47
+ store = null,
48
+ sessionId = process.pid,
49
+ heartbeatTtlMs = DEFAULT_HEARTBEAT_TTL_MS,
50
+ delegatorService = null,
51
+ } = {}) {
52
+ if (!router) {
53
+ throw new Error('router is required');
54
+ }
55
+
56
+ const pipePath = getPipePath(sessionId);
57
+ const clients = new Map();
58
+ let server = null;
59
+ let heartbeatTimer = null;
60
+
61
+ function sendFrame(client, frame) {
62
+ if (!client || client.closed || !client.socket.writable) return false;
63
+ try {
64
+ client.socket.write(`${JSON.stringify(frame)}\n`);
65
+ return true;
66
+ } catch {
67
+ return false;
68
+ }
69
+ }
70
+
71
+ function sendResponse(client, requestId, result) {
72
+ return sendFrame(client, { type: 'response', request_id: requestId, ...result });
73
+ }
74
+
75
+ function closeClient(client) {
76
+ if (!client || client.closed) return;
77
+ client.closed = true;
78
+ clients.delete(client.id);
79
+ try { client.socket.destroy(); } catch {}
80
+ }
81
+
82
+ function touchClient(client) {
83
+ client.lastHeartbeatMs = Date.now();
84
+ }
85
+
86
+ function resolveAgentId(client, payload) {
87
+ const agentId = payload?.agent_id || client?.agentId;
88
+ if (!agentId) {
89
+ throw new Error('agent_id required');
90
+ }
91
+ return agentId;
92
+ }
93
+
94
+ function pushEvent(agentId, message) {
95
+ let delivered = false;
96
+ for (const client of clients.values()) {
97
+ if (client.agentId !== agentId) continue;
98
+ if (sendFrame(client, {
99
+ type: 'event',
100
+ event: 'message',
101
+ payload: { agent_id: agentId, message },
102
+ })) {
103
+ delivered = true;
104
+ }
105
+ }
106
+ return delivered;
107
+ }
108
+
109
+ function pushPendingMessages(agentId) {
110
+ if (!agentId) return 0;
111
+ const pending = router.getPendingMessages(agentId, { max_messages: 100 });
112
+ let pushed = 0;
113
+ for (const message of pending) {
114
+ if (router.markMessagePushed(agentId, message.id)) {
115
+ pushed += pushEvent(agentId, message) ? 1 : 0;
116
+ } else if (pushEvent(agentId, message)) {
117
+ pushed += 1;
118
+ }
119
+ }
120
+ return pushed;
121
+ }
122
+
123
+ async function processCommand(client, action, payload = {}) {
124
+ switch (action) {
125
+ case 'register': {
126
+ const result = router.registerAgent(payload);
127
+ if (client) {
128
+ client.agentId = payload.agent_id;
129
+ client.subscriptions = new Set(router.getSubscribedTopics(client.agentId));
130
+ touchClient(client);
131
+ pushPendingMessages(client.agentId);
132
+ }
133
+ return { ok: true, data: { ...result, pipe_path: pipePath } };
134
+ }
135
+
136
+ case 'subscribe': {
137
+ const agentId = resolveAgentId(client, payload);
138
+ const topics = normalizeTopics(payload.topics);
139
+ const result = router.subscribeAgent(agentId, topics, {
140
+ replace: Boolean(payload.replace),
141
+ });
142
+ if (client) {
143
+ client.agentId = agentId;
144
+ client.subscriptions = new Set(result.topics);
145
+ touchClient(client);
146
+ }
147
+ const replayed = pushPendingMessages(agentId);
148
+ return {
149
+ ok: true,
150
+ data: { ...result, replayed_messages: replayed },
151
+ };
152
+ }
153
+
154
+ case 'ack': {
155
+ const agentId = resolveAgentId(client, payload);
156
+ const acked = router.ackMessages(payload.message_ids || payload.ack_ids || [], agentId);
157
+ if (client) touchClient(client);
158
+ return { ok: true, data: { agent_id: agentId, acked_count: acked } };
159
+ }
160
+
161
+ case 'heartbeat': {
162
+ const agentId = resolveAgentId(client, payload);
163
+ const result = router.refreshAgentLease(agentId, payload.heartbeat_ttl_ms || heartbeatTtlMs);
164
+ if (client) touchClient(client);
165
+ return { ok: true, data: result };
166
+ }
167
+
168
+ case 'publish': {
169
+ const result = router.handlePublish(payload);
170
+ if (client) touchClient(client);
171
+ return result;
172
+ }
173
+
174
+ case 'handoff': {
175
+ const result = router.handleHandoff(payload);
176
+ if (client) touchClient(client);
177
+ return result;
178
+ }
179
+
180
+ case 'assign': {
181
+ const result = router.assignAsync(payload);
182
+ if (client) touchClient(client);
183
+ return result;
184
+ }
185
+
186
+ case 'assign_result': {
187
+ const result = router.reportAssignResult(payload);
188
+ if (client) touchClient(client);
189
+ return result;
190
+ }
191
+
192
+ case 'assign_retry': {
193
+ const result = router.retryAssign(payload.job_id, payload);
194
+ if (client) touchClient(client);
195
+ return result;
196
+ }
197
+
198
+ case 'result': {
199
+ const result = router.handlePublish({
200
+ from: payload.agent_id,
201
+ to: `topic:${payload.topic || 'task.result'}`,
202
+ topic: payload.topic || 'task.result',
203
+ payload: payload.payload || {},
204
+ priority: 5,
205
+ ttl_ms: 3600000,
206
+ trace_id: payload.trace_id,
207
+ correlation_id: payload.correlation_id,
208
+ });
209
+ if (client) touchClient(client);
210
+ return result;
211
+ }
212
+
213
+ case 'control': {
214
+ const result = router.handlePublish({
215
+ from: payload.from_agent || 'lead',
216
+ to: payload.to_agent,
217
+ topic: 'lead.control',
218
+ payload: {
219
+ command: payload.command,
220
+ reason: payload.reason || '',
221
+ ...(payload.payload || {}),
222
+ issued_at: Date.now(),
223
+ },
224
+ priority: 8,
225
+ ttl_ms: Math.max(1000, Math.min(Number(payload.ttl_ms) || 3600000, 86400000)),
226
+ trace_id: payload.trace_id,
227
+ correlation_id: payload.correlation_id,
228
+ });
229
+ if (client) touchClient(client);
230
+ return result;
231
+ }
232
+
233
+ case 'deregister': {
234
+ const agentId = resolveAgentId(client, payload);
235
+ router.updateAgentStatus(agentId, 'offline');
236
+ if (client) touchClient(client);
237
+ return {
238
+ ok: true,
239
+ data: { agent_id: agentId, status: 'offline' },
240
+ };
241
+ }
242
+
243
+ case 'team_task_update': {
244
+ const result = await teamTaskUpdate(payload);
245
+ if (client) touchClient(client);
246
+ return result;
247
+ }
248
+
249
+ case 'team_send_message': {
250
+ const result = await teamSendMessage(payload);
251
+ if (client) touchClient(client);
252
+ return result;
253
+ }
254
+
255
+ case 'pipeline_advance': {
256
+ if (client) touchClient(client);
257
+ if (!store?.db) {
258
+ return { ok: false, error: 'hub_db_not_found' };
259
+ }
260
+ ensurePipelineTable(store.db);
261
+ const pipeline = createPipeline(store.db, payload.team_name);
262
+ return pipeline.advance(payload.phase);
263
+ }
264
+
265
+ case 'pipeline_init': {
266
+ if (client) touchClient(client);
267
+ if (!store?.db) {
268
+ return { ok: false, error: 'hub_db_not_found' };
269
+ }
270
+ ensurePipelineTable(store.db);
271
+ const state = initPipelineState(store.db, payload.team_name, {
272
+ fix_max: payload.fix_max,
273
+ ralph_max: payload.ralph_max,
274
+ });
275
+ return { ok: true, data: state };
276
+ }
277
+
278
+ case 'delegator_delegate': {
279
+ if (!delegatorService) {
280
+ return { ok: false, error: { code: 'DELEGATOR_NOT_AVAILABLE', message: 'Delegator service가 초기화되지 않았습니다' } };
281
+ }
282
+ if (client) touchClient(client);
283
+ const result = await delegatorService.delegate(payload);
284
+ return { ok: result?.ok !== false, data: result };
285
+ }
286
+
287
+ case 'delegator_reply': {
288
+ if (!delegatorService) {
289
+ return { ok: false, error: { code: 'DELEGATOR_NOT_AVAILABLE', message: 'Delegator service가 초기화되지 않았습니다' } };
290
+ }
291
+ if (client) touchClient(client);
292
+ const result = await delegatorService.reply(payload);
293
+ return { ok: result?.ok !== false, data: result };
294
+ }
295
+
296
+ default:
297
+ return {
298
+ ok: false,
299
+ error: { code: 'UNKNOWN_PIPE_COMMAND', message: `지원하지 않는 command: ${action}` },
300
+ };
301
+ }
302
+ }
303
+
304
+ function buildReplayMessages(agentId, payload = {}) {
305
+ const maxMessages = Math.max(1, Math.min(Number(payload.max_messages) || 20, 100));
306
+ const pending = router.getPendingMessages(agentId, {
307
+ max_messages: maxMessages,
308
+ include_topics: payload.topics,
309
+ });
310
+ if (!store?.getAuditMessagesForAgent) {
311
+ return pending.slice(0, maxMessages);
312
+ }
313
+
314
+ const audit = store.getAuditMessagesForAgent(agentId, {
315
+ max_messages: maxMessages,
316
+ include_topics: payload.topics,
317
+ });
318
+ const byId = new Map();
319
+ for (const message of [...pending, ...audit]) {
320
+ if (!message?.id || byId.has(message.id)) continue;
321
+ byId.set(message.id, message);
322
+ }
323
+ return Array.from(byId.values())
324
+ .sort((left, right) => right.created_at_ms - left.created_at_ms)
325
+ .slice(0, maxMessages);
326
+ }
327
+
328
+ async function processQuery(client, action, payload = {}) {
329
+ switch (action) {
330
+ case 'drain': {
331
+ const agentId = resolveAgentId(client, payload);
332
+ const messages = router.drainAgent(agentId, {
333
+ max_messages: payload.max_messages,
334
+ include_topics: payload.topics,
335
+ auto_ack: payload.auto_ack,
336
+ });
337
+ if (client) touchClient(client);
338
+ return {
339
+ ok: true,
340
+ data: { messages, count: messages.length, server_time_ms: Date.now() },
341
+ };
342
+ }
343
+
344
+ case 'context': {
345
+ const agentId = resolveAgentId(client, payload);
346
+ const messages = buildReplayMessages(agentId, payload);
347
+ if (client) touchClient(client);
348
+ return {
349
+ ok: true,
350
+ data: { messages, count: messages.length, server_time_ms: Date.now() },
351
+ };
352
+ }
353
+
354
+ case 'status': {
355
+ const scope = payload.scope || 'hub';
356
+ if (client) touchClient(client);
357
+ return router.getStatus(scope, payload);
358
+ }
359
+
360
+ case 'assign_status': {
361
+ if (client) touchClient(client);
362
+ return router.getAssignStatus(payload);
363
+ }
364
+
365
+ case 'team_info': {
366
+ const result = await teamInfo(payload);
367
+ if (client) touchClient(client);
368
+ return result;
369
+ }
370
+
371
+ case 'team_task_list': {
372
+ const result = await teamTaskList(payload);
373
+ if (client) touchClient(client);
374
+ return result;
375
+ }
376
+
377
+ case 'pipeline_state': {
378
+ if (client) touchClient(client);
379
+ if (!store?.db) {
380
+ return { ok: false, error: 'hub_db_not_found' };
381
+ }
382
+ ensurePipelineTable(store.db);
383
+ const state = readPipelineState(store.db, payload.team_name);
384
+ return state
385
+ ? { ok: true, data: state }
386
+ : { ok: false, error: 'pipeline_not_found' };
387
+ }
388
+
389
+ case 'pipeline_list': {
390
+ if (client) touchClient(client);
391
+ if (!store?.db) {
392
+ return { ok: false, error: 'hub_db_not_found' };
393
+ }
394
+ ensurePipelineTable(store.db);
395
+ return { ok: true, data: listPipelineStates(store.db) };
396
+ }
397
+
398
+ case 'delegator_status': {
399
+ if (!delegatorService) {
400
+ return { ok: false, error: { code: 'DELEGATOR_NOT_AVAILABLE', message: 'Delegator service가 초기화되지 않았습니다' } };
401
+ }
402
+ if (client) touchClient(client);
403
+ const result = await delegatorService.status(payload);
404
+ return { ok: result?.ok !== false, data: result };
405
+ }
406
+
407
+ default:
408
+ return {
409
+ ok: false,
410
+ error: { code: 'UNKNOWN_PIPE_QUERY', message: `지원하지 않는 query: ${action}` },
411
+ };
412
+ }
413
+ }
414
+
415
+ function onMessage(agentId, message) {
416
+ if (!agentId || !message) return;
417
+ if (router.markMessagePushed(agentId, message.id)) {
418
+ pushEvent(agentId, message);
419
+ return;
420
+ }
421
+ pushEvent(agentId, message);
422
+ }
423
+
424
+ async function handleFrame(client, frame) {
425
+ if (!frame || typeof frame !== 'object') {
426
+ return sendResponse(client, null, {
427
+ ok: false,
428
+ error: { code: 'INVALID_FRAME', message: 'JSON object frame required' },
429
+ });
430
+ }
431
+
432
+ if (!frame.type) {
433
+ return sendResponse(client, frame.request_id || null, {
434
+ ok: false,
435
+ error: { code: 'INVALID_FRAME', message: 'type required' },
436
+ });
437
+ }
438
+
439
+ touchClient(client);
440
+
441
+ try {
442
+ if (frame.type === 'command') {
443
+ const action = frame.payload?.action || frame.payload?.command;
444
+ const result = await processCommand(client, action, frame.payload || {});
445
+ return sendResponse(client, frame.payload?.request_id || frame.request_id || null, result);
446
+ }
447
+ if (frame.type === 'query') {
448
+ const action = frame.payload?.action || frame.payload?.query;
449
+ const result = await processQuery(client, action, frame.payload || {});
450
+ return sendResponse(client, frame.payload?.request_id || frame.request_id || null, result);
451
+ }
452
+ return sendResponse(client, frame.request_id || null, {
453
+ ok: false,
454
+ error: { code: 'INVALID_FRAME_TYPE', message: `지원하지 않는 type: ${frame.type}` },
455
+ });
456
+ } catch (error) {
457
+ return sendResponse(client, frame.request_id || null, {
458
+ ok: false,
459
+ error: { code: 'PIPE_REQUEST_FAILED', message: error.message },
460
+ });
461
+ }
462
+ }
463
+
464
+ function attachSocket(socket) {
465
+ const client = {
466
+ id: randomUUID(),
467
+ socket,
468
+ buffer: '',
469
+ agentId: null,
470
+ subscriptions: new Set(),
471
+ lastHeartbeatMs: Date.now(),
472
+ closed: false,
473
+ };
474
+ clients.set(client.id, client);
475
+
476
+ socket.setEncoding('utf8');
477
+ socket.on('data', async (chunk) => {
478
+ client.buffer += chunk;
479
+ let newlineIndex = client.buffer.indexOf('\n');
480
+ while (newlineIndex >= 0) {
481
+ const line = client.buffer.slice(0, newlineIndex).trim();
482
+ client.buffer = client.buffer.slice(newlineIndex + 1);
483
+ if (line) {
484
+ const frame = safeJsonParse(line);
485
+ await handleFrame(client, frame);
486
+ }
487
+ newlineIndex = client.buffer.indexOf('\n');
488
+ }
489
+ });
490
+
491
+ socket.on('close', () => closeClient(client));
492
+ socket.on('error', () => closeClient(client));
493
+ }
494
+
495
+ function startHeartbeatMonitor() {
496
+ heartbeatTimer = setInterval(() => {
497
+ const now = Date.now();
498
+ for (const client of clients.values()) {
499
+ if (now - client.lastHeartbeatMs <= heartbeatTtlMs) continue;
500
+ sendFrame(client, {
501
+ type: 'event',
502
+ event: 'disconnect',
503
+ payload: { reason: 'heartbeat_timeout' },
504
+ });
505
+ closeClient(client);
506
+ }
507
+ }, Math.max(1000, Math.floor(heartbeatTtlMs / 2)));
508
+ heartbeatTimer.unref();
509
+ }
510
+
511
+ return {
512
+ path: pipePath,
513
+
514
+ async start() {
515
+ if (server) return { path: pipePath };
516
+
517
+ if (!IS_WINDOWS && existsSync(pipePath)) {
518
+ try { unlinkSync(pipePath); } catch {}
519
+ }
520
+
521
+ server = net.createServer(attachSocket);
522
+ router.deliveryEmitter.on('message', onMessage);
523
+
524
+ await new Promise((resolve, reject) => {
525
+ server.once('error', reject);
526
+ server.listen(pipePath, () => {
527
+ server.off('error', reject);
528
+ resolve();
529
+ });
530
+ });
531
+
532
+ startHeartbeatMonitor();
533
+ return { path: pipePath };
534
+ },
535
+
536
+ async stop() {
537
+ if (heartbeatTimer) {
538
+ clearInterval(heartbeatTimer);
539
+ heartbeatTimer = null;
540
+ }
541
+
542
+ router.deliveryEmitter.off('message', onMessage);
543
+
544
+ for (const client of clients.values()) {
545
+ closeClient(client);
546
+ }
547
+
548
+ if (server) {
549
+ const current = server;
550
+ server = null;
551
+ await new Promise((resolve) => current.close(resolve));
552
+ }
553
+
554
+ if (!IS_WINDOWS && existsSync(pipePath)) {
555
+ try { unlinkSync(pipePath); } catch {}
556
+ }
557
+ },
558
+
559
+ getStatus() {
560
+ return {
561
+ path: pipePath,
562
+ protocol: 'ndjson',
563
+ clients: clients.size,
564
+ pending_messages: Array.from(clients.values()).reduce((sum, client) => {
565
+ if (!client.agentId) return sum;
566
+ return sum + router.countPendingMessages(client.agentId);
567
+ }, 0),
568
+ };
569
+ },
570
+
571
+ async executeCommand(action, payload) {
572
+ return await processCommand(null, action, payload);
573
+ },
574
+
575
+ async executeQuery(action, payload) {
576
+ return await processQuery(null, action, payload);
577
+ },
578
+ };
579
+ }