API Reference
openfang
OpenFang - AI agent gateway with pluggable adapters.
Usage
from src.spikes.deps import Deps, User, agent, chat, create_deps
Run modes
python -m src.spikes.deps # CLI help python -m src.spikes.deps chat # Interactive chat python -m src.spikes.deps telegram # Telegram bot python -m src.spikes.deps cron # Cron runner
FileSessions
File-based session storage using JSONL format.
Each session is stored as a separate .jsonl file with: - Header line: {"type": "session", "version": 1, "id": "...", "timestamp": "..."} - Message lines: {"type": "message", "message": {...}}
Similar to OpenClaw's transcript format.
Source code in src/openfang/capabilities/file_sessions.py
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 | |
__init__(directory)
Initialize file-based sessions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
str | Path
|
Directory to store session files (e.g. ~/.openfang/sessions) |
required |
Source code in src/openfang/capabilities/file_sessions.py
get(session_id)
async
Get all messages for a session.
Source code in src/openfang/capabilities/file_sessions.py
save(session_id, messages)
async
Save (replace) all messages for a session.
Source code in src/openfang/capabilities/file_sessions.py
append(session_id, messages)
async
Append messages to a session.
Source code in src/openfang/capabilities/file_sessions.py
delete(session_id)
async
list(user_id=None)
async
List all session IDs.
Note: user_id filtering is not supported for file-based storage. All sessions are returned.
Source code in src/openfang/capabilities/file_sessions.py
InMemoryCron
In-memory cron job storage.
Source code in src/openfang/capabilities/cron.py
InMemoryMemory
In-memory key-value store.
Source code in src/openfang/capabilities/memory.py
InMemorySessions
In-memory session storage.
Source code in src/openfang/capabilities/sessions.py
LocalFiles
File operations scoped to a root directory.
All paths are resolved relative to root unless absolute. Methods are async for protocol compatibility with remote implementations.
Source code in src/openfang/capabilities/files.py
read(path)
async
write(path, content)
async
Write content to file, creating parent dirs. Async for protocol compliance.
list(pattern='**/*')
async
List files matching glob pattern. Excludes .git and .venv.
Source code in src/openfang/capabilities/files.py
search(pattern, file_glob='**/*')
async
Search file contents with regex. Returns up to MAX_SEARCH_RESULTS matches.
Source code in src/openfang/capabilities/files.py
LocalProjects
Manage multiple local projects.
Source code in src/openfang/capabilities/projects.py
LocalShell
Execute commands locally.
Source code in src/openfang/capabilities/shell.py
PlaywrightSession
Stateful browser session using Playwright.
Source code in src/openfang/capabilities/web.py
PlaywrightWeb
Web access using Playwright.
Source code in src/openfang/capabilities/web.py
CommsAdapters
dataclass
Communication and capabilities.
Source code in src/openfang/deps.py
Deps
dataclass
All adapters + user context for an agent.
Source code in src/openfang/deps.py
is_subagent = False
class-attribute
instance-attribute
True if this is a subagent execution (prevents recursive spawning).
parent_run_id = None
class-attribute
instance-attribute
If subagent, the parent's run ID.
subagents = None
class-attribute
instance-attribute
Registry of available subagents.
subagent_executor = None
class-attribute
instance-attribute
Executor for spawning subagents.
switch_project(project_id)
async
Switch to a different project, updating file/shell roots.
Source code in src/openfang/deps.py
SchedulingAdapters
dataclass
StorageAdapters
dataclass
Data persistence adapters.
Source code in src/openfang/deps.py
WebAdapters
dataclass
Browser-based operations with session management.
Source code in src/openfang/deps.py
WorkspaceAdapters
dataclass
Local environment adapters.
Source code in src/openfang/deps.py
CronJob
dataclass
User
dataclass
Current user context.
Source code in src/openfang/models.py
Cron
Bases: Protocol
Schedule and manage cron jobs.
Source code in src/openfang/protocols.py
create(schedule, task, user_id=None)
async
get(job_id)
async
list(user_id=None)
async
delete(job_id)
async
enable(job_id)
async
Files
Bases: Protocol
File operations scoped to a root directory.
Source code in src/openfang/protocols.py
read(path)
async
Read file contents as text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Relative to root, or absolute path. |
required |
write(path, content)
async
Write content to file, creating parent directories if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Relative to root, or absolute path. |
required |
content
|
str
|
Text content to write. |
required |
list(pattern='**/*')
async
List files matching a glob pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Glob pattern like ".py" or "/.ts". |
'**/*'
|
search(pattern, file_glob='**/*')
async
Search file contents using regex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Regular expression to search for. |
required |
file_glob
|
str
|
Glob pattern to filter which files to search. |
'**/*'
|
Memory
Bases: Protocol
Key-value store.
Source code in src/openfang/protocols.py
get(key)
async
set(key, value)
async
delete(key)
async
Projects
Bases: Protocol
Project management.
Source code in src/openfang/protocols.py
current()
async
switch(project_id)
async
list()
async
Sessions
Bases: Protocol
Store and retrieve session message histories (transcripts).
Source code in src/openfang/protocols.py
get(session_id)
async
save(session_id, messages)
async
append(session_id, messages)
async
delete(session_id)
async
Shell
Bases: Protocol
Command execution.
Source code in src/openfang/protocols.py
Web
Bases: Protocol
Web access - stateless + session factory.
Source code in src/openfang/protocols.py
fetch(url)
async
browse(url)
async
screenshot(url)
async
WebSession
Bases: Protocol
Stateful browser session.
Source code in src/openfang/protocols.py
goto(url)
async
click(selector)
async
type(selector, text)
async
screenshot()
async
Match
dataclass
Page
dataclass
Result
dataclass
create_agent(model=None, system_prompt='You are a helpful assistant. Be concise.')
Create a configured agent with all tools registered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str | None
|
Model string (e.g. 'openai:gpt-4o'). If None, uses settings.get_model() which auto-prefixes with 'gateway/' if PYDANTIC_AI_GATEWAY_API_KEY is set. |
None
|
system_prompt
|
str
|
Base system prompt for the agent. |
'You are a helpful assistant. Be concise.'
|
Source code in src/openfang/agent.py
start_session(deps, session_id)
async
Start or resume a session.
create_deps(user, root=None, sessions=None, cron=None, current_page=None, session_id=None, enable_subagents=True)
async
Create Deps with sensible defaults. Use as async context manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user
|
User
|
User context for the agent. |
required |
root
|
Path | None
|
Root directory for file operations. |
None
|
sessions
|
Sessions | None
|
Session storage adapter. |
None
|
cron
|
Cron | None
|
Cron adapter. |
None
|
current_page
|
str | None
|
Current page context. |
None
|
session_id
|
str | None
|
Current session ID. |
None
|
enable_subagents
|
bool
|
Whether to enable subagent delegation. |
True
|