generated from nhcarrigan/template
58 lines
1.7 KiB
TypeScript
58 lines
1.7 KiB
TypeScript
/**
|
|
* @copyright nhcarrigan
|
|
* @license Naomi's Public License
|
|
* @author Naomi Carrigan
|
|
*/
|
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention -- 'Tis a class.
|
|
import Anthropic from "@anthropic-ai/sdk";
|
|
import {
|
|
announcementJsonSchema,
|
|
announcementSystemMessage,
|
|
} from "../config/announcements.js";
|
|
import type { AnnouncementResponse }
|
|
from "../interfaces/announcementResponse.js";
|
|
|
|
/**
|
|
* Generates announcements for all platforms using AI.
|
|
* @param content - The main body of the announcement.
|
|
* @returns The announcements for all platforms, or null if the request fails.
|
|
*/
|
|
export const generateAnnouncements = async(
|
|
content: string,
|
|
): Promise<AnnouncementResponse | null> => {
|
|
if (process.env.ANTHROPIC_KEY === undefined) {
|
|
return null;
|
|
}
|
|
const anthropic = new Anthropic({
|
|
apiKey: process.env.ANTHROPIC_KEY,
|
|
timeout: 5 * 60 * 1000,
|
|
});
|
|
const response = await anthropic.beta.messages.create({
|
|
betas: [ "structured-outputs-2025-11-13" ],
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention -- API requirement
|
|
max_tokens: 10_000,
|
|
messages: [
|
|
{
|
|
content: content,
|
|
role: "user",
|
|
},
|
|
],
|
|
model: "claude-opus-4-5-20251101",
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention -- API requirement
|
|
output_format: {
|
|
schema: announcementJsonSchema,
|
|
type: "json_schema",
|
|
},
|
|
system: announcementSystemMessage,
|
|
});
|
|
const text = response.content.find((m) => {
|
|
return m.type === "text";
|
|
});
|
|
if (text?.text === undefined) {
|
|
return null;
|
|
}
|
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Being lazy.
|
|
return JSON.parse(text.text) as AnnouncementResponse;
|
|
};
|