Merge pull request #10467 from uinstinct/cli-ai-sdk
feat: integrate ai-sdk provider
This commit is contained in:
commit
8eee3e0300
6 changed files with 408 additions and 18 deletions
|
|
@ -72,6 +72,7 @@ export function createLlmApi(
|
|||
}
|
||||
: {
|
||||
provider: model.provider as any,
|
||||
model: model.model,
|
||||
apiKey: model.apiKey,
|
||||
apiBase: model.apiBase,
|
||||
requestOptions: model.requestOptions,
|
||||
|
|
|
|||
303
packages/openai-adapters/src/apis/AiSdk.ts
Normal file
303
packages/openai-adapters/src/apis/AiSdk.ts
Normal file
|
|
@ -0,0 +1,303 @@
|
|||
import { createOpenAI } from "@ai-sdk/openai";
|
||||
import {
|
||||
ChatCompletion,
|
||||
ChatCompletionChunk,
|
||||
ChatCompletionCreateParamsNonStreaming,
|
||||
ChatCompletionCreateParamsStreaming,
|
||||
Completion,
|
||||
CompletionCreateParamsNonStreaming,
|
||||
CompletionCreateParamsStreaming,
|
||||
CreateEmbeddingResponse,
|
||||
EmbeddingCreateParams,
|
||||
Model,
|
||||
} from "openai/resources/index";
|
||||
import { AiSdkConfig } from "../types.js";
|
||||
import { customFetch, embedding } from "../util.js";
|
||||
import {
|
||||
BaseLlmApi,
|
||||
CreateRerankResponse,
|
||||
FimCreateParamsStreaming,
|
||||
RerankCreateParams,
|
||||
} from "./base.js";
|
||||
|
||||
type AiSdkProviderCreator = (options: {
|
||||
apiKey?: string;
|
||||
baseURL?: string;
|
||||
fetch?: typeof fetch;
|
||||
}) => (modelId: string) => any;
|
||||
|
||||
const PROVIDER_MAP: Record<string, AiSdkProviderCreator> = {
|
||||
openai: createOpenAI,
|
||||
anthropic: (options) =>
|
||||
createOpenAI({
|
||||
...options,
|
||||
baseURL: options.baseURL ?? "https://api.anthropic.com/v1/",
|
||||
}),
|
||||
openrouter: (options) =>
|
||||
createOpenAI({
|
||||
...options,
|
||||
baseURL: options.baseURL ?? "https://openrouter.ai/api/v1/",
|
||||
}),
|
||||
};
|
||||
|
||||
export class AiSdkApi implements BaseLlmApi {
|
||||
private provider?: (modelId: string) => any;
|
||||
private config: AiSdkConfig;
|
||||
private providerId: string;
|
||||
private modelId: string;
|
||||
|
||||
constructor(config: AiSdkConfig) {
|
||||
this.config = config;
|
||||
if (!config.model) {
|
||||
throw new Error(
|
||||
"AI SDK provider requires a model in the format '<provider>/<model>' (e.g., 'openai/gpt-4o')",
|
||||
);
|
||||
}
|
||||
const [providerId, ...modelParts] = config.model.split("/");
|
||||
this.providerId = providerId;
|
||||
this.modelId = modelParts.join("/");
|
||||
}
|
||||
|
||||
private initializeProvider() {
|
||||
if (this.provider) {
|
||||
return;
|
||||
}
|
||||
|
||||
const createFn = PROVIDER_MAP[this.providerId];
|
||||
if (!createFn) {
|
||||
const supportedProviders = Object.keys(PROVIDER_MAP).join(", ");
|
||||
throw new Error(
|
||||
`Unknown AI SDK provider: "${this.providerId}". ` +
|
||||
`Supported providers: ${supportedProviders}. ` +
|
||||
`To use a different provider, install the @ai-sdk/* package and add it to the provider map.`,
|
||||
);
|
||||
}
|
||||
|
||||
const hasRequestOptions =
|
||||
this.config.requestOptions &&
|
||||
(this.config.requestOptions.headers ||
|
||||
this.config.requestOptions.proxy ||
|
||||
this.config.requestOptions.caBundlePath ||
|
||||
this.config.requestOptions.clientCertificate ||
|
||||
this.config.requestOptions.extraBodyProperties);
|
||||
|
||||
this.provider = createFn({
|
||||
apiKey: this.config.apiKey ?? "",
|
||||
baseURL: this.config.apiBase,
|
||||
fetch: hasRequestOptions
|
||||
? customFetch(this.config.requestOptions)
|
||||
: undefined,
|
||||
});
|
||||
}
|
||||
|
||||
async chatCompletionNonStream(
|
||||
body: ChatCompletionCreateParamsNonStreaming,
|
||||
signal: AbortSignal,
|
||||
): Promise<ChatCompletion> {
|
||||
this.initializeProvider();
|
||||
|
||||
const { generateText } = await import("ai");
|
||||
const { convertOpenAIMessagesToVercel } = await import(
|
||||
"../openaiToVercelMessages.js"
|
||||
);
|
||||
const { convertToolsToVercelFormat } = await import(
|
||||
"../convertToolsToVercel.js"
|
||||
);
|
||||
const { convertToolChoiceToVercel } = await import(
|
||||
"../convertToolChoiceToVercel.js"
|
||||
);
|
||||
|
||||
const vercelMessages = convertOpenAIMessagesToVercel(body.messages);
|
||||
const systemMsg = vercelMessages.find((msg) => msg.role === "system");
|
||||
const systemText =
|
||||
systemMsg && typeof systemMsg.content === "string"
|
||||
? systemMsg.content
|
||||
: undefined;
|
||||
const nonSystemMessages = vercelMessages.filter(
|
||||
(msg) => msg.role !== "system",
|
||||
);
|
||||
|
||||
const modelId = this.modelId ?? body.model;
|
||||
const model = this.provider!(modelId);
|
||||
const vercelTools = await convertToolsToVercelFormat(body.tools);
|
||||
|
||||
const result = await generateText({
|
||||
model,
|
||||
system: systemText,
|
||||
messages: nonSystemMessages as any,
|
||||
temperature: body.temperature ?? undefined,
|
||||
maxTokens: body.max_tokens ?? undefined,
|
||||
topP: body.top_p ?? undefined,
|
||||
stopSequences: body.stop
|
||||
? Array.isArray(body.stop)
|
||||
? body.stop
|
||||
: [body.stop]
|
||||
: undefined,
|
||||
tools: vercelTools,
|
||||
toolChoice: convertToolChoiceToVercel(body.tool_choice),
|
||||
abortSignal: signal,
|
||||
});
|
||||
|
||||
const toolCalls = result.toolCalls?.map((tc) => ({
|
||||
id: tc.toolCallId,
|
||||
type: "function" as const,
|
||||
function: {
|
||||
name: tc.toolName,
|
||||
arguments: JSON.stringify(tc.args),
|
||||
},
|
||||
}));
|
||||
|
||||
return {
|
||||
id: result.response?.id ?? "",
|
||||
object: "chat.completion",
|
||||
created: Math.floor(Date.now() / 1000),
|
||||
model: modelId,
|
||||
choices: [
|
||||
{
|
||||
index: 0,
|
||||
message: {
|
||||
role: "assistant",
|
||||
content: result.text,
|
||||
tool_calls: toolCalls,
|
||||
refusal: null,
|
||||
},
|
||||
finish_reason:
|
||||
result.finishReason === "tool-calls" ? "tool_calls" : "stop",
|
||||
logprobs: null,
|
||||
},
|
||||
],
|
||||
usage: {
|
||||
prompt_tokens: result.usage.promptTokens,
|
||||
completion_tokens: result.usage.completionTokens,
|
||||
total_tokens: result.usage.totalTokens,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async *chatCompletionStream(
|
||||
body: ChatCompletionCreateParamsStreaming,
|
||||
signal: AbortSignal,
|
||||
): AsyncGenerator<ChatCompletionChunk> {
|
||||
this.initializeProvider();
|
||||
|
||||
const { streamText } = await import("ai");
|
||||
const { convertOpenAIMessagesToVercel } = await import(
|
||||
"../openaiToVercelMessages.js"
|
||||
);
|
||||
const { convertToolsToVercelFormat } = await import(
|
||||
"../convertToolsToVercel.js"
|
||||
);
|
||||
const { convertVercelStream } = await import("../vercelStreamConverter.js");
|
||||
const { convertToolChoiceToVercel } = await import(
|
||||
"../convertToolChoiceToVercel.js"
|
||||
);
|
||||
|
||||
const vercelMessages = convertOpenAIMessagesToVercel(body.messages);
|
||||
const systemMsg = vercelMessages.find((msg) => msg.role === "system");
|
||||
const systemText =
|
||||
systemMsg && typeof systemMsg.content === "string"
|
||||
? systemMsg.content
|
||||
: undefined;
|
||||
const nonSystemMessages = vercelMessages.filter(
|
||||
(msg) => msg.role !== "system",
|
||||
);
|
||||
|
||||
const modelId = this.modelId ?? body.model;
|
||||
const model = this.provider!(modelId);
|
||||
const vercelTools = await convertToolsToVercelFormat(body.tools);
|
||||
|
||||
const result = streamText({
|
||||
model,
|
||||
system: systemText,
|
||||
messages: nonSystemMessages as any,
|
||||
temperature: body.temperature ?? undefined,
|
||||
maxTokens: body.max_tokens ?? undefined,
|
||||
topP: body.top_p ?? undefined,
|
||||
stopSequences: body.stop
|
||||
? Array.isArray(body.stop)
|
||||
? body.stop
|
||||
: [body.stop]
|
||||
: undefined,
|
||||
tools: vercelTools,
|
||||
toolChoice: convertToolChoiceToVercel(body.tool_choice),
|
||||
abortSignal: signal,
|
||||
});
|
||||
|
||||
yield* convertVercelStream(result.fullStream as any, { model: modelId });
|
||||
}
|
||||
|
||||
async completionNonStream(
|
||||
_body: CompletionCreateParamsNonStreaming,
|
||||
_signal: AbortSignal,
|
||||
): Promise<Completion> {
|
||||
throw new Error(
|
||||
"AI SDK provider does not support legacy completions API. Use chat completions instead.",
|
||||
);
|
||||
}
|
||||
|
||||
async *completionStream(
|
||||
_body: CompletionCreateParamsStreaming,
|
||||
_signal: AbortSignal,
|
||||
): AsyncGenerator<Completion> {
|
||||
throw new Error(
|
||||
"AI SDK provider does not support legacy completions API. Use chat completions instead.",
|
||||
);
|
||||
}
|
||||
|
||||
async *fimStream(
|
||||
_body: FimCreateParamsStreaming,
|
||||
_signal: AbortSignal,
|
||||
): AsyncGenerator<ChatCompletionChunk> {
|
||||
throw new Error(
|
||||
"AI SDK provider does not support fill-in-the-middle (FIM) completions.",
|
||||
);
|
||||
}
|
||||
|
||||
async embed(body: EmbeddingCreateParams): Promise<CreateEmbeddingResponse> {
|
||||
this.initializeProvider();
|
||||
|
||||
const { embed: aiEmbed, embedMany } = await import("ai");
|
||||
|
||||
const modelId = typeof body.model === "string" ? body.model : body.model;
|
||||
const model = this.provider!(modelId);
|
||||
|
||||
const inputs = Array.isArray(body.input) ? body.input : [body.input];
|
||||
|
||||
if (inputs.length === 1) {
|
||||
const result = await aiEmbed({
|
||||
model,
|
||||
value: inputs[0],
|
||||
});
|
||||
return embedding({
|
||||
data: [result.embedding],
|
||||
model: modelId,
|
||||
usage: {
|
||||
prompt_tokens: result.usage?.tokens ?? 0,
|
||||
total_tokens: result.usage?.tokens ?? 0,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const result = await embedMany({
|
||||
model,
|
||||
values: inputs as string[],
|
||||
});
|
||||
|
||||
return embedding({
|
||||
data: result.embeddings,
|
||||
model: modelId,
|
||||
usage: {
|
||||
prompt_tokens: result.usage?.tokens ?? 0,
|
||||
total_tokens: result.usage?.tokens ?? 0,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async rerank(_body: RerankCreateParams): Promise<CreateRerankResponse> {
|
||||
throw new Error("AI SDK provider does not support reranking.");
|
||||
}
|
||||
|
||||
async list(): Promise<Model[]> {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
|
@ -4,6 +4,53 @@
|
|||
|
||||
import type { ChatCompletionCreateParams } from "openai/resources/index.js";
|
||||
|
||||
/**
|
||||
* Recursively transforms schemas to be OpenAI strict mode compatible:
|
||||
* - Adds additionalProperties: false to all object schemas
|
||||
* - Ensures all properties are listed in the required array
|
||||
*/
|
||||
function makeOpenAIStrictCompatible(schema: any): any {
|
||||
if (!schema || typeof schema !== "object") {
|
||||
return schema;
|
||||
}
|
||||
|
||||
const result = { ...schema };
|
||||
|
||||
if (result.type === "object") {
|
||||
result.additionalProperties = false;
|
||||
|
||||
if (result.properties) {
|
||||
const propertyKeys = Object.keys(result.properties);
|
||||
result.required = propertyKeys;
|
||||
|
||||
result.properties = Object.fromEntries(
|
||||
Object.entries(result.properties).map(([key, value]) => [
|
||||
key,
|
||||
makeOpenAIStrictCompatible(value),
|
||||
]),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (result.items) {
|
||||
result.items = makeOpenAIStrictCompatible(result.items);
|
||||
}
|
||||
|
||||
if (result.anyOf) {
|
||||
result.anyOf = result.anyOf.map(makeOpenAIStrictCompatible);
|
||||
}
|
||||
|
||||
if (result.oneOf) {
|
||||
result.oneOf = result.oneOf.map(makeOpenAIStrictCompatible);
|
||||
}
|
||||
|
||||
if (result.allOf) {
|
||||
result.allOf = result.allOf.map(makeOpenAIStrictCompatible);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts OpenAI tool format to Vercel AI SDK format.
|
||||
*
|
||||
|
|
@ -25,11 +72,12 @@ export async function convertToolsToVercelFormat(
|
|||
const vercelTools: Record<string, any> = {};
|
||||
for (const tool of openaiTools) {
|
||||
if (tool.type === "function") {
|
||||
const parameters = makeOpenAIStrictCompatible(
|
||||
tool.function.parameters ?? { type: "object", properties: {} },
|
||||
);
|
||||
vercelTools[tool.function.name] = {
|
||||
description: tool.function.description,
|
||||
parameters: aiJsonSchema(
|
||||
tool.function.parameters ?? { type: "object", properties: {} },
|
||||
),
|
||||
parameters: aiJsonSchema(parameters),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import dotenv from "dotenv";
|
||||
import { z } from "zod";
|
||||
import { AiSdkApi } from "./apis/AiSdk.js";
|
||||
import { AnthropicApi } from "./apis/Anthropic.js";
|
||||
import { AskSageApi } from "./apis/AskSage.js";
|
||||
import { AzureApi } from "./apis/Azure.js";
|
||||
|
|
@ -164,6 +165,8 @@ export function constructLlmApi(config: LLMConfig): BaseLlmApi | undefined {
|
|||
// Return undefined for native HuggingFace endpoints
|
||||
// (handled by HuggingFaceInferenceAPI class in core)
|
||||
return undefined;
|
||||
case "ai-sdk":
|
||||
return new AiSdkApi(config);
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
|
|
@ -182,14 +185,16 @@ export {
|
|||
} from "openai/resources/index";
|
||||
|
||||
// export
|
||||
export { AiSdkApi } from "./apis/AiSdk.js";
|
||||
export type { BaseLlmApi } from "./apis/base.js";
|
||||
export type {
|
||||
LLMConfig,
|
||||
AskSageTool,
|
||||
AskSageToolChoice,
|
||||
AskSageToolCall,
|
||||
AiSdkConfig,
|
||||
AskSageResponse,
|
||||
AskSageTokenResponse,
|
||||
AskSageTool,
|
||||
AskSageToolCall,
|
||||
AskSageToolChoice,
|
||||
LLMConfig,
|
||||
} from "./types.js";
|
||||
|
||||
export {
|
||||
|
|
@ -200,4 +205,4 @@ export {
|
|||
} from "./apis/AnthropicUtils.js";
|
||||
|
||||
export { isResponsesModel } from "./apis/openaiResponses.js";
|
||||
export { parseDataUrl, extractBase64FromDataUrl } from "./util/url.js";
|
||||
export { extractBase64FromDataUrl, parseDataUrl } from "./util/url.js";
|
||||
|
|
|
|||
|
|
@ -13,12 +13,13 @@ export interface VercelCoreMessage {
|
|||
* Converts OpenAI messages to Vercel AI SDK CoreMessage format.
|
||||
*
|
||||
* Key differences:
|
||||
* - OpenAI tool calls: { role: "assistant", tool_calls: [{ id, function: { name, arguments } }] }
|
||||
* - Vercel tool calls: { role: "assistant", content: [{ type: "tool-call", toolCallId, toolName, args }] }
|
||||
* - OpenAI tool results: { role: "tool", tool_call_id: "...", content: "string" }
|
||||
* - Vercel tool results: { role: "tool", content: [{ type: "tool-result", toolCallId: "...", toolName: "...", result: any }] }
|
||||
*
|
||||
* IMPORTANT: For multi-turn conversations with tools:
|
||||
* - We EXCLUDE assistant messages that have tool_calls because Vercel AI SDK manages tool call state internally
|
||||
* - We only include tool results, and Vercel will associate them with its internal tool call tracking
|
||||
* - We include assistant messages with tool_calls converted to Vercel format
|
||||
*/
|
||||
export function convertOpenAIMessagesToVercel(
|
||||
messages: ChatCompletionMessageParam[],
|
||||
|
|
@ -60,19 +61,43 @@ export function convertOpenAIMessagesToVercel(
|
|||
break;
|
||||
|
||||
case "assistant":
|
||||
// CRITICAL: Skip assistant messages with tool_calls in subsequent turns
|
||||
// Vercel AI SDK manages tool call state internally and doesn't expect us to re-send them
|
||||
// Only include assistant messages WITHOUT tool calls (normal responses)
|
||||
if (!msg.tool_calls || msg.tool_calls.length === 0) {
|
||||
if (msg.tool_calls && msg.tool_calls.length > 0) {
|
||||
const contentParts: any[] = [];
|
||||
|
||||
if (msg.content) {
|
||||
contentParts.push({
|
||||
type: "text",
|
||||
text: msg.content,
|
||||
});
|
||||
}
|
||||
|
||||
for (const tc of msg.tool_calls) {
|
||||
if (tc.type === "function") {
|
||||
let args: unknown;
|
||||
try {
|
||||
args = JSON.parse(tc.function.arguments);
|
||||
} catch {
|
||||
args = tc.function.arguments;
|
||||
}
|
||||
contentParts.push({
|
||||
type: "tool-call",
|
||||
toolCallId: tc.id,
|
||||
toolName: tc.function.name,
|
||||
args,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
vercelMessages.push({
|
||||
role: "assistant",
|
||||
content: contentParts,
|
||||
});
|
||||
} else {
|
||||
vercelMessages.push({
|
||||
role: "assistant",
|
||||
content: msg.content || "",
|
||||
});
|
||||
}
|
||||
// Note: We skip assistant messages WITH tool_calls because:
|
||||
// 1. They were generated by Vercel AI SDK in a previous turn
|
||||
// 2. Vercel tracks tool call state internally
|
||||
// 3. Re-sending them causes Anthropic API errors about missing tool_use blocks
|
||||
break;
|
||||
|
||||
case "tool":
|
||||
|
|
|
|||
|
|
@ -249,6 +249,13 @@ export const VertexAIConfigSchema = BasePlusConfig.extend({
|
|||
});
|
||||
export type VertexAIConfig = z.infer<typeof VertexAIConfigSchema>;
|
||||
|
||||
export const AiSdkConfigSchema = BasePlusConfig.extend({
|
||||
provider: z.literal("ai-sdk"),
|
||||
model: z.string(),
|
||||
providerOptions: z.record(z.unknown()).optional(),
|
||||
});
|
||||
export type AiSdkConfig = z.infer<typeof AiSdkConfigSchema>;
|
||||
|
||||
// Discriminated union
|
||||
export const LLMConfigSchema = z.discriminatedUnion("provider", [
|
||||
OpenAIConfigSchema,
|
||||
|
|
@ -268,5 +275,6 @@ export const LLMConfigSchema = z.discriminatedUnion("provider", [
|
|||
ContinueProxyConfigSchema,
|
||||
CometAPIConfigSchema,
|
||||
AskSageConfigSchema,
|
||||
AiSdkConfigSchema,
|
||||
]);
|
||||
export type LLMConfig = z.infer<typeof LLMConfigSchema>;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue