diff --git a/.changeset/thick-games-confess.md b/.changeset/thick-games-confess.md new file mode 100644 index 0000000000..03b8cecc25 --- /dev/null +++ b/.changeset/thick-games-confess.md @@ -0,0 +1,5 @@ +--- +"@assistant-ui/react": patch +--- + +feat(runtimes/local): reset thread diff --git a/packages/react/src/runtimes/local/LocalRuntime.tsx b/packages/react/src/runtimes/local/LocalRuntime.tsx index 8e69c09b31..cea8b8d076 100644 --- a/packages/react/src/runtimes/local/LocalRuntime.tsx +++ b/packages/react/src/runtimes/local/LocalRuntime.tsx @@ -1,9 +1,11 @@ import { type ModelConfigProvider } from "../../types/ModelConfigTypes"; +import type { CoreMessage } from "../../types/AssistantTypes"; import { BaseAssistantRuntime } from "../core/BaseAssistantRuntime"; import type { ChatModelAdapter } from "./ChatModelAdapter"; import { ProxyConfigProvider } from "../../internal"; import { LocalThreadRuntime } from "./LocalThreadRuntime"; import { LocalRuntimeOptions } from "./LocalRuntimeOptions"; +import { fromCoreMessages } from "../edge/converters/fromCoreMessage"; export class LocalRuntime extends BaseAssistantRuntime { private readonly _proxyConfigProvider: ProxyConfigProvider; @@ -32,4 +34,21 @@ export class LocalRuntime extends BaseAssistantRuntime { this.thread.adapter, )); } + + public reset({ + initialMessages, + }: { + initialMessages?: readonly CoreMessage[] | undefined; + } = {}) { + this.switchToThread(null); + if (!initialMessages) return; + + const messages = fromCoreMessages(initialMessages); + this.thread.import({ + messages: messages.map((m, idx) => ({ + parentId: messages[idx - 1]?.id ?? null, + message: m, + })), + }); + } }