From f81881e1387002cb0a5bec1a7109366d9ca2dee4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BE=84=E6=BD=AD?= Date: Sun, 1 Feb 2026 22:39:32 +0800 Subject: [PATCH] improve(skill): enhance higress-clawdbot-integration skill (#3438) --- .../higress-clawdbot-integration/SKILL.md | 409 ++++-------------- .../scripts/plugin-clawdbot/README.md | 79 ++++ .../plugin-clawdbot/clawdbot.plugin.json | 10 + .../scripts/plugin-clawdbot/index.ts | 284 ++++++++++++ .../scripts/plugin-clawdbot/package.json | 22 + 5 files changed, 477 insertions(+), 327 deletions(-) create mode 100644 .claude/skills/higress-clawdbot-integration/scripts/plugin-clawdbot/README.md create mode 100644 .claude/skills/higress-clawdbot-integration/scripts/plugin-clawdbot/clawdbot.plugin.json create mode 100644 .claude/skills/higress-clawdbot-integration/scripts/plugin-clawdbot/index.ts create mode 100644 .claude/skills/higress-clawdbot-integration/scripts/plugin-clawdbot/package.json diff --git a/.claude/skills/higress-clawdbot-integration/SKILL.md b/.claude/skills/higress-clawdbot-integration/SKILL.md index 075eee87b..2854f2746 100644 --- a/.claude/skills/higress-clawdbot-integration/SKILL.md +++ b/.claude/skills/higress-clawdbot-integration/SKILL.md @@ -85,19 +85,49 @@ After script completion: http://localhost:8001 ``` -### Step 5: Configure Clawdbot/OpenClaw (if applicable) +### Step 5: Configure Clawdbot/OpenClaw Plugin -If the user wants to use Higress with Clawdbot/OpenClaw: +If the user wants to use Higress with Clawdbot/OpenClaw, install the appropriate plugin: + +#### Automatic Installation + +Detect runtime and install the correct plugin version: ```bash -# For Clawdbot -clawdbot models auth login --provider higress +# Detect which runtime is installed +if command -v clawdbot &> /dev/null; then + RUNTIME="clawdbot" + RUNTIME_DIR="$HOME/.clawdbot" + PLUGIN_SRC="scripts/plugin-clawdbot" +elif command -v openclaw &> /dev/null; then + RUNTIME="openclaw" + RUNTIME_DIR="$HOME/.openclaw" + PLUGIN_SRC="scripts/plugin" +else + echo "Error: Neither clawdbot nor openclaw is installed" + exit 1 +fi -# For OpenClaw -openclaw models auth login --provider higress +# Install the plugin +PLUGIN_DEST="$RUNTIME_DIR/extensions/higress-ai-gateway" +echo "Installing Higress AI Gateway plugin for $RUNTIME..." +mkdir -p "$(dirname "$PLUGIN_DEST")" +[ -d "$PLUGIN_DEST" ] && rm -rf "$PLUGIN_DEST" +cp -r "$PLUGIN_SRC" "$PLUGIN_DEST" +echo "✓ Plugin installed at: $PLUGIN_DEST" + +# Configure provider +echo +echo "Configuring provider..." +$RUNTIME models auth login --provider higress ``` -This configures Clawdbot/OpenClaw to use Higress AI Gateway as a model provider. +The plugin will guide you through an interactive setup for: +1. Gateway URL (default: `http://localhost:8080`) +2. Console URL (default: `http://localhost:8001`) +3. API Key (optional for local deployments) +4. Model list (auto-detected or manually specified) +5. Auto-routing default model (if using `higress/auto`) ### Step 6: Manage API Keys (optional) @@ -119,6 +149,7 @@ After deployment, manage API keys without redeploying: ## CLI Parameters Reference ### Basic Options + | Parameter | Description | Default | |-----------|-------------|---------| | `--non-interactive` | Run without prompts | - | @@ -127,14 +158,13 @@ After deployment, manage API keys without redeploying: | `--console-port` | Console port | 8001 | | `--container-name` | Container name | higress-ai-gateway | | `--data-folder` | Data folder path | ./higress | - -### Auto-Routing Options -| Parameter | Description | -|-----------|-------------| -| `--auto-routing` | Enable auto-routing feature | -| `--auto-routing-default-model` | Default model when no rule matches | +| `--auto-routing` | Enable auto-routing feature | - | +| `--auto-routing-default-model` | Default model when no rule matches | - | ### LLM Provider API Keys + +**Top Providers:** + | Parameter | Provider | |-----------|----------| | `--dashscope-key` | Aliyun Dashscope (Qwen) | @@ -146,60 +176,29 @@ After deployment, manage API keys without redeploying: | `--claude-key` | Claude | | `--gemini-key` | Google Gemini | | `--groq-key` | Groq | -| `--doubao-key` | Doubao | -| `--baichuan-key` | Baichuan AI | -| `--yi-key` | 01.AI (Yi) | -| `--stepfun-key` | Stepfun | -| `--minimax-key` | Minimax | -| `--cohere-key` | Cohere | -| `--mistral-key` | Mistral AI | -| `--github-key` | Github Models | -| `--fireworks-key` | Fireworks AI | -| `--togetherai-key` | Together AI | -| `--grok-key` | Grok | -## Managing API Keys +**Additional Providers:** +`--doubao-key`, `--baichuan-key`, `--yi-key`, `--stepfun-key`, `--minimax-key`, `--cohere-key`, `--mistral-key`, `--github-key`, `--fireworks-key`, `--togetherai-key`, `--grok-key`, `--azure-key`, `--bedrock-key`, `--vertex-key` -After deployment, use the `config` subcommand to manage LLM provider API keys: +## Managing Configuration + +### API Keys ```bash # List all configured API keys ./get-ai-gateway.sh config list -# Add or update an API key +# Add or update an API key (hot-reload) ./get-ai-gateway.sh config add --provider deepseek --key sk-xxx -# Remove an API key +# Remove an API key (hot-reload) ./get-ai-gateway.sh config remove --provider deepseek ``` -**Important:** API key changes take effect immediately via hot-reload. No container restart is required. +**Supported provider aliases:** +`dashscope`/`qwen`, `moonshot`/`kimi`, `zhipuai`/`zhipu`, `togetherai`/`together` -**Supported providers:** -- `dashscope` (or `qwen`) - Aliyun Dashscope (Qwen) -- `deepseek` - DeepSeek -- `moonshot` (or `kimi`) - Moonshot (Kimi) -- `zhipuai` (or `zhipu`) - Zhipu AI -- `openai` - OpenAI -- `openrouter` - OpenRouter -- `claude` - Claude -- `gemini` - Google Gemini -- `groq` - Groq -- `doubao` - Doubao -- `baichuan` - Baichuan AI -- `yi` - 01.AI (Yi) -- `stepfun` - Stepfun -- `minimax` - Minimax -- `cohere` - Cohere -- `mistral` - Mistral AI -- `github` - Github Models -- `fireworks` - Fireworks AI -- `togetherai` (or `together`) - Together AI -- `grok` - Grok - -## Managing Routing Rules - -After deployment, use the `route` subcommand to manage auto-routing rules: +### Routing Rules ```bash # Add a routing rule @@ -216,7 +215,7 @@ See [higress-auto-router](../higress-auto-router/SKILL.md) for detailed document ## Access Logs -After deployment, gateway access logs are available at: +Gateway access logs are available at: ``` $DATA_FOLDER/logs/access.log ``` @@ -225,21 +224,11 @@ These logs can be used with the **agent-session-monitor** skill for token tracki ## Related Skills -### higress-auto-router -Configure automatic model routing using CLI commands. Example: -```bash -./get-ai-gateway.sh route add --model claude-opus-4.5 --trigger "深入思考|deep thinking" -``` +- **higress-auto-router**: Configure automatic model routing using CLI commands + See: [higress-auto-router](../higress-auto-router/SKILL.md) -See: [higress-auto-router](../higress-auto-router/SKILL.md) - -### agent-session-monitor -Monitor and track token usage across sessions. Example: -- View session statistics in web UI -- Export FinOps reports -- Parse logs from `$DATA_FOLDER/logs/access.log` - -See: [agent-session-monitor](../agent-session-monitor/SKILL.md) +- **agent-session-monitor**: Monitor and track token usage across sessions + See: [agent-session-monitor](../agent-session-monitor/SKILL.md) ## Examples @@ -273,60 +262,38 @@ curl 'http://localhost:8080/v1/chat/completions' \ -d '{"model": "qwen-turbo", "messages": [{"role": "user", "content": "Hello!"}]}' ``` -### Example 2: Deployment with Auto-Routing +### Example 2: Full Integration with Clawdbot -**User:** 部署Higress网关,启用自动路由,默认使用qwen-turbo +**User:** 完整配置Higress和Clawdbot的集成 **Steps:** -1. Download script -2. Get API keys -3. Run: - ```bash - ./get-ai-gateway.sh start --non-interactive \ - --dashscope-key sk-xxx \ - --auto-routing \ - --auto-routing-default-model qwen-turbo - ``` +1. Deploy Higress AI Gateway +2. Install and configure Clawdbot plugin +3. Enable auto-routing +4. Set up session monitoring **Response:** ``` -✅ Higress AI Gateway 部署完成! +✅ Higress AI Gateway 集成完成! -网关地址: http://localhost:8080/v1/chat/completions -控制台: http://localhost:8001 +1. 网关已部署: + - HTTP: http://localhost:8080 + - Console: http://localhost:8001 -🔀 自动路由已启用: -- 默认模型: qwen-turbo -- 使用 model="higress/auto" 触发自动路由 +2. Clawdbot 插件配置: + Plugin installed at: /root/.clawdbot/extensions/higress-ai-gateway + Run: clawdbot models auth login --provider higress -添加路由规则: -./get-ai-gateway.sh route add --model claude-opus-4.5 --trigger "深入思考|deep thinking" +3. 自动路由: + 已启用,使用 model="higress/auto" + +4. 会话监控: + 日志路径: ./higress/logs/access.log + +需要我帮你配置自动路由规则吗? ``` -### Example 3: Add Routing Rules - -**User:** 我希望在解决困难问题时路由到claude-opus-4.5 - -**Steps:** -```bash -./get-ai-gateway.sh route add \ - --model claude-opus-4.5 \ - --trigger "深入思考|deep thinking" -``` - -**Response:** -``` -✅ 路由规则添加成功! - -触发方式:以 "深入思考" 或 "deep thinking" 开头 -目标模型:claude-opus-4.5 - -使用示例: -- 深入思考 这道算法题应该怎么解? -- deep thinking What's the best architecture for this system? -``` - -### Example 4: Manage API Keys +### Example 3: Manage API Keys **User:** 帮我查看当前配置的API keys,并添加一个DeepSeek的key @@ -349,7 +316,6 @@ curl 'http://localhost:8080/v1/chat/completions' \ OpenAI: sk-cd***gh34 Adding API key for DeepSeek... -Updating AI Gateway configuration... ✅ API key updated successfully! @@ -359,38 +325,6 @@ Key: sk-xx***yy56 Configuration has been hot-reloaded (no restart needed). ``` -### Example 5: Full Integration with Clawdbot/OpenClaw - -**User:** 完整配置Higress和Clawdbot的集成 - -**Steps:** -1. Deploy Higress AI Gateway -2. Configure Clawdbot/OpenClaw provider -3. Enable auto-routing -4. Set up session monitoring - -**Response:** -``` -✅ Higress AI Gateway 集成完成! - -1. 网关已部署: - - HTTP: http://localhost:8080 - - Console: http://localhost:8001 - -2. Clawdbot/OpenClaw 配置: - - Clawdbot: `clawdbot models auth login --provider higress` - - OpenClaw: `openclaw models auth login --provider higress` - -3. 自动路由: - 已启用,使用 model="higress/auto" - -4. 会话监控: - 日志路径: ./higress/logs/access.log - 启动 Web UI: python3 agent-session-monitor/scripts/webserver.py --data-dir ./sessions - -需要我帮你配置自动路由规则吗? -``` - ## Troubleshooting ### Container fails to start @@ -403,191 +337,12 @@ Configuration has been hot-reloaded (no restart needed). - Verify port mapping: `docker port higress-ai-gateway` - Test locally: `curl http://localhost:8080/v1/models` -### API key errors -- Verify the API key is correct -- Check provider documentation for key format -- Some providers require additional configuration (e.g., Azure, Bedrock) - -## Clawdbot/OpenClaw Plugin Integration - -The Higress AI Gateway plugin enables Clawdbot and OpenClaw to use Higress as a model provider with full support for auto-routing and model management. - -### Installation - -The plugin is automatically available as part of this skill. To install it into your Clawdbot/OpenClaw environment: - -```bash -# Detect runtime and set variables -if command -v clawdbot &> /dev/null; then - RUNTIME="clawdbot" - RUNTIME_DIR="$HOME/.clawdbot" -elif command -v openclaw &> /dev/null; then - RUNTIME="openclaw" - RUNTIME_DIR="$HOME/.openclaw" -else - echo "Error: Neither clawdbot nor openclaw is installed" - exit 1 -fi - -# Install the plugin -PLUGIN_SRC_DIR="scripts/plugin" -PLUGIN_DEST_DIR="$RUNTIME_DIR/extensions/higress-ai-gateway" - -if [ ! -d "$PLUGIN_SRC_DIR" ]; then - echo "Error: Plugin source not found at $PLUGIN_SRC_DIR" - exit 1 -fi - -echo "Installing Higress AI Gateway plugin for $RUNTIME..." -mkdir -p "$(dirname "$PLUGIN_DEST_DIR")" -if [ -d "$PLUGIN_DEST_DIR" ]; then - echo "Plugin already exists, updating..." - rm -rf "$PLUGIN_DEST_DIR" -fi - -cp -r "$PLUGIN_SRC_DIR" "$PLUGIN_DEST_DIR" -echo "✓ Higress AI Gateway plugin installed at: $PLUGIN_DEST_DIR" - -echo -echo "To configure the provider, run:" -echo " $RUNTIME models auth login --provider higress" -``` - -### Configuration - -After installation, configure Higress as a model provider: - -```bash -# For Clawdbot -clawdbot models auth login --provider higress - -# For OpenClaw -openclaw models auth login --provider higress -``` - -The plugin will guide you through an interactive setup: - -1. **Gateway URL**: HTTP endpoint for Higress AI Gateway (default: `http://localhost:8080`) -2. **Console URL**: Higress Console endpoint for routing config (default: `http://localhost:8001`) -3. **API Key**: Optional API key for authentication (leave empty for local deployments) -4. **Model List**: Comma-separated model IDs (plugin auto-detects available models) -5. **Auto-routing**: If you include `higress/auto` in the model list, configure the default fallback model - -### Plugin Features - -#### 1. Auto-routing Support - -The plugin provides first-class support for Higress auto-routing: - -- Use `higress/auto` as the model ID to enable intelligent routing -- Configure default fallback model during setup -- Auto-routing rules are managed separately via the `higress-auto-router` skill - -#### 2. Dynamic Model Discovery - -During configuration, the plugin: -- Tests connectivity to the gateway -- Fetches available models from the Console API -- Pre-populates the model list with discovered models -- Allows customization of the model list - -#### 3. Smart URL Normalization - -The plugin automatically: -- Strips trailing slashes from URLs -- Appends `/v1` suffix if missing -- Validates URL format before saving - -#### 4. Profile Management - -Creates appropriate credential profiles: -- `higress:local` - for local deployments without API key -- `higress:default` - for remote deployments with API key - -### Plugin Structure - -``` -scripts/plugin/ -├── index.ts # Plugin implementation (TypeScript) -├── package.json # NPM package metadata -└── openclaw.plugin.json # OpenClaw plugin manifest -``` - -**index.ts**: Main plugin code implementing the provider registration and authentication flow. - -**package.json**: Declares the plugin as an OpenClaw extension with proper metadata. - -**openclaw.plugin.json**: Plugin manifest describing supported providers and configuration schema. - -### Integration with Skills - -The plugin works seamlessly with related skills: - -#### higress-auto-router -After plugin setup, use this skill to configure routing rules: -```bash -./get-ai-gateway.sh route add --model claude-opus-4.5 --trigger "深入思考" -``` - -See: [higress-auto-router](../higress-auto-router/SKILL.md) - -#### agent-session-monitor -Track token usage and costs across sessions using gateway access logs: -```bash -python3 agent-session-monitor/scripts/webserver.py --log-path ./higress/logs/access.log -``` - -See: [agent-session-monitor](../agent-session-monitor/SKILL.md) - -### Example: Full Setup Flow - -```bash -# 1. Deploy Higress AI Gateway (via get-ai-gateway.sh) -./get-ai-gateway.sh start --non-interactive \ - --dashscope-key sk-xxx \ - --auto-routing - -# 2. Detect and install plugin -if command -v clawdbot &> /dev/null; then - RUNTIME="clawdbot" - RUNTIME_DIR="$HOME/.clawdbot" -else - RUNTIME="openclaw" - RUNTIME_DIR="$HOME/.openclaw" -fi - -mkdir -p "$RUNTIME_DIR/extensions" -cp -r scripts/plugin "$RUNTIME_DIR/extensions/higress-ai-gateway" - -# 3. Configure provider -$RUNTIME models auth login --provider higress -# Follow interactive prompts to configure gateway URL, models, etc. - -# 4. Test the integration -$RUNTIME chat --model higress/auto "Hello, test auto-routing!" - -# 5. Configure routing rules (optional) -./get-ai-gateway.sh route add --model claude-opus-4.5 --trigger "深入思考" -``` - -### Troubleshooting - -#### Plugin not recognized +### Plugin not recognized - Verify plugin is installed at `~/.clawdbot/extensions/higress-ai-gateway` or `~/.openclaw/extensions/higress-ai-gateway` -- Check `package.json` contains correct `openclaw.extensions` field +- Check `package.json` contains correct extension field (`clawdbot.extensions` or `openclaw.extensions`) - Restart Clawdbot/OpenClaw after installation -#### Gateway connection fails -- Ensure Higress AI Gateway container is running: `docker ps` -- Verify gateway URL is accessible: `curl http://localhost:8080/v1/models` -- Check firewall/network settings if using remote gateway - -#### Models not available -- Run `clawdbot models list` or `openclaw models list` to verify provider is configured -- Check gateway logs: `docker logs higress-ai-gateway` -- Verify API keys are correctly configured in gateway - -#### Auto-routing not working +### Auto-routing not working - Confirm `higress/auto` is in your model list - Check routing rules exist: `./get-ai-gateway.sh route list` - Verify default model is configured diff --git a/.claude/skills/higress-clawdbot-integration/scripts/plugin-clawdbot/README.md b/.claude/skills/higress-clawdbot-integration/scripts/plugin-clawdbot/README.md new file mode 100644 index 000000000..1e0c873c1 --- /dev/null +++ b/.claude/skills/higress-clawdbot-integration/scripts/plugin-clawdbot/README.md @@ -0,0 +1,79 @@ +# Higress AI Gateway Plugin (Clawdbot) + +Clawdbot model provider plugin for Higress AI Gateway with auto-routing support. + +## What is this? + +This is a TypeScript-based provider plugin that enables Clawdbot to use Higress AI Gateway as a model provider. It provides: + +- **Auto-routing support**: Use `higress/auto` to intelligently route requests based on message content +- **Dynamic model discovery**: Auto-detect available models from Higress Console +- **Smart URL handling**: Automatic URL normalization and validation +- **Flexible authentication**: Support for both local and remote gateway deployments + +## Files + +- **index.ts**: Main plugin implementation +- **package.json**: NPM package metadata and Clawdbot extension declaration +- **clawdbot.plugin.json**: Plugin manifest for Clawdbot + +## Installation + +This plugin is automatically installed when you use the `higress-clawdbot-integration` skill. See the parent SKILL.md for complete installation instructions. + +### Manual Installation + +If you need to install manually: + +```bash +# Copy plugin files +mkdir -p "$HOME/.clawdbot/extensions/higress-ai-gateway" +cp -r ./* "$HOME/.clawdbot/extensions/higress-ai-gateway/" + +# Configure provider +clawdbot models auth login --provider higress +``` + +## Usage + +After installation, configure Higress as a model provider: + +```bash +clawdbot models auth login --provider higress +``` + +The plugin will prompt for: +1. Gateway URL (default: http://localhost:8080) +2. Console URL (default: http://localhost:8001) +3. API Key (optional for local deployments) +4. Model list (auto-detected or manually specified) +5. Auto-routing default model (if using higress/auto) + +## Auto-routing + +To use auto-routing, include `higress/auto` in your model list during configuration. Then use it in your conversations: + +```bash +# Use auto-routing +clawdbot chat --model higress/auto "深入思考 这个问题应该怎么解决?" + +# The gateway will automatically route to the appropriate model based on: +# - Message content triggers (configured via higress-auto-router skill) +# - Fallback to default model if no rule matches +``` + +## Related Resources + +- **Parent Skill**: [higress-clawdbot-integration](../SKILL.md) +- **Auto-routing Configuration**: [higress-auto-router](../../higress-auto-router/SKILL.md) +- **Session Monitoring**: [agent-session-monitor](../../agent-session-monitor/SKILL.md) +- **Higress AI Gateway**: https://github.com/higress-group/higress-standalone + +## Compatibility + +- **Clawdbot**: v2.0.0+ +- **Higress AI Gateway**: All versions + +## License + +Apache-2.0 diff --git a/.claude/skills/higress-clawdbot-integration/scripts/plugin-clawdbot/clawdbot.plugin.json b/.claude/skills/higress-clawdbot-integration/scripts/plugin-clawdbot/clawdbot.plugin.json new file mode 100644 index 000000000..2573d41bf --- /dev/null +++ b/.claude/skills/higress-clawdbot-integration/scripts/plugin-clawdbot/clawdbot.plugin.json @@ -0,0 +1,10 @@ +{ + "id": "higress-ai-gateway", + "name": "Higress AI Gateway", + "description": "Model provider plugin for Higress AI Gateway with auto-routing support", + "providers": ["higress"], + "configSchema": { + "type": "object", + "additionalProperties": true + } +} diff --git a/.claude/skills/higress-clawdbot-integration/scripts/plugin-clawdbot/index.ts b/.claude/skills/higress-clawdbot-integration/scripts/plugin-clawdbot/index.ts new file mode 100644 index 000000000..f8b5a802b --- /dev/null +++ b/.claude/skills/higress-clawdbot-integration/scripts/plugin-clawdbot/index.ts @@ -0,0 +1,284 @@ +import { emptyPluginConfigSchema } from "clawdbot/plugin-sdk"; + +const DEFAULT_GATEWAY_URL = "http://localhost:8080"; +const DEFAULT_CONSOLE_URL = "http://localhost:8001"; +const DEFAULT_CONTEXT_WINDOW = 128_000; +const DEFAULT_MAX_TOKENS = 8192; + +// Common models that Higress AI Gateway typically supports +const DEFAULT_MODEL_IDS = [ + // Auto-routing special model + "higress/auto", + // OpenAI models + "gpt-5.2", + "gpt-5-mini", + "gpt-5-nano", + // Anthropic models + "claude-opus-4.5", + "claude-sonnet-4.5", + "claude-haiku-4.5", + // Qwen models + "qwen3-turbo", + "qwen3-plus", + "qwen3-max", + "qwen3-coder-480b-a35b-instruct", + // DeepSeek models + "deepseek-chat", + "deepseek-reasoner", + // Other common models + "kimi-k2.5", + "glm-4.7", + "MiniMax-M2.1", +] as const; + +function normalizeBaseUrl(value: string): string { + const trimmed = value.trim(); + if (!trimmed) return DEFAULT_GATEWAY_URL; + let normalized = trimmed; + while (normalized.endsWith("/")) normalized = normalized.slice(0, -1); + if (!normalized.endsWith("/v1")) normalized = `${normalized}/v1`; + return normalized; +} + +function validateUrl(value: string): string | undefined { + const normalized = normalizeBaseUrl(value); + try { + new URL(normalized); + } catch { + return "Enter a valid URL"; + } + return undefined; +} + +function parseModelIds(input: string): string[] { + const parsed = input + .split(/[\n,]/) + .map((model) => model.trim()) + .filter(Boolean); + return Array.from(new Set(parsed)); +} + +function buildModelDefinition(modelId: string) { + const isAutoModel = modelId === "higress/auto"; + return { + id: modelId, + name: isAutoModel ? "Higress Auto Router" : modelId, + api: "openai-completions", + reasoning: false, + input: ["text", "image"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: DEFAULT_CONTEXT_WINDOW, + maxTokens: DEFAULT_MAX_TOKENS, + }; +} + +async function testGatewayConnection(gatewayUrl: string): Promise { + try { + const response = await fetch(`${gatewayUrl}/v1/models`, { + method: "GET", + headers: { "Content-Type": "application/json" }, + signal: AbortSignal.timeout(5000), + }); + return response.ok || response.status === 401; // 401 means gateway is up but needs auth + } catch { + return false; + } +} + +async function fetchAvailableModels(consoleUrl: string): Promise { + try { + // Try to get models from Higress Console API + const response = await fetch(`${consoleUrl}/v1/ai/routes`, { + method: "GET", + headers: { "Content-Type": "application/json" }, + signal: AbortSignal.timeout(5000), + }); + if (response.ok) { + const data = (await response.json()) as { data?: { model?: string }[] }; + if (data.data && Array.isArray(data.data)) { + return data.data + .map((route: { model?: string }) => route.model) + .filter((m): m is string => typeof m === "string"); + } + } + } catch { + // Ignore errors, use defaults + } + return []; +} + +const higressPlugin = { + id: "higress-ai-gateway", + name: "Higress AI Gateway", + description: "Model provider plugin for Higress AI Gateway with auto-routing support", + configSchema: emptyPluginConfigSchema(), + register(api) { + api.registerProvider({ + id: "higress", + label: "Higress AI Gateway", + docsPath: "/providers/models", + aliases: ["higress-gateway", "higress-ai"], + auth: [ + { + id: "api-key", + label: "API Key", + hint: "Configure Higress AI Gateway endpoint with optional API key", + kind: "custom", + run: async (ctx) => { + // Step 1: Get Gateway URL + const gatewayUrlInput = await ctx.prompter.text({ + message: "Higress AI Gateway URL", + initialValue: DEFAULT_GATEWAY_URL, + validate: validateUrl, + }); + const gatewayUrl = normalizeBaseUrl(gatewayUrlInput); + + // Step 2: Get Console URL (for auto-router configuration) + const consoleUrlInput = await ctx.prompter.text({ + message: "Higress Console URL (for auto-router config)", + initialValue: DEFAULT_CONSOLE_URL, + validate: validateUrl, + }); + const consoleUrl = normalizeBaseUrl(consoleUrlInput); + + // Step 3: Test connection (create a new spinner) + const spin = ctx.prompter.progress("Testing gateway connection…"); + const isConnected = await testGatewayConnection(gatewayUrl); + if (!isConnected) { + spin.stop("Gateway connection failed"); + await ctx.prompter.note( + [ + "Could not connect to Higress AI Gateway.", + "Make sure the gateway is running and the URL is correct.", + "", + `Tried: ${gatewayUrl}/v1/models`, + ].join("\n"), + "Connection Warning", + ); + } else { + spin.stop("Gateway connected"); + } + + // Step 4: Get API Key (optional for local gateway) + const apiKeyInput = await ctx.prompter.text({ + message: "API Key (leave empty if not required)", + initialValue: "", + }) || ''; + const apiKey = apiKeyInput.trim() || "higress-local"; + + // Step 5: Fetch available models (create a new spinner) + const spin2 = ctx.prompter.progress("Fetching available models…"); + const fetchedModels = await fetchAvailableModels(consoleUrl); + const defaultModels = fetchedModels.length > 0 + ? ["higress/auto", ...fetchedModels] + : DEFAULT_MODEL_IDS; + spin2.stop(); + + // Step 6: Let user customize model list + const modelInput = await ctx.prompter.text({ + message: "Model IDs (comma-separated, higress/auto enables auto-routing)", + initialValue: defaultModels.slice(0, 10).join(", "), + validate: (value) => + parseModelIds(value).length > 0 ? undefined : "Enter at least one model id", + }); + + const modelIds = parseModelIds(modelInput); + const hasAutoModel = modelIds.includes("higress/auto"); + + // FIX: Avoid double prefix - if modelId already starts with provider, don't add prefix again + const defaultModelId = hasAutoModel + ? "higress/auto" + : (modelIds[0] ?? "qwen-turbo"); + const defaultModelRef = defaultModelId.startsWith("higress/") + ? defaultModelId + : `higress/${defaultModelId}`; + + // Step 7: Configure default model for auto-routing + let autoRoutingDefaultModel = "qwen-turbo"; + if (hasAutoModel) { + const autoRoutingModelInput = await ctx.prompter.text({ + message: "Default model for auto-routing (when no rule matches)", + initialValue: "qwen-turbo", + }); + autoRoutingDefaultModel = autoRoutingModelInput.trim(); // FIX: Add trim() here + } + + return { + profiles: [ + { + profileId: `higress:${apiKey === "higress-local" ? "local" : "default"}`, + credential: { + type: "token", + provider: "higress", + token: apiKey, + }, + }, + ], + configPatch: { + models: { + providers: { + higress: { + baseUrl: `${gatewayUrl}/v1`, + apiKey: apiKey, + api: "openai-completions", + authHeader: apiKey !== "higress-local", + models: modelIds.map((modelId) => buildModelDefinition(modelId)), + }, + }, + }, + agents: { + defaults: { + models: Object.fromEntries( + modelIds.map((modelId) => { + // FIX: Avoid double prefix - only add provider prefix if not already present + const modelRef = modelId.startsWith("higress/") + ? modelId + : `higress/${modelId}`; + return [modelRef, {}]; + }), + ), + }, + }, + plugins: { + entries: { + "higress-ai-gateway": { + enabled: true, + config: { + gatewayUrl, + consoleUrl, + autoRoutingDefaultModel, + }, + }, + }, + }, + }, + defaultModel: defaultModelRef, + notes: [ + "Higress AI Gateway is now configured as a model provider.", + hasAutoModel + ? `Auto-routing enabled: use model "higress/auto" to route based on message content.` + : "Add 'higress/auto' to models to enable auto-routing.", + `Gateway endpoint: ${gatewayUrl}/v1/chat/completions`, + `Console: ${consoleUrl}`, + "", + "🎯 Recommended Skills (install via Clawdbot conversation):", + "", + "1. Auto-Routing Skill:", + " Configure automatic model routing based on message content", + " https://github.com/alibaba/higress/tree/main/.claude/skills/higress-auto-router", + ' Say: "Install higress-auto-router skill"', + "", + "2. Agent Session Monitor Skill:", + " Track token usage and monitor conversation history", + " https://github.com/alibaba/higress/tree/main/.claude/skills/agent-session-monitor", + ' Say: "Install agent-session-monitor skill"', + ], + }; + }, + }, + ], + }); + }, +}; + +export default higressPlugin; diff --git a/.claude/skills/higress-clawdbot-integration/scripts/plugin-clawdbot/package.json b/.claude/skills/higress-clawdbot-integration/scripts/plugin-clawdbot/package.json new file mode 100644 index 000000000..0cc966408 --- /dev/null +++ b/.claude/skills/higress-clawdbot-integration/scripts/plugin-clawdbot/package.json @@ -0,0 +1,22 @@ +{ + "name": "@higress/higress-ai-gateway", + "version": "1.0.0", + "description": "Higress AI Gateway model provider plugin for Clawdbot with auto-routing support", + "main": "index.ts", + "clawdbot": { + "extensions": ["./index.ts"] + }, + "keywords": [ + "clawdbot", + "higress", + "ai-gateway", + "model-router", + "auto-routing" + ], + "author": "Higress Team", + "license": "Apache-2.0", + "repository": { + "type": "git", + "url": "https://github.com/alibaba/higress" + } +}