Something went wrong. Try again.
A small AI buddy that lives in the bottom right-hand corner of your Mac that wakes when you press a keybind.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129import AppKit
// MARK: - API wire types
struct APIMessage: Codable { let role: String let content: String /// JPEG-encoded images sent alongside the text (OpenAI-compatible vision format). var images: [Data] = [] /// Tool calls an assistant message made. var toolCalls: [ToolCall] = [] /// For a `tool` message, the call it answers. var toolCallId: String? = nil
/// JSON object for an OpenAI-compatible `/chat/completions` request. var jsonObject: [String: Any] { if let toolCallId { return ["role": role, "tool_call_id": toolCallId, "content": content] } if !toolCalls.isEmpty { return ["role": role, "content": content, "tool_calls": toolCalls.map(\.jsonObject)] } guard !images.isEmpty else { return ["role": role, "content": content] } var parts: [[String: Any]] = images.map { ["type": "image_url", "image_url": ["url": "data:image/jpeg;base64,\($0.base64EncodedString())"]] } if !content.isEmpty { parts.append(["type": "text", "text": content]) } return ["role": role, "content": parts] }}
struct ToolCall: Codable, Sendable { let id: String let name: String /// JSON-encoded arguments object. let arguments: String
var jsonObject: [String: Any] { ["id": id, "type": "function", "function": ["name": name, "arguments": arguments]] }
/// One string argument, or nil if the arguments don't parse. func string(_ key: String) -> String? { argument(key) }
/// One boolean argument, or nil if it's missing or the arguments don't parse. func bool(_ key: String) -> Bool? { argument(key) }
private func argument<T>(_ key: String) -> T? { guard let data = arguments.data(using: .utf8), let object = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else { return nil } return object[key] as? T }}
/// A function the chat model may call, with its JSON-schema parameters.struct ChatTool: @unchecked Sendable { let name: String let description: String let parameters: [String: Any]
var jsonObject: [String: Any] { ["type": "function", "function": ["name": name, "description": description, "parameters": parameters]] }}
// MARK: - UI chat types
enum ChatRole { case user, assistant}
struct ChatAttachment: Identifiable { let id = UUID() enum Kind { case screenshot(NSImage) case file(URL, String) } var kind: Kind
var displayName: String { switch kind { case .screenshot: return "Screenshot" case .file(_, let name): return name } }
var previewImage: NSImage? { switch kind { case .screenshot(let img): return img case .file: return nil } }
/// JPEG data downscaled to at most `maxDimension` pixels on the long edge, for sending to a model. func apiImageData(maxDimension: CGFloat = 2000) -> Data? { guard case .screenshot(let img) = kind, var cg = img.cgImage(forProposedRect: nil, context: nil, hints: nil) else { return nil }
let longEdge = CGFloat(max(cg.width, cg.height)) if longEdge > maxDimension { let scale = maxDimension / longEdge let w = Int(CGFloat(cg.width) * scale), h = Int(CGFloat(cg.height) * scale) if let ctx = CGContext(data: nil, width: w, height: h, bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue) { ctx.interpolationQuality = .high ctx.draw(cg, in: CGRect(x: 0, y: 0, width: w, height: h)) if let scaled = ctx.makeImage() { cg = scaled } } } return NSBitmapImageRep(cgImage: cg).representation(using: .jpeg, properties: [.compressionFactor: 0.85]) }}
struct ChatMessage: Identifiable { let id = UUID() var role: ChatRole var content: String var attachments: [ChatAttachment] = [] var isStreaming: Bool = false
var displayContent: String { isStreaming ? content + "▋" : content }}