-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAgentRunsProvider.ts
More file actions
163 lines (139 loc) · 6.31 KB
/
AgentRunsProvider.ts
File metadata and controls
163 lines (139 loc) · 6.31 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
import * as vscode from 'vscode';
import { ApiClient, AgentRun } from '../api/ApiClient';
import { AuthManager } from '../auth/AuthManager';
import { GitUtils, GitRepository } from '../utils/GitUtils';
import { PrUtils, PullRequest } from '../utils/PrUtils';
export class AgentRunItem extends vscode.TreeItem {
constructor(
public readonly agentRun: AgentRun,
public readonly collapsibleState: vscode.TreeItemCollapsibleState
) {
super(agentRun.summary || `Agent Run ${agentRun.id}`, collapsibleState);
this.tooltip = this.getTooltip();
this.description = this.getDescription();
this.iconPath = this.getIcon();
this.contextValue = 'agentRun';
// Make it clickable to open PR diff or agent run
this.command = {
command: 'codegen.openAgentRunOrPr',
title: 'Open Agent Run or PR',
arguments: [agentRun]
};
}
private getTooltip(): string {
const createdAt = new Date(this.agentRun.created_at).toLocaleString();
const summary = this.agentRun.summary || 'No summary available';
const status = this.agentRun.status;
const prCount = this.agentRun.github_pull_requests?.length || 0;
return `ID: ${this.agentRun.id}\nStatus: ${status}\nCreated: ${createdAt}\nPRs: ${prCount}\n\n${summary}`;
}
private getDescription(): string {
const status = this.agentRun.status;
const createdAt = new Date(this.agentRun.created_at);
const now = new Date();
const diffMs = now.getTime() - createdAt.getTime();
const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
const diffDays = Math.floor(diffHours / 24);
let timeAgo: string;
if (diffDays > 0) {
timeAgo = `${diffDays}d ago`;
} else if (diffHours > 0) {
timeAgo = `${diffHours}h ago`;
} else {
const diffMinutes = Math.floor(diffMs / (1000 * 60));
timeAgo = `${diffMinutes}m ago`;
}
const prText = PrUtils.getPrStatusDescription(this.agentRun);
const repoText = this.agentRun.repository ? ` • ${this.agentRun.repository.name}` : '';
return `${status} • ${timeAgo} • ${prText}${repoText}`;
}
private getIcon(): vscode.ThemeIcon {
const status = this.agentRun.status.toLowerCase();
switch (status) {
case 'complete':
case 'completed':
return new vscode.ThemeIcon('check', new vscode.ThemeColor('charts.green'));
case 'running':
case 'in_progress':
return new vscode.ThemeIcon('sync~spin', new vscode.ThemeColor('charts.blue'));
case 'failed':
case 'error':
return new vscode.ThemeIcon('error', new vscode.ThemeColor('charts.red'));
case 'pending':
case 'queued':
return new vscode.ThemeIcon('clock', new vscode.ThemeColor('charts.yellow'));
default:
return new vscode.ThemeIcon('robot');
}
}
}
export class AgentRunsProvider implements vscode.TreeDataProvider<AgentRunItem> {
private _onDidChangeTreeData: vscode.EventEmitter<AgentRunItem | undefined | null | void> = new vscode.EventEmitter<AgentRunItem | undefined | null | void>();
readonly onDidChangeTreeData: vscode.Event<AgentRunItem | undefined | null | void> = this._onDidChangeTreeData.event;
private agentRuns: AgentRun[] = [];
private currentRepository: GitRepository | null = null;
constructor(
private apiClient: ApiClient,
private authManager: AuthManager
) {
// Listen for workspace changes to update repository context
vscode.workspace.onDidChangeWorkspaceFolders(() => {
this.updateRepositoryContext();
});
// Initialize repository context
this.updateRepositoryContext();
}
refresh(): void {
this.updateRepositoryContext();
this._onDidChangeTreeData.fire();
}
private async updateRepositoryContext(): Promise<void> {
try {
this.currentRepository = await GitUtils.getCurrentRepository();
console.log('Current repository:', this.currentRepository?.fullName || 'None');
} catch (error) {
console.error('Failed to get current repository:', error);
this.currentRepository = null;
}
}
getTreeItem(element: AgentRunItem): vscode.TreeItem {
return element;
}
async getChildren(element?: AgentRunItem): Promise<AgentRunItem[]> {
if (!this.authManager.isAuthenticated()) {
return [];
}
if (!element) {
// Root level - return agent runs
try {
// Filter by current repository if available
const repositoryName = this.currentRepository?.fullName;
const response = await this.apiClient.getAgentRuns(1, 20, repositoryName);
this.agentRuns = response.items;
// If we have a repository context but no results, show a helpful message
if (this.agentRuns.length === 0 && repositoryName) {
// Return a placeholder item to show the user what's happening
const placeholderItem = new vscode.TreeItem(
`No agents found for ${repositoryName}`,
vscode.TreeItemCollapsibleState.None
);
placeholderItem.description = 'Try creating a new agent or check a different repository';
placeholderItem.iconPath = new vscode.ThemeIcon('info');
placeholderItem.contextValue = 'placeholder';
return [placeholderItem];
}
return this.agentRuns.map(agentRun =>
new AgentRunItem(agentRun, vscode.TreeItemCollapsibleState.None)
);
} catch (error) {
console.error('Failed to load agent runs:', error);
vscode.window.showErrorMessage(`Failed to load agent runs: ${error}`);
return [];
}
}
return [];
}
getCurrentRepository(): GitRepository | null {
return this.currentRepository;
}
}