Inkspring / app · inkspring.ai / native-mac / Sources / Inkspring / PersonalProfile.swift
| 1 | import Foundation |
| 2 | |
| 3 | enum PersonalProfile { |
| 4 | static let sampleLimit = 16_000 |
| 5 | static let textLimits = ["voice": 2000, "imageStyle": 2000, "interests": 2000, "avoid": 2000, "samples": sampleLimit] |
| 6 | static let sourceNames = ["x", "news", "instagram", "linkedin"] |
| 7 | |
| 8 | static func normalized(_ input: [String: Any]) throws -> [String: Any] { |
| 9 | var result: [String: Any] = [:] |
| 10 | for (key, limit) in textLimits { |
| 11 | let value = input[key] ?? "" |
| 12 | guard let value = value as? String, value.utf16.count <= limit else { |
| 13 | throw WorkspaceError("Profile field \(key) must use up to \(limit) characters.") |
| 14 | } |
| 15 | result[key] = value.trimmingCharacters(in: .whitespacesAndNewlines) |
| 16 | } |
| 17 | guard let tags = (input["hashtags"] ?? [String]()) as? [String], tags.count <= 30, |
| 18 | let sources = (input["researchSources"] ?? ["x", "news"]) as? [String], |
| 19 | sources.allSatisfy(sourceNames.contains) else { |
| 20 | throw WorkspaceError("Choose supported sources and up to 30 hashtags.") |
| 21 | } |
| 22 | var normalizedTags: [String] = [] |
| 23 | for value in tags { |
| 24 | var tag = value.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() |
| 25 | if tag.hasPrefix("#") { tag.removeFirst() } |
| 26 | guard tag.range(of: #"\A[\p{L}\p{N}_]{1,32}\z"#, options: .regularExpression) != nil else { |
| 27 | throw WorkspaceError("Use 1–32 letters, numbers or underscores per hashtag.") |
| 28 | } |
| 29 | if !normalizedTags.contains(tag) { normalizedTags.append(tag) } |
| 30 | } |
| 31 | result["hashtags"] = normalizedTags |
| 32 | result["researchSources"] = sourceNames.filter(sources.contains) |
| 33 | return result |
| 34 | } |
| 35 | |
| 36 | static func publishedDrafts(in records: [WorkspaceRecord]) -> [WorkspaceRecord] { |
| 37 | let published = Set(records.filter { $0.kind == "publication" && $0["status"] == "published" }.map { $0["draftId"] }) |
| 38 | return Array(records.filter { |
| 39 | $0.kind == "draft" && $0.fields["sample"] as? Bool != true && |
| 40 | ($0.scheduleStatus == "posted" || published.contains($0.id)) && |
| 41 | !$0.body.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty |
| 42 | }.prefix(40)) |
| 43 | } |
| 44 | |
| 45 | static func samples(appending drafts: [WorkspaceRecord], to existing: String) throws -> String { |
| 46 | let sections = [existing] + drafts.map { String(decoding: $0.body.utf16.prefix(4000), as: UTF16.self) } |
| 47 | let combined = sections.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) } |
| 48 | .filter { !$0.isEmpty }.joined(separator: "\n\n") |
| 49 | guard combined.utf16.count <= sampleLimit else { |
| 50 | throw WorkspaceError("Choose fewer posts. Writing samples can use up to 16,000 characters.") |
| 51 | } |
| 52 | return combined |
| 53 | } |
| 54 | } |