UNPKG

assistant-cloud

Version:

Cloud integration for assistant-ui

35 lines (34 loc) • 1.65 kB
// src/FormattedCloudPersistence.ts /** * Wraps a CloudMessagePersistence with a MessageFormatAdapter's encode and * decode. The persistence parameter is typed structurally, so a caller does * not need to import the class. * * `append` and `update` take an optional run phase that is passed through to * the persistence: `running` opens (on append) or renews (on update) the * message's run lease, and an end reason makes the write the lease's final * write and ends it, the same as for `aui/v0` messages. */ const createFormattedPersistence = (persistence, adapter) => ({ append: async (threadId, item, run) => { const messageId = adapter.getId(item.message); const encoded = adapter.encode(item); return persistence.append(threadId, messageId, item.parentId, adapter.format, encoded, ...run !== void 0 ? [run] : []); }, update: persistence.update ? async (threadId, item, messageId, run) => { const encoded = adapter.encode(item); return persistence.update(threadId, messageId, adapter.format, encoded, ...run !== void 0 ? [run] : []); } : void 0, delete: persistence.delete ? async (threadId, items) => persistence.delete(threadId, items.map((item) => adapter.getId(item.message))) : void 0, load: async (threadId) => { return { messages: (await persistence.load(threadId, adapter.format)).filter((m) => m.format === adapter.format).map((m) => adapter.decode({ id: m.id, parent_id: m.parent_id, format: m.format, content: m.content })).reverse() }; }, isPersisted: (messageId) => persistence.isPersisted(messageId) }); export { createFormattedPersistence }; //# sourceMappingURL=FormattedCloudPersistence.js.map