@@ -205,14 +205,27 @@ func (a *InvokeAction) invokeRemote(ctx context.Context) error {
205205 }
206206 body ["session_id" ] = sid
207207
208+ // Acquire credential and token — used for both conversation creation and the invoke request
209+ credential , err := newAgentCredential ()
210+ if err != nil {
211+ return err
212+ }
213+
214+ token , err := credential .GetToken (ctx , policy.TokenRequestOptions {
215+ Scopes : []string {"https://ai.azure.com/.default" },
216+ })
217+ if err != nil {
218+ return fmt .Errorf ("failed to get auth token: %w" , err )
219+ }
220+
208221 // Conversation ID — enables multi-turn memory via Foundry Conversations API
209222 convID , err := resolveConversationID (ctx , azdClient , name , a .flags .newSession )
210223 if err != nil {
211224 return err
212225 }
213226 if convID == "" {
214227 // Create a new conversation
215- newConvID , err := createConversation (ctx , endpoint )
228+ newConvID , err := createConversation (ctx , endpoint , token . Token )
216229 if err != nil {
217230 fmt .Printf ("Warning: could not create conversation; multi-turn memory disabled (%v)\n " , err )
218231 } else {
@@ -226,19 +239,6 @@ func (a *InvokeAction) invokeRemote(ctx context.Context) error {
226239 body ["conversation" ] = map [string ]string {"id" : convID }
227240 }
228241
229- // Make the API call
230- credential , err := newAgentCredential ()
231- if err != nil {
232- return err
233- }
234-
235- token , err := credential .GetToken (ctx , policy.TokenRequestOptions {
236- Scopes : []string {"https://ai.azure.com/.default" },
237- })
238- if err != nil {
239- return fmt .Errorf ("failed to get auth token: %w" , err )
240- }
241-
242242 payload , err := json .Marshal (body )
243243 if err != nil {
244244 return fmt .Errorf ("failed to marshal request: %w" , err )
@@ -274,26 +274,14 @@ func (a *InvokeAction) invokeRemote(ctx context.Context) error {
274274}
275275
276276// createConversation creates a new Foundry conversation for multi-turn memory.
277- func createConversation (ctx context.Context , endpoint string ) (string , error ) {
278- credential , err := newAgentCredential ()
279- if err != nil {
280- return "" , err
281- }
282-
283- token , err := credential .GetToken (ctx , policy.TokenRequestOptions {
284- Scopes : []string {"https://ai.azure.com/.default" },
285- })
286- if err != nil {
287- return "" , err
288- }
289-
277+ func createConversation (ctx context.Context , endpoint string , bearerToken string ) (string , error ) {
290278 url := fmt .Sprintf ("%s/openai/conversations?api-version=%s" , endpoint , DefaultAgentAPIVersion )
291279 req , err := http .NewRequestWithContext (ctx , http .MethodPost , url , bytes .NewReader ([]byte ("{}" )))
292280 if err != nil {
293281 return "" , err
294282 }
295283 req .Header .Set ("Content-Type" , "application/json" )
296- req .Header .Set ("Authorization" , "Bearer " + token . Token )
284+ req .Header .Set ("Authorization" , "Bearer " + bearerToken )
297285
298286 client := & http.Client {Timeout : 30 * time .Second }
299287 resp , err := client .Do (req ) //nolint:gosec // G704: endpoint is resolved from azd environment configuration
0 commit comments