code wiki / (root) / nx_writehub_seat.nx

nx_writehub_seat.nx source

↩ module page · 128 lines · 7564 B

1// nx_writehub_seat.nx -- WRITING HUB rung R1: the sovereign SEAT WIRE. Closes the #1 census gap (nothing in .nx 2// POSTed to the local generation seats). Drives the operator's LOCAL models over sovereign HTTP -- no cloud, no 3// curl, no shell: gen-llm:11434 /chat (prose seat, sd-server/Qwen) or gen-img:7861 /generate (image seat, Z-Image). 4// Composes nx_http_client's sockaddr + POST-request builder (same primitive as nx_http_post) but application/json. 5// File-driven so there is zero arg-quoting: read knowledge/staging/writehub_seat_req.txt (line1 = llm|img target, 6// the rest = the writer artifact/prompt) -> POST -> bank the seat's reply to knowledge/staging/writehub_seat_out.txt. 7// SEAM BY CONSTRUCTION: this organ only ever connects to 127.0.0.1, so explicit content never leaves the box. 8// license_tier: ORIGINAL module: nishi-core.writehub.seat capability: WRITEHUB_SEAT_WIRE 9import "nx_http_client.nx" 10import "nx_connect.nx" // bounded connect: a raw sys_connect hangs ~127s on a black-holed host 11import "nx_gate.nx" 12const K_MAGIC_1048576: i64 = 1048576 13const K_MAGIC_1310720: i64 = 1310720 14const K_MAGIC_11434: i64 = 11434 15const K_MAGIC_7861: i64 = 7861 16const K_MAGIC_8388608: i64 = 8388608 17 18func ws_strlen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 19func ws_atoi3(buf: *u8, off: i64) -> i64 { var v: i64=0; var i: i64=0; while i<3 { let d: i64=buf[off+i] as i64; if d>=48 { if d<=57 { v=v*10+(d-48) } } i=i+1 } return v } 20func ws_drain(fd: i64, buf: *u8, cap: i64) -> i64 { var off: i64=0; var go: i64=1; while go==1 { if off>=cap { go=0 } else { let r: i64=sys_read(fd, ((buf as i64)+off) as *u8, cap-off); if r<=0 { go=0 } else { off=off+r } } } return off } 21 22// append null-terminated s into dst at off; return new off 23func ws_app(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64=off; var i: i64=0; while s[i]!=(0 as u8){ dst[o]=s[i]; o=o+1; i=i+1 } return o } 24// append one char (by code) into dst at off 25func ws_appc(dst: *u8, off: i64, ch: i64) -> i64 { dst[off]=ch as u8; return off+1 } 26// append a JSON-quoted bareword: "s" 27func ws_appq(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64=off; o=ws_appc(dst,o,34); o=ws_app(dst,o,s); o=ws_appc(dst,o,34); return o } 28// append src[0..len] into dst at off, JSON-string-escaped (quote/backslash/newline/tab; drop other control bytes) 29func ws_app_esc(dst: *u8, off: i64, src: *u8, len: i64) -> i64 { 30 var o: i64=off; var i: i64=0 31 while i<len { 32 let c: i64 = src[i] as i64 33 if c==34 { o=ws_appc(dst,o,92); o=ws_appc(dst,o,34) } 34 else { if c==92 { o=ws_appc(dst,o,92); o=ws_appc(dst,o,92) } 35 else { if c==10 { o=ws_appc(dst,o,92); o=ws_appc(dst,o,110) } 36 else { if c==9 { o=ws_appc(dst,o,92); o=ws_appc(dst,o,116) } 37 else { if c==13 { o=o } 38 else { if c<32 { o=o } 39 else { dst[o]=src[i]; o=o+1 } } } } } } 40 i=i+1 41 } 42 return o 43} 44func ws_starts(buf: *u8, n: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){ if i>=n {return 0} if buf[i]!=s[i] {return 0} i=i+1 } return 1 } 45 46func main() -> i64 { 47 let rfd: i64 = sys_openat_rd("knowledge/staging/writehub_seat_req.txt" as *u8) 48 if rfd < 0 { gw("SEAT: no knowledge/staging/writehub_seat_req.txt (line1=llm|img, rest=prompt)\n" as *u8); sys_exit(1); return 1 } 49 let rb: *u8 = sys_mmap(K_MAGIC_1048576) 50 let rn: i64 = sys_read(rfd, rb, K_MAGIC_1048576) 51 sys_close(rfd) 52 if rn <= 0 { gw("SEAT: empty seat_req\n" as *u8); sys_exit(1); return 1 } 53 54 // first line = target token; remainder = the prompt/artifact 55 var nl: i64 = 0 56 var done: i64 = 0 57 while done==0 { if nl>=rn { done=1 } else { if rb[nl]==(10 as u8) { done=1 } else { nl=nl+1 } } } 58 var is_img: i64 = 0 59 if ws_starts(rb, rn, "img" as *u8)==1 { is_img=1 } 60 var pstart: i64 = nl+1 61 if pstart > rn { pstart = rn } 62 let plen: i64 = rn - pstart 63 let prompt: *u8 = ((rb as i64)+pstart) as *u8 64 65 // build the JSON body (char-coded quotes -> no \" escape dependency) 66 let body: *u8 = sys_mmap(K_MAGIC_1310720 + plen) 67 var bo: i64 = 0 68 if is_img==1 { 69 // {"prompt":"<esc>","steps":8,"width":768,"height":768} 70 bo=ws_appc(body,bo,123) 71 bo=ws_appq(body,bo,"prompt" as *u8); bo=ws_appc(body,bo,58) 72 bo=ws_appc(body,bo,34); bo=ws_app_esc(body,bo,prompt,plen); bo=ws_appc(body,bo,34) 73 bo=ws_appc(body,bo,44); bo=ws_appq(body,bo,"steps" as *u8); bo=ws_appc(body,bo,58); bo=ws_app(body,bo,"8" as *u8) 74 bo=ws_appc(body,bo,44); bo=ws_appq(body,bo,"width" as *u8); bo=ws_appc(body,bo,58); bo=ws_app(body,bo,"768" as *u8) 75 bo=ws_appc(body,bo,44); bo=ws_appq(body,bo,"height" as *u8); bo=ws_appc(body,bo,58); bo=ws_app(body,bo,"768" as *u8) 76 bo=ws_appc(body,bo,125) 77 } else { 78 // {"messages":[{"role":"user","content":"<esc>"}],"max_tokens":256,"temperature":0.8} 79 bo=ws_appc(body,bo,123) 80 bo=ws_appq(body,bo,"messages" as *u8); bo=ws_appc(body,bo,58); bo=ws_appc(body,bo,91); bo=ws_appc(body,bo,123) 81 bo=ws_appq(body,bo,"role" as *u8); bo=ws_appc(body,bo,58); bo=ws_appq(body,bo,"user" as *u8) 82 bo=ws_appc(body,bo,44); bo=ws_appq(body,bo,"content" as *u8); bo=ws_appc(body,bo,58) 83 bo=ws_appc(body,bo,34); bo=ws_app_esc(body,bo,prompt,plen); bo=ws_appc(body,bo,34) 84 bo=ws_appc(body,bo,125); bo=ws_appc(body,bo,93) 85 bo=ws_appc(body,bo,44); bo=ws_appq(body,bo,"max_tokens" as *u8); bo=ws_appc(body,bo,58); bo=ws_app(body,bo,"256" as *u8) 86 bo=ws_appc(body,bo,44); bo=ws_appq(body,bo,"temperature" as *u8); bo=ws_appc(body,bo,58); bo=ws_app(body,bo,"0.8" as *u8) 87 bo=ws_appc(body,bo,125) 88 } 89 body[bo] = 0 as u8 90 91 var port: i64 = K_MAGIC_11434 92 // /complete = the seat's RAW-continuation route (2026-07-10; /chat is now 93 // ChatML instruct -- wrong shape for this PROSE-continuation seam). 94 let path: *u8 = "/complete" as *u8 95 if is_img==1 { port = K_MAGIC_7861 } 96 var ipath: *u8 = path 97 if is_img==1 { ipath = "/generate" as *u8 } 98 99 let addr: *u8 = sys_mmap(16) 100 nx_http_client_sockaddr_ipv4(addr, 127, 0, 0, 1, port) 101 let fd: i64 = sys_socket(2, 1, 0) 102 if fd < 0 { gw("SEAT socket-fail\n" as *u8); sys_exit(2); return 2 } 103 if nx_connect_bounded(fd, addr, 16, NX_CONN_DEFAULT_MS) < 0 { 104 sys_close(fd) 105 gw("SEAT " as *u8); if is_img==1 { gw("img" as *u8) } else { gw("llm" as *u8) } 106 gw(" -> CONN-FAIL (local seat not listening on 127.0.0.1:" as *u8); gn(port); gw(") -- wire is BUILT; bring the seat up to prove the round-trip\n" as *u8) 107 sys_exit(2); return 2 108 } 109 let ct: *u8 = "application/json" as *u8 110 let req: *u8 = sys_mmap(K_MAGIC_1310720 + bo) 111 let reqlen: i64 = nx_http_client_build_request_post(ipath, ws_strlen(ipath), "localhost" as *u8, 9, ct, ws_strlen(ct), body, bo, req) 112 sys_write(fd, req, reqlen) 113 let cap: i64 = K_MAGIC_8388608 114 let buf: *u8 = sys_mmap(cap) 115 let n: i64 = ws_drain(fd, buf, cap) 116 sys_close(fd) 117 var status: i64 = 0 118 if n > 12 { status = ws_atoi3(buf, 9) } 119 120 let ofd: i64 = sys_openat_wr("knowledge/staging/writehub_seat_out.txt" as *u8, 0x1a4) 121 if ofd >= 0 { sys_write(ofd, buf, n); sys_close(ofd) } 122 123 gw("SEAT " as *u8); if is_img==1 { gw("img" as *u8) } else { gw("llm" as *u8) } 124 gw(" 127.0.0.1:" as *u8); gn(port); gw(ipath); gw(" -> status=" as *u8); gn(status); gw(" bytes=" as *u8); gn(n) 125 gw(" (reply -> knowledge/staging/writehub_seat_out.txt)\n" as *u8) 126 if status >= 200 { if status < 400 { sys_exit(0); return 0 } } 127 sys_exit(3); return 3 128}