-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathelixir-wizard-agent.ts
More file actions
105 lines (96 loc) · 3.77 KB
/
Copy pathelixir-wizard-agent.ts
File metadata and controls
105 lines (96 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* Elixir wizard using posthog-agent with PostHog MCP */
import * as fs from 'node:fs';
import * as path from 'node:path';
import type { WizardRunOptions } from '@utils/types';
import type { FrameworkConfig } from '@lib/framework-config';
import { mixPackageManager } from '@lib/detection/package-manager';
import { Integration } from '@lib/constants';
type ElixirContext = {
phoenix?: boolean;
};
function readMixExs(installDir: string): string | undefined {
const mixExsPath = path.join(installDir, 'mix.exs');
if (!fs.existsSync(mixExsPath)) {
return undefined;
}
return fs.readFileSync(mixExsPath, 'utf-8');
}
/** Phoenix apps get the Plug integration; plain Elixir apps don't. */
function isPhoenixProject(installDir: string): boolean {
const mixExs = readMixExs(installDir);
return !!mixExs && /\{\s*:phoenix\s*,/.test(mixExs);
}
export const ELIXIR_AGENT_CONFIG: FrameworkConfig<ElixirContext> = {
metadata: {
name: 'Elixir',
integration: Integration.elixir,
docsUrl: 'https://posthog.com/docs/libraries/elixir',
gatherContext: (options: WizardRunOptions) => {
const phoenix = isPhoenixProject(options.installDir);
return Promise.resolve({ phoenix });
},
},
detection: {
packageName: 'posthog',
packageDisplayName: 'Elixir',
usesPackageJson: false,
getVersion: () => undefined,
// A mix.exs defining a project marks a Mix project root; the def project
// check keeps stray files named mix.exs from claiming the directory.
detect: (options) => {
const mixExs = readMixExs(options.installDir);
return Promise.resolve(!!mixExs && /def\s+project\b/.test(mixExs));
},
detectPackageManager: mixPackageManager,
},
environment: {
uploadToHosting: false,
getEnvVars: (apiKey: string, host: string) => ({
POSTHOG_API_KEY: apiKey,
POSTHOG_HOST: host,
}),
},
analytics: {
getTags: (context) => ({
projectType: context.phoenix ? 'phoenix' : 'elixir',
}),
},
prompts: {
projectTypeDetection:
'This is an Elixir project. Look for mix.exs, mix.lock, config/, lib/, and application.ex to confirm; Phoenix apps also have endpoint and router modules.',
packageInstallation:
'Mix has no single add command. Add `{:posthog, "~> 2.0"}` to the deps list in mix.exs, then run `mix deps.get` to fetch it.',
getAdditionalContextLines: (context) => {
const lines = [
`Framework docs ID: elixir (use posthog://docs/frameworks/elixir for documentation)`,
'Configure PostHog in application config (api_key, api_host, in_app_otp_apps) reading from environment variables; set test_mode: true in the test environment.',
];
if (context.phoenix) {
lines.push(
'This is a Phoenix app — add PostHog.Integrations.Plug before the router so request context is attached to captured events.',
);
}
return lines;
},
},
ui: {
successMessage: 'PostHog integration complete',
estimatedDurationMinutes: 5,
getOutroChanges: (context) => [
`Analyzed your ${
context.phoenix ? 'Phoenix' : 'Elixir'
} project structure`,
'Added the posthog Hex package to mix.exs and fetched it with mix deps.get',
'Configured PostHog in application config from environment variables',
'Instrumented meaningful events with PostHog.capture',
],
getOutroNextSteps: (context) => [
context.phoenix
? 'Start your Phoenix server with `mix phx.server` and trigger the instrumented code paths'
: 'Run your Elixir application and trigger the instrumented code paths',
'Visit your PostHog dashboard to see incoming events',
'Use PostHog.capture/2 to track custom events',
'Use PostHog.set_context/1 to set a distinct_id once per request or job process',
],
},
};