Skip to main content

Crate graphrt_ffi

Crate graphrt_ffi 

Source
Expand description

§graphrt-ffi

Embedding API for apps written in Kotlin (Android), Swift (iOS / macOS), Python and other languages supported by UniFFI.

The surface is deliberately small and JSON-string based so it stays stable across languages and versions:

Runtime::new(config_json)                       // providers, env, limits
  .register_wasm_plugin(bytes)                  // optional, sandboxed plugins
  .register_tool_provider(name, tools, provider) // native tools of the app
  .load_workflow(yaml_or_json) -> WorkflowHandle
WorkflowHandle.start(inputs_json, sys_json, run_listener) -> RunHandle   // typed events
WorkflowHandle.resume_paused(paused_run, secrets_json, run_listener) -> RunHandle
WorkflowHandle.run(inputs_json, sys_json, listener) -> RunHandle        // raw JSON events
WorkflowHandle.resume(snapshot_json, inputs_json, secrets_json, listener) -> RunHandle
RunHandle.abort(reason) / pause(reason) / wait_json(timeout_ms)
Runtime.set_credential_provider(provider, callback)  // keys from Keychain / Keystore
Runtime.install_skill / list_lessons / decide_lesson ...  // features `skills`, `learning`
validate_ui_block(json)                         // feature `ui`

Runs execute on a Tokio runtime owned by Runtime; listener callbacks are invoked from its worker threads, never from the caller’s thread. Nothing in this crate spawns processes, so every feature is usable on iOS and Android.

Every exported function and type exists whatever the cargo features: a capability that is compiled out fails with GraphrtError::Unsupported, so one set of generated bindings fits every build.

§Config JSON

{
  "providers": { "OPENAI_API_KEY": "sk-...", "OLLAMA_HOST": "http://10.0.2.2:11434" },
  "env": { "CRM_TOKEN": "..." },
  "limits": { "max_steps": 10000, "max_duration_ms": 60000, "default_node_timeout_ms": 30000 },
  "allow_private_network": false,
  "knowledge": { "...": "knowledge-retrieval datasets (see graphrt-rag)" },
  "mcp_servers": { "mcpServers": { "docs": { "url": "https://..." } } },
  "wasm": { "fuel": 1000000000, "memory_bytes": 67108864 },
  "credentials": {
    "openai": { "source": "external" },
    "proxy": { "source": "external", "kind": "openai", "base_url": "https://llm-proxy.example.com/v1" },
    "anthropic": { "source": "token_broker", "url": "https://api.example.com/llm-token", "app_auth": "external" }
  },
  "skills_dir": "/data/user/0/com.example/files/graphrt/skills",
  "learning_dir": "/data/user/0/com.example/files/graphrt/learning",
  "workspace": { "dir": "/data/user/0/com.example/files/graphrt/workspace", "read_only": false },
  "scripted_models": { "demo": [{ "text": "Hello from a scripted model." }] }
}

skills_dir (feature skills), learning_dir (feature learning) and workspace (feature files, the agents’ files tools) must be directories in the app’s own storage. scripted_models (feature scripted-llm) registers offline model providers for tests and previews.

credentials declares providers whose keys never appear in the config: an external provider asks the CredentialProvider registered with Runtime::set_credential_provider; a token_broker provider fetches short-lived tokens from your backend. See docs/credentials.md.

providers uses the same variable names as the CLI’s environment (OPENAI_API_KEY, OPENAI_BASE_URL, ANTHROPIC_API_KEY, OLLAMA_HOST, GRAPHRT_OPENAI_COMPAT_<NAME>_BASE_URL…). env values are exposed to workflows only for the env variables a workflow declares.

Structs§

ApprovalRequest
One node of a paused run that waits for an answer.
CredentialRecord
A credential returned by the host.
FormAction
A button of a human-input form.
FormField
A field of a human-input form.
HostToolCall
One call of a tool, as the app receives it.
HostToolResult
What a tool returns.
HostToolSpec
A tool the app offers to agents.
LessonInfo
A lesson of an agent.
LessonProposal
A lesson proposed from feedback on a run.
PausedRun
A paused run: its requests, the answers given so far and its snapshot.
PendingToolCall
A tool call waiting for approval.
RunHandle
Controls one run.
RunResult
The result of a run that did not pause.
Runtime
A configured runtime: node registry, host services and a thread pool. Create one per app and share it.
SkillGrant
A tool granted to a skill.
SkillInfo
An installed skill.
SkillInstall
What to install.
UiBlock
A UI block an agent produced, checked against its schema.
UniFfiTraitVtableEventListener
WorkflowHandle
A compiled workflow, ready to run any number of times concurrently.

Enums§

GraphrtError
Errors surfaced to foreign code as exceptions.
HostToolEffect
What a call of a tool does outside the run, as effects of a plugin tool say it. Send and Pay make an agent ask a person before each call.
LessonDecision
A decision about a lesson.
LessonKind
What kind of thing a lesson is.
LessonStatus
Where a lesson is in its life.
RequestKind
What a request asks for.
RunEvent
An event of a run.
RunStatus
How a run ended.
ToolCallError
An error a tool reports. The model receives reason and may try again.

Traits§

CredentialProvider
Supplies model credentials. Implemented in the host language.
EventListener
Receives the events of one run. Implemented in the host language.
RunListener
Receives the typed events of one run. Implemented in the host language.
ToolProvider
Native tools implemented by the app.

Functions§

graphrt_version
Version of the embedded runtime.
uniffi_graphrt_ffi_fn_init_callback_vtable_eventlistener
validate_ui_block
Check a UI block ({"$ui": "chart", "version": 1, ...}) against its schema, for blocks an app receives outside a run (stored messages, a server).