ak-gemini 1.2.0 → 2.0.1
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.
- package/README.md +259 -294
- package/base.js +485 -0
- package/chat.js +87 -0
- package/code-agent.js +687 -0
- package/index.cjs +1928 -1213
- package/index.js +40 -1501
- package/json-helpers.js +352 -0
- package/message.js +170 -0
- package/package.json +14 -7
- package/tool-agent.js +312 -0
- package/transformer.js +502 -0
- package/types.d.ts +452 -241
- package/agent.js +0 -481
- package/tools.js +0 -134
package/types.d.ts
CHANGED
|
@@ -1,225 +1,386 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ThinkingLevel, HarmCategory, HarmBlockThreshold } from '@google/genai';
|
|
2
2
|
|
|
3
3
|
export { ThinkingLevel, HarmCategory, HarmBlockThreshold };
|
|
4
4
|
|
|
5
|
+
// ── Shared Types ─────────────────────────────────────────────────────────────
|
|
6
|
+
|
|
5
7
|
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
8
|
includeThoughts?: boolean;
|
|
8
|
-
/**
|
|
9
|
+
/** Token budget for thinking. 0 = disabled, -1 = automatic. */
|
|
9
10
|
thinkingBudget?: number;
|
|
10
|
-
/** Optional. The number of thoughts tokens that the model should generate. */
|
|
11
11
|
thinkingLevel?: ThinkingLevel;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
export interface SafetySetting {
|
|
15
|
-
category: HarmCategory;
|
|
16
|
-
threshold: HarmBlockThreshold;
|
|
15
|
+
category: HarmCategory;
|
|
16
|
+
threshold: HarmBlockThreshold;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
export interface ChatConfig {
|
|
20
|
-
responseMimeType?: string;
|
|
21
|
-
temperature?: number;
|
|
22
|
-
topP?: number;
|
|
23
|
-
topK?: number;
|
|
24
|
-
maxOutputTokens?: number;
|
|
25
|
-
systemInstruction?: string;
|
|
26
|
-
safetySettings?: SafetySetting[];
|
|
27
|
-
responseSchema?: Object;
|
|
28
|
-
thinkingConfig?: ThinkingConfig;
|
|
29
|
-
labels?: Record<string, string>;
|
|
30
|
-
tools?: any[];
|
|
31
|
-
toolConfig?: any;
|
|
32
|
-
[key: string]: any;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** Metadata from the last API response, useful for debugging and cost tracking */
|
|
20
|
+
responseMimeType?: string;
|
|
21
|
+
temperature?: number;
|
|
22
|
+
topP?: number;
|
|
23
|
+
topK?: number;
|
|
24
|
+
maxOutputTokens?: number;
|
|
25
|
+
systemInstruction?: string;
|
|
26
|
+
safetySettings?: SafetySetting[];
|
|
27
|
+
responseSchema?: Object;
|
|
28
|
+
thinkingConfig?: ThinkingConfig;
|
|
29
|
+
labels?: Record<string, string>;
|
|
30
|
+
tools?: any[];
|
|
31
|
+
toolConfig?: any;
|
|
32
|
+
[key: string]: any;
|
|
33
|
+
}
|
|
34
|
+
|
|
36
35
|
export interface ResponseMetadata {
|
|
37
|
-
modelVersion: string | null;
|
|
38
|
-
requestedModel: string;
|
|
39
|
-
promptTokens: number;
|
|
40
|
-
responseTokens: number;
|
|
41
|
-
totalTokens: number;
|
|
42
|
-
timestamp: number;
|
|
36
|
+
modelVersion: string | null;
|
|
37
|
+
requestedModel: string;
|
|
38
|
+
promptTokens: number;
|
|
39
|
+
responseTokens: number;
|
|
40
|
+
totalTokens: number;
|
|
41
|
+
timestamp: number;
|
|
43
42
|
}
|
|
44
43
|
|
|
45
|
-
/** Structured usage data returned by getLastUsage() for billing verification */
|
|
46
44
|
export interface UsageData {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
45
|
+
/** CUMULATIVE input tokens across all retry attempts */
|
|
46
|
+
promptTokens: number;
|
|
47
|
+
/** CUMULATIVE output tokens across all retry attempts */
|
|
48
|
+
responseTokens: number;
|
|
49
|
+
/** CUMULATIVE total tokens across all retry attempts */
|
|
50
|
+
totalTokens: number;
|
|
51
|
+
/** Number of attempts (1 = first try success, 2+ = retries needed) */
|
|
52
|
+
attempts: number;
|
|
53
|
+
/** Actual model that responded (e.g., 'gemini-2.5-flash-001') */
|
|
54
|
+
modelVersion: string | null;
|
|
55
|
+
/** Model you requested (e.g., 'gemini-2.5-flash') */
|
|
56
|
+
requestedModel: string;
|
|
57
|
+
timestamp: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface TransformationExample {
|
|
61
|
+
CONTEXT?: Record<string, unknown> | string;
|
|
62
|
+
PROMPT?: Record<string, unknown>;
|
|
63
|
+
ANSWER?: Record<string, unknown>;
|
|
64
|
+
INPUT?: Record<string, unknown>;
|
|
65
|
+
OUTPUT?: Record<string, unknown>;
|
|
66
|
+
SYSTEM?: string;
|
|
67
|
+
EXPLANATION?: string;
|
|
68
|
+
[key: string]: any;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface GoogleAuthOptions {
|
|
72
|
+
keyFilename?: string;
|
|
73
|
+
keyFile?: string;
|
|
74
|
+
credentials?: { client_email?: string; private_key?: string; [key: string]: any };
|
|
75
|
+
scopes?: string | string[];
|
|
76
|
+
projectId?: string;
|
|
77
|
+
universeDomain?: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export type AsyncValidatorFunction = (payload: Record<string, unknown>) => Promise<unknown>;
|
|
81
|
+
export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal' | 'none';
|
|
82
|
+
|
|
83
|
+
// ── Constructor Options ──────────────────────────────────────────────────────
|
|
84
|
+
|
|
85
|
+
export interface BaseGeminiOptions {
|
|
86
|
+
/** Gemini model to use (default: 'gemini-2.5-flash') */
|
|
67
87
|
modelName?: string;
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
88
|
+
/** System prompt for the model (null or false to disable) */
|
|
89
|
+
systemPrompt?: string | null | false;
|
|
90
|
+
/** Chat session configuration overrides */
|
|
91
|
+
chatConfig?: Partial<ChatConfig>;
|
|
92
|
+
/** Thinking features configuration */
|
|
93
|
+
thinkingConfig?: ThinkingConfig | null;
|
|
94
|
+
/** Maximum output tokens (default: 50000, null removes limit) */
|
|
95
|
+
maxOutputTokens?: number | null;
|
|
96
|
+
/** Log level (default: based on NODE_ENV) */
|
|
97
|
+
logLevel?: LogLevel;
|
|
98
|
+
|
|
99
|
+
// Authentication
|
|
100
|
+
/** API key for Gemini API */
|
|
101
|
+
apiKey?: string;
|
|
102
|
+
/** Use Vertex AI instead of Gemini API */
|
|
103
|
+
vertexai?: boolean;
|
|
104
|
+
/** Google Cloud project ID (required for Vertex AI) */
|
|
105
|
+
project?: string;
|
|
106
|
+
/** Google Cloud location/region */
|
|
107
|
+
location?: string;
|
|
108
|
+
/** Authentication options for Vertex AI */
|
|
109
|
+
googleAuthOptions?: GoogleAuthOptions;
|
|
110
|
+
|
|
111
|
+
/** Billing labels for cost segmentation (Vertex AI only) */
|
|
112
|
+
labels?: Record<string, string>;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export interface TransformerOptions extends BaseGeminiOptions {
|
|
116
|
+
/** Path to JSON file containing transformation examples */
|
|
117
|
+
examplesFile?: string;
|
|
118
|
+
/** Inline examples to seed the transformer */
|
|
119
|
+
exampleData?: TransformationExample[];
|
|
120
|
+
/** Key for source/input data in examples (default: 'PROMPT') */
|
|
121
|
+
sourceKey?: string;
|
|
122
|
+
/** Alias for sourceKey */
|
|
74
123
|
promptKey?: string;
|
|
124
|
+
/** Key for target/output data in examples (default: 'ANSWER') */
|
|
125
|
+
targetKey?: string;
|
|
126
|
+
/** Alias for targetKey */
|
|
75
127
|
answerKey?: string;
|
|
128
|
+
/** Key for context data in examples (default: 'CONTEXT') */
|
|
76
129
|
contextKey?: string;
|
|
130
|
+
/** Key for explanation data in examples (default: 'EXPLANATION') */
|
|
77
131
|
explanationKey?: string;
|
|
78
|
-
|
|
132
|
+
/** Key for system prompt overrides in examples (default: 'SYSTEM') */
|
|
133
|
+
systemPromptKey?: string;
|
|
134
|
+
/** Maximum retry attempts for validation failures (default: 3) */
|
|
79
135
|
maxRetries?: number;
|
|
136
|
+
/** Initial retry delay in milliseconds (default: 1000) */
|
|
80
137
|
retryDelay?: number;
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
enableGrounding?: boolean;
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
vertexai?: boolean; // Whether using Vertex AI (true) or Gemini API (false)
|
|
92
|
-
estimate?: (nextPayload: Record<string, unknown> | string) => Promise<{ inputTokens: number }>;
|
|
93
|
-
getLastUsage?: () => UsageData | null;
|
|
94
|
-
lastResponseMetadata?: ResponseMetadata | null; // Metadata from the last API response
|
|
95
|
-
exampleCount?: number; // Number of example history items from seed()
|
|
96
|
-
clearConversation?: () => Promise<void>; // Clears conversation history while preserving examples
|
|
97
|
-
_cumulativeUsage?: { promptTokens: number; responseTokens: number; totalTokens: number; attempts: number }; // Internal cumulative tracking
|
|
138
|
+
/** Schema for validating model responses */
|
|
139
|
+
responseSchema?: Object;
|
|
140
|
+
/** If true, only JSON responses are allowed (default: true) */
|
|
141
|
+
onlyJSON?: boolean;
|
|
142
|
+
/** Global async validator function for response validation */
|
|
143
|
+
asyncValidator?: AsyncValidatorFunction;
|
|
144
|
+
/** Enable Google Search grounding (WARNING: costs $35/1k queries) */
|
|
145
|
+
enableGrounding?: boolean;
|
|
146
|
+
/** Additional grounding configuration */
|
|
147
|
+
groundingConfig?: Record<string, any>;
|
|
98
148
|
}
|
|
99
149
|
|
|
100
|
-
export interface
|
|
101
|
-
|
|
102
|
-
PROMPT?: Record<string, unknown>; // what the user provides as input
|
|
103
|
-
ANSWER?: Record<string, unknown>; // what the model should return as output
|
|
104
|
-
INPUT?: Record<string, unknown>; // alias for PROMPT
|
|
105
|
-
OUTPUT?: Record<string, unknown>; // alias for ANSWER
|
|
106
|
-
SYSTEM?: string; // system instructions for this example
|
|
107
|
-
EXPLANATION?: string; // explanation for this example
|
|
108
|
-
[key: string]: any; // allow additional properties for flexible key mapping
|
|
150
|
+
export interface ChatOptions extends BaseGeminiOptions {
|
|
151
|
+
// Chat uses base options only — no additional fields needed
|
|
109
152
|
}
|
|
110
153
|
|
|
111
|
-
export interface
|
|
112
|
-
|
|
154
|
+
export interface MessageOptions extends BaseGeminiOptions {
|
|
155
|
+
/** Schema for structured output validation */
|
|
156
|
+
responseSchema?: Object;
|
|
157
|
+
/** MIME type for responses (e.g., 'application/json' for structured output) */
|
|
158
|
+
responseMimeType?: string;
|
|
113
159
|
}
|
|
114
160
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
asyncValidator?: AsyncValidatorFunction; // Optional async validator function for response validation
|
|
149
|
-
logLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal' | 'none'; // Log level for the logger (defaults to 'info', 'none' disables logging)
|
|
150
|
-
enableGrounding?: boolean; // Enable Google Search grounding (default: false, WARNING: costs $35/1k queries)
|
|
151
|
-
groundingConfig?: Record<string, any>; // Additional grounding configuration options
|
|
152
|
-
labels?: Record<string, string>; // Custom labels for billing segmentation
|
|
153
|
-
|
|
154
|
-
// Vertex AI Authentication Options
|
|
155
|
-
// Use these instead of apiKey for Vertex AI with service account authentication
|
|
156
|
-
vertexai?: boolean; // Set to true to use Vertex AI instead of Gemini API
|
|
157
|
-
project?: string; // Google Cloud project ID (required for Vertex AI)
|
|
158
|
-
location?: string; // Google Cloud location/region (e.g., 'us-central1'). If not set, defaults to 'global' endpoint
|
|
159
|
-
googleAuthOptions?: GoogleAuthOptions; // Authentication options for Vertex AI (keyFilename, credentials, etc.)
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// Async validator function type
|
|
163
|
-
export type AsyncValidatorFunction = (payload: Record<string, unknown>) => Promise<unknown>;
|
|
161
|
+
/** Tool declaration in @google/genai FunctionDeclaration format */
|
|
162
|
+
export interface ToolDeclaration {
|
|
163
|
+
name: string;
|
|
164
|
+
description: string;
|
|
165
|
+
parametersJsonSchema: {
|
|
166
|
+
type: string;
|
|
167
|
+
properties: Record<string, any>;
|
|
168
|
+
required?: string[];
|
|
169
|
+
[key: string]: any;
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export interface ToolAgentOptions extends BaseGeminiOptions {
|
|
174
|
+
/** Tool declarations for the model */
|
|
175
|
+
tools?: ToolDeclaration[];
|
|
176
|
+
/** Function to execute tool calls: (toolName, args) => result */
|
|
177
|
+
toolExecutor?: (toolName: string, args: Record<string, any>) => Promise<any>;
|
|
178
|
+
/** Max tool-use loop iterations (default: 10) */
|
|
179
|
+
maxToolRounds?: number;
|
|
180
|
+
/** Callback fired when a tool is called */
|
|
181
|
+
onToolCall?: (toolName: string, args: Record<string, any>) => void;
|
|
182
|
+
/** Async callback before tool execution; return false to deny */
|
|
183
|
+
onBeforeExecution?: (toolName: string, args: Record<string, any>) => Promise<boolean>;
|
|
184
|
+
/** Directory for tool-written files (pass-through for toolExecutor use) */
|
|
185
|
+
writeDir?: string;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export interface LocalDataEntry {
|
|
189
|
+
/** Label shown to the model (e.g. "users", "config") */
|
|
190
|
+
name: string;
|
|
191
|
+
/** Any JSON-serializable value */
|
|
192
|
+
data: any;
|
|
193
|
+
}
|
|
164
194
|
|
|
195
|
+
export interface RagAgentOptions extends BaseGeminiOptions {
|
|
196
|
+
/** Paths to files uploaded via Google Files API (PDFs, images, audio, video) */
|
|
197
|
+
remoteFiles?: string[];
|
|
198
|
+
/** Paths to local text files read from disk (md, json, csv, yaml, txt) */
|
|
199
|
+
localFiles?: string[];
|
|
200
|
+
/** In-memory data objects to include as context */
|
|
201
|
+
localData?: LocalDataEntry[];
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export interface CodeAgentOptions extends BaseGeminiOptions {
|
|
205
|
+
/** Working directory for code execution (default: process.cwd()) */
|
|
206
|
+
workingDirectory?: string;
|
|
207
|
+
/** Max code execution loop iterations (default: 10) */
|
|
208
|
+
maxRounds?: number;
|
|
209
|
+
/** Per-execution timeout in milliseconds (default: 30000) */
|
|
210
|
+
timeout?: number;
|
|
211
|
+
/** Async callback before code execution; return false to deny */
|
|
212
|
+
onBeforeExecution?: (code: string) => Promise<boolean>;
|
|
213
|
+
/** Notification callback after code execution */
|
|
214
|
+
onCodeExecution?: (code: string, output: { stdout: string; stderr: string; exitCode: number }) => void;
|
|
215
|
+
/** Files whose contents are included in the system prompt for project context */
|
|
216
|
+
importantFiles?: string[];
|
|
217
|
+
/** Directory for writing script files (default: '{workingDirectory}/tmp') */
|
|
218
|
+
writeDir?: string;
|
|
219
|
+
/** Keep script files on disk after execution (default: false) */
|
|
220
|
+
keepArtifacts?: boolean;
|
|
221
|
+
/** Instruct model to write JSDoc comments in generated code (default: false) */
|
|
222
|
+
comments?: boolean;
|
|
223
|
+
/** Max consecutive failed executions before stopping (default: 3) */
|
|
224
|
+
maxRetries?: number;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export interface CodeExecution {
|
|
228
|
+
/** The JavaScript code that was executed */
|
|
229
|
+
code: string;
|
|
230
|
+
/** Short slug describing the script's purpose */
|
|
231
|
+
purpose?: string;
|
|
232
|
+
/** stdout from the execution */
|
|
233
|
+
output: string;
|
|
234
|
+
/** stderr from the execution */
|
|
235
|
+
stderr: string;
|
|
236
|
+
/** Process exit code (0 = success) */
|
|
237
|
+
exitCode: number;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export interface CodeAgentResponse {
|
|
241
|
+
/** The agent's final text response */
|
|
242
|
+
text: string;
|
|
243
|
+
/** All code executions during this interaction */
|
|
244
|
+
codeExecutions: CodeExecution[];
|
|
245
|
+
/** Token usage data */
|
|
246
|
+
usage: UsageData | null;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export interface CodeAgentStreamEvent {
|
|
250
|
+
type: 'text' | 'code' | 'output' | 'done';
|
|
251
|
+
/** For 'text' events: the text chunk */
|
|
252
|
+
text?: string;
|
|
253
|
+
/** For 'code' events: the code about to be executed */
|
|
254
|
+
code?: string;
|
|
255
|
+
/** For 'output' events: stdout from execution */
|
|
256
|
+
stdout?: string;
|
|
257
|
+
/** For 'output' events: stderr from execution */
|
|
258
|
+
stderr?: string;
|
|
259
|
+
/** For 'output' events: process exit code */
|
|
260
|
+
exitCode?: number;
|
|
261
|
+
/** For 'done' events: complete accumulated text */
|
|
262
|
+
fullText?: string;
|
|
263
|
+
/** For 'done' events: all code executions */
|
|
264
|
+
codeExecutions?: CodeExecution[];
|
|
265
|
+
/** For 'done' events: token usage */
|
|
266
|
+
usage?: UsageData | null;
|
|
267
|
+
/** For 'done' events: e.g. "Max tool rounds reached" or "Agent was stopped" */
|
|
268
|
+
warning?: string;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// ── Per-Message Options ──────────────────────────────────────────────────────
|
|
272
|
+
|
|
273
|
+
export interface SendOptions {
|
|
274
|
+
/** Per-message billing labels */
|
|
275
|
+
labels?: Record<string, string>;
|
|
276
|
+
/** Send without affecting chat history (Transformer only) */
|
|
277
|
+
stateless?: boolean;
|
|
278
|
+
/** Override max retries for this message */
|
|
279
|
+
maxRetries?: number;
|
|
280
|
+
/** Override retry delay for this message */
|
|
281
|
+
retryDelay?: number;
|
|
282
|
+
/** Override grounding setting for this message */
|
|
283
|
+
enableGrounding?: boolean;
|
|
284
|
+
/** Override grounding config for this message */
|
|
285
|
+
groundingConfig?: Record<string, any>;
|
|
286
|
+
/** @internal Used to restore grounding state after per-message override */
|
|
287
|
+
_restoreGrounding?: () => Promise<void>;
|
|
288
|
+
[key: string]: any;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// ── Response Types ───────────────────────────────────────────────────────────
|
|
292
|
+
|
|
293
|
+
export interface ChatResponse {
|
|
294
|
+
/** The model's text response */
|
|
295
|
+
text: string;
|
|
296
|
+
/** Token usage data */
|
|
297
|
+
usage: UsageData | null;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export interface MessageResponse {
|
|
301
|
+
/** The model's text response */
|
|
302
|
+
text: string;
|
|
303
|
+
/** Parsed structured data (when responseSchema or responseMimeType is set) */
|
|
304
|
+
data?: any;
|
|
305
|
+
/** Token usage data */
|
|
306
|
+
usage: UsageData | null;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export interface RagResponse {
|
|
310
|
+
/** The model's text response */
|
|
311
|
+
text: string;
|
|
312
|
+
/** Token usage data */
|
|
313
|
+
usage: UsageData | null;
|
|
314
|
+
}
|
|
165
315
|
|
|
166
|
-
export
|
|
167
|
-
|
|
168
|
-
|
|
316
|
+
export interface RagStreamEvent {
|
|
317
|
+
type: 'text' | 'done';
|
|
318
|
+
/** For 'text' events: the text chunk */
|
|
319
|
+
text?: string;
|
|
320
|
+
/** For 'done' events: complete accumulated text */
|
|
321
|
+
fullText?: string;
|
|
322
|
+
/** For 'done' events: token usage */
|
|
323
|
+
usage?: UsageData | null;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
export interface AgentResponse {
|
|
327
|
+
/** The agent's final text response */
|
|
328
|
+
text: string;
|
|
329
|
+
/** All tool calls made during this interaction */
|
|
330
|
+
toolCalls: Array<{ name: string; args: Record<string, any>; result: any }>;
|
|
331
|
+
/** Token usage data */
|
|
332
|
+
usage: UsageData | null;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export interface AgentStreamEvent {
|
|
336
|
+
type: 'text' | 'tool_call' | 'tool_result' | 'done';
|
|
337
|
+
/** For 'text' events: the text chunk */
|
|
338
|
+
text?: string;
|
|
339
|
+
/** For 'tool_call' and 'tool_result' events */
|
|
340
|
+
toolName?: string;
|
|
341
|
+
/** For 'tool_call' events: the tool arguments */
|
|
342
|
+
args?: Record<string, any>;
|
|
343
|
+
/** For 'tool_result' events: the tool result */
|
|
344
|
+
result?: any;
|
|
345
|
+
/** For 'done' events: the complete accumulated text */
|
|
346
|
+
fullText?: string;
|
|
347
|
+
/** For 'done' events: token usage */
|
|
348
|
+
usage?: UsageData | null;
|
|
349
|
+
/** For 'done' events: e.g. "Max tool rounds reached" */
|
|
350
|
+
warning?: string;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// ── Seed Options ─────────────────────────────────────────────────────────────
|
|
354
|
+
|
|
355
|
+
export interface SeedOptions {
|
|
356
|
+
promptKey?: string;
|
|
357
|
+
answerKey?: string;
|
|
358
|
+
contextKey?: string;
|
|
359
|
+
explanationKey?: string;
|
|
360
|
+
systemPromptKey?: string;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// ── Class Declarations ───────────────────────────────────────────────────────
|
|
364
|
+
|
|
365
|
+
export declare class BaseGemini {
|
|
366
|
+
constructor(options?: BaseGeminiOptions);
|
|
169
367
|
|
|
170
|
-
// Properties
|
|
171
368
|
modelName: string;
|
|
172
|
-
|
|
173
|
-
answerKey: string;
|
|
174
|
-
contextKey: string;
|
|
175
|
-
explanationKey: string;
|
|
176
|
-
systemInstructionKey: string;
|
|
177
|
-
maxRetries: number;
|
|
178
|
-
retryDelay: number;
|
|
179
|
-
systemInstructions: string | null | false;
|
|
369
|
+
systemPrompt: string | null | false;
|
|
180
370
|
chatConfig: ChatConfig;
|
|
181
|
-
apiKey: string;
|
|
182
|
-
onlyJSON: boolean;
|
|
183
|
-
asyncValidator: AsyncValidatorFunction | null;
|
|
184
371
|
genAIClient: any;
|
|
185
|
-
|
|
186
|
-
logLevel: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal' | 'none';
|
|
187
|
-
enableGrounding: boolean;
|
|
188
|
-
groundingConfig: Record<string, any>;
|
|
189
|
-
labels: Record<string, string>;
|
|
190
|
-
vertexai: boolean;
|
|
191
|
-
/** Metadata from the last API response (model version, token counts, etc.) */
|
|
372
|
+
chatSession: any;
|
|
192
373
|
lastResponseMetadata: ResponseMetadata | null;
|
|
193
|
-
/** Number of history items that are seeded examples (used by clearConversation) */
|
|
194
374
|
exampleCount: number;
|
|
375
|
+
labels: Record<string, string>;
|
|
376
|
+
vertexai: boolean;
|
|
195
377
|
|
|
196
|
-
// Methods
|
|
197
378
|
init(force?: boolean): Promise<void>;
|
|
198
|
-
seed(examples?: TransformationExample[]): Promise<any>;
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
* @param opts - Options including { stateless: true } to send without affecting history
|
|
203
|
-
* @param validatorFn - Optional validator function
|
|
204
|
-
*/
|
|
205
|
-
message(payload: Record<string, unknown>, opts?: MessageOptions, validatorFn?: AsyncValidatorFunction | null): Promise<Record<string, unknown>>;
|
|
206
|
-
rawMessage(sourcePayload: Record<string, unknown> | string, messageOptions?: { labels?: Record<string, string> }): Promise<Record<string, unknown> | any>;
|
|
207
|
-
transformWithValidation(sourcePayload: Record<string, unknown>, options?: MessageOptions, validatorFn?: AsyncValidatorFunction | null): Promise<Record<string, unknown>>;
|
|
208
|
-
messageAndValidate(sourcePayload: Record<string, unknown>, options?: MessageOptions, validatorFn?: AsyncValidatorFunction | null): Promise<Record<string, unknown>>;
|
|
209
|
-
rebuild(lastPayload: Record<string, unknown>, serverError: string): Promise<Record<string, unknown>>;
|
|
210
|
-
reset(): Promise<void>;
|
|
211
|
-
getHistory(): Array<any>;
|
|
212
|
-
/**
|
|
213
|
-
* Estimate INPUT tokens only for a payload before sending.
|
|
214
|
-
* NOTE: Output tokens cannot be predicted before the API call.
|
|
215
|
-
* Use getLastUsage() after message() to see actual consumption.
|
|
216
|
-
*/
|
|
379
|
+
seed(examples?: TransformationExample[], opts?: SeedOptions): Promise<any[]>;
|
|
380
|
+
getHistory(curated?: boolean): any[];
|
|
381
|
+
clearHistory(): Promise<void>;
|
|
382
|
+
getLastUsage(): UsageData | null;
|
|
217
383
|
estimate(nextPayload: Record<string, unknown> | string): Promise<{ inputTokens: number }>;
|
|
218
|
-
updateSystemInstructions(newInstructions: string): Promise<void>;
|
|
219
|
-
/**
|
|
220
|
-
* Estimates the INPUT cost of sending a payload.
|
|
221
|
-
* NOTE: Output cost depends on response length and cannot be predicted.
|
|
222
|
-
*/
|
|
223
384
|
estimateCost(nextPayload: Record<string, unknown> | string): Promise<{
|
|
224
385
|
inputTokens: number;
|
|
225
386
|
model: string;
|
|
@@ -227,76 +388,126 @@ export declare class AITransformer {
|
|
|
227
388
|
estimatedInputCost: number;
|
|
228
389
|
note: string;
|
|
229
390
|
}>;
|
|
230
|
-
/** Clears conversation history while preserving seeded examples */
|
|
231
|
-
clearConversation(): Promise<void>;
|
|
232
|
-
/**
|
|
233
|
-
* Returns structured usage data from the last API response for billing verification.
|
|
234
|
-
* Returns null if no API call has been made yet.
|
|
235
|
-
*/
|
|
236
|
-
getLastUsage(): UsageData | null;
|
|
237
391
|
}
|
|
238
392
|
|
|
239
|
-
|
|
393
|
+
export declare class Transformer extends BaseGemini {
|
|
394
|
+
constructor(options?: TransformerOptions);
|
|
240
395
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
396
|
+
promptKey: string;
|
|
397
|
+
answerKey: string;
|
|
398
|
+
contextKey: string;
|
|
399
|
+
explanationKey: string;
|
|
400
|
+
onlyJSON: boolean;
|
|
401
|
+
asyncValidator: AsyncValidatorFunction | null;
|
|
402
|
+
maxRetries: number;
|
|
403
|
+
retryDelay: number;
|
|
404
|
+
enableGrounding: boolean;
|
|
248
405
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
labels?: Record<string, string>; // Billing labels (Vertex AI)
|
|
257
|
-
thinkingConfig?: ThinkingConfig;
|
|
258
|
-
chatConfig?: Partial<ChatConfig>;
|
|
406
|
+
seed(examples?: TransformationExample[]): Promise<any[]>;
|
|
407
|
+
send(payload: Record<string, unknown> | string, opts?: SendOptions, validatorFn?: AsyncValidatorFunction | null): Promise<Record<string, unknown>>;
|
|
408
|
+
rawSend(payload: Record<string, unknown> | string, messageOptions?: { labels?: Record<string, string> }): Promise<Record<string, unknown>>;
|
|
409
|
+
rebuild(lastPayload: Record<string, unknown>, serverError: string): Promise<Record<string, unknown>>;
|
|
410
|
+
reset(): Promise<void>;
|
|
411
|
+
updateSystemPrompt(newPrompt: string): Promise<void>;
|
|
412
|
+
}
|
|
259
413
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
414
|
+
export declare class Chat extends BaseGemini {
|
|
415
|
+
constructor(options?: ChatOptions);
|
|
416
|
+
|
|
417
|
+
send(message: string, opts?: { labels?: Record<string, string> }): Promise<ChatResponse>;
|
|
263
418
|
}
|
|
264
419
|
|
|
265
|
-
export
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
420
|
+
export declare class Message extends BaseGemini {
|
|
421
|
+
constructor(options?: MessageOptions);
|
|
422
|
+
|
|
423
|
+
init(force?: boolean): Promise<void>;
|
|
424
|
+
send(payload: Record<string, unknown> | string, opts?: { labels?: Record<string, string> }): Promise<MessageResponse>;
|
|
270
425
|
}
|
|
271
426
|
|
|
272
|
-
export
|
|
273
|
-
|
|
274
|
-
text?: string; // For 'text' events: the text chunk
|
|
275
|
-
toolName?: string; // For 'tool_call' and 'tool_result' events
|
|
276
|
-
args?: Record<string, any>; // For 'tool_call' events: the tool arguments
|
|
277
|
-
result?: any; // For 'tool_result' events: the tool result
|
|
278
|
-
filename?: string; // For 'markdown' events: the suggested filename
|
|
279
|
-
content?: string; // For 'markdown' events: the markdown content
|
|
280
|
-
fullText?: string; // For 'done' events: the complete accumulated text
|
|
281
|
-
usage?: UsageData | null; // For 'done' events: token usage
|
|
282
|
-
markdownFiles?: Array<{ filename: string; content: string }>; // For 'done' events
|
|
283
|
-
warning?: string; // For 'done' events: e.g. "Max tool rounds reached"
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
export declare class AIAgent {
|
|
287
|
-
constructor(options?: AIAgentOptions);
|
|
427
|
+
export declare class ToolAgent extends BaseGemini {
|
|
428
|
+
constructor(options?: ToolAgentOptions);
|
|
288
429
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
430
|
+
tools: ToolDeclaration[];
|
|
431
|
+
toolExecutor: ((toolName: string, args: Record<string, any>) => Promise<any>) | null;
|
|
432
|
+
maxToolRounds: number;
|
|
433
|
+
onToolCall: ((toolName: string, args: Record<string, any>) => void) | null;
|
|
434
|
+
onBeforeExecution: ((toolName: string, args: Record<string, any>) => Promise<boolean>) | null;
|
|
435
|
+
/** Directory for tool-written files (pass-through for toolExecutor use) */
|
|
436
|
+
writeDir: string | null;
|
|
292
437
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
438
|
+
chat(message: string, opts?: { labels?: Record<string, string> }): Promise<AgentResponse>;
|
|
439
|
+
stream(message: string, opts?: { labels?: Record<string, string> }): AsyncGenerator<AgentStreamEvent, void, unknown>;
|
|
440
|
+
/** Stop the agent before the next tool execution round */
|
|
441
|
+
stop(): void;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
export declare class RagAgent extends BaseGemini {
|
|
445
|
+
constructor(options?: RagAgentOptions);
|
|
446
|
+
|
|
447
|
+
/** Paths to files uploaded via Google Files API */
|
|
448
|
+
remoteFiles: string[];
|
|
449
|
+
/** Paths to local text files read from disk */
|
|
450
|
+
localFiles: string[];
|
|
451
|
+
/** In-memory data objects */
|
|
452
|
+
localData: LocalDataEntry[];
|
|
453
|
+
|
|
454
|
+
init(force?: boolean): Promise<void>;
|
|
455
|
+
chat(message: string, opts?: { labels?: Record<string, string> }): Promise<RagResponse>;
|
|
456
|
+
stream(message: string, opts?: { labels?: Record<string, string> }): AsyncGenerator<RagStreamEvent, void, unknown>;
|
|
457
|
+
/** Add remote files uploaded via Files API (triggers reinitialize) */
|
|
458
|
+
addRemoteFiles(paths: string[]): Promise<void>;
|
|
459
|
+
/** Add local text files read from disk (triggers reinitialize) */
|
|
460
|
+
addLocalFiles(paths: string[]): Promise<void>;
|
|
461
|
+
/** Add in-memory data entries (triggers reinitialize) */
|
|
462
|
+
addLocalData(entries: LocalDataEntry[]): Promise<void>;
|
|
463
|
+
/** Returns metadata about all context sources */
|
|
464
|
+
getContext(): {
|
|
465
|
+
remoteFiles: Array<{ name: string; displayName: string; mimeType: string; sizeBytes: string; uri: string; originalPath: string }>;
|
|
466
|
+
localFiles: Array<{ name: string; path: string; size: number }>;
|
|
467
|
+
localData: Array<{ name: string; type: string }>;
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
export declare class CodeAgent extends BaseGemini {
|
|
472
|
+
constructor(options?: CodeAgentOptions);
|
|
473
|
+
|
|
474
|
+
workingDirectory: string;
|
|
475
|
+
maxRounds: number;
|
|
476
|
+
timeout: number;
|
|
477
|
+
onBeforeExecution: ((code: string) => Promise<boolean>) | null;
|
|
478
|
+
onCodeExecution: ((code: string, output: { stdout: string; stderr: string; exitCode: number }) => void) | null;
|
|
479
|
+
/** Files whose contents are included in the system prompt */
|
|
480
|
+
importantFiles: string[];
|
|
481
|
+
/** Directory for writing script files */
|
|
482
|
+
writeDir: string;
|
|
483
|
+
/** Keep script files on disk after execution */
|
|
484
|
+
keepArtifacts: boolean;
|
|
485
|
+
/** Whether the model writes comments in generated code */
|
|
486
|
+
comments: boolean;
|
|
487
|
+
/** Max consecutive failed executions before stopping */
|
|
488
|
+
maxRetries: number;
|
|
489
|
+
|
|
490
|
+
init(force?: boolean): Promise<void>;
|
|
491
|
+
chat(message: string, opts?: { labels?: Record<string, string> }): Promise<CodeAgentResponse>;
|
|
492
|
+
stream(message: string, opts?: { labels?: Record<string, string> }): AsyncGenerator<CodeAgentStreamEvent, void, unknown>;
|
|
493
|
+
/** Returns all code scripts written across all chat/stream calls. */
|
|
494
|
+
dump(): Array<{ fileName: string; purpose: string | null; script: string; filePath: string | null }>;
|
|
495
|
+
/** Stop the agent before the next code execution. Kills any running child process. */
|
|
496
|
+
stop(): void;
|
|
299
497
|
}
|
|
300
498
|
|
|
301
|
-
//
|
|
302
|
-
|
|
499
|
+
// ── Module Exports ───────────────────────────────────────────────────────────
|
|
500
|
+
|
|
501
|
+
export declare function extractJSON(text: string): any;
|
|
502
|
+
export declare function attemptJSONRecovery(text: string, maxAttempts?: number): any | null;
|
|
503
|
+
|
|
504
|
+
declare const _default: {
|
|
505
|
+
Transformer: typeof Transformer;
|
|
506
|
+
Chat: typeof Chat;
|
|
507
|
+
Message: typeof Message;
|
|
508
|
+
ToolAgent: typeof ToolAgent;
|
|
509
|
+
CodeAgent: typeof CodeAgent;
|
|
510
|
+
RagAgent: typeof RagAgent;
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
export default _default;
|