@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.
Files changed (136) hide show
  1. package/README.md +107 -53
  2. package/dist/calls.d.ts +29 -5
  3. package/dist/calls.d.ts.map +1 -1
  4. package/dist/calls.js +77 -10
  5. package/dist/calls.js.map +1 -1
  6. package/dist/claude/index.d.ts +36 -7
  7. package/dist/claude/index.d.ts.map +1 -1
  8. package/dist/claude/index.js +232 -151
  9. package/dist/claude/index.js.map +1 -1
  10. package/dist/client.d.ts +12 -3
  11. package/dist/client.d.ts.map +1 -1
  12. package/dist/client.js.map +1 -1
  13. package/dist/common.d.ts +151 -70
  14. package/dist/common.d.ts.map +1 -1
  15. package/dist/common.js +246 -204
  16. package/dist/common.js.map +1 -1
  17. package/dist/conversation.d.ts +478 -0
  18. package/dist/conversation.d.ts.map +1 -0
  19. package/dist/conversation.js +1256 -0
  20. package/dist/conversation.js.map +1 -0
  21. package/dist/deepseek/index.d.ts +38 -5
  22. package/dist/deepseek/index.d.ts.map +1 -1
  23. package/dist/deepseek/index.js +322 -106
  24. package/dist/deepseek/index.js.map +1 -1
  25. package/dist/deepseek/persistence.d.ts +9 -3
  26. package/dist/deepseek/persistence.d.ts.map +1 -1
  27. package/dist/deepseek/persistence.js +12 -6
  28. package/dist/deepseek/persistence.js.map +1 -1
  29. package/dist/index.d.ts +2 -2
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +2 -3
  32. package/dist/index.js.map +1 -1
  33. package/dist/langgraph/checkpointer.d.ts +16 -2
  34. package/dist/langgraph/checkpointer.d.ts.map +1 -1
  35. package/dist/langgraph/checkpointer.js +133 -26
  36. package/dist/langgraph/checkpointer.js.map +1 -1
  37. package/dist/langgraph/context.d.ts +10 -0
  38. package/dist/langgraph/context.d.ts.map +1 -0
  39. package/dist/langgraph/context.js +21 -0
  40. package/dist/langgraph/context.js.map +1 -0
  41. package/dist/langgraph/errors.d.ts +16 -0
  42. package/dist/langgraph/errors.d.ts.map +1 -0
  43. package/dist/langgraph/errors.js +30 -0
  44. package/dist/langgraph/errors.js.map +1 -0
  45. package/dist/langgraph/graph.d.ts +25 -0
  46. package/dist/langgraph/graph.d.ts.map +1 -0
  47. package/dist/langgraph/graph.js +346 -0
  48. package/dist/langgraph/graph.js.map +1 -0
  49. package/dist/langgraph/index.d.ts +65 -12
  50. package/dist/langgraph/index.d.ts.map +1 -1
  51. package/dist/langgraph/index.js +227 -171
  52. package/dist/langgraph/index.js.map +1 -1
  53. package/dist/langgraph/nodes.d.ts +70 -0
  54. package/dist/langgraph/nodes.d.ts.map +1 -0
  55. package/dist/langgraph/nodes.js +145 -0
  56. package/dist/langgraph/nodes.js.map +1 -0
  57. package/dist/messages.d.ts +18 -0
  58. package/dist/messages.d.ts.map +1 -0
  59. package/dist/messages.js +182 -0
  60. package/dist/messages.js.map +1 -0
  61. package/dist/object.d.ts +65 -23
  62. package/dist/object.d.ts.map +1 -1
  63. package/dist/object.js +123 -35
  64. package/dist/object.js.map +1 -1
  65. package/dist/opencode/db.d.ts +51 -10
  66. package/dist/opencode/db.d.ts.map +1 -1
  67. package/dist/opencode/db.js +122 -34
  68. package/dist/opencode/db.js.map +1 -1
  69. package/dist/opencode/index.d.ts +105 -21
  70. package/dist/opencode/index.d.ts.map +1 -1
  71. package/dist/opencode/index.js +827 -279
  72. package/dist/opencode/index.js.map +1 -1
  73. package/dist/output.d.ts +15 -7
  74. package/dist/output.d.ts.map +1 -1
  75. package/dist/output.js +44 -26
  76. package/dist/output.js.map +1 -1
  77. package/dist/pi/index.d.ts +39 -11
  78. package/dist/pi/index.d.ts.map +1 -1
  79. package/dist/pi/index.js +281 -77
  80. package/dist/pi/index.js.map +1 -1
  81. package/dist/pi/storage.d.ts +29 -4
  82. package/dist/pi/storage.d.ts.map +1 -1
  83. package/dist/pi/storage.js +18 -6
  84. package/dist/pi/storage.js.map +1 -1
  85. package/dist/signals.d.ts +74 -4
  86. package/dist/signals.d.ts.map +1 -1
  87. package/dist/signals.js +144 -15
  88. package/dist/signals.js.map +1 -1
  89. package/dist/steering.d.ts +31 -0
  90. package/dist/steering.d.ts.map +1 -0
  91. package/dist/steering.js +45 -0
  92. package/dist/steering.js.map +1 -0
  93. package/dist/tools.d.ts +28 -0
  94. package/dist/tools.d.ts.map +1 -1
  95. package/dist/tools.js +34 -5
  96. package/dist/tools.js.map +1 -1
  97. package/package.json +42 -13
  98. package/src/calls.ts +107 -15
  99. package/src/claude/README.md +41 -20
  100. package/src/claude/index.ts +266 -178
  101. package/src/client.ts +14 -3
  102. package/src/common.ts +317 -232
  103. package/src/conversation.ts +1547 -0
  104. package/src/deepseek/README.md +78 -11
  105. package/src/deepseek/index.ts +384 -137
  106. package/src/deepseek/persistence.ts +11 -8
  107. package/src/index.ts +13 -4
  108. package/src/langgraph/README.md +115 -25
  109. package/src/langgraph/checkpointer.ts +168 -28
  110. package/src/langgraph/context.ts +27 -0
  111. package/src/langgraph/errors.ts +26 -0
  112. package/src/langgraph/graph.ts +396 -0
  113. package/src/langgraph/index.ts +326 -181
  114. package/src/langgraph/nodes.ts +243 -0
  115. package/src/messages.ts +244 -0
  116. package/src/object.ts +169 -38
  117. package/src/opencode/README.md +172 -66
  118. package/src/opencode/db.ts +204 -40
  119. package/src/opencode/index.ts +1010 -325
  120. package/src/output.ts +52 -31
  121. package/src/pi/README.md +18 -3
  122. package/src/pi/index.ts +346 -78
  123. package/src/pi/storage.ts +20 -7
  124. package/src/signals.ts +225 -19
  125. package/src/steering.ts +67 -0
  126. package/src/tools.ts +61 -8
  127. package/dist/claude/conversation.d.ts +0 -33
  128. package/dist/claude/conversation.d.ts.map +0 -1
  129. package/dist/claude/conversation.js +0 -95
  130. package/dist/claude/conversation.js.map +0 -1
  131. package/dist/log.d.ts +0 -29
  132. package/dist/log.d.ts.map +0 -1
  133. package/dist/log.js +0 -146
  134. package/dist/log.js.map +0 -1
  135. package/src/claude/conversation.ts +0 -114
  136. 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: the system prompt
3
- // asks for it, with the schema, and each prompt again, at its end; the reply
4
- // is checked against it. The
5
- // prompt (and so `chat`) then answers with the value, as an RPC answers.
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. A retried attempt resumes the conversation, so it reads the
13
- // same reply.
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 ends with, with `output`: the system prompt's ask, again,
27
- * where an agent reads it last (after a long run of tool calls, it may have
28
- * drifted from its system prompt to a summary in prose).
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
- * Answer `text` with a value of `output`: the agent's reply to it, parsed;
65
- * one that is not is answered with what is wrong, once, for another reply.
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 async function answerWith<O extends z.ZodObject>(
68
- text: string,
69
- output: O,
70
- prompt: (text: string) => Promise<string>,
71
- ): Promise<z.infer<O>> {
72
- let answer = parsed(output, await prompt(text));
73
- if ('wrong' in answer) {
74
- const again = `Your reply is not the answer asked for: ${answer.wrong}\nReply again, with the JSON only.`;
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`), answering with a value of
84
- * `options.output` if it has one, or else with the agent's text.
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 withOutput = <T extends { system?: string; output?: z.ZodObject }>(
97
+ export const withChecked = async <T extends { system?: string; output?: z.ZodObject }>(
87
98
  text: string,
88
99
  options: T,
89
- prompt: (text: string, options: T) => Promise<string>,
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 = { ...options, system: [options.system, format(output)].filter(Boolean).join('\n\n') };
94
- return answerWith(text + reminder, output, (t) => prompt(t, asked)) 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']>;
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/coding.ts`](../../../examples/intermediate/coding.ts) a pi
87
- coordinator that hands coding tasks to OpenCode coders.
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