Skip to content

Commit

Permalink
feat: MessageRuntime.getContentPartByToolCallId (#1013)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Oct 15, 2024
1 parent 7c76939 commit 91d3951
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/shy-eggs-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react": patch
---

feat: MessageRuntime.getContentPartByToolCallId
23 changes: 22 additions & 1 deletion packages/react/src/api/MessageRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ export type MessageRuntime = {
subscribe(callback: () => void): Unsubscribe;

getContentPartByIndex(idx: number): ContentPartRuntime;
getContentPartByToolCallId(toolCallId: string): ContentPartRuntime;

getAttachmentByIndex(idx: number): AttachmentRuntime & { source: "message" };
};

Expand Down Expand Up @@ -243,7 +245,7 @@ export class MessageRuntimeImpl implements MessageRuntime {
}

public getContentPartByIndex(idx: number) {
if (idx < 0) throw new Error("Message index must be >= 0");
if (idx < 0) throw new Error("Content part index must be >= 0");
return new ContentPartRuntimeImpl(
new ShallowMemoizeSubject({
getState: () => {
Expand All @@ -256,6 +258,25 @@ export class MessageRuntimeImpl implements MessageRuntime {
);
}

public getContentPartByToolCallId(toolCallId: string) {
return new ContentPartRuntimeImpl(
new ShallowMemoizeSubject({
getState: () => {
const state = this._core.getState();
const idx = state.content.findIndex(
(part) =>
part.type === "tool-call" && part.toolCallId === toolCallId,
);
if (idx === -1) return SKIP_UPDATE;
return getContentPartState(state, idx);
},
subscribe: (callback) => this._core.subscribe(callback),
}),
this._core,
this._threadBinding,
);
}

public getAttachmentByIndex(idx: number) {
return new MessageAttachmentRuntimeImpl(
new ShallowMemoizeSubject({
Expand Down

0 comments on commit 91d3951

Please sign in to comment.