The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
[Metabot] Request cancellation (#65511)
Sloan Sparger53cf2ab3059c
diff --git a/dev/src/dev/deps_graph.clj b/dev/src/dev/deps_graph.cljindex 8a14af16898..e59234160d6 100644--- a/dev/src/dev/deps_graph.clj+++ b/dev/src/dev/deps_graph.clj@@ -356,8 +356,8 @@([deps module-x module-y](let [module-x-ns->module-y-ns (->> (external-usages deps module-y)- (filter #(= (:module %) module-x))- (map (juxt :namespace :depends-on-namespace)))]+ (filter #(= (:module %) module-x))+ (map (juxt :namespace :depends-on-namespace)))](reduce(fn [m [module-x-ns module-y-ns]](update m module-x-ns (fn [deps]diff --git a/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/__snapshots__/process-stream.unit.spec.ts.snap b/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/__snapshots__/process-stream.unit.spec.ts.snapindex 98650e77661..b426fab70d8 100644--- a/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/__snapshots__/process-stream.unit.spec.ts.snap+++ b/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/__snapshots__/process-stream.unit.spec.ts.snap@@ -2,6 +2,7 @@exports[`processChatResponse should be able to process a valid stream 1`] = `{+ "aborted": false,"data": [{"type": "state",@@ -90,3 +91,44 @@ exports[`processChatResponse should be able to process a valid stream 1`] = `],}`;++exports[`processChatResponse should resolve with partial response for aborted requests 1`] = `+{+ "aborted": false,+ "data": [+ {+ "type": "state",+ "value": {+ "testing": 123,+ },+ "version": 1,+ },+ ],+ "history": [+ {+ "content": "Partial response",+ "role": "assistant",+ },+ ],+ "parts": [+ {+ "code": "0",+ "name": "text",+ "value": "Partial response",+ },+ {+ "code": "2",+ "name": "data",+ "value": {+ "type": "state",+ "value": {+ "testing": 123,+ },+ "version": 1,+ },+ },+ ],+ "text": "Partial response",+ "toolCalls": [],+}+`;diff --git a/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/__snapshots__/requests.unit.spec.ts.snap b/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/__snapshots__/requests.unit.spec.ts.snapindex 863e4ec577e..098a5bdd31f 100644--- a/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/__snapshots__/requests.unit.spec.ts.snap+++ b/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/__snapshots__/requests.unit.spec.ts.snap@@ -2,6 +2,7 @@exports[`ai requests aiStreamingQuery should return full result of a successful request 1`] = `{+ "aborted": false,"data": [{"type": "state",diff --git a/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/process-stream.ts b/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/process-stream.tsindex b8f1c11650d..c4dac4cb0b0 100644--- a/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/process-stream.ts+++ b/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/process-stream.ts@@ -1,4 +1,4 @@-import { match } from "ts-pattern";+import { isMatching, match } from "ts-pattern";import _ from "underscore";import type { MetabotHistory } from "metabase-types/api";@@ -190,73 +190,95 @@ type StreamPartValue<name extends ParsedStreamPartName> = Extract<export type AIStreamingConfig = {onTextPart?: (part: StreamPartValue<"text">) => void;- // callback is only called if this version of the client is aware of the recieved data part type+ // callback is only called if this version of the client is aware of the received data part typeonDataPart?: (part: KnownDataPart) => void;onToolCallPart?: (part: StreamPartValue<"tool_call">) => void;onToolResultPart?: (part: StreamPartValue<"tool_result">) => void;onError?: (error: StreamPartValue<"error">) => void;};+interface ProcessedChatResponse extends AccumulatedStreamParts {+ aborted: boolean;+}++/**+ * Processes a stream that follows our AI response protocol and notifies+ * the appropriate handlers as text, data, tool call, etc. parts come in.+ *+ * This function does not error on aborted requests and will return whatever+ * values that it has received so far as a result.+ */export async function processChatResponse(stream: ReadableStream<Uint8Array>,config: AIStreamingConfig,-) {+): Promise<ProcessedChatResponse> {const parsedStreamParts: ParsedStreamPart[] = [];const reader = stream.getReader();const decoder = new TextDecoder();let chunks: Uint8Array[] = [];- // eslint-disable-next-line no-constant-condition- while (true) {- const { value: chunk } = await reader.read();+ let aborted = false;+ while (!aborted) {+ try {+ const { value: chunk } = await reader.read();- if (chunk) {- chunks.push(chunk);- if (chunk[chunk.length - 1] !== "\n".charCodeAt(0)) {- // if the last character is not a newline, we have not read the whole JSON value- continue;+ if (chunk) {+ chunks.push(chunk);+ if (chunk[chunk.length - 1] !== "\n".charCodeAt(0)) {+ // if the last character is not a newline, we have not read the whole JSON value+ continue;+ }}- }- if (chunks.length === 0) {- break; // we have reached the end of the stream- }+ if (chunks.length === 0) {+ break; // we have reached the end of the stream+ }- const concatenatedChunks = concatChunks(chunks);- chunks = [];+ const concatenatedChunks = concatChunks(chunks);+ chunks = [];- const streamParts = _.compact(- decoder- .decode(concatenatedChunks, { stream: true })- .split("\n")- .filter((line) => line !== "") // splitting leaves an empty string at the end- .map(parseDataStreamPart),- );+ const streamParts = _.compact(+ decoder+ .decode(concatenatedChunks, { stream: true })+ .split("\n")+ .filter((line) => line !== "") // splitting leaves an empty string at the end+ .map(parseDataStreamPart),+ );- for (const streamPart of streamParts) {- parsedStreamParts.push(streamPart);+ for (const streamPart of streamParts) {+ parsedStreamParts.push(streamPart);- if (streamPart.name === "text") {- config.onTextPart?.(streamPart.value);- }- if (streamPart.name === "data") {- if (isKnownDataPart(streamPart)) {- config.onDataPart?.(streamPart.value);- } else {- console.warn("Skipping unknown data part:", streamPart);+ if (streamPart.name === "text") {+ config.onTextPart?.(streamPart.value);+ }+ if (streamPart.name === "data") {+ if (isKnownDataPart(streamPart)) {+ config.onDataPart?.(streamPart.value);+ } else {+ console.warn("Skipping unknown data part:", streamPart);+ }+ }+ if (streamPart.name === "tool_call") {+ config.onToolCallPart?.(streamPart.value);+ }+ if (streamPart.name === "tool_result") {+ config.onToolResultPart?.(streamPart.value);+ }+ if (streamPart.name === "error") {+ config.onError?.(streamPart.value);}}- if (streamPart.name === "tool_call") {- config.onToolCallPart?.(streamPart.value);- }- if (streamPart.name === "tool_result") {- config.onToolResultPart?.(streamPart.value);- }- if (streamPart.name === "error") {- config.onError?.(streamPart.value);+ } catch (err) {+ if (isMatching({ name: "AbortError" }, err)) {+ aborted = true;+ } else {+ throw err;}}}- return accumulateStreamParts(parsedStreamParts);+ return {+ ...accumulateStreamParts(parsedStreamParts),+ aborted,+ };}diff --git a/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/process-stream.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/process-stream.unit.spec.tsindex ccc7ce57fae..c4bee6e8356 100644--- a/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/process-stream.unit.spec.ts+++ b/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/process-stream.unit.spec.ts@@ -109,4 +109,45 @@ describe("processChatResponse", () => {{ content: "You, but don't tell anyone.", role: "assistant" },]);});++ it("should resolve with partial response for aborted requests", async () => {+ const mockStream = createMockReadableStream(+ [+ `0:"Partial response"`,+ `2:{"type":"state","version":1,"value":{"testing":123}}`,+ ],+ {+ streamOptions: {+ pull() {+ throw new DOMException("Stream aborted", "AbortError");+ },+ },+ },+ );+ const config = getMockedCallbacks();++ const result = await processChatResponse(mockStream, config);++ expect(result).toMatchSnapshot();+ expect(config.onTextPart).toHaveBeenCalled();+ expect(config.onDataPart).toHaveBeenCalled();+ expect(config.onError).not.toHaveBeenCalled();+ });++ it("should throw error if stream errors for another reason", async () => {+ const error = new Error("some non-abort related error");++ await expect(+ processChatResponse(+ createMockReadableStream([`0:"Starting response"`], {+ streamOptions: {+ async start() {+ throw error;+ },+ },+ }),+ expectNoStreamedError,+ ),+ ).rejects.toThrow(error);+ });});diff --git a/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/test-utils.ts b/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/test-utils.tsindex 000470c9b6b..c808e9f3a46 100644--- a/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/test-utils.ts+++ b/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/test-utils.ts@@ -14,10 +14,11 @@ export function createPauses<Count extends number>(count: Count) {export function createMockReadableStream(textChunks: string[] | AsyncGenerator<string, void, unknown>,options?: {- disableAutoInsertNewLines: boolean;+ disableAutoInsertNewLines?: boolean;+ streamOptions?: Partial<ConstructorParameters<typeof ReadableStream>[0]>;},-) {- return new ReadableStream({+): ReadableStream<Uint8Array> {+ return new ReadableStream<Uint8Array>({async start(controller) {const textEncoder = new TextEncoder();try {@@ -30,12 +31,13 @@ export function createMockReadableStream(controller.close();}},+ ...(options?.streamOptions ?? {}),});}export function mockEndpoint<T extends Response>(url: string,- endpointMock: () => Promise<T>,+ endpointMock: (init?: RequestInit) => Promise<T>,) {const originalFetch = global.fetch;const mockedFetch = jest.spyOn(global, "fetch");@@ -47,7 +49,7 @@ export function mockEndpoint<T extends Response>(typeof fetchedUrl === "string" && fetchedUrl.includes(url);if (isRequestedUrl) {- return endpointMock();+ return endpointMock(args?.[0]);} else {// remove calls that route to global fetchmockedFetch.mock.calls.pop();@@ -75,15 +77,38 @@ export function mockStreamedEndpoint(url: string,{ textChunks, stream, initialDelay = 0 }: MockStreamedEndpointParams,) {- return mockEndpoint(url, async () => {+ return mockEndpoint(url, async (init) => {await delay(initialDelay);- return {- status: 202,- ok: true,- body:- stream ||- (textChunks && createMockReadableStream(textChunks)) ||- undefined,- } as any;+ const body =+ stream ||+ (textChunks && createMockReadableStream(textChunks)) ||+ undefined;++ // make stream abortable+ if (body) {+ let activeReader: ReadableStreamDefaultReader<any> | null = null;+ const originalGetReader = body.getReader.bind(body);++ body.getReader = function () {+ activeReader = originalGetReader();+ const originalRead = activeReader.read.bind(activeReader);++ // Race the read with the abort promise+ activeReader.read = async function () {+ return Promise.race([+ originalRead(),+ new Promise<never>((_, reject) => {+ init?.signal?.addEventListener("abort", () => {+ reject(new DOMException("Stream aborted", "AbortError"));+ });+ }),+ ]);+ };++ return activeReader;+ };+ }++ return { status: 202, ok: true, body } as any;});}diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotAgentToolCallMessage.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotAgentToolCallMessage.tsxindex 81af983f339..8fb7b6b37e3 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotAgentToolCallMessage.tsx+++ b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotAgentToolCallMessage.tsx@@ -71,7 +71,14 @@ const ToolCallDetailsModal = ({{message.result && (<Stack gap="xs"><Flex gap="xs">- <Text fw="bold">{t`Response`}</Text>+ <Flex align="center">+ <Text fw="bold">{t`Response`}</Text>+ {message.is_error && (+ <Badge ml="sm" bg="danger" c="text-white">+ {t`Errored`}+ </Badge>+ )}+ </Flex><ActionIcon h="sm" onClick={() => copy(message.result)}><Icon name="copy" size="1rem" /></ActionIcon>diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChat.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChat.tsxindex c016a51cbf7..a83ee4b4958 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChat.tsx+++ b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChat.tsx@@ -198,10 +198,11 @@ export const MetabotChat = ({ref={metabot.promptInputRef}value={metabot.prompt}autoFocus- disabled={metabot.isDoingScience}+ isResponding={metabot.isDoingScience}placeholder={t`Tell me to do something, or ask a question`}onChange={handleEditorChange}onSubmit={handleEditorSubmit}+ onStop={metabot.cancelRequest}suggestionModels={config.suggestionModels}/></Paper>diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChatEditor/MetabotChatEditor.module.css b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChatEditor/MetabotChatEditor.module.cssindex abec8ce1cd9..8457dc5f743 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChatEditor/MetabotChatEditor.module.css+++ b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChatEditor/MetabotChatEditor.module.css@@ -13,6 +13,63 @@justify-content: center;}+.button {+ margin-bottom: 0.625rem;+ margin-right: -0.125rem;+ background: var(--mb-color-brand);+ border-radius: 50%;+ align-self: flex-end;+ margin-left: 2px;+ width: 1.25rem;+ height: 1.25rem;+ display: flex;+ justify-content: center;+ align-items: center;+ transition:+ opacity 300ms ease,+ background 150ms ease,+ transform 150ms ease;++ &:hover,+ &:active {+ opacity: 0.7;+ }++ &:active {+ transform: scale(0.9);+ }+}++.buttonHidden {+ opacity: 0;+ transition: opacity 0ms ease;+ user-select: none;+ cursor: default;++ &:hover,+ &:active {+ opacity: 0;+ }+}++.buttonResponding {+ border: 1px solid var(--mb-color-brand);+ background: transparent;+}++.sendIcon {+ color: var(--mb-color-bg-white);+ width: 0.8rem;+ transition: color 150ms ease;+}++.stopIcon {+ color: var(--mb-color-brand);+ width: 0.5rem;+ height: 0.5rem;+ transition: color 150ms ease;+}+.contentWrapper {flex: 1;min-width: 0;diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChatEditor/MetabotChatEditor.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChatEditor/MetabotChatEditor.tsxindex 7519be3b823..7b607ee5627 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChatEditor/MetabotChatEditor.tsx+++ b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChatEditor/MetabotChatEditor.tsx@@ -13,7 +13,7 @@ import { t } from "ttag";import { useSelector } from "metabase/lib/redux";import type { MetabotChatInputRef } from "metabase/metabot";import { getSetting } from "metabase/selectors/settings";-import { Box, Icon } from "metabase/ui";+import { Box, Icon, UnstyledButton } from "metabase/ui";import { createMentionSuggestion } from "metabase-enterprise/rich_text_editing/tiptap/extensions/Mention/MentionSuggestion";import {MetabotMentionExtension,@@ -33,9 +33,10 @@ interface Props {value: string;placeholder?: string;autoFocus?: boolean;- disabled?: boolean;+ isResponding?: boolean;onChange: (value: string) => void;onSubmit: () => void;+ onStop: () => void;suggestionModels: SuggestionModel[];}@@ -45,24 +46,17 @@ export const MetabotChatEditor = forwardRef<MetabotChatInputRef | null, Props>(value,placeholder = t`Tell me to do something, or ask a question`,autoFocus = false,- disabled = false,+ isResponding = false,suggestionModels,onChange,onSubmit,+ onStop,},ref,) => {const siteUrl = useSelector((state) => getSetting(state, "site-url"));const serializedRef = useRef(value);- // Use refs to avoid recreating the editor when callbacks change - not doing so prepends all characters- const onChangeRef = useRef(onChange);- const onSubmitRef = useRef(onSubmit);- useEffect(() => {- onChangeRef.current = onChange;- onSubmitRef.current = onSubmit;- });-const extensions = [Document,Paragraph,@@ -93,7 +87,7 @@ export const MetabotChatEditor = forwardRef<MetabotChatInputRef | null, Props>(onUpdate: ({ editor }) => {const jsonContent = editor.getJSON();serializedRef.current = serializeTiptapToMetabotMessage(jsonContent);- onChangeRef.current(serializedRef.current);+ onChange(serializedRef.current);},editorProps: {handleDOMEvents: {@@ -142,11 +136,17 @@ export const MetabotChatEditor = forwardRef<MetabotChatInputRef | null, Props>(if (!isModifiedKeyPress) {event.preventDefault();- onSubmitRef.current();+ onSubmit();return true;}}+ if (event.key === "Escape") {+ event.preventDefault();+ onStop();+ return true;+ }+return false;},clipboardTextSerializer: (content) => {@@ -192,10 +192,27 @@ export const MetabotChatEditor = forwardRef<MetabotChatInputRef | null, Props>(data-testid="metabot-chat-input"editor={editor}className={cx(S.content, {- [S.disabled]: disabled,+ [S.disabled]: isResponding,})}/></Box>+ <UnstyledButton+ className={cx(+ S.button,+ isResponding && S.buttonResponding,+ value.length === 0 && !isResponding && S.buttonHidden,+ )}+ onClick={isResponding ? onStop : onSubmit}+ data-testid={+ isResponding ? "metabot-stop-response" : "metabot-send-message"+ }+ >+ {isResponding ? (+ <Icon className={S.stopIcon} name="stop" />+ ) : (+ <Icon className={S.sendIcon} name="arrow_up" />+ )}+ </UnstyledButton></Box>);},diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChatEditor/MetabotChatEditor.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChatEditor/MetabotChatEditor.unit.spec.tsxindex 141b4874145..fabd344d567 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChatEditor/MetabotChatEditor.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChatEditor/MetabotChatEditor.unit.spec.tsx@@ -21,6 +21,7 @@ const defaultProps = {value: "",onChange: jest.fn(),onSubmit: jest.fn(),+ onStop: jest.fn(),suggestionModels: ["table","database",diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/hooks/use-metabot-agent.ts b/enterprise/frontend/src/metabase-enterprise/metabot/hooks/use-metabot-agent.tsindex 00b2ffd6bbb..b5ec08560ea 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/hooks/use-metabot-agent.ts+++ b/enterprise/frontend/src/metabase-enterprise/metabot/hooks/use-metabot-agent.ts@@ -8,6 +8,7 @@ import { trackMetabotRequestSent } from "../analytics";import {type MetabotPromptSubmissionResult,type MetabotUserChatMessage,+ cancelInflightAgentRequests,getActiveToolCalls,getAgentErrorMessages,getDebugMode,@@ -153,6 +154,10 @@ export const useMetabotAgent = () => {[dispatch, getChatContext, metabotRequestId, prepareRetryIfUnsuccesful],);+ const cancelRequest = useCallback(() => {+ dispatch(cancelInflightAgentRequests());+ }, [dispatch]);+const startNewConversation = useCallback(async (message: string) => {await resetConversation();@@ -180,6 +185,7 @@ export const useMetabotAgent = () => {startNewConversation,submitInput,retryMessage,+ cancelRequest,activeToolCalls,debugMode,profileOverride,diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/metabot.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/metabot.unit.spec.tsxindex 6f7ff6ec6b2..13d55cf330e 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/metabot.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/metabot/metabot.unit.spec.tsx@@ -126,6 +126,8 @@ const enterChatMessage = async (message: string, send = true) => {await userEvent.type(await input(), "{Enter}");}};+const sendMessageButton = () => screen.findByTestId("metabot-send-message");+const stopResponseButton = () => screen.findByTestId("metabot-stop-response");const closeChatButton = () => screen.findByTestId("metabot-close-chat");const responseLoader = () => screen.findByTestId("metabot-response-loader");const resetChatButton = () => screen.findByTestId("metabot-reset-chat");@@ -152,7 +154,7 @@ const assertConversation = async (});} else {const realMessages = await chatMessages();- expect(expectedMessages.length).toBe(realMessages.length);+ expect(realMessages.length).toBe(expectedMessages.length);expectedMessages.forEach(([expectedRole, expectedMessage], index) => {const realMessage = realMessages[index];expect(realMessage).toHaveAttribute("data-message-role", expectedRole);@@ -517,10 +519,23 @@ describe("metabot-streaming", () => {expect(await input()).toHaveFocus();});+ it("should be able to send a message via send button", async () => {+ setup();+ mockAgentEndpoint({ textChunks: whoIsYourFavoriteResponse });++ await enterChatMessage("Who is your favorite?", false);+ expect(await input()).toHaveTextContent("Who is your favorite?");+ (await sendMessageButton()).click();++ expect(+ await screen.findByText("You, but don't tell anyone."),+ ).toBeInTheDocument();+ });+it("should properly handle partial messages", async () => {setup();- const [pause1] = createPauses(2);+ const [pause1] = createPauses(1);mockAgentEndpoint({stream: createMockReadableStream((async function* () {@@ -680,6 +695,62 @@ describe("metabot-streaming", () => {["agent", "Response 2"],]);});++ it("should be able to stop a response via stop button", async () => {+ setup();++ const [pause1] = createPauses(1);+ mockAgentEndpoint({+ stream: createMockReadableStream(+ (async function* () {+ yield `0:"You, but "\n`;+ await pause1.promise;+ yield `0:"don't tell anyone."\n`;+ })(),+ ),+ });++ await enterChatMessage("Who is your favorite?");+ await userEvent.click(await stopResponseButton());+ pause1.resolve();++ mockAgentEndpoint({ textChunks: whoIsYourFavoriteResponse });+ await enterChatMessage("Who is your favorite?");+ await assertConversation([+ ["user", "Who is your favorite?"],+ ["agent", "You, but"],+ ["user", "Who is your favorite?"],+ ["agent", "You, but don't tell anyone."],+ ]);+ });++ it("should be able to stop a response via escape press", async () => {+ setup();++ const [pause1] = createPauses(1);+ mockAgentEndpoint({+ stream: createMockReadableStream(+ (async function* () {+ yield `0:"You, but "\n`;+ await pause1.promise;+ yield `0:"don't tell anyone."\n`;+ })(),+ ),+ });++ await enterChatMessage("Who is your favorite?");+ await userEvent.type(await input(), "{Escape}");+ pause1.resolve();++ mockAgentEndpoint({ textChunks: whoIsYourFavoriteResponse });+ await enterChatMessage("Who is your favorite?");+ await assertConversation([+ ["user", "Who is your favorite?"],+ ["agent", "You, but"],+ ["user", "Who is your favorite?"],+ ["agent", "You, but don't tell anyone."],+ ]);+ });});describe("errors", () => {@@ -882,6 +953,68 @@ describe("metabot-streaming", () => {await enterChatMessage("Request");expect(getState()).toEqual({});});++ it("should preserve conversation state if aborted response didn't contain a state data object", async () => {+ const { store } = setup();+ const getState = () => getMetabotState(store.getState() as any);++ // insert some state via previous convo+ mockAgentEndpoint({+ textChunks: [+ `0:"here ya go"`,+ `2:{"type":"state","version":1,"value":{"testing":123}}`,+ `d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`,+ ],+ });+ await enterChatMessage("gimme state plz");+ assertConversation([+ ["user", "gimme state plz"],+ ["agent", "here ya go"],+ ]);+ expect(getState()).toEqual({ testing: 123 });++ const [pause1] = createPauses(1);+ mockAgentEndpoint({+ stream: createMockReadableStream(+ (async function* () {+ yield `0:"blah blah blah"\n`;+ await pause1.promise;+ yield `0:"something something"\n`;+ })(),+ ),+ });++ await enterChatMessage("i'm going to cancel this request...");+ assertConversation([+ ["user", "gimme state plz"],+ ["agent", "here ya go"],+ ["user", "i'm going to cancel this request..."],+ ["agent", "blah blah blah"],+ ]);+ await userEvent.click(await stopResponseButton());+ pause1.resolve();++ expect(getState()).toEqual({ testing: 123 });+ });++ it("should use new state object if aborted response contained one", async () => {+ const { store } = setup();+ const getState = () => getMetabotState(store.getState() as any);++ // insert some state via previous convo+ const [pause1] = createPauses(1);+ mockAgentEndpoint({+ stream: createMockReadableStream(+ (async function* () {+ yield `2:{"type":"state","version":1,"value":{"testing":123}}`,+ await pause1.promise;+ })(),+ ),+ });+ await enterChatMessage("hi");+ await userEvent.click(await stopResponseButton());+ expect(getState()).toEqual({ testing: 123 });+ });});describe("history", () => {@@ -1021,6 +1154,36 @@ describe("metabot-streaming", () => {});expect(screen.queryByText(/xxxxxxx/)).not.toBeInTheDocument();});++ it("should manually insert synthetic tool results for aborted requests with unresolved tool calls", async () => {+ const { store } = setup();++ // insert some state via previous convo+ const [pause1] = createPauses(1);+ mockAgentEndpoint({+ stream: createMockReadableStream(+ (async function* () {+ yield `9:{"toolCallId":"test","toolName":"test","args":""}`;+ await pause1.promise;+ })(),+ ),+ });+ await enterChatMessage("hi");+ await userEvent.click(await stopResponseButton());+ pause1.resolve();+ expect(getHistory(store.getState() as any)).toMatchObject([+ { content: "hi", role: "user" },+ {+ role: "assistant",+ tool_calls: [{ arguments: "", id: "test", name: "test" }],+ },+ {+ content: "Tool execution interrupted by user",+ role: "tool",+ tool_call_id: "test",+ },+ ]);+ });});describe("experimental", () => {diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/state/actions.ts b/enterprise/frontend/src/metabase-enterprise/metabot/state/actions.tsindex dd45c8c4849..5bb339a7cd4 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/state/actions.ts+++ b/enterprise/frontend/src/metabase-enterprise/metabot/state/actions.ts@@ -199,7 +199,7 @@ export const submitInput = createAsyncThunk<const result = await sendMessageRequestPromise;- if (isRejected(result)) {+ if (isRejected(result) && result.payload?.type === "error") {dispatch(stopProcessingAndNotify(result.payload?.errorMessage));return {prompt: data.message,@@ -218,12 +218,19 @@ export const submitInput = createAsyncThunk<},);+type SendAgentRequestError =+ | ({ type: "error" } & PromptErrorOutcome)+ | ({+ type: "abort";+ unresolved_tool_calls: { toolCallId: string; toolName: string }[];+ } & MetabotAgentResponse);+export const sendAgentRequest = createAsyncThunk<- Omit<MetabotAgentResponse, "reactions">,+ MetabotAgentResponse,Omit<MetabotAgentRequest, "conversation_id">,- { rejectValue: PromptErrorOutcome }+ { rejectValue: SendAgentRequestError }>(- "metabase-enterprise/metabot/sendStreamedAgentRequest",+ "metabase-enterprise/metabot/sendAgentRequest",async (req,{ dispatch, getState, signal, rejectWithValue, fulfillWithValue },@@ -326,6 +333,20 @@ export const sendAgentRequest = createAsyncThunk<throw error;}+ if (response.aborted) {+ return rejectWithValue({+ type: "abort",+ conversation_id: body.conversation_id,+ unresolved_tool_calls: response.toolCalls.filter(+ (tc) => tc.state === "call",+ ),+ history: [...getHistory(getState() as any), ...response.history],+ // state object comes at the end, so we may not have recieved it+ // so fallback to the state used when the request was issued+ state: Object.keys(state).length === 0 ? body.state : state,+ });+ }+return fulfillWithValue({conversation_id: body.conversation_id,history: [...getHistory(getState() as any), ...response.history],@@ -333,7 +354,7 @@ export const sendAgentRequest = createAsyncThunk<});} catch (error) {console.error(error);- return rejectWithValue(handleResponseError(error));+ return rejectWithValue({ type: "error", ...handleResponseError(error) });}},);diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/state/reducer.ts b/enterprise/frontend/src/metabase-enterprise/metabot/state/reducer.tsindex cc201517df8..ab9f282b8ac 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/state/reducer.ts+++ b/enterprise/frontend/src/metabase-enterprise/metabot/state/reducer.ts@@ -63,6 +63,7 @@ export type MetabotDebugToolCallMessage = {args?: string;status: "started" | "ended";result?: string;+ is_error?: boolean;};export type MetabotAgentChatMessage =@@ -339,12 +340,38 @@ export const metabot = createSlice({state.errorMessages = [];}).addCase(sendAgentRequest.fulfilled, (state, action) => {- state.history = action.payload?.history?.slice() ?? [];state.state = { ...(action.payload?.state ?? {}) };+ state.history = action.payload?.history?.slice() ?? [];state.activeToolCalls = [];state.isProcessing = false;})- .addCase(sendAgentRequest.rejected, (state) => {+ .addCase(sendAgentRequest.rejected, (state, action) => {+ // aborted requests needs special state adjustments+ if (action.payload?.type === "abort") {+ state.state = { ...(action.payload?.state ?? {}) };+ state.history = action.payload?.history?.slice() ?? [];+ if (action.payload.unresolved_tool_calls.length > 0) {+ // update history w/ synthetic tool_result entries for each unresolved tool call+ // as having a tool_call without a matching tool_result is invalid+ const syntheticToolResults =+ action.payload.unresolved_tool_calls.map((tc) => ({+ role: "tool" as const,+ content: "Tool execution interrupted by user",+ tool_call_id: tc.toolCallId,+ }));+ state.history.push(...syntheticToolResults);++ // update message state so that unresolved tools are marked as ended+ state.messages.forEach((msg) => {+ if (msg.type === "tool_call" && msg.status === "started") {+ msg.status = "ended";+ msg.result = "Tool execution interrupted by user";+ msg.is_error = true;+ }+ });+ }+ }+state.activeToolCalls = [];state.isProcessing = false;});diff --git a/frontend/src/metabase/ui/components/icons/Icon/icons/stop.svg b/frontend/src/metabase/ui/components/icons/Icon/icons/stop.svgindex 26e3555afec..ab2eb6191e3 100644--- a/frontend/src/metabase/ui/components/icons/Icon/icons/stop.svg+++ b/frontend/src/metabase/ui/components/icons/Icon/icons/stop.svg@@ -1,3 +1,3 @@-<svg width="12" height="12" fill="currentcolor" xmlns="http://www.w3.org/2000/svg">+<svg viewBox="0 0 12 12" width="12" height="12" fill="currentcolor" xmlns="http://www.w3.org/2000/svg"><rect width="12" height="12" rx="2" /></svg>