File size: 1,725 Bytes
b2d9e47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Observation ingest + context rendering at a token budget.

Pattern: send hook-style observations during a coding session, then ask
agentmemory to render the most relevant context back at a fixed token budget.

Prerequisites:
    pip install iii-sdk
    npx -y @agentmemory/agentmemory

Run:
    python examples/python/observe_and_recall.py
"""

from datetime import datetime, timezone
from iii import register_worker


SESSION_ID = "py-example-session-001"
PROJECT = "demo"


def now_iso() -> str:
    return datetime.now(timezone.utc).isoformat()


def main() -> None:
    iii = register_worker("ws://localhost:49134")
    iii.connect()

    observations = [
        ("PreToolUse", {"tool": "Bash", "command": "cargo test"}),
        ("PostToolUse", {"tool": "Bash", "exit_code": 0}),
        ("UserPromptSubmit", {"prompt": "refactor auth middleware to use HMAC"}),
    ]

    for hook_type, data in observations:
        iii.trigger(
            {
                "function_id": "mem::observe",
                "payload": {
                    "hookType": hook_type,
                    "sessionId": SESSION_ID,
                    "project": PROJECT,
                    "cwd": "/home/user/service",
                    "timestamp": now_iso(),
                    "data": data,
                },
            }
        )

    context = iii.trigger(
        {
            "function_id": "mem::context",
            "payload": {
                "sessionId": SESSION_ID,
                "project": PROJECT,
                "budget": 2000,
            },
        }
    )

    print(f"Rendered context ({context.get('token_count', 0)} tokens):\n")
    print(context.get("text", ""))


if __name__ == "__main__":
    main()