Skip to content

Commit

Permalink
feat: ThreadMessageLike metadata support (#1153)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Nov 11, 2024
1 parent 07752a2 commit 7fa9a1b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-ears-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react": patch
---

feat: ThreadMessageLike metadata support
19 changes: 15 additions & 4 deletions packages/react/src/runtimes/external-store/ThreadMessageLike.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from "../../types";
import {
CoreToolCallContentPart,
ThreadStep,
Unstable_AudioContentPart,
} from "../../types/AssistantTypes";

Expand All @@ -33,15 +34,18 @@ export type ThreadMessageLike = {
createdAt?: Date | undefined;
status?: MessageStatus | undefined;
attachments?: CompleteAttachment[] | undefined;
// TODO metadata
metadata?: {
steps?: ThreadStep[] | undefined;
custom?: Record<string, unknown> | undefined;
};
};

export const fromThreadMessageLike = (
like: ThreadMessageLike,
fallbackId: string,
fallbackStatus: MessageStatus,
): ThreadMessage => {
const { role, id, createdAt, attachments, status } = like;
const { role, id, createdAt, attachments, status, metadata } = like;
const common = {
id: id ?? fallbackId,
createdAt: createdAt ?? new Date(),
Expand All @@ -53,10 +57,13 @@ export const fromThreadMessageLike = (
: like.content;

if (role !== "user" && attachments)
throw new Error("Attachments are only supported for user messages");
throw new Error("attachments are only supported for user messages");

if (role !== "assistant" && status)
throw new Error("Status is only supported for assistant messages");
throw new Error("status is only supported for assistant messages");

if (role !== "assistant" && metadata?.steps)
throw new Error("metadata.steps is only supported for assistant messages");

switch (role) {
case "assistant":
Expand Down Expand Up @@ -90,6 +97,10 @@ export const fromThreadMessageLike = (
})
.filter((c) => !!c),
status: status ?? fallbackStatus,
metadata: {
custom: metadata?.custom ?? {},
steps: metadata?.steps ?? [],
},
} satisfies ThreadAssistantMessage;

case "user":
Expand Down

0 comments on commit 7fa9a1b

Please sign in to comment.