Spaces:
Build error
Build error
Upload folder using huggingface_hub
Browse files
app/api/analyze-edit-intent/route.ts
CHANGED
|
@@ -1,30 +1,9 @@
|
|
| 1 |
import { NextRequest, NextResponse } from 'next/server';
|
| 2 |
-
import { createAnthropic } from '@ai-sdk/anthropic';
|
| 3 |
-
import { createOpenAI } from '@ai-sdk/openai';
|
| 4 |
-
import { createGoogleGenerativeAI } from '@ai-sdk/google';
|
| 5 |
import { generateObject } from 'ai';
|
| 6 |
import { z } from 'zod';
|
|
|
|
| 7 |
// import type { FileManifest } from '@/types/file-manifest'; // Type is used implicitly through manifest parameter
|
| 8 |
|
| 9 |
-
// Check if we're using Vercel AI Gateway
|
| 10 |
-
const isUsingAIGateway = !!process.env.AI_GATEWAY_API_KEY;
|
| 11 |
-
const aiGatewayBaseURL = 'https://ai-gateway.vercel.sh/v1';
|
| 12 |
-
|
| 13 |
-
const anthropic = createAnthropic({
|
| 14 |
-
apiKey: process.env.AI_GATEWAY_API_KEY ?? process.env.ANTHROPIC_API_KEY,
|
| 15 |
-
baseURL: isUsingAIGateway ? aiGatewayBaseURL : (process.env.ANTHROPIC_BASE_URL || 'https://api.anthropic.com/v1'),
|
| 16 |
-
});
|
| 17 |
-
|
| 18 |
-
const openai = createOpenAI({
|
| 19 |
-
apiKey: process.env.AI_GATEWAY_API_KEY ?? process.env.OPENAI_API_KEY,
|
| 20 |
-
baseURL: isUsingAIGateway ? aiGatewayBaseURL : process.env.OPENAI_BASE_URL,
|
| 21 |
-
});
|
| 22 |
-
|
| 23 |
-
const googleGenerativeAI = createGoogleGenerativeAI({
|
| 24 |
-
apiKey: process.env.AI_GATEWAY_API_KEY ?? process.env.GEMINI_API_KEY,
|
| 25 |
-
baseURL: isUsingAIGateway ? aiGatewayBaseURL : undefined,
|
| 26 |
-
});
|
| 27 |
-
|
| 28 |
// Schema for the AI's search plan - not file selection!
|
| 29 |
const searchPlanSchema = z.object({
|
| 30 |
editType: z.enum([
|
|
@@ -98,19 +77,9 @@ export async function POST(request: NextRequest) {
|
|
| 98 |
console.log('[analyze-edit-intent] File summary preview:', fileSummary.split('\n').slice(0, 5).join('\n'));
|
| 99 |
|
| 100 |
// Select the appropriate AI model based on the request
|
| 101 |
-
|
| 102 |
-
if (model.startsWith('anthropic/')) {
|
| 103 |
-
aiModel = anthropic(model.replace('anthropic/', ''));
|
| 104 |
-
} else if (model.startsWith('openai/')) {
|
| 105 |
-
aiModel = openai(model.replace('openai/', ''));
|
| 106 |
-
} else if (model.startsWith('google/')) {
|
| 107 |
-
aiModel = googleGenerativeAI(model.replace('google/', ''));
|
| 108 |
-
} else {
|
| 109 |
-
// Default to openai if model format is unclear
|
| 110 |
-
aiModel = openai(model);
|
| 111 |
-
}
|
| 112 |
|
| 113 |
-
console.log('[analyze-edit-intent] Using AI model:',
|
| 114 |
|
| 115 |
// Use AI to create a search plan
|
| 116 |
const result = await generateObject({
|
|
|
|
| 1 |
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
|
|
|
|
|
|
| 2 |
import { generateObject } from 'ai';
|
| 3 |
import { z } from 'zod';
|
| 4 |
+
import { getProviderForModel } from '@/lib/ai/provider-manager';
|
| 5 |
// import type { FileManifest } from '@/types/file-manifest'; // Type is used implicitly through manifest parameter
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
// Schema for the AI's search plan - not file selection!
|
| 8 |
const searchPlanSchema = z.object({
|
| 9 |
editType: z.enum([
|
|
|
|
| 77 |
console.log('[analyze-edit-intent] File summary preview:', fileSummary.split('\n').slice(0, 5).join('\n'));
|
| 78 |
|
| 79 |
// Select the appropriate AI model based on the request
|
| 80 |
+
const { client: aiModel, actualModel } = getProviderForModel(model);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
console.log('[analyze-edit-intent] Using AI model:', actualModel);
|
| 83 |
|
| 84 |
// Use AI to create a search plan
|
| 85 |
const result = await generateObject({
|
app/api/generate-ai-code-stream/route.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
| 1 |
import { NextRequest, NextResponse } from 'next/server';
|
| 2 |
-
import { createAnthropic } from '@ai-sdk/anthropic';
|
| 3 |
-
import { createOpenAI } from '@ai-sdk/openai';
|
| 4 |
-
import { createGoogleGenerativeAI } from '@ai-sdk/google';
|
| 5 |
import { streamText } from 'ai';
|
| 6 |
import type { SandboxState } from '@/types/sandbox';
|
| 7 |
import { selectFilesForEdit, getFileContents, formatFilesForAI } from '@/lib/context-selector';
|
|
@@ -9,34 +6,11 @@ import { executeSearchPlan, formatSearchResultsForAI, selectTargetFile } from '@
|
|
| 9 |
import { FileManifest } from '@/types/file-manifest';
|
| 10 |
import type { ConversationState, ConversationMessage, ConversationEdit } from '@/types/conversation';
|
| 11 |
import { appConfig } from '@/config/app.config';
|
|
|
|
| 12 |
|
| 13 |
// Force dynamic route to enable streaming
|
| 14 |
export const dynamic = 'force-dynamic';
|
| 15 |
|
| 16 |
-
// Check if we're using Vercel AI Gateway
|
| 17 |
-
const isUsingAIGateway = !!process.env.AI_GATEWAY_API_KEY;
|
| 18 |
-
const aiGatewayBaseURL = 'https://ai-gateway.vercel.sh/v1';
|
| 19 |
-
|
| 20 |
-
console.log('[generate-ai-code-stream] AI Gateway config:', {
|
| 21 |
-
isUsingAIGateway,
|
| 22 |
-
hasAIGatewayKey: !!process.env.AI_GATEWAY_API_KEY
|
| 23 |
-
});
|
| 24 |
-
|
| 25 |
-
const anthropic = createAnthropic({
|
| 26 |
-
apiKey: process.env.AI_GATEWAY_API_KEY ?? process.env.ANTHROPIC_API_KEY,
|
| 27 |
-
baseURL: isUsingAIGateway ? aiGatewayBaseURL : (process.env.ANTHROPIC_BASE_URL || 'https://api.anthropic.com/v1'),
|
| 28 |
-
});
|
| 29 |
-
|
| 30 |
-
const googleGenerativeAI = createGoogleGenerativeAI({
|
| 31 |
-
apiKey: process.env.AI_GATEWAY_API_KEY ?? process.env.GEMINI_API_KEY,
|
| 32 |
-
baseURL: isUsingAIGateway ? aiGatewayBaseURL : undefined,
|
| 33 |
-
});
|
| 34 |
-
|
| 35 |
-
const openai = createOpenAI({
|
| 36 |
-
apiKey: process.env.AI_GATEWAY_API_KEY ?? process.env.OPENAI_API_KEY,
|
| 37 |
-
baseURL: isUsingAIGateway ? aiGatewayBaseURL : process.env.OPENAI_BASE_URL,
|
| 38 |
-
});
|
| 39 |
-
|
| 40 |
// Helper function to analyze user preferences from conversation history
|
| 41 |
function analyzeUserPreferences(messages: ConversationMessage[]): {
|
| 42 |
commonPatterns: string[];
|
|
@@ -1206,34 +1180,14 @@ MORPH FAST APPLY MODE (EDIT-ONLY):
|
|
| 1206 |
const packagesToInstall: string[] = [];
|
| 1207 |
|
| 1208 |
// Determine which provider to use based on model
|
| 1209 |
-
const
|
| 1210 |
-
const isGoogle = model.startsWith('google/');
|
| 1211 |
-
const isOpenAI = model.startsWith('openai/');
|
| 1212 |
-
const modelProvider = isAnthropic ? anthropic :
|
| 1213 |
-
(isOpenAI ? openai :
|
| 1214 |
-
(isGoogle ? googleGenerativeAI :
|
| 1215 |
-
(openai)));
|
| 1216 |
-
|
| 1217 |
-
// Fix model name transformation for different providers
|
| 1218 |
-
let actualModel: string;
|
| 1219 |
-
if (isAnthropic) {
|
| 1220 |
-
actualModel = model.replace('anthropic/', '');
|
| 1221 |
-
} else if (isOpenAI) {
|
| 1222 |
-
actualModel = model.replace('openai/', '');
|
| 1223 |
-
} else if (isGoogle) {
|
| 1224 |
-
// Google uses specific model names - convert our naming to theirs
|
| 1225 |
-
actualModel = model.replace('google/', '');
|
| 1226 |
-
} else {
|
| 1227 |
-
actualModel = model;
|
| 1228 |
-
}
|
| 1229 |
|
| 1230 |
-
console.log(`[generate-ai-code-stream] Using provider
|
| 1231 |
-
console.log(`[generate-ai-code-stream] AI Gateway enabled: ${isUsingAIGateway}`);
|
| 1232 |
console.log(`[generate-ai-code-stream] Model string: ${model}`);
|
| 1233 |
|
| 1234 |
// Make streaming API call with appropriate provider
|
| 1235 |
const streamOptions: any = {
|
| 1236 |
-
model: modelProvider
|
| 1237 |
messages: [
|
| 1238 |
{
|
| 1239 |
role: 'system',
|
|
|
|
| 1 |
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
|
|
|
|
|
|
| 2 |
import { streamText } from 'ai';
|
| 3 |
import type { SandboxState } from '@/types/sandbox';
|
| 4 |
import { selectFilesForEdit, getFileContents, formatFilesForAI } from '@/lib/context-selector';
|
|
|
|
| 6 |
import { FileManifest } from '@/types/file-manifest';
|
| 7 |
import type { ConversationState, ConversationMessage, ConversationEdit } from '@/types/conversation';
|
| 8 |
import { appConfig } from '@/config/app.config';
|
| 9 |
+
import { getProviderForModel } from '@/lib/ai/provider-manager';
|
| 10 |
|
| 11 |
// Force dynamic route to enable streaming
|
| 12 |
export const dynamic = 'force-dynamic';
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
// Helper function to analyze user preferences from conversation history
|
| 15 |
function analyzeUserPreferences(messages: ConversationMessage[]): {
|
| 16 |
commonPatterns: string[];
|
|
|
|
| 1180 |
const packagesToInstall: string[] = [];
|
| 1181 |
|
| 1182 |
// Determine which provider to use based on model
|
| 1183 |
+
const { client: modelProvider, actualModel } = getProviderForModel(model);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1184 |
|
| 1185 |
+
console.log(`[generate-ai-code-stream] Using provider for model: ${actualModel}`);
|
|
|
|
| 1186 |
console.log(`[generate-ai-code-stream] Model string: ${model}`);
|
| 1187 |
|
| 1188 |
// Make streaming API call with appropriate provider
|
| 1189 |
const streamOptions: any = {
|
| 1190 |
+
model: modelProvider,
|
| 1191 |
messages: [
|
| 1192 |
{
|
| 1193 |
role: 'system',
|