AI Proxy client for Forest Admin.
import { createAiProxyClient } from '@forestadmin/ai-proxy/client';
const client = createAiProxyClient({ baseUrl: 'https://my-agent.com/forest' });
const response = await client.chat('Hello!');
console.log(response.choices[0].message.content);Client only (frontend, no extra dependencies):
npm install @forestadmin/ai-proxyServer side (Router, ProviderDispatcher):
npm install @forestadmin/ai-proxy @langchain/core @langchain/openaiChoose one mode:
// Simple mode (recommended)
const client = createAiProxyClient({
baseUrl: 'https://my-agent.com/forest',
headers: { Authorization: 'Bearer token' }, // optional
timeout: 30000, // optional (default: 30s)
});
// Custom fetch mode
const client = createAiProxyClient({
fetch: myCustomFetch,
timeout: 30000,
});// Simple
await client.chat('Hello!');
// With options
await client.chat({
messages: [{ role: 'user', content: 'Hello!' }],
tools: [...], // optional
toolChoice: 'auto', // optional
aiName: 'gpt-4', // optional - server AI config name
});const tools = await client.getTools();
// [{ name: 'brave_search', description: '...', schema: {...} }]const result = await client.callTool('brave_search', [
{ role: 'user', content: 'cats' }
]);import { AiProxyClientError } from '@forestadmin/ai-proxy/client';
try {
await client.chat('Hello');
} catch (error) {
if (error instanceof AiProxyClientError) {
console.error(error.status, error.message);
if (error.isNetworkError) { /* status 0 */ }
if (error.isClientError) { /* status 4xx */ }
if (error.isServerError) { /* status 5xx */ }
}
}