Branchwell

Inkspring / app · inkspring.ai / native-mac / Sources / Inkspring / PostIdeaWriting.swift

Raw 3.2 KB on 332c2458022ae8f6648ea05f3c733b528a595172
1import Foundation
2import FoundationModels
3
4struct PostIdeaText: Hashable {
5 let title: String
6 let body: String
7
8 init(title: String, body: String) {
9 self.title = title.trimmingCharacters(in: .whitespacesAndNewlines)
10 self.body = body.trimmingCharacters(in: .whitespacesAndNewlines)
11 }
12
13 var isEmpty: Bool { title.isEmpty && body.isEmpty }
14
15 var canSaveOnMobile: Bool {
16 (1...140).contains(title.utf16.count) && (1...2000).contains(body.utf16.count)
17 }
18}
19
20enum PostIdeaWriting {
21 static let shapingInstructions = """
22 Treat the author's working title and notes as source material. Rewrite their actual point as an editable post idea in their language and voice.
23 Each idea body must stand on its own. Carry over the central claim or question, the concrete details that make it specific \
24 (such as who did what and to whom), and the author's reaction or conclusion. Put those details in the body itself.
25 A new angle can change the hook, emphasis, or structure while keeping the same message. Write the idea itself in the author's voice, \
26 rather than telling the author to explore a broad theme or offering advice they didn't ask for.
27 Clean up obvious dictation errors. Match the source's level of certainty, attribution, and point of view. \
28 Every statement in the rewrite should correspond to something the author said. Omit any new cause, outcome, speculation, or reaction.
29 """
30
31 static let draftingInstructions = """
32 Carry the approved idea's central message, relevant concrete details, and the author's stance into the draft itself. \
33 Adapt wording and structure for the format while keeping the author's meaning and level of certainty.
34 """
35
36 @available(macOS 26, iOS 26, *)
37 static func onDeviceSuggestions(for source: PostIdeaText) async throws -> [PostIdeaText] {
38 let session = LanguageModelSession(instructions: """
39 \(shapingInstructions)
40 Write three concise rewrites of the same idea. Use a different opening in each. Stop once you've restated the author's point.
41 """)
42 let response = try await session.respond(to: """
43 Working title:
44 \(source.title)
45
46 Author's notes:
47 \(source.body)
48 """, generating: OnDeviceIdeas.self, options: GenerationOptions(temperature: 0.25))
49 var suggestions: [PostIdeaText] = []
50 for idea in response.content.ideas {
51 let suggestion = PostIdeaText(title: idea.title, body: idea.body)
52 guard suggestion.canSaveOnMobile, !suggestions.contains(suggestion) else { continue }
53 suggestions.append(suggestion)
54 if suggestions.count == 3 { break }
55 }
56 return suggestions
57 }
58}
59
60@available(macOS 26, iOS 26, *)
61@Generable
62private struct OnDeviceIdeas {
63 @Guide(description: "Three rewrites of the same post idea with different openings.")
64 var ideas: [Idea]
65
66 @Generable
67 struct Idea {
68 @Guide(description: "Specific working title under 140 characters.")
69 var title: String
70 @Guide(description: "One short paragraph with the author's point, concrete details, any uncertainty, and the author's own reaction.")
71 var body: String
72 }
73}