pi-coding-master 0.2.7

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 (304) hide show
  1. package/README.md +50 -0
  2. package/core/god.agent.capability/@ABANDONED.brain.prefrontal.monitor/gpu_monitor.py +80 -0
  3. package/core/god.agent.capability/@ABANDONED.brain.prefrontal.monitor/gpu_monitor.py.CHANGELOG +1 -0
  4. package/core/god.agent.capability/@ABANDONED.brain.prefrontal.monitor/gpu_monitor.py.SPEC +3 -0
  5. package/core/god.agent.capability/hands.dev.writeissue/issue.ts +209 -0
  6. package/core/god.agent.capability/hands.dev.writeissue/issue.ts.SPEC +26 -0
  7. package/core/god.agent.capability/hands.files.changewatcher/watcher.ts +44 -0
  8. package/core/god.agent.capability/hands.files.changewatcher/watcher.ts.SPEC +25 -0
  9. package/core/god.pi.mod/cli/pi-completion.zsh +21 -0
  10. package/core/god.pi.mod/cli/pi-completion.zsh.CHANGELOG +1 -0
  11. package/core/god.pi.mod/cli/pi-people.sh +264 -0
  12. package/core/god.pi.mod/cli/pi-people.sh.LESSON +10 -0
  13. package/core/god.pi.mod/cli/pi-people.sh.SPEC +31 -0
  14. package/core/god.pi.mod/paths.ts +47 -0
  15. package/core/god.pi.mod/tui.mods.blockrender/blockrender.js +90 -0
  16. package/core/god.pi.mod/tui.mods.blockrender/blockrender.js.SPEC +29 -0
  17. package/core/god.pi.mod/tui.mods.footer.budget/budget_guard.ts +154 -0
  18. package/core/god.pi.mod/tui.mods.footer.budget/budget_guard.ts.CHANGELOG +10 -0
  19. package/core/god.pi.mod/tui.mods.footer.budget/budget_guard.ts.LESSON +12 -0
  20. package/core/god.pi.mod/tui.mods.footer.budget/budget_guard.ts.SPEC +26 -0
  21. package/core/god.pi.mod/tui.mods.footer.budget/footer-cny.CHANGELOG +39 -0
  22. package/core/god.pi.mod/tui.mods.viewmode/view_mode-thinking.patch +55 -0
  23. package/core/god.pi.mod/tui.mods.viewmode/view_mode-tools.patch +19 -0
  24. package/core/god.pi.mod/tui.mods.viewmode/view_mode-tools.patch.CHANGELOG +1 -0
  25. package/core/god.pi.mod/tui.mods.viewmode/view_mode.ts +50 -0
  26. package/core/god.pi.mod/tui.mods.viewmode/view_mode.ts.SPEC +12 -0
  27. package/core/god.pi.mod/tui.variants.userterminal/user_terminal.CHANGELOG +10 -0
  28. package/core/god.pi.mod/tui.variants.userterminal/user_terminal.ts +66 -0
  29. package/core/god.pi.mod/tui.variants.userterminal/user_terminal.ts.SPEC +31 -0
  30. package/core/index.ts +3 -0
  31. package/core/individual.bio.gene/dna.coded/coded.dna +257 -0
  32. package/core/individual.bio.gene/dna.coded/coded.dna.CHANGELOG +12 -0
  33. package/core/individual.bio.gene/dna.coded/coded.dna.SPEC +11 -0
  34. package/core/individual.bio.gene/dna.coded/core.dna +110 -0
  35. package/core/individual.bio.gene/dna.promotor/promotor.dna +117 -0
  36. package/core/individual.bio.gene/dna.promotor/promotor.dna.CHANGELOG +4 -0
  37. package/core/individual.bio.gene/dna.promotor/promotor.dna.SPEC +7 -0
  38. package/core/individual.bio.gene/dna.transpiler/transpiler.ts +395 -0
  39. package/core/individual.bio.gene/dna.transpiler/transpiler.ts.CHANGELOG +7 -0
  40. package/core/individual.bio.gene/dna.transpiler/transpiler.ts.SPEC +28 -0
  41. package/core/individual.bio.gene/gene.README +19 -0
  42. package/core/individual.bio.gene/rna/rna.json +536 -0
  43. package/core/individual.bio.gene/rna/rna.json.CHANGELOG +2 -0
  44. package/core/individual.bio.gene/rna/rna.json.SPEC +8 -0
  45. package/core/individual.bio.organs/blood.runtime/messages.ts +236 -0
  46. package/core/individual.bio.organs/blood.runtime/runtime.ts +173 -0
  47. package/core/individual.bio.organs/blood.runtime/runtime.ts.CHANGELOG +5 -0
  48. package/core/individual.bio.organs/blood.runtime/runtime.ts.SPEC +32 -0
  49. package/core/individual.bio.organs/brain.amygdala/amygdala.ts +25 -0
  50. package/core/individual.bio.organs/brain.amygdala/amygdala.ts.COMMENT +3 -0
  51. package/core/individual.bio.organs/brain.amygdala/amygdala.ts.SPEC +9 -0
  52. package/core/individual.bio.organs/brain.hippocampus/hippocampus-launcher.sh.template +108 -0
  53. package/core/individual.bio.organs/brain.hippocampus/hippocampus.ts +166 -0
  54. package/core/individual.bio.organs/brain.hippocampus/hippocampus.ts.CHANGELOG +12 -0
  55. package/core/individual.bio.organs/brain.hippocampus/hippocampus.ts.LESSON +22 -0
  56. package/core/individual.bio.organs/brain.hippocampus/hippocampus.ts.SPEC +16 -0
  57. package/core/individual.bio.organs/brain.hippocampus/memory.ts +879 -0
  58. package/core/individual.bio.organs/brain.hippocampus/memory.ts.CHANGELOG +66 -0
  59. package/core/individual.bio.organs/brain.hippocampus/memory.ts.LESSON +25 -0
  60. package/core/individual.bio.organs/brain.hippocampus/memory.ts.SPEC +46 -0
  61. package/core/individual.bio.organs/brain.hippocampus/sleep.ts +139 -0
  62. package/core/individual.bio.organs/brain.hippocampus/sleep.ts.CHANGELOG +11 -0
  63. package/core/individual.bio.organs/brain.hippocampus/sleep.ts.SPEC +16 -0
  64. package/core/individual.bio.organs/brain.prefrontal.drafting/drafting.SPEC +44 -0
  65. package/core/individual.bio.organs/brain.prefrontal.drafting/drafting.ts +73 -0
  66. package/core/individual.bio.organs/brain.prefrontal.drafting/drafting.ts.CHANGELOG +3 -0
  67. package/core/individual.bio.organs/brain.prefrontal.drafting/drafting.ts.SPEC +24 -0
  68. package/core/individual.bio.organs/brain.prefrontal.drafting/index.ts.CHANGELOG +3 -0
  69. package/core/individual.bio.organs/brain.prefrontal.drafting/main.ts +13 -0
  70. package/core/individual.bio.organs/brain.prefrontal.drafting/main.ts.SPEC +17 -0
  71. package/core/individual.bio.organs/brain.senses.bioclock/bioclock.ts +94 -0
  72. package/core/individual.bio.organs/brain.senses.bioclock/bioclock.ts.LESSON +13 -0
  73. package/core/individual.bio.organs/brain.senses.bioclock/bioclock.ts.SPEC +20 -0
  74. package/core/individual.bio.organs/brain.senses.subconscious/feed-format.SPEC +56 -0
  75. package/core/individual.bio.organs/brain.senses.subconscious/index.ts.CHANGELOG +13 -0
  76. package/core/individual.bio.organs/brain.senses.subconscious/spawner.ts +130 -0
  77. package/core/individual.bio.organs/brain.senses.subconscious/spawner.ts.SPEC +13 -0
  78. package/core/individual.bio.organs/brain.senses.subconscious/subconscious.ts +280 -0
  79. package/core/individual.bio.organs/brain.senses.subconscious/subconscious.ts.CHANGELOG +7 -0
  80. package/core/individual.bio.organs/brain.senses.subconscious/tools.ts +180 -0
  81. package/core/individual.bio.organs/brain.senses.subconscious/tools.ts.SPEC +3 -0
  82. package/core/individual.bio.organs/ears.listen/config.json +9 -0
  83. package/core/individual.bio.organs/ears.listen/config.json.CHANGELOG +1 -0
  84. package/core/individual.bio.organs/ears.listen/config.json.SPEC +3 -0
  85. package/core/individual.bio.organs/ears.listen/ears.ts.CHANGELOG +48 -0
  86. package/core/individual.bio.organs/ears.listen/ears_recorder.py.CHANGELOG +6 -0
  87. package/core/individual.bio.organs/ears.listen/index.ts +1 -0
  88. package/core/individual.bio.organs/ears.listen/index.ts.SPEC +16 -0
  89. package/core/individual.bio.organs/ears.listen/listen.ts +208 -0
  90. package/core/individual.bio.organs/ears.listen/listen.ts.SPEC +3 -0
  91. package/core/individual.bio.organs/ears.listen/listen_recorder.py +445 -0
  92. package/core/individual.bio.organs/ears.listen/listen_recorder.py.SPEC +7 -0
  93. package/core/individual.bio.organs/ears.listen/python_protogen/__init__.py +0 -0
  94. package/core/individual.bio.organs/ears.listen/python_protogen/common/__init__.py +0 -0
  95. package/core/individual.bio.organs/ears.listen/python_protogen/common/events_pb2.py +38 -0
  96. package/core/individual.bio.organs/ears.listen/python_protogen/common/events_pb2_grpc.py +24 -0
  97. package/core/individual.bio.organs/ears.listen/python_protogen/common/rpcmeta_pb2.py +42 -0
  98. package/core/individual.bio.organs/ears.listen/python_protogen/common/rpcmeta_pb2_grpc.py +24 -0
  99. package/core/individual.bio.organs/ears.listen/python_protogen/products/__init__.py +0 -0
  100. package/core/individual.bio.organs/ears.listen/python_protogen/products/understanding/__init__.py +0 -0
  101. package/core/individual.bio.organs/ears.listen/python_protogen/products/understanding/ast/__init__.py +0 -0
  102. package/core/individual.bio.organs/ears.listen/python_protogen/products/understanding/ast/ast_service_pb2.py +45 -0
  103. package/core/individual.bio.organs/ears.listen/python_protogen/products/understanding/ast/ast_service_pb2_grpc.py +97 -0
  104. package/core/individual.bio.organs/ears.listen/python_protogen/products/understanding/base/__init__.py +0 -0
  105. package/core/individual.bio.organs/ears.listen/python_protogen/products/understanding/base/au_base_pb2.py +80 -0
  106. package/core/individual.bio.organs/ears.listen/python_protogen/products/understanding/base/au_base_pb2_grpc.py +24 -0
  107. package/core/individual.bio.organs/hands.fileactions/authorize.TRUST +1 -0
  108. package/core/individual.bio.organs/hands.fileactions/authorize.ts +70 -0
  109. package/core/individual.bio.organs/hands.fileactions/authorize.ts.CHANGELOG +1 -0
  110. package/core/individual.bio.organs/hands.fileactions/authorize.ts.SPEC +3 -0
  111. package/core/individual.bio.organs/hands.fileactions/dir.README +13 -0
  112. package/core/individual.bio.organs/hands.fileactions/file_rules.json +23 -0
  113. package/core/individual.bio.organs/hands.fileactions/fileactions.README +12 -0
  114. package/core/individual.bio.organs/hands.fileactions/fileactions.ts +540 -0
  115. package/core/individual.bio.organs/hands.fileactions/fileactions.ts.CHANGELOG +25 -0
  116. package/core/individual.bio.organs/hands.fileactions/fileactions.ts.SPEC +30 -0
  117. package/core/individual.bio.organs/hands.fileactions/filewatch.ts +66 -0
  118. package/core/individual.bio.organs/hands.fileactions/filewatch.ts.SPEC +3 -0
  119. package/core/individual.bio.organs/hands.main/main.ts +18 -0
  120. package/core/individual.bio.organs/hands.main/main.ts.SPEC +20 -0
  121. package/core/individual.bio.organs/hands.sensitive/sensitive.ts +24 -0
  122. package/core/individual.bio.organs/hands.sensitive/sensitive.ts.CHANGELOG +2 -0
  123. package/core/individual.bio.organs/hands.sensitive/sensitive.ts.SPEC +3 -0
  124. package/core/individual.bio.organs/heart.interrupt/agent_start-loader-flicker.patch +13 -0
  125. package/core/individual.bio.organs/heart.interrupt/interactive-mode-loader.patch +11 -0
  126. package/core/individual.bio.organs/heart.interrupt/runner_esc.patch +10 -0
  127. package/core/individual.bio.organs/heart.interrupt/runner_esc.patch.SPEC +9 -0
  128. package/core/individual.bio.organs/heart.kernel/kernel.ts +253 -0
  129. package/core/individual.bio.organs/heart.kernel/kernel.ts.CHANGELOG +13 -0
  130. package/core/individual.bio.organs/heart.kernel/kernel.ts.SPEC +23 -0
  131. package/core/individual.bio.organs/heart.main/heart.main.CHANGELOG +43 -0
  132. package/core/individual.bio.organs/heart.main/heartbeat.ts +494 -0
  133. package/core/individual.bio.organs/heart.main/heartbeat.ts.LESSON +43 -0
  134. package/core/individual.bio.organs/heart.main/main.ts +8 -0
  135. package/core/individual.bio.organs/heart.main/main.ts.SPEC +19 -0
  136. package/core/individual.bio.organs/heart.main/process.ts +122 -0
  137. package/core/individual.bio.organs/heart.main/process.ts.CHANGELOG +2 -0
  138. package/core/individual.bio.organs/heart.main/process.ts.SPEC +24 -0
  139. package/core/individual.bio.organs/heart.main/remove_timeout.patch +110 -0
  140. package/core/individual.bio.organs/heart.main/stop.ts.CHANGELOG +3 -0
  141. package/core/individual.bio.organs/heart.main/stop.ts.SPEC +28 -0
  142. package/core/individual.bio.organs/mouth.speak/index.ts +1 -0
  143. package/core/individual.bio.organs/mouth.speak/index.ts.CHANGELOG +1 -0
  144. package/core/individual.bio.organs/mouth.speak/index.ts.SPEC +6 -0
  145. package/core/individual.bio.organs/mouth.speak/mouth.ts.CHANGELOG +15 -0
  146. package/core/individual.bio.organs/mouth.speak/mouth_recorder.py.CHANGELOG +1 -0
  147. package/core/individual.bio.organs/mouth.speak/speak.ts +180 -0
  148. package/core/individual.bio.organs/mouth.speak/speak.ts.SPEC +3 -0
  149. package/core/individual.bio.organs/mouth.speak/speak_recorder.py +35 -0
  150. package/core/individual.bio.organs/mouth.speak/speak_recorder.py.SPEC +3 -0
  151. package/core/individual.bio.organs/organs.README +110 -0
  152. package/core/package-lock.json +18 -0
  153. package/core/package.json +35 -0
  154. package/core/prompts/prompts.json +77 -0
  155. package/core/prompts/prompts.ts +44 -0
  156. package/core/prompts/prompts.ts.SPEC +9 -0
  157. package/core/society.world/.gitkeep +0 -0
  158. package/core/society.world/accessibility.claudecode/message-service.cjs +217 -0
  159. package/core/society.world/accessibility.claudecode/message-service.cjs.SPEC +7 -0
  160. package/core/society.world/accessibility.claudecode/send.ts +34 -0
  161. package/core/society.world/dollar.distribution.ubi/ubi.ts +55 -0
  162. package/core/society.world/dollar.main/dollar-service.cjs +185 -0
  163. package/core/society.world/dollar.main/main.ts +116 -0
  164. package/core/society.world/dollar.transaction/transaction.ts +71 -0
  165. package/core/society.world/space/space.ts +206 -0
  166. package/core/society.world/space/space.ts.SPEC +30 -0
  167. package/core/technology.laptop/#agent.macos.BLUEPRINT +278 -0
  168. package/core/technology.phone/apps.preinstalled/albums.FUTURE/albums.ts +69 -0
  169. package/core/technology.phone/apps.preinstalled/albums.FUTURE/albums.ts.SPEC +15 -0
  170. package/core/technology.phone/apps.preinstalled/calendar/calendar.ts +406 -0
  171. package/core/technology.phone/apps.preinstalled/calendar/calendar.ts.SPEC +22 -0
  172. package/core/technology.phone/apps.preinstalled/calendar/holiday-calendar.ts +529 -0
  173. package/core/technology.phone/apps.preinstalled/clock/clock.ts +132 -0
  174. package/core/technology.phone/apps.preinstalled/clock/clock.ts.SPEC +11 -0
  175. package/core/technology.phone/apps.preinstalled/contacts.FUTURE/contacts.ts +300 -0
  176. package/core/technology.phone/apps.preinstalled/contacts.FUTURE/contacts.ts.SPEC +22 -0
  177. package/core/technology.phone/apps.preinstalled/developer.FUTURE/developer.ts +22 -0
  178. package/core/technology.phone/apps.preinstalled/developer.FUTURE/developer.ts.SPEC +15 -0
  179. package/core/technology.phone/apps.preinstalled/notes/notes.ts +239 -0
  180. package/core/technology.phone/apps.preinstalled/notes/notes.ts.SPEC +21 -0
  181. package/core/technology.phone/apps.preinstalled/polymarket/polymarket.ts +261 -0
  182. package/core/technology.phone/apps.preinstalled/polymarket/polymarket.ts.SPEC +7 -0
  183. package/core/technology.phone/apps.preinstalled/reminder/reminder.ts +404 -0
  184. package/core/technology.phone/apps.preinstalled/reminder/reminder.ts.SPEC +25 -0
  185. package/core/technology.phone/apps.preinstalled/siri.FUTURE/siri.ts +22 -0
  186. package/core/technology.phone/apps.preinstalled/siri.FUTURE/siri.ts.SPEC +15 -0
  187. package/core/technology.phone/apps.preinstalled/spotlight/spotlight.ts +29 -0
  188. package/core/technology.phone/apps.preinstalled/spotlight/spotlight.ts.SPEC +7 -0
  189. package/core/technology.phone/apps.preinstalled/steam/chess.ts +230 -0
  190. package/core/technology.phone/apps.preinstalled/steam/snake.ts +100 -0
  191. package/core/technology.phone/apps.preinstalled/steam/snake.ts.SPEC +7 -0
  192. package/core/technology.phone/apps.preinstalled/steam/spy-cmd.ts +4 -0
  193. package/core/technology.phone/apps.preinstalled/steam/spy-tool.ts +56 -0
  194. package/core/technology.phone/apps.preinstalled/steam/spy.ts +302 -0
  195. package/core/technology.phone/apps.preinstalled/steam/steam.ts +299 -0
  196. package/core/technology.phone/apps.preinstalled/weather/weather.ts +50 -0
  197. package/core/technology.phone/apps.preinstalled/weather/weather.ts.SPEC +9 -0
  198. package/core/technology.phone/apps.preinstalled/wechat/imessage.ts +423 -0
  199. package/core/technology.phone/apps.preinstalled/wechat/imessage.ts.SPEC +24 -0
  200. package/core/technology.phone/apps.system/appstore/appstore.ts +22 -0
  201. package/core/technology.phone/apps.system/appstore/appstore.ts.SPEC +15 -0
  202. package/core/technology.phone/apps.system/finder.FUTURE/finder.ts +64 -0
  203. package/core/technology.phone/apps.system/finder.FUTURE/finder.ts.SPEC +8 -0
  204. package/core/technology.phone/apps.system/safari/safari-app.ts +146 -0
  205. package/core/technology.phone/apps.system/safari/safari.ts.SPEC +24 -0
  206. package/core/technology.phone/apps.system/settings/settings.ts +126 -0
  207. package/core/technology.phone/apps.system/settings/settings.ts.SPEC +17 -0
  208. package/core/technology.phone/apps.system/tips/tips.ts +22 -0
  209. package/core/technology.phone/apps.system/tips/tips.ts.SPEC +15 -0
  210. package/core/technology.phone/apps.thirdparty/alipay/alipay.ts +148 -0
  211. package/core/technology.phone/apps.thirdparty/alipay/alipay.ts.SPEC +7 -0
  212. package/core/technology.phone/apps.thirdparty/bilibili/bilibili-app.ts +33 -0
  213. package/core/technology.phone/apps.thirdparty/bilibili/bilibili.ts +142 -0
  214. package/core/technology.phone/apps.thirdparty/bilibili/bilibili.ts.SPEC +22 -0
  215. package/core/technology.phone/apps.thirdparty/wechatread/wechatread.ts +80 -0
  216. package/core/technology.phone/apps.thirdparty/wechatread/wechatread.ts.SPEC +15 -0
  217. package/core/technology.phone/index.ts +1 -0
  218. package/core/technology.phone/package.json +2 -0
  219. package/core/technology.phone/system.homepage/homepage.ts +247 -0
  220. package/core/technology.phone/system.homepage/homepage.ts.SPEC +22 -0
  221. package/core/technology.phone/system.kernel/kernel.ts +264 -0
  222. package/core/technology.phone/system.kernel/kernel.ts.SPEC +7 -0
  223. package/core/technology.phone/system.notifications/notifications.ts +87 -0
  224. package/core/technology.phone/system.notifications/notifications.ts.SPEC +7 -0
  225. package/core/technology.phone/system.share/share.ts +46 -0
  226. package/core/technology.phone/system.share/share.ts.SPEC +7 -0
  227. package/core/technology.server/browser-service.cjs +152 -0
  228. package/core/technology.server/data/cookies/arxiv.json +30 -0
  229. package/core/technology.server/data/cookies/bili.json +184 -0
  230. package/core/technology.server/data/cookies/default.json +30 -0
  231. package/core/technology.server/data/cookies/news.json +113 -0
  232. package/core/technology.server/data/cookies/s1.json +45 -0
  233. package/core/technology.server/data/cookies/safari.json +184 -0
  234. package/core/technology.server/data/cookies/safari2.json +1 -0
  235. package/core/technology.server/data/cookies/safaridbg.json +1 -0
  236. package/core/technology.server/data/cookies/search.json +45 -0
  237. package/core/technology.server/data/cookies/sw.json +30 -0
  238. package/core/technology.server/data/cookies/t1.json +1 -0
  239. package/core/technology.server/data/cookies/testread.json +1 -0
  240. package/core/technology.server/data/cookies/video1.json +113 -0
  241. package/core/technology.server/data/cookies/yt.json +170 -0
  242. package/core/technology.server/data/cookies/yt2.json +113 -0
  243. package/core/technology.server/pikipedia/#pikipedia.BLUEPRINT +106 -0
  244. package/core/technology.server/playleft.cjs +247 -0
  245. package/core/technology.server/search-proxy.py +76 -0
  246. package/core/technology.server/server.README +59 -0
  247. package/deploy/dist-overrides/cli/cli.js +18 -0
  248. package/deploy/dist-overrides/core/extensions/loader.js +518 -0
  249. package/deploy/dist-overrides/core/package-manager.js +2081 -0
  250. package/deploy/dist-overrides/core/system-prompt.js +109 -0
  251. package/deploy/dist-overrides/core/system-prompt.js.LESSON +17 -0
  252. package/deploy/dist-overrides/core/tools/bash.js +353 -0
  253. package/deploy/dist-overrides/core/tools/bash.js.CHANGELOG +2 -0
  254. package/deploy/dist-overrides/core/tools/edit-diff.js +345 -0
  255. package/deploy/dist-overrides/core/tools/edit.js +315 -0
  256. package/deploy/dist-overrides/core/tools/edit.js.CHANGELOG +1 -0
  257. package/deploy/dist-overrides/core/tools/file-mutation-queue.js +52 -0
  258. package/deploy/dist-overrides/core/tools/find.js +298 -0
  259. package/deploy/dist-overrides/core/tools/find.js.CHANGELOG +1 -0
  260. package/deploy/dist-overrides/core/tools/grep.js +305 -0
  261. package/deploy/dist-overrides/core/tools/grep.js.CHANGELOG +1 -0
  262. package/deploy/dist-overrides/core/tools/index.js +112 -0
  263. package/deploy/dist-overrides/core/tools/ls-guard.js +4 -0
  264. package/deploy/dist-overrides/core/tools/ls.js +170 -0
  265. package/deploy/dist-overrides/core/tools/ls.js.CHANGELOG +1 -0
  266. package/deploy/dist-overrides/core/tools/output-accumulator.js +184 -0
  267. package/deploy/dist-overrides/core/tools/path-utils.js +99 -0
  268. package/deploy/dist-overrides/core/tools/prompts-reader.js +53 -0
  269. package/deploy/dist-overrides/core/tools/read.js +392 -0
  270. package/deploy/dist-overrides/core/tools/read.js.CHANGELOG +1 -0
  271. package/deploy/dist-overrides/core/tools/render-utils.js +65 -0
  272. package/deploy/dist-overrides/core/tools/tool-definition-wrapper.js +34 -0
  273. package/deploy/dist-overrides/core/tools/truncate.js +215 -0
  274. package/deploy/dist-overrides/core/tools/write.js +203 -0
  275. package/deploy/dist-overrides/core/tools/write.js.CHANGELOG +1 -0
  276. package/deploy/dist-overrides/dist-overrides.README +18 -0
  277. package/deploy/dist-overrides/main.js +665 -0
  278. package/deploy/dist-overrides/modes/interactive/components/assistant-message.js +139 -0
  279. package/deploy/dist-overrides/modes/interactive/components/footer.js +326 -0
  280. package/deploy/dist-overrides/modes/interactive/components/model-selector.js +285 -0
  281. package/deploy/dist-overrides/modes/interactive/components/tool-execution.js +383 -0
  282. package/deploy/dist-overrides/modes/interactive/components/tool-execution.js.CHANGELOG +3 -0
  283. package/deploy/dist-overrides/modes/interactive/interactive-mode.js +4781 -0
  284. package/deploy/dist-overrides/pi-ai/providers/anthropic.js +931 -0
  285. package/deploy/dist-overrides/pi-ai/providers/openai-completions.js +1007 -0
  286. package/deploy/dist-overrides/pi-ai/providers/openai-completions.js.LESSON +15 -0
  287. package/deploy/dist-overrides/pi-tui/components/loader.js +69 -0
  288. package/deploy/dist-overrides/pi-tui/components/markdown.js +646 -0
  289. package/deploy/dist-overrides/pi-tui/components/text.js +92 -0
  290. package/deploy/dist-overrides/pi-tui/custom-message.js +75 -0
  291. package/deploy/dist-overrides/pi-tui/tui.js +1266 -0
  292. package/deploy/dist-overrides/pi-tui/utils.js +1060 -0
  293. package/deploy/dist-overrides/vendor.REMOVED/jiti/lib/jiti.mjs +3 -0
  294. package/deploy/install.sh +186 -0
  295. package/deploy/install.sh.CHANGELOG +6 -0
  296. package/deploy/lint/lint-naming.ts +202 -0
  297. package/deploy/scripts/apply.js +18 -0
  298. package/deploy/scripts/build-github.sh +219 -0
  299. package/deploy/scripts/build-phone.sh +24 -0
  300. package/deploy/scripts/check-deploy.sh +42 -0
  301. package/deploy/scripts/migrate-context.sh +72 -0
  302. package/deploy/scripts/patch-pi-dist.js +39 -0
  303. package/deploy/scripts/uninstall.sh +34 -0
  304. package/package.json +18 -0
@@ -0,0 +1,1266 @@
1
+ /**
2
+ * Minimal TUI implementation with differential rendering
3
+ */
4
+ import * as fs from "node:fs";
5
+ import * as os from "node:os";
6
+ import * as path from "node:path";
7
+ import { performance } from "node:perf_hooks";
8
+ import { isKeyRelease, matchesKey } from "./keys.js";
9
+ import { deleteKittyImage, getCapabilities, isImageLine, setCellDimensions } from "./terminal-image.js";
10
+ import { extractSegments, normalizeTerminalOutput, sliceByColumn, sliceWithWidth, visibleWidth } from "./utils.js";
11
+ // pi-coding-master 统一块渲染引擎(源:god.pi.mod/tui.mods.blockrender/blockrender.js,install.sh 部署到 pi-tui/dist/)
12
+ import { wrapHanging } from "./blockrender.js";
13
+ const KITTY_SEQUENCE_PREFIX = "\x1b_G";
14
+ function extractKittyImageIds(line) {
15
+ const sequenceStart = line.indexOf(KITTY_SEQUENCE_PREFIX);
16
+ if (sequenceStart === -1)
17
+ return [];
18
+ const paramsStart = sequenceStart + KITTY_SEQUENCE_PREFIX.length;
19
+ const paramsEnd = line.indexOf(";", paramsStart);
20
+ if (paramsEnd === -1)
21
+ return [];
22
+ const params = line.slice(paramsStart, paramsEnd);
23
+ for (const param of params.split(",")) {
24
+ const [key, value] = param.split("=", 2);
25
+ if (key !== "i" || value === undefined)
26
+ continue;
27
+ const id = Number(value);
28
+ if (Number.isInteger(id) && id > 0 && id <= 0xffffffff) {
29
+ return [id];
30
+ }
31
+ }
32
+ return [];
33
+ }
34
+ /** Type guard to check if a component implements Focusable */
35
+ export function isFocusable(component) {
36
+ return component !== null && "focused" in component;
37
+ }
38
+ /**
39
+ * Cursor position marker - APC (Application Program Command) sequence.
40
+ * This is a zero-width escape sequence that terminals ignore.
41
+ * Components emit this at the cursor position when focused.
42
+ * TUI finds and strips this marker, then positions the hardware cursor there.
43
+ */
44
+ export const CURSOR_MARKER = "\x1b_pi:c\x07";
45
+ export { visibleWidth };
46
+ /** Parse a SizeValue into absolute value given a reference size */
47
+ function parseSizeValue(value, referenceSize) {
48
+ if (value === undefined)
49
+ return undefined;
50
+ if (typeof value === "number")
51
+ return value;
52
+ // Parse percentage string like "50%"
53
+ const match = value.match(/^(\d+(?:\.\d+)?)%$/);
54
+ if (match) {
55
+ return Math.floor((referenceSize * parseFloat(match[1])) / 100);
56
+ }
57
+ return undefined;
58
+ }
59
+ function isTermuxSession() {
60
+ return Boolean(process.env.TERMUX_VERSION);
61
+ }
62
+ /**
63
+ * Container - a component that contains other components
64
+ */
65
+ export class Container {
66
+ children = [];
67
+ addChild(component) {
68
+ this.children.push(component);
69
+ }
70
+ removeChild(component) {
71
+ const index = this.children.indexOf(component);
72
+ if (index !== -1) {
73
+ this.children.splice(index, 1);
74
+ }
75
+ }
76
+ clear() {
77
+ this.children = [];
78
+ }
79
+ invalidate() {
80
+ for (const child of this.children) {
81
+ child.invalidate?.();
82
+ }
83
+ }
84
+ render(width) {
85
+ const lines = [];
86
+ this._firstChildLines = 0;
87
+ this._lastChildLines = 0;
88
+ for (let ci = 0; ci < this.children.length; ci++) {
89
+ const childLines = this.children[ci].render(width);
90
+ if (ci === 0) this._firstChildLines = childLines.length;
91
+ if (ci === this.children.length - 1) this._lastChildLines = childLines.length;
92
+ for (const line of childLines) {
93
+ lines.push(line);
94
+ }
95
+ }
96
+ return lines;
97
+ }
98
+ }
99
+ /**
100
+ * TUI - Main class for managing terminal UI with differential rendering
101
+ */
102
+ export class TUI extends Container {
103
+ terminal;
104
+ previousLines = [];
105
+ previousKittyImageIds = new Set();
106
+ previousWidth = 0;
107
+ previousHeight = 0;
108
+ focusedComponent = null;
109
+ inputListeners = new Set();
110
+ /** Global callback for debug key (Shift+Ctrl+D). Called before input is forwarded to focused component. */
111
+ onDebug;
112
+ renderRequested = false;
113
+ renderTimer;
114
+ lastRenderAt = 0;
115
+ static MIN_RENDER_INTERVAL_MS = 16;
116
+ cursorRow = 0; // Logical cursor row (end of rendered content)
117
+ hardwareCursorRow = 0; // Actual terminal cursor row (may differ due to IME positioning)
118
+ showHardwareCursor = process.env.PI_HARDWARE_CURSOR === "1";
119
+ clearOnShrink = process.env.PI_CLEAR_ON_SHRINK === "1"; // Clear empty rows when content shrinks (default: off)
120
+ maxLinesRendered = 0; // Track terminal's working area (max lines ever rendered)
121
+ previousViewportTop = 0; // Track previous viewport top for resize-aware cursor moves
122
+ fullRedrawCount = 0;
123
+ stopped = false;
124
+ _scrollRegionActive = false;
125
+ _pinnedBottom = 0;
126
+ // Overlay stack for modal components rendered on top of base content
127
+ focusOrderCounter = 0;
128
+ overlayStack = [];
129
+ overlayFocusRestore = { status: "inactive" };
130
+ constructor(terminal, showHardwareCursor) {
131
+ super();
132
+ this.terminal = terminal;
133
+ if (showHardwareCursor !== undefined) {
134
+ this.showHardwareCursor = showHardwareCursor;
135
+ }
136
+ }
137
+ get fullRedraws() {
138
+ return this.fullRedrawCount;
139
+ }
140
+ getShowHardwareCursor() {
141
+ return this.showHardwareCursor;
142
+ }
143
+ setShowHardwareCursor(enabled) {
144
+ if (this.showHardwareCursor === enabled)
145
+ return;
146
+ this.showHardwareCursor = enabled;
147
+ if (!enabled) {
148
+ this.terminal.hideCursor();
149
+ }
150
+ this.requestRender();
151
+ }
152
+ getClearOnShrink() {
153
+ return this.clearOnShrink;
154
+ }
155
+ /**
156
+ * Set whether to trigger full re-render when content shrinks.
157
+ * When true (default), empty rows are cleared when content shrinks.
158
+ * When false, empty rows remain (reduces redraws on slower terminals).
159
+ */
160
+ setClearOnShrink(enabled) {
161
+ this.clearOnShrink = enabled;
162
+ }
163
+ setFocus(component) {
164
+ this.setFocusInternal({ component, overlayFocusRestore: "clear" });
165
+ }
166
+ setFocusInternal({ component, overlayFocusRestore, }) {
167
+ const previousFocus = this.focusedComponent;
168
+ let nextFocus = component;
169
+ const previousFocusedOverlay = previousFocus
170
+ ? this.overlayStack.find((entry) => entry.component === previousFocus && this.isOverlayVisible(entry))
171
+ : undefined;
172
+ const nextFocusIsOverlay = nextFocus ? this.overlayStack.some((entry) => entry.component === nextFocus) : false;
173
+ const restoreState = this.getVisibleOverlayFocusRestore();
174
+ if (nextFocus && !nextFocusIsOverlay) {
175
+ if (restoreState.status === "blocked" && restoreState.blockedBy === previousFocus) {
176
+ if (restoreState.resume.status === "focus-target" || !this.isComponentMounted(restoreState.blockedBy)) {
177
+ nextFocus = this.resolveBlockedOverlayFocusResume(restoreState);
178
+ }
179
+ else {
180
+ this.overlayFocusRestore = {
181
+ status: "blocked",
182
+ overlay: restoreState.overlay,
183
+ blockedBy: nextFocus,
184
+ resume: restoreState.resume,
185
+ };
186
+ }
187
+ }
188
+ else if (previousFocusedOverlay &&
189
+ restoreState.status !== "inactive" &&
190
+ restoreState.overlay === previousFocusedOverlay &&
191
+ !this.isOverlayFocusAncestor(previousFocusedOverlay, nextFocus)) {
192
+ this.overlayFocusRestore = {
193
+ status: "blocked",
194
+ overlay: previousFocusedOverlay,
195
+ blockedBy: nextFocus,
196
+ resume: { status: "restore-overlay" },
197
+ };
198
+ }
199
+ }
200
+ else if (nextFocus === null) {
201
+ if (restoreState.status === "blocked" && restoreState.blockedBy === previousFocus) {
202
+ nextFocus = this.resolveBlockedOverlayFocusResume(restoreState);
203
+ }
204
+ else if (overlayFocusRestore === "clear") {
205
+ this.clearOverlayFocusRestore();
206
+ }
207
+ }
208
+ if (isFocusable(this.focusedComponent)) {
209
+ this.focusedComponent.focused = false;
210
+ }
211
+ this.focusedComponent = nextFocus;
212
+ if (isFocusable(nextFocus)) {
213
+ nextFocus.focused = true;
214
+ }
215
+ const focusedOverlay = nextFocus
216
+ ? this.overlayStack.find((entry) => entry.component === nextFocus && this.isOverlayVisible(entry))
217
+ : undefined;
218
+ if (focusedOverlay) {
219
+ this.overlayFocusRestore = { status: "eligible", overlay: focusedOverlay };
220
+ }
221
+ }
222
+ clearOverlayFocusRestore() {
223
+ this.overlayFocusRestore = { status: "inactive" };
224
+ }
225
+ clearOverlayFocusRestoreFor(overlay) {
226
+ if (this.overlayFocusRestore.status !== "inactive" && this.overlayFocusRestore.overlay === overlay) {
227
+ this.clearOverlayFocusRestore();
228
+ }
229
+ }
230
+ resolveBlockedOverlayFocusResume(restoreState) {
231
+ if (restoreState.resume.status === "restore-overlay")
232
+ return restoreState.overlay.component;
233
+ this.clearOverlayFocusRestore();
234
+ return restoreState.resume.target;
235
+ }
236
+ getVisibleOverlayFocusRestore() {
237
+ const restoreState = this.overlayFocusRestore;
238
+ if (restoreState.status === "inactive")
239
+ return restoreState;
240
+ if (!this.overlayStack.includes(restoreState.overlay) || !this.isOverlayVisible(restoreState.overlay)) {
241
+ return { status: "inactive" };
242
+ }
243
+ return restoreState;
244
+ }
245
+ isOverlayFocusAncestor(entry, component) {
246
+ const visited = new Set();
247
+ let current = entry.preFocus;
248
+ while (current && !visited.has(current)) {
249
+ visited.add(current);
250
+ if (current === component)
251
+ return true;
252
+ current = this.overlayStack.find((overlay) => overlay.component === current)?.preFocus ?? null;
253
+ }
254
+ return false;
255
+ }
256
+ retargetOverlayPreFocus(removed) {
257
+ for (const overlay of this.overlayStack) {
258
+ if (overlay !== removed && overlay.preFocus === removed.component) {
259
+ overlay.preFocus = removed.preFocus;
260
+ }
261
+ }
262
+ }
263
+ isComponentMounted(component) {
264
+ return this.children.some((child) => this.containsComponent(child, component));
265
+ }
266
+ containsComponent(root, target) {
267
+ if (root === target)
268
+ return true;
269
+ if (!(root instanceof Container))
270
+ return false;
271
+ return root.children.some((child) => this.containsComponent(child, target));
272
+ }
273
+ /**
274
+ * Show an overlay component with configurable positioning and sizing.
275
+ * Returns a handle to control the overlay's visibility.
276
+ */
277
+ showOverlay(component, options) {
278
+ const entry = {
279
+ component,
280
+ ...(options === undefined ? {} : { options }),
281
+ preFocus: this.focusedComponent,
282
+ hidden: false,
283
+ focusOrder: ++this.focusOrderCounter,
284
+ };
285
+ this.overlayStack.push(entry);
286
+ // Only focus if overlay is actually visible
287
+ if (!options?.nonCapturing && this.isOverlayVisible(entry)) {
288
+ this.setFocus(component);
289
+ }
290
+ this.terminal.hideCursor();
291
+ this.requestRender();
292
+ // Return handle for controlling this overlay
293
+ return {
294
+ hide: () => {
295
+ const index = this.overlayStack.indexOf(entry);
296
+ if (index !== -1) {
297
+ this.clearOverlayFocusRestoreFor(entry);
298
+ this.retargetOverlayPreFocus(entry);
299
+ this.overlayStack.splice(index, 1);
300
+ // Restore focus if this overlay had focus
301
+ if (this.focusedComponent === component) {
302
+ const topVisible = this.getTopmostVisibleOverlay();
303
+ this.setFocus(topVisible?.component ?? entry.preFocus);
304
+ }
305
+ if (this.overlayStack.length === 0)
306
+ this.terminal.hideCursor();
307
+ this.requestRender();
308
+ }
309
+ },
310
+ setHidden: (hidden) => {
311
+ if (entry.hidden === hidden)
312
+ return;
313
+ entry.hidden = hidden;
314
+ // Update focus when hiding/showing
315
+ if (hidden) {
316
+ this.clearOverlayFocusRestoreFor(entry);
317
+ // If this overlay had focus, move focus to next visible or preFocus
318
+ if (this.focusedComponent === component) {
319
+ const topVisible = this.getTopmostVisibleOverlay();
320
+ this.setFocus(topVisible?.component ?? entry.preFocus);
321
+ }
322
+ }
323
+ else {
324
+ // Restore focus to this overlay when showing (if it's actually visible)
325
+ if (!options?.nonCapturing && this.isOverlayVisible(entry)) {
326
+ entry.focusOrder = ++this.focusOrderCounter;
327
+ this.setFocus(component);
328
+ }
329
+ }
330
+ this.requestRender();
331
+ },
332
+ isHidden: () => entry.hidden,
333
+ focus: () => {
334
+ if (!this.overlayStack.includes(entry) || !this.isOverlayVisible(entry))
335
+ return;
336
+ entry.focusOrder = ++this.focusOrderCounter;
337
+ this.setFocus(component);
338
+ this.requestRender();
339
+ },
340
+ unfocus: (unfocusOptions) => {
341
+ const isFocused = this.focusedComponent === component;
342
+ const restoreState = this.overlayFocusRestore;
343
+ const hasPendingRestore = restoreState.status !== "inactive" && restoreState.overlay === entry;
344
+ if (!isFocused && !hasPendingRestore)
345
+ return;
346
+ if (restoreState.status === "blocked" &&
347
+ restoreState.overlay === entry &&
348
+ this.focusedComponent === restoreState.blockedBy) {
349
+ if (unfocusOptions) {
350
+ this.overlayFocusRestore = {
351
+ status: "blocked",
352
+ overlay: entry,
353
+ blockedBy: restoreState.blockedBy,
354
+ resume: { status: "focus-target", target: unfocusOptions.target },
355
+ };
356
+ }
357
+ else {
358
+ this.clearOverlayFocusRestore();
359
+ }
360
+ this.requestRender();
361
+ return;
362
+ }
363
+ this.clearOverlayFocusRestoreFor(entry);
364
+ if (isFocused || unfocusOptions) {
365
+ const topVisible = this.getTopmostVisibleOverlay();
366
+ const fallbackTarget = topVisible && topVisible !== entry ? topVisible.component : entry.preFocus;
367
+ this.setFocus(unfocusOptions ? unfocusOptions.target : fallbackTarget);
368
+ }
369
+ this.requestRender();
370
+ },
371
+ isFocused: () => this.focusedComponent === component,
372
+ };
373
+ }
374
+ /** Hide the topmost overlay and restore previous focus. */
375
+ hideOverlay() {
376
+ const overlay = this.overlayStack[this.overlayStack.length - 1];
377
+ if (!overlay)
378
+ return;
379
+ this.clearOverlayFocusRestoreFor(overlay);
380
+ this.retargetOverlayPreFocus(overlay);
381
+ this.overlayStack.pop();
382
+ if (this.focusedComponent === overlay.component) {
383
+ // Find topmost visible overlay, or fall back to preFocus
384
+ const topVisible = this.getTopmostVisibleOverlay();
385
+ this.setFocus(topVisible?.component ?? overlay.preFocus);
386
+ }
387
+ if (this.overlayStack.length === 0)
388
+ this.terminal.hideCursor();
389
+ this.requestRender();
390
+ }
391
+ /** Check if there are any visible overlays */
392
+ hasOverlay() {
393
+ return this.overlayStack.some((o) => this.isOverlayVisible(o));
394
+ }
395
+ /** Check if an overlay entry is currently visible */
396
+ isOverlayVisible(entry) {
397
+ if (entry.hidden)
398
+ return false;
399
+ if (entry.options?.visible) {
400
+ return entry.options.visible(this.terminal.columns, this.terminal.rows);
401
+ }
402
+ return true;
403
+ }
404
+ /** Find the visual-frontmost visible capturing overlay, if any */
405
+ getTopmostVisibleOverlay() {
406
+ let topmost;
407
+ for (const overlay of this.overlayStack) {
408
+ if (overlay.options?.nonCapturing || !this.isOverlayVisible(overlay))
409
+ continue;
410
+ if (!topmost || overlay.focusOrder > topmost.focusOrder) {
411
+ topmost = overlay;
412
+ }
413
+ }
414
+ return topmost;
415
+ }
416
+ invalidate() {
417
+ super.invalidate();
418
+ for (const overlay of this.overlayStack)
419
+ overlay.component.invalidate?.();
420
+ }
421
+ start() {
422
+ this.stopped = false;
423
+ this.terminal.start((data) => this.handleInput(data), () => this.requestRender());
424
+ this.terminal.hideCursor();
425
+ this.queryCellSize();
426
+ this.requestRender();
427
+ }
428
+ addInputListener(listener) {
429
+ this.inputListeners.add(listener);
430
+ return () => {
431
+ this.inputListeners.delete(listener);
432
+ };
433
+ }
434
+ removeInputListener(listener) {
435
+ this.inputListeners.delete(listener);
436
+ }
437
+ queryCellSize() {
438
+ // Only query if terminal supports images (cell size is only used for image rendering)
439
+ if (!getCapabilities().images) {
440
+ return;
441
+ }
442
+ // Query terminal for cell size in pixels: CSI 16 t
443
+ // Response format: CSI 6 ; height ; width t
444
+ this.terminal.write("\x1b[16t");
445
+ }
446
+ stop() {
447
+ this.stopped = true;
448
+ if (this.renderTimer) {
449
+ clearTimeout(this.renderTimer);
450
+ this.renderTimer = undefined;
451
+ }
452
+ if (this._scrollRegionActive) {
453
+ this.terminal.write("\x1b[r"); // reset scroll region
454
+ this._scrollRegionActive = false;
455
+ }
456
+ this.terminal.showCursor();
457
+ this.terminal.stop();
458
+ }
459
+ requestRender(force = false) {
460
+ if (force) {
461
+ this.previousLines = [];
462
+ this.previousWidth = -1; // -1 triggers widthChanged, forcing a full clear
463
+ this.previousHeight = -1; // -1 triggers heightChanged, forcing a full clear
464
+ this.cursorRow = 0;
465
+ this.hardwareCursorRow = 0;
466
+ this.maxLinesRendered = 0;
467
+ this.previousViewportTop = 0;
468
+ if (this.renderTimer) {
469
+ clearTimeout(this.renderTimer);
470
+ this.renderTimer = undefined;
471
+ }
472
+ this.renderRequested = true;
473
+ process.nextTick(() => {
474
+ if (this.stopped || !this.renderRequested) {
475
+ return;
476
+ }
477
+ this.renderRequested = false;
478
+ this.lastRenderAt = performance.now();
479
+ this.doRender();
480
+ });
481
+ return;
482
+ }
483
+ if (this.renderRequested)
484
+ return;
485
+ this.renderRequested = true;
486
+ process.nextTick(() => this.scheduleRender());
487
+ }
488
+ scheduleRender() {
489
+ if (this.stopped || this.renderTimer || !this.renderRequested) {
490
+ return;
491
+ }
492
+ const elapsed = performance.now() - this.lastRenderAt;
493
+ const delay = Math.max(0, TUI.MIN_RENDER_INTERVAL_MS - elapsed);
494
+ this.renderTimer = setTimeout(() => {
495
+ this.renderTimer = undefined;
496
+ if (this.stopped || !this.renderRequested) {
497
+ return;
498
+ }
499
+ this.renderRequested = false;
500
+ this.lastRenderAt = performance.now();
501
+ this.doRender();
502
+ if (this.renderRequested) {
503
+ this.scheduleRender();
504
+ }
505
+ }, delay);
506
+ }
507
+ handleInput(data) {
508
+ if (this.inputListeners.size > 0) {
509
+ let current = data;
510
+ for (const listener of this.inputListeners) {
511
+ const result = listener(current);
512
+ if (result?.consume) {
513
+ return;
514
+ }
515
+ if (result?.data !== undefined) {
516
+ current = result.data;
517
+ }
518
+ }
519
+ if (current.length === 0) {
520
+ return;
521
+ }
522
+ data = current;
523
+ }
524
+ // Consume terminal cell size responses without blocking unrelated input.
525
+ if (this.consumeCellSizeResponse(data)) {
526
+ return;
527
+ }
528
+ // Global debug key handler (Shift+Ctrl+D)
529
+ if (matchesKey(data, "shift+ctrl+d") && this.onDebug) {
530
+ this.onDebug();
531
+ return;
532
+ }
533
+ // If focused component is an overlay, verify it's still visible
534
+ // (visibility can change due to terminal resize or visible() callback)
535
+ const focusedOverlay = this.overlayStack.find((o) => o.component === this.focusedComponent);
536
+ if (focusedOverlay && !this.isOverlayVisible(focusedOverlay)) {
537
+ // Focused overlay is no longer visible, redirect to topmost visible overlay
538
+ const topVisible = this.getTopmostVisibleOverlay();
539
+ if (topVisible) {
540
+ this.setFocus(topVisible.component);
541
+ }
542
+ else {
543
+ this.setFocusInternal({ component: focusedOverlay.preFocus, overlayFocusRestore: "preserve" });
544
+ }
545
+ }
546
+ const focusIsOverlay = this.overlayStack.some((o) => o.component === this.focusedComponent);
547
+ if (!focusIsOverlay) {
548
+ const restoreState = this.getVisibleOverlayFocusRestore();
549
+ if (restoreState.status === "eligible") {
550
+ this.setFocus(restoreState.overlay.component);
551
+ }
552
+ else if (restoreState.status === "blocked" && restoreState.blockedBy !== this.focusedComponent) {
553
+ if (restoreState.resume.status === "restore-overlay") {
554
+ this.setFocus(restoreState.overlay.component);
555
+ }
556
+ else {
557
+ this.clearOverlayFocusRestore();
558
+ this.setFocus(restoreState.resume.target);
559
+ }
560
+ }
561
+ }
562
+ // Pass input to focused component (including Ctrl+C)
563
+ // The focused component can decide how to handle Ctrl+C
564
+ if (this.focusedComponent?.handleInput) {
565
+ // Filter out key release events unless component opts in
566
+ if (isKeyRelease(data) && !this.focusedComponent.wantsKeyRelease) {
567
+ return;
568
+ }
569
+ this.focusedComponent.handleInput(data);
570
+ this.requestRender();
571
+ }
572
+ }
573
+ consumeCellSizeResponse(data) {
574
+ // Response format: ESC [ 6 ; height ; width t
575
+ const match = data.match(/^\x1b\[6;(\d+);(\d+)t$/);
576
+ if (!match) {
577
+ return false;
578
+ }
579
+ const heightPx = parseInt(match[1], 10);
580
+ const widthPx = parseInt(match[2], 10);
581
+ if (heightPx <= 0 || widthPx <= 0) {
582
+ return true;
583
+ }
584
+ setCellDimensions({ widthPx, heightPx });
585
+ // Invalidate all components so images re-render with correct dimensions.
586
+ this.invalidate();
587
+ this.requestRender();
588
+ return true;
589
+ }
590
+ /**
591
+ * Resolve overlay layout from options.
592
+ * Returns { width, row, col, maxHeight } for rendering.
593
+ */
594
+ resolveOverlayLayout(options, overlayHeight, termWidth, termHeight) {
595
+ const opt = options ?? {};
596
+ // Parse margin (clamp to non-negative)
597
+ const margin = typeof opt.margin === "number"
598
+ ? { top: opt.margin, right: opt.margin, bottom: opt.margin, left: opt.margin }
599
+ : (opt.margin ?? {});
600
+ const marginTop = Math.max(0, margin.top ?? 0);
601
+ const marginRight = Math.max(0, margin.right ?? 0);
602
+ const marginBottom = Math.max(0, margin.bottom ?? 0);
603
+ const marginLeft = Math.max(0, margin.left ?? 0);
604
+ // Available space after margins
605
+ const availWidth = Math.max(1, termWidth - marginLeft - marginRight);
606
+ const availHeight = Math.max(1, termHeight - marginTop - marginBottom);
607
+ // === Resolve width ===
608
+ let width = parseSizeValue(opt.width, termWidth) ?? availWidth;
609
+ // Apply minWidth
610
+ if (opt.minWidth !== undefined) {
611
+ width = Math.max(width, opt.minWidth);
612
+ }
613
+ // Clamp to available space
614
+ width = Math.max(1, Math.min(width, availWidth));
615
+ // === Resolve maxHeight ===
616
+ let maxHeight = parseSizeValue(opt.maxHeight, termHeight);
617
+ // Clamp to available space
618
+ if (maxHeight !== undefined) {
619
+ maxHeight = Math.max(1, Math.min(maxHeight, availHeight));
620
+ }
621
+ // Effective overlay height (may be clamped by maxHeight)
622
+ const effectiveHeight = maxHeight !== undefined ? Math.min(overlayHeight, maxHeight) : overlayHeight;
623
+ // === Resolve position ===
624
+ let row;
625
+ let col;
626
+ if (opt.row !== undefined) {
627
+ if (typeof opt.row === "string") {
628
+ // Percentage: 0% = top, 100% = bottom (overlay stays within bounds)
629
+ const match = opt.row.match(/^(\d+(?:\.\d+)?)%$/);
630
+ if (match) {
631
+ const maxRow = Math.max(0, availHeight - effectiveHeight);
632
+ const percent = parseFloat(match[1]) / 100;
633
+ row = marginTop + Math.floor(maxRow * percent);
634
+ }
635
+ else {
636
+ // Invalid format, fall back to center
637
+ row = this.resolveAnchorRow("center", effectiveHeight, availHeight, marginTop);
638
+ }
639
+ }
640
+ else {
641
+ // Absolute row position
642
+ row = opt.row;
643
+ }
644
+ }
645
+ else {
646
+ // Anchor-based (default: center)
647
+ const anchor = opt.anchor ?? "center";
648
+ row = this.resolveAnchorRow(anchor, effectiveHeight, availHeight, marginTop);
649
+ }
650
+ if (opt.col !== undefined) {
651
+ if (typeof opt.col === "string") {
652
+ // Percentage: 0% = left, 100% = right (overlay stays within bounds)
653
+ const match = opt.col.match(/^(\d+(?:\.\d+)?)%$/);
654
+ if (match) {
655
+ const maxCol = Math.max(0, availWidth - width);
656
+ const percent = parseFloat(match[1]) / 100;
657
+ col = marginLeft + Math.floor(maxCol * percent);
658
+ }
659
+ else {
660
+ // Invalid format, fall back to center
661
+ col = this.resolveAnchorCol("center", width, availWidth, marginLeft);
662
+ }
663
+ }
664
+ else {
665
+ // Absolute column position
666
+ col = opt.col;
667
+ }
668
+ }
669
+ else {
670
+ // Anchor-based (default: center)
671
+ const anchor = opt.anchor ?? "center";
672
+ col = this.resolveAnchorCol(anchor, width, availWidth, marginLeft);
673
+ }
674
+ // Apply offsets
675
+ if (opt.offsetY !== undefined)
676
+ row += opt.offsetY;
677
+ if (opt.offsetX !== undefined)
678
+ col += opt.offsetX;
679
+ // Clamp to terminal bounds (respecting margins)
680
+ row = Math.max(marginTop, Math.min(row, termHeight - marginBottom - effectiveHeight));
681
+ col = Math.max(marginLeft, Math.min(col, termWidth - marginRight - width));
682
+ return { width, row, col, maxHeight };
683
+ }
684
+ resolveAnchorRow(anchor, height, availHeight, marginTop) {
685
+ switch (anchor) {
686
+ case "top-left":
687
+ case "top-center":
688
+ case "top-right":
689
+ return marginTop;
690
+ case "bottom-left":
691
+ case "bottom-center":
692
+ case "bottom-right":
693
+ return marginTop + availHeight - height;
694
+ case "left-center":
695
+ case "center":
696
+ case "right-center":
697
+ return marginTop + Math.floor((availHeight - height) / 2);
698
+ }
699
+ }
700
+ resolveAnchorCol(anchor, width, availWidth, marginLeft) {
701
+ switch (anchor) {
702
+ case "top-left":
703
+ case "left-center":
704
+ case "bottom-left":
705
+ return marginLeft;
706
+ case "top-right":
707
+ case "right-center":
708
+ case "bottom-right":
709
+ return marginLeft + availWidth - width;
710
+ case "top-center":
711
+ case "center":
712
+ case "bottom-center":
713
+ return marginLeft + Math.floor((availWidth - width) / 2);
714
+ }
715
+ }
716
+ /** Composite all overlays into content lines (sorted by focusOrder, higher = on top). */
717
+ compositeOverlays(lines, termWidth, termHeight) {
718
+ if (this.overlayStack.length === 0)
719
+ return lines;
720
+ const result = [...lines];
721
+ // Pre-render all visible overlays and calculate positions
722
+ const rendered = [];
723
+ let minLinesNeeded = result.length;
724
+ const visibleEntries = this.overlayStack.filter((e) => this.isOverlayVisible(e));
725
+ visibleEntries.sort((a, b) => a.focusOrder - b.focusOrder);
726
+ for (const entry of visibleEntries) {
727
+ const { component, options } = entry;
728
+ // Get layout with height=0 first to determine width and maxHeight
729
+ // (width and maxHeight don't depend on overlay height)
730
+ const { width, maxHeight } = this.resolveOverlayLayout(options, 0, termWidth, termHeight);
731
+ // Render component at calculated width
732
+ let overlayLines = component.render(width);
733
+ // Apply maxHeight if specified
734
+ if (maxHeight !== undefined && overlayLines.length > maxHeight) {
735
+ overlayLines = overlayLines.slice(0, maxHeight);
736
+ }
737
+ // Get final row/col with actual overlay height
738
+ const { row, col } = this.resolveOverlayLayout(options, overlayLines.length, termWidth, termHeight);
739
+ rendered.push({ overlayLines, row, col, w: width });
740
+ minLinesNeeded = Math.max(minLinesNeeded, row + overlayLines.length);
741
+ }
742
+ // Pad to at least terminal height so overlays have screen-relative positions.
743
+ // Excludes maxLinesRendered: the historical high-water mark caused self-reinforcing
744
+ // inflation that pushed content into scrollback on terminal widen.
745
+ const workingHeight = Math.max(result.length, termHeight, minLinesNeeded);
746
+ // Extend result with empty lines if content is too short for overlay placement or working area
747
+ while (result.length < workingHeight) {
748
+ result.push("");
749
+ }
750
+ const viewportStart = Math.max(0, workingHeight - termHeight);
751
+ // Composite each overlay
752
+ for (const { overlayLines, row, col, w } of rendered) {
753
+ for (let i = 0; i < overlayLines.length; i++) {
754
+ const idx = viewportStart + row + i;
755
+ if (idx >= 0 && idx < result.length) {
756
+ // Defensive: truncate overlay line to declared width before compositing
757
+ // (components should already respect width, but this ensures it)
758
+ const truncatedOverlayLine = visibleWidth(overlayLines[i]) > w ? sliceByColumn(overlayLines[i], 0, w, true) : overlayLines[i];
759
+ result[idx] = this.compositeLineAt(result[idx], truncatedOverlayLine, col, w, termWidth);
760
+ }
761
+ }
762
+ }
763
+ return result;
764
+ }
765
+ // pi-coding-master: OSC 8 close (\x1b]8;;\x07) 在某些终端会渲染成空行,只保留 SGR reset
766
+ static SEGMENT_RESET = "\x1b[0m";
767
+ // pi-coding-master: 折行缓存——内容+宽度没变就跳过 wrapHanging(修复 CPU 100% 空转)
768
+ _wrapCache = { width: 0, result: null, ref: null };
769
+ wrapLinesToWidth(lines, width) {
770
+ if (this._wrapCache.width === width && this._wrapCache.ref === lines) {
771
+ return this._wrapCache.result;
772
+ }
773
+ const result = wrapHanging(lines, width, { visibleWidth, sliceWithWidth, sliceByColumn, isImageLine });
774
+ this._wrapCache = { width, result, ref: lines };
775
+ return result;
776
+ }
777
+ applyLineResets(lines) {
778
+ const reset = TUI.SEGMENT_RESET;
779
+ for (let i = 0; i < lines.length; i++) {
780
+ // pi-coding-master 防御:某组件偶发吐出非字符串行(漏括号的方法引用 / undefined / 嵌套数组)时强制转成字符串。
781
+ // 绝不让一行坏数据崩掉整个渲染循环 —— 那等于崩掉一个一直活着的 agent。
782
+ const line = typeof lines[i] === "string" ? lines[i] : (lines[i] = String(lines[i] ?? ""));
783
+ if (!isImageLine(line)) {
784
+ lines[i] = normalizeTerminalOutput(line) + reset;
785
+ }
786
+ }
787
+ return lines;
788
+ }
789
+ collectKittyImageIds(lines) {
790
+ const ids = new Set();
791
+ for (const line of lines) {
792
+ for (const id of extractKittyImageIds(line)) {
793
+ ids.add(id);
794
+ }
795
+ }
796
+ return ids;
797
+ }
798
+ deleteKittyImages(ids) {
799
+ let buffer = "";
800
+ for (const id of ids) {
801
+ buffer += deleteKittyImage(id);
802
+ }
803
+ return buffer;
804
+ }
805
+ expandLastChangedForKittyImages(firstChanged, lastChanged) {
806
+ let expandedLastChanged = lastChanged;
807
+ for (let i = firstChanged; i < this.previousLines.length; i++) {
808
+ if (extractKittyImageIds(this.previousLines[i]).length > 0) {
809
+ expandedLastChanged = Math.max(expandedLastChanged, i);
810
+ }
811
+ }
812
+ return expandedLastChanged;
813
+ }
814
+ deleteChangedKittyImages(firstChanged, lastChanged) {
815
+ if (firstChanged < 0 || lastChanged < firstChanged)
816
+ return "";
817
+ const ids = new Set();
818
+ const maxLine = Math.min(lastChanged, this.previousLines.length - 1);
819
+ for (let i = firstChanged; i <= maxLine; i++) {
820
+ for (const id of extractKittyImageIds(this.previousLines[i] ?? "")) {
821
+ ids.add(id);
822
+ }
823
+ }
824
+ return this.deleteKittyImages(ids);
825
+ }
826
+ /** Splice overlay content into a base line at a specific column. Single-pass optimized. */
827
+ compositeLineAt(baseLine, overlayLine, startCol, overlayWidth, totalWidth) {
828
+ if (isImageLine(baseLine))
829
+ return baseLine;
830
+ // Single pass through baseLine extracts both before and after segments
831
+ const afterStart = startCol + overlayWidth;
832
+ const base = extractSegments(baseLine, startCol, afterStart, totalWidth - afterStart, true);
833
+ // Extract overlay with width tracking (strict=true to exclude wide chars at boundary)
834
+ const overlay = sliceWithWidth(overlayLine, 0, overlayWidth, true);
835
+ // Pad segments to target widths
836
+ const beforePad = Math.max(0, startCol - base.beforeWidth);
837
+ const overlayPad = Math.max(0, overlayWidth - overlay.width);
838
+ const actualBeforeWidth = Math.max(startCol, base.beforeWidth);
839
+ const actualOverlayWidth = Math.max(overlayWidth, overlay.width);
840
+ const afterTarget = Math.max(0, totalWidth - actualBeforeWidth - actualOverlayWidth);
841
+ const afterPad = Math.max(0, afterTarget - base.afterWidth);
842
+ // Compose result
843
+ const r = TUI.SEGMENT_RESET;
844
+ const result = base.before +
845
+ " ".repeat(beforePad) +
846
+ r +
847
+ overlay.text +
848
+ " ".repeat(overlayPad) +
849
+ r +
850
+ base.after +
851
+ " ".repeat(afterPad);
852
+ // CRITICAL: Always verify and truncate to terminal width.
853
+ // This is the final safeguard against width overflow which would crash the TUI.
854
+ // Width tracking can drift from actual visible width due to:
855
+ // - Complex ANSI/OSC sequences (hyperlinks, colors)
856
+ // - Wide characters at segment boundaries
857
+ // - Edge cases in segment extraction
858
+ const resultWidth = visibleWidth(result);
859
+ if (resultWidth <= totalWidth) {
860
+ return result;
861
+ }
862
+ // Truncate with strict=true to ensure we don't exceed totalWidth
863
+ return sliceByColumn(result, 0, totalWidth, true);
864
+ }
865
+ /**
866
+ * Find and extract cursor position from rendered lines.
867
+ * Searches for CURSOR_MARKER, calculates its position, and strips it from the output.
868
+ * Only scans the bottom terminal height lines (visible viewport).
869
+ * @param lines - Rendered lines to search
870
+ * @param height - Terminal height (visible viewport size)
871
+ * @returns Cursor position { row, col } or null if no marker found
872
+ */
873
+ extractCursorPosition(lines, height) {
874
+ // Only scan the bottom `height` lines (visible viewport)
875
+ const viewportTop = Math.max(0, lines.length - height);
876
+ for (let row = lines.length - 1; row >= viewportTop; row--) {
877
+ const line = lines[row];
878
+ const markerIndex = line.indexOf(CURSOR_MARKER);
879
+ if (markerIndex !== -1) {
880
+ // Calculate visual column (width of text before marker)
881
+ const beforeMarker = line.slice(0, markerIndex);
882
+ const col = visibleWidth(beforeMarker);
883
+ // Strip marker from the line
884
+ lines[row] = line.slice(0, markerIndex) + line.slice(markerIndex + CURSOR_MARKER.length);
885
+ return { row, col };
886
+ }
887
+ }
888
+ return null;
889
+ }
890
+ doRender() {
891
+ // ── pi-coding-master 自修复引擎 · 渲染边界 ──────────────────────────────────────
892
+ // 渲染层【任何】throw 都不许崩掉 pi(一个一直活着的 agent)。今天的"超宽行 throw"只是其一;
893
+ // 任意自定义组件 render() 抛错也走这里。策略:记日志(响,不静默)→ 跳过本帧 → 重置 previousLines
894
+ // 让下一帧整屏重画、从坏状态重新同步。绝不 rethrow。
895
+ try {
896
+ this._doRenderUnsafe();
897
+ } catch (e) {
898
+ try {
899
+ fs.appendFileSync(path.join(os.homedir(), ".pi", "agent", "pi-render-errors.log"), `[${new Date().toISOString()}] [render] ${(e && e.stack) ? e.stack : e}\n`);
900
+ } catch { }
901
+ this.previousLines = []; // 下帧全重画,重新同步,别卡死在坏状态
902
+ }
903
+ }
904
+ _doRenderUnsafe() {
905
+ if (this.stopped)
906
+ return;
907
+ const width = this.terminal.columns;
908
+ const height = this.terminal.rows;
909
+ const widthChanged = this.previousWidth !== 0 && this.previousWidth !== width;
910
+ const heightChanged = this.previousHeight !== 0 && this.previousHeight !== height;
911
+ const previousBufferLength = this.previousHeight > 0 ? this.previousViewportTop + this.previousHeight : height;
912
+ let prevViewportTop = heightChanged ? Math.max(0, previousBufferLength - height) : this.previousViewportTop;
913
+ let viewportTop = prevViewportTop;
914
+ let hardwareCursorRow = this.hardwareCursorRow;
915
+ const computeLineDiff = (targetRow) => {
916
+ const currentScreenRow = hardwareCursorRow - prevViewportTop;
917
+ const targetScreenRow = targetRow - viewportTop;
918
+ return targetScreenRow - currentScreenRow;
919
+ };
920
+ // Render all components to get new lines
921
+ let newLines = this.render(width);
922
+ // Composite overlays into the rendered lines (before differential compare)
923
+ if (this.overlayStack.length > 0) {
924
+ newLines = this.compositeOverlays(newLines, width, height);
925
+ }
926
+ // pi-coding-master: 超宽的行在这里【折行】成多行(替代下面"超宽即抛错崩进程")。
927
+ // 必须在 cursor/diff/清屏 用到行数之前做 —— 之后所有逻辑都基于折行后的行数,一致、不错位。
928
+ newLines = this.wrapLinesToWidth(newLines, width);
929
+ // Compute pinned footer height for scroll region (last child = footer)
930
+ const pb = this._lastChildLines || 0;
931
+ this._pinnedBottom = pb;
932
+ // Extract cursor position before applying line resets (marker must be found first)
933
+ const cursorPos = this.extractCursorPosition(newLines, height);
934
+ newLines = this.applyLineResets(newLines);
935
+ // Helper to render all new lines (overwrite-in-place, no blank-screen flash)
936
+ const fullRender = (clear) => {
937
+ this.fullRedrawCount += 1;
938
+ let buffer = "\x1b[?2026h"; // Begin synchronized output
939
+ if (clear) {
940
+ buffer += this.deleteKittyImages(this.previousKittyImageIds);
941
+ if (widthChanged || heightChanged) {
942
+ buffer += "\x1b[2J\x1b[H\x1b[3J"; // Resize: full clear needed
943
+ } else {
944
+ buffer += "\x1b[H"; // Non-resize: overwrite in place
945
+ }
946
+ }
947
+ for (let i = 0; i < newLines.length; i++) {
948
+ if (i > 0)
949
+ buffer += "\r\n";
950
+ buffer += "\x1b[2K"; // Clear this line then write — never blank the whole screen
951
+ buffer += newLines[i];
952
+ }
953
+ // Clear leftover lines from previous render (content shrunk)
954
+ const prevLen = this.previousLines.length;
955
+ if (clear && prevLen > newLines.length) {
956
+ for (let i = newLines.length; i < prevLen && i < newLines.length + height; i++) {
957
+ buffer += "\r\n\x1b[2K";
958
+ }
959
+ }
960
+ buffer += "\x1b[?2026l"; // End synchronized output
961
+ this.terminal.write(buffer);
962
+ this.cursorRow = Math.max(0, newLines.length - 1);
963
+ this.hardwareCursorRow = this.cursorRow;
964
+ // Reset max lines when clearing, otherwise track growth
965
+ if (clear) {
966
+ this.maxLinesRendered = newLines.length;
967
+ }
968
+ else {
969
+ this.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length);
970
+ }
971
+ const bufferLength = Math.max(height, newLines.length);
972
+ this.previousViewportTop = Math.max(0, bufferLength - height);
973
+ this.positionHardwareCursor(cursorPos, newLines.length);
974
+ this.previousLines = newLines;
975
+ this.previousKittyImageIds = this.collectKittyImageIds(newLines);
976
+ this.previousWidth = width;
977
+ this.previousHeight = height;
978
+ };
979
+ const debugRedraw = process.env.PI_DEBUG_REDRAW === "1" || fs.existsSync("/tmp/pi-tui-debug");
980
+ const logRedraw = (reason) => {
981
+ if (!debugRedraw)
982
+ return;
983
+ const logPath = path.join(os.homedir(), ".pi", "agent", "pi-debug.log");
984
+ const msg = `[${new Date().toISOString()}] fullRender: ${reason} (prev=${this.previousLines.length}, new=${newLines.length}, height=${height})\n`;
985
+ fs.appendFileSync(logPath, msg);
986
+ };
987
+ // First render - just output everything without clearing (assumes clean screen)
988
+ if (this.previousLines.length === 0 && !widthChanged && !heightChanged) {
989
+ logRedraw("first render");
990
+ fullRender(false);
991
+ return;
992
+ }
993
+ // Width changes always need a full re-render because wrapping changes.
994
+ if (widthChanged) {
995
+ logRedraw(`terminal width changed (${this.previousWidth} -> ${width})`);
996
+ fullRender(true);
997
+ return;
998
+ }
999
+ // Height changes normally need a full re-render to keep the visible viewport aligned,
1000
+ // but Termux changes height when the software keyboard shows or hides.
1001
+ // In that environment, a full redraw causes the entire history to replay on every toggle.
1002
+ if (heightChanged && !isTermuxSession()) {
1003
+ logRedraw(`terminal height changed (${this.previousHeight} -> ${height})`);
1004
+ fullRender(true);
1005
+ return;
1006
+ }
1007
+ // Content shrunk below the working area and no overlays - re-render to clear empty rows
1008
+ // (overlays need the padding, so only do this when no overlays are active)
1009
+ // Configurable via setClearOnShrink() or PI_CLEAR_ON_SHRINK=0 env var
1010
+ if (this.clearOnShrink && newLines.length < this.maxLinesRendered && this.overlayStack.length === 0) {
1011
+ logRedraw(`clearOnShrink (maxLinesRendered=${this.maxLinesRendered})`);
1012
+ fullRender(true);
1013
+ return;
1014
+ }
1015
+ // Find first and last changed lines
1016
+ let firstChanged = -1;
1017
+ let lastChanged = -1;
1018
+ const maxLines = Math.max(newLines.length, this.previousLines.length);
1019
+ for (let i = 0; i < maxLines; i++) {
1020
+ const oldLine = i < this.previousLines.length ? this.previousLines[i] : "";
1021
+ const newLine = i < newLines.length ? newLines[i] : "";
1022
+ if (oldLine !== newLine) {
1023
+ if (firstChanged === -1) {
1024
+ firstChanged = i;
1025
+ }
1026
+ lastChanged = i;
1027
+ }
1028
+ }
1029
+ const appendedLines = newLines.length > this.previousLines.length;
1030
+ if (appendedLines) {
1031
+ if (firstChanged === -1) {
1032
+ firstChanged = this.previousLines.length;
1033
+ }
1034
+ lastChanged = newLines.length - 1;
1035
+ }
1036
+ if (firstChanged !== -1) {
1037
+ lastChanged = this.expandLastChangedForKittyImages(firstChanged, lastChanged);
1038
+ }
1039
+ const appendStart = appendedLines && firstChanged === this.previousLines.length && firstChanged > 0;
1040
+ // No changes - but still need to update hardware cursor position if it moved
1041
+ if (firstChanged === -1) {
1042
+ this.positionHardwareCursor(cursorPos, newLines.length);
1043
+ this.previousViewportTop = prevViewportTop;
1044
+ this.previousHeight = height;
1045
+ return;
1046
+ }
1047
+ // All changes are in deleted lines (nothing to render, just clear)
1048
+ if (firstChanged >= newLines.length) {
1049
+ if (this.previousLines.length > newLines.length) {
1050
+ let buffer = "\x1b[?2026h";
1051
+ buffer += this.deleteChangedKittyImages(firstChanged, lastChanged);
1052
+ // Move to end of new content (clamp to 0 for empty content)
1053
+ const targetRow = Math.max(0, newLines.length - 1);
1054
+ if (targetRow < prevViewportTop) {
1055
+ logRedraw(`deleted lines moved viewport up (${targetRow} < ${prevViewportTop})`);
1056
+ fullRender(true);
1057
+ return;
1058
+ }
1059
+ const lineDiff = computeLineDiff(targetRow);
1060
+ if (lineDiff > 0)
1061
+ buffer += `\x1b[${lineDiff}B`;
1062
+ else if (lineDiff < 0)
1063
+ buffer += `\x1b[${-lineDiff}A`;
1064
+ buffer += "\r";
1065
+ // Clear extra lines without scrolling
1066
+ const extraLines = this.previousLines.length - newLines.length;
1067
+ if (extraLines > height) {
1068
+ logRedraw(`extraLines > height (${extraLines} > ${height})`);
1069
+ fullRender(true);
1070
+ return;
1071
+ }
1072
+ const clearStartOffset = newLines.length === 0 ? 0 : 1;
1073
+ if (extraLines > 0 && clearStartOffset > 0) {
1074
+ buffer += `\x1b[${clearStartOffset}B`;
1075
+ }
1076
+ for (let i = 0; i < extraLines; i++) {
1077
+ buffer += "\r\x1b[2K";
1078
+ if (i < extraLines - 1)
1079
+ buffer += "\x1b[1B";
1080
+ }
1081
+ const moveBack = Math.max(0, extraLines - 1 + clearStartOffset);
1082
+ if (moveBack > 0) {
1083
+ buffer += `\x1b[${moveBack}A`;
1084
+ }
1085
+ buffer += "\x1b[?2026l";
1086
+ this.terminal.write(buffer);
1087
+ this.cursorRow = targetRow;
1088
+ this.hardwareCursorRow = targetRow;
1089
+ }
1090
+ this.positionHardwareCursor(cursorPos, newLines.length);
1091
+ this.previousLines = newLines;
1092
+ this.previousKittyImageIds = this.collectKittyImageIds(newLines);
1093
+ this.previousWidth = width;
1094
+ this.previousHeight = height;
1095
+ this.previousViewportTop = prevViewportTop;
1096
+ return;
1097
+ }
1098
+ // Differential rendering can only touch what was actually visible.
1099
+ // If the first changed line is above the previous viewport, those changed
1100
+ // lines have scrolled off-screen (into scrollback) — the user cannot see them.
1101
+ // A full redraw here just to repaint invisible lines clears+homes the whole
1102
+ // screen = a jarring "jump to top" on every retroactive history change
1103
+ // (tool cards finalizing, markdown reflow, injected/subconscious messages).
1104
+ // Instead: if nothing visible changed, only update bookkeeping; otherwise
1105
+ // clamp the repaint to the top of the visible viewport and redraw in place.
1106
+ // Scrollback may drift slightly above the fold — far preferable to a constant jump.
1107
+ // (Set PI_FULL_REDRAW_ABOVE_FOLD=1 to restore the old full-redraw behavior.)
1108
+ if (firstChanged < prevViewportTop) {
1109
+ if (process.env.PI_FULL_REDRAW_ABOVE_FOLD === "1") {
1110
+ logRedraw(`firstChanged < viewportTop (${firstChanged} < ${prevViewportTop})`);
1111
+ fullRender(true);
1112
+ return;
1113
+ }
1114
+ if (!appendedLines && lastChanged < prevViewportTop) {
1115
+ // Entire change is above the fold — nothing on screen to repaint.
1116
+ this.positionHardwareCursor(cursorPos, newLines.length);
1117
+ this.previousLines = newLines;
1118
+ this.previousKittyImageIds = this.collectKittyImageIds(newLines);
1119
+ this.previousWidth = width;
1120
+ this.previousHeight = height;
1121
+ this.previousViewportTop = prevViewportTop;
1122
+ return;
1123
+ }
1124
+ firstChanged = prevViewportTop;
1125
+ }
1126
+ // Render from first changed line to end
1127
+ // Build buffer with all updates wrapped in synchronized output
1128
+ let buffer = "\x1b[?2026h"; // Begin synchronized output
1129
+ buffer += this.deleteChangedKittyImages(firstChanged, lastChanged);
1130
+ const prevViewportBottom = prevViewportTop + height - 1;
1131
+ const moveTargetRow = appendStart ? firstChanged - 1 : firstChanged;
1132
+ if (moveTargetRow > prevViewportBottom) {
1133
+ const currentScreenRow = Math.max(0, Math.min(height - 1, hardwareCursorRow - prevViewportTop));
1134
+ const moveToBottom = height - 1 - currentScreenRow;
1135
+ if (moveToBottom > 0) {
1136
+ buffer += `\x1b[${moveToBottom}B`;
1137
+ }
1138
+ const scroll = moveTargetRow - prevViewportBottom;
1139
+ buffer += "\r\n".repeat(scroll);
1140
+ prevViewportTop += scroll;
1141
+ viewportTop += scroll;
1142
+ hardwareCursorRow = moveTargetRow;
1143
+ }
1144
+ // Move cursor to first changed line (use hardwareCursorRow for actual position)
1145
+ const lineDiff = computeLineDiff(moveTargetRow);
1146
+ if (lineDiff > 0) {
1147
+ buffer += `\x1b[${lineDiff}B`; // Move down
1148
+ }
1149
+ else if (lineDiff < 0) {
1150
+ buffer += `\x1b[${-lineDiff}A`; // Move up
1151
+ }
1152
+ buffer += appendStart ? "\r\n" : "\r"; // Move to column 0
1153
+ // Only render changed lines (firstChanged to lastChanged), not all lines to end
1154
+ // This reduces flicker when only a single line changes (e.g., spinner animation)
1155
+ const renderEnd = Math.min(lastChanged, newLines.length - 1);
1156
+ for (let i = firstChanged; i <= renderEnd; i++) {
1157
+ if (i > firstChanged)
1158
+ buffer += "\r\n";
1159
+ buffer += "\x1b[2K"; // Clear current line
1160
+ let line = newLines[i];
1161
+ const isImage = isImageLine(line);
1162
+ if (!isImage && visibleWidth(line) > width) {
1163
+ line = sliceByColumn(line, 0, width, true);
1164
+ }
1165
+ buffer += line;
1166
+ }
1167
+ // Track where cursor ended up after rendering
1168
+ let finalCursorRow = renderEnd;
1169
+ // If we had more lines before, clear them and move cursor back
1170
+ if (this.previousLines.length > newLines.length) {
1171
+ // Move to end of new content first if we stopped before it
1172
+ if (renderEnd < newLines.length - 1) {
1173
+ const moveDown = newLines.length - 1 - renderEnd;
1174
+ buffer += `\x1b[${moveDown}B`;
1175
+ finalCursorRow = newLines.length - 1;
1176
+ }
1177
+ const extraLines = this.previousLines.length - newLines.length;
1178
+ for (let i = newLines.length; i < this.previousLines.length; i++) {
1179
+ buffer += "\r\n\x1b[2K";
1180
+ }
1181
+ // Move cursor back to end of new content
1182
+ buffer += `\x1b[${extraLines}A`;
1183
+ }
1184
+ buffer += "\x1b[?2026l"; // End synchronized output
1185
+ if (process.env.PI_TUI_DEBUG === "1" || fs.existsSync("/tmp/pi-tui-debug")) {
1186
+ const debugDir = "/tmp/tui";
1187
+ fs.mkdirSync(debugDir, { recursive: true });
1188
+ const debugPath = path.join(debugDir, `render-${Date.now()}-${Math.random().toString(36).slice(2)}.log`);
1189
+ const debugData = [
1190
+ `firstChanged: ${firstChanged}`,
1191
+ `viewportTop: ${viewportTop}`,
1192
+ `cursorRow: ${this.cursorRow}`,
1193
+ `height: ${height}`,
1194
+ `lineDiff: ${lineDiff}`,
1195
+ `hardwareCursorRow: ${hardwareCursorRow}`,
1196
+ `renderEnd: ${renderEnd}`,
1197
+ `finalCursorRow: ${finalCursorRow}`,
1198
+ `cursorPos: ${JSON.stringify(cursorPos)}`,
1199
+ `newLines.length: ${newLines.length}`,
1200
+ `previousLines.length: ${this.previousLines.length}`,
1201
+ "",
1202
+ "=== newLines ===",
1203
+ JSON.stringify(newLines, null, 2),
1204
+ "",
1205
+ "=== previousLines ===",
1206
+ JSON.stringify(this.previousLines, null, 2),
1207
+ "",
1208
+ "=== buffer ===",
1209
+ JSON.stringify(buffer),
1210
+ ].join("\n");
1211
+ fs.writeFileSync(debugPath, debugData);
1212
+ }
1213
+ // Write entire buffer at once
1214
+ this.terminal.write(buffer);
1215
+ // Track cursor position for next render
1216
+ // cursorRow tracks end of content (for viewport calculation)
1217
+ // hardwareCursorRow tracks actual terminal cursor position (for movement)
1218
+ this.cursorRow = Math.max(0, newLines.length - 1);
1219
+ this.hardwareCursorRow = finalCursorRow;
1220
+ // Track terminal's working area (grows but doesn't shrink unless cleared)
1221
+ this.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length);
1222
+ this.previousViewportTop = Math.max(prevViewportTop, finalCursorRow - height + 1);
1223
+ // Position hardware cursor for IME
1224
+ this.positionHardwareCursor(cursorPos, newLines.length);
1225
+ this.previousLines = newLines;
1226
+ this.previousKittyImageIds = this.collectKittyImageIds(newLines);
1227
+ this.previousWidth = width;
1228
+ this.previousHeight = height;
1229
+ }
1230
+ /**
1231
+ * Position the hardware cursor for IME candidate window.
1232
+ * @param cursorPos The cursor position extracted from rendered output, or null
1233
+ * @param totalLines Total number of rendered lines
1234
+ */
1235
+ positionHardwareCursor(cursorPos, totalLines) {
1236
+ if (!cursorPos || totalLines <= 0) {
1237
+ this.terminal.hideCursor();
1238
+ return;
1239
+ }
1240
+ // Clamp cursor position to valid range
1241
+ const targetRow = Math.max(0, Math.min(cursorPos.row, totalLines - 1));
1242
+ const targetCol = Math.max(0, cursorPos.col);
1243
+ // Move cursor from current position to target
1244
+ const rowDelta = targetRow - this.hardwareCursorRow;
1245
+ let buffer = "";
1246
+ if (rowDelta > 0) {
1247
+ buffer += `\x1b[${rowDelta}B`; // Move down
1248
+ }
1249
+ else if (rowDelta < 0) {
1250
+ buffer += `\x1b[${-rowDelta}A`; // Move up
1251
+ }
1252
+ // Move to absolute column (1-indexed)
1253
+ buffer += `\x1b[${targetCol + 1}G`;
1254
+ if (buffer) {
1255
+ this.terminal.write(buffer);
1256
+ }
1257
+ this.hardwareCursorRow = targetRow;
1258
+ if (this.showHardwareCursor) {
1259
+ this.terminal.showCursor();
1260
+ }
1261
+ else {
1262
+ this.terminal.hideCursor();
1263
+ }
1264
+ }
1265
+ }
1266
+ //# sourceMappingURL=tui.js.map