~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/asyncai

0
0
main
42 lines · 874 B Raw History
use std::sync::atomic::{AtomicU64, Ordering};

pub type AgentId = String;
pub type ThreadId = String;
pub type RunId = String;
pub type FileId = String;

#[derive(Debug)]
pub struct IdGen {
    prefix: String,
    counter: AtomicU64,
}

impl IdGen {
    pub fn new(prefix: impl Into<String>) -> Self {
        Self {
            prefix: prefix.into(),
            counter: AtomicU64::new(0),
        }
    }

    pub fn next(&self, kind: &str) -> String {
        let n = self.counter.fetch_add(1, Ordering::Relaxed);
        format!("{}-{}-{}", self.prefix, kind, n)
    }

    pub fn next_agent(&self) -> AgentId {
        self.next("agent")
    }

    pub fn next_thread(&self) -> ThreadId {
        self.next("thread")
    }

    pub fn next_run(&self) -> RunId {
        self.next("run")
    }

    pub fn next_file(&self) -> FileId {
        self.next("file")
    }
}