feat: stream reasoning_content and segment agent messages
build-and-push / build (push) Successful in 6m36s
build-and-push / build (push) Successful in 6m36s
Forward ACP agent_thought_chunk as OpenAI-style reasoning_content, and insert --- separators between distinct agent_message runs in one completion.
This commit is contained in:
+25
-2
@@ -38,7 +38,13 @@ import {
|
||||
toolNudgeBlock,
|
||||
noToolsDirectiveBlock,
|
||||
} from "./message-converter.mjs";
|
||||
import { textChunk, toolCallChunk, finishChunk, nonStreamResponse } from "./stream-translator.mjs";
|
||||
import {
|
||||
textChunk,
|
||||
reasoningChunk,
|
||||
toolCallChunk,
|
||||
finishChunk,
|
||||
nonStreamResponse,
|
||||
} from "./stream-translator.mjs";
|
||||
|
||||
const PORT = parseInt(process.env.PORT || "4646", 10);
|
||||
const EDGE_PORT = parseInt(process.env.EDGE_PORT || "4647", 10);
|
||||
@@ -348,6 +354,7 @@ async function streamSessionTurn({
|
||||
id: requestId,
|
||||
model: session.detectedModel || "auto",
|
||||
text: result.text,
|
||||
reasoning: result.reasoning,
|
||||
toolCalls: result.calls,
|
||||
finishReason: result.finishReason,
|
||||
usage: result.usage,
|
||||
@@ -420,6 +427,18 @@ async function streamSessionTurn({
|
||||
isFirst = false;
|
||||
res.write(`data: ${JSON.stringify(chunk)}\n\n`);
|
||||
},
|
||||
onReasoningDelta: (delta) => {
|
||||
if (res.writableEnded || finished) return;
|
||||
markFirstToken("reasoning");
|
||||
const chunk = reasoningChunk({
|
||||
id: requestId,
|
||||
model: session.detectedModel || "auto",
|
||||
delta,
|
||||
isFirst,
|
||||
});
|
||||
isFirst = false;
|
||||
res.write(`data: ${JSON.stringify(chunk)}\n\n`);
|
||||
},
|
||||
onToolCall: (call) => {
|
||||
if (res.writableEnded || finished) return;
|
||||
sawToolCall = true;
|
||||
@@ -479,6 +498,7 @@ async function streamSessionTurn({
|
||||
function collectSessionTurn(session, blocks) {
|
||||
return new Promise((resolve) => {
|
||||
let text = "";
|
||||
let reasoning = "";
|
||||
const calls = [];
|
||||
let finishReason = "stop";
|
||||
let usage = null;
|
||||
@@ -486,13 +506,16 @@ function collectSessionTurn(session, blocks) {
|
||||
onAssistantDelta: (d) => {
|
||||
text += d;
|
||||
},
|
||||
onReasoningDelta: (d) => {
|
||||
reasoning += d;
|
||||
},
|
||||
onToolCall: (c) => {
|
||||
calls.push(c);
|
||||
},
|
||||
onFinish: (reason, u) => {
|
||||
finishReason = reason;
|
||||
usage = u;
|
||||
resolve({ text, calls, finishReason, usage });
|
||||
resolve({ text, reasoning, calls, finishReason, usage });
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user