pinokiod 8.4.0 → 8.4.1
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/package.json +1 -1
- package/server/public/agent-bar-compose.js +7 -0
- package/server/public/agent-bar-launch.js +45 -0
- package/server/public/agent-bar-search.css +45 -0
- package/server/public/agent-bar-search.js +265 -0
- package/server/public/agent-bar-shortcut.css +30 -0
- package/server/public/agent-bar-shortcut.js +103 -0
- package/server/public/agent-bar.js +3 -1
- package/server/public/request-strip.js +146 -0
- package/server/views/app.ejs +2 -0
- package/server/views/partials/main_sidebar.ejs +4 -0
- package/server/views/terminal.ejs +5 -0
- package/system/plugin/cursor/pinokio.js +11 -0
- package/system/plugin/vscode/pinokio.js +78 -12
- package/test/agent-bar-launch.test.js +47 -0
- package/test/agent-bar-search.test.js +314 -0
- package/test/agent-bar-shortcut.test.js +215 -0
- package/test/desktop-plugin-requests.test.js +112 -0
- package/test/request-strip.test.js +110 -0
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
// The agent bar's app search (server/public/agent-bar-search.js): the apps
|
|
2
|
+
// listed inside the open box as you type, that Enter asks until the arrows have
|
|
3
|
+
// picked one, how an app is opened, and what differs on home.
|
|
4
|
+
//
|
|
5
|
+
// The real agent-bar.js, agent-bar-compose.js and agent-bar-search.js run here
|
|
6
|
+
// in a page with a top bar. The home page also has its hidden search box and
|
|
7
|
+
// its list of apps.
|
|
8
|
+
const assert = require('node:assert/strict')
|
|
9
|
+
const fs = require('node:fs')
|
|
10
|
+
const path = require('node:path')
|
|
11
|
+
const test = require('node:test')
|
|
12
|
+
const { JSDOM } = require('jsdom')
|
|
13
|
+
|
|
14
|
+
const publicDir = path.resolve(__dirname, '../server/public')
|
|
15
|
+
const script = (name) => fs.readFileSync(path.join(publicDir, name), 'utf8')
|
|
16
|
+
const barScript = script('agent-bar.js')
|
|
17
|
+
const composeScript = script('agent-bar-compose.js')
|
|
18
|
+
const searchScript = script('agent-bar-search.js')
|
|
19
|
+
|
|
20
|
+
const PHONE = '(max-width: 768px)'
|
|
21
|
+
const HINT = 'Search apps or ask Pinokio…'
|
|
22
|
+
// as /info/apps lists them: by folder
|
|
23
|
+
const APPS = [
|
|
24
|
+
{ name: 'automatic1111', title: 'Stable Diffusion WebUI', description: 'Make pictures from text', icon: '/asset/api/automatic1111/icon.png' },
|
|
25
|
+
{ name: 'comfy.git', title: 'ComfyUI', description: 'Node based image generation', icon: '' },
|
|
26
|
+
{ name: 'open-webui', title: 'Open WebUI', description: 'Chat with local models', icon: '/asset/api/open-webui/icon.png' },
|
|
27
|
+
{ name: 'webui-tools', title: 'webui-tools', description: 'Helpers', icon: '' },
|
|
28
|
+
]
|
|
29
|
+
const line = (app, index) => `<div class="line home-app-line" data-index="${index}" data-name="${app.title}" data-uri="${app.name}"></div>`
|
|
30
|
+
const TOP = '<header class="navheader"><h1><div class="flexible"></div></h1></header>'
|
|
31
|
+
const HOME = `<!doctype html><body class="is-home">${TOP}<main id="page">`
|
|
32
|
+
+ '<form class="search home-search-form"><input type="search" class="flexible"></form>'
|
|
33
|
+
+ `<div class="running-apps">${APPS.map(line).join('')}</div></main></body>`
|
|
34
|
+
const OTHER = `<!doctype html><body>${TOP}<main id="page"><button id="elsewhere">x</button></main></body>`
|
|
35
|
+
|
|
36
|
+
async function settle(window) {
|
|
37
|
+
for (let i = 0; i < 6; i += 1) await new Promise((resolve) => window.setTimeout(resolve, 0))
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function page(t, options = {}) {
|
|
41
|
+
const media = new Set(options.media || [])
|
|
42
|
+
const posts = []
|
|
43
|
+
const reads = []
|
|
44
|
+
const dom = new JSDOM(options.home ? HOME : OTHER, {
|
|
45
|
+
url: `http://127.0.0.1:42000/${options.home ? 'home' : 'plugins'}${options.search || ''}`,
|
|
46
|
+
runScripts: 'outside-only',
|
|
47
|
+
pretendToBeVisual: true,
|
|
48
|
+
beforeParse(window) {
|
|
49
|
+
window.matchMedia = (query) => ({ matches: media.has(query), addEventListener() {}, removeEventListener() {} })
|
|
50
|
+
window.Element.prototype.getBoundingClientRect = function () {
|
|
51
|
+
return { left: 300, top: 8, right: 900, bottom: 608, width: 600, height: 600, x: 300, y: 8 }
|
|
52
|
+
}
|
|
53
|
+
window.Element.prototype.scrollIntoView = function () {}
|
|
54
|
+
window.fetch = async (url, init) => {
|
|
55
|
+
if (init && init.method === 'POST') posts.push(String(url))
|
|
56
|
+
if (String(url) === '/info/shells') return { ok: true, json: async () => [] }
|
|
57
|
+
if (String(url) === '/info/apps') {
|
|
58
|
+
reads.push(String(url))
|
|
59
|
+
return options.noApps ? { ok: false, json: async () => ({}) } : { ok: true, json: async () => ({ apps: APPS }) }
|
|
60
|
+
}
|
|
61
|
+
if (String(url).split('?')[0] === '/api/plugin/menu') {
|
|
62
|
+
return { ok: true, json: async () => ({ menu: [{ href: '/run/plugin/codex/pinokio.js', title: 'Codex', default: true }] }) }
|
|
63
|
+
}
|
|
64
|
+
return { ok: false, json: async () => ({ ok: false, error: 'not sent in this test' }) }
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
t.after(() => dom.window.close())
|
|
69
|
+
const { window } = dom
|
|
70
|
+
const { document } = window
|
|
71
|
+
const clicked = []
|
|
72
|
+
const pageKeys = []
|
|
73
|
+
document.querySelectorAll('.line').forEach((node) => {
|
|
74
|
+
node.addEventListener('click', () => clicked.push(node.dataset.uri))
|
|
75
|
+
})
|
|
76
|
+
document.addEventListener('keydown', (event) => pageKeys.push(event.key))
|
|
77
|
+
window.eval(barScript)
|
|
78
|
+
window.eval(composeScript)
|
|
79
|
+
window.eval(searchScript)
|
|
80
|
+
const went = []
|
|
81
|
+
window.PinokioAgentBar.navigate = (url) => went.push(url)
|
|
82
|
+
await window.PinokioAgentBar.ready
|
|
83
|
+
await settle(window)
|
|
84
|
+
const root = document.querySelector('.agent-bar')
|
|
85
|
+
const input = root.querySelector('.agent-bar__input')
|
|
86
|
+
const rows = () => Array.from(root.querySelectorAll('.agent-bar-search__rows:not([hidden]) .agent-bar__row'))
|
|
87
|
+
return {
|
|
88
|
+
window,
|
|
89
|
+
document,
|
|
90
|
+
root,
|
|
91
|
+
input,
|
|
92
|
+
posts,
|
|
93
|
+
reads,
|
|
94
|
+
clicked,
|
|
95
|
+
went,
|
|
96
|
+
pageKeys,
|
|
97
|
+
rows,
|
|
98
|
+
shape: () => root.dataset.compose || '',
|
|
99
|
+
titles: () => rows().map((row) => row.querySelector('.agent-bar__row-label').textContent),
|
|
100
|
+
picked: () => rows().filter((row) => row.classList.contains('is-current')).map((row) => row.querySelector('.agent-bar__row-label').textContent),
|
|
101
|
+
async focus() {
|
|
102
|
+
input.focus()
|
|
103
|
+
await settle(window)
|
|
104
|
+
},
|
|
105
|
+
async type(text) {
|
|
106
|
+
input.value = text
|
|
107
|
+
input.dispatchEvent(new window.Event('input', { bubbles: true }))
|
|
108
|
+
await settle(window)
|
|
109
|
+
},
|
|
110
|
+
async key(init) {
|
|
111
|
+
const event = new window.KeyboardEvent('keydown', { bubbles: true, cancelable: true, ...init })
|
|
112
|
+
input.dispatchEvent(event)
|
|
113
|
+
await settle(window)
|
|
114
|
+
return event
|
|
115
|
+
},
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
test('going to the bar opens its box as before, and the empty text says it searches too', async (t) => {
|
|
120
|
+
const p = await page(t)
|
|
121
|
+
assert.equal(p.input.placeholder, HINT)
|
|
122
|
+
await p.focus()
|
|
123
|
+
assert.equal(p.shape(), 'place')
|
|
124
|
+
assert.equal(p.input.placeholder, 'Search apps or ask Pinokio')
|
|
125
|
+
assert.deepEqual(p.titles(), [])
|
|
126
|
+
assert.deepEqual(p.reads, ['/info/apps'])
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
test('typing lists the matching apps in the box, best match first, and picks none', async (t) => {
|
|
130
|
+
const p = await page(t)
|
|
131
|
+
await p.focus()
|
|
132
|
+
await p.type('webui')
|
|
133
|
+
// title starts with it; a word of the title starts with it (in the order given); the title holds it
|
|
134
|
+
assert.deepEqual(p.titles(), ['webui-tools', 'Stable Diffusion WebUI', 'Open WebUI'])
|
|
135
|
+
assert.deepEqual(p.picked(), [])
|
|
136
|
+
await p.type('fusion')
|
|
137
|
+
assert.deepEqual(p.titles(), ['Stable Diffusion WebUI'], 'the middle of a title')
|
|
138
|
+
await p.type('comfy.g')
|
|
139
|
+
assert.deepEqual(p.titles(), ['ComfyUI'], 'the folder')
|
|
140
|
+
await p.type('local models')
|
|
141
|
+
assert.deepEqual(p.titles(), ['Open WebUI'], 'the description')
|
|
142
|
+
await p.type('how do I make a video')
|
|
143
|
+
assert.deepEqual(p.titles(), [])
|
|
144
|
+
assert.equal(p.shape(), 'place')
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
test('a row shows the folder when it is not the title', async (t) => {
|
|
148
|
+
const p = await page(t)
|
|
149
|
+
await p.focus()
|
|
150
|
+
await p.type('webui')
|
|
151
|
+
const folders = p.rows().map((row) => (row.querySelector('.agent-bar-search__folder') || {}).textContent || '')
|
|
152
|
+
assert.deepEqual(folders, ['', 'automatic1111', 'open-webui'])
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
test('Enter asks, even when apps are listed', async (t) => {
|
|
156
|
+
const p = await page(t)
|
|
157
|
+
await p.focus()
|
|
158
|
+
await p.type('comfy')
|
|
159
|
+
assert.deepEqual(p.titles(), ['ComfyUI'])
|
|
160
|
+
await p.key({ key: 'Enter' })
|
|
161
|
+
assert.deepEqual(p.went, [])
|
|
162
|
+
assert.equal(p.posts.length, 1)
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
test('Down picks a row and Enter then opens that app and asks nothing', async (t) => {
|
|
166
|
+
const p = await page(t)
|
|
167
|
+
await p.focus()
|
|
168
|
+
await p.type('webui')
|
|
169
|
+
await p.key({ key: 'ArrowDown' })
|
|
170
|
+
assert.deepEqual(p.picked(), ['webui-tools'])
|
|
171
|
+
assert.equal(p.rows()[0].querySelector('.agent-bar__row-note').textContent, 'Open ↵')
|
|
172
|
+
await p.key({ key: 'ArrowDown' })
|
|
173
|
+
await p.key({ key: 'ArrowDown' })
|
|
174
|
+
await p.key({ key: 'ArrowDown' })
|
|
175
|
+
assert.deepEqual(p.picked(), ['Open WebUI'], 'it stops at the last row')
|
|
176
|
+
const event = await p.key({ key: 'Enter' })
|
|
177
|
+
assert.equal(event.defaultPrevented, true)
|
|
178
|
+
assert.deepEqual(p.went, ['/p/open-webui'])
|
|
179
|
+
assert.deepEqual(p.posts, [])
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
test('Up from the first row, or typing, goes back to asking', async (t) => {
|
|
183
|
+
const p = await page(t)
|
|
184
|
+
await p.focus()
|
|
185
|
+
await p.type('webui')
|
|
186
|
+
await p.key({ key: 'ArrowDown' })
|
|
187
|
+
await p.key({ key: 'ArrowUp' })
|
|
188
|
+
assert.deepEqual(p.picked(), [])
|
|
189
|
+
await p.key({ key: 'ArrowDown' })
|
|
190
|
+
await p.type('webu')
|
|
191
|
+
assert.deepEqual(p.picked(), [])
|
|
192
|
+
await p.key({ key: 'Enter' })
|
|
193
|
+
assert.deepEqual(p.went, [])
|
|
194
|
+
assert.equal(p.posts.length, 1)
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
test('Cmd+Enter asks even with a row picked', async (t) => {
|
|
198
|
+
const p = await page(t)
|
|
199
|
+
await p.focus()
|
|
200
|
+
await p.type('comfy')
|
|
201
|
+
await p.key({ key: 'ArrowDown' })
|
|
202
|
+
await p.key({ key: 'Enter', metaKey: true })
|
|
203
|
+
assert.deepEqual(p.went, [])
|
|
204
|
+
assert.equal(p.posts.length, 1)
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
test('a click on a row opens the app and keeps the cursor in the text', async (t) => {
|
|
208
|
+
const p = await page(t)
|
|
209
|
+
await p.focus()
|
|
210
|
+
await p.type('comfy')
|
|
211
|
+
const press = new p.window.Event('pointerdown', { bubbles: true, cancelable: true })
|
|
212
|
+
p.rows()[0].dispatchEvent(press)
|
|
213
|
+
assert.equal(press.defaultPrevented, true)
|
|
214
|
+
p.rows()[0].click()
|
|
215
|
+
assert.deepEqual(p.went, ['/p/comfy.git'])
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
test('no rows in another mode, on a second line, or with the box closed', async (t) => {
|
|
219
|
+
const p = await page(t)
|
|
220
|
+
await p.focus()
|
|
221
|
+
await p.type('comfy')
|
|
222
|
+
assert.equal(p.titles().length, 1)
|
|
223
|
+
p.window.PinokioAgentBar.choose('create_app')
|
|
224
|
+
await settle(p.window)
|
|
225
|
+
assert.deepEqual(p.titles(), [])
|
|
226
|
+
p.window.PinokioAgentBar.choose('ask')
|
|
227
|
+
await settle(p.window)
|
|
228
|
+
assert.equal(p.titles().length, 1)
|
|
229
|
+
await p.type('comfy\nmore')
|
|
230
|
+
assert.deepEqual(p.titles(), [])
|
|
231
|
+
await p.type('comfy')
|
|
232
|
+
await p.key({ key: 'Escape' })
|
|
233
|
+
assert.equal(p.shape(), '')
|
|
234
|
+
assert.deepEqual(p.titles(), [])
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
test('a list that cannot be read leaves the plain ask box', async (t) => {
|
|
238
|
+
const p = await page(t, { noApps: true })
|
|
239
|
+
await p.focus()
|
|
240
|
+
await p.type('comfy')
|
|
241
|
+
assert.deepEqual(p.titles(), [])
|
|
242
|
+
await p.key({ key: 'Enter' })
|
|
243
|
+
assert.equal(p.posts.length, 1)
|
|
244
|
+
})
|
|
245
|
+
|
|
246
|
+
test('a phone has no rows and the bar says what it said', async (t) => {
|
|
247
|
+
const p = await page(t, { media: [PHONE] })
|
|
248
|
+
assert.equal(p.input.placeholder, 'Ask Pinokio anything…')
|
|
249
|
+
await p.focus()
|
|
250
|
+
assert.equal(p.shape(), 'screen')
|
|
251
|
+
await p.type('comfy')
|
|
252
|
+
assert.deepEqual(p.titles(), [])
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
test('home: the cursor starts in the bar, its box closed, and the search box is hidden', async (t) => {
|
|
256
|
+
const p = await page(t, { home: true })
|
|
257
|
+
assert.equal(p.document.activeElement, p.input)
|
|
258
|
+
assert.equal(p.shape(), '')
|
|
259
|
+
assert.equal(p.input.placeholder, HINT)
|
|
260
|
+
assert.ok(p.document.documentElement.classList.contains('agent-bar-search-home'))
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
test('home: until the box opens, the arrows and Enter are the page\'s and nothing is sent', async (t) => {
|
|
264
|
+
const p = await page(t, { home: true })
|
|
265
|
+
await p.key({ key: 'ArrowDown' })
|
|
266
|
+
const event = await p.key({ key: 'Enter' })
|
|
267
|
+
assert.deepEqual(p.pageKeys, ['ArrowDown', 'Enter'])
|
|
268
|
+
assert.equal(event.defaultPrevented, true)
|
|
269
|
+
assert.deepEqual(p.posts, [])
|
|
270
|
+
assert.equal(p.shape(), '')
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
test('home: the first key opens the box with what was typed, and the page\'s list is left alone', async (t) => {
|
|
274
|
+
const p = await page(t, { home: true })
|
|
275
|
+
await p.type('c')
|
|
276
|
+
assert.equal(p.shape(), 'place')
|
|
277
|
+
assert.equal(p.document.activeElement, p.input)
|
|
278
|
+
await p.type('comfy')
|
|
279
|
+
assert.deepEqual(p.titles(), ['ComfyUI'])
|
|
280
|
+
assert.equal(p.document.querySelectorAll('.line.hidden, .line.selected').length, 0)
|
|
281
|
+
// the keys are the box's now
|
|
282
|
+
p.pageKeys.length = 0
|
|
283
|
+
await p.key({ key: 'ArrowDown' })
|
|
284
|
+
assert.deepEqual(p.pageKeys, [])
|
|
285
|
+
assert.deepEqual(p.picked(), ['ComfyUI'])
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
test('home: a click on the bar opens the box', async (t) => {
|
|
289
|
+
const p = await page(t, { home: true })
|
|
290
|
+
p.input.dispatchEvent(new p.window.Event('pointerdown', { bubbles: true }))
|
|
291
|
+
await settle(p.window)
|
|
292
|
+
assert.equal(p.shape(), 'place')
|
|
293
|
+
})
|
|
294
|
+
|
|
295
|
+
test('home: an app is opened by the page\'s own row for it', async (t) => {
|
|
296
|
+
const p = await page(t, { home: true })
|
|
297
|
+
await p.type('comfy')
|
|
298
|
+
await p.key({ key: 'ArrowDown' })
|
|
299
|
+
await p.key({ key: 'Enter' })
|
|
300
|
+
assert.deepEqual(p.clicked, ['comfy.git'])
|
|
301
|
+
assert.deepEqual(p.went, [])
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
test('home on a phone keeps its search box and does not take the cursor', async (t) => {
|
|
305
|
+
const p = await page(t, { home: true, media: [PHONE] })
|
|
306
|
+
assert.equal(p.document.documentElement.classList.contains('agent-bar-search-home'), false)
|
|
307
|
+
assert.notEqual(p.document.activeElement, p.input)
|
|
308
|
+
})
|
|
309
|
+
|
|
310
|
+
test('another page does not take the cursor', async (t) => {
|
|
311
|
+
const p = await page(t)
|
|
312
|
+
assert.notEqual(p.document.activeElement, p.input)
|
|
313
|
+
assert.equal(p.shape(), '')
|
|
314
|
+
})
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// The key that takes you to the agent bar (server/public/agent-bar-shortcut.js):
|
|
2
|
+
// which keys, where they are left alone, and what they do to the bar.
|
|
3
|
+
//
|
|
4
|
+
// The real agent-bar.js, agent-bar-app.js, agent-bar-compose.js,
|
|
5
|
+
// agent-bar-search.js and agent-bar-shortcut.js run here.
|
|
6
|
+
const assert = require('node:assert/strict')
|
|
7
|
+
const fs = require('node:fs')
|
|
8
|
+
const path = require('node:path')
|
|
9
|
+
const test = require('node:test')
|
|
10
|
+
const { JSDOM } = require('jsdom')
|
|
11
|
+
|
|
12
|
+
const publicDir = path.resolve(__dirname, '../server/public')
|
|
13
|
+
const script = (name) => fs.readFileSync(path.join(publicDir, name), 'utf8')
|
|
14
|
+
|
|
15
|
+
const TOP = '<header class="navheader"><h1><div class="flexible"></div></h1></header>'
|
|
16
|
+
const EXTRAS = '<input id="field"><div class="xterm"><textarea id="term"></textarea></div><div class="ace_editor"><textarea id="code"></textarea></div><button id="button">x</button>'
|
|
17
|
+
const OTHER = `<!doctype html><body>${TOP}<main id="page">${EXTRAS}</main></body>`
|
|
18
|
+
const HOME = `<!doctype html><body class="is-home">${TOP}<main id="page">`
|
|
19
|
+
+ `<form class="search home-search-form"><input type="search" class="flexible"></form>${EXTRAS}</main></body>`
|
|
20
|
+
const APP = '<!doctype html><body class="app-page"><header class="navheader"><h1>'
|
|
21
|
+
+ '<div class="app-header-identity"><span class="app-header-identity-title">Demo App</span></div>'
|
|
22
|
+
+ '<button id="ask-ai-tab" data-workspace-cwd="/home/api/demo"></button><div class="flexible"></div></h1></header>'
|
|
23
|
+
+ `<div class="appcanvas"><aside id="app-sidebar"><div class="menu-scroller"></div></aside><main id="page">${EXTRAS}</main>`
|
|
24
|
+
+ '<div class="mobile-bottom-nav"><div class="mobile-bottom-nav__identity"></div></div></div></body>'
|
|
25
|
+
|
|
26
|
+
async function settle(window) {
|
|
27
|
+
for (let i = 0; i < 6; i += 1) await new Promise((resolve) => window.setTimeout(resolve, 0))
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function page(t, options = {}) {
|
|
31
|
+
const dom = new JSDOM(options.app ? APP : options.home ? HOME : OTHER, {
|
|
32
|
+
url: `http://127.0.0.1:42000/${options.app ? 'p/demo' : options.home ? 'home' : 'plugins'}`,
|
|
33
|
+
runScripts: 'outside-only',
|
|
34
|
+
pretendToBeVisual: true,
|
|
35
|
+
beforeParse(window) {
|
|
36
|
+
Object.defineProperty(window.navigator, 'platform', { configurable: true, get: () => (options.mac === false ? 'Win32' : 'MacIntel') })
|
|
37
|
+
window.matchMedia = () => ({ matches: false, addEventListener() {}, removeEventListener() {} })
|
|
38
|
+
window.Element.prototype.getBoundingClientRect = function () {
|
|
39
|
+
return { left: 300, top: 8, right: 900, bottom: 608, width: 600, height: 600, x: 300, y: 8 }
|
|
40
|
+
}
|
|
41
|
+
// this page lays nothing out: everything counts as on screen
|
|
42
|
+
window.Element.prototype.getClientRects = function () { return [{}] }
|
|
43
|
+
window.fetch = async (url) => {
|
|
44
|
+
if (String(url) === '/info/shells') return { ok: true, json: async () => [] }
|
|
45
|
+
if (String(url) === '/info/apps') return { ok: true, json: async () => ({ apps: [] }) }
|
|
46
|
+
if (String(url).split('?')[0] === '/api/plugin/menu') {
|
|
47
|
+
return { ok: true, json: async () => ({ menu: [{ href: '/run/plugin/codex/pinokio.js', title: 'Codex', default: true }] }) }
|
|
48
|
+
}
|
|
49
|
+
return { ok: false, json: async () => ({ ok: false }) }
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
t.after(() => dom.window.close())
|
|
54
|
+
const { window } = dom
|
|
55
|
+
window.eval(script('agent-bar.js'))
|
|
56
|
+
if (options.app) window.eval(script('agent-bar-app.js'))
|
|
57
|
+
window.eval(script('agent-bar-compose.js'))
|
|
58
|
+
if (!options.app) window.eval(script('agent-bar-search.js'))
|
|
59
|
+
window.eval(script('agent-bar-shortcut.js'))
|
|
60
|
+
await window.PinokioAgentBar.ready
|
|
61
|
+
await settle(window)
|
|
62
|
+
const { document } = window
|
|
63
|
+
const root = document.querySelector('.agent-bar')
|
|
64
|
+
const input = root.querySelector('.agent-bar__input')
|
|
65
|
+
return {
|
|
66
|
+
window,
|
|
67
|
+
document,
|
|
68
|
+
root,
|
|
69
|
+
input,
|
|
70
|
+
shape: () => root.dataset.compose || '',
|
|
71
|
+
// a key pressed with the cursor in `target` (the page itself when none is given)
|
|
72
|
+
async key(init, target) {
|
|
73
|
+
const at = typeof target === 'string' ? document.getElementById(target) : (target || document.body)
|
|
74
|
+
if (at !== document.body) at.focus()
|
|
75
|
+
const event = new window.KeyboardEvent('keydown', { bubbles: true, cancelable: true, ...init })
|
|
76
|
+
at.dispatchEvent(event)
|
|
77
|
+
await settle(window)
|
|
78
|
+
return event
|
|
79
|
+
},
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const CMD_K = { key: 'k', metaKey: true }
|
|
84
|
+
const CTRL_K = { key: 'k', ctrlKey: true }
|
|
85
|
+
|
|
86
|
+
test('Cmd+K goes to the bar and opens its box', async (t) => {
|
|
87
|
+
const p = await page(t)
|
|
88
|
+
const event = await p.key(CMD_K)
|
|
89
|
+
assert.equal(p.document.activeElement, p.input)
|
|
90
|
+
assert.equal(p.shape(), 'place')
|
|
91
|
+
assert.equal(event.defaultPrevented, true)
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
test('it works from another field and from a button', async (t) => {
|
|
95
|
+
const p = await page(t)
|
|
96
|
+
await p.key(CMD_K, 'field')
|
|
97
|
+
assert.equal(p.document.activeElement, p.input)
|
|
98
|
+
await p.key({ key: 'Escape' }, p.input)
|
|
99
|
+
await p.key(CMD_K, 'button')
|
|
100
|
+
assert.equal(p.document.activeElement, p.input)
|
|
101
|
+
assert.equal(p.shape(), 'place')
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test('pressed again from inside the bar, it leaves the bar and keeps what was typed', async (t) => {
|
|
105
|
+
const p = await page(t)
|
|
106
|
+
await p.key(CMD_K)
|
|
107
|
+
p.input.value = 'a draft'
|
|
108
|
+
await p.key(CMD_K, p.input)
|
|
109
|
+
assert.equal(p.shape(), '')
|
|
110
|
+
assert.notEqual(p.document.activeElement, p.input)
|
|
111
|
+
assert.equal(p.input.value, 'a draft')
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
test('on macOS it is Cmd+K and Ctrl+K is left alone; elsewhere the other way round', async (t) => {
|
|
115
|
+
const mac = await page(t)
|
|
116
|
+
const ctrl = await mac.key(CTRL_K, 'field')
|
|
117
|
+
assert.equal(ctrl.defaultPrevented, false)
|
|
118
|
+
assert.equal(mac.shape(), '')
|
|
119
|
+
const pc = await page(t, { mac: false })
|
|
120
|
+
const cmd = await pc.key(CMD_K)
|
|
121
|
+
assert.equal(cmd.defaultPrevented, false)
|
|
122
|
+
assert.equal(pc.shape(), '')
|
|
123
|
+
await pc.key(CTRL_K)
|
|
124
|
+
assert.equal(pc.document.activeElement, pc.input)
|
|
125
|
+
assert.equal(pc.shape(), 'place')
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
test('with Shift or Alt it is another key', async (t) => {
|
|
129
|
+
const p = await page(t)
|
|
130
|
+
const event = await p.key({ key: 'K', metaKey: true, shiftKey: true })
|
|
131
|
+
assert.equal(event.defaultPrevented, false)
|
|
132
|
+
assert.equal(p.shape(), '')
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
test('a terminal and a code editor keep the key', async (t) => {
|
|
136
|
+
const p = await page(t, { mac: false })
|
|
137
|
+
for (const id of ['term', 'code']) {
|
|
138
|
+
const event = await p.key(CTRL_K, id)
|
|
139
|
+
assert.equal(event.defaultPrevented, false, id)
|
|
140
|
+
assert.equal(p.document.activeElement.id, id)
|
|
141
|
+
assert.equal(p.shape(), '')
|
|
142
|
+
}
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
test('a slash goes to the bar when no field has the cursor, and is not typed into it', async (t) => {
|
|
146
|
+
const p = await page(t)
|
|
147
|
+
const event = await p.key({ key: '/' }, 'button')
|
|
148
|
+
assert.equal(event.defaultPrevented, true)
|
|
149
|
+
assert.equal(p.document.activeElement, p.input)
|
|
150
|
+
assert.equal(p.shape(), 'place')
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
test('a slash in a field, a terminal or the bar itself is a slash', async (t) => {
|
|
154
|
+
const p = await page(t)
|
|
155
|
+
for (const id of ['field', 'term']) {
|
|
156
|
+
const event = await p.key({ key: '/' }, id)
|
|
157
|
+
assert.equal(event.defaultPrevented, false, id)
|
|
158
|
+
assert.equal(p.document.activeElement.id, id)
|
|
159
|
+
}
|
|
160
|
+
await p.key(CMD_K)
|
|
161
|
+
const event = await p.key({ key: '/' }, p.input)
|
|
162
|
+
assert.equal(event.defaultPrevented, false)
|
|
163
|
+
assert.equal(p.shape(), 'place')
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
test('home: the cursor is already in the bar, and the key opens its box', async (t) => {
|
|
167
|
+
const p = await page(t, { home: true })
|
|
168
|
+
assert.equal(p.document.activeElement, p.input)
|
|
169
|
+
assert.equal(p.shape(), '')
|
|
170
|
+
await p.key(CMD_K, p.input)
|
|
171
|
+
assert.equal(p.shape(), 'place')
|
|
172
|
+
assert.equal(p.document.activeElement, p.input)
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
test('an app\'s page: the key goes to that page\'s bar', async (t) => {
|
|
176
|
+
const p = await page(t, { app: true })
|
|
177
|
+
assert.equal(p.root.dataset.item, 'ask_app')
|
|
178
|
+
await p.key(CMD_K, 'field')
|
|
179
|
+
assert.equal(p.document.activeElement, p.input)
|
|
180
|
+
assert.equal(p.shape(), 'place')
|
|
181
|
+
await p.key(CMD_K, p.input)
|
|
182
|
+
assert.equal(p.shape(), '')
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
test('while a send is under way the key does nothing', async (t) => {
|
|
186
|
+
const p = await page(t)
|
|
187
|
+
p.input.disabled = true
|
|
188
|
+
const event = await p.key(CMD_K)
|
|
189
|
+
assert.equal(event.defaultPrevented, false)
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
test('the empty bar shows the key after its text, named for the platform', async (t) => {
|
|
193
|
+
const mac = await page(t)
|
|
194
|
+
const hint = mac.root.querySelector('.agent-bar-shortcut__hint')
|
|
195
|
+
assert.equal(hint.textContent, '⌘K')
|
|
196
|
+
assert.equal(hint.getAttribute('aria-hidden'), 'true')
|
|
197
|
+
assert.equal(hint.hidden, false)
|
|
198
|
+
assert.equal(hint.parentNode, mac.input.parentNode)
|
|
199
|
+
assert.ok(mac.input.compareDocumentPosition(hint) & mac.window.Node.DOCUMENT_POSITION_FOLLOWING)
|
|
200
|
+
const pc = await page(t, { mac: false })
|
|
201
|
+
assert.equal(pc.root.querySelector('.agent-bar-shortcut__hint').textContent, 'Ctrl K')
|
|
202
|
+
const app = await page(t, { app: true })
|
|
203
|
+
assert.equal(app.root.querySelectorAll('.agent-bar-shortcut__hint').length, 1)
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
test('the key is not shown where the bar is too narrow for it', async (t) => {
|
|
207
|
+
const p = await page(t)
|
|
208
|
+
const hint = p.root.querySelector('.agent-bar-shortcut__hint')
|
|
209
|
+
Object.defineProperty(p.input, 'clientWidth', { configurable: true, get: () => 180 })
|
|
210
|
+
p.window.dispatchEvent(new p.window.Event('resize'))
|
|
211
|
+
assert.equal(hint.hidden, true)
|
|
212
|
+
Object.defineProperty(p.input, 'clientWidth', { configurable: true, get: () => 400 })
|
|
213
|
+
p.window.dispatchEvent(new p.window.Event('resize'))
|
|
214
|
+
assert.equal(hint.hidden, false)
|
|
215
|
+
})
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// The Cursor and VS Code plugins hand the request to the app by the app's own
|
|
2
|
+
// way in: Cursor's prompt link, VS Code's `code chat` (or its link, when the
|
|
3
|
+
// command is nowhere on the machine).
|
|
4
|
+
const assert = require("node:assert/strict")
|
|
5
|
+
const fs = require("node:fs")
|
|
6
|
+
const os = require("node:os")
|
|
7
|
+
const path = require("node:path")
|
|
8
|
+
const test = require("node:test")
|
|
9
|
+
|
|
10
|
+
const cursor = require("../system/plugin/cursor/pinokio")
|
|
11
|
+
const vscode = require("../system/plugin/vscode/pinokio")
|
|
12
|
+
const URI = require("../kernel/api/uri")
|
|
13
|
+
const { execFileSync } = require("node:child_process")
|
|
14
|
+
const Shell = require("../kernel/shell")
|
|
15
|
+
|
|
16
|
+
const LINE = "My message is in .pinokio-sessions/s1/request.md. Read it and respond as if I had typed it here."
|
|
17
|
+
|
|
18
|
+
test("Cursor: the folder is opened, then the request goes in by Cursor's prompt link", () => {
|
|
19
|
+
const [open, link, missing] = cursor.run
|
|
20
|
+
assert.equal(open.method, "exec")
|
|
21
|
+
assert.equal(open.params.message, "cursor .")
|
|
22
|
+
assert.equal(open.params.path, "{{args.cwd}}")
|
|
23
|
+
|
|
24
|
+
assert.equal(link.method, "uri.open")
|
|
25
|
+
assert.equal(link.when, "{{which('cursor') && args.prompt}}", "only with a request, and only when Cursor is there")
|
|
26
|
+
assert.deepEqual(link.params, { uri: "cursor://anysphere.cursor-deeplink/prompt", params: { text: "{{args.prompt}}" } })
|
|
27
|
+
// the address Pinokio opens for it, with the text made safe for an address
|
|
28
|
+
const address = new URI().build({ uri: link.params.uri, params: { text: `${LINE} a&b "c" d+e` } })
|
|
29
|
+
const parsed = new URL(address)
|
|
30
|
+
assert.equal(`${parsed.protocol}//${parsed.host}${parsed.pathname}`, "cursor://anysphere.cursor-deeplink/prompt")
|
|
31
|
+
assert.equal(parsed.searchParams.get("text"), `${LINE} a&b "c" d+e`)
|
|
32
|
+
assert.ok(!address.includes(" "))
|
|
33
|
+
|
|
34
|
+
assert.equal(missing.method, "notify")
|
|
35
|
+
assert.equal(missing.when, "{{!which('cursor')}}")
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
const steps = (kernel, args) => vscode.run(kernel, {}, { args })
|
|
39
|
+
// a machine with `code` on the PATH, and one where it is nowhere
|
|
40
|
+
const onPath = (platform = "linux") => ({ platform, which: () => "/opt/bin/code" })
|
|
41
|
+
const nowhere = { platform: "linux", which: () => null }
|
|
42
|
+
const realExists = fs.existsSync
|
|
43
|
+
const withoutInstall = (t) => {
|
|
44
|
+
fs.existsSync = (file) => (/code(\.cmd)?$/.test(String(file)) ? false : realExists(file))
|
|
45
|
+
t.after(() => { fs.existsSync = realExists })
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
test("VS Code: `code` is found on the PATH, or where VS Code installs it", (t) => {
|
|
49
|
+
assert.equal(vscode.launch_type, "desktop", "its steps are built in code, so its kind is said outright")
|
|
50
|
+
assert.equal(steps(onPath(), { cwd: "/w" })[0].params.message._[0], "/opt/bin/code")
|
|
51
|
+
|
|
52
|
+
const inApp = "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"
|
|
53
|
+
fs.existsSync = (file) => file === inApp || (!/code(\.cmd)?$/.test(String(file)) && realExists(file))
|
|
54
|
+
t.after(() => { fs.existsSync = realExists })
|
|
55
|
+
const [open] = steps({ platform: "darwin", which: () => null }, { cwd: "/w" })
|
|
56
|
+
assert.equal(open.method, "shell.run")
|
|
57
|
+
assert.equal(open.params.message._[0], inApp)
|
|
58
|
+
// a `which` that throws is the same as not found
|
|
59
|
+
assert.equal(steps({ platform: "darwin", which: () => { throw new Error("no") } }, { cwd: "/w" })[0].params.message._[0], inApp)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
test("VS Code: without a request the folder is opened; with one, `code chat` gets it", () => {
|
|
63
|
+
const [plain, wait] = steps(onPath(), { cwd: "/w" })
|
|
64
|
+
assert.deepEqual(plain, { method: "shell.run", params: { conda: { skip: true }, message: { _: ["/opt/bin/code", "."] }, path: "/w" } })
|
|
65
|
+
assert.equal(wait.method, "process.wait")
|
|
66
|
+
|
|
67
|
+
const [chat] = steps(onPath(), { cwd: "/w", prompt: LINE })
|
|
68
|
+
assert.deepEqual(chat.params.message, { _: ["/opt/bin/code", "chat", LINE] })
|
|
69
|
+
assert.equal(Object.prototype.hasOwnProperty.call(chat.params, "input"), false, "it takes no typing: it is not a terminal tool")
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
test("VS Code: with `code` nowhere, VS Code's own links are opened instead", (t) => {
|
|
73
|
+
withoutInstall(t)
|
|
74
|
+
const [withRequest, wait] = steps(nowhere, { cwd: "/home/me/my app", prompt: LINE })
|
|
75
|
+
assert.equal(withRequest.method, "uri.open")
|
|
76
|
+
assert.deepEqual(withRequest.params, { uri: "vscode://agents/new", params: { prompt: LINE, workspace: "file:///home/me/my%20app" } })
|
|
77
|
+
const address = new URL(new URI().build(withRequest.params))
|
|
78
|
+
assert.equal(address.searchParams.get("prompt"), LINE)
|
|
79
|
+
assert.equal(address.searchParams.get("workspace"), "file:///home/me/my%20app")
|
|
80
|
+
assert.equal(wait.method, "process.wait")
|
|
81
|
+
|
|
82
|
+
const [folder] = steps(nowhere, { cwd: "/home/me/my app" })
|
|
83
|
+
assert.deepEqual(folder, { method: "uri.open", params: { uri: "vscode://file/home/me/my%20app" } })
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
// the command line Pinokio makes of the step's words, for a shell
|
|
87
|
+
const commandLine = (message, shell) => Shell.prototype.buildStructuredMessage.call(Object.assign(Object.create(Shell.prototype), { shell }), message, shell)
|
|
88
|
+
|
|
89
|
+
test("VS Code: the request reaches `code chat` whole, and nothing in it is run", { skip: process.platform === "win32" }, () => {
|
|
90
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "pinokio-code-chat-"))
|
|
91
|
+
try {
|
|
92
|
+
// a stand-in `code`, in a folder with a space in its name, that writes down what it was given
|
|
93
|
+
const bin = path.join(dir, "my bin")
|
|
94
|
+
fs.mkdirSync(bin)
|
|
95
|
+
const out = path.join(dir, "out.txt")
|
|
96
|
+
fs.writeFileSync(path.join(bin, "code"), `#!/bin/sh\nfor arg in "$@"; do printf "%s\\n--\\n" "$arg" >> "${out}"; done\n`, { mode: 0o755 })
|
|
97
|
+
const request = `${LINE} "quoted" 'single' $(touch ${path.join(dir, "ran")}) \`touch ${path.join(dir, "ran2")}\` $HOME ; echo hi`
|
|
98
|
+
const [chat] = steps({ platform: process.platform, which: () => path.join(bin, "code") }, { cwd: dir, prompt: request })
|
|
99
|
+
execFileSync("bash", ["-c", commandLine(chat.params.message, "bash")], { cwd: dir })
|
|
100
|
+
assert.equal(fs.readFileSync(out, "utf8"), `chat\n--\n${request}\n--\n`)
|
|
101
|
+
assert.equal(fs.existsSync(path.join(dir, "ran")), false)
|
|
102
|
+
assert.equal(fs.existsSync(path.join(dir, "ran2")), false)
|
|
103
|
+
} finally {
|
|
104
|
+
fs.rmSync(dir, { recursive: true, force: true })
|
|
105
|
+
}
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
test("VS Code: on Windows the request's quotes, percent signs and ampersands are escaped for cmd", () => {
|
|
109
|
+
const [chat] = steps({ platform: "win32", which: () => "C:\\Program Files\\Microsoft VS Code\\bin\\code.cmd" }, { cwd: "C:\\w", prompt: 'say "hi" & calc %PATH%' })
|
|
110
|
+
const line = commandLine(chat.params.message, "cmd.exe")
|
|
111
|
+
assert.ok(line.includes('say ^"hi^" ^& calc ^%PATH^%'), line)
|
|
112
|
+
})
|