File size: 2,881 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
// ์„ค๋ช… ๋ฌธ์„œ: docs/specs/openwork-cli-provider.md (v2.0.0)
// ์‘๋‹ต ์กฐํ•ฉ ์ง„๋‹จ๋งŒ ๋‹ด๋‹นํ•œ๋‹ค. ์›๋ฌธ์€ ์š”์ฒญ ์•ˆ์—์„œ๋งŒ ๋ณด๊ด€ํ•˜๊ณ  ํŒŒ์„œยท๋„๊ตฌ ์Šน์ธ์—๋Š” ๊ด€์—ฌํ•˜์ง€ ์•Š๋Š”๋‹ค.
const MAX_BYTES = 524288;
const MAX_CHUNKS = 65536;
function createStreamDiagnostics() {
  let joined = '', previous, bytes = 0, chunkCount = 0, validJsonChunks = 0, consecutiveDuplicateChunks = 0, limited = false;
  let messageIdChunks = 0, messageBoundaryCount = 0;
  function onDelta(chunk, metadata) {
    if (chunkCount === MAX_CHUNKS) { limited = true; joined = ''; previous = undefined; return; }
    chunkCount++;
    try {
      if (metadata?.messageIdPresent === true) messageIdChunks++;
      if (metadata?.messageBoundary === true) messageBoundaryCount++;
    } catch { limited = true; joined = ''; previous = undefined; }
    // ํ•œ๋„๋ฅผ ๋„˜๊ธด ๋’ค์—๋Š” ์›๋ฌธ์„ ์ถ”๊ฐ€ ๋ณด๊ด€ํ•˜๊ฑฐ๋‚˜ JSON ๊ฒ€์‚ฌ๋ฅผ ๋ฐ˜๋ณตํ•˜์ง€ ์•Š๋Š”๋‹ค.
    if (limited) return;
    if (typeof chunk !== 'string' || Buffer.byteLength(chunk) > MAX_BYTES - bytes) {
      limited = true; joined = ''; previous = undefined; return;
    }
    bytes += Buffer.byteLength(chunk);
    if (previous !== undefined && previous === chunk) consecutiveDuplicateChunks++;
    previous = chunk; joined += chunk;
    try { JSON.parse(chunk); validJsonChunks++; } catch { /* ํ˜•์‹ ํŒ์ •์šฉ ๊ด€์ธก์ด๋ฉฐ ์›๋ž˜ ํŒŒ์„œ๋Š” ๊ทธ๋Œ€๋กœ ์‹คํ–‰ํ•œ๋‹ค. */ }
  }
  function snapshot(finalText) {
    return { observed: chunkCount > 0, chunkCount, validJsonChunks, consecutiveDuplicateChunks, messageIdChunks, messageBoundaryCount, limited,
      returnedTextMatchesChunks: chunkCount > 0 && !limited && typeof finalText === 'string' && finalText === joined };
  }
  function dispose() { joined = ''; previous = undefined; }
  return { onDelta, snapshot, dispose };
}
function formatStreamMetadata(value) {
  try {
    if (!value || typeof value !== 'object') return '';
    // ๊ฐ ํ•„๋“œ๋ฅผ ํ•œ ๋ฒˆ๋งŒ ์ฝ๊ณ  ์ƒˆ ๊ฐ์ฒด๋กœ ๋ณต์‚ฌํ•œ๋‹ค. ์ž„์˜ toJSON/getter๊ฐ€ ๋กœ๊ทธ ์›๋ฌธ์„ ๋ผ์›Œ ๋„ฃ์ง€ ๋ชปํ•œ๋‹ค.
    const { observed, chunkCount, validJsonChunks, consecutiveDuplicateChunks, messageIdChunks, messageBoundaryCount, limited, returnedTextMatchesChunks } = value;
    if (![observed, limited, returnedTextMatchesChunks].every(item => typeof item === 'boolean')
      || ![chunkCount, validJsonChunks, consecutiveDuplicateChunks, messageIdChunks, messageBoundaryCount].every(item => Number.isSafeInteger(item) && item >= 0 && item <= MAX_CHUNKS)) return '';
    return ' stream=' + JSON.stringify({ observed, chunkCount, validJsonChunks, consecutiveDuplicateChunks, messageIdChunks, messageBoundaryCount, limited, returnedTextMatchesChunks });
  } catch { return ''; } // ์ง„๋‹จ ๊ฐ์ฒด ์˜ค๋ฅ˜๋กœ ์›๋ž˜ ์‹คํ–‰ ์˜ค๋ฅ˜๊ฐ€ ๊ฐ€๋ ค์ง€์ง€ ์•Š๊ฒŒ ํ•œ๋‹ค.
}
module.exports = { createStreamDiagnostics, formatStreamMetadata };