diff --git a/Shared/ScriptWidgetRuntime/AI/AIClient.swift b/Shared/ScriptWidgetRuntime/AI/AIClient.swift index 93fe7ce0..848222ef 100644 --- a/Shared/ScriptWidgetRuntime/AI/AIClient.swift +++ b/Shared/ScriptWidgetRuntime/AI/AIClient.swift @@ -13,7 +13,7 @@ import SwiftOpenAI import FoundationModels #endif -struct AITokenUsage: Equatable { +struct AITokenUsage: Equatable, Codable { var promptTokens: Int var completionTokens: Int var totalTokens: Int diff --git a/Shared/ScriptWidgetRuntime/AI/AIConversation.swift b/Shared/ScriptWidgetRuntime/AI/AIConversation.swift new file mode 100644 index 00000000..b4167cfb --- /dev/null +++ b/Shared/ScriptWidgetRuntime/AI/AIConversation.swift @@ -0,0 +1,220 @@ +// +// AIConversation.swift +// ScriptWidget +// +// The saved form of a multi-turn AI conversation about one widget: what the +// user asked in each turn, what came of it, and every version of the source +// a successful turn produced. +// +// A conversation never holds a credential. It names its provider profile by +// nothing at all: the profile that is active when a turn is sent is used. +// + +import Foundation + +enum AIConversationLimits { + /// One generation and up to three repairs. + static let maxAttemptsPerTurn = 4 + static let maxVersions = 30 + static let maxConversations = 50 + static let maxRequestCharacters = 4_000 + static let maxSourceBytes = 256 * 1024 + static let maxFileBytes = 12 * 1024 * 1024 + /// History sent to the model: the newest turns, shortened. + static let historyTurns = 12 + static let historyRequestCharacters = 500 + static let historyNoteCharacters = 200 + /// The version before the current one is sent for reference when it is small. + static let previousSourceCharacters = 8_000 + static let titleCharacters = 40 +} + +struct AIConversationVersion: Codable, Identifiable, Equatable { + let id: UUID + /// 1-based, in the order the versions were made; never reused. + let number: Int + let source: String + let parentID: UUID? + let createdAt: Date +} + +struct AIConversationTurn: Codable, Identifiable, Equatable { + enum Status: String, Codable { + /// Waiting for the model or running the result. Never read back from disk as such. + case running + case succeeded + /// Every attempt failed to run; the widget was left as it was. + case unconverged + /// The provider could not be reached or refused the request. + case failed + case cancelled + /// The app closed while the turn was running. + case interrupted + } + + let id: UUID + /// The user's own words. + var request: String + /// The version the turn started from; nil for the first turn of a new widget. + var baseVersionID: UUID? + var status: Status + /// The model's one-sentence account of what it did. + var note: String? + /// Why the turn did not succeed; safe to show. + var errorSummary: String? + /// How many attempts failed to run before the turn ended. + var repairs: Int + /// The version a successful turn produced. + var versionID: UUID? + /// The last attempt of an unconverged turn, for the user to look at. + var failedSource: String? + var usage: AITokenUsage + let createdAt: Date + /// The user went back to an older version and continued from there. + var isAbandoned: Bool +} + +struct AIConversation: Codable, Identifiable, Equatable { + static let currentFormatVersion = 1 + + var formatVersion: Int + let id: UUID + var title: String + var size: AIWidgetSize + var skillIDs: [String] + var turns: [AIConversationTurn] + var versions: [AIConversationVersion] + var currentVersionID: UUID? + /// Counts versions ever made, so a number is not reused after pruning. + var versionCounter: Int + /// The widget the conversation was saved as; nil while it is a draft. + var savedPackageName: String? + let createdAt: Date + var updatedAt: Date + + init(size: AIWidgetSize = .medium, skillIDs: [String] = [], now: Date = Date()) { + formatVersion = Self.currentFormatVersion + id = UUID() + title = "" + self.size = size + self.skillIDs = skillIDs + turns = [] + versions = [] + currentVersionID = nil + versionCounter = 0 + savedPackageName = nil + createdAt = now + updatedAt = now + } + + var isDraft: Bool { savedPackageName == nil } + var currentVersion: AIConversationVersion? { version(currentVersionID) } + var latestVersion: AIConversationVersion? { versions.max { $0.number < $1.number } } + var totalUsage: AITokenUsage { turns.reduce(.zero) { $0 + $1.usage } } + + func version(_ id: UUID?) -> AIConversationVersion? { + guard let id else { return nil } + return versions.first { $0.id == id } + } + + /// The turn that produced a version. + func turn(producing versionID: UUID) -> AIConversationTurn? { + turns.first { $0.versionID == versionID } + } + + /// Starts a turn from the current version. Turns made after that version + /// are marked abandoned: the conversation continues from where the user is. + mutating func beginTurn(request: String, now: Date = Date()) -> AIConversationTurn { + abandonTurnsAfterCurrentVersion() + let turn = AIConversationTurn( + id: UUID(), + request: String(request.prefix(AIConversationLimits.maxRequestCharacters)), + baseVersionID: currentVersionID, + status: .running, + note: nil, + errorSummary: nil, + repairs: 0, + versionID: nil, + failedSource: nil, + usage: .zero, + createdAt: now, + isAbandoned: false + ) + turns.append(turn) + if title.isEmpty { title = Self.title(from: request) } + updatedAt = now + return turn + } + + /// Records a version made by a turn and makes it the current one. + @discardableResult + mutating func addVersion(source: String, now: Date = Date()) -> AIConversationVersion { + versionCounter += 1 + let version = AIConversationVersion( + id: UUID(), number: versionCounter, source: source, parentID: currentVersionID, createdAt: now + ) + versions.append(version) + currentVersionID = version.id + pruneVersions() + updatedAt = now + return version + } + + mutating func updateTurn(_ id: UUID, _ change: (inout AIConversationTurn) -> Void) { + guard let index = turns.firstIndex(where: { $0.id == id }) else { return } + change(&turns[index]) + } + + /// The turns the model is told about: finished, not abandoned, oldest first. + func historyForPrompt() -> [AIConversationTurn] { + let finished = turns.filter { $0.status == .succeeded && !$0.isAbandoned } + return Array(finished.suffix(AIConversationLimits.historyTurns)) + } + + /// A turn left running by a closed app can never finish. + mutating func markRunningTurnsInterrupted() { + for index in turns.indices where turns[index].status == .running { + turns[index].status = .interrupted + } + } + + private mutating func abandonTurnsAfterCurrentVersion() { + guard let currentVersionID, version(currentVersionID) != nil else { return } + for index in turns.indices { + // A turn whose version was pruned keeps the state it had. + guard let produced = version(turns[index].versionID) else { continue } + let onCurrentPath = produced.id == currentVersionID || isAncestor(produced.id, of: currentVersionID) + turns[index].isAbandoned = !onCurrentPath + } + } + + private func isAncestor(_ candidate: UUID, of versionID: UUID) -> Bool { + var cursor = version(versionID)?.parentID + var steps = 0 + while let id = cursor, steps <= versions.count { + if id == candidate { return true } + cursor = version(id)?.parentID + steps += 1 + } + return false + } + + /// Keeps the newest versions and always the current one. + private mutating func pruneVersions() { + guard versions.count > AIConversationLimits.maxVersions else { return } + let removable = versions + .filter { $0.id != currentVersionID } + .sorted { $0.number < $1.number } + let excess = versions.count - AIConversationLimits.maxVersions + let removed = Set(removable.prefix(excess).map(\.id)) + versions.removeAll { removed.contains($0.id) } + } + + static func title(from request: String) -> String { + let line = request + .split(whereSeparator: \.isNewline).first.map(String.init)? + .trimmingCharacters(in: .whitespaces) ?? "" + guard line.count > AIConversationLimits.titleCharacters else { return line } + return String(line.prefix(AIConversationLimits.titleCharacters)) + "…" + } +} diff --git a/Shared/ScriptWidgetRuntime/AI/AIConversationSession.swift b/Shared/ScriptWidgetRuntime/AI/AIConversationSession.swift new file mode 100644 index 00000000..292ce286 --- /dev/null +++ b/Shared/ScriptWidgetRuntime/AI/AIConversationSession.swift @@ -0,0 +1,300 @@ +// +// AIConversationSession.swift +// ScriptWidget +// +// Drives one multi-turn AI conversation for SwiftUI: sends a turn through +// the agent loop, keeps every successful result as a version, keeps the last +// good widget on screen when a turn fails, and saves after every change. +// +// Generated code is untrusted. It only ever runs through AgentRuntimeBridge, +// in a temporary package, under the same runtime limits as any widget. +// + +import Foundation +#if canImport(Combine) +import Combine +#endif + +@MainActor +final class AIConversationSession: ObservableObject { + + enum Activity: Equatable { + case idle + case thinking(attempt: Int) + case running(attempt: Int) + case repairing(attempt: Int, errorSummary: String) + } + + @Published private(set) var conversation: AIConversation + @Published private(set) var activity: Activity = .idle + /// The current version, rendered at the conversation's size. + @Published private(set) var previewElement: ScriptWidgetRuntimeElement? + /// The package the preview was rendered in; views need it to resolve files. + @Published private(set) var previewPackage: ScriptWidgetPackage? + /// Console output of each attempt of the newest turn, for the Logs view. + @Published private(set) var attemptLogs: [AttemptLog] = [] + + struct AttemptLog: Identifiable, Equatable { + let id = UUID() + let attempt: Int + let errorSummary: String? + let lines: [String] + } + + var isRunning: Bool { activity != .idle } + var currentSource: String { conversation.currentVersion?.source ?? "" } + + private let store: AIConversationStore + private let bridge: AgentRuntimeBridge + private let makeLoop: () -> AgentLoop + private let loadSettings: () -> AISettings + private let skillsText: (Set) -> String? + + /// The temporary preview folder, removed when the session goes away. + private let previewPackageURL: URL? + + private(set) var currentTask: Task? + private var previewTask: Task? + /// Changes whenever a turn starts or is cancelled; late results are dropped. + private var generation = UUID() + private var previewGeneration = UUID() + private var pendingAttemptCode = "" + + init( + conversation: AIConversation = AIConversation(), + store: AIConversationStore = .shared, + bridge: AgentRuntimeBridge = .shared, + makeLoop: @escaping () -> AgentLoop = { AgentLoop() }, + loadSettings: @escaping () -> AISettings = { AISettingsStore.shared.load() }, + skillsText: @escaping (Set) -> String? = { AIWidgetSkills.instructions(selectedIDs: $0) } + ) { + self.conversation = conversation + self.store = store + self.bridge = bridge + self.makeLoop = makeLoop + self.loadSettings = loadSettings + self.skillsText = skillsText + let package = try? bridge.makeSandboxPackage(prefix: "chat-preview") + previewPackage = package + previewPackageURL = package?.path + if conversation.currentVersion != nil { refreshPreview() } + } + + deinit { + if let previewPackageURL { try? FileManager.default.removeItem(at: previewPackageURL) } + } + + // MARK: - turns + + /// Sends a new request, continuing from the current version. + func send(_ text: String) { + let request = text.trimmingCharacters(in: .whitespacesAndNewlines) + guard !request.isEmpty, !isRunning else { return } + + let base = conversation.currentVersion + let turn = conversation.beginTurn(request: request) + // Read after beginTurn: it drops the branch the user walked away from. + let exchanges = conversation.historyForPrompt() + .map { AITurnContext.Exchange(request: $0.request, note: $0.note) } + let context = AITurnContext( + history: exchanges, + currentCode: base?.source, + previousCode: conversation.version(base?.parentID)?.source, + request: turn.request, + skillsText: skillsText(Set(conversation.skillIDs)), + size: conversation.size + ) + persist() + run(turnID: turn.id, context: context) + } + + /// Sends a turn that did not succeed once more, from the version it started from. + func retry(turnID: UUID) { + guard !isRunning, let turn = conversation.turns.first(where: { $0.id == turnID }), + turn.status != .succeeded, turn.status != .running else { return } + if let base = turn.baseVersionID, conversation.version(base) != nil { + conversation.currentVersionID = base + refreshPreview() + } + send(turn.request) + } + + func cancel() { + guard isRunning else { return } + generation = UUID() + currentTask?.cancel() + finishRunningTurns(as: .cancelled, message: nil) + activity = .idle + persist() + } + + // MARK: - versions, size, skills + + /// Shows another version. Nothing is sent to the provider. + func selectVersion(_ id: UUID) { + guard !isRunning, conversation.version(id) != nil, conversation.currentVersionID != id else { return } + conversation.currentVersionID = id + conversation.updatedAt = Date() + persist() + refreshPreview() + } + + /// Renders the current version at another size, locally. + func setSize(_ size: AIWidgetSize) { + guard conversation.size != size else { return } + conversation.size = size + persist() + refreshPreview() + } + + func setSkills(_ ids: Set) { + conversation.skillIDs = ids.sorted() + persist() + } + + /// Records that the current version was saved as a widget. + func markSaved(packageName: String) { + conversation.savedPackageName = packageName + conversation.updatedAt = Date() + persist() + } + + // MARK: - private + + private func run(turnID: UUID, context: AITurnContext) { + let generation = UUID() + self.generation = generation + attemptLogs = [] + pendingAttemptCode = "" + activity = .thinking(attempt: 1) + + let settings = loadSettings() + let request = AgentLoopRequest( + mode: .turn(context), + size: conversation.size, + settings: settings, + maxIterations: AIConversationLimits.maxAttemptsPerTurn + ) + let loop = makeLoop() + currentTask = Task { [weak self] in + let outcome = await loop.run(request) { [weak self] event in + guard let self, self.generation == generation else { return } + self.apply(event: event, turnID: turnID) + } + guard let self, self.generation == generation else { return } + self.apply(outcome: outcome, turnID: turnID) + } + } + + private func apply(event: AgentLoopEvent, turnID: UUID) { + switch event { + case .thinking(let attempt): + if case .repairing = activity { break } + activity = .thinking(attempt: attempt) + case .produced(_, let jsx): + pendingAttemptCode = jsx + case .running(let attempt): + activity = .running(attempt: attempt) + case .ranFailed(let attempt, let summary, let logs): + attemptLogs.append(AttemptLog(attempt: attempt, errorSummary: summary, lines: logs)) + conversation.updateTurn(turnID) { $0.repairs += 1 } + activity = .repairing(attempt: attempt + 1, errorSummary: summary) + case .ranSucceeded(let attempt): + attemptLogs.append(AttemptLog(attempt: attempt, errorSummary: nil, lines: [])) + case .tokensUsed(let usage): + conversation.updateTurn(turnID) { $0.usage = usage } + case .note(let note): + // The first note describes the change; repairs only fix it. + conversation.updateTurn(turnID) { if $0.note == nil { $0.note = note } } + } + } + + private func apply(outcome: AgentLoopOutcome, turnID: UUID) { + activity = .idle + switch outcome { + case .succeeded(let jsx, let element, let usage): + guard jsx.utf8.count <= AIConversationLimits.maxSourceBytes else { + conversation.updateTurn(turnID) { + $0.status = .unconverged + $0.errorSummary = "The generated widget is larger than a conversation can keep." + $0.usage = usage + } + break + } + let version = conversation.addVersion(source: jsx) + conversation.updateTurn(turnID) { + $0.status = .succeeded + $0.versionID = version.id + $0.usage = usage + } + showPreview(element: element, source: jsx) + case .exhausted(let lastJSX, let lastError, let usage): + conversation.updateTurn(turnID) { + $0.status = .unconverged + $0.errorSummary = lastError + $0.failedSource = lastJSX.map { String($0.prefix(AIConversationLimits.maxSourceBytes)) } + $0.usage = usage + } + case .cancelled(let usage): + conversation.updateTurn(turnID) { + $0.status = .cancelled + $0.usage = usage + } + case .failed(let message, let usage): + conversation.updateTurn(turnID) { + $0.status = .failed + $0.errorSummary = message + $0.usage = usage + } + } + conversation.updatedAt = Date() + persist() + } + + private func finishRunningTurns(as status: AIConversationTurn.Status, message: String?) { + for turn in conversation.turns where turn.status == .running { + conversation.updateTurn(turn.id) { + $0.status = status + $0.errorSummary = message + } + } + } + + private func showPreview(element: ScriptWidgetRuntimeElement, source: String) { + previewGeneration = UUID() + previewTask?.cancel() + if let previewPackage { _ = previewPackage.writeMainFile(content: source) } + previewElement = element + } + + /// Runs the current version again in the preview package. + private func refreshPreview() { + guard let previewPackage, let version = conversation.currentVersion else { + previewElement = nil + return + } + let token = UUID() + previewGeneration = token + previewTask?.cancel() + let size = conversation.size + previewTask = Task { [weak self, bridge] in + let result = await bridge.run(jsx: version.source, in: previewPackage, size: size) + guard let self, self.previewGeneration == token else { return } + // A version only exists because it ran once; if it fails now (the + // network is gone, say) the element it does return is still shown. + self.previewElement = result.element + } + } + + private func persist() { + // An empty conversation is not worth a file. + guard !conversation.turns.isEmpty else { return } + store.save(conversation) + } + + /// Waits for the turn in flight and for the preview; for tests. + func settle() async { + await currentTask?.value + await previewTask?.value + } +} diff --git a/Shared/ScriptWidgetRuntime/AI/AIConversationStore.swift b/Shared/ScriptWidgetRuntime/AI/AIConversationStore.swift new file mode 100644 index 00000000..3296672c --- /dev/null +++ b/Shared/ScriptWidgetRuntime/AI/AIConversationStore.swift @@ -0,0 +1,130 @@ +// +// AIConversationStore.swift +// ScriptWidget +// +// Keeps AI conversations as one JSON file each in the app's own Application +// Support folder. Local to the device: not in iCloud, not in the app group, +// so a widget extension never reads them. +// +// What a file holds: the user's requests, the model's notes, error summaries +// and widget sources. It holds no API key, token, request header or profile. +// + +import Foundation + +struct AIConversationSummary: Identifiable, Equatable { + let id: UUID + let title: String + let updatedAt: Date + let versionCount: Int + let isDraft: Bool +} + +final class AIConversationStore { + static let shared = AIConversationStore(directory: AIConversationStore.defaultDirectory()) + + private let directory: URL + private let fileManager = FileManager.default + private let lock = NSLock() + + init(directory: URL) { + self.directory = directory + } + + static func defaultDirectory() -> URL { + let base = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first + ?? URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true) + return base + .appendingPathComponent("ScriptWidget", isDirectory: true) + .appendingPathComponent("AIConversations", isDirectory: true) + } + + /// Writes the conversation, then removes the oldest ones over the limit. + @discardableResult + func save(_ conversation: AIConversation) -> Bool { + lock.lock() + defer { lock.unlock() } + do { + try fileManager.createDirectory(at: directory, withIntermediateDirectories: true) + let data = try Self.encoder.encode(conversation) + guard data.count <= AIConversationLimits.maxFileBytes else { return false } + try data.write(to: fileURL(conversation.id), options: .atomic) + } catch { + return false + } + pruneLocked(keeping: conversation.id) + return true + } + + func load(id: UUID) -> AIConversation? { + lock.lock() + defer { lock.unlock() } + return readLocked(fileURL(id)) + } + + /// Newest first. Files that cannot be read are left out, not deleted. + func summaries(draftsOnly: Bool = false) -> [AIConversationSummary] { + lock.lock() + defer { lock.unlock() } + return allLocked() + .filter { !draftsOnly || $0.isDraft } + .filter { !$0.turns.isEmpty } + .map { + AIConversationSummary( + id: $0.id, title: $0.title, updatedAt: $0.updatedAt, + versionCount: $0.versions.count, isDraft: $0.isDraft + ) + } + } + + func delete(id: UUID) { + lock.lock() + defer { lock.unlock() } + try? fileManager.removeItem(at: fileURL(id)) + } + + // MARK: - private + + private static let encoder: JSONEncoder = { + let encoder = JSONEncoder() + encoder.outputFormatting = [.sortedKeys] + return encoder + }() + + private static let decoder = JSONDecoder() + + private func fileURL(_ id: UUID) -> URL { + directory.appendingPathComponent(id.uuidString.lowercased() + ".json", isDirectory: false) + } + + private func readLocked(_ url: URL) -> AIConversation? { + guard let values = try? url.resourceValues(forKeys: [.isRegularFileKey, .isSymbolicLinkKey, .fileSizeKey]), + values.isRegularFile == true, values.isSymbolicLink != true, + (values.fileSize ?? 0) <= AIConversationLimits.maxFileBytes, + let data = try? Data(contentsOf: url), + var conversation = try? Self.decoder.decode(AIConversation.self, from: data), + conversation.formatVersion == AIConversation.currentFormatVersion, + url.deletingPathExtension().lastPathComponent == conversation.id.uuidString.lowercased() + else { return nil } + conversation.markRunningTurnsInterrupted() + return conversation + } + + private func allLocked() -> [AIConversation] { + let urls = (try? fileManager.contentsOfDirectory( + at: directory, includingPropertiesForKeys: nil, options: [.skipsHiddenFiles] + )) ?? [] + return urls + .filter { $0.pathExtension == "json" } + .compactMap(readLocked) + .sorted { $0.updatedAt > $1.updatedAt } + } + + private func pruneLocked(keeping kept: UUID) { + let all = allLocked() + guard all.count > AIConversationLimits.maxConversations else { return } + for conversation in all.dropFirst(AIConversationLimits.maxConversations) where conversation.id != kept { + try? fileManager.removeItem(at: fileURL(conversation.id)) + } + } +} diff --git a/Shared/ScriptWidgetRuntime/AI/AIGenerateSession.swift b/Shared/ScriptWidgetRuntime/AI/AIGenerateSession.swift index 04b34603..81e95ed5 100644 --- a/Shared/ScriptWidgetRuntime/AI/AIGenerateSession.swift +++ b/Shared/ScriptWidgetRuntime/AI/AIGenerateSession.swift @@ -170,6 +170,8 @@ final class AIGenerateSession: ObservableObject { )) case .tokensUsed(let u): usage = u + case .note: + break } } diff --git a/Shared/ScriptWidgetRuntime/AI/AIWidgetSkills.swift b/Shared/ScriptWidgetRuntime/AI/AIWidgetSkills.swift index 20ac3945..39b0f6a7 100644 --- a/Shared/ScriptWidgetRuntime/AI/AIWidgetSkills.swift +++ b/Shared/ScriptWidgetRuntime/AI/AIWidgetSkills.swift @@ -389,10 +389,16 @@ enum AIWidgetSkills { static var all: [AIWidgetSkill] { AIWidgetSkillManager.shared.allSkills() } - static func augment(_ prompt: String, selectedIDs: Set) -> String { + /// The selected Skills as prompt text, or nil when none is selected. + /// Skills are text only: they add to a prompt and grant nothing. + static func instructions(selectedIDs: Set) -> String? { let selected = all.filter { selectedIDs.contains($0.id) } - guard !selected.isEmpty else { return prompt } - let instructions = selected.map { "- \($0.title) v\($0.version):\n \($0.instruction.replacingOccurrences(of: "\n", with: "\n "))" }.joined(separator: "\n") + guard !selected.isEmpty else { return nil } + return selected.map { "- \($0.title) v\($0.version):\n \($0.instruction.replacingOccurrences(of: "\n", with: "\n "))" }.joined(separator: "\n") + } + + static func augment(_ prompt: String, selectedIDs: Set) -> String { + guard let instructions = instructions(selectedIDs: selectedIDs) else { return prompt } return """ \(prompt) diff --git a/Shared/ScriptWidgetRuntime/AI/AgentLoop.swift b/Shared/ScriptWidgetRuntime/AI/AgentLoop.swift index d203a84c..304a12fe 100644 --- a/Shared/ScriptWidgetRuntime/AI/AgentLoop.swift +++ b/Shared/ScriptWidgetRuntime/AI/AgentLoop.swift @@ -21,6 +21,8 @@ enum AgentLoopEvent { case ranFailed(iteration: Int, errorSummary: String, logs: [String]) case ranSucceeded(iteration: Int) case tokensUsed(AITokenUsage) // incremental + /// The model's one-sentence account of its change; conversation turns only. + case note(String) } struct AgentLoopRequest { @@ -28,6 +30,8 @@ struct AgentLoopRequest { case fresh(userDescription: String) case refine(currentCode: String, refineInstruction: String) case copilot(currentCode: String, instruction: String, runtimeDiagnostic: String?) + /// One turn of a saved conversation; see AIConversationSession. + case turn(AITurnContext) } let mode: Mode let size: AIWidgetSize @@ -75,7 +79,12 @@ final class AgentLoop { ) ) latestCode = currentCode + case .turn(let context): + firstUserMessage = AIMessage(role: .user, content: PromptBuilder.userPromptTurn(context)) + latestCode = context.currentCode } + let isTurn: Bool + if case .turn = request.mode { isTurn = true } else { isTurn = false } let package: ScriptWidgetPackage do { @@ -128,7 +137,14 @@ final class AgentLoop { return .cancelled(usage: cumulativeUsage) } - let jsx = PromptBuilder.stripCodeFences(chatResult.content) + let jsx: String + if isTurn { + let reply = PromptBuilder.parseTurnReply(chatResult.content) + jsx = reply.code + if let note = reply.note { await onEvent(.note(note)) } + } else { + jsx = PromptBuilder.stripCodeFences(chatResult.content) + } latestCode = jsx await onEvent(.produced(iteration: iteration, jsx: jsx)) diff --git a/Shared/ScriptWidgetRuntime/AI/PromptBuilder.swift b/Shared/ScriptWidgetRuntime/AI/PromptBuilder.swift index 0d7b71fe..9ee2a38a 100644 --- a/Shared/ScriptWidgetRuntime/AI/PromptBuilder.swift +++ b/Shared/ScriptWidgetRuntime/AI/PromptBuilder.swift @@ -80,6 +80,24 @@ struct AIMessage { let content: String } +/// What one conversation turn sends to the model. +struct AITurnContext { + struct Exchange: Equatable { + let request: String + let note: String? + } + /// Earlier applied turns, oldest first. + let history: [Exchange] + /// The widget as it is now; nil when the turn starts a new widget. + let currentCode: String? + /// The version before the current one, if any. + let previousCode: String? + let request: String + /// Formatted text of the selected Skills; nil for none. + let skillsText: String? + let size: AIWidgetSize +} + struct AICopilotChangeSummary: Equatable { let originalLines: Int let proposedLines: Int @@ -280,7 +298,9 @@ enum PromptBuilder { if recentLogs.isEmpty { logBlock = "(no console output)" } else { - logBlock = recentLogs.suffix(10).joined(separator: "\n") + // A script may print anything, including what it read from the + // device, so only short tails of the last lines are sent. + logBlock = recentLogs.suffix(10).map { clip($0, 300) }.joined(separator: "\n") } return """ Your previous code failed to run: @@ -346,6 +366,91 @@ enum PromptBuilder { """ } + /// One turn of a conversation: what was said before, the widget as it is + /// now, and the new request. The earlier turns travel as text inside this + /// one message, so the system prompt stays the same from turn to turn. + static func userPromptTurn(_ context: AITurnContext) -> String { + var parts: [String] = [] + parts.append(""" + You are working with the user on one ScriptWidget widget over several turns. + Widget size: \(context.size.rawValue) + Size hint: \(context.size.designHint) + """) + + if let skills = context.skillsText, !skills.isEmpty { + parts.append("Apply these ScriptWidget skills in every turn:\n" + skills) + } + + if !context.history.isEmpty { + let lines = context.history.enumerated().map { index, exchange -> String in + let request = clip(exchange.request, AIConversationLimits.historyRequestCharacters) + let note = clip(exchange.note ?? "Done.", AIConversationLimits.historyNoteCharacters) + return "\(index + 1). User: \(request)\n You: \(note)" + } + parts.append("Earlier turns, oldest first (each one was applied):\n" + lines.joined(separator: "\n")) + } + + if let previous = context.previousCode, !previous.isEmpty, + previous.count <= AIConversationLimits.previousSourceCharacters { + parts.append(""" + The version before your last change, for reference only (the user may ask to bring something back): + + ```jsx + \(previous) + ``` + """) + } + + if let current = context.currentCode, !current.isEmpty { + parts.append(""" + Current working widget code: + + ```jsx + \(current) + ``` + + The user now asks: + \(context.request) + + Change only what the request needs and keep everything else as it is. + """) + } else { + parts.append("The user asks for a new widget:\n\(context.request)") + } + + parts.append(""" + Reply in exactly this form, which replaces the output rule above for this conversation: + SUMMARY: one short sentence saying what you made or changed, in the language the user wrote in + then the FULL widget code in one ```jsx fenced block. Nothing else. + """) + return parts.joined(separator: "\n\n") + } + + /// Splits a turn reply into the model's note and the widget code. A reply + /// without the SUMMARY line or without a fence is still read as code. + static func parseTurnReply(_ raw: String) -> (note: String?, code: String) { + let text = raw.trimmingCharacters(in: .whitespacesAndNewlines) + let fenceStart = text.range(of: "```")?.lowerBound ?? text.endIndex + var note: String? + var body = text + let head = text[.. String { + text.count > limit ? String(text.prefix(limit)) + "…" : text + } + // Best-effort extraction: prefer content between matching ```jsx ... // ``` fences, then strip any leading/trailing prose. static func stripCodeFences(_ raw: String) -> String { diff --git a/Tests/ScriptWidgetRuntimeTests/AIConversationTests.swift b/Tests/ScriptWidgetRuntimeTests/AIConversationTests.swift new file mode 100644 index 00000000..bfd47c94 --- /dev/null +++ b/Tests/ScriptWidgetRuntimeTests/AIConversationTests.swift @@ -0,0 +1,623 @@ +// +// AIConversationTests.swift +// ScriptWidgetRuntimeTests +// +// Multi-turn AI conversations, tested against a stub provider: no network, +// no key, no tokens. The stub answers the OpenAI-compatible chat endpoint +// from a queue and records every request body it receives. The generated +// code runs in the real ScriptWidget runtime. +// + +import XCTest +#if os(macOS) +import SwiftUI +import AppKit +#endif +@testable import ScriptWidget + +@MainActor +final class AIConversationTests: XCTestCase { + + private static let secret = "sk-stub-secret-7f3a91" + private static let goodWidget = "$render(Hello)" + private static let betterWidget = "$render(HelloWorld)" + private static let brokenWidget = "const broken = ;" + + private var directory: URL! + private var store: AIConversationStore! + private var urlSession: URLSession! + + override func setUp() async throws { + directory = FileManager.default.temporaryDirectory + .appendingPathComponent("AIConversationTests-\(UUID().uuidString)", isDirectory: true) + store = AIConversationStore(directory: directory) + let configuration = URLSessionConfiguration.ephemeral + configuration.protocolClasses = [ConversationProviderStub.self] + urlSession = URLSession(configuration: configuration) + ConversationProviderStub.reset() + } + + override func tearDown() async throws { + urlSession.invalidateAndCancel() + try? FileManager.default.removeItem(at: directory) + ConversationProviderStub.reset() + } + + private func makeSession( + _ conversation: AIConversation = AIConversation(), + skills: String? = nil + ) -> AIConversationSession { + let client = AIClient(session: urlSession) + var profile = AIProfile.makeDefault() + profile.baseURL = "http://conversation.test" + profile.model = "stub-model" + profile.apiKey = Self.secret + let settings = AISettings(profile: profile, maxIterations: 20, temperature: 0) + return AIConversationSession( + conversation: conversation, + store: store, + makeLoop: { AgentLoop(client: client) }, + loadSettings: { settings }, + skillsText: { _ in skills } + ) + } + + private func reply(_ summary: String, _ code: String) -> ConversationProviderStub.Reply { + .content("SUMMARY: \(summary)\n```jsx\n\(code)\n```") + } + + // MARK: - prompt and reply format + + func testTurnPromptCarriesHistoryBothSourcesSkillsAndRequest() { + let prompt = PromptBuilder.userPromptTurn(AITurnContext( + history: [.init(request: "A greeting card", note: "Made a greeting.")], + currentCode: "CURRENT_SOURCE", + previousCode: "PREVIOUS_SOURCE", + request: "Make the title smaller", + skillsText: "- Visual Polish v1:\n Keep it tidy.", + size: .small + )) + for expected in ["A greeting card", "Made a greeting.", "CURRENT_SOURCE", "PREVIOUS_SOURCE", + "Make the title smaller", "Visual Polish", "Widget size: small", "SUMMARY:"] { + XCTAssertTrue(prompt.contains(expected), "prompt should contain \(expected)") + } + } + + func testFirstTurnPromptHasNoCodeAndLongHistoryIsClipped() { + let long = String(repeating: "x", count: 2_000) + let prompt = PromptBuilder.userPromptTurn(AITurnContext( + history: [.init(request: long, note: nil)], + currentCode: nil, previousCode: nil, request: "A clock", skillsText: nil, size: .medium + )) + XCTAssertTrue(prompt.contains("The user asks for a new widget")) + XCTAssertFalse(prompt.contains("Current working widget code")) + XCTAssertFalse(prompt.contains(long)) + XCTAssertTrue(prompt.contains(String(repeating: "x", count: AIConversationLimits.historyRequestCharacters) + "…")) + } + + func testReplyParsingAcceptsEveryShapeAModelMaySend() { + let full = PromptBuilder.parseTurnReply("SUMMARY: 标题改小了\n```jsx\n$render(a)\n```") + XCTAssertEqual(full.note, "标题改小了") + XCTAssertEqual(full.code, "$render(a)") + + let noFence = PromptBuilder.parseTurnReply("Summary: Smaller title\n$render(a)") + XCTAssertEqual(noFence.note, "Smaller title") + XCTAssertEqual(noFence.code, "$render(a)") + + let codeOnly = PromptBuilder.parseTurnReply("$render(a)") + XCTAssertNil(codeOnly.note) + XCTAssertEqual(codeOnly.code, "$render(a)") + + let fenceOnly = PromptBuilder.parseTurnReply("```jsx\n$render(a)\n```") + XCTAssertNil(fenceOnly.note) + XCTAssertEqual(fenceOnly.code, "$render(a)") + } + + func testRepairPromptClipsLongLogLines() { + let line = String(repeating: "secret-looking-data ", count: 100) + let prompt = PromptBuilder.userPromptFix(previousCode: "x", errorSummary: "e", recentLogs: [line]) + XCTAssertFalse(prompt.contains(line)) + } + + // MARK: - turns + + func testSecondTurnSendsHistoryAndCurrentSourceAndMakesVersionTwo() async { + ConversationProviderStub.enqueue([ + reply("Made a greeting.", Self.goodWidget), + reply("Added a second line.", Self.betterWidget), + ]) + let session = makeSession(skills: "- Tidy v1:\n Keep it tidy.") + + session.send("A greeting card") + await session.settle() + XCTAssertEqual(session.conversation.turns.first?.status, .succeeded) + XCTAssertEqual(session.conversation.turns.first?.note, "Made a greeting.") + XCTAssertEqual(session.conversation.currentVersion?.number, 1) + XCTAssertEqual(session.currentSource, Self.goodWidget) + XCTAssertNotNil(session.previewElement) + XCTAssertEqual(session.conversation.title, "A greeting card") + + session.send("Add a second line") + await session.settle() + XCTAssertEqual(session.conversation.versions.count, 2) + XCTAssertEqual(session.conversation.currentVersion?.number, 2) + XCTAssertEqual(session.currentSource, Self.betterWidget) + XCTAssertEqual(session.conversation.totalUsage.totalTokens, 30) + + let bodies = ConversationProviderStub.bodies + XCTAssertEqual(bodies.count, 2) + XCTAssertFalse(bodies[0].contains("Current working widget code")) + XCTAssertTrue(bodies[0].contains("Keep it tidy.")) + let second = bodies[1] + XCTAssertTrue(second.contains("A greeting card"), "the earlier request is sent") + XCTAssertTrue(second.contains("Made a greeting."), "the earlier note is sent") + XCTAssertTrue(second.contains("Hello"), "the current source is sent") + XCTAssertTrue(second.contains("Add a second line")) + XCTAssertTrue(second.contains("Keep it tidy."), "skills apply to every turn") + } + + func testChangeThatNeverRunsLeavesTheWidgetAndPreviewAsTheyWere() async { + ConversationProviderStub.enqueue([reply("Made a greeting.", Self.goodWidget)]) + ConversationProviderStub.enqueue(Array(repeating: reply("Broke it.", Self.brokenWidget), count: 6)) + let session = makeSession() + session.send("A greeting card") + await session.settle() + let firstVersion = session.conversation.currentVersionID + + session.send("Break it") + await session.settle() + + let turn = session.conversation.turns[1] + XCTAssertEqual(turn.status, .unconverged) + XCTAssertNil(turn.versionID) + XCTAssertEqual(turn.failedSource, Self.brokenWidget) + XCTAssertNotNil(turn.errorSummary) + XCTAssertEqual(session.conversation.currentVersionID, firstVersion) + XCTAssertEqual(session.currentSource, Self.goodWidget) + XCTAssertNotNil(session.previewElement, "the last good widget stays on screen") + XCTAssertFalse(session.isRunning) + XCTAssertEqual(ConversationProviderStub.bodies.count, 1 + AIConversationLimits.maxAttemptsPerTurn, + "one generation and three repairs, no more") + } + + func testRepairedTurnSucceedsAndCountsItsRepairs() async { + ConversationProviderStub.enqueue([ + reply("Made a greeting.", Self.brokenWidget), + .content(Self.goodWidget), + ]) + let session = makeSession() + session.send("A greeting card") + await session.settle() + + let turn = session.conversation.turns[0] + XCTAssertEqual(turn.status, .succeeded) + XCTAssertEqual(turn.repairs, 1) + XCTAssertEqual(turn.note, "Made a greeting.") + XCTAssertEqual(session.currentSource, Self.goodWidget) + XCTAssertEqual(session.attemptLogs.count, 2) + XCTAssertTrue(ConversationProviderStub.bodies[1].contains("failed to run")) + } + + func testProviderErrorKeepsTheConversationUsableAndRetryWorks() async { + ConversationProviderStub.enqueue([reply("Made a greeting.", Self.goodWidget), .status(500)]) + let session = makeSession() + session.send("A greeting card") + await session.settle() + session.send("Add a second line") + await session.settle() + + let failed = session.conversation.turns[1] + XCTAssertEqual(failed.status, .failed) + XCTAssertNotNil(failed.errorSummary) + XCTAssertFalse(failed.errorSummary?.contains(Self.secret) ?? false) + XCTAssertEqual(session.currentSource, Self.goodWidget) + XCTAssertNotNil(session.previewElement) + + ConversationProviderStub.enqueue([reply("Added a second line.", Self.betterWidget)]) + session.retry(turnID: failed.id) + await session.settle() + XCTAssertEqual(session.conversation.turns.count, 3) + XCTAssertEqual(session.conversation.turns[2].status, .succeeded) + XCTAssertEqual(session.currentSource, Self.betterWidget) + } + + func testStoppingATurnKeepsTheCurrentVersion() async { + ConversationProviderStub.enqueue([reply("Made a greeting.", Self.goodWidget), .hang]) + let session = makeSession() + session.send("A greeting card") + await session.settle() + + session.send("Add a second line") + XCTAssertTrue(session.isRunning) + session.cancel() + await session.settle() + + XCTAssertFalse(session.isRunning) + XCTAssertEqual(session.conversation.turns[1].status, .cancelled) + XCTAssertEqual(session.conversation.currentVersion?.number, 1) + XCTAssertNotNil(session.previewElement) + XCTAssertEqual(store.load(id: session.conversation.id)?.turns[1].status, .cancelled) + } + + func testEmptyRequestAndASecondRequestWhileRunningAreIgnored() async { + ConversationProviderStub.enqueue([.hang]) + let session = makeSession() + session.send(" ") + XCTAssertTrue(session.conversation.turns.isEmpty) + session.send("A greeting card") + session.send("Another one") + XCTAssertEqual(session.conversation.turns.count, 1) + session.cancel() + await session.settle() + } + + // MARK: - versions + + func testGoingBackAndContinuingLeavesTheLaterTurnBehind() async { + ConversationProviderStub.enqueue([ + reply("Made a greeting.", Self.goodWidget), + reply("Added a second line.", Self.betterWidget), + reply("Made it a clock.", "$render(Clock)"), + ]) + let session = makeSession() + session.send("A greeting card") + await session.settle() + session.send("Add a second line") + await session.settle() + let first = session.conversation.versions[0] + + session.selectVersion(first.id) + await session.settle() + XCTAssertEqual(session.currentSource, Self.goodWidget) + XCTAssertEqual(ConversationProviderStub.bodies.count, 2, "going back sends nothing") + XCTAssertFalse(session.conversation.turns[1].isAbandoned, "looking is not leaving") + + session.send("Make it a clock instead") + await session.settle() + + XCTAssertTrue(session.conversation.turns[1].isAbandoned) + XCTAssertFalse(session.conversation.turns[0].isAbandoned) + XCTAssertEqual(session.conversation.currentVersion?.number, 3) + XCTAssertEqual(session.conversation.currentVersion?.parentID, first.id) + let third = ConversationProviderStub.bodies[2] + XCTAssertFalse(third.contains("Add a second line"), "the abandoned turn is not sent") + XCTAssertFalse(third.contains("World"), "the abandoned source is not sent") + XCTAssertTrue(third.contains("A greeting card")) + } + + func testChangingSizeRunsLocallyAndSendsNothing() async { + ConversationProviderStub.enqueue([reply("Made it.", "$render({$getenv(\"widget-size\")})")]) + let session = makeSession() + session.send("Show the size") + await session.settle() + XCTAssertEqual(texts(session.previewElement), ["medium"]) + + session.setSize(.large) + await session.settle() + + XCTAssertEqual(texts(session.previewElement), ["large"], "the widget ran again at the new size") + XCTAssertEqual(ConversationProviderStub.bodies.count, 1) + XCTAssertEqual(store.load(id: session.conversation.id)?.size, .large) + } + + func testVersionsAreCappedAndTheCurrentOneIsKept() { + var conversation = AIConversation() + let first = conversation.addVersion(source: "v1") + for index in 2...(AIConversationLimits.maxVersions + 5) { + conversation.addVersion(source: "v\(index)") + } + XCTAssertEqual(conversation.versions.count, AIConversationLimits.maxVersions) + XCTAssertNil(conversation.version(first.id)) + XCTAssertEqual(conversation.currentVersion?.number, AIConversationLimits.maxVersions + 5) + } + + // MARK: - storage + + func testSavedConversationRoundTripsAndHoldsNoCredential() async throws { + ConversationProviderStub.enqueue([reply("Made a greeting.", Self.goodWidget)]) + let session = makeSession() + session.send("A greeting card") + await session.settle() + session.markSaved(packageName: "Greeting") + + XCTAssertTrue(ConversationProviderStub.authorizations.allSatisfy { $0 == "Bearer " + Self.secret }, + "the key travels in the request header only") + let files = try FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: nil) + XCTAssertEqual(files.count, 1) + let text = try String(contentsOf: files[0], encoding: .utf8) + for forbidden in [Self.secret, "Bearer", "apiKey", "Authorization", "conversation.test"] { + XCTAssertFalse(text.contains(forbidden), "a saved conversation must not contain \(forbidden)") + } + + let loaded = try XCTUnwrap(store.load(id: session.conversation.id)) + XCTAssertEqual(loaded, session.conversation) + XCTAssertFalse(loaded.isDraft) + XCTAssertTrue(store.summaries(draftsOnly: true).isEmpty) + XCTAssertEqual(store.summaries().map(\.id), [loaded.id]) + + // A conversation opened again shows its current version without asking the provider. + let reopened = makeSession(loaded) + await reopened.settle() + XCTAssertNotNil(reopened.previewElement) + XCTAssertEqual(ConversationProviderStub.bodies.count, 1) + } + + func testTurnLeftRunningIsReadBackAsInterrupted() throws { + var conversation = AIConversation() + _ = conversation.beginTurn(request: "A greeting card") + XCTAssertTrue(store.save(conversation)) + let loaded = try XCTUnwrap(store.load(id: conversation.id)) + XCTAssertEqual(loaded.turns.first?.status, .interrupted) + } + + func testStoreKeepsOnlyTheNewestConversations() { + var kept: [UUID] = [] + for index in 0..<(AIConversationLimits.maxConversations + 3) { + var conversation = AIConversation(now: Date(timeIntervalSince1970: Double(index) * 60)) + _ = conversation.beginTurn(request: "Widget \(index)", now: Date(timeIntervalSince1970: Double(index) * 60)) + XCTAssertTrue(store.save(conversation)) + kept.append(conversation.id) + } + let remaining = store.summaries() + XCTAssertEqual(remaining.count, AIConversationLimits.maxConversations) + XCTAssertNil(store.load(id: kept[0])) + XCTAssertNotNil(store.load(id: kept.last!)) + } + + func testUnreadableOrForeignFilesAreIgnored() throws { + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + try Data("not json".utf8).write(to: directory.appendingPathComponent("\(UUID().uuidString.lowercased()).json")) + var conversation = AIConversation() + _ = conversation.beginTurn(request: "A greeting card") + XCTAssertTrue(store.save(conversation)) + // The same content under another name is not that conversation. + let other = UUID() + try FileManager.default.copyItem( + at: directory.appendingPathComponent(conversation.id.uuidString.lowercased() + ".json"), + to: directory.appendingPathComponent(other.uuidString.lowercased() + ".json") + ) + XCTAssertNil(store.load(id: other)) + XCTAssertEqual(store.summaries().count, 1) + } + + // MARK: - a real model, on request + + /// Three turns against a real OpenAI-compatible provider. Costs tokens, so it + /// runs only when ROCKY_OPENAI_APIKEY is set (see AIGenerationTests for the + /// variables). With AI_CONVERSATION_LIVE_DIR set, the sources and a short + /// account of each turn are written there; the key is never written. + func testThreeTurnsAgainstARealProviderWhenConfigured() async throws { + let environment = ProcessInfo.processInfo.environment + func read(_ name: String) -> String? { + for key in [name, "TEST_RUNNER_\(name)"] { + if let value = environment[key]?.trimmingCharacters(in: .whitespacesAndNewlines), !value.isEmpty { + return value + } + } + return nil + } + guard let key = read("ROCKY_OPENAI_APIKEY") else { + throw XCTSkip("ROCKY_OPENAI_APIKEY not set — skipping the live conversation test.") + } + let profile = AIProfile( + id: "live-conversation", name: "live", + baseURL: read("ROCKY_OPENAI_BASEURL") ?? AIProfile.defaultBaseURL, + model: read("ROCKY_OPENAI_MODEL") ?? AIProfile.defaultModel, + apiKey: key, authMethod: .apiKey + ) + let settings = AISettings(profile: profile, maxIterations: 20, temperature: 0.2) + let session = AIConversationSession(store: store, loadSettings: { settings }, skillsText: { _ in nil }) + + let requests = [ + "A card that says Hello in large text with today's date under it, on a light background", + "Make the Hello text smaller and change the background to dark blue with white text", + "Add a line at the bottom that says Have a nice day", + ] + var report: [String] = [] + for (index, request) in requests.enumerated() { + let started = Date() + session.send(request) + await session.settle() + let turn = try XCTUnwrap(session.conversation.turns.last) + report.append(""" + Turn \(index + 1): \(request) + status: \(turn.status.rawValue), repairs: \(turn.repairs), tokens: \(turn.usage.totalTokens), seconds: \(Int(Date().timeIntervalSince(started))) + note: \(turn.note ?? "(none)") + error: \(turn.errorSummary ?? "(none)") + """) + if let folder = read("AI_CONVERSATION_LIVE_DIR") { + let output = URL(fileURLWithPath: folder, isDirectory: true) + try FileManager.default.createDirectory(at: output, withIntermediateDirectories: true) + try (turn.failedSource ?? session.currentSource) + .write(to: output.appendingPathComponent("turn-\(index + 1).jsx"), atomically: true, encoding: .utf8) + try report.joined(separator: "\n").write( + to: output.appendingPathComponent("report.txt"), atomically: true, encoding: .utf8) + } + XCTAssertEqual(turn.status, .succeeded, "turn \(index + 1) should run: \(turn.errorSummary ?? "")") + XCTAssertNotNil(turn.note, "the model should say what it did") + } + print(report.joined(separator: "\n")) + + XCTAssertEqual(session.conversation.versions.count, 3) + let final = session.currentSource + XCTAssertTrue(final.contains("Hello"), "an unrelated part survives the later turns") + XCTAssertTrue(final.localizedCaseInsensitiveContains("Have a nice day")) + XCTAssertNotNil(session.previewElement) + } + + // MARK: - window snapshot (macOS, on request) + + #if os(macOS) + /// Writes pictures of the conversation window for review. Runs only when + /// AI_CONVERSATION_SNAPSHOT_DIR names a folder; it asserts nothing about looks. + func testWriteWindowSnapshotsWhenRequested() async throws { + let environment = ProcessInfo.processInfo.environment + guard let folder = environment["AI_CONVERSATION_SNAPSHOT_DIR"] + ?? environment["TEST_RUNNER_AI_CONVERSATION_SNAPSHOT_DIR"] else { + throw XCTSkip("Set AI_CONVERSATION_SNAPSHOT_DIR to write window pictures") + } + let output = URL(fileURLWithPath: folder, isDirectory: true) + try FileManager.default.createDirectory(at: output, withIntermediateDirectories: true) + + let card = """ + const today = new Date().toLocaleDateString("en-US", { weekday: "long", month: "long", day: "numeric" }); + + $render( + + Good morning + {today} + Humidity 48% + + 21° + , + ); + """ + var conversation = AIConversation() + let usage = AITokenUsage(promptTokens: 5_200, completionTokens: 310, totalTokens: 5_510) + func turn(_ request: String, note: String, source: String, repairs: Int = 0) { + let turn = conversation.beginTurn(request: request) + let version = conversation.addVersion(source: source) + conversation.updateTurn(turn.id) { + $0.status = .succeeded + $0.note = note + $0.versionID = version.id + $0.repairs = repairs + $0.usage = usage + } + } + turn("A card with a greeting, today's date and the temperature", + note: "Made a dark greeting card with the date and temperature.", + source: card.replacingOccurrences(of: "#0a84ff", with: "#1c1c1e")) + turn("Make the title smaller and use a blue background", + note: "Made the title smaller and switched to a blue background.", source: card, repairs: 1) + let failed = conversation.beginTurn(request: "Turn it into a chart of the week") + conversation.updateTurn(failed.id) { + $0.status = .unconverged + $0.errorSummary = "transformError: Unexpected token (3:14)" + $0.failedSource = Self.brokenWidget + $0.repairs = 3 + $0.usage = usage + } + + try await snapshot(AIGenerateWindowView(conversation: conversation), to: output.appendingPathComponent("conversation.png")) + try await snapshot(AIGenerateWindowView(), to: output.appendingPathComponent("empty.png")) + } + + private func snapshot(_ view: some View, to url: URL) async throws { + let size = NSSize(width: 940, height: 660) + let window = NSWindow(contentRect: NSRect(origin: .zero, size: size), + styleMask: [.titled], backing: .buffered, defer: false) + window.isReleasedWhenClosed = false + let host = NSHostingView(rootView: view + .frame(width: size.width, height: size.height) + .background(Color(nsColor: .windowBackgroundColor))) + host.appearance = NSAppearance(named: .aqua) + host.frame = NSRect(origin: .zero, size: size) + window.contentView = host + window.orderBack(nil) + defer { window.close() } + // The preview runs the widget off the main thread; give it time to arrive. + try await Task.sleep(nanoseconds: 2_500_000_000) + host.layoutSubtreeIfNeeded() + let bitmap = try XCTUnwrap(host.bitmapImageRepForCachingDisplay(in: host.bounds)) + host.cacheDisplay(in: host.bounds, to: bitmap) + try XCTUnwrap(bitmap.representation(using: .png, properties: [:])).write(to: url) + } + #endif + + // MARK: - helpers + + private func texts(_ element: ScriptWidgetRuntimeElement?) -> [String] { + guard let element else { return [] } + var found: [String] = [] + for child in element.children ?? [] { + if let text = child as? String { found.append(text) } + if let nested = child as? ScriptWidgetRuntimeElement { found += texts(nested) } + } + return found + } +} + +/// Answers `http://conversation.test/v1/chat/completions` from a queue. +final class ConversationProviderStub: URLProtocol { + enum Reply { + case content(String) + case status(Int) + /// Never answers; the request ends only when it is cancelled. + case hang + } + + private static let lock = NSLock() + private static var queue: [Reply] = [] + private static var recordedBodies: [String] = [] + private static var recordedAuthorizations: [String] = [] + + static var bodies: [String] { lock.withLock { recordedBodies } } + static var authorizations: [String] { lock.withLock { recordedAuthorizations } } + + static func enqueue(_ replies: [Reply]) { + lock.withLock { queue.append(contentsOf: replies) } + } + + static func reset() { + lock.withLock { + queue = [] + recordedBodies = [] + recordedAuthorizations = [] + } + } + + override class func canInit(with request: URLRequest) -> Bool { request.url?.host == "conversation.test" } + override class func canonicalRequest(for request: URLRequest) -> URLRequest { request } + + override func startLoading() { + let body = Self.readBody(request) + // The JSON body escapes the text; undo that so tests can search for it. + let messages = (try? JSONSerialization.jsonObject(with: body) as? [String: Any])?["messages"] as? [[String: Any]] + let text = (messages ?? []).compactMap { $0["content"] as? String }.joined(separator: "\n---\n") + let next: Reply? = Self.lock.withLock { + Self.recordedBodies.append(text) + Self.recordedAuthorizations.append(request.value(forHTTPHeaderField: "Authorization") ?? "") + return Self.queue.isEmpty ? nil : Self.queue.removeFirst() + } + switch next ?? .status(503) { + case .hang: + return + case .status(let code): + respond(code, Data("{}".utf8)) + case .content(let content): + let payload: [String: Any] = [ + "id": "stub", "object": "chat.completion", "created": 1, "model": "stub-model", + "choices": [["index": 0, "message": ["role": "assistant", "content": content], "finish_reason": "stop"]], + "usage": ["prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15], + ] + respond(200, (try? JSONSerialization.data(withJSONObject: payload)) ?? Data()) + } + } + + override func stopLoading() {} + + private func respond(_ status: Int, _ data: Data) { + guard let url = request.url, let response = HTTPURLResponse( + url: url, statusCode: status, httpVersion: nil, headerFields: ["Content-Type": "application/json"] + ) else { return } + client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed) + client?.urlProtocol(self, didLoad: data) + client?.urlProtocolDidFinishLoading(self) + } + + private static func readBody(_ request: URLRequest) -> Data { + if let body = request.httpBody { return body } + guard let stream = request.httpBodyStream else { return Data() } + stream.open() + defer { stream.close() } + var data = Data() + var buffer = [UInt8](repeating: 0, count: 16 * 1024) + while stream.hasBytesAvailable { + let count = stream.read(&buffer, maxLength: buffer.count) + if count <= 0 { break } + data.append(buffer, count: count) + } + return data + } +} diff --git a/docs/README.md b/docs/README.md index 11b1bb4e..77df684f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -17,6 +17,7 @@ Build widgets for iPhone, iPad, Mac, Android, Windows and Linux with JavaScript - [Scheduled widget states](timeline-api.md) — daily transitions, planned dates, read-only future entries and preview. - [Data API capability catalog](data-api-catalog.md) — which systems each API runs on, authorization, sync/async behavior, and editable preview samples. - [AI generation](ai-generate.md) — provider setup, prompting, iteration, privacy, and troubleshooting. +- [AI conversation](ai-conversation.md) — the multi-turn AI window of the Mac app: versions, drafts, what is sent and what is kept. - [AI quality testing](ai-quality-testing.md) — bounded live generation, native snapshots, offline replay, and visual review. - [Apple PCC AI plan](apple-pcc-ai-plan.md) — feasibility, constraints, architecture, and zero-key rollout. - [Skills 1.0](skills.md) — reusable AI guidance, authoring, safe sharing, and package format. diff --git a/docs/ai-conversation.md b/docs/ai-conversation.md new file mode 100644 index 00000000..82587eeb --- /dev/null +++ b/docs/ai-conversation.md @@ -0,0 +1,86 @@ +# AI conversation + +The AI window of the Mac app is a conversation about one widget: describe it, look at the preview, ask for a change, look again. This page says what it does, what it sends to the AI provider and what it keeps on the Mac. Provider setup is in [AI generation](ai-generate.md). + +![The conversation on the left and the preview on the right; the third turn did not run and the widget stayed at version 2](images/ai-conversation-mac.png) + +![The running app after a session with a local stand-in provider: a repaired turn, a turn left behind, and a change that did not run](images/ai-conversation-mac-live.png) + +iPhone and iPad still have the earlier single request with a Refine field; the conversation is planned for them and will use the same shared code. + +## Using it + +1. Open **Generate with AI** from the sidebar menu, the empty window or the File menu. +2. Describe the widget and press Return. The preview on the right shows it as soon as it runs. +3. Ask for a change in your own words, for example "make the title smaller". The preview keeps showing the current widget while the AI works. +4. Every change that runs becomes a numbered version. **Go to This Version** under a turn, or the arrows at the bottom, show an older one. Nothing is sent to the provider when you do that. +5. If you ask for something while an older version is shown, the conversation continues from that version, and the turns after it are marked "left behind" and are no longer sent. +6. **Save Widget** saves the version on screen as a widget. + +The size picker above the preview runs the widget again on the Mac at that size. It costs no tokens. + +The **+** button holds Skills and examples. Skills chosen there apply to every turn of the conversation. They are text added to the request and grant nothing; see [Skills](skills.md). + +## When something goes wrong + +| What happened | What you see | +|---|---| +| The code did not run | The AI is told the error and tries again, up to three times. A turn that needed that says "Fixed automatically". | +| It still did not run | The widget and the preview stay as they were. The turn shows the error, **Try Again** and **Show Its Code**. | +| The provider could not be reached or refused | The turn shows the reason and **Retry**. Everything else stays usable. | +| You pressed Stop (⌘.) | The request ends at once and the widget stays as it was. | +| The app closed during a turn | The turn is shown as interrupted, with **Send Again**. | +| No provider is set up | A notice with a button to the AI settings; nothing can be sent. | + +## Drafts + +A conversation is saved after every change, without asking. One that has not been saved as a widget is a draft: closing the window keeps it, and the window offers it under **Continue a draft** next time. The **…** menu starts a new conversation, opens another draft or deletes the current one. + +Conversations are files in the app's own Application Support folder (`ScriptWidget/AIConversations`), one JSON file each. They stay on this Mac: not in iCloud, not in the app group. The 50 most recent are kept, and each keeps its 30 most recent versions. + +## What is sent to the provider + +Each turn sends one request, plus one for each repair: + +- the fixed instructions and API reference, the same in every turn; +- your earlier requests in this conversation with the AI's one-sentence answer to each, shortened, at most the last 12, and never those left behind; +- the current widget source, and the version before it when it is short, so that "put it back" can work; +- the selected Skills, and your new request. + +A repair also sends the error and the ends of the last ten console lines of the failed run, each cut to 300 characters. A widget can print anything it read, so keep that in mind when a widget handles private data. + +## What is never sent or stored + +- The API key or OAuth token is read from the Keychain for the request header only. It is not written into a conversation file, a prompt, a log, UserDefaults or a widget. +- A conversation file holds your requests, the AI's notes, error summaries and widget sources. It names no provider, server or model. + +## Safety + +Generated code is untrusted. It runs only through the ScriptWidget runtime, in a temporary package, under the same time, memory and API limits as any widget. + +One difference from a saved widget remains: the preview package has no `widget.json`, so it is treated like a legacy package and may use the network, while a widget saved from the conversation gets a new Package 2.0 manifest with no permissions. A generated widget that fetches data therefore works in the preview and needs the `network` permission and its domains added in the editor after saving. The Android app already has the AI declare this; the Apple apps do not yet. + +## Limits + +| | | +|---|---| +| Attempts per turn | 4: one generation and three repairs (3 in all with Apple Private Cloud Compute) | +| Request length | 4,000 characters | +| Widget source kept per version | 256 KB | +| Versions per conversation | 30 | +| Conversations kept | 50 | + +## Not in this version + +Streaming output, thumbnails of several sizes at once, the same conversation in the editor's Copilot panel, targeted edits instead of whole-file replies, a view of what changed between versions, and the iPhone and iPad interface. + +## For contributors + +- `Shared/ScriptWidgetRuntime/AI/AIConversation.swift`: the saved model and its limits. +- `AIConversationStore.swift`: the files. +- `AIConversationSession.swift`: the state a view observes; it sends turns through `AgentLoop` in its `turn` mode. +- `PromptBuilder.userPromptTurn` and `parseTurnReply`: the request and reply format. +- `macOS/ScriptWidgetMac/AIGenerate/AIGenerateWindowView.swift`: the window. +- `Tests/ScriptWidgetRuntimeTests/AIConversationTests.swift`: tests against a stub provider. They need no key and make no network request. +- `testThreeTurnsAgainstARealProviderWhenConfigured` sends three turns to a real provider when `ROCKY_OPENAI_APIKEY`, `ROCKY_OPENAI_BASEURL` and `ROCKY_OPENAI_MODEL` are set, as in [AI quality testing](ai-quality-testing.md). It costs tokens and is skipped otherwise. `AI_CONVERSATION_LIVE_DIR` names a folder for the three sources and a short report. Pass the key through the environment; never write it into a command or a file of the repository. +- The picture on this page is written by the same test file when `AI_CONVERSATION_SNAPSHOT_DIR` names a folder (prefix it with `TEST_RUNNER_` for `xcodebuild`). It draws the window with a prepared conversation; it does not click through it. diff --git a/docs/ai-generate.md b/docs/ai-generate.md index 51d4b1f4..44853121 100644 --- a/docs/ai-generate.md +++ b/docs/ai-generate.md @@ -1,5 +1,7 @@ # AI Generate — 设计文档 +Mac 上的多轮对话(版本、草稿、每轮发送的内容)见 [AI conversation](ai-conversation.md);本文其余部分描述的是最初的单次生成设计,iPhone 和 iPad 目前仍是这种方式。 + 生成效果回归:见 [AI quality testing](ai-quality-testing.md),包含真实模型生成、深浅色原生截图、OCR 提示和无 API 调用的重放对照。 ScriptWidget 默认使用 Apple Private Cloud Compute (PCC) 生成 Widget,不要求用户提供 API key。用户输入自然语言 prompt 后,模型生成 JSX,App 在本机 runtime 中自动“跑—看错—修”,最后进入审阅和预览态,由用户确认落盘。OpenAI 及兼容端点继续作为可选后端。 diff --git a/docs/images/ai-conversation-mac-empty.png b/docs/images/ai-conversation-mac-empty.png new file mode 100644 index 00000000..d7fe6a50 Binary files /dev/null and b/docs/images/ai-conversation-mac-empty.png differ diff --git a/docs/images/ai-conversation-mac-live.png b/docs/images/ai-conversation-mac-live.png new file mode 100644 index 00000000..5e098aae Binary files /dev/null and b/docs/images/ai-conversation-mac-live.png differ diff --git a/docs/images/ai-conversation-mac.png b/docs/images/ai-conversation-mac.png new file mode 100644 index 00000000..ba8bb9d4 Binary files /dev/null and b/docs/images/ai-conversation-mac.png differ diff --git a/iOS/ScriptWidget.xcodeproj/project.pbxproj b/iOS/ScriptWidget.xcodeproj/project.pbxproj index 11d11899..9989b7c1 100644 --- a/iOS/ScriptWidget.xcodeproj/project.pbxproj +++ b/iOS/ScriptWidget.xcodeproj/project.pbxproj @@ -56,6 +56,9 @@ A102000500000000000000B0 /* AgentRuntimeBridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = A101000500000000000000B0 /* AgentRuntimeBridge.swift */; }; A102000600000000000000B0 /* AgentLoop.swift in Sources */ = {isa = PBXBuildFile; fileRef = A101000600000000000000B0 /* AgentLoop.swift */; }; A102000700000000000000B0 /* AIGenerateSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = A101000700000000000000B0 /* AIGenerateSession.swift */; }; + A102002300000000000000B0 /* AIConversationSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = A101002300000000000000B0 /* AIConversationSession.swift */; }; + A102002200000000000000B0 /* AIConversationStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = A101002200000000000000B0 /* AIConversationStore.swift */; }; + A102002100000000000000B0 /* AIConversation.swift in Sources */ = {isa = PBXBuildFile; fileRef = A101002100000000000000B0 /* AIConversation.swift */; }; A102000800000000000000B0 /* AIGenerateProgressView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A101000800000000000000B0 /* AIGenerateProgressView.swift */; }; A102000900000000000000B0 /* AIExamplePrompts.swift in Sources */ = {isa = PBXBuildFile; fileRef = A101000900000000000000B0 /* AIExamplePrompts.swift */; }; A102000A00000000000000B0 /* AIKeychain.swift in Sources */ = {isa = PBXBuildFile; fileRef = A101000A00000000000000B0 /* AIKeychain.swift */; }; @@ -83,6 +86,7 @@ A27000012F270001008B90A2 /* ScriptWidgetAppIntent.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0C7ECF62AD0759F0039D30D /* ScriptWidgetAppIntent.swift */; }; A27000022F270001008B90A2 /* AppConfigFrequency.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0C7ECF72AD0759F0039D30D /* AppConfigFrequency.swift */; }; B165542F120D2E511D4012B9 /* AIGenerationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A6FE5687E8CD01E2ED9C23C /* AIGenerationTests.swift */; }; + A102003100000000000000B0 /* AIConversationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A101003100000000000000B0 /* AIConversationTests.swift */; }; BFD2727EF994405AAC85396D /* ReloadWidgetAppIntent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 750919EB639B4EB3B07D27FC /* ReloadWidgetAppIntent.swift */; }; C803FC625CDC9D9CC69F403D /* ScriptWidgetRuntimeStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9CAE338C670B4A70B52A6CFD /* ScriptWidgetRuntimeStorage.swift */; }; ED7E5FDCF5026E15675F9A4E /* ScriptWidgetStorageLock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 99619D8E19192386E982EC46 /* ScriptWidgetStorageLock.swift */; }; @@ -537,6 +541,7 @@ 8796038A2FE7F817F67A2D52 /* ScriptWidgetServiceCredentialVault.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScriptWidgetServiceCredentialVault.swift; sourceTree = ""; }; 3ABFAA06102A6639A8AECACE /* ScriptWidgetServiceAuthorization.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScriptWidgetServiceAuthorization.swift; sourceTree = ""; }; 6A6FE5687E8CD01E2ED9C23C /* AIGenerationTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = AIGenerationTests.swift; path = ../Tests/ScriptWidgetRuntimeTests/AIGenerationTests.swift; sourceTree = ""; }; + A101003100000000000000B0 /* AIConversationTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = AIConversationTests.swift; path = ../Tests/ScriptWidgetRuntimeTests/AIConversationTests.swift; sourceTree = ""; }; 750919EB639B4EB3B07D27FC /* ReloadWidgetAppIntent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReloadWidgetAppIntent.swift; sourceTree = ""; }; 95D38A9C0960C28615553017 /* HealthKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = HealthKit.framework; path = System/Library/Frameworks/HealthKit.framework; sourceTree = SDKROOT; }; 99DD0AE0F4C799D5DFEE8E76 /* PackageCacheTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = PackageCacheTests.swift; path = ../Tests/ScriptWidgetRuntimeTests/PackageCacheTests.swift; sourceTree = ""; }; @@ -550,6 +555,9 @@ A101000500000000000000B0 /* AgentRuntimeBridge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AgentRuntimeBridge.swift; sourceTree = ""; }; A101000600000000000000B0 /* AgentLoop.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AgentLoop.swift; sourceTree = ""; }; A101000700000000000000B0 /* AIGenerateSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIGenerateSession.swift; sourceTree = ""; }; + A101002300000000000000B0 /* AIConversationSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIConversationSession.swift; sourceTree = ""; }; + A101002200000000000000B0 /* AIConversationStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIConversationStore.swift; sourceTree = ""; }; + A101002100000000000000B0 /* AIConversation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIConversation.swift; sourceTree = ""; }; A101000800000000000000B0 /* AIGenerateProgressView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIGenerateProgressView.swift; sourceTree = ""; }; A101000900000000000000B0 /* AIExamplePrompts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIExamplePrompts.swift; sourceTree = ""; }; A101000A00000000000000B0 /* AIKeychain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIKeychain.swift; sourceTree = ""; }; @@ -933,6 +941,7 @@ FF7F47EEACACEA82A0E6BB0F /* ServiceAuthorizationTests.swift */, 8B54AAAD0C7EFDF340B91B82 /* RuntimeDeliveryTests.swift */, 6A6FE5687E8CD01E2ED9C23C /* AIGenerationTests.swift */, + A101003100000000000000B0 /* AIConversationTests.swift */, E9ABF8F095487FE21DB73700 /* RuntimeExecutionTests.swift */, 4504B2C47E011F6AE74734CD /* BrowserPreviewParityTests.swift */, 99DD0AE0F4C799D5DFEE8E76 /* PackageCacheTests.swift */, @@ -951,6 +960,9 @@ A101000500000000000000B0 /* AgentRuntimeBridge.swift */, A101000600000000000000B0 /* AgentLoop.swift */, A101000700000000000000B0 /* AIGenerateSession.swift */, + A101002300000000000000B0 /* AIConversationSession.swift */, + A101002200000000000000B0 /* AIConversationStore.swift */, + A101002100000000000000B0 /* AIConversation.swift */, A101000800000000000000B0 /* AIGenerateProgressView.swift */, A101000900000000000000B0 /* AIExamplePrompts.swift */, A101001000000000000000B0 /* AIWidgetSkills.swift */, @@ -2041,6 +2053,7 @@ files = ( A120000600000000000000B0 /* WebStudioServerTests.swift in Sources */, B165542F120D2E511D4012B9 /* AIGenerationTests.swift in Sources */, + A102003100000000000000B0 /* AIConversationTests.swift in Sources */, 84C1353FBC065F971E23478C /* PackageCacheTests.swift in Sources */, 0A3A531AF5AC4D499D95C0F1 /* SystemServiceTests.swift in Sources */, CE64AF58A011259D931CFF8F /* RuntimeExecutionTests.swift in Sources */, @@ -2083,6 +2096,9 @@ A102000500000000000000B0 /* AgentRuntimeBridge.swift in Sources */, A102000600000000000000B0 /* AgentLoop.swift in Sources */, A102000700000000000000B0 /* AIGenerateSession.swift in Sources */, + A102002300000000000000B0 /* AIConversationSession.swift in Sources */, + A102002200000000000000B0 /* AIConversationStore.swift in Sources */, + A102002100000000000000B0 /* AIConversation.swift in Sources */, A102000800000000000000B0 /* AIGenerateProgressView.swift in Sources */, A104000100000000000000B0 /* SettingAIView.swift in Sources */, A104000200000000000000B0 /* AIGenerateView.swift in Sources */, diff --git a/macOS/ScriptWidgetMac.xcodeproj/project.pbxproj b/macOS/ScriptWidgetMac.xcodeproj/project.pbxproj index 28bbf69e..5a8e1b1c 100644 --- a/macOS/ScriptWidgetMac.xcodeproj/project.pbxproj +++ b/macOS/ScriptWidgetMac.xcodeproj/project.pbxproj @@ -54,6 +54,7 @@ 855878C6168D59CE4C6CB13C /* ScriptWidgetRuntimeSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E3B593386F820DCF99C631E /* ScriptWidgetRuntimeSystem.swift */; }; 966888252E454500AEA7852C /* ScriptWidgetRuntimeLocation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 124FAA64786342F5A3432631 /* ScriptWidgetRuntimeLocation.swift */; }; A10633E3A2F1C708E2078A33 /* AIGenerationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28F68F47C00CF93E4CBA1F1E /* AIGenerationTests.swift */; }; + A202003100000000000000B0 /* AIConversationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201003100000000000000B0 /* AIConversationTests.swift */; }; A20000112F200001008B90A2 /* ClockHandRotationEffect.swift in Sources */ = {isa = PBXBuildFile; fileRef = A20000102F200001008B90A2 /* ClockHandRotationEffect.swift */; }; A20000122F200001008B90A2 /* ClockHandRotationEffect.swift in Sources */ = {isa = PBXBuildFile; fileRef = A20000102F200001008B90A2 /* ClockHandRotationEffect.swift */; }; A202000100000000000000B0 /* AISettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201000100000000000000B0 /* AISettings.swift */; }; @@ -63,6 +64,9 @@ A202000500000000000000B0 /* AgentRuntimeBridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201000500000000000000B0 /* AgentRuntimeBridge.swift */; }; A202000600000000000000B0 /* AgentLoop.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201000600000000000000B0 /* AgentLoop.swift */; }; A202000700000000000000B0 /* AIGenerateSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201000700000000000000B0 /* AIGenerateSession.swift */; }; + A202002300000000000000B0 /* AIConversationSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201002300000000000000B0 /* AIConversationSession.swift */; }; + A202002200000000000000B0 /* AIConversationStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201002200000000000000B0 /* AIConversationStore.swift */; }; + A202002100000000000000B0 /* AIConversation.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201002100000000000000B0 /* AIConversation.swift */; }; A202000800000000000000B0 /* AIGenerateProgressView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201000800000000000000B0 /* AIGenerateProgressView.swift */; }; A202000900000000000000B0 /* AIExamplePrompts.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201000900000000000000B0 /* AIExamplePrompts.swift */; }; A202000A00000000000000B0 /* AIKeychain.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201000A00000000000000B0 /* AIKeychain.swift */; }; @@ -340,6 +344,7 @@ 1492D936BBC23E9D0195DE26 /* RuntimeExecutionTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = RuntimeExecutionTests.swift; path = ../Tests/ScriptWidgetRuntimeTests/RuntimeExecutionTests.swift; sourceTree = ""; }; A43E23B1BB04A5AC18DEC1B1 /* BrowserPreviewParityTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = BrowserPreviewParityTests.swift; path = ../Tests/ScriptWidgetRuntimeTests/BrowserPreviewParityTests.swift; sourceTree = ""; }; 28F68F47C00CF93E4CBA1F1E /* AIGenerationTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = AIGenerationTests.swift; path = ../Tests/ScriptWidgetRuntimeTests/AIGenerationTests.swift; sourceTree = ""; }; + A201003100000000000000B0 /* AIConversationTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = AIConversationTests.swift; path = ../Tests/ScriptWidgetRuntimeTests/AIConversationTests.swift; sourceTree = ""; }; 539EC132F05468E207A652AD /* ScriptWidgetRuntimeTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ScriptWidgetRuntimeTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 5A89B7992F0FB0A0008B90A2 /* ScriptWidgetElementTagGif.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScriptWidgetElementTagGif.swift; sourceTree = ""; }; 5A89B79C2F0FB0B5008B90A2 /* SwingAnimationModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwingAnimationModifier.swift; sourceTree = ""; }; @@ -367,6 +372,9 @@ A201000500000000000000B0 /* AgentRuntimeBridge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AgentRuntimeBridge.swift; sourceTree = ""; }; A201000600000000000000B0 /* AgentLoop.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AgentLoop.swift; sourceTree = ""; }; A201000700000000000000B0 /* AIGenerateSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIGenerateSession.swift; sourceTree = ""; }; + A201002300000000000000B0 /* AIConversationSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIConversationSession.swift; sourceTree = ""; }; + A201002200000000000000B0 /* AIConversationStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIConversationStore.swift; sourceTree = ""; }; + A201002100000000000000B0 /* AIConversation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIConversation.swift; sourceTree = ""; }; A201000800000000000000B0 /* AIGenerateProgressView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIGenerateProgressView.swift; sourceTree = ""; }; A201000900000000000000B0 /* AIExamplePrompts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIExamplePrompts.swift; sourceTree = ""; }; A201000A00000000000000B0 /* AIKeychain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIKeychain.swift; sourceTree = ""; }; @@ -584,6 +592,9 @@ A201000500000000000000B0 /* AgentRuntimeBridge.swift */, A201000600000000000000B0 /* AgentLoop.swift */, A201000700000000000000B0 /* AIGenerateSession.swift */, + A201002300000000000000B0 /* AIConversationSession.swift */, + A201002200000000000000B0 /* AIConversationStore.swift */, + A201002100000000000000B0 /* AIConversation.swift */, A201000800000000000000B0 /* AIGenerateProgressView.swift */, A201000900000000000000B0 /* AIExamplePrompts.swift */, A201001000000000000000B0 /* AIWidgetSkills.swift */, @@ -675,6 +686,7 @@ 3691AEA892C0F60DB0825F64 /* ServiceAuthorizationTests.swift */, 6DBFC205E7AE664442A28C84 /* RuntimeDeliveryTests.swift */, 28F68F47C00CF93E4CBA1F1E /* AIGenerationTests.swift */, + A201003100000000000000B0 /* AIConversationTests.swift */, 1492D936BBC23E9D0195DE26 /* RuntimeExecutionTests.swift */, A43E23B1BB04A5AC18DEC1B1 /* BrowserPreviewParityTests.swift */, 7CF0C59766F5837DEC033FB9 /* PackageCacheTests.swift */, @@ -1330,6 +1342,7 @@ buildActionMask = 2147483647; files = ( A10633E3A2F1C708E2078A33 /* AIGenerationTests.swift in Sources */, + A202003100000000000000B0 /* AIConversationTests.swift in Sources */, C43EFB088718FE26BF9B393F /* PackageCacheTests.swift in Sources */, DD4971B1EBB047D7A8B6AA34 /* SystemServiceTests.swift in Sources */, EBAE7DF1351F9A6ECE2E2AE2 /* RuntimeExecutionTests.swift in Sources */, @@ -1374,6 +1387,9 @@ A202000500000000000000B0 /* AgentRuntimeBridge.swift in Sources */, A202000600000000000000B0 /* AgentLoop.swift in Sources */, A202000700000000000000B0 /* AIGenerateSession.swift in Sources */, + A202002300000000000000B0 /* AIConversationSession.swift in Sources */, + A202002200000000000000B0 /* AIConversationStore.swift in Sources */, + A202002100000000000000B0 /* AIConversation.swift in Sources */, A202000800000000000000B0 /* AIGenerateProgressView.swift in Sources */, A204000100000000000000B0 /* SettingAIView.swift in Sources */, A204000200000000000000B0 /* AIGenerateWindowView.swift in Sources */, diff --git a/macOS/ScriptWidgetMac/AIGenerate/AIGenerateWindowView.swift b/macOS/ScriptWidgetMac/AIGenerate/AIGenerateWindowView.swift index 40349d3c..2804835e 100644 --- a/macOS/ScriptWidgetMac/AIGenerate/AIGenerateWindowView.swift +++ b/macOS/ScriptWidgetMac/AIGenerate/AIGenerateWindowView.swift @@ -2,9 +2,10 @@ // AIGenerateWindowView.swift // ScriptWidgetMac // -// The AI Generate experience for macOS, hosted inside a sheet. Shows -// prompt input, progress, and — once a widget is produced — an inline -// preview with refine / discard / save actions. +// The AI widget conversation for macOS, hosted in a sheet: the conversation +// on the left, the live preview on the right. Each turn that runs becomes a +// version the user can go back to, and the conversation is kept as a draft +// until it is saved as a widget. // import SwiftUI @@ -13,33 +14,60 @@ import WidgetKit struct AIGenerateWindowView: View { static let openRequestNotification = Notification.Name("AIGenerateWindowViewOpenRequest") - @Environment(\.dismiss) private var dismiss + /// A new session is made whenever the user starts or opens a conversation. + @State private var conversation: AIConversation + @State private var sessionKey = UUID() + + /// Opens on a new conversation, or on one that was saved earlier. + init(conversation: AIConversation = AIConversation()) { + _conversation = State(initialValue: conversation) + } + + var body: some View { + AIConversationWindowContent( + conversation: conversation, + onOpen: { opened in + conversation = opened + sessionKey = UUID() + } + ) + .id(sessionKey) + } +} - @StateObject private var session = AIGenerateSession() +private struct AIConversationWindowContent: View { + let onOpen: (AIConversation) -> Void + + @Environment(\.dismiss) private var dismiss + @StateObject private var session: AIConversationSession - @State private var prompt: String = "" - @State private var refineInstruction: String = "" - @State private var saveName: String = "" + @State private var draftText = "" + @State private var saveName = "" @State private var saveError: String? - @State private var showingCode: Bool = false - @State private var showingLogs: Bool = false - @State private var isDebugMode: Bool = false - @State private var previewPackage: ScriptWidgetPackage? + @State private var shownCode: ShownCode? + @State private var showingLogs = false + @State private var isDebugMode = false @State private var profiles: [AIProfile] = [] - @State private var activeProfileID: String = "" - @State private var selectedSkillIDs: Set = [] + @State private var activeProfileID = "" + @State private var isConfigured = true @State private var skills: [AIWidgetSkill] = AIWidgetSkills.builtIns + @State private var drafts: [AIConversationSummary] = [] + @FocusState private var composerFocused: Bool - private var jsx: String { session.lastJSX ?? "" } - private var hasResult: Bool { - switch session.phase { - case .done, .exhausted: return true - default: return false - } + private struct ShownCode: Identifiable { + let id = UUID() + let title: String + let code: String } - private var isExhausted: Bool { - if case .exhausted = session.phase { return true } - return false + + init(conversation: AIConversation, onOpen: @escaping (AIConversation) -> Void) { + self.onOpen = onOpen + _session = StateObject(wrappedValue: AIConversationSession(conversation: conversation)) + } + + private var conversation: AIConversation { session.conversation } + private var sortedVersions: [AIConversationVersion] { + conversation.versions.sorted { $0.number < $1.number } } var body: some View { @@ -47,33 +75,31 @@ struct AIGenerateWindowView: View { header Divider() HSplitView { - inputSide - .frame(minWidth: 300, idealWidth: 380) + chatSide + .frame(minWidth: 320, idealWidth: 400) previewSide .frame(minWidth: 300, idealWidth: 420) } Divider() footer } - .frame(idealWidth: 860, minHeight: 520, idealHeight: 620) - .frame(minWidth: 720) + .frame(idealWidth: 900, minHeight: 540, idealHeight: 640) + .frame(minWidth: 760) .onAppear { - ensurePreviewPackage() - prefillSaveNameIfNeeded() - loadProfiles() + reloadSettings() skills = AIWidgetSkillManager.shared.allSkills() + reloadDrafts() + if conversation.currentVersion != nil { prefillSaveNameIfNeeded() } + composerFocused = true } + .onDisappear { session.cancel() } .onReceive(NotificationCenter.default.publisher(for: AISettingsStore.changedNotification)) { _ in - loadProfiles() + reloadSettings() } .onReceive(NotificationCenter.default.publisher(for: AIWidgetSkillManager.changedNotification)) { _ in skills = AIWidgetSkillManager.shared.allSkills() } - .onChange(of: jsx) { - refreshPreviewPackage() - prefillSaveNameIfNeeded() - } - .sheet(isPresented: $showingCode) { codeSheet } + .sheet(item: $shownCode) { codeSheet($0) } .sheet(isPresented: $showingLogs) { logsSheet } .alert("Save Failed", isPresented: saveErrorBinding) { Button("OK") { saveError = nil } @@ -82,7 +108,7 @@ struct AIGenerateWindowView: View { } } - // MARK: - layout + // MARK: - header private var header: some View { HStack(spacing: 10) { @@ -90,125 +116,398 @@ struct AIGenerateWindowView: View { .font(.title2) .foregroundStyle(.tint) VStack(alignment: .leading, spacing: 2) { - Text("AI Widget Generator").font(.title3.weight(.semibold)) - Text("Describe what you want; the AI will iterate until the widget runs.") + HStack(spacing: 6) { + Text(conversation.title.isEmpty ? "New AI Widget" : conversation.title) + .font(.title3.weight(.semibold)) + .lineLimit(1) + if !conversation.turns.isEmpty { + Text(conversation.isDraft ? "Draft" : "Saved") + .font(.caption) + .padding(.horizontal, 6) + .padding(.vertical, 2) + .background(.quaternary, in: .capsule) + } + } + Text("Describe a widget, then keep asking for changes.") .font(.footnote) .foregroundStyle(.secondary) + .lineLimit(1) } Spacer() + if profiles.count > 1 { + Picker("Provider", selection: $activeProfileID) { + ForEach(profiles) { profile in + Text(profile.name.isEmpty ? "Unnamed" : profile.name).tag(profile.id) + } + } + .labelsHidden() + .frame(maxWidth: 180) + .disabled(session.isRunning) + .onChange(of: activeProfileID) { _, newValue in + AISettingsStore.shared.setActiveProfile(id: newValue) + } + } + Menu { + Button("New Conversation", systemImage: "plus") { onOpen(AIConversation()) } + .disabled(conversation.turns.isEmpty) + if !otherDrafts.isEmpty { + Section("Drafts") { + ForEach(otherDrafts) { draft in + Button(draftLabel(draft)) { open(draft) } + } + } + } + if !conversation.turns.isEmpty { + Divider() + Button("Delete This Conversation", systemImage: "trash", role: .destructive) { + session.cancel() + AIConversationStore.shared.delete(id: conversation.id) + onOpen(AIConversation()) + } + } + } label: { + Image(systemName: "ellipsis.circle") + } + .menuStyle(.borderlessButton) + .fixedSize() + .accessibilityLabel("Conversation options") Button("Close") { dismiss() } .keyboardShortcut(.cancelAction) } .padding(12) } - private var inputSide: some View { - ScrollView { - VStack(alignment: .leading, spacing: 12) { - Text("Prompt").font(.headline) - TextEditor(text: $prompt) - .font(.body) - .frame(minHeight: 140) - .padding(StudioDesign.compactSpacing) - .background(Color(nsColor: .textBackgroundColor), in: .rect(cornerRadius: StudioDesign.controlCornerRadius)) - .overlay { - RoundedRectangle(cornerRadius: StudioDesign.controlCornerRadius, style: .continuous) - .stroke(StudioDesign.separator, lineWidth: 1) - } - - examplesSection - skillsSection + // MARK: - conversation - if profiles.count > 1 { - HStack { - Text("Profile") - Picker("", selection: $activeProfileID) { - ForEach(profiles) { profile in - Text(profile.name.isEmpty ? "Unnamed" : profile.name).tag(profile.id) - } + private var chatSide: some View { + VStack(spacing: 0) { + ScrollViewReader { proxy in + ScrollView { + VStack(alignment: .leading, spacing: 14) { + if !isConfigured { + notConfiguredNotice + } else if conversation.turns.isEmpty { + emptyState } - .labelsHidden() - .onChange(of: activeProfileID) { _, newValue in - AISettingsStore.shared.setActiveProfile(id: newValue) + ForEach(conversation.turns) { turn in + turnView(turn) + .id(turn.id) } + Color.clear.frame(height: 1).id("bottom") } + .padding(12) + .frame(maxWidth: .infinity, alignment: .leading) + } + .onAppear { proxy.scrollTo("bottom", anchor: .bottom) } + .onChange(of: conversation.turns.count) { _, _ in + withAnimation { proxy.scrollTo("bottom", anchor: .bottom) } } + .onChange(of: session.activity) { _, _ in + proxy.scrollTo("bottom", anchor: .bottom) + } + } + Divider() + composer + } + } + + private var notConfiguredNotice: some View { + VStack(alignment: .leading, spacing: 8) { + Label("Set up an AI provider first", systemImage: "exclamationmark.triangle") + .font(.headline) + Text("The selected provider has no server, model or key yet. Keys are kept in the Keychain.") + .font(.callout) + .foregroundStyle(.secondary) + SettingsLink { + Text("Open AI Settings") + } + } + .studioCard() + } - HStack { - Text("Size") - Picker("", selection: $session.size) { - ForEach(AIWidgetSize.allCases) { size in - Text(size.displayName).tag(size) + private var emptyState: some View { + VStack(alignment: .leading, spacing: 14) { + VStack(alignment: .leading, spacing: 6) { + Text("Try an example") + .font(.caption) + .foregroundStyle(.secondary) + FlowButtons(items: AIExamplePrompts.all) { example in + Button { + draftText = example.prompt + session.setSize(example.size) + composerFocused = true + } label: { + Label(example.title, systemImage: example.symbol) + .font(.caption) + } + .buttonStyle(.bordered) + .controlSize(.small) + } + } + if !drafts.isEmpty { + VStack(alignment: .leading, spacing: 6) { + Text("Continue a draft") + .font(.caption) + .foregroundStyle(.secondary) + ForEach(drafts.prefix(5)) { draft in + HStack { + Button { open(draft) } label: { + HStack { + Image(systemName: "doc.text") + Text(draft.title.isEmpty ? "Untitled" : draft.title) + .lineLimit(1) + Spacer() + Text(draft.updatedAt, format: .relative(presentation: .named)) + .font(.caption) + .foregroundStyle(.secondary) + } + .contentShape(.rect) + } + .buttonStyle(.plain) + Button { + AIConversationStore.shared.delete(id: draft.id) + reloadDrafts() + } label: { + Image(systemName: "trash") + } + .buttonStyle(.borderless) + .accessibilityLabel("Delete draft \(draft.title)") } } - .labelsHidden() } + .studioCard(padding: 10) + } + } + } - Button { - session.start(userDescription: AIWidgetSkills.augment(prompt, selectedIDs: selectedSkillIDs)) - } label: { - HStack { - Image(systemName: "sparkles") - Text(session.isRunning ? "Generating..." : "Generate") - } - .frame(maxWidth: .infinity) + @ViewBuilder + private func turnView(_ turn: AIConversationTurn) -> some View { + VStack(alignment: .leading, spacing: 8) { + HStack { + Spacer(minLength: 40) + Text(turn.request) + .textSelection(.enabled) + .padding(.horizontal, 10) + .padding(.vertical, 7) + .background(Color.accentColor.opacity(0.16), in: .rect(cornerRadius: StudioDesign.cardCornerRadius)) + } + VStack(alignment: .leading, spacing: 6) { + turnOutcome(turn) + if turn.usage.totalTokens > 0 { + Text("\(turn.usage.totalTokens) tokens") + .font(.caption2) + .foregroundStyle(.tertiary) } - .buttonStyle(.borderedProminent) - .disabled(session.isRunning || prompt.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) + } + .frame(maxWidth: .infinity, alignment: .leading) + } + .opacity(turn.isAbandoned ? 0.5 : 1) + } - if session.isRunning || hasResult { - AIGenerateProgressView(session: session) + @ViewBuilder + private func turnOutcome(_ turn: AIConversationTurn) -> some View { + switch turn.status { + case .running: + HStack(spacing: 8) { + ProgressView().controlSize(.small) + Text(activityText) + .font(.callout) + .foregroundStyle(.secondary) + Spacer() + Button("Stop", systemImage: "stop.circle") { session.cancel() } + .controlSize(.small) + .keyboardShortcut(".", modifiers: .command) + } + case .succeeded: + Text(turn.note ?? "Done.") + .textSelection(.enabled) + if turn.repairs > 0 { + Label("Fixed automatically \(turn.repairs) time\(turn.repairs == 1 ? "" : "s")", systemImage: "wrench.and.screwdriver") + .font(.caption) + .foregroundStyle(.secondary) + } + versionLine(turn) + case .unconverged: + Label("This change did not run, so the widget was left as it was.", systemImage: "exclamationmark.triangle.fill") + .foregroundStyle(.orange) + if let error = turn.errorSummary { + Text(error) + .font(.caption.monospaced()) + .foregroundStyle(.secondary) + .lineLimit(4) + .textSelection(.enabled) + } + HStack { + retryButton(turn, title: "Try Again") + if let code = turn.failedSource, !code.isEmpty { + Button("Show Its Code") { shownCode = ShownCode(title: "Code That Did Not Run", code: code) } + .controlSize(.small) } + } + case .failed: + Label(turn.errorSummary ?? "The AI provider could not complete the request.", systemImage: "wifi.exclamationmark") + .foregroundStyle(.red) + .textSelection(.enabled) + retryButton(turn, title: "Retry") + case .cancelled: + Label("Stopped.", systemImage: "stop.circle") + .foregroundStyle(.secondary) + retryButton(turn, title: "Send Again") + case .interrupted: + Label("Interrupted when the app closed.", systemImage: "moon.zzz") + .foregroundStyle(.secondary) + retryButton(turn, title: "Send Again") + } + } - if hasResult { - Divider().padding(.vertical, 4) - Text("Refine").font(.headline) - Text("Ask the AI to change something — it will iterate again on top of the current code.") + @ViewBuilder + private func versionLine(_ turn: AIConversationTurn) -> some View { + if let version = conversation.version(turn.versionID) { + let isCurrent = version.id == conversation.currentVersionID + HStack(spacing: 8) { + Label("Version \(version.number)", systemImage: isCurrent ? "checkmark.circle.fill" : "checkmark.circle") + .font(.caption.weight(.medium)) + .foregroundStyle(isCurrent ? AnyShapeStyle(.tint) : AnyShapeStyle(.secondary)) + if isCurrent { + Text("shown now") + .font(.caption) + .foregroundStyle(.secondary) + } else { + Button("Go to This Version") { session.selectVersion(version.id) } + .buttonStyle(.link) + .font(.caption) + .disabled(session.isRunning) + } + if turn.isAbandoned { + Text("left behind") .font(.caption) .foregroundStyle(.secondary) - HStack { - TextField("e.g. use a darker background", text: $refineInstruction) - .textFieldStyle(.roundedBorder) - Button { - let instruction = refineInstruction - refineInstruction = "" - session.refine(currentCode: jsx, refineInstruction: instruction) - } label: { - Image(systemName: "arrow.right.circle.fill") + } + } + } + } + + private func retryButton(_ turn: AIConversationTurn, title: LocalizedStringKey) -> some View { + Button(title, systemImage: "arrow.clockwise") { session.retry(turnID: turn.id) } + .controlSize(.small) + .disabled(session.isRunning || !isConfigured) + } + + private var activityText: String { + switch session.activity { + case .idle: return "" + case .thinking: return "Writing the widget…" + case .running: return "Running it…" + case .repairing(let attempt, _): return "It did not run. Fixing, attempt \(attempt) of \(AIConversationLimits.maxAttemptsPerTurn)…" + } + } + + private var composer: some View { + HStack(alignment: .bottom, spacing: 8) { + Menu { + Section("Skills for this conversation") { + ForEach(skills) { skill in + Toggle(isOn: skillBinding(skill.id)) { + Label(skill.title, systemImage: skill.symbol) } - .disabled(jsx.isEmpty || refineInstruction.trimmingCharacters(in: .whitespaces).isEmpty || session.isRunning) } } + Button("Manage Skills…", systemImage: "slider.horizontal.3") { + NotificationCenter.default.post(name: SkillManagerOpenRequest.notification, object: nil) + } + Section("Examples") { + ForEach(AIExamplePrompts.all) { example in + Button(example.title, systemImage: example.symbol) { + draftText = example.prompt + if conversation.versions.isEmpty { session.setSize(example.size) } + } + } + } + } label: { + Image(systemName: conversation.skillIDs.isEmpty ? "plus.circle" : "plus.circle.fill") + .font(.title3) + } + .menuStyle(.borderlessButton) + .fixedSize() + .accessibilityLabel("Skills and examples") + .help(conversation.skillIDs.isEmpty ? "Skills and examples" : "\(conversation.skillIDs.count) skills on") + + TextField( + conversation.versions.isEmpty ? "Describe the widget you want…" : "Say what to change…", + text: $draftText, + axis: .vertical + ) + .lineLimit(1...6) + .textFieldStyle(.plain) + .padding(8) + .background(Color(nsColor: .textBackgroundColor), in: .rect(cornerRadius: StudioDesign.controlCornerRadius)) + .overlay { + RoundedRectangle(cornerRadius: StudioDesign.controlCornerRadius, style: .continuous) + .stroke(StudioDesign.separator, lineWidth: 1) } - .padding(12) + .focused($composerFocused) + .onSubmit(sendDraft) + .accessibilityLabel("Message to the AI") + + Button(action: sendDraft) { + Image(systemName: "arrow.up.circle.fill") + .font(.title2) + } + .buttonStyle(.plain) + .foregroundStyle(canSend ? AnyShapeStyle(.tint) : AnyShapeStyle(.tertiary)) + .disabled(!canSend) + .accessibilityLabel("Send") } + .padding(10) + } + + private var canSend: Bool { + isConfigured && !session.isRunning + && !draftText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty } + private func sendDraft() { + guard canSend else { return } + let text = draftText + draftText = "" + session.send(text) + } + + // MARK: - preview + private var previewSide: some View { VStack(alignment: .leading, spacing: 10) { HStack { Text("Preview").font(.headline) + Picker("Size", selection: sizeBinding) { + ForEach(AIWidgetSize.allCases) { size in + Text(size.displayName).tag(size) + } + } + .labelsHidden() + .frame(maxWidth: 190) + .help("Changing the size runs the widget again on this Mac. Nothing is sent to the AI.") Spacer() Toggle("Debug", isOn: $isDebugMode) .toggleStyle(.switch) .controlSize(.small) } - if isExhausted { - HStack(spacing: 6) { - Image(systemName: "exclamationmark.triangle.fill") - Text("Did not fully converge — showing the last attempt.") - .font(.caption) - } - .foregroundStyle(.orange) - } - - ZStack { + ZStack(alignment: .topTrailing) { StudioDesign.cardBackground previewContent + .frame(maxWidth: .infinity, maxHeight: .infinity) + if session.isRunning, session.previewElement != nil { + Label("Showing version \(conversation.currentVersion?.number ?? 0) while the AI works", systemImage: "clock") + .font(.caption) + .padding(.horizontal, 8) + .padding(.vertical, 4) + .background(.regularMaterial, in: .capsule) + .padding(8) + } } .frame(maxWidth: .infinity) - .frame(height: 360) + .frame(minHeight: 360) .clipShape(.rect(cornerRadius: StudioDesign.cardCornerRadius)) .overlay { RoundedRectangle(cornerRadius: StudioDesign.cardCornerRadius, style: .continuous) @@ -216,151 +515,117 @@ struct AIGenerateWindowView: View { } HStack { - Button { showingCode = true } label: { - Label("Code", systemImage: "curlybraces") - } - Button { showingLogs = true } label: { - Label("Logs", systemImage: "text.alignleft") + Button("Code", systemImage: "curlybraces") { + shownCode = ShownCode( + title: "Version \(conversation.currentVersion?.number ?? 0)", + code: session.currentSource + ) } + .disabled(session.currentSource.isEmpty) + Button("Logs", systemImage: "text.alignleft") { showingLogs = true } + .disabled(session.attemptLogs.isEmpty) Spacer() + usageText } - .disabled(jsx.isEmpty) } .padding(12) } - private var examplesSection: some View { - VStack(alignment: .leading, spacing: 4) { - Text("Try an example") - .font(.caption) - .foregroundStyle(.secondary) - ScrollView(.horizontal, showsIndicators: false) { - HStack(spacing: 6) { - ForEach(AIExamplePrompts.all) { example in - Button { - prompt = example.prompt - session.size = example.size - } label: { - HStack(spacing: 4) { - Image(systemName: example.symbol) - Text(example.title) - } - .font(.caption) - } - .buttonStyle(.bordered) - .controlSize(.small) - } - } - } - } - } - - private var skillsSection: some View { - VStack(alignment: .leading, spacing: 6) { - Text("Skills") - .font(.caption) - .foregroundStyle(.secondary) - Text("Add reusable expert instructions to this generation.") - .font(.caption2) - .foregroundStyle(.tertiary) - LazyVGrid(columns: [GridItem(.adaptive(minimum: 135))], alignment: .leading, spacing: 6) { - ForEach(skills) { skill in - Toggle(isOn: skillBinding(skill.id)) { - Label(skill.title, systemImage: skill.symbol) - .font(.caption) - } - .toggleStyle(.button) - .help(skill.summary) - } - } - Button("Manage Skills…", systemImage: "slider.horizontal.3") { - NotificationCenter.default.post(name: SkillManagerOpenRequest.notification, object: nil) - } - .buttonStyle(.link) - .controlSize(.small) - } - } - - private func skillBinding(_ id: String) -> Binding { - Binding( - get: { selectedSkillIDs.contains(id) }, - set: { enabled in - if enabled { selectedSkillIDs.insert(id) } - else { selectedSkillIDs.remove(id) } - } - ) + private var usageText: some View { + let total = conversation.totalUsage.totalTokens + let last = conversation.turns.last?.usage.totalTokens ?? 0 + return Text("Last turn \(last) · Total \(total) tokens") + .font(.caption) + .lineLimit(1) + .fixedSize() + .foregroundStyle(.secondary) + .accessibilityLabel("Tokens: last turn \(last), total \(total)") + .opacity(total == 0 ? 0 : 1) } @ViewBuilder private var previewContent: some View { - let size = session.size.previewSize - if let element = session.resultElement, let pkg = previewPackage { + let size = conversation.size.previewSize + if let element = session.previewElement, let package = session.previewPackage { let context = ScriptWidgetElementContext( runtime: nil, debugMode: isDebugMode, scriptName: "AI Preview", scriptParameter: "", - package: pkg + package: package ) - ScriptWidgetElementView(element: element, context: context) - .frame(width: size.width, height: size.height) - .background(Color(NSColor.textBackgroundColor)) - .cornerRadius(session.size.previewIsCircular ? size.height / 2 : 10) + ScrollView([.horizontal, .vertical]) { + ScriptWidgetElementView(element: element, context: context) + .frame(width: size.width, height: size.height) + .background(Color(NSColor.textBackgroundColor)) + .cornerRadius(conversation.size.previewIsCircular ? size.height / 2 : 10) + .padding(16) + } + .defaultScrollAnchor(.center) } else if session.isRunning { VStack(spacing: StudioDesign.standardSpacing) { ProgressView() - Text("Building and validating your widget…") + Text("Building and checking your widget…") .font(.footnote) .foregroundStyle(.secondary) } } else { ContentUnavailableView( - "Ready to Create", + "Nothing to Show Yet", systemImage: "sparkles.rectangle.stack", - description: Text("Describe a widget on the left, then generate a live preview.") + description: Text("Describe a widget on the left. It appears here as soon as it runs.") ) } } + // MARK: - footer + private var footer: some View { HStack { - Text("Tokens used: \(session.usage.totalTokens)") - .font(.caption) - .foregroundStyle(.secondary) - Spacer() - Button(role: .destructive) { - dismiss() - } label: { - Label("Discard", systemImage: "trash") + if let current = conversation.currentVersion { + let index = sortedVersions.firstIndex { $0.id == current.id } ?? 0 + Button("Previous version", systemImage: "chevron.left") { + session.selectVersion(sortedVersions[index - 1].id) + } + .labelStyle(.iconOnly) + .disabled(index == 0 || session.isRunning) + Text("Version \(current.number) of \(sortedVersions.last?.number ?? current.number)") + .font(.callout.monospacedDigit()) + Button("Next version", systemImage: "chevron.right") { + session.selectVersion(sortedVersions[index + 1].id) + } + .labelStyle(.iconOnly) + .disabled(index >= sortedVersions.count - 1 || session.isRunning) + } else { + Text("No version yet") + .font(.callout) + .foregroundStyle(.secondary) } - + Spacer() TextField("Widget name", text: $saveName) .textFieldStyle(.roundedBorder) - .frame(minWidth: 200) - - Button { - performSave() - } label: { - Label("Save Widget", systemImage: "square.and.arrow.down") - } - .buttonStyle(.borderedProminent) - .keyboardShortcut(.defaultAction) - .disabled(jsx.isEmpty || saveName.trimmingCharacters(in: .whitespaces).isEmpty) + .frame(minWidth: 200, maxWidth: 260) + Button("Save Widget", systemImage: "square.and.arrow.down", action: performSave) + .buttonStyle(.borderedProminent) + .disabled(session.currentSource.isEmpty || session.isRunning + || saveName.trimmingCharacters(in: .whitespaces).isEmpty) } .padding(12) + .onChange(of: conversation.currentVersionID) { _, _ in prefillSaveNameIfNeeded() } } - @ViewBuilder - private var codeSheet: some View { + // MARK: - sheets + + private func codeSheet(_ shown: ShownCode) -> some View { VStack(alignment: .leading) { HStack { - Text("Generated JSX").font(.headline) + Text(shown.title).font(.headline) Spacer() - Button("Done") { showingCode = false } + Button("Done") { shownCode = nil } .keyboardShortcut(.defaultAction) } ScrollView { - Text(jsx) + Text(shown.code) .font(.system(.footnote, design: .monospaced)) .frame(maxWidth: .infinity, alignment: .leading) .padding(8) @@ -371,31 +636,28 @@ struct AIGenerateWindowView: View { .frame(minWidth: 520, minHeight: 420) } - @ViewBuilder private var logsSheet: some View { VStack(alignment: .leading) { HStack { - Text("Iteration Logs").font(.headline) + Text("Attempts of the Last Turn").font(.headline) Spacer() Button("Done") { showingLogs = false } .keyboardShortcut(.defaultAction) } List { - ForEach(session.iterationHistory) { record in - Section("Iteration \(record.iteration)") { - if let err = record.errorSummary { - Label(err, systemImage: "exclamationmark.triangle") + ForEach(session.attemptLogs) { record in + Section("Attempt \(record.attempt)") { + if let error = record.errorSummary { + Label(error, systemImage: "exclamationmark.triangle") .foregroundStyle(.orange) } else { - Label("Success", systemImage: "checkmark.circle") + Label("Ran", systemImage: "checkmark.circle") .foregroundStyle(.green) } - if !record.logs.isEmpty { - ForEach(Array(record.logs.enumerated()), id: \.offset) { _, line in - Text(line) - .font(.caption.monospaced()) - .textSelection(.enabled) - } + ForEach(Array(record.lines.enumerated()), id: \.offset) { _, line in + Text(line) + .font(.caption.monospaced()) + .textSelection(.enabled) } } } @@ -408,38 +670,60 @@ struct AIGenerateWindowView: View { // MARK: - helpers private var saveErrorBinding: Binding { + Binding(get: { saveError != nil }, set: { if !$0 { saveError = nil } }) + } + + private var sizeBinding: Binding { + Binding(get: { conversation.size }, set: { session.setSize($0) }) + } + + private func skillBinding(_ id: String) -> Binding { Binding( - get: { saveError != nil }, - set: { if !$0 { saveError = nil } } + get: { conversation.skillIDs.contains(id) }, + set: { enabled in + var ids = Set(conversation.skillIDs) + if enabled { ids.insert(id) } else { ids.remove(id) } + session.setSkills(ids) + } ) } - private func loadProfiles() { - profiles = AISettingsStore.shared.loadProfiles() - activeProfileID = AISettingsStore.shared.loadActiveProfileID() + private var otherDrafts: [AIConversationSummary] { + Array(drafts.filter { $0.id != conversation.id }.prefix(8)) + } + + private func draftLabel(_ draft: AIConversationSummary) -> String { + draft.title.isEmpty ? "Untitled" : draft.title } - private func ensurePreviewPackage() { - if previewPackage == nil { - previewPackage = try? AgentRuntimeBridge.shared.makeSandboxPackage(prefix: "preview") + private func open(_ draft: AIConversationSummary) { + guard let loaded = AIConversationStore.shared.load(id: draft.id) else { + reloadDrafts() + return } - refreshPreviewPackage() + session.cancel() + onOpen(loaded) } - private func refreshPreviewPackage() { - guard let pkg = previewPackage else { return } - _ = pkg.writeMainFile(content: jsx) + private func reloadDrafts() { + drafts = AIConversationStore.shared.summaries(draftsOnly: true) + } + + private func reloadSettings() { + profiles = AISettingsStore.shared.loadProfiles() + activeProfileID = AISettingsStore.shared.loadActiveProfileID() + isConfigured = AISettingsStore.shared.load().isConfigured } private func prefillSaveNameIfNeeded() { guard saveName.trimmingCharacters(in: .whitespaces).isEmpty else { return } - saveName = "AI " + AIGenerateWindowView.defaultNameFormatter.string(from: Date()) + saveName = "AI " + Self.defaultNameFormatter.string(from: Date()) } private static let defaultNameFormatter: DateFormatter = { - let f = DateFormatter() - f.dateFormat = "yyyy-MM-dd HHmm" - return f + let formatter = DateFormatter() + formatter.dateFormat = "yyyy-MM-dd HHmm" + return formatter }() private func performSave() { @@ -449,11 +733,12 @@ struct AIGenerateWindowView: View { return } let result = sharedScriptManager.createScript( - content: jsx, + content: session.currentSource, recommendPackageName: trimmed, imageCopyPath: nil ) if result.0 { + session.markSaved(packageName: trimmed) NotificationCenter.default.post(name: SharedAppStore.scriptCreateNotification, object: nil) WidgetCenter.shared.reloadAllTimelines() dismiss() @@ -462,3 +747,52 @@ struct AIGenerateWindowView: View { } } } + +/// Buttons that wrap onto as many lines as they need. +private struct FlowButtons: View { + let items: [Item] + @ViewBuilder let content: (Item) -> Content + + var body: some View { + FlowLayout(spacing: 6) { + ForEach(items) { content($0) } + } + } +} + +private struct FlowLayout: Layout { + var spacing: CGFloat + + func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize { + let rows = arrange(width: proposal.width ?? .infinity, subviews: subviews) + return CGSize(width: proposal.width ?? rows.width, height: rows.height) + } + + func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) { + let rows = arrange(width: bounds.width, subviews: subviews) + for (index, origin) in rows.origins.enumerated() { + subviews[index].place( + at: CGPoint(x: bounds.minX + origin.x, y: bounds.minY + origin.y), + proposal: .unspecified + ) + } + } + + private func arrange(width: CGFloat, subviews: Subviews) -> (origins: [CGPoint], width: CGFloat, height: CGFloat) { + var origins: [CGPoint] = [] + var x: CGFloat = 0, y: CGFloat = 0, rowHeight: CGFloat = 0, widest: CGFloat = 0 + for subview in subviews { + let size = subview.sizeThatFits(.unspecified) + if x > 0, x + size.width > width { + x = 0 + y += rowHeight + spacing + rowHeight = 0 + } + origins.append(CGPoint(x: x, y: y)) + x += size.width + spacing + rowHeight = max(rowHeight, size.height) + widest = max(widest, x - spacing) + } + return (origins, widest, y + rowHeight) + } +}