graphrt-llm 0.7.0

Model providers (OpenAI-compatible, Anthropic, Gemini, AWS Bedrock, TypeSafe Jev) and LLM, classifier, extractor and agent nodes for the graphrt workflow runtime.
Documentation
//! The user a run's memory and lessons are scoped to: the id the node
//! configuration renders (by default `sys.user_id`) with where it comes from
//! (`sys.user_source`, set by the host over anything a client sent). The
//! same id from two sources is two users (`docs/memory.md`).

use graphrt_core::NodeContext;
use graphrt_core::memory::{USER_SOURCE_VAR, UserSource};
use graphrt_core::node::NodeError;
use graphrt_core::template::Template;

/// The scoped user of a run.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct RunUser {
    /// `<source>/<id>`, `None` for a run without a user.
    pub user: Option<String>,
    /// The host named the source of this very user (`sys.user_source` set
    /// and the id is `sys.user_id`): only then do the user's old records
    /// (kept under the id without its source) move to the scoped id.
    pub host_named: bool,
}

/// The scoped user for `rendered`, the id a node configuration rendered.
///
/// The host's source holds for the user it named (`sys.user_id`). Any other
/// id the configuration renders (an input, say) is a name the caller chose:
/// `api`, never `user` or `channel`. A run whose host named no source is `api`.
///
/// @throws NodeError - `what_scope` (fatal) for a `sys.user_source` that
///   names no source: a host that sets it wrongly must not fall back to a
///   shared scope. `what` names the setting in the message.
pub(crate) fn run_user(
    ctx: &NodeContext,
    rendered: Option<String>,
    kind: &'static str,
    what: &str,
) -> Result<RunUser, NodeError> {
    let named = match sys_text(ctx, USER_SOURCE_VAR) {
        None => None,
        Some(word) => Some(UserSource::parse(word.trim()).ok_or_else(|| {
            NodeError::fatal(kind, format!("{what}: `sys.user_source` must be `user`, `api` or `channel`"))
        })?),
    };
    let host_user = rendered.is_some() && rendered == sys_text(ctx, "user_id");
    let source = named.filter(|_| host_user).unwrap_or(UserSource::Api);
    Ok(RunUser { user: rendered.map(|id| source.scoped(&id)), host_named: named.is_some() && host_user })
}

fn sys_text(ctx: &NodeContext, name: &str) -> Option<String> {
    let template = Template::parse(&format!("{{{{#sys.{name}#}}}}"));
    Some(template.render(&ctx.pool)).filter(|text| !text.trim().is_empty())
}