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/omega-mv/blob/main/seed/agent/agent.om # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / seed / agent / agent.om` 2404 lines ยท 45719 bytes Raw Blame History 1 note seed/agent/agent.om -- omagent: a small coding agent in seeded Omega (program mode). 2 note Compose: { printf 'om2\n'; cat seed/json/json.om seed/json/jsonw.om seed/net/http.om seed/net/notls.om seed/agent/path.om seed/agent/agent.om; } | seed/out/omc > omagent 3 note Design, safety model, CLI and exit codes: seed/agent/README.md. Tests: seed/agent/tests/check.py. 4 note The model is untrusted: every byte it sends is parsed by json.om/http.om under fixed caps, every 5 note tool call is checked against permissions the operator gave on the command line, and the API key 6 note never reaches stdout, stderr, the session file or a child process (execprog passes no environment). 7 8 const QUSED ulong 0 9 const QIP ulong 1 10 const QPORT ulong 2 11 const QTMO ulong 3 12 const QCTMO ulong 4 13 const QROUND ulong 5 14 const QWRITE ulong 6 15 const QNCMD ulong 7 16 const QNFET ulong 8 17 const QSFD ulong 9 18 const QHBL ulong 10 19 const QNREC ulong 11 20 const QRES ulong 12 21 const QINL ulong 13 22 const QINP ulong 14 23 const QINEOF ulong 15 24 const QMEML ulong 16 25 const QHAVEK ulong 17 26 const QSESS ulong 18 27 const QTST ulong 19 28 const QURL ulong 20 29 const QMODEL ulong 21 30 const SCLEN ulong 0 31 const SNC ulong 1 32 const SFIN ulong 3 33 const SID ulong 8 34 const SNM ulong 16 35 const SARG ulong 24 36 const IPATH ulong 0 37 const IMODEL ulong 1 38 const IKEY ulong 2 39 const IWORK ulong 3 40 const ISESS ulong 4 41 const IMEM ulong 5 42 const IPROMPT ulong 6 43 const IKEYENV ulong 7 44 const IHOST ulong 8 45 const ICMD ulong 16 46 const IFET ulong 32 47 const QMAXC ulong 8 48 const QRESC ulong 65536 49 const QCONC ulong 131072 50 const QMSG ulong 262144 51 52 global gpool uchar 65536 53 global gsof ulong 64 54 global gsln ulong 64 55 global gnum ulong 32 56 global gst ulong 64 57 global gcont uchar 131072 58 global gtid uchar 2048 59 global gtnm uchar 1024 60 global gtarg uchar 524288 61 global gres uchar 70000 62 global gmem uchar 65536 63 global gmb ulong 8192 64 global gmr uchar 8192 65 global gin uchar 65536 66 67 text cpre "omagent: " 68 text cnl "\n" 69 text cver "omagent 0.1.0 (seeded Omega, omega-mv seed/agent)\n" 70 text cusage "usage: omagent --url http://IP:PORT/PATH --model NAME [-p PROMPT] [--workdir DIR]\n [--session FILE] [--memory FILE] [--allow-cmd /ABS/PATH]... [--allow-write]\n [--allow-fetch HOST:PORT]... [--timeout S] [--cmd-timeout S] [--max-rounds N]\n [--key-env NAME] | --version | --help\nWithout -p, prompts are read from stdin, one per line. The API key is read from the\nenvironment variable OMAGENT_API_KEY (or --key-env NAME) and sent as a Bearer token.\n" 71 text odurl "--url" 72 text odmodel "--model" 73 text odp "-p" 74 text odwork "--workdir" 75 text odsess "--session" 76 text odmem "--memory" 77 text odcmd "--allow-cmd" 78 text odwrite "--allow-write" 79 text odfetch "--allow-fetch" 80 text odtmo "--timeout" 81 text odctmo "--cmd-timeout" 82 text odround "--max-rounds" 83 text odkeyenv "--key-env" 84 text odver "--version" 85 text odhelp "--help" 86 text cdefkey "OMAGENT_API_KEY" 87 text chttp "http://" 88 text chttps "https://" 89 text clocal "localhost" 90 text cpost "POST" 91 text cget "GET" 92 text chost "Host" 93 text cctype "Content-Type" 94 text cjson "application/json" 95 text caccept "Accept" 96 text cevst "text/event-stream" 97 text cauth "Authorization" 98 text cbearer "Bearer " 99 text cnctype "content-type" 100 text cevs "event-stream" 101 text cslash "/" 102 text cbody1 "{\"model\":\"" 103 text cbody2 "\",\"stream\":true,\"tools\":" 104 text cbody3 ",\"messages\":[" 105 text ccomma "," 106 text cbody4 "]}" 107 text ctools "[{\"type\":\"function\",\"function\":{\"name\":\"read_file\",\"description\":\"Read a text file inside the workspace (at most 64 KiB).\",\"parameters\":{\"type\":\"object\",\"properties\":{\"path\":{\"type\":\"string\",\"description\":\"path relative to the workspace\"}},\"required\":[\"path\"]}}},{\"type\":\"function\",\"function\":{\"name\":\"write_file\",\"description\":\"Create or replace a file inside the workspace.\",\"parameters\":{\"type\":\"object\",\"properties\":{\"path\":{\"type\":\"string\",\"description\":\"path relative to the workspace\"},\"content\":{\"type\":\"string\"}},\"required\":[\"path\",\"content\"]}}},{\"type\":\"function\",\"function\":{\"name\":\"run_command\",\"description\":\"Run an allowlisted program in the workspace. argv[0] is its absolute path; no shell, no environment.\",\"parameters\":{\"type\":\"object\",\"properties\":{\"argv\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}}},\"required\":[\"argv\"]}}},{\"type\":\"function\",\"function\":{\"name\":\"http_fetch\",\"description\":\"GET an http:// URL on an allowlisted host:port (at most 64 KiB of body).\",\"parameters\":{\"type\":\"object\",\"properties\":{\"url\":{\"type\":\"string\"}},\"required\":[\"url\"]}}}]" 108 text csys "You are omagent, a small coding agent. Answer briefly. Use the tools only when they are needed; file paths are relative to the workspace. A tool result that starts with refused: was blocked by the operator's permissions; do not retry it." 109 text cmemh "\n\nMemory file:\n" 110 text crole "role" 111 text cuser "user" 112 text cassist "assistant" 113 text ctool "tool" 114 text csystem "system" 115 text ccontent "content" 116 text ctoolc "tool_calls" 117 text ctcid "tool_call_id" 118 text cid "id" 119 text ctype "type" 120 text cfunc "function" 121 text cname "name" 122 text cargs "arguments" 123 text cindex "index" 124 text cdelta "delta" 125 text cmessage "message" 126 text cchoices "choices" 127 text cfinish "finish_reason" 128 text cerror "error" 129 text cpath "path" 130 text cargv "argv" 131 text curl "url" 132 text cdone "[DONE]" 133 text ccall "call_" 134 text tnread "read_file" 135 text tnwrite "write_file" 136 text tnrun "run_command" 137 text tnfetch "http_fetch" 138 text cdevnull "/dev/null" 139 text ecfg "missing or bad option; see --help" 140 text ekeybad "the API key holds bytes outside 0x21..0x7e; refusing it" 141 text ework "cannot enter --workdir" 142 text esess "cannot open or read --session file" 143 text esline "session file line is not a valid message object; refusing to continue" 144 text emem "cannot read --memory file" 145 text econn "cannot connect to the model server" 146 text ewrite "sending the request failed" 147 text etmo "the model server timed out" 148 text eread "reading the model response failed" 149 text estat "the model server answered with status" 150 text ebad "malformed model output" 151 text ebig "malformed model output: a JSON reply or one SSE event is over 256 KiB" 152 text eevj "malformed model output: an event is not a JSON object" 153 text eserr "the model server sent an error object" 154 text enodone "malformed model output: the stream ended before [DONE] or a finish_reason" 155 text emany "malformed model output: more than 8 tool calls, or a bad index" 156 text elong "malformed model output: a tool call field is too long" 157 text econt "malformed model output: the reply is over 128 KiB" 158 text emsg "a message could not be encoded (invalid UTF-8 or over 256 KiB)" 159 text efull "the conversation history is full" 160 text esave "cannot append to the session file" 161 text eround "stopped: tool round limit reached (--max-rounds)" 162 text ltool "tool " 163 text lok ": ok" 164 text lref ": refused" 165 text lerr ": error" 166 text rnoargs "error: tool arguments are not a JSON object" 167 text runknown "refused: unknown tool " 168 text rpatharg "error: the path argument must be a string" 169 text rpath "refused: the path must be relative to the workspace, without empty, . or .. parts and without symbolic links" 170 text ropen "error: cannot open the file, errno " 171 text rreadf "error: reading the file failed, errno " 172 text rtrunc "\n[truncated at 65536 bytes]" 173 text rnowrite "refused: write_file is disabled (the operator did not pass --allow-write)" 174 text rcontarg "error: the content argument must be a string under 256 KiB" 175 text rwrote "ok: wrote " 176 text rbytes " bytes to " 177 text rwfail "error: writing the file failed, errno " 178 text rnocmd "refused: run_command is disabled (the operator gave no --allow-cmd)" 179 text rargv "error: argv must be an array of 1..63 strings" 180 text rnotal "refused: the program is not on the --allow-cmd list: " 181 text rnul "refused: an argument holds a zero byte" 182 text rpack "refused: argv is over 4 KiB" 183 text rpipe "error: cannot create a pipe or fork" 184 text rexit "\n[exit status " 185 text rsig "\n[killed by signal " 186 text rtmo "\n[killed: timeout after " 187 text rsec " s]" 188 text rclose "]" 189 text rnofet "refused: http_fetch is disabled (the operator gave no --allow-fetch)" 190 text rurlarg "error: the url argument must be a string" 191 text rnohttp "refused: only http:// URLs are supported by this build" 192 text rurlbad "error: malformed url" 193 text rnotfa "refused: the host:port is not on the --allow-fetch list: " 194 text rconn "error: cannot connect" 195 text rfreq "error: request failed" 196 text rftmo "error: timed out" 197 text rstatus "status=" 198 199 note ======== output and small helpers 200 201 fn agwr fd int b view uchar * o ulong n ulong to bool 202 p = ulong 0 203 while lt p n bound 999999 204 w = write fd b add o p sub n p 205 if le w 0 206 ret false 207 end 208 p = add p ulong w 209 end 210 ret true 211 end 212 213 fn agdec v ulong d edit uchar 24 to ulong 214 t = new uchar 24 0 215 k = ulong 0 216 x = v 217 for i ulong 21 218 put t k uchar add 48 rem x 10 219 k = add k 1 220 x = div x 10 221 if eq x 0 222 break 223 end 224 end 225 for j ulong k 226 put d j get t sub sub k 1 j 227 end 228 ret k 229 end 230 231 fn agerr t view uchar * to void 232 r = agwr 2 cpre 0 len cpre 233 r = agwr 2 t 0 len t 234 r = agwr 2 cnl 0 1 235 end 236 237 fn agerrn t view uchar * v ulong to void 238 d = new uchar 24 0 239 r = agwr 2 cpre 0 len cpre 240 r = agwr 2 t 0 len t 241 d2 = new uchar 1 32 242 r = agwr 2 d2 0 1 243 n = agdec v d 244 r = agwr 2 d 0 n 245 r = agwr 2 cnl 0 1 246 end 247 248 note ======== string pool for configuration (ids I*), filled once from argv and the environment 249 250 fn agstash id ulong a view uchar * to bool 251 u = get gnum QUSED 252 n = len a 253 if gt n sub 65536 u 254 ret false 255 end 256 for i ulong n 257 put gpool add u i get a i 258 end 259 put gsof id u 260 put gsln id n 261 put gnum QUSED add u n 262 ret true 263 end 264 265 fn agsget id ulong d edit uchar * to ulong 266 n = get gsln id 267 if gt n len d 268 ret 0 269 end 270 o = get gsof id 271 for i ulong n 272 put d i get gpool add o i 273 end 274 ret n 275 end 276 277 fn agseq id ulong a view uchar * to bool 278 n = get gsln id 279 if ne n len a 280 ret false 281 end 282 o = get gsof id 283 for i ulong n 284 if ne get gpool add o i get a i 285 ret false 286 end 287 end 288 ret true 289 end 290 291 note ======== tool result buffer gres[0, QRES), status QTST 0 ok, 1 refused, 2 error 292 293 fn agradd t view uchar * to void 294 r = get gnum QRES 295 for i ulong len t 296 if ge r 69000 297 break 298 end 299 put gres r get t i 300 r = add r 1 301 end 302 put gnum QRES r 303 end 304 305 fn agrset st ulong t view uchar * to void 306 put gnum QRES 0 307 put gnum QTST st 308 agradd t 309 end 310 311 fn agrnum v ulong to void 312 d = new uchar 24 0 313 n = agdec v d 314 agradd view part d 0 n 315 end 316 317 fn agrerr t view uchar * e long to void 318 agrset 2 t 319 x = ulong 0 320 if lt e 0 321 x = ulong neg e 322 end 323 agrnum x 324 end 325 326 note agu8fix: replace every byte of gres that is not part of well-formed UTF-8 by '?', so the 327 note JSON writer (which validates UTF-8) always accepts a tool result 328 fn agu8fix to void 329 n = get gnum QRES 330 i = ulong 0 331 while lt i n bound 999999 332 c = get gres i 333 if lt c 128 334 i = add i 1 335 continue 336 end 337 k = ulong 0 338 lo = uchar 128 339 hi = uchar 191 340 if and ge c 194 le c 223 341 k = 1 342 end 343 if and ge c 224 le c 239 344 k = 2 345 end 346 if and ge c 240 le c 244 347 k = 3 348 end 349 if eq c 224 350 lo = 160 351 end 352 if eq c 237 353 hi = 159 354 end 355 if eq c 240 356 lo = 144 357 end 358 if eq c 244 359 hi = 143 360 end 361 ok = and gt k 0 lt add i k n 362 if ok 363 b = get gres add i 1 364 if or lt b lo gt b hi 365 ok = false 366 end 367 end 368 if ok 369 for j ulong sub k 1 370 b2 = get gres add add i 2 j 371 if or lt b2 128 gt b2 191 372 ok = false 373 end 374 end 375 end 376 if ok 377 i = add add i k 1 378 else 379 put gres i 63 380 i = add i 1 381 end 382 end 383 end 384 385 note ======== URLs: http://HOST[:PORT][/PATH]; HOST is a dotted IPv4 address or localhost 386 note r: 0 ip, 1 port, 2 authority offset, 3 authority length, 4 path offset, 5 path length, 6 host length 387 388 fn agip4 u view uchar * o ulong n ulong to long 389 v = ulong 0 390 part4 = ulong 0 391 dig = ulong 0 392 cnt = ulong 0 393 for i ulong n 394 c = get u add o i 395 if eq c 46 396 if or eq dig 0 gt part4 255 397 ret -1 398 end 399 v = or shl v 8 part4 400 cnt = add cnt 1 401 part4 = 0 402 dig = 0 403 continue 404 end 405 if or lt c 48 gt c 57 406 ret -1 407 end 408 part4 = add mul part4 10 ulong sub c 48 409 dig = add dig 1 410 if gt dig 3 411 ret -1 412 end 413 end 414 if or or eq dig 0 gt part4 255 ne cnt 3 415 ret -1 416 end 417 ret long or shl v 8 part4 418 end 419 420 fn agurl u view uchar * r edit ulong 8 to long 421 n = len u 422 if and ge n 8 heq view part u 0 8 chttps 423 ret -1 424 end 425 if or lt n 8 not heq view part u 0 7 chttp 426 ret -1 427 end 428 a = ulong 7 429 e = a 430 while lt e n bound 999999 431 if eq get u e 47 432 break 433 end 434 e = add e 1 435 end 436 if eq e a 437 ret -2 438 end 439 hl = sub e a 440 port = ulong 80 441 for i ulong sub e a 442 if eq get u add a i 58 443 hl = i 444 pd = add add a i 1 445 if or eq pd e gt sub e pd 5 446 ret -2 447 end 448 if not hdigits view part u pd sub e pd 449 ret -2 450 end 451 port = hdecv view part u pd sub e pd 452 break 453 end 454 end 455 if or eq port 0 gt port 65535 456 ret -2 457 end 458 ip = long 0 459 if heq view part u a hl clocal 460 ip = `2130706433` 461 else 462 ip = agip4 u a hl 463 end 464 if lt ip 0 465 ret -2 466 end 467 for j ulong sub n e 468 c = get u add e j 469 if or le c 32 ge c 127 470 ret -2 471 end 472 end 473 put r 0 ulong ip 474 put r 1 port 475 put r 2 a 476 put r 3 sub e a 477 put r 4 e 478 put r 5 sub n e 479 put r 6 hl 480 ret 0 481 end 482 483 note ======== command line 484 485 fn agnamech c uchar to bool 486 if and ge c 97 le c 122 487 ret true 488 end 489 if and ge c 65 le c 90 490 ret true 491 end 492 if and ge c 48 le c 57 493 ret true 494 end 495 ret or or or eq c 46 eq c 95 or eq c 58 eq c 47 or or eq c 45 eq c 64 eq c 43 496 end 497 498 fn agsetnum ab view uchar 65536 n ulong slot ulong mul1 ulong to bool 499 if not hdigits view part ab 0 n 500 ret false 501 end 502 v = hdecv view part ab 0 n 503 if or eq v 0 gt v 86400 504 ret false 505 end 506 put gnum slot mul v mul1 507 ret true 508 end 509 510 fn agseturl ab view uchar 65536 n ulong to bool 511 r = new ulong 8 0 512 rc = agurl view part ab 0 n r 513 if lt rc 0 514 ret false 515 end 516 put gnum QIP get r 0 517 put gnum QPORT get r 1 518 put gnum QURL 1 519 if eq get r 5 0 520 if not agstash IPATH cslash 521 ret false 522 end 523 else 524 if not agstash IPATH view part ab get r 4 get r 5 525 ret false 526 end 527 end 528 ret agstash IHOST view part ab get r 2 get r 3 529 end 530 531 fn agsetmodel ab view uchar 65536 n ulong to bool 532 if or eq n 0 gt n 200 533 ret false 534 end 535 for i ulong n 536 if not agnamech get ab i 537 ret false 538 end 539 end 540 put gnum QMODEL 1 541 ret agstash IMODEL view part ab 0 n 542 end 543 544 note agopt1: one option that takes a value; ab holds the value, n its length 545 fn agopt1 op ulong ab view uchar 65536 n ulong to bool 546 if eq op 1 547 ret agseturl ab n 548 end 549 if eq op 2 550 ret agsetmodel ab n 551 end 552 if eq op 3 553 if eq n 0 554 ret false 555 end 556 ret agstash IPROMPT view part ab 0 n 557 end 558 if eq op 4 559 ret and gt n 0 agstash IWORK view part ab 0 n 560 end 561 if eq op 5 562 ret and gt n 0 agstash ISESS view part ab 0 n 563 end 564 if eq op 6 565 ret and gt n 0 agstash IMEM view part ab 0 n 566 end 567 if eq op 7 568 k = get gnum QNCMD 569 if or or ge k 16 lt n 2 ne get ab 0 47 570 ret false 571 end 572 put gnum QNCMD add k 1 573 ret agstash add ICMD k view part ab 0 n 574 end 575 if eq op 8 576 k2 = get gnum QNFET 577 if or ge k2 16 eq n 0 578 ret false 579 end 580 put gnum QNFET add k2 1 581 ret agstash add IFET k2 view part ab 0 n 582 end 583 if eq op 9 584 ret agsetnum ab n QTMO `1000000000` 585 end 586 if eq op 10 587 ret agsetnum ab n QCTMO `1000000000` 588 end 589 if eq op 11 590 if not agsetnum ab n QROUND 1 591 ret false 592 end 593 ret le get gnum QROUND 64 594 end 595 if eq op 12 596 ret and gt n 0 agstash IKEYENV view part ab 0 n 597 end 598 ret false 599 end 600 601 fn agoptid ab view uchar 65536 n ulong to ulong 602 if heq view part ab 0 n odurl 603 ret 1 604 end 605 if heq view part ab 0 n odmodel 606 ret 2 607 end 608 if heq view part ab 0 n odp 609 ret 3 610 end 611 if heq view part ab 0 n odwork 612 ret 4 613 end 614 if heq view part ab 0 n odsess 615 ret 5 616 end 617 if heq view part ab 0 n odmem 618 ret 6 619 end 620 if heq view part ab 0 n odcmd 621 ret 7 622 end 623 if heq view part ab 0 n odfetch 624 ret 8 625 end 626 if heq view part ab 0 n odtmo 627 ret 9 628 end 629 if heq view part ab 0 n odctmo 630 ret 10 631 end 632 if heq view part ab 0 n odround 633 ret 11 634 end 635 if heq view part ab 0 n odkeyenv 636 ret 12 637 end 638 if heq view part ab 0 n odwrite 639 ret 20 640 end 641 if heq view part ab 0 n odver 642 ret 21 643 end 644 if heq view part ab 0 n odhelp 645 ret 22 646 end 647 ret 0 648 end 649 650 note agargs: 0 ready, 1 printed --version/--help (exit 0), 2 usage error 651 fn agargs to int 652 ab = new uchar 65536 0 653 c = ulong argcnt 654 put gnum QTMO `120000000000` 655 put gnum QCTMO `10000000000` 656 put gnum QROUND 8 657 ok = agstash IKEYENV cdefkey 658 i = ulong 1 659 while lt i c bound 9999 660 n = argread i ab 0 65536 661 if lt n 0 662 agerr ecfg 663 ret 2 664 end 665 op = agoptid ab ulong n 666 if eq op 0 667 agerr ecfg 668 ret 2 669 end 670 if eq op 20 671 put gnum QWRITE 1 672 i = add i 1 673 continue 674 end 675 if eq op 21 676 ok = agwr 1 cver 0 len cver 677 ret 1 678 end 679 if eq op 22 680 ok = agwr 1 cusage 0 len cusage 681 ret 1 682 end 683 if ge add i 1 c 684 agerr ecfg 685 ret 2 686 end 687 n2 = argread add i 1 ab 0 65536 688 if lt n2 0 689 agerr ecfg 690 ret 2 691 end 692 if not agopt1 op ab ulong n2 693 agerr ecfg 694 ret 2 695 end 696 i = add i 2 697 end 698 if or eq get gnum QURL 0 eq get gnum QMODEL 0 699 agerr ecfg 700 ok = agwr 2 cusage 0 len cusage 701 ret 2 702 end 703 ret 0 704 end 705 706 fn agkeyok eb view uchar 8192 o ulong n ulong to bool 707 if or eq n 0 gt n 4000 708 ret false 709 end 710 for j ulong n 711 c = get eb add o j 712 if or lt c 33 gt c 126 713 ret false 714 end 715 end 716 ret true 717 end 718 719 note agkeyload: find KEYENV=VALUE in the environment; the value goes to the pool only 720 fn agkeyload to int 721 eb = new uchar 8192 0 722 kn = new uchar 256 0 723 k = agsget IKEYENV kn 724 if eq k 0 725 ret 0 726 end 727 rc = int 0 728 for i ulong ulong envcnt 729 n = envread i eb 0 8192 730 if le n long k 731 continue 732 end 733 if ne get eb k 61 734 continue 735 end 736 if not heq view part eb 0 k view part kn 0 k 737 continue 738 end 739 vl = sub ulong n add k 1 740 if agkeyok eb add k 1 vl 741 ok = agstash IKEY view part eb add k 1 vl 742 put gnum QHAVEK 1 743 else 744 agerr ekeybad 745 rc = 2 746 end 747 break 748 end 749 for z ulong 8192 750 put eb z 0 751 end 752 ret rc 753 end 754 755 note agmemload: --memory FILE (read before chdir) into gmem, at most 65535 bytes, UTF-8 repaired 756 fn agmemload to int 757 if eq get gsln IMEM 0 758 ret 0 759 end 760 pb = new uchar 4096 0 761 pn = agsget IMEM pb 762 if eq pn 0 763 agerr emem 764 ret 8 765 end 766 fd = fopen pb 0 pn 524288 0 767 if lt fd 0 768 agerr emem 769 ret 8 770 end 771 got = ulong 0 772 while lt got 65535 bound 999999 773 r = read int fd gres got sub 65535 got 774 if lt r 0 775 agerr emem 776 ret 8 777 end 778 if eq r 0 779 break 780 end 781 got = add got ulong r 782 end 783 r2 = fclose int fd 784 put gnum QRES got 785 agu8fix 786 for i ulong got 787 put gmem i get gres i 788 end 789 put gnum QMEML got 790 put gnum QRES 0 791 ret 0 792 end 793 794 note ======== conversation history: hb holds the message objects joined by commas; gmb/gmr 795 note record each object's offset and role (1 user, 2 assistant, 3 tool, 4 system, 5 assistant 796 note with tool_calls). The session file holds the same objects, one per line. 797 798 fn agkey src view uchar 262144 tp view uint 65536 obj ulong key view uchar * to ulong 799 if ne js_type tp obj jsobj 800 ret jsnone 801 end 802 k = js_first tp obj 803 while ne k jsnone bound 999999 804 if js_streq src js_off tp k key 805 ret add k 1 806 end 807 k = js_mnext tp k 808 end 809 ret jsnone 810 end 811 812 fn agrole src view uchar 262144 tp view uint 65536 to ulong 813 if ne js_type tp 0 jsobj 814 ret 0 815 end 816 r = agkey src tp 0 crole 817 if eq r jsnone 818 ret 0 819 end 820 if ne js_type tp r jsstr 821 ret 0 822 end 823 o = js_off tp r 824 if js_streq src o cuser 825 ret 1 826 end 827 if js_streq src o cassist 828 if ne agkey src tp 0 ctoolc jsnone 829 ret 5 830 end 831 ret 2 832 end 833 if js_streq src o ctool 834 ret 3 835 end 836 if js_streq src o csystem 837 ret 4 838 end 839 ret 0 840 end 841 842 note agfront: drop the records before index k (k may equal the count: drop all) 843 fn agfront hb edit uchar `1048576` k ulong to void 844 n = get gnum QNREC 845 h = get gnum QHBL 846 if eq k 0 847 ret 848 end 849 if ge k n 850 put gnum QNREC 0 851 put gnum QHBL 0 852 ret 853 end 854 off = get gmb k 855 hmove hb 0 off sub h off 856 for j ulong sub n k 857 put gmb j sub get gmb add j k off 858 put gmr j get gmr add j k 859 end 860 put gnum QNREC sub n k 861 put gnum QHBL sub h off 862 end 863 864 note agtrim: an interrupted turn (history ending in user, tool, or assistant with tool_calls) 865 note is dropped back to before its user message 866 fn agtrim to void 867 n = get gnum QNREC 868 if eq n 0 869 ret 870 end 871 last = get gmr sub n 1 872 if eq last 2 873 ret 874 end 875 u = n 876 for i ulong n 877 j = sub sub n 1 i 878 if eq get gmr j 1 879 u = j 880 break 881 end 882 end 883 if eq u n 884 put gnum QNREC 0 885 put gnum QHBL 0 886 ret 887 end 888 put gnum QNREC u 889 if eq u 0 890 put gnum QHBL 0 891 else 892 put gnum QHBL sub get gmb u 1 893 end 894 end 895 896 note agcompact: before a turn, keep the newest whole turns within 512 KiB and 4096 records 897 fn agcompact hb edit uchar `1048576` to void 898 n = get gnum QNREC 899 h = get gnum QHBL 900 if and le h 524288 le n 4096 901 ret 902 end 903 k = n 904 for i ulong n 905 if eq get gmr i 1 906 if le sub h get gmb i 524288 907 if le sub n i 4096 908 k = i 909 break 910 end 911 end 912 end 913 end 914 agfront hb k 915 end 916 917 fn agappend wb edit uchar 263184 hb edit uchar `1048576` role ulong to int 918 r = js_wdone wb 919 if lt r 0 920 agerr emsg 921 ret 9 922 end 923 m = ulong r 924 if gt m QMSG 925 agerr emsg 926 ret 9 927 end 928 h = get gnum QHBL 929 k = get gnum QNREC 930 if or gt add add h m 1 `1048576` ge k 8192 931 agerr efull 932 ret 9 933 end 934 if gt h 0 935 put hb h 44 936 h = add h 1 937 end 938 for i ulong m 939 put hb add h i get wb add 1040 i 940 end 941 put gmb k h 942 put gmr k uchar role 943 put gnum QNREC add k 1 944 put gnum QHBL add h m 945 if eq get gnum QSESS 1 946 fd = int get gnum QSFD 947 ok = agwr fd wb 1040 m 948 ok2 = agwr fd cnl 0 1 949 if not and ok ok2 950 agerr esave 951 ret 8 952 end 953 end 954 ret 0 955 end 956 957 note agsread: open --session FILE (O_RDWR|O_CREAT|O_APPEND|O_CLOEXEC, mode 0600) and read its 958 note last 768 KiB into hb, starting at a line boundary; returns the byte count or -1 959 fn agsread hb edit uchar `1048576` to long 960 pb = new uchar 4096 0 961 st = new uchar 128 0 962 pn = agsget ISESS pb 963 fd = fopen pb 0 pn 525378 384 964 if lt fd 0 965 ret -1 966 end 967 put gnum QSFD ulong fd 968 put gnum QSESS 1 969 r = fstat int fd st 0 128 970 if lt r 0 971 ret -1 972 end 973 sz = ulong 0 974 for b ulong 8 975 sz = or shl sz 8 ulong get st sub 55 b 976 end 977 start = ulong 0 978 if gt sz 786432 979 start = sub sz 786432 980 end 981 got = ulong 0 982 while lt got sub sz start bound 999999 983 r = pread int fd hb got sub sub sz start got long add start got 984 if lt r 0 985 ret -1 986 end 987 if eq r 0 988 break 989 end 990 got = add got ulong r 991 end 992 if eq start 0 993 ret long got 994 end 995 p = ulong 0 996 while lt p got bound 999999 997 if eq get hb p 10 998 break 999 end 1000 p = add p 1 1001 end 1002 if ge p got 1003 ret 0 1004 end 1005 hmove hb 0 add p 1 sub got add p 1 1006 ret long sub got add p 1 1007 end 1008 1009 note agsrec: check one session line hb[p, p+ln) and append it at w; returns the role or 0 1010 fn agsrec hb edit uchar `1048576` p ulong ln ulong w ulong to ulong 1011 lb = new uchar 262144 0 1012 tp = new uint 65536 0 1013 if gt ln 262144 1014 ret 0 1015 end 1016 ri = js_init tp 262144 64 1017 for i ulong ln 1018 put lb i get hb add p i 1019 end 1020 pr = js_parse lb tp ln 1021 if lt pr 0 1022 ret 0 1023 end 1024 ro = agrole lb tp 1025 if ne ro 0 1026 hmove hb w p ln 1027 end 1028 ret ro 1029 end 1030 1031 note agsload: rebuild hb, gmb and gmr from the session file; every line must be a message object 1032 fn agsload hb edit uchar `1048576` to int 1033 if eq get gsln ISESS 0 1034 ret 0 1035 end 1036 got = agsread hb 1037 if lt got 0 1038 agerr esess 1039 ret 8 1040 end 1041 n = ulong got 1042 p = ulong 0 1043 w = ulong 0 1044 k = ulong 0 1045 while lt p n bound 999999 1046 e = p 1047 while lt e n bound 999999 1048 if eq get hb e 10 1049 break 1050 end 1051 e = add e 1 1052 end 1053 if gt e p 1054 if gt w 0 1055 put hb w 44 1056 w = add w 1 1057 end 1058 ro = ulong 0 1059 if lt k 8192 1060 ro = agsrec hb p sub e p w 1061 end 1062 if eq ro 0 1063 agerr esline 1064 ret 8 1065 end 1066 put gmb k w 1067 put gmr k uchar ro 1068 k = add k 1 1069 w = add w sub e p 1070 end 1071 p = add e 1 1072 end 1073 put gnum QNREC k 1074 put gnum QHBL w 1075 agtrim 1076 u = get gnum QNREC 1077 for i ulong u 1078 if eq get gmr i 1 1079 u = i 1080 break 1081 end 1082 end 1083 agfront hb u 1084 ret 0 1085 end 1086 1087 note ======== messages written with the JSON writer (wb: 1040 state bytes + 262144 text bytes) 1088 1089 fn agwstr wb edit uchar 263184 t view uchar * to void 1090 js_wsbuf wb t 0 len t 1091 end 1092 1093 fn agsysmsg wb edit uchar 263184 to long 1094 r = js_winit wb 1095 js_wobj wb 1096 agwstr wb crole 1097 agwstr wb csystem 1098 agwstr wb ccontent 1099 js_wsbeg wb 1100 for i ulong len csys 1101 js_wsb wb get csys i 1102 end 1103 ml = get gnum QMEML 1104 if gt ml 0 1105 for j ulong len cmemh 1106 js_wsb wb get cmemh j 1107 end 1108 for k ulong ml 1109 js_wsb wb get gmem k 1110 end 1111 end 1112 js_wsend wb 1113 js_wobje wb 1114 ret js_wdone wb 1115 end 1116 1117 fn agwuser wb edit uchar 263184 ub view uchar 65536 un ulong to void 1118 r = js_winit wb 1119 js_wobj wb 1120 agwstr wb crole 1121 agwstr wb cuser 1122 agwstr wb ccontent 1123 js_wsbuf wb ub 0 un 1124 js_wobje wb 1125 end 1126 1127 note agwg: write one JSON string from a global: 0 gtid, 1 gtnm, 2 gtarg, 3 gcont, 4 gres 1128 fn agwg wb edit uchar 263184 sel ulong off ulong n ulong to void 1129 js_wsbeg wb 1130 for i ulong n 1131 c = uchar 0 1132 if eq sel 0 1133 c = get gtid add off i 1134 end 1135 if eq sel 1 1136 c = get gtnm add off i 1137 end 1138 if eq sel 2 1139 c = get gtarg add off i 1140 end 1141 if eq sel 3 1142 c = get gcont add off i 1143 end 1144 if eq sel 4 1145 c = get gres add off i 1146 end 1147 js_wsb wb c 1148 end 1149 js_wsend wb 1150 end 1151 1152 fn agwcall wb edit uchar 263184 c ulong to void 1153 js_wobj wb 1154 agwstr wb cid 1155 agwg wb 0 mul c 256 get gst add SID c 1156 agwstr wb ctype 1157 agwstr wb cfunc 1158 agwstr wb cfunc 1159 js_wobj wb 1160 agwstr wb cname 1161 agwg wb 1 mul c 128 get gst add SNM c 1162 agwstr wb cargs 1163 agwg wb 2 mul c 65536 get gst add SARG c 1164 js_wobje wb 1165 js_wobje wb 1166 end 1167 1168 fn agwasst wb edit uchar 263184 to void 1169 r = js_winit wb 1170 js_wobj wb 1171 agwstr wb crole 1172 agwstr wb cassist 1173 agwstr wb ccontent 1174 nc = get gst SNC 1175 if and eq get gst SCLEN 0 gt nc 0 1176 js_wnull wb 1177 else 1178 agwg wb 3 0 get gst SCLEN 1179 end 1180 if gt nc 0 1181 agwstr wb ctoolc 1182 js_warr wb 1183 for c ulong nc 1184 agwcall wb c 1185 end 1186 js_warre wb 1187 end 1188 js_wobje wb 1189 end 1190 1191 fn agwtool wb edit uchar 263184 c ulong to void 1192 r = js_winit wb 1193 js_wobj wb 1194 agwstr wb crole 1195 agwstr wb ctool 1196 agwstr wb ctcid 1197 agwg wb 0 mul c 256 get gst add SID c 1198 agwstr wb ccontent 1199 agwg wb 4 0 get gnum QRES 1200 js_wobje wb 1201 end 1202 1203 note agfixids: a call without an id gets call_N, so its tool message can name it 1204 fn agfixids to void 1205 for c ulong get gst SNC 1206 if eq get gst add SID c 0 1207 for i ulong len ccall 1208 put gtid add mul c 256 i get ccall i 1209 end 1210 put gtid add mul c 256 len ccall uchar add 48 c 1211 put gst add SID c add len ccall 1 1212 end 1213 end 1214 end 1215 1216 note ======== model response: SSE events or one JSON body; both carry choices[0].delta|message 1217 1218 note agapp: append dc[0, n) to field sel (0 id, 1 name, 2 arguments) of tool call ix 1219 fn agapp sel ulong ix ulong dc view uchar 262144 n ulong to bool 1220 slot = add SID ix 1221 cap = ulong 255 1222 if eq sel 1 1223 slot = add SNM ix 1224 cap = 127 1225 end 1226 if eq sel 2 1227 slot = add SARG ix 1228 cap = 65535 1229 end 1230 l = get gst slot 1231 if gt n sub cap l 1232 ret false 1233 end 1234 b = add mul ix add cap 1 l 1235 if eq sel 0 1236 for i ulong n 1237 put gtid add b i get dc i 1238 end 1239 end 1240 if eq sel 1 1241 for j ulong n 1242 put gtnm add b j get dc j 1243 end 1244 end 1245 if eq sel 2 1246 for k ulong n 1247 put gtarg add b k get dc k 1248 end 1249 end 1250 put gst slot add l n 1251 ret true 1252 end 1253 1254 note agtcstr: field sel of call ix from value v (absent: nothing; a string: decoded and appended; 1255 note an object as arguments: its raw JSON text appended) 1256 fn agtcstr src view uchar 262144 tp view uint 65536 v ulong sel ulong ix ulong dc edit uchar 262144 to int 1257 if eq v jsnone 1258 ret 0 1259 end 1260 t = js_type tp v 1261 if eq t jsnull 1262 ret 0 1263 end 1264 if eq t jsstr 1265 n = js_sget src js_off tp v dc 1266 if lt n 0 1267 agerr ebad 1268 ret 6 1269 end 1270 if not agapp sel ix dc ulong n 1271 agerr elong 1272 ret 6 1273 end 1274 ret 0 1275 end 1276 if and eq sel 2 eq t jsobj 1277 o = js_off tp v 1278 sp = js_span tp v 1279 for i ulong sp 1280 put dc i get src add o i 1281 end 1282 if not agapp sel ix dc sp 1283 agerr elong 1284 ret 6 1285 end 1286 ret 0 1287 end 1288 agerr ebad 1289 ret 6 1290 end 1291 1292 fn agtcix src view uchar 262144 tp view uint 65536 e ulong pos ulong to ulong 1293 xi = agkey src tp e cindex 1294 if eq xi jsnone 1295 ret pos 1296 end 1297 if ne js_type tp xi jsnum 1298 ret QMAXC 1299 end 1300 if not js_isu src js_off tp xi 1301 ret QMAXC 1302 end 1303 ret js_tou src js_off tp xi 1304 end 1305 1306 fn agtcdelta src view uchar 262144 tp view uint 65536 e ulong pos ulong dc edit uchar 262144 to int 1307 if ne js_type tp e jsobj 1308 agerr ebad 1309 ret 6 1310 end 1311 ix = agtcix src tp e pos 1312 if ge ix QMAXC 1313 agerr emany 1314 ret 6 1315 end 1316 if ge ix get gst SNC 1317 put gst SNC add ix 1 1318 end 1319 id = agkey src tp e cid 1320 if ne id jsnone 1321 put gst add SID ix 0 1322 end 1323 rc = agtcstr src tp id 0 ix dc 1324 if ne rc 0 1325 ret rc 1326 end 1327 fu = agkey src tp e cfunc 1328 if eq fu jsnone 1329 ret 0 1330 end 1331 if ne js_type tp fu jsobj 1332 agerr ebad 1333 ret 6 1334 end 1335 rc = agtcstr src tp agkey src tp fu cname 1 ix dc 1336 if ne rc 0 1337 ret rc 1338 end 1339 ret agtcstr src tp agkey src tp fu cargs 2 ix dc 1340 end 1341 1342 fn agdelta src view uchar 262144 tp view uint 65536 d ulong dc edit uchar 262144 to int 1343 if ne js_type tp d jsobj 1344 agerr ebad 1345 ret 6 1346 end 1347 c = agkey src tp d ccontent 1348 if and ne c jsnone eq js_type tp c jsstr 1349 n = js_sget src js_off tp c dc 1350 if lt n 0 1351 agerr ebad 1352 ret 6 1353 end 1354 l = get gst SCLEN 1355 nn = ulong n 1356 if gt nn sub QCONC l 1357 agerr econt 1358 ret 6 1359 end 1360 for i ulong nn 1361 put gcont add l i get dc i 1362 end 1363 put gst SCLEN add l nn 1364 ok = agwr 1 dc 0 nn 1365 end 1366 tc = agkey src tp d ctoolc 1367 if eq tc jsnone 1368 ret 0 1369 end 1370 if eq js_type tp tc jsnull 1371 ret 0 1372 end 1373 if ne js_type tp tc jsarr 1374 agerr ebad 1375 ret 6 1376 end 1377 e = js_first tp tc 1378 pos = ulong 0 1379 while ne e jsnone bound 999999 1380 rc = agtcdelta src tp e pos dc 1381 if ne rc 0 1382 ret rc 1383 end 1384 e = js_enext tp e 1385 pos = add pos 1 1386 end 1387 ret 0 1388 end 1389 1390 note agchunk: one parsed JSON object (an SSE event or a whole body) of n bytes in src 1391 fn agchunk src view uchar 262144 tp edit uint 65536 n ulong dc edit uchar 262144 to int 1392 pr = js_parse src tp n 1393 if lt pr 0 1394 agerr eevj 1395 ret 6 1396 end 1397 if ne js_type tp 0 jsobj 1398 agerr eevj 1399 ret 6 1400 end 1401 if ne agkey src tp 0 cerror jsnone 1402 agerr eserr 1403 ret 6 1404 end 1405 ch = agkey src tp 0 cchoices 1406 if eq ch jsnone 1407 ret 0 1408 end 1409 if ne js_type tp ch jsarr 1410 agerr ebad 1411 ret 6 1412 end 1413 c0 = js_first tp ch 1414 if eq c0 jsnone 1415 ret 0 1416 end 1417 if ne js_type tp c0 jsobj 1418 agerr ebad 1419 ret 6 1420 end 1421 fr = agkey src tp c0 cfinish 1422 if ne fr jsnone 1423 if eq js_type tp fr jsstr 1424 put gst SFIN 1 1425 end 1426 end 1427 d = agkey src tp c0 cdelta 1428 if eq d jsnone 1429 d = agkey src tp c0 cmessage 1430 end 1431 if eq d jsnone 1432 ret 0 1433 end 1434 ret agdelta src tp d dc 1435 end 1436 1437 fn agrderr n long to int 1438 if eq n HETIME 1439 agerr etmo 1440 ret 5 1441 end 1442 if eq n HEBIG 1443 agerr ebig 1444 ret 6 1445 end 1446 agerr eread 1447 ret 5 1448 end 1449 1450 note agevent: one ready SSE event in lb; 1 at [DONE], 0 to go on, else an exit code 1451 fn agevent ss edit ulong 16 lb view uchar 262400 eb edit uchar 262144 tp edit uint 65536 dc edit uchar 262144 to int 1452 dl = get ss HSDLEN 1453 hssenext ss 1454 if eq dl 6 1455 if heq view part lb 128 6 cdone 1456 ret 1 1457 end 1458 end 1459 if gt dl 262144 1460 agerr ebig 1461 ret 6 1462 end 1463 if eq dl 0 1464 ret 0 1465 end 1466 for i ulong dl 1467 put eb i get lb add 128 i 1468 end 1469 ret agchunk eb tp dl dc 1470 end 1471 1472 note agfeed: feed src[0, gst 40) to the SSE parser; 1 at [DONE], 0 to read on, else an exit code 1473 fn agfeed ss edit ulong 16 lb edit uchar 262400 src view uchar 4096 eb edit uchar 262144 tp edit uint 65536 dc edit uchar 262144 to int 1474 off = ulong 0 1475 while lt off get gst 40 bound 999999 1476 c = hssefeed ss lb view part src off sub get gst 40 off 1477 if lt c 0 1478 agerr ebad 1479 ret 6 1480 end 1481 off = add off ulong c 1482 if eq get ss HSRDY 0 1483 if eq c 0 1484 agerr ebad 1485 ret 6 1486 end 1487 continue 1488 end 1489 rc = agevent ss lb eb tp dc 1490 if ne rc 0 1491 ret rc 1492 end 1493 end 1494 ret 0 1495 end 1496 1497 note agsse: read the SSE body until [DONE] (or the end of the body after a finish_reason) 1498 fn agsse cs edit ulong 320 io edit uchar 131072 eb edit uchar 262144 tp edit uint 65536 dc edit uchar 262144 to int 1499 ss = new ulong 16 0 1500 lb = new uchar 262400 0 1501 src = new uchar 4096 0 1502 r = hsseinit ss 1503 for it ulong `16777216` 1504 n = hbodyrd cs io src 1505 if lt n 0 1506 ret agrderr n 1507 end 1508 if eq n 0 1509 break 1510 end 1511 put gst 40 ulong n 1512 rc = agfeed ss lb src eb tp dc 1513 if eq rc 1 1514 ret 0 1515 end 1516 if ne rc 0 1517 ret rc 1518 end 1519 end 1520 if eq get gst SFIN 0 1521 agerr enodone 1522 ret 6 1523 end 1524 ret 0 1525 end 1526 1527 fn agjsonbody cs edit ulong 320 io edit uchar 131072 eb edit uchar 262144 tp edit uint 65536 dc edit uchar 262144 to int 1528 n = hbodyall cs io eb 1529 if lt n 0 1530 ret agrderr n 1531 end 1532 ret agchunk eb tp ulong n dc 1533 end 1534 1535 fn agresp cs edit ulong 320 io edit uchar 131072 sse bool to int 1536 eb = new uchar 262144 0 1537 tp = new uint 65536 0 1538 dc = new uchar 262144 0 1539 ri = js_init tp 262144 64 1540 if sse 1541 ret agsse cs io eb tp dc 1542 end 1543 ret agjsonbody cs io eb tp dc 1544 end 1545 1546 fn agissse cs edit ulong 320 io edit uchar 131072 to bool 1547 i = hhfind cs io cnctype 1548 if lt i 0 1549 ret false 1550 end 1551 vo = hhvoff cs ulong i 1552 vl = hhvlen cs ulong i 1553 m = len cevs 1554 if lt vl m 1555 ret false 1556 end 1557 for s ulong add sub vl m 1 1558 if heq view part io add vo s m cevs 1559 ret true 1560 end 1561 end 1562 ret false 1563 end 1564 1565 fn agstatus cs edit ulong 320 io edit uchar 131072 st long to int 1566 agerrn estat ulong st 1567 b = new uchar 512 0 1568 n = hbodyrd cs io b 1569 if gt n 0 1570 for i ulong ulong n 1571 c = get b i 1572 if or lt c 32 gt c 126 1573 put b i 32 1574 end 1575 end 1576 ok = agwr 2 cpre 0 len cpre 1577 ok = agwr 2 b 0 ulong n 1578 ok = agwr 2 cnl 0 1 1579 end 1580 r = hclose cs 1581 ret 4 1582 end 1583 1584 note agconn: connect to ip:port (5 s connect limit) and set idle and body deadlines t (ns) 1585 fn agconn cs edit ulong 320 io edit uchar 131072 ip ulong port ulong t long to long 1586 a = new uchar 16 0 1587 r = haddr4 a uint ip port 1588 fd = hdial a `5000000000` 1589 if lt fd 0 1590 ret fd 1591 end 1592 r = hinit cs io int fd 0 1593 hsettmo cs t t 1594 hsetbdl cs t 1595 ret 0 1596 end 1597 1598 note agauth: the Authorization header, built in a local that is zeroed before return 1599 fn agauth cs edit ulong 320 io edit uchar 131072 r long to long 1600 if eq get gnum QHAVEK 0 1601 ret r 1602 end 1603 kb = new uchar 4200 0 1604 for i ulong len cbearer 1605 put kb i get cbearer i 1606 end 1607 ko = get gsof IKEY 1608 kl = get gsln IKEY 1609 for j ulong kl 1610 put kb add len cbearer j get gpool add ko j 1611 end 1612 x = hfe r hhname cs io cauth 1613 x = hfe x hhval cs io view part kb 0 add len cbearer kl 1614 for z ulong 4200 1615 put kb z 0 1616 end 1617 ret x 1618 end 1619 1620 fn aghead cs edit ulong 320 io edit uchar 131072 sn ulong to long 1621 pb = new uchar 8192 0 1622 hh = new uchar 256 0 1623 pn = agsget IPATH pb 1624 hl = agsget IHOST hh 1625 r = hreqst cs io cpost 1626 r = hfe r hreqtg cs io view part pb 0 pn 1627 r = hfe r hhname cs io chost 1628 r = hfe r hhval cs io view part hh 0 hl 1629 r = hfe r hhname cs io cctype 1630 r = hfe r hhval cs io cjson 1631 r = hfe r hhname cs io caccept 1632 r = hfe r hhval cs io cevst 1633 r = agauth cs io r 1634 c = add add len cbody1 get gsln IMODEL add len cbody2 len ctools 1635 c = add c add add len cbody3 sn add 1 len cbody4 1636 c = add c get gnum QHBL 1637 ret hfe r hhend cs io 0 c 1638 end 1639 1640 fn agbody cs edit ulong 320 io edit uchar 131072 hb view uchar `1048576` wb view uchar 263184 sn ulong r long to long 1641 mb = new uchar 256 0 1642 mn = agsget IMODEL mb 1643 r = hfe r hput cs io cbody1 1644 r = hfe r hput cs io view part mb 0 mn 1645 r = hfe r hput cs io cbody2 1646 r = hfe r hput cs io ctools 1647 r = hfe r hput cs io cbody3 1648 r = hfe r hput cs io view part wb 1040 sn 1649 r = hfe r hput cs io ccomma 1650 r = hfe r hput cs io view part hb 0 get gnum QHBL 1651 r = hfe r hput cs io cbody4 1652 ret hfe r hwfin cs io 1653 end 1654 1655 note agreq: one chat-completions request (stream: true) over a fresh connection 1656 fn agreq cs edit ulong 320 io edit uchar 131072 hb edit uchar `1048576` wb edit uchar 263184 to int 1657 sr = agsysmsg wb 1658 if lt sr 0 1659 agerr emsg 1660 ret 9 1661 end 1662 r = agconn cs io get gnum QIP get gnum QPORT long get gnum QTMO 1663 if lt r 0 1664 agerr econn 1665 ret 3 1666 end 1667 r = aghead cs io ulong sr 1668 r = agbody cs io hb wb ulong sr r 1669 if lt r 0 1670 x = hclose cs 1671 if eq r HETIME 1672 agerr etmo 1673 ret 5 1674 end 1675 agerr ewrite 1676 ret 3 1677 end 1678 st = hrsprd cs io 1679 if lt st 0 1680 x = hclose cs 1681 ret agrderr st 1682 end 1683 if ne st 200 1684 ret agstatus cs io st 1685 end 1686 rc = agresp cs io agissse cs io 1687 x = hclose cs 1688 ret rc 1689 end 1690 1691 note ======== tools 1692 1693 fn agargstr src view uchar 262144 tp view uint 65536 key view uchar * d edit uchar * to long 1694 v = agkey src tp 0 key 1695 if eq v jsnone 1696 ret -1 1697 end 1698 if ne js_type tp v jsstr 1699 ret -1 1700 end 1701 n = js_sget src js_off tp v d 1702 if lt n 0 1703 ret -2 1704 end 1705 ret n 1706 end 1707 1708 note agpathok and agcomp, the workspace path rule, are in seed/agent/path.om (composed before this file). 1709 1710 fn agtread ab view uchar 262144 tp view uint 65536 to void 1711 pb = new uchar 4096 0 1712 one = new uchar 1 0 1713 n = agargstr ab tp cpath pb 1714 if lt n 0 1715 agrset 2 rpatharg 1716 ret 1717 end 1718 pn = ulong n 1719 if not agpathok view part pb 0 pn 1720 agrset 1 rpath 1721 ret 1722 end 1723 fd = fopen pb 0 pn 557056 0 1724 if lt fd 0 1725 agrerr ropen fd 1726 ret 1727 end 1728 put gnum QTST 0 1729 got = ulong 0 1730 while lt got QRESC bound 999999 1731 r = read int fd gres got sub QRESC got 1732 if lt r 0 1733 r2 = fclose int fd 1734 agrerr rreadf r 1735 ret 1736 end 1737 if eq r 0 1738 break 1739 end 1740 got = add got ulong r 1741 end 1742 put gnum QRES got 1743 if eq got QRESC 1744 r3 = read int fd one 0 1 1745 if gt r3 0 1746 agradd rtrunc 1747 end 1748 end 1749 r4 = fclose int fd 1750 end 1751 1752 fn agtwrite ab view uchar 262144 tp view uint 65536 to void 1753 if eq get gnum QWRITE 0 1754 agrset 1 rnowrite 1755 ret 1756 end 1757 pb = new uchar 4096 0 1758 cb = new uchar 262144 0 1759 n = agargstr ab tp cpath pb 1760 if lt n 0 1761 agrset 2 rpatharg 1762 ret 1763 end 1764 pn = ulong n 1765 if not agpathok view part pb 0 pn 1766 agrset 1 rpath 1767 ret 1768 end 1769 cn = agargstr ab tp ccontent cb 1770 if lt cn 0 1771 agrset 2 rcontarg 1772 ret 1773 end 1774 fd = fopen pb 0 pn 557633 420 1775 if lt fd 0 1776 agrerr ropen fd 1777 ret 1778 end 1779 p = ulong 0 1780 m = ulong cn 1781 while lt p m bound 999999 1782 w = write int fd cb p sub m p 1783 if le w 0 1784 r = fclose int fd 1785 agrerr rwfail w 1786 ret 1787 end 1788 p = add p ulong w 1789 end 1790 r2 = fclose int fd 1791 agrset 0 rwrote 1792 agrnum m 1793 agradd rbytes 1794 agradd view part pb 0 pn 1795 end 1796 1797 fn agallowed base ulong cnt ulong a view uchar * to bool 1798 for k ulong cnt 1799 if agseq add base k a 1800 ret true 1801 end 1802 end 1803 ret false 1804 end 1805 1806 note agspawn: fork and run the packed argv as the leader of a new process group, stdout and 1807 note stderr on a close-on-exec pipe and stdin on /dev/null. Both sides call setpgid, so the group 1808 note exists before either goes on (as in seed/node/auth.om au_fork). Returns the child pid (the 1809 note pipe's read end in gnum 22) or a negative errno 1810 fn agspawn pk view uchar 4096 pn ulong to long 1811 pf = new uchar 8 0 1812 r = pipefds pf 0 8 524288 1813 if lt r 0 1814 ret r 1815 end 1816 rfd = int or ulong get pf 0 shl ulong get pf 1 8 1817 wfd = int or ulong get pf 4 shl ulong get pf 5 8 1818 dn = fopen cdevnull 0 len cdevnull 524288 0 1819 pid = fork 1820 if eq pid 0 1821 r = setpgid 0 0 1822 r = dupfd wfd 1 0 1823 r = dupfd wfd 2 0 1824 if ge dn 0 1825 r = dupfd int dn 0 0 1826 end 1827 r = execprog pk 0 pn 1828 exit 127 1829 end 1830 r = fclose wfd 1831 if ge dn 0 1832 r = fclose int dn 1833 end 1834 if lt pid 0 1835 r = fclose rfd 1836 ret pid 1837 end 1838 r = setpgid int pid int pid 1839 put gnum 22 ulong rfd 1840 ret pid 1841 end 1842 1843 note agreap: reap the direct child without blocking; once it is gone (status in gnum 24, gnum 23 1844 note = 1) SIGKILL the rest of its process group, so a grandchild cannot outlive the tool call 1845 fn agreap pid int to void 1846 ws = new uchar 4 0 1847 if ne get gnum 23 0 1848 ret 1849 end 1850 g = waitpid pid ws 0 4 1 1851 if eq g long pid 1852 put gnum 23 1 1853 put gnum 24 or ulong get ws 0 shl ulong get ws 1 8 1854 r = killpid neg pid 9 1855 end 1856 end 1857 1858 note agslice: the next wait in ns, the time left to deadline dl at most 50 ms 1859 fn agslice dl ulong now ulong to long 1860 if gt sub dl now `50000000` 1861 ret `50000000` 1862 end 1863 ret long sub dl now 1864 end 1865 1866 note agwaitc: end of file only means every holder closed the pipe, not that the child exited 1867 note (coreutils closes stdout before it exits). Reap the direct child first (agreap), polling every 1868 note 2 ms until deadline dl, so the group's SIGKILL cannot overtake a child that already finished; 1869 note true when the deadline passed first 1870 fn agwaitc dl ulong to bool 1871 for it ulong `999999999` 1872 agreap int get gnum 25 1873 if ne get gnum 23 0 1874 ret false 1875 end 1876 now = ulong monons 1877 if ge now dl 1878 ret true 1879 end 1880 r = sleepns `2000000` 1881 end 1882 ret true 1883 end 1884 1885 note agdrain: read the child's output into gres (at most 64 KiB kept) until end of file or the 1886 note --cmd-timeout deadline; true on timeout. It waits in 50 ms slices and calls agreap between 1887 note them (child pid in gnum 25), so after the child exits its group dies and end of file arrives; 1888 note at end of file it waits for the child itself (agwaitc) 1889 fn agdrain rfd int to bool 1890 buf = new uchar 4096 0 1891 put gnum QRES 0 1892 put gnum 23 0 1893 dl = add ulong monons get gnum QCTMO 1894 for it ulong 999999 1895 now = ulong monons 1896 if ge now dl 1897 ret true 1898 end 1899 wv = waitio rfd 1 agslice dl now 1900 if eq wv 0 1901 agreap int get gnum 25 1902 continue 1903 end 1904 n = read rfd buf 0 4096 1905 if eq n 0 1906 ret agwaitc dl 1907 end 1908 if lt n 0 1909 if or eq n -4 eq n -11 1910 continue 1911 end 1912 ret false 1913 end 1914 for i ulong ulong n 1915 if lt get gnum QRES QRESC 1916 put gres get gnum QRES get buf i 1917 put gnum QRES add get gnum QRES 1 1918 end 1919 end 1920 end 1921 ret true 1922 end 1923 1924 note agexec: run the packed argv, collect its output and reap it. At the --cmd-timeout deadline 1925 note the child's whole process group gets SIGKILL; when the child exits first, agdrain kills the 1926 note group's remaining members, so no tool grandchild outlives the call either way 1927 fn agexec pk view uchar 4096 pn ulong to void 1928 ws = new uchar 4 0 1929 pid = agspawn pk pn 1930 if lt pid 0 1931 agrset 2 rpipe 1932 ret 1933 end 1934 rfd = int get gnum 22 1935 put gnum QTST 0 1936 put gnum 25 ulong pid 1937 tmo = agdrain rfd 1938 r = killpid neg int pid 9 1939 if eq get gnum 23 0 1940 r = waitpid int pid ws 0 4 0 1941 put gnum 24 or ulong get ws 0 shl ulong get ws 1 8 1942 end 1943 r = fclose rfd 1944 if ge get gnum QRES QRESC 1945 agradd rtrunc 1946 end 1947 if tmo 1948 put gnum QTST 2 1949 agradd rtmo 1950 agrnum div get gnum QCTMO `1000000000` 1951 agradd rsec 1952 ret 1953 end 1954 wst = get gnum 24 1955 if ne and wst 127 0 1956 put gnum QTST 2 1957 agradd rsig 1958 agrnum and wst 127 1959 agradd rclose 1960 ret 1961 end 1962 agradd rexit 1963 agrnum and shr wst 8 255 1964 agradd rclose 1965 end 1966 1967 fn agtrun ab view uchar 262144 tp view uint 65536 to void 1968 nc = get gnum QNCMD 1969 if eq nc 0 1970 agrset 1 rnocmd 1971 ret 1972 end 1973 av = agkey ab tp 0 cargv 1974 if eq av jsnone 1975 agrset 2 rargv 1976 ret 1977 end 1978 if ne js_type tp av jsarr 1979 agrset 2 rargv 1980 ret 1981 end 1982 nel = js_nkids tp av 1983 if or eq nel 0 gt nel 63 1984 agrset 2 rargv 1985 ret 1986 end 1987 pk = new uchar 4096 0 1988 e1 = new uchar 4096 0 1989 w = ulong 2 1990 e = js_first tp av 1991 for k ulong nel 1992 if ne js_type tp e jsstr 1993 agrset 2 rargv 1994 ret 1995 end 1996 n = js_sget ab js_off tp e e1 1997 if lt n 0 1998 agrset 1 rpack 1999 ret 2000 end 2001 w = agpack pk w e1 ulong n k 2002 if eq w 0 2003 ret 2004 end 2005 e = js_enext tp e 2006 end 2007 put pk 0 uchar nel 2008 put pk 1 0 2009 agexec pk w 2010 end 2011 2012 note agpack: append argument k (e1[0, m)) to the packed list at w; the new end, or 0 with the 2013 note refusal already in gres 2014 fn agpack pk edit uchar 4096 w ulong e1 view uchar 4096 m ulong k ulong to ulong 2015 for z ulong m 2016 if eq get e1 z 0 2017 agrset 1 rnul 2018 ret 0 2019 end 2020 end 2021 if eq k 0 2022 if or eq m 0 not agallowed ICMD get gnum QNCMD view part e1 0 m 2023 agrset 1 rnotal 2024 agradd view part e1 0 m 2025 ret 0 2026 end 2027 end 2028 if gt add add w 2 m 4096 2029 agrset 1 rpack 2030 ret 0 2031 end 2032 put pk w uchar and m 255 2033 put pk add w 1 uchar shr m 8 2034 b = add w 2 2035 for j ulong m 2036 put pk add b j get e1 j 2037 end 2038 ret add b m 2039 end 2040 2041 fn agtfetch ab view uchar 262144 tp view uint 65536 to void 2042 nf = get gnum QNFET 2043 if eq nf 0 2044 agrset 1 rnofet 2045 ret 2046 end 2047 ub = new uchar 4096 0 2048 n = agargstr ab tp curl ub 2049 if lt n 0 2050 agrset 2 rurlarg 2051 ret 2052 end 2053 un = ulong n 2054 r = new ulong 8 0 2055 rc = agurl view part ub 0 un r 2056 if eq rc -1 2057 agrset 1 rnohttp 2058 ret 2059 end 2060 if lt rc 0 2061 agrset 2 rurlbad 2062 ret 2063 end 2064 cn = new uchar 300 0 2065 cl = agcanon ub r cn 2066 if not agallowed IFET nf view part cn 0 cl 2067 agrset 1 rnotfa 2068 agradd view part cn 0 cl 2069 ret 2070 end 2071 agfetch1 ub r 2072 end 2073 2074 note agcanon: HOST:PORT of a parsed url (the port always written), for the --allow-fetch check 2075 fn agcanon ub view uchar 4096 r view ulong 8 cn edit uchar 300 to ulong 2076 d = new uchar 24 0 2077 hl = get r 6 2078 if gt hl 255 2079 hl = 255 2080 end 2081 b = get r 2 2082 for i ulong hl 2083 put cn i get ub add b i 2084 end 2085 put cn hl 58 2086 dn = agdec get r 1 d 2087 b = add hl 1 2088 for j ulong dn 2089 put cn add b j get d j 2090 end 2091 ret add b dn 2092 end 2093 2094 note agfreq: send GET for the parsed url and read the status line and headers 2095 fn agfreq cs edit ulong 320 io edit uchar 131072 ub view uchar 4096 r view ulong 8 to long 2096 x = agconn cs io get r 0 get r 1 long get gnum QCTMO 2097 if lt x 0 2098 ret -1000 2099 end 2100 x = hreqst cs io cget 2101 if eq get r 5 0 2102 x = hfe x hreqtg cs io cslash 2103 else 2104 x = hfe x hreqtg cs io view part ub get r 4 get r 5 2105 end 2106 x = hfe x hhname cs io chost 2107 x = hfe x hhval cs io view part ub get r 2 get r 3 2108 x = hfe x hhend cs io 0 0 2109 x = hfe x hwfin cs io 2110 if lt x 0 2111 ret x 2112 end 2113 ret hrsprd cs io 2114 end 2115 2116 note agfbody: append the response body to gres (64 KiB kept) 2117 fn agfbody cs edit ulong 320 io edit uchar 131072 to void 2118 b = new uchar 4096 0 2119 for it ulong 999999 2120 n = hbodyrd cs io b 2121 if le n 0 2122 if eq n HETIME 2123 put gnum QTST 2 2124 agradd rftmo 2125 end 2126 ret 2127 end 2128 for i ulong ulong n 2129 if lt get gnum QRES QRESC 2130 put gres get gnum QRES get b i 2131 put gnum QRES add get gnum QRES 1 2132 end 2133 end 2134 if ge get gnum QRES QRESC 2135 agradd rtrunc 2136 ret 2137 end 2138 end 2139 end 2140 2141 fn agfetch1 ub view uchar 4096 r view ulong 8 to void 2142 cs = new ulong 320 0 2143 io = new uchar 131072 0 2144 st = agfreq cs io ub r 2145 if eq st -1000 2146 agrset 2 rconn 2147 ret 2148 end 2149 if lt st 0 2150 y = hclose cs 2151 if eq st HETIME 2152 agrset 2 rftmo 2153 ret 2154 end 2155 agrset 2 rfreq 2156 ret 2157 end 2158 agrset 0 rstatus 2159 agrnum ulong st 2160 agradd cnl 2161 agfbody cs io 2162 y = hclose cs 2163 end 2164 2165 fn agtoolid nm view uchar * to ulong 2166 if heq nm tnread 2167 ret 1 2168 end 2169 if heq nm tnwrite 2170 ret 2 2171 end 2172 if heq nm tnrun 2173 ret 3 2174 end 2175 if heq nm tnfetch 2176 ret 4 2177 end 2178 ret 0 2179 end 2180 2181 note agcall: run tool call c; its result is left in gres/QRES (valid UTF-8), its status in QTST. 2182 note stderr names the tool and the status only, never arguments or results. 2183 fn agcall c ulong to void 2184 ab = new uchar 262144 0 2185 tp = new uint 65536 0 2186 nm = new uchar 128 0 2187 ri = js_init tp 262144 64 2188 nl = get gst add SNM c 2189 for j ulong nl 2190 put nm j get gtnm add mul c 128 j 2191 end 2192 an = get gst add SARG c 2193 for i ulong an 2194 put ab i get gtarg add mul c 65536 i 2195 end 2196 put gnum QRES 0 2197 which = agtoolid view part nm 0 nl 2198 if eq which 0 2199 agrset 1 runknown 2200 agradd view part nm 0 nl 2201 else 2202 pr = js_parse ab tp an 2203 if or lt pr 0 ne js_type tp 0 jsobj 2204 agrset 2 rnoargs 2205 else 2206 if eq which 1 2207 agtread ab tp 2208 end 2209 if eq which 2 2210 agtwrite ab tp 2211 end 2212 if eq which 3 2213 agtrun ab tp 2214 end 2215 if eq which 4 2216 agtfetch ab tp 2217 end 2218 end 2219 end 2220 agu8fix 2221 ok = agwr 2 cpre 0 len cpre 2222 ok = agwr 2 ltool 0 len ltool 2223 ok = agwr 2 nm 0 nl 2224 s = get gnum QTST 2225 if eq s 0 2226 ok = agwr 2 lok 0 len lok 2227 end 2228 if eq s 1 2229 ok = agwr 2 lref 0 len lref 2230 end 2231 if eq s 2 2232 ok = agwr 2 lerr 0 len lerr 2233 end 2234 ok = agwr 2 cnl 0 1 2235 end 2236 2237 note ======== the agent loop 2238 2239 fn agturn ub view uchar 65536 un ulong hb edit uchar `1048576` wb edit uchar 263184 cs edit ulong 320 io edit uchar 131072 to int 2240 agtrim 2241 agcompact hb 2242 agwuser wb ub un 2243 rc = agappend wb hb 1 2244 if ne rc 0 2245 ret rc 2246 end 2247 mr = get gnum QROUND 2248 for k ulong mr 2249 for i ulong 64 2250 put gst i 0 2251 end 2252 rc = agreq cs io hb wb 2253 if ne rc 0 2254 if gt get gst SCLEN 0 2255 ok = agwr 1 cnl 0 1 2256 end 2257 ret rc 2258 end 2259 agfixids 2260 agwasst wb 2261 nc = get gst SNC 2262 role = ulong 2 2263 if gt nc 0 2264 role = 5 2265 end 2266 rc = agappend wb hb role 2267 if ne rc 0 2268 ret rc 2269 end 2270 if eq nc 0 2271 ok2 = agwr 1 cnl 0 1 2272 ret 0 2273 end 2274 if gt get gst SCLEN 0 2275 ok3 = agwr 1 cnl 0 1 2276 end 2277 for c ulong nc 2278 agcall c 2279 agwtool wb c 2280 rc = agappend wb hb 3 2281 if ne rc 0 2282 ret rc 2283 end 2284 end 2285 end 2286 agerr eround 2287 ret 7 2288 end 2289 2290 note agline: the next stdin line (CR LF or LF) into ub; -1 at the end of input, -2 too long 2291 fn agline ub edit uchar 65536 to long 2292 for it ulong 999999 2293 l = get gnum QINL 2294 p = get gnum QINP 2295 for i ulong sub l p 2296 if eq get gin add p i 10 2297 n = i 2298 if and gt n 0 eq get gin add p sub n 1 13 2299 n = sub n 1 2300 end 2301 for j ulong n 2302 put ub j get gin add p j 2303 end 2304 put gnum QINP add add p i 1 2305 ret long n 2306 end 2307 end 2308 if eq get gnum QINEOF 1 2309 if eq p l 2310 ret -1 2311 end 2312 n2 = sub l p 2313 for j2 ulong n2 2314 put ub j2 get gin add p j2 2315 end 2316 put gnum QINP l 2317 ret long n2 2318 end 2319 if gt p 0 2320 for m ulong sub l p 2321 put gin m get gin add p m 2322 end 2323 put gnum QINL sub l p 2324 put gnum QINP 0 2325 end 2326 l2 = get gnum QINL 2327 if ge l2 65535 2328 ret -2 2329 end 2330 r = read 0 gin l2 sub 65535 l2 2331 if le r 0 2332 put gnum QINEOF 1 2333 else 2334 put gnum QINL add l2 ulong r 2335 end 2336 end 2337 ret -1 2338 end 2339 2340 fn agchdir to int 2341 if eq get gsln IWORK 0 2342 ret 0 2343 end 2344 pb = new uchar 4096 0 2345 pn = agsget IWORK pb 2346 c = chdir pb 0 pn 2347 if lt c 0 2348 agerr ework 2349 ret 2 2350 end 2351 ret 0 2352 end 2353 2354 fn main to int 2355 cs = new ulong 320 0 2356 io = new uchar 131072 0 2357 hb = new uchar `1048576` 0 2358 wb = new uchar 263184 0 2359 ub = new uchar 65536 0 2360 r = agargs 2361 if eq r 1 2362 ret 0 2363 end 2364 if ne r 0 2365 ret r 2366 end 2367 r = agkeyload 2368 if ne r 0 2369 ret r 2370 end 2371 r = agmemload 2372 if ne r 0 2373 ret r 2374 end 2375 r = agsload hb 2376 if ne r 0 2377 ret r 2378 end 2379 r = agchdir 2380 if ne r 0 2381 ret r 2382 end 2383 if gt get gsln IPROMPT 0 2384 un = agsget IPROMPT ub 2385 ret agturn ub un hb wb cs io 2386 end 2387 for it ulong 999999 2388 n = agline ub 2389 if eq n -1 2390 ret 0 2391 end 2392 if eq n -2 2393 agerr emsg 2394 ret 9 2395 end 2396 if gt n 0 2397 rc = agturn ub ulong n hb wb cs io 2398 if ne rc 0 2399 ret rc 2400 end 2401 end 2402 end 2403 ret 0 2404 end