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/cortex/blob/main/tests/brain/mkimp.om # cortex aien-dev/cortex Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `cortex / tests / brain / mkimp.om` 163 lines ยท 2782 bytes Raw Blame History 1 note mkimp.om: Company Brain gate helper. usage: mkimp SPACE OWNER DATE FILE... 2 note Writes to stdout one pdrive corpus line per FILE (spec/PARITY.md corpus format): 3 note "brain-import TAB POST TAB /api/cortex/brain/import TAB aj TAB {JSON}" where the JSON body is 4 note {"space":SPACE,"owner":OWNER,"date":DATE,"text":}. 5 note The file is read and escaped in 65536-byte pieces, so no file size is refused here; the server's 6 note request limit (2,097,152 bytes) applies to the request. Exit 0 ok, 2 usage, 3 a file cannot be 7 note read, 4 write error. 8 global pa uchar 4096 9 global ib uchar 65536 10 global ob uchar 524288 11 global obn ulong 1 12 text u_use "usage: mkimp SPACE OWNER DATE FILE...\n" 13 text u_open "mkimp: cannot read a file\n" 14 text u_wr "mkimp: write error\n" 15 text l_head "brain-import\tPOST\t/api/cortex/brain/import\taj\t{\"space\":" 16 text l_own ",\"owner\":" 17 text l_date ",\"date\":" 18 text l_text ",\"text\":\"" 19 text l_tail "\"}\n" 20 text hx "`0123456789abcdef`" 21 22 fn say t view uchar * to void 23 w = write 2 t 0 len t 24 if lt w 0 25 ret 26 end 27 end 28 fn argp i ulong to ulong 29 n = argread i pa 0 len pa 30 if lt n 0 31 ret 0 32 end 33 ret ulong n 34 end 35 fn flush to bool 36 n = get obn 0 37 k = ulong 0 38 for it ulong `4611686018427387904` 39 if ge k n 40 break 41 end 42 w = write 1 ob k sub n k 43 if le w 0 44 ret false 45 end 46 k = add k ulong w 47 end 48 put obn 0 0 49 ret true 50 end 51 fn ob1 c uchar to bool 52 if ge get obn 0 len ob 53 if not flush 54 ret false 55 end 56 end 57 n = get obn 0 58 put ob n c 59 put obn 0 add n 1 60 ret true 61 end 62 fn obt t view uchar * to bool 63 for i ulong len t 64 if not ob1 get t i 65 ret false 66 end 67 end 68 ret true 69 end 70 note one byte as JSON string content 71 fn esc c uchar to bool 72 d = uchar 0 73 if eq c 34 74 d = 34 75 end 76 if eq c 92 77 d = 92 78 end 79 if eq c 10 80 d = 110 81 end 82 if eq c 9 83 d = 116 84 end 85 if ne d 0 86 ok = ob1 92 87 ret and ok ob1 d 88 end 89 if lt c 32 90 ok = ob1 92 91 ok = and ok ob1 117 92 ok = and ok ob1 48 93 ok = and ok ob1 48 94 ok = and ok ob1 get hx shr c 4 95 ok = and ok ob1 get hx and c 15 96 ret ok 97 end 98 ret ob1 c 99 end 100 note argument i as a JSON string 101 fn argjs i ulong to bool 102 n = argp i 103 ok = ob1 34 104 for k ulong n 105 ok = and ok esc get pa k 106 end 107 ret and ok ob1 34 108 end 109 fn onefile i ulong to int 110 n = argp i 111 fd = fopen pa 0 n 524288 0 112 if lt fd 0 113 say u_open 114 ret 3 115 end 116 ok = obt l_head 117 ok = and ok argjs 1 118 ok = and ok obt l_own 119 ok = and ok argjs 2 120 ok = and ok obt l_date 121 ok = and ok argjs 3 122 ok = and ok obt l_text 123 for it ulong `4611686018427387904` 124 r = read int fd ib 0 len ib 125 if lt r 0 126 x = fclose int fd 127 say u_open 128 ret 3 129 end 130 if eq r 0 131 break 132 end 133 for k ulong ulong r 134 ok = and ok esc get ib k 135 end 136 end 137 x = fclose int fd 138 ok = and ok obt l_tail 139 if not ok 140 say u_wr 141 ret 4 142 end 143 ret 0 144 end 145 fn main to int 146 c = ulong argcnt 147 if lt c 5 148 say u_use 149 ret 2 150 end 151 put obn 0 0 152 for i ulong sub c 4 153 r = onefile add i 4 154 if ne r 0 155 ret r 156 end 157 end 158 if not flush 159 say u_wr 160 ret 4 161 end 162 ret 0 163 end