feat: stream chat and settle metered CNY usage atomically
This commit is contained in:
1 parent
7360cf3ebe
commit
b46ee72f2d
28 files changed
+1098
-86
No files matched your search
@@ -0,0 +1,25 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readChatStream } from '../src/services/chat-stream.ts'
|
||||
|
||||
function stream(text, everyByte = false) {
|
||||
const bytes = new TextEncoder().encode(text)
|
||||
return new ReadableStream({ start(controller) {
|
||||
if (everyByte) for (const byte of bytes) controller.enqueue(Uint8Array.of(byte))
|
||||
else controller.enqueue(bytes)
|
||||
controller.close()
|
||||
} })
|
||||
}
|
||||
test('UTF-8 split at every byte and CRLF frames preserve reasoning and content', async () => {
|
||||
let text = '', reasoning = ''
|
||||
await readChatStream(stream('data: {"choices":[{"delta":{"reasoning_content":"思考"}}]}\r\n\r\ndata: {"choices":[{"delta":{"content":"你好🙂"}}]}\n\ndata: [DONE]\n\n', true), (a,b) => { text += a; reasoning += b })
|
||||
assert.equal(text, '你好🙂'); assert.equal(reasoning, '思考')
|
||||
})
|
||||
test('an SSE error after partial output is surfaced', async () => {
|
||||
let text = ''
|
||||
await assert.rejects(readChatStream(stream('data: {"choices":[{"delta":{"content":"partial"}}]}\n\ndata: {"error":{"message":"用量缺失,未扣费"}}\n\ndata: [DONE]\n\n'), a => text += a), /未扣费/)
|
||||
assert.equal(text, 'partial')
|
||||
})
|
||||
test('EOF alone is never treated as a completed request', async () => {
|
||||
await assert.rejects(readChatStream(stream('data: {"choices":[]}\n\n'), () => {}), /提前结束/)
|
||||
})
|
||||
Reference in new issue
Block a user