mirror of
https://github.com/radareorg/r2ai.git
synced 2026-11-09 17:46:36 -10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
83b96f7a3e | ||
|
|
e2ee8cdbb6 | ||
|
|
8630e95d83 | ||
|
|
e07293e40f | ||
|
|
415c546f92 |
No files matched your search
@@ -93,11 +93,8 @@ jobs:
|
||||
shell: bash
|
||||
run: |
|
||||
mkdir -p release
|
||||
# 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 -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
|
||||
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,14 +0,0 @@
|
||||
SHELL=/bin/bash
|
||||
|
||||
MODELS=gpt-oss:20b gemma3:4b gemma3:12b
|
||||
MODELS+=granite3-dense:8b mannix/jan-nano:latest yi-coder:latest qwen3:latest deepseek-coder-v2:latest hhao/qwen2.5-coder-tools:latest qwen2.5-coder:latest
|
||||
MODELS=granite3-dense:8b mannix/jan-nano:latest yi-coder:latest qwen3:latest deepseek-coder-v2:latest hhao/qwen2.5-coder-tools:latest qwen2.5-coder:latest
|
||||
LANGS=Dart Perl Ruby Javascript PHP C
|
||||
|
||||
all:
|
||||
for a in ${MODELS} ; do \
|
||||
declare -a ARGS; \
|
||||
for l in ${LANGS} ; do \
|
||||
ARGS+=("echo \"decai -e model=$$a;decai -e lang=$$l;s 0x100000924;af;decai -d\" | r2 -q /bin/ls"); \
|
||||
done ; \
|
||||
./run.sh "$${ARGS[@]}" > ${a//\//_}.html ; done
|
||||
@@ -0,0 +1 @@
|
||||
recursive-include r2ai/ui *.tcss
|
||||
@@ -1,6 +1,4 @@
|
||||
TARGETS=all clean mrproper user-install install user-uninstall uninstall fmt indent
|
||||
|
||||
TARGETS=all clean mrproper user-install install user-uninstall uninstall
|
||||
$(TARGETS):
|
||||
$(MAKE) -C src $@
|
||||
|
||||
.PHONY: $(TARGETS)
|
||||
@@ -19,17 +19,14 @@
|
||||
|
||||
This repository contains two plugins for radare2:
|
||||
|
||||
* **r2ai** - native [AI plugin](./src/README.md) for radare2
|
||||
* **decai** - r2js plugin with special [focus on decompilation](./decai/README.md)
|
||||
* **r2ai** - native plugin for radare2
|
||||
* **decai** - r2js plugin with special focus on decompilation
|
||||
|
||||
If you are looking to use radare2 with other agents via MCP:
|
||||
|
||||
* **r2mcp** - the [official radare2 mcp](https://github.com/radare2/radare2-mcp)
|
||||
* **r2copilot** - the mcp with focus on CTF [r2copilot](https://github.com/darallium/r2-copilot)
|
||||
|
||||
If you are looking for a radare2 focused autonomous agent:
|
||||
* **r2agent** - automate radare2 workflows through autonomous agents. [r2agent](https://github.com/nitanmarcel/r2agent)
|
||||
|
||||
## Features
|
||||
|
||||
* Configure different roles and customize prompts
|
||||
@@ -40,21 +37,12 @@ If you are looking for a radare2 focused autonomous agent:
|
||||
* RAG markdown, code or textfiles using its native vector database
|
||||
* Embed the output of an r2 command and resolve questions on the given data
|
||||
|
||||
## User defined Prompts
|
||||
## Practical Examples
|
||||
|
||||
```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]>
|
||||
```
|
||||
* Enhanced decompilation with `r2ai -d`
|
||||
* Autoname functions with `r2ai -n`
|
||||
* Explain function with `r2ai -x`
|
||||
* Find vulnerabilities with `r2ai -V`
|
||||
|
||||
## Installation
|
||||
|
||||
@@ -70,46 +58,39 @@ $ r2pm -Uci decai
|
||||
- Adds the **r2ai** command to the radare2 shell: `r2 -qc r2ai`
|
||||
- You can also run the wrapper in $PATH: `r2pm -r r2ai`
|
||||
|
||||
Drop your API keys in environment variables or use the configuration file:
|
||||
Drop your API keys in environment variables or files in your home:
|
||||
|
||||
```console
|
||||
$ export ANTHROPIC_API_KEY=sk-ant-api03-CENSORED
|
||||
$ cat ~/.r2ai.anthropic-key
|
||||
sk-ant-api03-CENSORED
|
||||
$ export OPENAI_API_KEY=sk-proj-6rlSPS-zN1v...
|
||||
```
|
||||
|
||||
Or edit the api keys file `~/.config/r2ai/apikeys.txt` run:
|
||||
| AI | API key |
|
||||
| --------- | -------------------------- |
|
||||
| OpenAI | `$HOME/.r2ai.openai-key` |
|
||||
| Gemini | `$HOME/.r2ai.gemini-key` |
|
||||
| Anthropic | `$HOME/.r2ai.anthropic-key` |
|
||||
| Mistral | `$HOME/.r2ai.mistral-key` |
|
||||
|
||||
```console
|
||||
$ r2ai -K
|
||||
```
|
||||
|
||||
## Saving settings
|
||||
|
||||
You may customize and save your configuration settings using your OS's default settings file (e.g `~/.radare2rc` on Linux).
|
||||
For example, the following configuration sets Claude 3.7 by default, with max output tokens to 64000.
|
||||
|
||||
|
||||
```console
|
||||
$ r2ai -E
|
||||
```
|
||||
|
||||
then you can type the commands you want to run when the r2ai plugin is loaded:
|
||||
|
||||
```console
|
||||
r2ai -e api=anthropic
|
||||
r2ai -e model=claude-3-7-sonnet-20250219
|
||||
r2ai -e max_tokens=64000
|
||||
```
|
||||
|
||||
## Further Reading
|
||||
## Documentation
|
||||
|
||||
* 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://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
|
||||
* Malware analysis [with r2ai](https://github.com/cryptax/talks/blob/master/BSidesKristiansand-2025/r2ai.pdf) by Cryptax
|
||||
|
||||
## Videos
|
||||
|
||||
- [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)
|
||||
- https://infosec.exchange/@radareorg/111946255058894583
|
||||
+1
-4
@@ -1,6 +1,3 @@
|
||||
#!/bin/sh
|
||||
vim decai/src/constants.ts
|
||||
make -C decai/src
|
||||
vim src/r2ai.h
|
||||
vim src/meson.build
|
||||
vim src/r2ai.c
|
||||
vim VERSION
|
||||
+6
-5
@@ -1,16 +1,17 @@
|
||||
PD=$(shell r2 -H R2_USER_PLUGINS)
|
||||
SPD=$(shell r2 -H R2_LIBR_PLUGINS)
|
||||
BF=/bin/dd
|
||||
#BF=/bin/dd
|
||||
BF=a.out
|
||||
|
||||
all:
|
||||
@echo "Run: 'make user-install' to install it in your home"
|
||||
@echo "Run: 'make -C src' to recompile the r2.js"
|
||||
@echo "Run: make user-install"
|
||||
|
||||
test:
|
||||
@echo 'You can now use 'decai' command'
|
||||
r2 -i decai.r2.js -c 'decai -e deterministic=true' -caf $(BF)
|
||||
|
||||
fmt indent:
|
||||
$(MAKE) -C src fmt
|
||||
deno fmt
|
||||
|
||||
user-install:
|
||||
mkdir -p "$(PD)"
|
||||
@@ -26,4 +27,4 @@ uninstall:
|
||||
make user-uninstall PD=$(SPD)
|
||||
|
||||
vs open:
|
||||
open -a "Visual Studio Code" src
|
||||
open -a "Visual Studio Code" .
|
||||
+1241
-48
File diff suppressed because it is too large.
Load diff
@@ -1,18 +0,0 @@
|
||||
PLUGIN_NAME=decai
|
||||
R2F_C_FLAGS=-c -S
|
||||
|
||||
all: build
|
||||
|
||||
build:
|
||||
r2pm -r r2frida-compile $(R2F_C_FLAGS) -o ../$(PLUGIN_NAME).r2.js main.ts
|
||||
|
||||
test: build
|
||||
r2 -i ../$(PLUGIN_NAME).r2.js -q -c 'af;decai -d' /bin/ls
|
||||
|
||||
fmt:
|
||||
deno fmt
|
||||
|
||||
clean:
|
||||
rm -rf out node_modules ../$(PLUGIN_NAME).r2.js
|
||||
|
||||
.PHONY: all build test clean
|
||||
@@ -1,57 +0,0 @@
|
||||
import { ApiKeyResult } from "./types";
|
||||
import { fileExists, parseEnvLikeString } from "./utils";
|
||||
|
||||
const PROVIDER_ENV_MAP: Record<string, string> = {
|
||||
mistral: "MISTRAL_API_KEY",
|
||||
anthropic: "ANTHROPIC_API_KEY",
|
||||
huggingface: "HUGGINGFACE_API_KEY",
|
||||
openai: "OPENAI_API_KEY",
|
||||
gemini: "GEMINI_API_KEY",
|
||||
deepseek: "DEEPSEEK_API_KEY",
|
||||
xai: "XAI_API_KEY",
|
||||
ollama: "OLLAMA_API_KEY",
|
||||
ollamacloud: "OLLAMA_API_KEY",
|
||||
};
|
||||
|
||||
export function getApiKey(provider: string, envvar: string): ApiKeyResult {
|
||||
const keyEnv = r2.cmd("'%" + envvar).trim();
|
||||
if (keyEnv.indexOf("=") === -1 && keyEnv !== "") {
|
||||
return [keyEnv.trim(), null, "env"];
|
||||
}
|
||||
|
||||
const providerLower = provider.toLowerCase();
|
||||
const keysPath = "~/.config/r2ai/apikeys.txt";
|
||||
|
||||
if (fileExists(keysPath)) {
|
||||
const keyFile = r2.cmd("'cat " + keysPath);
|
||||
const kv = parseEnvLikeString(keyFile);
|
||||
if (Object.keys(kv).indexOf(providerLower) !== -1) {
|
||||
return [kv[providerLower], null, "txt"];
|
||||
}
|
||||
}
|
||||
|
||||
const keyPath = "~/.r2ai." + providerLower + "-key";
|
||||
if (fileExists(keyPath)) {
|
||||
const keyFile = r2.cmd("'cat " + keyPath);
|
||||
return keyFile === ""
|
||||
? [null, "Cannot read " + keyPath, "no"]
|
||||
: [keyFile.trim(), null, "file"];
|
||||
}
|
||||
|
||||
return [null, "Not available", "nope"];
|
||||
}
|
||||
|
||||
export function editApiKeys(): void {
|
||||
r2.cmd("'ed ~/.config/r2ai/apikeys.txt");
|
||||
}
|
||||
|
||||
export function listApiKeys(): void {
|
||||
Object.entries(PROVIDER_ENV_MAP).forEach(([key, env]) => {
|
||||
const status = getApiKey(key, env)[2];
|
||||
console.log(status, "\t", key);
|
||||
});
|
||||
}
|
||||
|
||||
export function getProviderEnvVar(provider: string): string {
|
||||
return PROVIDER_ENV_MAP[provider.toLowerCase()] || "";
|
||||
}
|
||||
@@ -1,533 +0,0 @@
|
||||
import { AutoReply } from "./types";
|
||||
import { state } from "./state";
|
||||
import { COMMAND, DEFAULT_PROMPT, HELP_TEXT, VERSION } from "./constants";
|
||||
import {
|
||||
b64,
|
||||
debugLog,
|
||||
filterResponse,
|
||||
trimAnsi,
|
||||
trimDown,
|
||||
trimJson,
|
||||
} from "./utils";
|
||||
import { editApiKeys, listApiKeys } from "./apiKeys";
|
||||
import { evalConfig, listAllConfig } from "./config";
|
||||
import { r2ai } from "./r2ai";
|
||||
|
||||
export function showHelp(): void {
|
||||
let helpmsg = "";
|
||||
const msg = (m: string) => (helpmsg += " " + COMMAND + " " + m + "\n");
|
||||
|
||||
helpmsg += "Usage: " + COMMAND + " (-h) ...\n";
|
||||
helpmsg += "Version: " + VERSION + "\n";
|
||||
msg("-a [query] - solve query with auto mode");
|
||||
msg("-b [url] - set base URL (alias for decai -e baseurl)");
|
||||
msg("-d [f1 ..] - decompile given functions");
|
||||
msg("-dd [..] - same as above, but ignoring cache");
|
||||
msg("-dD [query] - decompile current function with given extra query");
|
||||
msg("-dr - decompile function and its called ones (recursive)");
|
||||
msg("-e - display and change eval config vars");
|
||||
msg("-h - show this help");
|
||||
msg("-H - help setting up r2ai");
|
||||
msg("-i [f] [q] - include given file and query");
|
||||
msg("-k - list API key status");
|
||||
msg("-K - edit apikeys.txt");
|
||||
msg("-m [model] - use -m? or -e model=? to list the available models");
|
||||
msg("-n - suggest better function name");
|
||||
msg("-p [provider] - same as decai -e api (will be provider)");
|
||||
msg("-q [text] - query language model with given text");
|
||||
msg("-Q [text] - query on top of the last output");
|
||||
msg("-r [prompt] - change role prompt (same as: decai -e prompt)");
|
||||
msg("-R - reset role prompt to default prompt");
|
||||
msg("-s - function signature");
|
||||
msg("-v - show local variables");
|
||||
msg("-V - find vulnerabilities");
|
||||
msg("-x[*] - eXplain current function (-x* for r2 script)");
|
||||
|
||||
r2.log(helpmsg.trim());
|
||||
}
|
||||
|
||||
export function decompile(
|
||||
args: string,
|
||||
extraQuery: boolean,
|
||||
useCache: boolean,
|
||||
recursiveCalls: boolean,
|
||||
): string | undefined {
|
||||
if (useCache) {
|
||||
const cachedAnnotation = r2.cmd("anos").trim();
|
||||
if (cachedAnnotation.length > 0) {
|
||||
return cachedAnnotation;
|
||||
}
|
||||
}
|
||||
|
||||
let context = "";
|
||||
if (recursiveCalls) {
|
||||
const at = r2.cmd("s");
|
||||
context += "## Context functions:\n";
|
||||
const funcs = r2.cmdAt("axff~^C[2]~$$", at);
|
||||
for (const addr of funcs.split(/\n/g)) {
|
||||
context += r2.cmd("pdc@" + addr);
|
||||
}
|
||||
r2.cmd("s " + at);
|
||||
}
|
||||
|
||||
const appendQuery = extraQuery ? " " + args : "";
|
||||
const origColor = r2.cmd("e scr.color");
|
||||
|
||||
try {
|
||||
const parsedArgs = args.slice(2).trim();
|
||||
let count = 0;
|
||||
let text = "";
|
||||
|
||||
if (
|
||||
state.contextFile !== "" &&
|
||||
r2.cmd2("test -f " + state.contextFile).value === 0
|
||||
) {
|
||||
text += "## Context:\n[START]\n" +
|
||||
r2.cmd("cat " + state.contextFile) +
|
||||
"\n[END]\n";
|
||||
}
|
||||
|
||||
r2.cmd("e scr.color=0");
|
||||
let body = "## Before:\n";
|
||||
|
||||
for (const c of state.commands.split(",")) {
|
||||
if (c.trim() === "") continue;
|
||||
|
||||
const oneliner = extraQuery || parsedArgs.trim().length === 0
|
||||
? c
|
||||
: c + "@@= " + parsedArgs;
|
||||
const output = r2.cmd(oneliner);
|
||||
|
||||
if (output.length > 5) {
|
||||
body += "Output of " + c + ":\n[START]\n" + output + "\n[END]\n";
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
body += "## After:\n";
|
||||
r2.cmd("e scr.color=" + origColor);
|
||||
|
||||
if (count === 0) {
|
||||
console.error("Nothing to do.");
|
||||
return;
|
||||
}
|
||||
|
||||
let out = "";
|
||||
if (state.decopipe.use) {
|
||||
const dpipe = state.decopipe[state.decopipe.default as string] as {
|
||||
pipeline: Array<{ model?: string; query: string }>;
|
||||
globalQuery: string;
|
||||
};
|
||||
const origModel = state.model;
|
||||
let code = text + body;
|
||||
|
||||
for (const dp of dpipe.pipeline) {
|
||||
if (dp.model) state.model = dp.model;
|
||||
const query = dp.query + ". " + dpipe.globalQuery;
|
||||
out = r2ai(query, code, true);
|
||||
if (state.debug) {
|
||||
console.log("QUERY\n", query, "\nINPUT\n", code, "\nOUTPUT\n", out);
|
||||
}
|
||||
code = out;
|
||||
}
|
||||
out = code;
|
||||
state.model = origModel;
|
||||
} else {
|
||||
const query = appendQuery;
|
||||
text += body + context;
|
||||
out = r2ai(query, text, false);
|
||||
state.lastOutput = out;
|
||||
}
|
||||
|
||||
if (useCache && out.length > 1) {
|
||||
r2.call("ano=base64:" + b64(out));
|
||||
}
|
||||
|
||||
if (out.startsWith("```")) {
|
||||
out = out.replace(/```.*\n/, "").replace(/```$/, "");
|
||||
}
|
||||
|
||||
return out.trim();
|
||||
} catch (e) {
|
||||
r2.cmd("e scr.color=" + origColor);
|
||||
const err = e as Error;
|
||||
console.error(err, err.stack);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
export function explainFunction(): string {
|
||||
const origColor = r2.cmd("e scr.color");
|
||||
r2.cmd("e scr.color=0");
|
||||
const hints = "[START]" +
|
||||
state.commands.split(",").map((c) => r2.cmd(c)).join("\n") + "[END]";
|
||||
r2.cmd("e scr.color=" + origColor);
|
||||
|
||||
const res = r2ai(
|
||||
"Analyze function calls, references, comments and strings, loops and ignore registers and memory accesses. Explain the purpose of this function in a single short sentence. /no_think Do not introduce or argue the response, translation of the explanation in " +
|
||||
state.humanLanguage,
|
||||
hints,
|
||||
true,
|
||||
);
|
||||
|
||||
const lines = res.trim().split(/\n/g);
|
||||
return lines[lines.length - 1].trim();
|
||||
}
|
||||
|
||||
export function suggestSignature(): string {
|
||||
const tmp = state.language;
|
||||
const code = r2.cmd("afv;pdc");
|
||||
state.language = "C";
|
||||
|
||||
let out = "'afs " +
|
||||
r2ai(
|
||||
"analyze the uses of the arguments and return value to infer the signature, identify which is the correct type for the return. Do NOT print the function body, ONLY output the function signature, like if it was going to be used in a C header",
|
||||
code,
|
||||
false,
|
||||
);
|
||||
|
||||
const brace = out.indexOf("{");
|
||||
if (brace !== -1) {
|
||||
out = out.substring(0, brace);
|
||||
}
|
||||
|
||||
state.language = tmp;
|
||||
return out;
|
||||
}
|
||||
|
||||
export function autoMode(
|
||||
queryText: string,
|
||||
mainHandler: (args: string) => boolean,
|
||||
): void {
|
||||
const replies: string[] = [];
|
||||
|
||||
while (true) {
|
||||
let q = HELP_TEXT.auto;
|
||||
|
||||
if (replies.length > 0) {
|
||||
q += "## Command Results\n\n";
|
||||
for (const rep of replies) {
|
||||
const rp = JSON.parse(rep);
|
||||
q += "### " + rp.command + "\n\n```\n" + rp.response + "\n```\n";
|
||||
}
|
||||
}
|
||||
|
||||
q += "\n\n## User Prompt\n\n" + queryText;
|
||||
|
||||
if (state.debug) {
|
||||
console.log("#### input\n", q, "\n#### /input");
|
||||
}
|
||||
|
||||
console.log("Thinking...");
|
||||
const out = r2ai("", q, true);
|
||||
|
||||
if (state.debug) {
|
||||
console.log("#### output\n", out, "\n#### /output");
|
||||
}
|
||||
|
||||
try {
|
||||
const o: AutoReply = JSON.parse(
|
||||
trimJson(trimDown(filterResponse(out))),
|
||||
);
|
||||
|
||||
if (
|
||||
o.action === "r2cmd" ||
|
||||
o.action === "response" ||
|
||||
o.action === o.command
|
||||
) {
|
||||
const ocmd = o.command || "";
|
||||
|
||||
if (o.reason) {
|
||||
console.log("[r2cmd] Reasoning: " + o.reason);
|
||||
if (state.tts) {
|
||||
r2.syscmd("pkill say");
|
||||
r2.syscmd(
|
||||
"say -v Alex -r 250 '" + o.reason.replace(/'/g, "") + "' &",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
console.log("[r2cmd] Action: " + o.description);
|
||||
console.log("[r2cmd] Command: " + ocmd);
|
||||
|
||||
let cmd = ocmd;
|
||||
if (!state.yolo) {
|
||||
cmd = autoRepl(ocmd, mainHandler);
|
||||
}
|
||||
|
||||
console.log("[r2cmd] Running: " + cmd);
|
||||
const obj = r2.cmd2(cmd);
|
||||
const logs = obj.logs
|
||||
? obj.logs.map((x: { type: string; message: string }) =>
|
||||
x.type + ": " + x.message
|
||||
).join("\n")
|
||||
: "";
|
||||
const res = (obj.res + logs).trim();
|
||||
|
||||
console.log(res);
|
||||
const cleanRes = trimAnsi(res);
|
||||
|
||||
if (state.debug) {
|
||||
console.log("<r2output>\n", cleanRes, "\n<(r2output>");
|
||||
}
|
||||
|
||||
replies.push(
|
||||
JSON.stringify({
|
||||
action: "response",
|
||||
command: cmd,
|
||||
description: o.description,
|
||||
response: cleanRes,
|
||||
}),
|
||||
);
|
||||
} else if (o.action === "reply") {
|
||||
console.log("Done\n", o.response);
|
||||
break;
|
||||
} else {
|
||||
console.log("Unknown response\n", JSON.stringify(out));
|
||||
}
|
||||
} catch (e) {
|
||||
const response = out.indexOf('response": "');
|
||||
if (response !== -1) {
|
||||
const res = out
|
||||
.slice(response + 12)
|
||||
.replace(/\\n/g, "\n")
|
||||
.replace(/\\/g, "");
|
||||
console.log(res);
|
||||
} else {
|
||||
console.log(out);
|
||||
console.error(e);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function autoRepl(
|
||||
ocmd: string,
|
||||
mainHandler: (args: string) => boolean,
|
||||
): string {
|
||||
while (true) {
|
||||
const cmd = r2.cmd("'?ie Tweak command? ('?' for help)").trim();
|
||||
|
||||
if (cmd === "q!") {
|
||||
console.error("Break!");
|
||||
break;
|
||||
}
|
||||
if (cmd === "?") {
|
||||
autoHelp();
|
||||
continue;
|
||||
} else if (cmd.startsWith(":")) {
|
||||
console.log(r2.cmd(cmd.slice(1)));
|
||||
continue;
|
||||
} else if (cmd.startsWith("-e")) {
|
||||
mainHandler(cmd);
|
||||
continue;
|
||||
} else if (cmd === "!") {
|
||||
return "?e do NOT execute '" + ocmd + "' again, continue without it";
|
||||
} else if (cmd.startsWith("!")) {
|
||||
console.log(r2.syscmd(cmd.slice(1)));
|
||||
continue;
|
||||
} else if (cmd === "q") {
|
||||
return "?e All data collected!. Do not call more commands, reply the solutions";
|
||||
} else if (!cmd) {
|
||||
return ocmd;
|
||||
} else {
|
||||
const comment = cmd.indexOf("#");
|
||||
if (comment !== -1) {
|
||||
const command = cmd.slice(0, comment).trim();
|
||||
return command;
|
||||
}
|
||||
return cmd;
|
||||
}
|
||||
}
|
||||
return ocmd;
|
||||
}
|
||||
|
||||
function autoHelp(): void {
|
||||
console.log(" '!' do not run this command");
|
||||
console.log(" '!c' run system command");
|
||||
console.log(" 'q' to quit auto and try to solve");
|
||||
console.log(" 'q!' quit auto without solving");
|
||||
console.log(" 'c # C' use given command with comment");
|
||||
console.log(" ':c' run r2 command without feeding auto");
|
||||
console.log(" '-e' set decai configuration variables");
|
||||
}
|
||||
|
||||
export function handleCommand(
|
||||
args: string,
|
||||
mainHandler: (args: string) => boolean,
|
||||
): string | undefined {
|
||||
if (args === "" || !args.startsWith("-")) {
|
||||
showHelp();
|
||||
return;
|
||||
}
|
||||
|
||||
let output = "";
|
||||
const flag = args[1];
|
||||
|
||||
switch (flag) {
|
||||
case "H":
|
||||
console.log(HELP_TEXT.decai);
|
||||
break;
|
||||
|
||||
case "a":
|
||||
autoMode(args.slice(2).trim(), mainHandler);
|
||||
break;
|
||||
|
||||
case "m": {
|
||||
const arg0 = args.slice(2).trim();
|
||||
if (arg0 === "=") {
|
||||
evalConfig("model=");
|
||||
} else if (arg0) {
|
||||
evalConfig("model=" + arg0);
|
||||
} else {
|
||||
evalConfig("model");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case "n":
|
||||
case "f": {
|
||||
output = r2.cmd("axff~$[3]");
|
||||
const considerations = r2
|
||||
.cmd("fd.")
|
||||
.trim()
|
||||
.split(/\n/)
|
||||
.filter((x: string) => !x.startsWith("secti"))
|
||||
.join(",");
|
||||
|
||||
output = r2ai(
|
||||
"give me a better name for this function. the output must be: 'afn NEWNAME'. do not include the function code, only the afn line. consider: " +
|
||||
considerations,
|
||||
output,
|
||||
false,
|
||||
).trim();
|
||||
output += " @ " + r2.cmd("?v $FB").trim();
|
||||
break;
|
||||
}
|
||||
|
||||
case "v":
|
||||
output = r2.cmd("afv;pdc");
|
||||
output = r2ai(
|
||||
"guess a better name and type for each local variable and function argument taking using. output an r2 script using afvn and afvt commands",
|
||||
output,
|
||||
false,
|
||||
);
|
||||
break;
|
||||
|
||||
case "i": {
|
||||
const parts = args.slice(2).trim().split(/ /g);
|
||||
if (parts.length >= 2) {
|
||||
const file = parts[0];
|
||||
const query = parts.slice(1).join(" ");
|
||||
const fileData = r2.cmd("cat " + file);
|
||||
console.log(r2ai(query, fileData, true));
|
||||
} else {
|
||||
console.log("Use: decai -i [file] [query ...]");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case "p": {
|
||||
const evalArg = args.slice(2).trim();
|
||||
if (evalArg) {
|
||||
evalConfig("api=" + evalArg);
|
||||
} else {
|
||||
listApiKeys();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case "r": {
|
||||
const prompt = args.slice(2).trim();
|
||||
if (prompt) {
|
||||
state.prompt = prompt;
|
||||
} else {
|
||||
console.log(state.prompt);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case "R":
|
||||
state.prompt = DEFAULT_PROMPT;
|
||||
break;
|
||||
|
||||
case "s":
|
||||
output = suggestSignature();
|
||||
break;
|
||||
|
||||
case "V":
|
||||
output = r2ai(
|
||||
"find vulnerabilities, dont show the code, only show the response, provide a sample exploit",
|
||||
state.lastOutput,
|
||||
false,
|
||||
);
|
||||
break;
|
||||
|
||||
case "K":
|
||||
editApiKeys();
|
||||
break;
|
||||
|
||||
case "k":
|
||||
listApiKeys();
|
||||
break;
|
||||
|
||||
case "b": {
|
||||
const baseUrlArg = args.slice(2).trim();
|
||||
if (baseUrlArg) {
|
||||
evalConfig("baseurl=" + baseUrlArg);
|
||||
} else {
|
||||
console.log(state.baseurl);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case "e": {
|
||||
const evalArg = args.slice(2).trim();
|
||||
if (evalArg) {
|
||||
evalConfig(evalArg);
|
||||
} else {
|
||||
listAllConfig();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case "q":
|
||||
try {
|
||||
output = r2ai(args.slice(2).trim(), null, true);
|
||||
} catch (e) {
|
||||
const err = e as Error;
|
||||
console.error(err, err.stack);
|
||||
}
|
||||
break;
|
||||
|
||||
case "Q":
|
||||
output = r2ai(args.slice(2).trim(), state.lastOutput, false);
|
||||
break;
|
||||
|
||||
case "x":
|
||||
output = explainFunction();
|
||||
if (args[2] === "*" || args[2] === "r") {
|
||||
output = "'CC " + output;
|
||||
}
|
||||
break;
|
||||
|
||||
case "d":
|
||||
if (args[2] === "r") {
|
||||
output = decompile(args.slice(2), true, state.cache, true) || "";
|
||||
} else if (args[2] === "d") {
|
||||
output = decompile(args, false, false, false) || "";
|
||||
} else if (args[2] === "D") {
|
||||
output = decompile(args, true, false, false) || "";
|
||||
} else {
|
||||
output = decompile(args, false, state.cache, false) || "";
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
showHelp();
|
||||
break;
|
||||
}
|
||||
|
||||
return output || undefined;
|
||||
}
|
||||
@@ -1,147 +0,0 @@
|
||||
import { ConfigHandlers } from "./types";
|
||||
import { state } from "./state";
|
||||
import { listModelsFor } from "./models";
|
||||
import { listProviders } from "./providers";
|
||||
|
||||
export const configHandlers: ConfigHandlers = {
|
||||
pipeline: {
|
||||
get: () => state.pipeline,
|
||||
set: (v: string) => {
|
||||
state.pipeline = v;
|
||||
try {
|
||||
state.decopipe = JSON.parse(r2.cmd("cat " + v));
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
},
|
||||
},
|
||||
model: {
|
||||
get: () => state.model,
|
||||
set: (v: string) => {
|
||||
if (v === "?") {
|
||||
listModelsFor(state.api);
|
||||
} else {
|
||||
state.model = v.trim();
|
||||
}
|
||||
},
|
||||
},
|
||||
deterministic: {
|
||||
get: () => state.deterministic,
|
||||
set: (v: string) => {
|
||||
state.deterministic = v === "true" || v === "1";
|
||||
},
|
||||
},
|
||||
files: {
|
||||
get: () => state.useFiles,
|
||||
set: (v: string) => {
|
||||
state.useFiles = v === "true";
|
||||
},
|
||||
},
|
||||
think: {
|
||||
get: () => state.think,
|
||||
set: (v: string) => {
|
||||
state.think = v === "true" ? 1 : v === "false" ? 0 : +v;
|
||||
},
|
||||
},
|
||||
debug: {
|
||||
get: () => state.debug,
|
||||
set: (v: string) => {
|
||||
state.debug = v === "true" || v === "1";
|
||||
},
|
||||
},
|
||||
api: {
|
||||
get: () => state.api,
|
||||
set: (v: string) => {
|
||||
if (v === "?") {
|
||||
const providersList = listProviders().join("\n");
|
||||
console.error(providersList);
|
||||
} else {
|
||||
state.api = v;
|
||||
}
|
||||
},
|
||||
},
|
||||
lang: {
|
||||
get: () => state.language,
|
||||
set: (v: string) => {
|
||||
state.language = v;
|
||||
},
|
||||
},
|
||||
hlang: {
|
||||
get: () => state.humanLanguage,
|
||||
set: (v: string) => {
|
||||
state.humanLanguage = v;
|
||||
},
|
||||
},
|
||||
cache: {
|
||||
get: () => state.cache,
|
||||
set: (v: string) => {
|
||||
state.cache = v === "true" || v === "1";
|
||||
},
|
||||
},
|
||||
cmds: {
|
||||
get: () => state.commands,
|
||||
set: (v: string) => {
|
||||
state.commands = v;
|
||||
},
|
||||
},
|
||||
tts: {
|
||||
get: () => state.tts,
|
||||
set: (v: string) => {
|
||||
state.tts = v === "true" || v === "1";
|
||||
},
|
||||
},
|
||||
yolo: {
|
||||
get: () => state.yolo,
|
||||
set: (v: string) => {
|
||||
state.yolo = v === "true" || v === "1";
|
||||
},
|
||||
},
|
||||
prompt: {
|
||||
get: () => state.prompt,
|
||||
set: (v: string) => {
|
||||
state.prompt = v;
|
||||
},
|
||||
},
|
||||
ctxfile: {
|
||||
get: () => state.contextFile,
|
||||
set: (v: string) => {
|
||||
state.contextFile = v;
|
||||
},
|
||||
},
|
||||
baseurl: {
|
||||
get: () => state.baseurl,
|
||||
set: (v: string) => {
|
||||
state.baseurl = v;
|
||||
},
|
||||
},
|
||||
maxtokens: {
|
||||
get: () => state.maxInputTokens,
|
||||
set: (v: string) => {
|
||||
state.maxInputTokens = parseInt(v, 10) || -1;
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export function evalConfig(arg: string): void {
|
||||
const eqIndex = arg.indexOf("=");
|
||||
const k = eqIndex === -1 ? arg : arg.slice(0, eqIndex);
|
||||
const v = eqIndex === -1 ? undefined : arg.slice(eqIndex + 1);
|
||||
|
||||
if (!configHandlers[k]) {
|
||||
console.error("Unknown config key");
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof v !== "undefined") {
|
||||
configHandlers[k].set(v);
|
||||
} else {
|
||||
console.log(configHandlers[k].get());
|
||||
}
|
||||
}
|
||||
|
||||
export function listAllConfig(): void {
|
||||
Object.keys(configHandlers).forEach((key) => {
|
||||
const value = configHandlers[key].get();
|
||||
console.log("decai -e " + key + "=" + value);
|
||||
});
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
export const VERSION = "1.3.2";
|
||||
export const COMMAND = "decai";
|
||||
|
||||
export 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";
|
||||
|
||||
export const HELP_TEXT = {
|
||||
decai: `# Using Decai
|
||||
|
||||
Decai is the radare2 plugin for decompiling functions with the help of language models.
|
||||
|
||||
By default uses a local ollama server, but can you can pick any other service by using 'decai -e api=?'.
|
||||
|
||||
[0x00000000]> decai -e api=?
|
||||
r2ai deepseek anthropic gemini hf mistral ollama openapi openai vllm xai
|
||||
|
||||
## Using Ollama
|
||||
|
||||
* Visit https://ollama.com to install it.
|
||||
* Download the model of choice: 'ollama run llama3.3'
|
||||
* Configure decai to use the given model with: 'decai -e model=?'
|
||||
|
||||
These are the most recommended models for decompiling in local:
|
||||
|
||||
* hhao/qwen2.5-coder-tools:latest (18GB of ram)
|
||||
* hhao/qwen2.5-coder-tools:32b (24GB of ram required)
|
||||
|
||||
## Common Options
|
||||
* 'decai -e baseurl=<url>' override default host and port for API endpoint (e.g., 'http://localhost:11434')
|
||||
|
||||
* 'decai -e deterministic=true' to remove randomness from decompilation responses
|
||||
* 'decai -e lang=Python' to output the decompilation in Python instead of C
|
||||
* 'decai -e hlang=Catalan' to add comments or explanations in that language (instead of English)
|
||||
* 'decai -e cmds=pdd,pdg' use r2dec and r2ghidra instead of r2's pdc as input for decompiling
|
||||
* 'decai -e prompt=..' default prompt must be fine for most models and binaries, feel free to tweak it
|
||||
|
||||
## API Keys
|
||||
|
||||
Remove services like OpenAI, Mistral, Anthropic, Grok, Gemini, .. require API keys to work.
|
||||
|
||||
See 'decai -k' to list the status of available APIkeys
|
||||
|
||||
Decai will pick them from the environment or the config files in your home:
|
||||
|
||||
* echo KEY > ~/.r2ai.openai-key
|
||||
* export OPENAI_API_KEY=...
|
||||
|
||||
## Using the R2AI Server:
|
||||
|
||||
Install r2ai or r2ai-server with r2pm:
|
||||
|
||||
[0x0000000]> decai -e api=r2ai
|
||||
[0x0000000]> r2pm -ci r2ai
|
||||
|
||||
Choose one of the recommended models (after r2pm -r r2ai):
|
||||
|
||||
* -m ibm-granite/granite-20b-code-instruct-8k-GGUF
|
||||
* -m QuantFactory/granite-8b-code-instruct-4k-GGUF
|
||||
* -m TheBloke/Mistral-7B-Instruct-v0.2-GGUF
|
||||
|
||||
Start the webserver:
|
||||
|
||||
$ r2pm -r r2ai-server -l r2ai -m granite-8...
|
||||
`,
|
||||
auto: `# Radare2 Auto Mode
|
||||
|
||||
Use function calling to execute radare2 commands in order to resolve the user request defined in the "User Prompt" section, analyze the responses attached in the "Command Results" section.
|
||||
|
||||
## Function Calling
|
||||
|
||||
Respond ONLY using plain JSON. Process user query and decide which function calls are necessary to solve the task.
|
||||
|
||||
1. Analyze the user request to determine if we need to run commands to extend the knowledge and context of the problem.
|
||||
2. If function call is needed, construct the JSON like this:
|
||||
- Fill the "action" key with the "r2cmd" value.
|
||||
- Specify the "command" as a string.
|
||||
- Optionally, provide a "reason" and "description"
|
||||
3. If the answer can be provided and no more function calls are required:
|
||||
- Use the key "action": "reply".
|
||||
- Include "response" with the direct answer to the user query.
|
||||
|
||||
Return the result as a JSON object.
|
||||
|
||||
### Sample Function Calling Communication
|
||||
|
||||
Command Results: already performed actions with their responses
|
||||
User Prompt: "Count how many functions we have here."
|
||||
Response:
|
||||
{
|
||||
"action": "r2cmd",
|
||||
"command": "aflc",
|
||||
"description": "Count functions"
|
||||
"reason": "Evaluate if the program is analyzed before running aaa"
|
||||
}
|
||||
|
||||
## Rules
|
||||
|
||||
Use radare2 to resolve user requests.
|
||||
|
||||
* Explain each step in the "reason" field of the JSON.
|
||||
* Follow the initial analysis instructions.
|
||||
* Output only valid JSON as specified.
|
||||
* Decompile and inspect functions, starting from main.
|
||||
* Run only the needed commands to gather info.
|
||||
* Use "@ (address|symbol)" to seek temporarily.
|
||||
* Output should be a verbose markdown report.
|
||||
* Use "sym." or "fcn." prefixes if "pdc" is empty.
|
||||
* If a seek fails, use "f~name" to find the symbol's address.
|
||||
|
||||
### Initial Analysis
|
||||
|
||||
1. Run "aflc" to count the number of functions
|
||||
2. If the output of "aflc" is "0" run "aaa" once, then "aflc" again
|
||||
3. Run only one command at a time (do not use ";")
|
||||
|
||||
### Special cases
|
||||
|
||||
* On Swift binaries run...
|
||||
`,
|
||||
};
|
||||
@@ -1,74 +0,0 @@
|
||||
import { HttpResponse } from "./types";
|
||||
import { state } from "./state";
|
||||
import { debugLog } from "./utils";
|
||||
|
||||
interface HttpRequestOptions {
|
||||
method: "GET" | "POST";
|
||||
url: string;
|
||||
headers: string[];
|
||||
payload?: string;
|
||||
}
|
||||
|
||||
function shellEscape(arg: string): string {
|
||||
return `'${arg.replace(/'/g, `'"'"'`)}'`;
|
||||
}
|
||||
|
||||
function executeCurl(options: HttpRequestOptions): string {
|
||||
const { method, url, headers, payload } = options;
|
||||
const cmdParts = ["curl", "-s", shellEscape(url)];
|
||||
headers.forEach((h) => cmdParts.push("-H", shellEscape(h)));
|
||||
cmdParts.push("-H", shellEscape("Content-Type: application/json"));
|
||||
|
||||
if (method === "POST") {
|
||||
if (!payload) throw new Error("Payload required for POST requests");
|
||||
const tmpfile = r2.fdump(payload);
|
||||
cmdParts.push("-d", shellEscape(`@${tmpfile}`));
|
||||
const cmd = cmdParts.join(" ") + " && rm " + shellEscape(tmpfile);
|
||||
debugLog(cmd);
|
||||
return r2.syscmds(cmd);
|
||||
} else {
|
||||
const cmd = cmdParts.join(" ");
|
||||
debugLog(cmd);
|
||||
return r2.syscmds(cmd);
|
||||
}
|
||||
}
|
||||
|
||||
function parseJson(output: string): any {
|
||||
if (output === "") {
|
||||
throw new Error("empty response");
|
||||
}
|
||||
try {
|
||||
return JSON.parse(output);
|
||||
} catch (e) {
|
||||
const err = e as Error;
|
||||
console.error("output:", output);
|
||||
console.error(err, err.stack);
|
||||
throw new Error(err.message || "JSON parse error");
|
||||
}
|
||||
}
|
||||
|
||||
export function httpRequest(options: HttpRequestOptions): HttpResponse {
|
||||
try {
|
||||
const output = executeCurl(options);
|
||||
try {
|
||||
return parseJson(output);
|
||||
} catch (e) {
|
||||
return { error: (e as Error).message };
|
||||
}
|
||||
} catch (e) {
|
||||
const err = e as Error;
|
||||
return { error: err.message };
|
||||
}
|
||||
}
|
||||
|
||||
export function httpGet(url: string, headers: string[]): HttpResponse {
|
||||
return httpRequest({ method: "GET", url, headers });
|
||||
}
|
||||
|
||||
export function httpPost(
|
||||
url: string,
|
||||
headers: string[],
|
||||
payload: string,
|
||||
): HttpResponse {
|
||||
return httpRequest({ method: "POST", url, headers, payload });
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
import type { R2PipeSync } from "./r2pipe";
|
||||
|
||||
import { COMMAND } from "./constants";
|
||||
import { handleCommand } from "./commands";
|
||||
|
||||
function main(args: string): boolean {
|
||||
const output = handleCommand(args, main);
|
||||
if (output) {
|
||||
r2.log(output);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function installPlugin(): void {
|
||||
r2.unload("core", COMMAND);
|
||||
r2.plugin("core", function () {
|
||||
function coreCall(cmd: string): boolean {
|
||||
if (cmd.startsWith(COMMAND)) {
|
||||
const args = cmd.slice(COMMAND.length).trim();
|
||||
return main(args);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return {
|
||||
name: COMMAND,
|
||||
license: "MIT",
|
||||
desc: "r2 decompiler based on r2ai",
|
||||
call: coreCall,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
installPlugin();
|
||||
@@ -1,37 +0,0 @@
|
||||
import { getProvider } from "./providers";
|
||||
|
||||
export function listModelsFor(api: string): void {
|
||||
const providerConfig = getProvider(api);
|
||||
|
||||
if (!providerConfig) {
|
||||
console.error(`Unknown provider: ${api}`);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
let models = "";
|
||||
if (providerConfig.listModelsCallback) {
|
||||
models = providerConfig.listModelsCallback(providerConfig);
|
||||
}
|
||||
|
||||
if (models) {
|
||||
console.log(models);
|
||||
}
|
||||
|
||||
if (providerConfig.hardcodedModels) {
|
||||
providerConfig.hardcodedModels.forEach((model) => console.log(model));
|
||||
}
|
||||
|
||||
if (api === "mistral") {
|
||||
console.log("ministral-8b-latest");
|
||||
}
|
||||
|
||||
if (!models && !providerConfig.hardcodedModels) {
|
||||
console.log(providerConfig.defaultModel);
|
||||
}
|
||||
} catch (e) {
|
||||
const err = e as Error;
|
||||
console.error(`Error listing models for ${api}:`, err.message);
|
||||
console.log(providerConfig.defaultModel);
|
||||
}
|
||||
}
|
||||
@@ -1,447 +0,0 @@
|
||||
import {
|
||||
ApiKeyResult,
|
||||
HeadersBuilder,
|
||||
PayloadBuilder,
|
||||
ProviderConfig,
|
||||
ProviderRegistry,
|
||||
ResponseParser,
|
||||
UrlBuilder,
|
||||
} from "./types";
|
||||
import { state } from "./state";
|
||||
import { filterResponse, padRight } from "./utils";
|
||||
import { getApiKey } from "./apiKeys";
|
||||
import { httpGet, httpPost } from "./http";
|
||||
|
||||
function listModels(
|
||||
url: string,
|
||||
headers: string[],
|
||||
mapper: (model: any) => string,
|
||||
): string {
|
||||
const response = httpGet(url, headers);
|
||||
if (response.error) {
|
||||
console.error(response.error);
|
||||
return "error invalid response";
|
||||
}
|
||||
return (response as any).data?.map(mapper).join("\n") || "";
|
||||
}
|
||||
|
||||
const listOllamaModels = (provider: ProviderConfig): string => {
|
||||
const base = state.baseurl || state.host + ":" + state.port;
|
||||
return listModels(
|
||||
`${base}/api/tags`,
|
||||
[],
|
||||
(model: { name: string }) => model.name,
|
||||
);
|
||||
};
|
||||
|
||||
const listOpenAIModels = (provider: ProviderConfig): string => {
|
||||
let key: ApiKeyResult | undefined;
|
||||
if (provider.requiresAuth && provider.authKey) {
|
||||
key = getApiKey(
|
||||
provider.authKey.split("_")[0].toLowerCase(),
|
||||
provider.authKey,
|
||||
);
|
||||
if (key && key[1]) throw new Error(key[1]);
|
||||
}
|
||||
const base = state.baseurl || provider.defaultBaseurl;
|
||||
const url = base + "/v1/models";
|
||||
const headers = key ? ["Authorization: Bearer " + key[0]] : [];
|
||||
return listModels(url, headers, (model: any) => model.id);
|
||||
};
|
||||
|
||||
const listAnthropicModels = (provider: ProviderConfig): string => {
|
||||
const key = getApiKey("anthropic", "ANTHROPIC_API_KEY");
|
||||
if (key && key[1]) throw new Error(key[1]);
|
||||
const base = state.baseurl || provider.defaultBaseurl;
|
||||
const url = base + "/v1/models";
|
||||
const headers = ["x-api-key: " + key[0], "anthropic-version: 2023-06-01"];
|
||||
return listModels(url, headers, (model: any) => model.id);
|
||||
};
|
||||
|
||||
const listMistralModels = (provider: ProviderConfig): string => {
|
||||
const key = getApiKey("mistral", "MISTRAL_API_KEY");
|
||||
if (key && key[1]) throw new Error(key[1]);
|
||||
const base = state.baseurl || provider.defaultBaseurl;
|
||||
const url = base + "/v1/models";
|
||||
const headers = ["Authorization: Bearer " + key[0]];
|
||||
const response = httpGet(url, headers) as any;
|
||||
if (response.data) {
|
||||
const uniqByName = (arr: any[]) =>
|
||||
arr.filter((obj, i, self) =>
|
||||
self.findIndex((o) => o.name === obj.name) === i
|
||||
);
|
||||
return uniqByName(response.data)
|
||||
.map((model: any) =>
|
||||
[
|
||||
padRight(model.name || model.id, 30),
|
||||
padRight("" + (model.max_context_length || ""), 10),
|
||||
model.description || "",
|
||||
].join(" ")
|
||||
)
|
||||
.join("\n");
|
||||
}
|
||||
return "";
|
||||
};
|
||||
|
||||
export const providerRegistry: ProviderRegistry = {
|
||||
anthropic: {
|
||||
defaultModel: "claude-3-7-sonnet-20250219",
|
||||
defaultBaseurl: "https://api.anthropic.com",
|
||||
requiresAuth: true,
|
||||
authKey: "ANTHROPIC_API_KEY",
|
||||
apiStyle: "anthropic",
|
||||
listModelsCallback: listAnthropicModels,
|
||||
},
|
||||
claude: {
|
||||
defaultModel: "claude-3-7-sonnet-20250219",
|
||||
defaultBaseurl: "https://api.anthropic.com",
|
||||
requiresAuth: true,
|
||||
authKey: "ANTHROPIC_API_KEY",
|
||||
apiStyle: "anthropic",
|
||||
listModelsCallback: listAnthropicModels,
|
||||
},
|
||||
openai: {
|
||||
defaultModel: "gpt-4o-mini",
|
||||
defaultBaseurl: "https://api.openai.com",
|
||||
requiresAuth: true,
|
||||
authKey: "OPENAI_API_KEY",
|
||||
apiStyle: "openai",
|
||||
listModelsCallback: listOpenAIModels,
|
||||
},
|
||||
ollama: {
|
||||
defaultModel: "qwen2.5-coder:latest",
|
||||
defaultBaseurl: "http://localhost:11434",
|
||||
requiresAuth: false,
|
||||
apiStyle: "ollama",
|
||||
listModelsCallback: listOllamaModels,
|
||||
},
|
||||
ollamacloud: {
|
||||
defaultModel: "gpt-oss:120b",
|
||||
defaultBaseurl: "https://ollama.com",
|
||||
requiresAuth: true,
|
||||
authKey: "OLLAMA_API_KEY",
|
||||
apiStyle: "openai",
|
||||
listModelsCallback: listOpenAIModels,
|
||||
},
|
||||
gemini: {
|
||||
defaultModel: "gemini-2.5-flash",
|
||||
defaultBaseurl: "https://generativelanguage.googleapis.com",
|
||||
requiresAuth: true,
|
||||
authKey: "GEMINI_API_KEY",
|
||||
apiStyle: "gemini",
|
||||
hardcodedModels: [
|
||||
"gemini-2.0-flash",
|
||||
"gemini-2.0-flash-lite",
|
||||
"gemini-2.5-pro",
|
||||
"gemini-2.5-flash",
|
||||
"gemini-2.5-flash-lite",
|
||||
],
|
||||
},
|
||||
mistral: {
|
||||
defaultModel: "codestral-latest",
|
||||
defaultBaseurl: "https://api.mistral.ai",
|
||||
requiresAuth: true,
|
||||
authKey: "MISTRAL_API_KEY",
|
||||
apiStyle: "openai",
|
||||
listModelsCallback: listMistralModels,
|
||||
},
|
||||
xai: {
|
||||
defaultModel: "grok-beta",
|
||||
defaultBaseurl: "https://api.x.ai",
|
||||
requiresAuth: true,
|
||||
authKey: "XAI_API_KEY",
|
||||
apiStyle: "openai",
|
||||
hardcodedModels: ["grok-2", "grok-beta"],
|
||||
},
|
||||
lmstudio: {
|
||||
defaultModel: "local-model",
|
||||
defaultBaseurl: "http://127.0.0.1:1234",
|
||||
requiresAuth: false,
|
||||
apiStyle: "openai",
|
||||
listModelsCallback: listOpenAIModels,
|
||||
},
|
||||
deepseek: {
|
||||
defaultModel: "deepseek-coder",
|
||||
defaultBaseurl: "https://api.deepseek.com",
|
||||
requiresAuth: true,
|
||||
authKey: "DEEPSEEK_API_KEY",
|
||||
apiStyle: "openai",
|
||||
hardcodedModels: ["deepseek-coder", "deepseek-chat"],
|
||||
},
|
||||
};
|
||||
|
||||
export function getProvider(api: string): ProviderConfig | undefined {
|
||||
return providerRegistry[api];
|
||||
}
|
||||
|
||||
export function listProviders(): string[] {
|
||||
return Object.keys(providerRegistry);
|
||||
}
|
||||
|
||||
function buildQuery(msg: string, hideprompt: boolean): string {
|
||||
let query = msg;
|
||||
|
||||
if (state.think >= 0) {
|
||||
if (state.think === 0) {
|
||||
query +=
|
||||
' Answers directly and concisely, without showing any thinking steps or internal reasoning. Never include phrases like "Let me think".';
|
||||
query += " /no_think";
|
||||
} else if (state.think > 0) {
|
||||
query =
|
||||
"Think step by step and explain the reasoning process, When answering, first output your reasoning inside <think> and </think> tags, then give the final answer." +
|
||||
query;
|
||||
}
|
||||
}
|
||||
|
||||
return hideprompt ? query : state.prompt + languagePrompt() + query;
|
||||
}
|
||||
|
||||
function languagePrompt(): string {
|
||||
return "\n.Translate the code into " + state.language +
|
||||
" programming language\n";
|
||||
}
|
||||
|
||||
function handleProvider(
|
||||
provider: ProviderConfig,
|
||||
msg: string,
|
||||
hideprompt: boolean,
|
||||
payloadBuilder: PayloadBuilder,
|
||||
responseParser: ResponseParser,
|
||||
urlBuilder: UrlBuilder,
|
||||
headersBuilder: HeadersBuilder,
|
||||
): string {
|
||||
const model = state.model || provider.defaultModel;
|
||||
const query = buildQuery(msg, hideprompt);
|
||||
|
||||
let key: ApiKeyResult | undefined;
|
||||
if (provider.requiresAuth && provider.authKey) {
|
||||
key = getApiKey(
|
||||
provider.authKey.split("_")[0].toLowerCase(),
|
||||
provider.authKey,
|
||||
);
|
||||
if (key[1]) {
|
||||
return `Cannot read ~/.r2ai.${
|
||||
provider.authKey.split("_")[0].toLowerCase()
|
||||
}-key`;
|
||||
}
|
||||
}
|
||||
|
||||
const payload = payloadBuilder(model, query, provider);
|
||||
const base = state.baseurl || provider.defaultBaseurl;
|
||||
const url = urlBuilder(base, model, key && key[0] ? key[0] : undefined);
|
||||
const headers = headersBuilder(key ? key[0] : null, provider);
|
||||
|
||||
try {
|
||||
const res = httpPost(url, headers, JSON.stringify(payload));
|
||||
return responseParser(res);
|
||||
} catch (e) {
|
||||
const err = e as Error;
|
||||
return "ERROR: " + err.message;
|
||||
}
|
||||
}
|
||||
|
||||
export function handleOpenAI(
|
||||
provider: ProviderConfig,
|
||||
msg: string,
|
||||
hideprompt: boolean,
|
||||
): string {
|
||||
const payloadBuilder: PayloadBuilder = (model, query) => ({
|
||||
stream: false,
|
||||
model,
|
||||
messages: [{ role: "user", content: query }],
|
||||
});
|
||||
|
||||
const responseParser: ResponseParser = (res) => {
|
||||
if (res.error && typeof res.error === "object" && res.error.message) {
|
||||
throw new Error(res.error.message);
|
||||
}
|
||||
if (res.choices && res.choices[0]?.message?.content) {
|
||||
return filterResponse(res.choices[0].message.content);
|
||||
}
|
||||
throw new Error("Invalid response format");
|
||||
};
|
||||
|
||||
const urlBuilder: UrlBuilder = (base, model) => base + "/v1/chat/completions";
|
||||
|
||||
const headersBuilder: HeadersBuilder = (key) =>
|
||||
key ? ["Authorization: Bearer " + key] : [];
|
||||
|
||||
return handleProvider(
|
||||
provider,
|
||||
msg,
|
||||
hideprompt,
|
||||
payloadBuilder,
|
||||
responseParser,
|
||||
urlBuilder,
|
||||
headersBuilder,
|
||||
);
|
||||
}
|
||||
|
||||
export function handleAnthropic(
|
||||
provider: ProviderConfig,
|
||||
msg: string,
|
||||
hideprompt: boolean,
|
||||
): string {
|
||||
if (!provider.authKey) {
|
||||
return "ERROR: No auth key configured";
|
||||
}
|
||||
|
||||
const payloadBuilder: PayloadBuilder = (model, query) => {
|
||||
const payload: Record<string, unknown> = {
|
||||
model,
|
||||
max_tokens: 5128,
|
||||
messages: [{ role: "user", content: query }],
|
||||
};
|
||||
if (state.deterministic) {
|
||||
Object.assign(payload, { temperature: 0, top_p: 0, top_k: 1 });
|
||||
}
|
||||
return payload;
|
||||
};
|
||||
|
||||
const responseParser: ResponseParser = (res) => {
|
||||
if (res.content && res.content[0]?.text) {
|
||||
return filterResponse(res.content[0].text);
|
||||
}
|
||||
if (res.error) {
|
||||
const errMsg = typeof res.error === "object"
|
||||
? res.error.message
|
||||
: res.error;
|
||||
throw new Error(errMsg || "Unknown error");
|
||||
}
|
||||
throw new Error("Invalid response format");
|
||||
};
|
||||
const urlBuilder: UrlBuilder = (base, model) => base + "/v1/messages";
|
||||
const headersBuilder: HeadersBuilder = (
|
||||
key,
|
||||
) => ["anthropic-version: 2023-06-01", "x-api-key: " + key];
|
||||
return handleProvider(
|
||||
provider,
|
||||
msg,
|
||||
hideprompt,
|
||||
payloadBuilder,
|
||||
responseParser,
|
||||
urlBuilder,
|
||||
headersBuilder,
|
||||
);
|
||||
}
|
||||
|
||||
export function handleOllama(
|
||||
provider: ProviderConfig,
|
||||
msg: string,
|
||||
hideprompt: boolean,
|
||||
): string {
|
||||
const payloadBuilder: PayloadBuilder = (model, query) => {
|
||||
const payload: Record<string, unknown> = {
|
||||
stream: false,
|
||||
model,
|
||||
messages: [{ role: "user", content: query }],
|
||||
};
|
||||
if (state.deterministic) {
|
||||
payload.options = {
|
||||
repeat_last_n: 0,
|
||||
top_p: 0.0,
|
||||
top_k: 1.0,
|
||||
temperature: 0.0,
|
||||
repeat_penalty: 1.0,
|
||||
seed: 123,
|
||||
};
|
||||
}
|
||||
return payload;
|
||||
};
|
||||
|
||||
const responseParser: ResponseParser = (res) => {
|
||||
if (res && res.error) {
|
||||
const errMsg = typeof res.error === "string"
|
||||
? res.error
|
||||
: JSON.stringify(res.error);
|
||||
throw new Error(errMsg);
|
||||
}
|
||||
if (res.message && res.message.content) {
|
||||
return filterResponse(res.message.content);
|
||||
}
|
||||
throw new Error(JSON.stringify(res));
|
||||
};
|
||||
const urlBuilder: UrlBuilder = (base, model) => base + "/api/chat";
|
||||
const headersBuilder: HeadersBuilder = () => [];
|
||||
return handleProvider(
|
||||
provider,
|
||||
msg,
|
||||
hideprompt,
|
||||
payloadBuilder,
|
||||
responseParser,
|
||||
urlBuilder,
|
||||
headersBuilder,
|
||||
);
|
||||
}
|
||||
|
||||
export function handleGemini(
|
||||
provider: ProviderConfig,
|
||||
msg: string,
|
||||
hideprompt: boolean,
|
||||
): string {
|
||||
if (!provider.authKey) {
|
||||
return "ERROR: No auth key configured";
|
||||
}
|
||||
|
||||
const payloadBuilder: PayloadBuilder = (model, query) => {
|
||||
const payload: Record<string, unknown> = {
|
||||
contents: [{ parts: [{ text: query }] }],
|
||||
};
|
||||
if (state.deterministic) {
|
||||
payload.generationConfig = {
|
||||
temperature: 0.0,
|
||||
topP: 1.0,
|
||||
topK: 1,
|
||||
};
|
||||
}
|
||||
return payload;
|
||||
};
|
||||
|
||||
const responseParser: ResponseParser = (res) => {
|
||||
const r = res as any;
|
||||
if (r.candidates && r.candidates[0]?.content?.parts?.[0]?.text) {
|
||||
return filterResponse(r.candidates[0].content.parts[0].text);
|
||||
}
|
||||
if (r.error) {
|
||||
throw new Error(
|
||||
typeof r.error === "string" ? r.error : JSON.stringify(r.error),
|
||||
);
|
||||
}
|
||||
console.log(JSON.stringify(r));
|
||||
throw new Error("Invalid response format");
|
||||
};
|
||||
const urlBuilder: UrlBuilder = (base, model, key) =>
|
||||
`${base}/v1beta/models/${model}:generateContent?key=${key}`;
|
||||
const headersBuilder: HeadersBuilder = () => [];
|
||||
return handleProvider(
|
||||
provider,
|
||||
msg,
|
||||
hideprompt,
|
||||
payloadBuilder,
|
||||
responseParser,
|
||||
urlBuilder,
|
||||
headersBuilder,
|
||||
);
|
||||
}
|
||||
|
||||
export function callProvider(msg: string, hideprompt: boolean): string {
|
||||
const providerConfig = getProvider(state.api);
|
||||
|
||||
if (!providerConfig) {
|
||||
const availableApis = listProviders().join(", ");
|
||||
return `Unknown value for 'decai -e api'. Available: ${availableApis}`;
|
||||
}
|
||||
|
||||
switch (providerConfig.apiStyle) {
|
||||
case "openai":
|
||||
return handleOpenAI(providerConfig, msg, hideprompt);
|
||||
case "anthropic":
|
||||
return handleAnthropic(providerConfig, msg, hideprompt);
|
||||
case "ollama":
|
||||
return handleOllama(providerConfig, msg, hideprompt);
|
||||
case "gemini":
|
||||
return handleGemini(providerConfig, msg, hideprompt);
|
||||
default:
|
||||
return `Unsupported API style: ${providerConfig.apiStyle}`;
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
import { state } from "./state";
|
||||
import { debugLog, fileDump, tmpdir } from "./utils";
|
||||
import { callProvider } from "./providers";
|
||||
import { httpGet } from "./http";
|
||||
|
||||
export function r2ai(
|
||||
queryText: string,
|
||||
fileData: string | null,
|
||||
hideprompt: boolean = false,
|
||||
): string {
|
||||
const cleanFileData = (fileData || "").replace(/`/g, "");
|
||||
const cleanQuery = queryText.replace(/'/g, "");
|
||||
|
||||
// Handle r2ai server mode
|
||||
if (state.api === "r2" || state.api === "r2ai") {
|
||||
const fileName = tmpdir(".pdc.txt");
|
||||
fileDump(fileName, cleanFileData);
|
||||
|
||||
const q = cleanQuery.startsWith("-")
|
||||
? cleanQuery
|
||||
: ["-i", fileName, cleanQuery].join(" ");
|
||||
|
||||
const host = state.baseurl
|
||||
? state.baseurl + "/cmd"
|
||||
: state.host + ":" + state.port + "/cmd";
|
||||
|
||||
const url = host + "/" + q.replace(/ /g, "%20").replace(/'/g, "\\'");
|
||||
const response = httpGet(url, []);
|
||||
if (response.error) {
|
||||
return `Error: ${response.error}`;
|
||||
}
|
||||
return (response as any).result || JSON.stringify(response) ||
|
||||
"Cannot curl, use r2ai-server or r2ai -w";
|
||||
}
|
||||
|
||||
if (cleanQuery.startsWith("-")) return "";
|
||||
|
||||
let q = cleanQuery + ":\n" + cleanFileData;
|
||||
if (state.maxInputTokens > 0 && q.length > state.maxInputTokens) {
|
||||
q = q.slice(0, state.maxInputTokens);
|
||||
}
|
||||
|
||||
return callProvider(q, hideprompt);
|
||||
}
|
||||
@@ -1,232 +0,0 @@
|
||||
export class R2PipeSyncFromSync {
|
||||
r2p: R2Pipe;
|
||||
constructor(r2p: R2Pipe) {
|
||||
this.r2p = r2p;
|
||||
}
|
||||
/**
|
||||
* Run a command in the associated instance of radare2 and return the output as a string
|
||||
*
|
||||
* @param {string} command to be executed inside radare2.
|
||||
* @returns {string} The output of the command execution
|
||||
*/
|
||||
cmd(command: string): string {
|
||||
return this.r2p.cmd(command);
|
||||
}
|
||||
cmd2(command: string): any {
|
||||
return this.r2p.cmd(command);
|
||||
}
|
||||
cmdAt(
|
||||
command: string,
|
||||
address: number | string | any,
|
||||
): string {
|
||||
return this.r2p.cmdAt(command, address);
|
||||
}
|
||||
cmdj(cmd: string): any {
|
||||
return this.r2p.cmdj(cmd);
|
||||
}
|
||||
call(command: string): string {
|
||||
return this.r2p.call(command);
|
||||
}
|
||||
callj(cmd: string): any {
|
||||
return this.r2p.cmdj(cmd);
|
||||
}
|
||||
callAt(
|
||||
command: string,
|
||||
address: number | string | any,
|
||||
): string {
|
||||
return this.r2p.cmdAt(command, address);
|
||||
}
|
||||
log(msg: string) {
|
||||
return this.r2p.log(msg);
|
||||
}
|
||||
plugin(type: string, maker: any): boolean {
|
||||
return this.r2p.plugin(type, maker);
|
||||
}
|
||||
unload(type: string, name: string): boolean {
|
||||
return this.r2p.unload(type, name);
|
||||
}
|
||||
}
|
||||
|
||||
export function newAsyncR2PipeFromSync(r2p: R2Pipe): R2PipeSync {
|
||||
const asyncR2Pipe = new R2PipeSyncFromSync(r2p);
|
||||
return asyncR2Pipe as R2PipeSync;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic interface to interact with radare2, abstracts the access to the associated
|
||||
* instance of the tool, which could be native via rlang or remote via pipes or tcp/http.
|
||||
*
|
||||
* @typedef R2Pipe
|
||||
*/
|
||||
export interface R2Pipe {
|
||||
/**
|
||||
* Run a command in the associated instance of radare2 and return the output as a string
|
||||
*
|
||||
* @param {string} command to be executed inside radare2.
|
||||
* @returns {string} The output of the command execution
|
||||
*/
|
||||
cmd(cmd: string): string;
|
||||
|
||||
/**
|
||||
* Run a radare2 command in a different address. Same as `.cmd(x + '@ ' + a)`
|
||||
*
|
||||
* @param {string} command to be executed inside radare2.
|
||||
* @param {number|string|NativePointer} command to be executed inside radare2.
|
||||
* @returns {string} The output of the command execution
|
||||
*/
|
||||
cmdAt(cmd: string, address: number | string | any): string;
|
||||
|
||||
/**
|
||||
* Run a radare2 command expecting the output to be JSON
|
||||
*
|
||||
* @param {string} command to be executed inside radare2. The given command should end with `j`
|
||||
* @returns {object} the JSON decoded object from the output of the command
|
||||
*/
|
||||
cmdj(cmd: string): any;
|
||||
|
||||
/**
|
||||
* Call a radare2 command. This is similar to `R2Pipe.cmd`, but skips all the command parsing rules,
|
||||
* which is safer and faster but you cannot use any special modifier like `@`, `~`, ...
|
||||
*
|
||||
* See R2Pipe.callAt() to call a command on a different address
|
||||
*
|
||||
* @param {string} command to be executed inside radare2. The given command should end with `j`
|
||||
* @returns {object} the JSON decoded object from the output of the command
|
||||
*/
|
||||
call(cmd: string): string;
|
||||
|
||||
/**
|
||||
* Call a radare2 command in a different address
|
||||
*
|
||||
* @param {string} command to be executed inside radare2. The given command should end with `j`
|
||||
* @param {NativePointer|string|number} where to seek to execute this command (previous offset is restored after executing it)
|
||||
* @returns {string} the string containing the output of the command
|
||||
*/
|
||||
callAt(cmd: string, address: string | number | any): string;
|
||||
|
||||
/**
|
||||
* Same as cmdj but using .call which avoids command injection problems
|
||||
*
|
||||
* @param {string} command to be executed inside radare2. The given command should end with `j`
|
||||
* @returns {object} the JSON decoded object from the command output
|
||||
*/
|
||||
callj(cmd: string): any;
|
||||
|
||||
/**
|
||||
* Log a string to the associated console. This is used internally by `console.log` in some implementations.
|
||||
*
|
||||
* @param {string} text to be displayed
|
||||
*/
|
||||
log(msg: string): void;
|
||||
|
||||
/**
|
||||
* Instantiate a new radare2 plugin with the given type and constructor method.
|
||||
*
|
||||
* @param {string} type of plugin ("core", "io", "arch", ...)
|
||||
* @param {string} function that returns the plugin definition
|
||||
* @returns {boolean} true if successful
|
||||
*/
|
||||
plugin(type: string, maker: any): boolean;
|
||||
|
||||
/**
|
||||
* Unload the plugin associated with a `type` and a `name`.
|
||||
*
|
||||
* @param {string} type of plugin ("core", "io", "arch", ...)
|
||||
* @param {string} name of the plugin
|
||||
* @returns {boolean} true if successful
|
||||
*/
|
||||
unload(type: string, name: string): boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic interface to interact with radare2, abstracts the access to the associated
|
||||
* instance of the tool, which could be native via rlang or remote via pipes or tcp/http.
|
||||
*
|
||||
* @typedef R2PipeSync
|
||||
*/
|
||||
export interface R2PipeSync {
|
||||
/**
|
||||
* Run a command in the associated instance of radare2
|
||||
*
|
||||
* @param {string} command to be executed inside radare2.
|
||||
* @returns {string} The output of the command execution
|
||||
*/
|
||||
cmd(cmd: string): string;
|
||||
|
||||
/**
|
||||
* Run a radare2 command in a different address. Same as `.cmd(x + '@ ' + a)`
|
||||
*
|
||||
* @param {string} command to be executed inside radare2.
|
||||
* @param {number|string|NativePointer} command to be executed inside radare2.
|
||||
* @returns {string} The output of the command execution
|
||||
*/
|
||||
cmdAt(cmd: string, address: number | string | any): string;
|
||||
|
||||
/**
|
||||
* Run a radare2 command expecting the output to be JSON
|
||||
*
|
||||
* @param {string} command to be executed inside radare2. The given command should end with `j`
|
||||
* @returns {object} the JSON decoded object from the output of the command
|
||||
*/
|
||||
cmdj(cmd: string): any;
|
||||
|
||||
/**
|
||||
* Call a radare2 command. This is similar to `R2Pipe.cmd`, but skips all the command parsing rules,
|
||||
* which is safer and faster but you cannot use any special modifier like `@`, `~`, ...
|
||||
*
|
||||
* See R2Pipe.callAt() to call a command on a different address
|
||||
*
|
||||
* @param {string} command to be executed inside radare2. The given command should end with `j`
|
||||
* @returns {object} the JSON decoded object from the output of the command
|
||||
*/
|
||||
call(cmd: string): string;
|
||||
|
||||
/**
|
||||
* Call a radare2 command in a different address
|
||||
*
|
||||
* @param {string} command to be executed inside radare2. The given command should end with `j`
|
||||
* @param {NativePointer|string|number} where to seek to execute this command (previous offset is restored after executing it)
|
||||
* @returns {string} the string containing the output of the command
|
||||
*/
|
||||
callAt(cmd: string, address: string | number | any): string;
|
||||
|
||||
/**
|
||||
* Same as cmdj but using .call which avoids command injection problems
|
||||
*
|
||||
* @param {string} command to be executed inside radare2. The given command should end with `j`
|
||||
* @returns {object} the JSON decoded object from the command output
|
||||
*/
|
||||
callj(cmd: string): any;
|
||||
|
||||
/**
|
||||
* Log a string to the associated console. This is used internally by `console.log` in some implementations.
|
||||
*
|
||||
* @param {string} text to be displayed
|
||||
*/
|
||||
log(msg: string): void;
|
||||
|
||||
/**
|
||||
* Instantiate a new radare2 plugin with the given type and constructor method.
|
||||
*
|
||||
* @param {string} type of plugin ("core", "io", "arch", ...)
|
||||
* @param {string} function that returns the plugin definition
|
||||
* @returns {boolean} true if successful
|
||||
*/
|
||||
plugin(type: string, maker: any): boolean;
|
||||
|
||||
/**
|
||||
* Unload the plugin associated with a `type` and a `name`.
|
||||
*
|
||||
* @param {string} type of plugin ("core", "io", "arch", ...)
|
||||
* @param {string} name of the plugin
|
||||
* @returns {boolean} true if successful
|
||||
*/
|
||||
unload(type: string, name: string): boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* A global instance of R2Pipe associated with the current instance of radare2
|
||||
*
|
||||
* @type {R2PipeSync}
|
||||
*/
|
||||
export declare const r2: R2Pipe;
|
||||
@@ -1,26 +0,0 @@
|
||||
import { AppState } from "./types";
|
||||
import { DEFAULT_PROMPT } from "./constants";
|
||||
|
||||
export const state: AppState = {
|
||||
decopipe: { use: false },
|
||||
host: "http://localhost",
|
||||
port: "11434",
|
||||
baseurl: "",
|
||||
api: "ollama",
|
||||
pipeline: "",
|
||||
commands: "pdc",
|
||||
yolo: false,
|
||||
tts: false,
|
||||
language: "C",
|
||||
humanLanguage: "English",
|
||||
deterministic: true,
|
||||
debug: false,
|
||||
think: -1,
|
||||
useFiles: false,
|
||||
contextFile: "",
|
||||
model: "",
|
||||
cache: false,
|
||||
maxInputTokens: -1,
|
||||
prompt: DEFAULT_PROMPT,
|
||||
lastOutput: "",
|
||||
};
|
||||
@@ -1,16 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2020",
|
||||
"lib": ["esnext"],
|
||||
"module": "commonjs",
|
||||
"outDir": "out",
|
||||
"sourceMap": true,
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"include": [
|
||||
"**/*.ts"
|
||||
]
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
import type { R2PipeSync } from "r2papi";
|
||||
|
||||
declare global {
|
||||
const r2: R2PipeSync;
|
||||
function btoa(str: string): string;
|
||||
}
|
||||
|
||||
export type ApiStyle = "openai" | "anthropic" | "ollama" | "gemini";
|
||||
|
||||
export interface ProviderConfig {
|
||||
defaultModel: string;
|
||||
defaultBaseurl: string;
|
||||
requiresAuth: boolean;
|
||||
authKey?: string;
|
||||
apiStyle: ApiStyle;
|
||||
hardcodedModels?: string[];
|
||||
listModelsCallback?: (provider: ProviderConfig) => string;
|
||||
}
|
||||
|
||||
export interface ProviderRegistry {
|
||||
[key: string]: ProviderConfig;
|
||||
}
|
||||
|
||||
export interface DecoPipeConfig {
|
||||
use: boolean;
|
||||
default?: string;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export interface AppState {
|
||||
decopipe: DecoPipeConfig;
|
||||
host: string;
|
||||
port: string;
|
||||
baseurl: string;
|
||||
api: string;
|
||||
pipeline: string;
|
||||
commands: string;
|
||||
yolo: boolean;
|
||||
tts: boolean;
|
||||
language: string;
|
||||
humanLanguage: string;
|
||||
deterministic: boolean;
|
||||
debug: boolean;
|
||||
think: number;
|
||||
useFiles: boolean;
|
||||
contextFile: string;
|
||||
model: string;
|
||||
cache: boolean;
|
||||
maxInputTokens: number;
|
||||
prompt: string;
|
||||
lastOutput: string;
|
||||
}
|
||||
|
||||
export interface ConfigHandler {
|
||||
get: () => unknown;
|
||||
set: (v: string) => void;
|
||||
}
|
||||
|
||||
export interface ConfigHandlers {
|
||||
[key: string]: ConfigHandler;
|
||||
}
|
||||
|
||||
export type ApiKeyResult = [string | null, string | null, string];
|
||||
|
||||
export interface HttpResponse {
|
||||
error?: { message?: string } | string;
|
||||
choices?: Array<{ message: { content: string } }>;
|
||||
content?: Array<{ text: string }>;
|
||||
message?: { content: string };
|
||||
candidates?: Array<{ content: { parts: Array<{ text: string }> } }>;
|
||||
data?: Array<
|
||||
{
|
||||
id: string;
|
||||
name?: string;
|
||||
max_context_length?: number;
|
||||
description?: string;
|
||||
}
|
||||
>;
|
||||
models?: Array<{ name: string }>;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export type PayloadBuilder = (
|
||||
model: string,
|
||||
query: string,
|
||||
provider: ProviderConfig,
|
||||
) => Record<string, unknown>;
|
||||
export type ResponseParser = (res: HttpResponse) => string;
|
||||
export type UrlBuilder = (base: string, model: string, key?: string) => string;
|
||||
export type HeadersBuilder = (
|
||||
key: string | null,
|
||||
provider: ProviderConfig,
|
||||
) => string[];
|
||||
|
||||
export interface AutoReply {
|
||||
action: string;
|
||||
command?: string;
|
||||
description?: string;
|
||||
reason?: string;
|
||||
response?: string;
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
import { state } from "./state";
|
||||
|
||||
export function tmpdir(path: string): string {
|
||||
const dir = r2.cmd("-e dir.tmp").trim() ?? ".";
|
||||
return dir + "/" + path;
|
||||
}
|
||||
|
||||
export function fileExists(path: string): boolean {
|
||||
if (r2.cmd2("test -h").logs[0].message.indexOf("-fdx") !== -1) {
|
||||
return true; // r2 is old
|
||||
}
|
||||
return r2.cmd("'test -vf " + path).startsWith("found");
|
||||
}
|
||||
|
||||
export function padRight(str: string, length: number): string {
|
||||
return str + " ".repeat(Math.max(0, length - str.length));
|
||||
}
|
||||
|
||||
export function trimAnsi(str: string): string {
|
||||
return str.replace(/\x1b\[[0-9;]*m/g, "");
|
||||
}
|
||||
|
||||
export function trimDown(out: string): string {
|
||||
const jsonMatch = out.match(/```json\s*([\s\S]*?)```/);
|
||||
return jsonMatch && jsonMatch[1] ? jsonMatch[1].trim() : out;
|
||||
}
|
||||
|
||||
export function trimJson(out: string): string {
|
||||
let result = out;
|
||||
const bob = result.indexOf("{");
|
||||
if (bob !== -1) result = result.slice(bob);
|
||||
const eob = result.indexOf("}");
|
||||
if (eob !== -1) result = result.slice(0, eob + 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
export function b64(str: string): string {
|
||||
return btoa(str);
|
||||
}
|
||||
|
||||
export function fileDump(fileName: string, fileData: string): void {
|
||||
const d = b64(fileData);
|
||||
r2.cmd("p6ds " + d + " > " + fileName);
|
||||
}
|
||||
|
||||
export function filterResponse(msg: string): string {
|
||||
let result = msg;
|
||||
if (state.think !== 2) {
|
||||
result = result.replace(/<think>[\s\S]*?<\/think>/gi, "");
|
||||
}
|
||||
return result
|
||||
.split("\n")
|
||||
.filter((line) => !line.trim().startsWith("```"))
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
export function debugLog(msg: string): void {
|
||||
if (state.debug) {
|
||||
console.log(msg);
|
||||
}
|
||||
}
|
||||
|
||||
export function parseEnvLikeString(input: string): Record<string, string> {
|
||||
const result: Record<string, string> = {};
|
||||
for (const line of input.split(/\r?\n/)) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed || trimmed.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
const [key, ...rest] = trimmed.split("=");
|
||||
if (!key || rest.length === 0) {
|
||||
continue;
|
||||
}
|
||||
const value = rest.join("=").trim();
|
||||
// Normalize key: strip trailing _API_KEY (case-insensitive)
|
||||
const normKey = key.toLowerCase().replace(/_api_key$/i, "");
|
||||
result[normKey] = value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -1,130 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<!-- Created with Inkscape (http://www.inkscape.org/) -->
|
||||
|
||||
<svg
|
||||
width="210mm"
|
||||
height="297mm"
|
||||
viewBox="0 0 210 297"
|
||||
version="1.1"
|
||||
id="svg1"
|
||||
inkscape:version="1.4.2 (ebf0e940, 2025-05-08)"
|
||||
sodipodi:docname="decai.svg"
|
||||
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
|
||||
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
xmlns:svg="http://www.w3.org/2000/svg">
|
||||
<sodipodi:namedview
|
||||
id="namedview1"
|
||||
pagecolor="#ffffff"
|
||||
bordercolor="#000000"
|
||||
borderopacity="0.25"
|
||||
inkscape:showpageshadow="2"
|
||||
inkscape:pageopacity="0.0"
|
||||
inkscape:pagecheckerboard="0"
|
||||
inkscape:deskcolor="#d1d1d1"
|
||||
inkscape:document-units="mm"
|
||||
inkscape:zoom="1.0482005"
|
||||
inkscape:cx="318.64133"
|
||||
inkscape:cy="426.92214"
|
||||
inkscape:window-width="1512"
|
||||
inkscape:window-height="960"
|
||||
inkscape:window-x="222"
|
||||
inkscape:window-y="44"
|
||||
inkscape:window-maximized="0"
|
||||
inkscape:current-layer="layer1" />
|
||||
<defs
|
||||
id="defs1" />
|
||||
<g
|
||||
inkscape:label="Layer 1"
|
||||
inkscape:groupmode="layer"
|
||||
id="layer1">
|
||||
<rect
|
||||
style="fill:#000000;fill-opacity:1;stroke:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke-opacity:1"
|
||||
id="rect2"
|
||||
width="186.28354"
|
||||
height="176.18687"
|
||||
x="1.5145004"
|
||||
y="23.979588"
|
||||
ry="0.047831953" />
|
||||
<path
|
||||
sodipodi:type="star"
|
||||
style="fill:#1a1a1a;stroke:#000000;stroke-width:2;stroke-linecap:round;stroke-linejoin:round"
|
||||
id="path1"
|
||||
inkscape:flatsided="false"
|
||||
sodipodi:sides="10"
|
||||
sodipodi:cx="71.751205"
|
||||
sodipodi:cy="102.09375"
|
||||
sodipodi:r1="53.804771"
|
||||
sodipodi:r2="49.845222"
|
||||
sodipodi:arg1="0.65927727"
|
||||
sodipodi:arg2="0.97343657"
|
||||
inkscape:rounded="0"
|
||||
inkscape:randomized="0"
|
||||
d="m 114.28039,135.05163 -14.49317,8.25532 -13.001341,10.44831 -16.577574,-1.84017 -16.659669,0.81086 -12.329911,-11.23279 -13.95457,-9.1363 -3.37264,-16.33486 -5.919299,-15.59371 6.872864,-15.197565 4.376943,-16.094859 14.493168,-8.255319 13.001341,-10.448311 16.577574,1.840177 16.659669,-0.810865 12.329915,11.232788 13.95457,9.136304 3.37264,16.334856 5.91929,15.593714 -6.87286,15.19757 z"
|
||||
transform="matrix(1.2188073,0,0,1.2188073,1.600691,-15.424646)" />
|
||||
<path
|
||||
sodipodi:type="star"
|
||||
style="fill:#37662e;fill-opacity:1;stroke:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round"
|
||||
id="path1-5"
|
||||
inkscape:flatsided="false"
|
||||
sodipodi:sides="10"
|
||||
sodipodi:cx="71.751205"
|
||||
sodipodi:cy="102.09375"
|
||||
sodipodi:r1="35.902484"
|
||||
sodipodi:r2="49.845222"
|
||||
sodipodi:arg1="0.32368816"
|
||||
sodipodi:arg2="0.63784749"
|
||||
inkscape:rounded="0"
|
||||
inkscape:randomized="0"
|
||||
d="m 105.78923,113.51309 6.0066,18.26187 -19.219401,-0.43572 -5.874621,18.30475 L 71.4091,137.9946 55.897164,149.35045 50.372443,130.93707 31.148226,131.00645 37.501747,112.86236 21.908246,101.61878 37.71318,90.674414 31.706582,72.412538 l 19.2194,0.435725 5.874621,-18.304757 15.292708,11.649389 15.511935,-11.355844 5.524722,18.413382 19.224212,-0.06938 -6.35352,18.144089 15.5935,11.243582 z"
|
||||
transform="matrix(1.2188073,0,0,1.2188073,0.97362416,-14.941195)" />
|
||||
<path
|
||||
sodipodi:type="star"
|
||||
style="fill:#800000;stroke:#000000;stroke-width:2;stroke-linecap:round;stroke-linejoin:round"
|
||||
id="path2"
|
||||
inkscape:flatsided="false"
|
||||
sodipodi:sides="10"
|
||||
sodipodi:cx="69.252403"
|
||||
sodipodi:cy="183.84013"
|
||||
sodipodi:r1="76.18972"
|
||||
sodipodi:r2="39.770834"
|
||||
sodipodi:arg1="0.94510027"
|
||||
sodipodi:arg2="1.2659605"
|
||||
inkscape:rounded="0.029345222"
|
||||
inkscape:randomized="0"
|
||||
d="m 113.8738,245.59614 c -0.96661,0.6886 -31.555039,-24.18254 -32.684714,-23.81876 -1.120974,0.36099 -10.958876,38.2632 -12.136488,38.25221 -1.186753,-0.0111 -11.314419,-38.11167 -12.442172,-38.48137 -1.119068,-0.36685 -31.356457,24.51411 -32.302709,23.81304 -0.953595,-0.70651 13.247923,-37.48344 12.552854,-38.44541 -0.689714,-0.95456 -39.7769375,1.40147 -40.1303916,0.27809 -0.3561976,-1.13209 32.7500076,-22.5378 32.7531166,-23.7246 0.0031,-1.17766 -33.0039804,-22.24649 -32.6296302,-23.36307 0.3772559,-1.12525 39.7427032,1.0165 40.4428022,0.0582 0.694707,-0.95093 -13.624624,-37.39704 -12.665459,-38.08034 0.966611,-0.68859 31.555037,24.18255 32.684712,23.81876 1.120974,-0.36098 10.958875,-38.26319 12.136488,-38.2522 1.186752,0.0111 11.314419,38.11167 12.442172,38.48137 1.119067,0.36685 31.356459,-24.51411 32.302709,-23.81304 0.95359,0.70651 -13.24792,37.48344 -12.55285,38.44541 0.68971,0.95456 39.77693,-1.40147 40.13039,-0.2781 0.35619,1.13209 -32.75001,22.53781 -32.75312,23.72461 -0.003,1.17766 33.00398,22.24649 32.62963,23.36307 -0.37726,1.12524 -39.7427,-1.01651 -40.4428,-0.0582 -0.69471,0.95093 13.62462,37.39704 12.66546,38.08033 z"
|
||||
transform="translate(19.276442,-75.320913)" />
|
||||
<path
|
||||
sodipodi:type="star"
|
||||
style="fill:#d45500;stroke:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke-opacity:1"
|
||||
id="path2-0"
|
||||
inkscape:flatsided="false"
|
||||
sodipodi:sides="10"
|
||||
sodipodi:cx="69.252403"
|
||||
sodipodi:cy="183.84013"
|
||||
sodipodi:r1="3.2911112"
|
||||
sodipodi:r2="71.359137"
|
||||
sodipodi:arg1="1.3521275"
|
||||
sodipodi:arg2="1.5760036"
|
||||
inkscape:rounded="0.029345222"
|
||||
inkscape:randomized="0"
|
||||
d="m 69.966345,187.05287 c -1.9053,0.60813 0.905346,68.33625 -1.085527,68.14543 -1.996473,-0.19135 0.964985,-67.70967 -0.939225,-68.33936 -1.898868,-0.62792 -39.434599,55.81734 -40.933089,54.49275 -1.502705,-1.3283 40.579437,-54.21107 39.409022,-55.83977 -1.167132,-1.62412 -64.7118681,21.9781 -65.1456023,20.0257 -0.43495449,-1.95789 64.6939243,-20.00568 64.7043623,-22.01128 0.01041,-1.99997 -65.27140303,-20.25603 -64.4747102,-22.09049 0.7989341,-1.83963 64.0975312,21.84119 65.2848352,20.22477 1.183973,-1.61189 -40.89948,-54.75304 -39.17667,-55.76887 1.727657,-1.01869 39.01806,55.34548 40.92872,54.73564 1.905301,-0.60812 -0.905346,-68.33625 1.085527,-68.14543 1.996474,0.19136 -0.964984,67.70968 0.939225,68.33937 1.898868,0.62792 39.434597,-55.81734 40.933087,-54.49276 1.50271,1.32831 -40.579435,54.21108 -39.409019,55.83977 1.167131,1.62413 64.711869,-21.97809 65.145599,-20.0257 0.43496,1.9579 -64.693921,20.00569 -64.704359,22.01129 -0.01041,1.99997 65.271399,20.25602 64.474709,22.09049 -0.79893,1.83963 -64.09753,-21.8412 -65.284834,-20.22477 -1.183973,1.61189 40.899484,54.75304 39.176674,55.76887 -1.72766,1.01869 -39.018064,-55.34548 -40.928725,-54.73565 z"
|
||||
transform="translate(19.25264,-74.484719)" />
|
||||
<path
|
||||
sodipodi:type="star"
|
||||
style="fill:#ffb380;stroke:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke-opacity:1"
|
||||
id="path2-0-7"
|
||||
inkscape:flatsided="false"
|
||||
sodipodi:sides="10"
|
||||
sodipodi:cx="69.252403"
|
||||
sodipodi:cy="183.84013"
|
||||
sodipodi:r1="2.0329688"
|
||||
sodipodi:r2="36.653175"
|
||||
sodipodi:arg1="1.3051581"
|
||||
sodipodi:arg2="1.5885803"
|
||||
inkscape:rounded="0.029345222"
|
||||
inkscape:randomized="0"
|
||||
d="m 69.786109,185.8018 c -0.974051,0.29744 -0.168255,34.73512 -1.18551,34.68571 -1.01845,-0.0495 0.894533,-34.41437 -0.06946,-34.74665 -0.962858,-0.3319 -20.552916,28.00241 -21.346851,27.36451 -0.794867,-0.63865 20.951954,-27.31602 20.367382,-28.15147 -0.583886,-0.83446 -33.087061,10.57373 -33.354419,9.59099 -0.267673,-0.98389 33.006439,-9.78387 33.024571,-10.80336 0.01811,-1.01829 -32.983073,-10.89376 -32.621734,-11.84596 0.361764,-0.95332 32.453587,11.48538 33.067497,10.67125 0.61319,-0.81317 -20.280673,-28.2002 -19.428655,-28.75816 0.853019,-0.55861 19.504568,28.36761 20.479763,28.06981 0.974051,-0.29744 0.168255,-34.73512 1.18551,-34.68572 1.01845,0.0495 -0.894534,34.41438 0.06946,34.74666 0.962858,0.3319 20.552915,-28.00241 21.34685,-27.36451 0.794868,0.63865 -20.951953,27.31602 -20.367382,28.15146 0.583886,0.83446 33.087059,-10.57372 33.354419,-9.59099 0.26767,0.98389 -33.006439,9.78388 -33.024571,10.80337 -0.01811,1.01829 32.983071,10.89376 32.621731,11.84596 -0.36176,0.95332 -32.453584,-11.48538 -33.067494,-10.67125 -0.613189,0.81317 20.280674,28.2002 19.428655,28.75816 C 89.412852,214.43022 70.761304,185.504 69.786109,185.8018 Z"
|
||||
transform="rotate(-17.984043,-157.18948,86.992365)" />
|
||||
</g>
|
||||
</svg>
|
||||
|
Before Width: | Height: | Size: 8.3 KiB |
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,41 @@
|
||||
SHELL=/bin/bash
|
||||
|
||||
PROVIDER=ollama
|
||||
MODELS=granite3-dense:8b mannix/jan-nano:latest yi-coder:latest qwen3:latest deepseek-coder-v2:latest hhao/qwen2.5-coder-tools:latest qwen2.5-coder:latest
|
||||
MODELS=gemma3:1b
|
||||
LANGS=Dart Perl Ruby Javascript PHP C
|
||||
BINARIES=files/complex1 files/complex2 files/complex3
|
||||
MODELS=gpt-4o
|
||||
PROVIDER=openai
|
||||
|
||||
all:
|
||||
mkdir -p tmp
|
||||
for b in $(BINARIES) ; do \
|
||||
for a in $(MODELS) ; do \
|
||||
echo "$$b $$a"; \
|
||||
./run.sh $$b $$a ${PROVIDER} ${LANGS} > tmp/$${b##*/}_$${a//\//_}.html ; \
|
||||
done ; \
|
||||
done
|
||||
|
||||
.PHONY: run index evaluate
|
||||
|
||||
compile: $(BINARIES)
|
||||
|
||||
files/complex1: files/complex1.c
|
||||
gcc -o $@ $<
|
||||
|
||||
files/complex2: files/complex2.c
|
||||
gcc -o $@ $<
|
||||
|
||||
files/complex3: files/complex3.c
|
||||
gcc -o $@ $<
|
||||
|
||||
lmstudio:
|
||||
for b in $(BINARIES) ; do \
|
||||
for a in $(MODELS) ; do \
|
||||
./run.sh $$b $$a lmstudio ${LANGS} > $${b##*/}_$${a//\//_}.html ; \
|
||||
done ; \
|
||||
done
|
||||
|
||||
clean:
|
||||
rm -rf tmp
|
||||
File renamed without changes.
Executable
+59
@@ -0,0 +1,59 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Evaluate decompiled outputs using mai and determine best model+lang
|
||||
|
||||
echo "Evaluating decompiled outputs..."
|
||||
|
||||
tmpfile=$(mktemp)
|
||||
MAI="mai -q -p openai -m gpt-4o"
|
||||
# MAI="mai -q"
|
||||
|
||||
for orig in files/complex1.c files/complex2.c files/complex3.c; do
|
||||
base=$(basename $orig .c)
|
||||
original_code=$(cat $orig)
|
||||
for f in tmp/${base}_*.txt; do
|
||||
if [ -f "$f" ]; then
|
||||
decompiled_code=$(cat $f)
|
||||
#echo "Comparing $orig $f"
|
||||
result=`echo n | ${MAI} -q -r "/template files/CMPROMPT.txt original=@${orig} decompiled=@${f}"`
|
||||
score=`echo "$result" | head -n 1 | grep -oE '[0-9]+'` # |head -n 1`
|
||||
perfect=`echo "$result" | grep 'No sign' && echo yes`
|
||||
[ -z "$score" ] && score=0
|
||||
if [ "$perfect" = yes ]; then
|
||||
score=100
|
||||
else
|
||||
score="$((0+${score}))"
|
||||
fi
|
||||
echo "$score\t$f"
|
||||
if [ -n "$score" ]; then
|
||||
key="${f%.*}" # remove .txt
|
||||
# Check if key exists in tmpfile
|
||||
existing=$(grep "^$key " $tmpfile | awk '{print $2}')
|
||||
if [ -n "$existing" ]; then
|
||||
new_score=$(( existing + score ))
|
||||
sed -i "s/^$key .*/$key $new_score/" $tmpfile
|
||||
else
|
||||
echo "$key $score" >> $tmpfile
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
done
|
||||
done
|
||||
|
||||
# Find the best (highest total score)
|
||||
best_key=""
|
||||
best_score=0
|
||||
while read key score; do
|
||||
if [ "$score" -gt "$best_score" ]; then
|
||||
best_score=$score
|
||||
best_key=$key
|
||||
fi
|
||||
done < $tmpfile
|
||||
|
||||
rm $tmpfile
|
||||
|
||||
if [ -n "$best_key" ]; then
|
||||
echo "Best model+language combination: $best_key with total score $best_score"
|
||||
else
|
||||
echo "No scores found"
|
||||
fi
|
||||
@@ -0,0 +1,17 @@
|
||||
Compare the two provided code in C with the decompiled counterpart. Ignore function or variable names, syntax differences, language-specific constructs, and formatting. Focus solely on what the code does: the operations performed, control flow, logic, and functional behavior. Determine if they are functionally equivalent in terms of their effects and computations.
|
||||
|
||||
Output only:
|
||||
- A numeric score from 1 to 100 (100 being identical logic and functionality)
|
||||
- Followed by a single sentence describing the most relevant semantic differences (or "No significant semantic differences" if score is 95+)
|
||||
|
||||
**Original Source Code:**
|
||||
|
||||
```C
|
||||
{original}
|
||||
```
|
||||
|
||||
**Decompiled Code:**
|
||||
|
||||
```
|
||||
{decompiled}
|
||||
```
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main() {
|
||||
int n = 5;
|
||||
int *arr = (int *)malloc(n * sizeof(int));
|
||||
if (arr == NULL) return 1;
|
||||
for (int i = 0; i < n; i++) {
|
||||
arr[i] = i * i;
|
||||
}
|
||||
int sum = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
sum += arr[i];
|
||||
for (int j = 0; j < i; j++) {
|
||||
sum -= j;
|
||||
}
|
||||
}
|
||||
printf("Sum: %d\n", sum);
|
||||
free(arr);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct Node {
|
||||
int data;
|
||||
struct Node *next;
|
||||
};
|
||||
|
||||
int main() {
|
||||
struct Node *head = NULL;
|
||||
struct Node *tail = NULL;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
|
||||
if (newNode == NULL) return 1;
|
||||
newNode->data = i * 2;
|
||||
newNode->next = NULL;
|
||||
if (head == NULL) {
|
||||
head = newNode;
|
||||
tail = newNode;
|
||||
} else {
|
||||
tail->next = newNode;
|
||||
tail = newNode;
|
||||
}
|
||||
}
|
||||
struct Node *current = head;
|
||||
int sum = 0;
|
||||
while (current != NULL) {
|
||||
sum += current->data;
|
||||
current = current->next;
|
||||
}
|
||||
printf("Sum: %d\n", sum);
|
||||
current = head;
|
||||
while (current != NULL) {
|
||||
struct Node *temp = current;
|
||||
current = current->next;
|
||||
free(temp);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int x = 42;
|
||||
int y = 0;
|
||||
switch (x & 15) {
|
||||
case 0:
|
||||
y = x << 1;
|
||||
break;
|
||||
case 1:
|
||||
y = x >> 1;
|
||||
break;
|
||||
case 2:
|
||||
y = x ^ 255;
|
||||
break;
|
||||
case 3:
|
||||
y = ~x;
|
||||
break;
|
||||
default:
|
||||
y = x | 128;
|
||||
break;
|
||||
}
|
||||
if ((y & 1) == 0) {
|
||||
y += 10;
|
||||
} else {
|
||||
y -= 5;
|
||||
}
|
||||
for (int i = 0; i < 5; i++) {
|
||||
y = (y << 2) | (y >> 30);
|
||||
}
|
||||
printf("Result: %d\n", y);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
#!/bin/bash
|
||||
|
||||
cp decai/BENCH.txt tmp/BENCH.txt
|
||||
# Find all directories containing BENCH.txt
|
||||
dirs=$(find . -name "BENCH.txt" -type f | xargs dirname | sort | uniq)
|
||||
|
||||
@@ -46,4 +47,4 @@ cat <<EOF >> benchmark_index.html
|
||||
<br />
|
||||
<br />
|
||||
</html>
|
||||
EOF
|
||||
EOF
|
||||
@@ -1,12 +1,55 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Generate HTML output for command executions with collapsible sections and width control
|
||||
# If PLAIN=1, output plain text instead
|
||||
|
||||
if [ $# -eq 0 ]; then
|
||||
echo "Usage: $0 command1 [command2 ...]"
|
||||
if [ $# -lt 4 ]; then
|
||||
echo "Usage: $0 <binary> <model> <provider> <lang1> [lang2 ...]"
|
||||
echo "Or: $0 command1 [command2 ...] (legacy)"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if first arg is a file (binary), assume new format
|
||||
if [ -f "$1" ]; then
|
||||
binary="$1"
|
||||
model="$2"
|
||||
provider="$3"
|
||||
shift 3
|
||||
langs=("$@")
|
||||
commands=()
|
||||
for lang in "${langs[@]}"; do
|
||||
# commands+=("# $lang echo \"decai -e model=$model;decai -e lang=$lang;decai -e api=$provider;af;decai -d\" | r2 -q $binary 2> /dev/null")
|
||||
commands+=("# $lang r2 -e scr.color=0 -qcq -qc \"decai -e model=$model;decai -e lang=$lang;decai -e api=$provider;af;decai -d\" $binary")
|
||||
done
|
||||
set -- "${commands[@]}"
|
||||
else
|
||||
# Legacy mode
|
||||
commands=("$@")
|
||||
fi
|
||||
|
||||
if [ "$PLAIN" = "1" ]; then
|
||||
# In plain mode, execute each command and print raw output.
|
||||
# Also, if TXTFILE is set, save combined raw output there.
|
||||
tmp_plain=$(mktemp)
|
||||
for cmd in "$@"; do
|
||||
if [[ "$cmd" =~ ^#[[:space:]]*(.*) ]]; then
|
||||
exec_cmd="${BASH_REMATCH[1]}"
|
||||
else
|
||||
exec_cmd="$cmd"
|
||||
fi
|
||||
tmpout=$(mktemp)
|
||||
eval "$exec_cmd" > "$tmpout" 2>&1 || true
|
||||
mv "$tmpout" "$tmp_plain"
|
||||
rm -f "$tmpout"
|
||||
done
|
||||
if [ -n "$TXTFILE" ]; then
|
||||
mv "$tmp_plain" "$TXTFILE"
|
||||
else
|
||||
rm -f "$tmp_plain"
|
||||
fi
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Timeout in seconds for each command (configurable via TIMEOUT environment variable)
|
||||
TIMEOUT=${TIMEOUT:-30}
|
||||
|
||||
@@ -109,24 +152,27 @@ EOF
|
||||
|
||||
# Process each command
|
||||
for cmd in "$@"; do
|
||||
# Extract title: if command starts with #, use the rest as title, else use the command
|
||||
if [[ "$cmd" =~ ^#[[:space:]]*(.*) ]]; then
|
||||
# Extract title: if command starts with #, parse title and command, else use the command
|
||||
if [[ "$cmd" =~ ^#[[:space:]]*([^[:space:]]+)[[:space:]]*(.*) ]]; then
|
||||
title="${BASH_REMATCH[1]}"
|
||||
exec_cmd="${cmd#*#}"
|
||||
exec_cmd="${exec_cmd#"${exec_cmd%%[![:space:]]*}"}" # trim leading spaces
|
||||
exec_cmd="${BASH_REMATCH[2]}"
|
||||
else
|
||||
title="$cmd"
|
||||
exec_cmd="$cmd"
|
||||
fi
|
||||
|
||||
echo "RUN $exec_cmd" > /dev/stderr
|
||||
# Execute command and time it
|
||||
start_time=$(date +%s.%N)
|
||||
tmpfile=$(mktemp)
|
||||
eval "$exec_cmd" > "$tmpfile" 2>&1 &
|
||||
# echo "eval $exec_cmd > $tmpfile" > /dev/stder
|
||||
eval "$exec_cmd" > "$tmpfile" 2> /dev/null &
|
||||
pid=$!
|
||||
(sleep $TIMEOUT && kill $pid 2>/dev/null) &
|
||||
wait $pid 2>/dev/null
|
||||
output=$(cat "$tmpfile")
|
||||
#echo "$output" > /dev/stderr
|
||||
|
||||
rm "$tmpfile"
|
||||
exit_code=$?
|
||||
end_time=$(date +%s.%N)
|
||||
@@ -139,8 +185,16 @@ for cmd in "$@"; do
|
||||
execution_time=$(printf "%.3f" "$execution_time")
|
||||
fi
|
||||
|
||||
# Save plain output to .txt file if in new format
|
||||
if [ -n "$binary" ]; then
|
||||
txtfile="tmp/${binary##*/}_${model//\//_}_${title}.txt"
|
||||
mkdir -p "$(dirname "$txtfile")" 2>/dev/null || true
|
||||
echo "$output" > "$txtfile"
|
||||
fi
|
||||
|
||||
# Escape HTML in output
|
||||
escaped_output=$(echo "$output" | sed 's/&/\&/g; s/</\</g; s/>/\>/g')
|
||||
#escaped_output=$(echo "$output" | sed 's/&/\&/g; s/</\</g; s/>/\>/g')
|
||||
escaped_output="$output"
|
||||
|
||||
cat << EOF
|
||||
<div class="command-section expanded">
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,43 +0,0 @@
|
||||
# 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
|
||||
@@ -1,8 +0,0 @@
|
||||
---
|
||||
description: 'Automatically suggest a better name for this function'
|
||||
version: 0.1
|
||||
author: pancake
|
||||
command: afv;r2ai -d
|
||||
if-empty: exit
|
||||
---
|
||||
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:
|
||||
@@ -1,8 +0,0 @@
|
||||
---
|
||||
description: 'Compact conversation history into a summary'
|
||||
version: 0.1
|
||||
author: pancake
|
||||
command: r2ai -C
|
||||
if-empty: exit
|
||||
---
|
||||
Create a compact summary of this conversation that preserves all key information, insights, and context for future reference. Focus on essential details about the binary analysis, tools used, findings, and any important conclusions.
|
||||
@@ -1,8 +0,0 @@
|
||||
---
|
||||
description: 'Augmented decompilation based on LLM'
|
||||
version: 0.1
|
||||
author: pancake
|
||||
command: pdc
|
||||
if-empty: exit
|
||||
---
|
||||
$(-e r2ai.prompt). Translate the code into $(-e r2ai.lang) programming language.
|
||||
@@ -1,8 +0,0 @@
|
||||
---
|
||||
description: 'Find and explain devices used'
|
||||
version: 0.1
|
||||
author: pancake
|
||||
command: izqq~^/dev
|
||||
if-empty: exit
|
||||
---
|
||||
explain in one line the purpose of each of these devices
|
||||
@@ -1,8 +0,0 @@
|
||||
---
|
||||
description: 'List libraries loaded with dlopen'
|
||||
version: 0.1
|
||||
author: pancake
|
||||
command: iiq~dlopen
|
||||
if-empty: This binary is not using dlopen
|
||||
---
|
||||
Some libraries are loaded
|
||||
@@ -1,8 +0,0 @@
|
||||
---
|
||||
description: 'Explain the current function'
|
||||
version: 0.1
|
||||
author: pancake
|
||||
command: r2ai -d
|
||||
if-empty: exit
|
||||
---
|
||||
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)
|
||||
@@ -1,8 +0,0 @@
|
||||
---
|
||||
description: 'Group imports by Libraries'
|
||||
version: 0.1
|
||||
author: pancake
|
||||
command: iiq;il
|
||||
if-empty: exit
|
||||
---
|
||||
Group the imports that belong to each library and describe its purpose and usage scope
|
||||
@@ -1,8 +0,0 @@
|
||||
---
|
||||
description: 'Suggest an improved function signature'
|
||||
version: 0.1
|
||||
author: pancake
|
||||
command: ?e variables;afv;?e function name:;afd;?e current signature:?e afs
|
||||
if-empty: exit
|
||||
---
|
||||
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
|
||||
@@ -1,10 +0,0 @@
|
||||
---
|
||||
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.
|
||||
@@ -1,8 +0,0 @@
|
||||
---
|
||||
description: 'Better variable names'
|
||||
version: 0.1
|
||||
author: pancake
|
||||
command: afv
|
||||
if-empty: exit
|
||||
---
|
||||
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
|
||||
@@ -1,8 +0,0 @@
|
||||
---
|
||||
description: 'Find vulnerabilities or bugs in the current function'
|
||||
version: 0.1
|
||||
author: pancake
|
||||
command: pdc
|
||||
if-empty: exit
|
||||
---
|
||||
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.
|
||||
@@ -0,0 +1,40 @@
|
||||
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
|
||||
@@ -1,33 +0,0 @@
|
||||
# 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
|
||||
|
||||
## Coding Style
|
||||
|
||||
- `R_NEW` and `R_NEW0` calls never return NULL, so there's no need to check after calling them
|
||||
- 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`
|
||||
+12
-42
@@ -1,24 +1,13 @@
|
||||
DOTEXE=
|
||||
# DOTEXE=.exe
|
||||
DOTLIB=.$(shell r2 -NNH R2_LIBEXT)
|
||||
DOTLIB=.$(shell r2 -H R2_LIBEXT)
|
||||
|
||||
R2SYSPLUGDIR=$(shell r2 -NNH R2_LIBR_PLUGINS)
|
||||
R2USRPLUGDIR=$(shell r2 -NNH R2_USER_PLUGINS)
|
||||
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)
|
||||
R2BINDIR=$(shell r2pm -H R2PM_BINDIR)
|
||||
R2MANDIR=$(shell r2pm -H R2PM_MANDIR)
|
||||
ifeq ($(R2MANDIR),)
|
||||
R2MANDIR=$(shell r2 -NNH R2_PREFIX)/share/man
|
||||
endif
|
||||
|
||||
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
|
||||
@@ -42,20 +31,14 @@ ifeq ($(USE_LIBCURL),1)
|
||||
endif
|
||||
|
||||
OBJS=r2ai.o
|
||||
OBJS+=apikeys.o
|
||||
OBJS+=llm.o
|
||||
OBJS+=auto.o
|
||||
OBJS+=json.o
|
||||
OBJS+=vdb.o
|
||||
OBJS+=tools.o
|
||||
OBJS+=messages.o
|
||||
OBJS+=anthropic.o
|
||||
OBJS+=gemini.o
|
||||
OBJS+=http.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)
|
||||
@@ -71,37 +54,24 @@ doc-install:
|
||||
|
||||
vdb.o: vdb_embed.inc.c
|
||||
|
||||
fmt format indent:
|
||||
clang-format-radare2 *.c http/*.c
|
||||
indent:
|
||||
for a in *.c ; do python indent.py $$a ; done
|
||||
|
||||
user-install: user-uninstall
|
||||
mkdir -p $(R2USRPLUGDIR)
|
||||
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/
|
||||
cp -f r2ai$(DOTLIB) $(R2USRPLUGDIR)
|
||||
cp -f r2ai$(DOTEXE) $(R2BINDIR)
|
||||
|
||||
user-uninstall:
|
||||
rm -f $(R2USRPLUGDIR)/r2ai$(DOTLIB)
|
||||
rm -f $(R2BINDIR)/r2ai$(DOTEXE)
|
||||
rm -f $(R2MANDIR)/man1/r2ai.1
|
||||
|
||||
install:
|
||||
$(MAKE) uninstall
|
||||
mkdir -p $(R2USRPLUGDIR)
|
||||
mkdir -p $(R2BINDIR)
|
||||
cp -f r2ai$(DOTLIB) $(R2SYSPLUGDIR)/r2ai$(DOTLIB)
|
||||
cp -f r2ai$(DOTEXE) $(R2BINDIR)/r2ai$(DOTEXE)
|
||||
cp -f r2ai$(DOTLIB) $(R2SYSPLUGDIR)
|
||||
|
||||
uninstall:
|
||||
rm -f $(R2SYSPLUGDIR)/r2ai$(DOTLIB)
|
||||
rm -f $(R2BINDIR)/r2ai$(DOTEXE)
|
||||
|
||||
r2check:
|
||||
@r2 -NN -qcq --
|
||||
@r2 -qcq --
|
||||
clean:
|
||||
rm -f *.o *.d
|
||||
+18
-41
@@ -1,10 +1,12 @@
|
||||
# R2AI
|
||||
# C-R2AI
|
||||
|
||||
The r2ai plugin for radare2.
|
||||
This directory contains the C rewrite of the r2ai plugin for radare2.
|
||||
|
||||
## Installation
|
||||
|
||||
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`.
|
||||
Requires radare2 from git if you want to use the stream HTTP APIs.
|
||||
|
||||
Otherwise the only requirement is a C compiler.
|
||||
|
||||
```console
|
||||
make
|
||||
@@ -35,53 +37,24 @@ 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 (k(=v)) Same as '-e r2ai.'
|
||||
| r2ai -e 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 -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 -n suggest a better name for the current function
|
||||
| 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 [query] query the selected model+provider with the given query
|
||||
| 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
|
||||
[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>`
|
||||
@@ -98,11 +71,12 @@ These can be set with `r2ai -e <keyname>=<value>`
|
||||
| r2ai.hlang | Tells LLM in which language to speak |
|
||||
| r2ai.prompt | User prompt to send to LLM with `r2ai -d` |
|
||||
| r2ai.auto.max_runs | Maximum loops when using auto mode `r2ai -a` |
|
||||
| r2ai.auto.verbose | Only for auto mode `r2ai -a`. Will show the output of the tool which ran locally |
|
||||
| r2ai.auto.hide_tool_output | Only for auto mode `r2ai -a`. Will not show the output of the tool which ran locally |
|
||||
| r2ai.auto.yolo | Set this to true if you don't want r2ai to ask you for approval before running commands sent by the LLM. This is **dangerous**. Recommendation: **leave this to false** unless you fully trust your LLM not to create havoc! |
|
||||
| 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)
|
||||
@@ -241,6 +215,8 @@ Note: System prompt is applied automatically but not stored in history
|
||||
{"command":"pdf @ main"}
|
||||
```
|
||||
|
||||
|
||||
|
||||
## TODO
|
||||
|
||||
* add "undo" command to drop the last message
|
||||
@@ -249,6 +225,7 @@ 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`.
|
||||
@@ -291,4 +268,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
|
||||
+42
-49
@@ -1,15 +1,14 @@
|
||||
#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(RCorePluginSession *cps, R2AIArgs args) {
|
||||
RCore *core = cps->core;
|
||||
R_IPI R2AI_ChatResponse *r2ai_anthropic(RCore *core, R2AIArgs args) {
|
||||
const char *model = args.model;
|
||||
char **error = args.error;
|
||||
RList *tools = args.tools;
|
||||
RList *messages_input = args.messages;
|
||||
const R2AI_Tools *tools = args.tools;
|
||||
R2AI_Messages *messages_input = args.messages;
|
||||
|
||||
if (error) {
|
||||
*error = NULL;
|
||||
@@ -41,7 +40,7 @@ R_IPI R2AI_ChatResponse *r2ai_anthropic(RCorePluginSession *cps, R2AIArgs args)
|
||||
// Create messages JSON
|
||||
char *messages_json = NULL;
|
||||
|
||||
if (messages_input && !r_list_empty (messages_input)) {
|
||||
if (messages_input && messages_input->n_messages > 0) {
|
||||
// Convert directly to JSON without filtering
|
||||
messages_json = r2ai_msgs_to_anthropic_json (messages_input);
|
||||
if (!messages_json) {
|
||||
@@ -61,7 +60,7 @@ R_IPI R2AI_ChatResponse *r2ai_anthropic(RCorePluginSession *cps, R2AIArgs args)
|
||||
|
||||
// Convert tools to Anthropic format if available
|
||||
char *anthropic_tools_json = NULL;
|
||||
if (tools && !r_list_empty (tools)) {
|
||||
if (tools && tools->n_tools > 0) {
|
||||
anthropic_tools_json = r2ai_tools_to_anthropic_json (tools);
|
||||
}
|
||||
|
||||
@@ -99,26 +98,10 @@ R_IPI R2AI_ChatResponse *r2ai_anthropic(RCorePluginSession *cps, R2AIArgs args)
|
||||
free (messages_json);
|
||||
|
||||
// Save the full JSON for debugging
|
||||
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_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");
|
||||
R_LOG_DEBUG ("Anthropic API request data: %s", data);
|
||||
|
||||
if (r_config_get_b (core->config, "r2ai.debug")) {
|
||||
// Generate curl command for debugging
|
||||
RStrBuf *curl_cmd = r_strbuf_new ("curl -X POST");
|
||||
for (int i = 0; headers[i]; i++) {
|
||||
r_strbuf_appendf (curl_cmd, " -H \"%s\"", headers[i]);
|
||||
}
|
||||
r_strbuf_appendf (curl_cmd, " -d '%s' \"%s\"", data, anthropic_url);
|
||||
eprintf ("Curl command: %s\n", r_strbuf_get (curl_cmd));
|
||||
r_strbuf_free (curl_cmd);
|
||||
}
|
||||
|
||||
// Make the API call
|
||||
int code = 0;
|
||||
char *res = r2ai_http_post (core, anthropic_url, headers, data, &code, NULL);
|
||||
@@ -137,19 +120,19 @@ R_IPI R2AI_ChatResponse *r2ai_anthropic(RCorePluginSession *cps, R2AIArgs args)
|
||||
}
|
||||
|
||||
// Save the response for inspection
|
||||
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);
|
||||
|
||||
if (r_config_get_b (core->config, "r2ai.debug")) {
|
||||
R_LOG_DEBUG ("Anthropic API response: %s", res);
|
||||
}
|
||||
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");
|
||||
R_LOG_DEBUG ("Anthropic API response: %s", res);
|
||||
|
||||
// Parse the response
|
||||
R2AI_ChatResponse *result = R_NEW0 (R2AI_ChatResponse);
|
||||
if (!result) {
|
||||
free (res);
|
||||
if (error) {
|
||||
*error = strdup ("Failed to allocate memory for response");
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
R2AI_Message *message = NULL;
|
||||
R2AI_Usage *usage = NULL;
|
||||
@@ -201,9 +184,9 @@ R_IPI R2AI_ChatResponse *r2ai_anthropic(RCorePluginSession *cps, R2AIArgs args)
|
||||
}
|
||||
}
|
||||
|
||||
// Allocate tool_calls list if needed
|
||||
// Allocate tool_calls array if needed
|
||||
if (has_tool_use && n_tool_calls > 0) {
|
||||
message->tool_calls = r_list_new ();
|
||||
message->tool_calls = R_NEWS0 (R2AI_ToolCall, n_tool_calls);
|
||||
if (!message->tool_calls) {
|
||||
if (error) {
|
||||
*error = strdup ("Failed to allocate memory for tool calls");
|
||||
@@ -214,13 +197,13 @@ R_IPI R2AI_ChatResponse *r2ai_anthropic(RCorePluginSession *cps, R2AIArgs args)
|
||||
free (res);
|
||||
return NULL;
|
||||
}
|
||||
message->tool_calls->free = (RListFree)r2ai_tool_call_free;
|
||||
message->n_tool_calls = n_tool_calls;
|
||||
}
|
||||
|
||||
// Process each content item
|
||||
int tool_idx = 0;
|
||||
if (content_array && content_array->type == R_JSON_ARRAY) {
|
||||
RList *cb = r2ai_content_blocks_new ();
|
||||
R2AI_ContentBlocks *cb = R_NEW0 (R2AI_ContentBlocks);
|
||||
if (!cb) {
|
||||
r_json_free (jres);
|
||||
free (response_copy);
|
||||
@@ -228,11 +211,22 @@ R_IPI R2AI_ChatResponse *r2ai_anthropic(RCorePluginSession *cps, R2AIArgs args)
|
||||
free (res);
|
||||
return NULL;
|
||||
}
|
||||
cb->n_blocks = content_array->children.count;
|
||||
cb->blocks = R_NEWS0 (R2AI_ContentBlock, cb->n_blocks);
|
||||
if (!cb->blocks) {
|
||||
free (cb);
|
||||
r_json_free (jres);
|
||||
free (response_copy);
|
||||
r2ai_message_free (message);
|
||||
free (res);
|
||||
return NULL;
|
||||
}
|
||||
int block_idx = 0;
|
||||
const RJson *content_item = content_array->children.first;
|
||||
while (content_item) {
|
||||
while (content_item && block_idx < cb->n_blocks) {
|
||||
const RJson *type = r_json_get (content_item, "type");
|
||||
if (type && type->type == R_JSON_STRING) {
|
||||
R2AI_ContentBlock *block = R_NEW0 (R2AI_ContentBlock);
|
||||
R2AI_ContentBlock *block = &cb->blocks[block_idx];
|
||||
if (!strcmp (type->str_value, "text")) {
|
||||
// Text content
|
||||
const RJson *text = r_json_get (content_item, "text");
|
||||
@@ -248,30 +242,29 @@ R_IPI R2AI_ChatResponse *r2ai_anthropic(RCorePluginSession *cps, R2AIArgs args)
|
||||
const RJson *input = r_json_get (content_item, "input");
|
||||
|
||||
block->type = strdup ("tool_use");
|
||||
R2AI_ToolCall *tc = R_NEW0 (R2AI_ToolCall);
|
||||
if (name && name->type == R_JSON_STRING) {
|
||||
block->name = strdup (name->str_value);
|
||||
R2AI_ToolCall *tc = (R2AI_ToolCall *)&message->tool_calls[tool_idx];
|
||||
tc->name = strdup (name->str_value);
|
||||
}
|
||||
|
||||
if (id && id->type == R_JSON_STRING) {
|
||||
block->id = strdup (id->str_value);
|
||||
R2AI_ToolCall *tc = (R2AI_ToolCall *)&message->tool_calls[tool_idx];
|
||||
tc->id = strdup (id->str_value);
|
||||
}
|
||||
|
||||
if (input && input->type == R_JSON_OBJECT) {
|
||||
#if 1
|
||||
char *input_str = r_json_to_string (input);
|
||||
if (input_str) {
|
||||
R_LOG_DEBUG ("Input string: %s", input_str);
|
||||
block->input = strdup (input_str);
|
||||
R2AI_ToolCall *tc = (R2AI_ToolCall *)&message->tool_calls[tool_idx];
|
||||
tc->arguments = strdup (input_str);
|
||||
free (input_str);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
r_list_append (message->tool_calls, tc);
|
||||
tool_idx++;
|
||||
} else if (!strcmp (type->str_value, "thinking")) {
|
||||
const RJson *data = r_json_get (content_item, "data");
|
||||
@@ -288,11 +281,11 @@ R_IPI R2AI_ChatResponse *r2ai_anthropic(RCorePluginSession *cps, R2AIArgs args)
|
||||
}
|
||||
block->type = strdup ("thinking");
|
||||
|
||||
r_strbuf_append (content_buf, "\n" Color_GRAY "<thinking>\n");
|
||||
r_strbuf_append (content_buf, "\n\x1b[90m<thinking>\n");
|
||||
r_strbuf_append (content_buf, block->thinking);
|
||||
r_strbuf_append (content_buf, "\n</thinking>" Color_RESET "\n");
|
||||
r_strbuf_append (content_buf, "\n</thinking>\x1b[0m\n");
|
||||
}
|
||||
r_list_append (cb, block);
|
||||
block_idx++;
|
||||
}
|
||||
content_item = content_item->next;
|
||||
}
|
||||
@@ -303,7 +296,7 @@ R_IPI R2AI_ChatResponse *r2ai_anthropic(RCorePluginSession *cps, R2AIArgs args)
|
||||
message->content = r_strbuf_drain (content_buf);
|
||||
|
||||
// If there's no content and no tool calls, clean up
|
||||
if (!message->content && (!message->tool_calls || r_list_empty (message->tool_calls))) {
|
||||
if (!message->content && !message->n_tool_calls) {
|
||||
r2ai_message_free (message);
|
||||
message = NULL;
|
||||
}
|
||||
|
||||
@@ -1,85 +0,0 @@
|
||||
/* r2ai - Copyright 2023-2025 pancake */
|
||||
|
||||
#define R_LOG_ORIGIN "r2ai"
|
||||
|
||||
#include "r2ai.h"
|
||||
#include "r2ai_priv.h"
|
||||
|
||||
/* Return the path to the apikeys.txt file */
|
||||
R_API char *r2ai_apikeys_path(bool *exists) {
|
||||
char *path = r_file_home (".config/r2ai/apikeys.txt");
|
||||
if (exists) {
|
||||
*exists = r_file_exists (path);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/* Edit the API keys file */
|
||||
R_API void r2ai_apikeys_edit(RCorePluginSession *cps) {
|
||||
RCore *core = cps->core;
|
||||
bool exists = false;
|
||||
char *keys_path = r2ai_apikeys_path (&exists);
|
||||
if (!exists) {
|
||||
const char *template = "# API Keys configuration for r2ai\n"
|
||||
"\n"
|
||||
"# Specify your API keys in the format: PROVIDER=KEY\n"
|
||||
"# OpenAI=sk-your-openai-api-key-here\n"
|
||||
"# MISTRAL=your-mistral-api-key-here\n";
|
||||
r_file_dump (keys_path, (const ut8 *)template, strlen (template), 0);
|
||||
}
|
||||
r_cons_editor (core->cons, keys_path, NULL);
|
||||
free (keys_path);
|
||||
}
|
||||
|
||||
/* Return a malloc'd API key read from ~/.config/r2ai/apikeys.txt
|
||||
* Provider matching is case-insensitive */
|
||||
R_API char *r2ai_apikeys_get(const char *provider) {
|
||||
if (!provider) {
|
||||
return NULL;
|
||||
}
|
||||
bool exists = false;
|
||||
char *config_file = r2ai_apikeys_path (&exists);
|
||||
if (!exists) {
|
||||
free (config_file);
|
||||
return NULL;
|
||||
}
|
||||
char *content = r_file_slurp (config_file, NULL);
|
||||
free (config_file);
|
||||
if (!content) {
|
||||
return NULL;
|
||||
}
|
||||
RList *lines = r_str_split_list (content, "\n", 0);
|
||||
char *key = NULL;
|
||||
if (lines) {
|
||||
RListIter *iter;
|
||||
char *line;
|
||||
r_list_foreach (lines, iter, line) {
|
||||
const char *trimmed = r_str_trim_head_ro (line);
|
||||
if (R_STR_ISEMPTY (trimmed) || *trimmed == '#') {
|
||||
continue;
|
||||
}
|
||||
char *eq = strchr (trimmed, '=');
|
||||
if (eq) {
|
||||
*eq = 0;
|
||||
char *lower = strdup (trimmed);
|
||||
r_str_case (lower, false); /* lowercase */
|
||||
char *tail = strstr (lower, "_api_key");
|
||||
if (tail) {
|
||||
*tail = 0;
|
||||
}
|
||||
if (r_str_casecmp (lower, provider) == 0) {
|
||||
key = strdup (eq + 1);
|
||||
r_str_trim (key);
|
||||
free (lower);
|
||||
break;
|
||||
}
|
||||
free (lower);
|
||||
} else if (*trimmed) {
|
||||
R_LOG_WARN ("Invalid syntax in apikeys.txt: %s", line);
|
||||
}
|
||||
}
|
||||
r_list_free (lines);
|
||||
}
|
||||
free (content);
|
||||
return key;
|
||||
}
|
||||
+183
-419
@@ -1,54 +1,76 @@
|
||||
/* Copyright r2ai - 2023-2025 - pancake */
|
||||
|
||||
#include "r2ai.h"
|
||||
#include <time.h>
|
||||
|
||||
// Stats are now stored in R2AI_State->stats, no longer need global variable
|
||||
// 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 };
|
||||
|
||||
// Helper function to format time duration
|
||||
static char *format_time_duration(time_t seconds) {
|
||||
if (seconds < 60) {
|
||||
return r_str_newf ("%" PFMT64d "s", (ut64)seconds);
|
||||
return r_str_newf ("%llds", (long long)seconds);
|
||||
}
|
||||
if (seconds < 3600) {
|
||||
return r_str_newf ("%" PFMT64d "m%" PFMT64d "s", (ut64) (seconds / 60), (ut64) (seconds % 60));
|
||||
return r_str_newf ("%lldm%llds", (long long)(seconds / 60), (long long)(seconds % 60));
|
||||
}
|
||||
return r_str_newf ("%" PFMT64d "h%" PFMT64d "m%" PFMT64d "s",
|
||||
(ut64) (seconds / 3600),
|
||||
(ut64) ((seconds % 3600) / 60),
|
||||
(ut64) (seconds % 60));
|
||||
return r_str_newf ("%lldh%lldm%llds",
|
||||
(long long)(seconds / 3600),
|
||||
(long long)((seconds % 3600) / 60),
|
||||
(long long)(seconds % 60));
|
||||
}
|
||||
|
||||
// Initialize timing tracking for a run
|
||||
static void r2ai_stats_init_run(R2AI_State *state, int n_run) {
|
||||
// Initialize timing and cost tracking for a run
|
||||
static void r2ai_stats_init_run(int n_run) {
|
||||
time_t run_start = time (NULL);
|
||||
if (n_run == 1) {
|
||||
// First run, initialize total timing
|
||||
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.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.start_time = run_start;
|
||||
stats.start_time = run_start;
|
||||
}
|
||||
|
||||
// Print a simple run indicator at the start
|
||||
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;
|
||||
static void r2ai_print_run_end(RCore *core, const R2AI_Usage *usage, int n_run, int max_runs) {
|
||||
(void)n_run;
|
||||
(void)max_runs;
|
||||
time_t run_time = time (NULL) - state->stats.start_time;
|
||||
time_t total_time = time (NULL) - state->stats.total_start_time;
|
||||
time_t run_time = time (NULL) - stats.start_time;
|
||||
time_t total_time = time (NULL) - stats.total_start_time;
|
||||
if (usage) {
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// Format times for display
|
||||
@@ -56,197 +78,85 @@ static void r2ai_print_run_end(RCorePluginSession *cps, const R2AI_Usage *usage,
|
||||
char *total_time_str = format_time_duration (total_time);
|
||||
|
||||
// Print detailed stats
|
||||
r_cons_printf (core->cons, "\x1b[1" Color_BLUE "%s | total: %d in: %d out: %d | run: %d in: %d out: %d | %s / %s" Color_RESET "\n",
|
||||
R2_PRINTF ("\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"),
|
||||
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,
|
||||
stats.total_tokens,
|
||||
stats.total_prompt_tokens,
|
||||
stats.total_completion_tokens,
|
||||
stats.run_tokens,
|
||||
stats.run_prompt_tokens,
|
||||
stats.run_completion_tokens,
|
||||
run_time_str,
|
||||
total_time_str);
|
||||
r_cons_newline (core->cons);
|
||||
r_cons_flush (core->cons);
|
||||
R2_NEWLINE ();
|
||||
R2_FLUSH ();
|
||||
|
||||
free (run_time_str);
|
||||
free (total_time_str);
|
||||
}
|
||||
|
||||
// Helper function to handle final response attempt without tools
|
||||
static void handle_final_response_attempt(RCorePluginSession *cps, RList *messages, const char *effective_prompt, char **error) {
|
||||
RCore *core = cps->core;
|
||||
if (r_cons_yesno (core->cons, 1, "Try to produce response without tool calling with collected information? (Y/n)")) {
|
||||
char *final_system_prompt = r_str_newf ("%s\n\nIMPORTANT: Do not use any tools. Provide a direct answer based on the collected information.", effective_prompt);
|
||||
R2AIArgs args_final = {
|
||||
.messages = messages,
|
||||
.error = error,
|
||||
.dorag = true,
|
||||
.tools = NULL, // No tools for final response
|
||||
.system_prompt = final_system_prompt
|
||||
};
|
||||
|
||||
R2AI_ChatResponse *final_response = r2ai_llmcall (cps, args_final);
|
||||
if (final_response && final_response->message) {
|
||||
const R2AI_Message *final_msg = final_response->message;
|
||||
r_cons_printf (core->cons, Color_RED "[Assistant]" Color_RESET);
|
||||
if (final_msg->reasoning_content) {
|
||||
r_cons_printf (core->cons, Color_GRAY "<thinking>\n%s\n</thinking>" Color_RESET "\n", final_msg->reasoning_content);
|
||||
r_cons_newline (core->cons);
|
||||
r_cons_flush (core->cons);
|
||||
}
|
||||
if (final_msg->content) {
|
||||
r_cons_printf (core->cons, "%s", final_msg->content);
|
||||
r_cons_newline (core->cons);
|
||||
r_cons_flush (core->cons);
|
||||
}
|
||||
// Add final response to messages for completeness
|
||||
r2ai_msgs_add (messages, final_msg);
|
||||
free (final_response);
|
||||
}
|
||||
free (final_system_prompt);
|
||||
} else {
|
||||
r_cons_printf (core->cons, "Auto mode interrupted without final response.\n");
|
||||
r_cons_flush (core->cons);
|
||||
}
|
||||
}
|
||||
|
||||
static void update_tool_call_argument(R2AI_Message *msg, const char *tool_call_id, const char *field_name, const char *field_value) {
|
||||
R_RETURN_IF_FAIL (msg && tool_call_id && field_name && field_value);
|
||||
if (!msg->tool_calls || r_list_empty (msg->tool_calls)) {
|
||||
return;
|
||||
}
|
||||
RListIter *iter;
|
||||
R2AI_ToolCall *tc;
|
||||
r_list_foreach (msg->tool_calls, iter, tc) {
|
||||
if (!tc->id || strcmp (tc->id, tool_call_id) || !tc->arguments) {
|
||||
continue;
|
||||
}
|
||||
char *args_dup = strdup (tc->arguments);
|
||||
if (!args_dup) {
|
||||
return;
|
||||
}
|
||||
RJson *args_json = r_json_parse (args_dup);
|
||||
if (!args_json) {
|
||||
free (args_dup);
|
||||
return;
|
||||
}
|
||||
RJson *field_json = (RJson *)r_json_get (args_json, field_name);
|
||||
if (field_json && field_json->str_value) {
|
||||
const char *orig_value = field_json->str_value;
|
||||
field_json->str_value = field_value;
|
||||
char *new_args = r_json_to_string (args_json);
|
||||
field_json->str_value = orig_value;
|
||||
if (new_args) {
|
||||
free ((void *)tc->arguments);
|
||||
tc->arguments = new_args;
|
||||
}
|
||||
}
|
||||
r_json_free (args_json);
|
||||
free (args_dup);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
#if 0
|
||||
static const char *Gprompt_auto =
|
||||
"You are a reverse engineer using radare2.\n"
|
||||
"Answer questions about the loaded binary using r2cmd tool calls.\n"
|
||||
"\n"
|
||||
"Rules:\n"
|
||||
"- Use r2cmd tool to run radare2 commands when you need information\n"
|
||||
"- Make tool calls immediately when you need data\n"
|
||||
"- Provide final answer only after gathering needed information\n"
|
||||
"- Keep responses short and direct\n"
|
||||
;
|
||||
#else
|
||||
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"
|
||||
"- IMPORTANT: You must make only ONE tool call per response. Do not make multiple tool calls in a single response.\n"
|
||||
"- Keep your reasoning concise and avoid repeating the same thoughts. If you have a clear next step, execute it immediately with a tool call.\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"
|
||||
"- When calling tools, use the exact format: {\"tool_calls\": [{\"id\": \"call_123\", \"type\": \"function\", \"function\": {\"name\": \"r2cmd\", \"arguments\": \"{\\\"command\\\":\\\"aa\\\"}\"}}]}\n"
|
||||
"- Arguments must be a JSON string, not an object.\n";
|
||||
#endif
|
||||
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";
|
||||
|
||||
// Helper function to process messages and handle tool calls recursively
|
||||
R_API void process_messages(RCorePluginSession *cps, RList *messages, const char *system_prompt, int n_run) {
|
||||
RCore *core = cps->core;
|
||||
R2AI_State *state = cps->data;
|
||||
R_API void process_messages(RCore *core, R2AI_Messages *messages, const char *system_prompt, int n_run) {
|
||||
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) {
|
||||
R_LOG_WARN ("Max runs reached");
|
||||
r_cons_flush (core->cons);
|
||||
R2_PRINTF ("\x1b[1;31m[r2ai] Max runs reached\x1b[0m\n");
|
||||
R2_FLUSH ();
|
||||
return;
|
||||
}
|
||||
|
||||
const char *effective_prompt = system_prompt? system_prompt: Gprompt_auto;
|
||||
if (!system_prompt) {
|
||||
const char *init_commands = r_config_get (core->config, "r2ai.auto.init_commands");
|
||||
if (R_STR_ISNOTEMPTY (init_commands)) {
|
||||
char *edited_command = NULL;
|
||||
char *comment = NULL;
|
||||
char *cmd_output = execute_tool (core, "r2cmd", r_str_newf ("{\"command\":\"%s\"}", init_commands), &edited_command, &comment);
|
||||
if (R_STR_ISNOTEMPTY (cmd_output)) {
|
||||
char *display_command = strip_command_comment (init_commands, NULL);
|
||||
char *content = r_str_newf ("Here is some information about the binary to get you started:\n>%s\n%s", display_command, cmd_output);
|
||||
if (comment && *comment) {
|
||||
char *new_content = r_str_newf ("%s\nHINT: %s", content, comment);
|
||||
free (content);
|
||||
content = new_content;
|
||||
}
|
||||
R2AI_Message init_msg = {
|
||||
.role = "system",
|
||||
.content = content
|
||||
};
|
||||
r2ai_msgs_add (messages, &init_msg);
|
||||
free (display_command);
|
||||
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);
|
||||
free (cmd_output);
|
||||
}
|
||||
free (edited_command);
|
||||
free (comment);
|
||||
} else {
|
||||
system_prompt = Gprompt_auto;
|
||||
}
|
||||
}
|
||||
|
||||
r2ai_stats_init_run (state, n_run);
|
||||
|
||||
r_cons_printf (core->cons, Color_BLUE "About to call r2ai_llmcall with n_run=%d%s\n", n_run, Color_RESET);
|
||||
r_cons_flush (core->cons);
|
||||
r2ai_stats_init_run (n_run);
|
||||
|
||||
// Set up args for r2ai_llmcall call with tools directly
|
||||
R2AIArgs args = {
|
||||
.messages = messages,
|
||||
.error = &error,
|
||||
.dorag = true,
|
||||
.tools = r2ai_get_tools (core, cps->data), // Always send tools in auto mode
|
||||
.system_prompt = effective_prompt
|
||||
.tools = r2ai_get_tools (),
|
||||
.system_prompt = system_prompt
|
||||
};
|
||||
|
||||
R2AI_ChatResponse *response = r2ai_llmcall (cps, args);
|
||||
// Call r2ai_llmcall to get a response
|
||||
R2AI_ChatResponse *response = r2ai_llmcall (core, args);
|
||||
|
||||
if (!response) {
|
||||
R_LOG_ERROR ("No response from llmcall");
|
||||
handle_final_response_attempt (cps, messages, effective_prompt, &error);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -256,41 +166,21 @@ R_API void process_messages(RCorePluginSession *cps, RList *messages, const char
|
||||
if (!message) {
|
||||
R_LOG_ERROR ("No message in response");
|
||||
free (response);
|
||||
handle_final_response_attempt (cps, messages, effective_prompt, &error);
|
||||
return;
|
||||
}
|
||||
|
||||
// Debug logging for rawtools
|
||||
R_LOG_DEBUG ("Model response - Role: %s", message->role? message->role: "null");
|
||||
if (message->content) {
|
||||
R_LOG_DEBUG ("Content: %s", message->content);
|
||||
}
|
||||
if (message->reasoning_content) {
|
||||
R_LOG_DEBUG ("Reasoning: %s", message->reasoning_content);
|
||||
}
|
||||
if (message->tool_calls && r_list_length (message->tool_calls) > 0) {
|
||||
R_LOG_DEBUG ("Tool calls: %d", r_list_length (message->tool_calls));
|
||||
RListIter *iter;
|
||||
R2AI_ToolCall *tc;
|
||||
int i = 0;
|
||||
r_list_foreach (message->tool_calls, iter, tc) {
|
||||
R_LOG_DEBUG ("Tool %d: %s - %s", i, tc->name? tc->name: "null", tc->arguments? tc->arguments: "null");
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
// Process the response - we need to add it to our messages array
|
||||
if (message->content || message->reasoning_content) {
|
||||
r_cons_printf (core->cons, Color_RED "[Assistant]" Color_RESET);
|
||||
R2_PRINTF ("\x1b[31m[Assistant]\x1b[0m\n\n");
|
||||
if (message->reasoning_content) {
|
||||
r_cons_printf (core->cons, Color_GRAY "<thinking>\n%s\n</thinking>" Color_RESET "\n", message->reasoning_content);
|
||||
r_cons_newline (core->cons);
|
||||
r_cons_flush (core->cons);
|
||||
R2_PRINTF ("\x1b[90m<thinking>\n%s\n</thinking>\x1b[0m\n", message->reasoning_content);
|
||||
R2_NEWLINE ();
|
||||
R2_FLUSH ();
|
||||
}
|
||||
if (message->content) {
|
||||
r_cons_printf (core->cons, "%s", message->content);
|
||||
r_cons_newline (core->cons);
|
||||
r_cons_flush (core->cons);
|
||||
R2_PRINTF ("%s", message->content);
|
||||
R2_NEWLINE ();
|
||||
R2_FLUSH ();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -299,21 +189,17 @@ R_API void process_messages(RCorePluginSession *cps, RList *messages, const char
|
||||
r2ai_msgs_add (messages, message);
|
||||
|
||||
// Check for tool calls and process them
|
||||
if (message->tool_calls && r_list_length (message->tool_calls) > 0) {
|
||||
R_LOG_DEBUG ("Found %d tool call(s)", r_list_length (message->tool_calls));
|
||||
if (message->tool_calls && message->n_tool_calls > 0) {
|
||||
R_LOG_DEBUG ("Found %d tool call (s)", message->n_tool_calls);
|
||||
|
||||
// Process only the first tool call
|
||||
RListIter *iter;
|
||||
R2AI_ToolCall *tool_call;
|
||||
int i = 0;
|
||||
r_list_foreach (message->tool_calls, iter, tool_call) {
|
||||
// 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);
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
R_LOG_DEBUG ("Processing first tool call %d: %s with args: %s", i, tool_call->name, tool_call->arguments);
|
||||
R_LOG_DEBUG ("Tool call: %s", tool_call->name);
|
||||
// Don't log the full arguments which might get truncated
|
||||
char *tool_name = strdup (tool_call->name);
|
||||
char *tool_args = strdup (tool_call->arguments);
|
||||
@@ -325,87 +211,54 @@ R_API void process_messages(RCorePluginSession *cps, RList *messages, const char
|
||||
}
|
||||
|
||||
char *cmd_output = NULL;
|
||||
char *comment = NULL;
|
||||
if (interrupted) {
|
||||
cmd_output = strdup ("<user interrupted>");
|
||||
} else {
|
||||
char *edited_command = NULL;
|
||||
cmd_output = execute_tool (core, tool_name, tool_args, &edited_command, &comment);
|
||||
if (edited_command) {
|
||||
// Update the last message's tool call arguments with the edited command
|
||||
R2AI_Message *last_msg = r_list_get_n (messages, r_list_length (messages) - 1);
|
||||
if (last_msg) {
|
||||
if (!strcmp (tool_name, "r2cmd")) {
|
||||
update_tool_call_argument (last_msg, tool_call->id, "command", edited_command);
|
||||
} else if (!strcmp (tool_name, "execute_js")) {
|
||||
update_tool_call_argument (last_msg, tool_call->id, "script", edited_command);
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
free (edited_command);
|
||||
}
|
||||
|
||||
free (tool_name);
|
||||
free (tool_args);
|
||||
if (strcmp (cmd_output, "R2AI_SIGINT") == 0) {
|
||||
r_cons_printf (core->cons, "\n\n\x1b[1" Color_RED "[r2ai] Processing interrupted after tool execution" Color_RESET "\n\n");
|
||||
r_cons_flush (core->cons);
|
||||
R2_PRINTF ("\n\n\x1b[1;31m[r2ai] Processing interrupted after tool execution\x1b[0m\n\n");
|
||||
R2_FLUSH ();
|
||||
free (cmd_output);
|
||||
cmd_output = strdup ("<user interrupted>");
|
||||
interrupted = true;
|
||||
|
||||
handle_final_response_attempt (cps, messages, effective_prompt, &error);
|
||||
}
|
||||
if (comment) {
|
||||
char *msg = r_str_newf ("HINT: %s\n%s", comment, cmd_output);
|
||||
free (cmd_output);
|
||||
cmd_output = msg;
|
||||
}
|
||||
free (comment);
|
||||
|
||||
// Create a tool call response message
|
||||
R2AI_Message tool_response = {
|
||||
.role = "tool",
|
||||
.tool_call_id = strdup (tool_call->id),
|
||||
.tool_call_id = tool_call->id,
|
||||
.content = cmd_output
|
||||
};
|
||||
|
||||
// Add the tool response to our messages array
|
||||
r2ai_msgs_add (messages, &tool_response);
|
||||
R_LOG_DEBUG ("Added tool response to messages: %s", cmd_output? cmd_output: "null");
|
||||
r_cons_printf (core->cons, Color_GREEN "Tool result: %s" Color_RESET, cmd_output? cmd_output: "no output");
|
||||
free (cmd_output);
|
||||
i++;
|
||||
#if 0
|
||||
// Only process the first valid tool call
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
|
||||
r2ai_print_run_end (cps, usage, n_run, max_runs);
|
||||
r2ai_print_run_end (core, usage, n_run, max_runs);
|
||||
|
||||
// Check if we should continue with recursion
|
||||
if (!interrupted && message->tool_calls && r_list_length (message->tool_calls) > 0) {
|
||||
R_LOG_DEBUG ("Recursing to process_messages with n_run=%d", n_run + 1);
|
||||
process_messages (cps, messages, effective_prompt, n_run + 1);
|
||||
} else {
|
||||
R_LOG_DEBUG ("Auto mode loop ending - no more tool calls or interrupted");
|
||||
if (!interrupted && message->tool_calls && message->n_tool_calls > 0) {
|
||||
process_messages (core, messages, system_prompt, n_run + 1);
|
||||
}
|
||||
} else {
|
||||
r2ai_print_run_end (cps, usage, n_run, max_runs);
|
||||
r2ai_print_run_end (core, 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(RCorePluginSession *cps, const char *user_query) {
|
||||
RCore *core = cps->core;
|
||||
R2AI_State *state = cps->data;
|
||||
r_cons_printf (core->cons, Color_CYAN "cmd_r2ai_a called with query: %s" Color_RESET "\n", user_query);
|
||||
r_cons_flush (core->cons);
|
||||
R_IPI void cmd_r2ai_a(RCore *core, const char *user_query) {
|
||||
// Get conversation
|
||||
RList *messages = r2ai_conversation_get (state);
|
||||
R2AI_Messages *messages = r2ai_conversation_get ();
|
||||
if (!messages) {
|
||||
R_LOG_ERROR ("Conversation not initialized");
|
||||
return;
|
||||
@@ -413,44 +266,51 @@ R_IPI void cmd_r2ai_a(RCorePluginSession *cps, 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 (r_list_empty (messages) || r_config_get_b (core->config, "r2ai.auto.reset_on_query")) {
|
||||
if (messages->n_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 = (char *)user_query
|
||||
.content = user_query
|
||||
};
|
||||
r2ai_msgs_add (messages, &user_msg);
|
||||
|
||||
const char *system_prompt = NULL;
|
||||
if (!r_config_get_b (core->config, "r2ai.auto.think")) {
|
||||
system_prompt = r_str_newf ("/no_think\nReasoning: Low\n%s", Gprompt_auto);
|
||||
}
|
||||
process_messages (cps, messages, system_prompt, 1);
|
||||
if (system_prompt) {
|
||||
free ((char *)system_prompt);
|
||||
}
|
||||
process_messages (core, 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) {
|
||||
if (R_STR_ISEMPTY (content)) {
|
||||
r_cons_println (core->cons, empty_msg? empty_msg: "<no 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>");
|
||||
return;
|
||||
}
|
||||
|
||||
size_t content_len = strlen (content);
|
||||
const size_t max_display = 200;
|
||||
|
||||
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",
|
||||
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",
|
||||
content, content_len);
|
||||
} else {
|
||||
r_cons_println (core->cons, content);
|
||||
R2_PRINTF ("%s\n", content);
|
||||
}
|
||||
}
|
||||
|
||||
// Add this function right after cmd_r2ai_a
|
||||
R_IPI void cmd_r2ai_logs(RCorePluginSession *cps) {
|
||||
RCore *core = cps->core;
|
||||
R2AI_State *state = cps->data;
|
||||
R_IPI void cmd_r2ai_logs(RCore *core) {
|
||||
// Get conversation
|
||||
RList *messages = r2ai_conversation_get (state);
|
||||
if (!messages || r_list_empty (messages)) {
|
||||
r_cons_printf (core->cons, "No conversation history available\n");
|
||||
R2AI_Messages *messages = r2ai_conversation_get ();
|
||||
if (!messages || messages->n_messages == 0) {
|
||||
R2_PRINTF ("No conversation history available\n");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -466,184 +326,88 @@ R_IPI void cmd_r2ai_logs(RCorePluginSession *cps) {
|
||||
|
||||
pj_a (pj);
|
||||
|
||||
RListIter *iter;
|
||||
const R2AI_Message *msg;
|
||||
r_list_foreach (messages, iter, msg) {
|
||||
for (int i = 0; i < messages->n_messages; i++) {
|
||||
const R2AI_Message *msg = &messages->messages[i];
|
||||
|
||||
pj_o (pj);
|
||||
pj_ks (pj, "role", msg->role? msg->role: "unknown");
|
||||
pj_ks (pj, "content", msg->content? msg->content: "");
|
||||
if (msg->tool_calls && r_list_length (msg->tool_calls) > 0) {
|
||||
|
||||
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");
|
||||
RListIter *iter;
|
||||
R2AI_ToolCall *tc;
|
||||
r_list_foreach (msg->tool_calls, iter, tc) {
|
||||
|
||||
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);
|
||||
r_cons_printf (core->cons, "%s\n", json_str);
|
||||
R2_PRINTF ("%s\n", json_str);
|
||||
free (json_str);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
r_cons_printf (core->cons, "\x1b[1" Color_BLUE "[r2ai] Chat Logs (%d messages)" Color_RESET "\n",
|
||||
r_list_length (messages));
|
||||
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" Color_YELLOW "Note: System prompt is applied automatically but not stored in history" Color_RESET "\n\n");
|
||||
R2_PRINTF ("\x1b[1;33mNote: System prompt is applied automatically but not stored in history\x1b[0m\n\n");
|
||||
|
||||
// Display each message in the conversation
|
||||
RListIter *iter;
|
||||
const R2AI_Message *msg;
|
||||
r_list_foreach (messages, iter, msg) {
|
||||
for (int i = 0; i < messages->n_messages; i++) {
|
||||
const R2AI_Message *msg = &messages->messages[i];
|
||||
const char *role = msg->role;
|
||||
|
||||
// Format based on role
|
||||
if (!strcmp (role, "user")) {
|
||||
r_cons_printf (core->cons, "\x1b[1" Color_GREEN "[user]:" Color_RESET " ");
|
||||
print_content_with_length (core, msg->content, "<no content>");
|
||||
R2_PRINTF ("\x1b[1;32m[user]:\x1b[0m ");
|
||||
print_content_with_length (core, msg->content, "<no content>", false);
|
||||
} else if (!strcmp (role, "assistant")) {
|
||||
r_cons_printf (core->cons, "\x1b[1" Color_CYAN "[assistant]:" Color_RESET " ");
|
||||
print_content_with_length (core, msg->content, "<no content>");
|
||||
R2_PRINTF ("\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 && r_list_length (msg->tool_calls) > 0) {
|
||||
RListIter *iter;
|
||||
R2AI_ToolCall *tc;
|
||||
r_list_foreach (msg->tool_calls, iter, tc) {
|
||||
r_cons_printf (core->cons, " \x1b[1" Color_MAGENTA "[tool call]:" Color_RESET " %s\n",
|
||||
tc->name? tc->name: "<unnamed>");
|
||||
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>");
|
||||
|
||||
if (tc->arguments) {
|
||||
r_cons_printf (core->cons, " %s\n", tc->arguments);
|
||||
R2_PRINTF (" %s\n", tc->arguments);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (!strcmp (role, "tool")) {
|
||||
r_cons_printf (core->cons, "\x1b[1" Color_MAGENTA "[tool]:" Color_RESET " ");
|
||||
print_content_with_length (core, msg->content, "<no result>");
|
||||
R2_PRINTF ("\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.)
|
||||
r_cons_printf (core->cons, "\x1b[1" Color_WHITE "[%s]:" Color_RESET " ", role);
|
||||
print_content_with_length (core, msg->content, "<no content>");
|
||||
R2_PRINTF ("\x1b[1;37m[%s]:\x1b[0m ", role);
|
||||
print_content_with_length (core, msg->content, "<no content>", false);
|
||||
}
|
||||
|
||||
r_cons_newline (core->cons);
|
||||
r_cons_flush (core->cons);
|
||||
R2_NEWLINE ();
|
||||
R2_FLUSH ();
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to format conversation log as string
|
||||
static char *format_conversation_log(RList *messages) {
|
||||
RStrBuf *sb = r_strbuf_new ("");
|
||||
r_strbuf_append (sb, "Conversation Log:\n");
|
||||
|
||||
RListIter *iter;
|
||||
const R2AI_Message *msg;
|
||||
r_list_foreach (messages, iter, msg) {
|
||||
const char *role = msg->role;
|
||||
|
||||
if (!strcmp (role, "user")) {
|
||||
r_strbuf_appendf (sb, "[user]: %s\n", msg->content? msg->content: "<no content>");
|
||||
} else if (!strcmp (role, "assistant")) {
|
||||
r_strbuf_appendf (sb, "[assistant]: %s\n", msg->content? msg->content: "<no content>");
|
||||
// Include tool calls if present
|
||||
if (msg->tool_calls && r_list_length (msg->tool_calls) > 0) {
|
||||
RListIter *iter_tc;
|
||||
R2AI_ToolCall *tc;
|
||||
r_list_foreach (msg->tool_calls, iter_tc, tc) {
|
||||
r_strbuf_appendf (sb, " [tool call]: %s\n", tc->name? tc->name: "<unnamed>");
|
||||
if (tc->arguments) {
|
||||
r_strbuf_appendf (sb, " %s\n", tc->arguments);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (!strcmp (role, "tool")) {
|
||||
r_strbuf_appendf (sb, "[tool]: %s\n", msg->content? msg->content: "<no result>");
|
||||
} else {
|
||||
r_strbuf_appendf (sb, "[%s]: %s\n", role, msg->content? msg->content: "<no content>");
|
||||
}
|
||||
}
|
||||
|
||||
return r_strbuf_drain (sb);
|
||||
}
|
||||
|
||||
// Helper function to process conversation with LLM and handle result
|
||||
static void process_conversation_with_llm(RCorePluginSession *cps, bool compact) {
|
||||
RCore *core = cps->core;
|
||||
R2AI_State *state = cps->data;
|
||||
// Get conversation
|
||||
RList *messages = r2ai_conversation_get (state);
|
||||
if (!messages || r_list_empty (messages)) {
|
||||
r_cons_printf (core->cons, "No conversation history available\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// Format conversation as string
|
||||
char *log_str = format_conversation_log (messages);
|
||||
|
||||
// Load the compact prompt from file
|
||||
char *prompt_text = r2ai_load_prompt_text (core, "compact");
|
||||
if (!prompt_text) {
|
||||
// Fallback to hardcoded prompt
|
||||
prompt_text = strdup (compact
|
||||
? "Create a compact summary of this conversation that preserves all key information, insights, and context for future reference."
|
||||
"Focus on essential details about the binary analysis, tools used, findings, and any important conclusions."
|
||||
: "mai create a summary of all the information retrieved from the binary that is relevant for future work");
|
||||
}
|
||||
|
||||
// Combine log and prompt
|
||||
char *full_input = r_str_newf ("%s\n\n%s", log_str, prompt_text);
|
||||
|
||||
char *error = NULL;
|
||||
char *res = r2ai (cps, (R2AIArgs){ .input = full_input, .error = &error, .dorag = false });
|
||||
|
||||
free (log_str);
|
||||
free (full_input);
|
||||
free (prompt_text);
|
||||
|
||||
if (error) {
|
||||
R_LOG_ERROR ("%s", error);
|
||||
free (error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (res) {
|
||||
if (compact) {
|
||||
// Clear the conversation and add summary as system message
|
||||
r2ai_msgs_clear (messages);
|
||||
R2AI_Message summary_msg = {
|
||||
.role = "system",
|
||||
.content = res
|
||||
};
|
||||
r2ai_msgs_add (messages, &summary_msg);
|
||||
r_cons_printf (core->cons, "Conversation compacted. Summary added to history.\n");
|
||||
} else {
|
||||
// Just print the result
|
||||
r_cons_printf (core->cons, "%s\n", res);
|
||||
}
|
||||
free (res);
|
||||
}
|
||||
}
|
||||
|
||||
R_IPI void cmd_r2ai_c(RCorePluginSession *cps) {
|
||||
process_conversation_with_llm (cps, true);
|
||||
}
|
||||
|
||||
R_IPI void cmd_r2ai_lr(RCorePluginSession *cps) {
|
||||
process_conversation_with_llm (cps, false);
|
||||
}
|
||||
-216
@@ -1,216 +0,0 @@
|
||||
/* r2ai - Copyright 2023-2025 pancake, dnakov */
|
||||
|
||||
#include "r2ai.h"
|
||||
|
||||
/* Gemini API implementation */
|
||||
|
||||
R_IPI R2AI_ChatResponse *r2ai_gemini(RCorePluginSession *cps, R2AIArgs args) {
|
||||
RCore *core = cps->core;
|
||||
args.model = r_config_get (core->config, "r2ai.model");
|
||||
|
||||
const char *base_url = r2ai_get_provider_url (core, args.provider);
|
||||
const char *model_name = args.model && strstr (args.model, "gemini")? args.model: "gemini-2.0-flash-exp";
|
||||
char **error = args.error;
|
||||
|
||||
// Create a temp conversation to include the system prompt and the rest of the messages
|
||||
RList *temp_msgs = r2ai_msgs_new ();
|
||||
if (!temp_msgs) {
|
||||
if (error) {
|
||||
*error = strdup ("Failed to create temporary messages array");
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Get system prompt
|
||||
const char *system_prompt = NULL;
|
||||
if (R_STR_ISNOTEMPTY (args.system_prompt)) {
|
||||
system_prompt = args.system_prompt;
|
||||
} else {
|
||||
system_prompt = r_config_get (core->config, "r2ai.system");
|
||||
}
|
||||
|
||||
if (args.messages) {
|
||||
RListIter *iter;
|
||||
R2AI_Message *msg;
|
||||
r_list_foreach (args.messages, iter, msg) {
|
||||
r2ai_msgs_add (temp_msgs, msg);
|
||||
}
|
||||
} else {
|
||||
R_LOG_WARN ("No messages");
|
||||
}
|
||||
|
||||
if (error) {
|
||||
*error = NULL;
|
||||
}
|
||||
|
||||
// Setup headers for Gemini API
|
||||
const char **headers = NULL;
|
||||
char *auth_header = NULL;
|
||||
char *api_key_header = NULL;
|
||||
|
||||
if (R_STR_ISNOTEMPTY (args.api_key)) {
|
||||
static const char *static_headers[] = { NULL, NULL, NULL };
|
||||
headers = static_headers;
|
||||
headers[0] = "Content-Type: application/json";
|
||||
|
||||
// Check if it's an OAuth token (starts with "ya29.")
|
||||
if (r_str_startswith (args.api_key, "ya29.")) {
|
||||
auth_header = r_str_newf ("Authorization: Bearer %s", args.api_key);
|
||||
headers[1] = auth_header;
|
||||
} else {
|
||||
api_key_header = r_str_newf ("x-goog-api-key: %s", args.api_key);
|
||||
headers[1] = api_key_header;
|
||||
}
|
||||
}
|
||||
|
||||
// Build Gemini API URL
|
||||
char *gemini_url = r_str_newf ("%s/models/%s:generateContent?key=%s",
|
||||
base_url, model_name, args.api_key);
|
||||
|
||||
// Create Gemini-style request JSON
|
||||
PJ *pj = pj_new ();
|
||||
pj_o (pj);
|
||||
|
||||
// Build contents array
|
||||
pj_ka (pj, "contents");
|
||||
RListIter *iter;
|
||||
R2AI_Message *msg;
|
||||
int msg_index = 0;
|
||||
r_list_foreach (temp_msgs, iter, msg) {
|
||||
pj_o (pj);
|
||||
// Gemini uses "model" for assistant, "user" for user
|
||||
const char *role = "user";
|
||||
if (strcmp (msg->role, "assistant") == 0) {
|
||||
role = "model";
|
||||
}
|
||||
pj_ks (pj, "role", role);
|
||||
pj_ka (pj, "parts");
|
||||
pj_o (pj);
|
||||
// Prepend system prompt to first user message
|
||||
char *content = NULL;
|
||||
if (system_prompt && strcmp (msg->role, "user") == 0 && msg_index == 0) {
|
||||
content = r_str_newf ("System: %s\n\n%s", system_prompt, msg->content? msg->content: "");
|
||||
} else {
|
||||
content = msg->content? strdup (msg->content): strdup ("");
|
||||
}
|
||||
pj_ks (pj, "text", content);
|
||||
free (content);
|
||||
pj_end (pj); // end parts object
|
||||
pj_end (pj); // end parts array
|
||||
pj_end (pj); // end content object
|
||||
msg_index++;
|
||||
}
|
||||
pj_end (pj); // end contents array
|
||||
|
||||
// Add generation config
|
||||
pj_ko (pj, "generationConfig");
|
||||
if (args.max_tokens > 0) {
|
||||
pj_kn (pj, "maxOutputTokens", args.max_tokens);
|
||||
}
|
||||
if (args.temperature > 0) {
|
||||
pj_kd (pj, "temperature", args.temperature);
|
||||
}
|
||||
pj_end (pj); // end generationConfig
|
||||
|
||||
pj_end (pj); // end root object
|
||||
|
||||
char *request_json = pj_drain (pj);
|
||||
|
||||
// Debug: save request
|
||||
char *tmpdir = r_file_tmpdir ();
|
||||
char *req_path = r_str_newf ("%s" R_SYS_DIR "r2ai_gemini_request.json", tmpdir);
|
||||
r_file_dump (req_path, (const ut8 *)request_json, strlen (request_json), 0);
|
||||
free (req_path);
|
||||
free (tmpdir);
|
||||
|
||||
// Make the API call
|
||||
char *response = NULL;
|
||||
int code = 0;
|
||||
response = r2ai_http_post (core, gemini_url, headers, request_json, &code, NULL);
|
||||
|
||||
free (request_json);
|
||||
free (gemini_url);
|
||||
free (auth_header);
|
||||
free (api_key_header);
|
||||
|
||||
if (code != 200) {
|
||||
R_LOG_ERROR ("Gemini API error %d", code);
|
||||
if (response) {
|
||||
R_LOG_ERROR ("Gemini API error response: %s", response);
|
||||
}
|
||||
free (response);
|
||||
r2ai_msgs_free (temp_msgs);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Debug: save response
|
||||
tmpdir = r_file_tmpdir ();
|
||||
char *res_path = r_str_newf ("%s" R_SYS_DIR "r2ai_gemini_response.json", tmpdir);
|
||||
r_file_dump (res_path, (const ut8 *)response, strlen (response), 0);
|
||||
free (res_path);
|
||||
free (tmpdir);
|
||||
|
||||
// Parse Gemini response
|
||||
RJson *jres = r_json_parse (response);
|
||||
if (!jres) {
|
||||
if (error) {
|
||||
*error = strdup ("Failed to parse Gemini response JSON");
|
||||
}
|
||||
free (response);
|
||||
r2ai_msgs_free (temp_msgs);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Extract response content
|
||||
const RJson *candidates = r_json_get (jres, "candidates");
|
||||
if (!candidates || candidates->type != R_JSON_ARRAY || candidates->children.count == 0) {
|
||||
r_json_free (jres);
|
||||
free (response);
|
||||
r2ai_msgs_free (temp_msgs);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const RJson *candidate = r_json_item (candidates, 0);
|
||||
const RJson *content = r_json_get (candidate, "content");
|
||||
const RJson *parts = r_json_get (content, "parts");
|
||||
|
||||
if (!parts || parts->type != R_JSON_ARRAY || parts->children.count == 0) {
|
||||
r_json_free (jres);
|
||||
free (response);
|
||||
r2ai_msgs_free (temp_msgs);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const RJson *part = r_json_item (parts, 0);
|
||||
const RJson *text = r_json_get (part, "text");
|
||||
|
||||
char *response_text = NULL;
|
||||
if (text && text->type == R_JSON_STRING) {
|
||||
response_text = strdup (text->str_value);
|
||||
}
|
||||
|
||||
r_json_free (jres);
|
||||
free (response);
|
||||
r2ai_msgs_free (temp_msgs);
|
||||
|
||||
if (!response_text) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Create response
|
||||
R2AI_Message *message = R_NEW0 (R2AI_Message);
|
||||
R2AI_Usage *usage = R_NEW0 (R2AI_Usage);
|
||||
|
||||
if (message) {
|
||||
message->role = strdup ("assistant");
|
||||
message->content = response_text;
|
||||
}
|
||||
|
||||
R2AI_ChatResponse *result = R_NEW0 (R2AI_ChatResponse);
|
||||
if (result) {
|
||||
result->message = message;
|
||||
result->usage = usage;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
-240
@@ -1,240 +0,0 @@
|
||||
/* Copyright r2ai - 2023-2025 - pancake */
|
||||
|
||||
#define _GNU_SOURCE
|
||||
// #define _POSIX_C_SOURCE 200809L
|
||||
#include <signal.h>
|
||||
#include "r2ai.h"
|
||||
|
||||
// Global flag for tracking interrupt status
|
||||
static volatile sig_atomic_t r2ai_http_interrupted = 0;
|
||||
|
||||
// Helper function to get HTTP configuration with defaults
|
||||
static R2AI_HttpConfig get_http_config(RCore *core) {
|
||||
R2AI_HttpConfig config = { 120, 10, 30 };
|
||||
if (core) {
|
||||
int t = r_config_get_i (core->config, "r2ai.http.timeout");
|
||||
if (t > 0) {
|
||||
config.timeout = t;
|
||||
}
|
||||
int mr = r_config_get_i (core->config, "r2ai.http.max_retries");
|
||||
if (mr >= 0) {
|
||||
config.max_retries = mr;
|
||||
}
|
||||
int mb = r_config_get_i (core->config, "r2ai.http.max_backoff");
|
||||
if (mb > 0) {
|
||||
config.max_backoff = mb;
|
||||
}
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
#if R2__UNIX__
|
||||
// Signal handler for timeout (SIGALRM)
|
||||
static void r2ai_http_sigint_handler(int sig) {
|
||||
if (sig == SIGALRM) {
|
||||
r2ai_http_interrupted = 1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// Portable break callback for r_cons_break (handles SIGINT)
|
||||
static void r2ai_http_break_callback(void *user) {
|
||||
(void)user;
|
||||
r2ai_http_interrupted = 1;
|
||||
}
|
||||
|
||||
// Function pointer type for HTTP request implementations
|
||||
typedef HttpResponse(*HttpRequestFunc)(const HTTPRequest *request);
|
||||
|
||||
// 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;
|
||||
}
|
||||
r_sys_sleep (delay_seconds);
|
||||
}
|
||||
|
||||
// Generic HTTP request with retry logic
|
||||
static HttpResponse r2ai_http_request_with_retry(HttpRequestFunc func, const HTTPRequest *request, RCore *core) {
|
||||
// Install portable break handler for interruption
|
||||
if (core && core->cons) {
|
||||
r_cons_break_push (core->cons, r2ai_http_break_callback, NULL);
|
||||
}
|
||||
|
||||
// Reset interrupt flag
|
||||
r2ai_http_interrupted = 0;
|
||||
|
||||
#if R2__UNIX__
|
||||
// Ignore SIGWINCH to prevent window resize from interrupting child processes
|
||||
struct sigaction old_winch_act;
|
||||
sigaction (SIGWINCH, &(struct sigaction){ .sa_handler = SIG_IGN }, &old_winch_act);
|
||||
#endif
|
||||
|
||||
// Retry loop
|
||||
HttpResponse result = { 0 };
|
||||
int retry_count = 0;
|
||||
bool success = false;
|
||||
|
||||
while (!success && retry_count <= request->config.max_retries && !r2ai_http_interrupted) {
|
||||
result = func (request);
|
||||
|
||||
// Check if we were interrupted
|
||||
if (r2ai_http_interrupted) {
|
||||
R_LOG_DEBUG ("HTTP request was interrupted by user");
|
||||
free (result.body);
|
||||
result.body = NULL;
|
||||
result.code = -1;
|
||||
break; // Exit the retry loop
|
||||
}
|
||||
|
||||
// Check for rate limiting or server errors (429, 500, 502, 503, 504)
|
||||
if (result.code > 0) {
|
||||
if (result.code == 429 || (result.code >= 500 && result.code < 600)) {
|
||||
R_LOG_WARN ("Server returned %d response code", result.code);
|
||||
free (result.body);
|
||||
result.body = NULL;
|
||||
result.code = 0;
|
||||
|
||||
if (retry_count < request->config.max_retries) {
|
||||
retry_count++;
|
||||
R_LOG_INFO ("Retrying request (%d/%d) after error...", retry_count, request->config.max_retries);
|
||||
sleep_retry (retry_count, request->config.max_backoff);
|
||||
continue;
|
||||
}
|
||||
result.code = -1;
|
||||
break; // Exit the retry loop after max retries
|
||||
}
|
||||
}
|
||||
|
||||
// Check for other failures
|
||||
if (result.code <= 0 || !result.body) {
|
||||
free (result.body);
|
||||
result.body = NULL;
|
||||
result.code = -1;
|
||||
if (retry_count < request->config.max_retries) {
|
||||
retry_count++;
|
||||
R_LOG_INFO ("Retrying request (%d/%d) after failure...", retry_count, request->config.max_retries);
|
||||
sleep_retry (retry_count, request->config.max_backoff);
|
||||
continue;
|
||||
}
|
||||
break; // Exit the retry loop after max retries
|
||||
}
|
||||
success = true;
|
||||
}
|
||||
|
||||
// Restore the original break handler
|
||||
if (core && core->cons) {
|
||||
r_cons_break_pop (core->cons);
|
||||
}
|
||||
|
||||
#if R2__UNIX__
|
||||
// Restore SIGWINCH handler
|
||||
sigaction (SIGWINCH, &old_winch_act, NULL);
|
||||
#endif
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#include "http/libcurl.inc.c"
|
||||
#include "http/pwsh.inc.c"
|
||||
#include "http/curl.inc.c"
|
||||
#include "http/r2.inc.c"
|
||||
|
||||
static HttpRequestFunc select_backend(const char *backend, bool is_post) {
|
||||
HttpRequestFunc func = NULL;
|
||||
if (!strcmp (backend, "auto")) {
|
||||
#if R2__WINDOWS__
|
||||
// TODO: we should use pwsh or r2 by default, but for not it seems the only backend that works on Windows is r2curl+system
|
||||
r_sys_setenv ("R2_CURL", "1");
|
||||
backend = "system";
|
||||
#elif USE_LIBCURL && HAVE_LIBCURL
|
||||
backend = "libcurl";
|
||||
#else
|
||||
backend = "system";
|
||||
#endif
|
||||
R_LOG_DEBUG ("Auto-selected %s backend", backend);
|
||||
}
|
||||
if (!strcmp (backend, "system") || !strcmp (backend, "curl")) {
|
||||
func = is_post? system_curl_post: system_curl_get;
|
||||
} else if (!strcmp (backend, "libcurl")) {
|
||||
#if USE_LIBCURL && HAVE_LIBCURL
|
||||
func = is_post? curl_http_post: curl_http_get;
|
||||
#else
|
||||
R_LOG_WARN ("LibCurl requested but not available, falling back to socket implementation");
|
||||
func = is_post? socket_http_post_with_interrupt: socket_http_get_with_interrupt;
|
||||
#endif
|
||||
} else if (!strcmp (backend, "socket")) {
|
||||
func = is_post? socket_http_post_with_interrupt: socket_http_get_with_interrupt;
|
||||
} else if (!strcmp (backend, "pwsh") || !strcmp (backend, "powershell")) {
|
||||
#if R2__WINDOWS__
|
||||
func = is_post? windows_http_post: windows_http_get;
|
||||
#else
|
||||
R_LOG_WARN ("powershell is only available on Windows");
|
||||
func = is_post? socket_http_post_with_interrupt: socket_http_get_with_interrupt;
|
||||
#endif
|
||||
} else if (!strcmp (backend, "r2curl")) {
|
||||
r_sys_setenv ("R2_CURL", "1");
|
||||
func = is_post? socket_http_post_with_interrupt: socket_http_get_with_interrupt;
|
||||
}
|
||||
return func;
|
||||
}
|
||||
|
||||
// Generic HTTP request function that handles backend selection
|
||||
static HttpResponse r2ai_http_request(const char *method, RCore *core, const char *url, const char *headers[], const char *data) {
|
||||
(void)method;
|
||||
bool is_post = (data != NULL);
|
||||
|
||||
HTTPRequest request = {
|
||||
.config = get_http_config (core),
|
||||
.url = url,
|
||||
.data = data,
|
||||
.headers = headers
|
||||
};
|
||||
|
||||
const char *backend = r_config_get (core->config, "r2ai.http.backend");
|
||||
HttpRequestFunc func = select_backend (backend, is_post);
|
||||
if (func) {
|
||||
return r2ai_http_request_with_retry (func, &request, core);
|
||||
}
|
||||
R_LOG_ERROR ("Cannot find a valid http backend");
|
||||
return (HttpResponse){ .body = NULL, .code = -1, .length = 0 };
|
||||
}
|
||||
|
||||
R_API char *r2ai_http_post(RCore *core, const char *url, const char *headers[], const char *data, int *code, int *rlen) {
|
||||
HttpResponse response = r2ai_http_request ("POST", core, url, headers, data);
|
||||
if (response.code <= 0) {
|
||||
free (response.body);
|
||||
if (code) {
|
||||
*code = response.code;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
if (code) {
|
||||
*code = response.code;
|
||||
}
|
||||
if (rlen) {
|
||||
*rlen = response.length;
|
||||
}
|
||||
return response.body;
|
||||
}
|
||||
|
||||
R_API char *r2ai_http_get(RCore *core, const char *url, const char *headers[], int *code, int *rlen) {
|
||||
HttpResponse response = r2ai_http_request ("GET", core, url, headers, NULL);
|
||||
if (response.code <= 0) {
|
||||
if (response.body) {
|
||||
R_FREE (response.body);
|
||||
}
|
||||
if (code) {
|
||||
*code = response.code;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
if (code) {
|
||||
*code = response.code;
|
||||
}
|
||||
if (rlen) {
|
||||
*rlen = response.length;
|
||||
}
|
||||
return response.body;
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
// Helper to escape double quotes for cmd double-quoted strings
|
||||
static char *escape_cmd_double_quotes(const char *str) {
|
||||
if (!str) {
|
||||
return NULL;
|
||||
}
|
||||
size_t len = strlen (str);
|
||||
size_t count = 0;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (str[i] == '"') {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if (count == 0) {
|
||||
return strdup (str);
|
||||
}
|
||||
char *escaped = malloc (len + count + 1);
|
||||
if (!escaped) {
|
||||
return NULL;
|
||||
}
|
||||
size_t j = 0;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (str[i] == '"') {
|
||||
escaped[j++] = '\\';
|
||||
escaped[j++] = '"';
|
||||
} else {
|
||||
escaped[j++] = str[i];
|
||||
}
|
||||
}
|
||||
escaped[j] = '\0';
|
||||
return escaped;
|
||||
}
|
||||
|
||||
static HttpResponse build_and_execute_curl(const char *cmd_start, const HTTPRequest *request, const char *input_data) {
|
||||
HttpResponse error = { .code = -1 };
|
||||
int timeout = request->config.timeout;
|
||||
if (!request->url) {
|
||||
return error;
|
||||
}
|
||||
|
||||
// Compose curl command
|
||||
RStrBuf *cmd = r_strbuf_new (cmd_start);
|
||||
|
||||
// Add timeout
|
||||
r_strbuf_appendf (cmd, " --connect-timeout %d --max-time %d", 10, timeout);
|
||||
|
||||
// Add headers
|
||||
if (request->headers) {
|
||||
for (int i = 0; request->headers[i] != NULL; i++) {
|
||||
char *escaped_header = escape_cmd_double_quotes (request->headers[i]);
|
||||
r_strbuf_appendf (cmd, " -H \"%s\"", escaped_header);
|
||||
free (escaped_header);
|
||||
}
|
||||
}
|
||||
char *escaped_url = escape_cmd_double_quotes (request->url);
|
||||
r_strbuf_appendf (cmd, " -w \"\\n%%{http_code}\" \"%s\"", escaped_url);
|
||||
free (escaped_url);
|
||||
char *cmd_str = r_strbuf_drain (cmd);
|
||||
R_LOG_DEBUG ("Running system curl: %s", cmd_str);
|
||||
char *response = r_sys_cmd_str (cmd_str, input_data, NULL);
|
||||
|
||||
free (cmd_str);
|
||||
|
||||
if (!response) {
|
||||
return error;
|
||||
}
|
||||
|
||||
// Parse the response: body\ncode
|
||||
char *last_nl = (char *)r_str_rchr (response, NULL, '\n');
|
||||
int code = -1;
|
||||
char *body = response;
|
||||
if (last_nl) {
|
||||
*last_nl = '\0';
|
||||
code = atoi (last_nl + 1);
|
||||
if (code == 0) {
|
||||
code = -1; // Invalid code
|
||||
}
|
||||
} else {
|
||||
// No newline, assume 200 if response
|
||||
code = 200;
|
||||
}
|
||||
|
||||
return (HttpResponse){ .body = body, .code = code, .length = strlen (body) };
|
||||
}
|
||||
|
||||
HttpResponse system_curl_get(const HTTPRequest *request) {
|
||||
R_LOG_DEBUG ("Using system curl GET");
|
||||
return build_and_execute_curl ("curl -s", request, NULL);
|
||||
}
|
||||
|
||||
HttpResponse system_curl_post(const HTTPRequest *request) {
|
||||
HttpResponse error = { .code = -1 };
|
||||
if (!request->url || !request->data) {
|
||||
return error;
|
||||
}
|
||||
|
||||
R_LOG_DEBUG ("Using system curl POST without files");
|
||||
HttpResponse res = build_and_execute_curl ("curl -s -X POST -d @-", request, request->data);
|
||||
return res;
|
||||
}
|
||||
@@ -1,140 +0,0 @@
|
||||
#if HAVE_LIBCURL
|
||||
#include <curl/curl.h>
|
||||
|
||||
// Struct to store response data
|
||||
typedef struct {
|
||||
char *data;
|
||||
size_t size;
|
||||
} CurlResponse;
|
||||
|
||||
// Callback function for curl to write response data
|
||||
static size_t write_callback(void *contents, size_t size, size_t nmemb, void *userp) {
|
||||
// Check if we've been interrupted
|
||||
if (r2ai_http_interrupted) {
|
||||
return 0; // Return 0 to abort the transfer
|
||||
}
|
||||
|
||||
size_t realsize = size * nmemb;
|
||||
CurlResponse *mem = (CurlResponse *)userp;
|
||||
|
||||
char *ptr = realloc (mem->data, mem->size + realsize + 1);
|
||||
if (!ptr) {
|
||||
R_LOG_ERROR ("Not enough memory for curl response");
|
||||
return 0; // Out of memory
|
||||
}
|
||||
|
||||
mem->data = ptr;
|
||||
memcpy (&(mem->data[mem->size]), contents, realsize);
|
||||
mem->size += realsize;
|
||||
mem->data[mem->size] = 0;
|
||||
|
||||
return realsize;
|
||||
}
|
||||
|
||||
// Curl progress callback function to handle interrupts
|
||||
static int progress_callback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) {
|
||||
return r2ai_http_interrupted? 1: 0;
|
||||
}
|
||||
|
||||
static HttpResponse curl_http_request(const HTTPRequest *request, bool is_post) {
|
||||
HttpResponse error = { .code = -1 };
|
||||
if (!request->url || (is_post && !request->data)) {
|
||||
return error;
|
||||
}
|
||||
|
||||
CURL *curl;
|
||||
CURLcode res;
|
||||
struct curl_slist *curl_headers = NULL;
|
||||
CurlResponse response = { 0 };
|
||||
|
||||
// Initialize response
|
||||
response.data = malloc (1);
|
||||
if (!response.data) {
|
||||
return error;
|
||||
}
|
||||
response.data[0] = '\0';
|
||||
response.size = 0;
|
||||
|
||||
curl = curl_easy_init ();
|
||||
if (!curl) {
|
||||
free (response.data);
|
||||
return error;
|
||||
}
|
||||
|
||||
// Set URL
|
||||
curl_easy_setopt (curl, CURLOPT_URL, request->url);
|
||||
|
||||
// Set method-specific options
|
||||
if (is_post) {
|
||||
curl_easy_setopt (curl, CURLOPT_POST, 1L);
|
||||
curl_easy_setopt (curl, CURLOPT_POSTFIELDS, request->data);
|
||||
curl_easy_setopt (curl, CURLOPT_POSTFIELDSIZE, (long)strlen (request->data));
|
||||
} else {
|
||||
curl_easy_setopt (curl, CURLOPT_HTTPGET, 1L);
|
||||
}
|
||||
|
||||
// Set headers if provided
|
||||
if (request->headers) {
|
||||
for (int i = 0; request->headers[i] != NULL; i++) {
|
||||
curl_headers = curl_slist_append (curl_headers, request->headers[i]);
|
||||
}
|
||||
curl_easy_setopt (curl, CURLOPT_HTTPHEADER, curl_headers);
|
||||
}
|
||||
|
||||
// Set write callback
|
||||
curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, write_callback);
|
||||
curl_easy_setopt (curl, CURLOPT_WRITEDATA, (void *)&response);
|
||||
|
||||
// Set progress callback to handle interrupts
|
||||
curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0L);
|
||||
curl_easy_setopt (curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
|
||||
curl_easy_setopt (curl, CURLOPT_XFERINFODATA, NULL);
|
||||
|
||||
// Set timeout options
|
||||
curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 10L); // 10 seconds connect timeout
|
||||
curl_easy_setopt (curl, CURLOPT_TIMEOUT, (long)request->config.timeout); // Use configured timeout
|
||||
|
||||
// Follow redirects
|
||||
curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L);
|
||||
|
||||
// Perform the request
|
||||
res = curl_easy_perform (curl);
|
||||
|
||||
// Check for interruption
|
||||
if (r2ai_http_interrupted) {
|
||||
R_LOG_DEBUG ("HTTP request was interrupted by user");
|
||||
free (response.data);
|
||||
curl_slist_free_all (curl_headers);
|
||||
curl_easy_cleanup (curl);
|
||||
return error;
|
||||
}
|
||||
|
||||
// Get response code
|
||||
long http_code;
|
||||
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
|
||||
|
||||
// Check for errors
|
||||
if (res != CURLE_OK) {
|
||||
R_LOG_ERROR ("curl_easy_perform () failed: %s", curl_easy_strerror (res));
|
||||
free (response.data);
|
||||
curl_slist_free_all (curl_headers);
|
||||
curl_easy_cleanup (curl);
|
||||
return error;
|
||||
}
|
||||
|
||||
// If we get here, the request was successful
|
||||
// Cleanup
|
||||
curl_slist_free_all (curl_headers);
|
||||
curl_easy_cleanup (curl);
|
||||
|
||||
return (HttpResponse){ .body = response.data, .code = (int)http_code, .length = response.size };
|
||||
}
|
||||
|
||||
HttpResponse curl_http_get(const HTTPRequest *request) {
|
||||
return curl_http_request (request, false);
|
||||
}
|
||||
|
||||
HttpResponse curl_http_post(const HTTPRequest *request) {
|
||||
return curl_http_request (request, true);
|
||||
}
|
||||
#endif // HAVE_LIBCURL
|
||||
@@ -1,118 +0,0 @@
|
||||
#if R2__WINDOWS__
|
||||
|
||||
// Helper to escape single quotes for PowerShell single-quoted strings
|
||||
static char *escape_single_quotes(const char *str) {
|
||||
if (!str) {
|
||||
return NULL;
|
||||
}
|
||||
size_t len = strlen (str);
|
||||
size_t count = 0;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (str[i] == '\'') {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if (count == 0) {
|
||||
return strdup (str);
|
||||
}
|
||||
char *escaped = malloc (len + count + 1);
|
||||
if (!escaped) {
|
||||
return NULL;
|
||||
}
|
||||
size_t j = 0;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (str[i] == '\'') {
|
||||
escaped[j++] = '\'';
|
||||
escaped[j++] = '\'';
|
||||
} else {
|
||||
escaped[j++] = str[i];
|
||||
}
|
||||
}
|
||||
escaped[j] = '\0';
|
||||
return escaped;
|
||||
}
|
||||
|
||||
// Helper to append headers to PowerShell command
|
||||
static void append_headers_to_cmd(RStrBuf *cmd, const char *const *headers) {
|
||||
r_strbuf_appendf (cmd, "$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);
|
||||
char *escaped_key = escape_single_quotes (key);
|
||||
char *escaped_value = escape_single_quotes (value);
|
||||
r_strbuf_appendf (cmd, "'%s'='%s';", escaped_key, escaped_value);
|
||||
free (escaped_key);
|
||||
free (escaped_value);
|
||||
free (key);
|
||||
free (value);
|
||||
}
|
||||
free (header);
|
||||
}
|
||||
}
|
||||
r_strbuf_appendf (cmd, "};");
|
||||
}
|
||||
|
||||
// Helper to parse PowerShell response
|
||||
static HttpResponse parse_powershell_response(char *full_response) {
|
||||
if (full_response) {
|
||||
char *newline = strchr (full_response, '\n');
|
||||
if (newline) {
|
||||
*newline = '\0';
|
||||
int code = atoi (full_response);
|
||||
char *body = newline + 1;
|
||||
char *body_copy = strdup (body);
|
||||
size_t length = strlen (body);
|
||||
free (full_response);
|
||||
return (HttpResponse){ .body = body_copy, .code = code, .length = length };
|
||||
} else {
|
||||
return (HttpResponse){ .body = full_response, .code = 200, .length = strlen (full_response) };
|
||||
}
|
||||
}
|
||||
return (HttpResponse){ .code = -1 };
|
||||
}
|
||||
/**
|
||||
* Windows-specific HTTP POST using PowerShell
|
||||
*/
|
||||
HttpResponse windows_http_post(const HTTPRequest *request) {
|
||||
const char *url = request->url;
|
||||
const char *const *headers = request->headers;
|
||||
const char *data = request->data;
|
||||
int timeout = request->config.timeout;
|
||||
char *escaped_url = escape_single_quotes (url);
|
||||
char *escaped_data = escape_single_quotes (data);
|
||||
RStrBuf *cmd = r_strbuf_new ("powershell -Command \"");
|
||||
append_headers_to_cmd (cmd, headers);
|
||||
r_strbuf_appendf (cmd, "$body='%s';", escaped_data);
|
||||
r_strbuf_appendf (cmd, "try{$r=Invoke-WebRequest -Method Post -Uri '%s' -Headers $headers -Body $body -TimeoutSec %d;", escaped_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);
|
||||
R_LOG_DEBUG ("Running PowerShell: %s", cmd_str);
|
||||
char *full_response = r_sys_cmd_str (cmd_str, NULL, NULL);
|
||||
free (cmd_str);
|
||||
return parse_powershell_response (full_response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Windows-specific HTTP GET using PowerShell
|
||||
*/
|
||||
HttpResponse windows_http_get(const HTTPRequest *request) {
|
||||
const char *url = request->url;
|
||||
const char *const *headers = request->headers;
|
||||
int timeout = request->config.timeout;
|
||||
char *escaped_url = escape_single_quotes (url);
|
||||
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;", escaped_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);
|
||||
R_LOG_DEBUG ("Running PowerShell: %s", cmd_str);
|
||||
char *full_response = r_sys_cmd_str (cmd_str, NULL, NULL);
|
||||
free (cmd_str);
|
||||
return parse_powershell_response (full_response);
|
||||
}
|
||||
#endif
|
||||
@@ -1,49 +0,0 @@
|
||||
extern volatile sig_atomic_t r2ai_http_interrupted;
|
||||
extern void r2ai_http_sigint_handler(int sig);
|
||||
|
||||
// Socket implementation with interrupt handling and retry logic
|
||||
static HttpResponse socket_http_post_with_interrupt(const HTTPRequest *request) {
|
||||
#if R2__WINDOWS__
|
||||
r_sys_setenv ("R2_CURL", "1");
|
||||
#endif
|
||||
#if R2__UNIX__
|
||||
signal (SIGALRM, r2ai_http_sigint_handler);
|
||||
alarm (request->config.timeout); // Use configured timeout
|
||||
#endif
|
||||
int code = 0;
|
||||
int rlen = 0;
|
||||
char *result = r_socket_http_post (request->url, (const char **)request->headers, request->data, &code, &rlen);
|
||||
#if R2__UNIX__
|
||||
alarm (0);
|
||||
#endif
|
||||
if (r2ai_http_interrupted) {
|
||||
R_LOG_DEBUG ("HTTP request was interrupted by user");
|
||||
free (result);
|
||||
return (HttpResponse){ .body = NULL, .code = -1, .length = 0 };
|
||||
}
|
||||
return (HttpResponse){ .body = result, .code = code, .length = rlen };
|
||||
}
|
||||
|
||||
// Socket implementation for GET requests
|
||||
static HttpResponse socket_http_get_with_interrupt(const HTTPRequest *request) {
|
||||
#if R2__WINDOWS__
|
||||
r_sys_setenv ("R2_CURL", "1");
|
||||
#endif
|
||||
#if R2__UNIX__
|
||||
signal (SIGALRM, r2ai_http_sigint_handler);
|
||||
alarm (request->config.timeout); // Use configured timeout
|
||||
#endif
|
||||
// Make the request - use r_socket_http_get if available
|
||||
int code = 0;
|
||||
int rlen = 0;
|
||||
char *result = r_socket_http_get (request->url, (const char **)request->headers, &code, &rlen);
|
||||
#if R2__UNIX__
|
||||
alarm (0);
|
||||
#endif
|
||||
if (r2ai_http_interrupted) {
|
||||
R_LOG_DEBUG ("HTTP request was interrupted by user");
|
||||
free (result);
|
||||
return (HttpResponse){ .body = NULL, .code = -1, .length = 0 };
|
||||
}
|
||||
return (HttpResponse){ .body = result, .code = code, .length = rlen };
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
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))
|
||||
-151
@@ -1,151 +0,0 @@
|
||||
#include "r2ai.h"
|
||||
// TODO: move into r2/libr/util/json.c
|
||||
//
|
||||
// Helper function to convert RJson to PJ without draining
|
||||
R_API PJ *r_json_to_pj(const RJson *json, PJ *existing_pj) {
|
||||
if (!json) {
|
||||
return existing_pj;
|
||||
}
|
||||
|
||||
PJ *pj = existing_pj? existing_pj: pj_new ();
|
||||
if (!pj) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
switch (json->type) {
|
||||
case R_JSON_STRING:
|
||||
pj_s (pj, json->str_value);
|
||||
break;
|
||||
case R_JSON_INTEGER:
|
||||
pj_n (pj, json->num.u_value);
|
||||
break;
|
||||
case R_JSON_DOUBLE:
|
||||
pj_d (pj, json->num.dbl_value);
|
||||
break;
|
||||
case R_JSON_BOOLEAN:
|
||||
pj_b (pj, json->num.u_value);
|
||||
break;
|
||||
case R_JSON_NULL:
|
||||
pj_null (pj);
|
||||
break;
|
||||
case R_JSON_OBJECT:
|
||||
if (!existing_pj) {
|
||||
pj_o (pj);
|
||||
}
|
||||
// Handle object's properties
|
||||
const RJson *prop = json->children.first;
|
||||
while (prop) {
|
||||
if (prop->key) {
|
||||
switch (prop->type) {
|
||||
case R_JSON_STRING:
|
||||
pj_ks (pj, prop->key, prop->str_value);
|
||||
break;
|
||||
case R_JSON_INTEGER:
|
||||
pj_kn (pj, prop->key, prop->num.u_value);
|
||||
break;
|
||||
case R_JSON_DOUBLE:
|
||||
pj_kd (pj, prop->key, prop->num.dbl_value);
|
||||
break;
|
||||
case R_JSON_BOOLEAN:
|
||||
pj_kb (pj, prop->key, prop->num.u_value);
|
||||
break;
|
||||
case R_JSON_NULL:
|
||||
pj_knull (pj, prop->key);
|
||||
break;
|
||||
case R_JSON_OBJECT:
|
||||
pj_ko (pj, prop->key);
|
||||
if (!r_json_to_pj (prop, pj)) {
|
||||
if (!existing_pj) {
|
||||
pj_free (pj);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
pj_end (pj);
|
||||
break;
|
||||
case R_JSON_ARRAY:
|
||||
pj_ka (pj, prop->key);
|
||||
if (!r_json_to_pj (prop, pj)) {
|
||||
if (!existing_pj) {
|
||||
pj_free (pj);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
pj_end (pj);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
prop = prop->next;
|
||||
}
|
||||
if (!existing_pj) {
|
||||
pj_end (pj);
|
||||
}
|
||||
break;
|
||||
case R_JSON_ARRAY:
|
||||
if (!existing_pj) {
|
||||
pj_a (pj);
|
||||
}
|
||||
// Handle array items
|
||||
const RJson *item = json->children.first;
|
||||
while (item) {
|
||||
switch (item->type) {
|
||||
case R_JSON_STRING:
|
||||
pj_s (pj, item->str_value);
|
||||
break;
|
||||
case R_JSON_INTEGER:
|
||||
pj_n (pj, item->num.u_value);
|
||||
break;
|
||||
case R_JSON_DOUBLE:
|
||||
pj_d (pj, item->num.dbl_value);
|
||||
break;
|
||||
case R_JSON_BOOLEAN:
|
||||
pj_b (pj, item->num.u_value);
|
||||
break;
|
||||
case R_JSON_NULL:
|
||||
pj_null (pj);
|
||||
break;
|
||||
case R_JSON_OBJECT:
|
||||
pj_o (pj);
|
||||
if (!r_json_to_pj (item, pj)) {
|
||||
if (!existing_pj) {
|
||||
pj_free (pj);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
pj_end (pj);
|
||||
break;
|
||||
case R_JSON_ARRAY:
|
||||
pj_a (pj);
|
||||
if (!r_json_to_pj (item, pj)) {
|
||||
if (!existing_pj) {
|
||||
pj_free (pj);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
pj_end (pj);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
item = item->next;
|
||||
}
|
||||
if (!existing_pj) {
|
||||
pj_end (pj);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (!existing_pj) {
|
||||
pj_free (pj);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return pj;
|
||||
}
|
||||
|
||||
// Helper function to clone RJson to string
|
||||
R_API char *r_json_to_string(const RJson *json) {
|
||||
PJ *pj = r_json_to_pj (json, NULL);
|
||||
return pj? pj_drain (pj): NULL;
|
||||
}
|
||||
@@ -1,385 +0,0 @@
|
||||
#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", R2AI_API_OPENAI_COMPATIBLE, true, true },
|
||||
{ "gemini", "https://generativelanguage.googleapis.com/v1beta", R2AI_API_GEMINI, true, false },
|
||||
{ "anthropic", "https://api.anthropic.com/v1", R2AI_API_ANTHROPIC, true, false },
|
||||
{ "ollama", "http://localhost:11434/api", R2AI_API_OLLAMA, false, true },
|
||||
{ "openapi", "http://127.0.0.1:11434", R2AI_API_OPENAI_COMPATIBLE, false, false },
|
||||
{ "xai", "https://api.x.ai/v1", R2AI_API_OPENAI_COMPATIBLE, true, true },
|
||||
{ "openrouter", "https://openrouter.ai/api/v1", R2AI_API_OPENAI_COMPATIBLE, true, true },
|
||||
{ "groq", "https://api.groq.com/openai/v1", R2AI_API_OPENAI_COMPATIBLE, true, true },
|
||||
{ "mistral", "https://api.mistral.ai/v1", R2AI_API_OPENAI_COMPATIBLE, true, true },
|
||||
{ "deepseek", "https://api.deepseek.com/v1", R2AI_API_OPENAI_COMPATIBLE, true, true },
|
||||
{ NULL, NULL, R2AI_API_OPENAI_COMPATIBLE, 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 && r_list_length (args.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_apikeys_get (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 the %s provider. Use r2ai -K", 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);
|
||||
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) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
switch (p->api_type) {
|
||||
case R2AI_API_ANTHROPIC:
|
||||
res = r2ai_anthropic (cps, args);
|
||||
break;
|
||||
case R2AI_API_GEMINI:
|
||||
res = r2ai_gemini (cps, args);
|
||||
break;
|
||||
case R2AI_API_OPENAI_COMPATIBLE:
|
||||
case R2AI_API_OLLAMA:
|
||||
default:
|
||||
res = r2ai_openai (cps, args);
|
||||
break;
|
||||
}
|
||||
if (context_pullback != -1) {
|
||||
R2AI_Message *msg = r_list_get_n (args.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;
|
||||
}
|
||||
|
||||
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 (p->supports_custom_baseurl) {
|
||||
const char *host = r_config_get (core->config, "r2ai.baseurl");
|
||||
if (R_STR_ISNOTEMPTY (host)) {
|
||||
if (r_str_startswith (host, "http")) {
|
||||
if (p->api_type == R2AI_API_OPENAI_COMPATIBLE) {
|
||||
return r_str_newf ("%s/v1", host);
|
||||
}
|
||||
return r_str_newf ("%s/api", host);
|
||||
}
|
||||
if (p->api_type == R2AI_API_OPENAI_COMPATIBLE) {
|
||||
return r_str_newf ("http://%s/v1", host);
|
||||
}
|
||||
return r_str_newf ("http://%s/api", host);
|
||||
}
|
||||
}
|
||||
|
||||
return p->url;
|
||||
}
|
||||
R_IPI RList *r2ai_fetch_available_models(RCore *core, const char *provider) {
|
||||
if (!provider) {
|
||||
return NULL;
|
||||
}
|
||||
const char *purl = r2ai_get_provider_url (core, provider);
|
||||
if (!purl) {
|
||||
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_apikeys_get (provider);
|
||||
}
|
||||
|
||||
char *models_url = NULL;
|
||||
int code = 0;
|
||||
char *response = NULL;
|
||||
|
||||
// Special handling for Gemini
|
||||
if (!strcmp (provider, "gemini")) {
|
||||
if (!api_key) {
|
||||
return NULL;
|
||||
}
|
||||
models_url = r_str_newf ("%s/models?key=%s", purl, api_key);
|
||||
const char *headers[2] = { "Content-Type: application/json", NULL };
|
||||
R_LOG_DEBUG ("GET %s", models_url);
|
||||
response = r2ai_http_get (core, models_url, headers, &code, NULL);
|
||||
} else {
|
||||
// Create models endpoint URL
|
||||
const R2AIProvider *prov = r2ai_get_provider (provider);
|
||||
const bool usetags = (prov && prov->api_type == R2AI_API_OLLAMA);
|
||||
models_url = r_str_newf ("%s/%s", purl, usetags? "tags": "models");
|
||||
|
||||
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->api_type == R2AI_API_ANTHROPIC) {
|
||||
// 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);
|
||||
} 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);
|
||||
free (api_key);
|
||||
|
||||
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 = NULL;
|
||||
|
||||
if (!strcmp (provider, "gemini")) {
|
||||
// Gemini has "models" array directly
|
||||
data = r_json_get (json, "models");
|
||||
} else {
|
||||
const R2AIProvider *prov = r2ai_get_provider (provider);
|
||||
const bool usetags = (prov && prov->api_type == R2AI_API_OLLAMA);
|
||||
data = r_json_get (json, usetags? "models": "data");
|
||||
}
|
||||
|
||||
if (data && data->type == R_JSON_ARRAY) {
|
||||
const RJson *model_item = data->children.first;
|
||||
while (model_item) {
|
||||
const RJson *id = NULL;
|
||||
char *model_id = NULL;
|
||||
|
||||
if (!strcmp (provider, "gemini")) {
|
||||
// Gemini: extract model ID from "name" field (e.g., "models/gemini-1.5-flash" -> "gemini-1.5-flash")
|
||||
const RJson *name = r_json_get (model_item, "name");
|
||||
if (name && name->type == R_JSON_STRING && R_STR_ISNOTEMPTY (name->str_value)) {
|
||||
char *s = strdup (name->str_value);
|
||||
RList *parts = r_str_split_list (s, "/", 0);
|
||||
if (parts && r_list_length (parts) > 1) {
|
||||
model_id = strdup ((char *)r_list_get_n (parts, r_list_length (parts) - 1));
|
||||
} else {
|
||||
model_id = strdup (name->str_value);
|
||||
}
|
||||
r_list_free (parts);
|
||||
// Only include Gemini models
|
||||
if (!strstr (model_id, "gemini")) {
|
||||
free (model_id);
|
||||
model_id = NULL;
|
||||
}
|
||||
free (s);
|
||||
}
|
||||
} else {
|
||||
const R2AIProvider *prov = r2ai_get_provider (provider);
|
||||
if (prov && prov->api_type == R2AI_API_OLLAMA) {
|
||||
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)) {
|
||||
model_id = strdup (id->str_value);
|
||||
}
|
||||
}
|
||||
|
||||
if (model_id) {
|
||||
R_LOG_DEBUG ("Model: %s", model_id);
|
||||
r_list_append (models, model_id);
|
||||
}
|
||||
model_item = model_item->next;
|
||||
}
|
||||
}
|
||||
r_json_free (json);
|
||||
}
|
||||
|
||||
free (response);
|
||||
return models;
|
||||
}
|
||||
|
||||
R_IPI void r2ai_list_providers(RCore *core, RStrBuf *sb) {
|
||||
for (size_t 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);
|
||||
}
|
||||
+18
-241
@@ -1,254 +1,31 @@
|
||||
/* Copyright r2ai - 2023-2025 - pancake */
|
||||
|
||||
#include "r2ai_priv.h"
|
||||
#include <r_core.h>
|
||||
#include "r2ai.h"
|
||||
|
||||
#define CLIPPY "?E"
|
||||
// #define CLIPPY "?EC" // pico le croco
|
||||
|
||||
static void show_help() {
|
||||
printf (
|
||||
"Usage: r2ai [-vhp:m:q:Eb:Kc:f:ie] <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"
|
||||
" -b <url> Set base URL for provider API\n"
|
||||
" -c <command> Execute command after loading file (can be used multiple times)\n"
|
||||
" -f <file> Load file with r2 API\n"
|
||||
" -i <script> Load and interpret script file before executing commands\n"
|
||||
" -e <var=value> Set configuration variable\n"
|
||||
" -E Edit the r2ai rc file\n"
|
||||
" -K Edit the API keys file\n");
|
||||
}
|
||||
|
||||
static void show_version() {
|
||||
printf ("r2ai " R2AI_VERSION "\n");
|
||||
}
|
||||
|
||||
static char *build_conversation(RList *conversation) {
|
||||
RListIter *iter;
|
||||
char *msg;
|
||||
RStrBuf *sb = r_strbuf_new ("");
|
||||
r_list_foreach (conversation, iter, msg) {
|
||||
r_strbuf_appendf (sb, "%s\n", msg);
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 2) {
|
||||
eprintf ("Usage: r2ai <prompt>\n");
|
||||
return 1;
|
||||
}
|
||||
return r_strbuf_drain (sb);
|
||||
}
|
||||
|
||||
static void r2ai_repl(RCorePluginSession *cps, const char *provider, const char *model, RList *conversation) {
|
||||
RCore *core = cps->core;
|
||||
r_line_set_prompt (core->cons->line, "[r2ai]> ");
|
||||
while (true) {
|
||||
const char *input = r_line_readline (core->cons);
|
||||
if (r_cons_is_breaked (core->cons) || R_STR_ISEMPTY (input)) {
|
||||
break;
|
||||
}
|
||||
if (input[0] == '!') {
|
||||
// Execute shell command
|
||||
system (input + 1);
|
||||
} else if (input[0] == ':') {
|
||||
// Run radare2 command
|
||||
char *res = r_core_cmd_str (core, input + 1);
|
||||
if (res) {
|
||||
r_cons_println (core->cons, res);
|
||||
free (res);
|
||||
}
|
||||
} else if (input[0] == 'q') {
|
||||
if (r_cons_yesno (core->cons, 'y', "Do you want to quit? (Y/n)")) {
|
||||
break;
|
||||
}
|
||||
} else if (input[0] == '-') {
|
||||
cmd_r2ai (cps, input);
|
||||
} else {
|
||||
// Send message to LLM as part of conversation
|
||||
r_list_append (conversation, r_str_newf ("User: %s", input));
|
||||
char *full_prompt = build_conversation (conversation);
|
||||
char *err = NULL;
|
||||
R2AIArgs args = {
|
||||
.input = full_prompt,
|
||||
.provider = provider,
|
||||
.model = model,
|
||||
.error = &err,
|
||||
.dorag = false,
|
||||
};
|
||||
char *res = r2ai (cps, args);
|
||||
if (res) {
|
||||
if (r_config_get_b (core->config, "r2ai.clippy")) {
|
||||
char *cmd = r_str_newf (CLIPPY " %s", res);
|
||||
r_core_cmd_call (core, cmd);
|
||||
free (cmd);
|
||||
} else {
|
||||
r_cons_println (core->cons, res);
|
||||
r_list_append (conversation, r_str_newf ("Assistant: %s", res));
|
||||
}
|
||||
free (res);
|
||||
}
|
||||
if (err) {
|
||||
r_cons_println (core->cons, err);
|
||||
free (err);
|
||||
}
|
||||
free (full_prompt);
|
||||
}
|
||||
r_cons_flush (core->cons);
|
||||
}
|
||||
}
|
||||
const char *prompt = argv[1];
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
int c;
|
||||
const char *provider = NULL;
|
||||
const char *model = NULL;
|
||||
const char *baseurl = NULL;
|
||||
const char *filename = NULL;
|
||||
const char *scriptfile = NULL;
|
||||
RList *conversation = r_list_newf (free);
|
||||
RList *queries = r_list_newf (free);
|
||||
RList *commands = r_list_newf (free);
|
||||
RCore *core = r_core_new ();
|
||||
RCorePluginSession cps = {
|
||||
.core = core
|
||||
char *err = NULL;
|
||||
|
||||
R2AIArgs args = {
|
||||
.input = prompt,
|
||||
.error = &err,
|
||||
.dorag = false,
|
||||
};
|
||||
r2ai_init (&cps);
|
||||
|
||||
RGetopt opt;
|
||||
r_getopt_init (&opt, argc, argv, "vhp:m:q:Eb:Kc:f:ie:");
|
||||
while ((c = r_getopt_next (&opt)) != -1) {
|
||||
switch (c) {
|
||||
case 'p':
|
||||
provider = opt.arg;
|
||||
break;
|
||||
case 'm':
|
||||
model = opt.arg;
|
||||
break;
|
||||
case 'q':
|
||||
r_list_append (queries, strdup (opt.arg));
|
||||
break;
|
||||
case 'b':
|
||||
baseurl = opt.arg;
|
||||
break;
|
||||
case 'c':
|
||||
r_list_append (commands, strdup (opt.arg));
|
||||
break;
|
||||
case 'f':
|
||||
filename = opt.arg;
|
||||
break;
|
||||
case 'i':
|
||||
scriptfile = opt.arg;
|
||||
break;
|
||||
case 'e':
|
||||
{
|
||||
char *var_value = strdup (opt.arg);
|
||||
char *equals = strchr (var_value, '=');
|
||||
if (equals) {
|
||||
*equals = '\0';
|
||||
const char *var = var_value;
|
||||
const char *value = equals + 1;
|
||||
r_config_set (core->config, var, value);
|
||||
} else {
|
||||
r_cons_println (core->cons, "Invalid format for -e. Use var=value.");
|
||||
}
|
||||
free (var_value);
|
||||
}
|
||||
break;
|
||||
case 'E':
|
||||
{
|
||||
char *rc_path = r_file_home (".config/r2ai/rc");
|
||||
r_cons_editor (core->cons, rc_path, NULL);
|
||||
free (rc_path);
|
||||
goto beach;
|
||||
}
|
||||
break;
|
||||
case 'K':
|
||||
{
|
||||
r2ai_apikeys_edit (&cps);
|
||||
goto beach;
|
||||
}
|
||||
break;
|
||||
case 'v':
|
||||
show_version ();
|
||||
goto beach;
|
||||
case 'h':
|
||||
show_help ();
|
||||
goto beach;
|
||||
default:
|
||||
show_help ();
|
||||
goto beach;
|
||||
}
|
||||
char *res = r2ai (core, args);
|
||||
if (res) {
|
||||
R_LOG_INFO (res);
|
||||
free (res);
|
||||
}
|
||||
|
||||
if (baseurl) {
|
||||
r_config_set (core->config, "r2ai.baseurl", baseurl);
|
||||
}
|
||||
if (filename) {
|
||||
char *cmd = r_str_newf ("o %s", filename);
|
||||
r_core_cmd_call (core, cmd);
|
||||
free (cmd);
|
||||
// TODO: use the C api instead of the 'o' command and check for error if file exists etc
|
||||
} else {
|
||||
r_core_cmd0 (core, CLIPPY " Interesting, no files to analyse. Use -f or :o");
|
||||
r_cons_flush (core->cons);
|
||||
}
|
||||
if (scriptfile) {
|
||||
char *cmd = r_str_newf (". %s", scriptfile);
|
||||
r_core_cmd_call (core, cmd);
|
||||
free (cmd);
|
||||
}
|
||||
if (!r_list_empty (commands)) {
|
||||
RListIter *iter;
|
||||
char *cmd;
|
||||
r_list_foreach (commands, iter, cmd) {
|
||||
char *res = r_core_cmd_str (core, cmd);
|
||||
if (res) {
|
||||
r_cons_println (core->cons, res);
|
||||
free (res);
|
||||
}
|
||||
}
|
||||
}
|
||||
r_config_set_b (core->config, "r2ai.utf8", false);
|
||||
r_config_set_b (core->config, "r2ai.clippy", true);
|
||||
if (provider) {
|
||||
r_config_set (core->config, "r2ai.api", provider);
|
||||
}
|
||||
if (model) {
|
||||
r_config_set (core->config, "r2ai.model", model);
|
||||
}
|
||||
|
||||
// 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) {
|
||||
r2ai_repl (&cps, provider, model, conversation);
|
||||
} else {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
beach:
|
||||
r_list_free (conversation);
|
||||
r_list_free (queries);
|
||||
r_list_free (commands);
|
||||
r2ai_fini (&cps);
|
||||
free (err);
|
||||
r_core_free (core);
|
||||
return 0;
|
||||
}
|
||||
+17
-17
@@ -28,20 +28,20 @@ R_API RMarkdownTheme r2ai_markdown_theme_default(void) {
|
||||
.code_inline = "\x1b[48;5;236m", // Dark gray background for inline code
|
||||
.code_block = "\x1b[48;5;235m", // Slightly darker gray for code blocks
|
||||
// Heading colors
|
||||
.heading1 = "\x1b[1;4" Color_RED, // Bold, underlined, red
|
||||
.heading2 = "\x1b[1;4" Color_GREEN, // Bold, underlined, green
|
||||
.heading3 = "\x1b[1;4" Color_YELLOW, // Bold, underlined, yellow
|
||||
.heading4 = "\x1b[1;4" Color_BLUE, // Bold, underlined, blue
|
||||
.heading5 = "\x1b[1;4" Color_MAGENTA, // Bold, underlined, magenta
|
||||
.heading6 = "\x1b[1;4" Color_CYAN, // Bold, underlined, cyan
|
||||
// List items
|
||||
.heading1 = "\x1b[1;4;31m", // Bold, underlined, red
|
||||
.heading2 = "\x1b[1;4;32m", // Bold, underlined, green
|
||||
.heading3 = "\x1b[1;4;33m", // Bold, underlined, yellow
|
||||
.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_bullet = "• ", // Bullet character
|
||||
.list_number = "\x1b[1m%s." Color_RESET " ", // Bold number
|
||||
// Checkbox states
|
||||
.checkbox_checked = Color_GREEN "[✓]" Color_RESET " ", // Green checkmark
|
||||
.checkbox_unchecked = Color_GRAY "[ ]" Color_RESET " ", // Gray empty box
|
||||
// Reset code
|
||||
.reset = Color_RESET
|
||||
.list_number = "\x1b[1m%s.\x1b[0m ", // Bold number
|
||||
// Checkbox states
|
||||
.checkbox_checked = "\x1b[32m[✓]\x1b[0m ", // Green checkmark
|
||||
.checkbox_unchecked = "\x1b[90m[ ]\x1b[0m ", // Gray empty box
|
||||
// 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 {
|
||||
|
||||
+8
-14
@@ -1,5 +1,5 @@
|
||||
project('r2ai', 'c',
|
||||
version : '1.3.2',
|
||||
version : '0.1.0',
|
||||
default_options : ['warning_level=2', 'c_std=c11']
|
||||
)
|
||||
|
||||
@@ -185,21 +185,15 @@ elif cc.get_id() == 'msvc'
|
||||
endif
|
||||
|
||||
srcs = files(
|
||||
'anthropic.c',
|
||||
'auto.c',
|
||||
'apikeys.c',
|
||||
'gemini.c',
|
||||
'http.c',
|
||||
'json.c',
|
||||
'llm.c',
|
||||
'markdown.c',
|
||||
'messages.c',
|
||||
'openai.c',
|
||||
'query.c',
|
||||
'r2ai.c',
|
||||
'rawtools.c',
|
||||
'tools.c',
|
||||
'auto.c',
|
||||
'vdb.c',
|
||||
'tools.c',
|
||||
'messages.c',
|
||||
'anthropic.c',
|
||||
'r2ai_http.c',
|
||||
'openai.c',
|
||||
'markdown.c',
|
||||
)
|
||||
|
||||
# Shared library (plugin)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
option('r2_prefix',
|
||||
type: 'string',
|
||||
value: '/usr/local',
|
||||
value: '',
|
||||
description: 'Path prefix where radare2 is installed (include/ and lib/).'
|
||||
)
|
||||
|
||||
+457
-199
@@ -2,200 +2,273 @@
|
||||
|
||||
#include "r2ai.h"
|
||||
|
||||
R_API void r2ai_tool_call_free(R2AI_ToolCall *tc) {
|
||||
if (tc) {
|
||||
free ((void *)tc->id);
|
||||
free ((void *)tc->name);
|
||||
free ((void *)tc->arguments);
|
||||
free (tc);
|
||||
}
|
||||
}
|
||||
// AITODO: remove globals
|
||||
// Static global message store for session persistence
|
||||
static R2AI_Messages *conversation = NULL;
|
||||
|
||||
R_API RList *r2ai_content_blocks_new(void) {
|
||||
RList *cb = r_list_new ();
|
||||
if (!cb) {
|
||||
return NULL;
|
||||
}
|
||||
cb->free = (RListFree)free; // ContentBlocks contain pointers to structs, not structs themselves
|
||||
return cb;
|
||||
}
|
||||
|
||||
R_API void r2ai_content_blocks_free(RList *cb) {
|
||||
if (!cb) {
|
||||
return;
|
||||
}
|
||||
r_list_free (cb);
|
||||
}
|
||||
#define INITIAL_CAPACITY 8
|
||||
#define GROWTH_FACTOR 1.5
|
||||
|
||||
R_API void r2ai_message_free(R2AI_Message *msg) {
|
||||
if (!msg) {
|
||||
return;
|
||||
}
|
||||
|
||||
free (msg->role);
|
||||
free ((void *)msg->content); // maybe double free
|
||||
free (msg->content);
|
||||
free (msg->reasoning_content);
|
||||
free (msg->tool_call_id);
|
||||
|
||||
// Free tool calls
|
||||
if (msg->tool_calls) {
|
||||
r_list_free (msg->tool_calls);
|
||||
for (int i = 0; i < msg->n_tool_calls; i++) {
|
||||
free ((void *)msg->tool_calls[i].id);
|
||||
free ((void *)msg->tool_calls[i].name);
|
||||
free ((void *)msg->tool_calls[i].arguments);
|
||||
}
|
||||
free ((void *)msg->tool_calls);
|
||||
}
|
||||
|
||||
if (msg->content_blocks) {
|
||||
r2ai_content_blocks_free (msg->content_blocks);
|
||||
for (int i = 0; i < msg->content_blocks->n_blocks; i++) {
|
||||
R2AI_ContentBlock *block = &msg->content_blocks->blocks[i];
|
||||
free (block->type);
|
||||
free (block->data);
|
||||
free (block->thinking);
|
||||
free (block->signature);
|
||||
free (block->text);
|
||||
free (block->id);
|
||||
free (block->name);
|
||||
free (block->input);
|
||||
}
|
||||
free (msg->content_blocks->blocks);
|
||||
free (msg->content_blocks);
|
||||
}
|
||||
free (msg);
|
||||
}
|
||||
|
||||
// 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
|
||||
// Initialize conversation container (call this during plugin init)
|
||||
R_API void r2ai_conversation_init(void) {
|
||||
if (conversation) {
|
||||
// Already initialized
|
||||
return;
|
||||
}
|
||||
|
||||
state->conversation = r2ai_msgs_new ();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
R_API RList *r2ai_conversation_get(R2AI_State *state) {
|
||||
return state? state->conversation: NULL;
|
||||
// Get the conversation instance (returns NULL if not initialized)
|
||||
R_API R2AI_Messages *r2ai_conversation_get(void) {
|
||||
return conversation;
|
||||
}
|
||||
|
||||
// Create a new temporary messages container
|
||||
R_API RList *r2ai_msgs_new(void) {
|
||||
RList *msgs = r_list_new ();
|
||||
R_API R2AI_Messages *r2ai_msgs_new(void) {
|
||||
R2AI_Messages *msgs = R_NEW0 (R2AI_Messages);
|
||||
if (!msgs) {
|
||||
return NULL;
|
||||
}
|
||||
msgs->free = (RListFree)r2ai_message_free;
|
||||
msgs->cap_messages = INITIAL_CAPACITY;
|
||||
msgs->messages = R_NEWS0 (R2AI_Message, msgs->cap_messages);
|
||||
if (!msgs->messages) {
|
||||
R_FREE (msgs);
|
||||
return NULL;
|
||||
}
|
||||
return msgs;
|
||||
}
|
||||
|
||||
R_API void r2ai_msgs_free(RList *msgs) {
|
||||
if (msgs) {
|
||||
r_list_free (msgs);
|
||||
R_API void r2ai_msgs_free(R2AI_Messages *msgs) {
|
||||
if (!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_FREE (msgs);
|
||||
}
|
||||
|
||||
// Free the conversation when plugin is unloaded
|
||||
R_API void r2ai_conversation_free(R2AI_State *state) {
|
||||
if (state && state->conversation) {
|
||||
r2ai_msgs_free (state->conversation);
|
||||
state->conversation = NULL;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// Clear messages in a container without freeing the container itself
|
||||
R_API void r2ai_msgs_clear(RList *msgs) {
|
||||
R_API void r2ai_msgs_clear(R2AI_Messages *msgs) {
|
||||
if (!msgs) {
|
||||
return;
|
||||
}
|
||||
r_list_purge (msgs);
|
||||
|
||||
for (int i = 0; i < msgs->n_messages; i++) {
|
||||
r2ai_message_free (&msgs->messages[i]);
|
||||
}
|
||||
msgs->n_messages = 0;
|
||||
}
|
||||
|
||||
R_API bool r2ai_msgs_add(RList *msgs, const R2AI_Message *msg) {
|
||||
R_API bool r2ai_msgs_add(R2AI_Messages *msgs, const R2AI_Message *msg) {
|
||||
if (!msgs || !msg) {
|
||||
return false;
|
||||
}
|
||||
|
||||
R2AI_Message *new_msg = R_NEW0 (R2AI_Message);
|
||||
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) {
|
||||
RList *cb = r2ai_content_blocks_new ();
|
||||
if (!cb) {
|
||||
r2ai_message_free (new_msg);
|
||||
free (new_msg);
|
||||
// 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;
|
||||
}
|
||||
RListIter *iter;
|
||||
R2AI_ContentBlock *src;
|
||||
r_list_foreach (msg->content_blocks, iter, src) {
|
||||
R2AI_ContentBlock *dst = R_NEW0 (R2AI_ContentBlock);
|
||||
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;
|
||||
r_list_append (cb, dst);
|
||||
}
|
||||
new_msg->content_blocks = cb;
|
||||
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));
|
||||
}
|
||||
|
||||
new_msg->tool_call_id = msg->tool_call_id? strdup (msg->tool_call_id): NULL;
|
||||
new_msg->tool_calls = r_list_new ();
|
||||
if (!new_msg->tool_calls) {
|
||||
r2ai_message_free (new_msg);
|
||||
free (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;
|
||||
|
||||
if (msg->content_blocks) {
|
||||
R2AI_ContentBlocks *cb = R_NEW0 (R2AI_ContentBlocks);
|
||||
if (!cb) {
|
||||
r2ai_message_free (dest);
|
||||
msgs->n_messages--;
|
||||
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--;
|
||||
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;
|
||||
}
|
||||
dest->content_blocks = cb;
|
||||
}
|
||||
new_msg->tool_calls->free = (RListFree)r2ai_tool_call_free;
|
||||
|
||||
dest->tool_call_id = msg->tool_call_id ? strdup (msg->tool_call_id) : NULL;
|
||||
dest->tool_calls = NULL;
|
||||
dest->n_tool_calls = 0;
|
||||
|
||||
// Copy tool calls if any
|
||||
if (msg->tool_calls) {
|
||||
RListIter *iter;
|
||||
R2AI_ToolCall *src_tc;
|
||||
r_list_foreach (msg->tool_calls, iter, src_tc) {
|
||||
R2AI_ToolCall *dst_tc = R_NEW0 (R2AI_ToolCall);
|
||||
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 (new_msg->tool_calls, dst_tc);
|
||||
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--;
|
||||
return false;
|
||||
}
|
||||
|
||||
dest->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];
|
||||
|
||||
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, new_msg);
|
||||
return true;
|
||||
}
|
||||
|
||||
R_API bool r2ai_msgs_add_tool_call(RList *msgs, const R2AI_ToolCall *tc) {
|
||||
if (!msgs || !tc || r_list_empty (msgs)) {
|
||||
R_API bool r2ai_msgs_add_tool_call(R2AI_Messages *msgs, const R2AI_ToolCall *tc) {
|
||||
if (!msgs || !tc || msgs->n_messages == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
R2AI_Message *msg = r_list_get_n (msgs, r_list_length (msgs) - 1);
|
||||
R2AI_Message *msg = &msgs->messages[msgs->n_messages - 1];
|
||||
|
||||
// Ensure tool_calls list exists
|
||||
if (!msg->tool_calls) {
|
||||
msg->tool_calls = r_list_new ();
|
||||
// Allocate or resize the tool_calls array
|
||||
if (msg->n_tool_calls == 0) {
|
||||
msg->tool_calls = R_NEWS0 (R2AI_ToolCall, 1);
|
||||
if (!msg->tool_calls) {
|
||||
return false;
|
||||
}
|
||||
msg->tool_calls->free = (RListFree)r2ai_tool_call_free;
|
||||
} else {
|
||||
R2AI_ToolCall *new_tool_calls = realloc (
|
||||
(void *)msg->tool_calls,
|
||||
sizeof (R2AI_ToolCall) * (msg->n_tool_calls + 1));
|
||||
if (!new_tool_calls) {
|
||||
return false;
|
||||
}
|
||||
msg->tool_calls = new_tool_calls;
|
||||
// Zero the new element
|
||||
memset ((void *)&msg->tool_calls[msg->n_tool_calls], 0, sizeof (R2AI_ToolCall));
|
||||
}
|
||||
|
||||
// Copy the tool call
|
||||
R2AI_ToolCall *dst_tc = R_NEW0 (R2AI_ToolCall);
|
||||
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;
|
||||
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;
|
||||
|
||||
r_list_append (msg->tool_calls, dst_tc);
|
||||
msg->n_tool_calls++;
|
||||
return true;
|
||||
}
|
||||
|
||||
R_API bool r2ai_msgs_from_response(RList *msgs, const char *json_str) {
|
||||
R_API bool r2ai_msgs_from_response(R2AI_Messages *msgs, const char *json_str) {
|
||||
if (!msgs || !json_str) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool result = false;
|
||||
// r_json_parse expects (and modifies) non-const char*, so we need to cast it
|
||||
// r_json_parse expects non-const char*, so we need to cast it
|
||||
RJson *json = r_json_parse ((char *)json_str);
|
||||
if (json) {
|
||||
result = r2ai_msgs_from_json (msgs, json);
|
||||
r_json_free (json);
|
||||
if (!json) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool result = r2ai_msgs_from_json (msgs, json);
|
||||
r_json_free (json);
|
||||
return result;
|
||||
}
|
||||
|
||||
R_API bool r2ai_msgs_from_json(RList *msgs, const RJson *json) {
|
||||
R_API bool r2ai_msgs_from_json(R2AI_Messages *msgs, const RJson *json) {
|
||||
if (!msgs || !json) {
|
||||
return false;
|
||||
}
|
||||
@@ -221,28 +294,31 @@ R_API bool r2ai_msgs_from_json(RList *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;
|
||||
|
||||
if (content_blocks && content_blocks->type == R_JSON_ARRAY && content_blocks->children.count > 0) {
|
||||
RList *cb = r2ai_content_blocks_new ();
|
||||
R2AI_ContentBlocks *cb = R_NEW0 (R2AI_ContentBlocks);
|
||||
if (!cb) {
|
||||
r2ai_message_free (&new_msg);
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 0; i < content_blocks->children.count; i++) {
|
||||
cb->n_blocks = content_blocks->children.count;
|
||||
cb->blocks = R_NEWS0 (R2AI_ContentBlock, cb->n_blocks);
|
||||
if (!cb->blocks) {
|
||||
free (cb);
|
||||
r2ai_message_free (&new_msg);
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < cb->n_blocks; i++) {
|
||||
const RJson *block = r_json_item (content_blocks, i);
|
||||
if (!block) {
|
||||
continue;
|
||||
}
|
||||
R2AI_ContentBlock *dst = R_NEW0 (R2AI_ContentBlock);
|
||||
if (!dst) {
|
||||
r2ai_content_blocks_free (cb);
|
||||
r2ai_message_free (&new_msg);
|
||||
return false;
|
||||
}
|
||||
R2AI_ContentBlock *dst = &cb->blocks[i];
|
||||
const RJson *type = r_json_get (block, "type");
|
||||
const RJson *data = r_json_get (block, "data");
|
||||
const RJson *thinking = r_json_get (block, "thinking");
|
||||
@@ -252,15 +328,14 @@ R_API bool r2ai_msgs_from_json(RList *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;
|
||||
r_list_append (cb, dst);
|
||||
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;
|
||||
}
|
||||
@@ -291,13 +366,12 @@ R_API bool r2ai_msgs_from_json(RList *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)) {
|
||||
r2ai_message_free (&new_msg);
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -305,8 +379,8 @@ R_API bool r2ai_msgs_from_json(RList *msgs, const RJson *json) {
|
||||
return true;
|
||||
}
|
||||
|
||||
R_API char *r2ai_msgs_to_json(const RList *msgs) {
|
||||
if (!msgs || r_list_empty (msgs)) {
|
||||
R_API char *r2ai_msgs_to_json(const R2AI_Messages *msgs) {
|
||||
if (!msgs || msgs->n_messages == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -317,22 +391,16 @@ R_API char *r2ai_msgs_to_json(const RList *msgs) {
|
||||
|
||||
pj_a (pj); // Start array
|
||||
|
||||
RListIter *iter;
|
||||
const R2AI_Message *msg;
|
||||
r_list_foreach (msgs, iter, msg) {
|
||||
for (int i = 0; i < msgs->n_messages; i++) {
|
||||
const R2AI_Message *msg = &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");
|
||||
|
||||
// Content is required for OpenAI API
|
||||
if (msg->content && *msg->content) {
|
||||
if (msg->content) {
|
||||
pj_ks (pj, "content", msg->content);
|
||||
} else if (msg->tool_calls && r_list_length (msg->tool_calls) > 0) {
|
||||
pj_knull (pj, "content");
|
||||
} else {
|
||||
pj_ks (pj, "content", msg->content? msg->content: "");
|
||||
}
|
||||
|
||||
if (msg->reasoning_content) {
|
||||
@@ -345,13 +413,13 @@ R_API char *r2ai_msgs_to_json(const RList *msgs) {
|
||||
}
|
||||
|
||||
// Add tool_calls if present
|
||||
if (msg->tool_calls && r_list_length (msg->tool_calls) > 0) {
|
||||
if (msg->tool_calls && msg->n_tool_calls > 0) {
|
||||
pj_k (pj, "tool_calls");
|
||||
pj_a (pj); // Start tool_calls array
|
||||
|
||||
RListIter *iter;
|
||||
R2AI_ToolCall *tc;
|
||||
r_list_foreach (msg->tool_calls, iter, tc) {
|
||||
for (int j = 0; j < msg->n_tool_calls; j++) {
|
||||
const R2AI_ToolCall *tc = &msg->tool_calls[j];
|
||||
|
||||
pj_o (pj); // Start tool call object
|
||||
|
||||
// Add id if present
|
||||
@@ -367,10 +435,12 @@ R_API char *r2ai_msgs_to_json(const RList *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 (required by OpenAI API)
|
||||
pj_ks (pj, "arguments", tc->arguments? tc->arguments: "{}");
|
||||
// Add arguments if present
|
||||
if (tc->arguments) {
|
||||
pj_ks (pj, "arguments", tc->arguments);
|
||||
}
|
||||
|
||||
pj_end (pj); // End function object
|
||||
pj_end (pj); // End tool call object
|
||||
@@ -388,8 +458,8 @@ R_API char *r2ai_msgs_to_json(const RList *msgs) {
|
||||
return result;
|
||||
}
|
||||
|
||||
R_API char *r2ai_msgs_to_anthropic_json(const RList *msgs) {
|
||||
if (!msgs || r_list_empty (msgs)) {
|
||||
R_API char *r2ai_msgs_to_anthropic_json(const R2AI_Messages *msgs) {
|
||||
if (!msgs || msgs->n_messages == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -400,24 +470,19 @@ R_API char *r2ai_msgs_to_anthropic_json(const RList *msgs) {
|
||||
|
||||
pj_a (pj); // Start array
|
||||
|
||||
RListIter *iter;
|
||||
const R2AI_Message *msg;
|
||||
r_list_foreach (msgs, iter, msg) {
|
||||
for (int i = 0; i < msgs->n_messages; i++) {
|
||||
const R2AI_Message *msg = &msgs->messages[i];
|
||||
|
||||
pj_o (pj); // Start message object
|
||||
|
||||
// Circumvent Anthropic's allergy to system role messages
|
||||
const char *role = msg->role? msg->role: "user";
|
||||
bool is_system_message = !strcmp (role, "system");
|
||||
if (is_system_message) {
|
||||
role = "user";
|
||||
}
|
||||
pj_ks (pj, "role", strcmp (role, "tool") == 0? "user": role);
|
||||
// Add 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
|
||||
RListIter *iter;
|
||||
R2AI_ContentBlock *block;
|
||||
r_list_foreach (msg->content_blocks, iter, block) {
|
||||
for (int j = 0; j < msg->content_blocks->n_blocks; j++) {
|
||||
const R2AI_ContentBlock *block = &msg->content_blocks->blocks[j];
|
||||
pj_o (pj); // Start content block object
|
||||
if (R_STR_ISNOTEMPTY (block->type)) {
|
||||
pj_ks (pj, "type", block->type);
|
||||
@@ -441,8 +506,22 @@ R_API char *r2ai_msgs_to_anthropic_json(const RList *msgs) {
|
||||
pj_ks (pj, "name", block->name);
|
||||
}
|
||||
if (R_STR_ISNOTEMPTY (block->input)) {
|
||||
pj_k (pj, "input");
|
||||
pj_raw (pj, block->input);
|
||||
// Try to parse the input as JSON first
|
||||
char *input_str = strdup (block->input);
|
||||
RJson *input_json = r_json_parse (input_str);
|
||||
if (input_json) {
|
||||
// If it's valid JSON, add it directly
|
||||
pj_ko (pj, "input");
|
||||
r_json_to_pj (input_json, pj);
|
||||
pj_end (pj);
|
||||
r_json_free (input_json);
|
||||
} else {
|
||||
// If not valid JSON, create a basic object with command
|
||||
pj_ko (pj, "input");
|
||||
pj_ks (pj, "command", block->input);
|
||||
pj_end (pj);
|
||||
}
|
||||
free (input_str);
|
||||
}
|
||||
pj_end (pj); // End content block object
|
||||
}
|
||||
@@ -458,35 +537,43 @@ R_API char *r2ai_msgs_to_anthropic_json(const RList *msgs) {
|
||||
pj_ks (pj, "content", msg->content);
|
||||
} else {
|
||||
pj_ks (pj, "type", "text");
|
||||
if (is_system_message) {
|
||||
char *prefixed = r_str_newf ("SYSTEM INSTRUCTIONS: %s", msg->content);
|
||||
pj_ks (pj, "text", prefixed);
|
||||
free (prefixed);
|
||||
} else {
|
||||
pj_ks (pj, "text", msg->content);
|
||||
}
|
||||
pj_ks (pj, "text", msg->content);
|
||||
}
|
||||
pj_end (pj); // End content block object
|
||||
}
|
||||
|
||||
if (msg->tool_calls && r_list_length (msg->tool_calls) > 0) {
|
||||
RListIter *iter;
|
||||
R2AI_ToolCall *tc;
|
||||
r_list_foreach (msg->tool_calls, iter, tc) {
|
||||
pj_o (pj); // Start tool_use content block
|
||||
pj_ks (pj, "type", "tool_use");
|
||||
pj_ks (pj, "id", tc->id? tc->id: "");
|
||||
pj_ks (pj, "name", tc->name? tc->name: "");
|
||||
if (msg->tool_calls && msg->n_tool_calls > 0) {
|
||||
pj_k (pj, "tool_calls");
|
||||
pj_a (pj); // Start tool_calls array
|
||||
|
||||
// Insert the arguments directly as the input object
|
||||
pj_k (pj, "input");
|
||||
if (tc->arguments) {
|
||||
pj_raw (pj, tc->arguments);
|
||||
} else {
|
||||
pj_raw (pj, "{}");
|
||||
for (int j = 0; j < msg->n_tool_calls; j++) {
|
||||
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 : "");
|
||||
|
||||
// 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;
|
||||
|
||||
pj_ko (pj, "input"); // Start input object
|
||||
if (arguments) {
|
||||
for (size_t k = 0; k < arguments->children.count; k++) {
|
||||
const RJson *arg = r_json_item (arguments, k);
|
||||
if (arg && arg->type == R_JSON_STRING) {
|
||||
pj_ks (pj, arg->key, arg->str_value);
|
||||
}
|
||||
}
|
||||
r_json_free (arguments);
|
||||
}
|
||||
pj_end (pj); // End tool_use content block
|
||||
free (arguments_copy);
|
||||
|
||||
pj_end (pj); // End input object
|
||||
pj_end (pj); // End tool_calls object
|
||||
}
|
||||
|
||||
pj_end (pj); // End tool_calls array
|
||||
}
|
||||
pj_end (pj); // End content array
|
||||
}
|
||||
@@ -500,8 +587,8 @@ R_API char *r2ai_msgs_to_anthropic_json(const RList *msgs) {
|
||||
}
|
||||
|
||||
// Function to delete the last N messages from conversation history
|
||||
R_API void r2ai_delete_last_messages(RList *messages, int n) {
|
||||
if (!messages || r_list_length (messages) == 0) {
|
||||
R_API void r2ai_delete_last_messages(R2AI_Messages *messages, int n) {
|
||||
if (!messages || messages->n_messages == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -511,13 +598,184 @@ R_API void r2ai_delete_last_messages(RList *messages, int n) {
|
||||
}
|
||||
|
||||
// Make sure we don't try to delete more messages than exist
|
||||
int len = r_list_length (messages);
|
||||
if (n > len) {
|
||||
n = len;
|
||||
if (n > messages->n_messages) {
|
||||
n = messages->n_messages;
|
||||
}
|
||||
|
||||
// Pop the last n messages
|
||||
for (int i = 0; i < n; i++) {
|
||||
r_list_pop (messages);
|
||||
// Free the last n messages
|
||||
for (int i = messages->n_messages - n; i < messages->n_messages; i++) {
|
||||
r2ai_message_free (&messages->messages[i]);
|
||||
}
|
||||
|
||||
// Update the message count
|
||||
messages->n_messages -= n;
|
||||
}
|
||||
|
||||
// Helper function to convert RJson to PJ without draining
|
||||
R_API PJ *r_json_to_pj(const RJson *json, PJ *existing_pj) {
|
||||
if (!json) {
|
||||
return existing_pj;
|
||||
}
|
||||
|
||||
PJ *pj = existing_pj ? existing_pj : pj_new ();
|
||||
if (!pj) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
switch (json->type) {
|
||||
case R_JSON_STRING:
|
||||
pj_s (pj, json->str_value);
|
||||
break;
|
||||
case R_JSON_INTEGER:
|
||||
pj_n (pj, json->num.u_value);
|
||||
break;
|
||||
case R_JSON_DOUBLE:
|
||||
pj_d (pj, json->num.dbl_value);
|
||||
break;
|
||||
case R_JSON_BOOLEAN:
|
||||
pj_b (pj, json->num.u_value);
|
||||
break;
|
||||
case R_JSON_NULL:
|
||||
pj_null (pj);
|
||||
break;
|
||||
case R_JSON_OBJECT:
|
||||
if (!existing_pj) {
|
||||
pj_o (pj);
|
||||
}
|
||||
// Handle object's properties
|
||||
const RJson *prop = json->children.first;
|
||||
while (prop) {
|
||||
if (prop->key) {
|
||||
switch (prop->type) {
|
||||
case R_JSON_STRING:
|
||||
pj_ks (pj, prop->key, prop->str_value);
|
||||
break;
|
||||
case R_JSON_INTEGER:
|
||||
pj_kn (pj, prop->key, prop->num.u_value);
|
||||
break;
|
||||
case R_JSON_DOUBLE:
|
||||
pj_kd (pj, prop->key, prop->num.dbl_value);
|
||||
break;
|
||||
case R_JSON_BOOLEAN:
|
||||
pj_kb (pj, prop->key, prop->num.u_value);
|
||||
break;
|
||||
case R_JSON_NULL:
|
||||
pj_knull (pj, prop->key);
|
||||
break;
|
||||
case R_JSON_OBJECT:
|
||||
pj_ko (pj, prop->key);
|
||||
if (!r_json_to_pj (prop, pj)) {
|
||||
if (!existing_pj) {
|
||||
pj_free (pj);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
pj_end (pj);
|
||||
break;
|
||||
case R_JSON_ARRAY:
|
||||
pj_ka (pj, prop->key);
|
||||
if (!r_json_to_pj (prop, pj)) {
|
||||
if (!existing_pj) {
|
||||
pj_free (pj);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
pj_end (pj);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
prop = prop->next;
|
||||
}
|
||||
if (!existing_pj) {
|
||||
pj_end (pj);
|
||||
}
|
||||
break;
|
||||
case R_JSON_ARRAY:
|
||||
if (!existing_pj) {
|
||||
pj_a (pj);
|
||||
}
|
||||
// Handle array items
|
||||
const RJson *item = json->children.first;
|
||||
while (item) {
|
||||
switch (item->type) {
|
||||
case R_JSON_STRING:
|
||||
pj_s (pj, item->str_value);
|
||||
break;
|
||||
case R_JSON_INTEGER:
|
||||
pj_n (pj, item->num.u_value);
|
||||
break;
|
||||
case R_JSON_DOUBLE:
|
||||
pj_d (pj, item->num.dbl_value);
|
||||
break;
|
||||
case R_JSON_BOOLEAN:
|
||||
pj_b (pj, item->num.u_value);
|
||||
break;
|
||||
case R_JSON_NULL:
|
||||
pj_null (pj);
|
||||
break;
|
||||
case R_JSON_OBJECT:
|
||||
pj_o (pj);
|
||||
if (!r_json_to_pj (item, pj)) {
|
||||
if (!existing_pj) {
|
||||
pj_free (pj);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
pj_end (pj);
|
||||
break;
|
||||
case R_JSON_ARRAY:
|
||||
pj_a (pj);
|
||||
if (!r_json_to_pj (item, pj)) {
|
||||
if (!existing_pj) {
|
||||
pj_free (pj);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
pj_end (pj);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
item = item->next;
|
||||
}
|
||||
if (!existing_pj) {
|
||||
pj_end (pj);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (!existing_pj) {
|
||||
pj_free (pj);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return pj;
|
||||
}
|
||||
|
||||
// Helper function to clone RJson to string
|
||||
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;
|
||||
}
|
||||
+225
-189
@@ -1,21 +1,104 @@
|
||||
/* r2ai - Copyright 2023-2025 pancake, dnakov */
|
||||
|
||||
#include <r_core.h>
|
||||
#include <r_util/r_json.h>
|
||||
#include "r2ai.h"
|
||||
#include "r2ai_priv.h"
|
||||
|
||||
R_IPI R2AI_ChatResponse *r2ai_openai(RCorePluginSession *cps, R2AIArgs args) {
|
||||
RCore *core = cps->core;
|
||||
args.provider = r_config_get (core->config, "r2ai.api");
|
||||
args.model = r_config_get (core->config, "r2ai.model");
|
||||
// Bit flags for different types of model errors/incompatibilities
|
||||
typedef enum {
|
||||
MODEL_ERROR_NONE = 0,
|
||||
MODEL_ERROR_TEMPERATURE = 1 << 0,
|
||||
// Can add more error types here as needed
|
||||
// MODEL_ERROR_TOP_P = 1 << 1,
|
||||
// MODEL_ERROR_MAX_TOKENS = 1 << 2,
|
||||
// etc.
|
||||
} ModelErrorFlags;
|
||||
|
||||
// Structure to store model compatibility info
|
||||
typedef struct {
|
||||
char *model_id; // Provider:model string
|
||||
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 ();
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (found_flag && compat) {
|
||||
// Update existing entry
|
||||
compat->error_flags |= flag;
|
||||
} else {
|
||||
// Create new entry
|
||||
compat = R_NEW0 (ModelCompat);
|
||||
compat->model_id = strdup (key);
|
||||
compat->error_flags = flag;
|
||||
ht_pp_insert (model_compat_db, key, compat);
|
||||
}
|
||||
free (key);
|
||||
}
|
||||
|
||||
// Free a ModelCompat item (for hash table)
|
||||
static bool model_compat_free_cb(void *user, const void *k, const void *v) {
|
||||
(void)user;
|
||||
(void)k;
|
||||
ModelCompat *compat = (ModelCompat *)v;
|
||||
if (compat) {
|
||||
free (compat->model_id);
|
||||
free (compat);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// 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 R2AI_ChatResponse *r2ai_openai(RCore *core, R2AIArgs args) {
|
||||
// Initialize compatibility database if needed
|
||||
if (!model_compat_db) {
|
||||
model_compat_db = ht_pp_new0 ();
|
||||
}
|
||||
|
||||
const R2AIProvider *provider_info = r2ai_get_provider (args.provider);
|
||||
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;
|
||||
RList *tools = args.tools;
|
||||
const R2AI_Tools *tools = args.tools;
|
||||
// create a temp conversation to include the system prompt and the rest of the messages
|
||||
RList *temp_msgs = r2ai_msgs_new ();
|
||||
R2AI_Messages *temp_msgs = r2ai_msgs_new ();
|
||||
if (!temp_msgs) {
|
||||
if (error) {
|
||||
*error = strdup ("Failed to create temporary messages array");
|
||||
@@ -24,7 +107,7 @@ R_IPI R2AI_ChatResponse *r2ai_openai(RCorePluginSession *cps, R2AIArgs args) {
|
||||
}
|
||||
R2AI_Message system_msg = {
|
||||
.role = "system",
|
||||
.content = (char *)args.system_prompt
|
||||
.content = args.system_prompt
|
||||
};
|
||||
// Add system message if available from args.system_prompt
|
||||
if (R_STR_ISNOTEMPTY (args.system_prompt)) {
|
||||
@@ -32,10 +115,10 @@ R_IPI R2AI_ChatResponse *r2ai_openai(RCorePluginSession *cps, 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 = (char *)args.system_prompt;
|
||||
system_msg.content = args.system_prompt;
|
||||
} else {
|
||||
system_msg.role = "system";
|
||||
system_msg.content = (char *)args.system_prompt;
|
||||
system_msg.content = args.system_prompt;
|
||||
}
|
||||
r2ai_msgs_add (temp_msgs, &system_msg);
|
||||
} else {
|
||||
@@ -48,22 +131,20 @@ R_IPI R2AI_ChatResponse *r2ai_openai(RCorePluginSession *cps, R2AIArgs args) {
|
||||
} else {
|
||||
system_msg.role = "system";
|
||||
}
|
||||
system_msg.content = (char *)sysprompt;
|
||||
system_msg.content = sysprompt;
|
||||
r2ai_msgs_add (temp_msgs, &system_msg);
|
||||
}
|
||||
}
|
||||
if (args.messages) {
|
||||
RListIter *iter;
|
||||
R2AI_Message *msg;
|
||||
r_list_foreach (args.messages, iter, msg) {
|
||||
r2ai_msgs_add (temp_msgs, msg);
|
||||
for (int i = 0; i < args.messages->n_messages; i++) {
|
||||
r2ai_msgs_add (temp_msgs, &args.messages->messages[i]);
|
||||
}
|
||||
} else {
|
||||
R_LOG_WARN ("No messages");
|
||||
}
|
||||
// Safely print debug info about first message
|
||||
if (temp_msgs && !r_list_empty (temp_msgs) && ((R2AI_Message *)r_list_get_n (temp_msgs, 0))->role) {
|
||||
R_LOG_DEBUG ("First message role: %s", ((R2AI_Message *)r_list_get_n (temp_msgs, 0))->role);
|
||||
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 (error) {
|
||||
*error = NULL;
|
||||
@@ -79,16 +160,20 @@ R_IPI R2AI_ChatResponse *r2ai_openai(RCorePluginSession *cps, R2AIArgs args) {
|
||||
headers[0] = "Content-Type: application/json";
|
||||
headers[1] = auth_header;
|
||||
}
|
||||
const char *urlfmt = (provider_info && provider_info->api_type == R2AI_API_OLLAMA)
|
||||
? "%s/chat"
|
||||
: "%s/chat/completions";
|
||||
char *openai_url = r_str_newf (urlfmt, base_url);
|
||||
|
||||
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);
|
||||
|
||||
// Create a messages JSON object, either from input messages or from content
|
||||
char *messages_json = NULL;
|
||||
|
||||
if (temp_msgs && !r_list_empty (temp_msgs)) {
|
||||
R_LOG_DEBUG ("Using input messages: %d messages", r_list_length (temp_msgs));
|
||||
if (temp_msgs && temp_msgs->n_messages > 0) {
|
||||
R_LOG_DEBUG ("Using input messages: %d messages", temp_msgs->n_messages);
|
||||
messages_json = r2ai_msgs_to_json (temp_msgs);
|
||||
if (!messages_json) {
|
||||
if (error) {
|
||||
@@ -107,7 +192,7 @@ R_IPI R2AI_ChatResponse *r2ai_openai(RCorePluginSession *cps, R2AIArgs args) {
|
||||
|
||||
// Convert tools to OpenAI format if available
|
||||
char *openai_tools_json = NULL;
|
||||
if (tools && !r_list_empty (tools)) {
|
||||
if (tools && tools->n_tools > 0) {
|
||||
openai_tools_json = r2ai_tools_to_openai_json (tools);
|
||||
}
|
||||
|
||||
@@ -117,37 +202,36 @@ R_IPI R2AI_ChatResponse *r2ai_openai(RCorePluginSession *cps, R2AIArgs args) {
|
||||
pj_ks (pj, "model", model_name);
|
||||
pj_kb (pj, "stream", false);
|
||||
|
||||
if (provider_info && provider_info->api_type == R2AI_API_OLLAMA) {
|
||||
// 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);
|
||||
|
||||
// Get the JSON for model settings
|
||||
char *model_json = pj_drain (pj);
|
||||
if (!model_json) {
|
||||
if (error) {
|
||||
*error = strdup ("Failed to create model settings JSON");
|
||||
}
|
||||
free (auth_header);
|
||||
free (messages_json);
|
||||
if (openai_tools_json) {
|
||||
free (openai_tools_json);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Manually create the final JSON by combining parts
|
||||
// Remove the closing brace from model_json
|
||||
@@ -181,37 +265,47 @@ R_IPI R2AI_ChatResponse *r2ai_openai(RCorePluginSession *cps, R2AIArgs args) {
|
||||
|
||||
// Save the full JSON to a file for inspection
|
||||
// XXX: only create request/response files when r2ai.debug is set
|
||||
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_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");
|
||||
R_LOG_DEBUG ("OpenAI API request data: %s", complete_json);
|
||||
|
||||
if (r_config_get_b (core->config, "r2ai.debug")) {
|
||||
// Generate curl command for debugging
|
||||
RStrBuf *curl_cmd = r_strbuf_new ("curl -X POST");
|
||||
for (int i = 0; headers[i]; i++) {
|
||||
r_strbuf_appendf (curl_cmd, " -H \"%s\"", headers[i]);
|
||||
}
|
||||
r_strbuf_appendf (curl_cmd, " -d '%s' \"%s\"", complete_json, openai_url);
|
||||
eprintf ("Curl command: %s\n", r_strbuf_get (curl_cmd));
|
||||
r_strbuf_free (curl_cmd);
|
||||
}
|
||||
|
||||
// Make the API call
|
||||
char *res = NULL;
|
||||
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);
|
||||
if (res) {
|
||||
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);
|
||||
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);
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
free (auth_header);
|
||||
free (res);
|
||||
@@ -219,14 +313,9 @@ R_IPI R2AI_ChatResponse *r2ai_openai(RCorePluginSession *cps, R2AIArgs args) {
|
||||
}
|
||||
|
||||
// Save the response for inspection
|
||||
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);
|
||||
if (r_config_get_b (core->config, "r2ai.debug")) {
|
||||
eprintf ("OpenAI API response: %s\n", res);
|
||||
}
|
||||
free (res_path);
|
||||
free (tmpdir);
|
||||
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");
|
||||
R_LOG_DEBUG ("OpenAI API response: %s", res);
|
||||
|
||||
// Parse the response into our messages structure
|
||||
|
||||
@@ -238,136 +327,83 @@ R_IPI R2AI_ChatResponse *r2ai_openai(RCorePluginSession *cps, 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 = NULL;
|
||||
if (provider_info && provider_info->api_type == R2AI_API_OLLAMA) {
|
||||
// Ollama has usage info at top level
|
||||
usage_json = jres;
|
||||
} else {
|
||||
usage_json = r_json_get (jres, "usage");
|
||||
}
|
||||
|
||||
const RJson *usage_json = r_json_get (jres, "usage");
|
||||
if (usage_json && usage_json->type == R_JSON_OBJECT) {
|
||||
if (provider_info && provider_info->api_type == R2AI_API_OLLAMA) {
|
||||
// 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 *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;
|
||||
}
|
||||
}
|
||||
const RJson *message_json = NULL;
|
||||
if (provider_info && provider_info->api_type == R2AI_API_OLLAMA) {
|
||||
// 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 (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 *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");
|
||||
|
||||
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");
|
||||
const RJson *thinking = r_json_get (message_json, "thinking");
|
||||
const RJson *tool_call_id = r_json_get (message_json, "tool_call_id");
|
||||
const RJson *tool_calls = r_json_get (message_json, "tool_calls");
|
||||
// Set the basic message properties
|
||||
message->role = (role && role->type == R_JSON_STRING) ? strdup (role->str_value) : strdup ("assistant");
|
||||
|
||||
// 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 (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);
|
||||
}
|
||||
|
||||
if (reasoning_content && reasoning_content->type == R_JSON_STRING && R_STR_ISNOTEMPTY (reasoning_content->str_value)) {
|
||||
message->reasoning_content = strdup (reasoning_content->str_value);
|
||||
} else if (thinking && thinking->type == R_JSON_STRING && R_STR_ISNOTEMPTY (thinking->str_value)) {
|
||||
message->reasoning_content = strdup (thinking->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_call_id && tool_call_id->type == R_JSON_STRING && R_STR_ISNOTEMPTY (tool_call_id->str_value)) {
|
||||
message->tool_call_id = strdup (tool_call_id->str_value);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
if (tool_calls && tool_calls->type == R_JSON_ARRAY && tool_calls->children.count > 0) {
|
||||
message->tool_calls = r_list_new ();
|
||||
if (message->tool_calls) {
|
||||
message->tool_calls->free = (RListFree)r2ai_tool_call_free;
|
||||
for (size_t i = 0; i < tool_calls->children.count; i++) {
|
||||
const RJson *tool_call = r_json_item (tool_calls, i);
|
||||
if (!tool_call || tool_call->type != R_JSON_OBJECT) {
|
||||
continue;
|
||||
}
|
||||
R2AI_ToolCall *tc = R_NEW0 (R2AI_ToolCall);
|
||||
if (!tc) {
|
||||
continue;
|
||||
}
|
||||
const RJson *tc_id = r_json_get (tool_call, "id");
|
||||
if (tc_id && tc_id->type == R_JSON_STRING && R_STR_ISNOTEMPTY (tc_id->str_value)) {
|
||||
tc->id = strdup (tc_id->str_value);
|
||||
}
|
||||
const RJson *function = r_json_get (tool_call, "function");
|
||||
const RJson *name_json = NULL;
|
||||
const RJson *arguments = NULL;
|
||||
if (function && function->type == R_JSON_OBJECT) {
|
||||
name_json = r_json_get (function, "name");
|
||||
arguments = r_json_get (function, "arguments");
|
||||
} else {
|
||||
name_json = r_json_get (tool_call, "name");
|
||||
arguments = r_json_get (tool_call, "arguments");
|
||||
}
|
||||
if (name_json && name_json->type == R_JSON_STRING && R_STR_ISNOTEMPTY (name_json->str_value)) {
|
||||
tc->name = strdup (name_json->str_value);
|
||||
}
|
||||
if (arguments) {
|
||||
if (arguments->type == R_JSON_STRING && R_STR_ISNOTEMPTY (arguments->str_value)) {
|
||||
tc->arguments = strdup (arguments->str_value);
|
||||
} else {
|
||||
char *arguments_json = r_json_to_string (arguments);
|
||||
if (arguments_json) {
|
||||
tc->arguments = arguments_json;
|
||||
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 (!tc->name && !tc->arguments && !tc->id) {
|
||||
r2ai_tool_call_free (tc);
|
||||
continue;
|
||||
}
|
||||
r_list_append (message->tool_calls, tc);
|
||||
}
|
||||
if (r_list_empty (message->tool_calls)) {
|
||||
r_list_free (message->tool_calls);
|
||||
message->tool_calls = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
// TODO: Handle tool calls if present?
|
||||
}
|
||||
}
|
||||
r_json_free (jres);
|
||||
|
||||
Loaded 100 of 110 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user