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/aien-train/blob/main/native/omegallm/serve.om
# aien-train
AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.
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
`aien-train / native / omegallm / serve.om`
2788 lines ยท 61344 bytes Raw Blame History
1 note SPDX-License-Identifier: AGPL-3.0-only
2 note omegallm serve.om (aien-train DRA-119): `ollm serve PORT CK MERGES [MAXCONN] [tplmax=N]
3 note [test=1]`, an OpenAI-compatible HTTP/1.1 server for a v1 BPE checkpoint (V > 256) on
4 note 127.0.0.1:PORT only. aarch64 only: build.sh composes it under one `om2` line after the
5 note omega-mv `e5b32567` units net/http.om, net/json.om and net/jsonw.om (vendored unmodified); the
6 note other targets take noserve.om. It prints `PORT n`, then serves one request at a time, one
7 note request per connection (Connection: close), until MAXCONN connections (default `1000000`) have
8 note been served. A connection is served once its first bytes arrive; a silent one waits in one
9 note of 8 pending slots and is closed unanswered 10 s after its accept (svwait). tplmax=N
10 note (256 .. 983040, default 65536) caps the chat template in bytes; test=1 enables the test-only
11 note members ollm_test_output and ollm_test_delay_ms (refused otherwise).
12 note Routes: GET /health {"status":"ok","in_flight":0,"busy":false}; GET /v1/models;
13 note POST /v1/chat/completions with JSON or SSE (stream true) responses; other paths 404, other
14 note methods 405. in_flight counts the generating chat completions (0 or 1, one slot); busy is
15 note in_flight 1. While a completion generates, the listener and pending connections are served
16 note between tokens: GET /health answers {"status":"ok","in_flight":1,"busy":true}, GET
17 note /v1/models the list, and every other request 503 server_busy with Retry-After: 1. A client
18 note that leaves (svgone) stops its generation: nothing more is sent, logged as status 499.
19 note Request: messages (non-empty array of objects with a string role of at most 32 bytes and a
20 note content that is a string, null, or an array of {"type":"text","text":string} parts joined
21 note with no separator; other part types are 400; content may be absent beside tool_calls),
22 note optional max_tokens or max_completion_tokens (1 .. 999999), temperature (0 .. 2 without
23 note exponent, used to 0.001; default 1), seed (unsigned, default 1), top_k (integer -1 ..
24 note 999999; -1, 0 and K >= V are off), top_p (0 <= p <= 1 without exponent, used to 0.000001;
25 note 0 keeps only the most probable token, 1 is off), stop (null, a string or an array of at most
26 note 8 strings of 1 .. 128 bytes), tools (array of at most 128
27 note {"type":"function","function":{"name":1 .. 64 bytes, ...}}), tool_choice ("none", "auto",
28 note "required" or a {"type":"function"} object), stream,
29 note stream_options.include_usage and n (only 1). Other members (model, ...) are ignored.
30 note Interim chat template (until DRA-123 fixes one shared template): when tools is non-empty,
31 note `tools: ` and the request's tools JSON text as sent, then `\n`; then `role: content\n` for
32 note each message in order, an assistant's tool_calls appended to its content as
33 note `{"name":NAME,"arguments":ARGS}` (NAME the JSON string as sent, ARGS
34 note the decoded string or the object's JSON text as sent, {} when absent); then `assistant: `.
35 note At most tplmax bytes, else 400. The context is token 0 then the template's encoding (CT0
36 note tokens, reported as prompt_tokens); CT0 >= T is refused with 400 context_length_exceeded
37 note stating CT0 and T. Output runs at most min(max_tokens, T - CT0) tokens, so the T-token window
38 note never slides (cmdsamp's step osstep, splitmix64 seed stream; with top_k or top_p on, the
39 note kept tokens are the most probable in order, ties to the lower id, up to K and until their
40 note mass reaches top_p, drawn by the same uniform scaled to the kept mass); a generated token 0
41 note ends it with finish_reason stop, the limit with length, the earliest stop string with stop
42 note (the content cut before it; a streamed chunk never sends a byte a stop could still claim).
43 note Tool calls (tools non-empty, tool_choice not "none"): when the content from the first
44 note `` on is 1 .. 8 blocks `{"name":N,"arguments":A}` with only
45 note whitespace between and after, N a declared tool name and A an object, a string or absent,
46 note the answer carries tool_calls (id call__, arguments the object's JSON text or the
47 note string) with finish_reason tool_calls and content the text before, trailing whitespace
48 note dropped, null when empty; otherwise the text is plain content. Streaming holds back a
49 note possible marker and trailing whitespace, then sends the tool_calls in one delta chunk.
50 note Decoded bytes are sent as UTF-8: a sequence cut by a token boundary waits for the next token,
51 note an ill-formed one (or one cut by the end) becomes U+FFFD. One line per routed response (and
52 note per 413 at the head, and per request answered during a generation) goes to stdout: `C status
53 note prompt_tokens completion_tokens finish(0 none, 1 stop, 2 length, 3 tool_calls, 4 client left)
54 note ms`. A body over 65532 JSON tokens is 413 request_too_large.
55 note Exit codes: 2 usage; 27 merges (bpe.om); 71 the port cannot be bound.
56 note wb layout: raw token bytes [0, 8192), content [8192, 73728), stop strings [73728, 74752) (8 x
57 note 128), markers [74752, 74784), message [81920, 82176), response id [82176, 82240), call id
58 note [82240, 82432), tool names [82432, 90624) (128 x 64), template [90624, `1073664`). bd holds the
59 note request, then the tool calls' names and arguments [0, 131072). sv slots: 0 stream, 1 max
60 note tokens, 2 TEMPMILLI, 3 seed state, 4 include_usage, 5 template bytes, 6 CT0, 7 generated, 8
61 note finish, 9 request count, 10 created, 11 start ns, 12 raw bytes, 13 content bytes, 14 content
62 note sent, 15 id length, 16 error code, 17 chunk end, 19 T, 21 last status, 22 test mode, 23
63 note tplmax, 24 forced tokens, 25 forced on, 26 test output bytes, 27 delay ms, 28 top_k, 29 top_p
64 note millionths, 30 tools, 31 tool detection, 32 tool calls, 33 marker position, 34 bd fill,
65 note 35 stop strings, 36 listen fd.
66 note Model id (svmid): read from the loaded checkpoint's header, so it names this model and this
67 note checkpoint: `ollm-ck0M-vV-tT-dD-hH-lL-stepS-fnvX`, M the checkpoint magic (1 OLLMCK01,
68 note 2 OLLMCK02), V T D H L its shapes, S its step field and X the FNV-1a 64 of its payload as the
69 note header records it (ockload has already refused a payload that does not hash to it, exit 14).
70 note The same id is `/v1/models` data[0].id (also while a completion generates) and the `model`
71 note of every completion and chunk; it is kept in the global svmi, its length in svml, and
72 note printed as `MODEL id` before `PORT n`.
73 note
74 note Mutants (tests/omegallm/build.sh --mutant NAME sets one constant to 0; tests/omegallm/
75 note serve-mutants.sh requires the serve gate to fail on each): OMUTMETH answers a known path's
76 note other methods instead of 405, OMUTPATH answers other paths as /health instead of 404, OMUTBIG
77 note drops the 413 (no head bound, an over-long body is a plain 400), OMUTJSON serves malformed JSON
78 note instead of 400, OMUTLOOP binds 0.0.0.0 instead of 127.0.0.1, OMUTSEED shifts the request seed
79 note by one (parity with `sample`), OMUTMID reports the old fixed id `ollm`.
80 const OMUTMETH ulong 1
81 const OMUTPATH ulong 1
82 const OMUTBIG ulong 1
83 const OMUTJSON ulong 1
84 const OMUTLOOP ulong 1
85 const OMUTSEED ulong 1
86 const OMUTMID ulong 1
87
88 text svtjson "application/json"
89 text svtctyp "Content-Type"
90 text svphlth "/health"
91 text svpmods "/v1/models"
92 text svpchat "/v1/chat/completions"
93 text svpcomp "/v1/completions"
94 text skprompt "prompt"
95 text svtcomp "text_completion"
96 text svcidp "cmpl-ollm-"
97 text svmget "GET"
98 text svmpost "POST"
99 text svthlth "{\"status\":\"ok\",\"in_flight\":0,\"busy\":false}"
100 text svthbsy "{\"status\":\"ok\",\"in_flight\":1,\"busy\":true}"
101 text svtlist "list"
102 text skdata "data"
103 text skowned "owned_by"
104 text svaien "aien"
105 text svt503 "{\"error\":{\"message\":\"a chat completion is in flight and this server runs one at a time: retry after 1 s\",\"type\":\"server_error\",\"param\":null,\"code\":\"server_busy\"}}"
106 text svtra "Retry-After"
107 text svone "1"
108 text svport "PORT "
109 text svmodl "MODEL "
110 text svmid1 "ollm-ck0"
111 text svmidv "-v"
112 text svmidt "-t"
113 text svmidd "-d"
114 text svmidh "-h"
115 text svmidl "-l"
116 text svmids "-step"
117 text svmidf "-fnv"
118 text skmsgs "messages"
119 text skrole "role"
120 text skcont "content"
121 text skstrm "stream"
122 text skmaxt "max_tokens"
123 text skmaxc "max_completion_tokens"
124 text sktemp "temperature"
125 text skseed "seed"
126 text skopts "stream_options"
127 text skincl "include_usage"
128 text skn "n"
129 text skid "id"
130 text skobj "object"
131 text skcrt "created"
132 text skmodel "model"
133 text skchoi "choices"
134 text skidx "index"
135 text skmsg "message"
136 text skdelta "delta"
137 text skfin "finish_reason"
138 text skusage "usage"
139 text skpt "prompt_tokens"
140 text skct "completion_tokens"
141 text sktt "total_tokens"
142 text skerr "error"
143 text sktype "type"
144 text skcode "code"
145 text skparam "param"
146 text sktext "text"
147 text sktopk "top_k"
148 text sktopp "top_p"
149 text sktools "tools"
150 text sktchoi "tool_choice"
151 text sktcall "tool_calls"
152 text skfunc "function"
153 text sknam "name"
154 text skargs "arguments"
155 text sktout "ollm_test_output"
156 text sktdel "ollm_test_delay_ms"
157 text sknone "none"
158 text svauto "auto"
159 text svreq "required"
160 text svollm "ollm"
161 text svasst "assistant"
162 text svcc "chat.completion"
163 text svccc "chat.completion.chunk"
164 text svstop "stop"
165 text svlen "length"
166 text svinv "invalid_request_error"
167 text svsrv "server_error"
168 text svdone "[DONE]"
169 text svidp "chatcmpl-ollm-"
170 text svcallp "call_"
171 text svus "_"
172 text svsep ": "
173 text svnl "\n"
174 text svaprm "assistant: "
175 text svtlp "tools: "
176 text svtco ""
177 text svtcc ""
178 text svtcn "{\"name\":"
179 text svtca ",\"arguments\":"
180 text svtcz "}"
181 text svempt "{}"
182 text svoptt "tplmax="
183 text svopte "test=1"
184 text secjson "invalid_json"
185 text secreq "invalid_request"
186 text secctx "context_length_exceeded"
187 text secbig "request_too_large"
188 text secnf "not_found"
189 text secmeth "method_not_allowed"
190 text secint "internal_error"
191 text sem1 "request body is not valid JSON (RFC 8259)"
192 text sem2 "the request must be a JSON object"
193 text sem3 "messages must be a non-empty array of objects"
194 text sem4 "message content must be a string, null or an array of text parts (absent only beside tool_calls)"
195 text sem5 "message role must be a string of at most 32 bytes"
196 text sem6a "the chat template is over "
197 text sem6b " bytes (the serve option tplmax=N sets this cap)"
198 text sem7 "temperature must be a number from 0 to 2 without exponent"
199 text sem8 "max_tokens must be an integer from 1 to 999999"
200 text sem9 "seed must be an unsigned integer"
201 text sem10 "stream must be true or false"
202 text sem12 "only n = 1 is supported"
203 text sem13 "the request body is over 983040 bytes"
204 text sem14 "no such path; routes: GET /health, GET /v1/models, POST /v1/chat/completions, POST /v1/completions"
205 text sem15 "method not allowed on this path"
206 text sem16 "response construction failed"
207 text sem17 "message content parts must be {\"type\":\"text\",\"text\":string}; image_url, input_audio, file and other part types are not supported"
208 text sem18 "stop must be null, a string or an array of at most 8 strings of 1 to 128 bytes"
209 text sem19 "top_k must be an integer from -1 to 999999 (-1 and 0 turn it off)"
210 text sem20 "top_p must be a number from 0 to 1 without exponent (resolution 0.000001; 0 keeps only the most probable token)"
211 text sem21 "tools must be an array of at most 128 {\"type\":\"function\",\"function\":{\"name\":string of 1 to 64 bytes}} objects"
212 text sem22 "tool_choice must be \"none\", \"auto\", \"required\" or a {\"type\":\"function\"} object"
213 text sem23 "tool_calls must be an array of {\"function\":{\"name\":string,\"arguments\":string or object}} objects"
214 text sem24 "ollm_test_output (a string of at most 16384 bytes) and ollm_test_delay_ms (0 to 10000) need a server started with test=1"
215 text sem25 "the request body has over 65532 JSON tokens (values, keys and closers), the JSON parser's limit"
216 text sem26 "prompt must be a non-empty string"
217 text sem27 "stream is not supported on /v1/completions: send stream false or leave it out"
218 text sem28 "messages, tools and tool_choice belong to /v1/chat/completions; /v1/completions takes prompt"
219 text sem11a "prompt is "
220 text sem11b " tokens including the start token; the context window is "
221 text sem11c " tokens, so no room is left for output"
222
223 note forced tokens (test=1), top_k/top_p marks, stop lengths, tool name lengths, tool call spans
224 global svft ulong 4096
225 global svmk uchar 4096
226 global svstl ulong 8
227 global svtnl ulong 128
228 global svtcx ulong 32
229 note pending connections (svwait): descriptor + 1 (0 free), accept time, record slot; pollfd records;
230 note the svgone peek byte
231 global svpfd ulong 8
232 global svpat ulong 8
233 global svpix ulong 8
234 global svpl uchar 72
235 global svpk uchar 1
236 global svmi uchar 192
237 global svml ulong 1
238
239 note ======== small byte helpers over wb
240
241 note svmcat: copy text t into wb at p, at most up to e; the next p
242 fn svmcat wb edit uchar `1073664` p ulong e ulong t view uchar * to ulong
243 q = p
244 for i ulong len t
245 if ge q e
246 ret q
247 end
248 put wb q get t i
249 q = add q 1
250 end
251 ret q
252 end
253
254 note svmdec: v in decimal into wb at p (at most 20 digits, up to e); the next p
255 fn svmdec wb edit uchar `1073664` p ulong e ulong v ulong to ulong
256 d = new uchar 20 0
257 i = ulong 20
258 x = v
259 for it ulong 20
260 i = sub i 1
261 put d i uchar add rem x 10 48
262 x = div x 10
263 if eq x 0
264 break
265 end
266 end
267 q = p
268 for k ulong sub 20 i
269 if ge q e
270 ret q
271 end
272 put wb q get d add i k
273 q = add q 1
274 end
275 ret q
276 end
277
278 note svat: whether the content at p starts with the pattern wb[po, po + pl) (p + pl <= content)
279 fn svat wb edit uchar `1073664` p ulong po ulong pl ulong to bool
280 for i ulong pl
281 if ne get wb add 8192 add p i get wb add po i
282 ret false
283 end
284 end
285 ret true
286 end
287
288 note svfind: the first position >= s where the pattern wb[po, po + pl) lies wholly in the content
289 note [0, n), or jsnone
290 fn svfind wb edit uchar `1073664` s ulong n ulong po ulong pl ulong to ulong
291 p = s
292 while le add p pl n bound 65537
293 if svat wb p po pl
294 ret p
295 end
296 p = add p 1
297 end
298 ret jsnone
299 end
300
301 note svpsuf: the longest k < pl, k <= n - lo, with content [n - k, n) a prefix of the pattern
302 fn svpsuf wb edit uchar `1073664` lo ulong n ulong po ulong pl ulong to ulong
303 k = sub pl 1
304 if lt sub n lo k
305 k = sub n lo
306 end
307 m = k
308 for it ulong m
309 if svat wb sub n k po k
310 ret k
311 end
312 k = sub k 1
313 end
314 ret 0
315 end
316
317 fn svisws c ulong to bool
318 ret or or eq c 32 eq c 9 or eq c 10 eq c 13
319 end
320
321 note svtrim: h moved back over trailing whitespace of the content, not below lo
322 fn svtrim wb edit uchar `1073664` lo ulong h ulong to ulong
323 if le h lo
324 ret h
325 end
326 q = h
327 for it ulong sub h lo
328 if not svisws ulong get wb add 8191 q
329 break
330 end
331 q = sub q 1
332 end
333 ret q
334 end
335
336 note svskws: p moved over whitespace of the content, not past n
337 fn svskws wb edit uchar `1073664` p ulong n ulong to ulong
338 q = p
339 for it ulong sub n p
340 if not svisws ulong get wb add 8192 q
341 break
342 end
343 q = add q 1
344 end
345 ret q
346 end
347
348 note ======== UTF-8 (RFC 3629 section 4)
349
350 note svu8n: the sequence at a[p], with a[0 .. n) the bytes so far: 1 .. 4 its length when well
351 note formed; 0 when a[p .. n) is a proper prefix of a well-formed sequence; 8 + k when its maximal
352 note ill-formed subpart is k bytes (replaced by one U+FFFD)
353 fn svu8n a view uchar * p ulong n ulong to ulong
354 c = ulong get a p
355 if lt c 128
356 ret 1
357 end
358 k = svu8k c
359 if eq k 0
360 ret 9
361 end
362 for i ulong sub k 1
363 q = add p add i 1
364 if ge q n
365 ret 0
366 end
367 if not svu8ok c i ulong get a q
368 ret add 9 i
369 end
370 end
371 ret k
372 end
373
374 note svu8k: the sequence length a lead byte c >= 128 starts, 0 when it starts none
375 fn svu8k c ulong to ulong
376 if and ge c 194 le c 223
377 ret 2
378 end
379 if and ge c 224 le c 239
380 ret 3
381 end
382 if and ge c 240 le c 244
383 ret 4
384 end
385 ret 0
386 end
387
388 note svu8ok: whether b may be continuation byte i (0 the first) after lead byte c (RFC 3629 table)
389 fn svu8ok c ulong i ulong b ulong to bool
390 lo = ulong 128
391 hi = ulong 191
392 if eq i 0
393 if eq c 224
394 lo = 160
395 end
396 if eq c 237
397 hi = 159
398 end
399 if eq c 240
400 lo = 144
401 end
402 if eq c 244
403 hi = 143
404 end
405 end
406 ret and ge b lo le b hi
407 end
408
409 note svrepl: U+FFFD (EF BF BD) into the content at its end
410 fn svrepl wb edit uchar `1073664` sv edit ulong 64 to void
411 g = add 8192 get sv 13
412 put wb g 239
413 put wb add g 1 191
414 put wb add g 2 189
415 put sv 13 add get sv 13 3
416 end
417
418 note svclean: move the complete sequences of the raw bytes wb[0, sv12) to the content; fin 1 also
419 note replaces a cut tail. The rest moves to the front of the raw bytes.
420 fn svclean wb edit uchar `1073664` sv edit ulong 64 fin ulong to void
421 rn = get sv 12
422 p = ulong 0
423 for it ulong add rn 1
424 if ge p rn
425 break
426 end
427 r = svu8n wb p rn
428 if eq r 0
429 if eq fin 0
430 break
431 end
432 svrepl wb sv
433 p = rn
434 else
435 if gt r 8
436 svrepl wb sv
437 p = add p sub r 8
438 else
439 svmove wb add 8192 get sv 13 p r
440 put sv 13 add get sv 13 r
441 p = add p r
442 end
443 end
444 end
445 svmove wb 0 p sub rn p
446 put sv 12 sub rn p
447 end
448
449 note svmove: wb[d, d + n) = wb[s, s + n), copying forward (d <= s or the ranges apart)
450 fn svmove wb edit uchar `1073664` d ulong s ulong n ulong to void
451 for i ulong n
452 put wb add d i get wb add s i
453 end
454 end
455
456 note ======== responses
457
458 fn svstat cs edit ulong 320 io edit uchar 131072 code ulong t view uchar * to long
459 r = hrspst cs io code
460 r = hfe r hhname cs io svtctyp
461 r = hfe r hhval cs io svtjson
462 r = hfe r hhend cs io 0 len t
463 r = hfe r hput cs io t
464 ret hfe r hwfin cs io
465 end
466
467 note svs503: the fixed server_busy error with Retry-After: 1
468 fn svs503 cs edit ulong 320 io edit uchar 131072 to long
469 r = hrspst cs io 503
470 r = hfe r hhname cs io svtctyp
471 r = hfe r hhval cs io svtjson
472 r = hfe r hhname cs io svtra
473 r = hfe r hhval cs io svone
474 r = hfe r hhend cs io 0 len svt503
475 r = hfe r hput cs io svt503
476 ret hfe r hwfin cs io
477 end
478
479 note svsend: the writer's text ob[1040, 1040 + n) as a JSON response with status code
480 fn svsend cs edit ulong 320 io edit uchar 131072 ob edit uchar 409600 code ulong n ulong to long
481 r = hrspst cs io code
482 r = hfe r hhname cs io svtctyp
483 r = hfe r hhval cs io svtjson
484 r = hfe r hhend cs io 0 n
485 r = hfe r hput cs io view part ob 1040 n
486 ret hfe r hwfin cs io
487 end
488
489 fn svwt ob edit uchar 409600 t view uchar * to void
490 js_wsbuf ob t 0 len t
491 end
492
493 note svemsg: the message of error code sv16 into wb[81920 ..); its length
494 fn svemsg wb edit uchar `1073664` sv edit ulong 64 to ulong
495 e = get sv 16
496 b = ulong 81920
497 z = ulong 82176
498 p = b
499 if eq e 1
500 p = svmcat wb p z sem1
501 end
502 if eq e 2
503 p = svmcat wb p z sem2
504 end
505 if eq e 3
506 p = svmcat wb p z sem3
507 end
508 if eq e 4
509 p = svmcat wb p z sem4
510 end
511 if eq e 5
512 p = svmcat wb p z sem5
513 end
514 if eq e 6
515 p = svmcat wb p z sem6a
516 p = svmdec wb p z get sv 23
517 p = svmcat wb p z sem6b
518 end
519 if eq e 7
520 p = svmcat wb p z sem7
521 end
522 if eq e 8
523 p = svmcat wb p z sem8
524 end
525 if eq e 9
526 p = svmcat wb p z sem9
527 end
528 if eq e 10
529 p = svmcat wb p z sem10
530 end
531 if eq e 11
532 p = svmcat wb p z sem11a
533 p = svmdec wb p z get sv 6
534 p = svmcat wb p z sem11b
535 p = svmdec wb p z get sv 19
536 p = svmcat wb p z sem11c
537 end
538 if eq e 12
539 p = svmcat wb p z sem12
540 end
541 if eq e 13
542 p = svmcat wb p z sem13
543 end
544 if eq e 14
545 p = svmcat wb p z sem14
546 end
547 if eq e 15
548 p = svmcat wb p z sem15
549 end
550 if eq e 16
551 p = svmcat wb p z sem16
552 end
553 ret sub svemsg2 wb e p z b
554 end
555
556 note svemsg2: the messages of error codes 17 .. 25 at p; the end
557 fn svemsg2 wb edit uchar `1073664` e ulong p ulong z ulong to ulong
558 if eq e 17
559 ret svmcat wb p z sem17
560 end
561 if eq e 18
562 ret svmcat wb p z sem18
563 end
564 if eq e 19
565 ret svmcat wb p z sem19
566 end
567 if eq e 20
568 ret svmcat wb p z sem20
569 end
570 if eq e 21
571 ret svmcat wb p z sem21
572 end
573 if eq e 22
574 ret svmcat wb p z sem22
575 end
576 if eq e 23
577 ret svmcat wb p z sem23
578 end
579 if eq e 24
580 ret svmcat wb p z sem24
581 end
582 if eq e 25
583 ret svmcat wb p z sem25
584 end
585 if eq e 26
586 ret svmcat wb p z sem26
587 end
588 if eq e 27
589 ret svmcat wb p z sem27
590 end
591 if eq e 28
592 ret svmcat wb p z sem28
593 end
594 ret p
595 end
596
597 fn svecode ob edit uchar 409600 e ulong to void
598 if eq e 1
599 svwt ob secjson
600 ret
601 end
602 if eq e 11
603 svwt ob secctx
604 ret
605 end
606 if or eq e 13 eq e 25
607 svwt ob secbig
608 ret
609 end
610 if eq e 14
611 svwt ob secnf
612 ret
613 end
614 if eq e 15
615 svwt ob secmeth
616 ret
617 end
618 if eq e 16
619 svwt ob secint
620 ret
621 end
622 svwt ob secreq
623 end
624
625 note svfail: {"error":{"message","type","param":null,"code"}} for error code sv16 with status st
626 fn svfail cs edit ulong 320 io edit uchar 131072 wb edit uchar `1073664` sv edit ulong 64 st ulong to long
627 ob = new uchar 409600 0
628 put sv 21 st
629 m = svemsg wb sv
630 x = js_winit ob
631 js_wobj ob
632 svwt ob skerr
633 js_wobj ob
634 svwt ob skmsg
635 js_wsbuf ob wb 81920 m
636 svwt ob sktype
637 if eq st 500
638 svwt ob svsrv
639 else
640 svwt ob svinv
641 end
642 svwt ob skparam
643 js_wnull ob
644 svwt ob skcode
645 svecode ob get sv 16
646 js_wobje ob
647 js_wobje ob
648 n = js_wdone ob
649 if lt n 0
650 put cs HKEEP 0
651 ret hrsperr cs io 500
652 end
653 ret svsend cs io ob st ulong n
654 end
655
656 note svhead: {"id", "object" t, "created", "model" opened; the caller adds the rest
657 fn svhead ob edit uchar 409600 wb edit uchar `1073664` sv edit ulong 64 t view uchar * to void
658 x = js_winit ob
659 js_wobj ob
660 svwt ob skid
661 js_wsbuf ob wb 82176 get sv 15
662 svwt ob skobj
663 svwt ob t
664 svwt ob skcrt
665 js_wulong ob get sv 10
666 svwt ob skmodel
667 svmidw ob
668 end
669
670 note svmidw: the model id as a JSON string value (the checkpoint id; OMUTMID 0: the fixed `ollm`)
671 fn svmidw ob edit uchar 409600 to void
672 if eq OMUTMID 0
673 svwt ob svollm
674 else
675 js_wsbeg ob
676 for i ulong get svml 0
677 js_wsb ob get svmi i
678 end
679 js_wsend ob
680 end
681 end
682
683 note svicat: copy text t into svmi at p, at most up to e; the next p
684 fn svicat p ulong e ulong t view uchar * to ulong
685 q = p
686 for i ulong len t
687 if ge q e
688 ret q
689 end
690 put svmi q get t i
691 q = add q 1
692 end
693 ret q
694 end
695
696 note svidec: v in decimal into svmi at p (at most 20 digits, up to e); the next p
697 fn svidec p ulong e ulong v ulong to ulong
698 d = new uchar 20 0
699 i = ulong 20
700 x = v
701 for it ulong 20
702 i = sub i 1
703 put d i uchar add rem x 10 48
704 x = div x 10
705 if eq x 0
706 break
707 end
708 end
709 q = p
710 for it ulong sub 20 i
711 if ge q e
712 ret q
713 end
714 put svmi q get d add i it
715 q = add q 1
716 end
717 ret q
718 end
719
720 note svihex: v as 16 lowercase hex digits into svmi at p, at most up to e; the next p
721 fn svihex p ulong e ulong v ulong to ulong
722 q = p
723 for i ulong 16
724 if ge q e
725 ret q
726 end
727 d = and shr v mul sub 15 i 4 15
728 c = add d 48
729 if gt d 9
730 c = add d 87
731 end
732 put svmi q uchar c
733 q = add q 1
734 end
735 ret q
736 end
737
738 note svmid: the model id of the checkpoint loaded from argv[3] (ockload has run and checked it)
739 note into svmi, its length into svml, then `MODEL id` on stdout. The header is read again for
740 note its payload FNV-1a field, which ockload keeps only while it loads.
741 fn svmid to void
742 hl = ulong 112
743 if eq get og 46 2
744 hl = 120
745 end
746 hn = oprd 3 0 0 120
747 if lt hn hl
748 exit 10
749 end
750 e = ulong 192
751 p = svicat 0 e svmid1
752 p = svidec p e get og 46
753 p = svicat p e svmidv
754 p = svidec p e get og 0
755 p = svicat p e svmidt
756 p = svidec p e get og 1
757 p = svicat p e svmidd
758 p = svidec p e get og 2
759 p = svicat p e svmidh
760 p = svidec p e get og 3
761 p = svicat p e svmidl
762 p = svidec p e get og 5
763 p = svicat p e svmids
764 p = svidec p e get og 41
765 p = svicat p e svmidf
766 p = svihex p e opw8 sub hl 8
767 put svml 0 p
768 for i ulong len svmodl
769 oputc ulong get svmodl i
770 end
771 for i ulong p
772 oputc ulong get svmi i
773 end
774 oputc 10
775 end
776
777 note svmodw: the /v1/models answer, the one model this server answers with (not logged)
778 fn svmodw cs edit ulong 320 io edit uchar 131072 to long
779 ob = new uchar 409600 0
780 x = js_winit ob
781 js_wobj ob
782 svwt ob skobj
783 svwt ob svtlist
784 svwt ob skdata
785 js_warr ob
786 js_wobj ob
787 svwt ob skid
788 svmidw ob
789 svwt ob skobj
790 svwt ob skmodel
791 svwt ob skcrt
792 js_wulong ob 0
793 svwt ob skowned
794 svwt ob svaien
795 js_wobje ob
796 js_warre ob
797 js_wobje ob
798 m = js_wdone ob
799 if lt m 0
800 put cs HKEEP 0
801 ret hrsperr cs io 500
802 end
803 ret svsend cs io ob 200 ulong m
804 end
805
806 note svmods: GET /v1/models, logged
807 fn svmods cs edit ulong 320 io edit uchar 131072 sv edit ulong 64 to long
808 put sv 21 200
809 r = svmodw cs io
810 svlog sv
811 ret r
812 end
813
814 note svbig: the status for a body over the bound, 413 (OMUTBIG 0: 400, the 413 refusal removed)
815 fn svbig to ulong
816 if eq OMUTBIG 0
817 ret 400
818 end
819 ret 413
820 end
821
822 fn svfr ob edit uchar 409600 sv edit ulong 64 to void
823 f = get sv 8
824 if eq f 1
825 svwt ob svstop
826 ret
827 end
828 if eq f 2
829 svwt ob svlen
830 ret
831 end
832 if eq f 3
833 svwt ob sktcall
834 ret
835 end
836 js_wnull ob
837 end
838
839 fn svusage ob edit uchar 409600 sv edit ulong 64 to void
840 svwt ob skusage
841 js_wobj ob
842 svwt ob skpt
843 js_wulong ob get sv 6
844 svwt ob skct
845 js_wulong ob get sv 7
846 svwt ob sktt
847 js_wulong ob add get sv 6 get sv 7
848 js_wobje ob
849 end
850
851 note svwcalls: the tool_calls array (idx 1 adds each call's index, as a stream delta does)
852 fn svwcalls ob edit uchar 409600 wb edit uchar `1073664` sv edit ulong 64 bd edit uchar 983040 idx ulong to void
853 js_warr ob
854 for i ulong get sv 32
855 b = mul i 4
856 js_wobj ob
857 if eq idx 1
858 svwt ob skidx
859 js_wulong ob i
860 end
861 svwt ob skid
862 p = svmcat wb 82240 82432 svcallp
863 p = svmdec wb p 82432 get sv 9
864 p = svmcat wb p 82432 svus
865 p = svmdec wb p 82432 i
866 js_wsbuf ob wb 82240 sub p 82240
867 svwt ob sktype
868 svwt ob skfunc
869 svwt ob skfunc
870 js_wobj ob
871 svwt ob sknam
872 js_wsbuf ob bd get svtcx b get svtcx add b 1
873 svwt ob skargs
874 js_wsbuf ob bd get svtcx add b 2 get svtcx add b 3
875 js_wobje ob
876 js_wobje ob
877 end
878 js_warre ob
879 end
880
881 note svcfull: the text_completion object of /v1/completions
882 fn svcfull ob edit uchar 409600 wb edit uchar `1073664` sv edit ulong 64 to long
883 svhead ob wb sv svtcomp
884 svwt ob skchoi
885 js_warr ob
886 js_wobj ob
887 svwt ob skidx
888 js_wulong ob 0
889 svwt ob sktext
890 js_wsbuf ob wb 8192 get sv 13
891 svwt ob skfin
892 svfr ob sv
893 js_wobje ob
894 js_warre ob
895 svusage ob sv
896 js_wobje ob
897 ret js_wdone ob
898 end
899
900 note svfull: the chat.completion object (stream false), or svcfull on /v1/completions
901 fn svfull ob edit uchar 409600 wb edit uchar `1073664` sv edit ulong 64 bd edit uchar 983040 to long
902 if eq get sv 37 1
903 ret svcfull ob wb sv
904 end
905 svhead ob wb sv svcc
906 svwt ob skchoi
907 js_warr ob
908 js_wobj ob
909 svwt ob skidx
910 js_wulong ob 0
911 svwt ob skmsg
912 js_wobj ob
913 svwt ob skrole
914 svwt ob svasst
915 svwt ob skcont
916 if gt get sv 32 0
917 h = svtrim wb 0 get sv 33
918 if eq h 0
919 js_wnull ob
920 else
921 js_wsbuf ob wb 8192 h
922 end
923 svwt ob sktcall
924 svwcalls ob wb sv bd 0
925 else
926 js_wsbuf ob wb 8192 get sv 13
927 end
928 js_wobje ob
929 svwt ob skfin
930 svfr ob sv
931 js_wobje ob
932 js_warre ob
933 svusage ob sv
934 js_wobje ob
935 ret js_wdone ob
936 end
937
938 note svchunk: one chat.completion.chunk; kind 0 the role, 1 the content wb[8192 + sv14, 8192 + sv17),
939 note 2 the finish reason, 3 usage (empty choices). Sends it as one SSE event.
940 fn svchunk cs edit ulong 320 io edit uchar 131072 ob edit uchar 409600 wb edit uchar `1073664` sv edit ulong 64 kind ulong to long
941 svhead ob wb sv svccc
942 svwt ob skchoi
943 js_warr ob
944 if ne kind 3
945 js_wobj ob
946 svwt ob skidx
947 js_wulong ob 0
948 svwt ob skdelta
949 js_wobj ob
950 if eq kind 0
951 svwt ob skrole
952 svwt ob svasst
953 svwt ob skcont
954 note "" as zero bytes of a non-empty text: under omc `e5b32567` an empty text passed as a view has a
955 note wrong length (omega-mv#775, fixed after this pin), and js_wsbuf then refuses it (jswearg)
956 js_wsbuf ob svasst 0 0
957 end
958 if eq kind 1
959 svwt ob skcont
960 g0 = get sv 14
961 js_wsbuf ob wb add 8192 g0 sub get sv 17 g0
962 end
963 js_wobje ob
964 svwt ob skfin
965 if eq kind 2
966 svfr ob sv
967 else
968 js_wnull ob
969 end
970 js_wobje ob
971 end
972 js_warre ob
973 if eq kind 3
974 svusage ob sv
975 end
976 js_wobje ob
977 ret svsse cs io ob js_wdone ob
978 end
979
980 note svsse: the writer's text ob[1040, 1040 + n) as one SSE event; -5 when n < 0
981 fn svsse cs edit ulong 320 io edit uchar 131072 ob edit uchar 409600 n long to long
982 if lt n 0
983 ret -5
984 end
985 r = hssedat cs io view part ob 1040 ulong n
986 ret hfe r hsseend cs io
987 end
988
989 note svtchunk: the tool_calls delta chunk (stream true)
990 fn svtchunk cs edit ulong 320 io edit uchar 131072 ob edit uchar 409600 wb edit uchar `1073664` sv edit ulong 64 bd edit uchar 983040 to long
991 svhead ob wb sv svccc
992 svwt ob skchoi
993 js_warr ob
994 js_wobj ob
995 svwt ob skidx
996 js_wulong ob 0
997 svwt ob skdelta
998 js_wobj ob
999 svwt ob sktcall
1000 svwcalls ob wb sv bd 1
1001 js_wobje ob
1002 svwt ob skfin
1003 js_wnull ob
1004 js_wobje ob
1005 js_warre ob
1006 js_wobje ob
1007 ret svsse cs io ob js_wdone ob
1008 end
1009
1010 note ======== request parsing
1011
1012 note svtemp: the temperature token's lexeme at bd[o, o + n) as TEMPMILLI (digits, optional
1013 note fraction, used to 0.001), or 999999 when it is not a number from 0 to 2
1014 fn svtemp bd edit uchar 983040 o ulong n ulong to ulong
1015 ip = ulong 0
1016 fr = ulong 0
1017 fd = ulong 0
1018 dot = false
1019 for i ulong n
1020 c = ulong get bd add o i
1021 if eq c 46
1022 if dot
1023 ret 999999
1024 end
1025 dot = true
1026 else
1027 if or lt c 48 gt c 57
1028 ret 999999
1029 end
1030 if dot
1031 if lt fd 3
1032 fr = add mul fr 10 sub c 48
1033 fd = add fd 1
1034 end
1035 else
1036 if ge ip 100
1037 ret 999999
1038 end
1039 ip = add mul ip 10 sub c 48
1040 end
1041 end
1042 end
1043 for k ulong 3
1044 if lt fd 3
1045 fr = mul fr 10
1046 fd = add fd 1
1047 end
1048 end
1049 m = add mul ip 1000 fr
1050 if gt m 2000
1051 ret 999999
1052 end
1053 ret m
1054 end
1055
1056 note svtopp: the top_p token's lexeme at bd[o, o + n) in millionths (used to 0.000001), or jsnone
1057 note when it is not a number from 0 to 1
1058 fn svtopp bd edit uchar 983040 o ulong n ulong to ulong
1059 ip = ulong 0
1060 fr = ulong 0
1061 fd = ulong 0
1062 dot = false
1063 for i ulong n
1064 c = ulong get bd add o i
1065 if eq c 46
1066 if dot
1067 ret jsnone
1068 end
1069 dot = true
1070 else
1071 if or lt c 48 gt c 57
1072 ret jsnone
1073 end
1074 if dot
1075 if lt fd 6
1076 fr = add mul fr 10 sub c 48
1077 fd = add fd 1
1078 end
1079 else
1080 if ge ip 10
1081 ret jsnone
1082 end
1083 ip = add mul ip 10 sub c 48
1084 end
1085 end
1086 end
1087 for k ulong 6
1088 if lt fd 6
1089 fr = mul fr 10
1090 fd = add fd 1
1091 end
1092 end
1093 m = add mul ip `1000000` fr
1094 if gt m `1000000`
1095 ret jsnone
1096 end
1097 ret m
1098 end
1099
1100 note svmem: the value token of member key in object o, or jsnone (the first when repeated)
1101 fn svmem src view uchar * tp view uint 262144 o ulong key view uchar * to ulong
1102 if ne js_type tp o 1
1103 ret jsnone
1104 end
1105 k = js_first tp o
1106 for it ulong js_nkids tp o
1107 if eq k jsnone
1108 break
1109 end
1110 if js_streq src js_off tp k key
1111 ret add k 1
1112 end
1113 k = js_mnext tp k
1114 end
1115 ret jsnone
1116 end
1117
1118 note svpcat: append text t to the template; false when it would pass tplmax (sv23)
1119 fn svpcat wb edit uchar `1073664` sv edit ulong 64 t view uchar * to bool
1120 pl = get sv 5
1121 if gt add pl len t get sv 23
1122 ret false
1123 end
1124 for i ulong len t
1125 put wb add 90624 add pl i get t i
1126 end
1127 put sv 5 add pl len t
1128 ret true
1129 end
1130
1131 note svpstr: append the decoded string token t to the template
1132 fn svpstr bd edit uchar 983040 tp edit uint 262144 t ulong wb edit uchar `1073664` sv edit ulong 64 to bool
1133 pl = get sv 5
1134 cap = get sv 23
1135 if ge pl cap
1136 ret false
1137 end
1138 n = js_sget bd js_off tp t part wb add 90624 pl sub cap pl
1139 if lt n 0
1140 ret false
1141 end
1142 put sv 5 add pl ulong n
1143 ret true
1144 end
1145
1146 note svpraw: append token t's JSON text as sent (a container's whole span) to the template
1147 fn svpraw bd edit uchar 983040 tp edit uint 262144 t ulong wb edit uchar `1073664` sv edit ulong 64 to bool
1148 ret svpcopy bd js_off tp t js_span tp t wb sv
1149 end
1150
1151 note svpcopy: append bd[o, o + n) to the template; false when it would pass tplmax (sv23)
1152 fn svpcopy bd edit uchar 983040 o ulong n ulong wb edit uchar `1073664` sv edit ulong 64 to bool
1153 d = add 90624 get sv 5
1154 if gt add get sv 5 n get sv 23
1155 ret false
1156 end
1157 for i ulong n
1158 put wb add d i get bd add o i
1159 end
1160 put sv 5 add get sv 5 n
1161 ret true
1162 end
1163
1164 note svparts: a content array of text parts into the template, joined with no separator
1165 fn svparts bd edit uchar 983040 tp edit uint 262144 a ulong wb edit uchar `1073664` sv edit ulong 64 to ulong
1166 e = js_first tp a
1167 for it ulong js_nkids tp a
1168 if eq e jsnone
1169 break
1170 end
1171 ty = svmem bd tp e sktype
1172 tx = svmem bd tp e sktext
1173 if or eq ty jsnone eq tx jsnone
1174 ret 17
1175 end
1176 if or ne js_type tp ty 6 ne js_type tp tx 6
1177 ret 17
1178 end
1179 if not js_streq bd js_off tp ty sktext
1180 ret 17
1181 end
1182 if not svpstr bd tp tx wb sv
1183 ret 6
1184 end
1185 e = js_enext tp e
1186 end
1187 ret 0
1188 end
1189
1190 note svtc1r: one assistant tool call e as {"name":N,"arguments":A}
1191 fn svtc1r bd edit uchar 983040 tp edit uint 262144 e ulong wb edit uchar `1073664` sv edit ulong 64 to ulong
1192 f = svmem bd tp e skfunc
1193 if eq f jsnone
1194 ret 23
1195 end
1196 nm = svmem bd tp f sknam
1197 if eq nm jsnone
1198 ret 23
1199 end
1200 if ne js_type tp nm 6
1201 ret 23
1202 end
1203 ar = svmem bd tp f skargs
1204 aty = ulong 0
1205 if ne ar jsnone
1206 aty = js_type tp ar
1207 end
1208 if and and ne aty 0 ne aty 6 ne aty 1
1209 ret 23
1210 end
1211 if not svpcat wb sv svtcn
1212 ret 6
1213 end
1214 if not svpraw bd tp nm wb sv
1215 ret 6
1216 end
1217 if not svpcat wb sv svtca
1218 ret 6
1219 end
1220 ok = true
1221 if eq aty 6
1222 ok = svpstr bd tp ar wb sv
1223 end
1224 if eq aty 1
1225 ok = svpraw bd tp ar wb sv
1226 end
1227 if eq aty 0
1228 ok = svpcat wb sv svempt
1229 end
1230 if not ok
1231 ret 6
1232 end
1233 if not svpcat wb sv svtcz
1234 ret 6
1235 end
1236 ret 0
1237 end
1238
1239 note svtcs: an assistant message's tool_calls array a into the template
1240 fn svtcs bd edit uchar 983040 tp edit uint 262144 a ulong wb edit uchar `1073664` sv edit ulong 64 to ulong
1241 if ne js_type tp a 3
1242 ret 23
1243 end
1244 e = js_first tp a
1245 for it ulong js_nkids tp a
1246 if eq e jsnone
1247 break
1248 end
1249 if not svpcat wb sv svtco
1250 ret 6
1251 end
1252 r = svtc1r bd tp e wb sv
1253 if ne r 0
1254 ret r
1255 end
1256 e = js_enext tp e
1257 end
1258 ret 0
1259 end
1260
1261 note svmsg1: one message object e into the template; 0 or an error code
1262 fn svmsg1 bd edit uchar 983040 tp edit uint 262144 e ulong wb edit uchar `1073664` sv edit ulong 64 to ulong
1263 if ne js_type tp e 1
1264 ret 3
1265 end
1266 ro = svmem bd tp e skrole
1267 if eq ro jsnone
1268 ret 5
1269 end
1270 if or ne js_type tp ro 6 gt js_slen tp ro 32
1271 ret 5
1272 end
1273 co = svmem bd tp e skcont
1274 tc = svmem bd tp e sktcall
1275 if ne tc jsnone
1276 if eq js_type tp tc 10
1277 tc = jsnone
1278 end
1279 end
1280 ct = ulong 10
1281 if ne co jsnone
1282 ct = js_type tp co
1283 end
1284 if and eq co jsnone eq tc jsnone
1285 ret 4
1286 end
1287 if and and ne ct 6 ne ct 10 ne ct 3
1288 ret 4
1289 end
1290 if not svpstr bd tp ro wb sv
1291 ret 6
1292 end
1293 if not svpcat wb sv svsep
1294 ret 6
1295 end
1296 if eq ct 6
1297 if not svpstr bd tp co wb sv
1298 ret 6
1299 end
1300 end
1301 if eq ct 3
1302 r = svparts bd tp co wb sv
1303 if ne r 0
1304 ret r
1305 end
1306 end
1307 if ne tc jsnone
1308 r = svtcs bd tp tc wb sv
1309 if ne r 0
1310 ret r
1311 end
1312 end
1313 if not svpcat wb sv svnl
1314 ret 6
1315 end
1316 ret 0
1317 end
1318
1319 fn svmsgs bd edit uchar 983040 tp edit uint 262144 a ulong wb edit uchar `1073664` sv edit ulong 64 to ulong
1320 if ne js_type tp a 3
1321 ret 3
1322 end
1323 n = js_nkids tp a
1324 if eq n 0
1325 ret 3
1326 end
1327 e = js_first tp a
1328 for it ulong n
1329 if eq e jsnone
1330 break
1331 end
1332 r = svmsg1 bd tp e wb sv
1333 if ne r 0
1334 ret r
1335 end
1336 e = js_enext tp e
1337 end
1338 if not svpcat wb sv svaprm
1339 ret 6
1340 end
1341 ret 0
1342 end
1343
1344 note svtool1: one tools entry e; its name into wb[82432 + 64 i ..)
1345 fn svtool1 bd edit uchar 983040 tp edit uint 262144 e ulong wb edit uchar `1073664` sv edit ulong 64 to ulong
1346 ty = svmem bd tp e sktype
1347 f = svmem bd tp e skfunc
1348 if or eq ty jsnone eq f jsnone
1349 ret 21
1350 end
1351 if ne js_type tp ty 6
1352 ret 21
1353 end
1354 if not js_streq bd js_off tp ty skfunc
1355 ret 21
1356 end
1357 nm = svmem bd tp f sknam
1358 if eq nm jsnone
1359 ret 21
1360 end
1361 if ne js_type tp nm 6
1362 ret 21
1363 end
1364 ret svtname bd js_off tp nm wb sv
1365 end
1366
1367 note svtname: the tool name string at bd[o ..) into wb[82432 + 64 i ..), i = sv30
1368 fn svtname bd edit uchar 983040 o ulong wb edit uchar `1073664` sv edit ulong 64 to ulong
1369 i = get sv 30
1370 if ge i 128
1371 ret 21
1372 end
1373 m = js_sget bd o part wb add 82432 mul i 64 64
1374 if lt m 1
1375 ret 21
1376 end
1377 put svtnl i ulong m
1378 put sv 30 add i 1
1379 ret 0
1380 end
1381
1382 note svtools: the tools value a (jsnone when absent): checked, its names kept, its JSON text
1383 note rendered as the template's first line
1384 fn svtools bd edit uchar 983040 tp edit uint 262144 a ulong wb edit uchar `1073664` sv edit ulong 64 to ulong
1385 if eq a jsnone
1386 ret 0
1387 end
1388 ty = js_type tp a
1389 if eq ty 10
1390 ret 0
1391 end
1392 if ne ty 3
1393 ret 21
1394 end
1395 n = js_nkids tp a
1396 if gt n 128
1397 ret 21
1398 end
1399 if eq n 0
1400 ret 0
1401 end
1402 e = js_first tp a
1403 for it ulong n
1404 if eq e jsnone
1405 break
1406 end
1407 r = svtool1 bd tp e wb sv
1408 if ne r 0
1409 ret r
1410 end
1411 e = js_enext tp e
1412 end
1413 if not svpcat wb sv svtlp
1414 ret 6
1415 end
1416 if not svpraw bd tp a wb sv
1417 ret 6
1418 end
1419 if not svpcat wb sv svnl
1420 ret 6
1421 end
1422 ret 0
1423 end
1424
1425 note svopts: stream_options value v: include_usage true sets sv4
1426 fn svopts bd edit uchar 983040 tp edit uint 262144 v ulong sv edit ulong 64 to void
1427 if ne js_type tp v 1
1428 ret
1429 end
1430 k = js_first tp v
1431 for it ulong js_nkids tp v
1432 if eq k jsnone
1433 break
1434 end
1435 if js_streq bd js_off tp k skincl
1436 if eq js_type tp add k 1 8
1437 put sv 4 1
1438 end
1439 end
1440 k = js_mnext tp k
1441 end
1442 end
1443
1444 note svscal: one scalar member (key token k, value k + 1); 0 or an error code
1445 fn svscal bd edit uchar 983040 tp edit uint 262144 k ulong sv edit ulong 64 to ulong
1446 o = js_off tp k
1447 v = add k 1
1448 ty = js_type tp v
1449 vo = js_off tp v
1450 if or js_streq bd o skmaxt js_streq bd o skmaxc
1451 if eq ty 10
1452 ret 0
1453 end
1454 if or ne ty 7 not js_isu bd vo
1455 ret 8
1456 end
1457 x = js_tou bd vo
1458 if or eq x 0 gt x 999999
1459 ret 8
1460 end
1461 put sv 1 x
1462 ret 0
1463 end
1464 if js_streq bd o sktemp
1465 if eq ty 10
1466 ret 0
1467 end
1468 if ne ty 7
1469 ret 7
1470 end
1471 if ne and js_nflags tp v 5 0
1472 ret 7
1473 end
1474 m = svtemp bd vo js_span tp v
1475 if gt m 2000
1476 ret 7
1477 end
1478 put sv 2 m
1479 ret 0
1480 end
1481 if js_streq bd o skseed
1482 if eq ty 10
1483 ret 0
1484 end
1485 if or ne ty 7 not js_isu bd vo
1486 ret 9
1487 end
1488 put sv 3 add js_tou bd vo sub 1 OMUTSEED
1489 ret 0
1490 end
1491 if js_streq bd o skstrm
1492 if eq ty 8
1493 put sv 0 1
1494 ret 0
1495 end
1496 if or eq ty 9 eq ty 10
1497 ret 0
1498 end
1499 ret 10
1500 end
1501 if js_streq bd o skn
1502 if eq ty 10
1503 ret 0
1504 end
1505 if or ne ty 7 not js_isu bd vo
1506 ret 12
1507 end
1508 if ne js_tou bd vo 1
1509 ret 12
1510 end
1511 ret 0
1512 end
1513 if js_streq bd o skopts
1514 svopts bd tp v sv
1515 end
1516 ret 0
1517 end
1518
1519 note svstop1: one stop string token t into wb[73728 + 128 i ..)
1520 fn svstop1 bd edit uchar 983040 tp edit uint 262144 t ulong wb edit uchar `1073664` sv edit ulong 64 to ulong
1521 if ne js_type tp t 6
1522 ret 18
1523 end
1524 ret svstopn bd js_off tp t wb sv
1525 end
1526
1527 note svstopn: the stop string at bd[o ..) into wb[73728 + 128 i ..), i = sv35
1528 fn svstopn bd edit uchar 983040 o ulong wb edit uchar `1073664` sv edit ulong 64 to ulong
1529 i = get sv 35
1530 if ge i 8
1531 ret 18
1532 end
1533 m = js_sget bd o part wb add 73728 mul i 128 128
1534 if lt m 1
1535 ret 18
1536 end
1537 put svstl i ulong m
1538 put sv 35 add i 1
1539 ret 0
1540 end
1541
1542 note svstopp: the stop value v (null, a string or an array of strings)
1543 fn svstopp bd edit uchar 983040 tp edit uint 262144 v ulong wb edit uchar `1073664` sv edit ulong 64 to ulong
1544 ty = js_type tp v
1545 if eq ty 10
1546 ret 0
1547 end
1548 if eq ty 6
1549 ret svstop1 bd tp v wb sv
1550 end
1551 if ne ty 3
1552 ret 18
1553 end
1554 n = js_nkids tp v
1555 if gt n 8
1556 ret 18
1557 end
1558 e = js_first tp v
1559 for it ulong n
1560 if eq e jsnone
1561 break
1562 end
1563 r = svstop1 bd tp e wb sv
1564 if ne r 0
1565 ret r
1566 end
1567 e = js_enext tp e
1568 end
1569 ret 0
1570 end
1571
1572 note svtopk: the top_k value v; -1, 0 and null leave it off
1573 fn svtopk bd edit uchar 983040 tp edit uint 262144 v ulong sv edit ulong 64 to ulong
1574 ty = js_type tp v
1575 if eq ty 10
1576 ret 0
1577 end
1578 if ne ty 7
1579 ret 19
1580 end
1581 vo = js_off tp v
1582 if js_isu bd vo
1583 x = js_tou bd vo
1584 if gt x 999999
1585 ret 19
1586 end
1587 put sv 28 x
1588 ret 0
1589 end
1590 if js_isl bd vo
1591 if eq js_tol bd vo neg long 1
1592 ret 0
1593 end
1594 end
1595 ret 19
1596 end
1597
1598 note svtchoi: the tool_choice value v; "none" turns tool-call detection off
1599 fn svtchoi bd edit uchar 983040 tp edit uint 262144 v ulong sv edit ulong 64 to ulong
1600 ty = js_type tp v
1601 if eq ty 10
1602 ret 0
1603 end
1604 if eq ty 6
1605 o = js_off tp v
1606 if js_streq bd o sknone
1607 put sv 31 0
1608 ret 0
1609 end
1610 if or js_streq bd o svauto js_streq bd o svreq
1611 ret 0
1612 end
1613 ret 22
1614 end
1615 if eq ty 1
1616 t = svmem bd tp v sktype
1617 if ne t jsnone
1618 if eq js_type tp t 6
1619 if js_streq bd js_off tp t skfunc
1620 ret 0
1621 end
1622 end
1623 end
1624 end
1625 ret 22
1626 end
1627
1628 note svtest: a test-only member (key k), refused unless the server runs with test=1
1629 fn svtest bd edit uchar 983040 tp edit uint 262144 k ulong wb edit uchar `1073664` sv edit ulong 64 to ulong
1630 if eq get sv 22 0
1631 ret 24
1632 end
1633 v = add k 1
1634 ty = js_type tp v
1635 vo = js_off tp v
1636 if js_streq bd js_off tp k sktdel
1637 if ne ty 7
1638 ret 24
1639 end
1640 if not js_isu bd vo
1641 ret 24
1642 end
1643 x = js_tou bd vo
1644 if gt x 10000
1645 ret 24
1646 end
1647 put sv 27 x
1648 ret 0
1649 end
1650 if ne ty 6
1651 ret 24
1652 end
1653 m = js_sget bd vo part wb 8192 16384
1654 if lt m 0
1655 ret 24
1656 end
1657 put sv 25 1
1658 put sv 26 ulong m
1659 ret 0
1660 end
1661
1662 note svscal2: the members stop, top_k, top_p, tool_choice and the test-only ones; 0 or a code
1663 fn svscal2 bd edit uchar 983040 tp edit uint 262144 k ulong wb edit uchar `1073664` sv edit ulong 64 to ulong
1664 o = js_off tp k
1665 v = add k 1
1666 if js_streq bd o svstop
1667 ret svstopp bd tp v wb sv
1668 end
1669 if js_streq bd o sktopk
1670 ret svtopk bd tp v sv
1671 end
1672 if js_streq bd o sktopp
1673 ty = js_type tp v
1674 if eq ty 10
1675 ret 0
1676 end
1677 if ne ty 7
1678 ret 20
1679 end
1680 if ne and js_nflags tp v 5 0
1681 ret 20
1682 end
1683 m = svtopp bd js_off tp v js_span tp v
1684 if eq m jsnone
1685 ret 20
1686 end
1687 put sv 29 m
1688 ret 0
1689 end
1690 if js_streq bd o sktchoi
1691 ret svtchoi bd tp v sv
1692 end
1693 if or js_streq bd o sktout js_streq bd o sktdel
1694 ret svtest bd tp k wb sv
1695 end
1696 ret 0
1697 end
1698
1699 note svreset: a request's options to their defaults
1700 fn svreset sv edit ulong 64 to void
1701 put sv 0 0
1702 put sv 1 999999
1703 put sv 2 1000
1704 put sv 3 1
1705 put sv 4 0
1706 put sv 5 0
1707 for i ulong 12
1708 put sv add 24 i 0
1709 end
1710 put sv 29 `1000000`
1711 put sv 31 1
1712 end
1713
1714 note svparse: the request body bd[0, n) into sv and the template; 0 or an error code
1715 fn svparse bd edit uchar 983040 n ulong wb edit uchar `1073664` sv edit ulong 64 to ulong
1716 tp = new uint 262144 0
1717 x = js_init tp 983040 64
1718 r = js_parse bd tp n
1719 if lt r 0
1720 if eq r neg long jsetok
1721 ret 25
1722 end
1723 if eq OMUTJSON 0
1724 svreset sv
1725 put sv 1 1
1726 ret 0
1727 end
1728 ret 1
1729 end
1730 if ne js_type tp 0 1
1731 ret 2
1732 end
1733 svreset sv
1734 k = js_first tp 0
1735 for it ulong js_nkids tp 0
1736 if eq k jsnone
1737 break
1738 end
1739 e = svscal bd tp k sv
1740 if eq e 0
1741 e = svscal2 bd tp k wb sv
1742 end
1743 if ne e 0
1744 ret e
1745 end
1746 k = js_mnext tp k
1747 end
1748 if eq get sv 37 1
1749 ret svcprm bd tp wb sv
1750 end
1751 ma = svmem bd tp 0 skmsgs
1752 if eq ma jsnone
1753 ret 3
1754 end
1755 e = svtools bd tp svmem bd tp 0 sktools wb sv
1756 if ne e 0
1757 ret e
1758 end
1759 if eq get sv 30 0
1760 put sv 31 0
1761 end
1762 ret svmsgs bd tp ma wb sv
1763 end
1764
1765 note svcprm: a /v1/completions body after its scalars: prompt (a non-empty string) is the whole
1766 note template, with no role lines and no assistant prefix; stream true, messages, tools and
1767 note tool_choice are refused; 0 or an error code
1768 fn svcprm bd edit uchar 983040 tp edit uint 262144 wb edit uchar `1073664` sv edit ulong 64 to ulong
1769 if eq get sv 0 1
1770 ret 27
1771 end
1772 if ne svmem bd tp 0 skmsgs jsnone
1773 ret 28
1774 end
1775 if ne svmem bd tp 0 sktools jsnone
1776 ret 28
1777 end
1778 if ne svmem bd tp 0 sktchoi jsnone
1779 ret 28
1780 end
1781 pr = svmem bd tp 0 skprompt
1782 if eq pr jsnone
1783 ret 26
1784 end
1785 if or ne js_type tp pr 6 eq js_slen tp pr 0
1786 ret 26
1787 end
1788 put sv 30 0
1789 put sv 31 0
1790 if not svpstr bd tp pr wb sv
1791 ret 6
1792 end
1793 ret 0
1794 end
1795
1796 note ======== encoding
1797
1798 note svemit: token t, the m-th of an encoding: md 0 onto the context, md 1 into the forced tokens
1799 fn svemit md ulong m ulong t ulong to void
1800 if eq md 0
1801 if lt get og 9 get og 1
1802 ocpush t
1803 end
1804 ret
1805 end
1806 if lt m 4096
1807 put svft m t
1808 end
1809 end
1810
1811 note svenc1: the pre-token oarg[i, i + l) encoded and emitted after m tokens; the new count
1812 fn svenc1 i ulong l ulong md ulong m ulong to ulong
1813 for k ulong l
1814 put bqtm k ushort get oarg add i k
1815 end
1816 c = bpenct l
1817 for k ulong c
1818 svemit md add m k ulong get bqtm k
1819 end
1820 ret add m c
1821 end
1822
1823 note svenc: encode src[0, n) of any length as obpenc does, in windows of at most 4000 bytes copied
1824 note to oarg; a window ends at a pre-token boundary at least 64 bytes before its end (a pre-token
1825 note is at most 64 bytes, so the boundaries agree with one pass), the last one at n. Returns the
1826 note token count.
1827 fn svenc src view uchar * n ulong md ulong to ulong
1828 s = ulong 0
1829 m = ulong 0
1830 while lt s n bound 999999
1831 w = sub n s
1832 if gt w 4000
1833 w = 4000
1834 end
1835 for k ulong w
1836 put oarg k get src add s k
1837 end
1838 la = eq add s w n
1839 i = ulong 0
1840 while lt i w bound 4097
1841 if and not la gt add i 64 w
1842 break
1843 end
1844 l = bpclena i w
1845 m = svenc1 i l md m
1846 i = add i l
1847 end
1848 s = add s i
1849 end
1850 ret m
1851 end
1852
1853 note ======== generation
1854
1855 note svgen: sample up to min(sv1, T - CT0) tokens; streams chunks when sv0 is 1. 0, or negative
1856 note when a chunk could not be sent or the client left (svgone: finish 4, status 499). Between
1857 note tokens the listener and pending connections are served (svside), waiting up to
1858 note ollm_test_delay_ms.
1859 fn svgen cs edit ulong 320 io edit uchar 131072 ob edit uchar 409600 wb edit uchar `1073664` sv edit ulong 64 bd edit uchar 983040 to long
1860 lim = sub get sv 19 get sv 6
1861 if lt get sv 1 lim
1862 lim = get sv 1
1863 end
1864 put sv 7 0
1865 put sv 8 0
1866 put sv 12 0
1867 put sv 13 0
1868 put sv 14 0
1869 put sv 17 0
1870 tmo = long mul get sv 27 `1000000`
1871 for g ulong lim
1872 svside bd sv tmo
1873 if svgone cs
1874 put sv 8 4
1875 put sv 21 499
1876 ret -104
1877 end
1878 if not svtake wb sv
1879 break
1880 end
1881 r = svpend cs io ob wb sv 0
1882 if lt r 0
1883 ret r
1884 end
1885 end
1886 if eq get sv 8 0
1887 put sv 8 2
1888 end
1889 p = get sv 13
1890 svclean wb sv 1
1891 x = svstopc wb sv p
1892 svtcall wb sv bd
1893 r = svpend cs io ob wb sv 1
1894 if lt r 0
1895 ret r
1896 end
1897 if and eq get sv 0 1 gt get sv 32 0
1898 ret svtchunk cs io ob wb sv bd
1899 end
1900 ret r
1901 end
1902
1903 note svtake: one sampled token into the context and the content; false when it ends the output
1904 note (sv8: 1 token 0 or a stop string, 2 the content would pass its 65536 bytes)
1905 fn svtake wb edit uchar `1073664` sv edit ulong 64 to bool
1906 c = svdraw sv
1907 if eq c 0
1908 put sv 8 1
1909 ret false
1910 end
1911 if not svfits sv c
1912 put sv 8 2
1913 ret false
1914 end
1915 svcap wb sv c
1916 p = get sv 13
1917 svclean wb sv 0
1918 if svstopc wb sv p
1919 ret false
1920 end
1921 ret true
1922 end
1923
1924 note svstopc: the earliest stop string match that ends past the content's old length p cuts the
1925 note content there (finish stop); whether one did
1926 fn svstopc wb edit uchar `1073664` sv edit ulong 64 p ulong to bool
1927 n = get sv 13
1928 b = jsnone
1929 for i ulong get sv 35
1930 l = get svstl i
1931 s = ulong 0
1932 if gt add p 1 l
1933 s = sub add p 1 l
1934 end
1935 q = svfind wb s n add 73728 mul i 128 l
1936 if lt q b
1937 b = q
1938 end
1939 end
1940 if eq b jsnone
1941 ret false
1942 end
1943 put sv 13 b
1944 put sv 12 0
1945 put sv 8 1
1946 ret true
1947 end
1948
1949 note svhold: how far a streamed chunk may send: not into the tail a stop string or the tool-call
1950 note marker could still claim, nor (detection on) past the marker or into trailing whitespace
1951 fn svhold wb edit uchar `1073664` sv edit ulong 64 to ulong
1952 n = get sv 13
1953 lo = get sv 14
1954 h = n
1955 for i ulong get sv 35
1956 k = svpsuf wb lo n add 73728 mul i 128 get svstl i
1957 if lt sub n k h
1958 h = sub n k
1959 end
1960 end
1961 if eq get sv 31 1
1962 q = svfind wb lo n 74752 11
1963 if lt q h
1964 h = q
1965 end
1966 k2 = svpsuf wb lo n 74752 11
1967 if lt sub n k2 h
1968 h = sub n k2
1969 end
1970 h = svtrim wb lo h
1971 end
1972 ret h
1973 end
1974
1975 note svdraw: the next token (cmdsamp's step osstep, or svdrawf with top_k/top_p on, or the forced
1976 note tokens of ollm_test_output then 0), advancing the seed stream when sampling
1977 fn svdraw sv edit ulong 64 to ulong
1978 if eq get sv 25 1
1979 g = get sv 7
1980 if lt g get sv 24
1981 ret get svft g
1982 end
1983 ret 0
1984 end
1985 st = get sv 3
1986 tm = get sv 2
1987 c = ulong 0
1988 if or eq tm 0 not svfon sv
1989 c = osstep st tm
1990 else
1991 c = svdrawf st div f64 tm 1000.0 get sv 28 get sv 29
1992 end
1993 if ne tm 0
1994 put sv 3 add st `11400714819323198485`
1995 end
1996 ret c
1997 end
1998
1999 note svfon: whether top_k (0 < K < V) or top_p (< 1) filters
2000 fn svfon sv edit ulong 64 to bool
2001 k = get sv 28
2002 if and gt k 0 lt k get og 0
2003 ret true
2004 end
2005 ret lt get sv 29 `1000000`
2006 end
2007
2008 note svdrawf: odraw over the kept tokens (svkeep), the uniform scaled to their mass
2009 fn svdrawf st ulong tau f64 k ulong p6 ulong to ulong
2010 svlogit tau
2011 o = get og 27
2012 v = get og 0
2013 cm = svkeep o v k p6
2014 u = mul f64 shr smx st 11 1.`1102230246251565e`-16
2015 r = svpick o v mul u cm
2016 for j ulong v
2017 put svmk j 0
2018 end
2019 ret r
2020 end
2021
2022 note svlogit: the next-token logits divided by tau, then made probabilities (as odraw does)
2023 fn svlogit tau f64 to void
2024 olastf
2025 o = get og 27
2026 v = get og 0
2027 m = bitcast f64 ulong `18442240474082181120`
2028 for c ulong v
2029 x = div get oa add o c tau
2030 put oa add o c x
2031 m = fmax m x
2032 end
2033 sm = osoft o v m
2034 end
2035
2036 note svkeep: mark the most probable tokens (ties to the lower id), at most K (0 < K < V), until
2037 note their mass reaches top_p (p6 < `1000000`); their mass
2038 fn svkeep o ulong v ulong k ulong p6 ulong to f64
2039 lim = v
2040 if and gt k 0 lt k v
2041 lim = k
2042 end
2043 pf = div f64 p6 `1000000`.0
2044 cm = 0.0f64
2045 for j ulong lim
2046 b = svmax o v
2047 put svmk b 1
2048 cm = add cm get oa add o b
2049 if and lt p6 `1000000` ge cm pf
2050 break
2051 end
2052 end
2053 ret cm
2054 end
2055
2056 note svmax: the unmarked token of highest probability, the lower id on ties
2057 fn svmax o ulong v ulong to ulong
2058 b = ulong 0
2059 bv = 0.0f64
2060 fd = false
2061 for c ulong v
2062 if eq get svmk c 0
2063 x = get oa add o c
2064 if or not fd gt x bv
2065 b = c
2066 bv = x
2067 fd = true
2068 end
2069 end
2070 end
2071 ret b
2072 end
2073
2074 note svpick: the first kept token (id order) whose running kept mass passes t; else the last kept
2075 fn svpick o ulong v ulong t f64 to ulong
2076 acc = 0.0f64
2077 la = ulong 0
2078 for c ulong v
2079 if eq get svmk c 1
2080 acc = add acc get oa add o c
2081 la = c
2082 if lt t acc
2083 ret c
2084 end
2085 end
2086 end
2087 ret la
2088 end
2089
2090 note svfits: whether token c's bytes fit the raw buffer (8192) and, as up to three content bytes
2091 note each, the content (65536)
2092 fn svfits sv edit ulong 64 c ulong to bool
2093 tl = obtlen c
2094 rn = add get sv 12 tl
2095 gn = add get sv 13 mul rn 3
2096 ret not or or gt rn 8192 gt tl 4096 gt gn 65536
2097 end
2098
2099 note svcap: token c's bytes onto the raw buffer, c onto the context
2100 fn svcap wb edit uchar `1073664` sv edit ulong 64 c ulong to void
2101 obtcap c
2102 rn = get sv 12
2103 for i ulong get obon 0
2104 put wb add rn i get obo i
2105 end
2106 put sv 12 add rn get obon 0
2107 ocpush c
2108 put sv 7 add get sv 7 1
2109 end
2110
2111 note svpend: when streaming, the content not yet sent (up to svhold; fin 1 all of it, or the text
2112 note before the tool calls) as one chunk; 0 or the send error
2113 fn svpend cs edit ulong 320 io edit uchar 131072 ob edit uchar 409600 wb edit uchar `1073664` sv edit ulong 64 fin ulong to long
2114 if ne get sv 0 1
2115 ret 0
2116 end
2117 h = get sv 13
2118 if eq fin 0
2119 h = svhold wb sv
2120 else
2121 if gt get sv 32 0
2122 h = svtrim wb get sv 14 get sv 33
2123 end
2124 end
2125 if le h get sv 14
2126 ret 0
2127 end
2128 put sv 17 h
2129 r = svchunk cs io ob wb sv 1
2130 put sv 14 h
2131 ret r
2132 end
2133
2134 note ======== tool calls in the output
2135
2136 note svtdecl: whether bd[f, f + q) is a declared tool name
2137 fn svtdecl wb edit uchar `1073664` bd edit uchar 983040 f ulong q ulong sv edit ulong 64 to bool
2138 for i ulong get sv 30
2139 if eq get svtnl i q
2140 if svteq wb bd f q add 82432 mul i 64
2141 ret true
2142 end
2143 end
2144 end
2145 ret false
2146 end
2147
2148 note svteq: whether bd[f, f + q) equals wb[b, b + q)
2149 fn svteq wb edit uchar `1073664` bd edit uchar 983040 f ulong q ulong b ulong to bool
2150 for j ulong q
2151 if ne get bd add f j get wb add b j
2152 ret false
2153 end
2154 end
2155 ret true
2156 end
2157
2158 note svtargs: the arguments token ar of a block at wb[b ..) into bd at f (object: its JSON text;
2159 note string: decoded; absent: {}); its length, or negative
2160 fn svtargs wb edit uchar `1073664` bd edit uchar 983040 tp view uint 262144 ar ulong f ulong b ulong to long
2161 if eq ar jsnone
2162 put bd f 123
2163 put bd add f 1 125
2164 ret 2
2165 end
2166 ty = js_type tp ar
2167 if eq ty 6
2168 ret js_sget view part wb b 65536 js_off tp ar part bd f 65536
2169 end
2170 if ne ty 1
2171 ret -1
2172 end
2173 o = add b js_off tp ar
2174 n = js_span tp ar
2175 for i ulong n
2176 put bd add f i get wb add o i
2177 end
2178 ret long n
2179 end
2180
2181 note svtc1: the block JSON at content [s, s + l) as tool call sv32; false when it is not one
2182 fn svtc1 wb edit uchar `1073664` sv edit ulong 64 bd edit uchar 983040 s ulong l ulong to bool
2183 if or eq l 0 gt l 65536
2184 ret false
2185 end
2186 tp = new uint 262144 0
2187 x = js_init tp 65536 64
2188 b = add 8192 s
2189 r = js_parse view part wb b l tp l
2190 if lt r 0
2191 ret false
2192 end
2193 if ne js_type tp 0 1
2194 ret false
2195 end
2196 ret svtc2 wb sv bd tp b
2197 end
2198
2199 note svtc2: the parsed block at wb[b ..) (tape tp) recorded as tool call sv32; whether it is one
2200 fn svtc2 wb edit uchar `1073664` sv edit ulong 64 bd edit uchar 983040 tp view uint 262144 b ulong to bool
2201 nm = svmem view part wb b 65536 tp 0 sknam
2202 if eq nm jsnone
2203 ret false
2204 end
2205 if ne js_type tp nm 6
2206 ret false
2207 end
2208 q = svtnam wb sv bd js_off tp nm b
2209 if lt q 1
2210 ret false
2211 end
2212 f = get sv 34
2213 g = add f ulong q
2214 a = svtargs wb bd tp svmem view part wb b 65536 tp 0 skargs g b
2215 if lt a 0
2216 ret false
2217 end
2218 svtrec sv f ulong q g ulong a
2219 ret true
2220 end
2221
2222 note svtnam: the name string at wb[b + o ..) decoded to bd[sv34 ..); its length when it is a
2223 note declared tool name, else -1
2224 fn svtnam wb edit uchar `1073664` sv edit ulong 64 bd edit uchar 983040 o ulong b ulong to long
2225 f = get sv 34
2226 q = js_sget view part wb b 65536 o part bd f 64
2227 if lt q 1
2228 ret -1
2229 end
2230 if not svtdecl wb bd f ulong q sv
2231 ret -1
2232 end
2233 ret q
2234 end
2235
2236 note svtrec: record tool call sv32's name bd[f, f + q) and arguments bd[g, g + a)
2237 fn svtrec sv edit ulong 64 f ulong q ulong g ulong a ulong to void
2238 m = mul get sv 32 4
2239 put svtcx m f
2240 put svtcx add m 1 q
2241 put svtcx add m 2 g
2242 put svtcx add m 3 a
2243 put sv 34 add g a
2244 put sv 32 add get sv 32 1
2245 end
2246
2247 note svtblk: the block JSON at content p; the position after it, or jsnone
2248 fn svtblk wb edit uchar `1073664` sv edit ulong 64 bd edit uchar 983040 p ulong n ulong to ulong
2249 if gt add p 11 n
2250 ret jsnone
2251 end
2252 if not svat wb p 74752 11
2253 ret jsnone
2254 end
2255 s = add p 11
2256 z = svfind wb s n 74763 12
2257 if eq z jsnone
2258 ret jsnone
2259 end
2260 if not svtc1 wb sv bd s sub z s
2261 ret jsnone
2262 end
2263 ret add z 12
2264 end
2265
2266 note svtcall: with detection on, whether the content from the first marker on is 1 .. 8 tool-call
2267 note blocks with only whitespace around: sv32 calls, sv33 the marker, finish tool_calls
2268 fn svtcall wb edit uchar `1073664` sv edit ulong 64 bd edit uchar 983040 to void
2269 put sv 32 0
2270 put sv 34 0
2271 if eq get sv 31 0
2272 ret
2273 end
2274 n = get sv 13
2275 q = svfind wb 0 n 74752 11
2276 if eq q jsnone
2277 ret
2278 end
2279 p = q
2280 for it ulong 9
2281 p = svskws wb p n
2282 if ge p n
2283 break
2284 end
2285 z = jsnone
2286 if lt get sv 32 8
2287 z = svtblk wb sv bd p n
2288 end
2289 if eq z jsnone
2290 put sv 32 0
2291 ret
2292 end
2293 p = z
2294 end
2295 if lt p n
2296 put sv 32 0
2297 ret
2298 end
2299 put sv 33 q
2300 put sv 8 3
2301 end
2302
2303 note ======== requests during a generation
2304
2305 note svwait: wait up to tmo ns for the listener or a pending connection (svpfd); the connection to
2306 note serve now (bytes arrived, or it closed or failed), or -1. A new connection whose first byte
2307 note does not arrive within 20 ms waits in a free pending slot (8; all taken: new connections stay
2308 note in the listen backlog), so a connection that sends nothing costs the caller 20 ms once, not a
2309 note head deadline. A pending connection is closed unanswered 10 s after its accept.
2310 fn svwait sv edit ulong 64 tmo long to long
2311 now = ulong monons
2312 iv = tmo
2313 n = ulong 0
2314 for i ulong 8
2315 f = get svpfd i
2316 if ne f 0
2317 age = sub now get svpat i
2318 if ge age `10000000000`
2319 y = fclose int sub f 1
2320 put svpfd i 0
2321 else
2322 rm = long sub `10000000000` age
2323 if or lt iv 0 lt rm iv
2324 iv = rm
2325 end
2326 svprec n sub f 1
2327 put svpix n i
2328 n = add n 1
2329 end
2330 end
2331 end
2332 fr = lt n 8
2333 m = n
2334 if fr
2335 svprec n get sv 36
2336 m = add n 1
2337 end
2338 r = pollfds svpl 0 mul m 8 iv
2339 if le r 0
2340 ret -1
2341 end
2342 for j ulong n
2343 if ne get svpl add mul j 8 6 0
2344 s = get svpix j
2345 cp = long sub get svpfd s 1
2346 put svpfd s 0
2347 ret cp
2348 end
2349 end
2350 if not fr
2351 ret -1
2352 end
2353 c = haccept int get sv 36 0
2354 if lt c 0
2355 ret -1
2356 end
2357 w = waitio int c 1 long `20000000`
2358 if ne w 0
2359 ret c
2360 end
2361 for k ulong 8
2362 if eq get svpfd k 0
2363 put svpfd k ulong add c 1
2364 put svpat k ulong monons
2365 ret -1
2366 end
2367 end
2368 y2 = fclose int c
2369 ret -1
2370 end
2371
2372 note svprec: pollfd record j for descriptor fd, events POLLIN
2373 fn svprec j ulong fd ulong to void
2374 b = mul j 8
2375 put svpl b uchar and fd 255
2376 put svpl add b 1 uchar and shr fd 8 255
2377 put svpl add b 2 uchar and shr fd 16 255
2378 put svpl add b 3 uchar and shr fd 24 255
2379 put svpl add b 4 1
2380 put svpl add b 5 0
2381 put svpl add b 6 0
2382 put svpl add b 7 0
2383 end
2384
2385 note svgone: whether the client of the generating request has left: a non-blocking peek (MSG_PEEK |
2386 note MSG_DONTWAIT) of its socket reads end of stream or an error. Request bytes stay unread. A
2387 note client that half-closes its sending side after the request counts as gone.
2388 fn svgone cs edit ulong 320 to bool
2389 r = recv int get cs HFD svpk 0 1 66
2390 if eq r 0
2391 ret true
2392 end
2393 ret and lt r 0 and ne r -11 ne r -4
2394 end
2395
2396 note svside: serve the connections svwait yields (the first wait up to tmo ns, then without waiting;
2397 note at most 9 per call) with svside2
2398 fn svside bd edit uchar 983040 sv edit ulong 64 tmo long to void
2399 c = svwait sv tmo
2400 for k ulong 9
2401 if lt c 0
2402 ret
2403 end
2404 svside2 c bd sv
2405 c = svwait sv 0
2406 end
2407 end
2408
2409 note svside2: one request while a completion generates: health busy, models, else 503 (logged)
2410 fn svside2 c long bd edit uchar 983040 sv edit ulong 64 to void
2411 cs = new ulong 320 0
2412 io = new uchar 131072 0
2413 r = hinit cs io int c 1
2414 if lt r 0
2415 x = hclose cs
2416 ret
2417 end
2418 note bytes have arrived (svwait), so a head or body that stalls costs at most 250 ms or 1 s
2419 hsettmo cs `250000000` `250000000`
2420 hsetbdl cs `1000000000`
2421 put cs HBCAP 983040
2422 r = hreqrd cs io
2423 if gt r 0
2424 put cs HKEEP 0
2425 st = svbusy cs io bd
2426 svlogs st
2427 else
2428 if lt r 0
2429 x = hreject cs io r
2430 end
2431 end
2432 x = hclose cs
2433 end
2434
2435 fn svbusy cs edit ulong 320 io edit uchar 131072 bd edit uchar 983040 to ulong
2436 if and hispath cs io svphlth hismeth cs io svmget
2437 x = svstat cs io 200 svthbsy
2438 ret 200
2439 end
2440 if and hispath cs io svpmods hismeth cs io svmget
2441 x = svmodw cs io
2442 ret 200
2443 end
2444 note read the body before answering, so the client sees the 503 rather than a reset
2445 n = hbodyall cs io bd
2446 x = svs503 cs io
2447 ret 503
2448 end
2449
2450 fn svlogs st ulong to void
2451 oputc 67
2452 oputc 32
2453 oputu st
2454 for i ulong 4
2455 oputc 32
2456 oputc 48
2457 end
2458 oputc 10
2459 oflush
2460 end
2461
2462 note ======== serving
2463
2464 note svid: response id chatcmpl-ollm- (cmpl-ollm- on /v1/completions) into
2465 note wb[82176 ..), its length into sv15
2466 fn svid wb edit uchar `1073664` sv edit ulong 64 to void
2467 put sv 9 add get sv 9 1
2468 p = ulong 82176
2469 if eq get sv 37 1
2470 p = svmcat wb p 82240 svcidp
2471 else
2472 p = svmcat wb p 82240 svidp
2473 end
2474 p = svmdec wb p 82240 get sv 9
2475 put sv 15 sub p 82176
2476 end
2477
2478 fn svlog sv edit ulong 64 to void
2479 oputc 67
2480 oputc 32
2481 oputu get sv 21
2482 oputc 32
2483 oputu get sv 6
2484 oputc 32
2485 oputu get sv 7
2486 oputc 32
2487 oputu get sv 8
2488 oputc 32
2489 oputu div sub ulong monons get sv 11 `1000000`
2490 oputc 10
2491 oflush
2492 end
2493
2494 note svprep: the context (token 0, then the template's encoding) and the forced tokens; 0 or 11
2495 fn svprep wb edit uchar `1073664` sv edit ulong 64 to ulong
2496 put og 9 0
2497 ocpush 0
2498 np = svenc view part wb 90624 get sv 5 get sv 5 0
2499 put sv 6 add np 1
2500 if ge get sv 6 get sv 19
2501 ret 11
2502 end
2503 if eq get sv 25 1
2504 c = ulong 0
2505 if gt get sv 26 0
2506 c = svenc view part wb 8192 get sv 26 get sv 26 1
2507 end
2508 if gt c 4096
2509 c = 4096
2510 end
2511 put sv 24 c
2512 end
2513 ret 0
2514 end
2515
2516 note svchat: POST /v1/chat/completions
2517 fn svchat cs edit ulong 320 io edit uchar 131072 bd edit uchar 983040 wb edit uchar `1073664` sv edit ulong 64 to long
2518 note the request's clock and log fields come from svroute (svbeg)
2519 n = hbodyall cs io bd
2520 if lt n 0
2521 put cs HKEEP 0
2522 if eq n HEBIG
2523 put sv 16 13
2524 r = svfail cs io wb sv svbig
2525 svlog sv
2526 ret r
2527 end
2528 put sv 21 400
2529 svlog sv
2530 ret hrsperr cs io 400
2531 end
2532 note generation outlasts the 60 s body deadline: only the idle limit applies from here
2533 hsetdl cs 0
2534 e = svparse bd ulong n wb sv
2535 if eq e 0
2536 e = svprep wb sv
2537 end
2538 if ne e 0
2539 put sv 16 e
2540 st = ulong 400
2541 if eq e 25
2542 st = ulong 413
2543 end
2544 r = svfail cs io wb sv st
2545 svlog sv
2546 ret r
2547 end
2548 svid wb sv
2549 put sv 10 ulong div realns `1000000000`
2550 ob = new uchar 409600 0
2551 put sv 21 200
2552 if eq get sv 0 1
2553 r = hrspsse cs io
2554 r = hfe r svchunk cs io ob wb sv 0
2555 if lt r 0
2556 svlog sv
2557 ret r
2558 end
2559 r = svgen cs io ob wb sv bd
2560 if lt r 0
2561 svlog sv
2562 ret r
2563 end
2564 r = svchunk cs io ob wb sv 2
2565 if eq get sv 4 1
2566 r = hfe r svchunk cs io ob wb sv 3
2567 end
2568 r = hfe r hssedat cs io svdone
2569 r = hfe r hsseend cs io
2570 r = hfe r hwfin cs io
2571 svlog sv
2572 ret r
2573 end
2574 r = svgen cs io ob wb sv bd
2575 if lt r 0
2576 svlog sv
2577 ret r
2578 end
2579 m = svfull ob wb sv bd
2580 if lt m 0
2581 put sv 16 16
2582 r = svfail cs io wb sv 500
2583 svlog sv
2584 ret r
2585 end
2586 r = svsend cs io ob 200 ulong m
2587 svlog sv
2588 ret r
2589 end
2590
2591 fn svroute cs edit ulong 320 io edit uchar 131072 bd edit uchar 983040 wb edit uchar `1073664` sv edit ulong 64 to long
2592 svbeg sv
2593 if hispath cs io svpchat
2594 if or hismeth cs io svmpost eq OMUTMETH 0
2595 put sv 37 0
2596 ret svchat cs io bd wb sv
2597 end
2598 ret svend cs io wb sv 15 405
2599 end
2600 if hispath cs io svpcomp
2601 if or hismeth cs io svmpost eq OMUTMETH 0
2602 put sv 37 1
2603 ret svchat cs io bd wb sv
2604 end
2605 ret svend cs io wb sv 15 405
2606 end
2607 if hispath cs io svphlth
2608 if or hismeth cs io svmget eq OMUTMETH 0
2609 ret svok cs io sv svthlth
2610 end
2611 ret svend cs io wb sv 15 405
2612 end
2613 if hispath cs io svpmods
2614 if or hismeth cs io svmget eq OMUTMETH 0
2615 ret svmods cs io sv
2616 end
2617 ret svend cs io wb sv 15 405
2618 end
2619 if eq OMUTPATH 0
2620 ret svok cs io sv svthlth
2621 end
2622 ret svend cs io wb sv 14 404
2623 end
2624
2625 note svbeg: a request's log fields cleared, its clock started
2626 fn svbeg sv edit ulong 64 to void
2627 put sv 11 ulong monons
2628 put sv 6 0
2629 put sv 7 0
2630 put sv 8 0
2631 end
2632
2633 note svend: error code e with status st, logged
2634 fn svend cs edit ulong 320 io edit uchar 131072 wb edit uchar `1073664` sv edit ulong 64 e ulong st ulong to long
2635 put sv 16 e
2636 r = svfail cs io wb sv st
2637 svlog sv
2638 ret r
2639 end
2640
2641 note svok: the fixed JSON text t with status 200, logged
2642 fn svok cs edit ulong 320 io edit uchar 131072 sv edit ulong 64 t view uchar * to long
2643 put sv 21 200
2644 r = svstat cs io 200 t
2645 svlog sv
2646 ret r
2647 end
2648
2649 note svconn: one accepted connection c carries one request, answered with Connection: close.
2650 note Connections are served one at a time, so a kept-alive idle connection would hold the listener:
2651 note allen-chat pools its upstream connection, and its next request queued 30 s behind the pooled
2652 note one (the old idle limit) until this change. svwait hands over a connection only once its first
2653 note bytes arrived; the rest of the head must arrive within 10 s.
2654 fn svconn c long cs edit ulong 320 io edit uchar 131072 bd edit uchar 983040 wb edit uchar `1073664` sv edit ulong 64 to void
2655 r = hinit cs io int c 1
2656 if lt r 0
2657 x = hclose cs
2658 ret
2659 end
2660 hsettmo cs `10000000000` `10000000000`
2661 hsetbdl cs `60000000000`
2662 note the JSON parser takes at most 983040 bytes (jsmaxn); larger Content-Length is 413 at the head
2663 put cs HBCAP 983040
2664 r = hreqrd cs io
2665 if eq r HEBIG
2666 put cs HKEEP 0
2667 svbeg sv
2668 x = svend cs io wb sv 13 svbig
2669 end
2670 if and lt r 0 ne r HEBIG
2671 x = hreject cs io r
2672 end
2673 if gt r 0
2674 put cs HKEEP 0
2675 x = svroute cs io bd wb sv
2676 end
2677 x = hclose cs
2678 end
2679
2680 note svpre: whether the argument in oarg (length n) starts with t
2681 fn svpre n ulong t view uchar * to bool
2682 if lt n len t
2683 ret false
2684 end
2685 for i ulong len t
2686 if ne get oarg i get t i
2687 ret false
2688 end
2689 end
2690 ret true
2691 end
2692
2693 note svdig: the decimal oarg[a, n) (1 .. 7 digits), or 0
2694 fn svdig a ulong n ulong to ulong
2695 if or le n a gt sub n a 7
2696 ret 0
2697 end
2698 v = ulong 0
2699 for i ulong sub n a
2700 c = ulong get oarg add a i
2701 if or lt c 48 gt c 57
2702 ret 0
2703 end
2704 v = add mul v 10 sub c 48
2705 end
2706 ret v
2707 end
2708
2709 note svcli: the options after MERGES: [MAXCONN] (first only), tplmax=N, test=1; MAXCONN
2710 fn svcli sv edit ulong 64 na ulong to ulong
2711 mx = ulong `1000000`
2712 for j ulong sub na 5
2713 i = add j 5
2714 n = oargp i
2715 if svpre n svoptt
2716 v = svdig 7 n
2717 if or lt v 256 gt v 983040
2718 exit 2
2719 end
2720 put sv 23 v
2721 else
2722 if and eq n len svopte svpre n svopte
2723 put sv 22 1
2724 else
2725 if ne i 5
2726 exit 2
2727 end
2728 mx = oargu 5
2729 if eq mx 0
2730 exit 2
2731 end
2732 end
2733 end
2734 end
2735 ret mx
2736 end
2737
2738 note serve PORT CK MERGES [MAXCONN] [tplmax=N] [test=1]
2739 fn cmdserve to void
2740 na = oargc
2741 if or lt na 5 gt na 8
2742 exit 2
2743 end
2744 port = oargu 2
2745 if or eq port 0 gt port 65535
2746 exit 2
2747 end
2748 sv = new ulong 64 0
2749 put sv 23 65536
2750 mx = svcli sv na
2751 ockload 3
2752 v = get og 0
2753 obload 4 v
2754 ochipup
2755 cs = new ulong 320 0
2756 io = new uchar 131072 0
2757 bd = new uchar 983040 0
2758 wb = new uchar `1073664` 0
2759 a = new uchar 16 0
2760 put sv 19 get og 1
2761 x = svmcat wb 74752 74763 svtco
2762 x = svmcat wb 74763 74775 svtcc
2763 svmid
2764 r = haddr4 a uint mul `2130706433` OMUTLOOP port
2765 lf = hlisten a 16
2766 if lt lf 0
2767 exit 71
2768 end
2769 put sv 36 ulong lf
2770 for i ulong len svport
2771 oputc ulong get svport i
2772 end
2773 oputu port
2774 oputc 10
2775 oflush
2776 nc = ulong 0
2777 for j ulong `999999999`
2778 c = svwait sv `60000000000`
2779 if ge c 0
2780 svconn c cs io bd wb sv
2781 nc = add nc 1
2782 if ge nc mx
2783 break
2784 end
2785 end
2786 end
2787 y = fclose int lf
2788 end