diff --git a/Shared/ScriptWidgetRuntime/AI/AIConversation.swift b/Shared/ScriptWidgetRuntime/AI/AIConversation.swift index 9481c792..fe6508ea 100644 --- a/Shared/ScriptWidgetRuntime/AI/AIConversation.swift +++ b/Shared/ScriptWidgetRuntime/AI/AIConversation.swift @@ -142,6 +142,9 @@ struct AIConversationTurn: Codable, Identifiable, Equatable { let createdAt: Date /// The user went back to an older version and continued from there. var isAbandoned: Bool + /// Not a request: the user changed the code in the editor, and this records + /// the version that came of it. + var isManualEdit: Bool? } struct AIConversation: Codable, Identifiable, Equatable { @@ -159,6 +162,8 @@ struct AIConversation: Codable, Identifiable, Equatable { var versionCounter: Int /// The widget the conversation was saved as; nil while it is a draft. var savedPackageName: String? + /// That widget's manifest id, which survives a rename; nil for a legacy package. + var savedPackageID: String? let createdAt: Date var updatedAt: Date @@ -238,10 +243,26 @@ struct AIConversation: Codable, Identifiable, Equatable { /// The turns the model is told about: finished, not abandoned, oldest first. func historyForPrompt() -> [AIConversationTurn] { - let finished = turns.filter { $0.status == .succeeded && !$0.isAbandoned } + let finished = turns.filter { $0.status == .succeeded && !$0.isAbandoned && $0.isManualEdit != true } return Array(finished.suffix(AIConversationLimits.historyTurns)) } + /// Takes over code that did not come from the AI: what the editor holds when + /// it differs from the current version. Returns whether a version was added. + @discardableResult + mutating func adoptExternalSource(_ source: String, networkDomains: [String] = [], now: Date = Date()) -> Bool { + guard currentVersion?.source != source else { return false } + abandonTurnsAfterCurrentVersion() + let base = currentVersionID + let version = addVersion(source: source, networkDomains: networkDomains, now: now) + turns.append(AIConversationTurn( + id: UUID(), request: "", baseVersionID: base, status: .succeeded, note: nil, errorSummary: nil, + repairs: 0, versionID: version.id, failedSource: nil, usage: .zero, createdAt: now, + isAbandoned: false, isManualEdit: true + )) + return true + } + /// A turn left running by a closed app can never finish. mutating func markRunningTurnsInterrupted() { for index in turns.indices where turns[index].status == .running { diff --git a/Shared/ScriptWidgetRuntime/AI/AIConversationSession.swift b/Shared/ScriptWidgetRuntime/AI/AIConversationSession.swift index 868add91..7c826dd7 100644 --- a/Shared/ScriptWidgetRuntime/AI/AIConversationSession.swift +++ b/Shared/ScriptWidgetRuntime/AI/AIConversationSession.swift @@ -58,6 +58,8 @@ final class AIConversationSession: ObservableObject { private let makeLoop: () -> AgentLoop private let loadSettings: () -> AISettings private let skillsText: (Set) -> String? + /// Set when the conversation is about an existing widget. + private let sandbox: AgentSandboxSetup? /// The temporary preview folder, removed when the session goes away. private let previewPackageURL: URL? @@ -76,8 +78,10 @@ final class AIConversationSession: ObservableObject { bridge: AgentRuntimeBridge = .shared, makeLoop: @escaping () -> AgentLoop = { AgentLoop() }, loadSettings: @escaping () -> AISettings = { AISettingsStore.shared.load() }, - skillsText: @escaping (Set) -> String? = { AIWidgetSkills.instructions(selectedIDs: $0) } + skillsText: @escaping (Set) -> String? = { AIWidgetSkills.instructions(selectedIDs: $0) }, + sandbox: AgentSandboxSetup? = nil ) { + self.sandbox = sandbox self.conversation = conversation self.store = store self.bridge = bridge @@ -85,6 +89,7 @@ final class AIConversationSession: ObservableObject { self.loadSettings = loadSettings self.skillsText = skillsText let package = try? bridge.makeSandboxPackage(prefix: "chat-preview") + if let package, let sandbox { bridge.seed(package, from: sandbox.seedDirectory) } previewPackage = package previewPackageURL = package?.path if conversation.currentVersion != nil { refreshPreview() } @@ -96,8 +101,21 @@ final class AIConversationSession: ObservableObject { // MARK: - turns + /// Takes over what the editor holds when it is not the current version, so + /// that the next turn starts from the user's own changes. + /// + /// Code that is already one of the conversation's versions is not taken + /// over: the editor simply has not received a newer version yet, and that + /// version stays the one being worked on. + func adoptEditorSource(_ source: String) { + guard !isRunning, !conversation.versions.contains(where: { $0.source == source }), + conversation.adoptExternalSource(source, networkDomains: currentDomains) else { return } + persist() + refreshPreview() + } + /// Sends a new request, continuing from the current version. - func send(_ text: String) { + func send(_ text: String, runtimeDiagnostic: String? = nil) { let request = text.trimmingCharacters(in: .whitespacesAndNewlines) guard !request.isEmpty, !isRunning else { return } @@ -113,7 +131,8 @@ final class AIConversationSession: ObservableObject { request: turn.request, skillsText: skillsText(Set(conversation.skillIDs)), size: conversation.size, - currentDomains: base?.networkDomains ?? [] + currentDomains: base?.networkDomains ?? [], + runtimeDiagnostic: runtimeDiagnostic ) persist() run(turnID: turn.id, context: context) @@ -166,8 +185,9 @@ final class AIConversationSession: ObservableObject { } /// Records that the current version was saved as a widget. - func markSaved(packageName: String) { + func markSaved(packageName: String, packageID: String? = nil) { conversation.savedPackageName = packageName + conversation.savedPackageID = packageID conversation.updatedAt = Date() persist() } @@ -188,7 +208,8 @@ final class AIConversationSession: ObservableObject { mode: .turn(context), size: conversation.size, settings: settings, - maxIterations: AIConversationLimits.maxAttemptsPerTurn + maxIterations: AIConversationLimits.maxAttemptsPerTurn, + sandbox: sandbox ) let loop = makeLoop() currentTask = Task { [weak self] in @@ -300,7 +321,9 @@ final class AIConversationSession: ObservableObject { previews = previews.filter { kept.contains($0.key) } var sizes = [conversation.size] + Self.thumbnailSizes.filter { $0 != conversation.size } sizes.removeAll { kept.contains($0) } - let manifest = AINetworkDeclaration.manifest(name: "AI Preview", domains: version.networkDomains ?? []) + let domains = version.networkDomains ?? [] + let manifest = sandbox.map { $0.manifest(declaring: domains) } + ?? AINetworkDeclaration.manifest(name: "AI Preview", domains: domains) previewTask = Task { [weak self, bridge] in for size in sizes { let result = await bridge.run(jsx: version.source, in: previewPackage, size: size, manifest: manifest) diff --git a/Shared/ScriptWidgetRuntime/AI/AIConversationStore.swift b/Shared/ScriptWidgetRuntime/AI/AIConversationStore.swift index 3296672c..e34b4163 100644 --- a/Shared/ScriptWidgetRuntime/AI/AIConversationStore.swift +++ b/Shared/ScriptWidgetRuntime/AI/AIConversationStore.swift @@ -77,6 +77,17 @@ final class AIConversationStore { } } + /// The newest conversation saved as this widget: by manifest id when there + /// is one, else by name. + func latest(packageID: String?, packageName: String) -> AIConversation? { + lock.lock() + defer { lock.unlock() } + return allLocked().first { conversation in + if let packageID, let saved = conversation.savedPackageID { return saved == packageID } + return conversation.savedPackageName == packageName + } + } + func delete(id: UUID) { lock.lock() defer { lock.unlock() } diff --git a/Shared/ScriptWidgetRuntime/AI/AgentLoop.swift b/Shared/ScriptWidgetRuntime/AI/AgentLoop.swift index f32503ca..c2df3a2c 100644 --- a/Shared/ScriptWidgetRuntime/AI/AgentLoop.swift +++ b/Shared/ScriptWidgetRuntime/AI/AgentLoop.swift @@ -29,6 +29,28 @@ enum AgentLoopEvent { case declared([String]) } +/// How a conversation about an existing widget sets up its sandbox: with the +/// widget's files, and with the widget's own permissions. +struct AgentSandboxSetup { + /// The widget's folder; its files are copied in before the first run. + let seedDirectory: URL + /// The widget's manifest. nil means a legacy package: the candidate runs + /// without widget.json, as that widget does. + let baseManifest: WidgetPackageManifest? + + /// The manifest a candidate runs with: the widget's own, plus the network + /// permission for the hosts the candidate declares. + func manifest(declaring domains: [String]) -> WidgetPackageManifest? { + guard var manifest = baseManifest else { return nil } + guard !domains.isEmpty else { return manifest } + if !manifest.permissions.contains(.network) { manifest.permissions.append(.network) } + for domain in domains where !manifest.networkDomains.contains(domain) { + manifest.networkDomains.append(domain) + } + return manifest + } +} + struct AgentLoopRequest { enum Mode { case fresh(userDescription: String) @@ -41,6 +63,8 @@ struct AgentLoopRequest { let size: AIWidgetSize let settings: AISettings let maxIterations: Int + /// Only for a conversation about an existing widget. + var sandbox: AgentSandboxSetup? = nil } final class AgentLoop { @@ -97,6 +121,12 @@ final class AgentLoop { return .failed(message: error.localizedDescription, usage: cumulativeUsage) } defer { bridge.cleanupSandboxPackage(package) } + if let sandbox = request.sandbox, !bridge.seed(package, from: sandbox.seedDirectory) { + return .failed( + message: "This widget has too many or too large files to try changes on a copy.", + usage: cumulativeUsage + ) + } var lastError: String? var lastLogs: [String] = [] @@ -158,10 +188,12 @@ final class AgentLoop { // once saved: none, or the network for the hosts it declares. switch AINetworkDeclaration.parse(json: reply.declaration) { case .none: - manifest = AINetworkDeclaration.manifest(name: "AI Preview", domains: []) + manifest = request.sandbox.map { $0.manifest(declaring: []) } + ?? AINetworkDeclaration.manifest(name: "AI Preview", domains: []) await onEvent(.declared([])) case .declared(let declaration): - manifest = declaration.manifest(name: "AI Preview") + manifest = request.sandbox.map { $0.manifest(declaring: declaration.domains) } + ?? declaration.manifest(name: "AI Preview") await onEvent(.declared(declaration.domains)) case .invalid(let reason): declarationError = "The network declaration block is not valid: \(reason)." diff --git a/Shared/ScriptWidgetRuntime/AI/AgentRuntimeBridge.swift b/Shared/ScriptWidgetRuntime/AI/AgentRuntimeBridge.swift index 608412ff..5d3dc0e3 100644 --- a/Shared/ScriptWidgetRuntime/AI/AgentRuntimeBridge.swift +++ b/Shared/ScriptWidgetRuntime/AI/AgentRuntimeBridge.swift @@ -83,6 +83,46 @@ final class AgentRuntimeBridge { return ScriptWidgetPackage(path: dir, readonly: false) } + /// Copies a widget's own files into a sandbox package, so that code which + /// imports or reads them runs as it would in the widget. Regular files + /// only, no links, nothing from the build cache, within fixed limits. + /// Returns false when the widget is too large to copy; nothing is half-copied + /// silently in that case, the caller is told. + @discardableResult + func seed(_ package: ScriptWidgetPackage, from source: URL) -> Bool { + let fileManager = FileManager.default + let keys: [URLResourceKey] = [.isRegularFileKey, .isDirectoryKey, .isSymbolicLinkKey, .fileSizeKey] + guard let enumerator = fileManager.enumerator( + at: source, includingPropertiesForKeys: keys, options: [.skipsHiddenFiles] + ) else { return false } + let root = source.standardizedFileURL.path + var files: [(relative: String, url: URL)] = [] + var total = 0 + for case let url as URL in enumerator { + guard let values = try? url.resourceValues(forKeys: Set(keys)) else { return false } + if values.isSymbolicLink == true { continue } + let path = url.standardizedFileURL.path + guard path.hasPrefix(root + "/") else { continue } + let relative = String(path.dropFirst(root.count + 1)) + if relative.split(separator: "/").first.map({ $0.lowercased() == "__build" }) == true { + if values.isDirectory == true { enumerator.skipDescendants() } + continue + } + guard values.isRegularFile == true else { continue } + total += values.fileSize ?? 0 + files.append((relative, url)) + guard files.count <= Self.seedMaximumFiles, total <= Self.seedMaximumBytes else { return false } + } + for file in files { + guard let data = try? Data(contentsOf: file.url), + ScriptWidgetPackageFiles.write(data, root: package.path, relative: file.relative) else { return false } + } + return true + } + + static let seedMaximumFiles = 300 + static let seedMaximumBytes = 20 * 1_048_576 + func cleanupSandboxPackage(_ package: ScriptWidgetPackage) { try? FileManager.default.removeItem(at: package.path) } @@ -96,6 +136,7 @@ final class AgentRuntimeBridge { return await withCheckedContinuation { continuation in runQueue.async { if let manifest { + // A seeded copy may carry the widget's own widget.json; this one replaces it. guard let data = try? JSONEncoder().encode(manifest), ScriptWidgetPackageFiles.write(data, root: package.path, relative: "widget.json") else { continuation.resume(returning: AgentRunResult( diff --git a/Shared/ScriptWidgetRuntime/AI/PromptBuilder.swift b/Shared/ScriptWidgetRuntime/AI/PromptBuilder.swift index d9bc144d..c385ba3a 100644 --- a/Shared/ScriptWidgetRuntime/AI/PromptBuilder.swift +++ b/Shared/ScriptWidgetRuntime/AI/PromptBuilder.swift @@ -98,6 +98,8 @@ struct AITurnContext { let size: AIWidgetSize /// Hosts the current widget declares it contacts. var currentDomains: [String] = [] + /// What the editor's preview last reported for this widget, if it failed. + var runtimeDiagnostic: String? = nil } struct AICopilotChangeSummary: Equatable { @@ -413,6 +415,8 @@ enum PromptBuilder { \(context.currentDomains.isEmpty ? "It declares no network use." : "Its network declaration: " + AINetworkDeclaration(domains: context.currentDomains).json) + \(context.runtimeDiagnostic.map { "The latest local ScriptWidget runtime diagnostic is:\n" + clip($0, 2_000) } ?? "") + The user now asks: \(context.request) @@ -468,7 +472,10 @@ enum PromptBuilder { } break } - return (note, stripCodeFences(body), declaration) + // The code ends with a line break, like a file. The runtime puts its own + // closing text right after the source, so a last line that is a `//` + // comment would otherwise swallow it. + return (note, stripCodeFences(body) + "\n", declaration) } /// Added to a repair request inside a conversation, where the declaration diff --git a/Tests/ScriptWidgetRuntimeTests/AIConversationTests.swift b/Tests/ScriptWidgetRuntimeTests/AIConversationTests.swift index c4b2fd37..d8af0f17 100644 --- a/Tests/ScriptWidgetRuntimeTests/AIConversationTests.swift +++ b/Tests/ScriptWidgetRuntimeTests/AIConversationTests.swift @@ -19,9 +19,9 @@ import AppKit 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 static let goodWidget = "$render(Hello)\n" + private static let betterWidget = "$render(HelloWorld)\n" + private static let brokenWidget = "const broken = ;\n" private var directory: URL! private var store: AIConversationStore! @@ -45,7 +45,8 @@ final class AIConversationTests: XCTestCase { private func makeSession( _ conversation: AIConversation = AIConversation(), - skills: String? = nil + skills: String? = nil, + sandbox: AgentSandboxSetup? = nil ) -> AIConversationSession { let client = AIClient(session: urlSession) var profile = AIProfile.makeDefault() @@ -58,10 +59,29 @@ final class AIConversationTests: XCTestCase { store: store, makeLoop: { AgentLoop(client: client) }, loadSettings: { settings }, - skillsText: { _ in skills } + skillsText: { _ in skills }, + sandbox: sandbox ) } + /// A widget folder with a second file that main.jsx imports. + private func makeWidgetFolder(manifest: WidgetPackageManifest?) throws -> URL { + let folder = directory.appendingPathComponent("Widget-\(UUID().uuidString)", isDirectory: true) + try FileManager.default.createDirectory(at: folder.appendingPathComponent("lib"), withIntermediateDirectories: true) + try "globalThis.Greeting = { text: \"From the library\" };" + .write(to: folder.appendingPathComponent("lib/greeting.js"), atomically: true, encoding: .utf8) + try Self.importingWidget.write(to: folder.appendingPathComponent("main.jsx"), atomically: true, encoding: .utf8) + if let manifest { + try JSONEncoder().encode(manifest).write(to: folder.appendingPathComponent("widget.json")) + } + return folder + } + + private static let importingWidget = """ + if (!$import("lib/greeting.js")) throw new Error("lib/greeting.js is missing"); + $render({Greeting.text}) + """ + private func reply(_ summary: String, _ code: String) -> ConversationProviderStub.Reply { .content("SUMMARY: \(summary)\n```jsx\n\(code)\n```") } @@ -98,19 +118,19 @@ final class AIConversationTests: XCTestCase { func testReplyParsingAcceptsEveryShapeAModelMaySend() { let full = PromptBuilder.parseTurnReply("SUMMARY: 标题改小了\n```jsx\n$render(a)\n```") XCTAssertEqual(full.note, "标题改小了") - XCTAssertEqual(full.code, "$render(a)") + XCTAssertEqual(full.code, "$render(a)\n") let noFence = PromptBuilder.parseTurnReply("Summary: Smaller title\n$render(a)") XCTAssertEqual(noFence.note, "Smaller title") - XCTAssertEqual(noFence.code, "$render(a)") + XCTAssertEqual(noFence.code, "$render(a)\n") let codeOnly = PromptBuilder.parseTurnReply("$render(a)") XCTAssertNil(codeOnly.note) - XCTAssertEqual(codeOnly.code, "$render(a)") + XCTAssertEqual(codeOnly.code, "$render(a)\n") let fenceOnly = PromptBuilder.parseTurnReply("```jsx\n$render(a)\n```") XCTAssertNil(fenceOnly.note) - XCTAssertEqual(fenceOnly.code, "$render(a)") + XCTAssertEqual(fenceOnly.code, "$render(a)\n") } func testRepairPromptClipsLongLogLines() { @@ -346,12 +366,12 @@ final class AIConversationTests: XCTestCase { ``` """) XCTAssertEqual(reply.note, "Shows a quote.") - XCTAssertEqual(reply.code, "$render(a)") + XCTAssertEqual(reply.code, "$render(a)\n") XCTAssertEqual(reply.declaration, #"{"permissions": ["network"], "networkDomains": ["api.example.com"]}"#) // Even without a fence around the code, the declaration is never taken for the widget. let bare = PromptBuilder.parseTurnReply("$render(a)\n```json\n{\"permissions\": []}\n```") - XCTAssertEqual(bare.code, "$render(a)") + XCTAssertEqual(bare.code, "$render(a)\n") XCTAssertNotNil(bare.declaration) } @@ -421,6 +441,145 @@ final class AIConversationTests: XCTestCase { XCTAssertEqual(ConversationProviderStub.bodies.count, 1) } + // MARK: - a conversation about an existing widget + + func testCodeFromTheEditorBecomesAVersionAndIsNotSentAsARequest() async { + ConversationProviderStub.enqueue([reply("Added a second line.", Self.betterWidget)]) + let session = makeSession() + session.adoptEditorSource(Self.goodWidget) + await session.settle() + + XCTAssertEqual(session.conversation.versions.count, 1) + XCTAssertEqual(session.conversation.turns.first?.isManualEdit, true) + XCTAssertEqual(session.currentSource, Self.goodWidget) + XCTAssertNotNil(session.previewElement) + XCTAssertEqual(ConversationProviderStub.bodies.count, 0, "taking over the editor's code sends nothing") + + session.send("Add a second line") + await session.settle() + let body = ConversationProviderStub.bodies[0] + XCTAssertTrue(body.contains("Hello"), "the editor's code is the widget the AI changes") + XCTAssertFalse(body.contains("Earlier turns"), "a manual edit is not an earlier request") + XCTAssertEqual(session.conversation.currentVersion?.number, 2) + } + + func testEditorStillHoldingAnOlderVersionDoesNotReplaceThePendingOne() async { + ConversationProviderStub.enqueue([reply("Added a second line.", Self.betterWidget)]) + let session = makeSession() + session.adoptEditorSource(Self.goodWidget) + session.send("Add a second line") + await session.settle() + + // The new version was not applied: the editor still holds version 1. + session.adoptEditorSource(Self.goodWidget) + XCTAssertEqual(session.currentSource, Self.betterWidget, "the pending version stays the one being worked on") + XCTAssertEqual(session.conversation.versions.count, 2) + + // Code the conversation never saw is the user's own edit. + let edited = "$render(Mine)" + session.adoptEditorSource(edited) + await session.settle() + XCTAssertEqual(session.currentSource, edited) + XCTAssertEqual(session.conversation.versions.count, 3) + XCTAssertEqual(session.conversation.turns.last?.isManualEdit, true) + } + + func testCandidateRunsWithTheWidgetsOwnFiles() async throws { + let folder = try makeWidgetFolder(manifest: nil) + ConversationProviderStub.enqueue([reply("Kept the import.", Self.importingWidget + "\n// changed")]) + let session = makeSession(sandbox: AgentSandboxSetup(seedDirectory: folder, baseManifest: nil)) + session.adoptEditorSource(Self.importingWidget) + await session.settle() + XCTAssertEqual(texts(session.previewElement), ["From the library"], "the preview copy has lib/greeting.js") + + session.send("Change something") + await session.settle() + XCTAssertEqual(session.conversation.turns.last?.status, .succeeded, + session.conversation.turns.last?.errorSummary ?? "") + XCTAssertTrue(session.currentSource.hasSuffix("// changed\n"), + "a last line that is a comment still runs: the code ends with a line break") + XCTAssertEqual(texts(session.previewElement), ["From the library"]) + XCTAssertEqual(try String(contentsOf: folder.appendingPathComponent("main.jsx"), encoding: .utf8), + Self.importingWidget, "the widget itself is not touched") + } + + func testCandidateRunsWithTheWidgetsOwnPermissionsPlusDeclaredHosts() async throws { + var manifest = WidgetPackageManifest.newProject(name: "Bound") + manifest.permissions = [.storage, .network] + manifest.networkDomains = ["one.example.com"] + let folder = try makeWidgetFolder(manifest: manifest) + let declaration = #"{"permissions": ["network"], "networkDomains": ["two.example.com"]}"# + ConversationProviderStub.enqueue([ + .content("SUMMARY: Kept it.\n```jsx\n\(Self.importingWidget)\n// v2\n```\n```json\n\(declaration)\n```"), + ]) + let session = makeSession(sandbox: AgentSandboxSetup(seedDirectory: folder, baseManifest: manifest)) + session.adoptEditorSource(Self.importingWidget) + session.send("Use a second host") + await session.settle() + + XCTAssertEqual(session.conversation.turns.last?.status, .succeeded, + session.conversation.turns.last?.errorSummary ?? "") + let preview = try XCTUnwrap(session.previewPackage?.readManifest()) + XCTAssertEqual(preview.id, manifest.id) + XCTAssertEqual(Set(preview.permissions), [.storage, .network], "nothing but the network is ever added") + XCTAssertEqual(preview.networkDomains, ["one.example.com", "two.example.com"]) + let saved = try JSONDecoder().decode(WidgetPackageManifest.self, + from: Data(contentsOf: folder.appendingPathComponent("widget.json"))) + XCTAssertEqual(saved.networkDomains, ["one.example.com"], "the widget's own manifest is not changed by a turn") + } + + func testSandboxSetupNeverInventsAManifestForALegacyWidget() { + let legacy = AgentSandboxSetup(seedDirectory: directory, baseManifest: nil) + XCTAssertNil(legacy.manifest(declaring: ["api.example.com"])) + } + + func testSeedingSkipsLinksAndTheBuildCacheAndRefusesAnOversizedWidget() throws { + let folder = try makeWidgetFolder(manifest: nil) + try FileManager.default.createDirectory(at: folder.appendingPathComponent("__Build"), withIntermediateDirectories: true) + try "cache".write(to: folder.appendingPathComponent("__Build/cache.js"), atomically: true, encoding: .utf8) + let outside = directory.appendingPathComponent("outside-\(UUID().uuidString).txt") + try "secret".write(to: outside, atomically: true, encoding: .utf8) + try FileManager.default.createSymbolicLink(at: folder.appendingPathComponent("link.txt"), withDestinationURL: outside) + + let package = try AgentRuntimeBridge.shared.makeSandboxPackage(prefix: "seed-test") + defer { AgentRuntimeBridge.shared.cleanupSandboxPackage(package) } + XCTAssertTrue(AgentRuntimeBridge.shared.seed(package, from: folder)) + let copied = package.path + XCTAssertTrue(FileManager.default.fileExists(atPath: copied.appendingPathComponent("lib/greeting.js").path)) + XCTAssertFalse(FileManager.default.fileExists(atPath: copied.appendingPathComponent("link.txt").path)) + XCTAssertFalse(FileManager.default.fileExists(atPath: copied.appendingPathComponent("__Build/cache.js").path)) + + for index in 0...AgentRuntimeBridge.seedMaximumFiles { + try "x".write(to: folder.appendingPathComponent("lib/f\(index).js"), atomically: true, encoding: .utf8) + } + let second = try AgentRuntimeBridge.shared.makeSandboxPackage(prefix: "seed-test") + defer { AgentRuntimeBridge.shared.cleanupSandboxPackage(second) } + XCTAssertFalse(AgentRuntimeBridge.shared.seed(second, from: folder)) + } + + func testConversationIsFoundAgainByTheWidgetItWasSavedAs() async { + ConversationProviderStub.enqueue([reply("Made a greeting.", Self.goodWidget)]) + let session = makeSession() + session.send("A greeting card") + await session.settle() + session.markSaved(packageName: "Greeting", packageID: "local.abc") + + XCTAssertEqual(store.latest(packageID: "local.abc", packageName: "Renamed")?.id, session.conversation.id, + "the manifest id finds it after a rename") + XCTAssertNil(store.latest(packageID: "local.other", packageName: "Greeting"), + "another widget that took the old name does not get the conversation") + XCTAssertEqual(store.latest(packageID: nil, packageName: "Greeting")?.id, session.conversation.id) + XCTAssertNil(store.latest(packageID: nil, packageName: "Other")) + } + + func testRuntimeDiagnosticIsPartOfTheRequest() { + let prompt = PromptBuilder.userPromptTurn(AITurnContext( + history: [], currentCode: "CODE", previousCode: nil, request: "Fix it", skillsText: nil, + size: .small, runtimeDiagnostic: "scriptException: boom" + )) + XCTAssertTrue(prompt.contains("scriptException: boom")) + } + // MARK: - versions func testGoingBackAndContinuingLeavesTheLaterTurnBehind() async { diff --git a/docs/ai-conversation.md b/docs/ai-conversation.md index f7a43fa3..d9fd027c 100644 --- a/docs/ai-conversation.md +++ b/docs/ai-conversation.md @@ -38,6 +38,18 @@ A widget that fetches data must say which hosts it contacts. The AI is asked to 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). +## In the editor + +The **Copilot** tab of the editor is the conversation about the open widget. A widget made in the AI window continues the conversation it was made in; any other widget starts one when you first write to it. + +- The AI works on a copy of the widget. Each version it makes is drawn small in the panel, and the widget and the editor stay as they are. +- **Apply to Editor** writes the version on screen into the open document and saves it. If the document changed since the panel last looked, nothing is written and the conversation continues from the editor's code instead. Undo in the editor restores what was there. +- Code the conversation has not seen (you typed in the editor, or used the Layout panel) is taken over as a version marked "Code from the editor" before your next request, so the AI always changes what you see. +- The copy contains the widget's other files, so a widget that imports its own files or reads its own images runs as it does in the widget. A widget with more than 300 files or 20 MB is too large for this, and the panel says so. +- The copy runs with the widget's own permissions. If a version declares a host the widget may not contact yet, Apply asks first and adds the network permission for that host to the widget. +- **Fix Runtime Error** sends the error the editor's preview last reported. +- The conversation changes the widget's main file. With another file selected, the panel asks you to select the main file. + ## When something goes wrong | What happened | What you see | @@ -77,7 +89,9 @@ Generated code is untrusted. It runs only through the ScriptWidget runtime, in a The preview package carries a Package 2.0 `widget.json` with no permission, or with the network permission for the declared hosts, so the code is held to what the saved widget will get. Other permissions (location, calendar, health and the rest) can not be declared in a conversation; a widget that needs one gets it in the editor's configuration after saving, with the usual system approval. -The earlier single request on iPhone and iPad, and the editor's Copilot, still run their candidate in a package without `widget.json`. +In the editor, the copy runs with the widget's own `widget.json` plus the declared hosts, and a legacy widget without `widget.json` runs as a legacy package, as it does when installed. A `widget.json` that is present but can not be read turns the panel off; it is never treated as absent. + +The earlier single request on iPhone and iPad still runs its candidate in a package without `widget.json`. ## Limits @@ -91,7 +105,7 @@ The earlier single request on iPhone and iPad, and the editor's Copilot, still r ## Not in this version -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. +Targeted edits instead of whole-file replies, a view of what changed between versions, and the iPhone and iPad interface. ## For contributors diff --git a/macOS/ScriptWidgetMac.xcodeproj/project.pbxproj b/macOS/ScriptWidgetMac.xcodeproj/project.pbxproj index 5a8e1b1c..63a6bc3d 100644 --- a/macOS/ScriptWidgetMac.xcodeproj/project.pbxproj +++ b/macOS/ScriptWidgetMac.xcodeproj/project.pbxproj @@ -78,6 +78,7 @@ A202001000000000000000B0 /* AIWidgetSkills.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201001000000000000000B0 /* AIWidgetSkills.swift */; }; A204000100000000000000B0 /* SettingAIView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A203000100000000000000B0 /* SettingAIView.swift */; }; A204000200000000000000B0 /* AIGenerateWindowView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A203000200000000000000B0 /* AIGenerateWindowView.swift */; }; + A204004100000000000000B0 /* AIConversationTurnsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A203004100000000000000B0 /* AIConversationTurnsView.swift */; }; A204000300000000000000B0 /* SkillManagerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A203000300000000000000B0 /* SkillManagerView.swift */; }; A206000300000000000000B0 /* SwiftOpenAI in Frameworks */ = {isa = PBXBuildFile; productRef = A206000200000000000000B0 /* SwiftOpenAI */; }; A207000200000000000000B0 /* EditorSchemeHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = A207000100000000000000B0 /* EditorSchemeHandler.swift */; }; @@ -386,6 +387,7 @@ A201001000000000000000B0 /* AIWidgetSkills.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIWidgetSkills.swift; sourceTree = ""; }; A203000100000000000000B0 /* SettingAIView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingAIView.swift; sourceTree = ""; }; A203000200000000000000B0 /* AIGenerateWindowView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIGenerateWindowView.swift; sourceTree = ""; }; + A203004100000000000000B0 /* AIConversationTurnsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIConversationTurnsView.swift; sourceTree = ""; }; A203000300000000000000B0 /* SkillManagerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SkillManagerView.swift; sourceTree = ""; }; A207000100000000000000B0 /* EditorSchemeHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditorSchemeHandler.swift; sourceTree = ""; }; 845252C200359AB13440A3E7 /* StudioEditorBundleSchemeHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ../Editor/native/StudioEditorBundleSchemeHandler.swift; sourceTree = SOURCE_ROOT; }; @@ -611,6 +613,7 @@ isa = PBXGroup; children = ( A203000200000000000000B0 /* AIGenerateWindowView.swift */, + A203004100000000000000B0 /* AIConversationTurnsView.swift */, ); path = AIGenerate; sourceTree = ""; @@ -1393,6 +1396,7 @@ A202000800000000000000B0 /* AIGenerateProgressView.swift in Sources */, A204000100000000000000B0 /* SettingAIView.swift in Sources */, A204000200000000000000B0 /* AIGenerateWindowView.swift in Sources */, + A204004100000000000000B0 /* AIConversationTurnsView.swift in Sources */, A204000300000000000000B0 /* SkillManagerView.swift in Sources */, D1995CFE29536BC700D1BD94 /* ScriptManager.swift in Sources */, 3B4D0329D9954989AE1517F0 /* ScriptWidgetStorageUpgrade.swift in Sources */, diff --git a/macOS/ScriptWidgetMac/AIGenerate/AIConversationTurnsView.swift b/macOS/ScriptWidgetMac/AIGenerate/AIConversationTurnsView.swift new file mode 100644 index 00000000..e31a31d9 --- /dev/null +++ b/macOS/ScriptWidgetMac/AIGenerate/AIConversationTurnsView.swift @@ -0,0 +1,177 @@ +// +// AIConversationTurnsView.swift +// ScriptWidgetMac +// +// The turns of an AI conversation, one under the other: the request, what +// came of it, the version it made, and what to do when it did not work. +// Used by the AI window and by the editor's Copilot panel. +// + +import SwiftUI + +struct AIConversationTurnsView: View { + @ObservedObject var session: AIConversationSession + /// False when no provider is set up or the widget can not be changed. + let canSend: Bool + /// Shows a piece of code with a title. + let showCode: (String, String) -> Void + + var body: some View { + ForEach(session.conversation.turns) { turn in + if turn.isManualEdit == true { + manualEdit(turn) + .id(turn.id) + } else { + turnView(turn) + .id(turn.id) + } + } + } + + private func manualEdit(_ turn: AIConversationTurn) -> some View { + HStack(spacing: 8) { + Label("Code from the editor", systemImage: "pencil.line") + .font(.caption) + .foregroundStyle(.secondary) + versionLine(turn) + } + .opacity(turn.isAbandoned ? 0.5 : 1) + } + + @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) + } + } + .frame(maxWidth: .infinity, alignment: .leading) + } + .opacity(turn.isAbandoned ? 0.5 : 1) + } + + @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) + } + if !session.liveText.isEmpty { + Text(Self.tail(of: session.liveText, lines: 8)) + .font(.caption.monospaced()) + .foregroundStyle(.secondary) + .lineLimit(8, reservesSpace: true) + .frame(maxWidth: .infinity, alignment: .leading) + .padding(8) + .background(Color(nsColor: .textBackgroundColor).opacity(0.6), in: .rect(cornerRadius: 6)) + .accessibilityLabel("Code being written") + } + 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") { showCode("Code That Did Not Run", 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") + } + } + + @ViewBuilder + private func versionLine(_ turn: AIConversationTurn) -> some View { + if let version = session.conversation.version(turn.versionID) { + let isCurrent = version.id == session.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) + } + } + } + } + + private func retryButton(_ turn: AIConversationTurn, title: LocalizedStringKey) -> some View { + Button(title, systemImage: "arrow.clockwise") { session.retry(turnID: turn.id) } + .controlSize(.small) + .disabled(session.isRunning || !canSend) + } + + 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 static func tail(of text: String, lines: Int) -> String { + text.split(separator: "\n", omittingEmptySubsequences: false).suffix(lines).joined(separator: "\n") + } +} diff --git a/macOS/ScriptWidgetMac/AIGenerate/AIGenerateWindowView.swift b/macOS/ScriptWidgetMac/AIGenerate/AIGenerateWindowView.swift index e69a2deb..e5781f49 100644 --- a/macOS/ScriptWidgetMac/AIGenerate/AIGenerateWindowView.swift +++ b/macOS/ScriptWidgetMac/AIGenerate/AIGenerateWindowView.swift @@ -190,9 +190,8 @@ private struct AIConversationWindowContent: View { } else if conversation.turns.isEmpty { emptyState } - ForEach(conversation.turns) { turn in - turnView(turn) - .id(turn.id) + AIConversationTurnsView(session: session, canSend: isConfigured) { title, code in + shownCode = ShownCode(title: title, code: code) } Color.clear.frame(height: 1).id("bottom") } @@ -281,138 +280,6 @@ private struct AIConversationWindowContent: View { } } - @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) - } - } - .frame(maxWidth: .infinity, alignment: .leading) - } - .opacity(turn.isAbandoned ? 0.5 : 1) - } - - @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) - } - if !session.liveText.isEmpty { - Text(Self.tail(of: session.liveText, lines: 8)) - .font(.caption.monospaced()) - .foregroundStyle(.secondary) - .lineLimit(8, reservesSpace: true) - .frame(maxWidth: .infinity, alignment: .leading) - .padding(8) - .background(Color(nsColor: .textBackgroundColor).opacity(0.6), in: .rect(cornerRadius: 6)) - .accessibilityLabel("Code being written") - } - 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") - } - } - - @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) - } - } - } - } - - 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 { @@ -601,10 +468,6 @@ private struct AIConversationWindowContent: View { } } - private static func tail(of text: String, lines: Int) -> String { - text.split(separator: "\n", omittingEmptySubsequences: false).suffix(lines).joined(separator: "\n") - } - private var usageText: some View { let total = conversation.totalUsage.totalTokens let last = conversation.turns.last?.usage.totalTokens ?? 0 @@ -848,7 +711,10 @@ private struct AIConversationWindowContent: View { warning = "The widget's configuration could not be read." } } - session.markSaved(packageName: packageName) + session.markSaved( + packageName: packageName, + packageID: sharedScriptManager.getScriptPackage(packageName: packageName).readManifest()?.id + ) NotificationCenter.default.post(name: SharedAppStore.scriptCreateNotification, object: nil) WidgetCenter.shared.reloadAllTimelines() if let warning { diff --git a/macOS/ScriptWidgetMac/Editor/EditorPanelView.swift b/macOS/ScriptWidgetMac/Editor/EditorPanelView.swift index 6e0ffd90..72349715 100644 --- a/macOS/ScriptWidgetMac/Editor/EditorPanelView.swift +++ b/macOS/ScriptWidgetMac/Editor/EditorPanelView.swift @@ -50,197 +50,359 @@ struct EditorPanelView: View { } } +/// The editor's Copilot panel: the AI conversation about this widget. It is +/// the same conversation the widget was made in, when it was made by the AI. +/// +/// The AI works on a copy. Its versions change the small preview here and +/// nothing else, until "Apply to Editor" writes the version on screen into +/// the open document through the editor's own conflict check. private struct StudioCopilotView: View { let scriptModel: ScriptModel let editorID: UUID let documentID: String? - @State private var proposalSnapshot: StudioDocumentSnapshot? - @State private var requestID = UUID() - @State private var isApplying = false - @State private var isReading = false - @StateObject private var session = AIGenerateSession() - @State private var instruction = "" - @State private var originalCode = "" - @State private var proposedCode = "" + @State private var conversation: AIConversation? + @State private var sessionKey = UUID() + + var body: some View { + let package = scriptModel.package + let manifest = package.readManifest() + Group { + if package.hasManifest && manifest == nil { + // A widget.json that can not be read is never treated as absent. + unavailable("This widget's widget.json can not be read, so the AI can not work on it. Fix it in the Config tab first.") + } else { + StudioCopilotConversationView( + scriptModel: scriptModel, + manifest: manifest, + editorID: editorID, + documentID: documentID, + conversation: conversation ?? Self.conversation(for: package, manifest: manifest), + onStartOver: { + conversation = Self.newConversation(for: package, manifest: manifest) + sessionKey = UUID() + } + ) + .id(sessionKey) + } + } + .id(package.path) + } + + private func unavailable(_ text: String) -> some View { + ContentUnavailableView("AI Copilot Unavailable", systemImage: "sparkles", description: Text(text)) + } + + private static func conversation(for package: ScriptWidgetPackage, manifest: WidgetPackageManifest?) -> AIConversation { + AIConversationStore.shared.latest(packageID: manifest?.id, packageName: package.name) + ?? newConversation(for: package, manifest: manifest) + } + + private static func newConversation(for package: ScriptWidgetPackage, manifest: WidgetPackageManifest?) -> AIConversation { + var conversation = AIConversation() + conversation.title = package.name + conversation.savedPackageName = package.name + conversation.savedPackageID = manifest?.id + return conversation + } +} + +private struct StudioCopilotConversationView: View { + let scriptModel: ScriptModel + let manifest: WidgetPackageManifest? + let editorID: UUID + let documentID: String? + let onStartOver: () -> Void + + @StateObject private var session: AIConversationSession + @State private var draftText = "" @State private var statusMessage: String? - @State private var showingOriginal = false - @State private var showingProposal = true - @State private var selectedSkillIDs: Set = [] + /// What the editor held the last time this panel looked. + @State private var knownEditorContent: String? + @State private var isReading = false + @State private var isApplying = false + @State private var requestID = UUID() + @State private var shownCode: ShownCode? + @State private var confirmingNetwork = false + @State private var isConfigured = true @State private var skills: [AIWidgetSkill] = AIWidgetSkills.builtIns - private var changeSummary: AICopilotChangeSummary { - .compare(original: originalCode, proposed: proposedCode) + private struct ShownCode: Identifiable { + let id = UUID() + let title: String + let code: String } - var body: some View { - VStack(alignment: .leading, spacing: 0) { - ScrollView { - VStack(alignment: .leading, spacing: 12) { - Label("AI Copilot", systemImage: "sparkles") - .font(.headline) - - Text("Ask Studio to improve the current widget. Changes are validated locally and shown for review before they are applied.") - .font(.caption) - .foregroundStyle(.secondary) - - TextEditor(text: $instruction) - .font(.body) - .frame(minHeight: 90) - .overlay { - RoundedRectangle(cornerRadius: 6) - .stroke(Color(nsColor: .separatorColor), lineWidth: 1) - } - - ScrollView(.horizontal, showsIndicators: false) { - HStack(spacing: 6) { - ForEach(skills) { skill in - Toggle(isOn: skillBinding(skill.id)) { - Label(skill.title, systemImage: skill.symbol) - .font(.caption) - } - .toggleStyle(.button) - .controlSize(.small) - .help(skill.summary) - } - } - } + init(scriptModel: ScriptModel, manifest: WidgetPackageManifest?, editorID: UUID, documentID: String?, + conversation: AIConversation, onStartOver: @escaping () -> Void) { + self.scriptModel = scriptModel + self.manifest = manifest + self.editorID = editorID + self.documentID = documentID + self.onStartOver = onStartOver + _session = StateObject(wrappedValue: AIConversationSession( + conversation: conversation, + sandbox: AgentSandboxSetup(seedDirectory: scriptModel.package.path, baseManifest: manifest) + )) + } - Button("Manage Skills…", systemImage: "slider.horizontal.3") { - NotificationCenter.default.post(name: SkillManagerOpenRequest.notification, object: nil) - } - .controlSize(.small) + private var package: ScriptWidgetPackage { scriptModel.package } + private var conversation: AIConversation { session.conversation } - HStack { - Button("Propose Change", systemImage: "wand.and.stars") { - beginCopilotRequest(fixCurrentError: false) - } - .buttonStyle(.borderedProminent) + /// The conversation changes the widget's entry file and no other. + private var isEntryDocument: Bool { + let entry = manifest?.entry ?? "main.jsx" + return documentID != nil + && documentID == package.resolvedPackageURL(relativePath: entry)?.standardizedFileURL.path + } - Button("Fix Runtime Error", systemImage: "wrench.and.screwdriver") { - beginCopilotRequest(fixCurrentError: true) - } - } - .disabled(session.isRunning || isReading || isApplying || scriptModel.package.readonly) + private var canWork: Bool { isConfigured && isEntryDocument && !package.readonly } - if session.isRunning { - AIGenerateProgressView(session: session) - Button("Cancel", role: .cancel) { session.cancel() } - } + /// The version on screen is not what the editor holds. + private var hasUnappliedVersion: Bool { + guard let known = knownEditorContent, !session.isRunning, !session.currentSource.isEmpty else { return false } + return session.currentSource != known + } - if let statusMessage { - Text(statusMessage) - .font(.caption) - .foregroundStyle(.secondary) - } + /// Hosts the version declares that the widget may not contact yet. + private var hostsToAllow: [String] { + guard let manifest else { return [] } + return session.currentDomains.filter { !manifest.networkDomains.contains($0) } + } - if !proposedCode.isEmpty { - Divider() - proposalReview + var body: some View { + VStack(spacing: 0) { + ScrollViewReader { proxy in + ScrollView { + VStack(alignment: .leading, spacing: 12) { + header + notices + if conversation.currentVersion != nil { preview } + AIConversationTurnsView(session: session, canSend: canWork) { title, code in + shownCode = ShownCode(title: title, code: code) + } + Color.clear.frame(height: 1).id("bottom") } + .padding(12) + .frame(maxWidth: .infinity, alignment: .leading) } - .padding(12) + .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) } + } + if hasUnappliedVersion { + Divider() + applyBar } + Divider() + composer } - .onChange(of: session.phase) { _, phase in - consume(phase: phase) + .onAppear { + isConfigured = AISettingsStore.shared.load().isConfigured + skills = AIWidgetSkillManager.shared.allSkills() + lookAtEditor() + } + .onDisappear { + requestID = UUID() + session.cancel() + } + .onChange(of: documentID) { _, _ in lookAtEditor() } + .onReceive(NotificationCenter.default.publisher(for: AISettingsStore.changedNotification)) { _ in + isConfigured = AISettingsStore.shared.load().isConfigured } - .onChange(of: documentID) { _, _ in resetProposal() } - .onDisappear { resetProposal() } - .onAppear { skills = AIWidgetSkillManager.shared.allSkills() } .onReceive(NotificationCenter.default.publisher(for: AIWidgetSkillManager.changedNotification)) { _ in skills = AIWidgetSkillManager.shared.allSkills() } - } - - private var proposalReview: some View { - VStack(alignment: .leading, spacing: 10) { - HStack { - Text("Review Proposal").font(.headline) - Spacer() - Text("\(changeSummary.changedLines) changed lines") - .font(.caption.monospacedDigit()) - .foregroundStyle(.secondary) - } - - DisclosureGroup("Original · \(changeSummary.originalLines) lines", isExpanded: $showingOriginal) { - codeBlock(originalCode) - } - DisclosureGroup("Proposed · \(changeSummary.proposedLines) lines", isExpanded: $showingProposal) { - codeBlock(proposedCode) + .sheet(item: $shownCode) { shown in + VStack(alignment: .leading) { + HStack { + Text(shown.title).font(.headline) + Spacer() + Button("Done") { shownCode = nil }.keyboardShortcut(.defaultAction) + } + ScrollView { + Text(shown.code) + .font(.system(.footnote, design: .monospaced)) + .frame(maxWidth: .infinity, alignment: .leading) + .padding(8) + .textSelection(.enabled) + } } + .padding(12) + .frame(minWidth: 520, minHeight: 420) + } + .confirmationDialog("Allow this widget to use the network?", isPresented: $confirmingNetwork) { + Button("Allow and Apply") { apply(allowingHosts: true) } + Button("Cancel", role: .cancel) { } + } message: { + Text("This version contacts: \(hostsToAllow.joined(separator: ", ")). Applying it adds the network permission for these hosts to the widget. You can change this in the Config tab.") + } + } - HStack { - Button("Reject", role: .destructive) { - proposedCode = "" - statusMessage = "Proposal rejected; the editor was not changed." + // MARK: - parts + + private var header: some View { + HStack(alignment: .firstTextBaseline) { + Label("AI Copilot", systemImage: "sparkles") + .font(.headline) + Spacer() + Menu { + Section("Skills for this conversation") { + ForEach(skills) { skill in + Toggle(isOn: skillBinding(skill.id)) { Label(skill.title, systemImage: skill.symbol) } + } } - Spacer() - Button("Apply to Editor", systemImage: "checkmark") { - applyProposal() + Button("Manage Skills…", systemImage: "slider.horizontal.3") { + NotificationCenter.default.post(name: SkillManagerOpenRequest.notification, object: nil) } - .buttonStyle(.borderedProminent) + Divider() + Button("Start a New Conversation", systemImage: "plus") { + session.cancel() + onStartOver() + } + .disabled(conversation.turns.isEmpty) + } label: { + Image(systemName: "ellipsis.circle") } - .disabled(isApplying || proposalSnapshot == nil) + .menuStyle(.borderlessButton) + .fixedSize() + .accessibilityLabel("Copilot options") } } - private func codeBlock(_ code: String) -> some View { - ScrollView(.horizontal) { - Text(code) - .font(.system(.caption, design: .monospaced)) + @ViewBuilder + private var notices: some View { + if conversation.turns.isEmpty { + Text("Say what to change. The AI works on a copy: you see the result here first, and the widget changes only when you apply it.") + .font(.caption) + .foregroundStyle(.secondary) + } + if !isConfigured { + Label("Set up an AI provider in Settings → AI first.", systemImage: "exclamationmark.triangle") + .font(.caption) + .foregroundStyle(.orange) + } else if package.readonly { + Label("This widget is read-only.", systemImage: "lock") + .font(.caption) + .foregroundStyle(.secondary) + } else if !isEntryDocument { + Label("The conversation changes the widget's main file. Select it in the file list to continue.", systemImage: "doc") + .font(.caption) + .foregroundStyle(.secondary) + } + if let statusMessage { + Text(statusMessage) + .font(.caption) + .foregroundStyle(.secondary) .textSelection(.enabled) - .frame(maxWidth: .infinity, alignment: .leading) - .padding(8) } - .frame(maxHeight: 180) - .background(Color(nsColor: .textBackgroundColor), in: .rect(cornerRadius: 6)) } - private func resetProposal() { - requestID = UUID() - proposalSnapshot = nil - proposedCode = "" - isApplying = false - isReading = false - statusMessage = nil - session.cancel() + /// The version on screen, drawn small. It is the AI's copy, not the editor's document. + private var preview: some View { + let size = conversation.size.previewSize + let scale = min(1, 150 / size.height, 300 / size.width) + return VStack(alignment: .leading, spacing: 4) { + Group { + if let element = session.previewElement, let previewPackage = session.previewPackage { + ScriptWidgetElementView( + element: element, + context: ScriptWidgetElementContext( + runtime: nil, debugMode: false, scriptName: scriptModel.name, + scriptParameter: "", package: previewPackage + ) + ) + .frame(width: size.width, height: size.height) + .background(Color(NSColor.textBackgroundColor)) + .cornerRadius(10) + .scaleEffect(scale) + .allowsHitTesting(false) + } else { + ProgressView().controlSize(.small) + } + } + .frame(width: size.width * scale, height: size.height * scale) + Text(hasUnappliedVersion + ? "Version \(conversation.currentVersion?.number ?? 0), not applied yet" + : "Version \(conversation.currentVersion?.number ?? 0)") + .font(.caption2) + .foregroundStyle(.secondary) + if !session.currentDomains.isEmpty { + Label("May contact: \(session.currentDomains.joined(separator: ", "))", systemImage: "globe") + .font(.caption2) + .foregroundStyle(.secondary) + .lineLimit(2) + } + } } - private func applyProposal() { - guard !isApplying, let snapshot = proposalSnapshot else { return } - let code = proposedCode, token = requestID - isApplying = true - statusMessage = "Applying and saving…" - EditorService.replaceDocument(with: code, snapshot: snapshot) { result in - guard requestID == token else { return } - isApplying = false - statusMessage = result.message - if result == .saved { - originalCode = code - proposedCode = "" - proposalSnapshot = nil + private var applyBar: some View { + let change = AICopilotChangeSummary.compare(original: knownEditorContent ?? "", proposed: session.currentSource) + return HStack { + VStack(alignment: .leading, spacing: 2) { + Text("Version \(conversation.currentVersion?.number ?? 0) is not in the editor yet") + .font(.callout.weight(.medium)) + Text("\(change.changedLines) changed lines") + .font(.caption.monospacedDigit()) + .foregroundStyle(.secondary) + } + Spacer() + Button("Show Code") { + shownCode = ShownCode(title: "Version \(conversation.currentVersion?.number ?? 0)", code: session.currentSource) + } + .controlSize(.small) + Button("Apply to Editor", systemImage: "checkmark") { + if hostsToAllow.isEmpty { apply(allowingHosts: false) } else { confirmingNetwork = true } } + .buttonStyle(.borderedProminent) + .controlSize(.small) + .disabled(isApplying || !canWork) } + .padding(10) } - private func beginCopilotRequest(fixCurrentError: Bool) { - let trimmed = instruction.trimmingCharacters(in: .whitespacesAndNewlines) - let diagnostic = PreviewDiagnosticStore.shared.diagnostic( - for: scriptModel.package.path.standardizedFileURL.path - ) - let requestText: String - if fixCurrentError { - requestText = trimmed.isEmpty ? "Fix the current runtime error while preserving the widget design." : trimmed - } else { - guard !trimmed.isEmpty else { - statusMessage = "Describe the change you want first." - return + private var composer: some View { + VStack(alignment: .leading, spacing: 6) { + HStack(alignment: .bottom, spacing: 8) { + TextField("Say what to change…", text: $draftText, axis: .vertical) + .lineLimit(1...5) + .textFieldStyle(.plain) + .padding(8) + .background(Color(nsColor: .textBackgroundColor), in: .rect(cornerRadius: 8)) + .overlay { + RoundedRectangle(cornerRadius: 8, style: .continuous) + .stroke(Color(nsColor: .separatorColor), lineWidth: 1) + } + .onSubmit { send(fixCurrentError: false) } + .accessibilityLabel("Message to the AI") + Button { send(fixCurrentError: false) } label: { + Image(systemName: "arrow.up.circle.fill").font(.title2) + } + .buttonStyle(.plain) + .disabled(!canSend) + .accessibilityLabel("Send") } - requestText = trimmed + Button("Fix Runtime Error", systemImage: "wrench.and.screwdriver") { send(fixCurrentError: true) } + .controlSize(.small) + .disabled(!canWork || session.isRunning || isReading || isApplying) } + .padding(10) + } + + private var canSend: Bool { + canWork && !session.isRunning && !isReading && !isApplying + && !draftText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty + } - guard !isReading, !isApplying, let documentID else { return } + // MARK: - actions + + /// Reads the open document. Code the conversation has never seen (the user + /// edited it, or it is the first look) becomes a version; a pending AI + /// version is left alone. + private func lookAtEditor(then next: ((StudioDocumentSnapshot) -> Void)? = nil) { + guard isEntryDocument, let documentID, !isReading else { return } let token = UUID() requestID = token isReading = true @@ -248,47 +410,85 @@ private struct StudioCopilotView: View { guard requestID == token else { return } isReading = false guard let snapshot else { - statusMessage = "The editor is not ready yet." + if next != nil { statusMessage = "The editor is not ready yet." } return } - proposalSnapshot = snapshot - originalCode = snapshot.content - proposedCode = "" - statusMessage = diagnostic == nil && fixCurrentError - ? "No current runtime error was recorded; asking AI to inspect the code." - : nil - session.copilot( - currentCode: snapshot.content, - instruction: AIWidgetSkills.augment(requestText, selectedIDs: selectedSkillIDs), - runtimeDiagnostic: diagnostic - ) + knownEditorContent = snapshot.content + if !conversation.versions.isEmpty || next != nil { + session.adoptEditorSource(snapshot.content) + } + next?(snapshot) + } + } + + private func send(fixCurrentError: Bool) { + let typed = draftText.trimmingCharacters(in: .whitespacesAndNewlines) + let request = fixCurrentError && typed.isEmpty + ? "Fix the current runtime error while keeping the widget's design." + : typed + guard canWork, !request.isEmpty, !session.isRunning, !isReading, !isApplying else { return } + let diagnostic = fixCurrentError + ? PreviewDiagnosticStore.shared.diagnostic(for: package.path.standardizedFileURL.path) + : nil + statusMessage = fixCurrentError && diagnostic == nil + ? "No runtime error was recorded; asking the AI to look at the code." + : nil + draftText = "" + lookAtEditor { _ in + session.send(request, runtimeDiagnostic: diagnostic) } } - private func consume(phase: AIGenerateSession.Phase) { - guard proposalSnapshot != nil else { return } - switch phase { - case .done(let jsx): - proposedCode = jsx - statusMessage = "Proposal passed the local ScriptWidget runtime." - case .exhausted(let lastJSX, let error): - proposedCode = lastJSX ?? "" - statusMessage = "The AI reached its iteration limit: \(error ?? "unknown runtime error")" - case .failed(let message): - statusMessage = message - case .cancelled: - statusMessage = "Request cancelled." - default: - break + /// Writes the version on screen into the open document. The document must + /// still be what this panel last saw; the editor checks again on its side. + private func apply(allowingHosts: Bool) { + guard canWork, !isApplying, let documentID else { return } + let source = session.currentSource + let expected = knownEditorContent + let token = UUID() + requestID = token + isApplying = true + statusMessage = "Applying and saving…" + EditorService.requestSnapshot(target: StudioEditorTarget(editorID: editorID, documentID: documentID)) { snapshot in + guard requestID == token else { return } + guard let snapshot else { + isApplying = false + statusMessage = StudioDocumentApplyResult.unavailable.message + return + } + guard snapshot.content == expected else { + isApplying = false + knownEditorContent = snapshot.content + session.adoptEditorSource(snapshot.content) + statusMessage = "The file changed in the editor, so nothing was applied. The conversation now continues from the editor's code." + return + } + if allowingHosts, !hostsToAllow.isEmpty, var updated = package.readManifest() { + if !updated.permissions.contains(.network) { updated.permissions.append(.network) } + for host in hostsToAllow where !updated.networkDomains.contains(host) { updated.networkDomains.append(host) } + let written = package.writeManifest(updated) + guard written.0 else { + isApplying = false + statusMessage = "The network permission could not be added, so nothing was applied: \(written.1)" + return + } + } + EditorService.replaceDocument(with: source, snapshot: snapshot) { result in + guard requestID == token else { return } + isApplying = false + statusMessage = result.message + if result == .saved { knownEditorContent = source } + } } } private func skillBinding(_ id: String) -> Binding { Binding( - get: { selectedSkillIDs.contains(id) }, + get: { conversation.skillIDs.contains(id) }, set: { enabled in - if enabled { selectedSkillIDs.insert(id) } - else { selectedSkillIDs.remove(id) } + var ids = Set(conversation.skillIDs) + if enabled { ids.insert(id) } else { ids.remove(id) } + session.setSkills(ids) } ) }