bingocode 1.0.1 → 1.0.3

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 (187) hide show
  1. package/bin/bingo-win.cjs +34 -3
  2. package/desktop/README.md +30 -0
  3. package/desktop/bunfig.toml +1 -0
  4. package/desktop/index.html +17 -0
  5. package/desktop/package.json +55 -0
  6. package/desktop/pnpm-lock.yaml +3832 -0
  7. package/desktop/public/app-icon.jpg +0 -0
  8. package/desktop/public/fonts/inter-latin-ext.woff2 +0 -0
  9. package/desktop/public/fonts/inter-latin.woff2 +0 -0
  10. package/desktop/public/fonts/jetbrains-mono-latin-ext.woff2 +0 -0
  11. package/desktop/public/fonts/jetbrains-mono-latin.woff2 +0 -0
  12. package/desktop/public/fonts/manrope-latin-ext.woff2 +0 -0
  13. package/desktop/public/fonts/manrope-latin.woff2 +0 -0
  14. package/desktop/public/fonts/material-symbols-outlined.woff2 +0 -0
  15. package/desktop/public/icons/bilibili.svg +1 -0
  16. package/desktop/public/icons/douyin.svg +1 -0
  17. package/desktop/public/icons/github.svg +3 -0
  18. package/desktop/public/icons/xiaohongshu.svg +1 -0
  19. package/desktop/scripts/build-macos-arm64.sh +270 -0
  20. package/desktop/scripts/build-sidecars.ts +183 -0
  21. package/desktop/scripts/build-windows-x64.ps1 +295 -0
  22. package/desktop/scripts/scan-missing-imports.ts +235 -0
  23. package/desktop/sidecars/claude-sidecar.ts +156 -0
  24. package/desktop/src/App.tsx +5 -0
  25. package/desktop/src/__tests__/agentsSettings.test.tsx +349 -0
  26. package/desktop/src/__tests__/pages.test.tsx +290 -0
  27. package/desktop/src/__tests__/skillsSettings.test.tsx +205 -0
  28. package/desktop/src/api/adapters.ts +12 -0
  29. package/desktop/src/api/agents.ts +36 -0
  30. package/desktop/src/api/cliTasks.ts +28 -0
  31. package/desktop/src/api/client.ts +63 -0
  32. package/desktop/src/api/computerUse.ts +76 -0
  33. package/desktop/src/api/filesystem.ts +30 -0
  34. package/desktop/src/api/hahaOAuth.ts +38 -0
  35. package/desktop/src/api/models.ts +28 -0
  36. package/desktop/src/api/providers.ts +63 -0
  37. package/desktop/src/api/search.ts +29 -0
  38. package/desktop/src/api/sessions.ts +56 -0
  39. package/desktop/src/api/settings.ts +20 -0
  40. package/desktop/src/api/skills.ts +19 -0
  41. package/desktop/src/api/tasks.ts +36 -0
  42. package/desktop/src/api/teams.ts +44 -0
  43. package/desktop/src/api/websocket.ts +164 -0
  44. package/desktop/src/components/chat/AskUserQuestion.tsx +268 -0
  45. package/desktop/src/components/chat/AssistantMessage.tsx +29 -0
  46. package/desktop/src/components/chat/AttachmentGallery.tsx +113 -0
  47. package/desktop/src/components/chat/ChatInput.tsx +622 -0
  48. package/desktop/src/components/chat/CodeViewer.tsx +161 -0
  49. package/desktop/src/components/chat/ComputerUsePermissionModal.test.tsx +174 -0
  50. package/desktop/src/components/chat/ComputerUsePermissionModal.tsx +311 -0
  51. package/desktop/src/components/chat/DiffViewer.tsx +157 -0
  52. package/desktop/src/components/chat/FileSearchMenu.tsx +198 -0
  53. package/desktop/src/components/chat/ImageGalleryModal.tsx +91 -0
  54. package/desktop/src/components/chat/InlineImageGallery.tsx +106 -0
  55. package/desktop/src/components/chat/InlineTaskSummary.tsx +60 -0
  56. package/desktop/src/components/chat/MermaidRenderer.test.tsx +98 -0
  57. package/desktop/src/components/chat/MermaidRenderer.tsx +361 -0
  58. package/desktop/src/components/chat/MessageActionBar.tsx +27 -0
  59. package/desktop/src/components/chat/MessageList.test.tsx +313 -0
  60. package/desktop/src/components/chat/MessageList.tsx +249 -0
  61. package/desktop/src/components/chat/PermissionDialog.tsx +262 -0
  62. package/desktop/src/components/chat/SessionTaskBar.test.tsx +99 -0
  63. package/desktop/src/components/chat/SessionTaskBar.tsx +159 -0
  64. package/desktop/src/components/chat/StreamingIndicator.tsx +41 -0
  65. package/desktop/src/components/chat/TerminalChrome.tsx +35 -0
  66. package/desktop/src/components/chat/ThinkingBlock.tsx +87 -0
  67. package/desktop/src/components/chat/ToolCallBlock.tsx +247 -0
  68. package/desktop/src/components/chat/ToolCallGroup.tsx +617 -0
  69. package/desktop/src/components/chat/ToolResultBlock.tsx +107 -0
  70. package/desktop/src/components/chat/UserMessage.tsx +38 -0
  71. package/desktop/src/components/chat/chatBlocks.test.tsx +136 -0
  72. package/desktop/src/components/chat/clipboard.ts +25 -0
  73. package/desktop/src/components/chat/composerUtils.test.ts +55 -0
  74. package/desktop/src/components/chat/composerUtils.ts +149 -0
  75. package/desktop/src/components/controls/ModelSelector.tsx +156 -0
  76. package/desktop/src/components/controls/PermissionModeSelector.tsx +229 -0
  77. package/desktop/src/components/layout/AppShell.tsx +107 -0
  78. package/desktop/src/components/layout/ContentRouter.tsx +27 -0
  79. package/desktop/src/components/layout/ProjectFilter.tsx +126 -0
  80. package/desktop/src/components/layout/Sidebar.test.tsx +158 -0
  81. package/desktop/src/components/layout/Sidebar.tsx +384 -0
  82. package/desktop/src/components/layout/StatusBar.tsx +31 -0
  83. package/desktop/src/components/layout/TabBar.test.tsx +136 -0
  84. package/desktop/src/components/layout/TabBar.tsx +318 -0
  85. package/desktop/src/components/layout/TitleBar.tsx +96 -0
  86. package/desktop/src/components/layout/WindowControls.test.tsx +69 -0
  87. package/desktop/src/components/layout/WindowControls.tsx +89 -0
  88. package/desktop/src/components/markdown/MarkdownRenderer.test.tsx +100 -0
  89. package/desktop/src/components/markdown/MarkdownRenderer.tsx +229 -0
  90. package/desktop/src/components/settings/ClaudeOfficialLogin.tsx +107 -0
  91. package/desktop/src/components/shared/Button.tsx +63 -0
  92. package/desktop/src/components/shared/CopyButton.tsx +58 -0
  93. package/desktop/src/components/shared/DirectoryPicker.tsx +316 -0
  94. package/desktop/src/components/shared/Dropdown.tsx +91 -0
  95. package/desktop/src/components/shared/Input.tsx +38 -0
  96. package/desktop/src/components/shared/Modal.tsx +65 -0
  97. package/desktop/src/components/shared/ProjectContextChip.tsx +30 -0
  98. package/desktop/src/components/shared/Spinner.tsx +30 -0
  99. package/desktop/src/components/shared/Textarea.tsx +38 -0
  100. package/desktop/src/components/shared/Toast.tsx +47 -0
  101. package/desktop/src/components/shared/UpdateChecker.tsx +90 -0
  102. package/desktop/src/components/skills/SkillDetail.test.tsx +89 -0
  103. package/desktop/src/components/skills/SkillDetail.tsx +403 -0
  104. package/desktop/src/components/skills/SkillList.tsx +254 -0
  105. package/desktop/src/components/tasks/DayOfWeekPicker.tsx +57 -0
  106. package/desktop/src/components/tasks/NewTaskModal.tsx +407 -0
  107. package/desktop/src/components/tasks/PromptEditor.tsx +74 -0
  108. package/desktop/src/components/tasks/TaskEmptyState.tsx +30 -0
  109. package/desktop/src/components/tasks/TaskList.tsx +46 -0
  110. package/desktop/src/components/tasks/TaskRow.tsx +253 -0
  111. package/desktop/src/components/tasks/TaskRunsPanel.tsx +195 -0
  112. package/desktop/src/components/teams/TeamStatusBar.tsx +147 -0
  113. package/desktop/src/config/providerPresets.ts +78 -0
  114. package/desktop/src/config/spinnerVerbs.ts +193 -0
  115. package/desktop/src/hooks/useKeyboardShortcuts.ts +60 -0
  116. package/desktop/src/i18n/index.ts +54 -0
  117. package/desktop/src/i18n/locales/en.ts +670 -0
  118. package/desktop/src/i18n/locales/zh.ts +670 -0
  119. package/desktop/src/lib/__tests__/cronDescribe.test.ts +93 -0
  120. package/desktop/src/lib/cronDescribe.ts +188 -0
  121. package/desktop/src/lib/desktopRuntime.ts +54 -0
  122. package/desktop/src/lib/parseRunOutput.ts +79 -0
  123. package/desktop/src/main.tsx +13 -0
  124. package/desktop/src/mocks/data.ts +202 -0
  125. package/desktop/src/pages/ActiveSession.test.tsx +181 -0
  126. package/desktop/src/pages/ActiveSession.tsx +219 -0
  127. package/desktop/src/pages/AdapterSettings.tsx +375 -0
  128. package/desktop/src/pages/AgentTeams.tsx +200 -0
  129. package/desktop/src/pages/ComputerUseSettings.tsx +420 -0
  130. package/desktop/src/pages/EmptySession.tsx +518 -0
  131. package/desktop/src/pages/NewTaskModal.tsx +346 -0
  132. package/desktop/src/pages/ScheduledTasks.tsx +66 -0
  133. package/desktop/src/pages/ScheduledTasksEmpty.tsx +152 -0
  134. package/desktop/src/pages/ScheduledTasksList.tsx +416 -0
  135. package/desktop/src/pages/SessionControls.tsx +460 -0
  136. package/desktop/src/pages/Settings.tsx +1448 -0
  137. package/desktop/src/pages/ToolInspection.tsx +235 -0
  138. package/desktop/src/stores/adapterStore.ts +106 -0
  139. package/desktop/src/stores/agentStore.ts +34 -0
  140. package/desktop/src/stores/chatStore.test.ts +505 -0
  141. package/desktop/src/stores/chatStore.ts +850 -0
  142. package/desktop/src/stores/cliTaskStore.ts +152 -0
  143. package/desktop/src/stores/hahaOAuthStore.test.ts +77 -0
  144. package/desktop/src/stores/hahaOAuthStore.ts +97 -0
  145. package/desktop/src/stores/providerStore.ts +101 -0
  146. package/desktop/src/stores/sessionStore.test.ts +63 -0
  147. package/desktop/src/stores/sessionStore.ts +102 -0
  148. package/desktop/src/stores/settingsStore.ts +120 -0
  149. package/desktop/src/stores/skillStore.ts +51 -0
  150. package/desktop/src/stores/tabStore.ts +169 -0
  151. package/desktop/src/stores/taskStore.ts +68 -0
  152. package/desktop/src/stores/teamStore.ts +344 -0
  153. package/desktop/src/stores/uiStore.ts +100 -0
  154. package/desktop/src/stores/updateStore.test.ts +71 -0
  155. package/desktop/src/stores/updateStore.ts +221 -0
  156. package/desktop/src/theme/globals.css +465 -0
  157. package/desktop/src/types/adapter.ts +33 -0
  158. package/desktop/src/types/chat.ts +152 -0
  159. package/desktop/src/types/cliTask.ts +24 -0
  160. package/desktop/src/types/provider.ts +62 -0
  161. package/desktop/src/types/session.ts +27 -0
  162. package/desktop/src/types/settings.ts +22 -0
  163. package/desktop/src/types/skill.ts +38 -0
  164. package/desktop/src/types/task.ts +56 -0
  165. package/desktop/src/types/team.ts +38 -0
  166. package/desktop/src-tauri/Cargo.lock +5549 -0
  167. package/desktop/src-tauri/Cargo.toml +20 -0
  168. package/desktop/src-tauri/app-icon.svg +13 -0
  169. package/desktop/src-tauri/build.rs +3 -0
  170. package/desktop/src-tauri/capabilities/default.json +106 -0
  171. package/desktop/src-tauri/icons/android/mipmap-anydpi-v26/ic_launcher.xml +5 -0
  172. package/desktop/src-tauri/icons/android/values/ic_launcher_background.xml +4 -0
  173. package/desktop/src-tauri/icons/icon.icns +0 -0
  174. package/desktop/src-tauri/icons/icon.ico +0 -0
  175. package/desktop/src-tauri/src/lib.rs +408 -0
  176. package/desktop/src-tauri/src/main.rs +6 -0
  177. package/desktop/src-tauri/tauri.conf.json +78 -0
  178. package/desktop/src-tauri/tauri.macos.conf.json +18 -0
  179. package/desktop/src-tauri/tauri.release-ci.json +5 -0
  180. package/desktop/src-tauri/tauri.windows.conf.json +16 -0
  181. package/desktop/src-tauri/windows-installer-hooks.nsh +17 -0
  182. package/desktop/tsconfig.json +25 -0
  183. package/desktop/vite.config.ts +26 -0
  184. package/desktop/vitest.config.ts +18 -0
  185. package/package.json +1 -1
  186. package/src/commands/desktop/desktop.tsx +9 -0
  187. package/src/commands/desktop/index.ts +26 -0
@@ -0,0 +1,152 @@
1
+ import { create } from 'zustand'
2
+ import { cliTasksApi } from '../api/cliTasks'
3
+ import type { CLITask, TaskStatus } from '../types/cliTask'
4
+
5
+ type TodoItem = {
6
+ content: string
7
+ status: string
8
+ activeForm?: string
9
+ }
10
+
11
+ type CLITaskStore = {
12
+ /** Current session ID being tracked */
13
+ sessionId: string | null
14
+ /** Tasks for the current session */
15
+ tasks: CLITask[]
16
+ /** Whether the task bar is expanded */
17
+ expanded: boolean
18
+ /** True when all tasks completed and the user already continued chatting.
19
+ * Set during history load so the sticky bar is suppressed on page refresh. */
20
+ completedAndDismissed: boolean
21
+ /** Snapshot of the completed task set that was dismissed */
22
+ dismissedCompletionKey: string | null
23
+
24
+ /** Fetch tasks for a given session (uses sessionId as taskListId) */
25
+ fetchSessionTasks: (sessionId: string) => Promise<void>
26
+ /** Refresh tasks for the currently tracked session */
27
+ refreshTasks: () => Promise<void>
28
+ /** Update tasks from TodoWrite V1 tool input (in-memory, no disk read needed) */
29
+ setTasksFromTodos: (todos: TodoItem[]) => void
30
+ /** Mark that completed tasks were already dismissed (conversation continued) */
31
+ markCompletedAndDismissed: () => void
32
+ /** Clear task tracking state */
33
+ clearTasks: () => void
34
+ /** Toggle expanded state */
35
+ toggleExpanded: () => void
36
+ }
37
+
38
+ function buildCompletedTaskKey(tasks: CLITask[]): string | null {
39
+ if (tasks.length === 0 || tasks.some((task) => task.status !== 'completed')) return null
40
+
41
+ return tasks
42
+ .map((task) => [
43
+ task.taskListId,
44
+ task.id,
45
+ task.subject,
46
+ task.status,
47
+ task.activeForm ?? '',
48
+ task.owner ?? '',
49
+ ].join('::'))
50
+ .join('|')
51
+ }
52
+
53
+ function resolveDismissState(tasks: CLITask[], dismissedCompletionKey: string | null) {
54
+ const completionKey = buildCompletedTaskKey(tasks)
55
+ const keepDismissed = completionKey !== null && completionKey === dismissedCompletionKey
56
+
57
+ return {
58
+ completedAndDismissed: keepDismissed,
59
+ dismissedCompletionKey: keepDismissed ? completionKey : null,
60
+ }
61
+ }
62
+
63
+ function mapTodosToTasks(todos: TodoItem[], sessionId: string | null): CLITask[] {
64
+ return todos.map((todo, index) => ({
65
+ id: String(index + 1),
66
+ subject: todo.content,
67
+ description: '',
68
+ activeForm: todo.activeForm,
69
+ status: (['pending', 'in_progress', 'completed'].includes(todo.status)
70
+ ? todo.status
71
+ : 'pending') as TaskStatus,
72
+ blocks: [],
73
+ blockedBy: [],
74
+ taskListId: sessionId || '',
75
+ }))
76
+ }
77
+
78
+ export const useCLITaskStore = create<CLITaskStore>((set, get) => ({
79
+ sessionId: null,
80
+ tasks: [],
81
+ expanded: false,
82
+ completedAndDismissed: false,
83
+ dismissedCompletionKey: null,
84
+
85
+ fetchSessionTasks: async (sessionId) => {
86
+ set({ sessionId })
87
+ try {
88
+ const { tasks } = await cliTasksApi.getTasksForList(sessionId)
89
+ // Only update if still tracking the same session
90
+ if (get().sessionId === sessionId) {
91
+ set((state) => ({
92
+ tasks,
93
+ ...resolveDismissState(tasks, state.dismissedCompletionKey),
94
+ }))
95
+ }
96
+ } catch {
97
+ // No tasks for this session — that's fine
98
+ if (get().sessionId === sessionId) {
99
+ set({ tasks: [], completedAndDismissed: false, dismissedCompletionKey: null })
100
+ }
101
+ }
102
+ },
103
+
104
+ refreshTasks: async () => {
105
+ const { sessionId } = get()
106
+ if (!sessionId) return
107
+ try {
108
+ const { tasks } = await cliTasksApi.getTasksForList(sessionId)
109
+ if (get().sessionId === sessionId) {
110
+ set((state) => ({
111
+ tasks,
112
+ ...resolveDismissState(tasks, state.dismissedCompletionKey),
113
+ }))
114
+ }
115
+ } catch {
116
+ // ignore
117
+ }
118
+ },
119
+
120
+ setTasksFromTodos: (todos) => {
121
+ const tasks = mapTodosToTasks(todos, get().sessionId)
122
+ set((state) => ({
123
+ tasks,
124
+ ...resolveDismissState(tasks, state.dismissedCompletionKey),
125
+ }))
126
+ },
127
+
128
+ markCompletedAndDismissed: () => {
129
+ const completionKey = buildCompletedTaskKey(get().tasks)
130
+ if (!completionKey) return
131
+
132
+ set({
133
+ completedAndDismissed: true,
134
+ dismissedCompletionKey: completionKey,
135
+ expanded: false,
136
+ })
137
+ },
138
+
139
+ clearTasks: () => {
140
+ set({
141
+ sessionId: null,
142
+ tasks: [],
143
+ completedAndDismissed: false,
144
+ dismissedCompletionKey: null,
145
+ expanded: false,
146
+ })
147
+ },
148
+
149
+ toggleExpanded: () => {
150
+ set((s) => ({ expanded: !s.expanded }))
151
+ },
152
+ }))
@@ -0,0 +1,77 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2
+
3
+ const { startMock, statusMock, logoutMock } = vi.hoisted(() => ({
4
+ startMock: vi.fn(),
5
+ statusMock: vi.fn(),
6
+ logoutMock: vi.fn(),
7
+ }))
8
+
9
+ vi.mock('../api/hahaOAuth', () => ({
10
+ hahaOAuthApi: {
11
+ start: startMock,
12
+ status: statusMock,
13
+ logout: logoutMock,
14
+ },
15
+ }))
16
+
17
+ import { useHahaOAuthStore } from './hahaOAuthStore'
18
+
19
+ const initialState = useHahaOAuthStore.getState()
20
+
21
+ describe('hahaOAuthStore', () => {
22
+ beforeEach(() => {
23
+ vi.useFakeTimers()
24
+ startMock.mockReset()
25
+ statusMock.mockReset()
26
+ logoutMock.mockReset()
27
+ useHahaOAuthStore.setState({
28
+ ...initialState,
29
+ status: null,
30
+ isPolling: false,
31
+ isLoading: false,
32
+ error: null,
33
+ })
34
+ })
35
+
36
+ afterEach(() => {
37
+ useHahaOAuthStore.getState().stopPolling()
38
+ useHahaOAuthStore.setState(initialState)
39
+ vi.useRealTimers()
40
+ })
41
+
42
+ it('login does not start polling until the browser launch succeeds', async () => {
43
+ startMock.mockResolvedValue({
44
+ authorizeUrl: 'http://localhost:3456/api/haha-oauth/callback',
45
+ state: 'state-123',
46
+ })
47
+
48
+ const result = await useHahaOAuthStore.getState().login()
49
+
50
+ expect(result.authorizeUrl).toContain('/api/haha-oauth/callback')
51
+ expect(useHahaOAuthStore.getState().isPolling).toBe(false)
52
+ })
53
+
54
+ it('startPolling stops after the status becomes logged in', async () => {
55
+ statusMock
56
+ .mockResolvedValueOnce({ loggedIn: false })
57
+ .mockResolvedValueOnce({
58
+ loggedIn: true,
59
+ expiresAt: Date.now() + 60_000,
60
+ scopes: ['user:inference'],
61
+ subscriptionType: 'max',
62
+ })
63
+
64
+ useHahaOAuthStore.getState().startPolling()
65
+ expect(useHahaOAuthStore.getState().isPolling).toBe(true)
66
+
67
+ await vi.advanceTimersByTimeAsync(2_000)
68
+ expect(useHahaOAuthStore.getState().isPolling).toBe(true)
69
+
70
+ await vi.advanceTimersByTimeAsync(2_000)
71
+ expect(useHahaOAuthStore.getState().status).toMatchObject({
72
+ loggedIn: true,
73
+ subscriptionType: 'max',
74
+ })
75
+ expect(useHahaOAuthStore.getState().isPolling).toBe(false)
76
+ })
77
+ })
@@ -0,0 +1,97 @@
1
+ // desktop/src/stores/hahaOAuthStore.ts
2
+
3
+ import { create } from 'zustand'
4
+ import { hahaOAuthApi, type HahaOAuthStatus } from '../api/hahaOAuth'
5
+
6
+ const POLL_INTERVAL_MS = 2_000
7
+
8
+ type HahaOAuthState = {
9
+ status: HahaOAuthStatus | null
10
+ isPolling: boolean
11
+ isLoading: boolean
12
+ error: string | null
13
+
14
+ fetchStatus: () => Promise<void>
15
+ login: () => Promise<{ authorizeUrl: string }>
16
+ logout: () => Promise<void>
17
+ startPolling: () => void
18
+ stopPolling: () => void
19
+ }
20
+
21
+ export const useHahaOAuthStore = create<HahaOAuthState>((set, get) => {
22
+ let pollTimer: ReturnType<typeof setTimeout> | null = null
23
+
24
+ return {
25
+ status: null,
26
+ isPolling: false,
27
+ isLoading: false,
28
+ error: null,
29
+
30
+ fetchStatus: async () => {
31
+ try {
32
+ const status = await hahaOAuthApi.status()
33
+ set({ status, error: null })
34
+ } catch (err) {
35
+ set({ error: err instanceof Error ? err.message : String(err) })
36
+ }
37
+ },
38
+
39
+ login: async () => {
40
+ set({ isLoading: true, error: null })
41
+ try {
42
+ const res = await hahaOAuthApi.start()
43
+ set({ isLoading: false })
44
+ return { authorizeUrl: res.authorizeUrl }
45
+ } catch (err) {
46
+ set({
47
+ isLoading: false,
48
+ error: err instanceof Error ? err.message : String(err),
49
+ })
50
+ throw err
51
+ }
52
+ },
53
+
54
+ logout: async () => {
55
+ get().stopPolling()
56
+ set({ isLoading: true })
57
+ try {
58
+ await hahaOAuthApi.logout()
59
+ set({ status: { loggedIn: false }, isLoading: false })
60
+ } catch (err) {
61
+ set({
62
+ isLoading: false,
63
+ error: err instanceof Error ? err.message : String(err),
64
+ })
65
+ throw err
66
+ }
67
+ },
68
+
69
+ startPolling: () => {
70
+ if (pollTimer) return
71
+ set({ isPolling: true })
72
+
73
+ const scheduleNext = () => {
74
+ pollTimer = setTimeout(async () => {
75
+ await get().fetchStatus()
76
+ const cur = get().status
77
+ if (cur && cur.loggedIn) {
78
+ get().stopPolling()
79
+ return
80
+ }
81
+ if (get().isPolling) {
82
+ scheduleNext()
83
+ }
84
+ }, POLL_INTERVAL_MS)
85
+ }
86
+ scheduleNext()
87
+ },
88
+
89
+ stopPolling: () => {
90
+ if (pollTimer) {
91
+ clearTimeout(pollTimer)
92
+ pollTimer = null
93
+ }
94
+ set({ isPolling: false })
95
+ },
96
+ }
97
+ })
@@ -0,0 +1,101 @@
1
+ // desktop/src/stores/providerStore.ts
2
+
3
+ import { create } from 'zustand'
4
+ import { providersApi } from '../api/providers'
5
+ import { useSettingsStore } from './settingsStore'
6
+ import type {
7
+ SavedProvider,
8
+ CreateProviderInput,
9
+ UpdateProviderInput,
10
+ TestProviderConfigInput,
11
+ ProviderTestResult,
12
+ } from '../types/provider'
13
+
14
+ // 与后端 src/server/api/models.ts 的 DEFAULT_MODEL 保持一致:
15
+ // 切回"官方"时把聊天页的 currentModel 重置到这个,避免残留第三方 provider
16
+ // 的 model id 在官方模型列表里找不到、ModelSelector 显示但不选中的状态。
17
+ const OFFICIAL_DEFAULT_MODEL_ID = 'claude-opus-4-7'
18
+
19
+ type ProviderStore = {
20
+ providers: SavedProvider[]
21
+ activeId: string | null
22
+ isLoading: boolean
23
+ error: string | null
24
+
25
+ fetchProviders: () => Promise<void>
26
+ createProvider: (input: CreateProviderInput) => Promise<SavedProvider>
27
+ updateProvider: (id: string, input: UpdateProviderInput) => Promise<SavedProvider>
28
+ deleteProvider: (id: string) => Promise<void>
29
+ activateProvider: (id: string) => Promise<void>
30
+ activateOfficial: () => Promise<void>
31
+ testProvider: (id: string, overrides?: { baseUrl?: string; modelId?: string; apiFormat?: string }) => Promise<ProviderTestResult>
32
+ testConfig: (input: TestProviderConfigInput) => Promise<ProviderTestResult>
33
+ }
34
+
35
+ export const useProviderStore = create<ProviderStore>((set, get) => ({
36
+ providers: [],
37
+ activeId: null,
38
+ isLoading: false,
39
+ error: null,
40
+
41
+ fetchProviders: async () => {
42
+ set({ isLoading: true, error: null })
43
+ try {
44
+ const { providers, activeId } = await providersApi.list()
45
+ set({ providers, activeId, isLoading: false })
46
+ } catch (err) {
47
+ set({ isLoading: false, error: err instanceof Error ? err.message : String(err) })
48
+ }
49
+ },
50
+
51
+ createProvider: async (input) => {
52
+ const { provider } = await providersApi.create(input)
53
+ await get().fetchProviders()
54
+ return provider
55
+ },
56
+
57
+ updateProvider: async (id, input) => {
58
+ const { provider } = await providersApi.update(id, input)
59
+ await get().fetchProviders()
60
+ return provider
61
+ },
62
+
63
+ deleteProvider: async (id) => {
64
+ await providersApi.delete(id)
65
+ await get().fetchProviders()
66
+ },
67
+
68
+ activateProvider: async (id) => {
69
+ await providersApi.activate(id)
70
+ await get().fetchProviders()
71
+ // 联动聊天页:把 currentModel 重置到新 provider 的 main model。
72
+ // 不这么做的话,用户在切换前手动选过的 model id (写进了 settings.json 的
73
+ // `model` 字段) 会继续被后端当作 explicit 返回,但新 provider 的模型列表
74
+ // 里没这个 id, ModelSelector 会卡在"显示旧名字 + radio 不选中"。
75
+ const provider = get().providers.find((p) => p.id === id)
76
+ if (provider) {
77
+ const settings = useSettingsStore.getState()
78
+ await settings.setModel(provider.models.main)
79
+ await settings.fetchAll()
80
+ }
81
+ },
82
+
83
+ activateOfficial: async () => {
84
+ await providersApi.activateOfficial()
85
+ await get().fetchProviders()
86
+ // 切回官方时同样重置 currentModel,避免残留第三方 model id。
87
+ const settings = useSettingsStore.getState()
88
+ await settings.setModel(OFFICIAL_DEFAULT_MODEL_ID)
89
+ await settings.fetchAll()
90
+ },
91
+
92
+ testProvider: async (id, overrides?) => {
93
+ const { result } = await providersApi.test(id, overrides)
94
+ return result
95
+ },
96
+
97
+ testConfig: async (input) => {
98
+ const { result } = await providersApi.testConfig(input)
99
+ return result
100
+ },
101
+ }))
@@ -0,0 +1,63 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2
+
3
+ const { createMock, listMock } = vi.hoisted(() => ({
4
+ createMock: vi.fn(),
5
+ listMock: vi.fn(),
6
+ }))
7
+
8
+ vi.mock('../api/sessions', () => ({
9
+ sessionsApi: {
10
+ create: createMock,
11
+ list: listMock,
12
+ delete: vi.fn(),
13
+ rename: vi.fn(),
14
+ },
15
+ }))
16
+
17
+ import { useSessionStore } from './sessionStore'
18
+
19
+ const initialState = useSessionStore.getState()
20
+
21
+ function delay(ms: number) {
22
+ return new Promise((resolve) => setTimeout(resolve, ms))
23
+ }
24
+
25
+ describe('sessionStore', () => {
26
+ beforeEach(() => {
27
+ createMock.mockReset()
28
+ listMock.mockReset()
29
+ useSessionStore.setState({
30
+ ...initialState,
31
+ sessions: [],
32
+ activeSessionId: null,
33
+ isLoading: false,
34
+ error: null,
35
+ selectedProjects: [],
36
+ availableProjects: [],
37
+ })
38
+ })
39
+
40
+ afterEach(() => {
41
+ useSessionStore.setState(initialState)
42
+ })
43
+
44
+ it('returns a new session id before the background refresh completes', async () => {
45
+ createMock.mockResolvedValue({ sessionId: 'session-optimistic-1' })
46
+ listMock.mockImplementation(() => new Promise(() => {}))
47
+
48
+ const result = await Promise.race([
49
+ useSessionStore.getState().createSession('D:/workspace/code/myself_code/cc-haha'),
50
+ delay(100).then(() => 'timed-out'),
51
+ ])
52
+
53
+ expect(result).toBe('session-optimistic-1')
54
+ expect(useSessionStore.getState().activeSessionId).toBe('session-optimistic-1')
55
+ expect(useSessionStore.getState().sessions[0]).toMatchObject({
56
+ id: 'session-optimistic-1',
57
+ title: 'New Session',
58
+ workDir: 'D:/workspace/code/myself_code/cc-haha',
59
+ workDirExists: true,
60
+ })
61
+ expect(listMock).toHaveBeenCalledOnce()
62
+ })
63
+ })
@@ -0,0 +1,102 @@
1
+ import { create } from 'zustand'
2
+ import { sessionsApi } from '../api/sessions'
3
+ import type { SessionListItem } from '../types/session'
4
+
5
+ type SessionStore = {
6
+ sessions: SessionListItem[]
7
+ activeSessionId: string | null
8
+ isLoading: boolean
9
+ error: string | null
10
+ selectedProjects: string[]
11
+ availableProjects: string[]
12
+
13
+ fetchSessions: (project?: string) => Promise<void>
14
+ createSession: (workDir?: string) => Promise<string>
15
+ deleteSession: (id: string) => Promise<void>
16
+ renameSession: (id: string, title: string) => Promise<void>
17
+ updateSessionTitle: (id: string, title: string) => void
18
+ setActiveSession: (id: string | null) => void
19
+ setSelectedProjects: (projects: string[]) => void
20
+ }
21
+
22
+ export const useSessionStore = create<SessionStore>((set, get) => ({
23
+ sessions: [],
24
+ activeSessionId: null,
25
+ isLoading: false,
26
+ error: null,
27
+ selectedProjects: [],
28
+ availableProjects: [],
29
+
30
+ fetchSessions: async (project?: string) => {
31
+ set({ isLoading: true, error: null })
32
+ try {
33
+ const { sessions: raw } = await sessionsApi.list({ project, limit: 100 })
34
+ // Deduplicate by session ID — keep the most recently modified entry
35
+ const byId = new Map<string, SessionListItem>()
36
+ for (const s of raw) {
37
+ const existing = byId.get(s.id)
38
+ if (!existing || new Date(s.modifiedAt) > new Date(existing.modifiedAt)) {
39
+ byId.set(s.id, s)
40
+ }
41
+ }
42
+ const sessions = [...byId.values()]
43
+ const availableProjects = [...new Set(sessions.map((s) => s.projectPath).filter(Boolean))].sort()
44
+ set({ sessions, availableProjects, isLoading: false })
45
+ } catch (err) {
46
+ set({ error: (err as Error).message, isLoading: false })
47
+ }
48
+ },
49
+
50
+ createSession: async (workDir?: string) => {
51
+ const { sessionId: id } = await sessionsApi.create(workDir || undefined)
52
+ const now = new Date().toISOString()
53
+ const optimisticSession: SessionListItem = {
54
+ id,
55
+ title: 'New Session',
56
+ createdAt: now,
57
+ modifiedAt: now,
58
+ messageCount: 0,
59
+ projectPath: '',
60
+ workDir: workDir ?? null,
61
+ workDirExists: true,
62
+ }
63
+
64
+ set((state) => ({
65
+ sessions: state.sessions.some((session) => session.id === id)
66
+ ? state.sessions
67
+ : [optimisticSession, ...state.sessions],
68
+ activeSessionId: id,
69
+ }))
70
+
71
+ void get().fetchSessions()
72
+ return id
73
+ },
74
+
75
+ deleteSession: async (id: string) => {
76
+ await sessionsApi.delete(id)
77
+ set((s) => ({
78
+ sessions: s.sessions.filter((session) => session.id !== id),
79
+ activeSessionId: s.activeSessionId === id ? null : s.activeSessionId,
80
+ }))
81
+ },
82
+
83
+ renameSession: async (id: string, title: string) => {
84
+ await sessionsApi.rename(id, title)
85
+ set((s) => ({
86
+ sessions: s.sessions.map((session) =>
87
+ session.id === id ? { ...session, title } : session,
88
+ ),
89
+ }))
90
+ },
91
+
92
+ updateSessionTitle: (id, title) => {
93
+ set((s) => ({
94
+ sessions: s.sessions.map((session) =>
95
+ session.id === id ? { ...session, title } : session,
96
+ ),
97
+ }))
98
+ },
99
+
100
+ setActiveSession: (id) => set({ activeSessionId: id }),
101
+ setSelectedProjects: (projects) => set({ selectedProjects: projects }),
102
+ }))