Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions app/api/evaluations/stt/datasets/[dataset_id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { NextResponse, NextRequest } from 'next/server';

export async function GET(
request: Request,
{ params }: { params: Promise<{ dataset_id: string }> }
) {
const { dataset_id } = await params;
const backendUrl = process.env.NEXT_PUBLIC_BACKEND_URL || 'http://localhost:8000';
const apiKey = request.headers.get('X-API-KEY');

try {
const response = await fetch(`${backendUrl}/api/v1/evaluations/stt/datasets/${dataset_id}`, {
headers: {
'X-API-KEY': apiKey || '',
},
});

const data = await response.json();
return NextResponse.json(data, { status: response.status });
} catch (error) {
return NextResponse.json(
{ success: false, error: 'Failed to fetch config', data: null },
{ status: 500 }
);
Comment on lines +20 to +24
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Error message is inaccurate — should reference "dataset", not "config".

Proposed fix
-      { success: false, error: 'Failed to fetch config', data: null },
+      { success: false, error: 'Failed to fetch dataset', data: null },
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
} catch (error) {
return NextResponse.json(
{ success: false, error: 'Failed to fetch config', data: null },
{ status: 500 }
);
} catch (error) {
return NextResponse.json(
{ success: false, error: 'Failed to fetch dataset', data: null },
{ status: 500 }
);
🤖 Prompt for AI Agents
In `@app/api/evaluations/stt/datasets/`[dataset_id]/route.ts around lines 20 - 24,
The catch block currently returns an inaccurate error message ("Failed to fetch
config"); update the response in the catch inside route.ts so the
NextResponse.json call uses a message referencing the dataset (e.g., "Failed to
fetch dataset" or include the dataset_id) instead of "config". Locate the catch
block that calls NextResponse.json and replace the error string to clearly
reference the dataset (and optionally include dataset_id or error details if
available) so the returned error message matches the operation performed.

}
}
64 changes: 64 additions & 0 deletions app/api/evaluations/stt/datasets/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { NextResponse, NextRequest } from 'next/server';



export async function GET(request: Request) {
const backendUrl = process.env.NEXT_PUBLIC_BACKEND_URL || 'http://localhost:8000';
const apiKey = request.headers.get('X-API-KEY');

try {
const response = await fetch(`${backendUrl}/api/v1/evaluations/stt/datasets`, {
headers: {
'X-API-KEY': apiKey || '',
},
});

const data = await response.json();
return NextResponse.json(data);
Comment on lines +16 to +17
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Backend status not forwarded — same issue as runs/route.ts.

Proposed fix
     const data = await response.json();
-    return NextResponse.json(data);
+    return NextResponse.json(data, { status: response.status });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const data = await response.json();
return NextResponse.json(data);
const data = await response.json();
return NextResponse.json(data, { status: response.status });
🤖 Prompt for AI Agents
In `@app/api/evaluations/stt/datasets/route.ts` around lines 16 - 17, The handler
currently returns NextResponse.json(data) but does not forward the upstream HTTP
status (same bug as runs/route.ts); change the return to preserve the original
response status (and optionally headers) by returning NextResponse.json(data, {
status: response.status /*, headers: response.headers */ }); locate the
variables response and data in route.ts and update the NextResponse.json call
accordingly.

} catch (error) {
return NextResponse.json(
{ success: false, error: error.message, data: null },
{ status: 500 }
);
Comment on lines +18 to +22
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

error.message on unknown — same TypeScript issue as runs/route.ts.

Apply the same instanceof Error narrowing here.

🤖 Prompt for AI Agents
In `@app/api/evaluations/stt/datasets/route.ts` around lines 18 - 22, The catch
block in the route handler currently assumes `error.message` exists; update the
error handling in the catch clause to narrow the unknown `error` with `if (error
instanceof Error)` and use `error.message` when true, otherwise provide a safe
fallback string (e.g., "Unknown error") when building the NextResponse JSON;
locate and update the catch in the evaluation STT datasets route (the block that
returns NextResponse.json({ success: false, error: error.message, ... })) to
implement this instanceof Error narrowing and fallback.

}
}


export async function POST(request: NextRequest) {
try {
const apiKey = request.headers.get('X-API-KEY');
if (!apiKey) {
return NextResponse.json({
error: 'Missing X-API-KEY. Either generate an API Key. Contact Kaapi team for more details'
},
{
status: 401
}

)
}
const body=await request.json();
const backendUrl=process.env.NEXT_PUBLIC_BACKEND_URL || 'http://localhost:8000';

const response=await fetch(`${backendUrl}/api/v1/evaluations/stt/datasets`, {
method:'POST',
body:JSON.stringify(body),
headers:{
'X-API-KEY':apiKey,
'Content-Type':'application/json'
},
});
const data=await response.json();
return NextResponse.json(data, {status:response.status})



} catch (error) {
console.error('Proxy error:', error);
return NextResponse.json(
{error:'Failed to forward request', details:error.message},
{status:500}
);
Comment on lines +56 to +61
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

error.message on unknown in POST catch block — same fix needed.

🤖 Prompt for AI Agents
In `@app/api/evaluations/stt/datasets/route.ts` around lines 56 - 61, The catch
block in the POST handler currently assumes error has a .message property
(console.error and NextResponse.json using error.message); update the POST catch
to normalize the error into a safe string (e.g., const msg = error instanceof
Error ? error.message : String(error)) and use that safe value in both the
console.error and the NextResponse.json details so we don't access .message on
unknown types; look for the catch in the POST route handler in route.ts and
replace direct error.message usage with this normalized message before returning
the response.

}

}
49 changes: 49 additions & 0 deletions app/api/evaluations/stt/files/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { NextRequest, NextResponse } from 'next/server';


export async function POST(request: NextRequest) {
try {
// Get the API key from request headers
const apiKey = request.headers.get('X-API-KEY');

if (!apiKey) {
return NextResponse.json(
{ error: 'Missing X-API-KEY header' },
{ status: 401 }
);
}

// Get the form data from the request
const formData = await request.formData();

// Get backend URL from environment variable
const backendUrl = process.env.NEXT_PUBLIC_BACKEND_URL || 'http://localhost:8000';

// Forward the request to the actual backend
const response = await fetch(`${backendUrl}/api/v1/evaluations/stt/files`, {
method: 'POST',
body: formData,
headers: {
'X-API-KEY': apiKey,

},
});

// Handle empty responses (204 No Content, etc.)
const text = await response.text();
const data = text ? JSON.parse(text) : { success: true };

// Return the response with the same status code
if (!response.ok) {
return NextResponse.json(data, { status: response.status });
}

return NextResponse.json(data, { status: response.status });
} catch (error: any) {
console.error('Proxy error:', error);
return NextResponse.json(
{ error: 'Failed to forward request to backend', details: error.message },
{ status: 500 }
);
}
}
26 changes: 26 additions & 0 deletions app/api/evaluations/stt/results/[result_id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { NextResponse, NextRequest } from 'next/server';

export async function GET(
request: Request,
{ params }: { params: Promise<{ result_id: string }> }
) {
const { result_id } = await params;
const backendUrl = process.env.NEXT_PUBLIC_BACKEND_URL || 'http://localhost:8000';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

NEXT_PUBLIC_ prefix exposes the backend URL to the client bundle.

Environment variables prefixed with NEXT_PUBLIC_ are inlined into the client-side JavaScript bundle. Since this is a server-only route handler, use a non-prefixed variable (e.g., BACKEND_URL) to avoid leaking internal infrastructure URLs to the browser. This applies to all six route files in this PR.

🤖 Prompt for AI Agents
In `@app/api/evaluations/stt/results/`[result_id]/route.ts at line 8, The code
uses a NEXT_PUBLIC_ prefixed env var for server-only route handlers, which leaks
to the client; update the backendUrl constant to read a non-public variable
(e.g., process.env.BACKEND_URL) with the existing fallback
('http://localhost:8000') and replace occurrences in this file's backendUrl
declaration (and the same change in the other five route files in this PR) so
the backend URL is read from a server-only env var instead of
NEXT_PUBLIC_BACKEND_URL.

const apiKey = request.headers.get('X-API-KEY');

try {
const response = await fetch(`${backendUrl}/api/v1/evaluations/stt/results/${result_id}`, {
headers: {
'X-API-KEY': apiKey || '',
},
});

const data = await response.json();
return NextResponse.json(data, { status: response.status });
} catch (error) {
return NextResponse.json(
{ success: false, error: 'Failed to fetch config', data: null },
{ status: 500 }
);
Comment on lines +20 to +24
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Error message is inaccurate — copy-paste artifact.

The error says 'Failed to fetch config' but this endpoint fetches a result, not a config. The same copy-paste error appears in the datasets/[dataset_id] and runs/[run_id] routes as well.

Proposed fix
-      { success: false, error: 'Failed to fetch config', data: null },
+      { success: false, error: 'Failed to fetch result', data: null },
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
} catch (error) {
return NextResponse.json(
{ success: false, error: 'Failed to fetch config', data: null },
{ status: 500 }
);
} catch (error) {
return NextResponse.json(
{ success: false, error: 'Failed to fetch result', data: null },
{ status: 500 }
);
🤖 Prompt for AI Agents
In `@app/api/evaluations/stt/results/`[result_id]/route.ts around lines 20 - 24,
The catch block returns an inaccurate error message ("Failed to fetch config") —
update the NextResponse.json call in the route's catch handler to return a
correct message like "Failed to fetch result" (keep success:false, data:null and
status:500) so the error reflects the endpoint's purpose; apply the same fix to
the analogous catch handlers in the datasets/[dataset_id] and runs/[run_id]
routes, locating the places by searching for NextResponse.json(...) in the catch
blocks of those route handlers.

}
}
26 changes: 26 additions & 0 deletions app/api/evaluations/stt/runs/[run_id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { NextResponse, NextRequest } from 'next/server';

export async function GET(
request: Request,
{ params }: { params: Promise<{ run_id: string }> }
) {
const { run_id } = await params;
const backendUrl = process.env.NEXT_PUBLIC_BACKEND_URL || 'http://localhost:8000';
const apiKey = request.headers.get('X-API-KEY');

try {
const response = await fetch(`${backendUrl}/api/v1/evaluations/stt/runs/${run_id}`, {
headers: {
'X-API-KEY': apiKey || '',
},
});

const data = await response.json();
return NextResponse.json(data, { status: response.status });
} catch (error) {
return NextResponse.json(
{ success: false, error: 'Failed to fetch config', data: null },
{ status: 500 }
);
Comment on lines +20 to +24
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Error message is inaccurate — should reference "run", not "config".

Proposed fix
-      { success: false, error: 'Failed to fetch config', data: null },
+      { success: false, error: 'Failed to fetch run', data: null },
🤖 Prompt for AI Agents
In `@app/api/evaluations/stt/runs/`[run_id]/route.ts around lines 20 - 24, The
catch block in route.ts returns an inaccurate error message; update the
NextResponse.json call inside the catch (where it currently returns { success:
false, error: 'Failed to fetch config', ... }) to reference the run (e.g.,
'Failed to fetch run' or include the run_id) so the error correctly describes
the failed operation; modify the error string passed to NextResponse.json in
that catch block (the code around NextResponse.json) accordingly.

}
}
64 changes: 64 additions & 0 deletions app/api/evaluations/stt/runs/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { NextResponse, NextRequest } from 'next/server';



export async function GET(request: Request) {
const backendUrl = process.env.NEXT_PUBLIC_BACKEND_URL || 'http://localhost:8000';
const apiKey = request.headers.get('X-API-KEY');

try {
const response = await fetch(`${backendUrl}/api/v1/evaluations/stt/runs`, {
headers: {
'X-API-KEY': apiKey || '',
},
});

const data = await response.json();
return NextResponse.json(data);
Comment on lines +16 to +17
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Backend HTTP status is silently dropped — client always receives 200.

Unlike the [run_id] sub-route (which passes { status: response.status }), this GET handler returns the data without forwarding the backend's status code. A 404 or 403 from the backend will appear as a 200 to the client. The same issue exists in datasets/route.ts GET (Line 17).

Proposed fix
     const data = await response.json();
-    return NextResponse.json(data);
+    return NextResponse.json(data, { status: response.status });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const data = await response.json();
return NextResponse.json(data);
const data = await response.json();
return NextResponse.json(data, { status: response.status });
🤖 Prompt for AI Agents
In `@app/api/evaluations/stt/runs/route.ts` around lines 16 - 17, The GET handler
currently returns the backend JSON body but drops the original HTTP status;
update the response to forward the backend status by returning
NextResponse.json(data, { status: response.status }) instead of
NextResponse.json(data). Apply the same change to the analogous GET handler in
datasets/route.ts so both app/api/evaluations/stt/runs/route.ts and
datasets/route.ts forward response.status via NextResponse.json.

} catch (error) {
return NextResponse.json(
{ success: false, error: error.message, data: null },
{ status: 500 }
);
Comment on lines +18 to +22
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

error.message on unknown type will fail TypeScript strict checks.

In TypeScript, the caught error is typed as unknown by default. Accessing .message directly without narrowing will cause a compile error under strict / useUnknownInCatchVariables. This same issue appears in datasets/route.ts Lines 20 and 59, and runs/route.ts Line 59.

Proposed fix
   } catch (error) {
     return NextResponse.json(
-      { success: false, error: error.message, data: null },
+      { success: false, error: error instanceof Error ? error.message : 'Internal server error', data: null },
       { status: 500 }
     );
   }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
} catch (error) {
return NextResponse.json(
{ success: false, error: error.message, data: null },
{ status: 500 }
);
} catch (error) {
return NextResponse.json(
{ success: false, error: error instanceof Error ? error.message : 'Internal server error', data: null },
{ status: 500 }
);
}
🤖 Prompt for AI Agents
In `@app/api/evaluations/stt/runs/route.ts` around lines 18 - 22, The catch blocks
currently access error.message on an unknown-typed catch variable (seen where
NextResponse.json is called in the catch), which breaks TypeScript strict
checks; update each catch (including in app/api/evaluations/stt/runs/route.ts,
datasets/route.ts and runs/route.ts) to safely narrow the error before reading
message—e.g. determine const msg = error instanceof Error ? error.message :
String(error) (or JSON.stringify when appropriate) and pass msg into
NextResponse.json instead of error.message so the code compiles under
strict/useUnknownInCatchVariables.

}
}


export async function POST(request: NextRequest) {
try {
const apiKey = request.headers.get('X-API-KEY');
if (!apiKey) {
return NextResponse.json({
error: 'Missing X-API-KEY. Either generate an API Key. Contact Kaapi team for more details'
},
{
status: 401
}

)
}
const body=await request.json();
const backendUrl=process.env.NEXT_PUBLIC_BACKEND_URL || 'http://localhost:8000';

const response=await fetch(`${backendUrl}/api/v1/evaluations/stt/runs`, {
method:'POST',
body:JSON.stringify(body),
headers:{
'X-API-KEY':apiKey,
'Content-Type':'application/json'
},
});
const data=await response.json();
return NextResponse.json(data, {status:response.status})



} catch (error) {
console.error('Proxy error:', error);
return NextResponse.json(
{error:'Failed to forward request', details:error.message},
{status:500}
);
Comment on lines +56 to +61
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Same error.message on unknown issue in the POST catch block.

Proposed fix
   } catch (error) {
     console.error('Proxy error:', error);
     return NextResponse.json(
-      {error:'Failed to forward request', details:error.message},
+      {error:'Failed to forward request', details: error instanceof Error ? error.message : 'Unknown error'},
       {status:500}
     );
   }
🤖 Prompt for AI Agents
In `@app/api/evaluations/stt/runs/route.ts` around lines 56 - 61, The catch block
in the POST handler currently uses error.message which will throw if error isn't
an Error; update the POST catch block (the try/catch around the request forward)
to safely extract a message by checking if error is an instance of Error and
using error.message, otherwise convert the error to a string (e.g.,
String(error) or JSON.stringify fallback) and include that safe message in the
NextResponse.json details and the console.error call so unknown error shapes
don't cause a secondary failure.

}

}