Skip to content

Commit

Permalink
cr fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom committed Dec 7, 2024
1 parent 1e37205 commit dd1af84
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 20 deletions.
26 changes: 17 additions & 9 deletions packages/react/src/api/ThreadListItemRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ export type ThreadListItemEventType = "switched-to" | "switched-away";
export type ThreadListItemState = {
readonly isMain: boolean;

readonly id: string;

/**
* @deprecated This field was renamed to `id`. This field will be removed in 0.8.0.
*/
readonly threadId: string;

readonly state: "archived" | "regular" | "new" | "deleted";
readonly title?: string | undefined;
};
Expand Down Expand Up @@ -52,40 +58,42 @@ export class ThreadListItemRuntimeImpl implements ThreadListItemRuntime {

public switchTo(): Promise<void> {
const state = this._core.getState();
return this._threadListBinding.switchToThread(state.threadId);
return this._threadListBinding.switchToThread(state.id);
}

public rename(newTitle: string): Promise<void> {
const state = this._core.getState();

return this._threadListBinding.rename(state.threadId, newTitle);
return this._threadListBinding.rename(state.id, newTitle);
}

public archive(): Promise<void> {
const state = this._core.getState();

return this._threadListBinding.archive(state.threadId);
return this._threadListBinding.archive(state.id);
}

public unarchive(): Promise<void> {
const state = this._core.getState();

return this._threadListBinding.unarchive(state.threadId);
return this._threadListBinding.unarchive(state.id);
}

public delete(): Promise<void> {
const state = this._core.getState();

return this._threadListBinding.delete(state.threadId);
return this._threadListBinding.delete(state.id);
}

public unstable_on(event: ThreadListItemEventType, callback: () => void) {
const isMain = this._core.getState().isMain;
let prevIsMain = this._core.getState().isMain;
return this.subscribe(() => {
const newIsMain = this._core.getState().isMain;
if (isMain === newIsMain) return;
if (event === "switched-to" && newIsMain) return;
if (event === "switched-away" && !newIsMain) return;
if (prevIsMain === newIsMain) return;
prevIsMain = newIsMain;

if (event === "switched-to" && !newIsMain) return;
if (event === "switched-away" && newIsMain) return;
callback();
});
}
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/api/ThreadListRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ const getThreadListItemState = (
const threadData = threadList.getItemById(threadId);
if (!threadData) return SKIP_UPDATE;
return {
threadId: threadData.threadId,
id: threadData.threadId,
threadId: threadData.threadId, // TODO remove in 0.8.0
title: threadData.title,
state: threadData.state,
isMain: threadData.threadId === threadList.mainThreadId,
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/api/ThreadRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export type ThreadListItemRuntimeBinding = SubscribableWithState<
export type ThreadState = {
/**
* The thread ID.
* @deprecated This field is deprecated and will be removed in 0.8.0. Use `metadata.threadId` instead.
* @deprecated This field is deprecated and will be removed in 0.8.0. Use `useThreadListItem().id` instead.
*/
readonly threadId: string;

Expand Down Expand Up @@ -139,7 +139,7 @@ export const getThreadState = (
): ThreadState => {
const lastMessage = runtime.messages.at(-1);
return Object.freeze({
threadId: threadListItemState.threadId,
threadId: threadListItemState.id,
metadata: threadListItemState,
capabilities: runtime.capabilities,
isDisabled: runtime.isDisabled,
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/runtimes/core/ThreadListRuntimeCore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type ThreadListRuntimeCore = {

// getLoadThreadsPromise(): Promise<void>;
// getLoadArchivedThreadsPromise(): Promise<void>;
// create(): Promise<ThreadListItemCoreState>;

rename(threadId: string, newTitle: string): Promise<void>;
archive(threadId: string): Promise<void>;
unarchive(threadId: string): Promise<void>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ export class LocalThreadListRuntimeCore implements ThreadListRuntimeCore {
);
break;

default:
default: {
const _exhaustiveCheck: never = lastState;
throw new Error(`Unsupported state: ${_exhaustiveCheck}`);
}
}

// newState
Expand All @@ -143,9 +144,10 @@ export class LocalThreadListRuntimeCore implements ThreadListRuntimeCore {
data.dispose();
break;

default:
default: {
const _exhaustiveCheck: never = newState;
throw new Error(`Unsupported state: ${_exhaustiveCheck}`);
}
}

if (newState !== "deleted") {
Expand Down
11 changes: 6 additions & 5 deletions packages/react/src/runtimes/local/LocalThreadRuntimeCore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,15 @@ export class LocalThreadRuntimeCore
if (hasUpdates) this._notifySubscribers();
}

public async append(message: AppendMessage): Promise<void> {
private _ensureInitialized() {
if (!this._isInitialized) {
this._isInitialized = true;
this._notifyEventSubscribers("initialize");
}
}

public async append(message: AppendMessage): Promise<void> {
this._ensureInitialized();

const newMessage = fromCoreMessage(message, {
attachments: message.attachments,
Expand All @@ -104,10 +108,7 @@ export class LocalThreadRuntimeCore
}

public async startRun(parentId: string | null): Promise<void> {
if (!this._isInitialized) {
this._isInitialized = true;
this._notifyEventSubscribers("initialize");
}
this._ensureInitialized();

this.repository.resetHead(parentId);

Expand Down

0 comments on commit dd1af84

Please sign in to comment.