@firstpick/pi-package-webui 0.11.0 → 0.11.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (318) hide show
  1. package/DEVELOPMENT.md +181 -12
  2. package/README.md +41 -9
  3. package/TECHNICAL.md +91 -8
  4. package/bin/pi-webui.mjs +5432 -11428
  5. package/index.ts +10 -3
  6. package/lib/codex-cli-usage.mjs +232 -0
  7. package/lib/codex-usage-auth.mjs +39 -3
  8. package/lib/codex-usage-source.mjs +36 -0
  9. package/lib/component-update-state.mjs +2 -2
  10. package/lib/git-workflow-preference-values.mjs +122 -0
  11. package/lib/git-workflow-preferences.mjs +35 -563
  12. package/lib/package-updates.mjs +260 -0
  13. package/lib/rpc-supervisor-client.mjs +8 -4
  14. package/lib/server/app-runners.mjs +1667 -0
  15. package/lib/server/codex-fast-mode.mjs +161 -0
  16. package/lib/server/conversation-mode.mjs +218 -0
  17. package/lib/server/conversation-voice.mjs +318 -0
  18. package/lib/server/extension-ui-requests.mjs +114 -0
  19. package/lib/server/git-generation.mjs +459 -0
  20. package/lib/server/git-mutations.mjs +1827 -0
  21. package/lib/server/git-reads.mjs +608 -0
  22. package/lib/server/git-worktree-sessions.mjs +408 -0
  23. package/lib/server/helper-requests.mjs +82 -0
  24. package/lib/server/no-project-workspaces.mjs +103 -0
  25. package/lib/server/rpc-transports.mjs +394 -0
  26. package/lib/server/settings-store.mjs +544 -0
  27. package/lib/server/sse-delivery.mjs +138 -0
  28. package/lib/server/workspace-files.mjs +563 -0
  29. package/lib/update/supervisor.mjs +4 -0
  30. package/lib/webui-workspaces.mjs +2 -0
  31. package/package.json +5 -5
  32. package/public/activity-agents-controller.mjs +269 -0
  33. package/public/activity-events-controller.mjs +236 -0
  34. package/public/activity-intercom-controller.mjs +627 -0
  35. package/public/activity-queue-controller.mjs +534 -0
  36. package/public/activity-todo-view.mjs +89 -0
  37. package/public/activity-usage-controller.mjs +589 -0
  38. package/public/app.js +3761 -3878
  39. package/public/conversation-overview.css +84 -0
  40. package/public/conversation-overview.mjs +193 -0
  41. package/public/draft-conversations.mjs +135 -0
  42. package/public/focused-composer-controls.css +111 -0
  43. package/public/focused-composer-controls.mjs +143 -0
  44. package/public/focused-composer-disclosure.mjs +223 -0
  45. package/public/focused-shell-adapter.css +264 -0
  46. package/public/focused-shell-adapter.mjs +257 -0
  47. package/public/index.html +137 -80
  48. package/public/model-catalog-order.mjs +63 -0
  49. package/public/platform-event-stream.mjs +64 -0
  50. package/public/project-artifact-controller.mjs +218 -0
  51. package/public/project-file-viewer-controller.mjs +383 -0
  52. package/public/project-git-tools-controller.mjs +181 -0
  53. package/public/project-runners-controller.mjs +220 -0
  54. package/public/service-worker.js +52 -1
  55. package/public/session-attention-scheduler.mjs +182 -0
  56. package/public/session-composer-store.mjs +56 -0
  57. package/public/session-context.mjs +51 -0
  58. package/public/session-extension-requests.mjs +63 -0
  59. package/public/session-shelf-state.mjs +151 -0
  60. package/public/session-shelves.css +171 -0
  61. package/public/session-shelves.mjs +95 -0
  62. package/public/settings-navigation-controller.mjs +249 -0
  63. package/public/settings-navigation.css +61 -0
  64. package/public/settings-resource-controller.mjs +232 -0
  65. package/public/settings-workspace.css +19 -0
  66. package/public/settings-workspace.mjs +175 -0
  67. package/public/shell-actions.mjs +87 -0
  68. package/public/shell-attention-inbox.css +72 -0
  69. package/public/shell-attention-inbox.mjs +198 -0
  70. package/public/shell-context-actions.css +61 -0
  71. package/public/shell-context-actions.mjs +168 -0
  72. package/public/shell-inspector-resize.mjs +124 -0
  73. package/public/shell-navigation-resize.mjs +107 -0
  74. package/public/shell-navigation.mjs +127 -0
  75. package/public/shell-preferences.mjs +108 -0
  76. package/public/shell-surfaces.mjs +99 -0
  77. package/public/shell-tools-launcher.css +97 -0
  78. package/public/shell-tools-launcher.mjs +222 -0
  79. package/public/shell-view-host.mjs +115 -0
  80. package/public/shell-workspace.css +266 -0
  81. package/public/shell-workspace.mjs +402 -0
  82. package/public/sidebar-update-menu.css +190 -0
  83. package/public/sidebar-update-menu.mjs +512 -0
  84. package/public/styles.css +320 -169
  85. package/public/subagent-launch-slot-state.mjs +2 -2
  86. package/public/theme-contract.mjs +1 -1
  87. package/public/themed-selects.css +156 -0
  88. package/public/workspace-choices.mjs +104 -0
  89. package/tests/activity-agents-controller.test.mjs +652 -0
  90. package/tests/activity-events-controller.test.mjs +511 -0
  91. package/tests/app-runner-terminal-static.test.mjs +3 -3
  92. package/tests/append-system-http.test.mjs +7 -6
  93. package/tests/append-system-selector-static.test.mjs +6 -6
  94. package/tests/aur-review-widget-static.test.mjs +11 -3
  95. package/tests/boot-failure-diagnostics.test.mjs +8 -3
  96. package/tests/browser/activity-intercom-controller.spec.mjs +457 -0
  97. package/tests/browser/activity-navigation-integration.spec.mjs +45 -0
  98. package/tests/browser/activity-owner-integration.spec.mjs +201 -0
  99. package/tests/browser/activity-queue-controller.spec.mjs +405 -0
  100. package/tests/browser/activity-section-picker.spec.mjs +294 -0
  101. package/tests/browser/activity-todo-view.spec.mjs +60 -0
  102. package/tests/browser/activity-usage-controller.spec.mjs +416 -0
  103. package/tests/browser/compact-layout-integration.spec.mjs +58 -0
  104. package/tests/browser/composer-action-grid-reorder.spec.mjs +16 -0
  105. package/tests/browser/composer-disclosure.spec.mjs +389 -0
  106. package/tests/browser/composer-toolbar-polish.spec.mjs +343 -0
  107. package/tests/browser/control-deck-component-updates.spec.mjs +5 -14
  108. package/tests/browser/control-deck-side-panels.spec.mjs +27 -12
  109. package/tests/browser/control-visibility.spec.mjs +5 -0
  110. package/tests/browser/controls-layout.spec.mjs +21 -2
  111. package/tests/browser/conversation-overview.spec.mjs +251 -0
  112. package/tests/browser/draft-conversations.spec.mjs +276 -0
  113. package/tests/browser/empty-startup-fixture.mjs +135 -0
  114. package/tests/browser/empty-startup.spec.mjs +282 -0
  115. package/tests/browser/events-tree-context-menu.spec.mjs +1 -0
  116. package/tests/browser/focused-composer-more.spec.mjs +127 -0
  117. package/tests/browser/focused-model-shortcuts.spec.mjs +120 -0
  118. package/tests/browser/focused-navigation-fixture.mjs +32 -0
  119. package/tests/browser/focused-project-activity.spec.mjs +111 -0
  120. package/tests/browser/focused-shell-integration.spec.mjs +208 -0
  121. package/tests/browser/folder-picker-layout-fixture.mjs +13 -0
  122. package/tests/browser/folder-picker-layout.spec.mjs +302 -0
  123. package/tests/browser/footer-model-advanced-layout.spec.mjs +27 -14
  124. package/tests/browser/git-setup-cancel.spec.mjs +251 -0
  125. package/tests/browser/guided-git-fallback.spec.mjs +40 -21
  126. package/tests/browser/inspector-header.spec.mjs +221 -0
  127. package/tests/browser/inspector-overview-integration.spec.mjs +104 -0
  128. package/tests/browser/inspector-resize.spec.mjs +227 -0
  129. package/tests/browser/inspector-topic-overflow.spec.mjs +56 -0
  130. package/tests/browser/interaction-action-discovery.spec.mjs +332 -0
  131. package/tests/browser/interaction-attention-inbox.spec.mjs +364 -0
  132. package/tests/browser/interaction-attention-integration.spec.mjs +53 -0
  133. package/tests/browser/interaction-composer-controls.spec.mjs +267 -0
  134. package/tests/browser/interaction-composer-integration.spec.mjs +175 -0
  135. package/tests/browser/interaction-continuity.spec.mjs +50 -26
  136. package/tests/browser/interaction-model-picker.spec.mjs +86 -0
  137. package/tests/browser/last-terminal-close.spec.mjs +280 -0
  138. package/tests/browser/mobile-foundation.spec.mjs +172 -20
  139. package/tests/browser/model-catalog-groups.spec.mjs +104 -0
  140. package/tests/browser/more-row-menus.spec.mjs +258 -0
  141. package/tests/browser/new-tab-hit-target.spec.mjs +228 -0
  142. package/tests/browser/no-project-conversation.spec.mjs +113 -0
  143. package/tests/browser/optimistic-prompt.spec.mjs +3 -0
  144. package/tests/browser/overhaul-w7-accessibility.spec.mjs +227 -0
  145. package/tests/browser/overhaul-w7-capabilities.spec.mjs +298 -0
  146. package/tests/browser/overhaul-w7-companions.spec.mjs +290 -0
  147. package/tests/browser/overhaul-w7-focus-repairs.spec.mjs +210 -0
  148. package/tests/browser/overhaul-w7-layout-repairs.spec.mjs +183 -0
  149. package/tests/browser/overhaul-w7-parent-repairs.spec.mjs +97 -0
  150. package/tests/browser/overhaul-w7-pwa.spec.mjs +247 -0
  151. package/tests/browser/overhaul-w7-responsive.spec.mjs +450 -0
  152. package/tests/browser/overhaul-w7-update.spec.mjs +215 -0
  153. package/tests/browser/overhaul-w7-zoom.spec.mjs +243 -0
  154. package/tests/browser/overview-agent-ownership.spec.mjs +45 -0
  155. package/tests/browser/persistent-ui-layout.spec.mjs +3 -0
  156. package/tests/browser/project-artifact-controller.spec.mjs +324 -0
  157. package/tests/browser/project-artifact-integration.spec.mjs +103 -0
  158. package/tests/browser/project-file-buffer-integration.spec.mjs +230 -0
  159. package/tests/browser/project-git-tools-integration.spec.mjs +369 -0
  160. package/tests/browser/session-fixture.mjs +118 -0
  161. package/tests/browser/session-ownership.spec.mjs +175 -0
  162. package/tests/browser/session-shelves-integration.spec.mjs +219 -0
  163. package/tests/browser/session-shelves.spec.mjs +340 -0
  164. package/tests/browser/session-sidebar-resize.spec.mjs +138 -0
  165. package/tests/browser/session-summary.spec.mjs +17 -12
  166. package/tests/browser/settings-complete-home.spec.mjs +463 -0
  167. package/tests/browser/settings-entrypoint.spec.mjs +104 -0
  168. package/tests/browser/settings-navigation-controller.spec.mjs +383 -0
  169. package/tests/browser/settings-navigation-integration.spec.mjs +348 -0
  170. package/tests/browser/settings-resource-lifetime.spec.mjs +349 -0
  171. package/tests/browser/settings-workspace-integration.spec.mjs +132 -0
  172. package/tests/browser/shell-ownership.spec.mjs +398 -0
  173. package/tests/browser/shell-rollout-fixture.mjs +259 -0
  174. package/tests/browser/shell-rollout.spec.mjs +357 -0
  175. package/tests/browser/shell-tools-launcher.spec.mjs +260 -0
  176. package/tests/browser/shell-workspace.spec.mjs +433 -0
  177. package/tests/browser/side-panel-section-reorder.spec.mjs +3 -1
  178. package/tests/browser/sidebar-floating-tooltips.spec.mjs +37 -0
  179. package/tests/browser/sidebar-update-integration.spec.mjs +277 -0
  180. package/tests/browser/sidebar-update-menu.spec.mjs +402 -0
  181. package/tests/browser/sidebar-update-progress.spec.mjs +472 -0
  182. package/tests/browser/stream-output-isolation.spec.mjs +6 -3
  183. package/tests/browser/themed-selects.spec.mjs +297 -0
  184. package/tests/browser/thread-start-flow.spec.mjs +481 -0
  185. package/tests/browser/ui-polish-activity-controls.spec.mjs +177 -0
  186. package/tests/browser/ui-polish-features.spec.mjs +85 -0
  187. package/tests/browser/ui-polish-file-header.spec.mjs +43 -0
  188. package/tests/browser/ui-polish-local-git.spec.mjs +183 -0
  189. package/tests/browser/ui-polish-mobile-recovery.spec.mjs +36 -0
  190. package/tests/browser/ui-polish-navigation.spec.mjs +127 -0
  191. package/tests/browser/ui-polish-session-actions.spec.mjs +282 -0
  192. package/tests/browser/user-noticeable-fixes.spec.mjs +10 -3
  193. package/tests/browser/windows-drive-parent.spec.mjs +4 -2
  194. package/tests/browser/workspace-draft-selection.spec.mjs +266 -0
  195. package/tests/codex-cli-usage.test.mjs +189 -0
  196. package/tests/codex-fast-mode-static.test.mjs +16 -11
  197. package/tests/codex-speed-mode-browser-state.test.mjs +6 -3
  198. package/tests/codex-usage-auth.test.mjs +168 -9
  199. package/tests/codex-usage-http.test.mjs +124 -0
  200. package/tests/codex-usage-source.test.mjs +146 -0
  201. package/tests/component-update-api-static.test.mjs +2 -2
  202. package/tests/composer-action-grid-reorder-static.test.mjs +3 -3
  203. package/tests/control-deck-component-updates-static.test.mjs +9 -4
  204. package/tests/control-visibility-static.test.mjs +3 -3
  205. package/tests/controls-layout-static.test.mjs +5 -5
  206. package/tests/conversation-overview-adapter.test.mjs +100 -0
  207. package/tests/document-artifact-static.test.mjs +2 -1
  208. package/tests/draft-conversations.test.mjs +205 -0
  209. package/tests/empty-startup-launcher.test.mjs +197 -0
  210. package/tests/events-tree-context-menu-static.test.mjs +22 -18
  211. package/tests/fake-pi-session-ownership.test.mjs +302 -0
  212. package/tests/fast-mode-client-static.test.mjs +1 -1
  213. package/tests/feature-decision-output-popup-static.test.mjs +2 -2
  214. package/tests/feature-system-prompt-optional-feature-static.test.mjs +1 -1
  215. package/tests/file-viewer-image-static.test.mjs +15 -11
  216. package/tests/file-viewer-search-static.test.mjs +5 -5
  217. package/tests/fixtures/fake-codex-app-server.mjs +61 -0
  218. package/tests/fixtures/fake-pi.mjs +41 -9
  219. package/tests/footer-provider-usage-visual-static.test.mjs +7 -6
  220. package/tests/foreground-transcript-catch-up-static.test.mjs +7 -4
  221. package/tests/git-footer-context-menu-static.test.mjs +1 -1
  222. package/tests/git-panel-file-preview-static.test.mjs +41 -48
  223. package/tests/git-panel-live-updates-static.test.mjs +2 -1
  224. package/tests/git-workflow-dispatch.test.mjs +66 -0
  225. package/tests/git-workflow-preferences.test.mjs +9 -1
  226. package/tests/guided-git-activation-static.test.mjs +14 -11
  227. package/tests/guided-git-fallback-ui-static.test.mjs +8 -5
  228. package/tests/guided-git-message-reliability-static.test.mjs +10 -6
  229. package/tests/guided-git-publication-backend-static.test.mjs +16 -11
  230. package/tests/http-endpoints-harness.test.mjs +29 -9
  231. package/tests/interaction-state-stability-static.test.mjs +4 -3
  232. package/tests/intercom-conversation-viewer-static.test.mjs +29 -17
  233. package/tests/intercom-tag-layout-static.test.mjs +13 -12
  234. package/tests/intercom-tag-overflow-layout.test.mjs +11 -10
  235. package/tests/main-output-loading-static.test.mjs +4 -4
  236. package/tests/middle-button-drag-scroll-static.test.mjs +3 -3
  237. package/tests/mobile-compact-layout.test.mjs +2 -2
  238. package/tests/mobile-continuity-tablet-static.test.mjs +3 -3
  239. package/tests/mobile-foundation-static.test.mjs +10 -7
  240. package/tests/mobile-phone-experience-static.test.mjs +3 -2
  241. package/tests/mobile-static.test.mjs +173 -135
  242. package/tests/model-catalog-order.test.mjs +103 -0
  243. package/tests/native-parity.test.mjs +6 -4
  244. package/tests/native-session-choice-owner.test.mjs +102 -0
  245. package/tests/native-update-lifecycle.test.mjs +10 -5
  246. package/tests/no-project-http-harness.test.mjs +328 -0
  247. package/tests/no-project-navigation.test.mjs +54 -0
  248. package/tests/no-project-workspaces.test.mjs +77 -0
  249. package/tests/open-issue-wizard-static.test.mjs +1 -1
  250. package/tests/optional-feature-migration-frontend.test.mjs +4 -4
  251. package/tests/optional-feature-prompt-impact-static.test.mjs +3 -3
  252. package/tests/package-updates.test.mjs +630 -0
  253. package/tests/persistent-ui-layout-static.test.mjs +3 -3
  254. package/tests/platform-event-stream.test.mjs +253 -0
  255. package/tests/project-file-viewer-controller.test.mjs +652 -0
  256. package/tests/project-git-tools-adapter.test.mjs +103 -0
  257. package/tests/project-git-tools-controller.test.mjs +577 -0
  258. package/tests/project-runners-controller.test.mjs +453 -0
  259. package/tests/prompt-input-vertical-scroll-static.test.mjs +3 -3
  260. package/tests/prompt-routing-input-clear-static.test.mjs +2 -2
  261. package/tests/questionnaire-dialog.test.mjs +6 -6
  262. package/tests/queue-edit-reorder-static.test.mjs +48 -51
  263. package/tests/rpc-supervisor-host.test.mjs +4 -1
  264. package/tests/safety-guard-auto-review-static.test.mjs +1 -1
  265. package/tests/sampling-native-controls-user-flow.test.mjs +7 -1
  266. package/tests/server-app-runners.test.mjs +536 -0
  267. package/tests/server-codex-fast-mode.test.mjs +346 -0
  268. package/tests/server-conversation-mode.test.mjs +386 -0
  269. package/tests/server-conversation-voice.test.mjs +426 -0
  270. package/tests/server-extension-ui-requests.test.mjs +357 -0
  271. package/tests/server-git-generation.test.mjs +558 -0
  272. package/tests/server-git-mutations.test.mjs +729 -0
  273. package/tests/server-git-reads.test.mjs +381 -0
  274. package/tests/server-git-worktree-sessions.test.mjs +502 -0
  275. package/tests/server-helper-requests.test.mjs +364 -0
  276. package/tests/server-rpc-transports.test.mjs +553 -0
  277. package/tests/server-settings-store.test.mjs +132 -0
  278. package/tests/server-shell-rollout.test.mjs +478 -0
  279. package/tests/server-sse-delivery.test.mjs +410 -0
  280. package/tests/server-workspace-files.test.mjs +546 -0
  281. package/tests/session-attention-scheduler.test.mjs +514 -0
  282. package/tests/session-composer-store.test.mjs +153 -0
  283. package/tests/session-context.test.mjs +134 -0
  284. package/tests/session-extension-requests.test.mjs +193 -0
  285. package/tests/session-ownership-adapter.test.mjs +130 -0
  286. package/tests/session-sampling-ui-static.test.mjs +10 -4
  287. package/tests/session-send-lifetime.test.mjs +140 -0
  288. package/tests/session-shelf-state.test.mjs +348 -0
  289. package/tests/session-shelves-adapter.test.mjs +79 -0
  290. package/tests/session-summary-lifetime.test.mjs +54 -0
  291. package/tests/settings-resource-controller.test.mjs +548 -0
  292. package/tests/shell-action-adapter.test.mjs +169 -0
  293. package/tests/shell-actions.test.mjs +182 -0
  294. package/tests/shell-navigation.test.mjs +348 -0
  295. package/tests/shell-preferences.test.mjs +367 -0
  296. package/tests/side-panel-section-height-removal-static.test.mjs +3 -3
  297. package/tests/stream-output-isolation-static.test.mjs +1 -1
  298. package/tests/subagent-gate-visibility.test.mjs +1 -1
  299. package/tests/subagent-launch-slot-state.test.mjs +2 -2
  300. package/tests/subagent-launch-slots-static.test.mjs +1 -1
  301. package/tests/subagent-observability-static.test.mjs +2 -2
  302. package/tests/subagent-reliability-integration.test.mjs +13 -30
  303. package/tests/syntax-highlighting-static.test.mjs +3 -3
  304. package/tests/tab-close-continuity-static.test.mjs +1 -1
  305. package/tests/tab-metadata-ordering.test.mjs +99 -0
  306. package/tests/terminal-cwd-grouping-static.test.mjs +2 -2
  307. package/tests/terminal-tab-prompt-tooltip-static.test.mjs +2 -0
  308. package/tests/terminal-tab-workspace-static.test.mjs +5 -4
  309. package/tests/terminal-tabs-resize-static.test.mjs +1 -1
  310. package/tests/theme-contract.test.mjs +10 -3
  311. package/tests/theme-customizer-static.test.mjs +1 -1
  312. package/tests/thread-display-title.test.mjs +44 -0
  313. package/tests/update-api-harness.test.mjs +15 -14
  314. package/tests/update-tab-restore-harness.test.mjs +3 -0
  315. package/tests/webui-settings-locking.test.mjs +2 -2
  316. package/tests/windows-drive-cwd-picker-static.test.mjs +7 -9
  317. package/tests/workspace-choices.test.mjs +114 -0
  318. package/tests/workspace-save-load-static.test.mjs +7 -4
@@ -0,0 +1,459 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import path from "node:path";
3
+ import { setTimeout as delay } from "node:timers/promises";
4
+ import {
5
+ gitMessageArtifactPairReadiness,
6
+ readStableGitMessageArtifactPair,
7
+ } from "../git-message-artifacts.mjs";
8
+ import {
9
+ isGitWorkflowSetupComplete,
10
+ readGitWorkflowPreferences,
11
+ supportedGitWorkflowThinkingLevels,
12
+ } from "../git-workflow-preferences.mjs";
13
+
14
+ export function createGitGenerationService({
15
+ getGitRoot,
16
+ gitMutations,
17
+ makeHttpError,
18
+ sanitizeError,
19
+ safeRpcData,
20
+ currentSessionState,
21
+ availableGitWorkflowModels,
22
+ stateIsBusyForSettings,
23
+ markTabWorking,
24
+ markTabIdle,
25
+ broadcastTabEvent,
26
+ sendSseToClient,
27
+ statusRpcTimeoutMs,
28
+ }) {
29
+ const {
30
+ assertExpectedStagedContentHash,
31
+ gitWorkflowMessageCwd,
32
+ commitMessagePaths,
33
+ readGitWorkflowBranchName,
34
+ readGitWorkflowPrDescription,
35
+ } = gitMutations;
36
+
37
+ function gitWorkflowGenerationProfileArgument(profile) {
38
+ const payload = Buffer.from(JSON.stringify({
39
+ version: 1,
40
+ provider: profile.provider,
41
+ modelId: profile.modelId,
42
+ thinkingLevel: profile.thinkingLevel,
43
+ }), "utf8").toString("base64url");
44
+ return `--firstpick-webui-generation-profile=${payload}`;
45
+ }
46
+
47
+ function gitWorkflowGenerationPrompt(kind, preferences, commandName, profile) {
48
+ const generationProfile = gitWorkflowGenerationProfileArgument(profile);
49
+ switch (kind) {
50
+ case "commit":
51
+ return `/${commandName} ${preferences.commit.language} ${preferences.commit.scope} ${generationProfile}`;
52
+ case "branch":
53
+ return `/${commandName} ${generationProfile}`;
54
+ case "pr":
55
+ return `/${commandName} ${preferences.commit.language} ${generationProfile}`;
56
+ default:
57
+ throw makeHttpError(400, "generation kind must be commit, branch, or pr");
58
+ }
59
+ }
60
+
61
+ function guidedGitNativeCommandBase(kind) {
62
+ if (kind === "commit") return "git-staged-msg";
63
+ if (kind === "branch") return "git-branch-name";
64
+ if (kind === "pr") return "pr";
65
+ throw makeHttpError(400, "generation kind must be commit, branch, or pr");
66
+ }
67
+
68
+ async function resolveGuidedGitNativeCommand(tab, kind) {
69
+ const baseName = guidedGitNativeCommandBase(kind);
70
+ const response = await safeRpcData(tab, { type: "get_commands" }, statusRpcTimeoutMs);
71
+ const commands = Array.isArray(response.data?.commands) ? response.data.commands : [];
72
+ const command = commands.find((candidate) => candidate?.source === "extension" && candidate?.name === baseName)
73
+ || commands.find((candidate) => candidate?.source === "extension" && String(candidate?.name || "").replace(/:\d+$/, "") === baseName);
74
+ if (!response.ok || !command) {
75
+ throw makeHttpError(409, `Guided Git requires the /${baseName} extension command in this Pi tab. Same-named prompt templates are not used.`);
76
+ }
77
+ return String(command.name);
78
+ }
79
+
80
+ const GUIDED_GIT_PROVIDER_FAILURE_MARKER = "FIRSTPICK_GUIDED_GIT_PROVIDER_FAILURE";
81
+ const GUIDED_GIT_NATIVE_COMMAND_ERROR_GRACE_MS = 500;
82
+
83
+ function boundedGitWorkflowGenerationError(message, fallback = "Guided Git generation failed") {
84
+ const text = typeof message === "string" ? message : fallback;
85
+ return text.replace(/[\r\n]+/g, " ").trim().slice(0, 240) || fallback;
86
+ }
87
+
88
+ function isGuidedGitProviderGenerationFailure(error) {
89
+ return String(error?.message || error || "").includes(GUIDED_GIT_PROVIDER_FAILURE_MARKER);
90
+ }
91
+
92
+ function guidedGitNativeCommandErrorFromEvent(record, event) {
93
+ const commandName = String(record?.nativeCommandName || "");
94
+ if (event?.type !== "extension_error" || event.event !== "command" || !commandName || event.extensionPath !== `command:${commandName}`) return null;
95
+ const error = new Error(String(event.error || `/${commandName} failed`));
96
+ error.guidedGitProviderGenerationFailure = isGuidedGitProviderGenerationFailure(error);
97
+ return error;
98
+ }
99
+
100
+ function gitWorkflowGenerationCancellationError() {
101
+ const error = makeHttpError(409, "Guided Git generation was cancelled");
102
+ error.code = "GIT_WORKFLOW_CANCELLED";
103
+ return error;
104
+ }
105
+
106
+ function assertGitWorkflowGenerationOwnership(tab, record) {
107
+ if (tab.gitWorkflowGeneration !== record) throw new Error("Guided Git generation is no longer active");
108
+ if (record.cancelled) throw gitWorkflowGenerationCancellationError();
109
+ }
110
+
111
+ function attachGitWorkflowGenerationError(error, record) {
112
+ if (!error || typeof error !== "object" || !record?.generationId || !record.fallbackStarted || record.cancelled || record.processLost) return error;
113
+ error.gitWorkflowGeneration = {
114
+ generationId: record.generationId,
115
+ kind: record.kind,
116
+ fallbackUsed: record.fallbackStarted === true,
117
+ generation: publicGitWorkflowGenerationProfile(record.fallbackStarted && record.fallback ? record.fallback : record.primary),
118
+ primaryGeneration: publicGitWorkflowGenerationProfile(record.primary),
119
+ };
120
+ return error;
121
+ }
122
+
123
+ function gitWorkflowGenerationProfile(provider, modelId, thinkingLevel, models) {
124
+ const model = models.find((candidate) => candidate.provider === provider && candidate.id === modelId) || null;
125
+ return { provider, modelId, thinkingLevel, model };
126
+ }
127
+
128
+ function publicGitWorkflowGenerationProfile(profile) {
129
+ return {
130
+ provider: String(profile?.provider || ""),
131
+ modelId: String(profile?.modelId || ""),
132
+ thinkingLevel: String(profile?.thinkingLevel || ""),
133
+ };
134
+ }
135
+
136
+ async function createGitWorkflowArtifactGeneration(tab, generationId, kind) {
137
+ const createdAt = Date.now();
138
+ if (kind === "commit") {
139
+ // Keep generation dispatch compatible with tabs whose cwd has not become
140
+ // a repository yet; staging boundaries still verify the real Git root.
141
+ const root = await getGitRoot(tab.cwd).catch(() => path.resolve(tab.cwd));
142
+ const cwd = gitWorkflowMessageCwd(root, tab.cwd);
143
+ return {
144
+ id: generationId,
145
+ kind,
146
+ root,
147
+ cwd,
148
+ createdAt,
149
+ baseline: await readStableGitMessageArtifactPair(commitMessagePaths(cwd)),
150
+ terminal: false,
151
+ successful: false,
152
+ };
153
+ }
154
+ const baseline = kind === "branch"
155
+ ? await readGitWorkflowBranchName(tab.cwd).catch(() => null)
156
+ : await readGitWorkflowPrDescription(tab.cwd).catch(() => null);
157
+ return { id: generationId, kind, cwd: path.resolve(tab.cwd), createdAt, baseline, terminal: false, successful: false };
158
+ }
159
+
160
+ async function assertGuidedGitNativeArtifactUpdated(tab, record) {
161
+ const generation = record.artifactGeneration;
162
+ if (!generation) throw new Error("Guided Git generation has no artifact correlation record");
163
+ if (generation.kind === "commit") {
164
+ const pair = await readStableGitMessageArtifactPair(commitMessagePaths(generation.cwd));
165
+ const readiness = gitMessageArtifactPairReadiness(generation.baseline, pair);
166
+ if (!readiness.ready) throw new Error(readiness.reason || "Native commit generation did not update both message artifacts");
167
+ return;
168
+ }
169
+ const current = generation.kind === "branch"
170
+ ? await readGitWorkflowBranchName(tab.cwd)
171
+ : await readGitWorkflowPrDescription(tab.cwd);
172
+ if (generation.baseline
173
+ && current.path === generation.baseline.path
174
+ && current.mtimeMs === generation.baseline.mtimeMs
175
+ && current.ctimeMs === generation.baseline.ctimeMs
176
+ && current.ino === generation.baseline.ino) {
177
+ throw new Error(`Native ${generation.kind} generation did not update its correlated artifact`);
178
+ }
179
+ }
180
+
181
+ function emitGitWorkflowFallbackLifecycle(tab, record, phase) {
182
+ const event = {
183
+ type: `webui_git_workflow_generation_fallback_${phase}`,
184
+ tabId: tab.id,
185
+ tabTitle: tab.title,
186
+ generationId: record.generationId,
187
+ kind: record.kind,
188
+ generation: publicGitWorkflowGenerationProfile(record.fallback),
189
+ primaryGeneration: publicGitWorkflowGenerationProfile(record.primary),
190
+ ...(phase === "started"
191
+ ? { reason: boundedGitWorkflowGenerationError("The primary Git-writing model ended with an error.") }
192
+ : { error: boundedGitWorkflowGenerationError("The fallback Git-writing model ended with an error.") }),
193
+ };
194
+ const lifecycle = tab.gitWorkflowFallbackLifecycle;
195
+ if (lifecycle?.generationId === record.generationId) {
196
+ lifecycle.events = [...lifecycle.events.filter((item) => item.type !== event.type), event].slice(-2);
197
+ }
198
+ broadcastTabEvent(tab, event);
199
+ }
200
+
201
+ function replayGitWorkflowFallbackLifecycle(tab, client) {
202
+ const lifecycle = tab.gitWorkflowFallbackLifecycle;
203
+ for (const event of lifecycle?.events || []) sendSseToClient(client, { ...event, replayed: true });
204
+ }
205
+
206
+ async function finishGitWorkflowGeneration(tab, record, { successful }) {
207
+ if (tab.gitWorkflowGeneration !== record || record.finishing) return;
208
+ record.finishing = true;
209
+ const artifactGeneration = record.artifactGeneration;
210
+ if (artifactGeneration && tab.gitWorkflowArtifactGeneration === artifactGeneration) {
211
+ artifactGeneration.terminal = true;
212
+ artifactGeneration.successful = successful === true && !record.cancelled;
213
+ }
214
+ if (!successful && record.messageGeneration && tab.gitWorkflowMessageGeneration?.id === record.messageGeneration.id) {
215
+ tab.gitWorkflowMessageGeneration = record.previousMessageGeneration;
216
+ }
217
+ markTabIdle(tab);
218
+ if (tab.gitWorkflowGeneration === record) tab.gitWorkflowGeneration = null;
219
+ }
220
+
221
+ async function dispatchGitWorkflowGenerationAttempt(tab, record, profile, attempt) {
222
+ assertGitWorkflowGenerationOwnership(tab, record);
223
+ if (!tab.rpc?.isRunning?.()) throw new Error("Pi RPC process is not running");
224
+ if (!profile.model) throw new Error(`Configured ${attempt} Git-writing model is unavailable: ${profile.provider}/${profile.modelId}`);
225
+ if (!supportedGitWorkflowThinkingLevels(profile.model).includes(profile.thinkingLevel)) {
226
+ throw new Error(`Configured ${attempt} thinking level ${profile.thinkingLevel} is unavailable for ${profile.provider}/${profile.modelId}`);
227
+ }
228
+
229
+ record.attempt = attempt;
230
+ record.attemptRunStarted = false;
231
+ record.promptAccepted = false;
232
+ record.pendingSettlement = null;
233
+ record.nativeCommandError = null;
234
+ record.message = gitWorkflowGenerationPrompt(record.kind, record.preferences, record.nativeCommandName, profile);
235
+
236
+ assertGitWorkflowGenerationOwnership(tab, record);
237
+ markTabWorking(tab);
238
+ const response = await tab.rpc.send({ type: "prompt", message: record.message });
239
+ assertGitWorkflowGenerationOwnership(tab, record);
240
+ if (response.success === false) {
241
+ const error = new Error(response.error || "Guided Git generation prompt was rejected");
242
+ error.guidedGitProviderGenerationFailure = isGuidedGitProviderGenerationFailure(response.error);
243
+ throw error;
244
+ }
245
+ if (record.nativeCommandError) throw record.nativeCommandError;
246
+ record.promptAccepted = true;
247
+ const pendingSettlement = record.pendingSettlement;
248
+ if (pendingSettlement?.attempt === attempt) {
249
+ record.pendingSettlement = null;
250
+ void settleGitWorkflowGeneration(tab, record, pendingSettlement);
251
+ }
252
+ const result = {
253
+ accepted: true,
254
+ kind: record.kind,
255
+ message: record.message,
256
+ generationId: record.generationId,
257
+ generation: publicGitWorkflowGenerationProfile(profile),
258
+ fallbackUsed: attempt === "fallback",
259
+ primaryGeneration: publicGitWorkflowGenerationProfile(record.primary),
260
+ };
261
+ if (record.nativeGeneration) {
262
+ try {
263
+ await assertGuidedGitNativeArtifactUpdated(tab, record);
264
+ } catch (artifactError) {
265
+ assertGitWorkflowGenerationOwnership(tab, record);
266
+ if (!record.nativeCommandError) await delay(GUIDED_GIT_NATIVE_COMMAND_ERROR_GRACE_MS);
267
+ assertGitWorkflowGenerationOwnership(tab, record);
268
+ if (record.nativeCommandError) throw record.nativeCommandError;
269
+ throw artifactError;
270
+ }
271
+ assertGitWorkflowGenerationOwnership(tab, record);
272
+ await finishGitWorkflowGeneration(tab, record, { successful: true });
273
+ }
274
+ return result;
275
+ }
276
+
277
+ async function startGitWorkflowFallback(tab, record, primaryError) {
278
+ if (tab.gitWorkflowGeneration !== record || record.fallbackStarted || !record.fallback) return null;
279
+ if (record.cancelled || !tab.rpc?.isRunning?.() || primaryError?.guidedGitProviderGenerationFailure !== true) return null;
280
+ record.fallbackStarted = true;
281
+ record.attempt = "fallback";
282
+ record.attemptRunStarted = false;
283
+ record.promptAccepted = false;
284
+ console.error(`[guided-git] primary generation ${record.generationId} failed; starting fallback: ${sanitizeError(primaryError)}`);
285
+ emitGitWorkflowFallbackLifecycle(tab, record, "started");
286
+ try {
287
+ return await dispatchGitWorkflowGenerationAttempt(tab, record, record.fallback, "fallback");
288
+ } catch (error) {
289
+ if (!record.cancelled && tab.gitWorkflowGeneration === record) {
290
+ console.error(`[guided-git] fallback generation ${record.generationId} failed: ${sanitizeError(error)}`);
291
+ emitGitWorkflowFallbackLifecycle(tab, record, "failed");
292
+ }
293
+ await finishGitWorkflowGeneration(tab, record, { successful: false });
294
+ throw error;
295
+ }
296
+ }
297
+
298
+ async function settleGitWorkflowGeneration(tab, record, { failed, cancelled }) {
299
+ if (tab.gitWorkflowGeneration !== record || record.finishing) return;
300
+ if (cancelled) record.cancelled = true;
301
+ if (record.attempt === "primary" && failed && !record.cancelled && record.fallback) {
302
+ try {
303
+ const result = await startGitWorkflowFallback(tab, record, "The primary Git-writing model ended with an error");
304
+ if (result) return;
305
+ } catch {
306
+ // startGitWorkflowFallback emitted the bounded terminal lifecycle event.
307
+ return;
308
+ }
309
+ }
310
+ if (record.attempt === "fallback" && failed) {
311
+ emitGitWorkflowFallbackLifecycle(tab, record, "failed");
312
+ }
313
+ await finishGitWorkflowGeneration(tab, record, { successful: !failed && !record.cancelled });
314
+ }
315
+
316
+ function consumeGitWorkflowGenerationEvent(tab, event, { finalError = false } = {}) {
317
+ const record = tab.gitWorkflowGeneration;
318
+ if (!record || record.finishing) return;
319
+ const nativeCommandError = guidedGitNativeCommandErrorFromEvent(record, event);
320
+ if (nativeCommandError) {
321
+ record.nativeCommandError = nativeCommandError;
322
+ return;
323
+ }
324
+ if ((event?.type === "agent_end" || event?.type === "agent_settled") && event.aborted === true) record.cancelled = true;
325
+ if (event?.type === "agent_start") {
326
+ record.attemptRunStarted = true;
327
+ return;
328
+ }
329
+ if (event?.type !== "agent_settled" || !record.attemptRunStarted) return;
330
+ record.attemptRunStarted = false;
331
+ const settlement = { attempt: record.attempt, failed: finalError, cancelled: record.cancelled };
332
+ if (!record.promptAccepted) {
333
+ record.pendingSettlement = settlement;
334
+ return;
335
+ }
336
+ void settleGitWorkflowGeneration(tab, record, settlement);
337
+ }
338
+
339
+ async function startGitWorkflowGeneration(tab, body = {}) {
340
+ if (tab.gitWorkflowGeneration) throw makeHttpError(409, "A guided Git generation request is already active in this tab");
341
+ const kind = String(body.kind || "").trim();
342
+ if (!new Set(["commit", "branch", "pr"]).has(kind)) throw makeHttpError(400, "generation kind must be commit, branch, or pr");
343
+
344
+ // Reserve the tab synchronously before the first await. All preflight and
345
+ // profile transitions retain this exact object as their ownership token.
346
+ const record = {
347
+ generationId: randomUUID(),
348
+ kind,
349
+ message: "",
350
+ preferences: null,
351
+ previousMessageGeneration: tab.gitWorkflowMessageGeneration || null,
352
+ messageGeneration: null,
353
+ artifactGeneration: null,
354
+ primary: null,
355
+ fallback: null,
356
+ fallbackStarted: false,
357
+ cancelled: false,
358
+ finishing: false,
359
+ attempt: "primary",
360
+ attemptRunStarted: false,
361
+ promptAccepted: false,
362
+ pendingSettlement: null,
363
+ nativeCommandName: "",
364
+ nativeCommandError: null,
365
+ nativeGeneration: true,
366
+ };
367
+ tab.gitWorkflowGeneration = record;
368
+ tab.gitWorkflowFallbackLifecycle = { generationId: record.generationId, kind, events: [] };
369
+
370
+ try {
371
+ // Browser state is untrusted. When it claims an approved content token,
372
+ // verify the exact current index again at this server action boundary.
373
+ await assertExpectedStagedContentHash(tab, body);
374
+ assertGitWorkflowGenerationOwnership(tab, record);
375
+ const preferences = await readGitWorkflowPreferences();
376
+ assertGitWorkflowGenerationOwnership(tab, record);
377
+ if (!isGitWorkflowSetupComplete(preferences)) throw makeHttpError(409, "Run /git-workflow-setup or open Guided Git Setup before generating Git text");
378
+ record.preferences = preferences;
379
+ const nativeCommandName = await resolveGuidedGitNativeCommand(tab, kind);
380
+ assertGitWorkflowGenerationOwnership(tab, record);
381
+ record.nativeCommandName = nativeCommandName;
382
+
383
+ const state = await currentSessionState(tab);
384
+ assertGitWorkflowGenerationOwnership(tab, record);
385
+ if (stateIsBusyForSettings(state)) throw makeHttpError(409, "Wait for the current agent run to finish before generating Git text");
386
+ const models = await availableGitWorkflowModels(tab);
387
+ assertGitWorkflowGenerationOwnership(tab, record);
388
+ record.primary = gitWorkflowGenerationProfile(
389
+ preferences.generation.provider,
390
+ preferences.generation.modelId,
391
+ preferences.generation.thinkingLevel,
392
+ models,
393
+ );
394
+ const fallbackConfigured = !!preferences.generation.fallback?.provider
395
+ && !!preferences.generation.fallback?.modelId
396
+ && (preferences.generation.fallback.provider !== preferences.generation.provider
397
+ || preferences.generation.fallback.modelId !== preferences.generation.modelId);
398
+ record.fallback = fallbackConfigured
399
+ ? gitWorkflowGenerationProfile(
400
+ preferences.generation.fallback.provider,
401
+ preferences.generation.fallback.modelId,
402
+ preferences.generation.fallback.thinkingLevel,
403
+ models,
404
+ )
405
+ : null;
406
+
407
+ record.artifactGeneration = await createGitWorkflowArtifactGeneration(tab, record.generationId, kind);
408
+ record.messageGeneration = kind === "commit" ? record.artifactGeneration : null;
409
+ assertGitWorkflowGenerationOwnership(tab, record);
410
+ tab.gitWorkflowArtifactGeneration = record.artifactGeneration;
411
+ if (record.messageGeneration) tab.gitWorkflowMessageGeneration = record.messageGeneration;
412
+
413
+ try {
414
+ return await dispatchGitWorkflowGenerationAttempt(tab, record, record.primary, "primary");
415
+ } catch (primaryError) {
416
+ if (tab.gitWorkflowGeneration === record) markTabIdle(tab);
417
+ if (record.fallback && !record.cancelled && tab.rpc?.isRunning?.() && primaryError?.guidedGitProviderGenerationFailure === true) {
418
+ try {
419
+ const fallbackResult = await startGitWorkflowFallback(tab, record, primaryError);
420
+ if (fallbackResult) return fallbackResult;
421
+ } catch (fallbackError) {
422
+ throw attachGitWorkflowGenerationError(fallbackError, record);
423
+ }
424
+ }
425
+ await finishGitWorkflowGeneration(tab, record, { successful: false });
426
+ throw attachGitWorkflowGenerationError(primaryError, record);
427
+ }
428
+ } catch (error) {
429
+ if (tab.gitWorkflowGeneration === record && !record.finishing) {
430
+ await finishGitWorkflowGeneration(tab, record, { successful: false });
431
+ }
432
+ throw error;
433
+ }
434
+ }
435
+
436
+ function cancelGitWorkflowGeneration(tab) {
437
+ const generation = tab?.gitWorkflowGeneration || null;
438
+ if (generation) generation.cancelled = true;
439
+ return !!generation;
440
+ }
441
+
442
+ function loseGitWorkflowGeneration(tab) {
443
+ if (tab.gitWorkflowGeneration) tab.gitWorkflowGeneration.processLost = true;
444
+ if (tab.gitWorkflowArtifactGeneration) {
445
+ tab.gitWorkflowArtifactGeneration.terminal = true;
446
+ tab.gitWorkflowArtifactGeneration.successful = false;
447
+ }
448
+ tab.gitWorkflowGeneration = null;
449
+ tab.gitWorkflowMessageGeneration = null;
450
+ }
451
+
452
+ return {
453
+ startGitWorkflowGeneration,
454
+ consumeGitWorkflowGenerationEvent,
455
+ replayGitWorkflowFallbackLifecycle,
456
+ cancelGitWorkflowGeneration,
457
+ loseGitWorkflowGeneration,
458
+ };
459
+ }