Skip to content

Commit

Permalink
[shared-ui] Teach ChatController about errors.
Browse files Browse the repository at this point in the history
- **Teach `ChatController` about errors.**
- **docs(changeset): Teach `ChatController` about errors.**

Fixes #4204
  • Loading branch information
dglazkov authored Jan 25, 2025
1 parent b17362c commit 976928d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-peaches-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@breadboard-ai/shared-ui": patch
---

Teach `ChatController` about errors.
2 changes: 2 additions & 0 deletions packages/shared-ui/src/elements/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ export class Chat extends LitElement {
} else {
items.push(html` <p>${entry.text}</p>`);
}
} else if ("error" in entry) {
items.push(html` <p style="color:red">${entry.error}</p>`);
}
}

Expand Down
13 changes: 12 additions & 1 deletion packages/shared-ui/src/state/chat-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
ChatStatus,
ChatSystemTurnState,
} from "./types";
import { formatError } from "../utils/format-error";

export { ChatController };

Expand Down Expand Up @@ -169,8 +170,18 @@ class ChatController {
this.#appendTurn(turn);
}

#onError(_event: RunErrorEvent) {
#onError(event: RunErrorEvent) {
this.#currentInput = null;
this.#status = "stopped";
this.#appendTurn(
this.#createSystemTurn([
{
title: "Stopping due to the following error",
error: formatError(event.data.error),
},
])
);
this.#stale = true;
}

#createSystemTurn(content: ChatContent[]): ChatSystemTurnState {
Expand Down
11 changes: 10 additions & 1 deletion packages/shared-ui/src/state/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ export type ChatObjectContent = {
object: NodeValue;
};

export type ChatContent = ChatTextContent | ChatLLMContent | ChatObjectContent;
export type ChatError = {
title: string;
error: string;
};

export type ChatContent =
| ChatTextContent
| ChatLLMContent
| ChatObjectContent
| ChatError;

/**
* Represents the system entry in the chat conversation between the
Expand Down

0 comments on commit 976928d

Please sign in to comment.