literally-aeo 0.2.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (483) hide show
  1. package/README.md +183 -14
  2. package/dist/author-tools.js +386 -283
  3. package/dist/cli.mjs +19943 -16532
  4. package/dist/lib/access/schema.js +6 -6
  5. package/dist/lib/agents/author-schema.js +12 -12
  6. package/dist/lib/agents/author-validation.js +15 -15
  7. package/dist/lib/agents/copy-validation.js +18 -17
  8. package/dist/lib/agents/copywriter-input.js +16 -15
  9. package/dist/lib/agents/credentials.js +1 -1
  10. package/dist/lib/agents/facts.js +15 -14
  11. package/dist/lib/agents/methodology.js +56 -0
  12. package/dist/lib/agents/narrative-facts.js +13 -13
  13. package/dist/lib/agents/verifier.js +22 -20
  14. package/dist/lib/analysis/domain-classifier.js +21 -19
  15. package/dist/lib/analysis/domain-labels.js +12 -12
  16. package/dist/lib/analysis/outreach.js +2 -2
  17. package/dist/lib/analysis/progress-schema.js +6 -2
  18. package/dist/lib/analysis/progress.js +17 -16
  19. package/dist/lib/analysis/rate-evidence.js +6 -0
  20. package/dist/lib/analysis/retrieval.js +12 -12
  21. package/dist/lib/analysis/scoring.js +12 -12
  22. package/dist/lib/analysis/search-analyzer.js +13 -13
  23. package/dist/lib/analysis/sentiment.js +20 -18
  24. package/dist/lib/analysis/source-mix.js +2 -2
  25. package/dist/lib/author/examples.js +13 -13
  26. package/dist/lib/bundle/contracts.js +19 -13
  27. package/dist/lib/bundle/export.js +86 -31
  28. package/dist/lib/bundle/isolation.js +2 -2
  29. package/dist/lib/bundle/journal.js +14 -14
  30. package/dist/lib/bundle/project-journal.js +18 -18
  31. package/dist/lib/bundle/project.js +87 -30
  32. package/dist/lib/bundle/read.js +87 -32
  33. package/dist/lib/bundle/recorder.js +16 -16
  34. package/dist/lib/bundle/redact.js +14 -14
  35. package/dist/lib/bundle/trace-coverage.js +14 -14
  36. package/dist/lib/bundle/verify-integrity.js +17 -17
  37. package/dist/lib/bundle/verify-measurement.js +31 -22
  38. package/dist/lib/bundle/verify-report.js +36 -26
  39. package/dist/lib/bundle/verify.js +45 -35
  40. package/dist/lib/capture-manifest.js +13 -13
  41. package/dist/lib/checks/crawler-access.js +2 -2
  42. package/dist/lib/checks/renderability.js +2 -2
  43. package/dist/lib/checks/run-checks.js +20 -20
  44. package/dist/lib/chunks/{chunk-4XIAXX7K.js → chunk-2A327DMI.js} +1 -1
  45. package/dist/lib/chunks/{chunk-7WO3FFPS.js → chunk-2FK6QVU6.js} +3 -3
  46. package/dist/lib/chunks/{chunk-F2NSJ6PG.js → chunk-2WS6RC35.js} +8 -4
  47. package/dist/lib/chunks/{chunk-6EI4CVRW.js → chunk-3ESH5NDR.js} +5 -1
  48. package/dist/lib/chunks/{chunk-WIBMVNIH.js → chunk-3F5ZP2EO.js} +4 -4
  49. package/dist/lib/chunks/{chunk-ZXQPD4OC.js → chunk-3TF4JGQA.js} +126 -5
  50. package/dist/lib/chunks/{chunk-BFWK3TBN.js → chunk-3YPAIXFG.js} +8 -8
  51. package/dist/lib/chunks/{chunk-PIFTG6YW.js → chunk-4332MUTU.js} +4 -4
  52. package/dist/lib/chunks/{chunk-K2FZFESR.js → chunk-4MOZ3DVS.js} +8 -7
  53. package/dist/lib/chunks/{chunk-KFAT65P7.js → chunk-4SCPKDJV.js} +1 -1
  54. package/dist/lib/chunks/{chunk-6HMHXSCS.js → chunk-4ZR2LPUD.js} +3 -3
  55. package/dist/lib/chunks/{chunk-QGZ4QSUE.js → chunk-55KFAM4X.js} +36 -5
  56. package/dist/lib/chunks/{chunk-FJL2V6P5.js → chunk-66OL2XA6.js} +13 -8
  57. package/dist/lib/chunks/{chunk-4QQJXCD2.js → chunk-6FRZN5SA.js} +13 -28
  58. package/dist/lib/chunks/{chunk-3OE4UQIB.js → chunk-6FXNXXY7.js} +116 -7
  59. package/dist/lib/chunks/chunk-6TJXJZUL.js +97 -0
  60. package/dist/lib/chunks/{chunk-YKJHFD64.js → chunk-72ZLXWD4.js} +21 -4
  61. package/dist/lib/chunks/{chunk-2FOKEIIN.js → chunk-744YWRX7.js} +1 -1
  62. package/dist/lib/chunks/{chunk-I3UE52ZV.js → chunk-77EQ3RT6.js} +4 -4
  63. package/dist/lib/chunks/{chunk-AJO7XVXK.js → chunk-7GHR6MOG.js} +12 -12
  64. package/dist/lib/chunks/{chunk-JYCVBFH5.js → chunk-7GJTWUI4.js} +2 -2
  65. package/dist/lib/chunks/{chunk-UNCUG2YS.js → chunk-7NQOQNE7.js} +3 -3
  66. package/dist/lib/chunks/chunk-7Y3S7LD5.js +160 -0
  67. package/dist/lib/chunks/{chunk-U64NCRRR.js → chunk-7ZQAAEOK.js} +7 -6
  68. package/dist/lib/chunks/{chunk-MENTBQGB.js → chunk-A4QTI7FN.js} +78 -19
  69. package/dist/lib/chunks/{chunk-Y2CZ6FZY.js → chunk-ADNYALFZ.js} +36 -7
  70. package/dist/lib/chunks/{chunk-CH7OCYDL.js → chunk-B5ZAFK6T.js} +1 -1
  71. package/dist/lib/chunks/{chunk-WDT2Y7D7.js → chunk-B6YKBZXD.js} +20 -11
  72. package/dist/lib/chunks/{chunk-ZSEY7AHQ.js → chunk-BBK54RVK.js} +14 -11
  73. package/dist/lib/chunks/{chunk-YGLWQFKL.js → chunk-BE7EDOJY.js} +3 -3
  74. package/dist/lib/chunks/{chunk-OD36HUXA.js → chunk-BECSPYFC.js} +7 -8
  75. package/dist/lib/chunks/{chunk-HGGOGCPX.js → chunk-BNKNZTYH.js} +17 -14
  76. package/dist/lib/chunks/{chunk-2KH7LNK7.js → chunk-BOLZJBPS.js} +56 -15
  77. package/dist/lib/chunks/chunk-BQMUZLHO.js +17 -0
  78. package/dist/lib/chunks/chunk-C22Q3TKU.js +90 -0
  79. package/dist/lib/chunks/chunk-C3AZMWQJ.js +109 -0
  80. package/dist/lib/chunks/{chunk-T6XZQOXS.js → chunk-CCSF7LP5.js} +7 -7
  81. package/dist/lib/chunks/{chunk-JBOOD6MU.js → chunk-CHDK5EQK.js} +57 -16
  82. package/dist/lib/chunks/{chunk-NZFMOQHU.js → chunk-CWEVVIZI.js} +11 -1
  83. package/dist/lib/chunks/{chunk-75DMLOQ7.js → chunk-CYJNBVQB.js} +1 -1
  84. package/dist/lib/chunks/{chunk-66T7JC57.js → chunk-CZRHH5HF.js} +85 -5
  85. package/dist/lib/chunks/{chunk-FAUFE7MI.js → chunk-D7N5OLYL.js} +1 -1
  86. package/dist/lib/chunks/chunk-DBQ3HVPX.js +73 -0
  87. package/dist/lib/chunks/{chunk-PKNODC6Y.js → chunk-DC6CS6GC.js} +1 -1
  88. package/dist/lib/chunks/{chunk-7YDXD4MD.js → chunk-DHQKAIOP.js} +2 -2
  89. package/dist/lib/chunks/{chunk-LTCDLURT.js → chunk-DIR4UIRF.js} +73 -24
  90. package/dist/lib/chunks/{chunk-E2YTTB6Q.js → chunk-DSK3PM4Z.js} +1 -1
  91. package/dist/lib/chunks/chunk-DWOTAT7R.js +312 -0
  92. package/dist/lib/chunks/chunk-E2JICEL3.js +446 -0
  93. package/dist/lib/chunks/{chunk-HXHFMFQU.js → chunk-E5COBP7A.js} +13 -2
  94. package/dist/lib/chunks/{chunk-XAN4CRFP.js → chunk-EUHVUF7I.js} +1 -1
  95. package/dist/lib/chunks/{chunk-TNL2GWYR.js → chunk-EUXNCXCR.js} +22 -0
  96. package/dist/lib/chunks/chunk-F3MHGDZG.js +97 -0
  97. package/dist/lib/chunks/chunk-F7LCGM6W.js +120 -0
  98. package/dist/lib/chunks/chunk-FJAZEGU6.js +354 -0
  99. package/dist/lib/chunks/chunk-FOJVAGAD.js +91 -0
  100. package/dist/lib/chunks/chunk-FXR6P7HY.js +14 -0
  101. package/dist/lib/chunks/chunk-GC4XJYQW.js +145 -0
  102. package/dist/lib/chunks/chunk-GQZHZSAQ.js +108 -0
  103. package/dist/lib/chunks/{chunk-BRENK356.js → chunk-GZC3ASNL.js} +1 -1
  104. package/dist/lib/chunks/{chunk-3CMPR6ZZ.js → chunk-H2JJCIZW.js} +1 -1
  105. package/dist/lib/chunks/{chunk-XSER7WEB.js → chunk-H33MFLSH.js} +12 -12
  106. package/dist/lib/chunks/chunk-H4IE2JE6.js +118 -0
  107. package/dist/lib/chunks/chunk-H4LZ3U26.js +31 -0
  108. package/dist/lib/chunks/{chunk-XFUYEFQP.js → chunk-HHGLGVOZ.js} +1 -1
  109. package/dist/lib/chunks/{chunk-52F5XYOW.js → chunk-I2KRWXXO.js} +15 -7
  110. package/dist/lib/chunks/chunk-IDNT2WBK.js +27 -0
  111. package/dist/lib/chunks/{chunk-R5UPOEWE.js → chunk-IUQ6C4Y6.js} +14 -4
  112. package/dist/lib/chunks/chunk-IXYVHA7C.js +101 -0
  113. package/dist/lib/chunks/{chunk-FDM2YNQL.js → chunk-J3BZ3WPV.js} +1 -1
  114. package/dist/lib/chunks/{chunk-U6BW6LGX.js → chunk-J63WQUKR.js} +10 -3
  115. package/dist/lib/chunks/{chunk-OOLWU667.js → chunk-JBEKIT7Z.js} +10 -10
  116. package/dist/lib/chunks/chunk-JEAZREDN.js +94 -0
  117. package/dist/lib/chunks/{chunk-2B5QF6UQ.js → chunk-JFUWSJZT.js} +46 -5
  118. package/dist/lib/chunks/{chunk-SIJOQRRK.js → chunk-JHOLYLI4.js} +4 -2
  119. package/dist/lib/chunks/chunk-JOLIDHZV.js +39 -0
  120. package/dist/lib/chunks/{chunk-HCUJ2J7P.js → chunk-JWH33YCK.js} +17 -16
  121. package/dist/lib/chunks/{chunk-HGGRPOSA.js → chunk-K5K27WBZ.js} +10 -9
  122. package/dist/lib/chunks/{chunk-7HM6IZ3L.js → chunk-KU53HTUY.js} +2 -2
  123. package/dist/lib/chunks/{chunk-PIF3GL62.js → chunk-LJTFZ52Y.js} +24 -12
  124. package/dist/lib/chunks/chunk-LMUTZ6O2.js +335 -0
  125. package/dist/lib/chunks/chunk-LTPLZSXI.js +86 -0
  126. package/dist/lib/chunks/{chunk-RXSHKWZJ.js → chunk-M3WZCV65.js} +3 -3
  127. package/dist/lib/chunks/{chunk-ZOGIJGU3.js → chunk-MN3YGXJH.js} +17 -17
  128. package/dist/lib/chunks/{chunk-ZQ3ER7JD.js → chunk-MSEGYN6W.js} +32 -14
  129. package/dist/lib/chunks/{chunk-JQDQVG3D.js → chunk-MWZIVYMK.js} +1 -1
  130. package/dist/lib/chunks/{chunk-WCLKIPJV.js → chunk-MX6YA6YH.js} +1 -1
  131. package/dist/lib/chunks/{chunk-GEFKDW7X.js → chunk-N4PQ6RPQ.js} +14 -2
  132. package/dist/lib/chunks/{chunk-EDQTINTL.js → chunk-N5IAPSLM.js} +52 -6
  133. package/dist/lib/chunks/{chunk-V5JJEEBT.js → chunk-NAPYHYA5.js} +6 -5
  134. package/dist/lib/chunks/{chunk-LR27H2HZ.js → chunk-NWUZJUJX.js} +8 -8
  135. package/dist/lib/chunks/{chunk-U5USH2HB.js → chunk-NZTOOSLX.js} +3 -3
  136. package/dist/lib/chunks/{chunk-S6QOXJAL.js → chunk-OQVEYTVV.js} +16 -7
  137. package/dist/lib/chunks/{chunk-GLUGYZQO.js → chunk-OXPPUUIO.js} +48 -60
  138. package/dist/lib/chunks/chunk-PPTUCXWS.js +95 -0
  139. package/dist/lib/chunks/{chunk-W5XYW6ZM.js → chunk-PT6PK2MJ.js} +26 -19
  140. package/dist/lib/chunks/{chunk-7DNLRXEY.js → chunk-PUZBLM7E.js} +9 -6
  141. package/dist/lib/chunks/{chunk-7YZPEX3T.js → chunk-PVW5W2MX.js} +9 -4
  142. package/dist/lib/chunks/{chunk-YEUCAPKP.js → chunk-QFIJSLAR.js} +26 -6
  143. package/dist/lib/chunks/{chunk-J5NPHPXY.js → chunk-QTUWCZ3X.js} +43 -8
  144. package/dist/lib/chunks/{chunk-6GKKALCI.js → chunk-R4BFYHLK.js} +2 -2
  145. package/dist/lib/chunks/chunk-R75F4MKU.js +162 -0
  146. package/dist/lib/chunks/{chunk-V5B6UZ3E.js → chunk-RQGEPR6V.js} +1 -0
  147. package/dist/lib/chunks/{chunk-TAPVCSCR.js → chunk-SCZ7PA4T.js} +1 -1
  148. package/dist/lib/chunks/{chunk-K4Y5NT4M.js → chunk-SFPPZGTA.js} +112 -26
  149. package/dist/lib/chunks/chunk-SN3DKEOY.js +11 -0
  150. package/dist/lib/chunks/{chunk-UYDQXXDQ.js → chunk-ST6IVXHT.js} +1 -1
  151. package/dist/lib/chunks/chunk-SXCTAL6V.js +116 -0
  152. package/dist/lib/chunks/chunk-SXVKFPC4.js +288 -0
  153. package/dist/lib/chunks/chunk-T4Z3GAEH.js +292 -0
  154. package/dist/lib/chunks/{chunk-RK57YZKM.js → chunk-T7DNKNMG.js} +81 -26
  155. package/dist/lib/chunks/{chunk-DL6YQDTE.js → chunk-TPPBTN3E.js} +50 -21
  156. package/dist/lib/chunks/{chunk-J7QKT5E7.js → chunk-UBH2P5IR.js} +91 -32
  157. package/dist/lib/chunks/{chunk-BYHXNFWN.js → chunk-UIXTXI7P.js} +9 -7
  158. package/dist/lib/chunks/chunk-UWXR24TV.js +38 -0
  159. package/dist/lib/chunks/chunk-V57HW7T4.js +86 -0
  160. package/dist/lib/chunks/{chunk-XA27KAUV.js → chunk-VCFLUGLQ.js} +80 -48
  161. package/dist/lib/chunks/{chunk-VL253W5C.js → chunk-VMDGYDZW.js} +3 -1
  162. package/dist/lib/chunks/{chunk-RIJERFGU.js → chunk-W6TGEP5C.js} +96 -26
  163. package/dist/lib/chunks/chunk-WB4ZBRF6.js +338 -0
  164. package/dist/lib/chunks/{chunk-F7DBTXKG.js → chunk-WQTDLVKV.js} +4 -3
  165. package/dist/lib/chunks/{chunk-VXYQWE45.js → chunk-WRH4CIFQ.js} +3 -3
  166. package/dist/lib/chunks/{chunk-JKPBXPCW.js → chunk-WSFXGX7S.js} +3 -3
  167. package/dist/lib/chunks/{chunk-BXF4OC74.js → chunk-WSWQCHSW.js} +32 -17
  168. package/dist/lib/chunks/{chunk-EOO5RBUW.js → chunk-WTAIFAQV.js} +42 -6
  169. package/dist/lib/chunks/{chunk-7YYDUHJG.js → chunk-WZA4H4TG.js} +39 -13
  170. package/dist/lib/chunks/{chunk-RYCRL3YA.js → chunk-X3HRV5CF.js} +12 -4
  171. package/dist/lib/chunks/{chunk-S2SFHTJD.js → chunk-XPUCJQIB.js} +39 -1
  172. package/dist/lib/chunks/{chunk-PWOYEIPF.js → chunk-Y2NZTYKV.js} +8 -8
  173. package/dist/lib/chunks/chunk-YLS4XX7B.js +105 -0
  174. package/dist/lib/chunks/{chunk-7CW6U5WJ.js → chunk-YPMRZX4U.js} +1 -1
  175. package/dist/lib/chunks/{chunk-SASE7U6E.js → chunk-YQD5ITKI.js} +1 -1
  176. package/dist/lib/chunks/{chunk-LTI2YYU3.js → chunk-YTEUQEJE.js} +1 -1
  177. package/dist/lib/chunks/{chunk-4GLW3ZRO.js → chunk-YYP547FI.js} +146 -22
  178. package/dist/lib/chunks/{chunk-QD3JXA3Y.js → chunk-ZDCKZEDJ.js} +56 -36
  179. package/dist/lib/chunks/{chunk-AT3XMGES.js → chunk-ZWTNELCS.js} +12 -6
  180. package/dist/lib/chunks/{chunk-VHKHYAC5.js → chunk-ZWWIXFCV.js} +3 -3
  181. package/dist/lib/chunks/{chunk-MA27TFEY.js → chunk-ZXL6FZX4.js} +8 -1
  182. package/dist/lib/cli/bundle-commands.js +137 -111
  183. package/dist/lib/cli/guide-command.js +1 -1
  184. package/dist/lib/cli/io.js +1 -1
  185. package/dist/lib/cli/run-commands.js +349 -168
  186. package/dist/lib/cli/status-command.js +114 -87
  187. package/dist/lib/codex-workflow/aggregate.js +41 -24
  188. package/dist/lib/codex-workflow/author-search.js +1 -1
  189. package/dist/lib/codex-workflow/author-tools.js +14 -14
  190. package/dist/lib/codex-workflow/author.js +35 -32
  191. package/dist/lib/codex-workflow/capture.js +53 -28
  192. package/dist/lib/codex-workflow/checkpoint.js +14 -14
  193. package/dist/lib/codex-workflow/checks.js +16 -16
  194. package/dist/lib/codex-workflow/copy-editor.js +29 -26
  195. package/dist/lib/codex-workflow/copy-recovery.js +13 -13
  196. package/dist/lib/codex-workflow/copy.js +32 -23
  197. package/dist/lib/codex-workflow/finish.js +67 -37
  198. package/dist/lib/codex-workflow/host-models.js +2 -2
  199. package/dist/lib/codex-workflow/loop.js +96 -72
  200. package/dist/lib/codex-workflow/prompts.js +1 -1
  201. package/dist/lib/codex-workflow/quality.js +13 -13
  202. package/dist/lib/codex-workflow/run-inputs.js +13 -13
  203. package/dist/lib/codex-workflow/run-source.js +24 -24
  204. package/dist/lib/codex-workflow/schemas.js +13 -13
  205. package/dist/lib/codex-workflow/session.js +15 -15
  206. package/dist/lib/codex-workflow/stage.js +49 -46
  207. package/dist/lib/codex-workflow/workdir.js +25 -18
  208. package/dist/lib/engine/audit-file.js +50 -16
  209. package/dist/lib/engine/author.js +77 -57
  210. package/dist/lib/engine/budget.js +3 -1
  211. package/dist/lib/engine/cost-estimate.js +46 -16
  212. package/dist/lib/engine/local-run.js +113 -85
  213. package/dist/lib/engine/preflight.js +47 -17
  214. package/dist/lib/engine/rejudge.js +121 -92
  215. package/dist/lib/engine/run-bindings.js +66 -0
  216. package/dist/lib/engine/run-configuration.js +3 -1
  217. package/dist/lib/engine/run-method.js +67 -0
  218. package/dist/lib/engine/run-models.js +48 -0
  219. package/dist/lib/engine/run-status.js +112 -85
  220. package/dist/lib/engine/templates.js +47 -17
  221. package/dist/lib/grounding/controller.js +10 -9
  222. package/dist/lib/grounding/extract.js +10 -8
  223. package/dist/lib/grounding/grounding-runtime.js +32 -31
  224. package/dist/lib/grounding/model-usage.js +2 -2
  225. package/dist/lib/grounding/model.js +9 -7
  226. package/dist/lib/grounding/recorded-tools.js +17 -17
  227. package/dist/lib/grounding/tools.js +5 -1
  228. package/dist/lib/harness/report/artifacts.js +12 -12
  229. package/dist/lib/harness/report/capture-coverage.js +4 -4
  230. package/dist/lib/lib/checkpoint.js +12 -12
  231. package/dist/lib/lib/compare.js +6 -0
  232. package/dist/lib/lib/log.js +1 -1
  233. package/dist/lib/lib/progress-comparison.js +3 -2
  234. package/dist/lib/models/accounting.js +1 -1
  235. package/dist/lib/models/batching.js +7 -5
  236. package/dist/lib/models/budget-protection.js +1 -1
  237. package/dist/lib/models/catalog.js +16 -0
  238. package/dist/lib/models/cost-coefficients.js +12 -1
  239. package/dist/lib/models/gateway-cost.js +7 -0
  240. package/dist/lib/models/local-profile.js +2 -2
  241. package/dist/lib/models/open-model.js +7 -0
  242. package/dist/lib/models/profiles.js +3 -1
  243. package/dist/lib/models/structured.js +6 -4
  244. package/dist/lib/models/supported-models.js +2 -2
  245. package/dist/lib/pipeline.js +50 -27
  246. package/dist/lib/probes/adapter.js +6 -0
  247. package/dist/lib/probes/adapters/cli-agents.js +44 -0
  248. package/dist/lib/probes/adapters/cli-claude-code.js +22 -0
  249. package/dist/lib/probes/adapters/cli-codex.js +24 -0
  250. package/dist/lib/probes/adapters/cli-cursor.js +22 -0
  251. package/dist/lib/probes/adapters/cli-process.js +35 -0
  252. package/dist/lib/probes/adapters/dataforseo-ai-mode.js +24 -0
  253. package/dist/lib/probes/adapters/dataforseo-ai-overview.js +21 -0
  254. package/dist/lib/probes/adapters/dataforseo-chatgpt-app.js +22 -0
  255. package/dist/lib/probes/adapters/gateway-chat.js +33 -0
  256. package/dist/lib/probes/adapters/gateway-perplexity.js +20 -0
  257. package/dist/lib/probes/agent-cli-parsers.js +4 -3
  258. package/dist/lib/probes/agent-clis.js +34 -19
  259. package/dist/lib/probes/answer-text.js +22 -0
  260. package/dist/lib/probes/attempt.js +29 -0
  261. package/dist/lib/probes/bindings.js +101 -0
  262. package/dist/lib/probes/charged-trace.js +49 -3
  263. package/dist/lib/probes/chat-retrieval.js +7 -2
  264. package/dist/lib/probes/dataforseo-locale.js +9 -3
  265. package/dist/lib/probes/dataforseo.js +16 -5
  266. package/dist/lib/probes/grounding-urls.js +6 -4
  267. package/dist/lib/probes/legacy-bindings.js +7 -0
  268. package/dist/lib/probes/limits.js +1 -1
  269. package/dist/lib/probes/probes.js +42 -19
  270. package/dist/lib/probes/web-tool-guard.js +3 -3
  271. package/dist/lib/report/contracts.js +7 -7
  272. package/dist/lib/report/core/progress-comparison.js +3 -2
  273. package/dist/lib/report/core/progress-schema.js +6 -2
  274. package/dist/lib/report/core/progress-statistics.js +8 -0
  275. package/dist/lib/report/core/rate-interval.js +6 -0
  276. package/dist/lib/report/core/retrieval.js +4 -1
  277. package/dist/lib/report/core/schema.js +23 -3
  278. package/dist/lib/report/core/surfaces.js +23 -1
  279. package/dist/lib/retrieval.js +4 -1
  280. package/dist/lib/runner.js +28 -25
  281. package/dist/lib/schema.js +37 -13
  282. package/dist/lib/section-result.js +15 -13
  283. package/dist/lib/stores/artifacts.js +15 -15
  284. package/dist/lib/stores/files.js +31 -27
  285. package/dist/lib/stores/local-versions.js +12 -12
  286. package/dist/lib/stores/memory.js +33 -27
  287. package/dist/lib/stores/publication.js +19 -18
  288. package/dist/lib/stores/unit-record.js +28 -0
  289. package/dist/lib/surfaces.js +23 -1
  290. package/dist/lib/viewer/page.js +1 -1
  291. package/dist/lib/viewer/server.js +47 -37
  292. package/dist/lib/workflow/schema.js +10 -10
  293. package/dist/types/agents/author-schema.d.ts +10 -10
  294. package/dist/types/agents/facts.d.ts +1 -1
  295. package/dist/types/agents/methodology.d.ts +26 -0
  296. package/dist/types/agents/verifier.d.ts +1 -1
  297. package/dist/types/analysis/domain-labels.d.ts +1 -1
  298. package/dist/types/analysis/rate-evidence.d.ts +11 -0
  299. package/dist/types/analysis/scoring.d.ts +17 -0
  300. package/dist/types/analysis/sentiment.d.ts +8 -4
  301. package/dist/types/bundle/contracts.d.ts +42 -20
  302. package/dist/types/bundle/project.d.ts +6 -0
  303. package/dist/types/bundle/recorder.d.ts +1 -1
  304. package/dist/types/bundle/trace-coverage.d.ts +4 -2
  305. package/dist/types/capture-manifest.d.ts +9 -4
  306. package/dist/types/codex-workflow/aggregate.d.ts +90 -12
  307. package/dist/types/codex-workflow/capture.d.ts +13 -0
  308. package/dist/types/codex-workflow/copy-editor.d.ts +40 -1
  309. package/dist/types/codex-workflow/copy.d.ts +136 -1
  310. package/dist/types/codex-workflow/run-inputs.d.ts +138 -32
  311. package/dist/types/codex-workflow/schemas.d.ts +20 -20
  312. package/dist/types/codex-workflow/stage.d.ts +7 -1
  313. package/dist/types/codex-workflow/workdir.d.ts +0 -1
  314. package/dist/types/engine/audit-file.d.ts +96 -6
  315. package/dist/types/engine/budget.d.ts +19 -6
  316. package/dist/types/engine/cost-estimate.d.ts +15 -2
  317. package/dist/types/engine/local-run.d.ts +12 -2
  318. package/dist/types/engine/preflight.d.ts +27 -6
  319. package/dist/types/engine/run-bindings.d.ts +52 -0
  320. package/dist/types/engine/run-configuration.d.ts +15 -3
  321. package/dist/types/engine/run-method.d.ts +58 -0
  322. package/dist/types/engine/run-models.d.ts +89 -0
  323. package/dist/types/engine/templates.d.ts +3 -1
  324. package/dist/types/grounding/tools.d.ts +10 -0
  325. package/dist/types/harness/report/artifacts.d.ts +48 -24
  326. package/dist/types/lib/compare.d.ts +3 -0
  327. package/dist/types/models/accounting.d.ts +7 -0
  328. package/dist/types/models/catalog.d.ts +52 -0
  329. package/dist/types/models/cost-coefficients.d.ts +77 -24
  330. package/dist/types/models/gateway-cost.d.ts +3 -0
  331. package/dist/types/models/local-profile.d.ts +5 -1
  332. package/dist/types/models/open-model.d.ts +10 -0
  333. package/dist/types/models/profiles.d.ts +113 -14
  334. package/dist/types/models/supported-models.d.ts +13 -3
  335. package/dist/types/pipeline.d.ts +2 -0
  336. package/dist/types/probes/adapter.d.ts +121 -0
  337. package/dist/types/probes/adapters/cli-agents.d.ts +19 -0
  338. package/dist/types/probes/adapters/cli-claude-code.d.ts +4 -0
  339. package/dist/types/probes/adapters/cli-codex.d.ts +10 -0
  340. package/dist/types/probes/adapters/cli-cursor.d.ts +4 -0
  341. package/dist/types/probes/adapters/cli-process.d.ts +86 -0
  342. package/dist/types/probes/adapters/dataforseo-ai-mode.d.ts +16 -0
  343. package/dist/types/probes/adapters/dataforseo-ai-overview.d.ts +12 -0
  344. package/dist/types/probes/adapters/dataforseo-chatgpt-app.d.ts +12 -0
  345. package/dist/types/probes/adapters/gateway-chat.d.ts +61 -0
  346. package/dist/types/probes/adapters/gateway-perplexity.d.ts +10 -0
  347. package/dist/types/probes/agent-clis.d.ts +7 -49
  348. package/dist/types/probes/answer-text.d.ts +20 -0
  349. package/dist/types/probes/attempt.d.ts +113 -0
  350. package/dist/types/probes/bindings.d.ts +168 -0
  351. package/dist/types/probes/charged-trace.d.ts +8 -1
  352. package/dist/types/probes/chat-retrieval.d.ts +2 -0
  353. package/dist/types/probes/dataforseo-locale.d.ts +16 -0
  354. package/dist/types/probes/dataforseo.d.ts +66 -1
  355. package/dist/types/probes/grounding-urls.d.ts +18 -5
  356. package/dist/types/probes/legacy-bindings.d.ts +36 -0
  357. package/dist/types/probes/limits.d.ts +12 -6
  358. package/dist/types/probes/probe-trace.d.ts +13 -0
  359. package/dist/types/probes/probes.d.ts +7 -5
  360. package/dist/types/report/contracts.d.ts +184 -30
  361. package/dist/types/report/core/progress-comparison.d.ts +19 -2
  362. package/dist/types/report/core/progress-schema.d.ts +13 -5
  363. package/dist/types/report/core/progress-statistics.d.ts +28 -0
  364. package/dist/types/report/core/rate-interval.d.ts +10 -0
  365. package/dist/types/report/core/retrieval.d.ts +5 -2
  366. package/dist/types/report/core/schema.d.ts +190 -30
  367. package/dist/types/report/core/surfaces.d.ts +38 -4
  368. package/dist/types/runner.d.ts +5 -0
  369. package/dist/types/schema.d.ts +126 -21
  370. package/dist/types/section-result.d.ts +7 -2
  371. package/dist/types/stores/ports.d.ts +1 -0
  372. package/dist/types/stores/unit-record.d.ts +11 -0
  373. package/dist/types/workflow/schema.d.ts +121 -41
  374. package/package.json +6 -6
  375. package/resources/guide/OPERATOR.md +35 -7
  376. package/src/agents/author-schema.ts +7 -0
  377. package/src/agents/credentials.ts +18 -3
  378. package/src/agents/facts.ts +116 -3
  379. package/src/agents/methodology.ts +168 -0
  380. package/src/agents/narrative-facts.ts +50 -13
  381. package/src/agents/verifier.ts +2 -1
  382. package/src/analysis/progress.ts +14 -2
  383. package/src/analysis/rate-evidence.ts +74 -0
  384. package/src/analysis/scoring.ts +53 -10
  385. package/src/bundle/contracts.ts +42 -5
  386. package/src/bundle/export.ts +24 -4
  387. package/src/bundle/project-journal.ts +7 -1
  388. package/src/bundle/project.ts +76 -7
  389. package/src/bundle/recorder.ts +50 -1
  390. package/src/bundle/trace-coverage.ts +13 -6
  391. package/src/bundle/verify-integrity.ts +1 -0
  392. package/src/bundle/verify-report.ts +9 -3
  393. package/src/capture-manifest.ts +6 -0
  394. package/src/cli/io.ts +2 -0
  395. package/src/cli/literally-aeo.ts +10 -6
  396. package/src/cli/run-commands.ts +268 -39
  397. package/src/codex-workflow/aggregate.ts +108 -15
  398. package/src/codex-workflow/capture.ts +62 -5
  399. package/src/codex-workflow/copy-editor.ts +13 -2
  400. package/src/codex-workflow/copy.ts +171 -2
  401. package/src/codex-workflow/finish.ts +39 -0
  402. package/src/codex-workflow/loop.ts +23 -2
  403. package/src/codex-workflow/prompts.ts +32 -42
  404. package/src/codex-workflow/schemas.ts +3 -1
  405. package/src/codex-workflow/stage.ts +40 -45
  406. package/src/codex-workflow/workdir.ts +3 -4
  407. package/src/engine/audit-file.ts +186 -13
  408. package/src/engine/author.ts +5 -2
  409. package/src/engine/budget.ts +70 -10
  410. package/src/engine/cost-estimate.ts +39 -9
  411. package/src/engine/guide/OPERATOR.md +35 -7
  412. package/src/engine/local-run.ts +109 -13
  413. package/src/engine/preflight.ts +170 -30
  414. package/src/engine/rejudge.ts +8 -2
  415. package/src/engine/run-bindings.ts +189 -0
  416. package/src/engine/run-configuration.ts +36 -6
  417. package/src/engine/run-method.ts +230 -0
  418. package/src/engine/run-models.ts +276 -0
  419. package/src/engine/run-status.ts +9 -4
  420. package/src/engine/templates.ts +7 -2
  421. package/src/grounding/controller.ts +2 -1
  422. package/src/grounding/extract.ts +1 -0
  423. package/src/grounding/model-usage.ts +4 -0
  424. package/src/grounding/tools.ts +18 -1
  425. package/src/lib/compare.ts +9 -0
  426. package/src/lib/log.ts +17 -15
  427. package/src/models/accounting.ts +34 -0
  428. package/src/models/budget-protection.ts +1 -0
  429. package/src/models/catalog.ts +500 -0
  430. package/src/models/cost-coefficients.ts +116 -27
  431. package/src/models/gateway-cost.ts +32 -0
  432. package/src/models/local-profile.ts +14 -8
  433. package/src/models/open-model.ts +70 -0
  434. package/src/models/profiles.ts +17 -4
  435. package/src/models/structured.ts +2 -10
  436. package/src/models/supported-models.ts +7 -5
  437. package/src/pipeline.ts +45 -19
  438. package/src/probes/adapter.ts +138 -0
  439. package/src/probes/adapters/cli-agents.ts +123 -0
  440. package/src/probes/adapters/cli-claude-code.ts +119 -0
  441. package/src/probes/adapters/cli-codex.ts +145 -0
  442. package/src/probes/adapters/cli-cursor.ts +95 -0
  443. package/src/probes/adapters/cli-process.ts +522 -0
  444. package/src/probes/adapters/dataforseo-ai-mode.ts +146 -0
  445. package/src/probes/adapters/dataforseo-ai-overview.ts +106 -0
  446. package/src/probes/adapters/dataforseo-chatgpt-app.ts +115 -0
  447. package/src/probes/adapters/gateway-chat.ts +453 -0
  448. package/src/probes/adapters/gateway-perplexity.ts +104 -0
  449. package/src/probes/agent-clis.ts +151 -1088
  450. package/src/probes/answer-text.ts +153 -0
  451. package/src/probes/attempt.ts +463 -0
  452. package/src/probes/bindings.ts +761 -0
  453. package/src/probes/charged-trace.ts +69 -13
  454. package/src/probes/chat-retrieval.ts +67 -0
  455. package/src/probes/dataforseo-locale.ts +153 -22
  456. package/src/probes/dataforseo.ts +474 -178
  457. package/src/probes/grounding-urls.ts +68 -19
  458. package/src/probes/legacy-bindings.ts +41 -0
  459. package/src/probes/limits.ts +31 -18
  460. package/src/probes/probe-trace.ts +13 -0
  461. package/src/probes/probes.ts +76 -738
  462. package/src/probes/web-tool-guard.ts +18 -8
  463. package/src/report/core/progress-comparison.ts +158 -44
  464. package/src/report/core/progress-schema.ts +25 -3
  465. package/src/report/core/progress-statistics.ts +194 -0
  466. package/src/report/core/rate-interval.ts +24 -0
  467. package/src/report/core/retrieval.ts +18 -4
  468. package/src/report/core/schema.ts +79 -4
  469. package/src/report/core/surfaces.ts +100 -9
  470. package/src/runner.ts +72 -4
  471. package/src/schema.ts +78 -17
  472. package/src/section-result.ts +141 -23
  473. package/src/stores/files.ts +78 -54
  474. package/src/stores/memory.ts +4 -1
  475. package/src/stores/ports.ts +3 -0
  476. package/src/stores/unit-record.ts +151 -0
  477. package/src/viewer/page.ts +126 -5
  478. package/dist/lib/chunks/chunk-AASWJR7Q.js +0 -42
  479. package/dist/lib/chunks/chunk-BGAHZIL3.js +0 -563
  480. package/dist/lib/chunks/chunk-DVFFYY5G.js +0 -74
  481. package/dist/lib/chunks/chunk-IB6RROHE.js +0 -204
  482. package/dist/lib/chunks/chunk-NH4QGBJV.js +0 -61
  483. package/dist/lib/chunks/chunk-WZGI7UVM.js +0 -937
@@ -1,59 +1,73 @@
1
1
  import { z } from 'zod';
2
2
  import { createLog } from '../lib/log';
3
- import { readEnv } from '../lib/env';
4
- import {
5
- dataForSeoLanguageCodes,
6
- dataForSeoLocationCodes,
7
- } from './dataforseo-locale';
8
- import { ProbeFailure } from './failure';
3
+ import type { resolveDataForSeoLocale } from './dataforseo-locale';
4
+ import { ProbeFailure, type ProbeFailureKind } from './failure';
9
5
 
10
6
  const log = createLog('DataForSEO');
11
7
 
12
8
  // ============================================================================
13
- // DataForSEO client — Google AI Overview slice only (ported from the
14
- // console's intelligence/clients/dataforseo.ts; the SERP/keyword endpoints
15
- // this audit doesn't use were left behind).
9
+ // DataForSEO client: the three endpoints the probes ask. Google AI Overview
10
+ // (ported from the console's intelligence/clients/dataforseo.ts; the
11
+ // SERP/keyword endpoints this audit doesn't use were left behind), and the
12
+ // two consumer-app scrapes of the method release spec §5: Google AI Mode
13
+ // and ChatGPT's web interface.
14
+ //
15
+ // One client per run (probe adapters spec §7, phase 3 item 5): its
16
+ // credentials are passed in, its throttle is its own, its response body is
17
+ // bounded while it is read, and every field it reads is checked. The market
18
+ // is a resolved DataForSEO locale; a market the table cannot place is refused
19
+ // when the run is planned, never sent as the United States.
16
20
  //
17
21
  // Failure taxonomy (hardening spec §2.2, §3.6): a SERP that came back with
18
22
  // no `ai_overview` item is an honest measurement (`no_overview`); a failed
19
23
  // task, a bad location code, a 5xx or a timeout is a typed ProbeFailure
20
24
  // that follows the probe retry policy — never a captured "no AI Overview".
21
25
  // Retries belong to the caller: each attempt here is one paid SERP task.
26
+ //
27
+ // The consumer-app response shapes are pinned by the captures in
28
+ // __fixtures__/dataforseo (11 November 2026).
22
29
  // ============================================================================
23
30
 
24
- // The envelope is read leniently: only the fields the client branches on are
25
- // checked, and a missing message prints as before.
31
+ // Provider fields the client does not read pass through unchecked, so a
32
+ // field DataForSEO adds never fails a probe; every field it reads is typed
33
+ // and bounded.
26
34
  const dataForSeoResponseSchema = z.object({
27
35
  status_code: z.number(),
28
- status_message: z.string().optional(),
36
+ status_message: z.string().max(2_000).optional(),
37
+ // What the whole response was billed, in USD; read by reportedCostOf.
38
+ cost: z.unknown().optional(),
29
39
  tasks: z
30
40
  .array(
31
41
  z.object({
32
- id: z.string().optional(),
42
+ id: z.string().max(200).optional(),
33
43
  status_code: z.number(),
34
- status_message: z.string().optional(),
35
- result: z.array(z.unknown()).nullish(),
44
+ status_message: z.string().max(2_000).optional(),
45
+ result: z.array(z.unknown()).max(100).nullish(),
36
46
  }),
37
47
  )
48
+ .max(100)
38
49
  .nullish(),
39
50
  });
40
51
 
41
52
  const serpResultSchema = z.object({
42
- items: z.array(z.record(z.string(), z.unknown())).nullish(),
53
+ items: z.array(z.record(z.string(), z.unknown())).max(1_000).nullish(),
43
54
  });
44
55
 
56
+ const referenceField = z.string().max(10_000).nullish();
57
+
45
58
  const aiOverviewItemSchema = z.object({
46
- markdown: z.string().nullish(),
59
+ markdown: z.string().max(200_000).nullish(),
47
60
  references: z
48
61
  .array(
49
62
  z.object({
50
- source: z.string().nullish(),
51
- domain: z.string().nullish(),
52
- url: z.string().nullish(),
53
- title: z.string().nullish(),
54
- text: z.string().nullish(),
63
+ source: referenceField,
64
+ domain: referenceField,
65
+ url: referenceField,
66
+ title: referenceField,
67
+ text: referenceField,
55
68
  }),
56
69
  )
70
+ .max(500)
57
71
  .nullish(),
58
72
  });
59
73
 
@@ -69,218 +83,500 @@ export type AIOverviewOutcome =
69
83
  | { status: 'overview'; markdown: string; references: AIOverviewReference[] }
70
84
  | { status: 'no_overview' };
71
85
 
72
- const baseUrl = 'https://api.dataforseo.com/v3';
73
- const requestDelayMs = 50;
74
- const requestTimeoutMs = 30_000;
86
+ // The parts of a scraped ChatGPT answer that are the answer: its prose and
87
+ // its tables. Every other item (image carousels, whose markdown is an
88
+ // embed of a thumbnail under the page title it was found on, and the
89
+ // shopping, ad and local-business items) is left out: none is the
90
+ // assistant's words or a source it cites.
91
+ const chatGptAnswerItemTypes: ReadonlySet<string> = new Set([
92
+ 'chat_gpt_text',
93
+ 'chat_gpt_table',
94
+ ]);
95
+
96
+ const chatGptResultSchema = z.object({
97
+ // The model the scraper read off the page; nothing pins it.
98
+ model: z.string().max(200).nullish(),
99
+ items: z
100
+ .array(
101
+ z.object({
102
+ type: z.string().max(100),
103
+ markdown: z.string().max(200_000).nullish(),
104
+ }),
105
+ )
106
+ .max(1_000)
107
+ .nullish(),
108
+ sources: z
109
+ .array(z.object({ url: referenceField, title: referenceField }))
110
+ .max(500)
111
+ .nullish(),
112
+ fan_out_queries: z.array(z.string().max(2_000)).max(200).nullish(),
113
+ });
75
114
 
76
- // Serialized throttle: callers queue behind one chain instead of all
77
- // reading the last request time before any of them awaits (§3.5).
78
- let throttleChain: Promise<void> = Promise.resolve();
115
+ /** What a paid response reported as its cost; null when it reported none. */
116
+ type ReportedCost = { costUsd: number | null };
79
117
 
80
- const throttled = <T>(fn: () => Promise<T>): Promise<T> => {
81
- const slot = throttleChain.then(
82
- () =>
83
- new Promise<void>((resolve) => {
84
- setTimeout(resolve, requestDelayMs);
85
- }),
86
- );
118
+ export type AiModeAnswer = ReportedCost & {
119
+ markdown: string;
120
+ references: AIOverviewReference[];
121
+ };
87
122
 
88
- throttleChain = slot.catch(() => undefined);
123
+ export type ChatGptAppSource = { url: string; title: string };
124
+
125
+ export type ChatGptAppAnswer = ReportedCost & {
126
+ /** The answer's prose and tables, in the order the page shows them. */
127
+ markdown: string;
128
+ /** The sources the vendor describes as cited or relied on. */
129
+ sources: ChatGptAppSource[];
130
+ /** The searches the answer ran, as a flat list; null when the response
131
+ * reported no list. */
132
+ fanOutQueries: string[] | null;
133
+ /** The model the scraper reports, verbatim; null when it names none. */
134
+ model: string | null;
135
+ };
89
136
 
90
- return slot.then(fn);
137
+ /** Where a SERP is placed: a market resolveDataForSeoLocale mapped. */
138
+ export type DataForSeoLocale = NonNullable<
139
+ ReturnType<typeof resolveDataForSeoLocale>
140
+ >;
141
+
142
+ export type DataForSeoProbeClient = {
143
+ getGoogleAIOverview: (
144
+ query: string,
145
+ locale: DataForSeoLocale,
146
+ ) => Promise<AIOverviewOutcome>;
147
+ getGoogleAiMode: (
148
+ query: string,
149
+ locale: DataForSeoLocale,
150
+ ) => Promise<AiModeAnswer>;
151
+ getChatGptAnswer: (
152
+ query: string,
153
+ locale: DataForSeoLocale,
154
+ ) => Promise<ChatGptAppAnswer>;
91
155
  };
92
156
 
93
- const getAuthHeader = (): string => {
94
- const login = readEnv('DATAFORSEO_LOGIN');
95
- const password = readEnv('DATAFORSEO_PASSWORD');
157
+ const baseUrl = 'https://api.dataforseo.com/v3';
158
+ export const aiOverviewEndpoint = '/serp/google/organic/live/advanced';
96
159
 
97
- if (!login || !password) {
98
- throw new ProbeFailure('auth', 'DataForSEO credentials not configured');
99
- }
160
+ /** aiOverviewTask is the SERP task an AI Overview probe sends. */
161
+ export const aiOverviewTask = (query: string, locale: DataForSeoLocale) => {
162
+ return {
163
+ keyword: query,
164
+ language_code: locale.languageCode,
165
+ location_code: locale.locationCode,
166
+ load_async_ai_overview: true,
167
+ };
168
+ };
100
169
 
101
- return `Basic ${Buffer.from(`${login}:${password}`).toString('base64')}`;
170
+ export const aiModeEndpoint = '/serp/google/ai_mode/live/advanced';
171
+
172
+ /** aiModeTask is the task an AI Mode probe sends. */
173
+ export const aiModeTask = (query: string, locale: DataForSeoLocale) => {
174
+ return {
175
+ keyword: query,
176
+ language_code: locale.languageCode,
177
+ location_code: locale.locationCode,
178
+ };
102
179
  };
103
180
 
181
+ export const chatGptEndpoint =
182
+ '/ai_optimization/chat_gpt/llm_scraper/live/advanced';
183
+
184
+ /** chatGptTask is the task a ChatGPT probe sends: the web interface, with
185
+ * its search forced on, as every other surface is asked. */
186
+ export const chatGptTask = (query: string, locale: DataForSeoLocale) => {
187
+ return {
188
+ keyword: query,
189
+ language_code: locale.languageCode,
190
+ location_code: locale.locationCode,
191
+ force_web_search: true,
192
+ };
193
+ };
194
+
195
+ const requestDelayMs = 50;
196
+ const requestTimeoutMs = 30_000;
197
+ // AI Mode answered in 9 seconds at the median and 19 at most over 50
198
+ // requests (11 November 2026).
199
+ const aiModeTimeoutMs = 60_000;
200
+ // DataForSEO documents up to 120 seconds for a live ChatGPT scrape; six
201
+ // took 13 to 61 (11 November 2026).
202
+ const chatGptTimeoutMs = 150_000;
203
+ const maxResponseBytes = 8 * 1024 * 1024;
204
+
104
205
  // DataForSEO's own status codes: 40202 is "too many requests".
105
206
  const capacityStatusCodes = new Set([40202]);
207
+ // 40102 is "No Search Results". AI Mode always answers in the product, so
208
+ // on that endpoint it is a scrape that came back without its answer
209
+ // (method release spec §5.3), not an absence to record.
210
+ const noResultsStatusCode = 40102;
211
+
212
+ type ApiRequestOptions = {
213
+ timeoutMs?: number;
214
+ /** Status codes that mean the surface returned no answer. */
215
+ emptyAnswerStatusCodes?: ReadonlySet<number>;
216
+ };
217
+
218
+ type ApiResponse = { results: unknown[]; costUsd: number | null };
219
+
220
+ /** readBoundedBody reads a response body, refusing one larger than the
221
+ * limit as it arrives instead of after buffering it whole. */
222
+ const readBoundedBody = async (response: Response): Promise<string> => {
223
+ if (!response.body) {
224
+ return '';
225
+ }
226
+
227
+ const reader = response.body.getReader();
228
+ const chunks: Uint8Array[] = [];
229
+ let size = 0;
106
230
 
107
- const apiRequest = async (
108
- endpoint: string,
109
- body: unknown[],
110
- ): Promise<unknown[]> => {
111
- const authorization = getAuthHeader();
112
-
113
- return throttled(async () => {
114
- let response: Response;
115
-
116
- try {
117
- response = await fetch(`${baseUrl}${endpoint}`, {
118
- method: 'POST',
119
- headers: {
120
- 'Authorization': authorization,
121
- 'Content-Type': 'application/json',
122
- },
123
- body: JSON.stringify(body),
124
- signal: AbortSignal.timeout(requestTimeoutMs),
125
- });
126
- } catch (error) {
127
- const name = error instanceof Error ? error.name : '';
128
-
129
- if (name === 'TimeoutError' || name === 'AbortError') {
231
+ try {
232
+ for (
233
+ let chunk = await reader.read();
234
+ !chunk.done;
235
+ chunk = await reader.read()
236
+ ) {
237
+ size += chunk.value.length;
238
+
239
+ if (size > maxResponseBytes) {
130
240
  throw new ProbeFailure(
131
- 'timeout',
132
- `DataForSEO request exceeded ${requestTimeoutMs}ms`,
133
- { cause: error },
241
+ 'cli_error',
242
+ `DataForSEO response exceeds ${maxResponseBytes} bytes`,
134
243
  );
135
244
  }
136
245
 
137
- throw new ProbeFailure(
138
- 'cli_error',
139
- `DataForSEO request failed: ${error instanceof Error ? error.message : String(error)}`,
140
- { cause: error },
141
- );
246
+ chunks.push(chunk.value);
142
247
  }
248
+ } finally {
249
+ // Cancelling a finished or failed reader has nothing left to report.
250
+ await reader.cancel().catch(() => undefined);
251
+ }
252
+
253
+ return Buffer.concat(chunks).toString('utf8');
254
+ };
255
+
256
+ /** parseProviderJson checks a provider document against `schema`, and
257
+ * reports a document that is not the shape the client reads as a parser
258
+ * failure of the probe. */
259
+ const parseProviderJson = <T>(schema: z.ZodType<T>, value: unknown): T => {
260
+ const parsed = schema.safeParse(value);
261
+
262
+ if (!parsed.success) {
263
+ const issue = parsed.error.issues[0];
264
+
265
+ throw new ProbeFailure(
266
+ 'parser',
267
+ `DataForSEO response does not match the expected shape (${issue?.path.join('.') ?? ''}: ${issue?.message ?? 'invalid'})`,
268
+ );
269
+ }
270
+
271
+ return parsed.data;
272
+ };
273
+
274
+ /** The cost a response reported, or null: a value that is not an amount
275
+ * is a cost that was not reported, never a reason to lose a paid answer. */
276
+ const reportedCostOf = (cost: unknown): number | null => {
277
+ return typeof cost === 'number' && Number.isFinite(cost) && cost >= 0
278
+ ? cost
279
+ : null;
280
+ };
281
+
282
+ const taskFailureKind = (
283
+ statusCode: number,
284
+ emptyAnswerStatusCodes: ReadonlySet<number>,
285
+ ): ProbeFailureKind => {
286
+ if (emptyAnswerStatusCodes.has(statusCode)) {
287
+ return 'empty_answer';
288
+ }
143
289
 
144
- if (!response.ok) {
145
- const detail = `${response.status}: ${response.statusText}`;
290
+ return capacityStatusCodes.has(statusCode) ? 'capacity' : 'cli_error';
291
+ };
292
+
293
+ /** The markdown and references of an `ai_overview` item, as the client
294
+ * hands them on: an absent field is an empty one. */
295
+ const aiOverviewContent = (
296
+ item: z.infer<typeof aiOverviewItemSchema>,
297
+ ): { markdown: string; references: AIOverviewReference[] } => {
298
+ return {
299
+ markdown: item.markdown ?? '',
300
+ references: (item.references ?? []).map((reference) => ({
301
+ source: reference.source ?? '',
302
+ domain: reference.domain ?? '',
303
+ url: reference.url ?? '',
304
+ title: reference.title ?? '',
305
+ text: reference.text ?? '',
306
+ })),
307
+ };
308
+ };
309
+
310
+ /**
311
+ * createDataForSeoProbeClient is one run's DataForSEO client. Requests are
312
+ * spaced by the client's own throttle: callers queue behind one chain
313
+ * instead of all reading the last request time before any of them awaits
314
+ * (§3.5).
315
+ */
316
+ export const createDataForSeoProbeClient = ({
317
+ login,
318
+ password,
319
+ }: {
320
+ login: string;
321
+ password: string;
322
+ }): DataForSeoProbeClient => {
323
+ if (!login || !password) {
324
+ throw new Error('Please provide both DataForSEO credentials.');
325
+ }
326
+
327
+ const authorization = `Basic ${Buffer.from(`${login}:${password}`).toString('base64')}`;
328
+ let throttleChain: Promise<void> = Promise.resolve();
329
+
330
+ const throttled = <T>(fn: () => Promise<T>): Promise<T> => {
331
+ const slot = throttleChain.then(
332
+ () =>
333
+ new Promise<void>((resolve) => {
334
+ setTimeout(resolve, requestDelayMs);
335
+ }),
336
+ );
337
+
338
+ throttleChain = slot.catch(() => undefined);
339
+
340
+ return slot.then(fn);
341
+ };
342
+
343
+ const apiRequest = async (
344
+ endpoint: string,
345
+ body: unknown[],
346
+ {
347
+ timeoutMs = requestTimeoutMs,
348
+ emptyAnswerStatusCodes = new Set(),
349
+ }: ApiRequestOptions = {},
350
+ ): Promise<ApiResponse> => {
351
+ return throttled(async () => {
352
+ let response: Response;
353
+
354
+ try {
355
+ response = await fetch(`${baseUrl}${endpoint}`, {
356
+ method: 'POST',
357
+ headers: {
358
+ 'Authorization': authorization,
359
+ 'Content-Type': 'application/json',
360
+ },
361
+ body: JSON.stringify(body),
362
+ signal: AbortSignal.timeout(timeoutMs),
363
+ });
364
+ } catch (error) {
365
+ const name = error instanceof Error ? error.name : '';
366
+
367
+ if (name === 'TimeoutError' || name === 'AbortError') {
368
+ throw new ProbeFailure(
369
+ 'timeout',
370
+ `DataForSEO request exceeded ${timeoutMs}ms`,
371
+ { cause: error },
372
+ );
373
+ }
146
374
 
147
- if (response.status === 401 || response.status === 403) {
148
375
  throw new ProbeFailure(
149
- 'auth',
150
- `DataForSEO rejected the credentials (${detail})`,
376
+ 'cli_error',
377
+ `DataForSEO request failed: ${error instanceof Error ? error.message : String(error)}`,
378
+ { cause: error },
151
379
  );
152
380
  }
153
381
 
154
- if (response.status === 429 || response.status === 503) {
155
- throw new ProbeFailure(
156
- 'capacity',
157
- `DataForSEO is throttling (${detail})`,
158
- );
382
+ if (!response.ok) {
383
+ await response.body?.cancel().catch(() => undefined);
384
+ const detail = `${response.status}: ${response.statusText}`;
385
+
386
+ if (response.status === 401 || response.status === 403) {
387
+ throw new ProbeFailure(
388
+ 'auth',
389
+ `DataForSEO rejected the credentials (${detail})`,
390
+ );
391
+ }
392
+
393
+ if (response.status === 429 || response.status === 503) {
394
+ throw new ProbeFailure(
395
+ 'capacity',
396
+ `DataForSEO is throttling (${detail})`,
397
+ );
398
+ }
399
+
400
+ throw new ProbeFailure('cli_error', `DataForSEO HTTP error ${detail}`);
159
401
  }
160
402
 
161
- throw new ProbeFailure('cli_error', `DataForSEO HTTP error ${detail}`);
162
- }
403
+ const text = await readBoundedBody(response);
404
+ let json: unknown;
405
+
406
+ try {
407
+ json = JSON.parse(text);
408
+ } catch (error) {
409
+ throw new ProbeFailure('parser', 'DataForSEO response is not JSON', {
410
+ cause: error,
411
+ });
412
+ }
163
413
 
164
- const data = dataForSeoResponseSchema.parse(await response.json());
414
+ const data = parseProviderJson(dataForSeoResponseSchema, json);
415
+
416
+ if (data.status_code !== 20000) {
417
+ if (emptyAnswerStatusCodes.has(data.status_code)) {
418
+ throw new ProbeFailure(
419
+ 'empty_answer',
420
+ `DataForSEO API ${data.status_code}: ${data.status_message}`,
421
+ );
422
+ }
423
+
424
+ if (capacityStatusCodes.has(data.status_code)) {
425
+ throw new ProbeFailure(
426
+ 'capacity',
427
+ `DataForSEO API ${data.status_code}: ${data.status_message}`,
428
+ );
429
+ }
430
+
431
+ // 40101 is a search-engine server error, not rejected credentials.
432
+ // https://docs.dataforseo.com/v3/appendix/errors/
433
+ if (data.status_code === 40100) {
434
+ throw new ProbeFailure(
435
+ 'auth',
436
+ `DataForSEO API ${data.status_code}: ${data.status_message}`,
437
+ );
438
+ }
165
439
 
166
- if (data.status_code !== 20000) {
167
- if (capacityStatusCodes.has(data.status_code)) {
168
440
  throw new ProbeFailure(
169
- 'capacity',
441
+ 'cli_error',
170
442
  `DataForSEO API ${data.status_code}: ${data.status_message}`,
171
443
  );
172
444
  }
173
445
 
174
- // 40101 is a search-engine server error, not rejected credentials.
175
- // https://docs.dataforseo.com/v3/appendix/errors/
176
- if (data.status_code === 40100) {
446
+ const results: unknown[] = [];
447
+
448
+ for (const task of data.tasks ?? []) {
449
+ // A completed task without a result has nothing to hand on; each
450
+ // caller says what an empty result means on its endpoint.
451
+ if (task.status_code === 20000) {
452
+ results.push(...(task.result ?? []));
453
+ continue;
454
+ }
455
+
456
+ // A task that did not complete is a failed probe, not an absent
457
+ // AI Overview (§2.2).
458
+ log.warn('DataForSEO task failed', {
459
+ endpoint,
460
+ taskId: task.id,
461
+ taskStatusCode: task.status_code,
462
+ taskStatusMessage: task.status_message,
463
+ });
464
+
177
465
  throw new ProbeFailure(
178
- 'auth',
179
- `DataForSEO API ${data.status_code}: ${data.status_message}`,
466
+ taskFailureKind(task.status_code, emptyAnswerStatusCodes),
467
+ `DataForSEO task ${task.status_code}: ${task.status_message}`,
180
468
  );
181
469
  }
182
470
 
183
- throw new ProbeFailure(
184
- 'cli_error',
185
- `DataForSEO API ${data.status_code}: ${data.status_message}`,
186
- );
187
- }
471
+ return { results, costUsd: reportedCostOf(data.cost) };
472
+ });
473
+ };
188
474
 
189
- const results: unknown[] = [];
475
+ const getGoogleAIOverview: DataForSeoProbeClient['getGoogleAIOverview'] =
476
+ async (query, locale) => {
477
+ const { results } = await apiRequest(aiOverviewEndpoint, [
478
+ aiOverviewTask(query, locale),
479
+ ]);
190
480
 
191
- for (const task of data.tasks ?? []) {
192
- if (task.status_code === 20000 && task.result) {
193
- results.push(...task.result);
194
- continue;
481
+ if (results.length === 0) {
482
+ throw new ProbeFailure(
483
+ 'cli_error',
484
+ 'DataForSEO returned a completed task with no SERP result.',
485
+ );
195
486
  }
196
487
 
197
- // A task that did not complete is a failed probe, not an absent
198
- // AI Overview (§2.2).
199
- log.warn('DataForSEO task failed', {
200
- endpoint,
201
- taskId: task.id,
202
- taskStatusCode: task.status_code,
203
- taskStatusMessage: task.status_message,
204
- });
488
+ for (const result of results) {
489
+ const items = parseProviderJson(serpResultSchema, result).items ?? [];
490
+ const aiOverviewItem = items.find(
491
+ (item) => item.type === 'ai_overview',
492
+ );
205
493
 
206
- throw new ProbeFailure(
207
- capacityStatusCodes.has(task.status_code) ? 'capacity' : 'cli_error',
208
- `DataForSEO task ${task.status_code}: ${task.status_message}`,
209
- );
210
- }
494
+ if (!aiOverviewItem) {
495
+ continue;
496
+ }
211
497
 
212
- return results;
213
- });
214
- };
498
+ const aiOverview = parseProviderJson(
499
+ aiOverviewItemSchema,
500
+ aiOverviewItem,
501
+ );
502
+ return { status: 'overview', ...aiOverviewContent(aiOverview) };
503
+ }
215
504
 
216
- export const getGoogleAIOverview = async (
217
- query: string,
218
- geography: string,
219
- language: string,
220
- ): Promise<AIOverviewOutcome> => {
221
- const tasks = [
222
- {
223
- keyword: query,
224
- ...getGeoParams(geography, language),
225
- load_async_ai_overview: true,
226
- },
227
- ];
505
+ return { status: 'no_overview' };
506
+ };
228
507
 
229
- const results = await apiRequest('/serp/google/organic/live/advanced', tasks);
508
+ const getGoogleAiMode: DataForSeoProbeClient['getGoogleAiMode'] = async (
509
+ query,
510
+ locale,
511
+ ) => {
512
+ const { results, costUsd } = await apiRequest(
513
+ aiModeEndpoint,
514
+ [aiModeTask(query, locale)],
515
+ {
516
+ timeoutMs: aiModeTimeoutMs,
517
+ emptyAnswerStatusCodes: new Set([noResultsStatusCode]),
518
+ },
519
+ );
520
+
521
+ // The endpoint returns the answer as the one `ai_overview` item of
522
+ // its result.
523
+ for (const result of results) {
524
+ const items = parseProviderJson(serpResultSchema, result).items ?? [];
525
+ const answerItem = items.find((item) => item.type === 'ai_overview');
526
+
527
+ if (answerItem) {
528
+ return {
529
+ ...aiOverviewContent(
530
+ parseProviderJson(aiOverviewItemSchema, answerItem),
531
+ ),
532
+ costUsd,
533
+ };
534
+ }
535
+ }
230
536
 
231
- if (results.length === 0) {
232
537
  throw new ProbeFailure(
233
- 'cli_error',
234
- 'DataForSEO returned a completed task with no SERP result.',
538
+ 'empty_answer',
539
+ 'DataForSEO returned an AI Mode result without its answer.',
235
540
  );
236
- }
541
+ };
237
542
 
238
- for (const result of results) {
239
- const items = serpResultSchema.parse(result).items ?? [];
240
- const aiOverviewItem = items.find((item) => item.type === 'ai_overview');
543
+ const getChatGptAnswer: DataForSeoProbeClient['getChatGptAnswer'] = async (
544
+ query,
545
+ locale,
546
+ ) => {
547
+ const { results, costUsd } = await apiRequest(
548
+ chatGptEndpoint,
549
+ [chatGptTask(query, locale)],
550
+ { timeoutMs: chatGptTimeoutMs },
551
+ );
552
+ const [first] = results;
241
553
 
242
- if (!aiOverviewItem) {
243
- continue;
554
+ if (first === undefined) {
555
+ throw new ProbeFailure(
556
+ 'empty_answer',
557
+ 'DataForSEO returned a completed ChatGPT task with no result.',
558
+ );
244
559
  }
245
560
 
246
- const aiOverview = aiOverviewItemSchema.parse(aiOverviewItem);
247
- const markdown = aiOverview.markdown ?? '';
248
- const rawRefs = aiOverview.references ?? [];
561
+ const result = parseProviderJson(chatGptResultSchema, first);
562
+ const markdown = (result.items ?? [])
563
+ .filter((item) => chatGptAnswerItemTypes.has(item.type))
564
+ .map((item) => (item.markdown ?? '').trim())
565
+ .filter((part) => part !== '')
566
+ .join('\n\n');
249
567
 
250
568
  return {
251
- status: 'overview',
252
569
  markdown,
253
- references: rawRefs.map((ref) => ({
254
- source: ref.source ?? '',
255
- domain: ref.domain ?? '',
256
- url: ref.url ?? '',
257
- title: ref.title ?? '',
258
- text: ref.text ?? '',
259
- })),
570
+ sources: (result.sources ?? []).flatMap((source) =>
571
+ source.url ? [{ url: source.url, title: source.title ?? '' }] : [],
572
+ ),
573
+ fanOutQueries:
574
+ result.fan_out_queries?.filter((fanOutQuery) => fanOutQuery !== '') ??
575
+ null,
576
+ model: result.model ?? null,
577
+ costUsd,
260
578
  };
261
- }
262
-
263
- return { status: 'no_overview' };
264
- };
265
-
266
- // ---------------------------------------------------------------------------
267
- // Geo helpers (ported subset — SERP live endpoints require a location code)
268
- // ---------------------------------------------------------------------------
269
-
270
- const getLocationCode = (geography: string): number => {
271
- return dataForSeoLocationCodes[geography.toLowerCase()] ?? 2840;
272
- };
273
-
274
- const getGeoParams = (
275
- geography: string,
276
- language: string,
277
- ): Record<string, number | string> => {
278
- return {
279
- language_code: getLanguageCode(language),
280
- location_code: getLocationCode(geography),
281
579
  };
282
- };
283
580
 
284
- const getLanguageCode = (language: string): string => {
285
- return dataForSeoLanguageCodes[language.toLowerCase()] ?? 'en';
581
+ return { getGoogleAIOverview, getGoogleAiMode, getChatGptAnswer };
286
582
  };