@restatedev/agents 0.3.0 → 0.5.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 +107 -53
- 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 +478 -0
- package/dist/conversation.d.ts.map +1 -0
- package/dist/conversation.js +1256 -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 +123 -35
- package/dist/object.js.map +1 -1
- package/dist/opencode/db.d.ts +51 -10
- package/dist/opencode/db.d.ts.map +1 -1
- package/dist/opencode/db.js +122 -34
- package/dist/opencode/db.js.map +1 -1
- package/dist/opencode/index.d.ts +105 -21
- package/dist/opencode/index.d.ts.map +1 -1
- package/dist/opencode/index.js +827 -279
- package/dist/opencode/index.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 +42 -13
- 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 +1547 -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 +169 -38
- package/src/opencode/README.md +172 -66
- package/src/opencode/db.ts +204 -40
- package/src/opencode/index.ts +1010 -325
- 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/src/claude/conversation.ts +0 -114
- package/src/log.ts +0 -194
package/src/output.ts
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
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
|
|
|
@@ -23,9 +26,11 @@ const format = (output: z.ZodObject) =>
|
|
|
23
26
|
JSON.stringify(z.toJSONSchema(output));
|
|
24
27
|
|
|
25
28
|
/**
|
|
26
|
-
* What each prompt
|
|
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.
|
|
29
34
|
*/
|
|
30
35
|
const reminder =
|
|
31
36
|
'\n\nWhen you are done, reply with your answer as JSON only, of the schema you were given: no other text.';
|
|
@@ -60,36 +65,52 @@ function parsed<O extends z.ZodObject>(output: O, reply: string): { value: z.inf
|
|
|
60
65
|
return result.success ? { value: result.data } : { wrong: z.prettifyError(result.error) };
|
|
61
66
|
}
|
|
62
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
|
+
|
|
63
72
|
/**
|
|
64
|
-
*
|
|
65
|
-
* one
|
|
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.
|
|
66
76
|
*/
|
|
67
|
-
export
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
)
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
answer = parsed(output, await prompt(again));
|
|
76
|
-
}
|
|
77
|
-
if ('wrong' in answer)
|
|
78
|
-
throw new restate.TerminalError(`The agent's reply is not the answer asked for: ${answer.wrong}`);
|
|
79
|
-
return answer.value;
|
|
80
|
-
}
|
|
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
|
+
};
|
|
81
85
|
|
|
82
86
|
/**
|
|
83
|
-
* `prompt` (a framework's, of `options`),
|
|
84
|
-
* `options.output` if it has one, or else with the
|
|
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.
|
|
85
96
|
*/
|
|
86
|
-
export const
|
|
97
|
+
export const withChecked = async <T extends { system?: string; output?: z.ZodObject }>(
|
|
87
98
|
text: string,
|
|
88
99
|
options: T,
|
|
89
|
-
prompt: (
|
|
100
|
+
prompt: (
|
|
101
|
+
text: string,
|
|
102
|
+
options: T,
|
|
103
|
+
check?: (answer: string) => string | undefined,
|
|
104
|
+
reminder?: string,
|
|
105
|
+
) => Promise<string>,
|
|
106
|
+
native = false,
|
|
90
107
|
): Promise<Answer<T['output']>> => {
|
|
91
108
|
const { output } = options;
|
|
92
109
|
if (!output) return prompt(text, options) as Promise<Answer<T['output']>>;
|
|
93
|
-
const asked =
|
|
94
|
-
|
|
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']>;
|
|
95
116
|
};
|
package/src/pi/README.md
CHANGED
|
@@ -26,6 +26,7 @@ await serve(assistant);
|
|
|
26
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
27
|
| `workspace` | A directory for pi-durable's coding tools (`read`, `write`, `edit`, `bash`), on the local file system; use a sandbox |
|
|
28
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 |
|
|
29
30
|
|
|
30
31
|
For a session object of your own, `pi.chat(options)` is the handler and
|
|
31
32
|
`pi.messages()` the view; `options` may be a function of the invocation's context, such as
|
|
@@ -68,7 +69,21 @@ handler does. A retried attempt opens the Harness again and carries on:
|
|
|
68
69
|
`Infinity`: Restate's retry policy decides);
|
|
69
70
|
- a tool's error fails the attempt, and Restate retries it (pi-durable would
|
|
70
71
|
hand it to the model); a `restate.TerminalError` of a `@restatedev/agents`
|
|
71
|
-
tool is its answer to the model
|
|
72
|
+
tool is its answer to the model;
|
|
73
|
+
- a provider's outage never ends the turn: pi asks the model again until it
|
|
74
|
+
answers (pi-durable gives up after 3 retries; `retry` sets how often), and
|
|
75
|
+
each retry is committed, so a retried attempt waits as the attempt before
|
|
76
|
+
it would have; a request the provider refuses for good (a wrong API key)
|
|
77
|
+
ends the turn, with a terminal error that says what to check;
|
|
78
|
+
- a run a killed invocation left is aborted, never finished: its turn is
|
|
79
|
+
closed (`ended`) in the agent's conversation of turns, and the next
|
|
80
|
+
prompt tells the model how it ended.
|
|
81
|
+
|
|
82
|
+
pi keeps its own history (and compacts it); each pi commit of a prompt's run
|
|
83
|
+
copies its new entries (the model's replies, the tools' results) into the
|
|
84
|
+
agent's conversation of turns, in the same transaction: what `messages`
|
|
85
|
+
shows ([docs/conversation.md](../../docs/conversation.md)). The views show
|
|
86
|
+
each turn as it was asked and answered, not pi's summaries and handoffs.
|
|
72
87
|
|
|
73
88
|
The files of a `workspace` are not in the database: they last as long as the
|
|
74
89
|
disk does, and a coding tool a crash cut off is not run again (the model is
|
|
@@ -83,8 +98,8 @@ cd packages/examples && OPENAI_API_KEY=… node basic/chat.ts
|
|
|
83
98
|
With a Restate server, and the agent registered with it: see
|
|
84
99
|
[Run an example](../../../examples/README.md#run-an-example).
|
|
85
100
|
[`examples/basic/chat.ts`](../../../examples/basic/chat.ts) is a chat agent with a
|
|
86
|
-
tool, and [`examples/intermediate/
|
|
87
|
-
|
|
101
|
+
tool, and [`examples/intermediate/team.ts`](../../../examples/intermediate/team.ts) a pi
|
|
102
|
+
editor that hands tasks to agents of other frameworks.
|
|
88
103
|
|
|
89
104
|
## Tests
|
|
90
105
|
|