-
Notifications
You must be signed in to change notification settings - Fork 10
feat: react router agent #215
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
gewenyu99
wants to merge
4
commits into
main
Choose a base branch
from
react-router
base: main
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| /* React Router wizard using posthog-agent with PostHog MCP */ | ||
| import type { WizardOptions } from '../utils/types'; | ||
| import type { FrameworkConfig } from '../lib/framework-config'; | ||
| import { enableDebugLogs } from '../utils/debug'; | ||
| import { runAgentWizard } from '../lib/agent-runner'; | ||
| import { Integration } from '../lib/constants'; | ||
| import { getPackageVersion } from '../utils/package-json'; | ||
| import { getPackageDotJson } from '../utils/clack-utils'; | ||
| import clack from '../utils/clack'; | ||
| import chalk from 'chalk'; | ||
| import * as semver from 'semver'; | ||
| import { | ||
| getReactRouterMode, | ||
| getReactRouterModeName, | ||
| getReactRouterVersionBucket, | ||
| ReactRouterMode, | ||
| } from './utils'; | ||
|
|
||
| /** | ||
| * React Router framework configuration for the universal agent runner. | ||
| */ | ||
| const MINIMUM_REACT_ROUTER_VERSION = '6.0.0'; | ||
|
|
||
| const REACT_ROUTER_AGENT_CONFIG: FrameworkConfig = { | ||
| metadata: { | ||
| name: 'React Router', | ||
| integration: Integration.reactRouter, | ||
| docsUrl: 'https://posthog.com/docs/libraries/react', | ||
| unsupportedVersionDocsUrl: 'https://posthog.com/docs/libraries/react', | ||
| abortMessage: | ||
| 'This wizard uses an LLM agent to intelligently modify your project. Please view the docs to setup React Router manually instead: https://posthog.com/docs/libraries/react', | ||
| gatherContext: async (options: WizardOptions) => { | ||
| const routerMode = await getReactRouterMode(options); | ||
| return { routerMode }; | ||
| }, | ||
| }, | ||
|
|
||
| detection: { | ||
| packageName: 'react-router', | ||
| packageDisplayName: 'React Router', | ||
| getVersion: (packageJson: any) => | ||
| getPackageVersion('react-router', packageJson), | ||
| getVersionBucket: getReactRouterVersionBucket, | ||
| }, | ||
|
|
||
| environment: { | ||
| uploadToHosting: false, | ||
| getEnvVars: (apiKey: string, host: string) => ({ | ||
| REACT_APP_POSTHOG_KEY: apiKey, | ||
| REACT_APP_POSTHOG_HOST: host, | ||
| }), | ||
| }, | ||
|
|
||
| analytics: { | ||
| getTags: (context: any) => { | ||
| const routerMode = context.routerMode as ReactRouterMode; | ||
| return { | ||
| routerMode: routerMode || 'unknown', | ||
| }; | ||
| }, | ||
| }, | ||
|
|
||
| prompts: { | ||
| getAdditionalContextLines: (context: any) => { | ||
| const routerMode = context.routerMode as ReactRouterMode; | ||
| const modeName = routerMode | ||
| ? getReactRouterModeName(routerMode) | ||
| : 'unknown'; | ||
|
|
||
| // Map router mode to framework ID for MCP docs resource | ||
| const frameworkIdMap: Record<ReactRouterMode, string> = { | ||
| [ReactRouterMode.V6]: 'react-react-router-6', | ||
| [ReactRouterMode.V7_FRAMEWORK]: 'react-react-router-7-framework', | ||
| [ReactRouterMode.V7_DATA]: 'react-react-router-7-data', | ||
| [ReactRouterMode.V7_DECLARATIVE]: 'react-react-router-7-declarative', | ||
| }; | ||
|
|
||
| const frameworkId = routerMode | ||
| ? frameworkIdMap[routerMode] | ||
| : ReactRouterMode.V7_FRAMEWORK; | ||
|
|
||
| return [ | ||
| `Router mode: ${modeName}`, | ||
| `Framework docs ID: ${frameworkId} (use posthog://docs/frameworks/${frameworkId} for documentation)`, | ||
| ]; | ||
| }, | ||
| }, | ||
|
|
||
| ui: { | ||
| welcomeMessage: 'PostHog React Router wizard (agent-powered)', | ||
| spinnerMessage: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably can share these between frameworks too
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True |
||
| 'Writing your PostHog setup with events, error capture and more...', | ||
| successMessage: 'PostHog integration complete', | ||
| estimatedDurationMinutes: 8, | ||
| getOutroChanges: (context: any) => { | ||
| const routerMode = context.routerMode as ReactRouterMode; | ||
| const modeName = routerMode | ||
| ? getReactRouterModeName(routerMode) | ||
| : 'React Router'; | ||
| return [ | ||
| `Analyzed your React Router project structure (${modeName})`, | ||
| `Created and configured PostHog initializers`, | ||
| `Integrated PostHog into your application`, | ||
| ]; | ||
| }, | ||
| getOutroNextSteps: () => [ | ||
| 'Start your development server to see PostHog in action', | ||
| 'Visit your PostHog dashboard to see incoming events', | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| /** | ||
| * React Router wizard powered by the universal agent runner. | ||
| */ | ||
| export async function runReactRouterWizardAgent( | ||
| options: WizardOptions, | ||
| ): Promise<void> { | ||
| if (options.debug) { | ||
| enableDebugLogs(); | ||
| } | ||
|
|
||
| // Check React Router version - agent wizard requires >= 6.0.0 | ||
| const packageJson = await getPackageDotJson(options); | ||
| const reactRouterVersion = getPackageVersion('react-router', packageJson); | ||
|
|
||
| if (reactRouterVersion) { | ||
| const coercedVersion = semver.coerce(reactRouterVersion); | ||
| if ( | ||
| coercedVersion && | ||
| semver.lt(coercedVersion, MINIMUM_REACT_ROUTER_VERSION) | ||
| ) { | ||
| const docsUrl = | ||
| REACT_ROUTER_AGENT_CONFIG.metadata.unsupportedVersionDocsUrl ?? | ||
| REACT_ROUTER_AGENT_CONFIG.metadata.docsUrl; | ||
|
|
||
| clack.log.warn( | ||
| `Sorry: the wizard can't help you with React Router ${reactRouterVersion}. Upgrade to React Router ${MINIMUM_REACT_ROUTER_VERSION} or later, or check out the manual setup guide.`, | ||
| ); | ||
| clack.log.info(`Setup React Router manually: ${chalk.cyan(docsUrl)}`); | ||
| clack.outro('PostHog wizard will see you next time!'); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| await runAgentWizard(REACT_ROUTER_AGENT_CONFIG, options); | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice, we can start to see the parts that are generalizable. We can probably live without a per-framework abort message. It's probably the same message each time, with the docs URL piped in for that framework