SDK สำหรับ TESAIoT Dev Kit
คู่มืออ้างอิง API และ Tutorial (ModusToolbox)
Loading...
Searching...
No Matches
nus_agent.h
Go to the documentation of this file.
1/*******************************************************************************
2 * File Name: nus_agent.h
3 *
4 * Description: Desktop -> Device agent stream handlers for the LLM-side loop
5 * that runs inside Bento Desktop Buddy. The device originates a
6 * bento.agent.ask (from the LCD ASK submode), then the desktop
7 * streams back bento.agent.token frames, optional
8 * bento.agent.tool_call notifications (log-only on the device),
9 * and finally a bento.agent.done frame carrying the full answer.
10 *
11 * Contract:
12 * 1. One in-flight request id at a time. Token frames whose id
13 * doesn't match the latest ask are silently dropped.
14 * 2. Accumulator is bounded (256 chars) — overflow truncates.
15 * 3. On .done, the final text is forwarded to the CM55 page via
16 * IPC_CMD_BUDDY_AGENT_RESPONSE so the ASK submode can render.
17 * 4. .tool_call is observability-only: we emit a hint-text IPC
18 * so the scrolling marquee shows which tool is running.
19 *
20 * Reference: TESAIoT_PLAN/2026-4/Bento_Buddy/SPEC.md §5.4.
21 *
22 ******************************************************************************/
23
24#ifndef NUS_AGENT_H
25#define NUS_AGENT_H
26
27#include <stddef.h>
28#include <stdint.h>
29
30#include "vendor/jsmn.h"
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36/* Returns the number of bytes currently buffered for the in-flight agent ask.
37 * 0 when idle. Exposed for telemetry + host unit tests. */
39
40/* Reset the accumulator — called when the LCD user leaves the ASK submode or
41 * when a new ask is issued. Discards any partial text. Safe to call from any
42 * task context (simple memset; single-reader single-writer on the BLE task). */
43void nus_agent_reset(void);
44
45/* Record a new in-flight ask id so subsequent token/done frames can be matched.
46 * Called from page_bento_buddy before (or in parallel with) the IPC-originated
47 * bento.agent.ask emission. Truncates ids longer than the internal buffer. */
48void nus_agent_note_ask(const char *id);
49
50/* Handle bento.agent.token — appends the "delta" string to the in-flight
51 * request's accumulator. Returns 0 if accepted, -1 if the id doesn't match the
52 * current ask (silently dropped) or the delta field is missing. */
53int nus_agent_handle_token(const char *json, const jsmntok_t *toks, int n_toks);
54
55/* Handle bento.agent.done — emits the final text to the CM55 UI via
56 * IPC_CMD_BUDDY_AGENT_RESPONSE and resets the accumulator. Returns 0 on
57 * success. The "text" field is authoritative; if missing, the accumulated
58 * deltas are forwarded instead. */
59int nus_agent_handle_done(const char *json, const jsmntok_t *toks, int n_toks);
60
61/* Handle bento.agent.tool_call — log-only on the device (tool execution
62 * happens desktop-side). Emits a short hint-text IPC to the CM55 marquee so
63 * the user sees which tool is running. Returns 0. */
64int nus_agent_handle_tool_call(const char *json, const jsmntok_t *toks, int n_toks);
65
66#ifdef __cplusplus
67}
68#endif
69
70#endif /* NUS_AGENT_H */
struct jsmntok jsmntok_t
Definition bento_fw.h:37
int nus_agent_handle_done(const char *json, const jsmntok_t *toks, int n_toks)
ส่งข้อความสุดท้ายต่อไปยังหน้าบน CM55 ผ่าน IPC แล้วรีเซ็ต ฟิลด์ "text" เป็นตัวชี้ขาด
int nus_agent_handle_tool_call(const char *json, const jsmntok_t *toks, int n_toks)
เพื่อการสังเกตเท่านั้น — ส่งข้อความใบ้สั้น ๆ ผ่าน IPC ให้ marquee บน CM55 แสดงว่าเครื่องมือใดกำลังทำง...
size_t nus_agent_buffer_len(void)
จำนวนไบต์ที่พักไว้สำหรับ ask ที่ค้างอยู่ เป็น probe สำหรับ telemetry ไม่ใช่กลไกการซิงโครไนซ์
int nus_agent_handle_token(const char *json, const jsmntok_t *toks, int n_toks)
ต่อ "delta" เข้ากับ accumulator คืน -1 เมื่อ id ไม่ตรง (ถูกทิ้งไปเงียบ ๆ)
void nus_agent_reset(void)
ล้าง id ของ ask ที่ค้างอยู่และ accumulator ไม่ต้อง init ไม่มีการจับ lock ไม่มีค่าคืน
void nus_agent_note_ask(const char *id)
บันทึก id ของ ask ที่ค้างอยู่ id ที่เป็น NULL หรือว่างเปล่าจะรีเซ็ต agent ไม่มีผู้เรียกที่ใดเลย