forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopilotApi.ts
More file actions
183 lines (168 loc) · 5.33 KB
/
Copy pathcopilotApi.ts
File metadata and controls
183 lines (168 loc) · 5.33 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import fetch from 'cross-fetch';
import JSZip from 'jszip';
import { OctokitCommon } from './common';
import { LoggingOctokit } from './loggingOctokit';
import { PullRequestModel } from './pullRequestModel';
export interface RemoteAgentJobPayload {
problem_statement: string;
pull_request?: {
title?: string;
body_placeholder?: string;
body_suffix?: string;
base_ref?: string;
};
run_name?: string;
}
export interface RemoteAgentJobResponse {
pull_request: {
html_url: string;
number: number;
}
}
export class CopilotApi {
constructor(private octokit: LoggingOctokit, private token: string) { }
private get baseUrl(): string {
return 'https://api.githubcopilot.com';
}
async postRemoteAgentJob(
owner: string,
name: string,
payload: RemoteAgentJobPayload,
): Promise<RemoteAgentJobResponse> {
const repoSlug = `${owner}/${name}`;
const apiUrl = `${this.baseUrl}/agents/swe/jobs/${repoSlug}`;
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Copilot-Integration-Id': 'copilot-developer-dev',
'Authorization': `Bearer ${this.token}`,
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(payload)
});
if (!response.ok) {
const text = await response.text();
throw new Error(`Coding agent API error: ${response.status} ${text}`);
}
const data = await response.json();
this.validateRemoteAgentJobResponse(data);
return data;
}
private validateRemoteAgentJobResponse(data: any): asserts data is RemoteAgentJobResponse {
if (!data || typeof data !== 'object') {
throw new Error('Invalid response from coding agent');
}
if (!data.pull_request || typeof data.pull_request !== 'object') {
throw new Error('Invalid pull_request in response');
}
if (typeof data.pull_request.html_url !== 'string') {
throw new Error('Invalid pull_request.html_url in response');
}
if (typeof data.pull_request.number !== 'number') {
throw new Error('Invalid pull_request.number in response');
}
}
public async getWorkflowRunsFromAction(pullRequest: PullRequestModel): Promise<OctokitCommon.ListWorkflowRunsForRepo> {
const runs = await this.octokit.api.actions.listWorkflowRunsForRepo(
{
owner: pullRequest.githubRepository.remote.owner,
repo: pullRequest.githubRepository.remote.repositoryName,
event: 'dynamic'
}
);
if (runs.status !== 200) {
throw new Error(`Failed to fetch workflow runs: ${runs.status}`);
}
return runs.data.workflow_runs;
}
public async getLogsFromZipUrl(logsUrl: string): Promise<string[]> {
const logsZip = await fetch(logsUrl, {
headers: {
Authorization: `Bearer ${this.token}`,
Accept: 'application/json',
},
});
if (!logsZip.ok) {
throw new Error(`Failed to fetch logs zip: ${logsZip.statusText}`);
}
const logsText = await logsZip.arrayBuffer();
const copilotSteps: string[] = [];
const zip = await JSZip.loadAsync(logsText);
for (const fileName of Object.keys(zip.files)) {
const file = zip.files[fileName];
if (!file.dir && fileName.endsWith('Processing Request.txt')) {
const content = await file.async('string');
copilotSteps.push(...content.split('\n'));
}
}
return copilotSteps;
}
public async getAllSessions(pullRequest: PullRequestModel | undefined): Promise<SessionInfo[]> {
const response = await fetch(
pullRequest
? `https://api.githubcopilot.com/agents/sessions/resource/pull/${pullRequest.id}`
: 'https://api.githubcopilot.com/agents/sessions',
{
headers: {
Authorization: `Bearer ${this.token}`,
Accept: 'application/json',
},
});
if (!response.ok) {
throw new Error(`Failed to fetch sessions: ${response.statusText}`);
}
const sessions = await response.json();
return sessions.sessions;
}
public async getSessionInfo(sessionId: string): Promise<SessionInfo> {
const response = await fetch(`https://api.githubcopilot.com/agents/sessions/${sessionId}`, {
method: 'GET',
headers: {
Authorization: `Bearer ${this.token}`,
'Accept': 'application/json'
}
});
if (!response.ok) {
throw new Error(`Failed to fetch session: ${response.statusText}`);
}
return (await response.json()) as SessionInfo;
}
public async getLogsFromSession(sessionId: string): Promise<string> {
const logsResponse = await fetch(`https://api.githubcopilot.com/agents/sessions/${sessionId}/logs`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${this.token}`,
'Content-Type': 'application/json',
},
});
if (!logsResponse.ok) {
throw new Error(`Failed to fetch logs: ${logsResponse.statusText}`);
}
return await logsResponse.text();
}
}
export interface SessionInfo {
id: string;
name: string;
user_id: number;
agent_id: number;
logs: string;
logs_blob_id: string;
state: string;
owner_id: number;
repo_id: number;
resource_type: string;
resource_id: number;
last_updated_at: string;
created_at: string;
completed_at: string;
event_type: string;
workflow_run_id: number;
premium_requests: number;
error: string | null;
}