UNPKG

@assistant-ui/react

Version:

Typescript/React library for AI Chat

46 lines 1.64 kB
import { useState } from "react"; import { useThreadListItemRuntime } from "../context"; import { auiV0Decode, auiV0Encode } from "./auiV0"; class AssistantCloudThreadHistoryAdapter { constructor(cloudRef, threadListItemRuntime) { this.cloudRef = cloudRef; this.threadListItemRuntime = threadListItemRuntime; } _getIdForLocalId = {}; async append({ parentId, message }) { const { remoteId } = await this.threadListItemRuntime.initialize(); const task = this.cloudRef.current.threads.messages.create(remoteId, { parent_id: parentId ? await this._getIdForLocalId[parentId] ?? parentId : null, format: "aui/v0", content: auiV0Encode(message) }).then(({ message_id }) => { this._getIdForLocalId[message.id] = message_id; return message_id; }); this._getIdForLocalId[message.id] = task; return task.then(() => { }); } async load() { const remoteId = this.threadListItemRuntime.getState().remoteId; if (!remoteId) return { messages: [] }; const { messages } = await this.cloudRef.current.threads.messages.list(remoteId); const payload = { messages: messages.filter( (m) => m.format === "aui/v0" ).map(auiV0Decode).reverse() }; return payload; } } const useAssistantCloudThreadHistoryAdapter = (cloudRef) => { const threadListItemRuntime = useThreadListItemRuntime(); const [adapter] = useState( () => new AssistantCloudThreadHistoryAdapter(cloudRef, threadListItemRuntime) ); return adapter; }; export { useAssistantCloudThreadHistoryAdapter }; //# sourceMappingURL=AssistantCloudThreadHistoryAdapter.js.map