Commit Graph

33 Commits

  • feat(ai): switch xiaomi default to api billing, add per-region token plan providers (#4112)
    Built-in `xiaomi` provider now targets the API billing endpoint (https://api.xiaomimimo.com/anthropic) — a single stable URL for keys issued at platform.xiaomimimo.com. The Token Plan endpoints are exposed as three sibling providers, each with its own env var:
    
    - xiaomi-token-plan-cn: XIAOMI_TOKEN_PLAN_CN_API_KEY
    - xiaomi-token-plan-ams: XIAOMI_TOKEN_PLAN_AMS_API_KEY
    - xiaomi-token-plan-sgp: XIAOMI_TOKEN_PLAN_SGP_API_KEY
    
    BREAKING CHANGE: users who previously set XIAOMI_API_KEY against the Token Plan AMS endpoint must move to xiaomi-token-plan-ams and set XIAOMI_TOKEN_PLAN_AMS_API_KEY. This also resolves the 401 reported by on #4005, where a platform.xiaomimimo.com key fails against the Token Plan endpoint.
    
    closes #4082
  • feat(ai): add Xiaomi MiMo provider (#4005)
    * fix(ai): include minimax-cn in cross-provider-handoff matrix
    
    * feat(ai): add Xiaomi MiMo provider
    
    Adds Xiaomi MiMo as an openai-completions-compatible provider.
    
    - packages/ai: register provider in types/KnownProvider, env-api-keys (XIAOMI_API_KEY), generate-models, models.generated.ts, overflow util, README, CHANGELOG
    - packages/ai/test: extend stream, tokens, abort, empty, context-overflow, overflow, image-tool-result, tool-call-without-result, total-tokens, unicode-surrogate, cross-provider-handoff matrices with Xiaomi
    - packages/coding-agent: default model (mimo-v2.5-pro), display name (Xiaomi MiMo), CLI env var docs, README, docs/providers.md
    
    closes #3912
    
    ---------
    
    Co-authored-by: Mario Zechner <badlogicgames@gmail.com>
  • feat(ai): add Cloudflare Workers AI as a provider (#3851)
    * feat(ai): add Cloudflare Workers AI as a provider
    
    Cloudflare Workers AI hosts open-weight LLMs (Kimi K2.6, GPT-OSS,
    GLM-4.7, Llama 4, Gemma 4, Nemotron 3) on Cloudflare's GPU network with
    an OpenAI-compatible endpoint. Reuses the openai-completions API
    protocol; the per-account URL contains a {CLOUDFLARE_ACCOUNT_ID}
    placeholder resolved at request time by a small helper.
    
    Pi automatically sets x-session-affinity for prefix caching:
    https://developers.cloudflare.com/workers-ai/features/prompt-caching/
    
    Auth: CLOUDFLARE_API_KEY (matches pi's *_API_KEY convention) +
    CLOUDFLARE_ACCOUNT_ID. The User-Agent identifies traffic as
    'pi-coding-agent' in Cloudflare analytics.
    
    Verified end-to-end against a real Cloudflare account: 17 e2e tests
    pass across stream/empty/tokens/unicode/tool-call-without-result/
    total-tokens against @cf/moonshotai/kimi-k2.6.
    
    Cloudflare AI Gateway is a separate, larger change (it requires routing
    through provider-specific subpaths with the matching API protocol per
    upstream) and will land in a follow-up PR.
    
    * refactor(ai): move Cloudflare User-Agent and session-affinity flag to per-model metadata
    
    Instead of conditionally setting them in openai-completions.ts based on
    provider detection, declare them as model-level fields in the catalog
    (headers + compat). This is consistent with how the github-copilot and
    kimi-coding entries already declare their static headers.
    
      packages/ai/scripts/generate-models.ts: emit headers and compat fields
      on each cloudflare-workers-ai entry (CLOUDFLARE_STATIC_HEADERS).
      packages/ai/src/providers/openai-completions.ts: drop the
      isCloudflareProvider conditional that injected User-Agent and the
      isCloudflareWorkersAI override of sendSessionAffinityHeaders.
      packages/ai/src/models.generated.ts: re-spliced 8 cloudflare-workers-ai
      entries with headers + compat.
    
    Behavior is unchanged - verified via fetch interceptor that User-Agent
    and x-session-affinity / session_id / x-client-request-id are still sent
    on outbound requests. 5/5 e2e tests pass.
  • fix(ai): own Anthropic SSE parsing to avoid SDK JSON.parse hard-failures
    Bypass the Anthropic SDK streaming parser entirely. Use
    client.messages.create().asResponse() and decode the SSE stream
    ourselves with defensive JSON parsing that repairs invalid escape
    sequences and control characters inside string literals.
    
    - Switch from SDK .stream() to .asResponse() + pi-owned SSE decoder
    - Add repairJson() / parseJsonWithRepair() to json-parse.ts
    - Add anthropic-sse-parsing.test.ts regression for malformed tool deltas
    - Update github-copilot-anthropic.test.ts mock to match new call path
    - Update deprecated claude-3-5-haiku-20241022 refs to claude-haiku-4-5
    - Remove stale non-reasoning model test
    
    fixes #3175
  • feat(ai): add Kimi For Coding provider support
    - Add kimi-coding provider using Anthropic Messages API
    - API endpoint: https://api.kimi.com/coding/v1
    - Environment variable: KIMI_API_KEY
    - Models: kimi-k2-thinking (text), k2p5 (text + image)
    - Add context overflow detection pattern for Kimi errors
    - Add tests for all standard test suites
  • feat(ai): add Hugging Face provider support
    - Add huggingface to KnownProvider type
    - Add HF_TOKEN env var mapping
    - Process huggingface models from models.dev (14 models)
    - Use openai-completions API with compat settings
    - Add tests for all provider test suites
    - Update documentation
    
    fixes #994
  • Fix PR #689: Add changelog attribution, coding-agent changelog, fix test types, add provider to test suites
    - Fix ai/CHANGELOG.md: add PR link and author attribution
    - Add coding-agent/CHANGELOG.md entry for vercel-ai-gateway provider
    - Fix model-resolver.test.ts: use anthropic-messages API type to match generated models
    - Add vercel-ai-gateway to test suites: tokens, abort, empty, context-overflow, unicode-surrogate, tool-call-without-result, image-tool-result, total-tokens, image-limits
  • Add MiniMax provider support (#656 by @dannote)
    - Add minimax to KnownProvider and Api types
    - Add MINIMAX_API_KEY to getEnvApiKey()
    - Generate MiniMax-M2 and MiniMax-M2.1 models
    - Add context overflow detection pattern
    - Add tests to all required test files
    - Update README and CHANGELOG with attribution
    
    Also fixes:
    - Bedrock duplicate toolResult ID when content has multiple blocks
    - Sandbox extension unused parameter lint warning
  • feat(ai): Add Amazon Bedrock provider (#494)
    Adds support for Amazon Bedrock with Claude models including:
    - Full streaming support via Converse API
    - Reasoning/thinking support for Claude models
    - Cross-region inference model ID handling
    - Multiple AWS credential sources (profile, IAM keys, API keys)
    - Image support in messages and tool results
    - Unicode surrogate sanitization
    
    Also adds 'Adding a New Provider' documentation to AGENTS.md and README.
    
    Co-authored-by: nickchan2 <nickchan2@users.noreply.github.com>
  • fix(ai): append system prompt to codex bridge message instead of converting to input
    Previously the system prompt was converted to an input message in convertMessages,
    then stripped out by filterPiSystemPrompts. Now the system prompt is passed directly
    to transformRequestBody and appended after CODEX_PI_BRIDGE in the bridge message.
  • WIP: Remove global state from pi-ai OAuth/API key handling
    - Remove setApiKey, resolveApiKey, and global apiKeys Map from stream.ts
    - Rename getApiKey to getApiKeyFromEnv (only checks env vars)
    - Remove OAuth storage layer (storage.ts deleted)
    - OAuth login/refresh functions now return credentials instead of saving
    - getOAuthApiKey/refreshOAuthToken now take credentials as params
    - Add test/oauth.ts helper for ai package tests
    - Simplify root npm run check (single biome + tsgo pass)
    - Remove redundant check scripts from most packages
    - Add web-ui and coding-agent examples to biome/tsgo includes
    
    coding-agent still has compile errors - needs refactoring for new API
  • Add Mistral as AI provider
    - Add Mistral to KnownProvider type and model generation
    - Implement Mistral-specific compat handling in openai-completions:
      - requiresToolResultName: tool results need name field
      - requiresAssistantAfterToolResult: synthetic assistant message between tool/user
      - requiresThinkingAsText: thinking blocks as <thinking> text
      - requiresMistralToolIds: tool IDs must be exactly 9 alphanumeric chars
    - Add MISTRAL_API_KEY environment variable support
    - Add Mistral tests across all test files
    - Update documentation (README, CHANGELOG) for both ai and coding-agent packages
    - Remove client IDs from gemini.md, reference upstream source instead
    
    Closes #165
  • Add totalTokens field to Usage type
    - Added totalTokens field to Usage interface in pi-ai
    - Anthropic: computed as input + output + cacheRead + cacheWrite
    - OpenAI/Google: uses native total_tokens/totalTokenCount
    - Fixed openai-completions to compute totalTokens when reasoning tokens present
    - Updated calculateContextTokens() to use totalTokens field
    - Added comprehensive test covering 13 providers
    
    fixes #130
  • refactor(ai): improve error handling and stop reason types
    - Add 'aborted' as a distinct stop reason separate from 'error'
    - Change AssistantMessage.error to errorMessage for clarity
    - Update error event to include reason field ('error' | 'aborted')
    - Map provider-specific safety/refusal reasons to 'error' stop reason
    - Reorganize utility functions into utils/ directory
    - Rename agent.ts to agent-loop.ts for better clarity
    - Fix error handling in all providers to properly distinguish abort from error
  • feat(ai): Implement Zod-based tool validation and improve Agent API
    - Replace JSON Schema with Zod schemas for tool parameter definitions
    - Add runtime validation for all tool calls at provider level
    - Create shared validation module with detailed error formatting
    - Update Agent API with comprehensive event system
    - Add agent tests with calculator tool for multi-turn execution
    - Add abort test to verify proper handling of aborted requests
    - Update documentation with detailed event flow examples
    - Rename generate.ts to stream.ts for clarity
  • feat(ai): Add zAI provider support
    - Add 'zai' as a KnownProvider type
    - Add ZAI_API_KEY environment variable mapping
    - Generate 4 zAI models (glm-4.5-air, glm-4.5v, etc.) using anthropic-messages API
    - Add comprehensive test coverage for zAI provider in generate.test.ts and empty.test.ts
    - Models support reasoning/thinking capabilities and tool calling
  • Massive refactor of API
    - Switch to function based API
    - Anthropic SDK style async generator
    - Fully typed with escape hatches for custom models
  • test(ai): Add empty assistant message tests
    - Test providers handling empty assistant messages in conversation flow
    - Pattern: user message -> empty assistant -> user message
    - All providers handle empty assistant messages gracefully
    - Tests ensure providers can continue conversation after empty response
  • test(ai): Add empty message tests for all providers
    - Test handling of empty content arrays
    - Test handling of empty string content
    - Test handling of whitespace-only content
    - All providers handle these edge cases gracefully