Download src/utils/cli-activity-stream.js from VanyaJ/bada-terminal: direct link, hf CLI and curl.
- Browser
- Download file 5.62 kB
-
https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/utils/cli-activity-stream.js
- Command line
-
hf download hf://VanyaJ/bada-terminal/src/utils/cli-activity-stream.js
-
curl -L -o cli-activity-stream.js https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/utils/cli-activity-stream.js
5.62 kB
| // ์ค๋ช ๋ฌธ์: /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 }; | |