SDK สำหรับ TESAIoT Dev Kit
คู่มืออ้างอิง API และ Tutorial (MTB & µPython)
Loading...
Searching...
No Matches
claw_session.h
Go to the documentation of this file.
1/*******************************************************************************
2 * File Name: claw_session.h
3 *
4 * Description: BentoClaw session memory — JSONL ring buffer on LittleFS.
5 * Stores last N conversation messages for context retention.
6 * Uses lfs_exec_py() pattern (MicroPython VFS bridge).
7 *
8 * Storage files:
9 * /.bentoclaw_session — JSONL ring buffer (max 20 entries)
10 * /.bentoclaw_memory — persistent key-value memory
11 *
12 *******************************************************************************/
13
14#ifndef CLAW_SESSION_H
15#define CLAW_SESSION_H
16
17#include <stdint.h>
18#include <stddef.h>
19#include <stdbool.h>
20
21/* Maximum messages in session ring buffer */
22#define CLAW_SESSION_MAX_MSGS (10)
23
24/* Maximum length of a single message line (JSON) */
25#define CLAW_MSG_MAX_LEN (256)
26
27/* Maximum key/value length for persistent memory */
28#define CLAW_MEM_KEY_MAX (32)
29#define CLAW_MEM_VAL_MAX (128)
30
31/* Message roles */
38
39/* Initialize session subsystem. Call from modbentoclaw init. */
41
42/* Add a message to the session ring buffer.
43 * Automatically trims to CLAW_SESSION_MAX_MSGS.
44 * role: message role
45 * content: message text (truncated to CLAW_MSG_MAX_LEN)
46 * Returns true on success. */
47bool claw_session_add(claw_role_t role, const char *content);
48
49/* Get message count in current session. */
50uint16_t claw_session_count(void);
51
52/* Clear session (delete file). */
54
55/* Flush dirty session to QSPI flash.
56 * Must be called from MicroPython task context. */
58
59/* Check if session has unflushed changes. */
61
62/* Store a persistent memory entry (survives session clear).
63 * key: memory key (max 32 chars)
64 * value: memory value (max 128 chars)
65 * Returns true on success. */
66bool claw_memory_set(const char *key, const char *value);
67
68/* Retrieve a persistent memory entry.
69 * key: memory key
70 * out_value: output buffer (at least CLAW_MEM_VAL_MAX bytes)
71 * Returns true if found. */
72bool claw_memory_get(const char *key, char *out_value, size_t out_max);
73
74/* Build context string from recent session + memory for LLM prompt.
75 * out_buf: output buffer
76 * out_max: max bytes
77 * Returns bytes written. */
78size_t claw_session_build_context(char *out_buf, size_t out_max);
79
80#endif /* CLAW_SESSION_H */
claw_role_t
Definition claw_session.h:32
@ CLAW_ROLE_SYSTEM
Definition claw_session.h:35
@ CLAW_ROLE_TOOL
Definition claw_session.h:36
@ CLAW_ROLE_ASSISTANT
Definition claw_session.h:34
@ CLAW_ROLE_USER
Definition claw_session.h:33
bool claw_memory_set(const char *key, const char *value)
เก็บคู่คีย์/ค่าแบบถาวรที่อยู่รอดทั้งการล้างเซสชันและ soft reset
size_t claw_session_build_context(char *out_buf, size_t out_max)
เรนเดอร์รอบสนทนาล่าสุดพร้อม memory ลงในบัฟเฟอร์ที่ผู้เรียกจัดหามาให้; ยังไม่ผ่านการ escape แบบ JSON
bool claw_memory_get(const char *key, char *out_value, size_t out_max)
ค้นหาคีย์ใน /.bentoclaw_memory; คืน false เมื่อไม่พบ ไม่มีผู้เรียกที่มีอยู่จริงในของที่ส่งมอบ
uint16_t claw_session_count(void)
จำนวนข้อความที่อยู่ใน ring ขณะนั้น
void claw_session_init(void)
เริ่มต้นระบบย่อยเซสชัน; ถูกเรียกเพียงครั้งเดียวผ่าน bentoclaw_ensure_init()
void claw_session_clear(void)
ลบ /.bentoclaw_session และล้าง ring ให้ว่าง; ที่เก็บ memory ไม่ถูกแตะ
bool claw_session_dirty(void)
predicate ที่ต้นทุนต่ำ: ring ใน RAM มีการเปลี่ยนแปลงที่ยังไม่ได้เขียนลงจริงหรือไม่ ไม่มีผู้เรียกที่มี...
bool claw_session_flush(void)
เขียน ring ที่ยังค้างอยู่ลงหน่วยความจำแฟลช จาก MicroPython task context เท่านั้น ไม่มีผู้เรียกที่มีอย...
bool claw_session_add(claw_role_t role, const char *content)
ผนวกข้อความหนึ่งรายการและตัด ring ให้เหลือเท่ากับ CLAW_SESSION_MAX_MSGS