@restatedev/agents 0.3.0 → 0.4.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 +89 -49
  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 +466 -0
  18. package/dist/conversation.d.ts.map +1 -0
  19. package/dist/conversation.js +1247 -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 +121 -35
  64. package/dist/object.js.map +1 -1
  65. package/dist/output.d.ts +15 -7
  66. package/dist/output.d.ts.map +1 -1
  67. package/dist/output.js +44 -26
  68. package/dist/output.js.map +1 -1
  69. package/dist/pi/index.d.ts +39 -11
  70. package/dist/pi/index.d.ts.map +1 -1
  71. package/dist/pi/index.js +281 -77
  72. package/dist/pi/index.js.map +1 -1
  73. package/dist/pi/storage.d.ts +29 -4
  74. package/dist/pi/storage.d.ts.map +1 -1
  75. package/dist/pi/storage.js +18 -6
  76. package/dist/pi/storage.js.map +1 -1
  77. package/dist/signals.d.ts +74 -4
  78. package/dist/signals.d.ts.map +1 -1
  79. package/dist/signals.js +144 -15
  80. package/dist/signals.js.map +1 -1
  81. package/dist/steering.d.ts +31 -0
  82. package/dist/steering.d.ts.map +1 -0
  83. package/dist/steering.js +45 -0
  84. package/dist/steering.js.map +1 -0
  85. package/dist/tools.d.ts +28 -0
  86. package/dist/tools.d.ts.map +1 -1
  87. package/dist/tools.js +34 -5
  88. package/dist/tools.js.map +1 -1
  89. package/package.json +33 -37
  90. package/src/calls.ts +107 -15
  91. package/src/claude/README.md +41 -20
  92. package/src/claude/index.ts +266 -178
  93. package/src/client.ts +14 -3
  94. package/src/common.ts +317 -232
  95. package/src/conversation.ts +1527 -0
  96. package/src/deepseek/README.md +78 -11
  97. package/src/deepseek/index.ts +384 -137
  98. package/src/deepseek/persistence.ts +11 -8
  99. package/src/index.ts +13 -4
  100. package/src/langgraph/README.md +115 -25
  101. package/src/langgraph/checkpointer.ts +168 -28
  102. package/src/langgraph/context.ts +27 -0
  103. package/src/langgraph/errors.ts +26 -0
  104. package/src/langgraph/graph.ts +396 -0
  105. package/src/langgraph/index.ts +326 -181
  106. package/src/langgraph/nodes.ts +243 -0
  107. package/src/messages.ts +244 -0
  108. package/src/object.ts +167 -38
  109. package/src/output.ts +52 -31
  110. package/src/pi/README.md +18 -3
  111. package/src/pi/index.ts +346 -78
  112. package/src/pi/storage.ts +20 -7
  113. package/src/signals.ts +225 -19
  114. package/src/steering.ts +67 -0
  115. package/src/tools.ts +61 -8
  116. package/dist/claude/conversation.d.ts +0 -33
  117. package/dist/claude/conversation.d.ts.map +0 -1
  118. package/dist/claude/conversation.js +0 -95
  119. package/dist/claude/conversation.js.map +0 -1
  120. package/dist/log.d.ts +0 -29
  121. package/dist/log.d.ts.map +0 -1
  122. package/dist/log.js +0 -146
  123. package/dist/log.js.map +0 -1
  124. package/dist/opencode/db.d.ts +0 -40
  125. package/dist/opencode/db.d.ts.map +0 -1
  126. package/dist/opencode/db.js +0 -161
  127. package/dist/opencode/db.js.map +0 -1
  128. package/dist/opencode/index.d.ts +0 -100
  129. package/dist/opencode/index.d.ts.map +0 -1
  130. package/dist/opencode/index.js +0 -436
  131. package/dist/opencode/index.js.map +0 -1
  132. package/src/claude/conversation.ts +0 -114
  133. package/src/log.ts +0 -194
  134. package/src/opencode/README.md +0 -125
  135. package/src/opencode/db.ts +0 -223
  136. package/src/opencode/index.ts +0 -559
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @restatedev/agents
2
2
 
3
- Durable agents on Restate: an agent of one of five frameworks (pi, OpenCode,
3
+ Durable agents on Restate: an agent of one of four frameworks (pi,
4
4
  LangGraph, Claude, the DeepSeek Harness) in a Restate virtual object, its
5
5
  conversation in the object's own SQL database. A crash, a restart or a
6
6
  person who takes a day to approve changes nothing: each message is answered
@@ -15,8 +15,9 @@ once, and each tool call is made once.
15
15
  - [Your own session object](#your-own-session-object)
16
16
  - [Build your own](#build-your-own)
17
17
  - [Words](#words)
18
- - [Turn semantics](TURN-SEMANTICS.md): how a message is answered, retried,
19
- stopped or killed, and what a client sees meanwhile
18
+ - [The conversation of turns](docs/README.md): how a message is answered,
19
+ retried, stopped or killed, how the conversation is stored, what the model
20
+ sees, and what a client sees meanwhile
20
21
 
21
22
  ## Start here
22
23
 
@@ -68,16 +69,16 @@ Restate, wherever it is.
68
69
 
69
70
  The same agent runs on each of them: `agent({ name, ...options })`, with
70
71
  the handler `chat` (a message in, the answer out), and the views
71
- `messages` (the conversation so far), `status` (what it is doing), `stop`
72
- (the message it answers), `clear` (forget it all), `sync`,
72
+ `messages` (the conversation so far), `status` (what it is doing), `steer`
73
+ (add to the message it answers), `stop` (the message it answers), `clear`
74
+ (forget it all), `sync`,
73
75
  `notifications` and `watch` (to follow it), and `signal` and `pending`
74
76
  ([Signals](#signals)).
75
77
 
76
78
  | Import | Framework | Choose it when |
77
79
  | --------------------------------------------------------- | ------------------------------- | --------------------------------------------------------------------------------------------- |
78
80
  | [`@restatedev/agents/pi`](src/pi/README.md) | pi-durable | You want the default: any provider, coding tools on request, and its Harness (compact, reset) |
79
- | [`@restatedev/agents/opencode`](src/opencode/README.md) | OpenCode 2.0, embedded | You want a coding agent: OpenCode's own tools (read, edit, bash, grep...) in a directory |
80
- | [`@restatedev/agents/langgraph`](src/langgraph/README.md) | LangGraph | You use LangChain: its tools, or a chat model of your own |
81
+ | [`@restatedev/agents/langgraph`](src/langgraph/README.md) | LangGraph | You use LangChain: its tools, a chat model of your own, or a graph of your own |
81
82
  | [`@restatedev/agents/claude`](src/claude/README.md) | the Anthropic SDK's tool runner | You want Claude with nothing in between: the lightest, the API's own messages |
82
83
  | [`@restatedev/agents/deepseek`](src/deepseek/README.md) | DeepSeek Harness | You want DeepSeek's harness: DeepSeek's models, or any pi-ai knows |
83
84
 
@@ -92,16 +93,18 @@ npm install @restatedev/agents @earendil-works/pi-durable @earendil-works/pi-ai
92
93
  npm install @restatedev/agents @anthropic-ai/sdk # Claude
93
94
  npm install @restatedev/agents @langchain/core @langchain/langgraph @langchain/langgraph-checkpoint \
94
95
  @langchain/openai # LangGraph, and the provider of its models: or @langchain/anthropic
95
- npm install @restatedev/agents @opencode/core@2.0.32 @opencode/plugin@2.0.32 @opencode/sdk@2.0.32 \
96
- @opencode/server@2.0.32 effect@4.0.1 # OpenCode, which pins its packages, and effect
97
96
  npm install @restatedev/agents @earendil-works/pi-ai @deepseek-ai/cordis \
98
97
  @deepseek-ai/{dsh-agent,dsh-agent-loop,dsh-llm,dsh-llm-deepseek-api-key,dsh-llm-pi-ai,dsh-session}@0.2.1-rc.2 \
99
98
  @deepseek-ai/{dsh-session-checkpoint-policy,dsh-session-persistence,dsh-session-projection}@0.2.1-rc.2 \
100
- @deepseek-ai/{dsh-system-prompt,dsh-tools}@0.2.1-rc.2 # DeepSeek
99
+ @deepseek-ai/{dsh-system-prompt,dsh-tools,dsh-user-approval}@0.2.1-rc.2 \
100
+ @deepseek-ai/{dsh-commands,dsh-compaction,dsh-compaction-basic,dsh-compaction-tool-result-pruner,dsh-token-meter}@0.2.1-rc.2 # DeepSeek
101
101
  ```
102
102
 
103
- OpenCode is pinned: @restatedev/agents stores its tables in the object's
104
- database, as its version lays them out. A subpath whose packages are
103
+ OpenCode was a framework of this package until its agents moved onto the
104
+ conversation of turns: a turn whose invocation is gone (killed) must be
105
+ closed, never finished, and OpenCode resumes a turn it claimed whenever it
106
+ boots, with no public way to end it first (see
107
+ [docs/frameworks.md](docs/frameworks.md#opencode)). A subpath whose packages are
105
108
  missing fails as it is imported, naming the first it lacks. The packages
106
109
  are not published yet:
107
110
  [`pnpm --filter e2e packaging`](../e2e/packaging.ts) installs them from
@@ -109,10 +112,10 @@ their tarballs, with pi alone. In this repository the frameworks' packages
109
112
  are @restatedev/agents' development dependencies, so every example runs as it is.
110
113
 
111
114
  Every framework takes `model`, `system`, `tools` and `output` (a
112
- [structured answer](#structured-answers)); pi and OpenCode take a
113
- `workspace` too, a directory for their coding tools. `system` is the whole
115
+ [structured answer](#structured-answers)); pi takes a
116
+ `workspace` too, a directory for its coding tools. `system` is the whole
114
117
  system prompt for Claude and LangGraph, and is added to the framework's own
115
- (which tells the model about its tools) for pi, OpenCode and DeepSeek.
118
+ (which tells the model about its tools) for pi and DeepSeek.
116
119
 
117
120
  Every agent takes these too, beside its framework's options:
118
121
 
@@ -121,6 +124,13 @@ Every agent takes these too, beside its framework's options:
121
124
  | `description` | What it is, for the UI, and for a parent's model: `subagent(agent)` tells it |
122
125
  | `background` | It works unwatched (a subagent, say): its turn tells its `signals` at its end only, not at each step, so a follower sees each turn once it ended |
123
126
  | `schema` | Tables of your own in its database ([Build your own](#build-your-own)) |
127
+ | `window` | How many of the latest answered turns its model sees whole, with their tool calls and results (default 1), or `'all'` |
128
+ | `budget` | How many tokens of the turns before the window its model sees, as their messages and answers (default 32 000); `summarize` summarizes the rest |
129
+
130
+ `window` and `budget` bound what Claude's and LangGraph's model is sent
131
+ ([what the model gets](docs/conversation.md)); pi and the DeepSeek Harness
132
+ keep their own history, and compact it themselves. An object holds one
133
+ framework's conversation: its turns are that framework's messages.
124
134
 
125
135
  ## Calling an agent
126
136
 
@@ -133,7 +143,7 @@ curl localhost:8080/assistant/alice/chat -d 'Hi, I am Alice' # "Hello Alic
133
143
  ```
134
144
 
135
145
  A key answers one message at a time: a message sent while another is
136
- answered waits for it ([turn semantics](TURN-SEMANTICS.md) spells out how a
146
+ answered waits for it ([turn semantics](docs/turns.md) spells out how a
137
147
  turn starts, ends, and is retried). A message that fails for good answers with an error:
138
148
  HTTP 424 if the model's provider refused it (a missing or wrong API key, an
139
149
  unknown model), 500 otherwise. A refused message is over: the next message
@@ -179,9 +189,30 @@ An attempt that ends before its turn does, while its process lives on
179
189
  work too, a model's request included: it can store nothing more, and the
180
190
  next attempt goes on with the turn, which is not cancelled.
181
191
 
182
- **A message while it works.** A message sent while the agent answers
183
- another waits for it: Restate answers a key's messages one at a time, in
184
- order. To change course, stop the turn (`stop`) and send the message that
192
+ **A message while it works.** A message sent to `chat` while the agent
193
+ answers another waits for it: Restate answers a key's messages one at a
194
+ time, in order. To add to the message being answered ("use Celsius", "skip
195
+ Friday"), send it to `steer`: the turn takes it at its next step (once the
196
+ model's tool calls have their results, before its next request), and its
197
+ answer covers it. A message that comes too late for a step (the model is
198
+ writing its final answer, say) is answered as a turn of its own, right after
199
+ the turn, before the messages sent after it; so is one sent with no turn
200
+ being answered. Either way it is answered once: send it with an idempotency
201
+ key, so that a request sent again is the same message.
202
+
203
+ ```sh
204
+ curl localhost:8080/assistant/alice/chat/send -d 'Plan my week'
205
+ curl localhost:8080/assistant/alice/steer -H 'idempotency-key: 7f3c…' -d 'Skip Friday' # {"id": "inv_…", "turn": "inv_1f…"}
206
+ ```
207
+
208
+ `steer` answers the message's id and the turn that holds it for its next
209
+ step, or `turn: null` if it is answered as a turn of its own; `status`
210
+ lists the messages the turn holds and has not taken yet (`steering`). A
211
+ turn takes at most 16. Claude and LangGraph's built-in agent take them
212
+ before each model request that follows a round of tool calls; pi and
213
+ DeepSeek through their own inboxes; a LangGraph graph of your own, and a
214
+ background agent, take none ([Steering a turn](docs/turns.md#steering-a-turn)).
215
+ To change course outright, stop the turn (`stop`) and send the message that
185
216
  replaces it.
186
217
 
187
218
  **Forget a conversation.** `clear` starts a key over, such as for a UI's
@@ -235,15 +266,17 @@ curl localhost:8080/assistant/alice/messages
235
266
  }
236
267
  ```
237
268
 
238
- The conversation as clients read it is a log in the object's database,
239
- which each change the agent publishes goes into. A message's `id` never
240
- changes; `cursor` is where the log is now. `limit` caps a page; `before` an
241
- id gives older messages (scrolling back, while `more` says there are older
242
- ones still); and `cursor` gives the messages new or changed since it, by
243
- when they changed, with the next cursor: a message that grew (a tool call
244
- with its output) comes again, with its `id`. A compacted or reset
245
- conversation keeps what was said, and the summary or handoff after it (the
246
- model's context is what changed):
269
+ The conversation as clients read it is the agent's conversation of turns,
270
+ in the object's database ([docs/conversation.md](docs/conversation.md)). A
271
+ message's `id` is its position, and never changes; `cursor` is where the
272
+ conversation is now. `limit` caps a page; `before` an id gives older
273
+ messages (scrolling back, while `more` says there are older ones still);
274
+ and `cursor` gives the messages new or changed since it, by when they
275
+ changed, with the next cursor: a message that grew (a tool call with its
276
+ output) comes again, with its `id`. `view: "summary"` gives each turn's
277
+ message and answer alone, under the same ids. A conversation whose earliest
278
+ turns are summarized, or that the framework compacted, keeps every turn as
279
+ it was said (only the model's context changed):
247
280
 
248
281
  ```sh
249
282
  curl localhost:8080/assistant/alice/messages --json '{"limit": 20, "before": 40}'
@@ -382,7 +415,7 @@ and Restate retries it: the tool call runs again.
382
415
  **Once.** A crash runs an unfinished tool call again with the same call id.
383
416
  A tool that must not run twice passes its `idempotencyKey` on (made of the
384
417
  invocation's id and the call id), as Restate's tools do with each call they
385
- make: run again, it is the same call. OpenCode and the DeepSeek Harness tell
418
+ make: run again, it is the same call. The DeepSeek Harness tells
386
419
  the model that a cut-off tool call was interrupted instead, and the model
387
420
  makes it again as a new tool call: Restate's tools then make the same call,
388
421
  if the input is the same (so a wait for an approval outlasts a restart), and
@@ -437,6 +470,10 @@ curl localhost:8080/support/alice/signal --json '{"id": "inv_1f…:toolu_01", "v
437
470
  curl localhost:8080/support/alice/signal --json '{"name": "approval", "value": "approved", "keep": true}'
438
471
  ```
439
472
 
473
+ A DeepSeek agent's tool calls that need a person's approval (its `approval`
474
+ option) wait the same way, for the signal `approval`: see
475
+ [its README](src/deepseek/README.md#approvals).
476
+
440
477
  A wait ends with its agent's invocation: a cancelled or killed one leaves `pending`.
441
478
  The waits are kept by the `signals` object, keyed `<agent>:<key>`, which
442
479
  `serve` serves beside the agents. See
@@ -445,10 +482,14 @@ The waits are kept by the `signals` object, keyed `<agent>:<key>`, which
445
482
  ## Structured answers
446
483
 
447
484
  With `output`, a zod object, an agent answers with data rather than text:
448
- its reply is its answer, as JSON of the schema (which its system prompt
449
- asks for, with the schema), and `chat` answers with the value, checked
450
- against the schema: the answer is the call's return value, as with any
451
- handler. Every framework takes it.
485
+ its reply is its answer, as JSON of the schema, and `chat` answers with the
486
+ value, checked against the schema: the answer is the call's return value,
487
+ as with any handler. Every framework takes it. Claude asks the model for it
488
+ natively where the model has structured outputs (the API's
489
+ `output_config.format`, which holds the reply to the schema), or as its
490
+ `nativeOutput` says; otherwise, and in the other frameworks, the system
491
+ prompt asks for it, with the schema, and the model is reminded of it after
492
+ each prompt, though `messages` shows the prompt as it was asked.
452
493
 
453
494
  ```ts
454
495
  const Plan = z.object({ summary: z.string(), parts: z.array(z.string()) });
@@ -457,12 +498,14 @@ const planner = claude.agent({ name: 'planner', output: Plan });
457
498
  const plan = await ctx.rpc(planner, 'ENG-1').chat('Plan the work on ENG-1'); // a Plan
458
499
  ```
459
500
 
460
- A reply that is not such JSON (a code fence around it is fine) is answered
461
- with what is wrong with it, once, for the model to reply again; then the
462
- invocation fails, with a terminal error. A retried attempt resumes the
463
- conversation, and reads the same reply. In a handler of your own, `prompt(ctx, text, {
464
- output })` answers with it too. As a subagent's, the parent's model gets it
465
- as JSON.
501
+ A reply that is not such JSON (a code fence around it is fine; asked
502
+ natively, the API holds it to the schema's types, not to checks such as a
503
+ string's `max`) is answered with what is wrong with it, once, for the model
504
+ to reply again; then the invocation fails, with a terminal error, and the
505
+ conversation shows its turn failed, with that error. A retried attempt
506
+ resumes the conversation, and reads the same reply. In a handler of your
507
+ own, `prompt(ctx, text, { output })` answers with it too. As a subagent's,
508
+ the parent's model gets it as JSON.
466
509
 
467
510
  ## Webhooks
468
511
 
@@ -530,7 +573,7 @@ environment variable, or else OpenAI's `openai/gpt-6-sol`, with
530
573
  pi.agent({ name: 'assistant', model: 'anthropic/claude-sonnet-5-5' }); // with ANTHROPIC_API_KEY
531
574
  ```
532
575
 
533
- pi, OpenCode and the DeepSeek Harness take any provider they know (the
576
+ pi and the DeepSeek Harness take any provider they know (the
534
577
  DeepSeek Harness, pi-ai's); LangGraph OpenAI's and Anthropic's models (and
535
578
  any LangChain chat model, as `model`); Claude Anthropic's.
536
579
 
@@ -579,10 +622,8 @@ await serve(assistant);
579
622
  ```
580
623
 
581
624
  Your tables share the database with the agent's, whose names all start with
582
- `restate_` (`restate_pi_tasks`, `restate_claude_messages`,
583
- `restate_agent_log`...): any other name is yours. OpenCode's tables are in a
584
- database of their own, beside it, as OpenCode boots only on a database that
585
- is empty or its own.
625
+ `restate_` (`restate_agent_turns`, `restate_agent_items`,
626
+ `restate_pi_tasks`...): any other name is yours.
586
627
 
587
628
  **Options for each invocation.** `chat` takes options, or a function of the
588
629
  invocation's context that gives them: each user's model and persona from
@@ -606,9 +647,8 @@ that takes its message as `chat` does (text, or a JSON string):
606
647
  summarize: chatHandler(async (ctx, topic) => pi.prompt(ctx, `Summarize what we said about ${topic}`)),
607
648
  ```
608
649
 
609
- pi and OpenCode also have `withPi(ctx, options, work)` and
610
- `withOpenCode(ctx, options, work)`, for more than a prompt: the Harness's own
611
- API, such as compacting the conversation (see their READMEs).
650
+ pi also has `withPi(ctx, options, work)`, for more than a prompt: the Harness's own
651
+ API, such as compacting the conversation (see its README).
612
652
 
613
653
  **What `sessionObject()` sets up.** `ctx.db` in every handler; retries for weeks
614
654
  (50 ms after a crash, then twice as long each time, up to 10 s apart), since
@@ -618,8 +658,8 @@ journal in Restate's UI. Its `options` (such as `retryPolicy`) and
618
658
  [`@restatedev/sessions`](../sessions/README.md): `handlers` run one at a time per
619
659
  key, and `views` beside them. Its own cannot be named `signal`, `pending`,
620
660
  `status`, `stop`, `notifications` or `watch`, views every agent's
621
- session object has, nor `sync`, `clear` or `erase`, which an agent has (one with a
622
- `messages` view). A view of yours reads the database too: `ctx.db` is the
661
+ session object has, nor `sync`, `clear`, `steer`, `erase`, `settle`, `compact`
662
+ or `steered`, which an agent has (one with a `messages` view). A view of yours reads the database too: `ctx.db` is the
623
663
  database as committed, beside a message being answered.
624
664
 
625
665
  ## Build your own
package/dist/calls.d.ts CHANGED
@@ -1,11 +1,23 @@
1
1
  import type * as restate from '@restatedev/sessions';
2
+ /**
3
+ * Run `work` (a turn's) with the calls its tools make kept as prompt
4
+ * `prompt`'s: they are matched, and rejoined, within it only. An invocation
5
+ * that prompts twice, its second prompt making the call its first made
6
+ * (the same tool and input), would otherwise take the first's after a crash
7
+ * (its record not yet forgotten), and never make its own.
8
+ */
9
+ export declare function forPrompt<T>(ctx: restate.SessionContext, prompt: string, work: () => Promise<T>): Promise<T>;
2
10
  /**
3
11
  * The call of tool `tool` with `input` that tool call `callId` makes: the
4
- * one it made before (a run again), or the one an earlier attempt made with
5
- * the same tool and input (made again under a new id), or else a new one.
6
- * `run(id)` makes it with its own id, and stores that it answered.
12
+ * one it made to `tool` before (a run again), or else, if `rejoin`, the one
13
+ * an earlier attempt made with the same tool and input (made again under a
14
+ * new id), or else a new one, an `approval` its tool call waits for if so.
15
+ * `answered()` stores that it answered.
7
16
  */
8
- export declare function callOf(ctx: restate.SessionContext, tool: string, input: unknown, callId: string): Promise<{
17
+ export declare function callOf(ctx: restate.SessionContext, tool: string, input: unknown, callId: string, { rejoin, approval }?: {
18
+ rejoin?: boolean;
19
+ approval?: boolean;
20
+ }): Promise<{
9
21
  id: string;
10
22
  /**
11
23
  * It answered, with `failed` if a terminal error answered it: kept until
@@ -22,5 +34,17 @@ export declare function callOf(ctx: restate.SessionContext, tool: string, input:
22
34
  * otherwise take one an earlier attempt made, and the model has the result
23
35
  * of.
24
36
  */
25
- export declare function settle(ctx: restate.SessionContext, stored: (id: string, failed?: string) => boolean): Promise<void>;
37
+ export declare function settleCalls(ctx: restate.SessionContext, stored: (id: string, failed?: string) => boolean): Promise<void>;
38
+ /**
39
+ * Which of `callIds` made a Restate call that answered, in this invocation
40
+ * or in `invocation`: each with the terminal error it answered with
41
+ * (`Error: <message>`), or `undefined` if it succeeded. A tool call whose
42
+ * call did not answer, or that made none, is left out; so is one whose only
43
+ * answered call is its approval, which it ran (or was denied) after.
44
+ *
45
+ * An earlier `invocation`'s calls are read only while they are still
46
+ * stored, until an invocation of the key makes calls of its own; read,
47
+ * never changed. With `prompt`, the calls of its turn only.
48
+ */
49
+ export declare function outcomes(ctx: restate.SessionContext, callIds: Iterable<string>, invocation?: string, prompt?: string): Promise<Map<string, string | undefined>>;
26
50
  //# sourceMappingURL=calls.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"calls.d.ts","sourceRoot":"","sources":["../src/calls.ts"],"names":[],"mappings":"AAoBA,OAAO,KAAK,KAAK,OAAO,MAAM,sBAAsB,CAAC;AA6DrD;;;;;GAKG;AACH,wBAAsB,MAAM,CAAC,GAAG,EAAE,OAAO,CAAC,cAAc,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM;;IAiBlG;;;OAGG;sBACqB,MAAM;GAMjC;AAED;;;;;;;;GAQG;AACH,wBAAsB,MAAM,CAAC,GAAG,EAAE,OAAO,CAAC,cAAc,EAAE,MAAM,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,KAAK,OAAO,iBAMzG"}
1
+ {"version":3,"file":"calls.d.ts","sourceRoot":"","sources":["../src/calls.ts"],"names":[],"mappings":"AA2BA,OAAO,KAAK,KAAK,OAAO,MAAM,sBAAsB,CAAC;AAkFrD;;;;;;GAMG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAAE,GAAG,EAAE,OAAO,CAAC,cAAc,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CASlH;AAED;;;;;;GAMG;AACH,wBAAsB,MAAM,CAC1B,GAAG,EAAE,OAAO,CAAC,cAAc,EAC3B,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,OAAO,EACd,MAAM,EAAE,MAAM,EACd,EAAE,MAAa,EAAE,QAAgB,EAAE,GAAE;IAAE,MAAM,CAAC,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;CAAO;;IAyBhF;;;OAGG;sBACqB,MAAM;GAMjC;AAED;;;;;;;;GAQG;AACH,wBAAsB,WAAW,CAAC,GAAG,EAAE,OAAO,CAAC,cAAc,EAAE,MAAM,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,KAAK,OAAO,iBAM9G;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,QAAQ,CAC5B,GAAG,EAAE,OAAO,CAAC,cAAc,EAC3B,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,EACzB,UAAU,CAAC,EAAE,MAAM,EACnB,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,CAc1C"}
package/dist/calls.js CHANGED
@@ -25,29 +25,67 @@ async function load(ctx, invocation) {
25
25
  }
26
26
  /** Each attempt's id, and its calls, by its context. */
27
27
  const ATTEMPT = new ScopeKey('the calls of an attempt');
28
- /** The attempt of `ctx`: its id, and the calls of its invocation. */
28
+ /** The attempt of `ctx`: its id, and the calls of its invocation (loaded once, shared by calls made at once). */
29
29
  function attemptOf(ctx) {
30
30
  return ATTEMPT.of(ctx, () => ({ id: randomUUID(), calls: load(ctx, ctx.request.id) }));
31
31
  }
32
+ /** `value` as JSON with its objects' keys sorted: the same input, however its keys came, is the same call. */
33
+ function canonical(value) {
34
+ return JSON.stringify(value, (_key, v) => v !== null && typeof v === 'object' && !Array.isArray(v)
35
+ ? Object.fromEntries(Object.entries(v).toSorted(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)))
36
+ : v);
37
+ }
38
+ /** The prompt whose turn the tools of `ctx` run for, while `answerOnce` answers it. */
39
+ const PROMPT = new ScopeKey('the prompt of the calls');
40
+ /**
41
+ * Run `work` (a turn's) with the calls its tools make kept as prompt
42
+ * `prompt`'s: they are matched, and rejoined, within it only. An invocation
43
+ * that prompts twice, its second prompt making the call its first made
44
+ * (the same tool and input), would otherwise take the first's after a crash
45
+ * (its record not yet forgotten), and never make its own.
46
+ */
47
+ export async function forPrompt(ctx, prompt, work) {
48
+ const outer = PROMPT.get(ctx);
49
+ PROMPT.set(ctx, prompt);
50
+ try {
51
+ return await work();
52
+ }
53
+ finally {
54
+ if (outer === undefined)
55
+ PROMPT.delete(ctx);
56
+ else
57
+ PROMPT.set(ctx, outer);
58
+ }
59
+ }
32
60
  /**
33
61
  * The call of tool `tool` with `input` that tool call `callId` makes: the
34
- * one it made before (a run again), or the one an earlier attempt made with
35
- * the same tool and input (made again under a new id), or else a new one.
36
- * `run(id)` makes it with its own id, and stores that it answered.
62
+ * one it made to `tool` before (a run again), or else, if `rejoin`, the one
63
+ * an earlier attempt made with the same tool and input (made again under a
64
+ * new id), or else a new one, an `approval` its tool call waits for if so.
65
+ * `answered()` stores that it answered.
37
66
  */
38
- export async function callOf(ctx, tool, input, callId) {
67
+ export async function callOf(ctx, tool, input, callId, { rejoin = true, approval = false } = {}) {
39
68
  const attempt = attemptOf(ctx);
40
69
  const state = await attempt.calls;
41
- const json = JSON.stringify(input);
42
- let call = state.calls.find((c) => c.ids.includes(callId)) ??
43
- state.calls.find((c) => c.attempt !== attempt.id && c.tool === tool && c.input === json);
70
+ const json = canonical(input);
71
+ const prompt = PROMPT.get(ctx);
72
+ const ours = state.calls.filter((c) => c.prompt === prompt);
73
+ // A tool call may make more than one call (an approval, then the tool's
74
+ // own), each to a tool of its own.
75
+ let call = ours.find((c) => c.ids.includes(callId) && c.tool === tool) ??
76
+ (rejoin ? ours.find((c) => c.attempt !== attempt.id && c.tool === tool && c.input === json) : undefined);
44
77
  if (call) {
45
78
  call.attempt = attempt.id;
46
79
  if (!call.ids.includes(callId))
47
80
  call.ids.push(callId);
48
81
  }
49
82
  else {
50
- state.calls.push((call = { tool, input: json, ids: [callId], attempt: attempt.id }));
83
+ call = { tool, input: json, ids: [callId], attempt: attempt.id };
84
+ if (approval)
85
+ call.approval = true;
86
+ if (prompt !== undefined)
87
+ call.prompt = prompt;
88
+ state.calls.push(call);
51
89
  }
52
90
  await store(ctx, state);
53
91
  const made = call;
@@ -74,7 +112,7 @@ export async function callOf(ctx, tool, input, callId) {
74
112
  * otherwise take one an earlier attempt made, and the model has the result
75
113
  * of.
76
114
  */
77
- export async function settle(ctx, stored) {
115
+ export async function settleCalls(ctx, stored) {
78
116
  const state = await attemptOf(ctx).calls;
79
117
  const kept = state.calls.filter((c) => !c.done || !stored(c.ids.at(-1), c.failed));
80
118
  if (kept.length === state.calls.length)
@@ -82,4 +120,33 @@ export async function settle(ctx, stored) {
82
120
  state.calls = kept;
83
121
  await store(ctx, state);
84
122
  }
123
+ /**
124
+ * Which of `callIds` made a Restate call that answered, in this invocation
125
+ * or in `invocation`: each with the terminal error it answered with
126
+ * (`Error: <message>`), or `undefined` if it succeeded. A tool call whose
127
+ * call did not answer, or that made none, is left out; so is one whose only
128
+ * answered call is its approval, which it ran (or was denied) after.
129
+ *
130
+ * An earlier `invocation`'s calls are read only while they are still
131
+ * stored, until an invocation of the key makes calls of its own; read,
132
+ * never changed. With `prompt`, the calls of its turn only.
133
+ */
134
+ export async function outcomes(ctx, callIds, invocation, prompt) {
135
+ const state = invocation !== undefined && invocation !== ctx.request.id
136
+ ? await load(ctx, invocation)
137
+ : await attemptOf(ctx).calls;
138
+ const wanted = new Set(callIds);
139
+ const found = new Map();
140
+ for (const call of state.calls) {
141
+ if (!call.done || call.approval)
142
+ continue;
143
+ // Another prompt's call, of the same invocation: not this turn's.
144
+ if (prompt !== undefined && call.prompt !== undefined && call.prompt !== prompt)
145
+ continue;
146
+ for (const id of call.ids)
147
+ if (wanted.has(id))
148
+ found.set(id, call.failed);
149
+ }
150
+ return found;
151
+ }
85
152
  //# sourceMappingURL=calls.js.map
package/dist/calls.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"calls.js","sourceRoot":"","sources":["../src/calls.ts"],"names":[],"mappings":"AAqBA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,+BAA+B,CAAC;AACjE,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAoBzC,uFAAuF;AACvF,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,sBAAsB,EAAE;IAChD;;;;IAIE;CACH,CAAC,CAAC;AAKH,oEAAoE;AACpE,KAAK,UAAU,KAAK,CAAC,GAAiB,EAAE,EAAE,UAAU,EAAE,KAAK,EAAS;IAClE,MAAM,GAAG,CAAC,EAAE,CAAC,OAAO,CAAC,sFAAsF,EAAE;QAC3G,UAAU;QACV,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC;KACtB,CAAC,CAAC;AACL,CAAC;AAED,qFAAqF;AACrF,KAAK,UAAU,IAAI,CAAC,GAAiB,EAAE,UAAkB;IACvD,MAAM,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAC5B,MAAM,GAAG,GAAG,MAAM,GAAG,CAAC,EAAE,CAAC,GAAG,CAC1B,mDAAmD,CACpD,CAAC;IACF,OAAO,GAAG,EAAE,UAAU,KAAK,UAAU;QACnC,CAAC,CAAC,EAAE,UAAU,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAW,EAAE;QACxD,CAAC,CAAC,EAAE,UAAU,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;AAChC,CAAC;AAED,wDAAwD;AACxD,MAAM,OAAO,GAAG,IAAI,QAAQ,CAAwC,yBAAyB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A2B,EAC3B,OAAyB,EACzB,UAAmB,EACnB,MAAe;IAEf,MAAM,KAAK,GACT,UAAU,KAAK,SAAS,IAAI,UAAU,KAAK,GAAG,CAAC,OAAO,CAAC,EAAE;QACvD,CAAC,CAAC,MAAM,IAAI,CAAC,GAAG,EAAE,UAAU,CAAC;QAC7B,CAAC,CAAC,MAAM,SAAS,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC;IACjC,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,CAAC;IAChC,MAAM,KAAK,GAAG,IAAI,GAAG,EAA8B,CAAC;IACpD,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,KAAK,EAAE,CAAC;QAC/B,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,QAAQ;YAAE,SAAS;QAC1C,kEAAkE;QAClE,IAAI,MAAM,KAAK,SAAS,IAAI,IAAI,CAAC,MAAM,KAAK,SAAS,IAAI,IAAI,CAAC,MAAM,KAAK,MAAM;YAAE,SAAS;QAC1F,KAAK,MAAM,EAAE,IAAI,IAAI,CAAC,GAAG;YAAE,IAAI,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;gBAAE,KAAK,CAAC,GAAG,CAAC,EAAE,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;IAC5E,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -1,13 +1,23 @@
1
1
  import * as restate from '@restatedev/sessions';
2
2
  import Anthropic from '@anthropic-ai/sdk';
3
3
  import type { BetaRunnableTool, BetaToolRunContext } from '@anthropic-ai/sdk/lib/tools/BetaRunnableTool';
4
- import { type Configure } from '../common.ts';
4
+ import { type Configure, type Summarize } from '../common.ts';
5
+ import { type Items } from '../conversation.ts';
5
6
  import { type Answer } from '../output.ts';
6
7
  import type { z } from 'zod';
7
8
  import { type Definition } from '../object.ts';
8
9
  import { type Tool as AnyTool } from '../tools.ts';
10
+ type MessageParam = Anthropic.Beta.Messages.BetaMessageParam;
9
11
  /** The id of the tool call a tool runs for: the runner always passes it. */
10
12
  export declare const callId: (context?: BetaToolRunContext) => string;
13
+ /**
14
+ * Claude's items: the SDK's messages, as JSON. A user's text is said by the
15
+ * user, an assistant's by the assistant, and its `tool_use` blocks are its
16
+ * calls; a `tool_result` is a result. A server tool's blocks (and a
17
+ * `system` message) say nothing: they leave no open call the API would
18
+ * refuse a result for.
19
+ */
20
+ export declare const items: Items<MessageParam>;
11
21
  /** How much effort the model puts into a reply (Anthropic's `output_config.effort`). */
12
22
  export type Effort = 'low' | 'medium' | 'high' | 'xhigh' | 'max';
13
23
  /** An agent's options: `chat` and `prompt` take them. */
@@ -32,12 +42,23 @@ export interface Options<O extends z.ZodObject | undefined = z.ZodObject | undef
32
42
  * (checked), and `chat` and `prompt` answer with (see output.ts).
33
43
  */
34
44
  output?: O;
45
+ /**
46
+ * Whether the model is asked for `output` natively, with the API's
47
+ * structured outputs (`output_config.format`: the JSON schema the API
48
+ * holds the final reply to), rather than in the prompt (default: if the
49
+ * model has them, as Claude's have since the 4.5 models). A schema the API
50
+ * cannot take (too complex, say) refuses the message: `false` asks in the
51
+ * prompt instead.
52
+ */
53
+ nativeOutput?: boolean;
35
54
  }
36
55
  /**
37
56
  * Answer `text`, once: a retried attempt resumes the same prompt. Each prompt
38
57
  * of an invocation is a prompt of its own.
39
58
  * With `output`, it answers with the value the agent replies with (see
40
- * output.ts), not its text.
59
+ * output.ts), not its text: a reply that is not one is asked again within
60
+ * its turn, once. A model with structured outputs is asked for it natively
61
+ * (`nativeOutput`), else in the prompt.
41
62
  */
42
63
  export declare const prompt: <O extends z.ZodObject | undefined = undefined>(ctx: restate.SessionContext, text: string, options?: Options<O>) => Promise<Answer<O>>;
43
64
  /**
@@ -53,13 +74,19 @@ export declare const chat: <O extends z.ZodObject | undefined = undefined>(optio
53
74
  handler: (ctx: restate.SessionContext, text: string) => Promise<Answer<O>>;
54
75
  };
55
76
  /**
56
- * A handler of the conversation as clients read it: a page at a time, or
57
- * what is new or changed since a cursor, from the log each change writes
58
- * (log.ts). It answers while a message is answered.
77
+ * A view of the conversation as clients read it: a page at a time, or what
78
+ * is new or changed since a cursor; `view: 'summary'` for each turn's
79
+ * message and answer. It answers while a message is answered.
59
80
  */
60
81
  export declare const messages: () => (ctx: restate.ViewContext, request?: import("../common.ts").MessagesRequest) => Promise<import("../common.ts").Messages>;
82
+ /**
83
+ * How an agent of `options` summarizes the earliest turns, past its budget:
84
+ * with its own client and model (`agent()` gives it; for an object of your
85
+ * own, `sessionObject`'s `summarize`).
86
+ */
87
+ export declare const summarizer: (options?: Options) => Summarize;
61
88
  /** An agent: a virtual object `name`, one per key, whose `chat` handler is {@link chat}, and `messages` {@link messages}. */
62
- export declare const agent: <P extends string, O extends z.ZodObject | undefined = undefined>({ name, description, schema, background, ...options }: Options<O> & Definition<P>) => restate.VirtualObjectDefinition<P, {
89
+ export declare const agent: <P extends string, O extends z.ZodObject | undefined = undefined>({ name, description, schema, background, window, budget, summarize, ...options }: Options<O> & Definition<P>) => restate.VirtualObjectDefinition<P, {
63
90
  signal: (ctx: restate.ViewContext, signal: import("../signals.ts").Signal) => Promise<import("../signals.ts").Resolved>;
64
91
  pending: (ctx: restate.ViewContext) => Promise<import("../signals.ts").Waiting[]>;
65
92
  status: (ctx: restate.ViewContext) => Promise<import("../object.ts").Status>;
@@ -68,10 +95,12 @@ export declare const agent: <P extends string, O extends z.ZodObject | undefined
68
95
  watch: (ctx: restate.ViewContext, watch: import("../signals.ts").Watch) => Promise<import("../signals.ts").Notifications>;
69
96
  } & {
70
97
  clear: (ctx: restate.ViewContext) => Promise<import("../object.ts").Stopped>;
71
- sync: (ctx: restate.ViewContext, { cursor, revision, timeoutSeconds, limit }?: import("../object.ts").SyncRequest) => Promise<import("../object.ts").Synced>;
98
+ steer: (ctx: restate.ViewContext, text: string) => Promise<import("../signals.ts").Steered>;
99
+ sync: (ctx: restate.ViewContext, { cursor, revision, timeoutSeconds, limit, view }?: import("../object.ts").SyncRequest) => Promise<import("../object.ts").Synced>;
72
100
  } & {
73
101
  chat: (ctx: restate.SessionContext, text: string) => Promise<Answer<O>>;
74
102
  } & {
75
103
  messages: (ctx: restate.ViewContext, request?: import("../common.ts").MessagesRequest) => Promise<import("../common.ts").Messages>;
76
104
  }>;
105
+ export {};
77
106
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/claude/index.ts"],"names":[],"mappings":"AAyBA,OAAO,KAAK,OAAO,MAAM,sBAAsB,CAAC;AAEhD,OAAO,SAAS,MAAM,mBAAmB,CAAC;AAC1C,OAAO,KAAK,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,8CAA8C,CAAC;AACzG,OAAO,EAML,KAAK,SAAS,EAef,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,KAAK,MAAM,EAAc,MAAM,cAAc,CAAC;AACvD,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,EAAE,KAAK,UAAU,EAAiB,MAAM,cAAc,CAAC;AAC9D,OAAO,EAAwB,KAAK,IAAI,IAAI,OAAO,EAAE,MAAM,aAAa,CAAC;AAGzE,4EAA4E;AAC5E,eAAO,MAAM,MAAM,aAAc,kBAAkB,WAGlD,CAAC;AAqIF,wFAAwF;AACxF,MAAM,MAAM,MAAM,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,OAAO,GAAG,KAAK,CAAC;AAEjE,yDAAyD;AACzD,MAAM,WAAW,OAAO,CAAC,CAAC,SAAS,CAAC,CAAC,SAAS,GAAG,SAAS,GAAG,CAAC,CAAC,SAAS,GAAG,SAAS;IAClF,sFAAsF;IACtF,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,yBAAyB;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,4GAA4G;IAC5G,KAAK,CAAC,EAAE,CAAC,OAAO,GAAG,gBAAgB,CAAC,EAAE,CAAC;IACvC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