@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.
Files changed (218) hide show
  1. package/README.md +189 -808
  2. package/dist/pi/factory.d.ts +73 -0
  3. package/dist/pi/factory.d.ts.map +1 -0
  4. package/dist/pi/factory.js +145 -0
  5. package/dist/pi/factory.js.map +1 -0
  6. package/dist/pi/index.d.ts +5 -129
  7. package/dist/pi/index.d.ts.map +1 -1
  8. package/dist/pi/index.js +31 -571
  9. package/dist/pi/index.js.map +1 -1
  10. package/dist/pi/keepalive.d.ts +3 -16
  11. package/dist/pi/keepalive.d.ts.map +1 -1
  12. package/dist/pi/keepalive.js +38 -41
  13. package/dist/pi/keepalive.js.map +1 -1
  14. package/dist/pi/left.d.ts +16 -0
  15. package/dist/pi/left.d.ts.map +1 -0
  16. package/dist/pi/left.js +23 -0
  17. package/dist/pi/left.js.map +1 -0
  18. package/dist/pi/storage.d.ts +19 -76
  19. package/dist/pi/storage.d.ts.map +1 -1
  20. package/dist/pi/storage.js +50 -368
  21. package/dist/pi/storage.js.map +1 -1
  22. package/dist/pi/tools.d.ts +12 -0
  23. package/dist/pi/tools.d.ts.map +1 -0
  24. package/dist/pi/tools.js +70 -0
  25. package/dist/pi/tools.js.map +1 -0
  26. package/package.json +11 -254
  27. package/src/pi/factory.ts +231 -0
  28. package/src/pi/index.ts +31 -759
  29. package/src/pi/keepalive.ts +46 -56
  30. package/src/pi/left.ts +52 -0
  31. package/src/pi/storage.ts +75 -409
  32. package/src/pi/tools.ts +78 -0
  33. package/dist/agentlib/agent.d.ts +0 -32
  34. package/dist/agentlib/agent.d.ts.map +0 -1
  35. package/dist/agentlib/agent.js +0 -174
  36. package/dist/agentlib/agent.js.map +0 -1
  37. package/dist/agentlib/index.d.ts +0 -5
  38. package/dist/agentlib/index.d.ts.map +0 -1
  39. package/dist/agentlib/index.js +0 -15
  40. package/dist/agentlib/index.js.map +0 -1
  41. package/dist/agentlib/messages.d.ts +0 -39
  42. package/dist/agentlib/messages.d.ts.map +0 -1
  43. package/dist/agentlib/messages.js +0 -177
  44. package/dist/agentlib/messages.js.map +0 -1
  45. package/dist/agentlib/model.d.ts +0 -31
  46. package/dist/agentlib/model.d.ts.map +0 -1
  47. package/dist/agentlib/model.js +0 -140
  48. package/dist/agentlib/model.js.map +0 -1
  49. package/dist/agentlib/options.d.ts +0 -58
  50. package/dist/agentlib/options.d.ts.map +0 -1
  51. package/dist/agentlib/options.js +0 -2
  52. package/dist/agentlib/options.js.map +0 -1
  53. package/dist/agentlib/runtime.d.ts +0 -37
  54. package/dist/agentlib/runtime.d.ts.map +0 -1
  55. package/dist/agentlib/runtime.js +0 -216
  56. package/dist/agentlib/runtime.js.map +0 -1
  57. package/dist/agentlib/state.d.ts +0 -114
  58. package/dist/agentlib/state.d.ts.map +0 -1
  59. package/dist/agentlib/state.js +0 -52
  60. package/dist/agentlib/state.js.map +0 -1
  61. package/dist/agentlib/steering.d.ts +0 -23
  62. package/dist/agentlib/steering.d.ts.map +0 -1
  63. package/dist/agentlib/steering.js +0 -48
  64. package/dist/agentlib/steering.js.map +0 -1
  65. package/dist/agentlib/tools.d.ts +0 -33
  66. package/dist/agentlib/tools.d.ts.map +0 -1
  67. package/dist/agentlib/tools.js +0 -245
  68. package/dist/agentlib/tools.js.map +0 -1
  69. package/dist/agentlib/turn.d.ts +0 -82
  70. package/dist/agentlib/turn.d.ts.map +0 -1
  71. package/dist/agentlib/turn.js +0 -253
  72. package/dist/agentlib/turn.js.map +0 -1
  73. package/dist/calls.d.ts +0 -90
  74. package/dist/calls.d.ts.map +0 -1
  75. package/dist/calls.js +0 -241
  76. package/dist/calls.js.map +0 -1
  77. package/dist/claude/index.d.ts +0 -106
  78. package/dist/claude/index.d.ts.map +0 -1
  79. package/dist/claude/index.js +0 -386
  80. package/dist/claude/index.js.map +0 -1
  81. package/dist/client.d.ts +0 -90
  82. package/dist/client.d.ts.map +0 -1
  83. package/dist/client.js +0 -88
  84. package/dist/client.js.map +0 -1
  85. package/dist/common.d.ts +0 -410
  86. package/dist/common.d.ts.map +0 -1
  87. package/dist/common.js +0 -563
  88. package/dist/common.js.map +0 -1
  89. package/dist/companion.d.ts +0 -286
  90. package/dist/companion.d.ts.map +0 -1
  91. package/dist/companion.js +0 -463
  92. package/dist/companion.js.map +0 -1
  93. package/dist/conversation.d.ts +0 -576
  94. package/dist/conversation.d.ts.map +0 -1
  95. package/dist/conversation.js +0 -1476
  96. package/dist/conversation.js.map +0 -1
  97. package/dist/deepseek/barriers.d.ts +0 -11
  98. package/dist/deepseek/barriers.d.ts.map +0 -1
  99. package/dist/deepseek/barriers.js +0 -36
  100. package/dist/deepseek/barriers.js.map +0 -1
  101. package/dist/deepseek/index.d.ts +0 -97
  102. package/dist/deepseek/index.d.ts.map +0 -1
  103. package/dist/deepseek/index.js +0 -603
  104. package/dist/deepseek/index.js.map +0 -1
  105. package/dist/deepseek/persistence.d.ts +0 -83
  106. package/dist/deepseek/persistence.d.ts.map +0 -1
  107. package/dist/deepseek/persistence.js +0 -262
  108. package/dist/deepseek/persistence.js.map +0 -1
  109. package/dist/index.d.ts +0 -14
  110. package/dist/index.d.ts.map +0 -1
  111. package/dist/index.js +0 -17
  112. package/dist/index.js.map +0 -1
  113. package/dist/langgraph/checkpointer.d.ts +0 -52
  114. package/dist/langgraph/checkpointer.d.ts.map +0 -1
  115. package/dist/langgraph/checkpointer.js +0 -477
  116. package/dist/langgraph/checkpointer.js.map +0 -1
  117. package/dist/langgraph/context.d.ts +0 -10
  118. package/dist/langgraph/context.d.ts.map +0 -1
  119. package/dist/langgraph/context.js +0 -21
  120. package/dist/langgraph/context.js.map +0 -1
  121. package/dist/langgraph/errors.d.ts +0 -16
  122. package/dist/langgraph/errors.d.ts.map +0 -1
  123. package/dist/langgraph/errors.js +0 -30
  124. package/dist/langgraph/errors.js.map +0 -1
  125. package/dist/langgraph/graph.d.ts +0 -25
  126. package/dist/langgraph/graph.d.ts.map +0 -1
  127. package/dist/langgraph/graph.js +0 -378
  128. package/dist/langgraph/graph.js.map +0 -1
  129. package/dist/langgraph/index.d.ts +0 -119
  130. package/dist/langgraph/index.d.ts.map +0 -1
  131. package/dist/langgraph/index.js +0 -325
  132. package/dist/langgraph/index.js.map +0 -1
  133. package/dist/langgraph/keepalive.d.ts +0 -16
  134. package/dist/langgraph/keepalive.d.ts.map +0 -1
  135. package/dist/langgraph/keepalive.js +0 -51
  136. package/dist/langgraph/keepalive.js.map +0 -1
  137. package/dist/langgraph/nodes.d.ts +0 -71
  138. package/dist/langgraph/nodes.d.ts.map +0 -1
  139. package/dist/langgraph/nodes.js +0 -148
  140. package/dist/langgraph/nodes.js.map +0 -1
  141. package/dist/messages.d.ts +0 -18
  142. package/dist/messages.d.ts.map +0 -1
  143. package/dist/messages.js +0 -182
  144. package/dist/messages.js.map +0 -1
  145. package/dist/object.d.ts +0 -213
  146. package/dist/object.d.ts.map +0 -1
  147. package/dist/object.js +0 -309
  148. package/dist/object.js.map +0 -1
  149. package/dist/opencode/db.d.ts +0 -81
  150. package/dist/opencode/db.d.ts.map +0 -1
  151. package/dist/opencode/db.js +0 -249
  152. package/dist/opencode/db.js.map +0 -1
  153. package/dist/opencode/index.d.ts +0 -184
  154. package/dist/opencode/index.d.ts.map +0 -1
  155. package/dist/opencode/index.js +0 -1079
  156. package/dist/opencode/index.js.map +0 -1
  157. package/dist/output.d.ts +0 -25
  158. package/dist/output.d.ts.map +0 -1
  159. package/dist/output.js +0 -99
  160. package/dist/output.js.map +0 -1
  161. package/dist/pi/commits.d.ts +0 -6
  162. package/dist/pi/commits.d.ts.map +0 -1
  163. package/dist/pi/commits.js +0 -3
  164. package/dist/pi/commits.js.map +0 -1
  165. package/dist/steering.d.ts +0 -32
  166. package/dist/steering.d.ts.map +0 -1
  167. package/dist/steering.js +0 -45
  168. package/dist/steering.js.map +0 -1
  169. package/dist/tools.d.ts +0 -267
  170. package/dist/tools.d.ts.map +0 -1
  171. package/dist/tools.js +0 -305
  172. package/dist/tools.js.map +0 -1
  173. package/dist/webhook.d.ts +0 -71
  174. package/dist/webhook.d.ts.map +0 -1
  175. package/dist/webhook.js +0 -102
  176. package/dist/webhook.js.map +0 -1
  177. package/src/agentlib/README.md +0 -229
  178. package/src/agentlib/agent.ts +0 -216
  179. package/src/agentlib/index.ts +0 -16
  180. package/src/agentlib/messages.ts +0 -207
  181. package/src/agentlib/model.ts +0 -156
  182. package/src/agentlib/options.ts +0 -62
  183. package/src/agentlib/runtime.ts +0 -252
  184. package/src/agentlib/state.ts +0 -93
  185. package/src/agentlib/steering.ts +0 -53
  186. package/src/agentlib/tools.ts +0 -355
  187. package/src/agentlib/turn.ts +0 -302
  188. package/src/calls.ts +0 -333
  189. package/src/claude/README.md +0 -107
  190. package/src/claude/index.ts +0 -487
  191. package/src/client.ts +0 -187
  192. package/src/common.ts +0 -766
  193. package/src/companion.ts +0 -675
  194. package/src/conversation.ts +0 -1799
  195. package/src/deepseek/README.md +0 -202
  196. package/src/deepseek/barriers.ts +0 -63
  197. package/src/deepseek/index.ts +0 -740
  198. package/src/deepseek/persistence.ts +0 -337
  199. package/src/index.ts +0 -41
  200. package/src/langgraph/README.md +0 -199
  201. package/src/langgraph/checkpointer.ts +0 -614
  202. package/src/langgraph/context.ts +0 -27
  203. package/src/langgraph/errors.ts +0 -26
  204. package/src/langgraph/graph.ts +0 -422
  205. package/src/langgraph/index.ts +0 -500
  206. package/src/langgraph/keepalive.ts +0 -58
  207. package/src/langgraph/nodes.ts +0 -247
  208. package/src/messages.ts +0 -244
  209. package/src/object.ts +0 -454
  210. package/src/opencode/README.md +0 -249
  211. package/src/opencode/db.ts +0 -387
  212. package/src/opencode/index.ts +0 -1339
  213. package/src/output.ts +0 -116
  214. package/src/pi/README.md +0 -174
  215. package/src/pi/commits.ts +0 -7
  216. package/src/steering.ts +0 -68
  217. package/src/tools.ts +0 -515
  218. package/src/webhook.ts +0 -161
package/dist/common.js DELETED
@@ -1,563 +0,0 @@
1
- // What the frameworks share: their options (`Configure`, the model), the
2
- // ids of an invocation's prompts, the `chat` handler with its turn (status,
3
- // cancellation, publishing for the clients that follow it), and what the
4
- // conversation of turns (conversation.ts) takes: its notes, window and
5
- // budget.
6
- import * as restate from '@restatedev/sessions';
7
- import { cancelCalls, cancelled, definitionOf, Link, ScopeKey } from '@restatedev/sessions/internal';
8
- import { flushCalls, flushHeld, settleCalls } from './calls.js';
9
- import { companionHere } from './companion.js';
10
- /** `value` for the invocation of `ctx`. */
11
- export const configured = async (value, ctx) => typeof value === 'function' ? value(ctx) : value;
12
- /** The prompts each invocation made so far, by its context. */
13
- const PROMPTS = new ScopeKey('nextPromptId');
14
- /**
15
- * The id of the next prompt of the invocation of `ctx`: the invocation's id,
16
- * then `<id>:2`, `<id>:3`... A retried attempt makes its prompts in the same
17
- * order, so each gets the same id again, and its answer.
18
- */
19
- export const nextPromptId = (ctx) => {
20
- const n = (PROMPTS.get(ctx) ?? 0) + 1;
21
- PROMPTS.set(ctx, n);
22
- return n === 1 ? ctx.request.id : `${ctx.request.id}:${n}`;
23
- };
24
- /** The default model of pi and LangGraph, with OPENAI_API_KEY. */
25
- export const DEFAULT_MODEL = 'openai/gpt-6-sol';
26
- /** The default model of Claude agents, with ANTHROPIC_API_KEY. */
27
- export const CLAUDE_MODEL = 'anthropic/claude-sonnet-5-5';
28
- /**
29
- * The model, as `provider/model-id` (such as `openai/gpt-6-sol`):
30
- * `model`, or else MODEL, or else the framework's default.
31
- */
32
- export const modelOf = (model, fallback = DEFAULT_MODEL) => model || process.env.MODEL || fallback;
33
- /** `provider/model-id` as its provider and model id, split at the first `/` (`openrouter/meta-llama/llama-4`). */
34
- export const providerOf = (model) => {
35
- const slash = model.indexOf('/');
36
- return slash < 0 ? ['', model] : [model.slice(0, slash), model.slice(slash + 1)];
37
- };
38
- /**
39
- * The error code of a message the model's provider refused (a missing or
40
- * wrong API key, an unknown model): 424, Failed Dependency, as its HTTP
41
- * status.
42
- */
43
- export const REFUSED = 424;
44
- /** The texts that approve a tool call that waits for a person's approval, whatever their case. */
45
- const APPROVED = new Set(['approved', 'approve', 'yes', 'ok', 'allow', 'allowed']);
46
- /**
47
- * Whether `value`, a signal's that a tool call waits for to run (an
48
- * approval), approves it: `true`, `{ "approved": true }`, or a text such as
49
- * "approved" or "yes", whatever its case. Anything else denies it, so a
50
- * value that does not say "approved" never runs the tool. Every framework's
51
- * approvals take it (DeepSeek's, OpenCode's, agentlib's).
52
- */
53
- export const approves = (value) => value === true ||
54
- (typeof value === 'string' && APPROVED.has(value.trim().toLowerCase())) ||
55
- (typeof value === 'object' && value !== null && value.approved === true);
56
- /** What to check when a model's provider refuses a request. */
57
- export const CHECK_MODEL = "check the agent's model (its `model` option, or MODEL) and its provider's API key";
58
- /**
59
- * An agent's model that it cannot take: not `provider/model-id`, another
60
- * provider's, or one its framework does not know. Terminal ({@link REFUSED}),
61
- * wherever it is thrown: a retry takes the same model.
62
- */
63
- export class ModelError extends restate.TerminalError {
64
- constructor(message) {
65
- super(message, { errorCode: REFUSED });
66
- this.name = 'ModelError';
67
- }
68
- }
69
- /** `model`, the explicit `model` option of `framework`'s agent, if it is not `provider/model-id`. */
70
- export const checkModelName = (framework, model) => {
71
- if (model !== undefined && !/^[^/\s]+\/\S+$/.test(model)) {
72
- throw new ModelError(`${framework}: the model "${model}" is not provider/model-id, such as openai/gpt-6-sol`);
73
- }
74
- };
75
- /**
76
- * The model id of `model`, for a framework with only `provider`'s models:
77
- * `provider/model-id`, or the id alone.
78
- */
79
- export const modelIdOf = (model, provider) => {
80
- const [named, id] = model.includes('/') ? providerOf(model) : [provider, model];
81
- if (named !== provider || !id) {
82
- const from = process.env.MODEL === model ? ' (from MODEL)' : '';
83
- throw new ModelError(`the model "${model}"${from} is not ${provider}'s: this agent takes ${provider}/<model-id>`);
84
- }
85
- return id;
86
- };
87
- /** A chat message: the request's body as text, or the string it is as JSON (`"Hi"`). */
88
- export const messageSerde = {
89
- contentType: 'text/plain',
90
- serialize: (text) => new TextEncoder().encode(text),
91
- deserialize: (bytes) => {
92
- const text = new TextDecoder().decode(bytes);
93
- try {
94
- const value = JSON.parse(text);
95
- return typeof value === 'string' ? value : text;
96
- }
97
- catch {
98
- return text;
99
- }
100
- },
101
- };
102
- /**
103
- * What an empty answer says, as the model sees it later in the conversation:
104
- * a provider's API may refuse an empty text (Anthropic's does).
105
- */
106
- export const NO_ANSWER = '(no answer)';
107
- /**
108
- * What a cancelled turn answers, and each tool call it cut off: the model
109
- * sees it in the conversation that goes on.
110
- */
111
- export const CANCELLED = 'Cancelled: the turn was stopped before it finished.';
112
- /**
113
- * What a prompt answers that the model's provider refused for good (see
114
- * {@link refusal}): the prompt is over, and the model sees it in the
115
- * conversation that goes on, rather than answer it with the next message.
116
- */
117
- export const UNANSWERED = "Unanswered: the model's provider refused the request.";
118
- /**
119
- * What a turn whose invocation ended without answering it (killed, say)
120
- * answers, and each tool call it cut off: the next message, or the companion
121
- * once it finds the invocation gone (`settle`), closes it with this.
122
- */
123
- export const ENDED = 'Ended: the turn ended before it finished, and was not answered.';
124
- /** The result of a tool call a stopped turn cut off once its Restate call had answered: it happened. */
125
- export const RETURNED_UNRECORDED = "Done, but the turn was stopped before this call's result was recorded: it did happen, so check its effect before doing it again.";
126
- /**
127
- * What a stopped turn records as the result of its tool call `callId`, which
128
- * has none: `text` (cancelled, say), unless the call's Restate call answered
129
- * (`outcomes` of calls.ts), which the model would take for undone, and do
130
- * again: then the error it answered with, or {@link RETURNED_UNRECORDED}.
131
- */
132
- export function cutOffResult(outcomes, callId, text) {
133
- if (!outcomes.has(callId))
134
- return text;
135
- return outcomes.get(callId) ?? RETURNED_UNRECORDED;
136
- }
137
- /** The window by default: the previous turn's tool calls and results stay in view, for a follow-up. */
138
- export const WINDOW = 1;
139
- /** How many tokens of the turns before the window the model sees, by default, counted as 4 characters each. */
140
- export const BUDGET = 32_000;
141
- /** What a request to summarize earlier turns starts with, before the turns themselves. */
142
- export const SUMMARIZE = "Summarize the conversation below, for the assistant who carries it on: keep the facts, names, numbers, decisions, the user's preferences, and what is still open. Answer with the summary only.\n\n";
143
- /** `window`, if it is one: a whole number of turns (0 or more), or `'all'`. */
144
- export function checkWindow(window) {
145
- if (window !== 'all' && !(Number.isInteger(window) && window >= 0)) {
146
- throw new Error(`window must be a number of turns (0 or more) or 'all', not ${JSON.stringify(window)}`);
147
- }
148
- return window;
149
- }
150
- /** What stops each turn, by its context: see {@link stopSignalOf}. */
151
- const TURN = new ScopeKey('stopSignalOf');
152
- /**
153
- * Why a turn stopped when its attempt ended before it did: Restate suspended
154
- * the attempt, gave up on it, or lost its connection. Nothing the attempt
155
- * does after is stored, and the next attempt goes on with the turn, so it
156
- * stops without answering: unlike a cancellation, nothing is cancelled.
157
- */
158
- export class AttemptEnded extends Error {
159
- constructor() {
160
- super('the attempt ended before its turn did');
161
- this.name = 'AttemptEnded';
162
- }
163
- }
164
- /**
165
- * The signal that stops the turn of `ctx` (of a `chat` handler,
166
- * `chatHandler`), and an agent's `prompt` stops its framework's work with it.
167
- * It aborts with a `restate.CancelledError` once Restate cancels the
168
- * invocation (the turn ends, answered as cancelled), or with an
169
- * {@link AttemptEnded} once the attempt ends first (the work stops, and
170
- * answers nothing).
171
- */
172
- export const stopSignalOf = (ctx) => TURN.get(ctx);
173
- // --- Keeping an attempt alive
174
- //
175
- // A model's request runs in `ctx.working`, so that Restate does not shut its
176
- // attempt down while it streams: the request would be made again, from the
177
- // start of the turn. Nothing else of a turn does, unless a tool says so
178
- // (`keepalive`): a wait on Restate (a call, a signal) suspends.
179
- /**
180
- * Open a `ctx.working` scope until the function it returns is called (once
181
- * or more): for a framework's hooks that bracket a model's request without
182
- * wrapping it. A scope never released keeps the attempt alive to its end.
183
- */
184
- export function keepAlive(ctx) {
185
- const released = Promise.withResolvers();
186
- void ctx.working(() => released.promise);
187
- return () => released.resolve();
188
- }
189
- /**
190
- * `source`, kept alive while it is iterated: from the first pull to its end,
191
- * however it ends (done, thrown, or returned early, as a `break` does). A
192
- * model's streamed response, say.
193
- */
194
- export async function* keptAlive(ctx, source) {
195
- const release = keepAlive(ctx);
196
- try {
197
- yield* source;
198
- }
199
- finally {
200
- release();
201
- }
202
- }
203
- /** An agent's settings, on its definition. */
204
- const AGENT = new Link('an agent');
205
- /** The items of a framework's `messages` view on the conversation of turns. */
206
- export const ITEMS = new Link('restate-agents.items');
207
- /** `definition` is an agent's, with these settings: its framework's items, if it is on the conversation of turns. */
208
- export const isAgent = (definition, { background = false, items = undefined, window = WINDOW, budget = BUDGET, summarize = undefined, }) => {
209
- return AGENT.on(definition, {
210
- background,
211
- items,
212
- window,
213
- budget,
214
- summarize,
215
- });
216
- };
217
- /** The settings of the agent of `ctx`, if it is one. */
218
- const agentOf = (ctx) => {
219
- const definition = definitionOf(ctx);
220
- return definition && AGENT.of(definition);
221
- };
222
- /** The settings of the agent of `ctx`: the defaults, for an object that is no agent. */
223
- export const settingsOf = (ctx) => agentOf(ctx) ?? { background: false, window: WINDOW, budget: BUDGET };
224
- /** Whether the agent of `ctx` runs in the background. */
225
- const unfollowed = (ctx) => agentOf(ctx)?.background === true;
226
- /**
227
- * The companion of the agent of `ctx`, for `needs`: its own, at its key. An
228
- * object without one fails with a terminal error (`NoCompanion`).
229
- */
230
- const companionOfAgent = (ctx, needs) => ctx.rpc(companionHere(ctx, needs), ctx.key);
231
- /**
232
- * The messages steered to a `chat` turn that it has not taken yet, as the
233
- * companion last published them. The companion answers them to every
234
- * `publish` (and `start`), which a turn makes as it starts and after each
235
- * write of its steps that commits: so the turn knows of them without a call
236
- * of its own, and finds them at its next step (`takeSteers`). The newest
237
- * revision wins, as writes that run at once publish at once.
238
- */
239
- export class Inbox {
240
- revision = -1;
241
- messages = [];
242
- #listeners = new Set();
243
- /** Keep what `publish` answered, if it is newer than what is kept, and tell the listeners if it lists messages. */
244
- saw(published) {
245
- if (typeof published !== 'object' || published === null)
246
- return;
247
- const revision = published.revision ?? -1;
248
- if (revision <= this.revision)
249
- return;
250
- this.revision = revision;
251
- this.messages = [...(published.steering ?? [])];
252
- if (this.messages.length > 0)
253
- for (const listener of this.#listeners)
254
- listener();
255
- }
256
- /**
257
- * Call `listener` after each publish that lists messages, until the
258
- * returned function is called: for a framework that takes steered
259
- * messages through an inbox of its own (pi's, DeepSeek's), handed to it as
260
- * they come.
261
- */
262
- listen(listener) {
263
- this.#listeners.add(listener);
264
- return () => this.#listeners.delete(listener);
265
- }
266
- }
267
- const INBOX = new ScopeKey('inbox');
268
- /** The inbox of the `chat` turn running on `ctx`, if it takes steered messages: none outside one. */
269
- export const inboxOf = (ctx) => INBOX.get(ctx);
270
- /**
271
- * The conversation of `ctx` changed (see `watch` in object.ts): its revision
272
- * increases, and the clients that follow it read it again; while a framework
273
- * answers ({@link answering}), the calls whose results it stored are
274
- * forgotten first. It is a call to Restate, so it also sends what the
275
- * handler wrote before, for them to read: the rows the conversation holds
276
- * back are written first (`flushHeld`), as the clients read them once told.
277
- *
278
- * Only a `chat` turn ({@link chatTurn}: after each commit of its steps) and
279
- * `settle` publish, on an object with a companion: one without fails with a
280
- * terminal error (`NoCompanion`), rather than call a companion nobody serves.
281
- */
282
- export const publish = async (ctx) => {
283
- const companion = companionOfAgent(ctx, 'a publish');
284
- await flushHeld(ctx);
285
- const published = await companion.publish();
286
- INBOX.get(ctx)?.saw(published);
287
- };
288
- /**
289
- * What the turn running on `ctx` makes after each commit of its steps (a
290
- * reply with tool calls, the tools' results), if anything: set by
291
- * {@link chatTurn}, for a followed turn, to publish. The conversation takes
292
- * it as it is, without knowing what it does (`answerOnce` hands it to
293
- * `Conversation.hold`); a turn run outside a `chat` turn (a handler of your
294
- * own that calls a framework's `prompt()`) has none: it stores its steps,
295
- * and publishes nothing.
296
- */
297
- const AFTER_STEPS = new ScopeKey('afterSteps');
298
- /** What the turn running on `ctx` makes after each commit of its steps, if anything: see {@link AFTER_STEPS}. */
299
- export const afterStepsOf = (ctx) => AFTER_STEPS.get(ctx);
300
- /**
301
- * A tool call of the turn of `ctx` starts: forget the calls whose results are
302
- * stored (calls.ts) before the tool makes its own. It publishes nothing: a
303
- * followed `chat` turn publishes once the call is stored (with its reply),
304
- * and once its result is ({@link afterStepsOf}).
305
- */
306
- export async function toolCallStarted(ctx) {
307
- await settleAnswered(ctx);
308
- }
309
- /**
310
- * Forget the call records (calls.ts) whose results the turn answering on
311
- * `ctx` has stored ({@link answering}), if one does: after each commit of its
312
- * steps (`answerOnce` does, before what the turn asked for then, such as a
313
- * `chat` turn's publish, for every turn, in a `chat` turn or not), as a tool
314
- * call starts, and as the turn ends.
315
- */
316
- export async function settleAnswered(ctx) {
317
- const stored = ANSWERING.get(ctx);
318
- if (stored)
319
- await settled(ctx, stored);
320
- }
321
- /** What the attempt's turn has stored, while it answers (see {@link answering}). */
322
- const ANSWERING = new ScopeKey('answering');
323
- /** The attempt's latest settling of calls: one after another, as parallel tool calls publish at once. */
324
- const SETTLING = new ScopeKey('settling');
325
- /** Forget the calls whose results the conversation stored (calls.ts), after any settling before. */
326
- function settled(ctx, stored) {
327
- const settle = () => settleCalls(ctx, stored);
328
- const done = (SETTLING.get(ctx) ?? Promise.resolve()).then(settle, settle);
329
- SETTLING.set(ctx, done);
330
- return done;
331
- }
332
- /**
333
- * Run `work`, a framework's turn on the conversation of turns, forgetting
334
- * the calls whose results it stores as it goes: each write of its steps
335
- * that commits ({@link settleAnswered}), each tool call's start, and its
336
- * end, forget the Restate calls of
337
- * the tool calls whose results `stored` says are stored (calls.ts).
338
- */
339
- export async function answering(ctx, stored, work) {
340
- const outer = ANSWERING.get(ctx);
341
- ANSWERING.set(ctx, stored);
342
- let failed = false;
343
- try {
344
- return await work();
345
- }
346
- catch (error) {
347
- failed = !(error instanceof restate.TerminalError);
348
- throw error;
349
- }
350
- finally {
351
- try {
352
- // A failed attempt stores nothing more: its retry runs again.
353
- if (!failed)
354
- await settled(ctx, stored);
355
- }
356
- finally {
357
- if (outer)
358
- ANSWERING.set(ctx, outer);
359
- else
360
- ANSWERING.delete(ctx);
361
- // What rides the next commit is written if none is to come: after a turn that answered, the commit that
362
- // stored its answer wrote it, and this does nothing. A failed attempt's own error is not this write's.
363
- await (failed ? flushCalls(ctx).catch(() => { }) : flushCalls(ctx));
364
- }
365
- }
366
- }
367
- /** A promise that rejects with `signal`'s reason once it aborts. */
368
- export const aborted = (signal) => new Promise((_, reject) => {
369
- if (signal.aborted)
370
- reject(signal.reason);
371
- else
372
- signal.addEventListener('abort', () => reject(signal.reason), { once: true });
373
- });
374
- /** What makes a `chat` handler (`chatHandler`): its answer, and whether it takes steered messages. */
375
- const CHAT = new Link('restate-agents.chat');
376
- /** The answer of `entry`, a handler that `chatHandler` made, and whether it takes steered messages. */
377
- export const chatOf = (entry) => CHAT.of(entry);
378
- /**
379
- * `answer` as a chat handler, which takes its message as plain text or as a
380
- * JSON string, so that both of these work:
381
- *
382
- * curl localhost:8080/assistant/alice/chat -d 'Hi'
383
- * curl localhost:8080/assistant/alice/chat --json '"Hi"'
384
- *
385
- * While it answers, the object's `status` says so (its {@link Answering}). Its
386
- * invocation's cancellation (Restate's, explicit: `cancelled(ctx)` of
387
- * @restatedev/sessions/internal) aborts {@link stopSignalOf} its
388
- * context, whenever it comes, and cancels the calls its tools made
389
- * (`cancelCalls(ctx)`): the agent's `prompt`
390
- * ends the turn, and the handler fails with a `restate.CancelledError`. An
391
- * attempt that ends first (`attemptCompletedSignal`) aborts it too, with an
392
- * {@link AttemptEnded}: the work stops, and the next attempt goes on with it.
393
- *
394
- * It publishes when it starts (its companion's `start` does) and ends
395
- * ({@link publish}), for the clients that follow the conversation, unless
396
- * the agent runs in the background, which publishes the end only.
397
- *
398
- * As the `chat` handler of an agent, it also answers each message sent to
399
- * the agent's `steer` view that no turn took, as a turn of its own (the
400
- * private `steered` handler). With `steers` (a framework's integration says
401
- * so), its turns take steered messages at their steps (`takeSteers`).
402
- */
403
- export const chatHandler = (answer, { steers = false } = {}) => CHAT.on({
404
- input: messageSerde,
405
- accept: '*/*',
406
- explicitCancellation: true,
407
- handler: (ctx, text) => chatTurn(ctx, text, answer, steers),
408
- }, { answer, steers });
409
- /**
410
- * Answer `text` as a turn of the agent of `ctx`, with `answer`: the body of a
411
- * `chat` handler ({@link chatHandler}), and of `steered`. With `steers`, the
412
- * turn takes the messages steered to it at its steps (unless the agent runs
413
- * in the background).
414
- */
415
- export async function chatTurn(ctx, text, answer, steers = false) {
416
- const invocationId = ctx.request.id;
417
- // The companion knows the turn from here to its close: `status` and `stop` ask it.
418
- // An agent on the conversation of turns has `settle`: the companion sends it
419
- // this turn, should its invocation be gone without closing it.
420
- const settles = agentOf(ctx)?.items !== undefined;
421
- const steering = !unfollowed(ctx) && (typeof steers === 'function' ? await steers(ctx) : steers);
422
- // Before the first publish, which may answer messages steered to the turn already (a retried attempt's).
423
- if (steering)
424
- INBOX.set(ctx, new Inbox());
425
- // A followed turn's start is its first publish: the companion makes it, and returns what `publish` returns.
426
- const followed = !unfollowed(ctx);
427
- const companion = companionOfAgent(ctx, 'a chat turn');
428
- const started = await companion.start({
429
- invocationId,
430
- since: new Date().toISOString(),
431
- ...(settles ? { settles } : {}),
432
- ...(steering ? { steers: steering } : {}),
433
- ...(followed ? { publishes: true } : {}),
434
- });
435
- INBOX.get(ctx)?.saw(started);
436
- // A followed turn publishes after each commit of its steps, for its
437
- // followers to read them there: the conversation makes what the turn sets
438
- // here (`afterStepsOf`), without knowing it publishes. A background turn
439
- // publishes its end only.
440
- if (followed)
441
- AFTER_STEPS.set(ctx, () => publish(ctx));
442
- // The log takes the turn's last change, then the companion closes the
443
- // turn, which publishes it: one revision. The close is a call the turn
444
- // waits for, so that it is done before the turn ends (see `awaitTurn`).
445
- const end = async () => {
446
- await settleAnswered(ctx);
447
- await companion.close(invocationId);
448
- };
449
- const turn = new AbortController();
450
- TURN.set(ctx, turn.signal);
451
- void (async () => {
452
- await cancelled(ctx);
453
- turn.abort(new restate.CancelledError());
454
- await cancelCalls(ctx);
455
- })().catch(() => { });
456
- // The attempt ends before the turn does: its work stops (a model's
457
- // request, say), rather than run on for an attempt that can store nothing.
458
- const attempt = ctx.request.attemptCompletedSignal;
459
- const attemptEnded = () => turn.abort(new AttemptEnded());
460
- attempt.addEventListener('abort', attemptEnded, { once: true });
461
- let answered;
462
- try {
463
- answered = await answer(ctx, text);
464
- // Cancelled once answered: the answer stands.
465
- }
466
- catch (error) {
467
- if (error instanceof restate.TerminalError) {
468
- await end();
469
- throw error;
470
- }
471
- // Any other error fails the attempt, and Restate retries the turn.
472
- const terminal = refusalOf(ctx, error);
473
- if (!terminal)
474
- throw error;
475
- await end();
476
- throw terminal;
477
- }
478
- finally {
479
- // An attempt's signal aborts at every attempt's end, the turn's too.
480
- attempt.removeEventListener('abort', attemptEnded);
481
- AFTER_STEPS.delete(ctx);
482
- }
483
- await end();
484
- return answered;
485
- }
486
- /** A tool's error, which fails the attempt (Restate retries it): the error `cause` of tool `tool`. */
487
- export class ToolError extends Error {
488
- tool;
489
- constructor(tool, cause) {
490
- super(`the tool ${tool} failed: ${cause instanceof Error ? cause.message : String(cause)}`, { cause });
491
- this.name = 'ToolError';
492
- this.tool = tool;
493
- }
494
- }
495
- /** The errors the attempt's framework tools (not `tool()`'s) failed with: see {@link fromTool}. */
496
- const TOOL_ERRORS = new ScopeKey('fromTool');
497
- /**
498
- * `error` is a tool's, in the attempt of `ctx`: a framework's own tool
499
- * (Claude's, LangChain's...) that failed, whose error is never taken for
500
- * the model's provider refusing (a tool that calls a model API may fail as
501
- * one does). `tool()`'s are {@link ToolError}s.
502
- */
503
- export function fromTool(ctx, error) {
504
- if (typeof error === 'object' && error !== null)
505
- TOOL_ERRORS.of(ctx, () => new WeakSet()).add(error);
506
- }
507
- /** The Anthropic SDK's errors of a request that fails again however often it is made. */
508
- const FOR_GOOD = new Set([
509
- 'AuthenticationError',
510
- 'PermissionDeniedError',
511
- 'NotFoundError',
512
- 'BadRequestError',
513
- ]);
514
- /**
515
- * Whether `error` is a model provider's refusal that a retry does not change:
516
- * a missing or wrong API key, an unknown model, a bad request. Other errors
517
- * (a tool's, an overloaded provider's) fail the attempt, and Restate retries.
518
- */
519
- function refused(error) {
520
- if (error instanceof ToolError)
521
- return false;
522
- if (error instanceof Error && FOR_GOOD.has(error.constructor.name))
523
- return true;
524
- return /API key not found|no API key|Could not resolve authentication method|Provider is not configured|"code":"(AUTH|MISSING_CREDENTIAL)"/i.test(String(error));
525
- }
526
- /** {@link refusal}, but not of an error a framework's tool failed with in the attempt of `ctx` ({@link fromTool}). */
527
- export function refusalOf(ctx, error) {
528
- const failedTool = typeof error === 'object' && error !== null && TOOL_ERRORS.get(ctx)?.has(error);
529
- return failedTool ? undefined : refusal(error);
530
- }
531
- /**
532
- * `error` as a terminal error ({@link REFUSED}), if a retry does not change
533
- * it: a model the agent cannot take, or its provider's refusal.
534
- */
535
- export function refusal(error) {
536
- if (error instanceof ModelError)
537
- return error;
538
- if (error instanceof restate.TerminalError || !refused(error))
539
- return undefined;
540
- return new restate.TerminalError(`the model's provider refused the request: ${CHECK_MODEL}. ${String(error)}`, {
541
- errorCode: REFUSED,
542
- });
543
- }
544
- /**
545
- * A prompt's stored answer, as the prompt returns it, by how its turn ended
546
- * (`status`): a turn refused ({@link UNANSWERED}) fails with
547
- * {@link REFUSED}, one failed for good with its error, one cancelled with
548
- * the cancellation, one another invocation ended with its note; so that a
549
- * retry after a crash right after the close fails as the prompt did.
550
- */
551
- export function storedAnswer(answer, status = 'answered') {
552
- if (status === 'refused') {
553
- throw new restate.TerminalError(`the model's provider refused the request: ${CHECK_MODEL}`, { errorCode: REFUSED });
554
- }
555
- if (status === 'cancelled')
556
- throw new restate.CancelledError();
557
- if (status === 'failed')
558
- throw new restate.TerminalError(answer.replace(/^Error: /, ''));
559
- if (status === 'ended')
560
- throw new restate.TerminalError(answer);
561
- return answer;
562
- }
563
- //# sourceMappingURL=common.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"common.js","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,4EA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