diff --git a/Shared/ScriptWidgetRuntime/AI/AIClient.swift b/Shared/ScriptWidgetRuntime/AI/AIClient.swift index 848222ef..495b190a 100644 --- a/Shared/ScriptWidgetRuntime/AI/AIClient.swift +++ b/Shared/ScriptWidgetRuntime/AI/AIClient.swift @@ -70,13 +70,139 @@ actor AIClient { if settings.providerKind == .applePrivateCloudCompute { return try await applePrivateCloudComputeChat(messages: messages, settings: settings) } + let request = try await makeChatRequest(messages: messages, settings: settings, stream: false) + do { + let (data, httpResponse) = try await session.data(for: request) + guard let http = httpResponse as? HTTPURLResponse else { throw URLError(.badServerResponse) } + try Self.checkStatus(http.statusCode) + guard data.count <= Self.maximumResponseBytes else { throw AIClientError.upstream("The AI response exceeded the size limit.") } + let response = try JSONDecoder().decode(ChatCompletionObject.self, from: data) + guard let content = response.choices?.first?.message?.content, !content.isEmpty else { + throw AIClientError.emptyResponse + } + let usage = AITokenUsage( + promptTokens: response.usage?.promptTokens ?? 0, + completionTokens: response.usage?.completionTokens ?? 0, + totalTokens: response.usage?.totalTokens ?? 0 + ) + return AIChatResult(content: content, usage: usage) + } catch let err as AIClientError { + throw err + } catch { + throw AIClientError.upstream(error.localizedDescription) + } + } + + /// The same request as `chat`, read as it is written. `onText` receives the + /// whole text so far, no more often than every 80 ms and once at the end. + /// + /// A server that refuses or garbles the streamed form is asked again in the + /// plain form, as long as nothing was received yet. Authentication, rate + /// limit and cancellation are reported as they are. Apple Private Cloud + /// Compute answers in one piece. + func chatStream( + messages: [AIMessage], + settings: AISettings, + onText: @escaping @Sendable (String) async -> Void + ) async throws -> AIChatResult { + if settings.providerKind == .applePrivateCloudCompute { + let result = try await applePrivateCloudComputeChat(messages: messages, settings: settings) + await onText(result.content) + return result + } + let request = try await makeChatRequest(messages: messages, settings: settings, stream: true) + var content = "" + var usage = AITokenUsage.zero + do { + let (bytes, httpResponse) = try await session.bytes(for: request) + guard let http = httpResponse as? HTTPURLResponse else { throw URLError(.badServerResponse) } + if [401, 403, 429].contains(http.statusCode) { try Self.checkStatus(http.statusCode) } + guard (200..<300).contains(http.statusCode) else { throw StreamUnusable() } + var lastSent = Date.distantPast + var sawEvent = false + for try await line in bytes.lines { + try Task.checkCancellation() + guard line.hasPrefix("data:") else { + // A plain JSON body means the server ignored "stream". + if !sawEvent, line.hasPrefix("{") { throw StreamUnusable() } + continue + } + let payload = line.dropFirst(5).trimmingCharacters(in: .whitespaces) + if payload == "[DONE]" { break } + guard let chunk = try? JSONDecoder().decode(StreamChunk.self, from: Data(payload.utf8)) else { continue } + sawEvent = true + if let piece = chunk.choices?.first?.delta?.content, !piece.isEmpty { + content += piece + guard content.utf8.count <= Self.maximumResponseBytes else { + throw AIClientError.upstream("The AI response exceeded the size limit.") + } + if Date().timeIntervalSince(lastSent) >= 0.08 { + lastSent = Date() + await onText(content) + } + } + if let reported = chunk.usage { + usage = AITokenUsage( + promptTokens: reported.prompt_tokens ?? 0, + completionTokens: reported.completion_tokens ?? 0, + totalTokens: reported.total_tokens ?? 0 + ) + } + } + guard !content.isEmpty else { throw sawEvent ? AIClientError.emptyResponse : StreamUnusable() } + await onText(content) + return AIChatResult(content: content, usage: usage) + } catch is CancellationError { + throw CancellationError() + } catch let error as AIClientError { + throw error + } catch { + if Task.isCancelled { throw CancellationError() } + // Text already shown must not be replaced by a second, different answer. + guard content.isEmpty else { throw AIClientError.upstream(error.localizedDescription) } + let result = try await chat(messages: messages, settings: settings) + await onText(result.content) + return result + } + } + private struct StreamUnusable: Error {} + + private struct StreamChunk: Decodable { + struct Choice: Decodable { + struct Delta: Decodable { let content: String? } + let delta: Delta? + } + struct Usage: Decodable { + let prompt_tokens: Int? + let completion_tokens: Int? + let total_tokens: Int? + } + let choices: [Choice]? + let usage: Usage? + } + + private static let maximumResponseBytes = 4 * 1_048_576 + + private static func checkStatus(_ status: Int) throws { + guard !(200..<300).contains(status) else { return } + switch status { + case 401, 403: throw AIClientError.upstream("The AI server rejected authentication. Check this profile's credentials.") + case 404: throw AIClientError.upstream("The AI endpoint or model was not found. Check the API base URL and model name.") + case 429: throw AIClientError.upstream("The AI server's usage limit was reached. Wait and try again.") + default: throw AIClientError.upstream("The AI server could not complete the request (HTTP \(status)).") + } + } + + /// Builds the chat-completions request. The credential goes into the + /// Authorization header and nowhere else. + private func makeChatRequest(messages: [AIMessage], settings: AISettings, stream: Bool) async throws -> URLRequest { let trimmedKey = settings.apiKey.trimmingCharacters(in: .whitespacesAndNewlines) guard settings.authMethod == .none || !trimmedKey.isEmpty else { throw AIClientError.missingAPIKey } let baseURLString = settings.normalizedBaseURL - guard settings.profile.endpointURL != nil else { + guard let base = settings.profile.endpointURL else { throw AIClientError.invalidBaseURL(baseURLString) } @@ -109,50 +235,37 @@ actor AIClient { let modelId = settings.model.trimmingCharacters(in: .whitespacesAndNewlines) guard !modelId.isEmpty else { throw AIClientError.upstream("Choose a model before generating a widget.") } - let resolvedModel: Model = .custom(modelId) - let parameters = ChatCompletionParameters( messages: chatMessages, - model: resolvedModel, + model: .custom(modelId), temperature: settings.temperature ) + var request = URLRequest(url: base.appendingPathComponent("v1/chat/completions")) + request.httpMethod = "POST" + request.timeoutInterval = 120 + request.setValue("application/json", forHTTPHeaderField: "Content-Type") + if settings.authMethod != .none { + request.setValue("Bearer " + resolvedKey, forHTTPHeaderField: "Authorization") + } do { - guard let base = settings.profile.endpointURL else { throw AIClientError.invalidBaseURL(baseURLString) } - var request = URLRequest(url: base.appendingPathComponent("v1/chat/completions")) - request.httpMethod = "POST" - request.timeoutInterval = 120 - request.setValue("application/json", forHTTPHeaderField: "Content-Type") - if settings.authMethod != .none { - request.setValue("Bearer " + resolvedKey, forHTTPHeaderField: "Authorization") - } - request.httpBody = try JSONEncoder().encode(parameters) - let (data, httpResponse) = try await session.data(for: request) - guard let http = httpResponse as? HTTPURLResponse else { throw URLError(.badServerResponse) } - guard (200..<300).contains(http.statusCode) else { - switch http.statusCode { - case 401, 403: throw AIClientError.upstream("The AI server rejected authentication. Check this profile's credentials.") - case 404: throw AIClientError.upstream("The AI endpoint or model was not found. Check the API base URL and model name.") - case 429: throw AIClientError.upstream("The AI server's usage limit was reached. Wait and try again.") - default: throw AIClientError.upstream("The AI server could not complete the request (HTTP \(http.statusCode)).") + var body = try JSONEncoder().encode(parameters) + if stream { + guard var object = try JSONSerialization.jsonObject(with: body) as? [String: Any] else { + throw AIClientError.upstream("The AI request could not be prepared.") } + object["stream"] = true + object["stream_options"] = ["include_usage": true] + body = try JSONSerialization.data(withJSONObject: object) + request.setValue("text/event-stream", forHTTPHeaderField: "Accept") } - guard data.count <= 4 * 1_048_576 else { throw AIClientError.upstream("The AI response exceeded the size limit.") } - let response = try JSONDecoder().decode(ChatCompletionObject.self, from: data) - guard let content = response.choices?.first?.message?.content, !content.isEmpty else { - throw AIClientError.emptyResponse - } - let usage = AITokenUsage( - promptTokens: response.usage?.promptTokens ?? 0, - completionTokens: response.usage?.completionTokens ?? 0, - totalTokens: response.usage?.totalTokens ?? 0 - ) - return AIChatResult(content: content, usage: usage) - } catch let err as AIClientError { - throw err + request.httpBody = body + } catch let error as AIClientError { + throw error } catch { throw AIClientError.upstream(error.localizedDescription) } + return request } func availableModels(profile: AIProfile) async throws -> [String] { diff --git a/Shared/ScriptWidgetRuntime/AI/AIConversation.swift b/Shared/ScriptWidgetRuntime/AI/AIConversation.swift index b4167cfb..9481c792 100644 --- a/Shared/ScriptWidgetRuntime/AI/AIConversation.swift +++ b/Shared/ScriptWidgetRuntime/AI/AIConversation.swift @@ -36,6 +36,76 @@ struct AIConversationVersion: Codable, Identifiable, Equatable { let source: String let parentID: UUID? let createdAt: Date + /// Hosts the widget declared it contacts; nil or empty for a widget that stays offline. + var networkDomains: [String]? +} + +/// What a generated widget says about its own network use. It is the model's +/// claim, so it is untrusted: it is checked like any manifest, shown to the +/// user before saving, and can only ever name the network permission. +struct AINetworkDeclaration: Equatable { + static let maximumDomains = 8 + + let domains: [String] + + enum ParseResult: Equatable { + case none + case declared(AINetworkDeclaration) + case invalid(String) + } + + /// Reads `{"permissions": ["network"], "networkDomains": [...]}`. + static func parse(json: String?) -> ParseResult { + guard let json, !json.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { return .none } + guard json.utf8.count <= 4_096, + let object = try? JSONSerialization.jsonObject(with: Data(json.utf8)) as? [String: Any] else { + return .invalid("it is not a JSON object") + } + guard Set(object.keys).isSubset(of: ["permissions", "networkDomains"]) else { + return .invalid("only \"permissions\" and \"networkDomains\" may appear") + } + let permissions = object["permissions"] as? [String] ?? [] + let domains = object["networkDomains"] as? [String] ?? [] + if permissions.isEmpty && domains.isEmpty { return .none } + guard permissions == ["network"] else { + return .invalid("\"permissions\" must be exactly [\"network\"]; no other permission can be declared") + } + guard !domains.isEmpty, domains.count <= maximumDomains else { + return .invalid("\"networkDomains\" must list 1 to \(maximumDomains) hosts") + } + let declaration = AINetworkDeclaration(domains: domains) + let scratch = ScriptWidgetPackage(path: FileManager.default.temporaryDirectory, readonly: true) + let report = WidgetPackageManifestValidator.validate( + declaration.manifest(name: "AI Preview"), package: scratch, checkEntryExists: false + ) + guard report.isValid else { + return .invalid(report.errors.map(\.message).joined(separator: " ")) + } + return .declared(declaration) + } + + /// A new Package 2.0 manifest that allows exactly these hosts and nothing else. + func manifest(name: String) -> WidgetPackageManifest { + Self.manifest(name: name, domains: domains) + } + + /// The manifest a widget saved from a conversation gets: no permission at + /// all, or the network permission for the declared hosts. + static func manifest(name: String, domains: [String]) -> WidgetPackageManifest { + var manifest = WidgetPackageManifest.newProject(name: name) + if !domains.isEmpty { + manifest.permissions = [.network] + manifest.networkDomains = domains + } + return manifest + } + + var json: String { + let data = try? JSONSerialization.data( + withJSONObject: ["permissions": ["network"], "networkDomains": domains], options: [.sortedKeys] + ) + return data.flatMap { String(data: $0, encoding: .utf8) } ?? "{}" + } } struct AIConversationTurn: Codable, Identifiable, Equatable { @@ -148,10 +218,11 @@ struct AIConversation: Codable, Identifiable, Equatable { /// Records a version made by a turn and makes it the current one. @discardableResult - mutating func addVersion(source: String, now: Date = Date()) -> AIConversationVersion { + mutating func addVersion(source: String, networkDomains: [String] = [], now: Date = Date()) -> AIConversationVersion { versionCounter += 1 let version = AIConversationVersion( - id: UUID(), number: versionCounter, source: source, parentID: currentVersionID, createdAt: now + id: UUID(), number: versionCounter, source: source, parentID: currentVersionID, createdAt: now, + networkDomains: networkDomains.isEmpty ? nil : networkDomains ) versions.append(version) currentVersionID = version.id diff --git a/Shared/ScriptWidgetRuntime/AI/AIConversationSession.swift b/Shared/ScriptWidgetRuntime/AI/AIConversationSession.swift index 292ce286..868add91 100644 --- a/Shared/ScriptWidgetRuntime/AI/AIConversationSession.swift +++ b/Shared/ScriptWidgetRuntime/AI/AIConversationSession.swift @@ -27,8 +27,11 @@ final class AIConversationSession: ObservableObject { @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 current version, rendered at the conversation's size and at the + /// thumbnail sizes. Rendering happens on this device and sends nothing. + @Published private(set) var previews: [AIWidgetSize: ScriptWidgetRuntimeElement] = [:] + /// The code of the reply being written, as far as it has arrived. + @Published private(set) var liveText = "" /// 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. @@ -41,7 +44,13 @@ final class AIConversationSession: ObservableObject { let lines: [String] } + /// The sizes always rendered next to the chosen one. + static let thumbnailSizes: [AIWidgetSize] = [.small, .medium, .large] + var isRunning: Bool { activity != .idle } + var previewElement: ScriptWidgetRuntimeElement? { previews[conversation.size] } + /// Hosts the version on screen may contact. + var currentDomains: [String] { conversation.currentVersion?.networkDomains ?? [] } var currentSource: String { conversation.currentVersion?.source ?? "" } private let store: AIConversationStore @@ -59,6 +68,7 @@ final class AIConversationSession: ObservableObject { private var generation = UUID() private var previewGeneration = UUID() private var pendingAttemptCode = "" + private var pendingDomains: [String] = [] init( conversation: AIConversation = AIConversation(), @@ -102,7 +112,8 @@ final class AIConversationSession: ObservableObject { previousCode: conversation.version(base?.parentID)?.source, request: turn.request, skillsText: skillsText(Set(conversation.skillIDs)), - size: conversation.size + size: conversation.size, + currentDomains: base?.networkDomains ?? [] ) persist() run(turnID: turn.id, context: context) @@ -125,6 +136,7 @@ final class AIConversationSession: ObservableObject { currentTask?.cancel() finishRunningTurns(as: .cancelled, message: nil) activity = .idle + liveText = "" persist() } @@ -144,7 +156,8 @@ final class AIConversationSession: ObservableObject { guard conversation.size != size else { return } conversation.size = size persist() - refreshPreview() + // A thumbnail size is already rendered; any other size is rendered now. + if previews[size] == nil { refreshPreview() } } func setSkills(_ ids: Set) { @@ -166,6 +179,8 @@ final class AIConversationSession: ObservableObject { self.generation = generation attemptLogs = [] pendingAttemptCode = "" + pendingDomains = [] + liveText = "" activity = .thinking(attempt: 1) let settings = loadSettings() @@ -189,8 +204,13 @@ final class AIConversationSession: ObservableObject { private func apply(event: AgentLoopEvent, turnID: UUID) { switch event { case .thinking(let attempt): + liveText = "" if case .repairing = activity { break } activity = .thinking(attempt: attempt) + case .partial(let text): + liveText = PromptBuilder.previewOfPartialReply(text) + case .declared(let domains): + pendingDomains = domains case .produced(_, let jsx): pendingAttemptCode = jsx case .running(let attempt): @@ -211,6 +231,7 @@ final class AIConversationSession: ObservableObject { private func apply(outcome: AgentLoopOutcome, turnID: UUID) { activity = .idle + liveText = "" switch outcome { case .succeeded(let jsx, let element, let usage): guard jsx.utf8.count <= AIConversationLimits.maxSourceBytes else { @@ -221,7 +242,7 @@ final class AIConversationSession: ObservableObject { } break } - let version = conversation.addVersion(source: jsx) + let version = conversation.addVersion(source: jsx, networkDomains: pendingDomains) conversation.updateTurn(turnID) { $0.status = .succeeded $0.versionID = version.id @@ -261,28 +282,33 @@ final class AIConversationSession: ObservableObject { } private func showPreview(element: ScriptWidgetRuntimeElement, source: String) { - previewGeneration = UUID() - previewTask?.cancel() - if let previewPackage { _ = previewPackage.writeMainFile(content: source) } - previewElement = element + // The loop already rendered the chosen size; the others follow. + previews = [conversation.size: element] + refreshPreview(keeping: [conversation.size]) } - /// Runs the current version again in the preview package. - private func refreshPreview() { - guard let previewPackage, let version = conversation.currentVersion else { - previewElement = nil - return - } + /// Runs the current version in the preview package: at the chosen size and + /// at the thumbnail sizes, with the permissions the version declares. + private func refreshPreview(keeping kept: Set = []) { let token = UUID() previewGeneration = token previewTask?.cancel() - let size = conversation.size + guard let previewPackage, let version = conversation.currentVersion else { + previews = [:] + return + } + 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 ?? []) 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 + for size in sizes { + let result = await bridge.run(jsx: version.source, in: previewPackage, size: size, manifest: manifest) + 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.previews[size] = result.element + } } } diff --git a/Shared/ScriptWidgetRuntime/AI/AIGenerateSession.swift b/Shared/ScriptWidgetRuntime/AI/AIGenerateSession.swift index 81e95ed5..206e42cd 100644 --- a/Shared/ScriptWidgetRuntime/AI/AIGenerateSession.swift +++ b/Shared/ScriptWidgetRuntime/AI/AIGenerateSession.swift @@ -170,7 +170,7 @@ final class AIGenerateSession: ObservableObject { )) case .tokensUsed(let u): usage = u - case .note: + case .note, .partial, .declared: break } } diff --git a/Shared/ScriptWidgetRuntime/AI/AgentLoop.swift b/Shared/ScriptWidgetRuntime/AI/AgentLoop.swift index 304a12fe..f32503ca 100644 --- a/Shared/ScriptWidgetRuntime/AI/AgentLoop.swift +++ b/Shared/ScriptWidgetRuntime/AI/AgentLoop.swift @@ -23,6 +23,10 @@ enum AgentLoopEvent { case tokensUsed(AITokenUsage) // incremental /// The model's one-sentence account of its change; conversation turns only. case note(String) + /// The reply so far, while it is being written; conversation turns only. + case partial(String) + /// Hosts the candidate about to run declares it contacts; conversation turns only. + case declared([String]) } struct AgentLoopRequest { @@ -116,7 +120,7 @@ final class AgentLoop { AIMessage(role: .assistant, content: previous), AIMessage(role: .user, content: PromptBuilder.userPromptFix( previousCode: previous, errorSummary: errMsg, recentLogs: lastLogs - )), + ) + (isTurn ? PromptBuilder.turnRepairSuffix : "")), ] } else { messages = [systemMessage, firstUserMessage] @@ -124,7 +128,13 @@ final class AgentLoop { let chatResult: AIChatResult do { - chatResult = try await client.chat(messages: messages, settings: request.settings) + if isTurn { + chatResult = try await client.chatStream(messages: messages, settings: request.settings) { text in + await onEvent(.partial(text)) + } + } else { + chatResult = try await client.chat(messages: messages, settings: request.settings) + } } catch { if Task.isCancelled { return .cancelled(usage: cumulativeUsage) } return .failed(message: error.localizedDescription, usage: cumulativeUsage) @@ -138,18 +148,39 @@ final class AgentLoop { } let jsx: String + var manifest: WidgetPackageManifest? + var declarationError: String? if isTurn { let reply = PromptBuilder.parseTurnReply(chatResult.content) jsx = reply.code if let note = reply.note { await onEvent(.note(note)) } + // A conversation's widget runs with the permissions it would have + // 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: []) + await onEvent(.declared([])) + case .declared(let declaration): + manifest = declaration.manifest(name: "AI Preview") + await onEvent(.declared(declaration.domains)) + case .invalid(let reason): + declarationError = "The network declaration block is not valid: \(reason)." + } } else { jsx = PromptBuilder.stripCodeFences(chatResult.content) } latestCode = jsx await onEvent(.produced(iteration: iteration, jsx: jsx)) + if let declarationError { + lastError = declarationError + lastLogs = [] + await onEvent(.ranFailed(iteration: iteration, errorSummary: declarationError, logs: [])) + continue + } + await onEvent(.running(iteration: iteration)) - let runResult = await bridge.run(jsx: jsx, in: package, size: request.size) + let runResult = await bridge.run(jsx: jsx, in: package, size: request.size, manifest: manifest) if Task.isCancelled { return .cancelled(usage: cumulativeUsage) diff --git a/Shared/ScriptWidgetRuntime/AI/AgentRuntimeBridge.swift b/Shared/ScriptWidgetRuntime/AI/AgentRuntimeBridge.swift index b3dddee8..608412ff 100644 --- a/Shared/ScriptWidgetRuntime/AI/AgentRuntimeBridge.swift +++ b/Shared/ScriptWidgetRuntime/AI/AgentRuntimeBridge.swift @@ -87,9 +87,23 @@ final class AgentRuntimeBridge { try? FileManager.default.removeItem(at: package.path) } - func run(jsx: String, in package: ScriptWidgetPackage, size: AIWidgetSize) async -> AgentRunResult { + /// Runs the code in the sandbox package. With a manifest, the package is a + /// Package 2.0 one and the code gets exactly the permissions written there, + /// as it would once saved; without, it is a legacy package. + func run( + jsx: String, in package: ScriptWidgetPackage, size: AIWidgetSize, manifest: WidgetPackageManifest? = nil + ) async -> AgentRunResult { return await withCheckedContinuation { continuation in runQueue.async { + if let manifest { + guard let data = try? JSONEncoder().encode(manifest), + ScriptWidgetPackageFiles.write(data, root: package.path, relative: "widget.json") else { + continuation.resume(returning: AgentRunResult( + element: nil, error: .internalError("Failed to write widget.json"), logs: [] + )) + return + } + } // Persist the JSX so packages that read themselves or // register support files still work. let writeResult = package.writeMainFile(content: jsx) diff --git a/Shared/ScriptWidgetRuntime/AI/PromptBuilder.swift b/Shared/ScriptWidgetRuntime/AI/PromptBuilder.swift index 9ee2a38a..d9bc144d 100644 --- a/Shared/ScriptWidgetRuntime/AI/PromptBuilder.swift +++ b/Shared/ScriptWidgetRuntime/AI/PromptBuilder.swift @@ -96,6 +96,8 @@ struct AITurnContext { /// Formatted text of the selected Skills; nil for none. let skillsText: String? let size: AIWidgetSize + /// Hosts the current widget declares it contacts. + var currentDomains: [String] = [] } struct AICopilotChangeSummary: Equatable { @@ -409,6 +411,8 @@ enum PromptBuilder { \(current) ``` + \(context.currentDomains.isEmpty ? "It declares no network use." : "Its network declaration: " + AINetworkDeclaration(domains: context.currentDomains).json) + The user now asks: \(context.request) @@ -421,15 +425,35 @@ enum PromptBuilder { 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. + then the FULL widget code in one ```jsx fenced block. + If, and only if, the widget uses `fetch` or `$http`, end with one more fenced block + that declares every host it contacts: + ```json + {"permissions": ["network"], "networkDomains": ["api.example.com"]} + ``` + Hosts are exact lowercase names or `*.example.com`. A host that is not declared is + blocked. No other permission can be declared. 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) + /// Splits a turn reply into the model's note, the widget code and the + /// network declaration, if any. A reply without the SUMMARY line or without + /// a fence is still read as code. + static func parseTurnReply(_ raw: String) -> (note: String?, code: String, declaration: String?) { + var text = raw.trimmingCharacters(in: .whitespacesAndNewlines) + + // The declaration is the last ```json block; take it out first so that + // it can never be mistaken for the widget. + var declaration: String? + if let open = text.range(of: "```json", options: .backwards), + let close = text.range(of: "```", range: open.upperBound.. String { + guard let open = partial.range(of: "```") else { return partial } + var code = partial[open.upperBound...] + if let newline = code.firstIndex(of: "\n") { code = code[code.index(after: newline)...] } else { return "" } + if let close = code.range(of: "```") { code = code[.. String { diff --git a/Tests/ScriptWidgetRuntimeTests/AIConversationTests.swift b/Tests/ScriptWidgetRuntimeTests/AIConversationTests.swift index bfd47c94..c4b2fd37 100644 --- a/Tests/ScriptWidgetRuntimeTests/AIConversationTests.swift +++ b/Tests/ScriptWidgetRuntimeTests/AIConversationTests.swift @@ -251,6 +251,176 @@ final class AIConversationTests: XCTestCase { await session.settle() } + // MARK: - streaming + + func testReplyIsReadAsItIsWrittenAndEndsAsOneText() async { + let content = "SUMMARY: Made a greeting.\n```jsx\n\(Self.goodWidget)\n```" + ConversationProviderStub.enqueue([.content(content)]) + let client = AIClient(session: urlSession) + var profile = AIProfile.makeDefault() + profile.baseURL = "http://conversation.test" + profile.model = "stub-model" + profile.apiKey = Self.secret + let collected = Collected() + let result = try? await client.chatStream( + messages: [.init(role: .user, content: "hi")], + settings: AISettings(profile: profile, maxIterations: 1, temperature: 0) + ) { text in await collected.append(text) } + + XCTAssertEqual(result?.content, content) + XCTAssertEqual(result?.usage.totalTokens, 15, "usage comes from the last event") + let texts = await collected.texts + XCTAssertEqual(texts.last, content) + XCTAssertTrue(texts.allSatisfy { content.hasPrefix($0) }, "every update is the text so far") + XCTAssertEqual(ConversationProviderStub.streamFlags, [true]) + } + + func testTurnUsesTheStreamedFormAndClearsTheLiveTextWhenDone() async { + ConversationProviderStub.enqueue([reply("Made a greeting.", Self.goodWidget)]) + let session = makeSession() + session.send("A greeting card") + await session.settle() + XCTAssertEqual(ConversationProviderStub.streamFlags, [true]) + XCTAssertEqual(session.conversation.turns[0].status, .succeeded) + XCTAssertEqual(session.conversation.turns[0].usage.totalTokens, 15) + XCTAssertEqual(session.liveText, "") + } + + func testServerWithoutStreamingIsAskedAgainInThePlainForm() async { + ConversationProviderStub.enqueue([.plainOnly("SUMMARY: Made a greeting.\n```jsx\n\(Self.goodWidget)\n```")]) + let session = makeSession() + session.send("A greeting card") + await session.settle() + XCTAssertEqual(session.conversation.turns[0].status, .succeeded) + XCTAssertEqual(session.currentSource, Self.goodWidget) + XCTAssertEqual(ConversationProviderStub.refusedStreams, 1) + XCTAssertEqual(ConversationProviderStub.streamFlags, [false]) + } + + func testPartialReplyShowsOnlyTheCode() { + XCTAssertEqual(PromptBuilder.previewOfPartialReply("SUMMARY: Made it.\n```jsx\n$render(a) + ``` + ```json + {"permissions": ["network"], "networkDomains": ["api.example.com"]} + ``` + """) + XCTAssertEqual(reply.note, "Shows a quote.") + XCTAssertEqual(reply.code, "$render(a)") + 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)") + XCTAssertNotNil(bare.declaration) + } + + func testDeclaredHostsAreKeptSentToTheNextTurnAndAreAllThePreviewMayReach() async throws { + let declaration = #"{"permissions": ["network"], "networkDomains": ["api.example.com"]}"# + ConversationProviderStub.enqueue([ + .content("SUMMARY: Made it.\n```jsx\n\(Self.goodWidget)\n```\n```json\n\(declaration)\n```"), + reply("Went offline.", Self.betterWidget), + ]) + let session = makeSession() + session.send("A quote of the day") + await session.settle() + + XCTAssertEqual(session.currentSource, Self.goodWidget, "the declaration is not part of the source") + XCTAssertEqual(session.currentDomains, ["api.example.com"]) + let package = try XCTUnwrap(session.previewPackage) + let manifest = try XCTUnwrap(package.readManifest()) + XCTAssertEqual(manifest.permissions, [.network]) + XCTAssertEqual(manifest.networkDomains, ["api.example.com"]) + XCTAssertTrue(ScriptWidgetNetworkPolicy.packageAllows(URL(string: "https://api.example.com/q")!, package: package)) + XCTAssertFalse(ScriptWidgetNetworkPolicy.packageAllows(URL(string: "https://other.example.net/")!, package: package)) + XCTAssertEqual(store.load(id: session.conversation.id)?.currentVersion?.networkDomains, ["api.example.com"]) + + session.send("Do it without the network") + await session.settle() + XCTAssertTrue(ConversationProviderStub.bodies[1].contains("api.example.com"), "the model is told what is declared") + XCTAssertEqual(session.currentDomains, [], "a reply without the block declares nothing") + let offline = try XCTUnwrap(package.readManifest()) + XCTAssertEqual(offline.permissions, []) + XCTAssertFalse(ScriptWidgetNetworkPolicy.packageAllows(URL(string: "https://api.example.com/q")!, package: package), + "a widget that declares nothing gets no network in the preview, as after saving") + } + + func testInvalidDeclarationIsNotRunAndIsSentBackForRepair() async { + let bad = #"{"permissions": ["network", "location"], "networkDomains": ["api.example.com"]}"# + ConversationProviderStub.enqueue([ + .content("SUMMARY: Made it.\n```jsx\n\(Self.goodWidget)\n```\n```json\n\(bad)\n```"), + reply("Made it.", Self.goodWidget), + ]) + let session = makeSession() + session.send("A quote of the day") + await session.settle() + + let turn = session.conversation.turns[0] + XCTAssertEqual(turn.status, .succeeded) + XCTAssertEqual(turn.repairs, 1) + XCTAssertEqual(session.currentDomains, []) + XCTAssertTrue(ConversationProviderStub.bodies[1].contains("network declaration block is not valid")) + XCTAssertTrue(ConversationProviderStub.bodies[1].contains("no other permission")) + } + + // MARK: - sizes + + func testThumbnailSizesAreRenderedLocally() async { + ConversationProviderStub.enqueue([reply("Made it.", "$render({$getenv(\"widget-size\")})")]) + let session = makeSession() + session.send("Show the size") + await session.settle() + for size in AIConversationSession.thumbnailSizes { + XCTAssertEqual(texts(session.previews[size]), [size.rawValue]) + } + XCTAssertEqual(ConversationProviderStub.bodies.count, 1) + + session.setSize(.accessoryRectangular) + await session.settle() + XCTAssertEqual(texts(session.previewElement), ["accessoryRectangular"]) + XCTAssertEqual(ConversationProviderStub.bodies.count, 1) + } + // MARK: - versions func testGoingBackAndContinuingLeavesTheLaterTurnBehind() async { @@ -445,6 +615,53 @@ final class AIConversationTests: XCTestCase { XCTAssertNotNil(session.previewElement) } + /// One turn that needs the network, against a real provider and a real + /// public host (httpbin.org). Runs only with ROCKY_OPENAI_APIKEY set. + func testNetworkWidgetAgainstARealProviderWhenConfigured() 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 network-widget 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 }) + + session.send("Fetch https://httpbin.org/uuid, which returns JSON like {\"uuid\": \"...\"}, and show the uuid in small text under the title Random ID. Show a short message if it can not be loaded.") + await session.settle() + + let turn = try XCTUnwrap(session.conversation.turns.last) + let report = """ + status: \(turn.status.rawValue), repairs: \(turn.repairs), tokens: \(turn.usage.totalTokens) + note: \(turn.note ?? "(none)") + error: \(turn.errorSummary ?? "(none)") + declared: \(session.currentDomains) + """ + print(report) + 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("network.jsx"), atomically: true, encoding: .utf8) + try report.write(to: output.appendingPathComponent("network-report.txt"), atomically: true, encoding: .utf8) + } + XCTAssertEqual(turn.status, .succeeded, turn.errorSummary ?? "") + XCTAssertEqual(session.currentDomains, ["httpbin.org"], "the widget declares the host it contacts") + XCTAssertTrue(session.currentSource.contains("httpbin.org")) + } + // MARK: - window snapshot (macOS, on request) #if os(macOS) @@ -538,6 +755,11 @@ final class AIConversationTests: XCTestCase { } } +private actor Collected { + private(set) var texts: [String] = [] + func append(_ text: String) { texts.append(text) } +} + /// Answers `http://conversation.test/v1/chat/completions` from a queue. final class ConversationProviderStub: URLProtocol { enum Reply { @@ -545,12 +767,21 @@ final class ConversationProviderStub: URLProtocol { case status(Int) /// Never answers; the request ends only when it is cancelled. case hang + /// Answered only in the plain form; the streamed form gets HTTP 400. + case plainOnly(String) } private static let lock = NSLock() private static var queue: [Reply] = [] private static var recordedBodies: [String] = [] private static var recordedAuthorizations: [String] = [] + private static var recordedStreamFlags: [Bool] = [] + private static var refused = 0 + + /// Whether each answered request asked for the streamed form. + static var streamFlags: [Bool] { lock.withLock { recordedStreamFlags } } + /// Streamed requests that were refused and asked again in the plain form. + static var refusedStreams: Int { lock.withLock { refused } } static var bodies: [String] { lock.withLock { recordedBodies } } static var authorizations: [String] { lock.withLock { recordedAuthorizations } } @@ -564,6 +795,8 @@ final class ConversationProviderStub: URLProtocol { queue = [] recordedBodies = [] recordedAuthorizations = [] + recordedStreamFlags = [] + refused = 0 } } @@ -572,34 +805,62 @@ final class ConversationProviderStub: URLProtocol { override func startLoading() { let body = Self.readBody(request) + let object = try? JSONSerialization.jsonObject(with: body) as? [String: Any] + let wantsStream = object?["stream"] as? Bool ?? false // 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 messages = object?["messages"] as? [[String: Any]] let text = (messages ?? []).compactMap { $0["content"] as? String }.joined(separator: "\n---\n") let next: Reply? = Self.lock.withLock { + // A server without streaming refuses the streamed form and is asked again. + if wantsStream, case .plainOnly = Self.queue.first { + Self.refused += 1 + return .status(400) + } Self.recordedBodies.append(text) + Self.recordedStreamFlags.append(wantsStream) Self.recordedAuthorizations.append(request.value(forHTTPHeaderField: "Authorization") ?? "") - return Self.queue.isEmpty ? nil : Self.queue.removeFirst() + return Self.queue.isEmpty ? .status(503) : 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()) + case .content(let content), .plainOnly(let content): + let usage: [String: Any] = ["prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15] + guard wantsStream else { + 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": usage, + ] + respond(200, (try? JSONSerialization.data(withJSONObject: payload)) ?? Data()) + return + } + // Server-sent events: the text in pieces, then the usage, then the end mark. + var events = "" + var rest = Substring(content) + while !rest.isEmpty { + let piece = rest.prefix(7) + rest = rest.dropFirst(7) + let chunk: [String: Any] = ["choices": [["index": 0, "delta": ["content": String(piece)]]]] + events += "data: " + Self.json(chunk) + "\n\n" + } + events += "data: " + Self.json(["choices": [], "usage": usage]) + "\n\n" + events += "data: [DONE]\n\n" + respond(200, Data(events.utf8), contentType: "text/event-stream") } } + private static func json(_ object: [String: Any]) -> String { + String(data: (try? JSONSerialization.data(withJSONObject: object)) ?? Data(), encoding: .utf8) ?? "{}" + } + override func stopLoading() {} - private func respond(_ status: Int, _ data: Data) { + private func respond(_ status: Int, _ data: Data, contentType: String = "application/json") { guard let url = request.url, let response = HTTPURLResponse( - url: url, statusCode: status, httpVersion: nil, headerFields: ["Content-Type": "application/json"] + url: url, statusCode: status, httpVersion: nil, headerFields: ["Content-Type": contentType] ) else { return } client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed) client?.urlProtocol(self, didLoad: data) diff --git a/docs/ai-conversation.md b/docs/ai-conversation.md index 82587eeb..f7a43fa3 100644 --- a/docs/ai-conversation.md +++ b/docs/ai-conversation.md @@ -17,7 +17,24 @@ iPhone and iPad still have the earlier single request with a Refine field; the c 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. +While the AI writes, the code appears under the request as it arrives. A provider that can not stream is asked in the plain way instead, and the code appears when it is complete. + +Under the preview the widget is drawn at the Small, Medium and Large sizes; a click shows that size above. The size picker offers the other sizes. All of this runs on the Mac and costs no tokens. A size larger than the preview area is drawn smaller to fit. + +![Three sizes under the preview and the host the widget declares](images/ai-conversation-mac-sizes.png) + +## Widgets that use the network + +A widget that fetches data must say which hosts it contacts. The AI is asked to end its reply with a short declaration, the same one the Android app uses: + +```json +{"permissions": ["network"], "networkDomains": ["api.example.com"]} +``` + +- The declaration is the AI's claim and is treated as untrusted. It may name only the network permission and at most 8 hosts, and it passes the same check as any `widget.json`. One that does not is not run; the AI is told why and tries again. +- The preview runs with exactly what was declared: a widget that declares nothing has no network, and a declared widget reaches its declared hosts only. This is what the saved widget will be allowed, so the preview no longer shows something that fails after saving. +- The hosts are shown under the preview. **Save Widget** asks before saving a widget that uses the network, and writes the permission and the hosts into the new widget's `widget.json`. +- Private and local hosts stay blocked by the runtime, whatever is declared. 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). @@ -47,7 +64,7 @@ Each turn sends one request, plus one for each repair: - 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. +The current widget's network declaration is sent with its source. 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 @@ -58,7 +75,9 @@ A repair also sends the error and the ends of the last ten console lines of the 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. +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`. ## Limits @@ -72,14 +91,15 @@ One difference from a saved widget remains: the preview package has no `widget.j ## 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. +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. +- `PromptBuilder.userPromptTurn` and `parseTurnReply`: the request and reply format. `AINetworkDeclaration` reads and checks the declaration. +- `AIClient.chatStream`: server-sent events, with the fallback to `chat`. - `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. diff --git a/docs/images/ai-conversation-mac-sizes.png b/docs/images/ai-conversation-mac-sizes.png new file mode 100644 index 00000000..dbcffc68 Binary files /dev/null and b/docs/images/ai-conversation-mac-sizes.png differ diff --git a/docs/images/ai-conversation-mac-streaming.png b/docs/images/ai-conversation-mac-streaming.png new file mode 100644 index 00000000..9ebaca29 Binary files /dev/null and b/docs/images/ai-conversation-mac-streaming.png differ diff --git a/macOS/ScriptWidgetMac/AIGenerate/AIGenerateWindowView.swift b/macOS/ScriptWidgetMac/AIGenerate/AIGenerateWindowView.swift index 2804835e..e69a2deb 100644 --- a/macOS/ScriptWidgetMac/AIGenerate/AIGenerateWindowView.swift +++ b/macOS/ScriptWidgetMac/AIGenerate/AIGenerateWindowView.swift @@ -46,6 +46,7 @@ private struct AIConversationWindowContent: View { @State private var saveError: String? @State private var shownCode: ShownCode? @State private var showingLogs = false + @State private var confirmingNetworkSave = false @State private var isDebugMode = false @State private var profiles: [AIProfile] = [] @State private var activeProfileID = "" @@ -101,7 +102,7 @@ private struct AIConversationWindowContent: View { } .sheet(item: $shownCode) { codeSheet($0) } .sheet(isPresented: $showingLogs) { logsSheet } - .alert("Save Failed", isPresented: saveErrorBinding) { + .alert("Saving the Widget", isPresented: saveErrorBinding) { Button("OK") { saveError = nil } } message: { Text(saveError ?? "") @@ -318,6 +319,16 @@ private struct AIConversationWindowContent: View { .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) @@ -507,13 +518,25 @@ private struct AIConversationWindowContent: View { } } .frame(maxWidth: .infinity) - .frame(minHeight: 360) + .frame(minHeight: 280) .clipShape(.rect(cornerRadius: StudioDesign.cardCornerRadius)) .overlay { RoundedRectangle(cornerRadius: StudioDesign.cardCornerRadius, style: .continuous) .stroke(StudioDesign.separator, lineWidth: 1) } + if conversation.currentVersion != nil { + thumbnails + } + + if !session.currentDomains.isEmpty { + Label("This version may contact: \(session.currentDomains.joined(separator: ", "))", systemImage: "globe") + .font(.caption) + .foregroundStyle(.secondary) + .lineLimit(2) + .textSelection(.enabled) + } + HStack { Button("Code", systemImage: "curlybraces") { shownCode = ShownCode( @@ -531,6 +554,57 @@ private struct AIConversationWindowContent: View { .padding(12) } + /// The widget at the three home-screen sizes; a click shows that size above. + private var thumbnails: some View { + HStack(alignment: .bottom, spacing: 12) { + ForEach(AIConversationSession.thumbnailSizes) { size in + let scale: CGFloat = 0.24 + let full = size.previewSize + Button { + session.setSize(size) + } label: { + VStack(spacing: 4) { + Group { + if let element = session.previews[size], let package = session.previewPackage { + ScriptWidgetElementView( + element: element, + context: ScriptWidgetElementContext( + runtime: nil, debugMode: false, scriptName: "AI Preview", + scriptParameter: "", package: package + ) + ) + .frame(width: full.width, height: full.height) + .background(Color(NSColor.textBackgroundColor)) + .cornerRadius(10) + .scaleEffect(scale) + .allowsHitTesting(false) + } else { + ProgressView().controlSize(.small) + } + } + .frame(width: full.width * scale, height: full.height * scale) + .overlay { + RoundedRectangle(cornerRadius: 4, style: .continuous) + .stroke(size == conversation.size ? Color.accentColor : StudioDesign.separator, + lineWidth: size == conversation.size ? 2 : 1) + } + Text(size.displayName) + .font(.caption2) + .foregroundStyle(size == conversation.size ? .primary : .secondary) + } + .contentShape(.rect) + } + .buttonStyle(.plain) + .accessibilityLabel("Show the \(size.displayName) size") + } + Spacer() + } + } + + 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 @@ -554,14 +628,16 @@ private struct AIConversationWindowContent: View { scriptParameter: "", package: package ) - ScrollView([.horizontal, .vertical]) { + // A size larger than the space is drawn smaller, never cut off. + GeometryReader { proxy in + let scale = min(1, (proxy.size.width - 32) / size.width, (proxy.size.height - 32) / size.height) 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) + .scaleEffect(max(0.1, scale)) + .frame(width: proxy.size.width, height: proxy.size.height) } - .defaultScrollAnchor(.center) } else if session.isRunning { VStack(spacing: StudioDesign.standardSpacing) { ProgressView() @@ -605,7 +681,18 @@ private struct AIConversationWindowContent: View { TextField("Widget name", text: $saveName) .textFieldStyle(.roundedBorder) .frame(minWidth: 200, maxWidth: 260) - Button("Save Widget", systemImage: "square.and.arrow.down", action: performSave) + Button("Save Widget", systemImage: "square.and.arrow.down") { + if session.currentDomains.isEmpty { performSave() } else { confirmingNetworkSave = true } + } + .confirmationDialog( + "Allow this widget to use the network?", + isPresented: $confirmingNetworkSave + ) { + Button("Save and Allow") { performSave() } + Button("Cancel", role: .cancel) { } + } message: { + Text("The AI wrote a widget that contacts: \(session.currentDomains.joined(separator: ", ")). Saving gives it the network permission for these hosts only. You can change this later in the widget's configuration.") + } .buttonStyle(.borderedProminent) .disabled(session.currentSource.isEmpty || session.isRunning || saveName.trimmingCharacters(in: .whitespaces).isEmpty) @@ -732,18 +819,42 @@ private struct AIConversationWindowContent: View { saveError = "Widget name can not be empty." return } + // Resolve the name first so that the package just created can be found again. + let packageName = sharedScriptManager.getValidPackageName(recommendPackageName: trimmed) + guard !packageName.isEmpty else { + saveError = "This name can not be used for a widget, or the widget library is not available." + return + } let result = sharedScriptManager.createScript( content: session.currentSource, - recommendPackageName: trimmed, + recommendPackageName: packageName, imageCopyPath: nil ) - if result.0 { - session.markSaved(packageName: trimmed) - NotificationCenter.default.post(name: SharedAppStore.scriptCreateNotification, object: nil) - WidgetCenter.shared.reloadAllTimelines() - dismiss() - } else { + guard result.0 else { saveError = result.1 + return + } + var warning: String? + let domains = session.currentDomains + if !domains.isEmpty { + // The declaration came from the model; writeManifest validates it again. + let package = sharedScriptManager.getScriptPackage(packageName: packageName) + if var manifest = package.readManifest() { + manifest.permissions = [.network] + manifest.networkDomains = domains + let written = package.writeManifest(manifest) + if !written.0 { warning = written.1 } + } else { + warning = "The widget's configuration could not be read." + } + } + session.markSaved(packageName: packageName) + NotificationCenter.default.post(name: SharedAppStore.scriptCreateNotification, object: nil) + WidgetCenter.shared.reloadAllTimelines() + if let warning { + saveError = "The widget was saved, but its network permission could not be added: \(warning) Add it in the widget's configuration." + } else { + dismiss() } } }