@restatedev/agents 0.7.0 → 0.8.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 +29 -5
- package/dist/agentlib/agent.d.ts +32 -0
- package/dist/agentlib/agent.d.ts.map +1 -0
- package/dist/agentlib/agent.js +172 -0
- package/dist/agentlib/agent.js.map +1 -0
- package/dist/agentlib/index.d.ts +5 -0
- package/dist/agentlib/index.d.ts.map +1 -0
- package/dist/agentlib/index.js +15 -0
- package/dist/agentlib/index.js.map +1 -0
- package/dist/agentlib/messages.d.ts +39 -0
- package/dist/agentlib/messages.d.ts.map +1 -0
- package/dist/agentlib/messages.js +177 -0
- package/dist/agentlib/messages.js.map +1 -0
- package/dist/agentlib/model.d.ts +29 -0
- package/dist/agentlib/model.d.ts.map +1 -0
- package/dist/agentlib/model.js +131 -0
- package/dist/agentlib/model.js.map +1 -0
- package/dist/agentlib/options.d.ts +58 -0
- package/dist/agentlib/options.d.ts.map +1 -0
- package/dist/agentlib/options.js +2 -0
- package/dist/agentlib/options.js.map +1 -0
- package/dist/agentlib/runtime.d.ts +37 -0
- package/dist/agentlib/runtime.d.ts.map +1 -0
- package/dist/agentlib/runtime.js +216 -0
- package/dist/agentlib/runtime.js.map +1 -0
- package/dist/agentlib/state.d.ts +114 -0
- package/dist/agentlib/state.d.ts.map +1 -0
- package/dist/agentlib/state.js +52 -0
- package/dist/agentlib/state.js.map +1 -0
- package/dist/agentlib/steering.d.ts +23 -0
- package/dist/agentlib/steering.d.ts.map +1 -0
- package/dist/agentlib/steering.js +48 -0
- package/dist/agentlib/steering.js.map +1 -0
- package/dist/agentlib/tools.d.ts +33 -0
- package/dist/agentlib/tools.d.ts.map +1 -0
- package/dist/agentlib/tools.js +243 -0
- package/dist/agentlib/tools.js.map +1 -0
- package/dist/agentlib/turn.d.ts +82 -0
- package/dist/agentlib/turn.d.ts.map +1 -0
- package/dist/agentlib/turn.js +253 -0
- package/dist/agentlib/turn.js.map +1 -0
- package/dist/common.d.ts +8 -0
- package/dist/common.d.ts.map +1 -1
- package/dist/common.js +12 -0
- package/dist/common.js.map +1 -1
- package/dist/companion.d.ts +2 -1
- package/dist/companion.d.ts.map +1 -1
- package/dist/companion.js.map +1 -1
- package/dist/deepseek/index.d.ts +2 -6
- package/dist/deepseek/index.d.ts.map +1 -1
- package/dist/deepseek/index.js +3 -14
- package/dist/deepseek/index.js.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -4
- package/dist/index.js.map +1 -1
- package/dist/opencode/index.d.ts.map +1 -1
- package/dist/opencode/index.js +11 -18
- package/dist/opencode/index.js.map +1 -1
- package/dist/tools.d.ts +53 -2
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +32 -15
- package/dist/tools.js.map +1 -1
- package/package.json +28 -4
- package/src/agentlib/README.md +229 -0
- package/src/agentlib/agent.ts +214 -0
- package/src/agentlib/index.ts +16 -0
- package/src/agentlib/messages.ts +207 -0
- package/src/agentlib/model.ts +150 -0
- package/src/agentlib/options.ts +62 -0
- package/src/agentlib/runtime.ts +252 -0
- package/src/agentlib/state.ts +93 -0
- package/src/agentlib/steering.ts +53 -0
- package/src/agentlib/tools.ts +353 -0
- package/src/agentlib/turn.ts +302 -0
- package/src/common.ts +15 -0
- package/src/companion.ts +2 -1
- package/src/deepseek/index.ts +5 -18
- package/src/index.ts +6 -4
- package/src/opencode/index.ts +12 -17
- package/src/tools.ts +118 -52
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// The agentlib agent's key/value state, and how its handlers reach that state,
|
|
2
|
+
// the agent's companion, and the agent itself.
|
|
3
|
+
import { internal } from '@restatedev/restate-sdk-gen';
|
|
4
|
+
import { sdk as restate } from '@restatedev/sessions/internal';
|
|
5
|
+
import { companionFor } from '../companion.js';
|
|
6
|
+
const { adopt } = internal;
|
|
7
|
+
export const META = 'meta';
|
|
8
|
+
/** The ids of steered messages that a turn took, so that their own `steered` turn does nothing. */
|
|
9
|
+
export const TAKEN = 'taken';
|
|
10
|
+
/** The model's conversation (agentlib's `LazyStateConversation`): chunks `t/<n>`, and `t/meta`. */
|
|
11
|
+
export const CONVERSATION = 't';
|
|
12
|
+
export const CONVERSATION_META = `${CONVERSATION}/meta`;
|
|
13
|
+
/** The key of the message `id`, as clients read it. */
|
|
14
|
+
export const entryKey = (id) => `c/${id}`;
|
|
15
|
+
export const emptyMeta = () => ({ chats: 0, cursor: 0 });
|
|
16
|
+
/** Reads state `key` inside an operation of the generator SDK. */
|
|
17
|
+
export function read(ctx, key) {
|
|
18
|
+
// At runtime, `get` returns a RestatePromise, although its type says Promise.
|
|
19
|
+
const value = ctx.get(key);
|
|
20
|
+
return adopt(value);
|
|
21
|
+
}
|
|
22
|
+
/** The agent's companion (`<agent>.prebuilt.restate`), at the agent's key. */
|
|
23
|
+
export function companionAt(ctx) {
|
|
24
|
+
const agent = ctx.request().target.service;
|
|
25
|
+
return ctx.objectClient(companionFor(agent), ctx.key);
|
|
26
|
+
}
|
|
27
|
+
/** The agent's companion, for one-way sends. */
|
|
28
|
+
export function companionSendAt(ctx) {
|
|
29
|
+
const agent = ctx.request().target.service;
|
|
30
|
+
return ctx.objectSendClient(companionFor(agent), ctx.key);
|
|
31
|
+
}
|
|
32
|
+
/** Calls one of the agent's own handlers, at its own key, with JSON in and out. */
|
|
33
|
+
export function callOwn(ctx, handler, input) {
|
|
34
|
+
return ctx.genericCall({
|
|
35
|
+
service: ctx.request().target.service,
|
|
36
|
+
method: handler,
|
|
37
|
+
key: ctx.key,
|
|
38
|
+
parameter: input,
|
|
39
|
+
inputSerde: restate.serde.json,
|
|
40
|
+
outputSerde: restate.serde.json,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Waits for the signal `name` and returns its value. The wait's id is
|
|
45
|
+
* `<invocation id>:<id>`; `pending` lists it with `input`, and `signal`
|
|
46
|
+
* resolves it.
|
|
47
|
+
*/
|
|
48
|
+
export function* waitForSignal(ctx, id, name, input) {
|
|
49
|
+
const wait = { id: `${ctx.request().id}:${id}`, name, input };
|
|
50
|
+
return yield* adopt(companionAt(ctx).wait(wait));
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state.js","sourceRoot":"","sources":["../../src/agentlib/state.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,+CAA+C;AAC/C,OAAO,EAAE,QAAQ,EAA+B,MAAM,6BAA6B,CAAC;AACpF,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,+BAA+B,CAAC;AAC/D,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C,MAAM,EAAE,KAAK,EAAE,GAAG,QAAQ,CAAC;AA4B3B,MAAM,CAAC,MAAM,IAAI,GAAG,MAAM,CAAC;AAE3B,mGAAmG;AACnG,MAAM,CAAC,MAAM,KAAK,GAAG,OAAO,CAAC;AAE7B,mGAAmG;AACnG,MAAM,CAAC,MAAM,YAAY,GAAG,GAAG,CAAC;AAChC,MAAM,CAAC,MAAM,iBAAiB,GAAG,GAAG,YAAY,OAAO,CAAC;AAExD,uDAAuD;AACvD,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,EAAU,EAAE,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC;AAElD,MAAM,CAAC,MAAM,SAAS,GAAG,GAAS,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC;AAE/D,kEAAkE;AAClE,MAAM,UAAU,IAAI,CAAI,GAA0B,EAAE,GAAW;IAC7D,8EAA8E;IAC9E,MAAM,KAAK,GAAG,GAAG,CAAC,GAAG,CAAI,GAAG,CAAgD,CAAC;IAC7E,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC;AACtB,CAAC;AAED,8EAA8E;AAC9E,MAAM,UAAU,WAAW,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ProgressEvent, SteeringUpdate } from '@restatedev/agentlib';
|
|
2
|
+
import { type Future, type Operation } from '@restatedev/restate-sdk-gen';
|
|
3
|
+
import type { Published } from '../companion.ts';
|
|
4
|
+
/**
|
|
5
|
+
* The messages steered to a turn. The turn learns of them when it publishes
|
|
6
|
+
* a change to its companion, which answers with the messages it holds for
|
|
7
|
+
* the turn. They go to the run, all at once, as one steering update, at the
|
|
8
|
+
* run's next progress event after it asks for one.
|
|
9
|
+
*/
|
|
10
|
+
export declare class Steering {
|
|
11
|
+
private next?;
|
|
12
|
+
private waiting;
|
|
13
|
+
private readonly handed;
|
|
14
|
+
/** agentlib's `controls.steering`: the next steering update, once there is one. */
|
|
15
|
+
readonly source: () => Future<string | SteeringUpdate>;
|
|
16
|
+
/** Notes the messages that a publish (or the turn's start) says are steered to the turn. */
|
|
17
|
+
saw(published: Published | undefined): void;
|
|
18
|
+
/** Hands the waiting messages to the run, if it asked for an update. */
|
|
19
|
+
hand(): Operation<void>;
|
|
20
|
+
}
|
|
21
|
+
/** Returns the ids of the steered messages that a `steering` event says the run took. */
|
|
22
|
+
export declare function takenIds(event: ProgressEvent): string[];
|
|
23
|
+
//# sourceMappingURL=steering.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"steering.d.ts","sourceRoot":"","sources":["../../src/agentlib/steering.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC1E,OAAO,EAAyB,KAAK,MAAM,EAAE,KAAK,SAAS,EAAE,MAAM,6BAA6B,CAAC;AACjG,OAAO,KAAK,EAAE,SAAS,EAAS,MAAM,iBAAiB,CAAC;AAExD;;;;;GAKG;AACH,qBAAa,QAAQ;IACnB,OAAO,CAAC,IAAI,CAAC,CAAmC;IAChD,OAAO,CAAC,OAAO,CAAe;IAC9B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAqB;IAE5C,mFAAmF;IACnF,QAAQ,CAAC,MAAM,QAAO,MAAM,CAAC,MAAM,GAAG,cAAc,CAAC,CAGnD;IAEF,4FAA4F;IAC5F,GAAG,CAAC,SAAS,EAAE,SAAS,GAAG,SAAS,QAKnC;IAED,wEAAwE;IACvE,IAAI,IAAI,SAAS,CAAC,IAAI,CAAC,CAWvB;CACF;AAED,yFAAyF;AACzF,wBAAgB,QAAQ,CAAC,KAAK,EAAE,aAAa,GAAG,MAAM,EAAE,CAKvD"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { channel } from '@restatedev/restate-sdk-gen';
|
|
2
|
+
/**
|
|
3
|
+
* The messages steered to a turn. The turn learns of them when it publishes
|
|
4
|
+
* a change to its companion, which answers with the messages it holds for
|
|
5
|
+
* the turn. They go to the run, all at once, as one steering update, at the
|
|
6
|
+
* run's next progress event after it asks for one.
|
|
7
|
+
*/
|
|
8
|
+
export class Steering {
|
|
9
|
+
next;
|
|
10
|
+
waiting = [];
|
|
11
|
+
handed = new Set();
|
|
12
|
+
/** agentlib's `controls.steering`: the next steering update, once there is one. */
|
|
13
|
+
source = () => {
|
|
14
|
+
this.next = channel();
|
|
15
|
+
return this.next.receive;
|
|
16
|
+
};
|
|
17
|
+
/** Notes the messages that a publish (or the turn's start) says are steered to the turn. */
|
|
18
|
+
saw(published) {
|
|
19
|
+
for (const steer of published?.steering ?? []) {
|
|
20
|
+
const known = this.handed.has(steer.id) || this.waiting.some((s) => s.id === steer.id);
|
|
21
|
+
if (!known)
|
|
22
|
+
this.waiting.push(steer);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/** Hands the waiting messages to the run, if it asked for an update. */
|
|
26
|
+
*hand() {
|
|
27
|
+
const next = this.next;
|
|
28
|
+
if (!next || this.waiting.length === 0)
|
|
29
|
+
return;
|
|
30
|
+
const batch = this.waiting.splice(0);
|
|
31
|
+
for (const { id } of batch)
|
|
32
|
+
this.handed.add(id);
|
|
33
|
+
this.next = undefined;
|
|
34
|
+
const message = batch.map((steer) => steer.text).join('\n\n');
|
|
35
|
+
const ids = batch.map((steer) => steer.id);
|
|
36
|
+
yield* next.send({ message, data: { steer: ids } });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/** Returns the ids of the steered messages that a `steering` event says the run took. */
|
|
40
|
+
export function takenIds(event) {
|
|
41
|
+
if (event.type !== 'steering')
|
|
42
|
+
return [];
|
|
43
|
+
const ids = event.data?.steer;
|
|
44
|
+
if (!Array.isArray(ids))
|
|
45
|
+
return [];
|
|
46
|
+
return ids.filter((id) => typeof id === 'string');
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=steering.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"steering.js","sourceRoot":"","sources":["../../src/agentlib/steering.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAA6C,MAAM,6BAA6B,CAAC;AAGjG;;;;;GAKG;AACH,MAAM,OAAO,QAAQ;IACX,IAAI,CAAoC;IACxC,OAAO,GAAY,EAAE,CAAC;IACb,MAAM,GAAG,IAAI,GAAG,EAAU,CAAC;IAE5C,mFAAmF;IAC1E,MAAM,GAAG,GAAoC,EAAE;QACtD,IAAI,CAAC,IAAI,GAAG,OAAO,EAA2B,CAAC;QAC/C,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC;IAC3B,CAAC,CAAC;IAEF,4FAA4F;IAC5F,GAAG,CAAC,SAAgC;QAClC,KAAK,MAAM,KAAK,IAAI,SAAS,EAAE,QAAQ,IAAI,EAAE,EAAE,CAAC;YAC9C,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,KAAK,CAAC,EAAE,CAAC,CAAC;YACvF,IAAI,CAAC,KAAK;gBAAE,IAAI,CAAC,OAAO,CAAC,IAAI,CAA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
|
|
1
|
+
import { type Tool as LibTool } from '@restatedev/agentlib';
|
|
2
|
+
import { sdk as restate } from '@restatedev/sessions/internal';
|
|
3
|
+
import { type Approval, type Tool } from '../tools.ts';
|
|
4
|
+
/**
|
|
5
|
+
* The context of an agentlib agent's run (agentlib's `RunContext.context`,
|
|
6
|
+
* and a tool's `context.context`): the invocation's context of the classic
|
|
7
|
+
* SDK. An operation awaits its calls with `internal.adopt` of
|
|
8
|
+
* @restatedev/restate-sdk-gen.
|
|
9
|
+
*/
|
|
10
|
+
export interface AgentlibContext {
|
|
11
|
+
readonly ctx: restate.ObjectContext;
|
|
12
|
+
}
|
|
13
|
+
/** A tool of agentlib (`tool`, `asyncTool`, `serviceTool`, `agentTool`, MCP's...). */
|
|
14
|
+
export type AgentlibTool = LibTool<any>;
|
|
15
|
+
/**
|
|
16
|
+
* An agentlib agent's tools: the tools of `@restatedev/agents` (named by
|
|
17
|
+
* their `name`), and agentlib's own (named by their key), in a list or in
|
|
18
|
+
* one record.
|
|
19
|
+
*/
|
|
20
|
+
export type Tools = ReadonlyArray<Tool | Record<string, AgentlibTool>> | Record<string, AgentlibTool | Tool>;
|
|
21
|
+
export type { Approval };
|
|
22
|
+
/**
|
|
23
|
+
* Gives each tool its name, turning the tools of `@restatedev/agents` into
|
|
24
|
+
* agentlib's. Those that `approval` names wait for a person's approval.
|
|
25
|
+
*/
|
|
26
|
+
export declare function toolsOf(agent: string, tools: Tools, approval?: Approval): Record<string, AgentlibTool>;
|
|
27
|
+
/**
|
|
28
|
+
* Turns `tool`, one of `@restatedev/agents`, into agentlib's, named `name`.
|
|
29
|
+
* As in every agent, arguments that do not fit its input are the model's to
|
|
30
|
+
* fix, and a terminal error is the tool's answer, which the model sees.
|
|
31
|
+
*/
|
|
32
|
+
export declare function fromTool(tool: Tool, name?: string, approval?: Approval): AgentlibTool;
|
|
33
|
+
//# sourceMappingURL=tools.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../../src/agentlib/tools.ts"],"names":[],"mappings":"AAYA,OAAO,EAIL,KAAK,IAAI,IAAI,OAAO,EAErB,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,+BAA+B,CAAC;AAG/D,OAAO,EACL,KAAK,QAAQ,EAQb,KAAK,IAAI,EAEV,MAAM,aAAa,CAAC;AAGrB;;;;;GAKG;AACH,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC,aAAa,CAAC;CACrC;AAED,sFAAsF;AAGtF,MAAM,MAAM,YAAY,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;AAExC;;;;GAIG;AACH,MAAM,MAAM,KAAK,GACb,aAAa,CAAC,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC,GAClD,MAAM,CAAC,MAAM,EAAE,YAAY,GAAG,IAAI,CAAC,CAAC;AAExC,YAAY,EAAE,QAAQ,EAAE,CAAC;AAEzB;;;GAGG;AACH,wBAAgB,OAAO,CACrB,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,KAAK,EACZ,QAAQ,CAAC,EAAE,QAAQ,GAClB,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAiC9B;AAED;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,SAAY,EAAE,QAAQ,CAAC,EAAE,QAAQ,GAAG,YAAY,CA6BxF"}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
// The tools of @restatedev/agents in an agentlib agent. Each becomes an
|
|
2
|
+
// agentlib tool whose calls are the generator SDK's, on the classic context
|
|
3
|
+
// of the agent's invocation, so Restate journals them:
|
|
4
|
+
//
|
|
5
|
+
// - `call`, `subagent` and `workflow` are calls: a crash replays their answer,
|
|
6
|
+
// and stopping the turn cancels them. No call records or idempotency keys
|
|
7
|
+
// are needed, as in a session object.
|
|
8
|
+
// - `send` is a send, made once.
|
|
9
|
+
// - `signal` waits at the agent's companion.
|
|
10
|
+
// - A `tool()` runs as a journaled step, as agentlib's `asyncTool` does. Its
|
|
11
|
+
// `ctx` has `key` and `request`, but no database or calls: a classic
|
|
12
|
+
// object has no session.
|
|
13
|
+
import { RunFailedError, tool as libTool, } from '@restatedev/agentlib';
|
|
14
|
+
import { call, run, send, serde } from '@restatedev/restate-sdk-gen';
|
|
15
|
+
import { sdk as restate } from '@restatedev/sessions/internal';
|
|
16
|
+
import { isTool, keyed, sentTo, specOf, text, } from '../tools.js';
|
|
17
|
+
import { waitForSignal } from './state.js';
|
|
18
|
+
/**
|
|
19
|
+
* Gives each tool its name, turning the tools of `@restatedev/agents` into
|
|
20
|
+
* agentlib's. Those that `approval` names wait for a person's approval.
|
|
21
|
+
*/
|
|
22
|
+
export function toolsOf(agent, tools, approval) {
|
|
23
|
+
const named = {};
|
|
24
|
+
const ours = new Set();
|
|
25
|
+
const add = (name, tool) => {
|
|
26
|
+
if (name in named)
|
|
27
|
+
throw new Error(`${agent}: two tools are named ${name}`);
|
|
28
|
+
if (isTool(tool)) {
|
|
29
|
+
named[name] = fromTool(tool, name, approval);
|
|
30
|
+
ours.add(name);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
named[name] = tool;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
const addRecord = (record) => {
|
|
37
|
+
for (const [name, tool] of Object.entries(record))
|
|
38
|
+
add(name, tool);
|
|
39
|
+
};
|
|
40
|
+
if (Array.isArray(tools)) {
|
|
41
|
+
for (const tool of tools) {
|
|
42
|
+
if (isTool(tool))
|
|
43
|
+
add(tool.name, tool);
|
|
44
|
+
else
|
|
45
|
+
addRecord(tool);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
addRecord(tools);
|
|
50
|
+
}
|
|
51
|
+
for (const name of Array.isArray(approval) ? approval : []) {
|
|
52
|
+
if (ours.has(name))
|
|
53
|
+
continue;
|
|
54
|
+
throw new Error(`${agent}: approval names ${name}, which is none of its tools of @restatedev/agents ` +
|
|
55
|
+
"(agentlib's own tools take needsApproval)");
|
|
56
|
+
}
|
|
57
|
+
return named;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Turns `tool`, one of `@restatedev/agents`, into agentlib's, named `name`.
|
|
61
|
+
* As in every agent, arguments that do not fit its input are the model's to
|
|
62
|
+
* fix, and a terminal error is the tool's answer, which the model sees.
|
|
63
|
+
*/
|
|
64
|
+
export function fromTool(tool, name = tool.name, approval) {
|
|
65
|
+
const spec = specOf(tool);
|
|
66
|
+
if (!spec) {
|
|
67
|
+
throw new Error(`${tool.name} is no tool of @restatedev/agents (tool(), call, send...)`);
|
|
68
|
+
}
|
|
69
|
+
function* execute(input, { context, callId }) {
|
|
70
|
+
try {
|
|
71
|
+
return yield* perform(tool, spec, input, context.ctx, callId);
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
// A tool() that reached for what an agentlib agent lacks is a programming
|
|
75
|
+
// error: it fails the turn, rather than reach the model as an answer.
|
|
76
|
+
if (error instanceof restate.TerminalError && error.message.startsWith(MISSING)) {
|
|
77
|
+
throw new RunFailedError(`the tool ${tool.name}: ${error.message}`);
|
|
78
|
+
}
|
|
79
|
+
throw error;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// agentlib's own tool(): it checks the input, and asks for the approval.
|
|
83
|
+
return libTool({
|
|
84
|
+
description: tool.description,
|
|
85
|
+
input: tool.input,
|
|
86
|
+
needsApproval: needsApprovalOf(name, approval),
|
|
87
|
+
execute,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
/** Whether the tool `name` needs approval, as agentlib's `needsApproval` takes it. */
|
|
91
|
+
function needsApprovalOf(name, approval) {
|
|
92
|
+
if (approval === undefined)
|
|
93
|
+
return false;
|
|
94
|
+
if (typeof approval !== 'function')
|
|
95
|
+
return approval.includes(name);
|
|
96
|
+
const asks = approval;
|
|
97
|
+
// oxlint-disable-next-line require-yield -- deciding needs no wait
|
|
98
|
+
return function* (args) {
|
|
99
|
+
return asks({ name, args });
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/** Does what `tool` does, with its checked `input`, as tool call `callId`. */
|
|
103
|
+
function* perform(tool, spec, input, ctx, callId) {
|
|
104
|
+
switch (spec.kind) {
|
|
105
|
+
case 'run':
|
|
106
|
+
return yield* runTool(tool.name, spec.run, input, ctx, callId);
|
|
107
|
+
case 'call':
|
|
108
|
+
return yield* callTool(tool.name, spec, input, ctx);
|
|
109
|
+
case 'send':
|
|
110
|
+
return yield* sendTool(tool.name, spec, input, ctx);
|
|
111
|
+
case 'workflow':
|
|
112
|
+
return yield* workflowTool(tool.name, spec, input, ctx, callId);
|
|
113
|
+
case 'signal': {
|
|
114
|
+
const shown = spec.shown ? input : undefined;
|
|
115
|
+
return text(yield* waitForSignal(ctx, callId, spec.name, shown));
|
|
116
|
+
}
|
|
117
|
+
case 'subagent': {
|
|
118
|
+
const { name, task } = input;
|
|
119
|
+
return text(yield* callJson(spec.agent, spec.handler, `${ctx.key}.${name}`, task));
|
|
120
|
+
}
|
|
121
|
+
default:
|
|
122
|
+
throw new Error(`the tool ${tool.name} does what no agentlib agent knows`);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* A `tool()`: its `run` as a journaled step. It has no retry limit of its
|
|
127
|
+
* own: a failure fails the attempt, which Restate retries.
|
|
128
|
+
*/
|
|
129
|
+
function* runTool(name, runOf, input, ctx, callId) {
|
|
130
|
+
const context = { ctx: sessionOf(ctx), callId, idempotencyKey: `${ctx.request().id}:${callId}` };
|
|
131
|
+
const action = runOf;
|
|
132
|
+
return yield* run(async () => text(await action(input, context)), { name, retry: {} });
|
|
133
|
+
}
|
|
134
|
+
/** A `call`: a call of the handler, or the run of a workflow, which it waits for. */
|
|
135
|
+
function* callTool(name, spec, input, ctx) {
|
|
136
|
+
const { target, handler, options } = spec;
|
|
137
|
+
const { key, parameter } = yield* split(name, target, options, input, ctx);
|
|
138
|
+
if ('workflow' in target && handler === 'run') {
|
|
139
|
+
return text(yield* runWorkflow(target.name, key, parameter));
|
|
140
|
+
}
|
|
141
|
+
return text(yield* callJson(target.name, handler, key, parameter));
|
|
142
|
+
}
|
|
143
|
+
/** A `send`: a one-way call, after the tool's `delay` if it has one. */
|
|
144
|
+
function* sendTool(name, spec, input, ctx) {
|
|
145
|
+
const { target, handler, options } = spec;
|
|
146
|
+
const { key, parameter } = yield* split(name, target, options, input, ctx);
|
|
147
|
+
const message = { service: target.name, method: handler, key, parameter, inputSerde: serde.json };
|
|
148
|
+
send(options.delay === undefined ? message : { ...message, delay: options.delay });
|
|
149
|
+
return sentTo(target.name, key, handler);
|
|
150
|
+
}
|
|
151
|
+
/** A `workflow`: a run of its own per tool call, unless the tool's `key` gives its id. */
|
|
152
|
+
function* workflowTool(name, spec, input, ctx, callId) {
|
|
153
|
+
const { target, options } = spec;
|
|
154
|
+
const key = options.key === undefined ? `${ctx.key}-${callId}` : yield* configured(name, options.key, ctx);
|
|
155
|
+
return text(yield* runWorkflow(target.name, key, options.input ? input : undefined));
|
|
156
|
+
}
|
|
157
|
+
// The calls.
|
|
158
|
+
/** Calls `method` of `service` (at `key`), with JSON in and out. */
|
|
159
|
+
function callJson(service, method, key, parameter) {
|
|
160
|
+
return call({
|
|
161
|
+
service,
|
|
162
|
+
method,
|
|
163
|
+
key,
|
|
164
|
+
parameter,
|
|
165
|
+
inputSerde: serde.json,
|
|
166
|
+
outputSerde: serde.json,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Runs workflow `service` with id `key` to its end, and returns its result.
|
|
171
|
+
* A workflow's `run` runs once per id: Restate refuses a second call of it,
|
|
172
|
+
* but that call's invocation id is the run's. So the tool calls `run`, takes
|
|
173
|
+
* the call's invocation id, and waits for that invocation; a fixed id that
|
|
174
|
+
* ran before gets its run's result. It is a call, not a send, so that
|
|
175
|
+
* stopping the turn cancels the run.
|
|
176
|
+
*/
|
|
177
|
+
function* runWorkflow(service, key, parameter) {
|
|
178
|
+
const started = call({
|
|
179
|
+
service,
|
|
180
|
+
method: 'run',
|
|
181
|
+
key,
|
|
182
|
+
parameter,
|
|
183
|
+
inputSerde: serde.json,
|
|
184
|
+
outputSerde: serde.binary,
|
|
185
|
+
});
|
|
186
|
+
const invocation = yield* started.invocation;
|
|
187
|
+
return yield* invocation.attach(serde.json);
|
|
188
|
+
}
|
|
189
|
+
/** The key, and the handler's input, of a tool call's input (as `split` in ../tools.ts). */
|
|
190
|
+
function* split(name, target, options, input, ctx) {
|
|
191
|
+
if (!keyed(target)) {
|
|
192
|
+
return { key: undefined, parameter: options.input ? input : undefined };
|
|
193
|
+
}
|
|
194
|
+
if (options.key !== undefined) {
|
|
195
|
+
const key = yield* configured(name, options.key, ctx);
|
|
196
|
+
return { key, parameter: options.input ? input : undefined };
|
|
197
|
+
}
|
|
198
|
+
// The model chose the key, as a field of the tool's input.
|
|
199
|
+
const { key, ...rest } = input;
|
|
200
|
+
return { key, parameter: options.input ? rest : undefined };
|
|
201
|
+
}
|
|
202
|
+
/** `value`, or, for a function of a session's context, what it returns, as a journaled step. */
|
|
203
|
+
function* configured(name, value, ctx) {
|
|
204
|
+
if (typeof value !== 'function')
|
|
205
|
+
return value;
|
|
206
|
+
const session = sessionOf(ctx);
|
|
207
|
+
const of = value;
|
|
208
|
+
return yield* run(async () => of(session), { name: `${name}: key` });
|
|
209
|
+
}
|
|
210
|
+
// What a tool() gets as its ctx.
|
|
211
|
+
/** How the error of a `tool()` that reached for a missing `ctx` field starts. */
|
|
212
|
+
const MISSING = 'not in an agentlib agent:';
|
|
213
|
+
const NO_DATABASE = 'an agentlib agent keeps its conversation in its state, with no SQL database';
|
|
214
|
+
const CALLS_INSTEAD = "give the agent Restate's tools (call, send, workflow, subagent) or agentlib's own";
|
|
215
|
+
/** Throws the error for a `tool()` that reached for `what`, saying what to use `instead`. */
|
|
216
|
+
function missing(what, instead) {
|
|
217
|
+
throw new restate.TerminalError(`${MISSING} ${what}: ${instead}`);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* A session object's context, as a `tool()` in an agentlib agent gets it:
|
|
221
|
+
* `key` and `request`. Its database and calls fail the turn, saying what to
|
|
222
|
+
* use instead.
|
|
223
|
+
*/
|
|
224
|
+
function sessionOf(ctx) {
|
|
225
|
+
const request = ctx.request();
|
|
226
|
+
return Object.freeze({
|
|
227
|
+
key: ctx.key,
|
|
228
|
+
request,
|
|
229
|
+
get db() {
|
|
230
|
+
return missing('ctx.db', NO_DATABASE);
|
|
231
|
+
},
|
|
232
|
+
get rpc() {
|
|
233
|
+
return missing('ctx.rpc', CALLS_INSTEAD);
|
|
234
|
+
},
|
|
235
|
+
get send() {
|
|
236
|
+
return missing('ctx.send', CALLS_INSTEAD);
|
|
237
|
+
},
|
|
238
|
+
get cancel() {
|
|
239
|
+
return missing('ctx.cancel', CALLS_INSTEAD);
|
|
240
|
+
},
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
//# sourceMappingURL=tools.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.js","sourceRoot":"","sources":["../../src/agentlib/tools.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,4EAA4E;AAC5E,uDAAuD;AACvD,EAAE;AACF,+EAA+E;AAC/E,4EAA4E;AAC5E,wCAAwC;AACxC,iCAAiC;AACjC,6CAA6C;AAC7C,6EAA6E;AAC7E,uEAAuE;AACvE,2BAA2B;AAC3B,OAAO,EACL,cAAc,EACd,IAAI,IAAI,OAAO,GAIhB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,KAAK,EAAkB,MAAM,6BAA6B,CAAC;AACrF,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,+BAA+B,CAAC;AAG/D,OAAO,EAEL,M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,CAAC,GAAG,CAAC,KAAK,IAAI,EAAE,CAAC,EAAE,CAAC,OAAO,CAAC,EAAE,EAAE,IAAI,EAAE,GAAG,IAAI,OAAO,EAAE,CAAC,CAAC;AACvE,CAAC;AAED,iCAAiC;AAEjC,iFAAiF;AACjF,MAAM,OAAO,GAAG,2BAA2B,CAAC;AAE5C,MAAM,WAAW,GAAG,6EAA6E,CAAC;AAElG,MAAM,aAAa,GACjB,mFAAmF,CAAC;AAEtF,6FAA6F;AAC7F,SAAS,OAAO,CAAC,IAAY,EAAE,OAAe;IAC5C,MAAM,IAAI,OAAO,CAAC,aAAa,CAAC,GAAG,OAAO,IAAI,IAAI,KAAK,OAAO,EAAE,CAAC,CAAC;AACpE,CAAC;AAED;;;;GAIG;AACH,SAAS,SAAS,CAAC,GAA0B;IAC3C,MAAM,OAAO,GAAG,GAAG,CAAC,OAAO,EAAE,CAAC;IAC9B,OAAO,MAAM,CAAC,MAAM,CAAC;QACnB,GAAG,EAAE,GAAG,CAAC,GAAG;QACZ,OAAO;QACP,IAAI,EAAE;YACJ,OAAO,OAAO,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC;QACxC,CAAC;QACD,IAAI,GAAG;YACL,OAAO,OAAO,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;QAC3C,CAAC;QACD,IAAI,IAAI;YACN,OAAO,OAAO,CAAC,UAAU,EAAE,aAAa,CAAC,CAAC;QAC5C,CAAC;QACD,IAAI,MAAM;YACR,OAAO,OAAO,CAAC,YAAY,EAAE,aAAa,CAAC,CAAC;QAC9C,CAAC;KACF,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { LazyStateConversation, type CompactionPolicy, type Conversation, type ConversationCompactionStatus, type ConversationSnapshot, type Message, type Model, type ProgressEvent } from '@restatedev/agentlib';
|
|
2
|
+
import { type Operation } from '@restatedev/restate-sdk-gen';
|
|
3
|
+
import type { sdk as restate } from '@restatedev/sessions/internal';
|
|
4
|
+
import { type LiveTurn } from './state.ts';
|
|
5
|
+
/** How the model's conversation is summarized at a turn's start (see the agent's `history`). */
|
|
6
|
+
export interface Compaction {
|
|
7
|
+
model: Model;
|
|
8
|
+
policy?: CompactionPolicy;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The model's conversation, in the state of the object of `ctx`: agentlib's
|
|
12
|
+
* `LazyStateConversation` over that state, its reads journaled. With
|
|
13
|
+
* `compaction`, it summarizes its earliest messages when they grow too many.
|
|
14
|
+
*/
|
|
15
|
+
export declare function conversationOf(ctx: restate.ObjectContext, compaction?: Compaction): LazyStateConversation;
|
|
16
|
+
/**
|
|
17
|
+
* A turn being answered. It keeps the turn's messages as clients read them
|
|
18
|
+
* (one state key each), and is the run's `Conversation`: agentlib loads the
|
|
19
|
+
* model's conversation through it, and saves the turn through it when the
|
|
20
|
+
* run ends. Its messages are then rewritten from what the run saved, under
|
|
21
|
+
* the same ids.
|
|
22
|
+
*/
|
|
23
|
+
export declare class Turn implements Conversation {
|
|
24
|
+
readonly live: LiveTurn;
|
|
25
|
+
/** Summarizes the model's conversation at the run's start, if the agent's `history` says to. */
|
|
26
|
+
compactor?: LazyStateConversation;
|
|
27
|
+
private readonly ctx;
|
|
28
|
+
private readonly meta;
|
|
29
|
+
private readonly conversation;
|
|
30
|
+
/** The turn's messages, as clients read them. Message `i` has id `live.from + i`. */
|
|
31
|
+
private entries;
|
|
32
|
+
/** The index in {@link entries} of each step's reply, by step. */
|
|
33
|
+
private readonly replies;
|
|
34
|
+
private step;
|
|
35
|
+
/** The conversation's version, once loaded or appended to. */
|
|
36
|
+
private version?;
|
|
37
|
+
/** The turn's messages of agentlib, once the run saved them. */
|
|
38
|
+
private saved;
|
|
39
|
+
private constructor();
|
|
40
|
+
/**
|
|
41
|
+
* Starts the turn that answers `prompt`. A turn before it that never ended
|
|
42
|
+
* (its invocation was killed) ends first.
|
|
43
|
+
*/
|
|
44
|
+
static start(ctx: restate.ObjectContext, prompt: string): Promise<Turn>;
|
|
45
|
+
/** Ends the turn `live`, whose invocation was killed, with the note {@link ENDED}. */
|
|
46
|
+
private static endKilled;
|
|
47
|
+
/** Records what a progress event of the run changes. Returns whether clients see a change. */
|
|
48
|
+
progress(event: ProgressEvent): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Returns the tool part of call `callId` and the index of its reply. A call
|
|
51
|
+
* that started earlier (a background call can end steps later) is found
|
|
52
|
+
* where it is; a new one is added to the current step's reply.
|
|
53
|
+
*/
|
|
54
|
+
private toolPart;
|
|
55
|
+
load(): Operation<ConversationSnapshot>;
|
|
56
|
+
compact(): Operation<ConversationCompactionStatus>;
|
|
57
|
+
/**
|
|
58
|
+
* Saves the turn's messages. Clients then read the turn as it was saved,
|
|
59
|
+
* with its replies' texts.
|
|
60
|
+
*/
|
|
61
|
+
append(messages: readonly Message[], expectedVersion: number): Operation<number>;
|
|
62
|
+
/** Ends the turn with its answer. A run that saved nothing leaves the prompt and the answer. */
|
|
63
|
+
answered(answer: unknown): Promise<void>;
|
|
64
|
+
/** Ends the turn without an answer: `note` says how (stopped, refused, failed, killed). */
|
|
65
|
+
close(note: string): Promise<void>;
|
|
66
|
+
/**
|
|
67
|
+
* Ends a turn that the run did not save, with `text`. The model's
|
|
68
|
+
* conversation gets the prompt, the messages steered to the turn, and
|
|
69
|
+
* `text`, but not the turn's tool calls, whose results may be missing.
|
|
70
|
+
* Clients see each open tool call closed with `text`.
|
|
71
|
+
*/
|
|
72
|
+
private endUnsaved;
|
|
73
|
+
/** Appends `messages` to the model's conversation outside the run, in an operation of its own. */
|
|
74
|
+
private appendOutsideRun;
|
|
75
|
+
/** Marks the turn ended: the next turn's messages start after its own. */
|
|
76
|
+
private finish;
|
|
77
|
+
/** Stores message `i` of the turn with the next cursor, and the turn's bookkeeping. */
|
|
78
|
+
private write;
|
|
79
|
+
/** Replaces the turn's messages with `entries`, each with a new cursor, in order. */
|
|
80
|
+
private rewrite;
|
|
81
|
+
}
|
|
82
|
+
//# sour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