assistant-ui icon indicating copy to clipboard operation
assistant-ui copied to clipboard

Type ThreadHistoryAdapter has withFormat as optional method, but it is required by useExternalHistory

Open CoolSpot opened this issue 5 months ago • 1 comments

assistant-ui version tag: 0.0.59 (2025/09/11)

ThreadHistoryAdapter is declared as:

export type ThreadHistoryAdapter = {
  load(): Promise<ExportedMessageRepository & { unstable_resume?: boolean }>;
  resume?(
    options: ChatModelRunOptions,
  ): AsyncGenerator<ChatModelRunResult, void, unknown>;
  append(item: ExportedMessageRepositoryItem): Promise<void>;
  withFormat?<TMessage, TStorageFormat>(
    formatAdapter: MessageFormatAdapter<TMessage, TStorageFormat>,
  ): GenericThreadHistoryAdapter<TMessage>;
};

note that the method withFormat is optional.

Then useExternalHistory does this in attempt to load history:

    const loadHistory = async () => {
      setIsLoading(true);
      try {
        const repo = await historyAdapter
          .withFormat?.(storageFormatAdapter)
          .load();
        if (repo && repo.messages.length > 0) {
          const converted = toExportedMessageRepository(toThreadMessages, repo);
          runtimeRef.current.thread.import(converted);

          const tempRepo = new MessageRepository();
          tempRepo.import(converted);
          const messages = tempRepo.getMessages();

          onSetMessagesRef.current(
            messages.map(getExternalStoreMessages<TMessage>).flat(),
          );

          historyIds.current = new Set(
            converted.messages.map((m) => m.message.id),
          );
        }
      } catch (error) {
        console.error("Failed to load message history:", error);
      } finally {
        setIsLoading(false);
      }
    };

Where "historyAdapter.withFormat?.(storageFormatAdapter).load()" means that if historyAdapter doesn't have a withFormat method, then .load() won't be called and whole expression evaluates to "undefined":

Image

CoolSpot avatar Sep 11 '25 21:09 CoolSpot

we could throw an error if the adapter doesnt have support for withFormat, I wonder if this is a better solution

Yonom avatar Sep 14 '25 01:09 Yonom