Skip to content

Commit

Permalink
feat: ThreadRuntime.getMesssageById (#1012)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Oct 14, 2024
1 parent cf6861c commit 7c76939
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-clocks-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react": patch
---

feat: ThreadRuntime.getMesssageById
9 changes: 9 additions & 0 deletions packages/react/src/api/MessageRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ export type MessageState = ThreadMessage & {
branchNumber: number;
branchCount: number;

/**
* @deprecated This API is still under active development and might change without notice.
*/
speech: SpeechState | undefined;
submittedFeedback: SubmittedFeedback | undefined;
};
Expand All @@ -119,7 +122,13 @@ export type MessageRuntime = {

getState(): MessageState;
reload(): void;
/**
* @deprecated This API is still under active development and might change without notice.
*/
speak(): void;
/**
* @deprecated This API is still under active development and might change without notice.
*/
stopSpeaking(): void;
submitFeedback({ type }: { type: "positive" | "negative" }): void;
switchToBranch({
Expand Down
49 changes: 43 additions & 6 deletions packages/react/src/api/ThreadRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export type ThreadState = Readonly<{
messages: readonly ThreadMessage[];
suggestions: readonly ThreadSuggestion[];
extras: unknown;

/**
* @deprecated This API is still under active development and might change without notice.
*/
speech: SpeechState | undefined;
}>;

Expand Down Expand Up @@ -117,7 +121,13 @@ export type ThreadRuntime = {
export(): ExportedMessageRepository;
import(repository: ExportedMessageRepository): void;
getMesssageByIndex(idx: number): MessageRuntime;
getMesssageById(messageId: string): MessageRuntime;

/**
* @deprecated This API is still under active development and might change without notice.
*/
stopSpeaking: () => void;

unstable_on(
event: "switched-to" | "run-start",
callback: () => void,
Expand Down Expand Up @@ -206,7 +216,9 @@ export type ThreadRuntime = {
beginEdit: (messageId: string) => void;
};

export class ThreadRuntimeImpl implements ThreadRuntimeCore, ThreadRuntime {
export class ThreadRuntimeImpl
implements Omit<ThreadRuntimeCore, "getMessageById">, ThreadRuntime
{
// public path = "assistant.threads[main]"; // TODO

/**
Expand Down Expand Up @@ -390,12 +402,37 @@ export class ThreadRuntimeImpl implements ThreadRuntimeCore, ThreadRuntime {
public getMesssageByIndex(idx: number) {
if (idx < 0) throw new Error("Message index must be >= 0");

return this._getMessageRuntime(() => {
const messages = this._threadBinding.getState().messages;
const message = messages[idx];
if (!message) return undefined;
return {
message,
parentId: messages[idx - 1]?.id ?? null,
};
});
}

public getMesssageById(messageId: string) {
return this._getMessageRuntime(() =>
this._threadBinding.getState().getMessageById(messageId),
);
}

private _getMessageRuntime(
callback: () =>
| { parentId: string | null; message: ThreadMessage }
| undefined,
) {
return new MessageRuntimeImpl(
new ShallowMemoizeSubject({
getState: () => {
const { messages, speech: speechState } = this.getState();
const message = messages[idx];
if (!message) return SKIP_UPDATE;
const { message, parentId } = callback() ?? {};

const { messages, speech: speechState } =
this._threadBinding.getState();

if (!message || !parentId) return SKIP_UPDATE;

const thread = this._threadBinding.getState();

Expand All @@ -406,8 +443,8 @@ export class ThreadRuntimeImpl implements ThreadRuntimeCore, ThreadRuntime {
...message,

message,
isLast: idx === messages.length - 1,
parentId: messages[idx - 1]?.id ?? null,
isLast: messages.at(-1)?.id === message.id,
parentId,

branches,
branchNumber: branches.indexOf(message.id) + 1,
Expand Down
7 changes: 7 additions & 0 deletions packages/react/src/runtimes/core/ThreadRuntimeCore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ export type SubmittedFeedback = Readonly<{
}>;

export type ThreadRuntimeCore = Readonly<{
getMessageById: (messageId: string) =>
| {
parentId: string | null;
message: ThreadMessage;
}
| undefined;

getBranches: (messageId: string) => readonly string[];
switchToBranch: (branchId: string) => void;

Expand Down

0 comments on commit 7c76939

Please sign in to comment.