@fuxi-ailabs/dsh-provider-auth 0.0.0-stage → 1.0.0

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 (3) hide show
  1. package/README.md +132 -2
  2. package/dist/index.js +273 -0
  3. package/package.json +51 -4
package/README.md CHANGED
@@ -1,3 +1,133 @@
1
- # Temporary Holding Version
1
+ # @fuxi-ailabs/dsh-provider-auth
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ DSH 插件共享的 **provider/auth resolver**。它不直接调用模型,也不保存密钥;只把
4
+ “某个消费者应该用哪一组 `baseUrl / apiKey / model`”按统一优先级解析出来。
5
+
6
+ ## 为什么单独拆插件
7
+
8
+ `dsh-insight` 的评分能力已经有一份 `显式配置 → 环境变量 → DSH credentials` 的解析逻辑。`dsh-audit`
9
+ 需要同样一套规则,但协议和默认值不同:
10
+
11
+ - `dsh-insight` 评分:OpenAI-compatible,默认 `https://api.deepseek.com`
12
+ - `dsh-audit`:Anthropic Messages,默认 `https://api.deepseek.com/anthropic`
13
+
14
+ 共享 resolver 只负责“从哪拿”,协议映射和探活仍留在各自插件。
15
+
16
+ ## 服务
17
+
18
+ 插件向宿主提供:
19
+
20
+ ```ts
21
+ ctx.provide('providerAuth', service)
22
+ ```
23
+
24
+ 消费方软取:
25
+
26
+ ```ts
27
+ const auth = ctx.get('providerAuth')
28
+ ```
29
+
30
+ ### `resolve(request)`
31
+
32
+ ```ts
33
+ const resolved = await auth.resolve({
34
+ id: 'sift',
35
+ protocol: 'anthropic-messages',
36
+ explicit: { baseUrl, apiKey, model },
37
+ env: {
38
+ baseUrl: ['DSH_SIFT_BASE_URL', 'ANTHROPIC_BASE_URL'],
39
+ apiKey: ['DSH_SIFT_AUTH_TOKEN', 'ANTHROPIC_AUTH_TOKEN'],
40
+ model: ['DSH_SIFT_MODEL', 'ANTHROPIC_MODEL'],
41
+ },
42
+ dsh: {
43
+ apiKeyRef: 'DEEPSEEK_API_KEY',
44
+ baseUrl: 'https://api.deepseek.com/anthropic',
45
+ model: 'deepseek-chat',
46
+ },
47
+ })
48
+ ```
49
+
50
+ 优先级:
51
+
52
+ ```text
53
+ 显式配置
54
+ > 环境变量
55
+ > DSH credentials
56
+ ```
57
+
58
+ ### `describe(request)`
59
+
60
+ 返回不包含密钥的状态:
61
+
62
+ ```ts
63
+ {
64
+ configured: true,
65
+ source: 'dsh-credentials',
66
+ sourceDetail: 'DSH credentials DEEPSEEK_API_KEY (file)',
67
+ maskedKey: 'sk-d…6789',
68
+ note: '来源:DSH credentials DEEPSEEK_API_KEY (file)',
69
+ skipped: []
70
+ }
71
+ ```
72
+
73
+ 供 `/score --probe-provider` 和后续独立的环境检查插件使用。
74
+
75
+ ### `mask(secret)`
76
+
77
+ 默认保留前 4 位和后 4 位;短 key 直接返回 `****`。
78
+
79
+ ## 配置
80
+
81
+ ```yaml
82
+ - id: dsh-provider-auth
83
+ name: '@fuxi-ailabs/dsh-provider-auth'
84
+ config:
85
+ allowDshCredentials: true
86
+ maskHead: 4
87
+ maskTail: 4
88
+ providers:
89
+ anthropic-messages:
90
+ baseUrl: https://api.deepseek.com/anthropic
91
+ model: deepseek-chat
92
+ apiKeyRef: DEEPSEEK_API_KEY
93
+ openai-chat:
94
+ baseUrl: https://api.deepseek.com
95
+ model: deepseek-chat
96
+ apiKeyRef: DEEPSEEK_API_KEY
97
+ ```
98
+
99
+ 消费插件可以只传协议,不再重复写 endpoint/model/key 引用:
100
+
101
+ ```ts
102
+ providerAuth.resolve({
103
+ id: 'sift',
104
+ protocol: 'anthropic-messages',
105
+ })
106
+ ```
107
+
108
+ 解析优先级:
109
+
110
+ ```text
111
+ 显式请求配置
112
+ > 环境变量
113
+ > providerAuth providers[protocol]
114
+ > 消费方传入的 DSH 兼容模板
115
+ ```
116
+
117
+ ## 边界
118
+
119
+ - 不保存密钥;密钥来自配置、环境变量或 DSH `credentials` 服务
120
+ - 不缓存密钥;每次 `resolve()` 现取现用
121
+ - 不做协议转换
122
+ - 不负责模型探活
123
+ - 不向 client 暴露 secret
124
+ - 不是 `dsh-insight` 评分能力 / `dsh-audit` 的硬依赖;两个消费方应在服务缺席时保留自己的 fallback
125
+
126
+ ## 开发
127
+
128
+ ```bash
129
+ npm install
130
+ npm test
131
+ npm run typecheck
132
+ npm run pack
133
+ ```
package/dist/index.js ADDED
@@ -0,0 +1,273 @@
1
+ // src/config.ts
2
+ var DEFAULT_PROVIDERS = {
3
+ "anthropic-messages": {
4
+ baseUrl: "https://api.deepseek.com/anthropic",
5
+ model: "deepseek-chat",
6
+ apiKeyRef: "DEEPSEEK_API_KEY"
7
+ },
8
+ "openai-chat": {
9
+ baseUrl: "https://api.deepseek.com",
10
+ model: "deepseek-chat",
11
+ apiKeyRef: "DEEPSEEK_API_KEY"
12
+ }
13
+ };
14
+ var defaultConfig = {
15
+ allowDshCredentials: true,
16
+ maskHead: 4,
17
+ maskTail: 4,
18
+ providers: DEFAULT_PROVIDERS
19
+ };
20
+ function nonNegativeInt(value, fallback) {
21
+ return typeof value === "number" && Number.isInteger(value) && value >= 0 ? value : fallback;
22
+ }
23
+ function normalizeProfiles(input) {
24
+ const given = input ?? {};
25
+ const out = {};
26
+ const protocols = ["anthropic-messages", "openai-chat"];
27
+ for (const protocol of protocols) {
28
+ const base = DEFAULT_PROVIDERS[protocol] ?? {};
29
+ const override = given[protocol] ?? {};
30
+ out[protocol] = { ...base, ...override };
31
+ }
32
+ return out;
33
+ }
34
+ function normalizeConfig(input) {
35
+ const given = input ?? {};
36
+ return {
37
+ allowDshCredentials: given.allowDshCredentials ?? defaultConfig.allowDshCredentials,
38
+ maskHead: nonNegativeInt(given.maskHead, defaultConfig.maskHead),
39
+ maskTail: nonNegativeInt(given.maskTail, defaultConfig.maskTail),
40
+ providers: normalizeProfiles(given.providers)
41
+ };
42
+ }
43
+ function maskSecret(secret, config = defaultConfig) {
44
+ const head = nonNegativeInt(config.maskHead, defaultConfig.maskHead);
45
+ const tail = nonNegativeInt(config.maskTail, defaultConfig.maskTail);
46
+ if (head === 0 && tail === 0) return "****";
47
+ if (secret.length <= head + tail + 1) return "****";
48
+ return `${head > 0 ? secret.slice(0, head) : ""}\u2026${tail > 0 ? secret.slice(-tail) : ""}`;
49
+ }
50
+
51
+ // src/core/resolve.ts
52
+ var VALUE_KEYS = ["baseUrl", "apiKey", "model"];
53
+ function trim(value) {
54
+ return typeof value === "string" ? value.trim() : "";
55
+ }
56
+ function pickEnv(env, names) {
57
+ for (const name2 of names ?? []) {
58
+ const value = trim(env[name2]);
59
+ if (value.length > 0) return { value, key: name2 };
60
+ }
61
+ return { value: "", key: "" };
62
+ }
63
+ function missing(values) {
64
+ return VALUE_KEYS.filter((key) => values[key].length === 0);
65
+ }
66
+ function hasAny(values) {
67
+ return VALUE_KEYS.some((key) => values[key].length > 0);
68
+ }
69
+ function result(request, values, source, sourceDetail) {
70
+ return {
71
+ id: request.id,
72
+ protocol: request.protocol,
73
+ baseUrl: values.baseUrl,
74
+ apiKey: values.apiKey,
75
+ model: values.model,
76
+ source,
77
+ sourceDetail,
78
+ note: `\u6765\u6E90:${sourceDetail}`
79
+ };
80
+ }
81
+ async function resolveProviderAuth(request, options) {
82
+ if (request.id.trim().length === 0) throw new Error("providerAuth: request.id must be non-empty");
83
+ if (request.protocol !== "openai-chat" && request.protocol !== "anthropic-messages") {
84
+ throw new Error(`providerAuth: unsupported protocol ${String(request.protocol)}`);
85
+ }
86
+ const skipped = [];
87
+ const explicit = {
88
+ baseUrl: trim(request.explicit?.baseUrl),
89
+ apiKey: trim(request.explicit?.apiKey),
90
+ model: trim(request.explicit?.model)
91
+ };
92
+ if (hasAny(explicit)) {
93
+ const miss = missing(explicit);
94
+ if (miss.length === 0) return { result: result(request, explicit, "config", `\u63D2\u4EF6\u663E\u5F0F\u914D\u7F6E(${request.id})`), skipped };
95
+ skipped.push(`\u663E\u5F0F\u914D\u7F6E\u4E0D\u5B8C\u6574,\u7F3A ${miss.join("/")}`);
96
+ }
97
+ const envSource = request.env ?? {};
98
+ const envPicks = {
99
+ baseUrl: pickEnv(options.env ?? process.env, envSource.baseUrl),
100
+ apiKey: pickEnv(options.env ?? process.env, envSource.apiKey),
101
+ model: pickEnv(options.env ?? process.env, envSource.model)
102
+ };
103
+ const envValues = {
104
+ baseUrl: envPicks.baseUrl.value,
105
+ apiKey: envPicks.apiKey.value,
106
+ model: envPicks.model.value
107
+ };
108
+ if (hasAny(envValues)) {
109
+ const miss = missing(envValues);
110
+ if (miss.length === 0) {
111
+ const keys = `${envPicks.baseUrl.key} / ${envPicks.apiKey.key} / ${envPicks.model.key}`;
112
+ return { result: result(request, envValues, "env", `\u73AF\u5883\u53D8\u91CF ${keys}`), skipped };
113
+ }
114
+ skipped.push(`\u73AF\u5883\u53D8\u91CF\u4E0D\u5B8C\u6574,\u7F3A ${miss.join("/")}`);
115
+ }
116
+ if (!options.allowDshCredentials) {
117
+ skipped.push("DSH \u51ED\u636E\u501F\u7528\u5DF2\u5173\u95ED");
118
+ return { result: null, skipped };
119
+ }
120
+ if (!options.credentials || typeof options.credentials.resolve !== "function") {
121
+ skipped.push("\u5BBF\u4E3B\u672A\u6302\u8F7D credentials \u670D\u52A1");
122
+ return { result: null, skipped };
123
+ }
124
+ const profile = options.providers?.[request.protocol];
125
+ if (profile) {
126
+ const profileBaseUrl = trim(profile.baseUrl);
127
+ const profileModel = trim(profile.model);
128
+ const profileRef = trim(profile.apiKeyRef);
129
+ const profileMiss = [];
130
+ if (profileBaseUrl.length === 0) profileMiss.push("baseUrl");
131
+ if (profileModel.length === 0) profileMiss.push("model");
132
+ if (profileRef.length === 0) profileMiss.push("apiKeyRef");
133
+ if (profileMiss.length === 0) {
134
+ try {
135
+ const hit = await options.credentials.resolve(profileRef);
136
+ const key = trim(hit?.value);
137
+ if (key.length > 0) {
138
+ const values = { baseUrl: profileBaseUrl, apiKey: key, model: profileModel };
139
+ return {
140
+ result: result(
141
+ request,
142
+ values,
143
+ "dsh-credentials",
144
+ `provider profile ${request.protocol} \u2192 DSH credentials ${profileRef}${hit?.source ? ` (${hit.source})` : ""}`
145
+ ),
146
+ skipped
147
+ };
148
+ }
149
+ skipped.push(`provider profile \u672A\u8BFB\u5230 DSH \u51ED\u636E ${profileRef}`);
150
+ } catch (error) {
151
+ skipped.push(`\u8BFB\u53D6 provider profile \u51ED\u636E\u5931\u8D25: ${error instanceof Error ? error.message : String(error)}`);
152
+ }
153
+ } else {
154
+ skipped.push(`provider profile ${request.protocol} \u4E0D\u5B8C\u6574,\u7F3A ${profileMiss.join("/")}`);
155
+ }
156
+ } else {
157
+ skipped.push(`\u672A\u914D\u7F6E provider profile ${request.protocol}`);
158
+ }
159
+ if (!request.dsh) {
160
+ skipped.push("\u672A\u63D0\u4F9B DSH \u51ED\u636E\u6A21\u677F");
161
+ return { result: null, skipped };
162
+ }
163
+ const dshBaseUrl = trim(request.dsh.baseUrl);
164
+ const dshModel = trim(request.dsh.model);
165
+ const ref = trim(request.dsh.apiKeyRef);
166
+ const dshMiss = [];
167
+ if (dshBaseUrl.length === 0) dshMiss.push("baseUrl");
168
+ if (dshModel.length === 0) dshMiss.push("model");
169
+ if (ref.length === 0) dshMiss.push("apiKeyRef");
170
+ if (dshMiss.length > 0) {
171
+ skipped.push(`DSH \u51ED\u636E\u6A21\u677F\u4E0D\u5B8C\u6574,\u7F3A ${dshMiss.join("/")}`);
172
+ return { result: null, skipped };
173
+ }
174
+ const dshValues = {
175
+ baseUrl: dshBaseUrl,
176
+ apiKey: "",
177
+ model: dshModel
178
+ };
179
+ try {
180
+ const hit = await options.credentials.resolve(ref);
181
+ const key = trim(hit?.value);
182
+ if (key.length === 0) {
183
+ skipped.push(`\u672A\u8BFB\u5230 DSH \u51ED\u636E ${ref}`);
184
+ return { result: null, skipped };
185
+ }
186
+ dshValues.apiKey = key;
187
+ return {
188
+ result: result(request, dshValues, "dsh-credentials", `DSH credentials ${ref}${hit?.source ? ` (${hit.source})` : ""}`),
189
+ skipped
190
+ };
191
+ } catch (error) {
192
+ skipped.push(`\u8BFB\u53D6 DSH \u51ED\u636E\u5931\u8D25: ${error instanceof Error ? error.message : String(error)}`);
193
+ return { result: null, skipped };
194
+ }
195
+ }
196
+
197
+ // src/service.ts
198
+ var PROVIDER_AUTH_SERVICE_NAME = "providerAuth";
199
+ function credentialsOf(ctx) {
200
+ const value = ctx.get("credentials");
201
+ return value && typeof value.resolve === "function" ? value : void 0;
202
+ }
203
+ function createProviderAuthService(ctx, config, env = process.env) {
204
+ const resolve = async (request) => {
205
+ const outcome = await resolveProviderAuth(request, {
206
+ allowDshCredentials: config.allowDshCredentials,
207
+ credentials: credentialsOf(ctx),
208
+ env,
209
+ providers: config.providers
210
+ });
211
+ return outcome.result;
212
+ };
213
+ const mask = (secret) => maskSecret(secret, config);
214
+ return {
215
+ resolve,
216
+ async describe(request) {
217
+ const outcome = await resolveProviderAuth(request, {
218
+ allowDshCredentials: config.allowDshCredentials,
219
+ credentials: credentialsOf(ctx),
220
+ env,
221
+ providers: config.providers
222
+ });
223
+ const hit = outcome.result;
224
+ if (hit) {
225
+ return {
226
+ id: request.id,
227
+ protocol: request.protocol,
228
+ configured: true,
229
+ source: hit.source,
230
+ sourceDetail: hit.sourceDetail,
231
+ maskedKey: mask(hit.apiKey),
232
+ note: hit.note,
233
+ skipped: outcome.skipped
234
+ };
235
+ }
236
+ return {
237
+ id: request.id,
238
+ protocol: request.protocol,
239
+ configured: false,
240
+ note: outcome.skipped.length > 0 ? `\u672A\u89E3\u6790\u5230\u5B8C\u6574 provider auth: ${outcome.skipped.join("; ")}` : "\u672A\u914D\u7F6E provider auth",
241
+ skipped: outcome.skipped
242
+ };
243
+ },
244
+ mask
245
+ };
246
+ }
247
+
248
+ // src/index.ts
249
+ var name = "dsh-provider-auth";
250
+ var inject = [];
251
+ function apply(ctx, config) {
252
+ if (typeof ctx.provide !== "function") {
253
+ throw new Error("dsh-provider-auth: \u5BBF\u4E3B ctx \u7F3A\u5C11 provide(),\u65E0\u6CD5\u6CE8\u518C providerAuth \u670D\u52A1");
254
+ }
255
+ const resolved = normalizeConfig(config);
256
+ const service = createProviderAuthService(ctx, resolved);
257
+ const register = () => ctx.provide(PROVIDER_AUTH_SERVICE_NAME, service);
258
+ if (typeof ctx.effect === "function") ctx.effect(register, "dsh-provider-auth: service");
259
+ else register();
260
+ process.stderr.write(`[dsh-provider-auth] ready \u2014 service=${PROVIDER_AUTH_SERVICE_NAME} borrowDsh=${resolved.allowDshCredentials}
261
+ `);
262
+ }
263
+ export {
264
+ PROVIDER_AUTH_SERVICE_NAME,
265
+ apply,
266
+ createProviderAuthService,
267
+ defaultConfig,
268
+ inject,
269
+ maskSecret,
270
+ name,
271
+ normalizeConfig,
272
+ resolveProviderAuth
273
+ };
package/package.json CHANGED
@@ -1,6 +1,53 @@
1
1
  {
2
2
  "name": "@fuxi-ailabs/dsh-provider-auth",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "private": false,
5
+ "type": "module",
6
+ "description": "Shared provider/auth resolver for DSH plugins: explicit config, environment variables, and DSH credentials",
7
+ "main": "dist/index.js",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "dsh": {
12
+ "manifestVersion": 1
13
+ },
14
+ "scripts": {
15
+ "build": "esbuild src/index.ts --bundle --platform=node --format=esm --target=node20 --outfile=dist/index.js",
16
+ "typecheck": "tsc --noEmit",
17
+ "test": "npm run build && node tests/smoke.mjs",
18
+ "pack": "npm run build && npm pack",
19
+ "prepack": "npm run build"
20
+ },
21
+ "devDependencies": {
22
+ "@types/node": "^20.19.43",
23
+ "esbuild": "^0.25.12",
24
+ "typescript": "^5.9.3"
25
+ },
26
+ "license": "UNLICENSED",
27
+ "exports": {
28
+ ".": "./dist/index.js"
29
+ },
30
+ "author": "Fuxi AI Labs",
31
+ "homepage": "https://fuxi-ailabs.github.io/dsh-plugin-market/plugins/insight/",
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/guixu-labs/dsh-plugin.git",
35
+ "directory": "packages/dsh-provider-auth"
36
+ },
37
+ "keywords": [
38
+ "dsh",
39
+ "dsh-plugin",
40
+ "provider-auth",
41
+ "credentials"
42
+ ],
43
+ "engines": {
44
+ "dsh": "^0.2.1-rc.2",
45
+ "node": ">=24"
46
+ },
47
+ "publishConfig": {
48
+ "access": "public"
49
+ },
50
+ "peerDependencies": {
51
+ "@deepseek-ai/cordis": "~4.0.5"
52
+ }
53
+ }