Skip to content

Commit

Permalink
feat: LocalRuntime export / import (#609)
Browse files Browse the repository at this point in the history
* refactor: Add export and import methods to LocalThreadRuntime and MessageRepository

* serialize messages as { message, parentId } pairs

* refactor: export `ExportedMessageRepository` type and add persisting head state

* pnpm changeset
  • Loading branch information
Rajaniraiyn authored Jul 31, 2024
1 parent e959c68 commit f83e4d1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/silver-queens-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react": patch
---

feat: LocalRuntime export / import
11 changes: 10 additions & 1 deletion packages/react/src/runtimes/local/LocalThreadRuntime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
Unsubscribe,
} from "../../types";
import { fromCoreMessages } from "../edge";
import { MessageRepository } from "../utils/MessageRepository";
import { ExportedMessageRepository, MessageRepository } from "../utils/MessageRepository";
import type { ChatModelAdapter, ChatModelRunResult } from "./ChatModelAdapter";
import { shouldContinue } from "./shouldContinue";
import { LocalRuntimeOptions } from "./LocalRuntimeOptions";
Expand Down Expand Up @@ -239,4 +239,13 @@ export class LocalThreadRuntime implements ThreadRuntime {
this.performRoundtrip(parentId, message);
}
}

export() {
return this.repository.export();
}

import(data: ExportedMessageRepository) {
this.repository.import(data);
this.notifySubscribers()
}
}
35 changes: 35 additions & 0 deletions packages/react/src/runtimes/utils/MessageRepository.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ type RepositoryMessage = RepositoryParent & {
level: number;
};

export interface ExportedMessageRepository {
headId?: string | null;
messages: Array<{
message: ThreadMessage;
parentId: string | null;
}>;
}

const findHead = (message: RepositoryMessage): RepositoryMessage => {
if (message.next) return findHead(message.next);
return message;
Expand Down Expand Up @@ -224,4 +232,31 @@ export class MessageRepository {
}
}
}

export(): ExportedMessageRepository {
const exportItems: ExportedMessageRepository["messages"] = [];

// hint: we are relying on the insertion order of the messages
// this is important for the import function to properly link the messages
for (const [, message] of this.messages) {
exportItems.push({
message: message.current,
parentId: message.prev?.current.id ?? null,
});
}

return {
headId: this.head?.current.id ?? null,
messages: exportItems,
};
}

import({ headId, messages }: ExportedMessageRepository) {
for (const { message, parentId } of messages) {
this.addOrUpdateMessage(parentId, message);
}

// switch to the saved head id if it is not the most recent message
this.resetHead(headId ?? messages.at(-1)?.message.id ?? null);
}
}

0 comments on commit f83e4d1

Please sign in to comment.