Skip to content

Commit

Permalink
feat: core message types (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Jul 9, 2024
1 parent d860ead commit 5a54b8c
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 13 deletions.
2 changes: 2 additions & 0 deletions packages/react/src/context/providers/AssistantProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";

import type { FC, PropsWithChildren } from "react";
import { useEffect, useInsertionEffect, useRef, useState } from "react";
import type { AssistantRuntime } from "../../runtime";
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/context/react/AssistantContext.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";

import { createContext, useContext } from "react";
import type { AssistantModelConfigState } from "../stores/AssistantModelConfig";
import type { AssistantToolUIsState } from "../stores/AssistantToolUIs";
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/context/react/ContentPartContext.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";

import { createContext, useContext } from "react";
import type { ContentPartState } from "../stores/ContentPart";
import { ReadonlyStore } from "../ReadonlyStore";
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/context/react/MessageContext.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";

import { createContext, useContext } from "react";
import type { MessageState } from "../stores/Message";
import type { EditComposerState } from "../stores/EditComposer";
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/context/react/ThreadContext.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";

import { createContext, useContext } from "react";
import type { ComposerState } from "../stores/Composer";
import type { ThreadState } from "../stores/Thread";
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/runtime/local/ChatModelAdapter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ export type ChatModelRunOptions = {
};

export type ChatModelAdapter = {
run: (options: ChatModelRunOptions) => Promise<ChatModelRunResult>;
run: (options: ChatModelRunOptions) => Promise<ChatModelRunResult | void>;
};
9 changes: 6 additions & 3 deletions packages/react/src/runtime/local/LocalRuntime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/runtime/utils/MessageRepository.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ThreadMessage } from "../../types/AssistantTypes";
import { generateOptimisticId } from "./idUtils";
import { generateOptimisticId } from "../../utils/idUtils";

type RepositoryParent = {
children: string[];
Expand Down
52 changes: 44 additions & 8 deletions packages/react/src/types/AssistantTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,62 @@ 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;
role: "user";
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;
7 changes: 7 additions & 0 deletions packages/react/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ export type {
ImageContentPart,
ToolCallContentPart,
UIContentPart,

// core message types
CoreUserContentPart,
CoreAssistantContentPart,
CoreUserMessage,
CoreAssistantMessage,
CoreThreadMessage,
} from "./AssistantTypes";

export type {
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/utils/combined/createCombinedStore.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";

import { useSyncExternalStore } from "react";
import type { Unsubscribe } from "../../types/Unsubscribe";
import { ReadonlyStore } from "../../context/ReadonlyStore";
Expand Down
File renamed without changes.

0 comments on commit 5a54b8c

Please sign in to comment.