diff --git a/.agents/notes/implemented/architecture/2026-10-27-plugin-cli-absolute-paths.i18n.yaml b/.agents/notes/implemented/architecture/2026-10-27-plugin-cli-absolute-paths.i18n.yaml index a624eda01f..3278cd0102 100644 --- a/.agents/notes/implemented/architecture/2026-10-27-plugin-cli-absolute-paths.i18n.yaml +++ b/.agents/notes/implemented/architecture/2026-10-27-plugin-cli-absolute-paths.i18n.yaml @@ -9,11 +9,11 @@ en: 623b3e7c6bbb038d zh: 0c3725119fb9c41e /agent-note-plugin-clis-by-absolute-path-in-runtime-context/decision: - en: 8f4689a5d6b71775 - zh: 9dc7def74ede2234 + en: ad847ad6a6a8e491 + zh: f9c09995440c43eb /agent-note-plugin-clis-by-absolute-path-in-runtime-context/alternatives-considered: en: 7c2f56152247cadf zh: a77e41a020ec6835 /agent-note-plugin-clis-by-absolute-path-in-runtime-context/consequences: - en: 69ede127f2b3a8ad - zh: 0e6b1dd5523e1686 + en: c80b941f81db6daf + zh: 964ff6d38b3903d1 diff --git a/.agents/notes/implemented/architecture/2026-10-27-plugin-cli-absolute-paths.md b/.agents/notes/implemented/architecture/2026-10-27-plugin-cli-absolute-paths.md index 5343a0499a..985392a91e 100644 --- a/.agents/notes/implemented/architecture/2026-10-27-plugin-cli-absolute-paths.md +++ b/.agents/notes/implemented/architecture/2026-10-27-plugin-cli-absolute-paths.md @@ -10,7 +10,7 @@ CLI app plugins such as Feishu, WeCom, DingTalk, and Tencent Meeting install a v ## Decision -Each app plugin calls `registerCliPathContext` from [`@deepseek-ai/dsh-cli-account`](../../../../packages/connector/cli-account/README.md) with its CLI's absolute path. The function registers required runtime context at the `PLUGIN_CLIS` context order, without interpolation, that tells the model to run the CLI by that path. For a skill whose scripts call the CLI by name, which applies to DingTalk and WeCom, the context also tells the model to prefix only the commands that run those scripts with `PATH=":$PATH"`, and never to export PATH or run the CLI by name. Skills tell the model to use the path from the current runtime context. The plugin's own sign-in, status, and sign-out commands spawn the same path through `CliCommandExecutor`. +Each app plugin calls `registerCliPathContext` from [`@deepseek-ai/dsh-cli-account`](../../../../packages/connector/cli-account/README.md) with its CLI's absolute path. The function registers required runtime context at the `PLUGIN_CLIS` context order, without interpolation, that tells the model to run the CLI by that path. For a skill whose scripts call the CLI by name, which applies to DingTalk and WeCom, the context also tells the model to prefix only the commands that run those scripts with `PATH=":$PATH"`, and never to export PATH or run the CLI by name. On Windows, where the shipped compositions mount `tool-pwsh` instead of `tool-bash`, the text uses PowerShell syntax: the call operator, `& ''`, and the prefix `$env:PATH = ';' + $env:PATH`. Skills tell the model to use the path from the current runtime context. The plugin's own sign-in, status, and sign-out commands spawn the same path through `CliCommandExecutor`. The harness changes no command environment: Bash, PowerShell, the sandbox, persistent terminals, and hooks run as they do without these plugins. Tencent Meeting registers its platform executable, `dist/tmeet-`. The DingTalk context contributes nothing until dws and its skills are installed. No load-time check requires commands to run on the Host; when a composition runs commands over SSH, a command fails when it reaches the missing path. @@ -30,6 +30,6 @@ The harness changes no command environment: Bash, PowerShell, the sandbox, persi The model, not PATH, maps each name to its path. Counted on 2026-10-23, the upstream guides call the CLIs by name on 3,004 lines for DingTalk, 2,542 for Feishu, 321 for WeCom, and 248 for Tencent Meeting, and 33 DingTalk scripts call `dws` by name. A command that still uses the bare name runs the installation that PATH resolves, or fails because none is found; nothing rewrites it. The script sentence forbids exporting PATH and running the CLI by name because, in real-model runs, a sentence that only asked for the prefix on script commands led the model to export PATH and then run the CLI by name. Approval details, tool cards, and job lists show the full path. -A CLI that the user installed under the same name is no longer hidden, and persistent terminals and hooks need no PATH handling. Runtime context stays out of the cached system prompt and is admitted again after compaction; installing, enabling, or disabling a plugin appends one runtime-context snapshot. The text uses POSIX shell syntax only, because these plugins do not support Windows. +A CLI that the user installed under the same name is no longer hidden, and persistent terminals and hooks need no PATH handling. Runtime context stays out of the cached system prompt and is admitted again after compaction; installing, enabling, or disabling a plugin appends one runtime-context snapshot. The text's dialect follows the Host platform with the same test the compositions use to choose the shell tool, so a composition that mounts the other shell on a platform gets the wrong syntax. Unit tests in the kit pin the text, required status, literal paths, readiness, and removal on disposal; each plugin's recorded Session snapshot pins its rendered context. diff --git a/.agents/notes/implemented/architecture/2026-10-27-plugin-cli-absolute-paths.zh.md b/.agents/notes/implemented/architecture/2026-10-27-plugin-cli-absolute-paths.zh.md index c1995eb0b4..6a61971a61 100644 --- a/.agents/notes/implemented/architecture/2026-10-27-plugin-cli-absolute-paths.zh.md +++ b/.agents/notes/implemented/architecture/2026-10-27-plugin-cli-absolute-paths.zh.md @@ -10,7 +10,7 @@ Status: implemented ## Decision -每个应用插件用 CLI 的绝对路径调用 [`@deepseek-ai/dsh-cli-account`](../../../../packages/connector/cli-account/README.zh.md) 的 `registerCliPathContext`。这个函数在 `PLUGIN_CLIS` 上下文顺序处注册一条必需、不做变量插值的运行时上下文,让模型按这个路径运行 CLI。skill 里的脚本按名称调用 CLI 时(钉钉和企业微信属于这种情况),这条上下文还让模型只在运行这些脚本的命令前加上 `PATH="<目录>:$PATH"`,并且不要导出 PATH,也不要按名称运行 CLI。skill 让模型使用当前运行时上下文给出的路径。插件自己的登录、状态和登出命令通过 `CliCommandExecutor` 启动同一路径。 +每个应用插件用 CLI 的绝对路径调用 [`@deepseek-ai/dsh-cli-account`](../../../../packages/connector/cli-account/README.zh.md) 的 `registerCliPathContext`。这个函数在 `PLUGIN_CLIS` 上下文顺序处注册一条必需、不做变量插值的运行时上下文,让模型按这个路径运行 CLI。skill 里的脚本按名称调用 CLI 时(钉钉和企业微信属于这种情况),这条上下文还让模型只在运行这些脚本的命令前加上 `PATH="<目录>:$PATH"`,并且不要导出 PATH,也不要按名称运行 CLI。在 Windows 上,内置组合挂载 `tool-pwsh` 而不是 `tool-bash`,文本改用 PowerShell 语法:调用运算符 `& '<路径>'`,以及前缀 `$env:PATH = '<目录>;' + $env:PATH`。skill 让模型使用当前运行时上下文给出的路径。插件自己的登录、状态和登出命令通过 `CliCommandExecutor` 启动同一路径。 harness 不改变任何命令的环境:Bash、PowerShell、沙箱、持久终端和 hook 的运行方式与没有这些插件时相同。腾讯会议直接注册各平台的可执行文件 `dist/tmeet-<平台>`。钉钉的上下文在 dws 和钉钉 skill 安装完成前不贡献任何内容。加载时不检查命令是否在 Host 上运行;组合把命令放到 SSH 远端运行时,命令在访问不存在的路径时失败。 @@ -30,6 +30,6 @@ harness 不改变任何命令的环境:Bash、PowerShell、沙箱、持久终 把名称换成路径的是模型,不再是 PATH。按 2026-10-23 的统计,上游指南按名称调用 CLI 的行数为:钉钉 3,004 行、飞书 2,542 行、企业微信 321 行、腾讯会议 248 行;钉钉还有 33 个脚本按名称调用 `dws`。仍用名称的命令会运行 PATH 解析到的那份安装,或者因为找不到而失败;没有任何机制改写它。脚本那句话之所以禁止导出 PATH 和按名称运行 CLI,是因为在真实模型的运行中,只要求给脚本命令加前缀的写法会让模型先导出 PATH、再按名称运行 CLI。审批详情、工具卡和任务列表显示完整路径。 -用户自己安装的同名 CLI 不再被遮住,持久终端和 hook 也不需要处理 PATH。运行时上下文不进入缓存的系统提示词,压缩后会重新提交;安装、启用或停用插件时追加一份运行时上下文快照。文字只用 POSIX shell 语法,因为这些插件目前不支持 Windows。 +用户自己安装的同名 CLI 不再被遮住,持久终端和 hook 也不需要处理 PATH。运行时上下文不进入缓存的系统提示词,压缩后会重新提交;安装、启用或停用插件时追加一份运行时上下文快照。文字的写法按 Host 平台选择,判断条件与组合选择 shell 工具的条件相同;因此某个平台上的组合如果挂载了另一种 shell,得到的写法就不对。 kit 的单元测试固定了文字、必需状态、路径原样保留、就绪条件和释放时移除;每个插件的录制会话快照固定了它渲染出的上下文。 diff --git a/packages/connector/cli-account/README.i18n.yaml b/packages/connector/cli-account/README.i18n.yaml index d91fe5fa89..669f90da1b 100644 --- a/packages/connector/cli-account/README.i18n.yaml +++ b/packages/connector/cli-account/README.i18n.yaml @@ -24,8 +24,8 @@ en: f08d6c7e9b1510b0 zh: fe971ae1d704eecf /deepseek-ai-dsh-cli-account/use-this-package/runtime-context: - en: ec57a680f5f696c6 - zh: 08676e8df0547d64 + en: 581aa6948e67cf64 + zh: 0ad2efcfc3d6daaa /deepseek-ai-dsh-cli-account/use-this-package/commands-links-and-failures: en: b045a8486900b2cf zh: 694192516867923b @@ -45,11 +45,14 @@ en: b5fed52af6c0d771 zh: 25b7aa5297583030 /deepseek-ai-dsh-cli-account/model-experience/cli-path/what-the-model-sees: - en: 340bd96fc40bdfca - zh: fd9f34bbed3252f1 + en: 5774596bf891f8e7 + zh: bb3c4bd4f9675d3b /deepseek-ai-dsh-cli-account/model-experience/cli-path/what-the-model-sees/runtime-context-entry: en: c91314832f78f460 zh: aac156ea0b5b816a +/deepseek-ai-dsh-cli-account/model-experience/cli-path/what-the-model-sees/runtime-context-entry-on-windows: + en: 54f899bbaf7ed198 + zh: 31256473a192427b /deepseek-ai-dsh-cli-account/model-experience/cli-path/token-effect: en: 209efa571e0cc967 zh: c8d778905ab24426 @@ -57,8 +60,8 @@ en: d5988fd18f0b1ad2 zh: f896f32489e219c9 /deepseek-ai-dsh-cli-account/known-limitations-and-deferred-work: - en: fd86b8446db0132d - zh: 1d7567b87f12d70a + en: 6b419ec562b1a0fa + zh: 95c5c87d3336fba4 /deepseek-ai-dsh-cli-account/known-limitations-and-deferred-work/dev-note: en: e67f633dc6fe7773 zh: e88aeb617ffb74ea diff --git a/packages/connector/cli-account/README.md b/packages/connector/cli-account/README.md index c1ba160019..f3633d33fa 100644 --- a/packages/connector/cli-account/README.md +++ b/packages/connector/cli-account/README.md @@ -34,12 +34,13 @@ Use it in the Host row of a plugin whose vendor CLI signs the user in and keeps A plugin states its CLI path in runtime context, subclasses the runtime, and runs each command through its control: ```ts +import { join } from 'node:path' import type { Context } from '@deepseek-ai/cordis' -import { CliAccountRuntime, CliCommandExecutor, exitFailure, registerCliPathContext, type CliOperationControl } from '@deepseek-ai/dsh-cli-account' +import { CliAccountRuntime, CliCommandExecutor, executableFileName, exitFailure, registerCliPathContext, type CliOperationControl } from '@deepseek-ai/dsh-cli-account' import type {} from '@deepseek-ai/dsh-subprocess' declare const ctx: Context -const executable = '/opt/vendor/bin/vendor-cli' +const executable = join('/opt/vendor/bin', executableFileName('vendor-cli')) class Account extends CliAccountRuntime<{ readonly state: 'unchecked' | 'out' | 'in' }, 'check', never> { protected async perform(_kind: 'check', control: CliOperationControl, signal: AbortSignal): Promise { @@ -61,7 +62,7 @@ const account = new Account({ ### Runtime context -`registerCliPathContext(ctx, cli)` registers one required runtime-context entry that tells the model to run `cli.program` by `cli.executable`, the path the executor runs. With `scripts`, it also tells the model to prefix only the commands that run the skill's scripts with `PATH=":$PATH"`, which changes PATH for that command only, and never to export PATH or run `cli.program` by name. With `ready`, the entry contributes nothing while `ready()` returns false. The text is never interpolated, and disposing the plugin removes the entry. [Model Experience](#model-experience) shows the text. +`registerCliPathContext(ctx, cli)` registers one required runtime-context entry that tells the model to run `cli.program` by `cli.executable`, the path the executor runs. With `scripts`, it also tells the model to put the executable's directory first on PATH only in the commands that run the skill's scripts, and never to run `cli.program` by name. The text follows the shell that shipped compositions give the model on the Host platform. Outside Windows it uses POSIX shell syntax: the prefix is `PATH=":$PATH"`, which changes PATH for that command only, and the text also forbids exporting PATH. On Windows, where the model runs PowerShell, it names the executable with the call operator, `& ''`, and gives the prefix as `$env:PATH = ';' + $env:PATH`, which lasts only for that call because each PowerShell call runs in a fresh process. With `ready`, the entry contributes nothing while `ready()` returns false. The text is never interpolated, and disposing the plugin removes the entry. `executableFileName(program)` names the file a plugin locates: `program`, or `program.exe` on Windows. [Model Experience](#model-experience) shows the text. ### Commands, links, and failures @@ -119,7 +120,7 @@ The runtime owns the operation lifecycle and the plugin owns the meaning of each #### What the model sees -One required runtime-context entry, at context order 220, for each plugin that calls `registerCliPathContext` and whose `ready` is absent or returns true; it stays when optional runtime context is off. The second line appears only with `scripts`. The placeholders come from the call, and the quoted values are JSON strings. +One required runtime-context entry, at context order 220, for each plugin that calls `registerCliPathContext` and whose `ready` is absent or returns true; it stays when optional runtime context is off. The second line appears only with `scripts`. The placeholders come from the call. Outside Windows the quoted values are JSON strings; on Windows they are PowerShell single-quoted strings, in which a single quote is doubled. ##### Runtime-context entry @@ -128,6 +129,13 @@ One required runtime-context entry, at context order 220, for each plugin that c Scripts in the skill call `` by name; prefix only the commands that run them with PATH=":$PATH", and never export PATH or run `` by name yourself. ``` +##### Runtime-context entry on Windows + +```markdown + CLI: run `` by its absolute path with the call operator, & ''; the bare name may resolve to another installation or to none. +Scripts in the skill call `` by name; start only the commands that run them with $env:PATH = ';' + $env:PATH, and never run `` by name yourself. +``` + #### Token effect One or two lines in each runtime-context snapshot for each registering plugin. A changed path, or an entry that appears or disappears, adds one snapshot. @@ -144,6 +152,7 @@ The path is runtime context rather than system prompt, so the cached prompt pref - Authorization hosts match their subdomains too; a plugin cannot restrict links to one exact host. - `exitFailure` classifies by exit code alone; a plugin whose CLI reports structured errors writes its own allowlisting classifier. - The runtime context asks the model to run the CLI by its absolute path but cannot enforce it; a command that names the CLI by bare name runs whatever installation PATH resolves, or fails. +- The text's shell dialect follows the Host platform, not the mounted shell tool: a composition that gives the model a POSIX shell on Windows, or PowerShell elsewhere, gets the other platform's syntax. ### Dev Note diff --git a/packages/connector/cli-account/README.zh.md b/packages/connector/cli-account/README.zh.md index 5e605c4be6..8e344d7140 100644 --- a/packages/connector/cli-account/README.zh.md +++ b/packages/connector/cli-account/README.zh.md @@ -34,12 +34,13 @@ kind: "package-library" 插件在运行时上下文中声明 CLI 路径,继承运行时,并通过 control 运行每条命令: ```ts +import { join } from 'node:path' import type { Context } from '@deepseek-ai/cordis' -import { CliAccountRuntime, CliCommandExecutor, exitFailure, registerCliPathContext, type CliOperationControl } from '@deepseek-ai/dsh-cli-account' +import { CliAccountRuntime, CliCommandExecutor, executableFileName, exitFailure, registerCliPathContext, type CliOperationControl } from '@deepseek-ai/dsh-cli-account' import type {} from '@deepseek-ai/dsh-subprocess' declare const ctx: Context -const executable = '/opt/vendor/bin/vendor-cli' +const executable = join('/opt/vendor/bin', executableFileName('vendor-cli')) class Account extends CliAccountRuntime<{ readonly state: 'unchecked' | 'out' | 'in' }, 'check', never> { protected async perform(_kind: 'check', control: CliOperationControl, signal: AbortSignal): Promise { @@ -61,7 +62,7 @@ const account = new Account({ ### 运行时上下文 -`registerCliPathContext(ctx, cli)` 注册一条必需的运行时上下文,让模型按 `cli.executable` 运行 `cli.program`,这正是执行器运行的路径。传入 `scripts` 时,它还让模型只在运行 skill 脚本的命令前加上 `PATH="<可执行文件所在目录>:$PATH"`,只改变这一条命令的 PATH,并且不要导出 PATH,也不要按名称运行 `cli.program`。传入 `ready` 时,`ready()` 返回 false 期间这条上下文不贡献任何内容。文本从不做变量插值,插件释放时这条上下文随之移除。文本见[模型体验](#model-experience)。 +`registerCliPathContext(ctx, cli)` 注册一条必需的运行时上下文,让模型按 `cli.executable` 运行 `cli.program`,这正是执行器运行的路径。传入 `scripts` 时,它还让模型只在运行 skill 脚本的命令中把可执行文件所在目录放到 PATH 最前面,并且不要按名称运行 `cli.program`。文本采用内置组合在 Host 平台上提供给模型的 shell 写法。在 Windows 以外的平台上使用 POSIX shell 语法:前缀是 `PATH="<可执行文件所在目录>:$PATH"`,只改变这一条命令的 PATH,文本还要求不要导出 PATH。在 Windows 上模型使用 PowerShell,文本用调用运算符写出可执行文件 `& '<可执行文件>'`,前缀写作 `$env:PATH = '<可执行文件所在目录>;' + $env:PATH`;PowerShell 每次调用都在新进程中运行,所以这个修改只在本次调用内有效。传入 `ready` 时,`ready()` 返回 false 期间这条上下文不贡献任何内容。文本从不做变量插值,插件释放时这条上下文随之移除。`executableFileName(program)` 给出插件要查找的文件名:`program`,在 Windows 上是 `program.exe`。文本见[模型体验](#model-experience)。 ### 命令、链接与失败 @@ -119,7 +120,7 @@ const account = new Account({ #### 模型看到什么 -每个调用 `registerCliPathContext` 且未传 `ready` 或 `ready` 返回 true 的插件,在上下文顺序 220 处有一条必需的运行时上下文;关闭可选运行时上下文时它仍然保留。第二行只在传入 `scripts` 时出现。占位符取自调用参数,引号内的值是 JSON 字符串。 +每个调用 `registerCliPathContext` 且未传 `ready` 或 `ready` 返回 true 的插件,在上下文顺序 220 处有一条必需的运行时上下文;关闭可选运行时上下文时它仍然保留。第二行只在传入 `scripts` 时出现。占位符取自调用参数。在 Windows 以外的平台上,引号内的值是 JSON 字符串;在 Windows 上是 PowerShell 单引号字符串,其中的单引号写成两个。 ##### 运行时上下文条目 @@ -128,6 +129,13 @@ const account = new Account({ Scripts in the skill call `` by name; prefix only the commands that run them with PATH=":$PATH", and never export PATH or run `` by name yourself. ``` +##### Windows 上的运行时上下文条目 + +```markdown + CLI: run `` by its absolute path with the call operator, & ''; the bare name may resolve to another installation or to none. +Scripts in the skill call `` by name; start only the commands that run them with $env:PATH = ';' + $env:PATH, and never run `` by name yourself. +``` + #### Token 影响 每个注册的插件在每份运行时上下文快照中占一到两行。路径变化,或这条上下文出现、消失时,追加一份快照。 @@ -144,6 +152,7 @@ Scripts in the skill call `` by name; prefix only the command - 授权主机同时匹配其子域名;插件无法把链接限制在一个确切的主机上。 - `exitFailure` 只按退出码分类;CLI 会输出结构化错误的插件,要自己编写按白名单取值的分类函数。 - 运行时上下文要求模型按绝对路径运行 CLI,但无法强制;按名称运行 CLI 的命令会运行 PATH 解析到的任意安装,或者失败。 +- 文本的 shell 写法按 Host 平台而不是实际挂载的 shell 工具选择:在 Windows 上给模型 POSIX shell,或在其他平台上给模型 PowerShell 的组合,得到的是另一个平台的写法。 ### 开发备注 diff --git a/packages/connector/cli-account/src/context.ts b/packages/connector/cli-account/src/context.ts index 402e697d84..3290c389f0 100644 --- a/packages/connector/cli-account/src/context.ts +++ b/packages/connector/cli-account/src/context.ts @@ -1,6 +1,6 @@ /** Runtime context that tells the model the absolute path of an app plugin's CLI. */ -import { dirname } from 'node:path' +import { posix, win32 } from 'node:path' import type { Context } from '@deepseek-ai/cordis' import type {} from '@deepseek-ai/dsh-system-prompt' @@ -20,22 +20,46 @@ export interface CliPathContext { readonly ready?: () => boolean } +/** + * Write the runtime-context text in the dialect of the shell that shipped compositions give the model on `platform`: + * PowerShell on Windows, where `tool-pwsh` replaces `tool-bash`, and a POSIX shell elsewhere. Paths are quoted as + * literals of that shell, so a path keeps its exact characters. + * @param cli - the CLI to describe. + * @param platform - the Host platform. + * @returns a line naming the absolute path, then, with `cli.scripts`, a line giving the PATH prefix for the commands that + * run the skill's scripts. + */ +export function cliPathContextText(cli: CliPathContext, platform: NodeJS.Platform): string { + const { product, program, executable } = cli + if (platform === 'win32') { + const lines = [`${product} CLI: run \`${program}\` by its absolute path with the call operator, & ${pwshLiteral(executable)}; the bare name may resolve to another installation or to none.`] + if (cli.scripts === true) { + lines.push(`Scripts in the ${product} skill call \`${program}\` by name; start only the commands that run them with $env:PATH = ${pwshLiteral(`${win32.dirname(executable)};`)} + $env:PATH, and never run \`${program}\` by name yourself.`) + } + return lines.join('\n') + } + const lines = [`${product} CLI: run \`${program}\` by its absolute path ${JSON.stringify(executable)}; the bare name may resolve to another installation or to none.`] + if (cli.scripts === true) { + lines.push(`Scripts in the ${product} skill call \`${program}\` by name; prefix only the commands that run them with PATH=${JSON.stringify(`${posix.dirname(executable)}:$PATH`)}, and never export PATH or run \`${program}\` by name yourself.`) + } + return lines.join('\n') +} + +/** A PowerShell single-quoted string, which expands nothing; an embedded quote is doubled. */ +function pwshLiteral(text: string): string { + return `'${text.replaceAll("'", "''")}'` +} + /** * Register required runtime context that tells the model to run the CLI by its absolute path, not by name, and, when - * the skill has scripts, to start each command that runs them with the executable's directory first on PATH. The text - * is never interpolated, so a path keeps its exact characters. + * the skill has scripts, to put the executable's directory first on PATH only in the commands that run them. The text + * uses the Host platform's shell dialect ({@link cliPathContextText}) and is never interpolated. * @param ctx - the plugin context, which injects `systemPrompt`; disposing it removes the context. * @param cli - the CLI to describe. * @returns the registration's effect disposer. */ export function registerCliPathContext(ctx: Context, cli: CliPathContext): () => void { - const lines = [ - `${cli.product} CLI: run \`${cli.program}\` by its absolute path ${JSON.stringify(cli.executable)}; the bare name may resolve to another installation or to none.`, - ] - if (cli.scripts === true) { - lines.push(`Scripts in the ${cli.product} skill call \`${cli.program}\` by name; prefix only the commands that run them with PATH=${JSON.stringify(`${dirname(cli.executable)}:$PATH`)}, and never export PATH or run \`${cli.program}\` by name yourself.`) - } - const text = lines.join('\n') + const text = cliPathContextText(cli, process.platform) const { ready } = cli return ctx.systemPrompt.context({ name: cli.name, diff --git a/packages/connector/cli-account/src/executor.ts b/packages/connector/cli-account/src/executor.ts index 7883667627..cf528dc479 100644 --- a/packages/connector/cli-account/src/executor.ts +++ b/packages/connector/cli-account/src/executor.ts @@ -12,6 +12,16 @@ const TERMINATE_GRACE_MS = 2_000 const DRAIN_AFTER_EXIT_MS = 2_000 const TIMEOUT_CODE = 'CLI_ACCOUNT_TIMEOUT' +/** + * Name a CLI's executable file as its packages install it on `platform`: with the `.exe` extension on Windows. + * @param program - the name the CLI's guides run it by, such as `lark-cli`. + * @param platform - the Host platform. + * @returns `program`, or `program.exe` on Windows. + */ +export function executableFileName(program: string, platform: NodeJS.Platform = process.platform): string { + return platform === 'win32' ? `${program}.exe` : program +} + /** The program a plugin runs and its deadline for commands that do not wait for the user. */ export interface CliCommandExecutorOptions { /** Name the CLI's guides run it by, used in diagnostics. */ diff --git a/packages/connector/cli-account/src/index.ts b/packages/connector/cli-account/src/index.ts index 7675defd31..d55dd0250d 100644 --- a/packages/connector/cli-account/src/index.ts +++ b/packages/connector/cli-account/src/index.ts @@ -9,7 +9,7 @@ import z from '@deepseek-ai/schemastery' import { MAX_TIMER_DELAY_MS } from '@deepseek-ai/dsh-timeout' export { registerCliPathContext, type CliPathContext } from './context.ts' -export { CliCommandExecutor, type CliCommandExecutorOptions, type CliCommandResult, type CliRunOptions } from './executor.ts' +export { CliCommandExecutor, executableFileName, type CliCommandExecutorOptions, type CliCommandResult, type CliRunOptions } from './executor.ts' export { CliAccountRuntime, exitFailure, type CliAccountRuntimeOptions, type CliOperationControl } from './runtime.ts' export type * from './types.ts' diff --git a/packages/connector/cli-account/tests/context.spec.ts b/packages/connector/cli-account/tests/context.spec.ts index 566277146e..350267d1eb 100644 --- a/packages/connector/cli-account/tests/context.spec.ts +++ b/packages/connector/cli-account/tests/context.spec.ts @@ -1,10 +1,12 @@ import { Context } from '@deepseek-ai/cordis' import SystemPrompt, { renderContextSections } from '@deepseek-ai/dsh-system-prompt' import { describe, expect, it, onTestFinished } from 'vitest' -import { registerCliPathContext, type CliPathContext } from '../src/context.ts' +import { cliPathContextText, registerCliPathContext, type CliPathContext } from '../src/context.ts' const CLI = { name: 'fixture:cli', product: 'Fixture', program: 'fixture-cli', executable: '/opt/fixture/bin/fixture-cli' } const RUN = 'Fixture CLI: run `fixture-cli` by its absolute path "/opt/fixture/bin/fixture-cli"; the bare name may resolve to another installation or to none.' +const WINDOWS_CLI = { ...CLI, executable: 'C:\\Users\\O\'Brien\\.dsh\\fixture\\fixture-cli.exe' } +const WINDOWS_RUN = 'Fixture CLI: run `fixture-cli` by its absolute path with the call operator, & \'C:\\Users\\O\'\'Brien\\.dsh\\fixture\\fixture-cli.exe\'; the bare name may resolve to another installation or to none.' /** A prompt registry with optional runtime context off, so only required context renders. */ async function requiredContextOnly(): Promise { @@ -23,12 +25,34 @@ async function rendered(ctx: Context) { return renderContextSections(await ctx.systemPrompt.assemble()) } +describe('cliPathContextText', () => { + it('names the executable by its absolute path as a JSON string for a POSIX shell', () => { + expect(cliPathContextText(CLI, 'darwin')).toBe(RUN) + }) + + it('gives the commands that run the skill scripts the executable directory first on PATH for a POSIX shell', () => { + expect(cliPathContextText({ ...CLI, scripts: true }, 'linux')).toBe( + `${RUN}\nScripts in the Fixture skill call \`fixture-cli\` by name; prefix only the commands that run them with PATH="/opt/fixture/bin:$PATH", and never export PATH or run \`fixture-cli\` by name yourself.`, + ) + }) + + it('runs the executable with the PowerShell call operator and a literal path on Windows', () => { + expect(cliPathContextText(WINDOWS_CLI, 'win32')).toBe(WINDOWS_RUN) + }) + + it('gives the commands that run the skill scripts the executable directory first on $env:PATH on Windows', () => { + expect(cliPathContextText({ ...WINDOWS_CLI, scripts: true }, 'win32')).toBe( + `${WINDOWS_RUN}\nScripts in the Fixture skill call \`fixture-cli\` by name; start only the commands that run them with $env:PATH = 'C:\\Users\\O''Brien\\.dsh\\fixture;' + $env:PATH, and never run \`fixture-cli\` by name yourself.`, + ) + }) +}) + describe('registerCliPathContext', () => { - it('names the executable by its absolute path in required context until its plugin is disposed', async () => { + it('names the executable in the Host shell dialect in required context until its plugin is disposed', async () => { const ctx = await requiredContextOnly() const fiber = await register(ctx, CLI) - expect(await rendered(ctx)).toEqual([{ name: 'fixture:cli', text: RUN }]) + expect(await rendered(ctx)).toEqual([{ name: 'fixture:cli', text: cliPathContextText(CLI, process.platform) }]) await fiber.dispose() expect(await rendered(ctx)).toEqual([]) }) @@ -37,26 +61,16 @@ describe('registerCliPathContext', () => { const ctx = await requiredContextOnly() await register(ctx, { ...CLI, executable: '/opt/{{tools}}/fixture-cli' }) - expect((await rendered(ctx))[0]?.text).toContain('"/opt/{{tools}}/fixture-cli"') - }) - - it('gives commands that run the skill scripts the executable directory first on PATH', async () => { - const ctx = await requiredContextOnly() - await register(ctx, { ...CLI, scripts: true }) - - expect(await rendered(ctx)).toEqual([{ - name: 'fixture:cli', - text: `${RUN}\nScripts in the Fixture skill call \`fixture-cli\` by name; prefix only the commands that run them with PATH="/opt/fixture/bin:$PATH", and never export PATH or run \`fixture-cli\` by name yourself.`, - }]) + expect((await rendered(ctx))[0]?.text).toContain('/opt/{{tools}}/fixture-cli') }) it('contributes nothing while the executable is not ready', async () => { const ctx = await requiredContextOnly() let ready = false - await register(ctx, { ...CLI, ready: () => ready }) + await register(ctx, { ...CLI, scripts: true, ready: () => ready }) expect(await rendered(ctx)).toEqual([]) ready = true - expect(await rendered(ctx)).toEqual([{ name: 'fixture:cli', text: RUN }]) + expect(await rendered(ctx)).toEqual([{ name: 'fixture:cli', text: cliPathContextText({ ...CLI, scripts: true }, process.platform) }]) }) }) diff --git a/packages/connector/cli-account/tests/executor.spec.ts b/packages/connector/cli-account/tests/executor.spec.ts index f75e6e3053..2339e423aa 100644 --- a/packages/connector/cli-account/tests/executor.spec.ts +++ b/packages/connector/cli-account/tests/executor.spec.ts @@ -1,7 +1,7 @@ import { homedir } from 'node:os' import { Context } from '@deepseek-ai/cordis' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' -import { CliCommandExecutor } from '../src/executor.ts' +import { CliCommandExecutor, executableFileName } from '../src/executor.ts' import { EXECUTABLE, PROGRAM, ScriptedSubprocess, type ScriptedProcess } from './harness.ts' const MAX_OUTPUT_BYTES = 1_048_576 @@ -17,6 +17,14 @@ async function writeStderr(process: ScriptedProcess, chunks: readonly string[]): process.reply({ stderr: chunks.at(-1) as string }) } +describe('executableFileName', () => { + it('adds the .exe extension on Windows only', () => { + expect(executableFileName('fixture-cli', 'win32')).toBe('fixture-cli.exe') + expect(executableFileName('fixture-cli', 'darwin')).toBe('fixture-cli') + expect(executableFileName('fixture-cli')).toBe(process.platform === 'win32' ? 'fixture-cli.exe' : 'fixture-cli') + }) +}) + describe('CliCommandExecutor', () => { let ctx: Context let subprocess: ScriptedSubprocess