opencode-apex 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 +155 -0
- package/advisor.ts +290 -0
- package/browser/actions.ts +811 -0
- package/browser/cdp.ts +603 -0
- package/browser/endpoint.ts +243 -0
- package/browser.ts +556 -0
- package/core/advisor-agent.ts +93 -0
- package/core/advisor-session.ts +292 -0
- package/core/advisor-tool.ts +76 -0
- package/core/bus.ts +192 -0
- package/core/conditions.ts +66 -0
- package/core/config.ts +533 -0
- package/core/crypto.ts +193 -0
- package/core/guard.ts +248 -0
- package/core/ingest-listener.ts +193 -0
- package/core/patterns.ts +385 -0
- package/core/redact.ts +256 -0
- package/core/rpc.ts +117 -0
- package/core/templates.ts +100 -0
- package/docs/ADVISOR.md +91 -0
- package/docs/BROWSER.md +226 -0
- package/docs/CONFIG.md +97 -0
- package/docs/EVENTS.md +102 -0
- package/docs/GUARD.md +165 -0
- package/docs/REDACT.md +121 -0
- package/docs/TROUBLESHOOTING.md +203 -0
- package/events.ts +934 -0
- package/guard.ts +250 -0
- package/index.ts +147 -0
- package/opencode-apex.jsonc +249 -0
- package/package.json +34 -0
- package/redact.ts +156 -0
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolución del endpoint CDP.
|
|
3
|
+
*
|
|
4
|
+
* Desde Chrome 144 hay dos rutas incompatibles y no se pueden sondear igual:
|
|
5
|
+
*
|
|
6
|
+
* A) --remote-debugging-port (flag)
|
|
7
|
+
* → GET /json/version devuelve el webSocketDebuggerUrl
|
|
8
|
+
* → INERTE sobre el user-data-dir por defecto desde Chrome 136
|
|
9
|
+
*
|
|
10
|
+
* B) chrome://inspect/#remote-debugging (toggle)
|
|
11
|
+
* → /json/version responde 404 POR DISEÑO
|
|
12
|
+
* → el WS sale del fichero DevToolsActivePort
|
|
13
|
+
* → exige un popup de consentimiento POR CONEXIÓN
|
|
14
|
+
*
|
|
15
|
+
* Por eso el orden es: DevToolsActivePort primero, discovery HTTP como
|
|
16
|
+
* respaldo. Un 404 en /json/version no es un error, es la ruta B.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { readFile } from "node:fs/promises"
|
|
20
|
+
import { homedir } from "node:os"
|
|
21
|
+
import { join } from "node:path"
|
|
22
|
+
|
|
23
|
+
/** Perfiles donde Chrome deja DevToolsActivePort. */
|
|
24
|
+
export const CANDIDATE_PROFILES = [
|
|
25
|
+
"google-chrome",
|
|
26
|
+
"google-chrome-beta",
|
|
27
|
+
"google-chrome-unstable",
|
|
28
|
+
"google-chrome-dev",
|
|
29
|
+
"chromium",
|
|
30
|
+
"chromium-browser",
|
|
31
|
+
"microsoft-edge",
|
|
32
|
+
"brave",
|
|
33
|
+
"browser-os",
|
|
34
|
+
"ai.opencode.desktop",
|
|
35
|
+
].map((name) => join(homedir(), ".config", name))
|
|
36
|
+
|
|
37
|
+
export interface ActivePort {
|
|
38
|
+
readonly port: number
|
|
39
|
+
readonly path: string
|
|
40
|
+
readonly source: string
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface EndpointCandidate {
|
|
44
|
+
readonly webSocketDebuggerUrl: string
|
|
45
|
+
readonly profileDir?: string
|
|
46
|
+
readonly how: string
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface Endpoint {
|
|
50
|
+
readonly candidates: readonly EndpointCandidate[]
|
|
51
|
+
readonly how: string
|
|
52
|
+
readonly profileDir?: string
|
|
53
|
+
readonly secure: boolean
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Lee DevToolsActivePort de un perfil. El fichero tiene dos líneas: puerto y
|
|
58
|
+
* ruta del websocket.
|
|
59
|
+
*/
|
|
60
|
+
export async function readActivePort(profileDir: string): Promise<ActivePort | null> {
|
|
61
|
+
// El perfil pone el fichero en la raíz; algunos builds lo dejan en Default/.
|
|
62
|
+
for (const candidate of [profileDir, join(profileDir, "Default")]) {
|
|
63
|
+
try {
|
|
64
|
+
const raw = await readFile(join(candidate, "DevToolsActivePort"), "utf8")
|
|
65
|
+
const [port, path] = raw.trim().split("\n")
|
|
66
|
+
if (!port || !path?.startsWith("/devtools/browser/")) continue
|
|
67
|
+
return { port: Number(port), path: path.trim(), source: join(candidate, "DevToolsActivePort") }
|
|
68
|
+
} catch {
|
|
69
|
+
// siguiente candidato
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return null
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Todos los perfiles con un DevToolsActivePort que declara `port`.
|
|
77
|
+
*
|
|
78
|
+
* OJO: varios perfiles pueden declarar el MISMO puerto. Medido en esta máquina:
|
|
79
|
+
* google-chrome, chromium y browser-os declaran los tres 9222, pero solo un
|
|
80
|
+
* proceso escucha de verdad (los ficheros de los otros dos están "stale": Chrome
|
|
81
|
+
* los dejó al cerrarse). Por esto devuelve una LISTA y quien conecta prueba en
|
|
82
|
+
* orden — elegir el primero a ciegas puede acabar en un perfil muerto o, peor,
|
|
83
|
+
* en el navegador que el usuario no quiere.
|
|
84
|
+
*/
|
|
85
|
+
export async function profilesForPort(
|
|
86
|
+
port: number,
|
|
87
|
+
candidates: readonly string[] = CANDIDATE_PROFILES,
|
|
88
|
+
): Promise<Array<ActivePort & { profileDir: string }>> {
|
|
89
|
+
const found: Array<ActivePort & { profileDir: string }> = []
|
|
90
|
+
for (const dir of candidates) {
|
|
91
|
+
const active = await readActivePort(dir)
|
|
92
|
+
if (active && active.port === port) found.push({ profileDir: dir, ...active })
|
|
93
|
+
}
|
|
94
|
+
return found
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Todos los perfiles con debugging declarado (pueden incluir stale). */
|
|
98
|
+
export async function listActive(
|
|
99
|
+
candidates: readonly string[] = CANDIDATE_PROFILES,
|
|
100
|
+
): Promise<Array<ActivePort & { profileDir: string }>> {
|
|
101
|
+
const found: Array<ActivePort & { profileDir: string }> = []
|
|
102
|
+
for (const dir of candidates) {
|
|
103
|
+
const active = await readActivePort(dir)
|
|
104
|
+
if (active) found.push({ profileDir: dir, ...active })
|
|
105
|
+
}
|
|
106
|
+
return found
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export interface ResolveInput {
|
|
110
|
+
readonly cdpUrl?: string
|
|
111
|
+
readonly profileDir?: string
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Resuelve el webSocketDebuggerUrl.
|
|
116
|
+
*
|
|
117
|
+
* Modos, en orden de precedencia:
|
|
118
|
+
* 1. ws:// o wss:// explícito → navegador remoto. Se usa tal cual: no hay
|
|
119
|
+
* DevToolsActivePort ni /json/version que consultar a distancia, así que el
|
|
120
|
+
* cliente tiene que poder hablar el websocket directamente (típicamente a
|
|
121
|
+
* través de un túnel/proxy inverso).
|
|
122
|
+
* 2. perfil explícito → DevToolsActivePort local.
|
|
123
|
+
* 3. "auto" → DevToolsActivePort local, 9222 preferido.
|
|
124
|
+
* 4. http://host:puerto → derivar perfil local, luego /json/version.
|
|
125
|
+
*/
|
|
126
|
+
export async function resolveEndpoint({ cdpUrl = "auto", profileDir }: ResolveInput = {}): Promise<Endpoint> {
|
|
127
|
+
// 0. Endpoint remoto explícito: ws:// o wss://. Sin discovery posible.
|
|
128
|
+
if (/^wss?:\/\//i.test(cdpUrl)) {
|
|
129
|
+
let parsed: URL
|
|
130
|
+
try {
|
|
131
|
+
parsed = new URL(cdpUrl)
|
|
132
|
+
} catch {
|
|
133
|
+
throw new Error(`browser.cdpUrl no es un websocket válido: ${cdpUrl}`)
|
|
134
|
+
}
|
|
135
|
+
return one({
|
|
136
|
+
webSocketDebuggerUrl: parsed.toString(),
|
|
137
|
+
how: parsed.protocol === "wss:" ? "remote-wss" : "remote-ws",
|
|
138
|
+
secure: parsed.protocol === "wss:",
|
|
139
|
+
})
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// 1. Perfil explícito: lee su DevToolsActivePort y ya está.
|
|
143
|
+
if (profileDir) {
|
|
144
|
+
const active = await readActivePort(profileDir)
|
|
145
|
+
if (!active) {
|
|
146
|
+
throw new Error(
|
|
147
|
+
`No hay DevToolsActivePort en ${profileDir}. Activa el debugging remoto en ` +
|
|
148
|
+
`chrome://inspect/#remote-debugging de ese perfil.`,
|
|
149
|
+
)
|
|
150
|
+
}
|
|
151
|
+
return one({
|
|
152
|
+
webSocketDebuggerUrl: `ws://127.0.0.1:${active.port}${active.path}`,
|
|
153
|
+
profileDir,
|
|
154
|
+
how: "devtools-active-port",
|
|
155
|
+
})
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// 2. Modo auto: preferimos 9222, si no el primer candidato vivo.
|
|
159
|
+
if (cdpUrl === "auto" || cdpUrl === "") {
|
|
160
|
+
const actives = await listActive()
|
|
161
|
+
if (actives.length === 0) {
|
|
162
|
+
throw new Error(
|
|
163
|
+
"Ningún perfil tiene DevToolsActivePort. Activa el debugging remoto en " +
|
|
164
|
+
"chrome://inspect/#remote-debugging del Chrome que quieras usar.",
|
|
165
|
+
)
|
|
166
|
+
}
|
|
167
|
+
const ordered = [...actives.filter((a) => a.port === 9222), ...actives.filter((a) => a.port !== 9222)]
|
|
168
|
+
const first = ordered[0]
|
|
169
|
+
if (!first) throw new Error("Ningún perfil tiene DevToolsActivePort.")
|
|
170
|
+
return one({
|
|
171
|
+
candidates: ordered.map((a) => ({
|
|
172
|
+
webSocketDebuggerUrl: `ws://127.0.0.1:${a.port}${a.path}`,
|
|
173
|
+
profileDir: a.profileDir,
|
|
174
|
+
how: "devtools-active-port",
|
|
175
|
+
})),
|
|
176
|
+
how: "devtools-active-port",
|
|
177
|
+
profileDir: first.profileDir,
|
|
178
|
+
})
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// 3. Puerto explícito → derivamos el perfil, o caemos al discovery HTTP.
|
|
182
|
+
const port = extractPort(cdpUrl)
|
|
183
|
+
const host = extractHost(cdpUrl)
|
|
184
|
+
|
|
185
|
+
if (port !== undefined) {
|
|
186
|
+
const matches = await profilesForPort(port)
|
|
187
|
+
const first = matches[0]
|
|
188
|
+
if (first) {
|
|
189
|
+
return one({
|
|
190
|
+
candidates: matches.map((m) => ({
|
|
191
|
+
webSocketDebuggerUrl: `ws://${host}:${m.port}${m.path}`,
|
|
192
|
+
profileDir: m.profileDir,
|
|
193
|
+
how: "port-to-profile",
|
|
194
|
+
})),
|
|
195
|
+
how: "port-to-profile",
|
|
196
|
+
profileDir: first.profileDir,
|
|
197
|
+
})
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// 4. Ruta del flag clásico: /json/version.
|
|
202
|
+
const base = `http://${host}:${port ?? 9222}`
|
|
203
|
+
const version = (await fetch(`${base}/json/version`)
|
|
204
|
+
.then((r) => (r.ok ? (r.json() as Promise<{ webSocketDebuggerUrl?: string }>) : null))
|
|
205
|
+
.catch(() => null)) as { webSocketDebuggerUrl?: string } | null
|
|
206
|
+
if (!version?.webSocketDebuggerUrl) {
|
|
207
|
+
throw new Error(
|
|
208
|
+
`No hay CDP en ${base}. Si el navegador usa el toggle de ` +
|
|
209
|
+
`chrome://inspect/#remote-debugging, /json/version da 404 por diseño: ` +
|
|
210
|
+
`pasa browser.profile o deja browser.cdpUrl en "auto".`,
|
|
211
|
+
)
|
|
212
|
+
}
|
|
213
|
+
return one({
|
|
214
|
+
webSocketDebuggerUrl: version.webSocketDebuggerUrl,
|
|
215
|
+
how: "http-discovery",
|
|
216
|
+
secure: false,
|
|
217
|
+
})
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Lo mínimo para describir un endpoint; `one` completa los campos ausentes. */
|
|
221
|
+
type EndpointSeed = EndpointCandidate & { secure?: boolean }
|
|
222
|
+
|
|
223
|
+
const one = (seed: EndpointSeed | { candidates: readonly EndpointCandidate[]; how: string; profileDir?: string }): Endpoint => {
|
|
224
|
+
const fallback: EndpointCandidate = "webSocketDebuggerUrl" in seed ? seed : (seed.candidates[0] as EndpointCandidate)
|
|
225
|
+
return {
|
|
226
|
+
how: "how" in seed ? seed.how : fallback.how,
|
|
227
|
+
profileDir: "profileDir" in seed ? seed.profileDir : fallback.profileDir,
|
|
228
|
+
secure: "secure" in seed ? (seed.secure ?? false) : fallback.webSocketDebuggerUrl.startsWith("wss://"),
|
|
229
|
+
candidates: "candidates" in seed && seed.candidates.length > 0 ? seed.candidates : [fallback],
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const extractPort = (value: string) => {
|
|
234
|
+
const match = /:(\d{2,5})\b/.exec(value)
|
|
235
|
+
if (match) return Number(match[1])
|
|
236
|
+
if (/^\d{2,5}$/.test(value.trim())) return Number(value.trim())
|
|
237
|
+
return undefined
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const extractHost = (value: string) => {
|
|
241
|
+
const match = /^[a-z]+:\/\/([^/:]+)/.exec(value)
|
|
242
|
+
return match && match[1] ? match[1] : "127.0.0.1"
|
|
243
|
+
}
|