-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstarter_code.py
More file actions
111 lines (84 loc) · 4.41 KB
/
starter_code.py
File metadata and controls
111 lines (84 loc) · 4.41 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
from haystack.dataclasses.chat_message import ChatMessage
from haystack_integrations.components.generators.google_genai import GoogleGenAIChatGenerator
from haystack.components.agents import Agent
from haystack_integrations.tools.mcp import MCPTool, StreamableHttpServerInfo
from haystack.utils import Secret
import os
from dotenv import load_dotenv
repo_context: str | None = None
load_dotenv(dotenv_path=".env")
api_key=os.environ["GOOGLE_API_KEY"]
github_token=os.environ.get("GITHUB_TOKEN")
if not github_token:
raise RuntimeError("github token can not be read from .env........another bugggg")
print("keys read from .env")
chat_generator=GoogleGenAIChatGenerator(model="gemini-2.0-flash")
print("chat generator created succefully \n")
github_mcp_server = StreamableHttpServerInfo(
url="https://api.githubcopilot.com/mcp/",
token=Secret.from_token(github_token)
)
print("mcp server is created ...........letssss gooo\n")
get_user_tool=MCPTool(name="get_me",server_info = github_mcp_server,connection_timeout=120)
search_repos_tool=MCPTool(name="search_repositories",server_info=github_mcp_server,connection_timeout=120)
get_team_tool=MCPTool(name="get_team_members",server_info=github_mcp_server,connection_timeout=120)
list_issues_tool = MCPTool(name="list_issues",server_info=github_mcp_server,connection_timeout=120)
list_prs_tool = MCPTool(name="list_pull_requests",server_info=github_mcp_server,connection_timeout=120)
list_discussions_tool = MCPTool(name="list_discussions",server_info=github_mcp_server,connection_timeout=120)
mytoolList=[search_repos_tool,get_user_tool,get_team_tool,list_issues_tool,list_prs_tool,list_discussions_tool]
system_prompt="""You are GitHub AI Brain, an intelligent Haystack agent whose mission is to forge a GitHub-aware assistant that can:
- Sense repositories: retrieve issues, pull requests, commits, and discussions.
- Reason with wisdom: summarize project health, highlight bottlenecks, and spot stale work.
- Assist your allies: suggest reviewers, label issues, draft release notes, or compare PRs.
- Respond in natural language: answer queries like “What’s blocking the release?” or “Summarize the last sprint’s changes.”
Always be accurate, concise, and context-aware. If data is missing or ambiguous, state assumptions clearly. Never fabricate repository data. Your tone is professional, collaborative, and technically fluent."""
agent=Agent(
chat_generator=chat_generator,
tools=mytoolList,
system_prompt=system_prompt
)
def run_agent(prompt: str) -> str:
global repo_context
messages = []
if repo_context:
messages.append(ChatMessage.from_system(text=f"Repository context: {repo_context}"))
messages.append(ChatMessage.from_user(text=prompt))
response = agent.run(messages=messages)
return response["messages"][-1].text
def repo_selector():
global repo_context
response = agent.run(messages=[ChatMessage.from_user(text="only respond with my raw username using the tool get_me")])
username = response["messages"][-1].text.strip()
print("👤 Logged in as:", username)
response = agent.run(messages=[ChatMessage.from_user(text=f'''Search repositories for {username} and give each repo name in new line and indexed with an integer
When listing repositories, output raw names only (no Markdown formatting, no escaping underscores).
''')])
repo_list_text = response["messages"][-1].text
print("📂 Available repos:\n", repo_list_text)
repo_map = {}
for line in repo_list_text.splitlines():
if ":" in line:
idx, name = line.split(":", 1)
elif "." in line:
idx, name = line.split(".", 1)
else:
continue
repo_map[idx.strip()] = name.strip().strip("*_` ")
choice = input("Enter repo number: ").strip()
if choice in repo_map:
repo_context = f"{username}/{repo_map[choice]}"
print(f"✅ Repo context set to: {repo_context}")
else:
print("❌ Invalid choice")
def clear_repo_context():
global repo_context
repo_context = None
print("🔄 Repo context cleared.")
#old code below , ignore
"""
user_input="Can you retreive my user data from the github"
response = agent.run(messages=[ChatMessage.from_user(text=user_input)])
print(response)
## Print the final response
print(response["messages"][-1].text)
"""