From 1a1f4a54288f254f4f55bad1d7e79794b3807776 Mon Sep 17 00:00:00 2001 From: Simon Farshid Date: Mon, 11 Nov 2024 14:15:55 -0800 Subject: [PATCH] feat: message custom metadata for all message types (#1154) --- .changeset/polite-bottles-cheer.md | 5 +++++ .../src/lib/playground-runtime.ts | 8 +++++++- .../edge/converters/fromCoreMessage.ts | 4 ++++ .../external-store/ThreadMessageLike.tsx | 6 ++++++ .../src/runtimes/local/ChatModelAdapter.tsx | 8 ++++---- .../runtimes/local/LocalThreadRuntimeCore.tsx | 1 + packages/react/src/types/AssistantTypes.ts | 18 +++++++++++++----- 7 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 .changeset/polite-bottles-cheer.md diff --git a/.changeset/polite-bottles-cheer.md b/.changeset/polite-bottles-cheer.md new file mode 100644 index 0000000000..97eeaabbe5 --- /dev/null +++ b/.changeset/polite-bottles-cheer.md @@ -0,0 +1,5 @@ +--- +"@assistant-ui/react": patch +--- + +feat: message metadata for all message types diff --git a/packages/react-playground/src/lib/playground-runtime.ts b/packages/react-playground/src/lib/playground-runtime.ts index 2af73e78ad..90285ced73 100644 --- a/packages/react-playground/src/lib/playground-runtime.ts +++ b/packages/react-playground/src/lib/playground-runtime.ts @@ -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) => { 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]); }; diff --git a/packages/react/src/runtimes/edge/converters/fromCoreMessage.ts b/packages/react/src/runtimes/edge/converters/fromCoreMessage.ts index e68ba64fd0..77022d482e 100644 --- a/packages/react/src/runtimes/edge/converters/fromCoreMessage.ts +++ b/packages/react/src/runtimes/edge/converters/fromCoreMessage.ts @@ -42,6 +42,8 @@ export const fromCoreMessage = ( return part; }), status, + + metadata: { steps: [], custom: {} }, } satisfies ThreadMessage; case "user": @@ -50,6 +52,7 @@ export const fromCoreMessage = ( role, content: message.content, attachments, + metadata: { custom: {} }, } satisfies ThreadMessage; case "system": @@ -57,6 +60,7 @@ export const fromCoreMessage = ( ...commonProps, role, content: message.content, + metadata: { custom: {} }, } satisfies ThreadMessage; default: { diff --git a/packages/react/src/runtimes/external-store/ThreadMessageLike.tsx b/packages/react/src/runtimes/external-store/ThreadMessageLike.tsx index e7e2db2f4d..b39b1b67e6 100644 --- a/packages/react/src/runtimes/external-store/ThreadMessageLike.tsx +++ b/packages/react/src/runtimes/external-store/ThreadMessageLike.tsx @@ -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: { diff --git a/packages/react/src/runtimes/local/ChatModelAdapter.tsx b/packages/react/src/runtimes/local/ChatModelAdapter.tsx index 7f2773d69b..1fc9e984d7 100644 --- a/packages/react/src/runtimes/local/ChatModelAdapter.tsx +++ b/packages/react/src/runtimes/local/ChatModelAdapter.tsx @@ -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; + steps?: ThreadStep[] | undefined; + custom?: Record | undefined; }; }; diff --git a/packages/react/src/runtimes/local/LocalThreadRuntimeCore.tsx b/packages/react/src/runtimes/local/LocalThreadRuntimeCore.tsx index b8366a2fa8..1f27f2f714 100644 --- a/packages/react/src/runtimes/local/LocalThreadRuntimeCore.tsx +++ b/packages/react/src/runtimes/local/LocalThreadRuntimeCore.tsx @@ -118,6 +118,7 @@ export class LocalThreadRuntimeCore role: "assistant", status: { type: "running" }, content: [], + metadata: { steps: [], custom: {} }, createdAt: new Date(), }; diff --git a/packages/react/src/types/AssistantTypes.ts b/packages/react/src/types/AssistantTypes.ts index 00145d174f..64ab4bac97 100644 --- a/packages/react/src/types/AssistantTypes.ts +++ b/packages/react/src/types/AssistantTypes.ts @@ -117,22 +117,27 @@ export type MessageStatus = export type ThreadSystemMessage = MessageCommonProps & { role: "system"; content: [TextContentPart]; + metadata: { + custom: Record; + }; }; export type ThreadUserMessage = MessageCommonProps & { role: "user"; content: ThreadUserContentPart[]; attachments: readonly CompleteAttachment[]; - // TODO metadata + metadata: { + custom: Record; + }; }; export type ThreadAssistantMessage = MessageCommonProps & { role: "assistant"; content: ThreadAssistantContentPart[]; status: MessageStatus; - metadata?: { - steps?: ThreadStep[] | undefined; - custom?: Record | undefined; + metadata: { + steps: ThreadStep[]; + custom: Record; }; }; @@ -144,7 +149,10 @@ export type AppendMessage = CoreMessage & { type BaseThreadMessage = { status?: ThreadAssistantMessage["status"]; - metadata?: ThreadAssistantMessage["metadata"]; + metadata: { + steps?: ThreadStep[]; + custom: Record; + }; attachments?: ThreadUserMessage["attachments"]; };