pinokiod 8.3.32 → 8.3.35

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,375 @@
1
+ // The card for a plugin that opens a desktop app (server/public/agent-bar-launch.js).
2
+ //
3
+ // The real script runs here with the bar's real helpers. The plugin's page is a
4
+ // stand-in that has only what the card looks for (the page's Stop, the folder
5
+ // question), and what that page would tell its parent is sent by the test.
6
+ // Layout and the look of the card are not covered: this page lays nothing out.
7
+ const assert = require('node:assert/strict')
8
+ const fs = require('node:fs')
9
+ const path = require('node:path')
10
+ const test = require('node:test')
11
+ const { JSDOM, ResourceLoader } = require('jsdom')
12
+
13
+ const script = (name) => fs.readFileSync(path.resolve(__dirname, '../server/public', name), 'utf8')
14
+
15
+ class PluginPage extends ResourceLoader {
16
+ fetch() {
17
+ const page = Promise.resolve(Buffer.from('<!doctype html><div class="run"><button class="stop"></button></div><div id="terminal"></div>'))
18
+ // a page still on its way when the test's window closes
19
+ page.abort = () => {}
20
+ return page
21
+ }
22
+ }
23
+
24
+ const MENU = {
25
+ menu: [
26
+ { href: '/pinokio/run/plugin/codex/pinokio.js', title: 'Codex', category: 'cli', default: true },
27
+ { href: '/pinokio/run/plugin/codex-desktop/pinokio.js', title: 'Codex Desktop', category: 'ide', link: 'https://openai.com/codex', image: '/codex.png' },
28
+ { href: '/pinokio/run/plugin/cursor/pinokio.js', title: 'Cursor', category: 'ide', link: 'https://cursor.com' },
29
+ ],
30
+ }
31
+ const DESKTOP = '/pinokio/run/plugin/codex-desktop/pinokio.js?cwd=%2Fhome%2Fapi%2Fdemo&session=s1&ask_ai=1'
32
+ const pause = (window, ms) => new Promise((resolve) => window.setTimeout(resolve, ms))
33
+
34
+ async function page(t, options = {}) {
35
+ const dom = new JSDOM('<!doctype html><body class="main-sidebar-page"><header class="navheader"><h1><div class="flexible"></div></h1></header></body>', {
36
+ url: 'http://127.0.0.1:42000/home',
37
+ resources: new PluginPage(),
38
+ runScripts: 'outside-only',
39
+ pretendToBeVisual: true,
40
+ })
41
+ t.after(() => dom.window.close())
42
+ const { window } = dom
43
+ const { document } = window
44
+ window.innerWidth = 1760
45
+ window.matchMedia = () => ({ matches: false, addEventListener() {}, removeEventListener() {} })
46
+ window.Element.prototype.getBoundingClientRect = () => ({ left: 300, top: 8, right: 900, bottom: 40, width: 600, height: 32 })
47
+ window.fetch = async (url) => {
48
+ if (url === '/info/shells') return { ok: true, json: async () => [] }
49
+ return { ok: true, json: async () => MENU }
50
+ }
51
+ const visited = []
52
+ window.open = (...args) => { visited.push(args) }
53
+ const names = ['agent-bar.js', 'agent-bar-launch.js'].concat(options.dock ? ['agent-bar-panel.js'] : [])
54
+ for (const name of names) window.eval(script(name))
55
+ await window.PinokioAgentBar.ready
56
+ const launch = window.PinokioAgentBarLaunch
57
+ Object.assign(launch.times, { close: 60, start: 400, run: 400, answer: 80, poll: 10 }, options.times)
58
+
59
+ const H = window.PinokioAgentBar.helpers
60
+ const tools = MENU.menu.map((plugin) => Object.assign(H.mapPluginMenuToTools([plugin])[0], { href: plugin.href }))
61
+ const calls = []
62
+ const dock = {
63
+ tools,
64
+ open: (session, settings) => calls.push(['open', session.key, settings]),
65
+ show: (session) => calls.push(['show', session.key]),
66
+ }
67
+ const session = { key: '/plugin/codex-desktop/pinokio.js|/home/api/demo|s1', kind: 'agent', label: 'Codex Desktop', placeName: 'demo' }
68
+ const card = () => document.querySelector('.agent-launch__card')
69
+ const frame = () => document.querySelector('.agent-launch__frame')
70
+ // the plugin's page, once it is there, with its Stop presses counted
71
+ const loaded = async () => {
72
+ const target = frame()
73
+ for (let i = 0; i < 100 && !(target.contentDocument && target.contentDocument.querySelector('.stop')); i += 1) await pause(window, 10)
74
+ const doc = target.contentDocument
75
+ const pressed = { stop: 0 }
76
+ doc.querySelector('.stop').addEventListener('click', () => { pressed.stop += 1 })
77
+ return { doc, pressed }
78
+ }
79
+ // what the plugin's page tells the page it is shown in
80
+ const tell = (data, source) => window.dispatchEvent(new window.MessageEvent('message', { data, source: source || frame().contentWindow }))
81
+ const step = (name, data) => tell({ type: 'pinokio:session-step', step: name, data })
82
+ const text = (selector) => {
83
+ const node = card() && card().querySelector(selector)
84
+ return node && !node.hidden ? node.textContent : ''
85
+ }
86
+ const buttons = () => Array.from(card().querySelectorAll('.agent-launch__actions button')).map((button) => button.textContent)
87
+ const press = (label) => Array.from(card().querySelectorAll('.agent-launch__actions button')).find((button) => button.textContent === label).click()
88
+ return { window, document, launch, dock, calls, session, card, frame, loaded, tell, step, text, buttons, press, visited }
89
+ }
90
+
91
+ test('a plugin that runs in a terminal is left to the dock', { timeout: 5000 }, async (t) => {
92
+ const p = await page(t)
93
+ const taken = p.launch.take({ key: 'k', kind: 'agent' }, '/pinokio/run/plugin/codex/pinokio.js?cwd=%2Fhome%2Fapi%2Fdemo', p.dock)
94
+ assert.equal(taken, false)
95
+ assert.equal(p.card(), null)
96
+ // so is a shell, and a plugin the list does not know
97
+ assert.equal(p.launch.take({ key: 's', kind: 'shell' }, DESKTOP, p.dock), false)
98
+ assert.equal(p.launch.take({ key: 'u', kind: 'agent' }, '/pinokio/run/plugin/unknown/pinokio.js', p.dock), false)
99
+ })
100
+
101
+ test('a desktop app: opening, then opened, then the card closes by itself', { timeout: 5000 }, async (t) => {
102
+ const p = await page(t)
103
+ assert.equal(p.launch.take(p.session, DESKTOP, p.dock), true)
104
+ assert.equal(p.card().dataset.open, 'true')
105
+ assert.equal(p.text('.agent-launch__title'), 'Opening Codex Desktop…')
106
+ assert.equal(p.text('.agent-launch__sub'), 'demo')
107
+ assert.ok(p.card().querySelector('.agent-launch__spinner'))
108
+ assert.equal(p.card().querySelector('.agent-launch__close').hidden, true)
109
+ // the plugin is started by loading its own page, with what was asked
110
+ assert.equal(new URL(p.frame().src).pathname + new URL(p.frame().src).search, DESKTOP)
111
+ assert.ok(p.frame().hasAttribute('data-skip-startup-focus'))
112
+
113
+ const { pressed } = await p.loaded()
114
+ p.tell({ type: 'start', data: { current: 0, total: 2 } })
115
+ assert.equal(p.text('.agent-launch__title'), 'Opening Codex Desktop…')
116
+ // the plugin has reached its step that waits for Stop: the app is open
117
+ p.step('process.wait.start', { title: 'Launched', description: 'Click the stop button to stop watching file changes' })
118
+ assert.equal(p.text('.agent-launch__title'), 'Opened in Codex Desktop')
119
+ assert.ok(p.card().querySelector('.agent-launch__check'))
120
+ assert.equal(pressed.stop, 1, 'the page\'s own Stop ends the waiting plugin')
121
+ assert.equal(p.card().querySelector('.agent-launch__timer').hidden, false)
122
+
123
+ await pause(p.window, 150)
124
+ assert.equal(p.card().dataset.open, 'false')
125
+ await pause(p.window, 300)
126
+ assert.equal(p.card(), null, 'the card, and the page in it, are gone')
127
+ assert.deepEqual(p.calls, [], 'the dock is asked for no window')
128
+ })
129
+
130
+ test('pointing at the card holds it open', { timeout: 5000 }, async (t) => {
131
+ const p = await page(t)
132
+ p.launch.take(p.session, DESKTOP, p.dock)
133
+ await p.loaded()
134
+ p.tell({ type: 'start' })
135
+ p.step('process.wait.start', {})
136
+ p.card().dispatchEvent(new p.window.MouseEvent('mouseenter'))
137
+ await pause(p.window, 200)
138
+ assert.equal(p.card().dataset.open, 'true')
139
+ p.card().dispatchEvent(new p.window.MouseEvent('mouseleave'))
140
+ await pause(p.window, 150)
141
+ assert.equal(p.card().dataset.open, 'false')
142
+ })
143
+
144
+ test('a plugin whose steps simply end has opened the app too, and nothing is stopped', { timeout: 5000 }, async (t) => {
145
+ const p = await page(t)
146
+ p.launch.take(Object.assign({}, p.session, { key: 'cursor' }), '/pinokio/run/plugin/cursor/pinokio.js?cwd=%2Fhome%2Fapi%2Fdemo', p.dock)
147
+ const { pressed } = await p.loaded()
148
+ // an end before any start is not this plugin's end
149
+ p.tell({ type: 'disconnect' })
150
+ assert.equal(p.text('.agent-launch__title'), 'Opening Cursor…')
151
+ p.tell({ type: 'start' })
152
+ p.tell({ type: 'disconnect' })
153
+ assert.equal(p.text('.agent-launch__title'), 'Opened in Cursor')
154
+ assert.equal(pressed.stop, 0)
155
+ })
156
+
157
+ test('a step that waits for something else is not "opened"', { timeout: 5000 }, async (t) => {
158
+ const p = await page(t)
159
+ p.launch.take(p.session, DESKTOP, p.dock)
160
+ await p.loaded()
161
+ p.tell({ type: 'start' })
162
+ p.step('process.wait.start', { title: 'Warming up', sec: 5 })
163
+ p.step('process.wait.start', { title: 'Waiting for the server', url: 'http://localhost:1234' })
164
+ assert.equal(p.text('.agent-launch__title'), 'Opening Codex Desktop…')
165
+ })
166
+
167
+ test('an error keeps the card, with the reason, the plugin\'s homepage and a way to try again', { timeout: 5000 }, async (t) => {
168
+ const p = await page(t)
169
+ p.launch.take(p.session, DESKTOP, p.dock)
170
+ await p.loaded()
171
+ const first = p.frame()
172
+ p.tell({ type: 'start' })
173
+ p.step('error', 'Error: Nothing on this computer could open the codex: link. The app for it may not be installed.\nopen exited with code 1\n at URI.open (/kernel/api/uri/index.js:50:13)')
174
+ // the end that follows an error changes nothing
175
+ p.tell({ type: 'disconnect' })
176
+ await pause(p.window, 20)
177
+ assert.equal(p.text('.agent-launch__title'), 'Couldn\'t open Codex Desktop')
178
+ assert.equal(p.text('.agent-launch__sub'), 'Nothing on this computer could open the codex: link. The app for it may not be installed.')
179
+ assert.ok(p.card().querySelector('.agent-launch__warn'))
180
+ assert.deepEqual(p.buttons(), ['Get Codex Desktop', 'Try again', 'Show details'])
181
+ assert.equal(p.card().querySelector('.agent-launch__close').hidden, false)
182
+
183
+ const details = p.card().querySelector('.agent-launch__details')
184
+ assert.equal(details.hidden, true)
185
+ p.press('Show details')
186
+ assert.equal(details.hidden, false)
187
+ assert.equal(details.textContent, 'Nothing on this computer could open the codex: link. The app for it may not be installed.\nopen exited with code 1')
188
+ assert.deepEqual(p.buttons(), ['Get Codex Desktop', 'Try again', 'Hide details'])
189
+
190
+ p.press('Get Codex Desktop')
191
+ assert.deepEqual(p.visited, [['https://openai.com/codex', '_blank', 'noopener']])
192
+
193
+ // it stays until it is dismissed
194
+ await pause(p.window, 500)
195
+ assert.equal(p.card().dataset.open, 'true')
196
+ assert.deepEqual(p.calls, [])
197
+
198
+ p.press('Try again')
199
+ assert.equal(p.text('.agent-launch__title'), 'Opening Codex Desktop…')
200
+ assert.notEqual(p.frame(), first, 'the plugin\'s page is loaded again')
201
+ assert.equal(p.document.querySelectorAll('.agent-launch__frame').length, 1)
202
+ assert.deepEqual(p.buttons(), [])
203
+
204
+ p.tell({ type: 'start' })
205
+ p.step('error', 'Error: again')
206
+ await pause(p.window, 20)
207
+ // one line of error: nothing more to show
208
+ assert.deepEqual(p.buttons(), ['Get Codex Desktop', 'Try again'])
209
+ p.card().querySelector('.agent-launch__close').click()
210
+ assert.equal(p.card().dataset.open, 'false')
211
+ })
212
+
213
+ test('a plugin that says something instead keeps the card, with its words and its link', { timeout: 5000 }, async (t) => {
214
+ const p = await page(t)
215
+ p.launch.take(Object.assign({}, p.session, { key: 'cursor' }), '/pinokio/run/plugin/cursor/pinokio.js?cwd=%2Fhome%2Fapi%2Fdemo', p.dock)
216
+ await p.loaded()
217
+ p.tell({ type: 'start' })
218
+ p.step('notify', { html: 'Cursor is <b>not installed</b>. Click to visit the Cursor homepage to download', href: 'https://www.cursor.com', target: '_blank' })
219
+ p.tell({ type: 'disconnect' })
220
+ assert.equal(p.text('.agent-launch__title'), 'Cursor')
221
+ assert.equal(p.text('.agent-launch__sub'), 'Cursor is not installed. Click to visit the Cursor homepage to download')
222
+ assert.deepEqual(p.buttons(), ['Open cursor.com', 'Try again'])
223
+ p.press('Open cursor.com')
224
+ assert.deepEqual(p.visited, [['https://www.cursor.com/', '_blank', 'noopener']])
225
+ await pause(p.window, 500)
226
+ assert.equal(p.card().dataset.open, 'true')
227
+
228
+ // a link that is not a web address gets no button
229
+ p.press('Try again')
230
+ p.tell({ type: 'start' })
231
+ p.step('notify', { html: 'Hello', href: 'javascript:alert(1)' })
232
+ assert.deepEqual(p.buttons(), ['Try again'])
233
+ })
234
+
235
+ test('Pinokio\'s question about the folder is shown in the card', { timeout: 5000 }, async (t) => {
236
+ const p = await page(t, { times: { start: 150 } })
237
+ p.launch.take(p.session, DESKTOP, p.dock)
238
+ const { doc } = await p.loaded()
239
+ assert.equal(p.card().dataset.stage, 'false')
240
+ const question = doc.createElement('div')
241
+ question.className = 'ai-consent-overlay is-visible'
242
+ doc.body.appendChild(question)
243
+ await pause(p.window, 40)
244
+ assert.equal(p.card().dataset.stage, 'true')
245
+ assert.equal(p.text('.agent-launch__title'), 'Codex Desktop needs your OK')
246
+ assert.equal(p.card().querySelector('.agent-launch__close').hidden, false)
247
+ // however long the answer takes, the card waits
248
+ await pause(p.window, 300)
249
+ assert.equal(p.card().dataset.open, 'true')
250
+ assert.deepEqual(p.calls, [])
251
+
252
+ // allowed: the plugin starts and the card carries on
253
+ question.remove()
254
+ await pause(p.window, 40)
255
+ assert.equal(p.card().dataset.stage, 'false')
256
+ assert.equal(p.text('.agent-launch__title'), 'Opening Codex Desktop…')
257
+ p.tell({ type: 'start' })
258
+ p.step('process.wait.start', {})
259
+ assert.equal(p.text('.agent-launch__title'), 'Opened in Codex Desktop')
260
+ })
261
+
262
+ test('the folder question answered and the plugin started before the card looked again: the page is put away', { timeout: 5000 }, async (t) => {
263
+ // the card looks for the question less often than the plugin takes to start
264
+ const p = await page(t, { times: { poll: 60 } })
265
+ p.launch.take(p.session, DESKTOP, p.dock)
266
+ const { doc } = await p.loaded()
267
+ const question = doc.createElement('div')
268
+ question.className = 'ai-consent-overlay is-visible'
269
+ doc.body.appendChild(question)
270
+ await pause(p.window, 120)
271
+ assert.equal(p.card().dataset.stage, 'true')
272
+ question.remove()
273
+ p.tell({ type: 'start' })
274
+ assert.equal(p.card().dataset.stage, 'false')
275
+ assert.equal(p.text('.agent-launch__title'), 'Opening Codex Desktop…')
276
+ p.step('process.wait.start', {})
277
+ assert.equal(p.card().dataset.stage, 'false')
278
+ assert.equal(p.text('.agent-launch__title'), 'Opened in Codex Desktop')
279
+ })
280
+
281
+ test('the folder question cancelled: nothing starts and the card goes', { timeout: 5000 }, async (t) => {
282
+ const p = await page(t)
283
+ p.launch.take(p.session, DESKTOP, p.dock)
284
+ const { doc } = await p.loaded()
285
+ const question = doc.createElement('div')
286
+ question.className = 'ai-consent-overlay is-visible'
287
+ doc.body.appendChild(question)
288
+ await pause(p.window, 40)
289
+ question.remove()
290
+ await pause(p.window, 200)
291
+ assert.equal(p.card().dataset.open, 'false')
292
+ assert.deepEqual(p.calls, [])
293
+ })
294
+
295
+ test('a page that never starts the plugin is handed to the dock, to load in a window', { timeout: 5000 }, async (t) => {
296
+ const p = await page(t, { times: { start: 80 } })
297
+ p.launch.take(p.session, DESKTOP, p.dock)
298
+ await pause(p.window, 200)
299
+ assert.equal(p.card(), null)
300
+ // (the settings object is made in the page, so it is compared as text)
301
+ assert.equal(JSON.stringify(p.calls), JSON.stringify([['open', p.session.key, { launchUrl: DESKTOP, window: true }]]))
302
+ })
303
+
304
+ test('a plugin that starts and gets nowhere is handed to the dock, to show if it still runs', { timeout: 5000 }, async (t) => {
305
+ const p = await page(t, { times: { run: 80 } })
306
+ p.launch.take(p.session, DESKTOP, p.dock)
307
+ await p.loaded()
308
+ p.tell({ type: 'start' })
309
+ // every step says it starts; only the first one counts
310
+ await pause(p.window, 50)
311
+ p.tell({ type: 'start' })
312
+ await pause(p.window, 60)
313
+ assert.equal(p.card(), null)
314
+ assert.deepEqual(p.calls, [['show', p.session.key]])
315
+ })
316
+
317
+ test('only the card\'s own page is listened to', { timeout: 5000 }, async (t) => {
318
+ const p = await page(t)
319
+ p.launch.take(p.session, DESKTOP, p.dock)
320
+ await p.loaded()
321
+ p.tell({ type: 'start' }, p.window)
322
+ p.tell({ type: 'pinokio:session-step', step: 'error', data: 'Error: from another page' }, p.window)
323
+ p.tell({ type: 'disconnect' }, null)
324
+ assert.equal(p.text('.agent-launch__title'), 'Opening Codex Desktop…')
325
+ })
326
+
327
+ test('the same launch sent again replaces its card', { timeout: 5000 }, async (t) => {
328
+ const p = await page(t)
329
+ p.launch.take(p.session, DESKTOP, p.dock)
330
+ p.launch.take(p.session, DESKTOP, p.dock)
331
+ assert.equal(p.document.querySelectorAll('.agent-launch__card').length, 1)
332
+ p.launch.take(Object.assign({}, p.session, { key: 'other' }), DESKTOP, p.dock)
333
+ assert.equal(p.document.querySelectorAll('.agent-launch__card').length, 2)
334
+ })
335
+
336
+ test('the dock: a desktop app gets the card and no window or tab; a terminal agent gets its window', { timeout: 5000 }, async (t) => {
337
+ const p = await page(t, { dock: true })
338
+ const { document, window } = p
339
+ for (let i = 0; i < 50 && typeof window.PinokioAgentBar.deliver !== 'function'; i += 1) await pause(window, 10)
340
+ // the dock knows the plugins before a send
341
+ await pause(window, 50)
342
+
343
+ assert.equal(window.PinokioAgentBar.deliver({
344
+ type: 'ask', cwd: '/home/api/demo', prompt: 'hello', tool: { label: 'Codex Desktop' },
345
+ url: '/pinokio/run/plugin/codex-desktop/pinokio.js?cwd=%2Fhome%2Fapi%2Fdemo&session=s1&ask_ai=1&prompt=hello',
346
+ }), true)
347
+ assert.equal(document.querySelectorAll('.agent-launch__card').length, 1)
348
+ assert.equal(document.querySelectorAll('.agent-bar-panel').length, 0)
349
+ assert.equal(document.querySelectorAll('.agent-dock__tab').length, 0)
350
+ assert.equal(new URL(p.frame().src).searchParams.get('prompt'), 'hello')
351
+
352
+ assert.equal(window.PinokioAgentBar.deliver({
353
+ type: 'ask', cwd: '/home/api/demo', prompt: 'hello', tool: { label: 'Codex' },
354
+ url: '/pinokio/run/plugin/codex/pinokio.js?cwd=%2Fhome%2Fapi%2Fdemo&session=s2&ask_ai=1&prompt=hello',
355
+ }), true)
356
+ assert.equal(document.querySelectorAll('.agent-launch__card').length, 1)
357
+ assert.equal(document.querySelectorAll('.agent-bar-panel').length, 1)
358
+ })
359
+
360
+ test('the dock: a launch the card hands back opens in a window', { timeout: 5000 }, async (t) => {
361
+ const p = await page(t, { dock: true, times: { start: 60 } })
362
+ const { document, window } = p
363
+ for (let i = 0; i < 50 && typeof window.PinokioAgentBar.deliver !== 'function'; i += 1) await pause(window, 10)
364
+ await pause(window, 50)
365
+ window.PinokioAgentBar.deliver({
366
+ type: 'ask', cwd: '/home/api/demo', tool: { label: 'Codex Desktop' },
367
+ url: '/pinokio/run/plugin/codex-desktop/pinokio.js?cwd=%2Fhome%2Fapi%2Fdemo&session=s1&ask_ai=1',
368
+ })
369
+ assert.equal(document.querySelectorAll('.agent-bar-panel').length, 0)
370
+ await pause(window, 150)
371
+ assert.equal(document.querySelectorAll('.agent-launch__card').length, 0)
372
+ const frame = document.querySelector('.agent-bar-panel .agent-bar-panel__frame')
373
+ assert.ok(frame, 'the session\'s page is in a dock window')
374
+ assert.equal(new URL(frame.src).pathname, '/pinokio/run/plugin/codex-desktop/pinokio.js')
375
+ })
@@ -98,3 +98,26 @@ test('uri.open dispatches the exact built URI to Util.openURI', async () => {
98
98
  Util.openURI = originalOpenURI
99
99
  }
100
100
  })
101
+
102
+ test('uri.open fails the step when the system could not open the link', async () => {
103
+ const api = new URI()
104
+ const originalOpenURI = Util.openURI
105
+ const printed = []
106
+
107
+ Util.openURI = async () => ({ ok: false, error: 'open exited with code 1' })
108
+
109
+ try {
110
+ await assert.rejects(
111
+ api.open({ params: { uri: 'codex://new', params: { path: '/tmp/demo' } } }, (packet) => printed.push(packet)),
112
+ (error) => {
113
+ assert.match(error.message, /could open the codex: link/)
114
+ assert.match(error.message, /may not be installed/)
115
+ assert.match(error.message, /open exited with code 1/)
116
+ return true
117
+ }
118
+ )
119
+ assert.equal(printed.length, 1)
120
+ } finally {
121
+ Util.openURI = originalOpenURI
122
+ }
123
+ })