Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

Serve Granite via OpenRouter as the primary lane when watsonx is unset

nirholas8da35fc4dcf6
diff --git a/api/_lib/watsonx.js b/api/_lib/watsonx.js
index 3ad057b00..169a5ed1f 100644
--- a/api/_lib/watsonx.js
+++ b/api/_lib/watsonx.js
@@ -166,15 +166,26 @@ export async function watsonxEmbed(cfg, { inputs, model } = {}) {
// answer rather than a token stream — e.g. asking Granite to name a cluster of
// semantically-similar agents. Returns the assistant text and token usage.
//
-// If the watsonx call fails outright (IAM auth, quota, region outage, an
-// unsupported model) AND an OpenRouter key is configured, this falls over to
-// OpenRouter-hosted Granite — the SAME model family (ibm-granite/*), just
-// different hosting — so every Granite consumer (Oracle, Twin, Galaxy, cluster
-// naming) stays up during a watsonx outage instead of hard-failing. Invisible
-// to callers: the return shape is identical and `provider` marks which lane
-// served. When OpenRouter is unset the original watsonx error propagates
-// unchanged, exactly as before.
+// Granite lane policy:
+// • watsonx creds present → watsonx leads; on any failure (IAM auth, quota,
+// region outage, unsupported model) it falls over to OpenRouter-hosted
+// Granite — the SAME model family (ibm-granite/*), just different hosting.
+// • watsonx creds ABSENT → Granite runs on OpenRouter directly as the primary
+// (no point attempting, and failing, the watsonx call first). This is what
+// lets the Granite features run on a deployment with no IBM account at all.
+// • neither configured → the real watsonx "not set" error propagates, exactly
+// as before.
+// Invisible to callers: the return shape is identical and `provider` marks which
+// lane served ('watsonx' | 'openrouter-granite').
export async function watsonxChatComplete(cfg, { messages, model, maxTokens, temperature } = {}) {
+ // No watsonx credentials on this deployment → serve Granite from OpenRouter
+ // directly rather than attempting a doomed watsonx round-trip on every call.
+ const watsonxUsable = Boolean(cfg?.apiKey && (cfg?.projectId || cfg?.spaceId));
+ if (!watsonxUsable) {
+ const direct = await graniteOpenRouterFallback({ messages, maxTokens, temperature });
+ if (direct) return direct;
+ // No OpenRouter either — fall through so the genuine watsonx error surfaces.
+ }
// Decoding params are TOP-LEVEL on the chat endpoint (it is OpenAI-shaped) —
// not nested under a `parameters` wrapper (that's the older text/generation
// API). Nesting them here silently dropped max_tokens/temperature; keep this
diff --git a/api/_mcpibm/tools.js b/api/_mcpibm/tools.js
index f390cc99b..665c8f97f 100644
--- a/api/_mcpibm/tools.js
+++ b/api/_mcpibm/tools.js
@@ -34,6 +34,23 @@ function graniteConfig() {
return cfg;
}
+// Relaxed config for the TEXT-generation tools (chat / code / analyze). Those run
+// on Granite via watsonxChatComplete(), which serves OpenRouter-hosted Granite
+// (ibm-granite/*) when watsonx creds are absent — so they only need SOME Granite
+// lane. The embed and forecast tools stay on the strict graniteConfig() above:
+// Granite embeddings and TimeSeries forecasting have no OpenRouter equivalent.
+function graniteChatConfig() {
+ const cfg = watsonxConfig();
+ if (cfg.configured) return cfg;
+ const openrouterAvailable = Boolean(
+ process.env.OPENROUTER_API_KEY?.trim() || (process.env.OPENROUTER_FALLBACK_KEYS || '').trim(),
+ );
+ if (openrouterAvailable) return cfg; // watsonxChatComplete falls over to OpenRouter Granite
+ throw new Error(
+ 'IBM Granite is not configured on this server (set WATSONX_API_KEY and WATSONX_PROJECT_ID, or an OPENROUTER_API_KEY backstop).',
+ );
+}
+
// MCP tool annotations (2025-06-18 spec) — mirrors the constants shipped in
// packages/ibm-x402-mcp/src/tools/_shared.js so the hosted and stdio transports
// advertise identical semantics. Granite inference reads the operator's models
@@ -250,7 +267,7 @@ const chatTool = {
},
},
async handler({ messages, model, max_new_tokens, temperature }) {
- const cfg = graniteConfig();
+ const cfg = graniteChatConfig();
const result = await watsonxChatComplete(cfg, {
messages,
model,
@@ -339,7 +356,7 @@ const codeTool = {
},
},
async handler({ task, prompt, language, context }) {
- const cfg = graniteConfig();
+ const cfg = graniteChatConfig();
const systemContent = buildCodeSystemPrompt(task, language);
const userContent = context ? `${prompt}\n\nContext: ${context}` : prompt;
const result = await watsonxChatComplete(cfg, {
@@ -511,7 +528,7 @@ const analyzeTool = {
},
},
async handler({ document, analysis_type = 'general', language }) {
- const cfg = graniteConfig();
+ const cfg = graniteChatConfig();
const systemPrompt = buildAnalysisPrompt(analysis_type, language);
const result = await watsonxChatComplete(cfg, {
messages: [