pinokiod 8.3.6 → 8.3.9

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.
@@ -0,0 +1,142 @@
1
+ /* Agent bar: attaching files (see agent-bar-attach.js). Uses the bar's own
2
+ colors, so light and dark follow agent-bar.css. */
3
+
4
+ /* the paperclip: only while you are in the bar, never in Download */
5
+ .agent-bar .agent-bar__attach {
6
+ display: none;
7
+ flex: none;
8
+ align-self: flex-end;
9
+ width: 22px;
10
+ height: 22px;
11
+ align-items: center;
12
+ justify-content: center;
13
+ padding: 0;
14
+ border: 0;
15
+ border-radius: 11px;
16
+ background: transparent;
17
+ font-size: 12px;
18
+ }
19
+ .agent-bar[data-engaged="true"]:not([data-item="download"]) .agent-bar__attach {
20
+ display: flex;
21
+ }
22
+ .agent-bar button.agent-bar__attach {
23
+ color: var(--agent-bar-muted);
24
+ }
25
+ .agent-bar button.agent-bar__attach:hover {
26
+ background: var(--agent-bar-chip);
27
+ color: var(--agent-bar-text);
28
+ }
29
+ .agent-bar button.agent-bar__attach:disabled {
30
+ opacity: 0.4;
31
+ }
32
+
33
+ /* what is attached: a row of the same box, under the prompt */
34
+ .agent-bar .agent-bar__files {
35
+ display: flex;
36
+ flex-wrap: wrap;
37
+ gap: 6px;
38
+ /* two rows of chips, then it scrolls */
39
+ max-height: 68px;
40
+ box-sizing: border-box;
41
+ overflow-y: auto;
42
+ padding: 6px 10px 7px;
43
+ border-top: 1px solid var(--agent-bar-border);
44
+ }
45
+ .agent-bar .agent-bar__file {
46
+ display: inline-flex;
47
+ align-items: center;
48
+ gap: 6px;
49
+ max-width: 220px;
50
+ height: 24px;
51
+ box-sizing: border-box;
52
+ padding: 0 3px 0 4px;
53
+ border-radius: 7px;
54
+ background: var(--agent-bar-chip);
55
+ font: 500 12px system-ui, -apple-system, "Segoe UI", sans-serif;
56
+ color: var(--agent-bar-text);
57
+ }
58
+ .agent-bar .agent-bar__file-thumb {
59
+ flex: none;
60
+ width: 16px;
61
+ height: 16px;
62
+ border-radius: 4px;
63
+ object-fit: cover;
64
+ }
65
+ .agent-bar .agent-bar__file-icon {
66
+ flex: none;
67
+ width: 16px;
68
+ text-align: center;
69
+ font-size: 12px;
70
+ color: var(--agent-bar-muted);
71
+ }
72
+ .agent-bar .agent-bar__file-name {
73
+ min-width: 0;
74
+ overflow: hidden;
75
+ text-overflow: ellipsis;
76
+ white-space: nowrap;
77
+ }
78
+ .agent-bar .agent-bar__file-x {
79
+ flex: none;
80
+ width: 18px;
81
+ height: 18px;
82
+ display: inline-flex;
83
+ align-items: center;
84
+ justify-content: center;
85
+ padding: 0;
86
+ border: 0;
87
+ border-radius: 5px;
88
+ background: transparent;
89
+ font-size: 10px;
90
+ }
91
+ .agent-bar button.agent-bar__file-x {
92
+ color: var(--agent-bar-muted);
93
+ }
94
+ .agent-bar button.agent-bar__file-x:hover {
95
+ background: var(--agent-bar-chip-hover);
96
+ color: var(--agent-bar-text);
97
+ }
98
+ .agent-bar button.agent-bar__file-x:disabled {
99
+ opacity: 0.4;
100
+ }
101
+
102
+ /* a file is being dragged over the page: the box says where it goes */
103
+ .agent-bar .agent-bar__drop {
104
+ display: none;
105
+ position: absolute;
106
+ inset: -1px;
107
+ z-index: 2;
108
+ align-items: center;
109
+ justify-content: center;
110
+ gap: 7px;
111
+ border: 1.5px dashed var(--agent-bar-border-strong);
112
+ border-radius: 15px;
113
+ background: var(--agent-bar-surface);
114
+ font: 600 12px system-ui, -apple-system, "Segoe UI", sans-serif;
115
+ color: var(--agent-bar-muted);
116
+ pointer-events: none;
117
+ }
118
+ .agent-bar[data-drag="true"] .agent-bar__drop {
119
+ display: flex;
120
+ }
121
+ .agent-bar[data-drag-over="true"] .agent-bar__drop {
122
+ border-style: solid;
123
+ color: var(--agent-bar-text);
124
+ background: color-mix(in srgb, var(--agent-bar-surface) 88%, var(--agent-bar-text));
125
+ }
126
+
127
+ /* phones: one line of chips that scrolls sideways, so the box stays short */
128
+ @media only screen and (max-width: 768px) {
129
+ .agent-bar .agent-bar__files {
130
+ flex-wrap: nowrap;
131
+ overflow-x: auto;
132
+ overflow-y: hidden;
133
+ scrollbar-width: none;
134
+ }
135
+ .agent-bar .agent-bar__files::-webkit-scrollbar {
136
+ display: none;
137
+ }
138
+ .agent-bar .agent-bar__file {
139
+ flex: none;
140
+ max-width: 150px;
141
+ }
142
+ }
@@ -0,0 +1,299 @@
1
+ // Agent bar: attaching files to what you send. On pages with the main sidebar
2
+ // (Ask, Create app, Create plugin) and on app pages ("Ask Pinokio about <app>").
3
+ //
4
+ // - Three ways in: the paperclip (shown only while you are in the bar), dropping
5
+ // a file on the top bar, and pasting a file or a screenshot into the prompt.
6
+ // Pasted text stays text.
7
+ // - What is attached shows as chips in the bar's own box, under the prompt.
8
+ // - Download takes a Git URL, not a prompt: nothing can be attached there.
9
+ // - Send: agent-bar.js asks PinokioAgentBar.attachments.token(). This file
10
+ // uploads the files the same way the dialog does (POST /create-upload) and
11
+ // returns the upload token, which Pinokio then copies into the new folder.
12
+ // Nothing is uploaded before Send.
13
+ // - Send on an app page: there is no new folder, the agent works in the app's
14
+ // own. agent-bar-app.js asks PinokioAgentBar.attachments.paths(appId). This
15
+ // file uploads the files with the request Pinokio already has for giving an
16
+ // app files (POST /apps/<id>/upload), which puts them in the app's
17
+ // .pinokio-temp/uploads folder (kept out of the app's git history), and
18
+ // returns where each one is, so the prompt can name them.
19
+ (function (window, document) {
20
+ 'use strict';
21
+ if (window.PinokioAgentBarAttach) return;
22
+ window.PinokioAgentBarAttach = true;
23
+
24
+ let bar = null;
25
+ let H = null;
26
+ const files = []; // { file, name, url }
27
+ let root = null;
28
+ let field = null;
29
+ let input = null;
30
+ let holders = []; // what can hold the bar: the top bar, and on a phone's app page the strip
31
+ let clip = null;
32
+ let row = null;
33
+ let picker = null;
34
+ let dragDepth = 0;
35
+
36
+ const isBusy = () => root.dataset.busy === 'true';
37
+ // Download has no prompt and no agent; a bar that is not on screen takes nothing.
38
+ const blocked = () => root.dataset.item === 'download' || isBusy()
39
+ || field.getBoundingClientRect().width === 0;
40
+ const shown = () => files.length > 0 && root.dataset.item !== 'download';
41
+
42
+ // the same file twice is one file (same rule as the dialog)
43
+ const fileKey = (file) => `${file.name || ''}:${file.size || 0}:${file.lastModified || 0}`;
44
+
45
+ // Two different files with the same name would overwrite each other in the folder.
46
+ function freeName(name) {
47
+ const taken = new Set(files.map((entry) => entry.name));
48
+ if (!taken.has(name)) return name;
49
+ const dot = name.lastIndexOf('.');
50
+ const stem = dot > 0 ? name.slice(0, dot) : name;
51
+ const ext = dot > 0 ? name.slice(dot) : '';
52
+ for (let i = 2; i < 1000; i += 1) {
53
+ const candidate = `${stem} ${i}${ext}`;
54
+ if (!taken.has(candidate)) return candidate;
55
+ }
56
+ return `${stem} ${Date.now()}${ext}`;
57
+ }
58
+
59
+ function add(list) {
60
+ if (blocked()) return;
61
+ const existing = new Set(files.map((entry) => fileKey(entry.file)));
62
+ Array.from(list || []).forEach((file) => {
63
+ if (!file || typeof file.name !== 'string') return;
64
+ const key = fileKey(file);
65
+ if (existing.has(key)) return;
66
+ existing.add(key);
67
+ files.push({
68
+ file,
69
+ name: freeName(file.name || 'file'),
70
+ url: file.type && file.type.startsWith('image/') ? URL.createObjectURL(file) : '',
71
+ });
72
+ });
73
+ renderChips();
74
+ input.focus();
75
+ }
76
+
77
+ function remove(entry) {
78
+ const index = files.indexOf(entry);
79
+ if (index < 0 || isBusy()) return;
80
+ if (entry.url) URL.revokeObjectURL(entry.url);
81
+ files.splice(index, 1);
82
+ renderChips();
83
+ input.focus();
84
+ }
85
+
86
+ function clear() {
87
+ files.splice(0).forEach((entry) => { if (entry.url) URL.revokeObjectURL(entry.url); });
88
+ if (row) renderChips();
89
+ }
90
+
91
+ function renderChips() {
92
+ row.textContent = '';
93
+ files.forEach((entry) => {
94
+ const chip = H.el('span', 'agent-bar__file');
95
+ if (entry.url) {
96
+ const img = H.el('img', 'agent-bar__file-thumb');
97
+ img.alt = '';
98
+ img.src = entry.url;
99
+ chip.appendChild(img);
100
+ } else {
101
+ chip.appendChild(H.faIcon('fa-regular fa-file agent-bar__file-icon'));
102
+ }
103
+ const name = H.el('span', 'agent-bar__file-name', entry.name);
104
+ name.title = entry.name;
105
+ const x = H.el('button', 'agent-bar__file-x');
106
+ x.type = 'button';
107
+ x.title = `Remove ${entry.name}`;
108
+ x.setAttribute('aria-label', x.title);
109
+ x.appendChild(H.faIcon('fa-solid fa-xmark'));
110
+ x.addEventListener('click', () => remove(entry));
111
+ chip.append(name, x);
112
+ row.appendChild(chip);
113
+ });
114
+ sync();
115
+ }
116
+
117
+ // The box grew or shrank: the same bookkeeping agent-bar.js does after its own
118
+ // changes (see fitInput and syncRaise there).
119
+ function sync() {
120
+ const show = shown();
121
+ row.hidden = !show;
122
+ const busy = isBusy();
123
+ clip.disabled = busy;
124
+ row.querySelectorAll('.agent-bar__file-x').forEach((button) => { button.disabled = busy; });
125
+ const height = field.offsetHeight;
126
+ root.style.setProperty('--agent-bar-field-h', `${height}px`);
127
+ root.dataset.tall = String(height > 32);
128
+ const header = root.closest('header');
129
+ if (header) {
130
+ const menuOpen = Boolean(root.querySelector('.agent-bar__menu:not([hidden])'));
131
+ const note = root.querySelector('.agent-bar__note');
132
+ const noteOpen = Boolean(note && !note.hidden);
133
+ header.classList.toggle('agent-bar-raised', height > 32 || menuOpen || noteOpen);
134
+ }
135
+ }
136
+
137
+ // Same request as the dialog's uploadFiles. The server answers a failure with
138
+ // { error }, which is shown as its message instead of as raw JSON.
139
+ async function token() {
140
+ if (!root || !shown()) return '';
141
+ const form = new FormData();
142
+ files.forEach((entry) => form.append('files', entry.file, entry.name));
143
+ const response = await fetch('/create-upload', { method: 'POST', body: form });
144
+ const text = await response.text();
145
+ let payload = null;
146
+ try {
147
+ payload = JSON.parse(text);
148
+ } catch (_) {}
149
+ if (!response.ok || !payload || payload.error || !payload.uploadToken) {
150
+ const message = payload && typeof payload.error === 'string' && payload.error
151
+ ? payload.error
152
+ : (!payload && text ? text : 'Failed to upload files.');
153
+ throw new Error(message);
154
+ }
155
+ return payload.uploadToken;
156
+ }
157
+
158
+ // App pages: into the app's own temp folder. Resolves to the files' paths there.
159
+ async function paths(appId) {
160
+ if (!root || !shown()) return [];
161
+ const form = new FormData();
162
+ files.forEach((entry) => form.append('files', entry.file, entry.name));
163
+ const response = await fetch(`/apps/${encodeURIComponent(appId)}/upload`, { method: 'POST', body: form });
164
+ const payload = await response.json().catch(() => null);
165
+ const stored = payload && Array.isArray(payload.files)
166
+ ? payload.files.map((file) => (file && typeof file.path === 'string' ? file.path : '')).filter(Boolean)
167
+ : [];
168
+ if (!response.ok || stored.length !== files.length) {
169
+ throw new Error(payload && typeof payload.error === 'string' && payload.error ? payload.error : 'Failed to upload files.');
170
+ }
171
+ return stored;
172
+ }
173
+
174
+ const hasFiles = (event) => Boolean(event.dataTransfer)
175
+ && Array.from(event.dataTransfer.types || []).includes('Files');
176
+
177
+ function setDrag(on, over) {
178
+ root.dataset.drag = String(Boolean(on));
179
+ root.dataset.dragOver = String(Boolean(on && over));
180
+ if (!on) dragDepth = 0;
181
+ }
182
+
183
+ function mount() {
184
+ root = document.querySelector('.agent-bar');
185
+ const top = root && root.querySelector('.agent-bar__top');
186
+ field = root && root.querySelector('.agent-bar__field');
187
+ input = root && root.querySelector('.agent-bar__input');
188
+ if (!top || !field || !input) return false;
189
+ holders = [document.querySelector('body > header.navheader'), document.querySelector('.mobile-bottom-nav')].filter(Boolean);
190
+ const overHolder = (target) => holders.some((holder) => holder.contains(target));
191
+
192
+ // the paperclip: right after the prompt, only while you are in the bar
193
+ clip = H.el('button', 'agent-bar__attach');
194
+ clip.type = 'button';
195
+ clip.title = 'Attach files';
196
+ clip.setAttribute('aria-label', 'Attach files');
197
+ clip.appendChild(H.faIcon('fa-solid fa-paperclip'));
198
+ top.insertBefore(clip, input.nextSibling);
199
+
200
+ picker = H.el('input');
201
+ picker.type = 'file';
202
+ picker.multiple = true;
203
+ picker.hidden = true;
204
+ picker.tabIndex = -1;
205
+ root.querySelector('.agent-bar__inner').appendChild(picker);
206
+ clip.addEventListener('click', () => { if (!blocked()) picker.click(); });
207
+ picker.addEventListener('change', () => {
208
+ add(picker.files);
209
+ picker.value = '';
210
+ });
211
+
212
+ // what is attached: a row of the same box, between the prompt and the folder name
213
+ row = H.el('div', 'agent-bar__files');
214
+ row.hidden = true;
215
+ row.setAttribute('aria-label', 'Attached files');
216
+ field.insertBefore(row, top.nextSibling);
217
+
218
+ // shown over the box while a file is dragged over the page
219
+ const drop = H.el('div', 'agent-bar__drop');
220
+ drop.append(H.faIcon('fa-solid fa-paperclip'), H.el('span', '', 'Drop to attach'));
221
+ field.appendChild(drop);
222
+
223
+ // paste a file or a screenshot; pasted text stays text
224
+ input.addEventListener('paste', (event) => {
225
+ const data = event.clipboardData;
226
+ if (!data || blocked()) return;
227
+ const list = Array.from(data.files || []);
228
+ if (!list.length || data.getData('text/plain')) return;
229
+ event.preventDefault();
230
+ add(list);
231
+ });
232
+
233
+ // drop: the whole top bar takes the file, so the target is easy to hit
234
+ window.addEventListener('dragenter', (event) => {
235
+ if (!hasFiles(event) || blocked()) return;
236
+ dragDepth += 1;
237
+ setDrag(true, overHolder(event.target));
238
+ });
239
+ window.addEventListener('dragleave', (event) => {
240
+ if (!hasFiles(event)) return;
241
+ dragDepth = Math.max(0, dragDepth - 1);
242
+ if (!dragDepth) setDrag(false);
243
+ });
244
+ window.addEventListener('dragend', () => setDrag(false));
245
+ window.addEventListener('drop', () => setDrag(false));
246
+ holders.forEach((holder) => {
247
+ holder.addEventListener('dragover', (event) => {
248
+ if (!hasFiles(event) || blocked()) return;
249
+ event.preventDefault();
250
+ event.dataTransfer.dropEffect = 'copy';
251
+ setDrag(true, true);
252
+ });
253
+ holder.addEventListener('dragleave', (event) => {
254
+ if (!holder.contains(event.relatedTarget)) root.dataset.dragOver = 'false';
255
+ });
256
+ holder.addEventListener('drop', (event) => {
257
+ if (!hasFiles(event) || blocked()) return;
258
+ event.preventDefault();
259
+ const list = event.dataTransfer.files;
260
+ setDrag(false);
261
+ add(list);
262
+ });
263
+ });
264
+
265
+ // the bar re-renders itself (another mode, sending): keep the row in step
266
+ let lastItem = root.dataset.item || '';
267
+ let lastBusy = root.dataset.busy || '';
268
+ new MutationObserver(() => {
269
+ const item = root.dataset.item || '';
270
+ const busy = root.dataset.busy || '';
271
+ if (item === lastItem && busy === lastBusy) return;
272
+ lastItem = item;
273
+ lastBusy = busy;
274
+ sync();
275
+ }).observe(root, { attributes: true, attributeFilter: ['data-item', 'data-busy'] });
276
+ window.addEventListener('resize', () => window.requestAnimationFrame(sync));
277
+ return true;
278
+ }
279
+
280
+ function start() {
281
+ bar = window.PinokioAgentBar;
282
+ const usable = bar && bar.helpers && bar.attachments && document.body
283
+ && (document.body.classList.contains('main-sidebar-page') || document.body.classList.contains('app-page'));
284
+ if (!usable) return;
285
+ H = bar.helpers;
286
+ // an app page puts its bar, and on phones the strip that holds it, in while it loads
287
+ if (document.body.classList.contains('app-page') && document.readyState === 'loading') {
288
+ document.addEventListener('DOMContentLoaded', start, { once: true });
289
+ return;
290
+ }
291
+ if (!mount()) return;
292
+ bar.attachments.token = token;
293
+ bar.attachments.paths = paths;
294
+ bar.attachments.clear = clear;
295
+ }
296
+
297
+ const bootBar = window.PinokioAgentBar;
298
+ if (bootBar && bootBar.ready && typeof bootBar.ready.then === 'function') bootBar.ready.then(start);
299
+ })(window, document);