loquent 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- loquent/__init__.py +20 -0
- loquent/__main__.py +5 -0
- loquent/adapters/__init__.py +4 -0
- loquent/adapters/agui_http.py +160 -0
- loquent/adapters/base.py +23 -0
- loquent/adapters/function.py +140 -0
- loquent/adapters/openai_compat.py +226 -0
- loquent/adapters/resolve.py +33 -0
- loquent/app.py +149 -0
- loquent/auth.py +163 -0
- loquent/cli.py +180 -0
- loquent/context.py +72 -0
- loquent/files.py +89 -0
- loquent/protocol.py +127 -0
- loquent/runner.py +420 -0
- loquent/server/__init__.py +5 -0
- loquent/server/app.py +118 -0
- loquent/server/deps.py +167 -0
- loquent/server/routes_auth.py +80 -0
- loquent/server/routes_files.py +234 -0
- loquent/server/routes_meta.py +48 -0
- loquent/server/routes_runs.py +339 -0
- loquent/server/routes_threads.py +109 -0
- loquent/server/security.py +44 -0
- loquent/server/static/assets/KaTeX_AMS-Regular-BQhdFMY1.woff2 +0 -0
- loquent/server/static/assets/KaTeX_AMS-Regular-DMm9YOAa.woff +0 -0
- loquent/server/static/assets/KaTeX_AMS-Regular-DRggAlZN.ttf +0 -0
- loquent/server/static/assets/KaTeX_Caligraphic-Bold-ATXxdsX0.ttf +0 -0
- loquent/server/static/assets/KaTeX_Caligraphic-Bold-BEiXGLvX.woff +0 -0
- loquent/server/static/assets/KaTeX_Caligraphic-Bold-Dq_IR9rO.woff2 +0 -0
- loquent/server/static/assets/KaTeX_Caligraphic-Regular-CTRA-rTL.woff +0 -0
- loquent/server/static/assets/KaTeX_Caligraphic-Regular-Di6jR-x-.woff2 +0 -0
- loquent/server/static/assets/KaTeX_Caligraphic-Regular-wX97UBjC.ttf +0 -0
- loquent/server/static/assets/KaTeX_Fraktur-Bold-BdnERNNW.ttf +0 -0
- loquent/server/static/assets/KaTeX_Fraktur-Bold-BsDP51OF.woff +0 -0
- loquent/server/static/assets/KaTeX_Fraktur-Bold-CL6g_b3V.woff2 +0 -0
- loquent/server/static/assets/KaTeX_Fraktur-Regular-CB_wures.ttf +0 -0
- loquent/server/static/assets/KaTeX_Fraktur-Regular-CTYiF6lA.woff2 +0 -0
- loquent/server/static/assets/KaTeX_Fraktur-Regular-Dxdc4cR9.woff +0 -0
- loquent/server/static/assets/KaTeX_Main-Bold-Cx986IdX.woff2 +0 -0
- loquent/server/static/assets/KaTeX_Main-Bold-Jm3AIy58.woff +0 -0
- loquent/server/static/assets/KaTeX_Main-Bold-waoOVXN0.ttf +0 -0
- loquent/server/static/assets/KaTeX_Main-BoldItalic-DxDJ3AOS.woff2 +0 -0
- loquent/server/static/assets/KaTeX_Main-BoldItalic-DzxPMmG6.ttf +0 -0
- loquent/server/static/assets/KaTeX_Main-BoldItalic-SpSLRI95.woff +0 -0
- loquent/server/static/assets/KaTeX_Main-Italic-3WenGoN9.ttf +0 -0
- loquent/server/static/assets/KaTeX_Main-Italic-BMLOBm91.woff +0 -0
- loquent/server/static/assets/KaTeX_Main-Italic-NWA7e6Wa.woff2 +0 -0
- loquent/server/static/assets/KaTeX_Main-Regular-B22Nviop.woff2 +0 -0
- loquent/server/static/assets/KaTeX_Main-Regular-Dr94JaBh.woff +0 -0
- loquent/server/static/assets/KaTeX_Main-Regular-ypZvNtVU.ttf +0 -0
- loquent/server/static/assets/KaTeX_Math-BoldItalic-B3XSjfu4.ttf +0 -0
- loquent/server/static/assets/KaTeX_Math-BoldItalic-CZnvNsCZ.woff2 +0 -0
- loquent/server/static/assets/KaTeX_Math-BoldItalic-iY-2wyZ7.woff +0 -0
- loquent/server/static/assets/KaTeX_Math-Italic-DA0__PXp.woff +0 -0
- loquent/server/static/assets/KaTeX_Math-Italic-flOr_0UB.ttf +0 -0
- loquent/server/static/assets/KaTeX_Math-Italic-t53AETM-.woff2 +0 -0
- loquent/server/static/assets/KaTeX_SansSerif-Bold-CFMepnvq.ttf +0 -0
- loquent/server/static/assets/KaTeX_SansSerif-Bold-D1sUS0GD.woff2 +0 -0
- loquent/server/static/assets/KaTeX_SansSerif-Bold-DbIhKOiC.woff +0 -0
- loquent/server/static/assets/KaTeX_SansSerif-Italic-C3H0VqGB.woff2 +0 -0
- loquent/server/static/assets/KaTeX_SansSerif-Italic-DN2j7dab.woff +0 -0
- loquent/server/static/assets/KaTeX_SansSerif-Italic-YYjJ1zSn.ttf +0 -0
- loquent/server/static/assets/KaTeX_SansSerif-Regular-BNo7hRIc.ttf +0 -0
- loquent/server/static/assets/KaTeX_SansSerif-Regular-CS6fqUqJ.woff +0 -0
- loquent/server/static/assets/KaTeX_SansSerif-Regular-DDBCnlJ7.woff2 +0 -0
- loquent/server/static/assets/KaTeX_Script-Regular-C5JkGWo-.ttf +0 -0
- loquent/server/static/assets/KaTeX_Script-Regular-D3wIWfF6.woff2 +0 -0
- loquent/server/static/assets/KaTeX_Script-Regular-D5yQViql.woff +0 -0
- loquent/server/static/assets/KaTeX_Size1-Regular-C195tn64.woff +0 -0
- loquent/server/static/assets/KaTeX_Size1-Regular-Dbsnue_I.ttf +0 -0
- loquent/server/static/assets/KaTeX_Size1-Regular-mCD8mA8B.woff2 +0 -0
- loquent/server/static/assets/KaTeX_Size2-Regular-B7gKUWhC.ttf +0 -0
- loquent/server/static/assets/KaTeX_Size2-Regular-Dy4dx90m.woff2 +0 -0
- loquent/server/static/assets/KaTeX_Size2-Regular-oD1tc_U0.woff +0 -0
- loquent/server/static/assets/KaTeX_Size3-Regular-CTq5MqoE.woff +0 -0
- loquent/server/static/assets/KaTeX_Size3-Regular-DgpXs0kz.ttf +0 -0
- loquent/server/static/assets/KaTeX_Size3-Regular-gV2CO0n9.woff2 +0 -0
- loquent/server/static/assets/KaTeX_Size4-Regular-BF-4gkZK.woff +0 -0
- loquent/server/static/assets/KaTeX_Size4-Regular-DWFBv043.ttf +0 -0
- loquent/server/static/assets/KaTeX_Size4-Regular-Dl5lxZxV.woff2 +0 -0
- loquent/server/static/assets/KaTeX_Typewriter-Regular-C0xS9mPB.woff +0 -0
- loquent/server/static/assets/KaTeX_Typewriter-Regular-CO6r4hn1.woff2 +0 -0
- loquent/server/static/assets/KaTeX_Typewriter-Regular-D3Ib7_Hf.ttf +0 -0
- loquent/server/static/assets/index-Cqcvqe01.js +308 -0
- loquent/server/static/assets/index-CuoIAv3p.css +1 -0
- loquent/server/static/index.html +20 -0
- loquent/server/static.py +94 -0
- loquent/settings.py +70 -0
- loquent/store/__init__.py +15 -0
- loquent/store/base.py +47 -0
- loquent/store/factory.py +19 -0
- loquent/store/models.py +56 -0
- loquent/store/mongo.py +262 -0
- loquent/store/sqlite.py +295 -0
- loquent/ui.py +313 -0
- loquent-0.1.0.dist-info/METADATA +772 -0
- loquent-0.1.0.dist-info/RECORD +101 -0
- loquent-0.1.0.dist-info/WHEEL +4 -0
- loquent-0.1.0.dist-info/entry_points.txt +2 -0
- loquent-0.1.0.dist-info/licenses/LICENSE +201 -0
loquent/__init__.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"""Loquent: serve a chat UI over AG-UI."""
|
|
2
|
+
|
|
3
|
+
from loquent import ui
|
|
4
|
+
from loquent.adapters.base import Adapter, AgentError
|
|
5
|
+
from loquent.app import App, serve
|
|
6
|
+
from loquent.context import FileRef, PublicUser, RunContext, WidgetAnswer
|
|
7
|
+
from loquent.settings import Settings
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"Adapter",
|
|
11
|
+
"AgentError",
|
|
12
|
+
"App",
|
|
13
|
+
"FileRef",
|
|
14
|
+
"PublicUser",
|
|
15
|
+
"RunContext",
|
|
16
|
+
"Settings",
|
|
17
|
+
"WidgetAnswer",
|
|
18
|
+
"serve",
|
|
19
|
+
"ui",
|
|
20
|
+
]
|
loquent/__main__.py
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"""Adapter that proxies to a remote AG-UI agent over HTTP."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from collections.abc import AsyncIterator
|
|
7
|
+
|
|
8
|
+
import httpx
|
|
9
|
+
from ag_ui.core import (
|
|
10
|
+
BaseEvent,
|
|
11
|
+
RunAgentInput,
|
|
12
|
+
TextMessageChunkEvent,
|
|
13
|
+
TextMessageContentEvent,
|
|
14
|
+
TextMessageEndEvent,
|
|
15
|
+
TextMessageStartEvent,
|
|
16
|
+
ToolCallArgsEvent,
|
|
17
|
+
ToolCallChunkEvent,
|
|
18
|
+
ToolCallEndEvent,
|
|
19
|
+
ToolCallStartEvent,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
from loquent.adapters.base import AgentError
|
|
23
|
+
from loquent.context import RunContext
|
|
24
|
+
from loquent.protocol import parse_event
|
|
25
|
+
|
|
26
|
+
_INVALID = "Upstream agent returned an invalid response"
|
|
27
|
+
_MAX_ERROR = 500
|
|
28
|
+
_LIFECYCLE = ("RUN_STARTED", "RUN_FINISHED")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _decode(data: str) -> BaseEvent:
|
|
32
|
+
try:
|
|
33
|
+
payload = json.loads(data)
|
|
34
|
+
if not isinstance(payload, dict):
|
|
35
|
+
raise AgentError(_INVALID)
|
|
36
|
+
return parse_event(payload)
|
|
37
|
+
except (ValueError, TypeError):
|
|
38
|
+
raise AgentError(_INVALID) from None
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class _ChunkExpander:
|
|
42
|
+
"""Expands AG-UI chunk events into START/CONTENT|ARGS/END, per the protocol's rules.
|
|
43
|
+
|
|
44
|
+
The first chunk for a new id opens a message (role defaults to assistant) or a tool call
|
|
45
|
+
(name and parent come from that first chunk); later chunks with the same id, or with no
|
|
46
|
+
id, append to it. A chunk with another id, any other event, or the end of the stream
|
|
47
|
+
closes it. Only one message or call is open at a time.
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
def __init__(self) -> None:
|
|
51
|
+
self._open: tuple[str, str] | None = None # ("text" | "tool", id)
|
|
52
|
+
|
|
53
|
+
def close(self) -> list[BaseEvent]:
|
|
54
|
+
if self._open is None:
|
|
55
|
+
return []
|
|
56
|
+
kind, id_ = self._open
|
|
57
|
+
self._open = None
|
|
58
|
+
if kind == "text":
|
|
59
|
+
return [TextMessageEndEvent(message_id=id_)]
|
|
60
|
+
return [ToolCallEndEvent(tool_call_id=id_)]
|
|
61
|
+
|
|
62
|
+
def _continue_or_open(self, kind: str, id_: str | None) -> tuple[list[BaseEvent], str, bool]:
|
|
63
|
+
"""Returns (events closing the previous one, the id to use, whether it is new)."""
|
|
64
|
+
if self._open is not None and self._open[0] == kind and id_ in (None, self._open[1]):
|
|
65
|
+
return [], self._open[1], False
|
|
66
|
+
if not id_:
|
|
67
|
+
raise AgentError(_INVALID) # nothing open to continue
|
|
68
|
+
return self.close(), id_, True
|
|
69
|
+
|
|
70
|
+
def feed(self, event: BaseEvent) -> list[BaseEvent]:
|
|
71
|
+
if isinstance(event, TextMessageChunkEvent):
|
|
72
|
+
out, mid, new = self._continue_or_open("text", event.message_id)
|
|
73
|
+
if new:
|
|
74
|
+
out.append(TextMessageStartEvent(message_id=mid, role=event.role or "assistant"))
|
|
75
|
+
self._open = ("text", mid)
|
|
76
|
+
if event.delta:
|
|
77
|
+
out.append(TextMessageContentEvent(message_id=mid, delta=event.delta))
|
|
78
|
+
return out
|
|
79
|
+
if not isinstance(event, ToolCallChunkEvent):
|
|
80
|
+
raise TypeError(f"not a chunk event: {event.type}")
|
|
81
|
+
out, cid, new = self._continue_or_open("tool", event.tool_call_id)
|
|
82
|
+
if new:
|
|
83
|
+
if not event.tool_call_name:
|
|
84
|
+
raise AgentError(_INVALID)
|
|
85
|
+
out.append(
|
|
86
|
+
ToolCallStartEvent(
|
|
87
|
+
tool_call_id=cid,
|
|
88
|
+
tool_call_name=event.tool_call_name,
|
|
89
|
+
parent_message_id=event.parent_message_id,
|
|
90
|
+
)
|
|
91
|
+
)
|
|
92
|
+
self._open = ("tool", cid)
|
|
93
|
+
if event.delta:
|
|
94
|
+
out.append(ToolCallArgsEvent(tool_call_id=cid, delta=event.delta))
|
|
95
|
+
return out
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
class AgUiHttpAdapter:
|
|
99
|
+
supports_widgets = True
|
|
100
|
+
supports_attachments = False
|
|
101
|
+
|
|
102
|
+
def __init__(
|
|
103
|
+
self,
|
|
104
|
+
url: str,
|
|
105
|
+
headers: dict[str, str] | None = None,
|
|
106
|
+
client: httpx.AsyncClient | None = None,
|
|
107
|
+
) -> None:
|
|
108
|
+
self._url = url
|
|
109
|
+
self._headers = dict(headers or {})
|
|
110
|
+
self._client = client or httpx.AsyncClient(timeout=httpx.Timeout(60, connect=10))
|
|
111
|
+
|
|
112
|
+
async def run(self, input: RunAgentInput, ctx: RunContext) -> AsyncIterator[BaseEvent]:
|
|
113
|
+
body = input.model_dump(by_alias=True, exclude_none=True, mode="json")
|
|
114
|
+
headers = {**self._headers, "Accept": "text/event-stream"}
|
|
115
|
+
chunks = _ChunkExpander()
|
|
116
|
+
seen_any = False # any content event, or RUN_FINISHED
|
|
117
|
+
try:
|
|
118
|
+
# 307/308 keep the POST and its body (e.g. FastAPI's /agent -> /agent/).
|
|
119
|
+
async with self._client.stream(
|
|
120
|
+
"POST", self._url, json=body, headers=headers, follow_redirects=True
|
|
121
|
+
) as resp:
|
|
122
|
+
if not 200 <= resp.status_code < 300:
|
|
123
|
+
raise AgentError(f"Upstream agent returned HTTP {resp.status_code}")
|
|
124
|
+
async for data in _sse_data(resp):
|
|
125
|
+
event = _decode(data)
|
|
126
|
+
kind = event.type.value
|
|
127
|
+
if kind == "RUN_ERROR":
|
|
128
|
+
raise AgentError(str(getattr(event, "message", ""))[:_MAX_ERROR])
|
|
129
|
+
if kind in ("TEXT_MESSAGE_CHUNK", "TOOL_CALL_CHUNK"):
|
|
130
|
+
out = chunks.feed(event)
|
|
131
|
+
else:
|
|
132
|
+
out = chunks.close()
|
|
133
|
+
if kind == "RUN_FINISHED":
|
|
134
|
+
seen_any = True
|
|
135
|
+
if kind not in _LIFECYCLE:
|
|
136
|
+
out.append(event)
|
|
137
|
+
for e in out:
|
|
138
|
+
seen_any = True
|
|
139
|
+
yield e
|
|
140
|
+
for e in chunks.close():
|
|
141
|
+
yield e
|
|
142
|
+
except (httpx.HTTPError, httpx.InvalidURL):
|
|
143
|
+
raise AgentError("Could not reach the agent endpoint") from None
|
|
144
|
+
if not seen_any:
|
|
145
|
+
raise AgentError(_INVALID)
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
async def _sse_data(resp: httpx.Response) -> AsyncIterator[str]:
|
|
149
|
+
"""The ``data`` payload of each SSE event (multi-line data joined with newlines)."""
|
|
150
|
+
data_lines: list[str] = []
|
|
151
|
+
async for line in resp.aiter_lines():
|
|
152
|
+
if line == "":
|
|
153
|
+
if data_lines:
|
|
154
|
+
yield "\n".join(data_lines)
|
|
155
|
+
data_lines = []
|
|
156
|
+
elif line.startswith("data:"):
|
|
157
|
+
data_lines.append(line[5:].removeprefix(" "))
|
|
158
|
+
# comments, id:, event:, retry: are ignored
|
|
159
|
+
if data_lines:
|
|
160
|
+
yield "\n".join(data_lines)
|
loquent/adapters/base.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""The Adapter protocol every backend implements."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import AsyncIterator
|
|
6
|
+
from typing import Protocol
|
|
7
|
+
|
|
8
|
+
from ag_ui.core import BaseEvent, RunAgentInput
|
|
9
|
+
|
|
10
|
+
from loquent.context import RunContext
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class AgentError(Exception):
|
|
14
|
+
"""An error whose message is safe to show to end users."""
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Adapter(Protocol):
|
|
18
|
+
"""Adapters yield content events only; the runner emits RUN_STARTED/FINISHED/ERROR."""
|
|
19
|
+
|
|
20
|
+
supports_widgets: bool
|
|
21
|
+
supports_attachments: bool
|
|
22
|
+
|
|
23
|
+
def run(self, input: RunAgentInput, ctx: RunContext) -> AsyncIterator[BaseEvent]: ...
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"""Adapter for plain Python functions."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import inspect
|
|
7
|
+
from collections.abc import AsyncIterator, Callable
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from ag_ui.core import (
|
|
11
|
+
BaseEvent,
|
|
12
|
+
RunAgentInput,
|
|
13
|
+
TextMessageContentEvent,
|
|
14
|
+
TextMessageEndEvent,
|
|
15
|
+
TextMessageStartEvent,
|
|
16
|
+
ToolCallArgsEvent,
|
|
17
|
+
ToolCallEndEvent,
|
|
18
|
+
ToolCallStartEvent,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
from loquent.context import RunContext
|
|
22
|
+
from loquent.protocol import new_id
|
|
23
|
+
from loquent.ui import WidgetRequest
|
|
24
|
+
|
|
25
|
+
_POSITIONAL = (inspect.Parameter.POSITIONAL_ONLY, inspect.Parameter.POSITIONAL_OR_KEYWORD)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _arity(fn: Callable) -> int:
|
|
29
|
+
try:
|
|
30
|
+
params = inspect.signature(fn).parameters.values()
|
|
31
|
+
except (TypeError, ValueError) as e:
|
|
32
|
+
raise TypeError(f"cannot inspect signature of {fn!r}") from e
|
|
33
|
+
n = sum(1 for p in params if p.kind in _POSITIONAL)
|
|
34
|
+
if n not in (1, 2):
|
|
35
|
+
raise TypeError(
|
|
36
|
+
f"agent function must take 1 (messages) or 2 (messages, ctx) positional "
|
|
37
|
+
f"parameters, got {n}"
|
|
38
|
+
)
|
|
39
|
+
return n
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
_STOP = object()
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _is_async(fn: Callable) -> bool:
|
|
46
|
+
"""Coroutine or async generator function (also as a partial or an async __call__)."""
|
|
47
|
+
for f in (fn, getattr(fn, "__call__", None)): # noqa: B004 - callable objects too
|
|
48
|
+
if f is not None and (inspect.iscoroutinefunction(f) or inspect.isasyncgenfunction(f)):
|
|
49
|
+
return True
|
|
50
|
+
return False
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
async def _aclose(gen: Any) -> None:
|
|
54
|
+
if hasattr(gen, "aclose"):
|
|
55
|
+
await gen.aclose()
|
|
56
|
+
elif hasattr(gen, "close"):
|
|
57
|
+
try:
|
|
58
|
+
await asyncio.to_thread(gen.close) # finally blocks may block too
|
|
59
|
+
except ValueError:
|
|
60
|
+
pass # "generator already executing": a step abandoned mid-run is still in its thread
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class FunctionAdapter:
|
|
64
|
+
supports_widgets = True
|
|
65
|
+
supports_attachments = True
|
|
66
|
+
|
|
67
|
+
def __init__(self, fn: Callable) -> None:
|
|
68
|
+
self._arity = _arity(fn)
|
|
69
|
+
self._fn = fn
|
|
70
|
+
self._async = _is_async(fn)
|
|
71
|
+
|
|
72
|
+
async def _items(self, input: RunAgentInput, ctx: RunContext) -> AsyncIterator[Any]:
|
|
73
|
+
args = (ctx.messages,) if self._arity == 1 else (ctx.messages, ctx)
|
|
74
|
+
# Sync code runs in a worker thread (the call and each step of a sync iterator), so a
|
|
75
|
+
# blocking agent never stalls the event loop. Async code runs on the loop as is.
|
|
76
|
+
result = await self._call(*args)
|
|
77
|
+
if inspect.isawaitable(result):
|
|
78
|
+
result = await result
|
|
79
|
+
if result is None:
|
|
80
|
+
return
|
|
81
|
+
if isinstance(result, str):
|
|
82
|
+
yield result
|
|
83
|
+
elif hasattr(result, "__aiter__"):
|
|
84
|
+
try:
|
|
85
|
+
async for item in result:
|
|
86
|
+
yield item
|
|
87
|
+
finally:
|
|
88
|
+
await _aclose(result)
|
|
89
|
+
elif isinstance(result, (list, tuple)):
|
|
90
|
+
for item in result:
|
|
91
|
+
yield item
|
|
92
|
+
else:
|
|
93
|
+
it = await asyncio.to_thread(iter, result)
|
|
94
|
+
try:
|
|
95
|
+
while (item := await asyncio.to_thread(next, it, _STOP)) is not _STOP:
|
|
96
|
+
yield item
|
|
97
|
+
finally:
|
|
98
|
+
await _aclose(it)
|
|
99
|
+
|
|
100
|
+
async def _call(self, *args: Any) -> Any:
|
|
101
|
+
if self._async:
|
|
102
|
+
return self._fn(*args)
|
|
103
|
+
return await asyncio.to_thread(self._fn, *args)
|
|
104
|
+
|
|
105
|
+
async def run(self, input: RunAgentInput, ctx: RunContext) -> AsyncIterator[BaseEvent]:
|
|
106
|
+
open_id: str | None = None
|
|
107
|
+
last_text_id: str | None = None
|
|
108
|
+
items = self._items(input, ctx)
|
|
109
|
+
try:
|
|
110
|
+
async for item in items:
|
|
111
|
+
if isinstance(item, str):
|
|
112
|
+
if not item:
|
|
113
|
+
continue
|
|
114
|
+
if open_id is None:
|
|
115
|
+
open_id = last_text_id = new_id()
|
|
116
|
+
yield TextMessageStartEvent(message_id=open_id, role="assistant")
|
|
117
|
+
yield TextMessageContentEvent(message_id=open_id, delta=item)
|
|
118
|
+
continue
|
|
119
|
+
if open_id is not None:
|
|
120
|
+
yield TextMessageEndEvent(message_id=open_id)
|
|
121
|
+
open_id = None
|
|
122
|
+
if isinstance(item, WidgetRequest):
|
|
123
|
+
call_id = new_id()
|
|
124
|
+
yield ToolCallStartEvent(
|
|
125
|
+
tool_call_id=call_id,
|
|
126
|
+
tool_call_name=item.tool_name,
|
|
127
|
+
parent_message_id=last_text_id,
|
|
128
|
+
)
|
|
129
|
+
yield ToolCallArgsEvent(tool_call_id=call_id, delta=item.args_json())
|
|
130
|
+
yield ToolCallEndEvent(tool_call_id=call_id)
|
|
131
|
+
return # a widget request ends the run; the finally closes the generator
|
|
132
|
+
if isinstance(item, BaseEvent):
|
|
133
|
+
yield item
|
|
134
|
+
else:
|
|
135
|
+
raise TypeError(f"unsupported value yielded by agent function: {item!r}")
|
|
136
|
+
if open_id is not None:
|
|
137
|
+
yield TextMessageEndEvent(message_id=open_id)
|
|
138
|
+
open_id = None
|
|
139
|
+
finally:
|
|
140
|
+
await items.aclose()
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"""Adapter for any OpenAI-compatible /chat/completions endpoint."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import logging
|
|
7
|
+
from collections.abc import AsyncIterator
|
|
8
|
+
from contextlib import asynccontextmanager
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
import httpx
|
|
12
|
+
from ag_ui.core import (
|
|
13
|
+
AssistantMessage,
|
|
14
|
+
BaseEvent,
|
|
15
|
+
RunAgentInput,
|
|
16
|
+
TextMessageContentEvent,
|
|
17
|
+
TextMessageEndEvent,
|
|
18
|
+
TextMessageStartEvent,
|
|
19
|
+
ToolCallArgsEvent,
|
|
20
|
+
ToolCallEndEvent,
|
|
21
|
+
ToolCallStartEvent,
|
|
22
|
+
ToolMessage,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
from loquent.adapters.base import AgentError
|
|
26
|
+
from loquent.context import RunContext
|
|
27
|
+
from loquent.protocol import new_id
|
|
28
|
+
|
|
29
|
+
log = logging.getLogger("loquent.adapters")
|
|
30
|
+
|
|
31
|
+
_INVALID = "Upstream model returned an invalid response"
|
|
32
|
+
_TEXT_ROLES = ("user", "system", "developer")
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _text(content: Any) -> str:
|
|
36
|
+
"""Flatten AG-UI message content (a string or a list of parts) into plain text."""
|
|
37
|
+
if content is None:
|
|
38
|
+
return ""
|
|
39
|
+
if isinstance(content, str):
|
|
40
|
+
return content
|
|
41
|
+
parts = []
|
|
42
|
+
for part in content:
|
|
43
|
+
t = part.get("text") if isinstance(part, dict) else getattr(part, "text", None)
|
|
44
|
+
if isinstance(t, str):
|
|
45
|
+
parts.append(t)
|
|
46
|
+
return "".join(parts)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _to_openai_messages(messages: list[Any]) -> list[dict[str, Any]]:
|
|
50
|
+
"""Convert AG-UI history to OpenAI messages.
|
|
51
|
+
|
|
52
|
+
OpenAI rejects tool calls that have no tool result, so unanswered calls (and tool
|
|
53
|
+
results with no matching call) are dropped, as are assistant messages left empty.
|
|
54
|
+
"""
|
|
55
|
+
result_positions: dict[str, int] = {}
|
|
56
|
+
for i, m in enumerate(messages):
|
|
57
|
+
if isinstance(m, ToolMessage):
|
|
58
|
+
result_positions.setdefault(m.tool_call_id, i)
|
|
59
|
+
|
|
60
|
+
out: list[dict[str, Any]] = []
|
|
61
|
+
kept_ids: set[str] = set()
|
|
62
|
+
for i, m in enumerate(messages):
|
|
63
|
+
role = getattr(m, "role", None)
|
|
64
|
+
if role in _TEXT_ROLES:
|
|
65
|
+
out.append({"role": role, "content": _text(m.content)})
|
|
66
|
+
elif isinstance(m, AssistantMessage):
|
|
67
|
+
calls = [
|
|
68
|
+
{
|
|
69
|
+
"id": c.id,
|
|
70
|
+
"type": "function",
|
|
71
|
+
"function": {"name": c.function.name, "arguments": c.function.arguments},
|
|
72
|
+
}
|
|
73
|
+
for c in (m.tool_calls or [])
|
|
74
|
+
if result_positions.get(c.id, -1) > i
|
|
75
|
+
]
|
|
76
|
+
content = _text(m.content)
|
|
77
|
+
if not content and not calls:
|
|
78
|
+
continue
|
|
79
|
+
msg: dict[str, Any] = {"role": "assistant", "content": content or None}
|
|
80
|
+
if calls:
|
|
81
|
+
msg["tool_calls"] = calls
|
|
82
|
+
kept_ids.update(c["id"] for c in calls)
|
|
83
|
+
out.append(msg)
|
|
84
|
+
elif isinstance(m, ToolMessage) and m.tool_call_id in kept_ids:
|
|
85
|
+
out.append(
|
|
86
|
+
{"role": "tool", "tool_call_id": m.tool_call_id, "content": _text(m.content)}
|
|
87
|
+
)
|
|
88
|
+
return out
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _to_openai_tools(tools: list[Any]) -> list[dict[str, Any]]:
|
|
92
|
+
return [
|
|
93
|
+
{
|
|
94
|
+
"type": "function",
|
|
95
|
+
"function": {
|
|
96
|
+
"name": t.name,
|
|
97
|
+
"description": t.description,
|
|
98
|
+
"parameters": t.parameters,
|
|
99
|
+
},
|
|
100
|
+
}
|
|
101
|
+
for t in tools
|
|
102
|
+
]
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class OpenAICompatAdapter:
|
|
106
|
+
supports_widgets = True
|
|
107
|
+
supports_attachments = False
|
|
108
|
+
|
|
109
|
+
def __init__(
|
|
110
|
+
self,
|
|
111
|
+
base_url: str,
|
|
112
|
+
model: str,
|
|
113
|
+
api_key: str | None = None,
|
|
114
|
+
client: httpx.AsyncClient | None = None,
|
|
115
|
+
) -> None:
|
|
116
|
+
self._url = base_url.rstrip("/") + "/chat/completions"
|
|
117
|
+
self._model = model
|
|
118
|
+
self._api_key = api_key
|
|
119
|
+
self._client = client or httpx.AsyncClient(timeout=httpx.Timeout(60, connect=10))
|
|
120
|
+
|
|
121
|
+
@asynccontextmanager
|
|
122
|
+
async def _open(self, body: dict[str, Any], headers: dict[str, str]):
|
|
123
|
+
"""POST the request. A 400 to a request that carried tools is retried once without
|
|
124
|
+
them: some backends (Ollama models without tool support, vLLM without auto tool
|
|
125
|
+
choice) reject tools outright."""
|
|
126
|
+
async with self._client.stream("POST", self._url, json=body, headers=headers) as resp:
|
|
127
|
+
if not (resp.status_code == 400 and "tools" in body):
|
|
128
|
+
yield resp
|
|
129
|
+
return
|
|
130
|
+
log.warning(
|
|
131
|
+
"model endpoint answered HTTP 400 to a request with tools; retrying without tools "
|
|
132
|
+
"(widgets are unavailable; pass --no-widgets or LOQUENT_WIDGETS=false to skip this)"
|
|
133
|
+
)
|
|
134
|
+
plain = {k: v for k, v in body.items() if k != "tools"}
|
|
135
|
+
async with self._client.stream("POST", self._url, json=plain, headers=headers) as resp:
|
|
136
|
+
yield resp
|
|
137
|
+
|
|
138
|
+
async def run(self, input: RunAgentInput, ctx: RunContext) -> AsyncIterator[BaseEvent]:
|
|
139
|
+
body: dict[str, Any] = {
|
|
140
|
+
"model": self._model,
|
|
141
|
+
"stream": True,
|
|
142
|
+
"messages": _to_openai_messages(ctx.messages),
|
|
143
|
+
}
|
|
144
|
+
if input.tools:
|
|
145
|
+
body["tools"] = _to_openai_tools(input.tools)
|
|
146
|
+
headers = {"Accept": "text/event-stream"}
|
|
147
|
+
if self._api_key:
|
|
148
|
+
headers["Authorization"] = f"Bearer {self._api_key}"
|
|
149
|
+
|
|
150
|
+
text_id: str | None = None
|
|
151
|
+
text_open = False
|
|
152
|
+
calls: dict[int, dict[str, str]] = {}
|
|
153
|
+
finished = False
|
|
154
|
+
error: str | None = None
|
|
155
|
+
try:
|
|
156
|
+
async with self._open(body, headers) as resp:
|
|
157
|
+
if resp.status_code >= 400:
|
|
158
|
+
raise AgentError(f"Upstream model returned HTTP {resp.status_code}")
|
|
159
|
+
async for line in resp.aiter_lines():
|
|
160
|
+
if not line.startswith("data:"):
|
|
161
|
+
continue # blank lines, ": ping" keepalives, other SSE fields
|
|
162
|
+
data = line[5:].strip()
|
|
163
|
+
if data == "[DONE]":
|
|
164
|
+
finished = True
|
|
165
|
+
break
|
|
166
|
+
try:
|
|
167
|
+
chunk = json.loads(data)
|
|
168
|
+
except ValueError:
|
|
169
|
+
raise AgentError(_INVALID) from None
|
|
170
|
+
if not isinstance(chunk, dict):
|
|
171
|
+
raise AgentError(_INVALID)
|
|
172
|
+
if chunk.get("error"):
|
|
173
|
+
raise AgentError("Upstream model reported an error")
|
|
174
|
+
choices = chunk.get("choices") or []
|
|
175
|
+
if not isinstance(choices, list):
|
|
176
|
+
raise AgentError(_INVALID)
|
|
177
|
+
for choice in choices:
|
|
178
|
+
if not isinstance(choice, dict):
|
|
179
|
+
raise AgentError(_INVALID)
|
|
180
|
+
if choice.get("finish_reason"):
|
|
181
|
+
finished = True
|
|
182
|
+
d = choice.get("delta") or {}
|
|
183
|
+
if not isinstance(d, dict):
|
|
184
|
+
raise AgentError(_INVALID)
|
|
185
|
+
content = d.get("content")
|
|
186
|
+
if isinstance(content, str) and content:
|
|
187
|
+
if not text_open:
|
|
188
|
+
text_id = new_id()
|
|
189
|
+
text_open = True
|
|
190
|
+
yield TextMessageStartEvent(message_id=text_id, role="assistant")
|
|
191
|
+
yield TextMessageContentEvent(message_id=text_id, delta=content)
|
|
192
|
+
tcs = d.get("tool_calls") or []
|
|
193
|
+
if not isinstance(tcs, list):
|
|
194
|
+
raise AgentError(_INVALID)
|
|
195
|
+
for tc in tcs:
|
|
196
|
+
if not isinstance(tc, dict):
|
|
197
|
+
raise AgentError(_INVALID)
|
|
198
|
+
fn = tc.get("function") or {}
|
|
199
|
+
if not isinstance(fn, dict):
|
|
200
|
+
raise AgentError(_INVALID)
|
|
201
|
+
slot = calls.setdefault(
|
|
202
|
+
tc.get("index", 0), {"id": "", "name": "", "args": ""}
|
|
203
|
+
)
|
|
204
|
+
slot["id"] = tc.get("id") or slot["id"]
|
|
205
|
+
slot["name"] += fn.get("name") or ""
|
|
206
|
+
slot["args"] += fn.get("arguments") or ""
|
|
207
|
+
if not (finished or text_open or calls):
|
|
208
|
+
raise AgentError(_INVALID)
|
|
209
|
+
except AgentError as e:
|
|
210
|
+
error = str(e)
|
|
211
|
+
except (httpx.HTTPError, httpx.InvalidURL):
|
|
212
|
+
error = "Could not reach the model endpoint"
|
|
213
|
+
|
|
214
|
+
if text_open:
|
|
215
|
+
yield TextMessageEndEvent(message_id=text_id)
|
|
216
|
+
if error is not None:
|
|
217
|
+
raise AgentError(error)
|
|
218
|
+
for _, call in sorted(calls.items()):
|
|
219
|
+
if not call["name"]:
|
|
220
|
+
continue
|
|
221
|
+
call_id = call["id"] or new_id()
|
|
222
|
+
yield ToolCallStartEvent(
|
|
223
|
+
tool_call_id=call_id, tool_call_name=call["name"], parent_message_id=text_id
|
|
224
|
+
)
|
|
225
|
+
yield ToolCallArgsEvent(tool_call_id=call_id, delta=call["args"] or "{}")
|
|
226
|
+
yield ToolCallEndEvent(tool_call_id=call_id)
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""Turn whatever the developer passed to serve() into an Adapter."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from loquent.adapters.agui_http import AgUiHttpAdapter
|
|
8
|
+
from loquent.adapters.base import Adapter
|
|
9
|
+
from loquent.adapters.function import FunctionAdapter
|
|
10
|
+
from loquent.adapters.openai_compat import OpenAICompatAdapter
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _is_adapter(target: Any) -> bool:
|
|
14
|
+
return (
|
|
15
|
+
not isinstance(target, type)
|
|
16
|
+
and callable(getattr(target, "run", None))
|
|
17
|
+
and hasattr(target, "supports_widgets")
|
|
18
|
+
and hasattr(target, "supports_attachments")
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def resolve_agent(target: Any, *, model: str | None = None, api_key: str | None = None) -> Adapter:
|
|
23
|
+
if _is_adapter(target):
|
|
24
|
+
return target
|
|
25
|
+
if isinstance(target, str) and target.startswith(("http://", "https://")):
|
|
26
|
+
if model:
|
|
27
|
+
return OpenAICompatAdapter(target, model, api_key)
|
|
28
|
+
return AgUiHttpAdapter(target)
|
|
29
|
+
if callable(target):
|
|
30
|
+
return FunctionAdapter(target)
|
|
31
|
+
raise TypeError(
|
|
32
|
+
f"Loquent can't serve {type(target).__name__}: pass a function, a URL, or an Adapter"
|
|
33
|
+
)
|