| Crates.io | context-weaver |
| lib.rs | context-weaver |
| version | 0.1.1 |
| created_at | 2025-04-22 00:20:33.96452+00 |
| updated_at | 2025-05-04 01:02:02.219697+00 |
| description | (WIP) A procedural text evaluation engine |
| homepage | |
| repository | https://github.com/Cruxial0/context-weaver |
| max_upload_size | |
| id | 1643403 |
| size | 180,012 |
Warning: This project is in an early development stage. Breaking changes will be frequent.
ContextWeaver is a (WIP) framework for dynamically managing and injecting “lore” into text-generation contexts (e.g., LLM prompts, game scripts, dynamic UIs). It uses entries with activation conditions plus inline processors to produce context-aware, procedurally generated text.
📄 Context Injection
Each entry contains a payload of text (“lore”) and one or more activation conditions. When a condition is met, that text is inserted into the target context (for example, an LLM prompt or chat history).
Activation conditions: see Roadmap.
⚙️ Dynamic Content via Processors
Entries can include inline “processors”—small snippets of logic that run at evaluation time and replace themselves with generated output. This makes your injected text dynamic rather than static.
Processors embed procedural logic directly in your entries using a familiar, JSON-like syntax.
@[weaver.core.rng(min: 0, max: 100)]
{
property: "value", // no quotes around property
}
<trigger id=0>
The generated number is: @[weaver.core.rng(min: 0, max: 100)]
→
The generated number is: 73
You can nest processors arbitrarily:
@[weaver.core.wildcard(
items: [
"Weather: @[weaver.core.wildcard(items:[\"sunny\",\"cloudy\",\"rainy\"])]",
"Number: @[weaver.core.rng(min:0,max:100)]"
]
)]
Evaluation steps:
"cloudy" or a random number).Use variables to inject key-value data at evaluation time.
{{SCOPE:VAR_NAME}}global — accessible everywhereENTRY_ID — only within that entryMacros let you write control flow in your entries. They live in {# ... #} blocks.
{# if CONDITION #}
…true branch…
{# else #}
…false branch…
{# endif #}
| Function | Input | Returns |
|---|---|---|
len(x) |
String, List, HashMap | Integer length |
contains(x,y) |
String/List & String | Boolean membership |
{# foreach ITEM in COLLECTION #}
…use {{ITEM}}…
{# endforeach #}
Iterate over lists, maps, or strings; collections may mix types.
Define your own processors via a bridge interface. Below is a Rust example.
use context_weaver::core::processors::PluginBridge;
use serde_json::Value;
struct MyBridge;
impl PluginBridge for MyBridge {
type PluginId = u32;
fn invoke_plugin(
&self,
plugin_id: Self::PluginId,
properties: Value
) -> Result<String, WorldInfoError> {
match plugin_id {
0 => dummy_logic(properties),
_ => Err(WorldInfoError::PluginError("Unknown plugin".into())),
}
}
}
fn dummy_logic(props: Value) -> Result<String, WorldInfoError> {
// Parse input properties carefully
let items = props.get("items").and_then(Value::as_array)
.ok_or(WorldInfoError::PluginError("'items' missing".into()))?;
let choice = items
.iter()
.filter_map(Value::as_str)
.choose(&mut rand::thread_rng())
.ok_or(WorldInfoError::PluginError("No valid items".into()))?;
Ok(format!("Forecast: {}", choice))
}
let registry = ProcessorRegistry::new(Arc::new(MyBridge));
registry.register_plugin_processor("dummy", "forecast");
let input = r#"@[weaver.plugin.dummy.forecast(
plugin_author:"dummy",
plugin_name:"forecast",
plugin_id:0,
plugin_data:{items:["sunny","rainy"]}
)]"#;
let mut wi = WorldInfo::new(Box::new(registry));
wi.insert_entry(WorldInfoEntry::create("e1", 0).with_text(input));
let result = wi.evaluate()?;
println!("{}", result); // e.g. "Forecast: rainy"
Naming convention:
weaver.plugin.<author>.<name>
cargo add context-weaver
git clone https://github.com/Cruxial0/context-weaver.git
# Add to your Cargo.toml as a workspace member or dependency path
| Construct | Syntax | Supported |
|---|---|---|
| Processor | @[processor.name(props)] |
Yes |
| Trigger | <trigger id=…> |
Yes |
| If-Macro | {# if … #}…{# endif #} |
Yes |
| Foreach-Macro | {# foreach … #}…{# endforeach #} |
Yes |
| Variable mutation | @[set], @[modify], etc. |
No |
| Documents | [[DOCUMENT_ID]] |
Kinda (no) |
Contributions are welcome! Please open issues or PRs on the GitHub repository.