Skip to content

Commit 4343fc8

Browse files
committed
feat(core): 添加并统一管理 x-dashscope-openapisource 请求头
- 在 headers.ts 中新增 OPEN_API_SOURCE 常量,作为静态的 OpenAPI 源标识 - 在 trackingHeaders 函数中添加 x-dashscope-openapisource 请求头 - 更新 client/index.ts 以导出 OPEN_API_SOURCE - 在 instrumented-fetch.test.ts 中添加对应请求头的测试,确保其正确添加或省略 - 修改文档注释,明确 x-dashscope-openapisource 与 x-dashscope-source-config 的用途和区别
1 parent 1b568e8 commit 4343fc8

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

packages/core/src/client/headers.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
/**
22
* Shared HTTP request headers for all outgoing requests.
33
*
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.
4+
* Centralises the `x-dashscope-source-config` and `x-dashscope-openapisource`
5+
* headers so Bailian/DashScope API transports use the same product identity.
6+
* Generic npm, OSS, and result-file transfers deliberately do not send this
7+
* gateway-consumed metadata.
78
*/
89

910
import type { Identity } from "../config/schema.ts";
1011

1112
export const CHANNEL = "bailian-cli";
1213

14+
/** Static source identifier advertised to the DashScope/Bailian OpenAPI gateway. */
15+
export const OPEN_API_SOURCE = "BailianCLI";
16+
1317
export type TrackingIdentity = Pick<Identity, "binName" | "version">;
1418

1519
export function sourceConfig(identity: TrackingIdentity): string {
@@ -27,5 +31,6 @@ export function sourceConfig(identity: TrackingIdentity): string {
2731
export function trackingHeaders(identity: TrackingIdentity): Record<string, string> {
2832
return {
2933
"x-dashscope-source-config": sourceConfig(identity),
34+
"x-dashscope-openapisource": OPEN_API_SOURCE,
3035
};
3136
}

packages/core/src/client/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ export {
3636
type ImageInputStyle,
3737
type ImageSizeProfile,
3838
} from "./image-routes.ts";
39-
export { CHANNEL, sourceConfig, trackingHeaders, type TrackingIdentity } from "./headers.ts";
39+
export {
40+
CHANNEL,
41+
OPEN_API_SOURCE,
42+
sourceConfig,
43+
trackingHeaders,
44+
type TrackingIdentity,
45+
} from "./headers.ts";
4046
export type { HttpDeps, RequestOpts } from "./http.ts";
4147
export { request, requestJson } from "./http.ts";
4248
export { createInstrumentedFetch, type FetchImplementation } from "./instrumented-fetch.ts";

packages/core/tests/instrumented-fetch.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ test("adds UA and tracking header on Alibaba Cloud hosts", async () => {
5757
tags: { t1: "public", t2: "bl", t3: "1.2.3" },
5858
}),
5959
);
60+
expect(headers.get("x-dashscope-openapisource")).toBe("BailianCLI");
6061
expect(headers.get("authorization")).toBe("Bearer k");
6162
});
6263

@@ -66,6 +67,7 @@ test("adds UA but no tracking header on third-party hosts", async () => {
6667
});
6768
expect(headers.get("user-agent")).toBe("bailian-cli/1.2.3");
6869
expect(headers.get("x-dashscope-source-config")).toBeNull();
70+
expect(headers.get("x-dashscope-openapisource")).toBeNull();
6971
});
7072

7173
test("does not override a caller-provided User-Agent", async () => {
@@ -86,6 +88,7 @@ test("passes non-URL-parseable inputs through without tracking headers", async (
8688
const { url, headers } = await capture("/relative/path");
8789
expect(url).toBe("/relative/path");
8890
expect(headers.get("x-dashscope-source-config")).toBeNull();
91+
expect(headers.get("x-dashscope-openapisource")).toBeNull();
8992
});
9093

9194
test("uses kscli identity and version in source config", () => {

0 commit comments

Comments
 (0)