Repository navigation
Expand file tree
/
Copy pathllm.py
More file actions
393 lines (324 loc) · 14.8 KB
/
Copy pathllm.py
File metadata and controls
393 lines (324 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
"""LLM gateway SDK service (sync).
Drop-in compatible with each provider's official SDK call shape (with
one deviation: passport per-call as a second arg, because STACK
passport TTLs are 15 min default and would silently invalidate
mid-session in a constructor-time pattern).
Usage::
from getstack import Stack
stack = Stack(api_key="sk_live_...")
mission = stack.missions.open("Answer support tickets", agent_id="agt_x")
session = stack.issue_passport(agent_id="agt_x", mission_ids=[mission["mission_id"]])
# non-streaming
response = stack.llm.anthropic.messages.create(
body={
"model": "claude-opus-4-7",
"messages": [{"role": "user", "content": "hi"}],
"max_tokens": 100,
},
passport=session,
)
# streaming
for event in stack.llm.anthropic.messages.create(
body={...},
passport=session,
stream=True,
):
print(event)
# observability
rows = stack.llm.usage.list(limit=10)
buckets = stack.llm.usage.summary(since="2026-05-01T00:00:00Z")
Wire shapes mirror packages/shared/src/schemas/llm/usage.ts. Types
stay loose (dicts) — same convention as missions.py.
"""
from __future__ import annotations
import json
from collections.abc import Iterator
from typing import Any, Union, cast
from ._authority_request import authority_request_headers
from .client import HttpClient
from .passport_session import PassportSession
PassportInput = Union[str, PassportSession]
def _resolve_passport(p: PassportInput) -> str:
if isinstance(p, str):
return p
return p.get_token()
_METHODS = ("GET", "POST", "PUT", "PATCH", "DELETE")
def openrouter_path(method: str, path: str) -> tuple[str, str]:
"""The method (upper-cased) and the gateway's path for one of OpenRouter's; refused here when it is not one."""
verb = method.upper()
if verb not in _METHODS:
raise ValueError(f"method is one of {', '.join(_METHODS)}")
if not path.startswith("/") or path.startswith("//"):
raise ValueError("An OpenRouter path starts with one slash, such as /chat/completions.")
return verb, f"/v1/llm/openrouter{path}"
_MAX_COST_CENTS_MIN = 1
def max_cost_header(max_cost_cents: int | None) -> dict[str, str]:
"""``X-Max-Cost-Cents``: a cap on the call's worst case, whole cents, 1 or more. Refused client-side otherwise."""
if max_cost_cents is None:
return {}
if isinstance(max_cost_cents, bool) or not isinstance(max_cost_cents, int) or max_cost_cents < _MAX_COST_CENTS_MIN:
raise ValueError(f"max_cost_cents is a whole number of cents, {_MAX_COST_CENTS_MIN} or more")
return {"X-Max-Cost-Cents": str(max_cost_cents)}
def _build_headers(
passport: PassportInput,
intent_approval_id: str | None = None,
authority_request_id: str | None = None,
max_cost_cents: int | None = None,
) -> dict[str, str]:
headers = {"X-Passport-Token": _resolve_passport(passport)}
if intent_approval_id:
headers["X-Intent-Approval-Id"] = intent_approval_id
headers.update(authority_request_headers(authority_request_id))
headers.update(max_cost_header(max_cost_cents))
return headers
def _iterate_sse(response: Any) -> Iterator[dict[str, Any]]:
"""Parse an httpx streaming Response's iter_lines into SSE events.
Yields each `data: { ... }` JSON payload. Skips `event:` / `id:` /
blank / comment lines. Terminates on `[DONE]` (OpenAI) or stream end.
"""
for line in response.iter_lines():
if not line:
continue
# httpx returns str (text); strip the SSE `data: ` prefix.
if not line.startswith("data: "):
continue
payload = line[6:].strip()
if payload == "[DONE]" or payload == "":
continue
try:
yield json.loads(payload)
except json.JSONDecodeError:
continue
# ── Provider sub-services ────────────────────────────────────────────
class _Images:
"""An image on your own OpenAI key: ``POST /v1/llm/openai/images``.
The call goes through the same gates as a chat call (Passport, Mission, scope, your controls); STACK counts one
action and OpenAI bills you. The answer is files saved to the agent's drive (never the bytes), marked
``untrusted``. The body takes ``model``, ``prompt``, ``n``, ``size`` and ``quality``.
"""
def __init__(self, client: HttpClient):
self._client = client
self._path = "/v1/llm/openai/images"
def generate(
self,
body: dict[str, Any],
*,
passport: PassportInput,
intent_approval_id: str | None = None,
authority_request_id: str | None = None,
) -> Any:
headers = _build_headers(passport, intent_approval_id, authority_request_id)
return self._client.post(self._path, json=body, extra_headers=headers)
class _AnthropicMessages:
def __init__(self, client: HttpClient):
self._client = client
def create(
self,
body: dict[str, Any],
*,
passport: PassportInput,
stream: bool | None = None,
intent_approval_id: str | None = None,
authority_request_id: str | None = None,
max_cost_cents: int | None = None,
) -> Any:
"""If body['stream'] is True OR stream=True, returns an iterator
of parsed SSE events. Otherwise returns the JSON response body.
"""
effective_stream = bool(stream if stream is not None else body.get("stream"))
if effective_stream:
body = {**body, "stream": True}
headers = _build_headers(passport, intent_approval_id, authority_request_id, max_cost_cents)
return _stream_provider_call(self._client, "/v1/llm/anthropic/messages", body, headers)
headers = _build_headers(passport, intent_approval_id, authority_request_id, max_cost_cents)
return self._client.post("/v1/llm/anthropic/messages", json=body, extra_headers=headers)
class _AnthropicProvider:
def __init__(self, client: HttpClient):
self.messages = _AnthropicMessages(client)
class _OpenAIChatCompletions:
def __init__(self, client: HttpClient):
self._client = client
def create(
self,
body: dict[str, Any],
*,
passport: PassportInput,
stream: bool | None = None,
intent_approval_id: str | None = None,
authority_request_id: str | None = None,
max_cost_cents: int | None = None,
) -> Any:
effective_stream = bool(stream if stream is not None else body.get("stream"))
if effective_stream:
body = {**body, "stream": True}
headers = _build_headers(passport, intent_approval_id, authority_request_id, max_cost_cents)
return _stream_provider_call(self._client, "/v1/llm/openai/chat/completions", body, headers)
headers = _build_headers(passport, intent_approval_id, authority_request_id, max_cost_cents)
return self._client.post("/v1/llm/openai/chat/completions", json=body, extra_headers=headers)
class _OpenAIChat:
def __init__(self, client: HttpClient):
self.completions = _OpenAIChatCompletions(client)
class _OpenAIProvider:
def __init__(self, client: HttpClient):
self.chat = _OpenAIChat(client)
self.images = _Images(client)
class _OpenRouterChatCompletions:
def __init__(self, client: HttpClient):
self._client = client
def create(
self,
body: dict[str, Any],
*,
passport: PassportInput,
stream: bool | None = None,
intent_approval_id: str | None = None,
authority_request_id: str | None = None,
max_cost_cents: int | None = None,
) -> Any:
effective_stream = bool(stream if stream is not None else body.get("stream"))
if effective_stream:
body = {**body, "stream": True}
headers = _build_headers(passport, intent_approval_id, authority_request_id, max_cost_cents)
return _stream_provider_call(self._client, "/v1/llm/openrouter/chat/completions", body, headers)
headers = _build_headers(passport, intent_approval_id, authority_request_id, max_cost_cents)
return self._client.post("/v1/llm/openrouter/chat/completions", json=body, extra_headers=headers)
class _OpenRouterChat:
def __init__(self, client: HttpClient):
self.completions = _OpenRouterChatCompletions(client)
class _OpenRouterProvider:
def __init__(self, client: HttpClient):
self._client = client
self.chat = _OpenRouterChat(client)
def request(
self,
method: str,
path: str,
*,
passport: PassportInput,
body: dict[str, Any] | None = None,
stream: bool | None = None,
intent_approval_id: str | None = None,
authority_request_id: str | None = None,
max_cost_cents: int | None = None,
) -> Any:
"""Any call to OpenRouter's API: any method, any path below it (``/chat/completions``, ``/images``,
``/embeddings``, ``/videos/<job id>``, ``/models?output_modalities=all``), with OpenRouter's own body and the
answer as OpenRouter sent it. On STACK's key (an agent account's) the call holds its worst case first
(``max_cost_cents`` lowers the hold to it; a cost above the hold is taken from the account's credit at once)
and is charged what OpenRouter reports it cost.
A JSON answer is returned parsed. If ``body["stream"]`` is True OR ``stream=True`` on a POST, returns an
iterator of parsed SSE events, as ``chat.completions.create`` does. An answer that is a file is read with
``request_bytes``.
"""
verb, target = openrouter_path(method, path)
headers = _build_headers(passport, intent_approval_id, authority_request_id, max_cost_cents)
if verb == "POST" and bool(stream if stream is not None else (body or {}).get("stream")):
return _stream_provider_call(self._client, target, {**(body or {}), "stream": True}, headers)
return self._client.request(verb, target, json=body, extra_headers=headers)
def request_bytes(
self,
method: str,
path: str,
*,
passport: PassportInput,
body: dict[str, Any] | None = None,
intent_approval_id: str | None = None,
authority_request_id: str | None = None,
max_cost_cents: int | None = None,
) -> dict[str, Any]:
"""The same call for an answer that is not JSON (a speech file, a picture, a finished video at
``/videos/<job id>/content``): ``{"status", "content_type", "content"}`` with the bytes in ``content``."""
verb, target = openrouter_path(method, path)
headers = _build_headers(passport, intent_approval_id, authority_request_id, max_cost_cents)
status, content_type, content = self._client.request_raw(verb, target, json=body, extra_headers=headers)
return {"status": status, "content_type": content_type, "content": content}
# ── Usage query sub-service ──────────────────────────────────────────
class _LlmUsageQuery:
def __init__(self, client: HttpClient):
self._client = client
def list(
self,
*,
limit: int | None = None,
cursor: str | None = None,
provider: str | None = None,
passport_jti: str | None = None,
mission_id: str | None = None,
agent_id: str | None = None,
since: str | None = None,
until: str | None = None,
) -> dict[str, Any]:
params: dict[str, Any] = {}
if limit is not None:
params["limit"] = limit
if cursor is not None:
params["cursor"] = cursor
if provider is not None:
params["provider"] = provider
if passport_jti is not None:
params["passport_jti"] = passport_jti
if mission_id is not None:
params["mission_id"] = mission_id
if agent_id is not None:
params["agent_id"] = agent_id
if since is not None:
params["since"] = since
if until is not None:
params["until"] = until
return cast("dict[str, Any]", self._client.get("/v1/llm/usage", params=params))
def summary(
self,
*,
provider: str | None = None,
passport_jti: str | None = None,
mission_id: str | None = None,
agent_id: str | None = None,
since: str | None = None,
until: str | None = None,
group_by_model: bool | None = None,
) -> dict[str, Any]:
params: dict[str, Any] = {}
if provider is not None:
params["provider"] = provider
if passport_jti is not None:
params["passport_jti"] = passport_jti
if mission_id is not None:
params["mission_id"] = mission_id
if agent_id is not None:
params["agent_id"] = agent_id
if since is not None:
params["since"] = since
if until is not None:
params["until"] = until
if group_by_model is not None:
params["group_by_model"] = str(group_by_model).lower()
return cast("dict[str, Any]", self._client.get("/v1/llm/usage/summary", params=params))
# ── Top-level service ────────────────────────────────────────────────
class LlmService:
def __init__(self, client: HttpClient):
self.anthropic = _AnthropicProvider(client)
self.openai = _OpenAIProvider(client)
self.openrouter = _OpenRouterProvider(client)
self.usage = _LlmUsageQuery(client)
# ── Streaming helper ─────────────────────────────────────────────────
def _stream_provider_call(
client: HttpClient,
path: str,
body: dict[str, Any],
headers: dict[str, str],
) -> Iterator[dict[str, Any]]:
"""Open a streaming connection and yield parsed SSE events. The httpx
response context manager is held open across the yield — caller MUST
fully drain the iterator (or `break` early; the GC closes the
connection). Keeps the streaming UX clean: `for event in
stack.llm.anthropic.messages.create(..., stream=True): ...`.
"""
with client.stream_post(path, json=body, extra_headers=headers) as response:
if response.status_code >= 400:
# Read the error body before yielding (no chunks to emit).
try:
body_text = response.read().decode("utf-8")
except Exception:
body_text = response.reason_phrase
raise RuntimeError(f"LLM streaming call failed: {response.status_code} {body_text}")
yield from _iterate_sse(response)