@opencode/codemode 0.0.0-dev-172 → 0.0.0-dev-177
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 +19 -0
- package/dist/codemode.d.ts +9 -7
- package/dist/codemode.js +1 -1
- package/dist/extension.d.ts +10 -4
- package/dist/interpreter/extensions.d.ts +1 -1
- package/dist/interpreter/extensions.js +34 -18
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -104,6 +104,25 @@ const web = Extension.make({ name: "web", globals: { fetch: (url: string) => glo
|
|
|
104
104
|
const runtime = CodeMode.make({ tools, extensions: [web] })
|
|
105
105
|
```
|
|
106
106
|
|
|
107
|
+
A global may return a value, a Promise, or an Effect. An Effect starts only after the `extension.before` hook admits
|
|
108
|
+
the call, and its requirements join the runtime's environment the same way a tool's do, so the host can provide
|
|
109
|
+
per-execution services such as the calling session:
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
const web = Extension.make({
|
|
113
|
+
name: "web",
|
|
114
|
+
globals: {
|
|
115
|
+
fetch: (url: string) =>
|
|
116
|
+
Effect.gen(function* () {
|
|
117
|
+
const session = yield* Session
|
|
118
|
+
yield* session.authorize(url)
|
|
119
|
+
return yield* Effect.promise(() => globalThis.fetch(url))
|
|
120
|
+
}),
|
|
121
|
+
},
|
|
122
|
+
})
|
|
123
|
+
const result = yield* CodeMode.make({ extensions: [web] }).execute(code).pipe(Effect.provideService(Session, session))
|
|
124
|
+
```
|
|
125
|
+
|
|
107
126
|
Every value crossing in either direction is converted, never shared: arguments come in as copies, results go out as
|
|
108
127
|
copies, and a function inside a result is callable the same way. A global that shadows a built-in or another
|
|
109
128
|
extension throws at `CodeMode.make`.
|
package/dist/codemode.d.ts
CHANGED
|
@@ -27,18 +27,20 @@ export type ResolvedExecutionLimits = {
|
|
|
27
27
|
readonly maxOutputBytes: number | undefined;
|
|
28
28
|
};
|
|
29
29
|
/** Configuration shared by `CodeMode.make` and `CodeMode.execute`. */
|
|
30
|
-
export type Options<Provided extends Record<string, unknown> = {}> = {
|
|
30
|
+
export type Options<Provided extends Record<string, unknown> = {}, Extensions extends ReadonlyArray<Extension> = []> = {
|
|
31
31
|
/** Explicit tools exposed to the program as `tools`. */
|
|
32
32
|
tools?: Provided & Tools<Services<Provided>>;
|
|
33
33
|
/** Hooks around every tool and extension call the program makes; see `Hooks`. */
|
|
34
|
-
hooks?: ToolRuntime.Hooks<
|
|
34
|
+
hooks?: ToolRuntime.Hooks<Environment<Provided, Extensions>>;
|
|
35
35
|
/** Host functions exposed as globals; see `Extension.make`. */
|
|
36
|
-
extensions?:
|
|
36
|
+
extensions?: Extensions;
|
|
37
37
|
/** Resource limits enforced on each execution. */
|
|
38
38
|
limits?: ExecutionLimits;
|
|
39
39
|
};
|
|
40
|
+
/** The Effect environment an execution needs: what the tools and the extensions' Effect globals require. */
|
|
41
|
+
export type Environment<Provided extends Record<string, unknown>, Extensions extends ReadonlyArray<Extension>> = Services<Provided> | Extension.Services<Extensions[number]>;
|
|
40
42
|
/** Options for one CodeMode execution. */
|
|
41
|
-
export type ExecuteOptions<Provided extends Record<string, unknown> = {}> = Options<Provided> & {
|
|
43
|
+
export type ExecuteOptions<Provided extends Record<string, unknown> = {}, Extensions extends ReadonlyArray<Extension> = []> = Options<Provided, Extensions> & {
|
|
42
44
|
/** Source for one program in the supported JavaScript subset. */
|
|
43
45
|
code: string;
|
|
44
46
|
};
|
|
@@ -145,6 +147,6 @@ export type Runtime<R = never> = {
|
|
|
145
147
|
readonly execute: (code: string) => Effect.Effect<Result, never, R>;
|
|
146
148
|
};
|
|
147
149
|
/** Executes one Effect-native CodeMode program without constructing a reusable runtime. */
|
|
148
|
-
export declare const execute: <const Provided extends Record<string, unknown
|
|
149
|
-
/** Creates an Effect-native runtime over explicit, schema-described tools. */
|
|
150
|
-
export declare const make: <const Provided extends Record<string, unknown> = {}>(options?: Options<Provided>) => Runtime<
|
|
150
|
+
export declare const execute: <const Provided extends Record<string, unknown>, const Extensions extends ReadonlyArray<Extension> = []>(options: ExecuteOptions<Provided, Extensions>) => Effect.Effect<Result, never, Environment<Provided, Extensions>>;
|
|
151
|
+
/** Creates an Effect-native runtime over explicit, schema-described tools and extensions. */
|
|
152
|
+
export declare const make: <const Provided extends Record<string, unknown> = {}, const Extensions extends ReadonlyArray<Extension> = []>(options?: Options<Provided, Extensions>) => Runtime<Environment<Provided, Extensions>>;
|
package/dist/codemode.js
CHANGED
|
@@ -57,7 +57,7 @@ const resolveExecutionLimits = (limits) => ({
|
|
|
57
57
|
});
|
|
58
58
|
/** Executes one Effect-native CodeMode program without constructing a reusable runtime. */
|
|
59
59
|
export const execute = (options) => make(options).execute(options.code);
|
|
60
|
-
/** Creates an Effect-native runtime over explicit, schema-described tools. */
|
|
60
|
+
/** Creates an Effect-native runtime over explicit, schema-described tools and extensions. */
|
|
61
61
|
export const make = (options = {}) => {
|
|
62
62
|
const prepared = ToolRuntime.prepare((options.tools ?? {}));
|
|
63
63
|
const limits = resolveExecutionLimits(options.limits);
|
package/dist/extension.d.ts
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
export * as Extension from "./extension.js";
|
|
2
|
+
import type { Effect } from "effect";
|
|
2
3
|
/**
|
|
3
4
|
* Host functions a program calls directly as globals. Values crossing in either direction are converted, never
|
|
4
5
|
* shared: arguments come in as copies, results go out as copies, and a function inside a result is callable the
|
|
5
|
-
* same way. Extension calls are not tool calls.
|
|
6
|
+
* same way. Extension calls are not tool calls. A global may return a value, a Promise, or an Effect; an Effect runs
|
|
7
|
+
* only after the extension hooks admit the call, and its requirements join the runtime's environment.
|
|
6
8
|
*/
|
|
7
|
-
export type Extension = {
|
|
9
|
+
export type Extension<Globals extends Readonly<Record<string, Function>> = Readonly<Record<string, Function>>> = {
|
|
8
10
|
readonly name: string;
|
|
9
|
-
readonly globals:
|
|
11
|
+
readonly globals: Globals;
|
|
10
12
|
};
|
|
11
|
-
|
|
13
|
+
/** The Effect environment required by an extension's Effect-returning globals. */
|
|
14
|
+
export type Services<E> = E extends {
|
|
15
|
+
readonly globals: infer Globals;
|
|
16
|
+
} ? Globals[keyof Globals] extends infer Global ? Global extends (...args: never) => infer Result ? Effect.Services<Result> : never : never : never;
|
|
17
|
+
export declare const make: <const Globals extends Readonly<Record<string, Function>>>(options: Extension<Globals>) => Extension<Globals>;
|
|
@@ -4,6 +4,6 @@ import { type Value } from "./objects.js";
|
|
|
4
4
|
/**
|
|
5
5
|
* The global bindings of one run's extensions. Everything crossing the boundary is converted: plain data and
|
|
6
6
|
* built-in wrappers are copied, a host function becomes a program function whose calls cross the same way, and a
|
|
7
|
-
* host Promise becomes a program promise.
|
|
7
|
+
* host Promise or Effect becomes a program promise.
|
|
8
8
|
*/
|
|
9
9
|
export declare const extensionGlobals: <R>(ctx: Interpreter<R>, extensions: ReadonlyArray<Extension>) => ReadonlyArray<readonly [string, Value]>;
|
|
@@ -9,7 +9,7 @@ import { describeValue, isOpaque } from "./references.js";
|
|
|
9
9
|
/**
|
|
10
10
|
* The global bindings of one run's extensions. Everything crossing the boundary is converted: plain data and
|
|
11
11
|
* built-in wrappers are copied, a host function becomes a program function whose calls cross the same way, and a
|
|
12
|
-
* host Promise becomes a program promise.
|
|
12
|
+
* host Promise or Effect becomes a program promise.
|
|
13
13
|
*/
|
|
14
14
|
export const extensionGlobals = (ctx, extensions) => {
|
|
15
15
|
const builtins = ctx.builtins;
|
|
@@ -127,27 +127,43 @@ export const extensionGlobals = (ctx, extensions) => {
|
|
|
127
127
|
// program catches a copy. Functions inside results are part of a value's API and skip the hooks.
|
|
128
128
|
const wrap = (value, label, describe) => fn(builtins, value.name, value.length, (_, values, site) => {
|
|
129
129
|
const args = values.map((item, index) => toHost(item, `Argument ${index + 1} to ${label}`));
|
|
130
|
+
const call = callHost(value, args);
|
|
130
131
|
const hooks = ctx.tools.hooks;
|
|
131
|
-
const
|
|
132
|
-
?
|
|
133
|
-
: hooked(describe(args), hooks["extension.before"], hooks["extension.after"],
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
catch (reason) {
|
|
139
|
-
return blocking(ctx.runner.blocking(settle(Effect.fail(reason))));
|
|
140
|
-
}
|
|
141
|
-
if (!(result instanceof Promise))
|
|
142
|
-
return blocking(ctx.runner.blocking(settle(Effect.succeed(result))));
|
|
143
|
-
const runner = ctx.runner;
|
|
144
|
-
const promise = runner.newPromise();
|
|
145
|
-
const resolving = runner.resolving(promise, site);
|
|
146
|
-
runner.host(settle(Effect.tryPromise({ try: () => result, catch: (reason) => reason })), (settled) => (settled.ok ? resolving.resolve(settled.value) : resolving.reject(settled.error)), site);
|
|
147
|
-
return promise;
|
|
132
|
+
const work = (describe === undefined
|
|
133
|
+
? call.effect
|
|
134
|
+
: hooked(describe(args), hooks["extension.before"], hooks["extension.after"], call.effect)).pipe(Effect.mapBoth({
|
|
135
|
+
onFailure: (reason) => new Throw(fromHost(reason, label)),
|
|
136
|
+
onSuccess: (result) => fromHost(result, label),
|
|
137
|
+
}));
|
|
138
|
+
return call.async ? pending(work, site) : blocking(ctx.runner.blocking(work));
|
|
148
139
|
});
|
|
140
|
+
// An asynchronous host call: the program holds a promise that settles when the host's work does.
|
|
141
|
+
const pending = (work, site) => {
|
|
142
|
+
const promise = ctx.runner.newPromise();
|
|
143
|
+
const resolving = ctx.runner.resolving(promise, site);
|
|
144
|
+
ctx.runner.host(work, (settled) => (settled.ok ? resolving.resolve(settled.value) : resolving.reject(settled.error)), site);
|
|
145
|
+
return promise;
|
|
146
|
+
};
|
|
149
147
|
return extensions.flatMap((extension) => Object.entries(extension.globals).map(([name, value]) => [name, wrap(value, name, (args) => ({ extension: extension.name, name, args }))]));
|
|
150
148
|
};
|
|
149
|
+
/**
|
|
150
|
+
* Calls a host function and describes what the program waits on. A returned Effect or Promise settles later; a
|
|
151
|
+
* returned value or a throw settles now. An Effect has not started yet, so it runs only once the hooks admit it.
|
|
152
|
+
*/
|
|
153
|
+
const callHost = (value, args) => {
|
|
154
|
+
try {
|
|
155
|
+
const result = value.apply(undefined, args);
|
|
156
|
+
// Extension.Services adds an Effect global's requirements to R.
|
|
157
|
+
if (Effect.isEffect(result))
|
|
158
|
+
return { async: true, effect: result };
|
|
159
|
+
if (result instanceof Promise)
|
|
160
|
+
return { async: true, effect: Effect.tryPromise({ try: () => result, catch: (reason) => reason }) };
|
|
161
|
+
return { async: false, effect: Effect.succeed(result) };
|
|
162
|
+
}
|
|
163
|
+
catch (reason) {
|
|
164
|
+
return { async: false, effect: Effect.fail(reason) };
|
|
165
|
+
}
|
|
166
|
+
};
|
|
151
167
|
// A synchronous host call: its hooks run before any other job, as the call itself did.
|
|
152
168
|
function* blocking(host) {
|
|
153
169
|
return (yield host);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "@opencode/codemode",
|
|
4
|
-
"version": "0.0.0-dev-
|
|
4
|
+
"version": "0.0.0-dev-177",
|
|
5
5
|
"description": "Effect-native confined code execution over schema-described tools",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"license": "MIT",
|