@mortiseai/stem 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,190 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for describing the origin of the Work and
141
+ reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Support. While redistributing the Work or
166
+ Derivative Works thereof, You may choose to offer, and charge a
167
+ fee for, acceptance of support, warranty, indemnity, or other
168
+ liability obligations and/or rights consistent with this License.
169
+ However, in accepting such obligations, You may act only on Your
170
+ own behalf and on Your sole responsibility, not on behalf of any
171
+ other Contributor, and only if You agree to indemnify, defend,
172
+ and hold each Contributor harmless for any liability incurred by,
173
+ or claims asserted against, such Contributor by reason of your
174
+ accepting any such warranty or support.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ Copyright 2025 MortiseAI
179
+
180
+ Licensed under the Apache License, Version 2.0 (the "License");
181
+ you may not use this file except in compliance with the License.
182
+ You may obtain a copy of the License at
183
+
184
+ http://www.apache.org/licenses/LICENSE-2.0
185
+
186
+ Unless required by applicable law or agreed to in writing, software
187
+ distributed under the License is distributed on an "AS IS" BASIS,
188
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189
+ See the License for the specific language governing permissions and
190
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,328 @@
1
+ # @mortiseai/stem
2
+
3
+ > **Stem** — MortiseAI 构建的 Self-Evolving AI Agent,基于 **MSCE**(MSC Engine)的自我进化智能体
4
+
5
+ 核心三种业务模式(详见[「业务模式」](#业务模式--agent----coding----fde)):
6
+
7
+ - **Agent 模式** — 面向通用智能体形态,默认模式,当前唯一开放。启动:`stem` / `stem --agent`;核心能力:业务图谱记忆
8
+ - **Coding 模式** — 面向编码工作流的形态(暂未开放)。启动:`stem --coding`;核心能力:业务图谱记忆,基于 MortiseAI Spec Code Engine 编码
9
+ - **FDE 模式** — 面向企业交付场景的形态(暂未开放)。启动:`stem --fde`;核心能力:业务图谱记忆,私有图谱记忆,基于 MortiseAI Spec Code Engine 编码
10
+
11
+ ---
12
+
13
+ ## 项目状态
14
+
15
+ **Phase 0 · 脚手架骨架**(当前阶段)
16
+ - ✅ 项目结构、构建链、NPM 发布配置就绪
17
+ - ✅ `stem` 可直接启动,终端渲染 Welcome 屏
18
+ - ⬜ MSC Engine 集成(Phase 1+)
19
+ - ⬜ Claude API / 工具 / 命令 / 远程 / 多 Agent(Phase 1-5)
20
+
21
+ 架构设计与完整路线图见 [`ARCHITECTURE.md`](./ARCHITECTURE.md)。
22
+
23
+ ---
24
+
25
+ ## 快速开始
26
+
27
+ ### 前置依赖
28
+ - Node.js >= 22
29
+ - Bun https://bun.sh/ 开发模式 & 构建需要
30
+
31
+ ### npm 安装(发布后)
32
+ ```bash
33
+ npm install -g @mortiseai/stem
34
+ stem
35
+ ```
36
+ macOS / Linux / Windows 均可,仅需 Node >= 22,无需 bun。
37
+
38
+ ### 开发模式
39
+ ```bash
40
+ bun install
41
+ bun run dev # bun 直跑源码
42
+ STEM_DEV=1 ./bin/stem.mjs # 等效:强制启动器走源码(忽略已构建的 dist)
43
+ ```
44
+
45
+ ### 构建并运行
46
+ ```bash
47
+ bun run build
48
+ ./dist/cli.mjs
49
+ # 或通过 Node 启动器(自动选 dist,并加载仓库根 .env)
50
+ ./bin/stem.mjs
51
+ ```
52
+
53
+ ### 退出
54
+ 在 Welcome 屏按 **q** · **Esc** · 或 **Ctrl+C**。
55
+
56
+ ---
57
+
58
+ ## 启动方式与命令行参数
59
+
60
+ ### 常用启动示例
61
+
62
+ ```bash
63
+ stem # 默认:agent 模式 · llms.json default 模型 · 系统语言
64
+ stem --lang en # 英文界面
65
+ stem --model BaiLian:glm-5.2 # 会话默认模型(platform:model 完整引用)
66
+ stem --model glm-5.2 # 裸模型 id(目录唯一命中,否则落默认平台)
67
+ stem --lang ja --model BaiLian:glm-5.2 # 组合使用
68
+ stem --resume <sessionId> # 恢复历史会话(续写同一份会话日志)
69
+ echo "一句话问答" | stem --bare # SIMPLE 模式:stdin → 单次回答(non-TTY)
70
+ ```
71
+
72
+ ### 运行形态总览
73
+
74
+ | 形态 | 启动方式 | 说明 |
75
+ |---|---|---|
76
+ | TUI 交互(默认) | `stem` | Welcome + Repl 全功能终端界面 |
77
+ | SIMPLE | `stem --bare` | TTY 下裸 prompt 简化 REPL;non-TTY + stdin pipe 走单次问答链路 |
78
+ | 无头(SDK 桥) | `stem --sdk-url ws://…` | 不渲染 TUI,WebSocket NDJSON 全双工驱动,见下节 |
79
+ | WS telemetry tap | `stem --telemetry-url ws://…` | TUI 正常交互,遥测帧单向推给外部 WS 观察端,见下节 |
80
+ | MCP stdio server | `./dist/mcp.mjs` | 把 stem 工具集暴露为 MCP server(stdio JSON-RPC),供其他 LLM 客户端接入 |
81
+ | 后台 daemon | `./dist/daemon.mjs [run\|status]` | 无 UI 常驻容器;`status` 输出一行 JSON 状态;SIGTERM/SIGINT 优雅退出 |
82
+
83
+ MCP / daemon 是独立构建产物(`bun run build:mcp` / `bun run build:daemon`),与 TUI 共用同一 MSC 容器逻辑。
84
+
85
+ ### 无头模式(headless SDK 桥)· `--sdk-url`
86
+
87
+ 被 sidecar server(FastAPI / desktop server 等)spawn,协议为 **WebSocket 上的 NDJSON**(每帧单行 JSON),与 Claude Code SDK stream-json 消息同构;内部与 TUI 走完全相同的 MSC agent loop:
88
+
89
+ ```bash
90
+ stem --sdk-url ws://127.0.0.1:8123/ws/<sid> # headless 全双工
91
+ stem --sdk-url ws://127.0.0.1:8123/ws/<sid> --sdk-telemetry # 追加 telemetry 观测帧(默认关)
92
+ ```
93
+
94
+ - 下行帧:`system`(init:model / tools / slash_commands / workflows / permissionMode)、`stream_event`(Anthropic streaming 同构)、`result`、`control_request`(权限卡)、`command_result` / `workflow_result` / `telemetry_event`
95
+ - 上行帧:`user`(消息,入队串行)、`control_request`(interrupt / set_permission_mode)、`control_response`(权限裁决)、`command`(slash 命令)、`workflow`(直接触发 workflow,不经 LLM)
96
+ - 会话生命周期 = 连接生命周期:ws 断开 → 容器销毁 + `exit 0`
97
+ - `--sdk-telemetry`(或 env `STEM_TELEMETRY=1`)开启八源观测帧(tool/hook/mcp/subagent/workflow/skill/command/plugin)
98
+
99
+ 人工驱动与协议细节:`bun scripts/ws-manual-server.ts --spawn`(交互式驱动器)· 协议文档 [`docs/sdk-telemetry-protocol.md`](./docs/sdk-telemetry-protocol.md) · 全量测试手册 [`stem_cli_test/ws/Stem-WS.md`](./stem_cli_test/ws/Stem-WS.md)。
100
+
101
+ ### WebSocket telemetry tap · `--telemetry-url`
102
+
103
+ 与无头模式不同,tap 是 **TUI 共存**的单向观测通道:TUI 正常渲染交互,telemetry 帧单向推给外部 WS 观察端;忽略一切下行帧,断开只记日志、不影响 TUI、不重连。env `STEM_TELEMETRY_WS` 兜底。
104
+
105
+ ```bash
106
+ stem --telemetry-url ws://127.0.0.1:8123/ws/manual
107
+ ```
108
+
109
+ ### 语言 · `--lang`
110
+
111
+ `--lang <zh|en|ja|ko>`(或 `--lang=zh`)。缺省时按优先级回退:
112
+
113
+ 1. `--lang` flag
114
+ 2. env `STEM_DEF_LANGUAGE`(shell export 或 `.mstem/settings.local.json` 的 `env` 段,见「运行时开关」)
115
+ 3. POSIX env `LC_ALL` / `LC_MESSAGES` / `LANG`(前缀匹配)
116
+ 4. 默认 `zh`
117
+
118
+ ### 模型 · `--model`
119
+
120
+ `--model <ref>`(或 `--model=<ref>`)设置**会话级默认模型**,不落盘:
121
+
122
+ - ref 形式:`platform:model`(如 `BaiLian:glm-5.2`,平台名做存在性校验,打错立即报错退出并列出已知平台)或裸模型 id(目录唯一命中,否则落默认平台,不做白名单)
123
+ - 平台与模型目录来自 `.mstem/llms.json`(见下节)
124
+ - 解析优先级:`/fast` 开启 > **`--model` 启动参数** > `/model` 会话内持久设置 > env `STEM_DEF_LLM_MODEL` > `llms.json` `default` > 首平台首模型
125
+ - 会话内用 `/model` 显式切换/重置会覆盖启动参数;`/model current` 可查看当前模型与来源
126
+
127
+ ### 模型目录 · `.mstem/llms.json`
128
+
129
+ 多平台 LLM 目录,搜索顺序:`$STEM_LLMS_PATH` → `<cwd>/.mstem/llms.json` → `~/.mstem/llms.json`。
130
+
131
+ - `protocol`:`anthropic`(`/v1/messages`)或 `openai`(`/chat/completions`)
132
+ - `apiKeyEnv`:密钥所在 env 变量名,缺省 `STEM_LLM_<平台大写>_API_KEY`;也可写 `apiKey` 字段(值为全大写下划线视为 env 变量名,否则视为密钥字面量)
133
+ - `authHeader`:缺省 anthropic → `x-api-key`、openai → `bearer`
134
+ - `context`:支持 `'200k'` / `'1M'` / 数字;`pricing` 单位 USD/1M tokens(供 `/cost` 分账)
135
+ - `vision`:该模型是否支持图片理解(布尔);未声明视为能力未知
136
+ - `default`:全局默认模型;`roles.fast`:`/fast` 快速模式用的模型
137
+
138
+ 完整示例:
139
+
140
+ ```json
141
+ {
142
+ "platforms": {
143
+ "anthropic": {
144
+ "protocol": "anthropic",
145
+ "baseUrl": "https://api.anthropic.com",
146
+ "apiKeyEnv": "STEM_LLM_ANTHROPIC_API_KEY",
147
+ "models": {
148
+ "claude-sonnet-4-6": { "context": "200k" },
149
+ "claude-haiku-4-5": { "context": "200k" }
150
+ }
151
+ },
152
+ "bailian": {
153
+ "protocol": "anthropic",
154
+ "baseUrl": "https://token-plan.cn-beijing.maas.aliyuncs.com/apps/anthropic",
155
+ "apiKeyEnv": "STEM_LLM_BAILIAN_API_KEY",
156
+ "authHeader": "bearer",
157
+ "models": {
158
+ "qwen3.8-max-preview": {
159
+ "context": "1M",
160
+ "vision": true,
161
+ "pricing": { "input": 1.3, "output": 7.8, "cacheRead": 0.26, "cacheWrite": 0 }
162
+ }
163
+ }
164
+ },
165
+ "minimax": {
166
+ "protocol": "anthropic",
167
+ "baseUrl": "https://api.minimax.io/anthropic",
168
+ "apiKeyEnv": "STEM_LLM_MINIMAX_API_KEY",
169
+ "authHeader": "bearer",
170
+ "models": {
171
+ "MiniMax-M2.7": { "context": "200k" },
172
+ "MiniMax-M2.7-highspeed": { "context": "200k" }
173
+ }
174
+ },
175
+ "deepseek": {
176
+ "protocol": "openai",
177
+ "baseUrl": "https://api.deepseek.com",
178
+ "apiKeyEnv": "STEM_LLM_DEEPSEEK_API_KEY",
179
+ "models": {
180
+ "deepseek-chat": { "context": "128k" }
181
+ }
182
+ }
183
+ },
184
+ "default": "bailian:qwen3.8-max-preview",
185
+ "roles": {
186
+ "fast": "minimax:MiniMax-M2.7-highspeed"
187
+ }
188
+ }
189
+ ```
190
+
191
+ ### 业务模式 · `--agent` / `--coding` / `--fde`
192
+
193
+ Stem 的三种核心业务模式:
194
+
195
+ | 模式 | 启动命令 | 状态 | 说明 | 核心能力 |
196
+ |---|---|---|---|---|
197
+ | Agent | `stem` / `stem --agent` | ✅ 开放(默认) | 面向通用智能体形态 | 业务图谱记忆 |
198
+ | Coding | `stem --coding` | ⬜ 暂未开放 | 面向编码工作流的形态 | 业务图谱记忆 · 基于 MortiseAI Spec Code Engine 编码 |
199
+ | FDE | `stem --fde` | ⬜ 暂未开放 | 面向企业交付场景的形态 | 业务图谱记忆 · 私有图谱记忆 · 基于 MortiseAI Spec Code Engine 编码 |
200
+
201
+ 三选一布尔开关,缺省回退 env `STEM_DEF_MODE`,默认 `agent`。显式请求 `--coding` / `--fde` 会拒绝启动并提示(不做静默回退)。
202
+
203
+ ### 会话 · `--resume`
204
+
205
+ `--resume <id>` / `-r <id>` / `--resume=<id>`:恢复指定会话(退出提示里有 `--resume <id>` 可直接复制),复用原会话日志文件续写。
206
+
207
+ ### 图片粘贴 · 图文混合理解(macOS)
208
+
209
+ 对齐 Claude Code:剪贴板里有图片(如 Cmd+Shift+Ctrl+4 截图)时按 **Ctrl+V**,输入框插入 `[Image #N]` 占位;提交后文本与图片按占位位置交错组成混合消息发给模型。模型也可以直接 `Read` 图片文件(png/jpg/gif/webp)"看到"内容。
210
+
211
+ - 依赖模型能力位:`llms.json` 模型条目的 `vision` 字段。`vision: false` 的模型粘贴时直接拦截提示;粘贴后切到不支持的模型,发送前自动剥离图片并提示「已省略 N 张图片」。
212
+ - 剪贴板读取优先用 `pngpaste`(brew 可选安装,更快),否则走系统 `osascript` — 首次调用可能弹 macOS 自动化权限框,拒绝后图片粘贴不可用(等同剪贴板无图)。
213
+ - 超限图片(>5MB 或长边 >8000px)自动用系统 `sips` 缩放到长边 1568px。
214
+ - 已知限制:`--resume` 恢复的历史会话中图片不回灌,模型只见 `[Image #N]` 字面。
215
+
216
+ ### 其他参数
217
+
218
+ | 参数 | 说明 |
219
+ |---|---|
220
+ | `--bare` | SIMPLE 模式(见「运行形态总览」) |
221
+ | `--plugin-dir <dir>` | 会话级插件根,可重复传入(相对路径按真实调用目录解析) |
222
+ | `--sdk-url` / `--sdk-telemetry` / `--telemetry-url` | 无头 SDK 桥 / 观测帧 / TUI tap(见上文两节) |
223
+ | `--worktree`/`-w` + `--tmux` | git worktree + tmux 并排会话(需 git 仓库) |
224
+ | `--update` / `--upgrade` | 自更新 |
225
+ | `--version`/`-v` · `--help`/`-h` | 版本 / 完整参数帮助 |
226
+
227
+ 完整参数清单以 `stem --help` 输出为准。
228
+
229
+ ### 运行时开关 · `.mstem/settings.local.json` 的 `env` 段
230
+
231
+ CLI 的全部运行时开关统一放 settings 级联的 **`env` 段**(不再使用 `.env`):
232
+ 入口启动时把 `env` 对象逐 key 注入 `process.env`,**仅补缺不覆盖** — shell
233
+ 显式 `export` 的同名变量仍最优先(CI/测试钩子用,如 `STEM_LLMS_PATH` /
234
+ `STEM_SETTINGS_PATH`)。
235
+
236
+ 层级(高层同 key 覆盖低层):
237
+
238
+ 1. `<cwd>/.mstem/settings.local.json` — 个人项目级(应 gitignore)
239
+ 2. `<cwd>/.mstem/settings.json` — 团队项目级(随仓库提交)
240
+ 3. `~/.mstem.json` — 用户全局
241
+
242
+ 示例 `.mstem/settings.local.json`:
243
+
244
+ ```json
245
+ {
246
+ "env": {
247
+ "STEM_DEF_LANGUAGE": "zh",
248
+ "STEM_API_RETRY_INTERVAL_MS": "3000",
249
+ "STEM_DEBUG": "1",
250
+ "STEM_LOOP_CRON_ENABLED": "1"
251
+ }
252
+ }
253
+ ```
254
+
255
+ 常用开关一览(全部可放 `env` 段,也可 shell export 临时覆盖):
256
+
257
+ | 变量 | 作用 |
258
+ |---|---|
259
+ | `STEM_DEF_LANGUAGE` | 界面语言 `zh\|en\|ja\|ko`(`--lang` 的兜底) |
260
+ | `STEM_DEF_MODE` | 业务模式(`--agent` 等的兜底) |
261
+ | `STEM_DEF_LLM_MODEL` | 模型引用(优先级低于 `--model` 与 `/model`) |
262
+ | `STEM_DEBUG` | `1` = 渲染 tool-use/tool-result 调试卡片 + Debug banner |
263
+ | `STEM_API_RETRY_INTERVAL_MS` | LLM 网络错误重试间隔(默认 60000,最多 10 次) |
264
+ | `STEM_API_TIMEOUT_MS` | LLM 请求全局超时(平台未配 `timeoutMs` 时生效,默认 600000) |
265
+ | `STEM_PERMISSION_MODE` | 工具权限模式 `default\|allow\|plan\|bypassPermissions\|...` |
266
+ | `STEM_LOOP_CRON_ENABLED` | `1` = 开启 `/loop` 后台 cron(默认关,opt-in 安全门) |
267
+ | `STEM_HUD_SHOW_USAGE` / `STEM_HUD_SHOW_WEEK` | HUD 用量行 / 本周行显示开关 |
268
+ | `STEM_WEBSEARCH_ENABLED` / `STEM_WEBSEARCH_PROVIDER` | web_search 总开关 / provider(`auto\|duckduckgo\|brave-web`) |
269
+ | `STEM_TELEMETRY` | `1` = 无头模式开启 telemetry 观测帧(`--sdk-telemetry` 的 env 形式) |
270
+ | `STEM_TELEMETRY_WS` | telemetry WS tap 地址 |
271
+
272
+ ---
273
+
274
+ ## 发布到 NPM
275
+
276
+ ```bash
277
+ bun run build # 生成 dist/cli.mjs
278
+ npm pack # 本地产出 tarball,检查清单
279
+ # → tar -tf *.tgz 应含 bin/ dist/ preload.ts README.md LICENSE
280
+
281
+ npm publish --access public # 实际发布
282
+ ```
283
+
284
+ `prepublishOnly` 会在发布前自动触发一次 `bun run build`。
285
+
286
+ ---
287
+
288
+ ## 目录结构速览
289
+
290
+ ```
291
+ mortise_ai_cli_project/
292
+ ├── bin/stem.mjs # Node 跨平台启动器(对标 mai-cc-cli/bin/mai-cli)
293
+ ├── src/
294
+ │ ├── entry/ # CLI / MCP / Daemon 入口
295
+ │ ├── ui/ # Phase 0 纯 Ink 组件(Welcome/Logo)
296
+ │ ├── msc-base/ # MSC Engine CLI 侧基建(Phase 1+ 填充)
297
+ │ ├── msc-core/ # 跨模块通用能力
298
+ │ └── msc-project/ # 业务 Module 骨架(shell/core-engine/tools/services/commands/remote/swarm/extension/daemon/boot)
299
+ ├── scripts/build-cli.ts
300
+ ├── preload.ts
301
+ ├── bunfig.toml
302
+ ├── tsconfig.json
303
+ ├── package.json
304
+ ├── LICENSE # Apache-2.0
305
+ └── ARCHITECTURE.md # ★ mai-cc-cli → MSC Module 完整映射
306
+ ```
307
+
308
+ 详细设计见 [ARCHITECTURE.md](./ARCHITECTURE.md)。
309
+
310
+ ---
311
+
312
+ ## 技术栈
313
+
314
+ | 维度 | 选择 |
315
+ |---|---|
316
+ | 语言 | TypeScript 5(严格模式 + 装饰器) |
317
+ | 开发运行时 | Bun |
318
+ | 发行格式 | ESM 单文件 bundle,Node >= 22 |
319
+ | UI | Ink 6 + React 19 |
320
+ | 架构引擎 | `@mortiseai/mai_msc_engine_ts_module@^0.0.3` |
321
+ | 许可证 | Apache-2.0 |
322
+
323
+ ---
324
+
325
+ ## 开发约定
326
+
327
+ 见 [`AGENTS.md`](./AGENTS.md) 和 [`ARCHITECTURE.md`](./ARCHITECTURE.md) 的「开发约定」章节。
328
+
package/bin/stem.mjs ADDED
@@ -0,0 +1,51 @@
1
+ #!/usr/bin/env node
2
+ // 跨平台启动器(macOS / Linux / Windows):npm 全局安装经 cmd-shim / 软链均可调用。
3
+ // 语义对齐旧 bin/stem bash 版:CALLER_DIR 透传、.env 仅补缺不覆盖、STEM_DEV 走 bun 源码。
4
+ import { existsSync, realpathSync } from 'node:fs'
5
+ import { dirname, join } from 'node:path'
6
+ import { devNull } from 'node:os'
7
+ import { fileURLToPath, pathToFileURL } from 'node:url'
8
+ import { spawnSync } from 'node:child_process'
9
+
10
+ // realpath:npm link / 全局 bin 软链下定位真实包根,而非软链父目录。
11
+ const selfPath = realpathSync(fileURLToPath(import.meta.url))
12
+ const rootDir = dirname(dirname(selfPath))
13
+
14
+ process.env.CALLER_DIR ||= process.cwd()
15
+
16
+ const envFile = join(rootDir, '.env')
17
+ const skipDotenv = process.env.STEM_SKIP_DOTENV === '1'
18
+ // loadEnvFile 与 node --env-file 同语义:已有环境变量优先,仅补缺。
19
+ if (!skipDotenv && existsSync(envFile)) {
20
+ process.loadEnvFile(envFile)
21
+ }
22
+
23
+ const distEntry = join(rootDir, 'dist', 'cli.mjs')
24
+ if (process.env.STEM_DEV !== '1' && existsSync(distEntry)) {
25
+ await import(pathToFileURL(distEntry).href)
26
+ } else {
27
+ // dev 模式:bun 直跑源码。bun 会自动加载 cwd 下的 .env,跳过时显式指向空设备。
28
+ const envFlag = skipDotenv
29
+ ? [`--env-file=${devNull}`]
30
+ : existsSync(envFile)
31
+ ? ['--env-file=.env']
32
+ : []
33
+ const result = spawnSync(
34
+ 'bun',
35
+ [...envFlag, './src/stem-entry/stem-entry-cli.tsx', ...process.argv.slice(2)],
36
+ { cwd: rootDir, stdio: 'inherit' },
37
+ )
38
+ if (result.error && result.error.code === 'ENOENT') {
39
+ // i18n: 启动器双语(CLI 还没起来,无法走 JS catalog)
40
+ const locale = (process.env.STEM_LOCALE ?? process.env.LANG ?? 'zh').toLowerCase()
41
+ console.error(
42
+ locale.startsWith('en')
43
+ ? "stem: dist/cli.mjs not found and 'bun' is not on PATH.\n" +
44
+ "Run 'bun run build' in the repo, or install bun (https://bun.sh) to use dev mode."
45
+ : 'stem: 未找到 dist/cli.mjs,且 PATH 中没有 \'bun\'。\n' +
46
+ "请在仓库根目录执行 'bun run build',或安装 bun (https://bun.sh) 以使用 dev 模式。",
47
+ )
48
+ process.exit(1)
49
+ }
50
+ process.exit(result.status ?? 1)
51
+ }