@myrialabs/clopen 0.2.23 → 0.3.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.
- package/README.md +1 -1
- package/backend/chat/helpers.ts +2 -20
- package/backend/chat/stream-manager.ts +556 -547
- package/backend/database/migrations/027_create_opencode_providers_table.ts +27 -0
- package/backend/database/migrations/028_create_opencode_accounts_table.ts +25 -0
- package/backend/database/migrations/029_migrate_messages_to_unified.ts +1108 -0
- package/backend/database/migrations/index.ts +21 -0
- package/backend/database/queries/engine-queries.ts +195 -36
- package/backend/database/queries/message-queries.ts +84 -139
- package/backend/database/queries/session-queries.ts +167 -21
- package/backend/database/queries/snapshot-queries.ts +5 -4
- package/backend/database/queries/utils-queries.ts +2 -2
- package/backend/engine/adapters/claude/environment.ts +4 -5
- package/backend/engine/adapters/claude/message-converter.ts +501 -0
- package/backend/engine/adapters/claude/stream.ts +52 -47
- package/backend/engine/adapters/opencode/config.ts +156 -0
- package/backend/engine/adapters/opencode/message-converter.ts +266 -326
- package/backend/engine/adapters/opencode/server.ts +54 -12
- package/backend/engine/adapters/opencode/stream.ts +84 -101
- package/backend/engine/types.ts +17 -11
- package/backend/project/status-manager.ts +10 -13
- package/backend/snapshot/helpers.ts +46 -90
- package/backend/snapshot/snapshot-service.ts +1 -1
- package/backend/utils/paths.ts +22 -1
- package/backend/ws/chat/index.ts +1 -11
- package/backend/ws/chat/stream.ts +101 -61
- package/backend/ws/engine/claude/accounts.ts +27 -7
- package/backend/ws/engine/claude/status.ts +3 -2
- package/backend/ws/engine/opencode/index.ts +3 -1
- package/backend/ws/engine/opencode/providers.ts +300 -0
- package/backend/ws/git/commit-message.ts +6 -8
- package/backend/ws/messages/crud.ts +4 -59
- package/backend/ws/sessions/crud.ts +78 -130
- package/backend/ws/sessions/index.ts +1 -9
- package/backend/ws/settings/crud.ts +14 -11
- package/backend/ws/snapshot/restore.ts +23 -26
- package/backend/ws/snapshot/timeline.ts +5 -5
- package/dist/assets/{index-BFFIy7ZX.js → index-B95VOHJY.js} +2 -2
- package/dist/assets/main-D1QNkcRL.js +257 -0
- package/dist/assets/main-rtLiuU_x.css +1 -0
- package/dist/index.html +2 -2
- package/package.json +4 -4
- package/shared/constants/engines.ts +141 -56
- package/shared/types/database/schema.ts +39 -40
- package/shared/types/stores/settings.ts +8 -4
- package/shared/types/unified/common.ts +21 -0
- package/shared/types/unified/display.ts +25 -0
- package/shared/types/unified/engine.ts +61 -0
- package/shared/types/unified/index.ts +108 -0
- package/shared/types/unified/message.ts +151 -0
- package/shared/types/unified/stream.ts +157 -0
- package/shared/types/unified/tool.ts +268 -0
- package/shared/utils/message-formatter.ts +9 -73
- package/shared/utils/path.ts +3 -2
- package/tsconfig.json +1 -1
- package/backend/engine/adapters/claude/path-utils.ts +0 -38
- package/backend/ws/chat/background.ts +0 -201
- package/dist/assets/main-CtNvgvwE.js +0 -249
- package/dist/assets/main-DmshCkFM.css +0 -1
- package/shared/types/engine/index.ts +0 -45
- package/shared/types/messaging/index.ts +0 -242
- package/shared/types/messaging/tool.ts +0 -374
|
@@ -25,6 +25,9 @@ import * as migration023 from './023_create_user_unread_sessions_table';
|
|
|
25
25
|
import * as migration024 from './024_create_users_table';
|
|
26
26
|
import * as migration025 from './025_create_auth_sessions_table';
|
|
27
27
|
import * as migration026 from './026_create_invite_tokens_table';
|
|
28
|
+
import * as migration027 from './027_create_opencode_providers_table';
|
|
29
|
+
import * as migration028 from './028_create_opencode_accounts_table';
|
|
30
|
+
import * as migration029 from './029_migrate_messages_to_unified';
|
|
28
31
|
|
|
29
32
|
// Export all migrations in order
|
|
30
33
|
export const migrations = [
|
|
@@ -183,6 +186,24 @@ export const migrations = [
|
|
|
183
186
|
description: migration026.description,
|
|
184
187
|
up: migration026.up,
|
|
185
188
|
down: migration026.down
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
id: '027',
|
|
192
|
+
description: migration027.description,
|
|
193
|
+
up: migration027.up,
|
|
194
|
+
down: migration027.down
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
id: '028',
|
|
198
|
+
description: migration028.description,
|
|
199
|
+
up: migration028.up,
|
|
200
|
+
down: migration028.down
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
id: '029',
|
|
204
|
+
description: migration029.description,
|
|
205
|
+
up: migration029.up,
|
|
206
|
+
down: migration029.down
|
|
186
207
|
}
|
|
187
208
|
];
|
|
188
209
|
|
|
@@ -1,75 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unified Engine Queries — providers + accounts for ALL engines.
|
|
3
|
+
*
|
|
4
|
+
* The previous split (claude_accounts + opencode_providers + opencode_accounts)
|
|
5
|
+
* is consolidated here into:
|
|
6
|
+
* engine_providers — one row per (engine_type, slug)
|
|
7
|
+
* engine_accounts — credentials under a provider
|
|
8
|
+
*
|
|
9
|
+
* Conventions:
|
|
10
|
+
* - `slug` : machine id (e.g. 'anthropic', 'openai'). Lowercase, stable.
|
|
11
|
+
* - `name` : display name (user-facing).
|
|
12
|
+
* - `provider_id`: FK to engine_providers.id (always numeric).
|
|
13
|
+
* - `credential` : generic secret (OAuth token for claude-code, API key for opencode).
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import type { EngineType } from '$shared/types/unified';
|
|
1
17
|
import { getDatabase } from '../index';
|
|
2
18
|
|
|
3
|
-
|
|
19
|
+
// ============================================================================
|
|
20
|
+
// Types
|
|
21
|
+
// ============================================================================
|
|
22
|
+
|
|
23
|
+
export interface EngineProvider {
|
|
24
|
+
id: number;
|
|
25
|
+
engine_type: EngineType;
|
|
26
|
+
slug: string;
|
|
27
|
+
name: string;
|
|
28
|
+
npm: string | null;
|
|
29
|
+
api_url: string | null;
|
|
30
|
+
options: string;
|
|
31
|
+
is_enabled: number;
|
|
32
|
+
created_at: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface EngineAccount {
|
|
4
36
|
id: number;
|
|
37
|
+
provider_id: number;
|
|
5
38
|
name: string;
|
|
6
|
-
|
|
39
|
+
credential: string;
|
|
7
40
|
is_active: number;
|
|
8
41
|
created_at: string;
|
|
9
42
|
}
|
|
10
43
|
|
|
44
|
+
/** Provider with its accounts joined. */
|
|
45
|
+
export interface EngineProviderWithAccounts extends EngineProvider {
|
|
46
|
+
accounts: EngineAccount[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// ============================================================================
|
|
50
|
+
// Queries
|
|
51
|
+
// ============================================================================
|
|
52
|
+
|
|
11
53
|
export const engineQueries = {
|
|
12
|
-
|
|
54
|
+
// ------------------------------------------------------------------
|
|
55
|
+
// Providers
|
|
56
|
+
// ------------------------------------------------------------------
|
|
57
|
+
|
|
58
|
+
getProviders(engineType?: EngineType): EngineProvider[] {
|
|
13
59
|
const db = getDatabase();
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
60
|
+
if (engineType) {
|
|
61
|
+
return db.prepare(
|
|
62
|
+
`SELECT * FROM engine_providers WHERE engine_type = ? ORDER BY created_at ASC`
|
|
63
|
+
).all(engineType) as EngineProvider[];
|
|
64
|
+
}
|
|
65
|
+
return db.prepare(
|
|
66
|
+
`SELECT * FROM engine_providers ORDER BY engine_type ASC, created_at ASC`
|
|
67
|
+
).all() as EngineProvider[];
|
|
18
68
|
},
|
|
19
69
|
|
|
20
|
-
|
|
70
|
+
getEnabledProviders(engineType?: EngineType): EngineProvider[] {
|
|
21
71
|
const db = getDatabase();
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
72
|
+
if (engineType) {
|
|
73
|
+
return db.prepare(
|
|
74
|
+
`SELECT * FROM engine_providers WHERE engine_type = ? AND is_enabled = 1 ORDER BY created_at ASC`
|
|
75
|
+
).all(engineType) as EngineProvider[];
|
|
76
|
+
}
|
|
77
|
+
return db.prepare(
|
|
78
|
+
`SELECT * FROM engine_providers WHERE is_enabled = 1 ORDER BY engine_type ASC, created_at ASC`
|
|
79
|
+
).all() as EngineProvider[];
|
|
25
80
|
},
|
|
26
81
|
|
|
27
|
-
|
|
82
|
+
getProvider(id: number): EngineProvider | null {
|
|
28
83
|
const db = getDatabase();
|
|
84
|
+
return db.prepare(`SELECT * FROM engine_providers WHERE id = ?`).get(id) as EngineProvider | null;
|
|
85
|
+
},
|
|
29
86
|
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
|
|
87
|
+
getProviderBySlug(engineType: EngineType, slug: string): EngineProvider | null {
|
|
88
|
+
const db = getDatabase();
|
|
89
|
+
return db.prepare(
|
|
90
|
+
`SELECT * FROM engine_providers WHERE engine_type = ? AND slug = ?`
|
|
91
|
+
).get(engineType, slug) as EngineProvider | null;
|
|
92
|
+
},
|
|
33
93
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
94
|
+
createProvider(data: {
|
|
95
|
+
engineType: EngineType;
|
|
96
|
+
slug: string;
|
|
97
|
+
name: string;
|
|
98
|
+
npm?: string | null;
|
|
99
|
+
apiUrl?: string | null;
|
|
100
|
+
options?: string;
|
|
101
|
+
}): EngineProvider {
|
|
102
|
+
const db = getDatabase();
|
|
103
|
+
const result = db.prepare(`
|
|
104
|
+
INSERT INTO engine_providers (engine_type, slug, name, npm, api_url, options)
|
|
105
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
106
|
+
`).run(
|
|
107
|
+
data.engineType,
|
|
108
|
+
data.slug,
|
|
109
|
+
data.name,
|
|
110
|
+
data.npm ?? null,
|
|
111
|
+
data.apiUrl ?? null,
|
|
112
|
+
data.options ?? '{}',
|
|
113
|
+
) as { lastInsertRowid: number | bigint };
|
|
114
|
+
const id = Number(result.lastInsertRowid);
|
|
115
|
+
return db.prepare(`SELECT * FROM engine_providers WHERE id = ?`).get(id) as EngineProvider;
|
|
116
|
+
},
|
|
38
117
|
|
|
39
|
-
|
|
40
|
-
const
|
|
118
|
+
updateProviderOptions(id: number, options: string): void {
|
|
119
|
+
const db = getDatabase();
|
|
120
|
+
db.prepare(`UPDATE engine_providers SET options = ? WHERE id = ?`).run(options, id);
|
|
121
|
+
},
|
|
41
122
|
|
|
42
|
-
|
|
123
|
+
toggleProvider(id: number, enabled: boolean): void {
|
|
124
|
+
const db = getDatabase();
|
|
125
|
+
db.prepare(`UPDATE engine_providers SET is_enabled = ? WHERE id = ?`).run(enabled ? 1 : 0, id);
|
|
43
126
|
},
|
|
44
127
|
|
|
45
|
-
|
|
128
|
+
deleteProvider(id: number): void {
|
|
46
129
|
const db = getDatabase();
|
|
47
|
-
|
|
48
|
-
db.prepare(
|
|
130
|
+
// Accounts cascade via FK.
|
|
131
|
+
db.prepare(`DELETE FROM engine_providers WHERE id = ?`).run(id);
|
|
49
132
|
},
|
|
50
133
|
|
|
51
|
-
|
|
134
|
+
// ------------------------------------------------------------------
|
|
135
|
+
// Accounts
|
|
136
|
+
// ------------------------------------------------------------------
|
|
137
|
+
|
|
138
|
+
getAccount(id: number): EngineAccount | null {
|
|
52
139
|
const db = getDatabase();
|
|
140
|
+
return db.prepare(`SELECT * FROM engine_accounts WHERE id = ?`).get(id) as EngineAccount | null;
|
|
141
|
+
},
|
|
53
142
|
|
|
54
|
-
|
|
55
|
-
const
|
|
143
|
+
getAccountsByProvider(providerId: number): EngineAccount[] {
|
|
144
|
+
const db = getDatabase();
|
|
145
|
+
return db.prepare(
|
|
146
|
+
`SELECT * FROM engine_accounts WHERE provider_id = ? ORDER BY created_at ASC`
|
|
147
|
+
).all(providerId) as EngineAccount[];
|
|
148
|
+
},
|
|
149
|
+
|
|
150
|
+
getActiveAccount(providerId: number): EngineAccount | null {
|
|
151
|
+
const db = getDatabase();
|
|
152
|
+
return db.prepare(
|
|
153
|
+
`SELECT * FROM engine_accounts WHERE provider_id = ? AND is_active = 1`
|
|
154
|
+
).get(providerId) as EngineAccount | null;
|
|
155
|
+
},
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Get the active account for the first enabled provider of the given engine.
|
|
159
|
+
* For claude-code this is effectively the active Anthropic account.
|
|
160
|
+
*/
|
|
161
|
+
getActiveAccountForEngine(engineType: EngineType): EngineAccount | null {
|
|
162
|
+
const db = getDatabase();
|
|
163
|
+
return db.prepare(`
|
|
164
|
+
SELECT a.*
|
|
165
|
+
FROM engine_accounts a
|
|
166
|
+
JOIN engine_providers p ON p.id = a.provider_id
|
|
167
|
+
WHERE p.engine_type = ? AND p.is_enabled = 1 AND a.is_active = 1
|
|
168
|
+
ORDER BY p.created_at ASC, a.created_at ASC
|
|
169
|
+
LIMIT 1
|
|
170
|
+
`).get(engineType) as EngineAccount | null;
|
|
171
|
+
},
|
|
172
|
+
|
|
173
|
+
createAccount(providerId: number, name: string, credential: string): EngineAccount {
|
|
174
|
+
const db = getDatabase();
|
|
175
|
+
|
|
176
|
+
// If it's the first account for this provider → mark active.
|
|
177
|
+
const count = (db.prepare(
|
|
178
|
+
`SELECT COUNT(*) as count FROM engine_accounts WHERE provider_id = ?`
|
|
179
|
+
).get(providerId) as { count: number }).count;
|
|
180
|
+
const isActive = count === 0 ? 1 : 0;
|
|
181
|
+
|
|
182
|
+
const result = db.prepare(`
|
|
183
|
+
INSERT INTO engine_accounts (provider_id, name, credential, is_active)
|
|
184
|
+
VALUES (?, ?, ?, ?)
|
|
185
|
+
`).run(providerId, name, credential, isActive) as { lastInsertRowid: number | bigint };
|
|
186
|
+
|
|
187
|
+
const id = Number(result.lastInsertRowid);
|
|
188
|
+
return db.prepare(`SELECT * FROM engine_accounts WHERE id = ?`).get(id) as EngineAccount;
|
|
189
|
+
},
|
|
190
|
+
|
|
191
|
+
switchAccount(accountId: number): void {
|
|
192
|
+
const db = getDatabase();
|
|
193
|
+
const account = db.prepare(`SELECT * FROM engine_accounts WHERE id = ?`).get(accountId) as EngineAccount | null;
|
|
56
194
|
if (!account) return;
|
|
195
|
+
db.prepare(`UPDATE engine_accounts SET is_active = 0 WHERE provider_id = ?`).run(account.provider_id);
|
|
196
|
+
db.prepare(`UPDATE engine_accounts SET is_active = 1 WHERE id = ?`).run(accountId);
|
|
197
|
+
},
|
|
57
198
|
|
|
58
|
-
|
|
199
|
+
deleteAccount(accountId: number): void {
|
|
200
|
+
const db = getDatabase();
|
|
201
|
+
const account = db.prepare(`SELECT * FROM engine_accounts WHERE id = ?`).get(accountId) as EngineAccount | null;
|
|
202
|
+
if (!account) return;
|
|
59
203
|
|
|
60
|
-
|
|
204
|
+
const wasActive = account.is_active === 1;
|
|
205
|
+
db.prepare(`DELETE FROM engine_accounts WHERE id = ?`).run(accountId);
|
|
61
206
|
|
|
62
|
-
// If
|
|
207
|
+
// If deleted account was active, activate the first remaining account for its provider.
|
|
63
208
|
if (wasActive) {
|
|
64
|
-
const remaining = db.prepare(
|
|
209
|
+
const remaining = db.prepare(
|
|
210
|
+
`SELECT id FROM engine_accounts WHERE provider_id = ? ORDER BY created_at ASC LIMIT 1`
|
|
211
|
+
).get(account.provider_id) as { id: number } | null;
|
|
65
212
|
if (remaining) {
|
|
66
|
-
db.prepare(
|
|
213
|
+
db.prepare(`UPDATE engine_accounts SET is_active = 1 WHERE id = ?`).run(remaining.id);
|
|
67
214
|
}
|
|
68
215
|
}
|
|
69
216
|
},
|
|
70
217
|
|
|
71
|
-
|
|
218
|
+
renameAccount(accountId: number, name: string): void {
|
|
72
219
|
const db = getDatabase();
|
|
73
|
-
db.prepare(
|
|
74
|
-
}
|
|
220
|
+
db.prepare(`UPDATE engine_accounts SET name = ? WHERE id = ?`).run(name, accountId);
|
|
221
|
+
},
|
|
222
|
+
|
|
223
|
+
// ------------------------------------------------------------------
|
|
224
|
+
// Composite
|
|
225
|
+
// ------------------------------------------------------------------
|
|
226
|
+
|
|
227
|
+
getProvidersWithAccounts(engineType?: EngineType): EngineProviderWithAccounts[] {
|
|
228
|
+
const providers = this.getProviders(engineType);
|
|
229
|
+
return providers.map(p => ({
|
|
230
|
+
...p,
|
|
231
|
+
accounts: this.getAccountsByProvider(p.id),
|
|
232
|
+
}));
|
|
233
|
+
},
|
|
75
234
|
};
|
|
@@ -1,87 +1,31 @@
|
|
|
1
1
|
import { getDatabase } from '../index';
|
|
2
|
-
import type { DatabaseMessage
|
|
3
|
-
import type {
|
|
4
|
-
import { formatDatabaseMessage } from '$shared/utils/message-formatter';
|
|
2
|
+
import type { DatabaseMessage } from '$shared/types/database/schema';
|
|
3
|
+
import type { UnifiedMessage } from '$shared/types/unified';
|
|
5
4
|
import { debug } from '$shared/utils/logger';
|
|
6
5
|
|
|
6
|
+
/** Parse message JSON from a DB row. */
|
|
7
|
+
function parseMessage(row: DatabaseMessage): UnifiedMessage {
|
|
8
|
+
return JSON.parse(row.data) as UnifiedMessage;
|
|
9
|
+
}
|
|
10
|
+
|
|
7
11
|
export const messageQueries = {
|
|
8
12
|
/**
|
|
9
13
|
* Get visible messages for a session (git-like: from HEAD to root)
|
|
10
|
-
* This is the main function used to display messages in UI
|
|
14
|
+
* This is the main function used to display messages in UI.
|
|
11
15
|
*/
|
|
12
|
-
getBySessionId(sessionId: string):
|
|
16
|
+
getBySessionId(sessionId: string): UnifiedMessage[] {
|
|
13
17
|
const db = getDatabase();
|
|
14
18
|
|
|
15
|
-
// Get current HEAD from session
|
|
16
19
|
const session = db.prepare(`
|
|
17
|
-
SELECT
|
|
18
|
-
`).get(sessionId) as {
|
|
20
|
+
SELECT head_message_id FROM chat_sessions WHERE id = ?
|
|
21
|
+
`).get(sessionId) as { head_message_id: string | null } | null;
|
|
19
22
|
|
|
20
|
-
if (!session || !session.
|
|
23
|
+
if (!session || !session.head_message_id) {
|
|
21
24
|
return [];
|
|
22
25
|
}
|
|
23
26
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
// Parse SDK messages — propagate sender from user to subsequent messages
|
|
28
|
-
let lastSenderId: string | null = null;
|
|
29
|
-
let lastSenderName: string | null = null;
|
|
30
|
-
|
|
31
|
-
return path.map(msg => {
|
|
32
|
-
// Track sender from messages that have it stored
|
|
33
|
-
if (msg.sender_id) {
|
|
34
|
-
lastSenderId = msg.sender_id;
|
|
35
|
-
lastSenderName = msg.sender_name || null;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return formatDatabaseMessage(msg, {
|
|
39
|
-
sender_id: msg.sender_id || lastSenderId,
|
|
40
|
-
sender_name: msg.sender_name || lastSenderName,
|
|
41
|
-
});
|
|
42
|
-
});
|
|
43
|
-
},
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Get minimal preview data for a session: first user msg, last assistant msg, and counts.
|
|
47
|
-
* Used by the Sessions/History modal to avoid loading all messages.
|
|
48
|
-
*/
|
|
49
|
-
getSessionPreview(sessionId: string): {
|
|
50
|
-
firstUserMessage: DatabaseMessage | null;
|
|
51
|
-
lastAssistantMessage: DatabaseMessage | null;
|
|
52
|
-
userCount: number;
|
|
53
|
-
assistantCount: number;
|
|
54
|
-
} {
|
|
55
|
-
const db = getDatabase();
|
|
56
|
-
|
|
57
|
-
const firstUserMessage = db.prepare(`
|
|
58
|
-
SELECT * FROM messages
|
|
59
|
-
WHERE session_id = ? AND json_extract(sdk_message, '$.type') = 'user'
|
|
60
|
-
ORDER BY timestamp ASC
|
|
61
|
-
LIMIT 1
|
|
62
|
-
`).get(sessionId) as DatabaseMessage | null;
|
|
63
|
-
|
|
64
|
-
const lastAssistantMessage = db.prepare(`
|
|
65
|
-
SELECT * FROM messages
|
|
66
|
-
WHERE session_id = ? AND json_extract(sdk_message, '$.type') = 'assistant'
|
|
67
|
-
ORDER BY timestamp DESC
|
|
68
|
-
LIMIT 1
|
|
69
|
-
`).get(sessionId) as DatabaseMessage | null;
|
|
70
|
-
|
|
71
|
-
const counts = db.prepare(`
|
|
72
|
-
SELECT
|
|
73
|
-
SUM(CASE WHEN json_extract(sdk_message, '$.type') = 'user' THEN 1 ELSE 0 END) AS user_count,
|
|
74
|
-
SUM(CASE WHEN json_extract(sdk_message, '$.type') = 'assistant' THEN 1 ELSE 0 END) AS assistant_count
|
|
75
|
-
FROM messages
|
|
76
|
-
WHERE session_id = ?
|
|
77
|
-
`).get(sessionId) as { user_count: number; assistant_count: number } | null;
|
|
78
|
-
|
|
79
|
-
return {
|
|
80
|
-
firstUserMessage,
|
|
81
|
-
lastAssistantMessage,
|
|
82
|
-
userCount: counts?.user_count ?? 0,
|
|
83
|
-
assistantCount: counts?.assistant_count ?? 0
|
|
84
|
-
};
|
|
27
|
+
const path = this.getPathToRoot(session.head_message_id);
|
|
28
|
+
return path.map(parseMessage);
|
|
85
29
|
},
|
|
86
30
|
|
|
87
31
|
/**
|
|
@@ -92,7 +36,7 @@ export const messageQueries = {
|
|
|
92
36
|
const messages = db.prepare(`
|
|
93
37
|
SELECT * FROM messages
|
|
94
38
|
WHERE session_id = ?
|
|
95
|
-
ORDER BY
|
|
39
|
+
ORDER BY created_at ASC
|
|
96
40
|
`).all(sessionId) as DatabaseMessage[];
|
|
97
41
|
|
|
98
42
|
return messages;
|
|
@@ -109,43 +53,50 @@ export const messageQueries = {
|
|
|
109
53
|
|
|
110
54
|
create(messageData: {
|
|
111
55
|
session_id: string;
|
|
112
|
-
|
|
56
|
+
message: UnifiedMessage;
|
|
113
57
|
timestamp?: string;
|
|
114
|
-
sender_id?: string;
|
|
115
|
-
sender_name?: string;
|
|
116
58
|
branch_id?: string;
|
|
117
59
|
parent_message_id?: string;
|
|
118
60
|
}): DatabaseMessage {
|
|
119
61
|
const db = getDatabase();
|
|
120
62
|
const id = crypto.randomUUID();
|
|
121
|
-
|
|
122
63
|
const timestamp = messageData.timestamp || new Date().toISOString();
|
|
64
|
+
const parentMessageId = messageData.parent_message_id || null;
|
|
65
|
+
|
|
66
|
+
// Sync metadata into JSON blob so it is the single source of truth
|
|
67
|
+
const msg: UnifiedMessage = {
|
|
68
|
+
...messageData.message,
|
|
69
|
+
messageId: id,
|
|
70
|
+
createdAt: timestamp,
|
|
71
|
+
sessionId: messageData.message.sessionId,
|
|
72
|
+
parent: {
|
|
73
|
+
...messageData.message.parent,
|
|
74
|
+
messageId: parentMessageId,
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const serialized = JSON.stringify(msg);
|
|
123
79
|
|
|
124
|
-
// Save SDK message with git-like parent pointer
|
|
125
80
|
db.prepare(`
|
|
126
|
-
INSERT INTO messages (id, session_id,
|
|
127
|
-
VALUES (?, ?, ?, ?,
|
|
81
|
+
INSERT INTO messages (id, session_id, created_at, data, is_deleted, branch_id, parent_message_id)
|
|
82
|
+
VALUES (?, ?, ?, ?, 0, ?, ?)
|
|
128
83
|
`).run(
|
|
129
84
|
id,
|
|
130
85
|
messageData.session_id,
|
|
131
86
|
timestamp,
|
|
132
|
-
|
|
133
|
-
messageData.sender_id || null,
|
|
134
|
-
messageData.sender_name || null,
|
|
87
|
+
serialized,
|
|
135
88
|
messageData.branch_id || null,
|
|
136
|
-
|
|
89
|
+
parentMessageId
|
|
137
90
|
);
|
|
138
91
|
|
|
139
92
|
return {
|
|
140
93
|
id,
|
|
141
94
|
session_id: messageData.session_id,
|
|
142
|
-
timestamp,
|
|
143
|
-
|
|
144
|
-
sender_id: messageData.sender_id || null,
|
|
145
|
-
sender_name: messageData.sender_name || null,
|
|
95
|
+
created_at: timestamp,
|
|
96
|
+
data: serialized,
|
|
146
97
|
is_deleted: 0,
|
|
147
98
|
branch_id: messageData.branch_id || null,
|
|
148
|
-
parent_message_id:
|
|
99
|
+
parent_message_id: parentMessageId
|
|
149
100
|
};
|
|
150
101
|
},
|
|
151
102
|
|
|
@@ -163,7 +114,7 @@ export const messageQueries = {
|
|
|
163
114
|
const db = getDatabase();
|
|
164
115
|
db.prepare(`
|
|
165
116
|
DELETE FROM messages
|
|
166
|
-
WHERE session_id = ? AND
|
|
117
|
+
WHERE session_id = ? AND created_at >= ?
|
|
167
118
|
`).run(sessionId, timestamp);
|
|
168
119
|
},
|
|
169
120
|
|
|
@@ -174,15 +125,13 @@ export const messageQueries = {
|
|
|
174
125
|
softDeleteAfterTimestamp(sessionId: string, checkpointTimestamp: string, branchId: string): void {
|
|
175
126
|
const db = getDatabase();
|
|
176
127
|
|
|
177
|
-
// Get all messages with their SDK message for type checking
|
|
178
128
|
const allMessages = db.prepare(`
|
|
179
|
-
SELECT
|
|
129
|
+
SELECT * FROM messages
|
|
180
130
|
WHERE session_id = ?
|
|
181
|
-
ORDER BY
|
|
182
|
-
`).all(sessionId) as
|
|
131
|
+
ORDER BY created_at ASC
|
|
132
|
+
`).all(sessionId) as DatabaseMessage[];
|
|
183
133
|
|
|
184
|
-
|
|
185
|
-
const checkpointIndex = allMessages.findIndex(msg => msg.timestamp === checkpointTimestamp);
|
|
134
|
+
const checkpointIndex = allMessages.findIndex(msg => msg.created_at === checkpointTimestamp);
|
|
186
135
|
|
|
187
136
|
if (checkpointIndex === -1) {
|
|
188
137
|
debug.warn('database', `Checkpoint message with timestamp ${checkpointTimestamp} not found`);
|
|
@@ -192,8 +141,8 @@ export const messageQueries = {
|
|
|
192
141
|
// Find next USER message after checkpoint (this is where we start deleting)
|
|
193
142
|
let deleteFromIndex = -1;
|
|
194
143
|
for (let i = checkpointIndex + 1; i < allMessages.length; i++) {
|
|
195
|
-
const
|
|
196
|
-
if (
|
|
144
|
+
const msg = parseMessage(allMessages[i]);
|
|
145
|
+
if (msg.type === 'user') {
|
|
197
146
|
deleteFromIndex = i;
|
|
198
147
|
break;
|
|
199
148
|
}
|
|
@@ -248,17 +197,17 @@ export const messageQueries = {
|
|
|
248
197
|
// Also restore messages that are on main branch (no branch_id) up to the branching point
|
|
249
198
|
// We need to restore all messages before the first message in the target branch
|
|
250
199
|
const firstBranchMessage = db.prepare(`
|
|
251
|
-
SELECT MIN(
|
|
200
|
+
SELECT MIN(created_at) as min_created_at
|
|
252
201
|
FROM messages
|
|
253
202
|
WHERE session_id = ? AND branch_id = ?
|
|
254
|
-
`).get(sessionId, branchId) as {
|
|
203
|
+
`).get(sessionId, branchId) as { min_created_at: string } | undefined;
|
|
255
204
|
|
|
256
|
-
if (firstBranchMessage?.
|
|
205
|
+
if (firstBranchMessage?.min_created_at) {
|
|
257
206
|
db.prepare(`
|
|
258
207
|
UPDATE messages
|
|
259
208
|
SET is_deleted = 0
|
|
260
|
-
WHERE session_id = ? AND (branch_id IS NULL OR branch_id = '') AND
|
|
261
|
-
`).run(sessionId, firstBranchMessage.
|
|
209
|
+
WHERE session_id = ? AND (branch_id IS NULL OR branch_id = '') AND created_at < ?
|
|
210
|
+
`).run(sessionId, firstBranchMessage.min_created_at);
|
|
262
211
|
}
|
|
263
212
|
},
|
|
264
213
|
|
|
@@ -270,7 +219,7 @@ export const messageQueries = {
|
|
|
270
219
|
const messages = db.prepare(`
|
|
271
220
|
SELECT * FROM messages
|
|
272
221
|
WHERE session_id = ? AND branch_id = ?
|
|
273
|
-
ORDER BY
|
|
222
|
+
ORDER BY created_at ASC
|
|
274
223
|
`).all(sessionId, branchId) as DatabaseMessage[];
|
|
275
224
|
|
|
276
225
|
return messages;
|
|
@@ -299,8 +248,8 @@ export const messageQueries = {
|
|
|
299
248
|
const db = getDatabase();
|
|
300
249
|
const message = db.prepare(`
|
|
301
250
|
SELECT * FROM messages
|
|
302
|
-
WHERE session_id = ? AND
|
|
303
|
-
ORDER BY
|
|
251
|
+
WHERE session_id = ? AND created_at < ? AND (is_deleted IS NULL OR is_deleted = 0)
|
|
252
|
+
ORDER BY created_at DESC
|
|
304
253
|
LIMIT 1
|
|
305
254
|
`).get(sessionId, timestamp) as DatabaseMessage | null;
|
|
306
255
|
|
|
@@ -313,17 +262,16 @@ export const messageQueries = {
|
|
|
313
262
|
*/
|
|
314
263
|
getFirstAssistantAfterTimestamp(sessionId: string, timestamp: string): DatabaseMessage | null {
|
|
315
264
|
const db = getDatabase();
|
|
316
|
-
const
|
|
265
|
+
const rows = db.prepare(`
|
|
317
266
|
SELECT * FROM messages
|
|
318
|
-
WHERE session_id = ? AND
|
|
319
|
-
ORDER BY
|
|
267
|
+
WHERE session_id = ? AND created_at > ? AND (is_deleted IS NULL OR is_deleted = 0)
|
|
268
|
+
ORDER BY created_at ASC
|
|
320
269
|
`).all(sessionId, timestamp) as DatabaseMessage[];
|
|
321
270
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
return message;
|
|
271
|
+
for (const row of rows) {
|
|
272
|
+
const msg = parseMessage(row);
|
|
273
|
+
if (msg.type === 'assistant') {
|
|
274
|
+
return row;
|
|
327
275
|
}
|
|
328
276
|
}
|
|
329
277
|
|
|
@@ -340,7 +288,7 @@ export const messageQueries = {
|
|
|
340
288
|
const messages = db.prepare(`
|
|
341
289
|
SELECT * FROM messages
|
|
342
290
|
WHERE parent_message_id = ?
|
|
343
|
-
ORDER BY
|
|
291
|
+
ORDER BY created_at ASC
|
|
344
292
|
`).all(messageId) as DatabaseMessage[];
|
|
345
293
|
|
|
346
294
|
return messages;
|
|
@@ -430,7 +378,7 @@ export const messageQueries = {
|
|
|
430
378
|
const messages = db.prepare(`
|
|
431
379
|
SELECT * FROM messages
|
|
432
380
|
WHERE session_id = ?
|
|
433
|
-
ORDER BY
|
|
381
|
+
ORDER BY created_at ASC
|
|
434
382
|
`).all(sessionId) as DatabaseMessage[];
|
|
435
383
|
|
|
436
384
|
const graph = new Map<string, DatabaseMessage>();
|
|
@@ -482,47 +430,44 @@ export const messageQueries = {
|
|
|
482
430
|
/**
|
|
483
431
|
* Mark messages with unanswered tool_use blocks as interrupted.
|
|
484
432
|
* Called when stream ends (complete/error/cancel) to persist the interrupted state.
|
|
485
|
-
* Adds metadata.interrupted = true to the sdk_message JSON at the message level.
|
|
486
433
|
*/
|
|
487
434
|
markInterruptedMessages(sessionId: string): void {
|
|
488
435
|
const db = getDatabase();
|
|
489
436
|
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
SELECT id, sdk_message FROM messages
|
|
437
|
+
const rows = db.prepare(`
|
|
438
|
+
SELECT * FROM messages
|
|
493
439
|
WHERE session_id = ? AND (is_deleted IS NULL OR is_deleted = 0)
|
|
494
|
-
ORDER BY
|
|
495
|
-
`).all(sessionId) as
|
|
440
|
+
ORDER BY created_at ASC
|
|
441
|
+
`).all(sessionId) as DatabaseMessage[];
|
|
496
442
|
|
|
497
443
|
// Collect all tool_use_ids that have a matching tool_result
|
|
498
444
|
const answeredToolIds = new Set<string>();
|
|
499
|
-
for (const
|
|
500
|
-
const
|
|
501
|
-
if (
|
|
502
|
-
const
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
answeredToolIds.add(item.tool_use_id);
|
|
445
|
+
for (const row of rows) {
|
|
446
|
+
const msg = parseMessage(row);
|
|
447
|
+
if (msg.type !== 'user') continue;
|
|
448
|
+
for (const block of msg.content) {
|
|
449
|
+
if (block.type === 'tool_result' && block.toolUseId) {
|
|
450
|
+
answeredToolIds.add(block.toolUseId);
|
|
506
451
|
}
|
|
507
452
|
}
|
|
508
453
|
}
|
|
509
454
|
|
|
510
455
|
// Find assistant messages with unanswered tool_use blocks and mark them
|
|
511
|
-
const updateStmt = db.prepare(`UPDATE messages SET
|
|
456
|
+
const updateStmt = db.prepare(`UPDATE messages SET data = ? WHERE id = ?`);
|
|
512
457
|
|
|
513
|
-
for (const
|
|
514
|
-
const
|
|
515
|
-
if (
|
|
516
|
-
const content = Array.isArray(sdk.message.content) ? sdk.message.content : [];
|
|
458
|
+
for (const row of rows) {
|
|
459
|
+
const msg = parseMessage(row);
|
|
460
|
+
if (msg.type !== 'assistant') continue;
|
|
517
461
|
|
|
518
|
-
const hasUnansweredTool = content.some(
|
|
519
|
-
|
|
462
|
+
const hasUnansweredTool = msg.content.some(
|
|
463
|
+
block => block.type === 'tool_use' && block.id && !answeredToolIds.has(block.id)
|
|
520
464
|
);
|
|
521
465
|
|
|
522
|
-
if (hasUnansweredTool
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
}
|
|
466
|
+
if (!hasUnansweredTool || msg.stopReason === 'interrupted') continue;
|
|
467
|
+
|
|
468
|
+
// Write back as UnifiedMessage with interrupted stopReason
|
|
469
|
+
const updated = { ...msg, stopReason: 'interrupted' as const };
|
|
470
|
+
updateStmt.run(JSON.stringify(updated), row.id);
|
|
526
471
|
}
|
|
527
472
|
},
|
|
528
473
|
|
|
@@ -558,4 +503,4 @@ export const messageQueries = {
|
|
|
558
503
|
|
|
559
504
|
return groups;
|
|
560
505
|
}
|
|
561
|
-
};
|
|
506
|
+
};
|