@cogitator-ai/wasm-tools 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/README.md +141 -6
  2. package/dist/index.d.ts +227 -0
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +194 -0
  5. package/dist/index.js.map +1 -1
  6. package/dist/plugins/compression.d.ts +2 -0
  7. package/dist/plugins/compression.d.ts.map +1 -0
  8. package/dist/plugins/compression.js +530 -0
  9. package/dist/plugins/compression.js.map +1 -0
  10. package/dist/plugins/csv.d.ts +2 -0
  11. package/dist/plugins/csv.d.ts.map +1 -0
  12. package/dist/plugins/csv.js +138 -0
  13. package/dist/plugins/csv.js.map +1 -0
  14. package/dist/plugins/datetime.d.ts +2 -0
  15. package/dist/plugins/datetime.d.ts.map +1 -0
  16. package/dist/plugins/datetime.js +184 -0
  17. package/dist/plugins/datetime.js.map +1 -0
  18. package/dist/plugins/diff.d.ts +2 -0
  19. package/dist/plugins/diff.d.ts.map +1 -0
  20. package/dist/plugins/diff.js +187 -0
  21. package/dist/plugins/diff.js.map +1 -0
  22. package/dist/plugins/markdown.d.ts +2 -0
  23. package/dist/plugins/markdown.d.ts.map +1 -0
  24. package/dist/plugins/markdown.js +203 -0
  25. package/dist/plugins/markdown.js.map +1 -0
  26. package/dist/plugins/regex.d.ts +2 -0
  27. package/dist/plugins/regex.d.ts.map +1 -0
  28. package/dist/plugins/regex.js +119 -0
  29. package/dist/plugins/regex.js.map +1 -0
  30. package/dist/plugins/signing.d.ts +2 -0
  31. package/dist/plugins/signing.d.ts.map +1 -0
  32. package/dist/plugins/signing.js +565 -0
  33. package/dist/plugins/signing.js.map +1 -0
  34. package/dist/plugins/slug.d.ts +2 -0
  35. package/dist/plugins/slug.d.ts.map +1 -0
  36. package/dist/plugins/slug.js +180 -0
  37. package/dist/plugins/slug.js.map +1 -0
  38. package/dist/plugins/validation.d.ts +2 -0
  39. package/dist/plugins/validation.d.ts.map +1 -0
  40. package/dist/plugins/validation.js +105 -0
  41. package/dist/plugins/validation.js.map +1 -0
  42. package/dist/plugins/xml.d.ts +2 -0
  43. package/dist/plugins/xml.d.ts.map +1 -0
  44. package/dist/plugins/xml.js +293 -0
  45. package/dist/plugins/xml.js.map +1 -0
  46. package/dist/temp/compression.js +614 -0
  47. package/dist/temp/csv.js +154 -0
  48. package/dist/temp/datetime.js +196 -0
  49. package/dist/temp/diff.js +198 -0
  50. package/dist/temp/markdown.js +220 -0
  51. package/dist/temp/regex.js +140 -0
  52. package/dist/temp/signing.js +569 -0
  53. package/dist/temp/slug.js +198 -0
  54. package/dist/temp/validation.js +125 -0
  55. package/dist/temp/xml.js +289 -0
  56. package/dist/wasm/base64.wasm +0 -0
  57. package/dist/wasm/calc.wasm +0 -0
  58. package/dist/wasm/compression.wasm +0 -0
  59. package/dist/wasm/csv.wasm +0 -0
  60. package/dist/wasm/datetime.wasm +0 -0
  61. package/dist/wasm/diff.wasm +0 -0
  62. package/dist/wasm/hash.wasm +0 -0
  63. package/dist/wasm/json.wasm +0 -0
  64. package/dist/wasm/markdown.wasm +0 -0
  65. package/dist/wasm/regex.wasm +0 -0
  66. package/dist/wasm/signing.wasm +0 -0
  67. package/dist/wasm/slug.wasm +0 -0
  68. package/dist/wasm/validation.wasm +0 -0
  69. package/dist/wasm/xml.wasm +0 -0
  70. package/package.json +2 -2
@@ -0,0 +1,198 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/plugins/slug.ts
21
+ var slug_exports = {};
22
+ __export(slug_exports, {
23
+ slug: () => slug
24
+ });
25
+ module.exports = __toCommonJS(slug_exports);
26
+ var CHAR_MAP = {
27
+ \u00E0: "a",
28
+ \u00E1: "a",
29
+ \u00E2: "a",
30
+ \u00E3: "a",
31
+ \u00E4: "a",
32
+ \u00E5: "a",
33
+ \u00E6: "ae",
34
+ \u00E7: "c",
35
+ \u00E8: "e",
36
+ \u00E9: "e",
37
+ \u00EA: "e",
38
+ \u00EB: "e",
39
+ \u00EC: "i",
40
+ \u00ED: "i",
41
+ \u00EE: "i",
42
+ \u00EF: "i",
43
+ \u00F0: "d",
44
+ \u00F1: "n",
45
+ \u00F2: "o",
46
+ \u00F3: "o",
47
+ \u00F4: "o",
48
+ \u00F5: "o",
49
+ \u00F6: "o",
50
+ \u00F8: "o",
51
+ \u00F9: "u",
52
+ \u00FA: "u",
53
+ \u00FB: "u",
54
+ \u00FC: "u",
55
+ \u00FD: "y",
56
+ \u00FF: "y",
57
+ \u00FE: "th",
58
+ \u00DF: "ss",
59
+ \u0105: "a",
60
+ \u0107: "c",
61
+ \u0119: "e",
62
+ \u0142: "l",
63
+ \u0144: "n",
64
+ \u015B: "s",
65
+ \u017A: "z",
66
+ \u017C: "z",
67
+ \u010D: "c",
68
+ \u010F: "d",
69
+ \u011B: "e",
70
+ \u0148: "n",
71
+ \u0159: "r",
72
+ \u0161: "s",
73
+ \u0165: "t",
74
+ \u016F: "u",
75
+ \u017E: "z",
76
+ \u0101: "a",
77
+ \u0113: "e",
78
+ \u0123: "g",
79
+ \u012B: "i",
80
+ \u0137: "k",
81
+ \u013C: "l",
82
+ \u0146: "n",
83
+ \u016B: "u",
84
+ \u0151: "o",
85
+ \u0171: "u",
86
+ \u0430: "a",
87
+ \u0431: "b",
88
+ \u0432: "v",
89
+ \u0433: "g",
90
+ \u0434: "d",
91
+ \u0435: "e",
92
+ \u0451: "yo",
93
+ \u0436: "zh",
94
+ \u0437: "z",
95
+ \u0438: "i",
96
+ \u0439: "y",
97
+ \u043A: "k",
98
+ \u043B: "l",
99
+ \u043C: "m",
100
+ \u043D: "n",
101
+ \u043E: "o",
102
+ \u043F: "p",
103
+ \u0440: "r",
104
+ \u0441: "s",
105
+ \u0442: "t",
106
+ \u0443: "u",
107
+ \u0444: "f",
108
+ \u0445: "h",
109
+ \u0446: "ts",
110
+ \u0447: "ch",
111
+ \u0448: "sh",
112
+ \u0449: "sch",
113
+ \u044A: "",
114
+ \u044B: "y",
115
+ \u044C: "",
116
+ \u044D: "e",
117
+ \u044E: "yu",
118
+ \u044F: "ya",
119
+ \u03B1: "a",
120
+ \u03B2: "b",
121
+ \u03B3: "g",
122
+ \u03B4: "d",
123
+ \u03B5: "e",
124
+ \u03B6: "z",
125
+ \u03B7: "h",
126
+ \u03B8: "th",
127
+ \u03B9: "i",
128
+ \u03BA: "k",
129
+ \u03BB: "l",
130
+ \u03BC: "m",
131
+ \u03BD: "n",
132
+ \u03BE: "x",
133
+ \u03BF: "o",
134
+ \u03C0: "p",
135
+ \u03C1: "r",
136
+ \u03C3: "s",
137
+ \u03C4: "t",
138
+ \u03C5: "y",
139
+ \u03C6: "ph",
140
+ \u03C7: "ch",
141
+ \u03C8: "ps",
142
+ \u03C9: "o"
143
+ };
144
+ function transliterate(str) {
145
+ let result = "";
146
+ for (const char of str) {
147
+ const lower = char.toLowerCase();
148
+ if (CHAR_MAP[lower]) {
149
+ const mapped = CHAR_MAP[lower];
150
+ result += char === lower ? mapped : mapped.toUpperCase();
151
+ } else {
152
+ result += char;
153
+ }
154
+ }
155
+ return result;
156
+ }
157
+ function generateSlug(text, separator, lowercase, maxLength) {
158
+ let slug2 = transliterate(text);
159
+ if (lowercase) {
160
+ slug2 = slug2.toLowerCase();
161
+ }
162
+ slug2 = slug2.replace(/[^a-zA-Z0-9\s-]/g, "").replace(/\s+/g, separator).replace(new RegExp(`${escapeRegex(separator)}+`, "g"), separator).replace(new RegExp(`^${escapeRegex(separator)}|${escapeRegex(separator)}$`, "g"), "");
163
+ if (maxLength && slug2.length > maxLength) {
164
+ slug2 = slug2.substring(0, maxLength);
165
+ const lastSep = slug2.lastIndexOf(separator);
166
+ if (lastSep > maxLength * 0.5) {
167
+ slug2 = slug2.substring(0, lastSep);
168
+ }
169
+ slug2 = slug2.replace(new RegExp(`${escapeRegex(separator)}$`), "");
170
+ }
171
+ return slug2;
172
+ }
173
+ function escapeRegex(str) {
174
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
175
+ }
176
+ function slug() {
177
+ try {
178
+ const inputStr = Host.inputString();
179
+ const input = JSON.parse(inputStr);
180
+ const separator = input.separator ?? "-";
181
+ const lowercase = input.lowercase ?? true;
182
+ const result = generateSlug(input.text, separator, lowercase, input.maxLength);
183
+ const output = {
184
+ slug: result,
185
+ original: input.text
186
+ };
187
+ Host.outputString(JSON.stringify(output));
188
+ return 0;
189
+ } catch (error) {
190
+ const output = {
191
+ slug: "",
192
+ original: "",
193
+ error: error instanceof Error ? error.message : String(error)
194
+ };
195
+ Host.outputString(JSON.stringify(output));
196
+ return 1;
197
+ }
198
+ }
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/plugins/validation.ts
21
+ var validation_exports = {};
22
+ __export(validation_exports, {
23
+ validate: () => validate
24
+ });
25
+ module.exports = __toCommonJS(validation_exports);
26
+ var EMAIL_REGEX = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
27
+ var UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
28
+ var IPV4_REGEX = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
29
+ var IPV6_REGEX = /^(?:(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,7}:|(?:[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,5}(?::[0-9a-fA-F]{1,4}){1,2}|(?:[0-9a-fA-F]{1,4}:){1,4}(?::[0-9a-fA-F]{1,4}){1,3}|(?:[0-9a-fA-F]{1,4}:){1,3}(?::[0-9a-fA-F]{1,4}){1,4}|(?:[0-9a-fA-F]{1,4}:){1,2}(?::[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:(?::[0-9a-fA-F]{1,4}){1,6}|:(?::[0-9a-fA-F]{1,4}){1,7}|::(?:[fF]{4}:)?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|(?:[0-9a-fA-F]{1,4}:){1,4}:(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))$/;
30
+ function validateEmail(value) {
31
+ const trimmed = value.trim().toLowerCase();
32
+ const valid = EMAIL_REGEX.test(trimmed) && trimmed.length <= 254;
33
+ return { valid, normalized: valid ? trimmed : void 0 };
34
+ }
35
+ function validateUrl(value) {
36
+ try {
37
+ const trimmed = value.trim();
38
+ const hasProtocol = /^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//.test(trimmed);
39
+ const urlStr = hasProtocol ? trimmed : `https://${trimmed}`;
40
+ const parts = urlStr.match(/^([a-zA-Z][a-zA-Z0-9+.-]*):\/\/([^/?#]+)(.*)$/);
41
+ if (!parts) return { valid: false };
42
+ const [, protocol, host] = parts;
43
+ if (!["http", "https", "ftp", "ftps"].includes(protocol.toLowerCase())) {
44
+ return { valid: false };
45
+ }
46
+ const hostParts = host.split(":");
47
+ const hostname = hostParts[0];
48
+ const port = hostParts[1];
49
+ if (port && (!/^\d+$/.test(port) || parseInt(port) > 65535)) {
50
+ return { valid: false };
51
+ }
52
+ if (!/^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)*$/.test(
53
+ hostname
54
+ )) {
55
+ if (!IPV4_REGEX.test(hostname) && !hostname.startsWith("[")) {
56
+ return { valid: false };
57
+ }
58
+ }
59
+ return { valid: true, normalized: urlStr };
60
+ } catch {
61
+ return { valid: false };
62
+ }
63
+ }
64
+ function validateUuid(value) {
65
+ const trimmed = value.trim().toLowerCase();
66
+ const valid = UUID_REGEX.test(trimmed);
67
+ return { valid, normalized: valid ? trimmed : void 0 };
68
+ }
69
+ function validateIpv4(value) {
70
+ const trimmed = value.trim();
71
+ const valid = IPV4_REGEX.test(trimmed);
72
+ if (valid) {
73
+ const normalized = trimmed.split(".").map((n) => parseInt(n, 10).toString()).join(".");
74
+ return { valid: true, normalized };
75
+ }
76
+ return { valid: false };
77
+ }
78
+ function validateIpv6(value) {
79
+ const trimmed = value.trim().toLowerCase();
80
+ const valid = IPV6_REGEX.test(trimmed);
81
+ return { valid, normalized: valid ? trimmed : void 0 };
82
+ }
83
+ function validate() {
84
+ try {
85
+ const inputStr = Host.inputString();
86
+ const input = JSON.parse(inputStr);
87
+ let result;
88
+ switch (input.type) {
89
+ case "email":
90
+ result = validateEmail(input.value);
91
+ break;
92
+ case "url":
93
+ result = validateUrl(input.value);
94
+ break;
95
+ case "uuid":
96
+ result = validateUuid(input.value);
97
+ break;
98
+ case "ipv4":
99
+ result = validateIpv4(input.value);
100
+ break;
101
+ case "ipv6":
102
+ result = validateIpv6(input.value);
103
+ break;
104
+ default:
105
+ throw new Error(`Unknown validation type: ${input.type}`);
106
+ }
107
+ const output = {
108
+ valid: result.valid,
109
+ type: input.type,
110
+ value: input.value,
111
+ normalized: result.normalized
112
+ };
113
+ Host.outputString(JSON.stringify(output));
114
+ return 0;
115
+ } catch (error) {
116
+ const output = {
117
+ valid: false,
118
+ type: "unknown",
119
+ value: "",
120
+ error: error instanceof Error ? error.message : String(error)
121
+ };
122
+ Host.outputString(JSON.stringify(output));
123
+ return 1;
124
+ }
125
+ }
@@ -0,0 +1,289 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/plugins/xml.ts
21
+ var xml_exports = {};
22
+ __export(xml_exports, {
23
+ xml: () => xml
24
+ });
25
+ module.exports = __toCommonJS(xml_exports);
26
+ function parseXml(xml2) {
27
+ let pos = 0;
28
+ const skipWhitespace = () => {
29
+ while (pos < xml2.length && /\s/.test(xml2[pos])) pos++;
30
+ };
31
+ const parseAttributes = () => {
32
+ const attrs = {};
33
+ while (pos < xml2.length) {
34
+ skipWhitespace();
35
+ if (xml2[pos] === ">" || xml2[pos] === "/" || xml2[pos] === "?") break;
36
+ let name = "";
37
+ while (pos < xml2.length && /[a-zA-Z0-9_:-]/.test(xml2[pos])) {
38
+ name += xml2[pos++];
39
+ }
40
+ if (!name) break;
41
+ skipWhitespace();
42
+ if (xml2[pos] !== "=") {
43
+ attrs[name] = "true";
44
+ continue;
45
+ }
46
+ pos++;
47
+ skipWhitespace();
48
+ const quote = xml2[pos];
49
+ if (quote !== '"' && quote !== "'") {
50
+ throw new Error(`Expected quote at position ${pos}`);
51
+ }
52
+ pos++;
53
+ let value = "";
54
+ while (pos < xml2.length && xml2[pos] !== quote) {
55
+ if (xml2[pos] === "&") {
56
+ const entity = parseEntity();
57
+ value += entity;
58
+ } else {
59
+ value += xml2[pos++];
60
+ }
61
+ }
62
+ pos++;
63
+ attrs[name] = value;
64
+ }
65
+ return attrs;
66
+ };
67
+ const parseEntity = () => {
68
+ let entity = "";
69
+ pos++;
70
+ while (pos < xml2.length && xml2[pos] !== ";") {
71
+ entity += xml2[pos++];
72
+ }
73
+ pos++;
74
+ switch (entity) {
75
+ case "lt":
76
+ return "<";
77
+ case "gt":
78
+ return ">";
79
+ case "amp":
80
+ return "&";
81
+ case "quot":
82
+ return '"';
83
+ case "apos":
84
+ return "'";
85
+ default:
86
+ if (entity.startsWith("#x")) {
87
+ return String.fromCharCode(parseInt(entity.slice(2), 16));
88
+ } else if (entity.startsWith("#")) {
89
+ return String.fromCharCode(parseInt(entity.slice(1), 10));
90
+ }
91
+ return `&${entity};`;
92
+ }
93
+ };
94
+ const parseText = () => {
95
+ let text = "";
96
+ while (pos < xml2.length && xml2[pos] !== "<") {
97
+ if (xml2[pos] === "&") {
98
+ text += parseEntity();
99
+ } else {
100
+ text += xml2[pos++];
101
+ }
102
+ }
103
+ return text;
104
+ };
105
+ const parseNode = () => {
106
+ skipWhitespace();
107
+ if (pos >= xml2.length) return null;
108
+ if (xml2[pos] !== "<") {
109
+ const text = parseText().trim();
110
+ if (text) {
111
+ return { type: "text", value: text };
112
+ }
113
+ return null;
114
+ }
115
+ pos++;
116
+ if (xml2.slice(pos, pos + 3) === "!--") {
117
+ pos += 3;
118
+ const endPos = xml2.indexOf("-->", pos);
119
+ if (endPos === -1) throw new Error("Unclosed comment");
120
+ const value = xml2.slice(pos, endPos);
121
+ pos = endPos + 3;
122
+ return { type: "comment", value };
123
+ }
124
+ if (xml2.slice(pos, pos + 8) === "![CDATA[") {
125
+ pos += 8;
126
+ const endPos = xml2.indexOf("]]>", pos);
127
+ if (endPos === -1) throw new Error("Unclosed CDATA");
128
+ const value = xml2.slice(pos, endPos);
129
+ pos = endPos + 3;
130
+ return { type: "cdata", value };
131
+ }
132
+ if (xml2[pos] === "?") {
133
+ const endPos = xml2.indexOf("?>", pos);
134
+ if (endPos === -1) throw new Error("Unclosed processing instruction");
135
+ pos = endPos + 2;
136
+ return parseNode();
137
+ }
138
+ if (xml2[pos] === "!") {
139
+ const endPos = xml2.indexOf(">", pos);
140
+ if (endPos === -1) throw new Error("Unclosed declaration");
141
+ pos = endPos + 1;
142
+ return parseNode();
143
+ }
144
+ if (xml2[pos] === "/") {
145
+ return null;
146
+ }
147
+ let name = "";
148
+ while (pos < xml2.length && /[a-zA-Z0-9_:-]/.test(xml2[pos])) {
149
+ name += xml2[pos++];
150
+ }
151
+ if (!name) throw new Error(`Expected element name at position ${pos}`);
152
+ const attributes = parseAttributes();
153
+ skipWhitespace();
154
+ if (xml2.slice(pos, pos + 2) === "/>") {
155
+ pos += 2;
156
+ return { type: "element", name, attributes, children: [] };
157
+ }
158
+ if (xml2[pos] !== ">") {
159
+ throw new Error(`Expected > at position ${pos}`);
160
+ }
161
+ pos++;
162
+ const children = [];
163
+ while (pos < xml2.length) {
164
+ skipWhitespace();
165
+ if (xml2.slice(pos, pos + 2) === "</") {
166
+ pos += 2;
167
+ let closeName = "";
168
+ while (pos < xml2.length && /[a-zA-Z0-9_:-]/.test(xml2[pos])) {
169
+ closeName += xml2[pos++];
170
+ }
171
+ skipWhitespace();
172
+ if (xml2[pos] !== ">") throw new Error(`Expected > at position ${pos}`);
173
+ pos++;
174
+ if (closeName !== name) {
175
+ throw new Error(`Mismatched tags: ${name} vs ${closeName}`);
176
+ }
177
+ break;
178
+ }
179
+ const child = parseNode();
180
+ if (child) {
181
+ children.push(child);
182
+ } else if (xml2[pos] !== "<") {
183
+ break;
184
+ }
185
+ }
186
+ return { type: "element", name, attributes, children };
187
+ };
188
+ const root = parseNode();
189
+ if (!root) throw new Error("Empty document");
190
+ return root;
191
+ }
192
+ function queryXml(node, query) {
193
+ const parts = query.split("/").filter((p) => p);
194
+ if (parts.length === 0) return node;
195
+ let current = node;
196
+ for (let i = 0; i < parts.length; i++) {
197
+ const part = parts[i];
198
+ if (part.startsWith("@")) {
199
+ const attrName = part.slice(1);
200
+ if (Array.isArray(current)) {
201
+ return current.filter((n) => n.type === "element" && n.attributes?.[attrName]).map((n) => n.attributes[attrName]);
202
+ }
203
+ if (current.type === "element" && current.attributes?.[attrName]) {
204
+ return current.attributes[attrName];
205
+ }
206
+ return null;
207
+ }
208
+ const isRecursive = part === "";
209
+ if (isRecursive) {
210
+ i++;
211
+ if (i >= parts.length) return null;
212
+ const targetName = parts[i];
213
+ const results = [];
214
+ const findAll = (n) => {
215
+ if (n.type === "element") {
216
+ if (n.name === targetName) results.push(n);
217
+ if (n.children) n.children.forEach(findAll);
218
+ }
219
+ };
220
+ if (Array.isArray(current)) {
221
+ current.forEach(findAll);
222
+ } else {
223
+ findAll(current);
224
+ }
225
+ current = results;
226
+ continue;
227
+ }
228
+ if (Array.isArray(current)) {
229
+ const results = [];
230
+ for (const n of current) {
231
+ if (n.type === "element" && n.children) {
232
+ for (const child of n.children) {
233
+ if (child.type === "element" && child.name === part) {
234
+ results.push(child);
235
+ }
236
+ }
237
+ }
238
+ }
239
+ current = results;
240
+ } else {
241
+ if (current.type !== "element" || !current.children) return null;
242
+ const matches = current.children.filter(
243
+ (c) => c.type === "element" && c.name === part
244
+ );
245
+ if (matches.length === 0) return null;
246
+ if (matches.length === 1) {
247
+ current = matches[0];
248
+ } else {
249
+ current = matches;
250
+ }
251
+ }
252
+ }
253
+ return current;
254
+ }
255
+ function xml() {
256
+ try {
257
+ const inputStr = Host.inputString();
258
+ const input = JSON.parse(inputStr);
259
+ const parsed = parseXml(input.xml);
260
+ let result = parsed;
261
+ let type = "document";
262
+ if (input.query) {
263
+ result = queryXml(parsed, input.query);
264
+ if (result === null) {
265
+ type = "element";
266
+ } else if (typeof result === "string") {
267
+ type = "text";
268
+ } else if (Array.isArray(result)) {
269
+ type = "array";
270
+ } else {
271
+ type = "element";
272
+ }
273
+ }
274
+ const output = {
275
+ result,
276
+ type
277
+ };
278
+ Host.outputString(JSON.stringify(output));
279
+ return 0;
280
+ } catch (error) {
281
+ const output = {
282
+ result: null,
283
+ type: "document",
284
+ error: error instanceof Error ? error.message : String(error)
285
+ };
286
+ Host.outputString(JSON.stringify(output));
287
+ return 1;
288
+ }
289
+ }
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cogitator-ai/wasm-tools",
3
- "version": "0.4.1",
3
+ "version": "0.5.0",
4
4
  "description": "WASM-based tools for Cogitator agents",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -19,7 +19,7 @@
19
19
  "chokidar": "^5.0.0",
20
20
  "zod": "^3.22.0",
21
21
  "zod-to-json-schema": "^3.22.0",
22
- "@cogitator-ai/types": "0.17.0"
22
+ "@cogitator-ai/types": "0.18.0"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@extism/extism": "^1.0.3",