Run the agent loop on every provider, not just API keys
The loop was gated to API-key providers, so a GPT-account (Codex) CEO failed with 'requires an API-key provider'. Port Pi's actual request handlers natively so every agent-capable provider runs: - chat-completions turn for xAI, Groq, Mistral, Cerebras, OpenRouter - Codex SSE Responses for GPT account - Anthropic-OAuth (Claude Code system + beta headers) for Claude Pro/Max - Copilot proxy (baseUrl from the token) for GitHub Copilot - Gemini Code Assist for Google Antigravity A credential resolver refreshes expired OAuth access tokens at request time and persists the rotation; the runner routes by provider key and passes the resolved credential. The heartbeat now retries failed tasks (bounded to 3 attempts) so a run that failed on the old gate recovers automatically. Cloud providers (Azure/Bedrock/Vertex) still report a clear unsupported message.
This commit is contained in:
1 parent
61f2162236
commit
937fdfa23f
16 files changed
+876
-129
No files matched your search
@@ -0,0 +1,38 @@
|
||||
/** Anthropic Messages API encoding/decoding, shared by anthropic + claude-pro. */
|
||||
import { isRecord, type JsonRecord } from "../provider-http";
|
||||
import { type AgentTurnMessage, type ToolCall, type ToolDefinition } from "../provider-tool-turn";
|
||||
|
||||
export function anthropicToolDefs(tools: ToolDefinition[]): unknown[] {
|
||||
return tools.map((tool) => ({ name: tool.name, description: tool.description, input_schema: tool.parameters }));
|
||||
}
|
||||
|
||||
export function toAnthropicMessages(messages: AgentTurnMessage[]): unknown[] {
|
||||
const out: unknown[] = [];
|
||||
for (const message of messages) {
|
||||
if (message.role === "user") {
|
||||
out.push({ role: "user", content: [{ type: "text", text: message.text }] });
|
||||
} else if (message.role === "assistant") {
|
||||
const content: unknown[] = [];
|
||||
if (message.text) content.push({ type: "text", text: message.text });
|
||||
for (const call of message.toolCalls) content.push({ type: "tool_use", id: call.id, name: call.name, input: call.arguments });
|
||||
out.push({ role: "assistant", content });
|
||||
} else {
|
||||
out.push({ role: "user", content: message.results.map((result) => ({ type: "tool_result", tool_use_id: result.toolCallId, content: result.output })) });
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function parseAnthropicContent(data: JsonRecord): { text: string; toolCalls: ToolCall[] } {
|
||||
const content = Array.isArray(data.content) ? data.content : [];
|
||||
const textParts: string[] = [];
|
||||
const toolCalls: ToolCall[] = [];
|
||||
for (const block of content) {
|
||||
if (!isRecord(block)) continue;
|
||||
if (block.type === "text" && typeof block.text === "string") textParts.push(block.text);
|
||||
else if (block.type === "tool_use" && typeof block.id === "string" && typeof block.name === "string") {
|
||||
toolCalls.push({ id: block.id, name: block.name, arguments: isRecord(block.input) ? block.input : {} });
|
||||
}
|
||||
}
|
||||
return { text: textParts.join("\n").trim(), toolCalls };
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/** OpenAI Chat Completions encoding/decoding (xAI, Groq, Mistral, Cerebras, OpenRouter). */
|
||||
import { isRecord, type JsonRecord } from "../provider-http";
|
||||
import { parseArguments, type AgentTurnMessage, type ToolCall, type ToolDefinition } from "../provider-tool-turn";
|
||||
|
||||
export function chatToolDefs(tools: ToolDefinition[]): unknown[] {
|
||||
return tools.map((tool) => ({ type: "function", function: { name: tool.name, description: tool.description, parameters: tool.parameters } }));
|
||||
}
|
||||
|
||||
export function toChatMessages(instructions: string | null, messages: AgentTurnMessage[]): unknown[] {
|
||||
const out: unknown[] = [];
|
||||
if (instructions) out.push({ role: "system", content: instructions });
|
||||
for (const message of messages) {
|
||||
if (message.role === "user") {
|
||||
out.push({ role: "user", content: message.text });
|
||||
} else if (message.role === "assistant") {
|
||||
const entry: JsonRecord = { role: "assistant", content: message.text || null };
|
||||
if (message.toolCalls.length > 0) {
|
||||
entry.tool_calls = message.toolCalls.map((call) => ({ id: call.id, type: "function", function: { name: call.name, arguments: JSON.stringify(call.arguments) } }));
|
||||
}
|
||||
out.push(entry);
|
||||
} else {
|
||||
for (const result of message.results) {
|
||||
out.push({ role: "tool", tool_call_id: result.toolCallId, content: result.output });
|
||||
}
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function parseChatChoice(data: JsonRecord): { text: string; toolCalls: ToolCall[] } {
|
||||
const choices = Array.isArray(data.choices) ? data.choices : [];
|
||||
const first = choices.length > 0 && isRecord(choices[0]) ? choices[0] : null;
|
||||
const message = first && isRecord(first.message) ? first.message : null;
|
||||
if (!message) return { text: "", toolCalls: [] };
|
||||
const text = typeof message.content === "string" ? message.content : "";
|
||||
const rawCalls = Array.isArray(message.tool_calls) ? message.tool_calls : [];
|
||||
const toolCalls: ToolCall[] = [];
|
||||
for (const call of rawCalls) {
|
||||
if (!isRecord(call)) continue;
|
||||
const fn = isRecord(call.function) ? call.function : null;
|
||||
const id = typeof call.id === "string" ? call.id : "";
|
||||
const name = fn && typeof fn.name === "string" ? fn.name : "";
|
||||
if (id && name) toolCalls.push({ id, name, arguments: parseArguments(fn?.arguments) });
|
||||
}
|
||||
return { text: text.trim(), toolCalls };
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* Resolves the per-request credential for a tool-use turn. API-key providers
|
||||
* pass the key straight through; OAuth/subscription providers parse the stored
|
||||
* credential JSON, refresh the access token when it has expired (persisting the
|
||||
* rotated token), and hand the turn a usable access token plus the extras
|
||||
* (accountId / projectId / enterpriseUrl) its backend needs.
|
||||
*/
|
||||
import { PROVIDER_AUTH_TYPE } from "../../../shared/domain";
|
||||
import type { AuthBrokerService } from "../auth-broker-service";
|
||||
import { loadCredential, type ProviderRow } from "../provider-internal";
|
||||
import { parseApiKey, requireApiKeyProfile } from "../provider-auth-profiles";
|
||||
import type { TurnCredential } from "../provider-tool-turn";
|
||||
import type { OAuthCredentials } from "../oauth/oauth-types";
|
||||
import { refreshCodex } from "../oauth/openai-codex-flow";
|
||||
import { refreshClaudePro } from "../oauth/anthropic-claude-flow";
|
||||
import { refreshAntigravity } from "../oauth/google-antigravity-flow";
|
||||
import { refreshGitHubCopilotToken } from "../oauth/github-copilot-flow";
|
||||
|
||||
interface StoredOAuth {
|
||||
access: string;
|
||||
refresh: string;
|
||||
expires: number;
|
||||
accountId?: string;
|
||||
projectId?: string;
|
||||
enterpriseUrl?: string | null;
|
||||
}
|
||||
|
||||
function parseStoredOAuth(secret: string): StoredOAuth {
|
||||
let parsed: unknown;
|
||||
try {
|
||||
parsed = JSON.parse(secret);
|
||||
} catch {
|
||||
throw new Error("Stored OAuth credential is not valid JSON; reconnect the provider.");
|
||||
}
|
||||
const creds = parsed as StoredOAuth;
|
||||
if (typeof creds.access !== "string" || typeof creds.refresh !== "string" || typeof creds.expires !== "number") {
|
||||
throw new Error("Stored OAuth credential is incomplete; reconnect the provider.");
|
||||
}
|
||||
return creds;
|
||||
}
|
||||
|
||||
function refreshOAuth(providerKey: string, creds: StoredOAuth): Promise<OAuthCredentials> {
|
||||
switch (providerKey) {
|
||||
case "gpt-account":
|
||||
return refreshCodex(creds.refresh);
|
||||
case "claude-pro":
|
||||
return refreshClaudePro(creds.refresh);
|
||||
case "google-antigravity":
|
||||
if (!creds.projectId) throw new Error("Antigravity credential is missing its project id; reconnect the provider.");
|
||||
return refreshAntigravity(creds.refresh, creds.projectId);
|
||||
case "github-copilot":
|
||||
return refreshGitHubCopilotToken(creds.refresh, creds.enterpriseUrl ?? undefined);
|
||||
default:
|
||||
throw new Error(`${providerKey} cannot refresh its credential.`);
|
||||
}
|
||||
}
|
||||
|
||||
function toTurnCredential(creds: StoredOAuth | OAuthCredentials): TurnCredential {
|
||||
const enterpriseUrl = creds.enterpriseUrl;
|
||||
return {
|
||||
accessToken: creds.access,
|
||||
accountId: typeof creds.accountId === "string" ? creds.accountId : undefined,
|
||||
projectId: typeof creds.projectId === "string" ? creds.projectId : undefined,
|
||||
enterpriseUrl: typeof enterpriseUrl === "string" ? enterpriseUrl : enterpriseUrl === null ? null : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
export async function resolveTurnCredential(deps: {
|
||||
authBroker: AuthBrokerService;
|
||||
orgId: string;
|
||||
provider: ProviderRow;
|
||||
}): Promise<TurnCredential> {
|
||||
const secret = loadCredential(deps.authBroker, deps.orgId, deps.provider).secret;
|
||||
if (deps.provider.auth_type === PROVIDER_AUTH_TYPE.API_KEY) {
|
||||
return { apiKey: requireApiKeyProfile(parseApiKey(secret), deps.provider.provider_key).apiKey };
|
||||
}
|
||||
const creds = parseStoredOAuth(secret);
|
||||
if (Date.now() < creds.expires) return toTurnCredential(creds);
|
||||
if (!deps.provider.credential_id) throw new Error("Provider credential id is missing; reconnect the provider.");
|
||||
const refreshed = await refreshOAuth(deps.provider.provider_key, creds);
|
||||
deps.authBroker.updateProviderSecret({
|
||||
orgId: deps.orgId,
|
||||
providerKey: deps.provider.provider_key,
|
||||
credentialId: deps.provider.credential_id,
|
||||
secret: JSON.stringify(refreshed),
|
||||
});
|
||||
return toTurnCredential(refreshed);
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
/**
|
||||
* Google Cloud Code Assist (Gemini) encoding for the Antigravity backend.
|
||||
* Ported from Pi's google-shared `convertMessages` / `convertTools` and the
|
||||
* Antigravity request wrapper in google-gemini-cli.
|
||||
*/
|
||||
import { isRecord, type JsonRecord, type ProviderEventStreamMessage } from "../provider-http";
|
||||
import { type AgentTurnMessage, type AgentTurnResult, type ToolCall, type ToolDefinition } from "../provider-tool-turn";
|
||||
|
||||
const ANTIGRAVITY_SYSTEM_INSTRUCTION =
|
||||
"You are Antigravity, a powerful agentic AI coding assistant designed by the Google Deepmind team working on Advanced Agentic Coding." +
|
||||
"You are pair programming with a USER to solve their coding task. The task may require creating a new codebase, modifying or debugging an existing codebase, or simply answering a question." +
|
||||
"**Absolute paths only**" +
|
||||
"**Proactiveness**";
|
||||
|
||||
function usesToolCallId(model: string): boolean {
|
||||
return model.startsWith("claude-") || model.startsWith("gpt-oss-");
|
||||
}
|
||||
|
||||
function geminiToolDefs(model: string, tools: ToolDefinition[]): unknown[] {
|
||||
const useParameters = model.startsWith("claude-");
|
||||
return [
|
||||
{
|
||||
functionDeclarations: tools.map((tool) => ({
|
||||
name: tool.name,
|
||||
description: tool.description,
|
||||
...(useParameters ? { parameters: tool.parameters } : { parametersJsonSchema: tool.parameters }),
|
||||
})),
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
function toGeminiContents(model: string, messages: AgentTurnMessage[]): JsonRecord[] {
|
||||
const withId = usesToolCallId(model);
|
||||
const contents: JsonRecord[] = [];
|
||||
for (const message of messages) {
|
||||
if (message.role === "user") {
|
||||
contents.push({ role: "user", parts: [{ text: message.text }] });
|
||||
} else if (message.role === "assistant") {
|
||||
const parts: unknown[] = [];
|
||||
if (message.text) parts.push({ text: message.text });
|
||||
for (const call of message.toolCalls) {
|
||||
parts.push({ functionCall: { name: call.name, args: call.arguments, ...(withId ? { id: call.id } : {}) } });
|
||||
}
|
||||
if (parts.length > 0) contents.push({ role: "model", parts });
|
||||
} else {
|
||||
const responses = message.results.map((result) => ({
|
||||
functionResponse: { name: result.toolName, response: { output: result.output }, ...(withId ? { id: result.toolCallId } : {}) },
|
||||
}));
|
||||
const last = contents[contents.length - 1];
|
||||
if (last && last.role === "user" && Array.isArray(last.parts) && last.parts.some((part) => isRecord(part) && "functionResponse" in part)) {
|
||||
(last.parts as unknown[]).push(...responses);
|
||||
} else {
|
||||
contents.push({ role: "user", parts: responses });
|
||||
}
|
||||
}
|
||||
}
|
||||
return contents;
|
||||
}
|
||||
|
||||
/** The full Antigravity request envelope ({project, model, request, ...}). */
|
||||
export function antigravityRequestBody(input: {
|
||||
model: string;
|
||||
projectId: string;
|
||||
instructions: string | null;
|
||||
messages: AgentTurnMessage[];
|
||||
tools: ToolDefinition[];
|
||||
requestId: string;
|
||||
}): JsonRecord {
|
||||
const request: JsonRecord = {
|
||||
contents: toGeminiContents(input.model, input.messages),
|
||||
systemInstruction: {
|
||||
role: "user",
|
||||
parts: [
|
||||
{ text: ANTIGRAVITY_SYSTEM_INSTRUCTION },
|
||||
{ text: `Please ignore following [ignore]${ANTIGRAVITY_SYSTEM_INSTRUCTION}[/ignore]` },
|
||||
...(input.instructions ? [{ text: input.instructions }] : []),
|
||||
],
|
||||
},
|
||||
};
|
||||
if (input.tools.length > 0) {
|
||||
request.tools = geminiToolDefs(input.model, input.tools);
|
||||
request.toolConfig = { functionCallingConfig: { mode: "AUTO" } };
|
||||
}
|
||||
return { project: input.projectId, model: input.model, request, requestType: "agent", userAgent: "antigravity", requestId: input.requestId };
|
||||
}
|
||||
|
||||
export async function accumulateGeminiStream(messages: AsyncIterable<ProviderEventStreamMessage>): Promise<AgentTurnResult> {
|
||||
const raw: string[] = [];
|
||||
const textParts: string[] = [];
|
||||
const toolCalls: ToolCall[] = [];
|
||||
let counter = 0;
|
||||
for await (const message of messages) {
|
||||
raw.push(message.rawData);
|
||||
if (isRecord(message.data.error)) {
|
||||
const error = message.data.error;
|
||||
throw new Error(typeof error.message === "string" ? error.message : "Antigravity stream failed.");
|
||||
}
|
||||
const response = isRecord(message.data.response) ? message.data.response : null;
|
||||
if (!response) continue;
|
||||
const candidates = Array.isArray(response.candidates) ? response.candidates : [];
|
||||
const content = candidates.length > 0 && isRecord(candidates[0]) && isRecord(candidates[0].content) ? candidates[0].content : null;
|
||||
const parts = content && Array.isArray(content.parts) ? content.parts : [];
|
||||
for (const part of parts) {
|
||||
if (!isRecord(part)) continue;
|
||||
if (typeof part.text === "string" && part.thought !== true) {
|
||||
textParts.push(part.text);
|
||||
} else if (isRecord(part.functionCall)) {
|
||||
const fc = part.functionCall;
|
||||
const name = typeof fc.name === "string" ? fc.name : "";
|
||||
if (!name) continue;
|
||||
const id = typeof fc.id === "string" && fc.id ? fc.id : `${name}_${(counter += 1)}`;
|
||||
toolCalls.push({ id, name, arguments: isRecord(fc.args) ? fc.args : {} });
|
||||
}
|
||||
}
|
||||
}
|
||||
return { text: textParts.join("").trim(), toolCalls, rawResponseJson: JSON.stringify(raw) };
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* OpenAI Responses API encoding/decoding, shared by the openai, gpt-account
|
||||
* (Codex) and github-copilot turns. Matches Pi's `convertResponsesMessages` /
|
||||
* `convertResponsesTools` and the Responses streaming event shapes.
|
||||
*/
|
||||
import { isRecord, type JsonRecord, type ProviderEventStreamMessage } from "../provider-http";
|
||||
import { parseArguments, type AgentTurnMessage, type AgentTurnResult, type ToolCall, type ToolDefinition } from "../provider-tool-turn";
|
||||
|
||||
export function responsesToolDefs(tools: ToolDefinition[], strict: boolean | null = false): unknown[] {
|
||||
return tools.map((tool) => ({ type: "function", name: tool.name, description: tool.description, parameters: tool.parameters, strict }));
|
||||
}
|
||||
|
||||
export function toResponsesInput(messages: AgentTurnMessage[]): unknown[] {
|
||||
const items: unknown[] = [];
|
||||
for (const message of messages) {
|
||||
if (message.role === "user") {
|
||||
items.push({ role: "user", content: [{ type: "input_text", text: message.text }] });
|
||||
} else if (message.role === "assistant") {
|
||||
if (message.text) {
|
||||
items.push({ type: "message", role: "assistant", content: [{ type: "output_text", text: message.text, annotations: [] }] });
|
||||
}
|
||||
for (const call of message.toolCalls) {
|
||||
items.push({ type: "function_call", call_id: call.id, name: call.name, arguments: JSON.stringify(call.arguments) });
|
||||
}
|
||||
} else {
|
||||
for (const result of message.results) {
|
||||
items.push({ type: "function_call_output", call_id: result.toolCallId, output: result.output });
|
||||
}
|
||||
}
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
/** Parse a non-streaming Responses payload (`data.output[]`). */
|
||||
export function parseResponsesOutput(data: JsonRecord): { text: string; toolCalls: ToolCall[] } {
|
||||
const output = Array.isArray(data.output) ? data.output : [];
|
||||
const textParts: string[] = [];
|
||||
const toolCalls: ToolCall[] = [];
|
||||
for (const item of output) {
|
||||
if (!isRecord(item)) continue;
|
||||
if (item.type === "function_call") {
|
||||
const id = typeof item.call_id === "string" ? item.call_id : typeof item.id === "string" ? item.id : "";
|
||||
const name = typeof item.name === "string" ? item.name : "";
|
||||
if (id && name) toolCalls.push({ id, name, arguments: parseArguments(item.arguments) });
|
||||
} else if (item.type === "message" && Array.isArray(item.content)) {
|
||||
for (const block of item.content) {
|
||||
if (isRecord(block) && block.type === "output_text" && typeof block.text === "string") textParts.push(block.text);
|
||||
}
|
||||
}
|
||||
}
|
||||
return { text: textParts.join("\n").trim(), toolCalls };
|
||||
}
|
||||
|
||||
function eventType(message: ProviderEventStreamMessage): string | null {
|
||||
const type = message.data.type;
|
||||
return typeof type === "string" ? type : message.eventName;
|
||||
}
|
||||
|
||||
function errorText(data: JsonRecord): string {
|
||||
const error = data.error;
|
||||
if (typeof error === "string") return error;
|
||||
if (isRecord(error) && typeof error.message === "string") return error.message;
|
||||
if (typeof data.message === "string") return data.message;
|
||||
return "Provider stream failed.";
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulate a Responses SSE stream (Codex/Copilot) into the final result. The
|
||||
* authoritative output arrives on the completion event's `response.output`; the
|
||||
* incremental events are accumulated only as a fallback.
|
||||
*/
|
||||
export async function accumulateResponsesStream(messages: AsyncIterable<ProviderEventStreamMessage>): Promise<AgentTurnResult> {
|
||||
const raw: string[] = [];
|
||||
const textParts: string[] = [];
|
||||
const calls = new Map<string, { id: string; name: string; args: string }>();
|
||||
for await (const message of messages) {
|
||||
raw.push(message.rawData);
|
||||
const type = eventType(message);
|
||||
if (!type) continue;
|
||||
if (type === "error" || type === "response.failed") throw new Error(errorText(message.data));
|
||||
if (type === "response.completed" || type === "response.done" || type === "response.incomplete") {
|
||||
const response = isRecord(message.data.response) ? message.data.response : null;
|
||||
if (response && Array.isArray(response.output)) {
|
||||
const parsed = parseResponsesOutput(response);
|
||||
return { text: parsed.text, toolCalls: parsed.toolCalls, rawResponseJson: JSON.stringify(raw) };
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (type === "response.output_text.delta" && typeof message.data.delta === "string") {
|
||||
textParts.push(message.data.delta);
|
||||
} else if (type === "response.output_item.added" && isRecord(message.data.item) && message.data.item.type === "function_call") {
|
||||
const item = message.data.item;
|
||||
const key = typeof item.id === "string" ? item.id : typeof item.call_id === "string" ? item.call_id : "";
|
||||
const id = typeof item.call_id === "string" ? item.call_id : key;
|
||||
if (key) calls.set(key, { id, name: typeof item.name === "string" ? item.name : "", args: typeof item.arguments === "string" ? item.arguments : "" });
|
||||
} else if (type === "response.function_call_arguments.delta" && typeof message.data.item_id === "string" && typeof message.data.delta === "string") {
|
||||
const entry = calls.get(message.data.item_id);
|
||||
if (entry) entry.args += message.data.delta;
|
||||
} else if (type === "response.function_call_arguments.done" && typeof message.data.item_id === "string" && typeof message.data.arguments === "string") {
|
||||
const entry = calls.get(message.data.item_id);
|
||||
if (entry) entry.args = message.data.arguments;
|
||||
}
|
||||
}
|
||||
const toolCalls = [...calls.values()].filter((entry) => entry.name).map((entry) => ({ id: entry.id, name: entry.name, arguments: parseArguments(entry.args) }));
|
||||
return { text: textParts.join("").trim(), toolCalls, rawResponseJson: JSON.stringify(raw) };
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import type { ToolTurnFn } from "../provider-tool-turn";
|
||||
import {
|
||||
makeChatCompletionsTurn,
|
||||
runAnthropicTurn,
|
||||
runAntigravityTurn,
|
||||
runClaudeProTurn,
|
||||
runCodexTurn,
|
||||
runCopilotTurn,
|
||||
runOpenAiResponsesTurn,
|
||||
} from "./turns";
|
||||
|
||||
/** Provider key -> the tool-use turn function that drives the agent loop. */
|
||||
const TURNS: Record<string, ToolTurnFn> = {
|
||||
openai: runOpenAiResponsesTurn,
|
||||
anthropic: runAnthropicTurn,
|
||||
xai: makeChatCompletionsTurn("https://api.x.ai/v1/chat/completions", "xAI"),
|
||||
groq: makeChatCompletionsTurn("https://api.groq.com/openai/v1/chat/completions", "Groq"),
|
||||
mistral: makeChatCompletionsTurn("https://api.mistral.ai/v1/chat/completions", "Mistral"),
|
||||
cerebras: makeChatCompletionsTurn("https://api.cerebras.ai/v1/chat/completions", "Cerebras"),
|
||||
openrouter: makeChatCompletionsTurn("https://openrouter.ai/api/v1/chat/completions", "OpenRouter"),
|
||||
"gpt-account": runCodexTurn,
|
||||
"github-copilot": runCopilotTurn,
|
||||
"google-antigravity": runAntigravityTurn,
|
||||
"claude-pro": runClaudeProTurn,
|
||||
};
|
||||
|
||||
export function supportsToolTurn(providerKey: string): boolean {
|
||||
return providerKey in TURNS;
|
||||
}
|
||||
|
||||
export function selectToolTurn(providerKey: string): ToolTurnFn {
|
||||
const turn = TURNS[providerKey];
|
||||
if (!turn) throw new Error(`${providerKey} does not support agent execution.`);
|
||||
return turn;
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
/**
|
||||
* Per-backend tool-use turn functions. Each takes the normalized
|
||||
* {@link ProviderToolTurnInput} (with a resolved credential) and returns an
|
||||
* {@link AgentTurnResult}. Faithful ports of Pi's provider request handlers.
|
||||
*/
|
||||
import { release as osRelease, arch as osArch, platform as osPlatform } from "node:os";
|
||||
import { postEventStream, postJson, type JsonRecord } from "../provider-http";
|
||||
import type { AgentTurnMessage, AgentTurnResult, ProviderToolTurnInput, TurnCredential } from "../provider-tool-turn";
|
||||
import { accumulateResponsesStream, parseResponsesOutput, responsesToolDefs, toResponsesInput } from "./responses-format";
|
||||
import { anthropicToolDefs, parseAnthropicContent, toAnthropicMessages } from "./anthropic-format";
|
||||
import { chatToolDefs, parseChatChoice, toChatMessages } from "./chat-format";
|
||||
import { accumulateGeminiStream, antigravityRequestBody } from "./gemini-format";
|
||||
import { COPILOT_HEADERS, copilotBaseUrl } from "../oauth/github-copilot-flow";
|
||||
|
||||
const ANTIGRAVITY_VERSION = "1.21.9";
|
||||
const ANTIGRAVITY_ENDPOINT = "https://cloudcode-pa.googleapis.com";
|
||||
const CODEX_BASE_URL = "https://chatgpt.com/backend-api";
|
||||
|
||||
function requireApiKey(credential: TurnCredential, label: string): string {
|
||||
if (!credential.apiKey) throw new Error(`${label} requires an API key.`);
|
||||
return credential.apiKey;
|
||||
}
|
||||
|
||||
function requireAccessToken(credential: TurnCredential, label: string): string {
|
||||
if (!credential.accessToken) throw new Error(`${label} requires a signed-in access token.`);
|
||||
return credential.accessToken;
|
||||
}
|
||||
|
||||
function lastMessageIsUser(messages: AgentTurnMessage[]): boolean {
|
||||
const last = messages[messages.length - 1];
|
||||
return last?.role === "user";
|
||||
}
|
||||
|
||||
// ---- OpenAI Responses (api key) ----
|
||||
|
||||
export async function runOpenAiResponsesTurn(input: ProviderToolTurnInput): Promise<AgentTurnResult> {
|
||||
const body: JsonRecord = {
|
||||
model: input.model,
|
||||
input: toResponsesInput(input.messages),
|
||||
tools: responsesToolDefs(input.tools),
|
||||
tool_choice: "auto",
|
||||
store: false,
|
||||
...(input.instructions ? { instructions: input.instructions } : {}),
|
||||
};
|
||||
const response = await postJson("https://api.openai.com/v1/responses", { authorization: `Bearer ${requireApiKey(input.credential, "OpenAI")}` }, body);
|
||||
const parsed = parseResponsesOutput(response.data);
|
||||
return { ...parsed, rawResponseJson: JSON.stringify(response.data) };
|
||||
}
|
||||
|
||||
// ---- OpenAI Chat Completions (xAI, Groq, Mistral, Cerebras, OpenRouter) ----
|
||||
|
||||
export function makeChatCompletionsTurn(endpoint: string, label: string): (input: ProviderToolTurnInput) => Promise<AgentTurnResult> {
|
||||
return async (input) => {
|
||||
const body: JsonRecord = {
|
||||
model: input.model,
|
||||
messages: toChatMessages(input.instructions, input.messages),
|
||||
tools: chatToolDefs(input.tools),
|
||||
tool_choice: "auto",
|
||||
stream: false,
|
||||
};
|
||||
const response = await postJson(endpoint, { authorization: `Bearer ${requireApiKey(input.credential, label)}` }, body);
|
||||
const parsed = parseChatChoice(response.data);
|
||||
return { ...parsed, rawResponseJson: JSON.stringify(response.data) };
|
||||
};
|
||||
}
|
||||
|
||||
// ---- Anthropic Messages (api key) ----
|
||||
|
||||
export async function runAnthropicTurn(input: ProviderToolTurnInput): Promise<AgentTurnResult> {
|
||||
const body: JsonRecord = {
|
||||
model: input.model,
|
||||
max_tokens: 4096,
|
||||
tools: anthropicToolDefs(input.tools),
|
||||
messages: toAnthropicMessages(input.messages),
|
||||
...(input.instructions ? { system: input.instructions } : {}),
|
||||
};
|
||||
const response = await postJson("https://api.anthropic.com/v1/messages", { "x-api-key": requireApiKey(input.credential, "Anthropic"), "anthropic-version": "2023-06-01" }, body);
|
||||
const parsed = parseAnthropicContent(response.data);
|
||||
return { ...parsed, rawResponseJson: JSON.stringify(response.data) };
|
||||
}
|
||||
|
||||
// ---- Claude Pro/Max (OAuth subscription) ----
|
||||
|
||||
export async function runClaudeProTurn(input: ProviderToolTurnInput): Promise<AgentTurnResult> {
|
||||
const system: unknown[] = [
|
||||
{ type: "text", text: "You are Claude Code, Anthropic's official CLI for Claude." },
|
||||
...(input.instructions ? [{ type: "text", text: input.instructions }] : []),
|
||||
];
|
||||
const body: JsonRecord = {
|
||||
model: input.model,
|
||||
max_tokens: 4096,
|
||||
tools: anthropicToolDefs(input.tools),
|
||||
messages: toAnthropicMessages(input.messages),
|
||||
system,
|
||||
};
|
||||
const response = await postJson("https://api.anthropic.com/v1/messages", {
|
||||
authorization: `Bearer ${requireAccessToken(input.credential, "Claude Pro")}`,
|
||||
"anthropic-version": "2023-06-01",
|
||||
"anthropic-beta": "claude-code-20250219,oauth-2025-04-20,fine-grained-tool-streaming-2025-05-14",
|
||||
"anthropic-dangerous-direct-browser-access": "true",
|
||||
"user-agent": "claude-cli/2.1.75",
|
||||
"x-app": "cli",
|
||||
}, body);
|
||||
const parsed = parseAnthropicContent(response.data);
|
||||
return { ...parsed, rawResponseJson: JSON.stringify(response.data) };
|
||||
}
|
||||
|
||||
// ---- GPT account / OpenAI Codex (OAuth, SSE) ----
|
||||
|
||||
export async function runCodexTurn(input: ProviderToolTurnInput): Promise<AgentTurnResult> {
|
||||
const access = requireAccessToken(input.credential, "GPT account");
|
||||
if (!input.credential.accountId) throw new Error("GPT account credential is missing its account id.");
|
||||
const body: JsonRecord = {
|
||||
model: input.model,
|
||||
store: false,
|
||||
stream: true,
|
||||
...(input.instructions ? { instructions: input.instructions } : {}),
|
||||
input: toResponsesInput(input.messages),
|
||||
text: { verbosity: "medium" },
|
||||
include: ["reasoning.encrypted_content"],
|
||||
tool_choice: "auto",
|
||||
parallel_tool_calls: true,
|
||||
tools: responsesToolDefs(input.tools, null),
|
||||
};
|
||||
const response = await postEventStream(`${CODEX_BASE_URL}/codex/responses`, {
|
||||
Authorization: `Bearer ${access}`,
|
||||
"chatgpt-account-id": input.credential.accountId,
|
||||
originator: "agentcompany",
|
||||
"User-Agent": `agentcompany (${osPlatform()} ${osRelease()}; ${osArch()})`,
|
||||
"OpenAI-Beta": "responses=experimental",
|
||||
}, body);
|
||||
return accumulateResponsesStream(response.messages);
|
||||
}
|
||||
|
||||
// ---- GitHub Copilot (OAuth, openai-responses at the proxy) ----
|
||||
|
||||
export async function runCopilotTurn(input: ProviderToolTurnInput): Promise<AgentTurnResult> {
|
||||
const access = requireAccessToken(input.credential, "GitHub Copilot");
|
||||
const baseUrl = copilotBaseUrl(access, input.credential.enterpriseUrl ?? undefined);
|
||||
const body: JsonRecord = {
|
||||
model: input.model,
|
||||
input: toResponsesInput(input.messages),
|
||||
tools: responsesToolDefs(input.tools),
|
||||
tool_choice: "auto",
|
||||
store: false,
|
||||
};
|
||||
const response = await postJson(`${baseUrl}/responses`, {
|
||||
Authorization: `Bearer ${access}`,
|
||||
...COPILOT_HEADERS,
|
||||
"X-Initiator": lastMessageIsUser(input.messages) ? "user" : "agent",
|
||||
"Openai-Intent": "conversation-edits",
|
||||
}, body);
|
||||
const parsed = parseResponsesOutput(response.data);
|
||||
return { ...parsed, rawResponseJson: JSON.stringify(response.data) };
|
||||
}
|
||||
|
||||
// ---- Google Antigravity (OAuth, Gemini Code Assist, SSE) ----
|
||||
|
||||
export async function runAntigravityTurn(input: ProviderToolTurnInput): Promise<AgentTurnResult> {
|
||||
const access = requireAccessToken(input.credential, "Google Antigravity");
|
||||
if (!input.credential.projectId) throw new Error("Antigravity credential is missing its project id.");
|
||||
const requestId = `agent-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
|
||||
const body = antigravityRequestBody({
|
||||
model: input.model,
|
||||
projectId: input.credential.projectId,
|
||||
instructions: input.instructions,
|
||||
messages: input.messages,
|
||||
tools: input.tools,
|
||||
requestId,
|
||||
});
|
||||
const response = await postEventStream(`${ANTIGRAVITY_ENDPOINT}/v1internal:streamGenerateContent?alt=sse`, {
|
||||
Authorization: `Bearer ${access}`,
|
||||
"User-Agent": `antigravity/${ANTIGRAVITY_VERSION} darwin/arm64`,
|
||||
}, body);
|
||||
return accumulateGeminiStream(response.messages);
|
||||
}
|
||||
Reference in new issue
Block a user