forked from lightningpixel/modly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextensionsStore.ts
More file actions
246 lines (216 loc) · 9.4 KB
/
Copy pathextensionsStore.ts
File metadata and controls
246 lines (216 loc) · 9.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { create } from 'zustand'
import type { ModelExtension, ProcessExtension, AnyExtension } from '@shared/types/electron.d'
// ─── Re-exports for consumers ─────────────────────────────────────────────────
export type { ModelExtension, ProcessExtension, AnyExtension }
export type InstallStep = 'downloading' | 'extracting' | 'validating' | 'setting_up' | 'done' | 'error'
export interface InstallProgress {
step: InstallStep
percent?: number
extensionId?: string
message?: string
}
// ─── Store ────────────────────────────────────────────────────────────────────
type InstallResult = {
success: boolean
error?: string
extension?: AnyExtension
extensionId?: string
needsRepair?: boolean
cancelled?: boolean
partialResults?: Array<{
success: boolean
error?: string
extension?: AnyExtension
extensionId?: string
}>
}
interface ExtensionsStore {
modelExtensions: ModelExtension[]
processExtensions: ProcessExtension[]
loading: boolean
installProgress: InstallProgress | null
installError: string | null
loadErrors: Record<string, string>
loadExtensions: () => Promise<void>
installFromGitHub: (url: string) => Promise<InstallResult>
installFromLocal: () => Promise<InstallResult>
uninstall: (extensionId: string) => Promise<{ success: boolean; error?: string }>
reload: () => Promise<void>
clearInstallState: () => void
}
export function partitionExtensionsByType(list: AnyExtension[]): {
modelExtensions: ModelExtension[]
processExtensions: ProcessExtension[]
} {
return {
modelExtensions: list.filter((extension): extension is ModelExtension =>
extension.type === 'model',
),
processExtensions: list.filter((extension): extension is ProcessExtension =>
extension.type === 'process',
),
}
}
export const useExtensionsStore = create<ExtensionsStore>((set, get) => ({
modelExtensions: [],
processExtensions: [],
loading: false,
installProgress: null,
installError: null,
loadErrors: {},
// ── Load list ──────────────────────────────────────────────────────────────
async loadExtensions() {
set({ loading: true })
try {
const list = (await window.electron.extensions.list()) as AnyExtension[]
const extensions = partitionExtensionsByType(list)
set({
...extensions,
loading: false,
})
} catch {
set({ loading: false })
}
},
// ── Install from GitHub ────────────────────────────────────────────────────
async installFromGitHub(url: string) {
return installExtension(() => window.electron.extensions.installFromGitHub(url), set)
},
// ── Install from local folder ──────────────────────────────────────────────
async installFromLocal() {
const result = await installExtension(() => window.electron.extensions.installFromLocal(), set)
// If user cancelled the folder picker, treat as a no-op (not an error)
if ((result as any).cancelled) {
set({ installProgress: null, installError: null })
return { success: false, cancelled: true }
}
return result
},
// ── Uninstall ──────────────────────────────────────────────────────────────
async uninstall(extensionId: string) {
const result = await window.electron.extensions.uninstall(extensionId)
if (result.success) {
set((state) => ({
modelExtensions: state.modelExtensions.filter((e) => e.id !== extensionId),
processExtensions: state.processExtensions.filter((e) => e.id !== extensionId),
}))
}
return result
},
// ── Reload (rescan extensions dir + Python registry) ──────────────────────
async reload() {
const result = await window.electron.extensions.reload()
if (result.success) {
set({ loadErrors: result.errors ?? {} })
}
await get().loadExtensions()
},
// ── Helpers ────────────────────────────────────────────────────────────────
clearInstallState() {
set({ installProgress: null, installError: null })
},
}))
async function installExtension(
invoke: () => Promise<InstallResult>,
set: (partial: Partial<ExtensionsStore> | ((state: ExtensionsStore) => Partial<ExtensionsStore>)) => void,
) {
set({ installProgress: { step: 'downloading', percent: 0 }, installError: null })
window.electron.extensions.onInstallProgress((data) => {
if (data.step === 'error') {
set({ installProgress: null, installError: data.message ?? 'Unknown error' })
} else {
set({ installProgress: data as InstallProgress })
}
})
try {
const result = await invoke()
// Handle bundle install with partial results
if (result.partialResults && result.partialResults.length > 0) {
const successful = result.partialResults.filter(r => r.success && r.extension)
const failed = result.partialResults.filter(r => !r.success)
// Add successful extensions
for (const res of successful) {
const ext = res.extension as AnyExtension
set((state) => {
if (ext.type === 'process') {
const filtered = state.processExtensions.filter((e) => e.id !== ext.id)
return {
processExtensions: [...filtered, ext],
modelExtensions: state.modelExtensions.filter((e) => e.id !== ext.id),
}
} else {
const filtered = state.modelExtensions.filter((e) => e.id !== ext.id)
return {
modelExtensions: [...filtered, ext],
processExtensions: state.processExtensions.filter((e) => e.id !== ext.id),
}
}
})
}
// Report partial success/failure
if (failed.length > 0 && successful.length > 0) {
set({
installProgress: { step: 'done', extensionId: successful.map(s => s.extensionId).join(', ') },
installError: `${failed.length} of ${result.partialResults.length} extensions failed: ${failed.map(f => f.error).join('; ')}`,
})
} else if (failed.length > 0) {
set({
installProgress: null,
installError: `All extensions failed: ${failed.map(f => f.error).join('; ')}`,
})
} else {
set({
installProgress: { step: 'done', extensionId: successful.map(s => s.extensionId).join(', ') },
installError: null,
})
}
return result
}
// Handle single extension (legacy)
if (result.success && result.extension) {
const ext = result.extension as AnyExtension
set((state) => {
if (ext.type === 'process') {
const filtered = state.processExtensions.filter((e) => e.id !== ext.id)
return {
processExtensions: [...filtered, ext],
modelExtensions: state.modelExtensions.filter((e) => e.id !== ext.id),
installProgress: { step: 'done', extensionId: result.extensionId },
installError: null,
}
} else {
const filtered = state.modelExtensions.filter((e) => e.id !== ext.id)
return {
modelExtensions: [...filtered, ext],
processExtensions: state.processExtensions.filter((e) => e.id !== ext.id),
installProgress: { step: 'done', extensionId: result.extensionId },
installError: null,
}
}
})
} else if (result.needsRepair && result.extension) {
const ext = result.extension as AnyExtension
const error = result.error ?? 'Extension setup is incomplete. Click Repair and retry.'
set((state) => ({
modelExtensions: ext.type === 'model'
? [...state.modelExtensions.filter((entry) => entry.id !== ext.id), ext]
: state.modelExtensions.filter((entry) => entry.id !== ext.id),
processExtensions: ext.type === 'process'
? [...state.processExtensions.filter((entry) => entry.id !== ext.id), ext]
: state.processExtensions.filter((entry) => entry.id !== ext.id),
loadErrors: { ...state.loadErrors, [ext.id]: error },
installProgress: null,
installError: error,
}))
} else {
set({ installProgress: null, installError: result.error ?? 'Installation failed' })
}
return result
} catch (err) {
const error = String(err)
set({ installProgress: null, installError: error })
return { success: false, error }
} finally {
window.electron.extensions.offInstallProgress()
}
}