import process from "node:process";
import { AIAgent } from "https://esm.town/v/zzz/AIAgent";
import { verifyJWT } from "https://esm.town/v/zzz/verifyJWT";
import { upstashRateLimiter } from "https://esm.town/v/zzz/upstashRateLimiter";
export const OpenAISummary = async (text: string, config: {
apiKey?: string;
jwt?: string;
modelName?: string;
} = {}) => {
const { apiKey, jwt, modelName = "gpt-4" } = config;
if (!apiKey && !jwt) {
const { success, reset } = await upstashRateLimiter(
"anon",
"@zzz.OpenAISummary",
2,
"60 s",
);
if (!success) {
return `Rate Limit Exceeded. Try again at ${reset}`;
}
}
else if (!apiKey) {
try {
await verifyJWT(jwt);
}
catch (err) {
return `Invalid JWT`;
}
}
const agent = await AIAgent(
apiKey || process.env.OPENAI_API_KEY_GPT4,
);
const response = await agent.summarize(text, modelName);
return response;
};