feat: initial prototype

This commit is contained in:
2025-10-30 20:52:42 -07:00
parent bf932d2052
commit d9188d5b74
11 changed files with 5404 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
node_modules
+7
View File
@@ -0,0 +1,7 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
},
"eslint.validate": ["typescript"],
}
+3
View File
@@ -0,0 +1,3 @@
import NaomisConfig from '@nhcarrigan/eslint-config';
export default NaomisConfig;
+32
View File
@@ -0,0 +1,32 @@
{
"name": "nomena",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"build": "tsc",
"lint": "eslint src --max-warnings 0",
"start": "op run --env-file=prod.env -- node prod/index.js",
"test": "echo \"Error: no test specified\" && exit 0"
},
"keywords": [],
"author": "",
"license": "ISC",
"packageManager": "pnpm@10.19.0",
"devDependencies": {
"@nhcarrigan/eslint-config": "5.2.0",
"@nhcarrigan/typescript-config": "4.0.0",
"@types/node": "24.9.2",
"eslint": "9.38.0",
"typescript": "5.9.3"
},
"dependencies": {
"@anthropic-ai/sdk": "0.68.0",
"@google/genai": "1.28.0",
"@nhcarrigan/discord-analytics": "0.0.6",
"@nhcarrigan/logger": "1.1.1",
"discord.js": "14.24.2",
"fastify": "5.6.1"
}
}
+5056
View File
File diff suppressed because it is too large Load Diff
+4
View File
@@ -0,0 +1,4 @@
DISCORD_TOKEN="op://Environment Variables - Naomi/Nomena/Discord token"
ANTHROPIC_API_KEY="op://Environment Variables - Naomi/Nomena/anthropic token"
GEMINI_API_KEY="op://Environment Variables - Naomi/Nomena/gemini token"
LOG_TOKEN="op://Environment Variables - Naomi/Alert Server/api_auth"
+94
View File
@@ -0,0 +1,94 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import Anthropic from "@anthropic-ai/sdk";
import { GoogleGenAI, PersonGeneration } from "@google/genai";
import { AttachmentBuilder } from "discord.js";
/**
*
*/
export class Ai {
anthropic;
gemini;
/**
* @param anthropicKey
* @param geminiKey
*/
constructor(anthropicKey, geminiKey) {
this.anthropic = new Anthropic({
apiKey: anthropicKey,
});
this.gemini = new GoogleGenAI({
apiKey: geminiKey,
});
}
/**
* @param prompt
*/
async generateProjectInfo(prompt) {
const projectRequest = await fetch("https://data.nhcarrigan.com/projects.json");
const projectResponse
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Fetch does not accept a generic.
= (await projectRequest.json());
const names = await this.generateText(`Your task is to generate a project name based on the user's description. Provide ONLY a list of 1-5 fitting names, and an explanation for why you chose them. Note that project names should be unique. Here's a list of all existing project names: ${projectResponse.
map((p) => {
return p.name;
}).
join(", ")}`, prompt);
const image = await this.generateImage(`Your task is to generate a full body anime girl mascot for this project. The image should have a transparent background. Potential names: ${names}. The project description is: ${prompt}`);
if (image === null) {
return { content: `Project Name: ${names}\nProject Description: ${prompt}\nSorry, I was unable to generate an image for you.` };
}
return { content: `Project Name: ${names}\nProject Description: ${prompt}`,
files: [new AttachmentBuilder(image, { name: "avatar.png" })] };
}
/**
* @param system
* @param prompt
*/
async generateText(system, prompt) {
const response = await this.anthropic.messages.create({
max_tokens: 1000,
messages: [
{
content: prompt,
role: "user",
},
],
model: "claude-sonnet-4-5-20250929",
system: system,
});
const text = response.content.
filter((c) => {
return c.type === "text";
}).
map((c) => {
return c.text;
}).
join("");
return text;
}
/**
* @param prompt
*/
async generateImage(prompt) {
const response = await this.gemini.models.generateImages({
config: {
aspectRatio: "3:4",
imageSize: "2K",
numberOfImages: 1,
outputMimeType: "image/png",
personGeneration: PersonGeneration.ALLOW_ADULT,
},
model: "models/imagen-4.0-generate-001",
prompt: prompt,
});
const base64 = response.generatedImages?.[0]?.image?.imageBytes;
if (base64 === undefined) {
return null;
}
return Buffer.from(base64, "base64");
}
}
+44
View File
@@ -0,0 +1,44 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { Logger } from "@nhcarrigan/logger";
import { Client, Events, GatewayIntentBits } from "discord.js";
import { Ai } from "./classes/ai.js";
if (process.env.ANTHROPIC_API_KEY === undefined
|| process.env.GEMINI_API_KEY === undefined
|| process.env.LOG_TOKEN === undefined
|| process.env.DISCORD_TOKEN === undefined) {
throw new Error(`ANTHROPIC_API_KEY, GEMINI_API_KEY, LOG_TOKEN, and DISCORD_TOKEN must be set.`);
}
const ai = new Ai(process.env.ANTHROPIC_API_KEY, process.env.GEMINI_API_KEY);
const logger = new Logger("Nomena", process.env.LOG_TOKEN);
const bot = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
bot.once("ready", () => {
void logger.log("debug", "Nomena is online!");
});
// eslint-disable-next-line @typescript-eslint/no-misused-promises -- Lazy.
bot.on(Events.MessageCreate, async (message) => {
if (!message.mentions.has("1433657054433771621", {
ignoreEveryone: true,
ignoreRepliedUser: true,
ignoreRoles: true,
})) {
return;
}
if (message.author.id !== "465650873650118659") {
await message.reply("Sorry, I can only generate project ideas for Naomi.");
}
await message.channel.sendTyping();
const prompt = message.content.replace(/<@!?1433657054433771621>/, "").trim();
const projectInfo = await ai.generateProjectInfo(prompt);
await message.reply(projectInfo);
});
await bot.login(process.env.DISCORD_TOKEN);
+103
View File
@@ -0,0 +1,103 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { Anthropic } from "@anthropic-ai/sdk";
import { GoogleGenAI, PersonGeneration } from "@google/genai";
import { AttachmentBuilder, type MessageCreateOptions } from "discord.js";
/**
* Utility class for generating project information and images.
*/
export class Ai {
private readonly anthropic: Anthropic;
private readonly gemini: GoogleGenAI;
/**
* Creates a new instance of the Ai class.
* @param anthropicKey - The API key for the Anthropic API.
* @param geminiKey - The API key for the Gemini API.
*/
public constructor(anthropicKey: string, geminiKey: string) {
this.anthropic = new Anthropic({
apiKey: anthropicKey,
});
this.gemini = new GoogleGenAI({
apiKey: geminiKey,
});
}
/**
* Generates a list of potential project names and a full body anime girl mascot for the project.
* @param prompt - The user's prompt for the project.
* @returns A message create options object containing the project name, description, and image.
*/
public async generateProjectInfo(prompt: string):
Promise<MessageCreateOptions> {
const projectRequest = await fetch(
"https://data.nhcarrigan.com/projects.json",
);
const projectResponse
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Fetch does not accept a generic.
= (await projectRequest.json()) as Array<{ name: string }>;
const names = await this.generateText(
`Your task is to generate a project name based on the user's description. Provide ONLY a list of 1-5 fitting names, and an explanation for why you chose them. Note that project names should be unique. Here's a list of all existing project names: ${projectResponse.
map((p) => {
return p.name;
}).
join(", ")}`,
prompt,
);
const image = await this.generateImage(`Your task is to generate a full body anime girl mascot for this project. The image should have a transparent background. The project description is: ${prompt}`);
if (image === null) {
return { content: `Project Name: ${names}\nProject Description: ${prompt}\nSorry, I was unable to generate an image for you.` };
}
return { content: `Project Name: ${names}\nProject Description: ${prompt}`,
files: [ new AttachmentBuilder(image, { name: "avatar.png" }) ] };
}
private async generateText(system: string, prompt: string): Promise<string> {
const response = await this.anthropic.messages.create({
// eslint-disable-next-line @typescript-eslint/naming-convention -- SDK requirement.
max_tokens: 1000,
messages: [
{
content: prompt,
role: "user",
},
],
model: "claude-sonnet-4-5-20250929",
system: system,
});
const text = response.content.
filter((c) => {
return c.type === "text";
}).
map((c) => {
return c.text;
}).
join("");
return text;
}
private async generateImage(prompt: string): Promise<Buffer | null> {
const response = await this.gemini.models.generateImages({
config: {
aspectRatio: "3:4",
imageSize: "2K",
numberOfImages: 1,
outputMimeType: "image/png",
personGeneration: PersonGeneration.ALLOW_ADULT,
},
model: "models/imagen-4.0-generate-001",
prompt: prompt,
});
const base64 = response.generatedImages?.[0]?.image?.imageBytes;
if (base64 === undefined) {
return null;
}
return Buffer.from(base64, "base64");
}
}
+53
View File
@@ -0,0 +1,53 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { Logger } from "@nhcarrigan/logger";
import { Client, Events, GatewayIntentBits } from "discord.js";
import { Ai } from "./classes/ai.js";
if (process.env.ANTHROPIC_API_KEY === undefined
|| process.env.GEMINI_API_KEY === undefined
|| process.env.LOG_TOKEN === undefined
|| process.env.DISCORD_TOKEN === undefined) {
throw new Error(
`ANTHROPIC_API_KEY, GEMINI_API_KEY, LOG_TOKEN, and DISCORD_TOKEN must be set.`,
);
}
const ai = new Ai(process.env.ANTHROPIC_API_KEY, process.env.GEMINI_API_KEY);
const logger = new Logger("Nomena", process.env.LOG_TOKEN);
const bot = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
bot.once("ready", () => {
void logger.log("debug", "Nomena is online!");
});
// eslint-disable-next-line @typescript-eslint/no-misused-promises -- Lazy.
bot.on(Events.MessageCreate, async(message) => {
if (!message.mentions.has("1433657054433771621", {
ignoreEveryone: true,
ignoreRepliedUser: true,
ignoreRoles: true,
})) {
return;
}
if (message.author.id !== "465650873650118659") {
await message.reply("Sorry, I can only generate project ideas for Naomi.");
}
await message.channel.sendTyping();
const prompt = message.content.replace(/<@!?1433657054433771621>/, "").trim();
const projectInfo = await ai.generateProjectInfo(prompt);
await message.reply(projectInfo);
});
await bot.login(process.env.DISCORD_TOKEN);
+7
View File
@@ -0,0 +1,7 @@
{
"extends": "@nhcarrigan/typescript-config",
"compilerOptions": {
"outDir": "./prod",
"rootDir": "./src",
}
}