ak-gemini 1.0.5 → 1.0.7

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 (6) hide show
  1. package/README.md +4 -3
  2. package/index.cjs +316 -86
  3. package/index.js +458 -126
  4. package/package.json +23 -17
  5. package/types.d.ts +138 -0
  6. package/types.ts +0 -65
package/package.json CHANGED
@@ -2,19 +2,25 @@
2
2
  "name": "ak-gemini",
3
3
  "author": "ak@mixpanel.com",
4
4
  "description": "AK's Generative AI Helper for doing... transforms",
5
- "version": "1.0.5",
5
+ "version": "1.0.7",
6
6
  "main": "index.js",
7
7
  "files": [
8
8
  "index.js",
9
9
  "index.cjs",
10
- "types.ts",
10
+ "types.d.ts",
11
11
  "logger.js"
12
12
  ],
13
- "types": "types.ts",
13
+ "types": "types.d.ts",
14
14
  "exports": {
15
15
  ".": {
16
- "import": "./index.js",
17
- "require": "./index.cjs"
16
+ "import": {
17
+ "types": "./types.d.ts",
18
+ "default": "./index.js"
19
+ },
20
+ "require": {
21
+ "types": "./types.d.ts",
22
+ "default": "./index.cjs"
23
+ }
18
24
  }
19
25
  },
20
26
  "repository": {
@@ -26,17 +32,17 @@
26
32
  },
27
33
  "homepage": "https://github.com/ak--47/ak-gemini#readme",
28
34
  "scripts": {
29
- "prepublishOnly": "npm run build:cjs",
30
- "post": "npm publish --access public",
35
+ "prepublishOnly": "npm run typecheck && npm run build:cjs",
36
+ "post": "npm publish",
31
37
  "release": "npm version patch && npm publish --access public",
32
- "local": "./scripts/local.sh",
33
- "fire": "./scripts/fire.sh",
34
- "deploy": "./scripts/deploy.sh",
35
- "perms": "chmod +x ./scripts/*.sh",
36
38
  "update-deps": "npx npm-check-updates -u && npm install",
37
39
  "prune": "rm -rf tmp/*",
38
40
  "test": "node --no-warnings --experimental-vm-modules node_modules/jest/bin/jest.js",
39
- "build:cjs": "esbuild index.js --bundle --platform=node --format=cjs --outfile=index.cjs --external:@google/genai --external:ak-tools --external:dotenv --external:pino-pretty --external:pino"
41
+ "test:unit": "npm test -- tests/module.test.js",
42
+ "build:cjs": "esbuild index.js --bundle --platform=node --format=cjs --outfile=index.cjs --external:@google/genai --external:ak-tools --external:dotenv --external:pino-pretty --external:pino",
43
+ "coverage": "node --no-warnings --experimental-vm-modules node_modules/jest/bin/jest.js --coverage",
44
+ "typecheck": "tsc --noEmit",
45
+ "update:gemini": "npm install @google/genai@latest"
40
46
  },
41
47
  "type": "module",
42
48
  "keywords": [
@@ -46,17 +52,17 @@
46
52
  ],
47
53
  "license": "ISC",
48
54
  "dependencies": {
49
- "@google-cloud/functions-framework": "^4.0.0",
50
- "@google/genai": "^1.4.0",
51
- "ak-tools": "^1.0.64",
55
+ "@google/genai": "^1.34.0",
56
+ "ak-tools": "^1.1.12",
52
57
  "dotenv": "^16.5.0",
53
58
  "pino": "^9.7.0",
54
59
  "pino-pretty": "^13.0.0"
55
60
  },
56
61
  "devDependencies": {
57
62
  "@types/jest": "^29.5.14",
58
- "esbuild": "^0.25.5",
63
+ "esbuild": "^0.25.11",
59
64
  "jest": "^29.7.0",
60
- "nodemon": "^3.1.10"
65
+ "nodemon": "^3.1.10",
66
+ "typescript": "^5.9.2"
61
67
  }
62
68
  }
package/types.d.ts ADDED
@@ -0,0 +1,138 @@
1
+ import type { GoogleGenAI, ThinkingLevel } from '@google/genai';
2
+
3
+ export { ThinkingLevel };
4
+
5
+ export interface ThinkingConfig {
6
+ /** Indicates whether to include thoughts in the response. If true, thoughts are returned only if the model supports thought and thoughts are available. */
7
+ includeThoughts?: boolean;
8
+ /** Indicates the thinking budget in tokens. 0 is DISABLED. -1 is AUTOMATIC. The default values and allowed ranges are model dependent. */
9
+ thinkingBudget?: number;
10
+ /** Optional. The number of thoughts tokens that the model should generate. */
11
+ thinkingLevel?: ThinkingLevel;
12
+ }
13
+
14
+ export interface SafetySetting {
15
+ category: string; // The harm category
16
+ threshold: string; // The blocking threshold
17
+ }
18
+
19
+ export interface ChatConfig {
20
+ responseMimeType?: string; // MIME type for responses
21
+ temperature?: number; // Controls randomness (0.0 to 1.0)
22
+ topP?: number; // Controls diversity via nucleus sampling
23
+ topK?: number; // Controls diversity by limiting top-k tokens
24
+ systemInstruction?: string; // System instruction for the model
25
+ safetySettings?: SafetySetting[]; // Safety settings array
26
+ responseSchema?: Object; // Schema for validating model responses
27
+ thinkingConfig?: ThinkingConfig; // Thinking features configuration
28
+ [key: string]: any; // Additional properties for flexibility
29
+ }
30
+
31
+ export interface AITransformerContext {
32
+ modelName?: string;
33
+ systemInstructions?: string;
34
+ chatConfig?: ChatConfig;
35
+ genAI?: any;
36
+ chat?: any;
37
+ examplesFile?: string | null;
38
+ exampleData?: TransformationExample[] | null;
39
+ promptKey?: string;
40
+ answerKey?: string;
41
+ contextKey?: string;
42
+ explanationKey?: string;
43
+ systemInstructionsKey?: string;
44
+ maxRetries?: number;
45
+ retryDelay?: number;
46
+ init?: (force?: boolean) => Promise<void>; // Initialization function
47
+ seed?: () => Promise<void>; // Function to seed the transformer with examples
48
+ message?: (payload: Record<string, unknown>) => Promise<Record<string, unknown>>; // Function to send messages to the model
49
+ rebuild?: (lastPayload: Record<string, unknown>, serverError: string) => Promise<Record<string, unknown>>; // Function to rebuild the transformer
50
+ rawMessage?: (payload: Record<string, unknown> | string) => Promise<Record<string, unknown>>; // Function to send raw messages to the model
51
+ genAIClient?: GoogleGenAI; // Google GenAI client instance
52
+ onlyJSON?: boolean; // If true, only JSON responses are allowed
53
+
54
+ }
55
+
56
+ export interface TransformationExample {
57
+ CONTEXT?: Record<string, unknown> | string; // optional context for the transformation
58
+ PROMPT?: Record<string, unknown>; // what the user provides as input
59
+ ANSWER?: Record<string, unknown>; // what the model should return as output
60
+ INPUT?: Record<string, unknown>; // alias for PROMPT
61
+ OUTPUT?: Record<string, unknown>; // alias for ANSWER
62
+ SYSTEM?: string; // system instructions for this example
63
+ EXPLANATION?: string; // explanation for this example
64
+ [key: string]: any; // allow additional properties for flexible key mapping
65
+ }
66
+
67
+ export interface ExampleFileContent {
68
+ examples: TransformationExample[];
69
+ }
70
+
71
+ export interface AITransformerOptions {
72
+ // ? https://ai.google.dev/gemini-api/docs/models
73
+ modelName?: string; // The Gemini model to use
74
+ systemInstructions?: string; // Custom system instructions for the model
75
+ chatConfig?: ChatConfig; // Configuration object for the chat session
76
+ thinkingConfig?: ThinkingConfig; // Thinking features configuration (defaults to thinkingBudget: 0, thinkingLevel: "MINIMAL")
77
+ examplesFile?: string; // Path to JSON file containing transformation examples
78
+ exampleData?: TransformationExample[]; // Inline examples to seed the transformer
79
+ sourceKey?: string; // Key name for source data in examples (alias for promptKey)
80
+ targetKey?: string; // Key name for target data in examples (alias for answerKey)
81
+ promptKey?: string; // Key for the prompt in examples
82
+ answerKey?: string; // Key for the answer in examples
83
+ contextKey?: string; // Key name for context data in examples
84
+ explanationKey?: string; // Key name for explanation data in examples
85
+ systemInstructionsKey?: string; // Key for system instructions in examples
86
+ maxRetries?: number; // Maximum retry attempts for auto-retry functionality
87
+ retryDelay?: number; // Initial retry delay in milliseconds
88
+ // ? https://ai.google.dev/gemini-api/docs/structured-output
89
+ responseSchema?: Object; // Schema for validating model responses
90
+ apiKey?: string; // API key for Google GenAI
91
+ onlyJSON?: boolean; // If true, only JSON responses are allowed
92
+ asyncValidator?: AsyncValidatorFunction; // Optional async validator function for response validation
93
+ logLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal' | 'none'; // Log level for the logger (defaults to 'info', 'none' disables logging)
94
+ }
95
+
96
+ // Async validator function type
97
+ export type AsyncValidatorFunction = (payload: Record<string, unknown>) => Promise<unknown>;
98
+
99
+
100
+ export declare class AITransformer {
101
+ // Constructor
102
+ constructor(options?: AITransformerOptions);
103
+
104
+ // Properties
105
+ modelName: string;
106
+ promptKey: string;
107
+ answerKey: string;
108
+ contextKey: string;
109
+ explanationKey: string;
110
+ systemInstructionKey: string;
111
+ maxRetries: number;
112
+ retryDelay: number;
113
+ systemInstructions: string;
114
+ chatConfig: ChatConfig;
115
+ apiKey: string;
116
+ onlyJSON: boolean;
117
+ asyncValidator: AsyncValidatorFunction | null;
118
+ genAIClient: any;
119
+ chat: any;
120
+ logLevel: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal' | 'none';
121
+
122
+ // Methods
123
+ init(force?: boolean): Promise<void>;
124
+ seed(examples?: TransformationExample[]): Promise<any>;
125
+ message(payload: Record<string, unknown>, opts?: object, validatorFn?: AsyncValidatorFunction | null): Promise<Record<string, unknown>>;
126
+ rawMessage(sourcePayload: Record<string, unknown> | string): Promise<Record<string, unknown> | any>;
127
+ transformWithValidation(sourcePayload: Record<string, unknown>, validatorFn: AsyncValidatorFunction, options?: object): Promise<Record<string, unknown>>;
128
+ messageAndValidate(sourcePayload: Record<string, unknown>, validatorFn: AsyncValidatorFunction, options?: object): Promise<Record<string, unknown>>;
129
+ rebuild(lastPayload: Record<string, unknown>, serverError: string): Promise<Record<string, unknown>>;
130
+ reset(): Promise<void>;
131
+ getHistory(): Array<any>;
132
+ estimateTokenUsage(nextPayload: Record<string, unknown> | string): Promise<{ totalTokens: number; breakdown?: any }>;
133
+ estimate(nextPayload: Record<string, unknown> | string): Promise<{ totalTokens: number; breakdown?: any }>;
134
+ }
135
+
136
+ // Default export
137
+ declare const _default: typeof AITransformer;
138
+ export default _default;
package/types.ts DELETED
@@ -1,65 +0,0 @@
1
- import type { GoogleGenAI } from '@google/genai';
2
-
3
- export interface SafetySetting {
4
- category: string; // The harm category
5
- threshold: string; // The blocking threshold
6
- }
7
-
8
- export interface ChatConfig {
9
- responseMimeType?: string; // MIME type for responses
10
- temperature?: number; // Controls randomness (0.0 to 1.0)
11
- topP?: number; // Controls diversity via nucleus sampling
12
- topK?: number; // Controls diversity by limiting top-k tokens
13
- systemInstruction?: string; // System instruction for the model
14
- safetySettings?: SafetySetting[]; // Safety settings array
15
- responseSchema?: Object; // Schema for validating model responses
16
- }
17
-
18
- export interface AITransformerContext {
19
- modelName?: string;
20
- systemInstructions?: string;
21
- chatConfig?: ChatConfig;
22
- genAI?: any;
23
- chat?: any;
24
- examplesFile?: string | null;
25
- exampleData?: TransformationExample[] | null;
26
- promptKey?: string;
27
- answerKey?: string;
28
- contextKey?: string;
29
- maxRetries?: number;
30
- retryDelay?: number;
31
- init: () => Promise<void>; // Initialization function
32
- seed: () => Promise<void>; // Function to seed the transformer with examples
33
- message: (payload: Record<string, unknown>) => Promise<Record<string, unknown>>; // Function to send messages to the model
34
- genAIClient?: GoogleGenAI; // Google GenAI client instance
35
-
36
- }
37
-
38
- export interface TransformationExample {
39
- CONTEXT?: Record<string, unknown>; // optional context for the transformation
40
- PROMPT?: Record<string, unknown>; // what the user provides as input
41
- ANSWER?: Record<string, unknown>; // what the model should return as output
42
- }
43
-
44
- export interface ExampleFileContent {
45
- examples: TransformationExample[];
46
- }
47
-
48
- export interface AITransformerOptions {
49
- modelName?: string; // The Gemini model to use
50
- systemInstructions?: string; // Custom system instructions for the model
51
- chatConfig?: ChatConfig; // Configuration object for the chat session
52
- examplesFile?: string; // Path to JSON file containing transformation examples
53
- exampleData?: TransformationExample[]; // Inline examples to seed the transformer
54
- sourceKey?: string; // Key name for source data in examples
55
- targetKey?: string; // Key name for target data in examples
56
- contextKey?: string; // Key name for context data in examples
57
- maxRetries?: number; // Maximum retry attempts for auto-retry functionality
58
- retryDelay?: number; // Initial retry delay in milliseconds
59
- // ? https://ai.google.dev/gemini-api/docs/structured-output
60
- responseSchema?: Object; // Schema for validating model responses
61
- apiKey?: string; // API key for Google GenAI
62
- }
63
-
64
- // Async validator function type
65
- export type AsyncValidatorFunction = (payload: Record<string, unknown>) => Promise<Record<string, unknown>>;