Skip to content

Commit 1e8d0f9

Browse files
authored
add use_tag flag (#134)
1 parent c76b4fd commit 1e8d0f9

File tree

2 files changed

+74
-19
lines changed

2 files changed

+74
-19
lines changed

docs/site/openapi.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,6 +2063,40 @@
20632063
"title": "Use Gists"
20642064
},
20652065
"description": "Whether to search event gists (default) or event tip"
2066+
},
2067+
{
2068+
"name": "use_tag",
2069+
"in": "query",
2070+
"required": false,
2071+
"schema": {
2072+
"type": "boolean",
2073+
"description": "Whether to search by tags instead of query",
2074+
"default": false,
2075+
"title": "Use Tag"
2076+
},
2077+
"description": "Whether to search by tags instead of query"
2078+
},
2079+
{
2080+
"name": "tags",
2081+
"in": "query",
2082+
"required": false,
2083+
"schema": {
2084+
"type": "string",
2085+
"description": "Comma-separated list of tag names that events must have (e.g.'emotion,romance')",
2086+
"title": "Tags"
2087+
},
2088+
"description": "Comma-separated list of tag names that events must have (e.g.'emotion,romance')"
2089+
},
2090+
{
2091+
"name": "tag_values",
2092+
"in": "query",
2093+
"required": false,
2094+
"schema": {
2095+
"type": "string",
2096+
"description": "Comma-separated tag=value pairs for exact matches (e.g., 'emotion=happy,topic=work')",
2097+
"title": "Tag Values"
2098+
},
2099+
"description": "Comma-separated tag=value pairs for exact matches (e.g., 'emotion=happy,topic=work')"
20662100
}
20672101
],
20682102
"responses": {

src/server/api/memobase_server/api_layer/event.py

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,33 @@
66
from fastapi import Path, Query, Body
77

88

9+
async def _search_user_events_by_tags_internal(
10+
user_id: UUID,
11+
project_id: str,
12+
tags: str = None,
13+
tag_values: str = None,
14+
topk: int = 10,
15+
) -> res.UserEventsDataResponse:
16+
"""Internal function to search user events by tags."""
17+
has_event_tag = None
18+
if tags:
19+
has_event_tag = [tag.strip() for tag in tags.split(",") if tag.strip()]
20+
21+
event_tag_equal = None
22+
if tag_values:
23+
event_tag_equal = {}
24+
for pair in tag_values.split(","):
25+
if "=" in pair:
26+
tag_name, tag_value = pair.split("=", 1)
27+
event_tag_equal[tag_name.strip()] = tag_value.strip()
28+
29+
p = await controllers.event.filter_user_events(
30+
user_id, project_id, has_event_tag, event_tag_equal, topk
31+
)
32+
33+
return p.to_response(res.UserEventsDataResponse)
34+
35+
936
async def get_user_events(
1037
request: Request,
1138
user_id: UUID = Path(..., description="The ID of the user"),
@@ -66,10 +93,19 @@ async def search_user_events(
6693
use_gists: bool = Query(
6794
True, description="Whether to search event gists (default) or event tip"
6895
),
69-
) -> res.UserEventGistsDataResponse |res.UserEventsDataResponse:
96+
use_tag: bool = Query(
97+
False, description="Whether to search by tags instead of query"
98+
),
99+
tags: str = Query(None, description="Comma-separated list of tag names that events must have (e.g.'emotion,romance')"),
100+
tag_values: str = Query(None, description="Comma-separated tag=value pairs for exact matches (e.g., 'emotion=happy,topic=work')"),
101+
) -> res.UserEventGistsDataResponse | res.UserEventsDataResponse:
70102
project_id = request.state.memobase_project_id
71103

72-
if use_gists:
104+
if use_tag:
105+
return await _search_user_events_by_tags_internal(
106+
user_id, project_id, tags, tag_values, topk
107+
)
108+
elif use_gists:
73109
p = await controllers.event_gist.search_user_event_gists(
74110
user_id, project_id, query, topk, similarity_threshold, time_range_in_days
75111
)
@@ -108,21 +144,6 @@ async def search_user_events_by_tags(
108144
topk: int = Query(10, description="Number of events to retrieve, default is 10"),
109145
) -> res.UserEventsDataResponse:
110146
project_id = request.state.memobase_project_id
111-
112-
has_event_tag = None
113-
if tags:
114-
has_event_tag = [tag.strip() for tag in tags.split(",") if tag.strip()]
115-
116-
event_tag_equal = None
117-
if tag_values:
118-
event_tag_equal = {}
119-
for pair in tag_values.split(","):
120-
if "=" in pair:
121-
tag_name, tag_value = pair.split("=", 1)
122-
event_tag_equal[tag_name.strip()] = tag_value.strip()
123-
124-
p = await controllers.event.filter_user_events(
125-
user_id, project_id, has_event_tag, event_tag_equal, topk
147+
return await _search_user_events_by_tags_internal(
148+
user_id, project_id, tags, tag_values, topk
126149
)
127-
128-
return p.to_response(res.UserEventsDataResponse)

0 commit comments

Comments
 (0)