@kubohiroya/turbowarp-plus-editor-workbench 0.4.0 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +42 -0
- package/README.md +42 -0
- package/dist/ai-agent-settings.d.ts +97 -0
- package/dist/ai-agent-settings.js +316 -0
- package/dist/ai-agents.d.ts +24 -0
- package/dist/ai-agents.js +75 -0
- package/dist/drawer-views.d.ts +66 -0
- package/dist/drawer-views.js +152 -0
- package/dist/sprite-tabs.d.ts +20 -0
- package/dist/sprite-tabs.js +101 -20
- package/dist/standard-layout.d.ts +4 -1
- package/dist/standard-layout.js +57 -13
- package/dist/target-tab-menu.d.ts +29 -0
- package/dist/target-tab-menu.js +76 -0
- package/dist/workspace-layout.d.ts +1 -0
- package/dist/workspace-layout.js +6 -2
- package/package.json +6 -2
- package/src/ai-agent-settings.ts +380 -0
- package/src/ai-agents.ts +95 -0
- package/src/drawer-views.ts +203 -0
- package/src/editor.css +270 -0
- package/src/sprite-layout-menu.css +37 -0
- package/src/sprite-tabs.ts +100 -20
- package/src/standard-layout.ts +67 -17
- package/src/target-tab-menu.ts +99 -0
- package/src/workspace-layout.ts +6 -3
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
import {isJapanese} from './drawer-views.ts';
|
|
2
|
+
|
|
3
|
+
/** The services the Editor knows by name. Their name and icon are fixed; their address can be replaced. */
|
|
4
|
+
export type AgentPresetId = 'chatgpt' | 'claude';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Where a service is opened. Only web pages for now; a desktop app launched with arguments is planned
|
|
8
|
+
* as another `kind`, which this version ignores when it reads the settings.
|
|
9
|
+
*/
|
|
10
|
+
export interface AgentUrlTarget {
|
|
11
|
+
kind: 'url';
|
|
12
|
+
/** An `https:` address: the service's start page or a deep link such as a previous conversation. */
|
|
13
|
+
url: string;
|
|
14
|
+
}
|
|
15
|
+
export type AgentTarget = AgentUrlTarget;
|
|
16
|
+
|
|
17
|
+
export interface AgentService {
|
|
18
|
+
id: string;
|
|
19
|
+
preset?: AgentPresetId;
|
|
20
|
+
name: string;
|
|
21
|
+
enabled: boolean;
|
|
22
|
+
target: AgentTarget;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface AgentSettings {
|
|
26
|
+
version: 1;
|
|
27
|
+
/** The presets in their fixed order, then the user's own services. */
|
|
28
|
+
services: AgentService[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type AgentStorage = Pick<Storage, 'getItem' | 'setItem'>;
|
|
32
|
+
|
|
33
|
+
export const agentSettingsKey = 'twPlusAiAgents';
|
|
34
|
+
|
|
35
|
+
export const agentPresets: readonly {id: AgentPresetId; name: string; url: string; letter: string}[] = [
|
|
36
|
+
{id: 'chatgpt', name: 'ChatGPT', url: 'https://chatgpt.com/', letter: 'G'},
|
|
37
|
+
{id: 'claude', name: 'Claude', url: 'https://claude.ai/new', letter: 'C'}
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
export type AgentUrlProblem = 'empty' | 'invalid' | 'not-https';
|
|
41
|
+
|
|
42
|
+
/** The address to save, or why it cannot be used. Only `https:` addresses are accepted. */
|
|
43
|
+
export function checkAgentUrl(value: unknown): {url: string} | {problem: AgentUrlProblem} {
|
|
44
|
+
const text = typeof value === 'string' ? value.trim() : '';
|
|
45
|
+
if (!text) return {problem: 'empty'};
|
|
46
|
+
let url: URL;
|
|
47
|
+
try {
|
|
48
|
+
url = new URL(text);
|
|
49
|
+
} catch {
|
|
50
|
+
return {problem: 'invalid'};
|
|
51
|
+
}
|
|
52
|
+
if (url.protocol !== 'https:') return {problem: 'not-https'};
|
|
53
|
+
if (!url.hostname) return {problem: 'invalid'};
|
|
54
|
+
return {url: url.href};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const presetService = (preset: (typeof agentPresets)[number], saved?: {enabled?: unknown; target?: {url?: unknown}}): AgentService => {
|
|
58
|
+
const checked = checkAgentUrl(saved?.target?.url);
|
|
59
|
+
return {
|
|
60
|
+
id: preset.id, preset: preset.id, name: preset.name,
|
|
61
|
+
enabled: saved?.enabled !== false,
|
|
62
|
+
target: {kind: 'url', url: 'url' in checked ? checked.url : preset.url}
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const defaultAgentSettings = (): AgentSettings => ({version: 1, services: agentPresets.map(preset => presetService(preset))});
|
|
67
|
+
|
|
68
|
+
/** Settings read from saved JSON. Anything missing, unknown or invalid falls back to the defaults, entry by entry. */
|
|
69
|
+
export function parseAgentSettings(raw: string | null | undefined): AgentSettings {
|
|
70
|
+
let saved: unknown;
|
|
71
|
+
try {
|
|
72
|
+
saved = JSON.parse(raw ?? 'null');
|
|
73
|
+
} catch {
|
|
74
|
+
return defaultAgentSettings();
|
|
75
|
+
}
|
|
76
|
+
if (!saved || typeof saved !== 'object' || (saved as {version?: unknown}).version !== 1 ||
|
|
77
|
+
!Array.isArray((saved as {services?: unknown}).services)) return defaultAgentSettings();
|
|
78
|
+
const entries = (saved as {services: unknown[]}).services.filter(entry => entry && typeof entry === 'object') as
|
|
79
|
+
{id?: unknown; preset?: unknown; name?: unknown; enabled?: unknown; target?: {kind?: unknown; url?: unknown}}[];
|
|
80
|
+
const presets = agentPresets.map(preset => presetService(preset, entries.find(entry => entry.preset === preset.id)));
|
|
81
|
+
const ids = new Set<string>(presets.map(service => service.id));
|
|
82
|
+
const custom: AgentService[] = [];
|
|
83
|
+
for (const entry of entries) {
|
|
84
|
+
if (entry.preset !== undefined || entry.target?.kind !== 'url') continue;
|
|
85
|
+
const name = typeof entry.name === 'string' ? entry.name.trim() : '';
|
|
86
|
+
const checked = checkAgentUrl(entry.target.url);
|
|
87
|
+
if (!name || !('url' in checked)) continue;
|
|
88
|
+
let id = typeof entry.id === 'string' && entry.id ? entry.id : 'custom';
|
|
89
|
+
for (let n = 2; ids.has(id); n++) id = `${String(entry.id || 'custom')}-${n}`;
|
|
90
|
+
ids.add(id);
|
|
91
|
+
custom.push({id, name, enabled: entry.enabled !== false, target: {kind: 'url', url: checked.url}});
|
|
92
|
+
}
|
|
93
|
+
return {version: 1, services: [...presets, ...custom]};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export const browserStorage = (): AgentStorage | null => {
|
|
97
|
+
try {
|
|
98
|
+
return globalThis.localStorage ?? null;
|
|
99
|
+
} catch {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
export function readAgentSettings(storage: AgentStorage | null = browserStorage(), key = agentSettingsKey): AgentSettings {
|
|
105
|
+
try {
|
|
106
|
+
return parseAgentSettings(storage?.getItem(key));
|
|
107
|
+
} catch {
|
|
108
|
+
return defaultAgentSettings();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Saves the settings; returns false when the storage refuses them. */
|
|
113
|
+
export function writeAgentSettings(settings: AgentSettings, storage: AgentStorage | null = browserStorage(),
|
|
114
|
+
key = agentSettingsKey): boolean {
|
|
115
|
+
if (!storage) return false;
|
|
116
|
+
try {
|
|
117
|
+
storage.setItem(key, JSON.stringify({version: 1, services: settings.services.map(service => service.preset ?
|
|
118
|
+
{preset: service.preset, enabled: service.enabled, target: service.target} :
|
|
119
|
+
{id: service.id, name: service.name, enabled: service.enabled, target: service.target})}));
|
|
120
|
+
return true;
|
|
121
|
+
} catch {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export const enabledAgentServices = (settings: AgentSettings) => settings.services.filter(service => service.enabled);
|
|
127
|
+
|
|
128
|
+
export type Opener = (url: string, target: string, features: string) => unknown;
|
|
129
|
+
|
|
130
|
+
/** Opens the service in a new tab, without giving it access to the Editor. */
|
|
131
|
+
export function openAgentService(service: AgentService, open: Opener = (...args) => window.open(...args)) {
|
|
132
|
+
if (service.target.kind === 'url') open(service.target.url, '_blank', 'noopener,noreferrer');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const texts = {
|
|
136
|
+
menu: ['AI エージェント', 'AI agents'],
|
|
137
|
+
settings: ['AI エージェント設定…', 'AI agent settings…'],
|
|
138
|
+
empty: ['メニューに出す AI サービスがありません。設定で選んでください。',
|
|
139
|
+
'No AI services are shown here. Choose them in the settings.'],
|
|
140
|
+
newTab: ['別のタブで開きます', 'Opens in a new tab'],
|
|
141
|
+
title: ['AI エージェント設定', 'AI agent settings'],
|
|
142
|
+
description: ['ドロワーの AI エージェントのメニューに出すサービスを選びます。URL は、以前の会話など https: で始まるアドレスに置き換えられます。',
|
|
143
|
+
'Choose the services the AI agent menu in the drawer shows. Each URL can be replaced by any https: address, such as a previous conversation.'],
|
|
144
|
+
presets: ['用意されたサービス', 'Built-in services'],
|
|
145
|
+
custom: ['追加したサービス', 'Your services'],
|
|
146
|
+
customEmpty: ['まだありません。', 'None yet.'],
|
|
147
|
+
customService: ['追加したサービス', 'Service'],
|
|
148
|
+
show: ['メニューに出す', 'Show in the menu'],
|
|
149
|
+
name: ['名前', 'Name'],
|
|
150
|
+
url: ['URL', 'URL'],
|
|
151
|
+
defaultUrl: ['空欄のときは', 'When empty:'],
|
|
152
|
+
add: ['サービスを追加', 'Add a service'],
|
|
153
|
+
remove: ['削除', 'Remove'],
|
|
154
|
+
cancel: ['キャンセル', 'Cancel'],
|
|
155
|
+
save: ['保存', 'Save'],
|
|
156
|
+
nameEmpty: ['名前を入力してください。', 'Enter a name.'],
|
|
157
|
+
urlEmpty: ['URL を入力してください。', 'Enter a URL.'],
|
|
158
|
+
urlInvalid: ['URL として読めません。https: で始まるアドレスを入力してください。', 'This is not a URL. Enter an address that starts with https:.'],
|
|
159
|
+
urlNotHttps: ['https: で始まるアドレスだけを使えます。', 'Only addresses that start with https: can be used.'],
|
|
160
|
+
saveFailed: ['設定を保存できませんでした。ブラウザーの保存領域が使えません。', 'The settings could not be saved: browser storage is unavailable.']
|
|
161
|
+
} as const;
|
|
162
|
+
export const agentText = (key: keyof typeof texts, locale: string) => texts[key][isJapanese(locale) ? 0 : 1];
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
export interface AgentSettingsDialogOptions {
|
|
166
|
+
document?: Document;
|
|
167
|
+
locale: string;
|
|
168
|
+
/** The workbench stylesheet's class names (`editor.css`). */
|
|
169
|
+
styles?: Record<string, string>;
|
|
170
|
+
storage?: AgentStorage | null;
|
|
171
|
+
storageKey?: string;
|
|
172
|
+
/** Where focus goes when the dialog closes. Defaults to the element focused when it opened. */
|
|
173
|
+
returnFocus?: HTMLElement | null;
|
|
174
|
+
onSave?(settings: AgentSettings): void;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export interface AgentSettingsDialog {
|
|
178
|
+
element: HTMLElement;
|
|
179
|
+
close(): void;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
interface Row {
|
|
183
|
+
service?: AgentService;
|
|
184
|
+
group: HTMLElement;
|
|
185
|
+
enabled: HTMLInputElement;
|
|
186
|
+
name?: HTMLInputElement;
|
|
187
|
+
url: HTMLInputElement;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
let openDialog: AgentSettingsDialog | null = null;
|
|
191
|
+
let nextDialog = 0;
|
|
192
|
+
const focusable = 'button:not([disabled]), input:not([disabled]), [href], [tabindex]:not([tabindex="-1"])';
|
|
193
|
+
const urlProblems = {empty: 'urlEmpty', invalid: 'urlInvalid', 'not-https': 'urlNotHttps'} as const;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* The settings dialog: turns the presets on and off, replaces their address, and adds or removes the
|
|
197
|
+
* user's own services. It is plain DOM so that any part of the Editor can open it, with or without React.
|
|
198
|
+
*/
|
|
199
|
+
export function openAgentSettingsDialog({document: doc = globalThis.document, locale, styles = {}, storage = browserStorage(),
|
|
200
|
+
storageKey = agentSettingsKey, returnFocus, onSave}: AgentSettingsDialogOptions): AgentSettingsDialog {
|
|
201
|
+
if (openDialog?.element.isConnected) {
|
|
202
|
+
(openDialog.element.querySelector(focusable) as HTMLElement)?.focus();
|
|
203
|
+
return openDialog;
|
|
204
|
+
}
|
|
205
|
+
const text = (key: keyof typeof texts) => agentText(key, locale);
|
|
206
|
+
const restore = returnFocus ?? doc.activeElement as HTMLElement | null;
|
|
207
|
+
const prefix = `plus-agent-settings-${++nextDialog}`;
|
|
208
|
+
let fieldId = 0;
|
|
209
|
+
const make = <K extends keyof HTMLElementTagNameMap>(tag: K, attributes: Record<string, string | undefined> = {},
|
|
210
|
+
...children: (Node | string | false | null | undefined)[]) => {
|
|
211
|
+
const node = doc.createElement(tag);
|
|
212
|
+
for (const [name, value] of Object.entries(attributes)) {
|
|
213
|
+
if (value !== undefined) node.setAttribute(name, value);
|
|
214
|
+
}
|
|
215
|
+
for (const child of children) if (child) node.append(child);
|
|
216
|
+
return node;
|
|
217
|
+
};
|
|
218
|
+
const field = (label: string, input: HTMLInputElement) => {
|
|
219
|
+
input.id = `${prefix}-field-${++fieldId}`;
|
|
220
|
+
const error = make('p', {id: `${input.id}-error`, class: styles.aiAgentDialogError, hidden: ''});
|
|
221
|
+
const wrapper = make('div', {class: input.type === 'checkbox' ? styles.aiAgentDialogCheck : styles.aiAgentDialogField},
|
|
222
|
+
...(input.type === 'checkbox' ? [input, make('label', {for: input.id}, label)] :
|
|
223
|
+
[make('label', {for: input.id}, label), input]), error);
|
|
224
|
+
input.addEventListener('input', () => setError(input, null));
|
|
225
|
+
return wrapper;
|
|
226
|
+
};
|
|
227
|
+
const setError = (input: HTMLInputElement, message: string | null) => {
|
|
228
|
+
const error = doc.getElementById(`${input.id}-error`);
|
|
229
|
+
if (error) {
|
|
230
|
+
error.textContent = message ?? '';
|
|
231
|
+
error.hidden = !message;
|
|
232
|
+
}
|
|
233
|
+
if (message) {
|
|
234
|
+
input.setAttribute('aria-invalid', 'true');
|
|
235
|
+
input.setAttribute('aria-describedby', `${input.id}-error`);
|
|
236
|
+
} else {
|
|
237
|
+
input.removeAttribute('aria-invalid');
|
|
238
|
+
input.removeAttribute('aria-describedby');
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
const checkbox = (checked: boolean) => {
|
|
242
|
+
const input = make('input', {type: 'checkbox'});
|
|
243
|
+
input.checked = checked;
|
|
244
|
+
return input;
|
|
245
|
+
};
|
|
246
|
+
const textInput = (type: string, value: string, placeholder?: string) => {
|
|
247
|
+
const input = make('input', {type, spellcheck: 'false', autocomplete: 'off', placeholder});
|
|
248
|
+
input.value = value;
|
|
249
|
+
return input;
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
const settings = readAgentSettings(storage, storageKey);
|
|
253
|
+
const rows: Row[] = [];
|
|
254
|
+
const presetGroup = make('fieldset', {class: styles.aiAgentDialogSection}, make('legend', {}, text('presets')));
|
|
255
|
+
for (const service of settings.services.filter(item => item.preset)) {
|
|
256
|
+
const preset = agentPresets.find(item => item.id === service.preset);
|
|
257
|
+
const enabled = checkbox(service.enabled);
|
|
258
|
+
const url = textInput('url', service.target.url === preset.url ? '' : service.target.url, preset.url);
|
|
259
|
+
const group = make('div', {role: 'group', 'aria-label': service.name, class: styles.aiAgentDialogRow},
|
|
260
|
+
field(`${service.name}${isJapanese(locale) ? ' を' : ': '}${text('show')}`, enabled),
|
|
261
|
+
field(`${service.name} ${text('url')}`, url),
|
|
262
|
+
make('p', {class: styles.aiAgentDialogNote}, `${text('defaultUrl')} ${preset.url}`));
|
|
263
|
+
rows.push({service, group, enabled, url});
|
|
264
|
+
presetGroup.append(group);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const customList = make('div', {class: styles.aiAgentDialogList});
|
|
268
|
+
const customEmpty = make('p', {class: styles.aiAgentDialogNote}, text('customEmpty'));
|
|
269
|
+
const addButton = make('button', {type: 'button', class: styles.aiAgentDialogButton}, text('add'));
|
|
270
|
+
const custom = () => rows.filter(row => !row.service);
|
|
271
|
+
const renumber = () => {
|
|
272
|
+
custom().forEach((row, index) => row.group.setAttribute('aria-label', `${text('customService')} ${index + 1}`));
|
|
273
|
+
customEmpty.hidden = custom().length > 0;
|
|
274
|
+
};
|
|
275
|
+
const addRow = (service?: AgentService) => {
|
|
276
|
+
const enabled = checkbox(service?.enabled ?? true);
|
|
277
|
+
const name = textInput('text', service?.name ?? '');
|
|
278
|
+
const url = textInput('url', service?.target.url ?? '', 'https://');
|
|
279
|
+
const remove = make('button', {type: 'button', class: styles.aiAgentDialogButton}, text('remove'));
|
|
280
|
+
const group = make('div', {role: 'group', class: styles.aiAgentDialogRow},
|
|
281
|
+
field(text('name'), name), field(text('url'), url), field(text('show'), enabled), remove);
|
|
282
|
+
const row: Row = {group, enabled, name, url};
|
|
283
|
+
remove.addEventListener('click', () => {
|
|
284
|
+
const index = custom().indexOf(row);
|
|
285
|
+
rows.splice(rows.indexOf(row), 1);
|
|
286
|
+
group.remove();
|
|
287
|
+
renumber();
|
|
288
|
+
(custom()[index]?.name ?? custom()[index - 1]?.name ?? addButton).focus();
|
|
289
|
+
});
|
|
290
|
+
rows.push(row);
|
|
291
|
+
customList.append(group);
|
|
292
|
+
renumber();
|
|
293
|
+
return row;
|
|
294
|
+
};
|
|
295
|
+
for (const service of settings.services.filter(item => !item.preset)) addRow(service);
|
|
296
|
+
addButton.addEventListener('click', () => addRow().name.focus());
|
|
297
|
+
const customGroup = make('fieldset', {class: styles.aiAgentDialogSection},
|
|
298
|
+
make('legend', {}, text('custom')), customList, customEmpty, addButton);
|
|
299
|
+
|
|
300
|
+
const status = make('p', {role: 'alert', class: styles.aiAgentDialogError, hidden: ''});
|
|
301
|
+
const cancel = make('button', {type: 'button', class: styles.aiAgentDialogButton}, text('cancel'));
|
|
302
|
+
const save = make('button', {type: 'submit', class: `${styles.aiAgentDialogButton ?? ''} ${styles.aiAgentDialogPrimary ?? ''}`.trim()},
|
|
303
|
+
text('save'));
|
|
304
|
+
const form = make('form', {novalidate: '', class: styles.aiAgentDialogForm}, presetGroup, customGroup, status,
|
|
305
|
+
make('div', {class: styles.aiAgentDialogActions}, cancel, save));
|
|
306
|
+
const element = make('div', {
|
|
307
|
+
role: 'dialog', 'aria-modal': 'true', 'aria-labelledby': `${prefix}-title`,
|
|
308
|
+
'aria-describedby': `${prefix}-description`, class: styles.aiAgentDialog
|
|
309
|
+
},
|
|
310
|
+
make('h2', {id: `${prefix}-title`}, text('title')),
|
|
311
|
+
make('p', {id: `${prefix}-description`}, text('description')),
|
|
312
|
+
form);
|
|
313
|
+
const backdrop = make('div', {class: styles.aiAgentDialogBackdrop}, element);
|
|
314
|
+
|
|
315
|
+
const close = () => {
|
|
316
|
+
if (!backdrop.isConnected) return;
|
|
317
|
+
backdrop.remove();
|
|
318
|
+
if (openDialog?.element === element) openDialog = null;
|
|
319
|
+
if (restore?.isConnected) restore.focus({preventScroll: true});
|
|
320
|
+
};
|
|
321
|
+
cancel.addEventListener('click', close);
|
|
322
|
+
element.addEventListener('keydown', event => {
|
|
323
|
+
if (event.key === 'Escape') {
|
|
324
|
+
event.preventDefault();
|
|
325
|
+
event.stopPropagation();
|
|
326
|
+
close();
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
if (event.key !== 'Tab') return;
|
|
330
|
+
const items = [...element.querySelectorAll<HTMLElement>(focusable)];
|
|
331
|
+
const first = items[0];
|
|
332
|
+
const last = items[items.length - 1];
|
|
333
|
+
if (event.shiftKey && doc.activeElement === first) {
|
|
334
|
+
event.preventDefault();
|
|
335
|
+
last.focus();
|
|
336
|
+
} else if (!event.shiftKey && doc.activeElement === last) {
|
|
337
|
+
event.preventDefault();
|
|
338
|
+
first.focus();
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
form.addEventListener('submit', event => {
|
|
342
|
+
event.preventDefault();
|
|
343
|
+
status.hidden = true;
|
|
344
|
+
const invalid: HTMLInputElement[] = [];
|
|
345
|
+
const services: AgentService[] = [];
|
|
346
|
+
for (const row of rows) {
|
|
347
|
+
const preset = row.service && agentPresets.find(item => item.id === row.service.preset);
|
|
348
|
+
const checked = preset && !row.url.value.trim() ? {url: preset.url} : checkAgentUrl(row.url.value);
|
|
349
|
+
const problem = 'problem' in checked ? text(urlProblems[checked.problem]) : null;
|
|
350
|
+
setError(row.url, problem);
|
|
351
|
+
const name = row.name?.value.trim() ?? row.service.name;
|
|
352
|
+
if (row.name) setError(row.name, name ? null : text('nameEmpty'));
|
|
353
|
+
if (row.name && !name) invalid.push(row.name);
|
|
354
|
+
if (problem) invalid.push(row.url);
|
|
355
|
+
if (!('url' in checked) || !name) continue;
|
|
356
|
+
services.push(preset ? {...row.service, enabled: row.enabled.checked, target: {kind: 'url', url: checked.url}} : {
|
|
357
|
+
id: '', name, enabled: row.enabled.checked, target: {kind: 'url', url: checked.url}
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
if (invalid.length) {
|
|
361
|
+
invalid[0].focus();
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
let count = 0;
|
|
365
|
+
for (const service of services) if (!service.preset) service.id = `custom-${++count}`;
|
|
366
|
+
const saved: AgentSettings = {version: 1, services};
|
|
367
|
+
if (!writeAgentSettings(saved, storage, storageKey)) {
|
|
368
|
+
status.textContent = text('saveFailed');
|
|
369
|
+
status.hidden = false;
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
close();
|
|
373
|
+
onSave?.(saved);
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
doc.body.append(backdrop);
|
|
377
|
+
rows[0]?.enabled.focus();
|
|
378
|
+
openDialog = {element, close};
|
|
379
|
+
return openDialog;
|
|
380
|
+
}
|
package/src/ai-agents.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type {DrawerMenu, DrawerMenuRenderProps} from './drawer-views.ts';
|
|
2
|
+
import {agentsIconPath} from './drawer-views.ts';
|
|
3
|
+
import {
|
|
4
|
+
agentPresets, agentSettingsKey, agentText, browserStorage, enabledAgentServices, openAgentService,
|
|
5
|
+
openAgentSettingsDialog, readAgentSettings, type AgentService, type AgentStorage, type Opener
|
|
6
|
+
} from './ai-agent-settings.ts';
|
|
7
|
+
|
|
8
|
+
export * from './ai-agent-settings.ts';
|
|
9
|
+
|
|
10
|
+
export interface AiAgentsOptions {
|
|
11
|
+
/** The workbench stylesheet's class names (`editor.css`), for the menu and the settings dialog. */
|
|
12
|
+
styles?: Record<string, string>;
|
|
13
|
+
storage?: AgentStorage | null;
|
|
14
|
+
storageKey?: string;
|
|
15
|
+
/** Opens a service; defaults to a new browser tab. */
|
|
16
|
+
open?: Opener;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The AI agent menu for `drawerViews.agents`, the settings dialog and the icon the host shows beside
|
|
21
|
+
* its own entry for the dialog. Every part reads the saved settings when it opens.
|
|
22
|
+
*/
|
|
23
|
+
export function createAiAgents(React, {styles = {}, storage = browserStorage(), storageKey = agentSettingsKey,
|
|
24
|
+
open}: AiAgentsOptions = {}) {
|
|
25
|
+
const {createElement: element, useRef, useState} = React;
|
|
26
|
+
const svg = (...children) => element('svg', {
|
|
27
|
+
'aria-hidden': true, focusable: false, width: 16, height: 16, viewBox: '0 0 16 16',
|
|
28
|
+
fill: 'none', stroke: 'currentColor', strokeWidth: 1.3, strokeLinecap: 'round', strokeLinejoin: 'round'
|
|
29
|
+
}, ...children);
|
|
30
|
+
const bubble = 'M3 2.5h10a1.5 1.5 0 0 1 1.5 1.5v6a1.5 1.5 0 0 1-1.5 1.5H7l-3 2.5v-2.5H3A1.5 1.5 0 0 1 1.5 10V4A1.5 1.5 0 0 1 3 2.5Z';
|
|
31
|
+
// The initials are drawn as strokes so they do not depend on a font or add to the item's text.
|
|
32
|
+
const initials = {
|
|
33
|
+
C: 'M9.9 5.6A2.4 2.4 0 1 0 9.9 8.4',
|
|
34
|
+
G: 'M9.9 5.6A2.4 2.4 0 1 0 10.4 7H8.4'
|
|
35
|
+
};
|
|
36
|
+
/** A plain chat bubble with the service's initial for a preset; a globe for the user's own address. */
|
|
37
|
+
const serviceIcon = (service: AgentService) => {
|
|
38
|
+
const preset = agentPresets.find(item => item.id === service.preset);
|
|
39
|
+
if (preset) {
|
|
40
|
+
const svgElement = svg(element('path', {d: bubble}), element('path', {d: initials[preset.letter]}));
|
|
41
|
+
return React.cloneElement(svgElement, {'data-initial': preset.letter});
|
|
42
|
+
}
|
|
43
|
+
return svg(element('circle', {cx: 8, cy: 8, r: 6.25}), element('path', {d: 'M1.75 8h12.5M8 1.75c-3 3.4-3 9.1 0 12.5M8 1.75c3 3.4 3 9.1 0 12.5'}));
|
|
44
|
+
};
|
|
45
|
+
const settingsIcon = () => svg(element('path', {d: agentsIconPath}));
|
|
46
|
+
|
|
47
|
+
const openSettings = ({locale, returnFocus}: {locale: string; returnFocus?: HTMLElement | null}) =>
|
|
48
|
+
openAgentSettingsDialog({document, locale, styles, storage, storageKey, returnFocus});
|
|
49
|
+
|
|
50
|
+
function AgentMenu({locale, close}: DrawerMenuRenderProps) {
|
|
51
|
+
const [services] = useState(() => enabledAgentServices(readAgentSettings(storage, storageKey)));
|
|
52
|
+
const list = useRef(null);
|
|
53
|
+
const hintId = useRef(null);
|
|
54
|
+
if (!hintId.current) hintId.current = `plus-agent-menu-hint-${++nextId}`;
|
|
55
|
+
const label = agentText('menu', locale);
|
|
56
|
+
const newTab = agentText('newTab', locale);
|
|
57
|
+
const move = event => {
|
|
58
|
+
const items = [...list.current.querySelectorAll('[role="menuitem"]')] as HTMLElement[];
|
|
59
|
+
const index = items.indexOf(event.target);
|
|
60
|
+
let next: number;
|
|
61
|
+
if (event.key === 'ArrowDown') next = (index + 1) % items.length;
|
|
62
|
+
else if (event.key === 'ArrowUp') next = (index - 1 + items.length) % items.length;
|
|
63
|
+
else if (event.key === 'Home') next = 0;
|
|
64
|
+
else if (event.key === 'End') next = items.length - 1;
|
|
65
|
+
else return;
|
|
66
|
+
event.preventDefault();
|
|
67
|
+
items[next]?.focus();
|
|
68
|
+
};
|
|
69
|
+
const item = (key: string, icon, text: string, onClick: () => void, title?: string) => element('button', {
|
|
70
|
+
key, type: 'button', role: 'menuitem', tabIndex: -1, className: styles.aiAgentMenuItem, title, onClick
|
|
71
|
+
}, icon, element('span', null, text));
|
|
72
|
+
return element(React.Fragment, null,
|
|
73
|
+
!services.length && element('p', {id: hintId.current, className: styles.aiAgentMenuHint}, agentText('empty', locale)),
|
|
74
|
+
element('div', {
|
|
75
|
+
role: 'menu', 'aria-label': label, ref: list, className: styles.aiAgentMenu, onKeyDown: move,
|
|
76
|
+
'aria-describedby': services.length ? undefined : hintId.current
|
|
77
|
+
},
|
|
78
|
+
services.map(service => item(service.id, serviceIcon(service), service.name, () => {
|
|
79
|
+
openAgentService(service, open);
|
|
80
|
+
close();
|
|
81
|
+
}, newTab)),
|
|
82
|
+
services.length > 0 && element('div', {role: 'separator', className: styles.aiAgentMenuSeparator}),
|
|
83
|
+
item('settings', settingsIcon(), agentText('settings', locale), () => {
|
|
84
|
+
close();
|
|
85
|
+
openSettings({locale, returnFocus: document.activeElement as HTMLElement});
|
|
86
|
+
})));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const drawerMenu: DrawerMenu = {
|
|
90
|
+
renderMenu: props => element(AgentMenu, props)
|
|
91
|
+
};
|
|
92
|
+
return {drawerMenu, openSettings, settingsIcon, serviceIcon};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let nextId = 0;
|