@@ -64,10 +64,13 @@ export class AIProvider {
6464 */
6565 async generateWithCopilot ( prompt , options = { } ) {
6666 let client ;
67+ let clientStarted = false ;
68+
6769 try {
6870 // Create and start the Copilot client
6971 client = new CopilotClient ( ) ;
7072 await client . start ( ) ;
73+ clientStarted = true ;
7174
7275 // Create session with model
7376 const session = await client . createSession ( {
@@ -79,25 +82,30 @@ export class AIProvider {
7982 prompt,
8083 } ) ;
8184
82- // Clean up
83- await client . stop ( ) ;
85+ // Extract the content from response, handling multiple possible shapes
86+ let content = null ;
87+
88+ if ( typeof response === 'string' ) {
89+ content = response ;
90+ } else {
91+ content =
92+ // Preserve original expected shape first
93+ response ?. data ?. content ??
94+ // Common OpenAI / chat-like shapes under data
95+ response ?. data ?. choices ?. [ 0 ] ?. message ?. content ??
96+ response ?. data ?. choices ?. [ 0 ] ?. content ??
97+ // Or directly on the response object
98+ response ?. choices ?. [ 0 ] ?. message ?. content ??
99+ response ?. choices ?. [ 0 ] ?. content ??
100+ response ?. content ;
101+ }
84102
85- // Extract the content from response
86- if ( response ?. data ?. content ) {
87- return response . data . content . trim ( ) ;
103+ if ( typeof content === 'string' && content . trim ( ) ) {
104+ return content . trim ( ) ;
88105 }
89106
90107 throw new Error ( 'No response from Copilot' ) ;
91108 } catch ( error ) {
92- // Clean up client if it was created
93- if ( client ) {
94- try {
95- await client . stop ( ) ;
96- } catch {
97- // Ignore cleanup errors
98- }
99- }
100-
101109 console . error ( 'Copilot error:' , error . message ) ;
102110
103111 // Fallback to OpenAI if available
@@ -114,6 +122,15 @@ export class AIProvider {
114122 '3. GitHub CLI installed and in PATH\n\n' +
115123 'Or set OpenAI key as fallback: magicc auth openai <key>' ,
116124 ) ;
125+ } finally {
126+ // Clean up client if it was started
127+ if ( client && clientStarted ) {
128+ try {
129+ await client . stop ( ) ;
130+ } catch {
131+ // Ignore cleanup errors
132+ }
133+ }
117134 }
118135 }
119136
0 commit comments