Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/cortex/blob/main/om/api3.om # cortex aien-dev/cortex Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `cortex / om / api3.om` 407 lines ยท 7097 bytes Raw Blame History 1 note api3.om: writes (entity upsert, claim upsert, retract, hard delete) and receipts. 2 text r_rec1 "{\"recorded\":true,\"receipt\":{\"id\":" 3 text r_rec2 ",\"operation\":" 4 text r_rec3 ",\"targetType\":" 5 text r_rec4 ",\"targetId\":" 6 text r_rec5 ",\"committedAt\":" 7 text t_eup "entity.upsert" 8 text t_cup "claim.upsert" 9 text t_entity "entity" 10 text t_claim "claim" 11 text t_retr ".retract" 12 text t_colsp ": " 13 text tobj0e "}}" 14 text r_del "{\"deleted\":" 15 16 note now as an RFC 3339 string (chrono to_rfc3339, AutoSi), staged in hs 17 fn stage_now to ulong 18 num_rfc realns false 19 ret stage_jtx 20 end 21 fn stage_uuid to ulong 22 ok = num_uuid false 23 ret stage_jtx 24 end 25 fn stage_jtx to ulong 26 n = get jtxl 0 27 o = get hst 0 28 for i ulong n 29 hsp add o i get jtx i 30 end 31 put hst 0 add o n 32 ret rf_hs o n 33 end 34 35 note ensure_space: the space slot for slug r (staged), creating it (uuid, now, next rowid) when missing 36 fn ensure_space r ulong to ulong 37 d = dict_intern r 38 s = sp_of d 39 if ne s NONE 40 ret s 41 end 42 idr = stage_uuid 43 ca = stage_now 44 st_commit 45 row = add get stv 3 1 46 s = sp_add row idr d ca 47 if not and lg_space s lg_end 48 trap 9 49 end 50 ret s 51 end 52 53 note receipt JSON for an operation text pair (op1 + op2), target type text and target id ref 54 fn o_receipt op1 view uchar * tt view uchar * tid ulong now ulong to void 55 idr = stage_uuid 56 js_raw r_rec1 57 o_hs idr 58 js_raw r_rec2 59 js_byte 34 60 js_raw op1 61 js_byte 34 62 js_raw r_rec3 63 js_esc_txt tt 64 js_raw r_rec4 65 o_hs tid 66 js_raw r_rec5 67 o_hs now 68 js_raw tobj0e 69 end 70 71 note ---- entity upsert (db.rs upsert_entity) ---- 72 fn api_write to ulong 73 k = get ap A_KIND 74 if eq k 1 75 ret api_upsert 76 end 77 if eq k 2 78 ret api_claim 79 end 80 ret api_retract 81 end 82 fn api_upsert to ulong 83 sp = get ap A_SPACE 84 if eq sp NONE 85 sp = stage_txt tdefsp 86 end 87 if not scope_ok sp 88 js_raw e403 89 ret 403 90 end 91 note Company Brain entries are append-only (brain.om): the generic write may not revise one 92 if bn_guard sp 93 ret 409 94 end 95 enc_fetch_w 96 s = ensure_space sp 97 d = sdi_of s 98 now = stage_now 99 e = mnm_find d get ap A_NAME 100 rev = long 1 101 if ne e NONE 102 rev = add get erev e 1 103 else 104 e = upsert_slot s d now 105 end 106 st_commit 107 upsert_apply e rev 108 if not and lg_entity e lg_end 109 trap 9 110 end 111 o_receipt t_eup t_entity get eid e now 112 ret 201 113 end 114 fn sdi_of s ulong to ulong 115 ret get sdi s 116 end 117 note no row with this name: the id (given or new) decides; ON CONFLICT(id) updates that row 118 fn upsert_slot s ulong d ulong now ulong to ulong 119 idr = get ap A_ID 120 if eq idr NONE 121 idr = stage_uuid 122 end 123 e = mid_find idr 124 if ne e NONE 125 ret e 126 end 127 e = get nent 0 128 if ge e EMAX 129 trap 9 130 end 131 put nent 0 add e 1 132 put erow e add get stv 0 1 133 put stv 0 get erow e 134 put eid e idr 135 put esid e get sid s 136 put esl e d 137 put eca e now 138 put eret e 2 139 put efts e 0 140 put eem e NONE 141 mid_add e 142 ret e 143 end 144 note write the input fields into slot e (keys change, so unlink first), then FTS delete + insert 145 fn upsert_apply e ulong rev long to void 146 if lt get eret e 2 147 ent_unlink e 148 end 149 put ety e dict_intern get ap A_TYPE 150 put enm e get ap A_NAME 151 put ect e get ap A_CONT 152 put eal e get ap A_ALIAS 153 put eav e 1 154 put emd e get ap A_META 155 put ecf e bitcast f64 get ap A_CONF 156 put erev e rev 157 put evf e get ap A_VF 158 put evt e get ap A_VT 159 m = get ap A_EMB 160 if ne m NONE 161 put eem e m 162 else 163 put eem e hc_from_disk get eem e 164 st_commit_hc 165 end 166 put eret e 0 167 ent_link e 168 st_fts_drop e 169 st_fts_new e 170 end 171 fn st_commit_hc to void 172 if gt get hcn 0 get hct 0 173 put hct 0 get hcn 0 174 end 175 end 176 177 note ---- claim upsert (db.rs upsert_claim) ---- 178 fn api_claim to ulong 179 sp = get ap A_SPACE 180 if eq sp NONE 181 sp = stage_txt tdefsp 182 end 183 if not scope_ok sp 184 js_raw e403 185 ret 403 186 end 187 s = ensure_space sp 188 idr = get ap A_ID 189 if eq idr NONE 190 idr = stage_uuid 191 end 192 now = stage_now 193 c = mcl_find idr 194 if eq c NONE 195 c = claim_new s idr now 196 end 197 st_commit 198 put cprd c get ap A_PRED 199 put cobj c get ap A_OBJ 200 put clit c get ap A_LIT 201 put ccf c bitcast f64 get ap A_CONF 202 put cmd c get ap A_META 203 put cret c 0 204 if not and lg_claim c lg_end 205 trap 9 206 end 207 o_receipt t_cup t_claim get cid c now 208 ret 201 209 end 210 fn claim_new s ulong idr ulong now ulong to ulong 211 c = get nc 0 212 if ge c CMAX 213 trap 9 214 end 215 put nc 0 add c 1 216 put crow c add get stv 2 1 217 put stv 2 get crow c 218 put cid c idr 219 put csid c get sid s 220 put csl c get sdi s 221 put csub c get ap A_SUBJ 222 put cca c now 223 mcl_add c 224 ret c 225 end 226 227 note ---- retract (db.rs retract_target) ---- 228 fn api_retract to ulong 229 bn_stale 230 tt = get ap A_TT 231 tid = get ap A_TID 232 now = stage_now 233 if hs_eqtxt tt t_entity 234 e = mid_find tid 235 if ne e NONE 236 if not scope_ok get dref get esl e 237 js_raw e403 238 ret 403 239 end 240 put eret e 1 241 st_fts_drop e 242 st_commit 243 if not and lg_idop OP_ERET tid lg_end 244 trap 9 245 end 246 else 247 if not scope_ok NONE 248 js_raw e403 249 ret 403 250 end 251 end 252 end 253 if hs_eqtxt tt t_claim 254 c = mcl_find tid 255 if ne c NONE 256 if not scope_ok get dref get csl c 257 js_raw e403 258 ret 403 259 end 260 put cret c 1 261 st_commit 262 if not and lg_idop OP_CRET tid lg_end 263 trap 9 264 end 265 else 266 if not scope_ok NONE 267 js_raw e403 268 ret 403 269 end 270 end 271 end 272 st_commit 273 o_receipt_rt tt tid now 274 ret 201 275 end 276 note receipt whose operation is ".retract" for any target type text 277 fn o_receipt_rt tt ulong tid ulong now ulong to void 278 idr = stage_uuid 279 js_raw r_rec1 280 o_hs idr 281 js_raw r_rec2 282 n = rf_len tt 283 o = rf_off tt 284 for i ulong n 285 put jes i hsg add o i 286 end 287 for i ulong len t_retr 288 put jes add n i get t_retr i 289 end 290 js_esc_buf 0 add n len t_retr 291 js_raw r_rec3 292 o_hs tt 293 js_raw r_rec4 294 o_hs tid 295 js_raw r_rec5 296 o_hs now 297 js_raw tobj0e 298 end 299 300 note ---- hard delete (extension, not in cortex-rs): POST /api/cortex/delete ---- 301 note {"targetType":"entity","targetId":ID} or {"space":SLUG}; data leaves the disk by compaction 302 fn api_delete to ulong 303 bn_stale 304 sp = get ap A_SPACE 305 n = ulong 0 306 if ne sp NONE 307 if not scope_ok sp 308 js_raw e403 309 ret 403 310 end 311 d = dict_find sp 312 if ne d NONE 313 n = del_count_space d 314 st_kill_space d 315 if not and lg_idop OP_SDEL sp lg_end 316 trap 9 317 end 318 end 319 else 320 e = mid_find get ap A_TID 321 if ne e NONE 322 if not scope_ok get dref get esl e 323 js_raw e403 324 ret 403 325 end 326 st_kill e 327 n = 1 328 if not and lg_idop OP_EDEL get ap A_TID lg_end 329 trap 9 330 end 331 end 332 end 333 st_commit 334 if gt n 0 335 c = lg_compact 336 if ne c 0 337 trap 9 338 end 339 end 340 js_raw r_del 341 js_u64 n 342 js_byte 125 343 ret 200 344 end 345 fn del_count_space d ulong to ulong 346 n = ulong 0 347 for e ulong get nent 0 348 if and eq get esl e d lt get eret e 2 349 n = add n 1 350 end 351 end 352 ret n 353 end 354 355 note ---- scoped tokens (extension): --tokens FILE, lines "TOKEN SPACE[,SPACE...]" ---- 356 note tks: token count; tko/tkl token bytes in tkb; tsa/tsn the token's first space and count in tsp 357 global tkb uchar `1048576` 358 global tks ulong 1 359 global tko ulong 4096 360 global tkl ulong 4096 361 global tsa ulong 4096 362 global tsn ulong 4096 363 global tsp ulong 65536 364 global tspn ulong 1 365 fn scope_check o ulong n ulong to ulong 366 for t ulong get tks 0 367 if auth_eq2 o n t 368 put ap A_SPSET t 369 ret 0 370 end 371 end 372 ret 401 373 end 374 fn auth_eq2 o ulong n ulong t ulong to bool 375 l = get tkl t 376 b = get tko t 377 d = ulong 0 378 if ne n l 379 d = 1 380 end 381 k = n 382 if gt k l 383 k = l 384 end 385 for i ulong k 386 x = ulong get jrs add o i 387 d = or d xor x ulong get tkb add b i 388 end 389 ret eq d 0 390 end 391 note true when the request may touch space slug r (NONE = not named: refused for scoped tokens) 392 fn scope_ok r ulong to bool 393 t = get ap A_SPSET 394 if eq t NONE 395 ret true 396 end 397 if eq r NONE 398 ret false 399 end 400 a = get tsa t 401 for i ulong get tsn t 402 if hs_eq get tsp add a i r 403 ret true 404 end 405 end 406 ret false 407 end