A custom OpenAPI-compatible agent that works with any OpenAI-compatible API endpoint.
- ✅ Universal Compatibility: Works with any OpenAI-compatible API
- ✅ Streaming Support: Full support for streaming responses
- ✅ Tool Calling: Supports function/tool calling
- ✅ Conversation History: Maintains chat context per conversation
- ✅ Model Discovery: Auto-discovers available models from endpoint
- ✅ Authentication: Supports API key authentication (optional)
The OpenAPI agent works with:
- LM Studio -
http://localhost:1234 - Ollama (with OpenAI compatibility) -
http://localhost:11434/v1 - LocalAI -
http://localhost:8080 - vLLM -
http://localhost:8000 - Text Generation WebUI -
http://localhost:5000
- Any OpenAI-compatible cloud endpoint
- Custom deployed models with OpenAI-compatible APIs
import { OpenAPIAgent } from '@computer-use-agent/agent-core'
const config = {
provider: 'custom',
model: {
id: 'llama-3-8b-instruct',
label: 'Llama 3 8B Instruct',
provider: 'custom',
},
credentials: {
baseUrl: 'http://localhost:1234',
apiKey: 'optional-api-key', // Optional
},
}
const agent = new OpenAPIAgent(config)const models = await agent.getModels()
console.log(models)
// [{ id: 'llama-3-8b-instruct', label: 'llama-3-8b-instruct', provider: 'custom' }, ...]const stream = await agent.sendMessage(
'What is the capital of France?',
'llama-3-8b-instruct', // optional model ID
'conversation-1' // optional conversation ID
)
// Process the stream
for await (const chunk of stream) {
console.log(chunk)
}The agent emits several event types:
// Text delta (incremental content)
event: message
data: {"type":"text-delta","delta":"Hello"}
// Tool execution result
event: tool-result
data: {"type":"tool-result","toolName":"mouse_move","result":{...}}
// Error during tool execution
event: error
data: {"type":"tool-error","error":"Tool execution failed"}
// Completion
event: meta
data: {"type":"completed"}- Download and install LM Studio
- Download a model (e.g., Llama 3)
- Start the local server (default:
http://localhost:1234) - Configure the agent:
const lmStudioAgent = new OpenAPIAgent({
provider: 'custom',
model: {
id: 'llama-3-8b-instruct',
label: 'Llama 3 8B Instruct',
provider: 'custom',
},
credentials: {
baseUrl: 'http://localhost:1234',
},
})- Install Ollama
- Pull a model:
ollama pull llama3.2 - Ollama's OpenAI-compatible endpoint is at
/v1 - Configure the agent:
const ollamaAgent = new OpenAPIAgent({
provider: 'custom',
model: {
id: 'llama3.2',
label: 'Llama 3.2',
provider: 'custom',
},
credentials: {
baseUrl: 'http://localhost:11434/v1',
},
})const customAgent = new OpenAPIAgent({
provider: 'custom',
model: {
id: 'custom-model-id',
label: 'Custom Model',
provider: 'custom',
},
credentials: {
baseUrl: 'https://api.example.com',
apiKey: 'your-api-key-here',
},
})The agent expects the endpoint to implement these OpenAI-compatible routes:
- Endpoint:
GET /v1/models - Response:
{
"data": [
{ "id": "model-id", "object": "model", ... }
]
}- Endpoint:
POST /v1/chat/completions - Request:
{
"model": "model-id",
"messages": [
{ "role": "user", "content": "Hello" }
],
"stream": true,
"tools": [...]
}- Response: Server-Sent Events (SSE) stream
- Each conversation is identified by a
chatId - Conversation history is maintained in memory
- Messages are automatically added to history
- Automatically includes all available tools in requests
- Executes tools when model requests them
- Returns tool results in the stream
- Gracefully falls back to default models on error
- Provides detailed error messages in stream
- Handles network failures and timeouts
Run tests:
pnpm test openapi-agentOpenAPIAgent
├── implements Agent<'custom'>
├── manages conversation history
├── handles streaming responses
├── executes tool calls
└── supports model discovery
When adding features:
- Maintain OpenAI compatibility
- Add tests for new functionality
- Update documentation
- Ensure streaming works correctly
MIT