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-rs/blob/main/om/enc.om # cortex-rs Pure compiled native Rust memory engine with vector search and SQLite WAL 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-rs / om / enc.om` 482 lines ยท 8264 bytes Raw Blame History 1 note enc.om: client for the embedding encoder (embeddings.rs fetch_embedding). 2 note POST /embed with a 1.5 s budget; 768 numbers become the f32 vector qem. 3 note The encoder itself (ONNX bge-base-en-v1.5) is a recorded host dependency (DEPENDENCIES.md). 4 note ens: 0 configured (1 yes), 1 port, 2 path len; enh = IPv4 bytes; enp = path prefix 5 global ens ulong 4 6 global enh uchar 4 7 global enp uchar 1024 8 global enb uchar `1048576` 9 global enq uchar `4194304` 10 global enql ulong 1 11 global ena uchar 16 12 text en_k1 "{\"modelId\":\"BAAI/bge-base-en-v1.5\",\"modelRevision\":\"cortex-bge-base-en-v1.5-768-v1\",\"priority\":\"recall\",\"text\":" 13 text en_post "POST " 14 text en_embed "/embed HTTP/1.1" 15 text en_host "host: " 16 text en_ct "content-type: application/json" 17 text en_acc "accept: */*" 18 text en_cl "content-length: " 19 text en_conn "connection: close" 20 text en_http "http://" 21 text en_lh "localhost" 22 text en_key "embedding" 23 text t_sep ": " 24 25 note parse --encoder-url bytes in jrs[0..n): http://IPv4|localhost[:port][/path] 26 fn enc_config n ulong to void 27 put ens 0 0 28 if lt n len en_http 29 ret 30 end 31 for i ulong len en_http 32 if ne get jrs i get en_http i 33 ret 34 end 35 end 36 p = ulong len en_http 37 h = p 38 while and lt p n and ne get jrs p 58 ne get jrs p 47 bound 999999 39 p = add p 1 40 end 41 if not enc_host h sub p h 42 ret 43 end 44 port = ulong 80 45 if and lt p n eq get jrs p 58 46 p = add p 1 47 port = 0 48 while and lt p n ne get jrs p 47 bound 999999 49 c = get jrs p 50 if or lt c 48 gt c 57 51 ret 52 end 53 port = add mul port 10 ulong sub c 48 54 if gt port 65535 55 ret 56 end 57 p = add p 1 58 end 59 end 60 note path without trailing slashes (trim_end_matches("/")) 61 e = n 62 while and gt e p eq get jrs sub e 1 47 bound 999999 63 e = sub e 1 64 end 65 for i ulong sub e p 66 put enp i get jrs add p i 67 end 68 put ens 2 sub e p 69 put ens 1 port 70 put ens 0 1 71 end 72 fn enc_host h ulong n ulong to bool 73 if eq n len en_lh 74 same = true 75 for i ulong n 76 if ne get jrs add h i get en_lh i 77 same = false 78 end 79 end 80 if same 81 put enh 0 127 82 put enh 1 0 83 put enh 2 0 84 put enh 3 1 85 ret true 86 end 87 end 88 k = ulong 0 89 v = ulong 0 90 dg = ulong 0 91 for i ulong n 92 c = get jrs add h i 93 if eq c 46 94 if or eq dg 0 ge k 3 95 ret false 96 end 97 put enh k uchar v 98 k = add k 1 99 v = 0 100 dg = 0 101 else 102 if or lt c 48 gt c 57 103 ret false 104 end 105 v = add mul v 10 ulong sub c 48 106 dg = add dg 1 107 if gt v 255 108 ret false 109 end 110 end 111 end 112 if or ne k 3 eq dg 0 113 ret false 114 end 115 put enh 3 uchar v 116 ret true 117 end 118 119 note request body: the JSON payload with text = enq[0..enql) 120 fn enc_body to ulong 121 put jobl 0 0 122 js_raw en_k1 123 for i ulong get enql 0 124 put jes i get enq i 125 end 126 js_esc_buf 0 get enql 0 127 js_byte 125 128 ret get jobl 0 129 end 130 fn eb_txt o ulong t view uchar * to ulong 131 for i ulong len t 132 put enb add o i get t i 133 end 134 ret add o len t 135 end 136 fn eb_crlf o ulong to ulong 137 put enb o 13 138 put enb add o 1 10 139 ret add o 2 140 end 141 fn eb_dec o ulong v ulong to ulong 142 k = o 143 if ge v 10 144 k = eb_dec o div v 10 145 end 146 put enb k uchar add rem v 10 48 147 ret add k 1 148 end 149 note request head for a body of bl bytes; returns head length 150 fn enc_head bl ulong to ulong 151 o = eb_txt 0 en_post 152 for i ulong get ens 2 153 put enb add o i get enp i 154 end 155 o = add o get ens 2 156 o = eb_crlf eb_txt o en_embed 157 o = eb_txt o en_host 158 for k ulong 4 159 o = eb_dec o ulong get enh k 160 if lt k 3 161 put enb o 46 162 o = add o 1 163 end 164 end 165 put enb o 58 166 o = eb_crlf eb_dec add o 1 get ens 1 167 o = eb_crlf eb_txt o en_ct 168 o = eb_crlf eb_txt o en_acc 169 o = eb_crlf eb_dec eb_txt o en_cl bl 170 o = eb_crlf eb_txt o en_conn 171 ret eb_crlf o 172 end 173 174 note fetch the vector for enq; true and qem/qemf set when the encoder answered with 768 numbers 175 fn enc_fetch to bool 176 put qemf 0 0 177 if eq get ens 0 0 178 ret false 179 end 180 dl = add monons `1500000000` 181 fd = socknew 2 524289 0 182 if lt fd 0 183 ret false 184 end 185 ok = enc_talk int fd dl 186 x = fclose int fd 187 if or not ok ne x 0 188 ret false 189 end 190 ret enc_parse 191 end 192 note connect (nonblocking, bounded by dl), send head + body, read the whole answer into enb 193 fn enc_talk fd int dl long to bool 194 put ena 0 2 195 put ena 1 0 196 p = get ens 1 197 put ena 2 uchar shr p 8 198 put ena 3 uchar and p 255 199 for i ulong 4 200 put ena add 4 i get enh i 201 end 202 r = connect fd ena 0 16 203 if and ne r 0 ne r -115 204 ret false 205 end 206 w = waitio fd 4 sub dl monons 207 if le w 0 208 ret false 209 end 210 bl = enc_body 211 hl = enc_head bl 212 if not enc_send fd 0 hl dl 213 ret false 214 end 215 if not enc_sendj fd bl dl 216 ret false 217 end 218 ret enc_recv fd dl 219 end 220 fn enc_send fd int o ulong n ulong dl long to bool 221 k = o 222 e = add o n 223 while lt k e bound 999999 224 w = waitio fd 4 sub dl monons 225 if le w 0 226 ret false 227 end 228 s = send fd enb k sub e k 0 229 if le s 0 230 ret false 231 end 232 k = add k ulong s 233 end 234 ret true 235 end 236 fn enc_sendj fd int n ulong dl long to bool 237 k = ulong 0 238 while lt k n bound 999999 239 w = waitio fd 4 sub dl monons 240 if le w 0 241 ret false 242 end 243 s = send fd job k sub n k 0 244 if le s 0 245 ret false 246 end 247 k = add k ulong s 248 end 249 ret true 250 end 251 note read until the peer closes (we sent connection: close); ens 3 = bytes held 252 fn enc_recv fd int dl long to bool 253 n = ulong 0 254 while true bound 999999 255 now = monons 256 if ge now dl 257 ret false 258 end 259 w = waitio fd 1 sub dl now 260 if le w 0 261 ret false 262 end 263 if ge n len enb 264 ret false 265 end 266 r = recv fd enb n sub len enb n 0 267 if lt r 0 268 ret false 269 end 270 if eq r 0 271 put ens 3 n 272 ret true 273 end 274 n = add n ulong r 275 end 276 ret false 277 end 278 279 fn enc_crlf2 i ulong to bool 280 if or ne get enb i 13 ne get enb add i 1 10 281 ret false 282 end 283 ret and eq get enb add i 2 13 eq get enb add i 3 10 284 end 285 note status 2xx, then the body (content-length or chunked or to EOF) parsed as JSON 286 fn enc_parse to bool 287 n = get ens 3 288 if lt n 12 289 ret false 290 end 291 c = get enb 9 292 if ne c 50 293 ret false 294 end 295 he = ulong 0 296 i = ulong 0 297 while lt add i 3 n bound 999999 298 if enc_crlf2 i 299 he = add i 4 300 break 301 end 302 i = add i 1 303 end 304 if eq he 0 305 ret false 306 end 307 bl = enc_body_copy he n 308 if eq bl NONE 309 ret false 310 end 311 put jinl 0 bl 312 if not js_parse 313 ret false 314 end 315 ret enc_vec 316 end 317 note copy the body to jin; chunked bodies are de-chunked; returns its length or NONE 318 fn enc_body_copy he ulong n ulong to ulong 319 if enc_is_chunked he 320 ret enc_dechunk he n 321 end 322 k = sub n he 323 if gt k len jin 324 ret NONE 325 end 326 for i ulong k 327 put jin i get enb add he i 328 end 329 ret k 330 end 331 text en_te "transfer-encoding: chunked" 332 fn enc_is_chunked he ulong to bool 333 m = len en_te 334 i = ulong 0 335 while lt add i m he bound 999999 336 k = ulong 0 337 while lt k m bound 64 338 c = get enb add i k 339 if and ge c 65 le c 90 340 c = add c 32 341 end 342 if ne c get en_te k 343 break 344 end 345 k = add k 1 346 end 347 if eq k m 348 ret true 349 end 350 i = add i 1 351 end 352 ret false 353 end 354 fn enc_dechunk he ulong n ulong to ulong 355 p = he 356 o = ulong 0 357 while lt p n bound 999999 358 sz = ulong 0 359 dg = ulong 0 360 while lt p n bound 999999 361 v = http_hexv get enb p 362 if eq v 99 363 break 364 end 365 sz = add mul sz 16 v 366 dg = add dg 1 367 p = add p 1 368 end 369 if or eq dg 0 gt sz len jin 370 ret NONE 371 end 372 while and lt p n ne get enb p 10 bound 999999 373 p = add p 1 374 end 375 p = add p 1 376 if eq sz 0 377 ret o 378 end 379 if or gt add p sz n gt add o sz len jin 380 ret NONE 381 end 382 for i ulong sz 383 put jin add o i get enb add p i 384 end 385 o = add o sz 386 p = add add p sz 2 387 end 388 ret NONE 389 end 390 note val.get("embedding") as array; numbers (as_f64) kept, others skipped; exactly 768 needed 391 fn enc_vec to bool 392 r = get jroot 0 393 if ne js_kind r 8 394 ret false 395 end 396 a = js_find r en_key 397 if or eq a jnone ne js_kind a 7 398 ret false 399 end 400 k = ulong 0 401 for i ulong js_len a 402 x = js_child a i 403 t = js_kind x 404 if and ge t 3 le t 5 405 if ge k 768 406 ret false 407 end 408 put qem k f32 enc_num x t 409 k = add k 1 410 end 411 end 412 if ne k 768 413 ret false 414 end 415 put qemf 0 1 416 ret true 417 end 418 fn enc_num x ulong t uchar to f64 419 v = js_num x 420 if eq t 3 421 ret f64 v 422 end 423 if eq t 4 424 ret f64 bitcast long v 425 end 426 ret bitcast f64 v 427 end 428 429 note recall: the query text; write: "name: content" when content.trim() is not empty, else the name 430 fn enc_fetch_q to void 431 q = get ap A_Q 432 o = rf_off q 433 n = rf_len q 434 for i ulong n 435 put enq i hsg add o i 436 end 437 put enql 0 n 438 ok = enc_fetch 439 end 440 fn enc_fetch_w to void 441 c = get ap A_CONT 442 o = rf_off c 443 n = rf_len c 444 for i ulong n 445 put jrs i hc_get add o i 446 end 447 ru_trim 0 n 448 nm = get ap A_NAME 449 a = rf_off nm 450 m = rf_len nm 451 for i ulong m 452 put enq i hsg add a i 453 end 454 k = m 455 if gt get jtrl 0 0 456 for i ulong len t_sep 457 put enq add k i get t_sep i 458 end 459 k = add k len t_sep 460 for i ulong n 461 put enq add k i hc_get add o i 462 end 463 k = add k n 464 end 465 put enql 0 k 466 put ap A_EMB NONE 467 if enc_fetch 468 put ap A_EMB enc_stage 469 end 470 end 471 note stage qem as 3072 little-endian bytes in hc 472 fn enc_stage to ulong 473 o = get hct 0 474 for i ulong 768 475 v = ulong bitcast uint get qem i 476 for b ulong 4 477 hc_put add add o mul i 4 b uchar and shr v mul b 8 255 478 end 479 end 480 put hct 0 add o 3072 481 ret rf_hc o 3072 482 end