@optima-chat/dev-skills 0.7.29 → 0.7.32

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.
@@ -0,0 +1,293 @@
1
+ # /trace-user - 用户链路追踪
2
+
3
+ 查看指定用户在 agentic-chat -> optima-gateway -> agent-runtime 全链路的日志,分析耗时、成功/失败、返回值等。
4
+
5
+ **版本**: v0.1.0
6
+
7
+ ## 使用场景
8
+
9
+ **前端开发者**: 用户反馈 "聊天没反应",快速定位是前端、网关还是 Agent 的问题
10
+ **后端开发者**: 追踪特定用户的请求链路,分析 LLM 调用耗时和工具执行
11
+ **DevOps**: 排查线上用户问题,查看完整的跨服务日志关联
12
+
13
+ ## 用法
14
+
15
+ ```
16
+ /trace-user <user-id-or-email> [environment] [time-range] [options]
17
+ ```
18
+
19
+ ## 参数
20
+
21
+ - `user-id-or-email` (必需): 用户标识
22
+ - 包含 `@` → email(用 `userEmail` 字段过滤)
23
+ - UUID 格式 → userId(用 `userId` 字段过滤)
24
+ - `environment` (可选): `stage` 或 `prod`,默认 `stage`
25
+ - `time-range` (可选): 如 `10m`, `30m`, `1h`, `2h`, `1d`,默认 `30m`
26
+ - 选项:
27
+ - `--errors` — 只看 error/warn 级别
28
+ - `--session SESSION_ID` — 限定某个 session
29
+ - `--trace TRACE_ID` — 限定某个 traceId
30
+ - `--raw` — 显示原始 JSON
31
+
32
+ ## 示例
33
+
34
+ ```bash
35
+ /trace-user alice@example.com # Stage,最近 30 分钟
36
+ /trace-user 37c03a9f-0b47-409c-81a3-5634eaab1a6c prod 2h # Prod,最近 2 小时
37
+ /trace-user alice@example.com --errors # 只看错误
38
+ /trace-user alice@example.com --session sess-xxx # 指定 session
39
+ ```
40
+
41
+ 如果用户输入 `/trace-user` 或 `/trace-user --help`,显示此帮助文档,不执行查询。
42
+
43
+ ARGUMENTS: $ARGUMENTS
44
+
45
+ ## Claude Code 执行步骤
46
+
47
+ ### 1. 解析参数
48
+
49
+ 从 `$ARGUMENTS` 中解析用户标识、环境、时间范围和选项。
50
+
51
+ **用户标识判断**:
52
+ - 包含 `@` → email → 使用 `userEmail` 字段
53
+ - UUID 格式(`xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`)→ 使用 `userId` 字段
54
+ - 其他 → 当作 userId 尝试
55
+
56
+ ### 2. 日志组映射
57
+
58
+ 三层服务对应的 CloudWatch Log Group:
59
+
60
+ | 层 | 服务 | Stage 日志组 | Prod 日志组 |
61
+ |----|------|-------------|------------|
62
+ | L1 | agentic-chat | `/ecs/agentic-chat-stage` | `/ecs/agentic-chat-prod` |
63
+ | L2 | gateway-core | `/ecs/gateway-core-stage` | `/ecs/gateway-core-prod` |
64
+ | L3 | agent-runtime | `/ecs/gw-agent-runtime-stage` | `/ecs/gw-agent-runtime-prod` |
65
+
66
+ **AWS 区域**: `ap-southeast-1`
67
+
68
+ ### 3. 搜索策略
69
+
70
+ #### 第一步:在 gateway-core 找到用户的 session
71
+
72
+ ```bash
73
+ # 按 email 搜索
74
+ aws logs filter-log-events \
75
+ --log-group-name /ecs/gateway-core-{ENV} \
76
+ --filter-pattern '{ $.userEmail = "USER_EMAIL" }' \
77
+ --start-time $(date -d 'TIME_RANGE ago' +%s)000 \
78
+ --region ap-southeast-1 \
79
+ --output json | head -100
80
+
81
+ # 按 userId 搜索
82
+ aws logs filter-log-events \
83
+ --log-group-name /ecs/gateway-core-{ENV} \
84
+ --filter-pattern '{ $.userId = "USER_ID" }' \
85
+ --start-time $(date -d 'TIME_RANGE ago' +%s)000 \
86
+ --region ap-southeast-1 \
87
+ --output json | head -100
88
+ ```
89
+
90
+ 从结果中提取 `sessionId` 和 `traceId`。
91
+
92
+ #### 第二步:跨服务关联查询(Logs Insights)
93
+
94
+ 优先使用 CloudWatch Logs Insights 跨多个 log group 查询:
95
+
96
+ ```bash
97
+ # 按 userId/email 跨服务查询
98
+ aws logs start-query \
99
+ --log-group-names \
100
+ /ecs/gateway-core-{ENV} \
101
+ /ecs/gw-agent-runtime-{ENV} \
102
+ --start-time $(date -d 'TIME_RANGE ago' +%s) \
103
+ --end-time $(date +%s) \
104
+ --query-string '
105
+ fields @timestamp, @logStream, @message
106
+ | filter userId = "USER_ID" or userEmail = "USER_EMAIL"
107
+ | sort @timestamp asc
108
+ | limit 500
109
+ ' \
110
+ --region ap-southeast-1
111
+
112
+ # 等几秒后获取结果
113
+ aws logs get-query-results --query-id QUERY_ID --region ap-southeast-1
114
+ ```
115
+
116
+ 如果已知 sessionId:
117
+
118
+ ```bash
119
+ aws logs start-query \
120
+ --log-group-names \
121
+ /ecs/gateway-core-{ENV} \
122
+ /ecs/gw-agent-runtime-{ENV} \
123
+ --start-time $(date -d 'TIME_RANGE ago' +%s) \
124
+ --end-time $(date +%s) \
125
+ --query-string '
126
+ fields @timestamp, @logStream, level, message, sessionId, traceId, duration_ms
127
+ | filter sessionId = "SESSION_ID"
128
+ | sort @timestamp asc
129
+ | limit 500
130
+ ' \
131
+ --region ap-southeast-1
132
+ ```
133
+
134
+ #### 第三步:agentic-chat 日志(补充)
135
+
136
+ agentic-chat 前端日志可能不含 userId/sessionId 字段,用时间窗口 + 关键词辅助:
137
+
138
+ ```bash
139
+ aws logs tail /ecs/agentic-chat-{ENV} --since {TIME_RANGE} \
140
+ --region ap-southeast-1 | grep -i "USER_EMAIL_OR_ID"
141
+ ```
142
+
143
+ #### 第四步:只看错误(--errors)
144
+
145
+ ```bash
146
+ aws logs start-query \
147
+ --log-group-names \
148
+ /ecs/gateway-core-{ENV} \
149
+ /ecs/gw-agent-runtime-{ENV} \
150
+ --start-time $(date -d 'TIME_RANGE ago' +%s) \
151
+ --end-time $(date +%s) \
152
+ --query-string '
153
+ fields @timestamp, @logStream, level, message, sessionId, error
154
+ | filter (userId = "USER_ID" or userEmail = "USER_EMAIL")
155
+ and (level = "error" or level = "warn")
156
+ | sort @timestamp asc
157
+ | limit 200
158
+ ' \
159
+ --region ap-southeast-1
160
+ ```
161
+
162
+ ### 4. 结构化日志字段参考
163
+
164
+ optima-gateway 的日志通过 AsyncLocalStorage 自动注入:
165
+
166
+ ```json
167
+ {
168
+ "timestamp": "2026-04-16T10:30:00.000Z",
169
+ "level": "info",
170
+ "service": "gateway-core",
171
+ "userId": "37c03a9f-...",
172
+ "userEmail": "alice@example.com",
173
+ "sessionId": "sess-xxx",
174
+ "traceId": "gw-abc123",
175
+ "message": "Session created",
176
+ "duration_ms": 123
177
+ }
178
+ ```
179
+
180
+ **关键字段**:
181
+ - `userId` / `userEmail` — 用户标识
182
+ - `sessionId` — 会话 ID
183
+ - `traceId` — 跨服务链路 ID
184
+ - `duration_ms` — 操作耗时
185
+ - `level` — 日志级别(info/warn/error)
186
+
187
+ ### 5. 输出格式
188
+
189
+ #### A. 概览信息
190
+
191
+ ```
192
+ 用户: alice@example.com (37c03a9f-...)
193
+ 环境: Stage
194
+ 时间范围: 最近 30 分钟
195
+ 找到 session 数: 3
196
+ ```
197
+
198
+ #### B. Session 列表
199
+
200
+ | Session ID | 创建时间 | 状态 | 持续时间 | 消息数 | 错误数 |
201
+ |-----------|---------|------|---------|-------|-------|
202
+ | sess-001 | 10:30:00 | running | 5m | 12 | 0 |
203
+ | sess-002 | 10:20:00 | terminated | 8m | 24 | 1 |
204
+
205
+ #### C. 请求链路时间线
206
+
207
+ ```
208
+ Session: sess-001
209
+ TraceId: gw-abc123
210
+
211
+ 时间线:
212
+ ┌──────────────────────────────────────────────────────────────────────┐
213
+ │ 10:30:00.000 [gateway-core] WS connected, authenticating... │
214
+ │ 10:30:00.150 [gateway-core] OIDC verified, userId=37c03a9f │ +150ms
215
+ │ 10:30:00.200 [gateway-core] Session creating │ +50ms
216
+ │ 10:30:00.800 [gateway-core] Agent task started (ECS RunTask) │ +600ms
217
+ │ 10:30:03.200 [agent-runtime] Container ready, WS connected │ +2400ms
218
+ │ 10:30:03.250 [gateway-core] Session running │ +50ms
219
+ │ 10:30:05.000 [gateway-core] Client message received │
220
+ │ 10:30:05.100 [agent-runtime] LLM request started (openai) │ +100ms
221
+ │ 10:30:08.500 [agent-runtime] LLM response complete │ +3400ms
222
+ │ 10:30:08.600 [agent-runtime] Tool call: read_file │
223
+ │ 10:30:08.800 [agent-runtime] Tool result returned │ +200ms
224
+ │ 10:30:09.000 [gateway-core] Message forwarded to client │
225
+ └──────────────────────────────────────────────────────────────────────┘
226
+ ```
227
+
228
+ #### D. 耗时分析
229
+
230
+ ```
231
+ 关键耗时:
232
+ OIDC 认证: 150ms
233
+ Session 创建: 50ms
234
+ ECS 容器启动: 2400ms (>2s 时标记 warning)
235
+ WS 回连: 50ms
236
+ 首次 LLM 调用: 3400ms
237
+ 工具执行: 200ms
238
+
239
+ 统计:
240
+ 总消息数: 12 (user: 5, assistant: 7)
241
+ LLM 调用次数: 7
242
+ 平均 LLM 延迟: 2800ms
243
+ 工具调用次数: 3
244
+ ```
245
+
246
+ #### E. 错误/告警
247
+
248
+ ```
249
+ 发现 1 个错误:
250
+ 10:35:12 [agent-runtime] ERROR: LLM request failed
251
+ provider: openai
252
+ error: "rate_limit_exceeded"
253
+ sessionId: sess-002
254
+ → 已自动重试(CircuitBreaker half-open)
255
+ ```
256
+
257
+ ### 6. 常用诊断场景
258
+
259
+ | 用户报告 | 推荐策略 |
260
+ |---------|---------|
261
+ | "连不上" / "打不开" | 查 gateway-core 连接日志 + OIDC 认证 |
262
+ | "没反应" / "卡住了" | 查 agent-runtime 是否收到消息 + LLM 是否响应 |
263
+ | "回复慢" | 查 duration_ms 字段,分析 ECS 启动 + LLM 延迟 |
264
+ | "出错了" / "报错" | `--errors` 模式,重点看 error 级别 |
265
+ | "用了一半断了" | 查 WS 断连事件 + session 状态变化 |
266
+ | "总是失败" | 查 CircuitBreaker 状态 + provider 切换日志 |
267
+
268
+ ### 7. 快捷跟进命令
269
+
270
+ ```bash
271
+ # 查看服务健康状态
272
+ curl -s https://gw.stage.optima.onl/health | jq .
273
+ curl -s https://ai.stage.optima.onl/api/health | jq .
274
+
275
+ # 查看服务 debug 信息
276
+ curl -s -H "X-Debug-Key: 7eede5747b6c50f1c8f2358b98462f74696cdef9bfeab85eaf7ea41166788b5c" \
277
+ https://gw.stage.optima.onl/debug/info | jq .
278
+
279
+ # 查看 ECS 服务状态
280
+ aws ecs describe-services --cluster optima-stage-cluster \
281
+ --services gateway-core-stage --region ap-southeast-1 \
282
+ --query 'services[0].{status:status,running:runningCount,desired:desiredCount}'
283
+ ```
284
+
285
+ ## 注意事项
286
+
287
+ 1. **CloudWatch 延迟**: 最新日志可能需要等 10-30 秒才出现
288
+ 2. **速率限制**: `filter-log-events` 有限流,大范围查询优先用 `start-query` (Logs Insights)
289
+ 3. **日志保留**: CloudWatch 保留 14 天
290
+ 4. **结构化日志**: JSON 日志输出到 stderr,在 CloudWatch 中显示为普通文本行
291
+ 5. **Insights 限制**: 最多返回 10000 条,一般够用
292
+ 6. **权限要求**: 需要 AWS CLI 配置了正确的凭证(`ap-southeast-1` 区域)
293
+ 7. 如果用户没有任何日志,可能是该用户在指定时间范围内没有活动、标识输入有误、或服务未部署
@@ -0,0 +1,102 @@
1
+ ---
2
+ name: "grant-balance"
3
+ description: "当用户请求赠送余额、充值 USD 余额、grant balance、加余额、奖励、补偿、推荐奖励、运营发放时,使用此技能。支持 Stage、Prod 两个环境。"
4
+ allowed-tools: ["Bash"]
5
+ ---
6
+
7
+ # 赠送 USD 余额(Grant Balance)
8
+
9
+ 当你需要为用户赠送 wallet USD 余额时,使用这个场景。金额会加到 `usd_wallets.granted_balance_micros`,billing 服务在扣费时会优先消费 granted balance。
10
+
11
+ ## 执行方式:使用 CLI 工具
12
+
13
+ ```bash
14
+ optima-grant-balance <email> --amount <usd> [options]
15
+ ```
16
+
17
+ **为什么使用 CLI 工具**:
18
+ - 自动通过 email 查找 userId(跨 user-auth 数据库)
19
+ - 自动处理 SSH 隧道和数据库连接
20
+ - 不会影响现有订阅和已有余额(纯追加到 granted balance)
21
+ - 自动写入 audit trail(`usd_wallet_topups` source=admin_grant)
22
+
23
+ ## 适用情况
24
+
25
+ - 奖励额外余额(推广、活动)
26
+ - 客户补偿(服务中断等)
27
+ - 推荐奖励
28
+ - 内部测试账户充值
29
+
30
+ ## 快速操作
31
+
32
+ ```bash
33
+ # 赠送 $5(Stage 环境,默认)
34
+ optima-grant-balance user@example.com --amount 5
35
+
36
+ # 赠送 $10 到 Prod
37
+ optima-grant-balance user@example.com --amount 10 --env prod
38
+
39
+ # 带描述(Reason 仅在 console 输出,不存 DB)
40
+ optima-grant-balance user@example.com --amount 20 --description "服务中断补偿" --env prod
41
+ ```
42
+
43
+ > **单位是美元(USD)**。`--amount 5` 即赠送 $5.00 到 granted balance。
44
+ > 数据库底层用 micro-USD 精度(1 USD = 1,000,000 micros),CLI 自动换算。
45
+
46
+ ### 参数说明
47
+
48
+ | 参数 | 说明 | 默认值 |
49
+ |------|------|--------|
50
+ | `<email>` | 用户邮箱(必填) | - |
51
+ | `--amount <usd>` | USD 金额(必填,> 0) | - |
52
+ | `--description <text>` | 描述/原因(仅 console 输出) | - |
53
+ | `--env <env>` | 环境:stage, prod | stage |
54
+
55
+ ## 与 grant-subscription 的区别
56
+
57
+ | | grant-balance | grant-subscription |
58
+ |---|---|---|
59
+ | 作用 | 追加 USD granted balance | 开通/切换订阅计划 |
60
+ | 现有余额 | 不影响(纯累加) | 重置 granted balance |
61
+ | 现有订阅 | 不影响 | 取消旧的,创建新的 |
62
+ | Token quota | 不影响 | 按计划更新 |
63
+ | 适用场景 | 奖励、补偿、推广 | 开通会员、升级计划 |
64
+
65
+ ## 常见使用场景
66
+
67
+ ### 场景 1:客户补偿
68
+
69
+ **用户请求**:"服务出了问题,给 xxx@gmail.com 补偿 $5"
70
+
71
+ ```bash
72
+ optima-grant-balance xxx@gmail.com --amount 5 --description "Service outage compensation" --env prod
73
+ ```
74
+
75
+ ### 场景 2:推荐奖励
76
+
77
+ **用户请求**:"xxx 推荐了新用户,奖励 $3"
78
+
79
+ ```bash
80
+ optima-grant-balance xxx@gmail.com --amount 3 --description "Referral reward" --env prod
81
+ ```
82
+
83
+ ### 场景 3:运营发放
84
+
85
+ **用户请求**:"给 xxx@gmail.com 充 $20 测试余额"
86
+
87
+ ```bash
88
+ optima-grant-balance xxx@gmail.com --amount 20 --env stage
89
+ ```
90
+
91
+ ## 安全提醒
92
+
93
+ 1. **Stage 优先**:默认操作 Stage 环境
94
+ 2. **Prod 谨慎**:操作 Prod 前确认邮箱和金额
95
+ 3. **纯追加**:不会影响现有余额和订阅(累加到 granted balance)
96
+ 4. **Audit trail**:每次赠送会插入一条 `usd_wallet_topups` 记录(source=`admin_grant`)
97
+
98
+ ## 相关命令
99
+
100
+ - `optima-grant-balance` - 赠送 USD 余额(主要方式)
101
+ - `optima-grant-subscription` - 开通订阅计划
102
+ - `optima-query-db` - 查询数据库验证结果(`SELECT granted_balance_micros FROM usd_wallets WHERE user_id=...`)
@@ -19,8 +19,8 @@ optima-grant-subscription <email> [options]
19
19
  **为什么使用 CLI 工具**:
20
20
  - 自动通过 email 查找 userId(跨 user-auth 数据库)
21
21
  - 自动处理 SSH 隧道和数据库连接
22
- - 自动取消旧订阅、清零旧 credits
23
- - 自动按 plan 配置授予 credits token quota
22
+ - 自动取消旧订阅、重置 wallet granted balance
23
+ - 自动按 plan 配置授予 USD wallet 余额和 token quota
24
24
  - 一条命令完成所有操作
25
25
 
26
26
  ## 适用情况
@@ -59,12 +59,14 @@ optima-grant-subscription user@example.com --plan enterprise --env prod
59
59
 
60
60
  ### 计划配置
61
61
 
62
- | Plan | Credits/月 | Session Token | Weekly Token |
63
- |------|-----------|---------------|--------------|
64
- | trial | 20 | 400K | 1M |
65
- | starter | 500 | 2M | 10M |
66
- | pro | 2,000 | 8M | 40M |
67
- | enterprise | 10,000 | 16M | 80M |
62
+ | Plan | 月授予额 (USD) | Credits 等价 | Session Token | Weekly Token |
63
+ |------|---------------|-------------|---------------|--------------|
64
+ | trial | $0.20 | 20 | 400K | 1M |
65
+ | starter | $5.00 | 500 | 2M | 10M |
66
+ | pro | $20.00 | 2,000 | 8M | 40M |
67
+ | enterprise | $100.00 | 10,000 | 16M | 80M |
68
+
69
+ > 1 credit = $0.01 = 10,000 micros。授予额存入 `usd_wallets.granted_balance_micros`。
68
70
 
69
71
  ## 常见使用场景
70
72
 
@@ -97,22 +99,22 @@ optima-grant-subscription user@example.com --plan starter --env prod
97
99
  工具会自动完成以下步骤:
98
100
 
99
101
  1. 通过 email 在 user-auth 数据库查找 userId
100
- 2. 加载对应 plan 的配置(credits、token 限额等)
102
+ 2. 加载对应 plan 的配置(月授予额、token 限额等)
101
103
  3. 取消该用户的所有活跃订阅
102
- 4. 清零旧的 credits
103
- 5. 创建新订阅(设置到期时间)
104
- 6. 授予对应 credits
104
+ 4. 创建新订阅(设置到期时间)
105
+ 5. 重置 wallet granted balance 并授予新额度
106
+ 6. 记录 topup 审计记录(source: subscription_grant)
105
107
  7. 更新 token quota 限额
106
108
 
107
109
  ## 安全提醒
108
110
 
109
111
  1. **Stage 优先**:默认操作 Stage 环境
110
112
  2. **Prod 谨慎**:操作 Prod 前确认用户邮箱正确
111
- 3. **不可逆**:旧订阅和 credits 会被清除
113
+ 3. **不可逆**:旧订阅会被取消,granted balance 会被重置
112
114
  4. **确认 email**:执行前务必确认邮箱地址无误
113
115
 
114
116
  ## 相关命令
115
117
 
116
118
  - `optima-grant-subscription` - 开通订阅(主要方式)
117
- - `optima-grant-credits` - 单独赠送 credits
119
+ - `optima-grant-balance` - 单独赠送 wallet 余额
118
120
  - `optima-query-db` - 查询数据库验证结果
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: "query-db"
3
- description: "当用户请求查询数据库、执行SQL、查看数据、统计数据、检查数据库、查询表、数据库查询时,使用此技能。支持 CI、Stage、Prod 三个环境的 commerce-backend、user-auth、agentic-chat、bi-backend、session-gateway、ads-backend、amazon-backend、shopify-backend、optima-sentinel 等服务的数据库查询。优先使用 optima-query-db CLI 工具。"
3
+ description: "当用户请求查询数据库、执行SQL、查看数据、统计数据、检查数据库、查询表、数据库查询时,使用此技能。支持 CI、Stage、Prod 三个环境的 commerce-backend、user-auth、agentic-chat、bi-backend、session-gateway、gateway-core、ads-backend、amazon-backend、shopify-backend、optima-sentinel 等服务的数据库查询。优先使用 optima-query-db CLI 工具。"
4
4
  allowed-tools: ["Bash", "SlashCommand"]
5
5
  ---
6
6
 
@@ -75,7 +75,8 @@ optima-query-db commerce-backend "SELECT status, COUNT(*) FROM orders GROUP BY s
75
75
  - `user-auth` - 用户认证数据库
76
76
  - `agentic-chat` - AI 聊天数据库
77
77
  - `bi-backend` - BI 后端数据库
78
- - `session-gateway` - AI Shell 网关数据库
78
+ - `session-gateway` - AI Shell 网关数据库(老)
79
+ - `gateway-core` - Optima Gateway (`optima_gateway`) 数据库 — sessions / conversations / messages / transcripts / credits
79
80
  - `optima-logistics` - 物流服务数据库
80
81
  - `ads-backend` - Ads 数据库
81
82
  - `amazon-backend` - Amazon 数据库
@@ -212,6 +213,7 @@ optima-query-db commerce-backend "SELECT status, COUNT(*) FROM orders GROUP BY s
212
213
  | shopify-backend | `/services/shopify-backend` |
213
214
  | optima-generation | `/services/optima-generation` |
214
215
  | optima-sentinel | `/services/optima-sentinel` |
216
+ | gateway-core | `/services/gateway-core` |
215
217
 
216
218
  ### RDS 连接
217
219
 
@@ -0,0 +1,101 @@
1
+ ---
2
+ name: "grant-balance"
3
+ description: "Use when the user wants to grant USD wallet balance to an Optima user — for promotional grants, compensation, referral rewards, etc. Adds to granted_balance_micros without affecting subscriptions."
4
+ ---
5
+
6
+ # 赠送 USD 余额(Grant Balance)
7
+
8
+ 当你需要为用户赠送 wallet USD 余额时,使用这个场景。金额会加到 `usd_wallets.granted_balance_micros`,billing 服务在扣费时会优先消费 granted balance。
9
+
10
+ ## 执行方式:使用 CLI 工具
11
+
12
+ ```bash
13
+ optima-grant-balance <email> --amount <usd> [options]
14
+ ```
15
+
16
+ **为什么使用 CLI 工具**:
17
+ - 自动通过 email 查找 userId(跨 user-auth 数据库)
18
+ - 自动处理 SSH 隧道和数据库连接
19
+ - 不会影响现有订阅和已有余额(纯追加到 granted balance)
20
+ - 自动写入 audit trail(`usd_wallet_topups` source=admin_grant)
21
+
22
+ ## 适用情况
23
+
24
+ - 奖励额外余额(推广、活动)
25
+ - 客户补偿(服务中断等)
26
+ - 推荐奖励
27
+ - 内部测试账户充值
28
+
29
+ ## 快速操作
30
+
31
+ ```bash
32
+ # 赠送 $5(Stage 环境,默认)
33
+ optima-grant-balance user@example.com --amount 5
34
+
35
+ # 赠送 $10 到 Prod
36
+ optima-grant-balance user@example.com --amount 10 --env prod
37
+
38
+ # 带描述(Reason 仅在 console 输出,不存 DB)
39
+ optima-grant-balance user@example.com --amount 20 --description "服务中断补偿" --env prod
40
+ ```
41
+
42
+ > **单位是美元(USD)**。`--amount 5` 即赠送 $5.00 到 granted balance。
43
+ > 数据库底层用 micro-USD 精度(1 USD = 1,000,000 micros),CLI 自动换算。
44
+
45
+ ### 参数说明
46
+
47
+ | 参数 | 说明 | 默认值 |
48
+ |------|------|--------|
49
+ | `<email>` | 用户邮箱(必填) | - |
50
+ | `--amount <usd>` | USD 金额(必填,> 0) | - |
51
+ | `--description <text>` | 描述/原因(仅 console 输出) | - |
52
+ | `--env <env>` | 环境:stage, prod | stage |
53
+
54
+ ## 与 grant-subscription 的区别
55
+
56
+ | | grant-balance | grant-subscription |
57
+ |---|---|---|
58
+ | 作用 | 追加 USD granted balance | 开通/切换订阅计划 |
59
+ | 现有余额 | 不影响(纯累加) | 重置 granted balance |
60
+ | 现有订阅 | 不影响 | 取消旧的,创建新的 |
61
+ | Token quota | 不影响 | 按计划更新 |
62
+ | 适用场景 | 奖励、补偿、推广 | 开通会员、升级计划 |
63
+
64
+ ## 常见使用场景
65
+
66
+ ### 场景 1:客户补偿
67
+
68
+ **用户请求**:"服务出了问题,给 xxx@gmail.com 补偿 $5"
69
+
70
+ ```bash
71
+ optima-grant-balance xxx@gmail.com --amount 5 --description "Service outage compensation" --env prod
72
+ ```
73
+
74
+ ### 场景 2:推荐奖励
75
+
76
+ **用户请求**:"xxx 推荐了新用户,奖励 $3"
77
+
78
+ ```bash
79
+ optima-grant-balance xxx@gmail.com --amount 3 --description "Referral reward" --env prod
80
+ ```
81
+
82
+ ### 场景 3:运营发放
83
+
84
+ **用户请求**:"给 xxx@gmail.com 充 $20 测试余额"
85
+
86
+ ```bash
87
+ optima-grant-balance xxx@gmail.com --amount 20 --env stage
88
+ ```
89
+
90
+ ## 安全提醒
91
+
92
+ 1. **Stage 优先**:默认操作 Stage 环境
93
+ 2. **Prod 谨慎**:操作 Prod 前确认邮箱和金额
94
+ 3. **纯追加**:不会影响现有余额和订阅(累加到 granted balance)
95
+ 4. **Audit trail**:每次赠送会插入一条 `usd_wallet_topups` 记录(source=`admin_grant`)
96
+
97
+ ## 相关命令
98
+
99
+ - `optima-grant-balance` - 赠送 USD 余额(主要方式)
100
+ - `optima-grant-subscription` - 开通订阅计划
101
+ - `optima-query-db` - 查询数据库验证结果(`SELECT granted_balance_micros FROM usd_wallets WHERE user_id=...`)
@@ -25,5 +25,5 @@ optima-grant-subscription user@example.com --plan enterprise --env prod
25
25
 
26
26
  - Default to `stage`.
27
27
  - Confirm the user email before running on `prod`.
28
- - This operation replaces existing subscription state and resets credits according to the selected plan.
28
+ - This operation replaces existing subscription state and resets the wallet granted balance according to the selected plan.
29
29
  - Use `optima-query-db` afterward if the user asks for verification.
package/AGENTS.md CHANGED
@@ -10,7 +10,7 @@ Prefer the installed CLI tools over reimplementing long shell workflows:
10
10
  - `optima-show-env <service> <stage|prod> [options]`
11
11
  - `optima-generate-test-token [options]`
12
12
  - `optima-grant-subscription <email> [options]`
13
- - `optima-grant-credits <email> --amount <n> [options]`
13
+ - `optima-grant-balance <email> --amount <usd> [options]`
14
14
 
15
15
  For code-reading tasks across Optima repositories, use `gh` commands against `Optima-Chat/<repo>`.
16
16
 
@@ -24,7 +24,7 @@ After `npm install -g @optima-chat/dev-skills`, this package installs skills und
24
24
  - `generate-test-token`
25
25
  - `read-code`
26
26
  - `grant-subscription`
27
- - `grant-credits`
27
+ - `grant-balance`
28
28
  - `restart-ecs`
29
29
  - `use-commerce-cli`
30
30
 
package/bin/cli.js CHANGED
@@ -33,7 +33,7 @@ switch (command) {
33
33
  log(' optima-query-db <service> "<sql>" [env] Query database', 'cyan');
34
34
  log(' optima-show-env <service> [env] Show service env vars', 'cyan');
35
35
  log(' optima-generate-test-token [--env production] Generate test token', 'cyan');
36
- log(' optima-grant-credits <email> --amount <n> [--env] Grant credits to user', 'cyan');
36
+ log(' optima-grant-balance <email> --amount <usd> [--env] Grant USD wallet balance', 'cyan');
37
37
  log(' optima-grant-subscription <email> --plan <p> [--env] Grant subscription', 'cyan');
38
38
  log(' /logs <service> [lines] [env] View service logs (skill)', 'cyan');
39
39
  log(' /restart-ecs <service> [env] Restart ECS service (skill)', 'cyan');
@@ -48,7 +48,7 @@ switch (command) {
48
48
  log('\nExamples:', 'yellow');
49
49
  log(' /logs commerce-backend 100 prod', 'cyan');
50
50
  log(' optima-query-db user-auth "SELECT COUNT(*) FROM users" prod', 'cyan');
51
- log(' optima-grant-credits user@example.com --amount 5000 --env prod', 'cyan');
51
+ log(' optima-grant-balance user@example.com --amount 5 --env prod', 'cyan');
52
52
 
53
53
  log('\nMore Info:', 'yellow');
54
54
  log(' optima-dev-skills --version Show version', 'cyan');