@chroxy/server 0.10.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/CONFIG.md +695 -0
- package/README.md +105 -0
- package/hooks/permission-hook.sh +285 -0
- package/package.json +69 -0
- package/scripts/fix-node-pty-helper.js +44 -0
- package/sidecar/Dockerfile +40 -0
- package/sidecar/PROTOCOL.md +691 -0
- package/sidecar/agent.js +1290 -0
- package/sidecar/package-lock.json +43 -0
- package/sidecar/package.json +17 -0
- package/src/activity-registry.js +491 -0
- package/src/anthropic-compatible-config.js +534 -0
- package/src/anthropic-compatible-session.js +579 -0
- package/src/anthropic-openai-shim.js +109 -0
- package/src/anthropic-openai-translate.js +377 -0
- package/src/auth-probes.js +296 -0
- package/src/background-shell-tracker.js +320 -0
- package/src/background-shells.js +125 -0
- package/src/base-session.js +1518 -0
- package/src/billing-budget.js +191 -0
- package/src/billing-canary-monitor.js +203 -0
- package/src/billing-class.js +149 -0
- package/src/bind-host.js +82 -0
- package/src/built-in-tools/bash-exec.js +167 -0
- package/src/built-in-tools/file-ops.js +198 -0
- package/src/built-in-tools/tool-transforms.js +179 -0
- package/src/builtin-commands.js +107 -0
- package/src/byok-compose-state-shared.js +31 -0
- package/src/byok-compose-state.js +234 -0
- package/src/byok-credentials.js +127 -0
- package/src/byok-event-translator.js +136 -0
- package/src/byok-mcp-client.js +404 -0
- package/src/byok-mcp-config.js +159 -0
- package/src/byok-mcp-fleet.js +214 -0
- package/src/byok-mcp-trust.js +175 -0
- package/src/byok-session.js +2019 -0
- package/src/byok-subagent-profiles.js +101 -0
- package/src/byok-tool-executor.js +813 -0
- package/src/byok-tools.js +271 -0
- package/src/channels/PACKAGING.md +181 -0
- package/src/channels/README.md +99 -0
- package/src/channels/chroxy-channel-server.js +248 -0
- package/src/checkpoint-manager.js +515 -0
- package/src/child-stream-guard.js +38 -0
- package/src/claude-channel-session.js +185 -0
- package/src/claude-model-catalog.js +266 -0
- package/src/claude-stream-parser.js +106 -0
- package/src/claude-tui/form-driver.js +874 -0
- package/src/claude-tui/pty-driver.js +520 -0
- package/src/claude-tui/tested-cli-version.js +22 -0
- package/src/claude-tui-attachments.js +342 -0
- package/src/claude-tui-session.js +4029 -0
- package/src/cli/config-cmd.js +34 -0
- package/src/cli/credentials-cmd.js +59 -0
- package/src/cli/deploy-cmd.js +167 -0
- package/src/cli/doctor-cmd.js +52 -0
- package/src/cli/identity-cmd.js +82 -0
- package/src/cli/init-cmd.js +209 -0
- package/src/cli/pages-cmd.js +167 -0
- package/src/cli/pair-code-cmd.js +108 -0
- package/src/cli/pair-discord-cmd.js +100 -0
- package/src/cli/providers-cmd.js +168 -0
- package/src/cli/server-cmd.js +94 -0
- package/src/cli/service-cmd.js +237 -0
- package/src/cli/session-cmd.js +143 -0
- package/src/cli/shared.js +194 -0
- package/src/cli/shell-cmd.js +130 -0
- package/src/cli/status-cmd.js +204 -0
- package/src/cli/tunnel-cmd.js +101 -0
- package/src/cli/update-cmd.js +45 -0
- package/src/cli/worktree-gc-cmd.js +195 -0
- package/src/cli-session.js +1820 -0
- package/src/cli.js +60 -0
- package/src/codex-app-server-client.js +156 -0
- package/src/codex-app-server-session.js +572 -0
- package/src/codex-session.js +604 -0
- package/src/config.js +1757 -0
- package/src/connection-info.js +42 -0
- package/src/connection-locality.js +55 -0
- package/src/content-blocks.js +42 -0
- package/src/control-room/byok-pool.js +130 -0
- package/src/control-room/constants.js +26 -0
- package/src/control-room/containers.js +258 -0
- package/src/control-room/emulators.js +279 -0
- package/src/control-room/handler-factory.js +142 -0
- package/src/control-room/host-prune.js +387 -0
- package/src/control-room/integrations.js +929 -0
- package/src/control-room/repo-runtime-config.js +239 -0
- package/src/control-room/repo-set.js +162 -0
- package/src/control-room/runners.js +479 -0
- package/src/control-room/simulators.js +223 -0
- package/src/control-room/skills-inventory.js +303 -0
- package/src/control-room/survey.js +685 -0
- package/src/control-room/wsl.js +182 -0
- package/src/conversation-scanner.js +270 -0
- package/src/conversation-scope.js +52 -0
- package/src/conversation-search.js +216 -0
- package/src/cost-budget-manager.js +190 -0
- package/src/credential-cipher.js +179 -0
- package/src/credential-store.js +786 -0
- package/src/credential-test.js +145 -0
- package/src/credentials-file.js +58 -0
- package/src/dashboard-next/dist/assets/index-C2ipeGlq.js +111 -0
- package/src/dashboard-next/dist/assets/index-DM-_dISG.css +1 -0
- package/src/dashboard-next/dist/index.html +13 -0
- package/src/deepseek-credentials.js +58 -0
- package/src/deepseek-session.js +192 -0
- package/src/detect-thinking-keyword.js +67 -0
- package/src/dev-preview.js +227 -0
- package/src/devcontainer-config.js +269 -0
- package/src/device-preferences.js +267 -0
- package/src/diagnostics.js +187 -0
- package/src/diff-parser.js +157 -0
- package/src/discord-credentials.js +188 -0
- package/src/discord-pair-delivery.js +113 -0
- package/src/docker-byok-pool-stats.js +221 -0
- package/src/docker-byok-pool.js +841 -0
- package/src/docker-byok-session.js +2172 -0
- package/src/docker-image-allowlist.js +99 -0
- package/src/docker-sdk-session.js +396 -0
- package/src/docker-session.js +382 -0
- package/src/doctor-billing.js +179 -0
- package/src/doctor.js +667 -0
- package/src/duration.js +77 -0
- package/src/environment-manager.js +753 -0
- package/src/environment-statuses.js +20 -0
- package/src/environments/backends/docker.js +931 -0
- package/src/environments/backends/k8s.js +3030 -0
- package/src/environments/backends/rancher.js +326 -0
- package/src/environments/backends/types.js +332 -0
- package/src/event-ingest.js +546 -0
- package/src/event-normalizer.js +1083 -0
- package/src/external-session-registry.js +0 -0
- package/src/file-store.js +52 -0
- package/src/gemini-session.js +498 -0
- package/src/get-public-ip.js +52 -0
- package/src/git.js +15 -0
- package/src/github-webhook.js +256 -0
- package/src/handler-utils.js +663 -0
- package/src/handlers/checkpoint-handlers.js +202 -0
- package/src/handlers/control-room-handlers.js +1945 -0
- package/src/handlers/conversation-handlers.js +201 -0
- package/src/handlers/credential-handlers.js +247 -0
- package/src/handlers/evaluator-handlers.js +94 -0
- package/src/handlers/feature-handlers.js +332 -0
- package/src/handlers/file-handlers.js +142 -0
- package/src/handlers/ide-handlers.js +238 -0
- package/src/handlers/input-handlers.js +1048 -0
- package/src/handlers/pairing-handlers.js +94 -0
- package/src/handlers/repo-handlers.js +295 -0
- package/src/handlers/session-handlers.js +660 -0
- package/src/handlers/settings-handlers.js +818 -0
- package/src/handlers/skills-handlers.js +682 -0
- package/src/handlers/summarize-handlers.js +172 -0
- package/src/handlers/token-handlers.js +57 -0
- package/src/http-oversize.js +51 -0
- package/src/http-routes.js +1158 -0
- package/src/ide/search.js +268 -0
- package/src/ide/symbols.js +423 -0
- package/src/ip-utils.js +85 -0
- package/src/json-state-file.js +169 -0
- package/src/jsonl-reader.js +187 -0
- package/src/jsonl-subprocess-session.js +441 -0
- package/src/keychain.js +458 -0
- package/src/lan-ip.js +38 -0
- package/src/logger.js +388 -0
- package/src/mailbox-route.js +252 -0
- package/src/mcp-tools.js +46 -0
- package/src/message-transform.js +102 -0
- package/src/metrics.js +67 -0
- package/src/model-discovery.js +288 -0
- package/src/models.js +1426 -0
- package/src/notification-prefs.js +544 -0
- package/src/notifications/discord-billing-sink.js +323 -0
- package/src/notifications/discord-webhook-client.js +187 -0
- package/src/notifications/discord-webhook-sink.js +768 -0
- package/src/notifications/expo-push-sink.js +414 -0
- package/src/notifications/ready-body.js +177 -0
- package/src/notifications/sink-registry.js +98 -0
- package/src/notifications/sink.js +82 -0
- package/src/ollama-session.js +221 -0
- package/src/ollama-tags.js +238 -0
- package/src/openai-compatible-session.js +118 -0
- package/src/pages-store.js +255 -0
- package/src/pairing.js +836 -0
- package/src/path-hash-trust-ledger.js +311 -0
- package/src/per-session-settings.js +366 -0
- package/src/permission-audit.js +120 -0
- package/src/permission-hook.js +257 -0
- package/src/permission-manager.js +829 -0
- package/src/permission-resolver.js +144 -0
- package/src/platform.js +139 -0
- package/src/prompt-evaluator.js +386 -0
- package/src/providers.js +436 -0
- package/src/push.js +565 -0
- package/src/rate-limiter.js +433 -0
- package/src/redaction.js +206 -0
- package/src/sdk-session.js +1773 -0
- package/src/semver.js +92 -0
- package/src/server-cli/push-notification-handler.js +240 -0
- package/src/server-cli/server-orchestrator.js +168 -0
- package/src/server-cli/startup-display.js +146 -0
- package/src/server-cli/tunnel-lifecycle-handler.js +219 -0
- package/src/server-cli-child.js +207 -0
- package/src/server-cli.js +1223 -0
- package/src/server-identity.js +383 -0
- package/src/service.js +835 -0
- package/src/session-context.js +49 -0
- package/src/session-lock.js +60 -0
- package/src/session-manager.js +3257 -0
- package/src/session-message-history.js +511 -0
- package/src/session-preset-trust.js +78 -0
- package/src/session-preset.js +458 -0
- package/src/session-state-persistence.js +307 -0
- package/src/session-timeout-manager.js +166 -0
- package/src/session-token-store.js +83 -0
- package/src/shell-approval-info.js +45 -0
- package/src/shell-approval-store.js +163 -0
- package/src/shell-audit.js +63 -0
- package/src/skills-budget.js +181 -0
- package/src/skills-content-validator.js +106 -0
- package/src/skills-frontmatter.js +422 -0
- package/src/skills-loader.js +1893 -0
- package/src/skills-manager.js +256 -0
- package/src/skills-trust.js +532 -0
- package/src/skills-usage.js +335 -0
- package/src/snapshots-store.js +345 -0
- package/src/ssrf-guard.js +130 -0
- package/src/subagent-counter.js +149 -0
- package/src/summarize-session.js +268 -0
- package/src/supervisor.js +929 -0
- package/src/test-client.js +131 -0
- package/src/token-compare.js +28 -0
- package/src/token-manager.js +220 -0
- package/src/tool-result.js +87 -0
- package/src/transcript-tasks.js +334 -0
- package/src/tunnel/base.js +294 -0
- package/src/tunnel/cloudflare.js +281 -0
- package/src/tunnel/index.js +54 -0
- package/src/tunnel-check.js +84 -0
- package/src/turn-tracker.js +0 -0
- package/src/user-shell-registry.js +218 -0
- package/src/user-shell-session.js +411 -0
- package/src/utils/check-dependencies.js +70 -0
- package/src/utils/concurrency.js +24 -0
- package/src/utils/context-window-learn.js +148 -0
- package/src/utils/error-message.js +25 -0
- package/src/utils/format-bytes.js +35 -0
- package/src/utils/preflight.js +143 -0
- package/src/utils/resolve-binary.js +91 -0
- package/src/utils/respawn-rate-limiter.js +59 -0
- package/src/utils/sleep.js +58 -0
- package/src/utils/spawn-env.js +193 -0
- package/src/utils/validation-patterns.js +23 -0
- package/src/utils/win-spawn.js +98 -0
- package/src/version-check.js +16 -0
- package/src/web-task-manager.js +383 -0
- package/src/worktree-gc.js +418 -0
- package/src/worktree-reaper.js +168 -0
- package/src/ws-auth.js +645 -0
- package/src/ws-broadcaster.js +385 -0
- package/src/ws-client-manager.js +476 -0
- package/src/ws-client-sender.js +115 -0
- package/src/ws-file-ops/browser.js +594 -0
- package/src/ws-file-ops/common.js +163 -0
- package/src/ws-file-ops/git.js +298 -0
- package/src/ws-file-ops/index.js +52 -0
- package/src/ws-file-ops/reader.js +595 -0
- package/src/ws-forwarding.js +543 -0
- package/src/ws-handler-context.js +196 -0
- package/src/ws-history.js +1251 -0
- package/src/ws-message-handlers.js +165 -0
- package/src/ws-permissions.js +676 -0
- package/src/ws-schemas.js +119 -0
- package/src/ws-server.js +2866 -0
|
@@ -0,0 +1,594 @@
|
|
|
1
|
+
import { readdir, readFile, stat, realpath } from 'fs/promises'
|
|
2
|
+
import { homedir } from 'os'
|
|
3
|
+
import { join, resolve, normalize, relative } from 'path'
|
|
4
|
+
import { createLogger } from '../logger.js'
|
|
5
|
+
import { getBuiltinCommands } from '../builtin-commands.js'
|
|
6
|
+
import { getProvider } from '../providers.js'
|
|
7
|
+
|
|
8
|
+
const log = createLogger('ws')
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Directory browsing, file listing, slash commands, and agent listing.
|
|
12
|
+
*
|
|
13
|
+
* @param {Function} sendFn - (ws, message) => void
|
|
14
|
+
* @param {Function} resolveSessionCwd - shared CWD resolver
|
|
15
|
+
* @param {Function} validatePathWithinCwd - shared path validator
|
|
16
|
+
* @returns {Object} browser operation methods
|
|
17
|
+
*/
|
|
18
|
+
export function createBrowserOps(sendFn, resolveSessionCwd, validatePathWithinCwd) {
|
|
19
|
+
|
|
20
|
+
/** List directories at a given path, sending a directory_listing response */
|
|
21
|
+
async function listDirectory(ws, requestedPath) {
|
|
22
|
+
let absPath = null
|
|
23
|
+
try {
|
|
24
|
+
const home = homedir()
|
|
25
|
+
if (!requestedPath || typeof requestedPath !== 'string' || !requestedPath.trim()) {
|
|
26
|
+
absPath = home
|
|
27
|
+
} else {
|
|
28
|
+
const trimmed = requestedPath.trim()
|
|
29
|
+
absPath = trimmed.startsWith('~')
|
|
30
|
+
? resolve(home, trimmed.slice(1).replace(/^\//, ''))
|
|
31
|
+
: resolve(trimmed)
|
|
32
|
+
}
|
|
33
|
+
absPath = normalize(absPath)
|
|
34
|
+
|
|
35
|
+
let realAbsPath
|
|
36
|
+
try {
|
|
37
|
+
realAbsPath = await realpath(absPath)
|
|
38
|
+
} catch (err) {
|
|
39
|
+
if (err.code === 'ENOENT') {
|
|
40
|
+
realAbsPath = absPath
|
|
41
|
+
} else {
|
|
42
|
+
throw err
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const homeReal = await realpath(home)
|
|
46
|
+
|
|
47
|
+
if (!realAbsPath.startsWith(homeReal + '/') && realAbsPath !== homeReal) {
|
|
48
|
+
sendFn(ws, {
|
|
49
|
+
type: 'directory_listing',
|
|
50
|
+
path: absPath,
|
|
51
|
+
parentPath: null,
|
|
52
|
+
entries: [],
|
|
53
|
+
error: 'Access denied: directory listing is restricted to the home directory',
|
|
54
|
+
})
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const dirents = await readdir(realAbsPath, { withFileTypes: true })
|
|
59
|
+
const entries = dirents
|
|
60
|
+
.filter(d => d.isDirectory() && !d.name.startsWith('.'))
|
|
61
|
+
.sort((a, b) => a.name.localeCompare(b.name))
|
|
62
|
+
.map(d => ({ name: d.name, isDirectory: true }))
|
|
63
|
+
|
|
64
|
+
const parentPath = absPath === '/' ? null : resolve(absPath, '..')
|
|
65
|
+
|
|
66
|
+
sendFn(ws, {
|
|
67
|
+
type: 'directory_listing',
|
|
68
|
+
path: absPath,
|
|
69
|
+
parentPath,
|
|
70
|
+
entries,
|
|
71
|
+
error: null,
|
|
72
|
+
})
|
|
73
|
+
} catch (err) {
|
|
74
|
+
let errorMessage
|
|
75
|
+
if (err.code === 'ENOENT') errorMessage = 'Directory not found'
|
|
76
|
+
else if (err.code === 'EACCES') errorMessage = 'Permission denied'
|
|
77
|
+
else if (err.code === 'ENOTDIR') errorMessage = 'Not a directory'
|
|
78
|
+
else errorMessage = err.message || 'Unknown error'
|
|
79
|
+
|
|
80
|
+
sendFn(ws, {
|
|
81
|
+
type: 'directory_listing',
|
|
82
|
+
path: absPath || requestedPath || null,
|
|
83
|
+
parentPath: null,
|
|
84
|
+
entries: [],
|
|
85
|
+
error: errorMessage,
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Browse files and directories at a given path within the session CWD */
|
|
91
|
+
async function browseFiles(ws, requestedPath, sessionCwd) {
|
|
92
|
+
if (!sessionCwd) {
|
|
93
|
+
sendFn(ws, {
|
|
94
|
+
type: 'file_listing',
|
|
95
|
+
path: null,
|
|
96
|
+
parentPath: null,
|
|
97
|
+
entries: [],
|
|
98
|
+
error: 'File browsing is not available in this mode',
|
|
99
|
+
})
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
let absPath = null
|
|
104
|
+
try {
|
|
105
|
+
if (!requestedPath || typeof requestedPath !== 'string' || !requestedPath.trim()) {
|
|
106
|
+
absPath = resolve(sessionCwd)
|
|
107
|
+
} else {
|
|
108
|
+
absPath = resolve(sessionCwd, requestedPath.trim())
|
|
109
|
+
}
|
|
110
|
+
absPath = normalize(absPath)
|
|
111
|
+
|
|
112
|
+
const { valid, realPath: realAbsPath, cwdReal } = await validatePathWithinCwd(absPath, sessionCwd)
|
|
113
|
+
if (!valid) {
|
|
114
|
+
sendFn(ws, {
|
|
115
|
+
type: 'file_listing',
|
|
116
|
+
path: absPath,
|
|
117
|
+
parentPath: null,
|
|
118
|
+
entries: [],
|
|
119
|
+
error: 'Access denied: browsing is restricted to the project directory',
|
|
120
|
+
})
|
|
121
|
+
return
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const dirents = await readdir(realAbsPath, { withFileTypes: true })
|
|
125
|
+
const entries = []
|
|
126
|
+
for (const d of dirents) {
|
|
127
|
+
if (d.name.startsWith('.')) continue
|
|
128
|
+
if (d.name === 'node_modules') continue
|
|
129
|
+
const entry = { name: d.name, isDirectory: d.isDirectory(), size: null }
|
|
130
|
+
if (!d.isDirectory()) {
|
|
131
|
+
try {
|
|
132
|
+
const s = await stat(join(realAbsPath, d.name))
|
|
133
|
+
entry.size = s.size
|
|
134
|
+
} catch (_) { /* skip size on error */ }
|
|
135
|
+
}
|
|
136
|
+
entries.push(entry)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
entries.sort((a, b) => {
|
|
140
|
+
if (a.isDirectory !== b.isDirectory) return a.isDirectory ? -1 : 1
|
|
141
|
+
return a.name.localeCompare(b.name)
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
const parentPath = realAbsPath === cwdReal ? null : resolve(realAbsPath, '..')
|
|
145
|
+
|
|
146
|
+
sendFn(ws, {
|
|
147
|
+
type: 'file_listing',
|
|
148
|
+
path: realAbsPath,
|
|
149
|
+
parentPath,
|
|
150
|
+
entries,
|
|
151
|
+
error: null,
|
|
152
|
+
})
|
|
153
|
+
} catch (err) {
|
|
154
|
+
let errorMessage
|
|
155
|
+
if (err.code === 'ENOENT') errorMessage = 'Directory not found'
|
|
156
|
+
else if (err.code === 'EACCES') errorMessage = 'Permission denied'
|
|
157
|
+
else if (err.code === 'ENOTDIR') errorMessage = 'Not a directory'
|
|
158
|
+
else errorMessage = err.message || 'Unknown error'
|
|
159
|
+
|
|
160
|
+
sendFn(ws, {
|
|
161
|
+
type: 'file_listing',
|
|
162
|
+
path: absPath || requestedPath || null,
|
|
163
|
+
parentPath: null,
|
|
164
|
+
entries: [],
|
|
165
|
+
error: errorMessage,
|
|
166
|
+
})
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Parse a .gitignore file into an array of { pattern, negated } rules.
|
|
172
|
+
* Supports basic gitignore patterns: globs, directory markers, negation.
|
|
173
|
+
*/
|
|
174
|
+
function parseGitignore(content) {
|
|
175
|
+
const rules = []
|
|
176
|
+
for (const raw of content.split('\n')) {
|
|
177
|
+
let line = raw.trim()
|
|
178
|
+
if (!line || line.startsWith('#')) continue
|
|
179
|
+
const negated = line.startsWith('!')
|
|
180
|
+
if (negated) line = line.slice(1)
|
|
181
|
+
rules.push({ pattern: line, negated })
|
|
182
|
+
}
|
|
183
|
+
return rules
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Test whether a relative file path matches a gitignore rule pattern.
|
|
188
|
+
* Handles: exact name, directory suffix (/), leading slash, glob (*).
|
|
189
|
+
*/
|
|
190
|
+
function matchesGitignorePattern(relPath, pattern, isDir) {
|
|
191
|
+
const segments = relPath.split('/')
|
|
192
|
+
let pat = pattern
|
|
193
|
+
|
|
194
|
+
// Directory-only pattern (trailing /) — only matches directories
|
|
195
|
+
const dirOnly = pat.endsWith('/')
|
|
196
|
+
if (dirOnly) {
|
|
197
|
+
if (!isDir) return false
|
|
198
|
+
pat = pat.slice(0, -1)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Rooted pattern (leading /) — must match from root
|
|
202
|
+
const rooted = pat.startsWith('/')
|
|
203
|
+
if (rooted) pat = pat.slice(1)
|
|
204
|
+
|
|
205
|
+
// Convert glob pattern to regex
|
|
206
|
+
const regexStr = pat
|
|
207
|
+
.replace(/[.+^${}()|[\]\\]/g, '\\$&')
|
|
208
|
+
.replace(/\*\*/g, '\u26a1GLOBSTAR\u26a1')
|
|
209
|
+
.replace(/\*/g, '[^/]*')
|
|
210
|
+
.replace(/\u26a1GLOBSTAR\u26a1/g, '.*')
|
|
211
|
+
.replace(/\?/g, '[^/]')
|
|
212
|
+
|
|
213
|
+
const regex = new RegExp(`^${regexStr}$`)
|
|
214
|
+
|
|
215
|
+
if (rooted) {
|
|
216
|
+
return regex.test(relPath)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Patterns with slashes match against path suffixes
|
|
220
|
+
if (pat.includes('/')) {
|
|
221
|
+
for (let i = 0; i < segments.length; i++) {
|
|
222
|
+
const subpath = segments.slice(i).join('/')
|
|
223
|
+
if (regex.test(subpath)) return true
|
|
224
|
+
}
|
|
225
|
+
return false
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Unrooted patterns without slashes match any segment or the full path
|
|
229
|
+
if (regex.test(relPath)) return true
|
|
230
|
+
for (const seg of segments) {
|
|
231
|
+
if (regex.test(seg)) return true
|
|
232
|
+
}
|
|
233
|
+
return false
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Check if a relative path is ignored by gitignore rules.
|
|
238
|
+
*/
|
|
239
|
+
function isIgnored(relPath, rules, isDir) {
|
|
240
|
+
let ignored = false
|
|
241
|
+
for (const rule of rules) {
|
|
242
|
+
if (matchesGitignorePattern(relPath, rule.pattern, isDir)) {
|
|
243
|
+
ignored = !rule.negated
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return ignored
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* List files recursively from session CWD with gitignore filtering.
|
|
251
|
+
* Max depth defaults to 3. Optional query for substring filtering.
|
|
252
|
+
*/
|
|
253
|
+
async function listFiles(ws, sessionCwd, query, sessionId) {
|
|
254
|
+
if (!sessionCwd) {
|
|
255
|
+
const response = { type: 'file_list', files: [], error: 'File listing is not available in this mode' }
|
|
256
|
+
if (sessionId) response.sessionId = sessionId
|
|
257
|
+
sendFn(ws, response)
|
|
258
|
+
return
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const MAX_DEPTH = 3
|
|
262
|
+
const MAX_FILES = 1000
|
|
263
|
+
|
|
264
|
+
try {
|
|
265
|
+
const cwdReal = await resolveSessionCwd(sessionCwd)
|
|
266
|
+
|
|
267
|
+
// Load .gitignore if it exists
|
|
268
|
+
let gitignoreRules = []
|
|
269
|
+
try {
|
|
270
|
+
const content = await readFile(join(cwdReal, '.gitignore'), 'utf-8')
|
|
271
|
+
gitignoreRules = parseGitignore(content)
|
|
272
|
+
} catch {
|
|
273
|
+
// No .gitignore or unreadable — proceed without
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const files = []
|
|
277
|
+
|
|
278
|
+
async function walk(dir, depth) {
|
|
279
|
+
if (depth > MAX_DEPTH || files.length >= MAX_FILES) return
|
|
280
|
+
|
|
281
|
+
let dirents
|
|
282
|
+
try {
|
|
283
|
+
dirents = await readdir(dir, { withFileTypes: true })
|
|
284
|
+
} catch (err) {
|
|
285
|
+
if (err && err.code === 'EMFILE') {
|
|
286
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
287
|
+
await new Promise(r => setTimeout(r, 50 * (attempt + 1)))
|
|
288
|
+
try {
|
|
289
|
+
dirents = await readdir(dir, { withFileTypes: true })
|
|
290
|
+
break
|
|
291
|
+
} catch (retryErr) {
|
|
292
|
+
if (retryErr?.code !== 'EMFILE' || attempt === 2) return
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
if (!dirents) return
|
|
296
|
+
} else {
|
|
297
|
+
return
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
for (const d of dirents) {
|
|
302
|
+
if (files.length >= MAX_FILES) break
|
|
303
|
+
if (d.name.startsWith('.')) continue
|
|
304
|
+
if (d.name === 'node_modules') continue
|
|
305
|
+
|
|
306
|
+
const absPath = join(dir, d.name)
|
|
307
|
+
const relPath = relative(cwdReal, absPath)
|
|
308
|
+
|
|
309
|
+
// Validate symlinks stay within CWD boundary
|
|
310
|
+
if (d.isSymbolicLink()) {
|
|
311
|
+
try {
|
|
312
|
+
const { valid: symValid } = await validatePathWithinCwd(absPath, sessionCwd)
|
|
313
|
+
if (!symValid) continue
|
|
314
|
+
} catch {
|
|
315
|
+
continue // Skip broken symlinks
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// Check gitignore
|
|
320
|
+
const isDir = d.isDirectory() || d.isSymbolicLink()
|
|
321
|
+
if (isIgnored(relPath, gitignoreRules, isDir)) continue
|
|
322
|
+
|
|
323
|
+
if (d.isDirectory() || d.isSymbolicLink()) {
|
|
324
|
+
// For symlinks, verify the target is a directory before walking
|
|
325
|
+
if (d.isSymbolicLink()) {
|
|
326
|
+
try {
|
|
327
|
+
const s = await stat(absPath)
|
|
328
|
+
if (!s.isDirectory()) {
|
|
329
|
+
// Symlink to a file — treat as file
|
|
330
|
+
files.push({ path: relPath, type: 'file', size: s.size })
|
|
331
|
+
continue
|
|
332
|
+
}
|
|
333
|
+
} catch { continue }
|
|
334
|
+
}
|
|
335
|
+
await walk(absPath, depth + 1)
|
|
336
|
+
} else {
|
|
337
|
+
let size = null
|
|
338
|
+
try {
|
|
339
|
+
const s = await stat(absPath)
|
|
340
|
+
size = s.size
|
|
341
|
+
} catch { /* skip size on error */ }
|
|
342
|
+
|
|
343
|
+
files.push({ path: relPath, type: 'file', size })
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
await walk(cwdReal, 0)
|
|
349
|
+
|
|
350
|
+
// Sort alphabetically by path
|
|
351
|
+
files.sort((a, b) => a.path.localeCompare(b.path))
|
|
352
|
+
|
|
353
|
+
// Apply query filter if provided
|
|
354
|
+
let result = files
|
|
355
|
+
if (query && typeof query === 'string' && query.trim()) {
|
|
356
|
+
const lower = query.toLowerCase()
|
|
357
|
+
result = files.filter(f => f.path.toLowerCase().includes(lower))
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const response = { type: 'file_list', files: result, error: null }
|
|
361
|
+
if (sessionId) response.sessionId = sessionId
|
|
362
|
+
sendFn(ws, response)
|
|
363
|
+
} catch (err) {
|
|
364
|
+
let message = 'Failed to list files'
|
|
365
|
+
if (err && typeof err === 'object' && err.code === 'ENOENT') {
|
|
366
|
+
message = 'Directory not found'
|
|
367
|
+
} else if (err && typeof err === 'object' && err.code === 'EACCES') {
|
|
368
|
+
message = 'Permission denied'
|
|
369
|
+
}
|
|
370
|
+
const response = { type: 'file_list', files: [], error: message }
|
|
371
|
+
if (sessionId) response.sessionId = sessionId
|
|
372
|
+
sendFn(ws, response)
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* List available slash commands. Merges three sources:
|
|
378
|
+
*
|
|
379
|
+
* 1. Built-in commands baked into the active provider's CLI/SDK (e.g.
|
|
380
|
+
* `/clear`, `/compact`, `/model`) — sourced from builtin-commands.js
|
|
381
|
+
* and filtered by the provider's `capabilities` (#3856).
|
|
382
|
+
* 2. Project-scoped markdown skills in `<cwd>/.claude/commands/`.
|
|
383
|
+
* 3. User-scoped markdown skills in `~/.claude/commands/`.
|
|
384
|
+
*
|
|
385
|
+
* Precedence on name collision is built-in > project > user. Users can't
|
|
386
|
+
* shadow `/clear` with a markdown file of the same name — keeps the picker
|
|
387
|
+
* predictable across machines and matches the provider's actual behaviour
|
|
388
|
+
* (the built-in is what the CLI executes regardless of what we render).
|
|
389
|
+
*
|
|
390
|
+
* Sort order: built-ins first (alphabetical), then everything else
|
|
391
|
+
* (alphabetical). Surfaces the canonical provider commands at the top of
|
|
392
|
+
* the picker so newcomers see them before their custom skills.
|
|
393
|
+
*
|
|
394
|
+
* @param {*} ws - WebSocket client
|
|
395
|
+
* @param {string|null} cwd - Session working directory (scanned for .claude/commands)
|
|
396
|
+
* @param {string|null} sessionId - Session ID to include in the response
|
|
397
|
+
* @param {string|null} [providerName] - Active session's provider id; used
|
|
398
|
+
* to look up built-ins. Null/unknown providers skip the built-in merge
|
|
399
|
+
* (picker still shows project + user commands).
|
|
400
|
+
*/
|
|
401
|
+
/**
|
|
402
|
+
* Compute the merged slash-command list (built-ins + project/user .md files)
|
|
403
|
+
* without touching the socket. Shared by the `list_slash_commands` request
|
|
404
|
+
* path (`listSlashCommands`) and the connect-time `auth_bootstrap` burst
|
|
405
|
+
* (#5555) so both produce an identical `commands` array.
|
|
406
|
+
*/
|
|
407
|
+
async function computeSlashCommands(cwd, providerName = null) {
|
|
408
|
+
const commands = []
|
|
409
|
+
const seen = new Set()
|
|
410
|
+
|
|
411
|
+
// 1. Built-ins go in first so they win every name collision.
|
|
412
|
+
let capabilities = null
|
|
413
|
+
if (providerName && typeof providerName === 'string') {
|
|
414
|
+
try {
|
|
415
|
+
const ProviderClass = getProvider(providerName)
|
|
416
|
+
capabilities = ProviderClass?.capabilities || null
|
|
417
|
+
} catch {
|
|
418
|
+
// Unknown provider — fall through with null capabilities. This is
|
|
419
|
+
// expected for legacy single-cliSession mode (no provider field) and
|
|
420
|
+
// for sessions whose provider was unregistered between server starts.
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
const builtins = getBuiltinCommands(providerName, capabilities)
|
|
424
|
+
for (const cmd of builtins) {
|
|
425
|
+
if (seen.has(cmd.name)) continue
|
|
426
|
+
seen.add(cmd.name)
|
|
427
|
+
commands.push(cmd)
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const scanDir = async (dir, source) => {
|
|
431
|
+
try {
|
|
432
|
+
const entries = await readdir(dir, { withFileTypes: true })
|
|
433
|
+
for (const entry of entries) {
|
|
434
|
+
if (!entry.isFile() || !entry.name.endsWith('.md')) continue
|
|
435
|
+
if (entry.name.includes('/') || entry.name.includes('\\')) continue
|
|
436
|
+
const name = entry.name.slice(0, -3)
|
|
437
|
+
if (seen.has(name)) continue
|
|
438
|
+
seen.add(name)
|
|
439
|
+
|
|
440
|
+
let description = ''
|
|
441
|
+
try {
|
|
442
|
+
const content = await readFile(join(dir, entry.name), 'utf-8')
|
|
443
|
+
const lines = content.split('\n')
|
|
444
|
+
for (const line of lines) {
|
|
445
|
+
const trimmed = line.trim()
|
|
446
|
+
if (!trimmed || trimmed.startsWith('#')) continue
|
|
447
|
+
description = trimmed.slice(0, 120)
|
|
448
|
+
break
|
|
449
|
+
}
|
|
450
|
+
} catch (err) {
|
|
451
|
+
log.error(`Failed to read command file ${join(dir, entry.name)}: ${err.message}`)
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
commands.push({ name, description, source })
|
|
455
|
+
}
|
|
456
|
+
} catch {
|
|
457
|
+
// Directory doesn't exist or is unreadable
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
if (cwd) {
|
|
462
|
+
await scanDir(join(cwd, '.claude', 'commands'), 'project')
|
|
463
|
+
}
|
|
464
|
+
await scanDir(join(homedir(), '.claude', 'commands'), 'user')
|
|
465
|
+
|
|
466
|
+
// Stable order: built-ins (alphabetical) first, then project/user
|
|
467
|
+
// (alphabetical) — using `source` as the primary key. `builtin` sorts
|
|
468
|
+
// before `project` and `user` lexically.
|
|
469
|
+
const sourceRank = (s) => (s === 'builtin' ? 0 : s === 'project' ? 1 : 2)
|
|
470
|
+
commands.sort((a, b) => {
|
|
471
|
+
const rankDiff = sourceRank(a.source) - sourceRank(b.source)
|
|
472
|
+
if (rankDiff !== 0) return rankDiff
|
|
473
|
+
return a.name.localeCompare(b.name)
|
|
474
|
+
})
|
|
475
|
+
|
|
476
|
+
return commands
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
async function listSlashCommands(ws, cwd, sessionId, providerName = null) {
|
|
480
|
+
const commands = await computeSlashCommands(cwd, providerName)
|
|
481
|
+
const response = { type: 'slash_commands', commands }
|
|
482
|
+
if (sessionId) response.sessionId = sessionId
|
|
483
|
+
sendFn(ws, response)
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* List custom agents from project and user agent directories.
|
|
488
|
+
*
|
|
489
|
+
* @param {*} ws - WebSocket client
|
|
490
|
+
* @param {string|null} cwd - Session working directory (scanned for .claude/agents)
|
|
491
|
+
* @param {string|null} sessionId - Session ID to include in the response
|
|
492
|
+
* @param {Object} [opts] - Options
|
|
493
|
+
* @param {string[]} [opts.userAgentsDirs] - Override the list of user-level agent directories
|
|
494
|
+
* to scan (#2965). When omitted, defaults to [~/.claude/agents].
|
|
495
|
+
*/
|
|
496
|
+
/**
|
|
497
|
+
* Compute the merged custom-agent list (project + user `.claude/agents`)
|
|
498
|
+
* without touching the socket. Shared by the `list_agents` request path
|
|
499
|
+
* (`listAgents`) and the connect-time `auth_bootstrap` burst (#5555).
|
|
500
|
+
*/
|
|
501
|
+
async function computeAgents(cwd, opts = {}) {
|
|
502
|
+
/**
|
|
503
|
+
* List the candidate agent .md files in a single directory. Returns
|
|
504
|
+
* `{ dir, source, filenames[] }`. Errors (missing dir, unreadable, etc.)
|
|
505
|
+
* yield an empty filenames array so a single failed dir does not abort
|
|
506
|
+
* the others (#3024).
|
|
507
|
+
*/
|
|
508
|
+
const listDir = async (dir, source) => {
|
|
509
|
+
try {
|
|
510
|
+
const entries = await readdir(dir, { withFileTypes: true })
|
|
511
|
+
const filenames = entries
|
|
512
|
+
.filter(e => e.isFile() && e.name.endsWith('.md'))
|
|
513
|
+
.filter(e => !e.name.includes('/') && !e.name.includes('\\'))
|
|
514
|
+
.map(e => e.name)
|
|
515
|
+
return { dir, source, filenames }
|
|
516
|
+
} catch {
|
|
517
|
+
// Directory doesn't exist or is unreadable
|
|
518
|
+
return { dir, source, filenames: [] }
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
// Scan user-level agent directories — default ~/.claude/agents, or
|
|
523
|
+
// the caller-supplied list when multiple providers are active (#2965).
|
|
524
|
+
const userAgentsDirs = (Array.isArray(opts.userAgentsDirs) && opts.userAgentsDirs.length > 0)
|
|
525
|
+
? opts.userAgentsDirs
|
|
526
|
+
: [join(homedir(), '.claude', 'agents')]
|
|
527
|
+
|
|
528
|
+
// Phase 1: readdir project + all user dirs in parallel (#3024). Promise.all
|
|
529
|
+
// preserves input order in the resolved array, so first-wins dedup below
|
|
530
|
+
// matches the previous sequential semantics: project dir first, then user
|
|
531
|
+
// dirs in their provided order.
|
|
532
|
+
const dirScans = []
|
|
533
|
+
if (cwd) {
|
|
534
|
+
dirScans.push(listDir(join(cwd, '.claude', 'agents'), 'project'))
|
|
535
|
+
}
|
|
536
|
+
for (const dir of userAgentsDirs) {
|
|
537
|
+
dirScans.push(listDir(dir, 'user'))
|
|
538
|
+
}
|
|
539
|
+
const dirResults = await Promise.all(dirScans)
|
|
540
|
+
|
|
541
|
+
// Phase 2: dedupe by agent name in input order — first occurrence wins.
|
|
542
|
+
// Skipping duplicates here avoids unnecessary readFile calls in later dirs
|
|
543
|
+
// (matching the old behavior that short-circuited via `seen.has(name)`).
|
|
544
|
+
const winners = []
|
|
545
|
+
const seen = new Set()
|
|
546
|
+
for (const { dir, source, filenames } of dirResults) {
|
|
547
|
+
for (const filename of filenames) {
|
|
548
|
+
const name = filename.slice(0, -3)
|
|
549
|
+
if (seen.has(name)) continue
|
|
550
|
+
seen.add(name)
|
|
551
|
+
winners.push({ name, dir, source, filename })
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
// Phase 3: read the winning agent files in parallel and parse descriptions.
|
|
556
|
+
const agents = await Promise.all(winners.map(async ({ name, dir, source, filename }) => {
|
|
557
|
+
let description = ''
|
|
558
|
+
try {
|
|
559
|
+
const content = await readFile(join(dir, filename), 'utf-8')
|
|
560
|
+
const lines = content.split('\n')
|
|
561
|
+
for (const line of lines) {
|
|
562
|
+
const trimmed = line.trim()
|
|
563
|
+
if (!trimmed || trimmed.startsWith('#')) continue
|
|
564
|
+
description = trimmed.slice(0, 120)
|
|
565
|
+
break
|
|
566
|
+
}
|
|
567
|
+
} catch (err) {
|
|
568
|
+
log.error(`Failed to read agent file ${join(dir, filename)}: ${err.message}`)
|
|
569
|
+
}
|
|
570
|
+
return { name, description, source }
|
|
571
|
+
}))
|
|
572
|
+
|
|
573
|
+
agents.sort((a, b) => a.name.localeCompare(b.name))
|
|
574
|
+
|
|
575
|
+
return agents
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
async function listAgents(ws, cwd, sessionId, opts = {}) {
|
|
579
|
+
const agents = await computeAgents(cwd, opts)
|
|
580
|
+
const response = { type: 'agent_list', agents }
|
|
581
|
+
if (sessionId) response.sessionId = sessionId
|
|
582
|
+
sendFn(ws, response)
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
return {
|
|
586
|
+
listDirectory,
|
|
587
|
+
browseFiles,
|
|
588
|
+
listFiles,
|
|
589
|
+
listSlashCommands,
|
|
590
|
+
listAgents,
|
|
591
|
+
computeSlashCommands,
|
|
592
|
+
computeAgents,
|
|
593
|
+
}
|
|
594
|
+
}
|