code wiki / (root) / nx_resource.nx

nx_resource.nx source

↩ module page · 41 lines · 2384 B

1// nx_resource.nx -- MCP Resources-primitive parity, sovereign: register read-only data resources (uri / description / 2// kind / source) and DISCOVER + READ them. Composes the canonical nx_registry (register/list on seg_store) + file 3// content. Record (tab-separated): uri <TAB> description <TAB> kind <TAB> source. kind "file" reads `source` as a path. 4// license_tier: ORIGINAL 5import "nx_registry.nx" 6import "nx_tabrec.nx" 7const RES_MAGIC_2048: i64 = 2048 8 9const RES_KP: *u8 = "res:" as *u8 10const RES_IDX: *u8 = "__residx__" as *u8 11const RES_PREFIX: *u8 = "knowledge/resreg-" as *u8 12 13func resource_register_pfx(prefix: *u8, uri: *u8, desc: *u8, kind: *u8, source: *u8) -> i64 { 14 let rec: *u8 = sys_mmap(RES_MAGIC_2048); var o: i64=0 15 o=tr_cat(rec,o,uri); o=tr_tab(rec,o); o=tr_cat(rec,o,desc); o=tr_tab(rec,o); o=tr_cat(rec,o,kind); o=tr_tab(rec,o); o=tr_cat(rec,o,source) 16 return reg_put(prefix, RES_KP, RES_IDX, uri, rec, o) 17} 18func resource_list_pfx(prefix: *u8, idxbuf: *u8, cap: i64) -> i64 { return reg_index(prefix, RES_IDX, idxbuf, cap) } 19 20// read the resource's content into outbuf (<= cap bytes); returns length, or 0 if absent / unsupported / read-fail. 21func resource_read_pfx(prefix: *u8, uri: *u8, outbuf: *u8, cap: i64) -> i64 { 22 let po: *i64=sys_mmap(16) as *i64; let lo: *i64=sys_mmap(16) as *i64 23 if reg_get(prefix, RES_KP, uri, po, lo) != 1 { return 0 } 24 let rec: *u8 = po[0] as *u8; let rlen: i64 = lo[0] 25 let f2: *i64 = sys_mmap(16) as *i64 26 if tr_field_eq(rec, rlen, 2, "file" as *u8, f2)==0 { return 0 } 27 if tr_field(rec, 0, rlen, 3, f2)==0 { return 0 } 28 let src: *u8 = sys_mmap(RES_MAGIC_2048); var i: i64=0; while i<f2[1] { src[i]=rec[f2[0]+i]; i=i+1 } src[f2[1]]=0 as u8 29 let box: *i64=sys_mmap(16) as *i64 30 let buf: *u8 = sys_read_file(src, box) 31 if buf == (0 as *u8) { return 0 } 32 let n: i64 = box[0] 33 var m: i64 = n; if m > cap { m = cap } 34 var k: i64=0; while k<m { outbuf[k]=buf[k]; k=k+1 } 35 return m 36} 37 38// production-prefix wrappers 39func resource_register(uri: *u8, desc: *u8, kind: *u8, source: *u8) -> i64 { return resource_register_pfx(RES_PREFIX, uri, desc, kind, source) } 40func resource_list(idxbuf: *u8, cap: i64) -> i64 { return resource_list_pfx(RES_PREFIX, idxbuf, cap) } 41func resource_read(uri: *u8, outbuf: *u8, cap: i64) -> i64 { return resource_read_pfx(RES_PREFIX, uri, outbuf, cap) }