Skip to content

Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!

Python

fix: Messages deduplication

Clémence Lesné0997ad10ecf5
diff --git a/app/models/call.py b/app/models/call.py
index 14b2410..e0b6f75 100644
--- a/app/models/call.py
+++ b/app/models/call.py
@@ -70,18 +70,26 @@ class CallGetModel(BaseModel):
"""
Merge messages with the same persona.
"""
- merged: list[MessageModel] = []
- for new_message in messages:
- if not (
- merged
- and (last := merged[-1]).persona == new_message.persona
- and last.action == new_message.action
- ):
+
+ # Skip if there are no messages
+ if not messages:
+ return messages
+
+ # Iterate over the messages
+ merged: list[MessageModel] = [messages[0]]
+ for new_message in messages[1:]:
+ # If the last message is not from the same persona or action, keep it as is
+ last = merged[-1]
+ if last.persona != new_message.persona or last.action != new_message.action:
merged.append(new_message)
continue
+
+ # Merge the content and tool calls
last.content = (last.content + " " + new_message.content).strip()
+ last.tool_calls = list({*last.tool_calls, *new_message.tool_calls})
+ # Override the style
last.style = new_message.style
- last.tool_calls += new_message.tool_calls
+
return merged
diff --git a/app/models/message.py b/app/models/message.py
index c216449..3a59ea0 100644
--- a/app/models/message.py
+++ b/app/models/message.py
@@ -55,6 +55,26 @@ class ToolModel(BaseModel):
function_name: str = ""
tool_id: str = ""
+ def __add__(self, other: object) -> "ToolModel":
+ if not isinstance(other, ChoiceDeltaToolCall):
+ return NotImplemented
+ if other.id:
+ self.tool_id = other.id
+ if other.function:
+ if other.function.name:
+ self.function_name = other.function.name
+ if other.function.arguments:
+ self.function_arguments += other.function.arguments
+ return self
+
+ def __hash__(self) -> int:
+ return self.tool_id.__hash__()
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, ToolModel):
+ return False
+ return self.tool_id == other.tool_id
+
def to_openai(self) -> ChatCompletionMessageToolCallParam:
return ChatCompletionMessageToolCallParam(
id=self.tool_id,
@@ -71,16 +91,6 @@ class ToolModel(BaseModel):
},
)
- def __add__(self, other: ChoiceDeltaToolCall) -> "ToolModel":
- if other.id:
- self.tool_id = other.id
- if other.function:
- if other.function.name:
- self.function_name = other.function.name
- if other.function.arguments:
- self.function_arguments += other.function.arguments
- return self
-
async def execute_function(self, plugin: object) -> None:
from app.helpers.logging import logger