Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ edition = "2024"
crate-type = ["cdylib"]

[dependencies]
runvil-plugin-sdk = { git = "https://github.com/cppalliance/runvil-plugin-api", tag = "api-v0.7.0" }
runvil-plugin-sdk = { git = "https://github.com/cppalliance/runvil-plugin-api", tag = "api-v0.8.0" }

[dev-dependencies]
runvil-plugin-sdk = { git = "https://github.com/cppalliance/runvil-plugin-api", tag = "api-v0.7.0", features = ["test-support"] }
runvil-plugin-sdk = { git = "https://github.com/cppalliance/runvil-plugin-api", tag = "api-v0.8.0", features = ["test-support"] }
```

`src/lib.rs`:
Expand Down Expand Up @@ -91,6 +91,7 @@ export_plugin!(Acme);
- The Plugin's name and version, and the name and description of each Pack, are checked when the Plugin is compiled: one the Harness would reject fails the build, with a message naming the rule.
- The Pack's name is a vendor and a Pack id joined by `/`. A prompt reaches the tool above as `hello/greet`, the Pack id and the tool's name. A model calls it by its wire name, which is `hello_greet` unless `ToolInfo::wire_name` sets another.
- Arguments and parameter schemas cross as JSON text, so the SDK needs no JSON library. A Plugin parses `call.arguments()` with one of its own.
- `call.caller()` says whether a model round (`Caller::Model`) or a section's Lua script (`Caller::Script`) asked for the call. `call.section()` names the section that made the call, `call.entry()` is the section entry (`sys.id`) when the Host names one, and `call.model()` is the JSON text describing the model of the round that asked for the call, or `None` on a script call.
- `call.resources()` is what the Host offers every Plugin, such as the address and the key of a service: the JSON text of an object keyed by resource name, as it was when the call started, and `{}` when the Host offers nothing. A Plugin parses it the same way. It can hold a key, so no `Debug` in the SDK prints it, and a Plugin puts none of it in an output or a failure.
- A tool call is a future the Host polls once per step. A `Pending` parks the call, and the Host sends the next step when the future's waker is woken. So a call may wait on a waker like any Rust future: on a timer, a channel, or a task on a runtime the Plugin owns. A future that returns `Pending` without arranging a wake, and without a file request, parks for good. A call that does a long job awaits `next_step()` every so often, which ends the step and wakes the call for the next one, so that no step works for long and the Host can abandon the call between two steps.
- A tool reaches the files of its run through its `ToolCall`. `stat`, `list`, `read`, and `read_range` read them, and `write`, `append`, `mkdir`, `remove`, `rename`, and `copy` change them. Each returns a future, as in `call.read(path).await`. Awaiting it ends the step with a request to the Host, and a later step goes on with the answer, so the run's policy applies to every operation. An operation that fails resolves to a `FileError` with a kind and the Host's words for what went wrong, and the call goes on, so the tool decides what the model reads. An answer comes only with a step, so a file method is awaited inside the future that `Pack::call` returns. A file future gets its answer only when a step polls it, so it does not work inside a set that polls a child only after that child's waker fired, such as `FuturesUnordered`.
Expand Down Expand Up @@ -138,7 +139,7 @@ let pack = loaded.new_pack("acme/hello").unwrap();
assert_eq!(pack.call("greet", "{}"), Ok(Ok(ToolOutput::trusted("Hello."))));
```

`load` returns the Plugin's name, version, and Packs, or the failure the Harness would report. `new_pack` returns one run's instance with its tools. `call` runs a tool call to its end as the Harness does: it sends a step, and after each step that parks the call it waits until the Plugin has woken the call, or goes on at once when the step made a file request. A call that nothing wakes fails its test after 60 seconds, with a sentence that says so, and does not hang it. `start` hands back a call whose steps the test sends one at a time with `step`, and `wait` blocks until the call is due its next one. Dropping each value sends the verb that frees what it stands for.
`load` returns the Plugin's name, version, and Packs, or the failure the Harness would report. `new_pack` returns one run's instance with its tools. `call` runs a tool call to its end as the Harness does: it sends a step, and after each step that parks the call it waits until the Plugin has woken the call, or goes on at once when the step made a file request. A call that nothing wakes fails its test after 60 seconds, with a sentence that says so, and does not hang it. `start` hands back a `Call` whose caller, section, entry, and model a test can set with `with_caller`, `with_section`, `with_entry`, and `with_model` before calling `run`, or whose steps the test sends one at a time with `step`, and `wait` blocks until the call is due its next one. Dropping each value sends the verb that frees what it stands for.

Every call is lent `{}` as the Host's resources, unless `with_resources` gives the run another text:

Expand Down Expand Up @@ -178,11 +179,11 @@ A Host can also link a Plugin crate in and pass `runvil_plugin_sdk::entry::<Acme

## Writing a Plugin in C or C++

Include `runvil_plugin.h` and define `RvStatus runvil_plugin(RvCall *call)`. The header's opening comment is the whole contract: the verbs, who calls what on which thread, and what the Plugin must free on every path. The functions a Plugin may call back come with the verb they serve, in its arguments struct behind `call->args` (an `RvLoadArgs` for `RV_PLUGIN_LOAD`, an `RvPackNewArgs` for `RV_PACK_NEW`, an `RvPackRenderArgs` for `RV_PACK_RENDER`, an `RvToolCallArgs` for `RV_TOOL_CALL`), and each takes that struct's `ctx` first. A step of a tool call makes one file request through one of the nine file functions in its `RvToolCallArgs`, such as `file_read` or `file_write`, and returns `RV_PENDING`, and the call's next step finds the answer behind `answer` in its own `RvToolCallArgs`. A step that returns `RV_PENDING` without a file request parks its call, and the Host owes it no step until a wake. So such a step first calls `args->waker.wake(args->waker.ctx)` when it wants the next step right away, or a thread of the Plugin makes that call later, when what the tool call waits for is there. `wake` takes the `ctx` of its own `RvWaker` and not the step's, and it is the one function a Plugin may call on any thread, until `RV_PLUGIN_UNLOAD` returns. `resources` in the same `RvToolCallArgs` is the JSON text of what the Host offers every Plugin. A Plugin that shapes its tools for a model answers `RV_PACK_RENDER`: its `RvPackRenderArgs` holds the section entry in `entry`, the model as JSON text in `model`, the tools the section offered in `offered` and `offered_len`, and the functions `add_tool` and `refuse`. The Plugin calls `add_tool` once for each tool the model is shown and returns `RV_OK`. A Plugin that does not render answers `RV_ERR_UNSUPPORTED`, and the model is shown the offered tools as they are. `tests/c-layout` compiles the header as C99 and as C++ on every CI run.
Include `runvil_plugin.h` and define `RvStatus runvil_plugin(RvCall *call)`. The header's opening comment is the whole contract: the verbs, who calls what on which thread, and what the Plugin must free on every path. The functions a Plugin may call back come with the verb they serve, in its arguments struct behind `call->args` (an `RvLoadArgs` for `RV_PLUGIN_LOAD`, an `RvPackNewArgs` for `RV_PACK_NEW`, an `RvPackRenderArgs` for `RV_PACK_RENDER`, an `RvToolCallArgs` for `RV_TOOL_CALL`), and each takes that struct's `ctx` first. A step of a tool call makes one file request through one of the nine file functions in its `RvToolCallArgs`, such as `file_read` or `file_write`, and returns `RV_PENDING`, and the call's next step finds the answer behind `answer` in its own `RvToolCallArgs`. A step that returns `RV_PENDING` without a file request parks its call, and the Host owes it no step until a wake. So such a step first calls `args->waker.wake(args->waker.ctx)` when it wants the next step right away, or a thread of the Plugin makes that call later, when what the tool call waits for is there. `wake` takes the `ctx` of its own `RvWaker` and not the step's, and it is the one function a Plugin may call on any thread, until `RV_PLUGIN_UNLOAD` returns. `caller` (`RV_CALLER_MODEL` or `RV_CALLER_SCRIPT`), `section`, `entry`, `model`, and `resources` in the same `RvToolCallArgs` tell the step who asked for the call, which section and entry made it, the JSON text describing the round's model on a model call, and the JSON text of what the Host offers every Plugin. A Plugin that shapes its tools for a model answers `RV_PACK_RENDER`: its `RvPackRenderArgs` holds the section entry in `entry`, the model as JSON text in `model`, the tools the section offered in `offered` and `offered_len`, and the functions `add_tool` and `refuse`. The Plugin calls `add_tool` once for each tool the model is shown and returns `RV_OK`. A Plugin that does not render answers `RV_ERR_UNSUPPORTED`, and the model is shown the offered tools as they are. `tests/c-layout` compiles the header as C99 and as C++ on every CI run.

## Versions

The contract's version is `RV_API_MAJOR`.`RV_API_MINOR`, and `runvil-plugin-sys` has the same version number. While the major version is 0, each version is its own contract, so a Plugin built for 0.7 loads only in a Host that speaks exactly 0.7. From 1.0, minor versions only add, and a Plugin built for 1.n loads in any Host that speaks 1.n or later.
The contract's version is `RV_API_MAJOR`.`RV_API_MINOR`, and `runvil-plugin-sys` has the same version number. While the major version is 0, each version is its own contract, so a Plugin built for 0.8 loads only in a Host that speaks exactly 0.8. From 1.0, minor versions only add, and a Plugin built for 1.n loads in any Host that speaks 1.n or later.

Cargo allows one package per `links` value in a build, and `runvil-plugin-sys` sets `links = "runvil_plugin"`, so a Host can never link two versions of the contract into one binary.

Expand Down
2 changes: 1 addition & 1 deletion crates/runvil-plugin-sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "runvil-plugin-sdk"
version = "0.7.0"
version = "0.8.0"
description = "Write a runvil Plugin in safe Rust, over runvil-plugin-sys"
edition.workspace = true
# The floor a Plugin author's toolchain needs; CI's msrv job builds it.
Expand Down
11 changes: 7 additions & 4 deletions crates/runvil-plugin-sdk/src/entry/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ mod tests {
use std::task::{Context, Poll, Waker};

use runvil_plugin_sys::{
RV_ERR_FILE, RV_ERR_MISUSE, RV_FILE_ERROR_DENIED, RV_FILE_TYPE_FILE, RV_OK, RV_PENDING,
RV_TOOL_CALL, RvBytes, RvCall, RvDirEntries, RvFileAnswer, RvFileError, RvStat, RvStatus,
RvStr, RvToolCallArgs, RvToolResult, RvWaker,
RV_CALLER_MODEL, RV_ERR_FILE, RV_ERR_MISUSE, RV_FILE_ERROR_DENIED, RV_FILE_TYPE_FILE,
RV_OK, RV_PENDING, RV_TOOL_CALL, RvBytes, RvCall, RvDirEntries, RvFileAnswer, RvFileError,
RvStat, RvStatus, RvStr, RvToolCallArgs, RvToolResult, RvWaker,
};

use super::{receive, send, text};
Expand Down Expand Up @@ -722,7 +722,7 @@ mod tests {
) -> RvToolCallArgs {
RvToolCallArgs {
size: size_field::<RvToolCallArgs>(),
reserved: 0,
caller: RV_CALLER_MODEL,
ctx: ptr::null_mut(),
tool: rv_str("tool"),
arguments: rv_str("{}"),
Expand All @@ -742,6 +742,9 @@ mod tests {
ctx: ptr::null_mut(),
wake: unused_wake,
},
section: rv_str("Test"),
entry: rv_str("0.0"),
model: rv_str(""),
}
}

Expand Down
Loading
Loading