fix(chat): a failed turn reads as a plain sentence, shown as an error
This commit is contained in:
7 files changed
+42
-22
No files matched your search
@@ -7,24 +7,25 @@ import (
|
||||
"github.com/samcharles93/ai-sdk/chat"
|
||||
)
|
||||
|
||||
// turnFailureText is what a chat reader sees when a turn fails. The cause
|
||||
// carries provider bodies, URLs and wrapping that belong in the log, which
|
||||
// failTurn writes, not in front of whoever is chatting.
|
||||
// turnFailureText is what a chat reader sees when a turn fails: one complete
|
||||
// sentence they can act on, shown as the reply itself. The cause carries
|
||||
// provider bodies, URLs and wrapping that belong in the log, which failTurn
|
||||
// writes, not in front of whoever is chatting.
|
||||
func turnFailureText(err error) string {
|
||||
switch {
|
||||
case errors.Is(err, chat.ErrRateLimited):
|
||||
return "the model provider is refusing requests (rate limit or spending cap). Try again later, or switch model with /model."
|
||||
return "The model provider is refusing requests right now (rate limit or spending cap). Try again later, or pick another model with /model."
|
||||
case errors.Is(err, chat.ErrAuthFailed):
|
||||
return "the model provider rejected Archie's credentials. Check the provider key in settings."
|
||||
return "The model provider rejected Archie's credentials. An admin needs to check the provider key in Settings."
|
||||
case errors.Is(err, chat.ErrProviderUnavailable):
|
||||
return "the model provider is unavailable. Try again in a moment."
|
||||
return "The model provider is unavailable. Try again in a moment."
|
||||
case errors.Is(err, chat.ErrContextLength):
|
||||
return "this conversation is too long for the model. Start a new chat, or shorten it with /compress."
|
||||
return "This conversation is too long for the model. Start a new chat, or shorten this one with /compress."
|
||||
case errors.Is(err, chat.ErrUnsupportedContent):
|
||||
return "the model can't read one of the attachments. Remove it, or switch model with /model."
|
||||
return "The model can't read one of the attachments. Remove it, or pick another model with /model."
|
||||
case errors.Is(err, context.DeadlineExceeded):
|
||||
return "the model took too long to answer. Try again."
|
||||
return "The model took too long to answer. Try again."
|
||||
default:
|
||||
return "something went wrong on Archie's side. Try again; the gateway log has the cause."
|
||||
return "Archie couldn't reply. Try again in a moment."
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ func TestStreamedTurnFailureHidesCause(t *testing.T) {
|
||||
}{
|
||||
{name: "spending cap", cause: chat.ErrRateLimited, want: "rate limit or spending cap"},
|
||||
{name: "bad key", cause: chat.ErrAuthFailed, want: "credentials"},
|
||||
{name: "unknown", cause: errors.New("dial tcp 10.0.0.1:5432: refused"), want: "gateway log"},
|
||||
{name: "unknown", cause: errors.New("dial tcp 10.0.0.1:5432: refused"), want: "Archie couldn't reply"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
raw := fmt.Errorf(`llm chat stream: gemini: status 429: {"message":"https://ai.studio/spend"}: %w`, tc.cause)
|
||||
|
||||
@@ -176,9 +176,8 @@ func (a *chatAPI) chatReady(w http.ResponseWriter) (*ChatService, bool) {
|
||||
|
||||
// chatFailureText is what the browser sees when a chat backend call fails.
|
||||
// The cause can carry SQL, gRPC status details or host paths, so failChat
|
||||
// logs it server-side and sends this sentence instead, in the same plain
|
||||
// wording the gateway gives chat readers for a failed turn.
|
||||
const chatFailureText = "something went wrong on Archie's side. Try again; the server log has the cause."
|
||||
// logs it server-side and sends this sentence instead.
|
||||
const chatFailureText = "Something went wrong. Try again in a moment."
|
||||
|
||||
// failChat logs the real cause with the request's trace and answers with the
|
||||
// plain sentence. A nil logger is tolerated, as tests build servers without one.
|
||||
|
||||
@@ -23,7 +23,7 @@ var errInternal = errors.New(internalDetail)
|
||||
|
||||
// plainChatFailure is the sentence the browser must see instead; spelled out
|
||||
// here so the test also compiles, and fails, against the unsanitised handlers.
|
||||
const plainChatFailure = "something went wrong on Archie's side. Try again; the server log has the cause."
|
||||
const plainChatFailure = "Something went wrong. Try again in a moment."
|
||||
|
||||
// failingChat fails the contract methods its fields name, so one fake covers
|
||||
// every chat endpoint.
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { Alert, AlertDescription } from "@/components/ui/alert";
|
||||
import { computed } from "vue";
|
||||
|
||||
import { Bubble, BubbleContent } from "@/components/ui/bubble";
|
||||
@@ -32,6 +33,8 @@ const props = withDefaults(
|
||||
reasoning?: string;
|
||||
/** The live turn is still arriving and has not reasoned yet. */
|
||||
thinkingPending?: boolean;
|
||||
/** The text says why the turn failed rather than answering. */
|
||||
error?: boolean;
|
||||
}>(),
|
||||
{ tools: () => [], media: () => [], assistant: false, streaming: false, reasoning: "", thinkingPending: false },
|
||||
);
|
||||
@@ -83,7 +86,14 @@ const segments = computed(() => {
|
||||
:text="reasoning"
|
||||
:pending="thinkingPending"
|
||||
/>
|
||||
<template v-for="(segment, i) in segments" :key="i">
|
||||
<template v-if="error">
|
||||
<Trace v-if="tools.length" class="mb-2">
|
||||
<ChatToolGroup v-if="tools.length > 1" :tools="tools" :streaming="false" />
|
||||
<ChatToolCall v-else :tool="tools[0]" :streaming="false" />
|
||||
</Trace>
|
||||
<Alert variant="destructive" class="text-label"><AlertDescription>{{ text }}</AlertDescription></Alert>
|
||||
</template>
|
||||
<template v-for="(segment, i) in segments" v-else :key="i">
|
||||
<ChatMarkdown v-if="'text' in segment" :text="segment.text" />
|
||||
<Trace v-else class="my-2">
|
||||
<ChatToolGroup
|
||||
|
||||
@@ -50,6 +50,7 @@ function parts(message: ChatMessage, index: number) {
|
||||
assistant: from !== "web",
|
||||
update: message.update,
|
||||
choose: message.choose,
|
||||
error: !!message.error,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -82,6 +83,7 @@ const rows = computed(() => {
|
||||
assistant: true,
|
||||
update: undefined,
|
||||
choose: undefined,
|
||||
error: turn.isError,
|
||||
live: true,
|
||||
reasoning: turn.reasoning ?? "",
|
||||
// Nothing said and nothing reasoned yet: the turn may still be thinking,
|
||||
@@ -129,6 +131,7 @@ const rows = computed(() => {
|
||||
:streaming="row.live"
|
||||
:reasoning="row.reasoning"
|
||||
:thinking-pending="row.thinkingPending"
|
||||
:error="row.error"
|
||||
>
|
||||
<template v-if="row.live && streamingTurn">
|
||||
<Button
|
||||
|
||||
+13
-6
@@ -108,6 +108,8 @@ export interface ChatMessage {
|
||||
update?: ChatUpdate;
|
||||
/** A bare /model, answered with its choices. */
|
||||
choose?: "model";
|
||||
/** A reply standing in for a turn that failed; the bubble shows it as an error. */
|
||||
error?: boolean;
|
||||
}
|
||||
|
||||
/** The update snapshot /update shows, and whether this host can install it. */
|
||||
@@ -433,7 +435,8 @@ async function readTranscript(id: string): Promise<ChatMessage[]> {
|
||||
text:
|
||||
failed.status === "cancelled"
|
||||
? "Stopped before replying."
|
||||
: `Unable to complete that turn: ${failed.error || "unknown error"}`,
|
||||
: failed.error || TURN_FAILED,
|
||||
error: failed.status !== "cancelled",
|
||||
tool_calls: failed.tool_calls || [],
|
||||
},
|
||||
];
|
||||
@@ -894,11 +897,11 @@ export async function sendMessage(retryOpts?: RetryOptions): Promise<void> {
|
||||
statusText.value = "Stopped";
|
||||
} else {
|
||||
streamingTurn.value = {
|
||||
text: `Unable to complete that turn: ${
|
||||
timedOut
|
||||
? `no activity for ${STREAM_TIMEOUT_MS / 60000} minutes${lastStep(activeTools)}`
|
||||
: failed.message || failed
|
||||
}`,
|
||||
text: timedOut
|
||||
? `Archie stopped responding: no activity for ${STREAM_TIMEOUT_MS / 60000} minutes${lastStep(activeTools)}. Try again.`
|
||||
: failed instanceof TypeError
|
||||
? "Archie can't be reached. Check your connection and try again."
|
||||
: failed.message || TURN_FAILED,
|
||||
tools: activeTools,
|
||||
isError: true,
|
||||
turn: turn ? retryChatTurn(turn) : undefined,
|
||||
@@ -922,6 +925,10 @@ export async function sendMessage(retryOpts?: RetryOptions): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
// The server sends a sentence the reader can act on; this is for a failure
|
||||
// that never reached it, such as a dropped connection.
|
||||
const TURN_FAILED = "Archie couldn't reply. Try again in a moment.";
|
||||
|
||||
function lastStep(tools: ChatToolCall[]): string {
|
||||
const last = tools.filter((tool) => tool.name || tool.tool).at(-1);
|
||||
return last ? `, last step: ${last.name || last.tool}` : "";
|
||||
|
||||
Reference in new issue
Block a user