-
Notifications
You must be signed in to change notification settings - Fork 161
Add plan-based Claude/OpenAI connections with OAuth #509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
0dce449
feat: Add OpenAI Codex provider and OAuth integration
kevin-on 1e727f1
feat: Enhance HTTP transport for Codex integration
kevin-on 71bbb96
feat: Add Anthropic Claude Code provider with OAuth support
kevin-on 3cea4e3
chore: Update OpenAI package and refactor message adapters
kevin-on eb11f00
feat: Add OpenAI Codex model settings and reasoning support
kevin-on 6d24448
feat: Update ChatModelSettings to support new Anthropic Claude Code m…
kevin-on 2dcd0cf
feat: Refactor provider settings and enhance migration logic for Open…
kevin-on 51629bf
feat: Refactor provider types and update settings for OpenAI and Anth…
kevin-on e5ee235
feat: Implement subscription connection settings for OpenAI and Anthr…
kevin-on 064bbfe
feat: Add PLAN_PROVIDER_TYPES constant and update provider filtering …
kevin-on 1af22b1
refactor: Clean up provider connection logic and improve formatting
kevin-on 7c1b0fa
chore: Update .gitignore to exclude CLAUDE.md
kevin-on 1dc9cb0
style: Enhance layout and descriptions in PlanConnectionsSection
kevin-on ee4d21e
fix: Resolve coderabbit review
kevin-on b0bd0fd
chore: Update column header in ProvidersSection from 'Credential' to …
kevin-on b6e968b
chore: Update README for v1.2.7 release
kevin-on 6d54f38
test: Enhance migration tests for v14 to v15
kevin-on File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
src/components/settings/modals/ConnectClaudePlanModal.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| import { App, Notice } from 'obsidian' | ||
| import { useEffect, useState } from 'react' | ||
|
|
||
| import { PROVIDER_TYPES_INFO } from '../../../constants' | ||
| import { | ||
| buildClaudeCodeAuthorizeUrl, | ||
| exchangeClaudeCodeForTokens, | ||
| generateClaudeCodePkce, | ||
| generateClaudeCodeState, | ||
| } from '../../../core/llm/claudeCodeAuth' | ||
| import SmartComposerPlugin from '../../../main' | ||
| import { ObsidianButton } from '../../common/ObsidianButton' | ||
| import { ObsidianSetting } from '../../common/ObsidianSetting' | ||
| import { ObsidianTextInput } from '../../common/ObsidianTextInput' | ||
| import { ReactModal } from '../../common/ReactModal' | ||
|
|
||
| type ConnectClaudePlanModalProps = { | ||
| plugin: SmartComposerPlugin | ||
| onClose: () => void | ||
| } | ||
|
|
||
| const CLAUDE_PLAN_PROVIDER_ID = PROVIDER_TYPES_INFO['anthropic-plan'] | ||
| .defaultProviderId as string | ||
|
|
||
| export class ConnectClaudePlanModal extends ReactModal<ConnectClaudePlanModalProps> { | ||
| constructor(app: App, plugin: SmartComposerPlugin) { | ||
| super({ | ||
| app: app, | ||
| Component: ConnectClaudePlanModalComponent, | ||
| props: { plugin }, | ||
| options: { | ||
| title: 'Connect Claude subscription', | ||
| }, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| function ConnectClaudePlanModalComponent({ | ||
| plugin, | ||
| onClose, | ||
| }: ConnectClaudePlanModalProps) { | ||
| const [authorizeUrl, setAuthorizeUrl] = useState('') | ||
| const [code, setCode] = useState('') | ||
| const [pkceVerifier, setPkceVerifier] = useState('') | ||
| const [state, setState] = useState('') | ||
| const [isConnecting, setIsConnecting] = useState(false) | ||
|
|
||
| const hasAuthData = authorizeUrl.length > 0 && pkceVerifier.length > 0 | ||
|
|
||
| useEffect(() => { | ||
| ;(async () => { | ||
| try { | ||
| const pkce = await generateClaudeCodePkce() | ||
| const newState = generateClaudeCodeState() | ||
| const url = buildClaudeCodeAuthorizeUrl({ pkce, state: newState }) | ||
| setPkceVerifier(pkce.verifier) | ||
| setState(newState) | ||
| setAuthorizeUrl(url) | ||
| } catch { | ||
| new Notice('Failed to initialize OAuth flow') | ||
| } | ||
| })() | ||
| }, []) | ||
|
|
||
| const connect = async () => { | ||
| if (isConnecting) return | ||
| if (!hasAuthData) { | ||
| new Notice('OAuth link is not ready. Try again.') | ||
| return | ||
| } | ||
| if (!code) { | ||
| new Notice('Paste the authorization code') | ||
| return | ||
| } | ||
|
|
||
| try { | ||
| setIsConnecting(true) | ||
|
|
||
| const tokens = await exchangeClaudeCodeForTokens({ | ||
| code, | ||
| pkceVerifier, | ||
| state, | ||
| }) | ||
|
|
||
| if ( | ||
| !plugin.settings.providers.find( | ||
| (p) => | ||
| p.type === 'anthropic-plan' && p.id === CLAUDE_PLAN_PROVIDER_ID, | ||
| ) | ||
| ) { | ||
| throw new Error('Claude Plan provider not found.') | ||
| } | ||
| await plugin.setSettings({ | ||
| ...plugin.settings, | ||
| providers: plugin.settings.providers.map((p) => { | ||
| if (p.type === 'anthropic-plan' && p.id === CLAUDE_PLAN_PROVIDER_ID) { | ||
| return { | ||
| ...p, | ||
| oauth: { | ||
| accessToken: tokens.access_token, | ||
| refreshToken: tokens.refresh_token, | ||
| expiresAt: Date.now() + (tokens.expires_in ?? 3600) * 1000, | ||
| }, | ||
| } | ||
| } | ||
| return p | ||
| }), | ||
| }) | ||
|
|
||
| new Notice('Claude Plan connected') | ||
| onClose() | ||
| } catch { | ||
| new Notice('OAuth failed. Double-check the code and try again.') | ||
kevin-on marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } finally { | ||
| setIsConnecting(false) | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <div> | ||
| <div className="smtcmp-plan-connect-steps"> | ||
| <div className="smtcmp-plan-connect-steps-title">How it works</div> | ||
| <ol> | ||
| <li>Login to Claude in your browser</li> | ||
| <li>Copy the code from the redirected URL</li> | ||
| <li>Paste it here and click "Connect"</li> | ||
| </ol> | ||
| </div> | ||
|
|
||
| <ObsidianSetting | ||
| name="Claude login" | ||
| desc="Login to Claude Code in your browser." | ||
| > | ||
| <ObsidianButton | ||
| text="Login to Claude" | ||
| disabled={!authorizeUrl || isConnecting} | ||
| onClick={() => { | ||
| if (!authorizeUrl) return | ||
| window.open(authorizeUrl, '_blank') | ||
| }} | ||
| cta | ||
| /> | ||
| </ObsidianSetting> | ||
|
|
||
| <ObsidianSetting | ||
| name="Authorization code" | ||
| desc="Paste the code from the redirected URL." | ||
| required | ||
| > | ||
| <ObsidianTextInput | ||
| value={code} | ||
| placeholder="Paste authorization code" | ||
| onChange={(value) => setCode(value)} | ||
| /> | ||
| </ObsidianSetting> | ||
|
|
||
| <ObsidianSetting> | ||
| <ObsidianButton | ||
| text="Connect" | ||
| onClick={() => void connect()} | ||
| disabled={isConnecting} | ||
| cta | ||
| /> | ||
| <ObsidianButton | ||
| text="Cancel" | ||
| onClick={onClose} | ||
| disabled={isConnecting} | ||
| /> | ||
| </ObsidianSetting> | ||
| </div> | ||
| ) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.