Implement existing lyrics revision flow
This commit is contained in:
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user