Inkspring / app · inkspring.ai / native-mac / Sources / Inkspring / WorkspacePlans.swift
| 1 | import Foundation |
| 2 | import SwiftUI |
| 3 | import InkspringSchedule |
| 4 | |
| 5 | extension Workspace { |
| 6 | @discardableResult |
| 7 | func savePlan(itemID: String, markdown: String, base: String) throws -> WorkspaceRecord { |
| 8 | try ContentPlan.validate(markdown) |
| 9 | return try withChanges { |
| 10 | guard var current = records.first(where: { $0.id == itemID && ["draft", "idea", "newsletter", "script", "article"].contains($0.kind) }) else { |
| 11 | throw WorkspaceError("This content is unavailable. Restore it from Trash before editing its plan.") |
| 12 | } |
| 13 | guard current["planMarkdown"] == base else { throw ContentPlanError.conflict } |
| 14 | current["previousPlanMarkdown"] = current["planMarkdown"] |
| 15 | current["planMarkdown"] = markdown |
| 16 | return try save(current) |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | func applyContentOperation(_ change: CompanionLibrary.Change, requestID: String) throws -> String? { |
| 21 | guard let operation = change.contentOperation else { return "Unsupported content operation." } |
| 22 | switch operation { |
| 23 | case .savePlan: |
| 24 | do { try savePlan(itemID: change.itemID, markdown: change.text, base: change.basePlan ?? "") } |
| 25 | catch { return error.localizedDescription } |
| 26 | case .createProject, .createDraft, .createNewsletter: |
| 27 | if (records + trashedRecords).contains(where: { $0.id == requestID }) || |
| 28 | (settings["libraryDeletedIDs"] as? [String] ?? []).contains(requestID) { return nil } |
| 29 | if operation != .createProject, !projects.contains(where: { $0.id == change.itemID }) { return "Choose an existing project." } |
| 30 | var record = WorkspaceRecord.new(operation == .createProject ? "project" : operation == .createDraft ? "draft" : "newsletter", projectID: operation == .createProject ? "" : change.itemID) |
| 31 | record["id"] = requestID |
| 32 | record.fields["planMarkdown"] = change.initialPlan |
| 33 | record[operation == .createProject ? "name" : "title"] = change.title |
| 34 | record[operation == .createNewsletter ? "body" : "content"] = change.text |
| 35 | try save(record) |
| 36 | case .setTitle: |
| 37 | guard var record = records.first(where: { $0.id == change.itemID }), record["updatedAt"] == change.revision else { return "This item changed. Refresh before renaming it." } |
| 38 | record[record.kind == "project" ? "name" : record.kind == "idea" ? "topic" : "title"] = change.text |
| 39 | try save(record) |
| 40 | case .moveProject: |
| 41 | guard var record = records.first(where: { $0.id == change.itemID }), |
| 42 | projects.contains(where: { $0.id == change.text }), record["updatedAt"] == change.revision else { |
| 43 | return "The item or project changed. Refresh before moving it." |
| 44 | } |
| 45 | record["projectId"] = change.text |
| 46 | try save(record) |
| 47 | } |
| 48 | return nil |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | struct WorkspaceOutline: View { |
| 53 | let record: WorkspaceRecord |
| 54 | let workspace: Workspace |
| 55 | let close: () -> Void |
| 56 | |
| 57 | var body: some View { |
| 58 | StudioScrollView { |
| 59 | VStack(alignment: .leading, spacing: 14) { |
| 60 | Button("Back", systemImage: "chevron.left", action: close) |
| 61 | Text("Outline").font(.title2) |
| 62 | ContentPlanEditor(markdown: record["planMarkdown"], reference: record.referenceID ?? record.id, |
| 63 | context: record.title, recoveryID: (workspace.directory?.path ?? "workspace") + "." + record.id, |
| 64 | save: { markdown, base in |
| 65 | try workspace.savePlan(itemID: record.id, markdown: markdown, base: base) |
| 66 | }, close: close) |
| 67 | if let previous = record.fields["previousPlanMarkdown"] as? String { |
| 68 | Button("Restore previous plan", systemImage: "arrow.uturn.backward") { |
| 69 | workspace.perform { |
| 70 | try workspace.savePlan(itemID: record.id, markdown: previous, base: record["planMarkdown"]) |
| 71 | close() |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | .padding(20) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | extension Workspace { |
| 82 | var assistantContents: [AssistantContent] { |
| 83 | records.filter { ["project", "draft", "idea", "newsletter"].contains($0.kind) }.map { |
| 84 | .init(id: $0.id, reference: $0.referenceID ?? $0.id, kind: $0.kind, title: $0.title, |
| 85 | project: $0.projectID, revision: $0["updatedAt"], body: $0.body) |
| 86 | } |
| 87 | } |
| 88 | func executeAssistant(_ command: LocalContentCommand, request: String) throws -> AssistantActionResult { |
| 89 | try command.validateIntent(request) |
| 90 | if command.operation == .find { |
| 91 | let query = command.target.lowercased() |
| 92 | let matches = assistantContents.filter { query.isEmpty || query == "my drafts" && $0.kind == "draft" || |
| 93 | query == "drafts" && $0.kind == "draft" || $0.kind == query || $0.title.localizedCaseInsensitiveContains(query) || $0.reference.localizedCaseInsensitiveContains(query) } |
| 94 | return .init(message: matches.isEmpty ? "No matches. Try an ID or title words." : matches.map { "\($0.reference) · \($0.kind) · \($0.title)" }.joined(separator: "\n")) |
| 95 | } |
| 96 | if command.operation == .review { |
| 97 | return .init(message: "Use the item’s Schedule, publishing, Trash or agent controls to review and confirm this action. Nothing was changed.") |
| 98 | } |
| 99 | if [.createIdea, .createDraft, .createNewsletter, .createProject].contains(command.operation) { |
| 100 | let kind = command.operation == .createIdea ? "idea" : command.operation == .createDraft ? "draft" : command.operation == .createNewsletter ? "newsletter" : "project" |
| 101 | let project = kind == "project" ? "" : try assistantProject(command.project) |
| 102 | var record = WorkspaceRecord.new(kind, projectID: kind == "project" ? "" : project) |
| 103 | record[kind == "project" ? "name" : kind == "idea" ? "topic" : "title"] = command.title |
| 104 | record[kind == "idea" ? "text" : kind == "newsletter" ? "body" : "content"] = command.value |
| 105 | if kind != "project", ContentPlan.requested(in: request) { record["planMarkdown"] = ContentPlan.starter(command.title) } |
| 106 | let saved = try save(record) |
| 107 | return .init(message: "Created \(saved.referenceID ?? saved.id) · \(saved.title). Saved on this Mac; unpublished.", itemID: saved.id) |
| 108 | } |
| 109 | let target = try command.resolve(in: assistantContents, request: request) |
| 110 | guard var record = records.first(where: { $0.id == target.id }) else { throw WorkspaceError("Refresh and try again.") } |
| 111 | if command.operation == .read { return .init(message: "\(target.reference) · \(target.title)\n\n\(target.body)", itemID: target.id) } |
| 112 | if command.operation == .plan { return .init(message: "Open \(target.reference) to edit its Plan and review AI suggestions. No saved text changed.", itemID: target.id) } |
| 113 | switch command.operation { |
| 114 | case .title: record[record.kind == "project" ? "name" : record.kind == "idea" ? "topic" : "title"] = command.value |
| 115 | case .body: |
| 116 | guard ["draft", "idea", "newsletter"].contains(record.kind) else { throw WorkspaceError("Choose a post, idea or newsletter.") } |
| 117 | record[record.kind == "idea" ? "text" : record.kind == "newsletter" ? "body" : "content"] = command.value |
| 118 | case .project: |
| 119 | guard record.kind != "project" else { throw WorkspaceError("Choose writing to move between projects.") } |
| 120 | record["projectId"] = try assistantProject(command.value) |
| 121 | default: throw WorkspaceError("That action needs the app’s review controls. Nothing changed.") |
| 122 | } |
| 123 | let saved = try save(record) |
| 124 | return .init(message: "Updated \(saved.referenceID ?? saved.id) · \(saved.title). Saved on this Mac; no publication was sent.", itemID: saved.id) |
| 125 | } |
| 126 | private func assistantProject(_ query: String) throws -> String { |
| 127 | if query.isEmpty { return projectID } |
| 128 | let matches = projects.filter { $0.matchesID(query) || $0.title.caseInsensitiveCompare(query) == .orderedSame } |
| 129 | guard matches.count == 1, let project = matches.first else { throw WorkspaceError("Use the exact project name or ID.") } |
| 130 | return project.id |
| 131 | } |
| 132 | } |