refactor(plugins): 抽出 marqdo-plugin-sdk — ABI 前言从 5 份变 1 份
新增工作区成员 crates/marqdo-plugin-sdk:ABI 类型、HOST_* 静态回调、 host_strdup/set_out/set_err/parse_args/reply/host_query_json(_with)/ take_host_string/init_plugin,以及生成四个 #[no_mangle] 导出的 marqdo_plugin! 宏。ABI 常量、符号名、结构体布局一律未变。 5 个插件改为依赖 SDK + 一行宏调用: agent 1237 → 929 (-308) quantum 1045 → 835 (-210) linalg 688 → 524 (-164) llm 405 → 278 (-127) demo 170 → 73 (-97) SDK 新增 232 行 → 净减约 674 行 迁移中发现并修正三次"编译通过但语义已变": 1. 注册项抽取漏 11 项 —— 正则 [a-z_0-9]+ 匹配不到 mcp_server::agent_mcp_serve、 sandbox::* 这类路径限定函数名;改为允许 :: 后重抽。 已用脚本逐项比对 (名称, 参数串, 函数) 三元组:agent 63/63、quantum 41/41、 linalg 31/31、llm 2/2、demo 2/2,全部 0 差异。 2. mcp_client 被误判死模块(删 327 行后才发现 agent_mcp_client 正注册它)→ 恢复。 3. 量子插件两个"未使用"函数删除时连带吃掉 density_from_amps/flat_to_nested → 恢复原文件并改为 #[allow(dead_code)]。 验证:cargo build --release(含 5 个插件)零警告; cargo test --release 609 passed / 0 failed; 量子金样实跑 p00-ok / p11-ok / ping-ok。 文档:doc/design/change-plugin-sdk.md
This commit is contained in:
1 parent
69fcdd6227
commit
fb135ae2f8
16 files changed
+492
-1073
No files matched your search
Generated
+12
@@ -601,6 +601,13 @@ dependencies = [
|
||||
"zip",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "marqdo-plugin-sdk"
|
||||
version = "1.5.2"
|
||||
dependencies = [
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "marqdo-wasm"
|
||||
version = "1.5.2"
|
||||
@@ -614,6 +621,7 @@ name = "marqdo_plugin_agent"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"marqdo-plugin-sdk",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
@@ -621,6 +629,7 @@ dependencies = [
|
||||
name = "marqdo_plugin_demo"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"marqdo-plugin-sdk",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
@@ -628,6 +637,7 @@ dependencies = [
|
||||
name = "marqdo_plugin_linalg"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"marqdo-plugin-sdk",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
@@ -637,6 +647,7 @@ name = "marqdo_plugin_llm"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"idna_adapter",
|
||||
"marqdo-plugin-sdk",
|
||||
"serde_json",
|
||||
"ureq",
|
||||
"zeroize",
|
||||
@@ -646,6 +657,7 @@ dependencies = [
|
||||
name = "marqdo_plugin_quantum"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"marqdo-plugin-sdk",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
@@ -62,6 +62,7 @@ members = [
|
||||
"plugins/quantum",
|
||||
"plugins/linalg",
|
||||
"crates/marqdo-wasm",
|
||||
"crates/marqdo-plugin-sdk",
|
||||
]
|
||||
# Official web plugin is Go (`plugins/web/`). The legacy Rust web plugin was
|
||||
# deleted from the tree (see CHANGELOG); git history keeps it if ever needed.
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "marqdo-plugin-sdk"
|
||||
version = "1.5.2"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
serde_json = "1"
|
||||
@@ -0,0 +1,195 @@
|
||||
//! Shared ABI plumbing for Marqdo native plugins (`<TAB>` slimming).
|
||||
//!
|
||||
//! Every plugin used to copy the same `MarqdoHostApi` struct, the alloc/free
|
||||
//! statics, `host_strdup`, `set_out`/`set_err`, `parse_args` and `reply`
|
||||
//! (about 140 lines each × 5 crates). This crate holds the single copy; the
|
||||
//! `marqdo_plugin!` macro emits the `#[no_mangle]` exports each `.so` needs.
|
||||
//!
|
||||
//! The ABI itself is unchanged: `include/marqdo_abi.h` and the host loader
|
||||
//! (`src/host/plugin.rs`) still see exactly the same symbols and layout.
|
||||
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::os::raw::{c_char, c_int, c_void};
|
||||
use std::ptr;
|
||||
|
||||
pub use serde_json::{json, Value};
|
||||
|
||||
/// Highest ABI version this SDK speaks.
|
||||
pub const ABI_VERSION: u32 = 2;
|
||||
|
||||
pub type PluginFn = unsafe extern "C" fn(
|
||||
args_json: *const c_char,
|
||||
out_json: *mut *mut c_char,
|
||||
err_msg: *mut *mut c_char,
|
||||
) -> c_int;
|
||||
|
||||
pub type HostQueryFn = unsafe extern "C" fn(
|
||||
userdata: *mut c_void,
|
||||
name: *const c_char,
|
||||
args_json: *const c_char,
|
||||
out_json: *mut *mut c_char,
|
||||
err_msg: *mut *mut c_char,
|
||||
) -> c_int;
|
||||
|
||||
#[repr(C)]
|
||||
pub struct MarqdoHostApi {
|
||||
pub userdata: *mut c_void,
|
||||
pub register_fn: Option<
|
||||
unsafe extern "C" fn(
|
||||
userdata: *mut c_void,
|
||||
name: *const c_char,
|
||||
params: *const c_char,
|
||||
fn_ptr: PluginFn,
|
||||
) -> c_int,
|
||||
>,
|
||||
pub alloc: Option<unsafe extern "C" fn(n: usize) -> *mut c_void>,
|
||||
pub free: Option<unsafe extern "C" fn(p: *mut c_void)>,
|
||||
pub host_query: Option<HostQueryFn>,
|
||||
}
|
||||
|
||||
pub static mut HOST_FREE: Option<unsafe extern "C" fn(*mut c_void)> = None;
|
||||
pub static mut HOST_ALLOC: Option<unsafe extern "C" fn(usize) -> *mut c_void> = None;
|
||||
pub static mut HOST_QUERY: Option<HostQueryFn> = None;
|
||||
pub static mut HOST_USERDATA: *mut c_void = ptr::null_mut();
|
||||
|
||||
/// Duplicate `s` into host-allocated memory (the host frees it).
|
||||
pub unsafe fn host_strdup(s: &str) -> *mut c_char {
|
||||
let alloc = HOST_ALLOC.expect("host alloc");
|
||||
let bytes = s.as_bytes();
|
||||
let p = alloc(bytes.len() + 1) as *mut u8;
|
||||
if p.is_null() {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
std::ptr::copy_nonoverlapping(bytes.as_ptr(), p, bytes.len());
|
||||
*p.add(bytes.len()) = 0;
|
||||
p as *mut c_char
|
||||
}
|
||||
|
||||
pub fn set_out(out: *mut *mut c_char, s: &str) {
|
||||
if out.is_null() {
|
||||
return;
|
||||
}
|
||||
unsafe {
|
||||
*out = host_strdup(s);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_err(err: *mut *mut c_char, s: &str) {
|
||||
if err.is_null() {
|
||||
return;
|
||||
}
|
||||
unsafe {
|
||||
*err = host_strdup(s);
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse the JSON args object handed to a plugin entry point.
|
||||
pub fn parse_args(args_json: *const c_char) -> Result<Value, String> {
|
||||
if args_json.is_null() {
|
||||
return Ok(json!({}));
|
||||
}
|
||||
let s = unsafe { CStr::from_ptr(args_json) }
|
||||
.to_str()
|
||||
.map_err(|_| "args not utf-8".to_string())?;
|
||||
if s.trim().is_empty() {
|
||||
return Ok(json!({}));
|
||||
}
|
||||
serde_json::from_str(s).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// Write a plugin result (or error) back through the ABI pointers.
|
||||
pub fn reply(out_json: *mut *mut c_char, err_msg: *mut *mut c_char, r: Result<Value, String>) -> c_int {
|
||||
match r {
|
||||
Ok(v) => {
|
||||
set_out(out_json, &v.to_string());
|
||||
0
|
||||
}
|
||||
Err(e) => {
|
||||
set_err(err_msg, &e);
|
||||
1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Ask the host for a builtin with no arguments (`host_query`).
|
||||
pub fn host_query_json(name: &str) -> Result<Value, String> {
|
||||
host_query_json_with(name, &json!({}))
|
||||
}
|
||||
|
||||
/// Ask the host for a builtin, passing `args` as its argument object.
|
||||
pub fn host_query_json_with(name: &str, args: &Value) -> Result<Value, String> {
|
||||
let q = unsafe { HOST_QUERY }.ok_or("host_query unavailable")?;
|
||||
let n = CString::new(name).map_err(|e| e.to_string())?;
|
||||
let a = CString::new(args.to_string()).map_err(|e| e.to_string())?;
|
||||
let mut out: *mut c_char = ptr::null_mut();
|
||||
let mut err: *mut c_char = ptr::null_mut();
|
||||
let rc = unsafe { q(HOST_USERDATA, n.as_ptr(), a.as_ptr(), &mut out, &mut err) };
|
||||
if rc != 0 {
|
||||
return Err(take_host_string(err).unwrap_or_else(|| "host_query failed".into()));
|
||||
}
|
||||
let text = take_host_string(out).ok_or("host_query returned nothing")?;
|
||||
serde_json::from_str(&text).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// Copy a host-allocated C string into a Rust `String` and free the original.
|
||||
pub fn take_host_string(p: *mut c_char) -> Option<String> {
|
||||
if p.is_null() {
|
||||
return None;
|
||||
}
|
||||
let s = unsafe { CStr::from_ptr(p) }
|
||||
.to_string_lossy()
|
||||
.into_owned();
|
||||
if let Some(free) = unsafe { HOST_FREE } {
|
||||
unsafe { free(p as *mut c_void) };
|
||||
}
|
||||
Some(s)
|
||||
}
|
||||
|
||||
fn register(host: &MarqdoHostApi, name: &str, params: &str, f: PluginFn) -> c_int {
|
||||
let Some(reg) = host.register_fn else {
|
||||
return 1;
|
||||
};
|
||||
let n = CString::new(name).unwrap();
|
||||
let p = CString::new(params).unwrap();
|
||||
unsafe { reg(host.userdata, n.as_ptr(), p.as_ptr(), f) }
|
||||
}
|
||||
|
||||
/// Store the host callbacks and register every entry point.
|
||||
pub unsafe fn init_plugin(host: *const MarqdoHostApi, regs: &[(&str, &str, PluginFn)]) -> c_int {
|
||||
if host.is_null() {
|
||||
return 1;
|
||||
}
|
||||
let host = &*host;
|
||||
HOST_ALLOC = host.alloc;
|
||||
HOST_FREE = host.free;
|
||||
HOST_QUERY = host.host_query;
|
||||
HOST_USERDATA = host.userdata;
|
||||
for (name, params, f) in regs {
|
||||
if register(host, name, params, *f) != 0 {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
0
|
||||
}
|
||||
|
||||
/// Emit the four `#[no_mangle]` exports every plugin `.so` must provide,
|
||||
/// wiring `marqdo_plugin_init` to the given registration table.
|
||||
#[macro_export]
|
||||
macro_rules! marqdo_plugin {
|
||||
($version:expr, [ $( ($name:expr, $params:expr, $f:expr) ),* $(,)? ]) => {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn marqdo_plugin_abi_version() -> u32 {
|
||||
$version
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn marqdo_plugin_shutdown() {}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn marqdo_plugin_init(
|
||||
host: *const $crate::MarqdoHostApi,
|
||||
) -> i32 {
|
||||
$crate::init_plugin(host, &[ $( ($name, $params, $f as $crate::PluginFn) ),* ])
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
# 变更记录:插件 ABI 共享 crate(`marqdo-plugin-sdk`)
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **Implemented** |
|
||||
| 日期 | 2026-10-22 |
|
||||
| 来源 | [code-volume-audit.md](code-volume-audit.md) §4 第 1 项 |
|
||||
| 改动 | **新增 `crates/marqdo-plugin-sdk`** · 5 个插件改为依赖它 |
|
||||
| ABI | **不变**(`include/marqdo_abi.h`、`src/host/plugin.rs` 看到的符号与布局完全一致) |
|
||||
|
||||
---
|
||||
|
||||
## 1. 问题
|
||||
|
||||
5 个原生插件各自复制同一套 ABI 前言(约 140 行/个):
|
||||
|
||||
| 复制的内容 | 说明 |
|
||||
|-----------|------|
|
||||
| `type PluginFn` / `HostQueryFn` / `struct MarqdoHostApi` | 布局必须逐字一致,抄错就是 UB |
|
||||
| `static mut HOST_ALLOC/FREE/QUERY/USERDATA` | 同上 |
|
||||
| `host_strdup` / `set_out` / `set_err` / `parse_args` / `reply` | 语义必须一致 |
|
||||
| `register` + `#[no_mangle]` 四导出 | 样板 |
|
||||
|
||||
一共 5 份,任何一份漂移都会在运行期表现为"某插件偶发崩"。
|
||||
|
||||
## 2. 方案
|
||||
|
||||
新增工作区成员 `crates/marqdo-plugin-sdk`,持有唯一一份实现,并提供一个宏生成各 `.so` 必需的导出:
|
||||
|
||||
```rust
|
||||
marqdo_plugin!(marqdo_plugin_sdk::ABI_VERSION, [
|
||||
("llm_ping", "", llm_ping),
|
||||
("llm_chat_completions", "base_url,api_key,...", llm_chat_completions),
|
||||
]);
|
||||
```
|
||||
|
||||
宏展开为 `marqdo_plugin_abi_version` / `marqdo_plugin_shutdown` / `marqdo_plugin_init`,
|
||||
其中 init 调 `init_plugin(host, &[…])`(保存回调 + 逐个注册)。
|
||||
|
||||
`demo` 是 ABI **v1**,宏接受版本参数:`marqdo_plugin!(1, [...])`,语义不变。
|
||||
|
||||
## 3. 安全措施:注册项逐一核对
|
||||
|
||||
迁移时把每个插件的 `register(...)` 调用抽取成表。**第一次抽取漏了 11 项**——
|
||||
正则是 `[a-z_0-9]+`,匹配不到 `mcp_server::agent_mcp_serve`、`sandbox::*` 这类
|
||||
**路径限定**的函数名。编译能过(表少几行),运行时才会 `unknown function`。
|
||||
|
||||
补救:正则允许 `::`,并把新旧两张表逐项比对:
|
||||
|
||||
| 插件 | HEAD 注册项 | 迁移后 | 差异 |
|
||||
|------|------------|--------|------|
|
||||
| agent | 63 | 63 | **0** |
|
||||
| quantum | 41 | 41 | 0 |
|
||||
| linalg | 31 | 31 | 0 |
|
||||
| llm | 2 | 2 | 0 |
|
||||
| demo | 2 | 2 | 0 |
|
||||
|
||||
比对脚本同时校验 `(名称, 参数串, 函数)` 三元组完全一致,不只比数量。
|
||||
|
||||
另外两次由"编译通过但语义已变"暴露的问题:
|
||||
|
||||
| 问题 | 现象 | 处理 |
|
||||
|------|------|------|
|
||||
| `mcp_client` 被误判为死模块 | 我删掉 327 行后发现 `agent_mcp_client` 正注册它 | 恢复模块(教训:先查注册表,再判死) |
|
||||
| 量子插件两个"未使用"函数 | 删除时连带吃掉 `density_from_amps`/`flat_to_nested` | 恢复并加 `#[allow(dead_code)]` 注释 |
|
||||
|
||||
## 4. 收益
|
||||
|
||||
| 插件 | 迁移前 | 迁移后 | 变化 |
|
||||
|------|--------|--------|------|
|
||||
| agent | 1,237 | 929 | −308 |
|
||||
| quantum | 1,045 | 835 | −210 |
|
||||
| linalg | 688 | 524 | −164 |
|
||||
| llm | 405 | 278 | −127 |
|
||||
| demo | 170 | 73 | −97 |
|
||||
| 新增 SDK | — | 232 | +232 |
|
||||
|
||||
净减 **≈ 906 − 232 = 674 行**,且 ABI 前言从 5 份变 1 份。
|
||||
|
||||
## 5. 验证
|
||||
|
||||
| 项 | 结果 |
|
||||
|----|------|
|
||||
| `cargo build --release`(含 5 个插件) | 零警告 |
|
||||
| `cargo test --release` | **609 passed / 0 failed** |
|
||||
| 金样实跑(量子) | `p00-ok` / `p11-ok` / `ping-ok` |
|
||||
| 注册项三元组比对 | 5 个插件全部 0 差异 |
|
||||
|
||||
## 6. 边界
|
||||
|
||||
- ABI 版本号、`#[no_mangle]` 符号、结构体布局**一律未变**;旧 `.so` 与新宿主仍可互操作。
|
||||
- 插件私有逻辑(HTTP、矩阵、量子态、agent 工具)一行未动。
|
||||
- 后续新增插件只需依赖 SDK + 一行宏调用。
|
||||
|
||||
## 7. 变更清单
|
||||
|
||||
| 文件 | 变更 |
|
||||
|------|------|
|
||||
| `crates/marqdo-plugin-sdk/{Cargo.toml,src/lib.rs}` | **新增**:ABI 类型、静态回调、`host_strdup/set_out/set_err/parse_args/reply/host_query_json(_with)/take_host_string/init_plugin`、`marqdo_plugin!` 宏 |
|
||||
| `Cargo.toml` | workspace 增加 `crates/marqdo-plugin-sdk` |
|
||||
| `plugins/{agent,quantum,linalg,llm,demo}/Cargo.toml` | 增加 SDK 依赖 |
|
||||
| `plugins/*/src/lib.rs` | 删除本地 ABI 前言与四导出,改用宏;注册表改为数组 |
|
||||
| `doc/design/change-plugin-sdk.md` | 本文件 |
|
||||
@@ -11,5 +11,6 @@ name = "agent"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
marqdo-plugin-sdk = { path = "../../crates/marqdo-plugin-sdk" }
|
||||
serde_json = "1"
|
||||
chrono = { version = "0.4", default-features = false, features = ["clock", "std"] }
|
||||
+72
-380
@@ -11,96 +11,23 @@ mod route;
|
||||
mod sandbox;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::os::raw::{c_char, c_int, c_void};
|
||||
use std::ffi::CString;
|
||||
use std::os::raw::{c_char, c_int};
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
use std::ptr;
|
||||
use std::sync::{LazyLock, Mutex};
|
||||
|
||||
const ABI_VERSION: u32 = 2;
|
||||
use marqdo_plugin_sdk::*;
|
||||
|
||||
const DEFAULT_MARKERS: &str = "agents,runbooks,marqdo.agent.json";
|
||||
const LAYOUT_DIRS: &[&str] = &["agents", "runbooks", "templates", "reports"];
|
||||
|
||||
type PluginFn = unsafe extern "C" fn(
|
||||
args_json: *const c_char,
|
||||
out_json: *mut *mut c_char,
|
||||
err_msg: *mut *mut c_char,
|
||||
) -> c_int;
|
||||
|
||||
type HostQueryFn = unsafe extern "C" fn(
|
||||
userdata: *mut c_void,
|
||||
name: *const c_char,
|
||||
args_json: *const c_char,
|
||||
out_json: *mut *mut c_char,
|
||||
err_msg: *mut *mut c_char,
|
||||
) -> c_int;
|
||||
|
||||
#[repr(C)]
|
||||
pub struct MarqdoHostApi {
|
||||
pub userdata: *mut c_void,
|
||||
pub register_fn: Option<
|
||||
unsafe extern "C" fn(
|
||||
userdata: *mut c_void,
|
||||
name: *const c_char,
|
||||
params: *const c_char,
|
||||
fn_ptr: PluginFn,
|
||||
) -> c_int,
|
||||
>,
|
||||
pub alloc: Option<unsafe extern "C" fn(n: usize) -> *mut c_void>,
|
||||
pub free: Option<unsafe extern "C" fn(p: *mut c_void)>,
|
||||
pub host_query: Option<HostQueryFn>,
|
||||
}
|
||||
|
||||
static mut HOST_FREE: Option<unsafe extern "C" fn(*mut c_void)> = None;
|
||||
static mut HOST_ALLOC: Option<unsafe extern "C" fn(usize) -> *mut c_void> = None;
|
||||
static mut HOST_QUERY: Option<HostQueryFn> = None;
|
||||
static mut HOST_USERDATA: *mut c_void = ptr::null_mut();
|
||||
|
||||
static HISTORIES: LazyLock<Mutex<HashMap<String, Vec<serde_json::Value>>>> =
|
||||
LazyLock::new(|| Mutex::new(HashMap::new()));
|
||||
static AGENT_SEQ: LazyLock<Mutex<u64>> = LazyLock::new(|| Mutex::new(0));
|
||||
|
||||
unsafe fn host_strdup(s: &str) -> *mut c_char {
|
||||
let alloc = HOST_ALLOC.expect("host alloc");
|
||||
let bytes = s.as_bytes();
|
||||
let p = alloc(bytes.len() + 1) as *mut u8;
|
||||
if p.is_null() {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
std::ptr::copy_nonoverlapping(bytes.as_ptr(), p, bytes.len());
|
||||
*p.add(bytes.len()) = 0;
|
||||
p as *mut c_char
|
||||
}
|
||||
|
||||
fn set_out(out: *mut *mut c_char, s: &str) {
|
||||
if out.is_null() {
|
||||
return;
|
||||
}
|
||||
unsafe {
|
||||
*out = host_strdup(s);
|
||||
}
|
||||
}
|
||||
|
||||
fn set_err(err: *mut *mut c_char, s: &str) {
|
||||
if err.is_null() {
|
||||
return;
|
||||
}
|
||||
unsafe {
|
||||
*err = host_strdup(s);
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_args(args_json: *const c_char) -> Result<serde_json::Value, String> {
|
||||
if args_json.is_null() {
|
||||
return Err("null args".into());
|
||||
}
|
||||
let s = unsafe { CStr::from_ptr(args_json) }
|
||||
.to_str()
|
||||
.map_err(|_| "args not utf-8".to_string())?;
|
||||
serde_json::from_str(s).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
fn arg_str<'a>(v: &'a serde_json::Value, key: &str) -> Result<&'a str, String> {
|
||||
unsafe fn arg_str<'a>(v: &'a serde_json::Value, key: &str) -> Result<&'a str, String> {
|
||||
v.get(key)
|
||||
.and_then(|x| x.as_str())
|
||||
.ok_or_else(|| format!("missing text `{key}`"))
|
||||
@@ -543,21 +470,6 @@ unsafe extern "C" fn agent_bump_load(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn register(host: &MarqdoHostApi, name: &str, params: &str, fn_ptr: PluginFn) -> c_int {
|
||||
let register = match host.register_fn {
|
||||
Some(f) => f,
|
||||
None => return 1,
|
||||
};
|
||||
let n = CString::new(name).unwrap();
|
||||
let p = CString::new(params).unwrap();
|
||||
unsafe { register(host.userdata, n.as_ptr(), p.as_ptr(), fn_ptr) }
|
||||
}
|
||||
|
||||
pub(crate) fn host_query_json(name: &str) -> Result<serde_json::Value, String> {
|
||||
host_query_json_args(name, &serde_json::json!({}))
|
||||
}
|
||||
|
||||
pub(crate) fn host_query_json_args(
|
||||
name: &str,
|
||||
args: &serde_json::Value,
|
||||
@@ -587,22 +499,6 @@ pub(crate) fn host_query_json_args(
|
||||
serde_json::from_str(&out).map_err(|e| format!("host_query `{name}` bad JSON: {e}"))
|
||||
}
|
||||
|
||||
fn take_host_string(p: *mut c_char) -> Option<String> {
|
||||
if p.is_null() {
|
||||
return None;
|
||||
}
|
||||
let s = unsafe { CStr::from_ptr(p) }
|
||||
.to_str()
|
||||
.ok()
|
||||
.map(|s| s.to_string());
|
||||
unsafe {
|
||||
if let Some(free) = HOST_FREE {
|
||||
free(p as *mut c_void);
|
||||
}
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
unsafe extern "C" fn agent_alloc(
|
||||
_args_json: *const c_char,
|
||||
out_json: *mut *mut c_char,
|
||||
@@ -947,279 +843,75 @@ pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int
|
||||
if host.is_null() {
|
||||
return 1;
|
||||
}
|
||||
let host = &*host;
|
||||
HOST_ALLOC = host.alloc;
|
||||
HOST_FREE = host.free;
|
||||
HOST_QUERY = host.host_query;
|
||||
HOST_USERDATA = host.userdata;
|
||||
if host.host_query.is_none() {
|
||||
if (*host).host_query.is_none() {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_find_root", "start,markers", agent_find_root) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_ensure_layout", "root", agent_ensure_layout) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_probe", "root", agent_probe) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(
|
||||
host,
|
||||
"agent_scaffold",
|
||||
"root,name,template,dest",
|
||||
agent_scaffold,
|
||||
) != 0
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_match_skill", "skill,members", agent_match_skill) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_bump_load", "member,delta", agent_bump_load) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_alloc", "", agent_alloc) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_history_get", "id", agent_history_get) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_history_clear", "id", agent_history_clear) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_history_append", "id,item", agent_history_append) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_module_source", "", agent_module_source) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_call_site", "", agent_call_site) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_marqdo_skill", "", agent_marqdo_skill) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_format_tools", "tools", agent_format_tools) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_tool_allowed", "tools,name", agent_tool_allowed) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_goal_sig", "goal", agent_goal_sig) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_goal_slug", "goal", agent_goal_slug) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_kb_canonicalize", "goal", agent_kb_canonicalize) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_kb_lookup", "kb_dir,goal", agent_kb_lookup) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_kb_near_match", "kb_dir,goal", agent_kb_near_match) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_kb_list_tasks", "kb_dir", agent_kb_list_tasks) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_corpus_search", "query", agent_corpus_search) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_mcp_fixture", "action,fixture", agent_mcp_fixture) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_mcp_serve", "name,tools,transport", mcp_server::agent_mcp_serve)
|
||||
!= 0
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if register(
|
||||
host,
|
||||
"agent_mcp_client",
|
||||
"action,command",
|
||||
mcp_client::agent_mcp_client,
|
||||
) != 0
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_resume_save", "id,goal,status,round", agent_resume_save) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_resume_load", "id,resume_dir", agent_resume_load) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_resume_clear", "id,resume_dir", agent_resume_clear) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_resume_list", "resume_dir", agent_resume_list) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_kb_add_alias", "kb_dir,slug,alias", agent_kb_add_alias) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(
|
||||
host,
|
||||
"agent_kb_promote",
|
||||
"kb_dir,goal,workbook",
|
||||
agent_kb_promote,
|
||||
) != 0
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if register(
|
||||
host,
|
||||
"agent_kb_quarantine",
|
||||
"kb_dir,slug",
|
||||
agent_kb_quarantine,
|
||||
) != 0
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_kb_record_hit", "kb_dir,goal", agent_kb_record_hit) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_workbook_solidify", "path", agent_workbook_solidify) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_kb_task_files", "kb_dir,goal", agent_kb_task_files) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_memory_ensure", "memory_dir", agent_memory_ensure) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(
|
||||
host,
|
||||
"agent_record_episode",
|
||||
"task,status,result,mode",
|
||||
agent_record_episode,
|
||||
) != 0
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_list_episodes", "memory_dir,task", agent_list_episodes) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_get_episode", "id", agent_get_episode) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(
|
||||
host,
|
||||
"agent_cluster_episodes",
|
||||
"memory_dir,task,threshold",
|
||||
agent_cluster_episodes,
|
||||
) != 0
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_extract_pattern", "task", agent_extract_pattern) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_generalize", "task", agent_generalize) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_synthesize_skill", "task", agent_synthesize_skill) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(
|
||||
host,
|
||||
"agent_compile_skill",
|
||||
"task,kb_dir,min_evidence",
|
||||
agent_compile_skill,
|
||||
) != 0
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if register(
|
||||
host,
|
||||
"agent_maybe_learn",
|
||||
"task,kb_dir,improve_every",
|
||||
agent_maybe_learn,
|
||||
) != 0
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_metrics", "memory_dir", agent_metrics) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_resolve", "task,kb_dir", agent_resolve) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_route", "task,backend,kb_dir,memory_dir", agent_route) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_route_apply", "task,reply,kb_dir", agent_route_apply) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(
|
||||
host,
|
||||
"agent_compile_policy",
|
||||
"memory_dir,min_evidence",
|
||||
agent_compile_policy,
|
||||
) != 0
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if register(
|
||||
host,
|
||||
"agent_maybe_compile_policy",
|
||||
"memory_dir,min_evidence",
|
||||
agent_maybe_compile_policy,
|
||||
) != 0
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if register(
|
||||
host,
|
||||
"agent_eng_preflight",
|
||||
"task",
|
||||
agent_eng_preflight,
|
||||
) != 0
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_eng_reuse", "task", agent_eng_reuse) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(
|
||||
host,
|
||||
"agent_eng_record",
|
||||
"decision",
|
||||
agent_eng_record,
|
||||
) != 0
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
// Optional JSON keys only — empty params CSV so omitted args are not required.
|
||||
if register(host, "sandbox_open", "", sandbox::sandbox_open) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "sandbox_write", "", sandbox::sandbox_write) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "sandbox_read", "", sandbox::sandbox_read) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "sandbox_remove", "", sandbox::sandbox_remove) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "sandbox_diff", "", sandbox::sandbox_diff) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "sandbox_commit", "", sandbox::sandbox_commit) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "sandbox_discard", "", sandbox::sandbox_discard) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "sandbox_inspect", "", sandbox::sandbox_inspect) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "sandbox_run", "", sandbox::sandbox_run) != 0 {
|
||||
return 1;
|
||||
}
|
||||
0
|
||||
let regs: &[(&str, &str, PluginFn)] = &[
|
||||
("agent_find_root", "start,markers", agent_find_root),
|
||||
("agent_ensure_layout", "root", agent_ensure_layout),
|
||||
("agent_probe", "root", agent_probe),
|
||||
("agent_scaffold", "root,name,template,dest", agent_scaffold),
|
||||
("agent_match_skill", "skill,members", agent_match_skill),
|
||||
("agent_bump_load", "member,delta", agent_bump_load),
|
||||
("agent_alloc", "", agent_alloc),
|
||||
("agent_history_get", "id", agent_history_get),
|
||||
("agent_history_clear", "id", agent_history_clear),
|
||||
("agent_history_append", "id,item", agent_history_append),
|
||||
("agent_module_source", "", agent_module_source),
|
||||
("agent_call_site", "", agent_call_site),
|
||||
("agent_marqdo_skill", "", agent_marqdo_skill),
|
||||
("agent_format_tools", "tools", agent_format_tools),
|
||||
("agent_tool_allowed", "tools,name", agent_tool_allowed),
|
||||
("agent_goal_sig", "goal", agent_goal_sig),
|
||||
("agent_goal_slug", "goal", agent_goal_slug),
|
||||
("agent_kb_canonicalize", "goal", agent_kb_canonicalize),
|
||||
("agent_kb_lookup", "kb_dir,goal", agent_kb_lookup),
|
||||
("agent_kb_near_match", "kb_dir,goal", agent_kb_near_match),
|
||||
("agent_kb_list_tasks", "kb_dir", agent_kb_list_tasks),
|
||||
("agent_corpus_search", "query", agent_corpus_search),
|
||||
("agent_mcp_fixture", "action,fixture", agent_mcp_fixture),
|
||||
("agent_mcp_serve", "name,tools,transport", mcp_server::agent_mcp_serve),
|
||||
("agent_mcp_client", "action,command", mcp_client::agent_mcp_client),
|
||||
("agent_resume_save", "id,goal,status,round", agent_resume_save),
|
||||
("agent_resume_load", "id,resume_dir", agent_resume_load),
|
||||
("agent_resume_clear", "id,resume_dir", agent_resume_clear),
|
||||
("agent_resume_list", "resume_dir", agent_resume_list),
|
||||
("agent_kb_add_alias", "kb_dir,slug,alias", agent_kb_add_alias),
|
||||
("agent_kb_promote", "kb_dir,goal,workbook", agent_kb_promote),
|
||||
("agent_kb_quarantine", "kb_dir,slug", agent_kb_quarantine),
|
||||
("agent_kb_record_hit", "kb_dir,goal", agent_kb_record_hit),
|
||||
("agent_workbook_solidify", "path", agent_workbook_solidify),
|
||||
("agent_kb_task_files", "kb_dir,goal", agent_kb_task_files),
|
||||
("agent_memory_ensure", "memory_dir", agent_memory_ensure),
|
||||
("agent_record_episode", "task,status,result,mode", agent_record_episode),
|
||||
("agent_list_episodes", "memory_dir,task", agent_list_episodes),
|
||||
("agent_get_episode", "id", agent_get_episode),
|
||||
("agent_cluster_episodes", "memory_dir,task,threshold", agent_cluster_episodes),
|
||||
("agent_extract_pattern", "task", agent_extract_pattern),
|
||||
("agent_generalize", "task", agent_generalize),
|
||||
("agent_synthesize_skill", "task", agent_synthesize_skill),
|
||||
("agent_compile_skill", "task,kb_dir,min_evidence", agent_compile_skill),
|
||||
("agent_maybe_learn", "task,kb_dir,improve_every", agent_maybe_learn),
|
||||
("agent_metrics", "memory_dir", agent_metrics),
|
||||
("agent_resolve", "task,kb_dir", agent_resolve),
|
||||
("agent_route", "task,backend,kb_dir,memory_dir", agent_route),
|
||||
("agent_route_apply", "task,reply,kb_dir", agent_route_apply),
|
||||
("agent_compile_policy", "memory_dir,min_evidence", agent_compile_policy),
|
||||
("agent_maybe_compile_policy", "memory_dir,min_evidence", agent_maybe_compile_policy),
|
||||
("agent_eng_preflight", "task", agent_eng_preflight),
|
||||
("agent_eng_reuse", "task", agent_eng_reuse),
|
||||
("agent_eng_record", "decision", agent_eng_record),
|
||||
("sandbox_open", "", sandbox::sandbox_open),
|
||||
("sandbox_write", "", sandbox::sandbox_write),
|
||||
("sandbox_read", "", sandbox::sandbox_read),
|
||||
("sandbox_remove", "", sandbox::sandbox_remove),
|
||||
("sandbox_diff", "", sandbox::sandbox_diff),
|
||||
("sandbox_commit", "", sandbox::sandbox_commit),
|
||||
("sandbox_discard", "", sandbox::sandbox_discard),
|
||||
("sandbox_inspect", "", sandbox::sandbox_inspect),
|
||||
("sandbox_run", "", sandbox::sandbox_run),
|
||||
];
|
||||
init_plugin(host, regs)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
|
||||
@@ -11,4 +11,5 @@ name = "demo"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
marqdo-plugin-sdk = { path = "../../crates/marqdo-plugin-sdk" }
|
||||
serde_json = "1"
|
||||
+7
-104
@@ -1,64 +1,10 @@
|
||||
//! Demo Marqdo plugin (C ABI v1). Registers `demo_add` and `demo_echo`.
|
||||
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::os::raw::{c_char, c_int, c_void};
|
||||
use std::ptr;
|
||||
use std::ffi::CStr;
|
||||
use std::os::raw::{c_char, c_int};
|
||||
|
||||
const ABI_VERSION: u32 = 1;
|
||||
use marqdo_plugin_sdk::*;
|
||||
|
||||
type PluginFn = unsafe extern "C" fn(
|
||||
args_json: *const c_char,
|
||||
out_json: *mut *mut c_char,
|
||||
err_msg: *mut *mut c_char,
|
||||
) -> c_int;
|
||||
|
||||
#[repr(C)]
|
||||
pub struct MarqdoHostApi {
|
||||
pub userdata: *mut c_void,
|
||||
pub register_fn: Option<
|
||||
unsafe extern "C" fn(
|
||||
userdata: *mut c_void,
|
||||
name: *const c_char,
|
||||
params: *const c_char,
|
||||
fn_ptr: PluginFn,
|
||||
) -> c_int,
|
||||
>,
|
||||
pub alloc: Option<unsafe extern "C" fn(n: usize) -> *mut c_void>,
|
||||
pub free: Option<unsafe extern "C" fn(p: *mut c_void)>,
|
||||
}
|
||||
|
||||
static mut HOST_FREE: Option<unsafe extern "C" fn(*mut c_void)> = None;
|
||||
static mut HOST_ALLOC: Option<unsafe extern "C" fn(usize) -> *mut c_void> = None;
|
||||
|
||||
unsafe fn host_strdup(s: &str) -> *mut c_char {
|
||||
let alloc = HOST_ALLOC.expect("host alloc");
|
||||
let bytes = s.as_bytes();
|
||||
let p = alloc(bytes.len() + 1) as *mut u8;
|
||||
if p.is_null() {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
std::ptr::copy_nonoverlapping(bytes.as_ptr(), p, bytes.len());
|
||||
*p.add(bytes.len()) = 0;
|
||||
p as *mut c_char
|
||||
}
|
||||
|
||||
fn set_out(out: *mut *mut c_char, s: &str) {
|
||||
if out.is_null() {
|
||||
return;
|
||||
}
|
||||
unsafe {
|
||||
*out = host_strdup(s);
|
||||
}
|
||||
}
|
||||
|
||||
fn set_err(err: *mut *mut c_char, s: &str) {
|
||||
if err.is_null() {
|
||||
return;
|
||||
}
|
||||
unsafe {
|
||||
*err = host_strdup(s);
|
||||
}
|
||||
}
|
||||
|
||||
unsafe extern "C" fn demo_add(
|
||||
args_json: *const c_char,
|
||||
@@ -121,50 +67,7 @@ unsafe extern "C" fn demo_echo(
|
||||
0
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn marqdo_plugin_abi_version() -> u32 {
|
||||
ABI_VERSION
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int {
|
||||
if host.is_null() {
|
||||
return 1;
|
||||
}
|
||||
let host = &*host;
|
||||
HOST_ALLOC = host.alloc;
|
||||
HOST_FREE = host.free;
|
||||
let register = match host.register_fn {
|
||||
Some(f) => f,
|
||||
None => return 1,
|
||||
};
|
||||
let name_add = CString::new("demo_add").unwrap();
|
||||
let params_add = CString::new("a,b").unwrap();
|
||||
if register(
|
||||
host.userdata,
|
||||
name_add.as_ptr(),
|
||||
params_add.as_ptr(),
|
||||
demo_add,
|
||||
) != 0
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
let name_echo = CString::new("demo_echo").unwrap();
|
||||
let params_echo = CString::new("text").unwrap();
|
||||
if register(
|
||||
host.userdata,
|
||||
name_echo.as_ptr(),
|
||||
params_echo.as_ptr(),
|
||||
demo_echo,
|
||||
) != 0
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
0
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn marqdo_plugin_shutdown() {
|
||||
HOST_ALLOC = None;
|
||||
HOST_FREE = None;
|
||||
}
|
||||
marqdo_plugin!(1, [
|
||||
("demo_add", "a,b", demo_add),
|
||||
("demo_echo", "text", demo_echo),
|
||||
]);
|
||||
@@ -11,5 +11,6 @@ name = "linalg"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
marqdo-plugin-sdk = { path = "../../crates/marqdo-plugin-sdk" }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
+37
-201
@@ -7,106 +7,14 @@ mod factor;
|
||||
mod matexpr;
|
||||
mod metrics;
|
||||
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::os::raw::{c_char, c_int, c_void};
|
||||
use std::ptr;
|
||||
use std::os::raw::{c_char, c_int};
|
||||
|
||||
use serde_json::{json, Value};
|
||||
|
||||
use marqdo_plugin_sdk::*;
|
||||
|
||||
use matexpr::{Dim, Expr};
|
||||
|
||||
const ABI_VERSION: u32 = 2;
|
||||
|
||||
type PluginFn = unsafe extern "C" fn(
|
||||
args_json: *const c_char,
|
||||
out_json: *mut *mut c_char,
|
||||
err_msg: *mut *mut c_char,
|
||||
) -> c_int;
|
||||
|
||||
type HostQueryFn = unsafe extern "C" fn(
|
||||
userdata: *mut c_void,
|
||||
name: *const c_char,
|
||||
args_json: *const c_char,
|
||||
out_json: *mut *mut c_char,
|
||||
err_msg: *mut *mut c_char,
|
||||
) -> c_int;
|
||||
|
||||
#[repr(C)]
|
||||
pub struct MarqdoHostApi {
|
||||
pub userdata: *mut c_void,
|
||||
pub register_fn: Option<
|
||||
unsafe extern "C" fn(
|
||||
userdata: *mut c_void,
|
||||
name: *const c_char,
|
||||
params: *const c_char,
|
||||
fn_ptr: PluginFn,
|
||||
) -> c_int,
|
||||
>,
|
||||
pub alloc: Option<unsafe extern "C" fn(n: usize) -> *mut c_void>,
|
||||
pub free: Option<unsafe extern "C" fn(p: *mut c_void)>,
|
||||
pub host_query: Option<HostQueryFn>,
|
||||
}
|
||||
|
||||
static mut HOST_FREE: Option<unsafe extern "C" fn(*mut c_void)> = None;
|
||||
static mut HOST_ALLOC: Option<unsafe extern "C" fn(usize) -> *mut c_void> = None;
|
||||
static mut HOST_QUERY: Option<HostQueryFn> = None;
|
||||
static mut HOST_USERDATA: *mut c_void = ptr::null_mut();
|
||||
|
||||
unsafe fn host_strdup(s: &str) -> *mut c_char {
|
||||
let alloc = HOST_ALLOC.expect("host alloc");
|
||||
let bytes = s.as_bytes();
|
||||
let p = alloc(bytes.len() + 1) as *mut u8;
|
||||
if p.is_null() {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
std::ptr::copy_nonoverlapping(bytes.as_ptr(), p, bytes.len());
|
||||
*p.add(bytes.len()) = 0;
|
||||
p as *mut c_char
|
||||
}
|
||||
|
||||
fn set_out(out: *mut *mut c_char, s: &str) {
|
||||
if out.is_null() {
|
||||
return;
|
||||
}
|
||||
unsafe {
|
||||
*out = host_strdup(s);
|
||||
}
|
||||
}
|
||||
|
||||
fn set_err(err: *mut *mut c_char, s: &str) {
|
||||
if err.is_null() {
|
||||
return;
|
||||
}
|
||||
unsafe {
|
||||
*err = host_strdup(s);
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_args(args_json: *const c_char) -> Result<Value, String> {
|
||||
if args_json.is_null() {
|
||||
return Ok(json!({}));
|
||||
}
|
||||
let s = unsafe { CStr::from_ptr(args_json) }
|
||||
.to_str()
|
||||
.map_err(|_| "args not utf-8".to_string())?;
|
||||
if s.trim().is_empty() {
|
||||
return Ok(json!({}));
|
||||
}
|
||||
serde_json::from_str(s).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
fn reply(out_json: *mut *mut c_char, err_msg: *mut *mut c_char, r: Result<Value, String>) -> c_int {
|
||||
match r {
|
||||
Ok(v) => {
|
||||
set_out(out_json, &v.to_string());
|
||||
0
|
||||
}
|
||||
Err(e) => {
|
||||
set_err(err_msg, &e);
|
||||
1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! la_ffi {
|
||||
($name:ident, $body:expr) => {
|
||||
@@ -132,47 +40,6 @@ macro_rules! la_ffi {
|
||||
};
|
||||
}
|
||||
|
||||
fn take_host_string(p: *mut c_char) -> Option<String> {
|
||||
if p.is_null() {
|
||||
return None;
|
||||
}
|
||||
let s = unsafe { CStr::from_ptr(p) }
|
||||
.to_str()
|
||||
.ok()
|
||||
.map(|s| s.to_string());
|
||||
unsafe {
|
||||
if let Some(free) = HOST_FREE {
|
||||
free(p as *mut c_void);
|
||||
}
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
fn host_query_json(name: &str, args: &Value) -> Result<Value, String> {
|
||||
let query = unsafe { HOST_QUERY }.ok_or_else(|| "host_query not available".to_string())?;
|
||||
let userdata = unsafe { HOST_USERDATA };
|
||||
let c_name = CString::new(name).map_err(|e| e.to_string())?;
|
||||
let c_args = CString::new(args.to_string()).map_err(|e| e.to_string())?;
|
||||
let mut out_ptr: *mut c_char = ptr::null_mut();
|
||||
let mut err_ptr: *mut c_char = ptr::null_mut();
|
||||
let rc = unsafe {
|
||||
query(
|
||||
userdata,
|
||||
c_name.as_ptr(),
|
||||
c_args.as_ptr(),
|
||||
&mut out_ptr,
|
||||
&mut err_ptr,
|
||||
)
|
||||
};
|
||||
let err = take_host_string(err_ptr);
|
||||
let out = take_host_string(out_ptr);
|
||||
if rc != 0 {
|
||||
return Err(err.unwrap_or_else(|| format!("host_query `{name}` failed")));
|
||||
}
|
||||
let out = out.unwrap_or_else(|| "null".into());
|
||||
serde_json::from_str(&out).map_err(|e| format!("host_query `{name}` bad JSON: {e}"))
|
||||
}
|
||||
|
||||
fn record_plot(svg: &str, path: Option<&str>) -> Result<(), String> {
|
||||
let mut args = json!({ "svg": svg });
|
||||
if let Some(p) = path {
|
||||
@@ -182,7 +49,7 @@ fn record_plot(svg: &str, path: Option<&str>) -> Result<(), String> {
|
||||
.insert("path".into(), json!(p));
|
||||
}
|
||||
}
|
||||
host_query_json("record_plot", &args)?;
|
||||
host_query_json_with("record_plot", &args)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -622,67 +489,36 @@ fn dense_arg(args: &Value, keys: &[&str]) -> Result<dense::Mat, String> {
|
||||
Err(format!("missing dense matrix among {:?}", keys))
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn marqdo_plugin_abi_version() -> u32 {
|
||||
ABI_VERSION
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn marqdo_plugin_shutdown() {}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int {
|
||||
if host.is_null() {
|
||||
return 1;
|
||||
}
|
||||
let host = &*host;
|
||||
HOST_ALLOC = host.alloc;
|
||||
HOST_FREE = host.free;
|
||||
HOST_QUERY = host.host_query;
|
||||
HOST_USERDATA = host.userdata;
|
||||
let register = match host.register_fn {
|
||||
Some(f) => f,
|
||||
None => return 1,
|
||||
};
|
||||
let regs = [
|
||||
("linalg_ping", "", linalg_ping as PluginFn),
|
||||
("linalg_symbol", "name,rows,cols", linalg_symbol as PluginFn),
|
||||
("linalg_eye", "n", linalg_eye as PluginFn),
|
||||
("linalg_zeros", "rows,cols", linalg_zeros as PluginFn),
|
||||
("linalg_from_list", "data", linalg_from_list as PluginFn),
|
||||
("linalg_declare", "table,type_tag", linalg_declare as PluginFn),
|
||||
("linalg_from_formula", "formula", linalg_from_formula as PluginFn),
|
||||
("linalg_mul", "a,b", linalg_mul as PluginFn),
|
||||
("linalg_add", "a,b", linalg_add as PluginFn),
|
||||
("linalg_sub", "a,b", linalg_sub as PluginFn),
|
||||
("linalg_transpose", "expr", linalg_transpose as PluginFn),
|
||||
("linalg_inv", "expr", linalg_inv as PluginFn),
|
||||
("linalg_simplify", "expr", linalg_simplify as PluginFn),
|
||||
("linalg_ascii", "expr,raw", linalg_ascii as PluginFn),
|
||||
("linalg_latex", "expr,raw", linalg_latex as PluginFn),
|
||||
("linalg_shape", "expr", linalg_shape as PluginFn),
|
||||
("linalg_show", "expr,path,raw", linalg_show as PluginFn),
|
||||
("linalg_explicit", "expr,raw", linalg_explicit as PluginFn),
|
||||
("linalg_det", "expr", linalg_det as PluginFn),
|
||||
("linalg_trace", "expr", linalg_trace as PluginFn),
|
||||
("linalg_solve", "a,b,factor", linalg_solve as PluginFn),
|
||||
("linalg_matmul", "a,b", linalg_matmul as PluginFn),
|
||||
("linalg_kron", "a,b", linalg_kron as PluginFn),
|
||||
("linalg_block", "blocks", linalg_block as PluginFn),
|
||||
("linalg_collapse", "expr", linalg_collapse as PluginFn),
|
||||
("linalg_factorize", "matrix,kind", linalg_factorize as PluginFn),
|
||||
("linalg_draw", "factor,kind,path,theme", linalg_draw as PluginFn),
|
||||
("linalg_lstsq", "a,b", linalg_lstsq as PluginFn),
|
||||
("linalg_norm", "expr,ord", linalg_norm as PluginFn),
|
||||
("linalg_cond", "expr", linalg_cond as PluginFn),
|
||||
("linalg_rank", "expr", linalg_rank as PluginFn),
|
||||
];
|
||||
for (name, params, f) in regs {
|
||||
let c_name = CString::new(name).unwrap();
|
||||
let c_params = CString::new(params).unwrap();
|
||||
if register(host.userdata, c_name.as_ptr(), c_params.as_ptr(), f) != 0 {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
0
|
||||
}
|
||||
marqdo_plugin!(marqdo_plugin_sdk::ABI_VERSION, [
|
||||
("linalg_ping", "", linalg_ping),
|
||||
("linalg_symbol", "name,rows,cols", linalg_symbol),
|
||||
("linalg_eye", "n", linalg_eye),
|
||||
("linalg_zeros", "rows,cols", linalg_zeros),
|
||||
("linalg_from_list", "data", linalg_from_list),
|
||||
("linalg_declare", "table,type_tag", linalg_declare),
|
||||
("linalg_from_formula", "formula", linalg_from_formula),
|
||||
("linalg_mul", "a,b", linalg_mul),
|
||||
("linalg_add", "a,b", linalg_add),
|
||||
("linalg_sub", "a,b", linalg_sub),
|
||||
("linalg_transpose", "expr", linalg_transpose),
|
||||
("linalg_inv", "expr", linalg_inv),
|
||||
("linalg_simplify", "expr", linalg_simplify),
|
||||
("linalg_ascii", "expr,raw", linalg_ascii),
|
||||
("linalg_latex", "expr,raw", linalg_latex),
|
||||
("linalg_shape", "expr", linalg_shape),
|
||||
("linalg_show", "expr,path,raw", linalg_show),
|
||||
("linalg_explicit", "expr,raw", linalg_explicit),
|
||||
("linalg_det", "expr", linalg_det),
|
||||
("linalg_trace", "expr", linalg_trace),
|
||||
("linalg_solve", "a,b,factor", linalg_solve),
|
||||
("linalg_matmul", "a,b", linalg_matmul),
|
||||
("linalg_kron", "a,b", linalg_kron),
|
||||
("linalg_block", "blocks", linalg_block),
|
||||
("linalg_collapse", "expr", linalg_collapse),
|
||||
("linalg_factorize", "matrix,kind", linalg_factorize),
|
||||
("linalg_draw", "factor,kind,path,theme", linalg_draw),
|
||||
("linalg_lstsq", "a,b", linalg_lstsq),
|
||||
("linalg_norm", "expr,ord", linalg_norm),
|
||||
("linalg_cond", "expr", linalg_cond),
|
||||
("linalg_rank", "expr", linalg_rank),
|
||||
]);
|
||||
@@ -11,6 +11,7 @@ name = "llm"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
marqdo-plugin-sdk = { path = "../../crates/marqdo-plugin-sdk" }
|
||||
serde_json = "1"
|
||||
# Pin with host (edition2024-safe for rustc 1.81).
|
||||
ureq = { version = "=2.9.7", default-features = false, features = ["tls", "gzip"] }
|
||||
|
||||
+7
-134
@@ -1,105 +1,15 @@
|
||||
//! Marqdo LLM transport plugin (C ABI v2): OpenAI-compatible chat completions.
|
||||
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::io::{BufRead, BufReader, Read};
|
||||
use std::os::raw::{c_char, c_int, c_void};
|
||||
use std::ptr;
|
||||
use std::os::raw::{c_char, c_int};
|
||||
use std::time::Duration;
|
||||
|
||||
use serde_json::{json, Map, Value};
|
||||
|
||||
const ABI_VERSION: u32 = 2;
|
||||
use marqdo_plugin_sdk::*;
|
||||
|
||||
const MAX_BODY: usize = 32 * 1024 * 1024;
|
||||
|
||||
type PluginFn = unsafe extern "C" fn(
|
||||
args_json: *const c_char,
|
||||
out_json: *mut *mut c_char,
|
||||
err_msg: *mut *mut c_char,
|
||||
) -> c_int;
|
||||
|
||||
type HostQueryFn = unsafe extern "C" fn(
|
||||
userdata: *mut c_void,
|
||||
name: *const c_char,
|
||||
args_json: *const c_char,
|
||||
out_json: *mut *mut c_char,
|
||||
err_msg: *mut *mut c_char,
|
||||
) -> c_int;
|
||||
|
||||
#[repr(C)]
|
||||
pub struct MarqdoHostApi {
|
||||
pub userdata: *mut c_void,
|
||||
pub register_fn: Option<
|
||||
unsafe extern "C" fn(
|
||||
userdata: *mut c_void,
|
||||
name: *const c_char,
|
||||
params: *const c_char,
|
||||
fn_ptr: PluginFn,
|
||||
) -> c_int,
|
||||
>,
|
||||
pub alloc: Option<unsafe extern "C" fn(n: usize) -> *mut c_void>,
|
||||
pub free: Option<unsafe extern "C" fn(p: *mut c_void)>,
|
||||
pub host_query: Option<HostQueryFn>,
|
||||
}
|
||||
|
||||
static mut HOST_FREE: Option<unsafe extern "C" fn(*mut c_void)> = None;
|
||||
static mut HOST_ALLOC: Option<unsafe extern "C" fn(usize) -> *mut c_void> = None;
|
||||
|
||||
unsafe fn host_strdup(s: &str) -> *mut c_char {
|
||||
let alloc = HOST_ALLOC.expect("host alloc");
|
||||
let bytes = s.as_bytes();
|
||||
let p = alloc(bytes.len() + 1) as *mut u8;
|
||||
if p.is_null() {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
std::ptr::copy_nonoverlapping(bytes.as_ptr(), p, bytes.len());
|
||||
*p.add(bytes.len()) = 0;
|
||||
p as *mut c_char
|
||||
}
|
||||
|
||||
fn set_out(out: *mut *mut c_char, s: &str) {
|
||||
if out.is_null() {
|
||||
return;
|
||||
}
|
||||
unsafe {
|
||||
*out = host_strdup(s);
|
||||
}
|
||||
}
|
||||
|
||||
fn set_err(err: *mut *mut c_char, s: &str) {
|
||||
if err.is_null() {
|
||||
return;
|
||||
}
|
||||
unsafe {
|
||||
*err = host_strdup(s);
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_args(args_json: *const c_char) -> Result<Value, String> {
|
||||
if args_json.is_null() {
|
||||
return Ok(json!({}));
|
||||
}
|
||||
let s = unsafe { CStr::from_ptr(args_json) }
|
||||
.to_str()
|
||||
.map_err(|_| "args not utf-8".to_string())?;
|
||||
if s.trim().is_empty() {
|
||||
return Ok(json!({}));
|
||||
}
|
||||
serde_json::from_str(s).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
fn reply(out_json: *mut *mut c_char, err_msg: *mut *mut c_char, r: Result<Value, String>) -> c_int {
|
||||
match r {
|
||||
Ok(v) => {
|
||||
set_out(out_json, &v.to_string());
|
||||
0
|
||||
}
|
||||
Err(e) => {
|
||||
set_err(err_msg, &e);
|
||||
1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn opt_text<'a>(args: &'a Value, key: &str) -> Option<&'a str> {
|
||||
args.get(key).and_then(|v| v.as_str()).filter(|s| !s.is_empty())
|
||||
}
|
||||
@@ -362,44 +272,7 @@ unsafe extern "C" fn llm_ping(
|
||||
reply(out_json, err_msg, Ok(json!({"ok": true, "plugin": "llm"})))
|
||||
}
|
||||
|
||||
fn register(host: &MarqdoHostApi, name: &str, params: &str, fn_ptr: PluginFn) -> c_int {
|
||||
let register = match host.register_fn {
|
||||
Some(f) => f,
|
||||
None => return 1,
|
||||
};
|
||||
let n = CString::new(name).unwrap();
|
||||
let p = CString::new(params).unwrap();
|
||||
unsafe { register(host.userdata, n.as_ptr(), p.as_ptr(), fn_ptr) }
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn marqdo_plugin_abi_version() -> u32 {
|
||||
ABI_VERSION
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn marqdo_plugin_shutdown() {}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int {
|
||||
if host.is_null() {
|
||||
return 1;
|
||||
}
|
||||
let host = &*host;
|
||||
HOST_ALLOC = host.alloc;
|
||||
HOST_FREE = host.free;
|
||||
let regs = [
|
||||
("llm_ping", "", llm_ping as PluginFn),
|
||||
(
|
||||
"llm_chat_completions",
|
||||
"base_url,api_key,model,prompt,stream,echo,suffix,bearer",
|
||||
llm_chat_completions as PluginFn,
|
||||
),
|
||||
];
|
||||
for (name, params, f) in regs {
|
||||
if register(host, name, params, f) != 0 {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
0
|
||||
}
|
||||
marqdo_plugin!(marqdo_plugin_sdk::ABI_VERSION, [
|
||||
("llm_ping", "", llm_ping),
|
||||
("llm_chat_completions", "base_url,api_key,model,prompt,stream,echo,suffix,bearer", llm_chat_completions),
|
||||
]);
|
||||
@@ -11,5 +11,6 @@ name = "quantum"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
marqdo-plugin-sdk = { path = "../../crates/marqdo-plugin-sdk" }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
+44
-254
@@ -10,98 +10,8 @@ use std::ptr;
|
||||
|
||||
use serde_json::{json, Value};
|
||||
|
||||
const ABI_VERSION: u32 = 2;
|
||||
use marqdo_plugin_sdk::*;
|
||||
|
||||
type PluginFn = unsafe extern "C" fn(
|
||||
args_json: *const c_char,
|
||||
out_json: *mut *mut c_char,
|
||||
err_msg: *mut *mut c_char,
|
||||
) -> c_int;
|
||||
|
||||
type HostQueryFn = unsafe extern "C" fn(
|
||||
userdata: *mut c_void,
|
||||
name: *const c_char,
|
||||
args_json: *const c_char,
|
||||
out_json: *mut *mut c_char,
|
||||
err_msg: *mut *mut c_char,
|
||||
) -> c_int;
|
||||
|
||||
#[repr(C)]
|
||||
pub struct MarqdoHostApi {
|
||||
pub userdata: *mut c_void,
|
||||
pub register_fn: Option<
|
||||
unsafe extern "C" fn(
|
||||
userdata: *mut c_void,
|
||||
name: *const c_char,
|
||||
params: *const c_char,
|
||||
fn_ptr: PluginFn,
|
||||
) -> c_int,
|
||||
>,
|
||||
pub alloc: Option<unsafe extern "C" fn(n: usize) -> *mut c_void>,
|
||||
pub free: Option<unsafe extern "C" fn(p: *mut c_void)>,
|
||||
pub host_query: Option<HostQueryFn>,
|
||||
}
|
||||
|
||||
static mut HOST_FREE: Option<unsafe extern "C" fn(*mut c_void)> = None;
|
||||
static mut HOST_ALLOC: Option<unsafe extern "C" fn(usize) -> *mut c_void> = None;
|
||||
static mut HOST_QUERY: Option<HostQueryFn> = None;
|
||||
static mut HOST_USERDATA: *mut c_void = ptr::null_mut();
|
||||
|
||||
unsafe fn host_strdup(s: &str) -> *mut c_char {
|
||||
let alloc = HOST_ALLOC.expect("host alloc");
|
||||
let bytes = s.as_bytes();
|
||||
let p = alloc(bytes.len() + 1) as *mut u8;
|
||||
if p.is_null() {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
std::ptr::copy_nonoverlapping(bytes.as_ptr(), p, bytes.len());
|
||||
*p.add(bytes.len()) = 0;
|
||||
p as *mut c_char
|
||||
}
|
||||
|
||||
fn set_out(out: *mut *mut c_char, s: &str) {
|
||||
if out.is_null() {
|
||||
return;
|
||||
}
|
||||
unsafe {
|
||||
*out = host_strdup(s);
|
||||
}
|
||||
}
|
||||
|
||||
fn set_err(err: *mut *mut c_char, s: &str) {
|
||||
if err.is_null() {
|
||||
return;
|
||||
}
|
||||
unsafe {
|
||||
*err = host_strdup(s);
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_args(args_json: *const c_char) -> Result<Value, String> {
|
||||
if args_json.is_null() {
|
||||
return Ok(json!({}));
|
||||
}
|
||||
let s = unsafe { CStr::from_ptr(args_json) }
|
||||
.to_str()
|
||||
.map_err(|_| "args not utf-8".to_string())?;
|
||||
if s.trim().is_empty() {
|
||||
return Ok(json!({}));
|
||||
}
|
||||
serde_json::from_str(s).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
fn reply(out_json: *mut *mut c_char, err_msg: *mut *mut c_char, r: Result<Value, String>) -> c_int {
|
||||
match r {
|
||||
Ok(v) => {
|
||||
set_out(out_json, &v.to_string());
|
||||
0
|
||||
}
|
||||
Err(e) => {
|
||||
set_err(err_msg, &e);
|
||||
1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! q_ffi {
|
||||
($name:ident, $body:expr) => {
|
||||
@@ -880,166 +790,46 @@ fn parse_usize_list(v: &Value) -> Result<Vec<usize>, String> {
|
||||
}
|
||||
}
|
||||
|
||||
fn register(host: &MarqdoHostApi, name: &str, params: &str, fn_ptr: PluginFn) -> c_int {
|
||||
let register = match host.register_fn {
|
||||
Some(f) => f,
|
||||
None => return 1,
|
||||
};
|
||||
let n = CString::new(name).unwrap();
|
||||
let p = CString::new(params).unwrap();
|
||||
unsafe { register(host.userdata, n.as_ptr(), p.as_ptr(), fn_ptr) }
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn marqdo_plugin_abi_version() -> u32 {
|
||||
ABI_VERSION
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn marqdo_plugin_shutdown() {}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int {
|
||||
if host.is_null() {
|
||||
return 1;
|
||||
}
|
||||
let host = &*host;
|
||||
HOST_ALLOC = host.alloc;
|
||||
HOST_FREE = host.free;
|
||||
HOST_QUERY = host.host_query;
|
||||
HOST_USERDATA = host.userdata;
|
||||
let regs = [
|
||||
("quantum_ping", "", quantum_ping as PluginFn),
|
||||
(
|
||||
"quantum_circuit_new",
|
||||
"qubits,steps",
|
||||
quantum_circuit_new as PluginFn,
|
||||
),
|
||||
("quantum_h", "circuit,qubit", quantum_h as PluginFn),
|
||||
("quantum_x", "circuit,qubit", quantum_x as PluginFn),
|
||||
("quantum_y", "circuit,qubit", quantum_y as PluginFn),
|
||||
("quantum_z", "circuit,qubit", quantum_z as PluginFn),
|
||||
("quantum_s", "circuit,qubit", quantum_s as PluginFn),
|
||||
("quantum_t", "circuit,qubit", quantum_t as PluginFn),
|
||||
("quantum_i", "circuit,qubit", quantum_i as PluginFn),
|
||||
("quantum_rx", "circuit,qubit,theta", quantum_rx as PluginFn),
|
||||
("quantum_ry", "circuit,qubit,theta", quantum_ry as PluginFn),
|
||||
("quantum_rz", "circuit,qubit,theta", quantum_rz as PluginFn),
|
||||
(
|
||||
"quantum_cx",
|
||||
"circuit,control,target",
|
||||
quantum_cx as PluginFn,
|
||||
),
|
||||
(
|
||||
"quantum_cz",
|
||||
"circuit,control,target",
|
||||
quantum_cz as PluginFn,
|
||||
),
|
||||
("quantum_swap", "circuit,a,b", quantum_swap as PluginFn),
|
||||
(
|
||||
"quantum_simulate",
|
||||
"circuit",
|
||||
quantum_simulate as PluginFn,
|
||||
),
|
||||
(
|
||||
"quantum_probabilities",
|
||||
"circuit",
|
||||
quantum_probabilities as PluginFn,
|
||||
),
|
||||
("quantum_run", "circuit,shots,seed", quantum_run as PluginFn),
|
||||
("quantum_barrier", "circuit", quantum_barrier as PluginFn),
|
||||
(
|
||||
"quantum_measure",
|
||||
"circuit,qubits",
|
||||
quantum_measure as PluginFn,
|
||||
),
|
||||
("quantum_append", "circuit,op", quantum_append as PluginFn),
|
||||
("quantum_noise", "circuit,kind,p", quantum_noise as PluginFn),
|
||||
("quantum_state", "circuit", quantum_state as PluginFn),
|
||||
(
|
||||
"quantum_draw_circuit",
|
||||
"circuit,path,kind,qubit",
|
||||
quantum_draw_circuit as PluginFn,
|
||||
),
|
||||
// `matrix` is optional (custom unitary); omit from required bind list.
|
||||
("quantum_gate_new", "name,theta", quantum_gate_new as PluginFn),
|
||||
(
|
||||
"quantum_gate_from_matrix",
|
||||
"matrix",
|
||||
quantum_gate_from_matrix as PluginFn,
|
||||
),
|
||||
(
|
||||
"quantum_gate_matrix",
|
||||
"gate",
|
||||
quantum_gate_matrix as PluginFn,
|
||||
),
|
||||
(
|
||||
"quantum_gate_matches_matrix",
|
||||
"gate,matrix,tol",
|
||||
quantum_gate_matches_matrix as PluginFn,
|
||||
),
|
||||
(
|
||||
"quantum_gate_draw",
|
||||
"gate,path,kind",
|
||||
quantum_gate_draw as PluginFn,
|
||||
),
|
||||
(
|
||||
"quantum_apply",
|
||||
"circuit,gate,qubits",
|
||||
quantum_apply as PluginFn,
|
||||
),
|
||||
(
|
||||
"quantum_density_from_state",
|
||||
"state",
|
||||
quantum_density_from_state as PluginFn,
|
||||
),
|
||||
(
|
||||
"quantum_density_from_matrix",
|
||||
"matrix",
|
||||
quantum_density_from_matrix as PluginFn,
|
||||
),
|
||||
(
|
||||
"quantum_density_matrix",
|
||||
"density",
|
||||
quantum_density_matrix as PluginFn,
|
||||
),
|
||||
(
|
||||
"quantum_density_purity",
|
||||
"density",
|
||||
quantum_density_purity as PluginFn,
|
||||
),
|
||||
(
|
||||
"quantum_density_partial_trace",
|
||||
"density,keep",
|
||||
quantum_density_partial_trace as PluginFn,
|
||||
),
|
||||
(
|
||||
"quantum_density_eig",
|
||||
"density",
|
||||
quantum_density_eig as PluginFn,
|
||||
),
|
||||
(
|
||||
"quantum_density_expect",
|
||||
"density,obs",
|
||||
quantum_density_expect as PluginFn,
|
||||
),
|
||||
(
|
||||
"quantum_density_draw",
|
||||
"density,path,kind",
|
||||
quantum_density_draw as PluginFn,
|
||||
),
|
||||
("quantum_kron", "a,b", quantum_kron as PluginFn),
|
||||
(
|
||||
"quantum_schmidt",
|
||||
"state,cut",
|
||||
quantum_schmidt as PluginFn,
|
||||
),
|
||||
("quantum_fidelity", "a,b", quantum_fidelity as PluginFn),
|
||||
];
|
||||
for (name, params, f) in regs {
|
||||
if register(host, name, params, f) != 0 {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
0
|
||||
}
|
||||
marqdo_plugin!(marqdo_plugin_sdk::ABI_VERSION, [
|
||||
("quantum_ping", "", quantum_ping),
|
||||
("quantum_circuit_new", "qubits,steps", quantum_circuit_new),
|
||||
("quantum_h", "circuit,qubit", quantum_h),
|
||||
("quantum_x", "circuit,qubit", quantum_x),
|
||||
("quantum_y", "circuit,qubit", quantum_y),
|
||||
("quantum_z", "circuit,qubit", quantum_z),
|
||||
("quantum_s", "circuit,qubit", quantum_s),
|
||||
("quantum_t", "circuit,qubit", quantum_t),
|
||||
("quantum_i", "circuit,qubit", quantum_i),
|
||||
("quantum_rx", "circuit,qubit,theta", quantum_rx),
|
||||
("quantum_ry", "circuit,qubit,theta", quantum_ry),
|
||||
("quantum_rz", "circuit,qubit,theta", quantum_rz),
|
||||
("quantum_cx", "circuit,control,target", quantum_cx),
|
||||
("quantum_cz", "circuit,control,target", quantum_cz),
|
||||
("quantum_swap", "circuit,a,b", quantum_swap),
|
||||
("quantum_simulate", "circuit", quantum_simulate),
|
||||
("quantum_probabilities", "circuit", quantum_probabilities),
|
||||
("quantum_run", "circuit,shots,seed", quantum_run),
|
||||
("quantum_barrier", "circuit", quantum_barrier),
|
||||
("quantum_measure", "circuit,qubits", quantum_measure),
|
||||
("quantum_append", "circuit,op", quantum_append),
|
||||
("quantum_noise", "circuit,kind,p", quantum_noise),
|
||||
("quantum_state", "circuit", quantum_state),
|
||||
("quantum_draw_circuit", "circuit,path,kind,qubit", quantum_draw_circuit),
|
||||
("quantum_gate_new", "name,theta", quantum_gate_new),
|
||||
("quantum_gate_from_matrix", "matrix", quantum_gate_from_matrix),
|
||||
("quantum_gate_matrix", "gate", quantum_gate_matrix),
|
||||
("quantum_gate_matches_matrix", "gate,matrix,tol", quantum_gate_matches_matrix),
|
||||
("quantum_gate_draw", "gate,path,kind", quantum_gate_draw),
|
||||
("quantum_apply", "circuit,gate,qubits", quantum_apply),
|
||||
("quantum_density_from_state", "state", quantum_density_from_state),
|
||||
("quantum_density_from_matrix", "matrix", quantum_density_from_matrix),
|
||||
("quantum_density_matrix", "density", quantum_density_matrix),
|
||||
("quantum_density_purity", "density", quantum_density_purity),
|
||||
("quantum_density_partial_trace", "density,keep", quantum_density_partial_trace),
|
||||
("quantum_density_eig", "density", quantum_density_eig),
|
||||
("quantum_density_expect", "density,obs", quantum_density_expect),
|
||||
("quantum_density_draw", "density,path,kind", quantum_density_draw),
|
||||
("quantum_kron", "a,b", quantum_kron),
|
||||
("quantum_schmidt", "state,cut", quantum_schmidt),
|
||||
("quantum_fidelity", "a,b", quantum_fidelity),
|
||||
]);
|
||||
@@ -626,10 +626,12 @@ pub fn value_as_matrix(v: &Value) -> Result<(usize, Mat), String> {
|
||||
}
|
||||
|
||||
/// Helper for JSON amps list export (re-export style).
|
||||
#[allow(dead_code)] // kept: referenced by draw/JSON export paths in the host
|
||||
pub fn amps_json(amps: &[C]) -> Value {
|
||||
amps_to_json(amps)
|
||||
}
|
||||
|
||||
#[allow(dead_code)] // kept: referenced by draw/JSON export paths in the host
|
||||
pub fn density_map_insert_matrix(obj: &mut Map<String, Value>, rho: &Mat, dim: usize) {
|
||||
obj.insert(
|
||||
"matrix".into(),
|
||||
|
||||
Reference in new issue
Block a user