@restatedev/agents 0.11.2 → 0.12.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 +189 -808
- package/dist/pi/factory.d.ts +73 -0
- package/dist/pi/factory.d.ts.map +1 -0
- package/dist/pi/factory.js +145 -0
- package/dist/pi/factory.js.map +1 -0
- package/dist/pi/index.d.ts +5 -129
- package/dist/pi/index.d.ts.map +1 -1
- package/dist/pi/index.js +31 -571
- package/dist/pi/index.js.map +1 -1
- package/dist/pi/keepalive.d.ts +3 -16
- package/dist/pi/keepalive.d.ts.map +1 -1
- package/dist/pi/keepalive.js +38 -41
- package/dist/pi/keepalive.js.map +1 -1
- package/dist/pi/left.d.ts +16 -0
- package/dist/pi/left.d.ts.map +1 -0
- package/dist/pi/left.js +23 -0
- package/dist/pi/left.js.map +1 -0
- package/dist/pi/storage.d.ts +19 -76
- package/dist/pi/storage.d.ts.map +1 -1
- package/dist/pi/storage.js +50 -368
- package/dist/pi/storage.js.map +1 -1
- package/dist/pi/tools.d.ts +12 -0
- package/dist/pi/tools.d.ts.map +1 -0
- package/dist/pi/tools.js +70 -0
- package/dist/pi/tools.js.map +1 -0
- package/package.json +11 -254
- package/src/pi/factory.ts +231 -0
- package/src/pi/index.ts +31 -759
- package/src/pi/keepalive.ts +46 -56
- package/src/pi/left.ts +52 -0
- package/src/pi/storage.ts +75 -409
- package/src/pi/tools.ts +78 -0
- package/dist/agentlib/agent.d.ts +0 -32
- package/dist/agentlib/agent.d.ts.map +0 -1
- package/dist/agentlib/agent.js +0 -174
- package/dist/agentlib/agent.js.map +0 -1
- package/dist/agentlib/index.d.ts +0 -5
- package/dist/agentlib/index.d.ts.map +0 -1
- package/dist/agentlib/index.js +0 -15
- package/dist/agentlib/index.js.map +0 -1
- package/dist/agentlib/messages.d.ts +0 -39
- package/dist/agentlib/messages.d.ts.map +0 -1
- package/dist/agentlib/messages.js +0 -177
- package/dist/agentlib/messages.js.map +0 -1
- package/dist/agentlib/model.d.ts +0 -31
- package/dist/agentlib/model.d.ts.map +0 -1
- package/dist/agentlib/model.js +0 -140
- package/dist/agentlib/model.js.map +0 -1
- package/dist/agentlib/options.d.ts +0 -58
- package/dist/agentlib/options.d.ts.map +0 -1
- package/dist/agentlib/options.js +0 -2
- package/dist/agentlib/options.js.map +0 -1
- package/dist/agentlib/runtime.d.ts +0 -37
- package/dist/agentlib/runtime.d.ts.map +0 -1
- package/dist/agentlib/runtime.js +0 -216
- package/dist/agentlib/runtime.js.map +0 -1
- package/dist/agentlib/state.d.ts +0 -114
- package/dist/agentlib/state.d.ts.map +0 -1
- package/dist/agentlib/state.js +0 -52
- package/dist/agentlib/state.js.map +0 -1
- package/dist/agentlib/steering.d.ts +0 -23
- package/dist/agentlib/steering.d.ts.map +0 -1
- package/dist/agentlib/steering.js +0 -48
- package/dist/agentlib/steering.js.map +0 -1
- package/dist/agentlib/tools.d.ts +0 -33
- package/dist/agentlib/tools.d.ts.map +0 -1
- package/dist/agentlib/tools.js +0 -245
- package/dist/agentlib/tools.js.map +0 -1
- package/dist/agentlib/turn.d.ts +0 -82
- package/dist/agentlib/turn.d.ts.map +0 -1
- package/dist/agentlib/turn.js +0 -253
- package/dist/agentlib/turn.js.map +0 -1
- package/dist/calls.d.ts +0 -90
- package/dist/calls.d.ts.map +0 -1
- package/dist/calls.js +0 -241
- package/dist/calls.js.map +0 -1
- package/dist/claude/index.d.ts +0 -106
- package/dist/claude/index.d.ts.map +0 -1
- package/dist/claude/index.js +0 -386
- package/dist/claude/index.js.map +0 -1
- package/dist/client.d.ts +0 -90
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js +0 -88
- package/dist/client.js.map +0 -1
- package/dist/common.d.ts +0 -410
- package/dist/common.d.ts.map +0 -1
- package/dist/common.js +0 -563
- package/dist/common.js.map +0 -1
- package/dist/companion.d.ts +0 -286
- package/dist/companion.d.ts.map +0 -1
- package/dist/companion.js +0 -463
- package/dist/companion.js.map +0 -1
- package/dist/conversation.d.ts +0 -576
- package/dist/conversation.d.ts.map +0 -1
- package/dist/conversation.js +0 -1476
- package/dist/conversation.js.map +0 -1
- package/dist/deepseek/barriers.d.ts +0 -11
- package/dist/deepseek/barriers.d.ts.map +0 -1
- package/dist/deepseek/barriers.js +0 -36
- package/dist/deepseek/barriers.js.map +0 -1
- package/dist/deepseek/index.d.ts +0 -97
- package/dist/deepseek/index.d.ts.map +0 -1
- package/dist/deepseek/index.js +0 -603
- package/dist/deepseek/index.js.map +0 -1
- package/dist/deepseek/persistence.d.ts +0 -83
- package/dist/deepseek/persistence.d.ts.map +0 -1
- package/dist/deepseek/persistence.js +0 -262
- package/dist/deepseek/persistence.js.map +0 -1
- package/dist/index.d.ts +0 -14
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -17
- package/dist/index.js.map +0 -1
- package/dist/langgraph/checkpointer.d.ts +0 -52
- package/dist/langgraph/checkpointer.d.ts.map +0 -1
- package/dist/langgraph/checkpointer.js +0 -477
- package/dist/langgraph/checkpointer.js.map +0 -1
- package/dist/langgraph/context.d.ts +0 -10
- package/dist/langgraph/context.d.ts.map +0 -1
- package/dist/langgraph/context.js +0 -21
- package/dist/langgraph/context.js.map +0 -1
- package/dist/langgraph/errors.d.ts +0 -16
- package/dist/langgraph/errors.d.ts.map +0 -1
- package/dist/langgraph/errors.js +0 -30
- package/dist/langgraph/errors.js.map +0 -1
- package/dist/langgraph/graph.d.ts +0 -25
- package/dist/langgraph/graph.d.ts.map +0 -1
- package/dist/langgraph/graph.js +0 -378
- package/dist/langgraph/graph.js.map +0 -1
- package/dist/langgraph/index.d.ts +0 -119
- package/dist/langgraph/index.d.ts.map +0 -1
- package/dist/langgraph/index.js +0 -325
- package/dist/langgraph/index.js.map +0 -1
- package/dist/langgraph/keepalive.d.ts +0 -16
- package/dist/langgraph/keepalive.d.ts.map +0 -1
- package/dist/langgraph/keepalive.js +0 -51
- package/dist/langgraph/keepalive.js.map +0 -1
- package/dist/langgraph/nodes.d.ts +0 -71
- package/dist/langgraph/nodes.d.ts.map +0 -1
- package/dist/langgraph/nodes.js +0 -148
- package/dist/langgraph/nodes.js.map +0 -1
- package/dist/messages.d.ts +0 -18
- package/dist/messages.d.ts.map +0 -1
- package/dist/messages.js +0 -182
- package/dist/messages.js.map +0 -1
- package/dist/object.d.ts +0 -213
- package/dist/object.d.ts.map +0 -1
- package/dist/object.js +0 -309
- package/dist/object.js.map +0 -1
- package/dist/opencode/db.d.ts +0 -81
- package/dist/opencode/db.d.ts.map +0 -1
- package/dist/opencode/db.js +0 -249
- package/dist/opencode/db.js.map +0 -1
- package/dist/opencode/index.d.ts +0 -184
- package/dist/opencode/index.d.ts.map +0 -1
- package/dist/opencode/index.js +0 -1079
- package/dist/opencode/index.js.map +0 -1
- package/dist/output.d.ts +0 -25
- package/dist/output.d.ts.map +0 -1
- package/dist/output.js +0 -99
- package/dist/output.js.map +0 -1
- package/dist/pi/commits.d.ts +0 -6
- package/dist/pi/commits.d.ts.map +0 -1
- package/dist/pi/commits.js +0 -3
- package/dist/pi/commits.js.map +0 -1
- package/dist/steering.d.ts +0 -32
- package/dist/steering.d.ts.map +0 -1
- package/dist/steering.js +0 -45
- package/dist/steering.js.map +0 -1
- package/dist/tools.d.ts +0 -267
- package/dist/tools.d.ts.map +0 -1
- package/dist/tools.js +0 -305
- package/dist/tools.js.map +0 -1
- package/dist/webhook.d.ts +0 -71
- package/dist/webhook.d.ts.map +0 -1
- package/dist/webhook.js +0 -102
- package/dist/webhook.js.map +0 -1
- package/src/agentlib/README.md +0 -229
- package/src/agentlib/agent.ts +0 -216
- package/src/agentlib/index.ts +0 -16
- package/src/agentlib/messages.ts +0 -207
- package/src/agentlib/model.ts +0 -156
- package/src/agentlib/options.ts +0 -62
- package/src/agentlib/runtime.ts +0 -252
- package/src/agentlib/state.ts +0 -93
- package/src/agentlib/steering.ts +0 -53
- package/src/agentlib/tools.ts +0 -355
- package/src/agentlib/turn.ts +0 -302
- package/src/calls.ts +0 -333
- package/src/claude/README.md +0 -107
- package/src/claude/index.ts +0 -487
- package/src/client.ts +0 -187
- package/src/common.ts +0 -766
- package/src/companion.ts +0 -675
- package/src/conversation.ts +0 -1799
- package/src/deepseek/README.md +0 -202
- package/src/deepseek/barriers.ts +0 -63
- package/src/deepseek/index.ts +0 -740
- package/src/deepseek/persistence.ts +0 -337
- package/src/index.ts +0 -41
- package/src/langgraph/README.md +0 -199
- package/src/langgraph/checkpointer.ts +0 -614
- package/src/langgraph/context.ts +0 -27
- package/src/langgraph/errors.ts +0 -26
- package/src/langgraph/graph.ts +0 -422
- package/src/langgraph/index.ts +0 -500
- package/src/langgraph/keepalive.ts +0 -58
- package/src/langgraph/nodes.ts +0 -247
- package/src/messages.ts +0 -244
- package/src/object.ts +0 -454
- package/src/opencode/README.md +0 -249
- package/src/opencode/db.ts +0 -387
- package/src/opencode/index.ts +0 -1339
- package/src/output.ts +0 -116
- package/src/pi/README.md +0 -174
- package/src/pi/commits.ts +0 -7
- package/src/steering.ts +0 -68
- package/src/tools.ts +0 -515
- package/src/webhook.ts +0 -161
package/src/output.ts
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
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: 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.
|
|
8
|
-
//
|
|
9
|
-
// const planner = claude.agent({ name: 'planner', output: Plan }); // chat answers with a Plan
|
|
10
|
-
//
|
|
11
|
-
// The JSON may be in a code fence, or after a sentence. A reply that is not
|
|
12
|
-
// such JSON is answered with what is wrong with it, once, for the agent to
|
|
13
|
-
// answer again; then the prompt fails (a terminal error), saying how the
|
|
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.
|
|
17
|
-
import * as restate from '@restatedev/sessions';
|
|
18
|
-
import { z } from 'zod';
|
|
19
|
-
|
|
20
|
-
/** What a prompt answers: its text, or with `output`, the value of the agent's reply. */
|
|
21
|
-
export type Answer<O> = O extends z.ZodObject ? z.infer<O> : string;
|
|
22
|
-
|
|
23
|
-
/** What the system prompt asks for, with `output`: JSON of its schema, and nothing else. */
|
|
24
|
-
const format = (output: z.ZodObject) =>
|
|
25
|
-
'Your final reply is your answer, as JSON only (no other text): an object of this JSON schema.\n' +
|
|
26
|
-
JSON.stringify(z.toJSONSchema(output));
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* What the model is told after each prompt, with `output` asked in the
|
|
30
|
-
* prompt: the system prompt's ask, again, where an agent reads it last
|
|
31
|
-
* (after a long run of tool calls, it may have drifted from its system
|
|
32
|
-
* prompt to a summary in prose). It is not stored: clients read the prompt
|
|
33
|
-
* as it was asked.
|
|
34
|
-
*/
|
|
35
|
-
const reminder =
|
|
36
|
-
'\n\nWhen you are done, reply with your answer as JSON only, of the schema you were given: no other text.';
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* The JSON `reply` holds: all of it, what a code fence (```json) holds, or
|
|
40
|
-
* else its object, from its first `{` to its last `}` (a model that ends a
|
|
41
|
-
* long run often says a sentence first: "Done. Here is the summary: {...}").
|
|
42
|
-
*/
|
|
43
|
-
function jsonOf(reply: string): unknown {
|
|
44
|
-
const fenced = /```(?:json)?\s*\n([^]*?)\n?```/.exec(reply)?.[1];
|
|
45
|
-
const object = reply.slice(reply.indexOf('{'), reply.lastIndexOf('}') + 1);
|
|
46
|
-
for (const text of [reply, fenced, object]) {
|
|
47
|
-
try {
|
|
48
|
-
if (text?.trim()) return JSON.parse(text);
|
|
49
|
-
} catch {
|
|
50
|
-
// the next
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
return undefined;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/** The start of `reply`, for an error. */
|
|
57
|
-
const excerpt = (reply: string) => (reply.length > 300 ? `${reply.slice(0, 300)}…` : reply);
|
|
58
|
-
|
|
59
|
-
/** `reply` as a value of `output`, or what is wrong with it. */
|
|
60
|
-
function parsed<O extends z.ZodObject>(output: O, reply: string): { value: z.infer<O> } | { wrong: string } {
|
|
61
|
-
if (!reply.trim()) return { wrong: 'it is empty' };
|
|
62
|
-
const json = jsonOf(reply);
|
|
63
|
-
if (json === undefined) return { wrong: `it is not JSON: ${JSON.stringify(excerpt(reply))}` };
|
|
64
|
-
const result = output.safeParse(json);
|
|
65
|
-
return result.success ? { value: result.data } : { wrong: z.prettifyError(result.error) };
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/** The error of a prompt whose answer is not a value of its `output`, `wrong` saying why. */
|
|
69
|
-
const notAsked = (wrong: string) =>
|
|
70
|
-
new restate.TerminalError(`The agent's reply is not the answer asked for: ${wrong}`);
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* The check of an answer that must be a value of `output`: what asks again
|
|
74
|
-
* for one, if it is not (`answerOnce`'s `check`, which asks within the
|
|
75
|
-
* turn), or, once it asked (`last`), the error that fails the turn.
|
|
76
|
-
*/
|
|
77
|
-
export const recheck =
|
|
78
|
-
(output: z.ZodObject) =>
|
|
79
|
-
(reply: string, last?: boolean): string | undefined => {
|
|
80
|
-
const answer = parsed(output, reply);
|
|
81
|
-
if (!('wrong' in answer)) return undefined;
|
|
82
|
-
if (last) throw notAsked(answer.wrong);
|
|
83
|
-
return `Your reply is not the answer asked for: ${answer.wrong}\nReply again, with the JSON only.`;
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* `prompt` (a framework's on the conversation of turns, of `options`),
|
|
88
|
-
* answering with a value of `options.output` if it has one, or else with the
|
|
89
|
-
* agent's text. A reply that is not the value is asked again within its
|
|
90
|
-
* turn, once (`check`), and the reply to that must be one, or the prompt
|
|
91
|
-
* fails, terminal, and its turn is closed failed. The model is told
|
|
92
|
-
* `reminder` after the prompt, which the conversation keeps as it was asked
|
|
93
|
-
* (`answerOnce`'s `reminder`). With `native`, the framework asks the model
|
|
94
|
-
* for the value itself (a JSON schema mode): neither the system prompt nor
|
|
95
|
-
* a reminder asks for it, and the reply is checked all the same.
|
|
96
|
-
*/
|
|
97
|
-
export const withChecked = async <T extends { system?: string; output?: z.ZodObject }>(
|
|
98
|
-
text: string,
|
|
99
|
-
options: T,
|
|
100
|
-
prompt: (
|
|
101
|
-
text: string,
|
|
102
|
-
options: T,
|
|
103
|
-
check?: (answer: string) => string | undefined,
|
|
104
|
-
reminder?: string,
|
|
105
|
-
) => Promise<string>,
|
|
106
|
-
native = false,
|
|
107
|
-
): Promise<Answer<T['output']>> => {
|
|
108
|
-
const { output } = options;
|
|
109
|
-
if (!output) return prompt(text, options) as Promise<Answer<T['output']>>;
|
|
110
|
-
const asked = native
|
|
111
|
-
? options
|
|
112
|
-
: { ...options, system: [options.system, format(output)].filter(Boolean).join('\n\n') };
|
|
113
|
-
const answer = parsed(output, await prompt(text, asked, recheck(output), native ? undefined : reminder));
|
|
114
|
-
if ('wrong' in answer) throw notAsked(answer.wrong);
|
|
115
|
-
return answer.value as Answer<T['output']>;
|
|
116
|
-
};
|
package/src/pi/README.md
DELETED
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
# pi
|
|
2
|
-
|
|
3
|
-
[pi-durable](https://www.npmjs.com/package/@earendil-works/pi-durable) is a
|
|
4
|
-
durable agent harness: it commits conversations, model turns and tool calls
|
|
5
|
-
to storage, and resumes unfinished work when the storage is opened again.
|
|
6
|
-
Here its storage is the agent's SQL database, in the Restate virtual object
|
|
7
|
-
([SQL in Restate virtual objects](../../../sessions/README.md)).
|
|
8
|
-
|
|
9
|
-
## Use
|
|
10
|
-
|
|
11
|
-
```ts
|
|
12
|
-
import { serve } from '@restatedev/agents';
|
|
13
|
-
import * as pi from '@restatedev/agents/pi';
|
|
14
|
-
import { weather } from './weather.ts';
|
|
15
|
-
|
|
16
|
-
const assistant = pi.agent({ name: 'assistant', system: 'Answer briefly.', tools: [weather] });
|
|
17
|
-
await serve(assistant);
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
| Option | What |
|
|
21
|
-
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
22
|
-
| `model` | `provider/model-id`, of any provider pi-ai knows; or else `MODEL`, or else `openai/gpt-6-sol` (`OPENAI_API_KEY`) |
|
|
23
|
-
| `models` | A pi-ai model registry, for a provider of your own |
|
|
24
|
-
| `system` | The system prompt |
|
|
25
|
-
| `tools` | `tool()`s and Restate's tools of `@restatedev/agents`, or pi-durable's `defineTool`s |
|
|
26
|
-
| `output` | A zod object: the answer as data, which the agent replies with as JSON, and `chat` answers with (see [Structured answers](../../README.md#structured-answers)) |
|
|
27
|
-
| `workspace` | A directory for pi-durable's coding tools (`read`, `write`, `edit`, `bash`), on the local file system; use a sandbox |
|
|
28
|
-
| `serviceTier` | OpenAI's service tier for each request: `fast` for its fast mode (priced higher), `flex`, and so on |
|
|
29
|
-
| `retry` | How pi asks the model again after a request failed for a reason that passes (an outage, a rate limit): by default without limit, 2 s to a minute apart |
|
|
30
|
-
| `commits` | `'each'` (the default): each pi commit is one write to Restate; `'effects'`: one for all the commits between two effects (see [Through crashes](#through-crashes)) |
|
|
31
|
-
|
|
32
|
-
For a session object of your own, `pi.chat(options)` is the handler and
|
|
33
|
-
`pi.messages()` the view; `options` may be a function of the invocation's context, such as
|
|
34
|
-
each user's model from the object's database. `pi.prompt(ctx, text,
|
|
35
|
-
options)` answers a prompt in a handler of your own. For more than a prompt,
|
|
36
|
-
`withPi(ctx, options, work)` runs `work` with the Harness: `prompt(text)`
|
|
37
|
-
answers a prompt once, and `conversation()` is the object's conversation, on
|
|
38
|
-
the model, for pi-durable's own API (`reset`, `compact`, `entries`...):
|
|
39
|
-
|
|
40
|
-
```ts
|
|
41
|
-
const options = async (ctx: restate.SessionContext): Promise<pi.Options> => ({
|
|
42
|
-
model: (await settingsOf(ctx)).model,
|
|
43
|
-
tools: [clock],
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
handlers: {
|
|
47
|
-
chat: pi.chat(options),
|
|
48
|
-
reset: async (ctx, handoff: string) =>
|
|
49
|
-
pi.withPi(ctx, await options(ctx), async ({ conversation, context }) => (await conversation()).reset(handoff, context)),
|
|
50
|
-
},
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
A conversation goes on with the model of each invocation: a `model` (or
|
|
54
|
-
MODEL) changed since it started is the model of its next turn.
|
|
55
|
-
[`examples/intermediate/harness.ts`](../../../examples/intermediate/harness.ts) keeps each user's
|
|
56
|
-
model and persona in the object's database.
|
|
57
|
-
|
|
58
|
-
## Through crashes
|
|
59
|
-
|
|
60
|
-
By default (`commits: 'each'`), each pi-durable commit is an SQL transaction
|
|
61
|
-
in the object's database ([`storage.ts`](storage.ts)), stored in order with
|
|
62
|
-
everything else the handler does. A retried attempt opens the Harness again
|
|
63
|
-
and carries on:
|
|
64
|
-
|
|
65
|
-
- a prompt is submitted once, by its id (pi-durable's `requestId`, the
|
|
66
|
-
invocation's id);
|
|
67
|
-
- an answered prompt returns its answer;
|
|
68
|
-
- unfinished work resumes: a model call is made again, and a tool call runs
|
|
69
|
-
again with the same call id, which a tool passes on to stay idempotent,
|
|
70
|
-
however often a crash cut it off (pi-durable's `restartLimit` is
|
|
71
|
-
`Infinity`: Restate's retry policy decides);
|
|
72
|
-
- a tool's error fails the attempt, and Restate retries it (pi-durable would
|
|
73
|
-
hand it to the model); a `restate.TerminalError` of a `@restatedev/agents`
|
|
74
|
-
tool is its answer to the model;
|
|
75
|
-
- a provider's outage never ends the turn: pi asks the model again until it
|
|
76
|
-
answers (pi-durable gives up after 3 retries; `retry` sets how often), and
|
|
77
|
-
each retry is committed, so a retried attempt waits as the attempt before
|
|
78
|
-
it would have; a request the provider refuses for good (a wrong API key)
|
|
79
|
-
ends the turn, with a terminal error that says what to check;
|
|
80
|
-
- a run a killed invocation left is aborted, never finished: its turn is
|
|
81
|
-
closed (`ended`) in the agent's conversation of turns, and the next
|
|
82
|
-
prompt tells the model how it ended.
|
|
83
|
-
|
|
84
|
-
pi keeps its own history (and compacts it); each pi commit of a prompt's run
|
|
85
|
-
copies its new entries (the model's replies, the tools' results) into the
|
|
86
|
-
agent's conversation of turns, in the same transaction: what `messages`
|
|
87
|
-
shows ([docs/conversation.md](../../docs/conversation.md)). A commit that
|
|
88
|
-
copied a reply with tool calls, or tools' results, is published once it is
|
|
89
|
-
done, in a `chat` turn (with `commits: 'each'`, pi commits each result on its
|
|
90
|
-
own: one publish each; a `prompt()` in a handler of your own publishes nothing), and nothing else
|
|
91
|
-
is: a tool call's start publishes nothing, so a follower reads the calls
|
|
92
|
-
once they are stored, and each output once it is. The views show
|
|
93
|
-
each turn as it was asked and answered, not pi's summaries and handoffs.
|
|
94
|
-
|
|
95
|
-
### One write for each effect
|
|
96
|
-
|
|
97
|
-
A step of a turn makes several pi commits (the preparation, the request, the
|
|
98
|
-
reply, the tool's start, its result), and each costs one round trip to
|
|
99
|
-
Restate: a turn of four model requests and three tool calls makes 30. With
|
|
100
|
-
`commits: 'effects'`, the commits between two effects (a model request, a
|
|
101
|
-
tool call) share one write: the same turn makes 10 (the turn opening, each
|
|
102
|
-
effect's start, the answer, the turn closing); in a `chat` turn that is
|
|
103
|
-
followed, a publish after each round that reads what was steered to it adds
|
|
104
|
-
one for each round. While an effect runs, its own commits (a model's
|
|
105
|
-
progress, a tool's result) are written at once, as with `'each'`; so is
|
|
106
|
-
everything outside a prompt (`work`, a compaction).
|
|
107
|
-
|
|
108
|
-
What `'effects'` keeps: what pi committed before a tool call, or a model call
|
|
109
|
-
that returns a promise, starts is stored before it does, so it never runs
|
|
110
|
-
ahead of its own history (a streamed model request starts while that write is
|
|
111
|
-
in flight, which is harmless: a retry only repeats the request); and what
|
|
112
|
-
anyone else can see (a publish, a Restate call, a send, the
|
|
113
|
-
end of the handler, a `ctx.db` call of your own) comes after a write of all
|
|
114
|
-
of it, never before. A failed pi commit still undoes only itself, and a
|
|
115
|
-
`ctx.db` call from inside one is refused, as with `'each'`.
|
|
116
|
-
|
|
117
|
-
What it gives up: what pi commits in the milliseconds between one effect's
|
|
118
|
-
result and the next effect's start is lost in a crash, and the retried
|
|
119
|
-
attempt does it again from the state that was stored. Tools that start together start
|
|
120
|
-
after one write, and the results of tools that end together are stored in one
|
|
121
|
-
commit, and published once. A `ctx.db` call of yours made while pi has
|
|
122
|
-
commits to write costs one more write (for the commits, which it waits for),
|
|
123
|
-
and no stall. A write of the commits that fails fails the attempt, as a
|
|
124
|
-
failed commit does, though pi was told its commits were done.
|
|
125
|
-
|
|
126
|
-
The files of a `workspace` are not in the database: they last as long as the
|
|
127
|
-
disk does, and a coding tool a crash cut off is not run again (the model is
|
|
128
|
-
told it was interrupted).
|
|
129
|
-
|
|
130
|
-
## Kept alive
|
|
131
|
-
|
|
132
|
-
An attempt of a session object is shut down by Restate after its inactivity
|
|
133
|
-
timeout (a minute), and run again from the start, unless work keeps it alive
|
|
134
|
-
(`ctx.working`). Each request of a model does, wherever pi makes it (a turn's,
|
|
135
|
-
a deferred response's, a compaction's): the agent wraps the model registry (its
|
|
136
|
-
own, or the `models` given), and a request is kept alive from its call until
|
|
137
|
-
its response ends, fails, is abandoned or is aborted. So does each built-in
|
|
138
|
-
coding tool (`read`, `write`, `edit`, `bash`): it works on the machine, and a
|
|
139
|
-
`bash` may run for minutes.
|
|
140
|
-
|
|
141
|
-
A tool of your own is not kept alive: a `tool()` sets `keepalive` if it may
|
|
142
|
-
work for longer than the timeout, or it is cut off, as by a crash (the next
|
|
143
|
-
attempt tells the model it was interrupted, see above). A tool kept alive must
|
|
144
|
-
not wait on Restate (a call, a signal) inside.
|
|
145
|
-
|
|
146
|
-
## Run
|
|
147
|
-
|
|
148
|
-
```sh
|
|
149
|
-
cd packages/examples && OPENAI_API_KEY=… node basic/chat.ts
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
With a Restate server, and the agent registered with it: see
|
|
153
|
-
[Run an example](../../../examples/README.md#run-an-example).
|
|
154
|
-
[`examples/basic/chat.ts`](../../../examples/basic/chat.ts) is a chat agent with a
|
|
155
|
-
tool, and [`examples/intermediate/team.ts`](../../../examples/intermediate/team.ts) a pi
|
|
156
|
-
editor that hands tasks to agents of other frameworks.
|
|
157
|
-
|
|
158
|
-
## Tests
|
|
159
|
-
|
|
160
|
-
The tests run on a scripted model, [`test/fakes/pi.ts`](../../test/fakes/pi.ts),
|
|
161
|
-
which needs no key (`pnpm --filter @restatedev/agents test pi`, from the repository root):
|
|
162
|
-
|
|
163
|
-
- [`test/storage/pi.test.ts`](../../test/storage/pi.test.ts):
|
|
164
|
-
pi-durable's storage conformance suite on the object's database;
|
|
165
|
-
[`pi-workspace.test.ts`](../../test/storage/pi-workspace.test.ts),
|
|
166
|
-
`withPi` with a workspace.
|
|
167
|
-
- [`test/pi-bank.test.ts`](../../test/pi-bank.test.ts):
|
|
168
|
-
the bank agent, with crashes at any of a message's state requests.
|
|
169
|
-
- [`test/pi.test.ts`](../../test/pi.test.ts): the chat,
|
|
170
|
-
its system prompt, `messages`, and sub-agents.
|
|
171
|
-
- [`test/pi.ts`](../../test/pi.ts): the bank agent,
|
|
172
|
-
whose tools move money in the [ledger](../../../sessions/test/ledger.ts)
|
|
173
|
-
with the tool call id as the transfer id, through crashes in
|
|
174
|
-
[the crash test](../../../e2e/crash.ts) (`pnpm --filter e2e crash`).
|
package/src/pi/commits.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
// The default of the pi option `commits` (index.ts), in one place: the tests
|
|
2
|
-
// of the whole suite set it (test/commits-default.ts) to run the pi suites in
|
|
3
|
-
// both modes.
|
|
4
|
-
import type { Commits } from './storage.ts';
|
|
5
|
-
|
|
6
|
-
/** What `commits` is when an agent does not say. */
|
|
7
|
-
export const commitsDefault: { commits: Commits } = { commits: 'each' };
|
package/src/steering.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
// Steering: a message sent while a turn is answered joins it, at the turn's
|
|
2
|
-
// next step.
|
|
3
|
-
//
|
|
4
|
-
// A client sends the message to the agent's `steer` view. The companion
|
|
5
|
-
// queues it at once as a turn of its own (the agent's private `steered`), in
|
|
6
|
-
// order with the messages sent after it, and, if the turn being answered
|
|
7
|
-
// takes steered messages, holds it for that turn too (`steer` of
|
|
8
|
-
// companion.ts).
|
|
9
|
-
//
|
|
10
|
-
// The turn learns of it from its next `publish`, which it makes anyway as it
|
|
11
|
-
// starts and after each write of steps that commits (a reply with tool
|
|
12
|
-
// calls, the tools' results), and waits for (`Inbox` of common.ts). At its next step, once
|
|
13
|
-
// the model's reply has a result for each of its tool calls and before the
|
|
14
|
-
// next model request, the framework's integration calls `takeSteers`: the
|
|
15
|
-
// turn stores each message once, as a message of the user that is a step of
|
|
16
|
-
// the turn, and the integration puts it in the request about to go out. The
|
|
17
|
-
// transaction that stores them tells the companion they are taken (`taken`),
|
|
18
|
-
// once.
|
|
19
|
-
//
|
|
20
|
-
// Once the turn ends, the queued `steered` of a message it took does
|
|
21
|
-
// nothing; one it did not take (it came after the turn's last step, say) is
|
|
22
|
-
// answered as a turn of its own. So each message is answered once.
|
|
23
|
-
import type * as restate from '@restatedev/sessions';
|
|
24
|
-
import type { Transaction } from '@restatedev/sessions';
|
|
25
|
-
import { type AgentContext, inboxOf, stopSignalOf } from './common.ts';
|
|
26
|
-
import type { Conversation } from './conversation.ts';
|
|
27
|
-
import { companionHere } from './companion.ts';
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Take the messages steered to the turn being answered, at a step: store
|
|
31
|
-
* each once, and return their items.
|
|
32
|
-
*
|
|
33
|
-
* Call it right before a model request that follows the model's reply and
|
|
34
|
-
* its tools' results (or before the turn's first request): the integration
|
|
35
|
-
* then puts the returned items in the request, after what it already holds;
|
|
36
|
-
* one that builds each request from `conversation.history()` finds them
|
|
37
|
-
* there. They are messages of the user, as the framework's `Items` makes them
|
|
38
|
-
* (`steered`, or else `prompt`). It returns nothing, at no cost, unless the
|
|
39
|
-
* turn runs in a `chat` handler whose framework takes steered messages, a
|
|
40
|
-
* message it has not taken is waiting, the turn is not stopping, and each of
|
|
41
|
-
* its tool calls has a result.
|
|
42
|
-
*
|
|
43
|
-
* The messages are steps of the turn from then on: a retried attempt finds
|
|
44
|
-
* them stored, in `history()`, and never takes them again. They share the
|
|
45
|
-
* turn's fate: stopped, refused or failed with it, they are not answered
|
|
46
|
-
* again.
|
|
47
|
-
*/
|
|
48
|
-
export async function takeSteers<I>(ctx: restate.SessionContext, conversation: Conversation<I>): Promise<I[]> {
|
|
49
|
-
const inbox = inboxOf(ctx);
|
|
50
|
-
if (!inbox || inbox.messages.length === 0) return [];
|
|
51
|
-
const stop = stopSignalOf(ctx);
|
|
52
|
-
// A turn stopped while it waited to store them takes none: each is then answered as a turn of its own.
|
|
53
|
-
const going = () => stop?.aborted !== true;
|
|
54
|
-
const turn = conversation.running();
|
|
55
|
-
if (!going() || !turn || turn.openCalls().length > 0) return [];
|
|
56
|
-
return turn.steer(inbox.messages, (tx, ids) => tookSteers(ctx, tx, ids), going);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Tell the companion that the turn of `ctx` took the steered messages `ids`,
|
|
61
|
-
* with `tx`, the transaction that stores them: so they are marked taken once
|
|
62
|
-
* stored, and once. A framework that places steered messages itself (pi,
|
|
63
|
-
* DeepSeek) calls it as it copies them into the turn.
|
|
64
|
-
*/
|
|
65
|
-
export function tookSteers(ctx: AgentContext, tx: Transaction, ids: string[]) {
|
|
66
|
-
if (ids.length === 0) return;
|
|
67
|
-
tx.send(companionHere(ctx, 'steering'), ctx.key, { idempotencyKey: `taken:${ids[0]}` }).taken(ids);
|
|
68
|
-
}
|