Skip to content

Commit

Permalink
处理首尾的 null
Browse files Browse the repository at this point in the history
  • Loading branch information
ljnchn authored Dec 21, 2023
1 parent a7d4ef6 commit 193e790
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ addEventListener('fetch', event => {
});

async function fetchAndStream(request) {
console.log(request)
// 从请求中获取json数据
if (new URL(request.url).pathname === '/v1/chat/completions') {
return handleChatCompletionsRequest(request);
Expand Down Expand Up @@ -88,8 +87,8 @@ async function handleChatCompletionsRequest(request) {
let tokenData = await tokenResponse.json();
let access_token = tokenData.token;
let uuidValue = await uuid();
let sessionId = uuidValue + Date.now();
let machineId = await sha256(uuidValue );
let sessionId = uuidValue + Date.now();
let machineId = await sha256(uuidValue);

// 构建新的请求头部
let acc_headers = {
Expand All @@ -115,21 +114,20 @@ async function handleChatCompletionsRequest(request) {
});
// 如果stream为true,返回流式响应
if (stream) {
// todo: hook 内容去掉null,目前字节断流,无法按行处理
// let { readable, writable } = new TransformStream({
// async transform(chunk, controller) {
// // 在这里对数据进行修改,chunk是每次读取到的数据块

// // 例如,将数据块转为字符串,修改内容,然后再转回为Uint8Array
// let text = new TextDecoder().decode(chunk);
// let modifiedText = modifyContent(text);
// let modifiedChunk = new TextEncoder().encode(modifiedText);

// // 将修改后的数据块写入可写流
// controller.enqueue(modifiedChunk);
// },
// });
let { readable, writable } = new TransformStream();
// let { readable, writable } = new TransformStream();
let { readable, writable } = new TransformStream({
async transform(chunk, controller) {
// 在这里对数据进行修改,chunk是每次读取到的数据块

// 例如,将数据块转为字符串,修改内容,然后再转回为Uint8Array
let text = new TextDecoder().decode(chunk);
let modifiedText = text.replace(/"content":null/g, '"content":""');
let modifiedChunk = new TextEncoder().encode(modifiedText);

// 将修改后的数据块写入可写流
controller.enqueue(modifiedChunk);
},
});
// Start pumping the body. NOTE: No await!
copilotResponse.body.pipeTo(writable);

Expand Down

0 comments on commit 193e790

Please sign in to comment.