@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.d.ts DELETED
@@ -1,410 +0,0 @@
1
- import * as restate from '@restatedev/sessions';
2
- import { Link } from '@restatedev/sessions/internal';
3
- import { type Published, type Steer } from './companion.ts';
4
- /**
5
- * The context of an agent's handler: a session context, from an object that
6
- * has a companion.
7
- *
8
- * The agent's own surface takes it: the handlers and views of
9
- * `sessionObject()` and of a framework's `agent()`, a `chat` handler's
10
- * answer, and what only an agent does with a context ({@link chatTurn},
11
- * {@link publish}). The layer a handler belongs to is then in its
12
- * signature, where the person writing it reads it, rather than found out in
13
- * the middle of a turn, after a model round-trip, looking like a tool
14
- * failure.
15
- *
16
- * It is a signpost, not a fence: it adds nothing to a session context, so
17
- * the two are the same type to the compiler, and `NoCompanion` stays the
18
- * guard that stops a wait from an object without a companion.
19
- */
20
- export interface AgentContext extends restate.SessionContext {
21
- }
22
- /**
23
- * The context of an agent's view: a view context, from an object that has a
24
- * companion. What {@link AgentContext} is to a handler, for the views that
25
- * read what the agent is doing, or tell its companion to (`status`,
26
- * `pending`, `signal`, `steer`, `watch`).
27
- */
28
- export interface AgentViewContext extends restate.ViewContext {
29
- }
30
- /**
31
- * A value, or a function of the invocation's context that gives it: the
32
- * same for every message, or each user's (from the object's database, say).
33
- *
34
- * pi.chat({ model: 'openai/gpt-6-sol' })
35
- * pi.chat(async (ctx) => ({ model: await userModel(ctx) }))
36
- */
37
- export type Configure<T> = T | ((ctx: restate.SessionContext) => T | Promise<T>);
38
- /** `value` for the invocation of `ctx`. */
39
- export declare const configured: <T>(value: Configure<T>, ctx: restate.SessionContext) => Promise<T>;
40
- /**
41
- * The id of the next prompt of the invocation of `ctx`: the invocation's id,
42
- * then `<id>:2`, `<id>:3`... A retried attempt makes its prompts in the same
43
- * order, so each gets the same id again, and its answer.
44
- */
45
- export declare const nextPromptId: (ctx: restate.SessionContext) => string;
46
- /** The default model of pi and LangGraph, with OPENAI_API_KEY. */
47
- export declare const DEFAULT_MODEL = "openai/gpt-6-sol";
48
- /** The default model of Claude agents, with ANTHROPIC_API_KEY. */
49
- export declare const CLAUDE_MODEL = "anthropic/claude-sonnet-5-5";
50
- /**
51
- * The model, as `provider/model-id` (such as `openai/gpt-6-sol`):
52
- * `model`, or else MODEL, or else the framework's default.
53
- */
54
- export declare const modelOf: (model: string | undefined, fallback?: string) => string;
55
- /** `provider/model-id` as its provider and model id, split at the first `/` (`openrouter/meta-llama/llama-4`). */
56
- export declare const providerOf: (model: string) => [provider: string, id: string];
57
- /**
58
- * The error code of a message the model's provider refused (a missing or
59
- * wrong API key, an unknown model): 424, Failed Dependency, as its HTTP
60
- * status.
61
- */
62
- export declare const REFUSED = 424;
63
- /**
64
- * Whether `value`, a signal's that a tool call waits for to run (an
65
- * approval), approves it: `true`, `{ "approved": true }`, or a text such as
66
- * "approved" or "yes", whatever its case. Anything else denies it, so a
67
- * value that does not say "approved" never runs the tool. Every framework's
68
- * approvals take it (DeepSeek's, OpenCode's, agentlib's).
69
- */
70
- export declare const approves: (value: unknown) => boolean;
71
- /** What to check when a model's provider refuses a request. */
72
- export declare const CHECK_MODEL = "check the agent's model (its `model` option, or MODEL) and its provider's API key";
73
- /**
74
- * An agent's model that it cannot take: not `provider/model-id`, another
75
- * provider's, or one its framework does not know. Terminal ({@link REFUSED}),
76
- * wherever it is thrown: a retry takes the same model.
77
- */
78
- export declare class ModelError extends restate.TerminalError {
79
- constructor(message: string);
80
- }
81
- /** `model`, the explicit `model` option of `framework`'s agent, if it is not `provider/model-id`. */
82
- export declare const checkModelName: (framework: string, model: string | undefined) => void;
83
- /**
84
- * The model id of `model`, for a framework with only `provider`'s models:
85
- * `provider/model-id`, or the id alone.
86
- */
87
- export declare const modelIdOf: (model: string, provider: string) => string;
88
- /** A chat message: the request's body as text, or the string it is as JSON (`"Hi"`). */
89
- export declare const messageSerde: restate.Serde<string>;
90
- /**
91
- * What an empty answer says, as the model sees it later in the conversation:
92
- * a provider's API may refuse an empty text (Anthropic's does).
93
- */
94
- export declare const NO_ANSWER = "(no answer)";
95
- /**
96
- * What a cancelled turn answers, and each tool call it cut off: the model
97
- * sees it in the conversation that goes on.
98
- */
99
- export declare const CANCELLED = "Cancelled: the turn was stopped before it finished.";
100
- /**
101
- * What a prompt answers that the model's provider refused for good (see
102
- * {@link refusal}): the prompt is over, and the model sees it in the
103
- * conversation that goes on, rather than answer it with the next message.
104
- */
105
- export declare const UNANSWERED = "Unanswered: the model's provider refused the request.";
106
- /**
107
- * What a turn whose invocation ended without answering it (killed, say)
108
- * answers, and each tool call it cut off: the next message, or the companion
109
- * once it finds the invocation gone (`settle`), closes it with this.
110
- */
111
- export declare const ENDED = "Ended: the turn ended before it finished, and was not answered.";
112
- /** The result of a tool call a stopped turn cut off once its Restate call had answered: it happened. */
113
- export declare 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.";
114
- /**
115
- * What a stopped turn records as the result of its tool call `callId`, which
116
- * has none: `text` (cancelled, say), unless the call's Restate call answered
117
- * (`outcomes` of calls.ts), which the model would take for undone, and do
118
- * again: then the error it answered with, or {@link RETURNED_UNRECORDED}.
119
- */
120
- export declare function cutOffResult(outcomes: ReadonlyMap<string, string | undefined>, callId: string, text: string): string;
121
- /** How many of the latest answered turns the model sees whole (with their tool calls and results), or `'all'`. */
122
- export type Window = number | 'all';
123
- /** The window by default: the previous turn's tool calls and results stay in view, for a follow-up. */
124
- export declare const WINDOW: Window;
125
- /** How many tokens of the turns before the window the model sees, by default, counted as 4 characters each. */
126
- export declare const BUDGET = 32000;
127
- /** How an agent summarizes earlier turns: from the request ({@link SUMMARIZE} and the turns, as text) to the summary. */
128
- export type Summarize = (ctx: restate.SessionContext, request: string) => Promise<string>;
129
- /** What a request to summarize earlier turns starts with, before the turns themselves. */
130
- export declare 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";
131
- /**
132
- * What a turn checks its answer with: nothing if the answer is what was
133
- * asked for, else the message that asks the model again, once, within the
134
- * turn (`answerOnce`'s `check`). With `last`, the model was asked again
135
- * already: a check that throws a terminal error then fails the turn, which
136
- * is closed with it.
137
- */
138
- export type Check = (answer: string, last?: boolean) => string | undefined;
139
- /** `window`, if it is one: a whole number of turns (0 or more), or `'all'`. */
140
- export declare function checkWindow(window: Window): Window;
141
- /** The message a `chat` handler is answering: its invocation, and when it started. */
142
- export interface Answering {
143
- /** The invocation's id: `/restate/invocation/<id>/attach` waits for its answer. */
144
- invocationId: string;
145
- /** When it started, as an ISO date. */
146
- since: string;
147
- }
148
- /**
149
- * Why a turn stopped when its attempt ended before it did: Restate suspended
150
- * the attempt, gave up on it, or lost its connection. Nothing the attempt
151
- * does after is stored, and the next attempt goes on with the turn, so it
152
- * stops without answering: unlike a cancellation, nothing is cancelled.
153
- */
154
- export declare class AttemptEnded extends Error {
155
- constructor();
156
- }
157
- /**
158
- * The signal that stops the turn of `ctx` (of a `chat` handler,
159
- * `chatHandler`), and an agent's `prompt` stops its framework's work with it.
160
- * It aborts with a `restate.CancelledError` once Restate cancels the
161
- * invocation (the turn ends, answered as cancelled), or with an
162
- * {@link AttemptEnded} once the attempt ends first (the work stops, and
163
- * answers nothing).
164
- */
165
- export declare const stopSignalOf: (ctx: restate.SessionContext) => AbortSignal | undefined;
166
- /**
167
- * Open a `ctx.working` scope until the function it returns is called (once
168
- * or more): for a framework's hooks that bracket a model's request without
169
- * wrapping it. A scope never released keeps the attempt alive to its end.
170
- */
171
- export declare function keepAlive(ctx: restate.SessionContext): () => void;
172
- /**
173
- * `source`, kept alive while it is iterated: from the first pull to its end,
174
- * however it ends (done, thrown, or returned early, as a `break` does). A
175
- * model's streamed response, say.
176
- */
177
- export declare function keptAlive<T>(ctx: restate.SessionContext, source: AsyncIterable<T>): AsyncGenerator<T>;
178
- /**
179
- * An agent's settings: whether it runs in the background (`background:
180
- * true`: no one follows it as it works), and, for an agent on the
181
- * conversation of turns (conversation.ts), its framework's items, how much
182
- * of the conversation its model sees (`window` and `budget`), and how it
183
- * summarizes the earliest turns (`summarize`).
184
- */
185
- export interface Settings {
186
- background: boolean;
187
- /** The items of its framework, if it keeps the conversation of turns (its `messages` view is `conversationView`'s). */
188
- items?: object;
189
- window: Window;
190
- budget: number;
191
- summarize?: Summarize;
192
- }
193
- /** The items of a framework's `messages` view on the conversation of turns. */
194
- export declare const ITEMS: Link<object>;
195
- /** `definition` is an agent's, with these settings: its framework's items, if it is on the conversation of turns. */
196
- export declare const isAgent: <D extends object>(definition: D, { background, items, window, budget, summarize, }: {
197
- background?: boolean | undefined;
198
- budget?: number | undefined;
199
- items?: object | undefined;
200
- summarize?: Summarize | undefined;
201
- window?: Window | undefined;
202
- }) => D;
203
- /** The settings of the agent of `ctx`: the defaults, for an object that is no agent. */
204
- export declare const settingsOf: (ctx: restate.SessionContext) => Settings;
205
- /**
206
- * The messages steered to a `chat` turn that it has not taken yet, as the
207
- * companion last published them. The companion answers them to every
208
- * `publish` (and `start`), which a turn makes as it starts and after each
209
- * write of its steps that commits: so the turn knows of them without a call
210
- * of its own, and finds them at its next step (`takeSteers`). The newest
211
- * revision wins, as writes that run at once publish at once.
212
- */
213
- export declare class Inbox {
214
- #private;
215
- revision: number;
216
- messages: Steer[];
217
- /** Keep what `publish` answered, if it is newer than what is kept, and tell the listeners if it lists messages. */
218
- saw(published: Published | undefined): void;
219
- /**
220
- * Call `listener` after each publish that lists messages, until the
221
- * returned function is called: for a framework that takes steered
222
- * messages through an inbox of its own (pi's, DeepSeek's), handed to it as
223
- * they come.
224
- */
225
- listen(listener: () => void): () => void;
226
- }
227
- /** The inbox of the `chat` turn running on `ctx`, if it takes steered messages: none outside one. */
228
- export declare const inboxOf: (ctx: restate.SessionContext) => Inbox | undefined;
229
- /**
230
- * The conversation of `ctx` changed (see `watch` in object.ts): its revision
231
- * increases, and the clients that follow it read it again; while a framework
232
- * answers ({@link answering}), the calls whose results it stored are
233
- * forgotten first. It is a call to Restate, so it also sends what the
234
- * handler wrote before, for them to read: the rows the conversation holds
235
- * back are written first (`flushHeld`), as the clients read them once told.
236
- *
237
- * Only a `chat` turn ({@link chatTurn}: after each commit of its steps) and
238
- * `settle` publish, on an object with a companion: one without fails with a
239
- * terminal error (`NoCompanion`), rather than call a companion nobody serves.
240
- */
241
- export declare const publish: (ctx: AgentContext) => Promise<void>;
242
- /** What the turn running on `ctx` makes after each commit of its steps, if anything: see {@link AFTER_STEPS}. */
243
- export declare const afterStepsOf: (ctx: restate.SessionContext) => (() => Promise<void>) | undefined;
244
- /**
245
- * A tool call of the turn of `ctx` starts: forget the calls whose results are
246
- * stored (calls.ts) before the tool makes its own. It publishes nothing: a
247
- * followed `chat` turn publishes once the call is stored (with its reply),
248
- * and once its result is ({@link afterStepsOf}).
249
- */
250
- export declare function toolCallStarted(ctx: restate.SessionContext): Promise<void>;
251
- /**
252
- * Forget the call records (calls.ts) whose results the turn answering on
253
- * `ctx` has stored ({@link answering}), if one does: after each commit of its
254
- * steps (`answerOnce` does, before what the turn asked for then, such as a
255
- * `chat` turn's publish, for every turn, in a `chat` turn or not), as a tool
256
- * call starts, and as the turn ends.
257
- */
258
- export declare function settleAnswered(ctx: restate.SessionContext): Promise<void>;
259
- /** Whether a tool call's result is stored: `stored(callId, failed)`, `failed` the error the call answered with. */
260
- export type Stored = (callId: string, failed?: string) => boolean;
261
- /**
262
- * Run `work`, a framework's turn on the conversation of turns, forgetting
263
- * the calls whose results it stores as it goes: each write of its steps
264
- * that commits ({@link settleAnswered}), each tool call's start, and its
265
- * end, forget the Restate calls of
266
- * the tool calls whose results `stored` says are stored (calls.ts).
267
- */
268
- export declare function answering<T>(ctx: restate.SessionContext, stored: Stored, work: () => Promise<T>): Promise<T>;
269
- /** A promise that rejects with `signal`'s reason once it aborts. */
270
- export declare const aborted: (signal: AbortSignal) => Promise<never>;
271
- /** A `chat` handler's answer to a message: a framework's prompt, say. */
272
- export type Chat<A = unknown> = (ctx: AgentContext, message: string) => Promise<A>;
273
- /**
274
- * Whether a `chat` handler's turns take steered messages: for every message,
275
- * or for each, as the invocation's context says (a framework's options that
276
- * a function gives, say).
277
- */
278
- export type Steers = boolean | ((ctx: AgentContext) => boolean | Promise<boolean>);
279
- /** The answer of `entry`, a handler that `chatHandler` made, and whether it takes steered messages. */
280
- export declare const chatOf: (entry: object) => {
281
- answer: Chat;
282
- steers: Steers;
283
- } | undefined;
284
- /**
285
- * `answer` as a chat handler, which takes its message as plain text or as a
286
- * JSON string, so that both of these work:
287
- *
288
- * curl localhost:8080/assistant/alice/chat -d 'Hi'
289
- * curl localhost:8080/assistant/alice/chat --json '"Hi"'
290
- *
291
- * While it answers, the object's `status` says so (its {@link Answering}). Its
292
- * invocation's cancellation (Restate's, explicit: `cancelled(ctx)` of
293
- * @restatedev/sessions/internal) aborts {@link stopSignalOf} its
294
- * context, whenever it comes, and cancels the calls its tools made
295
- * (`cancelCalls(ctx)`): the agent's `prompt`
296
- * ends the turn, and the handler fails with a `restate.CancelledError`. An
297
- * attempt that ends first (`attemptCompletedSignal`) aborts it too, with an
298
- * {@link AttemptEnded}: the work stops, and the next attempt goes on with it.
299
- *
300
- * It publishes when it starts (its companion's `start` does) and ends
301
- * ({@link publish}), for the clients that follow the conversation, unless
302
- * the agent runs in the background, which publishes the end only.
303
- *
304
- * As the `chat` handler of an agent, it also answers each message sent to
305
- * the agent's `steer` view that no turn took, as a turn of its own (the
306
- * private `steered` handler). With `steers` (a framework's integration says
307
- * so), its turns take steered messages at their steps (`takeSteers`).
308
- */
309
- export declare const chatHandler: <A = string>(answer: Chat<A>, { steers }?: {
310
- steers?: Steers;
311
- }) => {
312
- input: restate.Serde<string>;
313
- accept: string;
314
- explicitCancellation: boolean;
315
- handler: (ctx: AgentContext, text: string) => Promise<A>;
316
- };
317
- /**
318
- * Answer `text` as a turn of the agent of `ctx`, with `answer`: the body of a
319
- * `chat` handler ({@link chatHandler}), and of `steered`. With `steers`, the
320
- * turn takes the messages steered to it at its steps (unless the agent runs
321
- * in the background).
322
- */
323
- export declare function chatTurn<A>(ctx: AgentContext, text: string, answer: Chat<A>, steers?: Steers): Promise<A>;
324
- /** A tool's error, which fails the attempt (Restate retries it): the error `cause` of tool `tool`. */
325
- export declare class ToolError extends Error {
326
- readonly tool: string;
327
- constructor(tool: string, cause: unknown);
328
- }
329
- /**
330
- * `error` is a tool's, in the attempt of `ctx`: a framework's own tool
331
- * (Claude's, LangChain's...) that failed, whose error is never taken for
332
- * the model's provider refusing (a tool that calls a model API may fail as
333
- * one does). `tool()`'s are {@link ToolError}s.
334
- */
335
- export declare function fromTool(ctx: restate.SessionContext, error: unknown): void;
336
- /** {@link refusal}, but not of an error a framework's tool failed with in the attempt of `ctx` ({@link fromTool}). */
337
- export declare function refusalOf(ctx: restate.SessionContext, error: unknown): restate.TerminalError | undefined;
338
- /**
339
- * `error` as a terminal error ({@link REFUSED}), if a retry does not change
340
- * it: a model the agent cannot take, or its provider's refusal.
341
- */
342
- export declare function refusal(error: unknown): restate.TerminalError | undefined;
343
- /**
344
- * A prompt's stored answer, as the prompt returns it, by how its turn ended
345
- * (`status`): a turn refused ({@link UNANSWERED}) fails with
346
- * {@link REFUSED}, one failed for good with its error, one cancelled with
347
- * the cancellation, one another invocation ended with its note; so that a
348
- * retry after a crash right after the close fails as the prompt did.
349
- */
350
- export declare function storedAnswer(answer: string, status?: string): string;
351
- /** A message of a conversation, as an agent's `messages` handler returns it. */
352
- export interface ChatMessage {
353
- /** Its id, from 0, in the order of the conversation; the same however it changes. `before` takes it. */
354
- id: number;
355
- role: 'user' | 'assistant';
356
- parts: Part[];
357
- }
358
- /**
359
- * What a message says: text, or a tool call with its output once it ran,
360
- * and `error: true` if the output is the tool's error (`Error: <message>` of
361
- * a `restate.TerminalError`, say).
362
- */
363
- export type Part = {
364
- type: 'text';
365
- text: string;
366
- } | {
367
- type: 'tool';
368
- id: string;
369
- name: string;
370
- input: unknown;
371
- output?: string;
372
- error?: true;
373
- };
374
- /**
375
- * Which messages to read: the latest `limit`, or those before a message, for
376
- * a page of the conversation; or those new or changed since a `cursor`, to
377
- * follow it.
378
- */
379
- export interface MessagesRequest {
380
- /**
381
- * `full` (the default): every message, with its tool calls; `summary`:
382
- * each turn's message of the user and its answer, under the ids they have
383
- * in the full view.
384
- */
385
- view?: 'full' | 'summary';
386
- /** At most this many messages (default 50). */
387
- limit?: number;
388
- /** The messages before this one (its `id`): older ones, such as for scrolling back. */
389
- before?: number;
390
- /** The messages new or changed since this cursor (a page's `cursor`), by when they changed. */
391
- cursor?: number;
392
- }
393
- /**
394
- * A page of messages: oldest first; or, with a `cursor`, those new or
395
- * changed since it, by when they changed. Keep them by `id`: a message comes
396
- * again once it changed (its tool call's output came, say).
397
- */
398
- export interface Messages {
399
- messages: ChatMessage[];
400
- /** Whether there are more: older ones, or with a `cursor`, newer ones. */
401
- more: boolean;
402
- /** The cursor to read on from: what changes after this page. */
403
- cursor: number;
404
- /**
405
- * The id of the conversation's first message, once it was cleared
406
- * (`clear`): a client drops the messages it kept from before.
407
- */
408
- first?: number;
409
- }
410
- //# sourceMappingURL=common.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,OAAO,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAwC,IAAI,EAAY,MAAM,+BAA+B,CAAC;AAErG,OAAO,EAAiB,KAAK,SAAS,EAAE,KAAK,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAE3E;;;;;;;;;;;;;;;GAeG;AACH,MAAM,WAAW,YAAa,SAAQ,OAAO,CAAC,cAAc;CAAG;AAE/D;;;;;GAKG;AACH,MAAM,WAAW,gBAAiB,SAAQ,OAAO,CAAC,WAAW;CAAG;AAEhE;;;;;;GAMG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,OAAO,CAAC,cAAc,KAAK,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;AAEjF,2CAA2C;AAC3C,eAAO,MAAM,UAAU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