fix(chat): show a failed turn's error in a reopened conversation
This commit is contained in:
2 files changed
+20
-2
No files matched your search
@@ -120,6 +120,7 @@ type chatUpdateRequest struct {
|
||||
|
||||
type chatTurnView struct {
|
||||
TurnID string `json:"turn_id"`
|
||||
InputMessageID string `json:"input_message_id,omitempty"`
|
||||
AssistantMessageID string `json:"assistant_message_id,omitempty"`
|
||||
Status messaging.TurnStatus `json:"status"`
|
||||
Error string `json:"error,omitempty"`
|
||||
@@ -263,7 +264,7 @@ func (a *chatAPI) handleChatTurns(w http.ResponseWriter, r *http.Request) {
|
||||
views := make([]chatTurnView, 0, len(turns))
|
||||
for _, turn := range turns {
|
||||
view := chatTurnView{
|
||||
TurnID: turn.TurnID, AssistantMessageID: turn.AssistantMessageID,
|
||||
TurnID: turn.TurnID, InputMessageID: turn.InputMessageID, AssistantMessageID: turn.AssistantMessageID,
|
||||
Status: turn.Status, Error: turn.Error,
|
||||
ToolCalls: make([]chatToolView, 0, len(turn.ToolCalls)),
|
||||
}
|
||||
|
||||
+18
-1
@@ -353,13 +353,27 @@ async function readTranscript(id: string): Promise<ChatMessage[]> {
|
||||
api.chatTurns<ChatTurnView[]>(id),
|
||||
]);
|
||||
const byAssistantMessage = new Map<string, ChatTurnView>();
|
||||
// A failed turn records no reply, so its error follows the message it failed on.
|
||||
const failedByInput = new Map<string, ChatTurnView>();
|
||||
for (const turn of turns || []) {
|
||||
const assistantID = turn.assistant_message_id || turn.AssistantMessageID;
|
||||
if (assistantID) byAssistantMessage.set(assistantID, turn);
|
||||
else if (turn.status === "failed" && turn.input_message_id)
|
||||
failedByInput.set(turn.input_message_id, turn);
|
||||
}
|
||||
return (msgs || []).map((message) => {
|
||||
return (msgs || []).flatMap((message) => {
|
||||
const messageID = message.message_id || message.MessageID;
|
||||
const turn = messageID ? byAssistantMessage.get(messageID) : undefined;
|
||||
const failed = messageID ? failedByInput.get(messageID) : undefined;
|
||||
if (failed)
|
||||
return [
|
||||
message,
|
||||
{
|
||||
from: "assistant",
|
||||
text: `Unable to complete that turn: ${failed.error || "unknown error"}`,
|
||||
tool_calls: failed.tool_calls || [],
|
||||
},
|
||||
];
|
||||
return turn
|
||||
? { ...message, tool_calls: turn.tool_calls || turn.ToolCalls || [] }
|
||||
: message;
|
||||
@@ -395,6 +409,9 @@ function isCurrentSession(id: string): boolean {
|
||||
|
||||
/** One recorded turn, as GET /api/chat/sessions/{id}/turns returns it. */
|
||||
interface ChatTurnView {
|
||||
input_message_id?: string;
|
||||
status?: string;
|
||||
error?: string;
|
||||
assistant_message_id?: string;
|
||||
AssistantMessageID?: string;
|
||||
tool_calls?: ChatToolCall[];
|
||||
|
||||
Reference in new issue
Block a user