better-codex 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (405) hide show
  1. package/README.md +26 -0
  2. package/apps/backend/README.md +46 -0
  3. package/apps/backend/bun.lock +64 -0
  4. package/apps/backend/package.json +18 -0
  5. package/apps/backend/scripts/generate-protocol.ts +32 -0
  6. package/apps/backend/src/analytics/service.ts +219 -0
  7. package/apps/backend/src/analytics/store.ts +284 -0
  8. package/apps/backend/src/config.ts +98 -0
  9. package/apps/backend/src/core/app-server.ts +131 -0
  10. package/apps/backend/src/core/jsonrpc.ts +166 -0
  11. package/apps/backend/src/protocol/AbsolutePathBuf.ts +14 -0
  12. package/apps/backend/src/protocol/AddConversationListenerParams.ts +6 -0
  13. package/apps/backend/src/protocol/AddConversationSubscriptionResponse.ts +5 -0
  14. package/apps/backend/src/protocol/AgentMessageContent.ts +5 -0
  15. package/apps/backend/src/protocol/AgentMessageContentDeltaEvent.ts +5 -0
  16. package/apps/backend/src/protocol/AgentMessageDeltaEvent.ts +5 -0
  17. package/apps/backend/src/protocol/AgentMessageEvent.ts +5 -0
  18. package/apps/backend/src/protocol/AgentMessageItem.ts +6 -0
  19. package/apps/backend/src/protocol/AgentReasoningDeltaEvent.ts +5 -0
  20. package/apps/backend/src/protocol/AgentReasoningEvent.ts +5 -0
  21. package/apps/backend/src/protocol/AgentReasoningRawContentDeltaEvent.ts +5 -0
  22. package/apps/backend/src/protocol/AgentReasoningRawContentEvent.ts +5 -0
  23. package/apps/backend/src/protocol/AgentReasoningSectionBreakEvent.ts +5 -0
  24. package/apps/backend/src/protocol/Annotations.ts +9 -0
  25. package/apps/backend/src/protocol/ApplyPatchApprovalParams.ts +21 -0
  26. package/apps/backend/src/protocol/ApplyPatchApprovalRequestEvent.ts +23 -0
  27. package/apps/backend/src/protocol/ApplyPatchApprovalResponse.ts +6 -0
  28. package/apps/backend/src/protocol/ArchiveConversationParams.ts +6 -0
  29. package/apps/backend/src/protocol/ArchiveConversationResponse.ts +5 -0
  30. package/apps/backend/src/protocol/AskForApproval.ts +9 -0
  31. package/apps/backend/src/protocol/AudioContent.ts +9 -0
  32. package/apps/backend/src/protocol/AuthMode.ts +5 -0
  33. package/apps/backend/src/protocol/AuthStatusChangeNotification.ts +9 -0
  34. package/apps/backend/src/protocol/BackgroundEventEvent.ts +5 -0
  35. package/apps/backend/src/protocol/BlobResourceContents.ts +5 -0
  36. package/apps/backend/src/protocol/CallToolResult.ts +10 -0
  37. package/apps/backend/src/protocol/CancelLoginChatGptParams.ts +5 -0
  38. package/apps/backend/src/protocol/CancelLoginChatGptResponse.ts +5 -0
  39. package/apps/backend/src/protocol/ClientInfo.ts +5 -0
  40. package/apps/backend/src/protocol/ClientNotification.ts +5 -0
  41. package/apps/backend/src/protocol/ClientRequest.ts +46 -0
  42. package/apps/backend/src/protocol/CodexErrorInfo.ts +8 -0
  43. package/apps/backend/src/protocol/ContentBlock.ts +10 -0
  44. package/apps/backend/src/protocol/ContentItem.ts +5 -0
  45. package/apps/backend/src/protocol/ContextCompactedEvent.ts +5 -0
  46. package/apps/backend/src/protocol/ConversationGitInfo.ts +5 -0
  47. package/apps/backend/src/protocol/ConversationId.ts +5 -0
  48. package/apps/backend/src/protocol/ConversationSummary.ts +8 -0
  49. package/apps/backend/src/protocol/CreditsSnapshot.ts +5 -0
  50. package/apps/backend/src/protocol/CustomPrompt.ts +5 -0
  51. package/apps/backend/src/protocol/DeprecationNoticeEvent.ts +13 -0
  52. package/apps/backend/src/protocol/ElicitationRequestEvent.ts +6 -0
  53. package/apps/backend/src/protocol/EmbeddedResource.ts +13 -0
  54. package/apps/backend/src/protocol/EmbeddedResourceResource.ts +7 -0
  55. package/apps/backend/src/protocol/ErrorEvent.ts +6 -0
  56. package/apps/backend/src/protocol/EventMsg.ts +60 -0
  57. package/apps/backend/src/protocol/ExecApprovalRequestEvent.ts +32 -0
  58. package/apps/backend/src/protocol/ExecCommandApprovalParams.ts +12 -0
  59. package/apps/backend/src/protocol/ExecCommandApprovalResponse.ts +6 -0
  60. package/apps/backend/src/protocol/ExecCommandBeginEvent.ts +35 -0
  61. package/apps/backend/src/protocol/ExecCommandEndEvent.ts +59 -0
  62. package/apps/backend/src/protocol/ExecCommandOutputDeltaEvent.ts +18 -0
  63. package/apps/backend/src/protocol/ExecCommandSource.ts +5 -0
  64. package/apps/backend/src/protocol/ExecOneOffCommandParams.ts +6 -0
  65. package/apps/backend/src/protocol/ExecOneOffCommandResponse.ts +5 -0
  66. package/apps/backend/src/protocol/ExecOutputStream.ts +5 -0
  67. package/apps/backend/src/protocol/ExecPolicyAmendment.ts +12 -0
  68. package/apps/backend/src/protocol/ExitedReviewModeEvent.ts +6 -0
  69. package/apps/backend/src/protocol/FileChange.ts +5 -0
  70. package/apps/backend/src/protocol/ForcedLoginMethod.ts +5 -0
  71. package/apps/backend/src/protocol/FunctionCallOutputContentItem.ts +9 -0
  72. package/apps/backend/src/protocol/FunctionCallOutputPayload.ts +15 -0
  73. package/apps/backend/src/protocol/FuzzyFileSearchParams.ts +5 -0
  74. package/apps/backend/src/protocol/FuzzyFileSearchResponse.ts +6 -0
  75. package/apps/backend/src/protocol/FuzzyFileSearchResult.ts +8 -0
  76. package/apps/backend/src/protocol/GetAuthStatusParams.ts +5 -0
  77. package/apps/backend/src/protocol/GetAuthStatusResponse.ts +6 -0
  78. package/apps/backend/src/protocol/GetConversationSummaryParams.ts +6 -0
  79. package/apps/backend/src/protocol/GetConversationSummaryResponse.ts +6 -0
  80. package/apps/backend/src/protocol/GetHistoryEntryResponseEvent.ts +10 -0
  81. package/apps/backend/src/protocol/GetUserAgentResponse.ts +5 -0
  82. package/apps/backend/src/protocol/GetUserSavedConfigResponse.ts +6 -0
  83. package/apps/backend/src/protocol/GhostCommit.ts +8 -0
  84. package/apps/backend/src/protocol/GitDiffToRemoteParams.ts +5 -0
  85. package/apps/backend/src/protocol/GitDiffToRemoteResponse.ts +6 -0
  86. package/apps/backend/src/protocol/GitSha.ts +5 -0
  87. package/apps/backend/src/protocol/HistoryEntry.ts +5 -0
  88. package/apps/backend/src/protocol/ImageContent.ts +9 -0
  89. package/apps/backend/src/protocol/InitializeParams.ts +6 -0
  90. package/apps/backend/src/protocol/InitializeResponse.ts +5 -0
  91. package/apps/backend/src/protocol/InputItem.ts +5 -0
  92. package/apps/backend/src/protocol/InterruptConversationParams.ts +6 -0
  93. package/apps/backend/src/protocol/InterruptConversationResponse.ts +6 -0
  94. package/apps/backend/src/protocol/ItemCompletedEvent.ts +7 -0
  95. package/apps/backend/src/protocol/ItemStartedEvent.ts +7 -0
  96. package/apps/backend/src/protocol/ListConversationsParams.ts +5 -0
  97. package/apps/backend/src/protocol/ListConversationsResponse.ts +6 -0
  98. package/apps/backend/src/protocol/ListCustomPromptsResponseEvent.ts +9 -0
  99. package/apps/backend/src/protocol/ListSkillsResponseEvent.ts +9 -0
  100. package/apps/backend/src/protocol/LocalShellAction.ts +6 -0
  101. package/apps/backend/src/protocol/LocalShellExecAction.ts +5 -0
  102. package/apps/backend/src/protocol/LocalShellStatus.ts +5 -0
  103. package/apps/backend/src/protocol/LoginApiKeyParams.ts +5 -0
  104. package/apps/backend/src/protocol/LoginApiKeyResponse.ts +5 -0
  105. package/apps/backend/src/protocol/LoginChatGptCompleteNotification.ts +8 -0
  106. package/apps/backend/src/protocol/LoginChatGptResponse.ts +5 -0
  107. package/apps/backend/src/protocol/LogoutChatGptResponse.ts +5 -0
  108. package/apps/backend/src/protocol/McpAuthStatus.ts +5 -0
  109. package/apps/backend/src/protocol/McpInvocation.ts +18 -0
  110. package/apps/backend/src/protocol/McpListToolsResponseEvent.ts +25 -0
  111. package/apps/backend/src/protocol/McpStartupCompleteEvent.ts +6 -0
  112. package/apps/backend/src/protocol/McpStartupFailure.ts +5 -0
  113. package/apps/backend/src/protocol/McpStartupStatus.ts +5 -0
  114. package/apps/backend/src/protocol/McpStartupUpdateEvent.ts +14 -0
  115. package/apps/backend/src/protocol/McpToolCallBeginEvent.ts +10 -0
  116. package/apps/backend/src/protocol/McpToolCallEndEvent.ts +15 -0
  117. package/apps/backend/src/protocol/NetworkAccess.ts +8 -0
  118. package/apps/backend/src/protocol/NewConversationParams.ts +8 -0
  119. package/apps/backend/src/protocol/NewConversationResponse.ts +7 -0
  120. package/apps/backend/src/protocol/ParsedCommand.ts +12 -0
  121. package/apps/backend/src/protocol/PatchApplyBeginEvent.ts +23 -0
  122. package/apps/backend/src/protocol/PatchApplyEndEvent.ts +31 -0
  123. package/apps/backend/src/protocol/PlanItemArg.ts +6 -0
  124. package/apps/backend/src/protocol/PlanType.ts +5 -0
  125. package/apps/backend/src/protocol/Profile.ts +9 -0
  126. package/apps/backend/src/protocol/README.md +11 -0
  127. package/apps/backend/src/protocol/RateLimitSnapshot.ts +8 -0
  128. package/apps/backend/src/protocol/RateLimitWindow.ts +17 -0
  129. package/apps/backend/src/protocol/RawResponseItemEvent.ts +6 -0
  130. package/apps/backend/src/protocol/ReasoningContentDeltaEvent.ts +5 -0
  131. package/apps/backend/src/protocol/ReasoningEffort.ts +8 -0
  132. package/apps/backend/src/protocol/ReasoningItem.ts +5 -0
  133. package/apps/backend/src/protocol/ReasoningItemContent.ts +5 -0
  134. package/apps/backend/src/protocol/ReasoningItemReasoningSummary.ts +5 -0
  135. package/apps/backend/src/protocol/ReasoningRawContentDeltaEvent.ts +5 -0
  136. package/apps/backend/src/protocol/ReasoningSummary.ts +10 -0
  137. package/apps/backend/src/protocol/RemoveConversationListenerParams.ts +5 -0
  138. package/apps/backend/src/protocol/RemoveConversationSubscriptionResponse.ts +5 -0
  139. package/apps/backend/src/protocol/RequestId.ts +5 -0
  140. package/apps/backend/src/protocol/Resource.ts +9 -0
  141. package/apps/backend/src/protocol/ResourceLink.ts +11 -0
  142. package/apps/backend/src/protocol/ResourceTemplate.ts +9 -0
  143. package/apps/backend/src/protocol/ResponseItem.ts +17 -0
  144. package/apps/backend/src/protocol/ResumeConversationParams.ts +8 -0
  145. package/apps/backend/src/protocol/ResumeConversationResponse.ts +7 -0
  146. package/apps/backend/src/protocol/ReviewCodeLocation.ts +9 -0
  147. package/apps/backend/src/protocol/ReviewDecision.ts +9 -0
  148. package/apps/backend/src/protocol/ReviewFinding.ts +9 -0
  149. package/apps/backend/src/protocol/ReviewLineRange.ts +8 -0
  150. package/apps/backend/src/protocol/ReviewOutputEvent.ts +9 -0
  151. package/apps/backend/src/protocol/ReviewRequest.ts +9 -0
  152. package/apps/backend/src/protocol/ReviewTarget.ts +9 -0
  153. package/apps/backend/src/protocol/Role.ts +8 -0
  154. package/apps/backend/src/protocol/SandboxMode.ts +5 -0
  155. package/apps/backend/src/protocol/SandboxPolicy.ts +35 -0
  156. package/apps/backend/src/protocol/SandboxSettings.ts +6 -0
  157. package/apps/backend/src/protocol/SendUserMessageParams.ts +7 -0
  158. package/apps/backend/src/protocol/SendUserMessageResponse.ts +5 -0
  159. package/apps/backend/src/protocol/SendUserTurnParams.ts +11 -0
  160. package/apps/backend/src/protocol/SendUserTurnResponse.ts +5 -0
  161. package/apps/backend/src/protocol/ServerNotification.ts +36 -0
  162. package/apps/backend/src/protocol/ServerRequest.ts +13 -0
  163. package/apps/backend/src/protocol/SessionConfiguredEvent.ts +48 -0
  164. package/apps/backend/src/protocol/SessionConfiguredNotification.ts +8 -0
  165. package/apps/backend/src/protocol/SessionSource.ts +6 -0
  166. package/apps/backend/src/protocol/SetDefaultModelParams.ts +6 -0
  167. package/apps/backend/src/protocol/SetDefaultModelResponse.ts +5 -0
  168. package/apps/backend/src/protocol/SkillErrorInfo.ts +5 -0
  169. package/apps/backend/src/protocol/SkillMetadata.ts +6 -0
  170. package/apps/backend/src/protocol/SkillScope.ts +5 -0
  171. package/apps/backend/src/protocol/SkillsListEntry.ts +7 -0
  172. package/apps/backend/src/protocol/StepStatus.ts +5 -0
  173. package/apps/backend/src/protocol/StreamErrorEvent.ts +6 -0
  174. package/apps/backend/src/protocol/SubAgentSource.ts +5 -0
  175. package/apps/backend/src/protocol/TaskCompleteEvent.ts +5 -0
  176. package/apps/backend/src/protocol/TaskStartedEvent.ts +5 -0
  177. package/apps/backend/src/protocol/TerminalInteractionEvent.ts +17 -0
  178. package/apps/backend/src/protocol/TextContent.ts +9 -0
  179. package/apps/backend/src/protocol/TextResourceContents.ts +5 -0
  180. package/apps/backend/src/protocol/TokenCountEvent.ts +7 -0
  181. package/apps/backend/src/protocol/TokenUsage.ts +5 -0
  182. package/apps/backend/src/protocol/TokenUsageInfo.ts +6 -0
  183. package/apps/backend/src/protocol/Tool.ts +11 -0
  184. package/apps/backend/src/protocol/ToolAnnotations.ts +15 -0
  185. package/apps/backend/src/protocol/ToolInputSchema.ts +9 -0
  186. package/apps/backend/src/protocol/ToolOutputSchema.ts +10 -0
  187. package/apps/backend/src/protocol/Tools.ts +5 -0
  188. package/apps/backend/src/protocol/TurnAbortReason.ts +5 -0
  189. package/apps/backend/src/protocol/TurnAbortedEvent.ts +6 -0
  190. package/apps/backend/src/protocol/TurnDiffEvent.ts +5 -0
  191. package/apps/backend/src/protocol/TurnItem.ts +9 -0
  192. package/apps/backend/src/protocol/UndoCompletedEvent.ts +5 -0
  193. package/apps/backend/src/protocol/UndoStartedEvent.ts +5 -0
  194. package/apps/backend/src/protocol/UpdatePlanArgs.ts +6 -0
  195. package/apps/backend/src/protocol/UserInfoResponse.ts +5 -0
  196. package/apps/backend/src/protocol/UserInput.ts +8 -0
  197. package/apps/backend/src/protocol/UserMessageEvent.ts +5 -0
  198. package/apps/backend/src/protocol/UserMessageItem.ts +6 -0
  199. package/apps/backend/src/protocol/UserSavedConfig.ts +14 -0
  200. package/apps/backend/src/protocol/Verbosity.ts +9 -0
  201. package/apps/backend/src/protocol/ViewImageToolCallEvent.ts +13 -0
  202. package/apps/backend/src/protocol/WarningEvent.ts +5 -0
  203. package/apps/backend/src/protocol/WebSearchAction.ts +5 -0
  204. package/apps/backend/src/protocol/WebSearchBeginEvent.ts +5 -0
  205. package/apps/backend/src/protocol/WebSearchEndEvent.ts +5 -0
  206. package/apps/backend/src/protocol/WebSearchItem.ts +5 -0
  207. package/apps/backend/src/protocol/index.ts +198 -0
  208. package/apps/backend/src/protocol/serde_json/JsonValue.ts +5 -0
  209. package/apps/backend/src/protocol/v2/Account.ts +6 -0
  210. package/apps/backend/src/protocol/v2/AccountLoginCompletedNotification.ts +5 -0
  211. package/apps/backend/src/protocol/v2/AccountRateLimitsUpdatedNotification.ts +6 -0
  212. package/apps/backend/src/protocol/v2/AccountUpdatedNotification.ts +6 -0
  213. package/apps/backend/src/protocol/v2/AgentMessageDeltaNotification.ts +5 -0
  214. package/apps/backend/src/protocol/v2/ApprovalDecision.ts +6 -0
  215. package/apps/backend/src/protocol/v2/AskForApproval.ts +5 -0
  216. package/apps/backend/src/protocol/v2/CancelLoginAccountParams.ts +5 -0
  217. package/apps/backend/src/protocol/v2/CancelLoginAccountResponse.ts +6 -0
  218. package/apps/backend/src/protocol/v2/CancelLoginAccountStatus.ts +5 -0
  219. package/apps/backend/src/protocol/v2/CodexErrorInfo.ts +11 -0
  220. package/apps/backend/src/protocol/v2/CommandAction.ts +5 -0
  221. package/apps/backend/src/protocol/v2/CommandExecParams.ts +6 -0
  222. package/apps/backend/src/protocol/v2/CommandExecResponse.ts +5 -0
  223. package/apps/backend/src/protocol/v2/CommandExecutionOutputDeltaNotification.ts +5 -0
  224. package/apps/backend/src/protocol/v2/CommandExecutionRequestApprovalParams.ts +14 -0
  225. package/apps/backend/src/protocol/v2/CommandExecutionRequestApprovalResponse.ts +6 -0
  226. package/apps/backend/src/protocol/v2/CommandExecutionStatus.ts +5 -0
  227. package/apps/backend/src/protocol/v2/Config.ts +15 -0
  228. package/apps/backend/src/protocol/v2/ConfigBatchWriteParams.ts +10 -0
  229. package/apps/backend/src/protocol/v2/ConfigEdit.ts +7 -0
  230. package/apps/backend/src/protocol/v2/ConfigLayer.ts +7 -0
  231. package/apps/backend/src/protocol/v2/ConfigLayerMetadata.ts +6 -0
  232. package/apps/backend/src/protocol/v2/ConfigLayerSource.ts +6 -0
  233. package/apps/backend/src/protocol/v2/ConfigReadParams.ts +5 -0
  234. package/apps/backend/src/protocol/v2/ConfigReadResponse.ts +8 -0
  235. package/apps/backend/src/protocol/v2/ConfigValueWriteParams.ts +11 -0
  236. package/apps/backend/src/protocol/v2/ConfigWriteResponse.ts +12 -0
  237. package/apps/backend/src/protocol/v2/ContextCompactedNotification.ts +5 -0
  238. package/apps/backend/src/protocol/v2/CreditsSnapshot.ts +5 -0
  239. package/apps/backend/src/protocol/v2/DeprecationNoticeNotification.ts +13 -0
  240. package/apps/backend/src/protocol/v2/ErrorNotification.ts +6 -0
  241. package/apps/backend/src/protocol/v2/ExecPolicyAmendment.ts +5 -0
  242. package/apps/backend/src/protocol/v2/FeedbackUploadParams.ts +5 -0
  243. package/apps/backend/src/protocol/v2/FeedbackUploadResponse.ts +5 -0
  244. package/apps/backend/src/protocol/v2/FileChangeOutputDeltaNotification.ts +5 -0
  245. package/apps/backend/src/protocol/v2/FileChangeRequestApprovalParams.ts +14 -0
  246. package/apps/backend/src/protocol/v2/FileChangeRequestApprovalResponse.ts +6 -0
  247. package/apps/backend/src/protocol/v2/FileUpdateChange.ts +6 -0
  248. package/apps/backend/src/protocol/v2/GetAccountParams.ts +5 -0
  249. package/apps/backend/src/protocol/v2/GetAccountRateLimitsResponse.ts +6 -0
  250. package/apps/backend/src/protocol/v2/GetAccountResponse.ts +6 -0
  251. package/apps/backend/src/protocol/v2/GitInfo.ts +5 -0
  252. package/apps/backend/src/protocol/v2/ItemCompletedNotification.ts +6 -0
  253. package/apps/backend/src/protocol/v2/ItemStartedNotification.ts +6 -0
  254. package/apps/backend/src/protocol/v2/ListMcpServerStatusParams.ts +13 -0
  255. package/apps/backend/src/protocol/v2/ListMcpServerStatusResponse.ts +11 -0
  256. package/apps/backend/src/protocol/v2/LoginAccountParams.ts +5 -0
  257. package/apps/backend/src/protocol/v2/LoginAccountResponse.ts +9 -0
  258. package/apps/backend/src/protocol/v2/LogoutAccountResponse.ts +5 -0
  259. package/apps/backend/src/protocol/v2/McpAuthStatus.ts +5 -0
  260. package/apps/backend/src/protocol/v2/McpServerOauthLoginCompletedNotification.ts +5 -0
  261. package/apps/backend/src/protocol/v2/McpServerOauthLoginParams.ts +5 -0
  262. package/apps/backend/src/protocol/v2/McpServerOauthLoginResponse.ts +5 -0
  263. package/apps/backend/src/protocol/v2/McpServerStatus.ts +9 -0
  264. package/apps/backend/src/protocol/v2/McpToolCallError.ts +5 -0
  265. package/apps/backend/src/protocol/v2/McpToolCallProgressNotification.ts +5 -0
  266. package/apps/backend/src/protocol/v2/McpToolCallResult.ts +7 -0
  267. package/apps/backend/src/protocol/v2/McpToolCallStatus.ts +5 -0
  268. package/apps/backend/src/protocol/v2/MergeStrategy.ts +5 -0
  269. package/apps/backend/src/protocol/v2/Model.ts +7 -0
  270. package/apps/backend/src/protocol/v2/ModelListParams.ts +13 -0
  271. package/apps/backend/src/protocol/v2/ModelListResponse.ts +11 -0
  272. package/apps/backend/src/protocol/v2/NetworkAccess.ts +5 -0
  273. package/apps/backend/src/protocol/v2/OverriddenMetadata.ts +7 -0
  274. package/apps/backend/src/protocol/v2/PatchApplyStatus.ts +5 -0
  275. package/apps/backend/src/protocol/v2/PatchChangeKind.ts +5 -0
  276. package/apps/backend/src/protocol/v2/ProfileV2.ts +10 -0
  277. package/apps/backend/src/protocol/v2/RateLimitSnapshot.ts +8 -0
  278. package/apps/backend/src/protocol/v2/RateLimitWindow.ts +5 -0
  279. package/apps/backend/src/protocol/v2/RawResponseItemCompletedNotification.ts +6 -0
  280. package/apps/backend/src/protocol/v2/ReasoningEffortOption.ts +6 -0
  281. package/apps/backend/src/protocol/v2/ReasoningSummaryPartAddedNotification.ts +5 -0
  282. package/apps/backend/src/protocol/v2/ReasoningSummaryTextDeltaNotification.ts +5 -0
  283. package/apps/backend/src/protocol/v2/ReasoningTextDeltaNotification.ts +5 -0
  284. package/apps/backend/src/protocol/v2/ReviewDelivery.ts +5 -0
  285. package/apps/backend/src/protocol/v2/ReviewStartParams.ts +12 -0
  286. package/apps/backend/src/protocol/v2/ReviewStartResponse.ts +13 -0
  287. package/apps/backend/src/protocol/v2/ReviewTarget.ts +9 -0
  288. package/apps/backend/src/protocol/v2/SandboxMode.ts +5 -0
  289. package/apps/backend/src/protocol/v2/SandboxPolicy.ts +7 -0
  290. package/apps/backend/src/protocol/v2/SandboxWorkspaceWrite.ts +5 -0
  291. package/apps/backend/src/protocol/v2/SessionSource.ts +5 -0
  292. package/apps/backend/src/protocol/v2/SkillErrorInfo.ts +5 -0
  293. package/apps/backend/src/protocol/v2/SkillMetadata.ts +6 -0
  294. package/apps/backend/src/protocol/v2/SkillScope.ts +5 -0
  295. package/apps/backend/src/protocol/v2/SkillsListEntry.ts +7 -0
  296. package/apps/backend/src/protocol/v2/SkillsListParams.ts +13 -0
  297. package/apps/backend/src/protocol/v2/SkillsListResponse.ts +6 -0
  298. package/apps/backend/src/protocol/v2/TerminalInteractionNotification.ts +5 -0
  299. package/apps/backend/src/protocol/v2/Thread.ts +46 -0
  300. package/apps/backend/src/protocol/v2/ThreadArchiveParams.ts +5 -0
  301. package/apps/backend/src/protocol/v2/ThreadArchiveResponse.ts +5 -0
  302. package/apps/backend/src/protocol/v2/ThreadItem.ts +48 -0
  303. package/apps/backend/src/protocol/v2/ThreadListParams.ts +18 -0
  304. package/apps/backend/src/protocol/v2/ThreadListResponse.ts +11 -0
  305. package/apps/backend/src/protocol/v2/ThreadResumeParams.ts +35 -0
  306. package/apps/backend/src/protocol/v2/ThreadResumeResponse.ts +9 -0
  307. package/apps/backend/src/protocol/v2/ThreadStartParams.ts +15 -0
  308. package/apps/backend/src/protocol/v2/ThreadStartResponse.ts +9 -0
  309. package/apps/backend/src/protocol/v2/ThreadStartedNotification.ts +6 -0
  310. package/apps/backend/src/protocol/v2/ThreadTokenUsage.ts +6 -0
  311. package/apps/backend/src/protocol/v2/ThreadTokenUsageUpdatedNotification.ts +6 -0
  312. package/apps/backend/src/protocol/v2/TokenUsageBreakdown.ts +5 -0
  313. package/apps/backend/src/protocol/v2/ToolsV2.ts +5 -0
  314. package/apps/backend/src/protocol/v2/Turn.ts +18 -0
  315. package/apps/backend/src/protocol/v2/TurnCompletedNotification.ts +6 -0
  316. package/apps/backend/src/protocol/v2/TurnDiffUpdatedNotification.ts +9 -0
  317. package/apps/backend/src/protocol/v2/TurnError.ts +6 -0
  318. package/apps/backend/src/protocol/v2/TurnInterruptParams.ts +5 -0
  319. package/apps/backend/src/protocol/v2/TurnInterruptResponse.ts +5 -0
  320. package/apps/backend/src/protocol/v2/TurnPlanStep.ts +6 -0
  321. package/apps/backend/src/protocol/v2/TurnPlanStepStatus.ts +5 -0
  322. package/apps/backend/src/protocol/v2/TurnPlanUpdatedNotification.ts +6 -0
  323. package/apps/backend/src/protocol/v2/TurnStartParams.ts +34 -0
  324. package/apps/backend/src/protocol/v2/TurnStartResponse.ts +6 -0
  325. package/apps/backend/src/protocol/v2/TurnStartedNotification.ts +6 -0
  326. package/apps/backend/src/protocol/v2/TurnStatus.ts +5 -0
  327. package/apps/backend/src/protocol/v2/UserInput.ts +5 -0
  328. package/apps/backend/src/protocol/v2/WindowsWorldWritableWarningNotification.ts +5 -0
  329. package/apps/backend/src/protocol/v2/WriteStatus.ts +5 -0
  330. package/apps/backend/src/protocol/v2/index.ts +123 -0
  331. package/apps/backend/src/reviews/service.ts +27 -0
  332. package/apps/backend/src/reviews/store.ts +124 -0
  333. package/apps/backend/src/server.ts +531 -0
  334. package/apps/backend/src/services/profile-store.ts +114 -0
  335. package/apps/backend/src/services/supervisor.ts +102 -0
  336. package/apps/backend/src/thread-index/service.ts +75 -0
  337. package/apps/backend/src/thread-index/store.ts +195 -0
  338. package/apps/backend/src/ws/messages.ts +73 -0
  339. package/apps/backend/tsconfig.json +20 -0
  340. package/apps/web/README.md +24 -0
  341. package/apps/web/bun.lock +1062 -0
  342. package/apps/web/eslint.config.js +23 -0
  343. package/apps/web/index.html +16 -0
  344. package/apps/web/package.json +38 -0
  345. package/apps/web/src/app.tsx +83 -0
  346. package/apps/web/src/components/composer/slash-command-menu.tsx +47 -0
  347. package/apps/web/src/components/index.ts +2 -0
  348. package/apps/web/src/components/layout/account-usage-panel.tsx +167 -0
  349. package/apps/web/src/components/layout/analytics-view.tsx +296 -0
  350. package/apps/web/src/components/layout/index.ts +7 -0
  351. package/apps/web/src/components/layout/mobile-header.tsx +56 -0
  352. package/apps/web/src/components/layout/reviews-view.tsx +848 -0
  353. package/apps/web/src/components/layout/session-view.tsx +1374 -0
  354. package/apps/web/src/components/layout/settings-dialog.tsx +322 -0
  355. package/apps/web/src/components/layout/side-bar.tsx +417 -0
  356. package/apps/web/src/components/layout/thread-list.tsx +488 -0
  357. package/apps/web/src/components/layout/virtualized-message-list.tsx +748 -0
  358. package/apps/web/src/components/loading/startup-ascii.ts +652 -0
  359. package/apps/web/src/components/loading/startup-loader.tsx +37 -0
  360. package/apps/web/src/components/session-view/file-mention-menu.tsx +46 -0
  361. package/apps/web/src/components/session-view/session-auth-banner.tsx +61 -0
  362. package/apps/web/src/components/session-view/session-composer.tsx +328 -0
  363. package/apps/web/src/components/session-view/session-dialogs.tsx +280 -0
  364. package/apps/web/src/components/session-view/session-empty.tsx +47 -0
  365. package/apps/web/src/components/session-view/session-header.tsx +49 -0
  366. package/apps/web/src/components/ui/avatar.tsx +19 -0
  367. package/apps/web/src/components/ui/badge.tsx +21 -0
  368. package/apps/web/src/components/ui/button.tsx +47 -0
  369. package/apps/web/src/components/ui/collapsible-content.tsx +114 -0
  370. package/apps/web/src/components/ui/contribution-graph.tsx +182 -0
  371. package/apps/web/src/components/ui/dialog-box.tsx +203 -0
  372. package/apps/web/src/components/ui/icon-button.tsx +32 -0
  373. package/apps/web/src/components/ui/icons.tsx +187 -0
  374. package/apps/web/src/components/ui/index.tsx +15 -0
  375. package/apps/web/src/components/ui/input.tsx +43 -0
  376. package/apps/web/src/components/ui/markdown-stream.tsx +21 -0
  377. package/apps/web/src/components/ui/mobile-drawer.tsx +124 -0
  378. package/apps/web/src/components/ui/section-header.tsx +13 -0
  379. package/apps/web/src/components/ui/select.tsx +217 -0
  380. package/apps/web/src/components/ui/shimmer.tsx +138 -0
  381. package/apps/web/src/components/ui/status-dot.tsx +24 -0
  382. package/apps/web/src/config.ts +5 -0
  383. package/apps/web/src/hooks/index.ts +3 -0
  384. package/apps/web/src/hooks/use-analytics.ts +122 -0
  385. package/apps/web/src/hooks/use-hub-connection.ts +587 -0
  386. package/apps/web/src/hooks/use-mobile.ts +76 -0
  387. package/apps/web/src/hooks/use-thread-history.ts +210 -0
  388. package/apps/web/src/index.css +269 -0
  389. package/apps/web/src/main.tsx +10 -0
  390. package/apps/web/src/services/hub-client.ts +358 -0
  391. package/apps/web/src/store/index.ts +528 -0
  392. package/apps/web/src/types/index.ts +119 -0
  393. package/apps/web/src/utils/account-refresh.ts +168 -0
  394. package/apps/web/src/utils/approval-policy.ts +53 -0
  395. package/apps/web/src/utils/init-prompt.ts +41 -0
  396. package/apps/web/src/utils/item-format.ts +170 -0
  397. package/apps/web/src/utils/prompt-expander.ts +62 -0
  398. package/apps/web/src/utils/reasoning-summary.ts +48 -0
  399. package/apps/web/src/utils/slash-commands.ts +98 -0
  400. package/apps/web/tsconfig.app.json +28 -0
  401. package/apps/web/tsconfig.json +7 -0
  402. package/apps/web/tsconfig.node.json +26 -0
  403. package/apps/web/vite.config.ts +8 -0
  404. package/bin/better-codex.cjs +199 -0
  405. package/package.json +20 -0
@@ -0,0 +1,587 @@
1
+ import { useEffect } from 'react'
2
+ import { hubClient, type HubProfile } from '../services/hub-client'
3
+ import { useAppStore } from '../store'
4
+ import type { Account, Thread } from '../types'
5
+ import { buildSystemMessage } from '../utils/item-format'
6
+ import { accountStatusFromRead, parseUsage, refreshAccountSnapshot, fetchAllModels, type AccountReadResult, type RateLimitResult } from '../utils/account-refresh'
7
+
8
+ type ThreadListResult = {
9
+ data?: Array<{
10
+ id: string
11
+ preview?: string
12
+ modelProvider?: string
13
+ createdAt?: number
14
+ }>
15
+ }
16
+
17
+ type ItemPayload = { id?: string; type?: string } & Record<string, unknown>
18
+
19
+ const isThreadItem = (item: ItemPayload | undefined): item is { id: string; type: string } & Record<string, unknown> =>
20
+ !!item && typeof item.id === 'string' && typeof item.type === 'string'
21
+
22
+ const formatDate = (timestamp?: number): string => {
23
+ if (!timestamp) {
24
+ return ''
25
+ }
26
+ const date = new Date(timestamp * 1000)
27
+ return new Intl.DateTimeFormat('en-US', {
28
+ month: 'short',
29
+ day: 'numeric',
30
+ }).format(date)
31
+ }
32
+
33
+ const toAccount = (profile: HubProfile): Account => ({
34
+ id: profile.id,
35
+ name: profile.name,
36
+ email: '',
37
+ plan: 'Unknown',
38
+ status: 'offline',
39
+ rateLimit: 0,
40
+ })
41
+
42
+ const toThreads = (profileId: string, result: ThreadListResult): Thread[] => {
43
+ const items = result.data ?? []
44
+ return items.map((thread) => ({
45
+ id: thread.id,
46
+ accountId: profileId,
47
+ title: thread.preview?.trim() || 'Untitled session',
48
+ preview: thread.preview?.trim() || 'No preview available yet.',
49
+ model: thread.modelProvider ?? 'unknown',
50
+ createdAt: formatDate(thread.createdAt),
51
+ status: 'idle',
52
+ messageCount: 0,
53
+ }))
54
+ }
55
+
56
+ export const useHubConnection = () => {
57
+ const {
58
+ setConnectionStatus,
59
+ setAccounts,
60
+ updateAccount,
61
+ setThreadsForAccount,
62
+ addApproval,
63
+ addMessage,
64
+ appendMessageDelta,
65
+ upsertMessage,
66
+ ensureAssistantMessage,
67
+ setModelsForAccount,
68
+ updateThread,
69
+ setSelectedAccountId,
70
+ setSelectedThreadId,
71
+ shiftQueuedMessage,
72
+ enqueueMessage,
73
+ setMessagesForThread,
74
+ setThreadTurnId,
75
+ setThreadTurnStartedAt,
76
+ setThreadLastTurnDuration,
77
+ setThreadTokenUsage,
78
+ setAccountLoginId,
79
+ upsertReviewSession,
80
+ updateReviewSession,
81
+ } = useAppStore()
82
+
83
+ const nowTimestamp = () => new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
84
+
85
+ const addSystemMessage = (threadId: string, title: string, content: string) => {
86
+ addMessage(threadId, {
87
+ id: `sys-${Date.now()}-${Math.random().toString(16).slice(2)}`,
88
+ role: 'assistant',
89
+ kind: 'tool',
90
+ title,
91
+ content,
92
+ timestamp: nowTimestamp(),
93
+ })
94
+ }
95
+
96
+ useEffect(() => {
97
+ let disposed = false
98
+ let unsubscribe: (() => void) | null = null
99
+
100
+ const safeRequest = async <T,>(
101
+ profileId: string,
102
+ method: string,
103
+ params?: unknown
104
+ ): Promise<T | null> => {
105
+ try {
106
+ return (await hubClient.request(profileId, method, params)) as T
107
+ } catch (error) {
108
+ console.error(error)
109
+ return null
110
+ }
111
+ }
112
+
113
+ const refreshAccount = async (profileId: string) => {
114
+ try {
115
+ await refreshAccountSnapshot(profileId, updateAccount, setModelsForAccount)
116
+ } catch (error) {
117
+ console.error(error)
118
+ updateAccount(profileId, (prev) => ({ ...prev, status: 'degraded' }))
119
+ }
120
+ }
121
+
122
+ const dispatchQueuedMessage = async (profileId: string, threadId: string) => {
123
+ const next = shiftQueuedMessage(threadId)
124
+ if (!next) {
125
+ return
126
+ }
127
+ const state = useAppStore.getState()
128
+ const account = state.accounts.find((item) => item.id === profileId)
129
+ if (!account || account.status !== 'online' || state.connectionStatus !== 'connected') {
130
+ enqueueMessage(threadId, next)
131
+ return
132
+ }
133
+ addMessage(threadId, {
134
+ id: `msg-${Date.now()}`,
135
+ role: 'user',
136
+ content: next.text,
137
+ kind: 'chat',
138
+ timestamp: new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }),
139
+ })
140
+ updateThread(threadId, { status: 'active' })
141
+ try {
142
+ await hubClient.request(profileId, 'turn/start', {
143
+ threadId,
144
+ input: [{ type: 'text', text: next.text }],
145
+ model: next.model,
146
+ effort: next.effort ?? undefined,
147
+ summary: next.summary ?? undefined,
148
+ cwd: next.cwd ?? undefined,
149
+ approvalPolicy: next.approvalPolicy ?? undefined,
150
+ })
151
+ } catch (error) {
152
+ console.error(error)
153
+ enqueueMessage(threadId, next)
154
+ updateThread(threadId, { status: 'idle' })
155
+ }
156
+ }
157
+
158
+ const bootstrap = async () => {
159
+ try {
160
+ setConnectionStatus('connecting')
161
+ const profiles = await hubClient.listProfiles()
162
+ if (disposed) {
163
+ return
164
+ }
165
+ setAccounts(profiles.map(toAccount))
166
+ if (profiles.length > 0) {
167
+ setSelectedAccountId(profiles[0].id)
168
+ }
169
+
170
+ await hubClient.connect()
171
+ if (disposed) {
172
+ return
173
+ }
174
+ setConnectionStatus('connected')
175
+
176
+ unsubscribe = hubClient.onEvent((event) => {
177
+ // console.log('[HubConnection] Event received:', event.type, 'type' in event && event.type === 'rpc.event' ? (event as {method?: string}).method : '')
178
+ if (event.type === 'rpc.event') {
179
+ const { method, params, profileId } = event
180
+ if (method === 'account/updated' && params && typeof params === 'object') {
181
+ void refreshAccount(profileId)
182
+ }
183
+
184
+ if (method === 'account/login/completed' && params && typeof params === 'object') {
185
+ // console.log('[HubConnection] Login completed for:', profileId, params)
186
+ const { success } = params as { success?: boolean }
187
+ setAccountLoginId(profileId, null)
188
+ if (!success) {
189
+ updateAccount(profileId, (prev) => ({
190
+ ...prev,
191
+ status: 'offline',
192
+ }))
193
+ return
194
+ }
195
+ void refreshAccount(profileId)
196
+ }
197
+
198
+ if (method === 'account/rateLimits/updated' && params && typeof params === 'object') {
199
+ const usage = parseUsage({ rateLimits: (params as RateLimitResult).rateLimits })
200
+ const rate = usage?.primary?.usedPercent
201
+ updateAccount(profileId, (prev) => ({
202
+ ...prev,
203
+ rateLimit: typeof rate === 'number' ? Math.round(rate) : prev.rateLimit,
204
+ usage: usage ?? prev.usage,
205
+ }))
206
+ }
207
+
208
+ if (method === 'mcpServer/oauthLogin/completed' && params && typeof params === 'object') {
209
+ const { name, success, error } = params as { name?: string; success?: boolean; error?: string }
210
+ const selectedThreadId = useAppStore.getState().selectedThreadId
211
+ if (selectedThreadId) {
212
+ addSystemMessage(
213
+ selectedThreadId,
214
+ 'MCP Login',
215
+ success ? `${name ?? 'Server'} connected.` : `${name ?? 'Server'} login failed: ${error ?? 'Unknown error'}`
216
+ )
217
+ }
218
+ }
219
+
220
+ if (method === 'thread/started' && params && typeof params === 'object') {
221
+ const thread = (params as { thread?: { id: string; preview?: string; modelProvider?: string; createdAt?: number } }).thread
222
+ if (thread) {
223
+ // Initialize empty messages array FIRST to prevent thread/resume being called
224
+ const currentMessages = useAppStore.getState().messages
225
+ if (currentMessages[thread.id] === undefined) {
226
+ setMessagesForThread(thread.id, [])
227
+ }
228
+ updateThread(thread.id, {
229
+ accountId: profileId,
230
+ title: thread.preview?.trim() || 'Untitled session',
231
+ preview: thread.preview?.trim() || 'No preview available yet.',
232
+ model: thread.modelProvider ?? 'unknown',
233
+ createdAt: formatDate(thread.createdAt),
234
+ status: 'idle',
235
+ messageCount: 0,
236
+ })
237
+ }
238
+ }
239
+
240
+ if (method === 'turn/started' && params && typeof params === 'object') {
241
+ const { threadId, turn } = params as { threadId?: string; turn?: { id?: string } }
242
+ if (threadId) {
243
+ updateThread(threadId, { status: 'active' })
244
+ setThreadTurnStartedAt(threadId, Date.now())
245
+ if (turn?.id) {
246
+ setThreadTurnId(threadId, turn.id)
247
+ }
248
+ }
249
+ }
250
+
251
+ if (method === 'turn/completed' && params && typeof params === 'object') {
252
+ const { threadId, turn } = params as { threadId?: string; turn?: { id?: string; status?: string } }
253
+ // console.log('[HubConnection] turn/completed event for thread:', threadId)
254
+ if (threadId) {
255
+ const startedAt = useAppStore.getState().threadTurnStartedAt[threadId]
256
+ if (startedAt) {
257
+ const duration = Math.floor((Date.now() - startedAt) / 1000)
258
+ setThreadLastTurnDuration(threadId, duration)
259
+ }
260
+ updateThread(threadId, { status: 'idle' })
261
+ setThreadTurnId(threadId, null)
262
+ setThreadTurnStartedAt(threadId, null)
263
+ void dispatchQueuedMessage(profileId, threadId)
264
+ }
265
+ if (turn?.id && turn.status) {
266
+ updateReviewSession(turn.id, {
267
+ status: turn.status === 'failed' ? 'failed' : 'completed',
268
+ completedAt: Date.now(),
269
+ })
270
+ }
271
+ }
272
+
273
+ if (method === 'turn/diff/updated' && params && typeof params === 'object') {
274
+ const { threadId, turnId, diff } = params as { threadId?: string; turnId?: string; diff?: string }
275
+ if (threadId && diff) {
276
+ const content = diff.length > 4000 ? `${diff.slice(0, 4000)}\n…` : diff
277
+ upsertMessage(threadId, {
278
+ id: `diff-${turnId ?? threadId}`,
279
+ role: 'assistant',
280
+ kind: 'file',
281
+ title: 'Diff · updated',
282
+ content,
283
+ timestamp: nowTimestamp(),
284
+ })
285
+ }
286
+ }
287
+
288
+ if (method === 'turn/plan/updated' && params && typeof params === 'object') {
289
+ const { threadId, turnId, plan, explanation } = params as {
290
+ threadId?: string
291
+ turnId?: string
292
+ plan?: Array<{ step?: string; status?: string }>
293
+ explanation?: string
294
+ }
295
+ if (threadId && Array.isArray(plan)) {
296
+ const steps = plan
297
+ .map((entry) => `${entry.status ?? 'pending'} · ${entry.step ?? ''}`.trim())
298
+ .filter(Boolean)
299
+ .join('\n')
300
+ const content = [explanation, steps].filter(Boolean).join('\n\n')
301
+ upsertMessage(threadId, {
302
+ id: `plan-${turnId ?? threadId}`,
303
+ role: 'assistant',
304
+ kind: 'tool',
305
+ title: 'Plan',
306
+ content: content || 'Plan updated.',
307
+ timestamp: nowTimestamp(),
308
+ })
309
+ }
310
+ }
311
+
312
+ if (method === 'thread/tokenUsage/updated' && params && typeof params === 'object') {
313
+ const { threadId } = params as { threadId?: string }
314
+ const usage = (params as { usage?: unknown }).usage ?? (params as { tokenUsage?: unknown }).tokenUsage
315
+ if (threadId && usage) {
316
+ setThreadTokenUsage(threadId, usage)
317
+ }
318
+ }
319
+
320
+ if (method === 'item/agentMessage/delta' && params && typeof params === 'object') {
321
+ const { threadId, itemId, delta } = params as {
322
+ threadId?: string
323
+ itemId?: string
324
+ delta?: string
325
+ }
326
+ if (threadId && itemId && delta) {
327
+ appendMessageDelta(threadId, itemId, delta)
328
+ }
329
+ }
330
+
331
+ if (method === 'item/reasoning/summaryTextDelta' && params && typeof params === 'object') {
332
+ const { threadId, itemId, delta } = params as {
333
+ threadId?: string
334
+ itemId?: string
335
+ delta?: string
336
+ }
337
+ if (threadId && itemId && delta) {
338
+ appendMessageDelta(threadId, itemId, delta)
339
+ }
340
+ }
341
+
342
+ if (method === 'item/reasoning/summaryPartAdded' && params && typeof params === 'object') {
343
+ const { threadId, itemId } = params as { threadId?: string; itemId?: string }
344
+ if (threadId && itemId) {
345
+ appendMessageDelta(threadId, itemId, '\n\n')
346
+ }
347
+ }
348
+
349
+ if (method === 'item/reasoning/textDelta' && params && typeof params === 'object') {
350
+ const { threadId, itemId, delta } = params as {
351
+ threadId?: string
352
+ itemId?: string
353
+ delta?: string
354
+ }
355
+ if (threadId && itemId && delta) {
356
+ appendMessageDelta(threadId, itemId, delta)
357
+ }
358
+ }
359
+
360
+ if (method === 'item/commandExecution/outputDelta' && params && typeof params === 'object') {
361
+ const { threadId, itemId, delta } = params as {
362
+ threadId?: string
363
+ itemId?: string
364
+ delta?: string
365
+ }
366
+ if (threadId && itemId && delta) {
367
+ appendMessageDelta(threadId, itemId, delta)
368
+ }
369
+ }
370
+
371
+ if (method === 'item/fileChange/outputDelta' && params && typeof params === 'object') {
372
+ const { threadId, itemId, delta } = params as {
373
+ threadId?: string
374
+ itemId?: string
375
+ delta?: string
376
+ }
377
+ if (threadId && itemId && delta) {
378
+ appendMessageDelta(threadId, itemId, delta)
379
+ }
380
+ }
381
+
382
+ if (method === 'item/started' && params && typeof params === 'object') {
383
+ const { item, threadId, turnId } = params as {
384
+ item?: ItemPayload & { review?: string }
385
+ threadId?: string
386
+ turnId?: string
387
+ }
388
+ if (threadId && item?.type === 'agentMessage' && item.id) {
389
+ ensureAssistantMessage(threadId, item.id)
390
+ return
391
+ }
392
+ if (threadId && item?.type === 'enteredReviewMode') {
393
+ const sessionId = turnId ?? item.id ?? `review-${Date.now()}`
394
+ upsertReviewSession({
395
+ id: sessionId,
396
+ threadId,
397
+ profileId,
398
+ status: 'running',
399
+ startedAt: Date.now(),
400
+ label: typeof item.review === 'string' ? item.review : 'Review',
401
+ })
402
+ }
403
+ if (threadId && isThreadItem(item)) {
404
+ const systemMessage = buildSystemMessage(item)
405
+ if (systemMessage) {
406
+ systemMessage.timestamp = nowTimestamp()
407
+ upsertMessage(threadId, systemMessage)
408
+ }
409
+ }
410
+ }
411
+
412
+ if (method === 'item/completed' && params && typeof params === 'object') {
413
+ const { item, threadId, turnId } = params as {
414
+ item?: ItemPayload & { review?: string }
415
+ threadId?: string
416
+ turnId?: string
417
+ }
418
+ if (threadId && item?.type === 'exitedReviewMode') {
419
+ const sessionId = turnId ?? item.id
420
+ if (sessionId) {
421
+ updateReviewSession(sessionId, {
422
+ status: 'completed',
423
+ completedAt: Date.now(),
424
+ review: typeof item.review === 'string' ? item.review : undefined,
425
+ })
426
+ }
427
+ }
428
+ if (threadId && isThreadItem(item)) {
429
+ const systemMessage = buildSystemMessage(item)
430
+ if (systemMessage) {
431
+ systemMessage.timestamp = nowTimestamp()
432
+ upsertMessage(threadId, systemMessage)
433
+ }
434
+ }
435
+ }
436
+
437
+ if (method === 'error' && params && typeof params === 'object') {
438
+ const { threadId, error } = params as { threadId?: string; error?: { message?: string } }
439
+ if (threadId) {
440
+ const message = error?.message ?? 'Unknown error.'
441
+ addSystemMessage(threadId, 'Error', message)
442
+ }
443
+ }
444
+ }
445
+
446
+ if (event.type === 'profile.exit') {
447
+ updateAccount(event.profileId, (prev) => ({
448
+ ...prev,
449
+ status: 'offline',
450
+ }))
451
+ }
452
+
453
+ if (event.type === 'profile.error') {
454
+ updateAccount(event.profileId, (prev) => ({
455
+ ...prev,
456
+ status: 'degraded',
457
+ }))
458
+ }
459
+
460
+ if (event.type === 'rpc.serverRequest') {
461
+ const { method, params, profileId, id } = event
462
+ if (method === 'item/commandExecution/requestApproval' && params && typeof params === 'object') {
463
+ const parsed = params as {
464
+ itemId?: string
465
+ threadId?: string
466
+ parsedCmd?: string
467
+ command?: string[]
468
+ }
469
+ addApproval({
470
+ id: parsed.itemId ?? String(id),
471
+ requestId: id,
472
+ profileId,
473
+ threadId: parsed.threadId ?? '',
474
+ type: 'command',
475
+ payload: parsed.parsedCmd ?? parsed.command?.join(' ') ?? 'Command approval required',
476
+ status: 'pending',
477
+ })
478
+ }
479
+
480
+ if (method === 'item/fileChange/requestApproval' && params && typeof params === 'object') {
481
+ const parsed = params as {
482
+ itemId?: string
483
+ threadId?: string
484
+ reason?: string
485
+ }
486
+ addApproval({
487
+ id: parsed.itemId ?? String(id),
488
+ requestId: id,
489
+ profileId,
490
+ threadId: parsed.threadId ?? '',
491
+ type: 'file',
492
+ payload: parsed.reason ?? 'File changes requested',
493
+ status: 'pending',
494
+ })
495
+ }
496
+ }
497
+ })
498
+
499
+ for (const profile of profiles) {
500
+ await hubClient.startProfile(profile.id)
501
+
502
+ const accountResult = await safeRequest<AccountReadResult>(
503
+ profile.id,
504
+ 'account/read',
505
+ { refreshToken: false }
506
+ )
507
+ if (accountResult) {
508
+ updateAccount(profile.id, (prev) => ({
509
+ ...prev,
510
+ status: accountStatusFromRead(accountResult),
511
+ email: accountResult.account?.email ?? prev.email,
512
+ plan: accountResult.account?.planType ?? prev.plan,
513
+ }))
514
+ } else {
515
+ updateAccount(profile.id, (prev) => ({
516
+ ...prev,
517
+ status: 'degraded',
518
+ }))
519
+ }
520
+
521
+ const limits = await safeRequest<RateLimitResult>(
522
+ profile.id,
523
+ 'account/rateLimits/read'
524
+ )
525
+ if (limits) {
526
+ const usage = parseUsage(limits)
527
+ const rate = usage?.primary?.usedPercent
528
+ updateAccount(profile.id, (prev) => ({
529
+ ...prev,
530
+ rateLimit: typeof rate === 'number' ? Math.round(rate) : prev.rateLimit,
531
+ usage,
532
+ }))
533
+ }
534
+
535
+ const threads = await safeRequest<ThreadListResult>(profile.id, 'thread/list', {
536
+ limit: 50,
537
+ })
538
+ if (threads) {
539
+ setThreadsForAccount(profile.id, toThreads(profile.id, threads))
540
+ if (profile.id === profiles[0]?.id && threads.data?.length) {
541
+ setSelectedThreadId(threads.data[0].id)
542
+ }
543
+ }
544
+
545
+ const models = await fetchAllModels(profile.id)
546
+ if (models.length) {
547
+ setModelsForAccount(profile.id, models)
548
+ }
549
+ }
550
+ } catch (error) {
551
+ console.error(error)
552
+ setConnectionStatus('error')
553
+ }
554
+ }
555
+
556
+ bootstrap()
557
+
558
+ return () => {
559
+ disposed = true
560
+ unsubscribe?.()
561
+ hubClient.disconnect()
562
+ }
563
+ }, [
564
+ addApproval,
565
+ addMessage,
566
+ appendMessageDelta,
567
+ upsertMessage,
568
+ ensureAssistantMessage,
569
+ enqueueMessage,
570
+ setAccounts,
571
+ setAccountLoginId,
572
+ setConnectionStatus,
573
+ setModelsForAccount,
574
+ setSelectedAccountId,
575
+ setSelectedThreadId,
576
+ shiftQueuedMessage,
577
+ setThreadsForAccount,
578
+ setThreadTokenUsage,
579
+ setThreadTurnId,
580
+ setThreadTurnStartedAt,
581
+ setThreadLastTurnDuration,
582
+ updateAccount,
583
+ upsertReviewSession,
584
+ updateReviewSession,
585
+ updateThread,
586
+ ])
587
+ }
@@ -0,0 +1,76 @@
1
+ import { useEffect, useState } from 'react'
2
+
3
+ const MOBILE_BREAKPOINT = 768
4
+ const TABLET_BREAKPOINT = 1024
5
+
6
+ export function useIsMobile() {
7
+ const [isMobile, setIsMobile] = useState(() => {
8
+ if (typeof window === 'undefined') return false
9
+ return window.innerWidth < MOBILE_BREAKPOINT
10
+ })
11
+
12
+ useEffect(() => {
13
+ const mediaQuery = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`)
14
+
15
+ const handleChange = (e: MediaQueryListEvent) => {
16
+ setIsMobile(e.matches)
17
+ }
18
+
19
+ setIsMobile(mediaQuery.matches)
20
+
21
+ mediaQuery.addEventListener('change', handleChange)
22
+ return () => mediaQuery.removeEventListener('change', handleChange)
23
+ }, [])
24
+
25
+ return isMobile
26
+ }
27
+
28
+ export function useIsTablet() {
29
+ const [isTablet, setIsTablet] = useState(() => {
30
+ if (typeof window === 'undefined') return false
31
+ return window.innerWidth >= MOBILE_BREAKPOINT && window.innerWidth < TABLET_BREAKPOINT
32
+ })
33
+
34
+ useEffect(() => {
35
+ const mediaQuery = window.matchMedia(
36
+ `(min-width: ${MOBILE_BREAKPOINT}px) and (max-width: ${TABLET_BREAKPOINT - 1}px)`
37
+ )
38
+
39
+ const handleChange = (e: MediaQueryListEvent) => {
40
+ setIsTablet(e.matches)
41
+ }
42
+
43
+ setIsTablet(mediaQuery.matches)
44
+ mediaQuery.addEventListener('change', handleChange)
45
+ return () => mediaQuery.removeEventListener('change', handleChange)
46
+ }, [])
47
+
48
+ return isTablet
49
+ }
50
+
51
+ export function useBreakpoint() {
52
+ const isMobile = useIsMobile()
53
+ const isTablet = useIsTablet()
54
+
55
+ if (isMobile) return 'mobile'
56
+ if (isTablet) return 'tablet'
57
+ return 'desktop'
58
+ }
59
+
60
+ export function useDynamicViewportHeight() {
61
+ useEffect(() => {
62
+ const updateHeight = () => {
63
+ const vh = window.innerHeight * 0.01
64
+ document.documentElement.style.setProperty('--vh', `${vh}px`)
65
+ }
66
+
67
+ updateHeight()
68
+ window.addEventListener('resize', updateHeight)
69
+ window.addEventListener('orientationchange', updateHeight)
70
+
71
+ return () => {
72
+ window.removeEventListener('resize', updateHeight)
73
+ window.removeEventListener('orientationchange', updateHeight)
74
+ }
75
+ }, [])
76
+ }