@unieai/rabi-llm-deepseek-api-key 0.2.1-rc.3 → 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/lib/index.js +15 -26
- package/locale/de.json +6 -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/package.json +11 -8
- package/src/config.ts +61 -0
- package/src/index.ts +124 -0
package/lib/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { APP_IDENTITY, LlmError
|
|
1
|
+
import { APP_IDENTITY, LlmError } from "@unieai/rabi-llm";
|
|
2
2
|
import { launchEnvironmentOf } from "@unieai/rabi-launch-environment";
|
|
3
|
-
import {
|
|
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
4
|
import z from "@unieai/schemastery";
|
|
5
5
|
import { credentialRef } from "@unieai/rabi-credentials";
|
|
6
6
|
//#region lib/types/config.js
|
|
@@ -52,14 +52,8 @@ function apply(ctx, config) {
|
|
|
52
52
|
options();
|
|
53
53
|
const resolveApiKey = async (connection) => {
|
|
54
54
|
const ref = connection.apiKeyEnv;
|
|
55
|
-
const
|
|
56
|
-
if (
|
|
57
|
-
const hit = await credentials.resolve(ref);
|
|
58
|
-
if (hit !== void 0) return assertUsableApiKey(hit.value, "llm-deepseek", ref);
|
|
59
|
-
} else {
|
|
60
|
-
const ambient = launchEnvironmentOf(ctx).get(ref);
|
|
61
|
-
if (ambient !== void 0 && ambient.value.length > 0) return assertUsableApiKey(ambient.value, "llm-deepseek", ref);
|
|
62
|
-
}
|
|
55
|
+
const key = await resolveDeepSeekApiKey(ctx, ref);
|
|
56
|
+
if (key !== void 0) return key;
|
|
63
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");
|
|
64
58
|
};
|
|
65
59
|
/** The API key, or `undefined` when none is configured; other credential failures propagate. */
|
|
@@ -71,24 +65,19 @@ function apply(ctx, config) {
|
|
|
71
65
|
throw error;
|
|
72
66
|
}
|
|
73
67
|
};
|
|
74
|
-
const listings =
|
|
68
|
+
const listings = createModelListingCache(ctx, name, PROVIDER, {
|
|
69
|
+
settings: () => options().listing,
|
|
75
70
|
credential: async () => presentApiKey(options()),
|
|
76
|
-
load: (url, apiKey, signal) => requestModelListing(url, { "x-api-key": apiKey }, signal)
|
|
77
|
-
now: () => Date.now(),
|
|
78
|
-
info: (message) => {
|
|
79
|
-
console.error(LOG_TAG, message);
|
|
80
|
-
},
|
|
81
|
-
warn: (message) => {
|
|
82
|
-
console.warn(LOG_TAG, message);
|
|
83
|
-
}
|
|
71
|
+
load: (url, apiKey, signal) => requestModelListing(url, { "x-api-key": apiKey }, signal)
|
|
84
72
|
});
|
|
85
73
|
let reportedListing;
|
|
86
|
-
/** Resolve the catalog
|
|
74
|
+
/** Resolve the catalog, reporting each listing's hidden entries and dropped badges once. */
|
|
87
75
|
const connectionOf = (connection, view) => {
|
|
88
76
|
if (view === void 0) return {
|
|
89
77
|
...connection,
|
|
90
78
|
offered: connection.models,
|
|
91
|
-
refusals: /* @__PURE__ */ new Map()
|
|
79
|
+
refusals: /* @__PURE__ */ new Map(),
|
|
80
|
+
badges: /* @__PURE__ */ new Map()
|
|
92
81
|
};
|
|
93
82
|
const catalog = resolveListedCatalog({
|
|
94
83
|
listing: view.listing?.entries,
|
|
@@ -105,7 +94,8 @@ function apply(ctx, config) {
|
|
|
105
94
|
...connection,
|
|
106
95
|
models: catalog.resolution,
|
|
107
96
|
offered: catalog.offered,
|
|
108
|
-
refusals: catalog.refusals
|
|
97
|
+
refusals: catalog.refusals,
|
|
98
|
+
badges: catalog.badges
|
|
109
99
|
};
|
|
110
100
|
};
|
|
111
101
|
ctx.llm.registerConfigurableProviders([{
|
|
@@ -143,7 +133,9 @@ function apply(ctx, config) {
|
|
|
143
133
|
return connection.models.map((model) => catalogModelInfo(provider, model));
|
|
144
134
|
}
|
|
145
135
|
const view = await listings.view(connection.listing);
|
|
146
|
-
|
|
136
|
+
if (!view.authenticated) return [];
|
|
137
|
+
const { offered, badges } = connectionOf(connection, view);
|
|
138
|
+
return offered.map((model) => catalogModelInfo(provider, model, badges.get(model.id)));
|
|
147
139
|
}
|
|
148
140
|
});
|
|
149
141
|
let following = options().listing !== void 0;
|
|
@@ -157,9 +149,6 @@ function apply(ctx, config) {
|
|
|
157
149
|
if (next !== following) listings.invalidate();
|
|
158
150
|
following = next;
|
|
159
151
|
});
|
|
160
|
-
ctx.effect(() => () => {
|
|
161
|
-
listings.dispose();
|
|
162
|
-
}, "llm-deepseek-api-key: model listing");
|
|
163
152
|
}
|
|
164
153
|
//#endregion
|
|
165
154
|
export { Config, apply, inject, name, plainOptions, resolveAdapterOptions };
|
package/locale/de.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/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unieai/rabi-llm-deepseek-api-key",
|
|
3
3
|
"description": "DeepSeek api-key provider authentication and discovery",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.2-rc.5",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -21,16 +21,19 @@
|
|
|
21
21
|
"locale/*.json",
|
|
22
22
|
"lib/index.js",
|
|
23
23
|
"lib/types/**/*.d.ts",
|
|
24
|
+
"lib/types/**/*.d.ts.map",
|
|
25
|
+
"lib/**/*.js.map",
|
|
26
|
+
"src",
|
|
24
27
|
"README.md"
|
|
25
28
|
],
|
|
26
29
|
"license": "MIT",
|
|
27
30
|
"peerDependencies": {
|
|
28
|
-
"@unieai/cordis": "4.0
|
|
29
|
-
"@unieai/rabi-llm": "0.2.
|
|
30
|
-
"@unieai/rabi-llm-deepseek": "0.2.
|
|
31
|
-
"@unieai/rabi-launch-environment": "0.2.
|
|
32
|
-
"@unieai/rabi-credentials": "0.2.
|
|
33
|
-
"@unieai/cordis-plugin-loader": "1.0
|
|
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"
|
|
34
37
|
},
|
|
35
38
|
"peerDependenciesMeta": {
|
|
36
39
|
"@unieai/cordis-plugin-loader": {
|
|
@@ -38,6 +41,6 @@
|
|
|
38
41
|
}
|
|
39
42
|
},
|
|
40
43
|
"dependencies": {
|
|
41
|
-
"@unieai/schemastery": "3.
|
|
44
|
+
"@unieai/schemastery": "3.19.0"
|
|
42
45
|
}
|
|
43
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
|
+
}
|