1717import os
1818import re
1919import traceback
20- from typing import Generator , Iterator , List , Optional , Union
20+ from typing import Generator , List , Optional , Union
2121
2222import requests
2323from pydantic import BaseModel , Field
@@ -143,12 +143,17 @@ class Valves(BaseModel):
143143 def __init__ (self ) -> None :
144144 self .type = "manifold"
145145 self .valves = self .Valves ()
146- base = self .valves .OPENROUTER_BASE_URL .rstrip ("/" )
147- self .models_url = f"{ base } /models"
148- self .chat_url = f"{ base } /chat/completions"
149146 if not self .valves .OPENROUTER_API_KEY :
150147 print ("[OpenRouter Pipe] Warning: OPENROUTER_API_KEY not set" )
151148
149+ @property
150+ def models_url (self ) -> str :
151+ return f"{ self .valves .OPENROUTER_BASE_URL .rstrip ('/' )} /models"
152+
153+ @property
154+ def chat_url (self ) -> str :
155+ return f"{ self .valves .OPENROUTER_BASE_URL .rstrip ('/' )} /chat/completions"
156+
152157 def pipes (self ) -> List [dict ]:
153158 if not self .valves .OPENROUTER_API_KEY :
154159 return [{"id" : "error" , "name" : "OpenRouter API key not configured" }]
@@ -413,7 +418,6 @@ def _stream_response(
413418 self , headers : dict , payload : dict
414419 ) -> Generator [str , None , None ]:
415420 response = None
416- buffer = ""
417421 in_think = False
418422 latest_citations : List [str ] = []
419423 try :
@@ -438,14 +442,8 @@ def _stream_response(
438442 else str (err )
439443 )
440444 if in_think :
441- if buffer :
442- yield _insert_citations (buffer , latest_citations )
443- buffer = ""
444445 yield "\n </think>\n "
445446 in_think = False
446- elif buffer :
447- yield _insert_citations (buffer , latest_citations )
448- buffer = ""
449447 yield f"\n \n OpenRouter Pipe Error: { msg } "
450448 return
451449
@@ -459,25 +457,16 @@ def _stream_response(
459457
460458 if reasoning :
461459 if not in_think :
462- if buffer :
463- yield _insert_citations (buffer , latest_citations )
464- buffer = ""
465460 yield "<think>\n "
466461 in_think = True
467- buffer += reasoning
462+ yield _insert_citations ( reasoning , latest_citations )
468463
469464 if content :
470465 if in_think :
471- if buffer :
472- yield _insert_citations (buffer , latest_citations )
473- buffer = ""
474466 yield "\n </think>\n "
475467 in_think = False
476- buffer += content
468+ yield _insert_citations ( content , latest_citations )
477469
478- # Flush remaining buffer
479- if buffer :
480- yield _insert_citations (buffer , latest_citations )
481470 # Close <think> if still open
482471 if in_think :
483472 yield "\n </think>\n "
0 commit comments