Repository navigation
Expand file tree
/
Copy pathopenaiAgent.ts
More file actions
452 lines (419 loc) · 18.1 KB
/
Copy pathopenaiAgent.ts
File metadata and controls
452 lines (419 loc) · 18.1 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import { randomUUID } from 'node:crypto'
import type Anthropic from '@anthropic-ai/sdk'
import type { ModelAccount } from './modelAccounts.ts'
/**
* Runs one Doop Agent turn on OpenAI, speaking the Anthropic message shape
* the agent loop is already written in. Everything OpenAI-specific — the
* Responses API item format, tool-call ids, streaming — is contained here, so
* server/resident.ts stays a single provider-neutral loop.
*
* Three transports, one body:
* - a connected ChatGPT subscription, through the same Codex backend the
* Codex CLI uses (SSE only, no max_output_tokens);
* - a plain OpenAI API key against api.openai.com (ordinary JSON);
* - an Azure OpenAI deployment (same Responses API, authenticated with
* Azure's api-key header) — SERVER config only, the free tier when
* DOOP_AGENT_PROVIDER=azure. Deliberately not a connectable per-user
* account: a user-supplied endpoint would be a server-side fetch target,
* i.e. an SSRF into whatever network Doop runs on.
*
* The one real impedance mismatch is images. Anthropic returns screenshots
* inside tool results; the Responses API only accepts text in a
* function_call_output, so each image is re-attached as a following user
* message. The model sees the same pixels in the same order.
*/
export const CHATGPT_URL = process.env.CHATGPT_RESPONSES_URL || 'https://chatgpt.com/backend-api/codex/responses'
export const OPENAI_URL = process.env.OPENAI_RESPONSES_URL || 'https://api.openai.com/v1/responses'
/**
* The OpenAI tiers a ChatGPT sign-in and an API key can both reach: GPT-6
* Astra (the September 2026 flagship — on Codex it needs a Plus plan or
* better, and OpenAI is still rolling it out account by account) on top of
* the three GPT-5.6 tiers. Users pick one in Settings — they are paying for
* it, and the tiers trade real money against real quality — so this list is
* the menu, not a detail. Ordered best-first; the default stays on Terra:
* Astra bills several times Sol per token, which is a choice to opt into.
*/
export const AGENT_MODELS = [
{ id: 'gpt-6-astra', name: 'GPT-6 Astra', blurb: 'Newest flagship — the sharpest reasoning, at the highest price' },
{ id: 'gpt-5.6-sol', name: 'GPT-5.6 Sol', blurb: 'Flagship — the most detail and polish, and the priciest' },
{ id: 'gpt-5.6-terra', name: 'GPT-5.6 Terra', blurb: 'The everyday workhorse. A good default for design work' },
{ id: 'gpt-5.6-luna', name: 'GPT-5.6 Luna', blurb: 'Fastest and cheapest — fine for small, mechanical edits' },
] as const
export const DEFAULT_OPENAI_MODEL = process.env.DOOP_AGENT_OPENAI_MODEL || 'gpt-5.6-terra'
const REASONING_EFFORT = process.env.DOOP_AGENT_OPENAI_EFFORT || 'medium'
/* Azure deployments are named by their owner and can host non-reasoning
models, which reject a `reasoning` block outright — so unlike the OpenAI
paths there is no default effort: unset means the parameter stays home. */
const AZURE_REASONING_EFFORT = process.env.AZURE_OPENAI_REASONING_EFFORT || ''
/* Azure's v1 surface needs no api-version; older resources can pin one. */
const AZURE_API_VERSION = process.env.AZURE_OPENAI_API_VERSION || ''
export function isKnownModel(id: string): boolean {
return AGENT_MODELS.some((model) => model.id === id)
}
/** What this account runs on: the user's choice, else the server default. */
export function modelFor(account: Pick<ModelAccount, 'model'>): string {
return account.model || DEFAULT_OPENAI_MODEL
}
/* The Codex backend only answers clients it recognises, so this is the value
it expects rather than a free-form product name. Override if OpenAI ever
admits other originators. */
export const ORIGINATOR = process.env.CHATGPT_ORIGINATOR || 'codex_cli_rs'
export type TurnBlock = Anthropic.TextBlockParam | Anthropic.ToolUseBlockParam
export type StopReason = 'end_turn' | 'tool_use' | 'max_tokens' | 'refusal'
export interface TurnRequest {
system: string
tools: Anthropic.Tool[]
messages: Anthropic.MessageParam[]
maxTokens: number
}
export interface TurnResult {
content: TurnBlock[]
stop_reason: StopReason
}
/** The connection is no longer usable (revoked, expired past refresh, or the
* key was rotated) — the user has to reconnect. */
export class ModelAuthError extends Error {}
/** The account is fine but cannot run the chosen model — Astra still rolling
* out to a ChatGPT plan, an API key without access to it — so the fix is a
* different tier in Settings, not a reconnect. */
export class ModelUnavailableError extends Error {}
/* how the OpenAI-shaped surfaces phrase "no such model for you": the API's
`model_not_found` code, the Codex backend's prose about the model not
existing / access, OpenRouter's endpoint/data-policy refusals and id
complaints, and Gemini's api-version wording */
const MODEL_UNAVAILABLE =
/model_not_found|no endpoints found|not a valid model id|is not found for api version|model[^.]{0,80}(does not exist|not found|not available|unsupported|no access|not (have|allowed))/i
export function isModelUnavailable(detail: string): boolean {
return MODEL_UNAVAILABLE.test(detail)
}
/* ---------------------------------------------------------------- */
/* Anthropic messages -> Responses input items */
/* ---------------------------------------------------------------- */
export type Part = { type: 'input_text'; text: string } | { type: 'input_image'; image_url: string; detail: 'auto' }
export const IMAGE_NOTE = 'The image(s) above are the result of the tool call you just made.'
export function imagePart(source: { type: string; media_type?: string; data?: string; url?: string }): Part | null {
if (source.type === 'url' && source.url) return { type: 'input_image', image_url: source.url, detail: 'auto' }
if (source.type === 'base64' && source.data) {
return {
type: 'input_image',
image_url: `data:${source.media_type || 'image/png'};base64,${source.data}`,
detail: 'auto',
}
}
return null
}
/** Split a tool result into the text the function_call_output carries and the
* images that have to ride along in a separate user message. Shared with the
* Chat Completions transport, whose `role: 'tool'` messages are text-only in
* exactly the same way. */
export function flattenToolResult(block: Anthropic.ToolResultBlockParam): { text: string; images: Part[] } {
const images: Part[] = []
const texts: string[] = []
if (typeof block.content === 'string') {
texts.push(block.content)
} else if (Array.isArray(block.content)) {
for (const part of block.content) {
if (part.type === 'text') texts.push(part.text)
else if (part.type === 'image') {
const image = imagePart(part.source as never)
if (image) {
images.push(image)
texts.push('[image returned — see the attached image below]')
}
}
}
}
const body = texts.join('\n').trim() || 'ok'
return { text: block.is_error ? `Error: ${body}` : body, images }
}
function toInput(messages: Anthropic.MessageParam[]): unknown[] {
const items: unknown[] = []
for (const message of messages) {
if (typeof message.content === 'string') {
items.push({
type: 'message',
role: message.role,
content: [
message.role === 'assistant'
? { type: 'output_text', text: message.content }
: { type: 'input_text', text: message.content },
],
})
continue
}
const userParts: Part[] = []
const trailingImages: Part[] = []
for (const block of message.content) {
switch (block.type) {
case 'text':
if (message.role === 'assistant') {
items.push({ type: 'message', role: 'assistant', content: [{ type: 'output_text', text: block.text }] })
} else {
userParts.push({ type: 'input_text', text: block.text })
}
break
case 'tool_use':
items.push({
type: 'function_call',
call_id: block.id,
name: block.name,
arguments: JSON.stringify(block.input ?? {}),
})
break
case 'tool_result': {
const { text, images } = flattenToolResult(block)
items.push({ type: 'function_call_output', call_id: block.tool_use_id, output: text })
trailingImages.push(...images)
break
}
case 'image': {
const image = imagePart(block.source as never)
if (image) userParts.push(image)
break
}
default:
break
}
}
if (userParts.length > 0) items.push({ type: 'message', role: 'user', content: userParts })
if (trailingImages.length > 0) {
items.push({
type: 'message',
role: 'user',
content: [...trailingImages, { type: 'input_text', text: IMAGE_NOTE }],
})
}
}
return items
}
function toTools(tools: Anthropic.Tool[]): unknown[] {
return tools.map((tool) => ({
type: 'function',
name: tool.name,
description: tool.description ?? '',
parameters: tool.input_schema,
strict: false,
}))
}
/* ---------------------------------------------------------------- */
/* Responses output -> Anthropic blocks */
/* ---------------------------------------------------------------- */
interface ResponseBody {
status?: string
incomplete_details?: { reason?: string }
error?: { message?: string } | null
output?: {
type: string
call_id?: string
id?: string
name?: string
arguments?: string
content?: { type: string; text?: string; refusal?: string }[]
/** image_generation_call items: the finished image, base64 */
result?: string | null
status?: string
}[]
}
function fromResponse(body: ResponseBody): TurnResult {
const content: TurnBlock[] = []
let refused = false
let toolCalls = 0
for (const item of body.output ?? []) {
if (item.type === 'message') {
for (const part of item.content ?? []) {
if (part.type === 'output_text' && part.text) content.push({ type: 'text', text: part.text })
else if (part.type === 'refusal') refused = true
}
} else if (item.type === 'function_call') {
toolCalls++
let input: unknown
try {
input = item.arguments ? JSON.parse(item.arguments) : {}
} catch {
/* a malformed argument blob becomes an empty call; the tool's own
validation then returns a usable error to the model */
input = {}
}
content.push({
type: 'tool_use',
id: item.call_id || item.id || `call_${randomUUID()}`,
name: item.name || 'unknown',
input: input as Record<string, unknown>,
})
}
}
const truncated = body.status === 'incomplete' && body.incomplete_details?.reason === 'max_output_tokens'
const stop: StopReason = refused ? 'refusal' : toolCalls > 0 ? 'tool_use' : truncated ? 'max_tokens' : 'end_turn'
/* a response with nothing in it would end the loop silently; make it a
visible failure instead */
if (content.length === 0 && !refused) throw new Error('OpenAI returned an empty response')
return { content, stop_reason: stop }
}
/* ---------------------------------------------------------------- */
/* transports */
/* ---------------------------------------------------------------- */
/**
* Pull the final response out of a Responses SSE stream.
*
* The output items are accumulated from `response.output_item.done` rather
* than read off the terminal event: ChatGPT's Codex backend sends a
* `response.completed` that carries only bookkeeping (id, usage, end_turn)
* and no output array, so trusting that event alone yields an empty turn.
* api.openai.com does include the array, and it wins when present.
*/
export async function readEventStream(res: Response): Promise<ResponseBody> {
if (!res.body) throw new Error('OpenAI returned no response body')
const reader = res.body.getReader()
const decoder = new TextDecoder()
let buffer = ''
const streamed: NonNullable<ResponseBody['output']> = []
let final: ResponseBody | undefined
let failure: string | undefined
for (;;) {
const { done, value } = await reader.read()
if (done) break
buffer += decoder.decode(value, { stream: true })
let cut = buffer.indexOf('\n')
for (; cut >= 0; cut = buffer.indexOf('\n')) {
const line = buffer.slice(0, cut).trim()
buffer = buffer.slice(cut + 1)
if (!line.startsWith('data:')) continue
const payload = line.slice(5).trim()
if (!payload || payload === '[DONE]') continue
let event: {
type?: string
response?: ResponseBody
item?: NonNullable<ResponseBody['output']>[number]
error?: { message?: string }
message?: string
}
try {
event = JSON.parse(payload)
} catch {
continue
}
if (event.type === 'response.output_item.done' && event.item) streamed.push(event.item)
else if (event.type === 'response.completed' || event.type === 'response.incomplete') final = event.response
else if (event.type === 'response.failed') failure = event.response?.error?.message || 'the response failed'
else if (event.type === 'error') failure = event.error?.message || event.message || 'stream error'
}
}
if (failure) throw isModelUnavailable(failure) ? new ModelUnavailableError(failure) : new Error(failure)
if (!final) throw new Error('OpenAI stream ended without a completed response')
return final.output?.length ? final : { ...final, output: streamed }
}
export async function responseError(res: Response, label: string): Promise<Error> {
const text = await res.text().catch(() => '')
const detail = text.slice(0, 400)
if (res.status === 401 || res.status === 403) {
return new ModelAuthError(`${label} rejected the credentials (${res.status}) — reconnect the account`)
}
if (res.status === 429) {
return new Error(`${label} rate-limited this account (429). ${detail}`)
}
if ((res.status === 400 || res.status === 404) && isModelUnavailable(detail)) {
return new ModelUnavailableError(
`${label} cannot run this model on the connected account (${res.status}). ${detail}`,
)
}
return new Error(`${label} request failed (${res.status}). ${detail}`)
}
/** One turn against a connected ChatGPT subscription. */
async function runChatgpt(account: ModelAccount, req: TurnRequest): Promise<TurnResult> {
const res = await fetch(CHATGPT_URL, {
method: 'POST',
headers: {
Authorization: `Bearer ${account.accessToken}`,
'Content-Type': 'application/json',
Accept: 'text/event-stream',
'OpenAI-Beta': 'responses=experimental',
originator: ORIGINATOR,
session_id: randomUUID(),
...(account.accountId ? { 'chatgpt-account-id': account.accountId } : {}),
},
body: JSON.stringify({
model: modelFor(account),
instructions: req.system,
input: toInput(req.messages),
tools: toTools(req.tools),
tool_choice: 'auto',
parallel_tool_calls: false,
reasoning: { effort: REASONING_EFFORT, summary: 'auto' },
store: false,
stream: true,
}),
})
if (!res.ok) throw await responseError(res, 'ChatGPT')
return fromResponse(await readEventStream(res))
}
/** One turn against a plain OpenAI API key. */
async function runApiKey(account: ModelAccount, req: TurnRequest): Promise<TurnResult> {
const res = await fetch(OPENAI_URL, {
method: 'POST',
headers: { Authorization: `Bearer ${account.apiKey}`, 'Content-Type': 'application/json' },
body: JSON.stringify({
model: modelFor(account),
instructions: req.system,
input: toInput(req.messages),
tools: toTools(req.tools),
tool_choice: 'auto',
parallel_tool_calls: false,
reasoning: { effort: REASONING_EFFORT },
max_output_tokens: req.maxTokens,
store: false,
}),
})
if (!res.ok) throw await responseError(res, 'OpenAI')
return fromResponse((await res.json()) as ResponseBody)
}
/** Everything one Azure OpenAI call needs, straight from the AZURE_OPENAI_*
* env vars — server config, never user input. */
export interface AzureConfig {
/** the resource base (https://….openai.azure.com) or a full …/responses URL */
endpoint: string
deployment: string
apiKey: string
}
/** The Responses URL for an Azure resource. A bare endpoint gets the v1
* path appended; an endpoint that already names …/responses (an APIM
* front door, say) is used as given. */
export function azureResponsesUrl(endpoint: string): string {
const base = endpoint.replace(/\/+$/, '')
const url = /\/responses$/.test(new URL(base).pathname) ? base : `${base}/openai/v1/responses`
return AZURE_API_VERSION ? `${url}?api-version=${encodeURIComponent(AZURE_API_VERSION)}` : url
}
/** One turn against an Azure OpenAI deployment. */
export async function runAzureTurn(config: AzureConfig, req: TurnRequest): Promise<TurnResult> {
const res = await fetch(azureResponsesUrl(config.endpoint), {
method: 'POST',
headers: { 'api-key': config.apiKey, 'Content-Type': 'application/json' },
body: JSON.stringify({
model: config.deployment,
instructions: req.system,
input: toInput(req.messages),
tools: toTools(req.tools),
tool_choice: 'auto',
parallel_tool_calls: false,
...(AZURE_REASONING_EFFORT ? { reasoning: { effort: AZURE_REASONING_EFFORT } } : {}),
max_output_tokens: req.maxTokens,
store: false,
}),
})
if (!res.ok) throw await responseError(res, 'Azure OpenAI')
return fromResponse((await res.json()) as ResponseBody)
}
const transports: Record<
Exclude<ModelAccount['kind'], 'anthropic-key' | 'openrouter-key' | 'gemini-key'>,
(account: ModelAccount, req: TurnRequest) => Promise<TurnResult>
> = {
chatgpt: runChatgpt,
'openai-key': runApiKey,
}
export function runOpenAiTurn(account: ModelAccount, req: TurnRequest): Promise<TurnResult> {
if (account.kind === 'anthropic-key' || account.kind === 'openrouter-key' || account.kind === 'gemini-key') {
throw new Error(`${account.kind} accounts do not use the OpenAI transport`)
}
return transports[account.kind](account, req)
}
/* exported for tests */
export const _internal = { toInput, toTools, fromResponse, flattenToolResult, readEventStream }