Skip to content

Commit

Permalink
refactor: mark runtime fields as readonly (#1146)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Nov 11, 2024
1 parent 284a13f commit c2f75e5
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 124 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-tips-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react": patch
---

feat: ThreadListRuntime API types
4 changes: 2 additions & 2 deletions packages/react/src/api/AssistantRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ export type AssistantRuntime = {
/**
* The currently selected main thread.
*/
thread: ThreadRuntime;
readonly thread: ThreadRuntime;

/**
* The thread manager, to rename, archive and delete threads.
*/
threadList: ThreadListRuntime;
readonly threadList: ThreadListRuntime;

/**
* Switch to a new thread.
Expand Down
8 changes: 4 additions & 4 deletions packages/react/src/api/AttachmentRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import {
import { AttachmentRuntimePath } from "./RuntimePathTypes";

type MessageAttachmentState = CompleteAttachment & {
source: "message";
readonly source: "message";
};

type ThreadComposerAttachmentState = PendingAttachment & {
source: "thread-composer";
readonly source: "thread-composer";
};

type EditComposerAttachmentState = Attachment & {
source: "edit-composer";
readonly source: "edit-composer";
};

export type AttachmentState =
Expand All @@ -37,7 +37,7 @@ type AttachmentRuntimeSource = AttachmentState["source"];
export type AttachmentRuntime<
TSource extends AttachmentRuntimeSource = AttachmentRuntimeSource,
> = {
path: AttachmentRuntimePath & { attachmentSource: TSource };
readonly path: AttachmentRuntimePath & { attachmentSource: TSource };
readonly source: TSource;
getState(): AttachmentState & { source: TSource };
remove(): Promise<void>;
Expand Down
20 changes: 10 additions & 10 deletions packages/react/src/api/ComposerRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@ export type ComposerRuntimeCoreBinding = SubscribableWithState<
>;

type BaseComposerState = {
text: string;
role: MessageRole;
attachments: readonly Attachment[];
readonly text: string;
readonly role: MessageRole;
readonly attachments: readonly Attachment[];

canCancel: boolean;
isEditing: boolean;
isEmpty: boolean;
readonly canCancel: boolean;
readonly isEditing: boolean;
readonly isEmpty: boolean;
};

export type ThreadComposerState = BaseComposerState & {
type: "thread";
readonly type: "thread";

attachments: readonly PendingAttachment[];
readonly attachments: readonly PendingAttachment[];
};

export type EditComposerState = BaseComposerState & {
type: "edit";
readonly type: "edit";
};

export type ComposerState = ThreadComposerState | EditComposerState;
Expand Down Expand Up @@ -90,7 +90,7 @@ const getEditComposerState = (
};

export type ComposerRuntime = {
path: ComposerRuntimePath;
readonly path: ComposerRuntimePath;
readonly type: "edit" | "thread";
getState(): ComposerState;

Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/api/ContentPartRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type ContentPartState = (
| ThreadUserContentPart
| ThreadAssistantContentPart
) & {
status: ContentPartStatus | ToolCallContentPartStatus;
readonly status: ContentPartStatus | ToolCallContentPartStatus;
};

type ContentPartSnapshotBinding = SubscribableWithState<
Expand All @@ -23,7 +23,7 @@ type ContentPartSnapshotBinding = SubscribableWithState<
>;

export type ContentPartRuntime = {
path: ContentPartRuntimePath;
readonly path: ContentPartRuntimePath;

getState(): ContentPartState;
addToolResult(result: any): void;
Expand Down
18 changes: 9 additions & 9 deletions packages/react/src/api/MessageRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ import { SKIP_UPDATE } from "./subscribable/SKIP_UPDATE";
import { ShallowMemoizeSubject } from "./subscribable/ShallowMemoizeSubject";
import { SubscribableWithState } from "./subscribable/Subscribable";

const COMPLETE_STATUS: ContentPartStatus = {
const COMPLETE_STATUS: ContentPartStatus = Object.freeze({
type: "complete",
};
});

export const toContentPartStatus = (
message: ThreadMessage,
Expand Down Expand Up @@ -92,17 +92,17 @@ const getContentPartState = (
};

export type MessageState = ThreadMessage & {
parentId: string | null;
isLast: boolean;
readonly parentId: string | null;
readonly isLast: boolean;

branchNumber: number;
branchCount: number;
readonly branchNumber: number;
readonly branchCount: number;

/**
* @deprecated This API is still under active development and might change without notice.
*/
speech: SpeechState | undefined;
submittedFeedback: SubmittedFeedback | undefined;
readonly speech: SpeechState | undefined;
readonly submittedFeedback: SubmittedFeedback | undefined;
};

export type MessageStateBinding = SubscribableWithState<
Expand Down Expand Up @@ -169,7 +169,7 @@ export class MessageRuntimeImpl implements MessageRuntime {
);
}

public composer;
public readonly composer;

public getState() {
return this._core.getState();
Expand Down
52 changes: 26 additions & 26 deletions packages/react/src/api/RuntimePathTypes.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
export type ThreadListRuntimePath = {
ref: string;
readonly ref: string;
};

export type ThreadListItemRuntimePath = {
ref: string;
threadSelector:
| { type: "main" }
| { type: "index"; index: number }
| { type: "archiveIndex"; index: number }
| { type: "threadId"; threadId: string };
readonly ref: string;
readonly threadSelector:
| { readonly type: "main" }
| { readonly type: "index"; readonly index: number }
| { readonly type: "archiveIndex"; readonly index: number }
| { readonly type: "threadId"; readonly threadId: string };
};

export type ThreadRuntimePath = {
ref: string;
threadSelector: { type: "main" };
readonly ref: string;
readonly threadSelector: { type: "main" };
};

export type MessageRuntimePath = ThreadRuntimePath & {
messageSelector:
| { type: "messageId"; messageId: string }
| { type: "index"; index: number };
readonly messageSelector:
| { readonly type: "messageId"; readonly messageId: string }
| { readonly type: "index"; readonly index: number };
};

export type ContentPartRuntimePath = MessageRuntimePath & {
contentPartSelector:
| { type: "index"; index: number }
| { type: "toolCallId"; toolCallId: string };
readonly contentPartSelector:
| { readonly type: "index"; readonly index: number }
| { readonly type: "toolCallId"; readonly toolCallId: string };
};

export type AttachmentRuntimePath = (
| (MessageRuntimePath & {
attachmentSource: "message" | "edit-composer";
readonly attachmentSource: "message" | "edit-composer";
})
| (ThreadRuntimePath & {
attachmentSource: "thread-composer";
readonly attachmentSource: "thread-composer";
})
) & {
attachmentSelector:
readonly attachmentSelector:
| {
type: "index";
index: number;
readonly type: "index";
readonly index: number;
}
| {
type: "index";
index: number;
readonly type: "index";
readonly index: number;
}
| {
type: "index";
index: number;
readonly type: "index";
readonly index: number;
};
};

export type ComposerRuntimePath =
| (ThreadRuntimePath & {
composerSource: "thread";
readonly composerSource: "thread";
})
| (MessageRuntimePath & {
composerSource: "edit";
readonly composerSource: "edit";
});
8 changes: 4 additions & 4 deletions packages/react/src/api/ThreadListItemRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { SubscribableWithState } from "./subscribable/Subscribable";
import { ThreadListRuntimeCoreBinding } from "./ThreadListRuntime";

export type ThreadListItemState = ThreadMetadata & {
isMain: boolean;
readonly isMain: boolean;
};

export type ThreadListItemRuntime = Readonly<{
path: ThreadListItemRuntimePath;
export type ThreadListItemRuntime = {
readonly path: ThreadListItemRuntimePath;
getState(): ThreadListItemState;

switchTo(): Promise<void>;
Expand All @@ -19,7 +19,7 @@ export type ThreadListItemRuntime = Readonly<{
delete(): Promise<void>;

subscribe(callback: () => void): Unsubscribe;
}>;
};

export type ThreadListItemStateBinding = SubscribableWithState<
ThreadListItemState,
Expand Down
20 changes: 10 additions & 10 deletions packages/react/src/api/ThreadListRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import {
import { SKIP_UPDATE } from "./subscribable/SKIP_UPDATE";
import { ShallowMemoizeSubject } from "./subscribable/ShallowMemoizeSubject";

export type ThreadListState = Readonly<{
mainThreadId: string;
newThread: string | undefined;
threads: readonly string[];
archivedThreads: readonly string[];
}>;

export type ThreadListRuntime = Readonly<{
path: ThreadListRuntimePath;
export type ThreadListState = {
readonly mainThreadId: string;
readonly newThread: string | undefined;
readonly threads: readonly string[];
readonly archivedThreads: readonly string[];
};

export type ThreadListRuntime = {
readonly path: ThreadListRuntimePath;
getState(): ThreadListState;

subscribe(callback: () => void): Unsubscribe;

getThreadListItemById(threadId: string): ThreadListItemRuntime;
getThreadListItemByIndex(idx: number): ThreadListItemRuntime;
getThreadListArchivedItemByIndex(idx: number): ThreadListItemRuntime;
}>;
};

const getThreadListState = (
threadList: ThreadListRuntimeCore,
Expand Down
Loading

0 comments on commit c2f75e5

Please sign in to comment.