Repository Public
1// SP Notes Core2// File: Sources/SPNotesAPI/NotesAPI+AIConversations.swift3// Description: AI bookkeeping routes that never call a provider: conversations (list, read, rename, pin, delete,4// restore), usage, and the dry-run plan of a chat turn or an inline action.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation910extension NotesAPI {11 // MARK: - Conversations1213 /// `GET /api/ai/conversations[?pageId=]` — the live conversations (≤ 100), pinned first then most recent.14 public func aiConversations(pageID: String? = nil) async throws -> [AIConversation] {15 try await client.decode([AIConversation].self, .get("/api/ai/conversations", query: Self.item("pageId", pageID)))16 }1718 /// `GET /api/ai/conversations?id=` — one conversation with its messages. `404` (no code) when unknown or deleted.19 public func aiConversation(id: String) async throws -> AIConversationDetail {20 try await client.decode(AIConversationDetail.self, .get("/api/ai/conversations", query: [URLQueryItem(name: "id", value: id)]))21 }2223 private struct ConversationPatchBody: Encodable {24 var id: String25 var changes: AIConversationChanges26 enum CodingKeys: String, CodingKey { case id, title, pinned, model }27 func encode(to encoder: Encoder) throws {28 var c = encoder.container(keyedBy: CodingKeys.self)29 try c.encode(id, forKey: .id)30 try c.encodeIfPresent(changes.title, forKey: .title)31 try c.encodeIfPresent(changes.pinned, forKey: .pinned)32 try c.encodePatch(changes.model, forKey: .model)33 }34 }3536 /// `PATCH /api/ai/conversations` `{ id, title?, pinned?, model? }` — renames, pins or sets the model.37 /// Observed: answers `{ ok: true }` even for an unknown id.38 @discardableResult39 public func updateAIConversation(id: String, _ changes: AIConversationChanges, idempotencyKey: String? = nil) async throws -> OKResult {40 try await client.decode(OKResult.self, .json(.patch, "/api/ai/conversations", body: ConversationPatchBody(id: id, changes: changes), idempotencyKey: idempotencyKey))41 }4243 /// `DELETE /api/ai/conversations?id=` — soft delete (the conversation leaves the list). 200 even when unknown.44 @discardableResult45 public func deleteAIConversation(id: String, idempotencyKey: String? = nil) async throws -> OKResult {46 try await client.decode(OKResult.self, HTTPRequest(method: .delete, path: "/api/ai/conversations", query: [URLQueryItem(name: "id", value: id)], idempotencyKey: idempotencyKey))47 }4849 /// `DELETE /api/ai/conversations?id=&restore=1` — undo of a delete.50 @discardableResult51 public func restoreAIConversation(id: String, idempotencyKey: String? = nil) async throws -> OKResult {52 try await client.decode(OKResult.self, HTTPRequest(method: .delete, path: "/api/ai/conversations", query: [URLQueryItem(name: "id", value: id), URLQueryItem(name: "restore", value: "1")], idempotencyKey: idempotencyKey))53 }5455 // MARK: - Usage5657 /// `GET /api/ai/usage[?days=]` — cost and calls by model, day and task (≤ 90 days, server default 30) and the58 /// most recent calls. The daily budget is in `settings().aiBudgetUsd` and `dashboard().usage`.59 public func aiUsage(days: Int? = nil) async throws -> AIUsageReport {60 try await client.decode(AIUsageReport.self, .get("/api/ai/usage", query: Self.item("days", days.map(String.init))))61 }6263 // MARK: - Plan6465 /// `GET /api/ai/chat/plan?mode=&pageId=&model=&chars=&images=` — where a chat turn would go and its estimated cost.66 /// - Parameter chars: length of the message about to be sent (the text itself never leaves the device).67 public func aiChatPlan(mode: AIChatMode? = nil, pageID: String? = nil, model: String? = nil, chars: Int? = nil, images: Int? = nil) async throws -> AIPlan {68 let query = Self.item("mode", mode?.rawValue) + Self.item("pageId", pageID) + Self.item("model", model) + Self.item("chars", chars.map(String.init)) + Self.item("images", images.map(String.init))69 return try await client.decode(AIPlan.self, .get("/api/ai/chat/plan", query: query))70 }7172 /// `GET /api/ai/chat/plan?action=&chars=&model=` — the plan of one inline action on a selection of `chars` characters.73 public func aiActionPlan(_ action: AIActionKind, chars: Int, model: String? = nil) async throws -> AIPlan {74 let query = [URLQueryItem(name: "action", value: action.rawValue), URLQueryItem(name: "chars", value: String(chars))] + Self.item("model", model)75 return try await client.decode(AIPlan.self, .get("/api/ai/chat/plan", query: query))76 }7778 /// `GET /api/ai/chat/plan?actions=1&chars=` — the plans of the actions of the selection sheet, keyed by action79 /// id (`rewrite`, `fix`, `translate`, `summarize`, `expand`, `explain`, `to_list`, `to_tasks`, `table`, `custom`).80 public func aiActionPlans(chars: Int) async throws -> [String: AIPlan] {81 try await client.decode([String: AIPlan].self, .get("/api/ai/chat/plan", query: [URLQueryItem(name: "actions", value: "1"), URLQueryItem(name: "chars", value: String(chars))]))82 }83}84