@cogitator-ai/wasm-tools 0.1.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/LICENSE +21 -0
- package/README.md +79 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +51 -0
- package/dist/index.js.map +1 -0
- package/dist/plugins/calc.d.ts +12 -0
- package/dist/plugins/calc.d.ts.map +1 -0
- package/dist/plugins/calc.js +80 -0
- package/dist/plugins/calc.js.map +1 -0
- package/dist/plugins/json.d.ts +12 -0
- package/dist/plugins/json.d.ts.map +1 -0
- package/dist/plugins/json.js +61 -0
- package/dist/plugins/json.js.map +1 -0
- package/dist/temp/calc.js +88 -0
- package/dist/temp/json.js +72 -0
- package/dist/wasm/calc.wasm +0 -0
- package/dist/wasm/json.wasm +0 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Cogitator Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# @cogitator-ai/wasm-tools
|
|
2
|
+
|
|
3
|
+
WASM-based tools for Cogitator agents. Secure, sandboxed tool execution using WebAssembly.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @cogitator-ai/wasm-tools
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Built-in WASM Tools
|
|
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
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
import { Cogitator, Agent } from '@cogitator-ai/core';
|
|
36
|
+
import { wasmCalculator, wasmJsonProcessor } from '@cogitator-ai/wasm-tools';
|
|
37
|
+
|
|
38
|
+
const agent = new Agent({
|
|
39
|
+
name: 'data-processor',
|
|
40
|
+
tools: [wasmCalculator(), wasmJsonProcessor()],
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Custom WASM Tools
|
|
45
|
+
|
|
46
|
+
Build custom WASM tools using Extism PDK:
|
|
47
|
+
|
|
48
|
+
```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
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Build with:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
npx @anthropic/extism-js tool.ts -o tool.wasm
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Security
|
|
66
|
+
|
|
67
|
+
WASM tools run in a secure sandbox:
|
|
68
|
+
- No filesystem access
|
|
69
|
+
- No network access
|
|
70
|
+
- Memory limits enforced
|
|
71
|
+
- Timeout enforcement
|
|
72
|
+
|
|
73
|
+
## Documentation
|
|
74
|
+
|
|
75
|
+
See the [Cogitator documentation](https://github.com/eL1fe/cogitator) for full API reference.
|
|
76
|
+
|
|
77
|
+
## License
|
|
78
|
+
|
|
79
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @cogitator-ai/wasm-tools - WASM-based tools for Cogitator agents
|
|
3
|
+
*
|
|
4
|
+
* This package provides pre-built WASM tools that run in the Extism sandbox.
|
|
5
|
+
* WASM tools offer:
|
|
6
|
+
* - 100-500x faster cold start than Docker
|
|
7
|
+
* - Memory-safe execution in isolated sandbox
|
|
8
|
+
* - ~20x lower memory footprint
|
|
9
|
+
*/
|
|
10
|
+
import { z } from 'zod';
|
|
11
|
+
import type { SandboxConfig } from '@cogitator-ai/types';
|
|
12
|
+
/**
|
|
13
|
+
* Get the path to a WASM module in this package
|
|
14
|
+
*/
|
|
15
|
+
export declare function getWasmPath(name: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* Calculator tool configuration for WASM execution
|
|
18
|
+
*/
|
|
19
|
+
export declare const calcToolConfig: SandboxConfig;
|
|
20
|
+
/**
|
|
21
|
+
* Calculator tool schema
|
|
22
|
+
*/
|
|
23
|
+
export declare const calcToolSchema: z.ZodObject<{
|
|
24
|
+
expression: z.ZodString;
|
|
25
|
+
}, "strip", z.ZodTypeAny, {
|
|
26
|
+
expression: string;
|
|
27
|
+
}, {
|
|
28
|
+
expression: string;
|
|
29
|
+
}>;
|
|
30
|
+
/**
|
|
31
|
+
* JSON processor tool configuration
|
|
32
|
+
*/
|
|
33
|
+
export declare const jsonToolConfig: SandboxConfig;
|
|
34
|
+
/**
|
|
35
|
+
* JSON processor tool schema
|
|
36
|
+
*/
|
|
37
|
+
export declare const jsonToolSchema: z.ZodObject<{
|
|
38
|
+
json: z.ZodString;
|
|
39
|
+
query: z.ZodOptional<z.ZodString>;
|
|
40
|
+
}, "strip", z.ZodTypeAny, {
|
|
41
|
+
json: string;
|
|
42
|
+
query?: string | undefined;
|
|
43
|
+
}, {
|
|
44
|
+
json: string;
|
|
45
|
+
query?: string | undefined;
|
|
46
|
+
}>;
|
|
47
|
+
export type CalcToolInput = z.infer<typeof calcToolSchema>;
|
|
48
|
+
export type JsonToolInput = z.infer<typeof jsonToolSchema>;
|
|
49
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +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;;;;;;;;;EAGzB,CAAC;AAEH,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,cAAc,CAAC,CAAC;AAC3D,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,cAAc,CAAC,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @cogitator-ai/wasm-tools - WASM-based tools for Cogitator agents
|
|
3
|
+
*
|
|
4
|
+
* This package provides pre-built WASM tools that run in the Extism sandbox.
|
|
5
|
+
* WASM tools offer:
|
|
6
|
+
* - 100-500x faster cold start than Docker
|
|
7
|
+
* - Memory-safe execution in isolated sandbox
|
|
8
|
+
* - ~20x lower memory footprint
|
|
9
|
+
*/
|
|
10
|
+
import { z } from 'zod';
|
|
11
|
+
import { fileURLToPath } from 'node:url';
|
|
12
|
+
import { dirname, join } from 'node:path';
|
|
13
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
14
|
+
/**
|
|
15
|
+
* Get the path to a WASM module in this package
|
|
16
|
+
*/
|
|
17
|
+
export function getWasmPath(name) {
|
|
18
|
+
return join(__dirname, 'wasm', `${name}.wasm`);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Calculator tool configuration for WASM execution
|
|
22
|
+
*/
|
|
23
|
+
export const calcToolConfig = {
|
|
24
|
+
type: 'wasm',
|
|
25
|
+
wasmModule: getWasmPath('calc'),
|
|
26
|
+
wasmFunction: 'calculate',
|
|
27
|
+
timeout: 5000,
|
|
28
|
+
};
|
|
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
|
+
export const jsonToolConfig = {
|
|
39
|
+
type: 'wasm',
|
|
40
|
+
wasmModule: getWasmPath('json'),
|
|
41
|
+
wasmFunction: 'process',
|
|
42
|
+
timeout: 5000,
|
|
43
|
+
};
|
|
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
|
+
});
|
|
51
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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,OAAO,CAAC,CAAC;AACjD,CAAC;AAED;;GAEG;AACH,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;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,CAAC,MAAM,CAAC;IACrC,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,yDAAyD,CAAC;CAC3F,CAAC,CAAC;AAEH;;GAEG;AACH,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;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,CAAC,MAAM,CAAC;IACrC,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,kCAAkC,CAAC;IAC7D,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,yBAAyB,CAAC;CACjE,CAAC,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculator WASM Plugin
|
|
3
|
+
*
|
|
4
|
+
* This file is compiled to WASM using the Extism JS PDK.
|
|
5
|
+
* It provides safe mathematical expression evaluation.
|
|
6
|
+
*
|
|
7
|
+
* Build command:
|
|
8
|
+
* esbuild src/plugins/calc.ts -o dist/temp/calc.js --bundle --format=cjs --target=es2020
|
|
9
|
+
* extism-js dist/temp/calc.js -o dist/wasm/calc.wasm
|
|
10
|
+
*/
|
|
11
|
+
export declare function calculate(): number;
|
|
12
|
+
//# sourceMappingURL=calc.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"calc.d.ts","sourceRoot":"","sources":["../../src/plugins/calc.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AA8DH,wBAAgB,SAAS,IAAI,MAAM,CAuBlC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculator WASM Plugin
|
|
3
|
+
*
|
|
4
|
+
* This file is compiled to WASM using the Extism JS PDK.
|
|
5
|
+
* It provides safe mathematical expression evaluation.
|
|
6
|
+
*
|
|
7
|
+
* Build command:
|
|
8
|
+
* esbuild src/plugins/calc.ts -o dist/temp/calc.js --bundle --format=cjs --target=es2020
|
|
9
|
+
* extism-js dist/temp/calc.js -o dist/wasm/calc.wasm
|
|
10
|
+
*/
|
|
11
|
+
function safeEval(expression) {
|
|
12
|
+
const sanitized = expression.replace(/[^0-9+\-*/().%\s]/g, '');
|
|
13
|
+
if (sanitized !== expression.trim()) {
|
|
14
|
+
throw new Error('Invalid characters in expression');
|
|
15
|
+
}
|
|
16
|
+
const tokens = sanitized.match(/(\d+\.?\d*|[+\-*/()%])/g);
|
|
17
|
+
if (!tokens) {
|
|
18
|
+
throw new Error('No valid tokens in expression');
|
|
19
|
+
}
|
|
20
|
+
let result = 0;
|
|
21
|
+
let currentNumber = '';
|
|
22
|
+
let operator = '+';
|
|
23
|
+
const stack = [];
|
|
24
|
+
for (let i = 0; i <= tokens.length; i++) {
|
|
25
|
+
const token = tokens[i];
|
|
26
|
+
if (token && /\d/.test(token)) {
|
|
27
|
+
currentNumber = token;
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
const num = parseFloat(currentNumber) || 0;
|
|
31
|
+
if (operator === '+') {
|
|
32
|
+
stack.push(num);
|
|
33
|
+
}
|
|
34
|
+
else if (operator === '-') {
|
|
35
|
+
stack.push(-num);
|
|
36
|
+
}
|
|
37
|
+
else if (operator === '*') {
|
|
38
|
+
const prev = stack.pop() || 0;
|
|
39
|
+
stack.push(prev * num);
|
|
40
|
+
}
|
|
41
|
+
else if (operator === '/') {
|
|
42
|
+
const prev = stack.pop() || 0;
|
|
43
|
+
if (num === 0)
|
|
44
|
+
throw new Error('Division by zero');
|
|
45
|
+
stack.push(prev / num);
|
|
46
|
+
}
|
|
47
|
+
else if (operator === '%') {
|
|
48
|
+
const prev = stack.pop() || 0;
|
|
49
|
+
stack.push(prev % num);
|
|
50
|
+
}
|
|
51
|
+
operator = token || '+';
|
|
52
|
+
currentNumber = '';
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
result = stack.reduce((a, b) => a + b, 0);
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
export function calculate() {
|
|
59
|
+
try {
|
|
60
|
+
const inputStr = Host.inputString();
|
|
61
|
+
const input = JSON.parse(inputStr);
|
|
62
|
+
const result = safeEval(input.expression);
|
|
63
|
+
const output = {
|
|
64
|
+
result,
|
|
65
|
+
expression: input.expression,
|
|
66
|
+
};
|
|
67
|
+
Host.outputString(JSON.stringify(output));
|
|
68
|
+
return 0;
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
const output = {
|
|
72
|
+
result: NaN,
|
|
73
|
+
expression: '',
|
|
74
|
+
error: error instanceof Error ? error.message : String(error),
|
|
75
|
+
};
|
|
76
|
+
Host.outputString(JSON.stringify(output));
|
|
77
|
+
return 1;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=calc.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"calc.js","sourceRoot":"","sources":["../../src/plugins/calc.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAYH,SAAS,QAAQ,CAAC,UAAkB;IAClC,MAAM,SAAS,GAAG,UAAU,CAAC,OAAO,CAAC,oBAAoB,EAAE,EAAE,CAAC,CAAC;IAE/D,IAAI,SAAS,KAAK,UAAU,CAAC,IAAI,EAAE,EAAE,CAAC;QACpC,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;IACtD,CAAC;IAED,MAAM,MAAM,GAAG,SAAS,CAAC,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC1D,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;IACnD,CAAC;IAED,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,IAAI,aA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the Extism JS PDK.
|
|
5
|
+
* It provides safe JSON parsing and basic querying.
|
|
6
|
+
*
|
|
7
|
+
* Build command:
|
|
8
|
+
* esbuild src/plugins/json.ts -o dist/temp/json.js --bundle --format=cjs --target=es2020
|
|
9
|
+
* extism-js dist/temp/json.js -o dist/wasm/json.wasm
|
|
10
|
+
*/
|
|
11
|
+
export declare function process(): number;
|
|
12
|
+
//# sourceMappingURL=json.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/plugins/json.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AA0CH,wBAAgB,OAAO,IAAI,MAAM,CAwBhC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON Processor WASM Plugin
|
|
3
|
+
*
|
|
4
|
+
* This file is compiled to WASM using the Extism JS PDK.
|
|
5
|
+
* It provides safe JSON parsing and basic querying.
|
|
6
|
+
*
|
|
7
|
+
* Build command:
|
|
8
|
+
* esbuild src/plugins/json.ts -o dist/temp/json.js --bundle --format=cjs --target=es2020
|
|
9
|
+
* extism-js dist/temp/json.js -o dist/wasm/json.wasm
|
|
10
|
+
*/
|
|
11
|
+
function getByPath(obj, path) {
|
|
12
|
+
if (!path || path === '$')
|
|
13
|
+
return obj;
|
|
14
|
+
const parts = path.replace(/^\$\.?/, '').split('.');
|
|
15
|
+
let current = obj;
|
|
16
|
+
for (const part of parts) {
|
|
17
|
+
if (current === null || current === undefined) {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
const arrayMatch = /^(\w+)\[(\d+)\]$/.exec(part);
|
|
21
|
+
if (arrayMatch) {
|
|
22
|
+
const [, key, indexStr] = arrayMatch;
|
|
23
|
+
const index = parseInt(indexStr, 10);
|
|
24
|
+
current = current[key];
|
|
25
|
+
if (Array.isArray(current)) {
|
|
26
|
+
current = current[index];
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
current = current[part];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return current;
|
|
37
|
+
}
|
|
38
|
+
export function process() {
|
|
39
|
+
try {
|
|
40
|
+
const inputStr = Host.inputString();
|
|
41
|
+
const input = JSON.parse(inputStr);
|
|
42
|
+
const parsed = JSON.parse(input.json);
|
|
43
|
+
const result = input.query ? getByPath(parsed, input.query) : parsed;
|
|
44
|
+
const output = {
|
|
45
|
+
result,
|
|
46
|
+
type: Array.isArray(result) ? 'array' : typeof result,
|
|
47
|
+
};
|
|
48
|
+
Host.outputString(JSON.stringify(output));
|
|
49
|
+
return 0;
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
const output = {
|
|
53
|
+
result: null,
|
|
54
|
+
type: 'error',
|
|
55
|
+
error: error instanceof Error ? error.message : String(error),
|
|
56
|
+
};
|
|
57
|
+
Host.outputString(JSON.stringify(output));
|
|
58
|
+
return 1;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=json.js.map
|
|
@@ -0,0 +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,CA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AAC;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"}
|
|
@@ -0,0 +1,88 @@
|
|
|
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/calc.ts
|
|
21
|
+
var calc_exports = {};
|
|
22
|
+
__export(calc_exports, {
|
|
23
|
+
calculate: () => calculate
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(calc_exports);
|
|
26
|
+
function safeEval(expression) {
|
|
27
|
+
const sanitized = expression.replace(/[^0-9+\-*/().%\s]/g, "");
|
|
28
|
+
if (sanitized !== expression.trim()) {
|
|
29
|
+
throw new Error("Invalid characters in expression");
|
|
30
|
+
}
|
|
31
|
+
const tokens = sanitized.match(/(\d+\.?\d*|[+\-*/()%])/g);
|
|
32
|
+
if (!tokens) {
|
|
33
|
+
throw new Error("No valid tokens in expression");
|
|
34
|
+
}
|
|
35
|
+
let result = 0;
|
|
36
|
+
let currentNumber = "";
|
|
37
|
+
let operator = "+";
|
|
38
|
+
const stack = [];
|
|
39
|
+
for (let i = 0; i <= tokens.length; i++) {
|
|
40
|
+
const token = tokens[i];
|
|
41
|
+
if (token && /\d/.test(token)) {
|
|
42
|
+
currentNumber = token;
|
|
43
|
+
} else {
|
|
44
|
+
const num = parseFloat(currentNumber) || 0;
|
|
45
|
+
if (operator === "+") {
|
|
46
|
+
stack.push(num);
|
|
47
|
+
} else if (operator === "-") {
|
|
48
|
+
stack.push(-num);
|
|
49
|
+
} else if (operator === "*") {
|
|
50
|
+
const prev = stack.pop() || 0;
|
|
51
|
+
stack.push(prev * num);
|
|
52
|
+
} else if (operator === "/") {
|
|
53
|
+
const prev = stack.pop() || 0;
|
|
54
|
+
if (num === 0)
|
|
55
|
+
throw new Error("Division by zero");
|
|
56
|
+
stack.push(prev / num);
|
|
57
|
+
} else if (operator === "%") {
|
|
58
|
+
const prev = stack.pop() || 0;
|
|
59
|
+
stack.push(prev % num);
|
|
60
|
+
}
|
|
61
|
+
operator = token || "+";
|
|
62
|
+
currentNumber = "";
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
result = stack.reduce((a, b) => a + b, 0);
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
function calculate() {
|
|
69
|
+
try {
|
|
70
|
+
const inputStr = Host.inputString();
|
|
71
|
+
const input = JSON.parse(inputStr);
|
|
72
|
+
const result = safeEval(input.expression);
|
|
73
|
+
const output = {
|
|
74
|
+
result,
|
|
75
|
+
expression: input.expression
|
|
76
|
+
};
|
|
77
|
+
Host.outputString(JSON.stringify(output));
|
|
78
|
+
return 0;
|
|
79
|
+
} catch (error) {
|
|
80
|
+
const output = {
|
|
81
|
+
result: NaN,
|
|
82
|
+
expression: "",
|
|
83
|
+
error: error instanceof Error ? error.message : String(error)
|
|
84
|
+
};
|
|
85
|
+
Host.outputString(JSON.stringify(output));
|
|
86
|
+
return 1;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
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/json.ts
|
|
21
|
+
var json_exports = {};
|
|
22
|
+
__export(json_exports, {
|
|
23
|
+
process: () => process
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(json_exports);
|
|
26
|
+
function getByPath(obj, path) {
|
|
27
|
+
if (!path || path === "$")
|
|
28
|
+
return obj;
|
|
29
|
+
const parts = path.replace(/^\$\.?/, "").split(".");
|
|
30
|
+
let current = obj;
|
|
31
|
+
for (const part of parts) {
|
|
32
|
+
if (current === null || current === void 0) {
|
|
33
|
+
return void 0;
|
|
34
|
+
}
|
|
35
|
+
const arrayMatch = /^(\w+)\[(\d+)\]$/.exec(part);
|
|
36
|
+
if (arrayMatch) {
|
|
37
|
+
const [, key, indexStr] = arrayMatch;
|
|
38
|
+
const index = parseInt(indexStr, 10);
|
|
39
|
+
current = current[key];
|
|
40
|
+
if (Array.isArray(current)) {
|
|
41
|
+
current = current[index];
|
|
42
|
+
} else {
|
|
43
|
+
return void 0;
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
current = current[part];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return current;
|
|
50
|
+
}
|
|
51
|
+
function process() {
|
|
52
|
+
try {
|
|
53
|
+
const inputStr = Host.inputString();
|
|
54
|
+
const input = JSON.parse(inputStr);
|
|
55
|
+
const parsed = JSON.parse(input.json);
|
|
56
|
+
const result = input.query ? getByPath(parsed, input.query) : parsed;
|
|
57
|
+
const output = {
|
|
58
|
+
result,
|
|
59
|
+
type: Array.isArray(result) ? "array" : typeof result
|
|
60
|
+
};
|
|
61
|
+
Host.outputString(JSON.stringify(output));
|
|
62
|
+
return 0;
|
|
63
|
+
} catch (error) {
|
|
64
|
+
const output = {
|
|
65
|
+
result: null,
|
|
66
|
+
type: "error",
|
|
67
|
+
error: error instanceof Error ? error.message : String(error)
|
|
68
|
+
};
|
|
69
|
+
Host.outputString(JSON.stringify(output));
|
|
70
|
+
return 1;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
Binary file
|
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cogitator-ai/wasm-tools",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "WASM-based tools for Cogitator agents",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./wasm/*": "./dist/wasm/*"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"zod": "^3.22.0",
|
|
20
|
+
"@cogitator-ai/types": "0.1.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@extism/js-pdk": "^1.0.0",
|
|
24
|
+
"esbuild": "^0.20.0",
|
|
25
|
+
"typescript": "^5.3.0"
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "https://github.com/eL1fe/cogitator.git",
|
|
30
|
+
"directory": "packages/wasm-tools"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsc && npm run build:wasm",
|
|
38
|
+
"build:wasm": "node scripts/build-wasm.js",
|
|
39
|
+
"dev": "tsc --watch",
|
|
40
|
+
"clean": "rm -rf dist",
|
|
41
|
+
"typecheck": "tsc --noEmit"
|
|
42
|
+
}
|
|
43
|
+
}
|