|
| 1 | +import { definePluginEntry } from "openclaw/plugin-sdk/core"; |
| 2 | +import { |
| 3 | + buildOauthProviderAuthResult, |
| 4 | + createProviderApiKeyAuthMethod, |
| 5 | + loginChutes, |
| 6 | + resolveOAuthApiKeyMarker, |
| 7 | + type ProviderAuthContext, |
| 8 | + type ProviderAuthResult, |
| 9 | +} from "openclaw/plugin-sdk/provider-auth"; |
| 10 | +import { |
| 11 | + CHUTES_DEFAULT_MODEL_REF, |
| 12 | + applyChutesApiKeyConfig, |
| 13 | + applyChutesProviderConfig, |
| 14 | +} from "./onboard.js"; |
| 15 | +import { buildChutesProvider } from "./provider-catalog.js"; |
| 16 | + |
| 17 | +const PROVIDER_ID = "chutes"; |
| 18 | + |
| 19 | +async function runChutesOAuth(ctx: ProviderAuthContext): Promise<ProviderAuthResult> { |
| 20 | + const isRemote = ctx.isRemote; |
| 21 | + const redirectUri = |
| 22 | + process.env.CHUTES_OAUTH_REDIRECT_URI?.trim() || "http://127.0.0.1:1456/oauth-callback"; |
| 23 | + const scopes = process.env.CHUTES_OAUTH_SCOPES?.trim() || "openid profile chutes:invoke"; |
| 24 | + const clientId = |
| 25 | + process.env.CHUTES_CLIENT_ID?.trim() || |
| 26 | + String( |
| 27 | + await ctx.prompter.text({ |
| 28 | + message: "Enter Chutes OAuth client id", |
| 29 | + placeholder: "cid_xxx", |
| 30 | + validate: (value: string) => (value?.trim() ? undefined : "Required"), |
| 31 | + }), |
| 32 | + ).trim(); |
| 33 | + const clientSecret = process.env.CHUTES_CLIENT_SECRET?.trim() || undefined; |
| 34 | + |
| 35 | + await ctx.prompter.note( |
| 36 | + isRemote |
| 37 | + ? [ |
| 38 | + "You are running in a remote/VPS environment.", |
| 39 | + "A URL will be shown for you to open in your LOCAL browser.", |
| 40 | + "After signing in, paste the redirect URL back here.", |
| 41 | + "", |
| 42 | + `Redirect URI: ${redirectUri}`, |
| 43 | + ].join("\n") |
| 44 | + : [ |
| 45 | + "Browser will open for Chutes authentication.", |
| 46 | + "If the callback doesn't auto-complete, paste the redirect URL.", |
| 47 | + "", |
| 48 | + `Redirect URI: ${redirectUri}`, |
| 49 | + ].join("\n"), |
| 50 | + "Chutes OAuth", |
| 51 | + ); |
| 52 | + |
| 53 | + const progress = ctx.prompter.progress("Starting Chutes OAuth…"); |
| 54 | + try { |
| 55 | + const { onAuth, onPrompt } = ctx.oauth.createVpsAwareHandlers({ |
| 56 | + isRemote, |
| 57 | + prompter: ctx.prompter, |
| 58 | + runtime: ctx.runtime, |
| 59 | + spin: progress, |
| 60 | + openUrl: ctx.openUrl, |
| 61 | + localBrowserMessage: "Complete sign-in in browser…", |
| 62 | + }); |
| 63 | + |
| 64 | + const creds = await loginChutes({ |
| 65 | + app: { |
| 66 | + clientId, |
| 67 | + clientSecret, |
| 68 | + redirectUri, |
| 69 | + scopes: scopes.split(/\s+/).filter(Boolean), |
| 70 | + }, |
| 71 | + manual: isRemote, |
| 72 | + onAuth, |
| 73 | + onPrompt, |
| 74 | + onProgress: (message) => progress.update(message), |
| 75 | + }); |
| 76 | + |
| 77 | + progress.stop("Chutes OAuth complete"); |
| 78 | + |
| 79 | + return buildOauthProviderAuthResult({ |
| 80 | + providerId: PROVIDER_ID, |
| 81 | + defaultModel: CHUTES_DEFAULT_MODEL_REF, |
| 82 | + access: creds.access, |
| 83 | + refresh: creds.refresh, |
| 84 | + expires: creds.expires, |
| 85 | + email: typeof creds.email === "string" ? creds.email : undefined, |
| 86 | + credentialExtra: { |
| 87 | + clientId, |
| 88 | + ...("accountId" in creds && typeof creds.accountId === "string" |
| 89 | + ? { accountId: creds.accountId } |
| 90 | + : {}), |
| 91 | + }, |
| 92 | + configPatch: applyChutesProviderConfig({}), |
| 93 | + notes: [ |
| 94 | + "Chutes OAuth tokens auto-refresh. Re-run login if refresh fails or access is revoked.", |
| 95 | + `Redirect URI: ${redirectUri}`, |
| 96 | + ], |
| 97 | + }); |
| 98 | + } catch (err) { |
| 99 | + progress.stop("Chutes OAuth failed"); |
| 100 | + await ctx.prompter.note( |
| 101 | + [ |
| 102 | + "Trouble with OAuth?", |
| 103 | + "Verify CHUTES_CLIENT_ID (and CHUTES_CLIENT_SECRET if required).", |
| 104 | + `Verify the OAuth app redirect URI includes: ${redirectUri}`, |
| 105 | + "Chutes docs: https://chutes.ai/docs/sign-in-with-chutes/overview", |
| 106 | + ].join("\n"), |
| 107 | + "OAuth help", |
| 108 | + ); |
| 109 | + throw err; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +export default definePluginEntry({ |
| 114 | + id: PROVIDER_ID, |
| 115 | + name: "Chutes Provider", |
| 116 | + description: "Bundled Chutes.ai provider plugin", |
| 117 | + register(api) { |
| 118 | + api.registerProvider({ |
| 119 | + id: PROVIDER_ID, |
| 120 | + label: "Chutes", |
| 121 | + docsPath: "/providers/chutes", |
| 122 | + envVars: ["CHUTES_API_KEY", "CHUTES_OAUTH_TOKEN"], |
| 123 | + auth: [ |
| 124 | + { |
| 125 | + id: "oauth", |
| 126 | + label: "Chutes OAuth", |
| 127 | + hint: "Browser sign-in", |
| 128 | + kind: "oauth", |
| 129 | + wizard: { |
| 130 | + choiceId: "chutes", |
| 131 | + choiceLabel: "Chutes (OAuth)", |
| 132 | + choiceHint: "Browser sign-in", |
| 133 | + groupId: "chutes", |
| 134 | + groupLabel: "Chutes", |
| 135 | + groupHint: "OAuth + API key", |
| 136 | + }, |
| 137 | + run: async (ctx) => await runChutesOAuth(ctx), |
| 138 | + }, |
| 139 | + createProviderApiKeyAuthMethod({ |
| 140 | + providerId: PROVIDER_ID, |
| 141 | + methodId: "api-key", |
| 142 | + label: "Chutes API key", |
| 143 | + hint: "Open-source models including Llama, DeepSeek, and more", |
| 144 | + optionKey: "chutesApiKey", |
| 145 | + flagName: "--chutes-api-key", |
| 146 | + envVar: "CHUTES_API_KEY", |
| 147 | + promptMessage: "Enter Chutes API key", |
| 148 | + noteTitle: "Chutes", |
| 149 | + noteMessage: [ |
| 150 | + "Chutes provides access to leading open-source models including Llama, DeepSeek, and more.", |
| 151 | + "Get your API key at: https://chutes.ai/settings/api-keys", |
| 152 | + ].join("\n"), |
| 153 | + defaultModel: CHUTES_DEFAULT_MODEL_REF, |
| 154 | + expectedProviders: ["chutes"], |
| 155 | + applyConfig: (cfg) => applyChutesApiKeyConfig(cfg), |
| 156 | + wizard: { |
| 157 | + choiceId: "chutes-api-key", |
| 158 | + choiceLabel: "Chutes API key", |
| 159 | + groupId: "chutes", |
| 160 | + groupLabel: "Chutes", |
| 161 | + groupHint: "OAuth + API key", |
| 162 | + }, |
| 163 | + }), |
| 164 | + ], |
| 165 | + catalog: { |
| 166 | + order: "profile", |
| 167 | + run: async (ctx) => { |
| 168 | + const { apiKey, discoveryApiKey } = ctx.resolveProviderAuth(PROVIDER_ID, { |
| 169 | + oauthMarker: resolveOAuthApiKeyMarker(PROVIDER_ID), |
| 170 | + }); |
| 171 | + if (!apiKey) { |
| 172 | + return null; |
| 173 | + } |
| 174 | + return { |
| 175 | + provider: { |
| 176 | + ...(await buildChutesProvider(discoveryApiKey)), |
| 177 | + apiKey, |
| 178 | + }, |
| 179 | + }; |
| 180 | + }, |
| 181 | + }, |
| 182 | + }); |
| 183 | + }, |
| 184 | +}); |
0 commit comments