@overandoutnerd/dumbledore-bot-core 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ import { QUOTES } from "./data/quotes.js";
2
+ import { GIFS } from "./data/gifs.js";
3
+ export { QUOTES };
4
+ export { GIFS };
5
+ export { getRelevantQuote, getTopMatches, } from "./quoteSearch.js";
6
+ export type { QuoteMatch, } from "./quoteSearch.js";
7
+ export declare function getRandomQuote(): string;
8
+ export declare function getRandomGif(): string;
package/dist/index.js ADDED
@@ -0,0 +1,13 @@
1
+ import { QUOTES } from "./data/quotes.js";
2
+ import { GIFS } from "./data/gifs.js";
3
+ export { QUOTES };
4
+ export { GIFS };
5
+ export { getRelevantQuote, getTopMatches, } from "./quoteSearch.js";
6
+ export function getRandomQuote() {
7
+ return QUOTES[Math.floor(Math.random() *
8
+ QUOTES.length)];
9
+ }
10
+ export function getRandomGif() {
11
+ return GIFS[Math.floor(Math.random() *
12
+ GIFS.length)];
13
+ }
@@ -0,0 +1,6 @@
1
+ export type QuoteMatch = {
2
+ text: string;
3
+ score: number;
4
+ };
5
+ export declare function getTopMatches(message: string, apiKey: string, limit?: number): Promise<QuoteMatch[]>;
6
+ export declare function getRelevantQuote(message: string, apiKey: string): Promise<string>;
@@ -0,0 +1,63 @@
1
+ import { GoogleGenAI } from "@google/genai";
2
+ import quoteEmbeddings from "./data/quoteEmbeddings.json" with {
3
+ type: "json"
4
+ };
5
+ const quotes = quoteEmbeddings;
6
+ function cosineSimilarity(a, b) {
7
+ let dot = 0;
8
+ let normA = 0;
9
+ let normB = 0;
10
+ const length = Math.min(a.length, b.length);
11
+ for (let i = 0; i < length; i++) {
12
+ const av = a[i] ?? 0;
13
+ const bv = b[i] ?? 0;
14
+ dot += av * bv;
15
+ normA += av * av;
16
+ normB += bv * bv;
17
+ }
18
+ const denominator = Math.sqrt(normA) *
19
+ Math.sqrt(normB);
20
+ if (denominator === 0) {
21
+ return 0;
22
+ }
23
+ return dot / denominator;
24
+ }
25
+ async function getEmbedding(message, apiKey) {
26
+ const ai = new GoogleGenAI({
27
+ apiKey,
28
+ });
29
+ const response = await ai.models.embedContent({
30
+ model: "gemini-embedding-001",
31
+ contents: message,
32
+ config: {
33
+ outputDimensionality: 768,
34
+ },
35
+ });
36
+ const embedding = response.embeddings?.[0]
37
+ ?.values;
38
+ if (!embedding) {
39
+ throw new Error("Failed to generate embedding");
40
+ }
41
+ return embedding;
42
+ }
43
+ export async function getTopMatches(message, apiKey, limit = 5) {
44
+ const embedding = await getEmbedding(message, apiKey);
45
+ return quotes
46
+ .map((quote) => ({
47
+ text: quote.text,
48
+ score: cosineSimilarity(embedding, quote.embedding),
49
+ }))
50
+ .sort((a, b) => b.score - a.score)
51
+ .slice(0, limit);
52
+ }
53
+ export async function getRelevantQuote(message, apiKey) {
54
+ console.log("[QuoteSearch] Message:", message);
55
+ const matches = await getTopMatches(message, apiKey, 5);
56
+ console.log("[QuoteSearch] Top Matches:", matches);
57
+ const selected = matches[Math.floor(Math.random() * 2)];
58
+ if (!selected) {
59
+ throw new Error("No matching quotes found");
60
+ }
61
+ console.log("[QuoteSearch] Selected:", selected.score);
62
+ return selected.text;
63
+ }
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@overandoutnerd/dumbledore-bot-core",
3
+ "version": "1.0.2",
4
+ "description": "Shared Dumbledore bot library",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsc",
13
+ "prepublishOnly": "npm run build"
14
+ },
15
+ "dependencies": {
16
+ "@google/genai": "^2.8.0"
17
+ },
18
+ "devDependencies": {
19
+ "@types/node": "^25.9.3",
20
+ "tsx": "^4.22.4",
21
+ "typescript": "^6.0.3"
22
+ }
23
+ }