From 2f81f2d174c21670c7fdff7f25adb29b5eff0333 Mon Sep 17 00:00:00 2001 From: H-Chris233 Date: Tue, 17 Mar 2026 12:11:33 +0800 Subject: [PATCH] =?UTF-8?q?feat(core):=20=E4=B8=BA=20LLM=20API=20=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E8=A1=A5=E9=BD=90=20User-Agent=EF=BC=88=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E8=A6=86=E7=9B=96?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 4 ++++ packages/cli/src/commands/init.ts | 2 ++ packages/core/src/llm/provider.ts | 19 +++++++++++++++++-- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index 3e9d3347..1c3be079 100644 --- a/.env.example +++ b/.env.example @@ -13,6 +13,10 @@ INKOS_LLM_API_KEY=sk-your-key-here # Model name INKOS_LLM_MODEL=gpt-4o +# Optional: set a custom User-Agent header for LLM HTTP requests +# Some gateways/WAFs may block requests without a browser-like UA. +INKOS_HTTP_USER_AGENT=Mozilla/5.0 (compatible; InkOS) + # Notifications (optional) INKOS_TELEGRAM_BOT_TOKEN= INKOS_TELEGRAM_CHAT_ID= diff --git a/packages/cli/src/commands/init.ts b/packages/cli/src/commands/init.ts index 0a0bab5e..70feb600 100644 --- a/packages/cli/src/commands/init.ts +++ b/packages/cli/src/commands/init.ts @@ -72,6 +72,7 @@ export const initCommand = new Command("init") "# INKOS_LLM_BASE_URL=https://api.openai.com/v1", "# INKOS_LLM_API_KEY=your-api-key-here", "# INKOS_LLM_MODEL=gpt-4o", + "# INKOS_HTTP_USER_AGENT=Mozilla/5.0 (compatible; InkOS)", ].join("\n"), "utf-8", ); @@ -92,6 +93,7 @@ export const initCommand = new Command("init") "# INKOS_LLM_MAX_TOKENS=8192", "# INKOS_LLM_THINKING_BUDGET=0 # Anthropic extended thinking budget", "# INKOS_LLM_API_FORMAT=chat # chat (default) or responses (OpenAI Responses API)", + "# INKOS_HTTP_USER_AGENT=Mozilla/5.0 (compatible; InkOS)", "", "# Anthropic example:", "# INKOS_LLM_PROVIDER=anthropic", diff --git a/packages/core/src/llm/provider.ts b/packages/core/src/llm/provider.ts index e5172ea2..8fa12fa4 100644 --- a/packages/core/src/llm/provider.ts +++ b/packages/core/src/llm/provider.ts @@ -2,6 +2,12 @@ import OpenAI from "openai"; import Anthropic from "@anthropic-ai/sdk"; import type { LLMConfig } from "../models/project.js"; +function resolveUserAgent(): string { + const fromEnv = process.env.INKOS_HTTP_USER_AGENT?.trim(); + if (fromEnv) return fromEnv; + return "Mozilla/5.0 (compatible; InkOS)"; +} + // === Shared Types === export interface LLMResponse { @@ -65,6 +71,7 @@ export function createLLMClient(config: LLMConfig): LLMClient { thinkingBudget: config.thinkingBudget ?? 0, }; + const userAgent = resolveUserAgent(); const apiFormat = config.apiFormat ?? "chat"; const stream = config.stream ?? true; @@ -75,7 +82,11 @@ export function createLLMClient(config: LLMConfig): LLMClient { provider: "anthropic", apiFormat, stream, - _anthropic: new Anthropic({ apiKey: config.apiKey, baseURL }), + _anthropic: new Anthropic({ + apiKey: config.apiKey, + baseURL, + defaultHeaders: { "User-Agent": userAgent }, + }), defaults, }; } @@ -84,7 +95,11 @@ export function createLLMClient(config: LLMConfig): LLMClient { provider: "openai", apiFormat, stream, - _openai: new OpenAI({ apiKey: config.apiKey, baseURL: config.baseUrl }), + _openai: new OpenAI({ + apiKey: config.apiKey, + baseURL: config.baseUrl, + defaultHeaders: { "User-Agent": userAgent }, + }), defaults, }; }