SDK สำหรับ TESAIoT Dev Kit
คู่มืออ้างอิง API และ Tutorial (MTB & µPython)
Toggle main menu visibility
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 */
32
typedef
enum
{
33
CLAW_ROLE_USER
= 0,
34
CLAW_ROLE_ASSISTANT
= 1,
35
CLAW_ROLE_SYSTEM
= 2,
36
CLAW_ROLE_TOOL
= 3,
37
}
claw_role_t
;
38
39
/* Initialize session subsystem. Call from modbentoclaw init. */
40
void
claw_session_init
(
void
);
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. */
47
bool
claw_session_add
(
claw_role_t
role,
const
char
*content);
48
49
/* Get message count in current session. */
50
uint16_t
claw_session_count
(
void
);
51
52
/* Clear session (delete file). */
53
void
claw_session_clear
(
void
);
54
55
/* Flush dirty session to QSPI flash.
56
* Must be called from MicroPython task context. */
57
bool
claw_session_flush
(
void
);
58
59
/* Check if session has unflushed changes. */
60
bool
claw_session_dirty
(
void
);
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. */
66
bool
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. */
72
bool
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. */
78
size_t
claw_session_build_context
(
char
*out_buf,
size_t
out_max);
79
80
#endif
/* CLAW_SESSION_H */
claw_role_t
claw_role_t
Definition
claw_session.h:32
CLAW_ROLE_SYSTEM
@ CLAW_ROLE_SYSTEM
Definition
claw_session.h:35
CLAW_ROLE_TOOL
@ CLAW_ROLE_TOOL
Definition
claw_session.h:36
CLAW_ROLE_ASSISTANT
@ CLAW_ROLE_ASSISTANT
Definition
claw_session.h:34
CLAW_ROLE_USER
@ CLAW_ROLE_USER
Definition
claw_session.h:33
claw_memory_set
bool claw_memory_set(const char *key, const char *value)
เก็บคู่คีย์/ค่าแบบถาวรที่อยู่รอดทั้งการล้างเซสชันและ soft reset
claw_session_build_context
size_t claw_session_build_context(char *out_buf, size_t out_max)
เรนเดอร์รอบสนทนาล่าสุดพร้อม memory ลงในบัฟเฟอร์ที่ผู้เรียกจัดหามาให้; ยังไม่ผ่านการ escape แบบ JSON
claw_memory_get
bool claw_memory_get(const char *key, char *out_value, size_t out_max)
ค้นหาคีย์ใน /.bentoclaw_memory; คืน false เมื่อไม่พบ ไม่มีผู้เรียกที่มีอยู่จริงในของที่ส่งมอบ
claw_session_count
uint16_t claw_session_count(void)
จำนวนข้อความที่อยู่ใน ring ขณะนั้น
claw_session_init
void claw_session_init(void)
เริ่มต้นระบบย่อยเซสชัน; ถูกเรียกเพียงครั้งเดียวผ่าน bentoclaw_ensure_init()
claw_session_clear
void claw_session_clear(void)
ลบ /.bentoclaw_session และล้าง ring ให้ว่าง; ที่เก็บ memory ไม่ถูกแตะ
claw_session_dirty
bool claw_session_dirty(void)
predicate ที่ต้นทุนต่ำ: ring ใน RAM มีการเปลี่ยนแปลงที่ยังไม่ได้เขียนลงจริงหรือไม่ ไม่มีผู้เรียกที่มี...
claw_session_flush
bool claw_session_flush(void)
เขียน ring ที่ยังค้างอยู่ลงหน่วยความจำแฟลช จาก MicroPython task context เท่านั้น ไม่มีผู้เรียกที่มีอย...
claw_session_add
bool claw_session_add(claw_role_t role, const char *content)
ผนวกข้อความหนึ่งรายการและตัด ring ให้เหลือเท่ากับ CLAW_SESSION_MAX_MSGS
dist
mpy_secure
include
claw_session.h
Generated by
1.18.0