pinokiod 8.3.15 → 8.3.17

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 (36) hide show
  1. package/package.json +1 -1
  2. package/server/index.js +8 -3
  3. package/server/lib/create_page_url.js +21 -0
  4. package/server/lib/session_files.js +248 -0
  5. package/server/public/agent-bar-app.js +28 -6
  6. package/server/public/agent-bar-compose.js +18 -9
  7. package/server/public/agent-bar-files.css +391 -0
  8. package/server/public/agent-bar-files.js +394 -0
  9. package/server/public/agent-bar-panel.js +104 -14
  10. package/server/public/explore-create-handoff.js +49 -0
  11. package/server/public/sidebar-create.css +23 -0
  12. package/server/public/style.css +114 -0
  13. package/server/public/terminal-settings-sheet.js +66 -0
  14. package/server/public/terminal-settings.js +6 -1
  15. package/server/views/app.ejs +2 -0
  16. package/server/views/bootstrap.ejs +1 -0
  17. package/server/views/editor.ejs +1 -0
  18. package/server/views/explore.ejs +2 -1
  19. package/server/views/init/index.ejs +1 -0
  20. package/server/views/install.ejs +1 -0
  21. package/server/views/layout.ejs +13 -0
  22. package/server/views/partials/main_sidebar.ejs +4 -0
  23. package/server/views/pro.ejs +1 -0
  24. package/server/views/prototype/index.ejs +1 -0
  25. package/server/views/shell.ejs +1 -0
  26. package/server/views/terminal.ejs +1 -0
  27. package/test/agent-bar-compose.test.js +45 -0
  28. package/test/agent-bar-files.test.js +276 -0
  29. package/test/agent-bar-keyboard.test.js +84 -0
  30. package/test/agent-bar-running.test.js +148 -0
  31. package/test/agent-bar-step-aside.test.js +72 -0
  32. package/test/create-page.test.js +207 -0
  33. package/test/layout-soft-keyboard.test.js +12 -0
  34. package/test/session-files.test.js +214 -0
  35. package/test/terminal-settings-font-size.test.js +47 -0
  36. package/test/terminal-settings-sheet.test.js +58 -0
@@ -0,0 +1,394 @@
1
+ // Agent bar: the files a session made, shown in that session's own window.
2
+ // Loaded before agent-bar-panel.js, which calls attach() for every window it builds.
3
+ //
4
+ // - An agent is told an output folder in its request and puts what it makes
5
+ // there. This file lists that folder (GET /pinokio/session-files) and nothing
6
+ // else, so it works the same on this computer and on another device.
7
+ // - Once the session has a file, the window's header gets a switch: Terminal and
8
+ // Files, with the number of files. With no files there is no switch and the
9
+ // window is as it was. A shell, and a session that keeps no folder, never has one.
10
+ // - Files is laid over the terminal, which keeps its size and keeps running.
11
+ // Folders open in place, with the path above to go back up. A file opens in
12
+ // place: pictures, video, sound, text and markdown are shown; anything else
13
+ // says it cannot be shown. Every file has Save, which saves that one file to
14
+ // the device you are on.
15
+ // - Markdown is shown as a page. Its pictures are the session's own files, and a
16
+ // link to another file of the session opens that file here.
17
+ // - A file that arrives while you are on the terminal raises the number and
18
+ // shows a dot until you look. Files is where you left it when you come back.
19
+ (function (window, document) {
20
+ 'use strict';
21
+ if (window.PinokioAgentBarFiles) return;
22
+
23
+ const BASE = '/pinokio/session-files';
24
+ const ID = /^\d{8}-\d{6}-[a-f0-9]{8}$/;
25
+ const REFRESH_MS = 5000;
26
+ // text larger than this is saved, not shown
27
+ const MAX_TEXT_BYTES = 512 * 1024;
28
+ // a picture larger than this has an icon in the list, not itself
29
+ const MAX_THUMB_BYTES = 8 * 1024 * 1024;
30
+ const KINDS = [
31
+ ['image', /\.(png|jpe?g|gif|webp|avif|bmp|svg)$/i],
32
+ ['video', /\.(mp4|webm|mov|m4v)$/i],
33
+ ['audio', /\.(wav|mp3|flac|ogg|oga|m4a|aac|opus)$/i],
34
+ ['markdown', /\.(md|markdown)$/i],
35
+ ['text', /\.(txt|json|jsonl|csv|tsv|log|ya?ml|toml|ini|xml|html?|css|js|mjs|ts|py|sh|srt|vtt)$/i],
36
+ ];
37
+ const ICONS = {
38
+ image: 'fa-regular fa-image', video: 'fa-solid fa-film', audio: 'fa-solid fa-music',
39
+ markdown: 'fa-regular fa-file-lines', text: 'fa-regular fa-file-lines', other: 'fa-regular fa-file',
40
+ };
41
+ const kindOf = (name) => (KINDS.find(([, pattern]) => pattern.test(name)) || ['other'])[0];
42
+ const nameOf = (filePath) => filePath.split('/').pop();
43
+ const sizeText = (bytes) => {
44
+ if (bytes >= 1073741824) return `${(bytes / 1073741824).toFixed(1)} GB`;
45
+ if (bytes >= 1048576) return `${(bytes / 1048576).toFixed(1)} MB`;
46
+ return `${Math.max(1, Math.round(bytes / 1024))} KB`;
47
+ };
48
+ const byName = (a, b) => a.localeCompare(b, undefined, { numeric: true });
49
+
50
+ // The session a window shows, as the two things its files are found by: the
51
+ // folder it started in and its id (both are in the window's key).
52
+ function sessionOf(win) {
53
+ const session = win.session;
54
+ if (!session || session.kind !== 'agent') return null;
55
+ const parts = String(session.key || '').split('|');
56
+ if (parts.length < 3) return null;
57
+ const id = parts[parts.length - 1];
58
+ const cwd = parts.slice(1, -1).join('|');
59
+ return ID.test(id) && cwd ? { cwd, id } : null;
60
+ }
61
+ function addressOf(kind, who, filePath, download) {
62
+ const query = new URLSearchParams({ cwd: who.cwd, session: who.id });
63
+ if (download) query.set('download', '1');
64
+ const tail = filePath ? `/${kind}/${filePath.split('/').map(encodeURIComponent).join('/')}` : '';
65
+ return `${BASE}${tail}?${query.toString()}`;
66
+ }
67
+
68
+ function attach(win) {
69
+ const H = window.PinokioAgentBar && window.PinokioAgentBar.helpers;
70
+ if (!H || !win || !win.element || !win.part || win.files) return;
71
+ const { el, faIcon } = H;
72
+ const state = { pane: 'terminal', dir: '', file: '', files: [], more: false, seen: -1, shown: '' };
73
+ win.files = state;
74
+ let who = null;
75
+ let asked = 0;
76
+
77
+ // ---- the switch in the header ----
78
+ const views = el('div', 'agent-files__views');
79
+ views.setAttribute('role', 'group');
80
+ views.setAttribute('aria-label', 'View');
81
+ views.hidden = true;
82
+ const viewButton = (label, iconClass, pane) => {
83
+ const button = el('button', 'agent-files__view');
84
+ button.type = 'button';
85
+ button.title = label;
86
+ button.append(faIcon(iconClass), el('span', 'agent-files__word', label));
87
+ button.addEventListener('click', () => setPane(pane));
88
+ views.appendChild(button);
89
+ return button;
90
+ };
91
+ const terminalButton = viewButton('Terminal', 'fa-solid fa-terminal', 'terminal');
92
+ const filesButton = viewButton('Files', 'fa-regular fa-folder', 'files');
93
+ const count = el('span', 'agent-files__count');
94
+ const dot = el('span', 'agent-files__new');
95
+ dot.title = 'New since you last looked';
96
+ dot.hidden = true;
97
+ filesButton.append(count, dot);
98
+ win.part.open.before(views);
99
+
100
+ // ---- the view, over the terminal ----
101
+ const view = el('div', 'agent-files');
102
+ view.hidden = true;
103
+ const crumbs = el('div', 'agent-files__crumbs');
104
+ const body = el('div', 'agent-files__body');
105
+ view.append(crumbs, body);
106
+ win.part.frame.after(view);
107
+ const place = () => { view.style.top = `${win.part.frame.offsetTop}px`; };
108
+
109
+ const fileAt = (filePath) => state.files.find((file) => file.path === filePath) || null;
110
+ // what is directly inside a folder: its folders (with how many files each
111
+ // holds), then its files
112
+ function inside(dir) {
113
+ const prefix = dir ? `${dir}/` : '';
114
+ const folders = new Map();
115
+ const own = [];
116
+ state.files.forEach((file) => {
117
+ if (!file.path.startsWith(prefix)) return;
118
+ const rest = file.path.slice(prefix.length);
119
+ const cut = rest.indexOf('/');
120
+ if (cut < 0) own.push(file);
121
+ else folders.set(rest.slice(0, cut), (folders.get(rest.slice(0, cut)) || 0) + 1);
122
+ });
123
+ own.sort((a, b) => byName(nameOf(a.path), nameOf(b.path)));
124
+ return { folders: Array.from(folders.entries()).sort((a, b) => byName(a[0], b[0])), own };
125
+ }
126
+
127
+ function saveLink(file, className, withWord) {
128
+ const link = el('a', className);
129
+ const name = nameOf(file.path);
130
+ link.href = addressOf('file', who, file.path, true);
131
+ link.setAttribute('download', name);
132
+ link.title = `Save ${name} to this device`;
133
+ link.setAttribute('aria-label', `Save ${name}`);
134
+ link.appendChild(faIcon('fa-solid fa-download'));
135
+ if (withWord) link.appendChild(el('span', '', 'Save'));
136
+ return link;
137
+ }
138
+
139
+ function drawCrumbs(file) {
140
+ crumbs.textContent = '';
141
+ const parts = state.dir ? state.dir.split('/') : [];
142
+ const crumb = (label, dir, current) => {
143
+ const button = el('button', 'agent-files__crumb', label);
144
+ button.type = 'button';
145
+ if (current) button.setAttribute('aria-current', 'true');
146
+ else button.addEventListener('click', () => go({ dir, file: '' }));
147
+ crumbs.appendChild(button);
148
+ };
149
+ const slash = () => crumbs.appendChild(el('span', 'agent-files__slash', '/'));
150
+ crumb('Files', '', parts.length === 0 && !file);
151
+ parts.forEach((part, index) => {
152
+ slash();
153
+ crumb(part, parts.slice(0, index + 1).join('/'), index === parts.length - 1 && !file);
154
+ });
155
+ if (file) {
156
+ slash();
157
+ crumb(nameOf(file.path), '', true);
158
+ crumbs.append(el('span', 'agent-files__grow'), saveLink(file, 'agent-files__save', true));
159
+ }
160
+ // the end of the path is what you are looking at: keep it in sight
161
+ crumbs.scrollLeft = crumbs.scrollWidth;
162
+ }
163
+
164
+ function drawListing() {
165
+ body.className = 'agent-files__body agent-files__list';
166
+ body.textContent = '';
167
+ const { folders, own } = inside(state.dir);
168
+ folders.forEach(([name, total]) => {
169
+ const row = el('div', 'agent-files__row');
170
+ const open = el('button', 'agent-files__open');
171
+ open.type = 'button';
172
+ const icon = el('span', 'agent-files__icon agent-files__icon--folder');
173
+ icon.appendChild(faIcon('fa-solid fa-folder'));
174
+ const text = el('span', 'agent-files__text');
175
+ text.append(el('span', 'agent-files__name', name), el('span', 'agent-files__meta', `${total} file${total === 1 ? '' : 's'}`));
176
+ open.append(icon, text);
177
+ open.addEventListener('click', () => go({ dir: state.dir ? `${state.dir}/${name}` : name, file: '' }));
178
+ const arrow = el('span', 'agent-files__go');
179
+ arrow.appendChild(faIcon('fa-solid fa-chevron-right'));
180
+ row.append(open, arrow);
181
+ body.appendChild(row);
182
+ });
183
+ own.forEach((file) => {
184
+ const name = nameOf(file.path);
185
+ const kind = kindOf(name);
186
+ const row = el('div', 'agent-files__row');
187
+ const open = el('button', 'agent-files__open');
188
+ open.type = 'button';
189
+ open.title = name;
190
+ const icon = el('span', 'agent-files__icon');
191
+ if (kind === 'image' && file.size <= MAX_THUMB_BYTES) {
192
+ const img = document.createElement('img');
193
+ img.alt = '';
194
+ img.loading = 'lazy';
195
+ img.decoding = 'async';
196
+ img.src = addressOf('file', who, file.path);
197
+ img.addEventListener('error', () => { icon.textContent = ''; icon.appendChild(faIcon(ICONS.image)); });
198
+ icon.appendChild(img);
199
+ } else {
200
+ icon.appendChild(faIcon(ICONS[kind]));
201
+ }
202
+ const text = el('span', 'agent-files__text');
203
+ text.append(el('span', 'agent-files__name', name), el('span', 'agent-files__meta', sizeText(file.size)));
204
+ open.append(icon, text);
205
+ open.addEventListener('click', () => go({ file: file.path }));
206
+ row.append(open, saveLink(file, 'agent-files__rowsave', false));
207
+ body.appendChild(row);
208
+ });
209
+ if (state.more && !state.dir) body.appendChild(el('div', 'agent-files__note', 'There are more files than can be listed here.'));
210
+ }
211
+
212
+ const cannotShow = (file, why) => {
213
+ body.className = 'agent-files__body agent-files__none';
214
+ body.textContent = '';
215
+ body.append(
216
+ el('strong', '', nameOf(file.path)),
217
+ el('span', '', `${sizeText(file.size)} · ${why}`),
218
+ el('span', '', 'Save it to open it on this device.'),
219
+ );
220
+ };
221
+ // text and markdown are read, and may have been left for another file by then
222
+ const still = (file) => state.pane === 'files' && state.file === file.path;
223
+
224
+ function drawPreview(file) {
225
+ const name = nameOf(file.path);
226
+ const kind = kindOf(name);
227
+ const address = addressOf('file', who, file.path);
228
+ body.textContent = '';
229
+ if (kind === 'image') {
230
+ body.className = 'agent-files__body agent-files__picture';
231
+ const img = document.createElement('img');
232
+ img.alt = name;
233
+ img.src = address;
234
+ img.addEventListener('error', () => cannotShow(file, 'This picture could not be shown.'));
235
+ body.appendChild(img);
236
+ return;
237
+ }
238
+ if (kind === 'video' || kind === 'audio') {
239
+ body.className = `agent-files__body agent-files__media agent-files__media--${kind}`;
240
+ const media = document.createElement(kind);
241
+ media.controls = true;
242
+ media.preload = 'metadata';
243
+ if (kind === 'video') media.setAttribute('playsinline', '');
244
+ media.src = address;
245
+ body.appendChild(media);
246
+ return;
247
+ }
248
+ if (kind === 'markdown') {
249
+ body.className = 'agent-files__body agent-files__page';
250
+ fetch(addressOf('markdown', who, file.path))
251
+ .then((response) => response.json().catch(() => null).then((payload) => ({ response, payload })))
252
+ .then(({ response, payload }) => {
253
+ if (!still(file)) return;
254
+ if (!response.ok || !payload || !payload.ok) {
255
+ cannotShow(file, response.status === 413 ? 'This file is too large to show.' : 'This file could not be shown.');
256
+ return;
257
+ }
258
+ const page = el('div', 'agent-files__md');
259
+ // made by Pinokio from the file: no HTML of the file's own is in it
260
+ page.innerHTML = payload.html;
261
+ body.appendChild(page);
262
+ })
263
+ .catch(() => { if (still(file)) cannotShow(file, 'This file could not be shown.'); });
264
+ return;
265
+ }
266
+ if (kind === 'text' && file.size <= MAX_TEXT_BYTES) {
267
+ body.className = 'agent-files__body agent-files__plain';
268
+ fetch(address)
269
+ .then((response) => (response.ok ? response.text() : Promise.reject(new Error('unreadable'))))
270
+ .then((text) => {
271
+ if (!still(file)) return;
272
+ const pre = document.createElement('pre');
273
+ pre.textContent = text;
274
+ body.appendChild(pre);
275
+ })
276
+ .catch(() => { if (still(file)) cannotShow(file, 'This file could not be shown.'); });
277
+ return;
278
+ }
279
+ cannotShow(file, kind === 'text' ? 'This file is too large to show.' : "This kind of file can't be shown here.");
280
+ }
281
+ // a link in a markdown page to another file of this session opens it here
282
+ body.addEventListener('click', (event) => {
283
+ const link = event.target && event.target.closest ? event.target.closest('a[data-session-file]') : null;
284
+ if (!link) return;
285
+ event.preventDefault();
286
+ const target = fileAt(link.getAttribute('data-session-file'));
287
+ if (target) go({ file: target.path, dir: target.path.split('/').slice(0, -1).join('/') });
288
+ });
289
+
290
+ function drawView() {
291
+ if (state.pane !== 'files') return;
292
+ place();
293
+ // what was open may be gone: a file falls back to its folder, a folder to
294
+ // the nearest one that is still there
295
+ let file = state.file ? fileAt(state.file) : null;
296
+ if (state.file && !file) state.file = '';
297
+ while (state.dir && !state.files.some((item) => item.path.startsWith(`${state.dir}/`))) {
298
+ state.dir = state.dir.split('/').slice(0, -1).join('/');
299
+ }
300
+ file = state.file ? fileAt(state.file) : null;
301
+ // a file on screen is left alone while it is still the same file: a look
302
+ // at the folder every few seconds must not restart a video
303
+ const shown = file ? `file:${file.path}:${file.size}:${file.modified}` : `dir:${state.dir}:${state.more}:${JSON.stringify(inside(state.dir))}`;
304
+ if (shown === state.shown) return;
305
+ state.shown = shown;
306
+ drawCrumbs(file);
307
+ if (file) drawPreview(file);
308
+ else drawListing();
309
+ body.scrollTop = 0;
310
+ }
311
+
312
+ function drawSwitch() {
313
+ const total = state.files.length;
314
+ if (total === 0) state.pane = 'terminal';
315
+ if (state.pane === 'files' || state.seen < 0 || state.seen > total) state.seen = total;
316
+ views.hidden = total === 0;
317
+ // with the switch in a narrow header, Stop is its icon (see the styles)
318
+ win.element.dataset.files = String(total > 0);
319
+ count.textContent = String(total);
320
+ filesButton.title = `Files this session made (${total})`;
321
+ filesButton.setAttribute('aria-label', `Files, ${total}`);
322
+ dot.hidden = !(state.pane === 'terminal' && total > state.seen);
323
+ terminalButton.setAttribute('aria-pressed', String(state.pane === 'terminal'));
324
+ filesButton.setAttribute('aria-pressed', String(state.pane === 'files'));
325
+ view.hidden = state.pane !== 'files';
326
+ win.element.dataset.view = state.pane;
327
+ if (view.hidden) {
328
+ // nothing keeps playing behind the terminal
329
+ state.shown = '';
330
+ body.textContent = '';
331
+ }
332
+ }
333
+ function draw() {
334
+ drawSwitch();
335
+ drawView();
336
+ }
337
+ function go(patch) {
338
+ Object.assign(state, patch);
339
+ draw();
340
+ }
341
+ function setPane(pane) {
342
+ if (pane === 'files') {
343
+ // the terminal lets go of the keyboard
344
+ try {
345
+ const inner = win.part.frame.contentDocument;
346
+ if (inner && inner.activeElement && inner.activeElement.blur) inner.activeElement.blur();
347
+ } catch (_) {}
348
+ }
349
+ go({ pane });
350
+ }
351
+
352
+ // ---- reading the folder ----
353
+ function look() {
354
+ const next = sessionOf(win);
355
+ if (!next) {
356
+ who = null;
357
+ if (state.files.length) {
358
+ state.files = [];
359
+ draw();
360
+ }
361
+ return Promise.resolve();
362
+ }
363
+ who = next;
364
+ const mine = asked += 1;
365
+ return fetch(addressOf('', who, ''))
366
+ .then((response) => (response.ok ? response.json() : null))
367
+ .then((payload) => {
368
+ // a slower, earlier answer never replaces a later one
369
+ if (mine !== asked || !payload || !payload.ok || !Array.isArray(payload.files)) return;
370
+ state.files = payload.files.filter((file) => file && typeof file.path === 'string');
371
+ state.more = payload.more === true;
372
+ draw();
373
+ })
374
+ .catch(() => {});
375
+ }
376
+ const onScreen = () => win.element.dataset.open === 'true' && !document.hidden;
377
+ const timer = window.setInterval(() => {
378
+ if (!win.element.isConnected) {
379
+ window.clearInterval(timer);
380
+ return;
381
+ }
382
+ if (onScreen()) look();
383
+ }, REFRESH_MS);
384
+ // at once when the window comes on screen, not at the next look
385
+ if (typeof window.MutationObserver === 'function') {
386
+ new window.MutationObserver(() => { if (onScreen()) look(); })
387
+ .observe(win.element, { attributes: true, attributeFilter: ['data-open'] });
388
+ }
389
+ if (typeof window.ResizeObserver === 'function') new window.ResizeObserver(place).observe(win.element);
390
+ win.files.look = look;
391
+ }
392
+
393
+ window.PinokioAgentBarFiles = { attach };
394
+ })(window, document);
@@ -9,7 +9,8 @@
9
9
  // apps, and shells opened inside an app. An app's own running scripts are not
10
10
  // listed. Grouped by where it runs. Each row can be opened or stopped.
11
11
  // - App pages: the app's own bar (agent-bar-app.js) opens its sessions through
12
- // window.PinokioAgentDock, which this file provides there.
12
+ // window.PinokioAgentDock, which this file provides there. Its "N running" is
13
+ // the same count and the same list, with the app's own sessions first.
13
14
  // - Every session you open becomes a tab along the bottom, and each can have its
14
15
  // own window on screen. The windows sit side by side in a row above the tabs,
15
16
  // the first against the right edge and each new one to its left (their tabs
@@ -40,6 +41,7 @@
40
41
  const STORE_ASKED = 'pinokio.agentBar.panel.asked';
41
42
  const OPEN_CLASS = 'agent-bar-panel-open';
42
43
  const STILL_CLASS = 'agent-bar-panel-still';
44
+ const KEYBOARD_INSET = '--agent-dock-keyboard';
43
45
  // how long a session that was just started may take to show up as running
44
46
  const START_GRACE_MS = 20000;
45
47
  const REFRESH_MS = 5000;
@@ -157,6 +159,21 @@
157
159
  };
158
160
  const openTabs = () => tabs.filter((tab) => tab.open);
159
161
  const saveTabs = () => write(STORE_TABS, tabs);
162
+ // On a phone a window covers the whole page, so one that came back on a new
163
+ // page would hide that anything changed. There it steps aside to its tab and
164
+ // the new page shows. Loading the same page again brings it back as it was.
165
+ const STORE_PAGE = `pinokio.agentBar.dock.page${paneId ? `:${paneId}` : ''}`;
166
+ const pageNow = () => `${window.location.pathname}${window.location.search}`;
167
+ const PAGE = pageNow();
168
+ const cameFrom = read(STORE_PAGE, '');
169
+ write(STORE_PAGE, PAGE);
170
+ // a page may rewrite its own address after loading (the home page adds its
171
+ // sort order): what is remembered is the address it was left at
172
+ window.addEventListener('pagehide', () => write(STORE_PAGE, pageNow()));
173
+ if (cameFrom && cameFrom !== PAGE && window.innerWidth <= 768 && tabs.some((tab) => tab.open)) {
174
+ tabs.forEach((tab) => { tab.open = false; });
175
+ saveTabs();
176
+ }
160
177
  // A window was docked to the side on the page before this one: keep its room
161
178
  // from the first paint, so the page does not jump when it comes back.
162
179
  if (tabs.some((tab) => tab.open && tab.docked)) root.classList.add(OPEN_CLASS, STILL_CLASS);
@@ -340,8 +357,8 @@
340
357
  pruneTabs();
341
358
  renderChip();
342
359
  if (menuOpen) renderMenu();
343
- // an app page's bar keeps its own count of the app's sessions: it is
344
- // handed the list that was just read
360
+ // an app page's bar draws its own "N running": it is told, and handed
361
+ // the list that was just read
345
362
  window.dispatchEvent(new window.CustomEvent('pinokio:sessions-changed', { detail: { shells } }));
346
363
  }
347
364
  return true;
@@ -376,7 +393,7 @@
376
393
  let menu = null;
377
394
 
378
395
  function ensureChip() {
379
- if (APP) return false; // The app's own bar has its count and list
396
+ if (APP) return false; // the app's own bar draws the count and the list (see offerDock)
380
397
  const top = document.querySelector('.agent-bar__top');
381
398
  const inner = document.querySelector('.agent-bar__inner');
382
399
  if (!top || !inner) return false;
@@ -422,6 +439,8 @@
422
439
  // page, when the pointer first reaches "N running" or the list is first opened.
423
440
  let appIcons = null;
424
441
  let appTitles = null;
442
+ // called once they are in, for a list drawn somewhere other than this file's menu
443
+ let iconsArrived = null;
425
444
  function loadAppIcons() {
426
445
  if (appIcons) return;
427
446
  appIcons = new Map();
@@ -434,6 +453,7 @@
434
453
  if (app && app.name && app.title) appTitles.set(app.name, app.title);
435
454
  });
436
455
  if (menuOpen) renderMenu();
456
+ if (iconsArrived) iconsArrived();
437
457
  })
438
458
  .catch(() => {});
439
459
  }
@@ -459,7 +479,9 @@
459
479
  return title;
460
480
  }
461
481
 
462
- function rowFor(session) {
482
+ // `on` is given when the list is drawn in the app page's bar: `picked` closes
483
+ // that bar's list, `changed` draws it again.
484
+ function rowFor(session, on) {
463
485
  const icon = H.toolIcon({ iconSrc: session.iconSrc, iconClass: session.iconClass });
464
486
  const said = asked[session.key];
465
487
  // Two lines: what was asked (the agent's name when that is not known) with the
@@ -479,23 +501,36 @@
479
501
  );
480
502
  row.append(icon, text);
481
503
  row.addEventListener('click', () => {
482
- setMenu(false);
504
+ if (on && on.picked) on.picked();
505
+ else setMenu(false);
483
506
  show(session);
484
507
  });
485
508
  // Stop on the row, shown on hover; a second click confirms
486
509
  const wrap = H.el('div', 'agent-dock__listrow');
487
- const stop = stopButton('agent-dock__liststop', () => stopSession(session));
510
+ const stop = stopButton('agent-dock__liststop', () => stopSession(session, on && on.changed));
488
511
  wrap.append(row, stop);
489
512
  return wrap;
490
513
  }
491
514
 
492
515
  function renderMenu() {
493
- menu.textContent = '';
494
- const list = listed();
516
+ drawList(menu);
517
+ }
518
+ // The list, into `target`. On an app's page that app's own sessions come
519
+ // first; everything else follows as on any other page.
520
+ function drawList(target, on) {
521
+ target.textContent = '';
522
+ const here = APP && appCwd ? cleanPath(appCwd) : '';
523
+ const isHere = (session) => Boolean(here) && session.placeKind === 'app' && cleanPath(session.rootPath) === here;
524
+ const own = listed().filter(isHere);
525
+ if (own.length) {
526
+ target.appendChild(H.el('div', 'agent-bar__menu-title', 'This app'));
527
+ own.forEach((session) => target.appendChild(rowFor(session, on)));
528
+ }
529
+ const list = listed().filter((session) => !isHere(session));
495
530
  const workspaces = list.filter((session) => session.placeKind === 'workspace');
496
531
  if (workspaces.length) {
497
- menu.appendChild(H.el('div', 'agent-bar__menu-title', 'Workspaces'));
498
- workspaces.forEach((session) => menu.appendChild(rowFor(session)));
532
+ target.appendChild(H.el('div', 'agent-bar__menu-title', 'Workspaces'));
533
+ workspaces.forEach((session) => target.appendChild(rowFor(session, on)));
499
534
  }
500
535
  const places = [];
501
536
  list.filter((session) => session.placeKind !== 'workspace').forEach((session) => {
@@ -508,8 +543,8 @@
508
543
  place.sessions.push(session);
509
544
  });
510
545
  places.forEach((place) => {
511
- menu.appendChild(placeTitle(place));
512
- place.sessions.forEach((session) => menu.appendChild(rowFor(session)));
546
+ target.appendChild(placeTitle(place));
547
+ place.sessions.forEach((session) => target.appendChild(rowFor(session, on)));
513
548
  });
514
549
  }
515
550
 
@@ -613,6 +648,8 @@
613
648
  // pressed anywhere, it comes to the front (a press inside its page is seen in start)
614
649
  element.addEventListener('pointerdown', () => { if (frontKey !== key) focus(key); }, true);
615
650
  wins.set(key, win);
651
+ // the session's files, shown in this window (agent-bar-files.js)
652
+ if (window.PinokioAgentBarFiles) window.PinokioAgentBarFiles.attach(win);
616
653
  return win;
617
654
  }
618
655
 
@@ -988,6 +1025,41 @@
988
1025
  right: 0 !important;
989
1026
  }
990
1027
  `;
1028
+ function keepTerminalTapInFrame(panel, doc) {
1029
+ let releaseTimer = null;
1030
+ let releaseLayout;
1031
+ const releaseInset = () => {
1032
+ if (releaseTimer === null) return;
1033
+ window.clearTimeout(releaseTimer);
1034
+ releaseTimer = null;
1035
+ panel.style.removeProperty(KEYBOARD_INSET);
1036
+ releaseLayout?.();
1037
+ releaseLayout = null;
1038
+ };
1039
+ doc.addEventListener('pointerdown', (event) => {
1040
+ if (!narrow() || !event.isPrimary || (event.pointerType !== 'touch' && event.pointerType !== 'pen')
1041
+ || !event.target.closest?.('#terminal')) return;
1042
+ // Focusing the terminal on touch-down can open the keyboard before Safari
1043
+ // sends the tap's mouse events. Keep the iframe under that tap until it ends.
1044
+ if (releaseTimer === null) {
1045
+ panel.style.setProperty(KEYBOARD_INSET, root.style.getPropertyValue(KEYBOARD_INSET) || '0px');
1046
+ if (window.frameElement) releaseLayout = window.parent.PinokioLayoutViewport?.hold();
1047
+ }
1048
+ // Recover if the browser never sends a click or cancellation.
1049
+ window.clearTimeout(releaseTimer);
1050
+ releaseTimer = window.setTimeout(releaseInset, 1000);
1051
+ }, true);
1052
+ doc.addEventListener('click', () => {
1053
+ const timer = releaseTimer;
1054
+ if (timer === null) return;
1055
+ window.requestAnimationFrame(() => {
1056
+ if (releaseTimer === timer) releaseInset();
1057
+ });
1058
+ }, true);
1059
+ doc.addEventListener('pointercancel', releaseInset, true);
1060
+ doc.defaultView.addEventListener('pagehide', releaseInset);
1061
+ }
1062
+
991
1063
  function embed(win) {
992
1064
  let doc = null;
993
1065
  try {
@@ -1002,6 +1074,7 @@
1002
1074
  style.textContent = EMBED_STYLE;
1003
1075
  doc.head.appendChild(style);
1004
1076
  doc.documentElement.classList.add('pinokio-dock-embed');
1077
+ if (window.visualViewport) keepTerminalTapInFrame(win.element, doc);
1005
1078
  }
1006
1079
  watchTerminal(win, doc);
1007
1080
  return doc;
@@ -1281,6 +1354,23 @@
1281
1354
  const session = url ? sessionAt(url) : null;
1282
1355
  return session ? stopButton('agent-dock__liststop', () => stopSession(session, sent)) : null;
1283
1356
  },
1357
+ // "N running" in the app's bar: how many sessions are running, anywhere
1358
+ count: () => listed().length,
1359
+ // The list under it, drawn into the bar's own menu. `on.picked` is called
1360
+ // when a row is chosen, `on.changed` when a session was stopped from a row.
1361
+ list: (target, on) => {
1362
+ if (!appIcons) {
1363
+ // drawn once more when the apps' icons and titles are in, if it is still up
1364
+ iconsArrived = () => {
1365
+ iconsArrived = null;
1366
+ if (target.isConnected && !target.hidden && target.dataset.menu === 'running') drawList(target, on);
1367
+ };
1368
+ loadAppIcons();
1369
+ }
1370
+ drawList(target, on);
1371
+ },
1372
+ // reads what is running again; the bar is told if it changed
1373
+ look: () => refresh(),
1284
1374
  // the addresses of every session that has a window up
1285
1375
  openUrls: () => openTabs().map((tab) => {
1286
1376
  const win = wins.get(tab.key);
@@ -1891,7 +1981,7 @@
1891
1981
  if (vv) {
1892
1982
  const syncKeyboard = () => {
1893
1983
  const covered = Math.max(0, Math.round(window.innerHeight - vv.height - vv.offsetTop));
1894
- root.style.setProperty('--agent-dock-keyboard', `${covered}px`);
1984
+ root.style.setProperty(KEYBOARD_INSET, `${covered}px`);
1895
1985
  };
1896
1986
  vv.addEventListener('resize', syncKeyboard);
1897
1987
  vv.addEventListener('scroll', syncKeyboard);
@@ -0,0 +1,49 @@
1
+ // The registry page shown in the Explore pane (the Explore and Create pages)
2
+ // can ask this Pinokio to open its own bar with a request filled in, instead of
3
+ // sending the visitor to "a Pinokio somewhere" in a new window:
4
+ //
5
+ // registry -> here { e: 'pinokio:create', requestId, type, prompt, name? }
6
+ // here -> registry { e: 'pinokio:create:result', requestId, handled: true }
7
+ //
8
+ // The registry waits briefly for the result and falls back to its own
9
+ // "Open in Pinokio" panel when none comes, so a Pinokio without this file keeps
10
+ // working. On a request this page goes to the home page's pre-filled link
11
+ // (?universal&type&prompt&name), which fills the bar in and sends nothing.
12
+ (function () {
13
+ if (window.__pinokioExploreCreateHandoffInstalled) return;
14
+ window.__pinokioExploreCreateHandoffInstalled = true;
15
+
16
+ var TYPES = ['create_app', 'create_plugin', 'ask'];
17
+ // A longer link is refused by the server; the registry then shows its panel.
18
+ var MAX_LINK = 8000;
19
+
20
+ window.addEventListener('message', function (event) {
21
+ var data = event && event.data;
22
+ if (!data || typeof data !== 'object' || data.e !== 'pinokio:create') return;
23
+
24
+ // Only the registry iframe of this page, at the origin it was given, is heard.
25
+ var frame = document.querySelector('iframe[name="pinokio-explore"]');
26
+ if (!frame || event.source !== frame.contentWindow) return;
27
+ try {
28
+ if (event.origin !== new URL(frame.getAttribute('src') || '', window.location.href).origin) return;
29
+ } catch (_) {
30
+ return;
31
+ }
32
+
33
+ if (TYPES.indexOf(data.type) === -1) return;
34
+ var target = new URL('/home', window.location.origin);
35
+ target.searchParams.set('universal', '');
36
+ target.searchParams.set('type', data.type);
37
+ if (typeof data.prompt === 'string' && data.prompt.trim()) target.searchParams.set('prompt', data.prompt.trim());
38
+ if (typeof data.name === 'string' && data.name.trim()) target.searchParams.set('name', data.name.trim());
39
+ if (target.toString().length > MAX_LINK) return;
40
+
41
+ try {
42
+ event.source.postMessage(
43
+ { e: 'pinokio:create:result', requestId: data.requestId, handled: true },
44
+ event.origin
45
+ );
46
+ } catch (_) {}
47
+ window.location.assign(target.toString());
48
+ });
49
+ })();