-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathpython-wizard-agent.ts
More file actions
203 lines (187 loc) · 6.19 KB
/
python-wizard-agent.ts
File metadata and controls
203 lines (187 loc) · 6.19 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* Generic Python language wizard using posthog-agent with PostHog MCP */
import type { WizardOptions } from '../../utils/types';
import type { FrameworkConfig } from '../../lib/framework-config';
import { PYTHON_PACKAGE_INSTALLATION } from '../../lib/framework-config';
import { detectPythonPackageManagers } from '../../lib/package-manager-detection';
import { Integration } from '../../lib/constants';
import fg from 'fast-glob';
import * as fs from 'node:fs';
import * as path from 'node:path';
import {
getPythonVersion,
getPythonVersionBucket,
detectPackageManager,
getPackageManagerName,
PythonPackageManager,
} from './utils';
type PythonContext = {
packageManager?: PythonPackageManager;
};
export const PYTHON_AGENT_CONFIG: FrameworkConfig<PythonContext> = {
metadata: {
name: 'Python Language',
integration: Integration.python,
beta: true,
docsUrl: 'https://posthog.com/docs/libraries/python',
gatherContext: async (options: WizardOptions) => {
const packageManager = await detectPackageManager(options);
return { packageManager };
},
},
detection: {
packageName: 'python',
packageDisplayName: 'Python',
usesPackageJson: false,
getVersion: () => undefined,
getVersionBucket: getPythonVersionBucket,
minimumVersion: '3.8.0',
getInstalledVersion: (options: WizardOptions) =>
Promise.resolve(getPythonVersion(options)),
detect: async (options) => {
const { installDir } = options;
// Look for Python package management files
const pythonConfigFiles = await fg(
[
'**/requirements*.txt',
'**/pyproject.toml',
'**/setup.py',
'**/Pipfile',
],
{
cwd: installDir,
ignore: ['**/venv/**', '**/.venv/**', '**/env/**', '**/.env/**'],
},
);
if (pythonConfigFiles.length === 0) {
return false;
}
// Make sure this isn't Django or Flask (those should be detected first)
// Check for Django
const managePyMatches = await fg('**/manage.py', {
cwd: installDir,
ignore: ['**/venv/**', '**/.venv/**', '**/env/**', '**/.env/**'],
});
for (const match of managePyMatches) {
try {
const content = fs.readFileSync(
path.join(installDir, match),
'utf-8',
);
if (
content.includes('django') ||
content.includes('DJANGO_SETTINGS_MODULE')
) {
return false; // Django detected, use django agent instead
}
} catch {
continue;
}
}
// Check for Flask
for (const configFile of pythonConfigFiles) {
try {
const content = fs.readFileSync(
path.join(installDir, configFile),
'utf-8',
);
if (
/^flask([<>=~!]|$|\s)/im.test(content) ||
/["']flask["']/i.test(content)
) {
return false; // Flask detected, use flask agent instead
}
} catch {
continue;
}
}
const pyFiles = await fg(
['**/app.py', '**/wsgi.py', '**/application.py', '**/__init__.py'],
{
cwd: installDir,
ignore: [
'**/venv/**',
'**/.venv/**',
'**/env/**',
'**/.env/**',
'**/__pycache__/**',
],
},
);
for (const pyFile of pyFiles) {
try {
const content = fs.readFileSync(
path.join(installDir, pyFile),
'utf-8',
);
if (
content.includes('from flask import') ||
content.includes('import flask') ||
/Flask\s*\(/.test(content)
) {
return false; // Flask detected, use flask agent instead
}
} catch {
continue;
}
}
// If we have Python config files but it's not Django or Flask, it's a generic Python project
return true;
},
detectPackageManager: detectPythonPackageManagers,
},
environment: {
uploadToHosting: false,
getEnvVars: (apiKey: string, host: string) => ({
POSTHOG_PROJECT_TOKEN: apiKey,
POSTHOG_HOST: host,
}),
},
analytics: {
getTags: (context) => {
const packageManagerName = context.packageManager
? getPackageManagerName(context.packageManager)
: 'unknown';
return {
packageManager: packageManagerName,
};
},
},
prompts: {
packageInstallation: PYTHON_PACKAGE_INSTALLATION,
projectTypeDetection:
'This is a generic Python project. Look for requirements.txt, pyproject.toml, setup.py, or Pipfile to confirm.',
getAdditionalContextLines: (context) => {
const packageManagerName = context.packageManager
? getPackageManagerName(context.packageManager)
: 'unknown';
return [
`Package manager: ${packageManagerName}`,
`Framework docs ID: python (use posthog://docs/frameworks/python for documentation)`,
`Project type: Generic Python application (CLI, script, worker, data pipeline, etc.)`,
];
},
},
ui: {
successMessage: 'PostHog integration complete',
estimatedDurationMinutes: 5,
getOutroChanges: (context) => {
const packageManagerName = context.packageManager
? getPackageManagerName(context.packageManager)
: 'package manager';
return [
`Analyzed your Python project structure`,
`Installed the PostHog Python package using ${packageManagerName}`,
`Created PostHog initialization using instance-based API (Posthog class)`,
`Configured exception autocapture and graceful shutdown`,
`Added example code for events, feature flags, and error capture (without PII)`,
];
},
getOutroNextSteps: () => [
'Use Posthog() class (not module-level posthog) with enable_exception_autocapture=True',
'Call posthog_client.shutdown() on application exit (use atexit.register)',
'NEVER send PII in event properties (no emails, names, or user content)',
'Use posthog_client.capture() for events and posthog_client.identify() for users',
'Visit your PostHog dashboard to see incoming events',
],
},
};