Suryakiran AI
Suryakiran AI
How can I help you today?
Ask anything. Suryakiran AI is ready to help.
export default {
async fetch(request, env) {
// ==============================
// CORS
// ==============================
const origin = request.headers.get("Origin") || "*";
const corsHeaders = {
"Access-Control-Allow-Origin": origin,
"Access-Control-Allow-Methods": "POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
"Content-Type": "application/json"
};
// ==============================
// OPTIONS / CORS PREFLIGHT
// ==============================
if (request.method === "OPTIONS") {
return new Response(null, {
status: 204,
headers: corsHeaders
});
}
// ==============================
// ONLY POST REQUEST
// ==============================
if (request.method !== "POST") {
return new Response(
JSON.stringify({
error: "Only POST requests are allowed."
}),
{
status: 405,
headers: corsHeaders
}
);
}
// ==============================
// CHECK API KEY
// ==============================
if (!env. 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) {
return new Response(
JSON.stringify({
error: "OPENAI_API_KEY is not configured in Cloudflare."
}),
{
status: 500,
headers: corsHeaders
}
);
}
try {
// ==============================
// READ BLOGGER REQUEST
// ==============================
const body = await request.json();
let messages = body.messages;
if (!Array.isArray(messages)) {
return new Response(
JSON.stringify({
error: "Invalid messages format."
}),
{
status: 400,
headers: corsHeaders
}
);
}
// ==============================
// LIMIT CHAT HISTORY
// ==============================
messages = messages.slice(-20);
// ==============================
// CONVERT TO OPENAI INPUT
// ==============================
const input = messages
.map(message => {
const role =
message.role === "assistant"
? "assistant"
: "user";
const content =
typeof message.content === "string"
? message.content
: "";
return {
role: role,
content: [
{
type: "input_text",
text: content
}
]
};
})
.filter(message =>
message.content[0].text.trim() !== ""
);
// ==============================
// OPENAI API REQUEST
// ==============================
const openaiResponse = await fetch(
"https://api.openai.com/v1/responses",
{
method: "POST",
headers: {
"Authorization":
`Bearer ${env. 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}`,
"Content-Type":
"application/json"
},
body: JSON.stringify({
// You can change this model
model:
env.OPENAI_MODEL || "gpt-5-mini",
instructions:
`You are Suryakiran AI,
a helpful, intelligent and friendly AI assistant.
Answer the user's questions clearly and accurately.
You can answer in English, Hindi,
Gujarati or other languages depending
on the user's language.
Keep answers useful and easy to understand.
Do not mention internal API keys,
Cloudflare or backend configuration
unless specifically necessary.`,
input: input,
max_output_tokens: 1500
})
}
);
// ==============================
// OPENAI ERROR
// ==============================
if (!openaiResponse.ok) {
const errorText =
await openaiResponse.text();
return new Response(
JSON.stringify({
error:
"OpenAI API Error",
details:
errorText
}),
{
status: openaiResponse.status,
headers: corsHeaders
}
);
}
// ==============================
// GET OPENAI RESPONSE
// ==============================
const data =
await openaiResponse.json();
// ==============================
// EXTRACT TEXT
// ==============================
let answer = "";
if (typeof data.output_text === "string") {
answer =
data.output_text;
} else if (Array.isArray(data.output)) {
for (const item of data.output) {
if (
item.type === "message" &&
Array.isArray(item.content)
) {
for (const content of item.content) {
if (
content.type === "output_text" &&
typeof content.text === "string"
) {
answer += content.text;
}
}
}
}
}
if (!answer) {
answer =
"Sorry, I couldn't generate a response.";
}
// ==============================
// SEND RESPONSE TO BLOGGER
// ==============================
return new Response(
JSON.stringify({
output: answer
}),
{
status: 200,
headers: corsHeaders
}
);
} catch (error) {
// ==============================
// SERVER ERROR
// ==============================
return new Response(
JSON.stringify({
error:
"Server error",
details:
error.message
}),
{
status: 500,
headers: corsHeaders
}
);
}
}
};