@luzzle/core 0.0.242 → 0.0.244

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.
Files changed (51) hide show
  1. package/dist/src/database/client.d.ts +3 -2
  2. package/dist/src/database/client.js +4 -1
  3. package/dist/src/database/client.js.map +1 -1
  4. package/dist/src/index.d.ts +4 -3
  5. package/dist/src/index.js +4 -3
  6. package/dist/src/index.js.map +1 -1
  7. package/dist/src/lib/ajv.d.ts +4 -1
  8. package/dist/src/lib/ajv.js +8 -2
  9. package/dist/src/lib/ajv.js.map +1 -1
  10. package/dist/src/llm/field-schema.d.ts +2 -0
  11. package/dist/src/llm/field-schema.js +30 -0
  12. package/dist/src/llm/field-schema.js.map +1 -0
  13. package/dist/src/llm/google/client.d.ts +22 -0
  14. package/dist/src/llm/google/client.js +180 -0
  15. package/dist/src/llm/google/client.js.map +1 -0
  16. package/dist/src/llm/google/constants.d.ts +11 -0
  17. package/dist/src/llm/google/constants.js +12 -0
  18. package/dist/src/llm/google/constants.js.map +1 -0
  19. package/dist/src/llm/google/generation.d.ts +17 -0
  20. package/dist/src/llm/google/generation.js +72 -0
  21. package/dist/src/llm/google/generation.js.map +1 -0
  22. package/dist/src/llm/google/index.d.ts +4 -0
  23. package/dist/src/llm/google/index.js +4 -0
  24. package/dist/src/llm/google/index.js.map +1 -0
  25. package/dist/src/pieces/Piece.d.ts +5 -3
  26. package/dist/src/pieces/Piece.fixtures.js +1 -0
  27. package/dist/src/pieces/Piece.fixtures.js.map +1 -1
  28. package/dist/src/pieces/Piece.js +28 -28
  29. package/dist/src/pieces/Piece.js.map +1 -1
  30. package/dist/src/pieces/item.js +1 -1
  31. package/dist/src/pieces/item.js.map +1 -1
  32. package/dist/src/pieces/utils/frontmatter.d.ts +1 -1
  33. package/dist/src/pieces/utils/frontmatter.js.map +1 -1
  34. package/dist/src/pieces/utils/generation.d.ts +2 -0
  35. package/dist/src/pieces/utils/generation.js +32 -0
  36. package/dist/src/pieces/utils/generation.js.map +1 -0
  37. package/dist/src/pieces/utils/piece.d.ts +1 -1
  38. package/dist/src/pieces/utils/piece.js +70 -56
  39. package/dist/src/pieces/utils/piece.js.map +1 -1
  40. package/dist/src/storage/abstract.d.ts +5 -1
  41. package/dist/src/storage/abstract.js.map +1 -1
  42. package/dist/src/storage/fs.d.ts +4 -1
  43. package/dist/src/storage/fs.js +6 -2
  44. package/dist/src/storage/fs.js.map +1 -1
  45. package/dist/src/storage/storage.mock.js +1 -0
  46. package/dist/src/storage/storage.mock.js.map +1 -1
  47. package/dist/tsconfig.build.tsbuildinfo +1 -1
  48. package/package.json +3 -2
  49. package/dist/src/llm/google.d.ts +0 -12
  50. package/dist/src/llm/google.js +0 -103
  51. package/dist/src/llm/google.js.map +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luzzle/core",
3
- "version": "0.0.242",
3
+ "version": "0.0.244",
4
4
  "author": "eleith",
5
5
  "engines": {
6
6
  "node": ">=24"
@@ -42,13 +42,14 @@
42
42
  "vitest": "3.2.4"
43
43
  },
44
44
  "dependencies": {
45
- "@google/genai": "1.30.0",
45
+ "@google/genai": "2.24.0",
46
46
  "@paralleldrive/cuid2": "2.2.2",
47
47
  "@sindresorhus/slugify": "3.0.0",
48
48
  "ajv": "8.17.1",
49
49
  "fdir": "6.5.0",
50
50
  "file-type": "21.3.0",
51
51
  "got": "14.6.5",
52
+ "json-schema-traverse": "1.0.0",
52
53
  "jsonpathly": "3.0.0",
53
54
  "kysely": "0.28.17",
54
55
  "remark": "15.0.1",
@@ -1,12 +0,0 @@
1
- import type { JSONSchemaType } from 'ajv';
2
- import { type PieceFrontmatter } from '../pieces/index.js';
3
- declare function pieceFrontMatterFromPrompt(apiKey: string, schema: JSONSchemaType<PieceFrontmatter>, prompt: string, files?: Array<string | Buffer>): Promise<{
4
- [k: string]: import("../index.js").PieceFrontMatterValue;
5
- }>;
6
- declare function validateApiKey(apiKey: string): Promise<{
7
- ok: true;
8
- } | {
9
- ok: false;
10
- reason: string;
11
- }>;
12
- export { pieceFrontMatterFromPrompt, validateApiKey };
@@ -1,103 +0,0 @@
1
- import { fileTypeFromBuffer } from 'file-type';
2
- import { fileTypeFromFile } from 'file-type/node';
3
- import { createPartFromUri, GoogleGenAI, HarmCategory, HarmBlockThreshold } from '@google/genai';
4
- import { readFile } from 'fs/promises';
5
- import path from 'path';
6
- import {} from '../pieces/index.js';
7
- const MODEL_NAME = 'gemini-2.5-flash';
8
- const safetySettings = [
9
- {
10
- category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
11
- threshold: HarmBlockThreshold.BLOCK_NONE,
12
- },
13
- {
14
- category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
15
- threshold: HarmBlockThreshold.BLOCK_NONE,
16
- },
17
- {
18
- category: HarmCategory.HARM_CATEGORY_CIVIC_INTEGRITY,
19
- threshold: HarmBlockThreshold.BLOCK_NONE,
20
- },
21
- ];
22
- function getClient(apiKey) {
23
- return new GoogleGenAI({ apiKey });
24
- }
25
- async function extractPartFromFile(file, genAI) {
26
- const isBuffer = Buffer.isBuffer(file);
27
- const fileType = isBuffer ? await fileTypeFromBuffer(file) : await fileTypeFromFile(file);
28
- const mimeType = fileType?.mime;
29
- // assume it's non binary
30
- if (!mimeType) {
31
- const fileContent = isBuffer ? file.toString() : await readFile(file, 'utf-8');
32
- const fileName = isBuffer ? 'string buffer of downloaded file' : path.basename(file);
33
- const data = [
34
- `---[start] embedding ${fileName}---`,
35
- fileContent,
36
- `---[end] embedding of ${fileName}---`,
37
- ];
38
- return data.join('\n');
39
- }
40
- else {
41
- const uploadedFile = await genAI.files.upload({
42
- file: isBuffer ? new Blob([file]) : file,
43
- config: {
44
- mimeType,
45
- },
46
- });
47
- // Wait for the file to be processed.
48
- let getFile = await genAI.files.get({ name: uploadedFile.name });
49
- while (getFile.state === 'PROCESSING') {
50
- getFile = await genAI.files.get({ name: uploadedFile.name });
51
- await new Promise((resolve) => {
52
- setTimeout(resolve, 5000);
53
- });
54
- }
55
- if (uploadedFile.state === 'FAILED') {
56
- throw new Error('File processing failed.');
57
- }
58
- if (uploadedFile.uri && uploadedFile.mimeType) {
59
- const fileContent = createPartFromUri(uploadedFile.uri, uploadedFile.mimeType);
60
- return fileContent;
61
- }
62
- }
63
- throw new Error('File processing failed.');
64
- }
65
- async function pieceFrontMatterFromPrompt(apiKey, schema, prompt, files) {
66
- const genAI = getClient(apiKey);
67
- const content = [prompt];
68
- if (files) {
69
- for (const file of files) {
70
- const part = await extractPartFromFile(file, genAI);
71
- content.push(part);
72
- }
73
- }
74
- const result = await genAI.models.generateContent({
75
- model: MODEL_NAME,
76
- contents: content,
77
- config: {
78
- responseMimeType: 'application/json',
79
- safetySettings: safetySettings,
80
- responseJsonSchema: schema,
81
- systemInstruction: `you are an assistant that helps generate JSON metadata for a record that will be added to a collection of similar records.
82
-
83
- if you are provided pdf attachments, images or other text based files, please prioritize them as inputs for generating metadata for the record.
84
-
85
- you are also given a responseJsonSchema to guide your output. each field in the schema has a description and examples to help guide what the intention of each field is and what values to expect.`,
86
- },
87
- });
88
- const metadata = result.text || '{}';
89
- const frontmatter = JSON.parse(metadata);
90
- return Object.fromEntries(Object.entries(frontmatter).filter(([, value]) => value !== null || value === ''));
91
- }
92
- async function validateApiKey(apiKey) {
93
- try {
94
- const genAI = getClient(apiKey);
95
- await genAI.models.list();
96
- return { ok: true };
97
- }
98
- catch (err) {
99
- return { ok: false, reason: err instanceof Error ? err.message : String(err) };
100
- }
101
- }
102
- export { pieceFrontMatterFromPrompt, validateApiKey };
103
- //# sourceMappingURL=google.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"google.js","sourceRoot":"","sources":["../../../src/llm/google.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAA;AAC9C,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAA;AAIjD,OAAO,EACN,iBAAiB,E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AE,EAAE,IAAI,EAAE,CAAA;IACpB,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACd,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAA;IAC/E,CAAC;AACF,CAAC;AAED,OAAO,EAAE,0BAA0B,EAAE,cAAc,EAAE,CAAA","sourcesContent":["import { fileTypeFromBuffer } from 'file-type'\nimport { fileTypeFromFile } from 'file-type/node'\nimport type {\n\tContentListUnion,\n\tSafetySetting} from '@google/genai';\nimport {\n\tcreatePartFromUri,\n\tGoogleGenAI,\n\tHarmCategory,\n\tHarmBlockThreshold\n} from '@google/genai'\nimport { readFile } from 'fs/promises'\nimport path from 'path'\nimport type { JSONSchemaType } from 'ajv'\nimport { type PieceFrontmatter } from '../pieces/index.js'\n\nconst MODEL_NAME = 'gemini-2.5-flash'\n\nconst safetySettings: SafetySetting[] = [\n\t{\n\t\tcategory: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,\n\t\tthreshold: HarmBlockThreshold.BLOCK_NONE,\n\t},\n\t{\n\t\tcategory: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,\n\t\tthreshold: HarmBlockThreshold.BLOCK_NONE,\n\t},\n\t{\n\t\tcategory: HarmCategory.HARM_CATEGORY_CIVIC_INTEGRITY,\n\t\tthreshold: HarmBlockThreshold.BLOCK_NONE,\n\t},\n]\n\nfunction getClient(apiKey: string) {\n\treturn new GoogleGenAI({ apiKey })\n}\n\nasync function extractPartFromFile(file: string | Buffer, genAI: GoogleGenAI) {\n\tconst isBuffer = Buffer.isBuffer(file)\n\tconst fileType = isBuffer ? await fileTypeFromBuffer(file) : await fileTypeFromFile(file)\n\tconst mimeType = fileType?.mime\n\n\t// assume it's non binary\n\tif (!mimeType) {\n\t\tconst fileContent = isBuffer ? file.toString() : await readFile(file, 'utf-8')\n\t\tconst fileName = isBuffer ? 'string buffer of downloaded file' : path.basename(file)\n\t\tconst data = [\n\t\t\t`---[start] embedding ${fileName}---`,\n\t\t\tfileContent,\n\t\t\t`---[end] embedding of ${fileName}---`,\n\t\t]\n\n\t\treturn data.join('\\n')\n\t} else {\n\t\tconst uploadedFile = await genAI.files.upload({\n\t\t\tfile: isBuffer ? new Blob([file]) : file,\n\t\t\tconfig: {\n\t\t\t\tmimeType,\n\t\t\t},\n\t\t})\n\n\t\t// Wait for the file to be processed.\n\t\tlet getFile = await genAI.files.get({ name: uploadedFile.name as string })\n\n\t\twhile (getFile.state === 'PROCESSING') {\n\t\t\tgetFile = await genAI.files.get({ name: uploadedFile.name as string })\n\n\t\t\tawait new Promise((resolve) => {\n\t\t\t\tsetTimeout(resolve, 5000)\n\t\t\t})\n\t\t}\n\n\t\tif (uploadedFile.state === 'FAILED') {\n\t\t\tthrow new Error('File processing failed.')\n\t\t}\n\n\t\tif (uploadedFile.uri && uploadedFile.mimeType) {\n\t\t\tconst fileContent = createPartFromUri(uploadedFile.uri, uploadedFile.mimeType)\n\t\t\treturn fileContent\n\t\t}\n\t}\n\n\tthrow new Error('File processing failed.')\n}\n\nasync function pieceFrontMatterFromPrompt(\n\tapiKey: string,\n\tschema: JSONSchemaType<PieceFrontmatter>,\n\tprompt: string,\n\tfiles?: Array<string | Buffer>\n) {\n\tconst genAI = getClient(apiKey)\n\n\tconst content: ContentListUnion = [prompt]\n\n\tif (files) {\n\t\tfor (const file of files) {\n\t\t\tconst part = await extractPartFromFile(file, genAI)\n\t\t\tcontent.push(part)\n\t\t}\n\t}\n\n\tconst result = await genAI.models.generateContent({\n\t\tmodel: MODEL_NAME,\n\t\tcontents: content,\n\t\tconfig: {\n\t\t\tresponseMimeType: 'application/json',\n\t\t\tsafetySettings: safetySettings,\n\t\t\tresponseJsonSchema: schema,\n\t\t\tsystemInstruction: `you are an assistant that helps generate JSON metadata for a record that will be added to a collection of similar records. \n\nif you are provided pdf attachments, images or other text based files, please prioritize them as inputs for generating metadata for the record. \n\nyou are also given a responseJsonSchema to guide your output. each field in the schema has a description and examples to help guide what the intention of each field is and what values to expect.`,\n\t\t},\n\t})\n\n\tconst metadata = result.text || '{}'\n\tconst frontmatter = JSON.parse(metadata) as PieceFrontmatter\n\n\treturn Object.fromEntries(\n\t\tObject.entries(frontmatter).filter(([, value]) => value !== null || value === '')\n\t)\n}\n\nasync function validateApiKey(\n\tapiKey: string\n): Promise<{ ok: true } | { ok: false; reason: string }> {\n\ttry {\n\t\tconst genAI = getClient(apiKey)\n\t\tawait genAI.models.list()\n\t\treturn { ok: true }\n\t} catch (err) {\n\t\treturn { ok: false, reason: err instanceof Error ? err.message : String(err) }\n\t}\n}\n\nexport { pieceFrontMatterFromPrompt, validateApiKey }\n"]}