Compare commits

...
54 Commits
Author SHA1 Message Date
pancake 8886365925 Release 1.2.2 2025-10-25 11:03:12 +02:00
pancake 01d8e29420 Remove the r2-5.x compat macros 2025-10-25 11:02:39 +02:00
pancake e8b556ee5d Use r_sys_sleep and simplify the overengineered retry_sleep logic 2025-10-25 10:50:05 +02:00
pancake 6c336f48f7 Fix the meson build 2025-10-24 02:35:34 +02:00
pancake 53d61831b2 Initial support for vscode stype prompt files 2025-10-24 02:31:03 +02:00
pancake cbcff6b9dd Add r2ai -E in the cli 2025-10-24 02:29:34 +02:00
pancake f04bf8162e Add the manpage for r2ai.1 2025-10-24 02:04:36 +02:00
pancake 0206ceb390 Add support for r2ai -E and ~/.config/r2ai/rc 2025-10-24 02:00:15 +02:00
pancake ff1f788c48 Minor fix 2025-10-24 01:36:58 +02:00
pancake df135b2c70 Use jan-nano by default and fix the undefined in decai 2025-10-24 01:27:24 +02:00
pancake c152777bbe Dont do rawtools by default yet 2025-10-23 19:06:09 +02:00
pancake 3e084ce797 Implement r2ai.auto.raw to use tool calling with non-tools models 2025-10-23 02:12:36 +02:00
pancake 7fce8788b5 Release 1.2.0 2025-10-21 20:49:18 +02:00
pancake 585e30470e Aim to fix the execute_js tool for r2ai -a 2025-10-21 20:48:28 +02:00
pancake 5594e64ef3 r2ai-q must be public on windows 2025-10-21 20:44:07 +02:00
pancake 904a356290 Update READMEs 2025-10-21 20:40:17 +02:00
pancake d1e2bcf911 Use the powershell http fallback when needed 2025-10-21 20:38:10 +02:00
pancake 21528f7ea8 Use ollama/gpt-oss:20b as default provider/model pair 2025-10-21 20:34:58 +02:00
pancake 2883d134aa Add a WIP decompiler prompt 2025-10-21 20:18:58 +02:00
pancake ec96ba3329 Improve the behaviour of 'r2ai -m' to show, list and select models 2025-10-21 20:12:43 +02:00
pancake f92ea17b01 Move r2ai -n into r2ai -q autoname 2025-10-21 20:07:03 +02:00
pancake 2dc92031e7 Move r2ai -v into r2ai -q varnames 2025-10-21 20:03:16 +02:00
pancake 55d54b4953 Move r2ai -s into r2ai -q signature 2025-10-21 19:58:16 +02:00
pancake 35460e639c Move r2ai -x into r2ai -q explain 2025-10-21 19:49:47 +02:00
pancake a5382c17c4 Move r2ai -V to 'r2ai -q vulns' 2025-10-21 19:41:10 +02:00
pancake f4bdb847db Fix null deref when ^C in r2ai 2025-10-21 19:40:40 +02:00
pancake a7c3d3767c Use r2 -NNH instead of r2 -H to fix build when r2 have broken plugins 2025-10-21 19:29:09 +02:00
pancake 97c47ab28a Add -m, -p, -q to choose model, provider and query from the cli
* Fix the ollama provider
2025-10-21 19:24:11 +02:00
pancake c36bc1500c Dont pass R2AIState if we can pass the RCorePluginSession 2025-10-21 18:58:16 +02:00
pancake 179cb6ca58 Remove statistics global variable 2025-10-21 18:45:49 +02:00
pancake 402c31cc96 Handle -v and -h in the r2ai executable 2025-10-21 18:32:47 +02:00
pancake be14729424 Stop using the deprecated r_cons_singleton 2025-10-21 18:28:09 +02:00
pancake aa9b8e0fba Refactoring with the aim to reduce LOCs in r2ai_http.c 2025-10-21 18:24:09 +02:00
pancake 96225cd555 Update AGENTS.md 2025-10-21 17:45:39 +02:00
pancake 321ca1bb41 Move the LLM related functions from r2ai.c into llm.c 2025-10-21 17:29:14 +02:00
pancake d94ed5a088 Fix r2ai's main and use cps everywhere 2025-10-21 16:40:57 +02:00
pancake 3537d006de Added support to query custom user prompts in r2ai 2025-10-21 00:10:28 +02:00
pancake bdcee9e736 Remove global states, requires r2-6.0 at least 2025-10-20 21:54:48 +02:00
pancake 074b7a6c9f Add AGENTS.md for r2ai.c 2025-10-20 21:25:24 +02:00
pancake 6697df8437 Use RList instead of vector for messages. Fix message count in r2ai -L 2025-10-20 21:04:30 +02:00
pancake ff0fb4c802 Add version information in decai help message and use rcons 2025-10-20 19:49:40 +02:00
pancake ca7d02e03c Refactor the api/providers logic for easier maintainance 2025-10-20 17:35:43 +02:00
pancake bd105b2b0e Fix #176 - Do not accept invalid values in "r2ai -e api=" 2025-10-20 16:57:53 +02:00
pancake 5e18525403 Use clang-format-radare2 instead of the hacky indent.py 2025-10-20 16:48:42 +02:00
pancake c9d0756e74 Fallback to powershell when curl is not found on Windows 2025-10-20 11:05:20 +02:00
Abhi d3fc79f9d1 Fix tmp dir on android(termux) 2025-10-14 09:27:50 +02:00
pancake 8f4c972beb Remove pkg-config hard dependency for r2 >= 6.0.5 2025-10-08 13:51:34 +02:00
pancake f95cda42f7 Release 1.1.2 2025-10-06 23:25:27 +02:00
pancake b241f30432 Fix leak in openai_url 2025-10-06 23:25:01 +02:00
pancake 8eb50e466b Include the r2ai plugin in the meson ci builds 2025-10-06 23:22:01 +02:00
pancake 14b5dcbfb9 Fix make install and warnings 2025-10-06 22:59:22 +02:00
cryptax e264f8f0c8 More references In the README.md 2025-10-06 15:45:10 +02:00
pancake 46c2ce6c62 Fix r2ai install when old symlinks are around 2025-10-03 22:34:36 +02:00
pancake 693ad9270c Handle ollama errors 2025-10-03 18:21:35 +02:00
42 changed files with 2582 additions and 1506 deletions

No files matched your search

+5 -2
View File
@@ -93,8 +93,11 @@ jobs:
shell: bash
run: |
mkdir -p release
cp -rf src/r2ai.dll src/r2ai.exe src/r2ai src/r2ai.dylib src/r2ai.so release/ || true
cp -rf src/build/r2ai src/build/r2ai.dylib src/build/r2ai.so release/ || true
# Collect common binary artefact names produced by Make/Meson
# Include both "r2ai.*" and "libr2ai.*" variants for DLL/dylib/so on all platforms.
find src -type f \( -iname "r2ai.dll" -o -iname "libr2ai.dll" -o -iname "r2ai.dylib" -o -iname "libr2ai.dylib" -o -iname "r2ai.so" -o -iname "libr2ai.so" -o -iname "r2ai.exe" \) -exec cp -f {} release/ \; || true
# Also check top-level build output directories
find . -maxdepth 3 -type f \( -iname "r2ai.dll" -o -iname "libr2ai.dll" -o -iname "r2ai.dylib" -o -iname "libr2ai.dylib" -o -iname "r2ai.so" -o -iname "libr2ai.so" -o -iname "r2ai.exe" \) -exec cp -f {} release/ \; || true
cp -f decai/decai.r2.js release/ || true
if command -v zip >/dev/null 2>&1; then
zip -j "release/r2ai-${{ github.ref_name }}-${{ matrix.os }}-${{ matrix.build_system }}.zip" release/*
-1
View File
@@ -1 +0,0 @@
recursive-include r2ai/ui *.tcss
+3 -1
View File
@@ -1,4 +1,6 @@
TARGETS=all clean mrproper user-install install user-uninstall uninstall
TARGETS=all clean mrproper user-install install user-uninstall uninstall fmt indent
$(TARGETS):
$(MAKE) -C src $@
.PHONY: $(TARGETS)
+22 -10
View File
@@ -19,8 +19,8 @@
This repository contains two plugins for radare2:
* **r2ai** - native plugin for radare2
* **decai** - r2js plugin with special focus on decompilation
* **r2ai** - native [AI plugin](./src/README.md) for radare2
* **decai** - r2js plugin with special [focus on decompilation](./decai/README.md)
If you are looking to use radare2 with other agents via MCP:
@@ -37,12 +37,21 @@ If you are looking to use radare2 with other agents via MCP:
* RAG markdown, code or textfiles using its native vector database
* Embed the output of an r2 command and resolve questions on the given data
## Practical Examples
## User defined Prompts
* Enhanced decompilation with `r2ai -d`
* Autoname functions with `r2ai -n`
* Explain function with `r2ai -x`
* Find vulnerabilities with `r2ai -V`
```console
[0x00000000]> r2ai -q
explain: Explain the current function -
devices: Find and explain devices used -
libs: Group imports by Libraries -
varnames: Better variable names -
autoname: Automatically suggest a better name for this function -
vulns: Find vulnerabilities or bugs in the current function -
signature: Suggest an improved function signature -
dlopen: List libraries loaded with dlopen - Some libraries are loaded
decompile: Augmented decompilation based on LLM -
[0x00000000]>
```
## Installation
@@ -85,12 +94,15 @@ r2ai -e model=claude-3-7-sonnet-20250219
r2ai -e max_tokens=64000
```
## Documentation
## Further Reading
* There's [a chapter](https://book.rada.re/plugins/r2ai.html) in the official r2book
* Cryptax on [lmstudio+gptoss](https://cryptax.medium.com/r2ai-with-lmstudio-and-gpt-oss-08efa5ea2476) blog post
* Malware analysis [with r2ai](https://github.com/cryptax/talks/blob/master/BSidesKristiansand-2025/r2ai.pdf) by Cryptax
* Malware analysis [with r2ai](https://arxiv.org/pdf/2504.07574) by Cryptax and Daniel Nakov
* Analysis of [Linux/Trigona ransomware](https://cryptax.medium.com/linux-trigona-analysis-with-r2ai-3e2bd1815e52), [Linux/Prometei botnet](https://cryptax.medium.com/reversing-a-prometei-botnet-binary-with-r2-and-ai-part-one-3cdb3dc6ffab) and [W32/SkyAI](https://cryptax.medium.com/w32-skyai-uses-ai-so-do-i-d33f04d63534with) with r2ai
## Videos
- https://infosec.exchange/@radareorg/111946255058894583
- [Solving a crackme](https://infosec.exchange/@radareorg/111946255058894583)
- [De-obfuscation of malware Linux/Ladvix](https://asciinema.org/a/724126)
- [Analysis of the /fast option inside Linux/Trigona ransomware](https://asciinema.org/a/pBPEaJhp6cunWSKFpBUDTgPt4)
+1 -1
View File
@@ -1 +1 @@
1.1.1
1.2.2
+3 -1
View File
@@ -1,3 +1,5 @@
#!/bin/sh
vim src/r2ai.c
vim decai/decai.r2.js
vim src/r2ai.h
vim src/meson.build
vim VERSION
+18 -6
View File
@@ -1,5 +1,6 @@
(function () {
// Constants and configuration
const VERSION = "1.2.2";
const COMMAND = "decai";
const DEFAULT_PROMPT =
"Transform this pseudocode and respond ONLY with plain code (NO explanations, comments or markdown), Change 'goto' into if/else/for/while, Simplify as much as possible, use better variable names, take function arguments and strings from comments like 'string:', Reduce lines of code and fit everything in a single function, Remove all dead code";
@@ -727,8 +728,9 @@ Use radare2 to resolve user requests.
"x-api-key: " + key[0],
];
let res = {};
try {
const res = http.post(
res = http.post(
provider.defaultBaseurl + "/v1/messages",
headers,
JSON.stringify(payload),
@@ -765,13 +767,20 @@ Use radare2 to resolve user requests.
try {
const res = http.post(url, [], JSON.stringify(payload));
return utils.filterResponse(res.message.content);
} catch (e) {
if (res && res.error) {
throw new Error(res.error);
}
if (res.message && res.message.content) {
return utils.filterResponse(res.message.content);
}
throw new Error(JSON.stringify(res));
if (res.error && res.error.indexOf("try pulling")) {
const modelName = res.error.split(/"/g)[1];
res.error += "\n!ollama run " + modelName;
}
return "ERROR: " + (res.error || e.message);
} catch (e) {
console.error(e.stack);
return "ERROR: " + e.message;
}
},
@@ -866,8 +875,10 @@ Use radare2 to resolve user requests.
// Command handlers
const commands = {
help: () => {
const msg = (m) => console.error(" " + COMMAND + " " + m);
console.error("Usage: " + COMMAND + " (-h) ...");
let helpmsg = "";
const msg = (m) => helpmsg += " " + COMMAND + " " + m + "\n";
helpmsg += "Usage: " + COMMAND + " (-h) ...\n";
helpmsg += "Version: " + VERSION + "\n";
msg("-a [query] - solve query with auto mode");
msg("-d [f1 ..] - decompile given functions");
msg("-dd [..] - same as above, but ignoring cache");
@@ -888,6 +899,7 @@ Use radare2 to resolve user requests.
msg("-v - show local variables");
msg("-V - find vulnerabilities");
msg("-x[*] - eXplain current function (-x* for r2 script)");
r2.log(helpmsg.trim());
},
decompile: (args, extraQuery, useCache, recursiveCalls) => {
+43
View File
@@ -0,0 +1,43 @@
# R2AI Prompts
This directory contains predefined prompt files for the r2ai plugin.
## Format
Each prompt is stored in a `.r2ai` file using a simple Key: Value format.
Lines starting with `#` are comments and ignored.
Supported keys:
- `Title`: The title of the prompt (required)
- `Author`: The author of the prompt
- `Description`: A brief description
- `Command` or `Commands`: Commands to execute before sending the prompt, separated by `;` (required)
- `Prompt` or `Query`: The main prompt text sent to the LLM
- `Requires`: Requirements for the prompt (e.g., "analysis" to ensure binary analysis)
- `If-Empty`: Alternative prompt or message if the command output is empty
- `If-Command`: Additional commands to run if the main command produces output
## Variable Substitution
In the `Prompt` field, you can use:
- `${VAR}`: Replaced with the value of the environment variable `VAR`
- `$(cmd)`: Replaced with the output of the r2 command `cmd`
## Usage
- `r2ai -q`: List all available prompts
- `r2ai -q name`: Run the prompt named `name`
- `r2ai -q name extra text`: Run the prompt with additional text appended
## Conditionals
- `If-Empty`: If the command output is empty, use this as the prompt instead
- `If-Command`: If the command produces output, run these additional commands and append their output
## Future Features
- Support for r2js scripts
- More advanced conditionals
+4
View File
@@ -0,0 +1,4 @@
Title: Automatically suggest a better name for this function
Author: pancake
Command: afv;r2ai -d
Prompt: Suggest a better name for this function. Output only the suggested function name (must be one alphanumeric word) prefixed with `'afn '. Check the code below:
+4
View File
@@ -0,0 +1,4 @@
Title: Augmented decompilation based on LLM
Author: pancake
Command: pdc
Prompt: $(-e r2ai.prompt). Translate the code into $(-e r2ai.lang) programming language.
+4
View File
@@ -0,0 +1,4 @@
Title: Find and explain devices used
Author: pancake
Command: izqq~^/dev
Prompt: explain in one line the purpose of each of these devices
+6
View File
@@ -0,0 +1,6 @@
Title: List libraries loaded with dlopen
Description: Some libraries are loaded
Requires: analysis
Command: iiq~dlopen
If-Empty: This binary is not using dlopen
If-Command: e emu.str=true;pd--5@@c:axtq sym.imp.dlopen
+4
View File
@@ -0,0 +1,4 @@
Title: Explain the current function
Author: pancake
Command: r2ai -d
Prompt: Analyze function calls, comments and strings, ignore registers and memory accesses. Considering the references and involved loops; explain the purpose of this function in one or two short sentences. Output must be only the translation of the explanation in $(-e r2ai.hlang)
+4
View File
@@ -0,0 +1,4 @@
Title: Group imports by Libraries
Author: pancake
Commands: iiq;il
Query: Group the imports that belong to each library and describe its purpose and usage scope
+4
View File
@@ -0,0 +1,4 @@
Title: Suggest an improved function signature
Author: pancake
Command: ?e variables;afv;?e function name:;afd;?e current signature:?e afs
Prompt: Analyze the uses of the arguments and return value to infer the signature, identify which is the correct type for the resturn. Do NOT print the function body. Output must be *ONLY* the function signature prefixed with the `'afs ` text
+10
View File
@@ -0,0 +1,10 @@
---
description: 'Decompile this code into PHP'
version: 0.1
author: pancake
command: pdg
if-empty: exit
---
Your goal is to $(-e r2ai.prompt).
Translate the code into $(-e r2ai.lang) programming language.
+4
View File
@@ -0,0 +1,4 @@
Title: Better variable names
Author: pancake
Command: afv
Prompt: Output only the radare2 command without markdown, guess a better name and type for each local variable and function argument taking using. output an r2 script using afvn and afvt commands
+4
View File
@@ -0,0 +1,4 @@
Title: Find vulnerabilities or bugs in the current function
Author: pancake
Command: pdc
Prompt: Find vulnerabilities, do not show the code, only a short summary of the resulting analysis report including suggested good practices to address the problem and a practical exploit example.
-40
View File
@@ -1,40 +0,0 @@
BasedOnStyle: LLVM
Language: Cpp
PointerAlignment: Right
AlwaysBreakAfterDefinitionReturnType: None
BinPackParameters: true
MaxEmptyLinesToKeep: 1
SpaceInEmptyParentheses: false
SpacesInContainerLiterals: true
SpaceBeforeParens: Custom
SpaceBeforeParensOptions:
AfterIfMacros: true
AfterFunctionDefinitionName: false
AfterFunctionDeclarationName: false
AfterForeachMacros: true
AfterControlStatements: true
BeforeNonEmptyParentheses: false
SpacesInParentheses: false
InsertBraces: true
ContinuationIndentWidth: 8
IndentCaseLabels: false
IndentFunctionDeclarationAfterType: false
IndentWidth: 8
UseTab: ForContinuationAndIndentation
ColumnLimit: 0
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
AllowShortIfStatementsOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: Inline
AllowShortLoopsOnASingleLine: false
AlignAfterOpenBracket: DontAlign
AlignEscapedNewlines: DontAlign
AlignConsecutiveMacros: true
AlignTrailingComments: false
AlignOperands: false
Cpp11BracedListStyle: false
ForEachMacros: ['r_list_foreach', 'ls_foreach', 'fcn_tree_foreach_intersect', 'r_skiplist_foreach', 'graph_foreach_anode', 'r_list_foreach_safe', 'r_pvector_foreach', 'r_rbtree_foreach', 'r_interval_ tree_foreach']
SortIncludes: false
+32
View File
@@ -0,0 +1,32 @@
# R2AI Plugin for radare2
## Locations
- All the source code of the plugin is in the current directory
- Radare2 headers use to be in /usr/local/include/libr
## Formatting Style
- Use the radare2 coding style rules
- Always add a whitespace before `(` in function calls
- Indent the code with tabs
- Indent comments with spaces
- Do not define variables inside the `for` parenthesis
- Do not check for null before calling free
## Test
Run `r2ai` cli tool or radare2 oneliners running the `r2ai` command.
* `r2pm -r r2ai -h` -> help message for the r2ai tool
* `r2 -qc 'r2ai -h' /bin/ls -> help message of the r2ai plugin
* `r2 -qc 'r2ai -a what arch is used here' -> testing auto mode
* `r2 -e r2ai.model=gemma3:12b -e r2ai.auto.raw=true -qc 'r2ai -a what arch is used here' -> testing auto mode
## Actions
Run the following commands to perform the action described
- FORMAT: `make fmt`
- COMPILE: `make` (with no extra arguments)
- INSTALL: `make user-install`
+35 -11
View File
@@ -1,13 +1,21 @@
DOTEXE=
# DOTEXE=.exe
DOTLIB=.$(shell r2 -H R2_LIBEXT)
DOTLIB=.$(shell r2 -NNH R2_LIBEXT)
CFLAGS+=$(shell pkg-config --cflags r_core)
LDFLAGS+=$(shell pkg-config --libs r_core)
R2SYSPLUGDIR=$(shell r2 -H R2_LIBR_PLUGINS)
R2USRPLUGDIR=$(shell r2 -H R2_USER_PLUGINS)
R2SYSPLUGDIR=$(shell r2 -NNH R2_LIBR_PLUGINS)
R2USRPLUGDIR=$(shell r2 -NNH R2_USER_PLUGINS)
R2BINDIR=$(shell r2pm -H R2PM_BINDIR)
R2MANDIR=$(shell r2pm -H R2PM_MANDIR)
R2_CFLAGS=$(shell pkg-config --cflags r_core)
R2_LDFLAGS=$(shell pkg-config --libs r_core)
ifeq ($(R2_CFLAGS),)
R2_CFLAGS=$(shell r2 -NNH R2_CFLAGS)
R2_LDFLAGS=$(shell r2 -NNH R2_LDFLAGS)
endif
CFLAGS+=$(R2_CFLAGS)
LDFLAGS+=$(R2_LDFLAGS)
CFLAGS+=-I. -fPIC
CFLAGS+=-Wall
CFLAGS+=-g
@@ -31,6 +39,7 @@ ifeq ($(USE_LIBCURL),1)
endif
OBJS=r2ai.o
OBJS+=llm.o
OBJS+=auto.o
OBJS+=vdb.o
OBJS+=tools.o
@@ -39,6 +48,8 @@ OBJS+=anthropic.o
OBJS+=r2ai_http.o
OBJS+=openai.o
OBJS+=markdown.o
OBJS+=query.o
OBJS+=rawtools.o
all: $(OBJS) r2check
$(CC) -fPIC -shared -o r2ai$(DOTLIB) $(OBJS) $(CFLAGS) $(LDFLAGS)
@@ -54,24 +65,37 @@ doc-install:
vdb.o: vdb_embed.inc.c
indent:
for a in *.c ; do python indent.py $$a ; done
fmt format indent:
clang-format-radare2 *.c
user-install: user-uninstall
mkdir -p $(R2USRPLUGDIR)
cp -f r2ai$(DOTLIB) $(R2USRPLUGDIR)
cp -f r2ai$(DOTEXE) $(R2BINDIR)
mkdir -p $(R2BINDIR)
mkdir -p $(R2MANDIR)/man1
mkdir -p $(HOME)/.config/r2ai/prompts
$(MAKE) user-uninstall
cp -f r2ai$(DOTLIB) $(R2USRPLUGDIR)/r2ai$(DOTLIB)
cp -f r2ai$(DOTEXE) $(R2BINDIR)/r2ai$(DOTEXE)
cp -f r2ai.1 $(R2MANDIR)/man1/r2ai.1
cp -r ../prompts/* $(HOME)/.config/r2ai/prompts/
user-uninstall:
rm -f $(R2USRPLUGDIR)/r2ai$(DOTLIB)
rm -f $(R2BINDIR)/r2ai$(DOTEXE)
rm -f $(R2MANDIR)/man1/r2ai.1
install:
cp -f r2ai$(DOTLIB) $(R2SYSPLUGDIR)
$(MAKE) uninstall
mkdir -p $(R2USRPLUGDIR)
mkdir -p $(R2BINDIR)
cp -f r2ai$(DOTLIB) $(R2SYSPLUGDIR)/r2ai$(DOTLIB)
cp -f r2ai$(DOTEXE) $(R2BINDIR)/r2ai$(DOTEXE)
uninstall:
rm -f $(R2SYSPLUGDIR)/r2ai$(DOTLIB)
rm -f $(R2BINDIR)/r2ai$(DOTEXE)
r2check:
@r2 -qcq --
@r2 -NN -qcq --
clean:
rm -f *.o *.d
+40 -17
View File
@@ -1,12 +1,10 @@
# C-R2AI
# R2AI
This directory contains the C rewrite of the r2ai plugin for radare2.
The r2ai plugin for radare2.
## Installation
Requires radare2 from git if you want to use the stream HTTP APIs.
Otherwise the only requirement is a C compiler.
Requires a C compiler and modern version of radare2 if you want to use the stream HTTP APIs. It will fallback to curl or powershell if not available and `R2_CURL` environment is set to `1`.
```console
make
@@ -37,24 +35,53 @@ Usage: r2ai [-args] [...]
| r2ai -d [query] Ask a question on the current function
| r2ai -dr Decompile current function (+ 1 level of recursivity)
| r2ai -a [query] Resolve question using auto mode
| r2ai -e Same as '-e r2ai.'
| r2ai -e (k(=v)) Same as '-e r2ai.'
| r2ai -h Show this help message
| r2ai -i [file] [query] read file and ask the llm with the given query
| r2ai -m show selected model, list suggested ones, choose one
| r2ai -n suggest a better name for the current function
| r2ai -p [provider] set LLM provider (openai, anthropic, gemini, etc.)
| r2ai -q list available query prompts
| r2ai -q [name] (inst) run predefined prompt with optional instructions
| r2ai -r enter the chat repl
| r2ai -L show chat logs (See -Lj for json). Only for auto mode.
| r2ai -L-[N] delete the last (or N last messages from the chat history)
| r2ai -R reset the chat conversation context
| r2ai -Rq ([text]) refresh and query embeddings (see r2ai.data)
| r2ai -s function signature
| r2ai -x explain current function
| r2ai -v suggest better variables names and types
| r2ai -V[r] find vulnerabilities in the decompiled code (-Vr uses -dr)
| r2ai [arg] send a post request to talk to r2ai and print the output
| r2ai [query] query the selected model+provider with the given query
[0x100003f58]>
```
## Query Prompts
Query prompts are predefined templates that combine radare2 commands with structured prompts to the LLM for common reverse engineering tasks. They are stored in the `prompts/` directory as `.r2ai.txt` files.
### Available Prompts
- `autoname`: Automatically rename functions and variables
- `decompile`: Decompile the current function
- `devices`: Analyze device-related code
- `dlopen`: Analyze dynamic library loading
- `explain`: Explain the purpose of the current function
- `libs`: Analyze library usage
- `signature`: Improve function signatures
- `varnames`: Rename variables based on context
- `vulns`: Find vulnerabilities in the current function
### Usage
- `r2ai -q`: List all available query prompts
- `r2ai -q [name]`: Run the specified prompt on the current function
- `r2ai -q [name] [instructions]`: Run the prompt with additional custom instructions
### Example
```console
[0x00001303]> r2ai -q explain
The main function implements a simple substitution cipher program that reads user input, encrypts it using a hardcoded alphabet, and then decrypts it back to demonstrate the algorithm.
```
Query prompts automate repetitive analysis tasks by executing predefined radare2 commands and sending context-aware prompts to the LLM.
## Configuration
These can be set with `r2ai -e <keyname>=<value>`
@@ -76,7 +103,6 @@ These can be set with `r2ai -e <keyname>=<value>`
| r2ai.http.use_files | If you get CURL argument too long error, set this to true and arguments will be stored in a file.|
## Example
- Get the [substitute example binary](https://github.com/radareorg/r2ai/tree/master/examples)
@@ -215,8 +241,6 @@ Note: System prompt is applied automatically but not stored in history
{"command":"pdf @ main"}
```
## TODO
* add "undo" command to drop the last message
@@ -225,7 +249,6 @@ Note: System prompt is applied automatically but not stored in history
* keep context between direct commands unless explicitly reset
* modify context if tool command is user edited
## Messages API
The r2ai project includes a new API for managing messages and tool calls in conversations with AI models. This API is implemented in `messages.c` and `messages.h`.
@@ -268,4 +291,4 @@ In r2ai, as lm-studio uses an OpenAI-like API, configure:
- `r2ai -e api=openai`
- `r2ai -e baseurl=http://LM-STUDIO-IP:PORT`
- `r2ai -e model=?` to list available models
- `r2ai -e model=?` to list available models
+18 -7
View File
@@ -1,10 +1,11 @@
#include "r2ai.h"
static const char *modelname(const char *model_name) {
return model_name ? model_name : "claude-3-7-sonnet-20250219";
return model_name? model_name: "claude-3-7-sonnet-20250219";
}
R_IPI R2AI_ChatResponse *r2ai_anthropic(RCore *core, R2AIArgs args) {
R_IPI R2AI_ChatResponse *r2ai_anthropic(RCorePluginSession *cps, R2AIArgs args) {
RCore *core = cps->core;
const char *model = args.model;
char **error = args.error;
const R2AI_Tools *tools = args.tools;
@@ -40,7 +41,7 @@ R_IPI R2AI_ChatResponse *r2ai_anthropic(RCore *core, R2AIArgs args) {
// Create messages JSON
char *messages_json = NULL;
if (messages_input && messages_input->n_messages > 0) {
if (messages_input && r_list_length (messages_input->messages) > 0) {
// Convert directly to JSON without filtering
messages_json = r2ai_msgs_to_anthropic_json (messages_input);
if (!messages_json) {
@@ -98,8 +99,13 @@ R_IPI R2AI_ChatResponse *r2ai_anthropic(RCore *core, R2AIArgs args) {
free (messages_json);
// Save the full JSON for debugging
r_file_dump ("/tmp/r2ai_anthropic_request.json", (const ut8 *)data, strlen (data), 0);
R_LOG_DEBUG ("Full request saved to /tmp/r2ai_anthropic_request.json");
char *tmpdir = r_file_tmpdir ();
char *req_path = r_str_newf ("%s" R_SYS_DIR "r2ai_anthropic_request.json", tmpdir);
r_file_dump (req_path, (const ut8 *)data, strlen (data), 0);
R_LOG_DEBUG ("Full request saved to %s", req_path);
free (req_path);
free (tmpdir);
R_LOG_DEBUG ("Anthropic API request data: %s", data);
// Make the API call
@@ -120,8 +126,13 @@ R_IPI R2AI_ChatResponse *r2ai_anthropic(RCore *core, R2AIArgs args) {
}
// Save the response for inspection
r_file_dump ("/tmp/r2ai_anthropic_response.json", (const ut8 *)res, strlen (res), 0);
R_LOG_DEBUG ("Anthropic API response saved to /tmp/r2ai_anthropic_response.json");
tmpdir = r_file_tmpdir ();
char *res_path = r_str_newf ("%s" R_SYS_DIR "r2ai_anthropic_response.json", tmpdir);
r_file_dump (res_path, (const ut8 *)res, strlen (res), 0);
R_LOG_DEBUG ("Anthropic API response saved to %s", res_path);
free (res_path);
free (tmpdir);
R_LOG_DEBUG ("Anthropic API response: %s", res);
// Parse the response
+183 -142
View File
@@ -1,76 +1,60 @@
#include "r2ai.h"
#include <time.h>
// Forward declaration of the r2ai_llmcall function
extern R2AI_ChatResponse *r2ai_llmcall(RCore *core, R2AIArgs args);
// Add a global structure to track timing and costs
typedef struct {
double total_cost;
double run_cost;
int total_tokens;
int run_tokens;
int total_prompt_tokens;
int run_prompt_tokens;
int total_completion_tokens;
int run_completion_tokens;
time_t start_time;
time_t total_start_time;
} R2AIStats;
static R2AIStats stats = { 0 };
// Stats are now stored in R2AI_State->stats, no longer need global variable
// Helper function to format time duration
static char *format_time_duration(time_t seconds) {
if (seconds < 60) {
return r_str_newf ("%llds", (long long)seconds);
return r_str_newf ("%" PFMT64d "s", (long long)seconds);
}
if (seconds < 3600) {
return r_str_newf ("%lldm%llds", (long long)(seconds / 60), (long long)(seconds % 60));
return r_str_newf ("%" PFMT64d "m%" PFMT64d "s", (long long) (seconds / 60), (long long) (seconds % 60));
}
return r_str_newf ("%lldh%lldm%llds",
(long long)(seconds / 3600),
(long long)((seconds % 3600) / 60),
(long long)(seconds % 60));
return r_str_newf ("%" PFMT64d "h%" PFMT64d "m%" PFMT64d "s",
(long long) (seconds / 3600),
(long long) ((seconds % 3600) / 60),
(long long) (seconds % 60));
}
// Initialize timing and cost tracking for a run
static void r2ai_stats_init_run(int n_run) {
static void r2ai_stats_init_run(R2AI_State *state, int n_run) {
time_t run_start = time (NULL);
if (n_run == 1) {
// First run, initialize total timing
stats.total_cost = 0.0;
stats.run_cost = 0.0;
stats.total_start_time = run_start;
stats.total_tokens = 0;
stats.run_tokens = 0;
stats.total_prompt_tokens = 0;
stats.run_prompt_tokens = 0;
stats.total_completion_tokens = 0;
stats.run_completion_tokens = 0;
state->stats.total_cost = 0.0;
state->stats.run_cost = 0.0;
state->stats.total_start_time = run_start;
state->stats.total_tokens = 0;
state->stats.run_tokens = 0;
state->stats.total_prompt_tokens = 0;
state->stats.run_prompt_tokens = 0;
state->stats.total_completion_tokens = 0;
state->stats.run_completion_tokens = 0;
}
stats.start_time = run_start;
state->stats.start_time = run_start;
}
// Print a simple run indicator at the start
static void r2ai_print_run_end(RCore *core, const R2AI_Usage *usage, int n_run, int max_runs) {
static void r2ai_print_run_end(RCorePluginSession *cps, const R2AI_Usage *usage, int n_run, int max_runs) {
RCore *core = cps->core;
R2AI_State *state = cps->data;
(void)n_run;
(void)max_runs;
time_t run_time = time (NULL) - stats.start_time;
time_t total_time = time (NULL) - stats.total_start_time;
time_t run_time = time (NULL) - state->stats.start_time;
time_t total_time = time (NULL) - state->stats.total_start_time;
if (usage) {
stats.run_tokens = usage->total_tokens;
stats.run_prompt_tokens = usage->prompt_tokens;
stats.run_completion_tokens = usage->completion_tokens;
stats.total_tokens += usage->total_tokens;
stats.total_prompt_tokens += usage->prompt_tokens;
stats.total_completion_tokens += usage->completion_tokens;
state->stats.run_tokens = usage->total_tokens;
state->stats.run_prompt_tokens = usage->prompt_tokens;
state->stats.run_completion_tokens = usage->completion_tokens;
state->stats.total_tokens += usage->total_tokens;
state->stats.total_prompt_tokens += usage->prompt_tokens;
state->stats.total_completion_tokens += usage->completion_tokens;
}
if (r_config_get_b (core->config, "r2ai.chat.show_cost") == true) {
// TODO: calculate cost
stats.run_cost = 0.0 * run_time;
stats.total_cost += stats.run_cost;
state->stats.run_cost = 0.0 * run_time;
state->stats.total_cost += state->stats.run_cost;
}
// Format times for display
@@ -78,83 +62,90 @@ static void r2ai_print_run_end(RCore *core, const R2AI_Usage *usage, int n_run,
char *total_time_str = format_time_duration (total_time);
// Print detailed stats
R2_PRINTF ("\x1b[1;34m%s | total: %d in: %d out: %d | run: %d in: %d out: %d | %s / %s\x1b[0m\n",
r_cons_printf (core->cons, "\x1b[1;34m%s | total: %d in: %d out: %d | run: %d in: %d out: %d | %s / %s\x1b[0m\n",
r_config_get (core->config, "r2ai.model"),
stats.total_tokens,
stats.total_prompt_tokens,
stats.total_completion_tokens,
stats.run_tokens,
stats.run_prompt_tokens,
stats.run_completion_tokens,
state->stats.total_tokens,
state->stats.total_prompt_tokens,
state->stats.total_completion_tokens,
state->stats.run_tokens,
state->stats.run_prompt_tokens,
state->stats.run_completion_tokens,
run_time_str,
total_time_str);
R2_NEWLINE ();
R2_FLUSH ();
r_cons_newline (core->cons);
r_cons_flush (core->cons);
free (run_time_str);
free (total_time_str);
}
const char *Gprompt_auto = "You are a reverse engineer and you are using radare2 to analyze a binary.\n"
"The user will ask questions about the binary and you will respond with the answer to the best of your ability.\n"
"\n"
"# Guidelines\n"
"- Understand the Task: Grasp the main objective, goals, requirements, constraints, and expected output.\n"
"- Reasoning Before Conclusions**: Encourage reasoning steps before any conclusions are reached.\n"
"- Assume the user is always asking you about the binary, unless they're specifically asking you for radare2 help.\n"
"- The binary has already been loaded. You can interact with the binary using the r2cmd tool.\n"
"- `this` or `here` might refer to the current address in the binary or the binary itself.\n"
"- If you need more information, try to use the r2cmd tool to run commands before answering.\n"
"- You can use the r2cmd tool multiple times if you need or you can pass a command with pipes if you need to chain commands.\n"
"- If you're asked to decompile a function, make sure to return the code in the language you think it was originally written and rewrite it to be as easy as possible to be understood. Make sure you use descriptive variable and function names and add comments.\n"
"- Don't just regurgitate the same code, figure out what it's doing and rewrite it to be more understandable.\n"
"- If you need to run a command in r2 before answering, you can use the r2cmd tool\n"
"- Do not repeat commands if you already know the answer.\n"
"- Formulate a plan. Think step by step. Analyze the binary as much as possible before answering.\n"
"- You must keep going until you have a final answer.\n"
"- Double check that final answer. Make sure you didn't miss anything.\n"
"- Make sure you call tools and functions correctly.\n";
"The user will ask questions about the binary and you will respond with the answer to the best of your ability.\n"
"\n"
"# Guidelines\n"
"- Understand the Task: Grasp the main objective, goals, requirements, constraints, and expected output.\n"
"- Reasoning Before Conclusions**: Encourage reasoning steps before any conclusions are reached.\n"
"- Assume the user is always asking you about the binary, unless they're specifically asking you for radare2 help.\n"
"- The binary has already been loaded. You can interact with the binary using the r2cmd tool.\n"
"- `this` or `here` might refer to the current address in the binary or the binary itself.\n"
"- If you need more information, try to use the r2cmd tool to run commands before answering.\n"
"- You can use the r2cmd tool multiple times if you need or you can pass a command with pipes if you need to chain commands.\n"
"- If you're asked to decompile a function, make sure to return the code in the language you think it was originally written and rewrite it to be as easy as possible to be understood. Make sure you use descriptive variable and function names and add comments.\n"
"- Don't just regurgitate the same code, figure out what it's doing and rewrite it to be more understandable.\n"
"- If you need to run a command in r2 before answering, you can use the r2cmd tool\n"
"- Do not repeat commands if you already know the answer.\n"
"- Formulate a plan. Think step by step. Analyze the binary as much as possible before answering.\n"
"- You must keep going until you have a final answer.\n"
"- Double check that final answer. Make sure you didn't miss anything.\n"
"- Make sure you call tools and functions correctly.\n";
// Helper function to process messages and handle tool calls recursively
R_API void process_messages(RCore *core, R2AI_Messages *messages, const char *system_prompt, int n_run) {
R_API void process_messages(RCorePluginSession *cps, R2AI_Messages *messages, const char *system_prompt, int n_run) {
RCore *core = cps->core;
R2AI_State *state = cps->data;
char *error = NULL;
bool interrupted = false;
const int max_runs = r_config_get_i (core->config, "r2ai.auto.max_runs");
if (n_run > max_runs) {
R2_PRINTF ("\x1b[1;31m[r2ai] Max runs reached\x1b[0m\n");
R2_FLUSH ();
r_cons_printf (core->cons, "\x1b[1;31m[r2ai] Max runs reached\x1b[0m\n");
r_cons_flush (core->cons);
return;
}
if (!system_prompt) {
system_prompt = Gprompt_auto;
const char *init_commands = r_config_get (core->config, "r2ai.auto.init_commands");
if (R_STR_ISNOTEMPTY (init_commands)) {
char *edited_command = NULL;
char *cmd_output = execute_tool (core, "r2cmd", r_str_newf ("{\"command\":\"%s\"}", init_commands), &edited_command);
if (cmd_output) {
system_prompt = r_str_newf ("%s\n\nHere is some information about the binary to get you started:\n>%s\n%s", Gprompt_auto, edited_command, cmd_output);
if (R_STR_ISNOTEMPTY (cmd_output)) {
R2AI_Message init_msg = {
.role = "system",
.content = r_str_newf ("Here is some information about the binary to get you started:\n>%s\n%s", edited_command, cmd_output)
};
r2ai_msgs_add (messages, &init_msg);
free (cmd_output);
}
free (edited_command);
} else {
system_prompt = Gprompt_auto;
}
}
r2ai_stats_init_run (n_run);
r2ai_stats_init_run (state, n_run);
r_cons_printf (core->cons, "\x1b[35m[DEBUG] About to call r2ai_llmcall with n_run=%d\x1b[0m\n", n_run);
r_cons_flush (core->cons);
// Set up args for r2ai_llmcall call with tools directly
R2AIArgs args = {
.messages = messages,
.error = &error,
.dorag = true,
.tools = r2ai_get_tools (),
.tools = r2ai_get_tools (), // Always send tools in auto mode
.system_prompt = system_prompt
};
// Call r2ai_llmcall to get a response
R2AI_ChatResponse *response = r2ai_llmcall (core, args);
R2AI_ChatResponse *response = r2ai_llmcall (cps, args);
if (!response) {
return;
@@ -169,18 +160,34 @@ R_API void process_messages(RCore *core, R2AI_Messages *messages, const char *sy
return;
}
// Debug logging for rawtools
R_LOG_DEBUG ("[DEBUG] Model response - Role: %s", message->role? message->role: "null");
if (message->content) {
R_LOG_DEBUG ("[DEBUG] Content: %s", message->content);
}
if (message->reasoning_content) {
R_LOG_DEBUG ("[DEBUG] Reasoning: %s", message->reasoning_content);
}
if (message->tool_calls && message->n_tool_calls > 0) {
R_LOG_DEBUG ("[DEBUG] Tool calls: %d", message->n_tool_calls);
for (int i = 0; i < message->n_tool_calls; i++) {
const R2AI_ToolCall *tc = &message->tool_calls[i];
R_LOG_DEBUG ("[DEBUG] Tool %d: %s - %s", i, tc->name? tc->name: "null", tc->arguments? tc->arguments: "null");
}
}
// Process the response - we need to add it to our messages array
if (message->content || message->reasoning_content) {
R2_PRINTF ("\x1b[31m[Assistant]\x1b[0m\n\n");
r_cons_printf (core->cons, "\x1b[31m[Assistant]\x1b[0m\n\n");
if (message->reasoning_content) {
R2_PRINTF ("\x1b[90m<thinking>\n%s\n</thinking>\x1b[0m\n", message->reasoning_content);
R2_NEWLINE ();
R2_FLUSH ();
r_cons_printf (core->cons, "\x1b[90m<thinking>\n%s\n</thinking>\x1b[0m\n", message->reasoning_content);
r_cons_newline (core->cons);
r_cons_flush (core->cons);
}
if (message->content) {
R2_PRINTF ("%s", message->content);
R2_NEWLINE ();
R2_FLUSH ();
r_cons_printf (core->cons, "%s", message->content);
r_cons_newline (core->cons);
r_cons_flush (core->cons);
}
}
@@ -190,16 +197,17 @@ R_API void process_messages(RCore *core, R2AI_Messages *messages, const char *sy
// Check for tool calls and process them
if (message->tool_calls && message->n_tool_calls > 0) {
R_LOG_DEBUG ("Found %d tool call (s)", message->n_tool_calls);
R_LOG_DEBUG ("Found %d tool call(s)", message->n_tool_calls);
// Process each tool call
for (int i = 0; i < message->n_tool_calls; i++) {
const R2AI_ToolCall *tool_call = &message->tool_calls[i];
if (!tool_call->name || !tool_call->arguments || !tool_call->id) {
R_LOG_DEBUG ("Skipping invalid tool call %d", i);
continue;
}
R_LOG_DEBUG ("Tool call: %s", tool_call->name);
R_LOG_DEBUG ("Tool call %d: %s with args: %s", i, tool_call->name, tool_call->arguments);
// Don't log the full arguments which might get truncated
char *tool_name = strdup (tool_call->name);
char *tool_args = strdup (tool_call->arguments);
@@ -216,15 +224,46 @@ R_API void process_messages(RCore *core, R2AI_Messages *messages, const char *sy
} else {
char *edited_command = NULL;
cmd_output = execute_tool (core, tool_name, tool_args, &edited_command);
// TODO: need to edit the R2AI_Messages* and modify the command of the last tool_use
if (edited_command) {
// Update the last message's tool call arguments with the edited command
R2AI_Message *last_msg = r_list_get_n (messages->messages, r_list_length (messages->messages) - 1);
if (last_msg && last_msg->tool_calls && last_msg->n_tool_calls > 0) {
for (int j = 0; j < last_msg->n_tool_calls; j++) {
R2AI_ToolCall *tc = (R2AI_ToolCall *)&last_msg->tool_calls[j];
if (tc->id && strcmp (tc->id, tool_call->id) == 0) {
// For r2cmd, update the command in arguments
if (strcmp (tool_name, "r2cmd") == 0) {
char *args_dup = strdup (tc->arguments);
RJson *args_json = r_json_parse (args_dup);
if (args_json) {
RJson *cmd_json = (RJson *)r_json_get (args_json, "command");
if (cmd_json && cmd_json->str_value) {
// Update the command field
free ((char *)cmd_json->str_value);
cmd_json->str_value = strdup (edited_command);
// Serialize back to JSON
char *new_args = r_json_to_string (args_json);
if (new_args) {
free ((void *)tc->arguments);
tc->arguments = new_args;
}
}
r_json_free (args_json);
}
}
break;
}
}
}
}
free (edited_command);
}
free (tool_name);
free (tool_args);
if (strcmp (cmd_output, "R2AI_SIGINT") == 0) {
R2_PRINTF ("\n\n\x1b[1;31m[r2ai] Processing interrupted after tool execution\x1b[0m\n\n");
R2_FLUSH ();
r_cons_printf (core->cons, "\n\n\x1b[1;31m[r2ai] Processing interrupted after tool execution\x1b[0m\n\n");
r_cons_flush (core->cons);
free (cmd_output);
cmd_output = strdup ("<user interrupted>");
interrupted = true;
@@ -233,32 +272,41 @@ R_API void process_messages(RCore *core, R2AI_Messages *messages, const char *sy
// Create a tool call response message
R2AI_Message tool_response = {
.role = "tool",
.tool_call_id = tool_call->id,
.tool_call_id = strdup (tool_call->id),
.content = cmd_output
};
// Add the tool response to our messages array
r2ai_msgs_add (messages, &tool_response);
R_LOG_DEBUG ("[DEBUG] Added tool response to messages: %s", cmd_output? cmd_output: "null");
r_cons_printf (core->cons, "\x1b[32m[DEBUG] Tool result: %s\x1b[0m\n", cmd_output? cmd_output: "no output");
free (cmd_output);
}
r2ai_print_run_end (core, usage, n_run, max_runs);
r2ai_print_run_end (cps, usage, n_run, max_runs);
// Check if we should continue with recursion
if (!interrupted && message->tool_calls && message->n_tool_calls > 0) {
process_messages (core, messages, system_prompt, n_run + 1);
R_LOG_DEBUG ("[DEBUG] Recursing to process_messages with n_run=%d", n_run + 1);
process_messages (cps, messages, system_prompt, n_run + 1);
} else {
R_LOG_DEBUG ("[DEBUG] Auto mode loop ending - no more tool calls or interrupted");
}
} else {
r2ai_print_run_end (core, usage, n_run, max_runs);
r2ai_print_run_end (cps, usage, n_run, max_runs);
}
// Free the response struct itself since r2ai_message_free doesn't do it anymore
free (response);
}
R_IPI void cmd_r2ai_a(RCore *core, const char *user_query) {
R_IPI void cmd_r2ai_a(RCorePluginSession *cps, const char *user_query) {
RCore *core = cps->core;
R2AI_State *state = cps->data;
r_cons_printf (core->cons, "\x1b[35m[DEBUG] cmd_r2ai_a called with query: %s\x1b[0m\n", user_query);
r_cons_flush (core->cons);
// Get conversation
R2AI_Messages *messages = r2ai_conversation_get ();
R2AI_Messages *messages = r2ai_conversation_get (state);
if (!messages) {
R_LOG_ERROR ("Conversation not initialized");
return;
@@ -266,24 +314,24 @@ R_IPI void cmd_r2ai_a(RCore *core, const char *user_query) {
// Add user query to the conversation (no system prompt)
// If this is the first message in a new conversation, clear previous history
if (messages->n_messages == 0 || r_config_get_b (core->config, "r2ai.auto.reset_on_query")) {
if (r_list_length (messages->messages) == 0 || r_config_get_b (core->config, "r2ai.auto.reset_on_query")) {
r2ai_msgs_clear (messages);
}
// Add user query
R2AI_Message user_msg = {
.role = "user",
.content = user_query
.content = (char *)user_query
};
r2ai_msgs_add (messages, &user_msg);
process_messages (core, messages, NULL, 1);
process_messages (cps, messages, NULL, 1);
}
// Helper function to display content with length indication for long content
static void print_content_with_length(RCore *core, const char *content, const char *empty_msg, bool always_show_length) {
if (!content || *content == '\0') {
R2_PRINTF ("%s\n", empty_msg ? empty_msg : "<no content>");
r_cons_printf (core->cons, "%s\n", empty_msg? empty_msg: "<no content>");
return;
}
@@ -293,24 +341,26 @@ static void print_content_with_length(RCore *core, const char *content, const ch
if (content_len > max_display) {
// Truncate long content and show length
char *truncated = r_str_ndup (content, max_display);
R2_PRINTF ("%s... \x1b[1;37m(length: %zu chars)\x1b[0m\n",
r_cons_printf (core->cons, "%s... \x1b[1;37m(length: %zu chars)\x1b[0m\n",
truncated, content_len);
free (truncated);
} else if (always_show_length) {
// Always show length for certain types (like tool responses)
R2_PRINTF ("%s \x1b[1;37m(length: %zu chars)\x1b[0m\n",
r_cons_printf (core->cons, "%s \x1b[1;37m(length: %zu chars)\x1b[0m\n",
content, content_len);
} else {
R2_PRINTF ("%s\n", content);
r_cons_printf (core->cons, "%s\n", content);
}
}
// Add this function right after cmd_r2ai_a
R_IPI void cmd_r2ai_logs(RCore *core) {
R_IPI void cmd_r2ai_logs(RCorePluginSession *cps) {
RCore *core = cps->core;
R2AI_State *state = cps->data;
// Get conversation
R2AI_Messages *messages = r2ai_conversation_get ();
if (!messages || messages->n_messages == 0) {
R2_PRINTF ("No conversation history available\n");
R2AI_Messages *messages = r2ai_conversation_get (state);
if (!messages || r_list_length (messages->messages) == 0) {
r_cons_printf (core->cons, "No conversation history available\n");
return;
}
@@ -326,88 +376,79 @@ R_IPI void cmd_r2ai_logs(RCore *core) {
pj_a (pj);
for (int i = 0; i < messages->n_messages; i++) {
const R2AI_Message *msg = &messages->messages[i];
for (int i = 0; i < r_list_length (messages->messages); i++) {
const R2AI_Message *msg = r_list_get_n (messages->messages, i);
pj_o (pj);
pj_ks (pj, "role", msg->role ? msg->role : "unknown");
pj_ks (pj, "content", msg->content ? msg->content : "");
pj_ks (pj, "role", msg->role? msg->role: "unknown");
pj_ks (pj, "content", msg->content? msg->content: "");
if (msg->tool_calls && msg->n_tool_calls > 0) {
pj_ka (pj, "tool_calls");
for (int j = 0; j < msg->n_tool_calls; j++) {
const R2AI_ToolCall *tc = &msg->tool_calls[j];
pj_o (pj);
if (tc->name) {
pj_ks (pj, "name", tc->name);
}
if (tc->arguments) {
pj_ks (pj, "arguments", tc->arguments);
}
pj_end (pj);
}
pj_end (pj);
}
pj_end (pj);
}
pj_end (pj);
char *json_str = pj_drain (pj);
R2_PRINTF ("%s\n", json_str);
r_cons_printf (core->cons, "%s\n", json_str);
free (json_str);
return;
}
R2_PRINTF ("\x1b[1;34m[r2ai] Chat Logs (%d messages)\x1b[0m\n",
core->cons, messages->n_messages);
r_cons_printf (core->cons, "\x1b[1;34m[r2ai] Chat Logs (%d messages)\x1b[0m\n",
r_list_length (messages->messages));
R2_PRINTF ("\x1b[1;33mNote: System prompt is applied automatically but not stored in history\x1b[0m\n\n");
r_cons_printf (core->cons, "\x1b[1;33mNote: System prompt is applied automatically but not stored in history\x1b[0m\n\n");
// Display each message in the conversation
for (int i = 0; i < messages->n_messages; i++) {
const R2AI_Message *msg = &messages->messages[i];
for (int i = 0; i < r_list_length (messages->messages); i++) {
const R2AI_Message *msg = r_list_get_n (messages->messages, i);
const char *role = msg->role;
// Format based on role
if (!strcmp (role, "user")) {
R2_PRINTF ("\x1b[1;32m[user]:\x1b[0m ");
r_cons_printf (core->cons, "\x1b[1;32m[user]:\x1b[0m ");
print_content_with_length (core, msg->content, "<no content>", false);
} else if (!strcmp (role, "assistant")) {
R2_PRINTF ("\x1b[1;36m[assistant]:\x1b[0m ");
r_cons_printf (core->cons, "\x1b[1;36m[assistant]:\x1b[0m ");
print_content_with_length (core, msg->content, "<no content>", false);
// Show tool calls if present
if (msg->tool_calls && msg->n_tool_calls > 0) {
for (int j = 0; j < msg->n_tool_calls; j++) {
const R2AI_ToolCall *tc = &msg->tool_calls[j];
R2_PRINTF (" \x1b[1;35m[tool call]:\x1b[0m %s\n",
tc->name ? tc->name : "<unnamed>");
r_cons_printf (core->cons, " \x1b[1;35m[tool call]:\x1b[0m %s\n",
tc->name? tc->name: "<unnamed>");
if (tc->arguments) {
R2_PRINTF (" %s\n", tc->arguments);
r_cons_printf (core->cons, " %s\n", tc->arguments);
}
}
}
} else if (!strcmp (role, "tool")) {
R2_PRINTF ("\x1b[1;35m[tool]:\x1b[0m ");
r_cons_printf (core->cons, "\x1b[1;35m[tool]:\x1b[0m ");
print_content_with_length (core, msg->content, "<no result>", true);
// Don't show the tool call ID as requested
} else {
// Other roles (system, etc.)
R2_PRINTF ("\x1b[1;37m[%s]:\x1b[0m ", role);
r_cons_printf (core->cons, "\x1b[1;37m[%s]:\x1b[0m ", role);
print_content_with_length (core, msg->content, "<no content>", false);
}
R2_NEWLINE ();
R2_FLUSH ();
r_cons_newline (core->cons);
r_cons_flush (core->cons);
}
}
-39
View File
@@ -1,39 +0,0 @@
import re
import sys
import subprocess
arg = sys.argv[1]
# Step 1: Format the file in-place using clang-format
subprocess.run(["clang-format", "-i", arg], check=True)
def is_function(s):
return s and not s[0].isspace()
def is_control_structure(s):
return s in {"if", "for", "while", "switch", "catch", "return"}
def fix_line(line):
# Skip lines that are empty or only whitespace
if not line.strip():
return line
# Match function calls like: foo(bar) => foo (bar)
# Avoid if/for/while/catch/return and function *definitions*
pattern = r'\b([a-zA-Z_]\w*)\('
def replacer(match):
name = match.group(1)
if is_control_structure(name) or is_function(line):
return match.group(0) # No change
return f'{name} ('
return re.sub(pattern, replacer, line)
# Step 2: Read the file, transform it, and write it back
with open(arg, "r", encoding="utf-8") as f:
lines = f.readlines()
with open(arg, "w", encoding="utf-8") as f:
for line in lines:
f.write(fix_line(line))
+364
View File
@@ -0,0 +1,364 @@
#define R_LOG_ORIGIN "llm"
#include "r2ai.h"
#include "r2ai_priv.h"
#include <string.h>
static const R2AIProvider r2ai_providers[] = {
{ "openai", "https://api.openai.com/v1", true, false, false, false },
{ "gemini", "https://generativelanguage.googleapis.com/v1beta/openai", true, false, false, false },
{ "anthropic", "https://api.anthropic.com/v1", true, true, false, false },
{ "ollama", "http://localhost:11434/api", false, false, true, true },
{ "openapi", "http://127.0.0.1:11434", false, false, false, false },
{ "xai", "https://api.x.ai/v1", true, false, false, false },
{ "openrouter", "https://openrouter.ai/api/v1", true, false, false, false },
{ "groq", "https://api.groq.com/openai/v1", true, false, false, false },
{ "mistral", "https://api.mistral.ai/v1", true, false, false, false },
{ NULL, NULL, false, false, false, false } // sentinel
};
R_IPI const R2AIProvider *r2ai_get_provider(const char *name) {
if (R_STR_ISEMPTY (name)) {
return NULL;
}
for (int i = 0; r2ai_providers[i].name; i++) {
if (!strcmp (name, r2ai_providers[i].name)) {
return &r2ai_providers[i];
}
}
return NULL;
}
// Forward declaration for rawtools
R_IPI R2AI_ChatResponse *r2ai_llmcall(RCorePluginSession *cps, R2AIArgs args) {
RCore *core = cps->core;
// Check if rawtools mode is enabled
bool rawtools_enabled = r_config_get_b (core->config, "r2ai.auto.raw");
const char *provider = args.provider? args.provider: r_config_get (core->config, "r2ai.api");
if (!provider) {
provider = "gemini";
}
if (rawtools_enabled && args.tools && args.tools->n_tools > 0) {
return r2ai_rawtools_llmcall (cps, args);
}
R2AI_State *state = cps->data;
R2AI_ChatResponse *res = NULL;
if (!args.model) {
const char *config_model = r_config_get (core->config, "r2ai.model");
args.model = strdup (config_model? config_model: "");
}
if (!args.provider) {
args.provider = strdup (provider);
}
if (!args.max_tokens) {
args.max_tokens = r_config_get_i (core->config, "r2ai.max_tokens");
}
if (!args.temperature) {
const char *configtemp = r_config_get (core->config, "r2ai.temperature");
args.temperature = configtemp? atof (configtemp): 0;
}
const R2AIProvider *prov = r2ai_get_provider (provider);
if (!prov) {
R_LOG_ERROR ("Unknown provider: %s", provider);
return NULL;
}
char *api_key = NULL;
if (prov->requires_api_key) {
api_key = r2ai_get_api_key (core, provider);
if (api_key) {
args.api_key = api_key;
}
}
// Make sure we have an API key before proceeding
if (prov->requires_api_key) {
if (R_STR_ISEMPTY (args.api_key)) {
char *Provider = strdup (provider);
r_str_case (Provider, true);
R_LOG_ERROR ("No API key found for %s provider. Please set one with: r2ai "
"%s_API_KEY=YOUR_KEY",
provider, Provider);
free (Provider);
return NULL;
}
}
// Set system_prompt from config if it's not already set
if (!args.system_prompt) {
args.system_prompt = r_config_get (core->config, "r2ai.system");
}
if (!args.messages) {
args.messages = r2ai_msgs_new ();
}
int context_pullback = -1;
// context and user message
if (args.input && r_config_get_b (core->config, "r2ai.data")) {
const int K = r_config_get_i (core->config, "r2ai.data.nth");
if (!state->db) {
state->db = r_vdb_new (R2AI_DEFAULT_VECTORS);
r2ai_refresh_embeddings (cps);
}
RStrBuf *sb = r_strbuf_new ("");
r_strbuf_appendf (sb, "\n ## Query\n\n%s\n ## Context\n", args.input);
RVdbResultSet *rs = r_vdb_query (state->db, args.input, K);
if (rs) {
int i;
for (i = 0; i < rs->size; i++) {
RVdbResult *r = &rs->results[i];
KDNode *n = r->node;
r_strbuf_appendf (sb, "- %s.\n", n->text);
}
r_vdb_result_free (rs);
}
char *m = r_strbuf_drain (sb);
R2AI_Message msg = { .role = "user", .content = m };
context_pullback = r_list_length (args.messages->messages);
r2ai_msgs_add (args.messages, &msg);
free (m);
// TODO: we can save the msg without context
}
// Add the rest of the messages one by one
if (!args.messages && args.input) {
R2AI_Message msg = { .role = "user", .content = (char *)args.input };
args.messages = r2ai_msgs_new ();
r2ai_msgs_add (args.messages, &msg);
}
args.thinking_tokens = r_config_get_i (core->config, "r2ai.thinking_tokens");
const R2AIProvider *p = r2ai_get_provider (provider);
if (p && p->uses_anthropic_header) {
res = r2ai_anthropic (cps, args);
} else {
res = r2ai_openai (cps, args);
}
if (context_pullback != -1) {
R2AI_Message *msg = r_list_get_n (args.messages->messages, context_pullback);
free ((char *)msg->content);
msg->content = strdup (args.input);
}
if (*args.error) {
R_LOG_ERROR ("%s", *args.error);
free (*args.error);
*args.error = NULL;
}
return res;
}
/* Return a malloc'd API key read from the environment or from ~/.r2ai.<provider>-key
* Caller is responsible for freeing the returned string (or NULL if not found). */
R_IPI char *r2ai_get_api_key(RCore *core, const char *provider) {
(void)core;
char *api_key = NULL;
char *api_key_env = r_str_newf ("%s_API_KEY", provider);
r_str_case (api_key_env, true);
char *s = r_sys_getenv (api_key_env);
free (api_key_env);
if (R_STR_ISNOTEMPTY (s)) {
api_key = s;
} else {
free (s);
char *api_key_filename = r_str_newf ("~/.r2ai.%s-key", provider);
char *absolute_apikey = r_file_abspath (api_key_filename);
if (r_file_exists (absolute_apikey)) {
api_key = r_file_slurp (absolute_apikey, NULL);
if (api_key) {
r_str_trim (api_key);
}
}
free (api_key_filename);
free (absolute_apikey);
}
return api_key;
}
R_IPI const char *r2ai_get_provider_url(RCore *core, const char *provider) {
const R2AIProvider *p = r2ai_get_provider (provider);
if (!p) {
return NULL;
}
// Handle providers that support custom baseurl
if (!strcmp (provider, "openai") || !strcmp (provider, "ollama")) {
const char *host = r_config_get (core->config, "r2ai.baseurl");
if (R_STR_ISNOTEMPTY (host)) {
if (r_str_startswith (host, "http")) {
if (!strcmp (provider, "openai")) {
return r_str_newf ("%s/v1", host);
} else {
return r_str_newf ("%s/api", host);
}
}
if (!strcmp (provider, "openai")) {
return r_str_newf ("http://%s/v1", host);
} else {
return r_str_newf ("http://%s/api", host);
}
}
}
return p->url;
}
R_IPI RList *r2ai_fetch_available_models(RCore *core, const char *provider) {
const char *purl = r2ai_get_provider_url (core, provider);
if (!purl) {
return NULL;
}
// Create models endpoint URL
char *models_url = NULL;
const R2AIProvider *prov = r2ai_get_provider (provider);
if (prov && prov->uses_tags_endpoint) {
models_url = r_str_newf ("%s/tags", purl);
} else {
models_url = r_str_newf ("%s/models", purl);
}
if (!models_url) {
return NULL;
}
// Get API key for authentication (except for providers that don't require it)
char *api_key = NULL;
const R2AIProvider *p = r2ai_get_provider (provider);
if (p && p->requires_api_key) {
// Consolidated helper to fetch the API key from env or file
api_key = r2ai_get_api_key (core, provider);
}
int code = 0;
char *response = NULL;
if (api_key) {
const char *headers[4] = { "Content-Type: application/json", NULL, NULL, NULL };
char *auth_header = NULL;
char *version_header = NULL;
const R2AIProvider *prov = r2ai_get_provider (provider);
if (prov && prov->uses_anthropic_header) {
// Anthropic uses different header format
auth_header = r_str_newf ("x-api-key: %s", api_key);
version_header = strdup ("anthropic-version: 2023-06-01");
headers[1] = auth_header;
headers[2] = version_header;
} else {
// Standard OpenAI-compatible format
auth_header = r_str_newf ("Authorization: Bearer %s", api_key);
headers[1] = auth_header;
}
// Make HTTP GET request
R_LOG_DEBUG ("GET %s Headers: %s", models_url, headers);
response = r2ai_http_get (core, models_url, headers, &code, NULL);
free (auth_header);
free (version_header);
free (api_key);
} else {
// We have no headers
R_LOG_DEBUG ("GET %s", models_url);
response = r2ai_http_get (core, models_url, NULL, &code, NULL);
}
free (models_url);
if (!response || code != 200) {
R_LOG_DEBUG ("Failed to fetch models from %s (code: %d)", provider, code);
free (response);
return NULL;
}
// Parse JSON response
RList *models = r_list_newf (free);
if (!models) {
free (response);
return NULL;
}
RJson *json = r_json_parse (response);
if (json) {
const RJson *data;
const R2AIProvider *prov = r2ai_get_provider (provider);
if (prov && prov->uses_tags_endpoint) {
data = r_json_get (json, "models");
} else {
data = r_json_get (json, "data");
}
if (data && data->type == R_JSON_ARRAY) {
const RJson *model_item = data->children.first;
while (model_item) {
const RJson *id;
if (prov && prov->uses_tags_endpoint) {
id = r_json_get (model_item, "model");
} else {
id = r_json_get (model_item, "id");
}
if (id && id->type == R_JSON_STRING && R_STR_ISNOTEMPTY (id->str_value)) {
R_LOG_DEBUG ("Model: %s", id->str_value);
r_list_append (models, strdup (id->str_value));
}
model_item = model_item->next;
}
}
r_json_free (json);
}
free (response);
return models;
}
R_IPI void r2ai_list_providers(RCore *core, RStrBuf *sb) {
size_t i;
for (i = 0; r2ai_providers[i].name; i++) {
if (sb) {
if (i > 0) {
r_strbuf_append (sb, ", ");
}
r_strbuf_append (sb, r2ai_providers[i].name);
} else {
r_cons_println (core->cons, r2ai_providers[i].name);
}
}
}
R_IPI void r2ai_refresh_embeddings(RCorePluginSession *cps) {
RCore *core = cps->core;
R2AI_State *state = cps->data;
RListIter *iter, *iter2;
char *line;
char *file;
// refresh embeddings database
r_vdb_free (state->db);
state->db = r_vdb_new (R2AI_DEFAULT_VECTORS);
// enumerate .txt files in directory
const char *path = r_config_get (core->config, "r2ai.data.path");
RList *files = r_sys_dir (path);
if (r_list_empty (files)) {
R_LOG_WARN ("Cannot find any file in r2ai.data.path");
}
r_list_foreach (files, iter, file) {
if (!r_str_endswith (file, ".txt")) {
continue;
}
R_LOG_DEBUG ("Index %s", file);
char *filepath = r_file_new (path, file, NULL);
char *text = r_file_slurp (filepath, NULL);
if (text) {
R_LOG_DEBUG ("Index %s", file);
RList *lines = r_str_split_list (text, "\n", -1);
r_list_foreach (lines, iter2, line) {
if (r_str_trim_head_ro (line)[0] == 0) {
continue;
}
r_vdb_insert (state->db, line);
R_LOG_DEBUG ("Insert %s", line);
}
r_list_free (lines);
}
free (filepath);
}
r_list_free (files);
}
+102 -19
View File
@@ -1,31 +1,114 @@
/* Copyright r2ai - 2023-2025 - pancake */
#include <r_core.h>
#include "r2ai.h"
#include "r2ai_priv.h"
int main(int argc, char **argv) {
if (argc < 2) {
eprintf ("Usage: r2ai <prompt>\n");
return 1;
static void show_help() {
printf ("Usage: r2ai [-vhp:m:q:E] <prompt>\n"
" -v Show version information\n"
" -h Show this help message\n"
" -p <provider> Select LLM provider\n"
" -m <model> Select LLM model\n"
" -q <query> Execute predefined prompt query (can be used multiple times)\n"
" -E Edit the r2ai rc file\n");
}
static void show_version() {
printf ("r2ai " R2AI_VERSION "\n");
}
int main(int argc, const char **argv) {
int c;
const char *provider = NULL;
const char *model = NULL;
RList *queries = r_list_newf (free);
RGetopt opt;
r_getopt_init (&opt, argc, argv, "vhp:m:q:E");
while ((c = r_getopt_next (&opt)) != -1) {
switch (c) {
case 'v':
show_version ();
r_list_free (queries);
return 0;
case 'h':
show_help ();
r_list_free (queries);
return 0;
case 'p':
provider = opt.arg;
break;
case 'm':
model = opt.arg;
break;
case 'q':
r_list_append (queries, strdup (opt.arg));
break;
case 'E':
{
RCore *core = r_core_new ();
RCorePluginSession cps = {
.core = core
};
r2ai_init (&cps);
char *rc_path = r_file_home (".config/r2ai/rc");
r_cons_editor (core->cons, rc_path, NULL);
free (rc_path);
r_list_free (queries);
r2ai_fini (&cps);
r_core_free (core);
return 0;
}
default:
show_help ();
r_list_free (queries);
return 1;
}
}
const char *prompt = argv[1];
RCore *core = r_core_new ();
char *err = NULL;
R2AIArgs args = {
.input = prompt,
.error = &err,
.dorag = false,
RCorePluginSession cps = {
.core = core
};
r2ai_init (&cps);
char *res = r2ai (core, args);
if (res) {
R_LOG_INFO (res);
free (res);
// Process queries if any
if (!r_list_empty (queries)) {
RListIter *iter;
char *query;
r_list_foreach (queries, iter, query) {
r2ai_cmd_q (&cps, query);
}
} else {
if (opt.ind >= argc) {
// List available prompts
r2ai_cmd_q (&cps, "");
r_list_free (queries);
r2ai_fini (&cps);
r_core_free (core);
return 0;
}
const char *prompt = argv[opt.ind];
char *err = NULL;
R2AIArgs args = {
.input = prompt,
.provider = provider,
.model = model,
.error = &err,
.dorag = false,
};
char *res = r2ai (&cps, args);
if (res) {
r_cons_println (core->cons, res);
free (res);
}
free (err);
r_cons_flush (core->cons);
}
free (err);
r_list_free (queries);
r2ai_fini (&cps);
r_core_free (core);
return 0;
}
+7 -7
View File
@@ -34,13 +34,13 @@ R_API RMarkdownTheme r2ai_markdown_theme_default(void) {
.heading4 = "\x1b[1;4;34m", // Bold, underlined, blue
.heading5 = "\x1b[1;4;35m", // Bold, underlined, magenta
.heading6 = "\x1b[1;4;36m", // Bold, underlined, cyan
// List items
// List items
.list_bullet = "• ", // Bullet character
.list_number = "\x1b[1m%s.\x1b[0m ", // Bold number
// Checkbox states
// Checkbox states
.checkbox_checked = "\x1b[32m[✓]\x1b[0m ", // Green checkmark
.checkbox_unchecked = "\x1b[90m[ ]\x1b[0m ", // Gray empty box
// Reset code
// Reset code
.reset = "\x1b[0m"
};
return theme;
@@ -103,7 +103,7 @@ static void append_formatted(RStrBuf *sb, const char *text, int len, MarkdownSta
static int count_indent(const char *str) {
int count = 0;
while (*str == ' ' || *str == '\t') {
count += (*str == '\t') ? 4 : 1;
count += (*str == '\t')? 4: 1;
str++;
}
return count;
@@ -257,7 +257,7 @@ R_API char *r2ai_markdown(const char *markdown) {
append_formatted (sb, start, digit_start - start, state);
int num_len = p - digit_start;
char num_buf[16] = { 0 };
r_str_ncpy (num_buf, digit_start, num_len < 15 ? num_len + 1 : 15);
r_str_ncpy (num_buf, digit_start, num_len < 15? num_len + 1: 15);
// Use the actual number from markdown and format it
char formatted_number[32];
@@ -302,7 +302,7 @@ R_API char *r2ai_markdown(const char *markdown) {
if (p > start) {
append_formatted (sb, start, p - start, state);
}
p += double_marker ? 2 : 1;
p += double_marker? 2: 1;
start = p;
if (double_marker && marker == '*') {
@@ -317,7 +317,7 @@ R_API char *r2ai_markdown(const char *markdown) {
} else if (p > markdown && isalnum (*(p - 1)) && !isalnum (p[1 + double_marker])) {
// Closing marker
append_formatted (sb, start, p - start, state);
p += double_marker ? 2 : 1;
p += double_marker? 2: 1;
start = p;
state = NORMAL;
} else {
+4 -1
View File
@@ -1,5 +1,5 @@
project('r2ai', 'c',
version : '0.1.0',
version : '1.2.2',
default_options : ['warning_level=2', 'c_std=c11']
)
@@ -188,7 +188,10 @@ srcs = files(
'r2ai.c',
'auto.c',
'vdb.c',
'llm.c',
'tools.c',
'query.c',
'rawtools.c',
'messages.c',
'anthropic.c',
'r2ai_http.c',
+1 -1
View File
@@ -1,6 +1,6 @@
option('r2_prefix',
type: 'string',
value: '',
value: '/usr/local',
description: 'Path prefix where radare2 is installed (include/ and lib/).'
)
+102 -156
View File
@@ -2,12 +2,8 @@
#include "r2ai.h"
// AITODO: remove globals
// Static global message store for session persistence
static R2AI_Messages *conversation = NULL;
#define INITIAL_CAPACITY 8
#define GROWTH_FACTOR 1.5
#define GROWTH_FACTOR 1.5
R_API void r2ai_message_free(R2AI_Message *msg) {
if (!msg) {
@@ -15,7 +11,7 @@ R_API void r2ai_message_free(R2AI_Message *msg) {
}
free (msg->role);
free (msg->content);
free ((void *)msg->content); // maybe double free
free (msg->reasoning_content);
free (msg->tool_call_id);
@@ -46,24 +42,19 @@ R_API void r2ai_message_free(R2AI_Message *msg) {
}
}
// Initialize conversation container (call this during plugin init)
R_API void r2ai_conversation_init(void) {
if (conversation) {
// Already initialized
// Conversation is now stored in R2AI_State
R_API void r2ai_conversation_init(R2AI_State *state) {
if (!state || state->conversation) {
// Already initialized or invalid state
return;
}
conversation = R_NEW0 (R2AI_Messages);
conversation->cap_messages = INITIAL_CAPACITY;
conversation->messages = R_NEWS0 (R2AI_Message, conversation->cap_messages);
if (!conversation->messages) {
R_FREE (conversation);
}
state->conversation = r2ai_msgs_new ();
}
// Get the conversation instance (returns NULL if not initialized)
R_API R2AI_Messages *r2ai_conversation_get(void) {
return conversation;
R_API R2AI_Messages *r2ai_conversation_get(R2AI_State *state) {
return state? state->conversation: NULL;
}
// Create a new temporary messages container
@@ -72,12 +63,12 @@ R_API R2AI_Messages *r2ai_msgs_new(void) {
if (!msgs) {
return NULL;
}
msgs->cap_messages = INITIAL_CAPACITY;
msgs->messages = R_NEWS0 (R2AI_Message, msgs->cap_messages);
msgs->messages = r_list_new ();
if (!msgs->messages) {
R_FREE (msgs);
return NULL;
}
msgs->messages->free = (RListFree)r2ai_message_free;
return msgs;
}
@@ -86,38 +77,19 @@ R_API void r2ai_msgs_free(R2AI_Messages *msgs) {
return;
}
// Don't actually free if it's our static instance
if (msgs == conversation) {
// Just clear the messages but keep the allocated structure
for (int i = 0; i < msgs->n_messages; i++) {
r2ai_message_free (&msgs->messages[i]);
}
msgs->n_messages = 0;
return;
}
// Normal free for non-static instances
if (msgs->messages) {
for (int i = 0; i < msgs->n_messages; i++) {
r2ai_message_free (&msgs->messages[i]);
}
R_FREE (msgs->messages);
r_list_free (msgs->messages);
}
R_FREE (msgs);
}
// Free the conversation when plugin is unloaded
R_API void r2ai_conversation_free(void) {
if (conversation) {
if (conversation->messages) {
for (int i = 0; i < conversation->n_messages; i++) {
r2ai_message_free (&conversation->messages[i]);
}
R_FREE (conversation->messages);
}
R_FREE (conversation);
conversation = NULL;
R_API void r2ai_conversation_free(R2AI_State *state) {
if (state && state->conversation) {
r2ai_msgs_free (state->conversation);
state->conversation = NULL;
}
}
@@ -127,10 +99,7 @@ R_API void r2ai_msgs_clear(R2AI_Messages *msgs) {
return;
}
for (int i = 0; i < msgs->n_messages; i++) {
r2ai_message_free (&msgs->messages[i]);
}
msgs->n_messages = 0;
r_list_purge (msgs->messages);
}
R_API bool r2ai_msgs_add(R2AI_Messages *msgs, const R2AI_Message *msg) {
@@ -138,91 +107,79 @@ R_API bool r2ai_msgs_add(R2AI_Messages *msgs, const R2AI_Message *msg) {
return false;
}
// Check if we need to resize
if (msgs->n_messages >= msgs->cap_messages) {
int new_cap = msgs->cap_messages * GROWTH_FACTOR;
R2AI_Message *new_messages = realloc (msgs->messages, sizeof (R2AI_Message) * new_cap);
if (!new_messages) {
return false;
}
msgs->messages = new_messages;
msgs->cap_messages = new_cap;
// Zero the newly allocated portion
memset (&msgs->messages[msgs->n_messages], 0,
sizeof (R2AI_Message) * (msgs->cap_messages - msgs->n_messages));
R2AI_Message *new_msg = R_NEW0 (R2AI_Message);
if (!new_msg) {
return false;
}
// Copy the message to the array
R2AI_Message *dest = &msgs->messages[msgs->n_messages++];
dest->role = msg->role ? strdup (msg->role) : NULL;
dest->content = msg->content ? strdup (msg->content) : NULL;
dest->reasoning_content = msg->reasoning_content ? strdup (msg->reasoning_content) : NULL;
new_msg->role = msg->role? strdup (msg->role): NULL;
new_msg->content = msg->content? strdup (msg->content): NULL;
new_msg->reasoning_content = msg->reasoning_content? strdup (msg->reasoning_content): NULL;
if (msg->content_blocks) {
R2AI_ContentBlocks *cb = R_NEW0 (R2AI_ContentBlocks);
if (!cb) {
r2ai_message_free (dest);
msgs->n_messages--;
r2ai_message_free (new_msg);
free (new_msg);
return false;
}
cb->n_blocks = msg->content_blocks->n_blocks;
cb->blocks = R_NEWS0 (R2AI_ContentBlock, cb->n_blocks);
if (!cb->blocks) {
free (cb);
r2ai_message_free (dest);
msgs->n_messages--;
r2ai_message_free (new_msg);
free (new_msg);
return false;
}
for (int i = 0; i < cb->n_blocks; i++) {
R2AI_ContentBlock *src = &msg->content_blocks->blocks[i];
R2AI_ContentBlock *dst = &cb->blocks[i];
dst->type = src->type ? strdup (src->type) : NULL;
dst->data = src->data ? strdup (src->data) : NULL;
dst->thinking = src->thinking ? strdup (src->thinking) : NULL;
dst->signature = src->signature ? strdup (src->signature) : NULL;
dst->text = src->text ? strdup (src->text) : NULL;
dst->id = src->id ? strdup (src->id) : NULL;
dst->name = src->name ? strdup (src->name) : NULL;
dst->input = src->input ? strdup (src->input) : NULL;
dst->type = src->type? strdup (src->type): NULL;
dst->data = src->data? strdup (src->data): NULL;
dst->thinking = src->thinking? strdup (src->thinking): NULL;
dst->signature = src->signature? strdup (src->signature): NULL;
dst->text = src->text? strdup (src->text): NULL;
dst->id = src->id? strdup (src->id): NULL;
dst->name = src->name? strdup (src->name): NULL;
dst->input = src->input? strdup (src->input): NULL;
}
dest->content_blocks = cb;
new_msg->content_blocks = cb;
}
dest->tool_call_id = msg->tool_call_id ? strdup (msg->tool_call_id) : NULL;
dest->tool_calls = NULL;
dest->n_tool_calls = 0;
new_msg->tool_call_id = msg->tool_call_id? strdup (msg->tool_call_id): NULL;
new_msg->tool_calls = NULL;
new_msg->n_tool_calls = 0;
// Copy tool calls if any
if (msg->tool_calls && msg->n_tool_calls > 0) {
dest->tool_calls = R_NEWS0 (R2AI_ToolCall, msg->n_tool_calls);
if (!dest->tool_calls) {
// Clean up and return error
r2ai_message_free (dest);
msgs->n_messages--;
new_msg->tool_calls = R_NEWS0 (R2AI_ToolCall, msg->n_tool_calls);
if (!new_msg->tool_calls) {
r2ai_message_free (new_msg);
free (new_msg);
return false;
}
dest->n_tool_calls = msg->n_tool_calls;
new_msg->n_tool_calls = msg->n_tool_calls;
for (int i = 0; i < msg->n_tool_calls; i++) {
const R2AI_ToolCall *src_tc = &msg->tool_calls[i];
R2AI_ToolCall *dst_tc = (R2AI_ToolCall *)&dest->tool_calls[i];
R2AI_ToolCall *dst_tc = (R2AI_ToolCall *)&new_msg->tool_calls[i];
dst_tc->name = src_tc->name ? strdup (src_tc->name) : NULL;
dst_tc->arguments = src_tc->arguments ? strdup (src_tc->arguments) : NULL;
dst_tc->id = src_tc->id ? strdup (src_tc->id) : NULL;
dst_tc->name = src_tc->name? strdup (src_tc->name): NULL;
dst_tc->arguments = src_tc->arguments? strdup (src_tc->arguments): NULL;
dst_tc->id = src_tc->id? strdup (src_tc->id): NULL;
}
}
r_list_append (msgs->messages, new_msg);
return true;
}
R_API bool r2ai_msgs_add_tool_call(R2AI_Messages *msgs, const R2AI_ToolCall *tc) {
if (!msgs || !tc || msgs->n_messages == 0) {
if (!msgs || !tc || r_list_length (msgs->messages) == 0) {
return false;
}
R2AI_Message *msg = &msgs->messages[msgs->n_messages - 1];
R2AI_Message *msg = r_list_get_n (msgs->messages, r_list_length (msgs->messages) - 1);
// Allocate or resize the tool_calls array
if (msg->n_tool_calls == 0) {
@@ -233,7 +190,7 @@ R_API bool r2ai_msgs_add_tool_call(R2AI_Messages *msgs, const R2AI_ToolCall *tc)
} else {
R2AI_ToolCall *new_tool_calls = realloc (
(void *)msg->tool_calls,
sizeof (R2AI_ToolCall) * (msg->n_tool_calls + 1));
sizeof (R2AI_ToolCall) *(msg->n_tool_calls + 1));
if (!new_tool_calls) {
return false;
}
@@ -244,9 +201,9 @@ R_API bool r2ai_msgs_add_tool_call(R2AI_Messages *msgs, const R2AI_ToolCall *tc)
// Copy the tool call
R2AI_ToolCall *dst_tc = (R2AI_ToolCall *)&msg->tool_calls[msg->n_tool_calls];
dst_tc->name = tc->name ? strdup (tc->name) : NULL;
dst_tc->arguments = tc->arguments ? strdup (tc->arguments) : NULL;
dst_tc->id = tc->id ? strdup (tc->id) : NULL;
dst_tc->name = tc->name? strdup (tc->name): NULL;
dst_tc->arguments = tc->arguments? strdup (tc->arguments): NULL;
dst_tc->id = tc->id? strdup (tc->id): NULL;
msg->n_tool_calls++;
return true;
@@ -294,8 +251,8 @@ R_API bool r2ai_msgs_from_json(R2AI_Messages *msgs, const RJson *json) {
// Create a new message to add
R2AI_Message new_msg = { 0 };
new_msg.role = (role && role->type == R_JSON_STRING) ? strdup (role->str_value) : strdup ("assistant");
new_msg.content = (content && content->type == R_JSON_STRING) ? strdup (content->str_value) : NULL;
new_msg.role = (role && role->type == R_JSON_STRING)? strdup (role->str_value): strdup ("assistant");
new_msg.content = (content && content->type == R_JSON_STRING)? strdup (content->str_value): NULL;
new_msg.tool_call_id = NULL;
new_msg.tool_calls = NULL;
new_msg.n_tool_calls = 0;
@@ -328,14 +285,14 @@ R_API bool r2ai_msgs_from_json(R2AI_Messages *msgs, const RJson *json) {
const RJson *name = r_json_get (block, "name");
const RJson *input = r_json_get (block, "input");
dst->type = (type && type->type == R_JSON_STRING) ? strdup (type->str_value) : NULL;
dst->data = (data && data->type == R_JSON_STRING) ? strdup (data->str_value) : NULL;
dst->thinking = (thinking && thinking->type == R_JSON_STRING) ? strdup (thinking->str_value) : NULL;
dst->signature = (signature && signature->type == R_JSON_STRING) ? strdup (signature->str_value) : NULL;
dst->text = (text && text->type == R_JSON_STRING) ? strdup (text->str_value) : NULL;
dst->id = (id && id->type == R_JSON_STRING) ? strdup (id->str_value) : NULL;
dst->name = (name && name->type == R_JSON_STRING) ? strdup (name->str_value) : NULL;
dst->input = (input && input->type == R_JSON_STRING) ? strdup (input->str_value) : NULL;
dst->type = (type && type->type == R_JSON_STRING)? strdup (type->str_value): NULL;
dst->data = (data && data->type == R_JSON_STRING)? strdup (data->str_value): NULL;
dst->thinking = (thinking && thinking->type == R_JSON_STRING)? strdup (thinking->str_value): NULL;
dst->signature = (signature && signature->type == R_JSON_STRING)? strdup (signature->str_value): NULL;
dst->text = (text && text->type == R_JSON_STRING)? strdup (text->str_value): NULL;
dst->id = (id && id->type == R_JSON_STRING)? strdup (id->str_value): NULL;
dst->name = (name && name->type == R_JSON_STRING)? strdup (name->str_value): NULL;
dst->input = (input && input->type == R_JSON_STRING)? strdup (input->str_value): NULL;
}
new_msg.content_blocks = cb;
}
@@ -366,21 +323,32 @@ R_API bool r2ai_msgs_from_json(R2AI_Messages *msgs, const RJson *json) {
const RJson *arguments = r_json_get (function, "arguments");
R2AI_ToolCall tc = { 0 };
tc.name = (name && name->type == R_JSON_STRING) ? name->str_value : NULL;
tc.arguments = (arguments && arguments->type == R_JSON_STRING) ? arguments->str_value : NULL;
tc.id = (id && id->type == R_JSON_STRING) ? id->str_value : NULL;
tc.name = (name && name->type == R_JSON_STRING)? name->str_value: NULL;
tc.arguments = (arguments && arguments->type == R_JSON_STRING)? arguments->str_value: NULL;
tc.id = (id && id->type == R_JSON_STRING)? id->str_value: NULL;
if (!r2ai_msgs_add_tool_call (msgs, &tc)) {
break;
}
}
// Add the parsed message to the messages array
if (!r2ai_msgs_add (msgs, &new_msg)) {
r2ai_message_free (&new_msg);
return false;
}
// Free the temporary message struct (strings are duplicated in add)
r2ai_message_free (&new_msg);
return true;
}
return true;
}
R_API char *r2ai_msgs_to_json(const R2AI_Messages *msgs) {
if (!msgs || msgs->n_messages == 0) {
if (!msgs || r_list_length (msgs->messages) == 0) {
return NULL;
}
@@ -391,13 +359,13 @@ R_API char *r2ai_msgs_to_json(const R2AI_Messages *msgs) {
pj_a (pj); // Start array
for (int i = 0; i < msgs->n_messages; i++) {
const R2AI_Message *msg = &msgs->messages[i];
for (int i = 0; i < r_list_length (msgs->messages); i++) {
const R2AI_Message *msg = r_list_get_n (msgs->messages, i);
pj_o (pj); // Start message object
// Add role
pj_ks (pj, "role", msg->role ? msg->role : "user");
pj_ks (pj, "role", msg->role? msg->role: "user");
if (msg->content) {
pj_ks (pj, "content", msg->content);
@@ -435,7 +403,7 @@ R_API char *r2ai_msgs_to_json(const R2AI_Messages *msgs) {
pj_o (pj); // Start function object
// Add name
pj_ks (pj, "name", tc->name ? tc->name : "");
pj_ks (pj, "name", tc->name? tc->name: "");
// Add arguments if present
if (tc->arguments) {
@@ -459,7 +427,7 @@ R_API char *r2ai_msgs_to_json(const R2AI_Messages *msgs) {
}
R_API char *r2ai_msgs_to_anthropic_json(const R2AI_Messages *msgs) {
if (!msgs || msgs->n_messages == 0) {
if (!msgs || r_list_length (msgs->messages) == 0) {
return NULL;
}
@@ -470,14 +438,14 @@ R_API char *r2ai_msgs_to_anthropic_json(const R2AI_Messages *msgs) {
pj_a (pj); // Start array
for (int i = 0; i < msgs->n_messages; i++) {
const R2AI_Message *msg = &msgs->messages[i];
for (int i = 0; i < r_list_length (msgs->messages); i++) {
const R2AI_Message *msg = r_list_get_n (msgs->messages, i);
pj_o (pj); // Start message object
// Add role
const char *role = msg->role ? msg->role : "user";
pj_ks (pj, "role", strcmp (role, "tool") == 0 ? "user" : role);
const char *role = msg->role? msg->role: "user";
pj_ks (pj, "role", strcmp (role, "tool") == 0? "user": role);
if (msg->content_blocks) {
pj_ka (pj, "content"); // Start content array
@@ -550,12 +518,12 @@ R_API char *r2ai_msgs_to_anthropic_json(const R2AI_Messages *msgs) {
const R2AI_ToolCall *tc = &msg->tool_calls[j];
pj_o (pj); // Start tool_calls object
pj_ks (pj, "type", "tool_use");
pj_ks (pj, "id", tc->id ? tc->id : "");
pj_ks (pj, "name", tc->name ? tc->name : "");
pj_ks (pj, "id", tc->id? tc->id: "");
pj_ks (pj, "name", tc->name? tc->name: "");
// Create a non-const copy for r_json_parse
char *arguments_copy = tc->arguments ? strdup (tc->arguments) : NULL;
RJson *arguments = arguments_copy ? r_json_parse (arguments_copy) : NULL;
char *arguments_copy = tc->arguments? strdup (tc->arguments): NULL;
RJson *arguments = arguments_copy? r_json_parse (arguments_copy): NULL;
pj_ko (pj, "input"); // Start input object
if (arguments) {
@@ -588,7 +556,7 @@ R_API char *r2ai_msgs_to_anthropic_json(const R2AI_Messages *msgs) {
// Function to delete the last N messages from conversation history
R_API void r2ai_delete_last_messages(R2AI_Messages *messages, int n) {
if (!messages || messages->n_messages == 0) {
if (!messages || r_list_length (messages->messages) == 0) {
return;
}
@@ -598,17 +566,15 @@ R_API void r2ai_delete_last_messages(R2AI_Messages *messages, int n) {
}
// Make sure we don't try to delete more messages than exist
if (n > messages->n_messages) {
n = messages->n_messages;
int len = r_list_length (messages->messages);
if (n > len) {
n = len;
}
// Free the last n messages
for (int i = messages->n_messages - n; i < messages->n_messages; i++) {
r2ai_message_free (&messages->messages[i]);
// Pop the last n messages
for (int i = 0; i < n; i++) {
r_list_pop (messages->messages);
}
// Update the message count
messages->n_messages -= n;
}
// Helper function to convert RJson to PJ without draining
@@ -617,7 +583,7 @@ R_API PJ *r_json_to_pj(const RJson *json, PJ *existing_pj) {
return existing_pj;
}
PJ *pj = existing_pj ? existing_pj : pj_new ();
PJ *pj = existing_pj? existing_pj: pj_new ();
if (!pj) {
return NULL;
}
@@ -759,23 +725,3 @@ R_API char *r_json_to_string(const RJson *json) {
PJ *pj = r_json_to_pj (json, NULL);
return pj? pj_drain (pj): NULL;
}
// Create a conversation with optional initial user message
R_API R2AI_Messages *create_conversation(const char *user_message) {
// Create a temporary message container (not using static storage)
R2AI_Messages *msgs = r2ai_msgs_new ();
if (!msgs) {
return NULL;
}
// Add user message if provided (no system message - that's added during processing)
if (R_STR_ISNOTEMPTY (user_message)) {
R2AI_Message user_msg = {
.role = "user",
.content = user_message
};
r2ai_msgs_add (msgs, &user_msg);
}
return msgs;
}
+143 -152
View File
@@ -20,37 +20,19 @@ typedef struct {
int error_flags; // Bitfield of ModelErrorFlags
} ModelCompat;
// Hash table to store model compatibility info
static HtPP *model_compat_db = NULL;
#if 0
// Function to check if a model has a specific error flag
static bool model_has_error(const char *provider, const char *model, ModelErrorFlags flag) {
if (!model_compat_db) {
return false;
}
char *key = r_str_newf ("%s:%s", provider, model ? model : "default");
bool found_flag = false;
ModelCompat *compat = ht_pp_find (model_compat_db, key, &found_flag);
free (key);
if (found_flag && compat) {
return (compat->error_flags & flag) != 0;
}
return false;
}
#endif
// Function to add an error flag to a model
static void model_add_error(const char *provider, const char *model, ModelErrorFlags flag) {
if (!model_compat_db) {
model_compat_db = ht_pp_new0 ();
static void model_add_error(R2AI_State *state, const char *provider, const char *model, ModelErrorFlags flag) {
if (!state) {
return;
}
char *key = r_str_newf ("%s:%s", provider, model ? model : "default");
if (!state->model_compat_db) {
state->model_compat_db = ht_pp_new0 ();
}
char *key = r_str_newf ("%s:%s", provider, model? model: "default");
bool found_flag = false;
ModelCompat *compat = ht_pp_find (model_compat_db, key, &found_flag);
ModelCompat *compat = ht_pp_find (state->model_compat_db, key, &found_flag);
if (found_flag && compat) {
// Update existing entry
@@ -60,7 +42,7 @@ static void model_add_error(const char *provider, const char *model, ModelErrorF
compat = R_NEW0 (ModelCompat);
compat->model_id = strdup (key);
compat->error_flags = flag;
ht_pp_insert (model_compat_db, key, compat);
ht_pp_insert (state->model_compat_db, key, compat);
}
free (key);
}
@@ -78,23 +60,27 @@ static bool model_compat_free_cb(void *user, const void *k, const void *v) {
}
// Function to free the model_compat_db hash table
R_IPI void r2ai_openai_fini(void) {
if (model_compat_db) {
ht_pp_foreach (model_compat_db, model_compat_free_cb, NULL);
ht_pp_free (model_compat_db);
model_compat_db = NULL;
R_IPI void r2ai_openai_fini(R2AI_State *state) {
if (state && state->model_compat_db) {
ht_pp_foreach (state->model_compat_db, model_compat_free_cb, NULL);
ht_pp_free (state->model_compat_db);
state->model_compat_db = NULL;
}
}
R_IPI R2AI_ChatResponse *r2ai_openai(RCore *core, R2AIArgs args) {
R_IPI R2AI_ChatResponse *r2ai_openai(RCorePluginSession *cps, R2AIArgs args) {
RCore *core = cps->core;
R2AI_State *state = cps->data;
// Initialize compatibility database if needed
if (!model_compat_db) {
model_compat_db = ht_pp_new0 ();
if (!state->model_compat_db) {
state->model_compat_db = ht_pp_new0 ();
}
args.provider = r_config_get (core->config, "r2ai.api");
args.model = r_config_get (core->config, "r2ai.model");
const char *base_url = r2ai_get_provider_url (core, args.provider);
// TODO: default model name should depend on api
const char *model_name = args.model ? args.model : "gpt-4o-mini";
const char *model_name = args.model? args.model: "gpt-4o-mini";
char **error = args.error;
const R2AI_Tools *tools = args.tools;
// create a temp conversation to include the system prompt and the rest of the messages
@@ -107,7 +93,7 @@ R_IPI R2AI_ChatResponse *r2ai_openai(RCore *core, R2AIArgs args) {
}
R2AI_Message system_msg = {
.role = "system",
.content = args.system_prompt
.content = (char *)args.system_prompt
};
// Add system message if available from args.system_prompt
if (R_STR_ISNOTEMPTY (args.system_prompt)) {
@@ -115,10 +101,10 @@ R_IPI R2AI_ChatResponse *r2ai_openai(RCore *core, R2AIArgs args) {
// if the model name contains "o1" or "o3", it's "developer" role
if (strstr (model_name, "o1") || strstr (model_name, "o3")) {
system_msg.role = "developer";
system_msg.content = args.system_prompt;
system_msg.content = (char *)args.system_prompt;
} else {
system_msg.role = "system";
system_msg.content = args.system_prompt;
system_msg.content = (char *)args.system_prompt;
}
r2ai_msgs_add (temp_msgs, &system_msg);
} else {
@@ -131,20 +117,20 @@ R_IPI R2AI_ChatResponse *r2ai_openai(RCore *core, R2AIArgs args) {
} else {
system_msg.role = "system";
}
system_msg.content = sysprompt;
system_msg.content = (char *)sysprompt;
r2ai_msgs_add (temp_msgs, &system_msg);
}
}
if (args.messages) {
for (int i = 0; i < args.messages->n_messages; i++) {
r2ai_msgs_add (temp_msgs, &args.messages->messages[i]);
for (int i = 0; i < r_list_length (args.messages->messages); i++) {
r2ai_msgs_add (temp_msgs, r_list_get_n (args.messages->messages, i));
}
} else {
R_LOG_WARN ("No messages");
}
// Safely print debug info about first message
if (temp_msgs && temp_msgs->n_messages > 0 && temp_msgs->messages && temp_msgs->messages[0].role) {
R_LOG_DEBUG ("First message role: %s", temp_msgs->messages[0].role);
if (temp_msgs && r_list_length (temp_msgs->messages) > 0 && ((R2AI_Message *)r_list_get_n (temp_msgs->messages, 0))->role) {
R_LOG_DEBUG ("First message role: %s", ((R2AI_Message *)r_list_get_n (temp_msgs->messages, 0))->role);
}
if (error) {
*error = NULL;
@@ -160,20 +146,16 @@ R_IPI R2AI_ChatResponse *r2ai_openai(RCore *core, R2AIArgs args) {
headers[0] = "Content-Type: application/json";
headers[1] = auth_header;
}
const char *openai_url;
if (strcmp (args.provider, "ollama") == 0) {
openai_url = r_str_newf ("%s/chat", base_url);
} else {
openai_url = r_str_newf ("%s/chat/completions", base_url);
}
R_LOG_DEBUG ("OpenAI URL: %s", openai_url);
const char *urlfmt = strcmp (args.provider, "ollama")
? "%s/chat/completions"
: "%s/chat";
char *openai_url = r_str_newf (urlfmt, base_url);
// Create a messages JSON object, either from input messages or from content
char *messages_json = NULL;
if (temp_msgs && temp_msgs->n_messages > 0) {
R_LOG_DEBUG ("Using input messages: %d messages", temp_msgs->n_messages);
if (temp_msgs && r_list_length (temp_msgs->messages) > 0) {
R_LOG_DEBUG ("Using input messages: %d messages", r_list_length (temp_msgs->messages));
messages_json = r2ai_msgs_to_json (temp_msgs);
if (!messages_json) {
if (error) {
@@ -202,19 +184,31 @@ R_IPI R2AI_ChatResponse *r2ai_openai(RCore *core, R2AIArgs args) {
pj_ks (pj, "model", model_name);
pj_kb (pj, "stream", false);
if (strcmp (args.provider, "ollama") == 0) {
// Ollama uses "options" object for parameters
pj_ko (pj, "options");
if (args.max_tokens) {
pj_kn (pj, "num_predict", args.max_tokens);
}
if (args.temperature > 0) {
pj_kd (pj, "temperature", args.temperature);
}
pj_end (pj); // end options
} else {
#if 0
// gpt-5-mini-chat is the only gpt-5 model that supports temperature
// gpt-5 gpt-5-mini and gpt-5-nano just throw an error
// Only add temperature if this provider/model doesn't have the temperature error flag
if (!model_has_error (args.provider, model_name, MODEL_ERROR_TEMPERATURE)) {
pj_kd (pj, "temperature", args.temperature ? args.temperature : 0.01);
}
// gpt-5-mini-chat is the only gpt-5 model that supports temperature
// gpt-5 gpt-5-mini and gpt-5-nano just throw an error
// Only add temperature if this provider/model doesn't have the temperature error flag
if (!model_has_error (args.provider, model_name, MODEL_ERROR_TEMPERATURE)) {
pj_kd (pj, "temperature", args.temperature? args.temperature: 0.01);
}
#endif
if (strcmp (args.provider, "mistral") == 0) {
pj_kn (pj, "max_tokens", args.max_tokens ? args.max_tokens : 5128);
} else {
pj_kn (pj, "max_completion_tokens", args.max_tokens ? args.max_tokens : 5128);
if (strcmp (args.provider, "mistral") == 0) {
pj_kn (pj, "max_tokens", args.max_tokens? args.max_tokens: 5128);
} else {
pj_kn (pj, "max_completion_tokens", args.max_tokens? args.max_tokens: 5128);
}
}
pj_end (pj);
@@ -265,8 +259,13 @@ R_IPI R2AI_ChatResponse *r2ai_openai(RCore *core, R2AIArgs args) {
// Save the full JSON to a file for inspection
// XXX: only create request/response files when r2ai.debug is set
r_file_dump ("/tmp/r2ai_openai_request.json", (const ut8 *)complete_json, strlen (complete_json), 0);
R_LOG_DEBUG ("Full request saved to /tmp/r2ai_openai_request.json");
char *tmpdir = r_file_tmpdir ();
char *req_path = r_str_newf ("%s" R_SYS_DIR "r2ai_openai_request.json", tmpdir);
r_file_dump (req_path, (const ut8 *)complete_json, strlen (complete_json), 0);
R_LOG_DEBUG ("Full request saved to %s", req_path);
free (req_path);
free (tmpdir);
R_LOG_DEBUG ("OpenAI API request data: %s", complete_json);
// Make the API call
@@ -274,6 +273,7 @@ R_IPI R2AI_ChatResponse *r2ai_openai(RCore *core, R2AIArgs args) {
int code = 0;
res = r2ai_http_post (core, openai_url, headers, complete_json, &code, NULL);
free (complete_json);
free (openai_url);
if (code != 200) {
R_LOG_ERROR ("OpenAI API error %d", code);
@@ -281,30 +281,25 @@ R_IPI R2AI_ChatResponse *r2ai_openai(RCore *core, R2AIArgs args) {
R_LOG_ERROR ("OpenAI API error response: %s", res);
// Check for specific error types in the response
ModelErrorFlags error_flag = MODEL_ERROR_NONE;
const char *model_name = args.model ? args.model : "gpt-5-mini";
// Check for temperature errors
if (strstr (res, "temperature")) {
R_LOG_DEBUG ("Detected temperature error for %s model %s", args.provider, model_name);
R_LOG_DEBUG ("Detected temperature error for %s model %s", args.provider, args.model);
error_flag |= MODEL_ERROR_TEMPERATURE;
}
// Add more error type checks as needed
// if (strstr (res, "top_p")) {
// error_flag |= MODEL_ERROR_TOP_P;
// }
if (error_flag != MODEL_ERROR_NONE) {
// Record the error flags for this provider/model
model_add_error (args.provider, model_name, error_flag);
model_add_error (state, args.provider, args.model, error_flag);
// Clean up
free (auth_header);
free (res);
// Retry the call (it will skip problematic parameters this time)
R_LOG_INFO ("Retrying request with adjusted parameters for %s/%s", args.provider, model_name);
return r2ai_openai (core, args);
R_LOG_INFO ("Retrying request with adjusted parameters for %s/%s", args.provider, args.model);
RCorePluginSession retry_cps = { .core = core, .data = state };
return r2ai_openai (&retry_cps, args);
}
}
free (auth_header);
@@ -313,8 +308,13 @@ R_IPI R2AI_ChatResponse *r2ai_openai(RCore *core, R2AIArgs args) {
}
// Save the response for inspection
r_file_dump ("/tmp/r2ai_openai_response.json", (const ut8 *)res, strlen (res), 0);
R_LOG_DEBUG ("OpenAI API response saved to /tmp/r2ai_openai_response.json");
tmpdir = r_file_tmpdir ();
char *res_path = r_str_newf ("%s" R_SYS_DIR "r2ai_openai_response.json", tmpdir);
r_file_dump (res_path, (const ut8 *)res, strlen (res), 0);
R_LOG_DEBUG ("OpenAI API response saved to %s", res_path);
free (res_path);
free (tmpdir);
R_LOG_DEBUG ("OpenAI API response: %s", res);
// Parse the response into our messages structure
@@ -327,84 +327,75 @@ R_IPI R2AI_ChatResponse *r2ai_openai(RCore *core, R2AIArgs args) {
R2AI_Usage *usage = R_NEW0 (R2AI_Usage);
if (message) {
// Process the response using our r2ai_msgs_from_json logic
const RJson *usage_json = r_json_get (jres, "usage");
if (usage_json && usage_json->type == R_JSON_OBJECT) {
const RJson *prompt_tokens = r_json_get (usage_json, "prompt_tokens");
const RJson *completion_tokens = r_json_get (usage_json, "completion_tokens");
const RJson *total_tokens = r_json_get (usage_json, "total_tokens");
if (prompt_tokens && prompt_tokens->type == R_JSON_INTEGER) {
usage->prompt_tokens = prompt_tokens->num.u_value;
}
if (completion_tokens && completion_tokens->type == R_JSON_INTEGER) {
usage->completion_tokens = completion_tokens->num.u_value;
}
if (total_tokens && total_tokens->type == R_JSON_INTEGER) {
usage->total_tokens = total_tokens->num.u_value;
}
const RJson *usage_json = NULL;
if (strcmp (args.provider, "ollama") == 0) {
// Ollama has usage info at top level
usage_json = jres;
} else {
usage_json = r_json_get (jres, "usage");
}
const RJson *choices = r_json_get (jres, "choices");
if (choices && choices->type == R_JSON_ARRAY) {
const RJson *choice = r_json_item (choices, 0);
if (choice) {
const RJson *message_json = r_json_get (choice, "message");
if (message_json) {
const RJson *role = r_json_get (message_json, "role");
const RJson *content = r_json_get (message_json, "content");
const RJson *reasoning_content = r_json_get (message_json, "reasoning_content");
// Set the basic message properties
message->role = (role && role->type == R_JSON_STRING) ? strdup (role->str_value) : strdup ("assistant");
if (content && content->type == R_JSON_STRING) {
message->content = strdup (content->str_value);
}
if (reasoning_content && reasoning_content->type == R_JSON_STRING) {
message->reasoning_content = strdup (reasoning_content->str_value);
}
// Handle tool calls if present
const RJson *tool_calls = r_json_get (message_json, "tool_calls");
if (tool_calls && tool_calls->type == R_JSON_ARRAY && tool_calls->children.count > 0) {
int n_tool_calls = tool_calls->children.count;
R2AI_ToolCall *tool_calls_array = R_NEWS0 (R2AI_ToolCall, n_tool_calls);
if (tool_calls_array) {
for (int i = 0; i < n_tool_calls; i++) {
const RJson *tool_call = r_json_item (tool_calls, i);
if (!tool_call) {
continue;
}
const RJson *id = r_json_get (tool_call, "id");
const RJson *function = r_json_get (tool_call, "function");
if (!function) {
continue;
}
const RJson *name = r_json_get (function, "name");
const RJson *arguments = r_json_get (function, "arguments");
if (id && id->type == R_JSON_STRING) {
tool_calls_array[i].id = strdup (id->str_value);
}
if (name && name->type == R_JSON_STRING) {
tool_calls_array[i].name = strdup (name->str_value);
}
if (arguments && arguments->type == R_JSON_STRING) {
tool_calls_array[i].arguments = strdup (arguments->str_value);
}
}
message->tool_calls = tool_calls_array;
message->n_tool_calls = n_tool_calls;
}
}
if (usage_json && usage_json->type == R_JSON_OBJECT) {
if (strcmp (args.provider, "ollama") == 0) {
// Ollama field names
const RJson *prompt_tokens = r_json_get (usage_json, "prompt_eval_count");
const RJson *completion_tokens = r_json_get (usage_json, "eval_count");
if (prompt_tokens && prompt_tokens->type == R_JSON_INTEGER) {
usage->prompt_tokens = prompt_tokens->num.u_value;
}
if (completion_tokens && completion_tokens->type == R_JSON_INTEGER) {
usage->completion_tokens = completion_tokens->num.u_value;
}
// Calculate total_tokens for ollama
usage->total_tokens = usage->prompt_tokens + usage->completion_tokens;
} else {
// OpenAI field names
const RJson *prompt_tokens = r_json_get (usage_json, "prompt_tokens");
const RJson *completion_tokens = r_json_get (usage_json, "completion_tokens");
const RJson *total_tokens = r_json_get (usage_json, "total_tokens");
if (prompt_tokens && prompt_tokens->type == R_JSON_INTEGER) {
usage->prompt_tokens = prompt_tokens->num.u_value;
}
if (completion_tokens && completion_tokens->type == R_JSON_INTEGER) {
usage->completion_tokens = completion_tokens->num.u_value;
}
if (total_tokens && total_tokens->type == R_JSON_INTEGER) {
usage->total_tokens = total_tokens->num.u_value;
}
}
}
const RJson *message_json = NULL;
if (strcmp (args.provider, "ollama") == 0) {
// Ollama returns message directly
message_json = r_json_get (jres, "message");
} else {
// OpenAI-style response with choices array
const RJson *choices = r_json_get (jres, "choices");
if (choices && choices->type == R_JSON_ARRAY) {
const RJson *choice = r_json_item (choices, 0);
if (choice) {
message_json = r_json_get (choice, "message");
}
}
}
if (message_json) {
const RJson *role = r_json_get (message_json, "role");
const RJson *content = r_json_get (message_json, "content");
const RJson *reasoning_content = r_json_get (message_json, "reasoning_content");
// Set the basic message properties
message->role = (role && role->type == R_JSON_STRING)? strdup (role->str_value): strdup ("assistant");
if (content && content->type == R_JSON_STRING) {
message->content = strdup (content->str_value);
}
if (reasoning_content && reasoning_content->type == R_JSON_STRING) {
message->reasoning_content = strdup (reasoning_content->str_value);
}
// TODO: Handle tool calls if present?
}
}
r_json_free (jres);
R2AI_ChatResponse *result = R_NEW0 (R2AI_ChatResponse);
+346
View File
@@ -0,0 +1,346 @@
/* r2ai - Copyright 2023-2025 pancake */
#include "r2ai.h"
R_API void r2aiprompt_free(R2AIPrompt *prompt) {
if (!prompt) {
return;
}
free (prompt->title);
free (prompt->author);
free (prompt->desc);
free (prompt->command);
free (prompt->prompt);
free (prompt->requires);
free (prompt->if_empty);
free (prompt->if_command);
free (prompt->model);
free (prompt->provider);
free (prompt);
}
static R2AIPrompt *parse_prompt_file(const char *filepath) {
R2AIPrompt *prompt = R_NEW0 (R2AIPrompt);
char *content = r_file_slurp (filepath, NULL);
if (!content) {
free (prompt);
return NULL;
}
// Check if it's a markdown file with YAML frontmatter
if (r_str_startswith (content, "---\n")) {
// Parse YAML frontmatter
char *frontmatter_end = strstr (content + 4, "\n---\n");
if (frontmatter_end) {
char *frontmatter = r_str_ndup (content + 4, frontmatter_end - (content + 4));
RList *lines = r_str_split_list (frontmatter, "\n", -1);
RListIter *iter;
char *line;
r_list_foreach (lines, iter, line) {
r_str_trim (line);
if (*line == 0) {
continue;
}
char *colon = strchr (line, ':');
if (!colon) {
continue;
}
*colon = 0;
char *key = line;
char *value = colon + 1;
r_str_trim (key);
r_str_trim (value);
if (!strcmp (key, "title")) {
prompt->desc = strdup (value);
} else if (!strcmp (key, "author")) {
prompt->author = strdup (value);
} else if (!strcmp (key, "description")) {
prompt->title = strdup (value);
} else if (!strcmp (key, "command")) {
prompt->command = strdup (value);
} else if (!strcmp (key, "model")) {
prompt->model = strdup (value);
} else if (!strcmp (key, "provider")) {
prompt->provider = strdup (value);
}
}
r_list_free (lines);
free (frontmatter);
// The prompt content starts after the second ---
char *prompt_start = frontmatter_end + 5; // Skip "\n---\n"
prompt->prompt = strdup (prompt_start);
}
} else {
// Original parsing for .txt files
RList *lines = r_str_split_list (content, "\n", -1);
RListIter *iter;
char *line;
r_list_foreach (lines, iter, line) {
r_str_trim (line);
if (*line == '#' || *line == 0) {
continue;
}
char *colon = strchr (line, ':');
if (!colon) {
continue;
}
*colon = 0;
char *key = line;
char *value = colon + 1;
r_str_trim (key);
r_str_trim (value);
if (!strcmp (key, "Title")) {
prompt->title = strdup (value);
} else if (!strcmp (key, "Author")) {
prompt->author = strdup (value);
} else if (!strcmp (key, "Description")) {
prompt->desc = strdup (value);
} else if (!strcmp (key, "Command") || !strcmp (key, "Commands")) {
prompt->command = strdup (value);
} else if (!strcmp (key, "Prompt") || !strcmp (key, "Query")) {
prompt->prompt = strdup (value);
} else if (!strcmp (key, "Depends")) {
prompt->
requires
= strdup (value);
} else if (!strcmp (key, "If-Empty")) {
prompt->if_empty = strdup (value);
} else if (!strcmp (key, "If-Command")) {
prompt->if_command = strdup (value);
}
}
r_list_free (lines);
}
free (content);
return prompt;
}
static char *run_commands(RCore *core, const char *commands) {
if (!commands) {
return NULL;
}
RStrBuf *sb = r_strbuf_new ("");
char *cmds_str = strdup (commands);
RList *cmds = r_str_split_list (cmds_str, ";", -1);
RListIter *iter;
char *cmd;
r_list_foreach (cmds, iter, cmd) {
r_str_trim (cmd);
if (*cmd) {
char *out = r_core_cmd_str (core, cmd);
r_strbuf_append (sb, out);
free (out);
}
}
r_list_free (cmds);
free (cmds_str);
return r_strbuf_drain (sb);
}
static char *replace_vars(RCore *core, const char *text) {
if (!text) {
return NULL;
}
RStrBuf *sb = r_strbuf_new ("");
const char *p = text;
while (*p) {
if (*p == '$' && *(p + 1) == '{') {
p += 2;
const char *end = strchr (p, '}');
if (end) {
char *var = r_str_ndup (p, end - p);
char *val = getenv (var);
if (val) {
r_strbuf_append (sb, val);
}
free (var);
p = end + 1;
} else {
r_strbuf_append (sb, "${");
p += 2;
}
} else if (*p == '$' && *(p + 1) == '(') {
p += 2;
const char *end = strchr (p, ')');
if (end) {
char *cmd = r_str_ndup (p, end - p);
char *val = r_core_cmd_str (core, cmd);
r_str_trim (val);
r_strbuf_append (sb, val);
free (val);
free (cmd);
p = end + 1;
} else {
r_strbuf_append (sb, "$(");
p += 2;
}
} else {
r_strbuf_append_n (sb, p, 1);
p++;
}
}
return r_strbuf_drain (sb);
}
R_API void r2ai_cmd_q(RCorePluginSession *cps, const char *input) {
RCore *core = cps->core;
const char *promptdir = r_config_get (core->config, "r2ai.promptdir");
if (!promptdir || !*promptdir) {
promptdir = "~/.config/r2ai/prompts";
}
char *expanded_dir = r_file_abspath (promptdir);
char *local_prompts = r_file_abspath ("../prompts");
RList *search_dirs = r_list_newf (free);
if (r_file_is_directory (expanded_dir)) {
r_list_append (search_dirs, expanded_dir);
} else {
free (expanded_dir);
expanded_dir = NULL;
}
if (r_file_is_directory (local_prompts)) {
r_list_append (search_dirs, local_prompts);
} else {
free (local_prompts);
local_prompts = NULL;
}
if (r_list_empty (search_dirs)) {
R_LOG_ERROR ("No prompt directories found");
r_list_free (search_dirs);
return;
}
if (!input || !*input) {
// list prompts
RListIter *dir_iter;
char *dir;
r_list_foreach (search_dirs, dir_iter, dir) {
RList *files = r_sys_dir (dir);
RListIter *iter;
char *file;
r_list_foreach (files, iter, file) {
if (r_str_endswith (file, ".r2ai.txt") || r_str_endswith (file, ".r2ai.md")) {
char *dot = strchr (file, '.');
char *name = dot? r_str_ndup (file, dot - file): strdup (file);
char *filepath_txt = r_str_newf ("%s/%s.r2ai.txt", dir, name);
char *filepath_md = r_str_newf ("%s/%s.r2ai.md", dir, name);
char *filepath = NULL;
if (r_file_exists (filepath_txt)) {
filepath = filepath_txt;
} else if (r_file_exists (filepath_md)) {
filepath = filepath_md;
}
if (filepath) {
R2AIPrompt *prompt = parse_prompt_file (filepath);
if (prompt) {
r_cons_printf (core->cons, "%s: %s - %s\n", name, prompt->title? prompt->title: "", prompt->desc? prompt->desc: "");
r2aiprompt_free (prompt);
} else {
r_cons_println (core->cons, name);
}
}
free (name);
free (filepath_txt);
free (filepath_md);
}
}
r_list_free (files);
}
} else {
// run prompt
char *name = strdup (input);
char *extra = strchr (name, ' ');
if (extra) {
*extra++ = 0;
r_str_trim (extra);
}
r_str_trim (name);
char *filepath = NULL;
RListIter *dir_iter;
char *dir;
r_list_foreach (search_dirs, dir_iter, dir) {
char *filepath_txt = r_str_newf ("%s/%s.r2ai.txt", dir, name);
char *filepath_md = r_str_newf ("%s/%s.r2ai.md", dir, name);
if (r_file_exists (filepath_txt)) {
filepath = filepath_txt;
free (filepath_md);
break;
} else if (r_file_exists (filepath_md)) {
filepath = filepath_md;
free (filepath_txt);
break;
}
free (filepath_txt);
free (filepath_md);
}
if (!filepath) {
R_LOG_ERROR ("Cannot find prompt file: %s.r2ai.txt or %s.r2ai.md", name, name);
r_list_free (search_dirs);
free (name);
return;
}
R2AIPrompt *prompt = parse_prompt_file (filepath);
if (!prompt) {
R_LOG_ERROR ("Cannot read prompt file: %s", filepath);
free (filepath);
r_list_free (search_dirs);
free (name);
return;
}
if (!prompt->title || !prompt->command) {
R_LOG_WARN ("Prompt %s is missing required Title or Command directive", name);
}
free (filepath);
// run commands
char *cmd_output = run_commands (core, prompt->command);
// run if_command if output not empty
if (cmd_output && *cmd_output && prompt->if_command) {
char *extra_out = run_commands (core, prompt->if_command);
if (extra_out) {
char *new_output = r_str_newf ("%s\n%s", cmd_output, extra_out);
free (cmd_output);
cmd_output = new_output;
free (extra_out);
}
}
// determine the prompt to use
char *use_prompt = prompt->prompt;
if ((!cmd_output || !*cmd_output) && prompt->if_empty) {
if (!strcmp (prompt->if_empty, "exit")) {
R_LOG_ERROR ("Command output is empty, exiting prompt");
r2aiprompt_free (prompt);
free (cmd_output);
free (name);
r_list_free (search_dirs);
return;
} else {
use_prompt = prompt->if_empty;
}
}
// replace vars in prompt
char *replaced_prompt = replace_vars (core, use_prompt);
RStrBuf *sb = r_strbuf_new (replaced_prompt);
free (replaced_prompt);
if (cmd_output) {
r_strbuf_appendf (sb, "\n%s", cmd_output);
free (cmd_output);
}
if (extra) {
r_strbuf_appendf (sb, "\n%s", extra);
}
char *final_prompt = r_strbuf_drain (sb);
// now send to LLM
char *error = NULL;
char *res = r2ai (cps, (R2AIArgs){ .input = final_prompt, .model = prompt->model, .provider = prompt->provider, .error = &error, .dorag = true });
if (error) {
R_LOG_ERROR ("%s", error);
free (error);
} else if (res) {
r_cons_printf (core->cons, "%s\n", res);
free (res);
}
r2aiprompt_free (prompt);
free (final_prompt);
free (name);
}
r_list_free (search_dirs);
}
+158
View File
@@ -0,0 +1,158 @@
.TH R2AI 1 "October 2025" "radare2" "User Commands"
.SH NAME
r2ai \- AI assistant for radare2
.SH SYNOPSIS
.B r2ai
[\fIoptions\fR] [\fIquery\fR]
.SH DESCRIPTION
r2ai is an AI-powered plugin and command-line tool for radare2 that provides assistance with reverse engineering tasks, including decompilation, function analysis, vulnerability detection, and more. It integrates with various LLM providers like OpenAI, Anthropic, and Ollama.
.SH OPTIONS
.TP
.B \-d
Decompile the current function.
.TP
.B \-d \fIquery\fR
Ask a question about the current function.
.TP
.B \-dr
Decompile the current function with one level of recursivity.
.TP
.B \-a \fIquery\fR
Resolve a question using auto mode, which allows the AI to use tools.
.TP
.B \-e \fIk(=v)\fR
Set configuration option (same as 'r2ai.' prefix).
.TP
.B \-h
Show help message.
.TP
.B \-i \fIfile\fR \fIquery\fR
Read a file and ask the LLM with the given query.
.TP
.B \-m
Show selected model, list suggested ones, and choose one.
.TP
.B \-p \fIprovider\fR
Set LLM provider (openai, anthropic, gemini, etc.).
.TP
.B \-q
List available query prompts.
.TP
.B \-q \fIname\fR [\fIinstructions\fR]
Run predefined prompt with optional custom instructions.
.TP
.B \-r
Enter the chat REPL.
.TP
.B \-L
Show chat logs (use -Lj for JSON). Only for auto mode.
.TP
.B \-L-\fIN\fR
Delete the last N messages from the chat history.
.TP
.B \-R
Reset the chat conversation context.
.TP
.B \-Rq [\fItext\fR]
Refresh and query embeddings (see r2ai.data).
.TP
.B \fIquery\fR
Query the selected model and provider with the given query.
.SH CONFIGURATION
Configuration options can be set with \fBr2ai -e \fIkeyname\fR=\fIvalue\fR.
.TP
.B r2ai.api
Name of the provider (e.g., openai). List possibilities with \fBr2ai -e r2ai.api=?\fR.
.TP
.B r2ai.model
Model name. List possibilities with \fBr2ai -e r2ai.model=?\fR.
.TP
.B r2ai.baseurl
Remote LLM base URL (e.g., http://127.0.0.1:11434).
.TP
.B r2ai.max_tokens
Maximum output tokens or total tokens.
.TP
.B r2ai.temperature
Creativity level (0=not creative, 1=very creative).
.TP
.B r2ai.cmds
R2 command to issue and send output in context to model.
.TP
.B r2ai.lang
Programming language for decompilation (only for \fBr2ai -d\fR).
.TP
.B r2ai.hlang
Language for AI responses.
.TP
.B r2ai.prompt
User prompt for \fBr2ai -d\fR.
.TP
.B r2ai.auto.max_runs
Maximum loops in auto mode (\fBr2ai -a\fR).
.TP
.B r2ai.auto.hide_tool_output
Hide tool output in auto mode.
.TP
.B r2ai.auto.yolo
Allow AI to run commands without approval (dangerous, default false).
.TP
.B r2ai.http.use_files
Use files for arguments if CURL argument too long.
.SH QUERY PROMPTS
Query prompts are predefined templates for common tasks. Available prompts:
.TP
.B autoname
Automatically rename functions and variables.
.TP
.B decompile
Decompile the current function.
.TP
.B devices
Analyze device-related code.
.TP
.B dlopen
Analyze dynamic library loading.
.TP
.B explain
Explain the purpose of the current function.
.TP
.B libs
Analyze library usage.
.TP
.B signature
Improve function signatures.
.TP
.B varnames
Rename variables based on context.
.TP
.B vulns
Find vulnerabilities in the current function.
.SH EXAMPLES
.TP
Decompile current function:
.B r2ai -d
.TP
Ask about current function:
.B r2ai -d "what is the input buffer size?"
.TP
Use auto mode:
.B r2ai -a "find vulnerabilities in this function"
.TP
List prompts:
.B r2ai -q
.TP
Run a prompt:
.B r2ai -q explain
.TP
Set provider:
.B r2ai -p openai
.TP
Enter REPL:
.B r2ai -r
.SH SEE ALSO
.BR radare2 (1),
.BR r2pm (1),
.BR r2agent (1)
.SH AUTHOR
radare2 community
+158 -601
View File
File diff suppressed because it is too large. Load diff
+98 -44
View File
@@ -1,25 +1,36 @@
#ifndef R2AI_H
#define R2AI_H
#include <time.h>
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <r_core.h>
#include <r_util/r_json.h>
#include "r_vdb.h"
#include "markdown.h"
#if R2_VERSION_NUMBER >= 50909
#define R2_PRINTF(...) r_cons_printf (core->cons, __VA_ARGS__)
#define R2_FLUSH() r_cons_flush (core->cons)
#define R2_NEWLINE() r_cons_newline (core->cons)
#define R2_PRINTLN(x) r_cons_println (core->cons, x)
#define R2_INTERRUPTED() r_cons_is_breaked (core->cons)
#define R2AI_VERSION "1.2.2"
#if 0
#define R2AI_DEFAULT_MODEL "gpt-5-mini"
#define R2AI_DEFAULT_PROVIDER "openai"
#else
#define R2_PRINTF(...) r_cons_printf (__VA_ARGS__)
#define R2_FLUSH() r_cons_flush ()
#define R2_NEWLINE() r_cons_newline()
#define R2_PRINTLN(x) r_cons_println(x)
#define R2_INTERRUPTED() r_cons_is_breaked ()
// #define R2AI_DEFAULT_MODEL "gpt-oss:20b"
// #define R2AI_DEFAULT_MODEL "gemma3:12b"
#define R2AI_DEFAULT_MODEL "mannix/jan-nano"
#define R2AI_DEFAULT_PROVIDER "ollama"
#endif
#define R2AI_DEFAULT_VECTORS 16
#if R2_VERSION_NUMBER < 60000
#error Your radare2 is too old
#endif
// Tool definition structure
typedef struct {
char *name;
@@ -40,14 +51,14 @@ typedef struct {
} R2AI_ToolCall;
typedef struct {
const char *type;
const char *id;
const char *name;
const char *input;
const char *data;
const char *thinking;
const char *signature;
const char *text;
char *type;
char *id;
char *name;
char *input;
char *data;
char *thinking;
char *signature;
char *text;
} R2AI_ContentBlock;
typedef struct {
@@ -56,15 +67,28 @@ typedef struct {
} R2AI_ContentBlocks;
typedef struct {
const char *role;
const char *content;
const char *reasoning_content;
const R2AI_ContentBlocks *content_blocks;
const char *tool_call_id;
const R2AI_ToolCall *tool_calls;
char *role;
char *content;
char *reasoning_content;
R2AI_ContentBlocks *content_blocks;
char *tool_call_id;
R2AI_ToolCall *tool_calls;
int n_tool_calls;
} R2AI_Message;
typedef struct {
char *title;
char *author;
char *desc;
char *command;
char *prompt;
char *requires;
char *if_empty;
char *if_command;
char *model;
char *provider;
} R2AIPrompt;
typedef struct {
uint64_t prompt_tokens;
uint64_t completion_tokens;
@@ -78,9 +102,7 @@ typedef struct {
// Messages array management
typedef struct {
R2AI_Message *messages;
int n_messages;
int cap_messages;
RList *messages; // RList<R2AI_Message *>
} R2AI_Messages;
typedef struct {
@@ -98,6 +120,32 @@ typedef struct {
char **error;
} R2AIArgs;
// Stats structure from auto.c
typedef struct {
double total_cost;
double run_cost;
int total_tokens;
int run_tokens;
int total_prompt_tokens;
int run_prompt_tokens;
int total_completion_tokens;
int run_completion_tokens;
time_t start_time;
time_t total_start_time;
} R2AIStats;
// Main state structure to hold all global state
typedef struct r2ai_state_t {
R2AI_Messages *conversation; // Global conversation messages (from messages.c)
R2AIStats stats; // Global stats (from auto.c)
R2AI_Tools *tools; // Global tools instance (from tools.c)
RMarkdownTheme current_theme; // Global theme (from markdown.c)
bool theme_initialized; // Global theme flag (from markdown.c)
void *help_msg; // Global help message (from r2ai.c)
RVdb *db; // Vector database for embeddings
HtPP *model_compat_db; // Model compatibility database (from openai.c)
} R2AI_State;
/**
* Initialize a new empty messages array
*/
@@ -106,12 +154,12 @@ R_API R2AI_Messages *r2ai_msgs_new(void);
/**
* Initialize the conversation container (call during plugin init)
*/
R_API void r2ai_conversation_init(void);
R_API void r2ai_conversation_init(R2AI_State *state);
/**
* Get the conversation instance (returns NULL if not initialized)
*/
R_API R2AI_Messages *r2ai_conversation_get(void);
R_API R2AI_Messages *r2ai_conversation_get(R2AI_State *state);
/**
* Clear all messages in a container without freeing the container
@@ -162,13 +210,18 @@ R_API void r2ai_msgs_free(R2AI_Messages *msgs);
/**
* Free the conversation (call during plugin unload)
*/
R_API void r2ai_conversation_free(void);
R_API void r2ai_conversation_free(R2AI_State *state);
/**
* Free a R2AI_Message structure
*/
R_API void r2ai_message_free(R2AI_Message *msg);
/**
* Free a R2AIPrompt structure
*/
R_API void r2aiprompt_free(R2AIPrompt *prompt);
/**
* Delete the last N messages from the message array
* If n <= 0, defaults to deleting just the last message
@@ -236,7 +289,7 @@ R_API char *r2ai_http_post(RCore *core, const char *url, const char *headers[],
* @param rlen Pointer to store the response length
* @return Response body as string (must be freed by caller) or NULL on error
*/
R_API char *r2ai_http_get(const char *url, const char *headers[], int *code, int *rlen);
R_API char *r2ai_http_get(RCore *core, const char *url, const char *headers[], int *code, int *rlen);
/**
* Get the base URL for a given provider
@@ -248,29 +301,29 @@ R_API char *r2ai_http_get(const char *url, const char *headers[], int *code, int
R_IPI const char *r2ai_get_provider_url(RCore *core, const char *provider);
// anthropic
R_IPI R2AI_ChatResponse *r2ai_anthropic(RCore *core, R2AIArgs args);
R_IPI R2AI_ChatResponse *r2ai_anthropic(RCorePluginSession *cps, R2AIArgs args);
// openai
R_IPI R2AI_ChatResponse *r2ai_openai(RCore *core, R2AIArgs args);
R_IPI void r2ai_openai_fini(void);
R_IPI R2AI_ChatResponse *r2ai_openai(RCorePluginSession *cps, R2AIArgs args);
R_IPI void r2ai_openai_fini(R2AI_State *state);
// auto mode
R_IPI void cmd_r2ai_a(RCore *core, const char *user_query);
R_API char *r2ai(RCore *core, R2AIArgs args);
R_IPI void cmd_r2ai_a(RCorePluginSession *cps, const char *user_query);
// R_API char *r2ai(RCore *core, R2AI_State *state, R2AIArgs args);
R_API char *r2ai(RCorePluginSession *cps, R2AIArgs args);
R_API bool r2ai_init(RCorePluginSession *cps);
R_API bool r2ai_fini(RCorePluginSession *cps);
R_IPI R2AI_ChatResponse *r2ai_llmcall(RCore *core, R2AIArgs args);
R_IPI R2AI_ChatResponse *r2ai_llmcall(RCorePluginSession *cps, R2AIArgs args);
R_IPI void cmd_r2ai_logs(RCore *core);
R2AI_ChatResponse *r2ai_rawtools_llmcall(RCorePluginSession *cps, R2AIArgs args);
/**
* Create a conversation with system prompt and optional user message
*/
R_API R2AI_Messages *create_conversation(const char *user_message);
R_IPI void cmd_r2ai_logs(RCorePluginSession *cps);
/**
* Process messages through LLM and handle tool calls recursively
*/
R_API void process_messages(RCore *core, R2AI_Messages *messages, const char *system_prompt, int n_run);
R_API void process_messages(RCorePluginSession *cps, R2AI_Messages *messages, const char *system_prompt, int n_run);
/**
* Helper function to convert RJson to string
@@ -281,5 +334,6 @@ R_API char *r_json_to_string(const RJson *json);
* Helper function to convert RJson to PJ
*/
R_API PJ *r_json_to_pj(const RJson *json, PJ *existing_pj);
R_API void r2ai_cmd_q(RCorePluginSession *cps, const char *input);
#endif
+310 -194
View File
@@ -18,60 +18,80 @@
// Global flag for tracking interrupt status
static volatile sig_atomic_t r2ai_http_interrupted = 0;
// Helper function to get HTTP configuration with defaults
static void get_http_config(RCore *core, int *timeout, int *max_retries, int *max_backoff) {
*timeout = 120;
*max_retries = 10;
*max_backoff = 30;
if (core) {
int t = r_config_get_i (core->config, "r2ai.http.timeout");
if (t > 0) {
*timeout = t;
}
int mr = r_config_get_i (core->config, "r2ai.http.max_retries");
if (mr >= 0) {
*max_retries = mr;
}
int mb = r_config_get_i (core->config, "r2ai.http.max_backoff");
if (mb > 0) {
*max_backoff = mb;
}
}
}
#ifdef _WIN32
// Helper to append headers to PowerShell command
static void append_headers_to_cmd(RStrBuf *cmd, const char *headers[]) {
r_strbuf_appendf (cmd, "$ProgressPreference='SilentlyContinue';$headers=@{");
if (headers) {
for (int i = 0; headers[i]; i++) {
char *header = strdup (headers[i]);
char *colon = strchr (header, ':');
if (colon) {
*colon = '\0';
char *key = r_str_trim_dup (header);
char *value = r_str_trim_dup (colon + 1);
r_strbuf_appendf (cmd, "'%s'='%s';", key, value);
free (key);
free (value);
}
free (header);
}
}
r_strbuf_appendf (cmd, "};");
}
#endif
// Signal handler for SIGINT
static void r2ai_http_sigint_handler(int sig) {
(void)sig;
r2ai_http_interrupted = 1;
(void)sig;
r2ai_http_interrupted = 1;
}
/*
* Portable install/restore helpers for SIGINT handler.
* Some platforms may not have struct sigaction available; use signal()
* Some platforms may not have struct sigaction available; use signal ()
* as a fallback. We allocate storage for the old handler when
* sigaction is available so the restore function can restore it later.
*/
static void install_sigint_handler_local(void **out_old, int *out_old_is_sigaction) {
void (*old)(int) = signal (SIGINT, r2ai_http_sigint_handler);
*out_old = (void *)old;
*out_old_is_sigaction = 0;
void (*old) (int) = signal (SIGINT, r2ai_http_sigint_handler);
*out_old = (void *)old;
*out_old_is_sigaction = 0;
}
static void restore_sigint_handler_local(void *old, int old_is_sigaction) {
(void)old_is_sigaction;
signal (SIGINT, (void (*)(int))old);
(void)old_is_sigaction;
signal (SIGINT, (void (*) (int))old);
}
// Helper function to implement exponential backoff sleep
static void r2ai_sleep_with_backoff(int retry_count, int max_sleep_seconds) {
// Calculate sleep time with exponential backoff: 2^retry * base_time with jitter
int base_time_ms = 1000; // 500ms base time
int max_sleep_ms = max_sleep_seconds * 1000;
// Calculate exponential delay time with upper bound
int delay_ms = (1 << retry_count) * base_time_ms;
if (delay_ms > max_sleep_ms) {
delay_ms = max_sleep_ms;
// Simple retry sleep
static void sleep_retry(int retry_count, int max_sleep_seconds) {
int delay_seconds = retry_count + 1; // sleep 1, 2, 3, ... seconds
if (delay_seconds > max_sleep_seconds) {
delay_seconds = max_sleep_seconds;
}
// Add jitter (±20%)
int jitter = delay_ms / 5;
if (jitter > 0) {
srand (time (NULL) + retry_count);
delay_ms += (rand () % (jitter * 2)) - jitter;
}
// Ensure delay stays positive and within max limits
if (delay_ms <= 0) {
delay_ms = base_time_ms;
} else if (delay_ms > max_sleep_ms) {
delay_ms = max_sleep_ms;
}
// Sleep for the calculated time
struct timespec ts;
ts.tv_sec = delay_ms / 1000;
ts.tv_nsec = (delay_ms % 1000) * 1000000;
nanosleep (&ts, NULL);
r_sys_sleep (delay_seconds);
}
#if HAVE_LIBCURL
@@ -121,33 +141,13 @@ static char *curl_http_post(const char *url, const char *headers[], const char *
return NULL;
}
// Get timeout and retry configuration from config if available
int timeout = 120; // Default timeout in seconds
int max_retries = 10; // Default max retries
int max_backoff = 30; // Default max backoff in seconds
RCore *core = r_cons_singleton ()->user;
if (core) {
timeout = r_config_get_i (core->config, "r2ai.http.timeout");
if (timeout <= 0) {
timeout = 120; // Use default if invalid
}
max_retries = r_config_get_i (core->config, "r2ai.http.max_retries");
if (max_retries < 0) {
max_retries = 10; // Use default if invalid
}
max_backoff = r_config_get_i (core->config, "r2ai.http.max_backoff");
if (max_backoff <= 0) {
max_backoff = 30; // Use default if invalid
}
}
int timeout, max_retries, max_backoff;
get_http_config (NULL, &timeout, &max_retries, &max_backoff);
// Install signal handler for interruption (portable)
void *r2ai_old_sig = NULL;
int r2ai_old_is_sigaction = 0;
install_sigint_handler_local(&r2ai_old_sig, &r2ai_old_is_sigaction);
install_sigint_handler_local (&r2ai_old_sig, &r2ai_old_is_sigaction);
// Reset interrupt flag
r2ai_http_interrupted = 0;
@@ -168,10 +168,10 @@ static char *curl_http_post(const char *url, const char *headers[], const char *
if (!response.data) {
if (retry_count < max_retries) {
retry_count++;
r2ai_sleep_with_backoff (retry_count, max_backoff);
sleep_retry (retry_count, max_backoff);
continue;
}
restore_sigint_handler_local(r2ai_old_sig, r2ai_old_is_sigaction); // Restore signal handler
restore_sigint_handler_local (r2ai_old_sig, r2ai_old_is_sigaction); // Restore signal handler
return NULL;
}
response.data[0] = '\0';
@@ -182,10 +182,10 @@ static char *curl_http_post(const char *url, const char *headers[], const char *
free (response.data);
if (retry_count < max_retries) {
retry_count++;
r2ai_sleep_with_backoff (retry_count, max_backoff);
sleep_retry (retry_count, max_backoff);
continue;
}
restore_sigint_handler_local(r2ai_old_sig, r2ai_old_is_sigaction); // Restore signal handler
restore_sigint_handler_local (r2ai_old_sig, r2ai_old_is_sigaction); // Restore signal handler
return NULL;
}
@@ -245,7 +245,7 @@ static char *curl_http_post(const char *url, const char *headers[], const char *
if (retry_count < max_retries) {
retry_count++;
R_LOG_INFO ("Retrying request (%d/%d) after failure...", retry_count, max_retries);
r2ai_sleep_with_backoff (retry_count, max_backoff);
sleep_retry (retry_count, max_backoff);
continue;
}
break; // Exit the retry loop after max retries
@@ -261,7 +261,7 @@ static char *curl_http_post(const char *url, const char *headers[], const char *
if (retry_count < max_retries) {
retry_count++;
R_LOG_INFO ("Retrying request (%d/%d) after rate limiting...", retry_count, max_retries);
r2ai_sleep_with_backoff (retry_count, max_backoff);
sleep_retry (retry_count, max_backoff);
continue;
}
break; // Exit the retry loop after max retries
@@ -280,14 +280,86 @@ static char *curl_http_post(const char *url, const char *headers[], const char *
}
// Restore the original signal handler
restore_sigint_handler_local(r2ai_old_sig, r2ai_old_is_sigaction);
restore_sigint_handler_local (r2ai_old_sig, r2ai_old_is_sigaction);
return result;
}
#endif // HAVE_LIBCURL
#ifdef _WIN32
/**
* Windows-specific HTTP POST using PowerShell
*/
static char *windows_http_post(const char *url, const char *headers[], const char *data, int *code, int *rlen, int timeout) {
RStrBuf *cmd = r_strbuf_new ("powershell -Command \"");
append_headers_to_cmd (cmd, headers);
r_strbuf_appendf (cmd, "$body='%s';", data);
r_strbuf_appendf (cmd, "try{$r=Invoke-WebRequest -Method Post -Uri '%s' -Headers $headers -Body $body -TimeoutSec %d;", url, timeout);
r_strbuf_appendf (cmd, "Write-Host $r.StatusCode;$r.Content}catch{Write-Host 0;$_.Exception.Message}\"");
char *cmd_str = r_strbuf_drain (cmd);
char *full_response = r_sys_cmd_str (cmd_str, NULL, NULL);
free (cmd_str);
if (full_response) {
char *newline = strchr (full_response, '\n');
if (newline) {
*newline = '\0';
*code = atoi (full_response);
char *body = newline + 1;
if (rlen) {
*rlen = strlen (body);
}
char *result = strdup (body);
free (full_response);
return result;
}
*code = 200;
if (rlen) {
*rlen = strlen (full_response);
}
return full_response;
}
*code = 0;
return NULL;
}
/**
* Windows-specific HTTP GET using PowerShell
*/
static char *windows_http_get(const char *url, const char *headers[], int *code, int *rlen, int timeout) {
RStrBuf *cmd = r_strbuf_new ("powershell -Command \"");
append_headers_to_cmd (cmd, headers);
r_strbuf_appendf (cmd, "try{$r=Invoke-WebRequest -Method Get -Uri '%s' -Headers $headers -TimeoutSec %d;", url, timeout);
r_strbuf_appendf (cmd, "Write-Host $r.StatusCode;$r.Content}catch{Write-Host 0;$_.Exception.Message}\"");
char *cmd_str = r_strbuf_drain (cmd);
char *full_response = r_sys_cmd_str (cmd_str, NULL, NULL);
free (cmd_str);
if (full_response) {
char *newline = strchr (full_response, '\n');
if (newline) {
*newline = '\0';
*code = atoi (full_response);
char *body = newline + 1;
if (rlen) {
*rlen = strlen (body);
}
char *result = strdup (body);
free (full_response);
return result;
}
*code = 200;
if (rlen) {
*rlen = strlen (full_response);
}
return full_response;
}
*code = 0;
return NULL;
}
#endif
/**
* Execute curl as a system command, sending data via temporary file
* On Windows, use PowerShell as fallback when curl is not available
*
* @param url The URL to send the request to
* @param headers Array of headers, NULL terminated
@@ -301,25 +373,13 @@ static char *system_curl_post_file(RCore *core, const char *url, const char *hea
return NULL;
}
int timeout = r_config_get_i (core->config, "r2ai.http.timeout");
if (timeout <= 0) {
timeout = 120; // Use default if invalid
}
int max_retries = r_config_get_i (core->config, "r2ai.http.max_retries");
if (max_retries < 0) {
max_retries = 10; // Use default if invalid
}
int max_backoff = r_config_get_i (core->config, "r2ai.http.max_backoff");
if (max_backoff <= 0) {
max_backoff = 30; // Use default if invalid
}
int timeout, max_retries, max_backoff;
get_http_config (core, &timeout, &max_retries, &max_backoff);
// Install signal handler for interruption (portable)
void *r2ai_old_sig = NULL;
int r2ai_old_is_sigaction = 0;
install_sigint_handler_local(&r2ai_old_sig, &r2ai_old_is_sigaction);
install_sigint_handler_local (&r2ai_old_sig, &r2ai_old_is_sigaction);
// Reset interrupt flag
r2ai_http_interrupted = 0;
@@ -336,7 +396,7 @@ static char *system_curl_post_file(RCore *core, const char *url, const char *hea
R_LOG_ERROR ("Failed to create temporary file for curl data");
if (retry_count < max_retries) {
retry_count++;
r2ai_sleep_with_backoff (retry_count, max_backoff);
sleep_retry (retry_count, max_backoff);
continue;
}
break;
@@ -349,13 +409,88 @@ static char *system_curl_post_file(RCore *core, const char *url, const char *hea
free (temp_file);
if (retry_count < max_retries) {
retry_count++;
r2ai_sleep_with_backoff (retry_count, max_backoff);
sleep_retry (retry_count, max_backoff);
continue;
}
break;
}
}
#ifdef _WIN32
// On Windows, use PowerShell for HTTP requests
RStrBuf *cmd = r_strbuf_new ("powershell -Command \"");
r_strbuf_appendf (cmd, "$ProgressPreference = 'SilentlyContinue'; ");
r_strbuf_appendf (cmd, "$headers = @{");
// Add headers as PowerShell hashtable
for (int i = 0; headers[i] != NULL; i++) {
char *header = strdup (headers[i]);
char *colon = strchr (header, ':');
if (colon) {
*colon = '\0';
char *key = r_str_trim_dup (header);
char *value = r_str_trim_dup (colon + 1);
r_strbuf_appendf (cmd, "'%s'='%s';", key, value);
free (key);
free (value);
}
free (header);
}
r_strbuf_appendf (cmd, "}; ");
// Add body
if (use_files) {
r_strbuf_appendf (cmd, "$body = Get-Content '%s' -Raw; ", temp_file);
} else {
char *escaped_data = r_str_replace_all (strdup (data), "'", "''");
r_strbuf_appendf (cmd, "$body = '%s'; ", escaped_data);
free (escaped_data);
}
r_strbuf_appendf (cmd, "try { $response = Invoke-WebRequest -Method Post -Uri '%s' -Headers $headers -Body $body -TimeoutSec %d; ", url, timeout);
r_strbuf_appendf (cmd, "Write-Host $response.StatusCode; $response.Content } catch { Write-Host $_.Exception.Response.StatusCode.Value__; $_.Exception.Response.GetResponseStream().ReadToEnd() }\"");
// Execute the PowerShell command
char *cmd_str = r_strbuf_drain (cmd);
R_LOG_DEBUG ("Running PowerShell: %s", cmd_str);
char *full_response = r_sys_cmd_str (cmd_str, NULL, NULL);
free (cmd_str);
r_file_rm (temp_file);
free (temp_file);
// Parse PowerShell response: first line is status code, rest is body
if (full_response) {
char *newline = strchr (full_response, '\n');
if (newline) {
*newline = '\0';
*code = atoi (full_response);
char *body = newline + 1;
if (rlen) {
*rlen = strlen (body);
}
result = strdup (body);
} else {
*code = 200; // Assume success if no newline
if (rlen) {
*rlen = strlen (full_response);
}
result = full_response;
}
free (full_response);
success = true;
} else {
if (retry_count < max_retries) {
retry_count++;
R_LOG_INFO ("Retrying request (%d/%d) after failure...", retry_count, max_retries);
sleep_retry (retry_count, max_backoff);
continue;
}
*code = 0;
break;
}
#else
// Compose curl command
RStrBuf *cmd = r_strbuf_new ("curl -s");
@@ -406,7 +541,7 @@ static char *system_curl_post_file(RCore *core, const char *url, const char *hea
R_LOG_DEBUG ("HTTP request was interrupted by user");
free (response);
response = NULL;
restore_sigint_handler_local(r2ai_old_sig, r2ai_old_is_sigaction);
restore_sigint_handler_local (r2ai_old_sig, r2ai_old_is_sigaction);
return NULL;
}
@@ -423,16 +558,17 @@ static char *system_curl_post_file(RCore *core, const char *url, const char *hea
if (retry_count < max_retries) {
retry_count++;
R_LOG_INFO ("Retrying request (%d/%d) after failure...", retry_count, max_retries);
r2ai_sleep_with_backoff (retry_count, max_backoff);
sleep_retry (retry_count, max_backoff);
continue;
}
*code = 0;
break;
}
#endif
}
// Restore the original signal handler
restore_sigint_handler_local(r2ai_old_sig, r2ai_old_is_sigaction);
restore_sigint_handler_local (r2ai_old_sig, r2ai_old_is_sigaction);
return result;
}
@@ -452,32 +588,13 @@ static char *system_curl_get(const char *url, const char *headers[], int *code,
}
// Get timeout and retry configuration from config if available
int timeout = 120; // Default timeout in seconds
int max_retries = 10; // Default max retries
int max_backoff = 30; // Default max backoff in seconds
RCore *core = r_cons_singleton ()->user;
if (core) {
timeout = r_config_get_i (core->config, "r2ai.http.timeout");
if (timeout <= 0) {
timeout = 120; // Use default if invalid
}
max_retries = r_config_get_i (core->config, "r2ai.http.max_retries");
if (max_retries < 0) {
max_retries = 10; // Use default if invalid
}
max_backoff = r_config_get_i (core->config, "r2ai.http.max_backoff");
if (max_backoff <= 0) {
max_backoff = 30; // Use default if invalid
}
}
int timeout, max_retries, max_backoff;
get_http_config (NULL, &timeout, &max_retries, &max_backoff);
// Install signal handler for interruption (portable)
void *r2ai_old_sig = NULL;
int r2ai_old_is_sigaction = 0;
install_sigint_handler_local(&r2ai_old_sig, &r2ai_old_is_sigaction);
install_sigint_handler_local (&r2ai_old_sig, &r2ai_old_is_sigaction);
// Reset interrupt flag
r2ai_http_interrupted = 0;
@@ -488,6 +605,20 @@ static char *system_curl_get(const char *url, const char *headers[], int *code,
bool success = false;
while (!success && retry_count <= max_retries && !r2ai_http_interrupted) {
#ifdef _WIN32
result = windows_http_get (url, headers, code, rlen, timeout);
if (result) {
success = true;
} else if (retry_count < max_retries) {
retry_count++;
R_LOG_INFO ("Retrying request (%d/%d) after failure...", retry_count, max_retries);
sleep_retry (retry_count, max_backoff);
continue;
} else {
*code = 0;
break;
}
#else
// Compose curl command
RStrBuf *cmd = r_strbuf_new ("curl -s");
@@ -528,7 +659,7 @@ static char *system_curl_get(const char *url, const char *headers[], int *code,
if (r2ai_http_interrupted) {
R_LOG_DEBUG ("HTTP request was interrupted by user");
free (response);
restore_sigint_handler_local(r2ai_old_sig, r2ai_old_is_sigaction);
restore_sigint_handler_local (r2ai_old_sig, r2ai_old_is_sigaction);
return NULL;
}
@@ -545,16 +676,17 @@ static char *system_curl_get(const char *url, const char *headers[], int *code,
if (retry_count < max_retries) {
retry_count++;
R_LOG_INFO ("Retrying request (%d/%d) after failure...", retry_count, max_retries);
r2ai_sleep_with_backoff (retry_count, max_backoff);
sleep_retry (retry_count, max_backoff);
continue;
}
*code = 0;
break;
}
#endif
}
// Restore the original signal handler
restore_sigint_handler_local(r2ai_old_sig, r2ai_old_is_sigaction);
restore_sigint_handler_local (r2ai_old_sig, r2ai_old_is_sigaction);
return result;
}
@@ -562,32 +694,13 @@ static char *system_curl_get(const char *url, const char *headers[], int *code,
// Socket implementation with interrupt handling and retry logic
static char *socket_http_post_with_interrupt(const char *url, const char *headers[], const char *data, int *code, int *rlen) {
// Get timeout and retry configuration from config if available
int timeout = 120; // Default timeout in seconds
int max_retries = 10; // Default max retries
int max_backoff = 30; // Default max backoff in seconds
RCore *core = r_cons_singleton ()->user;
if (core) {
timeout = r_config_get_i (core->config, "r2ai.http.timeout");
if (timeout <= 0) {
timeout = 120; // Use default if invalid
}
max_retries = r_config_get_i (core->config, "r2ai.http.max_retries");
if (max_retries < 0) {
max_retries = 10; // Use default if invalid
}
max_backoff = r_config_get_i (core->config, "r2ai.http.max_backoff");
if (max_backoff <= 0) {
max_backoff = 30; // Use default if invalid
}
}
int timeout, max_retries, max_backoff;
get_http_config (NULL, &timeout, &max_retries, &max_backoff);
// Install signal handler for interruption (portable)
void *r2ai_old_sig = NULL;
int r2ai_old_is_sigaction = 0;
install_sigint_handler_local(&r2ai_old_sig, &r2ai_old_is_sigaction);
install_sigint_handler_local (&r2ai_old_sig, &r2ai_old_is_sigaction);
// Reset interrupt flag
r2ai_http_interrupted = 0;
@@ -634,7 +747,7 @@ static char *socket_http_post_with_interrupt(const char *url, const char *header
if (retry_count < max_retries) {
retry_count++;
R_LOG_INFO ("Retrying request (%d/%d) after error...", retry_count, max_retries);
r2ai_sleep_with_backoff (retry_count, max_backoff);
sleep_retry (retry_count, max_backoff);
continue;
}
break; // Exit the retry loop after max retries
@@ -646,7 +759,7 @@ static char *socket_http_post_with_interrupt(const char *url, const char *header
if (retry_count < max_retries) {
retry_count++;
R_LOG_INFO ("Retrying request (%d/%d) after failure...", retry_count, max_retries);
r2ai_sleep_with_backoff (retry_count, max_backoff);
sleep_retry (retry_count, max_backoff);
continue;
}
break; // Exit the retry loop after max retries
@@ -657,7 +770,7 @@ static char *socket_http_post_with_interrupt(const char *url, const char *header
}
// Restore the original signal handler
restore_sigint_handler_local(r2ai_old_sig, r2ai_old_is_sigaction);
restore_sigint_handler_local (r2ai_old_sig, r2ai_old_is_sigaction);
return result;
}
@@ -669,32 +782,13 @@ static char *curl_http_get(const char *url, const char *headers[], int *code, in
}
// Get timeout and retry configuration from config if available
int timeout = 120; // Default timeout in seconds
int max_retries = 10; // Default max retries
int max_backoff = 30; // Default max backoff in seconds
RCore *core = r_cons_singleton ()->user;
if (core) {
timeout = r_config_get_i (core->config, "r2ai.http.timeout");
if (timeout <= 0) {
timeout = 120; // Use default if invalid
}
max_retries = r_config_get_i (core->config, "r2ai.http.max_retries");
if (max_retries < 0) {
max_retries = 10; // Use default if invalid
}
max_backoff = r_config_get_i (core->config, "r2ai.http.max_backoff");
if (max_backoff <= 0) {
max_backoff = 30; // Use default if invalid
}
}
int timeout, max_retries, max_backoff;
get_http_config (NULL, &timeout, &max_retries, &max_backoff);
// Install signal handler for interruption (portable)
void *r2ai_old_sig = NULL;
int r2ai_old_is_sigaction = 0;
install_sigint_handler_local(&r2ai_old_sig, &r2ai_old_is_sigaction);
install_sigint_handler_local (&r2ai_old_sig, &r2ai_old_is_sigaction);
// Reset interrupt flag
r2ai_http_interrupted = 0;
@@ -715,10 +809,10 @@ static char *curl_http_get(const char *url, const char *headers[], int *code, in
if (!response.data) {
if (retry_count < max_retries) {
retry_count++;
r2ai_sleep_with_backoff (retry_count, max_backoff);
sleep_retry (retry_count, max_backoff);
continue;
}
restore_sigint_handler_local(r2ai_old_sig, r2ai_old_is_sigaction); // Restore signal handler
restore_sigint_handler_local (r2ai_old_sig, r2ai_old_is_sigaction); // Restore signal handler
return NULL;
}
response.data[0] = '\0';
@@ -729,10 +823,10 @@ static char *curl_http_get(const char *url, const char *headers[], int *code, in
free (response.data);
if (retry_count < max_retries) {
retry_count++;
r2ai_sleep_with_backoff (retry_count, max_backoff);
sleep_retry (retry_count, max_backoff);
continue;
}
restore_sigint_handler_local(r2ai_old_sig, r2ai_old_is_sigaction); // Restore signal handler
restore_sigint_handler_local (r2ai_old_sig, r2ai_old_is_sigaction); // Restore signal handler
return NULL;
}
@@ -794,7 +888,7 @@ static char *curl_http_get(const char *url, const char *headers[], int *code, in
if (retry_count < max_retries) {
retry_count++;
R_LOG_INFO ("Retrying request (%d/%d) after failure...", retry_count, max_retries);
r2ai_sleep_with_backoff (retry_count, max_backoff);
sleep_retry (retry_count, max_backoff);
continue;
}
break; // Exit the retry loop after max retries
@@ -810,7 +904,7 @@ static char *curl_http_get(const char *url, const char *headers[], int *code, in
if (retry_count < max_retries) {
retry_count++;
R_LOG_INFO ("Retrying request (%d/%d) after rate limiting...", retry_count, max_retries);
r2ai_sleep_with_backoff (retry_count, max_backoff);
sleep_retry (retry_count, max_backoff);
continue;
}
break; // Exit the retry loop after max retries
@@ -829,7 +923,7 @@ static char *curl_http_get(const char *url, const char *headers[], int *code, in
}
// Restore the original signal handler
restore_sigint_handler_local(r2ai_old_sig, r2ai_old_is_sigaction);
restore_sigint_handler_local (r2ai_old_sig, r2ai_old_is_sigaction);
return result;
}
@@ -838,32 +932,13 @@ static char *curl_http_get(const char *url, const char *headers[], int *code, in
// Socket implementation for GET requests
static char *socket_http_get_with_interrupt(const char *url, const char *headers[], int *code, int *rlen) {
// Get timeout and retry configuration from config if available
int timeout = 120; // Default timeout in seconds
int max_retries = 10; // Default max retries
int max_backoff = 30; // Default max backoff in seconds
RCore *core = r_cons_singleton ()->user;
if (core) {
timeout = r_config_get_i (core->config, "r2ai.http.timeout");
if (timeout <= 0) {
timeout = 120; // Use default if invalid
}
max_retries = r_config_get_i (core->config, "r2ai.http.max_retries");
if (max_retries < 0) {
max_retries = 10; // Use default if invalid
}
max_backoff = r_config_get_i (core->config, "r2ai.http.max_backoff");
if (max_backoff <= 0) {
max_backoff = 30; // Use default if invalid
}
}
int timeout, max_retries, max_backoff;
get_http_config (NULL, &timeout, &max_retries, &max_backoff);
// Install signal handler for interruption (portable)
void *r2ai_old_sig = NULL;
int r2ai_old_is_sigaction = 0;
install_sigint_handler_local(&r2ai_old_sig, &r2ai_old_is_sigaction);
install_sigint_handler_local (&r2ai_old_sig, &r2ai_old_is_sigaction);
// Reset interrupt flag
r2ai_http_interrupted = 0;
@@ -913,7 +988,7 @@ static char *socket_http_get_with_interrupt(const char *url, const char *headers
if (retry_count < max_retries) {
retry_count++;
R_LOG_INFO ("Retrying request (%d/%d) after error...", retry_count, max_retries);
r2ai_sleep_with_backoff (retry_count, max_backoff);
sleep_retry (retry_count, max_backoff);
continue;
}
break; // Exit the retry loop after max retries
@@ -925,7 +1000,7 @@ static char *socket_http_get_with_interrupt(const char *url, const char *headers
if (retry_count < max_retries) {
retry_count++;
R_LOG_INFO ("Retrying request (%d/%d) after failure...", retry_count, max_retries);
r2ai_sleep_with_backoff (retry_count, max_backoff);
sleep_retry (retry_count, max_backoff);
continue;
}
break; // Exit the retry loop after max retries
@@ -936,7 +1011,7 @@ static char *socket_http_get_with_interrupt(const char *url, const char *headers
}
// Restore the original signal handler
restore_sigint_handler_local(r2ai_old_sig, r2ai_old_is_sigaction);
restore_sigint_handler_local (r2ai_old_sig, r2ai_old_is_sigaction);
return result;
}
@@ -974,17 +1049,37 @@ R_API char *r2ai_http_post(RCore *core, const char *url, const char *headers[],
#if USE_LIBCURL && HAVE_LIBCURL
return curl_http_post (url, headers, data, code, rlen);
#else
#ifdef _WIN32
int timeout, max_retries, max_backoff;
get_http_config (core, &timeout, &max_retries, &max_backoff);
char *result = NULL;
int retry_count = 0;
bool success = false;
while (!success && retry_count <= max_retries) {
result = windows_http_post (url, headers, data, code, rlen, timeout);
if (result) {
success = true;
} else if (retry_count < max_retries) {
retry_count++;
R_LOG_INFO ("Retrying request (%d/%d) after failure...", retry_count, max_retries);
sleep_retry (retry_count, max_backoff);
} else {
*code = 0;
}
}
return result;
#else
#if USE_R2_CURL
r_sys_setenv ("R2_CURL", "1");
#endif
return socket_http_post_with_interrupt (url, headers, data, code, rlen);
#endif
#endif
}
}
}
R_API char *r2ai_http_get(const char *url, const char *headers[], int *code, int *rlen) {
RCore *core = r_cons_singleton ()->user;
R_API char *r2ai_http_get(RCore *core, const char *url, const char *headers[], int *code, int *rlen) {
const char *backend = "auto";
if (core) {
@@ -1009,10 +1104,31 @@ R_API char *r2ai_http_get(const char *url, const char *headers[], int *code, int
#if USE_LIBCURL && HAVE_LIBCURL
return curl_http_get (url, headers, code, rlen);
#else
#ifdef _WIN32
int timeout, max_retries, max_backoff;
get_http_config (core, &timeout, &max_retries, &max_backoff);
char *result = NULL;
int retry_count = 0;
bool success = false;
while (!success && retry_count <= max_retries) {
result = windows_http_get (url, headers, code, rlen, timeout);
if (result) {
success = true;
} else if (retry_count < max_retries) {
retry_count++;
R_LOG_INFO ("Retrying request (%d/%d) after failure...", retry_count, max_retries);
sleep_retry (retry_count, max_backoff);
} else {
*code = 0;
}
}
return result;
#else
#if USE_R2_CURL
r_sys_setenv ("R2_CURL", "1");
#endif
return socket_http_get_with_interrupt (url, headers, code, rlen);
#endif
#endif
}
}
+22
View File
@@ -0,0 +1,22 @@
#ifndef INCLUDE_R2AI_PRIV__H
#define INCLUDE_R2AI_PRIV__H
#include "r2ai.h"
typedef struct {
const char *name;
const char *url;
bool requires_api_key;
bool uses_anthropic_header;
bool uses_tags_endpoint;
bool uses_system_ls;
} R2AIProvider;
R_IPI char *r2ai_get_api_key(RCore *core, const char *provider);
R_IPI const R2AIProvider *r2ai_get_provider(const char *name);
R_IPI const char *r2ai_get_provider_url(RCore *core, const char *provider);
R_IPI RList *r2ai_fetch_available_models(RCore *core, const char *provider);
R_IPI void r2ai_list_providers(RCore *core, RStrBuf *sb);
R_IPI void r2ai_refresh_embeddings(RCorePluginSession *cps);
#endif
+297
View File
@@ -0,0 +1,297 @@
/* r2ai - Copyright 2023-2025 pancake */
#define R_LOG_ORIGIN "rawtools"
#include "r2ai.h"
#include "r2ai_priv.h"
// Function to build the rawtools prompt with available tools
static const char *RAWTOOLS_PROMPT =
"Common radare2 commands you can use with r2cmd:\n"
"- i: Show binary information\n"
"- iI: Show detailed binary info\n"
"- iz: Show strings\n"
"- afl: List functions\n"
"- pdf @ function: Disassemble function\n"
"- px @ address: Show hexdump\n"
"- aaa: Analyze all\n"
"- s address: Seek to address\n"
"- ? command: Get help\n\n"
"To run radare2 commands, use the r2cmd tool with JSON arguments.\n\n"
"TOOL: r2cmd\n"
"USAGE: TOOL: r2cmd {\"command\": \"your_command_here\"}\n\n"
"Example: TOOL: r2cmd {\"command\": \"i\"}\n\n"
"If you don't need to run commands, just answer directly.\n\n";
// Function to parse raw tool call from response text
static bool parse_raw_tool_call(const char *response, char **tool_name, char **tool_args) {
if (!response || !tool_name || !tool_args) {
return false;
}
*tool_name = NULL;
*tool_args = NULL;
// First try to parse "TOOL: " format
const char *tool_prefix = "TOOL: ";
const char *line_start = response;
while ((line_start = strstr (line_start, tool_prefix))) {
// Check if it's at the beginning of a line
if (line_start == response || *(line_start - 1) == '\n') {
// Found a tool call
const char *tool_start = line_start + strlen (tool_prefix);
const char *space_pos = strchr (tool_start, ' ');
if (space_pos) {
// Extract tool name
size_t name_len = space_pos - tool_start;
*tool_name = r_str_ndup (tool_start, name_len);
// Extract arguments (rest of the line)
*tool_args = strdup (space_pos + 1);
// Trim whitespace from arguments (simple implementation)
if (*tool_args) {
char *start = *tool_args;
while (*start && (*start == ' ' || *start == '\t')) {
start++;
}
char *end = start + strlen (start) - 1;
while (end > start && (*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r')) {
end--;
}
*(end + 1) = '\0';
if (start != *tool_args) {
memmove (*tool_args, start, strlen (start) + 1);
}
}
return true;
}
}
line_start += strlen (tool_prefix);
}
// If no TOOL: format found, try to parse JSON response
// Look for JSON that looks like tool arguments
const char *json_start = strstr (response, "{\"command\"");
if (json_start) {
*tool_name = strdup ("r2cmd");
*tool_args = strdup (json_start);
// Trim trailing whitespace and newlines
char *end = *tool_args + strlen (*tool_args) - 1;
while (end > *tool_args && (*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r')) {
*end = '\0';
end--;
}
return true;
}
return false;
}
// Function to handle rawtools mode in LLM call
R2AI_ChatResponse *r2ai_rawtools_llmcall(RCorePluginSession *cps, R2AIArgs args) {
if (!cps) {
return NULL;
}
RCore *core = cps->core;
// Modify the system prompt to include rawtools instructions only if no tool messages
const char *original_system_prompt = args.system_prompt;
// For rawtools, use a shorter system prompt to avoid token limits
const char *short_system_prompt = "You are a reverse engineer using radare2. The binary is loaded. Use r2cmd tool for analysis.";
// Check for initial command output in messages
char *init_output = NULL;
if (args.messages && args.messages->messages) {
RList *msgs = args.messages->messages;
int n_msgs = r_list_length (msgs);
for (int i = 0; i < n_msgs; i++) {
R2AI_Message *msg = r_list_get_n (msgs, i);
if (msg && msg->role && !strcmp (msg->role, "system") && msg->content &&
strstr (msg->content, "Here is some information about the binary")) {
init_output = strdup (msg->content);
break;
}
}
}
char *enhanced_system_prompt = NULL;
if (init_output) {
enhanced_system_prompt = r_str_newf ("%s\n\n%s\n\n%s", short_system_prompt, init_output, RAWTOOLS_PROMPT);
} else {
enhanced_system_prompt = r_str_newf ("%s\n\n%s", short_system_prompt, RAWTOOLS_PROMPT);
}
free (init_output);
// Temporarily modify args to use enhanced prompt and no tools (since we're using prompt engineering)
R2AIArgs rawtools_args = args;
rawtools_args.system_prompt = enhanced_system_prompt;
rawtools_args.tools = NULL; // Disable native tool calling
// Make the LLM call directly to provider
const char *provider = rawtools_args.provider? rawtools_args.provider: r_config_get (core->config, "r2ai.api");
if (!provider) {
R_LOG_ERROR ("No provider defined");
return NULL;
}
R2AI_ChatResponse *response = NULL;
const R2AIProvider *p = r2ai_get_provider (provider);
if (p && p->uses_anthropic_header) {
response = r2ai_anthropic (cps, rawtools_args);
} else {
response = r2ai_openai (cps, rawtools_args);
}
free (enhanced_system_prompt);
if (!response || !response->message || !response->message->content) {
return response;
}
R_LOG_DEBUG ("[RAWTOOLS] Raw model response: %s", response->message->content);
// Check if the response contains a raw tool call
char *tool_name = NULL;
char *tool_args = NULL;
if (parse_raw_tool_call (response->message->content, &tool_name, &tool_args)) {
R_LOG_DEBUG ("Raw tool call detected: %s with args: %s", tool_name, tool_args);
// Find the tool in our tools list
const R2AI_Tools *tools = r2ai_get_tools ();
bool tool_found = false;
for (int i = 0; i < tools->n_tools; i++) {
if (tools->tools[i].name && !strcmp (tools->tools[i].name, tool_name)) {
tool_found = true;
break;
}
}
if (tool_found) {
// Modify the existing response to include tool call information
R2AI_Message *modified_message = R_NEW0 (R2AI_Message);
if (modified_message) {
modified_message->role = strdup ("assistant");
// Remove the TOOL line from content, or set to empty if that's all there is
char *content = strdup (response->message->content? response->message->content: "");
char *tool_line = strstr (content, "TOOL: ");
if (tool_line) {
// Find the end of the line
char *line_end = strchr (tool_line, '\n');
if (line_end) {
// Remove the TOOL line
memmove (tool_line, line_end + 1, strlen (line_end + 1) + 1);
} else {
// The TOOL line is the entire content
*tool_line = '\0';
}
}
modified_message->content = content;
// Set up proper tool call structure
modified_message->tool_calls = R_NEWS0 (R2AI_ToolCall, 1);
if (modified_message->tool_calls) {
modified_message->tool_calls[0].name = tool_name;
modified_message->tool_calls[0].arguments = tool_args;
char id_buf[32];
snprintf (id_buf, sizeof (id_buf), "rawtool_%d", (int)time (NULL));
modified_message->tool_calls[0].id = strdup (id_buf);
modified_message->n_tool_calls = 1;
}
tool_name = NULL;
tool_args = NULL;
// Replace the response message
if (response->message) {
r2ai_message_free ((R2AI_Message *)response->message);
}
*(R2AI_Message **)&response->message = modified_message;
}
} else {
// Unknown tool, modify content to indicate error
R2AI_Message *modified_message = R_NEW0 (R2AI_Message);
if (modified_message) {
modified_message->role = strdup ("assistant");
size_t result_len = strlen ("Unknown tool: ") + strlen (tool_name) + 1;
modified_message->content = malloc (result_len);
if (modified_message->content) {
snprintf (modified_message->content, result_len, "Unknown tool: %s", tool_name);
}
// Replace the response message
if (response->message) {
r2ai_message_free ((R2AI_Message *)response->message);
}
*(R2AI_Message **)&response->message = modified_message;
}
free (tool_name);
free (tool_args);
tool_name = NULL;
tool_args = NULL;
}
return response;
}
// No tool call found
free (tool_name);
free (tool_args);
// If the response has no content, try again with normal mode (without rawtools prompt)
if (!response->message || !response->message->content || !*response->message->content) {
R_LOG_DEBUG ("No tool call found and no content, falling back to normal mode");
// Free the current response
if (response->message) {
r2ai_message_free ((R2AI_Message *)response->message);
}
free (response);
// Modify args to use original system prompt without rawtools enhancement
R2AIArgs fallback_args = args;
fallback_args.system_prompt = original_system_prompt;
fallback_args.tools = args.tools; // Keep tools for fallback
// Call provider directly
const char *provider = fallback_args.provider? fallback_args.provider: r_config_get (core->config, "r2ai.api");
if (!provider) {
R_LOG_ERROR ("No provider defined");
return NULL;
}
R2AI_ChatResponse *fallback_response = NULL;
const R2AIProvider *p = r2ai_get_provider (provider);
if (p && p->uses_anthropic_header) {
fallback_response = r2ai_anthropic (cps, fallback_args);
} else {
fallback_response = r2ai_openai (cps, fallback_args);
}
if (fallback_response && fallback_response->message && fallback_response->message->content && *fallback_response->message->content) {
return fallback_response;
}
// If still no content, create a response with warning
if (fallback_response) {
if (fallback_response->message) {
r2ai_message_free ((R2AI_Message *)fallback_response->message);
}
free (fallback_response);
}
// Create a warning response
R2AI_ChatResponse *warning_response = R_NEW0 (R2AI_ChatResponse);
R2AI_Message *msg = R_NEW0 (R2AI_Message);
msg->role = strdup ("assistant");
msg->content = strdup ("Warning: LLM provided no response content");
*((R2AI_Message **)&warning_response->message) = msg;
R_LOG_WARN ("LLM provided no response content");
return warning_response;
}
return response;
}
+12 -46
View File
@@ -19,7 +19,7 @@ static R2AI_Tool r2cmd_tool = {
static R2AI_Tool qjs_tool = {
.name = "execute_js",
.description = "Execute a JavaScript script in a quickjs environment. Only what you console.log will be returned.",
.description = "Execute a JavaScript script inside the radare2 environment. Use `var RESULT=r2.cmd(COMMAND)` to execute radare2 commands and `r2.log(MESSAGE)` instead of console.log`",
.parameters = "{\
\"type\": \"object\",\
\"properties\": {\
@@ -284,31 +284,19 @@ R_API char *r2ai_r2cmd(RCore *core, RJson *args, bool hide_tool_output, char **e
if (is_multiline) {
// Use editor for multi-line commands
*edited_command = strdup (command);
#if R2_VERSION_NUMBER >= 50909
r_cons_editor (core->cons, NULL, *edited_command);
#else
r_cons_editor (NULL, *edited_command);
#endif
command = *edited_command;
} else {
// For single-line commands, push the command to input buffer
R2_NEWLINE ();
R2_PRINTF ("\x1b[31m[edit cmd]>\x1b[0m ");
R2_FLUSH ();
r_cons_newline (core->cons);
// Push the command to the input buffer
// Get user input with command pre-filled
#if R2_VERSION_NUMBER >= 50909
r_cons_readpush (core->cons, command, strlen (command));
r_cons_readpush (core->cons, "\x05", 1); // Ctrl+E - move to end
const char *readline_result = r_line_readline (core->cons);
#else
r_cons_readpush (command, strlen (command));
r_cons_readpush ("\x05", 1); // Ctrl+E - move to end
const char *readline_result = r_line_readline ();
#endif
// Check if interrupted or ESC pressed (readline_result is NULL or empty)
if (R2_INTERRUPTED () || R_STR_ISEMPTY (readline_result)) {
if (r_cons_is_breaked (core->cons) || R_STR_ISEMPTY (readline_result)) {
R_LOG_INFO ("Command execution cancelled %s", readline_result);
return strdup ("R2AI_SIGINT");
}
@@ -330,8 +318,8 @@ R_API char *r2ai_r2cmd(RCore *core, RJson *args, bool hide_tool_output, char **e
if (!hide_tool_output) {
char *red_command = r_str_newf ("\x1b[31m%s\x1b[0m\n", *edited_command);
R2_PRINTF ("%s", red_command);
R2_FLUSH ();
r_cons_printf (core->cons, "%s", red_command);
r_cons_flush (core->cons);
free (red_command);
}
@@ -377,29 +365,17 @@ R_API char *r2ai_qjs(RCore *core, RJson *args, bool hide_tool_output) {
if (is_multiline) {
// Use editor for multi-line scripts
edited_script = strdup (script);
#if R2_VERSION_NUMBER >= 50909
r_cons_editor (core->cons, NULL, edited_script);
#else
r_cons_editor (NULL, edited_script);
#endif
script = edited_script;
} else {
// For single-line scripts, push the script to input buffer
R2_PRINTF ("\x1b[31m[edit js]>\x1b[0m ");
R2_FLUSH ();
#if R2_VERSION_NUMBER >= 50909
r_cons_readpush (core->cons, script, strlen (script));
r_cons_readpush (core->cons, "\x05", 1); // Ctrl+E - move to end
const char *readline_result = r_line_readline (core->cons);
#else
r_cons_readpush (script, strlen (script));
r_cons_readpush ("\x05", 1); // Ctrl+E - move to end
const char *readline_result = r_line_readline ();
#endif
// Check if interrupted or ESC pressed (readline_result is NULL or empty)
if (R2_INTERRUPTED () || R_STR_ISEMPTY (readline_result)) {
if (r_cons_is_breaked (core->cons) || R_STR_ISEMPTY (readline_result)) {
free (edited_script); // Free if already allocated
return strdup ("R2AI_SIGINT");
}
@@ -419,8 +395,8 @@ R_API char *r2ai_qjs(RCore *core, RJson *args, bool hide_tool_output) {
if (!hide_tool_output) {
char *print_script = r_str_newf ("\n```js\n%s\n```", script);
char *print_script_rendered = r2ai_markdown (print_script);
R2_PRINTF ("%s\n", print_script_rendered);
R2_FLUSH ();
r_cons_printf (core->cons, "%s\n", print_script_rendered);
r_cons_flush (core->cons);
free (print_script);
free (print_script_rendered);
}
@@ -435,11 +411,7 @@ R_API char *r2ai_qjs(RCore *core, RJson *args, bool hide_tool_output) {
return strdup ("{ \"res\":\"Failed to create script payload\" }");
}
#if R2_VERSION_NUMBER >= 50909
char *payload_base64 = r_base64_encode_dyn ((const ut8 *)payload, strlen (payload));
#else
char *payload_base64 = r_base64_encode_dyn ((const char *)payload, strlen (payload));
#endif
if (!payload_base64) {
free (payload);
return strdup ("{ \"res\":\"Failed to encode script\" }");
@@ -481,12 +453,6 @@ R_API char *execute_tool(RCore *core, const char *tool_name, const char *args, c
}
bool hide_tool_output = r_config_get_b (core->config, "r2ai.auto.hide_tool_output");
char *print_name = r_str_newf ("\x1b[1;32m\x1b[4m[%s]>\x1b[0m ", tool_name);
R2_PRINTF ("%s\n", print_name);
R2_FLUSH ();
free (print_name);
char *tool_result = NULL;
if (strcmp (tool_name, "r2cmd") == 0) {
@@ -627,10 +593,10 @@ R_API char *execute_tool(RCore *core, const char *tool_name, const char *args, c
return r_str_newf ("Error running tool: Unknown error\nCommand: %s", args);
}
if (!hide_tool_output) {
R2_NEWLINE ();
R2_PRINTF ("%s\n", result);
R2_NEWLINE ();
R2_FLUSH ();
r_cons_newline (core->cons);
r_cons_printf (core->cons, "%s\n", result);
r_cons_newline (core->cons);
r_cons_flush (core->cons);
}
r_str_ansi_strip (result);
+6 -6
View File
@@ -19,7 +19,7 @@ static void vector_free(Vector *v) {
/*-------------------------------
* Distance Function Using Squared Euclidean Distance
* For two unit vectors: dist^2 = 2 - 2 * (dot product)
* For two unit vectors: dist^2 = 2 - 2 *(dot product)
*-------------------------------*/
static float squared_distance(const Vector *a, const Vector *b) {
if (a->dim != b->dim) {
@@ -34,14 +34,14 @@ static float squared_distance(const Vector *a, const Vector *b) {
}
/*-------------------------------
KDNode Utility Functions
-------------------------------*/
KDNode Utility Functions
-------------------------------*/
static KDNode *create_kdnode(const Vector *v, const char *text, int split_dim) {
KDNode *node = (KDNode *)malloc (sizeof (KDNode));
node->point.dim = v->dim;
node->point.data = (float *)malloc (sizeof (float) * v->dim);
memcpy (node->point.data, v->data, sizeof (float) * v->dim);
node->text = text ? strdup (text) : NULL;
node->text = text? strdup (text): NULL;
node->split_dim = split_dim;
node->left = NULL;
node->right = NULL;
@@ -212,8 +212,8 @@ void kd_search_knn_recursive(KDNode *node, const Vector *query, RVdbResultSet *r
int axis = node->split_dim;
float diff = query->data[axis] - node->point.data[axis];
KDNode *first = (diff < 0) ? node->left : node->right;
KDNode *second = (diff < 0) ? node->right : node->left;
KDNode *first = (diff < 0)? node->left: node->right;
KDNode *second = (diff < 0)? node->right: node->left;
kd_search_knn_recursive (first, query, rs, depth + 1, dim);
+1 -1
View File
@@ -136,7 +136,7 @@ static void compute_embedding(RVdb *db, const char *text, float *embedding, unsi
// Compute term frequency: tf = 1 + log (token_count)
float tf = 1.0f + log ((float)dt->count);
RVdbToken *t = gtfidf_find (db->tokens, dt->token);
float df_value = t ? t->df : 1.0f;
float df_value = t? t->df: 1.0f;
// Compute inverse document frequency;
float idf = log (((float)db->total_docs + 1.0f) / ((float)df_value + 1.0f)) + 1.0f;
float weight = tf * idf;