@unieai/rabi-llm-deepseek-api-key 0.0.0-stage → 0.2.2-rc.5
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.
- package/LICENSE +21 -0
- package/README.md +7 -2
- package/lib/index.js +154 -0
- package/lib/types/config.d.ts +82 -0
- package/lib/types/index.d.ts +9 -0
- package/locale/de.json +6 -0
- package/locale/en.json +5 -0
- package/locale/es.json +6 -0
- package/locale/fr.json +6 -0
- package/locale/ja.json +6 -0
- package/locale/pt.json +6 -0
- package/locale/ru.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +44 -4
- package/src/config.ts +61 -0
- package/src/index.ts +124 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 DeepSeek
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @unieai/rabi-llm-deepseek-api-key
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
DeepSeek api-key provider authentication and discovery
|
|
4
|
+
|
|
5
|
+
Part of Rabi, the agent harness UnieAI Code runs on. Installed as a dependency of `@unieai/rabi`;
|
|
6
|
+
not meant to be used on its own.
|
|
7
|
+
|
|
8
|
+
See LICENSE for the license and copyright of this package.
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { APP_IDENTITY, LlmError } from "@unieai/rabi-llm";
|
|
2
|
+
import { launchEnvironmentOf } from "@unieai/rabi-launch-environment";
|
|
3
|
+
import { PUBLIC_BASE_URL, catalogModelInfo, createModelListingCache, deepSeekConfigFields, isBuiltInCatalog, modelListingConfigFields, modelListingURL, plainOptions as plainOptions$1, refuseListedModel, registerDeepSeekProvider, requestModelListing, resolveAdapterOptions as resolveAdapterOptions$1, resolveDeepSeekApiKey, resolveListedCatalog, resolveListingTiming } from "@unieai/rabi-llm-deepseek";
|
|
4
|
+
import z from "@unieai/schemastery";
|
|
5
|
+
import { credentialRef } from "@unieai/rabi-credentials";
|
|
6
|
+
//#region lib/types/config.js
|
|
7
|
+
/** Origin of the official API, the only endpoint whose `/models` listing this route follows. */
|
|
8
|
+
const OFFICIAL_ORIGIN = new URL(PUBLIC_BASE_URL).origin;
|
|
9
|
+
const Config = z.object({
|
|
10
|
+
...deepSeekConfigFields,
|
|
11
|
+
...modelListingConfigFields,
|
|
12
|
+
apiKeyEnv: z.string().role("credential-ref").default("DEEPSEEK_API_KEY").volatile()
|
|
13
|
+
});
|
|
14
|
+
/** Read one validated provider configuration.
|
|
15
|
+
* @param config - live plugin configuration.
|
|
16
|
+
* @returns detached resolver inputs.
|
|
17
|
+
*/
|
|
18
|
+
function plainOptions(config) {
|
|
19
|
+
return {
|
|
20
|
+
...plainOptions$1(config),
|
|
21
|
+
apiKeyEnv: config.apiKeyEnv.get()
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/** Resolve API-key, listing, and protocol settings together.
|
|
25
|
+
* @param config - raw deployment settings.
|
|
26
|
+
* @param environment - application launch environment.
|
|
27
|
+
* @returns validated endpoint facts, the matching credential reference, and the listing the catalog follows.
|
|
28
|
+
*/
|
|
29
|
+
function resolveAdapterOptions(config, environment) {
|
|
30
|
+
const { modelsTimeoutMs: _timeout, modelsMaxAgeMs: _maxAge, apiKeyEnv, ...messages } = config;
|
|
31
|
+
const timing = resolveListingTiming(config, "llm-deepseek-api-key");
|
|
32
|
+
const connection = resolveAdapterOptions$1(messages, environment);
|
|
33
|
+
const listed = new URL(connection.baseURL).origin === OFFICIAL_ORIGIN && isBuiltInCatalog(connection.models);
|
|
34
|
+
return {
|
|
35
|
+
...connection,
|
|
36
|
+
apiKeyEnv: credentialRef(apiKeyEnv ?? "DEEPSEEK_API_KEY"),
|
|
37
|
+
...listed ? { listing: {
|
|
38
|
+
url: modelListingURL(connection.baseURL),
|
|
39
|
+
...timing
|
|
40
|
+
} } : {}
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region lib/types/index.js
|
|
45
|
+
const name = "llm-deepseek-api-key";
|
|
46
|
+
const inject = ["llm"];
|
|
47
|
+
const PROVIDER = "deepseek-official";
|
|
48
|
+
/** Console tag of listing diagnostics; product profiles mount no console exporter for `ctx.logger`. */
|
|
49
|
+
const LOG_TAG = "[llm-deepseek-api-key]";
|
|
50
|
+
function apply(ctx, config) {
|
|
51
|
+
const options = () => resolveAdapterOptions(plainOptions(config), launchEnvironmentOf(ctx));
|
|
52
|
+
options();
|
|
53
|
+
const resolveApiKey = async (connection) => {
|
|
54
|
+
const ref = connection.apiKeyEnv;
|
|
55
|
+
const key = await resolveDeepSeekApiKey(ctx, ref);
|
|
56
|
+
if (key !== void 0) return key;
|
|
57
|
+
throw new LlmError(`llm-deepseek: no API key for provider route "${PROVIDER}"; store ${ref} through the credentials service (the web Models page writes it), or export ${ref} in the launching environment`, "MISSING_CREDENTIAL");
|
|
58
|
+
};
|
|
59
|
+
/** The API key, or `undefined` when none is configured; other credential failures propagate. */
|
|
60
|
+
const presentApiKey = async (connection) => {
|
|
61
|
+
try {
|
|
62
|
+
return await resolveApiKey(connection);
|
|
63
|
+
} catch (error) {
|
|
64
|
+
if (error instanceof LlmError && error.code === "MISSING_CREDENTIAL") return void 0;
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const listings = createModelListingCache(ctx, name, PROVIDER, {
|
|
69
|
+
settings: () => options().listing,
|
|
70
|
+
credential: async () => presentApiKey(options()),
|
|
71
|
+
load: (url, apiKey, signal) => requestModelListing(url, { "x-api-key": apiKey }, signal)
|
|
72
|
+
});
|
|
73
|
+
let reportedListing;
|
|
74
|
+
/** Resolve the catalog, reporting each listing's hidden entries and dropped badges once. */
|
|
75
|
+
const connectionOf = (connection, view) => {
|
|
76
|
+
if (view === void 0) return {
|
|
77
|
+
...connection,
|
|
78
|
+
offered: connection.models,
|
|
79
|
+
refusals: /* @__PURE__ */ new Map(),
|
|
80
|
+
badges: /* @__PURE__ */ new Map()
|
|
81
|
+
};
|
|
82
|
+
const catalog = resolveListedCatalog({
|
|
83
|
+
listing: view.listing?.entries,
|
|
84
|
+
supported: connection.models,
|
|
85
|
+
overrides: {},
|
|
86
|
+
routeMaxTokens: connection.maxTokens,
|
|
87
|
+
clientVersion: APP_IDENTITY.version
|
|
88
|
+
});
|
|
89
|
+
if (view.listing !== reportedListing) {
|
|
90
|
+
reportedListing = view.listing;
|
|
91
|
+
for (const problem of catalog.problems) console.warn(LOG_TAG, problem);
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
...connection,
|
|
95
|
+
models: catalog.resolution,
|
|
96
|
+
offered: catalog.offered,
|
|
97
|
+
refusals: catalog.refusals,
|
|
98
|
+
badges: catalog.badges
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
ctx.llm.registerConfigurableProviders([{
|
|
102
|
+
provider: PROVIDER,
|
|
103
|
+
displayName: "DeepSeek",
|
|
104
|
+
settingsNs: ctx.fiber.entry?.options.id ?? "llm-deepseek-api-key",
|
|
105
|
+
settingsPath: []
|
|
106
|
+
}]);
|
|
107
|
+
registerDeepSeekProvider(ctx, PROVIDER, {
|
|
108
|
+
providerName: "DeepSeek",
|
|
109
|
+
options: () => {
|
|
110
|
+
const connection = options();
|
|
111
|
+
return connectionOf(connection, connection.listing === void 0 ? void 0 : listings.current());
|
|
112
|
+
},
|
|
113
|
+
connect: async (signal) => {
|
|
114
|
+
const connection = options();
|
|
115
|
+
if (connection.listing === void 0) return connectionOf(connection, void 0);
|
|
116
|
+
let view;
|
|
117
|
+
try {
|
|
118
|
+
view = await listings.view(connection.listing, signal);
|
|
119
|
+
} catch (error) {
|
|
120
|
+
if (signal?.aborted === true) throw error;
|
|
121
|
+
return connectionOf(connection, void 0);
|
|
122
|
+
}
|
|
123
|
+
return connectionOf(connection, view);
|
|
124
|
+
},
|
|
125
|
+
resolveAuth: async (connection) => ({ headers: { "x-api-key": await resolveApiKey(connection) } }),
|
|
126
|
+
admit: (connection, model) => {
|
|
127
|
+
refuseListedModel(model, connection.refusals.get(model));
|
|
128
|
+
},
|
|
129
|
+
discoverModels: async (provider) => {
|
|
130
|
+
const connection = options();
|
|
131
|
+
if (connection.listing === void 0) {
|
|
132
|
+
if (await presentApiKey(connection) === void 0) return [];
|
|
133
|
+
return connection.models.map((model) => catalogModelInfo(provider, model));
|
|
134
|
+
}
|
|
135
|
+
const view = await listings.view(connection.listing);
|
|
136
|
+
if (!view.authenticated) return [];
|
|
137
|
+
const { offered, badges } = connectionOf(connection, view);
|
|
138
|
+
return offered.map((model) => catalogModelInfo(provider, model, badges.get(model.id)));
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
let following = options().listing !== void 0;
|
|
142
|
+
ctx.on("loader/volatile-update", () => {
|
|
143
|
+
let next;
|
|
144
|
+
try {
|
|
145
|
+
next = options().listing !== void 0;
|
|
146
|
+
} catch (_rejectedConfiguration) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (next !== following) listings.invalidate();
|
|
150
|
+
following = next;
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
//#endregion
|
|
154
|
+
export { Config, apply, inject, name, plainOptions, resolveAdapterOptions };
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/** API-key configuration resolved together with one Messages endpoint generation and its model listing. */
|
|
2
|
+
import type { Volatile } from '@unieai/cordis';
|
|
3
|
+
import z from '@unieai/schemastery';
|
|
4
|
+
import { type CredentialRef } from '@unieai/rabi-credentials';
|
|
5
|
+
import type { LaunchEnvironmentSnapshot } from '@unieai/rabi-launch-environment';
|
|
6
|
+
import type { Config as ProtocolConfig, DeepSeekConnectionOptions, ListingSettings, ModelListingConfig, ModelListingOptions, Options as ProtocolOptions } from '@unieai/rabi-llm-deepseek';
|
|
7
|
+
/** Messages configuration with a per-request API-key reference. */
|
|
8
|
+
export interface Config extends ProtocolConfig, ModelListingConfig {
|
|
9
|
+
/** Credential reference resolved per request; defaults to DEEPSEEK_API_KEY. */
|
|
10
|
+
apiKeyEnv: Volatile<string>;
|
|
11
|
+
}
|
|
12
|
+
export declare const Config: z<Schemastery.ObjectS<NoInfer<{
|
|
13
|
+
apiKeyEnv: z<string, string, "volatile-defined">;
|
|
14
|
+
modelsTimeoutMs: z<number, number, "volatile-defined">;
|
|
15
|
+
modelsMaxAgeMs: z<number, number, "volatile-defined">;
|
|
16
|
+
baseURL: z<string, string, "volatile">;
|
|
17
|
+
thinking: z<"enabled" | "disabled", "enabled" | "disabled", "volatile">;
|
|
18
|
+
reasoningEffort: z<"low" | "off" | "high" | "max", "low" | "off" | "high" | "max", "volatile">;
|
|
19
|
+
maxTokens: z<number, number, "volatile-defined">;
|
|
20
|
+
defaultContextWindow: z<number, number, "volatile-defined">;
|
|
21
|
+
models: z<NoInfer<import("@unieai/rabi-llm-deepseek").DeepSeekCatalogModel[]>, NoInfer<import("@unieai/rabi-llm-deepseek").DeepSeekCatalogModel[]>, "volatile-defined">;
|
|
22
|
+
streamIdleTimeoutMs: z<number, number, "volatile-defined">;
|
|
23
|
+
maxRequestFilesBytes: z<number, number, "volatile-defined">;
|
|
24
|
+
maxInlineRequestImageBytes: z<number, number, "volatile-defined">;
|
|
25
|
+
maxImagesPerRequest: z<number, number, "volatile-defined">;
|
|
26
|
+
imageOffloadByteQuantum: z<number, number, "volatile-defined">;
|
|
27
|
+
inlineImageOffloadByteQuantum: z<number, number, "volatile-defined">;
|
|
28
|
+
imageOffloadCountQuantum: z<number, number, "volatile-defined">;
|
|
29
|
+
filesApiTimeoutMs: z<number, number, "volatile-defined">;
|
|
30
|
+
fileExpiresAfterSeconds: z<number, number, "volatile-defined">;
|
|
31
|
+
fileRefreshMarginSeconds: z<number, number, "volatile-defined">;
|
|
32
|
+
fileQuotaCleanupBatch: z<number, number, "volatile-defined">;
|
|
33
|
+
retryPolicy: z<NoInfer<import("@unieai/rabi-llm").RetryPolicyConfig>, NoInfer<import("@unieai/rabi-llm").RetryPolicyConfig>, "volatile">;
|
|
34
|
+
}>>, Schemastery.ObjectT<NoInfer<{
|
|
35
|
+
apiKeyEnv: z<string, string, "volatile-defined">;
|
|
36
|
+
modelsTimeoutMs: z<number, number, "volatile-defined">;
|
|
37
|
+
modelsMaxAgeMs: z<number, number, "volatile-defined">;
|
|
38
|
+
baseURL: z<string, string, "volatile">;
|
|
39
|
+
thinking: z<"enabled" | "disabled", "enabled" | "disabled", "volatile">;
|
|
40
|
+
reasoningEffort: z<"low" | "off" | "high" | "max", "low" | "off" | "high" | "max", "volatile">;
|
|
41
|
+
maxTokens: z<number, number, "volatile-defined">;
|
|
42
|
+
defaultContextWindow: z<number, number, "volatile-defined">;
|
|
43
|
+
models: z<NoInfer<import("@unieai/rabi-llm-deepseek").DeepSeekCatalogModel[]>, NoInfer<import("@unieai/rabi-llm-deepseek").DeepSeekCatalogModel[]>, "volatile-defined">;
|
|
44
|
+
streamIdleTimeoutMs: z<number, number, "volatile-defined">;
|
|
45
|
+
maxRequestFilesBytes: z<number, number, "volatile-defined">;
|
|
46
|
+
maxInlineRequestImageBytes: z<number, number, "volatile-defined">;
|
|
47
|
+
maxImagesPerRequest: z<number, number, "volatile-defined">;
|
|
48
|
+
imageOffloadByteQuantum: z<number, number, "volatile-defined">;
|
|
49
|
+
inlineImageOffloadByteQuantum: z<number, number, "volatile-defined">;
|
|
50
|
+
imageOffloadCountQuantum: z<number, number, "volatile-defined">;
|
|
51
|
+
filesApiTimeoutMs: z<number, number, "volatile-defined">;
|
|
52
|
+
fileExpiresAfterSeconds: z<number, number, "volatile-defined">;
|
|
53
|
+
fileRefreshMarginSeconds: z<number, number, "volatile-defined">;
|
|
54
|
+
fileQuotaCleanupBatch: z<number, number, "volatile-defined">;
|
|
55
|
+
retryPolicy: z<NoInfer<import("@unieai/rabi-llm").RetryPolicyConfig>, NoInfer<import("@unieai/rabi-llm").RetryPolicyConfig>, "volatile">;
|
|
56
|
+
}>>, "plain">;
|
|
57
|
+
/** Plain deployment inputs for the API-key provider. */
|
|
58
|
+
export type Options = ProtocolOptions & ModelListingOptions & {
|
|
59
|
+
apiKeyEnv?: string;
|
|
60
|
+
};
|
|
61
|
+
/** Endpoint and credential reference captured from the same configuration generation. */
|
|
62
|
+
export interface ResolvedDeepSeekOptions extends DeepSeekConnectionOptions {
|
|
63
|
+
/** Credential reference used only for this connection snapshot. */
|
|
64
|
+
apiKeyEnv: CredentialRef;
|
|
65
|
+
/**
|
|
66
|
+
* The official `/models` listing this generation follows; absent when `models`
|
|
67
|
+
* differs from the built-in catalog or the endpoint is not the official API.
|
|
68
|
+
*/
|
|
69
|
+
listing?: ListingSettings;
|
|
70
|
+
}
|
|
71
|
+
/** Read one validated provider configuration.
|
|
72
|
+
* @param config - live plugin configuration.
|
|
73
|
+
* @returns detached resolver inputs.
|
|
74
|
+
*/
|
|
75
|
+
export declare function plainOptions(config: Config): Options;
|
|
76
|
+
/** Resolve API-key, listing, and protocol settings together.
|
|
77
|
+
* @param config - raw deployment settings.
|
|
78
|
+
* @param environment - application launch environment.
|
|
79
|
+
* @returns validated endpoint facts, the matching credential reference, and the listing the catalog follows.
|
|
80
|
+
*/
|
|
81
|
+
export declare function resolveAdapterOptions(config: Options, environment?: LaunchEnvironmentSnapshot): ResolvedDeepSeekOptions;
|
|
82
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** API-key authentication, credential-gated discovery, and the official model listing for the official DeepSeek route. */
|
|
2
|
+
import type { Context } from '@unieai/cordis';
|
|
3
|
+
import { Config } from './config.ts';
|
|
4
|
+
export { Config, plainOptions, resolveAdapterOptions } from './config.ts';
|
|
5
|
+
export type { Options, ResolvedDeepSeekOptions } from './config.ts';
|
|
6
|
+
export declare const name = "llm-deepseek-api-key";
|
|
7
|
+
export declare const inject: string[];
|
|
8
|
+
export declare function apply(ctx: Context, config: Config): void;
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
package/locale/de.json
ADDED
package/locale/en.json
ADDED
package/locale/es.json
ADDED
package/locale/fr.json
ADDED
package/locale/ja.json
ADDED
package/locale/pt.json
ADDED
package/locale/ru.json
ADDED
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unieai/rabi-llm-deepseek-api-key",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"description": "DeepSeek api-key provider authentication and discovery",
|
|
4
|
+
"version": "0.2.2-rc.5",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "lib/index.js",
|
|
10
|
+
"types": "lib/types/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./lib/types/index.d.ts",
|
|
14
|
+
"default": "./lib/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./src/*": "./src/*",
|
|
17
|
+
"./package.json": "./package.json",
|
|
18
|
+
"./locale/*.json": "./locale/*.json"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"locale/*.json",
|
|
22
|
+
"lib/index.js",
|
|
23
|
+
"lib/types/**/*.d.ts",
|
|
24
|
+
"lib/types/**/*.d.ts.map",
|
|
25
|
+
"lib/**/*.js.map",
|
|
26
|
+
"src",
|
|
27
|
+
"README.md"
|
|
28
|
+
],
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"@unieai/cordis": "4.1.0",
|
|
32
|
+
"@unieai/rabi-llm": "0.2.2-rc.5",
|
|
33
|
+
"@unieai/rabi-llm-deepseek": "0.2.2-rc.5",
|
|
34
|
+
"@unieai/rabi-launch-environment": "0.2.2-rc.5",
|
|
35
|
+
"@unieai/rabi-credentials": "0.2.2-rc.5",
|
|
36
|
+
"@unieai/cordis-plugin-loader": "1.1.0"
|
|
37
|
+
},
|
|
38
|
+
"peerDependenciesMeta": {
|
|
39
|
+
"@unieai/cordis-plugin-loader": {
|
|
40
|
+
"optional": true
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@unieai/schemastery": "3.19.0"
|
|
45
|
+
}
|
|
46
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/** API-key configuration resolved together with one Messages endpoint generation and its model listing. */
|
|
2
|
+
import type { Volatile } from '@unieai/cordis'
|
|
3
|
+
import z from '@unieai/schemastery'
|
|
4
|
+
import { credentialRef, type CredentialRef } from '@unieai/rabi-credentials'
|
|
5
|
+
import type { LaunchEnvironmentSnapshot } from '@unieai/rabi-launch-environment'
|
|
6
|
+
import {
|
|
7
|
+
deepSeekConfigFields, isBuiltInCatalog, modelListingConfigFields, modelListingURL, plainOptions as protocolOptions, PUBLIC_BASE_URL,
|
|
8
|
+
resolveAdapterOptions as resolveProtocolOptions, resolveListingTiming,
|
|
9
|
+
} from '@unieai/rabi-llm-deepseek'
|
|
10
|
+
import type {
|
|
11
|
+
Config as ProtocolConfig, DeepSeekConnectionOptions, ListingSettings, ModelListingConfig, ModelListingOptions, Options as ProtocolOptions,
|
|
12
|
+
} from '@unieai/rabi-llm-deepseek'
|
|
13
|
+
|
|
14
|
+
/** Origin of the official API, the only endpoint whose `/models` listing this route follows. */
|
|
15
|
+
const OFFICIAL_ORIGIN = new URL(PUBLIC_BASE_URL).origin
|
|
16
|
+
|
|
17
|
+
/** Messages configuration with a per-request API-key reference. */
|
|
18
|
+
export interface Config extends ProtocolConfig, ModelListingConfig {
|
|
19
|
+
/** Credential reference resolved per request; defaults to DEEPSEEK_API_KEY. */
|
|
20
|
+
apiKeyEnv: Volatile<string>
|
|
21
|
+
}
|
|
22
|
+
export const Config = z.object({
|
|
23
|
+
...deepSeekConfigFields,
|
|
24
|
+
...modelListingConfigFields,
|
|
25
|
+
apiKeyEnv: z.string().role('credential-ref').default('DEEPSEEK_API_KEY').volatile(),
|
|
26
|
+
})
|
|
27
|
+
/** Plain deployment inputs for the API-key provider. */
|
|
28
|
+
export type Options = ProtocolOptions & ModelListingOptions & { apiKeyEnv?: string }
|
|
29
|
+
/** Endpoint and credential reference captured from the same configuration generation. */
|
|
30
|
+
export interface ResolvedDeepSeekOptions extends DeepSeekConnectionOptions {
|
|
31
|
+
/** Credential reference used only for this connection snapshot. */
|
|
32
|
+
apiKeyEnv: CredentialRef
|
|
33
|
+
/**
|
|
34
|
+
* The official `/models` listing this generation follows; absent when `models`
|
|
35
|
+
* differs from the built-in catalog or the endpoint is not the official API.
|
|
36
|
+
*/
|
|
37
|
+
listing?: ListingSettings
|
|
38
|
+
}
|
|
39
|
+
/** Read one validated provider configuration.
|
|
40
|
+
* @param config - live plugin configuration.
|
|
41
|
+
* @returns detached resolver inputs.
|
|
42
|
+
*/
|
|
43
|
+
export function plainOptions(config: Config): Options {
|
|
44
|
+
return { ...protocolOptions(config), apiKeyEnv: config.apiKeyEnv.get() }
|
|
45
|
+
}
|
|
46
|
+
/** Resolve API-key, listing, and protocol settings together.
|
|
47
|
+
* @param config - raw deployment settings.
|
|
48
|
+
* @param environment - application launch environment.
|
|
49
|
+
* @returns validated endpoint facts, the matching credential reference, and the listing the catalog follows.
|
|
50
|
+
*/
|
|
51
|
+
export function resolveAdapterOptions(config: Options, environment?: LaunchEnvironmentSnapshot): ResolvedDeepSeekOptions {
|
|
52
|
+
const { modelsTimeoutMs: _timeout, modelsMaxAgeMs: _maxAge, apiKeyEnv, ...messages } = config
|
|
53
|
+
const timing = resolveListingTiming(config, 'llm-deepseek-api-key')
|
|
54
|
+
const connection = resolveProtocolOptions(messages, environment)
|
|
55
|
+
const listed = new URL(connection.baseURL).origin === OFFICIAL_ORIGIN && isBuiltInCatalog(connection.models)
|
|
56
|
+
return {
|
|
57
|
+
...connection,
|
|
58
|
+
apiKeyEnv: credentialRef(apiKeyEnv ?? 'DEEPSEEK_API_KEY'),
|
|
59
|
+
...listed ? { listing: { url: modelListingURL(connection.baseURL), ...timing } } : {},
|
|
60
|
+
}
|
|
61
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/** API-key authentication, credential-gated discovery, and the official model listing for the official DeepSeek route. */
|
|
2
|
+
import type { Context } from '@unieai/cordis'
|
|
3
|
+
import { APP_IDENTITY, LlmError } from '@unieai/rabi-llm'
|
|
4
|
+
import type { LlmModelBadge } from '@unieai/rabi-llm'
|
|
5
|
+
import type {} from '@unieai/cordis-plugin-loader'
|
|
6
|
+
import { launchEnvironmentOf } from '@unieai/rabi-launch-environment'
|
|
7
|
+
import {
|
|
8
|
+
catalogModelInfo, createModelListingCache, refuseListedModel, registerDeepSeekProvider, requestModelListing, resolveListedCatalog,
|
|
9
|
+
resolveDeepSeekApiKey,
|
|
10
|
+
} from '@unieai/rabi-llm-deepseek'
|
|
11
|
+
import type { CatalogRefusal, DeepSeekCatalogModel, ListingView, ModelListing } from '@unieai/rabi-llm-deepseek'
|
|
12
|
+
import { Config, plainOptions, resolveAdapterOptions } from './config.ts'
|
|
13
|
+
import type { ResolvedDeepSeekOptions } from './config.ts'
|
|
14
|
+
|
|
15
|
+
export { Config, plainOptions, resolveAdapterOptions } from './config.ts'
|
|
16
|
+
export type { Options, ResolvedDeepSeekOptions } from './config.ts'
|
|
17
|
+
export const name = 'llm-deepseek-api-key'
|
|
18
|
+
export const inject = ['llm']
|
|
19
|
+
|
|
20
|
+
const PROVIDER = 'deepseek-official'
|
|
21
|
+
/** Console tag of listing diagnostics; product profiles mount no console exporter for `ctx.logger`. */
|
|
22
|
+
const LOG_TAG = '[llm-deepseek-api-key]'
|
|
23
|
+
|
|
24
|
+
/** Connection facts of one operation: protocol settings plus the catalog resolved with them. */
|
|
25
|
+
interface OfficialConnection extends ResolvedDeepSeekOptions {
|
|
26
|
+
/** Selectable models: the configured catalog, or the offered part of the followed listing. */
|
|
27
|
+
readonly offered: readonly DeepSeekCatalogModel[]
|
|
28
|
+
/** Listed models refused before network I/O, by id. */
|
|
29
|
+
readonly refusals: ReadonlyMap<string, CatalogRefusal>
|
|
30
|
+
/** Listed badges of offered models, by id; empty without a followed listing. */
|
|
31
|
+
readonly badges: ReadonlyMap<string, LlmModelBadge>
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function apply(ctx: Context, config: Config): void {
|
|
35
|
+
const options = () => resolveAdapterOptions(plainOptions(config), launchEnvironmentOf(ctx))
|
|
36
|
+
options()
|
|
37
|
+
const resolveApiKey = async (connection: ResolvedDeepSeekOptions): Promise<string> => {
|
|
38
|
+
const ref = connection.apiKeyEnv
|
|
39
|
+
const key = await resolveDeepSeekApiKey(ctx, ref)
|
|
40
|
+
if (key !== undefined) return key
|
|
41
|
+
throw new LlmError(
|
|
42
|
+
`llm-deepseek: no API key for provider route "${PROVIDER}"; store ${ref} through the credentials`
|
|
43
|
+
+ ` service (the web Models page writes it), or export ${ref} in the launching environment`,
|
|
44
|
+
'MISSING_CREDENTIAL',
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
/** The API key, or `undefined` when none is configured; other credential failures propagate. */
|
|
48
|
+
const presentApiKey = async (connection: ResolvedDeepSeekOptions): Promise<string | undefined> => {
|
|
49
|
+
try { return await resolveApiKey(connection) }
|
|
50
|
+
catch (error) {
|
|
51
|
+
if (error instanceof LlmError && error.code === 'MISSING_CREDENTIAL') return undefined
|
|
52
|
+
throw error
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const listings = createModelListingCache(ctx, name, PROVIDER, {
|
|
56
|
+
settings: () => options().listing,
|
|
57
|
+
credential: async () => presentApiKey(options()),
|
|
58
|
+
load: (url, apiKey, signal) => requestModelListing(url, { 'x-api-key': apiKey }, signal),
|
|
59
|
+
})
|
|
60
|
+
let reportedListing: ModelListing | undefined
|
|
61
|
+
/** Resolve the catalog, reporting each listing's hidden entries and dropped badges once. */
|
|
62
|
+
const connectionOf = (connection: ResolvedDeepSeekOptions, view: ListingView | undefined): OfficialConnection => {
|
|
63
|
+
if (view === undefined) return { ...connection, offered: connection.models, refusals: new Map(), badges: new Map() }
|
|
64
|
+
const catalog = resolveListedCatalog({
|
|
65
|
+
listing: view.listing?.entries,
|
|
66
|
+
supported: connection.models,
|
|
67
|
+
overrides: {},
|
|
68
|
+
routeMaxTokens: connection.maxTokens,
|
|
69
|
+
clientVersion: APP_IDENTITY.version,
|
|
70
|
+
})
|
|
71
|
+
if (view.listing !== reportedListing) {
|
|
72
|
+
reportedListing = view.listing
|
|
73
|
+
for (const problem of catalog.problems) console.warn(LOG_TAG, problem)
|
|
74
|
+
}
|
|
75
|
+
return { ...connection, models: catalog.resolution, offered: catalog.offered, refusals: catalog.refusals, badges: catalog.badges }
|
|
76
|
+
}
|
|
77
|
+
ctx.llm.registerConfigurableProviders([
|
|
78
|
+
{ provider: PROVIDER, displayName: 'DeepSeek', settingsNs: ctx.fiber.entry?.options.id ?? name, settingsPath: [] },
|
|
79
|
+
])
|
|
80
|
+
registerDeepSeekProvider<OfficialConnection>(ctx, PROVIDER, {
|
|
81
|
+
providerName: 'DeepSeek',
|
|
82
|
+
options: () => {
|
|
83
|
+
const connection = options()
|
|
84
|
+
return connectionOf(connection, connection.listing === undefined ? undefined : listings.current())
|
|
85
|
+
},
|
|
86
|
+
connect: async (signal) => {
|
|
87
|
+
const connection = options()
|
|
88
|
+
if (connection.listing === undefined) return connectionOf(connection, undefined)
|
|
89
|
+
let view: ListingView
|
|
90
|
+
try { view = await listings.view(connection.listing, signal) }
|
|
91
|
+
catch (error) {
|
|
92
|
+
if (signal?.aborted === true) throw error
|
|
93
|
+
// The cache keeps the previous credential's listing when this lookup fails, so gate nothing;
|
|
94
|
+
// `resolveAuth` reports the failure on the model request, after the turn records the prompt.
|
|
95
|
+
return connectionOf(connection, undefined)
|
|
96
|
+
}
|
|
97
|
+
return connectionOf(connection, view)
|
|
98
|
+
},
|
|
99
|
+
resolveAuth: async connection => ({ headers: { 'x-api-key': await resolveApiKey(connection) } }),
|
|
100
|
+
admit: (connection, model) => { refuseListedModel(model, connection.refusals.get(model)) },
|
|
101
|
+
discoverModels: async (provider) => {
|
|
102
|
+
const connection = options()
|
|
103
|
+
if (connection.listing === undefined) {
|
|
104
|
+
if (await presentApiKey(connection) === undefined) return []
|
|
105
|
+
return connection.models.map(model => catalogModelInfo(provider, model))
|
|
106
|
+
}
|
|
107
|
+
const view = await listings.view(connection.listing)
|
|
108
|
+
if (!view.authenticated) return []
|
|
109
|
+
const { offered, badges } = connectionOf(connection, view)
|
|
110
|
+
return offered.map(model => catalogModelInfo(provider, model, badges.get(model.id)))
|
|
111
|
+
},
|
|
112
|
+
})
|
|
113
|
+
// Starting or stopping to follow the listing discards it, so restoring the built-in
|
|
114
|
+
// catalog after a customization reloads the listing on the next access.
|
|
115
|
+
let following = options().listing !== undefined
|
|
116
|
+
ctx.on('loader/volatile-update', () => {
|
|
117
|
+
let next: boolean
|
|
118
|
+
// The transport's own listener reports a configuration the adapter rejects.
|
|
119
|
+
try { next = options().listing !== undefined }
|
|
120
|
+
catch (_rejectedConfiguration) { return }
|
|
121
|
+
if (next !== following) listings.invalidate()
|
|
122
|
+
following = next
|
|
123
|
+
})
|
|
124
|
+
}
|