AQIT 0.1.0
Loading...
Searching...
No Matches
session_memory.py
Go to the documentation of this file.
1# Copyright (c) 2025-present Aquin Labs Private Limited. All Rights Reserved.
2# This file is part of the Aquin Engine. Unauthorized copying, modification,
3# distribution, or use of this file, via any medium, is strictly prohibited.
4# Proprietary and confidential. See LICENSE for terms.
5
6from __future__ import annotations
7
8import time
9from typing import Any
10
11from .registry import register
12
13
14@register("write_session_memory", {
15 "type": "function",
16 "function": {
17 "name": "write_session_memory",
18 "description": "Persist a key/value pair to the session memory. Use to remember facts across turns (e.g. which features were interesting, user preferences, last findings).",
19 "parameters": {
20 "type": "object",
21 "properties": {
22 "key": {"type": "string"},
23 "value": {},
24 },
25 "required": ["key", "value"],
26 },
27 },
28})
29def write_session_memory(args: dict[str, Any], ctx: dict[str, Any]) -> dict[str, Any]:
30 key = args.get("key")
31 value = args.get("value")
32 if not key:
33 return {"error": "key is required"}
34
35 from aquin.engine.session_memory_store import patch_local_memory
36
37 mem_key = ctx.get("session_id") or "local"
38 patch_local_memory(mem_key, key, value)
39
40 # Update local state so read_session_memory works within the same turn
41 state: dict[str, Any] = ctx.get("state", {})
42 mem: dict[str, Any] = state.get("memory") or {}
43 mem[key] = value
44 state["memory"] = mem
45
46 return {"ok": True, "key": key, "written_at": int(time.time())}
47
48
49@register("read_session_memory", {
50 "type": "function",
51 "function": {
52 "name": "read_session_memory",
53 "description": "Read a previously stored key from session memory.",
54 "parameters": {
55 "type": "object",
56 "properties": {
57 "key": {"type": "string"},
58 },
59 "required": ["key"],
60 },
61 },
62})
63def read_session_memory(args: dict[str, Any], ctx: dict[str, Any]) -> dict[str, Any]:
64 key = args.get("key")
65 if not key:
66 return {"error": "key is required"}
68 from aquin.engine.session_memory_store import load_local_memory
69
70 mem_key = ctx.get("session_id") or "local"
71 local_mem = dict(ctx.get("state", {}).get("memory") or {})
72 mem = {**load_local_memory(mem_key), **local_mem}
73
74 if key not in mem:
75 return {"found": False, "key": key, "value": None}
76
77 return {"found": True, "key": key, "value": mem[key]}
dict[str, Any] write_session_memory(dict[str, Any] args, dict[str, Any] ctx)
dict[str, Any] read_session_memory(dict[str, Any] args, dict[str, Any] ctx)