@pydantic/monty 0.0.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 +35 -0
- package/browser.js +1 -0
- package/index.d.ts +26 -0
- package/index.js +579 -0
- package/package.json +99 -0
- package/wrapper.d.ts +225 -0
- package/wrapper.js +423 -0
package/package.json
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pydantic/monty",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Sandboxed Python interpreter for JavaScript/TypeScript",
|
|
6
|
+
"main": "wrapper.js",
|
|
7
|
+
"types": "wrapper.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./wrapper.d.ts",
|
|
11
|
+
"default": "./wrapper.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "github.com/pydantic/monty"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"browser": "browser.js",
|
|
20
|
+
"files": [
|
|
21
|
+
"index.d.ts",
|
|
22
|
+
"index.js",
|
|
23
|
+
"wrapper.js",
|
|
24
|
+
"wrapper.d.ts",
|
|
25
|
+
"browser.js"
|
|
26
|
+
],
|
|
27
|
+
"napi": {
|
|
28
|
+
"binaryName": "monty",
|
|
29
|
+
"targets": [
|
|
30
|
+
"x86_64-pc-windows-msvc",
|
|
31
|
+
"x86_64-apple-darwin",
|
|
32
|
+
"x86_64-unknown-linux-gnu",
|
|
33
|
+
"aarch64-apple-darwin",
|
|
34
|
+
"wasm32-wasip1-threads"
|
|
35
|
+
],
|
|
36
|
+
"dtsHeaderFile": "./index-header.d.ts"
|
|
37
|
+
},
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">= 6.14.2 < 7 || >= 8.11.2 < 9 || >= 9.11.0 < 10 || >= 10.0.0"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"registry": "https://registry.npmjs.org/",
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"artifacts": "napi artifacts",
|
|
47
|
+
"bench": "node --import @oxc-node/core/register benchmark/bench.ts",
|
|
48
|
+
"build": "run-s build:napi build:ts",
|
|
49
|
+
"build:debug": "run-s build:napi:debug build:ts",
|
|
50
|
+
"build:napi": "napi build --platform --release --esm",
|
|
51
|
+
"build:napi:debug": "napi build --platform --esm",
|
|
52
|
+
"build:ts": "tsc",
|
|
53
|
+
"create-npm-dirs": "napi create-npm-dirs",
|
|
54
|
+
"format": "run-p format:prettier format:rs format:toml",
|
|
55
|
+
"format:prettier": "prettier . -w",
|
|
56
|
+
"format:toml": "taplo format",
|
|
57
|
+
"format:rs": "cargo fmt",
|
|
58
|
+
"lint": "oxlint .",
|
|
59
|
+
"prepublishOnly": "napi prepublish -t npm",
|
|
60
|
+
"test": "ava",
|
|
61
|
+
"smoke-test": "bash scripts/smoke-test.sh",
|
|
62
|
+
"preversion": "napi build --platform && git add .",
|
|
63
|
+
"version": "napi version"
|
|
64
|
+
},
|
|
65
|
+
"devDependencies": {
|
|
66
|
+
"@emnapi/core": "^1.5.0",
|
|
67
|
+
"@emnapi/runtime": "^1.5.0",
|
|
68
|
+
"@napi-rs/cli": "^3.2.0",
|
|
69
|
+
"@oxc-node/core": "^0.0.35",
|
|
70
|
+
"@taplo/cli": "^0.7.0",
|
|
71
|
+
"@tybys/wasm-util": "^0.10.0",
|
|
72
|
+
"@types/node": "^25.0.9",
|
|
73
|
+
"ava": "^6.4.1",
|
|
74
|
+
"chalk": "^5.6.2",
|
|
75
|
+
"npm-run-all2": "^8.0.4",
|
|
76
|
+
"oxlint": "^1.14.0",
|
|
77
|
+
"prettier": "^3.6.2",
|
|
78
|
+
"tinybench": "^6.0.0",
|
|
79
|
+
"typescript": "^5.9.2"
|
|
80
|
+
},
|
|
81
|
+
"ava": {
|
|
82
|
+
"extensions": {
|
|
83
|
+
"ts": "module"
|
|
84
|
+
},
|
|
85
|
+
"timeout": "2m",
|
|
86
|
+
"workerThreads": false,
|
|
87
|
+
"nodeArguments": [
|
|
88
|
+
"--import",
|
|
89
|
+
"@oxc-node/core/register"
|
|
90
|
+
]
|
|
91
|
+
},
|
|
92
|
+
"prettier": {
|
|
93
|
+
"printWidth": 120,
|
|
94
|
+
"semi": false,
|
|
95
|
+
"trailingComma": "all",
|
|
96
|
+
"singleQuote": true,
|
|
97
|
+
"arrowParens": "always"
|
|
98
|
+
}
|
|
99
|
+
}
|
package/wrapper.d.ts
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import type { MontyOptions, RunOptions, ResourceLimits, Frame, ExceptionInfo, StartOptions, ResumeOptions, ExceptionInput, SnapshotLoadOptions, JsMontyObject } from './index.js';
|
|
2
|
+
import { MontySnapshot as NativeMontySnapshot, MontyComplete as NativeMontyComplete, MontyException as NativeMontyException, MontyTypingError as NativeMontyTypingError } from './index.js';
|
|
3
|
+
export type { MontyOptions, RunOptions, ResourceLimits, Frame, ExceptionInfo, StartOptions, ResumeOptions, ExceptionInput, SnapshotLoadOptions, JsMontyObject, };
|
|
4
|
+
/**
|
|
5
|
+
* Alias for ResourceLimits (deprecated name).
|
|
6
|
+
*/
|
|
7
|
+
export type JsResourceLimits = ResourceLimits;
|
|
8
|
+
/**
|
|
9
|
+
* Base class for all Monty interpreter errors.
|
|
10
|
+
*
|
|
11
|
+
* This is the parent class for `MontySyntaxError`, `MontyRuntimeError`, and `MontyTypingError`.
|
|
12
|
+
* Catching `MontyError` will catch any exception raised by Monty.
|
|
13
|
+
*/
|
|
14
|
+
export declare class MontyError extends Error {
|
|
15
|
+
protected _typeName: string;
|
|
16
|
+
protected _message: string;
|
|
17
|
+
constructor(typeName: string, message: string);
|
|
18
|
+
/**
|
|
19
|
+
* Returns information about the inner Python exception.
|
|
20
|
+
*/
|
|
21
|
+
get exception(): ExceptionInfo;
|
|
22
|
+
/**
|
|
23
|
+
* Returns formatted exception string.
|
|
24
|
+
* @param format - 'type-msg' for 'ExceptionType: message', 'msg' for just the message
|
|
25
|
+
*/
|
|
26
|
+
display(format?: 'type-msg' | 'msg'): string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Raised when Python code has syntax errors or cannot be parsed by Monty.
|
|
30
|
+
*
|
|
31
|
+
* The inner exception is always a `SyntaxError`. Use `display()` to get
|
|
32
|
+
* formatted error output.
|
|
33
|
+
*/
|
|
34
|
+
export declare class MontySyntaxError extends MontyError {
|
|
35
|
+
private _native;
|
|
36
|
+
constructor(messageOrNative: string | NativeMontyException);
|
|
37
|
+
/**
|
|
38
|
+
* Returns formatted exception string.
|
|
39
|
+
* @param format - 'type-msg' for 'SyntaxError: message', 'msg' for just the message
|
|
40
|
+
*/
|
|
41
|
+
display(format?: 'type-msg' | 'msg'): string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Raised when Monty code fails during execution.
|
|
45
|
+
*
|
|
46
|
+
* Provides access to the traceback frames where the error occurred via `traceback()`,
|
|
47
|
+
* and formatted output via `display()`.
|
|
48
|
+
*/
|
|
49
|
+
export declare class MontyRuntimeError extends MontyError {
|
|
50
|
+
private _native;
|
|
51
|
+
private _tracebackString;
|
|
52
|
+
private _frames;
|
|
53
|
+
constructor(nativeOrTypeName: NativeMontyException | string, message?: string, tracebackString?: string, frames?: Frame[]);
|
|
54
|
+
/**
|
|
55
|
+
* Returns the Monty traceback as an array of Frame objects.
|
|
56
|
+
*/
|
|
57
|
+
traceback(): Frame[];
|
|
58
|
+
/**
|
|
59
|
+
* Returns formatted exception string.
|
|
60
|
+
* @param format - 'traceback' for full traceback, 'type-msg' for 'ExceptionType: message', 'msg' for just the message
|
|
61
|
+
*/
|
|
62
|
+
display(format?: 'traceback' | 'type-msg' | 'msg'): string;
|
|
63
|
+
}
|
|
64
|
+
export type TypingDisplayFormat = 'full' | 'concise' | 'azure' | 'json' | 'jsonlines' | 'rdjson' | 'pylint' | 'gitlab' | 'github';
|
|
65
|
+
/**
|
|
66
|
+
* Raised when type checking finds errors in the code.
|
|
67
|
+
*
|
|
68
|
+
* This exception is raised when static type analysis detects type errors.
|
|
69
|
+
* Use `displayDiagnostics()` to render rich diagnostics in various formats for tooling integration.
|
|
70
|
+
* Use `display()` (inherited) for simple 'type-msg' or 'msg' formats.
|
|
71
|
+
*/
|
|
72
|
+
export declare class MontyTypingError extends MontyError {
|
|
73
|
+
private _native;
|
|
74
|
+
constructor(messageOrNative: string | NativeMontyTypingError, nativeError?: NativeMontyTypingError | null);
|
|
75
|
+
/**
|
|
76
|
+
* Renders rich type error diagnostics for tooling integration.
|
|
77
|
+
*
|
|
78
|
+
* @param format - Output format (default: 'full')
|
|
79
|
+
* @param color - Include ANSI color codes (default: false)
|
|
80
|
+
*/
|
|
81
|
+
displayDiagnostics(format?: TypingDisplayFormat, color?: boolean): string;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Wrapped Monty class that throws proper Error subclasses.
|
|
85
|
+
*/
|
|
86
|
+
export declare class Monty {
|
|
87
|
+
private _native;
|
|
88
|
+
/**
|
|
89
|
+
* Creates a new Monty interpreter by parsing the given code.
|
|
90
|
+
*
|
|
91
|
+
* @param code - Python code to execute
|
|
92
|
+
* @param options - Configuration options
|
|
93
|
+
* @throws {MontySyntaxError} If the code has syntax errors
|
|
94
|
+
* @throws {MontyTypingError} If type checking is enabled and finds errors
|
|
95
|
+
*/
|
|
96
|
+
constructor(code: string, options?: MontyOptions);
|
|
97
|
+
/**
|
|
98
|
+
* Performs static type checking on the code.
|
|
99
|
+
*
|
|
100
|
+
* @param prefixCode - Optional code to prepend before type checking
|
|
101
|
+
* @throws {MontyTypingError} If type checking finds errors
|
|
102
|
+
*/
|
|
103
|
+
typeCheck(prefixCode?: string): void;
|
|
104
|
+
/**
|
|
105
|
+
* Executes the code and returns the result.
|
|
106
|
+
*
|
|
107
|
+
* @param options - Execution options (inputs, limits)
|
|
108
|
+
* @returns The result of the last expression
|
|
109
|
+
* @throws {MontyRuntimeError} If the code raises an exception
|
|
110
|
+
*/
|
|
111
|
+
run(options?: RunOptions): JsMontyObject;
|
|
112
|
+
/**
|
|
113
|
+
* Starts execution and returns either a snapshot (paused at external call) or completion.
|
|
114
|
+
*
|
|
115
|
+
* @param options - Execution options (inputs, limits)
|
|
116
|
+
* @returns MontySnapshot if an external function call is pending, MontyComplete if done
|
|
117
|
+
* @throws {MontyRuntimeError} If the code raises an exception
|
|
118
|
+
*/
|
|
119
|
+
start(options?: StartOptions): MontySnapshot | MontyComplete;
|
|
120
|
+
/**
|
|
121
|
+
* Serializes the Monty instance to a binary format.
|
|
122
|
+
*/
|
|
123
|
+
dump(): Buffer;
|
|
124
|
+
/**
|
|
125
|
+
* Deserializes a Monty instance from binary format.
|
|
126
|
+
*/
|
|
127
|
+
static load(data: Buffer): Monty;
|
|
128
|
+
/** Returns the script name. */
|
|
129
|
+
get scriptName(): string;
|
|
130
|
+
/** Returns the input variable names. */
|
|
131
|
+
get inputs(): string[];
|
|
132
|
+
/** Returns the external function names. */
|
|
133
|
+
get externalFunctions(): string[];
|
|
134
|
+
/** Returns a string representation of the Monty instance. */
|
|
135
|
+
repr(): string;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Represents paused execution waiting for an external function call return value.
|
|
139
|
+
*
|
|
140
|
+
* Contains information about the pending external function call and allows
|
|
141
|
+
* resuming execution with the return value or an exception.
|
|
142
|
+
*/
|
|
143
|
+
export declare class MontySnapshot {
|
|
144
|
+
private _native;
|
|
145
|
+
constructor(nativeSnapshot: NativeMontySnapshot);
|
|
146
|
+
/** Returns the name of the script being executed. */
|
|
147
|
+
get scriptName(): string;
|
|
148
|
+
/** Returns the name of the external function being called. */
|
|
149
|
+
get functionName(): string;
|
|
150
|
+
/** Returns the positional arguments passed to the external function. */
|
|
151
|
+
get args(): JsMontyObject[];
|
|
152
|
+
/** Returns the keyword arguments passed to the external function as an object. */
|
|
153
|
+
get kwargs(): Record<string, JsMontyObject>;
|
|
154
|
+
/**
|
|
155
|
+
* Resumes execution with either a return value or an exception.
|
|
156
|
+
*
|
|
157
|
+
* @param options - Object with either `returnValue` or `exception`
|
|
158
|
+
* @returns MontySnapshot if another external call is pending, MontyComplete if done
|
|
159
|
+
* @throws {MontyRuntimeError} If the code raises an exception
|
|
160
|
+
*/
|
|
161
|
+
resume(options: ResumeOptions): MontySnapshot | MontyComplete;
|
|
162
|
+
/**
|
|
163
|
+
* Serializes the MontySnapshot to a binary format.
|
|
164
|
+
*/
|
|
165
|
+
dump(): Buffer;
|
|
166
|
+
/**
|
|
167
|
+
* Deserializes a MontySnapshot from binary format.
|
|
168
|
+
*/
|
|
169
|
+
static load(data: Buffer, options?: SnapshotLoadOptions): MontySnapshot;
|
|
170
|
+
/** Returns a string representation of the MontySnapshot. */
|
|
171
|
+
repr(): string;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Represents completed execution with a final output value.
|
|
175
|
+
*/
|
|
176
|
+
export declare class MontyComplete {
|
|
177
|
+
private _native;
|
|
178
|
+
constructor(nativeComplete: NativeMontyComplete);
|
|
179
|
+
/** Returns the final output value from the executed code. */
|
|
180
|
+
get output(): JsMontyObject;
|
|
181
|
+
/** Returns a string representation of the MontyComplete. */
|
|
182
|
+
repr(): string;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Options for `runMontyAsync`.
|
|
186
|
+
*/
|
|
187
|
+
export interface RunMontyAsyncOptions {
|
|
188
|
+
/** Input values for the script. */
|
|
189
|
+
inputs?: Record<string, JsMontyObject>;
|
|
190
|
+
/** External function implementations (sync or async). */
|
|
191
|
+
externalFunctions?: Record<string, (...args: unknown[]) => unknown>;
|
|
192
|
+
/** Resource limits. */
|
|
193
|
+
limits?: ResourceLimits;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Runs a Monty script with async external function support.
|
|
197
|
+
*
|
|
198
|
+
* This function handles both synchronous and asynchronous external functions.
|
|
199
|
+
* When an external function returns a Promise, it will be awaited before
|
|
200
|
+
* resuming execution.
|
|
201
|
+
*
|
|
202
|
+
* @param montyRunner - The Monty runner instance to execute
|
|
203
|
+
* @param options - Execution options
|
|
204
|
+
* @returns The output of the Monty script
|
|
205
|
+
* @throws {MontyRuntimeError} If the code raises an exception
|
|
206
|
+
* @throws {MontySyntaxError} If the code has syntax errors
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* const m = new Monty('result = await fetch_data(url)', {
|
|
210
|
+
* inputs: ['url'],
|
|
211
|
+
* externalFunctions: ['fetch_data']
|
|
212
|
+
* });
|
|
213
|
+
*
|
|
214
|
+
* const result = await runMontyAsync(m, {
|
|
215
|
+
* inputs: { url: 'https://example.com' },
|
|
216
|
+
* externalFunctions: {
|
|
217
|
+
* fetch_data: async (url) => {
|
|
218
|
+
* const response = await fetch(url);
|
|
219
|
+
* return response.text();
|
|
220
|
+
* }
|
|
221
|
+
* }
|
|
222
|
+
* });
|
|
223
|
+
*/
|
|
224
|
+
export declare function runMontyAsync(montyRunner: Monty, options?: RunMontyAsyncOptions): Promise<JsMontyObject>;
|
|
225
|
+
//# sourceMappingURL=wrapper.d.ts.map
|