@restatedev/agents 0.7.0 → 0.9.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
package/README.md
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
# @restatedev/agents
|
|
2
2
|
|
|
3
|
-
Durable agents on Restate: an agent of one of
|
|
4
|
-
LangGraph, Claude, the DeepSeek Harness, OpenCode) in a Restate virtual
|
|
5
|
-
object, its conversation in the object's own SQL database
|
|
3
|
+
Durable agents on Restate: an agent of one of six frameworks (pi,
|
|
4
|
+
LangGraph, Claude, the DeepSeek Harness, OpenCode, agentlib) in a Restate virtual
|
|
5
|
+
object, its conversation in the object's own SQL database (agentlib's, in
|
|
6
|
+
its key/value state, journaled). A crash, a restart or a
|
|
6
7
|
person who takes a day to approve changes nothing: each message is answered
|
|
7
8
|
once, and each tool call is made once.
|
|
8
9
|
|
|
@@ -69,6 +70,13 @@ served([...]), port })`. `RESTATE_SERVE_ONLY=triage,planner` serves only
|
|
|
69
70
|
the services it names, each agent with its companion, so that one program
|
|
70
71
|
(or image) deploys in parts.
|
|
71
72
|
|
|
73
|
+
On Restate Cloud, where the program connects out rather than being called
|
|
74
|
+
in, use this package's `connect` (@restatedev/sessions'):
|
|
75
|
+
`connect({ services: [assistant], name: 'assistant-v1' })` brings each
|
|
76
|
+
agent's companion as `serve` does. The Restate SDK's own `connect` does
|
|
77
|
+
not: it binds the agents alone, and the first tool that waits has nothing
|
|
78
|
+
to wait on.
|
|
79
|
+
|
|
72
80
|
## Choose a framework
|
|
73
81
|
|
|
74
82
|
The same agent runs on each of them: `agent({ name, ...options })`, with
|
|
@@ -86,6 +94,7 @@ the handler `chat` (a message in, the answer out), and the views
|
|
|
86
94
|
| [`@restatedev/agents/claude`](src/claude/README.md) | the Anthropic SDK's tool runner | You want Claude with nothing in between: the lightest, the API's own messages |
|
|
87
95
|
| [`@restatedev/agents/deepseek`](src/deepseek/README.md) | DeepSeek Harness | You want DeepSeek's harness: DeepSeek's models, or any pi-ai knows |
|
|
88
96
|
| [`@restatedev/agents/opencode`](src/opencode/README.md) | OpenCode | You want OpenCode's agent: its own coding tools in a workspace, any provider it serves |
|
|
97
|
+
| [`@restatedev/agents/agentlib`](src/agentlib/README.md) | agentlib (Restate's own) | You want Restate's agent library: its tools, journaled on the classic Restate SDK, not in SQL |
|
|
89
98
|
|
|
90
99
|
Each framework's packages are optional peer dependencies of
|
|
91
100
|
@restatedev/agents: a project installs @restatedev/agents, whose required peers
|
|
@@ -104,6 +113,8 @@ npm install @restatedev/agents @earendil-works/pi-ai@1.5.2 @deepseek-ai/cordis \
|
|
|
104
113
|
@deepseek-ai/{dsh-system-prompt,dsh-tools,dsh-tool-executor-native,dsh-user-approval}@0.2.2-rc.2 \
|
|
105
114
|
@deepseek-ai/{dsh-commands,dsh-compaction,dsh-compaction-basic,dsh-compaction-tool-result-pruner,dsh-token-meter}@0.2.2-rc.2 # DeepSeek
|
|
106
115
|
npm install @restatedev/agents @opencode/{core,sdk,server,plugin}@2.2.7 effect@4.1.0 # OpenCode, on Node 24
|
|
116
|
+
npm install @restatedev/agents @restatedev/agentlib@0.1.0 @restatedev/restate-sdk-gen@1.18.2 \
|
|
117
|
+
@restatedev/restate-sdk@1.18.2 @ai-sdk/openai # agentlib, and the provider of its models: or @ai-sdk/anthropic
|
|
107
118
|
```
|
|
108
119
|
|
|
109
120
|
pi's packages are on 1.5 (`~1.5.2`): pi changes its API in minor releases
|
|
@@ -135,6 +146,16 @@ provider's outage is asked again as pi does (its `retry` option), and
|
|
|
135
146
|
never ends the turn. Not there yet: a change of `model` for a key that has
|
|
136
147
|
a session already ([its README](src/opencode/README.md)).
|
|
137
148
|
|
|
149
|
+
**agentlib** runs on the classic Restate SDK, not in a session object: its
|
|
150
|
+
`chat` is journaled, and a crash replays it, so that a model call or a tool
|
|
151
|
+
call that finished is not made again; its conversation is in the object's
|
|
152
|
+
key/value state. It has every agent's handlers and views, and their
|
|
153
|
+
companion, and takes the tools of `@restatedev/agents` below beside
|
|
154
|
+
agentlib's own (`asyncTool`, `serviceTool`, `agentTool`, MCP's...), though a
|
|
155
|
+
`tool()` of its own has no `ctx.db`; and neither `schema`, `window` nor
|
|
156
|
+
`budget`, which are the SQL database's: `history` keeps its conversation
|
|
157
|
+
short ([its README](src/agentlib/README.md)).
|
|
158
|
+
|
|
138
159
|
Every agent takes these too, beside its framework's options:
|
|
139
160
|
|
|
140
161
|
| Option | What |
|
|
@@ -147,7 +168,8 @@ Every agent takes these too, beside its framework's options:
|
|
|
147
168
|
|
|
148
169
|
`window` and `budget` bound what Claude's and LangGraph's model is sent
|
|
149
170
|
([what the model gets](docs/conversation.md)); pi, the DeepSeek Harness and
|
|
150
|
-
OpenCode keep their own history, and compact it themselves
|
|
171
|
+
OpenCode keep their own history, and compact it themselves; agentlib's
|
|
172
|
+
`history` summarizes its own. An object holds one
|
|
151
173
|
framework's conversation: its turns are that framework's messages.
|
|
152
174
|
|
|
153
175
|
## Calling an agent
|
|
@@ -509,7 +531,9 @@ A DeepSeek agent's tool calls that need a person's approval (its `approval`
|
|
|
509
531
|
option) wait the same way, for the signal `approval`: see
|
|
510
532
|
[its README](src/deepseek/README.md#approvals). So do an OpenCode agent's
|
|
511
533
|
with `approval: true`, those its permission rules ask about: see
|
|
512
|
-
[its README](src/opencode/README.md#permissions).
|
|
534
|
+
[its README](src/opencode/README.md#permissions). So do an agentlib agent's
|
|
535
|
+
that its `approval` option names: see
|
|
536
|
+
[its README](src/agentlib/README.md#approvals-and-signals).
|
|
513
537
|
|
|
514
538
|
A wait ends with its agent's invocation: a cancelled or killed one leaves `pending`.
|
|
515
539
|
The waits are kept by the agent's companion, `<agent>.prebuilt.restate`
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { sdk as restate } from '@restatedev/sessions/internal';
|
|
2
|
+
import { type Messages, type MessagesRequest } from '../common.ts';
|
|
3
|
+
import { type Notifications, type Resolved, type Signal, type Steer, type Steered, type Waiting, type Watch } from '../companion.ts';
|
|
4
|
+
import type { Status, Stopped, Synced, SyncRequest } from '../object.ts';
|
|
5
|
+
import type { Definition, Options } from './options.ts';
|
|
6
|
+
/**
|
|
7
|
+
* An agent of agentlib: a virtual object `name` of the classic Restate SDK,
|
|
8
|
+
* with one conversation per key, in its state. `chat` answers a message with
|
|
9
|
+
* agentlib's run of the agent, journaled: after a crash, Restate replays it,
|
|
10
|
+
* so a model call or a tool call that finished is not made again. It has the
|
|
11
|
+
* views of every agent, and its companion, which `serve` serves beside it.
|
|
12
|
+
*
|
|
13
|
+
* A tool call that needs approval waits for the signal `approval`: `pending`
|
|
14
|
+
* lists it, and `signal` approves it (`true`, `"approved"`, `{ "approved":
|
|
15
|
+
* true }`) or denies it (anything else).
|
|
16
|
+
*/
|
|
17
|
+
export declare function agent<P extends string, O = string>(definition: Options<O> & Definition<P>): restate.VirtualObjectDefinition<P, {
|
|
18
|
+
chat: (ctx: restate.ObjectContext<restate.UntypedState>, input: string) => Promise<string | O>;
|
|
19
|
+
steered: (ctx: restate.ObjectContext<restate.UntypedState>, input: Steer) => Promise<void>;
|
|
20
|
+
erase: (ctx: restate.ObjectContext<restate.UntypedState>) => Promise<void>;
|
|
21
|
+
messages: (ctx: restate.ObjectSharedContext<restate.UntypedState>, input: MessagesRequest | null | undefined) => Promise<Messages>;
|
|
22
|
+
status: (ctx: restate.ObjectSharedContext<restate.UntypedState>) => Promise<Status>;
|
|
23
|
+
pending: (ctx: restate.ObjectSharedContext<restate.UntypedState>) => Promise<Waiting[]>;
|
|
24
|
+
signal: (ctx: restate.ObjectSharedContext<restate.UntypedState>, input: Signal) => Promise<Resolved>;
|
|
25
|
+
stop: (ctx: restate.ObjectSharedContext<restate.UntypedState>) => Promise<Stopped>;
|
|
26
|
+
clear: (ctx: restate.ObjectSharedContext<restate.UntypedState>) => Promise<Stopped>;
|
|
27
|
+
steer: (ctx: restate.ObjectSharedContext<restate.UntypedState>, input: string) => Promise<Steered>;
|
|
28
|
+
notifications: (ctx: restate.ObjectSharedContext<restate.UntypedState>) => Promise<Notifications>;
|
|
29
|
+
watch: (ctx: restate.ObjectSharedContext<restate.UntypedState>, input: Watch) => Promise<Notifications>;
|
|
30
|
+
sync: (ctx: restate.ObjectSharedContext<restate.UntypedState>, input: SyncRequest | null | undefined) => Promise<Synced>;
|
|
31
|
+
}>;
|
|
32
|
+
//# sourceMappingURL=agent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../../src/agentlib/agent.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,+BAA+B,CAAC;AAC/D,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,eAAe,EAAgB,MAAM,cAAc,CAAC;AACjF,OAAO,EACL,KAAK,aAAa,EAClB,KAAK,QAAQ,EACb,KAAK,MAAM,EACX,KAAK,KAAK,EACV,KAAK,OAAO,EACZ,KAAK,OAAO,EACZ,KAAK,KAAK,EAEX,MAAM,iBAAiB,CAAC;AACzB,OAAO,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAEzE,OAAO,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAkCxD;;;;;;;;;;GAUG;AACH,wBAAgB,KAAK,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,GAAG,MAAM,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC;;;;;;;;;;;;;;GAgCzF"}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// The agentlib agent's virtual object: `chat`, which answers with agentlib's
|
|
2
|
+
// run, and the handlers and views that every agent here has.
|
|
3
|
+
import { GuardrailError } from '@restatedev/agentlib';
|
|
4
|
+
import { sdk as restate } from '@restatedev/sessions/internal';
|
|
5
|
+
import { messageSerde } from '../common.js';
|
|
6
|
+
import { withCompanion, } from '../companion.js';
|
|
7
|
+
import { readMessages } from './messages.js';
|
|
8
|
+
import { Runtime } from './runtime.js';
|
|
9
|
+
import { callOwn, companionAt, CONVERSATION_META, emptyMeta, META, TAKEN, } from './state.js';
|
|
10
|
+
/**
|
|
11
|
+
* The options of every agentlib agent's object. An invocation starts with
|
|
12
|
+
* the small keys only, not the whole conversation (each message is a key of
|
|
13
|
+
* its own). It retries as every agent does: soon after a failure, then twice
|
|
14
|
+
* as long each time, up to 10 seconds apart, for weeks.
|
|
15
|
+
*/
|
|
16
|
+
const OPTIONS = {
|
|
17
|
+
state: { preload: [META, TAKEN, CONVERSATION_META] },
|
|
18
|
+
retryPolicy: {
|
|
19
|
+
initialInterval: 50,
|
|
20
|
+
exponentiationFactor: 2,
|
|
21
|
+
maxInterval: 10_000,
|
|
22
|
+
maxAttempts: 100_000,
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
/** A handler that takes a message as plain text or as a JSON string. */
|
|
26
|
+
const TEXT_INPUT = { input: messageSerde, accept: '*/*' };
|
|
27
|
+
/**
|
|
28
|
+
* An agent of agentlib: a virtual object `name` of the classic Restate SDK,
|
|
29
|
+
* with one conversation per key, in its state. `chat` answers a message with
|
|
30
|
+
* agentlib's run of the agent, journaled: after a crash, Restate replays it,
|
|
31
|
+
* so a model call or a tool call that finished is not made again. It has the
|
|
32
|
+
* views of every agent, and its companion, which `serve` serves beside it.
|
|
33
|
+
*
|
|
34
|
+
* A tool call that needs approval waits for the signal `approval`: `pending`
|
|
35
|
+
* lists it, and `signal` approves it (`true`, `"approved"`, `{ "approved":
|
|
36
|
+
* true }`) or denies it (anything else).
|
|
37
|
+
*/
|
|
38
|
+
export function agent(definition) {
|
|
39
|
+
const { name, description, background = false, options, metadata, ...agentOptions } = definition;
|
|
40
|
+
checkOneSdk(name);
|
|
41
|
+
const runtime = new Runtime(name, background, agentOptions);
|
|
42
|
+
const object = restate.object({
|
|
43
|
+
name,
|
|
44
|
+
description,
|
|
45
|
+
metadata,
|
|
46
|
+
handlers: {
|
|
47
|
+
chat: restate.handlers.object.exclusive(TEXT_INPUT, (ctx, text) => runtime.answer(ctx, text)),
|
|
48
|
+
steered: restate.handlers.object.exclusive({ ingressPrivate: true }, (ctx, message) => answerSteered(runtime, ctx, message)),
|
|
49
|
+
erase: restate.handlers.object.exclusive({ ingressPrivate: true }, erase),
|
|
50
|
+
messages: restate.handlers.object.shared(messages),
|
|
51
|
+
status: restate.handlers.object.shared(status),
|
|
52
|
+
pending: restate.handlers.object.shared(pending),
|
|
53
|
+
signal: restate.handlers.object.shared(signal),
|
|
54
|
+
stop: restate.handlers.object.shared(stop),
|
|
55
|
+
clear: restate.handlers.object.shared(clear),
|
|
56
|
+
steer: restate.handlers.object.shared(TEXT_INPUT, steer),
|
|
57
|
+
notifications: restate.handlers.object.shared(notifications),
|
|
58
|
+
watch: restate.handlers.object.shared(watch),
|
|
59
|
+
sync: restate.handlers.object.shared(sync),
|
|
60
|
+
},
|
|
61
|
+
options: { ...OPTIONS, ...options },
|
|
62
|
+
});
|
|
63
|
+
return withCompanion(object);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Checks that agentlib runs on the same copy of the Restate SDK as
|
|
67
|
+
* @restatedev/sessions. With two copies, agentlib's terminal errors are not
|
|
68
|
+
* the SDK's `TerminalError`, so a failed turn would look retryable.
|
|
69
|
+
*/
|
|
70
|
+
function checkOneSdk(name) {
|
|
71
|
+
if (Object.getPrototypeOf(GuardrailError) === restate.TerminalError)
|
|
72
|
+
return;
|
|
73
|
+
throw new Error(`${name}: @restatedev/agentlib runs on another copy of @restatedev/restate-sdk ` +
|
|
74
|
+
'than @restatedev/sessions. Install @restatedev/restate-sdk@1.18.2 ' +
|
|
75
|
+
'(the version @restatedev/sessions pins) beside them.');
|
|
76
|
+
}
|
|
77
|
+
// The private handlers.
|
|
78
|
+
/**
|
|
79
|
+
* A message steered to a turn (`steer`), which the companion sent here as it
|
|
80
|
+
* came, so that it keeps its place in the key's queue: answered as a turn of
|
|
81
|
+
* its own, unless the turn it was steered to took it.
|
|
82
|
+
*/
|
|
83
|
+
async function answerSteered(runtime, ctx, { id, text }) {
|
|
84
|
+
const taken = (await ctx.get(TAKEN)) ?? [];
|
|
85
|
+
if (!taken.includes(id)) {
|
|
86
|
+
await runtime.answer(ctx, text);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const left = taken.filter((other) => other !== id);
|
|
90
|
+
if (left.length > 0)
|
|
91
|
+
ctx.set(TAKEN, left);
|
|
92
|
+
else
|
|
93
|
+
ctx.clear(TAKEN);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* `clear`'s work, once the messages before it are answered: forgets the
|
|
97
|
+
* conversation. Its ids go on from where they got to.
|
|
98
|
+
*/
|
|
99
|
+
async function erase(ctx) {
|
|
100
|
+
const meta = (await ctx.get(META)) ?? emptyMeta();
|
|
101
|
+
const next = meta.chats + (meta.live?.n ?? 0);
|
|
102
|
+
ctx.clearAll();
|
|
103
|
+
const cleared = { chats: next, cursor: meta.cursor + 1, first: next };
|
|
104
|
+
ctx.set(META, cleared);
|
|
105
|
+
await companionAt(ctx).reset();
|
|
106
|
+
}
|
|
107
|
+
// The views: they run beside the turn being answered.
|
|
108
|
+
/** The conversation, a page at a time, or what is new or changed since a cursor. */
|
|
109
|
+
async function messages(ctx, request) {
|
|
110
|
+
return readMessages(ctx, request ?? {});
|
|
111
|
+
}
|
|
112
|
+
/** What the agent is doing: the message it answers, its waits, and the messages steered to it. */
|
|
113
|
+
async function status(ctx) {
|
|
114
|
+
return companionAt(ctx).status();
|
|
115
|
+
}
|
|
116
|
+
/** The waits of the agent's tools. */
|
|
117
|
+
async function pending(ctx) {
|
|
118
|
+
return companionAt(ctx).pending();
|
|
119
|
+
}
|
|
120
|
+
/** Resolves the wait of an id, or else the oldest wait for a name. */
|
|
121
|
+
async function signal(ctx, wanted) {
|
|
122
|
+
return companionAt(ctx).resolve(wanted);
|
|
123
|
+
}
|
|
124
|
+
/** Cancels the invocation of the message being answered, and says which one, if any. */
|
|
125
|
+
async function stop(ctx) {
|
|
126
|
+
const turn = (await companionAt(ctx).status()).answering;
|
|
127
|
+
if (!turn)
|
|
128
|
+
return { stopped: null };
|
|
129
|
+
ctx.cancel(restate.InvocationIdParser.fromString(turn.invocationId));
|
|
130
|
+
return { stopped: turn.invocationId };
|
|
131
|
+
}
|
|
132
|
+
/** Stops the message being answered, then forgets the conversation. */
|
|
133
|
+
async function clear(ctx) {
|
|
134
|
+
const stopped = await stop(ctx);
|
|
135
|
+
await callOwn(ctx, 'erase');
|
|
136
|
+
return stopped;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Sends a message to the turn being answered, which takes it at its next
|
|
140
|
+
* step. Otherwise it is answered as a turn of its own.
|
|
141
|
+
*/
|
|
142
|
+
async function steer(ctx, text) {
|
|
143
|
+
return companionAt(ctx).steer({ id: ctx.request().id, text });
|
|
144
|
+
}
|
|
145
|
+
/** The conversation's revision. */
|
|
146
|
+
async function notifications(ctx) {
|
|
147
|
+
return companionAt(ctx).notifications();
|
|
148
|
+
}
|
|
149
|
+
/** The revision, once there is one newer than `revision`, or at the timeout. */
|
|
150
|
+
async function watch(ctx, request) {
|
|
151
|
+
return companionAt(ctx).watch(request);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* The conversation since a cursor, the status and the revision. With
|
|
155
|
+
* `revision`, it waits until there is a newer one.
|
|
156
|
+
*/
|
|
157
|
+
async function sync(ctx, request) {
|
|
158
|
+
const { cursor = 0, revision, timeoutSeconds = 50, limit = 100, view } = request ?? {};
|
|
159
|
+
if (revision !== undefined)
|
|
160
|
+
await companionAt(ctx).watch({ revision, timeoutSeconds });
|
|
161
|
+
// The revision first: a change after it shows in the next sync.
|
|
162
|
+
const now = await companionAt(ctx).notifications();
|
|
163
|
+
const current = await companionAt(ctx).status();
|
|
164
|
+
// A call of its own: it reads the state as it is now, while this view
|
|
165
|
+
// read its state as it started.
|
|
166
|
+
const page = { cursor, limit };
|
|
167
|
+
if (view)
|
|
168
|
+
page.view = view;
|
|
169
|
+
const changed = await callOwn(ctx, 'messages', page);
|
|
170
|
+
return { revision: now.revision, status: current, ...changed };
|
|
171
|
+
}
|
|
172
|
+
//# sourceMappingURL=agent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../../src/agentlib/agent.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,6DAA6D;AAC7D,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,GAAG,IAAI,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AChC,MAAM,IAAI,GAAoB,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;IAChD,IAAI,IAAI;QAAE,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IAC3B,MAAM,OAAO,GAAG,MAAM,OAAO,CAAW,GAAG,EAAE,UAAU,EAAE,IAAI,CAAC,CAAC;IAC/D,OAAO,EAAE,QAAQ,EAAE,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,EAAE,CAAC;AACjE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/agentlib/index.ts"],"names":[],"mappings":"AAYA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC3C,YAAY,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACxD,YAAY,EAAE,eAAe,EAAE,YAAY,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// @restatedev/agents/agentlib: an agent of Restate's agentlib
|
|
2
|
+
// (@restatedev/agentlib) in a virtual object of the classic Restate SDK,
|
|
3
|
+
// journaled and replayed, with the handlers and views of every agent here.
|
|
4
|
+
//
|
|
5
|
+
// const assistant = agentlib.agent({ name: 'assistant', tools: [weather] });
|
|
6
|
+
// await serve(assistant);
|
|
7
|
+
//
|
|
8
|
+
// The modules: agent.ts (the object, its handlers and views), runtime.ts (a
|
|
9
|
+
// turn, from start to end), turn.ts (a turn's messages and the model's
|
|
10
|
+
// conversation), steering.ts, messages.ts (the messages clients read),
|
|
11
|
+
// model.ts, tools.ts (the tools of @restatedev/agents, made agentlib's), and
|
|
12
|
+
// state.ts (the object's state).
|
|
13
|
+
export { agent } from './agent.js';
|
|
14
|
+
export { DEFAULT_MODEL } from './model.js';
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/agentlib/index.ts"],"names":[],"mappings":"AAAA,8DAA8D;AAC9D,yEAAyE;AACzE,2EAA2E;AAC3E,EAAE;AACF,+EAA+E;AAC/E,4BAA4B;AAC5B,EAAE;AACF,4EAA4E;AAC5E,uEAAuE;AACvE,uEAAuE;AACvE,6EAA6E;AAC7E,iCAAiC;AACjC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Message } from '@restatedev/agentlib';
|
|
2
|
+
import { sdk as restate } from '@restatedev/sessions/internal';
|
|
3
|
+
import type { ChatMessage, Messages, MessagesRequest, Part } from '../common.ts';
|
|
4
|
+
type ToolPart = Extract<Part, {
|
|
5
|
+
type: 'tool';
|
|
6
|
+
}>;
|
|
7
|
+
type ToolResult = Extract<Message, {
|
|
8
|
+
role: 'tool';
|
|
9
|
+
}>['result'];
|
|
10
|
+
/** A message as clients read it, without its id (that is its key), as the agent stores it. */
|
|
11
|
+
export interface Entry {
|
|
12
|
+
role: ChatMessage['role'];
|
|
13
|
+
parts: Part[];
|
|
14
|
+
/** The cursor of the message's latest change. */
|
|
15
|
+
at: number;
|
|
16
|
+
/** It is its turn's message from the user (`view: 'summary'` shows these). */
|
|
17
|
+
prompt?: true;
|
|
18
|
+
/** It is its turn's answer (`view: 'summary'` shows these). */
|
|
19
|
+
answer?: true;
|
|
20
|
+
}
|
|
21
|
+
/** An entry before it is stored, without its cursor. */
|
|
22
|
+
export type Rendered = Omit<Entry, 'at'>;
|
|
23
|
+
/** Returns what a tool's result says, as a tool part's output. */
|
|
24
|
+
export declare function outputOf(result: ToolResult): Pick<ToolPart, 'output' | 'error'>;
|
|
25
|
+
/** A message steered to a turn, as agentlib notes it in the conversation. */
|
|
26
|
+
export declare function steeringNote(message: string): string;
|
|
27
|
+
/** Returns a user message's text: for agentlib's steering note, the steered message. */
|
|
28
|
+
export declare function userText(content: string): string;
|
|
29
|
+
/**
|
|
30
|
+
* Renders a turn's messages of agentlib as clients read them: each reply
|
|
31
|
+
* with its tool calls, each call with its result as its output. The first
|
|
32
|
+
* message is the turn's prompt, and the last reply its answer.
|
|
33
|
+
*/
|
|
34
|
+
export declare function render(messages: readonly Message[]): Rendered[];
|
|
35
|
+
type View = restate.ObjectSharedContext;
|
|
36
|
+
/** The `messages` view: a page of the conversation, or what changed since a cursor. */
|
|
37
|
+
export declare function readMessages(ctx: View, request?: MessagesRequest): Promise<Messages>;
|
|
38
|
+
export {};
|
|
39
|
+
//# sourceMappingURL=messages.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../../src/agentlib/messages.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,+BAA+B,CAAC;AAC/D,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAGjF,KAAK,QAAQ,GAAG,OAAO,CAAC,IAAI,EAAE;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC;AAChD,KAAK,UAAU,GAAG,OAAO,CAAC,OAAO,EAAE;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC,QAAQ,CAAC,CAAC;AAE/D,8FAA8F;AAC9F,MAAM,WAAW,KAAK;IACpB,IAAI,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IAC1B,KAAK,EAAE,IAAI,EAAE,CAAC;IACd,iDAAiD;IACjD,EAAE,EAAE,MAAM,CAAC;IACX,8EAA8E;IAC9E,MAAM,CAAC,EAAE,IAAI,CAAC;IACd,+DAA+D;IAC/D,MAAM,CAAC,EAAE,IAAI,CAAC;CACf;AAED,wDAAwD;AACxD,MAAM,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;AAEzC,kEAAkE;AAClE,wBAAgB,QAAQ,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,CAgB/E;AAED,6EAA6E;AAC7E,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAEpD;AAED,wFAAwF;AACxF,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAUhD;AAED;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,QAAQ,EAAE,SAAS,OAAO,EAAE,GAAG,QAAQ,EAAE,CAkC/D;AAKD,KAAK,IAAI,GAAG,OAAO,CAAC,mBAAmB,CAAC;AAmCxC,uFAAuF;AACvF,wBAAsB,YAAY,CAAC,GAAG,EAAE,IAAI,EAAE,OAAO,GAAE,eAAoB,GAAG,OAAO,CAAC,QAAQ,CAAC,CAQ9F"}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { sdk as restate } from '@restatedev/sessions/internal';
|
|
2
|
+
import { emptyMeta, entryKey, META } from './state.js';
|
|
3
|
+
/** Returns what a tool's result says, as a tool part's output. */
|
|
4
|
+
export function outputOf(result) {
|
|
5
|
+
switch (result.status) {
|
|
6
|
+
case 'success': {
|
|
7
|
+
const { output } = result;
|
|
8
|
+
return { output: typeof output === 'string' ? output : JSON.stringify(output) };
|
|
9
|
+
}
|
|
10
|
+
case 'error':
|
|
11
|
+
return { output: `Error: ${result.message}`, error: true };
|
|
12
|
+
case 'denied':
|
|
13
|
+
return { output: `Denied: ${result.message}`, error: true };
|
|
14
|
+
case 'cancelled':
|
|
15
|
+
return { output: `Cancelled: ${result.message}`, error: true };
|
|
16
|
+
case 'pending':
|
|
17
|
+
default:
|
|
18
|
+
return {};
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** A message steered to a turn, as agentlib notes it in the conversation. */
|
|
22
|
+
export function steeringNote(message) {
|
|
23
|
+
return JSON.stringify({ source: 'agent-runtime', type: 'steering', message });
|
|
24
|
+
}
|
|
25
|
+
/** Returns a user message's text: for agentlib's steering note, the steered message. */
|
|
26
|
+
export function userText(content) {
|
|
27
|
+
if (!content.startsWith('{'))
|
|
28
|
+
return content;
|
|
29
|
+
try {
|
|
30
|
+
const note = JSON.parse(content);
|
|
31
|
+
const steering = note.source === 'agent-runtime' && note.type === 'steering';
|
|
32
|
+
if (steering && typeof note.message === 'string')
|
|
33
|
+
return note.message;
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
// Not JSON: the user's own text.
|
|
37
|
+
}
|
|
38
|
+
return content;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Renders a turn's messages of agentlib as clients read them: each reply
|
|
42
|
+
* with its tool calls, each call with its result as its output. The first
|
|
43
|
+
* message is the turn's prompt, and the last reply its answer.
|
|
44
|
+
*/
|
|
45
|
+
export function render(messages) {
|
|
46
|
+
const rendered = [];
|
|
47
|
+
const callParts = new Map();
|
|
48
|
+
for (const message of messages) {
|
|
49
|
+
switch (message.role) {
|
|
50
|
+
case 'user':
|
|
51
|
+
rendered.push({ role: 'user', parts: [{ type: 'text', text: userText(message.content) }] });
|
|
52
|
+
break;
|
|
53
|
+
case 'assistant': {
|
|
54
|
+
const parts = [];
|
|
55
|
+
if (message.content)
|
|
56
|
+
parts.push({ type: 'text', text: message.content });
|
|
57
|
+
for (const call of message.calls ?? []) {
|
|
58
|
+
const part = { type: 'tool', id: call.id, name: call.name, input: call.input };
|
|
59
|
+
callParts.set(call.id, part);
|
|
60
|
+
parts.push(part);
|
|
61
|
+
}
|
|
62
|
+
if (parts.length === 0)
|
|
63
|
+
parts.push({ type: 'text', text: '' });
|
|
64
|
+
rendered.push({ role: 'assistant', parts });
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
case 'tool': {
|
|
68
|
+
const part = callParts.get(message.callId);
|
|
69
|
+
if (part)
|
|
70
|
+
Object.assign(part, outputOf(message.result));
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
const first = rendered[0];
|
|
76
|
+
if (first?.role === 'user')
|
|
77
|
+
first.prompt = true;
|
|
78
|
+
const last = rendered.at(-1);
|
|
79
|
+
if (last?.role === 'assistant')
|
|
80
|
+
last.answer = true;
|
|
81
|
+
return rendered;
|
|
82
|
+
}
|
|
83
|
+
/** How many messages one read of the `messages` view fetches at once. */
|
|
84
|
+
const BATCH = 25;
|
|
85
|
+
/**
|
|
86
|
+
* Reads the entry `id`. At runtime, `get` returns a RestatePromise, although
|
|
87
|
+
* its type says Promise.
|
|
88
|
+
*/
|
|
89
|
+
function readEntry(ctx, id) {
|
|
90
|
+
return ctx.get(entryKey(id));
|
|
91
|
+
}
|
|
92
|
+
/** Reads the entries of `ids` at once. A missing entry is `null`. */
|
|
93
|
+
function readEntries(ctx, ids) {
|
|
94
|
+
return restate.RestatePromise.all(ids.map((id) => readEntry(ctx, id)));
|
|
95
|
+
}
|
|
96
|
+
/** Yields the ids from `from` down to `to` (included), newest first, a batch at a time. */
|
|
97
|
+
function* batches(from, to) {
|
|
98
|
+
for (let id = from; id >= to; id -= BATCH) {
|
|
99
|
+
const size = Math.min(BATCH, id - to + 1);
|
|
100
|
+
yield Array.from({ length: size }, (_, i) => id - i);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const chatMessage = (id, entry) => ({
|
|
104
|
+
id,
|
|
105
|
+
role: entry.role,
|
|
106
|
+
parts: entry.parts,
|
|
107
|
+
});
|
|
108
|
+
/** Whether `entry` belongs in the view that `request` asks for. */
|
|
109
|
+
function inView(request, entry) {
|
|
110
|
+
if (request.view !== 'summary')
|
|
111
|
+
return true;
|
|
112
|
+
return entry.prompt === true || entry.answer === true;
|
|
113
|
+
}
|
|
114
|
+
/** The `messages` view: a page of the conversation, or what changed since a cursor. */
|
|
115
|
+
export async function readMessages(ctx, request = {}) {
|
|
116
|
+
const meta = (await ctx.get(META)) ?? emptyMeta();
|
|
117
|
+
const page = request.cursor === undefined
|
|
118
|
+
? await pageBefore(ctx, meta, request)
|
|
119
|
+
: await changedSince(ctx, meta, request);
|
|
120
|
+
if (meta.first !== undefined)
|
|
121
|
+
page.first = meta.first;
|
|
122
|
+
return page;
|
|
123
|
+
}
|
|
124
|
+
/** The id after the conversation's last message (the turn being answered included). */
|
|
125
|
+
const endOf = (meta) => meta.chats + (meta.live?.n ?? 0);
|
|
126
|
+
/** The latest messages before `request.before` (by default, the end), at most `request.limit`. */
|
|
127
|
+
async function pageBefore(ctx, meta, request) {
|
|
128
|
+
const first = meta.first ?? 0;
|
|
129
|
+
const limit = request.limit ?? 50;
|
|
130
|
+
const messages = [];
|
|
131
|
+
// The oldest id read so far: there are more if older ids are left.
|
|
132
|
+
let oldest = Math.min(request.before ?? endOf(meta), endOf(meta));
|
|
133
|
+
reading: for (const ids of batches(oldest - 1, first)) {
|
|
134
|
+
const entries = await readEntries(ctx, ids);
|
|
135
|
+
for (const [i, id] of ids.entries()) {
|
|
136
|
+
if (messages.length >= limit)
|
|
137
|
+
break reading;
|
|
138
|
+
oldest = id;
|
|
139
|
+
const entry = entries[i];
|
|
140
|
+
if (entry && inView(request, entry))
|
|
141
|
+
messages.push(chatMessage(id, entry));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return { messages: messages.toReversed(), more: oldest > first, cursor: meta.cursor };
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* The messages new or changed since `request.cursor`, in the order they
|
|
148
|
+
* changed, at most `request.limit`. An ended turn's messages are rewritten
|
|
149
|
+
* in order as it ends, so the scan stops at the first message, older than
|
|
150
|
+
* the turn being answered, that did not change.
|
|
151
|
+
*/
|
|
152
|
+
async function changedSince(ctx, meta, request) {
|
|
153
|
+
const cursor = request.cursor ?? 0;
|
|
154
|
+
const limit = request.limit ?? 50;
|
|
155
|
+
const liveFrom = meta.live?.from ?? endOf(meta);
|
|
156
|
+
const changed = [];
|
|
157
|
+
scanning: for (const ids of batches(endOf(meta) - 1, meta.first ?? 0)) {
|
|
158
|
+
const entries = await readEntries(ctx, ids);
|
|
159
|
+
for (const [i, id] of ids.entries()) {
|
|
160
|
+
const entry = entries[i];
|
|
161
|
+
if (!entry)
|
|
162
|
+
continue;
|
|
163
|
+
if (entry.at > cursor)
|
|
164
|
+
changed.push([id, entry]);
|
|
165
|
+
else if (id < liveFrom)
|
|
166
|
+
break scanning;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
const ordered = changed
|
|
170
|
+
.toSorted(([, a], [, b]) => a.at - b.at)
|
|
171
|
+
.filter(([, entry]) => inView(request, entry));
|
|
172
|
+
const page = ordered.slice(0, limit);
|
|
173
|
+
const more = ordered.length > limit;
|
|
174
|
+
const next = more ? page.at(-1)[1].at : meta.cursor;
|
|
175
|
+
return { messages: page.map(([id, entry]) => chatMessage(id, entry)), more, cursor: next };
|
|
176
|
+
}
|
|
177
|
+
//# sourceMappingURL=messages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages.js","sourceRoot":"","sources":["../../src/agentlib/messages.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,+BAA+B,CAAC;AAE/D,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,IAAI,EAAa,MAAM,YAAY,CAAC;AAoBlE,kEAAkE;AAClE,MAAM,UAAU,QAAQ,CAAC,MAAkB;IACzC,QAAQ,MAAM,CAAC,MAAM,EAAE,CAAC;QACtB,KAAK,SAAS,EAAE,CAAC;YACf,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;YAC1B,OAAO,EAAE,MAAM,EAAE,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,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,CAAC,CAAC;IACrC,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,GAAG,KAAK,CAAC;IACpC,MAAM,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC;IACrD,OAAO,EAAE,QAAQ,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,WAAW,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;AAC7F,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { CompactionPolicy, Model } from '@restatedev/agentlib';
|
|
2
|
+
import { sdk as restate } from '@restatedev/sessions/internal';
|
|
3
|
+
/** The model of an agent that names none, without MODEL either: OpenAI's (OPENAI_API_KEY). */
|
|
4
|
+
export declare const DEFAULT_MODEL = "openai/gpt-6-sol";
|
|
5
|
+
/**
|
|
6
|
+
* Returns what loads agent `agent`'s model on its first turn: `model` itself,
|
|
7
|
+
* or the model it names (`provider/model-id`, else MODEL, else
|
|
8
|
+
* {@link DEFAULT_MODEL}) on the AI SDK. A name it cannot take throws at once.
|
|
9
|
+
*/
|
|
10
|
+
export declare function modelLoader(agent: string, model: string | Model | undefined): () => Promise<Model>;
|
|
11
|
+
/**
|
|
12
|
+
* Whether `error`, thrown by a model, is its provider's refusal that a retry
|
|
13
|
+
* does not change: a request answered with a 4xx (a wrong API key, an unknown
|
|
14
|
+
* model, a rejected request), or a missing API key. Only a plain
|
|
15
|
+
* `TerminalError` qualifies: agentlib's own errors (an overflow, a protocol
|
|
16
|
+
* error) and cancellations are subclasses.
|
|
17
|
+
*/
|
|
18
|
+
export declare function refused(error: unknown): error is restate.TerminalError;
|
|
19
|
+
/** Wraps `model` so that its provider's refusals end the turn with a 424, as for every agent. */
|
|
20
|
+
export declare function refusing(model: Model): Model;
|
|
21
|
+
/** Returns `schema` (a JSON schema) without the bounds of its numbers, at any depth. */
|
|
22
|
+
export declare function withoutBounds(schema: unknown): unknown;
|
|
23
|
+
/**
|
|
24
|
+
* Checks that agent `agent`'s `history` bounds are positive integers. agentlib
|
|
25
|
+
* checks them only when a turn compacts, with an error that a retry would meet
|
|
26
|
+
* again and again.
|
|
27
|
+
*/
|
|
28
|
+
export declare function checkHistory(agent: string, history: CompactionPolicy): void;
|
|
29
|
+
//# sourceMappingURL=model.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../src/agentlib/model.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,gBAAgB,EAAE,KAAK,EAAgB,MAAM,sBAAsB,CAAC;AAElF,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,+BAA+B,CAAC;AAG/D,8FAA8F;AAC9F,eAAO,MAAM,aAAa,qBAAqB,CAAC;AAUhD;;;;GAIG;AACH,wBAAgB,WAAW,CACzB,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,GAAG,KAAK,GAAG,SAAS,GAChC,MAAM,OAAO,CAAC,KAAK,CAAC,CA0BtB;AAmBD;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,OAAO,CAAC,aAAa,CAKtE;AAED,iGAAiG;AACjG,wBAAgB,QAAQ,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAU5C;AAUD,wFAAwF;AACxF,wBAAgB,aAAa,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAUtD;AAkBD;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,QASpE"}
|