@cogitator-ai/wasm-tools 0.2.8 → 0.3.1

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/README.md CHANGED
@@ -2,77 +2,208 @@
2
2
 
3
3
  WASM-based tools for Cogitator agents. Secure, sandboxed tool execution using WebAssembly.
4
4
 
5
+ ## Features
6
+
7
+ - 🚀 **100-500x faster cold start** than Docker containers
8
+ - 🔒 **Memory-safe execution** in isolated Extism sandbox
9
+ - 📦 **~20x lower memory footprint** compared to containers
10
+ - 🛠️ **Custom tool framework** - create your own WASM tools
11
+
5
12
  ## Installation
6
13
 
7
14
  ```bash
8
15
  pnpm add @cogitator-ai/wasm-tools
9
16
  ```
10
17
 
11
- ## Usage
18
+ ## Quick Start
12
19
 
13
- This package provides WASM tool configurations to be used with `@cogitator-ai/sandbox`.
20
+ ### Pre-built Tools
14
21
 
15
- ### With Sandbox Executor
22
+ Use the built-in WASM tools:
16
23
 
17
24
  ```typescript
18
- import { WasmSandbox } from '@cogitator-ai/sandbox';
19
- import { calcToolConfig, jsonToolConfig } from '@cogitator-ai/wasm-tools';
25
+ import {
26
+ createCalcTool,
27
+ createJsonTool,
28
+ createHashTool,
29
+ createBase64Tool,
30
+ } from '@cogitator-ai/wasm-tools';
31
+ import { Cogitator, Agent } from '@cogitator-ai/core';
20
32
 
21
- // Create executor
22
- const sandbox = new WasmSandbox(calcToolConfig);
33
+ const calc = createCalcTool();
34
+ const json = createJsonTool();
35
+ const hash = createHashTool();
36
+ const base64 = createBase64Tool();
23
37
 
24
- // Calculate expression
25
- const result = await sandbox.execute({ expression: '2 + 2 * 3' });
26
- // { result: 8, expression: '2 + 2 * 3' }
38
+ const agent = new Agent({
39
+ name: 'utility-assistant',
40
+ model: 'gpt-4o',
41
+ tools: [calc, json, hash, base64],
42
+ });
43
+
44
+ const cog = new Cogitator({ llm: { defaultProvider: 'openai' } });
45
+ const result = await cog.run(agent, {
46
+ input: 'Calculate the SHA-256 hash of "hello world"',
47
+ });
27
48
  ```
28
49
 
29
- ### With Cogitator Tools
50
+ ### Custom WASM Tools
30
51
 
31
- ```typescript
32
- import { tool } from '@cogitator-ai/core';
33
- import { WasmSandbox } from '@cogitator-ai/sandbox';
34
- import {
35
- calcToolConfig,
36
- calcToolSchema,
37
- jsonToolConfig,
38
- jsonToolSchema,
39
- } from '@cogitator-ai/wasm-tools';
52
+ Create custom tools that run in the WASM sandbox:
40
53
 
41
- const calcSandbox = new WasmSandbox(calcToolConfig);
54
+ ```typescript
55
+ import { defineWasmTool } from '@cogitator-ai/wasm-tools';
56
+ import { z } from 'zod';
57
+
58
+ const hashTool = defineWasmTool({
59
+ name: 'hash_text',
60
+ description: 'Hash text using various algorithms',
61
+ wasmModule: './my-hash.wasm',
62
+ wasmFunction: 'hash',
63
+ parameters: z.object({
64
+ text: z.string().describe('Text to hash'),
65
+ algorithm: z.enum(['sha256', 'sha512', 'md5']),
66
+ }),
67
+ category: 'utility',
68
+ tags: ['hash', 'crypto'],
69
+ timeout: 5000,
70
+ });
42
71
 
43
- const calculator = tool({
44
- name: 'calculator',
45
- description: 'Evaluate mathematical expressions safely',
46
- parameters: calcToolSchema,
47
- execute: async (input) => {
48
- const result = await calcSandbox.execute(input);
49
- return result;
50
- },
72
+ const agent = new Agent({
73
+ name: 'hasher',
74
+ tools: [hashTool],
51
75
  });
52
76
  ```
53
77
 
54
- ### Available Exports
78
+ ## API Reference
55
79
 
56
- | Export | Description |
57
- | ------------------- | --------------------------------------------- |
58
- | `calcToolConfig` | Sandbox config for calculator WASM module |
59
- | `calcToolSchema` | Zod schema for calculator input |
60
- | `jsonToolConfig` | Sandbox config for JSON processor WASM module |
61
- | `jsonToolSchema` | Zod schema for JSON processor input |
62
- | `getWasmPath(name)` | Get path to a WASM module by name |
80
+ ### defineWasmTool(config)
63
81
 
64
- ## Security
82
+ Create a custom WASM tool for agent use.
83
+
84
+ ```typescript
85
+ interface WasmToolConfig<TParams> {
86
+ name: string;
87
+ description: string;
88
+ wasmModule: string; // Path to .wasm file
89
+ wasmFunction?: string; // Function to call (default: 'run')
90
+ parameters: ZodType<TParams>;
91
+ category?: ToolCategory;
92
+ tags?: string[];
93
+ timeout?: number; // Execution timeout in ms
94
+ wasi?: boolean; // Enable WASI support
95
+ memoryPages?: number; // WASM memory limit
96
+ }
97
+ ```
65
98
 
66
- WASM tools run in a secure sandbox:
99
+ ### createCalcTool(options?)
100
+
101
+ Create a calculator tool for mathematical expressions.
102
+
103
+ ```typescript
104
+ const calc = createCalcTool({ timeout: 10000 });
105
+
106
+ // Supports: +, -, *, /, %, parentheses
107
+ // Example: "2 + 2 * 3" → 8
108
+ ```
109
+
110
+ ### createJsonTool(options?)
111
+
112
+ Create a JSON processor tool with JSONPath query support.
113
+
114
+ ```typescript
115
+ const json = createJsonTool({ timeout: 10000 });
116
+
117
+ // Example: { json: '{"a": {"b": 1}}', query: '$.a.b' } → 1
118
+ ```
119
+
120
+ ### createHashTool(options?)
121
+
122
+ Create a cryptographic hash tool supporting multiple algorithms.
123
+
124
+ ```typescript
125
+ const hash = createHashTool({ timeout: 10000 });
67
126
 
68
- - No filesystem access
69
- - No network access
70
- - Memory limits enforced
71
- - Timeout enforcement
127
+ // Supports: sha256, sha1, md5
128
+ // Example: { text: "hello", algorithm: "sha256" } → "2cf24dba5fb0a30e..."
129
+ ```
130
+
131
+ ### createBase64Tool(options?)
132
+
133
+ Create a Base64 encoding/decoding tool with URL-safe variant support.
134
+
135
+ ```typescript
136
+ const base64 = createBase64Tool({ timeout: 10000 });
137
+
138
+ // Example: { text: "hello", operation: "encode" } → "aGVsbG8="
139
+ // Example: { text: "aGVsbG8=", operation: "decode" } → "hello"
140
+ // URL-safe: { text: "hello", operation: "encode", urlSafe: true }
141
+ ```
142
+
143
+ ### getWasmPath(name)
144
+
145
+ Get the path to a pre-built WASM module.
146
+
147
+ ```typescript
148
+ import { getWasmPath } from '@cogitator-ai/wasm-tools';
149
+
150
+ const calcPath = getWasmPath('calc'); // Path to calc.wasm
151
+ const jsonPath = getWasmPath('json'); // Path to json.wasm
152
+ ```
153
+
154
+ ### Legacy Exports
155
+
156
+ For direct sandbox usage:
157
+
158
+ | Export | Description |
159
+ | ------------------ | ----------------------------------------- |
160
+ | `calcToolConfig` | Sandbox config for calculator WASM module |
161
+ | `calcToolSchema` | Zod schema for calculator input |
162
+ | `jsonToolConfig` | Sandbox config for JSON processor |
163
+ | `jsonToolSchema` | Zod schema for JSON processor input |
164
+ | `hashToolConfig` | Sandbox config for hash WASM module |
165
+ | `hashToolSchema` | Zod schema for hash input |
166
+ | `base64ToolConfig` | Sandbox config for base64 WASM module |
167
+ | `base64ToolSchema` | Zod schema for base64 input |
168
+
169
+ ## Building Custom WASM Modules
170
+
171
+ WASM modules use the Extism JS PDK:
172
+
173
+ ```typescript
174
+ // my-tool.ts
175
+ export function run(): number {
176
+ const input = JSON.parse(Host.inputString());
177
+
178
+ // Your logic here
179
+ const result = { processed: input.data };
180
+
181
+ Host.outputString(JSON.stringify(result));
182
+ return 0; // 0 = success
183
+ }
184
+
185
+ declare const Host: {
186
+ inputString(): string;
187
+ outputString(s: string): void;
188
+ };
189
+ ```
190
+
191
+ Build with:
192
+
193
+ ```bash
194
+ esbuild my-tool.ts -o temp/my-tool.js --bundle --format=cjs --target=es2020
195
+ extism-js temp/my-tool.js -o dist/my-tool.wasm
196
+ ```
197
+
198
+ ## Security
72
199
 
73
- ## Documentation
200
+ WASM tools run in a secure Extism sandbox:
74
201
 
75
- See the [Cogitator documentation](https://github.com/eL1fe/cogitator) for full API reference.
202
+ - No filesystem access (unless WASI enabled)
203
+ - ❌ No network access
204
+ - ✅ Memory limits enforced
205
+ - ✅ Timeout enforcement
206
+ - ✅ Isolated execution environment
76
207
 
77
208
  ## License
78
209
 
package/dist/index.d.ts CHANGED
@@ -1,25 +1,86 @@
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
- import { z } from 'zod';
11
- import type { SandboxConfig } from '@cogitator-ai/types';
32
+ import { z, type ZodType } from 'zod';
33
+ import type { Tool, SandboxConfig, ToolCategory } from '@cogitator-ai/types';
12
34
  /**
13
- * Get the path to a WASM module in this package
35
+ * Configuration for defining a custom WASM tool
14
36
  */
15
- export declare function getWasmPath(name: string): string;
37
+ export interface WasmToolConfig<TParams = unknown> {
38
+ name: string;
39
+ description: string;
40
+ wasmModule: string;
41
+ wasmFunction?: string;
42
+ parameters: ZodType<TParams>;
43
+ category?: ToolCategory;
44
+ tags?: string[];
45
+ timeout?: number;
46
+ wasi?: boolean;
47
+ memoryPages?: number;
48
+ }
16
49
  /**
17
- * Calculator tool configuration for WASM execution
50
+ * Create a custom WASM tool for agent use.
51
+ *
52
+ * WASM tools run in an isolated Extism sandbox with memory-safe execution.
53
+ * The execute function passes parameters to the WASM module, which handles
54
+ * the actual computation.
55
+ *
56
+ * @param config - WASM tool configuration
57
+ * @returns A Tool instance configured for WASM sandbox execution
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * const hashTool = defineWasmTool({
62
+ * name: 'hash_text',
63
+ * description: 'Hash text using various algorithms',
64
+ * wasmModule: './hash.wasm',
65
+ * wasmFunction: 'hash',
66
+ * parameters: z.object({
67
+ * text: z.string(),
68
+ * algorithm: z.enum(['sha256', 'sha512', 'md5']),
69
+ * }),
70
+ * });
71
+ *
72
+ * // Use with an agent
73
+ * const agent = new Agent({
74
+ * name: 'hasher',
75
+ * tools: [hashTool],
76
+ * });
77
+ * ```
18
78
  */
19
- export declare const calcToolConfig: SandboxConfig;
79
+ export declare function defineWasmTool<TParams>(config: WasmToolConfig<TParams>): Tool<TParams, unknown>;
20
80
  /**
21
- * Calculator tool schema
81
+ * Get the path to a pre-built WASM module in this package
22
82
  */
83
+ export declare function getWasmPath(name: string): string;
23
84
  export declare const calcToolSchema: z.ZodObject<{
24
85
  expression: z.ZodString;
25
86
  }, "strip", z.ZodTypeAny, {
@@ -27,13 +88,6 @@ export declare const calcToolSchema: z.ZodObject<{
27
88
  }, {
28
89
  expression: string;
29
90
  }>;
30
- /**
31
- * JSON processor tool configuration
32
- */
33
- export declare const jsonToolConfig: SandboxConfig;
34
- /**
35
- * JSON processor tool schema
36
- */
37
91
  export declare const jsonToolSchema: z.ZodObject<{
38
92
  json: z.ZodString;
39
93
  query: z.ZodOptional<z.ZodString>;
@@ -44,6 +98,115 @@ export declare const jsonToolSchema: z.ZodObject<{
44
98
  json: string;
45
99
  query?: string | undefined;
46
100
  }>;
101
+ export declare const hashToolSchema: z.ZodObject<{
102
+ text: z.ZodString;
103
+ algorithm: z.ZodEnum<["sha256", "sha1", "md5"]>;
104
+ }, "strip", z.ZodTypeAny, {
105
+ text: string;
106
+ algorithm: "sha256" | "sha1" | "md5";
107
+ }, {
108
+ text: string;
109
+ algorithm: "sha256" | "sha1" | "md5";
110
+ }>;
111
+ export declare const base64ToolSchema: z.ZodObject<{
112
+ text: z.ZodString;
113
+ operation: z.ZodEnum<["encode", "decode"]>;
114
+ urlSafe: z.ZodOptional<z.ZodBoolean>;
115
+ }, "strip", z.ZodTypeAny, {
116
+ text: string;
117
+ operation: "encode" | "decode";
118
+ urlSafe?: boolean | undefined;
119
+ }, {
120
+ text: string;
121
+ operation: "encode" | "decode";
122
+ urlSafe?: boolean | undefined;
123
+ }>;
124
+ /**
125
+ * Create a calculator WASM tool.
126
+ *
127
+ * Evaluates mathematical expressions safely in a WASM sandbox.
128
+ * Supports basic arithmetic: +, -, *, /, %, parentheses.
129
+ *
130
+ * @param options - Optional configuration overrides
131
+ * @returns A Tool for mathematical calculations
132
+ *
133
+ * @example
134
+ * ```ts
135
+ * const calc = createCalcTool();
136
+ * const agent = new Agent({ tools: [calc] });
137
+ *
138
+ * // Agent can now use: calculate({ expression: "2 + 2 * 3" })
139
+ * ```
140
+ */
141
+ export declare function createCalcTool(options?: {
142
+ timeout?: number;
143
+ }): Tool<CalcToolInput, unknown>;
144
+ /**
145
+ * Create a JSON processor WASM tool.
146
+ *
147
+ * Parses and queries JSON data safely in a WASM sandbox.
148
+ * Supports JSONPath queries for extracting nested data.
149
+ *
150
+ * @param options - Optional configuration overrides
151
+ * @returns A Tool for JSON processing
152
+ *
153
+ * @example
154
+ * ```ts
155
+ * const jsonTool = createJsonTool();
156
+ * const agent = new Agent({ tools: [jsonTool] });
157
+ *
158
+ * // Agent can now use: process_json({ json: '{"a": 1}', query: '$.a' })
159
+ * ```
160
+ */
161
+ export declare function createJsonTool(options?: {
162
+ timeout?: number;
163
+ }): Tool<JsonToolInput, unknown>;
164
+ /**
165
+ * Create a hash WASM tool.
166
+ *
167
+ * Computes cryptographic hashes safely in a WASM sandbox.
168
+ * Supports SHA-256, SHA-1, and MD5 algorithms.
169
+ *
170
+ * @param options - Optional configuration overrides
171
+ * @returns A Tool for hashing text
172
+ *
173
+ * @example
174
+ * ```ts
175
+ * const hashTool = createHashTool();
176
+ * const agent = new Agent({ tools: [hashTool] });
177
+ *
178
+ * // Agent can now use: hash_text({ text: "hello", algorithm: "sha256" })
179
+ * ```
180
+ */
181
+ export declare function createHashTool(options?: {
182
+ timeout?: number;
183
+ }): Tool<HashToolInput, unknown>;
184
+ /**
185
+ * Create a Base64 encoding/decoding WASM tool.
186
+ *
187
+ * Encodes and decodes Base64 safely in a WASM sandbox.
188
+ * Supports both standard and URL-safe Base64 variants.
189
+ *
190
+ * @param options - Optional configuration overrides
191
+ * @returns A Tool for Base64 operations
192
+ *
193
+ * @example
194
+ * ```ts
195
+ * const b64Tool = createBase64Tool();
196
+ * const agent = new Agent({ tools: [b64Tool] });
197
+ *
198
+ * // Agent can now use: base64({ text: "hello", operation: "encode" })
199
+ * ```
200
+ */
201
+ export declare function createBase64Tool(options?: {
202
+ timeout?: number;
203
+ }): Tool<Base64ToolInput, unknown>;
204
+ export declare const calcToolConfig: SandboxConfig;
205
+ export declare const jsonToolConfig: SandboxConfig;
206
+ export declare const hashToolConfig: SandboxConfig;
207
+ export declare const base64ToolConfig: SandboxConfig;
47
208
  export type CalcToolInput = z.infer<typeof calcToolSchema>;
48
209
  export type JsonToolInput = z.infer<typeof jsonToolSchema>;
210
+ export type HashToolInput = z.infer<typeof hashToolSchema>;
211
+ export type Base64ToolInput = z.infer<typeof base64ToolSchema>;
49
212
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAMzD;;GAEG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED;;GAEG;AACH,eAAO,MAAM,cAAc,EAAE,aAK5B,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,cAAc;;;;;;EAEzB,CAAC;AAEH;;GAEG;AACH,eAAO,MAAM,cAAc,EAAE,aAK5B,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,cAAc;;;;;;;;;EAG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