-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path02_issue_triage.py
More file actions
207 lines (167 loc) · 6 KB
/
Copy path02_issue_triage.py
File metadata and controls
207 lines (167 loc) · 6 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
204
205
206
207
#!/usr/bin/env python3
"""Issue Triage Bot using GitHub Copilot SDK Custom Tools (@define_tool).
See the tutorial for learning goals, prerequisites, and usage:
docs/copilot_sdk_tutorial/tutorials/02_custom_tools.md (English)
docs/copilot_sdk_tutorial/tutorials/02_custom_tools.ja.md (日本語)
"""
import argparse
import asyncio
import json
import sys
from typing import Any, TypedDict
from _telemetry import add_telemetry_arguments, apply_telemetry_arguments, make_client
from copilot.generated.rpc import PermissionDecisionApproveOnce
from copilot.generated.session_events import (
PermissionRequest,
SessionEventType,
)
from copilot.session import (
PermissionRequestResult,
SystemMessageReplaceConfig,
)
from copilot.tools import define_tool
from pydantic import BaseModel
# ---------------------------------------------------------------------------
# Custom tool input/output schemas
# ---------------------------------------------------------------------------
class ListIssuesInput(BaseModel):
pass
class IssueItem(BaseModel):
id: int
title: str
body: str
labels: list[str]
class ListIssuesOutput(BaseModel):
issues: list[IssueItem]
class LabelIssueInput(BaseModel):
issue_id: int
labels: list[str]
class LabelIssueOutput(BaseModel):
success: bool
issue_id: int
applied_labels: list[str]
# ---------------------------------------------------------------------------
# Sample issue data (no external API calls needed)
# ---------------------------------------------------------------------------
class _IssueDict(TypedDict):
id: int
title: str
body: str
labels: list[str]
SAMPLE_ISSUES: list[_IssueDict] = [
{
"id": 1,
"title": "Application crashes when uploading files larger than 100 MB",
"body": "Steps to reproduce: open the upload dialog, select a file > 100 MB, click Upload. "
"Expected: file uploads successfully. Actual: the app throws an unhandled exception.",
"labels": [],
},
{
"id": 2,
"title": "Add dark mode support",
"body": "It would be great to have a dark mode option in the settings panel.",
"labels": [],
},
{
"id": 3,
"title": "Typo in README: 'recieve' should be 'receive'",
"body": "Line 42 of README.md contains a typo.",
"labels": [],
},
]
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Issue Triage Bot using GitHub Copilot SDK Custom Tools",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=__doc__,
)
parser.add_argument(
"--cli-url",
"-c",
default=None,
help=(
"Optional Copilot CLI server URL (e.g. localhost:3000). "
"When omitted, the SDK launches the copilot CLI over stdio."
),
)
add_telemetry_arguments(parser)
return parser.parse_args()
async def run(cli_url: str | None) -> None:
# ------------------------------------------------------------------
# Tool implementations
# ------------------------------------------------------------------
triaged: list[dict[str, Any]] = []
@define_tool(
name="list_issues",
description="Return the list of open GitHub issues to triage.",
)
def list_issues(_input: ListIssuesInput) -> ListIssuesOutput: # noqa: ANN001
return ListIssuesOutput(
issues=[
IssueItem(
id=issue["id"],
title=issue["title"],
body=issue["body"],
labels=issue["labels"],
)
for issue in SAMPLE_ISSUES
]
)
@define_tool(
name="label_issue",
description="Apply one or more labels to a GitHub issue.",
)
def label_issue(input: LabelIssueInput) -> LabelIssueOutput: # noqa: ANN001
triaged.append({"id": input.issue_id, "labels": input.labels})
return LabelIssueOutput(
success=True,
issue_id=input.issue_id,
applied_labels=input.labels,
)
# ------------------------------------------------------------------
# Session setup
# ------------------------------------------------------------------
def approve_all(
request: PermissionRequest,
context: dict,
) -> PermissionRequestResult:
return PermissionDecisionApproveOnce()
client = make_client(cli_url)
await client.start()
session = await client.create_session(
on_permission_request=approve_all,
tools=[list_issues, label_issue],
streaming=False,
system_message=SystemMessageReplaceConfig(
mode="replace",
content=(
"You are an expert GitHub issue triage assistant. "
"Use list_issues to fetch open issues, classify each one "
"as 'bug', 'enhancement', or 'documentation', then call "
"label_issue to apply the appropriate label. "
"After triaging all issues, summarise your actions."
),
),
)
def on_event(event: Any) -> None:
if event.type == SessionEventType.TOOL_EXECUTION_START:
print(f"[Tool] Calling: {event.data.tool_name}", file=sys.stderr)
elif event.type == SessionEventType.SESSION_ERROR:
print(f"[Error] {event.data.message}", file=sys.stderr)
session.on(on_event)
prompt = "Please triage all open issues and apply the appropriate labels."
reply = await session.send_and_wait(prompt, timeout=300)
content = getattr(reply.data, "content", None) if reply else "(no response)"
print("=== Triage Summary ===")
print(content)
print("\n=== Applied Labels ===")
print(json.dumps(triaged, indent=2))
def main() -> None:
args = parse_args()
apply_telemetry_arguments(args)
try:
asyncio.run(run(args.cli_url))
except KeyboardInterrupt:
print("\nBye!")
if __name__ == "__main__":
main()