chore(telemetry): resolve loop crash caused by Temporal Dead Zone hoisting and fix conversational budget mapping
This commit is contained in:
@@ -830,6 +830,66 @@ export async function POST(request: Request) {
|
|||||||
function emit(chunk: object) {
|
function emit(chunk: object) {
|
||||||
if (streamClosed) return;
|
if (streamClosed) return;
|
||||||
try {
|
try {
|
||||||
|
if (
|
||||||
|
"type" in chunk &&
|
||||||
|
chunk.type !== "ping" &&
|
||||||
|
chunk.type !== "turn_start"
|
||||||
|
) {
|
||||||
|
if (chunk.type === "text" && "text" in chunk) {
|
||||||
|
assistantTimeline.push({ kind: "text", text: chunk.text });
|
||||||
|
} else if (chunk.type === "thinking" && "text" in chunk) {
|
||||||
|
assistantTimeline.push({ kind: "thought", text: chunk.text });
|
||||||
|
} else if (chunk.type === "tool_start" && "name" in chunk) {
|
||||||
|
assistantTimeline.push({
|
||||||
|
kind: "tool",
|
||||||
|
name: chunk.name,
|
||||||
|
status: "running",
|
||||||
|
});
|
||||||
|
} else if (
|
||||||
|
chunk.type === "tool_result" &&
|
||||||
|
"name" in chunk &&
|
||||||
|
"result" in chunk
|
||||||
|
) {
|
||||||
|
const lastRunning = [...assistantTimeline]
|
||||||
|
.reverse()
|
||||||
|
.find(
|
||||||
|
(t) =>
|
||||||
|
t.kind === "tool" &&
|
||||||
|
t.name === chunk.name &&
|
||||||
|
t.status === "running",
|
||||||
|
);
|
||||||
|
if (lastRunning) {
|
||||||
|
lastRunning.status = "done";
|
||||||
|
lastRunning.result = chunk.result;
|
||||||
|
// Quick check if result indicates error
|
||||||
|
try {
|
||||||
|
const p = JSON.parse(chunk.result as string);
|
||||||
|
if (p && p.ok === false) lastRunning.status = "error";
|
||||||
|
} catch {}
|
||||||
|
}
|
||||||
|
} else if (
|
||||||
|
chunk.type === "phase" &&
|
||||||
|
"phase" in chunk &&
|
||||||
|
"label" in chunk
|
||||||
|
) {
|
||||||
|
assistantTimeline.push({
|
||||||
|
kind: "phase",
|
||||||
|
phase: chunk.phase,
|
||||||
|
label: chunk.label,
|
||||||
|
});
|
||||||
|
} else if (
|
||||||
|
chunk.type === "checkpoint" &&
|
||||||
|
"goal" in chunk &&
|
||||||
|
"findings" in chunk
|
||||||
|
) {
|
||||||
|
assistantTimeline.push({
|
||||||
|
kind: "checkpoint",
|
||||||
|
goal: chunk.goal,
|
||||||
|
findings: chunk.findings,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
controller.enqueue(
|
controller.enqueue(
|
||||||
encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`),
|
encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`),
|
||||||
);
|
);
|
||||||
@@ -869,6 +929,7 @@ export async function POST(request: Request) {
|
|||||||
// restores the segmentation on reload.
|
// restores the segmentation on reload.
|
||||||
const assistantTextSegments: string[] = [];
|
const assistantTextSegments: string[] = [];
|
||||||
const assistantToolCalls: ToolCall[] = [];
|
const assistantToolCalls: ToolCall[] = [];
|
||||||
|
const assistantTimeline: any[] = [];
|
||||||
let aborted = clientSignal.aborted;
|
let aborted = clientSignal.aborted;
|
||||||
const onAbort = () => {
|
const onAbort = () => {
|
||||||
aborted = true;
|
aborted = true;
|
||||||
@@ -1339,6 +1400,7 @@ export async function POST(request: Request) {
|
|||||||
|
|
||||||
const finalMsg: ChatMessage & {
|
const finalMsg: ChatMessage & {
|
||||||
textSegments?: string[];
|
textSegments?: string[];
|
||||||
|
timeline?: any[];
|
||||||
_rawToolResults?: Array<{
|
_rawToolResults?: Array<{
|
||||||
name: string;
|
name: string;
|
||||||
args: Record<string, unknown>;
|
args: Record<string, unknown>;
|
||||||
@@ -1356,6 +1418,7 @@ export async function POST(request: Request) {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
: undefined,
|
: undefined,
|
||||||
|
timeline: assistantTimeline.length ? assistantTimeline : undefined,
|
||||||
_rawToolResults: assistantToolCalls.length ? [] : undefined,
|
_rawToolResults: assistantToolCalls.length ? [] : undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user