-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathplugin.ts
More file actions
77 lines (74 loc) · 2.67 KB
/
Copy pathplugin.ts
File metadata and controls
77 lines (74 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import type { PluginContext, PluginResult } from './plugin/types.ts'
import { initManager, manager } from './plugin/pty/manager.ts'
import { initPermissions } from './plugin/pty/permissions.ts'
import { ptySpawn } from './plugin/pty/tools/spawn.ts'
import { ptyWrite } from './plugin/pty/tools/write.ts'
import { ptyRead } from './plugin/pty/tools/read.ts'
import { ptyList } from './plugin/pty/tools/list.ts'
import { ptyKill } from './plugin/pty/tools/kill.ts'
import { ptySnapshot } from './plugin/pty/tools/snapshot.ts'
import { ptySnapshotWait } from './plugin/pty/tools/snapshot-wait.ts'
import { PTYServer } from './web/server/server.ts'
import open from 'open'
const ptyOpenClientCommand = 'pty-open-background-spy'
const ptyShowServerUrlCommand = 'pty-show-server-url'
export const PTYPlugin = async ({ client, directory }: PluginContext): Promise<PluginResult> => {
initPermissions(client, directory)
initManager(client)
let ptyServer: PTYServer | undefined
return {
'command.execute.before': async (input) => {
if (input.command !== ptyOpenClientCommand && input.command !== ptyShowServerUrlCommand) {
return
}
if (ptyServer === undefined) {
ptyServer = await PTYServer.createServer()
}
if (input.command === ptyOpenClientCommand) {
open(ptyServer.server.url.origin)
} else if (input.command === ptyShowServerUrlCommand) {
const message = `PTY Sessions Web Interface URL: ${ptyServer.server.url.origin}`
await client.session.prompt({
path: { id: input.sessionID },
body: {
noReply: true,
parts: [
{
type: 'text',
text: message,
},
],
},
})
}
throw new Error('Command handled by PTY plugin')
},
tool: {
pty_spawn: ptySpawn,
pty_write: ptyWrite,
pty_read: ptyRead,
pty_snapshot: ptySnapshot,
pty_snapshot_wait: ptySnapshotWait,
pty_list: ptyList,
pty_kill: ptyKill,
},
config: async (input) => {
if (!input.command) {
input.command = {}
}
input.command[ptyOpenClientCommand] = {
template: `This command will start the PTY Sessions Web Interface in your default browser.`,
description: 'Open PTY Sessions Web Interface',
}
input.command[ptyShowServerUrlCommand] = {
template: `This command will show the PTY Sessions Web Interface URL.`,
description: 'Show PTY Sessions Web Interface URL',
}
},
event: async ({ event }) => {
if (event.type === 'session.deleted') {
manager.cleanupBySession(event.properties.info.id)
}
},
}
}