@huace-aigc/aigc-maas-client 0.1.0 → 0.1.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.
Files changed (2) hide show
  1. package/README.md +76 -17
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,10 +1,7 @@
1
1
  # Huace AIGC MaaS Node.js SDK
2
2
 
3
- Node.js 客户端用于接入 new-api MaaS 托管 Key 能力,支持申请或复用托管 Key、查询 Key
4
- 元信息和查询用量。
5
-
6
- 调用方或 Agent 请先阅读 [`SKILL.md`](SKILL.md);公共 HTTP 契约见
7
- [`../openapi/maas.yaml`](../openapi/maas.yaml)。
3
+ Node.js 客户端用于接入 Huace AIGC MaaS 托管 Key 能力,支持申请或复用托管 Key、查询
4
+ Key 元信息和查询用量。本文档随 npm 包一起发布,可直接作为接入指南。
8
5
 
9
6
  ## 安装
10
7
 
@@ -12,27 +9,89 @@ Node.js 客户端用于接入 new-api MaaS 托管 Key 能力,支持申请或
12
9
  npm install @huace-aigc/aigc-maas-client
13
10
  ```
14
11
 
15
- ## 使用
12
+ ## 前置条件
13
+
14
+ - Node.js 18 或更高版本。
15
+ - `baseUrl` 是 MaaS 服务地址,必须包含协议和主机名。
16
+ - `appId` 是已在 AIGC Auth 注册的正整数应用 ID,使用字符串传入。
17
+ - 服务端应用已启用,并且该应用恰好只有一个启用的 MaaS 路由。
18
+ - 用户的服务端人员映射已确认,否则会返回 `MAPPING_REVIEW_PENDING`。
19
+
20
+ ## 快速开始
16
21
 
17
22
  ```js
18
- import { MaaSClient } from "@huace-aigc/aigc-maas-client";
23
+ import { APIError, MaaSClient } from "@huace-aigc/aigc-maas-client";
24
+
25
+ const userToken = process.env.AIGC_AUTH_USER_TOKEN;
26
+ if (!userToken) {
27
+ throw new Error("AIGC_AUTH_USER_TOKEN is required");
28
+ }
19
29
 
20
30
  const client = new MaaSClient({
21
31
  baseUrl: "https://maas.example.com",
22
- appId: "12", // Numeric AIGC Auth application ID.
32
+ appId: "12",
23
33
  });
24
- const ensured = await client.ensureKey({ userToken: "auth-user-token" });
25
- const key = ensured.credential.apiKey;
26
- const info = await client.getKeyInfo(key);
27
- const usage = await client.getUsage(key);
34
+
35
+ try {
36
+ const ensured = await client.ensureKey({
37
+ userToken,
38
+ idempotencyKey: "user-42-provision-v1",
39
+ });
40
+ const apiKey = ensured.credential.apiKey;
41
+
42
+ const info = await client.getKeyInfo(apiKey);
43
+ const usage = await client.getUsage(apiKey);
44
+ console.log({ keyId: info.id, requestCount: usage.summary.count });
45
+
46
+ // 模型请求使用 ensured.credential.baseUrl + "/v1/chat/completions",
47
+ // 并设置 Authorization: Bearer <apiKey>。SDK 不代替模型协议客户端。
48
+ } catch (error) {
49
+ if (error instanceof APIError) {
50
+ console.error({ status: error.statusCode, code: error.code, message: error.message });
51
+ }
52
+ throw error;
53
+ }
28
54
  ```
29
55
 
30
- `userToken` 只在申请时传入,Client 不保存用户 Token。未提供幂等键时,SDK 会自动
31
- 生成 UUID,并在内部重试中复用。API Key 是敏感凭证,请通过 HTTPS 传输并避免写入
32
- 日志。Key 信息查询接口不会返回明文 Key。
56
+ `ensureKey` 返回 `state`、用户和应用信息,以及 `credential.apiKey`、
57
+ `credential.baseUrl`、`credential.models`。同一应用和用户会复用同一个托管 Key;未提供
58
+ `idempotencyKey` 时,SDK 自动生成 UUID,并在重试中复用。
59
+
60
+ ## 接口
61
+
62
+ SDK 对应以下 MaaS HTTP 接口:
63
+
64
+ - `POST /api/integrations/v1/keys/ensure`:使用 AIGC Auth 用户 Token 申请或复用托管 Key。
65
+ - `GET /api/integrations/v1/self/key`:使用托管 API Key 查询 Key 状态、模型、额度和归属。
66
+ - `GET /api/integrations/v1/self/usage`:使用托管 API Key 查询汇总及按模型用量。
33
67
 
34
- ## 配置
68
+ 申请请求会发送 `Authorization: Bearer <userToken>`、`X-AIGC-Auth-App-Id: <appId>` 和
69
+ `Idempotency-Key`。查询请求只发送 `Authorization: Bearer <apiKey>`。
70
+
71
+ ## 查询用量
72
+
73
+ 可以通过时间戳限制查询范围;不传字段时不会发送对应查询参数。返回值会规范化为
74
+ camelCase:
75
+
76
+ ```js
77
+ const usage = await client.getUsage(apiKey, {
78
+ startTimestamp: 1720000000,
79
+ endTimestamp: 1720086400,
80
+ });
81
+
82
+ console.log(usage.period.startTimestamp, usage.models);
83
+ ```
84
+
85
+ ## 配置和重试
35
86
 
36
87
  - `baseUrl` 和 `appId` 必填。
37
- - 默认请求超时为 10 秒,默认最多重试 2 次。
88
+ - 默认请求超时为 10 秒,可通过 `timeout` 配置。
89
+ - 默认最多重试 2 次,可通过 `maxRetries` 配置。
38
90
  - 仅网络错误、429 和 5xx 会自动重试,4xx 错误直接抛出 `APIError`。
91
+ - 可通过 `retryBaseDelay` 配置重试基础延迟,单位为毫秒。
92
+
93
+ ## 安全边界
94
+
95
+ - `userToken` 只传给 `ensureKey`,Client 不保存用户 Token。
96
+ - API Key 是敏感凭证,必须通过 HTTPS 传输并安全存储,禁止写入日志、URL、指标或错误上报。
97
+ - 不要向请求传递来源应用 Secret,也不要让浏览器直接持有托管 API Key。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@huace-aigc/aigc-maas-client",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Node.js client for the new-api MaaS managed-key API",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -17,6 +17,6 @@
17
17
  "node": ">=18"
18
18
  },
19
19
  "scripts": {
20
- "test": "node --test test/client.test.js"
20
+ "test": "node --test test/*.test.js"
21
21
  }
22
22
  }