Skip to content

Commit

Permalink
fix: TextContentPartProvider should support contentPartRuntime.getSta…
Browse files Browse the repository at this point in the history
…te() (#1187)
  • Loading branch information
Yonom authored Nov 20, 2024
1 parent 61c4d79 commit 09a2a38
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-apes-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react": patch
---

fix: TextContentPartProvider should support contentPartRuntime.getState()
17 changes: 12 additions & 5 deletions packages/react/src/api/ContentPartRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,31 @@ export class ContentPartRuntimeImpl implements ContentPartRuntime {

constructor(
private contentBinding: ContentPartSnapshotBinding,
private messageApi: MessageStateBinding,
private threadApi: ThreadRuntimeCoreBinding,
private messageApi?: MessageStateBinding,
private threadApi?: ThreadRuntimeCoreBinding,
) {}

public getState() {
console.log(this.contentBinding);
return this.contentBinding.getState();
}

public addToolResult(result: any) {
const message = this.messageApi.getState();
if (!message) throw new Error("Message is not available");

const state = this.contentBinding.getState();
if (!state) throw new Error("Content part is not available");

if (state.type !== "tool-call")
throw new Error("Tried to add tool result to non-tool content part");

if (!this.messageApi)
throw new Error(
"Message API is not available. This is likely a bug in assistant-ui.",
);
if (!this.threadApi) throw new Error("Thread API is not available");

const message = this.messageApi.getState();
if (!message) throw new Error("Message is not available");

const toolName = state.toolName;
const toolCallId = state.toolCallId;

Expand Down
18 changes: 14 additions & 4 deletions packages/react/src/context/providers/TextContentPartProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,26 @@ export const TextContentPartProvider: FC<TextContentPartProvider.Props> = ({
isRunning,
}) => {
const [context] = useState<ContentPartContextValue>(() => {
const useContentPartRuntime = create(
// TODO
() => new ContentPartRuntimeImpl(null as any, null as any, null as any),
);
const useContentPart = create<ContentPartState>(() => ({
status: isRunning ? RUNNING_STATUS : COMPLETE_STATUS,
type: "text",
text,
}));

const useContentPartRuntime = create(
() =>
new ContentPartRuntimeImpl({
path: {
ref: "text",
threadSelector: { type: "main" },
messageSelector: { type: "messageId", messageId: "" },
contentPartSelector: { type: "index", index: 0 },
},
getState: useContentPart.getState,
subscribe: useContentPart.subscribe,
}),
);

return { useContentPartRuntime, useContentPart };
});

Expand Down

0 comments on commit 09a2a38

Please sign in to comment.