48 lines
1.2 KiB
JavaScript
48 lines
1.2 KiB
JavaScript
/**
|
|
* Mirrors lib/agent-sse-parse.js + parsing behavior used in agent-openai.js.
|
|
* Keep in sync when editing SSE helpers.
|
|
*/
|
|
import test from 'brittle'
|
|
|
|
function bareAgentParseSseDataPayload(dataLine) {
|
|
const t = String(dataLine).trim()
|
|
if (t === '[DONE]') return { kind: 'done' }
|
|
try {
|
|
const j = JSON.parse(t)
|
|
return { kind: 'json', value: j }
|
|
} catch {
|
|
return { kind: 'raw', value: t }
|
|
}
|
|
}
|
|
|
|
function bareAgentSplitSseLines(buf) {
|
|
const lines = []
|
|
let start = 0
|
|
for (let i = 0; i < buf.length; i++) {
|
|
if (buf.charCodeAt(i) === 10) {
|
|
lines.push(buf.slice(start, i))
|
|
start = i + 1
|
|
}
|
|
}
|
|
return { lines, rest: buf.slice(start) }
|
|
}
|
|
|
|
test('SSE data payload [DONE]', async (t) => {
|
|
t.is(bareAgentParseSseDataPayload('[DONE]').kind, 'done')
|
|
})
|
|
|
|
test('SSE data payload JSON', async (t) => {
|
|
const r = bareAgentParseSseDataPayload('{"x":1}')
|
|
t.is(r.kind, 'json')
|
|
t.is(r.value.x, 1)
|
|
})
|
|
|
|
test('SSE split preserves tail', async (t) => {
|
|
const sp = bareAgentSplitSseLines('a\nb\nc\n')
|
|
t.is(sp.lines.length, 3)
|
|
t.is(sp.rest, '')
|
|
const sp2 = bareAgentSplitSseLines('partial')
|
|
t.is(sp2.lines.length, 0)
|
|
t.is(sp2.rest, 'partial')
|
|
})
|