pinokiod 8.3.7 → 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.
- package/kernel/agent_instructions.js +56 -0
- package/kernel/app_log_sessions.js +1 -1
- package/kernel/bin/node.js +2 -2
- package/kernel/environment.js +6 -98
- package/kernel/managed_skills.js +2 -1
- package/package.json +1 -1
- package/prototype/system/SKILL_PINOKIO.md +5 -2
- package/server/index.js +7 -0
- package/server/lib/brain.js +279 -0
- package/server/public/agent-bar-app.css +359 -0
- package/server/public/agent-bar-app.js +821 -0
- package/server/public/agent-bar-attach.css +142 -0
- package/server/public/agent-bar-attach.js +299 -0
- package/server/public/agent-bar-panel.css +788 -0
- package/server/public/agent-bar-panel.js +1912 -0
- package/server/public/agent-bar-shell.css +128 -0
- package/server/public/agent-bar-shell.js +251 -0
- package/server/public/agent-bar.css +972 -0
- package/server/public/agent-bar.js +1513 -0
- package/server/public/common.js +65 -0
- package/server/public/layout.js +13 -1
- package/server/routes/apps.js +6 -0
- package/server/socket.js +6 -2
- package/server/views/app.ejs +13 -0
- package/server/views/partials/main_sidebar.ejs +7 -0
- package/system/plugin/opencode/pinokio.js +8 -2
- package/system/plugin/opencode-auto/pinokio.js +4 -1
- package/test/agent-instructions.test.js +69 -0
- package/test/app-log-sessions.test.js +29 -0
- package/test/brain.test.js +257 -0
- package/test/managed-skills.test.js +6 -1
- package/test/node-bin.test.js +4 -4
|
@@ -0,0 +1,821 @@
|
|
|
1
|
+
// Agent bar on app pages (Run and Dev). Loaded by agent-bar.js.
|
|
2
|
+
//
|
|
3
|
+
// What it does, all by rearranging what the page already has:
|
|
4
|
+
// 1. The app card (icon, name, Disk/CPU/RAM) moves from the top navbar to the
|
|
5
|
+
// top of the left sidebar. One click anywhere on it opens one popover with
|
|
6
|
+
// the app details and the resource switches (the page's own popovers).
|
|
7
|
+
// 2. "News": the navbar's Feed button becomes a row at the bottom of that card.
|
|
8
|
+
// It clicks the same button, so the same panel opens and closes.
|
|
9
|
+
// 3. The navbar gets the "Ask Pinokio about <app>" bar in place of the Ask AI button:
|
|
10
|
+
// an input, a small agent icon and a send arrow. Nothing opens on click.
|
|
11
|
+
// Run: a send opens the chosen agent with the prompt in the docked window
|
|
12
|
+
// (window.PinokioAgentDock, see agent-bar-panel.js). Without it, in the
|
|
13
|
+
// page's own Ask AI panel (window.PinokioAskAiDrawer), as before.
|
|
14
|
+
// Dev: a send opens the agent in the main area, through the page's own
|
|
15
|
+
// ?plugin=... launch link.
|
|
16
|
+
// Shells (Run): listed at the end of the agent menu; picking one opens it
|
|
17
|
+
// in the panel right away, as today.
|
|
18
|
+
// 4. Sessions (Run): every send starts its own session (a `session` value on
|
|
19
|
+
// the launch link, the same thing shells already get), so nothing running
|
|
20
|
+
// is replaced. The dropdown lists this app's running agent and shell
|
|
21
|
+
// sessions, read from GET /info/shells; picking one shows it in the panel
|
|
22
|
+
// again. Opening a running session's link attaches to it, it is not re-run.
|
|
23
|
+
// Nothing is removed from the page: the Ask AI and Feed buttons are hidden by
|
|
24
|
+
// agent-bar-app.css while this is active, and phones are left untouched.
|
|
25
|
+
//
|
|
26
|
+
// Timing: app.ejs loads this file in <head> and calls PinokioAgentBarApp.mount()
|
|
27
|
+
// right after the sidebar's markup, so the page is already rearranged the first
|
|
28
|
+
// time it is drawn (no flash of the old top bar). Parts of the page that come
|
|
29
|
+
// later in the markup (the Ask AI and News panels) are picked up once the page
|
|
30
|
+
// has finished loading. If the file is loaded any other way it mounts itself
|
|
31
|
+
// when the page has loaded.
|
|
32
|
+
(function (window, document) {
|
|
33
|
+
'use strict';
|
|
34
|
+
|
|
35
|
+
// agent-bar.js only loads this file on a page that may host the bar
|
|
36
|
+
const shared = window.PinokioAgentBar;
|
|
37
|
+
if (!shared || !shared.helpers) return;
|
|
38
|
+
const {
|
|
39
|
+
el, faIcon, menuRow, toolIcon, mapPluginMenuToTools, getStoredTool, setStoredTool,
|
|
40
|
+
newSession, pluginTail, timeAgo, readRunId,
|
|
41
|
+
agentPicker, agentMenuRows, fitTextarea, raiseTopBar, bindBar,
|
|
42
|
+
} = shared.helpers;
|
|
43
|
+
|
|
44
|
+
function withParam(href, key, value) {
|
|
45
|
+
const hasTarget = href.startsWith('@');
|
|
46
|
+
const url = new URL(hasTarget ? href.slice(1) : href, window.location.origin);
|
|
47
|
+
url.searchParams.set(key, value);
|
|
48
|
+
const next = `${url.pathname}${url.search}${url.hash}`;
|
|
49
|
+
return hasTarget ? `@${next}` : next;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function samePath(a, b) {
|
|
53
|
+
const clean = (value) => String(value || '').replace(/\\/g, '/').replace(/\/+$/, '');
|
|
54
|
+
return Boolean(a) && clean(a) === clean(b);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// The app page's own phone layout (app.ejs): at this width its top bar is off
|
|
58
|
+
// screen, and a strip with the logo and the app's name is all that is left of it.
|
|
59
|
+
const PHONE_CLASS = 'agent-bar-app-phone';
|
|
60
|
+
function phoneQuery(mobileQuery) {
|
|
61
|
+
const query = mobileQuery || window.PinokioAppMobileBreakpointQuery || '(max-width: 980px)';
|
|
62
|
+
return window.matchMedia ? window.matchMedia(query) : null;
|
|
63
|
+
}
|
|
64
|
+
function isPhoneLayout(mobileQuery) {
|
|
65
|
+
const media = phoneQuery(mobileQuery);
|
|
66
|
+
return Boolean(media && media.matches);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function whenPageLoaded(fn) {
|
|
70
|
+
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', fn);
|
|
71
|
+
else fn();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// ---------------------------------------------------------------- app card
|
|
75
|
+
function moveCard(identity, sidebar) {
|
|
76
|
+
identity.classList.add('agent-bar-app-identity');
|
|
77
|
+
sidebar.insertBefore(identity, sidebar.firstChild);
|
|
78
|
+
|
|
79
|
+
// The app icon becomes its own, larger column. It sits inside the info root
|
|
80
|
+
// so the page's own open/close handling keeps treating it as part of it.
|
|
81
|
+
const infoRoot = identity.querySelector('[data-app-info-root]');
|
|
82
|
+
const infoTrigger = identity.querySelector('[data-app-header-info-trigger]');
|
|
83
|
+
const smallIcon = identity.querySelector('.app-header-identity-icon');
|
|
84
|
+
if (infoRoot && infoTrigger) {
|
|
85
|
+
const iconButton = el('button', 'agent-bar-app-icon');
|
|
86
|
+
iconButton.type = 'button';
|
|
87
|
+
iconButton.tabIndex = -1;
|
|
88
|
+
iconButton.setAttribute('aria-label', 'App information');
|
|
89
|
+
const bigIcon = document.createElement('img');
|
|
90
|
+
bigIcon.src = smallIcon && smallIcon.getAttribute('src') ? smallIcon.getAttribute('src') : '/pinokio-black.png';
|
|
91
|
+
bigIcon.alt = '';
|
|
92
|
+
iconButton.appendChild(bigIcon);
|
|
93
|
+
infoRoot.insertBefore(iconButton, infoRoot.firstChild);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// One card: the whole block is one click target and opens ONE popover that
|
|
97
|
+
// holds everything (app info, folder, source, and the resource switches).
|
|
98
|
+
const infoPopover = identity.querySelector('[data-app-header-info-popover]');
|
|
99
|
+
const resourcePopover = identity.querySelector('[data-resource-usage-popover]');
|
|
100
|
+
const resourceTrigger = identity.querySelector('[data-resource-usage-trigger]');
|
|
101
|
+
const resourceRoot = identity.querySelector('[data-resource-usage-root]');
|
|
102
|
+
if (infoPopover && infoTrigger) {
|
|
103
|
+
identity.classList.add('agent-bar-app-card');
|
|
104
|
+
identity.tabIndex = -1;
|
|
105
|
+
if (resourceRoot && resourceTrigger && resourcePopover) {
|
|
106
|
+
// The card keeps a live copy of the stats line. The real resource block
|
|
107
|
+
// (with its switches) moves into the one popover, still wired to the
|
|
108
|
+
// page's own code, so the switches show and save the real state.
|
|
109
|
+
const stats = el('div', 'agent-bar-app-stats');
|
|
110
|
+
// Update in place and only when a value really changed: replacing nodes
|
|
111
|
+
// under the pointer between press and release swallows the click.
|
|
112
|
+
const mirrorStats = () => {
|
|
113
|
+
const src = resourceTrigger.children;
|
|
114
|
+
const dst = stats.children;
|
|
115
|
+
if (dst.length !== src.length) {
|
|
116
|
+
stats.innerHTML = resourceTrigger.innerHTML;
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
for (let i = 0; i < src.length; i += 1) {
|
|
120
|
+
if (dst[i].className !== src[i].className) dst[i].className = src[i].className;
|
|
121
|
+
if (dst[i].innerHTML !== src[i].innerHTML) dst[i].innerHTML = src[i].innerHTML;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
mirrorStats();
|
|
125
|
+
new MutationObserver(mirrorStats).observe(resourceTrigger, {
|
|
126
|
+
childList: true, subtree: true, characterData: true, attributes: true,
|
|
127
|
+
});
|
|
128
|
+
identity.appendChild(stats);
|
|
129
|
+
resourceRoot.classList.add('agent-bar-merged-resource-root');
|
|
130
|
+
resourcePopover.classList.add('agent-bar-merged-resources');
|
|
131
|
+
resourcePopover.removeAttribute('role');
|
|
132
|
+
infoPopover.appendChild(resourceRoot);
|
|
133
|
+
}
|
|
134
|
+
let cardWasOpen = false;
|
|
135
|
+
const cardIsOpen = () => !infoPopover.classList.contains('hidden');
|
|
136
|
+
// window-level capture runs before the page's own outside-click handling
|
|
137
|
+
window.addEventListener('pointerdown', (event) => {
|
|
138
|
+
if (identity.contains(event.target)) cardWasOpen = cardIsOpen();
|
|
139
|
+
}, true);
|
|
140
|
+
window.addEventListener('keydown', (event) => {
|
|
141
|
+
if (identity.contains(event.target)) cardWasOpen = cardIsOpen();
|
|
142
|
+
}, true);
|
|
143
|
+
identity.addEventListener('click', (event) => {
|
|
144
|
+
if (event.target.closest('[data-app-header-info-popover]')) return; // inside the open card
|
|
145
|
+
if (event.target.closest('.agent-bar-feed-row')) return; // the News row is its own target
|
|
146
|
+
if (event.target.closest('[data-app-header-info-trigger]') && !event.isTrusted) return; // our own forwarded click
|
|
147
|
+
event.preventDefault();
|
|
148
|
+
event.stopPropagation();
|
|
149
|
+
// keep focus on the card (not in the app's iframe), or the page's own
|
|
150
|
+
// "focus went into an iframe" check closes the popover again
|
|
151
|
+
identity.focus({ preventScroll: true });
|
|
152
|
+
if (cardIsOpen() === cardWasOpen) infoTrigger.click();
|
|
153
|
+
}, true);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// The sidebar clips anything inside it (it is a transformed, overflow-hidden
|
|
157
|
+
// box), and the popover is wider than it. So when it opens, pin it to the
|
|
158
|
+
// viewport directly under the card, left-aligned with it.
|
|
159
|
+
const aside = document.getElementById('app-sidebar');
|
|
160
|
+
const popovers = Array.from(identity.querySelectorAll('[data-app-header-info-popover], [data-resource-usage-popover]'));
|
|
161
|
+
// While a popover is open the sidebar must not be a transformed box, or it
|
|
162
|
+
// stays the popover's clipping parent. Switch instantly (no transition).
|
|
163
|
+
const setAsideFlat = (flat) => {
|
|
164
|
+
if (!aside) return;
|
|
165
|
+
const current = window.getComputedStyle(aside).transform;
|
|
166
|
+
if (flat && current !== 'none' && current !== 'matrix(1, 0, 0, 1, 0, 0)') return;
|
|
167
|
+
aside.style.transition = 'none';
|
|
168
|
+
aside.style.transform = flat ? 'none' : '';
|
|
169
|
+
void aside.offsetHeight;
|
|
170
|
+
aside.style.transition = '';
|
|
171
|
+
};
|
|
172
|
+
const placePopover = (popover) => {
|
|
173
|
+
setAsideFlat(true);
|
|
174
|
+
const rect = identity.getBoundingClientRect();
|
|
175
|
+
popover.style.left = `${Math.round(rect.left)}px`;
|
|
176
|
+
popover.style.top = `${Math.round(rect.bottom + 6)}px`;
|
|
177
|
+
};
|
|
178
|
+
popovers.forEach((popover) => {
|
|
179
|
+
new MutationObserver(() => {
|
|
180
|
+
if (!popover.classList.contains('hidden')) {
|
|
181
|
+
placePopover(popover);
|
|
182
|
+
} else if (popovers.every((item) => item.classList.contains('hidden'))) {
|
|
183
|
+
setAsideFlat(false);
|
|
184
|
+
}
|
|
185
|
+
}).observe(popover, { attributes: true, attributeFilter: ['class'] });
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Where the bar sits depends on the layout. The card is always in the sidebar.
|
|
190
|
+
// - Desktop: the bar is in the top bar.
|
|
191
|
+
// - Phones: the page's strip (logo, app name) is moved to the top by
|
|
192
|
+
// agent-bar-app.css and holds the bar instead of the app's name, like the
|
|
193
|
+
// home page's top bar on phones. The sidebar is the slide-in menu there.
|
|
194
|
+
function followLayout(h1, bar, slot, mobileQuery) {
|
|
195
|
+
const media = phoneQuery(mobileQuery);
|
|
196
|
+
const place = () => {
|
|
197
|
+
// the strip comes later in the page's markup than the top bar and the sidebar
|
|
198
|
+
const strip = document.querySelector('.mobile-bottom-nav');
|
|
199
|
+
const phone = Boolean(media && media.matches && strip);
|
|
200
|
+
document.documentElement.classList.toggle(PHONE_CLASS, phone);
|
|
201
|
+
if (phone) {
|
|
202
|
+
if (bar.parentNode !== strip) strip.insertBefore(bar, strip.querySelector('.mobile-bottom-nav__identity'));
|
|
203
|
+
} else if (bar.parentNode !== h1) {
|
|
204
|
+
h1.insertBefore(bar, slot);
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
if (media) media.addEventListener('change', place);
|
|
208
|
+
whenPageLoaded(place);
|
|
209
|
+
place();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// -------------------------------------------------------------------- News
|
|
213
|
+
function addNewsRow(identity) {
|
|
214
|
+
const feedToggle = document.getElementById('community-toggle');
|
|
215
|
+
if (!feedToggle) return;
|
|
216
|
+
const row = el('button', 'agent-bar-feed-row');
|
|
217
|
+
row.type = 'button';
|
|
218
|
+
row.setAttribute('aria-controls', 'community-drawer');
|
|
219
|
+
const icon = el('i', 'fa-regular fa-newspaper');
|
|
220
|
+
icon.setAttribute('aria-hidden', 'true');
|
|
221
|
+
const chevron = el('i', 'fa-solid fa-chevron-right agent-bar-feed-chevron');
|
|
222
|
+
chevron.setAttribute('aria-hidden', 'true');
|
|
223
|
+
row.append(icon, el('span', 'agent-bar-feed-label', 'News'), chevron);
|
|
224
|
+
identity.appendChild(row);
|
|
225
|
+
|
|
226
|
+
// the chevron points right when the panel is closed, left when it is open
|
|
227
|
+
// On phones the page shows the feed in the main area, not in a side panel:
|
|
228
|
+
// its own Feed row of the menu does that (and closes the menu), so there the
|
|
229
|
+
// News row presses that row and shows that row's state.
|
|
230
|
+
const phoneRow = () => (document.documentElement.classList.contains(PHONE_CLASS)
|
|
231
|
+
? document.querySelector('.appcanvas > aside .mobile-nav-community')
|
|
232
|
+
: null);
|
|
233
|
+
const sync = () => {
|
|
234
|
+
const feedRow = phoneRow();
|
|
235
|
+
const isOpen = feedRow
|
|
236
|
+
? feedRow.getAttribute('aria-pressed') === 'true'
|
|
237
|
+
: feedToggle.getAttribute('aria-expanded') === 'true';
|
|
238
|
+
row.classList.toggle('selected', isOpen);
|
|
239
|
+
row.setAttribute('aria-expanded', String(isOpen));
|
|
240
|
+
};
|
|
241
|
+
row.addEventListener('click', () => { (phoneRow() || feedToggle).click(); });
|
|
242
|
+
new MutationObserver(sync).observe(feedToggle, { attributes: true });
|
|
243
|
+
// the Feed row, and the switch between the two layouts, come later than this
|
|
244
|
+
whenPageLoaded(() => {
|
|
245
|
+
const feedRow = document.querySelector('.appcanvas > aside .mobile-nav-community');
|
|
246
|
+
if (feedRow) new MutationObserver(sync).observe(feedRow, { attributes: true, attributeFilter: ['aria-pressed'] });
|
|
247
|
+
new MutationObserver(sync).observe(document.documentElement, { attributes: true, attributeFilter: ['class'] });
|
|
248
|
+
sync();
|
|
249
|
+
});
|
|
250
|
+
sync();
|
|
251
|
+
|
|
252
|
+
// The panel's own title says "News" too. Wording only. The panel comes later
|
|
253
|
+
// in the page's markup than the sidebar, so this waits for the page to load.
|
|
254
|
+
whenPageLoaded(() => {
|
|
255
|
+
const drawer = document.getElementById('community-drawer');
|
|
256
|
+
const home = drawer && drawer.querySelector('.community-drawer-home');
|
|
257
|
+
if (!home) return;
|
|
258
|
+
const homeLabel = home.querySelector('span');
|
|
259
|
+
const homeIcon = home.querySelector('i');
|
|
260
|
+
if (homeLabel) homeLabel.textContent = 'News';
|
|
261
|
+
if (homeIcon) homeIcon.className = 'fa-regular fa-newspaper';
|
|
262
|
+
const emptyTitle = drawer.querySelector('.community-drawer-empty-title');
|
|
263
|
+
if (emptyTitle && emptyTitle.textContent.trim() === 'Load Feed') emptyTitle.textContent = 'Load News';
|
|
264
|
+
const frame = drawer.querySelector('.community-frame');
|
|
265
|
+
if (frame) frame.title = 'Project news';
|
|
266
|
+
// the page resets this tooltip whenever the panel's state changes
|
|
267
|
+
const syncTip = () => {
|
|
268
|
+
if (home.getAttribute('data-tippy-content') !== 'Project feed') return;
|
|
269
|
+
home.setAttribute('data-tippy-content', 'Project news');
|
|
270
|
+
home.setAttribute('aria-label', 'Project news');
|
|
271
|
+
if (home._tippy && typeof home._tippy.setContent === 'function') home._tippy.setContent('Project news');
|
|
272
|
+
};
|
|
273
|
+
new MutationObserver(syncTip).observe(home, { attributes: true, attributeFilter: ['data-tippy-content'] });
|
|
274
|
+
syncTip();
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// ----------------------------------------------------------------- the bar
|
|
279
|
+
// An input, a "N running" count (only when something is running), a small agent
|
|
280
|
+
// icon and a send arrow. Nothing opens when you click into it. The count and
|
|
281
|
+
// the agent icon each open a small menu.
|
|
282
|
+
function addBar(h1, slot, appName) {
|
|
283
|
+
// Run mode has the Ask AI button and panel; Dev mode opens plugins in the main
|
|
284
|
+
// area. The button is in the top bar, so it is there even this early.
|
|
285
|
+
const askTab = document.getElementById('ask-ai-tab');
|
|
286
|
+
const isRun = Boolean(askTab || document.getElementById('ask-ai-drawer'));
|
|
287
|
+
const cwdSource = askTab || document.querySelector('[data-workspace-cwd]');
|
|
288
|
+
const workspaceCwd = cwdSource && cwdSource.dataset.workspaceCwd ? cwdSource.dataset.workspaceCwd.trim() : '';
|
|
289
|
+
// the app's id is its folder's name (what Pinokio's own app requests go by).
|
|
290
|
+
// Dev mode has no element that carries the folder, so there it is read from
|
|
291
|
+
// the page's own address, /p/<folder>/dev.
|
|
292
|
+
const appId = workspaceCwd.replace(/\\/g, '/').replace(/\/+$/, '').split('/').pop() || (() => {
|
|
293
|
+
const page = window.location.pathname.match(/^\/p\/([^/]+)/);
|
|
294
|
+
try {
|
|
295
|
+
return page ? decodeURIComponent(page[1]) : '';
|
|
296
|
+
} catch (_) {
|
|
297
|
+
return '';
|
|
298
|
+
}
|
|
299
|
+
})();
|
|
300
|
+
// The dock (agent-bar-panel.js) when it is on the page; otherwise the page's own panel.
|
|
301
|
+
const askDrawer = () => (isRun ? (window.PinokioAgentDock || window.PinokioAskAiDrawer || null) : null);
|
|
302
|
+
|
|
303
|
+
const state = {
|
|
304
|
+
menu: null, // null | 'agent' | 'running'
|
|
305
|
+
engaged: false,
|
|
306
|
+
busy: false,
|
|
307
|
+
tools: [],
|
|
308
|
+
shells: [],
|
|
309
|
+
sessions: [],
|
|
310
|
+
tool: '',
|
|
311
|
+
loaded: false,
|
|
312
|
+
};
|
|
313
|
+
// what the Ask AI panel is showing: it records its current link in this field
|
|
314
|
+
// (the panel comes later in the page's markup, so these are set once it exists)
|
|
315
|
+
let panelLocation = null;
|
|
316
|
+
|
|
317
|
+
const root = el('div', 'agent-bar');
|
|
318
|
+
root.dataset.item = 'ask_app';
|
|
319
|
+
const inner = el('div', 'agent-bar__inner');
|
|
320
|
+
const field = el('div', 'agent-bar__field');
|
|
321
|
+
const top = el('div', 'agent-bar__top');
|
|
322
|
+
const lead = el('span', 'agent-bar__mark agent-bar__lead');
|
|
323
|
+
lead.setAttribute('aria-hidden', 'true');
|
|
324
|
+
const input = el('textarea', 'agent-bar__input');
|
|
325
|
+
input.rows = 1;
|
|
326
|
+
input.autocomplete = 'off';
|
|
327
|
+
input.placeholder = `Ask Pinokio about ${appName}…`;
|
|
328
|
+
input.setAttribute('aria-label', `Ask Pinokio about ${appName}`);
|
|
329
|
+
const running = el('button', 'agent-bar__running');
|
|
330
|
+
running.type = 'button';
|
|
331
|
+
running.hidden = true;
|
|
332
|
+
running.setAttribute('aria-haspopup', 'menu');
|
|
333
|
+
const agent = agentPicker();
|
|
334
|
+
const agentButton = agent.button;
|
|
335
|
+
const go = el('button', 'agent-bar__go');
|
|
336
|
+
go.type = 'button';
|
|
337
|
+
top.append(lead, input, running, agentButton, go);
|
|
338
|
+
field.appendChild(top);
|
|
339
|
+
|
|
340
|
+
const note = el('div', 'agent-bar__note');
|
|
341
|
+
note.setAttribute('role', 'status');
|
|
342
|
+
note.hidden = true;
|
|
343
|
+
const noteLabel = el('span', 'agent-bar__note-label');
|
|
344
|
+
note.appendChild(noteLabel);
|
|
345
|
+
const menu = el('div', 'agent-bar__menu');
|
|
346
|
+
menu.setAttribute('role', 'menu');
|
|
347
|
+
menu.hidden = true;
|
|
348
|
+
|
|
349
|
+
inner.append(field, note, menu);
|
|
350
|
+
root.appendChild(inner);
|
|
351
|
+
slot.parentNode.insertBefore(root, slot);
|
|
352
|
+
slot.dataset.agentBarReplaced = 'true';
|
|
353
|
+
|
|
354
|
+
const selectedTool = () => state.tools.find((tool) => tool.value === state.tool) || null;
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
function syncRaise() {
|
|
358
|
+
raiseTopBar(h1, Boolean(state.menu) || !note.hidden || field.offsetHeight > 32);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function setMsg(text, kind) {
|
|
362
|
+
noteLabel.textContent = text || '';
|
|
363
|
+
note.hidden = !text;
|
|
364
|
+
syncRaise();
|
|
365
|
+
if (kind) note.dataset.kind = kind;
|
|
366
|
+
else delete note.dataset.kind;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function fitInput() {
|
|
370
|
+
fitTextarea(root, field, top, input);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// Which session a link belongs to: the plugin or shell, plus its session value.
|
|
374
|
+
function describeLink(rawUrl) {
|
|
375
|
+
if (!rawUrl) return null;
|
|
376
|
+
let url;
|
|
377
|
+
try {
|
|
378
|
+
url = new URL(rawUrl, window.location.origin);
|
|
379
|
+
} catch (_) {
|
|
380
|
+
return null;
|
|
381
|
+
}
|
|
382
|
+
const session = url.searchParams.get('session') || '';
|
|
383
|
+
if (url.pathname.startsWith('/shell/')) {
|
|
384
|
+
let shellId = '';
|
|
385
|
+
try {
|
|
386
|
+
shellId = `shell/${decodeURIComponent(url.pathname.slice('/shell/'.length))}`;
|
|
387
|
+
} catch (_) {
|
|
388
|
+
return null;
|
|
389
|
+
}
|
|
390
|
+
const shell = state.shells.find((item) => item.shellId === shellId);
|
|
391
|
+
return shell ? { key: `${shellId}|${session}`, label: shell.label } : null;
|
|
392
|
+
}
|
|
393
|
+
const tail = pluginTail(url.pathname);
|
|
394
|
+
const tool = state.tools.find((item) => pluginTail(item.href) === tail);
|
|
395
|
+
return tool ? { key: `${tail}|${session}`, label: tool.label } : null;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
function currentPanelKey() {
|
|
399
|
+
const drawer = askDrawer();
|
|
400
|
+
if (!drawer || !drawer.isOpen()) return '';
|
|
401
|
+
// the page's own panel keeps the link of what it shows in its address field
|
|
402
|
+
const address = panelLocation ? panelLocation.value : '';
|
|
403
|
+
const current = describeLink(address);
|
|
404
|
+
return current ? current.key : '';
|
|
405
|
+
}
|
|
406
|
+
// Every session that is on screen. The dock can have several windows up; the
|
|
407
|
+
// page's own panel shows one.
|
|
408
|
+
function panelKeys() {
|
|
409
|
+
const drawer = askDrawer();
|
|
410
|
+
if (drawer && typeof drawer.openUrls === 'function') {
|
|
411
|
+
return drawer.openUrls().map((address) => describeLink(address)).filter(Boolean).map((link) => link.key);
|
|
412
|
+
}
|
|
413
|
+
const key = currentPanelKey();
|
|
414
|
+
return key ? [key] : [];
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// This app's running sessions, from the server's list of running shells.
|
|
418
|
+
// agent session: group = <plugin script path>?cwd=<this app's folder>[&session=...]
|
|
419
|
+
// shell session: id = <one of this app's project shells>[?session=...]
|
|
420
|
+
function readSessions(shells) {
|
|
421
|
+
const seen = new Set();
|
|
422
|
+
const sessions = [];
|
|
423
|
+
(Array.isArray(shells) ? shells : []).forEach((shell) => {
|
|
424
|
+
if (!shell || shell.done) return;
|
|
425
|
+
const group = typeof shell.group === 'string' ? shell.group : '';
|
|
426
|
+
const id = typeof shell.id === 'string' ? shell.id : '';
|
|
427
|
+
const queryIndex = group.indexOf('?');
|
|
428
|
+
if (queryIndex > 0) {
|
|
429
|
+
const { script, cwd, session } = readRunId(group);
|
|
430
|
+
if (!samePath(cwd, workspaceCwd)) return;
|
|
431
|
+
const tool = state.tools.find((item) => item.href && script.endsWith(pluginTail(item.href)));
|
|
432
|
+
if (!tool) return;
|
|
433
|
+
const key = `${pluginTail(tool.href)}|${session}`;
|
|
434
|
+
if (seen.has(key)) return;
|
|
435
|
+
seen.add(key);
|
|
436
|
+
sessions.push({
|
|
437
|
+
kind: 'agent',
|
|
438
|
+
key,
|
|
439
|
+
label: tool.label,
|
|
440
|
+
iconSrc: tool.iconSrc,
|
|
441
|
+
startTime: shell.start_time,
|
|
442
|
+
url: session ? withParam(tool.href, 'session', session) : tool.href,
|
|
443
|
+
});
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
if (!id.startsWith('shell/')) return;
|
|
447
|
+
const idQuery = id.indexOf('?');
|
|
448
|
+
const shellId = idQuery < 0 ? id : id.slice(0, idQuery);
|
|
449
|
+
const tool = state.shells.find((item) => item.shellId === shellId);
|
|
450
|
+
if (!tool) return;
|
|
451
|
+
const session = idQuery < 0 ? '' : (new URLSearchParams(id.slice(idQuery + 1)).get('session') || '');
|
|
452
|
+
const key = `${shellId}|${session}`;
|
|
453
|
+
if (seen.has(key)) return;
|
|
454
|
+
seen.add(key);
|
|
455
|
+
sessions.push({
|
|
456
|
+
kind: 'shell',
|
|
457
|
+
key,
|
|
458
|
+
label: tool.label,
|
|
459
|
+
iconClass: tool.iconClass,
|
|
460
|
+
startTime: shell.start_time,
|
|
461
|
+
url: session ? withParam(tool.href, 'session', session) : tool.href,
|
|
462
|
+
target: session ? withParam(tool.target, 'session', session) : tool.target,
|
|
463
|
+
});
|
|
464
|
+
});
|
|
465
|
+
sessions.sort((a, b) => Number(b.startTime || 0) - Number(a.startTime || 0));
|
|
466
|
+
return sessions;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
function renderMenu() {
|
|
470
|
+
menu.textContent = '';
|
|
471
|
+
menu.hidden = !state.menu;
|
|
472
|
+
menu.dataset.menu = state.menu || '';
|
|
473
|
+
if (state.menu === 'agent') {
|
|
474
|
+
agentMenuRows(menu, state.tools, state.loaded, state.tool, (tool) => {
|
|
475
|
+
state.tool = tool.value;
|
|
476
|
+
state.menu = null;
|
|
477
|
+
setStoredTool(tool.value);
|
|
478
|
+
setMsg('');
|
|
479
|
+
render();
|
|
480
|
+
input.focus();
|
|
481
|
+
});
|
|
482
|
+
// Shells are not agents: picking one opens it in the panel right away.
|
|
483
|
+
// Not shown while the top bar has its own shell button (agent-bar-shell.js).
|
|
484
|
+
if (isRun && state.shells.length && !window.PinokioShellButton) {
|
|
485
|
+
menu.appendChild(el('div', 'agent-bar__menu-title', 'Open a shell'));
|
|
486
|
+
state.shells.forEach((shell) => {
|
|
487
|
+
menu.appendChild(menuRow(toolIcon(shell), shell.label, shell.description, false, () => openShell(shell)));
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
} else if (state.menu === 'running') {
|
|
491
|
+
const openKeys = panelKeys();
|
|
492
|
+
menu.appendChild(el('div', 'agent-bar__menu-title', 'Running in this app'));
|
|
493
|
+
state.sessions.forEach((session) => {
|
|
494
|
+
const onScreen = openKeys.includes(session.key);
|
|
495
|
+
const noteText = onScreen ? `On screen · ${timeAgo(session.startTime)}` : timeAgo(session.startTime);
|
|
496
|
+
const row = menuRow(toolIcon(session), session.label, noteText, false, () => showSession(session));
|
|
497
|
+
row.classList.toggle('is-current', onScreen);
|
|
498
|
+
// Stop on the row, as on the home bar's list: the dock's own button (asks twice).
|
|
499
|
+
const drawer = askDrawer();
|
|
500
|
+
const stop = drawer && typeof drawer.stopButton === 'function'
|
|
501
|
+
? drawer.stopButton(session.url, () => {
|
|
502
|
+
state.sessions = state.sessions.filter((item) => item !== session);
|
|
503
|
+
render();
|
|
504
|
+
window.setTimeout(loadSessions, 1500);
|
|
505
|
+
})
|
|
506
|
+
: null;
|
|
507
|
+
if (!stop) {
|
|
508
|
+
menu.appendChild(row);
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
const wrap = el('div', 'agent-dock__listrow');
|
|
512
|
+
wrap.append(row, stop);
|
|
513
|
+
menu.appendChild(wrap);
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
function render() {
|
|
519
|
+
const tool = selectedTool();
|
|
520
|
+
root.dataset.busy = String(state.busy);
|
|
521
|
+
root.dataset.engaged = String(state.engaged);
|
|
522
|
+
input.disabled = state.busy;
|
|
523
|
+
agent.show(tool, state.busy, state.menu === 'agent');
|
|
524
|
+
// running sessions: a small count in the field, only when there are any
|
|
525
|
+
const count = state.sessions.length;
|
|
526
|
+
running.hidden = !(isRun && count > 0);
|
|
527
|
+
running.textContent = '';
|
|
528
|
+
running.append(el('span', 'agent-bar__dot'), `${count} running`);
|
|
529
|
+
running.classList.toggle('is-open', state.menu === 'running');
|
|
530
|
+
running.setAttribute('aria-expanded', String(state.menu === 'running'));
|
|
531
|
+
if (state.menu === 'running' && !count) state.menu = null;
|
|
532
|
+
go.disabled = state.busy;
|
|
533
|
+
go.textContent = '';
|
|
534
|
+
go.appendChild(faIcon(state.busy ? 'fa-solid fa-circle-notch fa-spin' : 'fa-solid fa-arrow-up'));
|
|
535
|
+
go.setAttribute('aria-label', state.busy ? 'Working' : 'Ask');
|
|
536
|
+
go.title = state.busy ? '' : 'Ask';
|
|
537
|
+
renderMenu();
|
|
538
|
+
fitInput();
|
|
539
|
+
syncRaise();
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// Same request the Ask AI panel makes: the plugin list, plus this app's shells.
|
|
543
|
+
let toolsPromise = null;
|
|
544
|
+
function loadTools() {
|
|
545
|
+
if (toolsPromise) return toolsPromise;
|
|
546
|
+
const url = workspaceCwd ? `/api/plugin/menu?workspace=${encodeURIComponent(workspaceCwd)}` : '/api/plugin/menu';
|
|
547
|
+
toolsPromise = fetch(url)
|
|
548
|
+
.then((res) => {
|
|
549
|
+
if (!res.ok) throw new Error('Failed to load plugins.');
|
|
550
|
+
return res.json();
|
|
551
|
+
})
|
|
552
|
+
.then((payload) => {
|
|
553
|
+
const list = payload && Array.isArray(payload.menu) ? payload.menu : [];
|
|
554
|
+
// same mapping as the home bar, plus the launch link the Ask AI panel uses
|
|
555
|
+
state.tools = list.map((plugin) => {
|
|
556
|
+
const tool = mapPluginMenuToTools([plugin])[0];
|
|
557
|
+
if (!tool) return null;
|
|
558
|
+
tool.href = String(plugin.href).trim();
|
|
559
|
+
return tool;
|
|
560
|
+
}).filter(Boolean);
|
|
561
|
+
const items = isRun && payload && payload.projectShell && Array.isArray(payload.projectShell.items)
|
|
562
|
+
? payload.projectShell.items
|
|
563
|
+
: [];
|
|
564
|
+
state.shells = items.map((item) => {
|
|
565
|
+
const href = item && typeof item.href === 'string' ? item.href.trim() : '';
|
|
566
|
+
if (!href || !href.startsWith('/shell/')) return null;
|
|
567
|
+
const label = (typeof item.title === 'string' && item.title.trim())
|
|
568
|
+
|| (typeof item.text === 'string' && item.text.trim())
|
|
569
|
+
|| 'Shell';
|
|
570
|
+
let shellId = typeof item.shellId === 'string' ? item.shellId.trim() : '';
|
|
571
|
+
if (!shellId) {
|
|
572
|
+
try {
|
|
573
|
+
shellId = `shell/${decodeURIComponent(href.split('?')[0].slice('/shell/'.length))}`;
|
|
574
|
+
} catch (_) {
|
|
575
|
+
shellId = '';
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
// When this shell is already running, the server reports its id with
|
|
579
|
+
// that session attached. The shell itself is the part before it.
|
|
580
|
+
shellId = shellId.split('?')[0];
|
|
581
|
+
return {
|
|
582
|
+
label,
|
|
583
|
+
href,
|
|
584
|
+
shellId,
|
|
585
|
+
target: typeof item.target === 'string' && item.target.trim() ? item.target.trim() : `@${href}`,
|
|
586
|
+
iconClass: typeof item.icon === 'string' && item.icon.trim() ? item.icon.trim() : 'fa-solid fa-terminal',
|
|
587
|
+
description: typeof item.subtitle === 'string' ? item.subtitle.trim() : '',
|
|
588
|
+
kind: 'shell',
|
|
589
|
+
};
|
|
590
|
+
}).filter(Boolean);
|
|
591
|
+
state.loaded = true;
|
|
592
|
+
// The agent: the last one used; otherwise the one the plugin list marks
|
|
593
|
+
// as default; otherwise none yet (the first send asks once).
|
|
594
|
+
const stored = getStoredTool();
|
|
595
|
+
const fallback = state.tools.find((tool) => tool.isDefault);
|
|
596
|
+
if (stored && state.tools.some((tool) => tool.value === stored)) state.tool = stored;
|
|
597
|
+
else if (!state.tools.some((tool) => tool.value === state.tool)) state.tool = fallback ? fallback.value : '';
|
|
598
|
+
render();
|
|
599
|
+
})
|
|
600
|
+
.catch(() => {
|
|
601
|
+
toolsPromise = null;
|
|
602
|
+
state.loaded = true;
|
|
603
|
+
render();
|
|
604
|
+
});
|
|
605
|
+
return toolsPromise;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
function loadSessions() {
|
|
609
|
+
if (!isRun || !workspaceCwd) return Promise.resolve();
|
|
610
|
+
return Promise.all([loadTools(), fetch('/info/shells').then((res) => (res.ok ? res.json() : []))])
|
|
611
|
+
.then((results) => {
|
|
612
|
+
state.sessions = readSessions(results[1]);
|
|
613
|
+
render();
|
|
614
|
+
})
|
|
615
|
+
.catch(() => {});
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
function engage() {
|
|
619
|
+
if (state.engaged) return;
|
|
620
|
+
state.engaged = true;
|
|
621
|
+
loadTools();
|
|
622
|
+
loadSessions(); // read fresh every time you come to the bar
|
|
623
|
+
render();
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
function leave() {
|
|
627
|
+
if (state.busy || (!state.engaged && !state.menu)) return;
|
|
628
|
+
state.engaged = false;
|
|
629
|
+
state.menu = null;
|
|
630
|
+
render();
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
function toggleMenu(which) {
|
|
634
|
+
if (state.busy) return;
|
|
635
|
+
state.menu = state.menu === which ? null : which;
|
|
636
|
+
state.engaged = true;
|
|
637
|
+
setMsg('');
|
|
638
|
+
loadTools();
|
|
639
|
+
if (which === 'running') loadSessions();
|
|
640
|
+
render();
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
// Show a running session in the panel again. Its link attaches to the
|
|
644
|
+
// running process; it does not start it a second time.
|
|
645
|
+
function showSession(session) {
|
|
646
|
+
const drawer = askDrawer();
|
|
647
|
+
if (!drawer) return;
|
|
648
|
+
const opened = session.kind === 'shell'
|
|
649
|
+
? drawer.openWithUrl(session.url, { frameName: session.target })
|
|
650
|
+
: drawer.openWithUrl(session.url, { workspaceCwd });
|
|
651
|
+
if (opened === false) {
|
|
652
|
+
setMsg('Could not open this session.', 'error');
|
|
653
|
+
return;
|
|
654
|
+
}
|
|
655
|
+
leave();
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
function openShell(shell) {
|
|
659
|
+
const drawer = askDrawer();
|
|
660
|
+
if (!drawer) return;
|
|
661
|
+
state.menu = null;
|
|
662
|
+
render();
|
|
663
|
+
// the page's own way of opening a shell (adds its tab, gives it its own session)
|
|
664
|
+
const opened = typeof drawer.openShellTool === 'function'
|
|
665
|
+
? drawer.openShellTool(shell)
|
|
666
|
+
: drawer.openWithUrl(shell.href, { frameName: shell.target });
|
|
667
|
+
Promise.resolve(opened).then((ok) => {
|
|
668
|
+
if (ok === false) {
|
|
669
|
+
setMsg('Could not open this shell.', 'error');
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
leave();
|
|
673
|
+
window.setTimeout(loadSessions, 1500);
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
// One send at a time: a second Enter while the agents are still being read
|
|
678
|
+
// must not start a second agent.
|
|
679
|
+
let sending = false;
|
|
680
|
+
async function submit() {
|
|
681
|
+
if (state.busy || sending) return;
|
|
682
|
+
sending = true;
|
|
683
|
+
state.menu = null;
|
|
684
|
+
state.engaged = true;
|
|
685
|
+
try {
|
|
686
|
+
await loadTools();
|
|
687
|
+
} finally {
|
|
688
|
+
sending = false;
|
|
689
|
+
}
|
|
690
|
+
const tool = selectedTool();
|
|
691
|
+
if (!tool || !tool.href) {
|
|
692
|
+
state.menu = 'agent';
|
|
693
|
+
render();
|
|
694
|
+
setMsg('Choose an agent once. It is remembered after that.', 'error');
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
setMsg('');
|
|
698
|
+
const prompt = input.value.trim();
|
|
699
|
+
state.busy = true;
|
|
700
|
+
render();
|
|
701
|
+
try {
|
|
702
|
+
const drawer = askDrawer();
|
|
703
|
+
// a plugin that is not installed yet goes to its install page, as today
|
|
704
|
+
if (drawer && typeof drawer.redirectToPluginInstallIfNeeded === 'function'
|
|
705
|
+
&& await drawer.redirectToPluginInstallIfNeeded(tool.href)) {
|
|
706
|
+
return;
|
|
707
|
+
}
|
|
708
|
+
// Attached files go into the app's own temp folder first (see
|
|
709
|
+
// agent-bar-attach.js); the end of the prompt tells the agent where they are.
|
|
710
|
+
const attached = await shared.attachments.paths(appId);
|
|
711
|
+
let text = attached.length
|
|
712
|
+
? `${prompt}${prompt ? '\n\n' : ''}Attached files:\n${attached.map((file) => `- ${file}`).join('\n')}`
|
|
713
|
+
: prompt;
|
|
714
|
+
// Pinokio adds where this app's skill is and where to save output
|
|
715
|
+
// (server/lib/brain.js). If that fails the Ask still goes out without it.
|
|
716
|
+
const brain = !appId || !text ? null : await fetch('/brain/ask', {
|
|
717
|
+
method: 'POST',
|
|
718
|
+
headers: { 'Content-Type': 'application/json' },
|
|
719
|
+
body: JSON.stringify({ app: appId, prompt, tool: tool.value }),
|
|
720
|
+
}).then((response) => (response.ok ? response.json() : null)).catch(() => null);
|
|
721
|
+
if (brain && typeof brain.append === 'string') text += `\n\n${brain.append}`;
|
|
722
|
+
if (drawer) {
|
|
723
|
+
// its own session, so a session that is already running is left alone
|
|
724
|
+
const opened = drawer.openWithAgent({
|
|
725
|
+
agentHref: withParam(tool.href, 'session', newSession()),
|
|
726
|
+
prompt: text,
|
|
727
|
+
workspaceCwd,
|
|
728
|
+
});
|
|
729
|
+
if (opened === false) throw new Error('Could not launch this plugin.');
|
|
730
|
+
shared.attachments.clear();
|
|
731
|
+
state.busy = false;
|
|
732
|
+
input.value = '';
|
|
733
|
+
leave();
|
|
734
|
+
render();
|
|
735
|
+
window.setTimeout(loadSessions, 1500);
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
// Dev: the page's own launch link opens the plugin as a tab in the main area.
|
|
739
|
+
const pluginPath = tool.href.startsWith('/pinokio/run/plugin/') ? tool.href : tool.href.replace(/^\/run/, '');
|
|
740
|
+
const url = new URL(window.location.pathname, window.location.origin);
|
|
741
|
+
url.searchParams.set('plugin', pluginPath);
|
|
742
|
+
url.searchParams.set('plugin_label', tool.label);
|
|
743
|
+
if (text) url.searchParams.set('prompt', text);
|
|
744
|
+
shared.navigate(`${url.pathname}${url.search}`);
|
|
745
|
+
} catch (error) {
|
|
746
|
+
state.busy = false;
|
|
747
|
+
render();
|
|
748
|
+
setMsg(error && error.message ? error.message : 'Failed to continue.', 'error');
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
running.addEventListener('click', () => toggleMenu('running'));
|
|
753
|
+
agentButton.addEventListener('click', () => toggleMenu('agent'));
|
|
754
|
+
input.addEventListener('focus', engage);
|
|
755
|
+
input.addEventListener('pointerdown', engage);
|
|
756
|
+
input.addEventListener('input', () => {
|
|
757
|
+
setMsg('');
|
|
758
|
+
fitInput();
|
|
759
|
+
});
|
|
760
|
+
input.addEventListener('keydown', (event) => {
|
|
761
|
+
if (event.key === 'Enter' && !event.isComposing && (!event.shiftKey || event.metaKey || event.ctrlKey)) {
|
|
762
|
+
event.preventDefault();
|
|
763
|
+
submit();
|
|
764
|
+
}
|
|
765
|
+
});
|
|
766
|
+
go.addEventListener('click', submit);
|
|
767
|
+
bindBar(root, inner, {
|
|
768
|
+
isBusy: () => state.busy,
|
|
769
|
+
hasMenu: () => Boolean(state.menu),
|
|
770
|
+
closeMenu: () => { state.menu = null; render(); },
|
|
771
|
+
leave,
|
|
772
|
+
fit: fitInput,
|
|
773
|
+
});
|
|
774
|
+
whenPageLoaded(() => {
|
|
775
|
+
const askDrawerEl = document.getElementById('ask-ai-drawer');
|
|
776
|
+
if (askDrawerEl) panelLocation = askDrawerEl.querySelector('.ask-ai-drawer-location-input');
|
|
777
|
+
// know the agent and the running count without being asked
|
|
778
|
+
loadTools().then(loadSessions);
|
|
779
|
+
});
|
|
780
|
+
// a shell opened from the top bar's shell button (agent-bar-shell.js)
|
|
781
|
+
window.addEventListener('pinokio:shell-opened', () => window.setTimeout(loadSessions, 1500));
|
|
782
|
+
|
|
783
|
+
render();
|
|
784
|
+
return root;
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
// From the moment this file runs (in <head>) until the page is rearranged, the
|
|
788
|
+
// top bar's old app pill and its Ask AI and Feed buttons are kept invisible by
|
|
789
|
+
// agent-bar-app.css, so they can never be drawn in their old place first.
|
|
790
|
+
const PENDING_CLASS = 'agent-bar-app-pending';
|
|
791
|
+
if (!isPhoneLayout()) document.documentElement.classList.add(PENDING_CLASS);
|
|
792
|
+
const clearPending = () => document.documentElement.classList.remove(PENDING_CLASS);
|
|
793
|
+
|
|
794
|
+
let mounted = false;
|
|
795
|
+
function mount(options) {
|
|
796
|
+
if (mounted) return;
|
|
797
|
+
const h1 = document.querySelector('header.navheader h1');
|
|
798
|
+
if (!h1 || h1.querySelector('.agent-bar')) return;
|
|
799
|
+
const identity = h1.querySelector(':scope > .app-header-identity');
|
|
800
|
+
const sidebar = document.querySelector('#app-sidebar .menu-scroller');
|
|
801
|
+
const slot = h1.querySelector(':scope > .flexible');
|
|
802
|
+
if (!identity || !sidebar || !slot) return;
|
|
803
|
+
mounted = true;
|
|
804
|
+
|
|
805
|
+
const titleNode = identity.querySelector('.app-header-identity-title');
|
|
806
|
+
const appName = titleNode && titleNode.textContent.trim() ? titleNode.textContent.trim() : 'this app';
|
|
807
|
+
moveCard(identity, sidebar);
|
|
808
|
+
addNewsRow(identity);
|
|
809
|
+
const bar = addBar(h1, slot, appName);
|
|
810
|
+
followLayout(h1, bar, slot, options && options.mobileQuery);
|
|
811
|
+
document.documentElement.classList.add('agent-bar-app');
|
|
812
|
+
clearPending();
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
// app.ejs calls this as soon as the top bar and sidebar are in the page.
|
|
816
|
+
window.PinokioAgentBarApp = { mount };
|
|
817
|
+
whenPageLoaded(() => {
|
|
818
|
+
mount();
|
|
819
|
+
clearPending(); // whatever happened, never leave the top bar's own parts hidden
|
|
820
|
+
});
|
|
821
|
+
})(window, document);
|