neg-0 / gina

0 stars 0 forks source link

Backend Service sometimes run its onMembersAdded function twice #2

Open neg-0 opened 1 day ago

neg-0 commented 1 day ago

Haven't found root cause. Sometimes the service will run the onMembersAdded function twice, resulting in 2 threads created. I think this consumed extra tokens as well.

Code:

this.onMembersAdded(async (context, next) => {
      const membersAdded = context.activity.membersAdded;
      for (let idx in membersAdded) {
        if (membersAdded[idx].id !== context.activity.recipient.id) {
          try {
            // Load the assistant
            console.log(`Loading Assistant: ${assistantId}`);
            this.assistantResponse = await client.beta.assistants.retrieve(assistantId);
            console.log(`Assistant loaded: ${JSON.stringify(this.assistantResponse)}`);

            // Create a thread
            this.assistantThread = await client.beta.threads.create();
            console.log(`Thread created: ${JSON.stringify(this.assistantThread)}`);

            await context.sendActivity("Hi, I'm GINA!");
          } catch (error) {
            console.error(error);
          }
        }
      }
      await next();
    });

Sample console output:

Loading Assistant: asst_kcmKmoWsZ2fTD32WAc9Zq704
Loading Assistant: asst_kcmKmoWsZ2fTD32WAc9Zq704
Assistant loaded: {"id":"asst_kcmKmoWsZ2fTD32WAc9Zq704","object":"assistant","created_at":1727150413,"name":"GINA Assistant","description":null,"model":"gpt-4o-mini","instructions":"You are a helpful AI assistant named GINA.","tools":[{"type":"function","function":{"name":"get_projects","description":"Retrieve project details from the database.","parameters":{"type":"object","properties":{"status":{"type":"string","description":"The status of the project (e.g., 'active', 'completed')"},"division":{"type":"string","description":"The division or team responsible for the project"}},"required":[]}}}],"top_p":1,"temperature":1,"file_ids":[],"metadata":{},"response_format":"auto"}
Assistant loaded: {"id":"asst_kcmKmoWsZ2fTD32WAc9Zq704","object":"assistant","created_at":1727150413,"name":"GINA Assistant","description":null,"model":"gpt-4o-mini","instructions":"You are a helpful AI assistant named GINA.","tools":[{"type":"function","function":{"name":"get_projects","description":"Retrieve project details from the database.","parameters":{"type":"object","properties":{"status":{"type":"string","description":"The status of the project (e.g., 'active', 'completed')"},"division":{"type":"string","description":"The division or team responsible for the project"}},"required":[]}}}],"top_p":1,"temperature":1,"file_ids":[],"metadata":{},"response_format":"auto"}
Thread created: {"id":"thread_D9der6xD1nYXvBd8Z40uXyCE","object":"thread","created_at":1727241773,"metadata":{}}
Thread created: {"id":"thread_UGXFYeygyQ3i4NUjb9LoragD","object":"thread","created_at":1727241773,"metadata":{}}
neg-0 commented 1 day ago

Maybe the for loop has multiple members being added? Maybe the app is "re-registering" itself with the Azure service when nodemon restarts it, creating multiple instances Maybe when I refresh the Vite frontend it still has a cached connection but creates a new one anyways, resulting in multiple function calls