import json
import httpx
from typing import Any, Dict, List, Optional
class AetherShellClient:
def __init__(self, base_url: str = "http://localhost:3002"):
self.base_url = base_url
self.client = httpx.Client()
def _post(self, data: Dict[str, Any]) -> Dict[str, Any]:
response = self.client.post(self.base_url, json=data)
response.raise_for_status()
return response.json()
def eval(self, code: str) -> Dict[str, Any]:
return self._post({"action": "eval", "code": code})
def call(self, builtin: str, args: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
data = {"action": "call", "builtin": builtin}
if args:
data["args"] = args
return self._post(data)
def list_builtins(self, category: Optional[str] = None) -> Dict[str, Any]:
data = {"action": "list_builtins"}
if category:
data["category"] = category
return self._post(data)
def describe(self, builtin: str) -> Dict[str, Any]:
return self._post({"action": "describe", "builtin": builtin})
def schema(self, format: str) -> Dict[str, Any]:
return self._post({"action": "schema", "format": format})
def pipeline(self, steps: List[Dict[str, Any]]) -> Dict[str, Any]:
return self._post({"action": "pipeline", "steps": steps})
def openai_integration_example():
try:
from openai import OpenAI
except ImportError:
print("Install openai: pip install openai")
return
aether = AetherShellClient()
openai_client = OpenAI()
schema = aether.schema("openai")
if not schema.get("success"):
print(f"Failed to get schema: {schema.get('error')}")
return
tools = schema["result"]["tools"]
messages = [
{"role": "system", "content": "You are a helpful shell assistant. Use the provided tools to help users with file operations."},
{"role": "user", "content": "List the files in the current directory"}
]
response = openai_client.chat.completions.create(
model="gpt-4o-mini", messages=messages,
tools=tools,
tool_choice="auto"
)
if response.choices[0].message.tool_calls:
for tool_call in response.choices[0].message.tool_calls:
function_name = tool_call.function.name
function_args = json.loads(tool_call.function.arguments)
print(f"AI requested: {function_name}({function_args})")
result = aether.call(function_name, function_args)
if result.get("success"):
print(f"Result: {result['result']}")
else:
print(f"Error: {result.get('error')}")
else:
print(f"AI response: {response.choices[0].message.content}")
def anthropic_integration_example():
try:
import anthropic
except ImportError:
print("Install anthropic: pip install anthropic")
return
aether = AetherShellClient()
claude_client = anthropic.Anthropic()
schema = aether.schema("claude")
if not schema.get("success"):
print(f"Failed to get schema: {schema.get('error')}")
return
tools = schema["result"]["tools"]
message = claude_client.messages.create(
model="claude-3-5-sonnet-20241022", max_tokens=1024,
tools=tools,
messages=[
{"role": "user", "content": "What files are in the current directory?"}
]
)
for block in message.content:
if block.type == "tool_use":
tool_name = block.name
tool_input = block.input
print(f"Claude requested: {tool_name}({tool_input})")
result = aether.call(tool_name, tool_input)
if result.get("success"):
print(f"Result: {result['result']}")
else:
print(f"Error: {result.get('error')}")
elif block.type == "text":
print(f"Claude: {block.text}")
def langchain_integration_example():
try:
from langchain_core.tools import StructuredTool
from langchain_openai import ChatOpenAI
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_core.prompts import ChatPromptTemplate
except ImportError:
print("Install langchain: pip install langchain langchain-openai")
return
aether = AetherShellClient()
builtins_response = aether.list_builtins()
if not builtins_response.get("success"):
print(f"Failed to list builtins: {builtins_response.get('error')}")
return
tools = []
for builtin in builtins_response["result"]["builtins"][:10]: name = builtin["name"]
description = builtin["description"]
detail = aether.describe(name)
if not detail.get("success"):
continue
def make_tool_func(builtin_name):
def tool_func(**kwargs):
result = aether.call(builtin_name, kwargs if kwargs else None)
if result.get("success"):
return json.dumps(result["result"])
return f"Error: {result.get('error')}"
return tool_func
tool = StructuredTool.from_function(
func=make_tool_func(name),
name=name,
description=description,
)
tools.append(tool)
llm = ChatOpenAI(model="gpt-4o-mini")
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful shell assistant. Use the provided tools to help users."),
("human", "{input}"),
("placeholder", "{agent_scratchpad}"),
])
agent = create_tool_calling_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
result = agent_executor.invoke({"input": "List files in the current directory"})
print(f"Final result: {result['output']}")
def direct_api_examples():
aether = AetherShellClient()
print("=== Direct API Examples ===\n")
print("1. Evaluate expression:")
result = aether.eval("1 + 2 * 3")
print(f" 1 + 2 * 3 = {result['result']}")
print("\n2. List filesystem builtins:")
result = aether.list_builtins("filesystem")
if result.get("success"):
for b in result["result"]["builtins"][:5]:
print(f" - {b['name']}: {b['description']}")
print("\n3. Call 'pwd' builtin:")
result = aether.call("pwd")
if result.get("success"):
print(f" Current directory: {result['result']}")
print("\n4. Execute pipeline (ls | filter | select):")
result = aether.pipeline([
{"builtin": "ls", "args": {"path": "."}},
{"eval": "take(3)"},
{"eval": "select(\"name\", \"size\")"}
])
if result.get("success"):
print(f" Result: {json.dumps(result['result'], indent=2)}")
print("\n5. Available schema formats:")
formats = ["openai", "claude", "gemini", "llama", "mistral", "groq", "ollama"]
for fmt in formats:
schema = aether.schema(fmt)
if schema.get("success"):
model_count = len(schema["result"].get("compatible_models", []))
print(f" - {fmt}: {model_count} compatible models")
if __name__ == "__main__":
print("AetherShell Agent API - Python Integration Examples\n")
print("=" * 60)
try:
client = httpx.Client()
response = client.post("http://localhost:3002", json={"action": "list_builtins"})
if response.status_code != 200:
raise Exception("API not responding")
except Exception as e:
print(f"Error: Agent API not running on localhost:3002")
print("Start it with: ae --agent-api")
exit(1)
print("Agent API is running!\n")
direct_api_examples()
print("\n" + "=" * 60)
print("\nTo run AI integration examples:")
print(" - OpenAI: Set OPENAI_API_KEY, then call openai_integration_example()")
print(" - Claude: Set ANTHROPIC_API_KEY, then call anthropic_integration_example()")
print(" - LangChain: Set OPENAI_API_KEY, then call langchain_integration_example()")