Skip to content

Commit

Permalink
feat: message custom metadata for all message types (#1154)
Browse files Browse the repository at this point in the history
Yonom authored Nov 11, 2024
1 parent 7fa9a1b commit 1a1f4a5
Showing 7 changed files with 40 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-bottles-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react": patch
---

feat: message metadata for all message types
8 changes: 7 additions & 1 deletion packages/react-playground/src/lib/playground-runtime.ts
Original file line number Diff line number Diff line change
@@ -269,6 +269,7 @@ export class PlaygroundThreadRuntimeCore implements INTERNAL.ThreadRuntimeCore {
role: "assistant",
status: { type: "running" },
content: [],
metadata: { steps: [], custom: {} },
createdAt: new Date(),
};

@@ -281,7 +282,12 @@ export class PlaygroundThreadRuntimeCore implements INTERNAL.ThreadRuntimeCore {
const updateMessage = (m: Partial<ChatModelRunResult>) => {
message = {
...message,
...m,
content: m.content ?? message.content,
status: m.status ?? message.status,
metadata: {
steps: m.metadata?.steps ?? message.metadata?.steps,
custom: m.metadata?.custom ?? message.metadata?.custom,
},
};
this.setMessages([...this.messages.slice(0, -1), message]);
};
Original file line number Diff line number Diff line change
@@ -42,6 +42,8 @@ export const fromCoreMessage = (
return part;
}),
status,

metadata: { steps: [], custom: {} },
} satisfies ThreadMessage;

case "user":
@@ -50,13 +52,15 @@ export const fromCoreMessage = (
role,
content: message.content,
attachments,
metadata: { custom: {} },
} satisfies ThreadMessage;

case "system":
return {
...commonProps,
role,
content: message.content,
metadata: { custom: {} },
} satisfies ThreadMessage;

default: {
Original file line number Diff line number Diff line change
@@ -123,6 +123,9 @@ export const fromThreadMessageLike = (
}
}),
attachments: attachments ?? [],
metadata: {
custom: metadata?.custom ?? {},
},
} satisfies ThreadUserMessage;

case "system":
@@ -135,6 +138,9 @@ export const fromThreadMessageLike = (
...common,
role,
content: content as [TextContentPart],
metadata: {
custom: metadata?.custom ?? {},
},
} satisfies ThreadSystemMessage;

default: {
8 changes: 4 additions & 4 deletions packages/react/src/runtimes/local/ChatModelAdapter.tsx
Original file line number Diff line number Diff line change
@@ -14,11 +14,11 @@ export type ChatModelRunUpdate = {
};

export type ChatModelRunResult = {
content?: ThreadAssistantContentPart[];
status?: MessageStatus;
content?: ThreadAssistantContentPart[] | undefined;
status?: MessageStatus | undefined;
metadata?: {
steps?: ThreadStep[];
custom?: Record<string, unknown>;
steps?: ThreadStep[] | undefined;
custom?: Record<string, unknown> | undefined;
};
};

Original file line number Diff line number Diff line change
@@ -118,6 +118,7 @@ export class LocalThreadRuntimeCore
role: "assistant",
status: { type: "running" },
content: [],
metadata: { steps: [], custom: {} },
createdAt: new Date(),
};

18 changes: 13 additions & 5 deletions packages/react/src/types/AssistantTypes.ts
Original file line number Diff line number Diff line change
@@ -117,22 +117,27 @@ export type MessageStatus =
export type ThreadSystemMessage = MessageCommonProps & {
role: "system";
content: [TextContentPart];
metadata: {
custom: Record<string, unknown>;
};
};

export type ThreadUserMessage = MessageCommonProps & {
role: "user";
content: ThreadUserContentPart[];
attachments: readonly CompleteAttachment[];
// TODO metadata
metadata: {
custom: Record<string, unknown>;
};
};

export type ThreadAssistantMessage = MessageCommonProps & {
role: "assistant";
content: ThreadAssistantContentPart[];
status: MessageStatus;
metadata?: {
steps?: ThreadStep[] | undefined;
custom?: Record<string, unknown> | undefined;
metadata: {
steps: ThreadStep[];
custom: Record<string, unknown>;
};
};

@@ -144,7 +149,10 @@ export type AppendMessage = CoreMessage & {

type BaseThreadMessage = {
status?: ThreadAssistantMessage["status"];
metadata?: ThreadAssistantMessage["metadata"];
metadata: {
steps?: ThreadStep[];
custom: Record<string, unknown>;
};
attachments?: ThreadUserMessage["attachments"];
};

0 comments on commit 1a1f4a5

Please sign in to comment.