@cogitator-ai/wasm-tools 0.1.0 → 0.2.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 +40 -40
- package/dist/plugins/json.d.ts.map +1 -1
- package/dist/plugins/json.js +9 -0
- package/dist/plugins/json.js.map +1 -1
- package/dist/temp/calc.js +1 -2
- package/dist/temp/json.js +10 -2
- package/dist/wasm/calc.wasm +0 -0
- package/dist/wasm/json.wasm +0 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -10,61 +10,61 @@ pnpm add @cogitator-ai/wasm-tools
|
|
|
10
10
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
This package provides WASM tool configurations to be used with `@cogitator-ai/sandbox`.
|
|
14
14
|
|
|
15
|
-
|
|
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 {
|
|
36
|
-
import {
|
|
18
|
+
import { WasmSandbox } from '@cogitator-ai/sandbox';
|
|
19
|
+
import { calcToolConfig, jsonToolConfig } from '@cogitator-ai/wasm-tools';
|
|
37
20
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
tools: [wasmCalculator(), wasmJsonProcessor()],
|
|
41
|
-
});
|
|
42
|
-
```
|
|
21
|
+
// Create executor
|
|
22
|
+
const sandbox = new WasmSandbox(calcToolConfig);
|
|
43
23
|
|
|
44
|
-
|
|
24
|
+
// Calculate expression
|
|
25
|
+
const result = await sandbox.execute({ expression: '2 + 2 * 3' });
|
|
26
|
+
// { result: 8, expression: '2 + 2 * 3' }
|
|
27
|
+
```
|
|
45
28
|
|
|
46
|
-
|
|
29
|
+
### With Cogitator Tools
|
|
47
30
|
|
|
48
31
|
```typescript
|
|
49
|
-
|
|
50
|
-
import {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
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';
|
|
40
|
+
|
|
41
|
+
const calcSandbox = new WasmSandbox(calcToolConfig);
|
|
42
|
+
|
|
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
|
+
},
|
|
51
|
+
});
|
|
57
52
|
```
|
|
58
53
|
|
|
59
|
-
|
|
54
|
+
### Available Exports
|
|
60
55
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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 |
|
|
64
63
|
|
|
65
64
|
## Security
|
|
66
65
|
|
|
67
66
|
WASM tools run in a secure sandbox:
|
|
67
|
+
|
|
68
68
|
- No filesystem access
|
|
69
69
|
- No network access
|
|
70
70
|
- Memory limits enforced
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/plugins/json.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;
|
|
1
|
+
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/plugins/json.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAoDH,wBAAgB,OAAO,IAAI,MAAM,CAwBhC"}
|
package/dist/plugins/json.js
CHANGED
|
@@ -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
|
}
|
package/dist/plugins/json.js.map
CHANGED
|
@@ -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,
|
|
1
|
+
{"version":3,"file":"json.js","sourceRoot":"","sources":["../../src/plugins/json.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAaH,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AAED,SAAS,SAAS,CAAC,GAAY,EAAE,IAAY;IAC3C,IAAI,CAAC,IAAI,IAAI,IAAI,KAAK,GAAG;QAAE,OAAO,GA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AAC;QAE9C,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACtC,MAAM,MAAM,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;QAErE,MAAM,MAAM,GAAe;YACzB,MAAM;YACN,IAAI,EAAE,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,MAAM;SACtD,CAAC;QAEF,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC;QAC1C,OAAO,CAAC,CAAC;IACX,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,MAAM,GAAe;YACzB,MAAM,EAAE,IAAI;YACZ,IAAI,EAAE,OAAO;YACb,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;SAC9D,CAAC;QACF,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC;QAC1C,OAAO,CAAC,CAAC;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
|
}
|
package/dist/wasm/calc.wasm
CHANGED
|
Binary file
|
package/dist/wasm/json.wasm
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cogitator-ai/wasm-tools",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
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.
|
|
20
|
+
"@cogitator-ai/types": "0.4.0"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@extism/js-pdk": "^1.0.0",
|
|
24
|
-
"esbuild": "^0.
|
|
24
|
+
"esbuild": "^0.27.2",
|
|
25
25
|
"typescript": "^5.3.0"
|
|
26
26
|
},
|
|
27
27
|
"repository": {
|