@six5536/smllm-wasm 0.4.1 → 0.6.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 +136 -2
- package/dist/node/host.d.ts +1 -1
- package/dist/node/host.js +4 -4
- package/dist/node/storage.js +1 -2
- package/dist/storage.js +1 -2
- package/dist/types.d.ts +96 -4
- package/dist/wrap.d.ts +40 -6
- package/dist/wrap.js +55 -4
- package/package.json +1 -1
- package/wasm/bundler/smllm_wasm.d.ts +30 -5
- package/wasm/bundler/smllm_wasm_bg.js +192 -15
- package/wasm/bundler/smllm_wasm_bg.wasm +0 -0
- package/wasm/bundler/smllm_wasm_bg.wasm.d.ts +5 -0
- package/wasm/web/smllm_wasm.d.ts +35 -5
- package/wasm/web/smllm_wasm.js +192 -15
- package/wasm/web/smllm_wasm_bg.wasm +0 -0
- package/wasm/web/smllm_wasm_bg.wasm.d.ts +5 -0
package/README.md
CHANGED
|
@@ -6,11 +6,13 @@ or Node. The large language model (LLM) can run anywhere. The API is typed: it t
|
|
|
6
6
|
objects, and the package includes TypeScript declarations.
|
|
7
7
|
|
|
8
8
|
This README helps you run the engine in your own host. It shows a first example, then describes
|
|
9
|
-
the host methods, storage, the
|
|
10
|
-
CLI.
|
|
9
|
+
the host methods, storage, the events that only the host fires, machines that the host drives for
|
|
10
|
+
a model without tools, the Node helpers, and when to use the WebAssembly engine instead of the CLI.
|
|
11
11
|
|
|
12
12
|
- [Host](#host)
|
|
13
13
|
- [Storage](#storage)
|
|
14
|
+
- [Host-only events](#host-only-events)
|
|
15
|
+
- [Host-driven machines](#host-driven-machines)
|
|
14
16
|
- [Run in Node](#run-in-node)
|
|
15
17
|
- [WebAssembly or CLI](#webassembly-or-cli)
|
|
16
18
|
- [Licence](#licence)
|
|
@@ -43,6 +45,10 @@ const stop = engine.stop(session, stopHookActive);
|
|
|
43
45
|
// "runaway": let it stop, show stop.text to the user; "allow": let it stop.
|
|
44
46
|
```
|
|
45
47
|
|
|
48
|
+
A param takes a string. A param that the machine declares with `type: json` takes any JSON value,
|
|
49
|
+
in `callTool`, `fire`, and `fireAsHost`, and the host receives its compact JSON text. A JSON string stays a string, even when its text spells an object. The events list shows
|
|
50
|
+
the param as a JSON value, with its JSON Schema.
|
|
51
|
+
|
|
46
52
|
The `examples/wasm` folder of the [smllm repository](https://github.com/six5536/smllm) holds a
|
|
47
53
|
whole agent loop, in TypeScript.
|
|
48
54
|
|
|
@@ -63,6 +69,7 @@ host leaves the method out:
|
|
|
63
69
|
- `random()` returns a random number. By default, it uses `crypto.getRandomValues`.
|
|
64
70
|
- `history(machine, id, entry)` and `put(kind, key, record)` receive writes. By default, the
|
|
65
71
|
engine drops them.
|
|
72
|
+
- `wantsAudit()` and `audit(entry, reply)` keep an audit, as the following section describes.
|
|
66
73
|
|
|
67
74
|
A host method that throws is a host failure: a failed guard or action, or a rejected pattern. The
|
|
68
75
|
reply text shows the failure, and the engine stays usable.
|
|
@@ -80,6 +87,133 @@ write.
|
|
|
80
87
|
IndexedDB, or one that sends the records to a server. `exportState()` and `importState(state)`
|
|
81
88
|
move the whole state at once, for backups and moves.
|
|
82
89
|
|
|
90
|
+
## Audit
|
|
91
|
+
|
|
92
|
+
An audit records every call that drove a session, accepted or refused: `bind`, `callTool`,
|
|
93
|
+
`events`, `fire`, `fireAsHost`, and `stop`. `view`, `status`, and `promptSubmitted` record
|
|
94
|
+
nothing. To keep an audit, give the storage or the host `wantsAudit()`, which returns true, and
|
|
95
|
+
`audit(entry, reply)`, which receives each `AuditEntry` and the text of the call's reply:
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
const storage = {
|
|
99
|
+
...memoryStorage(),
|
|
100
|
+
wantsAudit: () => true,
|
|
101
|
+
audit(entry: AuditEntry, reply: string) {
|
|
102
|
+
console.log(entry.source, entry.call, entry.event, entry.outcome, entry.reason);
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
An entry holds the time, the session, the source (`agent`, or `host` for `fireAsHost`), the call,
|
|
108
|
+
the instance with its states before and after, the params, the names of the events that the reply
|
|
109
|
+
offered, the outcome with the reason for a refusal, and for a call that created the session its
|
|
110
|
+
binding. Keep what you need of the reply's text. A hash names its algorithm, as `sha256:` and the
|
|
111
|
+
hex of the SHA-256 of the text's UTF-8 bytes, which is the default that the `smllm` CLI uses.
|
|
112
|
+
|
|
113
|
+
## Host-only events
|
|
114
|
+
|
|
115
|
+
A host-only event is an event that only the host fires, such as the result of checks that the host
|
|
116
|
+
runs outside the machine. A machine marks it with `source: host` in `meta.events`:
|
|
117
|
+
|
|
118
|
+
```yaml
|
|
119
|
+
meta:
|
|
120
|
+
smllm: 1
|
|
121
|
+
events:
|
|
122
|
+
checksPassed: { source: host }
|
|
123
|
+
checksFailed: { source: host }
|
|
124
|
+
states:
|
|
125
|
+
WORK:
|
|
126
|
+
on: { submit: CHECKING }
|
|
127
|
+
CHECKING:
|
|
128
|
+
on: { checksPassed: DONE, checksFailed: WORK }
|
|
129
|
+
DONE: { type: final }
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
The agent never sees a host-only event: the events lists of the tool's replies, of `view`, of
|
|
133
|
+
`events`, and of `stop` leave it out. `fire` and `callTool` refuse it with an error that names it
|
|
134
|
+
as an event that only the host fires. The host fires it with `fireAsHost`, which takes the same
|
|
135
|
+
arguments as `fire` and fires any event that the state offers:
|
|
136
|
+
|
|
137
|
+
```js
|
|
138
|
+
engine.callTool({ session, event: "submit" }); // the agent: WORK → CHECKING
|
|
139
|
+
const passed = await runChecks(); // your own checks, which can be async
|
|
140
|
+
engine.fireAsHost(session, passed ? "checksPassed" : "checksFailed");
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Fire an event that the agent chose with `fire` or `callTool`, never with `fireAsHost`, because
|
|
144
|
+
`fireAsHost` would then take a host-only event from the agent. Keep `fireAsHost` for the host's
|
|
145
|
+
own events.
|
|
146
|
+
|
|
147
|
+
`source: host` is a security boundary only when the host drives a model without tools. An agent
|
|
148
|
+
that runs under a harness, such as Claude Code, usually has a shell, and there the marker
|
|
149
|
+
prevents mistakes rather than an attack. A harness has no host to fire the events, so the smllm
|
|
150
|
+
CLI refuses to start an instance of such a machine, and `smllm validate` notes that the machine
|
|
151
|
+
needs a host to drive it.
|
|
152
|
+
|
|
153
|
+
## Host-driven machines
|
|
154
|
+
|
|
155
|
+
A host can drive a machine for a model that has no tools and only answers in text, such as a
|
|
156
|
+
model that the host calls through the Messages API with no tools. The host shows the model the
|
|
157
|
+
state, reads its answer as one event, runs its own checks, and fires the results. Four methods
|
|
158
|
+
serve it, and none of them changes anything:
|
|
159
|
+
|
|
160
|
+
- `hostView(key)` returns `{ location, model, host }`: where the session is, the events that the
|
|
161
|
+
model can fire, and the events that only the host fires. Each event has its `name`,
|
|
162
|
+
`description`, and `params`, and a JSON param has its bundled `schema` as an object.
|
|
163
|
+
- `stateText(key, { schemas })` returns the state's text for the model: its instructions, the
|
|
164
|
+
model's events with their params, and a request to answer with one event as JSON. Each JSON
|
|
165
|
+
param shows its schema, unless `schemas` is `false`.
|
|
166
|
+
- `answerSchema(key)` returns a JSON Schema of a valid answer, as an object, for a model that can
|
|
167
|
+
constrain its output, such as the Claude API's structured outputs.
|
|
168
|
+
- `checkAnswer(key, text)` reads the model's answer, a bare JSON object or one fenced block with
|
|
169
|
+
nothing outside it. It returns `{ ok: true, event, params }`, whose params are plain
|
|
170
|
+
JavaScript values ready for `fire`, or `{ ok: false, error }`, whose error the host sends back
|
|
171
|
+
to the model as it stands. Every malformed answer gets the same error.
|
|
172
|
+
|
|
173
|
+
This loop runs a machine from start to end. The machine is an extractor builder for bank
|
|
174
|
+
statements, which is in the `examples/extractor` folder of the smllm repository. The model
|
|
175
|
+
answers with an extractor, a JSON value with a schema, or asks for more pages. The host checks
|
|
176
|
+
the extractor and fires `checks_passed` or `checks_failed`, and the machine gives up after five
|
|
177
|
+
attempts:
|
|
178
|
+
|
|
179
|
+
```js
|
|
180
|
+
const engine = new Engine(compiled, {}, memoryStorage());
|
|
181
|
+
// A keyless `enter` creates the session and starts the machine in one call.
|
|
182
|
+
const { session } = engine.fire(undefined, "enter", { stateMachine: "extractor-build" });
|
|
183
|
+
for (;;) {
|
|
184
|
+
const view = engine.hostView(session);
|
|
185
|
+
if (view.location.machine === null) break; // a final state: the session is idle again
|
|
186
|
+
if (view.model.length > 0) {
|
|
187
|
+
let prompt = `${engine.stateText(session)}\n${statement}`;
|
|
188
|
+
for (;;) {
|
|
189
|
+
const answer = engine.checkAnswer(session, await askModel(prompt)); // your adapter
|
|
190
|
+
if (answer.ok) {
|
|
191
|
+
engine.fire(session, answer.event, answer.params); // the model chose the event
|
|
192
|
+
if (answer.event === "extractor") extractor = answer.params.json;
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
prompt = answer.error; // the refusal, as it stands; count it as an attempt
|
|
196
|
+
}
|
|
197
|
+
} else {
|
|
198
|
+
const errors = await runChecks(extractor); // your checks, which can be async
|
|
199
|
+
if (errors.length === 0) engine.fireAsHost(session, "checks_passed");
|
|
200
|
+
else engine.fireAsHost(session, "checks_failed", { errors: errors.join("\n") });
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
The model is never offered a built-in event, such as `yield`, `pause`, or `unmatched`, or a
|
|
206
|
+
host-only event, because those belong to whoever drives the session, which here is the host. The
|
|
207
|
+
host fires them as it needs them. An outcome that the model can choose, such as "I can't do this"
|
|
208
|
+
or "ask the user", is an event that the machine declares.
|
|
209
|
+
|
|
210
|
+
One machine can mix the model's steps, the host's deterministic steps, and a person's steps. A
|
|
211
|
+
state that offers the model events is a step of the model. A state whose events are all the
|
|
212
|
+
host's is a step of the host, such as `CHECKING`, or a person's step, such as an approval for
|
|
213
|
+
which the host fires the person's decision. A state that offers both lists is a step of the model
|
|
214
|
+
that the host can interrupt: the extractor builder's `READ` offers the host `timed_out`, which the
|
|
215
|
+
host fires when the model takes too long.
|
|
216
|
+
|
|
83
217
|
## Run in Node
|
|
84
218
|
|
|
85
219
|
`@six5536/smllm-wasm/node` provides a host and a storage for Node:
|
package/dist/node/host.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Host } from "../types.js";
|
|
2
|
-
/** `timeoutSecs` default (
|
|
2
|
+
/** `timeoutSecs` default (CLI-11), as the CLI's. */
|
|
3
3
|
export declare const DEFAULT_TIMEOUT_SECS = 60;
|
|
4
4
|
/**
|
|
5
5
|
* A host that runs `command` guards and actions. `configDir` is the folder
|
package/dist/node/host.js
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
// The Node command host (
|
|
2
|
-
// and actions run as the CLI runs them (
|
|
1
|
+
// The Node command host (WASM-3): `command` guards
|
|
2
|
+
// and actions run as the CLI runs them (CLI-11..CLI-11). Host methods are
|
|
3
3
|
// synchronous, so commands run with `spawnSync`. On unix the command is
|
|
4
4
|
// started detached, as the leader of its own process group, and a timeout
|
|
5
5
|
// kills that whole group, so what the command started dies with it; its
|
|
6
6
|
// output goes to a file, not a pipe, so a process it leaves running cannot
|
|
7
7
|
// hold the call open. On Windows the command's own process is killed, as the
|
|
8
8
|
// CLI does.
|
|
9
|
-
// @zen-component: HOST-JsPackage
|
|
10
9
|
import { spawnSync } from "node:child_process";
|
|
11
10
|
import { closeSync, mkdtempSync, openSync, readFileSync, rmSync } from "node:fs";
|
|
12
11
|
import { tmpdir } from "node:os";
|
|
13
12
|
import { join, resolve } from "node:path";
|
|
14
|
-
/** `timeoutSecs` default (
|
|
13
|
+
/** `timeoutSecs` default (CLI-11), as the CLI's. */
|
|
15
14
|
export const DEFAULT_TIMEOUT_SECS = 60;
|
|
16
15
|
const TAIL_CHARS = 400;
|
|
17
16
|
/** The last `TAIL_CHARS` characters of `text`, on one line (as the CLI's). */
|
|
@@ -22,6 +21,7 @@ function tail(text) {
|
|
|
22
21
|
return cut ? `…${s}` : s;
|
|
23
22
|
}
|
|
24
23
|
/** A command's failure text, `""` when it passed. */
|
|
24
|
+
// sokf-implements: WASM-3_AC-1
|
|
25
25
|
function runCommand(params, env, sessionCwd, configDir, timeoutSecs) {
|
|
26
26
|
const run = params.run;
|
|
27
27
|
let argv;
|
package/dist/node/storage.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
// Node file storage (
|
|
1
|
+
// Node file storage (WASM-2): a file per record, written
|
|
2
2
|
// atomically (temp file, then rename), and history as JSONL, one line per
|
|
3
3
|
// entry in the CLI's format. One process owns a folder: several processes
|
|
4
4
|
// sharing state should use the CLI, whose store locks.
|
|
5
|
-
// @zen-component: HOST-JsPackage
|
|
6
5
|
import { appendFileSync, mkdirSync, readdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
7
6
|
import { join } from "node:path";
|
|
8
7
|
const SHELF = {
|
package/dist/storage.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
// In-memory storage (
|
|
1
|
+
// In-memory storage (WASM-2): what `nodeFileStorage` does with files,
|
|
2
2
|
// without them; for tests, examples and hosts that persist elsewhere.
|
|
3
|
-
// @zen-component: HOST-JsPackage
|
|
4
3
|
/** A storage that keeps everything in memory. `history` holds the log by `machine/id`. */
|
|
5
4
|
export function memoryStorage() {
|
|
6
5
|
const state = { sessions: {}, bindings: {}, instances: {} };
|
package/dist/types.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export interface Reply {
|
|
|
13
13
|
location: Location;
|
|
14
14
|
text: string;
|
|
15
15
|
}
|
|
16
|
-
/** The stop hook's decision (
|
|
16
|
+
/** The stop hook's decision (AGT-8..6). */
|
|
17
17
|
export type StopDecision = {
|
|
18
18
|
decision: "allow";
|
|
19
19
|
}
|
|
@@ -53,6 +53,43 @@ export interface HistoryEntry {
|
|
|
53
53
|
params: Record<string, string>;
|
|
54
54
|
trace: string[];
|
|
55
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* One audit entry: a call that drove a session, accepted or refused, handed
|
|
58
|
+
* to `audit` with the text of its reply when the host or the storage wants
|
|
59
|
+
* an audit.
|
|
60
|
+
*/
|
|
61
|
+
export interface AuditEntry {
|
|
62
|
+
/** Unix ms. */
|
|
63
|
+
at: number;
|
|
64
|
+
session: string;
|
|
65
|
+
/** `host` for `fireAsHost`, else `agent`. */
|
|
66
|
+
source: "agent" | "host";
|
|
67
|
+
/** `view` is a tool call with no event. */
|
|
68
|
+
call: "bind" | "view" | "menu" | "fire" | "stop";
|
|
69
|
+
event: string | null;
|
|
70
|
+
machine: string | null;
|
|
71
|
+
instance: string | null;
|
|
72
|
+
/** The instance's state before the call. */
|
|
73
|
+
from: string | null;
|
|
74
|
+
/** The state where the session is after the call. */
|
|
75
|
+
to: string | null;
|
|
76
|
+
/** Each param as its text, a JSON value as its compact JSON. */
|
|
77
|
+
params: Record<string, string>;
|
|
78
|
+
/** The names of the events that the reply offered. */
|
|
79
|
+
offered: string[];
|
|
80
|
+
/** `accepted` or `refused`; for `stop`, the decision. */
|
|
81
|
+
outcome: "accepted" | "refused" | "allow" | "block" | "runaway";
|
|
82
|
+
/** Why the engine refused the call. */
|
|
83
|
+
reason: string | null;
|
|
84
|
+
trace: string[];
|
|
85
|
+
/** The binding of the session that the call created. */
|
|
86
|
+
binding: {
|
|
87
|
+
harness: string;
|
|
88
|
+
hostSession: string | null;
|
|
89
|
+
cwd: string;
|
|
90
|
+
configs: string[];
|
|
91
|
+
} | null;
|
|
92
|
+
}
|
|
56
93
|
/**
|
|
57
94
|
* What the engine needs from its host. Every method is optional:
|
|
58
95
|
* - `supports`, `check`, `run`: host guard/action kinds (`command`); none by default.
|
|
@@ -62,6 +99,9 @@ export interface HistoryEntry {
|
|
|
62
99
|
* - `now`, `random`: `Date.now()` and `crypto.getRandomValues` by default.
|
|
63
100
|
* - `history`: each history entry as it happens; dropped by default.
|
|
64
101
|
* - `put`: each session, binding and instance as it is saved (write-through storage); dropped by default.
|
|
102
|
+
* - `wantsAudit`, `audit`: whether to keep an audit, false by default, and each audit entry with the
|
|
103
|
+
* text of its reply. The host keeps what it likes of the text, such as `sha256:` and the hex of the
|
|
104
|
+
* SHA-256 of its UTF-8 bytes, the default that `smllm` uses.
|
|
65
105
|
* A method that throws is a host failure the engine reports, never a broken engine.
|
|
66
106
|
*/
|
|
67
107
|
export interface Host {
|
|
@@ -75,18 +115,25 @@ export interface Host {
|
|
|
75
115
|
random?(): number;
|
|
76
116
|
history?(machine: string, id: string, entry: HistoryEntry): void;
|
|
77
117
|
put?(kind: RecordKind, key: string, record: unknown): void;
|
|
118
|
+
wantsAudit?(): boolean;
|
|
119
|
+
audit?(entry: AuditEntry, reply: string): void;
|
|
78
120
|
}
|
|
79
121
|
/** What `put` hands on: a session (key: its key), a binding (`harness/hostSession`, record: the session key) or an instance (`machine/id`). */
|
|
80
122
|
export type RecordKind = "session" | "binding" | "instance";
|
|
81
123
|
/**
|
|
82
|
-
* Where an engine's state lives (
|
|
124
|
+
* Where an engine's state lives (WASM-2): `load` once, into `importState`;
|
|
83
125
|
* then each saved record through `put` and each history entry through
|
|
84
|
-
* `history`, as they happen, so persisting costs what changed
|
|
126
|
+
* `history`, as they happen, so persisting costs what changed; and, when
|
|
127
|
+
* `wantsAudit` returns true, each audit entry through `audit`.
|
|
85
128
|
*/
|
|
86
129
|
export interface Storage {
|
|
87
130
|
load(): State | undefined;
|
|
88
131
|
put(kind: RecordKind, key: string, record: unknown): void;
|
|
89
132
|
history(machine: string, id: string, entry: HistoryEntry): void;
|
|
133
|
+
/** Whether the storage keeps an audit; false when absent. */
|
|
134
|
+
wantsAudit?(): boolean;
|
|
135
|
+
/** Each audit entry, with the text of the call's reply. */
|
|
136
|
+
audit?(entry: AuditEntry, reply: string): void;
|
|
90
137
|
}
|
|
91
138
|
/** The agent-facing `smllm` tool: the same definition the MCP server offers. */
|
|
92
139
|
export interface Tool {
|
|
@@ -99,8 +146,53 @@ export interface Tool {
|
|
|
99
146
|
export interface ToolArgs {
|
|
100
147
|
session?: string;
|
|
101
148
|
event?: string;
|
|
102
|
-
|
|
149
|
+
/** A string for each param, or any JSON value for a param that the events list shows as a JSON value. */
|
|
150
|
+
params?: Record<string, unknown>;
|
|
103
151
|
}
|
|
152
|
+
/** How `stateText` renders the state. */
|
|
153
|
+
export interface StateTextOptions {
|
|
154
|
+
/** Show each JSON param's schema; `true` by default. */
|
|
155
|
+
schemas?: boolean;
|
|
156
|
+
}
|
|
157
|
+
/** A param of an offered event. */
|
|
158
|
+
export interface OfferedParam {
|
|
159
|
+
name: string;
|
|
160
|
+
required: boolean;
|
|
161
|
+
/** `json`: the param takes any JSON value. */
|
|
162
|
+
type: "string" | "json";
|
|
163
|
+
/** The allowed values; empty when any string is allowed. */
|
|
164
|
+
enum: string[];
|
|
165
|
+
pattern: string | null;
|
|
166
|
+
description: string | null;
|
|
167
|
+
/** A JSON param's JSON Schema, bundled, or `null`. */
|
|
168
|
+
schema: Record<string, unknown> | boolean | null;
|
|
169
|
+
}
|
|
170
|
+
/** An event that the state offers. */
|
|
171
|
+
export interface Offered {
|
|
172
|
+
name: string;
|
|
173
|
+
description: string | null;
|
|
174
|
+
params: OfferedParam[];
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* The state as a host that drives the machine sees it: the events that the
|
|
178
|
+
* model can answer with, and the events that only the host fires (with
|
|
179
|
+
* `fireAsHost`). Neither list holds the built-in events, which the host
|
|
180
|
+
* fires as it needs them.
|
|
181
|
+
*/
|
|
182
|
+
export interface HostView {
|
|
183
|
+
location: Location;
|
|
184
|
+
model: Offered[];
|
|
185
|
+
host: Offered[];
|
|
186
|
+
}
|
|
187
|
+
/** A model's answer, checked: the event and its params, ready for `fire`, or the refusal to send back to the model. */
|
|
188
|
+
export type CheckedAnswer = {
|
|
189
|
+
ok: true;
|
|
190
|
+
event: string;
|
|
191
|
+
params: Record<string, unknown>;
|
|
192
|
+
} | {
|
|
193
|
+
ok: false;
|
|
194
|
+
error: string;
|
|
195
|
+
};
|
|
104
196
|
/** Sessions, bindings and instances by key, as `exportState` returns them and `importState` takes them. */
|
|
105
197
|
export interface State {
|
|
106
198
|
sessions: Record<string, unknown>;
|
package/dist/wrap.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Host, RecordKind, Reply, SessionStatus, State, Storage, StopDecision, Tool, ToolArgs } from "./types.js";
|
|
1
|
+
import type { CheckedAnswer, Host, HostView, RecordKind, Reply, SessionStatus, State, StateTextOptions, Storage, StopDecision, Tool, ToolArgs } from "./types.js";
|
|
2
2
|
/** The host the wasm calls: strings in, strings out, every method there. */
|
|
3
3
|
export interface RawHost {
|
|
4
4
|
supports(kind: string): boolean;
|
|
@@ -10,6 +10,8 @@ export interface RawHost {
|
|
|
10
10
|
random(): number;
|
|
11
11
|
history(machine: string, id: string, entry: string): void;
|
|
12
12
|
put(kind: RecordKind, key: string, record: string): void;
|
|
13
|
+
wantsAudit(): boolean;
|
|
14
|
+
audit(entry: string, reply: string): void;
|
|
13
15
|
}
|
|
14
16
|
/** A build's wasm-bindgen `Engine`: JSON text in and out. */
|
|
15
17
|
export interface RawEngine {
|
|
@@ -21,8 +23,13 @@ export interface RawEngine {
|
|
|
21
23
|
events(key: string): string;
|
|
22
24
|
status(key: string): string;
|
|
23
25
|
fire(key: string | null | undefined, event: string, params: string): string;
|
|
26
|
+
fireAsHost(key: string | null | undefined, event: string, params: string): string;
|
|
24
27
|
stop(key: string, stopHookActive: boolean): string;
|
|
25
28
|
promptSubmitted(key: string): void;
|
|
29
|
+
stateText(key: string, schemas: boolean): string;
|
|
30
|
+
answerSchema(key: string): string;
|
|
31
|
+
checkAnswer(key: string, text: string): string;
|
|
32
|
+
hostView(key: string): string;
|
|
26
33
|
exportState(): string;
|
|
27
34
|
importState(state: string): void;
|
|
28
35
|
free(): void;
|
|
@@ -36,10 +43,11 @@ export declare class Engine {
|
|
|
36
43
|
protected static Raw: RawClass;
|
|
37
44
|
/**
|
|
38
45
|
* `storage`, when given, is loaded now and then receives every saved
|
|
39
|
-
* record and history entry
|
|
46
|
+
* record and history entry, and each audit entry when it wants an audit
|
|
47
|
+
* (its `put`, `history` and `audit` go before `host`'s).
|
|
40
48
|
*/
|
|
41
49
|
constructor(compiled: string | object, host?: Host, storage?: Storage);
|
|
42
|
-
/** The agent-facing tool, to hand to an LLM's tool list (
|
|
50
|
+
/** The agent-facing tool, to hand to an LLM's tool list (WASM-1). */
|
|
43
51
|
tool(): Tool;
|
|
44
52
|
/** Answer a tool call as the MCP server does; a malformed call is `ok: false`, never a throw. */
|
|
45
53
|
callTool(args?: ToolArgs): Reply;
|
|
@@ -47,15 +55,41 @@ export declare class Engine {
|
|
|
47
55
|
unsupported(): string[];
|
|
48
56
|
/** Bind a harness session (reused when `hostSession` is already bound). */
|
|
49
57
|
bind(harness: string, hostSession?: string, cwd?: string): Reply;
|
|
50
|
-
/** Where the session is; changes nothing. */
|
|
58
|
+
/** Where the session is; changes nothing and records no audit entry. A look of the agent's is `callTool` with no event. */
|
|
51
59
|
view(key: string): Reply;
|
|
52
60
|
/** The events list of the session's state. */
|
|
53
61
|
events(key: string): Reply;
|
|
54
62
|
status(key: string): SessionStatus;
|
|
55
|
-
/**
|
|
56
|
-
|
|
63
|
+
/**
|
|
64
|
+
* Fire an event for the agent; with no key, only `enter` (it starts a
|
|
65
|
+
* session). An event that the machine marks with `source: host` is
|
|
66
|
+
* refused: fire it with `fireAsHost`. Fire an event that the agent chose
|
|
67
|
+
* here, never with `fireAsHost`. A param takes a string, or any JSON
|
|
68
|
+
* value when the events list shows it as a JSON value.
|
|
69
|
+
*/
|
|
70
|
+
fire(key: string | undefined, event: string, params?: Record<string, unknown>): Reply;
|
|
71
|
+
/**
|
|
72
|
+
* Fire an event as the host: as `fire`, and also an event that the machine
|
|
73
|
+
* marks with `source: host`, such as the result of the host's own checks.
|
|
74
|
+
*/
|
|
75
|
+
fireAsHost(key: string | undefined, event: string, params?: Record<string, unknown>): Reply;
|
|
57
76
|
stop(key: string, stopHookActive?: boolean): StopDecision;
|
|
58
77
|
promptSubmitted(key: string): void;
|
|
78
|
+
/**
|
|
79
|
+
* The state's text for a model without tools: its instructions, the
|
|
80
|
+
* events that the model can answer with, and how to answer. Each JSON
|
|
81
|
+
* param shows its schema unless `schemas` is `false`.
|
|
82
|
+
*/
|
|
83
|
+
stateText(key: string, options?: StateTextOptions): string;
|
|
84
|
+
/** The JSON Schema of a valid answer: a `oneOf` with one branch for each event of the model. */
|
|
85
|
+
answerSchema(key: string): Record<string, unknown>;
|
|
86
|
+
/**
|
|
87
|
+
* Check the model's text answer: the event and its params, ready for
|
|
88
|
+
* `fire`, or a refusal to send back to the model. It fires nothing.
|
|
89
|
+
*/
|
|
90
|
+
checkAnswer(key: string, text: string): CheckedAnswer;
|
|
91
|
+
/** The state's offered events in two lists: the model's, and the host's own. */
|
|
92
|
+
hostView(key: string): HostView;
|
|
59
93
|
exportState(): State;
|
|
60
94
|
importState(state: State | string): void;
|
|
61
95
|
/** Free the wasm memory now, rather than when collected. */
|
package/dist/wrap.js
CHANGED
|
@@ -18,6 +18,10 @@ function adapt(host) {
|
|
|
18
18
|
put: (kind, key, record) => {
|
|
19
19
|
host.put?.(kind, key, parse(record));
|
|
20
20
|
},
|
|
21
|
+
wantsAudit: () => Boolean(host.wantsAudit?.()),
|
|
22
|
+
audit: (entry, reply) => {
|
|
23
|
+
host.audit?.(parse(entry), reply);
|
|
24
|
+
},
|
|
21
25
|
};
|
|
22
26
|
}
|
|
23
27
|
/** smllm's engine over compiled machines (`smllm compile` output). */
|
|
@@ -27,7 +31,8 @@ export class Engine {
|
|
|
27
31
|
#raw;
|
|
28
32
|
/**
|
|
29
33
|
* `storage`, when given, is loaded now and then receives every saved
|
|
30
|
-
* record and history entry
|
|
34
|
+
* record and history entry, and each audit entry when it wants an audit
|
|
35
|
+
* (its `put`, `history` and `audit` go before `host`'s).
|
|
31
36
|
*/
|
|
32
37
|
constructor(compiled, host = {}, storage) {
|
|
33
38
|
const json = typeof compiled === "string" ? compiled : JSON.stringify(compiled);
|
|
@@ -43,15 +48,25 @@ export class Engine {
|
|
|
43
48
|
storage.history(machine, id, entry);
|
|
44
49
|
host.history?.(machine, id, entry);
|
|
45
50
|
},
|
|
51
|
+
wantsAudit: () => Boolean(storage.wantsAudit?.() || host.wantsAudit?.()),
|
|
52
|
+
audit: (entry, reply) => {
|
|
53
|
+
if (storage.wantsAudit?.()) {
|
|
54
|
+
storage.audit?.(entry, reply);
|
|
55
|
+
}
|
|
56
|
+
if (host.wantsAudit?.()) {
|
|
57
|
+
host.audit?.(entry, reply);
|
|
58
|
+
}
|
|
59
|
+
},
|
|
46
60
|
}
|
|
47
61
|
: host;
|
|
48
62
|
this.#raw = new Raw(json, adapt(all));
|
|
63
|
+
// sokf-implements: WASM-2_AC-2
|
|
49
64
|
const saved = storage?.load();
|
|
50
65
|
if (saved) {
|
|
51
66
|
this.importState(saved);
|
|
52
67
|
}
|
|
53
68
|
}
|
|
54
|
-
/** The agent-facing tool, to hand to an LLM's tool list (
|
|
69
|
+
/** The agent-facing tool, to hand to an LLM's tool list (WASM-1). */
|
|
55
70
|
tool() {
|
|
56
71
|
return parse(this.#raw.tool());
|
|
57
72
|
}
|
|
@@ -67,7 +82,7 @@ export class Engine {
|
|
|
67
82
|
bind(harness, hostSession, cwd = "") {
|
|
68
83
|
return parse(this.#raw.bind(harness, hostSession, cwd));
|
|
69
84
|
}
|
|
70
|
-
/** Where the session is; changes nothing. */
|
|
85
|
+
/** Where the session is; changes nothing and records no audit entry. A look of the agent's is `callTool` with no event. */
|
|
71
86
|
view(key) {
|
|
72
87
|
return parse(this.#raw.view(key));
|
|
73
88
|
}
|
|
@@ -78,16 +93,52 @@ export class Engine {
|
|
|
78
93
|
status(key) {
|
|
79
94
|
return parse(this.#raw.status(key));
|
|
80
95
|
}
|
|
81
|
-
/**
|
|
96
|
+
/**
|
|
97
|
+
* Fire an event for the agent; with no key, only `enter` (it starts a
|
|
98
|
+
* session). An event that the machine marks with `source: host` is
|
|
99
|
+
* refused: fire it with `fireAsHost`. Fire an event that the agent chose
|
|
100
|
+
* here, never with `fireAsHost`. A param takes a string, or any JSON
|
|
101
|
+
* value when the events list shows it as a JSON value.
|
|
102
|
+
*/
|
|
82
103
|
fire(key, event, params = {}) {
|
|
83
104
|
return parse(this.#raw.fire(key, event, JSON.stringify(params)));
|
|
84
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Fire an event as the host: as `fire`, and also an event that the machine
|
|
108
|
+
* marks with `source: host`, such as the result of the host's own checks.
|
|
109
|
+
*/
|
|
110
|
+
fireAsHost(key, event, params = {}) {
|
|
111
|
+
return parse(this.#raw.fireAsHost(key, event, JSON.stringify(params)));
|
|
112
|
+
}
|
|
85
113
|
stop(key, stopHookActive = false) {
|
|
86
114
|
return parse(this.#raw.stop(key, stopHookActive));
|
|
87
115
|
}
|
|
88
116
|
promptSubmitted(key) {
|
|
89
117
|
this.#raw.promptSubmitted(key);
|
|
90
118
|
}
|
|
119
|
+
/**
|
|
120
|
+
* The state's text for a model without tools: its instructions, the
|
|
121
|
+
* events that the model can answer with, and how to answer. Each JSON
|
|
122
|
+
* param shows its schema unless `schemas` is `false`.
|
|
123
|
+
*/
|
|
124
|
+
stateText(key, options = {}) {
|
|
125
|
+
return this.#raw.stateText(key, options.schemas ?? true);
|
|
126
|
+
}
|
|
127
|
+
/** The JSON Schema of a valid answer: a `oneOf` with one branch for each event of the model. */
|
|
128
|
+
answerSchema(key) {
|
|
129
|
+
return parse(this.#raw.answerSchema(key));
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Check the model's text answer: the event and its params, ready for
|
|
133
|
+
* `fire`, or a refusal to send back to the model. It fires nothing.
|
|
134
|
+
*/
|
|
135
|
+
checkAnswer(key, text) {
|
|
136
|
+
return parse(this.#raw.checkAnswer(key, text));
|
|
137
|
+
}
|
|
138
|
+
/** The state's offered events in two lists: the model's, and the host's own. */
|
|
139
|
+
hostView(key) {
|
|
140
|
+
return parse(this.#raw.hostView(key));
|
|
141
|
+
}
|
|
91
142
|
exportState() {
|
|
92
143
|
return parse(this.#raw.exportState());
|
|
93
144
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@six5536/smllm-wasm",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "smllm's engine as WebAssembly: run smllm state machines that drive an LLM agent, in a browser or Node.",
|
|
5
5
|
"homepage": "https://github.com/six5536/smllm#readme",
|
|
6
6
|
"repository": {
|
|
@@ -7,17 +7,27 @@
|
|
|
7
7
|
export class Engine {
|
|
8
8
|
free(): void;
|
|
9
9
|
[Symbol.dispose](): void;
|
|
10
|
+
/**
|
|
11
|
+
* The JSON Schema of a valid answer, as JSON (ENG-8).
|
|
12
|
+
*/
|
|
13
|
+
answerSchema(key: string): string;
|
|
10
14
|
/**
|
|
11
15
|
* Bind a session; returns the reply JSON (its `session` is the key).
|
|
12
16
|
*/
|
|
13
17
|
bind(harness: string, host_session: string | null | undefined, cwd: string): string;
|
|
14
18
|
/**
|
|
15
19
|
* A tool call: the agent's arguments as JSON, answered as the MCP
|
|
16
|
-
* server answers them (
|
|
20
|
+
* server answers them (WASM-1). A malformed call or an engine error is
|
|
17
21
|
* a reply with `ok: false` and `error: …`, never a throw: the agent
|
|
18
22
|
* sees its mistake as the tool's answer.
|
|
19
23
|
*/
|
|
20
24
|
callTool(args: string): string;
|
|
25
|
+
/**
|
|
26
|
+
* A model's answer checked (ENG-9), as JSON: `{"ok": true, "event",
|
|
27
|
+
* "params"}`, whose params are JSON values ready for `fire`, or
|
|
28
|
+
* `{"ok": false, "error"}`.
|
|
29
|
+
*/
|
|
30
|
+
checkAnswer(key: string, text: string): string;
|
|
21
31
|
/**
|
|
22
32
|
* The events list.
|
|
23
33
|
*/
|
|
@@ -27,9 +37,19 @@ export class Engine {
|
|
|
27
37
|
*/
|
|
28
38
|
exportState(): string;
|
|
29
39
|
/**
|
|
30
|
-
* Fire an event
|
|
40
|
+
* Fire an event as the host, host-only events included (ENG-6).
|
|
41
|
+
*/
|
|
42
|
+
fireAsHost(key: string | null | undefined, event: string, params: string): string;
|
|
43
|
+
/**
|
|
44
|
+
* Fire an event for the agent; `params` is a JSON object, of strings
|
|
45
|
+
* except for a JSON param's value.
|
|
31
46
|
*/
|
|
32
47
|
fire(key: string | null | undefined, event: string, params: string): string;
|
|
48
|
+
/**
|
|
49
|
+
* The state's offered events in two lists, the model's and the
|
|
50
|
+
* host's, with where the session is, as JSON (ENG-10).
|
|
51
|
+
*/
|
|
52
|
+
hostView(key: string): string;
|
|
33
53
|
/**
|
|
34
54
|
* Replace the store from [`Engine::export_state`] JSON.
|
|
35
55
|
*/
|
|
@@ -43,11 +63,16 @@ export class Engine {
|
|
|
43
63
|
*/
|
|
44
64
|
promptSubmitted(key: string): void;
|
|
45
65
|
/**
|
|
46
|
-
*
|
|
66
|
+
* The state's text for a model without tools; `schemas` shows each
|
|
67
|
+
* JSON param's schema (ENG-7).
|
|
68
|
+
*/
|
|
69
|
+
stateText(key: string, schemas: boolean): string;
|
|
70
|
+
/**
|
|
71
|
+
* Where the session is, as the `smllm statusline --json` object (ENG-3).
|
|
47
72
|
*/
|
|
48
73
|
status(key: string): string;
|
|
49
74
|
/**
|
|
50
|
-
* The stop decision as JSON (
|
|
75
|
+
* The stop decision as JSON (AGT-8..6): `{"decision": "allow"}` = may
|
|
51
76
|
* stop; `{"decision": "block", "text"}` = hold the agent with the events
|
|
52
77
|
* list; `{"decision": "runaway", "text"}` = let it stop, showing the
|
|
53
78
|
* list to the user (the harness was already continuing because of a
|
|
@@ -56,7 +81,7 @@ export class Engine {
|
|
|
56
81
|
stop(key: string, stop_hook_active: boolean): string;
|
|
57
82
|
/**
|
|
58
83
|
* The agent-facing tool, as JSON `{name, description, inputSchema}`:
|
|
59
|
-
* the core's definition, as the MCP server offers it (
|
|
84
|
+
* the core's definition, as the MCP server offers it (WASM-1).
|
|
60
85
|
*/
|
|
61
86
|
tool(): string;
|
|
62
87
|
/**
|