Skip to content

Commit e41ec8f

Browse files
committed
refactor: streamline URL properties in Pipe class and clean up test setup
1 parent 22f3ba2 commit e41ec8f

2 files changed

Lines changed: 11 additions & 23 deletions

File tree

openrouter_pipe.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import os
1818
import re
1919
import traceback
20-
from typing import Generator, Iterator, List, Optional, Union
20+
from typing import Generator, List, Optional, Union
2121

2222
import requests
2323
from 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\nOpenRouter 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"

test_pipe.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,6 @@ def _make_sse_response(chunks: List[bytes]):
519519

520520
pipe = Pipe()
521521
pipe.valves = Pipe.Valves(OPENROUTER_API_KEY="k", MAX_RETRIES=2, REQUEST_TIMEOUT=5)
522-
pipe.chat_url = "https://openrouter.ai/api/v1/chat/completions"
523522

524523
# 13a. Success on first try
525524
mock_ok = MagicMock()

0 commit comments

Comments
 (0)