-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathproviders.js
More file actions
197 lines (183 loc) · 6.09 KB
/
providers.js
File metadata and controls
197 lines (183 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// LLM provider abstraction. Calls go through a proxy URL by default —
// never ship API keys in the browser. The server-side proxy at
// /api/llm/anthropic accepts an Anthropic-shape body and routes to
// Anthropic, Groq, or OpenRouter based on the model id — so this client
// keeps emitting Anthropic-shape requests for every supported model
// (paid Claude *and* free Llama/Hermes/GPT-OSS).
export class AnthropicProvider {
constructor({
// Free OpenRouter Llama 3.3 70B — no per-token cost to the host.
// Owners can override with a paid Claude model via embed-policy.
model = 'meta-llama/llama-3.3-70b-instruct:free',
proxyURL,
apiKey,
agentId,
apiOrigin,
temperature = 0.7,
maxTokens = 4096,
thinking = 'auto',
} = {}) {
this.model = model;
this.apiKey = apiKey;
this.temperature = temperature;
this.maxTokens = maxTokens;
this.thinking = thinking;
// Priority: explicit proxyURL > direct key > we-pay fallback.
// Direct-key mode is only safe for Claude models (apiKey is then an
// Anthropic key); free Groq/OR models always go through the proxy.
if (proxyURL) {
this.proxyURL = proxyURL;
} else if (!apiKey && agentId && apiOrigin) {
this.proxyURL = `${apiOrigin}/api/llm/anthropic?agent=${encodeURIComponent(agentId)}`;
} else {
this.proxyURL = null;
}
}
/**
* Send a completion request to Anthropic.
* Streams the response via SSE and calls `onChunk` for each text delta.
* @param {{ system, messages, tools, onChunk?, signal? }} opts
* @returns {Promise<{ text, toolCalls, thinking, stopReason }>}
*/
async complete({ system, messages, tools, onChunk, signal }) {
const body = {
model: this.model,
max_tokens: this.maxTokens,
temperature: this.temperature,
system,
messages,
stream: true,
};
if (tools && tools.length) body.tools = tools;
if (this.thinking === 'always') {
body.thinking = { type: 'enabled', budget_tokens: 2048 };
}
const url = this.proxyURL || 'https://api.anthropic.com/v1/messages';
const headers = { 'content-type': 'application/json' };
if (this.apiKey) {
headers['x-api-key'] = this.apiKey;
headers['anthropic-version'] = '2023-06-01';
headers['anthropic-dangerous-direct-browser-access'] = 'true';
}
const res = await fetch(url, {
method: 'POST',
headers,
body: JSON.stringify(body),
signal,
});
if (!res.ok) {
const text = await res.text();
throw new Error(`Anthropic ${res.status}: ${text}`);
}
const out = { text: '', toolCalls: [], thinking: '', stopReason: 'end_turn' };
let currentToolCall = null;
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buf = '';
signal?.addEventListener('abort', () => reader.cancel());
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
if (signal?.aborted) break;
buf += decoder.decode(value, { stream: true });
const lines = buf.split('\n');
buf = lines.pop();
for (const line of lines) {
if (!line.startsWith('data: ')) continue;
const payload = line.slice(6).trim();
if (payload === '[DONE]') break;
let evt;
try {
evt = JSON.parse(payload);
} catch {
continue;
}
if (evt.type === 'message_delta' && evt.delta?.stop_reason) {
out.stopReason = evt.delta.stop_reason;
} else if (evt.type === 'content_block_start') {
const block = evt.content_block;
if (block?.type === 'tool_use') {
currentToolCall = { id: block.id, name: block.name, input: '' };
}
} else if (evt.type === 'content_block_delta') {
const delta = evt.delta;
if (delta?.type === 'text_delta') {
out.text += delta.text;
onChunk?.(delta.text);
} else if (delta?.type === 'thinking_delta') {
out.thinking += delta.thinking;
} else if (delta?.type === 'input_json_delta' && currentToolCall) {
currentToolCall.input += delta.partial_json;
}
} else if (evt.type === 'content_block_stop') {
if (currentToolCall) {
try {
currentToolCall.input = JSON.parse(currentToolCall.input);
} catch {
currentToolCall.input = {};
}
out.toolCalls.push(currentToolCall);
currentToolCall = null;
}
}
}
}
} catch (err) {
if (err.name === 'AbortError') return out;
throw err;
}
return out;
}
_normalize(data) {
// Convert Anthropic response shape into the runtime's common shape:
// { text, toolCalls: [{ id, name, input }], stopReason }
const out = { text: '', toolCalls: [], thinking: '', stopReason: data.stop_reason };
for (const block of data.content || []) {
if (block.type === 'text') out.text += block.text;
else if (block.type === 'thinking') out.thinking += block.thinking || '';
else if (block.type === 'tool_use') {
out.toolCalls.push({ id: block.id, name: block.name, input: block.input });
}
}
return out;
}
// Convert normalized tool results back into a user-role message
formatToolResults(results) {
return {
role: 'user',
content: results.map((r) => ({
type: 'tool_result',
tool_use_id: r.id,
content: typeof r.output === 'string' ? r.output : JSON.stringify(r.output),
is_error: !!r.isError,
})),
};
}
formatAssistantWithToolCalls(text, toolCalls) {
const content = [];
if (text) content.push({ type: 'text', text });
for (const c of toolCalls) {
content.push({ type: 'tool_use', id: c.id, name: c.name, input: c.input });
}
return { role: 'assistant', content };
}
}
export class NullProvider {
// Used when brain.provider === "none" — skills drive the agent.
async complete() {
return { text: '', toolCalls: [], stopReason: 'end_turn' };
}
formatToolResults() {
return { role: 'user', content: [] };
}
formatAssistantWithToolCalls(text) {
return { role: 'assistant', content: text || '' };
}
}
export function createProvider(config = {}) {
const provider = config.provider || 'anthropic';
if (provider === 'anthropic') return new AnthropicProvider(config);
if (provider === 'none') return new NullProvider();
throw new Error(`Unsupported provider: ${provider}. Supported: anthropic, none`);
}