codexmate 0.0.23 → 0.0.25

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 (73) hide show
  1. package/README.md +32 -9
  2. package/README.zh.md +33 -9
  3. package/cli/auth-profiles.js +23 -7
  4. package/cli/builtin-proxy.js +35 -0
  5. package/cli/claude-proxy.js +24 -0
  6. package/cli/doctor-core.js +903 -0
  7. package/cli/import-skills-url.js +356 -0
  8. package/cli/openai-bridge.js +51 -4
  9. package/cli/session-usage.js +8 -2
  10. package/cli.js +1921 -399
  11. package/lib/automation.js +404 -0
  12. package/lib/cli-models-utils.js +0 -40
  13. package/lib/cli-network-utils.js +28 -2
  14. package/lib/cli-path-utils.js +21 -5
  15. package/lib/cli-sessions.js +32 -1
  16. package/lib/download-artifacts.js +17 -2
  17. package/lib/mcp-stdio.js +13 -0
  18. package/package.json +3 -3
  19. package/plugins/README.md +20 -0
  20. package/plugins/README.zh-CN.md +20 -0
  21. package/plugins/prompt-templates/comment-polish/index.mjs +25 -0
  22. package/plugins/prompt-templates/computed.mjs +253 -0
  23. package/plugins/prompt-templates/index.mjs +8 -0
  24. package/plugins/prompt-templates/manifest.mjs +15 -0
  25. package/plugins/prompt-templates/methods.mjs +619 -0
  26. package/plugins/prompt-templates/overview.mjs +90 -0
  27. package/plugins/prompt-templates/ownership.mjs +19 -0
  28. package/plugins/prompt-templates/rule-ack/index.mjs +21 -0
  29. package/plugins/prompt-templates/storage.mjs +64 -0
  30. package/plugins/registry.mjs +16 -0
  31. package/web-ui/app.js +21 -35
  32. package/web-ui/index.html +4 -3
  33. package/web-ui/logic.sessions.mjs +2 -2
  34. package/web-ui/modules/app.computed.dashboard.mjs +24 -22
  35. package/web-ui/modules/app.computed.main-tabs.mjs +3 -0
  36. package/web-ui/modules/app.computed.session.mjs +17 -0
  37. package/web-ui/modules/app.methods.agents.mjs +91 -3
  38. package/web-ui/modules/app.methods.codex-config.mjs +153 -164
  39. package/web-ui/modules/app.methods.install.mjs +28 -0
  40. package/web-ui/modules/app.methods.navigation.mjs +34 -1
  41. package/web-ui/modules/app.methods.runtime.mjs +24 -2
  42. package/web-ui/modules/app.methods.session-actions.mjs +8 -1
  43. package/web-ui/modules/app.methods.session-browser.mjs +37 -6
  44. package/web-ui/modules/app.methods.session-trash.mjs +4 -2
  45. package/web-ui/modules/config-mode.computed.mjs +1 -3
  46. package/web-ui/modules/i18n.dict.mjs +2055 -0
  47. package/web-ui/modules/i18n.mjs +2 -1769
  48. package/web-ui/partials/index/layout-header.html +48 -34
  49. package/web-ui/partials/index/modal-config-template-agents.html +3 -4
  50. package/web-ui/partials/index/modal-health-check.html +33 -60
  51. package/web-ui/partials/index/panel-config-claude.html +35 -15
  52. package/web-ui/partials/index/panel-config-codex.html +47 -19
  53. package/web-ui/partials/index/panel-config-openclaw.html +8 -3
  54. package/web-ui/partials/index/panel-dashboard.html +186 -0
  55. package/web-ui/partials/index/panel-docs.html +1 -1
  56. package/web-ui/partials/index/panel-market.html +3 -0
  57. package/web-ui/partials/index/panel-orchestration.html +3 -0
  58. package/web-ui/partials/index/panel-plugins.html +16 -10
  59. package/web-ui/partials/index/panel-sessions.html +8 -3
  60. package/web-ui/partials/index/panel-settings.html +1 -1
  61. package/web-ui/partials/index/panel-usage.html +9 -1
  62. package/web-ui/res/logo-pack.webp +0 -0
  63. package/web-ui/styles/controls-forms.css +58 -4
  64. package/web-ui/styles/dashboard.css +274 -0
  65. package/web-ui/styles/layout-shell.css +3 -2
  66. package/web-ui/styles/responsive.css +0 -2
  67. package/web-ui/styles/sessions-list.css +5 -7
  68. package/web-ui/styles/sessions-toolbar-trash.css +4 -4
  69. package/web-ui/styles/sessions-usage.css +33 -0
  70. package/web-ui/styles.css +1 -0
  71. package/res/logo.png +0 -0
  72. /package/{res → web-ui/res}/json5.min.js +0 -0
  73. /package/{res → web-ui/res}/vue.global.prod.js +0 -0
@@ -0,0 +1,619 @@
1
+ import {
2
+ persistPromptTemplatesToStorage,
3
+ persistPromptTemplateSelectedIdToStorage
4
+ } from './storage.mjs';
5
+ import {
6
+ getFirstPluginId,
7
+ getPluginEntry
8
+ } from '../registry.mjs';
9
+
10
+ const COMPOSER_VALUES_STORAGE_KEY = 'codexmate.plugins.promptTemplates.composerValues.v1';
11
+
12
+ function readComposerValuesFromStorage(storage = localStorage) {
13
+ if (!storage) return {};
14
+ let raw = '';
15
+ try {
16
+ raw = storage.getItem(COMPOSER_VALUES_STORAGE_KEY) || '';
17
+ } catch (_) {
18
+ raw = '';
19
+ }
20
+ if (!raw) return {};
21
+ try {
22
+ const parsed = JSON.parse(raw);
23
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return {};
24
+ return parsed;
25
+ } catch (_) {
26
+ return {};
27
+ }
28
+ }
29
+
30
+ function persistComposerValuesToStorage(map, storage = localStorage) {
31
+ if (!storage) return false;
32
+ try {
33
+ storage.setItem(COMPOSER_VALUES_STORAGE_KEY, JSON.stringify(map && typeof map === 'object' && !Array.isArray(map) ? map : {}));
34
+ return true;
35
+ } catch (_) {
36
+ return false;
37
+ }
38
+ }
39
+
40
+ function readComposerValuesForTemplate(templateId) {
41
+ const id = typeof templateId === 'string' ? templateId.trim() : '';
42
+ if (!id) return {};
43
+ const map = readComposerValuesFromStorage(localStorage);
44
+ const values = map && typeof map === 'object' ? map[id] : null;
45
+ return values && typeof values === 'object' && !Array.isArray(values) ? values : {};
46
+ }
47
+
48
+ function persistComposerValuesForTemplate(templateId, values) {
49
+ const id = typeof templateId === 'string' ? templateId.trim() : '';
50
+ if (!id) return false;
51
+ const map = readComposerValuesFromStorage(localStorage);
52
+ const next = map && typeof map === 'object' && !Array.isArray(map) ? { ...map } : {};
53
+ const payload = values && typeof values === 'object' && !Array.isArray(values) ? values : {};
54
+ next[id] = payload;
55
+ return persistComposerValuesToStorage(next, localStorage);
56
+ }
57
+
58
+ function createId(prefix = 'tpl') {
59
+ const rand = Math.random().toString(16).slice(2, 10);
60
+ return `${prefix}_${Date.now().toString(16)}_${rand}`;
61
+ }
62
+
63
+ function nowIso() {
64
+ return new Date().toISOString();
65
+ }
66
+
67
+ function normalizePromptTemplateDraft(draft) {
68
+ const safe = draft && typeof draft === 'object' ? draft : {};
69
+ return {
70
+ id: typeof safe.id === 'string' ? safe.id : '',
71
+ name: typeof safe.name === 'string' ? safe.name : '',
72
+ description: typeof safe.description === 'string' ? safe.description : '',
73
+ template: typeof safe.template === 'string' ? safe.template : '',
74
+ createdAt: typeof safe.createdAt === 'string' ? safe.createdAt : '',
75
+ updatedAt: typeof safe.updatedAt === 'string' ? safe.updatedAt : '',
76
+ isBuiltin: safe.isBuiltin === true,
77
+ createdBy: typeof safe.createdBy === 'string' ? safe.createdBy : '',
78
+ maintainers: Array.isArray(safe.maintainers) ? safe.maintainers : []
79
+ };
80
+ }
81
+
82
+ export function createPluginsMethods() {
83
+ return {
84
+ resetPromptComposerVarValues() {
85
+ this.promptComposerVarValuesRaw = {};
86
+ persistComposerValuesForTemplate(this.promptComposerSelectedTemplateId, {});
87
+ if (typeof this.$nextTick === 'function') {
88
+ this.$nextTick(() => {
89
+ const first = this.$refs && this.$refs.promptComposerFirstField
90
+ ? this.$refs.promptComposerFirstField
91
+ : null;
92
+ if (first && typeof first.focus === 'function') first.focus();
93
+ });
94
+ }
95
+ },
96
+
97
+ focusPromptComposerFirstMissingVar() {
98
+ const run = () => {
99
+ const input = document.querySelector('#panel-plugins .prompt-var-input.is-missing');
100
+ if (!input || typeof input.focus !== 'function') return;
101
+ try {
102
+ if (typeof input.scrollIntoView === 'function') {
103
+ input.scrollIntoView({ block: 'center', inline: 'nearest' });
104
+ }
105
+ } catch (_) {}
106
+ input.focus();
107
+ };
108
+ if (typeof this.$nextTick === 'function') {
109
+ this.$nextTick(run);
110
+ return;
111
+ }
112
+ run();
113
+ },
114
+
115
+ selectPromptComposerTemplate(id) {
116
+ const next = typeof id === 'string' ? id.trim() : '';
117
+ if (!next) return;
118
+ if (next === this.promptComposerSelectedTemplateId) return;
119
+ this.promptComposerSelectedTemplateId = next;
120
+ persistPromptTemplateSelectedIdToStorage(next, localStorage);
121
+ this.promptComposerVarValuesRaw = readComposerValuesForTemplate(next);
122
+ if (typeof this.$nextTick === 'function') {
123
+ this.$nextTick(() => {
124
+ const first = this.$refs && this.$refs.promptComposerFirstField
125
+ ? this.$refs.promptComposerFirstField
126
+ : null;
127
+ if (first && typeof first.focus === 'function') first.focus();
128
+ });
129
+ }
130
+ },
131
+
132
+ onPromptComposerInput() {
133
+ const raw = typeof this.promptComposerCommand === 'string' ? this.promptComposerCommand : '';
134
+ const text = raw.trimStart();
135
+ if (!text.startsWith('/')) {
136
+ if (this.promptComposerPickerVisible) {
137
+ this.promptComposerPickerVisible = false;
138
+ }
139
+ return;
140
+ }
141
+
142
+ const lower = text.toLowerCase();
143
+ const isPluginCommand = lower.startsWith('/pl') || lower.startsWith('/plugin') || lower.startsWith('/plugins');
144
+ if (!isPluginCommand) return;
145
+
146
+ const after = text.replace(/^\/plugins?\b/i, '').trim();
147
+ this.promptComposerPickerKeyword = after;
148
+ if (!this.promptComposerPickerVisible) {
149
+ this.openPromptComposerPicker({ keepKeyword: true });
150
+ }
151
+ },
152
+
153
+ onPromptComposerKeydown(event) {
154
+ const e = event || null;
155
+ if (!e || e.key !== 'Enter') return;
156
+ if (e.shiftKey) return;
157
+ e.preventDefault();
158
+
159
+ if (this.promptComposerPickerVisible) {
160
+ const list = Array.isArray(this.promptComposerPickerList) ? this.promptComposerPickerList : [];
161
+ if (list.length) {
162
+ this.usePromptTemplateInComposer(list[0].id);
163
+ }
164
+ return;
165
+ }
166
+
167
+ const value = typeof this.promptComposerCommand === 'string' ? this.promptComposerCommand.trim() : '';
168
+ const lower = value.toLowerCase();
169
+ if (lower === '/plugin' || lower === '/plugins' || lower === '/pl') {
170
+ this.openPromptComposerPicker();
171
+ return;
172
+ }
173
+ },
174
+
175
+ onPromptComposerPickerKeydown(event) {
176
+ const e = event || null;
177
+ if (!e) return;
178
+ if (e.key === 'Enter' && !e.shiftKey) {
179
+ e.preventDefault();
180
+ const list = Array.isArray(this.promptComposerPickerList) ? this.promptComposerPickerList : [];
181
+ if (list.length) {
182
+ this.usePromptTemplateInComposer(list[0].id);
183
+ }
184
+ return;
185
+ }
186
+ if (e.key === 'Escape') {
187
+ e.preventDefault();
188
+ this.closePromptComposerPicker();
189
+ }
190
+ },
191
+
192
+ openPromptComposerPicker(options = {}) {
193
+ this.promptComposerPickerVisible = true;
194
+ if (!(options && options.keepKeyword)) {
195
+ this.promptComposerPickerKeyword = '';
196
+ }
197
+ if (typeof this.$nextTick === 'function') {
198
+ this.$nextTick(() => {
199
+ const input = this.$refs && this.$refs.promptComposerPickerSearch
200
+ ? this.$refs.promptComposerPickerSearch
201
+ : null;
202
+ if (input && typeof input.focus === 'function') input.focus();
203
+ });
204
+ }
205
+ },
206
+
207
+ closePromptComposerPicker() {
208
+ this.promptComposerPickerVisible = false;
209
+ if (typeof this.$nextTick === 'function') {
210
+ this.$nextTick(() => {
211
+ const input = this.$refs && this.$refs.promptComposerCommandInput
212
+ ? this.$refs.promptComposerCommandInput
213
+ : null;
214
+ if (input && typeof input.focus === 'function') input.focus();
215
+ });
216
+ }
217
+ },
218
+
219
+ usePromptTemplateInComposer(id) {
220
+ const next = typeof id === 'string' ? id.trim() : '';
221
+ if (!next) return;
222
+ this.promptComposerSelectedTemplateId = next;
223
+ persistPromptTemplateSelectedIdToStorage(next, localStorage);
224
+ this.promptComposerVarValuesRaw = readComposerValuesForTemplate(next);
225
+ this.promptComposerCommand = '';
226
+ this.promptComposerPickerVisible = false;
227
+ this.promptTemplatesMode = 'compose';
228
+ if (typeof this.$nextTick === 'function') {
229
+ this.$nextTick(() => {
230
+ const firstVar = this.$refs && this.$refs.promptComposerFirstField
231
+ ? this.$refs.promptComposerFirstField
232
+ : null;
233
+ if (firstVar && typeof firstVar.focus === 'function') firstVar.focus();
234
+ });
235
+ }
236
+ },
237
+
238
+ resetPromptComposer() {
239
+ this.promptComposerCommand = '';
240
+ this.promptComposerSelectedTemplateId = '';
241
+ persistPromptTemplateSelectedIdToStorage('', localStorage);
242
+ this.promptComposerVarValuesRaw = {};
243
+ this.promptComposerPickerVisible = false;
244
+ this.promptComposerPickerKeyword = '';
245
+ },
246
+
247
+ setPromptComposerVarValue(name, value) {
248
+ const key = typeof name === 'string' ? name.trim() : '';
249
+ if (!key) return;
250
+ const current = this.promptComposerVarValuesRaw && typeof this.promptComposerVarValuesRaw === 'object'
251
+ ? this.promptComposerVarValuesRaw
252
+ : {};
253
+ const next = { ...current };
254
+ next[key] = value == null ? '' : String(value);
255
+ this.promptComposerVarValuesRaw = next;
256
+ persistComposerValuesForTemplate(this.promptComposerSelectedTemplateId, next);
257
+ },
258
+
259
+ async copyPromptComposerRendered() {
260
+ const text = typeof this.promptComposerRendered === 'string' ? this.promptComposerRendered.trim() : '';
261
+ if (!text) {
262
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.copy.empty') : 'Nothing to copy', 'info');
263
+ return;
264
+ }
265
+ try {
266
+ if (navigator.clipboard && window.isSecureContext) {
267
+ await navigator.clipboard.writeText(text);
268
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.copy.ok') : 'Copied', 'success');
269
+ return;
270
+ }
271
+ } catch (_) {}
272
+ const ok = typeof this.fallbackCopyText === 'function' ? this.fallbackCopyText(text) : false;
273
+ if (ok) {
274
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.copy.ok') : 'Copied', 'success');
275
+ return;
276
+ }
277
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.copy.fail') : 'Copy failed', 'error');
278
+ },
279
+
280
+ selectPlugin(pluginId) {
281
+ const id = typeof pluginId === 'string' ? pluginId.trim() : '';
282
+ if (!id) return;
283
+ if (!getPluginEntry(id)) return;
284
+ this.pluginsActiveId = id;
285
+ },
286
+
287
+ async loadPluginsOverview(options = {}) {
288
+ const silent = !!(options && options.silent);
289
+ const forceRefresh = !!(options && options.forceRefresh);
290
+ if (this.pluginsLoading) return false;
291
+
292
+ this.pluginsLoading = true;
293
+ this.pluginsError = '';
294
+ try {
295
+ const fallbackId = getFirstPluginId();
296
+ const currentId = typeof this.pluginsActiveId === 'string' ? this.pluginsActiveId.trim() : '';
297
+ const resolved = getPluginEntry(currentId) ? currentId : fallbackId;
298
+ if (resolved && resolved !== currentId) {
299
+ this.pluginsActiveId = resolved;
300
+ }
301
+
302
+ const entry = getPluginEntry(resolved);
303
+ if (!entry || typeof entry.loadOverview !== 'function') return true;
304
+ return await entry.loadOverview(this, { silent, forceRefresh });
305
+ } catch (e) {
306
+ this.pluginsError = e && e.message ? String(e.message) : 'Failed to load plugins';
307
+ if (!silent) {
308
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.plugins.loadFail') : 'Failed to load plugins', 'error');
309
+ }
310
+ return false;
311
+ } finally {
312
+ this.pluginsLoading = false;
313
+ }
314
+ },
315
+
316
+ selectPromptTemplate(id) {
317
+ const next = typeof id === 'string' ? id.trim() : '';
318
+ if (!next) return;
319
+ const list = this.promptTemplatesList;
320
+ const entry = list.find((item) => item.id === next);
321
+ if (!entry) return;
322
+ this.promptTemplateSelectedId = next;
323
+ this.promptTemplatesMode = 'manage';
324
+ this.promptTemplateDraftRaw = {
325
+ id: entry.id,
326
+ name: entry.name,
327
+ description: entry.description,
328
+ template: entry.template,
329
+ createdAt: entry.createdAt,
330
+ updatedAt: entry.updatedAt,
331
+ isBuiltin: entry.isBuiltin === true,
332
+ createdBy: entry.createdBy || '',
333
+ maintainers: Array.isArray(entry.maintainers) ? entry.maintainers : []
334
+ };
335
+ this.promptTemplateVarValuesRaw = {};
336
+ },
337
+
338
+ createPromptTemplate() {
339
+ const id = createId('prompt');
340
+ const name = typeof this.t === 'function'
341
+ ? this.t('plugins.promptTemplates.manage.newTemplateName')
342
+ : 'New template';
343
+ const draft = {
344
+ id,
345
+ name,
346
+ description: '',
347
+ template: '',
348
+ createdAt: nowIso(),
349
+ updatedAt: nowIso(),
350
+ isBuiltin: false
351
+ };
352
+ this.promptTemplateDraftRaw = draft;
353
+ this.promptTemplateSelectedId = id;
354
+ this.promptTemplateVarValuesRaw = {};
355
+ },
356
+
357
+ resetPromptVariableValues() {
358
+ this.promptTemplateVarValuesRaw = {};
359
+ },
360
+
361
+ addPromptTemplateVariable() {
362
+ const draft = normalizePromptTemplateDraft(this.promptTemplateDraftRaw);
363
+ if (!draft || !draft.id) return;
364
+ if (draft.isBuiltin) {
365
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.templates.builtinNotEditable') : 'Built-in templates are not editable', 'error');
366
+ return;
367
+ }
368
+ this.promptTemplateVarDraftName = 'var';
369
+ this.promptTemplateVarDraftError = '';
370
+ this.showPromptTemplateVarModal = true;
371
+ if (typeof this.$nextTick === 'function') {
372
+ this.$nextTick(() => {
373
+ const input = this.$refs && this.$refs.promptTemplateVarNameInput
374
+ ? this.$refs.promptTemplateVarNameInput
375
+ : null;
376
+ if (input && typeof input.focus === 'function') input.focus();
377
+ });
378
+ }
379
+ },
380
+
381
+ closePromptTemplateVarModal() {
382
+ this.showPromptTemplateVarModal = false;
383
+ this.promptTemplateVarDraftError = '';
384
+ },
385
+
386
+ confirmAddPromptTemplateVariable() {
387
+ const draft = normalizePromptTemplateDraft(this.promptTemplateDraftRaw);
388
+ if (!draft || !draft.id) return;
389
+ if (draft.isBuiltin) {
390
+ this.promptTemplateVarDraftError = typeof this.t === 'function'
391
+ ? this.t('toast.templates.builtinNotEditable')
392
+ : 'Built-in templates are not editable';
393
+ return;
394
+ }
395
+ const key = typeof this.promptTemplateVarDraftName === 'string'
396
+ ? this.promptTemplateVarDraftName.trim()
397
+ : '';
398
+ if (!key) {
399
+ this.promptTemplateVarDraftError = typeof this.t === 'function'
400
+ ? this.t('toast.templates.varNameRequired')
401
+ : 'Variable name is required';
402
+ return;
403
+ }
404
+ if (!/^[a-zA-Z0-9_.-]+$/.test(key)) {
405
+ this.promptTemplateVarDraftError = typeof this.t === 'function'
406
+ ? this.t('toast.templates.varNameInvalid')
407
+ : 'Variable name may only contain letters, numbers, underscore, dash, dot';
408
+ return;
409
+ }
410
+ const placeholder = `{{${key}}}`;
411
+ const current = typeof draft.template === 'string' ? draft.template : '';
412
+ if (current.includes(placeholder)) {
413
+ this.promptTemplateVarDraftError = typeof this.t === 'function'
414
+ ? this.t('toast.templates.varExists')
415
+ : 'Variable already exists';
416
+ return;
417
+ }
418
+ const nextText = current && !current.endsWith('\n')
419
+ ? `${current}\n${placeholder}\n`
420
+ : `${current}${placeholder}\n`;
421
+ this.promptTemplateDraftRaw = { ...draft, template: nextText };
422
+ this.showPromptTemplateVarModal = false;
423
+ this.promptTemplateVarDraftError = '';
424
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.templates.varAdded') : 'Variable added', 'success');
425
+ },
426
+
427
+ setPromptVariableValue(name, value) {
428
+ const key = typeof name === 'string' ? name.trim() : '';
429
+ if (!key) return;
430
+ const next = { ...(this.promptTemplateVarValuesRaw && typeof this.promptTemplateVarValuesRaw === 'object' ? this.promptTemplateVarValuesRaw : {}) };
431
+ next[key] = value == null ? '' : String(value);
432
+ this.promptTemplateVarValuesRaw = next;
433
+ },
434
+
435
+ async copyRenderedPrompt() {
436
+ const text = typeof this.renderedPrompt === 'string' ? this.renderedPrompt.trim() : '';
437
+ if (!text) {
438
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.copy.empty') : 'Nothing to copy', 'info');
439
+ return;
440
+ }
441
+ try {
442
+ if (navigator.clipboard && window.isSecureContext) {
443
+ await navigator.clipboard.writeText(text);
444
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.copy.ok') : 'Copied', 'success');
445
+ return;
446
+ }
447
+ } catch (_) {}
448
+ const ok = typeof this.fallbackCopyText === 'function' ? this.fallbackCopyText(text) : false;
449
+ if (ok) {
450
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.copy.ok') : 'Copied', 'success');
451
+ return;
452
+ }
453
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.copy.fail') : 'Copy failed', 'error');
454
+ },
455
+
456
+ async savePromptTemplate() {
457
+ const draft = normalizePromptTemplateDraft(this.promptTemplateDraftRaw);
458
+ if (draft.isBuiltin) {
459
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.templates.builtinNotModifiable') : 'Built-in templates are read-only. Duplicate first.', 'error');
460
+ return false;
461
+ }
462
+ const name = draft.name.trim();
463
+ if (!name) {
464
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.templates.nameRequired') : 'Template name is required', 'error');
465
+ return false;
466
+ }
467
+ const nextId = draft.id ? draft.id : createId('prompt');
468
+ const list = Array.isArray(this.promptTemplatesListRaw) ? [...this.promptTemplatesListRaw] : [];
469
+ const now = nowIso();
470
+ const entry = {
471
+ ...draft,
472
+ id: nextId,
473
+ name,
474
+ updatedAt: now,
475
+ createdAt: draft.createdAt || now,
476
+ isBuiltin: draft.isBuiltin === true
477
+ };
478
+ const index = list.findIndex((item) => item && item.id === nextId);
479
+ if (index >= 0) {
480
+ list[index] = entry;
481
+ } else {
482
+ list.unshift(entry);
483
+ }
484
+ this.promptTemplatesListRaw = list;
485
+ persistPromptTemplatesToStorage(list, localStorage);
486
+ this.promptTemplateDraftRaw = entry;
487
+ this.promptTemplateSelectedId = nextId;
488
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.save.ok') : 'Saved', 'success');
489
+ return true;
490
+ },
491
+
492
+ duplicatePromptTemplate() {
493
+ const draft = normalizePromptTemplateDraft(this.promptTemplateDraftRaw);
494
+ if (!draft.id) return;
495
+ if (draft.isBuiltin) {
496
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.templates.builtinNotDuplicable') : 'Built-in templates cannot be duplicated', 'error');
497
+ return;
498
+ }
499
+ const nextId = createId('prompt');
500
+ this.promptTemplateDraftRaw = {
501
+ ...draft,
502
+ id: nextId,
503
+ name: `${draft.name || 'Template'} (copy)`,
504
+ createdAt: nowIso(),
505
+ updatedAt: nowIso(),
506
+ isBuiltin: false
507
+ };
508
+ this.promptTemplateSelectedId = nextId;
509
+ this.promptTemplateVarValuesRaw = {};
510
+ },
511
+
512
+ async deletePromptTemplate() {
513
+ const draft = normalizePromptTemplateDraft(this.promptTemplateDraftRaw);
514
+ if (!draft.id) return;
515
+ if (draft.isBuiltin) {
516
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.templates.builtinNotDeletable') : 'Built-in templates cannot be deleted', 'error');
517
+ return;
518
+ }
519
+ const t = typeof this.t === 'function' ? this.t : null;
520
+ const confirmed = await this.requestConfirmDialog({
521
+ title: t ? t('toast.templates.deleteTitle') : 'Delete template',
522
+ message: t ? t('toast.templates.deleteMessage', { name: draft.name || draft.id }) : `Delete “${draft.name || draft.id}”? This action cannot be undone.`,
523
+ confirmText: t ? t('toast.templates.deleteConfirm') : 'Delete',
524
+ cancelText: t ? t('toast.templates.deleteCancel') : 'Cancel',
525
+ danger: true
526
+ });
527
+ if (!confirmed) return;
528
+
529
+ const list = Array.isArray(this.promptTemplatesListRaw) ? this.promptTemplatesListRaw : [];
530
+ const next = list.filter((item) => !(item && item.id === draft.id));
531
+ this.promptTemplatesListRaw = next;
532
+ persistPromptTemplatesToStorage(next, localStorage);
533
+ this.promptTemplateDraftRaw = null;
534
+ this.promptTemplateSelectedId = '';
535
+ const first = this.promptTemplatesList && this.promptTemplatesList.length ? this.promptTemplatesList[0] : null;
536
+ if (first) this.selectPromptTemplate(first.id);
537
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.delete.ok') : 'Deleted', 'success');
538
+ },
539
+
540
+ exportPromptTemplates() {
541
+ const list = this.promptTemplatesList;
542
+ if (!Array.isArray(list) || !list.length) {
543
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.export.empty') : 'Nothing to export', 'info');
544
+ return;
545
+ }
546
+ const payload = JSON.stringify(list.map((item) => ({
547
+ id: item.id,
548
+ name: item.name,
549
+ description: item.description,
550
+ template: item.template,
551
+ createdAt: item.createdAt,
552
+ updatedAt: item.updatedAt,
553
+ isBuiltin: item.isBuiltin
554
+ })), null, 2);
555
+ if (typeof this.downloadTextFile === 'function') {
556
+ this.downloadTextFile(`prompt-templates-${Date.now()}.json`, payload, 'application/json;charset=utf-8');
557
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.export.ok') : 'Exported', 'success');
558
+ return;
559
+ }
560
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.export.notSupported') : 'Export not supported', 'error');
561
+ },
562
+
563
+ triggerPromptTemplatesImport() {
564
+ const input = this.$refs && this.$refs.promptTemplatesImportInput
565
+ ? this.$refs.promptTemplatesImportInput
566
+ : null;
567
+ if (!input) {
568
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.import.notAvailable') : 'Import is not available', 'error');
569
+ return;
570
+ }
571
+ input.value = '';
572
+ input.click();
573
+ },
574
+
575
+ async handlePromptTemplatesImportChange(event) {
576
+ const input = event && event.target ? event.target : null;
577
+ const file = input && input.files && input.files[0] ? input.files[0] : null;
578
+ if (!file) return;
579
+ let text = '';
580
+ try {
581
+ text = await file.text();
582
+ } catch (_) {
583
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.import.readFileFail') : 'Failed to read file', 'error');
584
+ return;
585
+ }
586
+ let parsed;
587
+ try {
588
+ parsed = JSON.parse(text);
589
+ } catch (_) {
590
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.import.invalidJson') : 'Invalid JSON', 'error');
591
+ return;
592
+ }
593
+ if (!Array.isArray(parsed)) {
594
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.import.expectedArray') : 'Expected an array', 'error');
595
+ return;
596
+ }
597
+ const list = Array.isArray(this.promptTemplatesListRaw) ? [...this.promptTemplatesListRaw] : [];
598
+ for (const item of parsed) {
599
+ const draft = normalizePromptTemplateDraft(item);
600
+ if (!draft.name || !draft.template) continue;
601
+ const id = draft.id ? draft.id : createId('prompt');
602
+ const now = nowIso();
603
+ const entry = {
604
+ ...draft,
605
+ id,
606
+ createdAt: draft.createdAt || now,
607
+ updatedAt: now,
608
+ isBuiltin: false
609
+ };
610
+ const index = list.findIndex((existing) => existing && existing.id === id);
611
+ if (index >= 0) list[index] = entry;
612
+ else list.unshift(entry);
613
+ }
614
+ this.promptTemplatesListRaw = list;
615
+ persistPromptTemplatesToStorage(list, localStorage);
616
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.import.ok') : 'Imported', 'success');
617
+ }
618
+ };
619
+ }