File size: 8,941 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// ์„ค๋ช… ๋ฌธ์„œ: 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 };