@myrialabs/clopen 0.3.7 → 0.3.9

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 (149) hide show
  1. package/backend/auth/permissions.ts +41 -2
  2. package/backend/chat/stream-manager.ts +71 -0
  3. package/backend/database/migrations/030_add_files_panel_state_to_user_projects.ts +34 -0
  4. package/backend/database/migrations/031_create_db_client_tables.ts +74 -0
  5. package/backend/database/migrations/032_seed_copilot_provider.ts +19 -0
  6. package/backend/database/migrations/033_seed_codex_provider.ts +19 -0
  7. package/backend/database/migrations/034_seed_qwen_provider.ts +119 -0
  8. package/backend/database/migrations/035_add_owner_to_db_client_connections.ts +34 -0
  9. package/backend/database/migrations/index.ts +43 -1
  10. package/backend/database/queries/db-client-connection-queries.ts +343 -0
  11. package/backend/database/queries/db-client-query-history-queries.ts +113 -0
  12. package/backend/database/queries/engine-queries.ts +12 -0
  13. package/backend/database/queries/index.ts +3 -1
  14. package/backend/database/queries/project-queries.ts +28 -0
  15. package/backend/db-client/connection-manager.ts +257 -0
  16. package/backend/db-client/drivers/bun-sql-helpers.ts +41 -0
  17. package/backend/db-client/drivers/mongodb.ts +358 -0
  18. package/backend/db-client/drivers/mysql.ts +385 -0
  19. package/backend/db-client/drivers/postgres.ts +446 -0
  20. package/backend/db-client/drivers/redis.ts +267 -0
  21. package/backend/db-client/drivers/sql-builders.ts +215 -0
  22. package/backend/db-client/drivers/sqlite.ts +346 -0
  23. package/backend/db-client/drivers/types.ts +92 -0
  24. package/backend/db-client/query-executor.ts +126 -0
  25. package/backend/db-client/ssh-tunnel.ts +122 -0
  26. package/backend/engine/README.md +1702 -0
  27. package/backend/engine/adapters/claude/models.ts +141 -0
  28. package/backend/engine/adapters/claude/stream.ts +1 -1
  29. package/backend/engine/adapters/codex/credential.ts +179 -0
  30. package/backend/engine/adapters/codex/error-handler.ts +65 -0
  31. package/backend/engine/adapters/codex/index.ts +1 -0
  32. package/backend/engine/adapters/codex/message-converter.ts +646 -0
  33. package/backend/engine/adapters/codex/models.ts +105 -0
  34. package/backend/engine/adapters/codex/patch-rollout.ts +169 -0
  35. package/backend/engine/adapters/codex/session-fork.ts +149 -0
  36. package/backend/engine/adapters/codex/stream.ts +310 -0
  37. package/backend/engine/adapters/codex/usage-rollout.ts +145 -0
  38. package/backend/engine/adapters/copilot/error-handler.ts +94 -0
  39. package/backend/engine/adapters/copilot/index.ts +1 -0
  40. package/backend/engine/adapters/copilot/message-converter.ts +973 -0
  41. package/backend/engine/adapters/copilot/models.ts +92 -0
  42. package/backend/engine/adapters/copilot/session-fork.ts +111 -0
  43. package/backend/engine/adapters/copilot/stream.ts +573 -0
  44. package/backend/engine/adapters/opencode/config.ts +56 -104
  45. package/backend/engine/adapters/opencode/error-handler.ts +71 -0
  46. package/backend/engine/adapters/opencode/models.ts +79 -0
  47. package/backend/engine/adapters/opencode/presets.ts +91 -0
  48. package/backend/engine/adapters/opencode/stream.ts +5 -115
  49. package/backend/engine/adapters/qwen/credential.ts +78 -0
  50. package/backend/engine/adapters/qwen/environment.ts +84 -0
  51. package/backend/engine/adapters/qwen/error-handler.ts +71 -0
  52. package/backend/engine/adapters/qwen/index.ts +1 -0
  53. package/backend/engine/adapters/qwen/message-converter.ts +1055 -0
  54. package/backend/engine/adapters/qwen/models.ts +81 -0
  55. package/backend/engine/adapters/qwen/presets.ts +51 -0
  56. package/backend/engine/adapters/qwen/session-fork.ts +97 -0
  57. package/backend/engine/adapters/qwen/stream.ts +380 -0
  58. package/backend/engine/index.ts +35 -0
  59. package/backend/engine/install-recipes.ts +80 -1
  60. package/backend/engine/install-runner.ts +4 -0
  61. package/backend/git/git-service.ts +12 -0
  62. package/backend/mcp/README.md +84 -14
  63. package/backend/mcp/config.ts +218 -17
  64. package/backend/mcp/index.ts +2 -0
  65. package/backend/mcp/project-context.ts +55 -0
  66. package/backend/mcp/servers/browser-automation/actions.ts +6 -1
  67. package/backend/mcp/servers/helper.ts +13 -0
  68. package/backend/middleware/error-handler.ts +16 -2
  69. package/backend/preview/browser/browser-preview-service.ts +4 -4
  70. package/backend/preview/browser/scripts/video-stream.ts +9 -3
  71. package/backend/ws/README.md +128 -3
  72. package/backend/ws/access.ts +56 -0
  73. package/backend/ws/chat/stream.ts +36 -12
  74. package/backend/ws/db-client/access.ts +19 -0
  75. package/backend/ws/db-client/connections.ts +194 -0
  76. package/backend/ws/db-client/index.ts +17 -0
  77. package/backend/ws/db-client/io.ts +456 -0
  78. package/backend/ws/db-client/query.ts +214 -0
  79. package/backend/ws/db-client/schema.ts +81 -0
  80. package/backend/ws/db-client/structure.ts +230 -0
  81. package/backend/ws/engine/access.ts +39 -0
  82. package/backend/ws/engine/claude/accounts.ts +15 -10
  83. package/backend/ws/engine/codex/accounts.ts +516 -0
  84. package/backend/ws/engine/codex/index.ts +14 -0
  85. package/backend/ws/engine/codex/status.ts +87 -0
  86. package/backend/ws/engine/copilot/accounts.ts +122 -0
  87. package/backend/ws/engine/copilot/index.ts +13 -0
  88. package/backend/ws/engine/copilot/status.ts +54 -0
  89. package/backend/ws/engine/index.ts +7 -1
  90. package/backend/ws/engine/opencode/providers.ts +1 -1
  91. package/backend/ws/engine/qwen/accounts.ts +137 -0
  92. package/backend/ws/engine/qwen/index.ts +15 -0
  93. package/backend/ws/engine/qwen/presets.ts +40 -0
  94. package/backend/ws/engine/qwen/status.ts +54 -0
  95. package/backend/ws/files/index.ts +5 -1
  96. package/backend/ws/files/read.ts +34 -0
  97. package/backend/ws/files/state.ts +42 -0
  98. package/backend/ws/files/watch.ts +7 -1
  99. package/backend/ws/git/branch.ts +13 -19
  100. package/backend/ws/git/commit-message.ts +3 -4
  101. package/backend/ws/git/commit.ts +5 -7
  102. package/backend/ws/git/conflict.ts +7 -10
  103. package/backend/ws/git/diff.ts +7 -10
  104. package/backend/ws/git/log.ts +3 -4
  105. package/backend/ws/git/remote.ts +30 -43
  106. package/backend/ws/git/staging.ts +13 -19
  107. package/backend/ws/git/status.ts +7 -11
  108. package/backend/ws/index.ts +5 -1
  109. package/backend/ws/messages/crud.ts +7 -13
  110. package/backend/ws/preview/access.ts +45 -0
  111. package/backend/ws/preview/browser/cleanup.ts +3 -11
  112. package/backend/ws/preview/browser/console.ts +5 -40
  113. package/backend/ws/preview/browser/interact.ts +2 -6
  114. package/backend/ws/preview/browser/native-ui.ts +5 -18
  115. package/backend/ws/preview/browser/stats.ts +2 -12
  116. package/backend/ws/preview/browser/tab-info.ts +4 -20
  117. package/backend/ws/preview/browser/tab.ts +4 -27
  118. package/backend/ws/preview/browser/webcodecs.ts +6 -47
  119. package/backend/ws/projects/crud.ts +4 -10
  120. package/backend/ws/projects/status.ts +2 -0
  121. package/backend/ws/sessions/crud.ts +22 -33
  122. package/backend/ws/settings/crud.ts +13 -13
  123. package/backend/ws/snapshot/restore.ts +5 -2
  124. package/backend/ws/snapshot/timeline.ts +3 -1
  125. package/backend/ws/system-tools/access.ts +28 -0
  126. package/backend/ws/system-tools/install.ts +13 -4
  127. package/backend/ws/system-tools/status.ts +5 -2
  128. package/backend/ws/terminal/access.ts +56 -0
  129. package/backend/ws/terminal/persistence.ts +12 -6
  130. package/backend/ws/terminal/session.ts +35 -14
  131. package/backend/ws/terminal/stream.ts +4 -1
  132. package/dist/assets/{index-C86jwBxR.js → index-D8V2wj9B.js} +2 -2
  133. package/dist/assets/{main-DhLx8wD1.js → main-C9nYVmnw.js} +222 -195
  134. package/dist/assets/main-hspyFOoK.css +1 -0
  135. package/dist/index.html +2 -2
  136. package/package.json +7 -1
  137. package/shared/constants/engines.ts +45 -140
  138. package/shared/types/database/schema.ts +49 -1
  139. package/shared/types/db-client/connection.ts +60 -0
  140. package/shared/types/db-client/index.ts +6 -0
  141. package/shared/types/db-client/query.ts +77 -0
  142. package/shared/types/unified/common.ts +1 -1
  143. package/shared/types/unified/engine.ts +29 -1
  144. package/shared/types/unified/index.ts +2 -0
  145. package/shared/types/unified/message.ts +1 -1
  146. package/shared/types/unified/tool.ts +17 -2
  147. package/shared/utils/logger.ts +1 -0
  148. package/shared/utils/ws-server.ts +14 -0
  149. package/dist/assets/main-jTnmAoXS.css +0 -1
@@ -0,0 +1,343 @@
1
+ import { getDatabase } from '../index';
2
+ import type { DBDbClientConnectionRow } from '$shared/types/database/schema';
3
+ import type {
4
+ DbClientConnection,
5
+ DbClientConnectionInput,
6
+ DbClientSshConfig,
7
+ DbDriver,
8
+ DbSshAuthMethod,
9
+ DbSslMode
10
+ } from '$shared/types/db-client';
11
+
12
+ const DEFAULT_SSH: DbClientSshConfig = {
13
+ enabled: false,
14
+ host: '',
15
+ port: 22,
16
+ username: '',
17
+ authMethod: 'password',
18
+ password: '',
19
+ privateKey: '',
20
+ passphrase: ''
21
+ };
22
+
23
+ function rowToConnection(row: DBDbClientConnectionRow): DbClientConnection {
24
+ let options: Record<string, unknown> = {};
25
+ if (row.options_json) {
26
+ try {
27
+ const parsed = JSON.parse(row.options_json);
28
+ if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
29
+ options = parsed as Record<string, unknown>;
30
+ }
31
+ } catch {
32
+ options = {};
33
+ }
34
+ }
35
+
36
+ const ssh: DbClientSshConfig = {
37
+ enabled: row.ssh_enabled === 1,
38
+ host: row.ssh_host ?? '',
39
+ port: row.ssh_port ?? 22,
40
+ username: row.ssh_username ?? '',
41
+ authMethod: (row.ssh_auth_method ?? 'password') as DbSshAuthMethod,
42
+ password: row.ssh_password ?? '',
43
+ privateKey: row.ssh_private_key ?? '',
44
+ passphrase: row.ssh_passphrase ?? ''
45
+ };
46
+
47
+ return {
48
+ id: row.id,
49
+ name: row.name,
50
+ driver: row.driver as DbDriver,
51
+ host: row.host,
52
+ port: row.port,
53
+ username: row.username,
54
+ password: row.password,
55
+ database: row.database,
56
+ sslMode: (row.ssl_mode ?? 'disable') as DbSslMode,
57
+ sslCa: row.ssl_ca,
58
+ ssh,
59
+ options,
60
+ color: row.color,
61
+ createdAt: row.created_at,
62
+ updatedAt: row.updated_at,
63
+ lastUsedAt: row.last_used_at
64
+ };
65
+ }
66
+
67
+ function redactConnectionSecrets(connection: DbClientConnection): DbClientConnection {
68
+ return {
69
+ ...connection,
70
+ password: null,
71
+ ssh: {
72
+ ...connection.ssh,
73
+ password: undefined,
74
+ privateKey: undefined,
75
+ passphrase: undefined
76
+ }
77
+ };
78
+ }
79
+
80
+ function hasConnectionAccess(
81
+ row: DBDbClientConnectionRow,
82
+ userId: string,
83
+ isAdmin: boolean
84
+ ): boolean {
85
+ if (isAdmin) return true;
86
+ return row.owner_user_id === userId;
87
+ }
88
+
89
+ function preserveExistingSecret(
90
+ patchValue: string | undefined,
91
+ existingValue: string | null
92
+ ): string | null {
93
+ if (patchValue === undefined || patchValue === '') return existingValue;
94
+ return patchValue;
95
+ }
96
+
97
+ interface InsertParams {
98
+ name: string;
99
+ driver: DbDriver;
100
+ host: string | null;
101
+ port: number | null;
102
+ username: string | null;
103
+ password: string | null;
104
+ database: string | null;
105
+ sslMode: DbSslMode;
106
+ sslCa: string | null;
107
+ ssh: DbClientSshConfig;
108
+ options: Record<string, unknown>;
109
+ color: string | null;
110
+ ownerUserId: string | null;
111
+ }
112
+
113
+ function normalizeInput(input: DbClientConnectionInput, ownerUserId: string | null): InsertParams {
114
+ const ssh: DbClientSshConfig = {
115
+ ...DEFAULT_SSH,
116
+ ...(input.ssh ?? {})
117
+ };
118
+ return {
119
+ name: input.name,
120
+ driver: input.driver,
121
+ host: input.host ?? null,
122
+ port: input.port ?? null,
123
+ username: input.username ?? null,
124
+ password: input.password ?? null,
125
+ database: input.database ?? null,
126
+ sslMode: input.sslMode ?? 'disable',
127
+ sslCa: input.sslCa ?? null,
128
+ ssh,
129
+ options: input.options ?? {},
130
+ color: input.color ?? null,
131
+ ownerUserId
132
+ };
133
+ }
134
+
135
+ function getRowById(id: string): DBDbClientConnectionRow | null {
136
+ const db = getDatabase();
137
+ return db.prepare(`
138
+ SELECT * FROM db_client_connections WHERE id = ?
139
+ `).get(id) as DBDbClientConnectionRow | null;
140
+ }
141
+
142
+ function insertConnection(input: DbClientConnectionInput, ownerUserId: string | null): DbClientConnection {
143
+ const db = getDatabase();
144
+ const id = crypto.randomUUID();
145
+ const now = new Date().toISOString();
146
+ const params = normalizeInput(input, ownerUserId);
147
+
148
+ db.prepare(`
149
+ INSERT INTO db_client_connections (
150
+ id, name, driver,
151
+ host, port, username, password, database,
152
+ ssl_mode, ssl_ca,
153
+ ssh_enabled, ssh_host, ssh_port, ssh_username, ssh_auth_method,
154
+ ssh_password, ssh_private_key, ssh_passphrase,
155
+ options_json, color,
156
+ owner_user_id,
157
+ created_at, updated_at, last_used_at
158
+ ) VALUES (
159
+ ?, ?, ?,
160
+ ?, ?, ?, ?, ?,
161
+ ?, ?,
162
+ ?, ?, ?, ?, ?,
163
+ ?, ?, ?,
164
+ ?, ?,
165
+ ?,
166
+ ?, ?, ?
167
+ )
168
+ `).run(
169
+ id, params.name, params.driver,
170
+ params.host, params.port, params.username, params.password, params.database,
171
+ params.sslMode, params.sslCa,
172
+ params.ssh.enabled ? 1 : 0,
173
+ params.ssh.host || null,
174
+ params.ssh.port,
175
+ params.ssh.username || null,
176
+ params.ssh.authMethod,
177
+ params.ssh.password || null,
178
+ params.ssh.privateKey || null,
179
+ params.ssh.passphrase || null,
180
+ JSON.stringify(params.options),
181
+ params.color,
182
+ params.ownerUserId,
183
+ now, now, null
184
+ );
185
+
186
+ const row = getRowById(id);
187
+ if (!row) {
188
+ throw new Error('db-client connection not found');
189
+ }
190
+ return rowToConnection(row);
191
+ }
192
+
193
+ export const dbClientConnectionQueries = {
194
+ listForUser(userId: string, isAdmin: boolean): DbClientConnection[] {
195
+ const db = getDatabase();
196
+ const rows = (isAdmin
197
+ ? db.prepare(`
198
+ SELECT * FROM db_client_connections
199
+ ORDER BY (last_used_at IS NULL), last_used_at DESC, created_at DESC
200
+ `).all()
201
+ : db.prepare(`
202
+ SELECT * FROM db_client_connections
203
+ WHERE owner_user_id = ?
204
+ ORDER BY (last_used_at IS NULL), last_used_at DESC, created_at DESC
205
+ `).all(userId)) as DBDbClientConnectionRow[];
206
+
207
+ return rows.map((row) => redactConnectionSecrets(rowToConnection(row)));
208
+ },
209
+
210
+ getForUser(id: string, userId: string, isAdmin: boolean): DbClientConnection | null {
211
+ const row = getRowById(id);
212
+ if (!row || !hasConnectionAccess(row, userId, isAdmin)) {
213
+ return null;
214
+ }
215
+ return redactConnectionSecrets(rowToConnection(row));
216
+ },
217
+
218
+ ensureAccess(id: string, userId: string, isAdmin: boolean): DbClientConnection {
219
+ const row = getRowById(id);
220
+ if (!row || !hasConnectionAccess(row, userId, isAdmin)) {
221
+ throw new Error('db-client connection not found');
222
+ }
223
+ return rowToConnection(row);
224
+ },
225
+
226
+ list(): DbClientConnection[] {
227
+ const db = getDatabase();
228
+ const rows = db.prepare(`
229
+ SELECT * FROM db_client_connections
230
+ ORDER BY (last_used_at IS NULL), last_used_at DESC, created_at DESC
231
+ `).all() as DBDbClientConnectionRow[];
232
+ return rows.map(rowToConnection);
233
+ },
234
+
235
+ get(id: string): DbClientConnection | null {
236
+ const row = getRowById(id);
237
+ return row ? rowToConnection(row) : null;
238
+ },
239
+
240
+ create(input: DbClientConnectionInput): DbClientConnection {
241
+ return insertConnection(input, null);
242
+ },
243
+
244
+ createForUser(input: DbClientConnectionInput, ownerUserId: string): DbClientConnection {
245
+ const connection = insertConnection(input, ownerUserId);
246
+ return redactConnectionSecrets(connection);
247
+ },
248
+
249
+ update(id: string, patch: Partial<DbClientConnectionInput>): DbClientConnection {
250
+ const db = getDatabase();
251
+ const existing = getRowById(id);
252
+ if (!existing) {
253
+ throw new Error('db-client connection not found');
254
+ }
255
+
256
+ const sets: string[] = [];
257
+ const values: unknown[] = [];
258
+ const push = (col: string, val: unknown): void => {
259
+ sets.push(`${col} = ?`);
260
+ values.push(val);
261
+ };
262
+
263
+ if (patch.name !== undefined) push('name', patch.name);
264
+ if (patch.driver !== undefined) push('driver', patch.driver);
265
+ if (patch.host !== undefined) push('host', patch.host || null);
266
+ if (patch.port !== undefined) push('port', patch.port ?? null);
267
+ if (patch.username !== undefined) push('username', patch.username || null);
268
+ if (patch.password !== undefined && patch.password !== '') push('password', patch.password);
269
+ if (patch.database !== undefined) push('database', patch.database || null);
270
+ if (patch.sslMode !== undefined) push('ssl_mode', patch.sslMode);
271
+ if (patch.sslCa !== undefined) push('ssl_ca', patch.sslCa || null);
272
+ if (patch.color !== undefined) push('color', patch.color || null);
273
+ if (patch.options !== undefined) push('options_json', JSON.stringify(patch.options ?? {}));
274
+
275
+ if (patch.ssh !== undefined) {
276
+ const merged: DbClientSshConfig = {
277
+ enabled: existing.ssh_enabled === 1,
278
+ host: existing.ssh_host ?? '',
279
+ port: existing.ssh_port ?? 22,
280
+ username: existing.ssh_username ?? '',
281
+ authMethod: (existing.ssh_auth_method ?? 'password') as DbSshAuthMethod,
282
+ password: existing.ssh_password ?? '',
283
+ privateKey: existing.ssh_private_key ?? '',
284
+ passphrase: existing.ssh_passphrase ?? '',
285
+ ...patch.ssh
286
+ };
287
+ const sshPassword = preserveExistingSecret(patch.ssh.password, existing.ssh_password);
288
+ const sshPrivateKey = preserveExistingSecret(patch.ssh.privateKey, existing.ssh_private_key);
289
+ const sshPassphrase = preserveExistingSecret(patch.ssh.passphrase, existing.ssh_passphrase);
290
+
291
+ push('ssh_enabled', merged.enabled ? 1 : 0);
292
+ push('ssh_host', merged.host || null);
293
+ push('ssh_port', merged.port);
294
+ push('ssh_username', merged.username || null);
295
+ push('ssh_auth_method', merged.authMethod);
296
+ push('ssh_password', sshPassword);
297
+ push('ssh_private_key', sshPrivateKey);
298
+ push('ssh_passphrase', sshPassphrase);
299
+ }
300
+
301
+ push('updated_at', new Date().toISOString());
302
+
303
+ if (sets.length === 0) {
304
+ return rowToConnection(existing);
305
+ }
306
+
307
+ values.push(id);
308
+ db.prepare(`UPDATE db_client_connections SET ${sets.join(', ')} WHERE id = ?`).run(...values);
309
+
310
+ const row = getRowById(id);
311
+ if (!row) {
312
+ throw new Error('db-client connection not found');
313
+ }
314
+ return rowToConnection(row);
315
+ },
316
+
317
+ updateForUser(
318
+ id: string,
319
+ patch: Partial<DbClientConnectionInput>,
320
+ userId: string,
321
+ isAdmin: boolean
322
+ ): DbClientConnection {
323
+ this.ensureAccess(id, userId, isAdmin);
324
+ const updated = this.update(id, patch);
325
+ return redactConnectionSecrets(updated);
326
+ },
327
+
328
+ delete(id: string): void {
329
+ const db = getDatabase();
330
+ db.prepare('DELETE FROM db_client_connections WHERE id = ?').run(id);
331
+ },
332
+
333
+ deleteForUser(id: string, userId: string, isAdmin: boolean): void {
334
+ this.ensureAccess(id, userId, isAdmin);
335
+ this.delete(id);
336
+ },
337
+
338
+ markUsed(id: string): void {
339
+ const db = getDatabase();
340
+ const now = new Date().toISOString();
341
+ db.prepare('UPDATE db_client_connections SET last_used_at = ? WHERE id = ?').run(now, id);
342
+ }
343
+ };
@@ -0,0 +1,113 @@
1
+ import { getDatabase } from '../index';
2
+ import type { DBDbClientQueryHistoryRow } from '$shared/types/database/schema';
3
+ import type { DbClientQueryHistoryEntry } from '$shared/types/db-client';
4
+
5
+ function rowToEntry(row: DBDbClientQueryHistoryRow): DbClientQueryHistoryEntry {
6
+ return {
7
+ id: row.id,
8
+ connectionId: row.connection_id,
9
+ userId: row.user_id,
10
+ query: row.query,
11
+ durationMs: row.duration_ms,
12
+ rowCount: row.row_count,
13
+ status: row.status,
14
+ error: row.error,
15
+ executedAt: row.executed_at
16
+ };
17
+ }
18
+
19
+ interface InsertHistoryInput {
20
+ connectionId: string;
21
+ userId: string | null;
22
+ query: string;
23
+ durationMs: number | null;
24
+ rowCount: number | null;
25
+ status: 'success' | 'error';
26
+ error: string | null;
27
+ }
28
+
29
+ export const dbClientQueryHistoryQueries = {
30
+ insert(input: InsertHistoryInput): DbClientQueryHistoryEntry {
31
+ const db = getDatabase();
32
+ const id = crypto.randomUUID();
33
+ const now = new Date().toISOString();
34
+
35
+ db.prepare(`
36
+ INSERT INTO db_client_query_history (
37
+ id, connection_id, user_id, query,
38
+ duration_ms, row_count, status, error, executed_at
39
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
40
+ `).run(
41
+ id, input.connectionId, input.userId, input.query,
42
+ input.durationMs, input.rowCount, input.status, input.error, now
43
+ );
44
+
45
+ const row = db.prepare(
46
+ 'SELECT * FROM db_client_query_history WHERE id = ?'
47
+ ).get(id) as DBDbClientQueryHistoryRow;
48
+ return rowToEntry(row);
49
+ },
50
+
51
+ list(opts: {
52
+ connectionId: string;
53
+ limit?: number;
54
+ offset?: number;
55
+ search?: string;
56
+ }): { items: DbClientQueryHistoryEntry[]; total: number } {
57
+ const db = getDatabase();
58
+ const limit = opts.limit ?? 50;
59
+ const offset = opts.offset ?? 0;
60
+ const search = opts.search?.trim();
61
+
62
+ let where = 'connection_id = ?';
63
+ const params: unknown[] = [opts.connectionId];
64
+ if (search) {
65
+ where += ' AND query LIKE ?';
66
+ params.push(`%${search}%`);
67
+ }
68
+
69
+ const totalRow = db.prepare(
70
+ `SELECT COUNT(*) as count FROM db_client_query_history WHERE ${where}`
71
+ ).get(...params) as { count: number };
72
+
73
+ const rows = db.prepare(
74
+ `SELECT * FROM db_client_query_history
75
+ WHERE ${where}
76
+ ORDER BY executed_at DESC
77
+ LIMIT ? OFFSET ?`
78
+ ).all(...params, limit, offset) as DBDbClientQueryHistoryRow[];
79
+
80
+ return {
81
+ items: rows.map(rowToEntry),
82
+ total: totalRow.count
83
+ };
84
+ },
85
+
86
+ deleteByConnection(connectionId: string): number {
87
+ const db = getDatabase();
88
+ const result = db.prepare(
89
+ 'DELETE FROM db_client_query_history WHERE connection_id = ?'
90
+ ).run(connectionId) as { changes: number };
91
+ return result.changes;
92
+ },
93
+
94
+ deleteOne(id: string): void {
95
+ const db = getDatabase();
96
+ db.prepare('DELETE FROM db_client_query_history WHERE id = ?').run(id);
97
+ },
98
+
99
+ prune(connectionId: string, keep: number): number {
100
+ const db = getDatabase();
101
+ const result = db.prepare(`
102
+ DELETE FROM db_client_query_history
103
+ WHERE connection_id = ?
104
+ AND id NOT IN (
105
+ SELECT id FROM db_client_query_history
106
+ WHERE connection_id = ?
107
+ ORDER BY executed_at DESC
108
+ LIMIT ?
109
+ )
110
+ `).run(connectionId, connectionId, keep) as { changes: number };
111
+ return result.changes;
112
+ }
113
+ };
@@ -220,6 +220,18 @@ export const engineQueries = {
220
220
  db.prepare(`UPDATE engine_accounts SET name = ? WHERE id = ?`).run(name, accountId);
221
221
  },
222
222
 
223
+ /**
224
+ * Replace the stored credential for an existing account. Used by adapters
225
+ * whose CLI mutates an external dotfile in place (e.g. Codex's
226
+ * ~/.codex/auth.json token refresh) — the adapter snapshots the file back
227
+ * into `credential` after each stream so refreshed tokens survive across
228
+ * account switches.
229
+ */
230
+ updateAccountCredential(accountId: number, credential: string): void {
231
+ const db = getDatabase();
232
+ db.prepare(`UPDATE engine_accounts SET credential = ? WHERE id = ?`).run(credential, accountId);
233
+ },
234
+
223
235
  // ------------------------------------------------------------------
224
236
  // Composite
225
237
  // ------------------------------------------------------------------
@@ -7,4 +7,6 @@ export { dbUtils } from './utils-queries';
7
7
  export { snapshotQueries } from './snapshot-queries';
8
8
  export { checkpointQueries } from './checkpoint-queries';
9
9
  export { engineQueries } from './engine-queries';
10
- export { authQueries } from './auth-queries';
10
+ export { authQueries } from './auth-queries';
11
+ export { dbClientConnectionQueries } from './db-client-connection-queries';
12
+ export { dbClientQueryHistoryQueries } from './db-client-query-history-queries';
@@ -34,6 +34,16 @@ export const projectQueries = {
34
34
  `).get(path) as Project | null;
35
35
  },
36
36
 
37
+ userHasProject(userId: string, projectId: string): boolean {
38
+ const db = getDatabase();
39
+ const result = db.prepare(`
40
+ SELECT 1 as ok FROM user_projects
41
+ WHERE user_id = ? AND project_id = ?
42
+ LIMIT 1
43
+ `).get(userId, projectId) as { ok: number } | undefined;
44
+ return Boolean(result);
45
+ },
46
+
37
47
  create(project: Omit<Project, 'id'>): Project {
38
48
  const db = getDatabase();
39
49
  const id = crypto.randomUUID();
@@ -110,5 +120,23 @@ export const projectQueries = {
110
120
  SET current_session_id = ?
111
121
  WHERE user_id = ? AND project_id = ?
112
122
  `).run(sessionId, userId, projectId);
123
+ },
124
+
125
+ getFilesPanelState(userId: string, projectId: string): string | null {
126
+ const db = getDatabase();
127
+ const result = db.prepare(`
128
+ SELECT files_panel_state FROM user_projects
129
+ WHERE user_id = ? AND project_id = ?
130
+ `).get(userId, projectId) as { files_panel_state: string | null } | null;
131
+ return result?.files_panel_state || null;
132
+ },
133
+
134
+ setFilesPanelState(userId: string, projectId: string, state: string | null): void {
135
+ const db = getDatabase();
136
+ db.prepare(`
137
+ UPDATE user_projects
138
+ SET files_panel_state = ?
139
+ WHERE user_id = ? AND project_id = ?
140
+ `).run(state, userId, projectId);
113
141
  }
114
142
  };