@harrylabsj/kiwi 0.6.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 (636) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +150 -0
  3. package/contracts/candidate-agent-dto-1.0.README.md +39 -0
  4. package/contracts/candidate-agent-dto-1.0.schema.json +172 -0
  5. package/contracts/interop/kiwi-buyer-message-send.json +43 -0
  6. package/contracts/interop/knp-data-part-examples.json +243 -0
  7. package/contracts/kiwi-catalog/1.0/agent-record.schema.json +78 -0
  8. package/contracts/kiwi-catalog/1.0/listing-record.schema.json +88 -0
  9. package/contracts/kiwi-catalog/1.0/listing-search-result.schema.json +60 -0
  10. package/contracts/negotiation/1.0/schema.json +501 -0
  11. package/contracts/shopping.negotiation/0.1/capabilities.schema.json +47 -0
  12. package/contracts/shopping.negotiation/0.1/decision.schema.json +120 -0
  13. package/contracts/shopping.negotiation/0.1/policy-result.schema.json +52 -0
  14. package/contracts/shopping.negotiation/0.1/snapshot.schema.json +172 -0
  15. package/dist/a2a/client/client.d.ts +33 -0
  16. package/dist/a2a/client/client.js +161 -0
  17. package/dist/a2a/client/client.js.map +1 -0
  18. package/dist/a2a/client/error.d.ts +35 -0
  19. package/dist/a2a/client/error.js +34 -0
  20. package/dist/a2a/client/error.js.map +1 -0
  21. package/dist/a2a/client/index.d.ts +29 -0
  22. package/dist/a2a/client/index.js +26 -0
  23. package/dist/a2a/client/index.js.map +1 -0
  24. package/dist/a2a/client/jsonrpc.d.ts +49 -0
  25. package/dist/a2a/client/jsonrpc.js +85 -0
  26. package/dist/a2a/client/jsonrpc.js.map +1 -0
  27. package/dist/a2a/client/parse.d.ts +18 -0
  28. package/dist/a2a/client/parse.js +134 -0
  29. package/dist/a2a/client/parse.js.map +1 -0
  30. package/dist/a2a/client/types.d.ts +102 -0
  31. package/dist/a2a/client/types.js +26 -0
  32. package/dist/a2a/client/types.js.map +1 -0
  33. package/dist/a2a/client/url-policy.d.ts +44 -0
  34. package/dist/a2a/client/url-policy.js +242 -0
  35. package/dist/a2a/client/url-policy.js.map +1 -0
  36. package/dist/a2a/negotiate.d.ts +68 -0
  37. package/dist/a2a/negotiate.js +288 -0
  38. package/dist/a2a/negotiate.js.map +1 -0
  39. package/dist/a2a/node.d.ts +37 -0
  40. package/dist/a2a/node.js +155 -0
  41. package/dist/a2a/node.js.map +1 -0
  42. package/dist/a2a/server/auth.d.ts +38 -0
  43. package/dist/a2a/server/auth.js +112 -0
  44. package/dist/a2a/server/auth.js.map +1 -0
  45. package/dist/a2a/server/card.d.ts +19 -0
  46. package/dist/a2a/server/card.js +81 -0
  47. package/dist/a2a/server/card.js.map +1 -0
  48. package/dist/a2a/server/errors.d.ts +109 -0
  49. package/dist/a2a/server/errors.js +178 -0
  50. package/dist/a2a/server/errors.js.map +1 -0
  51. package/dist/a2a/server/handler.d.ts +22 -0
  52. package/dist/a2a/server/handler.js +57 -0
  53. package/dist/a2a/server/handler.js.map +1 -0
  54. package/dist/a2a/server/inbound-message.d.ts +37 -0
  55. package/dist/a2a/server/inbound-message.js +104 -0
  56. package/dist/a2a/server/inbound-message.js.map +1 -0
  57. package/dist/a2a/server/index.d.ts +43 -0
  58. package/dist/a2a/server/index.js +42 -0
  59. package/dist/a2a/server/index.js.map +1 -0
  60. package/dist/a2a/server/merchant-handler.d.ts +69 -0
  61. package/dist/a2a/server/merchant-handler.js +316 -0
  62. package/dist/a2a/server/merchant-handler.js.map +1 -0
  63. package/dist/a2a/server/pipeline.d.ts +69 -0
  64. package/dist/a2a/server/pipeline.js +329 -0
  65. package/dist/a2a/server/pipeline.js.map +1 -0
  66. package/dist/a2a/server/server.d.ts +64 -0
  67. package/dist/a2a/server/server.js +420 -0
  68. package/dist/a2a/server/server.js.map +1 -0
  69. package/dist/a2a/server/task-registry.d.ts +31 -0
  70. package/dist/a2a/server/task-registry.js +67 -0
  71. package/dist/a2a/server/task-registry.js.map +1 -0
  72. package/dist/a2a/server/throttle.d.ts +154 -0
  73. package/dist/a2a/server/throttle.js +289 -0
  74. package/dist/a2a/server/throttle.js.map +1 -0
  75. package/dist/a2a/server/types.d.ts +191 -0
  76. package/dist/a2a/server/types.js +17 -0
  77. package/dist/a2a/server/types.js.map +1 -0
  78. package/dist/a2a/server/ucp.d.ts +49 -0
  79. package/dist/a2a/server/ucp.js +85 -0
  80. package/dist/a2a/server/ucp.js.map +1 -0
  81. package/dist/a2a/task/index.d.ts +24 -0
  82. package/dist/a2a/task/index.js +22 -0
  83. package/dist/a2a/task/index.js.map +1 -0
  84. package/dist/a2a/task/ledger.d.ts +48 -0
  85. package/dist/a2a/task/ledger.js +65 -0
  86. package/dist/a2a/task/ledger.js.map +1 -0
  87. package/dist/a2a/task/poller.d.ts +93 -0
  88. package/dist/a2a/task/poller.js +184 -0
  89. package/dist/a2a/task/poller.js.map +1 -0
  90. package/dist/a2a/task/state.d.ts +72 -0
  91. package/dist/a2a/task/state.js +119 -0
  92. package/dist/a2a/task/state.js.map +1 -0
  93. package/dist/a2a/ucp-agent.d.ts +49 -0
  94. package/dist/a2a/ucp-agent.js +78 -0
  95. package/dist/a2a/ucp-agent.js.map +1 -0
  96. package/dist/agent/agent-db.d.ts +31 -0
  97. package/dist/agent/agent-db.js +91 -0
  98. package/dist/agent/agent-db.js.map +1 -0
  99. package/dist/agent/buyer/buyer-tools.d.ts +70 -0
  100. package/dist/agent/buyer/buyer-tools.js +1353 -0
  101. package/dist/agent/buyer/buyer-tools.js.map +1 -0
  102. package/dist/agent/buyer/ranker.d.ts +53 -0
  103. package/dist/agent/buyer/ranker.js +153 -0
  104. package/dist/agent/buyer/ranker.js.map +1 -0
  105. package/dist/agent/buyer/scheduler.d.ts +58 -0
  106. package/dist/agent/buyer/scheduler.js +317 -0
  107. package/dist/agent/buyer/scheduler.js.map +1 -0
  108. package/dist/agent/buyer/search-loop.d.ts +42 -0
  109. package/dist/agent/buyer/search-loop.js +280 -0
  110. package/dist/agent/buyer/search-loop.js.map +1 -0
  111. package/dist/agent/buyer/task-store.d.ts +178 -0
  112. package/dist/agent/buyer/task-store.js +637 -0
  113. package/dist/agent/buyer/task-store.js.map +1 -0
  114. package/dist/agent/buyer/types.d.ts +222 -0
  115. package/dist/agent/buyer/types.js +124 -0
  116. package/dist/agent/buyer/types.js.map +1 -0
  117. package/dist/agent/chat-tools.d.ts +41 -0
  118. package/dist/agent/chat-tools.js +193 -0
  119. package/dist/agent/chat-tools.js.map +1 -0
  120. package/dist/agent/chat-tui.d.ts +49 -0
  121. package/dist/agent/chat-tui.js +314 -0
  122. package/dist/agent/chat-tui.js.map +1 -0
  123. package/dist/agent/connector/fake-connector.d.ts +50 -0
  124. package/dist/agent/connector/fake-connector.js +138 -0
  125. package/dist/agent/connector/fake-connector.js.map +1 -0
  126. package/dist/agent/connector/http-connector.d.ts +49 -0
  127. package/dist/agent/connector/http-connector.js +208 -0
  128. package/dist/agent/connector/http-connector.js.map +1 -0
  129. package/dist/agent/connector/types.d.ts +103 -0
  130. package/dist/agent/connector/types.js +97 -0
  131. package/dist/agent/connector/types.js.map +1 -0
  132. package/dist/agent/fake-chat-model.d.ts +22 -0
  133. package/dist/agent/fake-chat-model.js +134 -0
  134. package/dist/agent/fake-chat-model.js.map +1 -0
  135. package/dist/agent/kernel.d.ts +258 -0
  136. package/dist/agent/kernel.js +1099 -0
  137. package/dist/agent/kernel.js.map +1 -0
  138. package/dist/agent/memory/schema.d.ts +32 -0
  139. package/dist/agent/memory/schema.js +291 -0
  140. package/dist/agent/memory/schema.js.map +1 -0
  141. package/dist/agent/memory/store.d.ts +193 -0
  142. package/dist/agent/memory/store.js +881 -0
  143. package/dist/agent/memory/store.js.map +1 -0
  144. package/dist/agent/memory/types.d.ts +139 -0
  145. package/dist/agent/memory/types.js +119 -0
  146. package/dist/agent/memory/types.js.map +1 -0
  147. package/dist/agent/memory/vault.d.ts +49 -0
  148. package/dist/agent/memory/vault.js +125 -0
  149. package/dist/agent/memory/vault.js.map +1 -0
  150. package/dist/agent/merchant/action-candidate.d.ts +106 -0
  151. package/dist/agent/merchant/action-candidate.js +241 -0
  152. package/dist/agent/merchant/action-candidate.js.map +1 -0
  153. package/dist/agent/merchant/credential-broker.d.ts +67 -0
  154. package/dist/agent/merchant/credential-broker.js +76 -0
  155. package/dist/agent/merchant/credential-broker.js.map +1 -0
  156. package/dist/agent/merchant/fake-merchant-client.d.ts +47 -0
  157. package/dist/agent/merchant/fake-merchant-client.js +143 -0
  158. package/dist/agent/merchant/fake-merchant-client.js.map +1 -0
  159. package/dist/agent/merchant/merchant-client.d.ts +49 -0
  160. package/dist/agent/merchant/merchant-client.js +186 -0
  161. package/dist/agent/merchant/merchant-client.js.map +1 -0
  162. package/dist/agent/merchant/merchant-tools.d.ts +74 -0
  163. package/dist/agent/merchant/merchant-tools.js +582 -0
  164. package/dist/agent/merchant/merchant-tools.js.map +1 -0
  165. package/dist/agent/merchant/types.d.ts +119 -0
  166. package/dist/agent/merchant/types.js +133 -0
  167. package/dist/agent/merchant/types.js.map +1 -0
  168. package/dist/agent/mode.d.ts +32 -0
  169. package/dist/agent/mode.js +34 -0
  170. package/dist/agent/mode.js.map +1 -0
  171. package/dist/agent/negotiation-chat.d.ts +81 -0
  172. package/dist/agent/negotiation-chat.js +330 -0
  173. package/dist/agent/negotiation-chat.js.map +1 -0
  174. package/dist/agent/session.d.ts +22 -0
  175. package/dist/agent/session.js +164 -0
  176. package/dist/agent/session.js.map +1 -0
  177. package/dist/agent/system-prompt.d.ts +26 -0
  178. package/dist/agent/system-prompt.js +77 -0
  179. package/dist/agent/system-prompt.js.map +1 -0
  180. package/dist/agent/write-gate.d.ts +98 -0
  181. package/dist/agent/write-gate.js +76 -0
  182. package/dist/agent/write-gate.js.map +1 -0
  183. package/dist/cli.d.ts +18 -0
  184. package/dist/cli.js +1139 -0
  185. package/dist/cli.js.map +1 -0
  186. package/dist/commerce/data-source.d.ts +88 -0
  187. package/dist/commerce/data-source.js +25 -0
  188. package/dist/commerce/data-source.js.map +1 -0
  189. package/dist/commerce/erp-source.d.ts +51 -0
  190. package/dist/commerce/erp-source.js +184 -0
  191. package/dist/commerce/erp-source.js.map +1 -0
  192. package/dist/commerce/fake-client.d.ts +167 -0
  193. package/dist/commerce/fake-client.js +549 -0
  194. package/dist/commerce/fake-client.js.map +1 -0
  195. package/dist/commerce/http-client.d.ts +73 -0
  196. package/dist/commerce/http-client.js +284 -0
  197. package/dist/commerce/http-client.js.map +1 -0
  198. package/dist/commerce/local-db-source.d.ts +50 -0
  199. package/dist/commerce/local-db-source.js +162 -0
  200. package/dist/commerce/local-db-source.js.map +1 -0
  201. package/dist/commerce/shopping-cli-source.d.ts +57 -0
  202. package/dist/commerce/shopping-cli-source.js +206 -0
  203. package/dist/commerce/shopping-cli-source.js.map +1 -0
  204. package/dist/commerce/types.d.ts +146 -0
  205. package/dist/commerce/types.js +31 -0
  206. package/dist/commerce/types.js.map +1 -0
  207. package/dist/config/profile.d.ts +122 -0
  208. package/dist/config/profile.js +386 -0
  209. package/dist/config/profile.js.map +1 -0
  210. package/dist/contracts/negotiation-schema.d.ts +28 -0
  211. package/dist/contracts/negotiation-schema.js +101 -0
  212. package/dist/contracts/negotiation-schema.js.map +1 -0
  213. package/dist/contracts/schemas.d.ts +31 -0
  214. package/dist/contracts/schemas.js +122 -0
  215. package/dist/contracts/schemas.js.map +1 -0
  216. package/dist/counterparty/a2a-direct/index.d.ts +41 -0
  217. package/dist/counterparty/a2a-direct/index.js +330 -0
  218. package/dist/counterparty/a2a-direct/index.js.map +1 -0
  219. package/dist/counterparty/channel.d.ts +200 -0
  220. package/dist/counterparty/channel.js +61 -0
  221. package/dist/counterparty/channel.js.map +1 -0
  222. package/dist/counterparty/index.d.ts +28 -0
  223. package/dist/counterparty/index.js +24 -0
  224. package/dist/counterparty/index.js.map +1 -0
  225. package/dist/counterparty/platform-api/index.d.ts +41 -0
  226. package/dist/counterparty/platform-api/index.js +65 -0
  227. package/dist/counterparty/platform-api/index.js.map +1 -0
  228. package/dist/counterparty/registry.d.ts +51 -0
  229. package/dist/counterparty/registry.js +71 -0
  230. package/dist/counterparty/registry.js.map +1 -0
  231. package/dist/counterparty/shopping-cli-hosted/index.d.ts +36 -0
  232. package/dist/counterparty/shopping-cli-hosted/index.js +337 -0
  233. package/dist/counterparty/shopping-cli-hosted/index.js.map +1 -0
  234. package/dist/discovery/agent-card/error.d.ts +28 -0
  235. package/dist/discovery/agent-card/error.js +31 -0
  236. package/dist/discovery/agent-card/error.js.map +1 -0
  237. package/dist/discovery/agent-card/extensions.d.ts +20 -0
  238. package/dist/discovery/agent-card/extensions.js +59 -0
  239. package/dist/discovery/agent-card/extensions.js.map +1 -0
  240. package/dist/discovery/agent-card/index.d.ts +29 -0
  241. package/dist/discovery/agent-card/index.js +38 -0
  242. package/dist/discovery/agent-card/index.js.map +1 -0
  243. package/dist/discovery/agent-card/secrets.d.ts +30 -0
  244. package/dist/discovery/agent-card/secrets.js +240 -0
  245. package/dist/discovery/agent-card/secrets.js.map +1 -0
  246. package/dist/discovery/agent-card/types.d.ts +110 -0
  247. package/dist/discovery/agent-card/types.js +37 -0
  248. package/dist/discovery/agent-card/types.js.map +1 -0
  249. package/dist/discovery/agent-card/validate.d.ts +31 -0
  250. package/dist/discovery/agent-card/validate.js +210 -0
  251. package/dist/discovery/agent-card/validate.js.map +1 -0
  252. package/dist/discovery/capability/error.d.ts +23 -0
  253. package/dist/discovery/capability/error.js +28 -0
  254. package/dist/discovery/capability/error.js.map +1 -0
  255. package/dist/discovery/capability/index.d.ts +21 -0
  256. package/dist/discovery/capability/index.js +21 -0
  257. package/dist/discovery/capability/index.js.map +1 -0
  258. package/dist/discovery/capability/intersect.d.ts +56 -0
  259. package/dist/discovery/capability/intersect.js +137 -0
  260. package/dist/discovery/capability/intersect.js.map +1 -0
  261. package/dist/discovery/catalog-source/errors.d.ts +32 -0
  262. package/dist/discovery/catalog-source/errors.js +41 -0
  263. package/dist/discovery/catalog-source/errors.js.map +1 -0
  264. package/dist/discovery/catalog-source/index.d.ts +30 -0
  265. package/dist/discovery/catalog-source/index.js +29 -0
  266. package/dist/discovery/catalog-source/index.js.map +1 -0
  267. package/dist/discovery/catalog-source/kiwi-record.d.ts +183 -0
  268. package/dist/discovery/catalog-source/kiwi-record.js +72 -0
  269. package/dist/discovery/catalog-source/kiwi-record.js.map +1 -0
  270. package/dist/discovery/catalog-source/kiwi-schema.d.ts +33 -0
  271. package/dist/discovery/catalog-source/kiwi-schema.js +106 -0
  272. package/dist/discovery/catalog-source/kiwi-schema.js.map +1 -0
  273. package/dist/discovery/catalog-source/kiwi-source.d.ts +38 -0
  274. package/dist/discovery/catalog-source/kiwi-source.js +318 -0
  275. package/dist/discovery/catalog-source/kiwi-source.js.map +1 -0
  276. package/dist/discovery/catalog-source/register.d.ts +48 -0
  277. package/dist/discovery/catalog-source/register.js +83 -0
  278. package/dist/discovery/catalog-source/register.js.map +1 -0
  279. package/dist/discovery/catalog-source/schema.d.ts +21 -0
  280. package/dist/discovery/catalog-source/schema.js +63 -0
  281. package/dist/discovery/catalog-source/schema.js.map +1 -0
  282. package/dist/discovery/catalog-source/source.d.ts +45 -0
  283. package/dist/discovery/catalog-source/source.js +197 -0
  284. package/dist/discovery/catalog-source/source.js.map +1 -0
  285. package/dist/discovery/catalog-source/types.d.ts +122 -0
  286. package/dist/discovery/catalog-source/types.js +30 -0
  287. package/dist/discovery/catalog-source/types.js.map +1 -0
  288. package/dist/discovery/index.d.ts +25 -0
  289. package/dist/discovery/index.js +25 -0
  290. package/dist/discovery/index.js.map +1 -0
  291. package/dist/discovery/resolve.d.ts +175 -0
  292. package/dist/discovery/resolve.js +367 -0
  293. package/dist/discovery/resolve.js.map +1 -0
  294. package/dist/discovery/ucp/error.d.ts +25 -0
  295. package/dist/discovery/ucp/error.js +27 -0
  296. package/dist/discovery/ucp/error.js.map +1 -0
  297. package/dist/discovery/ucp/index.d.ts +33 -0
  298. package/dist/discovery/ucp/index.js +28 -0
  299. package/dist/discovery/ucp/index.js.map +1 -0
  300. package/dist/discovery/ucp/intersect.d.ts +148 -0
  301. package/dist/discovery/ucp/intersect.js +312 -0
  302. package/dist/discovery/ucp/intersect.js.map +1 -0
  303. package/dist/discovery/ucp/resolver.d.ts +62 -0
  304. package/dist/discovery/ucp/resolver.js +214 -0
  305. package/dist/discovery/ucp/resolver.js.map +1 -0
  306. package/dist/discovery/ucp/types.d.ts +97 -0
  307. package/dist/discovery/ucp/types.js +103 -0
  308. package/dist/discovery/ucp/types.js.map +1 -0
  309. package/dist/discovery/ucp/validate.d.ts +38 -0
  310. package/dist/discovery/ucp/validate.js +232 -0
  311. package/dist/discovery/ucp/validate.js.map +1 -0
  312. package/dist/discovery/ucp/vendor.d.ts +60 -0
  313. package/dist/discovery/ucp/vendor.js +61 -0
  314. package/dist/discovery/ucp/vendor.js.map +1 -0
  315. package/dist/doctor.d.ts +26 -0
  316. package/dist/doctor.js +113 -0
  317. package/dist/doctor.js.map +1 -0
  318. package/dist/exit-codes.d.ts +26 -0
  319. package/dist/exit-codes.js +27 -0
  320. package/dist/exit-codes.js.map +1 -0
  321. package/dist/fanout/disclosure.d.ts +138 -0
  322. package/dist/fanout/disclosure.js +188 -0
  323. package/dist/fanout/disclosure.js.map +1 -0
  324. package/dist/fanout/index.d.ts +32 -0
  325. package/dist/fanout/index.js +30 -0
  326. package/dist/fanout/index.js.map +1 -0
  327. package/dist/fanout/orchestrator.d.ts +153 -0
  328. package/dist/fanout/orchestrator.js +439 -0
  329. package/dist/fanout/orchestrator.js.map +1 -0
  330. package/dist/fanout/policy.d.ts +128 -0
  331. package/dist/fanout/policy.js +130 -0
  332. package/dist/fanout/policy.js.map +1 -0
  333. package/dist/handoff/acp-commerce.d.ts +58 -0
  334. package/dist/handoff/acp-commerce.js +69 -0
  335. package/dist/handoff/acp-commerce.js.map +1 -0
  336. package/dist/handoff/authorization.d.ts +127 -0
  337. package/dist/handoff/authorization.js +107 -0
  338. package/dist/handoff/authorization.js.map +1 -0
  339. package/dist/handoff/candidate.d.ts +94 -0
  340. package/dist/handoff/candidate.js +172 -0
  341. package/dist/handoff/candidate.js.map +1 -0
  342. package/dist/handoff/channel.d.ts +88 -0
  343. package/dist/handoff/channel.js +189 -0
  344. package/dist/handoff/channel.js.map +1 -0
  345. package/dist/handoff/completion.d.ts +44 -0
  346. package/dist/handoff/completion.js +136 -0
  347. package/dist/handoff/completion.js.map +1 -0
  348. package/dist/handoff/delivery.d.ts +59 -0
  349. package/dist/handoff/delivery.js +185 -0
  350. package/dist/handoff/delivery.js.map +1 -0
  351. package/dist/handoff/destination.d.ts +39 -0
  352. package/dist/handoff/destination.js +99 -0
  353. package/dist/handoff/destination.js.map +1 -0
  354. package/dist/handoff/errors.d.ts +34 -0
  355. package/dist/handoff/errors.js +47 -0
  356. package/dist/handoff/errors.js.map +1 -0
  357. package/dist/handoff/idempotency.d.ts +51 -0
  358. package/dist/handoff/idempotency.js +168 -0
  359. package/dist/handoff/idempotency.js.map +1 -0
  360. package/dist/handoff/index.d.ts +46 -0
  361. package/dist/handoff/index.js +48 -0
  362. package/dist/handoff/index.js.map +1 -0
  363. package/dist/handoff/ledger.d.ts +94 -0
  364. package/dist/handoff/ledger.js +195 -0
  365. package/dist/handoff/ledger.js.map +1 -0
  366. package/dist/handoff/lifecycle.d.ts +29 -0
  367. package/dist/handoff/lifecycle.js +102 -0
  368. package/dist/handoff/lifecycle.js.map +1 -0
  369. package/dist/handoff/metrics.d.ts +56 -0
  370. package/dist/handoff/metrics.js +78 -0
  371. package/dist/handoff/metrics.js.map +1 -0
  372. package/dist/handoff/operator-approval.d.ts +143 -0
  373. package/dist/handoff/operator-approval.js +287 -0
  374. package/dist/handoff/operator-approval.js.map +1 -0
  375. package/dist/handoff/order-record.d.ts +104 -0
  376. package/dist/handoff/order-record.js +235 -0
  377. package/dist/handoff/order-record.js.map +1 -0
  378. package/dist/handoff/package.d.ts +102 -0
  379. package/dist/handoff/package.js +132 -0
  380. package/dist/handoff/package.js.map +1 -0
  381. package/dist/handoff/transaction.d.ts +96 -0
  382. package/dist/handoff/transaction.js +263 -0
  383. package/dist/handoff/transaction.js.map +1 -0
  384. package/dist/handoff/ucp-checkout/cart-channel.d.ts +111 -0
  385. package/dist/handoff/ucp-checkout/cart-channel.js +370 -0
  386. package/dist/handoff/ucp-checkout/cart-channel.js.map +1 -0
  387. package/dist/handoff/ucp-checkout/cart-parse.d.ts +22 -0
  388. package/dist/handoff/ucp-checkout/cart-parse.js +102 -0
  389. package/dist/handoff/ucp-checkout/cart-parse.js.map +1 -0
  390. package/dist/handoff/ucp-checkout/cart-types.d.ts +93 -0
  391. package/dist/handoff/ucp-checkout/cart-types.js +38 -0
  392. package/dist/handoff/ucp-checkout/cart-types.js.map +1 -0
  393. package/dist/handoff/ucp-checkout/channel.d.ts +159 -0
  394. package/dist/handoff/ucp-checkout/channel.js +636 -0
  395. package/dist/handoff/ucp-checkout/channel.js.map +1 -0
  396. package/dist/handoff/ucp-checkout/client.d.ts +88 -0
  397. package/dist/handoff/ucp-checkout/client.js +228 -0
  398. package/dist/handoff/ucp-checkout/client.js.map +1 -0
  399. package/dist/handoff/ucp-checkout/endpoint.d.ts +36 -0
  400. package/dist/handoff/ucp-checkout/endpoint.js +145 -0
  401. package/dist/handoff/ucp-checkout/endpoint.js.map +1 -0
  402. package/dist/handoff/ucp-checkout/index.d.ts +35 -0
  403. package/dist/handoff/ucp-checkout/index.js +36 -0
  404. package/dist/handoff/ucp-checkout/index.js.map +1 -0
  405. package/dist/handoff/ucp-checkout/parse.d.ts +71 -0
  406. package/dist/handoff/ucp-checkout/parse.js +349 -0
  407. package/dist/handoff/ucp-checkout/parse.js.map +1 -0
  408. package/dist/handoff/ucp-checkout/totals.d.ts +41 -0
  409. package/dist/handoff/ucp-checkout/totals.js +36 -0
  410. package/dist/handoff/ucp-checkout/totals.js.map +1 -0
  411. package/dist/handoff/ucp-checkout/types.d.ts +149 -0
  412. package/dist/handoff/ucp-checkout/types.js +58 -0
  413. package/dist/handoff/ucp-checkout/types.js.map +1 -0
  414. package/dist/handoff/url-safety.d.ts +44 -0
  415. package/dist/handoff/url-safety.js +121 -0
  416. package/dist/handoff/url-safety.js.map +1 -0
  417. package/dist/i18n.d.ts +24 -0
  418. package/dist/i18n.js +27 -0
  419. package/dist/i18n.js.map +1 -0
  420. package/dist/negotiation/action-candidate.d.ts +97 -0
  421. package/dist/negotiation/action-candidate.js +160 -0
  422. package/dist/negotiation/action-candidate.js.map +1 -0
  423. package/dist/negotiation/condition/evaluator.d.ts +36 -0
  424. package/dist/negotiation/condition/evaluator.js +123 -0
  425. package/dist/negotiation/condition/evaluator.js.map +1 -0
  426. package/dist/negotiation/context-map/index.d.ts +23 -0
  427. package/dist/negotiation/context-map/index.js +22 -0
  428. package/dist/negotiation/context-map/index.js.map +1 -0
  429. package/dist/negotiation/context-map/store.d.ts +51 -0
  430. package/dist/negotiation/context-map/store.js +186 -0
  431. package/dist/negotiation/context-map/store.js.map +1 -0
  432. package/dist/negotiation/context-map/types.d.ts +29 -0
  433. package/dist/negotiation/context-map/types.js +57 -0
  434. package/dist/negotiation/context-map/types.js.map +1 -0
  435. package/dist/negotiation/domain/common.d.ts +95 -0
  436. package/dist/negotiation/domain/common.js +216 -0
  437. package/dist/negotiation/domain/common.js.map +1 -0
  438. package/dist/negotiation/domain/envelope.d.ts +55 -0
  439. package/dist/negotiation/domain/envelope.js +110 -0
  440. package/dist/negotiation/domain/envelope.js.map +1 -0
  441. package/dist/negotiation/domain/identifiers.d.ts +65 -0
  442. package/dist/negotiation/domain/identifiers.js +130 -0
  443. package/dist/negotiation/domain/identifiers.js.map +1 -0
  444. package/dist/negotiation/domain/objects.d.ts +149 -0
  445. package/dist/negotiation/domain/objects.js +335 -0
  446. package/dist/negotiation/domain/objects.js.map +1 -0
  447. package/dist/negotiation/idempotency/index.d.ts +26 -0
  448. package/dist/negotiation/idempotency/index.js +25 -0
  449. package/dist/negotiation/idempotency/index.js.map +1 -0
  450. package/dist/negotiation/idempotency/store.d.ts +52 -0
  451. package/dist/negotiation/idempotency/store.js +189 -0
  452. package/dist/negotiation/idempotency/store.js.map +1 -0
  453. package/dist/negotiation/idempotency/types.d.ts +102 -0
  454. package/dist/negotiation/idempotency/types.js +47 -0
  455. package/dist/negotiation/idempotency/types.js.map +1 -0
  456. package/dist/negotiation/jcs.d.ts +21 -0
  457. package/dist/negotiation/jcs.js +114 -0
  458. package/dist/negotiation/jcs.js.map +1 -0
  459. package/dist/negotiation/ledger/event.d.ts +142 -0
  460. package/dist/negotiation/ledger/event.js +206 -0
  461. package/dist/negotiation/ledger/event.js.map +1 -0
  462. package/dist/negotiation/ledger/index.d.ts +26 -0
  463. package/dist/negotiation/ledger/index.js +25 -0
  464. package/dist/negotiation/ledger/index.js.map +1 -0
  465. package/dist/negotiation/ledger/store.d.ts +97 -0
  466. package/dist/negotiation/ledger/store.js +338 -0
  467. package/dist/negotiation/ledger/store.js.map +1 -0
  468. package/dist/negotiation/prompt.d.ts +34 -0
  469. package/dist/negotiation/prompt.js +113 -0
  470. package/dist/negotiation/prompt.js.map +1 -0
  471. package/dist/negotiation/recovery/index.d.ts +26 -0
  472. package/dist/negotiation/recovery/index.js +24 -0
  473. package/dist/negotiation/recovery/index.js.map +1 -0
  474. package/dist/negotiation/recovery/outbound.d.ts +49 -0
  475. package/dist/negotiation/recovery/outbound.js +34 -0
  476. package/dist/negotiation/recovery/outbound.js.map +1 -0
  477. package/dist/negotiation/recovery/recover.d.ts +59 -0
  478. package/dist/negotiation/recovery/recover.js +486 -0
  479. package/dist/negotiation/recovery/recover.js.map +1 -0
  480. package/dist/negotiation/recovery/types.d.ts +85 -0
  481. package/dist/negotiation/recovery/types.js +17 -0
  482. package/dist/negotiation/recovery/types.js.map +1 -0
  483. package/dist/negotiation/state/phase.d.ts +70 -0
  484. package/dist/negotiation/state/phase.js +200 -0
  485. package/dist/negotiation/state/phase.js.map +1 -0
  486. package/dist/negotiation/types.d.ts +139 -0
  487. package/dist/negotiation/types.js +24 -0
  488. package/dist/negotiation/types.js.map +1 -0
  489. package/dist/net/safe-http.d.ts +54 -0
  490. package/dist/net/safe-http.js +116 -0
  491. package/dist/net/safe-http.js.map +1 -0
  492. package/dist/operator/controller.d.ts +181 -0
  493. package/dist/operator/controller.js +784 -0
  494. package/dist/operator/controller.js.map +1 -0
  495. package/dist/operator/runner.d.ts +128 -0
  496. package/dist/operator/runner.js +248 -0
  497. package/dist/operator/runner.js.map +1 -0
  498. package/dist/operator/store.d.ts +39 -0
  499. package/dist/operator/store.js +169 -0
  500. package/dist/operator/store.js.map +1 -0
  501. package/dist/operator/strategy.d.ts +54 -0
  502. package/dist/operator/strategy.js +249 -0
  503. package/dist/operator/strategy.js.map +1 -0
  504. package/dist/operator/tui.d.ts +27 -0
  505. package/dist/operator/tui.js +395 -0
  506. package/dist/operator/tui.js.map +1 -0
  507. package/dist/operator/types.d.ts +224 -0
  508. package/dist/operator/types.js +17 -0
  509. package/dist/operator/types.js.map +1 -0
  510. package/dist/product-buyer.d.ts +74 -0
  511. package/dist/product-buyer.js +161 -0
  512. package/dist/product-buyer.js.map +1 -0
  513. package/dist/product-cli.d.ts +56 -0
  514. package/dist/product-cli.js +175 -0
  515. package/dist/product-cli.js.map +1 -0
  516. package/dist/product-compat.d.ts +50 -0
  517. package/dist/product-compat.js +63 -0
  518. package/dist/product-compat.js.map +1 -0
  519. package/dist/product-init.d.ts +73 -0
  520. package/dist/product-init.js +203 -0
  521. package/dist/product-init.js.map +1 -0
  522. package/dist/product-publish.d.ts +85 -0
  523. package/dist/product-publish.js +216 -0
  524. package/dist/product-publish.js.map +1 -0
  525. package/dist/protocol/legacy-shopping-negotiation/adapter.d.ts +42 -0
  526. package/dist/protocol/legacy-shopping-negotiation/adapter.js +561 -0
  527. package/dist/protocol/legacy-shopping-negotiation/adapter.js.map +1 -0
  528. package/dist/protocol/legacy-shopping-negotiation/mapping.d.ts +78 -0
  529. package/dist/protocol/legacy-shopping-negotiation/mapping.js +321 -0
  530. package/dist/protocol/legacy-shopping-negotiation/mapping.js.map +1 -0
  531. package/dist/protocol/legacy-shopping-negotiation/money.d.ts +49 -0
  532. package/dist/protocol/legacy-shopping-negotiation/money.js +70 -0
  533. package/dist/protocol/legacy-shopping-negotiation/money.js.map +1 -0
  534. package/dist/protocol/legacy-shopping-negotiation/types.d.ts +107 -0
  535. package/dist/protocol/legacy-shopping-negotiation/types.js +34 -0
  536. package/dist/protocol/legacy-shopping-negotiation/types.js.map +1 -0
  537. package/dist/runtime/buyer-policy.d.ts +58 -0
  538. package/dist/runtime/buyer-policy.js +89 -0
  539. package/dist/runtime/buyer-policy.js.map +1 -0
  540. package/dist/runtime/fake-model.d.ts +77 -0
  541. package/dist/runtime/fake-model.js +307 -0
  542. package/dist/runtime/fake-model.js.map +1 -0
  543. package/dist/runtime/foreground.d.ts +56 -0
  544. package/dist/runtime/foreground.js +97 -0
  545. package/dist/runtime/foreground.js.map +1 -0
  546. package/dist/runtime/heartbeat.d.ts +36 -0
  547. package/dist/runtime/heartbeat.js +49 -0
  548. package/dist/runtime/heartbeat.js.map +1 -0
  549. package/dist/runtime/merchant-turn.d.ts +35 -0
  550. package/dist/runtime/merchant-turn.js +20 -0
  551. package/dist/runtime/merchant-turn.js.map +1 -0
  552. package/dist/runtime/model.d.ts +40 -0
  553. package/dist/runtime/model.js +86 -0
  554. package/dist/runtime/model.js.map +1 -0
  555. package/dist/runtime/negotiation-turn.d.ts +101 -0
  556. package/dist/runtime/negotiation-turn.js +317 -0
  557. package/dist/runtime/negotiation-turn.js.map +1 -0
  558. package/dist/runtime/tools.d.ts +64 -0
  559. package/dist/runtime/tools.js +236 -0
  560. package/dist/runtime/tools.js.map +1 -0
  561. package/dist/supervisor/init.d.ts +31 -0
  562. package/dist/supervisor/init.js +222 -0
  563. package/dist/supervisor/init.js.map +1 -0
  564. package/dist/supervisor/logs.d.ts +28 -0
  565. package/dist/supervisor/logs.js +101 -0
  566. package/dist/supervisor/logs.js.map +1 -0
  567. package/dist/supervisor/manage.d.ts +72 -0
  568. package/dist/supervisor/manage.js +419 -0
  569. package/dist/supervisor/manage.js.map +1 -0
  570. package/dist/supervisor/manifest.d.ts +69 -0
  571. package/dist/supervisor/manifest.js +209 -0
  572. package/dist/supervisor/manifest.js.map +1 -0
  573. package/dist/supervisor/stack-config.d.ts +79 -0
  574. package/dist/supervisor/stack-config.js +262 -0
  575. package/dist/supervisor/stack-config.js.map +1 -0
  576. package/dist/supervisor/wrapper.d.ts +17 -0
  577. package/dist/supervisor/wrapper.js +138 -0
  578. package/dist/supervisor/wrapper.js.map +1 -0
  579. package/dist/trust/identity/auth-verifier.d.ts +96 -0
  580. package/dist/trust/identity/auth-verifier.js +166 -0
  581. package/dist/trust/identity/auth-verifier.js.map +1 -0
  582. package/dist/trust/identity/identity-binding.d.ts +63 -0
  583. package/dist/trust/identity/identity-binding.js +59 -0
  584. package/dist/trust/identity/identity-binding.js.map +1 -0
  585. package/dist/trust/identity/index.d.ts +38 -0
  586. package/dist/trust/identity/index.js +31 -0
  587. package/dist/trust/identity/index.js.map +1 -0
  588. package/dist/trust/identity/jws.d.ts +64 -0
  589. package/dist/trust/identity/jws.js +178 -0
  590. package/dist/trust/identity/jws.js.map +1 -0
  591. package/dist/trust/identity/keys.d.ts +71 -0
  592. package/dist/trust/identity/keys.js +118 -0
  593. package/dist/trust/identity/keys.js.map +1 -0
  594. package/dist/trust/identity/message-signature.d.ts +105 -0
  595. package/dist/trust/identity/message-signature.js +297 -0
  596. package/dist/trust/identity/message-signature.js.map +1 -0
  597. package/dist/trust/identity/nonce.d.ts +45 -0
  598. package/dist/trust/identity/nonce.js +50 -0
  599. package/dist/trust/identity/nonce.js.map +1 -0
  600. package/dist/trust/identity/signature-base.d.ts +84 -0
  601. package/dist/trust/identity/signature-base.js +261 -0
  602. package/dist/trust/identity/signature-base.js.map +1 -0
  603. package/dist/trust/identity/trust-policy.d.ts +92 -0
  604. package/dist/trust/identity/trust-policy.js +95 -0
  605. package/dist/trust/identity/trust-policy.js.map +1 -0
  606. package/dist/trust/index.d.ts +21 -0
  607. package/dist/trust/index.js +22 -0
  608. package/dist/trust/index.js.map +1 -0
  609. package/dist/trust/records/evaluator.d.ts +76 -0
  610. package/dist/trust/records/evaluator.js +122 -0
  611. package/dist/trust/records/evaluator.js.map +1 -0
  612. package/dist/trust/records/index.d.ts +28 -0
  613. package/dist/trust/records/index.js +26 -0
  614. package/dist/trust/records/index.js.map +1 -0
  615. package/dist/trust/records/store.d.ts +75 -0
  616. package/dist/trust/records/store.js +339 -0
  617. package/dist/trust/records/store.js.map +1 -0
  618. package/dist/trust/records/types.d.ts +159 -0
  619. package/dist/trust/records/types.js +40 -0
  620. package/dist/trust/records/types.js.map +1 -0
  621. package/dist/tui/banner.d.ts +46 -0
  622. package/dist/tui/banner.js +99 -0
  623. package/dist/tui/banner.js.map +1 -0
  624. package/dist/tui/styles.d.ts +70 -0
  625. package/dist/tui/styles.js +197 -0
  626. package/dist/tui/styles.js.map +1 -0
  627. package/dist/tui/theme.d.ts +63 -0
  628. package/dist/tui/theme.js +136 -0
  629. package/dist/tui/theme.js.map +1 -0
  630. package/examples/profiles/buyer.deepseek.yaml +34 -0
  631. package/examples/profiles/buyer.fake.yaml +36 -0
  632. package/examples/profiles/buyer.local.yaml +40 -0
  633. package/examples/profiles/merchant.deepseek.yaml +32 -0
  634. package/examples/profiles/merchant.fake.yaml +31 -0
  635. package/examples/profiles/merchant.local.yaml +34 -0
  636. package/package.json +55 -0
@@ -0,0 +1,317 @@
1
+ /**
2
+ * Copyright 2026 harrylabsj
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * Role-aware single-turn orchestration (buyer and merchant share one loop).
18
+ *
19
+ * Flow per design §10 (with M3 reliability):
20
+ * capabilities (fail closed) -> recover own stale claims -> list pending
21
+ * -> claim -> Pi agent loop under claim heartbeat (snapshot -> decision)
22
+ * -> policy result -> complete / fail / abandon.
23
+ *
24
+ * The marketplace conversation is the authoritative memory; no Pi session is
25
+ * persisted. Every turn rebuilds its context from the snapshot.
26
+ *
27
+ * Shutdown semantics (design §16.2): an external AbortSignal (SIGINT/SIGTERM
28
+ * in the foreground loop) aborts the Pi run; a claim that was never accepted
29
+ * is abandoned (never completed) so another worker can pick it up. A
30
+ * decision already accepted by the policy gate is never rolled back — the
31
+ * claim completes normally.
32
+ */
33
+ import { Agent } from "@earendil-works/pi-agent-core";
34
+ import { CommerceError, idempotencyKey } from "../commerce/types.js";
35
+ import { PROTOCOL_VERSION } from "../negotiation/types.js";
36
+ import { systemPromptForRole, userPromptForRole } from "../negotiation/prompt.js";
37
+ import { buildNegotiationTools, createToolGuard, toolAllowlistForRole, } from "./tools.js";
38
+ import { buildModel, resolveThinkingLevel } from "./model.js";
39
+ import { startClaimHeartbeat } from "./heartbeat.js";
40
+ const REQUIRED_CAPABILITIES = [
41
+ "consultation_read",
42
+ "consultation_write",
43
+ "inventory_read",
44
+ ];
45
+ /**
46
+ * M3 reliability constants (documented in README): claims untouched for
47
+ * STALE_CLAIM_TTL_SECONDS are recovered as crashed; a claimed turn
48
+ * heartbeats every HEARTBEAT_INTERVAL_MS — comfortably below the TTL — so
49
+ * healthy long turns are never mistaken for crashed ones.
50
+ */
51
+ export const STALE_CLAIM_TTL_SECONDS = 300;
52
+ export const HEARTBEAT_INTERVAL_MS = 60_000;
53
+ export async function runNegotiationTurn(options) {
54
+ const { profile, client, streamFn } = options;
55
+ const usage = { input: 0, output: 0, total: 0, cost_total: 0 };
56
+ // 0. Protocol + capability check: fail closed.
57
+ const caps = await client.getCapabilities();
58
+ if (!caps.protocol_versions.includes(PROTOCOL_VERSION)) {
59
+ throw new CommerceError("validation", `Gateway does not support ${PROTOCOL_VERSION}: ${caps.protocol_versions.join(", ")}`);
60
+ }
61
+ for (const cap of REQUIRED_CAPABILITIES) {
62
+ if (!caps.capabilities[cap]) {
63
+ throw new CommerceError("validation", `Gateway lacks required capability: ${cap}`);
64
+ }
65
+ }
66
+ if (caps.capabilities.orders !== false) {
67
+ throw new CommerceError("validation", "Gateway violates the no-order boundary (orders=true)");
68
+ }
69
+ // 0.5. Crash recovery BEFORE any work or heartbeat on this runtime:
70
+ // abandon this identity's own stale processing claims (never revives them;
71
+ // abandoned claims stay reclaimable). Runs in once and foreground modes.
72
+ await client.abandonStaleClaims({ ttl_seconds: STALE_CLAIM_TTL_SECONDS });
73
+ // 1. Find work. Role and owner are derived from the token server-side.
74
+ const pending = await client.listPendingMessages();
75
+ const target = pending[0];
76
+ if (!target) {
77
+ return { outcome: { kind: "no_work" }, steps: 0, usage };
78
+ }
79
+ // 2. Claim. Idempotency key: agent_id + message_id + protocol version.
80
+ const idem = idempotencyKey(profile.agent_id, target.message_id, PROTOCOL_VERSION);
81
+ const claim = await client.claimMessage({
82
+ conversation_id: target.conversation_id,
83
+ message_id: target.message_id,
84
+ idempotency_key: idem,
85
+ });
86
+ const base = {
87
+ conversation_id: target.conversation_id,
88
+ message_id: target.message_id,
89
+ idempotency_key: idem,
90
+ };
91
+ if (!claim.claimed) {
92
+ return {
93
+ outcome: { kind: "already_claimed", status: claim.status },
94
+ ...base,
95
+ steps: 0,
96
+ usage,
97
+ };
98
+ }
99
+ // 3. Run the constrained Pi agent loop under a claim heartbeat: a healthy
100
+ // long turn refreshes the claim so it is never mistaken for a crashed one.
101
+ const heartbeat = startClaimHeartbeat(client, target.message_id, options.heartbeatIntervalMs ?? HEARTBEAT_INTERVAL_MS);
102
+ let report;
103
+ try {
104
+ report = await finishTurn(options, profile, client, streamFn, target, idem, base, usage);
105
+ }
106
+ catch (err) {
107
+ // An error escaping a claimed turn (transient snapshot/submit/settle
108
+ // failures) must not strand the claim until the 300s stale TTL:
109
+ // best-effort abandon so the message is reclaimable immediately. The
110
+ // stale TTL stays the backstop if this abandon fails too.
111
+ try {
112
+ await client.abandonClaim({
113
+ message_id: target.message_id,
114
+ idempotency_key: idem,
115
+ error: `turn escaped settlement: ${err instanceof Error ? err.message : String(err)}`,
116
+ });
117
+ }
118
+ catch {
119
+ // The original error is what propagates.
120
+ }
121
+ throw err;
122
+ }
123
+ finally {
124
+ // No timer or in-flight heartbeat outlives the turn.
125
+ await heartbeat.stop();
126
+ }
127
+ return { ...report, heartbeat: { beats: heartbeat.beats(), failures: heartbeat.failures() } };
128
+ }
129
+ async function finishTurn(options, profile, client, streamFn, target, idem, base, usage) {
130
+ const tracker = { submissions: 0 };
131
+ const binding = {
132
+ conversation_id: target.conversation_id,
133
+ message_id: target.message_id,
134
+ idempotency_key: idem,
135
+ };
136
+ // max_retries = repair attempts beyond the first submission.
137
+ const maxSubmissions = profile.runtime.max_retries + 1;
138
+ const tools = buildNegotiationTools(client, binding, tracker, maxSubmissions, {
139
+ ...(profile.role === "buyer" && profile.buyer_policy
140
+ ? { buyerPolicy: profile.buyer_policy }
141
+ : {}),
142
+ });
143
+ const guard = createToolGuard(toolAllowlistForRole(profile.role), binding);
144
+ const model = buildModel(profile);
145
+ const thinkingLevel = resolveThinkingLevel(profile);
146
+ let steps = 0;
147
+ let stepCapHit = false;
148
+ const maxSteps = profile.runtime.max_model_steps;
149
+ const agent = new Agent({
150
+ streamFn,
151
+ initialState: {
152
+ systemPrompt: systemPromptForRole(profile),
153
+ model,
154
+ tools,
155
+ ...(thinkingLevel !== undefined ? { thinkingLevel } : {}),
156
+ },
157
+ convertToLlm: (messages) => messages,
158
+ toolExecution: "sequential",
159
+ beforeToolCall: guard,
160
+ ...(options.getApiKey !== undefined ? { getApiKey: options.getApiKey } : {}),
161
+ });
162
+ agent.subscribe((event) => {
163
+ if (event.type === "turn_end") {
164
+ steps += 1;
165
+ if (steps >= maxSteps) {
166
+ stepCapHit = true;
167
+ agent.abort();
168
+ }
169
+ }
170
+ if (event.type === "message_end" && event.message.role === "assistant") {
171
+ const u = event.message.usage;
172
+ usage.input += u.input;
173
+ usage.output += u.output;
174
+ usage.total += u.totalTokens;
175
+ usage.cost_total += u.cost.total;
176
+ }
177
+ return undefined;
178
+ });
179
+ const timeoutMs = profile.runtime.turn_timeout_seconds * 1000;
180
+ let timedOut = false;
181
+ let externalAbort = options.signal?.aborted === true;
182
+ const onExternalAbort = () => {
183
+ externalAbort = true;
184
+ agent.abort();
185
+ };
186
+ options.signal?.addEventListener("abort", onExternalAbort, { once: true });
187
+ const timer = setTimeout(() => {
188
+ timedOut = true;
189
+ agent.abort();
190
+ }, timeoutMs);
191
+ let runError;
192
+ try {
193
+ await agent.prompt(userPromptForRole(profile.role, target.conversation_id, target.message_id));
194
+ await agent.waitForIdle();
195
+ if (agent.state.errorMessage) {
196
+ runError = agent.state.errorMessage;
197
+ }
198
+ }
199
+ catch (err) {
200
+ runError = err instanceof Error ? err.message : String(err);
201
+ }
202
+ finally {
203
+ clearTimeout(timer);
204
+ options.signal?.removeEventListener("abort", onExternalAbort);
205
+ }
206
+ // 4. Settle the claim based on the policy result. A decision the gate
207
+ // already accepted is never rolled back, even if a shutdown arrived
208
+ // afterwards.
209
+ const result = tracker.decisionResult;
210
+ if (result) {
211
+ if (result.result === "accepted") {
212
+ await client.completeClaim({ message_id: target.message_id, idempotency_key: idem });
213
+ const outcome = { kind: "accepted" };
214
+ if (result.message_id !== undefined)
215
+ outcome.message_id = result.message_id;
216
+ return { outcome, ...base, policy_result: result, steps, usage };
217
+ }
218
+ if (result.result === "human_required") {
219
+ // The turn did its job by escalating; the conversation now routes to a
220
+ // human. Completing the claim avoids an infinite retry loop.
221
+ await client.completeClaim({ message_id: target.message_id, idempotency_key: idem });
222
+ return {
223
+ outcome: { kind: "human_required", reason_codes: result.reason_codes },
224
+ ...base,
225
+ policy_result: result,
226
+ steps,
227
+ usage,
228
+ };
229
+ }
230
+ // rejected_retryable with no successful resubmission inside the turn.
231
+ if (externalAbort) {
232
+ // Shutdown semantics (design §16.2): a claim that was never accepted
233
+ // is abandoned, never failed — both are reclaimable, but abandon is
234
+ // the documented "unsettled on shutdown" settlement.
235
+ const reason = "shutdown: turn aborted by signal before a decision was accepted";
236
+ await client.abandonClaim({
237
+ message_id: target.message_id,
238
+ idempotency_key: idem,
239
+ error: reason,
240
+ });
241
+ return {
242
+ outcome: { kind: "aborted", reason },
243
+ ...base,
244
+ policy_result: result,
245
+ steps,
246
+ usage,
247
+ };
248
+ }
249
+ const budgetNote = tracker.submissionLimitExceeded
250
+ ? `; submission budget exhausted (max_retries=${profile.runtime.max_retries})`
251
+ : "";
252
+ await client.failClaim({
253
+ message_id: target.message_id,
254
+ idempotency_key: idem,
255
+ error: `policy rejected: ${result.public_reason}${budgetNote}`,
256
+ });
257
+ return {
258
+ outcome: {
259
+ kind: "failed",
260
+ error: `policy rejected: ${result.public_reason}${budgetNote}`,
261
+ retriable: true,
262
+ },
263
+ ...base,
264
+ policy_result: result,
265
+ steps,
266
+ usage,
267
+ };
268
+ }
269
+ // 5. No decision submitted. External shutdown abandons the claim (another
270
+ // worker may retry it); timeout/step-cap/model errors fail it. Neither is
271
+ // ever completed.
272
+ let outcome;
273
+ let reason;
274
+ if (externalAbort) {
275
+ reason = "shutdown: turn aborted by signal before a decision was accepted";
276
+ outcome = { kind: "aborted", reason };
277
+ await client.abandonClaim({
278
+ message_id: target.message_id,
279
+ idempotency_key: idem,
280
+ error: reason,
281
+ });
282
+ return { outcome, ...base, steps, usage };
283
+ }
284
+ if (timedOut) {
285
+ reason = `turn timed out after ${profile.runtime.turn_timeout_seconds}s without a decision`;
286
+ outcome = { kind: "timeout", reason };
287
+ }
288
+ else if (stepCapHit) {
289
+ reason = `model did not submit a decision within ${maxSteps} steps`;
290
+ outcome = { kind: "no_decision", reason };
291
+ }
292
+ else if (runError !== undefined && runError !== "") {
293
+ reason = `model error: ${runError}`;
294
+ outcome = { kind: "failed", error: reason, retriable: !isModelConfigError(runError) };
295
+ }
296
+ else {
297
+ reason = "model ended without submitting a decision";
298
+ outcome = { kind: "no_decision", reason };
299
+ }
300
+ await client.failClaim({
301
+ message_id: target.message_id,
302
+ idempotency_key: idem,
303
+ error: reason,
304
+ });
305
+ return { outcome, ...base, steps, usage };
306
+ }
307
+ /** Model auth/config problems are not retried by an external supervisor. */
308
+ function isModelConfigError(message) {
309
+ const m = message.toLowerCase();
310
+ return (m.includes("api key") ||
311
+ m.includes("unauthorized") ||
312
+ m.includes("401") ||
313
+ m.includes("403") ||
314
+ m.includes("forbidden") ||
315
+ m.includes("quota"));
316
+ }
317
+ //# sourceMappingURL=negotiation-turn.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"negotiation-turn.js","sourceRoot":"","sources":["../../src/runtime/negotiation-turn.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAE,KAAK,EAAkC,MAAM,+BAA+B,CAAC;AAItF,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACrE,OAAO,EAAE,gBAAgB,EAAqB,MAAM,yBAAyB,CAAC;AAC9E,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAClF,OAAO,EACL,qBAAqB,EACrB,eAAe,EACf,oBAAoB,GAErB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,UAAU,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAC9D,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AA2CrD,MAAM,qBAAqB,GAAG;IAC5B,mBAAmB;IACnB,oBAAoB;IACpB,gBAAgB;CACR,CAAC;AAEX;;;;;GAKG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,GAAG,CAAC;AAC3C,MAAM,CAAC,MAAM,qBAAqB,GAAG,MAAM,CAAC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AAe;IACzC,MAAM,CAAC,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAChC,OAAO,CACL,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC;QACrB,CAAC,CAAC,QAAQ,CAAC,cAAc,CAAC;QAC1B,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC;QACjB,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC;QACjB,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC;QACvB,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CACpB,CAAC;AACJ,CAAC"}
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Copyright 2026 harrylabsj
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import type { AgentTool, BeforeToolCallContext, BeforeToolCallResult } from "@earendil-works/pi-agent-core";
17
+ import type { TSchema } from "@earendil-works/pi-ai";
18
+ import type { BuyerPolicy } from "../config/profile.js";
19
+ import { type PolicyResult, type Role } from "../negotiation/types.js";
20
+ import type { CommerceClient } from "../commerce/types.js";
21
+ export declare const TOOL_GET_SNAPSHOT = "get_negotiation_snapshot";
22
+ export declare const TOOL_SUBMIT_DECISION = "submit_negotiation_decision";
23
+ /** Both roles share the same minimal surface: snapshot read + decision write. */
24
+ export declare const NEGOTIATION_TOOL_ALLOWLIST: readonly string[];
25
+ /** Back-compat alias for the M1 name. */
26
+ export declare const MERCHANT_TOOL_ALLOWLIST: readonly string[];
27
+ export declare function toolAllowlistForRole(role: Role): readonly string[];
28
+ /** Hard cap on tool argument size, checked before execution. */
29
+ export declare const MAX_TOOL_ARGS_BYTES: number;
30
+ /**
31
+ * Content-addressed submit idempotency: resubmitting byte-identical
32
+ * arguments replays the stored gateway result, while a repaired decision
33
+ * gets a fresh key. This survives process restarts without local state.
34
+ */
35
+ export declare function submitIdempotencyKey(claimKey: string, decision: unknown): string;
36
+ export interface ConversationBinding {
37
+ conversation_id: string;
38
+ message_id: number;
39
+ idempotency_key: string;
40
+ }
41
+ export interface ToolOutcomeTracker {
42
+ /** The latest policy result returned by the gateway, if any. */
43
+ decisionResult?: PolicyResult;
44
+ /** Number of submissions that reached the gateway (first + repairs). */
45
+ submissions: number;
46
+ /** Set when a submission was blocked by the max_retries budget. */
47
+ submissionLimitExceeded?: boolean;
48
+ }
49
+ export interface NegotiationToolOptions {
50
+ /**
51
+ * Buyer profiles only: the local private policy gate evaluated BEFORE the
52
+ * gateway is called. Private thresholds never leave this process.
53
+ */
54
+ buyerPolicy?: BuyerPolicy;
55
+ }
56
+ export declare function buildNegotiationTools(client: CommerceClient, binding: ConversationBinding, tracker: ToolOutcomeTracker, maxSubmissions: number, options?: NegotiationToolOptions): AgentTool<TSchema, unknown>[];
57
+ /** Back-compat alias: merchant tools are the negotiation tools without a buyer gate. */
58
+ export declare function buildMerchantTools(client: CommerceClient, binding: ConversationBinding, tracker: ToolOutcomeTracker, maxSubmissions: number): AgentTool<TSchema, unknown>[];
59
+ /**
60
+ * beforeToolCall factory: allowlist enforcement, argument size cap, and
61
+ * conversation/message binding checks. Runs after Pi's own schema
62
+ * validation of the arguments.
63
+ */
64
+ export declare function createToolGuard(allowlist: readonly string[], binding: ConversationBinding): (context: BeforeToolCallContext) => Promise<BeforeToolCallResult | undefined>;
@@ -0,0 +1,236 @@
1
+ /**
2
+ * Copyright 2026 harrylabsj
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * Tool surface and beforeToolCall guard.
18
+ *
19
+ * The Pi agent for any role (buyer or merchant) sees exactly two tools:
20
+ * get_negotiation_snapshot (read-only, bound conversation) and
21
+ * submit_negotiation_decision (the single write intent, terminal).
22
+ * No file, shell, edit, arbitrary HTTP, search, or dynamic-install tools
23
+ * exist — nothing else is ever constructed, and the guard blocks anything
24
+ * unknown even if a model emits a forged call.
25
+ *
26
+ * For buyer profiles the submit tool additionally runs the LOCAL private
27
+ * policy gate (runtime/buyer-policy.ts) before the gateway is called, so
28
+ * the private budget never leaves this process.
29
+ *
30
+ * The guard is a last line of defense only; the authoritative policy gate
31
+ * remains the Commerce API.
32
+ */
33
+ import { createHash } from "node:crypto";
34
+ import { dereferenceSchema, loadSchema } from "../contracts/schemas.js";
35
+ import { PROTOCOL_VERSION, } from "../negotiation/types.js";
36
+ import { checkBuyerLocalPolicy, localBuyerPolicyResult } from "./buyer-policy.js";
37
+ export const TOOL_GET_SNAPSHOT = "get_negotiation_snapshot";
38
+ export const TOOL_SUBMIT_DECISION = "submit_negotiation_decision";
39
+ /** Both roles share the same minimal surface: snapshot read + decision write. */
40
+ export const NEGOTIATION_TOOL_ALLOWLIST = [
41
+ TOOL_GET_SNAPSHOT,
42
+ TOOL_SUBMIT_DECISION,
43
+ ];
44
+ /** Back-compat alias for the M1 name. */
45
+ export const MERCHANT_TOOL_ALLOWLIST = NEGOTIATION_TOOL_ALLOWLIST;
46
+ export function toolAllowlistForRole(role) {
47
+ // search_products is deliberately not exposed: conversations are bound to
48
+ // a single SKU by the marketplace, so both roles get the same two tools.
49
+ void role;
50
+ return NEGOTIATION_TOOL_ALLOWLIST;
51
+ }
52
+ /** Hard cap on tool argument size, checked before execution. */
53
+ export const MAX_TOOL_ARGS_BYTES = 32 * 1024;
54
+ /**
55
+ * Content-addressed submit idempotency: resubmitting byte-identical
56
+ * arguments replays the stored gateway result, while a repaired decision
57
+ * gets a fresh key. This survives process restarts without local state.
58
+ */
59
+ export function submitIdempotencyKey(claimKey, decision) {
60
+ const canonical = stableStringify(decision);
61
+ const hash = createHash("sha256").update(canonical).digest("hex").slice(0, 16);
62
+ return `${claimKey}:submit:${hash}`;
63
+ }
64
+ function stableStringify(value) {
65
+ if (Array.isArray(value))
66
+ return `[${value.map(stableStringify).join(",")}]`;
67
+ if (value !== null && typeof value === "object") {
68
+ const entries = Object.entries(value)
69
+ .filter(([, v]) => v !== undefined)
70
+ .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
71
+ .map(([k, v]) => `${JSON.stringify(k)}:${stableStringify(v)}`);
72
+ return `{${entries.join(",")}}`;
73
+ }
74
+ return JSON.stringify(value) ?? "null";
75
+ }
76
+ export function buildNegotiationTools(client, binding, tracker, maxSubmissions, options) {
77
+ const decisionParameters = dereferenceSchema(loadSchema("decision"));
78
+ const snapshotTool = {
79
+ name: TOOL_GET_SNAPSHOT,
80
+ label: "Get negotiation snapshot",
81
+ description: "Read the authoritative, role-trimmed negotiation snapshot for the currently claimed " +
82
+ "conversation. Takes no arguments; the conversation is bound by the runtime.",
83
+ parameters: {
84
+ type: "object",
85
+ additionalProperties: false,
86
+ properties: {},
87
+ },
88
+ execute: async () => {
89
+ const snapshot = await client.getNegotiationSnapshot({
90
+ conversation_id: binding.conversation_id,
91
+ message_id: binding.message_id,
92
+ });
93
+ return {
94
+ content: [{ type: "text", text: JSON.stringify(snapshot, null, 2) }],
95
+ details: snapshot,
96
+ };
97
+ },
98
+ };
99
+ const submitTool = {
100
+ name: TOOL_SUBMIT_DECISION,
101
+ label: "Submit negotiation decision",
102
+ description: "Submit the single structured negotiation decision for this turn. This is the only " +
103
+ "write intent and ends the turn when accepted. The marketplace policy gate is " +
104
+ "authoritative and may reject with a repairable reason.",
105
+ parameters: decisionParameters,
106
+ execute: async (_toolCallId, params) => {
107
+ // max_retries semantics: the number of repair attempts allowed beyond
108
+ // the first submission. Total gateway submissions per turn never exceed
109
+ // max_retries + 1; exceeding the budget is blocked here (the gateway is
110
+ // never called) and settles the turn as an auditable failure.
111
+ if (tracker.submissions >= maxSubmissions) {
112
+ tracker.submissionLimitExceeded = true;
113
+ return {
114
+ content: [
115
+ {
116
+ type: "text",
117
+ text: `Submission budget exhausted: profile runtime.max_retries allows ` +
118
+ `${maxSubmissions} submission(s) this turn (first attempt plus repairs). ` +
119
+ "The turn is ending; do not resubmit.",
120
+ },
121
+ ],
122
+ details: {
123
+ error: "submission_limit_exceeded",
124
+ submissions: tracker.submissions,
125
+ max_submissions: maxSubmissions,
126
+ },
127
+ terminate: true,
128
+ };
129
+ }
130
+ const decision = params;
131
+ // Buyer local private policy gate: evaluated before the gateway call.
132
+ // A violation never reaches the server and its reason never contains
133
+ // private threshold values.
134
+ if (options?.buyerPolicy) {
135
+ const violation = checkBuyerLocalPolicy(decision, options.buyerPolicy);
136
+ if (violation) {
137
+ tracker.submissions += 1;
138
+ const result = localBuyerPolicyResult(binding.conversation_id, violation, maxSubmissions - tracker.submissions);
139
+ tracker.decisionResult = result;
140
+ return {
141
+ content: [{ type: "text", text: JSON.stringify(result) }],
142
+ details: { ...result, local: true },
143
+ terminate: false,
144
+ };
145
+ }
146
+ }
147
+ const result = await client.submitNegotiationDecision({
148
+ decision,
149
+ idempotency_key: submitIdempotencyKey(binding.idempotency_key, decision),
150
+ });
151
+ tracker.decisionResult = result;
152
+ tracker.submissions += 1;
153
+ return {
154
+ content: [
155
+ {
156
+ type: "text",
157
+ text: JSON.stringify(result),
158
+ },
159
+ ],
160
+ details: result,
161
+ // Stop the loop after a terminal result; retryable rejections let the
162
+ // model repair and resubmit within the remaining step budget.
163
+ terminate: result.result !== "rejected_retryable",
164
+ };
165
+ },
166
+ };
167
+ return [snapshotTool, submitTool];
168
+ }
169
+ /** Back-compat alias: merchant tools are the negotiation tools without a buyer gate. */
170
+ export function buildMerchantTools(client, binding, tracker, maxSubmissions) {
171
+ return buildNegotiationTools(client, binding, tracker, maxSubmissions);
172
+ }
173
+ /**
174
+ * beforeToolCall factory: allowlist enforcement, argument size cap, and
175
+ * conversation/message binding checks. Runs after Pi's own schema
176
+ * validation of the arguments.
177
+ */
178
+ export function createToolGuard(allowlist, binding) {
179
+ return async (context) => {
180
+ const name = context.toolCall.name;
181
+ if (!allowlist.includes(name)) {
182
+ return {
183
+ block: true,
184
+ reason: `Tool "${name}" is not in the ${allowlist.join("/")} allowlist. ` +
185
+ "No file, shell, edit, HTTP, or install tools exist.",
186
+ };
187
+ }
188
+ const args = context.args;
189
+ let size;
190
+ try {
191
+ // UTF-8 byte length, not string length: multibyte characters count
192
+ // fully against the cap.
193
+ size = Buffer.byteLength(JSON.stringify(args ?? {}), "utf8");
194
+ }
195
+ catch {
196
+ return { block: true, reason: "Tool arguments are not serializable." };
197
+ }
198
+ if (size > MAX_TOOL_ARGS_BYTES) {
199
+ return { block: true, reason: `Tool arguments exceed the ${MAX_TOOL_ARGS_BYTES} byte cap.` };
200
+ }
201
+ if (name === TOOL_GET_SNAPSHOT) {
202
+ if (args !== null && typeof args === "object" && Object.keys(args).length > 0) {
203
+ return {
204
+ block: true,
205
+ reason: "get_negotiation_snapshot takes no arguments; the conversation is bound by the runtime.",
206
+ };
207
+ }
208
+ }
209
+ if (name === TOOL_SUBMIT_DECISION) {
210
+ const decision = args;
211
+ if (!decision || typeof decision !== "object") {
212
+ return { block: true, reason: "Decision arguments must be an object." };
213
+ }
214
+ if (decision.protocol_version !== PROTOCOL_VERSION) {
215
+ return {
216
+ block: true,
217
+ reason: `protocol_version must be ${PROTOCOL_VERSION}.`,
218
+ };
219
+ }
220
+ if (decision.conversation_id !== binding.conversation_id) {
221
+ return {
222
+ block: true,
223
+ reason: `conversation_id must be the claimed conversation ${binding.conversation_id}; cross-conversation access is not allowed.`,
224
+ };
225
+ }
226
+ if (decision.in_reply_to_message_id !== binding.message_id) {
227
+ return {
228
+ block: true,
229
+ reason: `in_reply_to_message_id must be the claimed message ${binding.message_id}.`,
230
+ };
231
+ }
232
+ }
233
+ return undefined;
234
+ };
235
+ }
236
+ //# sourceMappingURL=tools.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tools.js","sourceRoot":"","sources":["../../src/runtime/tools.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AASzC,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACxE,OAAO,EACL,gBAAgB,GAIjB,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAElF,MAAM,CAAC,MAAM,iBAAiB,GAAG,0BAA0B,CAAC;AAC5D,MAAM,CAAC,MAAM,oBAAoB,GAAG,6BAA6B,CAAC;AAElE,iFAAiF;AACjF,MAAM,CAAC,MAAM,0BAA0B,GAAsB;IAC3D,iBAAiB;IACjB,oBAAoB;CACrB,CAAC;AAEF,yCAAyC;AACzC,MAAM,CAAC,MAAM,uBAAuB,GAAsB,0BAA0B,CAAC;AAErF,MAAM,UAAU,oBAAoB,CAAC,IAAU;IAC7C,0EAA0E;IAC1E,yEAAyE;IACzE,KAAK,IAAI,CAAC;IACV,OAAO,0BAA0B,CAAC;AACpC,CAAC;AAED,gEAAgE;AAChE,MAAM,CAAC,MAAM,mBAAmB,GAAG,EAAE,GAAG,IAAI,CAAC;AAE7C;;;;GAIG;AACH,MAAM,UAAU,oBAAoB,CAAC,QAAgB,EAAE,QAAiB;IACtE,MAAM,SAAS,GAAG,eAAe,CAAC,QAAQ,CAAC,CAAC;IAC5C,MAAM,IAAI,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC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