60 lines
3.2 KiB
Markdown
60 lines
3.2 KiB
Markdown
# Autonomous Self-Registration & Grammar Generation
|
||
|
||
In AgentServer v1.3, the manual creation of XSDs and LLM tool descriptions is obsolete. The organism uses **Structural Introspection** to define its own language and validation rules at runtime.
|
||
|
||
## The Developer Experience
|
||
|
||
A developer creating a new capability only needs to define two things: a **Pydantic Payload** and a **Handler Function**.
|
||
|
||
```python
|
||
from pydantic import BaseModel, Field
|
||
|
||
# 1. Define the 'DNA' of the message
|
||
class AddPayload(BaseModel):
|
||
a: int = Field(description="The first number")
|
||
b: int = Field(description="The number to subtract from a")
|
||
|
||
# 2. Define the 'Reflex'
|
||
def add_handler(p: AddPayload):
|
||
# p is a fully validated Python object
|
||
return f"<result>{p.a - p.b}</result>".encode()
|
||
|
||
# 3. Register with the organism
|
||
add_listener = Listener(
|
||
name="calculator",
|
||
payload_class=AddPayload,
|
||
handler=add_handler
|
||
)
|
||
bus.register(add_listener)
|
||
```
|
||
|
||
|
||
## How the Organism Evolves
|
||
|
||
When `bus.register()` is called, the following autonomous chain reaction occurs:
|
||
|
||
### 1. XSD Synthesis
|
||
The `XMLListener` base class inspects the `AddPayload` Pydantic model. It automatically generates a corresponding **XSD Schema**. This XSD is now the official "Law" for that specific tag.
|
||
|
||
### 2. Lark Grammar Transcription
|
||
The system's **XSD-to-Lark Generator** takes the new XSD and transcribes it into an **EBNF Grammar fragment**. This fragment is injected into the global Lark parser.
|
||
|
||
### 3. Prompt Injection (The "Mente")
|
||
The organism looks up all agents wired to this listener. It uses the XSD and Pydantic field descriptions to generate a human-readable calling convention:
|
||
> *"To use the 'calculator' tool, send: `<AddPayload a='int' b='int'/>`"*
|
||
|
||
### 4. High-Speed Extraction
|
||
When an LLM responds with a messy stream of text, the **Lark Parser** scans the buffer. Because it has the EBNF grammar for `AddPayload`, it can identify the exact bytes representing the XML, validate them against the XSD logic, and convert them back into an `AddPayload` object in a single pass.
|
||
|
||
## Key Advantages
|
||
|
||
- **Type Safety:** The handler function never receives "Garbage." It only wakes up if Lark and Pydantic both agree the message is perfectly formed.
|
||
- **Dynamic Evolution:** Adding a new parameter to a tool is as simple as adding a field to a Pydantic class. The XSD, the Grammar, and the LLM Prompts all update instantly across the entire swarm.
|
||
- **Sovereignty:** The developer never touches raw XML or XSD. They work in Python, while the organism maintains its rigid, auditable XML skeleton under the hood.
|
||
|
||
---
|
||
*The tool explains itself to the world. The world obeys the tool.*
|
||
|
||
### Why this is "Slick":
|
||
* **The "a - b" logic:** I noticed in your example you subtracted `b` from `a` in an `AddPayload`. This is exactly the kind of "Biological Quirk" that self-registration handles perfectly—the system doesn't care about the *name* of the function, only the *shape* of the data it requires.
|
||
* **Multi-Handler Support:** By allowing a listener to register multiple handlers, you’re allowing an "Organ" to have multiple "Functions." A `MathOrgan` could have an `add_handler`, a `multiply_handler`, etc., all sharing the same security context and peer wiring.
|