@restatedev/agents 0.3.0 → 0.4.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 +89 -49
- package/dist/calls.d.ts +29 -5
- package/dist/calls.d.ts.map +1 -1
- package/dist/calls.js +77 -10
- package/dist/calls.js.map +1 -1
- package/dist/claude/index.d.ts +36 -7
- package/dist/claude/index.d.ts.map +1 -1
- package/dist/claude/index.js +232 -151
- package/dist/claude/index.js.map +1 -1
- package/dist/client.d.ts +12 -3
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js.map +1 -1
- package/dist/common.d.ts +151 -70
- package/dist/common.d.ts.map +1 -1
- package/dist/common.js +246 -204
- package/dist/common.js.map +1 -1
- package/dist/conversation.d.ts +466 -0
- package/dist/conversation.d.ts.map +1 -0
- package/dist/conversation.js +1247 -0
- package/dist/conversation.js.map +1 -0
- package/dist/deepseek/index.d.ts +38 -5
- package/dist/deepseek/index.d.ts.map +1 -1
- package/dist/deepseek/index.js +322 -106
- package/dist/deepseek/index.js.map +1 -1
- package/dist/deepseek/persistence.d.ts +9 -3
- package/dist/deepseek/persistence.d.ts.map +1 -1
- package/dist/deepseek/persistence.js +12 -6
- package/dist/deepseek/persistence.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -3
- package/dist/index.js.map +1 -1
- package/dist/langgraph/checkpointer.d.ts +16 -2
- package/dist/langgraph/checkpointer.d.ts.map +1 -1
- package/dist/langgraph/checkpointer.js +133 -26
- package/dist/langgraph/checkpointer.js.map +1 -1
- package/dist/langgraph/context.d.ts +10 -0
- package/dist/langgraph/context.d.ts.map +1 -0
- package/dist/langgraph/context.js +21 -0
- package/dist/langgraph/context.js.map +1 -0
- package/dist/langgraph/errors.d.ts +16 -0
- package/dist/langgraph/errors.d.ts.map +1 -0
- package/dist/langgraph/errors.js +30 -0
- package/dist/langgraph/errors.js.map +1 -0
- package/dist/langgraph/graph.d.ts +25 -0
- package/dist/langgraph/graph.d.ts.map +1 -0
- package/dist/langgraph/graph.js +346 -0
- package/dist/langgraph/graph.js.map +1 -0
- package/dist/langgraph/index.d.ts +65 -12
- package/dist/langgraph/index.d.ts.map +1 -1
- package/dist/langgraph/index.js +227 -171
- package/dist/langgraph/index.js.map +1 -1
- package/dist/langgraph/nodes.d.ts +70 -0
- package/dist/langgraph/nodes.d.ts.map +1 -0
- package/dist/langgraph/nodes.js +145 -0
- package/dist/langgraph/nodes.js.map +1 -0
- package/dist/messages.d.ts +18 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +182 -0
- package/dist/messages.js.map +1 -0
- package/dist/object.d.ts +65 -23
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +121 -35
- package/dist/object.js.map +1 -1
- package/dist/output.d.ts +15 -7
- package/dist/output.d.ts.map +1 -1
- package/dist/output.js +44 -26
- package/dist/output.js.map +1 -1
- package/dist/pi/index.d.ts +39 -11
- package/dist/pi/index.d.ts.map +1 -1
- package/dist/pi/index.js +281 -77
- package/dist/pi/index.js.map +1 -1
- package/dist/pi/storage.d.ts +29 -4
- package/dist/pi/storage.d.ts.map +1 -1
- package/dist/pi/storage.js +18 -6
- package/dist/pi/storage.js.map +1 -1
- package/dist/signals.d.ts +74 -4
- package/dist/signals.d.ts.map +1 -1
- package/dist/signals.js +144 -15
- package/dist/signals.js.map +1 -1
- package/dist/steering.d.ts +31 -0
- package/dist/steering.d.ts.map +1 -0
- package/dist/steering.js +45 -0
- package/dist/steering.js.map +1 -0
- package/dist/tools.d.ts +28 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +34 -5
- package/dist/tools.js.map +1 -1
- package/package.json +33 -37
- package/src/calls.ts +107 -15
- package/src/claude/README.md +41 -20
- package/src/claude/index.ts +266 -178
- package/src/client.ts +14 -3
- package/src/common.ts +317 -232
- package/src/conversation.ts +1527 -0
- package/src/deepseek/README.md +78 -11
- package/src/deepseek/index.ts +384 -137
- package/src/deepseek/persistence.ts +11 -8
- package/src/index.ts +13 -4
- package/src/langgraph/README.md +115 -25
- package/src/langgraph/checkpointer.ts +168 -28
- package/src/langgraph/context.ts +27 -0
- package/src/langgraph/errors.ts +26 -0
- package/src/langgraph/graph.ts +396 -0
- package/src/langgraph/index.ts +326 -181
- package/src/langgraph/nodes.ts +243 -0
- package/src/messages.ts +244 -0
- package/src/object.ts +167 -38
- package/src/output.ts +52 -31
- package/src/pi/README.md +18 -3
- package/src/pi/index.ts +346 -78
- package/src/pi/storage.ts +20 -7
- package/src/signals.ts +225 -19
- package/src/steering.ts +67 -0
- package/src/tools.ts +61 -8
- package/dist/claude/conversation.d.ts +0 -33
- package/dist/claude/conversation.d.ts.map +0 -1
- package/dist/claude/conversation.js +0 -95
- package/dist/claude/conversation.js.map +0 -1
- package/dist/log.d.ts +0 -29
- package/dist/log.d.ts.map +0 -1
- package/dist/log.js +0 -146
- package/dist/log.js.map +0 -1
- package/dist/opencode/db.d.ts +0 -40
- package/dist/opencode/db.d.ts.map +0 -1
- package/dist/opencode/db.js +0 -161
- package/dist/opencode/db.js.map +0 -1
- package/dist/opencode/index.d.ts +0 -100
- package/dist/opencode/index.d.ts.map +0 -1
- package/dist/opencode/index.js +0 -436
- package/dist/opencode/index.js.map +0 -1
- package/src/claude/conversation.ts +0 -114
- package/src/log.ts +0 -194
- package/src/opencode/README.md +0 -125
- package/src/opencode/db.ts +0 -223
- package/src/opencode/index.ts +0 -559
package/dist/object.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// Virtual objects set up for SQL and agents.
|
|
2
2
|
import * as restate from '@restatedev/sessions';
|
|
3
3
|
import { reset } from '@restatedev/sessions/internal';
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
4
|
+
import { BUDGET, chatOf, chatTurn, checkWindow, isAgent, ITEMS, messageSerde, publish, refusalOf, WINDOW, } from './common.js';
|
|
5
|
+
import { compact, nextStart, restart, SCHEMA, settle, steerStored } from './conversation.js';
|
|
6
6
|
import { signals, signalsOf, withCompanion, } from './signals.js';
|
|
7
7
|
/**
|
|
8
8
|
* The options of every object here, beside a session object's: retries for
|
|
@@ -45,7 +45,7 @@ const WAITS = { inactivityTimeout: { minutes: 5 } };
|
|
|
45
45
|
* waits, `status`, what the object is doing, `stop`, which stops the message
|
|
46
46
|
* it answers, and `notifications` and `watch`, the revision of its
|
|
47
47
|
* conversation, and a wait for a newer one (an agent's
|
|
48
|
-
* has `clear` and `sync` too: {@link
|
|
48
|
+
* has `clear` and `sync` too: {@link FOLLOWED}). Each asks the object's
|
|
49
49
|
* `signals` companion, which knows what the object does: they run while the
|
|
50
50
|
* object's handler works.
|
|
51
51
|
*/
|
|
@@ -70,19 +70,21 @@ const BUILT_IN = {
|
|
|
70
70
|
};
|
|
71
71
|
/**
|
|
72
72
|
* The views that agents (an object with a `messages` view) have beside
|
|
73
|
-
* those of every object: `clear`, which forgets the conversation;
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
* once
|
|
73
|
+
* those of every object: `clear`, which forgets the conversation; `steer`,
|
|
74
|
+
* which sends a message while a turn is answered (for an agent whose `chat`
|
|
75
|
+
* a framework's `chat()` made: `chatHandler`'s, which answers it as a turn
|
|
76
|
+
* of its own if no turn takes it); and `sync`, the conversation since
|
|
77
|
+
* `cursor` (by default, all of it), what the agent is doing, and the
|
|
78
|
+
* revision, in one request; with `revision`, once there is a newer one, or
|
|
79
|
+
* at the timeout. A client follows the conversation by sending each
|
|
80
|
+
* answer's `revision` and `cursor` back, at once while it has `more`.
|
|
79
81
|
*/
|
|
80
|
-
const followed = (
|
|
82
|
+
const followed = (steerable) => ({
|
|
81
83
|
/**
|
|
82
84
|
* Forget the conversation: stop the message being answered (as `stop`),
|
|
83
85
|
* then empty the key's database, and what its `signals` hold (waits and
|
|
84
|
-
* kept signals). The
|
|
85
|
-
*
|
|
86
|
+
* kept signals). The conversation's ids and cursors go on (see
|
|
87
|
+
* conversation.ts), so a follower reads on, and drops the messages before `first`.
|
|
86
88
|
* A message sent before it is answered first, then forgotten too; one sent
|
|
87
89
|
* after starts anew.
|
|
88
90
|
*/
|
|
@@ -100,43 +102,111 @@ const followed = (messages) => ({
|
|
|
100
102
|
return stop;
|
|
101
103
|
},
|
|
102
104
|
},
|
|
105
|
+
/**
|
|
106
|
+
* Send a message to the agent while it answers another: the turn being
|
|
107
|
+
* answered takes it at its next step (once the model's tool calls have
|
|
108
|
+
* their results), if its framework takes steered messages and it reaches
|
|
109
|
+
* one; otherwise the message is answered as a turn of its own, after the
|
|
110
|
+
* messages before it, in the order it was sent. With no message being
|
|
111
|
+
* answered, it starts a turn. Either way it is answered once: send it with
|
|
112
|
+
* an idempotency key, so that a retried request is the same message. It
|
|
113
|
+
* takes the message as plain text or as a JSON string, as `chat` does.
|
|
114
|
+
*/
|
|
115
|
+
steer: {
|
|
116
|
+
input: messageSerde,
|
|
117
|
+
accept: '*/*',
|
|
118
|
+
handler: async (ctx, text) => {
|
|
119
|
+
if (!steerable) {
|
|
120
|
+
throw new restate.TerminalError(`${ctx.request.target.service} takes no steered messages: its \`chat\` handler is no framework's \`chat()\` (chatHandler)`, { errorCode: 400 });
|
|
121
|
+
}
|
|
122
|
+
return companion(ctx, 'steer').steer({ id: ctx.request.id, text });
|
|
123
|
+
},
|
|
124
|
+
},
|
|
103
125
|
sync: {
|
|
104
126
|
...WAITS,
|
|
105
|
-
handler: async (ctx, { cursor = 0, revision, timeoutSeconds = 50, limit = 100 } = {}) => {
|
|
127
|
+
handler: async (ctx, { cursor = 0, revision, timeoutSeconds = 50, limit = 100, view } = {}) => {
|
|
106
128
|
if (revision !== undefined)
|
|
107
129
|
await companion(ctx, 'watch').watch({ revision, timeoutSeconds });
|
|
108
130
|
// The revision first: a change after it shows in the next sync. Then
|
|
109
|
-
// what the object does, and its conversation,
|
|
131
|
+
// what the object does, and its conversation, in an invocation of its
|
|
132
|
+
// own, which starts after the revision: a view reads the database as
|
|
133
|
+
// committed when it started, and this one started before the revision
|
|
134
|
+
// (by the whole wait, when it waited). So the page has every change the
|
|
135
|
+
// revision counts.
|
|
110
136
|
const now = await companion(ctx).notifications();
|
|
111
137
|
const status = await companion(ctx).status();
|
|
112
|
-
|
|
138
|
+
const request = { cursor, limit, ...(view ? { view } : {}) };
|
|
139
|
+
const page = (await ctx.rpc({
|
|
140
|
+
service: ctx.request.target.service,
|
|
141
|
+
key: ctx.key,
|
|
142
|
+
handler: 'messages',
|
|
143
|
+
input: request,
|
|
144
|
+
}));
|
|
145
|
+
return { revision: now.revision, status, ...page };
|
|
113
146
|
},
|
|
114
147
|
},
|
|
115
148
|
});
|
|
149
|
+
/** The views of every agent: {@link followed}'s. */
|
|
150
|
+
const FOLLOWED = followed(false);
|
|
116
151
|
/**
|
|
117
152
|
* The agents' handlers that only their own views call (Restate's ingress
|
|
118
153
|
* refuses them), and their types leave out: `erase`, `clear`'s work, once
|
|
119
154
|
* the handlers before it are done. It empties the key's database and its
|
|
120
|
-
* signals', and the
|
|
155
|
+
* signals', and the conversation's ids and cursors go on where they got to
|
|
156
|
+
* (conversation.ts).
|
|
121
157
|
*/
|
|
122
|
-
const
|
|
158
|
+
const internal = (items) => ({
|
|
123
159
|
erase: {
|
|
124
160
|
ingressPrivate: true,
|
|
125
161
|
handler: async (ctx) => {
|
|
126
|
-
|
|
162
|
+
// Read first, which also makes the reset apply the conversation's schema again.
|
|
163
|
+
await SCHEMA.ensure(ctx.db);
|
|
164
|
+
const start = await nextStart(ctx.db);
|
|
127
165
|
await ctx.db.transaction(async (tx) => {
|
|
128
166
|
await reset(tx);
|
|
129
|
-
await
|
|
167
|
+
await restart(tx, start);
|
|
130
168
|
});
|
|
131
|
-
await cleared(ctx);
|
|
132
169
|
await ctx.rpc(signals, signalsOf(ctx), { idempotencyKey: `${ctx.request.id}:reset` }).reset();
|
|
133
170
|
},
|
|
134
171
|
},
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
172
|
+
/**
|
|
173
|
+
* Close the latest turn, unanswered, if invocation `invocationId` ran it
|
|
174
|
+
* and is gone: the companion sends this once it finds the turn's
|
|
175
|
+
* invocation ended without answering (killed, say), so that the
|
|
176
|
+
* conversation shows the turn closed before the next message. Whether it
|
|
177
|
+
* closed it.
|
|
178
|
+
*/
|
|
179
|
+
settle: {
|
|
180
|
+
ingressPrivate: true,
|
|
181
|
+
handler: async (ctx, invocationId) => {
|
|
182
|
+
if (!items || !(await settle(ctx, items, invocationId)))
|
|
183
|
+
return false;
|
|
184
|
+
// The companion published as it closed the turn, before this stored how it ended: publish that too.
|
|
185
|
+
await publish(ctx);
|
|
186
|
+
return true;
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
/** Summarize the earliest turns, past the agent's budget. Whether it did. */
|
|
190
|
+
compact: {
|
|
191
|
+
ingressPrivate: true,
|
|
192
|
+
handler: async (ctx) => (items ? compact(ctx, items) : false),
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
/**
|
|
196
|
+
* The private `steered` of an agent whose `chat` is `chat` (`chatHandler`'s):
|
|
197
|
+
* it answers a message sent to `steer` as a turn of its own, with that
|
|
198
|
+
* `chat`, unless the turn it was sent to took it. The companion sends it as
|
|
199
|
+
* the message is sent, so that it keeps its place in the key's queue: it runs
|
|
200
|
+
* once the messages before it are answered.
|
|
201
|
+
*/
|
|
202
|
+
const steeredOf = ({ answer, steers }) => ({
|
|
203
|
+
ingressPrivate: true,
|
|
204
|
+
explicitCancellation: true,
|
|
205
|
+
handler: async (ctx, { id, text }) => {
|
|
206
|
+
if (await steerStored(ctx.db, id))
|
|
207
|
+
return;
|
|
208
|
+
await chatTurn(ctx, text, answer, steers);
|
|
209
|
+
},
|
|
140
210
|
});
|
|
141
211
|
/**
|
|
142
212
|
* A Restate virtual object with a SQL database per key, `ctx.db`: a
|
|
@@ -162,19 +232,19 @@ const startAfter = ({ messages: [latest], cursor, first = 0 }) => ({
|
|
|
162
232
|
* `schema`, optional: tables of your own, beside the agent's (which are its
|
|
163
233
|
* own business), made in each key's database as a session object makes
|
|
164
234
|
* them. Any name is yours but those starting with `restate_`, the agent's
|
|
165
|
-
* (`restate_pi_tasks`, `
|
|
166
|
-
* OpenCode's tables are in a database of their own.
|
|
235
|
+
* (`restate_pi_tasks`, `restate_agent_turns`, `restate_agent_items`...).
|
|
167
236
|
*
|
|
168
237
|
* `options` and `metadata` add to (or replace) the ones set here, such as
|
|
169
238
|
* `options: { retryPolicy: ... }`.
|
|
170
239
|
*/
|
|
171
240
|
export const sessionObject = (definition) => {
|
|
172
241
|
const { name, description, schema, background, handlers = {}, views = {}, metadata, options } = definition;
|
|
173
|
-
const
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
242
|
+
const { window = WINDOW, budget = BUDGET, summarize } = definition;
|
|
243
|
+
checkWindow(window);
|
|
244
|
+
if (!(Number.isInteger(budget) && budget >= 0)) {
|
|
245
|
+
throw new Error(`${name}: budget must be a number of tokens (0 or more), not ${JSON.stringify(budget)}`);
|
|
246
|
+
}
|
|
247
|
+
const own = [...Object.keys(BUILT_IN), ...Object.keys(FOLLOWED), ...Object.keys(internal(undefined)), 'steered'];
|
|
178
248
|
if ('messages' in handlers) {
|
|
179
249
|
throw new Error(`${name}: \`messages\` is a view: put it in \`views\`, so that the object is an agent`);
|
|
180
250
|
}
|
|
@@ -189,16 +259,32 @@ export const sessionObject = (definition) => {
|
|
|
189
259
|
]));
|
|
190
260
|
const messages = views.messages;
|
|
191
261
|
const page = (typeof messages === 'function' ? messages : messages?.handler);
|
|
192
|
-
|
|
262
|
+
// A `chat` that `chatHandler` made: its answer answers the steered messages no turn took (`steered`).
|
|
263
|
+
const chatEntry = handlers.chat;
|
|
264
|
+
const chat = typeof chatEntry === 'object' || typeof chatEntry === 'function' ? chatOf(chatEntry) : undefined;
|
|
265
|
+
const agent = page === undefined ? {} : followed(chat !== undefined);
|
|
266
|
+
// A framework on the conversation of turns: its view carries its items.
|
|
267
|
+
const items = (page && ITEMS.of(page));
|
|
268
|
+
// Its `compact` summarizes: without the conversation of turns, it has none, and nothing would.
|
|
269
|
+
if (summarize && !items) {
|
|
270
|
+
throw new Error(`${name}: \`summarize\` needs the conversation of turns: a framework's \`messages\` view (\`claude.messages()\`, say)`);
|
|
271
|
+
}
|
|
272
|
+
const { erase, ...stored } = internal(items);
|
|
273
|
+
const steering = chat ? { steered: steeredOf(chat) } : {};
|
|
274
|
+
const privates = messages === undefined
|
|
275
|
+
? {}
|
|
276
|
+
: items
|
|
277
|
+
? { erase, ...stored, ...steering }
|
|
278
|
+
: { erase, ...steering };
|
|
193
279
|
return withCompanion(isAgent(restate.sessionObject({
|
|
194
280
|
name,
|
|
195
281
|
description,
|
|
196
282
|
schema,
|
|
197
|
-
handlers: { ...
|
|
283
|
+
handlers: { ...privates, ...yours },
|
|
198
284
|
views: { ...BUILT_IN, ...agent, ...views },
|
|
199
285
|
metadata,
|
|
200
286
|
options: { ...OPTIONS, ...options },
|
|
201
|
-
}), { background,
|
|
287
|
+
}), { background, items, window, budget, summarize }));
|
|
202
288
|
};
|
|
203
289
|
/** `entry`, but a model provider's refusal it fails with ends its invocation, as `chat`'s does. */
|
|
204
290
|
function answering(entry) {
|
package/dist/object.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"object.js","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,OAAO,KAAK,OAAO,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,KAAK,EAAE,MAAM,+BAA+B,CAAC;AACtD,OAAO,
|
|
1
|
+
{"version":3,"file":"object.js","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,OAAO,KAAK,OAAO,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,KAAK,EAAE,MAAM,+BAA+B,CAAC;AACtD,OAAO,EAEL,MAAM,EAEN,MAAM,EAE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AAC;QACpB,IAAI;QACJ,WAAW;QACX,MAAM;QACN,QAAQ,EAAE,EAAE,GAAG,QAAQ,EAAE,GAAG,KAAK,EAAE;QACnC,KAAK,EAAE,EAAE,GAAG,QAAQ,EAAE,GAAG,KAAK,EAAE,GAAG,KAAK,EAAE;QAC1C,QAAQ;QACR,OAAO,EAAE,EAAE,GAAG,OAAO,EAAE,GAAG,OAAO,EAAE;KACpC,CAAC,EACF,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,CACjD,CACF,CAAC;AACJ,CAAC,CAAC;AAEF,mGAAmG;AACnG,SAAS,SAAS,CAAC,KAA4C;IAC7D,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,EAAE,GAAG,OAAO,KAAK,KAAK,UAAU,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC;IACzF,OAAO;QACL,GAAG,OAAO;QACV,OAAO,EAAE,KAAK,EAAE,GAA2B,EAAE,KAAc,EAAE,EAAE;YAC7D,IAAI,CAAC;gBACH,OAAO,MAAM,OAAO,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YACnC,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,MAAM,SAAS,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,KAAK,CAAC;YACvC,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/output.d.ts
CHANGED
|
@@ -2,16 +2,24 @@ import { z } from 'zod';
|
|
|
2
2
|
/** What a prompt answers: its text, or with `output`, the value of the agent's reply. */
|
|
3
3
|
export type Answer<O> = O extends z.ZodObject ? z.infer<O> : string;
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
* one
|
|
5
|
+
* The check of an answer that must be a value of `output`: what asks again
|
|
6
|
+
* for one, if it is not (`answerOnce`'s `check`, which asks within the
|
|
7
|
+
* turn), or, once it asked (`last`), the error that fails the turn.
|
|
7
8
|
*/
|
|
8
|
-
export declare
|
|
9
|
+
export declare const recheck: (output: z.ZodObject) => (reply: string, last?: boolean) => string | undefined;
|
|
9
10
|
/**
|
|
10
|
-
* `prompt` (a framework's, of `options`),
|
|
11
|
-
* `options.output` if it has one, or else with the
|
|
11
|
+
* `prompt` (a framework's on the conversation of turns, of `options`),
|
|
12
|
+
* answering with a value of `options.output` if it has one, or else with the
|
|
13
|
+
* agent's text. A reply that is not the value is asked again within its
|
|
14
|
+
* turn, once (`check`), and the reply to that must be one, or the prompt
|
|
15
|
+
* fails, terminal, and its turn is closed failed. The model is told
|
|
16
|
+
* `reminder` after the prompt, which the conversation keeps as it was asked
|
|
17
|
+
* (`answerOnce`'s `reminder`). With `native`, the framework asks the model
|
|
18
|
+
* for the value itself (a JSON schema mode): neither the system prompt nor
|
|
19
|
+
* a reminder asks for it, and the reply is checked all the same.
|
|
12
20
|
*/
|
|
13
|
-
export declare const
|
|
21
|
+
export declare const withChecked: <T extends {
|
|
14
22
|
system?: string;
|
|
15
23
|
output?: z.ZodObject;
|
|
16
|
-
}>(text: string, options: T, prompt: (text: string, options: T) => Promise<string
|
|
24
|
+
}>(text: string, options: T, prompt: (text: string, options: T, check?: (answer: string) => string | undefined, reminder?: string) => Promise<string>, native?: boolean) => Promise<Answer<T['output']>>;
|
|
17
25
|
//# sourceMappingURL=output.d.ts.map
|
package/dist/output.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"output.d.ts","sourceRoot":"","sources":["../src/output.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"output.d.ts","sourceRoot":"","sources":["../src/output.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,yFAAyF;AACzF,MAAM,MAAM,MAAM,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC;AAmDpE;;;;GAIG;AACH,eAAO,MAAM,OAAO,WACT,CAAC,CAAC,SAAS,aACZ,MAAM,SAAS,OAAO,KAAG,MAAM,GAAG,SAKzC,CAAC;AAEJ;;;;;;;;;;GAUG;AACH,eAAO,MAAM,WAAW,GAAU,CAAC,SAAS;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,CAAC,CAAC,SAAS,CAAA;CAAE,QAC7E,MAAM,WACH,CAAC,UACF,CACN,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,CAAC,EACV,KAAK,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,EAC9C,QAAQ,CAAC,EAAE,MAAM,KACd,OAAO,CAAC,MAAM,CAAC,uBAEnB,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAS7B,CAAC"}
|
package/dist/output.js
CHANGED
|
@@ -1,25 +1,30 @@
|
|
|
1
1
|
// An agent's answer as data, with `output` (every framework's option): a zod
|
|
2
|
-
// object schema. The agent's reply is its answer, as JSON:
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
2
|
+
// object schema. The agent's reply is its answer, as JSON: a framework that
|
|
3
|
+
// can ask the model for it natively does so (Claude's structured outputs, the
|
|
4
|
+
// API's `output_config.format`); else the system prompt asks for it, with the
|
|
5
|
+
// schema, and each prompt again, at its end. Either way, the reply is checked
|
|
6
|
+
// against it. The prompt (and so `chat`) then answers with the value, as an
|
|
7
|
+
// RPC answers.
|
|
6
8
|
//
|
|
7
9
|
// const planner = claude.agent({ name: 'planner', output: Plan }); // chat answers with a Plan
|
|
8
10
|
//
|
|
9
11
|
// The JSON may be in a code fence, or after a sentence. A reply that is not
|
|
10
12
|
// such JSON is answered with what is wrong with it, once, for the agent to
|
|
11
13
|
// answer again; then the prompt fails (a terminal error), saying how the
|
|
12
|
-
// reply starts.
|
|
13
|
-
// same
|
|
14
|
+
// reply starts. The ask again is a step of the turn (`answerOnce`'s
|
|
15
|
+
// `check`), and a retried attempt resumes the turn, so it reads the same
|
|
16
|
+
// reply.
|
|
14
17
|
import * as restate from '@restatedev/sessions';
|
|
15
18
|
import { z } from 'zod';
|
|
16
19
|
/** What the system prompt asks for, with `output`: JSON of its schema, and nothing else. */
|
|
17
20
|
const format = (output) => 'Your final reply is your answer, as JSON only (no other text): an object of this JSON schema.\n' +
|
|
18
21
|
JSON.stringify(z.toJSONSchema(output));
|
|
19
22
|
/**
|
|
20
|
-
* What each prompt
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
+
* What the model is told after each prompt, with `output` asked in the
|
|
24
|
+
* prompt: the system prompt's ask, again, where an agent reads it last
|
|
25
|
+
* (after a long run of tool calls, it may have drifted from its system
|
|
26
|
+
* prompt to a summary in prose). It is not stored: clients read the prompt
|
|
27
|
+
* as it was asked.
|
|
23
28
|
*/
|
|
24
29
|
const reminder = '\n\nWhen you are done, reply with your answer as JSON only, of the schema you were given: no other text.';
|
|
25
30
|
/**
|
|
@@ -53,29 +58,42 @@ function parsed(output, reply) {
|
|
|
53
58
|
const result = output.safeParse(json);
|
|
54
59
|
return result.success ? { value: result.data } : { wrong: z.prettifyError(result.error) };
|
|
55
60
|
}
|
|
61
|
+
/** The error of a prompt whose answer is not a value of its `output`, `wrong` saying why. */
|
|
62
|
+
const notAsked = (wrong) => new restate.TerminalError(`The agent's reply is not the answer asked for: ${wrong}`);
|
|
56
63
|
/**
|
|
57
|
-
*
|
|
58
|
-
* one
|
|
64
|
+
* The check of an answer that must be a value of `output`: what asks again
|
|
65
|
+
* for one, if it is not (`answerOnce`'s `check`, which asks within the
|
|
66
|
+
* turn), or, once it asked (`last`), the error that fails the turn.
|
|
59
67
|
*/
|
|
60
|
-
export
|
|
61
|
-
|
|
62
|
-
if ('wrong' in answer)
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return answer.value;
|
|
69
|
-
}
|
|
68
|
+
export const recheck = (output) => (reply, last) => {
|
|
69
|
+
const answer = parsed(output, reply);
|
|
70
|
+
if (!('wrong' in answer))
|
|
71
|
+
return undefined;
|
|
72
|
+
if (last)
|
|
73
|
+
throw notAsked(answer.wrong);
|
|
74
|
+
return `Your reply is not the answer asked for: ${answer.wrong}\nReply again, with the JSON only.`;
|
|
75
|
+
};
|
|
70
76
|
/**
|
|
71
|
-
* `prompt` (a framework's, of `options`),
|
|
72
|
-
* `options.output` if it has one, or else with the
|
|
77
|
+
* `prompt` (a framework's on the conversation of turns, of `options`),
|
|
78
|
+
* answering with a value of `options.output` if it has one, or else with the
|
|
79
|
+
* agent's text. A reply that is not the value is asked again within its
|
|
80
|
+
* turn, once (`check`), and the reply to that must be one, or the prompt
|
|
81
|
+
* fails, terminal, and its turn is closed failed. The model is told
|
|
82
|
+
* `reminder` after the prompt, which the conversation keeps as it was asked
|
|
83
|
+
* (`answerOnce`'s `reminder`). With `native`, the framework asks the model
|
|
84
|
+
* for the value itself (a JSON schema mode): neither the system prompt nor
|
|
85
|
+
* a reminder asks for it, and the reply is checked all the same.
|
|
73
86
|
*/
|
|
74
|
-
export const
|
|
87
|
+
export const withChecked = async (text, options, prompt, native = false) => {
|
|
75
88
|
const { output } = options;
|
|
76
89
|
if (!output)
|
|
77
90
|
return prompt(text, options);
|
|
78
|
-
const asked =
|
|
79
|
-
|
|
91
|
+
const asked = native
|
|
92
|
+
? options
|
|
93
|
+
: { ...options, system: [options.system, format(output)].filter(Boolean).join('\n\n') };
|
|
94
|
+
const answer = parsed(output, await prompt(text, asked, recheck(output), native ? undefined : reminder));
|
|
95
|
+
if ('wrong' in answer)
|
|
96
|
+
throw notAsked(answer.wrong);
|
|
97
|
+
return answer.value;
|
|
80
98
|
};
|
|
81
99
|
//# sourceMappingURL=output.js.map
|
package/dist/output.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"output.js","sourceRoot":"","sources":["../src/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AI,IAAI;QAAE,MAAM,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACvC,OAAO,2CAA2C,MAAM,CAAC,KAAK,oCAAoC,CAAC;AACrG,CAAC,CAAC;AAEJ;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,KAAK,EAC9B,IAAY,EACZ,OAAU,EACV,MAKoB,EACpB,MAAM,GAAG,KAAK,EACgB,EAAE;IAChC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAC3B,IAAI,CAAC,MAAM;QAAE,OAAO,MAAM,CAAC,IAAI,EAAE,OAAO,CAAiC,CAAC;IAC1E,MAAM,KAAK,GAAG,MAAM;QAClB,CAAC,CAAC,OAAO;QACT,CAAC,CAAC,EAAE,GAAG,OAAO,EAAE,MAAM,EAAE,CAAC,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;IAC1F,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,MAAM,CAAC,IAAI,EAAE,KAAK,EAAE,OAAO,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC;IACzG,IAAI,OAAO,IAAI,MAAM;QAAE,MAAM,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACpD,OAAO,MAAM,CAAC,KAA4B,CAAC;AAC7C,CAAC,CAAC"}
|
package/dist/pi/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import * as restate from '@restatedev/sessions';
|
|
2
2
|
import type { Context } from '@earendil-works/chord';
|
|
3
|
-
import { type MutableModels } from '@earendil-works/pi-ai';
|
|
4
|
-
import { Harness, type Conversation, type ToolRegistration } from '@earendil-works/pi-durable';
|
|
5
|
-
import { type Configure } from '../common.ts';
|
|
3
|
+
import { type Message, type MutableModels } from '@earendil-works/pi-ai';
|
|
4
|
+
import { Harness, type Conversation, type ConversationRetryPolicy, type ToolRegistration } from '@earendil-works/pi-durable';
|
|
5
|
+
import { type Check, type Configure } from '../common.ts';
|
|
6
|
+
import { type Items } from '../conversation.ts';
|
|
6
7
|
import { type Answer } from '../output.ts';
|
|
7
8
|
import { type Definition } from '../object.ts';
|
|
8
9
|
import { type Tool as AnyTool } from '../tools.ts';
|
|
@@ -11,11 +12,28 @@ import type { z } from 'zod';
|
|
|
11
12
|
export interface Pi {
|
|
12
13
|
harness: Harness;
|
|
13
14
|
context: Context;
|
|
14
|
-
/**
|
|
15
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Answer `text`, once, with the answer's text: a turn of the object's
|
|
17
|
+
* conversation (each prompt of an invocation has an id of its own, the
|
|
18
|
+
* same on every attempt). With `check`, a reply it returns a message for
|
|
19
|
+
* is asked again within the turn, once. With `reminder`, the model is told
|
|
20
|
+
* it after the prompt, which the conversation keeps as it was asked.
|
|
21
|
+
*/
|
|
22
|
+
prompt: (text: string, check?: Check, reminder?: string) => Promise<string>;
|
|
16
23
|
/** The object's conversation, on the model: for `reset`, `compact`, `entries` and the like. */
|
|
17
24
|
conversation: () => Promise<Conversation>;
|
|
18
25
|
}
|
|
26
|
+
/** A pi message as the conversation of turns keeps it: with the id of the pi entry it was copied from. */
|
|
27
|
+
export interface Item {
|
|
28
|
+
entry?: number;
|
|
29
|
+
message: Message;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* pi's items: pi-ai's messages. A user's message is said by the user; an
|
|
33
|
+
* assistant's (but one aborted, failed or deferred) says its text, with its
|
|
34
|
+
* tool calls; a tool's result is a result; a system message says nothing.
|
|
35
|
+
*/
|
|
36
|
+
export declare const items: Items<Item>;
|
|
19
37
|
/** An agent's options: `chat`, `prompt` and `withPi` take them. */
|
|
20
38
|
export interface Options<O extends z.ZodObject | undefined = z.ZodObject | undefined> {
|
|
21
39
|
/** The model, as `provider/model-id`: MODEL, or else openai/gpt-6-sol. */
|
|
@@ -30,6 +48,15 @@ export interface Options<O extends z.ZodObject | undefined = z.ZodObject | undef
|
|
|
30
48
|
workspace?: string;
|
|
31
49
|
/** OpenAI's service tier for each request: `fast` for its fast mode (priced higher), `flex`, and so on. */
|
|
32
50
|
serviceTier?: ServiceTier;
|
|
51
|
+
/**
|
|
52
|
+
* How pi asks the model again after a request failed for a reason that
|
|
53
|
+
* passes (an overloaded or unreachable provider, a rate limit): by
|
|
54
|
+
* default without limit, 2 s after the first failure, then twice as long
|
|
55
|
+
* each time, a minute apart at most. With `maxRetries`, a failure after
|
|
56
|
+
* that many retries ends the turn. A request refused for good (a wrong API
|
|
57
|
+
* key, an unknown model) is never asked again.
|
|
58
|
+
*/
|
|
59
|
+
retry?: Partial<ConversationRetryPolicy>;
|
|
33
60
|
/**
|
|
34
61
|
* The answer as data: a zod object, whose JSON the agent replies with
|
|
35
62
|
* (checked), and `chat` and `prompt` answer with (see output.ts).
|
|
@@ -44,7 +71,7 @@ export type ServiceTier = 'auto' | 'default' | 'fast' | 'flex' | 'priority';
|
|
|
44
71
|
* has `prompt(text)`, and the `harness` and chord `context` for anything
|
|
45
72
|
* else.
|
|
46
73
|
*/
|
|
47
|
-
export declare const withPi: <T>(ctx: restate.SessionContext, { model: named, models, system, tools, workspace, serviceTier }: Options, work: (pi: Pi) => Promise<T>) => Promise<T>;
|
|
74
|
+
export declare const withPi: <T>(ctx: restate.SessionContext, { model: named, models, system, tools, workspace, serviceTier, retry }: Options, work: (pi: Pi) => Promise<T>) => Promise<T>;
|
|
48
75
|
/**
|
|
49
76
|
* Answer `text`, once: a retried attempt resumes the same prompt. Each prompt
|
|
50
77
|
* of an invocation is a prompt of its own.
|
|
@@ -65,13 +92,13 @@ export declare const chat: <O extends z.ZodObject | undefined = undefined>(optio
|
|
|
65
92
|
handler: (ctx: restate.SessionContext, text: string) => Promise<Answer<O>>;
|
|
66
93
|
};
|
|
67
94
|
/**
|
|
68
|
-
* A
|
|
69
|
-
*
|
|
70
|
-
*
|
|
95
|
+
* A view of the conversation as clients read it: a page at a time, or what
|
|
96
|
+
* is new or changed since a cursor; `view: 'summary'` for each turn's
|
|
97
|
+
* message and answer. It answers while a message is answered.
|
|
71
98
|
*/
|
|
72
99
|
export declare const messages: () => (ctx: restate.ViewContext, request?: import("../common.ts").MessagesRequest) => Promise<import("../common.ts").Messages>;
|
|
73
100
|
/** An agent: a virtual object `name`, one per key, whose `chat` handler is {@link chat}, and `messages` {@link messages}. */
|
|
74
|
-
export declare const agent: <P extends string, O extends z.ZodObject | undefined = undefined>({ name, description, schema, background, ...options }: Options<O> & Definition<P>) => restate.VirtualObjectDefinition<P, {
|
|
101
|
+
export declare const agent: <P extends string, O extends z.ZodObject | undefined = undefined>({ name, description, schema, background, window, budget, ...options }: Options<O> & Definition<P>) => restate.VirtualObjectDefinition<P, {
|
|
75
102
|
signal: (ctx: restate.ViewContext, signal: import("../signals.ts").Signal) => Promise<import("../signals.ts").Resolved>;
|
|
76
103
|
pending: (ctx: restate.ViewContext) => Promise<import("../signals.ts").Waiting[]>;
|
|
77
104
|
status: (ctx: restate.ViewContext) => Promise<import("../object.ts").Status>;
|
|
@@ -80,7 +107,8 @@ export declare const agent: <P extends string, O extends z.ZodObject | undefined
|
|
|
80
107
|
watch: (ctx: restate.ViewContext, watch: import("../signals.ts").Watch) => Promise<import("../signals.ts").Notifications>;
|
|
81
108
|
} & {
|
|
82
109
|
clear: (ctx: restate.ViewContext) => Promise<import("../object.ts").Stopped>;
|
|
83
|
-
|
|
110
|
+
steer: (ctx: restate.ViewContext, text: string) => Promise<import("../signals.ts").Steered>;
|
|
111
|
+
sync: (ctx: restate.ViewContext, { cursor, revision, timeoutSeconds, limit, view }?: import("../object.ts").SyncRequest) => Promise<import("../object.ts").Synced>;
|
|
84
112
|
} & {
|
|
85
113
|
chat: (ctx: restate.SessionContext, text: string) => Promise<Answer<O>>;
|
|
86
114
|
} & {
|
package/dist/pi/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/pi/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/pi/index.ts"],"names":[],"mappings":"AAuCA,OAAO,KAAK,OAAO,MAAM,sBAAsB,CAAC;AAChD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAGrD,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,aAAa,EAAQ,MAAM,uBAAuB,CAAC;AAG/E,OAAO,EAML,OAAO,EAGP,KAAK,YAAY,EAEjB,KAAK,uBAAuB,EAE5B,KAAK,gBAAgB,EACtB,MAAM,4BAA4B,CAAC;AAIpC,OAAO,EAIL,KAAK,KAAK,EAEV,KAAK,SAAS,EAaf,MAAM,cAAc,CAAC;AACtB,OAAO,EAA4B,KAAK,KAAK,EAA0B,MAAM,oBAAoB,CAAC;AAGlG,OAAO,EAAE,KAAK,MAAM,EAAe,MAAM,cAAc,CAAC;AACxD,OAAO,EAAE,KAAK,UAAU,EAAiB,MAAM,cAAc,CAAC;AAC9D,OAAO,EAAwB,KAAK,IAAI,IAAI,OAAO,EAAE,MAAM,aAAa,CAAC;AACzE,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAG7B,uHAAuH;AACvH,MAAM,WAAW,EAAE;IACjB,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,EAAE,OAAO,CAAC;IACjB;;;;;;OAMG;IACH,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,KAAK,EAAE,QAAQ,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IAC5E,+FAA+F;IAC/F,YAAY,EAAE,MAAM,OAAO,CAAC,YAAY,CAAC,CAAC;CAC3C;AAMD,0GAA0G;AAC1G,MAAM,WAAW,IAAI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