-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat(codex): connect a chatgpt plan on web and bill codex runs to it #108909
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
Open
puemos
wants to merge
14
commits into
master
Choose a base branch
from
posthog/codex-personal-integration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
89058b8
feat(settings): connect codex as a personal integration on web
puemos c510cd7
chore(settings): drop posthog desktop mention from codex copy
puemos 6f39df8
feat(settings): use only the device code flow to connect codex
puemos c745997
chore(settings): drop repeated sentence from codex empty state
puemos 7103b4d
chore(visual): update storybook baselines
puemos 4cfacd0
fix(settings): match kea typegen output for codex integration logic
puemos 4e92cbb
feat(settings): copy the codex sign-in to the clipboard and hide it o…
puemos 487aacd
feat(posthog-ai): let codex runs bill to the user's chatgpt plan
puemos 2f2393f
feat(settings): move the codex connection to an ai subscriptions section
puemos fffc18e
fix(settings): close codex connect races found in review
puemos 5287394
fix(posthog-ai): select the chatgpt plan after connecting from the co…
puemos b904680
fix(posthog-ai): apply codex billing to seeded tasks and skip unusabl…
puemos 2706704
chore(visual): update storybook baselines
puemos f427526
fix(posthog-ai): keep a run's codex billing while the chatgpt connect…
puemos 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
Large diffs are not rendered by default.
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
137 changes: 137 additions & 0 deletions
137
frontend/src/scenes/settings/user/CodexConnectModal.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,137 @@ | ||
| import { useActions, useValues } from 'kea' | ||
|
|
||
| import { IconCheck } from '@posthog/icons' | ||
| import { LemonBanner, LemonButton, LemonInput, LemonModal, LemonSegmentedButton, Link } from '@posthog/lemon-ui' | ||
|
|
||
| import { CodeSnippet, Language } from 'lib/components/CodeSnippet' | ||
| import { platformCommandControlKey } from 'lib/utils/dom' | ||
|
|
||
| import { CODEX_LOGIN_PLATFORM_OPTIONS, codexLoginCommand } from './codexLoginCommands' | ||
| import { personalCodexIntegrationLogic } from './personalCodexIntegrationLogic' | ||
|
|
||
| const PLATFORM_HINTS = { | ||
| macos: 'Run it in Terminal.', | ||
| linux: 'Run it in a terminal. It needs wl-clipboard, xclip, or xsel to copy to the clipboard.', | ||
| windows: 'Run it in PowerShell.', | ||
| } | ||
|
|
||
| export const SETTINGS_CODEX_CONNECT_OPENER = 'settings' | ||
|
|
||
| export interface CodexConnectModalProps { | ||
| /** Several surfaces can show the dialog at once. Each opens only for the opener that asked for it. */ | ||
| opener: string | ||
| } | ||
|
|
||
| export function CodexConnectModal({ opener }: CodexConnectModalProps): JSX.Element { | ||
| const { connectModalOpener, authFileText, connectError, connecting, loginPlatform } = | ||
| useValues(personalCodexIntegrationLogic) | ||
| const { closeConnectModal, setLoginPlatform, commandCopied, pasteAuthFile, pasteFromClipboard, submitAuthFile } = | ||
| useActions(personalCodexIntegrationLogic) | ||
| const pasted = authFileText.length > 0 | ||
|
|
||
| return ( | ||
| <LemonModal | ||
| isOpen={connectModalOpener === opener} | ||
| onClose={closeConnectModal} | ||
| closable={!connecting} | ||
| title="Connect Codex" | ||
| description="Sign in to ChatGPT with a device code in your terminal. Then paste the sign-in here." | ||
| width={560} | ||
| footer={ | ||
| <> | ||
| <LemonButton | ||
| type="secondary" | ||
| onClick={closeConnectModal} | ||
| disabledReason={connecting ? 'Connecting…' : undefined} | ||
| > | ||
| Cancel | ||
| </LemonButton> | ||
| <LemonButton | ||
| type="primary" | ||
| onClick={submitAuthFile} | ||
| loading={connecting} | ||
| disabledReason={!pasted ? 'Paste the sign-in first' : undefined} | ||
| data-attr="codex-connect-submit" | ||
| > | ||
| Connect | ||
| </LemonButton> | ||
| </> | ||
| } | ||
| > | ||
| <div className="flex flex-col gap-4"> | ||
| <div className="flex flex-col gap-2"> | ||
| <div className="flex flex-wrap items-center justify-between gap-2"> | ||
| <h4 className="mb-0">1. Run this command</h4> | ||
| <LemonSegmentedButton | ||
| size="small" | ||
| value={loginPlatform} | ||
| onChange={setLoginPlatform} | ||
| options={CODEX_LOGIN_PLATFORM_OPTIONS} | ||
| data-attr="codex-login-platform" | ||
| /> | ||
| </div> | ||
| <CodeSnippet | ||
| language={loginPlatform === 'windows' ? Language.Text : Language.Bash} | ||
| compact | ||
| wrap | ||
| thing="command" | ||
| onCopy={commandCopied} | ||
| > | ||
| {codexLoginCommand(loginPlatform)} | ||
| </CodeSnippet> | ||
| <p className="mb-0 text-xs text-secondary"> | ||
| <span>{PLATFORM_HINTS[loginPlatform]}</span> Open the link that it shows, sign in, and enter the | ||
| one-time code. The command copies your sign-in to the clipboard and deletes its temporary | ||
| folder, so nothing stays on your computer. | ||
| </p> | ||
| <p className="mb-0 text-xs text-secondary"> | ||
| You need the{' '} | ||
| <Link to="https://developers.openai.com/codex/cli" target="_blank"> | ||
| Codex CLI | ||
| </Link> | ||
| . Install it with <code>npm install -g @openai/codex</code>. | ||
| </p> | ||
| <p className="mb-0 text-xs text-secondary"> | ||
|
puemos marked this conversation as resolved.
|
||
| Turn on device code login in your ChatGPT security settings first. In a ChatGPT workspace, an | ||
| admin turns it on in the workspace permissions. | ||
| </p> | ||
| </div> | ||
| <div className="flex flex-col gap-2"> | ||
| <h4 className="mb-0">2. Paste the sign-in</h4> | ||
| <div className="flex flex-wrap items-center gap-2"> | ||
| <LemonInput | ||
| className="min-w-48 flex-1" | ||
| value="" | ||
| onChange={() => undefined} | ||
| onPaste={(event) => { | ||
| event.preventDefault() | ||
| pasteAuthFile(event.clipboardData.getData('text')) | ||
| }} | ||
| placeholder={ | ||
| pasted ? 'Sign-in received' : `Click here and press ${platformCommandControlKey('V')}` | ||
| } | ||
| suffix={pasted ? <IconCheck className="text-success" /> : undefined} | ||
| autoComplete="off" | ||
| spellCheck={false} | ||
| disabled={connecting} | ||
| data-attr="codex-auth-file-input" | ||
| /> | ||
| <LemonButton | ||
| type="secondary" | ||
| onClick={pasteFromClipboard} | ||
| disabledReason={connecting ? 'Connecting…' : undefined} | ||
| data-attr="codex-paste-from-clipboard" | ||
| > | ||
| Paste from clipboard | ||
| </LemonButton> | ||
| </div> | ||
| </div> | ||
| {connectError ? <LemonBanner type="error">{connectError}</LemonBanner> : null} | ||
| <LemonBanner type="info"> | ||
| PostHog never shows your sign-in and tries to clear your clipboard after you paste. It stores the | ||
| sign-in encrypted and uses it only for your Codex cloud tasks. You can disconnect at any time. | ||
| </LemonBanner> | ||
| </div> | ||
| </LemonModal> | ||
| ) | ||
| } | ||
128 changes: 128 additions & 0 deletions
128
frontend/src/scenes/settings/user/PersonalCodexIntegration.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,128 @@ | ||
| import { useActions, useValues } from 'kea' | ||
|
|
||
| import { IconPlus, IconTerminal, IconTrash } from '@posthog/icons' | ||
| import { LemonBanner, LemonButton, LemonDialog, LemonSkeleton, LemonTag } from '@posthog/lemon-ui' | ||
|
|
||
| import { TZLabel } from 'lib/components/TZLabel' | ||
|
|
||
| import { CodexIntegrationStatusEnumApi, type UserCodexIntegrationApi } from '~/generated/core/api.schemas' | ||
|
|
||
| import { CodexConnectModal, SETTINGS_CODEX_CONNECT_OPENER } from './CodexConnectModal' | ||
| import { personalCodexIntegrationLogic } from './personalCodexIntegrationLogic' | ||
|
|
||
| function CodexAccountRow({ integration }: { integration: UserCodexIntegrationApi }): JSX.Element { | ||
| const { disconnectCodex } = useActions(personalCodexIntegrationLogic) | ||
| const { codexIntegrationLoading } = useValues(personalCodexIntegrationLogic) | ||
|
|
||
| const handleDisconnect = (): void => { | ||
| LemonDialog.open({ | ||
| title: 'Disconnect Codex?', | ||
| description: ( | ||
| <p> | ||
| PostHog removes your ChatGPT sign-in. Codex cloud tasks that use your ChatGPT plan stop working | ||
| until you connect again. | ||
| </p> | ||
| ), | ||
| primaryButton: { | ||
| children: 'Disconnect', | ||
| status: 'danger', | ||
| onClick: () => disconnectCodex(), | ||
| }, | ||
| secondaryButton: { children: 'Cancel' }, | ||
| }) | ||
| } | ||
|
|
||
| return ( | ||
| <div className="flex items-center gap-4 px-4 py-3"> | ||
| <div className="shrink-0"> | ||
| <div className="flex h-10 w-10 items-center justify-center rounded-md border bg-surface-secondary text-2xl"> | ||
| <IconTerminal /> | ||
| </div> | ||
| </div> | ||
| <div className="min-w-0 flex-1"> | ||
| <div className="flex flex-wrap items-center gap-2"> | ||
| <span className="font-semibold truncate">{integration.email || 'ChatGPT account'}</span> | ||
| {integration.plan_type ? ( | ||
| <LemonTag type="muted" className="capitalize"> | ||
| {`ChatGPT ${integration.plan_type}`} | ||
| </LemonTag> | ||
| ) : null} | ||
| </div> | ||
| <div className="mt-0.5 text-xs text-secondary"> | ||
| {integration.connected_at ? ( | ||
| <> | ||
| <span>Connected </span> | ||
| <TZLabel time={integration.connected_at} className="align-baseline" /> | ||
| </> | ||
| ) : ( | ||
| 'Connected' | ||
| )} | ||
| </div> | ||
| {integration.status === CodexIntegrationStatusEnumApi.ReauthRequired ? ( | ||
| <LemonBanner type="error" className="mt-2"> | ||
| Your ChatGPT sign-in stopped working. Connect Codex again to keep using your ChatGPT plan. | ||
| </LemonBanner> | ||
| ) : null} | ||
| </div> | ||
| <div className="flex shrink-0 items-center"> | ||
| <LemonButton | ||
| size="small" | ||
| type="secondary" | ||
| status="danger" | ||
| icon={<IconTrash />} | ||
| onClick={handleDisconnect} | ||
| disabledReason={codexIntegrationLoading ? 'Loading…' : undefined} | ||
| tooltip="Disconnect Codex" | ||
| data-attr="codex-disconnect" | ||
| /> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| export function PersonalCodexIntegration(): JSX.Element { | ||
| const { codexIntegration, codexIntegrationLoadFailed, connecting } = useValues(personalCodexIntegrationLogic) | ||
| const { openConnectModal } = useActions(personalCodexIntegrationLogic) | ||
|
|
||
| if (codexIntegration === null) { | ||
| return codexIntegrationLoadFailed ? ( | ||
| <LemonBanner type="error">Could not load your Codex connection. Refresh the page to try again.</LemonBanner> | ||
| ) : ( | ||
| <LemonSkeleton className="h-16 w-full" /> | ||
| ) | ||
| } | ||
|
|
||
| const notConnected = codexIntegration.status === CodexIntegrationStatusEnumApi.NotConnected | ||
| const reauthRequired = codexIntegration.status === CodexIntegrationStatusEnumApi.ReauthRequired | ||
|
|
||
| return ( | ||
| <> | ||
| <div className="divide-y rounded border bg-surface-primary"> | ||
| {notConnected ? ( | ||
| <div className="px-4 py-6 text-center text-sm text-secondary"> | ||
| <IconTerminal className="text-3xl mb-2 opacity-40" /> | ||
| <p className="mb-0">No Codex account connected</p> | ||
| </div> | ||
| ) : ( | ||
| <CodexAccountRow integration={codexIntegration} /> | ||
| )} | ||
| {notConnected || reauthRequired ? ( | ||
| <div className="flex flex-wrap items-center gap-x-3 gap-y-2 px-4 py-3"> | ||
| <LemonButton | ||
| type="secondary" | ||
| size="small" | ||
| icon={<IconPlus />} | ||
| onClick={() => openConnectModal(SETTINGS_CODEX_CONNECT_OPENER)} | ||
| disabledReason={connecting ? 'Connecting…' : undefined} | ||
| data-attr="codex-connect" | ||
| > | ||
| {reauthRequired ? 'Connect again' : 'Connect Codex'} | ||
| </LemonButton> | ||
| <span className="text-xs text-secondary text-balance">You need a paid ChatGPT plan.</span> | ||
| </div> | ||
| ) : null} | ||
| </div> | ||
| <CodexConnectModal opener={SETTINGS_CODEX_CONNECT_OPENER} /> | ||
| </> | ||
| ) | ||
| } |
Oops, something went wrong.
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.