feat(ai): Implement unified AI API with Anthropic provider

- Define clean API with complete() method and callbacks for streaming
- Add comprehensive type system for messages, tools, and usage
- Implement AnthropicAI provider with full feature support:
  - Thinking/reasoning with signatures
  - Tool calling with parallel execution
  - Streaming via callbacks (onText, onThinking)
  - Proper error handling and stop reasons
  - Cache tracking for input/output tokens
- Add working test/example demonstrating tool execution flow
- Support for system prompts, temperature, max tokens
- Proper message role types: user, assistant, toolResult
This commit is contained in:
Mario Zechner 2025-08-17 23:30:20 +02:00
parent f064ea0e14
commit e5aedfed29
7 changed files with 597 additions and 1 deletions

View file

@ -0,0 +1,40 @@
# AI Package Implementation Plan
**Status:** InProgress
**Agent PID:** 5114
## Original Todo
ai: create an implementation plan based on packages/ai/plan.md and implement it
## Description
Implement the unified AI API as designed in packages/ai/plan.md. Create a single interface that works with OpenAI, Anthropic, and Gemini SDKs, handling their differences internally while exposing unified streaming events, tool calling, thinking/reasoning, and caching capabilities.
*Read [plan.md](packages/ai/plan.md) in full for the complete API design and implementation details*
## Implementation Plan
- [x] Define unified types in src/types.ts based on plan.md interfaces (AIConfig, Message, Request, Event, TokenUsage, ModelInfo)
- [ ] Implement OpenAI provider in src/providers/openai.ts with both Chat Completions and Responses API support
- [x] Implement Anthropic provider in src/providers/anthropic.ts with MessageStream and content blocks handling
- [ ] Implement Gemini provider in src/providers/gemini.ts with parts system and thinking extraction
- [ ] Create main AI class in src/index.ts that selects and uses appropriate adapter
- [ ] Implement models database in src/models.ts with model information and cost data
- [ ] Add cost calculation integrated into each adapter's token tracking
- [ ] Create comprehensive test suite in test/ai.test.ts using Node.js test framework
- [ ] Test: Model database lookup and capabilities detection
- [ ] Test: Basic completion (non-streaming) for all providers (OpenAI, Anthropic, Gemini, OpenRouter, Groq)
- [ ] Test: Streaming responses with event normalization across all providers
- [ ] Test: Thinking/reasoning extraction (o1 via Responses API, Claude thinking, Gemini thinking)
- [ ] Test: Tool calling flow with execution and continuation across providers
- [ ] Test: Automatic caching (Anthropic explicit, OpenAI/Gemini automatic)
- [ ] Test: Message serialization/deserialization with full conversation history
- [ ] Test: Cross-provider conversation continuation (start with one provider, continue with another)
- [ ] Test: Abort/cancellation via AbortController
- [ ] Test: Error handling and retry logic for each provider
- [ ] Test: Cost tracking accuracy with known token counts
- [ ] Update root tsconfig.json paths to include @mariozechner/pi-ai
- [ ] Update root package.json build script to include AI package
## Notes
- Package structure already exists at packages/ai with dependencies installed
- Each adapter handles its own event normalization internally
- Tests use Node.js built-in test framework as per project conventions
- Available API keys: OPENAI_API_KEY, ANTHROPIC_API_KEY, GEMINI_API_KEY, GROQ_API_KEY, OPENROUTER_API_KEY