File size: 5,624 Bytes
d739b1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// ์„ค๋ช… ๋ฌธ์„œ: /docs/specs/bada-bong-agent-telemetry.md (v0.3.0)
// OpenCode ์ข…๋ฃŒ ์ด๋ฒคํŠธ์™€ ๋ณ„๋„ Claude ๊ณ„์•ฝ์„ ๊ณตํ†ต bounded JSONL ํ”„๋ ˆ์ด๋ฐ์œผ๋กœ ์ „๋‹ฌํ•œ๋‹ค. ๋ณธ๋ฌธ์€ ๊ณต๊ฐœํ•˜์ง€ ์•Š๋Š”๋‹ค.
// ๊ทผ๊ฑฐ: https://github.com/anomalyco/opencode/blob/dev/packages/opencode/src/cli/cmd/run.ts (2026-10-18 ์กฐํšŒ)
const { StringDecoder } = require('node:string_decoder');
const { createClaudeActivityEvents } = require('./claude-activity-events');
function createCliActivityStream(options = {}) {
  const noop = { feed() {}, end() {}, stop() {} };
  try {
    const { cli, transport = 'pipe', onActivity, maxBytes = 2 * 1024 * 1024 } = options;
    if (!['opencode', 'codeagent', 'claude'].includes(cli) || transport !== 'pipe' || typeof onActivity !== 'function' ||
        !Number.isSafeInteger(maxBytes) || maxBytes <= 0) return noop;
    const totalLimit = Math.min(maxBytes, 2 * 1024 * 1024), lineLimit = Math.min(totalLimit, 65536);
    const decoder = new StringDecoder('utf8'), seen = new Map(), coverage = new Set();
    let stopped = false, dropping = false, total = 0, line = '', lineBytes = 0, session = null;
    const id = value => typeof value === 'string' && /^[A-Za-z0-9_:-]{1,200}$/.test(value);
    const object = value => value !== null && typeof value === 'object' && !Array.isArray(value);
    function emit(activity) {
      try { const result = onActivity(Object.freeze(activity)); if (result?.then) Promise.resolve(result).catch(() => {}); }
      catch { /* ๊ด€์ œ callback ์‹คํŒจ๋Š” ์‹คํ–‰ ๊ฒฐ๊ณผยท๋น„์šฉ ์ •์‚ฐยท์ž์‹ ํšŒ์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๋Š”๋‹ค. */ }
    }
    function incomplete(code) { if (!coverage.has(code)) { coverage.add(code); emit({ kind: 'coverage', code }); } }
    const claude = cli === 'claude' ? createClaudeActivityEvents({ emit, incomplete }) : null;
    function stop() { stopped = true; line = ''; lineBytes = 0; session = null; seen.clear(); claude?.stop(); }
    function parse(value) {
      try {
        if (!value.trim()) return;
        const event = JSON.parse(value);
        if (!object(event)) { incomplete('invalid'); return; }
        if (claude) { claude.observe(event); return; }
        // ์ •์ƒ text/reasoning/usage ์ด๋ฒคํŠธ์˜ ๋‚ด์šฉ์ด๋‚˜ ์ƒํƒœ๋Š” ๊ฒ€์‚ฌํ•˜์ง€ ์•Š๋Š”๋‹ค.
        if (!['tool_use', 'step_start'].includes(event.type)) return;
        const part = event.part;
        if (!object(part) || !id(event.sessionID) || part.sessionID !== event.sessionID ||
            !id(part.id) || !id(part.messageID)) { incomplete('invalid'); return; }
        // stdout๋Š” ํ•œ ๋ฃจํŠธ ์„ธ์…˜์˜ ์ „์†ก๋กœ๋‹ค. ๋‹ค๋ฅธ ์„ธ์…˜ ์ด๋ฒคํŠธ๋ฅผ ๋‚ด๋ถ€ ์„œ๋ธŒ ์—์ด์ „ํŠธ๋กœ ์ถ”์ธกํ•˜์ง€ ์•Š๋Š”๋‹ค.
        if (session && event.sessionID !== session) return;
        if (event.type === 'step_start') {
          if (part.type !== 'step-start') { incomplete('invalid'); return; }
          session ||= event.sessionID; return;
        }
        if (part.type !== 'tool' || !id(part.callID) || typeof part.tool !== 'string' ||
            !/^[A-Za-z][A-Za-z0-9_.:-]{0,79}$/.test(part.tool) || !object(part.state)) { incomplete('invalid'); return; }
        // ํ˜„์žฌ stdout ๊ณ„์•ฝ์— ์—†๋Š” ์‹œ์ž‘ ์ƒํƒœ๋ฅผ ์ข…๋ฃŒยทํ™œ์„ฑ ์—์ด์ „ํŠธ๋กœ ์Šน๊ฒฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
        if (['pending', 'running'].includes(part.state.status)) return;
        if (!['completed', 'error'].includes(part.state.status)) { incomplete('invalid'); return; }
        session ||= event.sessionID;
        const signature = JSON.stringify([part.id, part.messageID, part.tool, part.state.status]);
        // ๊ฐ™์€ callID์˜ ์ถฉ๋Œ์€ ์ฒซ ๊ด€์ธก์„ ๋ณด์กดํ•˜๊ณ  ์ˆ˜์ง‘ ํ•œ๊ณ„๋ฅผ ๋ช…์‹œํ•œ๋‹ค.
        if (seen.has(part.callID)) { if (seen.get(part.callID) !== signature) incomplete('invalid'); return; }
        if (seen.size >= 512) { incomplete('truncated'); stop(); return; }
        seen.set(part.callID, signature);
        emit({ kind: 'tool', id: part.callID, tool: part.tool, status: part.state.status === 'completed' ? 'done' : 'failed' });
      } catch { incomplete('invalid'); }
    }
    function append(value) {
      if (dropping) return;
      const bytes = Buffer.byteLength(value, 'utf8');
      if (lineBytes + bytes > lineLimit) { line = ''; lineBytes = 0; dropping = true; incomplete('truncated'); return; }
      line += value; lineBytes += bytes;
    }
    function consume(text) {
      let start = 0, end;
      while (!stopped && (end = text.indexOf('\n', start)) !== -1) {
        append(text.slice(start, end));
        if (!dropping) parse(line);
        line = ''; lineBytes = 0; dropping = false; start = end + 1;
      }
      if (!stopped) append(text.slice(start));
    }
    return {
      feed(chunk) {
        if (stopped) return;
        try {
          if (typeof chunk !== 'string' && !Buffer.isBuffer(chunk) && !(chunk instanceof Uint8Array)) return;
          const size = typeof chunk === 'string' ? Buffer.byteLength(chunk, 'utf8') : chunk.byteLength;
          if (size > totalLimit - total) { incomplete('truncated'); stop(); return; }
          total += size;
          // Buffer์˜ UTF-8 ๊ฒฝ๊ณ„๊ฐ€ ์—ฌ๋Ÿฌ stdout chunk์— ๊ฑธ๋ ค๋„ JSON ๋ฌธ์ž์—ด์„ ํ›ผ์†ํ•˜์ง€ ์•Š๋Š”๋‹ค.
          consume(decoder.write(typeof chunk === 'string' ? Buffer.from(chunk, 'utf8') : chunk));
        } catch { incomplete('invalid'); stop(); }
      },
      end() {
        if (stopped) return;
        try { consume(decoder.end()); if (!stopped && !dropping) parse(line); }
        catch { incomplete('invalid'); }
        finally { stop(); }
      },
      stop,
    };
  } catch { return noop; }
}
module.exports = { createCliActivityStream };