@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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@restatedev/agents",
3
- "version": "0.11.2",
4
- "description": "Agent frameworks in Restate virtual objects, their state in the object's SQL database: pi, OpenCode, LangGraph, Claude (Anthropic SDK) and DeepSeek Harness; and agentlib, journaled",
3
+ "version": "0.12.0",
4
+ "description": "pi on Restate session objects, with its storage in the object's SQL database",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
@@ -15,266 +15,23 @@
15
15
  "type": "module",
16
16
  "sideEffects": false,
17
17
  "exports": {
18
- ".": {
19
- "types": "./dist/index.d.ts",
20
- "default": "./dist/index.js"
21
- },
22
- "./agentlib": {
23
- "types": "./dist/agentlib/index.d.ts",
24
- "default": "./dist/agentlib/index.js"
25
- },
26
- "./claude": {
27
- "types": "./dist/claude/index.d.ts",
28
- "default": "./dist/claude/index.js"
29
- },
30
- "./client": {
31
- "types": "./dist/client.d.ts",
32
- "default": "./dist/client.js"
33
- },
34
- "./deepseek": {
35
- "types": "./dist/deepseek/index.d.ts",
36
- "default": "./dist/deepseek/index.js"
37
- },
38
- "./langgraph": {
39
- "types": "./dist/langgraph/index.d.ts",
40
- "default": "./dist/langgraph/index.js"
41
- },
42
- "./opencode": {
43
- "types": "./dist/opencode/index.d.ts",
44
- "default": "./dist/opencode/index.js"
45
- },
46
- "./opencode/db": {
47
- "types": "./dist/opencode/db.d.ts",
48
- "default": "./dist/opencode/db.js"
49
- },
50
18
  "./pi": {
51
19
  "types": "./dist/pi/index.d.ts",
52
20
  "default": "./dist/pi/index.js"
53
- },
54
- "./pi/storage": {
55
- "types": "./dist/pi/storage.d.ts",
56
- "default": "./dist/pi/storage.js"
57
21
  }
58
22
  },
59
- "dependencies": {
60
- "@restatedev/restate-sdk-clients": "1.18.2",
61
- "@restatedev/restate-sdk-core": "1.18.2",
62
- "@standard-schema/spec": "1.1.0"
63
- },
64
23
  "devDependencies": {
65
- "@ai-sdk/anthropic": "4.0.14",
66
- "@ai-sdk/openai": "4.0.13",
67
- "@ai-sdk/provider": "3.0.8",
68
- "@anthropic-ai/sdk": "1.8.0",
69
- "@deepseek-ai/cordis": "4.1.0",
70
- "@deepseek-ai/cordis-plugin-loader": "1.1.0",
71
- "@deepseek-ai/dsh-agent": "0.2.2-rc.2",
72
- "@deepseek-ai/dsh-agent-loop": "0.2.2-rc.2",
73
- "@deepseek-ai/dsh-anonymous-user-id": "0.2.2-rc.2",
74
- "@deepseek-ai/dsh-atomic-write": "0.2.2-rc.2",
75
- "@deepseek-ai/dsh-attachment": "0.2.2-rc.2",
76
- "@deepseek-ai/dsh-authorization": "0.2.2-rc.2",
77
- "@deepseek-ai/dsh-commands": "0.2.2-rc.2",
78
- "@deepseek-ai/dsh-compaction": "0.2.2-rc.2",
79
- "@deepseek-ai/dsh-compaction-basic": "0.2.2-rc.2",
80
- "@deepseek-ai/dsh-compaction-tool-result-pruner": "0.2.2-rc.2",
81
- "@deepseek-ai/dsh-credentials": "0.2.2-rc.2",
82
- "@deepseek-ai/dsh-deepseek-llm-api-extensions": "0.2.2-rc.2",
83
- "@deepseek-ai/dsh-fs": "0.2.2-rc.2",
84
- "@deepseek-ai/dsh-home-paths": "0.2.2-rc.2",
85
- "@deepseek-ai/dsh-launch-environment": "0.2.2-rc.2",
86
- "@deepseek-ai/dsh-llm": "0.2.2-rc.2",
87
- "@deepseek-ai/dsh-llm-deepseek": "0.2.2-rc.2",
88
- "@deepseek-ai/dsh-llm-deepseek-api-key": "0.2.2-rc.2",
89
- "@deepseek-ai/dsh-llm-pi-ai": "0.2.2-rc.2",
90
- "@deepseek-ai/dsh-ptc-runtime": "0.2.2-rc.2",
91
- "@deepseek-ai/dsh-sandbox": "0.2.2-rc.2",
92
- "@deepseek-ai/dsh-sandbox-policy": "0.2.2-rc.2",
93
- "@deepseek-ai/dsh-scope": "0.2.2-rc.2",
94
- "@deepseek-ai/dsh-session": "0.2.2-rc.2",
95
- "@deepseek-ai/dsh-session-checkpoint-policy": "0.2.2-rc.2",
96
- "@deepseek-ai/dsh-session-persistence": "0.2.2-rc.2",
97
- "@deepseek-ai/dsh-session-projection": "0.2.2-rc.2",
98
- "@deepseek-ai/dsh-system-prompt": "0.2.2-rc.2",
99
- "@deepseek-ai/dsh-timeout": "0.2.2-rc.2",
100
- "@deepseek-ai/dsh-token-meter": "0.2.2-rc.2",
101
- "@deepseek-ai/dsh-tool-executor-native": "0.2.2-rc.2",
102
- "@deepseek-ai/dsh-tools": "0.2.2-rc.2",
103
- "@deepseek-ai/dsh-user-approval": "0.2.2-rc.2",
104
- "@earendil-works/chord": "1.5.2",
105
- "@earendil-works/pi-ai": "1.5.2",
106
- "@earendil-works/pi-durable": "1.5.2",
107
- "@earendil-works/pi-schema": "1.5.2",
108
- "@langchain/anthropic": "1.6.4",
109
- "@langchain/core": "1.4.0",
110
- "@langchain/langgraph": "1.7.3",
111
- "@langchain/langgraph-checkpoint": "1.2.2",
112
- "@langchain/langgraph-checkpoint-validation": "1.2.0",
113
- "@langchain/openai": "1.8.4",
114
- "@opencode/core": "2.2.7",
115
- "@opencode/plugin": "2.2.7",
116
- "@opencode/sdk": "2.2.7",
117
- "@opencode/server": "2.2.7",
118
- "@restatedev/agentlib": "0.2.0",
119
- "@restatedev/restate-sdk": "1.18.2",
120
- "@restatedev/restate-sdk-gen": "1.18.2",
121
- "@restatedev/sessions": "0.11.2",
122
- "effect": "4.1.0",
123
- "zod": "4.6.7"
24
+ "@earendil-works/chord": "1.7.0",
25
+ "@earendil-works/pi-ai": "1.7.0",
26
+ "@earendil-works/pi-durable": "1.7.0",
27
+ "@earendil-works/pi-schema": "1.7.0",
28
+ "@restatedev/sessions": "0.12.0"
124
29
  },
125
30
  "peerDependencies": {
126
- "@ai-sdk/anthropic": "^4.0.14",
127
- "@ai-sdk/openai": "^4.0.13",
128
- "@anthropic-ai/sdk": "^1.8.0",
129
- "@deepseek-ai/cordis": "~4.1.0",
130
- "@deepseek-ai/dsh-agent": "0.2.2-rc.2",
131
- "@deepseek-ai/dsh-agent-loop": "0.2.2-rc.2",
132
- "@deepseek-ai/dsh-commands": "0.2.2-rc.2",
133
- "@deepseek-ai/dsh-compaction": "0.2.2-rc.2",
134
- "@deepseek-ai/dsh-compaction-basic": "0.2.2-rc.2",
135
- "@deepseek-ai/dsh-compaction-tool-result-pruner": "0.2.2-rc.2",
136
- "@deepseek-ai/dsh-llm": "0.2.2-rc.2",
137
- "@deepseek-ai/dsh-llm-deepseek-api-key": "0.2.2-rc.2",
138
- "@deepseek-ai/dsh-llm-pi-ai": "0.2.2-rc.2",
139
- "@deepseek-ai/dsh-session": "0.2.2-rc.2",
140
- "@deepseek-ai/dsh-session-checkpoint-policy": "0.2.2-rc.2",
141
- "@deepseek-ai/dsh-session-persistence": "0.2.2-rc.2",
142
- "@deepseek-ai/dsh-session-projection": "0.2.2-rc.2",
143
- "@deepseek-ai/dsh-system-prompt": "0.2.2-rc.2",
144
- "@deepseek-ai/dsh-token-meter": "0.2.2-rc.2",
145
- "@deepseek-ai/dsh-tool-executor-native": "0.2.2-rc.2",
146
- "@deepseek-ai/dsh-tools": "0.2.2-rc.2",
147
- "@deepseek-ai/dsh-user-approval": "0.2.2-rc.2",
148
- "@earendil-works/chord": "~1.5.2",
149
- "@earendil-works/pi-ai": "~1.5.2",
150
- "@earendil-works/pi-durable": "~1.5.2",
151
- "@langchain/anthropic": "^1.6.4",
152
- "@langchain/core": "^1.4.0",
153
- "@langchain/langgraph": "^1.7.3",
154
- "@langchain/langgraph-checkpoint": "^1.2.2",
155
- "@langchain/openai": "^1.8.4",
156
- "@opencode/core": "2.2.7",
157
- "@opencode/plugin": "2.2.7",
158
- "@opencode/sdk": "2.2.7",
159
- "@opencode/server": "2.2.7",
160
- "@restatedev/agentlib": "~0.2.0",
161
- "@restatedev/restate-sdk-gen": "1.18.2",
162
- "@restatedev/sessions": "^0.11.2",
163
- "effect": "4.1.0",
164
- "zod": "^4.6.7"
165
- },
166
- "peerDependenciesMeta": {
167
- "@ai-sdk/anthropic": {
168
- "optional": true
169
- },
170
- "@ai-sdk/openai": {
171
- "optional": true
172
- },
173
- "@anthropic-ai/sdk": {
174
- "optional": true
175
- },
176
- "@deepseek-ai/cordis": {
177
- "optional": true
178
- },
179
- "@deepseek-ai/dsh-agent": {
180
- "optional": true
181
- },
182
- "@deepseek-ai/dsh-agent-loop": {
183
- "optional": true
184
- },
185
- "@deepseek-ai/dsh-commands": {
186
- "optional": true
187
- },
188
- "@deepseek-ai/dsh-compaction": {
189
- "optional": true
190
- },
191
- "@deepseek-ai/dsh-compaction-basic": {
192
- "optional": true
193
- },
194
- "@deepseek-ai/dsh-compaction-tool-result-pruner": {
195
- "optional": true
196
- },
197
- "@deepseek-ai/dsh-llm": {
198
- "optional": true
199
- },
200
- "@deepseek-ai/dsh-llm-deepseek-api-key": {
201
- "optional": true
202
- },
203
- "@deepseek-ai/dsh-llm-pi-ai": {
204
- "optional": true
205
- },
206
- "@deepseek-ai/dsh-session": {
207
- "optional": true
208
- },
209
- "@deepseek-ai/dsh-session-checkpoint-policy": {
210
- "optional": true
211
- },
212
- "@deepseek-ai/dsh-session-persistence": {
213
- "optional": true
214
- },
215
- "@deepseek-ai/dsh-session-projection": {
216
- "optional": true
217
- },
218
- "@deepseek-ai/dsh-system-prompt": {
219
- "optional": true
220
- },
221
- "@deepseek-ai/dsh-token-meter": {
222
- "optional": true
223
- },
224
- "@deepseek-ai/dsh-tool-executor-native": {
225
- "optional": true
226
- },
227
- "@deepseek-ai/dsh-tools": {
228
- "optional": true
229
- },
230
- "@deepseek-ai/dsh-user-approval": {
231
- "optional": true
232
- },
233
- "@earendil-works/chord": {
234
- "optional": true
235
- },
236
- "@earendil-works/pi-ai": {
237
- "optional": true
238
- },
239
- "@earendil-works/pi-durable": {
240
- "optional": true
241
- },
242
- "@langchain/anthropic": {
243
- "optional": true
244
- },
245
- "@langchain/core": {
246
- "optional": true
247
- },
248
- "@langchain/langgraph": {
249
- "optional": true
250
- },
251
- "@langchain/langgraph-checkpoint": {
252
- "optional": true
253
- },
254
- "@langchain/openai": {
255
- "optional": true
256
- },
257
- "@opencode/core": {
258
- "optional": true
259
- },
260
- "@opencode/plugin": {
261
- "optional": true
262
- },
263
- "@opencode/sdk": {
264
- "optional": true
265
- },
266
- "@opencode/server": {
267
- "optional": true
268
- },
269
- "@restatedev/agentlib": {
270
- "optional": true
271
- },
272
- "@restatedev/restate-sdk-gen": {
273
- "optional": true
274
- },
275
- "effect": {
276
- "optional": true
277
- }
31
+ "@earendil-works/chord": "~1.7.0",
32
+ "@earendil-works/pi-ai": "~1.7.0",
33
+ "@earendil-works/pi-durable": "~1.7.0",
34
+ "@restatedev/sessions": "^0.12.0"
278
35
  },
279
36
  "engines": {
280
37
  "node": ">=24"
@@ -0,0 +1,231 @@
1
+ // `withPi`: pi on a session object, configured once at module scope, and
2
+ // handed to each invocation's closure as a fresh registry, `ask`, `messages`
3
+ // and pi's own Harness. The Harness is opened on first use, with the wiring
4
+ // pi/index.ts describes, and closed when the closure ends.
5
+ import { BACKGROUND_CONTEXT, type Context } from '@earendil-works/chord/context';
6
+ import type { Message, Models } from '@earendil-works/pi-ai';
7
+ // pi-ai's subpath, as pi-durable imports it: its root index loads every provider.
8
+ import { contentText } from '@earendil-works/pi-ai/utils/text';
9
+ import {
10
+ type AgentChange,
11
+ createRegistry,
12
+ defineExtension,
13
+ Harness,
14
+ type HarnessOptions,
15
+ type Registry,
16
+ ROOT_CONVERSATION_ID,
17
+ type ToolRegistration,
18
+ } from '@earendil-works/pi-durable';
19
+ import * as restate from '@restatedev/sessions';
20
+ import { keptModels } from './keepalive.ts';
21
+ import { abortLeft } from './left.ts';
22
+ import { openSqlKvStorage } from './storage.ts';
23
+ import { keptTools } from './tools.ts';
24
+
25
+ /** The name of the extension that holds the `tools` option's tools. */
26
+ const TOOLS = 'withPi.tools';
27
+
28
+ /** The longest provider's message a failed turn's span carries, in characters. */
29
+ const DETAIL = 1000;
30
+
31
+ /**
32
+ * The `TerminalError` of a turn that settled `unanswered`: pi's reason only. pi's detail (the provider's message,
33
+ * which can echo what the request carried) goes to the trace instead: a span `withPi.ask`, opened for nothing but
34
+ * this, ends with an error that says the reason and the detail, bounded.
35
+ */
36
+ async function unanswered(
37
+ ctx: restate.SessionContext,
38
+ reason: string,
39
+ detail: unknown,
40
+ ): Promise<restate.TerminalError> {
41
+ const said = `unanswered: ${reason}`;
42
+ const text = detail === undefined ? '' : typeof detail === 'string' ? detail : JSON.stringify(detail);
43
+ const bounded = text.length > DETAIL ? `${text.slice(0, DETAIL)}…` : text;
44
+ await ctx
45
+ .span('withPi.ask', () => Promise.reject(new Error(bounded === '' ? said : `${said}: ${bounded}`)))
46
+ .catch(() => undefined);
47
+ return new restate.TerminalError(said);
48
+ }
49
+
50
+ /** `withPi`'s options: pi's `Harness.open` options but the registry, which each invocation gets fresh. */
51
+ export type PiOptions<Tool extends ToolRegistration = ToolRegistration> = Omit<
52
+ HarnessOptions<Tool>,
53
+ 'registry' | 'readOnly'
54
+ > & {
55
+ /**
56
+ * The invocation's tools, given its `ctx`: called once per invocation, before the closure runs, and installed in
57
+ * its `registry` as one extension named `withPi.tools`. The name is the same on every invocation, as pi needs for a
58
+ * conversation that selects extensions by name (`agent.extensions`, `settings.extensions`): a selection that does
59
+ * not name `withPi.tools` leaves these tools out. Tools installed in the
60
+ * closure are added to them; an extension of that name installed there replaces them. `Tool` is not inferred from
61
+ * these, so the closure's registry takes other tools too.
62
+ */
63
+ tools?: (ctx: restate.SessionContext) => NoInfer<Tool>[];
64
+ /**
65
+ * The agent the root conversation is created with, at the first `ask` on a key. pi keeps a conversation's agent:
66
+ * on a key whose conversation exists this changes nothing; pi's `configure` on the root (through `harness()`)
67
+ * changes it.
68
+ */
69
+ agent?: AgentChange;
70
+ /** The chord context of pi's calls. Default `BACKGROUND_CONTEXT`. */
71
+ context?: Context;
72
+ };
73
+
74
+ /** `ask`'s options. */
75
+ export type AskOptions = {
76
+ /** The submission's id, given to pi as its `requestId` as it is. Default: the invocation id (`ctx.request.id`). */
77
+ requestId?: string;
78
+ };
79
+
80
+ /** What an invocation's closure is handed. Everything in it is this invocation's, and ends with the closure. */
81
+ export type Pi<Tool extends ToolRegistration = ToolRegistration> = {
82
+ /**
83
+ * This invocation's tools, the `tools` option's already in it. Install more before the first `ask` or
84
+ * `harness()`: from then on pi may run again a tool call a crash cut off (once its scheduler runs, at `ask`'s
85
+ * submit), and a call whose tool it does not find then ends as interrupted ("may have partially run").
86
+ */
87
+ readonly registry: Registry<Tool>;
88
+ /**
89
+ * Asks `message` in the root conversation and returns the text of the submission's answer. The message is submitted
90
+ * under `options.requestId`, by default the invocation id, so a retry finds its submission and waits for its run.
91
+ * One id is one submission: an id the conversation already holds, in this attempt or from another invocation,
92
+ * returns that submission's answer, without storing `message`. To ask again in one invocation, pass another
93
+ * `requestId`. A run that does not end `done` is a `TerminalError` whose message is `unanswered: <pi's reason>`;
94
+ * pi's detail (the provider's message), up to 1000 characters, is on a span `withPi.ask` of the invocation.
95
+ */
96
+ readonly ask: (message: string, options?: AskOptions) => Promise<string>;
97
+ /**
98
+ * The root conversation's messages, `[]` before its first turn. Read through a read-only Harness, which runs
99
+ * nothing, unless this invocation has opened pi for writing already. Opening pi's storage commits its migrations on
100
+ * a key never written, and the writer row (left.ts) after a closure of this attempt that opened pi for writing and
101
+ * wrote nothing.
102
+ */
103
+ readonly messages: () => Promise<readonly Message[]>;
104
+ /**
105
+ * pi's Harness, opened for writing: model requests and tool calls kept alive and stopped by the stop signal, no
106
+ * restart limit unless `settings` give one, and a run another invocation left aborted. `ask` uses the same one.
107
+ */
108
+ readonly harness: () => Promise<Harness>;
109
+ };
110
+
111
+ /** `options` with model requests and tool calls kept alive, and no restart limit unless the author gives one. */
112
+ function kept<Tool extends ToolRegistration>(
113
+ ctx: restate.SessionContext,
114
+ options: HarnessOptions<Tool>,
115
+ ): HarnessOptions<Tool> {
116
+ const { models, settings } = options;
117
+ // pi reads a function's models at each use: wrap each `Models` once.
118
+ const wrapped = new WeakMap<Models, Models>();
119
+ const keep = (m: Models) => wrapped.get(m) ?? wrapped.set(m, keptModels(ctx, m)).get(m)!;
120
+ return {
121
+ ...options,
122
+ models: typeof models === 'function' ? () => keep(models()) : keep(models),
123
+ registry: keptTools(ctx, options.registry),
124
+ settings:
125
+ typeof settings === 'function'
126
+ ? () => {
127
+ const given = settings();
128
+ return { ...given, restartLimit: given?.restartLimit ?? Infinity };
129
+ }
130
+ : { ...settings, restartLimit: settings?.restartLimit ?? Infinity },
131
+ };
132
+ }
133
+
134
+ /** The root conversation's messages in `harness`, `[]` if it has none. */
135
+ async function rootMessages(harness: Harness, context: Context): Promise<readonly Message[]> {
136
+ // Not `harness.root`, which would create the conversation: a read-only Harness refuses that.
137
+ const root = await harness.conversation(ROOT_CONVERSATION_ID, context);
138
+ return root ? (await root.context(context)).messages : [];
139
+ }
140
+
141
+ /**
142
+ * pi on the key's database, for each invocation's closure:
143
+ *
144
+ * const pi = withPi({ models, agent: { model }, tools: (ctx) => [transfer(ctx)] });
145
+ *
146
+ * chat: (ctx, message: string) => pi(ctx, ({ ask }) => ask(message)),
147
+ * history: (ctx) => pi(ctx, ({ messages }) => messages()),
148
+ *
149
+ * The closure gets a fresh `registry`, `ask`, `messages` and `harness` (see `Pi`); a tool decided per call is
150
+ * installed in its `registry`. Nothing is opened until one of them is used, and whatever was opened is closed when
151
+ * the closure ends, however it ends.
152
+ */
153
+ export function withPi<Tool extends ToolRegistration = ToolRegistration>(options: PiOptions<Tool>) {
154
+ const { tools, agent, context = BACKGROUND_CONTEXT, ...open } = options;
155
+ return async <T>(ctx: restate.SessionContext, work: (pi: Pi<Tool>) => T | Promise<T>): Promise<T> => {
156
+ const registry = createRegistry<Tool>();
157
+ if (tools !== undefined) registry.install(defineExtension({ name: TOOLS, tools: tools(ctx) }));
158
+ let ended = false;
159
+ let writer: Promise<Harness> | undefined;
160
+ const live = () => {
161
+ if (ended) throw new Error('withPi: this invocation has ended; use pi inside its closure');
162
+ };
163
+ const harness = async () => {
164
+ live();
165
+ writer ??= (async () => {
166
+ const opened = await Harness.open(await openSqlKvStorage(ctx.db), kept(ctx, { ...open, registry }), context);
167
+ try {
168
+ await abortLeft(ctx, opened, context);
169
+ } catch (error) {
170
+ await opened.close(context);
171
+ throw error;
172
+ }
173
+ return opened;
174
+ })();
175
+ return writer;
176
+ };
177
+ const pi: Pi<Tool> = {
178
+ registry,
179
+ harness,
180
+ ask: async (message, given) => {
181
+ live();
182
+ const requestId = given?.requestId ?? ctx.request.id;
183
+ const root = await (await harness()).root(context, { agent });
184
+ const submission = await root.submit({ type: 'input', content: message, requestId }, context);
185
+ const settled = await submission.wait(context);
186
+ if (settled.type !== 'input' || settled.status !== 'done') {
187
+ throw await unanswered(ctx, settled.reason ?? settled.status, settled.detail);
188
+ }
189
+ // The submission's answer: the reply that settled it, read back through pi's storage.
190
+ const { items } = await root.entries(
191
+ { minEntryId: settled.answer, maxEntryId: settled.answer },
192
+ 1,
193
+ undefined,
194
+ context,
195
+ );
196
+ const reply = items[0]?.model?.findLast((m) => m.role === 'assistant');
197
+ if (reply === undefined) throw new Error(`withPi: pi stored no reply as the answer ${settled.answer}`);
198
+ return contentText(reply.content, '');
199
+ },
200
+ messages: async () => {
201
+ live();
202
+ if (writer !== undefined) return rootMessages(await writer, context);
203
+ const reader = await Harness.open(
204
+ await openSqlKvStorage(ctx.db),
205
+ { ...open, registry, readOnly: true },
206
+ context,
207
+ );
208
+ try {
209
+ return await rootMessages(reader, context);
210
+ } finally {
211
+ await reader.close(context);
212
+ }
213
+ },
214
+ };
215
+ const end = async () => {
216
+ ended = true;
217
+ const opened = await writer?.catch(() => undefined);
218
+ await opened?.close(context);
219
+ };
220
+ let result: T;
221
+ try {
222
+ result = await work(pi);
223
+ } catch (error) {
224
+ // The closure's error is the invocation's: a close that fails too does not replace it.
225
+ await end().catch(() => undefined);
226
+ throw error;
227
+ }
228
+ await end();
229
+ return result;
230
+ };
231
+ }