Implement existing lyrics revision flow

This commit is contained in:
diyaa
2026-09-13 20:51:31 +02:00
parent 5e1d3d7246
commit 1bf84ca049
3 changed files with 224 additions and 1 deletions
@@ -0,0 +1,61 @@
import Foundation
public final class ExistingLyricsRevisionDirector: Sendable {
private let aiService: any AIService
public init(aiService: any AIService) {
self.aiService = aiService
}
public func reviseExistingLyrics(
in project: SongProject,
sourceLyrics: Lyrics? = nil,
instruction: String,
mode: LyricsRevisionMode,
conversation: [AIConversationMessage] = [],
localeIdentifier: String? = nil,
revisedAt: Date = Date()
) async throws -> SongProjectUpdateResult {
guard mode != .writeFromIdea else {
throw ExistingLyricsRevisionDirectorError.unsupportedMode(mode)
}
let trimmedInstruction = instruction.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmedInstruction.isEmpty else {
throw ExistingLyricsRevisionDirectorError.emptyInstruction
}
let lyricsToRevise = sourceLyrics ?? project.lyrics
guard !lyricsToRevise.text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
throw ExistingLyricsRevisionDirectorError.emptySourceLyrics
}
let request = LyricsRevisionRequest(
context: AIRequestContext(
userInstruction: trimmedInstruction,
conversation: conversation,
localeIdentifier: localeIdentifier
),
project: project,
sourceLyrics: lyricsToRevise,
mode: mode
)
let result = try await aiService.reviseLyrics(from: request)
var updatedProject = project
updatedProject.lyrics = result.lyrics
updatedProject.updatedAt = revisedAt
return SongProjectUpdateResult(
project: updatedProject,
changedScopes: [.lyrics],
notes: result.notes
)
}
}
public enum ExistingLyricsRevisionDirectorError: Error, Equatable, Sendable {
case emptyInstruction
case emptySourceLyrics
case unsupportedMode(LyricsRevisionMode)
}