2121use function Laravel \Prompts \form ;
2222use function Laravel \Prompts \select ;
2323use function Laravel \Prompts \spin ;
24+ use function Laravel \Prompts \text ;
2425use function Termwind \render ;
2526
2627class ChatAssistant
@@ -49,33 +50,41 @@ public function __construct(OnBoardingSteps $onBoardingSteps)
4950 * @throws FatalRequestException
5051 * @throws RequestException
5152 */
52- public function getCurrentProject (): Project
53+ public function getCurrentProject (bool $ isNew ): Project
5354 {
5455 $ projectPath = getcwd ();
5556 $ project = Project::where ('path ' , $ projectPath )->first ();
5657
57- if ($ project ) {
58+ if ($ isNew && $ project ) {
59+ // Update the existing project if isNew is true
60+ $ project ->assistant_id = $ this ->createNewAssistant ()->id ; // Update based on new assistant
61+ $ project ->save ();
5862 return $ project ;
5963 }
6064
61- $ userChoice = select (
62- label: 'No existing project found. Would you like to create a new assistant or use an existing one? ' ,
63- options: [
64- 'create_new ' => 'Create New Assistant ' ,
65- 'use_existing ' => 'Use Existing Assistant ' ,
66- ]
67- );
65+ if (!$ project ) {
66+ // If there's no existing project, create a new one
67+ $ userChoice = select (
68+ label: 'No project found. Would you like to create a new assistant or use an existing one? ' ,
69+ options: [
70+ 'create_new ' => 'Create New Assistant ' ,
71+ 'use_existing ' => 'Use Existing Assistant ' ,
72+ ]
73+ );
6874
69- $ assistantId = match ($ userChoice ) {
70- 'create_new ' => $ this ->createNewAssistant ()->id ,
71- 'use_existing ' => $ this ->selectExistingAssistant (),
72- default => throw new Exception ('Invalid choice ' ),
73- };
75+ $ assistantId = match ($ userChoice ) {
76+ 'create_new ' => $ this ->createNewAssistant ()->id ,
77+ 'use_existing ' => $ this ->selectExistingAssistant (),
78+ default => throw new Exception ('Invalid choice ' ),
79+ };
7480
75- return Project::create ([
76- 'path ' => $ projectPath ,
77- 'assistant_id ' => $ assistantId ,
78- ]);
81+ return Project::create ([
82+ 'path ' => $ projectPath ,
83+ 'assistant_id ' => $ assistantId ,
84+ ]);
85+ }
86+
87+ return $ project ;
7988 }
8089
8190 /**
@@ -115,16 +124,16 @@ public function createNewAssistant(): Assistant
115124 'description ' => $ assistant ['description ' ],
116125 'model ' => $ assistant ['model ' ],
117126 'prompt ' => $ assistant ['prompt ' ],
118- 'service ' => $ service,
127+ 'service ' => $ service
119128 ]);
120129 }
121130
122131 /**
123132 * @throws Exception
124133 */
125- public function createThread ()
134+ public function createThread (bool $ isNew ): \ App \ Models \ Thread
126135 {
127- $ project = $ this ->getCurrentProject ();
136+ $ project = $ this ->getCurrentProject ($ isNew );
128137 $ latestThread = $ project ->threads ()->latest ()->first ();
129138
130139 if ($ latestThread && $ this ->shouldUseExistingThread ()) {
@@ -161,6 +170,11 @@ public function getAnswer($thread, ?string $message): string
161170 $ thread ->load ('messages ' );
162171
163172 $ service = $ thread ->assistant ->service ;
173+
174+ if (!config ("aiproviders. {$ service }" )) {
175+ throw new Exception ("Service {$ service } is not configured " );
176+ }
177+
164178 $ connector = $ this ->getConnector ($ service );
165179 $ chatRequest = $ this ->getChatRequest ($ service , $ thread );
166180
@@ -179,8 +193,18 @@ private function handleTools($thread, $message): string
179193 {
180194 $ answer = $ message ->content ;
181195
182- $ thread ->messages ()->create ($ message ->toArray ());
183- if ($ message ->tool_calls !== null && $ message ->tool_calls ->isNotEmpty ()) {
196+ $ messageData = [
197+ 'role ' => $ message ->role ,
198+ 'content ' => $ message ->content ,
199+ ];
200+
201+ if (!empty ($ message ->tool_calls )) {
202+ $ messageData ['tool_calls ' ] = $ message ->tool_calls ;
203+ }
204+
205+ $ thread ->messages ()->create ($ messageData );
206+
207+ if (!empty ($ message ->tool_calls )) {
184208 $ this ->renderAnswer ($ answer );
185209
186210 foreach ($ message ->tool_calls as $ toolCall ) {
@@ -211,7 +235,7 @@ private function getModels(string $service): Collection
211235 $ listModelsRequestClass = config ("aiproviders. {$ service }.listModelsRequest " );
212236
213237 if ($ listModelsRequestClass !== null ) {
214- $ connector = new $ connectorClass ();
238+ $ connector = new $ connectorClass ($ service );
215239 return $ connector ->send (new $ listModelsRequestClass ())->dto ();
216240 }
217241
@@ -255,7 +279,7 @@ private function shouldUseExistingThread(): bool
255279 private function getConnector (string $ service ): object
256280 {
257281 $ connectorClass = config ("aiproviders. {$ service }.connector " );
258- return new $ connectorClass ();
282+ return new $ connectorClass ($ service );
259283 }
260284
261285 private function getChatRequest (string $ service , $ thread ): object
@@ -295,7 +319,6 @@ private function executeToolCall($thread, $toolCall): void
295319
296320 private function ensureAPIKey (string $ service ): void
297321 {
298- $ apiKeyConfigName = strtoupper ($ service ).'_API_KEY ' ;
299322 if (!config ("aiproviders. {$ service }.api_key " )) {
300323 $ this ->onBoardingSteps ->requestAPIKey ($ service );
301324 }
0 commit comments