MCPcopy Create free account
hub / github.com/Roy3838/Observer / handle_request

Method handle_request

api/openrouter_handler.py:111–247  ·  view source on GitHub ↗

Process a /v1/chat/completions request asynchronously via OpenRouter. Translates display model name to actual OpenRouter model ID. Returns either dict (non-streaming) or StreamingResponse (streaming).

(self, request_data: dict)

Source from the content-addressed store, hash-verified

109 }
110
111 async def handle_request(self, request_data: dict):
112 """
113 Process a /v1/chat/completions request asynchronously via OpenRouter.
114 Translates display model name to actual OpenRouter model ID.
115 Returns either dict (non-streaming) or StreamingResponse (streaming).
116 """
117 if not self.api_key:
118 raise ConfigError("OPENROUTER_API_KEY is not configured on the server.")
119
120 # --- Get Display Name and Translate to Actual Model ID ---
121 display_model_name = request_data.get("model")
122 if not display_model_name:
123 raise ValueError("Request data must include a 'model' field (using the display name).")
124
125 # --- Special Case: NULL Model ---
126 if display_model_name == "NULL":
127 logger.info("NULL model requested, returning empty response without API call")
128
129 if request_data.get("stream", False):
130 # Return streaming response for NULL
131 return StreamingResponse(
132 self._generate_null_stream(),
133 media_type="text/event-stream"
134 )
135 else:
136 # Return non-streaming response for NULL
137 return {
138 "id": f"chatcmpl-null-{int(time.time())}",
139 "object": "chat.completion",
140 "created": int(time.time()),
141 "model": "NULL",
142 "choices": [{
143 "index": 0,
144 "message": {
145 "role": "assistant",
146 "content": " "
147 },
148 "finish_reason": "stop"
149 }],
150 "usage": {
151 "prompt_tokens": 0,
152 "completion_tokens": 1,
153 "total_tokens": 1
154 }
155 }
156 # --- End Special Case ---
157
158 # Look up the display name in our map
159 model_info = self.model_map.get(display_model_name)
160 if not model_info:
161 # If the display name isn't found, the model is unsupported by this mapping
162 logger.warning(f"Received request for unmapped OpenRouter model display name: {display_model_name}")
163 raise ValueError(f"Model display name '{display_model_name}' is not recognized or supported.")
164
165 actual_model_id = model_info.get("model_id")
166 if not actual_model_id:
167 # Should not happen if map is defined correctly, but good practice to check
168 logger.error(f"Internal configuration error: Missing 'model_id' for display name '{display_model_name}' in model_map.")

Callers

nothing calls this directly

Calls 8

_generate_null_streamMethod · 0.95
ConfigErrorClass · 0.90
BackendAPIErrorClass · 0.90
HandlerErrorClass · 0.90
get_http_clientFunction · 0.85
infoMethod · 0.80
errorMethod · 0.80

Tested by

no test coverage detected