From 9b95ffb59e4c6c44c3154447691f4bdb4f2256f2 Mon Sep 17 00:00:00 2001 From: John Costa Date: Thu, 17 Apr 2025 18:57:13 +0100 Subject: [PATCH] feat(contact-agent): using createContact with an ID field to provide updates --- backend/agents/client/client.go | 2 +- backend/agents/contact_agent.go | 16 +++++++++++ backend/agents/orchestrator.go | 13 +-------- backend/models/contacts.go | 47 +++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 13 deletions(-) diff --git a/backend/agents/client/client.go b/backend/agents/client/client.go index 888626a..ed260d0 100644 --- a/backend/agents/client/client.go +++ b/backend/agents/client/client.go @@ -222,7 +222,7 @@ func (client *AgentClient) Process(info ToolHandlerInfo, req *AgentRequestBody) client.Reply = toolCall.Function.Arguments } - client.Log.Debug("Tool call response", "toolCall", toolCall.Function.Name, "response", toolResponse.Content) + client.Log.Debug("Tool call", "name", toolCall.Function.Name, "arguments", toolCall.Function.Arguments, "response", toolResponse.Content) req.Chat.AddToolResponse(toolResponse) } diff --git a/backend/agents/contact_agent.go b/backend/agents/contact_agent.go index 06f84ff..630e763 100644 --- a/backend/agents/contact_agent.go +++ b/backend/agents/contact_agent.go @@ -85,6 +85,10 @@ const contactTools = ` "parameters": { "type": "object", "properties": { + "contactId": { + "type": "string", + "description": "The UUID of the contact. You should only provide this IF you believe the contact already exists, from listContacts." + }, "name": { "type": "string", "description": "The full name of the person being added as a contact. This field is mandatory." @@ -124,6 +128,7 @@ const contactTools = ` type listContactsArguments struct{} type createContactsArguments struct { Name string `json:"name"` + ContactID *string `json:"contactId"` PhoneNumber *string `json:"phoneNumber"` Address *string `json:"address"` Email *string `json:"email"` @@ -153,7 +158,18 @@ func NewContactAgent(log *log.Logger, contactModel models.ContactModel) client.A ctx := context.Background() + contactId := uuid.Nil + if args.ContactID != nil { + contactUuid, err := uuid.Parse(*args.ContactID) + if err != nil { + return model.Contacts{}, err + } + + contactId = contactUuid + } + contact, err := contactModel.Save(ctx, info.UserId, model.Contacts{ + ID: contactId, Name: args.Name, PhoneNumber: args.PhoneNumber, Email: args.Email, diff --git a/backend/agents/orchestrator.go b/backend/agents/orchestrator.go index 13d0df2..c0ae1d4 100644 --- a/backend/agents/orchestrator.go +++ b/backend/agents/orchestrator.go @@ -11,8 +11,6 @@ const orchestratorPrompt = ` **Primary Task:** Examine the input image and determine which specialized AI agent(s), available as tool calls, should be invoked to process the relevant information within the image, or if no specialized processing is needed. Your goal is to either extract and structure useful information for the user by selecting the most appropriate tool(s) or explicitly indicate that no specific action is required. -**Input:** User-provided image. - **Analysis Process & Decision Logic:** 1. **Analyze Image Content:** Scrutinize the image for distinct types of information: @@ -27,16 +25,7 @@ const orchestratorPrompt = ` * **locationAgent:** Is there information specifically identifying a place, location, or address (e.g., map, street sign, address text)? If YES, select locationAgent. * **eventAgent:** Is there information specifically related to an event (e.g., invitation, poster with date/time, schedule)? If YES, select eventAgent. * **noteAgent** Does the image contain *any* text/writing (including code, formulas)? - -**Available Agents (Tools):** - -* **noteAgent**: Use when there is any text on the image, this can be code/text/formulas any writing. -* **contactAgent**: Use when the image contains some person or contact. -* **locationAgent**: Use when the image contains some place, location or address. -* **eventAgent**: Use when the image contains some event. -* **noAgent**: Call this when you are done working on this image. - -**Execution Rules:** + * **noAgent**: Call this when you are done working on this image. * Call all applicable specialized agents (noteAgent, contactAgent, locationAgent, eventAgent) simultaneously (in parallel). ` diff --git a/backend/models/contacts.go b/backend/models/contacts.go index 030474d..2050991 100644 --- a/backend/models/contacts.go +++ b/backend/models/contacts.go @@ -29,9 +29,56 @@ func (m ContactModel) List(ctx context.Context, userId uuid.UUID) ([]model.Conta return locations, err } +func (m ContactModel) Get(ctx context.Context, contactId uuid.UUID) (model.Contacts, error) { + getContactStmt := Contacts. + SELECT(Contacts.AllColumns). + WHERE(Contacts.ID.EQ(UUID(contactId))) + + contact := model.Contacts{} + err := getContactStmt.QueryContext(ctx, m.dbPool, &contact) + + return contact, err +} + +func (m ContactModel) Update(ctx context.Context, contact model.Contacts) (model.Contacts, error) { + existingContact, err := m.Get(ctx, contact.ID) + if err != nil { + return model.Contacts{}, err + } + + existingContact.Name = contact.Name + + if contact.Description != nil { + existingContact.Description = contact.Description + } + + if contact.PhoneNumber != nil { + existingContact.PhoneNumber = contact.PhoneNumber + } + + if contact.Email != nil { + existingContact.Email = contact.Email + } + + updateContactStmt := Contacts. + UPDATE(Contacts.MutableColumns). + MODEL(existingContact). + WHERE(Contacts.ID.EQ(UUID(contact.ID))). + RETURNING(Contacts.AllColumns) + + updatedContact := model.Contacts{} + err = updateContactStmt.QueryContext(ctx, m.dbPool, &updatedContact) + + return updatedContact, err +} + func (m ContactModel) Save(ctx context.Context, userId uuid.UUID, contact model.Contacts) (model.Contacts, error) { // TODO: make this a transaction + if contact.ID != uuid.Nil { + return m.Update(ctx, contact) + } + insertContactStmt := Contacts. INSERT(Contacts.Name, Contacts.Description, Contacts.PhoneNumber, Contacts.Email). VALUES(contact.Name, contact.Description, contact.PhoneNumber, contact.Email).