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
181 changes: 147 additions & 34 deletions Shared/ScriptWidgetRuntime/AI/AIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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] {
Expand Down
75 changes: 73 additions & 2 deletions Shared/ScriptWidgetRuntime/AI/AIConversation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading