code wiki / (root) / nx_workflow_ingest.nx

nx_workflow_ingest.nx source

↩ module page · 112 lines · 4115 B

1// nx_workflow_ingest.nx -- SOVEREIGN media -> ComfyUI workflow extractor (piece 1 of the anti-ComfyUI ingest). 2// 3// ComfyUI embeds the gen-graph as JSON in media metadata (PNG tEXt chunks; MP4/WebM atoms): "workflow" 4// (UI graph, nodes[]) + "prompt" (API graph, class_type). This reads a media file, finds a marker, and 5// extracts the balanced JSON (string-aware brace matching), writes it out, and counts nodes. No 3rd party. 6// license_tier: ORIGINAL 7import "nx_syscalls.nx" 8import "nx_le.nx" 9import "nx_strconv.nx" 10const K_MAGIC_8388608: i64 = 8388608 11const K_MAGIC_1048576: i64 = 1048576 12 13// find needle[nlen] in buf[0..len) from start; -1 if absent 14func wi_find(buf: *u8, len: i64, needle: *u8, nlen: i64, start: i64) -> i64 { 15 var i: i64 = start 16 while i <= len - nlen { 17 var j: i64 = 0 18 var ok: i64 = 1 19 while j < nlen { if buf[i + j] != needle[j] { ok = 0; j = nlen } else { j = j + 1 } } 20 if ok == 1 { return i } 21 i = i + 1 22 } 23 return 0 - 1 24} 25 26// count occurrences of needle in buf[0..len) 27func wi_count(buf: *u8, len: i64, needle: *u8, nlen: i64) -> i64 { 28 var c: i64 = 0 29 var at: i64 = wi_find(buf, len, needle, nlen, 0) 30 while at >= 0 { c = c + 1; at = wi_find(buf, len, needle, nlen, at + nlen) } 31 return c 32} 33 34// extract balanced {..} starting at buf[start]=='{', string+escape aware, into out; returns length 35func wi_balanced(buf: *u8, len: i64, start: i64, out: *u8, cap: i64) -> i64 { 36 var depth: i64 = 0 37 var i: i64 = start 38 var instr: i64 = 0 39 var esc: i64 = 0 40 var o: i64 = 0 41 while i < len { 42 let c: i64 = buf[i] 43 if instr == 1 { 44 if esc == 1 { esc = 0 } else { if c == 0x5C { esc = 1 } else { if c == 0x22 { instr = 0 } } } 45 } else { 46 if c == 0x22 { instr = 1 } else { if c == 0x7B { depth = depth + 1 } else { if c == 0x7D { depth = depth - 1 } } } 47 } 48 if o < cap { out[o] = c } 49 o = o + 1 50 if depth == 0 { if c == 0x7D { return o } } 51 i = i + 1 52 } 53 return o 54} 55 56func wi_emit(fd: i64, key: *u8, kl: i64, v: i64) -> i64 { 57 let line: *u8 = sys_mmap(64) 58 var lo: i64 = 0 59 var i: i64 = 0 60 while i < kl { line[lo] = key[i]; lo = lo + 1; i = i + 1 } 61 line[lo] = 0x3D; lo = lo + 1 62 let dec: *u8 = sys_mmap(32) 63 let nd: i64 = nx_strconv_format_i64(v, dec) 64 var k: i64 = 0 65 while k < nd { line[lo] = dec[k]; lo = lo + 1; k = k + 1 } 66 line[lo] = 0x0A; lo = lo + 1 67 return sys_write(fd, line, lo) 68} 69 70func main() -> i64 { 71 let path: *u8 = "/mnt/c/Users/elder/Downloads/x.mp4" as *u8 72 let fd: i64 = sys_openat_rd(path) 73 if fd < 0 { return 30 } 74 let CAP: i64 = K_MAGIC_8388608 // 8 MB 75 let buf: *u8 = sys_mmap(CAP) 76 var total: i64 = 0 77 var go: i64 = 1 78 while go == 1 { 79 let r: i64 = sys_read(fd, ((buf as i64) + total) as *u8, CAP - total) 80 if r <= 0 { go = 0 } else { total = total + r; if total >= CAP { go = 0 } } 81 } 82 sys_close(fd) 83 if total < 1000 { return 31 } 84 85 // find the "workflow" marker, then the '{' after it 86 let mk: i64 = wi_find(buf, total, "workflow" as *u8, 8, 0) 87 if mk < 0 { return 40 } 88 let br: i64 = wi_find(buf, total, "{" as *u8, 1, mk) 89 if br < 0 { return 41 } 90 91 let ocap: i64 = K_MAGIC_1048576 // 1 MB out 92 let out: *u8 = sys_mmap(ocap) 93 let jlen: i64 = wi_balanced(buf, total, br, out, ocap) 94 95 // write extracted workflow 96 let wfd: i64 = sys_openat_wr("/tmp/extracted_workflow.json" as *u8, 0x1a4) 97 if wfd >= 0 { sys_write(wfd, out, jlen); sys_close(wfd) } 98 99 // count nodes ("type": appears once per node in the UI graph) 100 let nodes: i64 = wi_count(out, jlen, "\"type\":" as *u8, 7) 101 102 let rfd: i64 = sys_openat_wr("/tmp/wi_report.txt" as *u8, 0x1a4) 103 if rfd >= 0 { 104 wi_emit(rfd, "file_bytes" as *u8, 10, total) 105 wi_emit(rfd, "workflow_at" as *u8, 11, mk) 106 wi_emit(rfd, "json_bytes" as *u8, 10, jlen) 107 wi_emit(rfd, "node_count" as *u8, 10, nodes) 108 sys_close(rfd) 109 } 110 if jlen < 100 { return 20 } 111 return 0 112}