-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfull_agent_run.py
More file actions
132 lines (109 loc) · 5.32 KB
/
Copy pathfull_agent_run.py
File metadata and controls
132 lines (109 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
"""Full Agent Lifecycle Example.
Run this example without any LLM API keys:
python3 examples/full_agent_run.py
It demonstrates the complete TeaAgent lifecycle:
1. Workspace tool registration
2. Audit logging
3. Memory catalog
4. Budget and approval policy
5. Agent runner with a deterministic decision function
6. Tool execution and observation collection
7. Run store persistence and replay
8. Audit log inspection and pruning
"""
from __future__ import annotations
import tempfile
from pathlib import Path
from uuid import uuid4
from teaagent.memory import MemoryCatalog
from teaagent.policy import ApprovalPolicy
from teaagent.run_store import RunStore
from teaagent.runner import AgentRunner, FinalAnswer, ToolRequest
from teaagent.telemetry import InMemoryMetricsSink
from teaagent.types import AuditLogger, PermissionMode, RunBudget
from teaagent.workspace_tools import WorkspaceToolConfig, build_workspace_tool_registry
def demo() -> None:
with tempfile.TemporaryDirectory() as tmp:
workspace = Path(tmp) / 'workspace'
workspace.mkdir()
rprint('=== workspace', str(workspace))
# ── 1. Workspace tools ──────────────────────────────────────────
config = WorkspaceToolConfig.from_root(workspace)
registry = build_workspace_tool_registry(workspace)
rprint('=== tools', len(registry.mcp_metadata()))
rprint(
'=== config max_read',
config.max_read_bytes,
'max_write',
config.max_write_bytes,
)
# ── 2. Audit + metrics ──────────────────────────────────────────
audit = AuditLogger(path=workspace / '.teaagent' / 'audit.jsonl')
metrics = InMemoryMetricsSink()
audit.add_sink(metrics.handle_event)
# ── 3. Memory catalog ───────────────────────────────────────────
memory = MemoryCatalog(workspace)
memory.add('Prefer concise answers.', tags=('style',))
rprint('=== memory entries', len(memory.list()))
# ── 4. Budget and approval ──────────────────────────────────────
budget = RunBudget(max_iterations=5, max_tool_calls=3)
approval = ApprovalPolicy(permission_mode=PermissionMode.WORKSPACE_WRITE)
# ── 5. Deterministic decision function (no LLM needed) ──────────
step = [0]
def decide(context):
step[0] += 1
if step[0] == 1:
# Write a file that says hello.
return ToolRequest(
tool_name='workspace_write_file',
arguments={'path': 'greeting.txt', 'content': 'hello from agent\n'},
)
if step[0] == 2:
# Read it back.
return ToolRequest(
tool_name='workspace_read_file',
arguments={'path': 'greeting.txt'},
)
# After reading, emit a final answer.
return FinalAnswer(content='done')
# ── 6. Agent runner ─────────────────────────────────────────────
runner = AgentRunner(
registry=registry,
audit=audit,
budget=budget,
approval_policy=approval,
)
result = runner.run(
task='write a greeting file then read it back', decide=decide
)
rprint(
'=== result',
result.status,
'itr',
result.iterations,
'tc',
result.tool_calls,
)
# ── 7. Run store ────────────────────────────────────────────────
store = RunStore(workspace)
store.logger_for_result(result, audit)
runs = store.list_runs(limit=5)
rprint('=== runs', len(runs))
rprint('=== run summary', runs[0].task, runs[0].status)
# ── 8. Audit replay ─────────────────────────────────────────────
events = store.show_run(result.run_id)
rprint('=== audit events', len(events))
for evt in events:
rprint(f' {evt["event_type"]:30s} run_id={evt["run_id"][:8]}...')
# ── 9. Metrics snapshot ─────────────────────────────────────────
snap = metrics.snapshot()
rprint('=== counters', dict(snap.counters))
# ── 10. Cleanup ─────────────────────────────────────────────────
audit_pruner = AuditLogger()
audit_pruner.record('run_started', uuid4().hex)
audit_pruner.record('run_completed', uuid4().hex)
rprint('=== ok')
def rprint(label, *values):
print(f'{label:<24}', *values)
if __name__ == '__main__':
demo()