SPB Git

Repository Public

GraphModels.swift 27.6 KB 744 lines swift

1// SP Notes Core2// File: Sources/SPNotesAPI/GraphModels.swift3// Description: Models of the knowledge graph 2.0 (API-NEXT §4.3): typed relations, concepts, the graph views4//              (and the 2.x answer without `view`), deterministic intelligence and stored insights.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation910// MARK: - Vocabulary1112/// `view` of `GET /api/graph`.13public enum GraphView {14    public static let global = "global"15    public static let local = "local"16    public static let project = "project"17    public static let timeline = "timeline"18    public static let concepts = "concepts"19    public static let documents = "documents"20    public static let references = "references"21    public static let all = [global, local, project, timeline, concepts, documents, references]22}2324/// `kind` of a graph node. `tag` only exists in the 2.x answer (no `view`).25public enum GraphNodeKind {26    public static let page = "page"27    public static let database = "database"28    public static let research = "research"29    public static let row = "row"30    public static let canvas = "canvas"31    public static let reference = "reference"32    public static let concept = "concept"33    public static let attachment = "attachment"34    public static let tag = "tag"35}3637/// `kind` of an edge **inside a graph view** (not the `kind` of a relation row: see `RelationKind`).38public enum GraphEdgeKind {39    /// A link written in a document (mention, link, block reference, canvas link).40    public static let link = "link"41    /// A typed relation (a row of kind `relation`): its `id` is usable with the relations routes.42    public static let typed = "typed"43    public static let aiSuggested = "ai_suggested"44    public static let cites = "cites"45    /// A reference attached to a page.46    public static let reference = "reference"47    /// Page → concept.48    public static let about = "about"49    /// A **database** relation (`sub` = the property name).50    public static let relation = "relation"51    public static let attachment = "attachment"52    /// 2.x answer only: page → tag.53    public static let tag = "tag"54}5556/// `kind` of a relation row (family of the edge): who wrote it and whether it can be edited.57public enum RelationKind {58    /// Derived from a document: not editable (`409 derived_edge`).59    public static let link = "link"60    /// Derived from a `citation`: not editable.61    public static let cites = "cites"62    /// A typed relation created by the user or suggested by an analysis job: editable.63    public static let relation = "relation"64    /// A 2.x link suggestion: accept / reject only.65    public static let aiSuggested = "ai_suggested"66}6768/// `relType`: the meaning of a relation, read `fromId —relType→ toId`.69public enum RelationType {70    public static let related = "related"71    public static let supports = "supports"72    public static let contradicts = "contradicts"73    public static let extends = "extends"74    public static let exampleOf = "example_of"75    public static let partOf = "part_of"76    public static let cites = "cites"77    public static let precedes = "precedes"78    public static let follows = "follows"79    public static let about = "about"80    public static let answers = "answers"81    public static let duplicates = "duplicates"82    public static let all = [related, supports, contradicts, extends, exampleOf, partOf, cites, precedes, follows, about, answers, duplicates]83}8485/// `sourceKind` / `targetKind` of a relation: what the two ends are.86public enum GraphEntityKind {87    public static let page = "page"88    public static let reference = "reference"89    public static let concept = "concept"90}9192/// `origin` of an edge, a concept or an insight. **Anything with `ai` must be shown as AI-made**; it never changes.93public enum KnowledgeOrigin {94    public static let user = "user"95    public static let ai = "ai"96    public static let system = "system"97}9899/// `status` of a relation, a concept or a concept ↔ page link.100public enum DecisionStatus {101    public static let suggested = "suggested"102    public static let accepted = "accepted"103    public static let rejected = "rejected"104}105106// MARK: - Relations107108/// A typed relation — the `edge` row of the change feed plus `fromTitle` / `toTitle`109/// (`POST | GET | PATCH /api/graph/relations[/<id>]`).110public struct GraphRelation: Codable, Sendable, Equatable, Identifiable {111    public var id: String112    public var fromId: String113    public var toId: String114    /// `GraphEntityKind`.115    public var sourceKind: String116    public var targetKind: String117    /// `RelationKind`.118    public var kind: String119    /// `RelationType`.120    public var relType: String121    /// `KnowledgeOrigin`.122    public var origin: String123    /// `DecisionStatus`.124    public var status: String125    public var confidence: Double?126    public var rationale: String?127    public var note: String?128    /// The model that suggested it (`origin: "ai"`).129    public var model: String?130    public var validFrom: Date?131    public var validTo: Date?132    public var sourceBlockId: String?133    public var targetBlockId: String?134    public var createdAt: Date135    public var updatedAt: Date?136    /// When the user decided (`null` while `suggested`).137    public var decidedAt: Date?138    public var rev: Int64?139    public var fromTitle: String?140    public var toTitle: String?141142    /// Whether the relation routes may change or delete it (`kind == "relation"`).143    public var isEditable: Bool { kind == RelationKind.relation }144    public var isAI: Bool { origin == KnowledgeOrigin.ai }145}146147/// Body of `POST /api/graph/relations`. With an `id` the creation is idempotent.148public struct RelationDraft: Encodable, Sendable, Equatable {149    public var id: String?150    public var fromId: String151    public var toId: String152    /// `GraphEntityKind` (server default `page`).153    public var sourceKind: String?154    public var targetKind: String?155    /// `RelationType`.156    public var relType: String157    /// ≤ 2 000 characters.158    public var note: String?159    public var validFrom: Date?160    public var validTo: Date?161    public var sourceBlockId: String?162    public var targetBlockId: String?163164    public init(id: String? = nil, fromId: String, toId: String, relType: String, sourceKind: String? = nil, targetKind: String? = nil,165                note: String? = nil, validFrom: Date? = nil, validTo: Date? = nil, sourceBlockId: String? = nil, targetBlockId: String? = nil) {166        self.id = id167        self.fromId = fromId168        self.toId = toId169        self.relType = relType170        self.sourceKind = sourceKind171        self.targetKind = targetKind172        self.note = note173        self.validFrom = validFrom174        self.validTo = validTo175        self.sourceBlockId = sourceBlockId176        self.targetBlockId = targetBlockId177    }178}179180/// Body of `PATCH /api/graph/relations/<id>`. Only what is set is sent; `.null` clears a nullable field.181public struct RelationChanges: Encodable, Sendable, Equatable {182    /// `DecisionStatus`. A change sets `decidedAt` (back to `null` for `suggested`).183    public var status: String?184    public var relType: String?185    public var note: PatchValue<String>?186    public var validFrom: PatchValue<Date>?187    public var validTo: PatchValue<Date>?188189    public init(status: String? = nil, relType: String? = nil, note: PatchValue<String>? = nil, validFrom: PatchValue<Date>? = nil, validTo: PatchValue<Date>? = nil) {190        self.status = status191        self.relType = relType192        self.note = note193        self.validFrom = validFrom194        self.validTo = validTo195    }196197    public var isEmpty: Bool { status == nil && relType == nil && note == nil && validFrom == nil && validTo == nil }198199    enum CodingKeys: String, CodingKey { case status, relType, note, validFrom, validTo }200201    public func encode(to encoder: Encoder) throws {202        var c = encoder.container(keyedBy: CodingKeys.self)203        try c.encodeIfPresent(status, forKey: .status)204        try c.encodeIfPresent(relType, forKey: .relType)205        try c.encodePatch(note, forKey: .note)206        try c.encodePatch(validFrom, forKey: .validFrom)207        try c.encodePatch(validTo, forKey: .validTo)208    }209}210211/// Filters of `GET /api/graph/relations` (newest first, ≤ 500).212public struct RelationFilter: Sendable, Equatable {213    /// Relations touching that node (page, reference or concept id).214    public var nodeId: String?215    public var status: String?216    public var relType: String?217    public var origin: String?218    /// Families added to the default (`relation` only): `RelationKind.cites`, `.aiSuggested`, `.link`.219    public var include: [String]220221    public init(nodeId: String? = nil, status: String? = nil, relType: String? = nil, origin: String? = nil, include: [String] = []) {222        self.nodeId = nodeId223        self.status = status224        self.relType = relType225        self.origin = origin226        self.include = include227    }228}229230struct RelationListEnvelope: Decodable { var relations: [GraphRelation] }231232// MARK: - Concepts233234/// One page of a concept (`pageLinks` of `GET | POST | PATCH /api/concepts/<id>`).235public struct ConceptPageLink: Codable, Sendable, Equatable {236    public var pageId: String237    public var title: String?238    public var icon: String?239    public var origin: String?240    /// `DecisionStatus`.241    public var status: String?242    public var confidence: Double?243    public var blockId: String?244    public var model: String?245    public var createdAt: Date?246}247248/// A concept — the `concept` row of the change feed plus its page counts, and `pageLinks` on the routes of one249/// concept (`/api/concepts`).250public struct Concept: Codable, Sendable, Equatable, Identifiable {251    public var id: String252    public var name: String253    public var description: String?254    /// `KnowledgeOrigin`.255    public var origin: String256    /// `DecisionStatus`.257    public var status: String258    public var model: String?259    public var createdAt: Date?260    public var updatedAt: Date?261    public var decidedAt: Date?262    public var rev: Int64?263    /// Accepted links to live pages.264    public var pages: Int?265    public var suggestedPages: Int?266    /// Absent from the list route.267    public var pageLinks: [ConceptPageLink]?268269    public var isAI: Bool { origin == KnowledgeOrigin.ai }270}271272/// Body of `POST /api/concepts`. With an `id` the creation is idempotent.273public struct ConceptDraft: Encodable, Sendable, Equatable {274    public var id: String?275    /// 1…120 characters; unique per workspace, case-insensitive (`409 concept_exists`).276    public var name: String277    /// ≤ 2 000 characters.278    public var description: String?279    /// ≤ 200 live pages (`400 bad_page`).280    public var pageIds: [String]?281282    public init(id: String? = nil, name: String, description: String? = nil, pageIds: [String]? = nil) {283        self.id = id284        self.name = name285        self.description = description286        self.pageIds = pageIds287    }288}289290/// A decision on one concept ↔ page link (`pageLinks` of `ConceptChanges`).291public struct ConceptPageDecision: Codable, Sendable, Equatable {292    public var pageId: String293    /// `DecisionStatus`.294    public var status: String295296    public init(pageId: String, status: String) {297        self.pageId = pageId298        self.status = status299    }300}301302/// Body of `PATCH /api/concepts/<id>` (or of `PATCH /api/concepts` with the id in the body).303public struct ConceptChanges: Encodable, Sendable, Equatable {304    public var name: String?305    public var description: String?306    /// `DecisionStatus`.307    public var status: String?308    public var addPages: [String]?309    public var removePages: [String]?310    /// Accept / reject suggested links.311    public var pageLinks: [ConceptPageDecision]?312313    public init(name: String? = nil, description: String? = nil, status: String? = nil, addPages: [String]? = nil, removePages: [String]? = nil, pageLinks: [ConceptPageDecision]? = nil) {314        self.name = name315        self.description = description316        self.status = status317        self.addPages = addPages318        self.removePages = removePages319        self.pageLinks = pageLinks320    }321322    public var isEmpty: Bool { name == nil && description == nil && status == nil && addPages == nil && removePages == nil && pageLinks == nil }323}324325struct ConceptListEnvelope: Decodable { var concepts: [Concept] }326327// MARK: - Graph views328329/// Parameters of `GET /api/graph?view=…`.330public struct GraphQuery: Sendable, Equatable {331    /// `GraphView`.332    public var view: String333    /// Required for `local` (the centre) and `project` (the root page).334    public var pageId: String?335    /// `local` only: 1…4 (server default 1).336    public var depth: Int?337    /// `timeline`: start of the range (server default: the last 90 days).338    public var from: Date?339    public var to: Date?340    /// `false` sends `suggestions=0`: hides suggested edges, concepts and concept links.341    public var includeSuggestions: Bool342    /// `true` sends `hideOrphans=1`: drops the nodes without an edge in the view.343    public var hideOrphans: Bool344345    public init(view: String, pageId: String? = nil, depth: Int? = nil, from: Date? = nil, to: Date? = nil, includeSuggestions: Bool = true, hideOrphans: Bool = false) {346        self.view = view347        self.pageId = pageId348        self.depth = depth349        self.from = from350        self.to = to351        self.includeSuggestions = includeSuggestions352        self.hideOrphans = hideOrphans353    }354355    public static func local(pageId: String, depth: Int? = nil) -> GraphQuery { GraphQuery(view: GraphView.local, pageId: pageId, depth: depth) }356    public static func project(pageId: String) -> GraphQuery { GraphQuery(view: GraphView.project, pageId: pageId) }357    public static func timeline(from: Date? = nil, to: Date? = nil) -> GraphQuery { GraphQuery(view: GraphView.timeline, from: from, to: to) }358}359360/// A node of a graph answer. Sorted by the server: degree (descending), title, id.361public struct GraphNode: Codable, Sendable, Equatable, Identifiable {362    /// The page uuid, or `ref:<uuid>`, `concept:<uuid>`, `att:<uuid>` (2.x answer: also `tag:<uuid>`).363    public var id: String364    /// The bare uuid (3.0 views only).365    public var entityId: String?366    /// `GraphNodeKind`.367    public var kind: String368    public var title: String?369    public var icon: String?370    public var section: String?371    /// 3.0 views only.372    public var createdAt: Date?373    public var updatedAt: Date?374    public var databaseId: String?375    /// Number of edges of the node within the answer.376    public var degree: Int?377    /// Incoming edges (`in`).378    public var inDegree: Int?379    /// Outgoing edges (`out`).380    public var outDegree: Int?381    /// Concepts only: `DecisionStatus`.382    public var status: String?383    /// Concepts only: `KnowledgeOrigin`.384    public var origin: String?385    /// `project` view: a page one hop outside the project.386    public var external: Bool?387388    enum CodingKeys: String, CodingKey {389        case id, entityId, kind, title, icon, section, createdAt, updatedAt, databaseId, degree, status, origin, external390        case inDegree = "in"391        case outDegree = "out"392    }393394    /// The uuid of the entity behind the node, whatever the answer (strips `ref:`, `concept:`, `att:`, `tag:`).395    public var entityUUID: String {396        if let entityId { return entityId }397        guard let colon = id.firstIndex(of: ":") else { return id }398        return String(id[id.index(after: colon)...])399    }400}401402/// An edge of a graph answer.403public struct GraphEdge: Codable, Sendable, Equatable, Identifiable {404    /// The uuid of the relation row when there is one; synthetic edges have ids starting with `e-`.405    public var id: String406    /// Node ids.407    public var source: String408    public var target: String409    /// `GraphEdgeKind`.410    public var kind: String411    /// `RelationType` (3.0 views only).412    public var relType: String?413    public var origin: String?414    public var status: String?415    public var confidence: Double?416    public var rationale: String?417    public var note: String?418    public var model: String?419    public var validFrom: Date?420    public var validTo: Date?421    public var sourceBlockId: String?422    public var targetBlockId: String?423    public var createdAt: Date?424    /// Database relations: the property name.425    public var sub: String?426427    /// Whether `id` is the id of a real row (usable with the relations routes when `kind == "typed"`).428    public var isSynthetic: Bool { id.hasPrefix("e-") }429}430431/// `stats` of a graph answer. `references` and `concepts` only exist in the 3.0 views.432public struct GraphStats: Codable, Sendable, Equatable {433    public var pages: Int434    public var edges: Int435    public var suggested: Int?436    public var references: Int?437    public var concepts: Int?438}439440/// `range` of the `timeline` view.441public struct GraphRange: Codable, Sendable, Equatable {442    public var from: Date443    public var to: Date444}445446/// One UTC day of the `timeline` view.447public struct GraphBucket: Codable, Sendable, Equatable {448    /// `YYYY-MM-DD` (UTC).449    public var date: String450    /// Pages created that day.451    public var created: Int452    /// Pages modified that day.453    public var updated: Int454    /// Edges created that day.455    public var edges: Int456}457458/// `GET /api/graph` — a 3.0 view (`view` set) or the unchanged 2.x answer (`view == nil`).459public struct KnowledgeGraph: Codable, Sendable, Equatable {460    /// `GraphView`; `nil` for the 2.x answer.461    public var view: String?462    public var nodes: [GraphNode]463    public var edges: [GraphEdge]464    /// Node id of the centre (`local`, `project`, 2.x with `pageId`).465    public var center: String?466    /// `timeline` only.467    public var range: GraphRange?468    public var buckets: [GraphBucket]?469    public var stats: GraphStats470}471472// MARK: - Insights473474/// A page as the knowledge routes list it (orphans of the intelligence and of the health report).475public struct KnowledgePageRef: Codable, Sendable, Equatable, Identifiable {476    public var id: String477    public var title: String?478    public var icon: String?479    public var section: String?480    public var updatedAt: Date?481}482483/// Counters of the intelligence.484public struct GraphIntelligenceStats: Codable, Sendable, Equatable {485    public var pages: Int486    public var edges: Int487    public var orphans: Int488    public var clusters: Int489    public var concepts: Int490    public var tags: Int491}492493/// A central page (PageRank over accepted page → page edges and database relations).494public struct GraphCentralPage: Codable, Sendable, Equatable, Identifiable {495    public var id: String496    public var title: String?497    public var icon: String?498    public var degree: Int?499    public var inDegree: Int?500    public var outDegree: Int?501    public var pagerank: Double?502503    enum CodingKeys: String, CodingKey {504        case id, title, icon, degree, pagerank505        case inDegree = "in"506        case outDegree = "out"507    }508}509510/// A central concept (by number of pages).511public struct GraphCentralConcept: Codable, Sendable, Equatable, Identifiable {512    public var id: String513    public var name: String514    public var status: String?515    public var origin: String?516    public var pages: Int?517}518519/// A member shown for a cluster.520public struct GraphClusterMember: Codable, Sendable, Equatable, Identifiable {521    public var id: String522    public var title: String?523    public var icon: String?524}525526/// A connected group of ≥ 2 pages. `id` is stable for a given member set.527public struct GraphCluster: Codable, Sendable, Equatable, Identifiable {528    public var id: String529    public var size: Int530    /// Title of the most central page.531    public var label: String?532    public var pageIds: [String]533    public var top: [GraphClusterMember]?534}535536/// A tag or an accepted concept carried by ≥ 2 pages.537public struct GraphRecurringTopic: Codable, Sendable, Equatable, Identifiable {538    /// `tag` or `concept`.539    public var kind: String540    public var id: String541    public var name: String542    public var pages: Int?543    public var pageIds: [String]544    public var lastActivityAt: Date?545}546547/// Pages that look like the same note (`duplicates` of the intelligence, `potentialDuplicates` of the health report).548public struct DuplicateGroup: Codable, Sendable, Equatable {549    /// `title` (same normalised title) or `content` (same text of ≥ 5 words).550    public var reason: String?551    public var pageIds: [String]552    public var title: String?553}554555/// `intelligence` of `GET /api/graph/insights`: computed from the data at each call, **no model**.556public struct GraphIntelligence: Codable, Sendable, Equatable {557    public var computedAt: Date?558    public var stats: GraphIntelligenceStats?559    public var centralPages: [GraphCentralPage]560    public var centralConcepts: [GraphCentralConcept]561    public var orphans: [KnowledgePageRef]562    public var clusters: [GraphCluster]563    public var recurringTopics: [GraphRecurringTopic]564    public var duplicates: [DuplicateGroup]565566    public init(from decoder: Decoder) throws {567        let c = try decoder.container(keyedBy: CodingKeys.self)568        computedAt = try c.decodeIfPresent(Date.self, forKey: .computedAt)569        stats = try c.decodeIfPresent(GraphIntelligenceStats.self, forKey: .stats)570        centralPages = try c.decodeIfPresent([GraphCentralPage].self, forKey: .centralPages) ?? []571        centralConcepts = try c.decodeIfPresent([GraphCentralConcept].self, forKey: .centralConcepts) ?? []572        orphans = try c.decodeIfPresent([KnowledgePageRef].self, forKey: .orphans) ?? []573        clusters = try c.decodeIfPresent([GraphCluster].self, forKey: .clusters) ?? []574        recurringTopics = try c.decodeIfPresent([GraphRecurringTopic].self, forKey: .recurringTopics) ?? []575        duplicates = try c.decodeIfPresent([DuplicateGroup].self, forKey: .duplicates) ?? []576    }577}578579/// `kind` of a stored insight.580public enum InsightKind {581    public static let contradiction = "contradiction"582    public static let openQuestion = "open_question"583    public static let cluster = "cluster"584    public static let recurringTopic = "recurring_topic"585    public static let duplicate = "duplicate"586}587588/// `status` of a stored insight.589public enum InsightStatus {590    public static let suggested = "suggested"591    public static let accepted = "accepted"592    public static let dismissed = "dismissed"593    /// Filter value of the list only.594    public static let all = "all"595}596597/// A stored finding awaiting a decision (`insights` of `GET /api/graph/insights`, answer of `PATCH …/<id>`).598public struct KnowledgeInsight: Codable, Sendable, Equatable, Identifiable {599    public var id: String600    /// `InsightKind`.601    public var kind: String602    /// `InsightStatus`.603    public var status: String604    public var title: String?605    public var detail: String?606    /// Free-form: `pageIds` always; `blockIds` when passages are cited; `a`, `b`, `confidence` for a contradiction;607    /// `source` for an open question.608    public var data: RawJSON?609    /// `system` (deterministic) or `ai`.610    public var origin: String?611    public var model: String?612    public var createdAt: Date?613    public var decidedAt: Date?614    /// Answer of the decision only: the `contradicts` relation recorded by accepting a contradiction.615    public var relationId: String?616617    public var isAI: Bool { origin == KnowledgeOrigin.ai }618619    private struct PageIDs: Decodable { var pageIds: [String]?; var blockIds: [String]? }620621    /// `data.pageIds`.622    public var pageIds: [String] { (try? data?.decode(PageIDs.self))?.pageIds ?? [] }623    /// `data.blockIds` (empty when no passage is cited).624    public var blockIds: [String] { (try? data?.decode(PageIDs.self))?.blockIds ?? [] }625}626627/// `counts` of `GET /api/graph/insights`.628public struct InsightCounts: Codable, Sendable, Equatable {629    public var suggested: Int630    public var accepted: Int631    public var dismissed: Int632}633634/// `GET /api/graph/insights`.635public struct GraphInsights: Codable, Sendable, Equatable {636    /// `nil` when asked without intelligence.637    public var intelligence: GraphIntelligence?638    public var insights: [KnowledgeInsight]639    public var counts: InsightCounts?640}641642/// Parameters of `GET /api/graph/insights`.643public struct InsightQuery: Sendable, Equatable {644    /// `InsightStatus` (server default `suggested`).645    public var status: String?646    /// `InsightKind`.647    public var kind: String?648    /// 1…100 (server default 20): caps each list of the intelligence. Observed on the 3.0 scratch server: the stored649    /// `insights` are **not** capped by it.650    public var limit: Int?651    /// `false` sends `intelligence=0` (`intelligence` is then `nil`).652    public var includeIntelligence: Bool653654    public init(status: String? = nil, kind: String? = nil, limit: Int? = nil, includeIntelligence: Bool = true) {655        self.status = status656        self.kind = kind657        self.limit = limit658        self.includeIntelligence = includeIntelligence659    }660}661662/// `mode` of `POST /api/graph/insights/run`.663public enum InsightRunMode {664    /// Deterministic part in the request **and** one AI job (paid).665    public static let all = "all"666    /// Clusters, recurring topics and duplicates only: no model.667    public static let deterministic = "deterministic"668    /// The AI job only (paid).669    public static let ai = "ai"670}671672/// Body of `POST /api/graph/insights/run`.673public struct InsightRunRequest: Encodable, Sendable, Equatable {674    /// `InsightRunMode` (server default `all`, which queues a paid AI job).675    public var mode: String?676    /// ≤ 40 pages.677    public var pageIds: [String]?678    /// 2…40 pages sent to the model (server default 24).679    public var limit: Int?680681    public init(mode: String? = nil, pageIds: [String]? = nil, limit: Int? = nil) {682        self.mode = mode683        self.pageIds = pageIds684        self.limit = limit685    }686687    /// No model, nothing queued.688    public static let deterministic = InsightRunRequest(mode: InsightRunMode.deterministic)689}690691/// What the deterministic part did.692public struct InsightRunCounts: Codable, Sendable, Equatable {693    public var created: Int694    public var considered: Int695}696697/// `POST /api/graph/insights/run` → 200, or 202 when a job was queued.698public struct InsightRunResult: Codable, Sendable, Equatable {699    public var mode: String700    public var deterministic: InsightRunCounts?701    /// Whether the AI job was queued (at most one per workspace per 5 minutes).702    public var queued: Bool703    public var jobId: String?704    /// Why nothing was queued.705    public var reason: String?706}707708// MARK: - Typed errors709710/// The object that already exists (`409 relation_exists`, `409 concept_exists`).711public struct ExistingEntity: Sendable, Equatable {712    public var id: String713    /// `DecisionStatus` of the existing object (not sent by every route).714    public var status: String?715}716717extension APIError {718    private func existing(_ expected: String) -> ExistingEntity? {719        guard status == 409, code == expected, let id = detailString("id") else { return nil }720        return ExistingEntity(id: id, status: detailString("status"))721    }722723    /// `409 relation_exists`: the relation with the same (from, to, relType).724    public var existingRelation: ExistingEntity? { existing(NextCode.relationExists) }725726    /// `409 concept_exists`: the concept with the same name.727    public var existingConcept: ExistingEntity? { existing(NextCode.conceptExists) }728729    /// `400 bad_node`: which end is wrong (`fromId` / `toId`); `nil` when both ends are the same node.730    public var badNodeField: String? {731        guard code == NextCode.badNode else { return nil }732        return detailString("field")733    }734735    /// `409 derived_edge`: the edge comes from a document (or is a link suggestion) and cannot be edited this way.736    public var isDerivedEdge: Bool { status == 409 && code == NextCode.derivedEdge }737738    /// `400 bad_page` of the concept routes: the page ids that are not live pages of the workspace.739    public var unknownPageIDs: [String]? {740        guard code == NextCode.badPage else { return nil }741        return try? detail("pageIds")?.decode([String].self)742    }743}744
Files main
Menu

Theme