Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions src/lib/workflows/posthog-integration/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import opn from 'opn';
import type { WorkflowConfig } from '../workflow-step.js';
import type { WorkflowRun } from '../../agent/agent-runner.js';
import type { WizardSession } from '../../wizard-session.js';
Expand All @@ -15,8 +16,24 @@ import { analytics } from '../../../utils/analytics.js';
import { WIZARD_INTERACTION_EVENT_NAME } from '../../constants.js';
import { getUI } from '../../../ui/index.js';
import { getCloudUrlFromRegion } from '../../../utils/urls.js';
import { requestDeepLink } from '../../../utils/provisioning.js';
import type { CloudRegion } from '../../../utils/types.js';
import { POSTHOG_INTEGRATION_WORKFLOW } from './steps.js';

const DASHBOARD_DEEP_LINK_KEY = 'dashboardDeepLink';

function resolveContinueUrl(
sess: WizardSession,
cloudRegion: CloudRegion | undefined,
deepLink: unknown,
): string | undefined {
if (!sess.signup) return undefined;
if (typeof deepLink === 'string' && deepLink) return deepLink;
if (cloudRegion)
return `${getCloudUrlFromRegion(cloudRegion)}/products?source=wizard`;
return undefined;
}

export const SETUP_REPORT_FILE = 'posthog-setup-report.md';
export const EVENT_PLAN_FILE = '.posthog-events.json';

Expand Down Expand Up @@ -165,17 +182,30 @@ Important: Use the detect_package_manager tool (from the wizard-tools MCP server
});
}
}

if (sess.signup) {
const deepLink = await requestDeepLink(
credentials.accessToken,
credentials.host,
);
if (deepLink) {
sess.frameworkContext[DASHBOARD_DEEP_LINK_KEY] = deepLink;
if (process.env.NODE_ENV !== 'test') {
opn(deepLink, { wait: false }).catch(() => {
// opn throws in environments without a browser
});
}
}
}
},

buildOutroData: (sess, credentials, cloudRegion) => {
const envVars = config.environment.getEnvVars(
credentials.projectApiKey,
credentials.host,
);
const continueUrl =
sess.signup && cloudRegion
? `${getCloudUrlFromRegion(cloudRegion)}/products?source=wizard`
: undefined;
const deepLink = sess.frameworkContext[DASHBOARD_DEEP_LINK_KEY];
const continueUrl = resolveContinueUrl(sess, cloudRegion, deepLink);

const changes = [
...config.ui.getOutroChanges(frameworkContext),
Expand Down
39 changes: 39 additions & 0 deletions src/utils/provisioning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,42 @@ export async function provisionNewAccount(
accountId: tokenData.account?.id ?? '',
};
}

/**
* Request a one-time deep link URL that logs the user into PostHog
* and redirects to their project dashboard.
*/
export async function requestDeepLink(
accessToken: string,
host: string,
): Promise<string | null> {
try {
const baseUrl = host
.replace('us.i.posthog.com', 'us.posthog.com')
.replace('eu.i.posthog.com', 'eu.posthog.com');

const res = await axios.post(
`${baseUrl}/api/agentic/provisioning/deep_links`,
{ purpose: 'dashboard' },
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
'API-Version': API_VERSION,
'User-Agent': WIZARD_USER_AGENT,
},
timeout: 10_000,
},
);

const url = res.data?.url;
if (typeof url === 'string') {
logToFile(`[provisioning] deep link created: ${url}`);
return url;
}
return null;
} catch {
logToFile('[provisioning] deep link request failed, skipping');
return null;
}
}
Loading