@restatedev/agents 0.2.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 -8
  3. package/dist/calls.d.ts.map +1 -1
  4. package/dist/calls.js +79 -18
  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 +238 -156
  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 +161 -71
  14. package/dist/common.d.ts.map +1 -1
  15. package/dist/common.js +277 -226
  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 +323 -107
  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 +234 -180
  52. package/dist/langgraph/index.js.map +1 -1
  53. package/dist/langgraph/nodes.d.ts +70 -0
  54. package/dist/langgraph/nodes.d.ts.map +1 -0
  55. package/dist/langgraph/nodes.js +145 -0
  56. package/dist/langgraph/nodes.js.map +1 -0
  57. package/dist/messages.d.ts +18 -0
  58. package/dist/messages.d.ts.map +1 -0
  59. package/dist/messages.js +182 -0
  60. package/dist/messages.js.map +1 -0
  61. package/dist/object.d.ts +65 -23
  62. package/dist/object.d.ts.map +1 -1
  63. package/dist/object.js +123 -40
  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 +75 -7
  78. package/dist/signals.d.ts.map +1 -1
  79. package/dist/signals.js +154 -27
  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 +110 -24
  91. package/src/claude/README.md +41 -20
  92. package/src/claude/index.ts +271 -183
  93. package/src/client.ts +14 -3
  94. package/src/common.ts +354 -247
  95. package/src/conversation.ts +1527 -0
  96. package/src/deepseek/README.md +78 -11
  97. package/src/deepseek/index.ts +385 -138
  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 +337 -193
  106. package/src/langgraph/nodes.ts +243 -0
  107. package/src/messages.ts +244 -0
  108. package/src/object.ts +178 -48
  109. package/src/output.ts +52 -31
  110. package/src/pi/README.md +18 -3
  111. package/src/pi/index.ts +346 -79
  112. package/src/pi/storage.ts +20 -7
  113. package/src/signals.ts +235 -31
  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 -145
  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 -193
  134. package/src/opencode/README.md +0 -125
  135. package/src/opencode/db.ts +0 -223
  136. package/src/opencode/index.ts +0 -559
@@ -21,12 +21,14 @@ const assistant = deepseek.agent({ name: 'assistant', system: 'Answer briefly.',
21
21
  await serve(assistant);
22
22
  ```
23
23
 
24
- | Option | What |
25
- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
26
- | `model` | `provider/model-id`; or else `MODEL`, or else `deepseek/deepseek-flash` (see below) |
27
- | `system` | The system prompt |
28
- | `tools` | `tool()`s and Restate's tools of `@restatedev/agents`, or the harness's `defineTool`s |
29
- | `output` | A zod object: the answer as data, which the agent replies with as JSON, and `chat` answers with (see [Structured answers](../../README.md#structured-answers)) |
24
+ | Option | What |
25
+ | ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
26
+ | `model` | `provider/model-id`; or else `MODEL`, or else `deepseek/deepseek-flash` (see below) |
27
+ | `system` | The system prompt |
28
+ | `tools` | `tool()`s and Restate's tools of `@restatedev/agents`, or the harness's `defineTool`s |
29
+ | `approval` | The tool calls that wait for a person's approval before they run: the names of their tools, or a function of a call (see [Approvals](#approvals)) |
30
+ | `output` | A zod object: the answer as data, which the agent replies with as JSON, and `chat` answers with (see [Structured answers](../../README.md#structured-answers)) |
31
+ | `compaction` | When the model's input is condensed: `@deepseek-ai/dsh-compaction-basic`'s settings, such as `thresholdRatio` (by default 0.8 of the model's context window) |
30
32
 
31
33
  ## Models
32
34
 
@@ -50,6 +52,57 @@ invocation's context (each user's system prompt, say). `deepseek.prompt(ctx,
50
52
  text, options)` answers a prompt in a handler of your own, once, however
51
53
  often the handler runs; each prompt of an invocation is a prompt of its own.
52
54
 
55
+ ## Approvals
56
+
57
+ The harness can ask a person before a tool call runs. `approval` names the
58
+ calls that ask: by their tools, or as a function of a call (its tool's
59
+ `name`, and its `args`):
60
+
61
+ ```ts
62
+ deepseek.agent({ name: 'bank', tools: [transfer, audit], approval: ['transfer'] });
63
+ deepseek.agent({
64
+ name: 'bank',
65
+ tools: [transfer, audit],
66
+ approval: ({ name, args }) => name === 'transfer' && (args as { amount: number }).amount > 100,
67
+ });
68
+ ```
69
+
70
+ Such a call waits, before it runs, for the signal `approval`
71
+ (`deepseek.APPROVAL`) to the agent, as a `signal()` tool waits
72
+ ([Signals](../../README.md#signals)): without holding a process, for as
73
+ long as it takes. `pending` lists the wait, with the call's tool and
74
+ arguments, and `signal` answers it, by its id, or as the oldest wait for
75
+ `approval`:
76
+
77
+ ```sh
78
+ curl localhost:8080/bank/alice/pending
79
+ # [{"id": "inv_1f…:call_01", "name": "approval", "input": {"tool": "transfer", "args": {"from": 1, "to": 2, "amount": 500}}, "since": "…"}]
80
+ curl localhost:8080/bank/alice/signal --json '{"id": "inv_1f…:call_01", "value": "approved"}'
81
+ curl localhost:8080/bank/alice/signal --json '{"name": "approval", "value": "no"}'
82
+ ```
83
+
84
+ `true`, `{"approved": true}`, or a text such as `approved`, `yes` or `ok`
85
+ approves the call, once; anything else denies it, and the tool never runs.
86
+ The model gets the harness's denial (`Error: the user rejected tool
87
+ "transfer"`): the text of a denial does not reach it.
88
+
89
+ It is the harness's own approval: a policy of its tool runtime
90
+ (`tools/pre-execute`) asks, and its approval service
91
+ (`@deepseek-ai/dsh-user-approval`, mounted with `approval`) puts the
92
+ question to its answerer, the wait. The log keeps each question and its
93
+ decision (`approval/asked`, `approval/decided`), and the model is told, in
94
+ the harness's runtime context, that a call may ask for an approval.
95
+
96
+ A crash while a call waits, or a suspension (a wait that outlasts
97
+ Restate's inactivity timeout restarts the handler), asks the model again
98
+ ([below](#through-crashes)): it makes the call again under a new id, and
99
+ the call, with the same arguments, joins the wait. The person is asked
100
+ once, and the tool runs once, after the decision. A call with other
101
+ arguments, such as a transfer retried with `retryOf` after a crash left its
102
+ outcome unknown, is a call of its own, and asks anew. A turn stopped while
103
+ the call waits, or while the approved tool runs, ends the call as
104
+ cancelled: its approval is not its outcome.
105
+
53
106
  ## Through crashes
54
107
 
55
108
  The persistence ([`persistence.ts`](persistence.ts),
@@ -78,10 +131,23 @@ it, and leaves the model to decide what to do about it. So:
78
131
  without a result, as above.
79
132
  - **A failed turn fails the attempt**: a model request that failed, or a
80
133
  checkpoint that could not store the log, and Restate retries.
81
- - **A message is answered once per invocation**: the message carries the
82
- invocation id, and a retried attempt returns the answer of the turn that
83
- finished it. A message that a crash interrupted is finished before the next
84
- one starts.
134
+ - **A message is answered once per invocation** (`answerOnce`): a retried
135
+ attempt continues its own prompt (`CONTINUE`, a step of its turn), and
136
+ returns the answer once it has one. A turn a killed invocation left is
137
+ closed (`ended`) in the agent's conversation of turns, never continued,
138
+ and the harness's next turn is told how it ended.
139
+
140
+ The harness keeps its own event log, and its model's input is the harness's;
141
+ each flush of the log copies the turn's new messages (the model's replies,
142
+ the tools' results) into the agent's conversation of turns, in the same
143
+ transaction: what `messages` shows
144
+ ([docs/conversation.md](../../docs/conversation.md)). The harness's input
145
+ stays within the model's context window: as it nears the window (or
146
+ overflows it), the harness condenses the oldest history into a summary, one
147
+ more model request, after trimming tool results that are too long
148
+ (`@deepseek-ai/dsh-compaction-basic`, with the token meter and the tool
149
+ result pruner). The log keeps every event, and `messages` every message; a
150
+ crash while condensing is condensed again by the retry.
85
151
 
86
152
  ## Run
87
153
 
@@ -101,7 +167,8 @@ which makes a fresh tool call id for every call, as a model does
101
167
 
102
168
  - [`test/deepseek.test.ts`](../../test/deepseek.test.ts):
103
169
  each point above, with crashes that stop an attempt in a tool call, or
104
- at any of its state requests; and
170
+ at any of its state requests; approvals (approved, denied, waited for
171
+ through a crash, at any state request, and cancelled); and
105
172
  `messages`. Without the checkpoint policy four of its tests fail, and with
106
173
  a transfer tool that ignores `retryOf` three do.
107
174
  - [`test/deepseek.ts`](../../test/deepseek.ts): the