|
1 | 1 | import { definePluginEntry } from "openclaw/plugin-sdk/plugin-entry"; |
| 2 | +import type { ProviderAuthContext, ProviderAuthResult } from "openclaw/plugin-sdk/plugin-entry"; |
| 3 | +import type { ProviderAuthMethod } from "openclaw/plugin-sdk/plugin-entry"; |
| 4 | +import type { ProviderPlugin } from "openclaw/plugin-sdk/provider-model-shared"; |
| 5 | +import { |
| 6 | + OPENAI_API_KEY_LABEL, |
| 7 | + OPENAI_API_KEY_WIZARD_GROUP, |
| 8 | + OPENAI_CODEX_DEVICE_PAIRING_HINT, |
| 9 | + OPENAI_CODEX_DEVICE_PAIRING_LABEL, |
| 10 | + OPENAI_CODEX_LOGIN_HINT, |
| 11 | + OPENAI_CODEX_LOGIN_LABEL, |
| 12 | + OPENAI_CODEX_WIZARD_GROUP, |
| 13 | +} from "./auth-choice-copy.js"; |
2 | 14 | import { buildOpenAICodexCliBackend } from "./cli-backend.js"; |
3 | 15 |
|
| 16 | +async function runOpenAIProviderAuthMethod( |
| 17 | + methodId: string, |
| 18 | + ctx: ProviderAuthContext, |
| 19 | +): Promise<ProviderAuthResult> { |
| 20 | + const { buildOpenAIProvider } = await import("./openai-provider.js"); |
| 21 | + const method = buildOpenAIProvider().auth.find((entry) => entry.id === methodId); |
| 22 | + if (!method) { |
| 23 | + return { profiles: [] }; |
| 24 | + } |
| 25 | + return method.run(ctx); |
| 26 | +} |
| 27 | + |
| 28 | +async function runOpenAICodexProviderAuthMethod( |
| 29 | + methodId: string, |
| 30 | + ctx: ProviderAuthContext, |
| 31 | +): Promise<ProviderAuthResult> { |
| 32 | + const { buildOpenAICodexProviderPlugin } = await import("./openai-codex-provider.js"); |
| 33 | + const method = buildOpenAICodexProviderPlugin().auth.find((entry) => entry.id === methodId); |
| 34 | + if (!method) { |
| 35 | + return { profiles: [] }; |
| 36 | + } |
| 37 | + return method.run(ctx); |
| 38 | +} |
| 39 | + |
| 40 | +function buildOpenAISetupProvider(): ProviderPlugin { |
| 41 | + const apiKeyMethod = { |
| 42 | + id: "api-key", |
| 43 | + label: OPENAI_API_KEY_LABEL, |
| 44 | + hint: "Use your OpenAI API key directly", |
| 45 | + kind: "api_key", |
| 46 | + wizard: { |
| 47 | + choiceId: "openai-api-key", |
| 48 | + choiceLabel: OPENAI_API_KEY_LABEL, |
| 49 | + ...OPENAI_API_KEY_WIZARD_GROUP, |
| 50 | + }, |
| 51 | + run: async (ctx) => runOpenAIProviderAuthMethod("api-key", ctx), |
| 52 | + } satisfies ProviderAuthMethod; |
| 53 | + |
| 54 | + return { |
| 55 | + id: "openai", |
| 56 | + label: "OpenAI", |
| 57 | + docsPath: "/providers/models", |
| 58 | + envVars: ["OPENAI_API_KEY"], |
| 59 | + auth: [apiKeyMethod], |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +function buildOpenAICodexSetupProvider(): ProviderPlugin { |
| 64 | + const oauthMethod = { |
| 65 | + id: "oauth", |
| 66 | + label: OPENAI_CODEX_LOGIN_LABEL, |
| 67 | + hint: OPENAI_CODEX_LOGIN_HINT, |
| 68 | + kind: "oauth", |
| 69 | + wizard: { |
| 70 | + choiceId: "openai-codex", |
| 71 | + choiceLabel: OPENAI_CODEX_LOGIN_LABEL, |
| 72 | + choiceHint: OPENAI_CODEX_LOGIN_HINT, |
| 73 | + assistantPriority: -30, |
| 74 | + ...OPENAI_CODEX_WIZARD_GROUP, |
| 75 | + }, |
| 76 | + run: async (ctx) => runOpenAICodexProviderAuthMethod("oauth", ctx), |
| 77 | + } satisfies ProviderAuthMethod; |
| 78 | + |
| 79 | + const deviceCodeMethod = { |
| 80 | + id: "device-code", |
| 81 | + label: OPENAI_CODEX_DEVICE_PAIRING_LABEL, |
| 82 | + hint: OPENAI_CODEX_DEVICE_PAIRING_HINT, |
| 83 | + kind: "device_code", |
| 84 | + wizard: { |
| 85 | + choiceId: "openai-codex-device-code", |
| 86 | + choiceLabel: OPENAI_CODEX_DEVICE_PAIRING_LABEL, |
| 87 | + choiceHint: OPENAI_CODEX_DEVICE_PAIRING_HINT, |
| 88 | + assistantPriority: -10, |
| 89 | + ...OPENAI_CODEX_WIZARD_GROUP, |
| 90 | + }, |
| 91 | + run: async (ctx) => runOpenAICodexProviderAuthMethod("device-code", ctx), |
| 92 | + } satisfies ProviderAuthMethod; |
| 93 | + |
| 94 | + return { |
| 95 | + id: "openai-codex", |
| 96 | + label: "OpenAI Codex", |
| 97 | + docsPath: "/providers/models", |
| 98 | + auth: [oauthMethod, deviceCodeMethod], |
| 99 | + }; |
| 100 | +} |
| 101 | + |
4 | 102 | export default definePluginEntry({ |
5 | 103 | id: "openai", |
6 | 104 | name: "OpenAI Setup", |
7 | 105 | description: "Lightweight OpenAI setup hooks", |
8 | 106 | register(api) { |
| 107 | + api.registerProvider(buildOpenAISetupProvider()); |
| 108 | + api.registerProvider(buildOpenAICodexSetupProvider()); |
9 | 109 | api.registerCliBackend(buildOpenAICodexCliBackend()); |
10 | 110 | }, |
11 | 111 | }); |
0 commit comments