@cogitator-ai/wasm-tools 0.1.0 → 0.2.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/README.md CHANGED
@@ -10,61 +10,56 @@ pnpm add @cogitator-ai/wasm-tools
10
10
 
11
11
  ## Usage
12
12
 
13
- ### Built-in WASM Tools
13
+ This package provides WASM tool configurations to be used with `@cogitator-ai/sandbox`.
14
14
 
15
- ```typescript
16
- import { wasmCalculator, wasmJsonProcessor } from '@cogitator-ai/wasm-tools';
17
-
18
- // Calculator - safe math expression evaluation
19
- const calc = wasmCalculator();
20
- const result = await calc.execute({ expression: '2 + 2 * 3' });
21
- // { result: 8 }
22
-
23
- // JSON Processor - JSONPath queries
24
- const json = wasmJsonProcessor();
25
- const data = await json.execute({
26
- json: '{"users": [{"name": "Alice"}, {"name": "Bob"}]}',
27
- query: '$.users[*].name',
28
- });
29
- // { result: ["Alice", "Bob"] }
30
- ```
31
-
32
- ### Use with Cogitator
15
+ ### With Sandbox Executor
33
16
 
34
17
  ```typescript
35
- import { Cogitator, Agent } from '@cogitator-ai/core';
36
- import { wasmCalculator, wasmJsonProcessor } from '@cogitator-ai/wasm-tools';
18
+ import { WasmSandbox } from '@cogitator-ai/sandbox';
19
+ import { calcToolConfig, jsonToolConfig } from '@cogitator-ai/wasm-tools';
37
20
 
38
- const agent = new Agent({
39
- name: 'data-processor',
40
- tools: [wasmCalculator(), wasmJsonProcessor()],
41
- });
42
- ```
21
+ // Create executor
22
+ const sandbox = new WasmSandbox(calcToolConfig);
43
23
 
44
- ### Custom WASM Tools
24
+ // Calculate expression
25
+ const result = await sandbox.execute({ expression: '2 + 2 * 3' });
26
+ // { result: 8, expression: '2 + 2 * 3' }
27
+ ```
45
28
 
46
- Build custom WASM tools using Extism PDK:
29
+ ### With Cogitator Tools
47
30
 
48
31
  ```typescript
49
- // tool.ts
50
- import { Input, Output } from '@extism/js-pdk';
51
-
52
- export function run() {
53
- const input = JSON.parse(Input.string());
54
- const result = processData(input);
55
- Output.set(JSON.stringify(result));
56
- }
32
+ import { tool } from '@cogitator-ai/core';
33
+ import { WasmSandbox } from '@cogitator-ai/sandbox';
34
+ import { calcToolConfig, calcToolSchema, jsonToolConfig, jsonToolSchema } from '@cogitator-ai/wasm-tools';
35
+
36
+ const calcSandbox = new WasmSandbox(calcToolConfig);
37
+
38
+ const calculator = tool({
39
+ name: 'calculator',
40
+ description: 'Evaluate mathematical expressions safely',
41
+ parameters: calcToolSchema,
42
+ execute: async (input) => {
43
+ const result = await calcSandbox.execute(input);
44
+ return result;
45
+ },
46
+ });
57
47
  ```
58
48
 
59
- Build with:
49
+ ### Available Exports
60
50
 
61
- ```bash
62
- npx @anthropic/extism-js tool.ts -o tool.wasm
63
- ```
51
+ | Export | Description |
52
+ |--------|-------------|
53
+ | `calcToolConfig` | Sandbox config for calculator WASM module |
54
+ | `calcToolSchema` | Zod schema for calculator input |
55
+ | `jsonToolConfig` | Sandbox config for JSON processor WASM module |
56
+ | `jsonToolSchema` | Zod schema for JSON processor input |
57
+ | `getWasmPath(name)` | Get path to a WASM module by name |
64
58
 
65
59
  ## Security
66
60
 
67
61
  WASM tools run in a secure sandbox:
62
+
68
63
  - No filesystem access
69
64
  - No network access
70
65
  - Memory limits enforced
@@ -1 +1 @@
1
- {"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/plugins/json.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AA0CH,wBAAgB,OAAO,IAAI,MAAM,CAwBhC"}
1
+ {"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/plugins/json.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAoDH,wBAAgB,OAAO,IAAI,MAAM,CAwBhC"}
@@ -8,6 +8,9 @@
8
8
  * esbuild src/plugins/json.ts -o dist/temp/json.js --bundle --format=cjs --target=es2020
9
9
  * extism-js dist/temp/json.js -o dist/wasm/json.wasm
10
10
  */
11
+ function isRecord(value) {
12
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
13
+ }
11
14
  function getByPath(obj, path) {
12
15
  if (!path || path === '$')
13
16
  return obj;
@@ -21,6 +24,9 @@ function getByPath(obj, path) {
21
24
  if (arrayMatch) {
22
25
  const [, key, indexStr] = arrayMatch;
23
26
  const index = parseInt(indexStr, 10);
27
+ if (!isRecord(current)) {
28
+ return undefined;
29
+ }
24
30
  current = current[key];
25
31
  if (Array.isArray(current)) {
26
32
  current = current[index];
@@ -30,6 +36,9 @@ function getByPath(obj, path) {
30
36
  }
31
37
  }
32
38
  else {
39
+ if (!isRecord(current)) {
40
+ return undefined;
41
+ }
33
42
  current = current[part];
34
43
  }
35
44
  }
@@ -1 +1 @@
1
- {"version":3,"file":"json.js","sourceRoot":"","sources":["../../src/plugins/json.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAaH,SAAS,SAAS,CAAC,GAAY,EAAE,IAAY;IAC3C,IAAI,CAAC,IAAI,IAAI,IAAI,KAAK,GAAG;QAAE,OAAO,GAAG,CAAC;IAEtC,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACpD,IAAI,OAAO,GAAY,GAAG,CAAC;IAE3B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,IAAI,OAAO,KAAK,IAAI,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YAC9C,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,MAAM,UAAU,GAAG,kBAAkB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACjD,IAAI,UAAU,EAAE,CAAC;YACf,MAAM,CAAC,EAAE,GAAG,EAAE,QAAQ,CAAC,GAAG,UAAU,CAAC;YACrC,MAAM,KAAK,GAAG,QAAQ,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;YACrC,OAAO,GAAI,OAAmC,CAAC,GAAG,CAAC,CAAC;YACpD,IAAI,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AAC;IACX,CAAC;AACH,CAAC"}
package/dist/temp/calc.js CHANGED
@@ -51,8 +51,7 @@ function safeEval(expression) {
51
51
  stack.push(prev * num);
52
52
  } else if (operator === "/") {
53
53
  const prev = stack.pop() || 0;
54
- if (num === 0)
55
- throw new Error("Division by zero");
54
+ if (num === 0) throw new Error("Division by zero");
56
55
  stack.push(prev / num);
57
56
  } else if (operator === "%") {
58
57
  const prev = stack.pop() || 0;
package/dist/temp/json.js CHANGED
@@ -23,9 +23,11 @@ __export(json_exports, {
23
23
  process: () => process
24
24
  });
25
25
  module.exports = __toCommonJS(json_exports);
26
+ function isRecord(value) {
27
+ return value !== null && typeof value === "object" && !Array.isArray(value);
28
+ }
26
29
  function getByPath(obj, path) {
27
- if (!path || path === "$")
28
- return obj;
30
+ if (!path || path === "$") return obj;
29
31
  const parts = path.replace(/^\$\.?/, "").split(".");
30
32
  let current = obj;
31
33
  for (const part of parts) {
@@ -36,6 +38,9 @@ function getByPath(obj, path) {
36
38
  if (arrayMatch) {
37
39
  const [, key, indexStr] = arrayMatch;
38
40
  const index = parseInt(indexStr, 10);
41
+ if (!isRecord(current)) {
42
+ return void 0;
43
+ }
39
44
  current = current[key];
40
45
  if (Array.isArray(current)) {
41
46
  current = current[index];
@@ -43,6 +48,9 @@ function getByPath(obj, path) {
43
48
  return void 0;
44
49
  }
45
50
  } else {
51
+ if (!isRecord(current)) {
52
+ return void 0;
53
+ }
46
54
  current = current[part];
47
55
  }
48
56
  }
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cogitator-ai/wasm-tools",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "WASM-based tools for Cogitator agents",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -17,11 +17,11 @@
17
17
  ],
18
18
  "dependencies": {
19
19
  "zod": "^3.22.0",
20
- "@cogitator-ai/types": "0.1.0"
20
+ "@cogitator-ai/types": "0.3.1"
21
21
  },
22
22
  "devDependencies": {
23
23
  "@extism/js-pdk": "^1.0.0",
24
- "esbuild": "^0.20.0",
24
+ "esbuild": "^0.27.2",
25
25
  "typescript": "^5.3.0"
26
26
  },
27
27
  "repository": {