@dexto/agent-management 1.8.1 → 1.8.2

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.
@@ -0,0 +1,253 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var model_auth_profiles_exports = {};
30
+ __export(model_auth_profiles_exports, {
31
+ deleteModelAuthProfile: () => deleteModelAuthProfile,
32
+ getDefaultModelAuthProfile: () => getDefaultModelAuthProfile,
33
+ getDefaultModelAuthProfileIdForProvider: () => getDefaultModelAuthProfileIdForProvider,
34
+ getModelAuthProfileId: () => getModelAuthProfileId,
35
+ getModelAuthProfilesPath: () => getModelAuthProfilesPath,
36
+ listModelAuthProfiles: () => listModelAuthProfiles,
37
+ listSavedModelAuthProfiles: () => listSavedModelAuthProfiles,
38
+ loadModelAuthProfiles: () => loadModelAuthProfiles,
39
+ loadModelAuthProfilesSync: () => loadModelAuthProfilesSync,
40
+ saveApiKeyModelAuthProfile: () => saveApiKeyModelAuthProfile,
41
+ setDefaultModelAuthProfile: () => setDefaultModelAuthProfile,
42
+ upsertModelAuthProfile: () => upsertModelAuthProfile
43
+ });
44
+ module.exports = __toCommonJS(model_auth_profiles_exports);
45
+ var import_node_fs = require("node:fs");
46
+ var import_node_fs2 = require("node:fs");
47
+ var import_node_path = __toESM(require("node:path"), 1);
48
+ var import_yaml = require("yaml");
49
+ var import_core = require("@dexto/core");
50
+ const MODEL_AUTH_PROFILES_FILE = "model-auth.yml";
51
+ const EMPTY_MODEL_AUTH_PROFILES = {
52
+ version: 1,
53
+ defaults: {},
54
+ profiles: []
55
+ };
56
+ function getModelAuthProfilesPath() {
57
+ return (0, import_core.getDextoGlobalPath)("", MODEL_AUTH_PROFILES_FILE);
58
+ }
59
+ function getModelAuthProfileId(providerId, methodId) {
60
+ return `${providerId}:${methodId}`;
61
+ }
62
+ function nowIso() {
63
+ return (/* @__PURE__ */ new Date()).toISOString();
64
+ }
65
+ function isRecord(value) {
66
+ return value !== null && typeof value === "object";
67
+ }
68
+ function isStringRecord(value) {
69
+ if (!isRecord(value)) {
70
+ return false;
71
+ }
72
+ return Object.values(value).every((item) => typeof item === "string");
73
+ }
74
+ function isCredential(value) {
75
+ if (!isRecord(value)) {
76
+ return false;
77
+ }
78
+ if (value.type === "api_key_env") {
79
+ return typeof value.envVar === "string";
80
+ }
81
+ if (value.type !== "oauth") {
82
+ return false;
83
+ }
84
+ return typeof value.issuer === "string" && typeof value.refreshToken === "string" && typeof value.accessToken === "string" && typeof value.expiresAt === "number" && (value.metadata === void 0 || isStringRecord(value.metadata));
85
+ }
86
+ function isModelAuthProfile(value) {
87
+ if (!isRecord(value)) {
88
+ return false;
89
+ }
90
+ return typeof value.id === "string" && typeof value.providerId === "string" && typeof value.methodId === "string" && typeof value.label === "string" && isCredential(value.credential) && typeof value.createdAt === "string" && typeof value.updatedAt === "string";
91
+ }
92
+ function parseDefaults(rawDefaults) {
93
+ return isStringRecord(rawDefaults) ? rawDefaults : {};
94
+ }
95
+ function parseModelAuthProfiles(raw) {
96
+ if (!isRecord(raw)) {
97
+ return EMPTY_MODEL_AUTH_PROFILES;
98
+ }
99
+ if (raw.version !== 1 || !Array.isArray(raw.profiles)) {
100
+ return EMPTY_MODEL_AUTH_PROFILES;
101
+ }
102
+ return {
103
+ version: 1,
104
+ defaults: parseDefaults(raw.defaults),
105
+ profiles: raw.profiles.filter(isModelAuthProfile)
106
+ };
107
+ }
108
+ function parseModelAuthProfilesContent(content) {
109
+ try {
110
+ return parseModelAuthProfiles((0, import_yaml.parse)(content));
111
+ } catch {
112
+ return EMPTY_MODEL_AUTH_PROFILES;
113
+ }
114
+ }
115
+ function loadModelAuthProfilesSync() {
116
+ const profilesPath = getModelAuthProfilesPath();
117
+ if (!(0, import_node_fs.existsSync)(profilesPath)) {
118
+ return EMPTY_MODEL_AUTH_PROFILES;
119
+ }
120
+ return parseModelAuthProfilesContent((0, import_node_fs.readFileSync)(profilesPath, "utf-8"));
121
+ }
122
+ async function loadModelAuthProfiles() {
123
+ const profilesPath = getModelAuthProfilesPath();
124
+ if (!(0, import_node_fs.existsSync)(profilesPath)) {
125
+ return EMPTY_MODEL_AUTH_PROFILES;
126
+ }
127
+ return parseModelAuthProfilesContent(await import_node_fs2.promises.readFile(profilesPath, "utf-8"));
128
+ }
129
+ async function saveModelAuthProfiles(profiles) {
130
+ const profilesPath = getModelAuthProfilesPath();
131
+ await import_node_fs2.promises.mkdir(import_node_path.default.dirname(profilesPath), { recursive: true });
132
+ await import_node_fs2.promises.writeFile(
133
+ profilesPath,
134
+ (0, import_yaml.stringify)(profiles, {
135
+ indent: 2,
136
+ lineWidth: 100,
137
+ minContentWidth: 20
138
+ }),
139
+ "utf-8"
140
+ );
141
+ await import_node_fs2.promises.chmod(profilesPath, 384);
142
+ }
143
+ function listModelAuthProfiles(profiles, providerId) {
144
+ const filtered = providerId ? profiles.profiles.filter((profile) => profile.providerId === providerId) : profiles.profiles;
145
+ return [...filtered].sort((a, b) => a.id.localeCompare(b.id));
146
+ }
147
+ async function listSavedModelAuthProfiles(providerId) {
148
+ return listModelAuthProfiles(await loadModelAuthProfiles(), providerId);
149
+ }
150
+ function getDefaultModelAuthProfile(profiles, providerId) {
151
+ const defaultId = profiles.defaults[providerId];
152
+ if (!defaultId) {
153
+ return null;
154
+ }
155
+ return profiles.profiles.find(
156
+ (profile) => profile.id === defaultId && profile.providerId === providerId
157
+ ) ?? null;
158
+ }
159
+ async function getDefaultModelAuthProfileIdForProvider(providerId) {
160
+ const profiles = await loadModelAuthProfiles();
161
+ const defaultId = profiles.defaults[providerId];
162
+ return defaultId && profiles.profiles.some(
163
+ (profile) => profile.id === defaultId && profile.providerId === providerId
164
+ ) ? defaultId : null;
165
+ }
166
+ async function setDefaultModelAuthProfile(input) {
167
+ const profiles = await loadModelAuthProfiles();
168
+ if (input.profileId === null) {
169
+ const { [input.providerId]: _removed, ...defaults } = profiles.defaults;
170
+ await saveModelAuthProfiles({ ...profiles, defaults });
171
+ return;
172
+ }
173
+ const profile = profiles.profiles.find((item) => item.id === input.profileId);
174
+ if (!profile) {
175
+ throw new Error(`Model auth profile not found: ${input.profileId}`);
176
+ }
177
+ if (profile.providerId !== input.providerId) {
178
+ throw new Error(
179
+ `Model auth profile provider mismatch: ${profile.providerId} is not ${input.providerId}`
180
+ );
181
+ }
182
+ await saveModelAuthProfiles({
183
+ ...profiles,
184
+ defaults: {
185
+ ...profiles.defaults,
186
+ [input.providerId]: input.profileId
187
+ }
188
+ });
189
+ }
190
+ async function deleteModelAuthProfile(profileId) {
191
+ const profiles = await loadModelAuthProfiles();
192
+ const exists = profiles.profiles.some((profile) => profile.id === profileId);
193
+ if (!exists) {
194
+ return false;
195
+ }
196
+ const defaults = Object.fromEntries(
197
+ Object.entries(profiles.defaults).filter(
198
+ ([, defaultProfileId]) => defaultProfileId !== profileId
199
+ )
200
+ );
201
+ await saveModelAuthProfiles({
202
+ version: 1,
203
+ defaults,
204
+ profiles: profiles.profiles.filter((profile) => profile.id !== profileId)
205
+ });
206
+ return true;
207
+ }
208
+ async function upsertModelAuthProfile(profile) {
209
+ const existing = await loadModelAuthProfiles();
210
+ const timestamp = nowIso();
211
+ const current = existing.profiles.find((item) => item.id === profile.id);
212
+ const saved = {
213
+ ...profile,
214
+ createdAt: current?.createdAt ?? timestamp,
215
+ updatedAt: timestamp
216
+ };
217
+ await saveModelAuthProfiles({
218
+ version: 1,
219
+ defaults: {
220
+ ...existing.defaults,
221
+ [profile.providerId]: profile.id
222
+ },
223
+ profiles: [...existing.profiles.filter((item) => item.id !== profile.id), saved]
224
+ });
225
+ return saved;
226
+ }
227
+ async function saveApiKeyModelAuthProfile(providerId) {
228
+ return upsertModelAuthProfile({
229
+ id: getModelAuthProfileId(providerId, "api_key"),
230
+ providerId,
231
+ methodId: "api_key",
232
+ label: "API key",
233
+ credential: {
234
+ type: "api_key_env",
235
+ envVar: (0, import_core.getPrimaryApiKeyEnvVar)(providerId)
236
+ }
237
+ });
238
+ }
239
+ // Annotate the CommonJS export names for ESM import in node:
240
+ 0 && (module.exports = {
241
+ deleteModelAuthProfile,
242
+ getDefaultModelAuthProfile,
243
+ getDefaultModelAuthProfileIdForProvider,
244
+ getModelAuthProfileId,
245
+ getModelAuthProfilesPath,
246
+ listModelAuthProfiles,
247
+ listSavedModelAuthProfiles,
248
+ loadModelAuthProfiles,
249
+ loadModelAuthProfilesSync,
250
+ saveApiKeyModelAuthProfile,
251
+ setDefaultModelAuthProfile,
252
+ upsertModelAuthProfile
253
+ });
@@ -0,0 +1,46 @@
1
+ import type { LLMProvider } from '@dexto/llm';
2
+ export type ApiKeyEnvModelAuthCredential = {
3
+ type: 'api_key_env';
4
+ envVar: string;
5
+ };
6
+ export type OAuthModelAuthCredential = {
7
+ type: 'oauth';
8
+ issuer: string;
9
+ refreshToken: string;
10
+ accessToken: string;
11
+ expiresAt: number;
12
+ metadata?: Record<string, string> | undefined;
13
+ };
14
+ export type ModelAuthCredential = ApiKeyEnvModelAuthCredential | OAuthModelAuthCredential;
15
+ export type ModelAuthProfile = {
16
+ id: string;
17
+ providerId: string;
18
+ methodId: string;
19
+ label: string;
20
+ credential: ModelAuthCredential;
21
+ createdAt: string;
22
+ updatedAt: string;
23
+ };
24
+ type ModelAuthProfileDraft = Omit<ModelAuthProfile, 'createdAt' | 'updatedAt'>;
25
+ export type ModelAuthProfilesFile = {
26
+ version: 1;
27
+ defaults: Record<string, string>;
28
+ profiles: ModelAuthProfile[];
29
+ };
30
+ export declare function getModelAuthProfilesPath(): string;
31
+ export declare function getModelAuthProfileId(providerId: string, methodId: string): string;
32
+ export declare function loadModelAuthProfilesSync(): ModelAuthProfilesFile;
33
+ export declare function loadModelAuthProfiles(): Promise<ModelAuthProfilesFile>;
34
+ export declare function listModelAuthProfiles(profiles: ModelAuthProfilesFile, providerId?: string): ModelAuthProfile[];
35
+ export declare function listSavedModelAuthProfiles(providerId?: string): Promise<ModelAuthProfile[]>;
36
+ export declare function getDefaultModelAuthProfile(profiles: ModelAuthProfilesFile, providerId: string): ModelAuthProfile | null;
37
+ export declare function getDefaultModelAuthProfileIdForProvider(providerId: string): Promise<string | null>;
38
+ export declare function setDefaultModelAuthProfile(input: {
39
+ providerId: string;
40
+ profileId: string | null;
41
+ }): Promise<void>;
42
+ export declare function deleteModelAuthProfile(profileId: string): Promise<boolean>;
43
+ export declare function upsertModelAuthProfile(profile: ModelAuthProfileDraft): Promise<ModelAuthProfile>;
44
+ export declare function saveApiKeyModelAuthProfile(providerId: LLMProvider): Promise<ModelAuthProfile>;
45
+ export {};
46
+ //# sourceMappingURL=model-auth-profiles.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model-auth-profiles.d.ts","sourceRoot":"","sources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wBAAsB,qBAAqB,IAAI,OAAO,CAAC,qBAAqB,CAAC,CAO5E;AAiBD,wBAAgB,qBAAqB,CACjC,QAAQ,EAAE,qBAAqB,EAC/B,UAAU,CAAC,EAAE,MAAM,GACpB,gBAAgB,EAAE,CAKpB;AAED,wBAAsB,0BAA0B,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,EAAE,CAAC,CAEjG;AAED,wBAAgB,0BAA0B,CACtC,QAAQ,EAAE,qBAAqB,EAC/B,UAAU,EAAE,MAAM,GACnB,gBAAgB,GAAG,IAAI,CAWzB;AAED,wBAAsB,uCAAuC,CACzD,UAAU,EAAE,MAAM,GACnB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CASxB;AAED,wBAAsB,0BAA0B,CAAC,KAAK,EAAE;IACpD,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC5B,GAAG,OAAO,CAAC,IAAI,CAAC,CAyBhB;AAED,wBAAsB,sBAAsB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAkBhF;AAED,wBAAsB,sBAAsB,CACxC,OAAO,EAAE,qBAAqB,GAC/B,OAAO,CAAC,gBAAgB,CAAC,CAmB3B;AAED,wBAAsB,0BAA0B,CAC5C,UAAU,EAAE,WAAW,GACxB,OAAO,CAAC,gBAAgB,CAAC,CAW3B"}
@@ -0,0 +1,208 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { promises as fs } from "node:fs";
3
+ import path from "node:path";
4
+ import { parse as parseYaml, stringify as stringifyYaml } from "yaml";
5
+ import { getDextoGlobalPath, getPrimaryApiKeyEnvVar } from "@dexto/core";
6
+ const MODEL_AUTH_PROFILES_FILE = "model-auth.yml";
7
+ const EMPTY_MODEL_AUTH_PROFILES = {
8
+ version: 1,
9
+ defaults: {},
10
+ profiles: []
11
+ };
12
+ function getModelAuthProfilesPath() {
13
+ return getDextoGlobalPath("", MODEL_AUTH_PROFILES_FILE);
14
+ }
15
+ function getModelAuthProfileId(providerId, methodId) {
16
+ return `${providerId}:${methodId}`;
17
+ }
18
+ function nowIso() {
19
+ return (/* @__PURE__ */ new Date()).toISOString();
20
+ }
21
+ function isRecord(value) {
22
+ return value !== null && typeof value === "object";
23
+ }
24
+ function isStringRecord(value) {
25
+ if (!isRecord(value)) {
26
+ return false;
27
+ }
28
+ return Object.values(value).every((item) => typeof item === "string");
29
+ }
30
+ function isCredential(value) {
31
+ if (!isRecord(value)) {
32
+ return false;
33
+ }
34
+ if (value.type === "api_key_env") {
35
+ return typeof value.envVar === "string";
36
+ }
37
+ if (value.type !== "oauth") {
38
+ return false;
39
+ }
40
+ return typeof value.issuer === "string" && typeof value.refreshToken === "string" && typeof value.accessToken === "string" && typeof value.expiresAt === "number" && (value.metadata === void 0 || isStringRecord(value.metadata));
41
+ }
42
+ function isModelAuthProfile(value) {
43
+ if (!isRecord(value)) {
44
+ return false;
45
+ }
46
+ return typeof value.id === "string" && typeof value.providerId === "string" && typeof value.methodId === "string" && typeof value.label === "string" && isCredential(value.credential) && typeof value.createdAt === "string" && typeof value.updatedAt === "string";
47
+ }
48
+ function parseDefaults(rawDefaults) {
49
+ return isStringRecord(rawDefaults) ? rawDefaults : {};
50
+ }
51
+ function parseModelAuthProfiles(raw) {
52
+ if (!isRecord(raw)) {
53
+ return EMPTY_MODEL_AUTH_PROFILES;
54
+ }
55
+ if (raw.version !== 1 || !Array.isArray(raw.profiles)) {
56
+ return EMPTY_MODEL_AUTH_PROFILES;
57
+ }
58
+ return {
59
+ version: 1,
60
+ defaults: parseDefaults(raw.defaults),
61
+ profiles: raw.profiles.filter(isModelAuthProfile)
62
+ };
63
+ }
64
+ function parseModelAuthProfilesContent(content) {
65
+ try {
66
+ return parseModelAuthProfiles(parseYaml(content));
67
+ } catch {
68
+ return EMPTY_MODEL_AUTH_PROFILES;
69
+ }
70
+ }
71
+ function loadModelAuthProfilesSync() {
72
+ const profilesPath = getModelAuthProfilesPath();
73
+ if (!existsSync(profilesPath)) {
74
+ return EMPTY_MODEL_AUTH_PROFILES;
75
+ }
76
+ return parseModelAuthProfilesContent(readFileSync(profilesPath, "utf-8"));
77
+ }
78
+ async function loadModelAuthProfiles() {
79
+ const profilesPath = getModelAuthProfilesPath();
80
+ if (!existsSync(profilesPath)) {
81
+ return EMPTY_MODEL_AUTH_PROFILES;
82
+ }
83
+ return parseModelAuthProfilesContent(await fs.readFile(profilesPath, "utf-8"));
84
+ }
85
+ async function saveModelAuthProfiles(profiles) {
86
+ const profilesPath = getModelAuthProfilesPath();
87
+ await fs.mkdir(path.dirname(profilesPath), { recursive: true });
88
+ await fs.writeFile(
89
+ profilesPath,
90
+ stringifyYaml(profiles, {
91
+ indent: 2,
92
+ lineWidth: 100,
93
+ minContentWidth: 20
94
+ }),
95
+ "utf-8"
96
+ );
97
+ await fs.chmod(profilesPath, 384);
98
+ }
99
+ function listModelAuthProfiles(profiles, providerId) {
100
+ const filtered = providerId ? profiles.profiles.filter((profile) => profile.providerId === providerId) : profiles.profiles;
101
+ return [...filtered].sort((a, b) => a.id.localeCompare(b.id));
102
+ }
103
+ async function listSavedModelAuthProfiles(providerId) {
104
+ return listModelAuthProfiles(await loadModelAuthProfiles(), providerId);
105
+ }
106
+ function getDefaultModelAuthProfile(profiles, providerId) {
107
+ const defaultId = profiles.defaults[providerId];
108
+ if (!defaultId) {
109
+ return null;
110
+ }
111
+ return profiles.profiles.find(
112
+ (profile) => profile.id === defaultId && profile.providerId === providerId
113
+ ) ?? null;
114
+ }
115
+ async function getDefaultModelAuthProfileIdForProvider(providerId) {
116
+ const profiles = await loadModelAuthProfiles();
117
+ const defaultId = profiles.defaults[providerId];
118
+ return defaultId && profiles.profiles.some(
119
+ (profile) => profile.id === defaultId && profile.providerId === providerId
120
+ ) ? defaultId : null;
121
+ }
122
+ async function setDefaultModelAuthProfile(input) {
123
+ const profiles = await loadModelAuthProfiles();
124
+ if (input.profileId === null) {
125
+ const { [input.providerId]: _removed, ...defaults } = profiles.defaults;
126
+ await saveModelAuthProfiles({ ...profiles, defaults });
127
+ return;
128
+ }
129
+ const profile = profiles.profiles.find((item) => item.id === input.profileId);
130
+ if (!profile) {
131
+ throw new Error(`Model auth profile not found: ${input.profileId}`);
132
+ }
133
+ if (profile.providerId !== input.providerId) {
134
+ throw new Error(
135
+ `Model auth profile provider mismatch: ${profile.providerId} is not ${input.providerId}`
136
+ );
137
+ }
138
+ await saveModelAuthProfiles({
139
+ ...profiles,
140
+ defaults: {
141
+ ...profiles.defaults,
142
+ [input.providerId]: input.profileId
143
+ }
144
+ });
145
+ }
146
+ async function deleteModelAuthProfile(profileId) {
147
+ const profiles = await loadModelAuthProfiles();
148
+ const exists = profiles.profiles.some((profile) => profile.id === profileId);
149
+ if (!exists) {
150
+ return false;
151
+ }
152
+ const defaults = Object.fromEntries(
153
+ Object.entries(profiles.defaults).filter(
154
+ ([, defaultProfileId]) => defaultProfileId !== profileId
155
+ )
156
+ );
157
+ await saveModelAuthProfiles({
158
+ version: 1,
159
+ defaults,
160
+ profiles: profiles.profiles.filter((profile) => profile.id !== profileId)
161
+ });
162
+ return true;
163
+ }
164
+ async function upsertModelAuthProfile(profile) {
165
+ const existing = await loadModelAuthProfiles();
166
+ const timestamp = nowIso();
167
+ const current = existing.profiles.find((item) => item.id === profile.id);
168
+ const saved = {
169
+ ...profile,
170
+ createdAt: current?.createdAt ?? timestamp,
171
+ updatedAt: timestamp
172
+ };
173
+ await saveModelAuthProfiles({
174
+ version: 1,
175
+ defaults: {
176
+ ...existing.defaults,
177
+ [profile.providerId]: profile.id
178
+ },
179
+ profiles: [...existing.profiles.filter((item) => item.id !== profile.id), saved]
180
+ });
181
+ return saved;
182
+ }
183
+ async function saveApiKeyModelAuthProfile(providerId) {
184
+ return upsertModelAuthProfile({
185
+ id: getModelAuthProfileId(providerId, "api_key"),
186
+ providerId,
187
+ methodId: "api_key",
188
+ label: "API key",
189
+ credential: {
190
+ type: "api_key_env",
191
+ envVar: getPrimaryApiKeyEnvVar(providerId)
192
+ }
193
+ });
194
+ }
195
+ export {
196
+ deleteModelAuthProfile,
197
+ getDefaultModelAuthProfile,
198
+ getDefaultModelAuthProfileIdForProvider,
199
+ getModelAuthProfileId,
200
+ getModelAuthProfilesPath,
201
+ listModelAuthProfiles,
202
+ listSavedModelAuthProfiles,
203
+ loadModelAuthProfiles,
204
+ loadModelAuthProfilesSync,
205
+ saveApiKeyModelAuthProfile,
206
+ setDefaultModelAuthProfile,
207
+ upsertModelAuthProfile
208
+ };
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var provider_auth_definitions_exports = {};
20
+ __export(provider_auth_definitions_exports, {
21
+ AUTH_METHOD_KINDS: () => AUTH_METHOD_KINDS,
22
+ OPENAI_API_KEY_AUTH_METHOD: () => OPENAI_API_KEY_AUTH_METHOD,
23
+ OPENAI_CHATGPT_LOGIN_AUTH_METHOD: () => OPENAI_CHATGPT_LOGIN_AUTH_METHOD,
24
+ PROVIDER_AUTH_DEFINITIONS: () => PROVIDER_AUTH_DEFINITIONS,
25
+ getAuthMethodDefinition: () => getAuthMethodDefinition,
26
+ getProviderAuthDefinition: () => getProviderAuthDefinition,
27
+ getProviderAuthDefinitions: () => getProviderAuthDefinitions,
28
+ isOAuthAuthMethod: () => isOAuthAuthMethod
29
+ });
30
+ module.exports = __toCommonJS(provider_auth_definitions_exports);
31
+ const AUTH_METHOD_KINDS = ["api_key", "oauth"];
32
+ const OPENAI_API_KEY_AUTH_METHOD = {
33
+ id: "api_key",
34
+ label: "API key",
35
+ kind: "api_key"
36
+ };
37
+ const OPENAI_CHATGPT_LOGIN_AUTH_METHOD = {
38
+ id: "chatgpt_login",
39
+ label: "ChatGPT Login",
40
+ kind: "oauth",
41
+ hint: "Use your ChatGPT account for OpenAI models"
42
+ };
43
+ const PROVIDER_AUTH_DEFINITIONS = [
44
+ {
45
+ providerId: "openai",
46
+ label: "OpenAI",
47
+ methods: [OPENAI_API_KEY_AUTH_METHOD, OPENAI_CHATGPT_LOGIN_AUTH_METHOD]
48
+ }
49
+ ];
50
+ function getProviderAuthDefinitions() {
51
+ return PROVIDER_AUTH_DEFINITIONS;
52
+ }
53
+ function getProviderAuthDefinition(providerId) {
54
+ return PROVIDER_AUTH_DEFINITIONS.find((provider) => provider.providerId === providerId) ?? null;
55
+ }
56
+ function getAuthMethodDefinition(providerId, methodId) {
57
+ return getProviderAuthDefinition(providerId)?.methods.find((method) => method.id === methodId) ?? null;
58
+ }
59
+ function isOAuthAuthMethod(method) {
60
+ return method.kind === "oauth";
61
+ }
62
+ // Annotate the CommonJS export names for ESM import in node:
63
+ 0 && (module.exports = {
64
+ AUTH_METHOD_KINDS,
65
+ OPENAI_API_KEY_AUTH_METHOD,
66
+ OPENAI_CHATGPT_LOGIN_AUTH_METHOD,
67
+ PROVIDER_AUTH_DEFINITIONS,
68
+ getAuthMethodDefinition,
69
+ getProviderAuthDefinition,
70
+ getProviderAuthDefinitions,
71
+ isOAuthAuthMethod
72
+ });
@@ -0,0 +1,52 @@
1
+ export declare const AUTH_METHOD_KINDS: readonly ["api_key", "oauth"];
2
+ export type AuthMethodKind = (typeof AUTH_METHOD_KINDS)[number];
3
+ type AuthMethodDefinitionBase = {
4
+ id: string;
5
+ label: string;
6
+ kind: AuthMethodKind;
7
+ hint?: string | undefined;
8
+ };
9
+ export type ApiKeyAuthMethodDefinition = AuthMethodDefinitionBase & {
10
+ id: 'api_key';
11
+ kind: 'api_key';
12
+ };
13
+ export type OAuthAuthMethodDefinition = AuthMethodDefinitionBase & {
14
+ kind: 'oauth';
15
+ };
16
+ export type AuthMethodDefinition = ApiKeyAuthMethodDefinition | OAuthAuthMethodDefinition;
17
+ export type ProviderAuthDefinition = {
18
+ providerId: string;
19
+ label: string;
20
+ methods: readonly AuthMethodDefinition[];
21
+ };
22
+ export declare const OPENAI_API_KEY_AUTH_METHOD: {
23
+ readonly id: "api_key";
24
+ readonly label: "API key";
25
+ readonly kind: "api_key";
26
+ };
27
+ export declare const OPENAI_CHATGPT_LOGIN_AUTH_METHOD: {
28
+ readonly id: "chatgpt_login";
29
+ readonly label: "ChatGPT Login";
30
+ readonly kind: "oauth";
31
+ readonly hint: "Use your ChatGPT account for OpenAI models";
32
+ };
33
+ export declare const PROVIDER_AUTH_DEFINITIONS: readonly [{
34
+ readonly providerId: "openai";
35
+ readonly label: "OpenAI";
36
+ readonly methods: readonly [{
37
+ readonly id: "api_key";
38
+ readonly label: "API key";
39
+ readonly kind: "api_key";
40
+ }, {
41
+ readonly id: "chatgpt_login";
42
+ readonly label: "ChatGPT Login";
43
+ readonly kind: "oauth";
44
+ readonly hint: "Use your ChatGPT account for OpenAI models";
45
+ }];
46
+ }];
47
+ export declare function getProviderAuthDefinitions(): readonly ProviderAuthDefinition[];
48
+ export declare function getProviderAuthDefinition(providerId: string): ProviderAuthDefinition | null;
49
+ export declare function getAuthMethodDefinition(providerId: string, methodId: string): AuthMethodDefinition | null;
50
+ export declare function isOAuthAuthMethod(method: AuthMethodDefinition): method is OAuthAuthMethodDefinition;
51
+ export {};
52
+ //# sourceMappingURL=provider-auth-definitions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider-auth-defin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IAAI,yBAAyB,CAErC"}
@@ -0,0 +1,41 @@
1
+ const AUTH_METHOD_KINDS = ["api_key", "oauth"];
2
+ const OPENAI_API_KEY_AUTH_METHOD = {
3
+ id: "api_key",
4
+ label: "API key",
5
+ kind: "api_key"
6
+ };
7
+ const OPENAI_CHATGPT_LOGIN_AUTH_METHOD = {
8
+ id: "chatgpt_login",
9
+ label: "ChatGPT Login",
10
+ kind: "oauth",
11
+ hint: "Use your ChatGPT account for OpenAI models"
12
+ };
13
+ const PROVIDER_AUTH_DEFINITIONS = [
14
+ {
15
+ providerId: "openai",
16
+ label: "OpenAI",
17
+ methods: [OPENAI_API_KEY_AUTH_METHOD, OPENAI_CHATGPT_LOGIN_AUTH_METHOD]
18
+ }
19
+ ];
20
+ function getProviderAuthDefinitions() {
21
+ return PROVIDER_AUTH_DEFINITIONS;
22
+ }
23
+ function getProviderAuthDefinition(providerId) {
24
+ return PROVIDER_AUTH_DEFINITIONS.find((provider) => provider.providerId === providerId) ?? null;
25
+ }
26
+ function getAuthMethodDefinition(providerId, methodId) {
27
+ return getProviderAuthDefinition(providerId)?.methods.find((method) => method.id === methodId) ?? null;
28
+ }
29
+ function isOAuthAuthMethod(method) {
30
+ return method.kind === "oauth";
31
+ }
32
+ export {
33
+ AUTH_METHOD_KINDS,
34
+ OPENAI_API_KEY_AUTH_METHOD,
35
+ OPENAI_CHATGPT_LOGIN_AUTH_METHOD,
36
+ PROVIDER_AUTH_DEFINITIONS,
37
+ getAuthMethodDefinition,
38
+ getProviderAuthDefinition,
39
+ getProviderAuthDefinitions,
40
+ isOAuthAuthMethod
41
+ };