-
Notifications
You must be signed in to change notification settings - Fork 15
feat: js-web skill #272
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
sarahxsanders
wants to merge
5
commits into
main
Choose a base branch
from
js-skll
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.
+268
−0
Open
feat: js-web skill #272
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| /* Generic JavaScript Web (client-side) wizard using posthog-agent with PostHog MCP */ | ||
| import type { WizardOptions } from '../utils/types'; | ||
| import type { FrameworkConfig } from '../lib/framework-config'; | ||
| import { Integration } from '../lib/constants'; | ||
| import * as fs from 'node:fs'; | ||
| import * as path from 'node:path'; | ||
| import { hasPackageInstalled } from '../utils/package-json'; | ||
| import { tryGetPackageJson } from '../utils/clack-utils'; | ||
| import { | ||
| FRAMEWORK_PACKAGES, | ||
| detectJsPackageManager, | ||
| detectBundler, | ||
| type JavaScriptContext, | ||
| } from './utils'; | ||
|
|
||
| export const JAVASCRIPT_WEB_AGENT_CONFIG: FrameworkConfig<JavaScriptContext> = { | ||
| metadata: { | ||
| name: 'JavaScript (Web)', | ||
| integration: Integration.javascript_web, | ||
| beta: true, | ||
| docsUrl: 'https://posthog.com/docs/libraries/js', | ||
| gatherContext: (options: WizardOptions) => { | ||
| const packageManagerName = detectJsPackageManager(options); | ||
| const hasTypeScript = fs.existsSync( | ||
| path.join(options.installDir, 'tsconfig.json'), | ||
| ); | ||
| const hasBundler = detectBundler(options); | ||
| return Promise.resolve({ packageManagerName, hasTypeScript, hasBundler }); | ||
| }, | ||
| }, | ||
|
|
||
| detection: { | ||
| packageName: 'posthog-js', | ||
| packageDisplayName: 'JavaScript (Web)', | ||
| usesPackageJson: false, | ||
| getVersion: () => undefined, | ||
| detect: async (options) => { | ||
| const packageJson = await tryGetPackageJson(options); | ||
| if (!packageJson) { | ||
| return false; | ||
| } | ||
|
|
||
| // Exclude projects with known framework packages | ||
| for (const frameworkPkg of FRAMEWORK_PACKAGES) { | ||
| if (hasPackageInstalled(frameworkPkg, packageJson)) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| // Ensure this is actually a JS project, not just a package.json for tooling | ||
| const { installDir } = options; | ||
|
|
||
| // Check for a lockfile | ||
| const hasLockfile = [ | ||
| 'package-lock.json', | ||
| 'yarn.lock', | ||
| 'pnpm-lock.yaml', | ||
| 'bun.lockb', | ||
| 'bun.lock', | ||
| ].some((lockfile) => fs.existsSync(path.join(installDir, lockfile))); | ||
|
|
||
| if (hasLockfile) { | ||
| return true; | ||
| } | ||
|
|
||
| // Fallback: check if package.json has actual dependencies | ||
| const hasDeps = | ||
| (packageJson.dependencies && | ||
| Object.keys(packageJson.dependencies).length > 0) || | ||
| (packageJson.devDependencies && | ||
| Object.keys(packageJson.devDependencies).length > 0); | ||
|
|
||
| return !!hasDeps; | ||
| }, | ||
| }, | ||
|
|
||
| environment: { | ||
| uploadToHosting: false, | ||
| getEnvVars: (apiKey: string, host: string) => ({ | ||
| POSTHOG_API_KEY: apiKey, | ||
| POSTHOG_HOST: host, | ||
| }), | ||
| }, | ||
|
|
||
| analytics: { | ||
| getTags: (context) => { | ||
| const tags: Record<string, string> = { | ||
| packageManager: context.packageManagerName ?? 'unknown', | ||
| }; | ||
| if (context.hasBundler) { | ||
| tags.bundler = context.hasBundler; | ||
| } | ||
| return tags; | ||
| }, | ||
| }, | ||
|
|
||
| prompts: { | ||
| projectTypeDetection: | ||
| 'This is a JavaScript/TypeScript project. Look for package.json and lockfiles (package-lock.json, yarn.lock, pnpm-lock.yaml, bun.lockb) to confirm.', | ||
| packageInstallation: | ||
| 'Look for lockfiles to determine the package manager (npm, yarn, pnpm, bun). Do not manually edit package.json.', | ||
| getAdditionalContextLines: (context) => { | ||
| const lines = [ | ||
| `Package manager: ${context.packageManagerName ?? 'unknown'}`, | ||
| `Has TypeScript: ${context.hasTypeScript ? 'yes' : 'no'}`, | ||
| `Framework docs ID: js (use posthog://docs/frameworks/js for documentation if available)`, | ||
| `Project type: Generic JavaScript/TypeScript application (no specific framework detected)`, | ||
| ``, | ||
| `## CRITICAL: posthog-js Best Practices`, | ||
| ``, | ||
| `### 1. Use posthog-js (Browser SDK)`, | ||
| `This is a client-side web JavaScript project. Use the posthog-js package, NOT posthog-node.`, | ||
| `posthog-js is designed for browser use and includes autocapture, session recording, and feature flags.`, | ||
| ``, | ||
| `### 2. Initialization (REQUIRED)`, | ||
| `posthog.init() MUST be called before any other PostHog methods:`, | ||
| ``, | ||
| `import posthog from 'posthog-js'`, | ||
| ``, | ||
| `posthog.init('<api_key>', {`, | ||
| ` api_host: '<host>',`, | ||
| `})`, | ||
| ``, | ||
| `### 3. Autocapture`, | ||
| `Autocapture is ON by default with posthog-js. It tracks clicks, form submissions, and pageviews automatically.`, | ||
| `Do NOT disable autocapture unless the user explicitly requests it.`, | ||
| ``, | ||
| `### 4. Error Tracking`, | ||
| `Use posthog.captureException(error) for error tracking.`, | ||
| ``, | ||
| `### 5. NEVER Send PII in Event Properties`, | ||
| `DO NOT include in posthog.capture() event properties:`, | ||
| `- Email addresses, full names, phone numbers, physical addresses, IP addresses`, | ||
| `- Any user-generated content (messages, comments, form submissions)`, | ||
| ``, | ||
| `SAFE event properties:`, | ||
| `posthog.capture('form_submitted', { form_type: 'contact', field_count: 5 })`, | ||
| ``, | ||
| `UNSAFE (DO NOT DO THIS):`, | ||
| `posthog.capture('form_submitted', { email: userEmail, message: content })`, | ||
| ``, | ||
| `### 6. User Identification (identify with person properties)`, | ||
| `Call posthog.identify() on login AND on page refresh if the user is already logged in.`, | ||
| `ALWAYS pass person properties as the second argument — this is where email/name/role BELONG:`, | ||
| ``, | ||
| `posthog.identify(user.id, {`, | ||
| ` email: user.email,`, | ||
| ` name: user.name,`, | ||
| ` role: user.role`, | ||
| `})`, | ||
| ``, | ||
| `Person properties via identify() are NOT the same as event properties via capture().`, | ||
| `PII in identify() person properties is expected and recommended by PostHog docs.`, | ||
| `Call posthog.reset() on logout to unlink future events from the current user.`, | ||
| ``, | ||
| `### 7. SPA Pageview Tracking`, | ||
| `For single-page applications without a framework router, you may need to manually capture pageviews:`, | ||
| `posthog.capture('$pageview')`, | ||
| `Or use capture_pageview: 'history_change' in the init options for History API based routing.`, | ||
| ``, | ||
| `IMPORTANT: These best practices are MANDATORY. The implementation will fail review if they are not followed.`, | ||
| ]; | ||
|
|
||
| if (context.hasBundler) { | ||
| lines.unshift(`Bundler: ${context.hasBundler}`); | ||
| } | ||
|
|
||
| return lines; | ||
| }, | ||
| }, | ||
|
|
||
| ui: { | ||
| successMessage: 'PostHog integration complete', | ||
| estimatedDurationMinutes: 5, | ||
| getOutroChanges: (context) => { | ||
| const packageManagerName = | ||
| context.packageManagerName ?? 'package manager'; | ||
| return [ | ||
| `Analyzed your JavaScript project structure`, | ||
| `Installed the posthog-js package using ${packageManagerName}`, | ||
| `Created PostHog initialization code`, | ||
| `Configured autocapture, error tracking, and event capture`, | ||
| ]; | ||
| }, | ||
| getOutroNextSteps: () => [ | ||
| 'Ensure posthog.init() is called before any capture calls', | ||
| 'Autocapture tracks clicks, form submissions, and pageviews automatically', | ||
| 'Use posthog.capture() for custom events and posthog.identify() for users', | ||
| 'NEVER send PII in event properties (no emails, names, or user content)', | ||
| 'Visit your PostHog dashboard to see incoming events', | ||
| ], | ||
| }, | ||
| }; | ||
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,72 @@ | ||
| import * as fs from 'node:fs'; | ||
| import * as path from 'node:path'; | ||
| import { detectAllPackageManagers } from '../utils/package-manager'; | ||
| import type { WizardOptions } from '../utils/types'; | ||
|
|
||
| export type JavaScriptContext = { | ||
| packageManagerName?: string; | ||
| hasTypeScript?: boolean; | ||
| hasBundler?: string; | ||
| }; | ||
|
|
||
| /** | ||
| * Packages that indicate a specific framework integration exists. | ||
| * If any of these are in package.json, we should NOT match as generic JavaScript. | ||
| * | ||
| * When adding a new JS framework integration to the wizard, | ||
| * add its detection package here too. | ||
| */ | ||
| export const FRAMEWORK_PACKAGES = [ | ||
| 'next', | ||
| 'nuxt', | ||
| 'vue', | ||
| 'react-router', | ||
| '@tanstack/react-start', | ||
| '@tanstack/react-router', | ||
| 'react-native', | ||
| '@angular/core', | ||
| 'astro', | ||
| '@sveltejs/kit', | ||
| ] as const; | ||
|
|
||
| /** | ||
| * Detect the JS package manager for the project by checking lockfiles. | ||
| * Reuses the existing package manager detection infrastructure. | ||
| */ | ||
| export function detectJsPackageManager( | ||
| options: Pick<WizardOptions, 'installDir'>, | ||
| ): string { | ||
| const detected = detectAllPackageManagers(options); | ||
| if (detected.length > 0) { | ||
| return detected[0].label; | ||
| } | ||
| return 'unknown'; | ||
| } | ||
|
|
||
| /** | ||
| * Detect the bundler used in the project by checking package.json dependencies. | ||
| */ | ||
| export function detectBundler( | ||
| options: Pick<WizardOptions, 'installDir'>, | ||
| ): string | undefined { | ||
| try { | ||
| const content = fs.readFileSync( | ||
| path.join(options.installDir, 'package.json'), | ||
| 'utf-8', | ||
| ); | ||
| const pkg = JSON.parse(content); | ||
| const allDeps: Record<string, string> = { | ||
| ...pkg.dependencies, | ||
| ...pkg.devDependencies, | ||
| }; | ||
|
|
||
| if (allDeps['vite']) return 'vite'; | ||
| if (allDeps['webpack']) return 'webpack'; | ||
| if (allDeps['esbuild']) return 'esbuild'; | ||
| if (allDeps['parcel']) return 'parcel'; | ||
| if (allDeps['rollup']) return 'rollup'; | ||
| return undefined; | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } |
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
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.
This extra context is SO MEATY. Should it live in the context-mill/skill itself? Maybe we enhance the skill definition system to accept additional local files as framework guidance?
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.
im gonna test this approach bc it feels like the right call!! just make sure it doesn't break anything... and fix Python bc I did something similar there :]