@myrialabs/clopen 0.2.24 → 0.3.1
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/029_migrate_messages_to_unified.ts +1108 -0
- package/backend/database/migrations/index.ts +7 -0
- package/backend/database/queries/engine-queries.ts +195 -36
- package/backend/database/queries/index.ts +1 -2
- 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 +552 -0
- package/backend/engine/adapters/claude/stream.ts +59 -47
- package/backend/engine/adapters/opencode/config.ts +7 -7
- package/backend/engine/adapters/opencode/message-converter.ts +328 -352
- 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/providers.ts +25 -24
- 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-w7Uwi2Jz.js → index-BVWBZlgD.js} +2 -2
- package/dist/assets/main-Cgbs6dR7.js +246 -0
- package/dist/assets/main-TEa2MRnh.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 +130 -0
- package/shared/types/unified/message.ts +151 -0
- package/shared/types/unified/stream.ts +157 -0
- package/shared/types/unified/tool.ts +409 -0
- package/shared/utils/message-formatter.ts +9 -73
- package/shared/utils/path.ts +3 -2
- package/tsconfig.json +1 -1
- package/backend/database/queries/opencode-provider-queries.ts +0 -190
- package/backend/engine/adapters/claude/path-utils.ts +0 -38
- package/backend/ws/chat/background.ts +0 -201
- package/dist/assets/main-B4q8wX9B.js +0 -257
- package/dist/assets/main-Bfz5Urp3.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
|
@@ -65,10 +65,10 @@ const chatSessionInputState = new Map<string, { text: string; senderId: string;
|
|
|
65
65
|
const chatSessionEditMode = new Map<string, { isEditing: boolean; messageId: string | null; messageTimestamp: string | null }>();
|
|
66
66
|
|
|
67
67
|
// In-memory store for model state per chat session (keyed by chatSessionId)
|
|
68
|
-
const chatSessionModelState = new Map<string, { engine: string;
|
|
68
|
+
const chatSessionModelState = new Map<string, { engine: string; provider: string; modelId: string; modelName: string; senderId: string }>();
|
|
69
69
|
|
|
70
70
|
// In-memory store for account state per chat session (keyed by chatSessionId)
|
|
71
|
-
const chatSessionAccountState = new Map<string, {
|
|
71
|
+
const chatSessionAccountState = new Map<string, { accountId: number | null; senderId: string }>();
|
|
72
72
|
|
|
73
73
|
export const streamHandler = createRouter()
|
|
74
74
|
// Join a chat session room (subscribe to chat events for this session)
|
|
@@ -101,14 +101,23 @@ export const streamHandler = createRouter()
|
|
|
101
101
|
sessionId: t.String(),
|
|
102
102
|
chatSessionId: t.String(),
|
|
103
103
|
projectPath: t.String(),
|
|
104
|
-
prompt: t.Any(), //
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
104
|
+
prompt: t.Any(), // UserMessage object
|
|
105
|
+
engine: t.Object({
|
|
106
|
+
type: t.Union([t.Literal('claude-code'), t.Literal('opencode')]),
|
|
107
|
+
provider: t.String(),
|
|
108
|
+
model: t.Object({
|
|
109
|
+
id: t.String(),
|
|
110
|
+
name: t.String()
|
|
111
|
+
}),
|
|
112
|
+
account: t.Object({
|
|
113
|
+
id: t.Number(),
|
|
114
|
+
name: t.String()
|
|
115
|
+
})
|
|
116
|
+
}),
|
|
117
|
+
sender: t.Object({
|
|
118
|
+
id: t.String(),
|
|
119
|
+
name: t.String()
|
|
120
|
+
})
|
|
112
121
|
})
|
|
113
122
|
}, async ({ data, conn }) => {
|
|
114
123
|
const projectId = ws.getProjectId(conn);
|
|
@@ -116,8 +125,7 @@ export const streamHandler = createRouter()
|
|
|
116
125
|
try {
|
|
117
126
|
debug.log('chat', 'WS chat:stream received:', {
|
|
118
127
|
chatSessionId: data.chatSessionId,
|
|
119
|
-
projectId
|
|
120
|
-
messagesCount: data.messages?.length || 0
|
|
128
|
+
projectId
|
|
121
129
|
});
|
|
122
130
|
|
|
123
131
|
// Start background stream
|
|
@@ -125,14 +133,9 @@ export const streamHandler = createRouter()
|
|
|
125
133
|
projectPath: data.projectPath,
|
|
126
134
|
projectId,
|
|
127
135
|
prompt: data.prompt,
|
|
128
|
-
messages: data.messages || [],
|
|
129
136
|
chatSessionId: data.chatSessionId,
|
|
130
|
-
engine: data.engine
|
|
131
|
-
|
|
132
|
-
temperature: data.temperature,
|
|
133
|
-
senderId: data.senderId,
|
|
134
|
-
senderName: data.senderName,
|
|
135
|
-
claudeAccountId: data.claudeAccountId
|
|
137
|
+
engine: data.engine,
|
|
138
|
+
sender: data.sender
|
|
136
139
|
});
|
|
137
140
|
|
|
138
141
|
debug.log('chat', 'Stream started with ID:', streamId);
|
|
@@ -452,6 +455,63 @@ export const streamHandler = createRouter()
|
|
|
452
455
|
}
|
|
453
456
|
})
|
|
454
457
|
|
|
458
|
+
// Get active stream state (for reconnect after browser refresh / project switch)
|
|
459
|
+
.http('chat:stream-state', {
|
|
460
|
+
data: t.Object({
|
|
461
|
+
chatSessionId: t.Optional(t.String())
|
|
462
|
+
}),
|
|
463
|
+
response: t.Object({
|
|
464
|
+
streamId: t.String(),
|
|
465
|
+
status: t.Union([
|
|
466
|
+
t.Literal('active'),
|
|
467
|
+
t.Literal('completed'),
|
|
468
|
+
t.Literal('error'),
|
|
469
|
+
t.Literal('cancelled')
|
|
470
|
+
]),
|
|
471
|
+
processId: t.String(),
|
|
472
|
+
messages: t.Array(t.Any()),
|
|
473
|
+
currentPartialText: t.Optional(t.String()),
|
|
474
|
+
currentReasoningText: t.Optional(t.String()),
|
|
475
|
+
error: t.Optional(t.String()),
|
|
476
|
+
startedAt: t.String(),
|
|
477
|
+
completedAt: t.Optional(t.String())
|
|
478
|
+
})
|
|
479
|
+
}, async ({ data, conn }) => {
|
|
480
|
+
const projectId = ws.getProjectId(conn);
|
|
481
|
+
|
|
482
|
+
const streamState = data.chatSessionId
|
|
483
|
+
? streamManager.getSessionStream(data.chatSessionId, projectId)
|
|
484
|
+
: undefined;
|
|
485
|
+
|
|
486
|
+
// Return a "not found" response instead of throwing
|
|
487
|
+
// This happens normally when a stream was cancelled/completed and cleaned up
|
|
488
|
+
if (!streamState) {
|
|
489
|
+
return {
|
|
490
|
+
streamId: data.chatSessionId || '',
|
|
491
|
+
status: 'completed' as const,
|
|
492
|
+
processId: '',
|
|
493
|
+
messages: [],
|
|
494
|
+
currentPartialText: undefined,
|
|
495
|
+
currentReasoningText: undefined,
|
|
496
|
+
error: undefined,
|
|
497
|
+
startedAt: new Date().toISOString(),
|
|
498
|
+
completedAt: new Date().toISOString()
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
return {
|
|
503
|
+
streamId: streamState.streamId,
|
|
504
|
+
status: streamState.status,
|
|
505
|
+
processId: streamState.processId,
|
|
506
|
+
messages: streamState.messages,
|
|
507
|
+
currentPartialText: streamState.currentPartialText,
|
|
508
|
+
currentReasoningText: streamState.currentReasoningText,
|
|
509
|
+
error: streamState.error,
|
|
510
|
+
startedAt: streamState.startedAt.toISOString(),
|
|
511
|
+
completedAt: streamState.completedAt?.toISOString()
|
|
512
|
+
};
|
|
513
|
+
})
|
|
514
|
+
|
|
455
515
|
// Cancel stream
|
|
456
516
|
.on('chat:cancel', {
|
|
457
517
|
data: t.Object({
|
|
@@ -601,7 +661,9 @@ export const streamHandler = createRouter()
|
|
|
601
661
|
senderId: t.String(),
|
|
602
662
|
chatSessionId: t.String(),
|
|
603
663
|
engine: t.String(),
|
|
604
|
-
|
|
664
|
+
provider: t.String(),
|
|
665
|
+
modelId: t.String(),
|
|
666
|
+
modelName: t.String()
|
|
605
667
|
})
|
|
606
668
|
}, ({ data }) => {
|
|
607
669
|
const chatSessionId = data.chatSessionId;
|
|
@@ -609,14 +671,16 @@ export const streamHandler = createRouter()
|
|
|
609
671
|
// Store latest model state on server for late joiners / refresh
|
|
610
672
|
chatSessionModelState.set(chatSessionId, {
|
|
611
673
|
engine: data.engine,
|
|
612
|
-
|
|
674
|
+
provider: data.provider,
|
|
675
|
+
modelId: data.modelId,
|
|
676
|
+
modelName: data.modelName,
|
|
613
677
|
senderId: data.senderId
|
|
614
678
|
});
|
|
615
679
|
|
|
616
680
|
// Persist engine/model to the session record in the database
|
|
617
681
|
// so refreshes and late joiners get the correct model
|
|
618
682
|
try {
|
|
619
|
-
sessionQueries.updateEngineModel(chatSessionId, data.engine, data.
|
|
683
|
+
sessionQueries.updateEngineModel(chatSessionId, data.engine, data.provider, data.modelId, data.modelName);
|
|
620
684
|
} catch (err) {
|
|
621
685
|
debug.error('chat', 'Failed to persist model sync to DB:', err);
|
|
622
686
|
}
|
|
@@ -625,45 +689,32 @@ export const streamHandler = createRouter()
|
|
|
625
689
|
ws.emit.chatSession(chatSessionId, 'chat:model-sync', {
|
|
626
690
|
senderId: data.senderId,
|
|
627
691
|
engine: data.engine,
|
|
628
|
-
|
|
692
|
+
provider: data.provider,
|
|
693
|
+
modelId: data.modelId,
|
|
694
|
+
modelName: data.modelName
|
|
629
695
|
});
|
|
630
696
|
})
|
|
631
697
|
|
|
632
|
-
// Get current model state for a chat session (for refresh / late joiners)
|
|
633
|
-
.http('chat:get-model-state', {
|
|
634
|
-
data: t.Object({
|
|
635
|
-
chatSessionId: t.Optional(t.String())
|
|
636
|
-
}),
|
|
637
|
-
response: t.Object({
|
|
638
|
-
engine: t.String(),
|
|
639
|
-
model: t.String(),
|
|
640
|
-
senderId: t.String()
|
|
641
|
-
})
|
|
642
|
-
}, ({ data }) => {
|
|
643
|
-
const chatSessionId = data.chatSessionId || '';
|
|
644
|
-
const state = chatSessionModelState.get(chatSessionId);
|
|
645
|
-
return state || { engine: '', model: '', senderId: '' };
|
|
646
|
-
})
|
|
647
|
-
|
|
648
698
|
// Collaborative account sync - broadcast account changes to other users in the same chat session
|
|
649
699
|
.on('chat:account-sync', {
|
|
650
700
|
data: t.Object({
|
|
651
701
|
senderId: t.String(),
|
|
652
702
|
chatSessionId: t.String(),
|
|
653
|
-
|
|
703
|
+
accountId: t.Union([t.Number(), t.Null()]),
|
|
704
|
+
accountName: t.Optional(t.Union([t.String(), t.Null()]))
|
|
654
705
|
})
|
|
655
706
|
}, ({ data }) => {
|
|
656
707
|
const chatSessionId = data.chatSessionId;
|
|
657
708
|
|
|
658
709
|
// Store latest account state on server for late joiners / refresh
|
|
659
710
|
chatSessionAccountState.set(chatSessionId, {
|
|
660
|
-
|
|
711
|
+
accountId: data.accountId,
|
|
661
712
|
senderId: data.senderId
|
|
662
713
|
});
|
|
663
714
|
|
|
664
|
-
// Persist
|
|
715
|
+
// Persist account to the session record in the database
|
|
665
716
|
try {
|
|
666
|
-
sessionQueries.
|
|
717
|
+
sessionQueries.updateAccount(chatSessionId, data.accountId, data.accountName ?? null);
|
|
667
718
|
} catch (err) {
|
|
668
719
|
debug.error('chat', 'Failed to persist account sync to DB:', err);
|
|
669
720
|
}
|
|
@@ -671,25 +722,11 @@ export const streamHandler = createRouter()
|
|
|
671
722
|
// Broadcast to all users in the same chat session
|
|
672
723
|
ws.emit.chatSession(chatSessionId, 'chat:account-sync', {
|
|
673
724
|
senderId: data.senderId,
|
|
674
|
-
|
|
725
|
+
accountId: data.accountId,
|
|
726
|
+
accountName: data.accountName ?? null
|
|
675
727
|
});
|
|
676
728
|
})
|
|
677
729
|
|
|
678
|
-
// Get current account state for a chat session (for refresh / late joiners)
|
|
679
|
-
.http('chat:get-account-state', {
|
|
680
|
-
data: t.Object({
|
|
681
|
-
chatSessionId: t.Optional(t.String())
|
|
682
|
-
}),
|
|
683
|
-
response: t.Object({
|
|
684
|
-
claudeAccountId: t.Union([t.Number(), t.Null()]),
|
|
685
|
-
senderId: t.String()
|
|
686
|
-
})
|
|
687
|
-
}, ({ data }) => {
|
|
688
|
-
const chatSessionId = data.chatSessionId || '';
|
|
689
|
-
const state = chatSessionAccountState.get(chatSessionId);
|
|
690
|
-
return state || { claudeAccountId: null, senderId: '' };
|
|
691
|
-
})
|
|
692
|
-
|
|
693
730
|
// Event declarations
|
|
694
731
|
.emit('chat:edit-mode', t.Object({
|
|
695
732
|
senderId: t.String(),
|
|
@@ -714,12 +751,15 @@ export const streamHandler = createRouter()
|
|
|
714
751
|
.emit('chat:model-sync', t.Object({
|
|
715
752
|
senderId: t.String(),
|
|
716
753
|
engine: t.String(),
|
|
717
|
-
|
|
754
|
+
provider: t.String(),
|
|
755
|
+
modelId: t.String(),
|
|
756
|
+
modelName: t.String()
|
|
718
757
|
}))
|
|
719
758
|
|
|
720
759
|
.emit('chat:account-sync', t.Object({
|
|
721
760
|
senderId: t.String(),
|
|
722
|
-
|
|
761
|
+
accountId: t.Union([t.Number(), t.Null()]),
|
|
762
|
+
accountName: t.Union([t.String(), t.Null()])
|
|
723
763
|
}))
|
|
724
764
|
|
|
725
765
|
.emit('chat:connection', t.Object({
|
|
@@ -127,7 +127,9 @@ export const accountsHandler = createRouter()
|
|
|
127
127
|
}))
|
|
128
128
|
})
|
|
129
129
|
}, async () => {
|
|
130
|
-
const
|
|
130
|
+
const provider = engineQueries.getProviderBySlug('claude-code', 'anthropic');
|
|
131
|
+
if (!provider) return { accounts: [] };
|
|
132
|
+
const accounts = engineQueries.getAccountsByProvider(provider.id);
|
|
131
133
|
return {
|
|
132
134
|
accounts: accounts.map(a => ({
|
|
133
135
|
id: a.id,
|
|
@@ -142,7 +144,7 @@ export const accountsHandler = createRouter()
|
|
|
142
144
|
data: t.Object({ id: t.Number() }),
|
|
143
145
|
response: t.Object({ success: t.Boolean() })
|
|
144
146
|
}, async ({ data }) => {
|
|
145
|
-
engineQueries.
|
|
147
|
+
engineQueries.switchAccount(data.id);
|
|
146
148
|
resetEnvironment();
|
|
147
149
|
return { success: true };
|
|
148
150
|
})
|
|
@@ -151,8 +153,8 @@ export const accountsHandler = createRouter()
|
|
|
151
153
|
data: t.Object({ id: t.Number() }),
|
|
152
154
|
response: t.Object({ success: t.Boolean() })
|
|
153
155
|
}, async ({ data }) => {
|
|
154
|
-
const active = engineQueries.
|
|
155
|
-
engineQueries.
|
|
156
|
+
const active = engineQueries.getActiveAccountForEngine('claude-code');
|
|
157
|
+
engineQueries.deleteAccount(data.id);
|
|
156
158
|
if (active?.id === data.id) resetEnvironment();
|
|
157
159
|
return { success: true };
|
|
158
160
|
})
|
|
@@ -161,7 +163,7 @@ export const accountsHandler = createRouter()
|
|
|
161
163
|
data: t.Object({ id: t.Number(), name: t.String({ minLength: 1 }) }),
|
|
162
164
|
response: t.Object({ success: t.Boolean() })
|
|
163
165
|
}, async ({ data }) => {
|
|
164
|
-
engineQueries.
|
|
166
|
+
engineQueries.renameAccount(data.id, data.name);
|
|
165
167
|
return { success: true };
|
|
166
168
|
})
|
|
167
169
|
|
|
@@ -258,7 +260,16 @@ export const accountsHandler = createRouter()
|
|
|
258
260
|
entry.phase = 'done';
|
|
259
261
|
debug.log('engine', `[${setupId}] Token captured`);
|
|
260
262
|
|
|
261
|
-
const
|
|
263
|
+
const provider = engineQueries.getProviderBySlug('claude-code', 'anthropic');
|
|
264
|
+
if (!provider) {
|
|
265
|
+
ws.emit.user(userId, 'engine:claude-account-setup-error', {
|
|
266
|
+
setupId,
|
|
267
|
+
message: 'Anthropic provider not found in DB'
|
|
268
|
+
});
|
|
269
|
+
cleanupSetup(setupId);
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
const account = engineQueries.createAccount(provider.id, entry.accountName, token);
|
|
262
273
|
resetEnvironment();
|
|
263
274
|
|
|
264
275
|
ws.emit.user(userId, 'engine:claude-account-setup-complete', {
|
|
@@ -303,7 +314,16 @@ export const accountsHandler = createRouter()
|
|
|
303
314
|
const token = extractOAuthToken(clean);
|
|
304
315
|
if (token) {
|
|
305
316
|
debug.log('engine', `[${setupId}] Token found in final buffer`);
|
|
306
|
-
const
|
|
317
|
+
const provider = engineQueries.getProviderBySlug('claude-code', 'anthropic');
|
|
318
|
+
if (!provider) {
|
|
319
|
+
ws.emit.user(userId, 'engine:claude-account-setup-error', {
|
|
320
|
+
setupId,
|
|
321
|
+
message: 'Anthropic provider not found in DB'
|
|
322
|
+
});
|
|
323
|
+
cleanupSetup(setupId);
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
const account = engineQueries.createAccount(provider.id, entry.accountName, token);
|
|
307
327
|
resetEnvironment();
|
|
308
328
|
ws.emit.user(userId, 'engine:claude-account-setup-complete', {
|
|
309
329
|
setupId,
|
|
@@ -31,8 +31,9 @@ export const claudeCodeStatusHandler = createRouter()
|
|
|
31
31
|
debug.log('engine', 'Checking Claude Code status...');
|
|
32
32
|
|
|
33
33
|
const { installed, version } = await getStatus('claude');
|
|
34
|
-
const
|
|
35
|
-
const
|
|
34
|
+
const provider = engineQueries.getProviderBySlug('claude-code', 'anthropic');
|
|
35
|
+
const accounts = provider ? engineQueries.getAccountsByProvider(provider.id) : [];
|
|
36
|
+
const activeAccount = engineQueries.getActiveAccountForEngine('claude-code');
|
|
36
37
|
|
|
37
38
|
return {
|
|
38
39
|
installed,
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import { t } from 'elysia';
|
|
8
8
|
import { createRouter } from '$shared/utils/ws-server';
|
|
9
|
-
import {
|
|
9
|
+
import { engineQueries } from '../../../database/queries';
|
|
10
10
|
import {
|
|
11
11
|
fetchAndCacheModelsDevCatalog,
|
|
12
12
|
getCachedModelsDevCatalog,
|
|
@@ -26,9 +26,9 @@ const AccountSchema = t.Object({
|
|
|
26
26
|
|
|
27
27
|
const ProviderSchema = t.Object({
|
|
28
28
|
id: t.Number(),
|
|
29
|
-
|
|
29
|
+
slug: t.String(),
|
|
30
30
|
name: t.String(),
|
|
31
|
-
npm: t.String(),
|
|
31
|
+
npm: t.Union([t.String(), t.Null()]),
|
|
32
32
|
apiUrl: t.Union([t.String(), t.Null()]),
|
|
33
33
|
options: t.String(),
|
|
34
34
|
isEnabled: t.Boolean(),
|
|
@@ -48,11 +48,11 @@ export const openCodeProviderHandler = createRouter()
|
|
|
48
48
|
providers: t.Array(ProviderSchema)
|
|
49
49
|
})
|
|
50
50
|
}, async () => {
|
|
51
|
-
const providers =
|
|
51
|
+
const providers = engineQueries.getProvidersWithAccounts('opencode');
|
|
52
52
|
return {
|
|
53
53
|
providers: providers.map(p => ({
|
|
54
54
|
id: p.id,
|
|
55
|
-
|
|
55
|
+
slug: p.slug,
|
|
56
56
|
name: p.name,
|
|
57
57
|
npm: p.npm,
|
|
58
58
|
apiUrl: p.api_url,
|
|
@@ -71,42 +71,43 @@ export const openCodeProviderHandler = createRouter()
|
|
|
71
71
|
|
|
72
72
|
.http('engine:opencode-provider-add', {
|
|
73
73
|
data: t.Object({
|
|
74
|
-
|
|
74
|
+
slug: t.String({ minLength: 1 }),
|
|
75
75
|
name: t.String({ minLength: 1 }),
|
|
76
|
-
npm: t.String(
|
|
76
|
+
npm: t.Optional(t.Union([t.String(), t.Null()])),
|
|
77
77
|
apiUrl: t.Optional(t.String()),
|
|
78
78
|
options: t.Optional(t.String()),
|
|
79
79
|
accountName: t.String({ minLength: 1 }),
|
|
80
|
-
|
|
80
|
+
credential: t.String({ minLength: 1 }),
|
|
81
81
|
}),
|
|
82
82
|
response: t.Object({
|
|
83
83
|
provider: ProviderSchema
|
|
84
84
|
})
|
|
85
85
|
}, async ({ data }) => {
|
|
86
86
|
// Check if provider already exists
|
|
87
|
-
const existing =
|
|
87
|
+
const existing = engineQueries.getProviderBySlug('opencode', data.slug);
|
|
88
88
|
if (existing) {
|
|
89
|
-
throw new Error(`Provider "${data.
|
|
89
|
+
throw new Error(`Provider "${data.slug}" already configured`);
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
// Create provider
|
|
93
|
-
const provider =
|
|
94
|
-
|
|
93
|
+
const provider = engineQueries.createProvider({
|
|
94
|
+
engineType: 'opencode',
|
|
95
|
+
slug: data.slug,
|
|
95
96
|
name: data.name,
|
|
96
|
-
npm: data.npm,
|
|
97
|
+
npm: data.npm ?? undefined,
|
|
97
98
|
apiUrl: data.apiUrl,
|
|
98
99
|
options: data.options,
|
|
99
100
|
});
|
|
100
101
|
|
|
101
102
|
// Create first account (auto-active)
|
|
102
|
-
const account =
|
|
103
|
+
const account = engineQueries.createAccount(provider.id, data.accountName, data.credential);
|
|
103
104
|
|
|
104
|
-
debug.log('engine', `OpenCode provider added: ${data.
|
|
105
|
+
debug.log('engine', `OpenCode provider added: ${data.slug} with account "${data.accountName}"`);
|
|
105
106
|
|
|
106
107
|
return {
|
|
107
108
|
provider: {
|
|
108
109
|
id: provider.id,
|
|
109
|
-
|
|
110
|
+
slug: provider.slug,
|
|
110
111
|
name: provider.name,
|
|
111
112
|
npm: provider.npm,
|
|
112
113
|
apiUrl: provider.api_url,
|
|
@@ -127,7 +128,7 @@ export const openCodeProviderHandler = createRouter()
|
|
|
127
128
|
data: t.Object({ id: t.Number() }),
|
|
128
129
|
response: t.Object({ success: t.Boolean() })
|
|
129
130
|
}, async ({ data }) => {
|
|
130
|
-
|
|
131
|
+
engineQueries.deleteProvider(data.id);
|
|
131
132
|
debug.log('engine', `OpenCode provider removed: ${data.id}`);
|
|
132
133
|
return { success: true };
|
|
133
134
|
})
|
|
@@ -136,7 +137,7 @@ export const openCodeProviderHandler = createRouter()
|
|
|
136
137
|
data: t.Object({ id: t.Number(), enabled: t.Boolean() }),
|
|
137
138
|
response: t.Object({ success: t.Boolean() })
|
|
138
139
|
}, async ({ data }) => {
|
|
139
|
-
|
|
140
|
+
engineQueries.toggleProvider(data.id, data.enabled);
|
|
140
141
|
return { success: true };
|
|
141
142
|
})
|
|
142
143
|
|
|
@@ -146,7 +147,7 @@ export const openCodeProviderHandler = createRouter()
|
|
|
146
147
|
}, async ({ data }) => {
|
|
147
148
|
// Validate JSON
|
|
148
149
|
try { JSON.parse(data.options); } catch { throw new Error('Invalid JSON for options'); }
|
|
149
|
-
|
|
150
|
+
engineQueries.updateProviderOptions(data.id, data.options);
|
|
150
151
|
return { success: true };
|
|
151
152
|
})
|
|
152
153
|
|
|
@@ -158,13 +159,13 @@ export const openCodeProviderHandler = createRouter()
|
|
|
158
159
|
data: t.Object({
|
|
159
160
|
providerDbId: t.Number(),
|
|
160
161
|
name: t.String({ minLength: 1 }),
|
|
161
|
-
|
|
162
|
+
credential: t.String({ minLength: 1 }),
|
|
162
163
|
}),
|
|
163
164
|
response: t.Object({
|
|
164
165
|
account: AccountSchema
|
|
165
166
|
})
|
|
166
167
|
}, async ({ data }) => {
|
|
167
|
-
const account =
|
|
168
|
+
const account = engineQueries.createAccount(data.providerDbId, data.name, data.credential);
|
|
168
169
|
debug.log('engine', `OpenCode account added: "${data.name}" for provider ${data.providerDbId}`);
|
|
169
170
|
return {
|
|
170
171
|
account: {
|
|
@@ -180,7 +181,7 @@ export const openCodeProviderHandler = createRouter()
|
|
|
180
181
|
data: t.Object({ accountId: t.Number() }),
|
|
181
182
|
response: t.Object({ success: t.Boolean() })
|
|
182
183
|
}, async ({ data }) => {
|
|
183
|
-
|
|
184
|
+
engineQueries.switchAccount(data.accountId);
|
|
184
185
|
return { success: true };
|
|
185
186
|
})
|
|
186
187
|
|
|
@@ -188,7 +189,7 @@ export const openCodeProviderHandler = createRouter()
|
|
|
188
189
|
data: t.Object({ accountId: t.Number() }),
|
|
189
190
|
response: t.Object({ success: t.Boolean() })
|
|
190
191
|
}, async ({ data }) => {
|
|
191
|
-
|
|
192
|
+
engineQueries.deleteAccount(data.accountId);
|
|
192
193
|
return { success: true };
|
|
193
194
|
})
|
|
194
195
|
|
|
@@ -196,7 +197,7 @@ export const openCodeProviderHandler = createRouter()
|
|
|
196
197
|
data: t.Object({ accountId: t.Number(), name: t.String({ minLength: 1 }) }),
|
|
197
198
|
response: t.Object({ success: t.Boolean() })
|
|
198
199
|
}, async ({ data }) => {
|
|
199
|
-
|
|
200
|
+
engineQueries.renameAccount(data.accountId, data.name);
|
|
200
201
|
return { success: true };
|
|
201
202
|
})
|
|
202
203
|
|
|
@@ -9,8 +9,7 @@ import { createRouter } from '$shared/utils/ws-server';
|
|
|
9
9
|
import { execGit } from '../../git/git-executor';
|
|
10
10
|
import { projectQueries } from '../../database/queries/project-queries';
|
|
11
11
|
import { initializeEngine } from '../../engine';
|
|
12
|
-
import {
|
|
13
|
-
import type { EngineType } from '$shared/types/engine';
|
|
12
|
+
import type { EngineType } from '$shared/types/unified';
|
|
14
13
|
import type { GeneratedCommitMessage } from '$shared/types/git';
|
|
15
14
|
import { debug } from '$shared/utils/logger';
|
|
16
15
|
|
|
@@ -43,7 +42,8 @@ export const commitMessageHandler = createRouter()
|
|
|
43
42
|
data: t.Object({
|
|
44
43
|
projectId: t.String(),
|
|
45
44
|
engine: t.String(),
|
|
46
|
-
|
|
45
|
+
providerSlug: t.String(),
|
|
46
|
+
modelId: t.String(),
|
|
47
47
|
format: t.Union([t.Literal('single-line'), t.Literal('multi-line')])
|
|
48
48
|
}),
|
|
49
49
|
response: t.Object({
|
|
@@ -83,14 +83,12 @@ Rules:
|
|
|
83
83
|
Git diff:
|
|
84
84
|
${rawDiff}`;
|
|
85
85
|
|
|
86
|
-
|
|
87
|
-
const { modelId } = parseModelId(data.model);
|
|
88
|
-
|
|
89
|
-
debug.log('git', `Generating commit message via ${engineType}/${modelId}`);
|
|
86
|
+
debug.log('git', `Generating commit message via ${engineType}/${data.modelId}`);
|
|
90
87
|
|
|
91
88
|
const result = await engine.generateStructured<GeneratedCommitMessage>({
|
|
92
89
|
prompt,
|
|
93
|
-
|
|
90
|
+
providerSlug: data.providerSlug,
|
|
91
|
+
modelId: data.modelId,
|
|
94
92
|
schema: COMMIT_MESSAGE_SCHEMA,
|
|
95
93
|
projectPath: project.path
|
|
96
94
|
});
|
|
@@ -10,16 +10,7 @@
|
|
|
10
10
|
import { t } from 'elysia';
|
|
11
11
|
import { createRouter } from '$shared/utils/ws-server';
|
|
12
12
|
import { messageQueries } from '../../database/queries';
|
|
13
|
-
import {
|
|
14
|
-
|
|
15
|
-
function extractTextContent(content: unknown): string {
|
|
16
|
-
if (typeof content === 'string') return content;
|
|
17
|
-
if (!Array.isArray(content)) return '';
|
|
18
|
-
return content
|
|
19
|
-
.filter((c: any) => c.type === 'text')
|
|
20
|
-
.map((b: any) => b.text || '')
|
|
21
|
-
.join(' ');
|
|
22
|
-
}
|
|
13
|
+
import { loadMessage } from '$shared/utils/message-formatter';
|
|
23
14
|
|
|
24
15
|
export const crudHandler = createRouter()
|
|
25
16
|
// List messages
|
|
@@ -33,58 +24,13 @@ export const crudHandler = createRouter()
|
|
|
33
24
|
if (data.include_all) {
|
|
34
25
|
// Return all messages including those in other branches (for History view)
|
|
35
26
|
const allMessages = messageQueries.getAllBySessionId(data.session_id);
|
|
36
|
-
|
|
37
|
-
return messages;
|
|
27
|
+
return allMessages.map(msg => loadMessage(msg));
|
|
38
28
|
} else {
|
|
39
29
|
// Default: return only messages in current HEAD path (for Chat view)
|
|
40
|
-
|
|
41
|
-
return messages;
|
|
30
|
+
return messageQueries.getBySessionId(data.session_id);
|
|
42
31
|
}
|
|
43
32
|
})
|
|
44
33
|
|
|
45
|
-
// Bulk session preview — returns title, summary, and message counts for multiple sessions
|
|
46
|
-
// without loading all messages. Used by the Sessions/History modal.
|
|
47
|
-
.http('sessions:preview', {
|
|
48
|
-
data: t.Object({
|
|
49
|
-
session_ids: t.Array(t.String())
|
|
50
|
-
}),
|
|
51
|
-
response: t.Array(t.Any())
|
|
52
|
-
}, ({ data }) => {
|
|
53
|
-
return data.session_ids.map(sessionId => {
|
|
54
|
-
const preview = messageQueries.getSessionPreview(sessionId);
|
|
55
|
-
|
|
56
|
-
// Title from first user message
|
|
57
|
-
let title = 'New Conversation';
|
|
58
|
-
if (preview.firstUserMessage) {
|
|
59
|
-
const sdk = JSON.parse(preview.firstUserMessage.sdk_message);
|
|
60
|
-
const textContent = extractTextContent(sdk.message?.content).trim();
|
|
61
|
-
if (textContent) {
|
|
62
|
-
title = textContent.slice(0, 60) + (textContent.length > 60 ? '...' : '');
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// Summary from last assistant message
|
|
67
|
-
let summary = 'No messages yet';
|
|
68
|
-
if (preview.lastAssistantMessage) {
|
|
69
|
-
const sdk = JSON.parse(preview.lastAssistantMessage.sdk_message);
|
|
70
|
-
const rawText = extractTextContent(sdk.message?.content);
|
|
71
|
-
const cleanText = rawText.replace(/```[\s\S]*?```/g, '').trim();
|
|
72
|
-
if (cleanText) {
|
|
73
|
-
summary = cleanText.slice(0, 100) + (cleanText.length > 100 ? '...' : '');
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return {
|
|
78
|
-
session_id: sessionId,
|
|
79
|
-
title,
|
|
80
|
-
summary,
|
|
81
|
-
userCount: preview.userCount,
|
|
82
|
-
assistantCount: preview.assistantCount,
|
|
83
|
-
count: preview.userCount + preview.assistantCount
|
|
84
|
-
};
|
|
85
|
-
});
|
|
86
|
-
})
|
|
87
|
-
|
|
88
34
|
// Get message by ID
|
|
89
35
|
.http('messages:get', {
|
|
90
36
|
data: t.Object({
|
|
@@ -98,8 +44,7 @@ export const crudHandler = createRouter()
|
|
|
98
44
|
throw new Error('Message not found');
|
|
99
45
|
}
|
|
100
46
|
|
|
101
|
-
|
|
102
|
-
return formatDatabaseMessage(message);
|
|
47
|
+
return loadMessage(message);
|
|
103
48
|
})
|
|
104
49
|
|
|
105
50
|
// Delete message
|