agno 2.2.11__py3-none-any.whl → 2.2.12__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- agno/agent/agent.py +62 -47
- agno/db/mysql/mysql.py +1 -1
- agno/db/singlestore/singlestore.py +1 -1
- agno/db/sqlite/async_sqlite.py +1 -1
- agno/db/sqlite/sqlite.py +1 -1
- agno/filters.py +354 -0
- agno/knowledge/knowledge.py +43 -22
- agno/os/interfaces/slack/router.py +53 -33
- agno/os/interfaces/slack/slack.py +9 -1
- agno/os/router.py +25 -1
- agno/run/base.py +3 -2
- agno/session/agent.py +10 -5
- agno/team/team.py +44 -17
- agno/utils/agent.py +22 -17
- agno/utils/gemini.py +15 -5
- agno/utils/knowledge.py +12 -5
- agno/utils/log.py +1 -0
- agno/utils/print_response/agent.py +5 -4
- agno/utils/print_response/team.py +5 -4
- agno/vectordb/base.py +2 -4
- agno/vectordb/cassandra/cassandra.py +12 -5
- agno/vectordb/chroma/chromadb.py +10 -4
- agno/vectordb/clickhouse/clickhousedb.py +12 -4
- agno/vectordb/couchbase/couchbase.py +12 -3
- agno/vectordb/lancedb/lance_db.py +69 -144
- agno/vectordb/langchaindb/langchaindb.py +13 -4
- agno/vectordb/lightrag/lightrag.py +8 -3
- agno/vectordb/llamaindex/llamaindexdb.py +10 -4
- agno/vectordb/milvus/milvus.py +16 -5
- agno/vectordb/mongodb/mongodb.py +14 -3
- agno/vectordb/pgvector/pgvector.py +73 -15
- agno/vectordb/pineconedb/pineconedb.py +6 -2
- agno/vectordb/qdrant/qdrant.py +25 -13
- agno/vectordb/redis/redisdb.py +37 -30
- agno/vectordb/singlestore/singlestore.py +9 -4
- agno/vectordb/surrealdb/surrealdb.py +13 -3
- agno/vectordb/upstashdb/upstashdb.py +8 -5
- agno/vectordb/weaviate/weaviate.py +29 -12
- agno/workflow/workflow.py +13 -7
- {agno-2.2.11.dist-info → agno-2.2.12.dist-info}/METADATA +1 -1
- {agno-2.2.11.dist-info → agno-2.2.12.dist-info}/RECORD +44 -43
- {agno-2.2.11.dist-info → agno-2.2.12.dist-info}/WHEEL +0 -0
- {agno-2.2.11.dist-info → agno-2.2.12.dist-info}/licenses/LICENSE +0 -0
- {agno-2.2.11.dist-info → agno-2.2.12.dist-info}/top_level.txt +0 -0
agno/filters.py
ADDED
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
"""Search filter expressions for filtering knowledge base documents and search results.
|
|
2
|
+
|
|
3
|
+
This module provides a set of filter operators for constructing complex search queries
|
|
4
|
+
that can be applied to knowledge bases, vector databases, and other searchable content.
|
|
5
|
+
|
|
6
|
+
Filter Types:
|
|
7
|
+
- Comparison: EQ (equals), GT (greater than), LT (less than)
|
|
8
|
+
- Inclusion: IN (value in list)
|
|
9
|
+
- Logical: AND, OR, NOT
|
|
10
|
+
|
|
11
|
+
Example:
|
|
12
|
+
>>> from agno.filters import EQ, GT, IN, AND, OR, NOT
|
|
13
|
+
>>>
|
|
14
|
+
>>> # Simple equality filter
|
|
15
|
+
>>> filter = EQ("category", "technology")
|
|
16
|
+
>>>
|
|
17
|
+
>>> # Complex filter with multiple conditions
|
|
18
|
+
>>> filter = AND(
|
|
19
|
+
... EQ("status", "published"),
|
|
20
|
+
... GT("views", 1000),
|
|
21
|
+
... IN("category", ["tech", "science"])
|
|
22
|
+
... )
|
|
23
|
+
>>>
|
|
24
|
+
>>> # Using OR logic
|
|
25
|
+
>>> filter = OR(EQ("priority", "high"), EQ("urgent", True))
|
|
26
|
+
>>>
|
|
27
|
+
>>> # Negating conditions
|
|
28
|
+
>>> filter = NOT(EQ("status", "archived"))
|
|
29
|
+
>>>
|
|
30
|
+
>>> # Complex nested logic
|
|
31
|
+
>>> filter = OR(
|
|
32
|
+
... AND(EQ("type", "article"), GT("word_count", 500)),
|
|
33
|
+
... AND(EQ("type", "tutorial"), NOT(EQ("difficulty", "beginner")))
|
|
34
|
+
... )
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
from __future__ import annotations
|
|
38
|
+
|
|
39
|
+
from typing import Any, List
|
|
40
|
+
|
|
41
|
+
# ============================================================
|
|
42
|
+
# Base Expression
|
|
43
|
+
# ============================================================
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class FilterExpr:
|
|
47
|
+
"""Base class for all filter expressions.
|
|
48
|
+
|
|
49
|
+
Filters can be combined using AND, OR, and NOT classes:
|
|
50
|
+
- AND: Combine filters where both expressions must be true
|
|
51
|
+
- OR: Combine filters where either expression can be true
|
|
52
|
+
- NOT: Negate a filter expression
|
|
53
|
+
|
|
54
|
+
Example:
|
|
55
|
+
>>> # Create complex filters using AND, OR, NOT
|
|
56
|
+
>>> filter = OR(AND(EQ("status", "active"), GT("age", 18)), EQ("role", "admin"))
|
|
57
|
+
>>> # Equivalent to: (status == "active" AND age > 18) OR role == "admin"
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
# Logical operator overloads
|
|
61
|
+
def __or__(self, other: FilterExpr) -> OR:
|
|
62
|
+
"""Combine two filters with OR logic."""
|
|
63
|
+
return OR(self, other)
|
|
64
|
+
|
|
65
|
+
def __and__(self, other: FilterExpr) -> AND:
|
|
66
|
+
"""Combine two filters with AND logic."""
|
|
67
|
+
return AND(self, other)
|
|
68
|
+
|
|
69
|
+
def __invert__(self) -> NOT:
|
|
70
|
+
"""Negate a filter."""
|
|
71
|
+
return NOT(self)
|
|
72
|
+
|
|
73
|
+
def to_dict(self) -> dict:
|
|
74
|
+
"""Convert the filter expression to a dictionary representation."""
|
|
75
|
+
raise NotImplementedError("Subclasses must implement to_dict()")
|
|
76
|
+
|
|
77
|
+
def __repr__(self) -> str:
|
|
78
|
+
return f"{self.__class__.__name__}({self.__dict__})"
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
# ============================================================
|
|
82
|
+
# Comparison & Inclusion Filters
|
|
83
|
+
# ============================================================
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class EQ(FilterExpr):
|
|
87
|
+
"""Equality filter - matches documents where a field equals a specific value.
|
|
88
|
+
|
|
89
|
+
Args:
|
|
90
|
+
key: The field name to compare
|
|
91
|
+
value: The value to match against
|
|
92
|
+
|
|
93
|
+
Example:
|
|
94
|
+
>>> # Match documents where status is "published"
|
|
95
|
+
>>> filter = EQ("status", "published")
|
|
96
|
+
>>>
|
|
97
|
+
>>> # Match documents where author_id is 123
|
|
98
|
+
>>> filter = EQ("author_id", 123)
|
|
99
|
+
"""
|
|
100
|
+
|
|
101
|
+
def __init__(self, key: str, value: Any):
|
|
102
|
+
self.key = key
|
|
103
|
+
self.value = value
|
|
104
|
+
|
|
105
|
+
def to_dict(self) -> dict:
|
|
106
|
+
return {"op": "EQ", "key": self.key, "value": self.value}
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class IN(FilterExpr):
|
|
110
|
+
"""Inclusion filter - matches documents where a field's value is in a list of values.
|
|
111
|
+
|
|
112
|
+
Args:
|
|
113
|
+
key: The field name to check
|
|
114
|
+
values: List of acceptable values
|
|
115
|
+
|
|
116
|
+
Example:
|
|
117
|
+
>>> # Match documents where category is either "tech", "science", or "engineering"
|
|
118
|
+
>>> filter = IN("category", ["tech", "science", "engineering"])
|
|
119
|
+
>>>
|
|
120
|
+
>>> # Match documents where status is either "draft" or "published"
|
|
121
|
+
>>> filter = IN("status", ["draft", "published"])
|
|
122
|
+
"""
|
|
123
|
+
|
|
124
|
+
def __init__(self, key: str, values: List[Any]):
|
|
125
|
+
self.key = key
|
|
126
|
+
self.values = values
|
|
127
|
+
|
|
128
|
+
def to_dict(self) -> dict:
|
|
129
|
+
return {"op": "IN", "key": self.key, "values": self.values}
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
class GT(FilterExpr):
|
|
133
|
+
"""Greater than filter - matches documents where a field's value is greater than a threshold.
|
|
134
|
+
|
|
135
|
+
Args:
|
|
136
|
+
key: The field name to compare
|
|
137
|
+
value: The threshold value
|
|
138
|
+
|
|
139
|
+
Example:
|
|
140
|
+
>>> # Match documents where age is greater than 18
|
|
141
|
+
>>> filter = GT("age", 18)
|
|
142
|
+
>>>
|
|
143
|
+
>>> # Match documents where price is greater than 100.0
|
|
144
|
+
>>> filter = GT("price", 100.0)
|
|
145
|
+
>>>
|
|
146
|
+
>>> # Match documents created after a certain timestamp
|
|
147
|
+
>>> filter = GT("created_at", 1234567890)
|
|
148
|
+
"""
|
|
149
|
+
|
|
150
|
+
def __init__(self, key: str, value: Any):
|
|
151
|
+
self.key = key
|
|
152
|
+
self.value = value
|
|
153
|
+
|
|
154
|
+
def to_dict(self) -> dict:
|
|
155
|
+
return {"op": "GT", "key": self.key, "value": self.value}
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
class LT(FilterExpr):
|
|
159
|
+
"""Less than filter - matches documents where a field's value is less than a threshold.
|
|
160
|
+
|
|
161
|
+
Args:
|
|
162
|
+
key: The field name to compare
|
|
163
|
+
value: The threshold value
|
|
164
|
+
|
|
165
|
+
Example:
|
|
166
|
+
>>> # Match documents where age is less than 65
|
|
167
|
+
>>> filter = LT("age", 65)
|
|
168
|
+
>>>
|
|
169
|
+
>>> # Match documents where price is less than 50.0
|
|
170
|
+
>>> filter = LT("price", 50.0)
|
|
171
|
+
>>>
|
|
172
|
+
>>> # Match documents created before a certain timestamp
|
|
173
|
+
>>> filter = LT("created_at", 1234567890)
|
|
174
|
+
"""
|
|
175
|
+
|
|
176
|
+
def __init__(self, key: str, value: Any):
|
|
177
|
+
self.key = key
|
|
178
|
+
self.value = value
|
|
179
|
+
|
|
180
|
+
def to_dict(self) -> dict:
|
|
181
|
+
return {"op": "LT", "key": self.key, "value": self.value}
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
# ============================================================
|
|
185
|
+
# Logical Operators
|
|
186
|
+
# ============================================================
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
class AND(FilterExpr):
|
|
190
|
+
"""Logical AND operator - matches documents where ALL expressions are true.
|
|
191
|
+
|
|
192
|
+
Combines multiple filter expressions where every expression must be satisfied
|
|
193
|
+
for a document to match.
|
|
194
|
+
|
|
195
|
+
Args:
|
|
196
|
+
*expressions: Variable number of FilterExpr expressions to combine with AND logic
|
|
197
|
+
|
|
198
|
+
Example:
|
|
199
|
+
>>> # Match documents where status is "published" AND age > 18
|
|
200
|
+
>>> filter = AND(EQ("status", "published"), GT("age", 18))
|
|
201
|
+
>>>
|
|
202
|
+
>>> # Multiple expressions
|
|
203
|
+
>>> filter = AND(
|
|
204
|
+
... EQ("status", "active"),
|
|
205
|
+
... GT("score", 80),
|
|
206
|
+
... IN("category", ["tech", "science"])
|
|
207
|
+
... )
|
|
208
|
+
"""
|
|
209
|
+
|
|
210
|
+
def __init__(self, *expressions: FilterExpr):
|
|
211
|
+
self.expressions = list(expressions)
|
|
212
|
+
|
|
213
|
+
def to_dict(self) -> dict:
|
|
214
|
+
return {"op": "AND", "conditions": [e.to_dict() for e in self.expressions]}
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
class OR(FilterExpr):
|
|
218
|
+
"""Logical OR operator - matches documents where ANY expression is true.
|
|
219
|
+
|
|
220
|
+
Combines multiple filter expressions where at least one expression must be satisfied
|
|
221
|
+
for a document to match.
|
|
222
|
+
|
|
223
|
+
Args:
|
|
224
|
+
*expressions: Variable number of FilterExpr expressions to combine with OR logic
|
|
225
|
+
|
|
226
|
+
Example:
|
|
227
|
+
>>> # Match documents where status is "published" OR status is "archived"
|
|
228
|
+
>>> filter = OR(EQ("status", "published"), EQ("status", "archived"))
|
|
229
|
+
>>>
|
|
230
|
+
>>> # Complex: Match VIP users OR users with high score
|
|
231
|
+
>>> filter = OR(
|
|
232
|
+
... EQ("membership", "VIP"),
|
|
233
|
+
... GT("score", 1000)
|
|
234
|
+
... )
|
|
235
|
+
"""
|
|
236
|
+
|
|
237
|
+
def __init__(self, *expressions: FilterExpr):
|
|
238
|
+
self.expressions = list(expressions)
|
|
239
|
+
|
|
240
|
+
def to_dict(self) -> dict:
|
|
241
|
+
return {"op": "OR", "conditions": [e.to_dict() for e in self.expressions]}
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
class NOT(FilterExpr):
|
|
245
|
+
"""Logical NOT operator - matches documents where the expression is NOT true.
|
|
246
|
+
|
|
247
|
+
Negates a filter expression, matching documents that don't satisfy the expression.
|
|
248
|
+
|
|
249
|
+
Args:
|
|
250
|
+
expression: The FilterExpr expression to negate
|
|
251
|
+
|
|
252
|
+
Example:
|
|
253
|
+
>>> # Match documents where status is NOT "draft"
|
|
254
|
+
>>> filter = NOT(EQ("status", "draft"))
|
|
255
|
+
>>>
|
|
256
|
+
>>> # Exclude inactive users with low scores
|
|
257
|
+
>>> filter = NOT(AND(EQ("status", "inactive"), LT("score", 10)))
|
|
258
|
+
>>>
|
|
259
|
+
>>> # Match users who are NOT in the blocked list
|
|
260
|
+
>>> filter = NOT(IN("user_id", [101, 102, 103]))
|
|
261
|
+
"""
|
|
262
|
+
|
|
263
|
+
def __init__(self, expression: FilterExpr):
|
|
264
|
+
self.expression = expression
|
|
265
|
+
|
|
266
|
+
def to_dict(self) -> dict:
|
|
267
|
+
return {"op": "NOT", "condition": self.expression.to_dict()}
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
# ============================================================
|
|
271
|
+
# Deserialization
|
|
272
|
+
# ============================================================
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def from_dict(filter_dict: dict) -> FilterExpr:
|
|
276
|
+
"""Reconstruct a FilterExpr object from its dictionary representation.
|
|
277
|
+
|
|
278
|
+
This function deserializes filter expressions that were serialized using the
|
|
279
|
+
to_dict() method, enabling filters to be passed through JSON APIs and reconstructed
|
|
280
|
+
on the server side.
|
|
281
|
+
|
|
282
|
+
Args:
|
|
283
|
+
filter_dict: Dictionary representation of a filter expression with an "op" key
|
|
284
|
+
|
|
285
|
+
Returns:
|
|
286
|
+
FilterExpr: The reconstructed filter expression object
|
|
287
|
+
|
|
288
|
+
Raises:
|
|
289
|
+
ValueError: If the filter dictionary has an invalid structure or unknown operator
|
|
290
|
+
|
|
291
|
+
Example:
|
|
292
|
+
>>> # Serialize and deserialize a simple filter
|
|
293
|
+
>>> original = EQ("status", "published")
|
|
294
|
+
>>> serialized = original.to_dict()
|
|
295
|
+
>>> # {"op": "EQ", "key": "status", "value": "published"}
|
|
296
|
+
>>> reconstructed = from_dict(serialized)
|
|
297
|
+
>>>
|
|
298
|
+
>>> # Complex filter with nested expressions
|
|
299
|
+
>>> complex_filter = OR(AND(EQ("type", "article"), GT("views", 1000)), IN("priority", ["high", "urgent"]))
|
|
300
|
+
>>> serialized = complex_filter.to_dict()
|
|
301
|
+
>>> reconstructed = from_dict(serialized)
|
|
302
|
+
>>>
|
|
303
|
+
>>> # From JSON API
|
|
304
|
+
>>> import json
|
|
305
|
+
>>> json_str = '{"op": "AND", "conditions": [{"op": "EQ", "key": "status", "value": "active"}, {"op": "GT", "key": "age", "value": 18}]}'
|
|
306
|
+
>>> filter_dict = json.loads(json_str)
|
|
307
|
+
>>> filter_expr = from_dict(filter_dict)
|
|
308
|
+
"""
|
|
309
|
+
if not isinstance(filter_dict, dict) or "op" not in filter_dict:
|
|
310
|
+
raise ValueError(f"Invalid filter dictionary: must contain 'op' key. Got: {filter_dict}")
|
|
311
|
+
|
|
312
|
+
op = filter_dict["op"]
|
|
313
|
+
|
|
314
|
+
# Comparison and inclusion operators
|
|
315
|
+
if op == "EQ":
|
|
316
|
+
if "key" not in filter_dict or "value" not in filter_dict:
|
|
317
|
+
raise ValueError(f"EQ filter requires 'key' and 'value' fields. Got: {filter_dict}")
|
|
318
|
+
return EQ(filter_dict["key"], filter_dict["value"])
|
|
319
|
+
|
|
320
|
+
elif op == "IN":
|
|
321
|
+
if "key" not in filter_dict or "values" not in filter_dict:
|
|
322
|
+
raise ValueError(f"IN filter requires 'key' and 'values' fields. Got: {filter_dict}")
|
|
323
|
+
return IN(filter_dict["key"], filter_dict["values"])
|
|
324
|
+
|
|
325
|
+
elif op == "GT":
|
|
326
|
+
if "key" not in filter_dict or "value" not in filter_dict:
|
|
327
|
+
raise ValueError(f"GT filter requires 'key' and 'value' fields. Got: {filter_dict}")
|
|
328
|
+
return GT(filter_dict["key"], filter_dict["value"])
|
|
329
|
+
|
|
330
|
+
elif op == "LT":
|
|
331
|
+
if "key" not in filter_dict or "value" not in filter_dict:
|
|
332
|
+
raise ValueError(f"LT filter requires 'key' and 'value' fields. Got: {filter_dict}")
|
|
333
|
+
return LT(filter_dict["key"], filter_dict["value"])
|
|
334
|
+
|
|
335
|
+
# Logical operators
|
|
336
|
+
elif op == "AND":
|
|
337
|
+
if "conditions" not in filter_dict:
|
|
338
|
+
raise ValueError(f"AND filter requires 'conditions' field. Got: {filter_dict}")
|
|
339
|
+
conditions = [from_dict(cond) for cond in filter_dict["conditions"]]
|
|
340
|
+
return AND(*conditions)
|
|
341
|
+
|
|
342
|
+
elif op == "OR":
|
|
343
|
+
if "conditions" not in filter_dict:
|
|
344
|
+
raise ValueError(f"OR filter requires 'conditions' field. Got: {filter_dict}")
|
|
345
|
+
conditions = [from_dict(cond) for cond in filter_dict["conditions"]]
|
|
346
|
+
return OR(*conditions)
|
|
347
|
+
|
|
348
|
+
elif op == "NOT":
|
|
349
|
+
if "condition" not in filter_dict:
|
|
350
|
+
raise ValueError(f"NOT filter requires 'condition' field. Got: {filter_dict}")
|
|
351
|
+
return NOT(from_dict(filter_dict["condition"]))
|
|
352
|
+
|
|
353
|
+
else:
|
|
354
|
+
raise ValueError(f"Unknown filter operator: {op}")
|
agno/knowledge/knowledge.py
CHANGED
|
@@ -13,6 +13,7 @@ from httpx import AsyncClient
|
|
|
13
13
|
|
|
14
14
|
from agno.db.base import AsyncBaseDb, BaseDb
|
|
15
15
|
from agno.db.schemas.knowledge import KnowledgeRow
|
|
16
|
+
from agno.filters import FilterExpr
|
|
16
17
|
from agno.knowledge.content import Content, ContentAuth, ContentStatus, FileData
|
|
17
18
|
from agno.knowledge.document import Document
|
|
18
19
|
from agno.knowledge.reader import Reader, ReaderFactory
|
|
@@ -403,7 +404,7 @@ class Knowledge:
|
|
|
403
404
|
|
|
404
405
|
if path.is_file():
|
|
405
406
|
if self._should_include_file(str(path), include, exclude):
|
|
406
|
-
|
|
407
|
+
log_debug(f"Adding file {path} due to include/exclude filters")
|
|
407
408
|
|
|
408
409
|
await self._add_to_contents_db(content)
|
|
409
410
|
if self._should_skip(content.content_hash, skip_if_exists): # type: ignore[arg-type]
|
|
@@ -1392,7 +1393,7 @@ class Knowledge:
|
|
|
1392
1393
|
self,
|
|
1393
1394
|
query: str,
|
|
1394
1395
|
max_results: Optional[int] = None,
|
|
1395
|
-
filters: Optional[Dict[str, Any]] = None,
|
|
1396
|
+
filters: Optional[Union[Dict[str, Any], List[FilterExpr]]] = None,
|
|
1396
1397
|
search_type: Optional[str] = None,
|
|
1397
1398
|
) -> List[Document]:
|
|
1398
1399
|
"""Returns relevant documents matching a query"""
|
|
@@ -1423,7 +1424,7 @@ class Knowledge:
|
|
|
1423
1424
|
self,
|
|
1424
1425
|
query: str,
|
|
1425
1426
|
max_results: Optional[int] = None,
|
|
1426
|
-
filters: Optional[Dict[str, Any]] = None,
|
|
1427
|
+
filters: Optional[Union[Dict[str, Any], List[FilterExpr]]] = None,
|
|
1427
1428
|
search_type: Optional[str] = None,
|
|
1428
1429
|
) -> List[Document]:
|
|
1429
1430
|
"""Returns relevant documents matching a query"""
|
|
@@ -1465,38 +1466,58 @@ class Knowledge:
|
|
|
1465
1466
|
self.valid_metadata_filters.update(await self._aget_filters_from_db())
|
|
1466
1467
|
return self.valid_metadata_filters
|
|
1467
1468
|
|
|
1468
|
-
def _validate_filters(self, filters: Optional[Dict[str, Any]]) -> Tuple[
|
|
1469
|
+
def _validate_filters(self, filters: Optional[Union[Dict[str, Any], List[FilterExpr]]]) -> Tuple[Any, List[str]]:
|
|
1470
|
+
"""Internal method to validate filters against known metadata keys."""
|
|
1469
1471
|
if not filters:
|
|
1470
|
-
return
|
|
1472
|
+
return None, []
|
|
1471
1473
|
|
|
1472
|
-
valid_filters: Dict[str, Any] =
|
|
1474
|
+
valid_filters: Optional[Dict[str, Any]] = None
|
|
1473
1475
|
invalid_keys = []
|
|
1474
1476
|
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1477
|
+
if isinstance(filters, dict):
|
|
1478
|
+
# If no metadata filters tracked yet, all keys are considered invalid
|
|
1479
|
+
if self.valid_metadata_filters is None:
|
|
1480
|
+
invalid_keys = list(filters.keys())
|
|
1481
|
+
log_debug(f"No valid metadata filters tracked yet. All filter keys considered invalid: {invalid_keys}")
|
|
1482
|
+
return None, invalid_keys
|
|
1483
|
+
|
|
1484
|
+
valid_filters = {}
|
|
1485
|
+
for key, value in filters.items():
|
|
1486
|
+
# Handle both normal keys and prefixed keys like meta_data.key
|
|
1487
|
+
base_key = key.split(".")[-1] if "." in key else key
|
|
1488
|
+
if base_key in self.valid_metadata_filters or key in self.valid_metadata_filters:
|
|
1489
|
+
valid_filters[key] = value
|
|
1490
|
+
else:
|
|
1491
|
+
invalid_keys.append(key)
|
|
1492
|
+
log_debug(f"Invalid filter key: {key} - not present in knowledge base")
|
|
1493
|
+
|
|
1494
|
+
elif isinstance(filters, List):
|
|
1495
|
+
# Validate that list contains FilterExpr instances
|
|
1496
|
+
for i, filter_item in enumerate(filters):
|
|
1497
|
+
if not isinstance(filter_item, FilterExpr):
|
|
1498
|
+
log_warning(
|
|
1499
|
+
f"Invalid filter at index {i}: expected FilterExpr instance, "
|
|
1500
|
+
f"got {type(filter_item).__name__}. "
|
|
1501
|
+
f"Use filter expressions like EQ('key', 'value'), IN('key', [values]), "
|
|
1502
|
+
f"AND(...), OR(...), NOT(...) from agno.filters"
|
|
1503
|
+
)
|
|
1504
|
+
|
|
1505
|
+
# Filter expressions are already validated, return empty dict/list
|
|
1506
|
+
# The actual filtering happens in the vector_db layer
|
|
1507
|
+
return filters, []
|
|
1489
1508
|
|
|
1490
1509
|
return valid_filters, invalid_keys
|
|
1491
1510
|
|
|
1492
|
-
def validate_filters(self, filters: Optional[Dict[str, Any]]) -> Tuple[
|
|
1511
|
+
def validate_filters(self, filters: Optional[Union[Dict[str, Any], List[FilterExpr]]]) -> Tuple[Any, List[str]]:
|
|
1493
1512
|
if self.valid_metadata_filters is None:
|
|
1494
1513
|
self.valid_metadata_filters = set()
|
|
1495
1514
|
self.valid_metadata_filters.update(self._get_filters_from_db())
|
|
1496
1515
|
|
|
1497
1516
|
return self._validate_filters(filters)
|
|
1498
1517
|
|
|
1499
|
-
async def async_validate_filters(
|
|
1518
|
+
async def async_validate_filters(
|
|
1519
|
+
self, filters: Optional[Union[Dict[str, Any], List[FilterExpr]]]
|
|
1520
|
+
) -> Tuple[Any, List[str]]:
|
|
1500
1521
|
if self.valid_metadata_filters is None:
|
|
1501
1522
|
self.valid_metadata_filters = set()
|
|
1502
1523
|
self.valid_metadata_filters.update(await self._aget_filters_from_db())
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from typing import Optional
|
|
1
|
+
from typing import Optional, Union
|
|
2
2
|
|
|
3
3
|
from fastapi import APIRouter, BackgroundTasks, HTTPException, Request
|
|
4
4
|
from pydantic import BaseModel, Field
|
|
@@ -24,7 +24,11 @@ class SlackChallengeResponse(BaseModel):
|
|
|
24
24
|
|
|
25
25
|
|
|
26
26
|
def attach_routes(
|
|
27
|
-
router: APIRouter,
|
|
27
|
+
router: APIRouter,
|
|
28
|
+
agent: Optional[Agent] = None,
|
|
29
|
+
team: Optional[Team] = None,
|
|
30
|
+
workflow: Optional[Workflow] = None,
|
|
31
|
+
reply_to_mentions_only: bool = True,
|
|
28
32
|
) -> APIRouter:
|
|
29
33
|
# Determine entity type for documentation
|
|
30
34
|
entity_type = "agent" if agent else "team" if team else "workflow" if workflow else "unknown"
|
|
@@ -34,7 +38,7 @@ def attach_routes(
|
|
|
34
38
|
operation_id=f"slack_events_{entity_type}",
|
|
35
39
|
name="slack_events",
|
|
36
40
|
description="Process incoming Slack events",
|
|
37
|
-
response_model=SlackEventResponse,
|
|
41
|
+
response_model=Union[SlackChallengeResponse, SlackEventResponse],
|
|
38
42
|
response_model_exclude_none=True,
|
|
39
43
|
responses={
|
|
40
44
|
200: {"description": "Event processed successfully"},
|
|
@@ -71,36 +75,52 @@ def attach_routes(
|
|
|
71
75
|
return SlackEventResponse(status="ok")
|
|
72
76
|
|
|
73
77
|
async def _process_slack_event(event: dict):
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
78
|
+
event_type = event.get("type")
|
|
79
|
+
|
|
80
|
+
# Only handle app_mention and message events
|
|
81
|
+
if event_type not in ("app_mention", "message"):
|
|
82
|
+
return
|
|
83
|
+
|
|
84
|
+
channel_type = event.get("channel_type", "")
|
|
85
|
+
|
|
86
|
+
# Handle duplicate replies
|
|
87
|
+
if not reply_to_mentions_only and event_type == "app_mention":
|
|
88
|
+
return
|
|
89
|
+
|
|
90
|
+
# If reply_to_mentions_only is True, ignore every message that is not a DM
|
|
91
|
+
if reply_to_mentions_only and event_type == "message" and channel_type != "im":
|
|
92
|
+
return
|
|
93
|
+
|
|
94
|
+
# Extract event data
|
|
95
|
+
user = None
|
|
96
|
+
message_text = event.get("text", "")
|
|
97
|
+
channel_id = event.get("channel", "")
|
|
98
|
+
user = event.get("user")
|
|
99
|
+
if event.get("thread_ts"):
|
|
100
|
+
ts = event.get("thread_ts", "")
|
|
101
|
+
else:
|
|
102
|
+
ts = event.get("ts", "")
|
|
103
|
+
|
|
104
|
+
# Use the timestamp as the session id, so that each thread is a separate session
|
|
105
|
+
session_id = ts
|
|
106
|
+
|
|
107
|
+
if agent:
|
|
108
|
+
response = await agent.arun(message_text, user_id=user, session_id=session_id)
|
|
109
|
+
elif team:
|
|
110
|
+
response = await team.arun(message_text, user_id=user, session_id=session_id) # type: ignore
|
|
111
|
+
elif workflow:
|
|
112
|
+
response = await workflow.arun(message_text, user_id=user, session_id=session_id) # type: ignore
|
|
113
|
+
|
|
114
|
+
if response:
|
|
115
|
+
if hasattr(response, "reasoning_content") and response.reasoning_content:
|
|
116
|
+
_send_slack_message(
|
|
117
|
+
channel=channel_id,
|
|
118
|
+
message=f"Reasoning: \n{response.reasoning_content}",
|
|
119
|
+
thread_ts=ts,
|
|
120
|
+
italics=True,
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
_send_slack_message(channel=channel_id, message=response.content or "", thread_ts=ts)
|
|
104
124
|
|
|
105
125
|
def _send_slack_message(channel: str, thread_ts: str, message: str, italics: bool = False):
|
|
106
126
|
if len(message) <= 40000:
|
|
@@ -21,12 +21,14 @@ class Slack(BaseInterface):
|
|
|
21
21
|
workflow: Optional[Workflow] = None,
|
|
22
22
|
prefix: str = "/slack",
|
|
23
23
|
tags: Optional[List[str]] = None,
|
|
24
|
+
reply_to_mentions_only: bool = True,
|
|
24
25
|
):
|
|
25
26
|
self.agent = agent
|
|
26
27
|
self.team = team
|
|
27
28
|
self.workflow = workflow
|
|
28
29
|
self.prefix = prefix
|
|
29
30
|
self.tags = tags or ["Slack"]
|
|
31
|
+
self.reply_to_mentions_only = reply_to_mentions_only
|
|
30
32
|
|
|
31
33
|
if not (self.agent or self.team or self.workflow):
|
|
32
34
|
raise ValueError("Slack requires an agent, team or workflow")
|
|
@@ -34,6 +36,12 @@ class Slack(BaseInterface):
|
|
|
34
36
|
def get_router(self) -> APIRouter:
|
|
35
37
|
self.router = APIRouter(prefix=self.prefix, tags=self.tags) # type: ignore
|
|
36
38
|
|
|
37
|
-
self.router = attach_routes(
|
|
39
|
+
self.router = attach_routes(
|
|
40
|
+
router=self.router,
|
|
41
|
+
agent=self.agent,
|
|
42
|
+
team=self.team,
|
|
43
|
+
workflow=self.workflow,
|
|
44
|
+
reply_to_mentions_only=self.reply_to_mentions_only,
|
|
45
|
+
)
|
|
38
46
|
|
|
39
47
|
return self.router
|
agno/os/router.py
CHANGED
|
@@ -106,10 +106,34 @@ async def _get_request_kwargs(request: Request, endpoint_func: Callable) -> Dict
|
|
|
106
106
|
try:
|
|
107
107
|
if isinstance(knowledge_filters, str):
|
|
108
108
|
knowledge_filters_dict = json.loads(knowledge_filters) # type: ignore
|
|
109
|
-
|
|
109
|
+
|
|
110
|
+
# Try to deserialize FilterExpr objects
|
|
111
|
+
from agno.filters import from_dict
|
|
112
|
+
|
|
113
|
+
# Check if it's a single FilterExpr dict or a list of FilterExpr dicts
|
|
114
|
+
if isinstance(knowledge_filters_dict, dict) and "op" in knowledge_filters_dict:
|
|
115
|
+
# Single FilterExpr - convert to list format
|
|
116
|
+
kwargs["knowledge_filters"] = [from_dict(knowledge_filters_dict)]
|
|
117
|
+
elif isinstance(knowledge_filters_dict, list):
|
|
118
|
+
# List of FilterExprs or mixed content
|
|
119
|
+
deserialized = []
|
|
120
|
+
for item in knowledge_filters_dict:
|
|
121
|
+
if isinstance(item, dict) and "op" in item:
|
|
122
|
+
deserialized.append(from_dict(item))
|
|
123
|
+
else:
|
|
124
|
+
# Keep non-FilterExpr items as-is
|
|
125
|
+
deserialized.append(item)
|
|
126
|
+
kwargs["knowledge_filters"] = deserialized
|
|
127
|
+
else:
|
|
128
|
+
# Regular dict filter
|
|
129
|
+
kwargs["knowledge_filters"] = knowledge_filters_dict
|
|
110
130
|
except json.JSONDecodeError:
|
|
111
131
|
kwargs.pop("knowledge_filters")
|
|
112
132
|
log_warning(f"Invalid knowledge_filters parameter couldn't be loaded: {knowledge_filters}")
|
|
133
|
+
except ValueError as e:
|
|
134
|
+
# Filter deserialization failed
|
|
135
|
+
kwargs.pop("knowledge_filters")
|
|
136
|
+
log_warning(f"Invalid FilterExpr in knowledge_filters: {e}")
|
|
113
137
|
|
|
114
138
|
# Parse boolean and null values
|
|
115
139
|
for key, value in kwargs.items():
|
agno/run/base.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
from dataclasses import asdict, dataclass
|
|
2
2
|
from enum import Enum
|
|
3
|
-
from typing import Any, Dict, Optional
|
|
3
|
+
from typing import Any, Dict, List, Optional, Union
|
|
4
4
|
|
|
5
5
|
from pydantic import BaseModel
|
|
6
6
|
|
|
7
|
+
from agno.filters import FilterExpr
|
|
7
8
|
from agno.media import Audio, Image, Video
|
|
8
9
|
from agno.models.message import Citations, Message, MessageReferences
|
|
9
10
|
from agno.models.metrics import Metrics
|
|
@@ -18,7 +19,7 @@ class RunContext:
|
|
|
18
19
|
user_id: Optional[str] = None
|
|
19
20
|
|
|
20
21
|
dependencies: Optional[Dict[str, Any]] = None
|
|
21
|
-
knowledge_filters: Optional[Dict[str, Any]] = None
|
|
22
|
+
knowledge_filters: Optional[Union[Dict[str, Any], List[FilterExpr]]] = None
|
|
22
23
|
metadata: Optional[Dict[str, Any]] = None
|
|
23
24
|
session_state: Optional[Dict[str, Any]] = None
|
|
24
25
|
|