KrishnaCosmic commited on
Commit
c75dfef
·
1 Parent(s): b08debc

working version

Browse files
Files changed (2) hide show
  1. src/app/api/chat/route.ts +3 -1
  2. src/lib/ai-client.ts +19 -4
src/app/api/chat/route.ts CHANGED
@@ -33,9 +33,11 @@ export async function POST(request: NextRequest) {
33
 
34
  if (!result.success) {
35
  console.error("AI Engine error:", result.error);
 
36
  return NextResponse.json({
37
  error: "AI service unavailable",
38
- detail: result.error
 
39
  }, { status: 503 });
40
  }
41
 
 
33
 
34
  if (!result.success) {
35
  console.error("AI Engine error:", result.error);
36
+ // Return 503 with helpful error message
37
  return NextResponse.json({
38
  error: "AI service unavailable",
39
+ detail: result.error,
40
+ help: "The AI engine service is not running. Please ensure the AI engine is deployed and the AI_ENGINE_URL environment variable is correctly configured."
41
  }, { status: 503 });
42
  }
43
 
src/lib/ai-client.ts CHANGED
@@ -6,6 +6,7 @@
6
  */
7
 
8
  const AI_ENGINE_URL = process.env.AI_ENGINE_URL || "http://localhost:7860";
 
9
 
10
  interface AIResponse<T = unknown> {
11
  success: boolean;
@@ -19,7 +20,7 @@ interface AIResponse<T = unknown> {
19
  export async function callAIEngine<T = unknown>(
20
  endpoint: string,
21
  body: object,
22
- options?: { timeout?: number }
23
  ): Promise<AIResponse<T>> {
24
  const url = `${AI_ENGINE_URL}${endpoint}`;
25
  const timeout = options?.timeout || 30000;
@@ -33,11 +34,18 @@ export async function callAIEngine<T = unknown>(
33
  const controller = new AbortController();
34
  const timeoutId = setTimeout(() => controller.abort(), timeout);
35
 
 
 
 
 
 
 
 
 
 
36
  const response = await fetch(url, {
37
  method: "POST",
38
- headers: {
39
- "Content-Type": "application/json",
40
- },
41
  body: JSON.stringify(body),
42
  signal: controller.signal,
43
  });
@@ -48,6 +56,13 @@ export async function callAIEngine<T = unknown>(
48
  const errorText = await response.text();
49
  console.error(`AI Engine error [${response.status}]:`, errorText);
50
 
 
 
 
 
 
 
 
51
  if (response.status === 503 || response.status === 502) {
52
  return {
53
  success: false,
 
6
  */
7
 
8
  const AI_ENGINE_URL = process.env.AI_ENGINE_URL || "http://localhost:7860";
9
+ const AI_ENGINE_API_KEY = process.env.AI_ENGINE_API_KEY || "";
10
 
11
  interface AIResponse<T = unknown> {
12
  success: boolean;
 
20
  export async function callAIEngine<T = unknown>(
21
  endpoint: string,
22
  body: object,
23
+ options?: { timeout?: number; token?: string }
24
  ): Promise<AIResponse<T>> {
25
  const url = `${AI_ENGINE_URL}${endpoint}`;
26
  const timeout = options?.timeout || 30000;
 
34
  const controller = new AbortController();
35
  const timeoutId = setTimeout(() => controller.abort(), timeout);
36
 
37
+ const headers: Record<string, string> = {
38
+ "Content-Type": "application/json",
39
+ };
40
+
41
+ // Add API key authentication if configured
42
+ if (AI_ENGINE_API_KEY) {
43
+ headers["X-API-Key"] = AI_ENGINE_API_KEY;
44
+ }
45
+
46
  const response = await fetch(url, {
47
  method: "POST",
48
+ headers,
 
 
49
  body: JSON.stringify(body),
50
  signal: controller.signal,
51
  });
 
56
  const errorText = await response.text();
57
  console.error(`AI Engine error [${response.status}]:`, errorText);
58
 
59
+ if (response.status === 401) {
60
+ return {
61
+ success: false,
62
+ error: "Authentication failed with AI engine. Please ensure AI_ENGINE_URL and authentication are correctly configured."
63
+ };
64
+ }
65
+
66
  if (response.status === 503 || response.status === 502) {
67
  return {
68
  success: false,