@cogitator-ai/wasm-tools 0.2.8 → 0.3.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.
package/dist/index.js CHANGED
@@ -1,51 +1,269 @@
1
1
  /**
2
2
  * @cogitator-ai/wasm-tools - WASM-based tools for Cogitator agents
3
3
  *
4
- * This package provides pre-built WASM tools that run in the Extism sandbox.
4
+ * This package provides pre-built WASM tools and a framework for creating
5
+ * custom WASM tools that run in the Extism sandbox.
6
+ *
5
7
  * WASM tools offer:
6
8
  * - 100-500x faster cold start than Docker
7
9
  * - Memory-safe execution in isolated sandbox
8
10
  * - ~20x lower memory footprint
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { defineWasmTool, createCalcTool } from '@cogitator-ai/wasm-tools';
15
+ *
16
+ * // Use pre-built tools
17
+ * const calc = createCalcTool();
18
+ *
19
+ * // Create custom WASM tools
20
+ * const myTool = defineWasmTool({
21
+ * name: 'image_processor',
22
+ * description: 'Process images in WASM sandbox',
23
+ * wasmModule: './my-image-proc.wasm',
24
+ * wasmFunction: 'process',
25
+ * parameters: z.object({
26
+ * imageData: z.string(),
27
+ * operation: z.enum(['resize', 'crop', 'rotate']),
28
+ * }),
29
+ * });
30
+ * ```
9
31
  */
10
32
  import { z } from 'zod';
33
+ import { zodToJsonSchema } from 'zod-to-json-schema';
11
34
  import { fileURLToPath } from 'node:url';
12
35
  import { dirname, join } from 'node:path';
13
36
  const __dirname = dirname(fileURLToPath(import.meta.url));
14
37
  /**
15
- * Get the path to a WASM module in this package
38
+ * Create a custom WASM tool for agent use.
39
+ *
40
+ * WASM tools run in an isolated Extism sandbox with memory-safe execution.
41
+ * The execute function passes parameters to the WASM module, which handles
42
+ * the actual computation.
43
+ *
44
+ * @param config - WASM tool configuration
45
+ * @returns A Tool instance configured for WASM sandbox execution
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * const hashTool = defineWasmTool({
50
+ * name: 'hash_text',
51
+ * description: 'Hash text using various algorithms',
52
+ * wasmModule: './hash.wasm',
53
+ * wasmFunction: 'hash',
54
+ * parameters: z.object({
55
+ * text: z.string(),
56
+ * algorithm: z.enum(['sha256', 'sha512', 'md5']),
57
+ * }),
58
+ * });
59
+ *
60
+ * // Use with an agent
61
+ * const agent = new Agent({
62
+ * name: 'hasher',
63
+ * tools: [hashTool],
64
+ * });
65
+ * ```
66
+ */
67
+ export function defineWasmTool(config) {
68
+ const sandboxConfig = {
69
+ type: 'wasm',
70
+ wasmModule: config.wasmModule,
71
+ wasmFunction: config.wasmFunction ?? 'run',
72
+ timeout: config.timeout ?? 5000,
73
+ wasi: config.wasi,
74
+ };
75
+ const tool = {
76
+ name: config.name,
77
+ description: config.description,
78
+ category: config.category,
79
+ tags: config.tags,
80
+ parameters: config.parameters,
81
+ sandbox: sandboxConfig,
82
+ execute: async (params, _context) => {
83
+ return params;
84
+ },
85
+ toJSON: () => wasmToolToSchema(tool),
86
+ };
87
+ return tool;
88
+ }
89
+ function wasmToolToSchema(t) {
90
+ const jsonSchema = zodToJsonSchema(t.parameters, {
91
+ target: 'openApi3',
92
+ $refStrategy: 'none',
93
+ });
94
+ const schema = jsonSchema;
95
+ const properties = (schema.properties ?? {});
96
+ const required = schema.required;
97
+ return {
98
+ name: t.name,
99
+ description: t.description,
100
+ parameters: {
101
+ type: 'object',
102
+ properties,
103
+ required,
104
+ },
105
+ };
106
+ }
107
+ /**
108
+ * Get the path to a pre-built WASM module in this package
16
109
  */
17
110
  export function getWasmPath(name) {
18
111
  return join(__dirname, 'wasm', `${name}.wasm`);
19
112
  }
113
+ export const calcToolSchema = z.object({
114
+ expression: z.string().describe('Mathematical expression to evaluate (e.g., "2 + 2 * 3")'),
115
+ });
116
+ export const jsonToolSchema = z.object({
117
+ json: z.string().describe('JSON string to parse and process'),
118
+ query: z.string().optional().describe('Optional JSONPath query'),
119
+ });
120
+ export const hashToolSchema = z.object({
121
+ text: z.string().describe('Text to hash'),
122
+ algorithm: z.enum(['sha256', 'sha1', 'md5']).describe('Hash algorithm to use'),
123
+ });
124
+ export const base64ToolSchema = z.object({
125
+ text: z.string().describe('Text to encode or decode'),
126
+ operation: z.enum(['encode', 'decode']).describe('Whether to encode or decode'),
127
+ urlSafe: z.boolean().optional().describe('Use URL-safe Base64 variant'),
128
+ });
129
+ /**
130
+ * Create a calculator WASM tool.
131
+ *
132
+ * Evaluates mathematical expressions safely in a WASM sandbox.
133
+ * Supports basic arithmetic: +, -, *, /, %, parentheses.
134
+ *
135
+ * @param options - Optional configuration overrides
136
+ * @returns A Tool for mathematical calculations
137
+ *
138
+ * @example
139
+ * ```ts
140
+ * const calc = createCalcTool();
141
+ * const agent = new Agent({ tools: [calc] });
142
+ *
143
+ * // Agent can now use: calculate({ expression: "2 + 2 * 3" })
144
+ * ```
145
+ */
146
+ export function createCalcTool(options) {
147
+ return defineWasmTool({
148
+ name: 'calculate',
149
+ description: 'Evaluate a mathematical expression safely. Supports +, -, *, /, %, and parentheses.',
150
+ wasmModule: getWasmPath('calc'),
151
+ wasmFunction: 'calculate',
152
+ parameters: calcToolSchema,
153
+ category: 'math',
154
+ tags: ['calculation', 'math', 'arithmetic'],
155
+ timeout: options?.timeout ?? 5000,
156
+ });
157
+ }
158
+ /**
159
+ * Create a JSON processor WASM tool.
160
+ *
161
+ * Parses and queries JSON data safely in a WASM sandbox.
162
+ * Supports JSONPath queries for extracting nested data.
163
+ *
164
+ * @param options - Optional configuration overrides
165
+ * @returns A Tool for JSON processing
166
+ *
167
+ * @example
168
+ * ```ts
169
+ * const jsonTool = createJsonTool();
170
+ * const agent = new Agent({ tools: [jsonTool] });
171
+ *
172
+ * // Agent can now use: process_json({ json: '{"a": 1}', query: '$.a' })
173
+ * ```
174
+ */
175
+ export function createJsonTool(options) {
176
+ return defineWasmTool({
177
+ name: 'process_json',
178
+ description: 'Parse and query JSON data. Supports JSONPath queries for extracting nested values.',
179
+ wasmModule: getWasmPath('json'),
180
+ wasmFunction: 'process',
181
+ parameters: jsonToolSchema,
182
+ category: 'utility',
183
+ tags: ['json', 'parsing', 'query'],
184
+ timeout: options?.timeout ?? 5000,
185
+ });
186
+ }
20
187
  /**
21
- * Calculator tool configuration for WASM execution
188
+ * Create a hash WASM tool.
189
+ *
190
+ * Computes cryptographic hashes safely in a WASM sandbox.
191
+ * Supports SHA-256, SHA-1, and MD5 algorithms.
192
+ *
193
+ * @param options - Optional configuration overrides
194
+ * @returns A Tool for hashing text
195
+ *
196
+ * @example
197
+ * ```ts
198
+ * const hashTool = createHashTool();
199
+ * const agent = new Agent({ tools: [hashTool] });
200
+ *
201
+ * // Agent can now use: hash_text({ text: "hello", algorithm: "sha256" })
202
+ * ```
22
203
  */
204
+ export function createHashTool(options) {
205
+ return defineWasmTool({
206
+ name: 'hash_text',
207
+ description: 'Compute cryptographic hash of text. Supports SHA-256, SHA-1, and MD5 algorithms.',
208
+ wasmModule: getWasmPath('hash'),
209
+ wasmFunction: 'hash',
210
+ parameters: hashToolSchema,
211
+ category: 'utility',
212
+ tags: ['hash', 'crypto', 'sha256', 'md5'],
213
+ timeout: options?.timeout ?? 5000,
214
+ });
215
+ }
216
+ /**
217
+ * Create a Base64 encoding/decoding WASM tool.
218
+ *
219
+ * Encodes and decodes Base64 safely in a WASM sandbox.
220
+ * Supports both standard and URL-safe Base64 variants.
221
+ *
222
+ * @param options - Optional configuration overrides
223
+ * @returns A Tool for Base64 operations
224
+ *
225
+ * @example
226
+ * ```ts
227
+ * const b64Tool = createBase64Tool();
228
+ * const agent = new Agent({ tools: [b64Tool] });
229
+ *
230
+ * // Agent can now use: base64({ text: "hello", operation: "encode" })
231
+ * ```
232
+ */
233
+ export function createBase64Tool(options) {
234
+ return defineWasmTool({
235
+ name: 'base64',
236
+ description: 'Encode or decode Base64 text. Supports standard and URL-safe variants.',
237
+ wasmModule: getWasmPath('base64'),
238
+ wasmFunction: 'base64',
239
+ parameters: base64ToolSchema,
240
+ category: 'utility',
241
+ tags: ['base64', 'encoding', 'decoding'],
242
+ timeout: options?.timeout ?? 5000,
243
+ });
244
+ }
23
245
  export const calcToolConfig = {
24
246
  type: 'wasm',
25
247
  wasmModule: getWasmPath('calc'),
26
248
  wasmFunction: 'calculate',
27
249
  timeout: 5000,
28
250
  };
29
- /**
30
- * Calculator tool schema
31
- */
32
- export const calcToolSchema = z.object({
33
- expression: z.string().describe('Mathematical expression to evaluate (e.g., "2 + 2 * 3")'),
34
- });
35
- /**
36
- * JSON processor tool configuration
37
- */
38
251
  export const jsonToolConfig = {
39
252
  type: 'wasm',
40
253
  wasmModule: getWasmPath('json'),
41
254
  wasmFunction: 'process',
42
255
  timeout: 5000,
43
256
  };
44
- /**
45
- * JSON processor tool schema
46
- */
47
- export const jsonToolSchema = z.object({
48
- json: z.string().describe('JSON string to parse and process'),
49
- query: z.string().optional().describe('Optional JSONPath query'),
50
- });
257
+ export const hashToolConfig = {
258
+ type: 'wasm',
259
+ wasmModule: getWasmPath('hash'),
260
+ wasmFunction: 'hash',
261
+ timeout: 5000,
262
+ };
263
+ export const base64ToolConfig = {
264
+ type: 'wasm',
265
+ wasmModule: getWasmPath('base64'),
266
+ wasmFunction: 'base64',
267
+ timeout: 5000,
268
+ };
51
269
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAE1C,MAAM,SAAS,GAAG,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AAE1D;;GAEG;AACH,MAAM,UAAU,WAAW,CAAC,IAAY;IACtC,OAAO,IAAI,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,IAAI,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U,gBAAgB,CAAC,OAA8B;IAC7D,OAAO,cAAc,CAAC;QACpB,IAAI,EAAE,QAAQ;QACd,WAAW,EACT,wEAAwE;QAC1E,UAAU,EAAE,WAAW,CAAC,QAAQ,CAAC;QACjC,YAAY,EAAE,QAAQ;QACtB,UAAU,EAAE,gBAAgB;QAC5B,QAAQ,EAAE,SAAS;QACnB,IAAI,EAAE,CAAC,QAAQ,EAAE,UAAU,EAAE,UAAU,CAAC;QACxC,OAAO,EAAE,OAAO,EAAE,OAAO,IAAI,IAAI;KAClC,CAAC,CAAC;AACL,CAAC;AAED,MAAM,CAAC,MAAM,cAAc,GAAkB;IAC3C,IAAI,EAAE,MAAM;IACZ,UAAU,EAAE,WAAW,CAAC,MAAM,CAAC;IAC/B,YAAY,EAAE,WAAW;IACzB,OAAO,EAAE,IAAI;CACd,CAAC;AAEF,MAAM,CAAC,MAAM,cAAc,GAAkB;IAC3C,IAAI,EAAE,MAAM;IACZ,UAAU,EAAE,WAAW,CAAC,MAAM,CAAC;IAC/B,YAAY,EAAE,SAAS;IACvB,OAAO,EAAE,IAAI;CACd,CAAC;AAEF,MAAM,CAAC,MAAM,cAAc,GAAkB;IAC3C,IAAI,EAAE,MAAM;IACZ,UAAU,EAAE,WAAW,CAAC,MAAM,CAAC;IAC/B,YAAY,EAAE,MAAM;IACpB,OAAO,EAAE,IAAI;CACd,CAAC;AAEF,MAAM,CAAC,MAAM,gBAAgB,GAAkB;IAC7C,IAAI,EAAE,MAAM;IACZ,UAAU,EAAE,WAAW,CAAC,QAAQ,CAAC;IACjC,YAAY,EAAE,QAAQ;IACtB,OAAO,EAAE,IAAI;CACd,CAAC"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Base64 WASM Plugin
3
+ *
4
+ * This file is compiled to WASM using the Extism JS PDK.
5
+ * It provides base64 encoding and decoding functions.
6
+ *
7
+ * Build command:
8
+ * esbuild src/plugins/base64.ts -o dist/temp/base64.js --bundle --format=cjs --target=es2020
9
+ * extism-js dist/temp/base64.js -o dist/wasm/base64.wasm
10
+ */
11
+ export declare function base64(): number;
12
+ //# sourceMappingURL=base64.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64.d.ts","sourceRoot":"","sources":["../../src/plugins/base64.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAqFH,wBAAgB,MAAM,IAAI,MAAM,CA6B/B"}
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Base64 WASM Plugin
3
+ *
4
+ * This file is compiled to WASM using the Extism JS PDK.
5
+ * It provides base64 encoding and decoding functions.
6
+ *
7
+ * Build command:
8
+ * esbuild src/plugins/base64.ts -o dist/temp/base64.js --bundle --format=cjs --target=es2020
9
+ * extism-js dist/temp/base64.js -o dist/wasm/base64.wasm
10
+ */
11
+ const STANDARD_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
12
+ const URL_SAFE_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
13
+ function encodeBase64(input, urlSafe) {
14
+ const chars = urlSafe ? URL_SAFE_CHARS : STANDARD_CHARS;
15
+ let result = '';
16
+ let i = 0;
17
+ while (i < input.length) {
18
+ const a = input.charCodeAt(i++);
19
+ const b = i < input.length ? input.charCodeAt(i++) : 0;
20
+ const c = i < input.length ? input.charCodeAt(i++) : 0;
21
+ const bitmap = (a << 16) | (b << 8) | c;
22
+ result +=
23
+ chars.charAt((bitmap >> 18) & 63) +
24
+ chars.charAt((bitmap >> 12) & 63) +
25
+ chars.charAt((bitmap >> 6) & 63) +
26
+ chars.charAt(bitmap & 63);
27
+ }
28
+ const padding = input.length % 3;
29
+ if (padding === 1) {
30
+ result = result.slice(0, -2) + (urlSafe ? '' : '==');
31
+ }
32
+ else if (padding === 2) {
33
+ result = result.slice(0, -1) + (urlSafe ? '' : '=');
34
+ }
35
+ return result;
36
+ }
37
+ function decodeBase64(input, urlSafe) {
38
+ let chars = urlSafe ? URL_SAFE_CHARS : STANDARD_CHARS;
39
+ let normalized = input;
40
+ if (!urlSafe && input.includes('-')) {
41
+ chars = URL_SAFE_CHARS;
42
+ normalized = input;
43
+ }
44
+ normalized = normalized.replace(/[=]/g, '');
45
+ const lookup = {};
46
+ for (let i = 0; i < chars.length; i++) {
47
+ lookup[chars[i]] = i;
48
+ }
49
+ let result = '';
50
+ let buffer = 0;
51
+ let bits = 0;
52
+ for (let i = 0; i < normalized.length; i++) {
53
+ const char = normalized[i];
54
+ const value = lookup[char];
55
+ if (value === undefined) {
56
+ continue;
57
+ }
58
+ buffer = (buffer << 6) | value;
59
+ bits += 6;
60
+ if (bits >= 8) {
61
+ bits -= 8;
62
+ result += String.fromCharCode((buffer >> bits) & 0xff);
63
+ }
64
+ }
65
+ return result;
66
+ }
67
+ export function base64() {
68
+ try {
69
+ const inputStr = Host.inputString();
70
+ const input = JSON.parse(inputStr);
71
+ const urlSafe = input.urlSafe ?? false;
72
+ let result;
73
+ if (input.operation === 'encode') {
74
+ result = encodeBase64(input.text, urlSafe);
75
+ }
76
+ else {
77
+ result = decodeBase64(input.text, urlSafe);
78
+ }
79
+ const output = {
80
+ result,
81
+ operation: input.operation,
82
+ };
83
+ Host.outputString(JSON.stringify(output));
84
+ return 0;
85
+ }
86
+ catch (error) {
87
+ const output = {
88
+ result: '',
89
+ operation: 'unknown',
90
+ error: error instanceof Error ? error.message : String(error),
91
+ };
92
+ Host.outputString(JSON.stringify(output));
93
+ return 1;
94
+ }
95
+ }
96
+ //# sourceMappingURL=base64.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64.js","sourceRoot":"","sources":["../../src/plugins/base64.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAcH,MAAM,cAAc,GAAG,kEAAkE,CAAC;AAC1F,MAAM,cAAc,GAAG,kEAAkE,CAAC;AAE1F,SAAS,YAAY,CAAC,KAAa,EAAE,OAAgB;IACnD,MAAM,KAAK,GAAG,OAAO,CAAC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AC;QACF,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC;QAC1C,OAAO,CAAC,CAAC;IACX,CAAC;AACH,CAAC"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Hash WASM Plugin
3
+ *
4
+ * This file is compiled to WASM using the Extism JS PDK.
5
+ * It provides cryptographic hashing functions.
6
+ *
7
+ * Build command:
8
+ * esbuild src/plugins/hash.ts -o dist/temp/hash.js --bundle --format=cjs --target=es2020
9
+ * extism-js dist/temp/hash.js -o dist/wasm/hash.wasm
10
+ */
11
+ export declare function hash(): number;
12
+ //# sourceMappingURL=hash.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../src/plugins/hash.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AA2SH,wBAAgB,IAAI,IAAI,MAAM,CAmC7B"}