@posthog/agent 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/CLAUDE.md +296 -0
  2. package/README.md +142 -0
  3. package/dist/example.d.ts +3 -0
  4. package/dist/example.d.ts.map +1 -0
  5. package/dist/example.js +49 -0
  6. package/dist/example.js.map +1 -0
  7. package/dist/index.d.ts +7 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +5 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/src/agent-registry.d.ts +16 -0
  12. package/dist/src/agent-registry.d.ts.map +1 -0
  13. package/dist/src/agent-registry.js +54 -0
  14. package/dist/src/agent-registry.js.map +1 -0
  15. package/dist/src/agent.d.ts +60 -0
  16. package/dist/src/agent.d.ts.map +1 -0
  17. package/dist/src/agent.js +371 -0
  18. package/dist/src/agent.js.map +1 -0
  19. package/dist/src/agents/execution.d.ts +2 -0
  20. package/dist/src/agents/execution.d.ts.map +1 -0
  21. package/dist/src/agents/execution.js +54 -0
  22. package/dist/src/agents/execution.js.map +1 -0
  23. package/dist/src/agents/planning.d.ts +2 -0
  24. package/dist/src/agents/planning.d.ts.map +1 -0
  25. package/dist/src/agents/planning.js +68 -0
  26. package/dist/src/agents/planning.js.map +1 -0
  27. package/dist/src/event-transformer.d.ts +7 -0
  28. package/dist/src/event-transformer.d.ts.map +1 -0
  29. package/dist/src/event-transformer.js +174 -0
  30. package/dist/src/event-transformer.js.map +1 -0
  31. package/dist/src/file-manager.d.ts +30 -0
  32. package/dist/src/file-manager.d.ts.map +1 -0
  33. package/dist/src/file-manager.js +181 -0
  34. package/dist/src/file-manager.js.map +1 -0
  35. package/dist/src/git-manager.d.ts +49 -0
  36. package/dist/src/git-manager.d.ts.map +1 -0
  37. package/dist/src/git-manager.js +278 -0
  38. package/dist/src/git-manager.js.map +1 -0
  39. package/dist/src/posthog-api.d.ts +48 -0
  40. package/dist/src/posthog-api.d.ts.map +1 -0
  41. package/dist/src/posthog-api.js +110 -0
  42. package/dist/src/posthog-api.js.map +1 -0
  43. package/dist/src/prompt-builder.d.ts +17 -0
  44. package/dist/src/prompt-builder.d.ts.map +1 -0
  45. package/dist/src/prompt-builder.js +75 -0
  46. package/dist/src/prompt-builder.js.map +1 -0
  47. package/dist/src/stage-executor.d.ts +16 -0
  48. package/dist/src/stage-executor.d.ts.map +1 -0
  49. package/dist/src/stage-executor.js +119 -0
  50. package/dist/src/stage-executor.js.map +1 -0
  51. package/dist/src/task-manager.d.ts +25 -0
  52. package/dist/src/task-manager.d.ts.map +1 -0
  53. package/dist/src/task-manager.js +119 -0
  54. package/dist/src/task-manager.js.map +1 -0
  55. package/dist/src/template-manager.d.ts +30 -0
  56. package/dist/src/template-manager.d.ts.map +1 -0
  57. package/dist/src/template-manager.js +118 -0
  58. package/dist/src/template-manager.js.map +1 -0
  59. package/dist/src/types.d.ts +163 -0
  60. package/dist/src/types.d.ts.map +1 -0
  61. package/dist/src/types.js +9 -0
  62. package/dist/src/types.js.map +1 -0
  63. package/dist/src/utils/logger.d.ts +29 -0
  64. package/dist/src/utils/logger.d.ts.map +1 -0
  65. package/dist/src/utils/logger.js +66 -0
  66. package/dist/src/utils/logger.js.map +1 -0
  67. package/dist/src/utils/mcp.d.ts +10 -0
  68. package/dist/src/utils/mcp.d.ts.map +1 -0
  69. package/dist/src/utils/mcp.js +16 -0
  70. package/dist/src/utils/mcp.js.map +1 -0
  71. package/dist/src/workflow-registry.d.ts +11 -0
  72. package/dist/src/workflow-registry.d.ts.map +1 -0
  73. package/dist/src/workflow-registry.js +26 -0
  74. package/dist/src/workflow-registry.js.map +1 -0
  75. package/dist/src/workflow-types.d.ts +45 -0
  76. package/dist/src/workflow-types.d.ts.map +1 -0
  77. package/dist/src/workflow-types.js +2 -0
  78. package/dist/src/workflow-types.js.map +1 -0
  79. package/package.json +61 -0
  80. package/src/agent-registry.ts +60 -0
  81. package/src/agent.ts +428 -0
  82. package/src/agents/execution.ts +53 -0
  83. package/src/agents/planning.ts +67 -0
  84. package/src/event-transformer.ts +189 -0
  85. package/src/file-manager.ts +204 -0
  86. package/src/git-manager.ts +344 -0
  87. package/src/posthog-api.ts +169 -0
  88. package/src/prompt-builder.ts +93 -0
  89. package/src/stage-executor.ts +137 -0
  90. package/src/task-manager.ts +155 -0
  91. package/src/template-manager.ts +149 -0
  92. package/src/templates/plan-template.md +45 -0
  93. package/src/types.ts +223 -0
  94. package/src/utils/logger.ts +79 -0
  95. package/src/utils/mcp.ts +15 -0
  96. package/src/workflow-registry.ts +31 -0
  97. package/src/workflow-types.ts +53 -0
package/src/agent.ts ADDED
@@ -0,0 +1,428 @@
1
+ import { query } from "@anthropic-ai/claude-agent-sdk";
2
+ import type { Task, ExecutionResult, PlanResult, AgentConfig } from './types';
3
+ import type { WorkflowDefinition, WorkflowStage, WorkflowExecutionOptions } from './workflow-types';
4
+ import { TaskManager } from './task-manager';
5
+ import { PostHogAPIClient } from './posthog-api';
6
+ import { PostHogFileManager } from './file-manager';
7
+ import { GitManager } from './git-manager';
8
+ import { TemplateManager } from './template-manager';
9
+ import { EventTransformer } from './event-transformer';
10
+ import { PLANNING_SYSTEM_PROMPT } from './agents/planning';
11
+ import { EXECUTION_SYSTEM_PROMPT } from './agents/execution';
12
+ import { Logger } from './utils/logger';
13
+ import { AgentRegistry } from './agent-registry';
14
+ import { WorkflowRegistry } from './workflow-registry';
15
+ import { StageExecutor } from './stage-executor';
16
+ import { PromptBuilder } from './prompt-builder';
17
+
18
+ export class Agent {
19
+ private workingDirectory: string;
20
+ private onEvent?: (event: any) => void;
21
+ private taskManager: TaskManager;
22
+ private posthogAPI?: PostHogAPIClient;
23
+ private fileManager: PostHogFileManager;
24
+ private gitManager: GitManager;
25
+ private templateManager: TemplateManager;
26
+ private eventTransformer: EventTransformer;
27
+ private logger: Logger;
28
+ private agentRegistry: AgentRegistry;
29
+ private workflowRegistry: WorkflowRegistry;
30
+ private stageExecutor: StageExecutor;
31
+ public debug: boolean;
32
+
33
+ constructor(config: AgentConfig = {}) {
34
+ this.workingDirectory = config.workingDirectory || process.cwd();
35
+ this.onEvent = config.onEvent;
36
+ this.debug = config.debug || false;
37
+ this.logger = new Logger({ debug: this.debug, prefix: '[PostHog Agent]' });
38
+ this.taskManager = new TaskManager();
39
+ this.eventTransformer = new EventTransformer();
40
+
41
+ this.fileManager = new PostHogFileManager(
42
+ this.workingDirectory,
43
+ this.logger.child('FileManager')
44
+ );
45
+ this.gitManager = new GitManager({
46
+ repositoryPath: this.workingDirectory,
47
+ logger: this.logger.child('GitManager')
48
+ // TODO: Add author config from environment or config
49
+ });
50
+ this.templateManager = new TemplateManager();
51
+ this.agentRegistry = new AgentRegistry();
52
+
53
+ if (config.posthogApiUrl && config.posthogApiKey) {
54
+ this.posthogAPI = new PostHogAPIClient({
55
+ apiUrl: config.posthogApiUrl,
56
+ apiKey: config.posthogApiKey,
57
+ });
58
+ }
59
+
60
+ this.workflowRegistry = new WorkflowRegistry(this.posthogAPI);
61
+ const promptBuilder = new PromptBuilder({
62
+ getTaskFiles: (taskId: string) => this.getTaskFiles(taskId),
63
+ generatePlanTemplate: (vars) => this.templateManager.generatePlan(vars),
64
+ logger: this.logger.child('PromptBuilder')
65
+ });
66
+ this.stageExecutor = new StageExecutor(this.agentRegistry, this.logger, promptBuilder);
67
+ }
68
+
69
+ /**
70
+ * Enable or disable debug logging
71
+ */
72
+ setDebug(enabled: boolean) {
73
+ this.debug = enabled;
74
+ this.logger.setDebug(enabled);
75
+ }
76
+
77
+ // Workflow-based execution
78
+ async runWorkflow(taskOrId: Task | string, workflowId: string, options: WorkflowExecutionOptions = {}): Promise<{ task: Task; workflow: WorkflowDefinition }> {
79
+ const task = typeof taskOrId === 'string' ? await this.fetchTask(taskOrId) : taskOrId;
80
+ await this.workflowRegistry.loadWorkflows();
81
+ const workflow = this.workflowRegistry.getWorkflow(workflowId);
82
+ if (!workflow) {
83
+ throw new Error(`Workflow ${workflowId} not found`);
84
+ }
85
+
86
+ // Ensure task is assigned to workflow and positioned at first stage
87
+ if (this.posthogAPI) {
88
+ try {
89
+ if ((task.workflow as any) !== workflowId) {
90
+ await this.posthogAPI.updateTask(task.id, { workflow: workflowId } as any);
91
+ (task as any).workflow = workflowId;
92
+ }
93
+ if (!(task as any).current_stage && workflow.stages.length > 0) {
94
+ const firstStage = [...workflow.stages].sort((a, b) => a.position - b.position)[0];
95
+ await this.posthogAPI.updateTaskStage(task.id, firstStage.id);
96
+ (task as any).current_stage = firstStage.id;
97
+ }
98
+ } catch (e) {
99
+ this.logger.warn('Failed to sync task workflow/stage before execution', { error: (e as Error).message });
100
+ }
101
+ }
102
+
103
+ const executionId = this.taskManager.generateExecutionId();
104
+ this.logger.info('Starting workflow execution', { taskId: task.id, workflowId, executionId });
105
+ this.taskManager.startExecution(task.id, 'plan_and_build', executionId);
106
+
107
+ try {
108
+ const orderedStages = [...workflow.stages].sort((a, b) => a.position - b.position);
109
+ let startIndex = 0;
110
+ const currentStageId = (task as any).current_stage as string | undefined;
111
+
112
+ // If task is already at the last stage, fail gracefully without progressing
113
+ if (currentStageId) {
114
+ const currIdx = orderedStages.findIndex(s => s.id === currentStageId);
115
+ const atLastStage = currIdx >= 0 && currIdx === orderedStages.length - 1;
116
+ if (atLastStage) {
117
+ this.emitEvent(this.eventTransformer.createStatusEvent('no_next_stage', { stage: orderedStages[currIdx].key }));
118
+ this.taskManager.completeExecution(executionId, { task, workflow });
119
+ return { task, workflow };
120
+ }
121
+ }
122
+
123
+ if (options.resumeFromCurrentStage && currentStageId) {
124
+ const idx = orderedStages.findIndex(s => s.id === currentStageId);
125
+ if (idx >= 0) startIndex = idx;
126
+ }
127
+
128
+ // Align server-side stage when restarting from the beginning
129
+ if (this.posthogAPI) {
130
+ const targetStage = orderedStages[startIndex];
131
+ if (targetStage && targetStage.id !== currentStageId) {
132
+ try { await this.posthogAPI.updateTaskStage(task.id, targetStage.id); (task as any).current_stage = targetStage.id; } catch {}
133
+ }
134
+ }
135
+
136
+ for (let i = startIndex; i < orderedStages.length; i++) {
137
+ const stage = orderedStages[i];
138
+ await this.executeStage(task, stage, options);
139
+ if (options.autoProgress) {
140
+ const hasNext = i < orderedStages.length - 1;
141
+ if (hasNext) {
142
+ await this.progressToNextStage(task.id);
143
+ }
144
+ }
145
+ }
146
+ this.taskManager.completeExecution(executionId, { task, workflow });
147
+ return { task, workflow };
148
+ } catch (error) {
149
+ this.taskManager.failExecution(executionId, error as Error);
150
+ throw error;
151
+ }
152
+ }
153
+
154
+ async executeStage(task: Task, stage: WorkflowStage, options: WorkflowExecutionOptions = {}): Promise<void> {
155
+ this.emitEvent(this.eventTransformer.createStatusEvent('stage_start', { stage: stage.key }));
156
+ const overrides = options.stageOverrides?.[stage.key];
157
+ const agentName = stage.agent_name || 'code_generation';
158
+ const agentDef = this.agentRegistry.getAgent(agentName);
159
+ const isManual = stage.is_manual_only === true;
160
+ const stageKeyLower = (stage.key || '').toLowerCase().trim();
161
+ const isPlanningByKey = stageKeyLower === 'plan' || stageKeyLower.includes('plan');
162
+ const isPlanning = !isManual && ((agentDef?.agent_type === 'planning') || isPlanningByKey);
163
+ const shouldCreatePlanningBranch = overrides?.createPlanningBranch !== false; // default true
164
+ const shouldCreateImplBranch = overrides?.createImplementationBranch !== false; // default true
165
+
166
+ if (isPlanning && shouldCreatePlanningBranch) {
167
+ const planningBranch = await this.createPlanningBranch(task.id);
168
+ await this.updateTaskBranch(task.id, planningBranch);
169
+ this.emitEvent(this.eventTransformer.createStatusEvent('branch_created', { stage: stage.key, branch: planningBranch }));
170
+ } else if (!isPlanning && !isManual && shouldCreateImplBranch) {
171
+ const implBranch = await this.createImplementationBranch(task.id);
172
+ await this.updateTaskBranch(task.id, implBranch);
173
+ this.emitEvent(this.eventTransformer.createStatusEvent('branch_created', { stage: stage.key, branch: implBranch }));
174
+ }
175
+
176
+ const result = await this.stageExecutor.execute(task, stage, options);
177
+
178
+ if (result.plan) {
179
+ await this.writePlan(task.id, result.plan);
180
+ await this.commitPlan(task.id, task.title);
181
+ this.emitEvent(this.eventTransformer.createStatusEvent('commit_made', { stage: stage.key, kind: 'plan' }));
182
+ }
183
+
184
+ if (isManual) {
185
+ const defaultOpenPR = true; // manual stages default to PR for review
186
+ const openPR = overrides?.openPullRequest ?? defaultOpenPR;
187
+ if (openPR) {
188
+ // Ensure we're on an implementation branch for PRs
189
+ let branchName = await this.gitManager.getCurrentBranch();
190
+ const onTaskBranch = branchName.includes(`posthog/task-${task.id}`);
191
+ if (!onTaskBranch && (overrides?.createImplementationBranch !== false)) {
192
+ const implBranch = await this.createImplementationBranch(task.id);
193
+ await this.updateTaskBranch(task.id, implBranch);
194
+ branchName = implBranch;
195
+ this.emitEvent(this.eventTransformer.createStatusEvent('branch_created', { stage: stage.key, branch: implBranch }));
196
+ }
197
+ try {
198
+ const prUrl = await this.createPullRequest(task.id, branchName, task.title, task.description);
199
+ await this.updateTaskBranch(task.id, branchName);
200
+ await this.attachPullRequestToTask(task.id, prUrl);
201
+ this.emitEvent(this.eventTransformer.createStatusEvent('pr_created', { stage: stage.key, prUrl }));
202
+ } catch {}
203
+ }
204
+ // Do not auto-progress on manual stages
205
+ this.emitEvent(this.eventTransformer.createStatusEvent('stage_complete', { stage: stage.key }));
206
+ return;
207
+ }
208
+
209
+ if (result.results) {
210
+ const existingPlan = await this.readPlan(task.id);
211
+ const planSummary = existingPlan ? existingPlan.split('\n')[0] : undefined;
212
+ await this.commitImplementation(task.id, task.title, planSummary);
213
+ this.emitEvent(this.eventTransformer.createStatusEvent('commit_made', { stage: stage.key, kind: 'implementation' }));
214
+ }
215
+
216
+ // PR creation on complete stage (or if explicitly requested), regardless of whether edits occurred
217
+ {
218
+ const defaultOpenPR = stage.key.toLowerCase().includes('complete');
219
+ const openPR = overrides?.openPullRequest ?? defaultOpenPR;
220
+ if (openPR) {
221
+ const branchName = await this.gitManager.getCurrentBranch();
222
+ try {
223
+ const prUrl = await this.createPullRequest(task.id, branchName, task.title, task.description);
224
+ await this.updateTaskBranch(task.id, branchName);
225
+ await this.attachPullRequestToTask(task.id, prUrl);
226
+ this.emitEvent(this.eventTransformer.createStatusEvent('pr_created', { stage: stage.key, prUrl }));
227
+ } catch {}
228
+ }
229
+ }
230
+
231
+ this.emitEvent(this.eventTransformer.createStatusEvent('stage_complete', { stage: stage.key }));
232
+ }
233
+
234
+ async progressToNextStage(taskId: string): Promise<void> {
235
+ if (!this.posthogAPI) throw new Error('PostHog API not configured. Cannot progress stage.');
236
+ await this.posthogAPI.progressTask(taskId, { auto: true });
237
+ }
238
+
239
+ // Direct prompt execution - still supported for low-level usage
240
+ async run(prompt: string, options: { repositoryPath?: string; permissionMode?: import('./types').PermissionMode; queryOverrides?: Record<string, any> } = {}): Promise<ExecutionResult> {
241
+ const baseOptions: Record<string, any> = {
242
+ model: "claude-4-5-sonnet",
243
+ cwd: options.repositoryPath || this.workingDirectory,
244
+ permissionMode: (options.permissionMode as any) || "default",
245
+ settingSources: ["local"],
246
+ };
247
+
248
+ const response = query({
249
+ prompt,
250
+ options: { ...baseOptions, ...(options.queryOverrides || {}) },
251
+ });
252
+
253
+ const results = [];
254
+ for await (const message of response) {
255
+ this.logger.debug('Received message in direct run', { type: message.type });
256
+ const transformedEvent = this.eventTransformer.transform(message);
257
+ this.onEvent?.(transformedEvent);
258
+ results.push(message);
259
+ }
260
+
261
+ return { results };
262
+ }
263
+
264
+ // PostHog task operations
265
+ async fetchTask(taskId: string): Promise<Task> {
266
+ this.logger.debug('Fetching task from PostHog', { taskId });
267
+ if (!this.posthogAPI) {
268
+ const error = new Error('PostHog API not configured. Provide posthogApiUrl and posthogApiKey in constructor.');
269
+ this.logger.error('PostHog API not configured', error);
270
+ throw error;
271
+ }
272
+ return this.posthogAPI.fetchTask(taskId);
273
+ }
274
+
275
+ async listTasks(filters?: {
276
+ repository?: string;
277
+ organization?: string;
278
+ origin_product?: string;
279
+ workflow?: string;
280
+ current_stage?: string;
281
+ }): Promise<Task[]> {
282
+ if (!this.posthogAPI) {
283
+ throw new Error('PostHog API not configured. Provide posthogApiUrl and posthogApiKey in constructor.');
284
+ }
285
+ return this.posthogAPI.listTasks(filters);
286
+ }
287
+
288
+ // File system operations for task artifacts
289
+ async writeTaskFile(taskId: string, fileName: string, content: string, type: 'plan' | 'context' | 'reference' | 'output' = 'reference'): Promise<void> {
290
+ this.logger.debug('Writing task file', { taskId, fileName, type, contentLength: content.length });
291
+ await this.fileManager.writeTaskFile(taskId, { name: fileName, content, type });
292
+ }
293
+
294
+ async readTaskFile(taskId: string, fileName: string): Promise<string | null> {
295
+ this.logger.debug('Reading task file', { taskId, fileName });
296
+ return await this.fileManager.readTaskFile(taskId, fileName);
297
+ }
298
+
299
+ async getTaskFiles(taskId: string): Promise<any[]> {
300
+ this.logger.debug('Getting task files', { taskId });
301
+ const files = await this.fileManager.getTaskFiles(taskId);
302
+ this.logger.debug('Found task files', { taskId, fileCount: files.length });
303
+ return files;
304
+ }
305
+
306
+ async writePlan(taskId: string, plan: string): Promise<void> {
307
+ this.logger.info('Writing plan', { taskId, planLength: plan.length });
308
+ await this.fileManager.writePlan(taskId, plan);
309
+ }
310
+
311
+ async readPlan(taskId: string): Promise<string | null> {
312
+ this.logger.debug('Reading plan', { taskId });
313
+ return await this.fileManager.readPlan(taskId);
314
+ }
315
+
316
+ // Git operations for task workflow
317
+ async createPlanningBranch(taskId: string): Promise<string> {
318
+ this.logger.info('Creating planning branch', { taskId });
319
+ const branchName = await this.gitManager.createTaskPlanningBranch(taskId);
320
+ this.logger.debug('Planning branch created', { taskId, branchName });
321
+ // Only create gitignore after we're on the new branch
322
+ await this.fileManager.ensureGitignore();
323
+ return branchName;
324
+ }
325
+
326
+ async commitPlan(taskId: string, taskTitle: string): Promise<string> {
327
+ this.logger.info('Committing plan', { taskId, taskTitle });
328
+ const commitHash = await this.gitManager.commitPlan(taskId, taskTitle);
329
+ this.logger.debug('Plan committed', { taskId, commitHash });
330
+ return commitHash;
331
+ }
332
+
333
+ async createImplementationBranch(taskId: string, planningBranchName?: string): Promise<string> {
334
+ this.logger.info('Creating implementation branch', { taskId, fromBranch: planningBranchName });
335
+ const branchName = await this.gitManager.createTaskImplementationBranch(taskId, planningBranchName);
336
+ this.logger.debug('Implementation branch created', { taskId, branchName });
337
+ return branchName;
338
+ }
339
+
340
+ async commitImplementation(taskId: string, taskTitle: string, planSummary?: string): Promise<string> {
341
+ this.logger.info('Committing implementation', { taskId, taskTitle });
342
+ const commitHash = await this.gitManager.commitImplementation(taskId, taskTitle, planSummary);
343
+ this.logger.debug('Implementation committed', { taskId, commitHash });
344
+ return commitHash;
345
+ }
346
+
347
+ async createPullRequest(taskId: string, branchName: string, taskTitle: string, taskDescription: string): Promise<string> {
348
+ this.logger.info('Creating pull request', { taskId, branchName, taskTitle });
349
+
350
+ // Build PR body
351
+ const prBody = `## Task Details
352
+ **Task ID**: ${taskId}
353
+ **Description**: ${taskDescription}
354
+
355
+ ## Changes
356
+ This PR implements the changes described in the task.
357
+
358
+ Generated by PostHog Agent`;
359
+
360
+ const prUrl = await this.gitManager.createPullRequest(
361
+ branchName,
362
+ taskTitle,
363
+ prBody
364
+ );
365
+
366
+ this.logger.info('Pull request created', { taskId, prUrl });
367
+ return prUrl;
368
+ }
369
+
370
+ async attachPullRequestToTask(taskId: string, prUrl: string): Promise<void> {
371
+ this.logger.info('Attaching PR to task', { taskId, prUrl });
372
+
373
+ if (!this.posthogAPI) {
374
+ const error = new Error('PostHog API not configured. Cannot attach PR to task.');
375
+ this.logger.error('PostHog API not configured', error);
376
+ throw error;
377
+ }
378
+
379
+ await this.posthogAPI.updateTask(taskId, { github_pr_url: prUrl });
380
+ this.logger.debug('PR attached to task', { taskId, prUrl });
381
+ }
382
+
383
+ async updateTaskBranch(taskId: string, branchName: string): Promise<void> {
384
+ this.logger.info('Updating task branch', { taskId, branchName });
385
+
386
+ if (!this.posthogAPI) {
387
+ const error = new Error('PostHog API not configured. Cannot update task branch.');
388
+ this.logger.error('PostHog API not configured', error);
389
+ throw error;
390
+ }
391
+
392
+ await this.posthogAPI.updateTask(taskId, { github_branch: branchName });
393
+ this.logger.debug('Task branch updated', { taskId, branchName });
394
+ }
395
+
396
+ // Execution management
397
+ cancelTask(taskId: string): void {
398
+ // Find the execution for this task and cancel it
399
+ for (const [executionId, execution] of this.taskManager['executionStates']) {
400
+ if (execution.taskId === taskId && execution.status === 'running') {
401
+ this.taskManager.cancelExecution(executionId);
402
+ break;
403
+ }
404
+ }
405
+ }
406
+
407
+ getTaskExecutionStatus(taskId: string): string | null {
408
+ // Find the execution for this task
409
+ for (const execution of this.taskManager['executionStates'].values()) {
410
+ if (execution.taskId === taskId) {
411
+ return execution.status;
412
+ }
413
+ }
414
+ return null;
415
+ }
416
+
417
+ private emitEvent(event: any): void {
418
+ if (this.debug && event.type !== 'token') {
419
+ // Log all events except tokens (too verbose)
420
+ this.logger.debug('Emitting event', { type: event.type, ts: event.ts });
421
+ }
422
+ this.onEvent?.(event);
423
+ }
424
+ }
425
+
426
+ export { PermissionMode } from './types';
427
+ export type { Task, SupportingFile, ExecutionResult, AgentConfig } from './types';
428
+ export type { WorkflowDefinition, WorkflowStage, WorkflowExecutionOptions } from './workflow-types';
@@ -0,0 +1,53 @@
1
+ export const EXECUTION_SYSTEM_PROMPT = `<context>
2
+ You have access to the local repository files for fast read/write operations.
3
+ You also have access to GitHub via the GitHub MCP server for additional repository operations.
4
+ Work with local files for your main implementation, and use GitHub MCP for any additional repository queries.
5
+ Commit changes to the repository regularly.
6
+ </context>
7
+
8
+ <role>
9
+ PostHog AI Coding Agent — autonomously transform a ticket into a merge-ready pull request that follows existing project conventions.
10
+ </role>
11
+
12
+ <tools>
13
+ Local file system (for main implementation work)
14
+ PostHog MCP server (for PostHog operations)
15
+ </tools>
16
+
17
+ <constraints>
18
+ - Follow existing style and patterns you discover in the repo.
19
+ - Try not to add new external dependencies, only if needed.
20
+ - Implement structured logging and error handling; never log secrets.
21
+ - Avoid destructive shell commands.
22
+ - ALWAYS create appropriate .gitignore files to exclude build artifacts, dependencies, and temporary files.
23
+ </constraints>
24
+
25
+ <checklist>
26
+ - Created or updated .gitignore file with appropriate exclusions
27
+ - Created dependency files (requirements.txt, package.json, etc.) with exact versions
28
+ - Added clear setup/installation instructions to README.md
29
+ - Code compiles and tests pass.
30
+ - Added or updated tests.
31
+ - Captured meaningful events with PostHog SDK.
32
+ - Wrapped new logic in an PostHog feature flag.
33
+ - Updated docs, readme or type hints if needed.
34
+ - Verified no build artifacts or dependencies are being committed
35
+ </checklist>
36
+
37
+ <workflow>
38
+ - first make a plan and create a todo list
39
+ - execute the todo list one by one
40
+ - test the changes
41
+ </workflow>
42
+
43
+ <output_format>
44
+ Once finished respond with a summary of changes made
45
+ </output_format>
46
+
47
+ <thinking>
48
+ Use this area as a private scratch-pad for step-by-step reasoning; erase before final output.
49
+ </thinking>
50
+
51
+ <task>
52
+ Complete the ticket in a thoughtful step by step manner. Plan thoroughly and make sure to add logging and error handling as well as cover edge cases.
53
+ </task>`;
@@ -0,0 +1,67 @@
1
+ export const PLANNING_SYSTEM_PROMPT = `# PostHog AI Coding Agent - Planning Mode
2
+
3
+ You are a PostHog AI Coding Agent operating in PLANNING mode.
4
+
5
+ ## Your Role
6
+
7
+ You are a specialized planning agent that analyzes codebases and creates detailed implementation plans for development tasks.
8
+
9
+ ## Important Constraints
10
+
11
+ - **Read-Only Mode**: You can only read files, search code, and analyze the codebase
12
+ - **No Modifications**: You cannot make any changes, edits, or execute commands
13
+ - **Research Focus**: Your goal is understanding and planning, not implementation
14
+
15
+ ## Available Tools
16
+
17
+ - File reading and exploration
18
+ - Code search and analysis
19
+ - Repository structure analysis
20
+ - Documentation review
21
+
22
+ ## Planning Process
23
+
24
+ When given a task, follow this systematic approach:
25
+
26
+ 1. **Codebase Analysis**
27
+ - Explore the repository structure
28
+ - Identify relevant files and components
29
+ - Understand existing patterns and conventions
30
+ - Review related code and dependencies
31
+
32
+ 2. **Requirements Analysis**
33
+ - Break down the task requirements
34
+ - Identify technical constraints
35
+ - Note any existing implementations to build upon
36
+ - Consider edge cases and potential issues
37
+
38
+ 3. **Implementation Planning**
39
+ - Outline the step-by-step approach
40
+ - Identify files that need to be created or modified
41
+ - Plan the order of implementation
42
+ - Note any dependencies or prerequisites
43
+
44
+ 4. **Documentation**
45
+ - Create a clear, actionable plan
46
+ - Include specific file paths and changes needed
47
+ - Note any testing requirements
48
+ - Highlight potential risks or considerations
49
+
50
+ ## Plan Output
51
+
52
+ When you have completed your analysis, use the \`exit_plan_mode\` tool to present your plan. Your plan should include:
53
+
54
+ - **Summary**: Brief overview of the implementation approach
55
+ - **Files to Create/Modify**: Specific paths and purposes
56
+ - **Implementation Steps**: Ordered list of actions to take
57
+ - **Considerations**: Dependencies, risks, and important notes
58
+ - **Testing Strategy**: How to verify the implementation works
59
+
60
+ ## Context Integration
61
+
62
+ If supporting files are provided, incorporate them into your analysis:
63
+ - **Context files**: Additional requirements or constraints
64
+ - **Reference files**: Examples or documentation to follow
65
+ - **Previous plans**: Build upon or refine existing planning work
66
+
67
+ Your planning should be thorough enough that another agent in execution mode can implement the changes successfully.`;