Files
planet/docs/technical/en/agents-aiprovider.md
T

27 KiB
Raw Blame History

AI Provider Guide

Overview

aiprovider is the model-adapter service for Planet.

It isolates model-vendor details from the main backend so the rest of the system can call a stable business API:

  • Caller service -> planet backend
  • planet backend -> aiprovider
  • aiprovider -> concrete model provider

The recommended default is:

  • External and cross-service callers use planet backend
  • Only infrastructure-grade internal jobs call aiprovider directly

Responsibilities

backend is responsible for:

  • authentication and authorization
  • business-level request shaping
  • stable /api/v1/ai/... endpoints
  • internal service-to-service authentication toward aiprovider
  • reading the default provider, model, and per-provider keys saved in Settings, then overriding aiprovider .env defaults through internal headers

aiprovider is responsible for:

  • model protocol adaptation
  • provider selection by .env when no backend override headers are present
  • timeout and lightweight retry
  • request tracing via X-Request-ID

This now follows an OpenClaw-like seam:

  • AI_PROVIDER identifies the vendor or logical provider
  • AI_PROVIDER_API identifies the wire adapter

That split makes MiniMax, Claude-compatible gateways, and self-hosted OpenAI-compatible services easier to model without overloading one config field.

Supported Providers

aiprovider currently supports these provider identities:

  • openai
  • anthropic
  • minimax
  • ollama

Supported request adapters:

  • openai-completions
  • openai-responses
  • anthropic-messages
  • ollama-generate

Backward-compatible aliases still accepted:

  • openai_compatible
  • anthropic_compatible
  • claude_compatible

Provider mapping:

  • vLLM, LM Studio, One API: AI_PROVIDER=openai, AI_PROVIDER_API=openai-completions
  • MiniMax: AI_PROVIDER=minimax, AI_PROVIDER_API=anthropic-messages
  • Claude-compatible gateways: AI_PROVIDER=anthropic, AI_PROVIDER_API=anthropic-messages
  • Ollama: AI_PROVIDER=ollama, AI_PROVIDER_API=ollama-generate

API Surfaces

Main backend API

Preferred stable entrypoints:

  • GET /api/v1/ai/provider/status
  • POST /api/v1/ai/situational-awareness/analyze

Authentication:

  • Authorization: Bearer <jwt>

Optional tracing header:

  • X-Request-ID: <caller-generated-id>

The backend will propagate X-Request-ID to aiprovider and return the same header in the response.

Settings API

The AI settings page uses:

  • GET /api/v1/settings/integrations
  • PUT /api/v1/settings/integrations
  • POST /api/v1/settings/integrations/ai-provider/connect
  • GET /api/v1/settings/integrations/ai-provider/secrets
  • GET /api/v1/settings/integrations/ai-provider/presets
  • POST /api/v1/settings/integrations/ai-provider/presets/refresh-public
  • POST /api/v1/settings/integrations/ai-provider/presets/{provider}/refresh
  • GET /api/v1/settings/ai-prompts
  • PUT /api/v1/settings/ai-prompts/{task_key}
  • POST /api/v1/settings/ai-prompts/{task_key}/reset

These endpoints require an authenticated user, and preset catalog refresh endpoints require administrator privileges. The secrets endpoint is only used when the settings page reveals a key or token; hiding the field restores the masked preview.

Model refresh and lightweight checks share backend/app/services/llm_model_catalog.py. When an API key is configured (draft, saved, or env), refresh queries the provider endpoint directly while preserving regional hosts and custom gateway paths. For official public catalogs (openrouter.ai, opencode.ai) and local Ollama, catalog refresh does not require or send an API key so stale or invalid credentials do not block public discovery. For keyed built-in providers on their official hosts (minimax, openai, anthropic, deepseek, alibaba, moonshotai, glm, meta, gemini, grok), keyless refresh automatically falls back to public aggregators (models.dev/api.json primary -> openrouter.ai/api/v1/models backup, cached in-process for 5 minutes); pointing those providers at a custom private gateway still requires and sends credentials. Lightweight connectivity checks continue querying the configured endpoint directly with credentials (Ollama remains key-free under both ollama-generate and openai-completions) and never fall back to aggregators. Missing required credentials, invalid base URLs, or unsupported protocols return 400; upstream/aggregator failures return 502 with safe errors and retain the previous catalog. POST /api/v1/settings/integrations/ai-provider/presets/refresh-public refreshes and persists all currently credential-free and aggregator-backed presets in one pass, isolating per-provider failures while preserving prior catalogs.

Successful catalogs and refreshed_at are stored under llm_provider_preset:<provider> in system_settings. Dynamic categories use the provider preset as their defaults and are validated before committing. Listing prefers saved catalogs and otherwise shows bundled suggestions. Refresh never modifies the active model, protocol, URL or credentials in external_integrations. The frontend retains the draft and previously loaded models when listing fails, with an explicit error message.

Model Catalog Endpoints

These endpoints align with official and public aggregator catalogs. The table lists discovery URLs; the form still takes the generation base URL. Direct keyed queries must use credentials for the configured service region; keyless official hosts query mapped public aggregator catalogs.

Provider Model list GET endpoint Authentication and parsing
MiniMax https://api.minimaxi.com/anthropic/v1/models; international: api.minimax.io Keyed: x-api-key + has_more/last_id; keyless: aggregator minimax-cn/minimax
OpenAI https://api.openai.com/v1/models Keyed: Bearer; keyless: aggregator openai
Anthropic https://api.anthropic.com/v1/models Keyed: x-api-key, anthropic-version; keyless: aggregator anthropic
DeepSeek https://api.deepseek.com/v1/models Keyed: Bearer; keyless: aggregator deepseek
Alibaba Model Studio /api/v1/models on the configured regional host Keyed: Bearer + page_no + capabilities=TG; keyless: aggregator alibaba-cn/alibaba
Moonshot / Kimi https://api.moonshot.ai/v1/models; China: api.moonshot.cn Keyed: Bearer; keyless: aggregator moonshotai/moonshotai-cn
Zhipu GLM (CN) https://open.bigmodel.cn/api/paas/v4/models Keyed: Bearer; keyless: aggregator zhipuai
Meta AI https://api.meta.ai/v1/models Keyed: Bearer; keyless: aggregator meta (backup meta-llama)
Google Gemini https://generativelanguage.googleapis.com/v1beta/openai/models Keyed: Bearer (models/ prefix stripped); keyless: aggregator google
xAI Grok https://api.x.ai/v1/models Keyed: Bearer; keyless: aggregator xai
OpenRouter https://openrouter.ai/api/v1/models Official public catalog refreshes without sending a key; custom gateways and connectivity checks send Bearer; data[].id
OpenCode Go https://opencode.ai/zen/go/v1/models Official public catalog refreshes without sending a key; custom gateways and connectivity checks send Bearer; data[].id
Ollama /api/tags on the configured server (or /v1/models under openai-completions) Local servers need no key; models[].model/name or data[].id; empty means no installed models

New Model Studio endpoints for Beijing, Tokyo, Frankfurt and Virginia require the actual workspace host, such as <WorkspaceId>.cn-beijing.maas.aliyuncs.com. Singapore uses dashscope-intl.aliyuncs.com; Hong Kong uses cn-hongkong.dashscope.aliyuncs.com. Discovery changes only the path, never guesses a workspace or switches credential regions. If a legacy Beijing host no longer accepts the account, update the base URL from the provider console.

Pagination and retries have a 30-second overall deadline. Network failures and 502/503/504 allow up to two attempts; authentication failures are not retried. Models are sorted by supplied creation/release dates, preserving upstream order on ties, without arbitrary list truncation. Public catalog membership does not establish account-specific generation permission.

Admin separates AI configuration from workflow orchestration across two areas:

  • Operations and Settings → AI (/ai):
    • Model Providers: manages provider, wire adapter, default model, LLM API key, proxy URL/token, catalog refresh (keyless official providers fall back to public aggregator catalogs sorted selected-first + MRU + release date with a 6-item collapsible list), set-as-default, and connectivity testing.
    • Tool Calling: manages WebSearch (including keyless SearXNG) and OCR tool credentials.
  • AI Advisor (/advisor/*):
    • Playground (/advisor/playground): unified multimodal debugging workbench (text, images, sampled video keyframes, SSRF-isolated web links, JSON, and automatic Earth context mounting) with slash commands, Markdown rendering, and inline 👍/👎 assistant feedback persisted in WorkflowRun.budget_used.
    • Workflows & Nodes (/advisor/workflows): @xyflow/react visual graph studio over all 10 built-in workflows (including meta.workflow_evolver) and custom DAGs executed via dag_executor.py, supporting Earth interactive bindings, immutable revisions/rollback, Fixture A/B evaluation, and 3-tier bounded self-evolution controls.
    • Run History (/advisor/runs): execution trace audit surfacing model, latency, revision, and feedback filters with one-click canvas replay and trace-guided tuning.

Three-Tier Workflow Self-Evolution and Meta-Evolution

Implemented in workflow_evolver.py with zero database schema changes or migrations by reusing ai_workflows, ai_workflow_revisions, ai_workflow_fixtures, workflow_runs.budget_used, and system_settings (ai_evolution_policy and ai_evolution_ledger):

  • Tier 0 (Immutable Python Constitution Kernel): enforces acyclic DAG structure and trigger/output preservation, blocks emptying system_prompt, restricts tools to allowlisted IDs, caps auto-mode blast radius (<= 2 nodes and <= 40% of nodes), freezes the verify_output node on meta.workflow_evolver, blocks custom meta.* creation (single-level recursion lock), enforces 24h / 7d cooldowns and daily auto-evolution quotas, and runs a 5-run canary window with automatic rollback on consecutive failures or elevated bad-rating rates (with a 3-outcome meta-canary rollback guard on meta.workflow_evolver).
  • Tier 1 (Business Optimization & Model Drift Adaptation): GET /api/v1/ai/evolution/health/{workflow_id} computes 0-token health metrics, active model capability drift, unused tools, and DGM best-parent revision recommendations; POST /api/v1/ai/evolution/propose/{workflow_id} reads live diag_node / patch_node prompts from meta.workflow_evolver alongside historical ledger lessons; POST /api/v1/ai/evolution/eval/{workflow_id} runs Fixture A/B checks and POST /api/v1/ai/evolution/incubate proposes new workflows from frequent default.general queries. Non-blocking post-run hooks run via schedule_post_run_evolution.
  • Tier 2 (Meta-Evolution & Extensibility Hooks): POST /api/v1/ai/evolution/meta-evolve evolves meta.workflow_evolver prompts under an independent meta_mode (off | draft | auto) using objective ledger outcomes; EvolutionSignalDetector (SIGNAL_DETECTORS) and EvolutionEvaluator (EVALUATORS) provide clean Protocol extension points.
  • Evolution safety constraints (hardened after the v0.82.0 review):
    • "No change" is a valid outcome: a valid empty node_patches list ends the round with applied_status: no_change (no revision, no ledger entry); the deterministic fallback only patches on real signals (failures/downvotes/model capability mismatch/admin instruction/ledger outcomes) and never pads prompts.
    • Gradable A/B fixtures: fixtures accept expect (numbers, must_include, must_not_include, require_uncertainty_without_evidence, min_evidence, stored in context_json.expect) on POST /api/v1/ai/workflows/{id}/fixtures; grading covers execution errors, guardrail verification, and assertions, and any fixture that passes on live but fails on the candidate yields regressed. Auto-publish requires the A/B gate: missing fixtures, fixtures without assertions, or any regression block publication.
    • Live revision baseline: background/auto-publish evolution always starts from the live revision, never reads or clears unpublished human drafts (auto-draft mode skips when a human draft exists), records the full candidate-vs-live diff in changes_vs_live, and applies the auto blast-radius limit to that full diff.
    • Canary cannot overwrite newer revisions: before settling, the canary checks the live revision is still its target; if a manual publish/rollback superseded it, the entry becomes superseded without rollback (same for the meta canary).
    • Meta fields must take effect: meta-evolution may only change diag_node.system_prompt / prompt and patch_node.system_prompt / prompt / thinking_enabled, all of which feed the business request (build_business_evolution_request); meta-evolution stays off by default.
  • Earth workflow data, evidence, and card output: dag_executor.py selects each data source's own target ID by entity type (cable/vessel/BGP multi-select is order-independent); interactive / ingest edges route strictly by trigger source (a news click no longer runs the RSS localization branch); strict-JSON nodes such as compute.posture.brief run non-streaming, are verified via StrategicPostureBrief + evidence IDs, rendered as narrative, and the output node compiles the compute view_spec workbench (delivered via a closing meta event and done.view_spec); a failed require_evidence guardrail appends a "cause pending verification" block with follow-ups and marks done.status as unverified with verification.
  • Draft binding isolation: Earth entry bindings edited in a draft live in draft_graph.bindings (returned as draft_bindings), apply only on publish, and are restored by discarding the draft; each published revision snapshots bindings in graph_json.bindings so rollback restores them. "Save bindings only" (PATCH /workflows/{id}) remains an explicit immediate action.
  • Feedback & Policy APIs: POST /api/v1/ai/runs/{run_id}/feedback, GET /api/v1/ai/runs?feedback=negative|positive, GET/PUT /api/v1/ai/evolution/policy, GET /api/v1/ai/evolution/ledger, and POST /api/v1/ai/evolution/ledger/{entry_id}/dismiss.

Save, set-as-default, and connectivity testing remain three separate responsibilities. Runtime prompts resolve from published AIWorkflow nodes and versioned default_prompts.json resources while preserving task_key compatibility.

Prompt Boundary

aiprovider is a pure model adapter and injects no business system prompts. Each workflow node and task key resolves its own effective prompt and serializes structured context as compact JSON.

Agent And Tool Boundary

Agent workflows belong in backend, not aiprovider. aiprovider transparently passes normalized tools, tool_choice, response_format, native_web_search, and multi-turn tool / tool_calls messages across adapters and normalizes returned tool_calls + stop_reason, while tool whitelists, validation, permission checks, WorkflowRun audit logs, and Diff approval remain strictly in the backend.

AI provider internal API

Internal-only endpoints:

  • GET /v1/provider/status
  • POST /v1/analyze

Authentication:

  • X-Provider-Token: <shared-secret>

Optional tracing header:

  • X-Request-ID: <caller-generated-id>

Request Example

Call through backend

curl -X POST http://localhost:8000/api/v1/ai/situational-awareness/analyze \
  -H "Authorization: Bearer <access_token>" \
  -H "X-Request-ID: bgp-incident-20260407-001" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "BGP异常研判",
    "objective": "总结当前风险并给出处置建议",
    "observations": [
      "collector A 在 5 分钟内出现多次 origin 变更",
      "异常集中在同一地区前缀"
    ],
    "constraints": [
      "不要编造不存在的数据",
      "区分事实和推断"
    ],
    "context": {
      "source": "bgp-monitor",
      "severity": "high"
    }
  }'

Call aiprovider directly

curl -X POST http://localhost:8010/v1/analyze \
  -H "X-Provider-Token: change_me" \
  -H "X-Request-ID: ai-batch-job-001" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "链路波动分析",
    "objective": "给出简要态势摘要和下一步建议",
    "observations": [
      "多个节点出现延迟上升"
    ],
    "constraints": [
      "不要假设根因已经确认"
    ],
    "context": {
      "region": "APAC"
    }
  }'

Response Shape

Both backend and aiprovider return the same payload shape:

{
  "provider": "minimax",
  "api": "anthropic-messages",
  "model": "MiniMax-M2.7",
  "content": "1) 态势摘要 ...",
  "content_blocks": [],
  "text_blocks": [],
  "thinking_blocks": [],
  "raw_response": {}
}

Both services also return:

  • X-Request-ID: <id>

Configuration

Runtime Configuration Flow

The backend Settings system owns the global LLM default. The runtime flow is:

  1. Frontend or application code calls a backend /api/v1/ai/... endpoint.
  2. backend reads category = external_integrations from the PostgreSQL system_settings table.
  3. payload.ai_provider.default_provider selects the active provider.
  4. payload.ai_provider.providers[provider] supplies that provider's api_key, provider_api, base_url, model, max_tokens, and anthropic_version.
  5. backend converts those values to internal headers such as X-AI-Provider, X-AI-Provider-API, X-AI-Base-URL, X-AI-API-Key, and X-AI-Model.
  6. aiprovider uses those headers to override its .env defaults before calling the real model vendor.

After the AI settings page saves a new default provider/model/key, Playground, alert briefs, datasource mapping generation, and other backend AI calls all use that same default.

Persistence Shape

AI settings are persisted in PostgreSQL, not a JSON file. The core payload shape is:

{
  "ai_provider": {
    "service_url": "http://localhost:8010",
    "service_token": "",
    "default_provider": "openai",
    "providers": {
      "openai": {
        "provider_api": "openai-completions",
        "base_url": "https://api.openai.com/v1",
        "model": "gpt-5.1",
        "api_key": "<saved secret>",
        "max_tokens": 4096,
        "anthropic_version": "2023-06-01"
      },
      "minimax": {
        "provider_api": "anthropic-messages",
        "base_url": "https://api.minimaxi.com/anthropic",
        "model": "MiniMax-M2.7",
        "api_key": "<saved secret>",
        "max_tokens": 1200,
        "anthropic_version": "2023-06-01"
      }
    },
    "timeout_seconds": 60,
    "retry_attempts": 2
  }
}

Legacy single-slot settings are mapped to providers[provider] on read and are written back in the new shape on save.

Key Fallback

Each provider has its own key slot. Resolution order is:

  1. providers[provider].api_key in PostgreSQL
  2. the provider-specific variable in aiprovider/.env, such as OPENAI_API_KEY, MINIMAX_API_KEY, or ANTHROPIC_API_KEY
  3. the generic AI_API_KEY in aiprovider/.env

.env is only a fallback. After the settings page saves successfully, or after the connection test succeeds, PostgreSQL becomes the global default source.

Admin must compute key status per provider or tool:

  • If the database has a key for the current provider/tool, show configured.
  • If the database has no key but the fallback provider, model, or tool matches the current item, show the fallback masked preview.
  • If neither database nor matching fallback exists, show not configured; a generic .env key for another provider must not make this item appear configured.
  • Masking keeps the prefix before the first -, for example sk-********. Plaintext reveal is only available inside the authorized configuration page.

Tool keys follow the same rule. WebSearch and OCR must match the current tool and provider before they can use fallback credentials.

Lightweight Connectivity Testing

The Admin plug button checks proxy configuration and queries the current provider catalog. It returns success=true only when discovery succeeds and contains the selected model. A 404, authentication error, invalid response or missing model never becomes success based on bundled suggestions. The message distinguishes discovery from generation; Playground or a business task verifies actual generation. Testing never saves the draft or switches the default provider.

OpenCode Go Routing Model

OpenCode Go protocol mappings are owned by the backend catalog: MiniMax M3/M2.7/M2.5 and the verified Qwen3.6/3.7/3.8 models use Anthropic Messages; GPT-5.6 Luna, Grok 4.6 and Muse Spark Contributor use Responses; other supported models use Chat Completions. The official /models response currently supplies IDs only, so new models still require checking the documented endpoint table. Selecting a model updates its protocol in the draft, and backend mappings supersede known stale mappings. Responses uses input, max_output_tokens and store=false, and parses text and reasoning summaries. OpenCode calls carry an application User-Agent and a stable Playground conversation identifier. Generic thinking=enabled is translated to MiniMax M3’s adaptive format.

Settings Page Behavior

  • The Provider select controls the global default provider.
  • The model select saves the default model for the selected provider.
  • The LLM API Key field shows a masked preview while hidden; keys with a - prefix keep the prefix, for example sk-********, and keys without a prefix are fully masked.
  • Clicking the eye icon fetches and displays the full plaintext value; hiding restores the masked preview.
  • Save AI Configuration saves the current form as the global default.
  • Test Connection uses the current form for a real model-chain test, then saves it as the global default only when the test succeeds.
  • Leaving a key field empty keeps the old key; it does not delete it.

Backend

Recommended backend .env:

AI_PROVIDER_SERVICE_URL=http://localhost:8010
AI_PROVIDER_SERVICE_TOKEN=change_me
AI_PROVIDER_TIMEOUT_SECONDS=60
AI_PROVIDER_RETRY_ATTEMPTS=2

Reference file:

AI Provider

Reference file:

Frontend local reference:

Common settings:

SERVICE_NAME=planet-ai-provider
SERVICE_VERSION=0.1.0
AI_PROVIDER_SERVICE_TOKEN=change_me
AI_TIMEOUT_SECONDS=60
AI_HTTP_RETRY_ATTEMPTS=2

Optional provider-specific keys:

MINIMAX_API_KEY=sk-cp-xxxxx
OPENAI_API_KEY=sk-xxxxx
ANTHROPIC_API_KEY=sk-ant-xxxxx
DEEPSEEK_API_KEY=sk-xxxxx
DASHSCOPE_API_KEY=sk-xxxxx
MOONSHOT_API_KEY=sk-xxxxx
OPENROUTER_API_KEY=sk-or-xxxxx

OpenAI-compatible example

AI_PROVIDER=openai
AI_PROVIDER_API=openai-completions
AI_BASE_URL=http://127.0.0.1:8001/v1
AI_API_KEY=local-key
AI_MODEL=your-local-model

MiniMax CN example

AI_PROVIDER=minimax
AI_PROVIDER_API=anthropic-messages
AI_BASE_URL=https://api.minimaxi.com/anthropic
AI_API_KEY=sk-cp-xxxxx
AI_MODEL=MiniMax-M2.7
AI_MAX_TOKENS=1200
AI_ANTHROPIC_VERSION=2023-06-01

MiniMax note:

  • This follows the same Anthropic Messages request shape as the official MiniMax examples.
  • For MiniMax, aiprovider now disables thinking by default unless the caller explicitly passes a thinking object.
  • This mirrors OpenClaw's caution around MiniMax Anthropic-compatible behavior.

Anthropic-compatible example

AI_PROVIDER=anthropic
AI_PROVIDER_API=anthropic-messages
AI_BASE_URL=https://your-claude-compatible-endpoint.example.com/anthropic
AI_API_KEY=your_api_key
AI_MODEL=your-model
AI_MAX_TOKENS=1200
AI_ANTHROPIC_VERSION=2023-06-01

Ollama example

AI_PROVIDER=ollama
AI_PROVIDER_API=ollama-generate
AI_BASE_URL=http://127.0.0.1:11434
AI_API_KEY=
AI_MODEL=qwen2.5:7b

Deployment Modes

Single machine

Recommended local flow:

  • backend on localhost:8000
  • aiprovider on localhost:8010
  • local model gateway on localhost:11434 or another local port

Helpers already included:

Multi-machine

Example topology:

  • app machine: backend
  • AI gateway machine: aiprovider
  • model machine: local model service or cloud proxy

In that case, this becomes service-to-service HTTP RPC:

  • caller -> backend
  • backend -> http://10.0.0.12:8010
  • aiprovider -> model endpoint

Recommended cross-machine backend config:

AI_PROVIDER_SERVICE_URL=http://10.0.0.12:8010
AI_PROVIDER_SERVICE_TOKEN=change_me
AI_PROVIDER_TIMEOUT_SECONDS=60
AI_PROVIDER_RETRY_ATTEMPTS=2

Recommended operating rules:

  • keep aiprovider on a private network
  • protect it with X-Provider-Token at minimum
  • always send X-Request-ID
  • keep callers on the backend API unless they are infrastructure jobs

Retry And Failure Behavior

backend -> aiprovider:

  • retries lightweight network / 5xx failures
  • returns 502 when the provider service is unavailable

aiprovider -> model provider:

  • retries lightweight network / 5xx failures
  • returns 502 when the model provider is unavailable

This is intentionally conservative. It avoids masking persistent errors while still absorbing short hiccups.

Operational Notes

  • ./planet.sh start now starts aiprovider automatically
  • ./planet.sh restart -a restarts only aiprovider
  • ./planet.sh log -a tails aiprovider logs
  • ./planet.sh health reports aiprovider health
  • Frontend and application services: call backend
  • Scheduled infra jobs and diagnostics: optionally call aiprovider
  • Do not let multiple business services integrate model vendors independently

That keeps provider switching centralized and avoids model-specific drift across the system.