codexmate 0.0.29 → 0.0.30

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 (143) hide show
  1. package/README.md +421 -421
  2. package/README.zh.md +354 -354
  3. package/cli/agents-files.js +224 -224
  4. package/cli/archive-helpers.js +446 -446
  5. package/cli/auth-profiles.js +375 -375
  6. package/cli/builtin-proxy.js +1725 -1725
  7. package/cli/claude-proxy.js +1022 -1022
  8. package/cli/config-bootstrap.js +402 -384
  9. package/cli/config-health.js +454 -338
  10. package/cli/doctor-core.js +903 -903
  11. package/cli/import-skills-url.js +356 -356
  12. package/cli/local-bridge.js +324 -0
  13. package/cli/openai-bridge.js +1653 -1576
  14. package/cli/openclaw-config.js +629 -629
  15. package/cli/session-convert-args.js +65 -65
  16. package/cli/session-convert-io.js +82 -82
  17. package/cli/session-convert.js +43 -43
  18. package/cli/session-usage.concurrent.js +28 -28
  19. package/cli/session-usage.js +118 -118
  20. package/cli/session-usage.models.js +176 -176
  21. package/cli/skills.js +1141 -1141
  22. package/cli/zip-commands.js +510 -510
  23. package/cli.js +15481 -15340
  24. package/lib/automation.js +404 -404
  25. package/lib/cli-file-utils.js +151 -151
  26. package/lib/cli-models-utils.js +440 -440
  27. package/lib/cli-network-utils.js +190 -190
  28. package/lib/cli-path-utils.js +85 -85
  29. package/lib/cli-session-utils.js +121 -121
  30. package/lib/cli-sessions.js +417 -417
  31. package/lib/cli-utils.js +155 -155
  32. package/lib/cli-webhook.js +126 -126
  33. package/lib/download-artifacts.js +92 -92
  34. package/lib/mcp-stdio.js +453 -453
  35. package/lib/task-orchestrator.js +869 -869
  36. package/lib/text-diff.js +303 -303
  37. package/lib/workflow-engine.js +340 -340
  38. package/package.json +76 -76
  39. package/plugins/README.md +20 -20
  40. package/plugins/README.zh-CN.md +20 -20
  41. package/plugins/prompt-templates/comment-polish/index.mjs +25 -25
  42. package/plugins/prompt-templates/computed.mjs +253 -253
  43. package/plugins/prompt-templates/index.mjs +8 -8
  44. package/plugins/prompt-templates/manifest.mjs +15 -15
  45. package/plugins/prompt-templates/methods.mjs +553 -553
  46. package/plugins/prompt-templates/overview.mjs +91 -91
  47. package/plugins/prompt-templates/ownership.mjs +19 -19
  48. package/plugins/prompt-templates/rule-ack/index.mjs +21 -21
  49. package/plugins/prompt-templates/storage.mjs +64 -64
  50. package/plugins/registry.mjs +16 -16
  51. package/web-ui/app.js +645 -639
  52. package/web-ui/index.html +36 -36
  53. package/web-ui/logic.agents-diff.mjs +386 -386
  54. package/web-ui/logic.claude.mjs +168 -168
  55. package/web-ui/logic.codex.mjs +69 -69
  56. package/web-ui/logic.mjs +5 -5
  57. package/web-ui/logic.runtime.mjs +128 -128
  58. package/web-ui/logic.session-convert.mjs +70 -70
  59. package/web-ui/logic.sessions.mjs +765 -765
  60. package/web-ui/modules/api.mjs +90 -90
  61. package/web-ui/modules/app.computed.dashboard.mjs +248 -225
  62. package/web-ui/modules/app.computed.index.mjs +17 -17
  63. package/web-ui/modules/app.computed.main-tabs.mjs +205 -205
  64. package/web-ui/modules/app.computed.session.mjs +999 -999
  65. package/web-ui/modules/app.constants.mjs +15 -15
  66. package/web-ui/modules/app.methods.agents.mjs +632 -632
  67. package/web-ui/modules/app.methods.claude-config.mjs +200 -200
  68. package/web-ui/modules/app.methods.codex-config.mjs +917 -892
  69. package/web-ui/modules/app.methods.index.mjs +94 -94
  70. package/web-ui/modules/app.methods.install.mjs +205 -205
  71. package/web-ui/modules/app.methods.navigation.mjs +774 -761
  72. package/web-ui/modules/app.methods.openclaw-core.mjs +814 -814
  73. package/web-ui/modules/app.methods.openclaw-editing.mjs +372 -372
  74. package/web-ui/modules/app.methods.openclaw-persist.mjs +369 -369
  75. package/web-ui/modules/app.methods.providers.mjs +529 -493
  76. package/web-ui/modules/app.methods.runtime.mjs +345 -345
  77. package/web-ui/modules/app.methods.session-actions.mjs +593 -593
  78. package/web-ui/modules/app.methods.session-browser.mjs +984 -984
  79. package/web-ui/modules/app.methods.session-timeline.mjs +479 -479
  80. package/web-ui/modules/app.methods.session-trash.mjs +438 -438
  81. package/web-ui/modules/app.methods.startup-claude.mjs +534 -533
  82. package/web-ui/modules/app.methods.task-orchestration.mjs +556 -556
  83. package/web-ui/modules/app.methods.webhook.mjs +79 -79
  84. package/web-ui/modules/config-mode.computed.mjs +124 -124
  85. package/web-ui/modules/config-template-confirm-pref.mjs +33 -33
  86. package/web-ui/modules/i18n.dict.mjs +3146 -3131
  87. package/web-ui/modules/i18n.mjs +62 -62
  88. package/web-ui/modules/plugins.computed.mjs +3 -3
  89. package/web-ui/modules/plugins.methods.mjs +3 -3
  90. package/web-ui/modules/plugins.storage.mjs +11 -11
  91. package/web-ui/modules/provider-url-display.mjs +17 -17
  92. package/web-ui/modules/sessions-filters-url.mjs +85 -85
  93. package/web-ui/modules/skills.computed.mjs +107 -107
  94. package/web-ui/modules/skills.methods.mjs +482 -481
  95. package/web-ui/partials/index/layout-footer.html +13 -13
  96. package/web-ui/partials/index/layout-header.html +500 -500
  97. package/web-ui/partials/index/modal-config-template-agents.html +174 -174
  98. package/web-ui/partials/index/modal-confirm-toast.html +32 -32
  99. package/web-ui/partials/index/modal-health-check.html +45 -45
  100. package/web-ui/partials/index/modal-openclaw-config.html +280 -280
  101. package/web-ui/partials/index/modal-skills.html +200 -200
  102. package/web-ui/partials/index/modals-basic.html +162 -162
  103. package/web-ui/partials/index/panel-config-claude.html +194 -194
  104. package/web-ui/partials/index/panel-config-codex.html +337 -323
  105. package/web-ui/partials/index/panel-config-openclaw.html +83 -83
  106. package/web-ui/partials/index/panel-dashboard.html +219 -186
  107. package/web-ui/partials/index/panel-docs.html +147 -147
  108. package/web-ui/partials/index/panel-market.html +177 -177
  109. package/web-ui/partials/index/panel-orchestration.html +391 -391
  110. package/web-ui/partials/index/panel-plugins.html +253 -253
  111. package/web-ui/partials/index/panel-sessions.html +308 -316
  112. package/web-ui/partials/index/panel-settings.html +190 -190
  113. package/web-ui/partials/index/panel-trash.html +88 -88
  114. package/web-ui/partials/index/panel-usage.html +371 -371
  115. package/web-ui/res/json5.min.js +1 -1
  116. package/web-ui/res/vue.global.prod.js +13 -13
  117. package/web-ui/session-helpers.mjs +594 -576
  118. package/web-ui/source-bundle.cjs +233 -233
  119. package/web-ui/styles/base-theme.css +281 -281
  120. package/web-ui/styles/controls-forms.css +422 -422
  121. package/web-ui/styles/dashboard.css +406 -274
  122. package/web-ui/styles/docs-panel.css +271 -271
  123. package/web-ui/styles/feedback.css +108 -108
  124. package/web-ui/styles/health-check-dialog.css +144 -144
  125. package/web-ui/styles/layout-shell.css +626 -626
  126. package/web-ui/styles/modals-core.css +466 -466
  127. package/web-ui/styles/navigation-panels.css +391 -391
  128. package/web-ui/styles/openclaw-structured.css +266 -266
  129. package/web-ui/styles/plugins-panel.css +564 -523
  130. package/web-ui/styles/responsive.css +454 -454
  131. package/web-ui/styles/sessions-list.css +417 -419
  132. package/web-ui/styles/sessions-preview.css +407 -411
  133. package/web-ui/styles/sessions-toolbar-trash.css +334 -330
  134. package/web-ui/styles/sessions-usage.css +1040 -1040
  135. package/web-ui/styles/settings-panel.css +349 -349
  136. package/web-ui/styles/skills-list.css +305 -303
  137. package/web-ui/styles/skills-market.css +429 -406
  138. package/web-ui/styles/task-orchestration.css +822 -822
  139. package/web-ui/styles/titles-cards.css +472 -472
  140. package/web-ui/styles/trash-panel.css +90 -90
  141. package/web-ui/styles/webhook.css +81 -81
  142. package/web-ui/styles.css +23 -23
  143. package/web-ui.html +17 -17
@@ -1,553 +1,553 @@
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
- setPromptVariableValue(name, value) {
362
- const key = typeof name === 'string' ? name.trim() : '';
363
- if (!key) return;
364
- const next = { ...(this.promptTemplateVarValuesRaw && typeof this.promptTemplateVarValuesRaw === 'object' ? this.promptTemplateVarValuesRaw : {}) };
365
- next[key] = value == null ? '' : String(value);
366
- this.promptTemplateVarValuesRaw = next;
367
- },
368
-
369
- async copyRenderedPrompt() {
370
- const text = typeof this.renderedPrompt === 'string' ? this.renderedPrompt.trim() : '';
371
- if (!text) {
372
- this.showMessage(typeof this.t === 'function' ? this.t('toast.copy.empty') : 'Nothing to copy', 'info');
373
- return;
374
- }
375
- try {
376
- if (navigator.clipboard && window.isSecureContext) {
377
- await navigator.clipboard.writeText(text);
378
- this.showMessage(typeof this.t === 'function' ? this.t('toast.copy.ok') : 'Copied', 'success');
379
- return;
380
- }
381
- } catch (_) {}
382
- const ok = typeof this.fallbackCopyText === 'function' ? this.fallbackCopyText(text) : false;
383
- if (ok) {
384
- this.showMessage(typeof this.t === 'function' ? this.t('toast.copy.ok') : 'Copied', 'success');
385
- return;
386
- }
387
- this.showMessage(typeof this.t === 'function' ? this.t('toast.copy.fail') : 'Copy failed', 'error');
388
- },
389
-
390
- async savePromptTemplate() {
391
- const draft = normalizePromptTemplateDraft(this.promptTemplateDraftRaw);
392
- if (draft.isBuiltin) {
393
- this.showMessage(typeof this.t === 'function' ? this.t('toast.templates.builtinNotModifiable') : 'Built-in templates are read-only. Duplicate first.', 'error');
394
- return false;
395
- }
396
- const name = draft.name.trim();
397
- if (!name) {
398
- this.showMessage(typeof this.t === 'function' ? this.t('toast.templates.nameRequired') : 'Template name is required', 'error');
399
- return false;
400
- }
401
- const nextId = draft.id ? draft.id : createId('prompt');
402
- const list = Array.isArray(this.promptTemplatesListRaw) ? [...this.promptTemplatesListRaw] : [];
403
- const now = nowIso();
404
- const entry = {
405
- ...draft,
406
- id: nextId,
407
- name,
408
- updatedAt: now,
409
- createdAt: draft.createdAt || now,
410
- isBuiltin: draft.isBuiltin === true
411
- };
412
- const index = list.findIndex((item) => item && item.id === nextId);
413
- if (index >= 0) {
414
- list[index] = entry;
415
- } else {
416
- list.unshift(entry);
417
- }
418
- this.promptTemplatesListRaw = list;
419
- persistPromptTemplatesToStorage(list, localStorage);
420
- this.promptTemplateDraftRaw = entry;
421
- this.promptTemplateSelectedId = nextId;
422
- this.showMessage(typeof this.t === 'function' ? this.t('toast.save.ok') : 'Saved', 'success');
423
- return true;
424
- },
425
-
426
- duplicatePromptTemplate() {
427
- const draft = normalizePromptTemplateDraft(this.promptTemplateDraftRaw);
428
- if (!draft.id) return;
429
- if (draft.isBuiltin) {
430
- this.showMessage(typeof this.t === 'function' ? this.t('toast.templates.builtinNotDuplicable') : 'Built-in templates cannot be duplicated', 'error');
431
- return;
432
- }
433
- const nextId = createId('prompt');
434
- this.promptTemplateDraftRaw = {
435
- ...draft,
436
- id: nextId,
437
- name: `${draft.name || 'Template'} (copy)`,
438
- createdAt: nowIso(),
439
- updatedAt: nowIso(),
440
- isBuiltin: false
441
- };
442
- this.promptTemplateSelectedId = nextId;
443
- this.promptTemplateVarValuesRaw = {};
444
- },
445
-
446
- async deletePromptTemplate() {
447
- const draft = normalizePromptTemplateDraft(this.promptTemplateDraftRaw);
448
- if (!draft.id) return;
449
- if (draft.isBuiltin) {
450
- this.showMessage(typeof this.t === 'function' ? this.t('toast.templates.builtinNotDeletable') : 'Built-in templates cannot be deleted', 'error');
451
- return;
452
- }
453
- const t = typeof this.t === 'function' ? this.t : null;
454
- const confirmed = await this.requestConfirmDialog({
455
- title: t ? t('toast.templates.deleteTitle') : 'Delete template',
456
- message: t ? t('toast.templates.deleteMessage', { name: draft.name || draft.id }) : `Delete “${draft.name || draft.id}”? This action cannot be undone.`,
457
- confirmText: t ? t('toast.templates.deleteConfirm') : 'Delete',
458
- cancelText: t ? t('toast.templates.deleteCancel') : 'Cancel',
459
- danger: true
460
- });
461
- if (!confirmed) return;
462
-
463
- const list = Array.isArray(this.promptTemplatesListRaw) ? this.promptTemplatesListRaw : [];
464
- const next = list.filter((item) => !(item && item.id === draft.id));
465
- this.promptTemplatesListRaw = next;
466
- persistPromptTemplatesToStorage(next, localStorage);
467
- this.promptTemplateDraftRaw = null;
468
- this.promptTemplateSelectedId = '';
469
- const first = this.promptTemplatesList && this.promptTemplatesList.length ? this.promptTemplatesList[0] : null;
470
- if (first) this.selectPromptTemplate(first.id);
471
- this.showMessage(typeof this.t === 'function' ? this.t('toast.delete.ok') : 'Deleted', 'success');
472
- },
473
-
474
- exportPromptTemplates() {
475
- const list = this.promptTemplatesList;
476
- if (!Array.isArray(list) || !list.length) {
477
- this.showMessage(typeof this.t === 'function' ? this.t('toast.export.empty') : 'Nothing to export', 'info');
478
- return;
479
- }
480
- const payload = JSON.stringify(list.map((item) => ({
481
- id: item.id,
482
- name: item.name,
483
- description: item.description,
484
- template: item.template,
485
- createdAt: item.createdAt,
486
- updatedAt: item.updatedAt,
487
- isBuiltin: item.isBuiltin
488
- })), null, 2);
489
- if (typeof this.downloadTextFile === 'function') {
490
- this.downloadTextFile(`prompt-templates-${Date.now()}.json`, payload, 'application/json;charset=utf-8');
491
- this.showMessage(typeof this.t === 'function' ? this.t('toast.export.ok') : 'Exported', 'success');
492
- return;
493
- }
494
- this.showMessage(typeof this.t === 'function' ? this.t('toast.export.notSupported') : 'Export not supported', 'error');
495
- },
496
-
497
- triggerPromptTemplatesImport() {
498
- const input = this.$refs && this.$refs.promptTemplatesImportInput
499
- ? this.$refs.promptTemplatesImportInput
500
- : null;
501
- if (!input) {
502
- this.showMessage(typeof this.t === 'function' ? this.t('toast.import.notAvailable') : 'Import is not available', 'error');
503
- return;
504
- }
505
- input.value = '';
506
- input.click();
507
- },
508
-
509
- async handlePromptTemplatesImportChange(event) {
510
- const input = event && event.target ? event.target : null;
511
- const file = input && input.files && input.files[0] ? input.files[0] : null;
512
- if (!file) return;
513
- let text = '';
514
- try {
515
- text = await file.text();
516
- } catch (_) {
517
- this.showMessage(typeof this.t === 'function' ? this.t('toast.import.readFileFail') : 'Failed to read file', 'error');
518
- return;
519
- }
520
- let parsed;
521
- try {
522
- parsed = JSON.parse(text);
523
- } catch (_) {
524
- this.showMessage(typeof this.t === 'function' ? this.t('toast.import.invalidJson') : 'Invalid JSON', 'error');
525
- return;
526
- }
527
- if (!Array.isArray(parsed)) {
528
- this.showMessage(typeof this.t === 'function' ? this.t('toast.import.expectedArray') : 'Expected an array', 'error');
529
- return;
530
- }
531
- const list = Array.isArray(this.promptTemplatesListRaw) ? [...this.promptTemplatesListRaw] : [];
532
- for (const item of parsed) {
533
- const draft = normalizePromptTemplateDraft(item);
534
- if (!draft.name || !draft.template) continue;
535
- const id = draft.id ? draft.id : createId('prompt');
536
- const now = nowIso();
537
- const entry = {
538
- ...draft,
539
- id,
540
- createdAt: draft.createdAt || now,
541
- updatedAt: now,
542
- isBuiltin: false
543
- };
544
- const index = list.findIndex((existing) => existing && existing.id === id);
545
- if (index >= 0) list[index] = entry;
546
- else list.unshift(entry);
547
- }
548
- this.promptTemplatesListRaw = list;
549
- persistPromptTemplatesToStorage(list, localStorage);
550
- this.showMessage(typeof this.t === 'function' ? this.t('toast.import.ok') : 'Imported', 'success');
551
- }
552
- };
553
- }
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
+ setPromptVariableValue(name, value) {
362
+ const key = typeof name === 'string' ? name.trim() : '';
363
+ if (!key) return;
364
+ const next = { ...(this.promptTemplateVarValuesRaw && typeof this.promptTemplateVarValuesRaw === 'object' ? this.promptTemplateVarValuesRaw : {}) };
365
+ next[key] = value == null ? '' : String(value);
366
+ this.promptTemplateVarValuesRaw = next;
367
+ },
368
+
369
+ async copyRenderedPrompt() {
370
+ const text = typeof this.renderedPrompt === 'string' ? this.renderedPrompt.trim() : '';
371
+ if (!text) {
372
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.copy.empty') : 'Nothing to copy', 'info');
373
+ return;
374
+ }
375
+ try {
376
+ if (navigator.clipboard && window.isSecureContext) {
377
+ await navigator.clipboard.writeText(text);
378
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.copy.ok') : 'Copied', 'success');
379
+ return;
380
+ }
381
+ } catch (_) {}
382
+ const ok = typeof this.fallbackCopyText === 'function' ? this.fallbackCopyText(text) : false;
383
+ if (ok) {
384
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.copy.ok') : 'Copied', 'success');
385
+ return;
386
+ }
387
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.copy.fail') : 'Copy failed', 'error');
388
+ },
389
+
390
+ async savePromptTemplate() {
391
+ const draft = normalizePromptTemplateDraft(this.promptTemplateDraftRaw);
392
+ if (draft.isBuiltin) {
393
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.templates.builtinNotModifiable') : 'Built-in templates are read-only. Duplicate first.', 'error');
394
+ return false;
395
+ }
396
+ const name = draft.name.trim();
397
+ if (!name) {
398
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.templates.nameRequired') : 'Template name is required', 'error');
399
+ return false;
400
+ }
401
+ const nextId = draft.id ? draft.id : createId('prompt');
402
+ const list = Array.isArray(this.promptTemplatesListRaw) ? [...this.promptTemplatesListRaw] : [];
403
+ const now = nowIso();
404
+ const entry = {
405
+ ...draft,
406
+ id: nextId,
407
+ name,
408
+ updatedAt: now,
409
+ createdAt: draft.createdAt || now,
410
+ isBuiltin: draft.isBuiltin === true
411
+ };
412
+ const index = list.findIndex((item) => item && item.id === nextId);
413
+ if (index >= 0) {
414
+ list[index] = entry;
415
+ } else {
416
+ list.unshift(entry);
417
+ }
418
+ this.promptTemplatesListRaw = list;
419
+ persistPromptTemplatesToStorage(list, localStorage);
420
+ this.promptTemplateDraftRaw = entry;
421
+ this.promptTemplateSelectedId = nextId;
422
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.save.ok') : 'Saved', 'success');
423
+ return true;
424
+ },
425
+
426
+ duplicatePromptTemplate() {
427
+ const draft = normalizePromptTemplateDraft(this.promptTemplateDraftRaw);
428
+ if (!draft.id) return;
429
+ if (draft.isBuiltin) {
430
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.templates.builtinNotDuplicable') : 'Built-in templates cannot be duplicated', 'error');
431
+ return;
432
+ }
433
+ const nextId = createId('prompt');
434
+ this.promptTemplateDraftRaw = {
435
+ ...draft,
436
+ id: nextId,
437
+ name: `${draft.name || 'Template'} (copy)`,
438
+ createdAt: nowIso(),
439
+ updatedAt: nowIso(),
440
+ isBuiltin: false
441
+ };
442
+ this.promptTemplateSelectedId = nextId;
443
+ this.promptTemplateVarValuesRaw = {};
444
+ },
445
+
446
+ async deletePromptTemplate() {
447
+ const draft = normalizePromptTemplateDraft(this.promptTemplateDraftRaw);
448
+ if (!draft.id) return;
449
+ if (draft.isBuiltin) {
450
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.templates.builtinNotDeletable') : 'Built-in templates cannot be deleted', 'error');
451
+ return;
452
+ }
453
+ const t = typeof this.t === 'function' ? this.t : null;
454
+ const confirmed = await this.requestConfirmDialog({
455
+ title: t ? t('toast.templates.deleteTitle') : 'Delete template',
456
+ message: t ? t('toast.templates.deleteMessage', { name: draft.name || draft.id }) : `Delete “${draft.name || draft.id}”? This action cannot be undone.`,
457
+ confirmText: t ? t('toast.templates.deleteConfirm') : 'Delete',
458
+ cancelText: t ? t('toast.templates.deleteCancel') : 'Cancel',
459
+ danger: true
460
+ });
461
+ if (!confirmed) return;
462
+
463
+ const list = Array.isArray(this.promptTemplatesListRaw) ? this.promptTemplatesListRaw : [];
464
+ const next = list.filter((item) => !(item && item.id === draft.id));
465
+ this.promptTemplatesListRaw = next;
466
+ persistPromptTemplatesToStorage(next, localStorage);
467
+ this.promptTemplateDraftRaw = null;
468
+ this.promptTemplateSelectedId = '';
469
+ const first = this.promptTemplatesList && this.promptTemplatesList.length ? this.promptTemplatesList[0] : null;
470
+ if (first) this.selectPromptTemplate(first.id);
471
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.delete.ok') : 'Deleted', 'success');
472
+ },
473
+
474
+ exportPromptTemplates() {
475
+ const list = this.promptTemplatesList;
476
+ if (!Array.isArray(list) || !list.length) {
477
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.export.empty') : 'Nothing to export', 'info');
478
+ return;
479
+ }
480
+ const payload = JSON.stringify(list.map((item) => ({
481
+ id: item.id,
482
+ name: item.name,
483
+ description: item.description,
484
+ template: item.template,
485
+ createdAt: item.createdAt,
486
+ updatedAt: item.updatedAt,
487
+ isBuiltin: item.isBuiltin
488
+ })), null, 2);
489
+ if (typeof this.downloadTextFile === 'function') {
490
+ this.downloadTextFile(`prompt-templates-${Date.now()}.json`, payload, 'application/json;charset=utf-8');
491
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.export.ok') : 'Exported', 'success');
492
+ return;
493
+ }
494
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.export.notSupported') : 'Export not supported', 'error');
495
+ },
496
+
497
+ triggerPromptTemplatesImport() {
498
+ const input = this.$refs && this.$refs.promptTemplatesImportInput
499
+ ? this.$refs.promptTemplatesImportInput
500
+ : null;
501
+ if (!input) {
502
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.import.notAvailable') : 'Import is not available', 'error');
503
+ return;
504
+ }
505
+ input.value = '';
506
+ input.click();
507
+ },
508
+
509
+ async handlePromptTemplatesImportChange(event) {
510
+ const input = event && event.target ? event.target : null;
511
+ const file = input && input.files && input.files[0] ? input.files[0] : null;
512
+ if (!file) return;
513
+ let text = '';
514
+ try {
515
+ text = await file.text();
516
+ } catch (_) {
517
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.import.readFileFail') : 'Failed to read file', 'error');
518
+ return;
519
+ }
520
+ let parsed;
521
+ try {
522
+ parsed = JSON.parse(text);
523
+ } catch (_) {
524
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.import.invalidJson') : 'Invalid JSON', 'error');
525
+ return;
526
+ }
527
+ if (!Array.isArray(parsed)) {
528
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.import.expectedArray') : 'Expected an array', 'error');
529
+ return;
530
+ }
531
+ const list = Array.isArray(this.promptTemplatesListRaw) ? [...this.promptTemplatesListRaw] : [];
532
+ for (const item of parsed) {
533
+ const draft = normalizePromptTemplateDraft(item);
534
+ if (!draft.name || !draft.template) continue;
535
+ const id = draft.id ? draft.id : createId('prompt');
536
+ const now = nowIso();
537
+ const entry = {
538
+ ...draft,
539
+ id,
540
+ createdAt: draft.createdAt || now,
541
+ updatedAt: now,
542
+ isBuiltin: false
543
+ };
544
+ const index = list.findIndex((existing) => existing && existing.id === id);
545
+ if (index >= 0) list[index] = entry;
546
+ else list.unshift(entry);
547
+ }
548
+ this.promptTemplatesListRaw = list;
549
+ persistPromptTemplatesToStorage(list, localStorage);
550
+ this.showMessage(typeof this.t === 'function' ? this.t('toast.import.ok') : 'Imported', 'success');
551
+ }
552
+ };
553
+ }