@staal/node 0.24.1 → 0.26.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 +43 -0
- package/bin/darwin-arm64/staal.node +0 -0
- package/bin/darwin-x64/staal.node +0 -0
- package/bin/linux-arm64-gnu/staal.node +0 -0
- package/bin/linux-x64-gnu/staal.node +0 -0
- package/claude-agent-sdk.d.mts +5 -0
- package/claude-agent-sdk.mjs +77 -0
- package/index.d.ts +106 -0
- package/index.js +171 -1
- package/langgraph.d.ts +3 -0
- package/langgraph.js +31 -0
- package/openai-agents.d.ts +3 -0
- package/openai-agents.js +35 -0
- package/package.json +58 -3
- package/vercel-ai.d.mts +5 -0
- package/vercel-ai.mjs +58 -0
package/README.md
CHANGED
|
@@ -31,6 +31,49 @@ in `index.d.ts`: `done` with a `result`, `failed` with a `failure`, `stopped` (a
|
|
|
31
31
|
action in doubt: `resolve` it, then `resume`), `invalid` with located check errors,
|
|
32
32
|
or `error`. Pass `{ engine: 'native' }` to run pure code as machine code.
|
|
33
33
|
|
|
34
|
+
**As a tool.** `staal.Tool.open` makes a program a tool an agent can call many times,
|
|
35
|
+
with quotas across calls, `once` (the same operation called again is the same run),
|
|
36
|
+
and recovery of a run a crash interrupted when the journal directory is next opened.
|
|
37
|
+
Grants are fixed, or computed by your app for each call from a context it passes:
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
const refund = await staal.Tool.open('refund.staal', {
|
|
41
|
+
name: 'refund_order',
|
|
42
|
+
description: 'Refund part of an order.',
|
|
43
|
+
grants: (customer) => ({ pay: { cap: 'payments.refund', uses: 1, each: { amount: LIMITS[customer.tier], currency: 'USD' } } }),
|
|
44
|
+
journals: 'runs',
|
|
45
|
+
quota: { window: '1h', money: { amount: '1000.00', currency: 'USD' } },
|
|
46
|
+
});
|
|
47
|
+
const r = await refund.call({ order: '4417', amount: { amount: '35.00', currency: 'USD' } }, { context: customer });
|
|
48
|
+
// r.status: 'done', 'failed', 'stopped', 'in_progress' or 'not_run'; r.text, r.value, r.run
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
A grant is never an argument, and the model never sees one. An app serving several
|
|
52
|
+
tenants or users from one directory also gives `scope: (customer) => customer.tenantId`,
|
|
53
|
+
from its own records: `once` keys are then held per scope, and a run's status is given
|
|
54
|
+
only in its scope (`refund.status(run, { context: customer })`). Inputs that only the
|
|
55
|
+
app may give, such as the customer a call is for, are `appInputs: { customer: (customer)
|
|
56
|
+
=> customer.id }`: they are left out of the schema a model sees, and a call whose
|
|
57
|
+
arguments name one is refused. One process uses a
|
|
58
|
+
journal directory at a time, so give each worker its own directory (whose quotas are
|
|
59
|
+
then its own), or send the calls through one process.
|
|
60
|
+
|
|
61
|
+
**Agent frameworks.** `functionTool` from `@staal/node/openai-agents` gives an OpenAI
|
|
62
|
+
Agents SDK tool (grants from the run's `context`), and `structuredTool` from
|
|
63
|
+
`@staal/node/langgraph` a LangChain tool for LangGraph (context from
|
|
64
|
+
`config.configurable.staal_context`). `aiTools` from `@staal/node/vercel-ai` gives
|
|
65
|
+
Vercel AI SDK tools, an ES module for `ai` 7 (Node 22 or newer), whose context is each
|
|
66
|
+
tool's entry in `toolsContext`; a refusal reaches the model as an error with Staal's
|
|
67
|
+
message. `mcpServer` from `@staal/node/claude-agent-sdk` serves tools to the Claude Agent SDK as
|
|
68
|
+
an in-process MCP server, with the session's context bound when the server is made
|
|
69
|
+
(one per query), and `allowedTools` gives their names for `allowedTools`. The
|
|
70
|
+
frameworks are optional peer dependencies. Each is tested through the framework's own
|
|
71
|
+
agent loop (@openai/agents 0.20.0, @langchain/langgraph 1.5.1, ai 7.0.145,
|
|
72
|
+
@anthropic-ai/claude-agent-sdk 0.3.304, whose loop runs in the CLI it ships, against a
|
|
73
|
+
local stand-in for the Messages API), and once a week against each framework's latest
|
|
74
|
+
release; the examples are in
|
|
75
|
+
[examples/frameworks](https://github.com/laurells/staal/tree/main/examples/frameworks).
|
|
76
|
+
|
|
34
77
|
**Platforms.** Node 18 or newer, on Linux (x86_64 and arm64, glibc 2.28 or newer) and
|
|
35
78
|
macOS (Apple Silicon, macOS 11 or newer; Intel, macOS 10.12 or newer). Elsewhere,
|
|
36
79
|
build `node/` from the repository.
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Tool } from './index.js';
|
|
2
|
+
/** staal Tools as an in-process MCP server config for the Claude Agent SDK's `mcpServers`. */
|
|
3
|
+
export function mcpServer(tools: Tool<any>[], options?: { name?: string; context?: unknown }): { type: 'sdk'; name: string; instance: any };
|
|
4
|
+
/** The names the Agent SDK gives `tools` served under `name`, for `allowedTools`. */
|
|
5
|
+
export function allowedTools(name: string, tools: Tool<any>[]): string[];
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// staal Tools as an in-process MCP server for the Claude Agent SDK
|
|
2
|
+
// (@anthropic-ai/claude-agent-sdk, with @modelcontextprotocol/sdk 1.x, which the app
|
|
3
|
+
// installs; @staal/node depends on neither). The Agent SDK is published as ES modules
|
|
4
|
+
// only, so this adapter is one too.
|
|
5
|
+
//
|
|
6
|
+
// import { query } from '@anthropic-ai/claude-agent-sdk';
|
|
7
|
+
// import { allowedTools, mcpServer } from '@staal/node/claude-agent-sdk';
|
|
8
|
+
// for await (const message of query({ prompt, options: {
|
|
9
|
+
// mcpServers: { staal: mcpServer([refund], { context: customer }) }, // one per query
|
|
10
|
+
// allowedTools: allowedTools('staal', [refund]),
|
|
11
|
+
// tools: [],
|
|
12
|
+
// } })) { ... }
|
|
13
|
+
//
|
|
14
|
+
// The Agent SDK gives a tool only its arguments, so the app's context for the session
|
|
15
|
+
// (the signed-in customer, the tenant) is bound when the server is made: make one per
|
|
16
|
+
// query, from the app's own records, never from anything the model wrote. Each call
|
|
17
|
+
// runs the Staal program with the grants the app's policy gives for that context.
|
|
18
|
+
//
|
|
19
|
+
// The server is a plain MCP server (the MCP SDK's low-level Server), so the program's
|
|
20
|
+
// argument schema reaches the model as JSON Schema, unconverted, and Staal's own check
|
|
21
|
+
// of the arguments is the one that decides. A refusal, a failure or an unknown tool is
|
|
22
|
+
// a tool result with isError, with Staal's message, so the model can correct itself;
|
|
23
|
+
// nothing is retried. The model sees each tool as `mcp__<name>__<tool name>`.
|
|
24
|
+
|
|
25
|
+
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
26
|
+
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
27
|
+
|
|
28
|
+
const HINTS = ['title', 'readOnlyHint', 'destructiveHint', 'idempotentHint', 'openWorldHint'];
|
|
29
|
+
|
|
30
|
+
function checked(tools) {
|
|
31
|
+
if (!Array.isArray(tools) || tools.length === 0) throw new TypeError('staal: mcpServer takes a list of Tools from Tool.open');
|
|
32
|
+
const byName = new Map();
|
|
33
|
+
for (const t of tools) {
|
|
34
|
+
if (!t || typeof t.call !== 'function' || typeof t.name !== 'string') throw new TypeError('staal: mcpServer takes a list of Tools from Tool.open');
|
|
35
|
+
if (byName.has(t.name)) throw new TypeError(`staal: mcpServer: two tools are named '${t.name}'`);
|
|
36
|
+
byName.set(t.name, t);
|
|
37
|
+
}
|
|
38
|
+
return byName;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* `tools` as an in-process MCP server for the Agent SDK's `mcpServers` option.
|
|
43
|
+
* `options.context` is what each tool's grants (and scope) function receives, for
|
|
44
|
+
* every call made through this server; `options.name` (default "staal") must be the
|
|
45
|
+
* key the server is given in `mcpServers`.
|
|
46
|
+
*/
|
|
47
|
+
export function mcpServer(tools, options = {}) {
|
|
48
|
+
const byName = checked(tools);
|
|
49
|
+
const name = options.name ?? 'staal';
|
|
50
|
+
if (typeof name !== 'string' || !/^[A-Za-z0-9_-]+$/.test(name)) throw new TypeError('staal: mcpServer: name is letters, digits, _ and -');
|
|
51
|
+
const context = options.context;
|
|
52
|
+
const server = new Server({ name, version: '1.0.0' }, { capabilities: { tools: {} } });
|
|
53
|
+
const listed = [...byName.values()].map((t) => {
|
|
54
|
+
const annotations = {};
|
|
55
|
+
for (const k of HINTS) if (t.annotations && k in t.annotations) annotations[k] = t.annotations[k];
|
|
56
|
+
return { name: t.name, description: t.description, inputSchema: t.schema, annotations };
|
|
57
|
+
});
|
|
58
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: listed }));
|
|
59
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
60
|
+
const t = byName.get(request.params.name);
|
|
61
|
+
if (!t) return { content: [{ type: 'text', text: `there is no tool '${request.params.name}'` }], isError: true };
|
|
62
|
+
try {
|
|
63
|
+
const result = await t.call(request.params.arguments ?? {}, { context });
|
|
64
|
+
return { content: [{ type: 'text', text: result.text }], isError: result.isError };
|
|
65
|
+
} catch (e) {
|
|
66
|
+
// The call did not reach an answer (the host is closed, the grants function
|
|
67
|
+
// threw): the model is told so, as an MCP tool error, not a protocol error.
|
|
68
|
+
return { content: [{ type: 'text', text: `the tool could not be called: ${e && e.message ? e.message : String(e)}` }], isError: true };
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
return { type: 'sdk', name, instance: server };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The names the Agent SDK gives `tools` served under `name`, for `allowedTools`. */
|
|
75
|
+
export function allowedTools(name, tools) {
|
|
76
|
+
return [...checked(tools).keys()].map((n) => `mcp__${name}__${n}`);
|
|
77
|
+
}
|
package/index.d.ts
CHANGED
|
@@ -30,6 +30,7 @@ export type FailureKind =
|
|
|
30
30
|
| 'provider'
|
|
31
31
|
| 'step_limit'
|
|
32
32
|
| 'depth_limit'
|
|
33
|
+
| 'deadline'
|
|
33
34
|
| 'internal'
|
|
34
35
|
| 'journal'
|
|
35
36
|
| 'in_doubt';
|
|
@@ -39,8 +40,17 @@ export interface Failure {
|
|
|
39
40
|
message: string;
|
|
40
41
|
}
|
|
41
42
|
|
|
43
|
+
/** What one granted capability used over a run (RUNTIME.md section 6). */
|
|
44
|
+
export interface GrantUsage {
|
|
45
|
+
capability: string;
|
|
46
|
+
uses: number;
|
|
47
|
+
charged: { amount: string; currency: string }[];
|
|
48
|
+
}
|
|
49
|
+
|
|
42
50
|
interface Ran {
|
|
43
51
|
log: LogEvent[];
|
|
52
|
+
/** For each capability parameter of the entry function, by name. */
|
|
53
|
+
usage: { [parameter: string]: GrantUsage };
|
|
44
54
|
/** Present when the run loaded C code, which Staal cannot check (RUNTIME.md section 19). */
|
|
45
55
|
unchecked?: Json[];
|
|
46
56
|
}
|
|
@@ -75,3 +85,99 @@ export function check(program: string): Promise<CheckResult>;
|
|
|
75
85
|
export function run(program: string, config: RunConfig, options?: RunOptions): Promise<RunResult>;
|
|
76
86
|
export function resume(program: string, config: RunConfig, journal: string, options?: Pick<RunOptions, 'engine'>): Promise<RunResult>;
|
|
77
87
|
export function resolve(journal: string, resolution: Resolution): Promise<ResolveResult>;
|
|
88
|
+
|
|
89
|
+
/** A host or tool could not be opened, or a call was malformed: nothing ran. */
|
|
90
|
+
export class StaalError extends Error {}
|
|
91
|
+
|
|
92
|
+
export interface HostOptions {
|
|
93
|
+
/** Days a settled run's files are kept (default 90), or "forever". */
|
|
94
|
+
keep?: number | 'forever';
|
|
95
|
+
max_concurrent_runs?: number;
|
|
96
|
+
notify?: string[];
|
|
97
|
+
notify_env?: string[];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Where tools keep their runs: one journal directory, its ledger and its quotas. */
|
|
101
|
+
export class Host {
|
|
102
|
+
static open(journals: string, options?: HostOptions): Promise<Host>;
|
|
103
|
+
readonly journals: string;
|
|
104
|
+
/** What happened to a run, by its id. Changes nothing. */
|
|
105
|
+
/** A run made in a scope is described only when the same scope is given. */
|
|
106
|
+
status(run: string, options?: { scope?: string }): Promise<RunStatus>;
|
|
107
|
+
close(): Promise<void>;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export type RunStatus = {
|
|
111
|
+
run: string;
|
|
112
|
+
status: 'running' | 'waiting_for_operator' | 'done' | 'failed' | 'not_started' | 'expired' | 'unknown';
|
|
113
|
+
observed_at: string;
|
|
114
|
+
result?: Json;
|
|
115
|
+
failure?: { kind: string; message: string };
|
|
116
|
+
reason?: string;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
export interface ToolOptions<Context = unknown> {
|
|
120
|
+
name: string;
|
|
121
|
+
description: string;
|
|
122
|
+
/** The same grants for every call, or a function of the call's context giving that call's. */
|
|
123
|
+
grants?: { [name: string]: Json } | ((context: Context) => { [name: string]: Json } | Promise<{ [name: string]: Json }>);
|
|
124
|
+
/**
|
|
125
|
+
* The call's trusted scope (a tenant's or a user's id, from the app's own records,
|
|
126
|
+
* never from the arguments), from its context: `once` keys are held within a scope,
|
|
127
|
+
* and a run's status is given only in its scope.
|
|
128
|
+
*/
|
|
129
|
+
scope?: (context: Context) => string | Promise<string>;
|
|
130
|
+
/**
|
|
131
|
+
* Inputs only the app supplies, such as the customer a call is for: each name, an
|
|
132
|
+
* input of the entry function, maps to a function of the call's context giving its
|
|
133
|
+
* value. They are not in the schema a model sees, and a call whose arguments name
|
|
134
|
+
* one is refused.
|
|
135
|
+
*/
|
|
136
|
+
appInputs?: { [name: string]: (context: Context) => Json | Promise<Json> };
|
|
137
|
+
/** A journal directory, or an open host: one is required. */
|
|
138
|
+
journals?: string;
|
|
139
|
+
host?: Host;
|
|
140
|
+
title?: string;
|
|
141
|
+
entry?: string;
|
|
142
|
+
quota?: { window: string; calls?: number; money?: { amount: string; currency: string } };
|
|
143
|
+
once?: { by: string[]; keep?: string };
|
|
144
|
+
deadline?: string;
|
|
145
|
+
max_concurrent_runs?: number;
|
|
146
|
+
engine?: 'interpreter' | 'native';
|
|
147
|
+
model?: Json;
|
|
148
|
+
secrets?: Json;
|
|
149
|
+
net?: Json;
|
|
150
|
+
fs?: Json;
|
|
151
|
+
adapters?: Json;
|
|
152
|
+
limits?: Json;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export interface ToolResult {
|
|
156
|
+
status: 'done' | 'failed' | 'stopped' | 'in_progress' | 'not_run';
|
|
157
|
+
isError: boolean;
|
|
158
|
+
/** What to show a model. */
|
|
159
|
+
text: string;
|
|
160
|
+
structured?: { result: Json } | { status: 'waiting_for_operator'; run: string };
|
|
161
|
+
run?: string;
|
|
162
|
+
/** The program's result, when done. */
|
|
163
|
+
value?: Json;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** A program's entry function as a tool, with quotas, once and recovery across calls. */
|
|
167
|
+
export class Tool<Context = unknown> {
|
|
168
|
+
static open<Context = unknown>(program: string, options: ToolOptions<Context>): Promise<Tool<Context>>;
|
|
169
|
+
readonly name: string;
|
|
170
|
+
readonly title?: string;
|
|
171
|
+
readonly description: string;
|
|
172
|
+
/** The JSON Schema of its arguments. */
|
|
173
|
+
readonly schema: Json;
|
|
174
|
+
readonly outputSchema: Json;
|
|
175
|
+
readonly annotations: { readOnlyHint: boolean; destructiveHint: boolean; idempotentHint: boolean; openWorldHint: boolean; title?: string };
|
|
176
|
+
readonly warnings: string[];
|
|
177
|
+
readonly perCallGrants: boolean;
|
|
178
|
+
readonly host: Host;
|
|
179
|
+
call(args?: { [name: string]: Json }, options?: { context?: Context }): Promise<ToolResult>;
|
|
180
|
+
/** A run's status, in the scope the tool's scope function gives for the context. */
|
|
181
|
+
status(run: string, options?: { context?: Context }): Promise<RunStatus>;
|
|
182
|
+
close(): Promise<void>;
|
|
183
|
+
}
|
package/index.js
CHANGED
|
@@ -77,4 +77,174 @@ async function resolve(journal, resolution) {
|
|
|
77
77
|
return parse(await addon.resolve(str(journal, 'journal'), JSON.stringify(resolution)));
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
|
|
80
|
+
class StaalError extends Error {}
|
|
81
|
+
|
|
82
|
+
function ok(out) {
|
|
83
|
+
if (out.status === 'error') throw new StaalError(out.error);
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Where tools keep their runs: one journal directory, its ledger and its quotas.
|
|
89
|
+
* Opening it resumes or settles whatever an earlier process left there. One process
|
|
90
|
+
* uses a directory at a time; opening it again in this process gives the same host.
|
|
91
|
+
*/
|
|
92
|
+
class Host {
|
|
93
|
+
constructor(handle, journals) {
|
|
94
|
+
this._handle = handle;
|
|
95
|
+
this.journals = journals;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
static async open(journals, options = {}) {
|
|
99
|
+
const settings = { journals: str(journals, 'journals'), keep: 90, max_concurrent_runs: 8, ...options };
|
|
100
|
+
const out = ok(parse(await addon.hostOpen(JSON.stringify(settings))));
|
|
101
|
+
return new Host(out.host, journals);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* What happened to a run, by its id. Changes nothing. A run made in a scope is
|
|
106
|
+
* described only when `options.scope` is that scope; otherwise it is unknown.
|
|
107
|
+
*/
|
|
108
|
+
async status(run, options = {}) {
|
|
109
|
+
return ok(parse(await addon.runStatus(this._handle, str(run, 'run'), scopeOf(options.scope))));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async close() {
|
|
113
|
+
if (this._handle !== null) {
|
|
114
|
+
const h = this._handle;
|
|
115
|
+
this._handle = null;
|
|
116
|
+
ok(parse(await addon.hostClose(h)));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function scopeOf(scope) {
|
|
122
|
+
if (scope === undefined || scope === null) return undefined;
|
|
123
|
+
if (typeof scope !== 'string') throw new TypeError("staal: a scope must be a string (a tenant's or a user's id, from the app's own records)");
|
|
124
|
+
return scope;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const SPEC_KEYS = ['title', 'entry', 'quota', 'once', 'deadline', 'max_concurrent_runs', 'engine',
|
|
128
|
+
'model', 'secrets', 'net', 'fs', 'adapters', 'limits'];
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* A program's entry function as a tool. `grants` is an object, the same for every
|
|
132
|
+
* call, or a function of the call's context that returns that call's grants, so the
|
|
133
|
+
* app's policy decides each call's limits. `scope`, if given, is a function of the
|
|
134
|
+
* same context returning the call's trusted scope as a string (a tenant's or a user's
|
|
135
|
+
* id, from the app's own records, never from the arguments): a `once` key is held
|
|
136
|
+
* within its scope, and a run's status is given only in its scope. An app serving more
|
|
137
|
+
* than one tenant or user from one journal directory gives a scope. `appInputs`, if
|
|
138
|
+
* given, maps names of the entry function's inputs to functions of the same context
|
|
139
|
+
* that give their values: inputs only the app supplies, such as the customer a call
|
|
140
|
+
* is for. They are not in the schema a model sees, and a call whose arguments name
|
|
141
|
+
* one is refused. Give `journals` (a directory) or `host`.
|
|
142
|
+
*/
|
|
143
|
+
class Tool {
|
|
144
|
+
static async open(program, options) {
|
|
145
|
+
const { name, description, grants, scope, appInputs, journals, host, ...spec } = options || {};
|
|
146
|
+
if (scope !== undefined && typeof scope !== 'function') throw new TypeError("staal: Tool: scope is a function of the call's context");
|
|
147
|
+
if (appInputs !== undefined) {
|
|
148
|
+
const entries = appInputs !== null && typeof appInputs === 'object' && !Array.isArray(appInputs) ? Object.entries(appInputs) : [];
|
|
149
|
+
if (entries.length === 0 || !entries.every(([, f]) => typeof f === 'function')) {
|
|
150
|
+
throw new TypeError("staal: Tool: appInputs maps input names to functions of the call's context");
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
for (const k of Object.keys(spec)) {
|
|
154
|
+
if (!SPEC_KEYS.includes(k)) throw new TypeError(`staal: Tool: unknown option '${k}'`);
|
|
155
|
+
}
|
|
156
|
+
if ((journals === undefined) === (host === undefined)) {
|
|
157
|
+
throw new TypeError('staal: Tool: give journals (a directory) or host, not both and not neither');
|
|
158
|
+
}
|
|
159
|
+
const ownHost = host === undefined;
|
|
160
|
+
const h = ownHost ? await Host.open(journals) : host;
|
|
161
|
+
const body = { name, description, program: str(program, 'program') };
|
|
162
|
+
if (grants !== undefined && typeof grants !== 'function') body.grants = grants;
|
|
163
|
+
if (appInputs !== undefined) body.app_inputs = Object.keys(appInputs);
|
|
164
|
+
for (const [k, v] of Object.entries(spec)) if (v !== undefined && v !== null) body[k] = v;
|
|
165
|
+
let out;
|
|
166
|
+
try {
|
|
167
|
+
out = ok(parse(await addon.toolOpen(h._handle, JSON.stringify(body))));
|
|
168
|
+
} catch (e) {
|
|
169
|
+
if (ownHost) await h.close();
|
|
170
|
+
throw e;
|
|
171
|
+
}
|
|
172
|
+
const t = new Tool(out, h, ownHost, typeof grants === 'function' ? grants : null, scope || null, appInputs ? { ...appInputs } : null);
|
|
173
|
+
if (t.perCallGrants && t._grants === null) {
|
|
174
|
+
await t.close();
|
|
175
|
+
throw new TypeError('staal: Tool: the program needs grants; give grants (an object or a function of the context)');
|
|
176
|
+
}
|
|
177
|
+
if (!t.perCallGrants && t._grants !== null) {
|
|
178
|
+
await t.close();
|
|
179
|
+
throw new TypeError("staal: Tool: the program's entry function takes no capabilities, so it takes no grants");
|
|
180
|
+
}
|
|
181
|
+
return t;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
constructor(out, host, ownHost, grants, scope, appInputs) {
|
|
185
|
+
this._handle = out.tool;
|
|
186
|
+
this._scope = scope;
|
|
187
|
+
this._appInputs = appInputs;
|
|
188
|
+
this.host = host;
|
|
189
|
+
this._ownHost = ownHost;
|
|
190
|
+
this._grants = grants;
|
|
191
|
+
this.name = out.name;
|
|
192
|
+
this.title = out.title;
|
|
193
|
+
this.description = out.description;
|
|
194
|
+
this.schema = out.input_schema;
|
|
195
|
+
this.outputSchema = out.output_schema;
|
|
196
|
+
this.annotations = out.annotations;
|
|
197
|
+
this.warnings = out.warnings;
|
|
198
|
+
this.perCallGrants = out.grants === 'per_call';
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* One call. Resolves to { status, isError, text, structured, run, value }: status is
|
|
203
|
+
* "done", "failed", "stopped" (waiting for an operator), "in_progress" or "not_run";
|
|
204
|
+
* a refusal is a result too, so a model can be told why.
|
|
205
|
+
*/
|
|
206
|
+
async call(args = {}, options = {}) {
|
|
207
|
+
if (args === null || typeof args !== 'object' || Array.isArray(args)) {
|
|
208
|
+
throw new TypeError(`staal: the arguments are an object (a JSON object), not ${args === null ? 'null' : Array.isArray(args) ? 'an array' : typeof args}`);
|
|
209
|
+
}
|
|
210
|
+
let grants;
|
|
211
|
+
if (this.perCallGrants) {
|
|
212
|
+
const g = await this._grants(options.context);
|
|
213
|
+
if (g === null || typeof g !== 'object' || Array.isArray(g)) throw new TypeError('staal: the grants function must return an object');
|
|
214
|
+
grants = JSON.stringify(g);
|
|
215
|
+
}
|
|
216
|
+
const scope = this._scope ? scopeOf(await this._scope(options.context)) : undefined;
|
|
217
|
+
let inputs;
|
|
218
|
+
if (this._appInputs) {
|
|
219
|
+
const values = {};
|
|
220
|
+
for (const [k, f] of Object.entries(this._appInputs)) values[k] = await f(options.context);
|
|
221
|
+
inputs = JSON.stringify(values);
|
|
222
|
+
}
|
|
223
|
+
const out = ok(parse(await addon.toolCall(this._handle, JSON.stringify(args), inputs, grants, scope)));
|
|
224
|
+
return {
|
|
225
|
+
status: out.status,
|
|
226
|
+
isError: out.is_error,
|
|
227
|
+
text: out.text,
|
|
228
|
+
structured: out.structured,
|
|
229
|
+
run: out.run,
|
|
230
|
+
value: out.status === 'done' && out.structured ? out.structured.result : undefined,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** A run's status, in the scope the tool's scope function gives for `options.context`. */
|
|
235
|
+
async status(run, options = {}) {
|
|
236
|
+
const scope = this._scope ? scopeOf(await this._scope(options.context)) : undefined;
|
|
237
|
+
return this.host.status(run, { scope });
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async close() {
|
|
241
|
+
if (this._handle !== null) {
|
|
242
|
+
const t = this._handle;
|
|
243
|
+
this._handle = null;
|
|
244
|
+
ok(parse(await addon.toolClose(t)));
|
|
245
|
+
}
|
|
246
|
+
if (this._ownHost) await this.host.close();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
module.exports = { version, check, run, resume, resolve, Host, Tool, StaalError };
|
package/langgraph.d.ts
ADDED
package/langgraph.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// A staal Tool as a LangChain tool, for LangGraph's ToolNode and agents (@langchain/core
|
|
3
|
+
// and @langchain/langgraph, which the app installs; @staal/node does not depend on them).
|
|
4
|
+
//
|
|
5
|
+
// const { ToolNode } = require('@langchain/langgraph/prebuilt');
|
|
6
|
+
// const { structuredTool } = require('@staal/node/langgraph');
|
|
7
|
+
// const tools = new ToolNode([structuredTool(refund)]);
|
|
8
|
+
// await graph.invoke(state, { configurable: { staal_context: customer } });
|
|
9
|
+
//
|
|
10
|
+
// Each call runs the Staal program with the grants the app's policy gives for the call's
|
|
11
|
+
// context: by default `config.configurable.staal_context`, or whatever `context` returns
|
|
12
|
+
// for the call's config. A refusal or a failure is given to the model as the tool's
|
|
13
|
+
// output, the broker's message included, so the model can correct itself.
|
|
14
|
+
|
|
15
|
+
const { tool } = require('@langchain/core/tools');
|
|
16
|
+
|
|
17
|
+
/** `staalTool` (from Tool.open) as a @langchain/core tool. */
|
|
18
|
+
function structuredTool(staalTool, options = {}) {
|
|
19
|
+
const pick = options.context || ((config) => (config && config.configurable ? config.configurable.staal_context : undefined));
|
|
20
|
+
const schema = { ...staalTool.schema };
|
|
21
|
+
delete schema.$schema;
|
|
22
|
+
return tool(
|
|
23
|
+
async (input, config) => {
|
|
24
|
+
const result = await staalTool.call(input || {}, { context: pick(config) });
|
|
25
|
+
return result.text;
|
|
26
|
+
},
|
|
27
|
+
{ name: staalTool.name, description: staalTool.description, schema },
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = { structuredTool };
|
package/openai-agents.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// A staal Tool as a tool of the OpenAI Agents SDK for JavaScript (@openai/agents, which
|
|
3
|
+
// the app installs; @staal/node does not depend on it).
|
|
4
|
+
//
|
|
5
|
+
// const { Agent } = require('@openai/agents');
|
|
6
|
+
// const { functionTool } = require('@staal/node/openai-agents');
|
|
7
|
+
// const agent = new Agent({ name: 'Support', tools: [functionTool(refund)] });
|
|
8
|
+
//
|
|
9
|
+
// Each call runs the Staal program with the grants the app's policy gives for the run's
|
|
10
|
+
// context (the `context` passed to run(), unless `context` says otherwise). A refusal
|
|
11
|
+
// or a failure is given to the model as the tool's output, the broker's message
|
|
12
|
+
// included, so the model can correct itself.
|
|
13
|
+
|
|
14
|
+
const { tool } = require('@openai/agents');
|
|
15
|
+
|
|
16
|
+
/** `staalTool` (from Tool.open) as an @openai/agents function tool. */
|
|
17
|
+
function functionTool(staalTool, options = {}) {
|
|
18
|
+
const pick = options.context || ((runContext) => (runContext ? runContext.context : undefined));
|
|
19
|
+
const parameters = { ...staalTool.schema };
|
|
20
|
+
delete parameters.$schema;
|
|
21
|
+
return tool({
|
|
22
|
+
name: staalTool.name,
|
|
23
|
+
description: staalTool.description,
|
|
24
|
+
parameters,
|
|
25
|
+
// Staal's own check of the arguments is the one that decides; the schema is given
|
|
26
|
+
// as it is rather than rewritten into the API's strict subset.
|
|
27
|
+
strict: false,
|
|
28
|
+
execute: async (input, runContext) => {
|
|
29
|
+
const result = await staalTool.call(input || {}, { context: pick(runContext) });
|
|
30
|
+
return result.text;
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = { functionTool };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@staal/node",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.26.0",
|
|
4
4
|
"description": "Staal, the programming language for agents, for Node.js: the staalc compiler and runtime, through its C interface",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"staal",
|
|
@@ -27,7 +27,15 @@
|
|
|
27
27
|
"README.md",
|
|
28
28
|
"LICENSE",
|
|
29
29
|
"NOTICE",
|
|
30
|
-
"LICENSE-THIRD-PARTY.txt"
|
|
30
|
+
"LICENSE-THIRD-PARTY.txt",
|
|
31
|
+
"openai-agents.js",
|
|
32
|
+
"openai-agents.d.ts",
|
|
33
|
+
"langgraph.js",
|
|
34
|
+
"langgraph.d.ts",
|
|
35
|
+
"vercel-ai.mjs",
|
|
36
|
+
"vercel-ai.d.mts",
|
|
37
|
+
"claude-agent-sdk.mjs",
|
|
38
|
+
"claude-agent-sdk.d.mts"
|
|
31
39
|
],
|
|
32
40
|
"publishConfig": {
|
|
33
41
|
"access": "public"
|
|
@@ -45,10 +53,57 @@
|
|
|
45
53
|
],
|
|
46
54
|
"scripts": {
|
|
47
55
|
"test": "node --test test/staal.test.mjs",
|
|
48
|
-
"types": "tsc --noEmit -p test/tsconfig.json"
|
|
56
|
+
"types": "tsc --noEmit -p test/tsconfig.json && tsc --noEmit -p test/tsconfig.esm.json"
|
|
49
57
|
},
|
|
50
58
|
"devDependencies": {
|
|
51
59
|
"@types/node": "22.10.2",
|
|
52
60
|
"typescript": "5.7.2"
|
|
61
|
+
},
|
|
62
|
+
"exports": {
|
|
63
|
+
".": {
|
|
64
|
+
"types": "./index.d.ts",
|
|
65
|
+
"default": "./index.js"
|
|
66
|
+
},
|
|
67
|
+
"./openai-agents": {
|
|
68
|
+
"types": "./openai-agents.d.ts",
|
|
69
|
+
"default": "./openai-agents.js"
|
|
70
|
+
},
|
|
71
|
+
"./langgraph": {
|
|
72
|
+
"types": "./langgraph.d.ts",
|
|
73
|
+
"default": "./langgraph.js"
|
|
74
|
+
},
|
|
75
|
+
"./vercel-ai": {
|
|
76
|
+
"types": "./vercel-ai.d.mts",
|
|
77
|
+
"import": "./vercel-ai.mjs"
|
|
78
|
+
},
|
|
79
|
+
"./claude-agent-sdk": {
|
|
80
|
+
"types": "./claude-agent-sdk.d.mts",
|
|
81
|
+
"import": "./claude-agent-sdk.mjs"
|
|
82
|
+
},
|
|
83
|
+
"./package.json": "./package.json"
|
|
84
|
+
},
|
|
85
|
+
"peerDependencies": {
|
|
86
|
+
"@openai/agents": ">=0.20.0",
|
|
87
|
+
"@langchain/core": ">=1.2.18",
|
|
88
|
+
"ai": ">=7.0.145",
|
|
89
|
+
"@anthropic-ai/claude-agent-sdk": ">=0.3.304",
|
|
90
|
+
"@modelcontextprotocol/sdk": ">=1.29.0"
|
|
91
|
+
},
|
|
92
|
+
"peerDependenciesMeta": {
|
|
93
|
+
"@openai/agents": {
|
|
94
|
+
"optional": true
|
|
95
|
+
},
|
|
96
|
+
"@langchain/core": {
|
|
97
|
+
"optional": true
|
|
98
|
+
},
|
|
99
|
+
"ai": {
|
|
100
|
+
"optional": true
|
|
101
|
+
},
|
|
102
|
+
"@anthropic-ai/claude-agent-sdk": {
|
|
103
|
+
"optional": true
|
|
104
|
+
},
|
|
105
|
+
"@modelcontextprotocol/sdk": {
|
|
106
|
+
"optional": true
|
|
107
|
+
}
|
|
53
108
|
}
|
|
54
109
|
}
|
package/vercel-ai.d.mts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Tool } from './index.js';
|
|
2
|
+
/** A staal Tool as a Vercel AI SDK tool; `context` maps the tool's `toolsContext` entry to the staal Tool's context. */
|
|
3
|
+
export function aiTool(staalTool: Tool<any>, options?: { context?: (toolContext: any) => unknown }): any;
|
|
4
|
+
/** Several staal Tools as an AI SDK tool set, keyed by each tool's name. */
|
|
5
|
+
export function aiTools(staalTools: Tool<any>[], options?: { context?: (toolContext: any) => unknown }): Record<string, any>;
|
package/vercel-ai.mjs
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// A staal Tool as a tool of the Vercel AI SDK (`ai`, version 7, which the app installs;
|
|
2
|
+
// @staal/node does not depend on it). The AI SDK is published as ES modules only, so
|
|
3
|
+
// this adapter is one too.
|
|
4
|
+
//
|
|
5
|
+
// import { generateText, stepCountIs } from 'ai';
|
|
6
|
+
// import { aiTools } from '@staal/node/vercel-ai';
|
|
7
|
+
// const result = await generateText({
|
|
8
|
+
// model, prompt, stopWhen: stepCountIs(5),
|
|
9
|
+
// tools: aiTools([refund]),
|
|
10
|
+
// toolsContext: { refund_order: customer }, // the app's context for that tool
|
|
11
|
+
// });
|
|
12
|
+
//
|
|
13
|
+
// Each call runs the Staal program with the grants the app's policy gives for the
|
|
14
|
+
// tool's context: the AI SDK hands each tool its own entry of `toolsContext`, keyed by
|
|
15
|
+
// the tool's name, and that entry is what the Staal tool's grants (and scope) function
|
|
16
|
+
// receives, unless `options.context` maps it to something else. Context never reaches
|
|
17
|
+
// the model.
|
|
18
|
+
//
|
|
19
|
+
// The model is shown the program's argument schema as it is, without validation by
|
|
20
|
+
// the AI SDK: Staal's own check of the arguments is the one that decides, and a call it
|
|
21
|
+
// refuses comes back to the model as an error with Staal's message, as a refusal by the
|
|
22
|
+
// broker does, so the model can correct itself. Nothing is retried. An abort of the
|
|
23
|
+
// generation does not stop a Staal run that has started: its outcome is in its journal
|
|
24
|
+
// and in the tool's status, as with any call.
|
|
25
|
+
|
|
26
|
+
import { tool, jsonSchema } from 'ai';
|
|
27
|
+
|
|
28
|
+
/** `staalTool` (from Tool.open) as an AI SDK tool. Give it to `tools` under `staalTool.name`. */
|
|
29
|
+
export function aiTool(staalTool, options = {}) {
|
|
30
|
+
if (!staalTool || typeof staalTool.call !== 'function' || typeof staalTool.name !== 'string') {
|
|
31
|
+
throw new TypeError('staal: aiTool takes a Tool from Tool.open');
|
|
32
|
+
}
|
|
33
|
+
const pick = options.context || ((context) => context);
|
|
34
|
+
if (typeof pick !== 'function') throw new TypeError('staal: aiTool: options.context is a function of the tool context');
|
|
35
|
+
const schema = { ...staalTool.schema };
|
|
36
|
+
delete schema.$schema;
|
|
37
|
+
return tool({
|
|
38
|
+
description: staalTool.description,
|
|
39
|
+
// No `validate`: the AI SDK passes the model's input on as it is, and Staal checks it.
|
|
40
|
+
inputSchema: jsonSchema(schema),
|
|
41
|
+
execute: async (input, { context }) => staalTool.call(input ?? {}, { context: await pick(context) }),
|
|
42
|
+
// What the model reads: the answer's text, as an MCP client would get it, marked
|
|
43
|
+
// as an error when Staal refused or the run failed.
|
|
44
|
+
toModelOutput: ({ output }) => (output.isError ? { type: 'error-text', value: output.text } : { type: 'text', value: output.text }),
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Several staal Tools as an AI SDK tool set, each under its own name. */
|
|
49
|
+
export function aiTools(staalTools, options = {}) {
|
|
50
|
+
if (!Array.isArray(staalTools)) throw new TypeError('staal: aiTools takes a list of Tools');
|
|
51
|
+
const set = {};
|
|
52
|
+
for (const t of staalTools) {
|
|
53
|
+
const made = aiTool(t, options);
|
|
54
|
+
if (Object.prototype.hasOwnProperty.call(set, t.name)) throw new TypeError(`staal: aiTools: two tools are named '${t.name}'`);
|
|
55
|
+
set[t.name] = made;
|
|
56
|
+
}
|
|
57
|
+
return set;
|
|
58
|
+
}
|