Initial commit
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import Foundation
|
||||
|
||||
public struct AppConfiguration: Equatable, Sendable {
|
||||
public let openAIAPIKey: String?
|
||||
public let privateApplicationRules: PrivateApplicationRules?
|
||||
|
||||
public init(
|
||||
openAIAPIKey: String?,
|
||||
privateApplicationRules: PrivateApplicationRules? = nil
|
||||
) {
|
||||
self.openAIAPIKey = openAIAPIKey
|
||||
self.privateApplicationRules = privateApplicationRules
|
||||
}
|
||||
|
||||
public static func development(environment: [String: String] = ProcessInfo.processInfo.environment) -> AppConfiguration {
|
||||
AppConfiguration(
|
||||
openAIAPIKey: environment["MUSIC_ASSISTANT_OPENAI_API_KEY"],
|
||||
privateApplicationRules: PrivateApplicationRules(content: environment["MUSIC_ASSISTANT_PRIVATE_AI_RULES"])
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
public struct ProductBoundarySummary: Equatable, Sendable {
|
||||
public let description: String
|
||||
|
||||
public init(description: String) {
|
||||
self.description = description
|
||||
}
|
||||
|
||||
public static let initial = ProductBoundarySummary(
|
||||
description: "Create and refine song project concepts, then prepare lyrics and style instructions for Suno handoff without starting generation."
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,842 @@
|
||||
import Foundation
|
||||
|
||||
public struct SongProject: Codable, Equatable, Identifiable, Sendable {
|
||||
public let id: String
|
||||
public var title: String
|
||||
public var idea: String
|
||||
public var duration: SongDuration?
|
||||
public var conversationMode: ConversationMode
|
||||
public var languages: [LanguageSelection]
|
||||
public var dialects: [DialectSelection]
|
||||
public var arabicPronunciationSettings: ArabicPronunciationSettings
|
||||
public var genres: [GenreStyle]
|
||||
public var moods: [MoodTag]
|
||||
public var emotionalArc: [EmotionalArcPoint]
|
||||
public var bpm: ManualAutoValue<Int>?
|
||||
public var key: ManualAutoValue<String>?
|
||||
public var scale: ManualAutoValue<String>?
|
||||
public var maqam: ManualAutoValue<String>?
|
||||
public var sections: [SongSection]
|
||||
public var instruments: [InstrumentTrack]
|
||||
public var vocalists: [Vocalist]
|
||||
public var lyrics: Lyrics
|
||||
public var productionDirections: [ProductionDirection]
|
||||
public var sunoOutput: SunoOutput?
|
||||
public var versions: [SongVersion]
|
||||
public let createdAt: Date
|
||||
public var updatedAt: Date
|
||||
|
||||
public init(
|
||||
id: String = UUID().uuidString,
|
||||
title: String,
|
||||
idea: String,
|
||||
duration: SongDuration? = nil,
|
||||
conversationMode: ConversationMode = .auto,
|
||||
languages: [LanguageSelection] = [],
|
||||
dialects: [DialectSelection] = [],
|
||||
arabicPronunciationSettings: ArabicPronunciationSettings = ArabicPronunciationSettings(),
|
||||
genres: [GenreStyle] = [],
|
||||
moods: [MoodTag] = [],
|
||||
emotionalArc: [EmotionalArcPoint] = [],
|
||||
bpm: ManualAutoValue<Int>? = nil,
|
||||
key: ManualAutoValue<String>? = nil,
|
||||
scale: ManualAutoValue<String>? = nil,
|
||||
maqam: ManualAutoValue<String>? = nil,
|
||||
sections: [SongSection] = [],
|
||||
instruments: [InstrumentTrack] = [],
|
||||
vocalists: [Vocalist] = [],
|
||||
lyrics: Lyrics = Lyrics(),
|
||||
productionDirections: [ProductionDirection] = [],
|
||||
sunoOutput: SunoOutput? = nil,
|
||||
versions: [SongVersion] = [],
|
||||
createdAt: Date = Date(),
|
||||
updatedAt: Date = Date()
|
||||
) {
|
||||
self.id = id
|
||||
self.title = title
|
||||
self.idea = idea
|
||||
self.duration = duration
|
||||
self.conversationMode = conversationMode
|
||||
self.languages = languages
|
||||
self.dialects = dialects
|
||||
self.arabicPronunciationSettings = arabicPronunciationSettings
|
||||
self.genres = genres
|
||||
self.moods = moods
|
||||
self.emotionalArc = emotionalArc
|
||||
self.bpm = bpm
|
||||
self.key = key
|
||||
self.scale = scale
|
||||
self.maqam = maqam
|
||||
self.sections = sections
|
||||
self.instruments = instruments
|
||||
self.vocalists = vocalists
|
||||
self.lyrics = lyrics
|
||||
self.productionDirections = productionDirections
|
||||
self.sunoOutput = sunoOutput
|
||||
self.versions = versions
|
||||
self.createdAt = createdAt
|
||||
self.updatedAt = updatedAt
|
||||
}
|
||||
}
|
||||
|
||||
public extension SongProject {
|
||||
var orderedSections: [SongSection] {
|
||||
sections
|
||||
.enumerated()
|
||||
.sorted { left, right in
|
||||
if left.element.order == right.element.order {
|
||||
return left.offset < right.offset
|
||||
}
|
||||
return left.element.order < right.element.order
|
||||
}
|
||||
.map(\.element)
|
||||
}
|
||||
|
||||
var selectedGenreStyleIDs: Set<String> {
|
||||
Set(genres.map(\.id))
|
||||
}
|
||||
|
||||
func isGenreStyleSelected(_ genreStyle: GenreStyle) -> Bool {
|
||||
selectedGenreStyleIDs.contains(genreStyle.id)
|
||||
}
|
||||
|
||||
mutating func selectGenreStyle(_ genreStyle: GenreStyle) {
|
||||
guard !isGenreStyleSelected(genreStyle) else { return }
|
||||
genres.append(genreStyle)
|
||||
}
|
||||
|
||||
mutating func deselectGenreStyle(id: String) {
|
||||
genres.removeAll { $0.id == id }
|
||||
}
|
||||
|
||||
var languageIDs: Set<String> {
|
||||
Set(languages.map(\.id))
|
||||
}
|
||||
|
||||
var dialectIDs: Set<String> {
|
||||
Set(dialects.map(\.id))
|
||||
}
|
||||
|
||||
func language(for languageID: String) -> LanguageSelection? {
|
||||
languages.first { $0.id == languageID }
|
||||
}
|
||||
|
||||
func dialect(for dialectID: String) -> DialectSelection? {
|
||||
dialects.first { $0.id == dialectID }
|
||||
}
|
||||
|
||||
func dialects(forLanguageID languageID: String) -> [DialectSelection] {
|
||||
dialects.filter { $0.languageID == languageID }
|
||||
}
|
||||
|
||||
mutating func upsertLanguage(_ language: LanguageSelection) {
|
||||
if let index = languages.firstIndex(where: { $0.id == language.id }) {
|
||||
languages[index] = language
|
||||
} else {
|
||||
languages.append(language)
|
||||
}
|
||||
}
|
||||
|
||||
mutating func removeLanguage(id languageID: String) {
|
||||
languages.removeAll { $0.id == languageID }
|
||||
dialects.removeAll { $0.languageID == languageID }
|
||||
}
|
||||
|
||||
mutating func upsertDialect(_ dialect: DialectSelection) {
|
||||
if let index = dialects.firstIndex(where: { $0.id == dialect.id }) {
|
||||
dialects[index] = dialect
|
||||
} else {
|
||||
dialects.append(dialect)
|
||||
}
|
||||
}
|
||||
|
||||
mutating func removeDialect(id dialectID: String) {
|
||||
dialects.removeAll { $0.id == dialectID }
|
||||
}
|
||||
|
||||
mutating func setBPMManually(_ value: Int) {
|
||||
bpm = ManualAutoValue(mode: .manual, value: value)
|
||||
}
|
||||
|
||||
mutating func setBPMAutomatic() {
|
||||
bpm = ManualAutoValue(mode: .auto)
|
||||
}
|
||||
|
||||
mutating func setKeyManually(_ value: String) {
|
||||
key = ManualAutoValue(mode: .manual, value: value)
|
||||
}
|
||||
|
||||
mutating func setKeyAutomatic() {
|
||||
key = ManualAutoValue(mode: .auto)
|
||||
}
|
||||
|
||||
mutating func setScaleManually(_ value: String) {
|
||||
scale = ManualAutoValue(mode: .manual, value: value)
|
||||
}
|
||||
|
||||
mutating func setScaleAutomatic() {
|
||||
scale = ManualAutoValue(mode: .auto)
|
||||
}
|
||||
|
||||
mutating func setMaqamManually(_ value: String) {
|
||||
maqam = ManualAutoValue(mode: .manual, value: value)
|
||||
}
|
||||
|
||||
mutating func setMaqamAutomatic() {
|
||||
maqam = ManualAutoValue(mode: .auto)
|
||||
}
|
||||
|
||||
func emotionalArcPoint(for pointID: String) -> EmotionalArcPoint? {
|
||||
emotionalArc.first { $0.id == pointID }
|
||||
}
|
||||
|
||||
func emotionalArcPoints(forSectionID sectionID: String) -> [EmotionalArcPoint] {
|
||||
emotionalArc.filter { $0.sectionID == sectionID }
|
||||
}
|
||||
|
||||
mutating func upsertEmotionalArcPoint(_ point: EmotionalArcPoint) {
|
||||
if let index = emotionalArc.firstIndex(where: { $0.id == point.id }) {
|
||||
emotionalArc[index] = point
|
||||
} else {
|
||||
emotionalArc.append(point)
|
||||
}
|
||||
}
|
||||
|
||||
mutating func removeEmotionalArcPoint(id pointID: String) {
|
||||
emotionalArc.removeAll { $0.id == pointID }
|
||||
}
|
||||
|
||||
func productionDirection(for directionID: String) -> ProductionDirection? {
|
||||
productionDirections.first { $0.id == directionID }
|
||||
}
|
||||
|
||||
func productionDirections(forSectionID sectionID: String) -> [ProductionDirection] {
|
||||
productionDirections.filter { $0.sectionID == sectionID }
|
||||
}
|
||||
|
||||
mutating func upsertProductionDirection(_ direction: ProductionDirection) {
|
||||
if let index = productionDirections.firstIndex(where: { $0.id == direction.id }) {
|
||||
productionDirections[index] = direction
|
||||
} else {
|
||||
productionDirections.append(direction)
|
||||
}
|
||||
}
|
||||
|
||||
mutating func removeProductionDirection(id directionID: String) {
|
||||
productionDirections.removeAll { $0.id == directionID }
|
||||
}
|
||||
|
||||
var selectedInstrumentTracks: [InstrumentTrack] {
|
||||
instruments.filter(\.selected)
|
||||
}
|
||||
|
||||
var selectedInstrumentIDs: Set<String> {
|
||||
Set(selectedInstrumentTracks.map(\.instrumentId))
|
||||
}
|
||||
|
||||
func instrumentTrack(for instrumentId: String) -> InstrumentTrack? {
|
||||
instruments.first { $0.instrumentId == instrumentId }
|
||||
}
|
||||
|
||||
func isInstrumentSelected(id instrumentId: String) -> Bool {
|
||||
instrumentTrack(for: instrumentId)?.selected == true
|
||||
}
|
||||
|
||||
mutating func selectInstrument(_ instrument: InstrumentTrack) {
|
||||
var selectedInstrument = instrument
|
||||
selectedInstrument.selected = true
|
||||
|
||||
if let index = instruments.firstIndex(where: { $0.instrumentId == instrument.instrumentId }) {
|
||||
instruments[index] = selectedInstrument
|
||||
} else {
|
||||
instruments.append(selectedInstrument)
|
||||
}
|
||||
}
|
||||
|
||||
mutating func deselectInstrument(id instrumentId: String) {
|
||||
guard let index = instruments.firstIndex(where: { $0.instrumentId == instrumentId }) else { return }
|
||||
instruments[index].selected = false
|
||||
}
|
||||
|
||||
mutating func configureInstrument(
|
||||
id instrumentId: String,
|
||||
variant: String?,
|
||||
playingStyle: String?,
|
||||
role: String?,
|
||||
autoArrangementEnabled: Bool
|
||||
) {
|
||||
guard let index = instruments.firstIndex(where: { $0.instrumentId == instrumentId }) else { return }
|
||||
instruments[index].variant = variant
|
||||
instruments[index].playingStyle = playingStyle
|
||||
instruments[index].role = role
|
||||
instruments[index].autoArrangementEnabled = autoArrangementEnabled
|
||||
}
|
||||
|
||||
mutating func addInstrumentPlacement(_ placement: InstrumentPlacement, to instrumentId: String) {
|
||||
if let index = instruments.firstIndex(where: { $0.instrumentId == instrumentId }) {
|
||||
instruments[index].placements.append(placement)
|
||||
} else {
|
||||
instruments.append(
|
||||
InstrumentTrack(
|
||||
instrumentId: instrumentId,
|
||||
selected: true,
|
||||
placements: [placement]
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
var vocalistIDs: Set<String> {
|
||||
Set(vocalists.map(\.id))
|
||||
}
|
||||
|
||||
func vocalist(for vocalistID: String) -> Vocalist? {
|
||||
vocalists.first { $0.id == vocalistID }
|
||||
}
|
||||
|
||||
func vocalistsAssigned(toSectionID sectionID: String) -> [Vocalist] {
|
||||
vocalists.filter { $0.assignedSections.contains(sectionID) }
|
||||
}
|
||||
|
||||
mutating func upsertVocalist(_ vocalist: Vocalist) {
|
||||
var normalizedVocalist = vocalist
|
||||
normalizedVocalist.assignedSections = Self.uniqueOrderedIDs(vocalist.assignedSections)
|
||||
|
||||
if let index = vocalists.firstIndex(where: { $0.id == vocalist.id }) {
|
||||
vocalists[index] = normalizedVocalist
|
||||
} else {
|
||||
vocalists.append(normalizedVocalist)
|
||||
}
|
||||
}
|
||||
|
||||
mutating func removeVocalist(id vocalistID: String) {
|
||||
vocalists.removeAll { $0.id == vocalistID }
|
||||
}
|
||||
|
||||
mutating func assignSection(_ sectionID: String, toVocalistID vocalistID: String) {
|
||||
guard let index = vocalists.firstIndex(where: { $0.id == vocalistID }) else { return }
|
||||
guard !vocalists[index].assignedSections.contains(sectionID) else { return }
|
||||
vocalists[index].assignedSections.append(sectionID)
|
||||
}
|
||||
|
||||
mutating func unassignSection(_ sectionID: String, fromVocalistID vocalistID: String) {
|
||||
guard let index = vocalists.firstIndex(where: { $0.id == vocalistID }) else { return }
|
||||
vocalists[index].assignedSections.removeAll { $0 == sectionID }
|
||||
}
|
||||
|
||||
mutating func createVersion(
|
||||
id: String = UUID().uuidString,
|
||||
label: String,
|
||||
createdAt: Date = Date()
|
||||
) -> SongVersion {
|
||||
let version = SongVersion(
|
||||
id: id,
|
||||
label: label,
|
||||
snapshot: SongProjectSnapshot(project: self),
|
||||
createdAt: createdAt
|
||||
)
|
||||
versions.append(version)
|
||||
return version
|
||||
}
|
||||
|
||||
func version(id versionID: String) -> SongVersion? {
|
||||
versions.first { $0.id == versionID }
|
||||
}
|
||||
|
||||
mutating func restoreVersion(id versionID: String, restoredAt: Date = Date()) -> Bool {
|
||||
guard let version = version(id: versionID) else { return false }
|
||||
restoreSnapshot(version.snapshot, updatedAt: restoredAt)
|
||||
return true
|
||||
}
|
||||
|
||||
mutating func restoreSnapshot(_ snapshot: SongProjectSnapshot, updatedAt: Date = Date()) {
|
||||
title = snapshot.title
|
||||
idea = snapshot.idea
|
||||
duration = snapshot.duration
|
||||
conversationMode = snapshot.conversationMode
|
||||
languages = snapshot.languages
|
||||
dialects = snapshot.dialects
|
||||
arabicPronunciationSettings = snapshot.arabicPronunciationSettings
|
||||
genres = snapshot.genres
|
||||
moods = snapshot.moods
|
||||
emotionalArc = snapshot.emotionalArc
|
||||
bpm = snapshot.bpm
|
||||
key = snapshot.key
|
||||
scale = snapshot.scale
|
||||
maqam = snapshot.maqam
|
||||
sections = snapshot.sections
|
||||
instruments = snapshot.instruments
|
||||
vocalists = snapshot.vocalists
|
||||
lyrics = snapshot.lyrics
|
||||
productionDirections = snapshot.productionDirections
|
||||
sunoOutput = snapshot.sunoOutput
|
||||
self.updatedAt = updatedAt
|
||||
}
|
||||
|
||||
mutating func appendSection(_ section: SongSection) {
|
||||
var orderedSection = section
|
||||
orderedSection.order = nextSectionOrder
|
||||
sections.append(orderedSection)
|
||||
}
|
||||
|
||||
mutating func moveSection(id: String, toOrder targetOrder: Int) {
|
||||
var ordered = orderedSections
|
||||
guard let sourceIndex = ordered.firstIndex(where: { $0.id == id }) else { return }
|
||||
|
||||
let section = ordered.remove(at: sourceIndex)
|
||||
let boundedTargetOrder = min(max(targetOrder, 0), ordered.count)
|
||||
ordered.insert(section, at: boundedTargetOrder)
|
||||
sections = Self.sectionsWithNormalizedOrder(ordered)
|
||||
}
|
||||
|
||||
mutating func normalizeSectionOrder() {
|
||||
sections = Self.sectionsWithNormalizedOrder(orderedSections)
|
||||
}
|
||||
|
||||
private var nextSectionOrder: Int {
|
||||
(sections.map(\.order).max() ?? -1) + 1
|
||||
}
|
||||
|
||||
private static func sectionsWithNormalizedOrder(_ sections: [SongSection]) -> [SongSection] {
|
||||
sections.enumerated().map { index, section in
|
||||
var orderedSection = section
|
||||
orderedSection.order = index
|
||||
return orderedSection
|
||||
}
|
||||
}
|
||||
|
||||
private static func uniqueOrderedIDs(_ ids: [String]) -> [String] {
|
||||
var seenIDs = Set<String>()
|
||||
return ids.filter { seenIDs.insert($0).inserted }
|
||||
}
|
||||
}
|
||||
|
||||
public enum ConversationMode: String, Codable, Equatable, Sendable {
|
||||
case auto
|
||||
case discuss
|
||||
}
|
||||
|
||||
public enum ControlMode: String, Codable, Equatable, Sendable {
|
||||
case manual
|
||||
case auto
|
||||
}
|
||||
|
||||
public struct ManualAutoValue<Value: Codable & Equatable & Sendable>: Codable, Equatable, Sendable {
|
||||
public var mode: ControlMode
|
||||
public var value: Value?
|
||||
|
||||
public init(mode: ControlMode, value: Value? = nil) {
|
||||
self.mode = mode
|
||||
self.value = value
|
||||
}
|
||||
}
|
||||
|
||||
public struct SongDuration: Codable, Equatable, Sendable {
|
||||
public var seconds: Int?
|
||||
public var description: String?
|
||||
|
||||
public init(seconds: Int? = nil, description: String? = nil) {
|
||||
self.seconds = seconds
|
||||
self.description = description
|
||||
}
|
||||
}
|
||||
|
||||
public struct LanguageSelection: Codable, Equatable, Identifiable, Sendable {
|
||||
public let id: String
|
||||
public var name: String
|
||||
|
||||
public init(id: String = UUID().uuidString, name: String) {
|
||||
self.id = id
|
||||
self.name = name
|
||||
}
|
||||
}
|
||||
|
||||
public struct DialectSelection: Codable, Equatable, Identifiable, Sendable {
|
||||
public let id: String
|
||||
public var name: String
|
||||
public var languageID: String?
|
||||
|
||||
public init(id: String = UUID().uuidString, name: String, languageID: String? = nil) {
|
||||
self.id = id
|
||||
self.name = name
|
||||
self.languageID = languageID
|
||||
}
|
||||
}
|
||||
|
||||
public struct ArabicPronunciationSettings: Codable, Equatable, Sendable {
|
||||
public var isEnabled: Bool
|
||||
public var diacritizationPolicy: ArabicDiacritizationPolicy
|
||||
public var tanweenPolicy: ArabicTanweenPolicy
|
||||
public var userNotes: String?
|
||||
public var preservedSpellings: [String]
|
||||
|
||||
public init(
|
||||
isEnabled: Bool = false,
|
||||
diacritizationPolicy: ArabicDiacritizationPolicy = .unspecified,
|
||||
tanweenPolicy: ArabicTanweenPolicy = .unspecified,
|
||||
userNotes: String? = nil,
|
||||
preservedSpellings: [String] = []
|
||||
) {
|
||||
self.isEnabled = isEnabled
|
||||
self.diacritizationPolicy = diacritizationPolicy
|
||||
self.tanweenPolicy = tanweenPolicy
|
||||
self.userNotes = userNotes
|
||||
self.preservedSpellings = preservedSpellings
|
||||
}
|
||||
}
|
||||
|
||||
public enum ArabicDiacritizationPolicy: String, Codable, Equatable, Sendable {
|
||||
case unspecified
|
||||
case pronunciationTargeted
|
||||
case fullTashkeel
|
||||
}
|
||||
|
||||
public enum ArabicTanweenPolicy: String, Codable, Equatable, Sendable {
|
||||
case unspecified
|
||||
case preserveUserInput
|
||||
case addWhenPronunciationRequires
|
||||
case removeWhenUnwanted
|
||||
}
|
||||
|
||||
public struct GenreStyle: Codable, Equatable, Identifiable, Sendable {
|
||||
public let id: String
|
||||
public var name: String
|
||||
public var aliases: [String]
|
||||
|
||||
public init(id: String = UUID().uuidString, name: String, aliases: [String] = []) {
|
||||
self.id = id
|
||||
self.name = name
|
||||
self.aliases = aliases
|
||||
}
|
||||
}
|
||||
|
||||
public struct MoodTag: Codable, Equatable, Identifiable, Sendable {
|
||||
public let id: String
|
||||
public var name: String
|
||||
|
||||
public init(id: String = UUID().uuidString, name: String) {
|
||||
self.id = id
|
||||
self.name = name
|
||||
}
|
||||
}
|
||||
|
||||
public struct EmotionalArcPoint: Codable, Equatable, Identifiable, Sendable {
|
||||
public let id: String
|
||||
public var sectionID: String?
|
||||
public var emotion: String
|
||||
public var energy: Int?
|
||||
public var direction: String?
|
||||
|
||||
public init(
|
||||
id: String = UUID().uuidString,
|
||||
sectionID: String? = nil,
|
||||
emotion: String,
|
||||
energy: Int? = nil,
|
||||
direction: String? = nil
|
||||
) {
|
||||
self.id = id
|
||||
self.sectionID = sectionID
|
||||
self.emotion = emotion
|
||||
self.energy = energy
|
||||
self.direction = direction
|
||||
}
|
||||
}
|
||||
|
||||
public struct Lyrics: Codable, Equatable, Sendable {
|
||||
public var text: String
|
||||
public var sectionTexts: [String: String]
|
||||
|
||||
public init(text: String = "", sectionTexts: [String: String] = [:]) {
|
||||
self.text = text
|
||||
self.sectionTexts = sectionTexts
|
||||
}
|
||||
}
|
||||
|
||||
public struct ProductionDirection: Codable, Equatable, Identifiable, Sendable {
|
||||
public let id: String
|
||||
public var sectionID: String?
|
||||
public var text: String
|
||||
|
||||
public init(id: String = UUID().uuidString, sectionID: String? = nil, text: String) {
|
||||
self.id = id
|
||||
self.sectionID = sectionID
|
||||
self.text = text
|
||||
}
|
||||
}
|
||||
|
||||
public struct InstrumentCatalogItem: Codable, Equatable, Identifiable, Sendable {
|
||||
public let id: String
|
||||
public var name: String
|
||||
public var familyCategory: String
|
||||
public var regionOrigin: String?
|
||||
public var aliases: [String]
|
||||
public var searchTerms: [String]
|
||||
|
||||
public init(
|
||||
id: String,
|
||||
name: String,
|
||||
familyCategory: String,
|
||||
regionOrigin: String? = nil,
|
||||
aliases: [String] = [],
|
||||
searchTerms: [String] = []
|
||||
) {
|
||||
self.id = id
|
||||
self.name = name
|
||||
self.familyCategory = familyCategory
|
||||
self.regionOrigin = regionOrigin
|
||||
self.aliases = aliases
|
||||
self.searchTerms = searchTerms
|
||||
}
|
||||
}
|
||||
|
||||
public struct SongSection: Codable, Equatable, Identifiable, Sendable {
|
||||
public let id: String
|
||||
public var type: SongSectionType
|
||||
public var title: String
|
||||
public var order: Int
|
||||
public var startTime: TimeInterval?
|
||||
public var endTime: TimeInterval?
|
||||
public var lyrics: String
|
||||
public var emotion: String?
|
||||
public var energy: Int?
|
||||
public var vocalDirection: String?
|
||||
public var productionDirection: String?
|
||||
|
||||
public init(
|
||||
id: String = UUID().uuidString,
|
||||
type: SongSectionType,
|
||||
title: String,
|
||||
order: Int = 0,
|
||||
startTime: TimeInterval? = nil,
|
||||
endTime: TimeInterval? = nil,
|
||||
lyrics: String = "",
|
||||
emotion: String? = nil,
|
||||
energy: Int? = nil,
|
||||
vocalDirection: String? = nil,
|
||||
productionDirection: String? = nil
|
||||
) {
|
||||
self.id = id
|
||||
self.type = type
|
||||
self.title = title
|
||||
self.order = order
|
||||
self.startTime = startTime
|
||||
self.endTime = endTime
|
||||
self.lyrics = lyrics
|
||||
self.emotion = emotion
|
||||
self.energy = energy
|
||||
self.vocalDirection = vocalDirection
|
||||
self.productionDirection = productionDirection
|
||||
}
|
||||
}
|
||||
|
||||
public enum SongSectionType: String, Codable, Equatable, Sendable {
|
||||
case intro
|
||||
case verse
|
||||
case preChorus
|
||||
case chorus
|
||||
case bridge
|
||||
case outro
|
||||
case custom
|
||||
}
|
||||
|
||||
public struct InstrumentTrack: Codable, Equatable, Sendable {
|
||||
public var instrumentId: String
|
||||
public var selected: Bool
|
||||
public var variant: String?
|
||||
public var playingStyle: String?
|
||||
public var role: String?
|
||||
public var autoArrangementEnabled: Bool
|
||||
public var placements: [InstrumentPlacement]
|
||||
|
||||
public init(
|
||||
instrumentId: String,
|
||||
selected: Bool = true,
|
||||
variant: String? = nil,
|
||||
playingStyle: String? = nil,
|
||||
role: String? = nil,
|
||||
autoArrangementEnabled: Bool = true,
|
||||
placements: [InstrumentPlacement] = []
|
||||
) {
|
||||
self.instrumentId = instrumentId
|
||||
self.selected = selected
|
||||
self.variant = variant
|
||||
self.playingStyle = playingStyle
|
||||
self.role = role
|
||||
self.autoArrangementEnabled = autoArrangementEnabled
|
||||
self.placements = placements
|
||||
}
|
||||
}
|
||||
|
||||
public struct InstrumentPlacement: Codable, Equatable, Sendable {
|
||||
public var sectionId: String?
|
||||
public var startTime: TimeInterval?
|
||||
public var endTime: TimeInterval?
|
||||
public var direction: String?
|
||||
|
||||
public init(
|
||||
sectionId: String? = nil,
|
||||
startTime: TimeInterval? = nil,
|
||||
endTime: TimeInterval? = nil,
|
||||
direction: String? = nil
|
||||
) {
|
||||
self.sectionId = sectionId
|
||||
self.startTime = startTime
|
||||
self.endTime = endTime
|
||||
self.direction = direction
|
||||
}
|
||||
}
|
||||
|
||||
public struct Vocalist: Codable, Equatable, Identifiable, Sendable {
|
||||
public let id: String
|
||||
public var label: String
|
||||
public var voiceType: String?
|
||||
public var genderSelection: String?
|
||||
public var performanceStyle: String?
|
||||
public var assignedSections: [String]
|
||||
|
||||
public init(
|
||||
id: String = UUID().uuidString,
|
||||
label: String,
|
||||
voiceType: String? = nil,
|
||||
genderSelection: String? = nil,
|
||||
performanceStyle: String? = nil,
|
||||
assignedSections: [String] = []
|
||||
) {
|
||||
self.id = id
|
||||
self.label = label
|
||||
self.voiceType = voiceType
|
||||
self.genderSelection = genderSelection
|
||||
self.performanceStyle = performanceStyle
|
||||
self.assignedSections = assignedSections
|
||||
}
|
||||
}
|
||||
|
||||
public struct SunoOutput: Codable, Equatable, Sendable {
|
||||
public var lyricsText: String
|
||||
public var stylePrompt: String
|
||||
public var additionalFields: [String: String]
|
||||
public var generatedAt: Date
|
||||
|
||||
public init(
|
||||
lyricsText: String,
|
||||
stylePrompt: String,
|
||||
additionalFields: [String: String] = [:],
|
||||
generatedAt: Date = Date()
|
||||
) {
|
||||
self.lyricsText = lyricsText
|
||||
self.stylePrompt = stylePrompt
|
||||
self.additionalFields = additionalFields
|
||||
self.generatedAt = generatedAt
|
||||
}
|
||||
}
|
||||
|
||||
public struct SongVersion: Codable, Equatable, Identifiable, Sendable {
|
||||
public let id: String
|
||||
public var label: String
|
||||
public var snapshot: SongProjectSnapshot
|
||||
public let createdAt: Date
|
||||
|
||||
public init(
|
||||
id: String = UUID().uuidString,
|
||||
label: String,
|
||||
snapshot: SongProjectSnapshot,
|
||||
createdAt: Date = Date()
|
||||
) {
|
||||
self.id = id
|
||||
self.label = label
|
||||
self.snapshot = snapshot
|
||||
self.createdAt = createdAt
|
||||
}
|
||||
}
|
||||
|
||||
public struct SongProjectSnapshot: Codable, Equatable, Sendable {
|
||||
public var title: String
|
||||
public var idea: String
|
||||
public var duration: SongDuration?
|
||||
public var conversationMode: ConversationMode
|
||||
public var languages: [LanguageSelection]
|
||||
public var dialects: [DialectSelection]
|
||||
public var arabicPronunciationSettings: ArabicPronunciationSettings
|
||||
public var genres: [GenreStyle]
|
||||
public var moods: [MoodTag]
|
||||
public var emotionalArc: [EmotionalArcPoint]
|
||||
public var bpm: ManualAutoValue<Int>?
|
||||
public var key: ManualAutoValue<String>?
|
||||
public var scale: ManualAutoValue<String>?
|
||||
public var maqam: ManualAutoValue<String>?
|
||||
public var sections: [SongSection]
|
||||
public var instruments: [InstrumentTrack]
|
||||
public var vocalists: [Vocalist]
|
||||
public var lyrics: Lyrics
|
||||
public var productionDirections: [ProductionDirection]
|
||||
public var sunoOutput: SunoOutput?
|
||||
|
||||
public init(
|
||||
title: String,
|
||||
idea: String,
|
||||
duration: SongDuration? = nil,
|
||||
conversationMode: ConversationMode = .auto,
|
||||
languages: [LanguageSelection] = [],
|
||||
dialects: [DialectSelection] = [],
|
||||
arabicPronunciationSettings: ArabicPronunciationSettings = ArabicPronunciationSettings(),
|
||||
genres: [GenreStyle] = [],
|
||||
moods: [MoodTag] = [],
|
||||
emotionalArc: [EmotionalArcPoint] = [],
|
||||
bpm: ManualAutoValue<Int>? = nil,
|
||||
key: ManualAutoValue<String>? = nil,
|
||||
scale: ManualAutoValue<String>? = nil,
|
||||
maqam: ManualAutoValue<String>? = nil,
|
||||
sections: [SongSection] = [],
|
||||
instruments: [InstrumentTrack] = [],
|
||||
vocalists: [Vocalist] = [],
|
||||
lyrics: Lyrics = Lyrics(),
|
||||
productionDirections: [ProductionDirection] = [],
|
||||
sunoOutput: SunoOutput? = nil
|
||||
) {
|
||||
self.title = title
|
||||
self.idea = idea
|
||||
self.duration = duration
|
||||
self.conversationMode = conversationMode
|
||||
self.languages = languages
|
||||
self.dialects = dialects
|
||||
self.arabicPronunciationSettings = arabicPronunciationSettings
|
||||
self.genres = genres
|
||||
self.moods = moods
|
||||
self.emotionalArc = emotionalArc
|
||||
self.bpm = bpm
|
||||
self.key = key
|
||||
self.scale = scale
|
||||
self.maqam = maqam
|
||||
self.sections = sections
|
||||
self.instruments = instruments
|
||||
self.vocalists = vocalists
|
||||
self.lyrics = lyrics
|
||||
self.productionDirections = productionDirections
|
||||
self.sunoOutput = sunoOutput
|
||||
}
|
||||
|
||||
public init(project: SongProject) {
|
||||
self.init(
|
||||
title: project.title,
|
||||
idea: project.idea,
|
||||
duration: project.duration,
|
||||
conversationMode: project.conversationMode,
|
||||
languages: project.languages,
|
||||
dialects: project.dialects,
|
||||
arabicPronunciationSettings: project.arabicPronunciationSettings,
|
||||
genres: project.genres,
|
||||
moods: project.moods,
|
||||
emotionalArc: project.emotionalArc,
|
||||
bpm: project.bpm,
|
||||
key: project.key,
|
||||
scale: project.scale,
|
||||
maqam: project.maqam,
|
||||
sections: project.sections,
|
||||
instruments: project.instruments,
|
||||
vocalists: project.vocalists,
|
||||
lyrics: project.lyrics,
|
||||
productionDirections: project.productionDirections,
|
||||
sunoOutput: project.sunoOutput
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
import Foundation
|
||||
|
||||
public struct OpenAIClientConfiguration: Equatable, Sendable {
|
||||
public var apiKey: String
|
||||
public var endpointURL: URL
|
||||
public var model: String?
|
||||
public var organizationID: String?
|
||||
public var projectID: String?
|
||||
|
||||
public init(
|
||||
apiKey: String,
|
||||
endpointURL: URL,
|
||||
model: String? = nil,
|
||||
organizationID: String? = nil,
|
||||
projectID: String? = nil
|
||||
) {
|
||||
self.apiKey = apiKey
|
||||
self.endpointURL = endpointURL
|
||||
self.model = model
|
||||
self.organizationID = organizationID
|
||||
self.projectID = projectID
|
||||
}
|
||||
}
|
||||
|
||||
public struct OpenAIClientRequest: Equatable, Sendable {
|
||||
public var method: String
|
||||
public var url: URL?
|
||||
public var body: Data
|
||||
public var additionalHeaders: [String: String]
|
||||
|
||||
public init(
|
||||
method: String = "POST",
|
||||
url: URL? = nil,
|
||||
body: Data,
|
||||
additionalHeaders: [String: String] = [:]
|
||||
) {
|
||||
self.method = method
|
||||
self.url = url
|
||||
self.body = body
|
||||
self.additionalHeaders = additionalHeaders
|
||||
}
|
||||
}
|
||||
|
||||
public protocol OpenAIClientAdapter: Sendable {
|
||||
func makeProjectGenerationRequest(
|
||||
_ request: SongProjectGenerationRequest,
|
||||
configuration: OpenAIClientConfiguration
|
||||
) throws -> OpenAIClientRequest
|
||||
|
||||
func decodeProjectGenerationResult(from data: Data) throws -> SongProjectGenerationResult
|
||||
|
||||
func makeLyricsRevisionRequest(
|
||||
_ request: LyricsRevisionRequest,
|
||||
configuration: OpenAIClientConfiguration
|
||||
) throws -> OpenAIClientRequest
|
||||
|
||||
func decodeLyricsRevisionResult(from data: Data) throws -> LyricsRevisionResult
|
||||
|
||||
func makeProjectUpdateRequest(
|
||||
_ request: SongProjectUpdateRequest,
|
||||
configuration: OpenAIClientConfiguration
|
||||
) throws -> OpenAIClientRequest
|
||||
|
||||
func decodeProjectUpdateResult(from data: Data) throws -> SongProjectUpdateResult
|
||||
}
|
||||
|
||||
public protocol OpenAIHTTPTransport: Sendable {
|
||||
func data(for request: URLRequest) async throws -> (Data, HTTPURLResponse)
|
||||
}
|
||||
|
||||
extension URLSession: OpenAIHTTPTransport {
|
||||
public func data(for request: URLRequest) async throws -> (Data, HTTPURLResponse) {
|
||||
let (data, response) = try await data(for: request, delegate: nil)
|
||||
guard let httpResponse = response as? HTTPURLResponse else {
|
||||
throw OpenAIClientError.invalidResponse
|
||||
}
|
||||
return (data, httpResponse)
|
||||
}
|
||||
}
|
||||
|
||||
public final class OpenAIClient: AIService, Sendable {
|
||||
private let configuration: OpenAIClientConfiguration
|
||||
private let adapter: any OpenAIClientAdapter
|
||||
private let transport: any OpenAIHTTPTransport
|
||||
|
||||
public init(
|
||||
configuration: OpenAIClientConfiguration,
|
||||
adapter: any OpenAIClientAdapter,
|
||||
transport: any OpenAIHTTPTransport = URLSession.shared
|
||||
) {
|
||||
self.configuration = configuration
|
||||
self.adapter = adapter
|
||||
self.transport = transport
|
||||
}
|
||||
|
||||
public func generateSongProject(from request: SongProjectGenerationRequest) async throws -> SongProjectGenerationResult {
|
||||
let clientRequest = try adapter.makeProjectGenerationRequest(request, configuration: configuration)
|
||||
let data = try await perform(clientRequest)
|
||||
return try adapter.decodeProjectGenerationResult(from: data)
|
||||
}
|
||||
|
||||
public func reviseLyrics(from request: LyricsRevisionRequest) async throws -> LyricsRevisionResult {
|
||||
let clientRequest = try adapter.makeLyricsRevisionRequest(request, configuration: configuration)
|
||||
let data = try await perform(clientRequest)
|
||||
return try adapter.decodeLyricsRevisionResult(from: data)
|
||||
}
|
||||
|
||||
public func proposeProjectUpdate(from request: SongProjectUpdateRequest) async throws -> SongProjectUpdateResult {
|
||||
let clientRequest = try adapter.makeProjectUpdateRequest(request, configuration: configuration)
|
||||
let data = try await perform(clientRequest)
|
||||
return try adapter.decodeProjectUpdateResult(from: data)
|
||||
}
|
||||
|
||||
private func perform(_ clientRequest: OpenAIClientRequest) async throws -> Data {
|
||||
guard !configuration.apiKey.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
|
||||
throw OpenAIClientError.missingAPIKey
|
||||
}
|
||||
|
||||
var request = URLRequest(url: clientRequest.url ?? configuration.endpointURL)
|
||||
request.httpMethod = clientRequest.method
|
||||
request.httpBody = clientRequest.body
|
||||
request.setValue("Bearer \(configuration.apiKey)", forHTTPHeaderField: "Authorization")
|
||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
|
||||
if let organizationID = configuration.organizationID {
|
||||
request.setValue(organizationID, forHTTPHeaderField: "OpenAI-Organization")
|
||||
}
|
||||
|
||||
if let projectID = configuration.projectID {
|
||||
request.setValue(projectID, forHTTPHeaderField: "OpenAI-Project")
|
||||
}
|
||||
|
||||
for (header, value) in clientRequest.additionalHeaders {
|
||||
request.setValue(value, forHTTPHeaderField: header)
|
||||
}
|
||||
|
||||
let (data, response) = try await transport.data(for: request)
|
||||
guard (200..<300).contains(response.statusCode) else {
|
||||
throw OpenAIClientError.unacceptableStatusCode(response.statusCode)
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
public enum OpenAIClientError: Error, Equatable, Sendable {
|
||||
case missingAPIKey
|
||||
case invalidResponse
|
||||
case unacceptableStatusCode(Int)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
public protocol SunoHandoffService: Sendable {
|
||||
func handOff(_ output: CompiledSunoOutput) async throws
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
import Foundation
|
||||
|
||||
public protocol ProjectStore: Sendable {
|
||||
func create(_ project: SongProject) async throws
|
||||
func save(_ project: SongProject) async throws
|
||||
func open(id: String) async throws -> SongProject
|
||||
func loadProjectIDs() async throws -> [String]
|
||||
func loadProjectList() async throws -> [SongProjectListItem]
|
||||
}
|
||||
|
||||
public struct SongProjectListItem: Codable, Equatable, Identifiable, Sendable {
|
||||
public let id: String
|
||||
public var title: String
|
||||
public var idea: String
|
||||
public var createdAt: Date
|
||||
public var updatedAt: Date
|
||||
|
||||
public init(id: String, title: String, idea: String, createdAt: Date, updatedAt: Date) {
|
||||
self.id = id
|
||||
self.title = title
|
||||
self.idea = idea
|
||||
self.createdAt = createdAt
|
||||
self.updatedAt = updatedAt
|
||||
}
|
||||
|
||||
public init(project: SongProject) {
|
||||
self.init(
|
||||
id: project.id,
|
||||
title: project.title,
|
||||
idea: project.idea,
|
||||
createdAt: project.createdAt,
|
||||
updatedAt: project.updatedAt
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
public actor LocalSongProjectStore: ProjectStore {
|
||||
private let directoryURL: URL
|
||||
private let encoder: JSONEncoder
|
||||
private let decoder: JSONDecoder
|
||||
|
||||
public init(directoryURL: URL) {
|
||||
self.directoryURL = directoryURL
|
||||
self.encoder = JSONEncoder()
|
||||
self.decoder = JSONDecoder()
|
||||
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
|
||||
}
|
||||
|
||||
public func create(_ project: SongProject) async throws {
|
||||
try ensureDirectoryExists()
|
||||
let url = projectURL(for: project.id)
|
||||
guard !FileManager.default.fileExists(atPath: url.path) else {
|
||||
throw LocalSongProjectStoreError.projectAlreadyExists(project.id)
|
||||
}
|
||||
try saveEncoded(project, to: url)
|
||||
}
|
||||
|
||||
public func save(_ project: SongProject) async throws {
|
||||
try ensureDirectoryExists()
|
||||
try saveEncoded(project, to: projectURL(for: project.id))
|
||||
}
|
||||
|
||||
public func open(id: String) async throws -> SongProject {
|
||||
let url = projectURL(for: id)
|
||||
guard FileManager.default.fileExists(atPath: url.path) else {
|
||||
throw LocalSongProjectStoreError.projectNotFound(id)
|
||||
}
|
||||
let data = try Data(contentsOf: url)
|
||||
return try decoder.decode(SongProject.self, from: data)
|
||||
}
|
||||
|
||||
public func loadProjectIDs() async throws -> [String] {
|
||||
try await loadProjectList().map(\.id)
|
||||
}
|
||||
|
||||
public func loadProjectList() async throws -> [SongProjectListItem] {
|
||||
try ensureDirectoryExists()
|
||||
return try projectFileURLs()
|
||||
.map { url in
|
||||
let data = try Data(contentsOf: url)
|
||||
let project = try decoder.decode(SongProject.self, from: data)
|
||||
return SongProjectListItem(project: project)
|
||||
}
|
||||
.sorted { left, right in
|
||||
if left.updatedAt == right.updatedAt {
|
||||
return left.title.localizedStandardCompare(right.title) == .orderedAscending
|
||||
}
|
||||
return left.updatedAt > right.updatedAt
|
||||
}
|
||||
}
|
||||
|
||||
private func ensureDirectoryExists() throws {
|
||||
try FileManager.default.createDirectory(
|
||||
at: directoryURL,
|
||||
withIntermediateDirectories: true
|
||||
)
|
||||
}
|
||||
|
||||
private func saveEncoded(_ project: SongProject, to url: URL) throws {
|
||||
let data = try encoder.encode(project)
|
||||
try data.write(to: url, options: [.atomic])
|
||||
}
|
||||
|
||||
private func projectFileURLs() throws -> [URL] {
|
||||
try FileManager.default.contentsOfDirectory(
|
||||
at: directoryURL,
|
||||
includingPropertiesForKeys: nil
|
||||
)
|
||||
.filter { $0.pathExtension == Self.fileExtension }
|
||||
}
|
||||
|
||||
private func projectURL(for id: String) -> URL {
|
||||
directoryURL
|
||||
.appendingPathComponent(id)
|
||||
.appendingPathExtension(Self.fileExtension)
|
||||
}
|
||||
|
||||
private static let fileExtension = "json"
|
||||
}
|
||||
|
||||
public enum LocalSongProjectStoreError: Error, Equatable, Sendable {
|
||||
case projectAlreadyExists(String)
|
||||
case projectNotFound(String)
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
import Foundation
|
||||
|
||||
public protocol AIService: Sendable {
|
||||
func generateSongProject(from request: SongProjectGenerationRequest) async throws -> SongProjectGenerationResult
|
||||
func reviseLyrics(from request: LyricsRevisionRequest) async throws -> LyricsRevisionResult
|
||||
func proposeProjectUpdate(from request: SongProjectUpdateRequest) async throws -> SongProjectUpdateResult
|
||||
}
|
||||
|
||||
public struct AIConversationMessage: Equatable, Identifiable, Sendable {
|
||||
public let id: String
|
||||
public var role: AIConversationRole
|
||||
public var content: String
|
||||
|
||||
public init(
|
||||
id: String = UUID().uuidString,
|
||||
role: AIConversationRole,
|
||||
content: String
|
||||
) {
|
||||
self.id = id
|
||||
self.role = role
|
||||
self.content = content
|
||||
}
|
||||
}
|
||||
|
||||
public enum AIConversationRole: String, Equatable, Sendable {
|
||||
case user
|
||||
case assistant
|
||||
case system
|
||||
}
|
||||
|
||||
public struct AIRequestContext: Equatable, Sendable {
|
||||
public var userInstruction: String
|
||||
public var conversation: [AIConversationMessage]
|
||||
public var localeIdentifier: String?
|
||||
internal var privateApplicationRules: PrivateApplicationRules?
|
||||
|
||||
public init(
|
||||
userInstruction: String,
|
||||
conversation: [AIConversationMessage] = [],
|
||||
localeIdentifier: String? = nil
|
||||
) {
|
||||
self.userInstruction = userInstruction
|
||||
self.conversation = conversation
|
||||
self.localeIdentifier = localeIdentifier
|
||||
self.privateApplicationRules = nil
|
||||
}
|
||||
|
||||
internal mutating func injectPrivateApplicationRules(_ rules: PrivateApplicationRules?) {
|
||||
privateApplicationRules = rules
|
||||
}
|
||||
}
|
||||
|
||||
public struct SongProjectGenerationRequest: Equatable, Sendable {
|
||||
public var context: AIRequestContext
|
||||
public var seedProject: SongProject?
|
||||
public var discussionMode: ConversationMode
|
||||
|
||||
public init(
|
||||
context: AIRequestContext,
|
||||
seedProject: SongProject? = nil,
|
||||
discussionMode: ConversationMode = .auto
|
||||
) {
|
||||
self.context = context
|
||||
self.seedProject = seedProject
|
||||
self.discussionMode = discussionMode
|
||||
}
|
||||
}
|
||||
|
||||
public struct SongProjectGenerationResult: Equatable, Sendable {
|
||||
public var project: SongProject
|
||||
public var notes: [String]
|
||||
public var followUpQuestions: [String]
|
||||
|
||||
public init(
|
||||
project: SongProject,
|
||||
notes: [String] = [],
|
||||
followUpQuestions: [String] = []
|
||||
) {
|
||||
self.project = project
|
||||
self.notes = notes
|
||||
self.followUpQuestions = followUpQuestions
|
||||
}
|
||||
}
|
||||
|
||||
public struct LyricsRevisionRequest: Equatable, Sendable {
|
||||
public var context: AIRequestContext
|
||||
public var project: SongProject
|
||||
public var sourceLyrics: Lyrics
|
||||
public var mode: LyricsRevisionMode
|
||||
|
||||
public init(
|
||||
context: AIRequestContext,
|
||||
project: SongProject,
|
||||
sourceLyrics: Lyrics,
|
||||
mode: LyricsRevisionMode
|
||||
) {
|
||||
self.context = context
|
||||
self.project = project
|
||||
self.sourceLyrics = sourceLyrics
|
||||
self.mode = mode
|
||||
}
|
||||
}
|
||||
|
||||
public enum LyricsRevisionMode: String, Equatable, Sendable {
|
||||
case writeFromIdea
|
||||
case correct
|
||||
case improve
|
||||
case restructure
|
||||
case complete
|
||||
}
|
||||
|
||||
public struct LyricsRevisionResult: Equatable, Sendable {
|
||||
public var lyrics: Lyrics
|
||||
public var notes: [String]
|
||||
|
||||
public init(lyrics: Lyrics, notes: [String] = []) {
|
||||
self.lyrics = lyrics
|
||||
self.notes = notes
|
||||
}
|
||||
}
|
||||
|
||||
public struct SongProjectUpdateRequest: Equatable, Sendable {
|
||||
public var context: AIRequestContext
|
||||
public var project: SongProject
|
||||
public var allowedScopes: [SongProjectUpdateScope]
|
||||
|
||||
public init(
|
||||
context: AIRequestContext,
|
||||
project: SongProject,
|
||||
allowedScopes: [SongProjectUpdateScope]
|
||||
) {
|
||||
self.context = context
|
||||
self.project = project
|
||||
self.allowedScopes = allowedScopes
|
||||
}
|
||||
}
|
||||
|
||||
public enum SongProjectUpdateScope: String, Codable, Equatable, Sendable {
|
||||
case duration
|
||||
case genres
|
||||
case instruments
|
||||
case vocals
|
||||
case languages
|
||||
case lyrics
|
||||
case structure
|
||||
case arrangement
|
||||
case musicalParameters
|
||||
case emotionalArc
|
||||
case productionDirections
|
||||
case sunoOutput
|
||||
}
|
||||
|
||||
public struct SongProjectUpdateResult: Equatable, Sendable {
|
||||
public var project: SongProject
|
||||
public var changedScopes: [SongProjectUpdateScope]
|
||||
public var notes: [String]
|
||||
public var followUpQuestions: [String]
|
||||
|
||||
public init(
|
||||
project: SongProject,
|
||||
changedScopes: [SongProjectUpdateScope] = [],
|
||||
notes: [String] = [],
|
||||
followUpQuestions: [String] = []
|
||||
) {
|
||||
self.project = project
|
||||
self.changedScopes = changedScopes
|
||||
self.notes = notes
|
||||
self.followUpQuestions = followUpQuestions
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import Foundation
|
||||
|
||||
public struct PrivateApplicationRules: Equatable, Sendable {
|
||||
public let content: String
|
||||
|
||||
public init?(content: String?) {
|
||||
guard let content else { return nil }
|
||||
|
||||
let trimmedContent = content.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard !trimmedContent.isEmpty else { return nil }
|
||||
|
||||
self.content = trimmedContent
|
||||
}
|
||||
}
|
||||
|
||||
public protocol ApplicationRuleProviding: Sendable {
|
||||
func privateApplicationRules() throws -> PrivateApplicationRules?
|
||||
}
|
||||
|
||||
public struct StaticApplicationRuleProvider: ApplicationRuleProviding {
|
||||
private let rules: PrivateApplicationRules?
|
||||
|
||||
public init(rules: PrivateApplicationRules?) {
|
||||
self.rules = rules
|
||||
}
|
||||
|
||||
public func privateApplicationRules() throws -> PrivateApplicationRules? {
|
||||
rules
|
||||
}
|
||||
}
|
||||
|
||||
public final class ApplicationRuleInjectingAIService: AIService, Sendable {
|
||||
private let baseService: any AIService
|
||||
private let ruleProvider: any ApplicationRuleProviding
|
||||
|
||||
public init(
|
||||
baseService: any AIService,
|
||||
ruleProvider: any ApplicationRuleProviding
|
||||
) {
|
||||
self.baseService = baseService
|
||||
self.ruleProvider = ruleProvider
|
||||
}
|
||||
|
||||
public func generateSongProject(from request: SongProjectGenerationRequest) async throws -> SongProjectGenerationResult {
|
||||
try await baseService.generateSongProject(from: requestWithInjectedRules(request))
|
||||
}
|
||||
|
||||
public func reviseLyrics(from request: LyricsRevisionRequest) async throws -> LyricsRevisionResult {
|
||||
try await baseService.reviseLyrics(from: requestWithInjectedRules(request))
|
||||
}
|
||||
|
||||
public func proposeProjectUpdate(from request: SongProjectUpdateRequest) async throws -> SongProjectUpdateResult {
|
||||
try await baseService.proposeProjectUpdate(from: requestWithInjectedRules(request))
|
||||
}
|
||||
|
||||
private func requestWithInjectedRules(_ request: SongProjectGenerationRequest) throws -> SongProjectGenerationRequest {
|
||||
var request = request
|
||||
request.context.injectPrivateApplicationRules(try ruleProvider.privateApplicationRules())
|
||||
return request
|
||||
}
|
||||
|
||||
private func requestWithInjectedRules(_ request: LyricsRevisionRequest) throws -> LyricsRevisionRequest {
|
||||
var request = request
|
||||
request.context.injectPrivateApplicationRules(try ruleProvider.privateApplicationRules())
|
||||
return request
|
||||
}
|
||||
|
||||
private func requestWithInjectedRules(_ request: SongProjectUpdateRequest) throws -> SongProjectUpdateRequest {
|
||||
var request = request
|
||||
request.context.injectPrivateApplicationRules(try ruleProvider.privateApplicationRules())
|
||||
return request
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import Foundation
|
||||
|
||||
public struct StructuredSongProjectUpdateResponse: Codable, Equatable, Sendable {
|
||||
public static let schemaName = "song_project_update_response"
|
||||
|
||||
public var project: SongProjectSnapshot
|
||||
public var changedScopes: [SongProjectUpdateScope]
|
||||
public var notes: [String]
|
||||
public var followUpQuestions: [String]
|
||||
|
||||
public init(
|
||||
project: SongProjectSnapshot,
|
||||
changedScopes: [SongProjectUpdateScope] = [],
|
||||
notes: [String] = [],
|
||||
followUpQuestions: [String] = []
|
||||
) {
|
||||
self.project = project
|
||||
self.changedScopes = changedScopes
|
||||
self.notes = notes
|
||||
self.followUpQuestions = followUpQuestions
|
||||
}
|
||||
|
||||
public func makeUpdateResult(
|
||||
updating existingProject: SongProject,
|
||||
updatedAt: Date = Date()
|
||||
) -> SongProjectUpdateResult {
|
||||
var updatedProject = existingProject
|
||||
updatedProject.restoreSnapshot(project, updatedAt: updatedAt)
|
||||
|
||||
return SongProjectUpdateResult(
|
||||
project: updatedProject,
|
||||
changedScopes: changedScopes,
|
||||
notes: notes,
|
||||
followUpQuestions: followUpQuestions
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
import Foundation
|
||||
|
||||
public struct ProjectAutosavePolicy: Equatable, Sendable {
|
||||
public static let `default` = ProjectAutosavePolicy()
|
||||
|
||||
public var minimumVersionInterval: TimeInterval
|
||||
public var versionLabel: String
|
||||
|
||||
public init(
|
||||
minimumVersionInterval: TimeInterval = 300,
|
||||
versionLabel: String = "Autosave checkpoint"
|
||||
) {
|
||||
self.minimumVersionInterval = minimumVersionInterval
|
||||
self.versionLabel = versionLabel
|
||||
}
|
||||
}
|
||||
|
||||
public struct ProjectAutosaveResult: Equatable, Sendable {
|
||||
public var project: SongProject
|
||||
public var didCreateVersion: Bool
|
||||
|
||||
public init(project: SongProject, didCreateVersion: Bool) {
|
||||
self.project = project
|
||||
self.didCreateVersion = didCreateVersion
|
||||
}
|
||||
}
|
||||
|
||||
public actor ProjectAutosaveService {
|
||||
private let store: any ProjectStore
|
||||
private let policy: ProjectAutosavePolicy
|
||||
|
||||
public init(store: any ProjectStore, policy: ProjectAutosavePolicy = .default) {
|
||||
self.store = store
|
||||
self.policy = policy
|
||||
}
|
||||
|
||||
public func autosave(_ project: SongProject, at savedAt: Date = Date()) async throws -> ProjectAutosaveResult {
|
||||
var autosavedProject = project
|
||||
var didCreateVersion = false
|
||||
|
||||
if shouldCreateAutosaveVersion(for: autosavedProject, at: savedAt) {
|
||||
_ = autosavedProject.createVersion(label: policy.versionLabel, createdAt: savedAt)
|
||||
didCreateVersion = true
|
||||
}
|
||||
|
||||
autosavedProject.updatedAt = savedAt
|
||||
try await store.save(autosavedProject)
|
||||
|
||||
return ProjectAutosaveResult(project: autosavedProject, didCreateVersion: didCreateVersion)
|
||||
}
|
||||
|
||||
private func shouldCreateAutosaveVersion(for project: SongProject, at savedAt: Date) -> Bool {
|
||||
let currentSnapshot = SongProjectSnapshot(project: project)
|
||||
|
||||
guard let latestVersion = project.versions.last else {
|
||||
return true
|
||||
}
|
||||
|
||||
guard latestVersion.snapshot != currentSnapshot else {
|
||||
return false
|
||||
}
|
||||
|
||||
return savedAt.timeIntervalSince(latestVersion.createdAt) >= policy.minimumVersionInterval
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
public protocol PromptCompiling: Sendable {
|
||||
associatedtype Project
|
||||
|
||||
func compile(project: Project) throws -> CompiledSunoOutput
|
||||
}
|
||||
|
||||
public struct CompiledSunoOutput: Equatable, Sendable {
|
||||
public let lyricsText: String
|
||||
public let stylePrompt: String
|
||||
|
||||
public init(lyricsText: String, stylePrompt: String) {
|
||||
self.lyricsText = lyricsText
|
||||
self.stylePrompt = stylePrompt
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user