Skip to content

Commit aadd24a

Browse files
authored
Merge pull request #1060 from jupyter-naas/refactor-chatbar
refactor: comprehensive chatbar and agent model resolution overhaul
2 parents 11c6a32 + 8f7ca1b commit aadd24a

17 files changed

Lines changed: 528 additions & 328 deletions

File tree

libs/naas-abi-cli/uv.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/naas-abi-core/naas_abi_core/services/model_registry/AGENTS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ get(canonical_id, provider=None) -> Model
4343
get_chat_model(canonical_id, provider=None) -> ChatModel # raises if wrong type
4444
get_embedding_model(canonical_id, provider=None) -> EmbeddingModel # raises if wrong type
4545

46+
default_chat_model_id -> str | None # configured default canonical id (property, never raises)
47+
default_embedding_model_id -> str | None # configured default canonical id (property, never raises)
48+
4649
get_default_chat_model() -> ChatModel
4750
get_default_embedding_model() -> EmbeddingModel
4851
validate_defaults() # called at engine startup

libs/naas-abi-core/naas_abi_core/services/model_registry/ModelRegistryPort.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,22 @@ def get_embedding_model(
131131

132132
# ------------------------------------------------------------------ defaults
133133

134+
@property
135+
def default_chat_model_id(self) -> Optional[str]:
136+
"""Return the canonical id configured as the engine default chat model.
137+
138+
Unlike ``get_default_chat_model`` this never builds the live model and
139+
never raises: it exposes the raw configured canonical id (or ``None``
140+
when no default is configured), which callers can use for display or to
141+
cross-reference the model catalog."""
142+
raise NotImplementedError
143+
144+
@property
145+
def default_embedding_model_id(self) -> Optional[str]:
146+
"""Return the canonical id configured as the engine default embedding
147+
model, or ``None`` when no default is configured. Never raises."""
148+
raise NotImplementedError
149+
134150
def get_default_chat_model(self) -> ChatModel:
135151
"""Return the chat model configured as the engine default.
136152

libs/naas-abi-core/naas_abi_core/services/model_registry/ModelRegistryService.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,14 @@ def _build_off_catalog_embedding(
231231

232232
# ------------------------------------------------------------------ defaults
233233

234+
@property
235+
def default_chat_model_id(self) -> Optional[str]:
236+
return self._default_chat_model
237+
238+
@property
239+
def default_embedding_model_id(self) -> Optional[str]:
240+
return self._default_embedding_model
241+
234242
def get_default_chat_model(self) -> ChatModel:
235243
if self._default_chat_model is None:
236244
raise DefaultModelNotResolvedError(

libs/naas-abi-marketplace/naas_abi_marketplace/applications/openrouter/agents/OpenRouterAgent.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
class OpenRouterAgent(IntentAgent):
1515
name: str = "OpenRouter"
1616
description: str = "Helps you interact with OpenRouter for accessing multiple AI models."
17+
# Canonical model id this agent runs on (single source of truth): ``New``
18+
# builds its chat_model from it and the API/UI reads it via get_chat_model_id.
19+
MODEL_ID: str = "openrouter/free"
1720
logo_url: str = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSMyMkLa1_OdyK9b4LZTiDiR7W5SkPRtydKKw&s"
1821
suggestions: list = []
1922
system_prompt: str = """<role>
@@ -51,6 +54,10 @@ class OpenRouterAgent(IntentAgent):
5154
</constraints>
5255
"""
5356

57+
@classmethod
58+
def get_chat_model_id(cls) -> Optional[str]:
59+
return cls.MODEL_ID
60+
5461
@classmethod
5562
def New(
5663
cls,
@@ -71,7 +78,7 @@ def New(
7178
api_key = module.configuration.openrouter_api_key
7279
object_storage = module.engine.services.object_storage
7380

74-
chat_model = OpenRouterModel(api_key=api_key).get_model("openrouter/free")
81+
chat_model = OpenRouterModel(api_key=api_key).get_model(cls.MODEL_ID)
7582

7683
registry = get_default_model_registry()
7784
assert registry is not None, "ModelRegistryService not initialized"

libs/naas-abi-marketplace/naas_abi_marketplace/applications/openrouter/agents/OpenRouterAgents.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818
ASSETS_DIR = Path(__file__).parent.parent / "assets" / "public"
1919

2020

21+
def _declared_model_id(cls) -> Optional[str]:
22+
"""Canonical model id a dynamically-built OpenRouter agent runs on.
23+
24+
Bound as the ``get_chat_model_id`` classmethod on every generated agent so
25+
the API/UI can surface the model without instantiating the agent. Mirrors
26+
the ``MODEL_ID`` attribute, which ``New`` uses to build the chat model."""
27+
return cls.MODEL_ID
28+
29+
2130
class OpenRouterAgents:
2231
def __init__(
2332
self,
@@ -61,8 +70,9 @@ def create_agent(
6170
agent_shared_state: Optional[AgentSharedState] = None,
6271
agent_configuration: Optional[AgentConfiguration] = None,
6372
) -> Agent:
64-
# Create ChatOpenRouter model instance
65-
chat_model = self.openrouter_model.get_model(m_id)
73+
# Build the chat model from the class-declared MODEL_ID so the
74+
# model stays the single source of truth (matches get_chat_model_id).
75+
chat_model = self.openrouter_model.get_model(cls.MODEL_ID)
6676

6777
# Use provided configuration or create default one
6878
if agent_configuration is None:
@@ -145,6 +155,11 @@ def create_agent(
145155
"name": model_name,
146156
"description": model_description,
147157
"logo_url": model_logo_url,
158+
# Canonical model id this agent runs on (single source of truth):
159+
# ``New`` builds its chat_model from it and the API/UI reads it
160+
# via ``get_chat_model_id``.
161+
"MODEL_ID": model_id,
162+
"get_chat_model_id": classmethod(_declared_model_id),
148163
"New": classmethod(create_agent_func),
149164
"__module__": OPENROUTER_AGENT_MODULE + "." + class_name,
150165
},

libs/naas-abi-marketplace/naas_abi_marketplace/applications/openrouter/models/OpenRouterModel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def __init__(self, api_key: str):
77
self.api_key = api_key
88
self.base_url = "https://openrouter.ai/api/v1"
99

10-
def get_model(self, model_id: str):
10+
def get_model(self, model_id: str) -> ChatOpenAI:
1111
return ChatOpenAI(
1212
model=model_id,
1313
api_key=SecretStr(self.api_key),

0 commit comments

Comments
 (0)