code wiki / _hdl_build / nx_vizsla_capture.nx
nx_vizsla_capture.nx source
↩ module page · 186 lines · 7434 B
1// nx_vizsla_capture.nx -- NISHI VIZSLA: the OPT-IN page->person extractor for "capture as you browse".
2// Operator 2026-06-23: "lets NOT make it automatic but an OPTION to add to our personal/professional/business
3// relationship manager." So this NEVER auto-runs and NEVER auto-adds: it is invoked on demand (a button in the
4// Nishi browser), it parses the page YOU are looking at locally (no API, no upload), and emits a SUGGESTION you
5// then confirm into a chosen realm (personal|professional|business). Extraction priority (sovereign, structured):
6// 1) JSON-LD <script type="application/ld+json"> ... "@type":"Person" ... "name":"<N>"
7// 2) OpenGraph <meta property="og:title" content="<N> | site">
8// 3) <title> <title><N> | site</title>
9// The site suffix (" | ...", " - ...") is stripped; the name is sanitized to an engine handle. Output feeds the
10// existing action controller (id=&name=) -> the operator picks the realm at confirm time. license_tier: ORIGINAL
11import "nx_syscalls.nx"
12const K_MAGIC_65536: i64 = 65536
13const K_MAGIC_1024: i64 = 1024
14
15func cp_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
16func cp_p(s: *u8) -> i64 { sys_write(1, s, cp_slen(s)); return 0 }
17
18func cp_cat(dst: *u8, off: i64, s: *u8) -> i64 {
19 var i: i64 = 0
20 while s[i] != (0 as u8) { dst[off + i] = s[i]; i = i + 1 }
21 return off + i
22}
23
24func cp_readall(path: *u8, szp: *i64) -> *u8 {
25 let fd: i64 = sys_openat_rd(path)
26 if fd < 0 { szp[0] = 0; return 0 as *u8 }
27 let sz: i64 = sys_lseek(fd, 0, 2)
28 sys_lseek(fd, 0, 0)
29 let buf: *u8 = sys_mmap(sz + 64)
30 var got: i64 = 0
31 var n: i64 = 1
32 while n > 0 { n = sys_read(fd, (buf as i64 + got) as *u8, K_MAGIC_65536); if n > 0 { got = got + n } }
33 sys_close(fd)
34 szp[0] = got
35 return buf
36}
37
38// index of needle in buf[from,n) or -1
39func cp_find(buf: *u8, n: i64, needle: *u8, from: i64) -> i64 {
40 let nl: i64 = cp_slen(needle)
41 if nl == 0 { return 0 - 1 }
42 var i: i64 = from
43 while i + nl <= n {
44 var ok: i64 = 1
45 var j: i64 = 0
46 while j < nl { if buf[i + j] != needle[j] { ok = 0; j = nl } else { j = j + 1 } }
47 if ok == 1 { return i }
48 i = i + 1
49 }
50 return 0 - 1
51}
52
53// sanitize "John Smith" -> disp "John-Smith" + id "john-smith"; returns length (0 = empty)
54func cp_san(src: *u8, disp: *u8, id: *u8) -> i64 {
55 var i: i64 = 0
56 var o: i64 = 0
57 var lastsep: i64 = 1
58 while src[i] != (0 as u8) {
59 let c: i64 = src[i] as i64
60 var isaln: i64 = 0
61 if c >= 48 { if c <= 57 { isaln = 1 } }
62 if c >= 65 { if c <= 90 { isaln = 1 } }
63 if c >= 97 { if c <= 122 { isaln = 1 } }
64 if isaln == 1 {
65 disp[o] = c as u8
66 var lc: i64 = c
67 if c >= 65 { if c <= 90 { lc = c + 32 } }
68 id[o] = lc as u8
69 o = o + 1; lastsep = 0
70 } else {
71 if lastsep == 0 { disp[o] = 45 as u8; id[o] = 45 as u8; o = o + 1; lastsep = 1 }
72 }
73 i = i + 1
74 }
75 if o > 0 { if disp[o - 1] == (45 as u8) { o = o - 1 } }
76 disp[o] = 0 as u8
77 id[o] = 0 as u8
78 return o
79}
80
81// truncate s at the first " | " or " - " (site suffix)
82func cp_strip_suffix(s: *u8) -> i64 {
83 var i: i64 = 0
84 var cut: i64 = 0 - 1
85 while s[i] != (0 as u8) {
86 if cut < 0 {
87 if s[i] == (32 as u8) {
88 let a: i64 = s[i + 1] as i64
89 if a == 124 { if s[i + 2] == (32 as u8) { cut = i } }
90 if a == 45 { if s[i + 2] == (32 as u8) { cut = i } }
91 }
92 }
93 i = i + 1
94 }
95 if cut >= 0 { s[cut] = 0 as u8 }
96 return 0
97}
98
99// JSON-LD Person "name":"..." -> out; 1 if found
100func cp_jsonld(buf: *u8, n: i64, out: *u8) -> i64 {
101 if cp_find(buf, n, "application/ld+json" as *u8, 0) < 0 { return 0 }
102 if cp_find(buf, n, "Person" as *u8, 0) < 0 { return 0 }
103 let ni: i64 = cp_find(buf, n, "\"name\"" as *u8, 0)
104 if ni < 0 { return 0 }
105 var p: i64 = ni + 6
106 while p < n { if buf[p] == (58 as u8) { break } p = p + 1 }
107 while p < n { if buf[p] == (34 as u8) { break } p = p + 1 }
108 p = p + 1
109 var o: i64 = 0
110 while p < n { if buf[p] == (34 as u8) { break } if o < 255 { out[o] = buf[p]; o = o + 1 } p = p + 1 }
111 out[o] = 0 as u8
112 if o > 0 { return 1 }
113 return 0
114}
115
116// OpenGraph og:title content -> out; 1 if found
117func cp_og(buf: *u8, n: i64, out: *u8) -> i64 {
118 let oi: i64 = cp_find(buf, n, "og:title" as *u8, 0)
119 if oi < 0 { return 0 }
120 let ci: i64 = cp_find(buf, n, "content=\"" as *u8, oi)
121 if ci < 0 { return 0 }
122 var p: i64 = ci + 9
123 var o: i64 = 0
124 while p < n { if buf[p] == (34 as u8) { break } if o < 255 { out[o] = buf[p]; o = o + 1 } p = p + 1 }
125 out[o] = 0 as u8
126 if o > 0 { return 1 }
127 return 0
128}
129
130// <title> -> out; 1 if found
131func cp_title(buf: *u8, n: i64, out: *u8) -> i64 {
132 let ti: i64 = cp_find(buf, n, "<title>" as *u8, 0)
133 if ti < 0 { return 0 }
134 var p: i64 = ti + 7
135 var o: i64 = 0
136 while p < n { if buf[p] == (60 as u8) { break } if o < 255 { out[o] = buf[p]; o = o + 1 } p = p + 1 }
137 out[o] = 0 as u8
138 if o > 0 { return 1 }
139 return 0
140}
141
142func cp_cmd_extract(argc: i64, argv: *i64) -> i64 {
143 if argc < 3 { cp_p("VIZSLA-CAPTURE extract needs <htmlfile> -- fail loud\n" as *u8); return 1 }
144 let szp: *i64 = sys_mmap(16) as *i64
145 let buf: *u8 = cp_readall(argv[2] as *u8, szp)
146 let n: i64 = szp[0]
147 if n <= 0 { cp_p("VIZSLA-CAPTURE html MISSING/EMPTY -- fail loud\n" as *u8); return 1 }
148
149 let raw: *u8 = sys_mmap(512)
150 var src: *u8 = "none" as *u8
151 var found: i64 = 0
152 if cp_jsonld(buf, n, raw) == 1 { src = "jsonld" as *u8; found = 1 }
153 if found == 0 { if cp_og(buf, n, raw) == 1 { cp_strip_suffix(raw); src = "og" as *u8; found = 1 } }
154 if found == 0 { if cp_title(buf, n, raw) == 1 { cp_strip_suffix(raw); src = "title" as *u8; found = 1 } }
155 if found == 0 { cp_p("VIZSLA-CAPTURE no person found on this page (no JSON-LD Person / og:title / <title>) -- fail loud\n" as *u8); return 1 }
156
157 let disp: *u8 = sys_mmap(256)
158 let id: *u8 = sys_mmap(256)
159 if cp_san(raw, disp, id) == 0 { cp_p("VIZSLA-CAPTURE extracted name is empty after sanitize -- fail loud\n" as *u8); return 1 }
160
161 let rep: *u8 = sys_mmap(K_MAGIC_1024)
162 var o: i64 = 0
163 o = cp_cat(rep, o, "VIZSLA-CAPTURE id=" as *u8); o = cp_cat(rep, o, id)
164 o = cp_cat(rep, o, " name=" as *u8); o = cp_cat(rep, o, disp)
165 o = cp_cat(rep, o, " source=" as *u8); o = cp_cat(rep, o, src); o = cp_cat(rep, o, "\n" as *u8)
166 // the suggestion as an action-ready urlencoded body (the operator picks the realm at confirm time)
167 o = cp_cat(rep, o, "VIZSLA-CAPTURE-FORM id=" as *u8); o = cp_cat(rep, o, id)
168 o = cp_cat(rep, o, "&name=" as *u8); o = cp_cat(rep, o, disp); o = cp_cat(rep, o, "\n" as *u8)
169 sys_write(1, rep, o)
170 return 0
171}
172
173func cp_eqc(a: *u8, b: *u8) -> i64 {
174 var i: i64 = 0
175 var go: i64 = 1
176 while go == 1 { if a[i] != b[i] { return 0 } if a[i] == (0 as u8) { return 1 } i = i + 1 }
177 return 0
178}
179
180func main(argc: i64, argv: *i64) -> i64 {
181 if argc < 2 { cp_p("VIZSLA-CAPTURE usage: extract <htmlfile> -- fail loud\n" as *u8); return 1 }
182 let cmd: *u8 = argv[1] as *u8
183 if cp_eqc(cmd, "extract" as *u8) == 1 { return cp_cmd_extract(argc, argv) }
184 cp_p("VIZSLA-CAPTURE unknown command (extract) -- fail loud\n" as *u8)
185 return 1
186}