File size: 4,885 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
// μ„€λͺ… λ¬Έμ„œ: docs/specs/openwork-cli-provider.md (v2.0.0)
// 응닡을 λ³΅κ΅¬ν•˜κ±°λ‚˜ μ‹€ν–‰ν•˜μ§€ μ•ŠλŠ”λ‹€. 원문 μ—†λŠ” μœ ν•œ λ©”νƒ€λ‘œ ν˜•μ‹ μ‹€νŒ¨μ˜ ν›„λ³΄λ§Œ κ΅¬λΆ„ν•œλ‹€.
const MAX_BYTES = 262144, MAX_OBJECTS = 8;
const starts = ['empty', 'object', 'array', 'fence', 'other'];
const kinds = ['message', 'tool_calls', 'other'];
function inspectResponse(output) {
  if (typeof output !== 'string') return { kind: 'non_string' };
  // 문자 μˆ˜λΆ€ν„° ν™•μΈν•˜μ—¬ κ³ΌλŒ€ μž…λ ₯을 λ°”μ΄νŠΈ κ³„μ‚°Β·νŒŒμ‹±Β·λ¬Έλ²• μŠ€μΊ”ν•˜μ§€ μ•ŠλŠ”λ‹€.
  if (output.length > MAX_BYTES) return { kind: 'oversize' };
  const bytes = Buffer.byteLength(output);
  if (bytes > MAX_BYTES) return { kind: 'oversize' };
  const value = output.trim();
  const startClass = !value ? 'empty' : value.startsWith('```') ? 'fence' : value[0] === '{' ? 'object' : value[0] === '[' ? 'array' : 'other';
  let validJson = false;
  try { JSON.parse(value); validJson = true; } catch { /* 였λ₯˜ λ©”μ‹œμ§€μ—λŠ” 응닡 일뢀가 포함될 수 μžˆμ–΄ λ³΄κ΄€ν•˜μ§€ μ•ŠλŠ”λ‹€. */ }
  let quoted = false, escaped = false, begin = -1, first = -1, last = -1, objectCount = 0;
  let firstObject = '', objectsIdentical = null;
  const decisionKinds = [], objectBytes = [];
  const stack = [];
  for (let index = 0; index < value.length; index++) {
    const char = value[index];
    if (quoted) {
      if (escaped) escaped = false;
      else if (char === '\\') escaped = true;
      else if (char === '"') quoted = false;
      continue;
    }
    if (char === '"') { quoted = true; continue; }
    if (char === '{' || char === '[') {
      if (!stack.length) begin = index;
      stack.push(char);
    } else if (char === '}' || char === ']') {
      if (!stack.length || stack.at(-1) !== (char === '}' ? '{' : '[')) continue;
      stack.pop();
      if (!stack.length && value[begin] === '{') {
        // μ„œλ‘œ κ²ΉμΉ˜μ§€ μ•ŠλŠ” μ΅œμƒμœ„ κ΅¬κ°„λ§Œ κ²€μ‚¬ν•˜λ―€λ‘œ 전체 μ²˜λ¦¬λŸ‰μ€ μž…λ ₯ 길이에 λΉ„λ‘€ν•œλ‹€.
        // λΆ€λΆ„ JSON 값은 버리고 μ‹€ν–‰ κ²½λ‘œμ—λŠ” μ ˆλŒ€ μ „λ‹¬ν•˜μ§€ μ•ŠλŠ”λ‹€.
        try {
          const source = value.slice(begin, index + 1), parsed = JSON.parse(source);
          // λΆ„λ₯˜λŠ” 곡개 계약 enum만 남긴닀. 동일성은 μ •κ·œν™” 없이 객체 ꡬ간 λ¬Έμžμ—΄ 전체λ₯Ό λΉ„κ΅ν•œλ‹€.
          if (!objectCount) firstObject = source;
          else objectsIdentical = (objectsIdentical ?? true) && firstObject === source;
          if (objectCount < MAX_OBJECTS) {
            decisionKinds.push(parsed.type === 'message' ? 'message' : parsed.type === 'tool_calls' ? 'tool_calls' : 'other');
            objectBytes.push(Buffer.byteLength(source));
          }
          objectCount = Math.min(MAX_OBJECTS, objectCount + 1);
          if (first < 0) first = begin;
          last = index + 1;
        } catch { /* κ· ν˜•λ§Œ 맞고 JSON 문법이 ν‹€λ¦° ꡬ간은 JSON 객체둜 μ§‘κ³„ν•˜μ§€ μ•ŠλŠ”λ‹€. */ }
      }
    }
  }
  return { kind: 'bounded', bytes, startClass, validJson, objectCount, decisionKinds, objectBytes, objectsIdentical,
    prefixText: first >= 0 && value.slice(0, first).trim().length > 0,
    suffixText: last >= 0 && value.slice(last).trim().length > 0,
    incomplete: stack.length > 0 || quoted };
}
function formatResponseMetadata(value) {
  // 였λ₯˜ 객체에 μž„μ˜ 속성이 μΆ”κ°€λ˜μ–΄λ„ ν—ˆμš©ν•œ μ›μ‹œ νƒ€μž…Β·λ²”μœ„λ§Œ μƒˆ 객체둜 λ³΅μ‚¬ν•œλ‹€.
  if (!value || typeof value !== 'object') return '';
  const { kind, bytes, startClass, objectCount, validJson, prefixText, suffixText, incomplete, decisionKinds, objectBytes, objectsIdentical } = value;
  if (kind === 'non_string' || kind === 'oversize') return ' response=' + JSON.stringify({ kind });
  if (kind !== 'bounded' || !Number.isInteger(bytes) || bytes < 0 || bytes > MAX_BYTES
    || !starts.includes(startClass) || !Number.isInteger(objectCount) || objectCount < 0 || objectCount > MAX_OBJECTS
    || ![validJson, prefixText, suffixText, incomplete].every(flag => typeof flag === 'boolean')
    || (objectCount < 2 ? objectsIdentical !== null : typeof objectsIdentical !== 'boolean')
    || !Array.isArray(decisionKinds) || !Array.isArray(objectBytes)
    || decisionKinds.length !== objectCount || objectBytes.length !== objectCount) return '';
  const safeKinds = [], safeBytes = [];
  for (let index = 0; index < objectCount; index++) {
    const decisionKind = decisionKinds[index], size = objectBytes[index];
    if (!kinds.includes(decisionKind) || !Number.isInteger(size) || size < 2 || size > MAX_BYTES) return '';
    safeKinds.push(decisionKind); safeBytes.push(size);
  }
  return ' response=' + JSON.stringify({ kind, bytes, startClass, validJson, objectCount, prefixText, suffixText, incomplete,
    decisionKinds: safeKinds, objectBytes: safeBytes, objectsIdentical });
}
module.exports = { inspectResponse, formatResponseMetadata };