File size: 1,639 Bytes
abcf568 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | import type {
StudioAssistantMessage,
StudioMessagePart,
StudioMessageStore,
StudioPartStore
} from '../../domain/types'
import { replaceMessagePart } from '../../domain/factories'
export class StudioPartSynchronizer {
constructor(
private readonly messageStore: StudioMessageStore,
private readonly partStore: StudioPartStore
) {}
async appendPart(
assistantMessage: StudioAssistantMessage,
part: StudioMessagePart
): Promise<StudioMessagePart> {
await this.partStore.create(part)
const currentMessage = await this.messageStore.getById(assistantMessage.id)
const currentParts = currentMessage && currentMessage.role === 'assistant'
? currentMessage.parts
: assistantMessage.parts
const refreshed = replaceMessagePart(currentParts, part)
const updated = await this.messageStore.updateAssistantMessage(assistantMessage.id, {
parts: refreshed
})
assistantMessage.parts = updated?.parts ?? refreshed
return part
}
async updatePart(partId: string, patch: Partial<StudioMessagePart> | StudioMessagePart): Promise<void> {
const current = await this.partStore.getById(partId)
if (!current) {
return
}
const next = {
...current,
...patch
} as StudioMessagePart
await this.partStore.update(partId, next)
const message = await this.messageStore.getById(current.messageId)
if (!message || message.role !== 'assistant') {
return
}
const refreshed = replaceMessagePart(message.parts, next)
await this.messageStore.updateAssistantMessage(message.id, {
parts: refreshed
})
}
}
|