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