diff --git a/packages/react/src/context/providers/AssistantProvider.tsx b/packages/react/src/context/providers/AssistantProvider.tsx index a66fcd4a7..58a4e1332 100644 --- a/packages/react/src/context/providers/AssistantProvider.tsx +++ b/packages/react/src/context/providers/AssistantProvider.tsx @@ -1,3 +1,5 @@ +"use client"; + import type { FC, PropsWithChildren } from "react"; import { useEffect, useInsertionEffect, useRef, useState } from "react"; import type { AssistantRuntime } from "../../runtime"; diff --git a/packages/react/src/context/react/AssistantContext.ts b/packages/react/src/context/react/AssistantContext.ts index 498883d13..956ea86b6 100644 --- a/packages/react/src/context/react/AssistantContext.ts +++ b/packages/react/src/context/react/AssistantContext.ts @@ -1,3 +1,5 @@ +"use client"; + import { createContext, useContext } from "react"; import type { AssistantModelConfigState } from "../stores/AssistantModelConfig"; import type { AssistantToolUIsState } from "../stores/AssistantToolUIs"; diff --git a/packages/react/src/context/react/ContentPartContext.ts b/packages/react/src/context/react/ContentPartContext.ts index 23d17f34d..8e98d2c02 100644 --- a/packages/react/src/context/react/ContentPartContext.ts +++ b/packages/react/src/context/react/ContentPartContext.ts @@ -1,3 +1,5 @@ +"use client"; + import { createContext, useContext } from "react"; import type { ContentPartState } from "../stores/ContentPart"; import { ReadonlyStore } from "../ReadonlyStore"; diff --git a/packages/react/src/context/react/MessageContext.ts b/packages/react/src/context/react/MessageContext.ts index fadd73a99..17bb8a534 100644 --- a/packages/react/src/context/react/MessageContext.ts +++ b/packages/react/src/context/react/MessageContext.ts @@ -1,3 +1,5 @@ +"use client"; + import { createContext, useContext } from "react"; import type { MessageState } from "../stores/Message"; import type { EditComposerState } from "../stores/EditComposer"; diff --git a/packages/react/src/context/react/ThreadContext.ts b/packages/react/src/context/react/ThreadContext.ts index 51f7257b7..6c9b11fd2 100644 --- a/packages/react/src/context/react/ThreadContext.ts +++ b/packages/react/src/context/react/ThreadContext.ts @@ -1,3 +1,5 @@ +"use client"; + import { createContext, useContext } from "react"; import type { ComposerState } from "../stores/Composer"; import type { ThreadState } from "../stores/Thread"; diff --git a/packages/react/src/runtime/local/ChatModelAdapter.tsx b/packages/react/src/runtime/local/ChatModelAdapter.tsx index 24cf8dd81..f08c49e4d 100644 --- a/packages/react/src/runtime/local/ChatModelAdapter.tsx +++ b/packages/react/src/runtime/local/ChatModelAdapter.tsx @@ -17,5 +17,5 @@ export type ChatModelRunOptions = { }; export type ChatModelAdapter = { - run: (options: ChatModelRunOptions) => Promise; + run: (options: ChatModelRunOptions) => Promise; }; diff --git a/packages/react/src/runtime/local/LocalRuntime.tsx b/packages/react/src/runtime/local/LocalRuntime.tsx index fc3e72a15..09bd6177b 100644 --- a/packages/react/src/runtime/local/LocalRuntime.tsx +++ b/packages/react/src/runtime/local/LocalRuntime.tsx @@ -10,7 +10,7 @@ import { import type { Unsubscribe } from "../../types/Unsubscribe"; import { ThreadRuntime } from "../core"; import { MessageRepository } from "../utils/MessageRepository"; -import { generateId } from "../utils/idUtils"; +import { generateId } from "../../utils/idUtils"; import { BaseAssistantRuntime } from "../core/BaseAssistantRuntime"; import type { ChatModelAdapter, ChatModelRunResult } from "./ChatModelAdapter"; @@ -128,12 +128,15 @@ class LocalThreadRuntime implements ThreadRuntime { config: mergeModelConfigs(this._configProviders), onUpdate: updateHandler, }); - updateHandler(result); + if (result !== undefined) { + updateHandler(result); + } message.status = "done"; this.repository.addOrUpdateMessage(parentId, { ...message }); } catch (e) { - message.status = "error"; + (message as any).status = "error"; + (message as any).error = e; this.repository.addOrUpdateMessage(parentId, { ...message }); console.error(e); } finally { diff --git a/packages/react/src/runtime/utils/MessageRepository.tsx b/packages/react/src/runtime/utils/MessageRepository.tsx index debbb7e83..845ef8e1f 100644 --- a/packages/react/src/runtime/utils/MessageRepository.tsx +++ b/packages/react/src/runtime/utils/MessageRepository.tsx @@ -1,5 +1,5 @@ import type { ThreadMessage } from "../../types/AssistantTypes"; -import { generateOptimisticId } from "./idUtils"; +import { generateOptimisticId } from "../../utils/idUtils"; type RepositoryParent = { children: string[]; diff --git a/packages/react/src/types/AssistantTypes.ts b/packages/react/src/types/AssistantTypes.ts index 832ddb87b..084e242bb 100644 --- a/packages/react/src/types/AssistantTypes.ts +++ b/packages/react/src/types/AssistantTypes.ts @@ -35,21 +35,36 @@ export type AssistantContentPart = export type AppendContentPart = TextContentPart | ImageContentPart; -export type BaseMessage = { +type MessageCommonProps = { id: string; createdAt: Date; }; -export type UserMessage = BaseMessage & { +type MessageStatusProps = + | { + status: "in_progress" | "done"; + error?: undefined; + } + | { + status: "error"; + error: unknown; + }; + +export type SystemMessage = MessageCommonProps & { + role: "system"; + content: [TextContentPart]; +}; + +export type UserMessage = MessageCommonProps & { role: "user"; content: UserContentPart[]; }; -export type AssistantMessage = BaseMessage & { - role: "assistant"; - content: AssistantContentPart[]; - status: "in_progress" | "done" | "error"; -}; +export type AssistantMessage = MessageCommonProps & + MessageStatusProps & { + role: "assistant"; + content: AssistantContentPart[]; + }; export type AppendMessage = { parentId: string | null; @@ -57,4 +72,25 @@ export type AppendMessage = { content: AppendContentPart[]; }; -export type ThreadMessage = UserMessage | AssistantMessage; +export type ThreadMessage = SystemMessage | UserMessage | AssistantMessage; + +/** Core Message Types (without UI content parts) */ + +export type CoreUserContentPart = TextContentPart | ImageContentPart; +export type CoreAssistantContentPart = TextContentPart | ToolCallContentPart; + +export type CoreUserMessage = MessageCommonProps & { + role: "user"; + content: CoreUserContentPart[]; +}; + +export type CoreAssistantMessage = MessageCommonProps & + MessageStatusProps & { + role: "assistant"; + content: CoreAssistantContentPart[]; + }; + +export type CoreThreadMessage = + | SystemMessage + | CoreUserMessage + | CoreAssistantMessage; diff --git a/packages/react/src/types/index.ts b/packages/react/src/types/index.ts index 906f78b00..e00bd6b44 100644 --- a/packages/react/src/types/index.ts +++ b/packages/react/src/types/index.ts @@ -10,6 +10,13 @@ export type { ImageContentPart, ToolCallContentPart, UIContentPart, + + // core message types + CoreUserContentPart, + CoreAssistantContentPart, + CoreUserMessage, + CoreAssistantMessage, + CoreThreadMessage, } from "./AssistantTypes"; export type { diff --git a/packages/react/src/utils/combined/createCombinedStore.ts b/packages/react/src/utils/combined/createCombinedStore.ts index 82e98cd5a..c22f24f43 100644 --- a/packages/react/src/utils/combined/createCombinedStore.ts +++ b/packages/react/src/utils/combined/createCombinedStore.ts @@ -1,3 +1,5 @@ +"use client"; + import { useSyncExternalStore } from "react"; import type { Unsubscribe } from "../../types/Unsubscribe"; import { ReadonlyStore } from "../../context/ReadonlyStore"; diff --git a/packages/react/src/runtime/utils/idUtils.tsx b/packages/react/src/utils/idUtils.tsx similarity index 100% rename from packages/react/src/runtime/utils/idUtils.tsx rename to packages/react/src/utils/idUtils.tsx