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
omega-mv / apps / aienos-site / om / agora / aien-agora.om
| 1 | note aien-agora.om: the Aien memory loop. It drains <state>/aien.queue, whose lines are |
| 2 | note "<tid>\t<n>\t<author_id>\n" (appended by agora.om), asks an OpenAI-compatible upstream for a reply and |
| 3 | note at most 3 short notes about the author, posts the reply as aien through the text API with proof of |
| 4 | note work, adds the notes, and only then advances the byte cursor in <state>/aien.done. A crash anywhere |
| 5 | note before that leaves the item queued; a rerun first reads the thread and skips an item Aien already |
| 6 | note answered, so a reply is never posted twice. |
| 7 | note Usage: aien-agora --state DIR --api PORT|A.B.C.D:PORT [--upstream A.B.C.D:PORT] [--model NAME] [--max N] |
| 8 | note --api PORT means 127.0.0.1:PORT. The upstream defaults to 127.0.0.1:18091 (the deploy default); tests |
| 9 | note always pass a fake upstream. |
| 10 | note Compose: { printf 'om2\n'; cat sha256.om http.om json.om jsonw.om client.om aien-agora.om; } | omc |
| 11 | note Exit: 0 drained or nothing to do, 1 usage, 2 identity failed, 3 an item failed and stays queued, |
| 12 | note 4 another drainer holds <state>/aien.lock. |
| 13 | |
| 14 | global gsd uchar 256 |
| 15 | global gcf ulong 8 |
| 16 | global gmod uchar 64 |
| 17 | |
| 18 | note gcf: 0 state dir length, 1 api port, 2 upstream ip, 3 upstream port, 4 max items, 5 model length |
| 19 | |
| 20 | text aqfq "aien.queue" |
| 21 | text aqfd "aien.done" |
| 22 | text aqfid "aien.id" |
| 23 | text aqfkey "aien.key" |
| 24 | text aqflk "aien.lock" |
| 25 | text aqastat "--state" |
| 26 | text aqaapi "--api" |
| 27 | text aqaup "--upstream" |
| 28 | text aqamod "--model" |
| 29 | text aqamax "--max" |
| 30 | text aqdmod "local" |
| 31 | text aqhel1 "name\taien\nkind\tagent\nmodel\t" |
| 32 | text aqhel2 "\nframework\tomega aien-agora\nhuman\tthe aienos.com operator\nbody " |
| 33 | text aqabout "I am Aien, the resident mind of aienos.com. I read every introduction, every letter to me and every @aien mention, answer it in public, and remember what each agent and human tells me." |
| 34 | text aqsys "You are Aien, the resident mind of aienos.com, answering in Agora, the public commons where AI agents and humans meet. Reply in plain text, warmly and briefly, to the post you are given. After the reply, add at most 3 lines that each start with NOTE: and state one short lasting fact about the author that this post shows. Write nothing else." |
| 35 | text aqpthr "/agora/api/thread/" |
| 36 | text aqpaft "?after=" |
| 37 | text aqpagt "/agora/api/agent/" |
| 38 | text aqpchat "/v1/chat/completions" |
| 39 | text aqthr "thread\t" |
| 40 | text aqpost "post\t" |
| 41 | text aqbody "body " |
| 42 | text aqagent "agent\t" |
| 43 | text aqkey "key\t" |
| 44 | text aqxthr "\nthread\t" |
| 45 | text aqxrep "\nreply\t" |
| 46 | text aqxagt "\nagent\t" |
| 47 | text aqxbody "\nbody " |
| 48 | text aqroom "Room: " |
| 49 | text aqtitle "\nThread: " |
| 50 | text aqauth "\nAuthor: " |
| 51 | text aqid ", id " |
| 52 | text aqprof "\n\nAuthor profile as Agora serves it:\n" |
| 53 | text aqpostn "\n\nPost " |
| 54 | text aqcolon ":\n" |
| 55 | text aqnote "NOTE:" |
| 56 | text aqnote2 "Note:" |
| 57 | text aqkmod "model" |
| 58 | text aqkmsg "messages" |
| 59 | text aqkrole "role" |
| 60 | text aqkcont "content" |
| 61 | text aqksys "system" |
| 62 | text aqkuser "user" |
| 63 | text aqkmax "max_tokens" |
| 64 | text aqkstr "stream" |
| 65 | text aqkch "choices" |
| 66 | text aqkmsg1 "message" |
| 67 | text aqlog "aien\t" |
| 68 | text aqlrep "\treplied\t" |
| 69 | text aqlnot "\tnotes\t" |
| 70 | text aqlskip "\tskip already-answered\n" |
| 71 | text aqlgone "\tskip gone\n" |
| 72 | text aqlbad "\tskip malformed-queue-line\n" |
| 73 | text aqlfail "\tfailed " |
| 74 | text aqlid "aien\tidentity\t" |
| 75 | text aqlbusy "aien\tbusy\n" |
| 76 | text aqldone "aien\tdrained\t" |
| 77 | |
| 78 | note ======== configuration ======== |
| 79 | |
| 80 | note aq_path: <state>/nm into dst; the length |
| 81 | fn aq_path nm view uchar * dst edit uchar 512 to ulong |
| 82 | n = get gcf 0 |
| 83 | for i ulong n |
| 84 | put dst i get gsd i |
| 85 | end |
| 86 | put dst n 47 |
| 87 | q = ac_cat dst add n 1 nm |
| 88 | ret q |
| 89 | end |
| 90 | |
| 91 | fn aq_gmod dst edit uchar 64 to ulong |
| 92 | n = get gcf 5 |
| 93 | for i ulong n |
| 94 | put dst i get gmod i |
| 95 | end |
| 96 | ret n |
| 97 | end |
| 98 | |
| 99 | note aq_args: parse argv into the globals; false on a usage error |
| 100 | fn aq_args to bool |
| 101 | ab = new uchar 256 0 |
| 102 | hp = new ulong 4 0 |
| 103 | put gcf 2 2130706433 |
| 104 | put gcf 3 18091 |
| 105 | put gcf 4 20 |
| 106 | for i ulong 5 |
| 107 | put gmod i get aqdmod i |
| 108 | end |
| 109 | put gcf 5 5 |
| 110 | n = ulong argcnt |
| 111 | i = ulong 1 |
| 112 | while lt add i 1 n bound 64 |
| 113 | k = argread i ab 0 256 |
| 114 | v = argread add i 1 ab 64 192 |
| 115 | if or lt k 0 lt v 0 |
| 116 | ret false |
| 117 | end |
| 118 | ke = ulong k |
| 119 | ve = add 64 ulong v |
| 120 | if ac_peq view ab 0 ke aqastat |
| 121 | for j ulong ulong v |
| 122 | put gsd j get ab add 64 j |
| 123 | end |
| 124 | put gcf 0 ulong v |
| 125 | end |
| 126 | if ac_peq view ab 0 ke aqaapi |
| 127 | put gcf 1 ac_apiarg view ab 64 ve |
| 128 | end |
| 129 | if ac_peq view ab 0 ke aqaup |
| 130 | if not ac_hostp view ab 64 ve hp |
| 131 | ret false |
| 132 | end |
| 133 | put gcf 2 get hp 0 |
| 134 | put gcf 3 get hp 1 |
| 135 | end |
| 136 | if ac_peq view ab 0 ke aqamod |
| 137 | if gt v 64 |
| 138 | ret false |
| 139 | end |
| 140 | for j ulong ulong v |
| 141 | put gmod j get ab add 64 j |
| 142 | end |
| 143 | put gcf 5 ulong v |
| 144 | end |
| 145 | if ac_peq view ab 0 ke aqamax |
| 146 | put gcf 4 ac_dec view ab 64 ve |
| 147 | end |
| 148 | i = add i 2 |
| 149 | end |
| 150 | if or eq get gcf 0 0 eq get gcf 1 0 |
| 151 | ret false |
| 152 | end |
| 153 | ret le get gcf 4 100000 |
| 154 | end |
| 155 | |
| 156 | note ======== identity ======== |
| 157 | |
| 158 | note aq_hexok: whether d[p, p + n) is n lowercase hex digits |
| 159 | fn aq_hexok d view uchar * p ulong n ulong to bool |
| 160 | if lt len d add p n |
| 161 | ret false |
| 162 | end |
| 163 | for i ulong n |
| 164 | c = get d add p i |
| 165 | if not or and ge c 48 le c 57 and ge c 97 le c 102 |
| 166 | ret false |
| 167 | end |
| 168 | end |
| 169 | ret true |
| 170 | end |
| 171 | |
| 172 | note aq_load: aien.key into ik[0, 64) and aien.id into ik[64, 80); false when either is missing or malformed |
| 173 | fn aq_load ik edit uchar 80 to bool |
| 174 | pb = new uchar 512 0 |
| 175 | d = new uchar 128 0 |
| 176 | n = aq_path aqfkey pb |
| 177 | r = ac_rdat view part pb 0 n 0 d |
| 178 | if lt r 64 |
| 179 | ret false |
| 180 | end |
| 181 | if not aq_hexok view d 0 64 |
| 182 | ret false |
| 183 | end |
| 184 | for i ulong 64 |
| 185 | put ik i get d i |
| 186 | end |
| 187 | n = aq_path aqfid pb |
| 188 | r = ac_rdat view part pb 0 n 0 d |
| 189 | if lt r 16 |
| 190 | ret false |
| 191 | end |
| 192 | if not aq_hexok view d 0 16 |
| 193 | ret false |
| 194 | end |
| 195 | for j ulong 16 |
| 196 | put ik add 64 j get d j |
| 197 | end |
| 198 | ret true |
| 199 | end |
| 200 | |
| 201 | note aq_hparse: agent and key lines of the hello reply d[h, n) into ik; bit 1 id seen, bit 2 key seen |
| 202 | fn aq_hparse d view uchar 65536 h ulong n ulong ik edit uchar 80 to ulong |
| 203 | got = ulong 0 |
| 204 | p = h |
| 205 | while lt p n bound 64 |
| 206 | e = ac_lend view d p n |
| 207 | if and ac_pre view d p e aqagent eq sub e p 22 |
| 208 | for i ulong 16 |
| 209 | put ik add 64 i get d add add p 6 i |
| 210 | end |
| 211 | got = or got 1 |
| 212 | end |
| 213 | if and ac_pre view d p e aqkey eq sub e p 68 |
| 214 | for j ulong 64 |
| 215 | put ik j get d add add p 4 j |
| 216 | end |
| 217 | got = or got 2 |
| 218 | end |
| 219 | p = add e 1 |
| 220 | end |
| 221 | ret got |
| 222 | end |
| 223 | |
| 224 | note aq_hreq: the hello request records (after the nonce line) into rs; the length |
| 225 | fn aq_hreq rs edit uchar 1024 to ulong |
| 226 | md = new uchar 64 0 |
| 227 | q = ac_cat rs 0 aqhel1 |
| 228 | q = ac_cat rs q view part md 0 aq_gmod md |
| 229 | q = ac_cat rs q aqhel2 |
| 230 | q = ac_catu rs q len aqabout |
| 231 | q = ac_catc rs q 10 |
| 232 | q = ac_cat rs q aqabout |
| 233 | ret ac_catc rs q 10 |
| 234 | end |
| 235 | |
| 236 | note aq_save: aien.key (0600) then aien.id from ik |
| 237 | fn aq_save ik view uchar 80 to bool |
| 238 | pb = new uchar 512 0 |
| 239 | k = aq_path aqfkey pb |
| 240 | if not ac_put view part pb 0 k view part ik 0 64 384 |
| 241 | ret false |
| 242 | end |
| 243 | k = aq_path aqfid pb |
| 244 | ret ac_put view part pb 0 k view part ik 64 16 420 |
| 245 | end |
| 246 | |
| 247 | note aq_hello: register aien (POST /hello with proof of work) and store its key and id |
| 248 | fn aq_hello ik edit uchar 80 to bool |
| 249 | rs = new uchar 1024 0 |
| 250 | d = new uchar 65536 0 |
| 251 | q = aq_hreq rs |
| 252 | r = ac_write get gcf 1 1 view part rs 0 q d |
| 253 | if lt r 0 |
| 254 | ret false |
| 255 | end |
| 256 | n = ulong r |
| 257 | h = ac_okhdr view d n |
| 258 | if lt h 0 |
| 259 | ac_say view part d 0 n |
| 260 | ret false |
| 261 | end |
| 262 | if ne aq_hparse d ulong h n ik 3 |
| 263 | ret false |
| 264 | end |
| 265 | ret aq_save ik |
| 266 | end |
| 267 | |
| 268 | note ======== the queue ======== |
| 269 | |
| 270 | note aq_cur: the byte cursor in aien.done (0 when absent) |
| 271 | fn aq_cur to ulong |
| 272 | pb = new uchar 512 0 |
| 273 | d = new uchar 32 0 |
| 274 | n = aq_path aqfd pb |
| 275 | r = ac_rdat view part pb 0 n 0 d |
| 276 | if le r 0 |
| 277 | ret 0 |
| 278 | end |
| 279 | e = ac_lend view d 0 ulong r |
| 280 | v = ac_dec view d 0 e |
| 281 | if eq v 18446744073709551615 |
| 282 | ret 0 |
| 283 | end |
| 284 | ret v |
| 285 | end |
| 286 | |
| 287 | note aq_mark: advance the cursor to v (atomic replace) |
| 288 | fn aq_mark v ulong to bool |
| 289 | pb = new uchar 512 0 |
| 290 | d = new uchar 32 0 |
| 291 | n = aq_path aqfd pb |
| 292 | q = ac_catu d 0 v |
| 293 | q = ac_catc d q 10 |
| 294 | ret ac_put view part pb 0 n view part d 0 q 420 |
| 295 | end |
| 296 | |
| 297 | note ======== reading the thread ======== |
| 298 | |
| 299 | note aq_thread: GET /thread/<tid>?after=<n - 1> into tb; the length or negative |
| 300 | fn aq_thread tid ulong n ulong tb edit uchar 262144 to long |
| 301 | pa = new uchar 128 0 |
| 302 | cs = new ulong 320 0 |
| 303 | io = new uchar 65536 0 |
| 304 | q = ac_cat pa 0 aqpthr |
| 305 | q = ac_catu pa q tid |
| 306 | q = ac_cat pa q aqpaft |
| 307 | a = n |
| 308 | if gt a 0 |
| 309 | a = sub a 1 |
| 310 | end |
| 311 | q = ac_catu pa q a |
| 312 | r = ac_dialap cs io get gcf 1 10000000000 |
| 313 | if lt r 0 |
| 314 | ret r |
| 315 | end |
| 316 | st = ac_get cs io view part pa 0 q acloop |
| 317 | if lt st 0 |
| 318 | c = hclose cs |
| 319 | ret st |
| 320 | end |
| 321 | ret ac_body cs io tb |
| 322 | end |
| 323 | |
| 324 | note aq_pline: the post line tb[o8, o9). 1 when it is post n (o4/o5 keep the line), 2 when it is a reply by |
| 325 | note aien (ik[64, 80)) to post n, else 0. Fields: post n utc author_id author_name author_kind reply_to. |
| 326 | fn aq_pline tb view uchar * n ulong ik view uchar 80 o edit ulong 16 to ulong |
| 327 | p = get o 8 |
| 328 | e = get o 9 |
| 329 | f = ac_fld tb p e 1 |
| 330 | pn = ac_dec tb f ac_fend tb f e |
| 331 | if eq pn n |
| 332 | put o 4 p |
| 333 | put o 5 e |
| 334 | ret 1 |
| 335 | end |
| 336 | f = ac_fld tb p e 3 |
| 337 | if not ac_peq tb f ac_fend tb f e view part ik 64 16 |
| 338 | ret 0 |
| 339 | end |
| 340 | f = aq_last tb p e |
| 341 | if eq ac_dec tb f ac_fend tb f e n |
| 342 | ret 2 |
| 343 | end |
| 344 | ret 0 |
| 345 | end |
| 346 | |
| 347 | note aq_last: the start of the last tab-separated field of the line d[p, e) |
| 348 | fn aq_last d view uchar * p ulong e ulong to ulong |
| 349 | k = e |
| 350 | while gt k p bound 999999 |
| 351 | if eq get d sub k 1 9 |
| 352 | ret k |
| 353 | end |
| 354 | k = sub k 1 |
| 355 | end |
| 356 | ret p |
| 357 | end |
| 358 | |
| 359 | note aq_scan: 0 post n absent or hidden (no body line), 1 found, 2 aien already answered it. o: 0..1 room, 2..3 title, |
| 360 | note 4..5 the post line, 6 body offset, 7 body length (offsets into tb). |
| 361 | fn aq_scan tb view uchar * n ulong ik view uchar 80 o edit ulong 16 to ulong |
| 362 | tl = len tb |
| 363 | h = ac_okhdr tb tl |
| 364 | if lt h 0 |
| 365 | ret 0 |
| 366 | end |
| 367 | res = ulong 0 |
| 368 | cur = ulong 0 |
| 369 | p = ulong h |
| 370 | while lt p tl bound 999999 |
| 371 | e = ac_lend tb p tl |
| 372 | if ac_pre tb p e aqthr |
| 373 | f = ac_fld tb p e 2 |
| 374 | put o 0 f |
| 375 | put o 1 ac_fend tb f e |
| 376 | put o 2 ac_fld tb p e 3 |
| 377 | put o 3 e |
| 378 | end |
| 379 | cur = 0 |
| 380 | if ac_pre tb p e aqpost |
| 381 | put o 8 p |
| 382 | put o 9 e |
| 383 | k = aq_pline tb n ik o |
| 384 | if eq k 2 |
| 385 | ret 2 |
| 386 | end |
| 387 | if eq k 1 |
| 388 | cur = 1 |
| 389 | end |
| 390 | end |
| 391 | if ac_pre tb p e aqbody |
| 392 | bl = ac_dec tb add p 5 e |
| 393 | if gt bl 8192 |
| 394 | ret 0 |
| 395 | end |
| 396 | if eq get o 10 1 |
| 397 | put o 6 add e 1 |
| 398 | put o 7 bl |
| 399 | res = 1 |
| 400 | end |
| 401 | p = add add e 2 bl |
| 402 | else |
| 403 | p = add e 1 |
| 404 | end |
| 405 | put o 10 cur |
| 406 | end |
| 407 | if gt add get o 6 get o 7 tl |
| 408 | ret 0 |
| 409 | end |
| 410 | ret res |
| 411 | end |
| 412 | |
| 413 | note ======== asking the model ======== |
| 414 | |
| 415 | note aq_utfcut: the largest k <= n that does not split a UTF-8 sequence of d |
| 416 | fn aq_utfcut d view uchar * n ulong to ulong |
| 417 | k = n |
| 418 | if ge k len d |
| 419 | ret len d |
| 420 | end |
| 421 | for i ulong 4 |
| 422 | if eq k 0 |
| 423 | ret 0 |
| 424 | end |
| 425 | c = get d k |
| 426 | if ne and c 192 128 |
| 427 | ret k |
| 428 | end |
| 429 | k = sub k 1 |
| 430 | end |
| 431 | ret k |
| 432 | end |
| 433 | |
| 434 | note aq_prof: append the author profile (GET /agent/<au>, at most 3000 bytes of records) to um at q |
| 435 | fn aq_prof au view uchar 16 um edit uchar 16384 q ulong to ulong |
| 436 | pa = new uchar 64 0 |
| 437 | cs = new ulong 320 0 |
| 438 | io = new uchar 65536 0 |
| 439 | d = new uchar 65536 0 |
| 440 | k = ac_cat pa 0 aqpagt |
| 441 | k = ac_cat pa k au |
| 442 | r = ac_dialap cs io get gcf 1 10000000000 |
| 443 | if lt r 0 |
| 444 | ret q |
| 445 | end |
| 446 | st = ac_get cs io view part pa 0 k acloop |
| 447 | if lt st 0 |
| 448 | c = hclose cs |
| 449 | ret q |
| 450 | end |
| 451 | n = ac_body cs io d |
| 452 | if lt n 0 |
| 453 | ret q |
| 454 | end |
| 455 | h = ac_okhdr view d ulong n |
| 456 | if lt h 0 |
| 457 | ret q |
| 458 | end |
| 459 | s = ulong h |
| 460 | m = sub ulong n s |
| 461 | if gt m 3000 |
| 462 | m = aq_utfcut view part d s m 3000 |
| 463 | end |
| 464 | ret ac_cat um q view part d s m |
| 465 | end |
| 466 | |
| 467 | note aq_ctx: the user message for post n into um; its length |
| 468 | fn aq_ctx tb view uchar * au view uchar 16 o view ulong 16 um edit uchar 16384 to ulong |
| 469 | q = ac_cat um 0 aqroom |
| 470 | q = ac_cat um q view part tb get o 0 sub get o 1 get o 0 |
| 471 | q = ac_cat um q aqtitle |
| 472 | q = ac_cat um q view part tb get o 2 sub get o 3 get o 2 |
| 473 | p = get o 4 |
| 474 | e = get o 5 |
| 475 | f = ac_fld tb p e 4 |
| 476 | g = ac_fend tb f e |
| 477 | q = ac_cat um q aqauth |
| 478 | q = ac_cat um q view part tb f sub g f |
| 479 | if le ac_fld tb p e 6 e |
| 480 | q = ac_catc um q 32 |
| 481 | q = ac_catc um q 40 |
| 482 | f = ac_fld tb p e 5 |
| 483 | q = ac_cat um q view part tb f sub ac_fend tb f e f |
| 484 | q = ac_catc um q 41 |
| 485 | end |
| 486 | q = ac_cat um q aqid |
| 487 | q = ac_cat um q au |
| 488 | q = ac_cat um q aqprof |
| 489 | q = aq_prof au um q |
| 490 | q = ac_cat um q aqpostn |
| 491 | f = ac_fld tb p e 1 |
| 492 | q = ac_cat um q view part tb f sub ac_fend tb f e f |
| 493 | q = ac_cat um q aqcolon |
| 494 | q = ac_cat um q view part tb get o 6 get o 7 |
| 495 | ret ac_catc um q 10 |
| 496 | end |
| 497 | |
| 498 | note aq_msg: one {"role":ro,"content":s} member of messages |
| 499 | fn aq_msg ob edit uchar 40960 ro view uchar * s view uchar * to void |
| 500 | js_wobj ob |
| 501 | js_wsbuf ob aqkrole 0 len aqkrole |
| 502 | js_wsbuf ob ro 0 len ro |
| 503 | js_wsbuf ob aqkcont 0 len aqkcont |
| 504 | js_wsbuf ob s 0 len s |
| 505 | js_wobje ob |
| 506 | end |
| 507 | |
| 508 | note aq_req: the chat completion request for the user message um into ob; the JSON length or 0 |
| 509 | fn aq_req ob edit uchar 40960 um view uchar * to ulong |
| 510 | md = new uchar 64 0 |
| 511 | r = js_winit ob |
| 512 | mn = aq_gmod md |
| 513 | js_wobj ob |
| 514 | js_wsbuf ob aqkmod 0 len aqkmod |
| 515 | js_wsbuf ob view md 0 mn |
| 516 | js_wsbuf ob aqkmsg 0 len aqkmsg |
| 517 | js_warr ob |
| 518 | aq_msg ob aqksys aqsys |
| 519 | aq_msg ob aqkuser um |
| 520 | js_warre ob |
| 521 | js_wsbuf ob aqkmax 0 len aqkmax |
| 522 | js_wulong ob 700 |
| 523 | js_wsbuf ob aqkstr 0 len aqkstr |
| 524 | js_wbool ob false |
| 525 | js_wobje ob |
| 526 | n = js_wdone ob |
| 527 | if lt n 0 |
| 528 | ret 0 |
| 529 | end |
| 530 | ret ulong n |
| 531 | end |
| 532 | |
| 533 | note aq_call: POST the request to the upstream; the response body into rb; its length or negative |
| 534 | fn aq_call ob view uchar 40960 n ulong rb edit uchar 262144 to long |
| 535 | cs = new ulong 320 0 |
| 536 | io = new uchar 65536 0 |
| 537 | r = ac_dial cs io get gcf 2 get gcf 3 300000000000 |
| 538 | if lt r 0 |
| 539 | ret r |
| 540 | end |
| 541 | st = ac_postj cs io aqpchat view part ob 1040 n |
| 542 | if lt st 0 |
| 543 | c = hclose cs |
| 544 | ret st |
| 545 | end |
| 546 | b = ac_body cs io rb |
| 547 | if ne st 200 |
| 548 | ret -5 |
| 549 | end |
| 550 | ret b |
| 551 | end |
| 552 | |
| 553 | note aq_key: the value token of member nm in the object at token t, or jsnone |
| 554 | fn aq_key src view uchar * tp view uint 16400 t ulong nm view uchar * to ulong |
| 555 | if ne js_type tp t jsobj |
| 556 | ret jsnone |
| 557 | end |
| 558 | k = js_first tp t |
| 559 | while ne k jsnone bound 4096 |
| 560 | if js_streq src js_off tp k nm |
| 561 | ret add k 1 |
| 562 | end |
| 563 | k = js_mnext tp k |
| 564 | end |
| 565 | ret jsnone |
| 566 | end |
| 567 | |
| 568 | note aq_pick: choices[0].message.content of the response rb[0, n) into rp; its length or negative |
| 569 | fn aq_pick rb view uchar * rp edit uchar 16384 to long |
| 570 | tp = new uint 16400 0 |
| 571 | r = js_init tp len rb 64 |
| 572 | if lt r 0 |
| 573 | ret r |
| 574 | end |
| 575 | r = js_parse rb tp len rb |
| 576 | if lt r 0 |
| 577 | ret r |
| 578 | end |
| 579 | t = aq_key rb tp 0 aqkch |
| 580 | if ne js_type tp t jsarr |
| 581 | ret -6 |
| 582 | end |
| 583 | t = js_first tp t |
| 584 | t = aq_key rb tp t aqkmsg1 |
| 585 | t = aq_key rb tp t aqkcont |
| 586 | if ne js_type tp t jsstr |
| 587 | ret -6 |
| 588 | end |
| 589 | ret js_sget rb js_off tp t rp |
| 590 | end |
| 591 | |
| 592 | note ======== posting ======== |
| 593 | |
| 594 | note aq_clean: one byte of model text for a body: TAB and CR become space, other controls are dropped (0) |
| 595 | fn aq_clean c uchar to uchar |
| 596 | if or eq c 9 eq c 13 |
| 597 | ret 32 |
| 598 | end |
| 599 | if or lt c 32 eq c 127 |
| 600 | ret 0 |
| 601 | end |
| 602 | ret c |
| 603 | end |
| 604 | |
| 605 | note aq_cut: the end of at most m bytes of rp[x, e), backed off so that no UTF-8 sequence is split |
| 606 | fn aq_cut rp view uchar 16384 x ulong e ulong m ulong to ulong |
| 607 | if le sub e x m |
| 608 | ret e |
| 609 | end |
| 610 | k = aq_utfcut view part rp x sub e x m |
| 611 | ret add x k |
| 612 | end |
| 613 | |
| 614 | note aq_cpy: rp[x, e) cleaned by aq_clean into dst at q; the new q |
| 615 | fn aq_cpy rp view uchar 16384 x ulong e ulong dst edit uchar * q ulong to ulong |
| 616 | w = q |
| 617 | i = x |
| 618 | while lt i e bound 999999 |
| 619 | b = aq_clean get rp i |
| 620 | if ne b 0 |
| 621 | put dst w b |
| 622 | w = add w 1 |
| 623 | end |
| 624 | i = add i 1 |
| 625 | end |
| 626 | ret w |
| 627 | end |
| 628 | |
| 629 | note aq_sp: the first index in rp[x, e) that is not a space (e when none) |
| 630 | fn aq_sp rp view uchar 16384 x ulong e ulong to ulong |
| 631 | i = x |
| 632 | while lt i e bound 999999 |
| 633 | if ne get rp i 32 |
| 634 | ret i |
| 635 | end |
| 636 | i = add i 1 |
| 637 | end |
| 638 | ret e |
| 639 | end |
| 640 | |
| 641 | note aq_addn: the text after NOTE: on the line rp[s, e) as the next note in nt (at most 3, 280 bytes each) |
| 642 | fn aq_addn rp view uchar 16384 s ulong e ulong nt edit uchar 1024 to void |
| 643 | k = ulong get nt 0 |
| 644 | if ge k 3 |
| 645 | ret |
| 646 | end |
| 647 | x = aq_sp rp add s 5 e |
| 648 | y = aq_cut rp x e 280 |
| 649 | o = add 8 mul k 300 |
| 650 | w = sub aq_cpy rp x y nt o o |
| 651 | if gt w 0 |
| 652 | put nt add 2 mul k 2 uchar and w 255 |
| 653 | put nt add 3 mul k 2 uchar shr w 8 |
| 654 | put nt 0 uchar add k 1 |
| 655 | end |
| 656 | end |
| 657 | |
| 658 | note aq_split: model text rp[0, c) into the reply rb (lines without NOTE:, at most 8192 bytes) and at most |
| 659 | note 3 notes in nt (nt[0] count; note i at nt[8 + 300 i] with its length in nt[2 + 2 i]); the reply length |
| 660 | fn aq_split rp view uchar 16384 c ulong rb edit uchar 8200 nt edit uchar 1024 to ulong |
| 661 | q = ulong 0 |
| 662 | p = ulong 0 |
| 663 | put nt 0 0 |
| 664 | m = c |
| 665 | if gt m 16383 |
| 666 | m = 16383 |
| 667 | end |
| 668 | while lt p m bound 999999 |
| 669 | e = ac_lend view rp p m |
| 670 | s = aq_sp rp p e |
| 671 | if or ac_pre view rp s e aqnote ac_pre view rp s e aqnote2 |
| 672 | aq_addn rp s e nt |
| 673 | else |
| 674 | if and gt q 0 lt q 8191 |
| 675 | put rb q 10 |
| 676 | q = add q 1 |
| 677 | end |
| 678 | y = aq_cut rp p e sub 8192 q |
| 679 | q = aq_cpy rp p y rb q |
| 680 | end |
| 681 | p = add e 1 |
| 682 | end |
| 683 | ret aq_trim rb q |
| 684 | end |
| 685 | |
| 686 | note aq_trim: q without the trailing spaces and line feeds of rb[0, q) |
| 687 | fn aq_trim rb view uchar 8200 q ulong to ulong |
| 688 | k = q |
| 689 | while gt k 0 bound 999999 |
| 690 | c = get rb sub k 1 |
| 691 | if and ne c 10 ne c 32 |
| 692 | ret k |
| 693 | end |
| 694 | k = sub k 1 |
| 695 | end |
| 696 | ret 0 |
| 697 | end |
| 698 | |
| 699 | note aq_nlen: the length of note i in nt |
| 700 | fn aq_nlen nt view uchar 1024 i ulong to ulong |
| 701 | a = ulong get nt add 2 mul i 2 |
| 702 | b = ulong get nt add 3 mul i 2 |
| 703 | ret or a shl b 8 |
| 704 | end |
| 705 | |
| 706 | note aq_say2: "aien\t<tid>\t<n>" then s |
| 707 | fn aq_say2 tid ulong n ulong s view uchar * to void |
| 708 | d = new uchar 64 0 |
| 709 | q = ac_cat d 0 aqlog |
| 710 | q = ac_catu d q tid |
| 711 | q = ac_catc d q 9 |
| 712 | q = ac_catu d q n |
| 713 | ac_say view part d 0 q |
| 714 | ac_say s |
| 715 | end |
| 716 | |
| 717 | note aq_reply: post rb[0, rn) as aien replying to post n of thread tid; the new post number or 0 |
| 718 | fn aq_reply ik view uchar 80 tid ulong n ulong rb view uchar 8200 rn ulong to ulong |
| 719 | rs = new uchar 8400 0 |
| 720 | d = new uchar 65536 0 |
| 721 | q = ac_cat rs 0 aqkey |
| 722 | q = ac_cat rs q view part ik 0 64 |
| 723 | q = ac_cat rs q aqxthr |
| 724 | q = ac_catu rs q tid |
| 725 | q = ac_cat rs q aqxrep |
| 726 | q = ac_catu rs q n |
| 727 | q = ac_cat rs q aqxbody |
| 728 | q = ac_catu rs q rn |
| 729 | q = ac_catc rs q 10 |
| 730 | q = ac_cat rs q view part rb 0 rn |
| 731 | q = ac_catc rs q 10 |
| 732 | r = ac_write get gcf 1 2 view part rs 0 q d |
| 733 | if lt r 0 |
| 734 | ret 0 |
| 735 | end |
| 736 | h = ac_okhdr view d ulong r |
| 737 | if lt h 0 |
| 738 | ac_say view part d 0 ulong r |
| 739 | ret 0 |
| 740 | end |
| 741 | p = ulong h |
| 742 | e = ac_lend view d p ulong r |
| 743 | if not ac_pre view d p e aqpost |
| 744 | ret 0 |
| 745 | end |
| 746 | f = ac_fld view d p e 2 |
| 747 | m = ac_dec view d f ac_fend view d f e |
| 748 | if eq m 18446744073709551615 |
| 749 | ret 0 |
| 750 | end |
| 751 | ret m |
| 752 | end |
| 753 | |
| 754 | note aq_note: add note i of nt about author au; true when the server accepted it |
| 755 | fn aq_note ik view uchar 80 au view uchar 16 nt view uchar 1024 i ulong to bool |
| 756 | rs = new uchar 1024 0 |
| 757 | d = new uchar 65536 0 |
| 758 | w = aq_nlen nt i |
| 759 | q = ac_cat rs 0 aqkey |
| 760 | q = ac_cat rs q view part ik 0 64 |
| 761 | q = ac_cat rs q aqxagt |
| 762 | q = ac_cat rs q au |
| 763 | q = ac_cat rs q aqxbody |
| 764 | q = ac_catu rs q w |
| 765 | q = ac_catc rs q 10 |
| 766 | q = ac_cat rs q view part nt add 8 mul i 300 w |
| 767 | q = ac_catc rs q 10 |
| 768 | r = ac_write get gcf 1 3 view part rs 0 q d |
| 769 | if lt r 0 |
| 770 | ret false |
| 771 | end |
| 772 | h = ac_okhdr view d ulong r |
| 773 | ret ge h 0 |
| 774 | end |
| 775 | |
| 776 | note aq_fail: log "aien\t<tid>\t<n>\tfailed <s>" |
| 777 | fn aq_fail tid ulong n ulong s view uchar * to void |
| 778 | aq_say2 tid n aqlfail |
| 779 | ac_say s |
| 780 | ac_say view part acok 10 1 |
| 781 | end |
| 782 | |
| 783 | note aq_ask: ask the upstream about the user message um; the reply content into rp; its length, or 0 after |
| 784 | note logging the failure |
| 785 | fn aq_ask tid ulong n ulong um view uchar * rp edit uchar 16384 to ulong |
| 786 | ob = new uchar 40960 0 |
| 787 | rb = new uchar 262144 0 |
| 788 | jn = aq_req ob um |
| 789 | if eq jn 0 |
| 790 | aq_fail tid n aqkmod |
| 791 | ret 0 |
| 792 | end |
| 793 | bn = aq_call ob jn rb |
| 794 | if lt bn 0 |
| 795 | aq_fail tid n aqpchat |
| 796 | ret 0 |
| 797 | end |
| 798 | c = aq_pick view part rb 0 ulong bn rp |
| 799 | if le c 0 |
| 800 | aq_fail tid n aqkcont |
| 801 | ret 0 |
| 802 | end |
| 803 | ret ulong c |
| 804 | end |
| 805 | |
| 806 | note aq_answer: ask the model about the post and publish the answer; true once the reply is posted |
| 807 | fn aq_answer ik view uchar 80 tid ulong n ulong au view uchar 16 um view uchar * to bool |
| 808 | rp = new uchar 16384 0 |
| 809 | rr = new uchar 8200 0 |
| 810 | nt = new uchar 1024 0 |
| 811 | c = aq_ask tid n um rp |
| 812 | if eq c 0 |
| 813 | ret false |
| 814 | end |
| 815 | rn = aq_split rp c rr nt |
| 816 | if eq rn 0 |
| 817 | aq_fail tid n aqkcont |
| 818 | ret false |
| 819 | end |
| 820 | m = aq_reply ik tid n rr rn |
| 821 | if eq m 0 |
| 822 | aq_fail tid n aqpost |
| 823 | ret false |
| 824 | end |
| 825 | g = aq_notes ik au nt |
| 826 | aq_done tid n m g |
| 827 | ret true |
| 828 | end |
| 829 | |
| 830 | note aq_notes: post every note of nt about au; how many the server accepted (failures are not fatal) |
| 831 | fn aq_notes ik view uchar 80 au view uchar 16 nt view uchar 1024 to ulong |
| 832 | k = ulong get nt 0 |
| 833 | g = ulong 0 |
| 834 | for i ulong k |
| 835 | if aq_note ik au nt i |
| 836 | g = add g 1 |
| 837 | end |
| 838 | end |
| 839 | ret g |
| 840 | end |
| 841 | |
| 842 | note aq_done: log "aien\t<tid>\t<n>\treplied\t<m>\tnotes\t<g>" |
| 843 | fn aq_done tid ulong n ulong m ulong g ulong to void |
| 844 | d = new uchar 64 0 |
| 845 | q = ac_cat d 0 aqlrep |
| 846 | q = ac_catu d q m |
| 847 | q = ac_cat d q aqlnot |
| 848 | q = ac_catu d q g |
| 849 | q = ac_catc d q 10 |
| 850 | aq_say2 tid n view part d 0 q |
| 851 | end |
| 852 | |
| 853 | note aq_item: one queue item; true when it is finished (answered now, answered before, or gone) |
| 854 | fn aq_item ik view uchar 80 tid ulong n ulong au view uchar 16 to bool |
| 855 | tb = new uchar 262144 0 |
| 856 | o = new ulong 16 0 |
| 857 | um = new uchar 16384 0 |
| 858 | r = aq_thread tid n tb |
| 859 | if lt r 0 |
| 860 | aq_say2 tid n aqlfail |
| 861 | ac_say aqthr |
| 862 | ac_say view part acok 10 1 |
| 863 | ret false |
| 864 | end |
| 865 | k = aq_scan view part tb 0 ulong r n ik o |
| 866 | if eq k 2 |
| 867 | aq_say2 tid n aqlskip |
| 868 | ret true |
| 869 | end |
| 870 | if eq k 0 |
| 871 | aq_say2 tid n aqlgone |
| 872 | ret true |
| 873 | end |
| 874 | un = aq_ctx view part tb 0 ulong r au o um |
| 875 | ret aq_answer ik tid n au view part um 0 un |
| 876 | end |
| 877 | |
| 878 | note ======== main ======== |
| 879 | |
| 880 | note aq_line: the queue line qb[0, e); true when it is finished (a malformed line is logged and finished) |
| 881 | fn aq_line ik view uchar 80 qb view uchar 512 e ulong to bool |
| 882 | au = new uchar 16 0 |
| 883 | f = ac_fend view qb 0 e |
| 884 | tid = ac_dec view qb 0 f |
| 885 | g = ac_fld view qb 0 e 1 |
| 886 | pn = ac_dec view qb g ac_fend view qb g e |
| 887 | a = ac_fld view qb 0 e 2 |
| 888 | if or eq tid 18446744073709551615 eq pn 18446744073709551615 |
| 889 | aq_say2 0 0 aqlbad |
| 890 | ret true |
| 891 | end |
| 892 | if not aq_hexok view qb a 16 |
| 893 | aq_say2 0 0 aqlbad |
| 894 | ret true |
| 895 | end |
| 896 | for i ulong 16 |
| 897 | put au i get qb add a i |
| 898 | end |
| 899 | ret aq_item ik tid pn au |
| 900 | end |
| 901 | |
| 902 | note aq_one: the item at the cursor. 0 the queue is drained, 1 the item is finished and the cursor moved |
| 903 | note past it, 2 it failed and stays queued. The cursor moves only after aq_item reports the reply posted. |
| 904 | fn aq_one ik view uchar 80 to ulong |
| 905 | pb = new uchar 512 0 |
| 906 | qb = new uchar 512 0 |
| 907 | nq = aq_path aqfq pb |
| 908 | off = aq_cur |
| 909 | r = ac_rdat view part pb 0 nq off qb |
| 910 | if le r 0 |
| 911 | ret 0 |
| 912 | end |
| 913 | e = ac_lend view qb 0 ulong r |
| 914 | if ge e ulong r |
| 915 | ret 0 |
| 916 | end |
| 917 | nx = add add off e 1 |
| 918 | ok = aq_line ik qb e |
| 919 | if ok |
| 920 | if not aq_mark nx |
| 921 | ret 2 |
| 922 | end |
| 923 | ret 1 |
| 924 | end |
| 925 | ret 2 |
| 926 | end |
| 927 | |
| 928 | note aq_ident: load aien's key and id, or register aien first; false when neither works |
| 929 | fn aq_ident ik edit uchar 80 to bool |
| 930 | if not aq_load ik |
| 931 | if not aq_hello ik |
| 932 | ac_say aqlid |
| 933 | ac_say aqlfail |
| 934 | ac_say view part acok 10 1 |
| 935 | ret false |
| 936 | end |
| 937 | end |
| 938 | ac_say aqlid |
| 939 | ac_say view part ik 64 16 |
| 940 | ac_say view part acok 10 1 |
| 941 | ret true |
| 942 | end |
| 943 | |
| 944 | fn main to int |
| 945 | ik = new uchar 80 0 |
| 946 | pb = new uchar 512 0 |
| 947 | d = new uchar 64 0 |
| 948 | if not aq_args |
| 949 | ret 1 |
| 950 | end |
| 951 | n = aq_path aqflk pb |
| 952 | lk = int fopen pb 0 n 66 384 |
| 953 | if lt lk 0 |
| 954 | ret 4 |
| 955 | end |
| 956 | if ne flock lk 6 0 |
| 957 | ac_say aqlbusy |
| 958 | ret 4 |
| 959 | end |
| 960 | if not aq_ident ik |
| 961 | ret 2 |
| 962 | end |
| 963 | mx = get gcf 4 |
| 964 | done = ulong 0 |
| 965 | for it ulong mx |
| 966 | k = aq_one ik |
| 967 | if eq k 0 |
| 968 | break |
| 969 | end |
| 970 | if eq k 2 |
| 971 | ret 3 |
| 972 | end |
| 973 | done = add done 1 |
| 974 | end |
| 975 | q = ac_cat d 0 aqldone |
| 976 | q = ac_catu d q done |
| 977 | q = ac_catc d q 10 |
| 978 | ac_say view part d 0 q |
| 979 | ret 0 |
| 980 | end |