Repository navigation
Expand file tree
/
Copy pathagentModel.ts
More file actions
329 lines (307 loc) · 12 KB
/
Copy pathagentModel.ts
File metadata and controls
329 lines (307 loc) · 12 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import { getLocalAgentPreference } from './localAgentPreferences.ts'
import { geminiCloudRuns, geminiCloudWorkerFor } from './geminiCloudRuns.ts'
import { localAgentRuns, type LocalHarnessRequest } from './localAgentRuns.ts'
import type { LocalAgentResult } from '../shared/localAgent.ts'
import Anthropic from '@anthropic-ai/sdk'
import { getAccount, withFreshToken, accountModelFor, accountVisionFor } from './modelAccounts.ts'
import type { AccountKind, ModelAccount } from './modelAccounts.ts'
import { ModelAuthError, ModelUnavailableError, runAzureTurn, runOpenAiTurn } from './openaiAgent.ts'
import type { StopReason, TurnBlock } from './openaiAgent.ts'
import { geminiConfig, openrouterConfig, runChatCompletionsTurn } from './chatCompletionsAgent.ts'
/**
* Which model runs a Doop Agent turn, and on whose bill.
*
* Two layers, resolved in this order:
*
* - a user's own model account (their ChatGPT subscription or an OpenAI API
* key) — the moment one is connected, that user's runs move onto it and
* the free-task meter stops applying to them;
* - the server tier, which pays for the free tasks every account starts
* with. DOOP_AGENT_PROVIDER picks what it runs on: 'anthropic' (the
* default, on ANTHROPIC_API_KEY) or 'azure' (the AZURE_OPENAI_* vars).
*
* A run bills exactly ONE person: the requester behind the work it claims. The
* queue is worked one requester at a time rather than sweeping several people's
* cards into a single call, so nobody's subscription ever pays for someone
* else's request.
*/
export type ServerProvider = 'anthropic' | 'azure'
export type Provider = ServerProvider | AccountKind | 'claude-local' | 'gemini-cloud'
export interface AgentTurnRequest {
/** ordered system blocks; `cache` marks an Anthropic cache breakpoint */
system: { text: string; cache?: boolean }[]
tools: Anthropic.Tool[]
messages: Anthropic.MessageParam[]
maxTokens: number
}
export interface AgentTurnResult {
content: TurnBlock[]
stop_reason: StopReason
}
export interface AgentModel {
provider: Provider
/** for logs and the canvas status line, e.g. "ChatGPT (gpt-5)" */
label: string
/** the user whose account pays, when it isn't the server's key */
userId?: string
/** false = the model cannot see images and the run degrades to no visual
* review; absent means true (every pre-roster provider has vision) */
vision?: boolean
runHarness?: (req: LocalHarnessRequest) => Promise<LocalAgentResult>
run(req: AgentTurnRequest): Promise<AgentTurnResult>
}
export { ModelAuthError, ModelUnavailableError }
export class ModelConfigurationError extends ModelAuthError {}
/* ---------------------------------------------------------------- */
/* the server tier: pays for everyone's free tasks */
/* ---------------------------------------------------------------- */
const ANTHROPIC_MODEL = process.env.DOOP_AGENT_MODEL || 'claude-opus-5'
let anthropic: Anthropic | null = null
function anthropicTier(): AgentModel | null {
if (!process.env.ANTHROPIC_API_KEY) {
warnOnce(
'[doop-agent] ANTHROPIC_API_KEY not set — the free Doop Agent tier is off. Users who connect a model account of their own still get the agent; everyone else sees queued cards and @mentions go unpicked. See README → "The Doop Agent".',
)
return null
}
if (!anthropic) anthropic = new Anthropic()
const client = anthropic
return {
provider: 'anthropic',
label: `Doop (${ANTHROPIC_MODEL})`,
run: (req) => runAnthropicTurn(client, ANTHROPIC_MODEL, req),
}
}
/**
* One Anthropic turn, streamed and collected into a message.
*
* Streaming is not for show here: it is what lets a turn run long. The SDK
* refuses a non-streaming request whose max_tokens implies more than ten
* minutes of generation ("Streaming is required for operations that may take
* longer than 10 minutes"), and GitHub recon asks for 32k tokens per turn. A
* bigger client timeout would silence that check but leave a silent HTTP
* connection open for the whole generation, which proxies drop. Over SSE the
* SDK's timeout only guards the wait for headers; the response itself stays
* alive on the API's ping events until the message is complete.
*/
export async function runAnthropicTurn(
client: Anthropic,
model: string,
req: AgentTurnRequest,
): Promise<AgentTurnResult> {
const res = await client.messages
.stream({
model,
max_tokens: req.maxTokens,
system: req.system.map((block) => ({
type: 'text' as const,
text: block.text,
...(block.cache ? { cache_control: { type: 'ephemeral' as const } } : {}),
})),
tools: req.tools,
messages: req.messages,
})
.finalMessage()
const stop: StopReason =
res.stop_reason === 'refusal'
? 'refusal'
: res.stop_reason === 'max_tokens'
? 'max_tokens'
: res.stop_reason === 'tool_use'
? 'tool_use'
: 'end_turn'
return { content: res.content as TurnBlock[], stop_reason: stop }
}
function azureTier(): AgentModel | null {
const endpoint = process.env.AZURE_OPENAI_ENDPOINT
const deployment = process.env.AZURE_OPENAI_DEPLOYMENT
const apiKey = process.env.AZURE_OPENAI_API_KEY
if (!endpoint || !deployment || !apiKey) {
warnOnce(
'[doop-agent] DOOP_AGENT_PROVIDER=azure needs AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_DEPLOYMENT and AZURE_OPENAI_API_KEY — the free Doop Agent tier is off until all three are set.',
)
return null
}
const config = { endpoint, deployment, apiKey }
return {
provider: 'azure',
label: `Doop (${deployment})`,
async run(req) {
try {
return await runAzureTurn(config, {
system: joinSystem(req),
tools: req.tools,
messages: req.messages,
maxTokens: req.maxTokens,
})
} catch (err) {
/* these are the SERVER's credentials — "reconnect your account" would
send users chasing a connection they don't have */
if (err instanceof ModelAuthError) {
throw new Error(
'Azure OpenAI rejected this server’s credentials — check AZURE_OPENAI_API_KEY and AZURE_OPENAI_DEPLOYMENT',
{ cause: err },
)
}
throw err
}
},
}
}
const serverTiers: Record<ServerProvider, () => AgentModel | null> = {
anthropic: anthropicTier,
azure: azureTier,
}
function serverProvider(): ServerProvider {
const chosen = process.env.DOOP_AGENT_PROVIDER || 'anthropic'
if (chosen in serverTiers) return chosen as ServerProvider
warnOnce(
`[doop-agent] DOOP_AGENT_PROVIDER="${chosen}" is not a server provider (anthropic | azure) — using anthropic.`,
)
return 'anthropic'
}
/** What the boot banner reports: which provider the free tier would run on,
* and whether it actually can. */
export function serverTierInfo(): { provider: ServerProvider; ready: boolean } {
const provider = serverProvider()
return { provider, ready: serverTiers[provider]() !== null }
}
const warnedAbout = new Set<string>()
function warnOnce(message: string) {
if (warnedAbout.has(message)) return
warnedAbout.add(message)
console.log(message)
}
/* ---------------------------------------------------------------- */
/* a user's own account */
/* ---------------------------------------------------------------- */
const BYO_LABELS: Record<AccountKind, string> = {
chatgpt: 'ChatGPT',
'openai-key': 'OpenAI',
'anthropic-key': 'Claude API',
'openrouter-key': 'OpenRouter',
'gemini-key': 'Gemini',
}
/* the OpenAI-shaped transports take one system string; cache breakpoints are
an Anthropic concept and simply flatten away */
function joinSystem(req: AgentTurnRequest): string {
return req.system.map((block) => block.text).join('\n\n')
}
function byoModel(account: ModelAccount): AgentModel {
if (account.kind === 'anthropic-key') {
if (!account.apiKey) throw new ModelAuthError('Reconnect your Claude API key in Settings.')
const client = new Anthropic({ apiKey: account.apiKey })
const model = accountModelFor(account)
return {
provider: account.kind,
label: `Claude API (${model})`,
userId: account.userId,
async run(req) {
try {
return await runAnthropicTurn(client, model, req)
} catch (error) {
if (
error instanceof Anthropic.APIError &&
(error.status === 400 || error.status === 404) &&
/anthropic-workspace-id|workspace/i.test(error.message)
) {
throw new ModelConfigurationError(
'Create an Anthropic API key scoped to one workspace, then use Rotate key in Settings → Claude API key and retry.',
)
}
if (error instanceof Anthropic.APIError && (error.status === 401 || error.status === 403)) {
throw new ModelAuthError('Anthropic rejected your API key. Reconnect it in Settings.')
}
throw error
}
},
}
}
if (account.kind === 'openrouter-key' || account.kind === 'gemini-key') {
if (!account.apiKey) throw new ModelAuthError(`Reconnect your ${BYO_LABELS[account.kind]} key in Settings.`)
const model = accountModelFor(account)
const vision = accountVisionFor(account)
const config =
account.kind === 'openrouter-key'
? openrouterConfig(account.apiKey, model, vision)
: geminiConfig(account.apiKey, model, vision)
return {
provider: account.kind,
label: `${BYO_LABELS[account.kind]} (${model})`,
userId: account.userId,
vision,
run: (req) =>
runChatCompletionsTurn(config, {
system: joinSystem(req),
tools: req.tools,
messages: req.messages,
maxTokens: req.maxTokens,
}),
}
}
return {
provider: account.kind,
label: `${BYO_LABELS[account.kind]} (${accountModelFor(account)})`,
userId: account.userId,
async run(req) {
/* refreshed per turn, not per run: a long design run outlives an
hour-long access token */
const live = await withFreshToken(account)
return runOpenAiTurn(live, {
system: joinSystem(req),
tools: req.tools,
messages: req.messages,
maxTokens: req.maxTokens,
})
},
}
}
/**
* Pick the model for one run, billed to exactly one person: `payerId` is the
* human whose work the run is about to claim. Returns null when nothing can
* run it — they have no account of their own and the server tier is off.
*
* A connected account wins outright: someone who has just linked their own
* subscription expects the very next task to run on it, and the free tier is a
* trial to get them here, not a balance to spend down first. It also means
* connecting stops costing us anything from that moment on.
*/
export async function pickModel(payerId?: string): Promise<AgentModel | null> {
if (payerId) {
const worker = geminiCloudWorkerFor(payerId)
if (worker) {
return {
provider: 'gemini-cloud',
label: 'Gemini CLI (cloud pilot)',
userId: payerId,
runHarness: (req) => geminiCloudRuns.start(payerId, worker, req),
run: () =>
Promise.reject(
new Error('Gemini cloud pilot supports canvas tasks only. Repository imports require a server provider.'),
),
}
}
const local = await getLocalAgentPreference(payerId)
if (local.enabled) {
if (!localAgentRuns.online(payerId)) return null
return {
provider: 'claude-local',
label: `Claude CLI (${local.model})`,
userId: payerId,
runHarness: (req) => localAgentRuns.start(payerId, local.model, req),
run: () =>
Promise.reject(
new Error('Repository imports require a server provider. Select your connected account in Settings.'),
),
}
}
}
const account = payerId
? await getAccount(payerId).catch((err) => {
console.error('[doop-agent] could not read the connected model account', err)
return null
})
: null
if (account) return byoModel(account)
return serverTiers[serverProvider()]()
}