Skip to content

Commit 2575571

Browse files
author
sagitchu
committed
fix: macth gem real id
1 parent 2e6395c commit 2575571

1 file changed

Lines changed: 30 additions & 17 deletions

File tree

app/services/client.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,19 @@ class GeminiClientWrapper(GeminiClient):
4444
def __init__(self, client_id: str, **kwargs):
4545
super().__init__(**kwargs)
4646
self.id = client_id
47+
self.gem_id_mapping: dict[str, str] = {} # config_id -> real_gem_id
4748

4849
def start_chat(self, **kwargs) -> Any:
4950
model = kwargs.get("model")
51+
52+
# Intercept 'gem' argument and map it to real ID
53+
if "gem" in kwargs:
54+
gem_config_id = kwargs["gem"]
55+
if gem_config_id in self.gem_id_mapping:
56+
real_id = self.gem_id_mapping[gem_config_id]
57+
logger.debug(f"Mapping gem config ID '{gem_config_id}' to real ID '{real_id}'")
58+
kwargs["gem"] = real_id
59+
5060
logger.info(f"[DEBUG_GEM] GeminiClientWrapper.start_chat intercepted. model={model}, kwargs={kwargs}")
5161
return super().start_chat(**kwargs)
5262

@@ -90,32 +100,35 @@ async def sync_gems(self) -> None:
90100

91101
try:
92102
gem_jar = await self.fetch_gems()
93-
# GemJar behaves like a dict-like object where keys are ID and values are Gem objects
94-
# Or it might be a list. Let's assume we can iterate it.
95-
# To be safe against unknown structure, we'll try to inspect one if possible or just rely on 'title' attribute.
96-
97-
# Since we can't easily debug, let's look at the probe output again.
98-
# It has 'gems' attribute on client too. Maybe that's cached?
99-
100-
existing_titles = set()
103+
104+
# Map existing gems: Name (Title) -> Real ID
105+
existing_gems_map = {}
101106
if gem_jar:
102-
# Assuming gem_jar is iterable yielding Gem objects
103107
for g in gem_jar:
104-
# Try to find the name/title attribute
105-
title = getattr(g, "title", getattr(g, "name", None))
106-
if title:
107-
existing_titles.add(title)
108+
# Gem object has 'name' (title) and 'id' (real id)
109+
name = getattr(g, "name", None)
110+
real_id = getattr(g, "id", None)
111+
if name and real_id:
112+
existing_gems_map[name] = real_id
108113

114+
# Update internal mapping
109115
for gem_def in g_config.gemini.gems:
110-
if gem_def.id not in existing_titles:
116+
if gem_def.id in existing_gems_map:
117+
real_id = existing_gems_map[gem_def.id]
118+
self.gem_id_mapping[gem_def.id] = real_id
119+
logger.debug(f"Gem '{gem_def.id}' found with ID: {real_id}")
120+
else:
111121
logger.info(f"Creating missing gem for client {self.id}: {gem_def.id}")
112-
await self.create_gem(
122+
new_gem = await self.create_gem(
113123
name=gem_def.id,
114124
prompt=gem_def.system_prompt or "",
115125
description=f"Auto-generated gem for {gem_def.id}",
116126
)
117-
else:
118-
logger.debug(f"Gem already exists: {gem_def.id}")
127+
if new_gem and hasattr(new_gem, "id"):
128+
self.gem_id_mapping[gem_def.id] = new_gem.id
129+
logger.info(f"Created gem '{gem_def.id}' with new ID: {new_gem.id}")
130+
else:
131+
logger.warning(f"Created gem '{gem_def.id}' but could not retrieve its ID.")
119132

120133
except Exception as e:
121134
logger.error(f"Failed to sync gems for client {self.id}: {e}")

0 commit comments

Comments
 (0)