fae-plugin-py 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.
- fae_plugin/__init__.py +19 -0
- fae_plugin/_host.py +55 -0
- fae_plugin/_inspect.py +30 -0
- fae_plugin/_protocol.py +57 -0
- fae_plugin/_worker.py +584 -0
- fae_plugin/chain.py +91 -0
- fae_plugin/config.py +93 -0
- fae_plugin/context.py +175 -0
- fae_plugin/engine.py +16 -0
- fae_plugin/errors.py +96 -0
- fae_plugin/hooks.py +56 -0
- fae_plugin/py.typed +1 -0
- fae_plugin/registration.py +173 -0
- fae_plugin/session.py +42 -0
- fae_plugin/types.py +464 -0
- fae_plugin_py-0.1.0.dist-info/METADATA +300 -0
- fae_plugin_py-0.1.0.dist-info/RECORD +20 -0
- fae_plugin_py-0.1.0.dist-info/WHEEL +5 -0
- fae_plugin_py-0.1.0.dist-info/licenses/LICENSE +201 -0
- fae_plugin_py-0.1.0.dist-info/top_level.txt +1 -0
fae_plugin/__init__.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from .types import (
|
|
2
|
+
API_VERSION, JsonValue, StackEntryId, RegistrationMode, ContextStateKind, ContextStateUpdate,
|
|
3
|
+
ContextFailure, ContextOutput, ContextState, ContextStackPhase, TaskTypeKind, TaskType,
|
|
4
|
+
TaskMeta, ContextStackDraft, ContextStack, ContextStackQuery, ContextUser, ContextSession,
|
|
5
|
+
TodoPlanScope, TodoContextBinding, SingleAgentSource, TimerOwner, TimerExecutionBinding,
|
|
6
|
+
ContextScope, ContextChildSpec, MessageReceipt, ModelRequest, ModelResponse, ToolRequest,
|
|
7
|
+
ToolResponse, SessionMessage, UserMemory,
|
|
8
|
+
)
|
|
9
|
+
from .context import Context, ContextStateView
|
|
10
|
+
from .session import Session
|
|
11
|
+
from .engine import Engine
|
|
12
|
+
from .hooks import SingleAgentHook, ToolsHook, ModelHook
|
|
13
|
+
from .chain import ChainSingleAgentHook, ChainToolsHook, ChainModelHook, HookNext
|
|
14
|
+
from .errors import (
|
|
15
|
+
PluginHostError, ContextUnavailable, ContextFinished, InvalidStateTransition,
|
|
16
|
+
ScopeAlreadyBound, InvalidScope, InvalidHandle, HandleExpired, StateViewClosed,
|
|
17
|
+
UnsupportedOutputType, UnsupportedValueType, StackCapacityExceeded, InvalidStackEntry,
|
|
18
|
+
SessionBusy, SessionUnavailable, InvalidMessage, HostCallTimeout, HookCancelled, InvalidHookNext, MissingChainHandler,
|
|
19
|
+
)
|
fae_plugin/_host.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import itertools
|
|
3
|
+
from typing import Any
|
|
4
|
+
from .errors import HandleExpired, HookCancelled, HostCallTimeout, from_wire
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Host:
|
|
8
|
+
def __init__(self, writer: Any):
|
|
9
|
+
self.writer = writer
|
|
10
|
+
self.pending: dict[str, asyncio.Future[Any]] = {}
|
|
11
|
+
self.ids = itertools.count()
|
|
12
|
+
|
|
13
|
+
async def call(self, invocation: Any, member: str, method: str, params: dict[str, Any]) -> Any:
|
|
14
|
+
invocation.check()
|
|
15
|
+
if len(self.pending) >= 256:
|
|
16
|
+
raise from_wire({"code": "plugin_busy", "message": "Host request capacity exceeded",
|
|
17
|
+
"resource": "host_requests"})
|
|
18
|
+
request_id = f"host-{next(self.ids)}"
|
|
19
|
+
future = asyncio.get_running_loop().create_future()
|
|
20
|
+
self.pending[request_id] = future
|
|
21
|
+
try:
|
|
22
|
+
await self.writer.send({"id": request_id, "method": "host.call", "params": {
|
|
23
|
+
"call_id": invocation.id, "member": member, "method": method, "params": params,
|
|
24
|
+
}}, control=True)
|
|
25
|
+
try:
|
|
26
|
+
value = await asyncio.wait_for(future, invocation.remaining())
|
|
27
|
+
except asyncio.TimeoutError:
|
|
28
|
+
raise HostCallTimeout("Host request exceeded invocation deadline") from None
|
|
29
|
+
if isinstance(value, dict) and value.get("_invocation_terminal"):
|
|
30
|
+
invocation.terminal = True
|
|
31
|
+
value = value.get("result")
|
|
32
|
+
return value
|
|
33
|
+
finally:
|
|
34
|
+
self.pending.pop(request_id, None)
|
|
35
|
+
|
|
36
|
+
def response(self, frame: dict[str, Any]) -> None:
|
|
37
|
+
future = self.pending.get(frame.get("id", ""))
|
|
38
|
+
if future is not None and not future.done():
|
|
39
|
+
if "error" in frame:
|
|
40
|
+
future.set_exception(from_wire(frame["error"]))
|
|
41
|
+
else:
|
|
42
|
+
future.set_result(frame.get("result"))
|
|
43
|
+
|
|
44
|
+
def close(self) -> None:
|
|
45
|
+
for future in self.pending.values():
|
|
46
|
+
if not future.done():
|
|
47
|
+
future.set_exception(HandleExpired("Host connection closed"))
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class BoundHost:
|
|
51
|
+
def __init__(self, host: Host, invocation: Any, member: str):
|
|
52
|
+
self.host, self.invocation, self.member = host, invocation, member
|
|
53
|
+
|
|
54
|
+
async def call(self, method: str, params: dict[str, Any]) -> Any:
|
|
55
|
+
return await self.host.call(self.invocation, self.member, method, params)
|
fae_plugin/_inspect.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""Candidate-only helper. stdout is reserved for one framed configuration result."""
|
|
2
|
+
import sys
|
|
3
|
+
import json
|
|
4
|
+
import struct
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from .config import inspect_config, normalize
|
|
7
|
+
from .registration import ModuleLoader
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def main() -> None:
|
|
11
|
+
output = sys.stdout.buffer
|
|
12
|
+
sys.stdout = sys.stderr
|
|
13
|
+
source = Path(sys.argv[2])
|
|
14
|
+
try:
|
|
15
|
+
if source.is_file():
|
|
16
|
+
loader = ModuleLoader(sys.argv[1], source.parent)
|
|
17
|
+
config = inspect_config(loader.load(source), sys.argv[1], source)
|
|
18
|
+
else:
|
|
19
|
+
config = normalize(json.loads((source / "plugin.config").read_text(encoding="utf-8")),
|
|
20
|
+
sys.argv[1], source)
|
|
21
|
+
result = {"result": config}
|
|
22
|
+
except Exception as error:
|
|
23
|
+
result = {"error": {"code": "plugin_config_invalid", "message": str(error)}}
|
|
24
|
+
data = json.dumps(result, allow_nan=False).encode()
|
|
25
|
+
output.write(struct.pack(">I", len(data)) + data)
|
|
26
|
+
output.flush()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
if __name__ == "__main__":
|
|
30
|
+
main()
|
fae_plugin/_protocol.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""Four-byte big-endian length followed by UTF-8 JSON."""
|
|
2
|
+
import asyncio
|
|
3
|
+
import json
|
|
4
|
+
import struct
|
|
5
|
+
from typing import Any, BinaryIO
|
|
6
|
+
|
|
7
|
+
MAX_FRAME = 4 * 1024 * 1024
|
|
8
|
+
PROTOCOL_VERSION = 1
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
async def reader() -> asyncio.StreamReader:
|
|
12
|
+
import sys
|
|
13
|
+
stream = asyncio.StreamReader(limit=MAX_FRAME + 4)
|
|
14
|
+
await asyncio.get_running_loop().connect_read_pipe(
|
|
15
|
+
lambda: asyncio.StreamReaderProtocol(stream), sys.stdin.buffer)
|
|
16
|
+
return stream
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
async def read_frame(stream: asyncio.StreamReader) -> dict[str, Any]:
|
|
20
|
+
size = struct.unpack(">I", await stream.readexactly(4))[0]
|
|
21
|
+
if size == 0 or size > MAX_FRAME:
|
|
22
|
+
raise ValueError("Invalid plugin protocol frame length")
|
|
23
|
+
data = json.loads(await stream.readexactly(size))
|
|
24
|
+
if not isinstance(data, dict):
|
|
25
|
+
raise ValueError("Protocol frame must be an object")
|
|
26
|
+
return data
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class Writer:
|
|
30
|
+
def __init__(self, output: BinaryIO):
|
|
31
|
+
self.output = output
|
|
32
|
+
self.control: asyncio.Queue[bytes] = asyncio.Queue(64)
|
|
33
|
+
self.business: asyncio.Queue[bytes] = asyncio.Queue(64)
|
|
34
|
+
self.ready = asyncio.Event()
|
|
35
|
+
|
|
36
|
+
async def send(self, frame: dict[str, Any], *, control: bool = False) -> None:
|
|
37
|
+
data = json.dumps(frame, allow_nan=False, separators=(",", ":")).encode("utf-8")
|
|
38
|
+
if not data or len(data) > MAX_FRAME:
|
|
39
|
+
raise ValueError("Plugin protocol message exceeds 4 MiB")
|
|
40
|
+
await (self.control if control else self.business).put(struct.pack(">I", len(data)) + data)
|
|
41
|
+
self.ready.set()
|
|
42
|
+
|
|
43
|
+
def _write(self, data: bytes) -> None:
|
|
44
|
+
self.output.write(data)
|
|
45
|
+
self.output.flush()
|
|
46
|
+
|
|
47
|
+
async def run(self) -> None:
|
|
48
|
+
while True:
|
|
49
|
+
await self.ready.wait()
|
|
50
|
+
queue = self.control if not self.control.empty() else self.business
|
|
51
|
+
if queue.empty():
|
|
52
|
+
self.ready.clear()
|
|
53
|
+
continue
|
|
54
|
+
data = queue.get_nowait()
|
|
55
|
+
await asyncio.to_thread(self._write, data)
|
|
56
|
+
if self.control.empty() and self.business.empty():
|
|
57
|
+
self.ready.clear()
|