Download src/openwork/cli-provider-request.js from VanyaJ/bada-terminal: direct link, hf CLI and curl.
- Browser
- Download file 8.94 kB
-
https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/openwork/cli-provider-request.js
- Command line
-
hf download hf://VanyaJ/bada-terminal/src/openwork/cli-provider-request.js
-
curl -L -o cli-provider-request.js https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/openwork/cli-provider-request.js
8.94 kB
| // ์ค๋ช ๋ฌธ์: docs/specs/openwork-cli-provider.md (v2.0.1) | |
| // ๋ธ๋ผ์ฐ์ ๊ฐ ์คํ ๋ช ๋ น/์์ ํด๋๋ฅผ ์ง์ ํ์ง ๋ชปํ๊ฒ ํธ์คํธ๊ฐ ๊ณ ์ ํ ๋ชจ๋ธ ๋ชฉ๋ก๋ง ํด์ํ๋ค. | |
| const { randomUUID } = require('crypto'); | |
| const protocol = require('./cli-tool-protocol'); | |
| async function infer(options) { | |
| const cli = require('../config/cli-paths').normalizeExecutableName(require('../config/claude-execution-config').shellTokens(options.cliCommand)[0]?.value || ''); | |
| if (cli === 'claude') return require('./cli-claude-inference').runClaudeInference(options); | |
| if (['agy', 'antigravity', 'antigravity-cli'].includes(cli)) { | |
| return require('../services/bong-antigravity-chat').runAntigravityChat({ | |
| ...options, | |
| agent: require('../services/bong-antigravity-agent').INFERENCE_AGENT_NAME, | |
| profile: options.profile || { skills: [], knowledgePaths: [] } | |
| }); | |
| } | |
| throw fail('BADA_CLI_MODEL', 422); | |
| } | |
| const fail = (code, status = 400) => Object.assign(Error(code), { code, status }); | |
| function modelsForWorkspace(cwd) { | |
| const models = new Map([['antigravity-flash', { name: 'Antigravity ยท Gemini 3.8 Flash', cliCommand: 'agy', model: 'gemini-3.8-flash-medium', profile: null }]]); | |
| models.set('claude-sonnet', { name: 'Claude Code ยท Sonnet', cliCommand: 'claude', model: 'sonnet', profile: null }); | |
| const store = require('../agent-profile-manager').loadProfiles(cwd, undefined, { readOnly: true }); | |
| const paths = require('../config/cli-paths'), shell = require('../config/claude-execution-config'); | |
| for (const [id, profile] of Object.entries(store.profiles || {}).slice(0, 100)) { | |
| const tokens = shell.shellTokens(profile.cliCommand || ''); | |
| if (!/^[\w-]{1,120}$/.test(id) || tokens.length !== 1 || !['claude', 'agy', 'antigravity', 'antigravity-cli'].includes(paths.normalizeExecutableName(tokens[0].value)) | |
| || profile.cliArguments?.length || typeof profile.model !== 'string' || !/^[\w.-]{1,160}$/.test(profile.model)) continue; | |
| models.set('profile-' + id, { name: (paths.normalizeExecutableName(tokens[0].value) === 'claude' ? 'Claude Code ยท ' : 'Antigravity ยท ') + String(profile.name || id).slice(0, 100), cliCommand: profile.cliCommand, model: profile.model, profile: { ...profile, id } }); | |
| } | |
| return models; | |
| } | |
| function history(payload) { | |
| if (!payload || typeof payload !== 'object' || Array.isArray(payload) || typeof payload.model !== 'string' | |
| || payload.stream !== undefined && typeof payload.stream !== 'boolean' | |
| || !Array.isArray(payload.messages) || !payload.messages.length || payload.messages.length > 256) throw fail('BADA_CLI_INVALID'); | |
| return payload.messages.map(message => { | |
| if (!message || !['system', 'developer', 'user', 'assistant', 'tool'].includes(message.role)) throw fail('BADA_CLI_INVALID'); | |
| let content = message.content; | |
| if (Array.isArray(content)) { | |
| if (!content.every(part => part?.type === 'text' && typeof part.text === 'string')) throw fail('BADA_CLI_TEXT_ONLY'); | |
| content = content.map(part => part.text).join('\n'); | |
| } | |
| if (typeof content !== 'string') throw fail('BADA_CLI_TEXT_ONLY'); | |
| return { role: message.role, content }; | |
| }); | |
| } | |
| async function execute({ selected, messages, payload, cwd, signal, runner = infer }) { | |
| // ์ถ๋ก CLI๋ ๋๊ตฌ๋ฅผ ์คํํ์ง ์๋๋ค. ์ค์ ์คํ๊ณผ ์น์ธ์ OpenCode ์์ง์ด ์์ ํ๋ค. | |
| const request = await protocol.parseRequest(payload || { model: selected.model, messages }, { signal }); | |
| const localRequest = require('./cli-system-steering').normalizeSystemSteering(request); | |
| const projection = require('./cli-inference-projection').projectRequest(localRequest, { cwd }); | |
| const prompt = protocol.buildPrompt(projection.request); | |
| require('./cli-inference-diagnostics').record(projection.request, prompt, undefined, request); | |
| const ledger = require('../services/runtime-usage-ledger'); | |
| const run = { runId: 'openwork_cli_' + randomUUID(), projectId: cwd, workDir: cwd, | |
| runBudgetUsd: require('../config-manager').getConfig().conductorRequestBudgetUsd ?? null }; | |
| // ๋ช ์ ์์ฒญ ์์ฐ์ ์ ์ฒด ๊ธ์ก์ ์์ฝํด ๋ฏธ์ ์ฌ์ฉ๋/์ด๊ณผ๋ฅผ ์ฑ๊ณต์ผ๋ก ๋ฐํํ์ง ์๋๋ค. | |
| run.requestReserveUsd = run.runBudgetUsd; | |
| const owner = { type: 'openwork_cli', projectId: cwd, requestId: randomUUID() }; | |
| const lease = require('../services/http-usage-owner').acquire(run, owner); | |
| let report = null, text; | |
| const streamDiagnostics = require('./cli-response-stream').createStreamDiagnostics(); | |
| try { | |
| ledger.bind(run, { owner }); | |
| text = await runner({ cliCommand: selected.cliCommand, model: selected.model, profile: selected.profile, | |
| prompt, | |
| cwd, signal, timeoutMs: 120000, maxBytes: 524288, permissionBypass: false, requireRuntimePolicy: true, | |
| onDelta: streamDiagnostics.onDelta, | |
| usageOwner: run, onUsage: usage => { | |
| // ์์ฅ ํฉ์ฐ์ฉ 0๊ณผ ๋ฏธ์ ๋น์ฉ์ ๊ตฌ๋ถํ๋ค. source=unknown๊ณผ null ๋ณด๊ณ ๋ฅผ ๋ณด์กดํ๋ค. | |
| require('../utils/run-usage').recordUsage(run, usage.costUsd ?? 0, usage); report = usage; | |
| } }); | |
| signal.throwIfAborted(); | |
| require('../services/runtime-request-reservation').assertReady(run, { settled: true }); | |
| if (typeof text !== 'string' || !text.trim()) throw fail('BADA_CLI_EMPTY', 422); | |
| const decision = projection.restore(await protocol.parseDecision(text, projection.request, { signal })); | |
| // ๋ชจ๋ธ์ ํธ์คํธ ๋งคํ์ ์ ๊ณตํ ์ ์๋ค. ๋ณต์๋ ์ธ์๋ ์๋ณธ ์คํค๋ง๋ก ๋ค์ ๊ฒ์ฆํ๋ค. | |
| const restored = decision.tool_calls?.length | |
| ? { type: 'tool_calls', calls: decision.tool_calls.map(call => ({ name: call.function.name, arguments: JSON.parse(call.function.arguments) })) } | |
| : { type: 'message', content: decision.content }; | |
| const message = await protocol.parseDecision(JSON.stringify(restored), request, { signal }); | |
| return { text: message.content || '', message, receipt: { usageRunId: run.runId, costUsd: report?.costUsd ?? null, totalTokens: report?.totalTokens ?? null, costSource: report?.costSource || 'unknown' } }; | |
| } catch (error) { | |
| try { error.protocolStream = streamDiagnostics.snapshot(text); } | |
| catch { /* ์ง๋จ ์คํจ๋ ๊ธฐ์กด ์คํ ์ค๋ฅ์ ์ ์ฐ ์์์ฆ์ ๋์ฒดํ์ง ์๋๋ค. */ } | |
| error.bada = { usageRunId: run.runId, costUsd: report?.costUsd ?? null, totalTokens: report?.totalTokens ?? null, costSource: report?.costSource || 'unknown' }; | |
| throw error; | |
| } finally { | |
| // ๊ณตํต runner๋ ์ค์ ์์ close/์ฌ์ฉ๋ ์ ์ฐ์ ์๋ฃํ ๋ค ๋ฐํํ๋ค. | |
| try { streamDiagnostics.dispose(); } | |
| catch { /* ๊ด์ธก ์ ๋ฆฌ ์คํจ๊ฐ ์๋ ๊ฒฐ๊ณผ๋ฅผ ๋ฎ์ง ์๊ฒ ํ๋ค. */ } | |
| finally { lease.close(); } | |
| } | |
| } | |
| // ์๋ฌธ stderrยทํ๋กฌํํธ๋ฅผ ๋ ธ์ถํ์ง ์๊ณ ๊ณต๊ฐ ์ฝ๋๋ก ๋ค์ ํ๋์ ์๋ดํ๋ค. | |
| function failureResponse(error, executionStarted) { | |
| const code = /^[A-Z_]{1,80}$/.test(error.code || '') ? error.code : 'BADA_CLI_FAILED'; | |
| const messages = { | |
| BADA_CLI_EMPTY: 'CLI ์ถ๋ก ์ด ๋ต๋ณ ์์ด ์ข ๋ฃํ์ต๋๋ค. ์คํ ๊ธฐ๋ก์ ํ์ธํ์ธ์.', | |
| BADA_CLI_TOOL_PROTOCOL: '๋ชจ๋ธ์ ๋๊ตฌ ์ ํ ํ์์ ๊ฒ์ฆํ์ง ๋ชปํด ์ด๋ฒ ๋๊ตฌ ์คํ์ ๊ฑฐ๋ถํ์ต๋๋ค.', | |
| BONG_AGY_TOOLS_DENIED: 'Antigravity๊ฐ GUI ๋๊ตฌ ์ ํ ๋์ ์์ฒด ๋๊ตฌ๋ฅผ ํธ์ถํ๋ ค ํด ์ค๋จํ์ต๋๋ค. ์์ฒด ๋๊ตฌ ์ฐจ๋จ์ ์ ์ง๋ฉ๋๋ค.', | |
| CLI_EXIT_FAILED: 'Antigravity ์คํ์ด ์ ์ ์๋ฃ๋์ง ์์์ต๋๋ค. ์์ ์ด ์ผ๋ถ ์คํ๋์ ์ ์์ผ๋ฏ๋ก ๊ฒฐ๊ณผ์ ์คํ ๊ธฐ๋ก์ ํ์ธํ์ธ์.', | |
| HOST_ISOLATION_UNAVAILABLE: 'ํ์ฌ ๋ณด์ ๋ชจ๋๋ ์ด ์คํ ๊ฒฝ๋ก๋ฅผ ์ง์ํ์ง ์์ ์ฐจ๋จํ์ต๋๋ค.', | |
| HOST_SECURITY_POLICY_INVALID: '๋ณด์ ์ค์ ์ ํ์ธํ ์ ์์ด ์คํ์ ์ฐจ๋จํ์ต๋๋ค. ๋ณด์ ์ค์ ์ํ๋ฅผ ํ์ธํ์ธ์.', | |
| HOST_SECURITY_POLICY_CHANGED: '์ค๋น ์ค ๋ณด์ ์ค์ ์ด ๋ณ๊ฒฝ๋์ด ์คํ์ ์ค๋จํ์ต๋๋ค.', | |
| USAGE_RECONCILIATION_REQUIRED: '์ฌ์ฉ๋ ์ ์ฐ ํ์ธ์ด ํ์ํฉ๋๋ค. ์คํ ๊ธฐ๋ก๊ณผ ์ฌ์ฉ๋์ ํ์ธํ ๋ค ๋ค์ ์์ ์ ์งํํ์ธ์.', | |
| BUDGET_EXCEEDED: '์์ฒญ ์์ฐ์ ์ด๊ณผํ์ต๋๋ค. ์ฌ์ฉ๋๊ณผ ์์ฐ ์ค์ ์ ํ์ธํ์ธ์.', | |
| BADA_CLI_BUSY: '๋์ ์์ฒญ ํ๋์ ๋๋ฌํ์ต๋๋ค. ์งํ ์ค์ธ ์์ ์ด ๋๋ ๋ค ๋ค์ ์์ฒญํ์ธ์.' | |
| }; | |
| const guidance = executionStarted | |
| ? ' ์๋ ์ฌ์๋ํ์ง ์์ต๋๋ค. CLI๋ ์ถ๋ก ๋ง ๋ด๋นํ๋ฉฐ ๋๊ตฌ ์คํ๊ณผ ์น์ธ์ ๋ฐ๋ค์ํฌ์์ ์ฒ๋ฆฌํฉ๋๋ค.' : ''; | |
| return { status: executionStarted ? 422 : error.status || 400, body: { ...(error.bada ? { bada: error.bada } : {}), | |
| error: { code, message: (messages[code] || '์์ฒญ์ ์๋ฃํ์ง ๋ชปํ์ต๋๋ค. ์ค๋ฅ ์ฝ๋์ ์คํ ๊ธฐ๋ก์ ํ์ธํ์ธ์.') + guidance, | |
| ...(executionStarted ? { retryable: false } : {}) } } }; | |
| } | |
| module.exports = { modelsForWorkspace, history, execute, fail, failureResponse }; | |