NocoBase CLI 命令详解:`nb session remove` 清理 NB_SESSION_ID 的 Shell 与 Agent 集成 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 CLInb中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_IDnb session id显示当前生效的 session idnb session remove移除 session 集成本文主题。用法nb session remove [flags]参数参数类型说明--shellstring指定目标 shell支持bash、zsh、fish、powershell、cmd如果不传--shell命令会尝试自动探测当前 shell见下文“shell 自动探测”。该参数的定义在 remove.ts 中通过 oclif 的Flags.string声明options限定为上述五种取值传入其他值会直接报参数错误。示例# 自动探测当前 shell 并移除对应集成 nb session remove # 明确指定 zsh移除 zsh 的 session 集成 nb session remove --shell zsh执行流程与清理范围从 remove.ts 可以看到命令的完整执行链路解析--shell参数若未指定调用detectSessionShell()自动探测若探测不到任何 shell输出错误并提示Re-run with --shell bash|zsh|fish|powershell|cmd调用removeSessionIntegration(shell)执行实际清理核心逻辑位于 session-integration.ts按结果逐项输出日志包括 profile 更新、托管文件删除、cmd AutoRun 更新、opencode 配置更新与插件删除等。removeSessionIntegration的清理范围可以归纳为三个层面1. 托管文件managed filenb session setup会把生成 session 的初始化脚本写入 CLI home 目录下的shell/文件夹文件名按 shell 区分见 managedFilePathbashsession.bashzshsession.zshfishsession.fishpowershellsession.ps1cmdnb.cmdnb session remove会直接删除对应的托管文件fs.rm(managedFile, { force: true })。注意删除的是这个由 CLI 生成并管理的文件本身而不是整个shell/目录。2. shell profile 中的标记块对于 bash、zsh、fish、powershellsetup会在 profile 文件中写入一个带标记的代码块remove会精确删除该标记块而不触碰用户的其他配置。标记块由一对固定注释界定见 session-integration.ts# nocobase nb session ... 集成片段 ... # nocobase nb session 删除时使用正则START_MARKER[\s\S]*?END_MARKER匹配整块内容并移除见 removeMarkedBlock同时会把连续的多余空行压缩为单个空行保证 profile 文件本身整洁。各 shell 对应的 profile 路径由 getSessionShellProfilePaths 定义shellprofile 路径bash~/.bashrczsh~/.zshrcfish~/.config/fish/config.fishpowershellWindows 下同时处理Documents/PowerShell/Microsoft.PowerShell_profile.ps1与Documents/WindowsPowerShell/Microsoft.PowerShell_profile.ps1非 Windows 为~/.config/powershell/Microsoft.PowerShell_profile.ps1cmd无 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.json的plugin数组中注册。remove时删除插件文件并把该插件从opencode.json的plugin列表中过滤掉见 removeOpencodeSessionPlugin。输出信息解读命令执行后按结果输出对应日志见 remove.tsSession 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: fileCLI 管理的托管初始化文件已被删除cmd AutoRun updated: locationcmd 的 AutoRun 已更新location 通常为注册表键路径Opencode config updated: fileopencode 配置中已移除插件注册Opencode agent plugin removed: fileopencode 插件文件已被删除。shell 自动探测机制不传--shell时命令通过 detectSessionShell 自动识别当前 shell探测顺序为检查环境变量FISH_VERSION、ZSH_VERSION、BASH_VERSION在对应 shell 中这些变量会被自动导出解析SHELL/LOGINSHELL环境变量兼容fish.exe、pwsh、git-bash等变体Windows 下通过powershell.exe查询父进程链Get-CimInstance Win32_Process最多回溯 6 层识别启动nb的终端类型并综合MSYSTEM、ComSpec、PROMPT、PSModulePath等信号判定。探测失败时命令会报错退出提示显式指定--shell这保证了清理操作的目标 shell 始终是明确的。典型使用场景场景一卸载单条 shell 集成nb session setup # 此前在默认 shell如 bash中安装过集成 nb session remove # 移除 bash 集成场景二清理非默认 shellnb 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 profile、removeSessionIntegration 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),仅供参考