Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion Shared/ScriptWidgetRuntime/AI/AIConversation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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

Expand Down Expand Up @@ -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 {
Expand Down
35 changes: 29 additions & 6 deletions Shared/ScriptWidgetRuntime/AI/AIConversationSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ final class AIConversationSession: ObservableObject {
private let makeLoop: () -> AgentLoop
private let loadSettings: () -> AISettings
private let skillsText: (Set<String>) -> 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?
Expand All @@ -76,15 +78,18 @@ final class AIConversationSession: ObservableObject {
bridge: AgentRuntimeBridge = .shared,
makeLoop: @escaping () -> AgentLoop = { AgentLoop() },
loadSettings: @escaping () -> AISettings = { AISettingsStore.shared.load() },
skillsText: @escaping (Set<String>) -> String? = { AIWidgetSkills.instructions(selectedIDs: $0) }
skillsText: @escaping (Set<String>) -> String? = { AIWidgetSkills.instructions(selectedIDs: $0) },
sandbox: AgentSandboxSetup? = nil
) {
self.sandbox = sandbox
self.conversation = conversation
self.store = store
self.bridge = bridge
self.makeLoop = makeLoop
self.loadSettings = loadSettings
self.skillsText = skillsText
let package = try? bridge.makeSandboxPackage(prefix: "chat-preview")
if let package, let sandbox { bridge.seed(package, from: sandbox.seedDirectory) }
previewPackage = package
previewPackageURL = package?.path
if conversation.currentVersion != nil { refreshPreview() }
Expand All @@ -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 }

Expand All @@ -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)
Expand Down Expand Up @@ -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()
}
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions Shared/ScriptWidgetRuntime/AI/AIConversationStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand Down
36 changes: 34 additions & 2 deletions Shared/ScriptWidgetRuntime/AI/AgentLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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] = []
Expand Down Expand Up @@ -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)."
Expand Down
41 changes: 41 additions & 0 deletions Shared/ScriptWidgetRuntime/AI/AgentRuntimeBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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(
Expand Down
9 changes: 8 additions & 1 deletion Shared/ScriptWidgetRuntime/AI/PromptBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
Loading
Loading