Merge remote-tracking branch 'origin/feat/directory-picker' into feat/workspace-directory-browser

This commit is contained in:
creatixchu
2026-07-29 01:10:33 +08:00
312 changed files with 8172 additions and 3465 deletions

View File

@@ -2,5 +2,5 @@
# side as of the last confirmed-consistent state. Both languages carry equal authority;
# after editing either side, bring the other along and re-record with:
# pnpm run verify-translation-pairing --write apps/cli/README.md
README.md: f5e52382fb86ecd6b96b84b90b310514285f1904
README.zh.md: b2089c67d751a25c6443a5e15b53266c728e5156
README.md: 5e7326107e46d5a469f99365ea25168dc09950c3
README.zh.md: 9dad51cf012293ba9ecba08b22e62e9249016602

View File

@@ -26,4 +26,6 @@ Symlink the source-running launcher onto your PATH; it resolves the checkout thr
ln -sf "$(pwd)/bin/dsh" ~/.local/bin/dsh
```
Source launches run `apps/cli/src/bin.ts` through Node's `--experimental-transform-types`; `scripts/tspath-loader.ts` only projects tsconfig `paths` into module resolution and does not transform code. Every module reachable from the CLI source entry follows Node's transform-types contract: erased bindings use `import type`, exports use native ESM, and the graph contains no TSX/JSX or transforms that only tsx/esbuild provides. The loader reads `TSX_TSCONFIG_PATH` when set (relative paths resolve from the invoking cwd), otherwise the repository's root tsconfig, using the root TypeScript development tool rather than an application dependency. It maps a workspace import only for a package self-reference or a declared runtime dependency. The TUI configs resolve bare plugins through `examples/package.json`, while the Web/headless `cordis.yml` resolves them through this package's `dependencies`; `verify-cordis-config` requires every configured bare plugin to be declared, while allowing unrelated dependencies.
`pnpm run dsh` runs the same entry from the repo root and forwards arguments directly, for example `pnpm run dsh -p "task"`. The built form (`lib/bin.js`, via `pnpm run build`) boots the same config under plain Node.

View File

@@ -26,4 +26,6 @@ Web 和无头界面启动同一个共享组合(`cordis.yml`):两者都将
ln -sf "$(pwd)/bin/dsh" ~/.local/bin/dsh
```
源码启动会通过 Node 的 `--experimental-transform-types` 运行 `apps/cli/src/bin.ts``scripts/tspath-loader.ts` 只会将 tsconfig 的 `paths` 映射投射到模块解析中,而不会转换代码。从 CLI 源码入口可达的每个模块都遵守 Node transform-types 契约:会被擦除的绑定使用 `import type`export 使用原生 ESM整个依赖图不含 TSX/JSX也不依赖仅由 tsx/esbuild 提供的转换。设置 `TSX_TSCONFIG_PATH`loader 会读取该路径(相对路径从调用方的 cwd 解析),否则读取仓库根 tsconfig它使用根目录的 TypeScript 开发工具,而不是应用依赖。仅当 workspace import 是包自身引用或已声明的运行时依赖时loader 才会映射该 import。TUI 配置通过 `examples/package.json` 解析裸插件,而 Web无头 `cordis.yml` 则通过本包的 `dependencies` 解析;`verify-cordis-config` 要求每个已配置的裸插件均已声明,同时允许存在无关依赖。
`pnpm run dsh` 从仓库根目录运行同一入口并直接转发参数,例如 `pnpm run dsh -p "task"`。构建形式(`lib/bin.js`,通过 `pnpm run build`)会在普通 Node 下启动同一配置。

View File

@@ -116,6 +116,16 @@
- id: workspace
name: '@deepseek-ai/dsh-workspace'
# Persisted projection cache: durable per-session checkpoints of every
# registered projection unit (json backend → ./.storages/session_projcache.json,
# beside workspace.json), throttled between the two mandatory points
# (turn/end + detach), serving cold listings without full-log loads.
- id: session-projection-cache
name: '@deepseek-ai/dsh-session-projection-cache'
config:
writeEveryEvents: 200
writeIntervalMs: 5000
# Managed child-process groups for the bash executor (spawn/kill/output plumbing).
- id: subprocess
name: '@deepseek-ai/dsh-subprocess-local'

View File

@@ -58,6 +58,7 @@
"@deepseek-ai/dsh-session": "workspace:^",
"@deepseek-ai/dsh-session-persistence-jsonl": "workspace:^",
"@deepseek-ai/dsh-session-projection": "workspace:^",
"@deepseek-ai/dsh-session-projection-cache": "workspace:^",
"@deepseek-ai/dsh-session-title": "workspace:^",
"@deepseek-ai/dsh-session-title-first-message-llm": "workspace:^",
"@deepseek-ai/dsh-skill": "workspace:^",

View File

@@ -0,0 +1,216 @@
/**
* Node module resolve hook for the `dsh` source launcher. It projects the root
* tsconfig `paths` map into Node resolution while leaving all TypeScript syntax
* handling to Node's native transform-types runtime.
* @module @deepseek-ai/dsh/tsconfig-paths-loader
*/
import { readFile, stat } from 'node:fs/promises'
import { dirname, extname, join, resolve } from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
import type { ResolveHookContext, ResolveFnOutput } from 'node:module'
import ts from 'typescript'
interface LoaderData {
tsconfigPath: string
}
interface PackageManifest {
name?: string
dependencies?: Record<string, string>
optionalDependencies?: Record<string, string>
peerDependencies?: Record<string, string>
}
interface PathRule {
pattern: string
prefix: string
suffix: string
targets: readonly string[]
}
interface PathsCompilerOptions {
readonly baseUrl?: string
readonly paths?: ts.MapLike<string[]>
readonly pathsBasePath?: string
}
// Node's native TypeScript transform cannot parse JSX, so `.tsx` is excluded.
const SOURCE_EXTENSIONS = ['.ts', '.mts', '.cts'] as const
/**
* Resolve package imports through one parsed tsconfig paths table.
*
* Manifest reads are process-scoped and memoized by path. Only matched source
* aliases enter the cache, bounding it to directories participating in source
* resolution.
*/
export class TsconfigPathsResolver {
private readonly rules: readonly PathRule[]
private readonly configDirectory: string
private readonly manifests = new Map<string, Promise<PackageManifest | undefined>>()
private constructor(configDirectory: string, paths: ts.MapLike<string[]>) {
this.configDirectory = configDirectory
this.rules = Object.entries(paths)
.map(([pattern, targets]) => {
const wildcard = pattern.indexOf('*')
return {
pattern,
prefix: wildcard === -1 ? pattern : pattern.slice(0, wildcard),
suffix: wildcard === -1 ? '' : pattern.slice(wildcard + 1),
targets,
}
})
.sort((left, right) => {
const leftExact = left.pattern.includes('*') ? 0 : 1
const rightExact = right.pattern.includes('*') ? 0 : 1
return rightExact - leftExact || right.prefix.length - left.prefix.length || right.suffix.length - left.suffix.length
})
}
/**
* Parse a tsconfig including its `extends` chain.
* @param tsconfigPath Absolute tsconfig path supplying `compilerOptions.paths`.
* @returns A resolver backed by that path table.
*/
static create(tsconfigPath: string): TsconfigPathsResolver {
let unrecoverable: ts.Diagnostic | undefined
const parsed = ts.getParsedCommandLineOfConfigFile(tsconfigPath, {}, {
...ts.sys,
onUnRecoverableConfigFileDiagnostic(diagnostic) { unrecoverable = diagnostic },
})
if (parsed === undefined) {
const detail = unrecoverable === undefined
? 'unknown configuration error'
: ts.flattenDiagnosticMessageText(unrecoverable.messageText, '\n')
throw new Error(`dsh source loader could not parse ${tsconfigPath}: ${detail}`)
}
const options = parsed.options as PathsCompilerOptions
const paths = options.paths
if (paths === undefined) throw new Error(`dsh source loader requires compilerOptions.paths in ${tsconfigPath}`)
const configDirectory = options.baseUrl ?? options.pathsBasePath ?? dirname(tsconfigPath)
return new TsconfigPathsResolver(configDirectory, paths)
}
/**
* Resolve one bare package specifier to a source file when the importing
* package (or config-directory owner) declares that package at runtime.
* @param specifier Module specifier passed to Node.
* @param parentURL Importing file or Loader config-directory URL.
* @returns Source file URL, or `undefined` when normal Node resolution owns the request.
*/
async resolve(specifier: string, parentURL: string | undefined): Promise<string | undefined> {
const packageName = packageNameFromSpecifier(specifier)
if (packageName === undefined || parentURL === undefined || !parentURL.startsWith('file:')) return undefined
const matched = this.match(specifier)
if (matched === undefined) return undefined
const configParent = parentURL.endsWith('/')
const parentPath = fileURLToPath(parentURL)
const startDirectory = configParent ? parentPath : dirname(parentPath)
if (!await this.isDeclaredRuntimeDependency(startDirectory, packageName, configParent)) return undefined
for (const target of matched.targets) {
const substituted = target.replace('*', matched.wildcard)
const candidate = await existingSourcePath(resolve(this.configDirectory, substituted))
if (candidate !== undefined) return pathToFileURL(candidate).href
}
return undefined
}
private match(specifier: string): { targets: readonly string[]; wildcard: string } | undefined {
for (const rule of this.rules) {
if (!rule.pattern.includes('*')) {
if (specifier === rule.pattern) return { targets: rule.targets, wildcard: '' }
continue
}
if (!specifier.startsWith(rule.prefix) || !specifier.endsWith(rule.suffix)) continue
const wildcard = specifier.slice(rule.prefix.length, specifier.length - rule.suffix.length)
return { targets: rule.targets, wildcard }
}
return undefined
}
private async isDeclaredRuntimeDependency(
startDirectory: string,
packageName: string,
searchAncestors: boolean,
): Promise<boolean> {
for (let directory = startDirectory; ; directory = dirname(directory)) {
const manifest = await this.readManifest(join(directory, 'package.json'))
if (manifest !== undefined) {
if (declaresRuntimeDependency(manifest, packageName)) return true
if (!searchAncestors) return false
}
const parent = dirname(directory)
if (parent === directory) return false
}
}
private readManifest(path: string): Promise<PackageManifest | undefined> {
let pending = this.manifests.get(path)
if (pending !== undefined) return pending
pending = readFile(path, 'utf8').then(
content => JSON.parse(content) as PackageManifest,
(error: unknown) => {
if (error instanceof Error && (error as NodeJS.ErrnoException).code === 'ENOENT') return undefined
throw error
},
)
this.manifests.set(path, pending)
return pending
}
}
let resolver: TsconfigPathsResolver | undefined
/** Initialize the hook worker from the source-launch preloader. */
export function initialize(data: LoaderData): void {
resolver = TsconfigPathsResolver.create(data.tsconfigPath)
}
/** Resolve declared workspace packages to source and delegate every other request to Node. */
export async function resolveHook(
specifier: string,
context: ResolveHookContext,
nextResolve: (specifier: string, context: ResolveHookContext) => Promise<ResolveFnOutput>,
): Promise<ResolveFnOutput> {
const url = await resolver?.resolve(specifier, context.parentURL)
return url === undefined ? nextResolve(specifier, context) : { url, shortCircuit: true }
}
// Node customization hooks discover this exact export name.
export { resolveHook as resolve }
function packageNameFromSpecifier(specifier: string): string | undefined {
if (specifier.startsWith('.') || specifier.startsWith('/') || /^[a-z][a-z+.-]*:/i.test(specifier)) {
return undefined
}
const segments = specifier.split('/')
return specifier.startsWith('@')
? segments.length >= 2 ? `${segments[0]}/${segments[1]}` : undefined
: segments[0] || undefined
}
function declaresRuntimeDependency(manifest: PackageManifest, packageName: string): boolean {
return manifest.name === packageName
|| packageName in (manifest.dependencies ?? {})
|| packageName in (manifest.optionalDependencies ?? {})
|| packageName in (manifest.peerDependencies ?? {})
}
async function existingSourcePath(base: string): Promise<string | undefined> {
const extension = extname(base)
if (extension === '.tsx') return undefined
const candidates = extension === ''
? [base, ...SOURCE_EXTENSIONS.map(extension => `${base}${extension}`), ...SOURCE_EXTENSIONS.map(extension => join(base, `index${extension}`))]
: [base]
for (const candidate of candidates) {
try {
if ((await stat(candidate)).isFile()) return candidate
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error
}
}
return undefined
}

View File

@@ -0,0 +1,180 @@
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import type { ResolveFnOutput, ResolveHookContext } from 'node:module'
import { tmpdir } from 'node:os'
import { dirname, join } from 'node:path'
import { pathToFileURL } from 'node:url'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { initialize, resolveHook, TsconfigPathsResolver } from '../src/tsconfig-paths-loader.ts'
class ResolverFixture {
readonly root = mkdtempSync(join(tmpdir(), 'dsh-tsconfig-paths-'))
path(relativePath: string): string {
return join(this.root, relativePath)
}
write(relativePath: string, content = 'export {}\n'): string {
const path = this.path(relativePath)
mkdirSync(dirname(path), { recursive: true })
writeFileSync(path, content)
return path
}
writeJson(relativePath: string, value: unknown): string {
return this.write(relativePath, `${JSON.stringify(value)}\n`)
}
createResolver(paths: Record<string, string[]>): TsconfigPathsResolver {
const tsconfigPath = this.writeJson('tsconfig.json', { compilerOptions: { paths } })
return TsconfigPathsResolver.create(tsconfigPath)
}
parentURL(relativePath = 'consumer/src/nested/index.ts'): string {
return pathToFileURL(this.path(relativePath)).href
}
dispose(): void {
rmSync(this.root, { recursive: true, force: true })
}
}
const fixtures: ResolverFixture[] = []
function fixture(): ResolverFixture {
const value = new ResolverFixture()
fixtures.push(value)
return value
}
afterEach(() => {
for (const value of fixtures.splice(0)) value.dispose()
})
describe('TsconfigPathsResolver', () => {
it('orders exact, longer-prefix, and longer-suffix path rules', async () => {
const files = fixture()
files.writeJson('consumer/package.json', {
dependencies: {
'@scope/feature-name': '*',
'@scope/feature-other': '*',
'@scope/plain-suffix': '*',
},
})
files.write('targets/exact.ts')
files.write('targets/prefix/other.ts')
files.write('targets/generic/feature-other.ts')
files.write('targets/suffix/plain.ts')
files.write('targets/generic/plain-suffix.ts')
const resolver = files.createResolver({
'@scope/*': ['./targets/generic/*'],
'@scope/*-suffix': ['./targets/suffix/*'],
'@scope/feature-*': ['./targets/prefix/*'],
'@scope/feature-name': ['./targets/exact.ts'],
})
await expect(resolver.resolve('@scope/feature-name', files.parentURL()))
.resolves.toBe(pathToFileURL(files.path('targets/exact.ts')).href)
await expect(resolver.resolve('@scope/feature-other', files.parentURL()))
.resolves.toBe(pathToFileURL(files.path('targets/prefix/other.ts')).href)
await expect(resolver.resolve('@scope/plain-suffix', files.parentURL()))
.resolves.toBe(pathToFileURL(files.path('targets/suffix/plain.ts')).href)
})
it('resolves only self-references and runtime dependencies from the nearest ancestor manifest', async () => {
const files = fixture()
files.writeJson('consumer/package.json', {
name: 'self-package',
dependencies: { dependency: '*' },
optionalDependencies: { optional: '*' },
peerDependencies: { peer: '*' },
})
for (const name of ['self-package', 'dependency', 'optional', 'peer', 'undeclared']) {
files.write(`targets/${name}.ts`)
}
const resolver = files.createResolver(Object.fromEntries(
['self-package', 'dependency', 'optional', 'peer', 'undeclared']
.map(name => [name, [`./targets/${name}`]]),
))
for (const name of ['self-package', 'dependency', 'optional', 'peer']) {
await expect(resolver.resolve(name, files.parentURL()))
.resolves.toBe(pathToFileURL(files.path(`targets/${name}.ts`)).href)
}
await expect(resolver.resolve('undeclared', files.parentURL())).resolves.toBeUndefined()
})
it('probes native TypeScript extensions and index files but excludes TSX and missing targets', async () => {
const files = fixture()
const names = ['plain-ts', 'module-mts', 'common-cts', 'directory', 'tsx-implicit', 'tsx-explicit', 'missing']
files.writeJson('consumer/package.json', {
dependencies: Object.fromEntries(names.map(name => [name, '*'])),
})
files.write('targets/plain.ts')
files.write('targets/module.mts')
files.write('targets/common.cts')
files.write('targets/directory/index.ts')
files.write('targets/component.tsx')
const resolver = files.createResolver({
'plain-ts': ['./targets/plain'],
'module-mts': ['./targets/module'],
'common-cts': ['./targets/common'],
'directory': ['./targets/directory'],
'tsx-implicit': ['./targets/component'],
'tsx-explicit': ['./targets/component.tsx'],
'missing': ['./targets/missing'],
})
for (const [name, target] of [
['plain-ts', 'targets/plain.ts'],
['module-mts', 'targets/module.mts'],
['common-cts', 'targets/common.cts'],
['directory', 'targets/directory/index.ts'],
] as const) {
await expect(resolver.resolve(name, files.parentURL()))
.resolves.toBe(pathToFileURL(files.path(target)).href)
}
await expect(resolver.resolve('tsx-implicit', files.parentURL())).resolves.toBeUndefined()
await expect(resolver.resolve('tsx-explicit', files.parentURL())).resolves.toBeUndefined()
await expect(resolver.resolve('missing', files.parentURL())).resolves.toBeUndefined()
})
it('anchors inherited paths at the config that declared them', async () => {
const files = fixture()
files.writeJson('consumer/package.json', { dependencies: { custom: '*' } })
files.write('targets/custom.ts')
files.writeJson('base.json', { compilerOptions: { paths: { custom: ['./targets/custom'] } } })
const customTsconfig = files.writeJson('configs/custom.json', { extends: '../base.json' })
const resolver = TsconfigPathsResolver.create(customTsconfig)
await expect(resolver.resolve('custom', files.parentURL()))
.resolves.toBe(pathToFileURL(files.path('targets/custom.ts')).href)
})
it('short-circuits matched aliases and delegates unsupported schemes or unmatched requests', async () => {
const files = fixture()
files.writeJson('consumer/package.json', { dependencies: { matched: '*' } })
const target = files.write('targets/matched.ts')
const tsconfigPath = files.writeJson('tsconfig.json', {
compilerOptions: { paths: { matched: ['./targets/matched'] } },
})
initialize({ tsconfigPath })
const context: ResolveHookContext = {
conditions: [],
importAttributes: {},
parentURL: files.parentURL(),
}
const nextResolve = vi.fn(async (
specifier: string,
_context: ResolveHookContext,
): Promise<ResolveFnOutput> => ({ url: `next:${specifier}` }))
await expect(resolveHook('matched', context, nextResolve))
.resolves.toEqual({ url: pathToFileURL(target).href, shortCircuit: true })
expect(nextResolve).not.toHaveBeenCalled()
for (const specifier of ['unmatched', 'node:fs', 'data:text/javascript,export default 1', 'https://example.test/mod.ts']) {
await expect(resolveHook(specifier, context, nextResolve)).resolves.toEqual({ url: `next:${specifier}` })
expect(nextResolve).toHaveBeenLastCalledWith(specifier, context)
}
})
})