@baishuyun/chat-backend 0.0.19 → 0.0.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/config/default.ts +8 -0
- package/dist/config/default.js +12 -5
- package/dist/src/app/main.js +9 -0
- package/dist/src/config/hono.config.js +2 -1
- package/dist/src/const/error_code.js +3 -0
- package/dist/src/controllers/agent/bots.controller.js +1 -1
- package/dist/src/controllers/common/connect.controll.js +4 -2
- package/dist/src/controllers/common/model.js +2 -0
- package/dist/src/controllers/common/transformer-factory/agent-calling-listener.js +44 -0
- package/dist/src/controllers/form/attachment-upload.controller.js +15 -15
- package/dist/src/controllers/form/build/build.controller.js +3 -2
- package/dist/src/controllers/form/conversation/clear.controller.js +6 -6
- package/dist/src/controllers/form/fill/batch-fill.controller.js +3 -4
- package/dist/src/controllers/form/fill/createBatchFillingTransformStream.js +1 -17
- package/dist/src/controllers/form/fill/fill.controller.js +2 -1
- package/dist/src/controllers/form/fill/utils.js +16 -0
- package/dist/src/controllers/report/query/query.controller.js +1 -1
- package/dist/src/middleware/botRouter.js +34 -0
- package/dist/src/routes/common/common.route.js +2 -1
- package/dist/src/services/asr/asr-websocket.js +183 -0
- package/dist/src/services/asr/volc-protocol.js +143 -0
- package/dist/src/services/fetchCozeInfo.js +1 -1
- package/dist/src/utils/createFakeUIMessageStreamResponse.js +25 -0
- package/dist/src/utils/createJsonStreamTransformer.js +17 -2
- package/dist/src/utils/createSpecialPartMeta.js +5 -0
- package/package.json +7 -5
- package/src/app/main.ts +12 -0
- package/src/config/hono.config.ts +2 -1
- package/src/const/error_code.ts +3 -0
- package/src/controllers/common/connect.controll.ts +5 -2
- package/src/controllers/common/model.ts +2 -0
- package/src/controllers/common/transformer-factory/agent-calling-listener.ts +69 -0
- package/src/controllers/form/build/build.controller.ts +5 -2
- package/src/controllers/form/fill/batch-fill.controller.ts +3 -5
- package/src/controllers/form/fill/createBatchFillingTransformStream.ts +1 -20
- package/src/controllers/form/fill/fill.controller.ts +2 -0
- package/src/controllers/form/fill/utils.ts +18 -0
- package/src/middleware/botRouter.ts +41 -0
- package/src/routes/common/common.route.ts +2 -1
- package/src/services/asr/asr-websocket.ts +231 -0
- package/src/services/asr/volc-protocol.ts +220 -0
- package/src/utils/createFakeUIMessageStreamResponse.ts +27 -0
- package/src/utils/createJsonStreamTransformer.ts +21 -2
- package/src/utils/createSpecialPartMeta.ts +7 -0
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import WebSocket from 'ws';
|
|
2
|
+
import type { IncomingMessage } from 'node:http';
|
|
3
|
+
import config from 'config';
|
|
4
|
+
import crypto from 'crypto';
|
|
5
|
+
import {
|
|
6
|
+
createFullClientRequest,
|
|
7
|
+
createAudioOnlyRequest,
|
|
8
|
+
parseHeader,
|
|
9
|
+
readPayloadSize,
|
|
10
|
+
parseJsonPayload,
|
|
11
|
+
parseErrorPayload,
|
|
12
|
+
MSG_TYPE_FULL_SERVER_RESPONSE,
|
|
13
|
+
MSG_TYPE_ERROR_RESPONSE,
|
|
14
|
+
} from './volc-protocol.js';
|
|
15
|
+
import { logger } from '../../logger/index.js';
|
|
16
|
+
import type { ASRConfig, ASRResponse } from './volc-protocol.js';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 处理浏览器 ASR WebSocket 连接
|
|
20
|
+
* 作为浏览器与火山引擎 ASR 之间的双向代理
|
|
21
|
+
*/
|
|
22
|
+
export function handleASRConnection(clientWs: WebSocket, req: IncomingMessage): void {
|
|
23
|
+
const asrConfig = config.get<ASRConfig>('agent.asr');
|
|
24
|
+
|
|
25
|
+
if (!asrConfig?.appid || !asrConfig?.token || !asrConfig?.cluster) {
|
|
26
|
+
logger.error('ASR configuration missing: appid, token, cluster are required');
|
|
27
|
+
clientWs.close(1011, 'ASR config missing');
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
logger.info('ASR client connected');
|
|
32
|
+
|
|
33
|
+
// 音频数据缓冲区(在火山连接建立前暂存)
|
|
34
|
+
const audioBuffer: Buffer[] = [];
|
|
35
|
+
let isVolcReady = false;
|
|
36
|
+
let isClosing = false;
|
|
37
|
+
|
|
38
|
+
// v3 接口地址:双向流式优化版
|
|
39
|
+
const volcUrl = `wss://${asrConfig.host}/api/v3/sauc/bigmodel_async`;
|
|
40
|
+
|
|
41
|
+
// 鉴权 Header(v3 大模型语音识别通过 HTTP Header 鉴权)
|
|
42
|
+
const connectId = crypto.randomUUID();
|
|
43
|
+
const requestHeaders: Record<string, string> = {
|
|
44
|
+
'X-Api-App-Key': asrConfig.appid,
|
|
45
|
+
'X-Api-Access-Key': asrConfig.token,
|
|
46
|
+
'X-Api-Resource-Id': asrConfig.cluster,
|
|
47
|
+
'X-Api-Connect-Id': connectId,
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
logger.info(
|
|
51
|
+
{ url: volcUrl, headers: { ...requestHeaders, 'X-Api-Access-Key': '***' } },
|
|
52
|
+
'Connecting to Volc ASR v3'
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
const volcWs = new WebSocket(volcUrl, {
|
|
56
|
+
headers: requestHeaders,
|
|
57
|
+
perMessageDeflate: false,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
volcWs.on('unexpected-response', (req, res) => {
|
|
61
|
+
let body = '';
|
|
62
|
+
res.on('data', (chunk) => { body += chunk; });
|
|
63
|
+
res.on('end', () => {
|
|
64
|
+
logger.error(
|
|
65
|
+
{ statusCode: res.statusCode, statusMessage: res.statusMessage, body },
|
|
66
|
+
'Volc ASR unexpected response'
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// 火山引擎连接建立后立即发送初始化请求并开始转发音频
|
|
72
|
+
volcWs.on('open', () => {
|
|
73
|
+
logger.info('Connected to Volc ASR v3, sending init request');
|
|
74
|
+
volcWs.send(createFullClientRequest());
|
|
75
|
+
|
|
76
|
+
// 火山引擎不返回初始化确认,直接开始等待音频
|
|
77
|
+
// 立即开始转发缓冲的音频数据
|
|
78
|
+
isVolcReady = true;
|
|
79
|
+
while (audioBuffer.length > 0) {
|
|
80
|
+
const buffered = audioBuffer.shift()!;
|
|
81
|
+
if (volcWs.readyState === WebSocket.OPEN) {
|
|
82
|
+
volcWs.send(createAudioOnlyRequest(buffered));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// 处理火山引擎返回的消息(二进制帧)
|
|
88
|
+
volcWs.on('message', (data: Buffer) => {
|
|
89
|
+
if (isClosing) return;
|
|
90
|
+
|
|
91
|
+
if (!Buffer.isBuffer(data) || data.length < 4) {
|
|
92
|
+
logger.warn({ len: data.length, type: typeof data }, 'Invalid ASR message');
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const { msgType, flags, bodyOffset } = parseHeader(data);
|
|
97
|
+
|
|
98
|
+
if (msgType === MSG_TYPE_FULL_SERVER_RESPONSE) {
|
|
99
|
+
// 根据 flags 判断是否有 sequence(flags bit0=1 表示有正 sequence)
|
|
100
|
+
const hasSequence = (flags & 0b0001) !== 0;
|
|
101
|
+
const payloadSizeOffset = hasSequence ? bodyOffset + 4 : bodyOffset;
|
|
102
|
+
const payloadOffset = payloadSizeOffset + 4;
|
|
103
|
+
|
|
104
|
+
if (data.length < payloadOffset) {
|
|
105
|
+
logger.warn({ len: data.length, payloadOffset, hex: data.toString('hex') }, 'ASR response too short');
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const payloadSize = readPayloadSize(data, payloadSizeOffset);
|
|
110
|
+
|
|
111
|
+
if (data.length < payloadOffset + payloadSize) {
|
|
112
|
+
logger.warn({ len: data.length, payloadSize, payloadOffset }, 'ASR response incomplete');
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const payload = data.slice(payloadOffset, payloadOffset + payloadSize);
|
|
117
|
+
const response = parseJsonPayload(payload);
|
|
118
|
+
|
|
119
|
+
if (!response) {
|
|
120
|
+
logger.warn({ payloadStr: payload.toString('utf-8').slice(0, 200) }, 'Failed to parse ASR JSON');
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const text = response.result?.text;
|
|
125
|
+
const utterances = response.result?.utterances;
|
|
126
|
+
const isDefinite = utterances?.some((u) => u.definite);
|
|
127
|
+
|
|
128
|
+
logger.info({ text, definite: isDefinite }, 'ASR result');
|
|
129
|
+
|
|
130
|
+
// 转发识别文本给浏览器
|
|
131
|
+
if (text !== undefined && clientWs.readyState === WebSocket.OPEN) {
|
|
132
|
+
clientWs.send(
|
|
133
|
+
JSON.stringify({
|
|
134
|
+
type: isDefinite ? 'final' : 'result',
|
|
135
|
+
text,
|
|
136
|
+
})
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (isDefinite && clientWs.readyState === WebSocket.OPEN) {
|
|
141
|
+
clientWs.send(JSON.stringify({ type: 'done' }));
|
|
142
|
+
}
|
|
143
|
+
} else if (msgType === MSG_TYPE_ERROR_RESPONSE) {
|
|
144
|
+
const error = parseErrorPayload(data, bodyOffset);
|
|
145
|
+
logger.error({ error }, 'ASR error response');
|
|
146
|
+
if (clientWs.readyState === WebSocket.OPEN) {
|
|
147
|
+
clientWs.send(
|
|
148
|
+
JSON.stringify({
|
|
149
|
+
type: 'error',
|
|
150
|
+
message: error?.message || `ASR error code: ${error?.code}`,
|
|
151
|
+
})
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// 处理浏览器发来的消息
|
|
158
|
+
clientWs.on('message', (data: Buffer | ArrayBuffer | string) => {
|
|
159
|
+
if (isClosing) return;
|
|
160
|
+
|
|
161
|
+
// 结束信号
|
|
162
|
+
if (typeof data === 'string' && data === 'end') {
|
|
163
|
+
if (volcWs.readyState === WebSocket.OPEN) {
|
|
164
|
+
volcWs.send(createAudioOnlyRequest(Buffer.alloc(0), true));
|
|
165
|
+
}
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// 将数据转为 Buffer
|
|
170
|
+
let audioData: Buffer;
|
|
171
|
+
if (Buffer.isBuffer(data)) {
|
|
172
|
+
audioData = data;
|
|
173
|
+
} else if (data instanceof ArrayBuffer) {
|
|
174
|
+
audioData = Buffer.from(data);
|
|
175
|
+
} else {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (audioData.length === 0) return;
|
|
180
|
+
|
|
181
|
+
// 火山连接就绪则直接转发,否则缓冲
|
|
182
|
+
if (isVolcReady && volcWs.readyState === WebSocket.OPEN) {
|
|
183
|
+
volcWs.send(createAudioOnlyRequest(audioData));
|
|
184
|
+
} else {
|
|
185
|
+
audioBuffer.push(audioData);
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
// 浏览器关闭
|
|
190
|
+
clientWs.on('close', () => {
|
|
191
|
+
logger.info('ASR client disconnected');
|
|
192
|
+
isClosing = true;
|
|
193
|
+
if (volcWs.readyState === WebSocket.OPEN) {
|
|
194
|
+
volcWs.send(createAudioOnlyRequest(Buffer.alloc(0), true));
|
|
195
|
+
setTimeout(() => volcWs.close(), 500);
|
|
196
|
+
} else {
|
|
197
|
+
volcWs.close();
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// 火山引擎关闭
|
|
202
|
+
volcWs.on('close', () => {
|
|
203
|
+
logger.info('Volc ASR connection closed');
|
|
204
|
+
isClosing = true;
|
|
205
|
+
if (clientWs.readyState === WebSocket.OPEN) {
|
|
206
|
+
clientWs.close();
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
// 浏览器端错误
|
|
211
|
+
clientWs.on('error', (err) => {
|
|
212
|
+
logger.error({ err }, 'Client WebSocket error');
|
|
213
|
+
isClosing = true;
|
|
214
|
+
volcWs.close();
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
// 火山引擎错误
|
|
218
|
+
volcWs.on('error', (err) => {
|
|
219
|
+
logger.error({ err }, 'Volc ASR WebSocket error');
|
|
220
|
+
isClosing = true;
|
|
221
|
+
if (clientWs.readyState === WebSocket.OPEN) {
|
|
222
|
+
clientWs.send(
|
|
223
|
+
JSON.stringify({
|
|
224
|
+
type: 'error',
|
|
225
|
+
message: 'ASR service connection error',
|
|
226
|
+
})
|
|
227
|
+
);
|
|
228
|
+
clientWs.close(1011, 'ASR error');
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 火山引擎 ASR v3 大模型语音识别 WebSocket 二进制协议
|
|
3
|
+
* 协议文档: https://www.volcengine.com/docs/6561/1354869
|
|
4
|
+
*
|
|
5
|
+
* v3 协议要点:
|
|
6
|
+
* - 4 字节 header
|
|
7
|
+
* - payloadSize 为 4 字节 uint32 大端
|
|
8
|
+
* - FullServerResponse 包含 4 字节 sequence 字段
|
|
9
|
+
* - ErrorResponse 包含 errorCode(4B) + errorMsgSize(4B) + errorMsg
|
|
10
|
+
* - 鉴权在 HTTP Header 中,不在 payload
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const PROTOCOL_VERSION = 0x01;
|
|
14
|
+
const HEADER_SIZE_VALUE = 0x01; // actual header size = 1 * 4 = 4 bytes
|
|
15
|
+
|
|
16
|
+
/** 消息类型 */
|
|
17
|
+
export const MSG_TYPE_FULL_CLIENT_REQUEST = 0x01;
|
|
18
|
+
export const MSG_TYPE_AUDIO_ONLY_REQUEST = 0x02;
|
|
19
|
+
export const MSG_TYPE_FULL_SERVER_RESPONSE = 0x09;
|
|
20
|
+
export const MSG_TYPE_ERROR_RESPONSE = 0x0f;
|
|
21
|
+
|
|
22
|
+
/** 序列化方式 */
|
|
23
|
+
const SERIALIZATION_JSON = 0x01;
|
|
24
|
+
const SERIALIZATION_NONE = 0x00;
|
|
25
|
+
|
|
26
|
+
/** 压缩方式 */
|
|
27
|
+
const COMPRESSION_NONE = 0x00;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 构建 4 字节协议头
|
|
31
|
+
*
|
|
32
|
+
* byte0: version(4bit) | header_size(4bit)
|
|
33
|
+
* byte1: msg_type(4bit) | flags(4bit)
|
|
34
|
+
* byte2: serialization(4bit) | compression(4bit)
|
|
35
|
+
* byte3: reserved
|
|
36
|
+
*/
|
|
37
|
+
function build4ByteHeader(
|
|
38
|
+
msgType: number,
|
|
39
|
+
flags: number,
|
|
40
|
+
serialization: number
|
|
41
|
+
): Buffer {
|
|
42
|
+
const header = Buffer.alloc(4);
|
|
43
|
+
header[0] = (PROTOCOL_VERSION << 4) | HEADER_SIZE_VALUE;
|
|
44
|
+
header[1] = (msgType << 4) | (flags & 0x0f);
|
|
45
|
+
header[2] = (serialization << 4) | COMPRESSION_NONE;
|
|
46
|
+
header[3] = 0x00;
|
|
47
|
+
return header;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 构建完整消息:header + payloadSize(4B) + payload */
|
|
51
|
+
function buildMessage(
|
|
52
|
+
msgType: number,
|
|
53
|
+
flags: number,
|
|
54
|
+
serialization: number,
|
|
55
|
+
payload: Buffer
|
|
56
|
+
): Buffer {
|
|
57
|
+
const header = build4ByteHeader(msgType, flags, serialization);
|
|
58
|
+
const payloadSize = Buffer.alloc(4);
|
|
59
|
+
payloadSize.writeUInt32BE(payload.length, 0);
|
|
60
|
+
return Buffer.concat([header, payloadSize, payload]);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** 解析后的协议头信息 */
|
|
64
|
+
export interface ParsedMessage {
|
|
65
|
+
msgType: number;
|
|
66
|
+
flags: number;
|
|
67
|
+
/** header 之后的偏移量(header 4B + 扩展字段) */
|
|
68
|
+
bodyOffset: number;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 解析 4 字节协议头
|
|
73
|
+
*/
|
|
74
|
+
export function parseHeader(buffer: Buffer): ParsedMessage {
|
|
75
|
+
const version = buffer[0] >> 4;
|
|
76
|
+
const headerSizeValue = buffer[0] & 0x0f;
|
|
77
|
+
const headerSize = headerSizeValue * 4;
|
|
78
|
+
const msgType = buffer[1] >> 4;
|
|
79
|
+
const flags = buffer[1] & 0x0f;
|
|
80
|
+
const serialization = buffer[2] >> 4;
|
|
81
|
+
const compression = buffer[2] & 0x0f;
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
msgType,
|
|
85
|
+
flags,
|
|
86
|
+
bodyOffset: headerSize,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 读取 payload size(4 字节 uint32 大端)
|
|
92
|
+
*/
|
|
93
|
+
export function readPayloadSize(buffer: Buffer, offset: number): number {
|
|
94
|
+
return buffer.readUInt32BE(offset);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** ASR 配置(鉴权信息在 HTTP Header 中,不在 payload) */
|
|
98
|
+
export interface ASRConfig {
|
|
99
|
+
appid: string;
|
|
100
|
+
token: string;
|
|
101
|
+
secretKey: string;
|
|
102
|
+
cluster: string;
|
|
103
|
+
host: string;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 创建 FullClientRequest(初始化请求)
|
|
108
|
+
*
|
|
109
|
+
* Payload JSON 格式:
|
|
110
|
+
* {
|
|
111
|
+
* user: { uid: string },
|
|
112
|
+
* audio: { format, rate, bits, channel, codec, language },
|
|
113
|
+
* request: { model_name, enable_itn, enable_punc, ... }
|
|
114
|
+
* }
|
|
115
|
+
*/
|
|
116
|
+
export function createFullClientRequest(): Buffer {
|
|
117
|
+
const payload = Buffer.from(
|
|
118
|
+
JSON.stringify({
|
|
119
|
+
user: {
|
|
120
|
+
uid: '0',
|
|
121
|
+
},
|
|
122
|
+
audio: {
|
|
123
|
+
format: 'pcm',
|
|
124
|
+
rate: 16000,
|
|
125
|
+
bits: 16,
|
|
126
|
+
channel: 1,
|
|
127
|
+
codec: 'raw',
|
|
128
|
+
language: 'zh-CN',
|
|
129
|
+
},
|
|
130
|
+
request: {
|
|
131
|
+
model_name: 'bigmodel',
|
|
132
|
+
enable_itn: true,
|
|
133
|
+
enable_punc: true,
|
|
134
|
+
enable_ddc: false,
|
|
135
|
+
},
|
|
136
|
+
}),
|
|
137
|
+
'utf-8'
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
return buildMessage(
|
|
141
|
+
MSG_TYPE_FULL_CLIENT_REQUEST,
|
|
142
|
+
0b0000,
|
|
143
|
+
SERIALIZATION_JSON,
|
|
144
|
+
payload
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* 创建 AudioOnlyRequest(音频数据请求)
|
|
150
|
+
*
|
|
151
|
+
* flags:
|
|
152
|
+
* - 0b0000: 普通音频包
|
|
153
|
+
* - 0b0010: 最后一包音频(负包)
|
|
154
|
+
*/
|
|
155
|
+
export function createAudioOnlyRequest(
|
|
156
|
+
audioData: Buffer,
|
|
157
|
+
isLast: boolean = false
|
|
158
|
+
): Buffer {
|
|
159
|
+
const flags = isLast ? 0b0010 : 0b0000;
|
|
160
|
+
return buildMessage(
|
|
161
|
+
MSG_TYPE_AUDIO_ONLY_REQUEST,
|
|
162
|
+
flags,
|
|
163
|
+
SERIALIZATION_NONE,
|
|
164
|
+
audioData
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** 火山引擎响应结果 */
|
|
169
|
+
export interface ASRResponse {
|
|
170
|
+
result?: {
|
|
171
|
+
text?: string;
|
|
172
|
+
utterances?: Array<{
|
|
173
|
+
text: string;
|
|
174
|
+
definite: boolean;
|
|
175
|
+
start_time: number;
|
|
176
|
+
end_time: number;
|
|
177
|
+
}>;
|
|
178
|
+
[key: string]: unknown;
|
|
179
|
+
};
|
|
180
|
+
audio_info?: {
|
|
181
|
+
duration: number;
|
|
182
|
+
};
|
|
183
|
+
[key: string]: unknown;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* 解析 JSON payload
|
|
188
|
+
*/
|
|
189
|
+
export function parseJsonPayload(payloadBuffer: Buffer): ASRResponse | null {
|
|
190
|
+
try {
|
|
191
|
+
return JSON.parse(payloadBuffer.toString('utf-8'));
|
|
192
|
+
} catch {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* 解析 ErrorResponse
|
|
199
|
+
*
|
|
200
|
+
* 格式:header(4B) + errorCode(4B) + errorMsgSize(4B) + errorMsg
|
|
201
|
+
*/
|
|
202
|
+
export function parseErrorPayload(buffer: Buffer, bodyOffset: number): { code: number; message: string } | null {
|
|
203
|
+
const errorCodeOffset = bodyOffset;
|
|
204
|
+
const errorMsgSizeOffset = errorCodeOffset + 4;
|
|
205
|
+
const errorMsgOffset = errorMsgSizeOffset + 4;
|
|
206
|
+
|
|
207
|
+
if (buffer.length < errorMsgOffset) {
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const code = buffer.readUInt32BE(errorCodeOffset);
|
|
212
|
+
const msgSize = buffer.readUInt32BE(errorMsgSizeOffset);
|
|
213
|
+
|
|
214
|
+
if (buffer.length < errorMsgOffset + msgSize) {
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const message = buffer.slice(errorMsgOffset, errorMsgOffset + msgSize).toString('utf-8');
|
|
219
|
+
return { code, message };
|
|
220
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { createUIMessageStream, createUIMessageStreamResponse, generateId } from 'ai';
|
|
2
|
+
import { createSpecialPartMeta } from './createSpecialPartMeta.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 创建一个与 `streamText().toUIMessageStreamResponse()` 格式一致的假流式响应,
|
|
6
|
+
* 用于在无法调用真实模型时向客户端推送一条指定的文本消息。
|
|
7
|
+
*/
|
|
8
|
+
export const createFakeUIMessageStreamResponse = (text: string): Response => {
|
|
9
|
+
const stream = createUIMessageStream({
|
|
10
|
+
execute: ({ writer }) => {
|
|
11
|
+
const messageId = generateId();
|
|
12
|
+
const textId = generateId();
|
|
13
|
+
writer.write({ type: 'start', messageId });
|
|
14
|
+
writer.write({ type: 'text-start', id: textId });
|
|
15
|
+
writer.write({
|
|
16
|
+
type: 'text-delta',
|
|
17
|
+
id: textId,
|
|
18
|
+
delta: text,
|
|
19
|
+
providerMetadata: createSpecialPartMeta('error-bot-id-required'),
|
|
20
|
+
});
|
|
21
|
+
writer.write({ type: 'text-end', id: textId });
|
|
22
|
+
writer.write({ type: 'finish', finishReason: 'stop' });
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return createUIMessageStreamResponse({ stream });
|
|
27
|
+
};
|
|
@@ -31,6 +31,8 @@ export interface JsonStreamTransformerOptions<T> {
|
|
|
31
31
|
onErrorChunk?: (chunk: LanguageModelV2StreamPart) => void;
|
|
32
32
|
/** JSON 解析出错时的回调,返回要展示的错误消息(可选) */
|
|
33
33
|
onParseError?: (error: any) => string;
|
|
34
|
+
|
|
35
|
+
bypassParseError?: boolean; // 是否完全绕过解析错误处理,直接透传原始 chunk(不推荐,除非特殊场景)
|
|
34
36
|
}
|
|
35
37
|
|
|
36
38
|
/**
|
|
@@ -75,6 +77,7 @@ class JsonStreamProcessor<T> {
|
|
|
75
77
|
private parseCompleted: Promise<void> | null = null;
|
|
76
78
|
private resolveParseCompleted: (() => void) | null = null;
|
|
77
79
|
private terminated = false;
|
|
80
|
+
private passthroughMode = false; // 当启用且遇到非 JSON chunk 时,切换到透传模式
|
|
78
81
|
|
|
79
82
|
constructor(options: JsonStreamTransformerOptions<T>) {
|
|
80
83
|
this.options = options;
|
|
@@ -96,6 +99,11 @@ class JsonStreamProcessor<T> {
|
|
|
96
99
|
return;
|
|
97
100
|
}
|
|
98
101
|
|
|
102
|
+
if (this.passthroughMode) {
|
|
103
|
+
controller.enqueue(chunk);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
99
107
|
try {
|
|
100
108
|
// 1) error chunk → 通知下游后终止
|
|
101
109
|
if (chunk.type === 'error') {
|
|
@@ -117,6 +125,11 @@ class JsonStreamProcessor<T> {
|
|
|
117
125
|
this.parser.write(stripCodeFence(chunk.delta));
|
|
118
126
|
}
|
|
119
127
|
} catch (error) {
|
|
128
|
+
if (this.options.bypassParseError) {
|
|
129
|
+
this.passthroughMode = true;
|
|
130
|
+
controller.enqueue(chunk);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
120
133
|
this.enqueueError(controller, error);
|
|
121
134
|
}
|
|
122
135
|
}
|
|
@@ -169,6 +182,11 @@ class JsonStreamProcessor<T> {
|
|
|
169
182
|
};
|
|
170
183
|
|
|
171
184
|
this.parser.onError = (err: any) => {
|
|
185
|
+
if (this.options.bypassParseError) {
|
|
186
|
+
this.passthroughMode = true;
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
172
190
|
if (!this.terminated) {
|
|
173
191
|
const msg = this.options.onParseError
|
|
174
192
|
? this.options.onParseError(err)
|
|
@@ -180,8 +198,9 @@ class JsonStreamProcessor<T> {
|
|
|
180
198
|
};
|
|
181
199
|
|
|
182
200
|
this.parser.onEnd = () => {
|
|
183
|
-
|
|
184
|
-
|
|
201
|
+
if (!this.options.bypassParseError) {
|
|
202
|
+
enqueue(' ', JSON.stringify({ appendConfirm: 'save-fields' }));
|
|
203
|
+
}
|
|
185
204
|
this.completeParsing();
|
|
186
205
|
};
|
|
187
206
|
}
|