codexmate 0.0.27 → 0.0.28

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 (137) 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 +1620 -1299
  7. package/cli/claude-proxy.js +1022 -1022
  8. package/cli/config-bootstrap.js +384 -384
  9. package/cli/config-health.js +338 -338
  10. package/cli/doctor-core.js +903 -903
  11. package/cli/import-skills-url.js +356 -356
  12. package/cli/openai-bridge.js +1489 -1091
  13. package/cli/openclaw-config.js +629 -629
  14. package/cli/session-convert-args.js +65 -65
  15. package/cli/session-convert-io.js +82 -82
  16. package/cli/session-convert.js +43 -43
  17. package/cli/session-usage.concurrent.js +28 -28
  18. package/cli/session-usage.js +118 -118
  19. package/cli/session-usage.models.js +176 -176
  20. package/cli/skills.js +1141 -1141
  21. package/cli/zip-commands.js +510 -510
  22. package/cli.js +15264 -15251
  23. package/lib/automation.js +404 -404
  24. package/lib/cli-file-utils.js +151 -151
  25. package/lib/cli-models-utils.js +440 -379
  26. package/lib/cli-network-utils.js +190 -190
  27. package/lib/cli-path-utils.js +85 -85
  28. package/lib/cli-session-utils.js +121 -121
  29. package/lib/cli-sessions.js +417 -417
  30. package/lib/cli-utils.js +155 -155
  31. package/lib/download-artifacts.js +92 -92
  32. package/lib/mcp-stdio.js +453 -453
  33. package/lib/task-orchestrator.js +869 -869
  34. package/lib/text-diff.js +303 -303
  35. package/lib/workflow-engine.js +340 -340
  36. package/package.json +3 -1
  37. package/plugins/README.md +20 -20
  38. package/plugins/README.zh-CN.md +20 -20
  39. package/plugins/prompt-templates/comment-polish/index.mjs +25 -25
  40. package/plugins/prompt-templates/computed.mjs +253 -253
  41. package/plugins/prompt-templates/index.mjs +8 -8
  42. package/plugins/prompt-templates/manifest.mjs +15 -15
  43. package/plugins/prompt-templates/methods.mjs +553 -619
  44. package/plugins/prompt-templates/overview.mjs +91 -90
  45. package/plugins/prompt-templates/ownership.mjs +19 -19
  46. package/plugins/prompt-templates/rule-ack/index.mjs +21 -21
  47. package/plugins/prompt-templates/storage.mjs +64 -64
  48. package/plugins/registry.mjs +16 -16
  49. package/web-ui/app.js +625 -634
  50. package/web-ui/index.html +35 -35
  51. package/web-ui/logic.agents-diff.mjs +386 -386
  52. package/web-ui/logic.claude.mjs +168 -168
  53. package/web-ui/logic.codex.mjs +56 -0
  54. package/web-ui/logic.mjs +5 -5
  55. package/web-ui/logic.runtime.mjs +128 -128
  56. package/web-ui/logic.session-convert.mjs +70 -70
  57. package/web-ui/logic.sessions.mjs +765 -765
  58. package/web-ui/modules/api.mjs +90 -90
  59. package/web-ui/modules/app.computed.dashboard.mjs +225 -171
  60. package/web-ui/modules/app.computed.index.mjs +17 -17
  61. package/web-ui/modules/app.computed.main-tabs.mjs +205 -205
  62. package/web-ui/modules/app.computed.session.mjs +994 -994
  63. package/web-ui/modules/app.constants.mjs +15 -15
  64. package/web-ui/modules/app.methods.agents.mjs +632 -632
  65. package/web-ui/modules/app.methods.claude-config.mjs +190 -184
  66. package/web-ui/modules/app.methods.codex-config.mjs +892 -860
  67. package/web-ui/modules/app.methods.index.mjs +92 -92
  68. package/web-ui/modules/app.methods.install.mjs +205 -205
  69. package/web-ui/modules/app.methods.navigation.mjs +743 -743
  70. package/web-ui/modules/app.methods.openclaw-core.mjs +814 -814
  71. package/web-ui/modules/app.methods.openclaw-editing.mjs +372 -372
  72. package/web-ui/modules/app.methods.openclaw-persist.mjs +369 -369
  73. package/web-ui/modules/app.methods.providers.mjs +412 -404
  74. package/web-ui/modules/app.methods.runtime.mjs +345 -345
  75. package/web-ui/modules/app.methods.session-actions.mjs +593 -596
  76. package/web-ui/modules/app.methods.session-browser.mjs +984 -989
  77. package/web-ui/modules/app.methods.session-timeline.mjs +479 -479
  78. package/web-ui/modules/app.methods.session-trash.mjs +439 -439
  79. package/web-ui/modules/app.methods.startup-claude.mjs +533 -526
  80. package/web-ui/modules/app.methods.task-orchestration.mjs +556 -556
  81. package/web-ui/modules/config-mode.computed.mjs +124 -124
  82. package/web-ui/modules/config-template-confirm-pref.mjs +33 -33
  83. package/web-ui/modules/i18n.dict.mjs +2109 -2131
  84. package/web-ui/modules/i18n.mjs +56 -56
  85. package/web-ui/modules/plugins.computed.mjs +3 -3
  86. package/web-ui/modules/plugins.methods.mjs +3 -3
  87. package/web-ui/modules/plugins.storage.mjs +11 -11
  88. package/web-ui/modules/provider-url-display.mjs +17 -0
  89. package/web-ui/modules/sessions-filters-url.mjs +85 -85
  90. package/web-ui/modules/skills.computed.mjs +107 -107
  91. package/web-ui/modules/skills.methods.mjs +481 -481
  92. package/web-ui/partials/index/layout-footer.html +13 -13
  93. package/web-ui/partials/index/layout-header.html +475 -475
  94. package/web-ui/partials/index/modal-config-template-agents.html +174 -174
  95. package/web-ui/partials/index/modal-confirm-toast.html +32 -32
  96. package/web-ui/partials/index/modal-health-check.html +45 -45
  97. package/web-ui/partials/index/modal-openclaw-config.html +280 -280
  98. package/web-ui/partials/index/modal-skills.html +200 -200
  99. package/web-ui/partials/index/modals-basic.html +165 -165
  100. package/web-ui/partials/index/panel-config-claude.html +188 -187
  101. package/web-ui/partials/index/panel-config-codex.html +312 -283
  102. package/web-ui/partials/index/panel-config-openclaw.html +83 -83
  103. package/web-ui/partials/index/panel-dashboard.html +186 -186
  104. package/web-ui/partials/index/panel-docs.html +147 -147
  105. package/web-ui/partials/index/panel-market.html +177 -177
  106. package/web-ui/partials/index/panel-orchestration.html +391 -391
  107. package/web-ui/partials/index/panel-plugins.html +253 -279
  108. package/web-ui/partials/index/panel-sessions.html +316 -326
  109. package/web-ui/partials/index/panel-settings.html +253 -274
  110. package/web-ui/partials/index/panel-usage.html +371 -371
  111. package/web-ui/res/json5.min.js +1 -1
  112. package/web-ui/res/vue.global.prod.js +13 -13
  113. package/web-ui/session-helpers.mjs +576 -576
  114. package/web-ui/source-bundle.cjs +233 -233
  115. package/web-ui/styles/base-theme.css +281 -268
  116. package/web-ui/styles/controls-forms.css +422 -423
  117. package/web-ui/styles/dashboard.css +274 -274
  118. package/web-ui/styles/docs-panel.css +247 -247
  119. package/web-ui/styles/feedback.css +108 -108
  120. package/web-ui/styles/health-check-dialog.css +144 -144
  121. package/web-ui/styles/layout-shell.css +606 -603
  122. package/web-ui/styles/modals-core.css +466 -464
  123. package/web-ui/styles/navigation-panels.css +391 -390
  124. package/web-ui/styles/openclaw-structured.css +266 -266
  125. package/web-ui/styles/plugins-panel.css +523 -523
  126. package/web-ui/styles/responsive.css +454 -454
  127. package/web-ui/styles/sessions-list.css +419 -415
  128. package/web-ui/styles/sessions-preview.css +411 -411
  129. package/web-ui/styles/sessions-toolbar-trash.css +330 -330
  130. package/web-ui/styles/sessions-usage.css +1040 -1040
  131. package/web-ui/styles/settings-panel.css +185 -185
  132. package/web-ui/styles/skills-list.css +303 -303
  133. package/web-ui/styles/skills-market.css +406 -406
  134. package/web-ui/styles/task-orchestration.css +822 -822
  135. package/web-ui/styles/titles-cards.css +472 -408
  136. package/web-ui/styles.css +21 -21
  137. package/web-ui.html +17 -17
@@ -1,619 +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
- 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
- }
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
+ }