1+ import sys
2+ import os
3+ import json
4+
5+ from devchat .llm .chat import chat_json
6+ from github import Github
7+ from github import Auth
8+ # from common_util import editor
9+
10+ def read_config ():
11+ config_path = os .path .join (os .getcwd (), '.devchat' , 'information' , 'config' , 'config.json' )
12+ if not os .path .exists (config_path ):
13+ print ('Config file does not exist' )
14+ return None
15+ with open (config_path , 'r' ) as f :
16+ return json .load (f )
17+
18+ def write_config (repo_path , github_token ):
19+ config_path = os .path .join (os .getcwd (), '.devchat' , 'information' , 'config' , 'config.json' )
20+ with open (config_path , 'w' ) as f :
21+ json .dump ({'repo_path' : repo_path , 'github_token' : github_token }, f )
22+
23+ PROMPT = prompt = """
24+ pls return a JSON object without line break(the response must be parsed as json, not contain any redundant chars), including a string field Title and a markdown string field called Body.
25+ {information}
26+ {request}
27+ {context}
28+ """
29+ @chat_json (prompt = PROMPT )
30+ def get_issue_content (information , request , context ):
31+ pass
32+
33+ PROMPT = prompt = """
34+ I will request you to write an issue. The request is consists of two parts, Firstly, I will give a context list(organized by concept name). You may choose most related ones and return it as a json object(with key files) according to the requst content. Then I will ask you for issue content with the context you chosen. Now is the first request, pls only return an json object with context files you chosen. context files: {context_list}, issue content:
35+ `{request_content}`
36+ """
37+ @chat_json (prompt = PROMPT )
38+ def get_related_context_file (context_list , request_content ):
39+ pass
40+
41+ # @editor("Please specify the issue's repository, "
42+ # "If the issue is within this repository, no need to specify. "
43+ # "Otherwise, format as: username/repository-name")
44+ # @editor("Input your github TOKEN to access github api:")
45+ # def set_config(repo_path, github_token):
46+ # pass
47+
48+ def get_request (path ):
49+ if not os .path .exists (path ):
50+ return
51+ with open (path , 'r' ) as f :
52+ request = f .read ()
53+ return request
54+
55+ def read_api_config ():
56+ config = read_config ()
57+ # set config by devlake ui
58+ # repo_path = ''
59+ # github_token = ''
60+ # if config != None:
61+ # repo_path = config['repo_path']
62+ # github_token = config['github_token']
63+ # else:
64+ # # request user config
65+ # repo_path, github_token = set_config(repo_path, github_token)
66+
67+ # write_config(repo_path, github_token)
68+ repo_path = config ['repo_path' ]
69+ github_token = config ['github_token' ]
70+ return repo_path , github_token
71+ def create_github_issue (github_token , repo_path , issue_content , issue_title ):
72+ auth = Auth .Token (github_token )
73+ g = Github (auth = auth )
74+ repo = g .get_repo (repo_path )
75+ issue = repo .create_issue (title = issue_title , body = issue_content )
76+ print (issue , flush = True )
77+
78+ def main ():
79+ # get user input
80+ user_input = sys .argv [1 ]
81+ params = user_input .strip ().split (' ' )
82+ user_content = params [1 ]
83+ print (f'reading request...\n ' , flush = True )
84+ request_path = os .path .join (os .getcwd (), params [0 ])
85+ request = get_request (request_path )
86+ if request == '' :
87+ print ('Request file does not exist' , flush = True )
88+ return
89+ # get repository context
90+ information = ''
91+ # get request context
92+ print (f'reading context list\n ' , flush = True )
93+ # get context list
94+ context_list = os .listdir (os .path .join (os .getcwd (), '.devchat' , 'information' , 'context' ))
95+ # print(context_list, flush=True)
96+ print (f'[ai]choosing context files...\n ' , flush = True )
97+ response = get_related_context_file (context_list = context_list , request_content = request + user_content )
98+ print (response )
99+ chosen_context_files = response ['files' ]
100+ print (f'chosen context files: { chosen_context_files } \n ' , flush = True )
101+ print (f'reading context...\n ' , flush = True )
102+ for context_file in chosen_context_files :
103+ context_file_path = os .path .join (os .getcwd (), '.devchat' , 'information' , 'context' , context_file )
104+ if not os .path .exists (context_file_path ):
105+ print (f'context file { context_file } does not exist\n ' , flush = True )
106+ return
107+ with open (context_file_path , 'r' ) as f :
108+ information += f .read ()
109+ print (f'context read\n ' , flush = True )
110+
111+ print (f'reading config...\n ' , flush = True )
112+ # check if github token is configured
113+ repo_path , github_token = read_api_config ()
114+ print (f'[ai] generating issue content...\n ' , flush = True )
115+ # # debug
116+ # print(information)
117+ # print(request)
118+ # print(user_content)
119+ # get issue_content from ai
120+ issue_content = get_issue_content (information = information , request = request , context = user_content )
121+ # print issue content and wait for user confirmation
122+ print (issue_content , flush = True )
123+ # TODO:: add user confirmation
124+ print (f'creating issue...\n ' , flush = True )
125+ create_github_issue (github_token , repo_path , issue_content ['Body' ], issue_content ['Title' ])
126+
127+ # request github api to create issue
128+ print (f'issue created\n ' , flush = True )
129+
130+
131+ if __name__ == "__main__" :
132+ main ()
0 commit comments