fix(chat): keep long turns alive, record and log why a turn failed (#1813)
This commit is contained in:
3 files changed
+52
-1
No files matched your search
@@ -518,6 +518,10 @@ func (r *TurnRunner) generateAndComplete(
|
|||||||
MaxOutputTokens: prep.modelDetails.MaxOutputTokens,
|
MaxOutputTokens: prep.modelDetails.MaxOutputTokens,
|
||||||
}, recorder)
|
}, recorder)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
turn.ToolCalls = recorder.events
|
||||||
|
if n := len(recorder.events); n > 0 {
|
||||||
|
err = fmt.Errorf("%w (last completed step: %s)", err, recorder.events[n-1].Name)
|
||||||
|
}
|
||||||
return "", r.failTurn(ctx, turn, err)
|
return "", r.failTurn(ctx, turn, err)
|
||||||
}
|
}
|
||||||
assistant := messaging.Message{
|
assistant := messaging.Message{
|
||||||
@@ -578,6 +582,15 @@ func (r *TurnRunner) failTurn(ctx context.Context, turn TurnRecord, cause error)
|
|||||||
}
|
}
|
||||||
turn.Error = cause.Error()
|
turn.Error = cause.Error()
|
||||||
turn.UpdatedAt = time.Now().UTC()
|
turn.UpdatedAt = time.Now().UTC()
|
||||||
|
if r.Log != nil {
|
||||||
|
level := slog.LevelError
|
||||||
|
if turn.Status == TurnStatusCancelled {
|
||||||
|
level = slog.LevelWarn
|
||||||
|
}
|
||||||
|
r.Log.Log(ctx, level, "chat turn did not complete",
|
||||||
|
"turn", turn.TurnID, "session", turn.SessionID, "status", string(turn.Status),
|
||||||
|
"tool_calls", len(turn.ToolCalls), "error", cause)
|
||||||
|
}
|
||||||
if err := r.saveTurn(ctx, turn); err != nil {
|
if err := r.saveTurn(ctx, turn); err != nil {
|
||||||
return errors.Join(cause, fmt.Errorf("save failed chat turn: %w", err))
|
return errors.Join(cause, fmt.Errorf("save failed chat turn: %w", err))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -417,6 +417,8 @@ func (a *chatAPI) chatShowToolCalls(ctx context.Context) bool {
|
|||||||
return view.Chat.ShowToolCalls
|
return view.Chat.ShowToolCalls
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const chatKeepaliveInterval = 15 * time.Second
|
||||||
|
|
||||||
func (a *chatAPI) handleChatStream(w http.ResponseWriter, r *http.Request) {
|
func (a *chatAPI) handleChatStream(w http.ResponseWriter, r *http.Request) {
|
||||||
chat, ok := a.chatReady(w)
|
chat, ok := a.chatReady(w)
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -445,6 +447,30 @@ func (a *chatAPI) handleChatStream(w http.ResponseWriter, r *http.Request) {
|
|||||||
_, _ = fmt.Fprintf(w, "data: %s\n\n", payload)
|
_, _ = fmt.Fprintf(w, "data: %s\n\n", payload)
|
||||||
flusher.Flush()
|
flusher.Flush()
|
||||||
}
|
}
|
||||||
|
// A tool can run for minutes without a frame; a comment line keeps the
|
||||||
|
// browser's idle timer and any proxy from cutting the stream.
|
||||||
|
stopKeepalive := make(chan struct{})
|
||||||
|
keepaliveDone := make(chan struct{})
|
||||||
|
defer func() {
|
||||||
|
close(stopKeepalive)
|
||||||
|
<-keepaliveDone
|
||||||
|
}()
|
||||||
|
go func() {
|
||||||
|
defer close(keepaliveDone)
|
||||||
|
tick := time.NewTicker(chatKeepaliveInterval)
|
||||||
|
defer tick.Stop()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-stopKeepalive:
|
||||||
|
return
|
||||||
|
case <-tick.C:
|
||||||
|
writeMu.Lock()
|
||||||
|
_, _ = fmt.Fprint(w, ": keepalive\n\n")
|
||||||
|
flusher.Flush()
|
||||||
|
writeMu.Unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
asker := dashboardAsker{asks: &a.asks, write: func(frame chatStreamEvent) { writeChatEvent(frame, "") }}
|
asker := dashboardAsker{asks: &a.asks, write: func(frame chatStreamEvent) { writeChatEvent(frame, "") }}
|
||||||
events, err := chat.Contract.Stream(withDashboardAsks(r.Context(), asker), msg)
|
events, err := chat.Contract.Stream(withDashboardAsks(r.Context(), asker), msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+13
-1
@@ -626,6 +626,9 @@ export async function sendMessage(retryOpts?: RetryOptions): Promise<void> {
|
|||||||
await reader.cancel().catch(() => {});
|
await reader.cancel().catch(() => {});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// Any bytes, including the server's keepalive, show the turn is alive;
|
||||||
|
// a question waiting on the operator stops the clock until answered.
|
||||||
|
if (!done && !streamingTurn.value?.ask) arm();
|
||||||
buffer += decoder.decode(value || new Uint8Array(), { stream: !done });
|
buffer += decoder.decode(value || new Uint8Array(), { stream: !done });
|
||||||
const frames = buffer.split("\n\n");
|
const frames = buffer.split("\n\n");
|
||||||
buffer = frames.pop() || "";
|
buffer = frames.pop() || "";
|
||||||
@@ -713,7 +716,11 @@ export async function sendMessage(retryOpts?: RetryOptions): Promise<void> {
|
|||||||
statusText.value = "Stopped";
|
statusText.value = "Stopped";
|
||||||
} else {
|
} else {
|
||||||
streamingTurn.value = {
|
streamingTurn.value = {
|
||||||
text: `Unable to complete that turn: ${failed.message || failed}`,
|
text: `Unable to complete that turn: ${
|
||||||
|
timedOut
|
||||||
|
? `no activity for ${STREAM_TIMEOUT_MS / 60000} minutes${lastStep(activeTools)}`
|
||||||
|
: failed.message || failed
|
||||||
|
}`,
|
||||||
tools: activeTools,
|
tools: activeTools,
|
||||||
isError: true,
|
isError: true,
|
||||||
turn: turn ? retryChatTurn(turn) : undefined,
|
turn: turn ? retryChatTurn(turn) : undefined,
|
||||||
@@ -736,6 +743,11 @@ export async function sendMessage(retryOpts?: RetryOptions): Promise<void> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function lastStep(tools: ChatToolCall[]): string {
|
||||||
|
const last = tools.filter((tool) => tool.name || tool.tool).at(-1);
|
||||||
|
return last ? `, last step: ${last.name || last.tool}` : "";
|
||||||
|
}
|
||||||
|
|
||||||
export function stopTurn(): void {
|
export function stopTurn(): void {
|
||||||
statusText.value = "Stopping…";
|
statusText.value = "Stopping…";
|
||||||
if (currentSession.value) {
|
if (currentSession.value) {
|
||||||
|
|||||||
Reference in new issue
Block a user