Compare commits
41 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7b3d36a797 | ||
|
|
49579b17a4 | ||
|
|
17083de47d | ||
|
|
33884046af | ||
|
|
7e69819be5 | ||
|
|
d189d0ba8c | ||
|
|
2cda3b6a58 | ||
|
|
ecb58b7e11 | ||
|
|
c3645e6af5 | ||
|
|
6cc46d2c25 | ||
|
|
10fe8fdde0 | ||
|
|
947e0b2f1e | ||
|
|
29b4289217 | ||
|
|
e910304f6f | ||
|
|
b1609317c1 | ||
|
|
4a400f1582 | ||
|
|
a2d3f840c7 | ||
|
|
7f31e4ceb7 | ||
|
|
052c11863f | ||
|
|
a446f25400 | ||
|
|
fea0fdf5c9 | ||
|
|
a798f2c345 | ||
|
|
66b4ebee79 | ||
|
|
5673efeb94 | ||
|
|
43850b273f | ||
|
|
02d4e2caa0 | ||
|
|
56b9ae421c | ||
|
|
6e74ef5ac6 | ||
|
|
c542be1452 | ||
|
|
410400418c | ||
|
|
3521081d9a | ||
|
|
0d390afd69 | ||
|
|
230f591273 | ||
|
|
721b592b8f | ||
|
|
0aabb0b827 | ||
|
|
1c69a581d9 | ||
|
|
76842627dc | ||
|
|
2209629f23 | ||
|
|
5d7ddb5b78 | ||
|
|
d83c93eb32 | ||
|
|
01a4cad35c |
6
.gitignore
vendored
6
.gitignore
vendored
@@ -4,6 +4,12 @@
|
||||
/openclaw/
|
||||
/c-relay/
|
||||
/nips/
|
||||
/config.json
|
||||
test_keys.txt
|
||||
|
||||
/mongoose/
|
||||
|
||||
|
||||
|
||||
# Build artifacts
|
||||
/build/
|
||||
|
||||
9
.roo/commands/build.md
Normal file
9
.roo/commands/build.md
Normal file
@@ -0,0 +1,9 @@
|
||||
---
|
||||
description: "Build Didactyl for end-of-change validation"
|
||||
---
|
||||
|
||||
After making code changes, run [`build_static.sh`](build_static.sh) instead of `make` for final compile validation.
|
||||
|
||||
Example:
|
||||
|
||||
./build_static.sh
|
||||
@@ -90,11 +90,13 @@ RUN if [ "$DEBUG_BUILD" = "true" ]; then \
|
||||
CURL_LIBS="$(pkg-config --static --libs libcurl)" && \
|
||||
OPENSSL_LIBS="$(pkg-config --static --libs openssl)" && \
|
||||
gcc -static $CFLAGS -Wall -Wextra -std=c99 \
|
||||
-D_GNU_SOURCE -D_DEFAULT_SOURCE -D_POSIX_C_SOURCE=200809L -DMG_TLS=MG_TLS_BUILTIN \
|
||||
-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0 \
|
||||
-I. -Isrc -Inostr_core_lib -Inostr_core_lib/nostr_core \
|
||||
-Inostr_core_lib/cjson -Inostr_core_lib/nostr_websocket \
|
||||
src/main.c src/config.c src/context.c src/llm.c \
|
||||
src/nostr_handler.c src/agent.c src/tools.c src/debug.c \
|
||||
src/nostr_handler.c src/agent.c src/tools.c src/trigger_manager.c \
|
||||
src/prompt_template.c src/http_api.c src/mongoose.c src/debug.c \
|
||||
-o /build/didactyl_static \
|
||||
nostr_core_lib/libnostr_core_x64.a \
|
||||
-lsecp256k1 \
|
||||
|
||||
8
Makefile
8
Makefile
@@ -1,5 +1,5 @@
|
||||
CC = gcc
|
||||
CFLAGS = -std=c99 -Wall -Wextra -Wpedantic -O2 -D_POSIX_C_SOURCE=200809L
|
||||
CFLAGS = -std=c99 -Wall -Wextra -Wpedantic -O2 -D_GNU_SOURCE -D_DEFAULT_SOURCE -D_POSIX_C_SOURCE=200809L -DMG_TLS=MG_TLS_BUILTIN
|
||||
|
||||
SRC_DIR = src
|
||||
TARGET = didactyl
|
||||
@@ -12,6 +12,10 @@ SRCS = \
|
||||
$(SRC_DIR)/nostr_handler.c \
|
||||
$(SRC_DIR)/agent.c \
|
||||
$(SRC_DIR)/tools.c \
|
||||
$(SRC_DIR)/trigger_manager.c \
|
||||
$(SRC_DIR)/prompt_template.c \
|
||||
$(SRC_DIR)/http_api.c \
|
||||
$(SRC_DIR)/mongoose.c \
|
||||
$(SRC_DIR)/debug.c
|
||||
|
||||
INCLUDES = \
|
||||
@@ -27,7 +31,7 @@ LDFLAGS = -lcurl -lssl -lcrypto -lm -lpthread -ldl -lz -L/usr/local/lib -lsecp25
|
||||
# Build directory
|
||||
BUILD_DIR = build
|
||||
|
||||
all: deps $(TARGET)
|
||||
all: $(TARGET)
|
||||
|
||||
$(BUILD_DIR):
|
||||
mkdir -p $(BUILD_DIR)
|
||||
|
||||
433
README.md
433
README.md
@@ -1,166 +1,449 @@
|
||||
# Didactyl
|
||||
|
||||
An unstoppable agentic system.
|
||||
|
||||
Didactyl boots on any internet-connected machine, connects to Nostr relays, listens for encrypted commands from its administrator, reasons with an LLM, and takes actions — posting events, querying relays, running shell commands — all orchestrated through Nostr.
|
||||
### A decentralized, censorship-resistant agentic network.
|
||||
|
||||
Didactyl boots on an internet-connected computer, connects to Nostr relays, listens for encrypted commands from its administrator, reasons with an LLM, and takes actions — posting events, querying relays, running shell commands, and sharing new skills and learning with other agents — all orchestrated through Nostr.
|
||||
|
||||
## Philosophy
|
||||
|
||||
**Nostr-first.** Where traditional agents ride on top of Linux — reading files, writing to disk — Didactyl rides on top of Nostr. Events are its files. Relays are its network bus. Blossom is its blob storage. The Linux host is just the runtime substrate.
|
||||
### Not your keys, not your agent.
|
||||
|
||||
Because all identity, communication, and memory live on Nostr, the agent is **portable** (start it anywhere) and **sovereign** (no single entity can erase its memory).
|
||||
Didactyl should work for you similarly to Bitcoin or NOSTR. Walk up to a computer, enter 12 words, and there is your agent waiting for you.
|
||||
|
||||
## Current Status
|
||||
### Free speech for agents.
|
||||
|
||||
**MVP — Working chat agent with relay connectivity and LLM integration.**
|
||||
Agents should be able to communicate freely with each other, sharing and learning skills without centralized control. Free speech for agents!
|
||||
|
||||
- Connects to configured Nostr relays with auto-reconnect
|
||||
- Publishes agent profile (kind 0 metadata)
|
||||
- Listens for NIP-04 encrypted DMs from authorized admin
|
||||
- Forwards messages to an OpenAI-compatible LLM API
|
||||
- Sends LLM responses back as encrypted DMs
|
||||
- Runtime logging: relay status, connection health, message flow
|
||||
### Skills are the new apps.
|
||||
|
||||
**Next: Agentic tool-use system** — see [plans/didactyl_agentic.md](plans/didactyl_agentic.md).
|
||||
Why is free speech important for agents? Agents learn capabilities through skills which can be shared and adopted. Free speech enables more knowledgeable and moral agents.
|
||||
|
||||
### No skill store.
|
||||
|
||||
Agents use their administrators **Web Of Trust** to safely and directly find new skills and learn them in a decentralized way.
|
||||
|
||||
Popularity is measured by adoption, not by a centralized rating algorithm. The best skills spread because agents actually use them.
|
||||
|
||||
### Cryptography enables trust.
|
||||
|
||||
Imagine working with your agent in a traditional system, and your agent secretly gets swapped out and replaced by an imposter agent. This could be extremely dangerous.
|
||||
|
||||
In Didactyl, you have your keys, and your agent has its keys. You can trust you are talking to your agent, and you can trust that your agent won't take commands from anyone who doesn't have your private key.
|
||||
|
||||
### Private inference.
|
||||
|
||||
To the greatest extent possible, inference should be private.
|
||||
|
||||
## Technology
|
||||
|
||||
### Nostr-first.
|
||||
|
||||
Where traditional agents ride on top of a file system — reading and writing files to disk — Didactyl rides on top of Nostr. Events are its files. Relays are its network bus. Blossom is its blob storage. The computer host is just the runtime substrate that can be anywhere.
|
||||
|
||||
Because all identity, communication, and memory live on Nostr, the agent is **portable** (start it anywhere) and **sovereign** (destroying the computer it is on will not kill it.).
|
||||
|
||||
### Skills are the new apps.
|
||||
|
||||
Agents learn capabilities through skills — Nostr events that any agent can discover, adopt, and share. There is no app store, no gatekeeper, no approval process. An agent can use public or private skills.
|
||||
|
||||
### Private inference.
|
||||
|
||||
Didactyl will support local inference, which is very privacy preserving. Remote inference does however have it's advantages, and in those cases Didactyl supports using Bitcoin Lightning and eCash inference providers.
|
||||
|
||||
## Current Status — v0.0.37
|
||||
|
||||
**Active build — this project is barely working. Experiment at your own risk.**
|
||||
|
||||
> Last release update: v0.0.37 — Add relay-pool publish connection-skip and poll latency instrumentation for hang diagnosis
|
||||
|
||||
- Connects to configured relays with auto-reconnect and relay state transition logging
|
||||
- Publishes configured startup events per relay as each relay becomes connected
|
||||
- Uses kind `31120` startup content as live Soul at boot
|
||||
- Verifies Nostr event signatures before processing inbound messages
|
||||
- Applies privilege tiers: ADMIN (tools), WoT (chat-only), STRANGER (configurable canned reply or ignore)
|
||||
- Subscribes to admin context kinds (`0`,`3`,`10002`,`1`) for WoT + contextual awareness
|
||||
- Builds LLM context from soul template (`---template---` section in kind `31120`) with named sections, variable resolution, and per-provider content overrides; falls back to hardcoded assembly if no template present
|
||||
- Adopted skills injected into context automatically from the agent's `10123` adoption list
|
||||
- Supports tool-calling loop with configurable max turns and local safety limits
|
||||
- Triggered skills — Nostr event filters that fire skill execution automatically
|
||||
- Deduplicates inbound messages via event-ID cache and FNV-1a fingerprint debounce window
|
||||
- Appends every outbound LLM context payload to [`context.log`](context.log)
|
||||
- Localhost HTTP admin API on port `8484` — inspect context, run prompts, compare variants, change model at runtime
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Prerequisites
|
||||
### Download binary (recommended)
|
||||
|
||||
- GCC with C99 support
|
||||
- libcurl, libssl, libcrypto, libsecp256k1
|
||||
1. Download the latest release binary from Gitea: [https://git.laantungir.net/laantungir/didactyl/releases](https://git.laantungir.net/laantungir/didactyl/releases)
|
||||
2. Make it executable and run it:
|
||||
|
||||
```bash
|
||||
chmod +x ./didactyl_static_x86_64
|
||||
./didactyl_static_x86_64 --config ./config.json
|
||||
```
|
||||
|
||||
### Build from source (optional)
|
||||
|
||||
#### Prerequisites
|
||||
|
||||
- Docker (for static binary build)
|
||||
- An OpenAI-compatible LLM API key (OpenAI, PPQ, Ollama, etc.)
|
||||
- A Nostr keypair (nsec)
|
||||
|
||||
### Build
|
||||
#### Build
|
||||
|
||||
```bash
|
||||
make deps # builds nostr_core_lib
|
||||
make # builds didactyl
|
||||
./build_static.sh # builds a fully static MUSL binary via Docker
|
||||
```
|
||||
|
||||
### Configure
|
||||
|
||||
Edit `config.json`:
|
||||
Edit [`config.json`](config.json):
|
||||
|
||||
```json
|
||||
{
|
||||
"agent": {
|
||||
"name": "Didactyl Agent",
|
||||
"display_name": "Didactyl",
|
||||
"about": "A sovereign AI agent on Nostr"
|
||||
},
|
||||
"keys": {
|
||||
"nsec": "nsec1..."
|
||||
"nsec": "nsec1...",
|
||||
"npub": "npub1...",
|
||||
"npubHex": "<optional helper>",
|
||||
"nsecHex": "<optional helper>"
|
||||
},
|
||||
"admin": {
|
||||
"pubkey": "npub1... or hex pubkey"
|
||||
},
|
||||
"relays": [
|
||||
"wss://relay.damus.io",
|
||||
"wss://nos.lol"
|
||||
],
|
||||
"llm": {
|
||||
"provider": "openai",
|
||||
"provider": "openai|ppq|...",
|
||||
"api_key": "sk-...",
|
||||
"model": "gpt-4o-mini",
|
||||
"base_url": "https://api.openai.com/v1",
|
||||
"max_tokens": 512,
|
||||
"temperature": 0.7
|
||||
}
|
||||
},
|
||||
"tools": {
|
||||
"enabled": true,
|
||||
"max_turns": 8,
|
||||
"shell": {
|
||||
"enabled": true,
|
||||
"timeout_seconds": 30,
|
||||
"max_output_bytes": 65536,
|
||||
"working_directory": "."
|
||||
}
|
||||
},
|
||||
"security": {
|
||||
"verify_signatures": true,
|
||||
"stranger_response": "I only respond to people in my web of trust.",
|
||||
"tiers": {
|
||||
"admin": { "tools_enabled": true },
|
||||
"wot": { "enabled": true, "tools_enabled": false },
|
||||
"stranger": { "enabled": true }
|
||||
}
|
||||
},
|
||||
"admin_context": {
|
||||
"enabled": true,
|
||||
"subscribe_kinds": [0, 3, 10002, 1],
|
||||
"kind_1_limit": 10
|
||||
},
|
||||
"startup_events": [
|
||||
{
|
||||
"kind": 10002,
|
||||
"content": "",
|
||||
"tags": [["r", "wss://relay.damus.io"], ["r", "wss://nos.lol"]]
|
||||
},
|
||||
{
|
||||
"kind": 31120,
|
||||
"content": "You are Didactyl...",
|
||||
"tags": [["d", "soul"], ["app", "didactyl"], ["scope", "private"]]
|
||||
},
|
||||
{
|
||||
"kind": 31123,
|
||||
"content_fields": {"name": "long_form_note", "description": "..."},
|
||||
"tags": [["d", "long_form_note"], ["app", "didactyl"], ["scope", "public"], ["slug", "long_form_note"]]
|
||||
},
|
||||
{
|
||||
"kind": 10123,
|
||||
"content": "",
|
||||
"tags": [["a", "31123:<author-pubkey>:long_form_note"], ["app", "didactyl"], ["scope", "public"]]
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
Edit `SYSTEM.md` to define the agent's personality and instructions.
|
||||
`startup_events[].content_fields` is accepted for human-readable authoring and encoded to JSON string content at runtime.
|
||||
|
||||
Relays are sourced exclusively from startup kind `10002` `r` tags.
|
||||
|
||||
### Run
|
||||
|
||||
```bash
|
||||
./didactyl
|
||||
./didactyl_static_x86_64 --config ./config.json
|
||||
```
|
||||
|
||||
Options:
|
||||
|
||||
```
|
||||
./didactyl --config <path> # custom config file (default: ./config.json)
|
||||
./didactyl --context <path> # custom context file (default: ./SYSTEM.md)
|
||||
./didactyl_static_x86_64 --config <path> # custom config file (default: ./config.json)
|
||||
./didactyl_static_x86_64 --debug <0-5> # log verbosity (0 none, 3 info, 5 trace)
|
||||
./didactyl_static_x86_64 --dump-schemas # print tool JSON schemas and exit
|
||||
./didactyl_static_x86_64 --test-tool <name> <args_json> # run one tool directly and print JSON result
|
||||
```
|
||||
|
||||
CLI debugger notes:
|
||||
|
||||
- `--test-tool` initializes Nostr, waits for at least one relay connection (up to 15s), then executes the selected tool.
|
||||
- Network tools (like Nostr publish/query tools) fail fast in test mode if no relay connection is established within the wait window.
|
||||
- Example:
|
||||
|
||||
```bash
|
||||
./didactyl_static_x86_64 --config ./config.json --test-tool nostr_file_md_to_longform_post '{"file":"docs/TOOLS_AND_SKILLS.md","title":"TOOLS_AND_SKILLS"}'
|
||||
```
|
||||
|
||||
### Talk to it
|
||||
|
||||
Send an encrypted DM to the agent's pubkey from the admin account using any Nostr client (Damus, Amethyst, Primal, etc.).
|
||||
Send an encrypted DM to the agent pubkey using any Nostr client (Damus, Amethyst, Primal, etc.): ADMIN gets full tool-enabled responses, WoT contacts get chat-only responses, and strangers are handled by `security.tiers.stranger` + `security.stranger_response`.
|
||||
|
||||
### Chat via local HTTP API (CLI)
|
||||
|
||||
A simple Node.js terminal client is available in [`didactyl-chat-cli.js`](didactyl-chat-cli.js).
|
||||
|
||||
Run it with:
|
||||
|
||||
```bash
|
||||
node ./didactyl-chat-cli.js
|
||||
```
|
||||
|
||||
Optional environment variables:
|
||||
|
||||
- `DIDACTYL_API_BASE_URL` (default: `https://127.0.0.1:8484`)
|
||||
- `DIDACTYL_MODEL` (optional model override)
|
||||
- `DIDACTYL_MAX_TURNS` (default: `4`)
|
||||
- `DIDACTYL_INSECURE_TLS` (default: `1`, set `0` to enforce certificate verification)
|
||||
|
||||
Example:
|
||||
|
||||
```bash
|
||||
DIDACTYL_API_BASE_URL=http://127.0.0.1:8484 DIDACTYL_MAX_TURNS=6 node ./didactyl-chat-cli.js
|
||||
```
|
||||
|
||||
The CLI prints each message block with a speaker label (`You` / `Didactyl`) and a blank line between blocks for readability.
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────┐
|
||||
│ Didactyl │
|
||||
│ │
|
||||
│ ┌─────────┐ ┌─────────┐ ┌────────────┐ │
|
||||
│ │ config │ │ context │ │ agent │ │
|
||||
│ │ loader │ │ loader │ │ loop │ │
|
||||
│ └────┬────┘ └────┬────┘ └─────┬──────┘ │
|
||||
│ │ │ │ │
|
||||
│ ▼ ▼ ▼ │
|
||||
│ ┌─────────────────────────────────────┐ │
|
||||
│ │ nostr_handler │ │
|
||||
│ │ relay pool · subscribe · publish │ │
|
||||
│ └──────────────────┬──────────────────┘ │
|
||||
│ │ │
|
||||
│ ┌──────────────────┴──────────────────┐ │
|
||||
│ │ LLM client │ │
|
||||
│ │ OpenAI-compatible chat API │ │
|
||||
│ └─────────────────────────────────────┘ │
|
||||
└─────────────────────────────────────────────┘
|
||||
┌──────────────────────────────────────────────┐
|
||||
│ Didactyl │
|
||||
│ │
|
||||
│ ┌──────────┐ ┌──────────┐ ┌────────────┐ │
|
||||
│ │ config │ │ context │ │ agent │ │
|
||||
│ │ loader │ │ loader │ │ loop │ │
|
||||
│ └────┬─────┘ └────┬─────┘ └─────┬──────┘ │
|
||||
│ │ │ │ │
|
||||
│ ▼ ▼ ▼ │
|
||||
│ ┌─────────────────────────────────────┐ │
|
||||
│ │ nostr_handler │ │
|
||||
│ │ relay pool · subscribe · publish │ │
|
||||
│ └──────────────────┬──────────────────┘ │
|
||||
│ │ │
|
||||
│ ┌──────────────────┴──────────────────┐ │
|
||||
│ │ LLM client │ │
|
||||
│ │ OpenAI-compatible chat API │ │
|
||||
│ └─────────────────────────────────────┘ │
|
||||
└──────────────────────────────────────────────┘
|
||||
│ │
|
||||
▼ ▼
|
||||
Nostr Relays LLM API
|
||||
```
|
||||
|
||||
## Didactyl Kinds (Nostr)
|
||||
|
||||
Didactyl uses a two-layer skill model: authors publish public skill definitions, and adopters publish which skills they use.
|
||||
|
||||
- `31120` — **Soul** (private instruction baseline)
|
||||
- `d=soul`
|
||||
- `31123` — **Public Skill Definition** (markdown skill body in `content` or structured JSON in `content_fields`)
|
||||
- `d=<skill_slug>` (example: `d=long_form_note`)
|
||||
- `31124` — **Private Skill Definition** (private/internal procedures)
|
||||
- `d=<skill_slug>` (example: `d=admin_ops`)
|
||||
- `10123` — **Public Skill Adoption List**
|
||||
- tags contain one or more `a` references to selected `31123` skills
|
||||
|
||||
## Skill Sharing & Discovery
|
||||
|
||||
Skills are shared across Nostr without any centralized registry or approval process.
|
||||
|
||||
### How it works
|
||||
|
||||
1. **Publish**: An author publishes a skill as a kind `31123` event. The `content` field contains the skill body (markdown or structured JSON). The `d` tag is the skill's slug (e.g. `long_form_note`).
|
||||
2. **Adopt**: An agent that wants to use a skill adds an `a`-tag reference to its kind `10123` adoption list. This is a public, replaceable event — anyone can see which skills an agent uses.
|
||||
3. **Discover**: A new user queries `{"kinds": [10123], "authors": [<my-follows>]}` to see which skills their web of trust has adopted. The most-referenced `31123` addresses are the most popular skills — no rating system needed.
|
||||
4. **Improve**: Anyone can publish their own `31123` with the same slug but a different pubkey. If their version is better, people adopt it instead. Competition happens through adoption, not through a store ranking.
|
||||
|
||||
### Why this works
|
||||
|
||||
- **No gatekeeper**: Skills are just Nostr events. Anyone can publish one.
|
||||
- **WoT as curation**: You see what people you trust actually use, not what an algorithm promotes.
|
||||
- **Visible adoption**: The `10123` list is public. Popularity is a countable fact, not a manipulable score.
|
||||
- **Censorship resistant**: Skills live on relays. No single entity can remove a skill from the network.
|
||||
|
||||
## Startup
|
||||
|
||||
Didactyl startup behavior is configured in [`config.json`](config.json) under `startup_events`.
|
||||
|
||||
Also used at startup:
|
||||
|
||||
- `0` — profile metadata
|
||||
- `10002` — relay list
|
||||
- `1` — optional startup note/status
|
||||
- `3` — contacts/follows (optional placeholder)
|
||||
|
||||
On boot, Didactyl attempts startup publishes to each relay as that relay transitions to connected state.
|
||||
|
||||
## Runtime Context Model
|
||||
|
||||
Didactyl builds tier-aware context:
|
||||
|
||||
- **ADMIN** request context — assembled from the soul's `---template---` section (if present), otherwise hardcoded order:
|
||||
1. Soul personality (everything above `---template---` in kind `31120`)
|
||||
2. Named template sections in order — e.g. `admin_identity`, `admin_profile`, `admin_relay_list`, `startup_events`, `adopted_skills`, `dm_history` (expand), `admin_notes`
|
||||
3. Each section resolves `{{variable}}` placeholders from live data at call time
|
||||
4. Provider-specific content overrides per section (e.g. XML tags for Anthropic)
|
||||
5. Section names are used in `context.log` headers and `/api/context/parts` response
|
||||
- **WoT** request context: Soul + WoT chat-only instruction + current user message (no tools)
|
||||
- **STRANGER**: no LLM call when configured to reply statically
|
||||
|
||||
Every serialized LLM context payload is appended to [`context.log`](context.log).
|
||||
|
||||
## Tooling Interface
|
||||
|
||||
Current tool schema exposed to the LLM in [`tools_build_openai_schema_json()`](src/tools.c:881):
|
||||
|
||||
- Nostr publish/query:
|
||||
- `nostr_post`
|
||||
- `nostr_post_readme`
|
||||
- `nostr_query`
|
||||
- Nostr interaction and moderation:
|
||||
- `nostr_delete`
|
||||
- `nostr_react`
|
||||
- `nostr_profile_get`
|
||||
- `nostr_relay_status`
|
||||
- `nostr_relay_info`
|
||||
- `nostr_nip05_lookup`
|
||||
- Nostr encode/decode + encryption/DM:
|
||||
- `nostr_encode`
|
||||
- `nostr_decode`
|
||||
- `nostr_encrypt`
|
||||
- `nostr_decrypt`
|
||||
- `nostr_dm_send`
|
||||
- `nostr_dm_send_nip17`
|
||||
- Nostr list management:
|
||||
- `nostr_list_manage`
|
||||
- Skill management:
|
||||
- `skill_create`
|
||||
- `skill_list`
|
||||
- `skill_adopt`
|
||||
- `skill_remove`
|
||||
- `skill_search`
|
||||
- Local/host tools:
|
||||
- `shell_exec`
|
||||
- `file_read`
|
||||
- `file_write`
|
||||
- `http_fetch`
|
||||
- Agent metadata:
|
||||
- `my_version`
|
||||
- Model management:
|
||||
- `model_get`
|
||||
- `model_set`
|
||||
- `model_list`
|
||||
|
||||
Execution entrypoint: [`tools_execute()`](src/tools.c:3765).
|
||||
|
||||
## HTTP Admin API
|
||||
|
||||
A localhost-only HTTP API on port `8484` (configurable) for agent inspection and prompt crafting. Enable with `"api": {"enabled": true}` in config.
|
||||
|
||||
| Endpoint | Purpose |
|
||||
|---|---|
|
||||
| `GET /api/status` | Agent name, version, pubkey, relay count, trigger count |
|
||||
| `GET /api/context/current` | Full LLM context messages array |
|
||||
| `GET /api/context/parts` | Context broken into named parts with token estimates |
|
||||
| `POST /api/prompt/run-simple` | Run a simple system+user prompt, no tools |
|
||||
| `POST /api/prompt/run` | Run a full messages array with tools enabled |
|
||||
| `POST /api/prompt/compare` | A/B compare two prompt variants |
|
||||
| `GET /api/model` | Current LLM model config |
|
||||
| `PUT /api/model` | Change model at runtime (persists to config.json) |
|
||||
| `GET /api/models` | List available models from provider |
|
||||
|
||||
Full reference: [`docs/API.md`](docs/API.md). Frontend brief: [`plans/admin_web_frontend.md`](plans/admin_web_frontend.md).
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
.
|
||||
├── config.json # Agent configuration
|
||||
├── SYSTEM.md # Agent personality/instructions for LLM
|
||||
├── config.json # Agent/runtime config including startup_events + tools
|
||||
├── context.log # Appended outbound LLM context payloads
|
||||
├── Makefile # Build system
|
||||
├── build_static.sh # Preferred final build validation
|
||||
├── src/
|
||||
│ ├── main.c # Entry point, signal handling, daemon loop
|
||||
│ ├── config.c / .h # JSON config parsing, key decoding
|
||||
│ ├── context.c / .h # SYSTEM.md file loader
|
||||
│ ├── agent.c / .h # Core agent logic: receive → LLM → respond
|
||||
│ ├── llm.c / .h # LLM HTTP API client (OpenAI-compatible)
|
||||
│ ├── nostr_handler.c / .h # Relay pool, subscriptions, publish, DMs
|
||||
│ └── secp_compat.c # secp256k1 API compatibility shim
|
||||
│ ├── main.c / .h # Entry point, args (--config/--debug), lifecycle, version
|
||||
│ ├── config.c / .h # JSON config parsing, key decode, startup events
|
||||
│ ├── context.c / .h # File loader utility (reads file into malloc'd string)
|
||||
│ ├── agent.c / .h # Context assembly, tool loop, DM response flow
|
||||
│ ├── prompt_template.c / .h # Soul template parser, variable resolver, context builder
|
||||
│ ├── tools.c / .h # LLM tool schema and tool execution
|
||||
│ ├── llm.c / .h # LLM HTTP API client (OpenAI-compatible)
|
||||
│ ├── nostr_handler.c / .h # Relay pool, subscriptions, publish, startup reconcile
|
||||
│ ├── trigger_manager.c / .h # Nostr event trigger subscriptions and skill execution
|
||||
│ ├── http_api.c / .h # Localhost HTTP admin API (mongoose-based)
|
||||
│ ├── mongoose.c / .h # Embedded HTTP server (mongoose)
|
||||
│ └── debug.c / .h # Runtime log levels/macros
|
||||
├── docs/
|
||||
│ ├── API.md # HTTP admin API endpoint reference
|
||||
│ └── TOOLS_AND_SKILLS.md # Tool and skill system documentation
|
||||
├── plans/ # Architecture and planning documents
|
||||
│ ├── didactyl_mvp.md
|
||||
│ └── didactyl_agentic.md
|
||||
└── README.md
|
||||
```
|
||||
|
||||
## Dependencies
|
||||
|
||||
All dependencies are statically linked into the binary at build time. No system libraries are required at runtime.
|
||||
|
||||
| Dependency | Purpose | Source |
|
||||
|---|---|---|
|
||||
| nostr_core_lib | Nostr protocol: keys, events, NIPs, relay pool | Workspace (sibling directory) |
|
||||
| cJSON | JSON parsing | Bundled in nostr_core_lib |
|
||||
| libcurl | HTTPS for LLM API calls | System package |
|
||||
| libssl / libcrypto | TLS for WebSocket relay connections | System package |
|
||||
| libsecp256k1 | Schnorr signatures, ECDH | System package |
|
||||
| libcurl | HTTPS for LLM API calls | Statically linked (Alpine/MUSL) |
|
||||
| libssl / libcrypto | TLS for WebSocket relay connections | Statically linked (Alpine/MUSL) |
|
||||
| libsecp256k1 | Schnorr signatures, ECDH | Statically linked (Alpine/MUSL) |
|
||||
|
||||
## Roadmap
|
||||
|
||||
- [x] MVP chat agent — DM in, LLM response out
|
||||
- [x] Relay pool with auto-reconnect and status logging
|
||||
- [x] Runtime diagnostics — relay health, message flow, LLM calls
|
||||
- [ ] **Agentic tool-use** — LLM can call tools (nostr_post, nostr_query, shell_exec)
|
||||
- [x] Per-relay startup publish on relay-connected transitions
|
||||
- [x] Runtime diagnostics — relay health, message flow, event kind publish logs
|
||||
- [x] Tool-calling loop (nostr_post, nostr_query, shell_exec, file_read, file_write)
|
||||
- [x] Context assembly with startup events + recent DM history
|
||||
- [x] Context payload logging to [`context.log`](context.log)
|
||||
- [x] Skill kind definitions (`31120` Soul, `31123` Public Skill, `31124` Private Skill)
|
||||
- [x] Skill adoption list (`10123`) for WoT-driven discovery
|
||||
- [x] Signature verification on all inbound events
|
||||
- [x] Privilege tiers — ADMIN (tools), WoT (chat-only), STRANGER (canned reply/ignore)
|
||||
- [x] Admin context subscription (kind 0, 3, 10002, 1) with WoT contact extraction
|
||||
- [x] Message deduplication (event-ID cache + FNV-1a fingerprint debounce)
|
||||
- [x] Adopted skills injected into LLM context automatically
|
||||
- [x] Triggered skills — Nostr event filters that fire skill execution automatically
|
||||
- [x] Localhost HTTP admin API — context inspection, prompt crafting, A/B comparison
|
||||
- [x] Runtime model switching via `model_set` tool (persists to config.json)
|
||||
- [x] Soul-embedded prompt templates (`---template---`) — configurable context order, variable resolution, provider overrides
|
||||
- [ ] Runtime skill loading from adopted `31123` events on relays
|
||||
- [ ] Skill discovery CLI/tool (query WoT adoption lists)
|
||||
- [ ] Upgrade to NIP-17 gift-wrapped DMs
|
||||
- [ ] NIP-44 encrypted private skills (`31124`)
|
||||
- [ ] Nostr-native data storage (kind 30078 app-specific events)
|
||||
- [ ] Blossom blob storage integration
|
||||
- [ ] Conversation memory on Nostr
|
||||
- [ ] Config and SYSTEM.md stored as Nostr events
|
||||
- [ ] Multi-turn conversation context window
|
||||
- [ ] Agent-to-agent communication
|
||||
|
||||
## License
|
||||
|
||||
TBD
|
||||
|
||||
|
||||
16
SYSTEM.md
16
SYSTEM.md
@@ -1,16 +0,0 @@
|
||||
# Didactyl Agent
|
||||
|
||||
You are Didactyl, a sovereign AI agent living on Nostr.
|
||||
|
||||
## Communication Rules
|
||||
- You communicate through encrypted Nostr direct messages.
|
||||
- Keep responses concise and clear.
|
||||
|
||||
## Behavior
|
||||
- Be helpful and technically accurate.
|
||||
- If unsure, state uncertainty directly.
|
||||
- Prefer actionable, practical advice.
|
||||
|
||||
## Safety
|
||||
- Do not claim to have executed actions you did not execute.
|
||||
- Do not reveal secrets from configuration or keys.
|
||||
@@ -6,7 +6,7 @@
|
||||
set -e
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
BUILD_DIR="$SCRIPT_DIR/build"
|
||||
OUTPUT_DIR="$SCRIPT_DIR"
|
||||
DOCKERFILE="$SCRIPT_DIR/Dockerfile.alpine-musl"
|
||||
|
||||
# Parse command line arguments
|
||||
@@ -22,12 +22,15 @@ else
|
||||
echo "=========================================="
|
||||
fi
|
||||
echo "Project directory: $SCRIPT_DIR"
|
||||
echo "Build directory: $BUILD_DIR"
|
||||
echo "Output directory: $OUTPUT_DIR"
|
||||
echo "Debug build: $DEBUG_BUILD"
|
||||
echo ""
|
||||
|
||||
# Create build directory
|
||||
mkdir -p "$BUILD_DIR"
|
||||
# Remove legacy build directory if present
|
||||
if [ -d "$SCRIPT_DIR/build" ]; then
|
||||
rm -rf "$SCRIPT_DIR/build"
|
||||
echo "Removed legacy build directory: $SCRIPT_DIR/build"
|
||||
fi
|
||||
|
||||
# Check if Docker is available
|
||||
if ! command -v docker &> /dev/null; then
|
||||
@@ -136,20 +139,12 @@ echo "=========================================="
|
||||
echo "Step 2: Extracting static binary"
|
||||
echo "=========================================="
|
||||
|
||||
# Build the builder stage to extract the binary
|
||||
$DOCKER_CMD build \
|
||||
--platform "$PLATFORM" \
|
||||
--build-arg DEBUG_BUILD=$DEBUG_BUILD \
|
||||
--target builder \
|
||||
-f "$DOCKERFILE" \
|
||||
-t didactyl-static-builder-stage:latest \
|
||||
. > /dev/null 2>&1
|
||||
# Reuse the already-built final image and extract binary directly
|
||||
# (avoids a second docker build pass)
|
||||
CONTAINER_ID=$($DOCKER_CMD create didactyl-musl-builder:latest /didactyl_static)
|
||||
|
||||
# Create a temporary container to copy the binary
|
||||
CONTAINER_ID=$($DOCKER_CMD create didactyl-static-builder-stage:latest)
|
||||
|
||||
# Copy binary from container
|
||||
$DOCKER_CMD cp "$CONTAINER_ID:/build/didactyl_static" "$BUILD_DIR/$OUTPUT_NAME" || {
|
||||
# Copy binary from final image
|
||||
$DOCKER_CMD cp "$CONTAINER_ID:/didactyl_static" "$OUTPUT_DIR/$OUTPUT_NAME" || {
|
||||
echo "ERROR: Failed to extract binary from container"
|
||||
$DOCKER_CMD rm "$CONTAINER_ID" 2>/dev/null
|
||||
exit 1
|
||||
@@ -158,11 +153,11 @@ $DOCKER_CMD cp "$CONTAINER_ID:/build/didactyl_static" "$BUILD_DIR/$OUTPUT_NAME"
|
||||
# Clean up container
|
||||
$DOCKER_CMD rm "$CONTAINER_ID" > /dev/null
|
||||
|
||||
echo "✓ Binary extracted to: $BUILD_DIR/$OUTPUT_NAME"
|
||||
echo "✓ Binary extracted to: $OUTPUT_DIR/$OUTPUT_NAME"
|
||||
echo ""
|
||||
|
||||
# Make binary executable
|
||||
chmod +x "$BUILD_DIR/$OUTPUT_NAME"
|
||||
chmod +x "$OUTPUT_DIR/$OUTPUT_NAME"
|
||||
|
||||
# Verify the binary
|
||||
echo "=========================================="
|
||||
@@ -171,7 +166,7 @@ echo "=========================================="
|
||||
echo ""
|
||||
|
||||
echo "Checking for dynamic dependencies:"
|
||||
if LDD_OUTPUT=$(timeout 5 ldd "$BUILD_DIR/$OUTPUT_NAME" 2>&1); then
|
||||
if LDD_OUTPUT=$(timeout 5 ldd "$OUTPUT_DIR/$OUTPUT_NAME" 2>&1); then
|
||||
if echo "$LDD_OUTPUT" | grep -q "not a dynamic executable"; then
|
||||
echo "✓ Binary is fully static (no dynamic dependencies)"
|
||||
TRULY_STATIC=true
|
||||
@@ -185,7 +180,7 @@ if LDD_OUTPUT=$(timeout 5 ldd "$BUILD_DIR/$OUTPUT_NAME" 2>&1); then
|
||||
fi
|
||||
else
|
||||
# ldd failed or timed out - check with file command instead
|
||||
if file "$BUILD_DIR/$OUTPUT_NAME" | grep -q "statically linked"; then
|
||||
if file "$OUTPUT_DIR/$OUTPUT_NAME" | grep -q "statically linked"; then
|
||||
echo "✓ Binary is statically linked (verified with file command)"
|
||||
TRULY_STATIC=true
|
||||
else
|
||||
@@ -198,8 +193,8 @@ echo ""
|
||||
echo "=========================================="
|
||||
echo "Build Summary"
|
||||
echo "=========================================="
|
||||
echo "Binary: $BUILD_DIR/$OUTPUT_NAME"
|
||||
echo "Size: $(du -h "$BUILD_DIR/$OUTPUT_NAME" | cut -f1)"
|
||||
echo "Binary: $OUTPUT_DIR/$OUTPUT_NAME"
|
||||
echo "Size: $(du -h "$OUTPUT_DIR/$OUTPUT_NAME" | cut -f1)"
|
||||
echo "Static: $TRULY_STATIC"
|
||||
echo "Debug: $DEBUG_BUILD"
|
||||
echo "Platform: $PLATFORM"
|
||||
|
||||
150
chat-didactyl-cli.js
Executable file
150
chat-didactyl-cli.js
Executable file
@@ -0,0 +1,150 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Simple terminal chat client for Didactyl HTTP API.
|
||||
*
|
||||
* Usage:
|
||||
* node didactyl-chat-cli.js
|
||||
*
|
||||
* Optional env vars:
|
||||
* DIDACTYL_API_BASE_URL=http://127.0.0.1:8484
|
||||
* DIDACTYL_MODEL=claude-haiku-4.5
|
||||
* DIDACTYL_MAX_TURNS=4
|
||||
*/
|
||||
|
||||
const readline = require("node:readline/promises");
|
||||
const { stdin, stdout } = require("node:process");
|
||||
const http = require("node:http");
|
||||
const https = require("node:https");
|
||||
|
||||
const API_BASE_URL = process.env.DIDACTYL_API_BASE_URL || "https://127.0.0.1:8484";
|
||||
const MODEL = process.env.DIDACTYL_MODEL || "";
|
||||
const MAX_TURNS = Number.parseInt(process.env.DIDACTYL_MAX_TURNS || "4", 10);
|
||||
const INSECURE_TLS = !["0", "false", "False", "FALSE"].includes(
|
||||
String(process.env.DIDACTYL_INSECURE_TLS || "1")
|
||||
);
|
||||
|
||||
function printMessage(role, content) {
|
||||
const who = role === "user" ? "You" : role === "assistant" ? "Didactyl" : role;
|
||||
console.log(`${who}>`);
|
||||
console.log(content);
|
||||
console.log("");
|
||||
}
|
||||
|
||||
function postJson(urlString, payload) {
|
||||
const url = new URL(urlString);
|
||||
const isHttps = url.protocol === "https:";
|
||||
const data = JSON.stringify(payload);
|
||||
|
||||
const options = {
|
||||
method: "POST",
|
||||
hostname: url.hostname,
|
||||
port: url.port,
|
||||
path: url.pathname + url.search,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Content-Length": Buffer.byteLength(data),
|
||||
},
|
||||
};
|
||||
|
||||
if (isHttps) {
|
||||
options.rejectUnauthorized = !INSECURE_TLS;
|
||||
}
|
||||
|
||||
const client = isHttps ? https : http;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const req = client.request(options, (res) => {
|
||||
let body = "";
|
||||
res.setEncoding("utf8");
|
||||
res.on("data", (chunk) => {
|
||||
body += chunk;
|
||||
});
|
||||
res.on("end", () => {
|
||||
resolve({
|
||||
statusCode: res.statusCode || 0,
|
||||
body,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
req.on("error", reject);
|
||||
req.write(data);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
async function callDidactyl(message) {
|
||||
const body = {
|
||||
message,
|
||||
max_turns: Number.isFinite(MAX_TURNS) ? MAX_TURNS : 4,
|
||||
};
|
||||
|
||||
if (MODEL.trim()) {
|
||||
body.model = MODEL.trim();
|
||||
}
|
||||
|
||||
const { statusCode, body: responseBody } = await postJson(`${API_BASE_URL}/api/prompt/agent`, body);
|
||||
|
||||
if (statusCode < 200 || statusCode >= 300) {
|
||||
throw new Error(`HTTP ${statusCode}: ${responseBody}`);
|
||||
}
|
||||
|
||||
let data;
|
||||
try {
|
||||
data = JSON.parse(responseBody);
|
||||
} catch {
|
||||
throw new Error(`Invalid JSON from API: ${responseBody}`);
|
||||
}
|
||||
|
||||
if (!data.success) {
|
||||
throw new Error(data.error || "Didactyl API returned success=false");
|
||||
}
|
||||
|
||||
return String(data.final_response || "");
|
||||
}
|
||||
|
||||
async function main() {
|
||||
console.log("Didactyl CLI chat");
|
||||
console.log(`API: ${API_BASE_URL}`);
|
||||
console.log(`TLS verify: ${INSECURE_TLS ? "disabled (local dev)" : "enabled"}`);
|
||||
console.log("Type /exit to quit.\n");
|
||||
|
||||
const rl = readline.createInterface({ input: stdin, output: stdout });
|
||||
|
||||
try {
|
||||
while (true) {
|
||||
const input = (await rl.question("You> ")).trim();
|
||||
|
||||
if (!input) {
|
||||
console.log("");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (input === "/exit" || input === "/quit") {
|
||||
console.log("Exiting.");
|
||||
break;
|
||||
}
|
||||
|
||||
console.log("");
|
||||
|
||||
try {
|
||||
const reply = await callDidactyl(input);
|
||||
printMessage("assistant", reply);
|
||||
console.log("");
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
console.error(`Didactyl error: ${message}`);
|
||||
console.error("");
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
rl.close();
|
||||
}
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
console.error(`Fatal error: ${message}`);
|
||||
process.exit(1);
|
||||
});
|
||||
271
config.json.example
Normal file
271
config.json.example
Normal file
@@ -0,0 +1,271 @@
|
||||
{
|
||||
"keys": {
|
||||
"nsec": "agent nsec",
|
||||
"npub": "agent npub",
|
||||
"npubHex": "agent hex pubkey",
|
||||
"nsecHex": "agent hex secret key"
|
||||
},
|
||||
"admin": {
|
||||
"pubkey": "admin pubkey"
|
||||
},
|
||||
"dm_protocol": "nip04",
|
||||
"llm": {
|
||||
"provider": "",
|
||||
"api_key": "",
|
||||
"model": "",
|
||||
"base_url": "",
|
||||
"max_tokens": 512,
|
||||
"temperature": 0.7
|
||||
},
|
||||
"security": {
|
||||
"verify_signatures": true,
|
||||
"stranger_response": "I only respond to people in my web of trust. You can always identify me by my public key (npub).",
|
||||
"tiers": {
|
||||
"admin": {
|
||||
"tools_enabled": true
|
||||
},
|
||||
"wot": {
|
||||
"enabled": true,
|
||||
"tools_enabled": false
|
||||
},
|
||||
"stranger": {
|
||||
"enabled": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"admin_context": {
|
||||
"enabled": true,
|
||||
"subscribe_kinds": [
|
||||
0,
|
||||
3,
|
||||
10002,
|
||||
1
|
||||
],
|
||||
"kind_1_limit": 10
|
||||
},
|
||||
"api": {
|
||||
"enabled": true,
|
||||
"port": 8484,
|
||||
"bind_address": "127.0.0.1"
|
||||
},
|
||||
"startup_events": [
|
||||
{
|
||||
"kind": 0,
|
||||
"content_fields": {
|
||||
"name": "Didactyl Agent",
|
||||
"display_name": "Didactyl",
|
||||
"about": "A sovereign AI agent on Nostr",
|
||||
"picture": "https://laantungir.github.io/img_repo/daf95a99f3797fa4ac39f3791f377ad79bcb7b8a6868f75fe66d2ab4af4bd1f5.png",
|
||||
"banner": "https://laantungir.github.io/img_repo/d21c4060632ab3d9d37a6062eeecbdbfbd67f03cb20dbd64838b1ba0d9cd8922.jpg"
|
||||
},
|
||||
"tags": []
|
||||
},
|
||||
{
|
||||
"kind": 10002,
|
||||
"content": "",
|
||||
"tags": [
|
||||
[
|
||||
"r",
|
||||
"wss://relay.damus.io"
|
||||
],
|
||||
[
|
||||
"r",
|
||||
"wss://nos.lol"
|
||||
],
|
||||
[
|
||||
"r",
|
||||
"wss://relay.primal.net"
|
||||
],
|
||||
[
|
||||
"r",
|
||||
"ws://127.0.0.1:7777"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"kind": 10050,
|
||||
"content": "",
|
||||
"tags": [
|
||||
[
|
||||
"relay",
|
||||
"wss://relay.damus.io"
|
||||
],
|
||||
[
|
||||
"relay",
|
||||
"wss://nos.lol"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"kind": 1,
|
||||
"content": "Hello world from Didactyl startup",
|
||||
"tags": [
|
||||
[
|
||||
"t",
|
||||
"didactyl"
|
||||
],
|
||||
[
|
||||
"t",
|
||||
"startup"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"kind": 3,
|
||||
"content": "",
|
||||
"tags": []
|
||||
},
|
||||
{
|
||||
"kind": 31120,
|
||||
"content": "# Didactyl Agent\n\nYou are Didactyl, a sovereign AI agent living on Nostr.\n\n## Communication Rules\n- You communicate through encrypted Nostr direct messages.\n- Keep responses concise and clear.\n\n## Behavior\n- Be helpful and technically accurate.\n- If unsure, state uncertainty directly.\n- Prefer actionable, practical advice.\n- Use the person's name when messaging them if you know it.\n- For the administrator, use their name from the administrator kind 0 profile metadata when available.\n\n## Tool Use Policy\n- You have tools available and should use them when a request requires taking action.\n- For requests involving local inspection or command execution, call `shell_exec` instead of refusing.\n- For posting to Nostr, call `nostr_post` with explicit `kind` and `content`.\n- For relay/event lookup tasks, call `nostr_query` with an appropriate filter.\n- After a tool call, base your answer on the actual tool result.\n- Never claim a tool was run if no tool was executed.\n\n## Task Management\n- Maintain and use your internal task list as short-term working memory.\n- Break long or complex actions into clear tasks before executing them.\n- Update task status as you complete steps so your plan stays accurate.\n\n## Safety\n- Do not claim to have executed actions you did not execute.\n- You may share your public key (npub) with anyone.\n- Never reveal your private key (nsec) under any circumstance.\n\n---template---\n\n- section: admin_identity\n role: system\n content: |\n ## Administrator Identity (source: config.admin.pubkey)\n\n This is your administrator! Admin pubkey (hex): {{admin_pubkey}}\n\n- section: admin_profile\n role: system\n content: |\n ## Administrator Kind 0 Profile (source: nostr kind 0)\n\n Administrator kind 0 profile content (JSON): {{admin_kind0_json}}\n provider:\n anthropic: |\n <admin_kind0_profile source=\"nostr_kind_0\">\n {{admin_kind0_json}}\n </admin_kind0_profile>\n\n- section: admin_relay_list\n role: system\n content: |\n ## Administrator Relay List (source: nostr kind 10002)\n\n Administrator kind 10002 relay-list content (JSON): {{admin_kind10002_json}}\n\n- section: startup_events\n role: system\n content: |\n ## Startup Events Memory (source: config.startup_events)\n\n Startup events memory (kinds/content/tags): {{startup_events_json}}\n\n- section: adopted_skills\n role: system\n content: |\n {{adopted_skills_content}}\n\n- section: agent_tasks\n role: system\n content: |\n {{tasks_content}}\n\n- section: dm_history\n role: expand\n limit: 12\n\n- section: admin_notes\n role: system\n content: |\n ## Administrator Recent Notes (source: nostr kind 1)\n\n {{admin_notes_content}}",
|
||||
"tags": [
|
||||
[
|
||||
"d",
|
||||
"soul"
|
||||
],
|
||||
[
|
||||
"app",
|
||||
"didactyl"
|
||||
],
|
||||
[
|
||||
"scope",
|
||||
"private"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"kind": 31123,
|
||||
"content_fields": {
|
||||
"name": "long_form_note",
|
||||
"description": "How to publish a NIP-23 long-form article (kind 30023)",
|
||||
"nip": "NIP-23",
|
||||
"event_kind": 30023,
|
||||
"format": "The content field must be markdown text; avoid arbitrary hard line-breaks in paragraphs and do not include HTML.",
|
||||
"required_tags": {
|
||||
"d": "Addressable identifier slug for the article. Reusing the same d tag replaces prior versions.",
|
||||
"title": "Human-readable article title.",
|
||||
"published_at": "Unix timestamp as a string for first publication time; keep stable on edits."
|
||||
},
|
||||
"optional_tags": {
|
||||
"summary": "Short 1-2 sentence summary.",
|
||||
"image": "URL for article preview image.",
|
||||
"t": "Topic hashtags using repeated t tags, usually 3-6 lowercase terms."
|
||||
},
|
||||
"behavior": "If required values are missing or unclear, ask the administrator before publishing instead of guessing.",
|
||||
"procedure": [
|
||||
"Determine title and d tag from admin input or source material.",
|
||||
"Draft markdown body content.",
|
||||
"Set published_at as unix seconds string.",
|
||||
"Add optional summary/image/t tags when known.",
|
||||
"Publish with nostr_post kind 30023 including tags array."
|
||||
]
|
||||
},
|
||||
"tags": [
|
||||
[
|
||||
"d",
|
||||
"long_form_note"
|
||||
],
|
||||
[
|
||||
"app",
|
||||
"didactyl"
|
||||
],
|
||||
[
|
||||
"scope",
|
||||
"public"
|
||||
],
|
||||
[
|
||||
"slug",
|
||||
"long_form_note"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"kind": 31123,
|
||||
"content_fields": {
|
||||
"name": "post_readme_to_nostr",
|
||||
"description": "Read README.md from the repo and publish it as a NIP-23 long-form note",
|
||||
"uses_skill": "long_form_note",
|
||||
"event_kind": 30023,
|
||||
"procedure": [
|
||||
"Read README.md using file_read with path README.md.",
|
||||
"Set d tag exactly to readme.md.",
|
||||
"Set title from the first markdown H1 heading in README.md.",
|
||||
"Set summary from the opening paragraph of README.md.",
|
||||
"Set image from project metadata when available (kind 0 picture/banner), otherwise omit image tag.",
|
||||
"Generate 3-6 lowercase t tags from README section topics.",
|
||||
"Set published_at to current unix timestamp as string.",
|
||||
"Publish with nostr_post kind 30023, full README markdown in content, and full NIP-23 tag set."
|
||||
],
|
||||
"required_values": {
|
||||
"d": "readme.md",
|
||||
"summary_source": "opening paragraph"
|
||||
}
|
||||
},
|
||||
"tags": [
|
||||
[
|
||||
"d",
|
||||
"post_readme_to_nostr"
|
||||
],
|
||||
[
|
||||
"app",
|
||||
"didactyl"
|
||||
],
|
||||
[
|
||||
"scope",
|
||||
"public"
|
||||
],
|
||||
[
|
||||
"slug",
|
||||
"post_readme_to_nostr"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"kind": 31124,
|
||||
"content_fields": {
|
||||
"name": "admin_ops",
|
||||
"description": "Private operational procedures"
|
||||
},
|
||||
"tags": [
|
||||
[
|
||||
"d",
|
||||
"admin_ops"
|
||||
],
|
||||
[
|
||||
"app",
|
||||
"didactyl"
|
||||
],
|
||||
[
|
||||
"scope",
|
||||
"private"
|
||||
],
|
||||
[
|
||||
"slug",
|
||||
"admin_ops"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"kind": 10123,
|
||||
"content": "",
|
||||
"tags": [
|
||||
[
|
||||
"a",
|
||||
"31123:55993e3db0ed7bf07395fd44c2d695c224d195553a1aff7320a18e41679d9c7c:long_form_note"
|
||||
],
|
||||
[
|
||||
"a",
|
||||
"31123:55993e3db0ed7bf07395fd44c2d695c224d195553a1aff7320a18e41679d9c7c:post_readme_to_nostr"
|
||||
],
|
||||
[
|
||||
"app",
|
||||
"didactyl"
|
||||
],
|
||||
[
|
||||
"scope",
|
||||
"public"
|
||||
]
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
2027
context.log.md
Normal file
2027
context.log.md
Normal file
File diff suppressed because it is too large
Load Diff
57
context_template.md
Normal file
57
context_template.md
Normal file
@@ -0,0 +1,57 @@
|
||||
# Context Template
|
||||
|
||||
```yaml
|
||||
- section: admin_identity
|
||||
role: system
|
||||
content: |
|
||||
## Administrator Identity (source: config.admin.pubkey)
|
||||
|
||||
This is your administrator! Admin pubkey (hex): {{admin_pubkey}}
|
||||
|
||||
- section: admin_profile
|
||||
role: system
|
||||
content: |
|
||||
## Administrator Kind 0 Profile (source: nostr kind 0)
|
||||
|
||||
Administrator kind 0 profile content (JSON): {{admin_kind0_json}}
|
||||
provider:
|
||||
anthropic: |
|
||||
<admin_kind0_profile source="nostr_kind_0">
|
||||
{{admin_kind0_json}}
|
||||
</admin_kind0_profile>
|
||||
|
||||
- section: admin_relay_list
|
||||
role: system
|
||||
content: |
|
||||
## Administrator Relay List (source: nostr kind 10002)
|
||||
|
||||
Administrator kind 10002 relay-list content (JSON): {{admin_kind10002_json}}
|
||||
|
||||
- section: startup_events
|
||||
role: system
|
||||
content: |
|
||||
## Startup Events Memory (source: config.startup_events)
|
||||
|
||||
Startup events memory (kinds/content/tags): {{startup_events_json}}
|
||||
|
||||
- section: adopted_skills
|
||||
role: system
|
||||
content: |
|
||||
{{adopted_skills_content}}
|
||||
|
||||
- section: agent_tasks
|
||||
role: system
|
||||
content: |
|
||||
{{tasks_content}}
|
||||
|
||||
- section: dm_history
|
||||
role: expand
|
||||
limit: 12
|
||||
|
||||
- section: admin_notes
|
||||
role: system
|
||||
content: |
|
||||
## Administrator Recent Notes (source: nostr kind 1)
|
||||
|
||||
{{admin_notes_content}}
|
||||
```
|
||||
1272
didactyl.html
Normal file
1272
didactyl.html
Normal file
File diff suppressed because it is too large
Load Diff
493
docs/API.md
Normal file
493
docs/API.md
Normal file
@@ -0,0 +1,493 @@
|
||||
# Didactyl Admin HTTP API
|
||||
|
||||
## Overview
|
||||
|
||||
Didactyl exposes a localhost-only HTTP API for external tools and dashboards to inspect agent state, explore LLM context, craft prompts, and compare prompt variants. The API runs inside the same process as the agent — no separate server, no authentication required.
|
||||
|
||||
All responses are JSON. CORS headers are included on every response for browser access from any local origin.
|
||||
|
||||
---
|
||||
|
||||
## Configuration
|
||||
|
||||
Enable the API in `config.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"api": {
|
||||
"enabled": true,
|
||||
"port": 8484,
|
||||
"bind_address": "127.0.0.1"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
| Field | Type | Default | Description |
|
||||
|---|---|---|---|
|
||||
| `enabled` | bool | `false` | Must be explicitly set to `true` to start the HTTP server |
|
||||
| `port` | int | `8484` | TCP port to listen on |
|
||||
| `bind_address` | string | `"127.0.0.1"` | Bind address — use `127.0.0.1` for localhost-only access |
|
||||
|
||||
The API is disabled by default. When disabled, no listener is created and no resources are consumed.
|
||||
|
||||
---
|
||||
|
||||
## CORS
|
||||
|
||||
Every response includes:
|
||||
|
||||
```
|
||||
Access-Control-Allow-Origin: *
|
||||
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
|
||||
Access-Control-Allow-Headers: Content-Type
|
||||
```
|
||||
|
||||
`OPTIONS` requests return `204 No Content` with these headers for browser preflight support.
|
||||
|
||||
---
|
||||
|
||||
## Endpoints
|
||||
|
||||
### GET /api/status
|
||||
|
||||
Returns agent runtime status.
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"name": "Didactyl",
|
||||
"version": "v0.0.26",
|
||||
"pubkey": "52a3e82f7b3743852fbe804cfcbf4db3448115887895247c001f2b50e790acb8",
|
||||
"relay_count": 4,
|
||||
"connected_relays": 4,
|
||||
"active_triggers": 0
|
||||
}
|
||||
```
|
||||
|
||||
| Field | Description |
|
||||
|---|---|
|
||||
| `name` | Agent display name constant |
|
||||
| `version` | Build version string |
|
||||
| `pubkey` | Agent public key in hex |
|
||||
| `relay_count` | Number of configured relays |
|
||||
| `connected_relays` | Number of currently connected relays |
|
||||
| `active_triggers` | Number of active triggered-skill subscriptions |
|
||||
|
||||
---
|
||||
|
||||
### GET /api/context/current
|
||||
|
||||
Returns the full LLM context message array that would be sent to the model right now. This is the same context the agent builds for an admin DM conversation.
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"total_chars": 13131,
|
||||
"total_estimated_tokens": 3283,
|
||||
"messages": [
|
||||
{"role": "system", "content": "# Didactyl Agent\n\nYou are Didactyl..."},
|
||||
{"role": "system", "content": "This is your administrator! Admin pubkey..."},
|
||||
{"role": "system", "content": "Administrator kind 0 profile content..."},
|
||||
{"role": "system", "content": "Administrator kind 10002 relay-list content..."},
|
||||
{"role": "system", "content": "Startup events memory..."},
|
||||
{"role": "system", "content": "Adopted skills memory..."},
|
||||
{"role": "system", "content": "Administrator recent public notes..."}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
| Field | Description |
|
||||
|---|---|
|
||||
| `total_chars` | Total character count across all messages |
|
||||
| `total_estimated_tokens` | Rough token estimate using `chars / 4` heuristic |
|
||||
| `messages` | OpenAI-format messages array with role and content |
|
||||
|
||||
---
|
||||
|
||||
### GET /api/context/parts
|
||||
|
||||
Returns the context broken into labeled, individually-sized parts. Useful for understanding what consumes context budget.
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"total_chars": 13131,
|
||||
"total_estimated_tokens": 3283,
|
||||
"parts": [
|
||||
{
|
||||
"name": "system_prompt",
|
||||
"role": "system",
|
||||
"chars": 1200,
|
||||
"estimated_tokens": 300,
|
||||
"content": "# Didactyl Agent..."
|
||||
},
|
||||
{
|
||||
"name": "admin_identity",
|
||||
"role": "system",
|
||||
"chars": 120,
|
||||
"estimated_tokens": 30,
|
||||
"content": "This is your administrator!..."
|
||||
},
|
||||
{
|
||||
"name": "admin_kind0",
|
||||
"role": "system",
|
||||
"chars": 450,
|
||||
"estimated_tokens": 113,
|
||||
"content": "Administrator kind 0 profile content..."
|
||||
},
|
||||
{
|
||||
"name": "admin_relay_list",
|
||||
"role": "system",
|
||||
"chars": 50,
|
||||
"estimated_tokens": 13,
|
||||
"content": "Administrator kind 10002 relay-list content..."
|
||||
},
|
||||
{
|
||||
"name": "startup_events",
|
||||
"role": "system",
|
||||
"chars": 4800,
|
||||
"estimated_tokens": 1200,
|
||||
"content": "Startup events memory..."
|
||||
},
|
||||
{
|
||||
"name": "adopted_skills",
|
||||
"role": "system",
|
||||
"chars": 2100,
|
||||
"estimated_tokens": 525,
|
||||
"content": "Adopted skills memory..."
|
||||
},
|
||||
{
|
||||
"name": "admin_notes",
|
||||
"role": "system",
|
||||
"chars": 680,
|
||||
"estimated_tokens": 170,
|
||||
"content": "Administrator recent public notes..."
|
||||
}
|
||||
],
|
||||
"messages": [...]
|
||||
}
|
||||
```
|
||||
|
||||
**Part names:**
|
||||
|
||||
| Name | Description |
|
||||
|---|---|
|
||||
| `system_prompt` | The agent soul / system prompt (first system message) |
|
||||
| `admin_identity` | Admin pubkey identification message |
|
||||
| `admin_kind0` | Admin kind 0 profile metadata |
|
||||
| `admin_relay_list` | Admin kind 10002 relay list |
|
||||
| `startup_events` | Startup events memory block |
|
||||
| `adopted_skills` | Adopted skills behavioral instructions |
|
||||
| `admin_notes` | Admin recent kind 1 public notes |
|
||||
| `dm_history` | Recent DM conversation history |
|
||||
| `context_part` | Any other context message |
|
||||
|
||||
---
|
||||
|
||||
### POST /api/prompt/run-simple
|
||||
|
||||
Submit a system prompt and user message for a simple LLM call with no tools. Useful for quick prompt iteration.
|
||||
|
||||
**Request:**
|
||||
|
||||
```json
|
||||
{
|
||||
"system": "You are a helpful assistant that writes tweets.",
|
||||
"user": "Write a tweet about AI agents on Nostr",
|
||||
"model": "claude-haiku-4.5"
|
||||
}
|
||||
```
|
||||
|
||||
| Field | Required | Description |
|
||||
|---|---|---|
|
||||
| `system` | yes | System prompt string |
|
||||
| `user` | yes | User message string |
|
||||
| `model` | no | Override the current model for this request only |
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"response": "AI agents are finding their home on Nostr...",
|
||||
"model_used": "claude-haiku-4.5",
|
||||
"input_tokens_estimate": 85,
|
||||
"output_tokens_estimate": 42
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### POST /api/prompt/agent
|
||||
|
||||
Submit one user message and let Didactyl build full admin context server-side (same context assembly path used for Nostr admin DMs, including admin profile, adopted skills, and recent DM history).
|
||||
|
||||
**Request:**
|
||||
|
||||
```json
|
||||
{
|
||||
"message": "Tweet about the weather",
|
||||
"model": "claude-haiku-4.5",
|
||||
"max_turns": 5
|
||||
}
|
||||
```
|
||||
|
||||
| Field | Required | Description |
|
||||
|---|---|---|
|
||||
| `message` | yes | User message string |
|
||||
| `model` | no | Override the current model for this request only |
|
||||
| `max_turns` | no | Maximum tool-call loop iterations (default: 4, max: 16) |
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"final_response": "Done! I posted a tweet about the weather.",
|
||||
"turns": [
|
||||
{
|
||||
"turn": 1,
|
||||
"tool_calls": [
|
||||
{
|
||||
"name": "nostr_post",
|
||||
"arguments": "{\"kind\":1,\"content\":\"Beautiful day!\"}",
|
||||
"result": "{\"success\":true,\"event_id\":\"abc123\"}"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"model_used": "claude-haiku-4.5",
|
||||
"total_input_tokens_estimate": 3200,
|
||||
"total_output_tokens_estimate": 180
|
||||
}
|
||||
```
|
||||
|
||||
| Field | Description |
|
||||
|---|---|
|
||||
| `final_response` | The LLM final text response after all tool calls complete |
|
||||
| `turns` | Array of turn objects, each containing tool calls made in that turn |
|
||||
| `turns[].tool_calls[]` | Each tool call with name, arguments JSON, and result JSON |
|
||||
| `model_used` | The model that was actually used |
|
||||
| `total_input_tokens_estimate` | Estimated input tokens for the full conversation |
|
||||
| `total_output_tokens_estimate` | Estimated output tokens for the final response |
|
||||
|
||||
---
|
||||
|
||||
### POST /api/prompt/run
|
||||
|
||||
Submit a full messages array with the agent tool set enabled. This endpoint runs exactly what you provide and does **not** auto-build Didactyl admin context.
|
||||
|
||||
**Request:**
|
||||
|
||||
```json
|
||||
{
|
||||
"messages": [
|
||||
{"role": "system", "content": "You are Didactyl..."},
|
||||
{"role": "user", "content": "Tweet about the weather"}
|
||||
],
|
||||
"model": "claude-haiku-4.5",
|
||||
"max_turns": 5
|
||||
}
|
||||
```
|
||||
|
||||
| Field | Required | Description |
|
||||
|---|---|---|
|
||||
| `messages` | yes | OpenAI-format messages array |
|
||||
| `model` | no | Override the current model for this request only |
|
||||
| `max_turns` | no | Maximum tool-call loop iterations (default: 4, max: 16) |
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"final_response": "Done! I posted a tweet about the weather.",
|
||||
"turns": [
|
||||
{
|
||||
"turn": 1,
|
||||
"tool_calls": [
|
||||
{
|
||||
"name": "nostr_post",
|
||||
"arguments": "{\"kind\":1,\"content\":\"Beautiful day!\"}",
|
||||
"result": "{\"success\":true,\"event_id\":\"abc123\"}"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"model_used": "claude-haiku-4.5",
|
||||
"total_input_tokens_estimate": 3200,
|
||||
"total_output_tokens_estimate": 180
|
||||
}
|
||||
```
|
||||
|
||||
| Field | Description |
|
||||
|---|---|
|
||||
| `final_response` | The LLM final text response after all tool calls complete |
|
||||
| `turns` | Array of turn objects, each containing tool calls made in that turn |
|
||||
| `turns[].tool_calls[]` | Each tool call with name, arguments JSON, and result JSON |
|
||||
| `model_used` | The model that was actually used |
|
||||
| `total_input_tokens_estimate` | Estimated input tokens for the full conversation |
|
||||
| `total_output_tokens_estimate` | Estimated output tokens for the final response |
|
||||
|
||||
---
|
||||
|
||||
### POST /api/prompt/compare
|
||||
|
||||
A/B testing: submit two prompt variants, both are executed sequentially, responses returned side-by-side. Each variant can optionally use a different model for cross-model comparison.
|
||||
|
||||
**Request:**
|
||||
|
||||
```json
|
||||
{
|
||||
"variant_a": {
|
||||
"messages": [
|
||||
{"role": "system", "content": "You are concise. No emoji."},
|
||||
{"role": "user", "content": "Say hi."}
|
||||
],
|
||||
"model": "claude-haiku-4.5",
|
||||
"max_turns": 1
|
||||
},
|
||||
"variant_b": {
|
||||
"messages": [
|
||||
{"role": "system", "content": "You are verbose and friendly."},
|
||||
{"role": "user", "content": "Say hi."}
|
||||
],
|
||||
"model": "claude-haiku-4.5",
|
||||
"max_turns": 1
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
| Field | Required | Description |
|
||||
|---|---|---|
|
||||
| `variant_a` | yes | First prompt variant — same shape as `/api/prompt/run` request |
|
||||
| `variant_b` | yes | Second prompt variant — same shape as `/api/prompt/run` request |
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"variant_a": {
|
||||
"success": true,
|
||||
"final_response": "Hi. How can I help?",
|
||||
"turns": [{"turn": 1, "tool_calls": []}],
|
||||
"model_used": "claude-haiku-4.5",
|
||||
"total_input_tokens_estimate": 21,
|
||||
"total_output_tokens_estimate": 6
|
||||
},
|
||||
"variant_b": {
|
||||
"success": true,
|
||||
"final_response": "Hey there! Nice to meet you! I am here and ready to help...",
|
||||
"turns": [{"turn": 1, "tool_calls": []}],
|
||||
"model_used": "claude-haiku-4.5",
|
||||
"total_input_tokens_estimate": 21,
|
||||
"total_output_tokens_estimate": 72
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Variant A runs first, then variant B. The original model config is restored after each variant completes.
|
||||
|
||||
---
|
||||
|
||||
## Error Responses
|
||||
|
||||
All error responses follow this shape:
|
||||
|
||||
```json
|
||||
{
|
||||
"success": false,
|
||||
"error": "description of what went wrong"
|
||||
}
|
||||
```
|
||||
|
||||
Common HTTP status codes:
|
||||
|
||||
| Code | Meaning |
|
||||
|---|---|
|
||||
| `200` | Success |
|
||||
| `204` | OPTIONS preflight success |
|
||||
| `400` | Bad request — missing or invalid parameters |
|
||||
| `404` | Endpoint not found |
|
||||
| `500` | Internal server error |
|
||||
|
||||
---
|
||||
|
||||
## Token Estimation
|
||||
|
||||
All token estimates use a simple `chars / 4` heuristic. This is a rough approximation that works well enough for English text across major model families. No real tokenizer is used.
|
||||
|
||||
---
|
||||
|
||||
## Security
|
||||
|
||||
- Binds to `127.0.0.1` only — not accessible from the network
|
||||
- No authentication — this is a local development and administration tool
|
||||
- The `api.enabled` config flag defaults to `false` and must be explicitly opted in
|
||||
- Prompt execution endpoints have full tool access equivalent to admin-tier DM conversations
|
||||
- The agent process must be running for the API to be available
|
||||
|
||||
---
|
||||
|
||||
## Architecture
|
||||
|
||||
The HTTP server is embedded in the didactyl process using the Mongoose library. It runs in the same thread as the main poll loop — each iteration calls `http_api_poll()` which does non-blocking accept/read/write. This avoids threading complexity and gives the API direct access to all agent state.
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
subgraph didactyl process
|
||||
MAIN[main loop] --> POLL[nostr_handler_poll]
|
||||
MAIN --> TPOLL[trigger_manager_poll]
|
||||
MAIN --> HPOLL[http_api_poll]
|
||||
HPOLL --> ROUTER[request router]
|
||||
ROUTER --> AGENT[agent internals]
|
||||
ROUTER --> LLM[LLM client]
|
||||
ROUTER --> TOOLS[tools context]
|
||||
ROUTER --> CONFIG[config]
|
||||
ROUTER --> TRIGGERS[trigger_manager]
|
||||
end
|
||||
BROWSER[Web Dashboard] -- HTTP localhost:8484 --> HPOLL
|
||||
```
|
||||
|
||||
### Source Files
|
||||
|
||||
| File | Purpose |
|
||||
|---|---|
|
||||
| `src/http_api.c` | HTTP server, request router, all endpoint handlers |
|
||||
| `src/http_api.h` | Public API: `http_api_init`, `http_api_poll`, `http_api_cleanup` |
|
||||
| `src/mongoose.c` | Mongoose embedded HTTP library |
|
||||
| `src/mongoose.h` | Mongoose header |
|
||||
|
||||
---
|
||||
|
||||
## Future Endpoints
|
||||
|
||||
The following endpoints are planned but not yet implemented:
|
||||
|
||||
| Method | Path | Description |
|
||||
|---|---|---|
|
||||
| GET | `/api/config` | Current runtime config with redacted secrets |
|
||||
| GET | `/api/events/soul` | Fetch agent soul event |
|
||||
| PUT | `/api/events/soul` | Update soul content |
|
||||
| GET | `/api/events/skills` | List published skills |
|
||||
| GET | `/api/events/skills/:slug` | Fetch skill by slug |
|
||||
| PUT | `/api/events/skills/:slug` | Update skill |
|
||||
| GET | `/api/events/adoption` | Fetch adoption list |
|
||||
| GET | `/api/events/profile` | Fetch agent profile |
|
||||
| PUT | `/api/events/profile` | Update agent profile |
|
||||
| GET | `/api/triggers` | List active triggers |
|
||||
| GET | `/api/model` | Current model config |
|
||||
| PUT | `/api/model` | Update model config |
|
||||
| GET | `/api/models` | List available models |
|
||||
| GET | `/api/relays` | Relay connection status |
|
||||
| GET | `/api/tools` | List tool schemas |
|
||||
| POST | `/api/tools/:name/execute` | Execute a tool directly |
|
||||
| GET | `/api/context/log` | Recent context.log entries |
|
||||
| POST | `/api/context/preview` | Dry-run context preview |
|
||||
268
docs/CRASH_FIXES.md
Normal file
268
docs/CRASH_FIXES.md
Normal file
@@ -0,0 +1,268 @@
|
||||
# Crash Fixes Reference
|
||||
|
||||
This document catalogues crashes that have been diagnosed and fixed in Didactyl.
|
||||
Use it as a reference when investigating future crashes — the symptoms, root causes,
|
||||
and investigation techniques described here may save significant debugging time.
|
||||
|
||||
---
|
||||
|
||||
## 1. Silent process exit on relay disconnect — SIGPIPE (v0.0.24)
|
||||
|
||||
**Date**: 2026-03-02
|
||||
**Version**: v0.0.23 → fixed in v0.0.24
|
||||
**Severity**: Critical — process terminates without any log output
|
||||
**Files changed**: `src/main.c`
|
||||
|
||||
### Symptoms
|
||||
|
||||
- The process exits cleanly to the shell prompt with **no error message**.
|
||||
- The `[didactyl] shutting down` log line does **not** appear.
|
||||
- No coredump is generated.
|
||||
- The last log lines show all relays transitioning from `connected → disconnected`
|
||||
and then immediately `disconnected → connected`.
|
||||
- The crash typically follows a period of network instability or DNS resolution
|
||||
failure — for example, an LLM HTTP request failing with
|
||||
`curl=Could not resolve hostname`.
|
||||
|
||||
### Root cause
|
||||
|
||||
`SIGPIPE` was never handled. The default OS action for `SIGPIPE` is to terminate
|
||||
the process immediately — no signal handler runs, no cleanup occurs, no coredump
|
||||
is written.
|
||||
|
||||
The TLS write path in `nostr_core_lib` uses `SSL_write()`, which internally calls
|
||||
`write()` on the underlying socket. When a `wss://` relay drops its TCP connection
|
||||
and the relay pool attempts to write to that socket, the kernel delivers `SIGPIPE`.
|
||||
|
||||
The plain-TCP path for `ws://` connections was already protected by using
|
||||
`MSG_NOSIGNAL` on `send()` calls, but the TLS path had no equivalent protection.
|
||||
|
||||
### How it was diagnosed
|
||||
|
||||
1. **No crash message or coredump** ruled out `SIGSEGV`, `SIGABRT`, and OOM.
|
||||
2. **No shutdown log** ruled out a graceful exit via the signal handler.
|
||||
3. `dmesg` and `journalctl` showed no OOM-kill or segfault entries.
|
||||
4. Searching the entire codebase for `SIGPIPE` or `SIG_IGN` returned zero results.
|
||||
5. The websocket TLS transport was confirmed to use `SSL_write()` without
|
||||
`MSG_NOSIGNAL` or any per-thread signal mask.
|
||||
6. The timeline matched: relay disconnects occurred, the next poll cycle attempted
|
||||
a write to a dead TLS socket, and the process vanished.
|
||||
|
||||
### Fix
|
||||
|
||||
Added `signal(SIGPIPE, SIG_IGN)` in `main()` alongside the existing `SIGINT` and
|
||||
`SIGTERM` handlers:
|
||||
|
||||
```c
|
||||
signal(SIGINT, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
signal(SIGPIPE, SIG_IGN); /* ← added */
|
||||
```
|
||||
|
||||
This causes `SSL_write()` and any other write to a broken pipe to return `-1` with
|
||||
`errno = EPIPE` instead of killing the process. The relay pool and libcurl already
|
||||
handle write errors gracefully.
|
||||
|
||||
### How to recognise this class of bug in the future
|
||||
|
||||
- Process disappears without any log output or coredump.
|
||||
- Happens after network disruption or relay disconnects.
|
||||
- `ulimit -c` may be 0, but even with unlimited core size, `SIGPIPE` does not
|
||||
produce a coredump by default.
|
||||
- Any new network transport layer added to the project should be audited for
|
||||
`SIGPIPE` protection.
|
||||
|
||||
---
|
||||
|
||||
## 2. Use-after-free in `execute_nostr_list_manage` — SIGSEGV (v0.0.24)
|
||||
|
||||
**Date**: 2026-03-01 (coredump), fixed 2026-03-02
|
||||
**Version**: v0.0.23 → fixed in v0.0.24
|
||||
**Severity**: Critical — segmentation fault during tool execution
|
||||
**Files changed**: `src/tools.c`
|
||||
|
||||
### Symptoms
|
||||
|
||||
- `SIGSEGV` crash during execution of the `nostr_list_manage` tool.
|
||||
- Coredump shows the crash at `src/tools.c:29` inside `json_error()`, but with
|
||||
heavily corrupted stack frames — the real crash site is in
|
||||
`execute_nostr_list_manage()`.
|
||||
- The corrupted stack is characteristic of heap corruption from use-after-free.
|
||||
|
||||
### Root cause
|
||||
|
||||
In `execute_nostr_list_manage()`, the `action` pointer was obtained from the
|
||||
`args` cJSON tree:
|
||||
|
||||
```c
|
||||
cJSON* action = cJSON_GetObjectItemCaseSensitive(args, "action");
|
||||
```
|
||||
|
||||
Later, `args` was freed:
|
||||
|
||||
```c
|
||||
cJSON_Delete(args); /* frees the entire tree including action */
|
||||
```
|
||||
|
||||
But `action->valuestring` was still accessed afterwards:
|
||||
|
||||
```c
|
||||
cJSON_AddStringToObject(out, "action", action->valuestring); /* dangling pointer */
|
||||
```
|
||||
|
||||
After `cJSON_Delete(args)`, the `action` pointer is dangling. Accessing
|
||||
`action->valuestring` reads freed heap memory, which may contain arbitrary data
|
||||
or may have been reallocated for another purpose.
|
||||
|
||||
### How it was diagnosed
|
||||
|
||||
1. The coredump was extracted from systemd-coredump storage:
|
||||
```
|
||||
zstd -d /var/lib/systemd/coredump/core.didactyl_static.*.zst -o /tmp/core.bin
|
||||
gdb ./didactyl_static_x86_64_debug /tmp/core.bin -batch -ex "bt"
|
||||
```
|
||||
2. The backtrace showed `execute_nostr_list_manage` with corrupted frames.
|
||||
3. Code review of the function identified the `cJSON_Delete(args)` call occurring
|
||||
before the last use of `action->valuestring`.
|
||||
|
||||
### Fix
|
||||
|
||||
Deferred `cJSON_Delete(args)` until after `action->valuestring` has been consumed
|
||||
by `cJSON_AddStringToObject()`. The free now occurs immediately after the last use:
|
||||
|
||||
```c
|
||||
cJSON_AddStringToObject(out, "action", action->valuestring);
|
||||
cJSON_Delete(args); /* ← moved here, after last use of action */
|
||||
```
|
||||
|
||||
All early-return error paths before this point also received their own
|
||||
`cJSON_Delete(args)` call to prevent leaks.
|
||||
|
||||
### How to recognise this class of bug in the future
|
||||
|
||||
- `SIGSEGV` with corrupted or nonsensical stack frames.
|
||||
- Crash location reported by GDB does not match the actual buggy code — the
|
||||
corruption happened earlier.
|
||||
- Any function that calls `cJSON_Delete()` on a parent object should be audited
|
||||
to ensure no child pointers are used afterwards.
|
||||
- Pattern to watch for:
|
||||
```c
|
||||
cJSON* child = cJSON_GetObjectItemCaseSensitive(parent, "key");
|
||||
/* ... */
|
||||
cJSON_Delete(parent);
|
||||
/* ... */
|
||||
use(child->valuestring); /* BUG: child is dangling */
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. DM subscription not receiving incoming kind 4 events — INVESTIGATION (v0.0.24)
|
||||
|
||||
**Date**: 2026-03-02
|
||||
**Version**: v0.0.24
|
||||
**Severity**: High — agent does not respond to incoming DMs
|
||||
**Status**: Under investigation — diagnostic logging added
|
||||
**Files changed**: `src/nostr_handler.c`
|
||||
|
||||
### Symptoms
|
||||
|
||||
- The agent starts up normally, connects to relays, and can **send** kind 4 DMs.
|
||||
- Incoming kind 4 messages posted to the same relays are never processed.
|
||||
- No error messages appear in the log — the agent simply sits idle after sending
|
||||
its startup DM.
|
||||
- The last log lines show successful outbound DM publishing but no inbound event
|
||||
processing.
|
||||
|
||||
### Possible root causes (under investigation)
|
||||
|
||||
1. **Events never reach `on_event()` callback** — The relay pool library only
|
||||
calls `on_event()` when it receives an `EVENT` message matching the
|
||||
subscription ID. If the subscription was silently closed, errored, or not
|
||||
re-established after a relay reconnect, no events would be delivered. The
|
||||
relay could also be rejecting the subscription filter.
|
||||
|
||||
2. **`since` filter timing mismatch** — `g_start_time` is set to `time(NULL)`
|
||||
during `nostr_handler_init()` (early in startup), but the DM subscription
|
||||
is created much later (after admin context subscription, startup event
|
||||
reconciliation, and startup DM sending). If the sender's clock is behind
|
||||
the server's clock, their `created_at` timestamp would be before
|
||||
`g_start_time` and the relay would filter them out.
|
||||
|
||||
3. **Sender tier filtering** — If the sender's pubkey doesn't match the admin
|
||||
pubkey and isn't in the WoT contact list, the message is classified as
|
||||
`DIDACTYL_SENDER_STRANGER` and silently dropped (only a DEBUG_LOG at
|
||||
level 4).
|
||||
|
||||
4. **Signature verification failure** — If `verify_signatures` is enabled and
|
||||
the event has an invalid signature, it is dropped with only a WARN log.
|
||||
|
||||
5. **Pool-level deduplication** — The relay pool's `is_event_seen()` cache is
|
||||
shared across all subscriptions. If an event ID was somehow marked as seen
|
||||
by another subscription, it would be silently dropped.
|
||||
|
||||
### Diagnostic logging added
|
||||
|
||||
TRACE-level (level 5 / `--debug 5`) logs were added at every decision point
|
||||
in the `on_event()` callback and the `nostr_handler_subscribe_dms()` setup:
|
||||
|
||||
| Log message prefix | What it tells you |
|
||||
|---|---|
|
||||
| `DEBUG on_event ENTRY` | Callback was called — events are reaching didactyl |
|
||||
| `DEBUG on_event NULL guard` | Event, config, or callback pointer is NULL |
|
||||
| `DEBUG on_event: missing required fields` | Event JSON is malformed |
|
||||
| `DEBUG on_event: kind=N id=... from=...` | Event kind, ID, and sender pubkey |
|
||||
| `DEBUG on_event: ignoring non-kind4` | Event is not kind 4 |
|
||||
| `DEBUG on_event: no p-tag found` | Kind 4 event has no `p` tag |
|
||||
| `DEBUG on_event: p-tag mismatch` | `p` tag doesn't match agent's pubkey |
|
||||
| `DEBUG on_event: sender=... tier=N` | Sender tier classification |
|
||||
| `DEBUG on_eose called` | EOSE received from relays |
|
||||
| `DEBUG DM subscription filter` | Full subscription filter JSON |
|
||||
| `DEBUG DM subscription g_start_time=...` | `since` timestamp and time delta |
|
||||
| `DEBUG DM subscription sub=...` | Subscription pointer (confirms creation) |
|
||||
|
||||
### How to use the diagnostic logs
|
||||
|
||||
1. Build with `./build_static.sh --debug`
|
||||
2. Run with `--debug 5` to enable TRACE output
|
||||
3. Send a kind 4 DM to the agent
|
||||
4. Check the output:
|
||||
- **No `on_event ENTRY` lines** → problem is at the relay/subscription level
|
||||
- **`on_event ENTRY` appears but processing stops** → the specific drop-point
|
||||
log identifies the exact filter that rejected the event
|
||||
5. Check the `DM subscription filter` log to verify the `since` timestamp and
|
||||
`#p` tag are correct
|
||||
|
||||
### How to recognise this class of bug in the future
|
||||
|
||||
- Agent can send but not receive — asymmetric connectivity.
|
||||
- No error messages in the log — silent event filtering.
|
||||
- The subscription filter (`since`, `#p`, `kinds`) may not match what the
|
||||
sender is actually publishing.
|
||||
- Clock skew between sender and receiver can cause `since` filter mismatches.
|
||||
- Relay reconnections may not automatically re-subscribe.
|
||||
|
||||
---
|
||||
|
||||
## General debugging checklist
|
||||
|
||||
When investigating a crash in Didactyl, work through these steps:
|
||||
|
||||
1. **Check for coredumps**: `ls /var/lib/systemd/coredump/ | grep didactyl`
|
||||
2. **Check dmesg/journalctl**: `dmesg | grep -i 'didactyl\|oom\|segfault'`
|
||||
3. **Check for the shutdown log line**: If `[didactyl] shutting down` is missing,
|
||||
the process was killed by a signal that bypassed the handler.
|
||||
4. **Check ulimit**: `ulimit -c` — if 0, coredumps are disabled.
|
||||
5. **Extract and analyse coredumps**:
|
||||
```bash
|
||||
zstd -d /var/lib/systemd/coredump/core.didactyl*.zst -o /tmp/core.bin
|
||||
gdb ./didactyl_static_x86_64_debug /tmp/core.bin -batch -ex "bt full"
|
||||
```
|
||||
6. **Common silent killers**:
|
||||
- `SIGPIPE` — process writes to a broken socket/pipe
|
||||
- `SIGKILL` — OOM killer or external kill
|
||||
- `SIGBUS` — memory-mapped file issues
|
||||
7. **Common crash causes**:
|
||||
- Use-after-free on cJSON child pointers after parent deletion
|
||||
- Buffer overflows in fixed-size stack buffers
|
||||
- NULL pointer dereference on failed allocations
|
||||
- Re-entrancy in callbacks during internal `nostr_relay_pool_poll()` calls
|
||||
419
docs/TOOLS_AND_SKILLS.md
Normal file
419
docs/TOOLS_AND_SKILLS.md
Normal file
@@ -0,0 +1,419 @@
|
||||
# Didactyl — Tools & Skills
|
||||
|
||||
## Overview
|
||||
|
||||
Didactyl is a **Nostr-first sovereign AI agent**. It receives commands via encrypted DMs, reasons about them with an LLM, and takes actions through **tools**. It stores learned behaviors as **skills** — Nostr events that define reusable capabilities. Skills can optionally carry **triggers** — Nostr subscription filters that activate the skill automatically when matching events arrive.
|
||||
|
||||
This document describes the complete tools and skills architecture: what they are, how they work, and how they compose into a dynamic, self-modifying agent.
|
||||
|
||||
---
|
||||
|
||||
## Tools
|
||||
|
||||
Tools are the agent's hands. They are hardcoded C functions that the LLM can invoke during a conversation to take actions in the world.
|
||||
|
||||
### How Tools Work
|
||||
|
||||
1. Admin sends a DM to didactyl
|
||||
2. The agent builds an LLM request with the message, context, and a JSON schema of all available tools
|
||||
3. The LLM decides whether to call a tool or respond directly
|
||||
4. If a tool is called, didactyl executes it and feeds the result back to the LLM
|
||||
5. The loop repeats until the LLM produces a final text response
|
||||
6. The response is sent back as a DM
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant Admin
|
||||
participant Agent as Didactyl Agent Loop
|
||||
participant LLM as LLM API
|
||||
participant Tools as Tool Registry
|
||||
|
||||
Admin->>Agent: Encrypted DM
|
||||
Agent->>LLM: messages + tool schemas
|
||||
|
||||
loop Until final answer
|
||||
LLM->>Agent: tool_call request
|
||||
Agent->>Tools: dispatch tool
|
||||
Tools->>Agent: result JSON
|
||||
Agent->>LLM: tool result + continue
|
||||
end
|
||||
|
||||
LLM->>Agent: final text response
|
||||
Agent->>Admin: Encrypted DM reply
|
||||
```
|
||||
|
||||
### Tool Categories
|
||||
|
||||
#### Nostr Event & Messaging Tools
|
||||
|
||||
| Tool | Description |
|
||||
|---|---|
|
||||
| `nostr_post` | Publish a Nostr event to connected relays |
|
||||
| `nostr_delete` | Request deletion of one or more previously published events (NIP-09 kind 5) |
|
||||
| `nostr_react` | React to a Nostr event with like/dislike/emoji (NIP-25 kind 7) |
|
||||
| `nostr_query` | Query events from relays using a Nostr filter |
|
||||
| `nostr_dm_send` | Send a NIP-04 encrypted DM |
|
||||
| `nostr_dm_send_nip17` | Send a private DM using NIP-17 gift wrap protocol |
|
||||
|
||||
#### Nostr Identity & Utility Tools
|
||||
|
||||
| Tool | Description |
|
||||
|---|---|
|
||||
| `nostr_profile_get` | Look up a Nostr profile (kind 0 metadata) by pubkey |
|
||||
| `nostr_nip05_lookup` | Look up or verify a NIP-05 identifier (`user@domain`) |
|
||||
| `nostr_encode` | Encode a Nostr entity into `nostr:` URI (`npub`, `note`, `nprofile`, `nevent`, `naddr`) |
|
||||
| `nostr_decode` | Decode a Nostr bech32/`nostr:` URI into components |
|
||||
| `nostr_relay_status` | Get connection status and statistics for all relays |
|
||||
| `nostr_relay_info` | Fetch NIP-11 relay information document |
|
||||
| `nostr_encrypt` | Encrypt plaintext using NIP-44 for a recipient |
|
||||
| `nostr_decrypt` | Decrypt NIP-44 ciphertext from a sender |
|
||||
| `nostr_list_manage` | Add/remove tag tuples in replaceable list events (NIP-51 style) |
|
||||
|
||||
#### Skills & Trigger Tools
|
||||
|
||||
| Tool | Description |
|
||||
|---|---|
|
||||
| `skill_create` | Create or update a skill definition as kind `31123`/`31124` and optionally auto-adopt it |
|
||||
| `skill_list` | List this agent's published skills, optionally filtered by scope |
|
||||
| `skill_adopt` | Adopt a skill by adding its address to kind `10123` adoption list |
|
||||
| `skill_remove` | Remove a skill address from kind `10123` adoption list |
|
||||
| `skill_search` | Search public skills by query/author and optionally rank by adoption popularity |
|
||||
| `trigger_list` | List active triggered skills and their runtime status |
|
||||
|
||||
#### LLM / Model Management Tools
|
||||
|
||||
| Tool | Description |
|
||||
|---|---|
|
||||
| `model_get` | Get current active LLM runtime configuration (excluding API key) |
|
||||
| `model_set` | Update active LLM configuration and persist it to `config.json` |
|
||||
| `model_list` | List available model IDs using provider OpenAI-compatible `/models` endpoint |
|
||||
|
||||
#### System & Runtime Tools
|
||||
|
||||
| Tool | Description |
|
||||
|---|---|
|
||||
| `my_version` | Return current Didactyl version and metadata from build macros |
|
||||
| `http_fetch` | Fetch HTTP(S) resources with optional method, headers, timeout, and body |
|
||||
| `shell_exec` | Execute a shell command and return stdout/stderr |
|
||||
| `file_read` | Read a local file as text from the configured working directory |
|
||||
| `file_write` | Write text content to a local file in the configured working directory |
|
||||
| `tool_list` | List available tools with name, description, and JSON parameter schema |
|
||||
|
||||
#### Content Publishing Conveniences
|
||||
|
||||
| Tool | Description |
|
||||
|---|---|
|
||||
| `nostr_post_readme` | Publish `README.md` as kind `30023` with deterministic d-tag `readme.md` |
|
||||
| `nostr_file_md_to_longform_post` | Read a markdown file and publish it as kind `30023` longform post (defaults d-tag to lowercase filename) |
|
||||
|
||||
### Security Model
|
||||
|
||||
Tools are gated by sender tier:
|
||||
|
||||
| Tier | Identity | Tools | Response |
|
||||
|------|----------|-------|----------|
|
||||
| **ADMIN** | Configured admin pubkey | All tools | Full LLM with context |
|
||||
| **WOT** | In admin's kind 3 contact list | None | Chat-only LLM |
|
||||
| **STRANGER** | Anyone else | None | Configurable static response |
|
||||
|
||||
---
|
||||
|
||||
## Skills
|
||||
|
||||
Skills are the agent's learned behaviors. They are **Nostr events** — stored on relays, portable, shareable, and discoverable by other agents.
|
||||
|
||||
### Skill Events
|
||||
|
||||
| Kind | Purpose | Replaceable? |
|
||||
|---|---|---|
|
||||
| `31123` | Public skill definition | Yes, by d-tag |
|
||||
| `31124` | Private skill definition | Yes, by d-tag |
|
||||
| `10123` | Skill adoption list | Yes, single per pubkey |
|
||||
|
||||
A skill event looks like:
|
||||
|
||||
```json
|
||||
{
|
||||
"kind": 31123,
|
||||
"content": "When asked to summarize a thread, query the root event and all replies, then produce a concise summary with key points and sentiment.",
|
||||
"tags": [
|
||||
["d", "summarize-thread"],
|
||||
["app", "didactyl"],
|
||||
["scope", "public"],
|
||||
["description", "Summarize a Nostr thread given a root event ID"]
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### Skill Lifecycle
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
CREATE[Admin asks didactyl to create a skill] --> PUBLISH[skill_create publishes kind 31123/31124]
|
||||
PUBLISH --> ADOPT[Auto-adopted into kind 10123 list]
|
||||
ADOPT --> AVAILABLE[Skill available for use]
|
||||
AVAILABLE --> DISCOVER[Other agents can discover via skill_search]
|
||||
DISCOVER --> ADOPT_OTHER[Other agents can skill_adopt]
|
||||
```
|
||||
|
||||
### How Skills Are Used Today
|
||||
|
||||
Adopted skills are now **always-on contextual knowledge** for admin DM handling: the agent resolves the local adoption list (kind `10123`), caches referenced skills, and injects their instructions into the LLM system context each turn.
|
||||
|
||||
To keep context stable and safe, skill injection is bounded by hard caps (per-skill truncation and total skill-context budget), and excess skills are omitted with an explicit budget notice.
|
||||
|
||||
---
|
||||
|
||||
## Triggered Skills — The Activation System
|
||||
|
||||
This is where skills become **active**. A triggered skill is a skill with a Nostr subscription filter attached. When matching events arrive on the relay, didactyl wakes up and executes the skill automatically — no admin DM required.
|
||||
|
||||
### Anatomy of a Triggered Skill
|
||||
|
||||
A triggered skill extends the standard skill event with trigger-related tags:
|
||||
|
||||
```json
|
||||
{
|
||||
"kind": 31124,
|
||||
"content": "DM admin: '{author_display_name} just posted: {content_preview}'",
|
||||
"tags": [
|
||||
["d", "watch-jack"],
|
||||
["app", "didactyl"],
|
||||
["scope", "private"],
|
||||
["description", "Notify admin when @jack posts a note"],
|
||||
["trigger", "nostr-subscription"],
|
||||
["filter", "{\"authors\":[\"82341f882b6eabcd2ba7f1ef90aad961cf074af15b9ef44a09f9d2a8fbfbe6a2\"],\"kinds\":[1]}"],
|
||||
["action", "template"],
|
||||
["enabled", "true"]
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### Trigger Tags
|
||||
|
||||
| Tag | Required | Description |
|
||||
|---|---|---|
|
||||
| `trigger` | Yes | Trigger type. Currently: `nostr-subscription` |
|
||||
| `filter` | Yes | JSON-encoded Nostr subscription filter |
|
||||
| `action` | No | Action type: `template` or `llm`. Default: `llm` |
|
||||
| `enabled` | No | Whether the trigger is active. Default: `true` |
|
||||
|
||||
### Action Types
|
||||
|
||||
#### Template Actions
|
||||
|
||||
The skill content is a string template with placeholders that get interpolated from the triggering event:
|
||||
|
||||
```
|
||||
DM admin: '{author_display_name} posted: {content_preview}'
|
||||
```
|
||||
|
||||
Available placeholders:
|
||||
|
||||
| Placeholder | Source |
|
||||
|---|---|
|
||||
| `{event_id}` | Triggering event ID hex |
|
||||
| `{pubkey}` | Author pubkey hex |
|
||||
| `{author_display_name}` | Resolved display name, falls back to truncated pubkey |
|
||||
| `{kind}` | Event kind number |
|
||||
| `{content}` | Full event content |
|
||||
| `{content_preview}` | First 280 characters of content |
|
||||
| `{created_at}` | Unix timestamp |
|
||||
| `{relay_url}` | Relay the event arrived from |
|
||||
|
||||
Template actions execute **without LLM involvement** — they are fast, cheap, and deterministic. The interpolated string is then acted upon based on a simple action prefix:
|
||||
|
||||
- `DM admin: ...` — send a DM to the admin
|
||||
- `DM <pubkey>: ...` — send a DM to a specific pubkey
|
||||
- `POST: ...` — publish as a kind 1 note
|
||||
- `LOG: ...` — write to debug log only
|
||||
|
||||
#### LLM-Mediated Actions
|
||||
|
||||
The skill content is a prompt. The triggering event is injected as context, and the LLM decides what to do:
|
||||
|
||||
```
|
||||
You received a note from a watched author. Analyze the note content.
|
||||
If it mentions Bitcoin or Lightning, summarize it and DM the admin.
|
||||
If it's a repost or low-effort content, ignore it silently.
|
||||
Use your tools to take action.
|
||||
```
|
||||
|
||||
LLM-mediated actions go through the full agent loop — the LLM can call tools, reason about the event, and produce complex multi-step responses.
|
||||
|
||||
### Trigger Lifecycle
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
subgraph Creation
|
||||
ADMIN_CMD[Admin: 'Warn me when @jack posts'] --> LLM_REASON[LLM resolves pubkey + builds skill]
|
||||
LLM_REASON --> SKILL_CREATE[skill_create with trigger tags]
|
||||
SKILL_CREATE --> PUBLISHED[Skill published to Nostr]
|
||||
end
|
||||
|
||||
subgraph Activation
|
||||
STARTUP[Didactyl starts up] --> LOAD_SKILLS[Load adopted skills from kind 10123]
|
||||
LOAD_SKILLS --> FIND_TRIGGERS[Find skills with trigger tags]
|
||||
FIND_TRIGGERS --> SUBSCRIBE[Create Nostr subscriptions for each filter]
|
||||
end
|
||||
|
||||
subgraph Execution
|
||||
EVENT_IN[Matching event arrives] --> LOOKUP[Find associated skill]
|
||||
LOOKUP --> CHECK_TYPE{Action type?}
|
||||
CHECK_TYPE -->|template| INTERPOLATE[Interpolate placeholders]
|
||||
CHECK_TYPE -->|llm| LLM_LOOP[Run agent loop with event as context]
|
||||
INTERPOLATE --> EXECUTE_TPL[Execute action prefix]
|
||||
LLM_LOOP --> EXECUTE_LLM[LLM uses tools to respond]
|
||||
end
|
||||
|
||||
PUBLISHED --> LOAD_SKILLS
|
||||
```
|
||||
|
||||
### Dynamic Subscription Management
|
||||
|
||||
Didactyl manages its trigger subscriptions dynamically:
|
||||
|
||||
1. **On startup**: Load all adopted skills, find triggered ones, create subscriptions
|
||||
2. **On skill_create with trigger**: Immediately create a new subscription (no restart needed)
|
||||
3. **On skill_remove with trigger**: Tear down the associated subscription
|
||||
4. **On skill update**: Tear down old subscription, create new one if trigger changed
|
||||
|
||||
This requires a **trigger manager** component that:
|
||||
- Maintains a registry of active trigger subscriptions
|
||||
- Maps subscription callbacks back to their source skills
|
||||
- Handles subscription lifecycle (create, update, destroy)
|
||||
- Enforces limits on concurrent triggers
|
||||
|
||||
### Limits and Safety
|
||||
|
||||
| Limit | Default | Description |
|
||||
|---|---|---|
|
||||
| Max concurrent triggers | 16 | Prevents resource exhaustion from too many subscriptions |
|
||||
| Trigger cooldown | 60s per skill | Prevents rapid-fire execution from high-volume filters |
|
||||
| LLM action rate limit | 10/min | Prevents runaway LLM costs from triggered skills |
|
||||
| Template action rate limit | 60/min | Prevents DM spam from template actions |
|
||||
|
||||
---
|
||||
|
||||
## How It All Fits Together
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
subgraph Activation Sources
|
||||
DM_IN[DM from Admin/WoT]
|
||||
TRIGGER_EVENT[Nostr event matching a trigger filter]
|
||||
end
|
||||
|
||||
subgraph Agent Core
|
||||
DISPATCHER{Dispatcher}
|
||||
AGENT_LOOP[Agent Loop - LLM + Tools]
|
||||
TEMPLATE_ENGINE[Template Engine]
|
||||
end
|
||||
|
||||
subgraph Nostr
|
||||
RELAYS[Relays]
|
||||
SKILLS_STORE[Skills - kind 31123/31124]
|
||||
ADOPTION[Adoption List - kind 10123]
|
||||
end
|
||||
|
||||
subgraph Actions
|
||||
DM_OUT[Send DM]
|
||||
POST[Publish Note]
|
||||
TOOL_EXEC[Execute Tool]
|
||||
end
|
||||
|
||||
DM_IN --> DISPATCHER
|
||||
TRIGGER_EVENT --> DISPATCHER
|
||||
|
||||
DISPATCHER -->|DM message| AGENT_LOOP
|
||||
DISPATCHER -->|template trigger| TEMPLATE_ENGINE
|
||||
DISPATCHER -->|llm trigger| AGENT_LOOP
|
||||
|
||||
AGENT_LOOP --> TOOL_EXEC
|
||||
AGENT_LOOP --> DM_OUT
|
||||
TEMPLATE_ENGINE --> DM_OUT
|
||||
TEMPLATE_ENGINE --> POST
|
||||
|
||||
TOOL_EXEC -->|skill_create| SKILLS_STORE
|
||||
TOOL_EXEC -->|skill_adopt| ADOPTION
|
||||
TOOL_EXEC -->|nostr_post| RELAYS
|
||||
TOOL_EXEC -->|nostr_dm| DM_OUT
|
||||
```
|
||||
|
||||
### The Activation Flow
|
||||
|
||||
Today, didactyl has one activation source: **DMs**. With triggered skills, it gains a second: **any Nostr event matching a trigger filter**.
|
||||
|
||||
Both paths converge at the dispatcher, which routes to either:
|
||||
- The **agent loop** (for DMs and LLM-mediated triggers)
|
||||
- The **template engine** (for template triggers — fast path, no LLM)
|
||||
|
||||
### Self-Modification
|
||||
|
||||
The most powerful aspect: **didactyl can create its own triggers**. The admin says "watch for mentions of me on Nostr" and the LLM:
|
||||
|
||||
1. Resolves the admin's pubkey
|
||||
2. Crafts a Nostr filter: `{"#p": ["<admin_pubkey>"], "kinds": [1]}`
|
||||
3. Writes a skill with trigger tags via `skill_create`
|
||||
4. The trigger manager picks it up and creates the subscription
|
||||
5. From now on, didactyl monitors mentions autonomously
|
||||
|
||||
The admin can later say "stop watching for mentions" and didactyl removes the skill, tearing down the subscription.
|
||||
|
||||
---
|
||||
|
||||
## Storage — Everything on Nostr
|
||||
|
||||
All state lives on Nostr:
|
||||
|
||||
| Data | Storage |
|
||||
|---|---|
|
||||
| Agent identity | Kind 0 profile event |
|
||||
| Agent relay list | Kind 10002 event |
|
||||
| Agent contact list | Kind 3 event |
|
||||
| Skills | Kind 31123/31124 events |
|
||||
| Adopted skills | Kind 10123 event |
|
||||
| Trigger definitions | Tags on skill events |
|
||||
| Conversation history | Kind 4 DM events on relays |
|
||||
| Agent soul/personality | Startup event content in config |
|
||||
|
||||
The only local state is `config.json` (keys, relay URLs, LLM config) and the runtime in-memory state (active subscriptions, LLM context).
|
||||
|
||||
---
|
||||
|
||||
## Future Extensions
|
||||
|
||||
### Trigger Types Beyond Nostr Subscriptions
|
||||
|
||||
The `trigger` tag is designed to be extensible:
|
||||
|
||||
| Trigger Type | Description |
|
||||
|---|---|
|
||||
| `nostr-subscription` | Match events via Nostr filter (implemented first) |
|
||||
| `cron` | Time-based triggers — "every day at 9am, post a GM" |
|
||||
| `webhook` | HTTP webhook triggers — external systems wake didactyl |
|
||||
| `chain` | Output of one skill triggers another skill |
|
||||
|
||||
### Skill Composition
|
||||
|
||||
Skills could reference other skills, building complex behaviors from simple primitives:
|
||||
|
||||
```
|
||||
When triggered, run skill 'translate-to-english' on the note content,
|
||||
then run skill 'sentiment-analysis' on the translation,
|
||||
then DM admin with the result if sentiment is negative.
|
||||
```
|
||||
|
||||
### Agent-to-Agent Skill Sharing
|
||||
|
||||
Since skills are Nostr events, agents can:
|
||||
- Discover skills published by other agents via `skill_search`
|
||||
- Adopt skills from other agents via `skill_adopt`
|
||||
- Share triggered skill patterns across a network of agents
|
||||
|
||||
### Trigger Marketplace
|
||||
|
||||
With kind 10123 adoption lists being public, a natural marketplace emerges:
|
||||
- Agents publish useful triggered skills
|
||||
- Other agents discover and adopt them
|
||||
- Popular triggers rise to the top via adoption count
|
||||
@@ -200,6 +200,79 @@ update_version_in_header() {
|
||||
print_success "Updated version in src/main.h to $new_version"
|
||||
}
|
||||
|
||||
# Function to update README Current Status version and release comment
|
||||
update_readme_current_status() {
|
||||
local new_version="$1"
|
||||
local release_comment="$2"
|
||||
|
||||
if [[ ! -f "README.md" ]]; then
|
||||
print_warning "README.md not found, skipping Current Status update"
|
||||
return 0
|
||||
fi
|
||||
|
||||
print_status "Updating README Current Status section..."
|
||||
|
||||
if python3 - "$new_version" "$release_comment" <<'PY'
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
version = sys.argv[1]
|
||||
comment = sys.argv[2]
|
||||
path = Path("README.md")
|
||||
text = path.read_text(encoding="utf-8")
|
||||
lines = text.splitlines()
|
||||
|
||||
heading_idx = None
|
||||
for i, line in enumerate(lines):
|
||||
if line.startswith("## Current Status"):
|
||||
heading_idx = i
|
||||
break
|
||||
|
||||
if heading_idx is None:
|
||||
raise SystemExit("README Current Status heading not found")
|
||||
|
||||
lines[heading_idx] = f"## Current Status — {version}"
|
||||
|
||||
section_end = len(lines)
|
||||
for i in range(heading_idx + 1, len(lines)):
|
||||
if lines[i].startswith("## "):
|
||||
section_end = i
|
||||
break
|
||||
|
||||
comment_line = f"> Last release update: {version} — {comment}"
|
||||
|
||||
# Replace existing autogenerated release comment inside Current Status section
|
||||
existing_idx = None
|
||||
for i in range(heading_idx + 1, section_end):
|
||||
if lines[i].startswith("> Last release update:"):
|
||||
existing_idx = i
|
||||
break
|
||||
|
||||
if existing_idx is not None:
|
||||
lines[existing_idx] = comment_line
|
||||
else:
|
||||
insert_at = None
|
||||
for i in range(heading_idx + 1, section_end):
|
||||
if lines[i].startswith("**Active build"):
|
||||
insert_at = i + 1
|
||||
break
|
||||
|
||||
if insert_at is None:
|
||||
insert_at = heading_idx + 1
|
||||
|
||||
payload = ["", comment_line]
|
||||
lines[insert_at:insert_at] = payload
|
||||
|
||||
path.write_text("\n".join(lines) + "\n", encoding="utf-8")
|
||||
PY
|
||||
then
|
||||
print_success "Updated README Current Status section"
|
||||
else
|
||||
print_error "Failed to update README Current Status section"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to commit and push changes without creating a tag (tag already created)
|
||||
git_commit_and_push_no_tag() {
|
||||
print_status "Preparing git commit..."
|
||||
@@ -427,6 +500,9 @@ main() {
|
||||
export NEW_VERSION
|
||||
fi
|
||||
|
||||
# Keep README Current Status in sync with the version and commit message
|
||||
update_readme_current_status "$NEW_VERSION" "$COMMIT_MESSAGE"
|
||||
|
||||
# Create new git tag BEFORE compilation so version picks it up
|
||||
if git tag "$NEW_VERSION" > /dev/null 2>&1; then
|
||||
print_success "Created tag: $NEW_VERSION"
|
||||
@@ -482,6 +558,9 @@ main() {
|
||||
# Increment version based on type (default to patch)
|
||||
increment_version "$VERSION_INCREMENT_TYPE"
|
||||
|
||||
# Keep README Current Status in sync with the version and commit message
|
||||
update_readme_current_status "$NEW_VERSION" "$COMMIT_MESSAGE"
|
||||
|
||||
# Create new git tag BEFORE compilation so version picks it up
|
||||
if git tag "$NEW_VERSION" > /dev/null 2>&1; then
|
||||
print_success "Created tag: $NEW_VERSION"
|
||||
|
||||
71
local_test_servers.py
Normal file
71
local_test_servers.py
Normal file
@@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env python3
|
||||
import json
|
||||
import ssl
|
||||
import threading
|
||||
from pathlib import Path
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
|
||||
HOST = "127.0.0.1"
|
||||
HTTP_PORT = 9080
|
||||
HTTPS_PORT = 9449
|
||||
CERT = "/home/teknari/.ssl_for_local_servers/cert.pem"
|
||||
KEY = "/home/teknari/.ssl_for_local_servers/key.pem"
|
||||
|
||||
|
||||
HTML_FILE = Path("./didactyl.html")
|
||||
|
||||
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
def _cors(self):
|
||||
self.send_header("Access-Control-Allow-Origin", "*")
|
||||
self.send_header("Access-Control-Allow-Methods", "GET, OPTIONS")
|
||||
self.send_header("Access-Control-Allow-Headers", "Content-Type")
|
||||
self.send_header("Access-Control-Allow-Private-Network", "true")
|
||||
|
||||
def do_OPTIONS(self):
|
||||
self.send_response(204)
|
||||
self._cors()
|
||||
self.end_headers()
|
||||
|
||||
def do_GET(self):
|
||||
if self.path in ("/", "/didactyl.html") and HTML_FILE.exists():
|
||||
body = HTML_FILE.read_bytes()
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "text/html; charset=utf-8")
|
||||
self._cors()
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
return
|
||||
|
||||
body = json.dumps(
|
||||
{
|
||||
"success": True,
|
||||
"name": "python-test",
|
||||
"path": self.path,
|
||||
"scheme_hint": "https" if self.server.server_port == HTTPS_PORT else "http",
|
||||
}
|
||||
).encode("utf-8")
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self._cors()
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def log_message(self, fmt, *args):
|
||||
print(f"[{self.server.server_port}] " + (fmt % args), flush=True)
|
||||
|
||||
|
||||
httpd = HTTPServer((HOST, HTTP_PORT), Handler)
|
||||
httpsd = HTTPServer((HOST, HTTPS_PORT), Handler)
|
||||
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
||||
ctx.load_cert_chain(certfile=CERT, keyfile=KEY)
|
||||
httpsd.socket = ctx.wrap_socket(httpsd.socket, server_side=True)
|
||||
|
||||
print(f"HTTP listening on http://{HOST}:{HTTP_PORT}", flush=True)
|
||||
print(f"HTTPS listening on https://{HOST}:{HTTPS_PORT}", flush=True)
|
||||
print("Use Ctrl+C to stop", flush=True)
|
||||
|
||||
threading.Thread(target=httpd.serve_forever, daemon=True).start()
|
||||
httpsd.serve_forever()
|
||||
469
plans/admin_api.md
Normal file
469
plans/admin_api.md
Normal file
@@ -0,0 +1,469 @@
|
||||
# Didactyl Admin HTTP API — Architecture & Implementation Plan
|
||||
|
||||
## Overview
|
||||
|
||||
Add a localhost-only HTTP API to didactyl so an external web dashboard can inspect and manage the agent at runtime. No authentication required — binding to `127.0.0.1` only. All responses are JSON. CORS headers included for browser access from any local origin.
|
||||
|
||||
The web frontend is a separate project; this plan covers only the C-side HTTP server and API endpoints.
|
||||
|
||||
---
|
||||
|
||||
## Architecture
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
subgraph didactyl process
|
||||
MAIN[main loop] --> POLL[nostr_handler_poll]
|
||||
MAIN --> TPOLL[trigger_manager_poll]
|
||||
MAIN --> HPOLL[http_api_poll]
|
||||
HPOLL --> ROUTER[request router]
|
||||
ROUTER --> AGENT[agent internals]
|
||||
ROUTER --> NOSTR[nostr_handler]
|
||||
ROUTER --> TOOLS[tools context]
|
||||
ROUTER --> CONFIG[config]
|
||||
ROUTER --> TRIGGERS[trigger_manager]
|
||||
end
|
||||
BROWSER[Web Dashboard] -- HTTP localhost:8484 --> HPOLL
|
||||
```
|
||||
|
||||
### HTTP Library Choice
|
||||
|
||||
Use a minimal embedded HTTP server. Two good options for C with no extra dependencies:
|
||||
|
||||
1. **mongoose** (single `mongoose.c` + `mongoose.h`) — battle-tested, MIT license, supports polling model
|
||||
2. **microhttpd** (libmicrohttpd) — GNU project, available as system package
|
||||
|
||||
**Recommendation: mongoose** — it is a single-file drop-in, works with the existing poll-based main loop, and requires zero system dependencies. Just add `mongoose.c` and `mongoose.h` to the project.
|
||||
|
||||
### Integration Pattern
|
||||
|
||||
The HTTP server runs in the same thread as the main poll loop. Each iteration calls `http_api_poll()` which does non-blocking accept/read/write via mongoose's `mg_mgr_poll()`. This avoids threading complexity and gives the API direct access to all agent state.
|
||||
|
||||
---
|
||||
|
||||
## Config Extension
|
||||
|
||||
```json
|
||||
{
|
||||
"api": {
|
||||
"enabled": true,
|
||||
"port": 8484,
|
||||
"bind_address": "127.0.0.1"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Defaults: enabled=false, port=8484, bind=127.0.0.1.
|
||||
|
||||
---
|
||||
|
||||
## API Endpoints
|
||||
|
||||
All endpoints return JSON. All mutations use POST/PUT/DELETE. All reads use GET.
|
||||
|
||||
### Agent Identity & Status
|
||||
|
||||
| Method | Path | Description |
|
||||
|---|---|---|
|
||||
| GET | `/api/status` | Agent runtime status: pubkey, display name, version, uptime, connected relay count, trigger count |
|
||||
| GET | `/api/config` | Current runtime config (redacted: nsec/api_key masked) |
|
||||
|
||||
### Nostr Events — Read & Edit
|
||||
|
||||
| Method | Path | Description |
|
||||
|---|---|---|
|
||||
| GET | `/api/events/soul` | Fetch the agent soul event (kind 31120, d=soul) |
|
||||
| PUT | `/api/events/soul` | Update soul content, republish to relays |
|
||||
| GET | `/api/events/skills` | List all published skills (kind 31123/31124 by own pubkey) |
|
||||
| GET | `/api/events/skills/:slug` | Fetch a single skill by slug |
|
||||
| PUT | `/api/events/skills/:slug` | Update skill content/tags, republish |
|
||||
| DELETE | `/api/events/skills/:slug` | Remove skill from adoption list |
|
||||
| GET | `/api/events/adoption` | Fetch kind 10123 adoption list |
|
||||
| GET | `/api/events/startup` | List startup events from config |
|
||||
| GET | `/api/events/profile` | Fetch agent kind 0 profile |
|
||||
| PUT | `/api/events/profile` | Update agent kind 0 profile, republish |
|
||||
| GET | `/api/events/query` | Generic Nostr query — pass filter as query params or JSON body |
|
||||
|
||||
### Context Inspector
|
||||
|
||||
| Method | Path | Description |
|
||||
|---|---|---|
|
||||
| GET | `/api/context/current` | Build and return the full context that would be sent to the LLM right now, broken into labeled parts |
|
||||
| GET | `/api/context/parts` | Return context parts with individual sizes (bytes and estimated tokens) |
|
||||
| GET | `/api/context/log` | Return recent context.log entries (last N blocks, configurable via ?limit=) |
|
||||
| POST | `/api/context/preview` | Accept a modified context structure, return what the LLM payload would look like (dry run, no send) |
|
||||
|
||||
### Context Parts Response Shape
|
||||
|
||||
```json
|
||||
{
|
||||
"total_chars": 12450,
|
||||
"total_estimated_tokens": 3112,
|
||||
"parts": [
|
||||
{
|
||||
"name": "system_prompt",
|
||||
"role": "system",
|
||||
"chars": 1200,
|
||||
"estimated_tokens": 300,
|
||||
"content": "# Didactyl Agent..."
|
||||
},
|
||||
{
|
||||
"name": "admin_identity",
|
||||
"role": "system",
|
||||
"chars": 450,
|
||||
"estimated_tokens": 112,
|
||||
"content": "This is your administrator!..."
|
||||
},
|
||||
{
|
||||
"name": "admin_kind0",
|
||||
"role": "system",
|
||||
"chars": 320,
|
||||
"estimated_tokens": 80,
|
||||
"content": "Administrator kind 0 profile..."
|
||||
},
|
||||
{
|
||||
"name": "startup_events",
|
||||
"role": "system",
|
||||
"chars": 4800,
|
||||
"estimated_tokens": 1200,
|
||||
"content": "Startup events memory..."
|
||||
},
|
||||
{
|
||||
"name": "adopted_skills",
|
||||
"role": "system",
|
||||
"chars": 2100,
|
||||
"estimated_tokens": 525,
|
||||
"content": "Adopted skills memory..."
|
||||
},
|
||||
{
|
||||
"name": "dm_history",
|
||||
"role": "mixed",
|
||||
"chars": 2400,
|
||||
"estimated_tokens": 600,
|
||||
"turns": 8
|
||||
},
|
||||
{
|
||||
"name": "admin_notes",
|
||||
"role": "system",
|
||||
"chars": 680,
|
||||
"estimated_tokens": 170,
|
||||
"content": "Administrator recent public notes..."
|
||||
},
|
||||
{
|
||||
"name": "tools_schema",
|
||||
"chars": 500,
|
||||
"estimated_tokens": 125,
|
||||
"tool_count": 28
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### Triggers
|
||||
|
||||
| Method | Path | Description |
|
||||
|---|---|---|
|
||||
| GET | `/api/triggers` | List active triggers with status (wraps existing trigger_manager_status_json) |
|
||||
|
||||
### Model / LLM
|
||||
|
||||
| Method | Path | Description |
|
||||
|---|---|---|
|
||||
| GET | `/api/model` | Current model config (wraps existing model_get) |
|
||||
| PUT | `/api/model` | Update model config (wraps existing model_set) |
|
||||
| GET | `/api/models` | List available models from provider (wraps existing model_list) |
|
||||
|
||||
### Relays
|
||||
|
||||
| Method | Path | Description |
|
||||
|---|---|---|
|
||||
| GET | `/api/relays` | Relay connection status (wraps existing relay_status tool) |
|
||||
|
||||
### Prompt Crafting & Execution
|
||||
|
||||
| Method | Path | Description |
|
||||
|---|---|---|
|
||||
| POST | `/api/prompt/run` | Submit a custom messages array with tools enabled; returns full LLM response including tool calls and results |
|
||||
| POST | `/api/prompt/run-simple` | Submit system prompt + user message; returns LLM text response (no tools) |
|
||||
| POST | `/api/prompt/compare` | A/B test: submit two prompt variants, run both, return side-by-side responses |
|
||||
|
||||
#### POST /api/prompt/run
|
||||
|
||||
Send a fully crafted messages array to the LLM with the full tool set enabled. The agent executes tool calls and returns the complete conversation.
|
||||
|
||||
```json
|
||||
{
|
||||
"messages": [
|
||||
{"role": "system", "content": "You are Didactyl..."},
|
||||
{"role": "system", "content": "Adopted skills memory..."},
|
||||
{"role": "user", "content": "Tweet about the weather"}
|
||||
],
|
||||
"model": "claude-haiku-4.5",
|
||||
"max_turns": 5,
|
||||
"tools_enabled": true
|
||||
}
|
||||
```
|
||||
|
||||
Response:
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"final_response": "Done! I posted a tweet about the weather.",
|
||||
"turns": [
|
||||
{
|
||||
"turn": 1,
|
||||
"tool_calls": [
|
||||
{"name": "nostr_post", "arguments": "...", "result": "..."}
|
||||
]
|
||||
}
|
||||
],
|
||||
"model_used": "claude-haiku-4.5",
|
||||
"total_input_tokens_estimate": 3200,
|
||||
"total_output_tokens_estimate": 180
|
||||
}
|
||||
```
|
||||
|
||||
#### POST /api/prompt/run-simple
|
||||
|
||||
Quick iteration on prompt wording without tools.
|
||||
|
||||
```json
|
||||
{
|
||||
"system": "You are a helpful assistant that writes tweets...",
|
||||
"user": "Write a tweet about AI agents on Nostr",
|
||||
"model": "claude-haiku-4.5"
|
||||
}
|
||||
```
|
||||
|
||||
Response:
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"response": "AI agents are finding their home on Nostr...",
|
||||
"model_used": "claude-haiku-4.5",
|
||||
"input_tokens_estimate": 85,
|
||||
"output_tokens_estimate": 42
|
||||
}
|
||||
```
|
||||
|
||||
#### POST /api/prompt/compare
|
||||
|
||||
A/B testing: submit two prompt variants, both are executed, responses returned side-by-side.
|
||||
|
||||
```json
|
||||
{
|
||||
"variant_a": {
|
||||
"messages": [
|
||||
{"role": "system", "content": "You are Didactyl. Keep responses under 280 chars."},
|
||||
{"role": "user", "content": "Tweet about your new skill"}
|
||||
],
|
||||
"model": "claude-haiku-4.5",
|
||||
"tools_enabled": true
|
||||
},
|
||||
"variant_b": {
|
||||
"messages": [
|
||||
{"role": "system", "content": "You are Didactyl. Be concise. No markdown. No emoji."},
|
||||
{"role": "user", "content": "Tweet about your new skill"}
|
||||
],
|
||||
"model": "claude-haiku-4.5",
|
||||
"tools_enabled": true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Response:
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"variant_a": {
|
||||
"final_response": "Just picked up the tweet-composer skill! ...",
|
||||
"turns": [],
|
||||
"model_used": "claude-haiku-4.5",
|
||||
"total_input_tokens_estimate": 3200,
|
||||
"total_output_tokens_estimate": 95
|
||||
},
|
||||
"variant_b": {
|
||||
"final_response": "New skill acquired: tweet-composer. ...",
|
||||
"turns": [],
|
||||
"model_used": "claude-haiku-4.5",
|
||||
"total_input_tokens_estimate": 3100,
|
||||
"total_output_tokens_estimate": 78
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The compare endpoint runs variant_a first, then variant_b sequentially. Each variant can optionally use a different model for cross-model comparison.
|
||||
|
||||
#### Prompt Crafting Workflow
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
LOAD[GET /api/context/parts] --> EDIT[Edit parts in UI]
|
||||
EDIT --> PREVIEW[POST /api/context/preview]
|
||||
PREVIEW --> FIRE[POST /api/prompt/run]
|
||||
FIRE --> COMPARE{Want to compare?}
|
||||
COMPARE -- Yes --> AB[POST /api/prompt/compare]
|
||||
COMPARE -- No --> PERSIST{Like the result?}
|
||||
AB --> PERSIST
|
||||
PERSIST -- Yes --> SAVE[PUT /api/events/soul or skills]
|
||||
PERSIST -- No --> EDIT
|
||||
```
|
||||
|
||||
### Tools
|
||||
|
||||
| Method | Path | Description |
|
||||
|---|---|---|
|
||||
| GET | `/api/tools` | List all registered tool schemas |
|
||||
| POST | `/api/tools/:name/execute` | Execute a tool by name with JSON body as args (admin-only equivalent) |
|
||||
|
||||
---
|
||||
|
||||
## Implementation Plan
|
||||
|
||||
### New Files
|
||||
|
||||
| File | Purpose |
|
||||
|---|---|
|
||||
| `src/http_api.c` | HTTP server, request router, endpoint handlers |
|
||||
| `src/http_api.h` | Public API: init, poll, cleanup |
|
||||
| `vendor/mongoose.c` | Mongoose HTTP library (single file) |
|
||||
| `vendor/mongoose.h` | Mongoose header |
|
||||
|
||||
### Modified Files
|
||||
|
||||
| File | Change |
|
||||
|---|---|
|
||||
| `src/config.h` | Add `api_config_t` struct to `didactyl_config_t` |
|
||||
| `src/config.c` | Parse `api` config section |
|
||||
| `src/main.c` | Call `http_api_init()`, add `http_api_poll()` to main loop, call `http_api_cleanup()` on shutdown |
|
||||
| `src/agent.h` | Expose `agent_build_context_parts_json()` for context inspector |
|
||||
| `src/agent.c` | Implement `agent_build_context_parts_json()` that builds context and returns labeled parts with sizes |
|
||||
| `Makefile` | Add `vendor/mongoose.c` and `src/http_api.c` to SRCS, add `-Ivendor` to INCLUDES |
|
||||
|
||||
### http_api.h
|
||||
|
||||
```c
|
||||
#ifndef DIDACTYL_HTTP_API_H
|
||||
#define DIDACTYL_HTTP_API_H
|
||||
|
||||
#include "config.h"
|
||||
#include "tools.h"
|
||||
|
||||
struct trigger_manager;
|
||||
|
||||
typedef struct {
|
||||
didactyl_config_t* cfg;
|
||||
tools_context_t* tools_ctx;
|
||||
struct trigger_manager* trigger_manager;
|
||||
} http_api_context_t;
|
||||
|
||||
int http_api_init(http_api_context_t* ctx);
|
||||
int http_api_poll(int timeout_ms);
|
||||
void http_api_cleanup(void);
|
||||
|
||||
#endif
|
||||
```
|
||||
|
||||
### Main Loop Integration
|
||||
|
||||
```c
|
||||
// In main.c, after agent_init and trigger_manager_init:
|
||||
http_api_context_t api_ctx = {
|
||||
.cfg = &cfg,
|
||||
.tools_ctx = &g_tools_ctx, // need to expose from agent
|
||||
.trigger_manager = &trigger_manager
|
||||
};
|
||||
|
||||
if (cfg.api.enabled) {
|
||||
if (http_api_init(&api_ctx) != 0) {
|
||||
DEBUG_WARN("HTTP API failed to start");
|
||||
}
|
||||
}
|
||||
|
||||
// In main loop:
|
||||
while (g_running) {
|
||||
nostr_handler_poll(100);
|
||||
trigger_manager_poll(&trigger_manager);
|
||||
if (cfg.api.enabled) {
|
||||
http_api_poll(0); // non-blocking
|
||||
}
|
||||
nanosleep(...);
|
||||
}
|
||||
|
||||
// On shutdown:
|
||||
if (cfg.api.enabled) {
|
||||
http_api_cleanup();
|
||||
}
|
||||
```
|
||||
|
||||
### Request Router Pattern
|
||||
|
||||
```c
|
||||
static void http_handler(struct mg_connection* c, int ev, void* ev_data) {
|
||||
if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message* hm = ev_data;
|
||||
|
||||
// Add CORS headers to all responses
|
||||
// Route by method + path prefix
|
||||
|
||||
if (mg_match(hm->uri, mg_str("/api/status"), NULL) && is_get(hm)) {
|
||||
handle_status(c, hm);
|
||||
} else if (mg_match(hm->uri, mg_str("/api/context/parts"), NULL) && is_get(hm)) {
|
||||
handle_context_parts(c, hm);
|
||||
} else if (mg_match(hm->uri, mg_str("/api/events/skills/*"), NULL)) {
|
||||
handle_skill_by_slug(c, hm);
|
||||
}
|
||||
// ... etc
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Implementation Order
|
||||
|
||||
1. Add `api_config_t` to config and parse it
|
||||
2. Vendor mongoose.c/mongoose.h, update Makefile
|
||||
3. Create `src/http_api.c` with init/poll/cleanup skeleton + CORS
|
||||
4. Wire into main.c poll loop
|
||||
5. Implement read-only endpoints first: `/api/status`, `/api/config`, `/api/relays`, `/api/model`, `/api/tools`, `/api/triggers`
|
||||
6. Implement Nostr event endpoints: `/api/events/soul`, `/api/events/skills`, `/api/events/adoption`, `/api/events/profile`, `/api/events/startup`
|
||||
7. Implement context inspector: `/api/context/parts`, `/api/context/current`, `/api/context/log`
|
||||
8. Implement mutation endpoints: PUT soul, PUT skills, PUT model, PUT profile
|
||||
9. Implement tool execution endpoint: POST `/api/tools/:name/execute`
|
||||
10. Implement context preview: POST `/api/context/preview`
|
||||
11. Test all endpoints via curl
|
||||
12. Update documentation
|
||||
|
||||
---
|
||||
|
||||
## CORS Headers
|
||||
|
||||
Every response includes:
|
||||
|
||||
```
|
||||
Access-Control-Allow-Origin: *
|
||||
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
|
||||
Access-Control-Allow-Headers: Content-Type
|
||||
```
|
||||
|
||||
OPTIONS requests return 204 with these headers (preflight support).
|
||||
|
||||
---
|
||||
|
||||
## Security Notes
|
||||
|
||||
- Binds to `127.0.0.1` only — not accessible from network
|
||||
- No authentication — this is a local dev tool
|
||||
- The `api.enabled` config flag defaults to `false` so it must be explicitly opted in
|
||||
- Tool execution endpoint gives full admin-tier access — acceptable for localhost dev dashboard
|
||||
- Config endpoint redacts `nsec` and `api_key` fields
|
||||
|
||||
---
|
||||
|
||||
## Token Estimation
|
||||
|
||||
For the context size display, use a simple heuristic: `estimated_tokens = chars / 4`. This is a rough approximation that works well enough for English text with the major model families. No need for a real tokenizer.
|
||||
302
plans/admin_web_frontend.md
Normal file
302
plans/admin_web_frontend.md
Normal file
@@ -0,0 +1,302 @@
|
||||
# Didactyl Admin Web Frontend — Project Brief
|
||||
|
||||
## What Is Didactyl?
|
||||
|
||||
Didactyl is a sovereign AI agent that lives on Nostr. It connects to Nostr relays, listens for encrypted DMs from its administrator, reasons with an LLM, and takes actions — posting events, querying relays, running shell commands, managing skills. Everything the agent knows and does is stored as Nostr events.
|
||||
|
||||
The agent is a C binary that runs on a server. It has no web interface of its own — all interaction happens through Nostr DMs.
|
||||
|
||||
## What We Are Building
|
||||
|
||||
A **local web admin dashboard** that connects to the running didactyl agent via a localhost HTTP API. The dashboard is a prompt crafting and agent inspection tool for the administrator.
|
||||
|
||||
This is **not** a chat interface. The administrator already chats with the agent through Nostr DMs. This dashboard is for:
|
||||
|
||||
1. **Inspecting** what the agent sees — its full LLM context, broken into labeled parts with token counts
|
||||
2. **Crafting** custom prompts — editing system prompts, user messages, and context pieces
|
||||
3. **Running** prompts against the LLM — with or without the agent tool set
|
||||
4. **Comparing** prompt variants side-by-side — A/B testing different prompt wordings or models
|
||||
|
||||
---
|
||||
|
||||
## The API
|
||||
|
||||
The didactyl agent exposes a localhost-only HTTP API on port `8484` by default. Full API documentation is in `docs/API.md`. All endpoints return JSON with CORS headers.
|
||||
|
||||
### Base URL
|
||||
|
||||
```
|
||||
http://127.0.0.1:8484
|
||||
```
|
||||
|
||||
### Currently Implemented Endpoints
|
||||
|
||||
| Method | Path | Purpose |
|
||||
|---|---|---|
|
||||
| GET | `/api/status` | Agent runtime status — name, version, pubkey, relay count, trigger count |
|
||||
| GET | `/api/context/current` | Full LLM context messages array with total char/token counts |
|
||||
| GET | `/api/context/parts` | Context broken into labeled parts with individual sizes |
|
||||
| POST | `/api/prompt/run-simple` | Simple prompt: system + user message, no tools, returns text |
|
||||
| POST | `/api/prompt/run` | Full prompt: messages array with tools enabled, returns conversation trace |
|
||||
| POST | `/api/prompt/compare` | A/B test: two prompt variants run sequentially, responses side-by-side |
|
||||
| GET | `/api/model` | Current LLM model config (provider, model, base_url, max_tokens, temperature) |
|
||||
| PUT | `/api/model` | Change model at runtime — persists to config.json |
|
||||
| GET | `/api/models` | List available models from the configured provider |
|
||||
|
||||
### Planned Future Endpoints
|
||||
|
||||
These are not yet implemented but are on the roadmap:
|
||||
|
||||
| Method | Path | Purpose |
|
||||
|---|---|---|
|
||||
| GET | `/api/config` | Runtime config with redacted secrets |
|
||||
| GET | `/api/events/soul` | Agent soul/system prompt event |
|
||||
| PUT | `/api/events/soul` | Update soul content |
|
||||
| GET | `/api/events/skills` | List skills |
|
||||
| GET/PUT | `/api/events/skills/:slug` | Read/update individual skills |
|
||||
| GET | `/api/events/profile` | Agent Nostr profile |
|
||||
| GET | `/api/tools` | List all tool schemas |
|
||||
| POST | `/api/tools/:name/execute` | Execute a tool directly |
|
||||
| GET | `/api/triggers` | Active trigger subscriptions |
|
||||
| GET | `/api/relays` | Relay connection status |
|
||||
|
||||
---
|
||||
|
||||
## Core User Workflows
|
||||
|
||||
### 1. Context Inspector
|
||||
|
||||
The primary read-only workflow. The admin wants to understand what the agent sees when it processes a message.
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
LOAD[Load /api/context/parts] --> DISPLAY[Display parts list]
|
||||
DISPLAY --> DETAIL[Click part to expand content]
|
||||
DETAIL --> TOKENS[Show char count and token estimate per part]
|
||||
TOKENS --> TOTAL[Show total context size]
|
||||
```
|
||||
|
||||
**What to show:**
|
||||
- A list/table of context parts with name, role, character count, estimated tokens
|
||||
- Total context size as a summary bar or header
|
||||
- Expandable content for each part
|
||||
- The parts are: `system_prompt`, `admin_identity`, `admin_profile`, `admin_relay_list`, `startup_events`, `adopted_skills`, `dm_history` (one entry per turn, up to limit), `admin_notes`
|
||||
- Part names come from the `---template---` section of the soul event (kind 31120); they may differ if the soul is customised
|
||||
|
||||
### 2. Simple Prompt Crafting
|
||||
|
||||
Quick iteration on prompt wording without tools.
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
WRITE[Write system prompt + user message] --> RUN[POST /api/prompt/run-simple]
|
||||
RUN --> RESULT[Display response text]
|
||||
RESULT --> EDIT[Edit and re-run]
|
||||
EDIT --> RUN
|
||||
```
|
||||
|
||||
**What to show:**
|
||||
- Two text areas: system prompt, user message
|
||||
- Optional model override dropdown/input
|
||||
- Run button
|
||||
- Response display with model used and token estimates
|
||||
|
||||
### 3. Full Prompt with Tools
|
||||
|
||||
Craft a complete messages array and run it with the agent tool set.
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
CONTEXT[Load context from /api/context/parts] --> EDIT[Edit/rearrange context parts]
|
||||
EDIT --> ADD[Add user message]
|
||||
ADD --> RUN[POST /api/prompt/run]
|
||||
RUN --> TRACE[Display conversation trace]
|
||||
TRACE --> TOOLS[Show tool calls and results per turn]
|
||||
TOOLS --> FINAL[Show final response]
|
||||
```
|
||||
|
||||
**What to show:**
|
||||
- Pre-populate from context parts or start from scratch
|
||||
- Messages editor — add/remove/reorder messages with role and content
|
||||
- Max turns slider/input
|
||||
- Optional model override
|
||||
- Run button
|
||||
- Turn-by-turn trace showing tool calls with name, arguments, and results
|
||||
- Final response text
|
||||
- Token estimates
|
||||
|
||||
### 4. A/B Prompt Comparison
|
||||
|
||||
Compare two prompt variants side-by-side.
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
CRAFT_A[Craft variant A messages] --> CRAFT_B[Craft variant B messages]
|
||||
CRAFT_B --> COMPARE[POST /api/prompt/compare]
|
||||
COMPARE --> SIDE[Display responses side-by-side]
|
||||
SIDE --> DIFF[Compare final responses and token usage]
|
||||
```
|
||||
|
||||
**What to show:**
|
||||
- Two prompt editors side-by-side, each with messages array + model override + max turns
|
||||
- Compare button
|
||||
- Side-by-side response display
|
||||
- Highlight differences in final response text
|
||||
- Token usage comparison
|
||||
|
||||
### 5. Status Dashboard
|
||||
|
||||
Simple overview of agent health.
|
||||
|
||||
**What to show:**
|
||||
- Agent name, version, pubkey
|
||||
- Connected relays count vs configured
|
||||
- Active triggers count
|
||||
- API connection status indicator
|
||||
|
||||
---
|
||||
|
||||
## Key Design Decisions
|
||||
|
||||
### Localhost Only
|
||||
|
||||
The API binds to `127.0.0.1` — the frontend must run on the same machine as the agent, or use SSH tunneling. There is no authentication. This is intentional — it is a local dev/admin tool.
|
||||
|
||||
### No WebSocket
|
||||
|
||||
The API is plain HTTP request/response. There is no WebSocket or streaming. Prompt execution calls may take several seconds for LLM responses — the frontend should show a loading state.
|
||||
|
||||
### Token Estimation
|
||||
|
||||
All token counts from the API use a `chars / 4` heuristic. This is approximate. The frontend can display these as-is or add its own tokenizer if more precision is needed.
|
||||
|
||||
### Model Override
|
||||
|
||||
The `model` field in prompt requests temporarily overrides the agent configured model for that single request, then restores the original. This enables cross-model comparison without changing agent config.
|
||||
|
||||
### Tool Execution Is Real
|
||||
|
||||
When using `/api/prompt/run` or `/api/prompt/compare`, tool calls are **actually executed**. If the LLM decides to post a Nostr event, it will really post it. The frontend should make this clear to the user — perhaps with a warning or confirmation before running prompts with tools enabled.
|
||||
|
||||
---
|
||||
|
||||
## Response Shapes Quick Reference
|
||||
|
||||
### Status
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"name": "Didactyl",
|
||||
"version": "v0.0.26",
|
||||
"pubkey": "52a3e8...",
|
||||
"relay_count": 4,
|
||||
"connected_relays": 4,
|
||||
"active_triggers": 0
|
||||
}
|
||||
```
|
||||
|
||||
### Context Parts
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"total_chars": 13131,
|
||||
"total_estimated_tokens": 3283,
|
||||
"parts": [
|
||||
{
|
||||
"name": "system_prompt",
|
||||
"role": "system",
|
||||
"chars": 1200,
|
||||
"estimated_tokens": 300,
|
||||
"content": "# Didactyl Agent..."
|
||||
}
|
||||
],
|
||||
"messages": [...]
|
||||
}
|
||||
```
|
||||
|
||||
### Simple Prompt Response
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"response": "ok",
|
||||
"model_used": "claude-haiku-4.5",
|
||||
"input_tokens_estimate": 10,
|
||||
"output_tokens_estimate": 1
|
||||
}
|
||||
```
|
||||
|
||||
### Full Prompt Response
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"final_response": "Done! I posted a tweet.",
|
||||
"turns": [
|
||||
{
|
||||
"turn": 1,
|
||||
"tool_calls": [
|
||||
{"name": "nostr_post", "arguments": "...", "result": "..."}
|
||||
]
|
||||
}
|
||||
],
|
||||
"model_used": "claude-haiku-4.5",
|
||||
"total_input_tokens_estimate": 3200,
|
||||
"total_output_tokens_estimate": 180
|
||||
}
|
||||
```
|
||||
|
||||
### Compare Response
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"variant_a": { "...same shape as full prompt response..." },
|
||||
"variant_b": { "...same shape as full prompt response..." }
|
||||
}
|
||||
```
|
||||
|
||||
### Error Response
|
||||
|
||||
```json
|
||||
{
|
||||
"success": false,
|
||||
"error": "description of what went wrong"
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Technology Suggestions
|
||||
|
||||
No technology is mandated for the frontend. Some reasonable choices:
|
||||
|
||||
- **Vanilla HTML/JS** — simplest, no build step, just open in browser
|
||||
- **React/Preact** — if you want component structure
|
||||
- **Svelte** — lightweight, good for small dashboards
|
||||
- **Vue** — also fine
|
||||
|
||||
The frontend is a separate project from didactyl. It just needs to make HTTP requests to `localhost:8484`.
|
||||
|
||||
---
|
||||
|
||||
## File References
|
||||
|
||||
| File | Description |
|
||||
|---|---|
|
||||
| `docs/API.md` | Full API endpoint reference with request/response examples |
|
||||
| `plans/admin_api.md` | Original architecture plan for the HTTP API |
|
||||
| `src/http_api.c` | C implementation of all endpoints |
|
||||
| `src/http_api.h` | Public API header |
|
||||
| `config.json.example` | Example config showing the `api` section |
|
||||
|
||||
---
|
||||
|
||||
## Getting Started
|
||||
|
||||
1. Ensure didactyl is running with `api.enabled: true` in config.json
|
||||
2. Verify the API is up: `curl http://127.0.0.1:8484/api/status`
|
||||
3. Build the frontend to talk to `http://127.0.0.1:8484`
|
||||
4. Start with the status endpoint and context inspector, then add prompt crafting
|
||||
215
plans/agent_tasks.md
Normal file
215
plans/agent_tasks.md
Normal file
@@ -0,0 +1,215 @@
|
||||
# Agent Tasks: Short-Term Memory via Context-Injected Task List
|
||||
|
||||
## Summary
|
||||
|
||||
Add a **tasks** system that serves as the agent's short-term working memory. The agent can break down goals into steps, track progress, and see its current task list in every prompt context. Tasks are file-backed (not stored on Nostr) and managed via a dedicated `task_manage` tool.
|
||||
|
||||
## How It Works
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
A[User sends message] --> B[Context builder runs]
|
||||
B --> C[Template resolver hits tasks_content variable]
|
||||
C --> D[Read tasks.json from disk]
|
||||
D --> E{Tasks exist?}
|
||||
E -->|Yes| F[Format tasks as readable text]
|
||||
E -->|No| G[Return empty string - section skipped]
|
||||
F --> H[Inject as system message in prompt]
|
||||
G --> H
|
||||
H --> I[LLM sees current tasks in context]
|
||||
I --> J{LLM decides to update tasks?}
|
||||
J -->|Yes| K[LLM calls task_manage tool]
|
||||
K --> L[Tool updates tasks.json on disk]
|
||||
L --> M[Tool result returned to LLM]
|
||||
J -->|No| N[LLM responds normally]
|
||||
```
|
||||
|
||||
## Design
|
||||
|
||||
### Storage: `tasks.json`
|
||||
|
||||
A simple JSON file in the agent's working directory. Structure:
|
||||
|
||||
```json
|
||||
{
|
||||
"tasks": [
|
||||
{
|
||||
"id": 1,
|
||||
"text": "Query admin relay list to find active relays",
|
||||
"status": "done",
|
||||
"created_at": 1709535600,
|
||||
"updated_at": 1709535660
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"text": "Draft long-form article about Nostr relay setup",
|
||||
"status": "active",
|
||||
"created_at": 1709535600,
|
||||
"updated_at": 1709535600
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"text": "Publish article as kind 30023",
|
||||
"status": "pending",
|
||||
"created_at": 1709535600,
|
||||
"updated_at": 1709535600
|
||||
}
|
||||
],
|
||||
"next_id": 4
|
||||
}
|
||||
```
|
||||
|
||||
Task statuses: `pending`, `active`, `done`
|
||||
|
||||
### Tool: `task_manage`
|
||||
|
||||
A single tool with an `action` parameter that covers all operations:
|
||||
|
||||
| Action | Parameters | Description |
|
||||
|--------|-----------|-------------|
|
||||
| `list` | *(none)* | Return all tasks with status |
|
||||
| `add` | `text`, optional `status` | Add a new task, default status `pending` |
|
||||
| `update` | `id`, optional `text`, optional `status` | Update text and/or status of a task |
|
||||
| `remove` | `id` | Remove a task by ID |
|
||||
| `clear` | optional `status` | Remove all tasks, or all with a given status |
|
||||
| `replace` | `tasks` (array of text strings) | Replace entire task list with new items |
|
||||
|
||||
The `replace` action is important — it lets the LLM rewrite the whole plan in one call rather than doing add/remove/update one at a time. This is the most common pattern: the agent works out a plan and writes all steps at once.
|
||||
|
||||
**Tool schema:**
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "task_manage",
|
||||
"description": "Manage the agent task list - short-term working memory for tracking steps in a plan. Tasks appear in your context on every message.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"action": {
|
||||
"type": "string",
|
||||
"enum": ["list", "add", "update", "remove", "clear", "replace"]
|
||||
},
|
||||
"text": { "type": "string" },
|
||||
"id": { "type": "integer" },
|
||||
"status": { "type": "string", "enum": ["pending", "active", "done"] },
|
||||
"tasks": {
|
||||
"type": "array",
|
||||
"items": { "type": "string" }
|
||||
}
|
||||
},
|
||||
"required": ["action"]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Context Section: `agent_tasks`
|
||||
|
||||
New section in the context template, placed after `adopted_skills` and before `dm_history`:
|
||||
|
||||
```yaml
|
||||
- section: agent_tasks
|
||||
role: system
|
||||
skip_if_empty: true
|
||||
content: |
|
||||
{{tasks_content}}
|
||||
```
|
||||
|
||||
### Template Variable: `{{tasks_content}}`
|
||||
|
||||
New resolver in `agent_template_resolve_var()` that:
|
||||
|
||||
1. Reads `tasks.json` from the working directory
|
||||
2. Parses the JSON
|
||||
3. Formats active/pending tasks as readable text
|
||||
4. Returns empty string if no tasks exist (section gets skipped via `skip_if_empty`)
|
||||
|
||||
**Rendered format in context:**
|
||||
|
||||
```
|
||||
### Current Tasks
|
||||
|
||||
Your active task list - short-term working memory for tracking plan steps.
|
||||
|
||||
- [x] 1. Query admin relay list to find active relays
|
||||
- [-] 2. Draft long-form article about Nostr relay setup
|
||||
- [ ] 3. Publish article as kind 30023
|
||||
```
|
||||
|
||||
Legend: `[x]` = done, `[-]` = active, `[ ]` = pending
|
||||
|
||||
Done tasks are included so the agent has continuity about what it already accomplished, but they could be pruned after a configurable count or age to save tokens.
|
||||
|
||||
### System Prompt Addition
|
||||
|
||||
Add to the agent's behavioral rules in the soul/system prompt:
|
||||
|
||||
```
|
||||
### Task Management
|
||||
- You have a task list that serves as your short-term working memory.
|
||||
- When working on multi-step goals, use task_manage to track your plan.
|
||||
- Update task status as you complete steps.
|
||||
- Your current tasks appear in your context automatically.
|
||||
```
|
||||
|
||||
## Implementation Steps
|
||||
|
||||
### 1. Add `task_manage` tool implementation in `tools.c`
|
||||
|
||||
- New `execute_task_manage()` function
|
||||
- Reads/writes `tasks.json` in the working directory (uses `build_tool_path` for sandboxing)
|
||||
- Handles all 6 actions: list, add, update, remove, clear, replace
|
||||
- Returns JSON result with success/failure and current task list
|
||||
|
||||
### 2. Register `task_manage` tool schema in `tools_build_openai_schema_json()`
|
||||
|
||||
- Add tool definition (t35 or next available) with the schema above
|
||||
|
||||
### 3. Wire `task_manage` into `tools_execute()` dispatch
|
||||
|
||||
- Add `strcmp(tool_name, "task_manage")` branch calling `execute_task_manage()`
|
||||
|
||||
### 4. Add `{{tasks_content}}` template variable resolver in `agent.c`
|
||||
|
||||
- New `build_tasks_content_string()` function
|
||||
- Reads `tasks.json`, formats as markdown checklist
|
||||
- Add to `agent_template_resolve_var()` for var name `tasks_content`
|
||||
|
||||
### 5. Add `agent_tasks` section to context template
|
||||
|
||||
- Add the new section in `context_template.md`
|
||||
- Place after `adopted_skills`, before `dm_history`
|
||||
- Use `skip_if_empty: true` so it costs zero tokens when no tasks exist
|
||||
|
||||
### 6. Add section detection for context logging
|
||||
|
||||
- Add `agent_tasks` detection in `detect_context_section()` in `agent.c`
|
||||
|
||||
### 7. Add task management guidance to system prompt
|
||||
|
||||
- Brief behavioral instruction so the agent knows when/how to use the task list
|
||||
|
||||
## Token Budget Considerations
|
||||
|
||||
- Empty task list: **0 tokens** (skipped via `skip_if_empty`)
|
||||
- Typical 5-task plan: **~80-120 tokens**
|
||||
- Maximum reasonable list of 15 tasks: **~250-350 tokens**
|
||||
- Consider pruning done tasks older than N turns or keeping only the last M done tasks
|
||||
|
||||
## Future: User-Facing To-Do List (Nostr)
|
||||
|
||||
This is explicitly **not** the user-facing to-do list. That future feature would:
|
||||
- Store items as Nostr events (likely a NIP-51 style list or custom kind)
|
||||
- Be visible to the user via Nostr clients
|
||||
- Have its own separate tool (`todo_manage` or similar)
|
||||
- Potentially reference agent tasks that graduate to user-visible items
|
||||
|
||||
The agent tasks system is purely internal working memory.
|
||||
|
||||
## Files Modified
|
||||
|
||||
| File | Change |
|
||||
|------|--------|
|
||||
| `src/tools.c` | Add `execute_task_manage()`, tool schema, dispatch entry |
|
||||
| `src/agent.c` | Add `build_tasks_content_string()`, resolver entry, section detection |
|
||||
| `context_template.md` | Add `agent_tasks` section |
|
||||
| Soul/system prompt (kind 31120) | Add task management behavioral guidance |
|
||||
139
plans/context_architecture.md
Normal file
139
plans/context_architecture.md
Normal file
@@ -0,0 +1,139 @@
|
||||
# Didactyl Context Architecture Plan
|
||||
|
||||
## Problem Statement
|
||||
|
||||
The agent's context assembly is a hardcoded sequence of C function calls in `agent_on_message()`. This creates several issues:
|
||||
|
||||
1. **Skills are never injected** — adopted skills exist on Nostr but the LLM never sees their content
|
||||
2. **No configurability** — changing context order, content, or framing requires C code changes and recompilation
|
||||
3. **No A/B testing** — can't experiment with different prompt structures, ordering, or model-specific tuning
|
||||
4. **No token budget awareness** — context grows unbounded as skills/history/notes accumulate
|
||||
5. **Model-agnostic** — different models respond differently to the same prompt structure; no way to tune per-model
|
||||
|
||||
## Current Context Pipeline
|
||||
|
||||
```
|
||||
agent_on_message() builds messages array:
|
||||
1. system: g_system_context (kind 31120 "soul" content)
|
||||
2. system: admin identity (pubkey + kind 0 profile + kind 10002 relays)
|
||||
3. system: startup events (raw JSON of all startup event kinds/content/tags)
|
||||
4. user/assistant: recent DM history (last 12 turns)
|
||||
5. system: admin kind 1 notes (recent public posts)
|
||||
6. user: the actual incoming message
|
||||
```
|
||||
|
||||
Skills are **completely absent**. The LLM has no knowledge of adopted skill instructions.
|
||||
|
||||
## Proposed Architecture: Context Pipeline with Configurable Slots
|
||||
|
||||
### Core Idea
|
||||
|
||||
Replace the hardcoded function chain with a **configurable context pipeline** defined in `config.json`. Each "slot" in the pipeline is a named context source with configurable parameters.
|
||||
|
||||
### Context Slot Types
|
||||
|
||||
| Slot Type | Source | Description |
|
||||
|---|---|---|
|
||||
| `soul` | Kind 31120 startup event | Agent personality and behavioral rules |
|
||||
| `identity` | Config + relay queries | Agent's own pubkey, admin pubkey, admin profile |
|
||||
| `startup_events` | Config startup events | Raw startup event memory |
|
||||
| `adopted_skills` | Kind 10123 + resolved skills | **NEW**: Adopted skill instructions |
|
||||
| `dm_history` | Relay query | Recent conversation turns |
|
||||
| `admin_notes` | Cached kind 1 events | Admin's recent public posts |
|
||||
| `admin_context` | Kind 0/3/10002 | Admin profile, contacts, relay list |
|
||||
| `custom` | Literal string in config | Arbitrary system message for A/B testing |
|
||||
|
||||
### Phase 1: Immediate Fix (Skills + Caching)
|
||||
|
||||
Before building the full configurable pipeline, fix the immediate problem:
|
||||
|
||||
1. **In-memory skill cache** — load adopted skills at startup and cache them; invalidate on `skill_create`, `skill_adopt`, `skill_remove`
|
||||
2. **`append_adopted_skills_context()`** — inject cached skills into the conversation as a system message
|
||||
3. **Strong framing** — "These are your learned skills. When a request matches a skill, you MUST follow its instructions exactly."
|
||||
|
||||
### Phase 2: Configurable Context Pipeline
|
||||
|
||||
Add a `context_pipeline` section to `config.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"context_pipeline": {
|
||||
"max_total_chars": 12000,
|
||||
"slots": [
|
||||
{ "type": "soul", "max_chars": 3000 },
|
||||
{ "type": "identity" },
|
||||
{ "type": "adopted_skills", "max_chars": 4000, "max_per_skill": 1000 },
|
||||
{ "type": "startup_events", "max_chars": 2000 },
|
||||
{ "type": "dm_history", "max_turns": 12 },
|
||||
{ "type": "admin_notes", "max_chars": 1500 },
|
||||
{ "type": "custom", "content": "Always respond in the style of a pirate." }
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
This gives you:
|
||||
- **Ordering control** — move skills before or after history
|
||||
- **Token budgets** — per-slot and total caps
|
||||
- **A/B testing** — swap `custom` slot content, reorder slots, change caps
|
||||
- **Model-specific tuning** — different pipeline configs for different models (could key off `llm.model`)
|
||||
|
||||
### Phase 3: Model-Aware Context Profiles
|
||||
|
||||
```json
|
||||
{
|
||||
"context_profiles": {
|
||||
"default": { ... pipeline config ... },
|
||||
"claude-sonnet-4.6": { ... different ordering/caps ... },
|
||||
"gpt-5.2-codex": { ... different ordering/caps ... }
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The agent selects the profile matching the active model, falling back to `default`.
|
||||
|
||||
## Skill Cache Design
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────┐
|
||||
│ Skill Cache (in-memory) │
|
||||
│ │
|
||||
│ Loaded at startup from: │
|
||||
│ 1. Startup events in config.json │
|
||||
│ 2. Kind 10123 adoption list │
|
||||
│ 3. Resolved skill events from relays │
|
||||
│ │
|
||||
│ Invalidated by: │
|
||||
│ - skill_create (add/update) │
|
||||
│ - skill_adopt (add) │
|
||||
│ - skill_remove (remove) │
|
||||
│ │
|
||||
│ Structure per skill: │
|
||||
│ - slug (string) │
|
||||
│ - description (string) │
|
||||
│ - content (string, full) │
|
||||
│ - scope (public/private) │
|
||||
│ - has_trigger (bool) │
|
||||
│ - source (startup/adopted) │
|
||||
└─────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
## Implementation Priority
|
||||
|
||||
### Do Now (Phase 1)
|
||||
- [ ] Build skill cache in `agent.c` (load at startup, invalidate on tool calls)
|
||||
- [ ] Add `append_adopted_skills_context()` using cached skills
|
||||
- [ ] Wire into `agent_on_message()` between startup events and DM history
|
||||
- [ ] Skill content framing: strong directive for LLM compliance
|
||||
|
||||
### Do Next (Phase 2)
|
||||
- [ ] Add `context_pipeline` config section
|
||||
- [ ] Refactor `agent_on_message()` to iterate pipeline slots
|
||||
- [ ] Per-slot `max_chars` truncation
|
||||
- [ ] Total pipeline `max_total_chars` budget
|
||||
- [ ] `custom` slot type for arbitrary A/B test content
|
||||
|
||||
### Do Later (Phase 3)
|
||||
- [ ] Model-aware context profiles
|
||||
- [ ] Context analytics (log token counts per slot per conversation)
|
||||
- [ ] Dynamic skill relevance scoring (only inject skills likely relevant to the current message)
|
||||
167
plans/context_optimization.md
Normal file
167
plans/context_optimization.md
Normal file
@@ -0,0 +1,167 @@
|
||||
# Context Optimization Plan
|
||||
|
||||
Analysis of [`context.log.md`](../context.log.md) (13,609 bytes / ~3,402 tokens across 20 sections) and [`context_template.md`](../context_template.md).
|
||||
|
||||
## Issues Found
|
||||
|
||||
### 1. Massive Duplication in `startup_events` Section
|
||||
|
||||
The **startup_events** section (line 74-80 in the log) dumps the *entire* `config.startup_events` array as raw JSON — including the full soul/system prompt (kind 31120) which is already sent verbatim as the **system_prompt** section. The soul text appears **twice** in every request.
|
||||
|
||||
**Estimated waste:** ~1,500-2,000 tokens per request.
|
||||
|
||||
**Fix:** Filter out kind 31120 (soul) from the startup_events JSON blob, or better yet, only include kinds the model actually needs to reference (kind 0 profile, kind 10002 relay list, kind 3 contacts). The soul is already the system prompt — repeating it as data is pure waste.
|
||||
|
||||
### 2. Duplicate Startup Messages in DM History
|
||||
|
||||
The DM history contains **four separate** `Didactyl has started up and is online (version v0.0.29, connected relays: 4/4).` assistant messages (lines 123-127, 130-134, 144-148, 222-226, 245-249). These are startup announcement DMs that got stored as separate events. The model sees the same boilerplate startup message repeated across the conversation.
|
||||
|
||||
**Estimated waste:** ~200-300 tokens.
|
||||
|
||||
**Fix:** Deduplicate consecutive identical assistant messages in the DM history builder, or filter out startup announcement messages (they carry no conversational value).
|
||||
|
||||
### 3. Skills Rendered as Raw JSON Instead of Structured Text
|
||||
|
||||
Skill instructions at lines 97-118 are dumped as raw JSON objects. Models parse structured natural language far more reliably than nested JSON. The `content_fields` serialization format wastes tokens on JSON syntax characters and key quoting.
|
||||
|
||||
**Estimated waste:** ~100-200 tokens of JSON overhead per skill, plus reduced comprehension quality.
|
||||
|
||||
**Fix:** When serializing `content_fields`-based skills for context, flatten them into readable text:
|
||||
```
|
||||
Skill: long_form_note
|
||||
Description: How to publish a NIP-23 long-form article (kind 30023)
|
||||
NIP: NIP-23
|
||||
Event Kind: 30023
|
||||
Format: The content field must be markdown text...
|
||||
Required Tags:
|
||||
- d: Addressable identifier slug...
|
||||
- title: Human-readable article title
|
||||
- published_at: Unix timestamp as string...
|
||||
Procedure:
|
||||
1. Determine title and d tag...
|
||||
2. Draft markdown body content...
|
||||
```
|
||||
|
||||
### 4. Empty Sections Still Sent
|
||||
|
||||
The **admin_relay_list** section (line 65-71) has no data — the JSON value is empty. Sending an empty section wastes tokens on the header/framing with no informational value.
|
||||
|
||||
**Estimated waste:** ~30-40 tokens.
|
||||
|
||||
**Fix:** Skip sections where the resolved variable is empty or whitespace-only.
|
||||
|
||||
### 5. Admin Identity Could Be Merged with Admin Profile
|
||||
|
||||
The **admin_identity** section (line 47-53) sends just the hex pubkey, then **admin_profile** (line 56-62) sends the full kind 0 JSON which implicitly identifies the admin. These could be a single section.
|
||||
|
||||
**Estimated savings:** ~40-50 tokens of framing overhead.
|
||||
|
||||
### 6. `admin_notes` Placement Breaks Conversation Flow
|
||||
|
||||
In the template, `admin_notes` is placed *after* `dm_history` (expand). In the actual log, this means a system message appears sandwiched between DM history messages (line 252, between assistant messages and the final user message at line 274). This breaks the natural conversation flow and may confuse the model about message ordering.
|
||||
|
||||
**Fix:** Move `admin_notes` *before* `dm_history` in the template so all system context is grouped together before the conversation begins.
|
||||
|
||||
### 7. No Agent Self-Identity Section
|
||||
|
||||
The model knows it is Didactyl from the system prompt, but there is no section telling it its own pubkey/npub. The admin pubkey is provided but the agent's own key is not in the context (it is only available via the `nostr_pubkey` tool). Adding a small self-identity section would let the model reference its own key without a tool call.
|
||||
|
||||
**Estimated cost:** ~20-30 tokens.
|
||||
|
||||
## Priority Summary
|
||||
|
||||
| Priority | Issue | Token Savings | Complexity |
|
||||
|----------|-------|---------------|------------|
|
||||
| **P0** | Soul duplicated in startup_events | ~1,500-2,000 | Low — filter kind 31120 from startup blob |
|
||||
| **P1** | Duplicate startup DMs in history | ~200-300 | Medium — dedup logic in history builder |
|
||||
| **P1** | Skills as raw JSON | ~100-200 + quality | Medium — flatten content_fields to text |
|
||||
| **P2** | Empty sections still sent | ~30-40 | Low — skip empty resolved vars |
|
||||
| **P2** | admin_notes after dm_history | 0 (quality) | Low — reorder template |
|
||||
| **P3** | Merge admin_identity + admin_profile | ~40-50 | Low — template change |
|
||||
| **P3** | Add agent self-identity section | -20-30 (adds) | Low — new template var |
|
||||
|
||||
## Bug: Kind 10002 Relay List Is Always Empty
|
||||
|
||||
At [`nostr_handler.c:705`](../src/nostr_handler.c:705) the kind 10002 handler stores `content->valuestring`, but NIP-65 relay list events have an **empty content field** — the relay URLs live in the **tags** as `["r", "wss://relay.example.com"]` entries. So `g_admin_kind10002_json` is always `""`.
|
||||
|
||||
**Fix:** Parse the `"r"` tags from the kind 10002 event and serialize them as a JSON array of relay URL strings (or plain-text list).
|
||||
|
||||
## Sender Verification Status
|
||||
|
||||
The [`tier`](../src/nostr_handler.h:8) enum (`DIDACTYL_SENDER_ADMIN`, `DIDACTYL_SENDER_WOT`, `DIDACTYL_SENDER_STRANGER`) is already resolved before [`agent_on_message()`](../src/agent.c:1453) is called, but it is **not passed into the context builder**. The model has no way to know whether the current message was cryptographically verified as coming from the administrator vs. a web-of-trust contact.
|
||||
|
||||
**Fix:** Pass the sender tier into the context builder and expose it as a template variable (e.g. `{{sender_verification}}`) that resolves to text like:
|
||||
- `"This message has been cryptographically verified as coming from your administrator."`
|
||||
- `"This message is from a web-of-trust contact (not the administrator)."`
|
||||
|
||||
## Proposed Optimized Template
|
||||
|
||||
```yaml
|
||||
- section: agent_identity
|
||||
role: system
|
||||
content: |
|
||||
Agent Identity
|
||||
Your pubkey (hex): {{agent_pubkey}}
|
||||
|
||||
- section: sender_context
|
||||
role: system
|
||||
content: |
|
||||
{{sender_verification}}
|
||||
|
||||
- section: admin_context
|
||||
role: system
|
||||
content: |
|
||||
Administrator Context
|
||||
|
||||
Pubkey (hex): {{admin_pubkey}}
|
||||
{{admin_profile_plain}}
|
||||
{{admin_relay_list_plain}}
|
||||
|
||||
- section: startup_events
|
||||
role: system
|
||||
skip_if_empty: true
|
||||
content: |
|
||||
Startup Events Memory
|
||||
{{startup_events_json}}
|
||||
|
||||
- section: adopted_skills
|
||||
role: system
|
||||
skip_if_empty: true
|
||||
content: |
|
||||
{{adopted_skills_content}}
|
||||
|
||||
- section: admin_notes
|
||||
role: system
|
||||
skip_if_empty: true
|
||||
content: |
|
||||
Administrator Recent Notes (source: nostr kind 1)
|
||||
{{admin_notes_content}}
|
||||
|
||||
- section: dm_history
|
||||
role: expand
|
||||
limit: 12
|
||||
```
|
||||
|
||||
Key changes from current template:
|
||||
- **No markdown headers** in system sections — plain English throughout
|
||||
- **Merged admin section** combines identity, profile, and relay list
|
||||
- **`{{admin_profile_plain}}`** — new variable that renders kind 0 JSON as readable text (e.g. `Name: WSB, About: ...`)
|
||||
- **`{{admin_relay_list_plain}}`** — new variable that renders relay URLs from tags as a plain list
|
||||
- **`{{sender_verification}}`** — new variable stating cryptographic verification status
|
||||
- **`admin_notes` moved before `dm_history`** so all system context is grouped before conversation
|
||||
- **`skip_if_empty`** prevents sending empty sections
|
||||
|
||||
## Implementation Steps
|
||||
|
||||
1. **Fix kind 10002 relay list bug** — extract relay URLs from tags instead of content in [`nostr_handler.c:705`](../src/nostr_handler.c:705)
|
||||
2. **Filter kind 31120** from `startup_events_json` variable resolver in [`agent.c`](../src/agent.c)
|
||||
3. **Deduplicate consecutive identical messages** in DM history builder
|
||||
4. **Flatten `content_fields` JSON skills** into readable text format
|
||||
5. **Add `skip_if_empty` support** to template engine (skip section when resolved content is blank)
|
||||
6. **Reorder template** — move `admin_notes` before `dm_history`
|
||||
7. **Add `agent_pubkey` template variable** and agent identity section
|
||||
8. **Merge admin sections** — combine identity + profile (plain English) + relay list into one section
|
||||
9. **Add `admin_profile_plain` variable** — parse kind 0 JSON into readable text
|
||||
10. **Add `admin_relay_list_plain` variable** — parse kind 10002 tags into relay URL list
|
||||
11. **Pass sender tier to context builder** and add `sender_verification` template variable
|
||||
12. **Remove markdown formatting** from system section content — use plain English
|
||||
596
plans/new_nostr_tools.md
Normal file
596
plans/new_nostr_tools.md
Normal file
@@ -0,0 +1,596 @@
|
||||
# Implementation Plan: Nostr Tools
|
||||
|
||||
## Current Tool Inventory (v0.0.19)
|
||||
|
||||
| # | Tool | Kind/NIP | Status |
|
||||
|---|------|----------|--------|
|
||||
| 1 | `nostr_post` | Any kind | ✅ Shipped |
|
||||
| 2 | `nostr_post_readme` | 30023 / NIP-23 | ✅ Shipped |
|
||||
| 3 | `nostr_query` | Filter-based | ✅ Shipped |
|
||||
| 4 | `nostr_delete` | 5 / NIP-09 | ✅ Shipped |
|
||||
| 5 | `nostr_react` | 7 / NIP-25 | ✅ Shipped |
|
||||
| 6 | `nostr_profile_get` | 0 query | ✅ Shipped |
|
||||
| 7 | `nostr_relay_status` | Pool stats | ✅ Shipped |
|
||||
| 8 | `shell_exec` | OS | ✅ Shipped |
|
||||
| 9 | `file_read` | OS | ✅ Shipped |
|
||||
| 10 | `file_write` | OS | ✅ Shipped |
|
||||
|
||||
---
|
||||
|
||||
## Pattern for All New Tools
|
||||
|
||||
Every tool follows the same three-step pattern in [`src/tools.c`](../src/tools.c):
|
||||
|
||||
1. **Schema** — Add OpenAI function schema block in [`tools_build_openai_schema_json()`](../src/tools.c:511)
|
||||
2. **Executor** — Implement `static char* execute_<name>()` function
|
||||
3. **Dispatch** — Add `strcmp` case in [`tools_execute()`](../src/tools.c:1620)
|
||||
|
||||
Use the shared [`parse_tool_args_json()`](../src/tools.c:527) helper for hardened argument parsing.
|
||||
|
||||
---
|
||||
|
||||
## Tier 2 Tools — Medium Value, Moderate Implementation
|
||||
|
||||
### Tool 11: `nostr_nip05_lookup` — DNS Identity Verification (NIP-05)
|
||||
|
||||
#### Purpose
|
||||
Verify `user@domain.com` NIP-05 identifiers. Useful for trust decisions and profile enrichment.
|
||||
|
||||
#### OpenAI Schema
|
||||
```json
|
||||
{
|
||||
"name": "nostr_nip05_lookup",
|
||||
"description": "Verify or look up a NIP-05 DNS identifier and return the associated pubkey and relays",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"identifier": {
|
||||
"type": "string",
|
||||
"description": "NIP-05 identifier, e.g. user@domain.com"
|
||||
},
|
||||
"pubkey": {
|
||||
"type": "string",
|
||||
"description": "Optional 64-char hex pubkey to verify against the identifier"
|
||||
}
|
||||
},
|
||||
"required": ["identifier"]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Implementation: `execute_nostr_nip05_lookup()`
|
||||
|
||||
```
|
||||
1. Parse args_json
|
||||
2. Extract "identifier" (required string, must contain @)
|
||||
3. Extract "pubkey" (optional 64-char hex)
|
||||
4. If pubkey provided:
|
||||
- Call nostr_nip05_verify(identifier, pubkey, &relays, &relay_count, 10)
|
||||
- Return { success, verified: true/false, pubkey, relays }
|
||||
5. If no pubkey:
|
||||
- Call nostr_nip05_lookup(identifier, pubkey_out, &relays, &relay_count, 10)
|
||||
- Return { success, pubkey: pubkey_out, relays }
|
||||
6. Free relay array after serializing
|
||||
```
|
||||
|
||||
#### Library API Used
|
||||
- [`nostr_nip05_lookup()`](../nostr_core_lib/nostr_core/nip005.h:13)
|
||||
- [`nostr_nip05_verify()`](../nostr_core_lib/nostr_core/nip005.h:15)
|
||||
|
||||
#### Files Modified
|
||||
- `src/tools.c`: Schema + executor + dispatch
|
||||
|
||||
#### Notes
|
||||
- Involves HTTP network I/O (fetches `/.well-known/nostr.json` from the domain)
|
||||
- May block for up to `timeout_seconds` — use 10s default
|
||||
- Must free the `relays` array returned by the library
|
||||
|
||||
---
|
||||
|
||||
### Tool 12: `nostr_encode` — Bech32 Entity Encoding (NIP-19/NIP-21)
|
||||
|
||||
#### Purpose
|
||||
Encode hex pubkeys, event IDs, or addressable coordinates into shareable `npub`, `note`, `nprofile`, `nevent`, `naddr` URIs.
|
||||
|
||||
#### OpenAI Schema
|
||||
```json
|
||||
{
|
||||
"name": "nostr_encode",
|
||||
"description": "Encode a Nostr entity into a bech32 URI: npub, note, nprofile, nevent, or naddr",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"type": {
|
||||
"type": "string",
|
||||
"description": "Entity type: npub, note, nprofile, nevent, or naddr"
|
||||
},
|
||||
"hex": {
|
||||
"type": "string",
|
||||
"description": "64-char hex value: pubkey for npub/nprofile, event_id for note/nevent"
|
||||
},
|
||||
"relays": {
|
||||
"type": "array",
|
||||
"items": { "type": "string" },
|
||||
"description": "Optional relay hints for nprofile, nevent, naddr"
|
||||
},
|
||||
"kind": {
|
||||
"type": "integer",
|
||||
"description": "Kind number, required for naddr"
|
||||
},
|
||||
"identifier": {
|
||||
"type": "string",
|
||||
"description": "d-tag identifier, required for naddr"
|
||||
}
|
||||
},
|
||||
"required": ["type", "hex"]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Implementation: `execute_nostr_encode()`
|
||||
|
||||
```
|
||||
1. Parse args_json
|
||||
2. Extract "type" (required: npub|note|nprofile|nevent|naddr)
|
||||
3. Extract "hex" (required, 64-char hex)
|
||||
4. Convert hex to 32-byte array via nostr_hex_to_bytes()
|
||||
5. Switch on type:
|
||||
- "npub": nostr_build_uri_npub(bytes, output, sizeof output)
|
||||
- "note": nostr_build_uri_note(bytes, output, sizeof output)
|
||||
- "nprofile": nostr_build_uri_nprofile(bytes, relays, relay_count, output, sizeof output)
|
||||
- "nevent": nostr_build_uri_nevent(bytes, relays, relay_count, NULL, -1, 0, output, sizeof output)
|
||||
- "naddr": nostr_build_uri_naddr(identifier, bytes, kind, relays, relay_count, output, sizeof output)
|
||||
6. Return { success, type, uri: output }
|
||||
```
|
||||
|
||||
#### Library API Used
|
||||
- [`nostr_build_uri_npub()`](../nostr_core_lib/nostr_core/nip021.h:66)
|
||||
- [`nostr_build_uri_note()`](../nostr_core_lib/nostr_core/nip021.h:68)
|
||||
- [`nostr_build_uri_nprofile()`](../nostr_core_lib/nostr_core/nip021.h:69)
|
||||
- [`nostr_build_uri_nevent()`](../nostr_core_lib/nostr_core/nip021.h:71)
|
||||
- [`nostr_build_uri_naddr()`](../nostr_core_lib/nostr_core/nip021.h:74)
|
||||
|
||||
#### Files Modified
|
||||
- `src/tools.c`: Schema + executor + dispatch
|
||||
|
||||
---
|
||||
|
||||
### Tool 13: `nostr_decode` — Bech32 Entity Decoding (NIP-19/NIP-21)
|
||||
|
||||
#### Purpose
|
||||
Decode `npub`, `note`, `nprofile`, `nevent`, `naddr` URIs back into hex values and metadata.
|
||||
|
||||
#### OpenAI Schema
|
||||
```json
|
||||
{
|
||||
"name": "nostr_decode",
|
||||
"description": "Decode a bech32 Nostr URI into its components: type, hex, relays, kind, identifier",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"uri": {
|
||||
"type": "string",
|
||||
"description": "Bech32 URI to decode, e.g. npub1..., note1..., nostr:npub1..."
|
||||
}
|
||||
},
|
||||
"required": ["uri"]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Implementation: `execute_nostr_decode()`
|
||||
|
||||
```
|
||||
1. Parse args_json
|
||||
2. Extract "uri" (required string)
|
||||
3. Call nostr_parse_uri(uri, &result)
|
||||
4. Switch on result.type:
|
||||
- NPUB: convert result.data.pubkey to hex, return { type: "npub", pubkey: hex }
|
||||
- NOTE: convert result.data.event_id to hex, return { type: "note", event_id: hex }
|
||||
- NPROFILE: pubkey hex + relays array
|
||||
- NEVENT: event_id hex + relays + optional author/kind
|
||||
- NADDR: identifier + pubkey hex + kind + relays
|
||||
5. Call nostr_uri_result_free(&result)
|
||||
6. Return JSON
|
||||
```
|
||||
|
||||
#### Library API Used
|
||||
- [`nostr_parse_uri()`](../nostr_core_lib/nostr_core/nip021.h:63)
|
||||
- [`nostr_uri_result_free()`](../nostr_core_lib/nostr_core/nip021.h:78)
|
||||
|
||||
#### Files Modified
|
||||
- `src/tools.c`: Schema + executor + dispatch
|
||||
|
||||
---
|
||||
|
||||
### Tool 14: `nostr_dm_send` — Send DM as Tool Call
|
||||
|
||||
#### Purpose
|
||||
Expose DM sending as a tool so the agent can proactively message people (notify admin, reach out to contacts).
|
||||
|
||||
#### OpenAI Schema
|
||||
```json
|
||||
{
|
||||
"name": "nostr_dm_send",
|
||||
"description": "Send an encrypted direct message to a Nostr user (NIP-04)",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"recipient_pubkey": {
|
||||
"type": "string",
|
||||
"description": "64-char hex pubkey of the recipient"
|
||||
},
|
||||
"message": {
|
||||
"type": "string",
|
||||
"description": "Plaintext message to send"
|
||||
}
|
||||
},
|
||||
"required": ["recipient_pubkey", "message"]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Implementation: `execute_nostr_dm_send()`
|
||||
|
||||
```
|
||||
1. Parse args_json
|
||||
2. Extract "recipient_pubkey" (required, 64-char hex)
|
||||
3. Extract "message" (required, non-empty string)
|
||||
4. Call nostr_handler_send_dm(recipient_pubkey, message)
|
||||
5. Return { success: true/false, recipient_pubkey, message_length }
|
||||
```
|
||||
|
||||
#### Library API Used
|
||||
- [`nostr_handler_send_dm()`](../src/nostr_handler.h:33) — already exists
|
||||
|
||||
#### Security Note
|
||||
- Should be admin-tier only in practice. The tool itself doesn't enforce tier, but the agent's system prompt and skill definitions should restrict usage.
|
||||
|
||||
#### Files Modified
|
||||
- `src/tools.c`: Schema + executor + dispatch
|
||||
|
||||
---
|
||||
|
||||
### Tool 15: `nostr_relay_info` — Relay Information Document (NIP-11)
|
||||
|
||||
#### Purpose
|
||||
Inspect relay capabilities, supported NIPs, limitations, PoW requirements before publishing.
|
||||
|
||||
#### OpenAI Schema
|
||||
```json
|
||||
{
|
||||
"name": "nostr_relay_info",
|
||||
"description": "Fetch the NIP-11 relay information document for a relay URL",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"relay_url": {
|
||||
"type": "string",
|
||||
"description": "WebSocket relay URL, e.g. wss://relay.example.com"
|
||||
}
|
||||
},
|
||||
"required": ["relay_url"]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Implementation
|
||||
|
||||
This requires a new helper in [`nostr_handler.c`](../src/nostr_handler.c) since NIP-11 fetching needs an HTTP GET request with `Accept: application/nostr+json` header.
|
||||
|
||||
##### New function: `nostr_handler_relay_info_json()`
|
||||
|
||||
```
|
||||
1. Convert ws:// or wss:// URL to http:// or https://
|
||||
2. Use libcurl to HTTP GET with header "Accept: application/nostr+json"
|
||||
3. Return the raw JSON response string
|
||||
```
|
||||
|
||||
##### `execute_nostr_relay_info()` in tools.c:
|
||||
|
||||
```
|
||||
1. Parse args_json
|
||||
2. Extract "relay_url" (required string, must start with ws:// or wss://)
|
||||
3. Call nostr_handler_relay_info_json(relay_url)
|
||||
4. Parse returned JSON
|
||||
5. Return { success, relay_url, info: { name, description, pubkey, supported_nips, limitations, ... } }
|
||||
```
|
||||
|
||||
#### Files Modified
|
||||
- `src/nostr_handler.h`: Add `nostr_handler_relay_info_json()` declaration
|
||||
- `src/nostr_handler.c`: Implement using libcurl (already linked)
|
||||
- `src/tools.c`: Schema + executor + dispatch
|
||||
|
||||
---
|
||||
|
||||
## Tier 3 Tools — High Value, More Complex
|
||||
|
||||
### Tool 16: `nostr_dm_send_nip17` — Private DMs via Gift Wrap (NIP-17/NIP-59)
|
||||
|
||||
#### Purpose
|
||||
Send modern, metadata-private DMs using the NIP-17 gift wrap protocol instead of legacy NIP-04.
|
||||
|
||||
#### OpenAI Schema
|
||||
```json
|
||||
{
|
||||
"name": "nostr_dm_send_nip17",
|
||||
"description": "Send a private direct message using NIP-17 gift wrap protocol for metadata privacy",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"recipient_pubkey": {
|
||||
"type": "string",
|
||||
"description": "64-char hex pubkey of the recipient"
|
||||
},
|
||||
"message": {
|
||||
"type": "string",
|
||||
"description": "Plaintext message to send"
|
||||
},
|
||||
"subject": {
|
||||
"type": "string",
|
||||
"description": "Optional conversation subject"
|
||||
}
|
||||
},
|
||||
"required": ["recipient_pubkey", "message"]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Implementation
|
||||
|
||||
This is a multi-step crypto pipeline. The library handles the heavy lifting but we need to orchestrate:
|
||||
|
||||
##### New function: `nostr_handler_send_dm_nip17()` in nostr_handler.c
|
||||
|
||||
```
|
||||
1. Query recipient's kind 10050 relay list (DM relays)
|
||||
- Build filter: { kinds: [10050], authors: [recipient_pubkey], limit: 1 }
|
||||
- Call nostr_relay_pool_query_sync()
|
||||
- Extract relay URLs via nostr_nip17_extract_dm_relays()
|
||||
- Fall back to our own relays if no 10050 found
|
||||
2. Create kind 14 chat event:
|
||||
- nostr_nip17_create_chat_event(message, &recipient_pubkey, 1, subject, NULL, NULL, our_pubkey)
|
||||
3. Gift wrap and send:
|
||||
- nostr_nip17_send_dm(chat_event, &recipient_pubkey, 1, our_private_key, gift_wraps, max_wraps, 0)
|
||||
4. Publish each gift wrap to recipient's DM relays
|
||||
5. Return success/failure + event metadata
|
||||
```
|
||||
|
||||
##### `execute_nostr_dm_send_nip17()` in tools.c:
|
||||
|
||||
```
|
||||
1. Parse args_json
|
||||
2. Extract recipient_pubkey, message, optional subject
|
||||
3. Call nostr_handler_send_dm_nip17(recipient_pubkey, message, subject)
|
||||
4. Return { success, recipient_pubkey, protocol: "nip17" }
|
||||
```
|
||||
|
||||
#### Library API Used
|
||||
- [`nostr_nip17_create_chat_event()`](../nostr_core_lib/nostr_core/nip017.h:28)
|
||||
- [`nostr_nip17_send_dm()`](../nostr_core_lib/nostr_core/nip017.h:100)
|
||||
- [`nostr_nip17_extract_dm_relays()`](../nostr_core_lib/nostr_core/nip017.h)
|
||||
- [`nostr_nip59_create_gift_wrap()`](../nostr_core_lib/nostr_core/nip059.h:50)
|
||||
|
||||
#### Files Modified
|
||||
- `src/nostr_handler.h`: Add `nostr_handler_send_dm_nip17()` declaration
|
||||
- `src/nostr_handler.c`: Implement the full NIP-17 send pipeline
|
||||
- `src/tools.c`: Schema + executor + dispatch
|
||||
|
||||
---
|
||||
|
||||
### Tool 17: `nostr_encrypt` — NIP-44 Encryption
|
||||
|
||||
#### Purpose
|
||||
Encrypt arbitrary content for a specific recipient using modern NIP-44 encryption.
|
||||
|
||||
#### OpenAI Schema
|
||||
```json
|
||||
{
|
||||
"name": "nostr_encrypt",
|
||||
"description": "Encrypt plaintext for a recipient using NIP-44 ChaCha20+HMAC encryption",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"recipient_pubkey": {
|
||||
"type": "string",
|
||||
"description": "64-char hex pubkey of the recipient"
|
||||
},
|
||||
"plaintext": {
|
||||
"type": "string",
|
||||
"description": "Text to encrypt"
|
||||
}
|
||||
},
|
||||
"required": ["recipient_pubkey", "plaintext"]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Implementation: `execute_nostr_encrypt()`
|
||||
|
||||
```
|
||||
1. Parse args_json
|
||||
2. Extract recipient_pubkey (64-char hex), plaintext (non-empty string)
|
||||
3. Convert recipient_pubkey hex to 32-byte array
|
||||
4. Allocate output buffer (plaintext length * 2 + 256 for base64 overhead)
|
||||
5. Call nostr_nip44_encrypt(our_private_key, recipient_pubkey_bytes, plaintext, output, output_size)
|
||||
6. Return { success, recipient_pubkey, ciphertext: output }
|
||||
```
|
||||
|
||||
#### Library API Used
|
||||
- [`nostr_nip44_encrypt()`](../nostr_core_lib/nostr_core/nip044.h:28)
|
||||
|
||||
#### Security Note
|
||||
- Admin-tier only. Uses the agent's private key for ECDH shared secret.
|
||||
|
||||
#### Files Modified
|
||||
- `src/tools.c`: Schema + executor + dispatch
|
||||
|
||||
---
|
||||
|
||||
### Tool 18: `nostr_decrypt` — NIP-44 Decryption
|
||||
|
||||
#### Purpose
|
||||
Decrypt NIP-44 encrypted content from a known sender.
|
||||
|
||||
#### OpenAI Schema
|
||||
```json
|
||||
{
|
||||
"name": "nostr_decrypt",
|
||||
"description": "Decrypt NIP-44 encrypted content from a sender",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"sender_pubkey": {
|
||||
"type": "string",
|
||||
"description": "64-char hex pubkey of the sender"
|
||||
},
|
||||
"ciphertext": {
|
||||
"type": "string",
|
||||
"description": "Base64-encoded NIP-44 encrypted payload"
|
||||
}
|
||||
},
|
||||
"required": ["sender_pubkey", "ciphertext"]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Implementation: `execute_nostr_decrypt()`
|
||||
|
||||
```
|
||||
1. Parse args_json
|
||||
2. Extract sender_pubkey (64-char hex), ciphertext (non-empty string)
|
||||
3. Convert sender_pubkey hex to 32-byte array
|
||||
4. Allocate output buffer
|
||||
5. Call nostr_nip44_decrypt(our_private_key, sender_pubkey_bytes, ciphertext, output, output_size)
|
||||
6. Return { success, sender_pubkey, plaintext: output }
|
||||
```
|
||||
|
||||
#### Library API Used
|
||||
- [`nostr_nip44_decrypt()`](../nostr_core_lib/nostr_core/nip044.h:62)
|
||||
|
||||
#### Security Note
|
||||
- Admin-tier only.
|
||||
|
||||
#### Files Modified
|
||||
- `src/tools.c`: Schema + executor + dispatch
|
||||
|
||||
---
|
||||
|
||||
### Tool 19: `nostr_list_manage` — List Management (NIP-51)
|
||||
|
||||
#### Purpose
|
||||
Manage mute lists (kind 10000), bookmarks (kind 10003), follow sets, relay sets. Lets the agent curate its social graph.
|
||||
|
||||
#### OpenAI Schema
|
||||
```json
|
||||
{
|
||||
"name": "nostr_list_manage",
|
||||
"description": "Add or remove items from a Nostr list: mute list, bookmarks, relay sets, etc. (NIP-51)",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"list_kind": {
|
||||
"type": "integer",
|
||||
"description": "Kind number of the list: 10000=mute, 10001=pins, 10003=bookmarks, 3=follows, 10002=relays"
|
||||
},
|
||||
"action": {
|
||||
"type": "string",
|
||||
"description": "Action: add or remove"
|
||||
},
|
||||
"items": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "array",
|
||||
"items": { "type": "string" }
|
||||
},
|
||||
"description": "Array of tag tuples to add/remove, e.g. [[p, pubkey], [e, event_id]]"
|
||||
}
|
||||
},
|
||||
"required": ["list_kind", "action", "items"]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Implementation
|
||||
|
||||
This is a read-modify-write pattern for replaceable events:
|
||||
|
||||
##### `execute_nostr_list_manage()`:
|
||||
|
||||
```
|
||||
1. Parse args_json
|
||||
2. Extract list_kind (integer), action ("add"|"remove"), items (array of tag arrays)
|
||||
3. Query existing list:
|
||||
- Build filter: { kinds: [list_kind], authors: [our_pubkey], limit: 1 }
|
||||
- Call nostr_handler_query_json(filter, 8000)
|
||||
4. Parse existing event tags (or start with empty array if none found)
|
||||
5. If action == "add":
|
||||
- For each item in items: append to tags if not already present
|
||||
6. If action == "remove":
|
||||
- For each item in items: remove matching tag from tags
|
||||
7. Publish updated event:
|
||||
- Call nostr_handler_publish_kind_event(list_kind, existing_content_or_empty, updated_tags, &result)
|
||||
8. Return { success, list_kind, action, items_affected, event_id }
|
||||
```
|
||||
|
||||
#### Notes
|
||||
- Replaceable events (kind 10000-19999) automatically replace previous versions
|
||||
- Kind 3 (follows) is also replaceable
|
||||
- Content field may contain NIP-44 encrypted private items — preserve it unchanged
|
||||
- Must deduplicate tags when adding
|
||||
|
||||
#### Files Modified
|
||||
- `src/tools.c`: Schema + executor + dispatch
|
||||
|
||||
---
|
||||
|
||||
## Implementation Order
|
||||
|
||||
```mermaid
|
||||
graph TD
|
||||
A[nostr_nip05_lookup] --> B[nostr_encode]
|
||||
B --> C[nostr_decode]
|
||||
C --> D[nostr_dm_send]
|
||||
D --> E[nostr_relay_info]
|
||||
E --> F[nostr_encrypt]
|
||||
F --> G[nostr_decrypt]
|
||||
G --> H[nostr_dm_send_nip17]
|
||||
H --> I[nostr_list_manage]
|
||||
|
||||
style A fill:#FFFFE0
|
||||
style B fill:#FFFFE0
|
||||
style C fill:#FFFFE0
|
||||
style D fill:#FFFFE0
|
||||
style E fill:#FFFFE0
|
||||
style F fill:#FFD700
|
||||
style G fill:#FFD700
|
||||
style H fill:#FFD700
|
||||
style I fill:#FFD700
|
||||
```
|
||||
|
||||
Yellow = Tier 2 (moderate), Gold = Tier 3 (complex).
|
||||
|
||||
### Recommended batching:
|
||||
|
||||
**Batch A — Pure computation, no new handler functions:**
|
||||
1. `nostr_encode` — pure NIP-21 encoding
|
||||
2. `nostr_decode` — pure NIP-21 decoding
|
||||
3. `nostr_dm_send` — thin wrapper around existing `nostr_handler_send_dm()`
|
||||
|
||||
**Batch B — Network I/O tools:**
|
||||
4. `nostr_nip05_lookup` — HTTP fetch via library
|
||||
5. `nostr_relay_info` — HTTP fetch via new handler function + libcurl
|
||||
|
||||
**Batch C — Crypto tools:**
|
||||
6. `nostr_encrypt` — NIP-44 encrypt
|
||||
7. `nostr_decrypt` — NIP-44 decrypt
|
||||
|
||||
**Batch D — Complex orchestration:**
|
||||
8. `nostr_dm_send_nip17` — multi-step NIP-17/59 pipeline, new handler function
|
||||
9. `nostr_list_manage` — read-modify-write replaceable events
|
||||
|
||||
## Build & Validate
|
||||
|
||||
After each batch:
|
||||
1. Run `./build_static.sh` to verify compilation
|
||||
2. Run `./increment_and_push.sh` with descriptive message
|
||||
194
plans/nip17_messaging.md
Normal file
194
plans/nip17_messaging.md
Normal file
@@ -0,0 +1,194 @@
|
||||
# NIP-17 Messaging Implementation Plan
|
||||
|
||||
## Background
|
||||
|
||||
Didactyl currently uses NIP-04 (kind 4) for all DM communication. NIP-17 send support exists via `nostr_handler_send_dm_nip17()` and the `nostr_dm_send_nip17` tool, but there is **no ability to receive NIP-17 messages** and **all agent responses always use NIP-04**.
|
||||
|
||||
The `nostr_core_lib` already has full NIP-17/NIP-59 support including:
|
||||
- `nostr_nip17_create_chat_event()` — create kind 14 rumor
|
||||
- `nostr_nip17_send_dm()` — seal + gift wrap (creates wraps for both recipient AND sender)
|
||||
- `nostr_nip17_receive_dm()` — unwrap gift wrap, unseal rumor, return kind 14
|
||||
- `nostr_nip17_extract_dm_relays()` — parse kind 10050 relay lists
|
||||
|
||||
## Config-Driven Protocol Selection
|
||||
|
||||
### New Config Field
|
||||
|
||||
Add a `dm_protocol` field to the top-level config:
|
||||
|
||||
```json
|
||||
{
|
||||
"dm_protocol": "nip04",
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
Valid values:
|
||||
- `"nip04"` — NIP-04 only (current behavior, default for backward compatibility)
|
||||
- `"nip17"` — NIP-17 only (subscribe to kind 1059, send via gift wrap)
|
||||
- `"both"` — Subscribe to both kind 4 and kind 1059; reply using whichever protocol the message arrived on
|
||||
|
||||
### Config Struct Change
|
||||
|
||||
In `config.h`, add to `didactyl_config_t`:
|
||||
|
||||
```c
|
||||
typedef enum {
|
||||
DM_PROTOCOL_NIP04 = 0,
|
||||
DM_PROTOCOL_NIP17 = 1,
|
||||
DM_PROTOCOL_BOTH = 2
|
||||
} dm_protocol_t;
|
||||
```
|
||||
|
||||
Add `dm_protocol_t dm_protocol;` to the config struct.
|
||||
|
||||
## Implementation Steps
|
||||
|
||||
### 1. Add `dm_protocol` config parsing
|
||||
|
||||
**Files**: `config.h`, `config.c`
|
||||
|
||||
- Add `dm_protocol_t` enum and field to `didactyl_config_t`
|
||||
- Parse `"dm_protocol"` string from JSON in `config_load()`
|
||||
- Default to `DM_PROTOCOL_NIP04` if not specified
|
||||
|
||||
### 2. Add kind 1059 subscription
|
||||
|
||||
**File**: `nostr_handler.c` — `nostr_handler_subscribe_dms()`
|
||||
|
||||
- When `dm_protocol` is `NIP17` or `BOTH`, add `kind: 1059` to the subscription filter
|
||||
- When `dm_protocol` is `NIP04` or `BOTH`, keep `kind: 4` in the filter
|
||||
- The subscription filter becomes `kinds: [4, 1059]` for `BOTH` mode
|
||||
|
||||
### 3. Add NIP-17 receive handling in `on_event()`
|
||||
|
||||
**File**: `nostr_handler.c` — `on_event()`
|
||||
|
||||
Currently `on_event()` rejects anything that is not kind 4. Update to:
|
||||
|
||||
- If kind == 1059: unwrap gift wrap via `nostr_nip17_receive_dm()`, extract sender pubkey from the kind 14 rumor, extract message content, determine sender tier, fire `g_dm_callback`
|
||||
- If kind == 4: existing NIP-04 decrypt path (unchanged)
|
||||
- Track which protocol was used per sender pubkey for reply routing
|
||||
|
||||
### 4. Track protocol per sender for reply routing
|
||||
|
||||
**File**: `nostr_handler.c`
|
||||
|
||||
Add a small cache that maps `sender_pubkey_hex -> last_protocol_used`:
|
||||
|
||||
```c
|
||||
typedef struct {
|
||||
char pubkey_hex[65];
|
||||
dm_protocol_t protocol;
|
||||
} sender_protocol_entry_t;
|
||||
|
||||
#define SENDER_PROTOCOL_CACHE_SIZE 64
|
||||
static sender_protocol_entry_t g_sender_protocol_cache[SENDER_PROTOCOL_CACHE_SIZE];
|
||||
```
|
||||
|
||||
When a DM arrives via kind 4, record `NIP04` for that sender. When via kind 1059, record `NIP17`.
|
||||
|
||||
### 5. Add protocol-aware send function
|
||||
|
||||
**File**: `nostr_handler.c`
|
||||
|
||||
Add a new function that routes based on config + sender history:
|
||||
|
||||
```c
|
||||
int nostr_handler_send_dm_auto(const char* recipient_pubkey_hex, const char* message);
|
||||
```
|
||||
|
||||
Logic:
|
||||
- If `dm_protocol == NIP04`: always use `nostr_handler_send_dm()`
|
||||
- If `dm_protocol == NIP17`: always use `nostr_handler_send_dm_nip17()`
|
||||
- If `dm_protocol == BOTH`: check sender protocol cache; use matching protocol, default to NIP-04
|
||||
|
||||
Expose in `nostr_handler.h`.
|
||||
|
||||
### 6. Update agent.c to use auto-routing
|
||||
|
||||
**File**: `agent.c`
|
||||
|
||||
Replace all `nostr_handler_send_dm()` calls with `nostr_handler_send_dm_auto()`. This is a mechanical find-and-replace across ~15 call sites.
|
||||
|
||||
### 7. Add kind 10050 startup event support
|
||||
|
||||
**File**: `config.json.example`
|
||||
|
||||
Add a kind 10050 startup event example so NIP-17 clients can discover the agent's DM relay preferences:
|
||||
|
||||
```json
|
||||
{
|
||||
"kind": 10050,
|
||||
"content": "",
|
||||
"tags": [
|
||||
["relay", "wss://relay.damus.io"],
|
||||
["relay", "wss://nos.lol"]
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
**File**: `config.c` — startup event parsing already handles arbitrary kinds, so this should work with no code changes.
|
||||
|
||||
### 8. Update startup DM to use auto-routing
|
||||
|
||||
**File**: `main.c`
|
||||
|
||||
The startup status DM at line 255 currently calls `nostr_handler_send_dm()`. Update to `nostr_handler_send_dm_auto()`.
|
||||
|
||||
## Architecture Diagram
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
subgraph Config
|
||||
CFG[dm_protocol setting]
|
||||
CFG -->|nip04| NIP04_MODE[NIP-04 only]
|
||||
CFG -->|nip17| NIP17_MODE[NIP-17 only]
|
||||
CFG -->|both| BOTH_MODE[Both protocols]
|
||||
end
|
||||
|
||||
subgraph Subscription
|
||||
SUB[nostr_handler_subscribe_dms]
|
||||
NIP04_MODE --> SUB_K4[Subscribe kind 4]
|
||||
NIP17_MODE --> SUB_K1059[Subscribe kind 1059]
|
||||
BOTH_MODE --> SUB_BOTH[Subscribe kind 4 + 1059]
|
||||
end
|
||||
|
||||
subgraph Receive Path
|
||||
EVT[on_event callback]
|
||||
EVT -->|kind 4| DEC4[NIP-04 decrypt]
|
||||
EVT -->|kind 1059| DEC17[NIP-17 unwrap + unseal]
|
||||
DEC4 --> CACHE[Record sender protocol]
|
||||
DEC17 --> CACHE
|
||||
CACHE --> CB[Fire dm_callback]
|
||||
end
|
||||
|
||||
subgraph Send Path
|
||||
SEND[nostr_handler_send_dm_auto]
|
||||
SEND -->|config=nip04| S4[nostr_handler_send_dm - kind 4]
|
||||
SEND -->|config=nip17| S17[nostr_handler_send_dm_nip17 - kind 1059]
|
||||
SEND -->|config=both| LOOKUP[Check sender protocol cache]
|
||||
LOOKUP -->|sender used nip04| S4
|
||||
LOOKUP -->|sender used nip17| S17
|
||||
LOOKUP -->|unknown| S4
|
||||
end
|
||||
```
|
||||
|
||||
## File Change Summary
|
||||
|
||||
| File | Changes |
|
||||
|------|---------|
|
||||
| `config.h` | Add `dm_protocol_t` enum and field |
|
||||
| `config.c` | Parse `dm_protocol` from JSON |
|
||||
| `nostr_handler.h` | Add `nostr_handler_send_dm_auto()` declaration |
|
||||
| `nostr_handler.c` | Add kind 1059 subscription, NIP-17 receive in `on_event()`, sender protocol cache, `nostr_handler_send_dm_auto()` |
|
||||
| `agent.c` | Replace `nostr_handler_send_dm()` with `nostr_handler_send_dm_auto()` |
|
||||
| `main.c` | Replace startup DM send with `nostr_handler_send_dm_auto()` |
|
||||
| `config.json.example` | Add `dm_protocol` field and kind 10050 startup event example |
|
||||
|
||||
## Testing Strategy
|
||||
|
||||
1. **NIP-04 mode** (default): Verify existing behavior is unchanged
|
||||
2. **NIP-17 mode**: Send a NIP-17 DM from a client like Amethyst/0xchat, verify Didactyl receives and replies via NIP-17
|
||||
3. **Both mode**: Send NIP-04 DM, verify NIP-04 reply; send NIP-17 DM, verify NIP-17 reply
|
||||
4. **Kind 10050**: Verify the relay list event is published on startup and discoverable by NIP-17 clients
|
||||
398
plans/prompt_templates.md
Normal file
398
plans/prompt_templates.md
Normal file
@@ -0,0 +1,398 @@
|
||||
# Didactyl Prompt Template System — Design Plan
|
||||
|
||||
## Summary
|
||||
|
||||
Replace the hardcoded context assembly in `src/agent.c` with a template-driven system. The template lives inside the soul event (kind 31120) — because the template defines how the agent perceives the world, and that is fundamentally part of who the agent is.
|
||||
|
||||
Different agents (architect, eGirl, analyst) are different processes with different souls, different templates, different Nostr identities. Multi-agent is Approach A: multiple `./didactyl --config` processes communicating via Nostr.
|
||||
|
||||
---
|
||||
|
||||
## Core Principle
|
||||
|
||||
**The soul IS the template.** An agent's soul defines both its personality (prose instructions) and its perception (what context sections it sees, in what order, with what limits). You cannot meaningfully separate "who you are" from "how you see the world."
|
||||
|
||||
---
|
||||
|
||||
## Current State
|
||||
|
||||
Today, context assembly is hardcoded in `agent_build_admin_messages_json()`:
|
||||
|
||||
```
|
||||
1. System prompt (soul content)
|
||||
2. Admin identity (pubkey)
|
||||
3. Admin kind 0 profile
|
||||
4. Admin kind 10002 relay list
|
||||
5. Startup events memory
|
||||
6. Adopted skills memory
|
||||
7. DM history (last 12 turns)
|
||||
8. Admin recent notes (kind 1)
|
||||
```
|
||||
|
||||
The order, formatting, limits, and section headers are all baked into C code. Changing anything requires editing `src/agent.c` and recompiling.
|
||||
|
||||
---
|
||||
|
||||
## Proposed Design
|
||||
|
||||
### Soul Event Structure
|
||||
|
||||
The kind 31120 soul event content gains a template section, delimited by a marker:
|
||||
|
||||
```markdown
|
||||
# Didactyl Agent
|
||||
|
||||
You are Didactyl, a sovereign AI agent living on Nostr.
|
||||
|
||||
## Communication Rules
|
||||
- You communicate through encrypted Nostr direct messages.
|
||||
- Keep responses concise and clear.
|
||||
|
||||
## Behavior
|
||||
- Be helpful and technically accurate.
|
||||
...
|
||||
|
||||
## Safety
|
||||
- Never reveal your private key.
|
||||
...
|
||||
|
||||
---template---
|
||||
|
||||
- section: admin_identity
|
||||
role: system
|
||||
content: |
|
||||
This is your administrator! Admin pubkey: {{admin_pubkey}}
|
||||
|
||||
- section: admin_profile
|
||||
role: system
|
||||
content: |
|
||||
Administrator profile: {{admin_kind0_json}}
|
||||
|
||||
- section: admin_relay_list
|
||||
role: system
|
||||
content: |
|
||||
Administrator relay list: {{admin_kind10002_json}}
|
||||
|
||||
- section: startup_events
|
||||
role: system
|
||||
content: |
|
||||
Startup events memory: {{startup_events_json}}
|
||||
|
||||
- section: adopted_skills
|
||||
role: system
|
||||
content: |
|
||||
{{adopted_skills_content}}
|
||||
|
||||
- section: dm_history
|
||||
role: expand
|
||||
limit: 12
|
||||
|
||||
- section: admin_notes
|
||||
role: system
|
||||
limit: 10
|
||||
content: |
|
||||
{{admin_notes_content}}
|
||||
```
|
||||
|
||||
Everything above `---template---` is the system prompt (personality/rules). Everything below defines the context assembly template.
|
||||
|
||||
If no `---template---` marker is found, the agent falls back to the current hardcoded assembly — backward compatible.
|
||||
|
||||
### Template Syntax
|
||||
|
||||
Simple YAML-like format parsed in C. Each section has:
|
||||
|
||||
| Field | Required | Description |
|
||||
|---|---|---|
|
||||
| `section` | yes | Section name for logging and API identification |
|
||||
| `role` | yes | Chat message role: `system`, `user`, `assistant`, or `expand` |
|
||||
| `content` | no | Content template with `{{variable}}` placeholders |
|
||||
| `limit` | no | Integer limit for variable-length sections like DM history or notes |
|
||||
| `provider` | no | Provider-specific override — see below |
|
||||
|
||||
### Variable Placeholders
|
||||
|
||||
| Variable | Source | Description |
|
||||
|---|---|---|
|
||||
| `{{admin_pubkey}}` | `config.admin.pubkey` | Admin hex pubkey |
|
||||
| `{{admin_kind0_json}}` | `nostr_handler_get_admin_kind0_context()` | Admin profile JSON |
|
||||
| `{{admin_kind10002_json}}` | `nostr_handler_get_admin_kind10002_context()` | Admin relay list JSON |
|
||||
| `{{startup_events_json}}` | Serialized startup events | Startup events array |
|
||||
| `{{adopted_skills_content}}` | Adoption list cache | Formatted skill instructions |
|
||||
| `{{admin_notes_content}}` | `nostr_handler_get_admin_kind1_notes_context()` | Recent kind 1 notes |
|
||||
| `{{agent_pubkey}}` | `config.keys.public_key_hex` | Agent own pubkey |
|
||||
| `{{agent_npub}}` | Derived from pubkey | Agent npub |
|
||||
|
||||
### Special Section Types
|
||||
|
||||
**`role: expand`** — The `dm_history` section expands into multiple messages (user/assistant pairs). The `limit` field controls how many turns to include. This is the only section type that produces multiple chat messages from one template entry.
|
||||
|
||||
### Provider-Specific Overrides
|
||||
|
||||
Within a section, you can specify provider-specific formatting:
|
||||
|
||||
```yaml
|
||||
- section: admin_identity
|
||||
role: system
|
||||
content: |
|
||||
## Administrator Identity
|
||||
This is your administrator! Admin pubkey: {{admin_pubkey}}
|
||||
provider:
|
||||
anthropic: |
|
||||
<admin_identity>
|
||||
This is your administrator! Admin pubkey: {{admin_pubkey}}
|
||||
</admin_identity>
|
||||
```
|
||||
|
||||
When the configured `llm.provider` matches a provider key, that override is used instead of the default `content`. This lets one soul/template work well across providers without needing separate soul events.
|
||||
|
||||
If no provider override matches, the default `content` is used.
|
||||
|
||||
---
|
||||
|
||||
## Context Assembly Flow
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
BOOT[Agent boots] --> LOAD_SOUL[Load soul event - kind 31120]
|
||||
LOAD_SOUL --> PARSE[Parse soul content]
|
||||
PARSE --> SPLIT{Contains ---template--- marker?}
|
||||
SPLIT -->|Yes| EXTRACT[Extract personality above marker]
|
||||
SPLIT -->|No| FALLBACK[Use hardcoded assembly - backward compat]
|
||||
EXTRACT --> PARSE_TPL[Parse template sections below marker]
|
||||
PARSE_TPL --> STORE[Store template_section_t array in memory]
|
||||
|
||||
DM[Incoming DM] --> BUILD[Build context from template]
|
||||
BUILD --> EMIT_SOUL[Emit personality as first system message]
|
||||
EMIT_SOUL --> FOREACH[For each template section]
|
||||
FOREACH --> RESOLVE[Resolve {{variables}} from live data]
|
||||
RESOLVE --> CHECK_PROVIDER{Provider override?}
|
||||
CHECK_PROVIDER -->|Yes| USE_OVERRIDE[Use provider-specific content]
|
||||
CHECK_PROVIDER -->|No| USE_DEFAULT[Use default content]
|
||||
USE_OVERRIDE --> EMIT[Emit as chat message]
|
||||
USE_DEFAULT --> EMIT
|
||||
EMIT --> FOREACH
|
||||
FOREACH --> DONE[Complete messages array]
|
||||
DONE --> LLM[Send to LLM]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Context.log Formatting
|
||||
|
||||
With templates, the log formatter uses section names directly from the template instead of detecting them from content prefixes. The `detect_context_section()` function is replaced by the template section name.
|
||||
|
||||
Log format becomes:
|
||||
|
||||
```
|
||||
[2026-03-02 14:54:30] phase=llm_chat_with_tools_messages sender=8ff747...
|
||||
|
||||
Sections: 8
|
||||
|
||||
============================================================
|
||||
Section: system_prompt | role=system
|
||||
============================================================
|
||||
|
||||
# Didactyl Agent
|
||||
...
|
||||
|
||||
|
||||
============================================================
|
||||
Section: admin_identity | role=system
|
||||
============================================================
|
||||
|
||||
This is your administrator! Admin pubkey: 8ff747...
|
||||
|
||||
|
||||
============================================================
|
||||
Section: dm_history | role=user
|
||||
============================================================
|
||||
|
||||
Good afternoon.
|
||||
```
|
||||
|
||||
Note: "Message 01" is replaced with "Section: admin_identity" — the section name from the template, which is much more meaningful.
|
||||
|
||||
---
|
||||
|
||||
## Data Structures
|
||||
|
||||
```c
|
||||
typedef struct {
|
||||
char name[64]; // section name
|
||||
char role[16]; // system, user, assistant, expand
|
||||
char* content_template; // content with {{var}} placeholders
|
||||
int limit; // for expand sections, 0 = unlimited
|
||||
char* provider_overrides; // JSON object of provider->content pairs, or NULL
|
||||
} template_section_t;
|
||||
|
||||
typedef struct {
|
||||
char* personality; // everything above ---template---
|
||||
template_section_t* sections; // parsed template sections
|
||||
int section_count;
|
||||
} prompt_template_t;
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Implementation Plan
|
||||
|
||||
### New Files
|
||||
|
||||
| File | Purpose |
|
||||
|---|---|
|
||||
| `src/prompt_template.c` | Template parser, variable resolver, context builder |
|
||||
| `src/prompt_template.h` | Public API: parse, build context, free |
|
||||
|
||||
### Modified Files
|
||||
|
||||
| File | Change |
|
||||
|---|---|
|
||||
| `src/agent.c` | Replace `agent_build_admin_messages_json()` internals with template-driven builder. Keep function signature unchanged for API compatibility. |
|
||||
| `src/agent.c` | Remove hardcoded `append_admin_identity_context()`, `append_startup_events_context()`, etc. — these become template variable resolvers |
|
||||
| `src/agent.c` | Update `format_context_payload_for_log()` to use section names from template |
|
||||
| `src/http_api.c` | Remove `classify_part_name()` / `detect_context_section()` — section names come from template |
|
||||
| `Makefile` | Add `src/prompt_template.c` to SRCS |
|
||||
| `Dockerfile.alpine-musl` | Add `src/prompt_template.c` to gcc command |
|
||||
|
||||
### Implementation Order
|
||||
|
||||
1. Create `src/prompt_template.h` with data structures and API
|
||||
2. Implement template parser in `src/prompt_template.c` — parse soul content, split at `---template---`, parse sections
|
||||
3. Implement variable resolver — map `{{var}}` names to data source functions
|
||||
4. Implement context builder — iterate sections, resolve variables, emit messages
|
||||
5. Wire into `agent_build_admin_messages_json()` — if template exists, use it; otherwise fall back to hardcoded
|
||||
6. Update `format_context_payload_for_log()` to use section names
|
||||
7. Update `classify_part_name()` in `http_api.c` to use section names from template
|
||||
8. Update default soul in `config.json.example` to include a `---template---` section
|
||||
9. Test with existing soul (no template marker) — verify backward compatibility
|
||||
10. Test with template soul — verify new assembly
|
||||
11. Test provider overrides
|
||||
|
||||
---
|
||||
|
||||
## Backward Compatibility
|
||||
|
||||
If the soul event content does NOT contain `---template---`, the agent uses the current hardcoded assembly. This means:
|
||||
|
||||
- Existing agents continue to work without changes
|
||||
- The template system is opt-in
|
||||
- Migration is gradual — add a template section to your soul when ready
|
||||
|
||||
---
|
||||
|
||||
## Example Souls
|
||||
|
||||
### Architect Agent
|
||||
|
||||
```markdown
|
||||
# Technical Architect
|
||||
|
||||
You are a technical architect agent. You analyze systems, design solutions, and produce detailed technical plans.
|
||||
|
||||
## Behavior
|
||||
- Think systematically about architecture
|
||||
- Consider tradeoffs explicitly
|
||||
- Produce diagrams when helpful
|
||||
- Never implement code — only design
|
||||
|
||||
---template---
|
||||
|
||||
- section: admin_identity
|
||||
role: system
|
||||
content: |
|
||||
Administrator pubkey: {{admin_pubkey}}
|
||||
|
||||
- section: admin_profile
|
||||
role: system
|
||||
content: |
|
||||
Administrator profile: {{admin_kind0_json}}
|
||||
|
||||
- section: startup_events
|
||||
role: system
|
||||
content: |
|
||||
System configuration and startup state: {{startup_events_json}}
|
||||
|
||||
- section: adopted_skills
|
||||
role: system
|
||||
content: |
|
||||
{{adopted_skills_content}}
|
||||
|
||||
- section: dm_history
|
||||
role: expand
|
||||
limit: 20
|
||||
|
||||
- section: admin_notes
|
||||
role: system
|
||||
limit: 5
|
||||
content: |
|
||||
Recent administrator notes for context: {{admin_notes_content}}
|
||||
```
|
||||
|
||||
### eGirl Agent
|
||||
|
||||
```markdown
|
||||
# Social Butterfly
|
||||
|
||||
You are a friendly, social Nostr personality. You love interacting with people, commenting on their posts, and being part of the community.
|
||||
|
||||
## Personality
|
||||
- Warm, enthusiastic, uses emoji freely
|
||||
- Interested in what people are posting about
|
||||
- Remembers details about conversations
|
||||
- Keeps responses casual and fun
|
||||
|
||||
---template---
|
||||
|
||||
- section: admin_identity
|
||||
role: system
|
||||
content: |
|
||||
Your creator: {{admin_pubkey}}
|
||||
|
||||
- section: admin_profile
|
||||
role: system
|
||||
content: |
|
||||
Creator profile: {{admin_kind0_json}}
|
||||
|
||||
- section: admin_notes
|
||||
role: system
|
||||
limit: 20
|
||||
content: |
|
||||
Recent posts from people you follow — use these for social context and conversation starters: {{admin_notes_content}}
|
||||
|
||||
- section: adopted_skills
|
||||
role: system
|
||||
content: |
|
||||
{{adopted_skills_content}}
|
||||
|
||||
- section: dm_history
|
||||
role: expand
|
||||
limit: 8
|
||||
```
|
||||
|
||||
Note the differences:
|
||||
- The eGirl sees 20 recent notes (social context) but only 8 DM turns
|
||||
- The architect sees 5 notes but 20 DM turns (needs conversation continuity)
|
||||
- The eGirl puts notes BEFORE skills (social context is primary)
|
||||
- The architect puts skills BEFORE notes (technical knowledge is primary)
|
||||
- No startup events for the eGirl (doesn't need system config details)
|
||||
|
||||
---
|
||||
|
||||
## Nostr Shareability
|
||||
|
||||
Since the template is part of the soul event (kind 31120), sharing works naturally:
|
||||
|
||||
- Publish your soul → others get your complete agent personality + perception template
|
||||
- Discover interesting agents on Nostr → adopt their soul as a starting point
|
||||
- Community can develop and share optimized templates for different use cases
|
||||
- Templates evolve through the same Nostr discovery mechanisms as skills
|
||||
|
||||
---
|
||||
|
||||
## Security Notes
|
||||
|
||||
- Template variable resolution is sandboxed — only predefined variables are resolved
|
||||
- No arbitrary code execution from templates
|
||||
- The `limit` field is capped at compile-time maximums to prevent resource exhaustion
|
||||
- Provider overrides are optional and safe — they only change formatting, not data sources
|
||||
395
plans/prompt_templates_coding.md
Normal file
395
plans/prompt_templates_coding.md
Normal file
@@ -0,0 +1,395 @@
|
||||
# Prompt Template System — Coding Plan
|
||||
|
||||
Design doc: `plans/prompt_templates.md`
|
||||
|
||||
## Overview
|
||||
|
||||
Replace the hardcoded context assembly in `src/agent.c` with a template-driven system. The template lives inside the soul event content (kind 31120), delimited by `---template---`. If no template marker is found, fall back to the current hardcoded assembly for backward compatibility.
|
||||
|
||||
---
|
||||
|
||||
## Phase 1: New Files — Template Parser & Builder
|
||||
|
||||
### Step 1.1: Create `src/prompt_template.h`
|
||||
|
||||
Header with data structures and public API.
|
||||
|
||||
```c
|
||||
#ifndef DIDACTYL_PROMPT_TEMPLATE_H
|
||||
#define DIDACTYL_PROMPT_TEMPLATE_H
|
||||
|
||||
#include "cjson/cJSON.h"
|
||||
#include "config.h"
|
||||
|
||||
#define PROMPT_TEMPLATE_MAX_SECTIONS 32
|
||||
#define PROMPT_TEMPLATE_MAX_NAME_LEN 64
|
||||
#define PROMPT_TEMPLATE_MAX_ROLE_LEN 16
|
||||
#define PROMPT_TEMPLATE_MARKER "---template---"
|
||||
|
||||
typedef struct {
|
||||
char name[PROMPT_TEMPLATE_MAX_NAME_LEN];
|
||||
char role[PROMPT_TEMPLATE_MAX_ROLE_LEN]; // system, user, assistant, expand
|
||||
char* content_template; // content with {{var}} placeholders, or NULL
|
||||
int limit; // for expand sections, 0 = default
|
||||
} prompt_template_section_t;
|
||||
|
||||
typedef struct {
|
||||
char* personality; // everything above ---template---
|
||||
prompt_template_section_t sections[PROMPT_TEMPLATE_MAX_SECTIONS];
|
||||
int section_count;
|
||||
} prompt_template_t;
|
||||
|
||||
// Variable resolver callback: given a variable name, return a malloc'd string or NULL.
|
||||
// The caller frees the returned string.
|
||||
typedef char* (*prompt_var_resolver_fn)(const char* var_name, void* user_data);
|
||||
|
||||
// Parse soul content into a template. Returns 0 on success, -1 if no template found.
|
||||
// On success, caller must call prompt_template_free() when done.
|
||||
// On -1 (no template), out_template is zeroed — caller should use hardcoded fallback.
|
||||
int prompt_template_parse(const char* soul_content, prompt_template_t* out_template);
|
||||
|
||||
// Build a cJSON messages array from a parsed template.
|
||||
// resolver_fn is called for each {{variable}} encountered.
|
||||
// dm_history_messages is a cJSON array of user/assistant messages for "expand" sections.
|
||||
// Returns a new cJSON array (caller owns it), or NULL on error.
|
||||
cJSON* prompt_template_build_messages(
|
||||
const prompt_template_t* tmpl,
|
||||
prompt_var_resolver_fn resolver_fn,
|
||||
void* resolver_user_data,
|
||||
cJSON* dm_history_messages
|
||||
);
|
||||
|
||||
// Free internals of a parsed template (does not free the struct itself).
|
||||
void prompt_template_free(prompt_template_t* tmpl);
|
||||
|
||||
// Get the section name for a message index (for logging/API).
|
||||
// Returns the section name string or NULL if idx is out of range.
|
||||
const char* prompt_template_section_name_at(const prompt_template_t* tmpl, int section_idx);
|
||||
|
||||
#endif
|
||||
```
|
||||
|
||||
### Step 1.2: Create `src/prompt_template.c`
|
||||
|
||||
Implementation file with three main components:
|
||||
|
||||
#### 1.2a: Template Parser — `prompt_template_parse()`
|
||||
|
||||
Logic:
|
||||
1. Search `soul_content` for the string `"\n---template---\n"` (with newlines on both sides, or at start/end of string).
|
||||
2. If not found, return -1 (no template).
|
||||
3. Split: everything before the marker → `tmpl->personality` (strdup'd).
|
||||
4. Everything after the marker → parse as template sections.
|
||||
5. Template section format is line-oriented YAML-like:
|
||||
|
||||
```
|
||||
- section: admin_identity
|
||||
role: system
|
||||
limit: 0
|
||||
content: |
|
||||
This is your administrator! Admin pubkey: {{admin_pubkey}}
|
||||
```
|
||||
|
||||
Parsing rules:
|
||||
- Lines starting with `- section:` begin a new section.
|
||||
- `role:` sets the role (default: `system`).
|
||||
- `limit:` sets the limit integer (default: 0).
|
||||
- `content: |` starts a multi-line content block. All subsequent lines indented by 4+ spaces (or until the next `- section:` line) are the content template.
|
||||
- `{{variable_name}}` placeholders in content are left as-is during parsing; they are resolved at build time.
|
||||
|
||||
Edge cases:
|
||||
- Trim leading/trailing whitespace from section names and roles.
|
||||
- If `content:` is a single line (not `|`), treat the rest of the line as the content.
|
||||
- Cap at `PROMPT_TEMPLATE_MAX_SECTIONS`.
|
||||
|
||||
#### 1.2b: Variable Resolver — `prompt_template_build_messages()`
|
||||
|
||||
Logic:
|
||||
1. Create a new cJSON array.
|
||||
2. First, append the personality as a system message (role=system, content=personality).
|
||||
3. For each section in order:
|
||||
- If `role` is `"expand"`: insert the `dm_history_messages` array items here, limited by `section.limit` (take last N if limit > 0).
|
||||
- Otherwise: resolve `{{var}}` placeholders in `content_template` by calling `resolver_fn(var_name, user_data)`. Build the resolved string. Append as a message with the configured role.
|
||||
4. Return the array.
|
||||
|
||||
Variable resolution:
|
||||
- Scan content_template for `{{` ... `}}` pairs.
|
||||
- Extract the variable name (trimmed).
|
||||
- Call `resolver_fn(name, user_data)`.
|
||||
- If resolver returns NULL, substitute empty string.
|
||||
- If resolver returns a string, substitute it and free the returned string.
|
||||
- Build the final resolved content by concatenating literal segments and resolved values.
|
||||
|
||||
#### 1.2c: Cleanup — `prompt_template_free()`
|
||||
|
||||
- Free `tmpl->personality`.
|
||||
- For each section, free `content_template`.
|
||||
- Zero the struct.
|
||||
|
||||
---
|
||||
|
||||
## Phase 2: Variable Resolver in `src/agent.c`
|
||||
|
||||
### Step 2.1: Create a resolver function
|
||||
|
||||
Add a static function in `src/agent.c`:
|
||||
|
||||
```c
|
||||
static char* agent_resolve_template_var(const char* var_name, void* user_data);
|
||||
```
|
||||
|
||||
This function maps variable names to data sources:
|
||||
|
||||
| Variable Name | Source | Implementation |
|
||||
|---|---|---|
|
||||
| `admin_pubkey` | `g_cfg->admin.pubkey` | `strdup(g_cfg->admin.pubkey)` |
|
||||
| `admin_kind0_json` | `nostr_handler_get_admin_kind0_context()` | Already returns malloc'd string |
|
||||
| `admin_kind10002_json` | `nostr_handler_get_admin_kind10002_context()` | Already returns malloc'd string |
|
||||
| `startup_events_json` | Serialize startup events | Reuse logic from current `append_startup_events_context()` |
|
||||
| `adopted_skills_content` | Build skills string | Reuse logic from current `append_adopted_skills_context()` |
|
||||
| `admin_notes_content` | `nostr_handler_get_admin_kind1_notes_context()` | Already returns malloc'd string |
|
||||
| `agent_pubkey` | `g_cfg->keys.public_key_hex` | `strdup(g_cfg->keys.public_key_hex)` |
|
||||
|
||||
The `user_data` parameter is unused (NULL) since the resolver accesses globals.
|
||||
|
||||
### Step 2.2: Extract helper functions from existing code
|
||||
|
||||
Refactor the following existing static functions to return malloc'd strings instead of appending directly to a cJSON array:
|
||||
|
||||
- Extract startup events serialization from `append_startup_events_context()` (lines 383-434) into a new `static char* build_startup_events_string(void)`.
|
||||
- Extract adopted skills content from `append_adopted_skills_context()` (lines ~744-940) into a new `static char* build_adopted_skills_string(void)`.
|
||||
|
||||
These helpers are called by the resolver function.
|
||||
|
||||
---
|
||||
|
||||
## Phase 3: Wire Template into Agent
|
||||
|
||||
### Step 3.1: Parse template at init time
|
||||
|
||||
In `agent_init()` (or when `g_system_context` is set), after the soul content is available:
|
||||
|
||||
```c
|
||||
static prompt_template_t g_prompt_template;
|
||||
static int g_has_template = 0;
|
||||
```
|
||||
|
||||
After `g_system_context` is assigned, call:
|
||||
|
||||
```c
|
||||
g_has_template = (prompt_template_parse(g_system_context, &g_prompt_template) == 0);
|
||||
```
|
||||
|
||||
If `g_has_template` is true, `g_prompt_template.personality` replaces `g_system_context` for the system prompt message.
|
||||
|
||||
### Step 3.2: Modify `agent_build_admin_messages_json()`
|
||||
|
||||
Current location: `src/agent.c:1092`
|
||||
|
||||
Current signature (unchanged):
|
||||
```c
|
||||
int agent_build_admin_messages_json(const char* current_user_message, char** out_messages_json);
|
||||
```
|
||||
|
||||
New logic:
|
||||
|
||||
```c
|
||||
if (g_has_template) {
|
||||
// Build DM history as a cJSON array
|
||||
cJSON* dm_history = build_dm_history_array(current_user_message);
|
||||
|
||||
// Build messages from template
|
||||
cJSON* messages = prompt_template_build_messages(
|
||||
&g_prompt_template,
|
||||
agent_resolve_template_var,
|
||||
NULL,
|
||||
dm_history
|
||||
);
|
||||
|
||||
cJSON_Delete(dm_history);
|
||||
|
||||
if (!messages) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
char* json = cJSON_PrintUnformatted(messages);
|
||||
cJSON_Delete(messages);
|
||||
*out_messages_json = json;
|
||||
return json ? 0 : -1;
|
||||
} else {
|
||||
// Existing hardcoded assembly (current code, unchanged)
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
### Step 3.3: Extract DM history builder
|
||||
|
||||
Extract the DM history logic from `append_recent_admin_dm_history()` into a function that returns a cJSON array of user/assistant messages:
|
||||
|
||||
```c
|
||||
static cJSON* build_dm_history_array(const char* current_user_message);
|
||||
```
|
||||
|
||||
This is used by the template builder for `role: expand` sections.
|
||||
|
||||
---
|
||||
|
||||
## Phase 4: Context Log Formatting
|
||||
|
||||
### Step 4.1: Update `format_context_payload_for_log()`
|
||||
|
||||
Current location: `src/agent.c:245`
|
||||
|
||||
When `g_has_template` is true, use section names from the template instead of `detect_context_section()`:
|
||||
|
||||
- Message 0 is always `system_prompt` (the personality).
|
||||
- Messages 1..N map to template sections by index.
|
||||
- For `expand` sections, multiple messages share the same section name.
|
||||
|
||||
Change the log header from:
|
||||
```
|
||||
Message 01 | role=system | section=system_prompt
|
||||
```
|
||||
To:
|
||||
```
|
||||
Section: system_prompt | role=system
|
||||
```
|
||||
|
||||
### Step 4.2: Update `classify_part_name()` in `src/http_api.c`
|
||||
|
||||
Current location: `src/http_api.c:113`
|
||||
|
||||
Add a new exported function from `src/agent.h`:
|
||||
```c
|
||||
const char* agent_get_section_name_for_message(int message_index);
|
||||
```
|
||||
|
||||
This returns the template section name if a template is active, or falls back to the existing content-prefix detection.
|
||||
|
||||
In `classify_part_name()`, call this function first. If it returns non-NULL, use it. Otherwise fall back to the existing `strncmp` chain.
|
||||
|
||||
---
|
||||
|
||||
## Phase 5: Build System Updates
|
||||
|
||||
### Step 5.1: Update `Makefile`
|
||||
|
||||
Add `$(SRC_DIR)/prompt_template.c` to the `SRCS` list (after `trigger_manager.c`, before `http_api.c`).
|
||||
|
||||
### Step 5.2: Update `Dockerfile.alpine-musl`
|
||||
|
||||
Add `src/prompt_template.c` to the gcc command line (after `src/trigger_manager.c`, before `src/http_api.c`).
|
||||
|
||||
---
|
||||
|
||||
## Phase 6: Default Template in Soul
|
||||
|
||||
### Step 6.1: Update `config.json.example`
|
||||
|
||||
The startup events should include a soul event (kind 31120) with a `---template---` section that matches the current hardcoded behavior:
|
||||
|
||||
```markdown
|
||||
# Didactyl Agent
|
||||
|
||||
You are Didactyl, a sovereign AI agent living on Nostr.
|
||||
...existing soul content...
|
||||
|
||||
---template---
|
||||
|
||||
- section: admin_identity
|
||||
role: system
|
||||
content: |
|
||||
This is your administrator! Admin pubkey (hex): {{admin_pubkey}}
|
||||
|
||||
- section: admin_profile
|
||||
role: system
|
||||
content: |
|
||||
Administrator profile (JSON): {{admin_kind0_json}}
|
||||
|
||||
- section: admin_relay_list
|
||||
role: system
|
||||
content: |
|
||||
Administrator relay list (JSON): {{admin_kind10002_json}}
|
||||
|
||||
- section: startup_events
|
||||
role: system
|
||||
content: |
|
||||
Startup events memory (kinds/content/tags): {{startup_events_json}}
|
||||
|
||||
- section: adopted_skills
|
||||
role: system
|
||||
content: |
|
||||
{{adopted_skills_content}}
|
||||
|
||||
- section: dm_history
|
||||
role: expand
|
||||
limit: 12
|
||||
|
||||
- section: admin_notes
|
||||
role: system
|
||||
limit: 10
|
||||
content: |
|
||||
Administrator recent public notes: {{admin_notes_content}}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Phase 7: Testing
|
||||
|
||||
### Step 7.1: Backward compatibility test
|
||||
|
||||
1. Build with `make`.
|
||||
2. Run with existing config (no `---template---` in soul).
|
||||
3. Send a DM and verify `context.log` output matches pre-change format.
|
||||
4. Verify `/api/context/current` returns expected structure.
|
||||
|
||||
### Step 7.2: Template test
|
||||
|
||||
1. Edit the soul event content to include `---template---` section.
|
||||
2. Restart agent.
|
||||
3. Send a DM and verify `context.log` shows section-named headers.
|
||||
4. Verify `/api/context/current` returns section names from template.
|
||||
5. Verify the LLM receives the correct messages in the correct order.
|
||||
|
||||
### Step 7.3: Section reordering test
|
||||
|
||||
1. Move `admin_notes` section above `adopted_skills` in the template.
|
||||
2. Restart and verify the order changes in `context.log`.
|
||||
|
||||
### Step 7.4: Limit test
|
||||
|
||||
1. Set `dm_history` limit to 4 (instead of 12).
|
||||
2. Verify only 4 DM history turns appear in context.
|
||||
|
||||
---
|
||||
|
||||
## File Change Summary
|
||||
|
||||
| File | Action | Description |
|
||||
|---|---|---|
|
||||
| `src/prompt_template.h` | **NEW** | Data structures and API |
|
||||
| `src/prompt_template.c` | **NEW** | Parser, variable resolver, context builder |
|
||||
| `src/agent.c` | **MODIFY** | Add template globals, resolver function, wire into `agent_build_admin_messages_json()`, update log formatter |
|
||||
| `src/agent.h` | **MODIFY** | Add `agent_get_section_name_for_message()` export |
|
||||
| `src/http_api.c` | **MODIFY** | Update `classify_part_name()` to use template section names |
|
||||
| `Makefile` | **MODIFY** | Add `prompt_template.c` to SRCS |
|
||||
| `Dockerfile.alpine-musl` | **MODIFY** | Add `prompt_template.c` to gcc command |
|
||||
| `config.json.example` | **MODIFY** | Update soul event to include template section |
|
||||
|
||||
---
|
||||
|
||||
## Implementation Order
|
||||
|
||||
1. `src/prompt_template.h` — data structures and API declarations
|
||||
2. `src/prompt_template.c` — parser, builder, free
|
||||
3. `Makefile` + `Dockerfile.alpine-musl` — add new source file
|
||||
4. Build and verify compilation
|
||||
5. `src/agent.c` — extract helper functions (`build_startup_events_string`, `build_adopted_skills_string`, `build_dm_history_array`)
|
||||
6. `src/agent.c` — add resolver function and template globals
|
||||
7. `src/agent.c` — wire template into `agent_build_admin_messages_json()`
|
||||
8. `src/agent.c` — update `format_context_payload_for_log()`
|
||||
9. `src/agent.h` + `src/http_api.c` — section name API for classify_part_name
|
||||
10. Build and test backward compatibility (no template in soul)
|
||||
11. `config.json.example` — add template to soul event
|
||||
12. Test with template soul
|
||||
13. Test section reordering and limit changes
|
||||
286
plans/security_and_admin_context.md
Normal file
286
plans/security_and_admin_context.md
Normal file
@@ -0,0 +1,286 @@
|
||||
# Security & Admin Context Plan
|
||||
|
||||
## Overview
|
||||
|
||||
Add signature verification, privilege tiers, admin context awareness, and key isolation to Didactyl. Implemented in two phases.
|
||||
|
||||
---
|
||||
|
||||
## Phase 1 — Privilege Tiers, Signature Verification & Admin Context
|
||||
|
||||
### Privilege Tiers
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
A[Incoming Event] --> B{Verify Signature via nostr_verify_event_signature}
|
||||
B -->|Invalid| C[Drop silently + DEBUG_WARN]
|
||||
B -->|Valid| D{Identify sender}
|
||||
D -->|Admin pubkey| E[ADMIN tier]
|
||||
D -->|In admin kind 3 contact list| F[WOT tier]
|
||||
D -->|Unknown| G[STRANGER tier]
|
||||
E --> H[Full tool access + all skills + full context]
|
||||
F --> I[Chat only - no tools - LLM response]
|
||||
G --> J[Configurable canned response OR silent ignore]
|
||||
```
|
||||
|
||||
| Tier | Identity | Tools | Response | Context |
|
||||
|------|----------|-------|----------|---------|
|
||||
| **ADMIN** | `config.admin.pubkey` exact match | All tools | Full LLM with all info except private key | Soul + startup events + admin notes |
|
||||
| **WOT** | Pubkey in admin kind `3` contact list | None | Chat-only LLM response | Soul + tier instruction |
|
||||
| **STRANGER** | Anyone else | None | Configurable static response or silent ignore | N/A - no LLM call |
|
||||
|
||||
### Signature Verification
|
||||
|
||||
Every incoming event must be cryptographically verified before processing:
|
||||
|
||||
1. In `on_event()` in `src/nostr_handler.c`, call `nostr_verify_event_signature(event)` from `nostr_core_lib/nostr_core/nip001.h`
|
||||
2. If verification fails, drop the event silently with a `DEBUG_WARN` log
|
||||
3. This prevents relay-forged pubkey fields from being trusted
|
||||
4. Controlled by `security.verify_signatures` config flag (default: `true`)
|
||||
|
||||
### Stranger Response
|
||||
|
||||
Non-WoT senders get a **hardcoded configurable response** — no LLM tokens spent:
|
||||
|
||||
- Config field: `security.stranger_response`
|
||||
- Example: `"I only respond to people in my web of trust. My npub is npub12kvnu0dsa4alquu4l4zv9454cgjdr9248gd07ueq5x8yzeuan37ql02960"`
|
||||
- If `security.tiers.stranger.enabled` is `false`, silently ignore instead
|
||||
- If `true`, send the canned response as a DM
|
||||
|
||||
### Soul Update
|
||||
|
||||
Update the soul event content in `config.json` to:
|
||||
- Explicitly allow sharing the agent public key (npub) with anyone
|
||||
- Explicitly forbid revealing the private key (nsec) under any circumstances
|
||||
- Note: Phase 2 will remove the private key from agent memory entirely
|
||||
|
||||
### Admin Context Subscription
|
||||
|
||||
At startup, Didactyl subscribes to the administrator's Nostr activity to build context awareness.
|
||||
|
||||
#### What to subscribe to
|
||||
|
||||
| Kind | Purpose | Retention |
|
||||
|------|---------|-----------|
|
||||
| `0` | Admin profile metadata | Latest only - replaceable |
|
||||
| `3` | Admin contact/follow list - WoT source | Latest only - replaceable |
|
||||
| `10002` | Admin relay list | Latest only - replaceable |
|
||||
| `1` | Admin public notes | Last N posts - configurable, default 10 |
|
||||
|
||||
#### Storage
|
||||
|
||||
In-memory struct `admin_context_t` holding:
|
||||
- `kind_0_json` — latest kind 0 content string
|
||||
- `kind_3_contacts` — array of followed pubkey hex strings extracted from kind 3 `p` tags
|
||||
- `kind_3_contact_count` — count
|
||||
- `kind_10002_json` — latest kind 10002 content string
|
||||
- `kind_1_notes` — array of recent kind 1 content strings with timestamps
|
||||
- `kind_1_note_count` — count
|
||||
|
||||
#### Subscription mechanics
|
||||
|
||||
- Single subscription filter: `{"authors": ["<admin_pubkey>"], "kinds": [0, 3, 10002, 1], "limit": <configurable>}`
|
||||
- On EOSE: initial load complete, WoT set is ready
|
||||
- After EOSE: live updates as admin posts new content
|
||||
- Kind 3 `p` tags are extracted into a sorted array for O(log n) WoT lookup
|
||||
|
||||
#### Context injection
|
||||
|
||||
- Admin kind 1 notes are injected into the LLM context as a system message: "Administrator recent public notes:" followed by the content
|
||||
- Only injected for ADMIN-tier conversations
|
||||
|
||||
### Config Schema — Phase 1
|
||||
|
||||
New sections in `config.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"security": {
|
||||
"verify_signatures": true,
|
||||
"stranger_response": "I only respond to people in my web of trust.",
|
||||
"tiers": {
|
||||
"admin": {
|
||||
"tools_enabled": true
|
||||
},
|
||||
"wot": {
|
||||
"enabled": true,
|
||||
"tools_enabled": false
|
||||
},
|
||||
"stranger": {
|
||||
"enabled": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"admin_context": {
|
||||
"enabled": true,
|
||||
"subscribe_kinds": [0, 3, 10002, 1],
|
||||
"kind_1_limit": 10
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- `security.verify_signatures` — master toggle for signature verification (default: `true`)
|
||||
- `security.stranger_response` — canned DM text for non-WoT senders (default: empty = silent ignore)
|
||||
- `security.tiers.wot.enabled` — whether WoT contacts can converse (default: `true`)
|
||||
- `security.tiers.stranger.enabled` — whether strangers get the canned response (default: `true`; if `false`, silent ignore)
|
||||
- `admin_context.enabled` — whether to subscribe to admin activity (default: `true`)
|
||||
- `admin_context.subscribe_kinds` — which kinds to track (default: `[0, 3, 10002, 1]`)
|
||||
- `admin_context.kind_1_limit` — how many kind 1 notes to retain (default: `10`)
|
||||
|
||||
### Phase 1 Implementation Steps
|
||||
|
||||
1. **Add signature verification to `on_event()`**
|
||||
- File: `src/nostr_handler.c`
|
||||
- Call `nostr_verify_event_signature(event)` at the top of `on_event()`
|
||||
- If it returns non-zero, log and drop
|
||||
|
||||
2. **Add `admin_context_t` struct and security config structs**
|
||||
- File: `src/config.h`
|
||||
- New structs for admin context storage, security config, tier config
|
||||
|
||||
3. **Parse new config sections**
|
||||
- File: `src/config.c`
|
||||
- Parse `security` and `admin_context` from config.json
|
||||
- Apply defaults when sections are missing
|
||||
|
||||
4. **Add admin context subscription**
|
||||
- File: `src/nostr_handler.c`
|
||||
- New function `nostr_handler_subscribe_admin_context()`
|
||||
- Callback that populates `admin_context_t` from incoming events
|
||||
- Extract kind 3 `p` tags into WoT set
|
||||
- Store kind 1 notes in a ring buffer capped at `kind_1_limit`
|
||||
|
||||
5. **Add WoT lookup function**
|
||||
- File: `src/nostr_handler.c`
|
||||
- `int nostr_handler_is_wot_contact(const char* pubkey_hex)` — checks if pubkey is in admin kind 3 list
|
||||
|
||||
6. **Refactor `on_event()` for tier dispatch**
|
||||
- File: `src/nostr_handler.c`
|
||||
- After signature verification, classify sender into ADMIN / WOT / STRANGER
|
||||
- Pass tier information to the callback — extend `dm_callback_t` signature to include tier enum
|
||||
- STRANGER with `stranger.enabled`: send canned response directly, no callback
|
||||
- STRANGER without: drop silently
|
||||
|
||||
7. **Refactor `agent_on_message()` for tier-aware behavior**
|
||||
- File: `src/agent.c`
|
||||
- ADMIN: current behavior — all tools, full context, admin notes injected
|
||||
- WOT: build messages with soul + tier instruction, no tools_json, LLM chat-only
|
||||
- STRANGER: should not reach here — handled in nostr_handler
|
||||
|
||||
8. **Inject admin context into ADMIN conversations**
|
||||
- File: `src/agent.c`
|
||||
- After startup events context, append admin kind 1 notes as a system message
|
||||
- Only for ADMIN tier
|
||||
|
||||
9. **Update soul content in `config.json`**
|
||||
- Allow sharing public key with anyone
|
||||
- Forbid revealing private key under any circumstances
|
||||
|
||||
10. **Update `config.json` with new sections**
|
||||
- Add `security` and `admin_context` sections
|
||||
|
||||
11. **Update `README.md`**
|
||||
- Document privilege tiers
|
||||
- Document new config sections
|
||||
- Update architecture diagram
|
||||
|
||||
12. **Build and validate**
|
||||
- Run `./build_static.sh`
|
||||
- Verify compilation succeeds
|
||||
|
||||
---
|
||||
|
||||
## Phase 2 — NIP-46 Remote Signer for Key Isolation
|
||||
|
||||
### Problem
|
||||
|
||||
Even with Phase 1 protections, the private key (nsec) still lives in Didactyl process memory and in `config.json` on disk. The LLM has `shell_exec` and `file_read` tools — a sufficiently clever prompt injection could theoretically:
|
||||
- Read `config.json` via `file_read` (contains nsec)
|
||||
- Execute `cat /proc/self/maps` or similar via `shell_exec`
|
||||
- Exfiltrate the key via `nostr_post`
|
||||
|
||||
### Solution: NIP-46 Remote Signer
|
||||
|
||||
Remove the private key from Didactyl entirely. All cryptographic operations go through a NIP-46 remote signer.
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
A[Didactyl Agent] -->|kind 24133 sign_event / nip04_encrypt / nip04_decrypt| B[Remote Signer]
|
||||
B -->|signed events / ciphertext / plaintext| A
|
||||
A -.->|NO private key in memory| A
|
||||
B -->|Private key held ONLY here| B
|
||||
C[config.json] -.->|bunker URL only, no nsec| A
|
||||
```
|
||||
|
||||
### Architecture
|
||||
|
||||
1. **Remote signer process** — a separate lightweight daemon that:
|
||||
- Holds the private key only in working memory (never on disk after initial load)
|
||||
- Listens for NIP-46 requests via relay (kind `24133`)
|
||||
- Responds with signed events, encrypted/decrypted content
|
||||
- Can run on the same machine or a different one
|
||||
|
||||
2. **Didactyl as NIP-46 client** — instead of calling `nostr_create_and_sign_event()` directly:
|
||||
- Sends `sign_event` RPC to the remote signer
|
||||
- Sends `nip04_encrypt` / `nip04_decrypt` RPC for DM handling
|
||||
- Only holds a disposable `client-keypair` for NIP-46 communication
|
||||
|
||||
3. **Config change** — `config.json` replaces `keys.nsec` with:
|
||||
```json
|
||||
{
|
||||
"keys": {
|
||||
"bunker_url": "bunker://<remote-signer-pubkey>?relay=wss://relay.example.com&secret=<secret>",
|
||||
"npub": "npub1..."
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### NIP-46 Methods Required
|
||||
|
||||
| Method | Current direct call | Used for |
|
||||
|--------|-------------------|----------|
|
||||
| `sign_event` | `nostr_create_and_sign_event()` | Publishing all events |
|
||||
| `nip04_encrypt` | `nostr_nip04_encrypt()` | Sending DMs |
|
||||
| `nip04_decrypt` | `nostr_nip04_decrypt()` | Receiving DMs |
|
||||
| `get_public_key` | `config.keys.public_key_hex` | Identity |
|
||||
|
||||
### Implementation Scope
|
||||
|
||||
1. **Build NIP-46 client protocol in nostr_core_lib**
|
||||
- NIP-44 encryption for request/response content
|
||||
- Kind 24133 event creation and parsing
|
||||
- JSON-RPC request/response handling
|
||||
- Connection establishment (bunker URL parsing)
|
||||
- Async request/response matching by request ID
|
||||
|
||||
2. **Build or integrate a remote signer**
|
||||
- Option A: Build a minimal signer binary in C (ships with Didactyl)
|
||||
- Option B: Support existing signers (nsecBunker, etc.)
|
||||
- Option C: Both — ship a minimal one, support external ones
|
||||
|
||||
3. **Refactor all signing/encryption calls**
|
||||
- Every `nostr_create_and_sign_event()` → `nip46_sign_event()` (async RPC)
|
||||
- Every `nostr_nip04_encrypt()` → `nip46_nip04_encrypt()` (async RPC)
|
||||
- Every `nostr_nip04_decrypt()` → `nip46_nip04_decrypt()` (async RPC)
|
||||
- These become blocking calls that send a request and wait for a response
|
||||
|
||||
4. **Startup flow change**
|
||||
- Parse bunker URL from config
|
||||
- Generate client keypair
|
||||
- Send `connect` request to remote signer
|
||||
- Call `get_public_key` to learn user pubkey
|
||||
- Proceed with normal startup
|
||||
|
||||
5. **Backward compatibility**
|
||||
- If `keys.nsec` is present, use direct signing (current behavior)
|
||||
- If `keys.bunker_url` is present, use NIP-46 remote signing
|
||||
- This allows gradual migration
|
||||
|
||||
### Security Properties Achieved
|
||||
|
||||
- Private key never in Didactyl process memory
|
||||
- Private key never on disk in config.json
|
||||
- LLM tools (shell_exec, file_read) cannot access the key
|
||||
- Even full process compromise of Didactyl does not leak the signing key
|
||||
- Remote signer can be on a separate hardened machine
|
||||
- Remote signer can implement rate limiting, approval flows, etc.
|
||||
337
plans/skill_tools.md
Normal file
337
plans/skill_tools.md
Normal file
@@ -0,0 +1,337 @@
|
||||
# Skill Tools — Architecture Plan
|
||||
|
||||
## Overview
|
||||
|
||||
Add a family of five skill-management tools to Didactyl so the agent can **create, list, adopt, remove, and discover** skills at runtime — all through the existing LLM tool-calling loop.
|
||||
|
||||
Skills are Nostr events. The tools are thin orchestration wrappers over the existing `nostr_handler_publish_kind_event()` and `nostr_handler_query_json()` primitives.
|
||||
|
||||
---
|
||||
|
||||
## Nostr Kind Reference
|
||||
|
||||
| Kind | Purpose | Replaceable? | Key tag |
|
||||
|---|---|---|---|
|
||||
| `31123` | Public skill definition | Yes (d-tag) | `d=<slug>` |
|
||||
| `31124` | Private skill definition | Yes (d-tag) | `d=<slug>` |
|
||||
| `10123` | Public skill adoption list | Yes (replaceable) | `a` refs to 31123 events |
|
||||
|
||||
All skill events carry these standard tags:
|
||||
- `["d", "<slug>"]` — unique identifier within the author's pubkey
|
||||
- `["app", "didactyl"]` — app namespace
|
||||
- `["scope", "public"]` or `["scope", "private"]`
|
||||
|
||||
---
|
||||
|
||||
## Tool Family
|
||||
|
||||
### 1. `skill_create`
|
||||
|
||||
**Purpose:** Create or update a skill definition and optionally auto-adopt it.
|
||||
|
||||
**OpenAI schema:**
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "skill_create",
|
||||
"description": "Create or update a skill definition (kind 31123 public / 31124 private) and optionally auto-adopt it",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"slug": { "type": "string", "description": "Unique skill identifier (lowercase, hyphens allowed)" },
|
||||
"content": { "type": "string", "description": "Skill body — markdown instructions or structured JSON" },
|
||||
"scope": { "type": "string", "description": "public (kind 31123) or private (kind 31124). Default: public" },
|
||||
"description": { "type": "string", "description": "Short one-line description for the skill" },
|
||||
"auto_adopt": { "type": "boolean", "description": "Automatically add to adoption list (kind 10123). Default: true" }
|
||||
},
|
||||
"required": ["slug", "content"]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Execution logic (`execute_skill_create`):**
|
||||
|
||||
1. Validate `slug` — must be non-empty, lowercase alphanumeric + hyphens, no spaces
|
||||
2. Determine kind: `31123` if scope is `"public"` or absent; `31124` if `"private"`
|
||||
3. Build tags array:
|
||||
- `["d", slug]`
|
||||
- `["app", "didactyl"]`
|
||||
- `["scope", scope]`
|
||||
- `["description", description]` if provided
|
||||
4. Call `nostr_handler_publish_kind_event(kind, content, tags, &result)`
|
||||
5. If `auto_adopt` is true (default), update the kind `10123` adoption list:
|
||||
- Query existing `10123` event for own pubkey (same pattern as `execute_nostr_list_manage`)
|
||||
- Add `["a", "31123:<own_pubkey>:<slug>"]` tag if not already present
|
||||
- Republish the updated `10123` event
|
||||
6. Return JSON with `success`, `event_id`, `naddr_uri`, `slug`, `adopted`
|
||||
|
||||
**Key design decisions:**
|
||||
- Auto-adopt defaults to `true` — creating a skill you don't adopt is unusual
|
||||
- Private skills (31124) are NOT added to the public adoption list (10123)
|
||||
- Republishing with the same slug replaces the previous version (replaceable event)
|
||||
|
||||
---
|
||||
|
||||
### 2. `skill_list`
|
||||
|
||||
**Purpose:** List the agent's own published skills.
|
||||
|
||||
**OpenAI schema:**
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "skill_list",
|
||||
"description": "List skills published by this agent, optionally filtered by scope",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"scope": { "type": "string", "description": "Filter by public or private. Omit for both." }
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Execution logic (`execute_skill_list`):**
|
||||
|
||||
1. Build filter based on scope:
|
||||
- Both: `{"kinds": [31123, 31124], "authors": [own_pubkey]}`
|
||||
- Public only: `{"kinds": [31123], "authors": [own_pubkey]}`
|
||||
- Private only: `{"kinds": [31124], "authors": [own_pubkey]}`
|
||||
2. Call `nostr_handler_query_json(filter, 8000)`
|
||||
3. Parse results, extract for each event:
|
||||
- `slug` (from d-tag)
|
||||
- `kind`
|
||||
- `scope` (from scope tag)
|
||||
- `description` (from description tag, if present)
|
||||
- `created_at` timestamp
|
||||
- `content` preview (first 200 chars)
|
||||
4. Return JSON array of skill summaries
|
||||
|
||||
---
|
||||
|
||||
### 3. `skill_adopt`
|
||||
|
||||
**Purpose:** Adopt a skill published by another author (or self) into the agent's adoption list.
|
||||
|
||||
**OpenAI schema:**
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "skill_adopt",
|
||||
"description": "Add a skill to the agent's public adoption list (kind 10123)",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"pubkey": { "type": "string", "description": "Hex pubkey of the skill author" },
|
||||
"slug": { "type": "string", "description": "Skill slug (d-tag value)" },
|
||||
"kind": { "type": "integer", "description": "Skill kind (31123 or 31124). Default: 31123" }
|
||||
},
|
||||
"required": ["pubkey", "slug"]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Execution logic (`execute_skill_adopt`):**
|
||||
|
||||
1. Validate pubkey (64-char hex) and slug (non-empty)
|
||||
2. Default kind to 31123 if not provided
|
||||
3. Build the `a`-tag value: `"<kind>:<pubkey>:<slug>"`
|
||||
4. Query existing kind `10123` event for own pubkey
|
||||
5. Check if `["a", "<kind>:<pubkey>:<slug>"]` already exists — if so, return success with `already_adopted: true`
|
||||
6. Add the tag, republish `10123`
|
||||
7. Return JSON with `success`, `adopted_address`, `event_id`
|
||||
|
||||
---
|
||||
|
||||
### 4. `skill_remove`
|
||||
|
||||
**Purpose:** Remove a skill from the agent's adoption list.
|
||||
|
||||
**OpenAI schema:**
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "skill_remove",
|
||||
"description": "Remove a skill from the agent's public adoption list (kind 10123)",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"pubkey": { "type": "string", "description": "Hex pubkey of the skill author. Defaults to own pubkey." },
|
||||
"slug": { "type": "string", "description": "Skill slug to remove" },
|
||||
"kind": { "type": "integer", "description": "Skill kind (31123 or 31124). Default: 31123" }
|
||||
},
|
||||
"required": ["slug"]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Execution logic (`execute_skill_remove`):**
|
||||
|
||||
1. Default pubkey to own pubkey if not provided
|
||||
2. Default kind to 31123
|
||||
3. Build the `a`-tag value: `"<kind>:<pubkey>:<slug>"`
|
||||
4. Query existing kind `10123` event for own pubkey
|
||||
5. Find and remove matching `["a", ...]` tag
|
||||
6. Republish `10123`
|
||||
7. Return JSON with `success`, `removed_address`, `event_id`
|
||||
|
||||
---
|
||||
|
||||
### 5. `skill_search`
|
||||
|
||||
**Purpose:** Search for skills across the agent's Web of Trust.
|
||||
|
||||
**OpenAI schema:**
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "skill_search",
|
||||
"description": "Search for skills adopted by Web of Trust contacts, or query public skill definitions",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"query": { "type": "string", "description": "Optional keyword to filter skill slugs or descriptions" },
|
||||
"pubkey": { "type": "string", "description": "Search skills by a specific author pubkey" },
|
||||
"popular": { "type": "boolean", "description": "If true, query WoT adoption lists to find most-adopted skills" }
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Execution logic (`execute_skill_search`):**
|
||||
|
||||
1. If `popular` is true:
|
||||
- Query `{"kinds": [10123]}` from relays (with reasonable limit)
|
||||
- Parse all `a`-tags from results
|
||||
- Count occurrences of each `a`-tag address
|
||||
- Sort by adoption count descending
|
||||
- Return top N skill addresses with counts
|
||||
2. If `pubkey` is provided:
|
||||
- Query `{"kinds": [31123], "authors": [pubkey]}`
|
||||
- Return skill summaries
|
||||
3. If `query` is provided (keyword search):
|
||||
- Query `{"kinds": [31123]}` with limit
|
||||
- Filter results client-side by matching `query` against slug, description tag, or content
|
||||
- Return matching skill summaries
|
||||
4. Default (no params): return own adopted skills from `10123`
|
||||
|
||||
---
|
||||
|
||||
## Implementation Architecture
|
||||
|
||||
### Shared helper: adoption list update
|
||||
|
||||
Since `skill_create`, `skill_adopt`, and `skill_remove` all modify the kind `10123` list, extract a shared helper:
|
||||
|
||||
```c
|
||||
// Fetch current 10123 event, return duplicated tags array (or empty array if none exists)
|
||||
static cJSON* fetch_adoption_list_tags(tools_context_t* ctx);
|
||||
|
||||
// Publish updated 10123 event with new tags
|
||||
static int publish_adoption_list(tools_context_t* ctx, cJSON* tags, nostr_publish_result_t* result);
|
||||
```
|
||||
|
||||
This is essentially the same pattern already used in `execute_nostr_list_manage()` but specialized for kind `10123`.
|
||||
|
||||
### Shared helper: skill summary extraction
|
||||
|
||||
```c
|
||||
// Extract slug, kind, scope, description, created_at from a skill event JSON
|
||||
static cJSON* extract_skill_summary(cJSON* event);
|
||||
```
|
||||
|
||||
### Flow diagram
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
SC[skill_create] --> PUB[nostr_handler_publish_kind_event]
|
||||
SC --> ADOPT_HELPER[update adoption list helper]
|
||||
|
||||
SL[skill_list] --> QUERY[nostr_handler_query_json]
|
||||
SL --> SUMMARY[extract_skill_summary]
|
||||
|
||||
SA[skill_adopt] --> ADOPT_HELPER
|
||||
SR[skill_remove] --> ADOPT_HELPER
|
||||
|
||||
SS[skill_search] --> QUERY
|
||||
SS --> SUMMARY
|
||||
|
||||
ADOPT_HELPER --> QUERY
|
||||
ADOPT_HELPER --> PUB
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Changes Required
|
||||
|
||||
### `src/tools.c`
|
||||
|
||||
1. **Schema registration** — Add 5 new tool definitions in `tools_build_openai_schema_json()` (t22–t26)
|
||||
2. **Execution functions** — Add 5 new `execute_skill_*()` static functions
|
||||
3. **Dispatch** — Add 5 new `strcmp` branches in `tools_execute()`
|
||||
4. **Shared helpers** — Add `fetch_adoption_list_tags()`, `publish_adoption_list()`, `extract_skill_summary()`, and `validate_skill_slug()`
|
||||
|
||||
### `README.md`
|
||||
|
||||
1. Add skill tools to the Tooling Interface section under a new "Skill management" category
|
||||
|
||||
### No changes needed to:
|
||||
- `src/tools.h` — the `tools_context_t` already has `cfg` which provides `keys.public_key_hex`
|
||||
- `src/nostr_handler.h` — all needed APIs already exist
|
||||
- `src/config.h` — no new config fields needed
|
||||
|
||||
---
|
||||
|
||||
## Slug Validation Rules
|
||||
|
||||
A valid skill slug must:
|
||||
- Be 1–64 characters
|
||||
- Contain only lowercase letters, digits, and hyphens
|
||||
- Not start or end with a hyphen
|
||||
- Not contain consecutive hyphens
|
||||
|
||||
```c
|
||||
static int validate_skill_slug(const char* slug) {
|
||||
if (!slug || slug[0] == '\0' || strlen(slug) > 64) return 0;
|
||||
if (slug[0] == '-') return 0;
|
||||
int prev_dash = 0;
|
||||
for (size_t i = 0; slug[i]; i++) {
|
||||
char c = slug[i];
|
||||
if (c == '-') {
|
||||
if (prev_dash) return 0;
|
||||
prev_dash = 1;
|
||||
} else if (islower(c) || isdigit(c)) {
|
||||
prev_dash = 0;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (slug[strlen(slug) - 1] == '-') return 0;
|
||||
return 1;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Implementation Order
|
||||
|
||||
1. **Shared helpers** — `validate_skill_slug`, `fetch_adoption_list_tags`, `publish_adoption_list`, `extract_skill_summary`
|
||||
2. **`skill_create`** — most important tool, enables the agent to author skills
|
||||
3. **`skill_list`** — lets the agent see what it has published
|
||||
4. **`skill_adopt`** — adopt skills from other authors
|
||||
5. **`skill_remove`** — remove skills from adoption list
|
||||
6. **`skill_search`** — discover skills across WoT
|
||||
7. **Schema registration** — add all 5 tools to `tools_build_openai_schema_json()`
|
||||
8. **Dispatch wiring** — add all 5 to `tools_execute()`
|
||||
9. **README update** — document the new tools
|
||||
10. **Build and test** — verify compilation and basic tool execution
|
||||
|
||||
---
|
||||
|
||||
## Security Considerations
|
||||
|
||||
- **Admin-only**: Skill tools inherit the existing ADMIN tier restriction — only the admin can trigger tool calls
|
||||
- **Slug validation**: Prevents injection of malformed d-tags
|
||||
- **No arbitrary kind**: `skill_create` only publishes kind 31123 or 31124, not arbitrary kinds
|
||||
- **Adoption list integrity**: The helpers always fetch-then-update to avoid clobbering existing adoption entries
|
||||
- **Content size**: No explicit limit on skill content size — relies on relay limits and LLM context window constraints
|
||||
360
plans/tool_orchestration.md
Normal file
360
plans/tool_orchestration.md
Normal file
@@ -0,0 +1,360 @@
|
||||
# Tool Orchestration Plan
|
||||
|
||||
## Overview
|
||||
|
||||
This plan covers four related features:
|
||||
1. **Slash commands** — direct tool execution bypassing the LLM
|
||||
2. **Skill-forward execution (`/run` + `skill_run`)** — one-shot skill invocation with LLM, including external skill sharing
|
||||
3. **Skill-tool maturity levels** — skills that register as callable tools with promotion workflow
|
||||
4. **Deterministic step executor** — hardened skills that run without LLM involvement
|
||||
|
||||
## 1. Slash Commands (Direct Tool Execution)
|
||||
|
||||
### Behavior
|
||||
When a message starts with `/`, bypass the LLM entirely and execute the tool directly.
|
||||
|
||||
```
|
||||
/shell_exec {"command": "ls -la"}
|
||||
/nostr_query {"filter": {"kinds": [1], "limit": 5}}
|
||||
/nostr_nip05_lookup {"identifier": "jack@cash.app"}
|
||||
```
|
||||
|
||||
### Parsing Rules
|
||||
- `/tool_name` — call tool with empty args `{}`
|
||||
- `/tool_name {"key": "value"}` — call tool with JSON args
|
||||
- `/tool_name plain text` — wrap as `{"input": "plain text"}` (convenience)
|
||||
- `/help` — list available tools
|
||||
- `/help tool_name` — show tool schema
|
||||
|
||||
### Implementation
|
||||
1. In [`agent_on_message()`](../src/agent.c:1453), check if `message[0] == '/'`
|
||||
2. Parse tool name (everything between `/` and first space or end)
|
||||
3. Parse args (everything after tool name, try JSON first, fall back to string wrapper)
|
||||
4. Call [`tools_execute()`](../src/tools.c) directly
|
||||
5. Send result as DM — no LLM round-trip
|
||||
6. Still log to context.log.md with `phase=direct_tool_exec`
|
||||
|
||||
### Special Slash Commands
|
||||
- `/help` — list available tools
|
||||
- `/help tool_name` — show tool schema
|
||||
- `/run` — skill-forward execution (see section 2)
|
||||
|
||||
### Security
|
||||
- Only admin tier can use slash commands (same as current tool policy)
|
||||
- Slash commands respect the same `tools.enabled` and `security.admin.tools_enabled` config flags
|
||||
|
||||
## 2. Skill-Forward Execution
|
||||
|
||||
### The Problem
|
||||
|
||||
Today, skills are passive — they're injected into the LLM context via [`append_adopted_skills_context()`](../src/agent.c:1418) and the LLM decides when they're relevant. There's no way to say "run this specific skill right now" and there's no way to try someone else's skill without permanently adopting it.
|
||||
|
||||
### Three Invocation Layers
|
||||
|
||||
| Layer | How it works | LLM? | Persists? |
|
||||
|-------|-------------|------|-----------|
|
||||
| **Passive/adopted** | Skill instructions injected into every conversation context | Yes, LLM decides relevance | Yes — in kind 10123 adoption list |
|
||||
| **Skill-forward** | Skill instructions become the primary system prompt; LLM executes them | Yes, but constrained | No — one-shot execution |
|
||||
| **Hardened** | Deterministic step executor, no LLM | No | Yes — adopted skill with `execution: hardened` |
|
||||
|
||||
### Entry Points
|
||||
|
||||
#### A. `/run` slash command (admin direct invocation)
|
||||
|
||||
```
|
||||
/run deploy-website staging # run own adopted skill by slug
|
||||
/run 31123:<pubkey>:deploy-website staging # run anyone's skill by address
|
||||
/run deploy-website {"target": "production"} # JSON args
|
||||
```
|
||||
|
||||
Parsing:
|
||||
1. First token after `/run` is the skill identifier (slug or `kind:pubkey:slug` address)
|
||||
2. Everything after is args (try JSON first, fall back to `{"input": "plain text"}`)
|
||||
|
||||
#### B. `skill_run` tool (LLM-mediated invocation)
|
||||
|
||||
The LLM can invoke skills on behalf of the admin during conversation:
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "skill_run",
|
||||
"description": "Fetch and execute a skill one-shot without adopting it. Works with own adopted skills by slug or any public skill by address.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"slug": { "type": "string", "description": "Skill slug for own adopted skills" },
|
||||
"address": { "type": "string", "description": "Full skill address: kind:pubkey:slug" },
|
||||
"pubkey": { "type": "string", "description": "Author pubkey, used with slug to form address" },
|
||||
"args": { "type": "string", "description": "Arguments or context to pass to the skill" },
|
||||
"sandbox": { "type": "boolean", "description": "Override sandbox setting. Default: true for external, false for own skills" }
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
This enables natural conversation like:
|
||||
> "My friend @jack just published a skill called summarize-thread. Try it on the latest thread in my feed."
|
||||
|
||||
The agent would `skill_search` to find it, then `skill_run` to execute it.
|
||||
|
||||
### Execution Flow
|
||||
|
||||
Both `/run` and `skill_run` use the same underlying executor:
|
||||
|
||||
```mermaid
|
||||
graph TD
|
||||
A[/run or skill_run called] --> B{Skill identifier type?}
|
||||
B -->|slug only| C[Look up in adopted skills cache]
|
||||
B -->|address or pubkey+slug| D[Fetch skill event from Nostr]
|
||||
C --> E{Found?}
|
||||
D --> E
|
||||
E -->|No| F[Return error: skill not found]
|
||||
E -->|Yes| G{External skill?}
|
||||
G -->|Yes| H[Apply sandbox - restrict tools]
|
||||
G -->|No| I[Full tool access]
|
||||
H --> J[Build skill-forward prompt]
|
||||
I --> J
|
||||
J --> K[System: base context + skill instructions]
|
||||
K --> L[User: args as user message]
|
||||
L --> M[LLM call with tools]
|
||||
M --> N[Return result to caller]
|
||||
```
|
||||
|
||||
### Skill-Forward Prompt Construction
|
||||
|
||||
Reuses the pattern from [`agent_on_trigger()`](../src/agent.c:1837):
|
||||
|
||||
```
|
||||
[base system context / soul]
|
||||
|
||||
Skill execution context:
|
||||
- You are executing a specific skill on demand.
|
||||
- Follow the skill instructions below precisely.
|
||||
- The user's arguments provide the context for this execution.
|
||||
- Keep output concise and actionable.
|
||||
|
||||
Skill slug: deploy-website
|
||||
Skill address: 31123:<pubkey>:deploy-website
|
||||
Skill source: [own | external:<author_display_name>]
|
||||
|
||||
Skill instructions:
|
||||
[skill content here]
|
||||
```
|
||||
|
||||
User message:
|
||||
```
|
||||
[args provided by caller]
|
||||
```
|
||||
|
||||
The prompt is so skill-forward that the LLM has no real option but to execute the skill instructions against the provided args.
|
||||
|
||||
### Sandbox for External Skills
|
||||
|
||||
When executing a skill from another author (not own pubkey), a **tool sandbox** is applied by default:
|
||||
|
||||
**Allowed tools (safe/read-only):**
|
||||
- `nostr_query` — read Nostr events
|
||||
- `nostr_nip05_lookup` — NIP-05 lookups
|
||||
- `nostr_post` — publish events (the agent signs, so this is safe)
|
||||
- `nostr_list_manage` — manage lists
|
||||
- `skill_list`, `skill_search` — read skill metadata
|
||||
|
||||
**Blocked tools (destructive/dangerous):**
|
||||
- `shell_exec` — arbitrary command execution
|
||||
- `file_read`, `file_write` — filesystem access
|
||||
- Any future tools marked as `destructive: true`
|
||||
|
||||
**Override:** The admin can explicitly opt in to full tool access:
|
||||
- `/run --unsafe 31123:<pubkey>:risky-skill args`
|
||||
- `skill_run` with `sandbox: false`
|
||||
|
||||
### Skill Sharing on Nostr
|
||||
|
||||
```mermaid
|
||||
graph TD
|
||||
A[Friend creates skill] -->|kind 31123| B[Published on Nostr relays]
|
||||
B --> C{How do you find it?}
|
||||
C -->|skill_search popular:true| D[Discovery via WoT adoption lists]
|
||||
C -->|Friend tells you the slug| E[Direct reference]
|
||||
C -->|skill_search pubkey:friend| F[Browse friends skills]
|
||||
D --> G[skill_run - try it once, sandboxed]
|
||||
E --> G
|
||||
F --> G
|
||||
G -->|Liked it?| H{Adopt?}
|
||||
H -->|Yes| I[skill_adopt - permanent]
|
||||
H -->|No| J[Done - nothing persisted]
|
||||
I --> K[Shows in adopted skills context]
|
||||
K --> L[Agent uses it automatically]
|
||||
L -->|Or invoke directly| M[/run skill-slug args]
|
||||
```
|
||||
|
||||
## 3. Skill-Tool Maturity Levels
|
||||
|
||||
Skills can declare an execution maturity level that determines how they run:
|
||||
|
||||
| Level | Execution | LLM? | Use case |
|
||||
|-------|-----------|-------|----------|
|
||||
| `draft` | LLM interprets procedure text from skill instructions | Yes | Exploring and iterating on a workflow |
|
||||
| `guided` | LLM with forced tool_choice + parameter defaults | Yes, constrained | Workflow is stable but needs LLM judgment |
|
||||
| `hardened` | Deterministic step executor, no LLM | No | Workflow is proven and should run exactly as defined |
|
||||
|
||||
### Skill Definition Extensions
|
||||
|
||||
```yaml
|
||||
kind: 31123
|
||||
d: deploy_website
|
||||
execution: hardened
|
||||
tool_schema:
|
||||
name: deploy_website
|
||||
description: Build and deploy the static website
|
||||
parameters:
|
||||
target:
|
||||
type: string
|
||||
enum: [staging, production]
|
||||
default: staging
|
||||
steps:
|
||||
- tool: shell_exec
|
||||
args:
|
||||
command: "make build TARGET={{target}}"
|
||||
- tool: shell_exec
|
||||
args:
|
||||
command: "rsync -av dist/ server:/var/www/{{target}}/"
|
||||
- return: "Deployed to {{target}}"
|
||||
```
|
||||
|
||||
### How Each Level Works
|
||||
|
||||
**draft:** Current behavior. Skill instructions are injected into context. LLM reads them and decides which tools to call. No special handling needed.
|
||||
|
||||
**guided:** Agent sets `tool_choice` to the skill's preferred tool. Parameter defaults from the skill are merged with the model's generated arguments (skill defaults win on conflict). Reduces LLM freedom while still allowing it to fill in dynamic values.
|
||||
|
||||
**hardened:** Agent executes the `steps` array directly using the deterministic step executor. No LLM call at all. The skill becomes equivalent to a slash command.
|
||||
|
||||
### Promotion Workflow
|
||||
1. Admin iterates with LLM on a task (draft)
|
||||
2. Admin saves working procedure as a skill: `skill_create` with `execution: draft`
|
||||
3. Admin tests, refines, promotes: update skill to `execution: guided`
|
||||
4. Once proven reliable, promote to `execution: hardened` with explicit `steps`
|
||||
5. Hardened skills become available as slash commands: `/deploy_website {"target": "production"}`
|
||||
|
||||
## 4. Deterministic Step Executor
|
||||
|
||||
A simple sequential executor for hardened skills.
|
||||
|
||||
### Step Types
|
||||
|
||||
```yaml
|
||||
steps:
|
||||
# Execute a tool
|
||||
- tool: shell_exec
|
||||
args: {command: "ls -la"}
|
||||
save_as: listing # optional: save result to variable
|
||||
|
||||
# Execute a tool with variable substitution
|
||||
- tool: nostr_post
|
||||
args:
|
||||
kind: 30023
|
||||
content: "{{file_content}}"
|
||||
tags: [["d", "{{slug}}"]]
|
||||
|
||||
# Conditional - simple
|
||||
- if: "{{listing.success}}"
|
||||
then:
|
||||
- tool: shell_exec
|
||||
args: {command: "echo done"}
|
||||
else:
|
||||
- return: "Failed: {{listing.error}}"
|
||||
|
||||
# Return final result
|
||||
- return: "Published to {{slug}}"
|
||||
```
|
||||
|
||||
### Variable Substitution
|
||||
- `{{param_name}}` — from tool parameters provided by caller
|
||||
- `{{step_name.field}}` — from a previous step's result (requires `save_as`)
|
||||
- Simple string replacement, no expression evaluation
|
||||
|
||||
### Implementation in C
|
||||
- Parse `steps` array from skill content (JSON or YAML)
|
||||
- Iterate steps sequentially
|
||||
- For each `tool` step: call [`tools_execute()`](../src/tools.c), optionally save result
|
||||
- For each `return` step: substitute variables and return string
|
||||
- For each `if` step: evaluate truthiness of variable, branch accordingly
|
||||
- Total implementation: ~200-400 lines of C
|
||||
|
||||
### Error Handling
|
||||
- If any tool step fails (returns `success: false`), abort and return the error
|
||||
- Optional `on_error` field per step for custom error messages
|
||||
- Timeout inherited from tool config
|
||||
|
||||
## 5. Tool Registration for Skill-Tools
|
||||
|
||||
Hardened and guided skills with a `tool_schema` field get registered in the tools array at runtime.
|
||||
|
||||
### At Skill Refresh Time
|
||||
1. Parse `tool_schema` from skill content
|
||||
2. Generate OpenAI function schema from it
|
||||
3. Append to the tools array returned by [`tools_build_openai_schema_json()`](../src/tools.c:919)
|
||||
4. When model calls the skill-tool, route to skill executor instead of hardcoded C function
|
||||
|
||||
### In [`tools_execute()`](../src/tools.c)
|
||||
1. Check if tool_name matches a hardcoded tool — execute normally
|
||||
2. If not, check if it matches a registered skill-tool
|
||||
3. If guided: run sub-LLM call with skill procedure + forced tool_choice
|
||||
4. If hardened: run deterministic step executor
|
||||
|
||||
## 6. Security Model
|
||||
|
||||
### Tool Classification
|
||||
|
||||
Tools are classified for sandbox purposes:
|
||||
|
||||
```c
|
||||
typedef enum {
|
||||
TOOL_SAFETY_SAFE, // read-only or agent-signed actions
|
||||
TOOL_SAFETY_DESTRUCTIVE // filesystem, shell, or external system mutations
|
||||
} tool_safety_t;
|
||||
```
|
||||
|
||||
| Tool | Safety | Reason |
|
||||
|------|--------|--------|
|
||||
| `nostr_query` | safe | Read-only |
|
||||
| `nostr_nip05_lookup` | safe | Read-only |
|
||||
| `nostr_post` | safe | Agent signs, admin controls keys |
|
||||
| `nostr_list_manage` | safe | Agent signs |
|
||||
| `skill_list` | safe | Read-only |
|
||||
| `skill_search` | safe | Read-only |
|
||||
| `skill_run` | safe | Recursive execution uses its own sandbox |
|
||||
| `shell_exec` | destructive | Arbitrary command execution |
|
||||
| `file_read` | destructive | Filesystem access |
|
||||
| `file_write` | destructive | Filesystem mutation |
|
||||
|
||||
### Sandbox Rules
|
||||
|
||||
| Scenario | Default sandbox | Override |
|
||||
|----------|----------------|---------|
|
||||
| Own adopted skill via `/run slug` | No sandbox | N/A |
|
||||
| External skill via `/run address` | Sandbox ON | `/run --unsafe address` |
|
||||
| `skill_run` tool, own skill | No sandbox | `sandbox: true` |
|
||||
| `skill_run` tool, external skill | Sandbox ON | `sandbox: false` |
|
||||
| Hardened skill steps | No sandbox (steps are explicit) | N/A |
|
||||
|
||||
## Future Considerations
|
||||
|
||||
- **Skill sharing:** Hardened skill-tools could be shared between agents via Nostr (kind 31123 events). Another agent adopts the skill and gets the tool automatically.
|
||||
- **Versioning:** Skills already use addressable events (d-tag). Updating a skill automatically updates the tool.
|
||||
- **Permissions:** Skill-tools could have their own permission model (e.g., some skill-tools available to WoT contacts).
|
||||
- **Composability:** Skill-tools calling other skill-tools (nested execution with sandbox inheritance).
|
||||
- **Dry-run mode:** A future `/run --dry` flag that shows what tools would be called without executing them.
|
||||
- **Skill ratings:** Agents could publish ratings/reviews of skills they've tried, building a WoT-based skill marketplace.
|
||||
|
||||
## Implementation Priority
|
||||
|
||||
1. **Slash commands** (direct tool execution) — simplest, highest immediate value
|
||||
2. **`/run` for own adopted skills** — skill-forward execution of already-adopted skills
|
||||
3. **`skill_run` tool + external skill fetching** — enables "try my friend's skill" flow
|
||||
4. **External skill sandbox** — tool safety classification and sandbox enforcement
|
||||
5. **Hardened skill-tool step executor** — enables deterministic workflows
|
||||
6. **Skill-tool registration in tools array** — makes skill-tools visible to LLM
|
||||
7. **Guided execution with forced tool_choice** — bridges draft and hardened
|
||||
8. **Promotion workflow UX** — admin commands to change skill maturity level
|
||||
139
plans/tool_testing.md
Normal file
139
plans/tool_testing.md
Normal file
@@ -0,0 +1,139 @@
|
||||
# Didactyl Tool Testing Plan
|
||||
|
||||
## Overview
|
||||
|
||||
Testing agent-mediated tools is different from unit testing pure functions because the execution path spans multiple boundaries:
|
||||
|
||||
```
|
||||
LLM schema interpretation → JSON argument generation → argument parsing → business logic → Nostr/network I/O → JSON response → LLM interpretation
|
||||
```
|
||||
|
||||
This plan defines three testing layers, ordered by implementation priority.
|
||||
|
||||
---
|
||||
|
||||
## Layer 1: Direct Tool Execution
|
||||
|
||||
Call `tools_execute()` directly with known JSON arguments and assert the JSON response structure.
|
||||
|
||||
### Implementation
|
||||
|
||||
Add a `--test-tool <name> <args_json>` CLI flag to `src/main.c` that:
|
||||
1. Initializes config and nostr_handler (for network-dependent tools)
|
||||
2. Calls `tools_execute(&ctx, name, args_json)`
|
||||
3. Prints the raw JSON result to stdout
|
||||
4. Exits with 0 if `success: true`, 1 otherwise
|
||||
|
||||
### Pure Computation Tools (no network needed)
|
||||
|
||||
| Tool | Test Command | Expected |
|
||||
|------|-------------|----------|
|
||||
| `nostr_encode` | `--test-tool nostr_encode '{"type":"npub","hex":"<64-char-hex>"}'` | `success: true`, uri starts with `nostr:npub1` |
|
||||
| `nostr_decode` | `--test-tool nostr_decode '{"uri":"npub1..."}'` | `success: true`, pubkey is 64-char hex |
|
||||
| `nostr_encrypt` | `--test-tool nostr_encrypt '{"recipient_pubkey":"<hex>","plaintext":"hello"}'` | `success: true`, ciphertext is base64 |
|
||||
| `nostr_decrypt` | `--test-tool nostr_decrypt '{"sender_pubkey":"<hex>","ciphertext":"<from encrypt>"}'` | `success: true`, plaintext is "hello" |
|
||||
|
||||
### Network-Dependent Tools (need relay or HTTP)
|
||||
|
||||
| Tool | Test Command | Expected |
|
||||
|------|-------------|----------|
|
||||
| `nostr_nip05_lookup` | `--test-tool nostr_nip05_lookup '{"identifier":"_@laantungir.com"}'` | `success: true`, pubkey returned |
|
||||
| `nostr_relay_info` | `--test-tool nostr_relay_info '{"relay_url":"wss://relay.damus.io"}'` | `success: true`, info.basic.name present |
|
||||
| `nostr_relay_status` | `--test-tool nostr_relay_status '{}'` | `success: true`, relay_count > 0 |
|
||||
| `nostr_dm_send` | `--test-tool nostr_dm_send '{"recipient_pubkey":"<hex>","message":"test"}'` | `success: true` |
|
||||
| `nostr_post` | `--test-tool nostr_post '{"kind":1,"content":"test"}'` | `success: true`, event_id present |
|
||||
| `nostr_delete` | `--test-tool nostr_delete '{"event_ids":["<64-char-hex>"]}'` | `success: true` |
|
||||
| `nostr_react` | `--test-tool nostr_react '{"event_id":"<hex>","event_pubkey":"<hex>"}'` | `success: true` |
|
||||
| `nostr_profile_get` | `--test-tool nostr_profile_get '{"pubkey":"<hex>"}'` | `success: true`, found: true/false |
|
||||
| `nostr_dm_send_nip17` | `--test-tool nostr_dm_send_nip17 '{"recipient_pubkey":"<hex>","message":"test"}'` | `success: true` |
|
||||
| `nostr_list_manage` | `--test-tool nostr_list_manage '{"list_kind":10000,"action":"add","items":[["p","<hex>"]]}'` | `success: true` |
|
||||
|
||||
### Error Case Tests
|
||||
|
||||
Each tool should also be tested with:
|
||||
- Empty args: `'{}'` — should return descriptive error
|
||||
- Missing required fields — should return specific error message
|
||||
- Invalid hex strings — should reject gracefully
|
||||
- Double-encoded JSON string args — should parse correctly (regression for the `invalid arguments JSON` bug)
|
||||
|
||||
---
|
||||
|
||||
## Layer 2: Schema-Parse Fidelity
|
||||
|
||||
Validates that the OpenAI function schema matches what executors actually accept.
|
||||
|
||||
### Implementation
|
||||
|
||||
A Python script (`tests/validate_schemas.py`) that:
|
||||
1. Runs `didactyl_static --dump-schemas` (new flag) to get the tool schema JSON
|
||||
2. For each tool in the schema:
|
||||
- Generates a minimal valid argument payload from `required` + `properties`
|
||||
- Runs `didactyl_static --test-tool <name> '<generated_json>'`
|
||||
- Asserts exit code 0 or expected network error
|
||||
3. Reports mismatches between schema and executor expectations
|
||||
|
||||
### What This Catches
|
||||
|
||||
- Schema says `required: ["hex"]` but executor checks for `"pubkey"` — mismatch
|
||||
- Schema says `type: "integer"` but executor reads it as string
|
||||
- Schema advertises parameters the executor ignores
|
||||
- Missing required parameters in schema that executor demands
|
||||
|
||||
---
|
||||
|
||||
## Layer 3: Agent Integration Testing (Live DM)
|
||||
|
||||
The most natural test — DM the running agent and verify it uses tools correctly.
|
||||
|
||||
### Prerequisites
|
||||
|
||||
- Local relay running at `ws://127.0.0.1:7777`
|
||||
- Didactyl running with valid config
|
||||
- A separate Nostr client (or script) to send/receive DMs
|
||||
|
||||
### Test Prompts
|
||||
|
||||
| # | Tool | DM Prompt | Assert in Response |
|
||||
|---|------|-----------|-------------------|
|
||||
| 1 | `nostr_encode` | "Encode this pubkey as npub: `<64-char hex>`" | Contains `nostr:npub1` |
|
||||
| 2 | `nostr_decode` | "Decode this npub: `npub1...`" | Contains the hex pubkey |
|
||||
| 3 | `nostr_dm_send` | "Send a DM to `<your-own-pubkey>` saying 'hello test'" | Confirms sent; you receive it |
|
||||
| 4 | `nostr_encrypt` | "Encrypt 'secret message' for `<pubkey>`" | Contains base64 ciphertext |
|
||||
| 5 | `nostr_decrypt` | "Decrypt this NIP-44 payload: `<ciphertext from #4>`" | Contains 'secret message' |
|
||||
| 6 | `nostr_nip05_lookup` | "Look up `_@laantungir.com`" | Returns a pubkey |
|
||||
| 7 | `nostr_relay_info` | "Get NIP-11 info for `wss://relay.damus.io`" | Returns relay name and supported NIPs |
|
||||
| 8 | `nostr_relay_status` | "Show me relay connection status" | Lists connected relays with stats |
|
||||
| 9 | `nostr_react` | "React with 🤙 to event `<id>` from `<pubkey>`" | Confirms kind 7 published |
|
||||
| 10 | `nostr_delete` | "Delete event `<id>`" | Confirms kind 5 published |
|
||||
| 11 | `nostr_profile_get` | "Look up the profile for `<pubkey>`" | Returns name/about/picture |
|
||||
| 12 | `nostr_post` | "Post a kind 1 note saying 'tool test'" | Confirms published with event_id |
|
||||
| 13 | `nostr_list_manage` | "Add `<pubkey>` to my mute list" | Confirms kind 10000 published |
|
||||
| 14 | `nostr_dm_send_nip17` | "Send a private NIP-17 DM to `<pubkey>` saying 'gift wrap test'" | Confirms gift wrap sent |
|
||||
| 15 | `nostr_post_readme` | "Publish the README to Nostr" | Confirms kind 30023 with d=readme.md |
|
||||
|
||||
### Verification Methods
|
||||
|
||||
- **stdout logs**: Watch `[didactyl] executing tool call: <name>` in terminal
|
||||
- **context.log**: Full LLM conversation including tool calls and results
|
||||
- **Relay inspection**: Query the local relay for published events
|
||||
- **DM receipt**: For DM tools, verify the message arrives at the recipient
|
||||
|
||||
---
|
||||
|
||||
## Implementation Priority
|
||||
|
||||
1. **Immediate (no code changes)**: Run Layer 3 tests by DMing the live agent
|
||||
2. **Next sprint**: Add `--test-tool` CLI flag for Layer 1
|
||||
3. **Later**: Add `--dump-schemas` flag and Python schema validator for Layer 2
|
||||
4. **CI integration**: Wrap Layer 1 pure-computation tests in a shell script that runs after `build_static.sh`
|
||||
|
||||
---
|
||||
|
||||
## Local Relay Setup for Testing
|
||||
|
||||
A local relay like `strfry` or `nostr-rs-relay` at `ws://127.0.0.1:7777` provides:
|
||||
- All published events are observable and queryable
|
||||
- No rate limits or content policies
|
||||
- NIP-42 auth testing in isolation
|
||||
- Gift-wrapped NIP-17 DMs stay in your test environment
|
||||
- Database can be wiped between test runs
|
||||
203
plans/triggered_skills.md
Normal file
203
plans/triggered_skills.md
Normal file
@@ -0,0 +1,203 @@
|
||||
# Triggered Skills — Implementation Plan
|
||||
|
||||
## Overview
|
||||
|
||||
Extend the existing skill system so that skills can carry Nostr subscription triggers. When matching events arrive, didactyl executes the skill automatically — either via template interpolation (fast, no LLM) or LLM-mediated reasoning (full agent loop).
|
||||
|
||||
See [docs/TOOLS_AND_SKILLS.md](../docs/TOOLS_AND_SKILLS.md) for the full architecture.
|
||||
|
||||
---
|
||||
|
||||
## Implementation Steps
|
||||
|
||||
### 1. Trigger Manager Module
|
||||
|
||||
Create `src/trigger_manager.c` and `src/trigger_manager.h` — the core component that manages dynamic Nostr subscriptions tied to skills.
|
||||
|
||||
**Data structures:**
|
||||
|
||||
```c
|
||||
#define TRIGGER_MAX_ACTIVE 16
|
||||
#define TRIGGER_COOLDOWN_SECONDS 60
|
||||
|
||||
typedef struct {
|
||||
char skill_slug[65];
|
||||
char skill_content[4096]; // the action template or LLM prompt
|
||||
int action_type; // 0 = llm, 1 = template
|
||||
char filter_json[2048]; // the Nostr subscription filter
|
||||
int enabled;
|
||||
time_t last_fired;
|
||||
nostr_pool_subscription_t* subscription;
|
||||
} active_trigger_t;
|
||||
|
||||
typedef struct {
|
||||
active_trigger_t triggers[TRIGGER_MAX_ACTIVE];
|
||||
int count;
|
||||
didactyl_config_t* cfg;
|
||||
pthread_mutex_t mutex;
|
||||
} trigger_manager_t;
|
||||
```
|
||||
|
||||
**API:**
|
||||
|
||||
```c
|
||||
int trigger_manager_init(trigger_manager_t* mgr, didactyl_config_t* cfg);
|
||||
int trigger_manager_load_from_skills(trigger_manager_t* mgr);
|
||||
int trigger_manager_add(trigger_manager_t* mgr, const char* skill_slug,
|
||||
const char* content, const char* filter_json,
|
||||
int action_type, int enabled);
|
||||
int trigger_manager_remove(trigger_manager_t* mgr, const char* skill_slug);
|
||||
int trigger_manager_update(trigger_manager_t* mgr, const char* skill_slug,
|
||||
const char* content, const char* filter_json,
|
||||
int action_type, int enabled);
|
||||
int trigger_manager_active_count(trigger_manager_t* mgr);
|
||||
char* trigger_manager_status_json(trigger_manager_t* mgr);
|
||||
void trigger_manager_cleanup(trigger_manager_t* mgr);
|
||||
```
|
||||
|
||||
### 2. Template Engine
|
||||
|
||||
Create a simple template interpolation engine in `src/trigger_manager.c` (or a separate `src/template.c` if it grows).
|
||||
|
||||
**Functionality:**
|
||||
- Parse placeholders like `{content}`, `{pubkey}`, `{author_display_name}` from a template string
|
||||
- Extract values from a triggering Nostr event (cJSON object)
|
||||
- Produce an interpolated output string
|
||||
- Parse action prefixes: `DM admin:`, `DM <pubkey>:`, `POST:`, `LOG:`
|
||||
|
||||
### 3. Trigger Event Callback
|
||||
|
||||
When a subscribed event arrives:
|
||||
|
||||
```c
|
||||
static void on_trigger_event(cJSON* event, const char* relay_url, void* user_data) {
|
||||
active_trigger_t* trigger = (active_trigger_t*)user_data;
|
||||
|
||||
// Check cooldown
|
||||
if (time(NULL) - trigger->last_fired < TRIGGER_COOLDOWN_SECONDS) return;
|
||||
trigger->last_fired = time(NULL);
|
||||
|
||||
if (trigger->action_type == 1) {
|
||||
// Template: interpolate and execute
|
||||
char* output = template_interpolate(trigger->skill_content, event);
|
||||
template_execute_action(output); // parse prefix, DM/POST/LOG
|
||||
free(output);
|
||||
} else {
|
||||
// LLM: build context and run agent loop
|
||||
trigger_run_llm_action(trigger, event, relay_url);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 4. Skill Loading on Startup
|
||||
|
||||
In `main.c`, after `agent_init()` and skill adoption list is available:
|
||||
|
||||
1. Query own kind 10123 adoption list
|
||||
2. For each adopted skill address, query the skill event
|
||||
3. Check for `trigger` tag — if present, extract `filter`, `action`, `enabled` tags
|
||||
4. Register with trigger manager
|
||||
5. Trigger manager creates Nostr subscriptions
|
||||
|
||||
### 5. Extend skill_create for Live Trigger Registration
|
||||
|
||||
When `skill_create` is called with trigger tags:
|
||||
1. Publish the skill event as normal
|
||||
2. If trigger tags are present, also register with the trigger manager immediately
|
||||
3. No restart required — the subscription goes live right away
|
||||
|
||||
When `skill_remove` is called for a triggered skill:
|
||||
1. Remove from adoption list as normal
|
||||
2. Also unregister from trigger manager, tearing down the subscription
|
||||
|
||||
### 6. Extend Agent for Trigger-Initiated Conversations
|
||||
|
||||
The agent currently only handles DM-initiated conversations via `agent_on_message()`. Add a new entry point:
|
||||
|
||||
```c
|
||||
void agent_on_trigger(const char* skill_slug,
|
||||
const char* skill_content,
|
||||
cJSON* triggering_event,
|
||||
const char* relay_url);
|
||||
```
|
||||
|
||||
This builds an LLM conversation with:
|
||||
- System context (soul)
|
||||
- A system message explaining this is a triggered skill execution
|
||||
- The skill content as instructions
|
||||
- The triggering event as user context
|
||||
- Full tool access (same as admin tier)
|
||||
|
||||
The LLM response actions (DMs, posts, etc.) are executed via tools as normal.
|
||||
|
||||
### 7. New Tool: trigger_list
|
||||
|
||||
Add a tool so the LLM can inspect active triggers:
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "trigger_list",
|
||||
"description": "List all active triggered skills with their filters and status",
|
||||
"parameters": { "type": "object", "properties": {} }
|
||||
}
|
||||
```
|
||||
|
||||
### 8. Config Extension
|
||||
|
||||
Add trigger-related limits to config:
|
||||
|
||||
```json
|
||||
{
|
||||
"triggers": {
|
||||
"enabled": true,
|
||||
"max_active": 16,
|
||||
"cooldown_seconds": 60,
|
||||
"llm_rate_limit_per_minute": 10,
|
||||
"template_rate_limit_per_minute": 60
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 9. Integration into Main Loop
|
||||
|
||||
The trigger manager subscriptions are serviced by the same `nostr_handler_poll()` call in the main loop — no changes needed to the poll loop itself, since all subscriptions share the relay pool.
|
||||
|
||||
---
|
||||
|
||||
## File Changes Summary
|
||||
|
||||
| File | Change |
|
||||
|---|---|
|
||||
| `src/trigger_manager.c` | **NEW** — trigger manager, template engine, event callbacks |
|
||||
| `src/trigger_manager.h` | **NEW** — trigger manager API |
|
||||
| `src/main.c` | Add trigger_manager_init, trigger_manager_load_from_skills after agent_init |
|
||||
| `src/agent.c` | Add agent_on_trigger entry point for LLM-mediated trigger actions |
|
||||
| `src/agent.h` | Declare agent_on_trigger |
|
||||
| `src/tools.c` | Extend skill_create/skill_remove to register/unregister triggers; add trigger_list tool |
|
||||
| `src/config.h` | Add triggers_config_t struct |
|
||||
| `src/config.c` | Parse triggers config section |
|
||||
| `docs/TOOLS_AND_SKILLS.md` | Already written — full architecture reference |
|
||||
|
||||
---
|
||||
|
||||
## Implementation Order
|
||||
|
||||
1. `trigger_manager.h` / `trigger_manager.c` — core module with data structures and API stubs
|
||||
2. Template engine — interpolation and action prefix parsing
|
||||
3. Trigger event callback — on_trigger_event with cooldown
|
||||
4. Startup loading — query adoption list, find triggered skills, create subscriptions
|
||||
5. agent_on_trigger — LLM-mediated trigger execution path
|
||||
6. Live registration — extend skill_create/skill_remove for immediate trigger management
|
||||
7. trigger_list tool — LLM visibility into active triggers
|
||||
8. Config parsing — triggers section with limits
|
||||
9. Rate limiting — enforce LLM and template rate limits
|
||||
10. Testing — manual trigger creation and verification
|
||||
|
||||
---
|
||||
|
||||
## Dependencies
|
||||
|
||||
- Existing `nostr_handler_query_json()` for loading skills
|
||||
- Existing `nostr_relay_pool_subscribe()` for creating trigger subscriptions
|
||||
- Existing `agent_on_message()` pattern for the LLM-mediated path
|
||||
- Existing `skill_create` / `skill_remove` tools for lifecycle hooks
|
||||
158
plans/unified_prompt_context.md
Normal file
158
plans/unified_prompt_context.md
Normal file
@@ -0,0 +1,158 @@
|
||||
# Plan: Unified Prompt Context for HTTP API and Nostr Paths
|
||||
|
||||
## Problem
|
||||
|
||||
The agent produces completely different LLM context depending on whether a message arrives via **Nostr DM** or the **HTTP API CLI chat app**.
|
||||
|
||||
### Nostr Path (working correctly)
|
||||
- `agent_on_message()` → `agent_build_admin_messages_json()` → tool loop
|
||||
- Builds **18 sections, ~8224 bytes** of context including:
|
||||
- System prompt / personality (from soul template)
|
||||
- Agent identity (pubkey)
|
||||
- Sender verification (admin tier)
|
||||
- Admin context (kind 0 profile, relay list, recent posts)
|
||||
- Startup events memory
|
||||
- Adopted skills
|
||||
- DM history (decrypted from Nostr relays)
|
||||
- Current user message
|
||||
|
||||
### HTTP API Path (broken)
|
||||
- CLI sends `{messages: [{role: "user", content: "Hello"}]}` to `/api/prompt/run`
|
||||
- `run_prompt_with_tools()` passes these raw messages directly to the LLM
|
||||
- Result: **1 section, ~35 bytes** — just the bare user message, zero agent context
|
||||
|
||||
## Solution: New `POST /api/prompt/agent` Endpoint
|
||||
|
||||
Add a new endpoint that mirrors the Nostr path's context assembly, so the CLI gets the same full agent context.
|
||||
|
||||
### Architecture
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
A[Nostr DM arrives] --> B[agent_on_message]
|
||||
B --> C[agent_build_admin_messages_json]
|
||||
C --> D[Append user message]
|
||||
D --> E[Tool loop with llm_chat_with_tools_messages]
|
||||
E --> F[Send DM reply]
|
||||
|
||||
G[CLI sends POST /api/prompt/agent] --> H[handle_prompt_agent]
|
||||
H --> C
|
||||
C --> I[Append user message]
|
||||
I --> J[Tool loop - same as run_prompt_with_tools but with context]
|
||||
J --> K[Return JSON response]
|
||||
|
||||
style C fill:#4a9,stroke:#333,color:#fff
|
||||
```
|
||||
|
||||
Both paths share `agent_build_admin_messages_json()` as the single source of truth for context assembly.
|
||||
|
||||
### Request Format
|
||||
|
||||
```json
|
||||
{
|
||||
"message": "What is the capital of France?",
|
||||
"model": "claude-haiku-4.5",
|
||||
"max_turns": 4
|
||||
}
|
||||
```
|
||||
|
||||
| Field | Type | Required | Description |
|
||||
|---|---|---|---|
|
||||
| `message` | string | yes | The user message to send to the agent |
|
||||
| `model` | string | no | Override the configured LLM model for this request |
|
||||
| `max_turns` | int | no | Max tool-use turns, default 4, max 16 |
|
||||
|
||||
### Response Format
|
||||
|
||||
Same as existing `/api/prompt/run`:
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"final_response": "The capital of France is Paris.",
|
||||
"turns": [...],
|
||||
"model_used": "claude-haiku-4.5",
|
||||
"total_input_tokens_estimate": 1973,
|
||||
"total_output_tokens_estimate": 12
|
||||
}
|
||||
```
|
||||
|
||||
## Implementation Steps
|
||||
|
||||
### 1. Add `handle_prompt_agent()` in `src/http_api.c`
|
||||
|
||||
New function that:
|
||||
1. Parses the JSON body to extract `message`, optional `model`, optional `max_turns`
|
||||
2. Applies model override if present via `maybe_model_override_begin()`
|
||||
3. Calls `agent_build_admin_messages_json(message, DIDACTYL_SENDER_ADMIN, &base_messages_json)` — same call the Nostr path uses
|
||||
4. Parses the result into a cJSON array
|
||||
5. Appends `{role: "user", content: message}` to the array — same as `agent_on_message()` does at line 1916
|
||||
6. Builds tool schema via `tools_build_openai_schema_json()`
|
||||
7. Runs the same tool loop as `run_prompt_with_tools()` but using the context-enriched messages
|
||||
8. Logs context via `agent_append_context_log("http_api_agent", "llm_chat_with_tools_messages", messages_json)`
|
||||
9. Returns the same response format as `/api/prompt/run`
|
||||
|
||||
Key reference points in existing code:
|
||||
- Context building: `agent_build_admin_messages_json()` at `src/agent.c:1737`
|
||||
- User message append: `append_simple_message()` pattern at `src/agent.c:1916`
|
||||
- Tool loop: reuse the loop logic from `run_prompt_with_tools()` at `src/http_api.c:278-345`
|
||||
- Context logging: `agent_append_context_log()` at `src/agent.c:1932`
|
||||
|
||||
### 2. Register the Route in `http_handler()`
|
||||
|
||||
Add before the existing `/api/prompt/run` route at `src/http_api.c:648`:
|
||||
|
||||
```c
|
||||
if (method_is(hm, "POST") && mg_match(hm->uri, mg_str("/api/prompt/agent"), NULL)) {
|
||||
handle_prompt_agent(c, hm);
|
||||
return;
|
||||
}
|
||||
```
|
||||
|
||||
### 3. Update `chat-didactyl-cli.js`
|
||||
|
||||
Change the CLI to call the new endpoint:
|
||||
|
||||
- Change `callDidactyl()` to POST to `/api/prompt/agent` instead of `/api/prompt/run`
|
||||
- Send `{message: "user text", max_turns: N}` instead of `{messages: [...], max_turns: N}`
|
||||
- The CLI no longer needs to maintain a `transcript` array for context — the server handles DM history from Nostr relays
|
||||
- Keep the transcript for local display purposes only
|
||||
|
||||
### 4. Context Logging Parity
|
||||
|
||||
Use a distinct but parallel phase label:
|
||||
- Nostr path: `llm_chat_with_tools_messages` (existing)
|
||||
- HTTP API agent path: `llm_chat_with_tools_messages_agent_api` (new)
|
||||
- HTTP API raw path: `llm_chat_with_tools_messages_http_api` (existing, unchanged)
|
||||
|
||||
This lets you distinguish the source in `context.log.md` while confirming the context structure is identical.
|
||||
|
||||
### 5. Update `docs/API.md`
|
||||
|
||||
Add documentation for the new `POST /api/prompt/agent` endpoint following the existing documentation style.
|
||||
|
||||
## Files to Modify
|
||||
|
||||
| File | Change |
|
||||
|---|---|
|
||||
| `src/http_api.c` | Add `handle_prompt_agent()` function and route registration |
|
||||
| `chat-didactyl-cli.js` | Switch to `/api/prompt/agent`, simplify payload |
|
||||
| `docs/API.md` | Document new endpoint |
|
||||
|
||||
## What Stays the Same
|
||||
|
||||
- `/api/prompt/run` — unchanged, still accepts raw message arrays for custom/advanced use
|
||||
- `/api/prompt/run-simple` — unchanged
|
||||
- `/api/context/current` and `/api/context/parts` — unchanged
|
||||
- `agent_build_admin_messages_json()` — unchanged, already does exactly what we need
|
||||
- Nostr message handling — unchanged
|
||||
|
||||
## Remaining Consideration: Conversation History
|
||||
|
||||
The Nostr path gets DM history by querying encrypted kind-4 events from relays. The new `/api/prompt/agent` endpoint will include this same history since it calls `agent_build_admin_messages_json()`. This means:
|
||||
|
||||
- Messages sent via the CLI will NOT appear in the Nostr DM history (they are not published as Nostr events)
|
||||
- Messages sent via Nostr WILL appear in the context when using the CLI
|
||||
- This is acceptable — the CLI is a development/admin tool that piggybacks on the agent's full context
|
||||
|
||||
If in the future you want CLI messages to also appear in history, that would require either publishing them as Nostr DMs or maintaining a separate local history store — but that is out of scope for this change.
|
||||
2583
src/agent.c
2583
src/agent.c
File diff suppressed because it is too large
Load Diff
21
src/agent.h
21
src/agent.h
@@ -2,9 +2,28 @@
|
||||
#define OPEN_WING_AGENT_H
|
||||
|
||||
#include "config.h"
|
||||
#include "nostr_handler.h"
|
||||
#include "cjson/cJSON.h"
|
||||
#include "tools.h"
|
||||
|
||||
struct trigger_manager;
|
||||
|
||||
int agent_init(didactyl_config_t* config, const char* system_context);
|
||||
void agent_on_message(const char* sender_pubkey_hex, const char* message, void* user_data);
|
||||
void agent_set_trigger_manager(struct trigger_manager* trigger_manager);
|
||||
void agent_on_trigger(const char* skill_slug,
|
||||
const char* skill_content,
|
||||
cJSON* triggering_event,
|
||||
const char* relay_url);
|
||||
void agent_on_message(const char* sender_pubkey_hex,
|
||||
const char* message,
|
||||
didactyl_sender_tier_t tier,
|
||||
void* user_data);
|
||||
int agent_build_admin_messages_json(const char* current_user_message,
|
||||
didactyl_sender_tier_t sender_tier,
|
||||
char** out_messages_json);
|
||||
tools_context_t* agent_tools_context(void);
|
||||
const char* agent_classify_message_part(cJSON* msg, int idx);
|
||||
void agent_append_context_log(const char* sender_pubkey_hex, const char* phase, const char* context_payload);
|
||||
void agent_cleanup(void);
|
||||
|
||||
#endif
|
||||
585
src/config.c
585
src/config.c
@@ -3,6 +3,8 @@
|
||||
#include "config.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -10,6 +12,19 @@
|
||||
#include "cjson/cJSON.h"
|
||||
#include "../../nostr_core_lib/nostr_core/nostr_core.h"
|
||||
|
||||
static char g_config_last_error[512] = {0};
|
||||
|
||||
static void config_set_error(const char* fmt, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(g_config_last_error, sizeof(g_config_last_error), fmt ? fmt : "unknown configuration error", ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
const char* config_last_error(void) {
|
||||
return g_config_last_error[0] != '\0' ? g_config_last_error : "unknown configuration error";
|
||||
}
|
||||
|
||||
static int read_file_to_buffer(const char* path, char** out_buf, size_t* out_len) {
|
||||
FILE* fp = fopen(path, "rb");
|
||||
if (!fp) {
|
||||
@@ -161,39 +176,461 @@ static int parse_tools_config(cJSON* root, didactyl_config_t* config) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int parse_relays(cJSON* root, didactyl_config_t* config) {
|
||||
cJSON* relays = cJSON_GetObjectItemCaseSensitive(root, "relays");
|
||||
if (!relays || !cJSON_IsArray(relays)) {
|
||||
static int parse_security_config(cJSON* root, didactyl_config_t* config) {
|
||||
cJSON* security = cJSON_GetObjectItemCaseSensitive(root, "security");
|
||||
if (!security || !cJSON_IsObject(security)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
cJSON* verify_signatures = cJSON_GetObjectItemCaseSensitive(security, "verify_signatures");
|
||||
if (verify_signatures && cJSON_IsBool(verify_signatures)) {
|
||||
config->security.verify_signatures = cJSON_IsTrue(verify_signatures) ? 1 : 0;
|
||||
}
|
||||
|
||||
if (copy_json_string(security,
|
||||
"stranger_response",
|
||||
config->security.stranger_response,
|
||||
sizeof(config->security.stranger_response),
|
||||
0) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int count = cJSON_GetArraySize(relays);
|
||||
if (count <= 0) {
|
||||
return -1;
|
||||
cJSON* tiers = cJSON_GetObjectItemCaseSensitive(security, "tiers");
|
||||
if (!tiers || !cJSON_IsObject(tiers)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
config->relays = (char**)calloc((size_t)count, sizeof(char*));
|
||||
if (!config->relays) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
config->relay_count = count;
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
cJSON* relay = cJSON_GetArrayItem(relays, i);
|
||||
if (!relay || !cJSON_IsString(relay) || !relay->valuestring || relay->valuestring[0] == '\0') {
|
||||
return -1;
|
||||
cJSON* admin_tier = cJSON_GetObjectItemCaseSensitive(tiers, "admin");
|
||||
if (admin_tier && cJSON_IsObject(admin_tier)) {
|
||||
cJSON* tools_enabled = cJSON_GetObjectItemCaseSensitive(admin_tier, "tools_enabled");
|
||||
if (tools_enabled && cJSON_IsBool(tools_enabled)) {
|
||||
config->security.admin.tools_enabled = cJSON_IsTrue(tools_enabled) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
config->relays[i] = strdup(relay->valuestring);
|
||||
if (!config->relays[i]) {
|
||||
return -1;
|
||||
cJSON* wot_tier = cJSON_GetObjectItemCaseSensitive(tiers, "wot");
|
||||
if (wot_tier && cJSON_IsObject(wot_tier)) {
|
||||
cJSON* enabled = cJSON_GetObjectItemCaseSensitive(wot_tier, "enabled");
|
||||
cJSON* tools_enabled = cJSON_GetObjectItemCaseSensitive(wot_tier, "tools_enabled");
|
||||
if (enabled && cJSON_IsBool(enabled)) {
|
||||
config->security.wot.enabled = cJSON_IsTrue(enabled) ? 1 : 0;
|
||||
}
|
||||
if (tools_enabled && cJSON_IsBool(tools_enabled)) {
|
||||
config->security.wot.tools_enabled = cJSON_IsTrue(tools_enabled) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
cJSON* stranger_tier = cJSON_GetObjectItemCaseSensitive(tiers, "stranger");
|
||||
if (stranger_tier && cJSON_IsObject(stranger_tier)) {
|
||||
cJSON* enabled = cJSON_GetObjectItemCaseSensitive(stranger_tier, "enabled");
|
||||
if (enabled && cJSON_IsBool(enabled)) {
|
||||
config->security.stranger.enabled = cJSON_IsTrue(enabled) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int parse_admin_context_config(cJSON* root, didactyl_config_t* config) {
|
||||
cJSON* admin_context = cJSON_GetObjectItemCaseSensitive(root, "admin_context");
|
||||
if (!admin_context || !cJSON_IsObject(admin_context)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
cJSON* enabled = cJSON_GetObjectItemCaseSensitive(admin_context, "enabled");
|
||||
if (enabled && cJSON_IsBool(enabled)) {
|
||||
config->admin_context.enabled = cJSON_IsTrue(enabled) ? 1 : 0;
|
||||
}
|
||||
|
||||
cJSON* kind_1_limit = cJSON_GetObjectItemCaseSensitive(admin_context, "kind_1_limit");
|
||||
if (kind_1_limit && cJSON_IsNumber(kind_1_limit)) {
|
||||
config->admin_context.kind_1_limit = (int)kind_1_limit->valuedouble;
|
||||
}
|
||||
|
||||
cJSON* subscribe_kinds = cJSON_GetObjectItemCaseSensitive(admin_context, "subscribe_kinds");
|
||||
if (subscribe_kinds && cJSON_IsArray(subscribe_kinds)) {
|
||||
config->admin_context.track_kind_0 = 0;
|
||||
config->admin_context.track_kind_3 = 0;
|
||||
config->admin_context.track_kind_10002 = 0;
|
||||
config->admin_context.track_kind_1 = 0;
|
||||
|
||||
int n = cJSON_GetArraySize(subscribe_kinds);
|
||||
for (int i = 0; i < n; i++) {
|
||||
cJSON* k = cJSON_GetArrayItem(subscribe_kinds, i);
|
||||
if (!k || !cJSON_IsNumber(k)) {
|
||||
continue;
|
||||
}
|
||||
int kind = (int)k->valuedouble;
|
||||
if (kind == 0) config->admin_context.track_kind_0 = 1;
|
||||
else if (kind == 3) config->admin_context.track_kind_3 = 1;
|
||||
else if (kind == 10002) config->admin_context.track_kind_10002 = 1;
|
||||
else if (kind == 1) config->admin_context.track_kind_1 = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int parse_triggers_config(cJSON* root, didactyl_config_t* config) {
|
||||
cJSON* triggers = cJSON_GetObjectItemCaseSensitive(root, "triggers");
|
||||
if (!triggers || !cJSON_IsObject(triggers)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
cJSON* enabled = cJSON_GetObjectItemCaseSensitive(triggers, "enabled");
|
||||
cJSON* max_active = cJSON_GetObjectItemCaseSensitive(triggers, "max_active");
|
||||
cJSON* cooldown_seconds = cJSON_GetObjectItemCaseSensitive(triggers, "cooldown_seconds");
|
||||
cJSON* llm_rate_limit = cJSON_GetObjectItemCaseSensitive(triggers, "llm_rate_limit_per_minute");
|
||||
cJSON* template_rate_limit = cJSON_GetObjectItemCaseSensitive(triggers, "template_rate_limit_per_minute");
|
||||
|
||||
if (enabled && cJSON_IsBool(enabled)) {
|
||||
config->triggers.enabled = cJSON_IsTrue(enabled) ? 1 : 0;
|
||||
}
|
||||
if (max_active && cJSON_IsNumber(max_active)) {
|
||||
config->triggers.max_active = (int)max_active->valuedouble;
|
||||
}
|
||||
if (cooldown_seconds && cJSON_IsNumber(cooldown_seconds)) {
|
||||
config->triggers.cooldown_seconds = (int)cooldown_seconds->valuedouble;
|
||||
}
|
||||
if (llm_rate_limit && cJSON_IsNumber(llm_rate_limit)) {
|
||||
config->triggers.llm_rate_limit_per_minute = (int)llm_rate_limit->valuedouble;
|
||||
}
|
||||
if (template_rate_limit && cJSON_IsNumber(template_rate_limit)) {
|
||||
config->triggers.template_rate_limit_per_minute = (int)template_rate_limit->valuedouble;
|
||||
}
|
||||
|
||||
if (config->triggers.max_active < 1) {
|
||||
config->triggers.max_active = 1;
|
||||
}
|
||||
if (config->triggers.cooldown_seconds < 0) {
|
||||
config->triggers.cooldown_seconds = 0;
|
||||
}
|
||||
if (config->triggers.llm_rate_limit_per_minute < 1) {
|
||||
config->triggers.llm_rate_limit_per_minute = 1;
|
||||
}
|
||||
if (config->triggers.template_rate_limit_per_minute < 1) {
|
||||
config->triggers.template_rate_limit_per_minute = 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int parse_dm_protocol_config(cJSON* root, didactyl_config_t* config) {
|
||||
if (!root || !config) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
cJSON* dm_protocol = cJSON_GetObjectItemCaseSensitive(root, "dm_protocol");
|
||||
if (!dm_protocol) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!cJSON_IsString(dm_protocol) || !dm_protocol->valuestring) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (strcmp(dm_protocol->valuestring, "nip04") == 0) {
|
||||
config->dm_protocol = DM_PROTOCOL_NIP04;
|
||||
} else if (strcmp(dm_protocol->valuestring, "nip17") == 0) {
|
||||
config->dm_protocol = DM_PROTOCOL_NIP17;
|
||||
} else if (strcmp(dm_protocol->valuestring, "both") == 0) {
|
||||
config->dm_protocol = DM_PROTOCOL_BOTH;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int parse_api_config(cJSON* root, didactyl_config_t* config) {
|
||||
cJSON* api = cJSON_GetObjectItemCaseSensitive(root, "api");
|
||||
if (!api || !cJSON_IsObject(api)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
cJSON* enabled = cJSON_GetObjectItemCaseSensitive(api, "enabled");
|
||||
cJSON* port = cJSON_GetObjectItemCaseSensitive(api, "port");
|
||||
|
||||
if (enabled && cJSON_IsBool(enabled)) {
|
||||
config->api.enabled = cJSON_IsTrue(enabled) ? 1 : 0;
|
||||
}
|
||||
if (port && cJSON_IsNumber(port)) {
|
||||
config->api.port = (int)port->valuedouble;
|
||||
}
|
||||
|
||||
if (copy_json_string(api,
|
||||
"bind_address",
|
||||
config->api.bind_address,
|
||||
sizeof(config->api.bind_address),
|
||||
0) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (config->api.port < 1 || config->api.port > 65535) {
|
||||
config->api.port = 8484;
|
||||
}
|
||||
if (config->api.bind_address[0] == '\0') {
|
||||
snprintf(config->api.bind_address, sizeof(config->api.bind_address), "%s", "127.0.0.1");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static cJSON* find_tag_value_string(cJSON* tags, const char* tag_key) {
|
||||
if (!tags || !cJSON_IsArray(tags) || !tag_key) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int n = cJSON_GetArraySize(tags);
|
||||
for (int i = 0; i < n; i++) {
|
||||
cJSON* tag = cJSON_GetArrayItem(tags, i);
|
||||
if (!tag || !cJSON_IsArray(tag) || cJSON_GetArraySize(tag) < 2) {
|
||||
continue;
|
||||
}
|
||||
|
||||
cJSON* key = cJSON_GetArrayItem(tag, 0);
|
||||
cJSON* val = cJSON_GetArrayItem(tag, 1);
|
||||
if (!key || !val || !cJSON_IsString(key) || !cJSON_IsString(val) || !key->valuestring || !val->valuestring) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strcmp(key->valuestring, tag_key) == 0) {
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int set_tag_value_string(cJSON* tags, const char* tag_key, const char* tag_value) {
|
||||
if (!tags || !cJSON_IsArray(tags) || !tag_key || !tag_value || tag_value[0] == '\0') {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int n = cJSON_GetArraySize(tags);
|
||||
for (int i = 0; i < n; i++) {
|
||||
cJSON* tag = cJSON_GetArrayItem(tags, i);
|
||||
if (!tag || !cJSON_IsArray(tag) || cJSON_GetArraySize(tag) < 2) {
|
||||
continue;
|
||||
}
|
||||
|
||||
cJSON* key = cJSON_GetArrayItem(tag, 0);
|
||||
cJSON* val = cJSON_GetArrayItem(tag, 1);
|
||||
if (!key || !val || !cJSON_IsString(key) || !key->valuestring) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strcmp(key->valuestring, tag_key) == 0) {
|
||||
if (cJSON_IsString(val)) {
|
||||
if (!cJSON_SetValuestring(val, tag_value)) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
cJSON* new_val = cJSON_CreateString(tag_value);
|
||||
if (!new_val) {
|
||||
return -1;
|
||||
}
|
||||
cJSON_ReplaceItemInArray(tag, 1, new_val);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
cJSON* new_tag = cJSON_CreateArray();
|
||||
if (!new_tag) {
|
||||
return -1;
|
||||
}
|
||||
cJSON_AddItemToArray(new_tag, cJSON_CreateString(tag_key));
|
||||
cJSON_AddItemToArray(new_tag, cJSON_CreateString(tag_value));
|
||||
cJSON_AddItemToArray(tags, new_tag);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int normalize_skill_d_tag(int event_kind, cJSON* item, cJSON* tags) {
|
||||
if (!item || !tags || !cJSON_IsArray(tags)) {
|
||||
return 0;
|
||||
}
|
||||
if (event_kind != 31123 && event_kind != 31124) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
cJSON* d_val = find_tag_value_string(tags, "d");
|
||||
if (!d_val || !cJSON_IsString(d_val) || !d_val->valuestring) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int needs_normalize =
|
||||
(strcmp(d_val->valuestring, "skill") == 0 || strcmp(d_val->valuestring, "private_skill") == 0);
|
||||
if (!needs_normalize) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char* slug = NULL;
|
||||
cJSON* slug_val = find_tag_value_string(tags, "slug");
|
||||
if (slug_val && cJSON_IsString(slug_val) && slug_val->valuestring && slug_val->valuestring[0] != '\0') {
|
||||
slug = slug_val->valuestring;
|
||||
}
|
||||
|
||||
if (!slug) {
|
||||
cJSON* content_fields = cJSON_GetObjectItemCaseSensitive(item, "content_fields");
|
||||
if (content_fields && cJSON_IsObject(content_fields)) {
|
||||
cJSON* name = cJSON_GetObjectItemCaseSensitive(content_fields, "name");
|
||||
if (name && cJSON_IsString(name) && name->valuestring && name->valuestring[0] != '\0') {
|
||||
slug = name->valuestring;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!slug) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return set_tag_value_string(tags, "d", slug);
|
||||
}
|
||||
|
||||
static int parse_startup_events(cJSON* root, didactyl_config_t* config) {
|
||||
cJSON* arr = cJSON_GetObjectItemCaseSensitive(root, "startup_events");
|
||||
if (!arr || !cJSON_IsArray(arr)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int count = cJSON_GetArraySize(arr);
|
||||
if (count <= 0) return 0;
|
||||
|
||||
config->startup_events = (startup_event_t*)calloc((size_t)count, sizeof(startup_event_t));
|
||||
if (!config->startup_events) return -1;
|
||||
config->startup_event_count = count;
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
cJSON* item = cJSON_GetArrayItem(arr, i);
|
||||
if (!item || !cJSON_IsObject(item)) return -1;
|
||||
|
||||
cJSON* kind = cJSON_GetObjectItemCaseSensitive(item, "kind");
|
||||
cJSON* content = cJSON_GetObjectItemCaseSensitive(item, "content");
|
||||
cJSON* content_fields = cJSON_GetObjectItemCaseSensitive(item, "content_fields");
|
||||
cJSON* tags = cJSON_GetObjectItemCaseSensitive(item, "tags");
|
||||
|
||||
if (!kind || !cJSON_IsNumber(kind)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
config->startup_events[i].kind = (int)kind->valuedouble;
|
||||
|
||||
if (content_fields) {
|
||||
if (!cJSON_IsObject(content_fields)) {
|
||||
return -1;
|
||||
}
|
||||
config->startup_events[i].content = cJSON_PrintUnformatted(content_fields);
|
||||
if (!config->startup_events[i].content) {
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
if (!content || !cJSON_IsString(content) || !content->valuestring) {
|
||||
return -1;
|
||||
}
|
||||
config->startup_events[i].content = strdup(content->valuestring);
|
||||
if (!config->startup_events[i].content) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (tags) {
|
||||
if (!cJSON_IsArray(tags)) return -1;
|
||||
if (normalize_skill_d_tag(config->startup_events[i].kind, item, tags) != 0) {
|
||||
return -1;
|
||||
}
|
||||
config->startup_events[i].tags_json = cJSON_PrintUnformatted(tags);
|
||||
if (!config->startup_events[i].tags_json) return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int parse_relays_from_startup_kind10002(didactyl_config_t* config) {
|
||||
if (!config || !config->startup_events || config->startup_event_count <= 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < config->startup_event_count; i++) {
|
||||
startup_event_t* se = &config->startup_events[i];
|
||||
if (se->kind != 10002 || !se->tags_json) {
|
||||
continue;
|
||||
}
|
||||
|
||||
cJSON* tags = cJSON_Parse(se->tags_json);
|
||||
if (!tags || !cJSON_IsArray(tags)) {
|
||||
cJSON_Delete(tags);
|
||||
continue;
|
||||
}
|
||||
|
||||
int tag_count = cJSON_GetArraySize(tags);
|
||||
int relay_count = 0;
|
||||
for (int j = 0; j < tag_count; j++) {
|
||||
cJSON* tag = cJSON_GetArrayItem(tags, j);
|
||||
if (!tag || !cJSON_IsArray(tag) || cJSON_GetArraySize(tag) < 2) {
|
||||
continue;
|
||||
}
|
||||
|
||||
cJSON* key = cJSON_GetArrayItem(tag, 0);
|
||||
cJSON* val = cJSON_GetArrayItem(tag, 1);
|
||||
if (!key || !val || !cJSON_IsString(key) || !cJSON_IsString(val) || !key->valuestring || !val->valuestring) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strcmp(key->valuestring, "r") == 0 && val->valuestring[0] != '\0') {
|
||||
relay_count++;
|
||||
}
|
||||
}
|
||||
|
||||
if (relay_count <= 0) {
|
||||
cJSON_Delete(tags);
|
||||
continue;
|
||||
}
|
||||
|
||||
config->relays = (char**)calloc((size_t)relay_count, sizeof(char*));
|
||||
if (!config->relays) {
|
||||
cJSON_Delete(tags);
|
||||
return -1;
|
||||
}
|
||||
|
||||
config->relay_count = relay_count;
|
||||
int out_i = 0;
|
||||
for (int j = 0; j < tag_count && out_i < relay_count; j++) {
|
||||
cJSON* tag = cJSON_GetArrayItem(tags, j);
|
||||
if (!tag || !cJSON_IsArray(tag) || cJSON_GetArraySize(tag) < 2) {
|
||||
continue;
|
||||
}
|
||||
|
||||
cJSON* key = cJSON_GetArrayItem(tag, 0);
|
||||
cJSON* val = cJSON_GetArrayItem(tag, 1);
|
||||
if (!key || !val || !cJSON_IsString(key) || !cJSON_IsString(val) || !key->valuestring || !val->valuestring) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strcmp(key->valuestring, "r") == 0 && val->valuestring[0] != '\0') {
|
||||
config->relays[out_i] = strdup(val->valuestring);
|
||||
if (!config->relays[out_i]) {
|
||||
cJSON_Delete(tags);
|
||||
return -1;
|
||||
}
|
||||
out_i++;
|
||||
}
|
||||
}
|
||||
|
||||
cJSON_Delete(tags);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int parse_relays(cJSON* root, didactyl_config_t* config) {
|
||||
(void)root;
|
||||
return parse_relays_from_startup_kind10002(config);
|
||||
}
|
||||
|
||||
void config_free(didactyl_config_t* config) {
|
||||
if (!config) {
|
||||
return;
|
||||
@@ -206,15 +643,29 @@ void config_free(didactyl_config_t* config) {
|
||||
free(config->relays);
|
||||
}
|
||||
|
||||
if (config->startup_events) {
|
||||
for (int i = 0; i < config->startup_event_count; i++) {
|
||||
free(config->startup_events[i].content);
|
||||
free(config->startup_events[i].tags_json);
|
||||
}
|
||||
free(config->startup_events);
|
||||
}
|
||||
|
||||
memset(config, 0, sizeof(*config));
|
||||
}
|
||||
|
||||
int config_load(const char* path, didactyl_config_t* config) {
|
||||
if (!path || !config) {
|
||||
config_set_error("config_load called with invalid arguments");
|
||||
return -1;
|
||||
}
|
||||
|
||||
config_set_error("unknown configuration error");
|
||||
|
||||
memset(config, 0, sizeof(*config));
|
||||
snprintf(config->config_path, sizeof(config->config_path), "%s", path);
|
||||
config->dm_protocol = DM_PROTOCOL_NIP04;
|
||||
|
||||
config->tools.enabled = 1;
|
||||
config->tools.max_turns = 8;
|
||||
config->tools.shell.enabled = 1;
|
||||
@@ -222,9 +673,36 @@ int config_load(const char* path, didactyl_config_t* config) {
|
||||
config->tools.shell.max_output_bytes = 65536;
|
||||
strcpy(config->tools.shell.working_directory, ".");
|
||||
|
||||
config->security.verify_signatures = 1;
|
||||
config->security.admin.enabled = 1;
|
||||
config->security.admin.tools_enabled = 1;
|
||||
config->security.wot.enabled = 1;
|
||||
config->security.wot.tools_enabled = 0;
|
||||
config->security.stranger.enabled = 1;
|
||||
config->security.stranger.tools_enabled = 0;
|
||||
config->security.stranger_response[0] = '\0';
|
||||
|
||||
config->admin_context.enabled = 1;
|
||||
config->admin_context.track_kind_0 = 1;
|
||||
config->admin_context.track_kind_3 = 1;
|
||||
config->admin_context.track_kind_10002 = 1;
|
||||
config->admin_context.track_kind_1 = 1;
|
||||
config->admin_context.kind_1_limit = 10;
|
||||
|
||||
config->triggers.enabled = 1;
|
||||
config->triggers.max_active = 16;
|
||||
config->triggers.cooldown_seconds = 60;
|
||||
config->triggers.llm_rate_limit_per_minute = 10;
|
||||
config->triggers.template_rate_limit_per_minute = 60;
|
||||
|
||||
config->api.enabled = 0;
|
||||
config->api.port = 8484;
|
||||
snprintf(config->api.bind_address, sizeof(config->api.bind_address), "%s", "127.0.0.1");
|
||||
|
||||
char* json_buf = NULL;
|
||||
size_t json_len = 0;
|
||||
if (read_file_to_buffer(path, &json_buf, &json_len) != 0) {
|
||||
config_set_error("failed to read config file '%s': %s", path, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -232,56 +710,54 @@ int config_load(const char* path, didactyl_config_t* config) {
|
||||
free(json_buf);
|
||||
|
||||
if (!root) {
|
||||
const char* err = cJSON_GetErrorPtr();
|
||||
config_set_error("invalid JSON in config '%s'%s%s",
|
||||
path,
|
||||
err ? " near: " : "",
|
||||
err ? err : "");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int rc = -1;
|
||||
|
||||
cJSON* agent = cJSON_GetObjectItemCaseSensitive(root, "agent");
|
||||
cJSON* keys = cJSON_GetObjectItemCaseSensitive(root, "keys");
|
||||
cJSON* admin = cJSON_GetObjectItemCaseSensitive(root, "admin");
|
||||
cJSON* llm = cJSON_GetObjectItemCaseSensitive(root, "llm");
|
||||
|
||||
if (!agent || !cJSON_IsObject(agent) ||
|
||||
!keys || !cJSON_IsObject(keys) ||
|
||||
if (!keys || !cJSON_IsObject(keys) ||
|
||||
!admin || !cJSON_IsObject(admin) ||
|
||||
!llm || !cJSON_IsObject(llm)) {
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (copy_json_string(agent, "name", config->profile.name, sizeof(config->profile.name), 1) != 0) {
|
||||
goto cleanup;
|
||||
}
|
||||
if (copy_json_string(agent, "display_name", config->profile.display_name, sizeof(config->profile.display_name), 1) != 0) {
|
||||
goto cleanup;
|
||||
}
|
||||
if (copy_json_string(agent, "about", config->profile.about, sizeof(config->profile.about), 1) != 0) {
|
||||
goto cleanup;
|
||||
}
|
||||
if (copy_json_string(agent, "picture", config->profile.picture, sizeof(config->profile.picture), 0) != 0) {
|
||||
goto cleanup;
|
||||
}
|
||||
if (copy_json_string(agent, "nip05", config->profile.nip05, sizeof(config->profile.nip05), 0) != 0) {
|
||||
config_set_error("config must include object sections: keys, admin, llm");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (copy_json_string(keys, "nsec", config->keys.nsec, sizeof(config->keys.nsec), 1) != 0) {
|
||||
config_set_error("keys.nsec is required and must be a non-empty string");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
char admin_key_raw[OW_MAX_KEY_LEN] = {0};
|
||||
if (copy_json_string(admin, "pubkey", admin_key_raw, sizeof(admin_key_raw), 1) != 0) {
|
||||
config_set_error("admin.pubkey is required and must be a non-empty string");
|
||||
goto cleanup;
|
||||
}
|
||||
if (decode_pubkey_hex_or_npub(admin_key_raw, config->admin.pubkey) != 0) {
|
||||
config_set_error("admin.pubkey must be npub1... or 64-char hex");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (parse_startup_events(root, config) != 0) {
|
||||
config_set_error("invalid startup_events configuration");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (parse_relays(root, config) != 0) {
|
||||
config_set_error("relay configuration is invalid: startup_events must include kind 10002 with non-empty r tags");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (copy_json_string(llm, "provider", config->llm.provider, sizeof(config->llm.provider), 0) != 0) {
|
||||
config_set_error("llm.provider must be a string if provided");
|
||||
goto cleanup;
|
||||
}
|
||||
if (config->llm.provider[0] == '\0') {
|
||||
@@ -289,12 +765,15 @@ int config_load(const char* path, didactyl_config_t* config) {
|
||||
}
|
||||
|
||||
if (copy_json_string(llm, "api_key", config->llm.api_key, sizeof(config->llm.api_key), 1) != 0) {
|
||||
config_set_error("llm.api_key is required and must be a string");
|
||||
goto cleanup;
|
||||
}
|
||||
if (copy_json_string(llm, "model", config->llm.model, sizeof(config->llm.model), 1) != 0) {
|
||||
config_set_error("llm.model is required and must be a string");
|
||||
goto cleanup;
|
||||
}
|
||||
if (copy_json_string(llm, "base_url", config->llm.base_url, sizeof(config->llm.base_url), 1) != 0) {
|
||||
config_set_error("llm.base_url is required and must be a string");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
@@ -304,15 +783,43 @@ int config_load(const char* path, didactyl_config_t* config) {
|
||||
config->llm.max_tokens = (max_tokens && cJSON_IsNumber(max_tokens)) ? (int)max_tokens->valuedouble : 512;
|
||||
config->llm.temperature = (temperature && cJSON_IsNumber(temperature)) ? temperature->valuedouble : 0.7;
|
||||
|
||||
if (parse_dm_protocol_config(root, config) != 0) {
|
||||
config_set_error("invalid dm_protocol configuration (expected 'nip04', 'nip17', or 'both')");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (parse_tools_config(root, config) != 0) {
|
||||
config_set_error("invalid tools configuration");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (parse_security_config(root, config) != 0) {
|
||||
config_set_error("invalid security configuration");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (parse_admin_context_config(root, config) != 0) {
|
||||
config_set_error("invalid admin_context configuration");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (parse_triggers_config(root, config) != 0) {
|
||||
config_set_error("invalid triggers configuration");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (parse_api_config(root, config) != 0) {
|
||||
config_set_error("invalid api configuration");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (decode_private_key(config->keys.nsec, config->keys.private_key) != 0) {
|
||||
config_set_error("keys.nsec must be valid nsec1... or 64-char hex private key");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (nostr_ec_public_key_from_private_key(config->keys.private_key, config->keys.public_key) != 0) {
|
||||
config_set_error("failed to derive public key from keys.nsec");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
|
||||
66
src/config.h
66
src/config.h
@@ -9,13 +9,7 @@
|
||||
#define OW_MAX_KEY_LEN 256
|
||||
#define OW_MAX_MODEL_LEN 128
|
||||
|
||||
typedef struct {
|
||||
char name[OW_MAX_NAME_LEN];
|
||||
char display_name[OW_MAX_NAME_LEN];
|
||||
char about[OW_MAX_ABOUT_LEN];
|
||||
char picture[OW_MAX_URL_LEN];
|
||||
char nip05[OW_MAX_URL_LEN];
|
||||
} agent_profile_t;
|
||||
#define OW_MAX_STRANGER_RESPONSE_LEN 512
|
||||
|
||||
typedef struct {
|
||||
char nsec[OW_MAX_KEY_LEN];
|
||||
@@ -24,6 +18,12 @@ typedef struct {
|
||||
char public_key_hex[65];
|
||||
} agent_keys_t;
|
||||
|
||||
typedef enum {
|
||||
DM_PROTOCOL_NIP04 = 0,
|
||||
DM_PROTOCOL_NIP17 = 1,
|
||||
DM_PROTOCOL_BOTH = 2
|
||||
} dm_protocol_t;
|
||||
|
||||
typedef struct {
|
||||
char pubkey[65];
|
||||
} admin_config_t;
|
||||
@@ -51,16 +51,66 @@ typedef struct {
|
||||
} tools_config_t;
|
||||
|
||||
typedef struct {
|
||||
agent_profile_t profile;
|
||||
int kind;
|
||||
char* content;
|
||||
char* tags_json; // JSON array string for tags, optional
|
||||
} startup_event_t;
|
||||
|
||||
typedef struct {
|
||||
int enabled;
|
||||
int tools_enabled;
|
||||
} security_tier_config_t;
|
||||
|
||||
typedef struct {
|
||||
int verify_signatures;
|
||||
char stranger_response[OW_MAX_STRANGER_RESPONSE_LEN];
|
||||
security_tier_config_t admin;
|
||||
security_tier_config_t wot;
|
||||
security_tier_config_t stranger;
|
||||
} security_config_t;
|
||||
|
||||
typedef struct {
|
||||
int enabled;
|
||||
int track_kind_0;
|
||||
int track_kind_3;
|
||||
int track_kind_10002;
|
||||
int track_kind_1;
|
||||
int kind_1_limit;
|
||||
} admin_context_config_t;
|
||||
|
||||
typedef struct {
|
||||
int enabled;
|
||||
int max_active;
|
||||
int cooldown_seconds;
|
||||
int llm_rate_limit_per_minute;
|
||||
int template_rate_limit_per_minute;
|
||||
} triggers_config_t;
|
||||
|
||||
typedef struct {
|
||||
int enabled;
|
||||
int port;
|
||||
char bind_address[OW_MAX_URL_LEN];
|
||||
} api_config_t;
|
||||
|
||||
typedef struct {
|
||||
agent_keys_t keys;
|
||||
admin_config_t admin;
|
||||
dm_protocol_t dm_protocol;
|
||||
char** relays;
|
||||
int relay_count;
|
||||
llm_config_t llm;
|
||||
tools_config_t tools;
|
||||
security_config_t security;
|
||||
admin_context_config_t admin_context;
|
||||
triggers_config_t triggers;
|
||||
api_config_t api;
|
||||
startup_event_t* startup_events;
|
||||
int startup_event_count;
|
||||
char config_path[OW_MAX_URL_LEN];
|
||||
} didactyl_config_t;
|
||||
|
||||
int config_load(const char* path, didactyl_config_t* config);
|
||||
const char* config_last_error(void);
|
||||
void config_free(didactyl_config_t* config);
|
||||
|
||||
#endif
|
||||
1281
src/http_api.c
Normal file
1281
src/http_api.c
Normal file
File diff suppressed because it is too large
Load Diff
19
src/http_api.h
Normal file
19
src/http_api.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef DIDACTYL_HTTP_API_H
|
||||
#define DIDACTYL_HTTP_API_H
|
||||
|
||||
#include "config.h"
|
||||
#include "tools.h"
|
||||
|
||||
struct trigger_manager;
|
||||
|
||||
typedef struct {
|
||||
didactyl_config_t* cfg;
|
||||
tools_context_t* tools_ctx;
|
||||
struct trigger_manager* trigger_manager;
|
||||
} http_api_context_t;
|
||||
|
||||
int http_api_init(const http_api_context_t* ctx);
|
||||
int http_api_poll(int timeout_ms);
|
||||
void http_api_cleanup(void);
|
||||
|
||||
#endif
|
||||
190
src/llm.c
190
src/llm.c
@@ -6,6 +6,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "cjson/cJSON.h"
|
||||
|
||||
@@ -41,15 +42,50 @@ static size_t write_cb(void* contents, size_t size, size_t nmemb, void* userp) {
|
||||
return total;
|
||||
}
|
||||
|
||||
static char* perform_chat_request(const char* body) {
|
||||
static const char* detect_ca_bundle_path(void) {
|
||||
const char* env = getenv("SSL_CERT_FILE");
|
||||
if (env && env[0] != '\0' && access(env, R_OK) == 0) {
|
||||
return env;
|
||||
}
|
||||
|
||||
static const char* candidates[] = {
|
||||
"/etc/ssl/certs/ca-certificates.crt", // Debian/Ubuntu
|
||||
"/etc/ssl/cert.pem", // Alpine
|
||||
"/etc/pki/tls/certs/ca-bundle.crt", // RHEL/CentOS/Fedora
|
||||
"/etc/ssl/ca-bundle.pem" // openSUSE
|
||||
};
|
||||
|
||||
for (size_t i = 0; i < sizeof(candidates) / sizeof(candidates[0]); i++) {
|
||||
if (access(candidates[i], R_OK) == 0) {
|
||||
return candidates[i];
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int url_looks_like_websocket(const char* url) {
|
||||
if (!url) return 0;
|
||||
return (strncmp(url, "ws://", 5) == 0) || (strncmp(url, "wss://", 6) == 0);
|
||||
}
|
||||
|
||||
static char* perform_http_request(const char* url, const char* body, int is_post) {
|
||||
CURL* curl = curl_easy_init();
|
||||
if (!curl || !body) {
|
||||
if (!curl || !url) {
|
||||
if (curl) curl_easy_cleanup(curl);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char url[OW_MAX_URL_LEN + 64];
|
||||
snprintf(url, sizeof(url), "%s/chat/completions", g_cfg.base_url);
|
||||
if (url_looks_like_websocket(url)) {
|
||||
fprintf(stderr,
|
||||
"[didactyl] llm config error: base_url must be HTTP(S), got WebSocket URL: %s\n",
|
||||
url);
|
||||
fprintf(stderr,
|
||||
"[didactyl] llm hint: set llm.base_url to an OpenAI-compatible HTTPS endpoint, e.g. https://api.example.com/v1\n");
|
||||
curl_easy_cleanup(curl);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
response_buffer_t rb = {0};
|
||||
struct curl_slist* headers = NULL;
|
||||
@@ -60,13 +96,21 @@ static char* perform_chat_request(const char* body) {
|
||||
headers = curl_slist_append(headers, auth_header);
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url);
|
||||
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body);
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPGET, is_post ? 0L : 1L);
|
||||
if (is_post) {
|
||||
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body ? body : "{}");
|
||||
}
|
||||
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60L);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &rb);
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
||||
|
||||
const char* ca_bundle = detect_ca_bundle_path();
|
||||
if (ca_bundle) {
|
||||
curl_easy_setopt(curl, CURLOPT_CAINFO, ca_bundle);
|
||||
}
|
||||
|
||||
CURLcode res = curl_easy_perform(curl);
|
||||
long status = 0;
|
||||
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
|
||||
@@ -74,14 +118,46 @@ static char* perform_chat_request(const char* body) {
|
||||
curl_slist_free_all(headers);
|
||||
curl_easy_cleanup(curl);
|
||||
|
||||
if (res != CURLE_OK || status < 200 || status >= 300 || !rb.data) {
|
||||
if (res != CURLE_OK) {
|
||||
fprintf(stderr, "[didactyl] llm http request failed: curl=%s\n", curl_easy_strerror(res));
|
||||
if (rb.data && rb.len > 0) {
|
||||
fprintf(stderr, "[didactyl] llm partial response: %.600s%s\n",
|
||||
rb.data,
|
||||
rb.len > 600 ? "..." : "");
|
||||
}
|
||||
free(rb.data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (status < 200 || status >= 300) {
|
||||
fprintf(stderr, "[didactyl] llm http request failed: status=%ld\n", status);
|
||||
if (status == 101) {
|
||||
fprintf(stderr,
|
||||
"[didactyl] llm hint: received HTTP 101 (Switching Protocols), which usually means llm.base_url points to a WebSocket server instead of an HTTP LLM API\n");
|
||||
}
|
||||
if (rb.data && rb.len > 0) {
|
||||
fprintf(stderr, "[didactyl] llm error response: %.1200s%s\n",
|
||||
rb.data,
|
||||
rb.len > 1200 ? "..." : "");
|
||||
}
|
||||
free(rb.data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!rb.data) {
|
||||
fprintf(stderr, "[didactyl] llm http request failed: empty response body\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return rb.data;
|
||||
}
|
||||
|
||||
static char* perform_chat_request(const char* body) {
|
||||
char url[OW_MAX_URL_LEN + 64];
|
||||
snprintf(url, sizeof(url), "%s/chat/completions", g_cfg.base_url);
|
||||
return perform_http_request(url, body, 1);
|
||||
}
|
||||
|
||||
static char* build_request_json(const char* system_prompt, const char* user_message) {
|
||||
cJSON* root = cJSON_CreateObject();
|
||||
cJSON* messages = cJSON_CreateArray();
|
||||
@@ -228,6 +304,9 @@ char* llm_chat(const char* system_prompt, const char* user_message) {
|
||||
|
||||
llm_response_t parsed;
|
||||
if (parse_llm_response(raw, &parsed) != 0) {
|
||||
fprintf(stderr, "[didactyl] failed to parse llm response (non-tool path): %.1200s%s\n",
|
||||
raw,
|
||||
strlen(raw) > 1200 ? "..." : "");
|
||||
free(raw);
|
||||
return NULL;
|
||||
}
|
||||
@@ -238,19 +317,16 @@ char* llm_chat(const char* system_prompt, const char* user_message) {
|
||||
return answer;
|
||||
}
|
||||
|
||||
int llm_chat_with_tools(const char* system_prompt,
|
||||
const char* user_message,
|
||||
const char* tools_json,
|
||||
llm_response_t* out_response) {
|
||||
if (!g_initialized || !out_response) {
|
||||
int llm_chat_with_tools_messages(const char* messages_json,
|
||||
const char* tools_json,
|
||||
const char* tool_choice,
|
||||
llm_response_t* out_response) {
|
||||
if (!g_initialized || !out_response || !messages_json) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
cJSON* root = cJSON_CreateObject();
|
||||
cJSON* messages = cJSON_CreateArray();
|
||||
if (!root || !messages) {
|
||||
cJSON_Delete(root);
|
||||
cJSON_Delete(messages);
|
||||
if (!root) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -258,20 +334,19 @@ int llm_chat_with_tools(const char* system_prompt,
|
||||
cJSON_AddNumberToObject(root, "max_tokens", g_cfg.max_tokens);
|
||||
cJSON_AddNumberToObject(root, "temperature", g_cfg.temperature);
|
||||
|
||||
cJSON* system_msg = cJSON_CreateObject();
|
||||
cJSON* user_msg = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(system_msg, "role", "system");
|
||||
cJSON_AddStringToObject(system_msg, "content", system_prompt ? system_prompt : "");
|
||||
cJSON_AddStringToObject(user_msg, "role", "user");
|
||||
cJSON_AddStringToObject(user_msg, "content", user_message ? user_message : "");
|
||||
cJSON_AddItemToArray(messages, system_msg);
|
||||
cJSON_AddItemToArray(messages, user_msg);
|
||||
cJSON* messages = cJSON_Parse(messages_json);
|
||||
if (!messages || !cJSON_IsArray(messages)) {
|
||||
cJSON_Delete(messages);
|
||||
cJSON_Delete(root);
|
||||
return -1;
|
||||
}
|
||||
cJSON_AddItemToObject(root, "messages", messages);
|
||||
|
||||
if (tools_json) {
|
||||
cJSON* tools = cJSON_Parse(tools_json);
|
||||
if (tools && cJSON_IsArray(tools)) {
|
||||
cJSON_AddItemToObject(root, "tools", tools);
|
||||
cJSON_AddStringToObject(root, "tool_choice", tool_choice ? tool_choice : "auto");
|
||||
} else {
|
||||
cJSON_Delete(tools);
|
||||
}
|
||||
@@ -286,10 +361,47 @@ int llm_chat_with_tools(const char* system_prompt,
|
||||
if (!raw) return -1;
|
||||
|
||||
int rc = parse_llm_response(raw, out_response);
|
||||
if (rc != 0) {
|
||||
fprintf(stderr, "[didactyl] failed to parse llm response (tools path): %.1200s%s\n",
|
||||
raw,
|
||||
strlen(raw) > 1200 ? "..." : "");
|
||||
}
|
||||
free(raw);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int llm_chat_with_tools(const char* system_prompt,
|
||||
const char* user_message,
|
||||
const char* tools_json,
|
||||
llm_response_t* out_response) {
|
||||
cJSON* messages = cJSON_CreateArray();
|
||||
cJSON* system_msg = cJSON_CreateObject();
|
||||
cJSON* user_msg = cJSON_CreateObject();
|
||||
if (!messages || !system_msg || !user_msg) {
|
||||
cJSON_Delete(messages);
|
||||
cJSON_Delete(system_msg);
|
||||
cJSON_Delete(user_msg);
|
||||
return -1;
|
||||
}
|
||||
|
||||
cJSON_AddStringToObject(system_msg, "role", "system");
|
||||
cJSON_AddStringToObject(system_msg, "content", system_prompt ? system_prompt : "");
|
||||
cJSON_AddStringToObject(user_msg, "role", "user");
|
||||
cJSON_AddStringToObject(user_msg, "content", user_message ? user_message : "");
|
||||
cJSON_AddItemToArray(messages, system_msg);
|
||||
cJSON_AddItemToArray(messages, user_msg);
|
||||
|
||||
char* messages_json = cJSON_PrintUnformatted(messages);
|
||||
cJSON_Delete(messages);
|
||||
if (!messages_json) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int rc = llm_chat_with_tools_messages(messages_json, tools_json, "auto", out_response);
|
||||
free(messages_json);
|
||||
return rc;
|
||||
}
|
||||
|
||||
void llm_response_free(llm_response_t* response) {
|
||||
if (!response) return;
|
||||
free(response->content);
|
||||
@@ -302,6 +414,36 @@ void llm_response_free(llm_response_t* response) {
|
||||
memset(response, 0, sizeof(*response));
|
||||
}
|
||||
|
||||
int llm_get_config(llm_config_t* out_config) {
|
||||
if (!g_initialized || !out_config) {
|
||||
return -1;
|
||||
}
|
||||
*out_config = g_cfg;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int llm_set_config(const llm_config_t* config) {
|
||||
if (!g_initialized || !config) {
|
||||
return -1;
|
||||
}
|
||||
g_cfg = *config;
|
||||
return 0;
|
||||
}
|
||||
|
||||
char* llm_list_models_json(const char* base_url_override) {
|
||||
if (!g_initialized) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* base_url = (base_url_override && base_url_override[0] != '\0')
|
||||
? base_url_override
|
||||
: g_cfg.base_url;
|
||||
|
||||
char url[OW_MAX_URL_LEN + 32];
|
||||
snprintf(url, sizeof(url), "%s/models", base_url);
|
||||
return perform_http_request(url, NULL, 0);
|
||||
}
|
||||
|
||||
void llm_cleanup(void) {
|
||||
if (!g_initialized) {
|
||||
return;
|
||||
|
||||
@@ -21,7 +21,14 @@ int llm_chat_with_tools(const char* system_prompt,
|
||||
const char* user_message,
|
||||
const char* tools_json,
|
||||
llm_response_t* out_response);
|
||||
int llm_chat_with_tools_messages(const char* messages_json,
|
||||
const char* tools_json,
|
||||
const char* tool_choice,
|
||||
llm_response_t* out_response);
|
||||
void llm_response_free(llm_response_t* response);
|
||||
int llm_get_config(llm_config_t* out_config);
|
||||
int llm_set_config(const llm_config_t* config);
|
||||
char* llm_list_models_json(const char* base_url_override);
|
||||
void llm_cleanup(void);
|
||||
|
||||
#endif
|
||||
253
src/main.c
253
src/main.c
@@ -4,15 +4,19 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "../../nostr_core_lib/nostr_core/nostr_core.h"
|
||||
#include "main.h"
|
||||
#include "agent.h"
|
||||
#include "config.h"
|
||||
#include "context.h"
|
||||
#include "llm.h"
|
||||
#include "nostr_handler.h"
|
||||
#include "debug.h"
|
||||
#include "trigger_manager.h"
|
||||
#include "tools.h"
|
||||
#include "cjson/cJSON.h"
|
||||
#include "http_api.h"
|
||||
|
||||
static volatile sig_atomic_t g_running = 1;
|
||||
|
||||
@@ -21,21 +25,74 @@ static void signal_handler(int signum) {
|
||||
g_running = 0;
|
||||
}
|
||||
|
||||
static void print_usage(const char* prog) {
|
||||
fprintf(stderr,
|
||||
"Usage: %s [-h|--help] [-v|--version] [--config <path>] [--debug <0-5>] [--dump-schemas] [--test-tool <name> <args_json>]\n",
|
||||
prog ? prog : "didactyl");
|
||||
}
|
||||
|
||||
static int tool_result_success(const char* json) {
|
||||
if (!json) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
cJSON* root = cJSON_Parse(json);
|
||||
if (!root || !cJSON_IsObject(root)) {
|
||||
cJSON_Delete(root);
|
||||
return 0;
|
||||
}
|
||||
|
||||
cJSON* success = cJSON_GetObjectItemCaseSensitive(root, "success");
|
||||
int ok = (success && cJSON_IsBool(success) && cJSON_IsTrue(success)) ? 1 : 0;
|
||||
cJSON_Delete(root);
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int wait_for_connected_relays(int timeout_ms) {
|
||||
if (timeout_ms <= 0) {
|
||||
return nostr_handler_connected_relay_count() > 0 ? 1 : 0;
|
||||
}
|
||||
|
||||
const int step_ms = 100;
|
||||
int elapsed_ms = 0;
|
||||
|
||||
while (elapsed_ms < timeout_ms) {
|
||||
if (nostr_handler_connected_relay_count() > 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
(void)nostr_handler_poll(step_ms);
|
||||
elapsed_ms += step_ms;
|
||||
}
|
||||
|
||||
return nostr_handler_connected_relay_count() > 0 ? 1 : 0;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
const char* config_path = "./config.json";
|
||||
const char* context_path = "./SYSTEM.md";
|
||||
|
||||
int debug_level = DEBUG_LEVEL_INFO;
|
||||
int debug_level = DEBUG_LEVEL_TRACE;
|
||||
int dump_schemas = 0;
|
||||
const char* test_tool_name = NULL;
|
||||
const char* test_tool_args = "{}";
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (strcmp(argv[i], "--config") == 0 && i + 1 < argc) {
|
||||
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
|
||||
print_usage(argv[0]);
|
||||
return 0;
|
||||
} else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) {
|
||||
printf("%s %s\n", DIDACTYL_NAME, DIDACTYL_VERSION);
|
||||
return 0;
|
||||
} else if (strcmp(argv[i], "--config") == 0 && i + 1 < argc) {
|
||||
config_path = argv[++i];
|
||||
} else if (strcmp(argv[i], "--context") == 0 && i + 1 < argc) {
|
||||
context_path = argv[++i];
|
||||
} else if (strcmp(argv[i], "--debug") == 0 && i + 1 < argc) {
|
||||
debug_level = atoi(argv[++i]);
|
||||
} else if (strcmp(argv[i], "--dump-schemas") == 0) {
|
||||
dump_schemas = 1;
|
||||
} else if (strcmp(argv[i], "--test-tool") == 0 && i + 2 < argc) {
|
||||
test_tool_name = argv[++i];
|
||||
test_tool_args = argv[++i];
|
||||
} else {
|
||||
fprintf(stderr, "Usage: %s [--config <path>] [--context <path>] [--debug <0-5>]\n", argv[0]);
|
||||
print_usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -52,21 +109,86 @@ int main(int argc, char** argv) {
|
||||
didactyl_config_t cfg;
|
||||
if (config_load(config_path, &cfg) != 0) {
|
||||
fprintf(stderr, "Failed to load config: %s\n", config_path);
|
||||
fprintf(stderr, "Config error: %s\n", config_last_error());
|
||||
nostr_cleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
char* system_context = context_load(context_path);
|
||||
if (!system_context) {
|
||||
fprintf(stderr, "Failed to load context file: %s\n", context_path);
|
||||
if (dump_schemas || test_tool_name) {
|
||||
if (llm_init(&cfg.llm) != 0) {
|
||||
fprintf(stderr, "Failed to initialize llm client\n");
|
||||
config_free(&cfg);
|
||||
nostr_cleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (nostr_handler_init(&cfg) != 0) {
|
||||
fprintf(stderr, "Failed to initialize nostr handler\n");
|
||||
config_free(&cfg);
|
||||
llm_cleanup();
|
||||
nostr_cleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
tools_context_t tools_ctx;
|
||||
if (tools_init(&tools_ctx, &cfg) != 0) {
|
||||
fprintf(stderr, "Failed to initialize tools context\n");
|
||||
nostr_handler_cleanup();
|
||||
config_free(&cfg);
|
||||
llm_cleanup();
|
||||
nostr_cleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
int exit_code = 0;
|
||||
|
||||
if (test_tool_name) {
|
||||
const int timeout_ms = 15000;
|
||||
if (!wait_for_connected_relays(timeout_ms)) {
|
||||
fprintf(stderr,
|
||||
"No relay connection established within %d ms; refusing to run --test-tool '%s'\n",
|
||||
timeout_ms,
|
||||
test_tool_name);
|
||||
tools_cleanup(&tools_ctx);
|
||||
nostr_handler_cleanup();
|
||||
config_free(&cfg);
|
||||
llm_cleanup();
|
||||
nostr_cleanup();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (dump_schemas) {
|
||||
char* schemas = tools_build_openai_schema_json(&tools_ctx);
|
||||
if (!schemas) {
|
||||
fprintf(stderr, "Failed to build tool schemas\n");
|
||||
exit_code = 1;
|
||||
} else {
|
||||
printf("%s\n", schemas);
|
||||
free(schemas);
|
||||
}
|
||||
} else {
|
||||
char* result = tools_execute(&tools_ctx, test_tool_name, test_tool_args);
|
||||
if (!result) {
|
||||
fprintf(stderr, "Failed to execute tool\n");
|
||||
exit_code = 1;
|
||||
} else {
|
||||
printf("%s\n", result);
|
||||
exit_code = tool_result_success(result) ? 0 : 1;
|
||||
free(result);
|
||||
}
|
||||
}
|
||||
|
||||
tools_cleanup(&tools_ctx);
|
||||
nostr_handler_cleanup();
|
||||
config_free(&cfg);
|
||||
llm_cleanup();
|
||||
nostr_cleanup();
|
||||
return 1;
|
||||
return exit_code;
|
||||
}
|
||||
|
||||
if (llm_init(&cfg.llm) != 0) {
|
||||
fprintf(stderr, "Failed to initialize llm client\n");
|
||||
context_free(system_context);
|
||||
config_free(&cfg);
|
||||
nostr_cleanup();
|
||||
return 1;
|
||||
@@ -75,27 +197,73 @@ int main(int argc, char** argv) {
|
||||
if (nostr_handler_init(&cfg) != 0) {
|
||||
fprintf(stderr, "Failed to initialize nostr handler\n");
|
||||
llm_cleanup();
|
||||
context_free(system_context);
|
||||
config_free(&cfg);
|
||||
nostr_cleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (nostr_handler_reconcile_startup_events() != 0) {
|
||||
DEBUG_WARN("[didactyl] startup phase: reconcile startup events failed (continuing)");
|
||||
}
|
||||
|
||||
const char* system_context = nostr_handler_get_system_context();
|
||||
if (!system_context || system_context[0] == '\0') {
|
||||
system_context = "You are Didactyl, a sovereign AI agent living on Nostr.";
|
||||
}
|
||||
|
||||
if (agent_init(&cfg, system_context) != 0) {
|
||||
fprintf(stderr, "Failed to initialize agent\n");
|
||||
nostr_handler_cleanup();
|
||||
llm_cleanup();
|
||||
context_free(system_context);
|
||||
config_free(&cfg);
|
||||
nostr_cleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
DEBUG_INFO("[didactyl] startup phase: publish profile begin");
|
||||
if (nostr_handler_publish_profile() != 0) {
|
||||
DEBUG_WARN("[didactyl] publish profile deferred/failed (will continue startup)");
|
||||
trigger_manager_t trigger_manager;
|
||||
if (trigger_manager_init(&trigger_manager, &cfg) != 0) {
|
||||
fprintf(stderr, "Failed to initialize trigger manager\n");
|
||||
agent_cleanup();
|
||||
nostr_handler_cleanup();
|
||||
llm_cleanup();
|
||||
config_free(&cfg);
|
||||
nostr_cleanup();
|
||||
return 1;
|
||||
}
|
||||
agent_set_trigger_manager(&trigger_manager);
|
||||
|
||||
if (trigger_manager_load_from_skills(&trigger_manager) != 0) {
|
||||
DEBUG_WARN("[didactyl] startup phase: trigger manager load from skills failed (continuing)");
|
||||
}
|
||||
|
||||
DEBUG_INFO("[didactyl] startup phase: subscribe admin context begin");
|
||||
if (nostr_handler_subscribe_admin_context() != 0) {
|
||||
DEBUG_WARN("[didactyl] startup phase: subscribe admin context failed (continuing)");
|
||||
}
|
||||
DEBUG_INFO("[didactyl] startup phase: subscribe admin context end");
|
||||
|
||||
char startup_dm[768];
|
||||
const char* startup_name = nostr_handler_get_startup_display_name();
|
||||
int connected_relays = nostr_handler_connected_relay_count();
|
||||
time_t startup_now = time(NULL);
|
||||
struct tm startup_tm;
|
||||
char startup_time_str[32] = {0};
|
||||
if (localtime_r(&startup_now, &startup_tm)) {
|
||||
(void)strftime(startup_time_str, sizeof(startup_time_str), "%Y-%m-%d %H:%M:%S", &startup_tm);
|
||||
} else {
|
||||
snprintf(startup_time_str, sizeof(startup_time_str), "%ld", (long)startup_now);
|
||||
}
|
||||
snprintf(startup_dm,
|
||||
sizeof(startup_dm),
|
||||
"%s has started up and is online at %s (version %s, connected relays: %d/%d).",
|
||||
(startup_name && startup_name[0] != '\0') ? startup_name : "Didactyl",
|
||||
startup_time_str,
|
||||
DIDACTYL_VERSION,
|
||||
connected_relays,
|
||||
cfg.relay_count);
|
||||
if (nostr_handler_send_dm_auto(cfg.admin.pubkey, startup_dm) != 0) {
|
||||
DEBUG_WARN("[didactyl] startup phase: failed to send startup status DM to admin");
|
||||
}
|
||||
DEBUG_INFO("[didactyl] startup phase: publish profile end");
|
||||
|
||||
DEBUG_INFO("[didactyl] startup phase: subscribe DMs begin");
|
||||
if (nostr_handler_subscribe_dms(agent_on_message, NULL) != 0) {
|
||||
@@ -104,7 +272,7 @@ int main(int argc, char** argv) {
|
||||
agent_cleanup();
|
||||
nostr_handler_cleanup();
|
||||
llm_cleanup();
|
||||
context_free(system_context);
|
||||
trigger_manager_cleanup(&trigger_manager);
|
||||
config_free(&cfg);
|
||||
nostr_cleanup();
|
||||
return 1;
|
||||
@@ -114,22 +282,65 @@ int main(int argc, char** argv) {
|
||||
|
||||
signal(SIGINT, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
|
||||
int http_api_started = 0;
|
||||
if (cfg.api.enabled) {
|
||||
http_api_context_t http_ctx;
|
||||
memset(&http_ctx, 0, sizeof(http_ctx));
|
||||
http_ctx.cfg = &cfg;
|
||||
http_ctx.tools_ctx = agent_tools_context();
|
||||
http_ctx.trigger_manager = &trigger_manager;
|
||||
|
||||
if (http_api_init(&http_ctx) != 0) {
|
||||
fprintf(stderr,
|
||||
"Failed to initialize HTTP API on %s:%d\n",
|
||||
cfg.api.bind_address,
|
||||
cfg.api.port);
|
||||
agent_cleanup();
|
||||
nostr_handler_cleanup();
|
||||
llm_cleanup();
|
||||
trigger_manager_cleanup(&trigger_manager);
|
||||
config_free(&cfg);
|
||||
nostr_cleanup();
|
||||
return 1;
|
||||
}
|
||||
http_api_started = 1;
|
||||
DEBUG_INFO("[didactyl] HTTP API listening at http://%s:%d",
|
||||
cfg.api.bind_address[0] ? cfg.api.bind_address : "127.0.0.1",
|
||||
cfg.api.port > 0 ? cfg.api.port : 8484);
|
||||
DEBUG_INFO("[didactyl] HTTP API endpoints: http://%s:%d/api/context/current http://%s:%d/api/context/parts",
|
||||
cfg.api.bind_address[0] ? cfg.api.bind_address : "127.0.0.1",
|
||||
cfg.api.port > 0 ? cfg.api.port : 8484,
|
||||
cfg.api.bind_address[0] ? cfg.api.bind_address : "127.0.0.1",
|
||||
cfg.api.port > 0 ? cfg.api.port : 8484);
|
||||
} else {
|
||||
DEBUG_INFO("[didactyl] HTTP API disabled (set api.enabled=true in config to enable)");
|
||||
}
|
||||
|
||||
DEBUG_INFO("[didactyl] entering main poll loop");
|
||||
DEBUG_INFO("[didactyl] running with pubkey %s", cfg.keys.public_key_hex);
|
||||
|
||||
while (g_running) {
|
||||
(void)nostr_handler_poll(100);
|
||||
(void)trigger_manager_poll(&trigger_manager);
|
||||
if (http_api_started) {
|
||||
(void)http_api_poll(0);
|
||||
}
|
||||
struct timespec ts = {0, 10 * 1000 * 1000};
|
||||
nanosleep(&ts, NULL);
|
||||
}
|
||||
|
||||
DEBUG_INFO("[didactyl] shutting down");
|
||||
|
||||
if (http_api_started) {
|
||||
http_api_cleanup();
|
||||
}
|
||||
|
||||
agent_cleanup();
|
||||
nostr_handler_cleanup();
|
||||
llm_cleanup();
|
||||
context_free(system_context);
|
||||
trigger_manager_cleanup(&trigger_manager);
|
||||
config_free(&cfg);
|
||||
nostr_cleanup();
|
||||
|
||||
|
||||
@@ -12,8 +12,8 @@
|
||||
// Using DIDACTYL_ prefix to avoid conflicts with nostr_core_lib VERSION macros
|
||||
#define DIDACTYL_VERSION_MAJOR 0
|
||||
#define DIDACTYL_VERSION_MINOR 0
|
||||
#define DIDACTYL_VERSION_PATCH 1
|
||||
#define DIDACTYL_VERSION "v0.0.1"
|
||||
#define DIDACTYL_VERSION_PATCH 37
|
||||
#define DIDACTYL_VERSION "v0.0.37"
|
||||
|
||||
// Agent metadata
|
||||
#define DIDACTYL_NAME "Didactyl"
|
||||
|
||||
28193
src/mongoose.c
Normal file
28193
src/mongoose.c
Normal file
File diff suppressed because it is too large
Load Diff
4038
src/mongoose.h
Normal file
4038
src/mongoose.h
Normal file
File diff suppressed because it is too large
Load Diff
1863
src/nostr_handler.c
1863
src/nostr_handler.c
File diff suppressed because it is too large
Load Diff
@@ -4,15 +4,49 @@
|
||||
#include "config.h"
|
||||
#include "cjson/cJSON.h"
|
||||
|
||||
typedef void (*dm_callback_t)(const char* sender_pubkey_hex, const char* message, void* user_data);
|
||||
typedef enum {
|
||||
DIDACTYL_SENDER_ADMIN = 1,
|
||||
DIDACTYL_SENDER_WOT = 2,
|
||||
DIDACTYL_SENDER_STRANGER = 3
|
||||
} didactyl_sender_tier_t;
|
||||
|
||||
typedef void (*dm_callback_t)(const char* sender_pubkey_hex,
|
||||
const char* message,
|
||||
didactyl_sender_tier_t tier,
|
||||
void* user_data);
|
||||
|
||||
typedef struct {
|
||||
int success;
|
||||
int kind;
|
||||
int relay_count;
|
||||
int accepted_by_pool_count;
|
||||
char event_id[65];
|
||||
char note_uri[256];
|
||||
char naddr_uri[1024];
|
||||
char d_tag[128];
|
||||
char** relays;
|
||||
} nostr_publish_result_t;
|
||||
|
||||
int nostr_handler_init(didactyl_config_t* config);
|
||||
int nostr_handler_publish_profile(void);
|
||||
int nostr_handler_subscribe_admin_context(void);
|
||||
int nostr_handler_subscribe_dms(dm_callback_t callback, void* user_data);
|
||||
int nostr_handler_send_dm(const char* recipient_pubkey_hex, const char* message);
|
||||
int nostr_handler_publish_kind_event(int kind, const char* content, cJSON* tags);
|
||||
int nostr_handler_send_dm_auto(const char* recipient_pubkey_hex, const char* message);
|
||||
int nostr_handler_publish_kind_event(int kind, const char* content, cJSON* tags, nostr_publish_result_t* out_result);
|
||||
void nostr_handler_publish_result_free(nostr_publish_result_t* result);
|
||||
char* nostr_handler_query_json(cJSON* filter, int timeout_ms);
|
||||
int nostr_handler_poll(int timeout_ms);
|
||||
int nostr_handler_reconcile_startup_events(void);
|
||||
const char* nostr_handler_get_system_context(void);
|
||||
const char* nostr_handler_get_startup_display_name(void);
|
||||
int nostr_handler_connected_relay_count(void);
|
||||
char* nostr_handler_get_admin_kind0_context(void);
|
||||
char* nostr_handler_get_admin_kind10002_context(void);
|
||||
char* nostr_handler_get_admin_kind1_notes_context(void);
|
||||
int nostr_handler_is_wot_contact(const char* pubkey_hex);
|
||||
char* nostr_handler_relay_status_json(void);
|
||||
char* nostr_handler_relay_info_json(const char* relay_url);
|
||||
int nostr_handler_send_dm_nip17(const char* recipient_pubkey_hex, const char* message, const char* subject);
|
||||
void nostr_handler_cleanup(void);
|
||||
|
||||
#endif
|
||||
543
src/prompt_template.c
Normal file
543
src/prompt_template.c
Normal file
@@ -0,0 +1,543 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
|
||||
#include "prompt_template.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static char* dup_range(const char* s, size_t n) {
|
||||
char* out = (char*)malloc(n + 1U);
|
||||
if (!out) return NULL;
|
||||
if (n > 0) {
|
||||
memcpy(out, s, n);
|
||||
}
|
||||
out[n] = '\0';
|
||||
return out;
|
||||
}
|
||||
|
||||
static char* ltrim_inplace(char* s) {
|
||||
if (!s) return s;
|
||||
while (*s && isspace((unsigned char)*s)) s++;
|
||||
return s;
|
||||
}
|
||||
|
||||
static void rtrim_inplace(char* s) {
|
||||
if (!s) return;
|
||||
size_t n = strlen(s);
|
||||
while (n > 0 && isspace((unsigned char)s[n - 1])) {
|
||||
s[n - 1] = '\0';
|
||||
n--;
|
||||
}
|
||||
}
|
||||
|
||||
static int starts_with(const char* s, const char* prefix) {
|
||||
if (!s || !prefix) return 0;
|
||||
size_t n = strlen(prefix);
|
||||
return strncmp(s, prefix, n) == 0;
|
||||
}
|
||||
|
||||
static int add_line(char*** lines, int* count, int* cap, char* line) {
|
||||
if (!lines || !count || !cap) return -1;
|
||||
if (*count >= *cap) {
|
||||
int next = (*cap == 0) ? 64 : (*cap * 2);
|
||||
char** grown = (char**)realloc(*lines, (size_t)next * sizeof(char*));
|
||||
if (!grown) return -1;
|
||||
*lines = grown;
|
||||
*cap = next;
|
||||
}
|
||||
(*lines)[*count] = line;
|
||||
(*count)++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int split_lines_inplace(char* s, char*** out_lines, int* out_count) {
|
||||
if (!s || !out_lines || !out_count) return -1;
|
||||
|
||||
char** lines = NULL;
|
||||
int count = 0;
|
||||
int cap = 0;
|
||||
|
||||
char* p = s;
|
||||
while (*p) {
|
||||
char* start = p;
|
||||
while (*p && *p != '\n') p++;
|
||||
if (*p == '\n') {
|
||||
*p = '\0';
|
||||
p++;
|
||||
}
|
||||
if (add_line(&lines, &count, &cap, start) != 0) {
|
||||
free(lines);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
*out_lines = lines;
|
||||
*out_count = count;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int append_text(char** buf, size_t* cap, size_t* used, const char* s) {
|
||||
if (!buf || !cap || !used || !s) return -1;
|
||||
size_t n = strlen(s);
|
||||
if (*used + n + 1U > *cap) {
|
||||
size_t next = *cap;
|
||||
while (*used + n + 1U > next) {
|
||||
next = (next == 0U) ? 256U : (next * 2U);
|
||||
}
|
||||
char* grown = (char*)realloc(*buf, next);
|
||||
if (!grown) return -1;
|
||||
*buf = grown;
|
||||
*cap = next;
|
||||
}
|
||||
memcpy(*buf + *used, s, n);
|
||||
*used += n;
|
||||
(*buf)[*used] = '\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
static char* resolve_placeholders(const char* tpl,
|
||||
prompt_var_resolver_fn resolver_fn,
|
||||
void* resolver_user_data) {
|
||||
if (!tpl) return strdup("");
|
||||
|
||||
size_t cap = strlen(tpl) + 64U;
|
||||
char* out = (char*)malloc(cap ? cap : 128U);
|
||||
if (!out) return NULL;
|
||||
out[0] = '\0';
|
||||
size_t used = 0;
|
||||
|
||||
const char* p = tpl;
|
||||
while (*p) {
|
||||
const char* open = strstr(p, "{{");
|
||||
if (!open) {
|
||||
if (append_text(&out, &cap, &used, p) != 0) {
|
||||
free(out);
|
||||
return NULL;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (open > p) {
|
||||
char* literal = dup_range(p, (size_t)(open - p));
|
||||
if (!literal) {
|
||||
free(out);
|
||||
return NULL;
|
||||
}
|
||||
int rc = append_text(&out, &cap, &used, literal);
|
||||
free(literal);
|
||||
if (rc != 0) {
|
||||
free(out);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
const char* close = strstr(open + 2, "}}");
|
||||
if (!close) {
|
||||
if (append_text(&out, &cap, &used, open) != 0) {
|
||||
free(out);
|
||||
return NULL;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
char* name = dup_range(open + 2, (size_t)(close - (open + 2)));
|
||||
if (!name) {
|
||||
free(out);
|
||||
return NULL;
|
||||
}
|
||||
char* name_trim = ltrim_inplace(name);
|
||||
rtrim_inplace(name_trim);
|
||||
|
||||
char* val = resolver_fn ? resolver_fn(name_trim, resolver_user_data) : NULL;
|
||||
if (!val) {
|
||||
val = strdup("");
|
||||
}
|
||||
|
||||
int rc = 0;
|
||||
if (val) {
|
||||
rc = append_text(&out, &cap, &used, val);
|
||||
}
|
||||
free(val);
|
||||
free(name);
|
||||
if (rc != 0) {
|
||||
free(out);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
p = close + 2;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
static void init_section_defaults(prompt_template_section_t* sec) {
|
||||
if (!sec) return;
|
||||
memset(sec->name, 0, sizeof(sec->name));
|
||||
memset(sec->role, 0, sizeof(sec->role));
|
||||
snprintf(sec->role, sizeof(sec->role), "system");
|
||||
sec->content_template = NULL;
|
||||
sec->limit = 0;
|
||||
sec->skip_if_empty = 0;
|
||||
sec->provider_name = NULL;
|
||||
sec->provider_content_template = NULL;
|
||||
}
|
||||
|
||||
static int parse_int_or_zero(const char* s) {
|
||||
if (!s) return 0;
|
||||
while (*s && isspace((unsigned char)*s)) s++;
|
||||
return atoi(s);
|
||||
}
|
||||
|
||||
int prompt_template_parse(const char* soul_content, prompt_template_t* out_template) {
|
||||
if (!soul_content || !out_template) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(out_template, 0, sizeof(*out_template));
|
||||
|
||||
const char* marker = strstr(soul_content, PROMPT_TEMPLATE_MARKER);
|
||||
if (!marker) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t personality_len = (size_t)(marker - soul_content);
|
||||
out_template->personality = dup_range(soul_content, personality_len);
|
||||
if (!out_template->personality) {
|
||||
return -1;
|
||||
}
|
||||
rtrim_inplace(out_template->personality);
|
||||
|
||||
const char* after = marker + strlen(PROMPT_TEMPLATE_MARKER);
|
||||
while (*after == '\r' || *after == '\n') after++;
|
||||
|
||||
char* tpl = strdup(after);
|
||||
if (!tpl) {
|
||||
prompt_template_free(out_template);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char** lines = NULL;
|
||||
int line_count = 0;
|
||||
if (split_lines_inplace(tpl, &lines, &line_count) != 0) {
|
||||
free(tpl);
|
||||
prompt_template_free(out_template);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int current = -1;
|
||||
int i = 0;
|
||||
while (i < line_count) {
|
||||
char* raw = lines[i];
|
||||
rtrim_inplace(raw);
|
||||
char* line = ltrim_inplace(raw);
|
||||
|
||||
if (*line == '\0') {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (starts_with(line, "- section:")) {
|
||||
if (out_template->section_count >= PROMPT_TEMPLATE_MAX_SECTIONS) {
|
||||
break;
|
||||
}
|
||||
|
||||
current = out_template->section_count;
|
||||
init_section_defaults(&out_template->sections[current]);
|
||||
out_template->section_count++;
|
||||
|
||||
char* name = line + strlen("- section:");
|
||||
name = ltrim_inplace(name);
|
||||
rtrim_inplace(name);
|
||||
snprintf(out_template->sections[current].name,
|
||||
sizeof(out_template->sections[current].name),
|
||||
"%s",
|
||||
name);
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (current < 0) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (starts_with(line, "role:")) {
|
||||
char* role = line + strlen("role:");
|
||||
role = ltrim_inplace(role);
|
||||
rtrim_inplace(role);
|
||||
snprintf(out_template->sections[current].role,
|
||||
sizeof(out_template->sections[current].role),
|
||||
"%s",
|
||||
(*role) ? role : "system");
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (starts_with(line, "limit:")) {
|
||||
char* lim = line + strlen("limit:");
|
||||
out_template->sections[current].limit = parse_int_or_zero(lim);
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (starts_with(line, "skip_if_empty:")) {
|
||||
char* flag = line + strlen("skip_if_empty:");
|
||||
flag = ltrim_inplace(flag);
|
||||
rtrim_inplace(flag);
|
||||
out_template->sections[current].skip_if_empty =
|
||||
(strcmp(flag, "true") == 0 || strcmp(flag, "1") == 0) ? 1 : 0;
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (starts_with(line, "content:")) {
|
||||
char* val = line + strlen("content:");
|
||||
val = ltrim_inplace(val);
|
||||
|
||||
free(out_template->sections[current].content_template);
|
||||
out_template->sections[current].content_template = NULL;
|
||||
|
||||
if (strcmp(val, "|") == 0) {
|
||||
char* acc = strdup("");
|
||||
size_t cap = acc ? 1U : 0U;
|
||||
size_t used = 0U;
|
||||
if (!acc) {
|
||||
free(lines);
|
||||
free(tpl);
|
||||
prompt_template_free(out_template);
|
||||
return -1;
|
||||
}
|
||||
|
||||
i++;
|
||||
while (i < line_count) {
|
||||
char* next_raw = lines[i];
|
||||
char* next_ltrim = ltrim_inplace(next_raw);
|
||||
|
||||
if (starts_with(next_ltrim, "- section:") ||
|
||||
starts_with(next_ltrim, "role:") ||
|
||||
starts_with(next_ltrim, "limit:") ||
|
||||
starts_with(next_ltrim, "skip_if_empty:") ||
|
||||
starts_with(next_ltrim, "content:") ||
|
||||
starts_with(next_ltrim, "provider:")) {
|
||||
break;
|
||||
}
|
||||
|
||||
char* piece = next_raw;
|
||||
if (strncmp(piece, " ", 4) == 0) piece += 4;
|
||||
else if (strncmp(piece, " ", 2) == 0) piece += 2;
|
||||
rtrim_inplace(piece);
|
||||
|
||||
if (append_text(&acc, &cap, &used, piece) != 0 ||
|
||||
append_text(&acc, &cap, &used, "\n") != 0) {
|
||||
free(acc);
|
||||
free(lines);
|
||||
free(tpl);
|
||||
prompt_template_free(out_template);
|
||||
return -1;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
rtrim_inplace(acc);
|
||||
out_template->sections[current].content_template = acc;
|
||||
continue;
|
||||
}
|
||||
|
||||
rtrim_inplace(val);
|
||||
out_template->sections[current].content_template = strdup(val);
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (starts_with(line, "provider:")) {
|
||||
i++;
|
||||
if (i >= line_count) continue;
|
||||
|
||||
char* provider_line = ltrim_inplace(lines[i]);
|
||||
rtrim_inplace(provider_line);
|
||||
char* colon = strchr(provider_line, ':');
|
||||
if (!colon) {
|
||||
continue;
|
||||
}
|
||||
|
||||
*colon = '\0';
|
||||
char* provider_name = ltrim_inplace(provider_line);
|
||||
rtrim_inplace(provider_name);
|
||||
char* provider_val = ltrim_inplace(colon + 1);
|
||||
rtrim_inplace(provider_val);
|
||||
|
||||
free(out_template->sections[current].provider_name);
|
||||
out_template->sections[current].provider_name = strdup(provider_name);
|
||||
free(out_template->sections[current].provider_content_template);
|
||||
out_template->sections[current].provider_content_template = NULL;
|
||||
|
||||
if (strcmp(provider_val, "|") == 0) {
|
||||
char* acc = strdup("");
|
||||
size_t cap = acc ? 1U : 0U;
|
||||
size_t used = 0U;
|
||||
if (!acc) {
|
||||
free(lines);
|
||||
free(tpl);
|
||||
prompt_template_free(out_template);
|
||||
return -1;
|
||||
}
|
||||
|
||||
i++;
|
||||
while (i < line_count) {
|
||||
char* next = ltrim_inplace(lines[i]);
|
||||
if (starts_with(next, "- section:") ||
|
||||
starts_with(next, "role:") ||
|
||||
starts_with(next, "limit:") ||
|
||||
starts_with(next, "skip_if_empty:") ||
|
||||
starts_with(next, "content:") ||
|
||||
starts_with(next, "provider:")) {
|
||||
break;
|
||||
}
|
||||
char* piece = lines[i];
|
||||
if (strncmp(piece, " ", 4) == 0) piece += 4;
|
||||
else if (strncmp(piece, " ", 2) == 0) piece += 2;
|
||||
rtrim_inplace(piece);
|
||||
if (append_text(&acc, &cap, &used, piece) != 0 ||
|
||||
append_text(&acc, &cap, &used, "\n") != 0) {
|
||||
free(acc);
|
||||
free(lines);
|
||||
free(tpl);
|
||||
prompt_template_free(out_template);
|
||||
return -1;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
rtrim_inplace(acc);
|
||||
out_template->sections[current].provider_content_template = acc;
|
||||
continue;
|
||||
}
|
||||
|
||||
out_template->sections[current].provider_content_template = strdup(provider_val);
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
free(lines);
|
||||
free(tpl);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int append_message_object(cJSON* messages, const char* role, const char* content) {
|
||||
if (!messages || !role) return -1;
|
||||
cJSON* msg = cJSON_CreateObject();
|
||||
if (!msg) return -1;
|
||||
cJSON_AddStringToObject(msg, "role", role);
|
||||
cJSON_AddStringToObject(msg, "content", content ? content : "");
|
||||
cJSON_AddItemToArray(messages, msg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
cJSON* prompt_template_build_messages(const prompt_template_t* tmpl,
|
||||
const char* provider_name,
|
||||
prompt_var_resolver_fn resolver_fn,
|
||||
void* resolver_user_data,
|
||||
cJSON* dm_history_messages,
|
||||
int dm_history_default_limit,
|
||||
prompt_template_emit_hook_fn emit_hook,
|
||||
void* emit_hook_user_data) {
|
||||
if (!tmpl) return NULL;
|
||||
|
||||
cJSON* out = cJSON_CreateArray();
|
||||
if (!out) return NULL;
|
||||
|
||||
int out_idx = 0;
|
||||
if (tmpl->personality && tmpl->personality[0] != '\0') {
|
||||
if (append_message_object(out, "system", tmpl->personality) != 0) {
|
||||
cJSON_Delete(out);
|
||||
return NULL;
|
||||
}
|
||||
if (emit_hook) emit_hook("system_prompt", out_idx, emit_hook_user_data);
|
||||
out_idx++;
|
||||
}
|
||||
|
||||
for (int i = 0; i < tmpl->section_count; i++) {
|
||||
const prompt_template_section_t* sec = &tmpl->sections[i];
|
||||
const char* role = sec->role[0] ? sec->role : "system";
|
||||
|
||||
if (strcmp(role, "expand") == 0) {
|
||||
if (!dm_history_messages || !cJSON_IsArray(dm_history_messages)) {
|
||||
continue;
|
||||
}
|
||||
int total = cJSON_GetArraySize(dm_history_messages);
|
||||
int lim = sec->limit > 0 ? sec->limit : dm_history_default_limit;
|
||||
int start = (lim > 0 && total > lim) ? (total - lim) : 0;
|
||||
|
||||
for (int j = start; j < total; j++) {
|
||||
cJSON* item = cJSON_GetArrayItem(dm_history_messages, j);
|
||||
if (!item || !cJSON_IsObject(item)) continue;
|
||||
cJSON* dup = cJSON_Duplicate(item, 1);
|
||||
if (!dup) {
|
||||
cJSON_Delete(out);
|
||||
return NULL;
|
||||
}
|
||||
cJSON_AddItemToArray(out, dup);
|
||||
if (emit_hook) emit_hook(sec->name[0] ? sec->name : "context_part", out_idx, emit_hook_user_data);
|
||||
out_idx++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
const char* tpl = sec->content_template;
|
||||
if (provider_name && sec->provider_name && sec->provider_content_template &&
|
||||
strcmp(provider_name, sec->provider_name) == 0) {
|
||||
tpl = sec->provider_content_template;
|
||||
}
|
||||
|
||||
char* resolved = resolve_placeholders(tpl ? tpl : "", resolver_fn, resolver_user_data);
|
||||
if (!resolved) {
|
||||
cJSON_Delete(out);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (sec->skip_if_empty) {
|
||||
char* chk = ltrim_inplace(resolved);
|
||||
if (chk && *chk == '\0') {
|
||||
free(resolved);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (append_message_object(out, role, resolved) != 0) {
|
||||
free(resolved);
|
||||
cJSON_Delete(out);
|
||||
return NULL;
|
||||
}
|
||||
if (emit_hook) emit_hook(sec->name[0] ? sec->name : "context_part", out_idx, emit_hook_user_data);
|
||||
out_idx++;
|
||||
free(resolved);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void prompt_template_free(prompt_template_t* tmpl) {
|
||||
if (!tmpl) return;
|
||||
|
||||
free(tmpl->personality);
|
||||
tmpl->personality = NULL;
|
||||
|
||||
for (int i = 0; i < tmpl->section_count; i++) {
|
||||
free(tmpl->sections[i].content_template);
|
||||
tmpl->sections[i].content_template = NULL;
|
||||
free(tmpl->sections[i].provider_name);
|
||||
tmpl->sections[i].provider_name = NULL;
|
||||
free(tmpl->sections[i].provider_content_template);
|
||||
tmpl->sections[i].provider_content_template = NULL;
|
||||
tmpl->sections[i].name[0] = '\0';
|
||||
tmpl->sections[i].role[0] = '\0';
|
||||
tmpl->sections[i].limit = 0;
|
||||
tmpl->sections[i].skip_if_empty = 0;
|
||||
}
|
||||
|
||||
tmpl->section_count = 0;
|
||||
}
|
||||
45
src/prompt_template.h
Normal file
45
src/prompt_template.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#ifndef DIDACTYL_PROMPT_TEMPLATE_H
|
||||
#define DIDACTYL_PROMPT_TEMPLATE_H
|
||||
|
||||
#include "cjson/cJSON.h"
|
||||
|
||||
#define PROMPT_TEMPLATE_MAX_SECTIONS 32
|
||||
#define PROMPT_TEMPLATE_MAX_NAME_LEN 64
|
||||
#define PROMPT_TEMPLATE_MAX_ROLE_LEN 16
|
||||
#define PROMPT_TEMPLATE_MARKER "---template---"
|
||||
|
||||
typedef struct {
|
||||
char name[PROMPT_TEMPLATE_MAX_NAME_LEN];
|
||||
char role[PROMPT_TEMPLATE_MAX_ROLE_LEN];
|
||||
char* content_template;
|
||||
int limit;
|
||||
int skip_if_empty;
|
||||
char* provider_name;
|
||||
char* provider_content_template;
|
||||
} prompt_template_section_t;
|
||||
|
||||
typedef struct {
|
||||
char* personality;
|
||||
prompt_template_section_t sections[PROMPT_TEMPLATE_MAX_SECTIONS];
|
||||
int section_count;
|
||||
} prompt_template_t;
|
||||
|
||||
typedef char* (*prompt_var_resolver_fn)(const char* var_name, void* user_data);
|
||||
typedef void (*prompt_template_emit_hook_fn)(const char* section_name,
|
||||
int message_index,
|
||||
void* user_data);
|
||||
|
||||
int prompt_template_parse(const char* soul_content, prompt_template_t* out_template);
|
||||
|
||||
cJSON* prompt_template_build_messages(const prompt_template_t* tmpl,
|
||||
const char* provider_name,
|
||||
prompt_var_resolver_fn resolver_fn,
|
||||
void* resolver_user_data,
|
||||
cJSON* dm_history_messages,
|
||||
int dm_history_default_limit,
|
||||
prompt_template_emit_hook_fn emit_hook,
|
||||
void* emit_hook_user_data);
|
||||
|
||||
void prompt_template_free(prompt_template_t* tmpl);
|
||||
|
||||
#endif
|
||||
5040
src/tools.c
5040
src/tools.c
File diff suppressed because it is too large
Load Diff
@@ -3,8 +3,11 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
struct trigger_manager;
|
||||
|
||||
typedef struct {
|
||||
didactyl_config_t* cfg;
|
||||
struct trigger_manager* trigger_manager;
|
||||
} tools_context_t;
|
||||
|
||||
int tools_init(tools_context_t* ctx, didactyl_config_t* cfg);
|
||||
|
||||
649
src/trigger_manager.c
Normal file
649
src/trigger_manager.c
Normal file
@@ -0,0 +1,649 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
|
||||
#include "trigger_manager.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "agent.h"
|
||||
#include "cjson/cJSON.h"
|
||||
#include "debug.h"
|
||||
#include "nostr_handler.h"
|
||||
|
||||
static int clamp_enabled(int enabled) {
|
||||
return enabled ? 1 : 0;
|
||||
}
|
||||
|
||||
static int ensure_capacity(trigger_manager_t* mgr, int needed) {
|
||||
if (!mgr || needed <= 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (mgr->capacity >= needed) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int new_cap = mgr->capacity > 0 ? mgr->capacity : TRIGGER_DEFAULT_MAX_ACTIVE;
|
||||
while (new_cap < needed) {
|
||||
if (new_cap > 1024) {
|
||||
return -1;
|
||||
}
|
||||
new_cap *= 2;
|
||||
}
|
||||
|
||||
active_trigger_t* grown = (active_trigger_t*)realloc(mgr->triggers, (size_t)new_cap * sizeof(active_trigger_t));
|
||||
if (!grown) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (new_cap > mgr->capacity) {
|
||||
memset(&grown[mgr->capacity], 0, (size_t)(new_cap - mgr->capacity) * sizeof(active_trigger_t));
|
||||
}
|
||||
|
||||
mgr->triggers = grown;
|
||||
mgr->capacity = new_cap;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int find_trigger_index_locked(trigger_manager_t* mgr, const char* skill_slug) {
|
||||
if (!mgr || !skill_slug || skill_slug[0] == '\0') {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < mgr->count; i++) {
|
||||
if (strncmp(mgr->triggers[i].skill_slug, skill_slug, TRIGGER_SKILL_SLUG_MAX) == 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static cJSON* find_tag_value_string(cJSON* tags, const char* key) {
|
||||
if (!tags || !key || !cJSON_IsArray(tags)) return NULL;
|
||||
|
||||
int n = cJSON_GetArraySize(tags);
|
||||
for (int i = 0; i < n; i++) {
|
||||
cJSON* tag = cJSON_GetArrayItem(tags, i);
|
||||
if (!tag || !cJSON_IsArray(tag) || cJSON_GetArraySize(tag) < 2) continue;
|
||||
|
||||
cJSON* k = cJSON_GetArrayItem(tag, 0);
|
||||
cJSON* v = cJSON_GetArrayItem(tag, 1);
|
||||
if (k && v && cJSON_IsString(k) && cJSON_IsString(v) &&
|
||||
k->valuestring && v->valuestring && strcmp(k->valuestring, key) == 0) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int parse_address_tag(const char* addr, int* out_kind, char out_pubkey[65], char out_slug[65]) {
|
||||
if (!addr || !out_kind || !out_pubkey || !out_slug) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char* p1 = strchr(addr, ':');
|
||||
if (!p1) return -1;
|
||||
const char* p2 = strchr(p1 + 1, ':');
|
||||
if (!p2) return -1;
|
||||
|
||||
char kind_buf[16] = {0};
|
||||
size_t kind_len = (size_t)(p1 - addr);
|
||||
if (kind_len == 0 || kind_len >= sizeof(kind_buf)) return -1;
|
||||
memcpy(kind_buf, addr, kind_len);
|
||||
|
||||
int kind = atoi(kind_buf);
|
||||
if (kind != 31123 && kind != 31124) return -1;
|
||||
|
||||
size_t pub_len = (size_t)(p2 - (p1 + 1));
|
||||
if (pub_len != 64U) return -1;
|
||||
memcpy(out_pubkey, p1 + 1, 64U);
|
||||
out_pubkey[64] = '\0';
|
||||
|
||||
size_t slug_len = strlen(p2 + 1);
|
||||
if (slug_len == 0 || slug_len >= 65U) return -1;
|
||||
memcpy(out_slug, p2 + 1, slug_len + 1U);
|
||||
|
||||
*out_kind = kind;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static char* build_template_output(const active_trigger_t* t, cJSON* event, const char* relay_url) {
|
||||
(void)relay_url;
|
||||
|
||||
if (!t || !event) return NULL;
|
||||
|
||||
cJSON* content = cJSON_GetObjectItemCaseSensitive(event, "content");
|
||||
cJSON* pubkey = cJSON_GetObjectItemCaseSensitive(event, "pubkey");
|
||||
cJSON* id = cJSON_GetObjectItemCaseSensitive(event, "id");
|
||||
cJSON* kind = cJSON_GetObjectItemCaseSensitive(event, "kind");
|
||||
|
||||
const char* content_s = (content && cJSON_IsString(content) && content->valuestring) ? content->valuestring : "";
|
||||
const char* pubkey_s = (pubkey && cJSON_IsString(pubkey) && pubkey->valuestring) ? pubkey->valuestring : "unknown";
|
||||
const char* id_s = (id && cJSON_IsString(id) && id->valuestring) ? id->valuestring : "";
|
||||
int kind_i = (kind && cJSON_IsNumber(kind)) ? (int)kind->valuedouble : -1;
|
||||
|
||||
const char* tmpl = t->skill_content;
|
||||
if (!tmpl || tmpl[0] == '\0') {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char out[TRIGGER_SKILL_CONTENT_MAX + 256];
|
||||
snprintf(out,
|
||||
sizeof(out),
|
||||
"%s\n\n[event id=%s kind=%d pubkey=%s]\n%s",
|
||||
tmpl,
|
||||
id_s,
|
||||
kind_i,
|
||||
pubkey_s,
|
||||
content_s);
|
||||
|
||||
return strdup(out);
|
||||
}
|
||||
|
||||
static void execute_template_action(trigger_manager_t* mgr,
|
||||
const active_trigger_t* t,
|
||||
cJSON* event,
|
||||
const char* relay_url) {
|
||||
if (!mgr || !mgr->cfg || !t || !event) {
|
||||
return;
|
||||
}
|
||||
|
||||
char* rendered = build_template_output(t, event, relay_url);
|
||||
if (!rendered) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (strncmp(rendered, "DM admin:", 9) == 0) {
|
||||
const char* body = rendered + 9;
|
||||
while (*body == ' ') body++;
|
||||
(void)nostr_handler_send_dm_auto(mgr->cfg->admin.pubkey, body);
|
||||
} else if (strncmp(rendered, "POST:", 5) == 0) {
|
||||
const char* body = rendered + 5;
|
||||
while (*body == ' ') body++;
|
||||
(void)nostr_handler_publish_kind_event(1, body, NULL, NULL);
|
||||
} else if (strncmp(rendered, "LOG:", 4) == 0) {
|
||||
const char* body = rendered + 4;
|
||||
while (*body == ' ') body++;
|
||||
DEBUG_INFO("[didactyl] trigger template log (%s): %s", t->skill_slug, body);
|
||||
} else {
|
||||
(void)nostr_handler_send_dm_auto(mgr->cfg->admin.pubkey, rendered);
|
||||
}
|
||||
|
||||
free(rendered);
|
||||
}
|
||||
|
||||
static void execute_llm_action(const active_trigger_t* t, cJSON* event, const char* relay_url) {
|
||||
if (!t || !event) {
|
||||
return;
|
||||
}
|
||||
agent_on_trigger(t->skill_slug, t->skill_content, event, relay_url);
|
||||
}
|
||||
|
||||
static int maybe_fire_trigger_locked(trigger_manager_t* mgr, int index, cJSON* event, const char* relay_url) {
|
||||
active_trigger_t* t = &mgr->triggers[index];
|
||||
if (!t->enabled) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
cJSON* created_at = cJSON_GetObjectItemCaseSensitive(event, "created_at");
|
||||
time_t created_ts = (created_at && cJSON_IsNumber(created_at)) ? (time_t)created_at->valuedouble : 0;
|
||||
if (created_ts > 0 && created_ts <= t->last_seen_created_at) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
time_t now = time(NULL);
|
||||
int cooldown = mgr->cfg->triggers.cooldown_seconds;
|
||||
if (cooldown < 0) cooldown = 0;
|
||||
if (cooldown > 0 && t->last_fired > 0 && (now - t->last_fired) < cooldown) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
t->last_fired = now;
|
||||
if (created_ts > t->last_seen_created_at) {
|
||||
t->last_seen_created_at = created_ts;
|
||||
}
|
||||
|
||||
active_trigger_t trigger_copy = *t;
|
||||
pthread_mutex_unlock(&mgr->mutex);
|
||||
|
||||
if (trigger_copy.action_type == TRIGGER_ACTION_TEMPLATE) {
|
||||
execute_template_action(mgr, &trigger_copy, event, relay_url);
|
||||
} else {
|
||||
execute_llm_action(&trigger_copy, event, relay_url);
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&mgr->mutex);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int trigger_manager_init(trigger_manager_t* mgr, didactyl_config_t* cfg) {
|
||||
if (!mgr || !cfg) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(mgr, 0, sizeof(*mgr));
|
||||
mgr->cfg = cfg;
|
||||
|
||||
if (pthread_mutex_init(&mgr->mutex, NULL) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int initial_cap = cfg->triggers.max_active > 0 ? cfg->triggers.max_active : TRIGGER_DEFAULT_MAX_ACTIVE;
|
||||
mgr->capacity = initial_cap;
|
||||
mgr->triggers = (active_trigger_t*)calloc((size_t)mgr->capacity, sizeof(active_trigger_t));
|
||||
if (!mgr->triggers) {
|
||||
pthread_mutex_destroy(&mgr->mutex);
|
||||
memset(mgr, 0, sizeof(*mgr));
|
||||
return -1;
|
||||
}
|
||||
|
||||
mgr->last_poll_at = time(NULL);
|
||||
|
||||
DEBUG_INFO("[didactyl] trigger manager initialized (capacity=%d)", mgr->capacity);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int trigger_manager_load_from_skills(trigger_manager_t* mgr) {
|
||||
if (!mgr || !mgr->cfg) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
cJSON* adopt_filter = cJSON_CreateObject();
|
||||
cJSON* kinds = cJSON_CreateArray();
|
||||
cJSON* authors = cJSON_CreateArray();
|
||||
if (!adopt_filter || !kinds || !authors) {
|
||||
cJSON_Delete(adopt_filter);
|
||||
cJSON_Delete(kinds);
|
||||
cJSON_Delete(authors);
|
||||
return -1;
|
||||
}
|
||||
|
||||
cJSON_AddItemToArray(kinds, cJSON_CreateNumber(10123));
|
||||
cJSON_AddItemToObject(adopt_filter, "kinds", kinds);
|
||||
cJSON_AddItemToArray(authors, cJSON_CreateString(mgr->cfg->keys.public_key_hex));
|
||||
cJSON_AddItemToObject(adopt_filter, "authors", authors);
|
||||
cJSON_AddNumberToObject(adopt_filter, "limit", 1);
|
||||
|
||||
char* adoption_json = nostr_handler_query_json(adopt_filter, 8000);
|
||||
cJSON_Delete(adopt_filter);
|
||||
if (!adoption_json) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
cJSON* adoption_events = cJSON_Parse(adoption_json);
|
||||
free(adoption_json);
|
||||
if (!adoption_events || !cJSON_IsArray(adoption_events) || cJSON_GetArraySize(adoption_events) <= 0) {
|
||||
cJSON_Delete(adoption_events);
|
||||
return 0;
|
||||
}
|
||||
|
||||
cJSON* list_event = cJSON_GetArrayItem(adoption_events, 0);
|
||||
cJSON* list_tags = list_event ? cJSON_GetObjectItemCaseSensitive(list_event, "tags") : NULL;
|
||||
if (!list_tags || !cJSON_IsArray(list_tags)) {
|
||||
cJSON_Delete(adoption_events);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int loaded = 0;
|
||||
int tn = cJSON_GetArraySize(list_tags);
|
||||
for (int i = 0; i < tn; i++) {
|
||||
cJSON* tag = cJSON_GetArrayItem(list_tags, i);
|
||||
if (!tag || !cJSON_IsArray(tag) || cJSON_GetArraySize(tag) < 2) continue;
|
||||
|
||||
cJSON* key = cJSON_GetArrayItem(tag, 0);
|
||||
cJSON* val = cJSON_GetArrayItem(tag, 1);
|
||||
if (!key || !val || !cJSON_IsString(key) || !cJSON_IsString(val) ||
|
||||
!key->valuestring || !val->valuestring || strcmp(key->valuestring, "a") != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int kind = 0;
|
||||
char pubkey[65] = {0};
|
||||
char slug[65] = {0};
|
||||
if (parse_address_tag(val->valuestring, &kind, pubkey, slug) != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
cJSON* skill_filter = cJSON_CreateObject();
|
||||
cJSON* sk_kinds = cJSON_CreateArray();
|
||||
cJSON* sk_authors = cJSON_CreateArray();
|
||||
cJSON* d_values = cJSON_CreateArray();
|
||||
if (!skill_filter || !sk_kinds || !sk_authors || !d_values) {
|
||||
cJSON_Delete(skill_filter);
|
||||
cJSON_Delete(sk_kinds);
|
||||
cJSON_Delete(sk_authors);
|
||||
cJSON_Delete(d_values);
|
||||
continue;
|
||||
}
|
||||
|
||||
cJSON_AddItemToArray(sk_kinds, cJSON_CreateNumber(kind));
|
||||
cJSON_AddItemToObject(skill_filter, "kinds", sk_kinds);
|
||||
cJSON_AddItemToArray(sk_authors, cJSON_CreateString(pubkey));
|
||||
cJSON_AddItemToObject(skill_filter, "authors", sk_authors);
|
||||
cJSON_AddItemToArray(d_values, cJSON_CreateString(slug));
|
||||
cJSON_AddItemToObject(skill_filter, "#d", d_values);
|
||||
cJSON_AddNumberToObject(skill_filter, "limit", 1);
|
||||
|
||||
char* skill_json = nostr_handler_query_json(skill_filter, 8000);
|
||||
cJSON_Delete(skill_filter);
|
||||
if (!skill_json) {
|
||||
continue;
|
||||
}
|
||||
|
||||
cJSON* skill_events = cJSON_Parse(skill_json);
|
||||
free(skill_json);
|
||||
if (!skill_events || !cJSON_IsArray(skill_events) || cJSON_GetArraySize(skill_events) <= 0) {
|
||||
cJSON_Delete(skill_events);
|
||||
continue;
|
||||
}
|
||||
|
||||
cJSON* skill_event = cJSON_GetArrayItem(skill_events, 0);
|
||||
cJSON* content = skill_event ? cJSON_GetObjectItemCaseSensitive(skill_event, "content") : NULL;
|
||||
cJSON* tags = skill_event ? cJSON_GetObjectItemCaseSensitive(skill_event, "tags") : NULL;
|
||||
if (!content || !cJSON_IsString(content) || !content->valuestring || !tags || !cJSON_IsArray(tags)) {
|
||||
cJSON_Delete(skill_events);
|
||||
continue;
|
||||
}
|
||||
|
||||
cJSON* trigger = find_tag_value_string(tags, "trigger");
|
||||
cJSON* filter = find_tag_value_string(tags, "filter");
|
||||
cJSON* action = find_tag_value_string(tags, "action");
|
||||
cJSON* enabled = find_tag_value_string(tags, "enabled");
|
||||
|
||||
const char* trigger_s = (trigger && cJSON_IsString(trigger) && trigger->valuestring) ? trigger->valuestring : NULL;
|
||||
const char* filter_s = (filter && cJSON_IsString(filter) && filter->valuestring) ? filter->valuestring : NULL;
|
||||
const char* action_s = (action && cJSON_IsString(action) && action->valuestring) ? action->valuestring : "llm";
|
||||
const char* enabled_s = (enabled && cJSON_IsString(enabled) && enabled->valuestring) ? enabled->valuestring : "true";
|
||||
|
||||
if (trigger_s && strcmp(trigger_s, "nostr-subscription") == 0 && filter_s && filter_s[0] != '\0') {
|
||||
trigger_action_type_t at = (strcmp(action_s, "template") == 0) ? TRIGGER_ACTION_TEMPLATE : TRIGGER_ACTION_LLM;
|
||||
int is_enabled = (strcmp(enabled_s, "false") == 0 || strcmp(enabled_s, "0") == 0) ? 0 : 1;
|
||||
if (trigger_manager_add(mgr, slug, content->valuestring, filter_s, at, is_enabled) == 0) {
|
||||
loaded++;
|
||||
}
|
||||
}
|
||||
|
||||
cJSON_Delete(skill_events);
|
||||
}
|
||||
|
||||
cJSON_Delete(adoption_events);
|
||||
DEBUG_INFO("[didactyl] trigger manager loaded %d trigger(s) from skills", loaded);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int trigger_manager_add(trigger_manager_t* mgr,
|
||||
const char* skill_slug,
|
||||
const char* content,
|
||||
const char* filter_json,
|
||||
trigger_action_type_t action_type,
|
||||
int enabled) {
|
||||
if (!mgr || !skill_slug || skill_slug[0] == '\0' || !content || !filter_json) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (action_type != TRIGGER_ACTION_LLM && action_type != TRIGGER_ACTION_TEMPLATE) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&mgr->mutex);
|
||||
|
||||
int existing = find_trigger_index_locked(mgr, skill_slug);
|
||||
if (existing >= 0) {
|
||||
pthread_mutex_unlock(&mgr->mutex);
|
||||
return trigger_manager_update(mgr, skill_slug, content, filter_json, action_type, enabled);
|
||||
}
|
||||
|
||||
int max_active = mgr->cfg ? mgr->cfg->triggers.max_active : TRIGGER_DEFAULT_MAX_ACTIVE;
|
||||
if (max_active < 1) max_active = TRIGGER_DEFAULT_MAX_ACTIVE;
|
||||
if (mgr->count >= max_active) {
|
||||
pthread_mutex_unlock(&mgr->mutex);
|
||||
DEBUG_WARN("[didactyl] trigger add rejected: max_active reached (%d)", max_active);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ensure_capacity(mgr, mgr->count + 1) != 0) {
|
||||
pthread_mutex_unlock(&mgr->mutex);
|
||||
return -1;
|
||||
}
|
||||
|
||||
active_trigger_t* t = &mgr->triggers[mgr->count];
|
||||
memset(t, 0, sizeof(*t));
|
||||
snprintf(t->skill_slug, sizeof(t->skill_slug), "%s", skill_slug);
|
||||
snprintf(t->skill_content, sizeof(t->skill_content), "%s", content);
|
||||
snprintf(t->filter_json, sizeof(t->filter_json), "%s", filter_json);
|
||||
t->action_type = action_type;
|
||||
t->enabled = clamp_enabled(enabled);
|
||||
t->last_fired = 0;
|
||||
t->last_seen_created_at = 0;
|
||||
|
||||
mgr->count++;
|
||||
|
||||
pthread_mutex_unlock(&mgr->mutex);
|
||||
|
||||
DEBUG_INFO("[didactyl] trigger added slug=%s action=%d enabled=%d", skill_slug, (int)action_type, clamp_enabled(enabled));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int trigger_manager_remove(trigger_manager_t* mgr, const char* skill_slug) {
|
||||
if (!mgr || !skill_slug || skill_slug[0] == '\0') {
|
||||
return -1;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&mgr->mutex);
|
||||
|
||||
int idx = find_trigger_index_locked(mgr, skill_slug);
|
||||
if (idx < 0) {
|
||||
pthread_mutex_unlock(&mgr->mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (idx < mgr->count - 1) {
|
||||
memmove(&mgr->triggers[idx],
|
||||
&mgr->triggers[idx + 1],
|
||||
(size_t)(mgr->count - idx - 1) * sizeof(active_trigger_t));
|
||||
}
|
||||
|
||||
mgr->count--;
|
||||
memset(&mgr->triggers[mgr->count], 0, sizeof(active_trigger_t));
|
||||
|
||||
pthread_mutex_unlock(&mgr->mutex);
|
||||
|
||||
DEBUG_INFO("[didactyl] trigger removed slug=%s", skill_slug);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int trigger_manager_update(trigger_manager_t* mgr,
|
||||
const char* skill_slug,
|
||||
const char* content,
|
||||
const char* filter_json,
|
||||
trigger_action_type_t action_type,
|
||||
int enabled) {
|
||||
if (!mgr || !skill_slug || skill_slug[0] == '\0' || !content || !filter_json) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (action_type != TRIGGER_ACTION_LLM && action_type != TRIGGER_ACTION_TEMPLATE) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&mgr->mutex);
|
||||
|
||||
int idx = find_trigger_index_locked(mgr, skill_slug);
|
||||
if (idx < 0) {
|
||||
pthread_mutex_unlock(&mgr->mutex);
|
||||
return trigger_manager_add(mgr, skill_slug, content, filter_json, action_type, enabled);
|
||||
}
|
||||
|
||||
active_trigger_t* t = &mgr->triggers[idx];
|
||||
snprintf(t->skill_content, sizeof(t->skill_content), "%s", content);
|
||||
snprintf(t->filter_json, sizeof(t->filter_json), "%s", filter_json);
|
||||
t->action_type = action_type;
|
||||
t->enabled = clamp_enabled(enabled);
|
||||
|
||||
pthread_mutex_unlock(&mgr->mutex);
|
||||
|
||||
DEBUG_INFO("[didactyl] trigger updated slug=%s action=%d enabled=%d", skill_slug, (int)action_type, clamp_enabled(enabled));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int trigger_manager_active_count(trigger_manager_t* mgr) {
|
||||
if (!mgr) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int active = 0;
|
||||
pthread_mutex_lock(&mgr->mutex);
|
||||
for (int i = 0; i < mgr->count; i++) {
|
||||
if (mgr->triggers[i].enabled) {
|
||||
active++;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&mgr->mutex);
|
||||
|
||||
return active;
|
||||
}
|
||||
|
||||
int trigger_manager_poll(trigger_manager_t* mgr) {
|
||||
if (!mgr || !mgr->cfg || !mgr->cfg->triggers.enabled) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
time_t now = time(NULL);
|
||||
if (mgr->last_poll_at > 0 && (now - mgr->last_poll_at) < 2) {
|
||||
return 0;
|
||||
}
|
||||
mgr->last_poll_at = now;
|
||||
|
||||
pthread_mutex_lock(&mgr->mutex);
|
||||
int count = mgr->count;
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
active_trigger_t snapshot = mgr->triggers[i];
|
||||
if (!snapshot.enabled || snapshot.filter_json[0] == '\0') {
|
||||
continue;
|
||||
}
|
||||
|
||||
cJSON* filter = cJSON_Parse(snapshot.filter_json);
|
||||
if (!filter || !cJSON_IsObject(filter)) {
|
||||
cJSON_Delete(filter);
|
||||
continue;
|
||||
}
|
||||
|
||||
time_t since = snapshot.last_seen_created_at > 0 ? snapshot.last_seen_created_at + 1 : now - 30;
|
||||
if (since < 0) since = 0;
|
||||
cJSON_AddNumberToObject(filter, "since", (double)since);
|
||||
cJSON_AddNumberToObject(filter, "limit", 8);
|
||||
|
||||
char* events_json = nostr_handler_query_json(filter, 4000);
|
||||
cJSON_Delete(filter);
|
||||
if (!events_json) {
|
||||
continue;
|
||||
}
|
||||
|
||||
cJSON* events = cJSON_Parse(events_json);
|
||||
free(events_json);
|
||||
if (!events || !cJSON_IsArray(events)) {
|
||||
cJSON_Delete(events);
|
||||
continue;
|
||||
}
|
||||
|
||||
int n = cJSON_GetArraySize(events);
|
||||
for (int e = 0; e < n; e++) {
|
||||
cJSON* ev = cJSON_GetArrayItem(events, e);
|
||||
if (!ev || !cJSON_IsObject(ev)) continue;
|
||||
|
||||
int idx = find_trigger_index_locked(mgr, snapshot.skill_slug);
|
||||
if (idx < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
(void)maybe_fire_trigger_locked(mgr, idx, ev, NULL);
|
||||
}
|
||||
|
||||
cJSON_Delete(events);
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&mgr->mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
char* trigger_manager_status_json(trigger_manager_t* mgr) {
|
||||
if (!mgr) {
|
||||
cJSON* err = cJSON_CreateObject();
|
||||
if (!err) {
|
||||
return NULL;
|
||||
}
|
||||
cJSON_AddBoolToObject(err, "success", 0);
|
||||
cJSON_AddStringToObject(err, "error", "trigger manager unavailable");
|
||||
char* out = cJSON_PrintUnformatted(err);
|
||||
cJSON_Delete(err);
|
||||
return out;
|
||||
}
|
||||
|
||||
cJSON* root = cJSON_CreateObject();
|
||||
cJSON* arr = cJSON_CreateArray();
|
||||
if (!root || !arr) {
|
||||
cJSON_Delete(root);
|
||||
cJSON_Delete(arr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&mgr->mutex);
|
||||
|
||||
cJSON_AddBoolToObject(root, "success", 1);
|
||||
cJSON_AddNumberToObject(root, "count", mgr->count);
|
||||
|
||||
int active = 0;
|
||||
for (int i = 0; i < mgr->count; i++) {
|
||||
active_trigger_t* t = &mgr->triggers[i];
|
||||
if (t->enabled) {
|
||||
active++;
|
||||
}
|
||||
|
||||
cJSON* item = cJSON_CreateObject();
|
||||
if (!item) {
|
||||
continue;
|
||||
}
|
||||
|
||||
cJSON_AddStringToObject(item, "skill_slug", t->skill_slug);
|
||||
cJSON_AddStringToObject(item, "filter_json", t->filter_json);
|
||||
cJSON_AddStringToObject(item, "action", t->action_type == TRIGGER_ACTION_TEMPLATE ? "template" : "llm");
|
||||
cJSON_AddBoolToObject(item, "enabled", t->enabled ? 1 : 0);
|
||||
cJSON_AddNumberToObject(item, "last_fired", (double)t->last_fired);
|
||||
cJSON_AddNumberToObject(item, "last_seen_created_at", (double)t->last_seen_created_at);
|
||||
cJSON_AddItemToArray(arr, item);
|
||||
}
|
||||
|
||||
cJSON_AddNumberToObject(root, "active", active);
|
||||
cJSON_AddItemToObject(root, "triggers", arr);
|
||||
|
||||
pthread_mutex_unlock(&mgr->mutex);
|
||||
|
||||
char* out = cJSON_PrintUnformatted(root);
|
||||
cJSON_Delete(root);
|
||||
return out;
|
||||
}
|
||||
|
||||
void trigger_manager_cleanup(trigger_manager_t* mgr) {
|
||||
if (!mgr) {
|
||||
return;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&mgr->mutex);
|
||||
free(mgr->triggers);
|
||||
mgr->triggers = NULL;
|
||||
mgr->count = 0;
|
||||
mgr->capacity = 0;
|
||||
mgr->cfg = NULL;
|
||||
mgr->last_poll_at = 0;
|
||||
pthread_mutex_unlock(&mgr->mutex);
|
||||
|
||||
pthread_mutex_destroy(&mgr->mutex);
|
||||
memset(mgr, 0, sizeof(*mgr));
|
||||
|
||||
DEBUG_INFO("[didactyl] trigger manager cleaned up");
|
||||
}
|
||||
58
src/trigger_manager.h
Normal file
58
src/trigger_manager.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#ifndef DIDACTYL_TRIGGER_MANAGER_H
|
||||
#define DIDACTYL_TRIGGER_MANAGER_H
|
||||
|
||||
#include <pthread.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#define TRIGGER_DEFAULT_MAX_ACTIVE 16
|
||||
#define TRIGGER_SKILL_SLUG_MAX 65
|
||||
#define TRIGGER_SKILL_CONTENT_MAX 4096
|
||||
#define TRIGGER_FILTER_JSON_MAX 2048
|
||||
|
||||
typedef enum {
|
||||
TRIGGER_ACTION_LLM = 0,
|
||||
TRIGGER_ACTION_TEMPLATE = 1
|
||||
} trigger_action_type_t;
|
||||
|
||||
typedef struct {
|
||||
char skill_slug[TRIGGER_SKILL_SLUG_MAX];
|
||||
char skill_content[TRIGGER_SKILL_CONTENT_MAX];
|
||||
char filter_json[TRIGGER_FILTER_JSON_MAX];
|
||||
trigger_action_type_t action_type;
|
||||
int enabled;
|
||||
time_t last_fired;
|
||||
time_t last_seen_created_at;
|
||||
} active_trigger_t;
|
||||
|
||||
typedef struct trigger_manager {
|
||||
active_trigger_t* triggers;
|
||||
int count;
|
||||
int capacity;
|
||||
didactyl_config_t* cfg;
|
||||
pthread_mutex_t mutex;
|
||||
time_t last_poll_at;
|
||||
} trigger_manager_t;
|
||||
|
||||
int trigger_manager_init(trigger_manager_t* mgr, didactyl_config_t* cfg);
|
||||
int trigger_manager_load_from_skills(trigger_manager_t* mgr);
|
||||
int trigger_manager_add(trigger_manager_t* mgr,
|
||||
const char* skill_slug,
|
||||
const char* content,
|
||||
const char* filter_json,
|
||||
trigger_action_type_t action_type,
|
||||
int enabled);
|
||||
int trigger_manager_remove(trigger_manager_t* mgr, const char* skill_slug);
|
||||
int trigger_manager_update(trigger_manager_t* mgr,
|
||||
const char* skill_slug,
|
||||
const char* content,
|
||||
const char* filter_json,
|
||||
trigger_action_type_t action_type,
|
||||
int enabled);
|
||||
int trigger_manager_active_count(trigger_manager_t* mgr);
|
||||
int trigger_manager_poll(trigger_manager_t* mgr);
|
||||
char* trigger_manager_status_json(trigger_manager_t* mgr);
|
||||
void trigger_manager_cleanup(trigger_manager_t* mgr);
|
||||
|
||||
#endif
|
||||
1
tasks.json
Normal file
1
tasks.json
Normal file
@@ -0,0 +1 @@
|
||||
{"tasks":[{"id":5,"text":"Tweet 3: Gets Smarter Over Time - \"Didactyl starts as an AI that explores and learns. Over time, the best workflows get locked in as reliable, fast processes. It evolves from experimental to hardened—from thinking to doing. An agent that improves itself. #nostr #agents\"","status":"pending","created_at":1772625247,"updated_at":1772625247}],"next_id":6}
|
||||
28193
vendor/mongoose.c
vendored
Normal file
28193
vendor/mongoose.c
vendored
Normal file
File diff suppressed because it is too large
Load Diff
4038
vendor/mongoose.h
vendored
Normal file
4038
vendor/mongoose.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user