@puku-ai/sdk 4.0.0 → 4.0.2

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 (583) hide show
  1. package/README.md +29 -10
  2. package/dist/index.d.ts +26 -0
  3. package/dist/index.js +32 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/vendor/_vendor/partial-json-parser/parser.d.ts +2 -0
  6. package/dist/vendor/_vendor/partial-json-parser/parser.js +223 -0
  7. package/dist/vendor/_vendor/partial-json-parser/parser.js.map +1 -0
  8. package/dist/vendor/api-promise.d.ts +2 -0
  9. package/dist/vendor/api-promise.js +3 -0
  10. package/dist/vendor/api-promise.js.map +1 -0
  11. package/dist/vendor/client.d.ts +399 -0
  12. package/dist/vendor/client.js +976 -0
  13. package/dist/vendor/client.js.map +1 -0
  14. package/dist/vendor/core/api-promise.d.ts +49 -0
  15. package/dist/vendor/core/api-promise.js +77 -0
  16. package/dist/vendor/core/api-promise.js.map +1 -0
  17. package/dist/vendor/core/api.d.ts +10 -0
  18. package/dist/vendor/core/api.js +2 -0
  19. package/dist/vendor/core/api.js.map +1 -0
  20. package/dist/vendor/core/credentials.d.ts +130 -0
  21. package/dist/vendor/core/credentials.js +274 -0
  22. package/dist/vendor/core/credentials.js.map +1 -0
  23. package/dist/vendor/core/error.d.ts +82 -0
  24. package/dist/vendor/core/error.js +140 -0
  25. package/dist/vendor/core/error.js.map +1 -0
  26. package/dist/vendor/core/middleware.d.ts +153 -0
  27. package/dist/vendor/core/middleware.js +168 -0
  28. package/dist/vendor/core/middleware.js.map +1 -0
  29. package/dist/vendor/core/pagination.d.ts +121 -0
  30. package/dist/vendor/core/pagination.js +207 -0
  31. package/dist/vendor/core/pagination.js.map +1 -0
  32. package/dist/vendor/core/resource.d.ts +5 -0
  33. package/dist/vendor/core/resource.js +8 -0
  34. package/dist/vendor/core/resource.js.map +1 -0
  35. package/dist/vendor/core/streaming.d.ts +41 -0
  36. package/dist/vendor/core/streaming.js +342 -0
  37. package/dist/vendor/core/streaming.js.map +1 -0
  38. package/dist/vendor/core/uploads.d.ts +2 -0
  39. package/dist/vendor/core/uploads.js +2 -0
  40. package/dist/vendor/core/uploads.js.map +1 -0
  41. package/dist/vendor/error.d.ts +2 -0
  42. package/dist/vendor/error.js +3 -0
  43. package/dist/vendor/error.js.map +1 -0
  44. package/dist/vendor/helpers/beta/environments.d.ts +22 -0
  45. package/dist/vendor/helpers/beta/environments.js +23 -0
  46. package/dist/vendor/helpers/beta/environments.js.map +1 -0
  47. package/dist/vendor/helpers/beta/json-schema.d.ts +36 -0
  48. package/dist/vendor/helpers/beta/json-schema.js +53 -0
  49. package/dist/vendor/helpers/beta/json-schema.js.map +1 -0
  50. package/dist/vendor/helpers/beta/mcp.d.ts +302 -0
  51. package/dist/vendor/helpers/beta/mcp.js +406 -0
  52. package/dist/vendor/helpers/beta/mcp.js.map +1 -0
  53. package/dist/vendor/helpers/beta/memory.d.ts +10 -0
  54. package/dist/vendor/helpers/beta/memory.js +15 -0
  55. package/dist/vendor/helpers/beta/memory.js.map +1 -0
  56. package/dist/vendor/helpers/beta/standard-schema.d.ts +44 -0
  57. package/dist/vendor/helpers/beta/standard-schema.js +54 -0
  58. package/dist/vendor/helpers/beta/standard-schema.js.map +1 -0
  59. package/dist/vendor/helpers/beta/zod.d.ts +32 -0
  60. package/dist/vendor/helpers/beta/zod.js +52 -0
  61. package/dist/vendor/helpers/beta/zod.js.map +1 -0
  62. package/dist/vendor/helpers/index.d.ts +2 -0
  63. package/dist/vendor/helpers/index.js +3 -0
  64. package/dist/vendor/helpers/index.js.map +1 -0
  65. package/dist/vendor/helpers/json-schema.d.ts +17 -0
  66. package/dist/vendor/helpers/json-schema.js +34 -0
  67. package/dist/vendor/helpers/json-schema.js.map +1 -0
  68. package/dist/vendor/helpers/zod.d.ts +12 -0
  69. package/dist/vendor/helpers/zod.js +42 -0
  70. package/dist/vendor/helpers/zod.js.map +1 -0
  71. package/dist/vendor/index.d.ts +16 -0
  72. package/dist/vendor/index.js +23 -0
  73. package/dist/vendor/index.js.map +1 -0
  74. package/dist/vendor/internal/builtin-types.d.ts +72 -0
  75. package/dist/vendor/internal/builtin-types.js +3 -0
  76. package/dist/vendor/internal/builtin-types.js.map +1 -0
  77. package/dist/vendor/internal/constants.d.ts +4 -0
  78. package/dist/vendor/internal/constants.js +10 -0
  79. package/dist/vendor/internal/constants.js.map +1 -0
  80. package/dist/vendor/internal/decoders/jsonl.d.ts +9 -0
  81. package/dist/vendor/internal/decoders/jsonl.js +37 -0
  82. package/dist/vendor/internal/decoders/jsonl.js.map +1 -0
  83. package/dist/vendor/internal/decoders/line.d.ts +16 -0
  84. package/dist/vendor/internal/decoders/line.js +105 -0
  85. package/dist/vendor/internal/decoders/line.js.map +1 -0
  86. package/dist/vendor/internal/detect-platform.d.ts +14 -0
  87. package/dist/vendor/internal/detect-platform.js +155 -0
  88. package/dist/vendor/internal/detect-platform.js.map +1 -0
  89. package/dist/vendor/internal/errors.d.ts +2 -0
  90. package/dist/vendor/internal/errors.js +38 -0
  91. package/dist/vendor/internal/errors.js.map +1 -0
  92. package/dist/vendor/internal/file-store.d.ts +135 -0
  93. package/dist/vendor/internal/file-store.js +529 -0
  94. package/dist/vendor/internal/file-store.js.map +1 -0
  95. package/dist/vendor/internal/headers.d.ts +28 -0
  96. package/dist/vendor/internal/headers.js +114 -0
  97. package/dist/vendor/internal/headers.js.map +1 -0
  98. package/dist/vendor/internal/message-stream-utils.d.ts +9 -0
  99. package/dist/vendor/internal/message-stream-utils.js +30 -0
  100. package/dist/vendor/internal/message-stream-utils.js.map +1 -0
  101. package/dist/vendor/internal/node.browser.d.ts +8 -0
  102. package/dist/vendor/internal/node.browser.js +19 -0
  103. package/dist/vendor/internal/node.browser.js.map +1 -0
  104. package/dist/vendor/internal/node.d.ts +13 -0
  105. package/dist/vendor/internal/node.js +14 -0
  106. package/dist/vendor/internal/node.js.map +1 -0
  107. package/dist/vendor/internal/parse.d.ts +17 -0
  108. package/dist/vendor/internal/parse.js +62 -0
  109. package/dist/vendor/internal/parse.js.map +1 -0
  110. package/dist/vendor/internal/qs/formats.d.ts +6 -0
  111. package/dist/vendor/internal/qs/formats.js +9 -0
  112. package/dist/vendor/internal/qs/formats.js.map +1 -0
  113. package/dist/vendor/internal/qs/index.d.ts +9 -0
  114. package/dist/vendor/internal/qs/index.js +10 -0
  115. package/dist/vendor/internal/qs/index.js.map +1 -0
  116. package/dist/vendor/internal/qs/stringify.d.ts +2 -0
  117. package/dist/vendor/internal/qs/stringify.js +274 -0
  118. package/dist/vendor/internal/qs/stringify.js.map +1 -0
  119. package/dist/vendor/internal/qs/types.d.ts +56 -0
  120. package/dist/vendor/internal/qs/types.js +2 -0
  121. package/dist/vendor/internal/qs/types.js.map +1 -0
  122. package/dist/vendor/internal/qs/utils.d.ts +14 -0
  123. package/dist/vendor/internal/qs/utils.js +217 -0
  124. package/dist/vendor/internal/qs/utils.js.map +1 -0
  125. package/dist/vendor/internal/request-options.d.ts +126 -0
  126. package/dist/vendor/internal/request-options.js +27 -0
  127. package/dist/vendor/internal/request-options.js.map +1 -0
  128. package/dist/vendor/internal/request-signal.d.ts +3 -0
  129. package/dist/vendor/internal/request-signal.js +45 -0
  130. package/dist/vendor/internal/request-signal.js.map +1 -0
  131. package/dist/vendor/internal/shim-types.d.ts +16 -0
  132. package/dist/vendor/internal/shim-types.js +3 -0
  133. package/dist/vendor/internal/shim-types.js.map +1 -0
  134. package/dist/vendor/internal/shims.d.ts +25 -0
  135. package/dist/vendor/internal/shims.js +85 -0
  136. package/dist/vendor/internal/shims.js.map +1 -0
  137. package/dist/vendor/internal/stainless-helper-header.d.ts +59 -0
  138. package/dist/vendor/internal/stainless-helper-header.js +83 -0
  139. package/dist/vendor/internal/stainless-helper-header.js.map +1 -0
  140. package/dist/vendor/internal/stream-utils.d.ts +7 -0
  141. package/dist/vendor/internal/stream-utils.js +35 -0
  142. package/dist/vendor/internal/stream-utils.js.map +1 -0
  143. package/dist/vendor/internal/to-file.d.ts +44 -0
  144. package/dist/vendor/internal/to-file.js +93 -0
  145. package/dist/vendor/internal/to-file.js.map +1 -0
  146. package/dist/vendor/internal/types.d.ts +62 -0
  147. package/dist/vendor/internal/types.js +3 -0
  148. package/dist/vendor/internal/types.js.map +1 -0
  149. package/dist/vendor/internal/uploads.d.ts +41 -0
  150. package/dist/vendor/internal/uploads.js +139 -0
  151. package/dist/vendor/internal/uploads.js.map +1 -0
  152. package/dist/vendor/internal/utils/abort.d.ts +11 -0
  153. package/dist/vendor/internal/utils/abort.js +22 -0
  154. package/dist/vendor/internal/utils/abort.js.map +1 -0
  155. package/dist/vendor/internal/utils/async-queue.d.ts +30 -0
  156. package/dist/vendor/internal/utils/async-queue.js +66 -0
  157. package/dist/vendor/internal/utils/async-queue.js.map +1 -0
  158. package/dist/vendor/internal/utils/backoff.d.ts +22 -0
  159. package/dist/vendor/internal/utils/backoff.js +36 -0
  160. package/dist/vendor/internal/utils/backoff.js.map +1 -0
  161. package/dist/vendor/internal/utils/base64.d.ts +2 -0
  162. package/dist/vendor/internal/utils/base64.js +33 -0
  163. package/dist/vendor/internal/utils/base64.js.map +1 -0
  164. package/dist/vendor/internal/utils/bytes.d.ts +3 -0
  165. package/dist/vendor/internal/utils/bytes.js +26 -0
  166. package/dist/vendor/internal/utils/bytes.js.map +1 -0
  167. package/dist/vendor/internal/utils/env.d.ts +8 -0
  168. package/dist/vendor/internal/utils/env.js +18 -0
  169. package/dist/vendor/internal/utils/env.js.map +1 -0
  170. package/dist/vendor/internal/utils/log.d.ts +46 -0
  171. package/dist/vendor/internal/utils/log.js +105 -0
  172. package/dist/vendor/internal/utils/log.js.map +1 -0
  173. package/dist/vendor/internal/utils/path.d.ts +14 -0
  174. package/dist/vendor/internal/utils/path.js +74 -0
  175. package/dist/vendor/internal/utils/path.js.map +1 -0
  176. package/dist/vendor/internal/utils/promise.d.ts +10 -0
  177. package/dist/vendor/internal/utils/promise.js +15 -0
  178. package/dist/vendor/internal/utils/promise.js.map +1 -0
  179. package/dist/vendor/internal/utils/query.d.ts +1 -0
  180. package/dist/vendor/internal/utils/query.js +6 -0
  181. package/dist/vendor/internal/utils/query.js.map +1 -0
  182. package/dist/vendor/internal/utils/sleep.d.ts +9 -0
  183. package/dist/vendor/internal/utils/sleep.js +24 -0
  184. package/dist/vendor/internal/utils/sleep.js.map +1 -0
  185. package/dist/vendor/internal/utils/time.d.ts +2 -0
  186. package/dist/vendor/internal/utils/time.js +5 -0
  187. package/dist/vendor/internal/utils/time.js.map +1 -0
  188. package/dist/vendor/internal/utils/uuid.d.ts +4 -0
  189. package/dist/vendor/internal/utils/uuid.js +15 -0
  190. package/dist/vendor/internal/utils/uuid.js.map +1 -0
  191. package/dist/vendor/internal/utils/values.d.ts +24 -0
  192. package/dist/vendor/internal/utils/values.js +106 -0
  193. package/dist/vendor/internal/utils/values.js.map +1 -0
  194. package/dist/vendor/internal/utils.d.ts +7 -0
  195. package/dist/vendor/internal/utils.js +9 -0
  196. package/dist/vendor/internal/utils.js.map +1 -0
  197. package/dist/vendor/lib/BetaMessageStream.d.ts +123 -0
  198. package/dist/vendor/lib/BetaMessageStream.js +662 -0
  199. package/dist/vendor/lib/BetaMessageStream.js.map +1 -0
  200. package/dist/vendor/lib/MessageStream.d.ts +122 -0
  201. package/dist/vendor/lib/MessageStream.js +609 -0
  202. package/dist/vendor/lib/MessageStream.js.map +1 -0
  203. package/dist/vendor/lib/beta-parser.d.ts +36 -0
  204. package/dist/vendor/lib/beta-parser.js +75 -0
  205. package/dist/vendor/lib/beta-parser.js.map +1 -0
  206. package/dist/vendor/lib/credentials/credential-chain.d.ts +38 -0
  207. package/dist/vendor/lib/credentials/credential-chain.js +208 -0
  208. package/dist/vendor/lib/credentials/credential-chain.js.map +1 -0
  209. package/dist/vendor/lib/credentials/identity-token.d.ts +10 -0
  210. package/dist/vendor/lib/credentials/identity-token.js +35 -0
  211. package/dist/vendor/lib/credentials/identity-token.js.map +1 -0
  212. package/dist/vendor/lib/credentials/oidc-federation.d.ts +39 -0
  213. package/dist/vendor/lib/credentials/oidc-federation.js +79 -0
  214. package/dist/vendor/lib/credentials/oidc-federation.js.map +1 -0
  215. package/dist/vendor/lib/credentials/token-cache.d.ts +53 -0
  216. package/dist/vendor/lib/credentials/token-cache.js +110 -0
  217. package/dist/vendor/lib/credentials/token-cache.js.map +1 -0
  218. package/dist/vendor/lib/credentials/types.d.ts +95 -0
  219. package/dist/vendor/lib/credentials/types.js +226 -0
  220. package/dist/vendor/lib/credentials/types.js.map +1 -0
  221. package/dist/vendor/lib/credentials/user-oauth.d.ts +20 -0
  222. package/dist/vendor/lib/credentials/user-oauth.js +94 -0
  223. package/dist/vendor/lib/credentials/user-oauth.js.map +1 -0
  224. package/dist/vendor/lib/credentials.d.ts +3 -0
  225. package/dist/vendor/lib/credentials.js +3 -0
  226. package/dist/vendor/lib/credentials.js.map +1 -0
  227. package/dist/vendor/lib/environments/index.d.ts +3 -0
  228. package/dist/vendor/lib/environments/index.js +4 -0
  229. package/dist/vendor/lib/environments/index.js.map +1 -0
  230. package/dist/vendor/lib/environments/poller.d.ts +94 -0
  231. package/dist/vendor/lib/environments/poller.js +218 -0
  232. package/dist/vendor/lib/environments/poller.js.map +1 -0
  233. package/dist/vendor/lib/environments/worker.d.ts +226 -0
  234. package/dist/vendor/lib/environments/worker.js +572 -0
  235. package/dist/vendor/lib/environments/worker.js.map +1 -0
  236. package/dist/vendor/lib/helper-client.d.ts +30 -0
  237. package/dist/vendor/lib/helper-client.js +55 -0
  238. package/dist/vendor/lib/helper-client.js.map +1 -0
  239. package/dist/vendor/lib/middleware.d.ts +97 -0
  240. package/dist/vendor/lib/middleware.js +775 -0
  241. package/dist/vendor/lib/middleware.js.map +1 -0
  242. package/dist/vendor/lib/parser.d.ts +31 -0
  243. package/dist/vendor/lib/parser.js +62 -0
  244. package/dist/vendor/lib/parser.js.map +1 -0
  245. package/dist/vendor/lib/sessions/accumulate.d.ts +22 -0
  246. package/dist/vendor/lib/sessions/accumulate.js +42 -0
  247. package/dist/vendor/lib/sessions/accumulate.js.map +1 -0
  248. package/dist/vendor/lib/standard-schema.d.ts +130 -0
  249. package/dist/vendor/lib/standard-schema.js +41 -0
  250. package/dist/vendor/lib/standard-schema.js.map +1 -0
  251. package/dist/vendor/lib/tools/BetaRunnableTool.d.ts +48 -0
  252. package/dist/vendor/lib/tools/BetaRunnableTool.js +26 -0
  253. package/dist/vendor/lib/tools/BetaRunnableTool.js.map +1 -0
  254. package/dist/vendor/lib/tools/BetaToolRunner.d.ts +160 -0
  255. package/dist/vendor/lib/tools/BetaToolRunner.js +497 -0
  256. package/dist/vendor/lib/tools/BetaToolRunner.js.map +1 -0
  257. package/dist/vendor/lib/tools/CompactionControl.d.ts +29 -0
  258. package/dist/vendor/lib/tools/CompactionControl.js +25 -0
  259. package/dist/vendor/lib/tools/CompactionControl.js.map +1 -0
  260. package/dist/vendor/lib/tools/SessionToolRunner.d.ts +178 -0
  261. package/dist/vendor/lib/tools/SessionToolRunner.js +737 -0
  262. package/dist/vendor/lib/tools/SessionToolRunner.js.map +1 -0
  263. package/dist/vendor/lib/tools/ToolError.d.ts +32 -0
  264. package/dist/vendor/lib/tools/ToolError.js +43 -0
  265. package/dist/vendor/lib/tools/ToolError.js.map +1 -0
  266. package/dist/vendor/lib/transform-json-schema.d.ts +2 -0
  267. package/dist/vendor/lib/transform-json-schema.js +111 -0
  268. package/dist/vendor/lib/transform-json-schema.js.map +1 -0
  269. package/dist/vendor/pagination.d.ts +2 -0
  270. package/dist/vendor/pagination.js +3 -0
  271. package/dist/vendor/pagination.js.map +1 -0
  272. package/dist/vendor/resource.d.ts +2 -0
  273. package/dist/vendor/resource.js +3 -0
  274. package/dist/vendor/resource.js.map +1 -0
  275. package/dist/vendor/resources/beta/agents/agents.d.ts +1128 -0
  276. package/dist/vendor/resources/beta/agents/agents.js +123 -0
  277. package/dist/vendor/resources/beta/agents/agents.js.map +1 -0
  278. package/dist/vendor/resources/beta/agents/index.d.ts +2 -0
  279. package/dist/vendor/resources/beta/agents/index.js +4 -0
  280. package/dist/vendor/resources/beta/agents/index.js.map +1 -0
  281. package/dist/vendor/resources/beta/agents/versions.d.ts +32 -0
  282. package/dist/vendor/resources/beta/agents/versions.js +32 -0
  283. package/dist/vendor/resources/beta/agents/versions.js.map +1 -0
  284. package/dist/vendor/resources/beta/agents.d.ts +1 -0
  285. package/dist/vendor/resources/beta/agents.js +3 -0
  286. package/dist/vendor/resources/beta/agents.js.map +1 -0
  287. package/dist/vendor/resources/beta/beta.d.ts +131 -0
  288. package/dist/vendor/resources/beta/beta.js +69 -0
  289. package/dist/vendor/resources/beta/beta.js.map +1 -0
  290. package/dist/vendor/resources/beta/deployment-runs.d.ts +305 -0
  291. package/dist/vendor/resources/beta/deployment-runs.js +51 -0
  292. package/dist/vendor/resources/beta/deployment-runs.js.map +1 -0
  293. package/dist/vendor/resources/beta/deployments.d.ts +698 -0
  294. package/dist/vendor/resources/beta/deployments.js +192 -0
  295. package/dist/vendor/resources/beta/deployments.js.map +1 -0
  296. package/dist/vendor/resources/beta/dreams.d.ts +335 -0
  297. package/dist/vendor/resources/beta/dreams.js +112 -0
  298. package/dist/vendor/resources/beta/dreams.js.map +1 -0
  299. package/dist/vendor/resources/beta/environments/environments.d.ts +432 -0
  300. package/dist/vendor/resources/beta/environments/environments.js +142 -0
  301. package/dist/vendor/resources/beta/environments/environments.js.map +1 -0
  302. package/dist/vendor/resources/beta/environments/index.d.ts +2 -0
  303. package/dist/vendor/resources/beta/environments/index.js +4 -0
  304. package/dist/vendor/resources/beta/environments/index.js.map +1 -0
  305. package/dist/vendor/resources/beta/environments/work.d.ts +500 -0
  306. package/dist/vendor/resources/beta/environments/work.js +251 -0
  307. package/dist/vendor/resources/beta/environments/work.js.map +1 -0
  308. package/dist/vendor/resources/beta/environments.d.ts +1 -0
  309. package/dist/vendor/resources/beta/environments.js +3 -0
  310. package/dist/vendor/resources/beta/environments.js.map +1 -0
  311. package/dist/vendor/resources/beta/files.d.ts +190 -0
  312. package/dist/vendor/resources/beta/files.js +120 -0
  313. package/dist/vendor/resources/beta/files.js.map +1 -0
  314. package/dist/vendor/resources/beta/index.d.ts +17 -0
  315. package/dist/vendor/resources/beta/index.js +19 -0
  316. package/dist/vendor/resources/beta/index.js.map +1 -0
  317. package/dist/vendor/resources/beta/memory-stores/index.d.ts +3 -0
  318. package/dist/vendor/resources/beta/memory-stores/index.js +5 -0
  319. package/dist/vendor/resources/beta/memory-stores/index.js.map +1 -0
  320. package/dist/vendor/resources/beta/memory-stores/memories.d.ts +346 -0
  321. package/dist/vendor/resources/beta/memory-stores/memories.js +126 -0
  322. package/dist/vendor/resources/beta/memory-stores/memories.js.map +1 -0
  323. package/dist/vendor/resources/beta/memory-stores/memory-stores.d.ts +230 -0
  324. package/dist/vendor/resources/beta/memory-stores/memory-stores.js +137 -0
  325. package/dist/vendor/resources/beta/memory-stores/memory-stores.js.map +1 -0
  326. package/dist/vendor/resources/beta/memory-stores/memory-versions.d.ts +252 -0
  327. package/dist/vendor/resources/beta/memory-stores/memory-versions.js +77 -0
  328. package/dist/vendor/resources/beta/memory-stores/memory-versions.js.map +1 -0
  329. package/dist/vendor/resources/beta/memory-stores.d.ts +1 -0
  330. package/dist/vendor/resources/beta/memory-stores.js +3 -0
  331. package/dist/vendor/resources/beta/memory-stores.js.map +1 -0
  332. package/dist/vendor/resources/beta/messages/batches.d.ts +685 -0
  333. package/dist/vendor/resources/beta/messages/batches.js +203 -0
  334. package/dist/vendor/resources/beta/messages/batches.js.map +1 -0
  335. package/dist/vendor/resources/beta/messages/index.d.ts +3 -0
  336. package/dist/vendor/resources/beta/messages/index.js +5 -0
  337. package/dist/vendor/resources/beta/messages/index.js.map +1 -0
  338. package/dist/vendor/resources/beta/messages/messages.d.ts +5376 -0
  339. package/dist/vendor/resources/beta/messages/messages.js +158 -0
  340. package/dist/vendor/resources/beta/messages/messages.js.map +1 -0
  341. package/dist/vendor/resources/beta/messages.d.ts +1 -0
  342. package/dist/vendor/resources/beta/messages.js +3 -0
  343. package/dist/vendor/resources/beta/messages.js.map +1 -0
  344. package/dist/vendor/resources/beta/models.d.ts +217 -0
  345. package/dist/vendor/resources/beta/models.js +56 -0
  346. package/dist/vendor/resources/beta/models.js.map +1 -0
  347. package/dist/vendor/resources/beta/organization/api-keys.d.ts +176 -0
  348. package/dist/vendor/resources/beta/organization/api-keys.js +52 -0
  349. package/dist/vendor/resources/beta/organization/api-keys.js.map +1 -0
  350. package/dist/vendor/resources/beta/organization/compliance-settings.d.ts +73 -0
  351. package/dist/vendor/resources/beta/organization/compliance-settings.js +48 -0
  352. package/dist/vendor/resources/beta/organization/compliance-settings.js.map +1 -0
  353. package/dist/vendor/resources/beta/organization/external-keys.d.ts +271 -0
  354. package/dist/vendor/resources/beta/organization/external-keys.js +115 -0
  355. package/dist/vendor/resources/beta/organization/external-keys.js.map +1 -0
  356. package/dist/vendor/resources/beta/organization/federation/federation.d.ts +13 -0
  357. package/dist/vendor/resources/beta/organization/federation/federation.js +13 -0
  358. package/dist/vendor/resources/beta/organization/federation/federation.js.map +1 -0
  359. package/dist/vendor/resources/beta/organization/federation/index.d.ts +3 -0
  360. package/dist/vendor/resources/beta/organization/federation/index.js +5 -0
  361. package/dist/vendor/resources/beta/organization/federation/index.js.map +1 -0
  362. package/dist/vendor/resources/beta/organization/federation/issuers.d.ts +352 -0
  363. package/dist/vendor/resources/beta/organization/federation/issuers.js +165 -0
  364. package/dist/vendor/resources/beta/organization/federation/issuers.js.map +1 -0
  365. package/dist/vendor/resources/beta/organization/federation/rules/index.d.ts +2 -0
  366. package/dist/vendor/resources/beta/organization/federation/rules/index.js +4 -0
  367. package/dist/vendor/resources/beta/organization/federation/rules/index.js.map +1 -0
  368. package/dist/vendor/resources/beta/organization/federation/rules/rules.d.ts +467 -0
  369. package/dist/vendor/resources/beta/organization/federation/rules/rules.js +190 -0
  370. package/dist/vendor/resources/beta/organization/federation/rules/rules.js.map +1 -0
  371. package/dist/vendor/resources/beta/organization/federation/rules/workspaces.d.ts +123 -0
  372. package/dist/vendor/resources/beta/organization/federation/rules/workspaces.js +110 -0
  373. package/dist/vendor/resources/beta/organization/federation/rules/workspaces.js.map +1 -0
  374. package/dist/vendor/resources/beta/organization/federation/rules.d.ts +1 -0
  375. package/dist/vendor/resources/beta/organization/federation/rules.js +3 -0
  376. package/dist/vendor/resources/beta/organization/federation/rules.js.map +1 -0
  377. package/dist/vendor/resources/beta/organization/federation.d.ts +1 -0
  378. package/dist/vendor/resources/beta/organization/federation.js +3 -0
  379. package/dist/vendor/resources/beta/organization/federation.js.map +1 -0
  380. package/dist/vendor/resources/beta/organization/index.d.ts +10 -0
  381. package/dist/vendor/resources/beta/organization/index.js +12 -0
  382. package/dist/vendor/resources/beta/organization/index.js.map +1 -0
  383. package/dist/vendor/resources/beta/organization/invites.d.ts +161 -0
  384. package/dist/vendor/resources/beta/organization/invites.js +72 -0
  385. package/dist/vendor/resources/beta/organization/invites.js.map +1 -0
  386. package/dist/vendor/resources/beta/organization/organization.d.ts +72 -0
  387. package/dist/vendor/resources/beta/organization/organization.js +54 -0
  388. package/dist/vendor/resources/beta/organization/organization.js.map +1 -0
  389. package/dist/vendor/resources/beta/organization/rate-limits.d.ts +76 -0
  390. package/dist/vendor/resources/beta/organization/rate-limits.js +27 -0
  391. package/dist/vendor/resources/beta/organization/rate-limits.js.map +1 -0
  392. package/dist/vendor/resources/beta/organization/service-accounts/index.d.ts +2 -0
  393. package/dist/vendor/resources/beta/organization/service-accounts/index.js +4 -0
  394. package/dist/vendor/resources/beta/organization/service-accounts/index.js.map +1 -0
  395. package/dist/vendor/resources/beta/organization/service-accounts/service-accounts.d.ts +257 -0
  396. package/dist/vendor/resources/beta/organization/service-accounts/service-accounts.js +166 -0
  397. package/dist/vendor/resources/beta/organization/service-accounts/service-accounts.js.map +1 -0
  398. package/dist/vendor/resources/beta/organization/service-accounts/workspaces.d.ts +140 -0
  399. package/dist/vendor/resources/beta/organization/service-accounts/workspaces.js +121 -0
  400. package/dist/vendor/resources/beta/organization/service-accounts/workspaces.js.map +1 -0
  401. package/dist/vendor/resources/beta/organization/service-accounts.d.ts +1 -0
  402. package/dist/vendor/resources/beta/organization/service-accounts.js +3 -0
  403. package/dist/vendor/resources/beta/organization/service-accounts.js.map +1 -0
  404. package/dist/vendor/resources/beta/organization/users.d.ts +123 -0
  405. package/dist/vendor/resources/beta/organization/users.js +63 -0
  406. package/dist/vendor/resources/beta/organization/users.js.map +1 -0
  407. package/dist/vendor/resources/beta/organization/workspaces/index.d.ts +4 -0
  408. package/dist/vendor/resources/beta/organization/workspaces/index.js +6 -0
  409. package/dist/vendor/resources/beta/organization/workspaces/index.js.map +1 -0
  410. package/dist/vendor/resources/beta/organization/workspaces/members.d.ts +134 -0
  411. package/dist/vendor/resources/beta/organization/workspaces/members.js +98 -0
  412. package/dist/vendor/resources/beta/organization/workspaces/members.js.map +1 -0
  413. package/dist/vendor/resources/beta/organization/workspaces/rate-limits.d.ts +82 -0
  414. package/dist/vendor/resources/beta/organization/workspaces/rate-limits.js +30 -0
  415. package/dist/vendor/resources/beta/organization/workspaces/rate-limits.js.map +1 -0
  416. package/dist/vendor/resources/beta/organization/workspaces/service-accounts.d.ts +209 -0
  417. package/dist/vendor/resources/beta/organization/workspaces/service-accounts.js +185 -0
  418. package/dist/vendor/resources/beta/organization/workspaces/service-accounts.js.map +1 -0
  419. package/dist/vendor/resources/beta/organization/workspaces/workspaces.d.ts +302 -0
  420. package/dist/vendor/resources/beta/organization/workspaces/workspaces.js +104 -0
  421. package/dist/vendor/resources/beta/organization/workspaces/workspaces.js.map +1 -0
  422. package/dist/vendor/resources/beta/organization/workspaces.d.ts +1 -0
  423. package/dist/vendor/resources/beta/organization/workspaces.js +3 -0
  424. package/dist/vendor/resources/beta/organization/workspaces.js.map +1 -0
  425. package/dist/vendor/resources/beta/organization.d.ts +1 -0
  426. package/dist/vendor/resources/beta/organization.js +3 -0
  427. package/dist/vendor/resources/beta/organization.js.map +1 -0
  428. package/dist/vendor/resources/beta/sessions/events.d.ts +1513 -0
  429. package/dist/vendor/resources/beta/sessions/events.js +115 -0
  430. package/dist/vendor/resources/beta/sessions/events.js.map +1 -0
  431. package/dist/vendor/resources/beta/sessions/index.d.ts +4 -0
  432. package/dist/vendor/resources/beta/sessions/index.js +6 -0
  433. package/dist/vendor/resources/beta/sessions/index.js.map +1 -0
  434. package/dist/vendor/resources/beta/sessions/resources.d.ts +227 -0
  435. package/dist/vendor/resources/beta/sessions/resources.js +128 -0
  436. package/dist/vendor/resources/beta/sessions/resources.js.map +1 -0
  437. package/dist/vendor/resources/beta/sessions/sessions.d.ts +866 -0
  438. package/dist/vendor/resources/beta/sessions/sessions.js +150 -0
  439. package/dist/vendor/resources/beta/sessions/sessions.js.map +1 -0
  440. package/dist/vendor/resources/beta/sessions/threads/events.d.ts +77 -0
  441. package/dist/vendor/resources/beta/sessions/threads/events.js +57 -0
  442. package/dist/vendor/resources/beta/sessions/threads/events.js.map +1 -0
  443. package/dist/vendor/resources/beta/sessions/threads/index.d.ts +2 -0
  444. package/dist/vendor/resources/beta/sessions/threads/index.js +4 -0
  445. package/dist/vendor/resources/beta/sessions/threads/index.js.map +1 -0
  446. package/dist/vendor/resources/beta/sessions/threads/threads.d.ts +195 -0
  447. package/dist/vendor/resources/beta/sessions/threads/threads.js +80 -0
  448. package/dist/vendor/resources/beta/sessions/threads/threads.js.map +1 -0
  449. package/dist/vendor/resources/beta/sessions/threads.d.ts +1 -0
  450. package/dist/vendor/resources/beta/sessions/threads.js +3 -0
  451. package/dist/vendor/resources/beta/sessions/threads.js.map +1 -0
  452. package/dist/vendor/resources/beta/sessions.d.ts +1 -0
  453. package/dist/vendor/resources/beta/sessions.js +3 -0
  454. package/dist/vendor/resources/beta/sessions.js.map +1 -0
  455. package/dist/vendor/resources/beta/skills/index.d.ts +2 -0
  456. package/dist/vendor/resources/beta/skills/index.js +4 -0
  457. package/dist/vendor/resources/beta/skills/index.js.map +1 -0
  458. package/dist/vendor/resources/beta/skills/skills.d.ts +178 -0
  459. package/dist/vendor/resources/beta/skills/skills.js +96 -0
  460. package/dist/vendor/resources/beta/skills/skills.js.map +1 -0
  461. package/dist/vendor/resources/beta/skills/versions.d.ts +181 -0
  462. package/dist/vendor/resources/beta/skills/versions.js +125 -0
  463. package/dist/vendor/resources/beta/skills/versions.js.map +1 -0
  464. package/dist/vendor/resources/beta/skills.d.ts +1 -0
  465. package/dist/vendor/resources/beta/skills.js +3 -0
  466. package/dist/vendor/resources/beta/skills.js.map +1 -0
  467. package/dist/vendor/resources/beta/tunnels/certificates.d.ts +162 -0
  468. package/dist/vendor/resources/beta/tunnels/certificates.js +125 -0
  469. package/dist/vendor/resources/beta/tunnels/certificates.js.map +1 -0
  470. package/dist/vendor/resources/beta/tunnels/index.d.ts +2 -0
  471. package/dist/vendor/resources/beta/tunnels/index.js +4 -0
  472. package/dist/vendor/resources/beta/tunnels/index.js.map +1 -0
  473. package/dist/vendor/resources/beta/tunnels/tunnels.d.ts +211 -0
  474. package/dist/vendor/resources/beta/tunnels/tunnels.js +173 -0
  475. package/dist/vendor/resources/beta/tunnels/tunnels.js.map +1 -0
  476. package/dist/vendor/resources/beta/tunnels.d.ts +1 -0
  477. package/dist/vendor/resources/beta/tunnels.js +3 -0
  478. package/dist/vendor/resources/beta/tunnels.js.map +1 -0
  479. package/dist/vendor/resources/beta/user-profiles.d.ts +244 -0
  480. package/dist/vendor/resources/beta/user-profiles.js +114 -0
  481. package/dist/vendor/resources/beta/user-profiles.js.map +1 -0
  482. package/dist/vendor/resources/beta/vaults/credentials.d.ts +715 -0
  483. package/dist/vendor/resources/beta/vaults/credentials.js +173 -0
  484. package/dist/vendor/resources/beta/vaults/credentials.js.map +1 -0
  485. package/dist/vendor/resources/beta/vaults/index.d.ts +2 -0
  486. package/dist/vendor/resources/beta/vaults/index.js +4 -0
  487. package/dist/vendor/resources/beta/vaults/index.js.map +1 -0
  488. package/dist/vendor/resources/beta/vaults/vaults.d.ts +191 -0
  489. package/dist/vendor/resources/beta/vaults/vaults.js +141 -0
  490. package/dist/vendor/resources/beta/vaults/vaults.js.map +1 -0
  491. package/dist/vendor/resources/beta/vaults.d.ts +1 -0
  492. package/dist/vendor/resources/beta/vaults.js +3 -0
  493. package/dist/vendor/resources/beta/vaults.js.map +1 -0
  494. package/dist/vendor/resources/beta/webhooks.d.ts +464 -0
  495. package/dist/vendor/resources/beta/webhooks.js +29 -0
  496. package/dist/vendor/resources/beta/webhooks.js.map +1 -0
  497. package/dist/vendor/resources/beta.d.ts +1 -0
  498. package/dist/vendor/resources/beta.js +3 -0
  499. package/dist/vendor/resources/beta.js.map +1 -0
  500. package/dist/vendor/resources/completions.d.ts +172 -0
  501. package/dist/vendor/resources/completions.js +19 -0
  502. package/dist/vendor/resources/completions.js.map +1 -0
  503. package/dist/vendor/resources/files.d.ts +104 -0
  504. package/dist/vendor/resources/files.js +48 -0
  505. package/dist/vendor/resources/files.js.map +1 -0
  506. package/dist/vendor/resources/index.d.ts +7 -0
  507. package/dist/vendor/resources/index.js +9 -0
  508. package/dist/vendor/resources/index.js.map +1 -0
  509. package/dist/vendor/resources/messages/batches.d.ts +312 -0
  510. package/dist/vendor/resources/messages/batches.js +157 -0
  511. package/dist/vendor/resources/messages/batches.js.map +1 -0
  512. package/dist/vendor/resources/messages/index.d.ts +2 -0
  513. package/dist/vendor/resources/messages/index.js +4 -0
  514. package/dist/vendor/resources/messages/index.js.map +1 -0
  515. package/dist/vendor/resources/messages/messages.d.ts +3886 -0
  516. package/dist/vendor/resources/messages/messages.js +130 -0
  517. package/dist/vendor/resources/messages/messages.js.map +1 -0
  518. package/dist/vendor/resources/messages.d.ts +1 -0
  519. package/dist/vendor/resources/messages.js +3 -0
  520. package/dist/vendor/resources/messages.js.map +1 -0
  521. package/dist/vendor/resources/models.d.ts +196 -0
  522. package/dist/vendor/resources/models.js +41 -0
  523. package/dist/vendor/resources/models.js.map +1 -0
  524. package/dist/vendor/resources/shared.d.ts +43 -0
  525. package/dist/vendor/resources/shared.js +3 -0
  526. package/dist/vendor/resources/shared.js.map +1 -0
  527. package/dist/vendor/resources/skills/index.d.ts +2 -0
  528. package/dist/vendor/resources/skills/index.js +4 -0
  529. package/dist/vendor/resources/skills/index.js.map +1 -0
  530. package/dist/vendor/resources/skills/skills.d.ts +128 -0
  531. package/dist/vendor/resources/skills/skills.js +36 -0
  532. package/dist/vendor/resources/skills/skills.js.map +1 -0
  533. package/dist/vendor/resources/skills/versions.d.ts +103 -0
  534. package/dist/vendor/resources/skills/versions.js +37 -0
  535. package/dist/vendor/resources/skills/versions.js.map +1 -0
  536. package/dist/vendor/resources/skills.d.ts +1 -0
  537. package/dist/vendor/resources/skills.js +3 -0
  538. package/dist/vendor/resources/skills.js.map +1 -0
  539. package/dist/vendor/resources/top-level.d.ts +1 -0
  540. package/dist/vendor/resources/top-level.js +3 -0
  541. package/dist/vendor/resources/top-level.js.map +1 -0
  542. package/dist/vendor/resources.d.ts +1 -0
  543. package/dist/vendor/resources.js +2 -0
  544. package/dist/vendor/resources.js.map +1 -0
  545. package/dist/vendor/streaming.d.ts +2 -0
  546. package/dist/vendor/streaming.js +3 -0
  547. package/dist/vendor/streaming.js.map +1 -0
  548. package/dist/vendor/tools/agent-toolset/fs-util.d.ts +74 -0
  549. package/dist/vendor/tools/agent-toolset/fs-util.js +184 -0
  550. package/dist/vendor/tools/agent-toolset/fs-util.js.map +1 -0
  551. package/dist/vendor/tools/agent-toolset/memories.d.ts +172 -0
  552. package/dist/vendor/tools/agent-toolset/memories.js +910 -0
  553. package/dist/vendor/tools/agent-toolset/memories.js.map +1 -0
  554. package/dist/vendor/tools/agent-toolset/node.browser.d.ts +80 -0
  555. package/dist/vendor/tools/agent-toolset/node.browser.js +129 -0
  556. package/dist/vendor/tools/agent-toolset/node.browser.js.map +1 -0
  557. package/dist/vendor/tools/agent-toolset/node.d.ts +191 -0
  558. package/dist/vendor/tools/agent-toolset/node.js +886 -0
  559. package/dist/vendor/tools/agent-toolset/node.js.map +1 -0
  560. package/dist/vendor/tools/agent-toolset/skills.d.ts +73 -0
  561. package/dist/vendor/tools/agent-toolset/skills.js +320 -0
  562. package/dist/vendor/tools/agent-toolset/skills.js.map +1 -0
  563. package/dist/vendor/tools/agent-toolset/sync-interval.d.ts +18 -0
  564. package/dist/vendor/tools/agent-toolset/sync-interval.js +25 -0
  565. package/dist/vendor/tools/agent-toolset/sync-interval.js.map +1 -0
  566. package/dist/vendor/tools/memory/node.browser.d.ts +26 -0
  567. package/dist/vendor/tools/memory/node.browser.js +44 -0
  568. package/dist/vendor/tools/memory/node.browser.js.map +1 -0
  569. package/dist/vendor/tools/memory/node.d.ts +17 -0
  570. package/dist/vendor/tools/memory/node.js +327 -0
  571. package/dist/vendor/tools/memory/node.js.map +1 -0
  572. package/dist/vendor/uploads.d.ts +2 -0
  573. package/dist/vendor/uploads.js +3 -0
  574. package/dist/vendor/uploads.js.map +1 -0
  575. package/dist/vendor/version.d.ts +1 -0
  576. package/dist/vendor/version.js +2 -0
  577. package/dist/vendor/version.js.map +1 -0
  578. package/package.json +7 -14
  579. package/sdk.cjs +6 -6
  580. package/sdk.cjs.map +18 -18
  581. package/sdk.d.ts +10 -10
  582. package/sdk.mjs +21 -21
  583. package/sdk.mjs.map +18 -18
@@ -0,0 +1,910 @@
1
+ /**
2
+ * Session-level memory-store download and sync.
3
+ *
4
+ * A session may have several memory stores attached. This module resolves
5
+ * where each store's folder goes on disk, opens a {@link LocalFileStore}
6
+ * there, and reconciles each folder with its remote store — the merge rules
7
+ * live on {@link SessionMemoryStores}.
8
+ *
9
+ * Node-only (it sits on the filesystem-backed FileStore); like `skills.ts`,
10
+ * it is reachable through the shimmed `node.ts` entry point.
11
+ */
12
+ import { crypto, path } from '../../internal/node.js';
13
+ import { PukuError } from '../../core/error.js';
14
+ import { loggerFor } from '../../internal/utils/log.js';
15
+ import { decodeUTF8 } from '../../internal/utils/bytes.js';
16
+ import { isStatus } from '../../internal/utils/backoff.js';
17
+ import { isPathLegal, FileStoreError, LocalFileStore } from '../../internal/file-store.js';
18
+ import { DEFAULT_MEMORY_SYNC_INTERVAL_MS, checkMemorySyncInterval } from './sync-interval.js';
19
+ export { DEFAULT_MEMORY_SYNC_INTERVAL_MS, MIN_MEMORY_SYNC_INTERVAL_MS } from './sync-interval.js';
20
+ /**
21
+ * Time bound the worker puts on each teardown pass — the final
22
+ * {@link SessionMemoryStores.finish}, then {@link SessionMemoryStores.flushWrites} —
23
+ * so a slow server cannot stall teardown.
24
+ */
25
+ export const MEMORY_FLUSH_TIMEOUT_MS = 30_000;
26
+ /**
27
+ * Marker file stamped into every store folder; a sync trusts the folder only
28
+ * when it matches. Never itself syncs.
29
+ */
30
+ export const MARKER_PATH = '.puku-memory-store';
31
+ const MARKER_VERSION = 1;
32
+ function markerSha(memoryStoreId) {
33
+ return crypto
34
+ .createHash('sha256')
35
+ .update(`version ${MARKER_VERSION}\n${memoryStoreId}`, 'utf-8')
36
+ .digest('hex');
37
+ }
38
+ /** How long a file must stay missing locally before its server delete goes out. */
39
+ export const DELETE_CORROBORATION_MS = 30_000;
40
+ /**
41
+ * Page sizes for memory listings — the API's maximum per view: `basic` pages
42
+ * carry up to 100 items, `full` pages are capped by the server.
43
+ */
44
+ const LIST_PAGE_SIZE = 100;
45
+ const FULL_LIST_PAGE_SIZE = 20;
46
+ /**
47
+ * How many single-memory content fetches may be in flight at once during one
48
+ * store's pull pass. A sync rarely pulls more than a handful of memories, so
49
+ * a higher cap buys nothing in the common case.
50
+ */
51
+ const FETCH_CONCURRENCY = 16;
52
+ /**
53
+ * How many uploads one store's flush keeps in flight. At ~0.3s per upload,
54
+ * 32 clears the server's 2000-memories-per-store cap inside
55
+ * {@link MEMORY_FLUSH_TIMEOUT_MS}.
56
+ */
57
+ export const UPLOAD_CONCURRENCY = 32;
58
+ /**
59
+ * Per-sync remote-delete cap bounds. The floor lets a small store's
60
+ * deletes clear in one pass; the ceiling caps damage on large ones.
61
+ */
62
+ const DELETE_CAP_FLOOR = 8;
63
+ const DELETE_CAP_CEILING = 50;
64
+ /**
65
+ * A session's memory stores could not be mounted.
66
+ *
67
+ * Thrown by {@link SessionMemoryStores.download} when a store cannot be
68
+ * materialised on disk, and by the environment worker when a work item for a
69
+ * session that has memory stores carried no sessions token to reach them with.
70
+ */
71
+ export class SessionMemoryError extends PukuError {
72
+ constructor(message, cause) {
73
+ super(message);
74
+ this.name = 'SessionMemoryError';
75
+ // in some environments the 'cause' property is already declared
76
+ // @ts-ignore
77
+ if (cause !== undefined)
78
+ this.cause = cause;
79
+ }
80
+ }
81
+ /** One sync's remote-delete gate and counters. */
82
+ class DeletePass {
83
+ mode;
84
+ cap;
85
+ waiveWindow;
86
+ attempted = 0;
87
+ capped = 0;
88
+ suppressed = 0;
89
+ constructor(mode, cap,
90
+ /** Skip the delete wait — set on the session's last sync. */
91
+ waiveWindow) {
92
+ this.mode = mode;
93
+ this.cap = cap;
94
+ this.waiveWindow = waiveWindow;
95
+ }
96
+ takeSlot() {
97
+ if (this.attempted >= this.cap) {
98
+ this.capped++;
99
+ return false;
100
+ }
101
+ this.attempted++;
102
+ return true;
103
+ }
104
+ }
105
+ /**
106
+ * The memory stores attached to one session, materialised on disk.
107
+ *
108
+ * {@link SessionMemoryStores.download} opens a {@link LocalFileStore} at each
109
+ * attached store's directory (its `mount_path`, or a workdir fallback — see
110
+ * {@link SessionMemoryStores.download}), pulls its memories, and records each
111
+ * one's `content_sha256` as the sync baseline. Each sync
112
+ * ({@link SessionMemoryStores.syncIfDue} on the worker's cadence,
113
+ * {@link SessionMemoryStores.finish} once at the end) reconciles disk against
114
+ * server, per store and per path:
115
+ *
116
+ * - a memory changed only remotely is written to disk;
117
+ * - a file changed only locally is uploaded — an update with a
118
+ * `content_sha256` precondition, or a create for a new file;
119
+ * - a file changed on both sides logs a warning and takes the server version;
120
+ * - a file the server refuses (too large, invalid content) is skipped —
121
+ * warned once and retried only after the file changes; other files keep
122
+ * syncing;
123
+ * - a file deleted locally is deleted on the server after a delay and a
124
+ * re-check — never on the first sync that notices, and only up to a
125
+ * per-sync cap. `syncDeletions` gates it;
126
+ * - a memory deleted on the server is deleted on disk — unless the local
127
+ * file holds un-pushed edits: a writable store re-creates the memory
128
+ * from the file, a read-only one keeps the file unsynced;
129
+ * - a store attached read-only pulls but never pushes.
130
+ *
131
+ * A download pulls the whole store, so it lists with content included. The
132
+ * recurring syncs instead run two phases: a content-free listing (paths and
133
+ * shas) drives the merge decisions, then only the memories actually being
134
+ * written to disk are fetched, a bounded number at a time. A sync that finds
135
+ * nothing changed moves no content at all.
136
+ *
137
+ * A file whose write to disk failed is never in the baseline, so its absence
138
+ * reads as a failed download — it is pulled again, never deleted. A write
139
+ * never re-creates a store folder that vanished mid-sync: it fails, and the
140
+ * next sync's scan finds whatever is at the path by then — nothing
141
+ * (re-downloaded) or someone else's files (left alone) — under the rules
142
+ * below.
143
+ *
144
+ * A store folder that loses its {@link MARKER_PATH} marker, is emptied,
145
+ * or vanishes is re-downloaded rather than treated as a mass local
146
+ * delete; a folder whose marker names another store is left as found —
147
+ * nothing pushed, nothing deleted.
148
+ *
149
+ * Two things about the store's directory make
150
+ * {@link SessionMemoryStores.download} refuse the session outright, with
151
+ * {@link SessionMemoryError}: a `mount_path` that is not a clean absolute
152
+ * path, and a directory already sitting at that path.
153
+ *
154
+ * {@link SessionMemoryStores.download} throws on the first store it cannot
155
+ * materialise. The syncs never throw: mid-session, one bad store or one bad
156
+ * file is logged and the rest continue. Instances are not safe for concurrent
157
+ * use. The worker builds one on its token-scoped sub-client (the memory
158
+ * endpoints reject the environment key): `syncIfDue` after each tool call,
159
+ * `finish` once at a clean end, a bounded {@link SessionMemoryStores.flushWrites}
160
+ * in every teardown, `dispose` last.
161
+ */
162
+ export class SessionMemoryStores {
163
+ #client;
164
+ #workdir;
165
+ #syncIntervalMs;
166
+ #syncDeletions;
167
+ #log;
168
+ #lastSyncAt;
169
+ #finished = false;
170
+ #stores = [];
171
+ constructor(client, opts) {
172
+ this.#client = client;
173
+ this.#workdir = opts.workdir;
174
+ this.#syncIntervalMs = opts.syncIntervalMs ?? DEFAULT_MEMORY_SYNC_INTERVAL_MS;
175
+ checkMemorySyncInterval(this.#syncIntervalMs, 'syncIntervalMs');
176
+ this.#syncDeletions = opts.syncDeletions ?? 'enabled';
177
+ this.#log = loggerFor(client);
178
+ this.#lastSyncAt = Date.now();
179
+ }
180
+ /**
181
+ * Every attached store's root directory.
182
+ *
183
+ * The worker lists these as the file tools' allowed roots so a store
184
+ * mounted outside the workdir stays reachable.
185
+ */
186
+ get roots() {
187
+ return this.#stores.map((s) => s.files.root().path);
188
+ }
189
+ /**
190
+ * Root directories of stores attached read-only.
191
+ *
192
+ * The file tools consult this to refuse writes into read-only stores.
193
+ */
194
+ get readOnlyRoots() {
195
+ return this.#stores.filter((s) => s.readOnly).map((s) => s.files.root().path);
196
+ }
197
+ /**
198
+ * Where one store's files land on disk.
199
+ *
200
+ * The store's files land at its `mount_path` — the very location the agent's
201
+ * system prompt tells it to read. A `mount_path` we cannot use verbatim is
202
+ * refused rather than quietly relocated: the agent would read an empty
203
+ * folder at the path it was told about, and write notes somewhere the next
204
+ * session looks for nothing.
205
+ */
206
+ #storeRoot(resource) {
207
+ if (resource.mount_path) {
208
+ if (!isPathLegal(resource.mount_path)) {
209
+ throw new SessionMemoryError(`memory store mount_path is not a clean absolute path: ${JSON.stringify(resource.mount_path)} ` +
210
+ `(memory_store_id=${resource.memory_store_id})`);
211
+ }
212
+ return resource.mount_path;
213
+ }
214
+ // No mount_path at all: nothing points the agent anywhere, so the workdir
215
+ // is as good a home as any.
216
+ return path.join(this.#workdir, 'memory', resource.name || resource.memory_store_id);
217
+ }
218
+ /**
219
+ * Download every attached store's memories to disk.
220
+ *
221
+ * `session` arrives already fetched — one snapshot shared with the skills
222
+ * download, so the two cannot disagree about the resources.
223
+ */
224
+ async download(session) {
225
+ for (const resource of session.resources) {
226
+ if (resource.type !== 'memory_store')
227
+ continue;
228
+ const root = this.#storeRoot(resource);
229
+ let store;
230
+ try {
231
+ store = {
232
+ memoryStoreId: resource.memory_store_id,
233
+ // utf8: a binary file is refused at put/get, not mid-sync.
234
+ files: await LocalFileStore.open(root, { utf8: true }),
235
+ readOnly: resource.access === 'read_only',
236
+ baseline: new Map(),
237
+ refusedShas: new Map(),
238
+ pendingDeletes: new Map(),
239
+ };
240
+ // A root `open` did not create is a dead run's leftovers; the first
241
+ // sync would upload them into the customer's store.
242
+ if (!store.files.root().removedOnDispose) {
243
+ // The configured path, not root().path — that one is resolved and
244
+ // would name a symlinked mount's target.
245
+ throw new SessionMemoryError(`something already exists at the memory store's path: ${root} ` +
246
+ `(memory_store_id=${resource.memory_store_id}); ` +
247
+ 'it must not exist when the session starts');
248
+ }
249
+ try {
250
+ await store.files.createRoot();
251
+ }
252
+ catch (e) {
253
+ if (!isErrno(e))
254
+ throw e;
255
+ // An unmountable root fails the item, not just one file.
256
+ throw new SessionMemoryError(`cannot create the memory store's folder: ${root} ` +
257
+ `(memory_store_id=${resource.memory_store_id}): ${e}; ` +
258
+ 'the worker host must make this mount path writable', e);
259
+ }
260
+ await this.#stampAndPull(store);
261
+ this.#log.info('downloaded memories', {
262
+ count: store.baseline.size,
263
+ memory_store_id: store.memoryStoreId,
264
+ dest: store.files.root().path,
265
+ });
266
+ this.#stores.push(store);
267
+ }
268
+ catch (e) {
269
+ // A half-downloaded folder self-destructs; `dispose` leaves a refused
270
+ // pre-existing directory exactly as found.
271
+ if (store)
272
+ await store.files.dispose().catch(() => { });
273
+ // Every store must land: a session missing a folder its system prompt
274
+ // names runs with amnesia and syncs nothing back.
275
+ if (e instanceof SessionMemoryError)
276
+ throw e;
277
+ throw new SessionMemoryError(`failed to download memory store memory_store_id=${resource.memory_store_id}: ${e}`, e);
278
+ }
279
+ }
280
+ this.#lastSyncAt = Date.now();
281
+ }
282
+ /**
283
+ * The session's last sync — skips the delete wait, so calling it twice
284
+ * would undo the protection; it throws instead.
285
+ */
286
+ async finish() {
287
+ if (this.#finished) {
288
+ throw new PukuError("finish() was already called: it is the session's last sync and runs once");
289
+ }
290
+ this.#finished = true;
291
+ await this.syncAll(true);
292
+ }
293
+ /** @internal — reconcile every store once; the tests' deterministic driver */
294
+ async syncAll(final) {
295
+ await Promise.all(this.#stores.map((store) => this.#syncStore(store, final)));
296
+ this.#lastSyncAt = Date.now();
297
+ }
298
+ async #scanMarker(store) {
299
+ const local = await store.files.hashtree();
300
+ const marker = local[MARKER_PATH];
301
+ delete local[MARKER_PATH];
302
+ if (marker === markerSha(store.memoryStoreId)) {
303
+ return { files: local, markerOk: true, distrustReason: null };
304
+ }
305
+ return {
306
+ files: local,
307
+ markerOk: false,
308
+ distrustReason: marker !== undefined ? 'the marker file does not match this store' : 'the marker file is gone',
309
+ };
310
+ }
311
+ async #syncStore(store, final) {
312
+ try {
313
+ const scan = await this.#scanMarker(store);
314
+ const local = scan.files;
315
+ if (!scan.markerOk) {
316
+ if (Object.keys(local).length > 0) {
317
+ this.#log.warn(`${scan.distrustReason}; leaving the memory store folder as found and not syncing`, {
318
+ root: store.files.root().path,
319
+ memory_store_id: store.memoryStoreId,
320
+ });
321
+ return;
322
+ }
323
+ await this.#recover(store, 'the folder or its marker is gone');
324
+ return;
325
+ }
326
+ // A lone file vanishing is an ordinary deletion; two or more at
327
+ // once with nothing left is a wiped folder.
328
+ if (Object.keys(local).length === 0 && store.baseline.size > 1) {
329
+ await this.#recover(store, 'every memory file is gone at once');
330
+ return;
331
+ }
332
+ const remote = new Map();
333
+ for await (const [rel, item] of this.#listMemories(store.memoryStoreId)) {
334
+ remote.set(rel, item);
335
+ }
336
+ const deletes = new DeletePass(this.#syncDeletions, Math.max(DELETE_CAP_FLOOR, Math.min(DELETE_CAP_CEILING, Math.floor(store.baseline.size / 4))), final);
337
+ const pulls = [];
338
+ const baseline = new Map();
339
+ const paths = [...new Set([...remote.keys(), ...Object.keys(local), ...store.baseline.keys()])].sort();
340
+ for (const rel of paths) {
341
+ const remoteItem = remote.get(rel);
342
+ const localSha = local[rel];
343
+ const baseSha = store.baseline.get(rel);
344
+ let sha;
345
+ if (localSha === undefined &&
346
+ baseSha !== undefined &&
347
+ remoteItem !== undefined &&
348
+ remoteItem.content_sha256 === baseSha &&
349
+ !store.readOnly) {
350
+ sha = await this.#corroboratedDelete(store, rel, remoteItem, baseSha, deletes);
351
+ }
352
+ else {
353
+ sha = await this.#syncPath(store, rel, remoteItem, localSha, pulls);
354
+ }
355
+ if (sha !== undefined)
356
+ baseline.set(rel, sha);
357
+ }
358
+ store.baseline = baseline;
359
+ // The content pass: everything above moved only shas.
360
+ await this.#pullAll(store, pulls);
361
+ if (deletes.suppressed > 0) {
362
+ this.#log.debug('remote deletes are disabled; locally deleted memories stay on the server', {
363
+ count: deletes.suppressed,
364
+ memory_store_id: store.memoryStoreId,
365
+ });
366
+ }
367
+ if (deletes.capped > 0) {
368
+ this.#log.warn(`delete cap reached: ${deletes.mode === 'log_only' ? 'would send' : 'sent'} ` +
369
+ `${deletes.attempted} deletes, held ${deletes.capped} for later syncs`, { memory_store_id: store.memoryStoreId });
370
+ }
371
+ }
372
+ catch (e) {
373
+ this.#log.warn('memory sync failed', { memory_store_id: store.memoryStoreId, error: String(e) });
374
+ }
375
+ }
376
+ /** Sync when `syncIntervalMs` has elapsed since the last one. Never throws. */
377
+ async syncIfDue() {
378
+ if (Date.now() - this.#lastSyncAt < this.#syncIntervalMs)
379
+ return;
380
+ await this.syncAll(false);
381
+ }
382
+ /**
383
+ * Upload new and changed files; send no deletes and pull nothing.
384
+ *
385
+ * The push-only rescue pass for a session ending on an error or
386
+ * cancel — best-effort, bounded by the caller: once `signal` aborts no
387
+ * further upload starts, each store cut off part-way logs how many
388
+ * changed files it had not finished uploading, and this resolves without
389
+ * waiting for requests already in flight. Each store uploads up to
390
+ * {@link UPLOAD_CONCURRENCY} files at a time. Skips read-only stores,
391
+ * refused files, files the server already holds, and folders that fail
392
+ * the marker check. Never throws.
393
+ */
394
+ async flushWrites(signal) {
395
+ await Promise.all(this.#stores.map((store) => this.#flushStore(store, signal)));
396
+ }
397
+ async #flushStore(store, signal) {
398
+ const dirty = new Map();
399
+ const unsent = new Set();
400
+ const push = async () => {
401
+ if (store.readOnly)
402
+ return;
403
+ const scan = await this.#scanMarker(store);
404
+ if (!scan.markerOk) {
405
+ this.#log.warn(`${scan.distrustReason}; not uploading anything from the memory store folder`, {
406
+ root: store.files.root().path,
407
+ memory_store_id: store.memoryStoreId,
408
+ });
409
+ return;
410
+ }
411
+ for (const [rel, sha] of Object.entries(scan.files)) {
412
+ if (sha !== store.baseline.get(rel) && store.refusedShas.get(rel) !== sha) {
413
+ dirty.set(rel, sha);
414
+ unsent.add(rel);
415
+ }
416
+ }
417
+ if (dirty.size === 0 || signal?.aborted)
418
+ return;
419
+ const remote = new Map();
420
+ for await (const [rel, item] of this.#listMemories(store.memoryStoreId)) {
421
+ if (signal?.aborted)
422
+ return;
423
+ remote.set(rel, item);
424
+ }
425
+ const uploads = [];
426
+ for (const rel of [...dirty.keys()].sort()) {
427
+ const localSha = dirty.get(rel);
428
+ const baseSha = store.baseline.get(rel);
429
+ const existing = remote.get(rel);
430
+ if (existing !== undefined && existing.content_sha256 === localSha) {
431
+ store.baseline.set(rel, existing.content_sha256);
432
+ unsent.delete(rel);
433
+ continue;
434
+ }
435
+ if (existing !== undefined && existing.content_sha256 !== baseSha) {
436
+ this.#log.warn('memory changed both locally and remotely; the flush leaves the remote version', {
437
+ path: rel,
438
+ memory_store_id: store.memoryStoreId,
439
+ });
440
+ unsent.delete(rel);
441
+ continue;
442
+ }
443
+ uploads.push([rel, localSha, existing]);
444
+ }
445
+ await this.#uploadAll(store, uploads, unsent, signal);
446
+ };
447
+ try {
448
+ await settledOrAborted(push(), signal);
449
+ if (signal?.aborted && unsent.size > 0) {
450
+ this.#log.warn(`memory flush cut off part-way; ${unsent.size} of ${dirty.size} changed files had not finished uploading`, { memory_store_id: store.memoryStoreId });
451
+ }
452
+ }
453
+ catch (e) {
454
+ this.#log.warn('memory flush failed', { memory_store_id: store.memoryStoreId, error: String(e) });
455
+ }
456
+ }
457
+ /**
458
+ * Remove every store directory that {@link SessionMemoryStores.download}
459
+ * created. Pre-existing directories are left alone — that is
460
+ * {@link FileStore.dispose}'s own rule. A folder that fails the marker
461
+ * check is kept too — sync left it as found, so must dispose.
462
+ */
463
+ async dispose() {
464
+ for (const store of this.#stores) {
465
+ const root = store.files.root();
466
+ try {
467
+ const scan = await this.#scanMarker(store);
468
+ if (!scan.markerOk && Object.keys(scan.files).length > 0) {
469
+ this.#log.warn(`${scan.distrustReason}; leaving the memory store folder on disk`, {
470
+ root: root.path,
471
+ memory_store_id: store.memoryStoreId,
472
+ });
473
+ continue;
474
+ }
475
+ await store.files.dispose();
476
+ }
477
+ catch (e) {
478
+ if (!(e instanceof FileStoreError) && !isErrno(e))
479
+ throw e;
480
+ this.#log.warn('failed to remove the memory store folder', {
481
+ root: store.files.root().path,
482
+ memory_store_id: store.memoryStoreId,
483
+ error: String(e),
484
+ });
485
+ continue;
486
+ }
487
+ if (root.removedOnDispose) {
488
+ this.#log.info('removed memory store dir', {
489
+ dest: root.path,
490
+ memory_store_id: store.memoryStoreId,
491
+ });
492
+ }
493
+ }
494
+ }
495
+ /** Rebuild a destroyed folder from the server; sends no deletes, no uploads. */
496
+ async #recover(store, reason) {
497
+ this.#log.warn(`${reason}; re-downloading the memory store folder instead of syncing`, {
498
+ root: store.files.root().path,
499
+ memory_store_id: store.memoryStoreId,
500
+ });
501
+ await store.files.createRoot();
502
+ await this.#stampAndPull(store);
503
+ }
504
+ /**
505
+ * Write the marker, then pull every remote memory. Baseline is cleared
506
+ * first so a failed write never leaves an entry whose file is not on disk.
507
+ * Every memory is needed here, so the listing carries the content — pages
508
+ * cost far fewer round-trips than a request per memory.
509
+ */
510
+ async #stampAndPull(store) {
511
+ store.baseline = new Map();
512
+ store.pendingDeletes.clear();
513
+ await store.files.put(MARKER_PATH, `version ${MARKER_VERSION}\n${store.memoryStoreId}`);
514
+ for await (const [rel, item] of this.#listMemories(store.memoryStoreId, 'full')) {
515
+ if (await this.#write(store, rel, item.content ?? '')) {
516
+ store.baseline.set(rel, item.content_sha256);
517
+ }
518
+ }
519
+ }
520
+ /**
521
+ * Reconcile one path. Returns the sha to record in the baseline, or
522
+ * `undefined` to drop the path from it.
523
+ *
524
+ * `pulls` is an output: when the remote version should be written to disk,
525
+ * this appends `[rel, remote]` to it instead of writing — `rel` is the
526
+ * file to write, `remote` the listed memory whose content `#pullAll` will
527
+ * fetch and write there.
528
+ */
529
+ async #syncPath(store, rel, remote, localSha, pulls) {
530
+ const baseSha = store.baseline.get(rel);
531
+ if (localSha !== undefined) {
532
+ store.pendingDeletes.delete(rel);
533
+ }
534
+ if (!remote) {
535
+ if (localSha === undefined) {
536
+ store.pendingDeletes.delete(rel);
537
+ return undefined;
538
+ }
539
+ if (baseSha !== undefined) {
540
+ if (localSha === baseSha) {
541
+ const fresh = await this.#removeLocal(store, rel, baseSha);
542
+ if (fresh === undefined)
543
+ return undefined;
544
+ if (fresh === baseSha)
545
+ return baseSha;
546
+ localSha = fresh;
547
+ }
548
+ // The file holds an un-pushed edit — the only copy; falling
549
+ // through re-creates or keeps it.
550
+ if (store.readOnly) {
551
+ this.#log.warn('memory deleted remotely but edited locally; keeping the file, ' +
552
+ 'which a read-only store cannot push', { path: rel, memory_store_id: store.memoryStoreId });
553
+ }
554
+ else if (store.refusedShas.get(rel) !== localSha) {
555
+ this.#log.info('memory deleted remotely but edited locally; re-creating it from the file', {
556
+ path: rel,
557
+ memory_store_id: store.memoryStoreId,
558
+ });
559
+ }
560
+ }
561
+ if (store.readOnly)
562
+ return undefined;
563
+ if (store.refusedShas.get(rel) === localSha)
564
+ return undefined;
565
+ return await this.#upload(store, rel, localSha, undefined);
566
+ }
567
+ const remoteSha = remote.content_sha256;
568
+ const remoteChanged = remoteSha !== baseSha;
569
+ const locallyEdited = localSha !== undefined && localSha !== baseSha && localSha !== remoteSha;
570
+ // Read-only stores never push, so their local edits don't count.
571
+ const localChanged = !store.readOnly && locallyEdited;
572
+ if (localSha === undefined && baseSha !== undefined) {
573
+ // Only successful writes enter the baseline, so this file was verifiably
574
+ // on disk and is now gone: a real local deletion. The unchanged-remote
575
+ // case went to #corroboratedDelete.
576
+ if (remoteChanged) {
577
+ this.#log.warn('memory deleted locally but changed remotely; restoring the remote version', {
578
+ path: rel,
579
+ memory_store_id: store.memoryStoreId,
580
+ });
581
+ store.pendingDeletes.delete(rel);
582
+ pulls.push([rel, remote]);
583
+ }
584
+ return baseSha;
585
+ }
586
+ if (remoteChanged) {
587
+ // The file already holds the remote bytes — adopt without a fetch.
588
+ if (localSha === remoteSha)
589
+ return remoteSha;
590
+ // locallyEdited, not localChanged: warn on read-only overwrites too.
591
+ if (locallyEdited) {
592
+ this.#log.warn('memory changed both locally and remotely; keeping the remote version', {
593
+ path: rel,
594
+ memory_store_id: store.memoryStoreId,
595
+ });
596
+ }
597
+ pulls.push([rel, remote]);
598
+ return baseSha;
599
+ }
600
+ if (localChanged) {
601
+ if (store.refusedShas.get(rel) === localSha)
602
+ return remoteSha;
603
+ return (await this.#upload(store, rel, localSha, remote)) ?? remoteSha;
604
+ }
605
+ return remoteSha;
606
+ }
607
+ /**
608
+ * Remove the file for a memory the server no longer has, if it still holds
609
+ * `expectSha`. Returns `undefined` when the file is gone from disk,
610
+ * `expectSha` when it must stay in the baseline (I/O error), or the file's
611
+ * fresh sha when it was edited since the scan.
612
+ */
613
+ async #removeLocal(store, rel, expectSha) {
614
+ // Re-read: an edit since the scan makes this file the only copy.
615
+ let freshSha;
616
+ try {
617
+ freshSha = await store.files.hashFile(rel);
618
+ }
619
+ catch (e) {
620
+ if (!(e instanceof FileStoreError) && !isErrno(e))
621
+ throw e;
622
+ return expectSha;
623
+ }
624
+ if (freshSha === null)
625
+ return undefined;
626
+ if (freshSha !== expectSha)
627
+ return freshSha;
628
+ try {
629
+ await store.files.remove(rel);
630
+ }
631
+ catch (e) {
632
+ if (!(e instanceof FileStoreError) && !isErrno(e))
633
+ throw e;
634
+ this.#log.warn('failed to remove memory deleted remotely', {
635
+ path: rel,
636
+ memory_store_id: store.memoryStoreId,
637
+ error: String(e),
638
+ });
639
+ return expectSha;
640
+ }
641
+ return undefined;
642
+ }
643
+ /**
644
+ * Write a memory's content to disk; `false` (and a warning) on failure.
645
+ *
646
+ * A `..` component in the wire path reaches here as {@link FileStoreError} —
647
+ * that is the escape guard.
648
+ */
649
+ async #write(store, rel, content) {
650
+ try {
651
+ await store.files.put(rel, content);
652
+ }
653
+ catch (e) {
654
+ if (!(e instanceof FileStoreError) && !isErrno(e))
655
+ throw e;
656
+ this.#log.warn('failed to write memory', {
657
+ path: rel,
658
+ memory_store_id: store.memoryStoreId,
659
+ error: String(e),
660
+ });
661
+ return false;
662
+ }
663
+ return true;
664
+ }
665
+ /**
666
+ * Fetch and write the given memories, {@link FETCH_CONCURRENCY} at a time.
667
+ *
668
+ * The sync's content pass: the listing carried no content, so each memory
669
+ * is fetched individually and written as it arrives. On success the path's
670
+ * baseline advances; on a failed fetch or write the old entry stays and the
671
+ * next sync retries. A 404 means the memory was deleted after the listing —
672
+ * the next sync reconciles it.
673
+ */
674
+ async #pullAll(store, pulls) {
675
+ if (pulls.length === 0)
676
+ return;
677
+ const pullOne = async (rel, listed) => {
678
+ let item;
679
+ try {
680
+ item = await this.#client.beta.memoryStores.memories.retrieve(listed.id, {
681
+ memory_store_id: store.memoryStoreId,
682
+ view: 'full',
683
+ });
684
+ }
685
+ catch (e) {
686
+ if (isStatus(e, 404))
687
+ return;
688
+ this.#log.warn('failed to fetch memory content', {
689
+ path: rel,
690
+ memory_store_id: store.memoryStoreId,
691
+ error: String(e),
692
+ });
693
+ return;
694
+ }
695
+ if (await this.#write(store, rel, item.content ?? '')) {
696
+ store.baseline.set(rel, item.content_sha256);
697
+ }
698
+ };
699
+ // A fixed pool of workers drains the queue; the write stays inside the
700
+ // worker so a slow disk cannot let fetched bodies pile up beyond the bound.
701
+ const queue = pulls[Symbol.iterator]();
702
+ const worker = async () => {
703
+ for (const [rel, listed] of queue)
704
+ await pullOne(rel, listed);
705
+ };
706
+ await Promise.all(Array.from({ length: Math.min(FETCH_CONCURRENCY, pulls.length) }, worker));
707
+ }
708
+ /**
709
+ * Upload the given files, {@link UPLOAD_CONCURRENCY} at a time, taking each
710
+ * path off `unsent` as its upload returns. No upload starts once `signal`
711
+ * aborts; the ones already in flight run to completion.
712
+ */
713
+ async #uploadAll(store, uploads, unsent, signal) {
714
+ const queue = uploads[Symbol.iterator]();
715
+ const worker = async () => {
716
+ for (const [rel, localSha, existing] of queue) {
717
+ if (signal?.aborted)
718
+ return;
719
+ const sha = await this.#upload(store, rel, localSha, existing);
720
+ unsent.delete(rel);
721
+ if (sha !== undefined)
722
+ store.baseline.set(rel, sha);
723
+ }
724
+ };
725
+ await Promise.all(Array.from({ length: Math.min(UPLOAD_CONCURRENCY, uploads.length) }, worker));
726
+ }
727
+ /**
728
+ * The store's memories keyed by relative path (the wire path's leading `/`
729
+ * stripped — `#upload` re-prefixes it) — `basic` view (shas, no content) at
730
+ * {@link LIST_PAGE_SIZE} per page unless the caller needs `full` pages.
731
+ * `memory_prefix` rollups and the reserved marker path are skipped.
732
+ */
733
+ async *#listMemories(memoryStoreId, view = 'basic') {
734
+ const limit = view === 'basic' ? LIST_PAGE_SIZE : FULL_LIST_PAGE_SIZE;
735
+ for await (const item of this.#client.beta.memoryStores.memories.list(memoryStoreId, { view, limit })) {
736
+ if (item.type !== 'memory')
737
+ continue;
738
+ const rel = item.path.replace(/^\/+/, '');
739
+ if (rel === MARKER_PATH) {
740
+ this.#log.warn('the server listed the reserved marker path; skipping', {
741
+ path: item.path,
742
+ memory_store_id: memoryStoreId,
743
+ });
744
+ continue;
745
+ }
746
+ yield [rel, item];
747
+ }
748
+ }
749
+ /**
750
+ * Push one local file; `undefined` keeps the old baseline so the next pass retries.
751
+ *
752
+ * A refusal the server would repeat (400/413, the utf-8 gate) enters
753
+ * `refusedShas`: warned once, retried only after the file changes.
754
+ */
755
+ async #upload(store, rel, localSha, existing) {
756
+ try {
757
+ const data = await store.files.get(rel);
758
+ if (data === null)
759
+ return undefined;
760
+ const content = decodeUTF8(data);
761
+ const item = existing ?
762
+ await this.#client.beta.memoryStores.memories.update(existing.id, {
763
+ memory_store_id: store.memoryStoreId,
764
+ content,
765
+ precondition: { type: 'content_sha256', content_sha256: existing.content_sha256 },
766
+ })
767
+ : await this.#client.beta.memoryStores.memories.create(store.memoryStoreId, {
768
+ path: '/' + rel,
769
+ content,
770
+ });
771
+ store.refusedShas.delete(rel);
772
+ return item.content_sha256;
773
+ }
774
+ catch (e) {
775
+ if (existing && isStatus(e, 404)) {
776
+ // Deleted remotely since the listing, so this file is now the only copy.
777
+ return await this.#upload(store, rel, localSha, undefined);
778
+ }
779
+ const permanent = e instanceof FileStoreError || isStatus(e, 400) || isStatus(e, 413);
780
+ if (existing && isStatus(e, 409)) {
781
+ // The precondition lost a race: the remote moved under us, so the push
782
+ // is dropped. The local file is now stale — the next sync sees
783
+ // remoteChanged and pulls the winner over it.
784
+ this.#log.warn('memory changed both locally and remotely; the upload was refused and the local edit loses', {
785
+ path: rel,
786
+ memory_store_id: store.memoryStoreId,
787
+ });
788
+ }
789
+ else if (permanent && localSha !== undefined) {
790
+ store.refusedShas.set(rel, localSha);
791
+ this.#log.warn('the server rejected this memory file, so it stays un-synced until its content changes', { path: rel, memory_store_id: store.memoryStoreId, rejection: String(e) });
792
+ }
793
+ else {
794
+ this.#log.warn('failed to upload memory', {
795
+ path: rel,
796
+ memory_store_id: store.memoryStoreId,
797
+ error: String(e),
798
+ });
799
+ }
800
+ return undefined;
801
+ }
802
+ }
803
+ /** Send the server delete only after the wait, the cap, and a fresh re-check all clear. */
804
+ async #corroboratedDelete(store, rel, remote, baseSha, deletes) {
805
+ if (deletes.mode === 'disabled') {
806
+ deletes.suppressed++;
807
+ return baseSha;
808
+ }
809
+ let firstAbsent = store.pendingDeletes.get(rel);
810
+ if (firstAbsent === undefined) {
811
+ firstAbsent = Date.now();
812
+ store.pendingDeletes.set(rel, firstAbsent);
813
+ }
814
+ if (!deletes.waiveWindow && Date.now() - firstAbsent < DELETE_CORROBORATION_MS) {
815
+ return baseSha;
816
+ }
817
+ let markerOk;
818
+ let stillAbsent;
819
+ try {
820
+ // Re-check: the folder may have been wiped mid-sync.
821
+ markerOk = (await store.files.hashFile(MARKER_PATH)) === markerSha(store.memoryStoreId);
822
+ stillAbsent = (await store.files.hashFile(rel)) === null;
823
+ }
824
+ catch (e) {
825
+ if (!(e instanceof FileStoreError) && !isErrno(e))
826
+ throw e;
827
+ markerOk = stillAbsent = false;
828
+ }
829
+ if (!markerOk)
830
+ return baseSha;
831
+ if (!stillAbsent) {
832
+ store.pendingDeletes.delete(rel);
833
+ return baseSha;
834
+ }
835
+ if (!deletes.takeSlot())
836
+ return baseSha;
837
+ if (deletes.mode === 'log_only') {
838
+ // Repeats each sync — the log is the dry run.
839
+ this.#log.info('log-only: sync would delete this memory on the server', {
840
+ path: rel,
841
+ memory_store_id: store.memoryStoreId,
842
+ });
843
+ return baseSha;
844
+ }
845
+ const sha = await this.#deleteRemote(store, rel, remote, baseSha);
846
+ if (sha === undefined) {
847
+ store.pendingDeletes.delete(rel);
848
+ }
849
+ return sha;
850
+ }
851
+ async #deleteRemote(store, rel, remote, baseSha) {
852
+ try {
853
+ await this.#client.beta.memoryStores.memories.delete(remote.id, {
854
+ memory_store_id: store.memoryStoreId,
855
+ expected_content_sha256: baseSha,
856
+ });
857
+ }
858
+ catch (e) {
859
+ if (isStatus(e, 404))
860
+ return undefined; // already gone remotely too
861
+ if (isStatus(e, 409) || isStatus(e, 412)) {
862
+ this.#log.warn('memory deleted locally but changed remotely; keeping the remote version', {
863
+ path: rel,
864
+ memory_store_id: store.memoryStoreId,
865
+ });
866
+ }
867
+ else {
868
+ this.#log.warn('failed to delete memory', {
869
+ path: rel,
870
+ memory_store_id: store.memoryStoreId,
871
+ error: String(e),
872
+ });
873
+ }
874
+ return baseSha;
875
+ }
876
+ this.#log.info('propagated local deletion', { path: rel, memory_store_id: store.memoryStoreId });
877
+ return undefined;
878
+ }
879
+ }
880
+ /**
881
+ * True for a thrown value shaped like a Node filesystem error. Shape-checked,
882
+ * not `instanceof Error` — fs errors can come from another realm.
883
+ */
884
+ function isErrno(e) {
885
+ return typeof e === 'object' && e !== null && typeof e.code === 'string';
886
+ }
887
+ /**
888
+ * Resolve when `p` settles, or as soon as `signal` aborts. A rejection from
889
+ * `p` before the abort propagates; one after it is dropped.
890
+ */
891
+ async function settledOrAborted(p, signal) {
892
+ if (!signal) {
893
+ await p;
894
+ return;
895
+ }
896
+ let onAbort;
897
+ const aborted = new Promise((resolve) => {
898
+ onAbort = resolve;
899
+ if (signal.aborted)
900
+ resolve();
901
+ });
902
+ signal.addEventListener('abort', onAbort, { once: true });
903
+ try {
904
+ await Promise.race([p, aborted]);
905
+ }
906
+ finally {
907
+ signal.removeEventListener('abort', onAbort);
908
+ }
909
+ }
910
+ //# sourceMappingURL=memories.js.map