Skip to content

Commit ba6a5c1

Browse files
committed
fix(src): define MAX_ITERATIONS, disable DEBUG flag;
Add API.md
1 parent 8006e55 commit ba6a5c1

File tree

2 files changed

+173
-2
lines changed

2 files changed

+173
-2
lines changed

API.md

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# SQLite Agent API Reference
2+
3+
## Dependencies
4+
5+
**Required:**
6+
- [sqlite-mcp](../sqlite-mcp) – MCP client for tool access
7+
- [sqlite-ai](../sqlite-ai) – LLM inference and embeddings
8+
9+
**Optional:**
10+
- [sqlite-vector](../sqlite-vector) – For vector search (auto-enabled in table mode)
11+
12+
---
13+
14+
## Functions
15+
16+
### `agent_version()`
17+
18+
Returns the extension version.
19+
20+
**Syntax:**
21+
```sql
22+
SELECT agent_version();
23+
```
24+
25+
**Returns:** `TEXT` – Version string (e.g., "0.1.0")
26+
27+
**Example:**
28+
```sql
29+
SELECT agent_version();
30+
-- 0.1.0
31+
```
32+
33+
---
34+
35+
### `agent_run()`
36+
37+
Runs an autonomous AI agent that uses MCP tools to accomplish a goal.
38+
39+
**Syntax:**
40+
```sql
41+
-- MODE 1: Text response
42+
SELECT agent_run(goal);
43+
SELECT agent_run(goal, max_iterations);
44+
45+
-- MODE 2: Table extraction
46+
SELECT agent_run(goal, table_name);
47+
SELECT agent_run(goal, table_name, max_iterations);
48+
SELECT agent_run(goal, table_name, max_iterations, system_prompt);
49+
```
50+
51+
**Parameters:**
52+
53+
| Parameter | Type | Required | Default | Description |
54+
|-----------|------|----------|---------|-------------|
55+
| `goal` | TEXT | Yes | - | Task or goal for the agent |
56+
| `table_name` | TEXT | No | NULL | Target table (NULL for text mode) |
57+
| `max_iterations` | INTEGER | No | 5 | Maximum iterations |
58+
| `system_prompt` | TEXT | No | NULL | Custom system prompt |
59+
60+
**Returns:**
61+
- **MODE 1 (text):** TEXT – Agent's final response
62+
- **MODE 2 (table):** TEXT – Status message (e.g., "Inserted 5 rows into listings")
63+
64+
**MODE 1: Text Response**
65+
66+
Agent returns text after using tools.
67+
68+
```sql
69+
SELECT agent_run('Find affordable apartments in Rome', 8);
70+
```
71+
72+
Returns:
73+
```
74+
I found several affordable apartments in Rome:
75+
1. Bright Studio - €85/night
76+
2. Cozy Room - €72/night
77+
3. Modern Apartment - €95/night
78+
```
79+
80+
**MODE 2: Table Extraction**
81+
82+
Agent fetches data and populates a table.
83+
84+
```sql
85+
CREATE TABLE listings (
86+
id INTEGER PRIMARY KEY,
87+
title TEXT,
88+
price REAL,
89+
embedding BLOB
90+
);
91+
92+
SELECT agent_run('Find apartments in Rome under 100 EUR', 'listings', 8);
93+
-- Returns: "Inserted 5 rows into listings"
94+
```
95+
96+
**Auto-Features (MODE 2):**
97+
98+
1. **Schema Inspection** – Reads table schema to understand target data structure
99+
2. **Structured Extraction** – Extracts data matching column names and types
100+
3. **Transaction Safety** – Wraps all insertions in BEGIN/COMMIT
101+
4. **Auto-Embeddings** – Generates embeddings for BLOB columns named `*_embedding`
102+
5. **Auto-Vector Index** – Initializes vector indices when embeddings are created
103+
104+
**Custom System Prompt:**
105+
106+
```sql
107+
SELECT agent_run(
108+
'Find vegan restaurants',
109+
'restaurants',
110+
10,
111+
'You are a restaurant finder. Focus on highly-rated establishments.'
112+
);
113+
```
114+
115+
---
116+
117+
## Error Handling
118+
119+
**Common Errors:**
120+
121+
```sql
122+
-- Not connected to MCP
123+
SELECT agent_run('Find apartments', 5);
124+
-- Error: Not connected. Call mcp_connect() first
125+
126+
-- Table doesn't exist
127+
SELECT agent_run('Find apartments', 'nonexistent', 5);
128+
-- Error: Table does not exist or has no columns
129+
130+
-- LLM not loaded (table mode)
131+
SELECT agent_run('Find apartments', 'listings', 5);
132+
-- Error: Failed to create LLM chat context
133+
```
134+
135+
**Checking for Errors:**
136+
137+
```sql
138+
SELECT
139+
CASE
140+
WHEN result LIKE '%error%' OR result LIKE '%ERROR%'
141+
THEN 'Error occurred'
142+
ELSE 'Success'
143+
END
144+
FROM (SELECT agent_run('Find apartments', 5) as result);
145+
```
146+
147+
---
148+
149+
## Performance
150+
151+
**Timing:**
152+
- **Agent Iterations**: 100ms-10s per iteration (LLM inference)
153+
- **Tool Calls**: 10-1000ms (network latency)
154+
- **Embedding Generation**: 10-100ms per text (model-dependent)
155+
- **Vector Index**: <100ms (initialization)
156+
157+
**Tips:**
158+
- Use appropriate `max_iterations` (default: 5)
159+
- Reuse MCP connections (global client persists)
160+
- Use the Agent in a separated thread
161+
162+
---
163+
164+
## See Also
165+
166+
- [USAGE.md](USAGE.md) – Usage guide and examples
167+
- [README.md](README.md) – Project overview
168+
- [sqlite-mcp API](https://github.com/sqliteai/sqlite-mcp/blob/main/API.md) – MCP extension API
169+
- [sqlite-ai API](https://github.com/sqliteai/sqlite-ai/blob/main/API.md) – AI extension API
170+
- [sqlite-vector API](https://github.com/sqliteai/sqlite-vector/blob/main/API.md) – Vector extension API

src/sqlite-agent.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
// Created by Gioele Cantoni on 05/11/25.
66
//
77

8-
#define AGENT_DEBUG 1
8+
#define DEFAULT_AGENT_MAX_ITERATIONS 5
99

10+
//#define AGENT_DEBUG 1
1011
#ifdef AGENT_DEBUG
1112
#define D(x) fprintf(stderr, "[DEBUG] " x "\n")
1213
#define DF(fmt, ...) fprintf(stderr, "[DEBUG] " fmt "\n", __VA_ARGS__)
@@ -141,7 +142,7 @@ static void agent_run_func(
141142

142143
const char *goal = (const char*)sqlite3_value_text(argv[0]);
143144
const char *table_name = NULL;
144-
int max_iterations = 5;
145+
int max_iterations = DEFAULT_AGENT_MAX_ITERATIONS;
145146
const char *custom_system_prompt = NULL;
146147

147148
if (argc >= 2) {

0 commit comments

Comments
 (0)