aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / om / serve / router.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note core/sovereign-core/om/serve/router.om |
| 3 | note Pure OM-2 conversion of aien-serve-http OpenAI-compatible router (/health, /v1/models, |
| 4 | note /v1/chat/completions) with strict JSON request validation and SSE/JSON response formatting. |
| 5 | note Compose after si/directives/om2.om and core/sovereign-core/om/serve/http_auth.om. |
| 6 | |
| 7 | const SRV_RESP_CAP ulong 2048 |
| 8 | const SRV_MAX_TOKENS_CAP ulong 4096 |
| 9 | |
| 10 | note Response buffer and metadata: [0]=resp_len, [1]=last_status, [2]=last_stream, [3]=last_max_tokens |
| 11 | global srv_resp_meta ulong 8 |
| 12 | global srv_resp_buf uchar 2048 |
| 13 | |
| 14 | text rt_health "/health" |
| 15 | text rt_models "/v1/models" |
| 16 | text rt_chat "/v1/chat/completions" |
| 17 | |
| 18 | text st_200 "HTTP/1.1 200 OK" |
| 19 | text st_400 "HTTP/1.1 400 Bad Request" |
| 20 | text st_401 "HTTP/1.1 401 Unauthorized" |
| 21 | text st_404 "HTTP/1.1 404 Not Found" |
| 22 | text st_405 "HTTP/1.1 405 Method Not Allowed" |
| 23 | text st_413 "HTTP/1.1 413 Payload Too Large" |
| 24 | text st_431 "HTTP/1.1 431 Request Header Fields Too Large" |
| 25 | text ct_json "Content-Type: application/json" |
| 26 | text ct_sse "Content-Type: text/event-stream" |
| 27 | text hdr_close "Connection: close" |
| 28 | |
| 29 | text body_health "{\"status\":\"ok\",\"engine\":\"omega-sovereign-core\"}\n" |
| 30 | text body_models "{\"object\":\"list\",\"data\":[{\"id\":\"qwen-native-om\",\"object\":\"model\"}]}\n" |
| 31 | text body_chat_ok "{\"id\":\"chatcmpl-om-1\",\"object\":\"chat.completion\",\"model\":\"qwen-native-om\",\"choices\":[{\"index\":0,\"message\":{\"role\":\"assistant\",\"content\":\"ok\"},\"finish_reason\":\"stop\"}]}\n" |
| 32 | text body_chat_sse "data: {\"id\":\"chatcmpl-om-1\",\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"ok\"}}]}\n\ndata: [DONE]\n\n" |
| 33 | |
| 34 | text err_bad_req "{\"error\":\"bad_request\"}\n" |
| 35 | text err_unauth "{\"error\":\"unauthorized\"}\n" |
| 36 | text err_not_found "{\"error\":\"not_found\"}\n" |
| 37 | text err_method "{\"error\":\"method_not_allowed\"}\n" |
| 38 | text err_too_large "{\"error\":\"payload_too_large\"}\n" |
| 39 | text err_hdr_large "{\"error\":\"headers_too_large\"}\n" |
| 40 | |
| 41 | text js_key_model "\"model\"" |
| 42 | text js_key_msgs "\"messages\"" |
| 43 | text js_key_maxt "\"max_tokens\"" |
| 44 | text js_key_stream "\"stream\"" |
| 45 | text js_lit_true "true" |
| 46 | text js_lit_false "false" |
| 47 | |
| 48 | fn srv_resp_reset to void |
| 49 | put srv_resp_meta 0 0 |
| 50 | put srv_resp_meta 1 0 |
| 51 | end |
| 52 | |
| 53 | fn srv_resp_len to ulong |
| 54 | ret get srv_resp_meta 0 |
| 55 | end |
| 56 | |
| 57 | fn srv_resp_status to ulong |
| 58 | ret get srv_resp_meta 1 |
| 59 | end |
| 60 | |
| 61 | fn srv_resp_append s view uchar * to void |
| 62 | n = len s |
| 63 | cur = get srv_resp_meta 0 |
| 64 | for i ulong n bound 512 |
| 65 | if ge add cur i SRV_RESP_CAP |
| 66 | break |
| 67 | end |
| 68 | put srv_resp_buf add cur i get s i |
| 69 | end |
| 70 | nlen = add cur n |
| 71 | if gt nlen SRV_RESP_CAP |
| 72 | nlen = SRV_RESP_CAP |
| 73 | end |
| 74 | put srv_resp_meta 0 nlen |
| 75 | end |
| 76 | |
| 77 | fn srv_resp_crlf to void |
| 78 | cur = get srv_resp_meta 0 |
| 79 | if lt add cur 1 SRV_RESP_CAP |
| 80 | put srv_resp_buf cur uchar 13 |
| 81 | put srv_resp_buf add cur 1 uchar 10 |
| 82 | put srv_resp_meta 0 add cur 2 |
| 83 | end |
| 84 | end |
| 85 | |
| 86 | fn srv_emit_resp status ulong st_line view uchar * is_sse bool body view uchar * to ulong |
| 87 | srv_resp_reset |
| 88 | srv_resp_append st_line |
| 89 | srv_resp_crlf |
| 90 | if is_sse |
| 91 | srv_resp_append ct_sse |
| 92 | else |
| 93 | srv_resp_append ct_json |
| 94 | end |
| 95 | srv_resp_crlf |
| 96 | srv_resp_append hdr_close |
| 97 | srv_resp_crlf |
| 98 | srv_resp_crlf |
| 99 | srv_resp_append body |
| 100 | put srv_resp_meta 1 status |
| 101 | ret status |
| 102 | end |
| 103 | |
| 104 | fn srv_path_eq lit view uchar * to bool |
| 105 | plen = get srv_req_meta 1 |
| 106 | if ne plen len lit |
| 107 | ret false |
| 108 | end |
| 109 | for i ulong plen bound 128 |
| 110 | if ne get srv_req_path i get lit i |
| 111 | ret false |
| 112 | end |
| 113 | end |
| 114 | ret true |
| 115 | end |
| 116 | |
| 117 | note Validate balanced JSON object braces/brackets/quotes in srv_req_body[0..blen). |
| 118 | fn srv_json_balanced blen ulong to bool |
| 119 | if lt blen 2 |
| 120 | ret false |
| 121 | end |
| 122 | s = ulong 0 |
| 123 | while lt s blen bound 2048 |
| 124 | b = get srv_req_body s |
| 125 | if or eq b uchar 32 or eq b uchar 9 or eq b uchar 10 eq b uchar 13 |
| 126 | s = add s 1 |
| 127 | else |
| 128 | break |
| 129 | end |
| 130 | end |
| 131 | if or ge s blen ne get srv_req_body s uchar 123 |
| 132 | ret false |
| 133 | end |
| 134 | brace = long 0 |
| 135 | brack = long 0 |
| 136 | in_str = false |
| 137 | esc = false |
| 138 | for i ulong sub blen s bound 2048 |
| 139 | b = get srv_req_body add s i |
| 140 | if eq b uchar 0 |
| 141 | ret false |
| 142 | end |
| 143 | if in_str |
| 144 | if esc |
| 145 | esc = false |
| 146 | else |
| 147 | if eq b uchar 92 |
| 148 | esc = true |
| 149 | else |
| 150 | if eq b uchar 34 |
| 151 | in_str = false |
| 152 | end |
| 153 | end |
| 154 | end |
| 155 | else |
| 156 | if eq b uchar 34 |
| 157 | in_str = true |
| 158 | else |
| 159 | if eq b uchar 123 |
| 160 | brace = add brace 1 |
| 161 | else |
| 162 | if eq b uchar 125 |
| 163 | brace = sub brace 1 |
| 164 | if lt brace 0 |
| 165 | ret false |
| 166 | end |
| 167 | else |
| 168 | if eq b uchar 91 |
| 169 | brack = add brack 1 |
| 170 | else |
| 171 | if eq b uchar 93 |
| 172 | brack = sub brack 1 |
| 173 | if lt brack 0 |
| 174 | ret false |
| 175 | end |
| 176 | end |
| 177 | end |
| 178 | end |
| 179 | end |
| 180 | end |
| 181 | end |
| 182 | end |
| 183 | if or in_str or ne brace 0 ne brack 0 |
| 184 | ret false |
| 185 | end |
| 186 | ret true |
| 187 | end |
| 188 | |
| 189 | note Find literal needle inside srv_req_body[0..blen); returns index after needle or 18446744073709551615. |
| 190 | fn srv_body_find_after blen ulong needle view uchar * to ulong |
| 191 | nlen = len needle |
| 192 | if or eq nlen 0 gt nlen blen |
| 193 | ret 18446744073709551615 |
| 194 | end |
| 195 | lim = add sub blen nlen 1 |
| 196 | for i ulong lim bound 2048 |
| 197 | ok = true |
| 198 | for j ulong nlen bound 64 |
| 199 | if ne get srv_req_body add i j get needle j |
| 200 | ok = false |
| 201 | break |
| 202 | end |
| 203 | end |
| 204 | if ok |
| 205 | ret add i nlen |
| 206 | end |
| 207 | end |
| 208 | ret 18446744073709551615 |
| 209 | end |
| 210 | |
| 211 | note Skip JSON whitespace + ':' + whitespace starting at pos; returns value start or 18446744073709551615. |
| 212 | fn srv_body_skip_colon blen ulong pos ulong to ulong |
| 213 | p = pos |
| 214 | while lt p blen bound 2048 |
| 215 | b = get srv_req_body p |
| 216 | if or eq b uchar 32 or eq b uchar 9 or eq b uchar 10 eq b uchar 13 |
| 217 | p = add p 1 |
| 218 | else |
| 219 | break |
| 220 | end |
| 221 | end |
| 222 | if or ge p blen ne get srv_req_body p uchar 58 |
| 223 | ret 18446744073709551615 |
| 224 | end |
| 225 | p = add p 1 |
| 226 | while lt p blen bound 2048 |
| 227 | b = get srv_req_body p |
| 228 | if or eq b uchar 32 or eq b uchar 9 or eq b uchar 10 eq b uchar 13 |
| 229 | p = add p 1 |
| 230 | else |
| 231 | break |
| 232 | end |
| 233 | end |
| 234 | if ge p blen |
| 235 | ret 18446744073709551615 |
| 236 | end |
| 237 | ret p |
| 238 | end |
| 239 | |
| 240 | fn srv_body_match_at blen ulong pos ulong lit view uchar * to bool |
| 241 | nlen = len lit |
| 242 | if gt add pos nlen blen |
| 243 | ret false |
| 244 | end |
| 245 | for i ulong nlen bound 32 |
| 246 | if ne get srv_req_body add pos i get lit i |
| 247 | ret false |
| 248 | end |
| 249 | end |
| 250 | ret true |
| 251 | end |
| 252 | |
| 253 | note Validate /v1/chat/completions JSON body: requires balanced {}, "model": "...", "messages": [...], |
| 254 | note optional "max_tokens" in 1..SRV_MAX_TOKENS_CAP, optional "stream": true|false. |
| 255 | fn srv_validate_chat_body to bool |
| 256 | blen = get srv_req_meta 6 |
| 257 | if not srv_json_balanced blen |
| 258 | ret false |
| 259 | end |
| 260 | mp = srv_body_find_after blen js_key_model |
| 261 | if eq mp 18446744073709551615 |
| 262 | ret false |
| 263 | end |
| 264 | mv = srv_body_skip_colon blen mp |
| 265 | if or eq mv 18446744073709551615 ne get srv_req_body mv uchar 34 |
| 266 | ret false |
| 267 | end |
| 268 | note Non-empty model string |
| 269 | if or ge add mv 1 blen eq get srv_req_body add mv 1 uchar 34 |
| 270 | ret false |
| 271 | end |
| 272 | msp = srv_body_find_after blen js_key_msgs |
| 273 | if eq msp 18446744073709551615 |
| 274 | ret false |
| 275 | end |
| 276 | msv = srv_body_skip_colon blen msp |
| 277 | if or eq msv 18446744073709551615 ne get srv_req_body msv uchar 91 |
| 278 | ret false |
| 279 | end |
| 280 | put srv_resp_meta 2 0 |
| 281 | put srv_resp_meta 3 256 |
| 282 | tp = srv_body_find_after blen js_key_maxt |
| 283 | if ne tp 18446744073709551615 |
| 284 | tv = srv_body_skip_colon blen tp |
| 285 | if eq tv 18446744073709551615 |
| 286 | ret false |
| 287 | end |
| 288 | acc = ulong 0 |
| 289 | digits = ulong 0 |
| 290 | p = tv |
| 291 | while lt p blen bound 16 |
| 292 | b = get srv_req_body p |
| 293 | if and ge b uchar 48 le b uchar 57 |
| 294 | acc = add mul acc 10 ulong sub b uchar 48 |
| 295 | digits = add digits 1 |
| 296 | p = add p 1 |
| 297 | else |
| 298 | break |
| 299 | end |
| 300 | end |
| 301 | if or eq digits 0 or eq acc 0 gt acc SRV_MAX_TOKENS_CAP |
| 302 | ret false |
| 303 | end |
| 304 | put srv_resp_meta 3 acc |
| 305 | end |
| 306 | sp = srv_body_find_after blen js_key_stream |
| 307 | if ne sp 18446744073709551615 |
| 308 | sv = srv_body_skip_colon blen sp |
| 309 | if eq sv 18446744073709551615 |
| 310 | ret false |
| 311 | end |
| 312 | if srv_body_match_at blen sv js_lit_true |
| 313 | put srv_resp_meta 2 1 |
| 314 | else |
| 315 | if not srv_body_match_at blen sv js_lit_false |
| 316 | ret false |
| 317 | end |
| 318 | end |
| 319 | end |
| 320 | ret true |
| 321 | end |
| 322 | |
| 323 | note Main HTTP request handler: parse -> route -> auth -> validate -> emit response. |
| 324 | fn srv_handle_http raw view uchar * to ulong |
| 325 | pst = srv_parse_http_req raw |
| 326 | if ne pst PARSE_OK |
| 327 | if eq pst PARSE_ERR_TOO_LARGE |
| 328 | ret srv_emit_resp 413 st_413 false err_too_large |
| 329 | end |
| 330 | if eq pst PARSE_ERR_HDR_TOO_LARGE |
| 331 | ret srv_emit_resp 431 st_431 false err_hdr_large |
| 332 | end |
| 333 | if eq pst PARSE_ERR_METHOD |
| 334 | ret srv_emit_resp 405 st_405 false err_method |
| 335 | end |
| 336 | ret srv_emit_resp 400 st_400 false err_bad_req |
| 337 | end |
| 338 | m = get srv_req_meta 0 |
| 339 | if srv_path_eq rt_health |
| 340 | if or eq m HTTP_M_GET eq m HTTP_M_HEAD |
| 341 | ret srv_emit_resp 200 st_200 false body_health |
| 342 | end |
| 343 | ret srv_emit_resp 405 st_405 false err_method |
| 344 | end |
| 345 | if srv_path_eq rt_models |
| 346 | if ne m HTTP_M_GET |
| 347 | ret srv_emit_resp 405 st_405 false err_method |
| 348 | end |
| 349 | if not srv_auth_verify_req |
| 350 | ret srv_emit_resp 401 st_401 false err_unauth |
| 351 | end |
| 352 | ret srv_emit_resp 200 st_200 false body_models |
| 353 | end |
| 354 | if srv_path_eq rt_chat |
| 355 | if ne m HTTP_M_POST |
| 356 | ret srv_emit_resp 405 st_405 false err_method |
| 357 | end |
| 358 | if not srv_auth_verify_req |
| 359 | ret srv_emit_resp 401 st_401 false err_unauth |
| 360 | end |
| 361 | if not srv_validate_chat_body |
| 362 | ret srv_emit_resp 400 st_400 false err_bad_req |
| 363 | end |
| 364 | if ne get srv_resp_meta 2 0 |
| 365 | ret srv_emit_resp 200 st_200 true body_chat_sse |
| 366 | end |
| 367 | ret srv_emit_resp 200 st_200 false body_chat_ok |
| 368 | end |
| 369 | ret srv_emit_resp 404 st_404 false err_not_found |
| 370 | end |