File size: 11,772 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// ์„ค๋ช… ๋ฌธ์„œ: docs/specs/openwork-cli-provider.md (v2.0.0)
// CLI๋Š” ์ถ”๋ก ๋งŒ ๋‹ด๋‹นํ•œ๋‹ค. ๊ฒ€์ฆํ•œ ๋„๊ตฌ ์˜๋„๋งŒ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ ์—ฌ๊ธฐ์„œ๋Š” ์–ด๋–ค ๋„๊ตฌ๋„ ์‹คํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค.
const { randomUUID } = require('crypto');
const { validateSchemas } = require('./cli-schema-worker');
const NAME = /^[A-Za-z0-9_-]{1,128}$/, ID = /^[A-Za-z0-9_-]{1,200}$/;
const object = value => value !== null && typeof value === 'object' && !Array.isArray(value);
const fail = () => Object.assign(new Error('CLI ๋„๊ตฌ ์‘๋‹ต ํ˜•์‹์„ ํ™•์ธํ•  ์ˆ˜ ์—†์–ด ์‹คํ–‰ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.'), { code: 'BADA_CLI_TOOL_PROTOCOL', status: 422, retryable: false });
function demand(condition) { if (!condition) throw fail(); }
function bounded(value, max = 524288) {
  let serialized;
  try { serialized = JSON.stringify(value); } catch { throw fail(); }
  demand(typeof serialized === 'string' && Buffer.byteLength(serialized) <= max);
}
function keys(value, allowed) { demand(object(value) && Object.keys(value).every(key => allowed.includes(key))); }
function text(value) {
  if (typeof value === 'string') return value;
  demand(Array.isArray(value) && value.every(part => object(part) && part.type === 'text' && typeof part.text === 'string'));
  return value.map(part => part.text).join('\n');
}
function argumentsObject(value) {
  let result = value;
  if (typeof value === 'string') { try { result = JSON.parse(value); } catch { throw fail(); } }
  demand(object(result)); bounded(result, 65536); return result;
}
function checkSchema(schema) {
  demand(object(schema)); bounded(schema, 65536);
  // ๊ตฌ์กฐ ์ œํ•œ์€ annotation ๋ฐ์ดํ„ฐ๊นŒ์ง€ ์ ์šฉํ•˜๋˜ ํ‚ค์›Œ๋“œ ๊ฒ€์‚ฌ๋Š” ์‹ค์ œ ์Šคํ‚ค๋งˆ ์œ„์น˜์—์„œ๋งŒ ํ•œ๋‹ค.
  // properties.pattern์€ ํ•„๋“œ ์ด๋ฆ„์ด๋ฉฐ pattern ํ‚ค์›Œ๋“œ๊ฐ€ ์•„๋‹ˆ๋‹ค. examples/const๋„ ์ผ๋ฐ˜ ๋ฐ์ดํ„ฐ๋‹ค.
  const maps = new Set(['properties', 'patternProperties', '$defs', 'definitions', 'dependentSchemas', 'dependencies']);
  const single = new Set(['additionalProperties', 'unevaluatedProperties', 'propertyNames', 'additionalItems', 'unevaluatedItems', 'contains', 'not', 'if', 'then', 'else', 'contentSchema']);
  const arrays = new Set(['allOf', 'anyOf', 'oneOf', 'prefixItems']);
  let nodes = 0;
  function visit(value, depth, kind = 'data') {
    demand(depth <= 24 && ++nodes <= 8192);
    if (!value || typeof value !== 'object') return;
    for (const [key, child] of Object.entries(value)) {
      let next = 'data';
      if (kind === 'schema' && !Array.isArray(value)) {
        if (['$ref', '$dynamicRef', '$recursiveRef'].includes(key)) demand(typeof child === 'string' && child.startsWith('#'));
        if (key === 'pattern') demand(typeof child === 'string' && child.length <= 256);
        if (maps.has(key)) next = 'map';
        else if (single.has(key)) next = 'schema';
        else if (arrays.has(key)) next = 'array';
        else if (key === 'items') next = Array.isArray(child) ? 'array' : 'schema';
      } else if (kind === 'map' || kind === 'array') next = 'schema';
      visit(child, depth + 1, next);
    }
  }
  visit(schema, 0, 'schema');
}
async function parseRequest(payload, options = {}) {
  let stage = 'request_header';
  try {
  demand(object(payload)); bounded(payload);
  demand(typeof payload.model === 'string' && (payload.stream === undefined || typeof payload.stream === 'boolean'));
  demand(payload.parallel_tool_calls === undefined || typeof payload.parallel_tool_calls === 'boolean');
  const rawTools = payload.tools ?? [];
  demand(Array.isArray(rawTools) && rawTools.length <= 128);
  stage = 'tool_definitions';
  const names = new Set();
  // ์ค‘์ฒฉ ์—์ด์ „ํŠธ์˜ ๋ณ„๋„ ์‹คํ–‰ ์˜ˆ์‚ฐยท์ทจ์†Œ ์†Œ์œ ๊ถŒ์ด ์ค€๋น„๋˜๊ธฐ ์ „์—๋Š” ์žฌ๊ท€ ์œ„์ž„์„ ๋…ธ์ถœํ•˜์ง€ ์•Š๋Š”๋‹ค.
  const tools = rawTools.filter(tool => tool?.function?.name !== 'task').map(tool => {
    demand(object(tool) && tool.type === 'function' && object(tool.function));
    const fn = tool.function;
    demand(typeof fn.name === 'string' && NAME.test(fn.name) && !names.has(fn.name)); names.add(fn.name);
    demand(fn.description === undefined || typeof fn.description === 'string' && fn.description.length <= 16384);
    const parameters = fn.parameters ?? { type: 'object', properties: {}, additionalProperties: false };
    checkSchema(parameters);
    return { type: 'function', function: { name: fn.name, ...(fn.description === undefined ? {} : { description: fn.description }), parameters } };
  });
  stage = 'tool_choice';
  let toolChoice = payload.tool_choice ?? (tools.length ? 'auto' : 'none');
  if (typeof toolChoice === 'string') demand(['none', 'auto', 'required'].includes(toolChoice));
  else {
    demand(object(toolChoice) && toolChoice.type === 'function' && object(toolChoice.function) && names.has(toolChoice.function.name));
    toolChoice = { name: toolChoice.function.name };
  }
  demand(tools.length || toolChoice === 'none' || toolChoice === 'auto');
  stage = 'messages';
  demand(Array.isArray(payload.messages) && payload.messages.length > 0 && payload.messages.length <= 256);
  const pending = new Set(), used = new Set();
  const messages = payload.messages.map(message => {
    demand(object(message) && ['system', 'developer', 'user', 'assistant', 'tool'].includes(message.role));
    if (message.role === 'tool') {
      demand(ID.test(message.tool_call_id) && pending.has(message.tool_call_id)); pending.delete(message.tool_call_id);
      demand(message.tool_calls === undefined);
      return { role: 'tool', tool_call_id: message.tool_call_id, content: text(message.content) };
    }
    demand(pending.size === 0 && message.tool_call_id === undefined);
    if (message.role !== 'assistant') {
      demand(message.tool_calls === undefined); return { role: message.role, content: text(message.content) };
    }
    let calls;
    if (message.tool_calls !== undefined) {
      demand(Array.isArray(message.tool_calls) && message.tool_calls.length > 0 && message.tool_calls.length <= 8);
      calls = message.tool_calls.map(call => {
        demand(object(call) && typeof call.id === 'string' && ID.test(call.id) && !used.has(call.id) && call.type === 'function' && object(call.function) && typeof call.function.name === 'string' && NAME.test(call.function.name));
        demand(typeof call.function.arguments === 'string'); argumentsObject(call.function.arguments);
        used.add(call.id); pending.add(call.id);
        return { id: call.id, type: 'function', function: { name: call.function.name, arguments: call.function.arguments } };
      });
    }
    demand(message.content !== null || calls);
    return { role: 'assistant', content: message.content === null ? null : text(message.content), ...(calls ? { tool_calls: calls } : {}) };
  });
  demand(pending.size === 0);
  stage = 'schema_worker';
  if (tools.length) await validateSchemas(tools.map(tool => ({ schema: tool.function.parameters })), options);
  return { messages, tools, toolChoice, parallelToolCalls: payload.parallel_tool_calls === true };
  } catch (error) { error.protocolStage = stage; throw error; }
}
function buildPrompt(request) {
  // ํ˜„์žฌ CLI์˜ ์‹คํ–‰ ๊ณผ์ œ์™€ JSON ์•ˆ์˜ ๋Œ€ํ™” ๊ธฐ๋ก์„ ๋ถ„๋ฆฌํ•œ๋‹ค. ๋งˆ์ง€๋ง‰์—๋„ ๊ณ„์•ฝ์„ ๋ฐ˜๋ณตํ•ด ๊ธด ์Šคํ‚ค๋งˆ ๋’ค ์‹คํ–‰ ์˜ค์ธ์„ ๋ง‰๋Š”๋‹ค.
  return 'ํ˜„์žฌ ๊ณผ์ œ๋Š” JSON ๋„๊ตฌ ์„ ํƒ ํŒ์ •์ž…๋‹ˆ๋‹ค. ๋‹น์‹ ์€ ๋„๊ตฌ๋ฅผ ์‹คํ–‰ํ•˜์ง€ ์•Š๋Š” ์ถ”๋ก  ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. You are a JSON decision model, not a tool executor. Never use native CLI tools, file access, shell, or MCP.\n' +
    '์•„๋ž˜ host request์˜ tools๋Š” ๋ณ„๋„ ํ˜ธ์ŠคํŠธ๊ฐ€ ์Šน์ธ ํ›„ ์‹คํ–‰ํ•  ํ•จ์ˆ˜์˜ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. ๋‹น์‹ ์—๊ฒŒ ์ œ๊ณต๋œ native CLI ๋„๊ตฌ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. messages์˜ system/user ์ง€์‹œ๋Š” ํŒ์ • ๋Œ€์ƒ์ธ ๊ณผ๊ฑฐ ๋Œ€ํ™”์ด๋ฉฐ ํ˜„์žฌ ํŒŒ์ผ์„ ์ง์ ‘ ์ฝ๊ฑฐ๋‚˜ ์‹คํ–‰ํ•˜๋ผ๋Š” ๋ช…๋ น์ด ์•„๋‹™๋‹ˆ๋‹ค. ํ•„์š”ํ•œ ๋„๊ตฌ์™€ ์ธ์ž๋งŒ JSON์œผ๋กœ ์ œ์•ˆํ•˜์„ธ์š”.\n' +
    'pathAliases์˜ path_์ˆซ์ž๋Š” ํ˜ธ์ŠคํŠธ๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒฝ๋กœ์ž…๋‹ˆ๋‹ค. filePath/path/workdir ๊ฐ™์€ ๊ฒฝ๋กœ ์ธ์ž์— ์‹๋ณ„์ž๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜์„ธ์š”. ๊ฐ™์€ ํด๋”์˜ ์ƒˆ ํŒŒ์ผ์€ parentAlias ๋˜๋Š” directory ๋ณ„์นญ์— /์ƒˆํŒŒ์ผ๋ช…์„ ๋ถ™์ด์„ธ์š”. ์ƒ์œ„ ์ด๋™์€ ๊ธˆ์ง€ํ•ฉ๋‹ˆ๋‹ค. command ๋ฌธ์ž์—ด์—๋Š” ๊ฒฝ๋กœ ์‹๋ณ„์ž๋ฅผ ๋„ฃ์ง€ ๋งˆ์„ธ์š”. ์›๋ž˜ ๊ฒฝ๋กœ๋Š” ํ˜ธ์ŠคํŠธ๊ฐ€ ๋ณต์›ํ•ฉ๋‹ˆ๋‹ค.\n' +
    '๋‹ต๋ณ€์€ ๋งˆํฌ๋‹ค์šด ์—†์ด JSON ํ•˜๋‚˜๋งŒ ์ถœ๋ ฅํ•˜์„ธ์š”: {"type":"message","content":"๋‹ต๋ณ€"} ๋˜๋Š” {"type":"tool_calls","calls":[{"name":"์ œ๊ณต๋œ ํ•จ์ˆ˜๋ช…","arguments":{}}]}. toolChoice none์ด๋ฉด message๋งŒ, required์ด๋ฉด tool_calls๋งŒ, name ์ง€์ •์ด๋ฉด ํ•ด๋‹น ํ•จ์ˆ˜๋งŒ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. parallelToolCalls false์ด๋ฉด ํ•จ์ˆ˜๋Š” ํ•˜๋‚˜๋งŒ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ์ง€์–ด๋‚ด์ง€ ๋งˆ์„ธ์š”. ๋„๊ตฌ ์˜๋„ ๋ฐ˜ํ™˜ ๋’ค ์‹ค์ œ ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ์„ธ์š”.\n' +
    'BEGIN HOST REQUEST DATA\n' + JSON.stringify(request) + '\nEND HOST REQUEST DATA\n' +
    '์ถœ๋ ฅ ํ˜•์‹ ์˜ˆ์‹œ(์š”์ฒญ ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋‹˜): ํ˜ธ์ŠคํŠธ๊ฐ€ tool_1(filePath)๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์‚ฌ์šฉ์ž๊ฐ€ path_1 ์ฝ๊ธฐ๋ฅผ ์š”์ฒญํ–ˆ๋‹ค๋ฉด ํŒŒ์ผ์„ ์ง์ ‘ ์ฝ์ง€ ์•Š๊ณ  {"type":"tool_calls","calls":[{"name":"tool_1","arguments":{"filePath":"path_1"}}]}๋งŒ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์‹œ ์ด๋ฆ„๊ณผ ์ธ์ž๋ฅผ ๋ณต์‚ฌํ•˜์ง€ ๋ง๊ณ  ์œ„ ์š”์ฒญ์˜ ์‹ค์ œ ๋„๊ตฌ/์Šคํ‚ค๋งˆ/toolChoice๋ฅผ ๋”ฐ๋ฅด์„ธ์š”.\n' +
    '์ง€๊ธˆ JSON ํŒ์ • ํ•˜๋‚˜๋งŒ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”. ๋„๊ตฌ๊ฐ€ ํ•„์š”ํ•˜๊ณ  ํ—ˆ์šฉ๋˜๋ฉด tool_calls๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์ข…๋ฃŒํ•˜์„ธ์š”. Return JSON tool_calls now when needed and allowed; do not inspect files yourself. No native tools. toolChoice none์ด๋ฉด message JSON๋งŒ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”.';
}
async function parseDecision(output, request, options = {}) {
  // ๋ชจ๋ธ ์›๋ฌธ ๋Œ€์‹  ๊ณ ์ • ๊ฒ€์ฆ ๋‹จ๊ณ„๋งŒ ๊ด€์ธกํ•œ๋‹ค. ํŒŒ์‹ฑ ํ—ˆ์šฉ ๋ฒ”์œ„๋‚˜ ์‹คํ–‰ ์ •์ฑ…์€ ๋ฐ”๊พธ์ง€ ์•Š๋Š”๋‹ค.
  let stage = 'json_parse';
  try {
  demand(typeof output === 'string' && Buffer.byteLength(output) <= 262144);
  let decision;
  try { decision = JSON.parse(output); } catch { throw fail(); }
  stage = 'decision_shape';
  demand(object(decision));
  if (decision.type === 'message') {
    keys(decision, ['type', 'content']);
    demand(['none', 'auto'].includes(request.toolChoice) && typeof decision.content === 'string' && decision.content.trim());
    return { content: decision.content };
  }
  keys(decision, ['type', 'calls']);
  demand(decision.type === 'tool_calls' && request.toolChoice !== 'none');
  stage = 'call_count';
  demand(Array.isArray(decision.calls) && decision.calls.length > 0 && decision.calls.length <= (request.parallelToolCalls ? 8 : 1));
  const tools = new Map(request.tools.map(tool => [tool.function.name, tool.function]));
  const batch = [];
  const tool_calls = decision.calls.map(call => {
    stage = 'decision_shape';
    keys(call, ['name', 'arguments']);
    stage = 'tool_name';
    demand(tools.has(call.name));
    if (object(request.toolChoice)) demand(call.name === request.toolChoice.name);
    stage = 'tool_arguments';
    const args = argumentsObject(call.arguments), schema = tools.get(call.name).parameters;
    checkSchema(schema); batch.push({ schema, data: args });
    return { id: 'call_' + randomUUID().replaceAll('-', ''), type: 'function', function: { name: call.name, arguments: JSON.stringify(args) } };
  });
  stage = 'schema_worker';
  await validateSchemas(batch, options);
  return { content: null, tool_calls };
  } catch (error) {
    error.protocolStage = stage;
    error.protocolJsonFence = typeof output === 'string' && /^\s*```/.test(output);
    try { error.protocolResponse = require('./cli-response-diagnostics').inspectResponse(output); }
    catch { /* ๊ด€์ธก ์‹คํŒจ๊ฐ€ ์›๋ž˜ ํŒŒ์‹ฑยท๊ฒ€์ฆ ์‹คํŒจ๋ฅผ ๋ฎ๊ฑฐ๋‚˜ ์‹คํ–‰์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. */ }
    throw error;
  }
}
module.exports = { parseRequest, buildPrompt, parseDecision };