@dexto/agent-management 1.8.0 → 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.
Files changed (49) hide show
  1. package/dist/auth/chatgpt-oauth.cjs +332 -0
  2. package/dist/auth/chatgpt-oauth.d.ts +28 -0
  3. package/dist/auth/chatgpt-oauth.d.ts.map +1 -0
  4. package/dist/auth/chatgpt-oauth.js +305 -0
  5. package/dist/auth/index.cjs +83 -0
  6. package/dist/auth/index.d.ts +5 -0
  7. package/dist/auth/index.d.ts.map +1 -0
  8. package/dist/auth/index.js +64 -0
  9. package/dist/auth/model-auth-handlers.cjs +141 -0
  10. package/dist/auth/model-auth-handlers.d.ts +17 -0
  11. package/dist/auth/model-auth-handlers.d.ts.map +1 -0
  12. package/dist/auth/model-auth-handlers.js +126 -0
  13. package/dist/auth/model-auth-profiles.cjs +253 -0
  14. package/dist/auth/model-auth-profiles.d.ts +46 -0
  15. package/dist/auth/model-auth-profiles.d.ts.map +1 -0
  16. package/dist/auth/model-auth-profiles.js +208 -0
  17. package/dist/auth/provider-auth-definitions.cjs +72 -0
  18. package/dist/auth/provider-auth-definitions.d.ts +52 -0
  19. package/dist/auth/provider-auth-definitions.d.ts.map +1 -0
  20. package/dist/auth/provider-auth-definitions.js +41 -0
  21. package/dist/index.cjs +55 -0
  22. package/dist/index.d.ts +1 -0
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +56 -0
  25. package/dist/models/model-picker-state.cjs +2 -2
  26. package/dist/models/model-picker-state.d.ts +1 -1
  27. package/dist/models/model-picker-state.d.ts.map +1 -1
  28. package/dist/models/model-picker-state.js +1 -1
  29. package/dist/preferences/loader.d.ts +1 -1
  30. package/dist/preferences/loader.d.ts.map +1 -1
  31. package/dist/preferences/schemas.cjs +9 -8
  32. package/dist/preferences/schemas.d.ts.map +1 -1
  33. package/dist/preferences/schemas.js +4 -6
  34. package/dist/tool-factories/agent-spawner/llm-resolution.cjs +3 -3
  35. package/dist/tool-factories/agent-spawner/llm-resolution.js +1 -1
  36. package/dist/tool-factories/agent-spawner/runtime.cjs +4 -3
  37. package/dist/tool-factories/agent-spawner/runtime.d.ts.map +1 -1
  38. package/dist/tool-factories/agent-spawner/runtime.js +2 -6
  39. package/dist/tool-factories/agent-spawner/schemas.d.ts +1 -1
  40. package/dist/tool-factories/agent-spawner/schemas.d.ts.map +1 -1
  41. package/dist/utils/api-key-resolver.d.ts +1 -1
  42. package/dist/utils/api-key-resolver.d.ts.map +1 -1
  43. package/dist/utils/api-key-store.cjs +2 -2
  44. package/dist/utils/api-key-store.d.ts +1 -1
  45. package/dist/utils/api-key-store.d.ts.map +1 -1
  46. package/dist/utils/api-key-store.js +1 -1
  47. package/dist/writer.d.ts +1 -1
  48. package/dist/writer.d.ts.map +1 -1
  49. package/package.json +11 -5
@@ -0,0 +1,83 @@
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 auth_exports = {};
20
+ __export(auth_exports, {
21
+ AUTH_METHOD_KINDS: () => import_provider_auth_definitions.AUTH_METHOD_KINDS,
22
+ OPENAI_API_KEY_AUTH_METHOD: () => import_provider_auth_definitions.OPENAI_API_KEY_AUTH_METHOD,
23
+ OPENAI_CHATGPT_LOGIN_AUTH_METHOD: () => import_provider_auth_definitions.OPENAI_CHATGPT_LOGIN_AUTH_METHOD,
24
+ PROVIDER_AUTH_DEFINITIONS: () => import_provider_auth_definitions.PROVIDER_AUTH_DEFINITIONS,
25
+ createChatGPTRuntimeAuth: () => import_chatgpt_oauth.createChatGPTRuntimeAuth,
26
+ createModelAuthResolver: () => import_model_auth_handlers.createModelAuthResolver,
27
+ deleteModelAuthProfile: () => import_model_auth_profiles.deleteModelAuthProfile,
28
+ getAuthMethodDefinition: () => import_provider_auth_definitions.getAuthMethodDefinition,
29
+ getDefaultModelAuthProfile: () => import_model_auth_profiles.getDefaultModelAuthProfile,
30
+ getDefaultModelAuthProfileIdForProvider: () => import_model_auth_profiles.getDefaultModelAuthProfileIdForProvider,
31
+ getModelAuthProfileId: () => import_model_auth_profiles.getModelAuthProfileId,
32
+ getModelAuthProfilesPath: () => import_model_auth_profiles.getModelAuthProfilesPath,
33
+ getProviderAuthDefinition: () => import_provider_auth_definitions.getProviderAuthDefinition,
34
+ getProviderAuthDefinitions: () => import_provider_auth_definitions.getProviderAuthDefinitions,
35
+ isOAuthAuthMethod: () => import_provider_auth_definitions.isOAuthAuthMethod,
36
+ listModelAuthProfiles: () => import_model_auth_profiles.listModelAuthProfiles,
37
+ listSavedModelAuthProfiles: () => import_model_auth_profiles.listSavedModelAuthProfiles,
38
+ loadModelAuthProfiles: () => import_model_auth_profiles.loadModelAuthProfiles,
39
+ loadModelAuthProfilesSync: () => import_model_auth_profiles.loadModelAuthProfilesSync,
40
+ markModelAuthProviderConnected: () => import_model_auth_handlers.markModelAuthProviderConnected,
41
+ refreshChatGPTOAuthCredential: () => import_chatgpt_oauth.refreshChatGPTOAuthCredential,
42
+ saveApiKeyModelAuthProfile: () => import_model_auth_profiles.saveApiKeyModelAuthProfile,
43
+ saveChatGPTLoginModelAuthProfile: () => import_model_auth_handlers.saveChatGPTLoginModelAuthProfile,
44
+ setDefaultModelAuthProfile: () => import_model_auth_profiles.setDefaultModelAuthProfile,
45
+ startChatGPTBrowserLogin: () => import_chatgpt_oauth.startChatGPTBrowserLogin,
46
+ startModelAuthBrowserLogin: () => import_model_auth_handlers.startModelAuthBrowserLogin,
47
+ upsertModelAuthProfile: () => import_model_auth_profiles.upsertModelAuthProfile
48
+ });
49
+ module.exports = __toCommonJS(auth_exports);
50
+ var import_provider_auth_definitions = require("./provider-auth-definitions.js");
51
+ var import_model_auth_handlers = require("./model-auth-handlers.js");
52
+ var import_model_auth_profiles = require("./model-auth-profiles.js");
53
+ var import_chatgpt_oauth = require("./chatgpt-oauth.js");
54
+ // Annotate the CommonJS export names for ESM import in node:
55
+ 0 && (module.exports = {
56
+ AUTH_METHOD_KINDS,
57
+ OPENAI_API_KEY_AUTH_METHOD,
58
+ OPENAI_CHATGPT_LOGIN_AUTH_METHOD,
59
+ PROVIDER_AUTH_DEFINITIONS,
60
+ createChatGPTRuntimeAuth,
61
+ createModelAuthResolver,
62
+ deleteModelAuthProfile,
63
+ getAuthMethodDefinition,
64
+ getDefaultModelAuthProfile,
65
+ getDefaultModelAuthProfileIdForProvider,
66
+ getModelAuthProfileId,
67
+ getModelAuthProfilesPath,
68
+ getProviderAuthDefinition,
69
+ getProviderAuthDefinitions,
70
+ isOAuthAuthMethod,
71
+ listModelAuthProfiles,
72
+ listSavedModelAuthProfiles,
73
+ loadModelAuthProfiles,
74
+ loadModelAuthProfilesSync,
75
+ markModelAuthProviderConnected,
76
+ refreshChatGPTOAuthCredential,
77
+ saveApiKeyModelAuthProfile,
78
+ saveChatGPTLoginModelAuthProfile,
79
+ setDefaultModelAuthProfile,
80
+ startChatGPTBrowserLogin,
81
+ startModelAuthBrowserLogin,
82
+ upsertModelAuthProfile
83
+ });
@@ -0,0 +1,5 @@
1
+ export { AUTH_METHOD_KINDS, OPENAI_API_KEY_AUTH_METHOD, OPENAI_CHATGPT_LOGIN_AUTH_METHOD, PROVIDER_AUTH_DEFINITIONS, getAuthMethodDefinition, getProviderAuthDefinition, getProviderAuthDefinitions, isOAuthAuthMethod, type ApiKeyAuthMethodDefinition, type AuthMethodDefinition, type AuthMethodKind, type OAuthAuthMethodDefinition, type ProviderAuthDefinition, } from './provider-auth-definitions.js';
2
+ export { createModelAuthResolver, markModelAuthProviderConnected, saveChatGPTLoginModelAuthProfile, startModelAuthBrowserLogin, type PendingModelAuthBrowserLogin, } from './model-auth-handlers.js';
3
+ export { deleteModelAuthProfile, getDefaultModelAuthProfile, getDefaultModelAuthProfileIdForProvider, getModelAuthProfileId, getModelAuthProfilesPath, listModelAuthProfiles, listSavedModelAuthProfiles, loadModelAuthProfiles, loadModelAuthProfilesSync, saveApiKeyModelAuthProfile, setDefaultModelAuthProfile, upsertModelAuthProfile, type ApiKeyEnvModelAuthCredential, type ModelAuthCredential, type ModelAuthProfile, type ModelAuthProfilesFile, type OAuthModelAuthCredential, } from './model-auth-profiles.js';
4
+ export { createChatGPTRuntimeAuth, refreshChatGPTOAuthCredential, startChatGPTBrowserLogin, type ChatGPTOAuthCredential, type ChatGPTRuntimeAuth, type PendingChatGPTLogin, } from './chatgpt-oauth.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,iBAAiB,EACjB,0BAA0B,EAC1B,gCAAgC,EAChC,yBAAyB,EACzB,uBAAuB,EACvB,yBAAyB,EACzB,0BAA0B,EAC1B,iBAAiB,EACjB,KAAK,0BAA0B,EAC/B,KAAK,oBAAoB,EACzB,KAAK,cAAc,EACnB,KAAK,yBAAyB,EAC9B,KAAK,sBAAsB,GAC9B,MAAM,gCAAgC,CAAC;AAExC,OAAO,EACH,uBAAuB,EACvB,8BAA8B,EAC9B,gCAAgC,EAChC,0BAA0B,EAC1B,KAAK,4BAA4B,GACpC,MAAM,0BAA0B,CAAC;AAElC,OAAO,EACH,sBAAsB,EACtB,0BAA0B,EAC1B,uCAAuC,EACvC,qBAAqB,EACrB,wBAAwB,EACxB,qBAAqB,EACrB,0BAA0B,EAC1B,qBAAqB,EACrB,yBAAyB,EACzB,0BAA0B,EAC1B,0BAA0B,EAC1B,sBAAsB,EACtB,KAAK,4BAA4B,EACjC,KAAK,mBAAmB,EACxB,KAAK,gBAAgB,EACrB,KAAK,qBAAqB,EAC1B,KAAK,wBAAwB,GAChC,MAAM,0BAA0B,CAAC;AAElC,OAAO,EACH,wBAAwB,EACxB,6BAA6B,EAC7B,wBAAwB,EACxB,KAAK,sBAAsB,EAC3B,KAAK,kBAAkB,EACvB,KAAK,mBAAmB,GAC3B,MAAM,oBAAoB,CAAC"}
@@ -0,0 +1,64 @@
1
+ import {
2
+ AUTH_METHOD_KINDS,
3
+ OPENAI_API_KEY_AUTH_METHOD,
4
+ OPENAI_CHATGPT_LOGIN_AUTH_METHOD,
5
+ PROVIDER_AUTH_DEFINITIONS,
6
+ getAuthMethodDefinition,
7
+ getProviderAuthDefinition,
8
+ getProviderAuthDefinitions,
9
+ isOAuthAuthMethod
10
+ } from "./provider-auth-definitions.js";
11
+ import {
12
+ createModelAuthResolver,
13
+ markModelAuthProviderConnected,
14
+ saveChatGPTLoginModelAuthProfile,
15
+ startModelAuthBrowserLogin
16
+ } from "./model-auth-handlers.js";
17
+ import {
18
+ deleteModelAuthProfile,
19
+ getDefaultModelAuthProfile,
20
+ getDefaultModelAuthProfileIdForProvider,
21
+ getModelAuthProfileId,
22
+ getModelAuthProfilesPath,
23
+ listModelAuthProfiles,
24
+ listSavedModelAuthProfiles,
25
+ loadModelAuthProfiles,
26
+ loadModelAuthProfilesSync,
27
+ saveApiKeyModelAuthProfile,
28
+ setDefaultModelAuthProfile,
29
+ upsertModelAuthProfile
30
+ } from "./model-auth-profiles.js";
31
+ import {
32
+ createChatGPTRuntimeAuth,
33
+ refreshChatGPTOAuthCredential,
34
+ startChatGPTBrowserLogin
35
+ } from "./chatgpt-oauth.js";
36
+ export {
37
+ AUTH_METHOD_KINDS,
38
+ OPENAI_API_KEY_AUTH_METHOD,
39
+ OPENAI_CHATGPT_LOGIN_AUTH_METHOD,
40
+ PROVIDER_AUTH_DEFINITIONS,
41
+ createChatGPTRuntimeAuth,
42
+ createModelAuthResolver,
43
+ deleteModelAuthProfile,
44
+ getAuthMethodDefinition,
45
+ getDefaultModelAuthProfile,
46
+ getDefaultModelAuthProfileIdForProvider,
47
+ getModelAuthProfileId,
48
+ getModelAuthProfilesPath,
49
+ getProviderAuthDefinition,
50
+ getProviderAuthDefinitions,
51
+ isOAuthAuthMethod,
52
+ listModelAuthProfiles,
53
+ listSavedModelAuthProfiles,
54
+ loadModelAuthProfiles,
55
+ loadModelAuthProfilesSync,
56
+ markModelAuthProviderConnected,
57
+ refreshChatGPTOAuthCredential,
58
+ saveApiKeyModelAuthProfile,
59
+ saveChatGPTLoginModelAuthProfile,
60
+ setDefaultModelAuthProfile,
61
+ startChatGPTBrowserLogin,
62
+ startModelAuthBrowserLogin,
63
+ upsertModelAuthProfile
64
+ };
@@ -0,0 +1,141 @@
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 model_auth_handlers_exports = {};
20
+ __export(model_auth_handlers_exports, {
21
+ createModelAuthResolver: () => createModelAuthResolver,
22
+ markModelAuthProviderConnected: () => markModelAuthProviderConnected,
23
+ saveChatGPTLoginModelAuthProfile: () => saveChatGPTLoginModelAuthProfile,
24
+ startModelAuthBrowserLogin: () => startModelAuthBrowserLogin
25
+ });
26
+ module.exports = __toCommonJS(model_auth_handlers_exports);
27
+ var import_chatgpt_oauth = require("./chatgpt-oauth.js");
28
+ var import_model_auth_profiles = require("./model-auth-profiles.js");
29
+ var import_preferences = require("../preferences/index.js");
30
+ function toChatGPTCredential(credential) {
31
+ return {
32
+ type: "oauth",
33
+ issuer: "https://auth.openai.com",
34
+ refreshToken: credential.refreshToken,
35
+ accessToken: credential.accessToken,
36
+ expiresAt: credential.expiresAt,
37
+ accountId: credential.metadata?.accountId
38
+ };
39
+ }
40
+ function fromChatGPTCredential(credential) {
41
+ return {
42
+ type: "oauth",
43
+ issuer: credential.issuer,
44
+ refreshToken: credential.refreshToken,
45
+ accessToken: credential.accessToken,
46
+ expiresAt: credential.expiresAt,
47
+ ...credential.accountId ? { metadata: { accountId: credential.accountId } } : {}
48
+ };
49
+ }
50
+ async function saveChatGPTLoginModelAuthProfile(credential) {
51
+ return (0, import_model_auth_profiles.upsertModelAuthProfile)({
52
+ id: (0, import_model_auth_profiles.getModelAuthProfileId)("openai", "chatgpt_login"),
53
+ providerId: "openai",
54
+ methodId: "chatgpt_login",
55
+ label: "ChatGPT Login",
56
+ credential: fromChatGPTCredential(credential)
57
+ });
58
+ }
59
+ async function updateChatGPTLoginCredential(credential) {
60
+ await saveChatGPTLoginModelAuthProfile(credential);
61
+ }
62
+ async function startOpenAIChatGPTBrowserLogin() {
63
+ const login = await (0, import_chatgpt_oauth.startChatGPTBrowserLogin)();
64
+ return {
65
+ label: "ChatGPT Login",
66
+ authUrl: login.authUrl,
67
+ waitForProfile: async () => saveChatGPTLoginModelAuthProfile(await login.waitForCredential()),
68
+ cancel: login.cancel
69
+ };
70
+ }
71
+ const MODEL_AUTH_METHOD_HANDLERS = [
72
+ {
73
+ providerId: "openai",
74
+ methodId: "chatgpt_login",
75
+ startBrowserLogin: startOpenAIChatGPTBrowserLogin,
76
+ resolveRuntimeAuth: (profile) => profile.credential.type === "oauth" ? (0, import_chatgpt_oauth.createChatGPTRuntimeAuth)({
77
+ credential: toChatGPTCredential(profile.credential),
78
+ updateCredential: updateChatGPTLoginCredential
79
+ }) : null
80
+ }
81
+ ];
82
+ function getModelAuthMethodHandler(providerId, methodId) {
83
+ return MODEL_AUTH_METHOD_HANDLERS.find(
84
+ (handler) => handler.providerId === providerId && handler.methodId === methodId
85
+ ) ?? null;
86
+ }
87
+ async function startModelAuthBrowserLogin(input) {
88
+ const handler = getModelAuthMethodHandler(input.providerId, input.methodId);
89
+ if (!handler?.startBrowserLogin) {
90
+ throw new Error(
91
+ `Auth method is not implemented yet: ${input.providerId}/${input.methodId}`
92
+ );
93
+ }
94
+ return handler.startBrowserLogin();
95
+ }
96
+ async function markModelAuthProviderConnected(providerId) {
97
+ if (!(0, import_preferences.globalPreferencesExist)()) {
98
+ return;
99
+ }
100
+ const preferences = await (0, import_preferences.loadGlobalPreferences)();
101
+ if (preferences.llm.provider !== providerId) {
102
+ return;
103
+ }
104
+ await (0, import_preferences.updateGlobalPreferences)({
105
+ setup: {
106
+ apiKeyPending: false
107
+ }
108
+ });
109
+ }
110
+ function resolveRuntimeAuthFromProfile(profile) {
111
+ const auth = {
112
+ source: "profile",
113
+ profileId: profile.id,
114
+ providerId: profile.providerId,
115
+ methodId: profile.methodId,
116
+ credentialType: profile.credential.type
117
+ };
118
+ if (profile.credential.type === "api_key_env") {
119
+ const apiKey = process.env[profile.credential.envVar]?.trim();
120
+ return apiKey ? { apiKey, auth } : null;
121
+ }
122
+ const runtimeAuth = getModelAuthMethodHandler(profile.providerId, profile.methodId)?.resolveRuntimeAuth?.(
123
+ profile
124
+ ) ?? null;
125
+ return runtimeAuth ? { ...runtimeAuth, auth } : null;
126
+ }
127
+ function createModelAuthResolver() {
128
+ return {
129
+ resolveRuntimeAuth(input) {
130
+ const profile = (0, import_model_auth_profiles.getDefaultModelAuthProfile)((0, import_model_auth_profiles.loadModelAuthProfilesSync)(), input.provider);
131
+ return profile ? resolveRuntimeAuthFromProfile(profile) : null;
132
+ }
133
+ };
134
+ }
135
+ // Annotate the CommonJS export names for ESM import in node:
136
+ 0 && (module.exports = {
137
+ createModelAuthResolver,
138
+ markModelAuthProviderConnected,
139
+ saveChatGPTLoginModelAuthProfile,
140
+ startModelAuthBrowserLogin
141
+ });
@@ -0,0 +1,17 @@
1
+ import type { LlmAuthResolver } from '@dexto/core';
2
+ import { type ChatGPTOAuthCredential } from './chatgpt-oauth.js';
3
+ import { type ModelAuthProfile } from './model-auth-profiles.js';
4
+ export type PendingModelAuthBrowserLogin = {
5
+ label: string;
6
+ authUrl: string;
7
+ waitForProfile(): Promise<ModelAuthProfile>;
8
+ cancel(): Promise<void>;
9
+ };
10
+ export declare function saveChatGPTLoginModelAuthProfile(credential: ChatGPTOAuthCredential): Promise<ModelAuthProfile>;
11
+ export declare function startModelAuthBrowserLogin(input: {
12
+ providerId: string;
13
+ methodId: string;
14
+ }): Promise<PendingModelAuthBrowserLogin>;
15
+ export declare function markModelAuthProviderConnected(providerId: string): Promise<void>;
16
+ export declare function createModelAuthResolver(): LlmAuthResolver;
17
+ //# sourceMappingURL=model-auth-handlers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model-auth-handlers.d.ts","sourceRoot":"","sources":["../../src/auth/model-auth-handlers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAGH,KAAK,sBAAsB,EAE9B,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAKH,KAAK,gBAAgB,EAExB,MAAM,0BAA0B,CAAC;AAOlC,MAAM,MAAM,4BAA4B,GAAG;IACvC,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,cAAc,IAAI,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAC5C,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3B,CAAC;AAiCF,wBAAsB,gCAAgC,CAClD,UAAU,EAAE,sBAAsB,GACnC,OAAO,CAAC,gBAAgB,CAAC,CAQ3B;AA2CD,wBAAsB,0BAA0B,CAAC,KAAK,EAAE;IACpD,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;CACpB,GAAG,OAAO,CAAC,4BAA4B,CAAC,CASxC;AAED,wBAAsB,8BAA8B,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAetF;AAyBD,wBAAgB,uBAAuB,IAAI,eAAe,CAOzD"}
@@ -0,0 +1,126 @@
1
+ import {
2
+ createChatGPTRuntimeAuth,
3
+ startChatGPTBrowserLogin
4
+ } from "./chatgpt-oauth.js";
5
+ import {
6
+ getDefaultModelAuthProfile,
7
+ getModelAuthProfileId,
8
+ loadModelAuthProfilesSync,
9
+ upsertModelAuthProfile
10
+ } from "./model-auth-profiles.js";
11
+ import {
12
+ globalPreferencesExist,
13
+ loadGlobalPreferences,
14
+ updateGlobalPreferences
15
+ } from "../preferences/index.js";
16
+ function toChatGPTCredential(credential) {
17
+ return {
18
+ type: "oauth",
19
+ issuer: "https://auth.openai.com",
20
+ refreshToken: credential.refreshToken,
21
+ accessToken: credential.accessToken,
22
+ expiresAt: credential.expiresAt,
23
+ accountId: credential.metadata?.accountId
24
+ };
25
+ }
26
+ function fromChatGPTCredential(credential) {
27
+ return {
28
+ type: "oauth",
29
+ issuer: credential.issuer,
30
+ refreshToken: credential.refreshToken,
31
+ accessToken: credential.accessToken,
32
+ expiresAt: credential.expiresAt,
33
+ ...credential.accountId ? { metadata: { accountId: credential.accountId } } : {}
34
+ };
35
+ }
36
+ async function saveChatGPTLoginModelAuthProfile(credential) {
37
+ return upsertModelAuthProfile({
38
+ id: getModelAuthProfileId("openai", "chatgpt_login"),
39
+ providerId: "openai",
40
+ methodId: "chatgpt_login",
41
+ label: "ChatGPT Login",
42
+ credential: fromChatGPTCredential(credential)
43
+ });
44
+ }
45
+ async function updateChatGPTLoginCredential(credential) {
46
+ await saveChatGPTLoginModelAuthProfile(credential);
47
+ }
48
+ async function startOpenAIChatGPTBrowserLogin() {
49
+ const login = await startChatGPTBrowserLogin();
50
+ return {
51
+ label: "ChatGPT Login",
52
+ authUrl: login.authUrl,
53
+ waitForProfile: async () => saveChatGPTLoginModelAuthProfile(await login.waitForCredential()),
54
+ cancel: login.cancel
55
+ };
56
+ }
57
+ const MODEL_AUTH_METHOD_HANDLERS = [
58
+ {
59
+ providerId: "openai",
60
+ methodId: "chatgpt_login",
61
+ startBrowserLogin: startOpenAIChatGPTBrowserLogin,
62
+ resolveRuntimeAuth: (profile) => profile.credential.type === "oauth" ? createChatGPTRuntimeAuth({
63
+ credential: toChatGPTCredential(profile.credential),
64
+ updateCredential: updateChatGPTLoginCredential
65
+ }) : null
66
+ }
67
+ ];
68
+ function getModelAuthMethodHandler(providerId, methodId) {
69
+ return MODEL_AUTH_METHOD_HANDLERS.find(
70
+ (handler) => handler.providerId === providerId && handler.methodId === methodId
71
+ ) ?? null;
72
+ }
73
+ async function startModelAuthBrowserLogin(input) {
74
+ const handler = getModelAuthMethodHandler(input.providerId, input.methodId);
75
+ if (!handler?.startBrowserLogin) {
76
+ throw new Error(
77
+ `Auth method is not implemented yet: ${input.providerId}/${input.methodId}`
78
+ );
79
+ }
80
+ return handler.startBrowserLogin();
81
+ }
82
+ async function markModelAuthProviderConnected(providerId) {
83
+ if (!globalPreferencesExist()) {
84
+ return;
85
+ }
86
+ const preferences = await loadGlobalPreferences();
87
+ if (preferences.llm.provider !== providerId) {
88
+ return;
89
+ }
90
+ await updateGlobalPreferences({
91
+ setup: {
92
+ apiKeyPending: false
93
+ }
94
+ });
95
+ }
96
+ function resolveRuntimeAuthFromProfile(profile) {
97
+ const auth = {
98
+ source: "profile",
99
+ profileId: profile.id,
100
+ providerId: profile.providerId,
101
+ methodId: profile.methodId,
102
+ credentialType: profile.credential.type
103
+ };
104
+ if (profile.credential.type === "api_key_env") {
105
+ const apiKey = process.env[profile.credential.envVar]?.trim();
106
+ return apiKey ? { apiKey, auth } : null;
107
+ }
108
+ const runtimeAuth = getModelAuthMethodHandler(profile.providerId, profile.methodId)?.resolveRuntimeAuth?.(
109
+ profile
110
+ ) ?? null;
111
+ return runtimeAuth ? { ...runtimeAuth, auth } : null;
112
+ }
113
+ function createModelAuthResolver() {
114
+ return {
115
+ resolveRuntimeAuth(input) {
116
+ const profile = getDefaultModelAuthProfile(loadModelAuthProfilesSync(), input.provider);
117
+ return profile ? resolveRuntimeAuthFromProfile(profile) : null;
118
+ }
119
+ };
120
+ }
121
+ export {
122
+ createModelAuthResolver,
123
+ markModelAuthProviderConnected,
124
+ saveChatGPTLoginModelAuthProfile,
125
+ startModelAuthBrowserLogin
126
+ };