Skip to content

Commit fbc9cc4

Browse files
committed
feat: add CLI source config tags
1 parent 081d098 commit fbc9cc4

13 files changed

Lines changed: 230 additions & 43 deletions

File tree

AGENTS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Skill / 命令手册随 `skills/bailian-cli/` 经 `npx skills add modelstudioai/
6565
| 模型上下架 | 增加新模型 / 改默认模型 / 废弃旧模型 | [docs/agents/model-add-remove.md](docs/agents/model-add-remove.md) |
6666
| 错误文案变更 |`BailianError` 的 message 或 hint | [docs/agents/error-hint-change.md](docs/agents/error-hint-change.md) |
6767
| URL / 渠道变更 | 控制台域名 / 文档站 / 追踪参数 | [docs/agents/url-change.md](docs/agents/url-change.md) |
68+
| 埋点变更 | 改 AEM 命令事件、后端渠道 header、User-Agent 或参数 allowlist | [docs/agents/telemetry-change.md](docs/agents/telemetry-change.md) |
6869
| 鉴权扩展 | 加 OAuth / SSO / 换 token 来源 | [docs/agents/auth-change.md](docs/agents/auth-change.md) |
6970
| 配置项扩展 | 新 env var 或 `~/.bailian/config.json` 字段 | [docs/agents/config-add.md](docs/agents/config-add.md) |
7071
| Profile / 激活 | 改命名 Profile、预设或 `active_config` | [docs/agents/config-profile-change.md](docs/agents/config-profile-change.md) |

docs/agents/telemetry-change.md

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# 埋点变更
2+
3+
## 触发条件
4+
5+
- 调整 AEM 命令事件、事件字段或参数 allowlist
6+
- 调整 `User-Agent``x-dashscope-source-config` 或其他后端渠道标识
7+
- 新增鉴权域、请求网关或绕开统一 Client 的网络出口
8+
- 排查命令量、成功率、版本、鉴权域或后端渠道数据不一致
9+
10+
## 当前数据流
11+
12+
三套鉴权对应三套请求域,但不代表三套网关使用相同的后端埋点。命令侧另有一套覆盖所有实际执行命令的 AEM 客户端事件,两者必须分开理解。
13+
14+
```text
15+
命令进入 run
16+
├─ telemetryStage
17+
│ ├─ ~/.bailian/telemetry.jsonl
18+
│ └─ AEM(pid=bailian-cli-node, event name=命令路径)
19+
20+
└─ authStage
21+
├─ apiKey → DashScope / 模型域
22+
├─ console → Bailian Console Gateway
23+
├─ openapi → 阿里云 OpenAPI
24+
└─ none → 无凭证域;本地命令也仍有 AEM 命令事件
25+
```
26+
27+
### 1. 三套鉴权与埋点标识
28+
29+
| 命令声明 | 凭证 / 请求域 | 主要请求出口 | 后端埋点标识 | 前端埋点标识(AEM) |
30+
| ----------------- | --------------------------------------------------- | ------------------------------------------------------------------------------------- | --------------------------------------------- | ------------------------------------------------ |
31+
| `auth: "apiKey"` | API Key;DashScope / OpenAI-compatible 模型域 | `Client.request/requestJson``McpClient`、Managed Agent instrumented fetch、上传策略 | 有:`User-Agent``x-dashscope-source-config` | 有:`pid=bailian-cli-node``authMethod=apiKey` |
32+
| `auth: "console"` | Console access token;Bailian Console Gateway | `callConsoleGateway()``/cli/api.json` || 有:`pid=bailian-cli-node``authMethod=console` |
33+
| `auth: "openapi"` | AccessKey ID/Secret,可选 STS token;阿里云 OpenAPI | `Client.openApiJson()` | 有:`x-dashscope-source-config` | 有:`pid=bailian-cli-node``authMethod=openapi` |
34+
| `auth: "none"` | 无凭证域 | 本地逻辑或命令自行管理的登录/配置流程 || 有:`pid=bailian-cli-node``authMethod=none` |
35+
36+
`authMethod` 记录的是命令声明的鉴权域,不是凭证来源。它不会区分 API Key 来自 flag、env 还是 config。
37+
鉴权域是命令的准入门槛和主请求域,不保证命令内部只有一种网络出口;例如部分 `apiKey` 命令也可能读取匿名 Console 公共目录,Managed Agent 还可能访问其他 provider。
38+
39+
表中的后端埋点按该鉴权域的主要业务请求填写:
40+
41+
- Managed Agent 的 `User-Agent` 对所有 SDK 请求注入;`x-dashscope-source-config` 仅对阿里云 host 注入
42+
- DashScope 上传策略 `getPolicy` 只有 `x-dashscope-source-config`,没有显式 CLI `User-Agent`
43+
- OpenAPI 的 ACS 签名头,以及 Console Gateway 的 `product``action``api` 是鉴权或路由字段,不计为埋点标识
44+
45+
### 2. 后端渠道参数
46+
47+
当前 `x-dashscope-source-config` 结构为:
48+
49+
```json
50+
{
51+
"channel": "bailian-cli",
52+
"tags": {
53+
"t1": "public",
54+
"t2": "bl 或 kscli",
55+
"t3": "实际 CLI 版本"
56+
}
57+
}
58+
```
59+
60+
- `t2` 取产品 `identity.binName`:完整 CLI 为 `bl`,Knowledge Studio CLI 为 `kscli`
61+
- `t3` 取产品 `identity.version`,由产品入口的 `package.json` 注入
62+
- `channel``t1` 是当前固定口径
63+
- `User-Agent` 是独立标识:`bl``bailian-cli/<version>``kscli``knowledge-studio-cli/<version>`
64+
65+
source-config 只用于百炼 / DashScope API 侧消费,不发送到通用网络传输:
66+
67+
| 请求 | source-config |
68+
| ------------------------------------ | ------------- |
69+
| 模型 API、任务提交与轮询 ||
70+
| Bailian MCP / OpenAPI ||
71+
| DashScope 上传策略 `getPolicy` ||
72+
| OSS 文件上传 ||
73+
| 图片、视频、音频、转录结果下载 ||
74+
| npm / 二进制更新检查、Skill registry ||
75+
76+
当前已知例外:Pipeline runtime 自建的 `Identity.version``0.0.0-dev`,因此 Pipeline 内部模型请求的 `t3` 不代表产品包版本;现阶段不纳入本轮收敛。
77+
78+
### 3. 全命令 AEM 客户端埋点
79+
80+
`packages/runtime/src/middleware.ts``telemetryStage` 包裹 `authStage` 与命令执行,因此成功、业务失败、网络失败和鉴权失败都会形成一次命令事件。事件名是空格连接的命令路径,例如 `text chat`
81+
82+
以下情况不会形成命令事件,因为没有进入 middleware 的 `run`
83+
84+
- 根帮助、子命令 `--help``--version`
85+
- 未识别命令、参数解析失败、缺少必填参数
86+
- `defineCommand.validate` 在 dispatch 阶段拒绝的请求
87+
88+
遥测默认开启;`DO_NOT_TRACK=1` 一票否决,配置文件 `telemetry: false` 也可关闭。关闭后本地和远端均不记录。
89+
90+
单条 `TrackingEvent` 当前包含:
91+
92+
- `command``timestamp``durationMs``success`
93+
- `cliVersion``nodeVersion``os`
94+
- `authMethod`
95+
- 失败时的 `errorMessage``httpStatus``requestId`
96+
- 安全 allowlist 过滤后的 `params`
97+
98+
参数默认不上传,只有 `packages/core/src/telemetry/tracker.ts``PARAM_ALLOWLIST` 中字段会进入事件。不得加入 prompt、凭证、文件路径、URL、账号/租户/工作空间 ID 或其他用户内容。
99+
100+
事件同时写入两处:
101+
102+
1. 本地 `~/.bailian/telemetry.jsonl`:权限 `0600`,超过 5 MB 后重建
103+
2. AEM:`pid=bailian-cli-node`,源码运行自动使用 `env=dev`,npm 安装或编译二进制使用 `env=prod`
104+
105+
底层 Node tracker 还会附加公共设备字段:OS 类型/版本、Node 应用名与版本、平台,以及由本机网络标识计算的 MD5 `device_id`
106+
107+
当前 AEM 事件没有 `binName``clientName` 产品维度,并且 `bl``kscli` 共用 `pid=bailian-cli-node`。两边相同路径的 `config show``config set``update` 无法仅凭当前事件稳定区分产品;Knowledge 命令虽然因路径映射不同而表现为 `knowledge chat``chat`,也不应把命令路径当作长期产品标识。后端 source-config 的 `t2` 已能区分 `bl/kscli`,但这个维度尚未进入 AEM 客户端事件。
108+
109+
AEM 映射:
110+
111+
| AEM 字段 | 内容 |
112+
| ---------- | ----------------------------------------- |
113+
| event name | 命令路径 |
114+
| `et` | `EXP` |
115+
| `ext` |`command``params` 外的结构化事件字段 |
116+
| `c1` | allowlist 参数 |
117+
| `c2` | `success` / `failure` |
118+
| `c3` | HTTP status |
119+
| `c4` | 错误文案,最多 500 字符 |
120+
| `c5` | request ID |
121+
122+
远端发送是 best-effort,不得阻塞命令或改变退出码。正常退出最多等待 1 秒,SIGINT 最多等待 500 ms。
123+
124+
## 必查清单
125+
126+
### A. 新增或调整命令
127+
128+
- [ ] `defineCommand({ auth })` 必须声明真实请求域;AEM 的 `authMethod` 直接读取该值
129+
- [ ] 新命令进入 `run` 后自动有基础事件,不得在命令内重复发送同名事件
130+
- [ ] 需要按产品分析 AEM 数据时,必须显式设计产品字段;不得从命令路径推断 `bl/kscli`
131+
- [ ] 只有可枚举、数值或布尔等低风险字段才可加入 `PARAM_ALLOWLIST`
132+
- [ ] 新增 console raw API flag 时只允许记录公开 API 名,不得记录请求 `data`
133+
134+
### B. 调整后端渠道参数
135+
136+
- [ ] 同时核对 `packages/core/src/client/http.ts``mcp.ts``instrumented-fetch.ts``client.ts``files/upload.ts`
137+
- [ ] 产品身份必须来自 `Identity`;不得从命令路径、环境变量或 `process.argv` 猜测
138+
- [ ] `bl``kscli` 必须分别验证 `binName``clientName``version`
139+
- [ ] OSS、结果文件、npm、二进制和 Skill 下载不得为了业务渠道统计新增 source-config
140+
- [ ] 改 URL / host 范围时同时执行 [URL / 渠道变更](url-change.md) 清单
141+
142+
### C. 调整 AEM 事件
143+
144+
- [ ] 更新 `TrackingEvent``createTrackingEvent()``buildRemoteAemOptions()` 的字段映射
145+
- [ ] 本地 JSONL 与远端 AEM 必须基于同一结构化事件,不能维护两套字段口径
146+
- [ ] 成功与失败均覆盖;遥测异常必须静默且不改变业务退出码
147+
- [ ] 检查 `DO_NOT_TRACK=1``telemetry: false` 两个关闭入口
148+
- [ ] 错误字段不得额外拼接 token、请求体、prompt 或本地路径
149+
150+
## 完成后自查
151+
152+
```sh
153+
rg -n "trackingHeaders|x-dashscope-source-config|User-Agent" packages --glob '*.ts'
154+
rg -n "trackCommandExecution|PARAM_ALLOWLIST|buildRemoteAemOptions" packages/core packages/runtime --glob '*.ts'
155+
vp check
156+
vp test packages/core/tests packages/commands/tests/e2e/auth.e2e.test.ts
157+
```
158+
159+
## 常见漏点
160+
161+
- ✗ 只看 AEM 命令事件,误以为它能替代网关侧请求渠道统计
162+
- ✗ 把 `authMethod` 当成实际凭证来源;它只是命令声明的鉴权域
163+
- ✗ 新增 bypass `fetch` 后漏掉应由网关消费的 source-config,或把它发给 OSS / npm / 第三方下载地址
164+
- ✗ 只改 `bl` 入口,导致 `kscli` 的产品名或版本标签错误
165+
- ✗ 把帮助、版本或参数校验失败算进“全部命令”;这些路径当前没有进入 telemetry middleware

packages/commands/src/commands/speech/recognize.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
type DashScopeASRRequest,
1010
type DashScopeASRTaskResult,
1111
type DashScopeAsyncResponse,
12-
trackingHeaders,
1312
stripUndefined,
1413
taskPath,
1514
speechRecognizePath,
@@ -201,9 +200,7 @@ async function handleAsyncMode(
201200
}
202201

203202
// Fetch transcription JSON
204-
const transRes = await fetch(subResult.transcription_url, {
205-
headers: trackingHeaders(),
206-
});
203+
const transRes = await fetch(subResult.transcription_url);
207204
if (!transRes.ok) {
208205
throw new BailianError(
209206
`Failed to download transcription: HTTP ${transRes.status}`,

packages/core/src/client/client.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ export class Client {
126126
/** Resolve a file arg: upload a local path to OSS (returns oss:// URL), or pass a URL through. */
127127
uploadFile(source: string, model: string, opts: { signal?: AbortSignal } = {}): Promise<string> {
128128
if (!isLocalFile(source)) return Promise.resolve(source);
129-
return resolveFileUrl(source, this.requireApi().token, model, opts);
129+
return resolveFileUrl(source, this.requireApi().token, model, {
130+
...opts,
131+
identity: this.deps.identity,
132+
});
130133
}
131134

132135
/**
@@ -233,7 +236,7 @@ export class Client {
233236
const timeoutMs = this.deps.settings.timeout * 1000;
234237
const res = await fetch(endpoint, {
235238
method: opts.method,
236-
headers: { ...headers, ...trackingHeaders() },
239+
headers: { ...headers, ...trackingHeaders(this.deps.identity) },
237240
body: bodyStr || undefined,
238241
signal: AbortSignal.timeout(timeoutMs),
239242
});
Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
/**
22
* Shared HTTP request headers for all outgoing requests.
33
*
4-
* Centralises the `x-dashscope-source-config` header so every fetch call
5-
* (both via the central http client and the bypass paths) uses the
6-
* same values from a single source of truth.
4+
* Centralises the `x-dashscope-source-config` header so Bailian/DashScope API
5+
* transports use the same product identity. Generic npm, OSS, and result-file
6+
* transfers deliberately do not send this gateway-consumed metadata.
77
*/
88

9+
import type { Identity } from "../config/schema.ts";
10+
911
export const CHANNEL = "bailian-cli";
1012

11-
export const TAGS = { t1: "public", t2: "" };
13+
export type TrackingIdentity = Pick<Identity, "binName" | "version">;
1214

13-
export const SOURCE_CONFIG = JSON.stringify({
14-
channel: CHANNEL,
15-
tags: TAGS,
16-
});
15+
export function sourceConfig(identity: TrackingIdentity): string {
16+
return JSON.stringify({
17+
channel: CHANNEL,
18+
tags: {
19+
t1: "public",
20+
t2: identity.binName,
21+
t3: identity.version,
22+
},
23+
});
24+
}
1725

18-
/** Standard tracking headers required on every outbound request. */
19-
export function trackingHeaders(): Record<string, string> {
26+
/** Tracking headers for Bailian/DashScope API requests. */
27+
export function trackingHeaders(identity: TrackingIdentity): Record<string, string> {
2028
return {
21-
"x-dashscope-source-config": SOURCE_CONFIG,
29+
"x-dashscope-source-config": sourceConfig(identity),
2230
};
2331
}

packages/core/src/client/http.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { BailianError } from "../errors/base.ts";
44
import { ExitCode } from "../errors/codes.ts";
55
import { mapApiError } from "../errors/api.ts";
66
import { maskToken } from "../utils/token.ts";
7-
import { SOURCE_CONFIG, trackingHeaders } from "./headers.ts";
7+
import { sourceConfig, trackingHeaders } from "./headers.ts";
88

99
/** 传输层依赖:UA 用 identity,timeout/verbose 用 settings。凭证由调用方(Client)注头。 */
1010
export interface HttpDeps {
@@ -39,7 +39,7 @@ export async function request(deps: HttpDeps, opts: RequestOpts): Promise<Respon
3939

4040
const headers: Record<string, string> = {
4141
"User-Agent": `${deps.identity.clientName}/${deps.identity.version}`,
42-
...trackingHeaders(),
42+
...trackingHeaders(deps.identity),
4343
...opts.headers,
4444
};
4545

@@ -59,7 +59,7 @@ export async function request(deps: HttpDeps, opts: RequestOpts): Promise<Respon
5959
console.error(`> ${opts.method ?? "GET"} ${opts.url}`);
6060
const auth = headers["Authorization"];
6161
if (auth) console.error(`> Auth: ${maskToken(auth.replace(/^Bearer /, ""))}`);
62-
console.error(`> x-dashscope-source-config: ${SOURCE_CONFIG}`);
62+
console.error(`> x-dashscope-source-config: ${sourceConfig(deps.identity)}`);
6363
}
6464

6565
const timeoutMs = (opts.timeout ?? deps.settings.timeout) * 1000;

packages/core/src/client/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export {
3434
type ImageInputStyle,
3535
type ImageSizeProfile,
3636
} from "./image-routes.ts";
37-
export { CHANNEL, SOURCE_CONFIG, TAGS, trackingHeaders } from "./headers.ts";
37+
export { CHANNEL, sourceConfig, trackingHeaders, type TrackingIdentity } from "./headers.ts";
3838
export type { HttpDeps, RequestOpts } from "./http.ts";
3939
export { request, requestJson } from "./http.ts";
4040
export { createInstrumentedFetch, type FetchImplementation } from "./instrumented-fetch.ts";

packages/core/src/client/instrumented-fetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export function createInstrumentedFetch(deps: HttpDeps): FetchImplementation {
5050
headers.set("User-Agent", `${deps.identity.clientName}/${deps.identity.version}`);
5151
}
5252
if (isAlibabaCloudHost(url)) {
53-
for (const [name, value] of Object.entries(trackingHeaders())) {
53+
for (const [name, value] of Object.entries(trackingHeaders(deps.identity))) {
5454
headers.set(name, value);
5555
}
5656
}

packages/core/src/client/mcp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export class McpClient {
148148
"Content-Type": "application/json",
149149
Accept: "application/json, text/event-stream",
150150
"User-Agent": `${this.deps.identity.clientName}/${this.deps.identity.version}`,
151-
...trackingHeaders(),
151+
...trackingHeaders(this.deps.identity),
152152
};
153153

154154
if (this.authToken) {

packages/core/src/files/upload.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { existsSync, readFileSync, statSync } from "fs";
99
import { basename, extname } from "path";
1010
import { BailianError } from "../errors/base.ts";
1111
import { ExitCode } from "../errors/codes.ts";
12-
import { trackingHeaders } from "../client/headers.ts";
12+
import { trackingHeaders, type TrackingIdentity } from "../client/headers.ts";
1313
import { REGIONS } from "../config/schema.ts";
1414

1515
// Pinned to cn region; thread baseUrl through if overseas upload becomes a requirement.
@@ -36,6 +36,7 @@ interface UploadPolicyResponse {
3636
async function getUploadPolicy(
3737
apiKey: string,
3838
model: string,
39+
identity: TrackingIdentity,
3940
signal?: AbortSignal,
4041
): Promise<UploadPolicy> {
4142
const url = `${UPLOAD_API}?action=getPolicy&model=${encodeURIComponent(model)}`;
@@ -44,7 +45,7 @@ async function getUploadPolicy(
4445
headers: {
4546
Authorization: `Bearer ${apiKey}`,
4647
"Content-Type": "application/json",
47-
...trackingHeaders(),
48+
...trackingHeaders(identity),
4849
},
4950
signal: policySignal.signal,
5051
}).finally(policySignal.cleanup);
@@ -87,9 +88,6 @@ async function uploadToOSS(
8788
const uploadSignal = combineWithTimeout(120_000, signal);
8889
const res = await fetch(policy.upload_host, {
8990
method: "POST",
90-
headers: {
91-
...trackingHeaders(),
92-
},
9391
body: form,
9492
signal: uploadSignal.signal,
9593
}).finally(uploadSignal.cleanup);
@@ -109,6 +107,7 @@ export interface UploadOptions {
109107
apiKey: string;
110108
model: string;
111109
filePath: string;
110+
identity: TrackingIdentity;
112111
signal?: AbortSignal;
113112
}
114113

@@ -160,7 +159,7 @@ export function redactDataUri(input: string): string {
160159
* The URL is valid for 48 hours.
161160
*/
162161
export async function uploadFile(opts: UploadOptions): Promise<string> {
163-
const { apiKey, model, filePath, signal } = opts;
162+
const { apiKey, model, filePath, identity, signal } = opts;
164163

165164
if (!existsSync(filePath)) {
166165
throw new BailianError(`File not found: ${filePath}`, ExitCode.USAGE);
@@ -171,7 +170,7 @@ export async function uploadFile(opts: UploadOptions): Promise<string> {
171170
throw new BailianError(`Not a file: ${filePath}`, ExitCode.USAGE);
172171
}
173172

174-
const policy = await getUploadPolicy(apiKey, model, signal);
173+
const policy = await getUploadPolicy(apiKey, model, identity, signal);
175174
return uploadToOSS(policy, filePath, signal);
176175
}
177176

@@ -193,10 +192,16 @@ export async function resolveFileUrl(
193192
input: string,
194193
apiKey: string,
195194
model: string,
196-
opts: { signal?: AbortSignal } = {},
195+
opts: { identity: TrackingIdentity; signal?: AbortSignal },
197196
): Promise<string> {
198197
if (!isLocalFile(input)) return input;
199-
return uploadFile({ apiKey, model, filePath: input, signal: opts.signal });
198+
return uploadFile({
199+
apiKey,
200+
model,
201+
filePath: input,
202+
identity: opts.identity,
203+
signal: opts.signal,
204+
});
200205
}
201206

202207
function combineWithTimeout(

0 commit comments

Comments
 (0)