@yuridulin/pointer 0.0.0-stage → 0.1.0
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/README.md +54 -2
- package/package.json +51 -4
- package/server/acp/catalog.ts +111 -0
- package/server/acp/connection.ts +233 -0
- package/server/acp/fake-agent.ts +408 -0
- package/server/acp/protocol.ts +167 -0
- package/server/acp/run.ts +139 -0
- package/server/acp/session.ts +1249 -0
- package/server/attachments.ts +115 -0
- package/server/changes.ts +239 -0
- package/server/cli.ts +362 -0
- package/server/compact.ts +361 -0
- package/server/config.ts +176 -0
- package/server/db.ts +462 -0
- package/server/dev.ts +8 -0
- package/server/diff.ts +190 -0
- package/server/engines.ts +63 -0
- package/server/files.ts +254 -0
- package/server/git.ts +319 -0
- package/server/history.ts +425 -0
- package/server/http/attachments.ts +63 -0
- package/server/http/browse.ts +108 -0
- package/server/http/chats.ts +627 -0
- package/server/http/closing.ts +14 -0
- package/server/http/files.ts +115 -0
- package/server/http/git.ts +84 -0
- package/server/http/preview.ts +21 -0
- package/server/http/providers.ts +200 -0
- package/server/http/reply.ts +38 -0
- package/server/hub.ts +521 -0
- package/server/install.ts +169 -0
- package/server/jobs.ts +664 -0
- package/server/linkPreview.ts +172 -0
- package/server/loop.ts +328 -0
- package/server/main.ts +353 -0
- package/server/probe.ts +34 -0
- package/server/process.ts +333 -0
- package/server/prompt.ts +196 -0
- package/server/providers/fake.ts +100 -0
- package/server/providers/opencode.ts +468 -0
- package/server/providers/registry.ts +149 -0
- package/server/providers/store.ts +210 -0
- package/server/segments.ts +248 -0
- package/server/sse.ts +93 -0
- package/server/tools/bash.ts +222 -0
- package/server/tools/edit.ts +254 -0
- package/server/tools/glob.ts +63 -0
- package/server/tools/grep.ts +122 -0
- package/server/tools/ignore.ts +54 -0
- package/server/tools/index.ts +129 -0
- package/server/tools/jail.ts +85 -0
- package/server/tools/jobs.ts +293 -0
- package/server/tools/read.ts +248 -0
- package/server/tools/write.ts +70 -0
- package/server/tray.ps1 +191 -0
- package/server/turn.ts +663 -0
- package/server/watch.ts +94 -0
- package/shared/acpConfig.ts +113 -0
- package/shared/links.ts +122 -0
- package/shared/types.ts +392 -0
- package/web/dist/app.css +1 -0
- package/web/dist/assets/codicon-mxdk0fnd.ttf +0 -0
- package/web/dist/index.html +28 -0
- package/web/dist/main.css +1 -0
- package/web/dist/main.js +1366 -0
- package/web/dist/tokens.css +1 -0
- package/web/dist/workers/css.worker.js +97 -0
- package/web/dist/workers/editor.worker.js +30 -0
- package/web/dist/workers/html.worker.js +300 -0
- package/web/dist/workers/json.worker.js +62 -0
- package/web/dist/workers/ts.worker.js +67733 -0
package/README.md
CHANGED
|
@@ -1,3 +1,55 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Pointer
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Local-first coding agent in one Bun daemon: chats, live streaming, diffs with rollback,
|
|
4
|
+
Monaco editor and Git, all in a single interface. Runs on Windows as a logon task with a
|
|
5
|
+
tray icon and an Edge app window.
|
|
6
|
+
|
|
7
|
+
## Requirements
|
|
8
|
+
|
|
9
|
+
- Bun 1.4.4 or newer
|
|
10
|
+
- Git for Windows (without it the `bash` tool is off)
|
|
11
|
+
- Microsoft Edge (the tray opens the window in app mode)
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
bun add -g @yuridulin/pointer
|
|
17
|
+
pointer install
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
`pointer install` writes `%USERPROFILE%\.pointer` (database, config, token), registers the
|
|
21
|
+
logon task, the tray and the Explorer context menu item, then starts the daemon. API keys
|
|
22
|
+
are only read from the environment.
|
|
23
|
+
|
|
24
|
+
## Update
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
pointer update
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
The command stops the task, replaces the package outside it, re-registers the task and
|
|
31
|
+
starts it again. A run that was in flight is interrupted.
|
|
32
|
+
|
|
33
|
+
## Commands
|
|
34
|
+
|
|
35
|
+
| Command | Action |
|
|
36
|
+
| --- | --- |
|
|
37
|
+
| `pointer install` | Write state, task, tray and Explorer item |
|
|
38
|
+
| `pointer uninstall` | Remove the task and Explorer item, keep the database |
|
|
39
|
+
| `pointer start` / `stop` / `restart` | Control the scheduled task |
|
|
40
|
+
| `pointer status` | Daemon state, clients, running turns |
|
|
41
|
+
| `pointer open [folder]` | Add the folder as a project and show the window |
|
|
42
|
+
| `pointer update` | Replace the package and restart |
|
|
43
|
+
|
|
44
|
+
## Development
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
bun install
|
|
48
|
+
bun run dev
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
`bun run dev` uses `%USERPROFILE%\.pointer-dev` and port 8788, so a checkout never writes
|
|
52
|
+
the daily database. The installed task, tray and Explorer item do not touch it.
|
|
53
|
+
|
|
54
|
+
See [docs/SPEC.md](docs/SPEC.md), [docs/DEPLOY.md](docs/DEPLOY.md) and
|
|
55
|
+
[docs/ROADMAP.md](docs/ROADMAP.md).
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yuridulin/pointer",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Local-first coding agent in one Bun daemon: chats, diffs, Monaco, Git",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"agent",
|
|
7
|
+
"coding",
|
|
8
|
+
"harness",
|
|
9
|
+
"bun",
|
|
10
|
+
"monaco"
|
|
11
|
+
],
|
|
12
|
+
"type": "module",
|
|
13
|
+
"engines": {
|
|
14
|
+
"bun": ">=1.4.4"
|
|
15
|
+
},
|
|
16
|
+
"bin": {
|
|
17
|
+
"pointer": "server/cli.ts"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"server",
|
|
21
|
+
"shared",
|
|
22
|
+
"web/dist",
|
|
23
|
+
"!web/dist/**/*.map",
|
|
24
|
+
"!server/**/*.test.ts",
|
|
25
|
+
"!**/*.test.ts"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "bun web/build.ts",
|
|
29
|
+
"prepack": "bun web/build.ts --no-map",
|
|
30
|
+
"start": "bun server/main.ts",
|
|
31
|
+
"dev": "bun server/dev.ts",
|
|
32
|
+
"typecheck": "tsc --noEmit && tsc -p web/tsconfig.json --noEmit",
|
|
33
|
+
"test": "bun test"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/bun": "^1.4.4",
|
|
37
|
+
"@types/react": "^19.3.0",
|
|
38
|
+
"@types/react-dom": "^19.3.0",
|
|
39
|
+
"monaco-editor": "^0.57.0",
|
|
40
|
+
"react": "^19.3.1",
|
|
41
|
+
"react-dom": "^19.3.1",
|
|
42
|
+
"react-markdown": "^10.1.0",
|
|
43
|
+
"remark-gfm": "^4.0.1",
|
|
44
|
+
"typescript": "^7.0.2"
|
|
45
|
+
},
|
|
46
|
+
"repository": {
|
|
47
|
+
"type": "git",
|
|
48
|
+
"url": "git+https://github.com/yuridulin/pointer.git"
|
|
49
|
+
},
|
|
50
|
+
"publishConfig": {
|
|
51
|
+
"access": "public"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { Database } from 'bun:sqlite'
|
|
2
|
+
import { parseAcpConfigOptions, type AcpConfigOption } from '../../shared/acpConfig'
|
|
3
|
+
import { findGitBash, type Config } from '../config'
|
|
4
|
+
import { listWorkspaces } from '../db'
|
|
5
|
+
import { acpAgent, resolveAcpCommand } from '../engines'
|
|
6
|
+
import { acpOverride, listProviders, providerEnabled } from '../providers/store'
|
|
7
|
+
import { AcpConnection } from './connection'
|
|
8
|
+
import { PROTOCOL_VERSION, type AcpInitializeResult } from './protocol'
|
|
9
|
+
|
|
10
|
+
export type AcpCatalogView = {
|
|
11
|
+
options: AcpConfigOption[]
|
|
12
|
+
loading: boolean
|
|
13
|
+
error: string | null
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type Entry = AcpCatalogView & { pending: Promise<void> | null }
|
|
17
|
+
|
|
18
|
+
const entries = new Map<string, Entry>()
|
|
19
|
+
let probeConnection: AcpConnection | null = null
|
|
20
|
+
|
|
21
|
+
export function acpCatalogView(providerId: string): AcpCatalogView {
|
|
22
|
+
const entry = entries.get(providerId)
|
|
23
|
+
if (!entry) return { options: [], loading: false, error: null }
|
|
24
|
+
return { options: entry.options, loading: entry.loading, error: entry.error }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Starts a background read of each enabled ACP provider. Does not block startup. */
|
|
28
|
+
export function warmAcpCatalog(db: Database, config: Config): void {
|
|
29
|
+
for (const row of listProviders(db)) {
|
|
30
|
+
if (row.kind !== 'acp' || !providerEnabled(row)) continue
|
|
31
|
+
void ensureAcpCatalog(db, config, row.id)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function ensureAcpCatalog(db: Database, config: Config, providerId: string): AcpCatalogView {
|
|
36
|
+
const spec = acpAgent(providerId)
|
|
37
|
+
if (!spec) return { options: [], loading: false, error: 'Неизвестный ACP-провайдер' }
|
|
38
|
+
const current = entries.get(providerId)
|
|
39
|
+
if (current?.pending || (current && current.options.length > 0 && !current.loading)) {
|
|
40
|
+
return acpCatalogView(providerId)
|
|
41
|
+
}
|
|
42
|
+
const entry: Entry = { options: current?.options ?? [], loading: true, error: null, pending: null }
|
|
43
|
+
const pending = probe(db, config, providerId)
|
|
44
|
+
.then((options) => {
|
|
45
|
+
entry.options = options
|
|
46
|
+
entry.loading = false
|
|
47
|
+
entry.error = null
|
|
48
|
+
})
|
|
49
|
+
.catch((error: unknown) => {
|
|
50
|
+
entry.loading = false
|
|
51
|
+
entry.error = error instanceof Error ? error.message : String(error)
|
|
52
|
+
})
|
|
53
|
+
.finally(() => {
|
|
54
|
+
entry.pending = null
|
|
55
|
+
})
|
|
56
|
+
entry.pending = pending
|
|
57
|
+
entries.set(providerId, entry)
|
|
58
|
+
return acpCatalogView(providerId)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function stopAcpCatalog(): void {
|
|
62
|
+
probeConnection?.kill()
|
|
63
|
+
probeConnection = null
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function probe(db: Database, config: Config, providerId: string): Promise<AcpConfigOption[]> {
|
|
67
|
+
const spec = acpAgent(providerId)
|
|
68
|
+
if (!spec) throw new Error('Неизвестный ACP-провайдер')
|
|
69
|
+
const resolved = resolveAcpCommand(config, spec, acpOverride(db))
|
|
70
|
+
if (!resolved) {
|
|
71
|
+
throw new Error(`ACP-агент не найден: ${config.acpCommand}. Проверь acpCommand в config.json`)
|
|
72
|
+
}
|
|
73
|
+
const cwd = acpOverride(db).cwd || listWorkspaces(db)[0]?.root_path || process.cwd()
|
|
74
|
+
const connection = new AcpConnection({
|
|
75
|
+
command: resolved.command,
|
|
76
|
+
args: resolved.args,
|
|
77
|
+
cwd,
|
|
78
|
+
marker: crypto.randomUUID(),
|
|
79
|
+
shell: findGitBash(config) ?? '',
|
|
80
|
+
onRequest: (method) => {
|
|
81
|
+
if (method === 'session/request_permission') return { outcome: { outcome: 'cancelled' } }
|
|
82
|
+
return {}
|
|
83
|
+
},
|
|
84
|
+
onNotification: () => {},
|
|
85
|
+
})
|
|
86
|
+
probeConnection = connection
|
|
87
|
+
try {
|
|
88
|
+
const initialized = (await connection.request('initialize', {
|
|
89
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
90
|
+
clientCapabilities: {
|
|
91
|
+
fs: { readTextFile: false, writeTextFile: false },
|
|
92
|
+
terminal: false,
|
|
93
|
+
_meta: { parameterizedModelPicker: true },
|
|
94
|
+
},
|
|
95
|
+
clientInfo: { name: 'pointer', version: '0.1.0' },
|
|
96
|
+
_meta: { parameterizedModelPicker: true },
|
|
97
|
+
})) as AcpInitializeResult
|
|
98
|
+
if (initialized.protocolVersion !== PROTOCOL_VERSION) {
|
|
99
|
+
throw new Error(
|
|
100
|
+
`ACP-агент объявил протокол ${initialized.protocolVersion}; ожидается ${PROTOCOL_VERSION}`,
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
const created = await connection.request('session/new', { cwd, mcpServers: [] })
|
|
104
|
+
const options = parseAcpConfigOptions(created)
|
|
105
|
+
if (options.length === 0) throw new Error('Агент не сообщил модели')
|
|
106
|
+
return options
|
|
107
|
+
} finally {
|
|
108
|
+
connection.kill()
|
|
109
|
+
if (probeConnection === connection) probeConnection = null
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import type { FileSink } from 'bun'
|
|
2
|
+
import { AcpError, type JsonRpcId } from './protocol'
|
|
3
|
+
import { killTree, PROCESS_MARKER_ENV } from '../process'
|
|
4
|
+
|
|
5
|
+
type Pending = {
|
|
6
|
+
resolve: (value: unknown) => void
|
|
7
|
+
reject: (error: Error) => void
|
|
8
|
+
timer: ReturnType<typeof setTimeout> | undefined
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type AcpConnectionOptions = {
|
|
12
|
+
command: string
|
|
13
|
+
args: string[]
|
|
14
|
+
cwd: string
|
|
15
|
+
marker: string
|
|
16
|
+
shell?: string
|
|
17
|
+
onRequest: (method: string, params: unknown) => unknown | Promise<unknown>
|
|
18
|
+
onNotification: (method: string, params: unknown) => void
|
|
19
|
+
onExit?: (code: number | null, stderrTail: string) => void
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const REQUEST_TIMEOUT_MS = 30_000
|
|
23
|
+
const STDERR_TAIL_LIMIT = 8 * 1024
|
|
24
|
+
|
|
25
|
+
/** JSON-RPC 2.0 over an ACP agent's stdin/stdout. One instance owns one child process. */
|
|
26
|
+
export class AcpConnection {
|
|
27
|
+
private readonly proc: ReturnType<typeof Bun.spawn>
|
|
28
|
+
private readonly shell: string
|
|
29
|
+
private readonly marker: string
|
|
30
|
+
private readonly onExit?: (code: number | null, stderrTail: string) => void
|
|
31
|
+
private readonly onRequest: AcpConnectionOptions['onRequest']
|
|
32
|
+
private readonly onNotification: AcpConnectionOptions['onNotification']
|
|
33
|
+
private readonly pending = new Map<JsonRpcId, Pending>()
|
|
34
|
+
private nextId = 1
|
|
35
|
+
private tail = ''
|
|
36
|
+
private exited = false
|
|
37
|
+
private killed = false
|
|
38
|
+
|
|
39
|
+
constructor(options: AcpConnectionOptions) {
|
|
40
|
+
this.shell = options.shell ?? ''
|
|
41
|
+
this.marker = options.marker
|
|
42
|
+
this.onExit = options.onExit
|
|
43
|
+
this.onRequest = options.onRequest
|
|
44
|
+
this.onNotification = options.onNotification
|
|
45
|
+
this.proc = spawn(options)
|
|
46
|
+
void this.readStdout(this.proc.stdout as ReadableStream<Uint8Array>)
|
|
47
|
+
void this.readStderr(this.proc.stderr as ReadableStream<Uint8Array>)
|
|
48
|
+
void this.watchExit()
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
get alive(): boolean {
|
|
52
|
+
return !this.exited && !this.killed
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
get stderrTail(): string {
|
|
56
|
+
return this.tail
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
request(method: string, params?: unknown, timeoutMs = REQUEST_TIMEOUT_MS): Promise<unknown> {
|
|
60
|
+
if (!this.alive) return Promise.reject(new Error('ACP-агент не запущен'))
|
|
61
|
+
const id = this.nextId++
|
|
62
|
+
return new Promise((resolve, reject) => {
|
|
63
|
+
const timer =
|
|
64
|
+
timeoutMs > 0
|
|
65
|
+
? setTimeout(() => {
|
|
66
|
+
this.pending.delete(id)
|
|
67
|
+
reject(new Error(`ACP-агент не ответил на ${method}`))
|
|
68
|
+
this.kill()
|
|
69
|
+
}, timeoutMs)
|
|
70
|
+
: undefined
|
|
71
|
+
this.pending.set(id, { resolve, reject, timer })
|
|
72
|
+
this.write({ jsonrpc: '2.0', id, method, params })
|
|
73
|
+
})
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
notify(method: string, params?: unknown): void {
|
|
77
|
+
if (!this.alive) return
|
|
78
|
+
this.write({ jsonrpc: '2.0', method, params })
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
respond(id: JsonRpcId, result: unknown): void {
|
|
82
|
+
if (!this.alive) return
|
|
83
|
+
this.write({ jsonrpc: '2.0', id, result: result ?? null })
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
respondError(id: JsonRpcId, code: number, message: string): void {
|
|
87
|
+
if (!this.alive) return
|
|
88
|
+
this.write({ jsonrpc: '2.0', id, error: { code, message } })
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
kill(): void {
|
|
92
|
+
if (this.killed) return
|
|
93
|
+
this.killed = true
|
|
94
|
+
try {
|
|
95
|
+
this.proc.kill()
|
|
96
|
+
} catch {}
|
|
97
|
+
void killTree(this.proc.pid, this.shell, this.marker).finally(() => {
|
|
98
|
+
try {
|
|
99
|
+
this.proc.kill()
|
|
100
|
+
} catch {}
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private write(message: unknown): void {
|
|
105
|
+
const stdin = this.proc.stdin as FileSink
|
|
106
|
+
try {
|
|
107
|
+
stdin.write(`${JSON.stringify(message)}\n`)
|
|
108
|
+
void Promise.resolve(stdin.flush()).catch(() => this.kill())
|
|
109
|
+
} catch {
|
|
110
|
+
this.kill()
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private async readStdout(stream: ReadableStream<Uint8Array>): Promise<void> {
|
|
115
|
+
const reader = stream.getReader()
|
|
116
|
+
const decoder = new TextDecoder()
|
|
117
|
+
let buffer = ''
|
|
118
|
+
try {
|
|
119
|
+
for (;;) {
|
|
120
|
+
const { done, value } = await reader.read()
|
|
121
|
+
if (done) break
|
|
122
|
+
buffer += decoder.decode(value, { stream: true })
|
|
123
|
+
for (;;) {
|
|
124
|
+
const newline = buffer.indexOf('\n')
|
|
125
|
+
if (newline < 0) break
|
|
126
|
+
const line = buffer.slice(0, newline).replace(/\r$/, '')
|
|
127
|
+
buffer = buffer.slice(newline + 1)
|
|
128
|
+
if (line.trim()) this.handleLine(line)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
buffer += decoder.decode()
|
|
132
|
+
if (buffer.trim()) this.handleLine(buffer.replace(/\r$/, ''))
|
|
133
|
+
} catch {
|
|
134
|
+
// The process exiting closes the stream; watchExit reports it.
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private async readStderr(stream: ReadableStream<Uint8Array>): Promise<void> {
|
|
139
|
+
const reader = stream.getReader()
|
|
140
|
+
const decoder = new TextDecoder()
|
|
141
|
+
try {
|
|
142
|
+
for (;;) {
|
|
143
|
+
const { done, value } = await reader.read()
|
|
144
|
+
if (done) break
|
|
145
|
+
this.tail = (this.tail + decoder.decode(value, { stream: true })).slice(-STDERR_TAIL_LIMIT)
|
|
146
|
+
}
|
|
147
|
+
this.tail = (this.tail + decoder.decode()).slice(-STDERR_TAIL_LIMIT)
|
|
148
|
+
} catch {
|
|
149
|
+
// Same as stdout.
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
private handleLine(line: string): void {
|
|
154
|
+
let parsed: unknown
|
|
155
|
+
try {
|
|
156
|
+
parsed = JSON.parse(line)
|
|
157
|
+
} catch {
|
|
158
|
+
return
|
|
159
|
+
}
|
|
160
|
+
if (typeof parsed !== 'object' || parsed === null) return
|
|
161
|
+
const message = parsed as Record<string, unknown>
|
|
162
|
+
const method = message.method
|
|
163
|
+
if (typeof method === 'string') {
|
|
164
|
+
const id = message.id
|
|
165
|
+
if (typeof id === 'string' || typeof id === 'number') {
|
|
166
|
+
void this.handleRequest(id, method, message.params)
|
|
167
|
+
} else {
|
|
168
|
+
try {
|
|
169
|
+
this.onNotification(method, message.params)
|
|
170
|
+
} catch {}
|
|
171
|
+
}
|
|
172
|
+
return
|
|
173
|
+
}
|
|
174
|
+
const id = message.id
|
|
175
|
+
if (typeof id !== 'string' && typeof id !== 'number') return
|
|
176
|
+
const pending = this.pending.get(id)
|
|
177
|
+
if (!pending) return
|
|
178
|
+
this.pending.delete(id)
|
|
179
|
+
clearTimeout(pending.timer)
|
|
180
|
+
const error = message.error
|
|
181
|
+
if (typeof error === 'object' && error !== null) {
|
|
182
|
+
const payload = error as Record<string, unknown>
|
|
183
|
+
const data = typeof payload.data === 'object' && payload.data !== null
|
|
184
|
+
? payload.data as Record<string, unknown>
|
|
185
|
+
: null
|
|
186
|
+
const text =
|
|
187
|
+
(data && typeof data.message === 'string' && data.message) ||
|
|
188
|
+
(typeof payload.message === 'string' ? payload.message : 'ACP error')
|
|
189
|
+
const code = typeof payload.code === 'number' ? payload.code : -32000
|
|
190
|
+
pending.reject(new AcpError(text, code, payload.data))
|
|
191
|
+
} else {
|
|
192
|
+
pending.resolve(message.result)
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
private async handleRequest(id: JsonRpcId, method: string, params: unknown): Promise<void> {
|
|
197
|
+
try {
|
|
198
|
+
const result = await this.onRequest(method, params)
|
|
199
|
+
this.respond(id, result)
|
|
200
|
+
} catch (error) {
|
|
201
|
+
if (error instanceof AcpError) this.respondError(id, error.code, error.message)
|
|
202
|
+
else this.respondError(id, -32000, error instanceof Error ? error.message : String(error))
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
private async watchExit(): Promise<void> {
|
|
207
|
+
let code: number | null = null
|
|
208
|
+
try {
|
|
209
|
+
code = await this.proc.exited
|
|
210
|
+
} catch {}
|
|
211
|
+
this.exited = true
|
|
212
|
+
const tail = this.tail
|
|
213
|
+
for (const [id, pending] of this.pending) {
|
|
214
|
+
clearTimeout(pending.timer)
|
|
215
|
+
pending.reject(new AcpError('ACP-агент завершился', -32000))
|
|
216
|
+
this.pending.delete(id)
|
|
217
|
+
}
|
|
218
|
+
try {
|
|
219
|
+
this.onExit?.(code, tail)
|
|
220
|
+
} catch {}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function spawn(options: AcpConnectionOptions): ReturnType<typeof Bun.spawn> {
|
|
225
|
+
return Bun.spawn([options.command, ...options.args], {
|
|
226
|
+
cwd: options.cwd,
|
|
227
|
+
stdin: 'pipe',
|
|
228
|
+
stdout: 'pipe',
|
|
229
|
+
stderr: 'pipe',
|
|
230
|
+
env: { ...process.env, [PROCESS_MARKER_ENV]: options.marker },
|
|
231
|
+
...(process.platform === 'win32' ? {} : { detached: true }),
|
|
232
|
+
})
|
|
233
|
+
}
|