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 };
|