NocoBase CLI 命令详解:`nb session remove` 清理 NB_SESSION_ID 的 Shell 与 Agent 集成
2026/9/13 19:07:34 网站建设 项目流程

NocoBase CLI 命令详解:nb session remove清理 NB_SESSION_ID 的 Shell 与 Agent 集成

【免费下载链接】nocobaseNocoBase is an open-source AI + no-code platform for building business systems fast. Instead of generating everything from scratch, AI works on top of production-proven infrastructure and a WYSIWYG no-code interface, so you get both speed and reliability.项目地址: https://gitcode.com/GitHub_Trending/no/nocobase

导读

nb session remove是 NocoBase CLI 提供的 session 集成清理命令,用于移除之前由nb session setup写入的 shell 初始化配置(profile 片段、托管文件、cmd AutoRun),并在检测到 opencode 插件配置时一并删除对应的 agent 集成。本文将以官方文档为主体,结合packages/core/cli下的命令实现与单元测试,完整讲解该命令的用法、参数、执行流程、清理范围与典型场景,帮助你安全、干净地卸载NB_SESSION_ID相关的环境集成。

命令背景:什么是 NB_SESSION_ID 集成

在 NocoBase CLI(nb)中,NB_SESSION_ID是一个用于标识当前 shell / runtime 会话的环境变量。它让 CLI 可以把会话级的上下文(例如当前选中的 env 环境)与具体的一次终端会话绑定起来,而不是全局共享。相关实现位于 session-store.ts,其中getSessionId()直接读取process.env.NB_SESSION_ID,会话状态则以${sessionId}.json的形式存放在 CLI home 目录的sessions/下。

为了让每个新打开的 shell 会话都能自动注入一个独立的NB_SESSION_IDnb session setup会写入对应的初始化文件;而nb session remove则负责把这些写入的内容精确、可逆地清理干净。

nb session remove命令参考

作用与定位

nb session remove会清理之前由nb session setup写入的 shell 配置;如果检测到 opencode 插件配置,也会一起移除对应的集成。

它在 session 命令族中的位置如下:

  • nb session setup:安装 shell / runtime 集成,让新会话自动注入NB_SESSION_ID
  • nb session id:显示当前生效的 session id;
  • nb session remove:移除 session 集成(本文主题)。

用法

nb session remove [flags]

参数

参数类型说明
--shellstring指定目标 shell,支持bashzshfishpowershellcmd

如果不传--shell,命令会尝试自动探测当前 shell(见下文“shell 自动探测”)。该参数的定义在 remove.ts 中,通过 oclif 的Flags.string声明,options限定为上述五种取值,传入其他值会直接报参数错误。

示例

# 自动探测当前 shell 并移除对应集成 nb session remove # 明确指定 zsh,移除 zsh 的 session 集成 nb session remove --shell zsh

执行流程与清理范围

从 remove.ts 可以看到命令的完整执行链路:

  1. 解析--shell参数;若未指定,调用detectSessionShell()自动探测;
  2. 若探测不到任何 shell,输出错误并提示Re-run with --shell bash|zsh|fish|powershell|cmd
  3. 调用removeSessionIntegration(shell)执行实际清理(核心逻辑位于 session-integration.ts);
  4. 按结果逐项输出日志,包括 profile 更新、托管文件删除、cmd AutoRun 更新、opencode 配置更新与插件删除等。

removeSessionIntegration的清理范围可以归纳为三个层面:

1. 托管文件(managed file)

nb session setup会把生成 session 的初始化脚本写入 CLI home 目录下的shell/文件夹,文件名按 shell 区分(见 managedFilePath):

  • bash:session.bash
  • zsh:session.zsh
  • fish:session.fish
  • powershell:session.ps1
  • cmd:nb.cmd

nb session remove会直接删除对应的托管文件(fs.rm(managedFile, { force: true }))。注意:删除的是这个由 CLI 生成并管理的文件本身,而不是整个shell/目录。

2. shell profile 中的标记块

对于 bash、zsh、fish、powershell,setup会在 profile 文件中写入一个带标记的代码块,remove会精确删除该标记块而不触碰用户的其他配置。

标记块由一对固定注释界定(见 session-integration.ts):

# >>> nocobase nb session >>> ... 集成片段 ... # <<< nocobase nb session <<<

删除时使用正则START_MARKER[\s\S]*?END_MARKER匹配整块内容并移除(见 removeMarkedBlock),同时会把连续的多余空行压缩为单个空行,保证 profile 文件本身整洁。各 shell 对应的 profile 路径由 getSessionShellProfilePaths 定义:

shellprofile 路径
bash~/.bashrc
zsh~/.zshrc
fish~/.config/fish/config.fish
powershellWindows 下同时处理Documents/PowerShell/Microsoft.PowerShell_profile.ps1Documents/WindowsPowerShell/Microsoft.PowerShell_profile.ps1;非 Windows 为~/.config/powershell/Microsoft.PowerShell_profile.ps1
cmd无 profile(改走 AutoRun,见下)

3. cmd AutoRun 与 opencode agent 集成

  • cmd:由于 cmd.exe 没有类似.bashrc的 profile 文件,setup通过注册表HKCU\Software\Microsoft\Command Processor下的AutoRun值(REG_SZ)注入if exist "<managedFile>" call "<managedFile>"片段。remove时先读取当前 AutoRun 值,仅移除属于自己的那一段,若移除后值为空则直接删除该注册表项(见 removeCmdAutoRun 与 removeCmdAutoRunSegment),避免误伤 AutoRun 中的其他命令。
  • opencode agent:若~/.config/opencode/存在,setup会写入插件文件plugins/nb-agent-session.js并在opencode.jsonplugin数组中注册。remove时删除插件文件,并把该插件从opencode.jsonplugin列表中过滤掉(见 removeOpencodeSessionPlugin)。

输出信息解读

命令执行后按结果输出对应日志(见 remove.ts):

Session integration removed for zsh. Profile updated: /home/user/.zshrc Managed file removed: /home/user/.nocobase/shell/session.zsh

各类输出含义:

  • Session integration removed for <shell>.:基础确认信息;
  • Profile updated: <file>:标记块已从对应 profile 中移除;
  • Managed file removed: <file>:CLI 管理的托管初始化文件已被删除;
  • cmd AutoRun updated: <location>:cmd 的 AutoRun 已更新(location 通常为注册表键路径);
  • Opencode config updated: <file>:opencode 配置中已移除插件注册;
  • Opencode agent plugin removed: <file>:opencode 插件文件已被删除。

shell 自动探测机制

不传--shell时,命令通过 detectSessionShell 自动识别当前 shell,探测顺序为:

  1. 检查环境变量FISH_VERSIONZSH_VERSIONBASH_VERSION(在对应 shell 中这些变量会被自动导出);
  2. 解析SHELL/LOGINSHELL环境变量(兼容fish.exepwshgit-bash等变体);
  3. Windows 下通过powershell.exe查询父进程链(Get-CimInstance Win32_Process,最多回溯 6 层)识别启动nb的终端类型,并综合MSYSTEMComSpecPROMPTPSModulePath等信号判定。

探测失败时命令会报错退出,提示显式指定--shell,这保证了清理操作的目标 shell 始终是明确的。

典型使用场景

场景一:卸载单条 shell 集成

nb session setup # 此前在默认 shell(如 bash)中安装过集成 nb session remove # 移除 bash 集成

场景二:清理非默认 shell

nb session remove --shell zsh nb session remove --shell powershell nb session remove --shell fish nb session remove --shell cmd

场景三:同时清理 opencode agent 集成

若此前setup时本机已存在~/.config/opencode/remove会一并清理nb-agent-session.js插件与opencode.json中的注册项,无需手动编辑配置文件。

相关命令

  • nb session setup:安装NB_SESSION_ID的 shell / runtime 集成;
  • nb session id:显示当前生效的NB_SESSION_ID,未配置时会提示先执行nb session setup并重开会话。

注意事项

  • nb session remove只清理由 CLI 管理、带标记的内容(托管文件、标记块、AutoRun 片段、opencode 插件),不会删除NB_SESSION_ID变量本身或用户手动写入的其他配置;
  • 移除集成不会影响当前已运行的 shell 会话,需要重新打开新会话后变更才完全生效;
  • 如果希望保留 CLI 会话级状态(如sessions/下的 session 状态文件),这些文件由 session-store.ts 独立管理,不在nb session remove的清理范围内;
  • 相关行为已由单元测试覆盖,例如 session-integration.test.ts 中的removeSessionIntegration removes the managed shell block from bash profileremoveSessionIntegration removes only the managed cmd AutoRun segment等用例,可对照验证清理逻辑的精确性。

【免费下载链接】nocobaseNocoBase is an open-source AI + no-code platform for building business systems fast. Instead of generating everything from scratch, AI works on top of production-proven infrastructure and a WYSIWYG no-code interface, so you get both speed and reliability.项目地址: https://gitcode.com/GitHub_Trending/no/nocobase

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询