Agent: durable turn and queue state, visible inbox closure, OpenAI phase; prompt fixes; fetch/bench fixes
This commit is contained in:
@@ -2,6 +2,32 @@
|
||||
use sausage_actor::{Inbox, Message, json, me, reply};
|
||||
use wstd::http::{Body, Client, Request};
|
||||
|
||||
fn next_fetch_id() -> Result<u64, String> {
|
||||
let previous = match std::fs::read_to_string("/data/fetch-sequence") {
|
||||
Ok(value) => value.parse::<u64>().map_err(|e| e.to_string())?,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => existing_fetch_id()?,
|
||||
Err(e) => return Err(e.to_string()),
|
||||
};
|
||||
let next = previous
|
||||
.checked_add(1)
|
||||
.ok_or_else(|| "fetch ID space exhausted".to_string())?;
|
||||
std::fs::write("/data/fetch-sequence", next.to_string()).map_err(|e| e.to_string())?;
|
||||
Ok(next)
|
||||
}
|
||||
|
||||
fn existing_fetch_id() -> Result<u64, String> {
|
||||
let entries = match std::fs::read_dir("/public/fetch") {
|
||||
Ok(entries) => entries,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(0),
|
||||
Err(e) => return Err(e.to_string()),
|
||||
};
|
||||
Ok(entries
|
||||
.filter_map(Result::ok)
|
||||
.filter_map(|entry| entry.file_name().to_str()?.parse().ok())
|
||||
.max()
|
||||
.unwrap_or(0))
|
||||
}
|
||||
|
||||
async fn fetch(n: u64, message: Message) {
|
||||
let url = message.body["url"].as_str().unwrap_or_default();
|
||||
let started = wstd::time::Instant::now();
|
||||
@@ -41,12 +67,13 @@ async fn fetch(n: u64, message: Message) {
|
||||
fn main() {
|
||||
wstd::runtime::block_on(async {
|
||||
let inbox = Inbox::start();
|
||||
let mut n = 0;
|
||||
while let Some(message) = inbox.recv().await {
|
||||
match message.kind() {
|
||||
"fetch" => {
|
||||
n += 1;
|
||||
wstd::runtime::spawn(fetch(n, message)).detach();
|
||||
"fetch" => match next_fetch_id() {
|
||||
Ok(n) => wstd::runtime::spawn(fetch(n, message)).detach(),
|
||||
Err(reason) => {
|
||||
let _ = reply(&message, json!({"type": "error", "reason": reason}));
|
||||
}
|
||||
}
|
||||
"ping" => {
|
||||
let _ = reply(&message, json!({"type": "pong"}));
|
||||
|
||||
Reference in New Issue
Block a user