Skip to content

Commit

Permalink
feat(runtimes/local): add reset functionality (#679)
Browse files Browse the repository at this point in the history
* refactor: Add reset functionality to LocalThreadRuntime and MessageRepository
* add reset functionality to LocalRuntime
  • Loading branch information
Rajaniraiyn authored Aug 23, 2024
1 parent 60c0fdc commit edbab24
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-games-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react": patch
---

feat(runtimes/local): reset thread
19 changes: 19 additions & 0 deletions packages/react/src/runtimes/local/LocalRuntime.tsx
Original file line number Diff line number Diff line change
@@ -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<LocalThreadRuntime> {
private readonly _proxyConfigProvider: ProxyConfigProvider;
Expand Down Expand Up @@ -32,4 +34,21 @@ export class LocalRuntime extends BaseAssistantRuntime<LocalThreadRuntime> {
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,
})),
});
}
}

0 comments on commit edbab24

Please sign in to comment.