geminisdk 0.1.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/src/types.ts ADDED
@@ -0,0 +1,352 @@
1
+ /**
2
+ * Type definitions for GeminiSDK TypeScript
3
+ *
4
+ * Based on:
5
+ * - GitHub Copilot SDK types
6
+ * - Google Gemini CLI implementation
7
+ */
8
+
9
+ // =============================================================================
10
+ // Connection and Session Types
11
+ // =============================================================================
12
+
13
+ export type ConnectionState = 'disconnected' | 'connecting' | 'connected' | 'error';
14
+ export type LogLevel = 'none' | 'error' | 'warning' | 'info' | 'debug' | 'all';
15
+
16
+ export enum Role {
17
+ USER = 'user',
18
+ ASSISTANT = 'assistant',
19
+ SYSTEM = 'system',
20
+ }
21
+
22
+ // =============================================================================
23
+ // OAuth and Authentication Types
24
+ // =============================================================================
25
+
26
+ export interface GeminiOAuthCredentials {
27
+ accessToken: string;
28
+ refreshToken: string;
29
+ tokenType: string;
30
+ expiryDate: number; // Timestamp in milliseconds
31
+ }
32
+
33
+ // =============================================================================
34
+ // Model Types
35
+ // =============================================================================
36
+
37
+ export interface GeminiModelInfo {
38
+ id: string;
39
+ name: string;
40
+ contextWindow: number;
41
+ maxOutput: number;
42
+ inputPrice: number;
43
+ outputPrice: number;
44
+ supportsNativeTools: boolean;
45
+ supportsThinking: boolean;
46
+ }
47
+
48
+ export interface ModelVisionLimits {
49
+ supportedMediaTypes?: string[];
50
+ maxPromptImages?: number;
51
+ maxPromptImageSize?: number;
52
+ }
53
+
54
+ export interface ModelLimits {
55
+ maxPromptTokens?: number;
56
+ maxContextWindowTokens?: number;
57
+ vision?: ModelVisionLimits;
58
+ }
59
+
60
+ export interface ModelSupports {
61
+ vision: boolean;
62
+ tools: boolean;
63
+ thinking: boolean;
64
+ }
65
+
66
+ export interface ModelCapabilities {
67
+ supports: ModelSupports;
68
+ limits: ModelLimits;
69
+ }
70
+
71
+ export interface ModelInfo {
72
+ id: string;
73
+ name: string;
74
+ capabilities: ModelCapabilities;
75
+ }
76
+
77
+ // =============================================================================
78
+ // Message and Content Types
79
+ // =============================================================================
80
+
81
+ export interface ContentPart {
82
+ text?: string;
83
+ imageUrl?: string;
84
+ imageData?: Uint8Array;
85
+ imageMimeType?: string;
86
+ }
87
+
88
+ export interface Message {
89
+ role: Role;
90
+ content: string | ContentPart[];
91
+ name?: string;
92
+ toolCalls?: ToolCall[];
93
+ toolCallId?: string;
94
+ }
95
+
96
+ export interface Attachment {
97
+ type: 'file' | 'image';
98
+ path?: string;
99
+ url?: string;
100
+ data?: string; // base64 encoded
101
+ mimeType?: string;
102
+ }
103
+
104
+ // =============================================================================
105
+ // Tool Types
106
+ // =============================================================================
107
+
108
+ export interface FunctionCall {
109
+ name: string;
110
+ arguments: Record<string, unknown> | string;
111
+ }
112
+
113
+ export interface ToolCall {
114
+ id: string;
115
+ type: 'function';
116
+ function: FunctionCall;
117
+ }
118
+
119
+ export interface ToolInvocation {
120
+ name: string;
121
+ arguments: Record<string, unknown>;
122
+ callId: string;
123
+ }
124
+
125
+ export type ToolResultType = 'success' | 'failure' | 'rejected' | 'denied';
126
+
127
+ export interface ToolResult {
128
+ resultType?: ToolResultType;
129
+ textResultForLlm?: string;
130
+ binaryResult?: Uint8Array;
131
+ sessionLog?: string;
132
+ }
133
+
134
+ export type ToolHandler = (
135
+ invocation: ToolInvocation
136
+ ) => ToolResult | Promise<ToolResult>;
137
+
138
+ export interface Tool {
139
+ name: string;
140
+ description: string;
141
+ parameters?: Record<string, unknown>;
142
+ handler?: ToolHandler;
143
+ }
144
+
145
+ // =============================================================================
146
+ // Generation Config Types
147
+ // =============================================================================
148
+
149
+ export interface GenerationConfig {
150
+ temperature?: number;
151
+ maxOutputTokens?: number;
152
+ topP?: number;
153
+ topK?: number;
154
+ stopSequences?: string[];
155
+ }
156
+
157
+ export interface ThinkingConfig {
158
+ includeThoughts?: boolean;
159
+ thinkingBudget?: number;
160
+ }
161
+
162
+ // =============================================================================
163
+ // Request/Response Types
164
+ // =============================================================================
165
+
166
+ export interface MessageOptions {
167
+ prompt: string;
168
+ attachments?: Attachment[];
169
+ context?: string;
170
+ }
171
+
172
+ export interface LLMUsage {
173
+ promptTokens: number;
174
+ completionTokens: number;
175
+ totalTokens: number;
176
+ }
177
+
178
+ export interface LLMChunk {
179
+ content: string;
180
+ reasoningContent?: string;
181
+ toolCalls?: ToolCall[];
182
+ usage?: LLMUsage;
183
+ finishReason?: string;
184
+ }
185
+
186
+ // =============================================================================
187
+ // Session Types
188
+ // =============================================================================
189
+
190
+ export interface SessionConfig {
191
+ sessionId?: string;
192
+ model?: string;
193
+ tools?: Tool[];
194
+ systemMessage?: string;
195
+ generationConfig?: GenerationConfig;
196
+ thinkingConfig?: ThinkingConfig;
197
+ streaming?: boolean;
198
+ }
199
+
200
+ export interface SessionMetadata {
201
+ sessionId: string;
202
+ startTime: string;
203
+ modifiedTime: string;
204
+ summary?: string;
205
+ model: string;
206
+ }
207
+
208
+ // =============================================================================
209
+ // Client Options Types
210
+ // =============================================================================
211
+
212
+ export interface GeminiClientOptions {
213
+ oauthPath?: string;
214
+ clientId?: string;
215
+ clientSecret?: string;
216
+ baseUrl?: string;
217
+ timeout?: number;
218
+ logLevel?: LogLevel;
219
+ autoRefresh?: boolean;
220
+ }
221
+
222
+ // =============================================================================
223
+ // Event Types
224
+ // =============================================================================
225
+
226
+ export enum EventType {
227
+ SESSION_CREATED = 'session.created',
228
+ SESSION_IDLE = 'session.idle',
229
+ SESSION_ERROR = 'session.error',
230
+ ASSISTANT_MESSAGE = 'assistant.message',
231
+ ASSISTANT_MESSAGE_DELTA = 'assistant.message_delta',
232
+ ASSISTANT_REASONING = 'assistant.reasoning',
233
+ ASSISTANT_REASONING_DELTA = 'assistant.reasoning_delta',
234
+ TOOL_CALL = 'tool.call',
235
+ TOOL_RESULT = 'tool.result',
236
+ }
237
+
238
+ export interface SessionEvent {
239
+ type: EventType;
240
+ data: unknown;
241
+ sessionId: string;
242
+ }
243
+
244
+ export type SessionEventHandler = (event: SessionEvent) => void;
245
+
246
+ // =============================================================================
247
+ // Constants
248
+ // =============================================================================
249
+
250
+ export const GEMINI_OAUTH_REDIRECT_URI = 'http://localhost:45289';
251
+ export const GEMINI_OAUTH_BASE_URL = 'https://accounts.google.com';
252
+ export const GEMINI_OAUTH_TOKEN_ENDPOINT = `${GEMINI_OAUTH_BASE_URL}/o/oauth2/token`;
253
+ export const GEMINI_OAUTH_AUTH_ENDPOINT = `${GEMINI_OAUTH_BASE_URL}/o/oauth2/v2/auth`;
254
+
255
+ // Official Google OAuth client credentials for Gemini CLI
256
+ export const GEMINI_OAUTH_CLIENT_ID =
257
+ '681255809395-oo8ft2oprdrnp9e3aqf6av3hmdib135j.apps.googleusercontent.com';
258
+ export const GEMINI_OAUTH_CLIENT_SECRET = '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';
259
+
260
+ export const GEMINI_OAUTH_SCOPES = [
261
+ 'https://www.googleapis.com/auth/cloud-platform',
262
+ 'https://www.googleapis.com/auth/userinfo.email',
263
+ 'https://www.googleapis.com/auth/userinfo.profile',
264
+ ];
265
+
266
+ export const GEMINI_CODE_ASSIST_ENDPOINT = 'https://cloudcode-pa.googleapis.com';
267
+ export const GEMINI_CODE_ASSIST_API_VERSION = 'v1internal';
268
+
269
+ export const GEMINI_DIR = '.gemini';
270
+ export const GEMINI_CREDENTIAL_FILENAME = 'oauth_creds.json';
271
+ export const GEMINI_ENV_FILENAME = '.env';
272
+
273
+ export const TOKEN_REFRESH_BUFFER_MS = 5 * 60 * 1000;
274
+
275
+ export const HTTP_OK = 200;
276
+ export const HTTP_UNAUTHORIZED = 401;
277
+ export const HTTP_FORBIDDEN = 403;
278
+
279
+ export const GEMINI_CLI_MODELS: Record<string, GeminiModelInfo> = {
280
+ 'gemini-3-pro-preview': {
281
+ id: 'gemini-3-pro-preview',
282
+ name: 'Gemini 3 Pro Preview',
283
+ contextWindow: 1_000_000,
284
+ maxOutput: 65_536,
285
+ inputPrice: 0.0,
286
+ outputPrice: 0.0,
287
+ supportsNativeTools: true,
288
+ supportsThinking: true,
289
+ },
290
+ 'gemini-3-flash-preview': {
291
+ id: 'gemini-3-flash-preview',
292
+ name: 'Gemini 3 Flash Preview',
293
+ contextWindow: 1_000_000,
294
+ maxOutput: 65_536,
295
+ inputPrice: 0.0,
296
+ outputPrice: 0.0,
297
+ supportsNativeTools: true,
298
+ supportsThinking: true,
299
+ },
300
+ 'gemini-2.5-pro': {
301
+ id: 'gemini-2.5-pro',
302
+ name: 'Gemini 2.5 Pro',
303
+ contextWindow: 1_048_576,
304
+ maxOutput: 65_536,
305
+ inputPrice: 0.0,
306
+ outputPrice: 0.0,
307
+ supportsNativeTools: true,
308
+ supportsThinking: true,
309
+ },
310
+ 'gemini-2.5-flash': {
311
+ id: 'gemini-2.5-flash',
312
+ name: 'Gemini 2.5 Flash',
313
+ contextWindow: 1_048_576,
314
+ maxOutput: 65_536,
315
+ inputPrice: 0.0,
316
+ outputPrice: 0.0,
317
+ supportsNativeTools: true,
318
+ supportsThinking: true,
319
+ },
320
+ 'gemini-2.5-flash-lite': {
321
+ id: 'gemini-2.5-flash-lite',
322
+ name: 'Gemini 2.5 Flash Lite',
323
+ contextWindow: 1_000_000,
324
+ maxOutput: 32_768,
325
+ inputPrice: 0.0,
326
+ outputPrice: 0.0,
327
+ supportsNativeTools: true,
328
+ supportsThinking: false,
329
+ },
330
+ auto: {
331
+ id: 'auto',
332
+ name: 'Auto (Default)',
333
+ contextWindow: 1_048_576,
334
+ maxOutput: 65_536,
335
+ inputPrice: 0.0,
336
+ outputPrice: 0.0,
337
+ supportsNativeTools: true,
338
+ supportsThinking: true,
339
+ },
340
+ };
341
+
342
+ export function getGeminiCliCredentialPath(customPath?: string): string {
343
+ if (customPath) return customPath;
344
+ const home = process.env['HOME'] ?? process.env['USERPROFILE'] ?? '';
345
+ return `${home}/${GEMINI_DIR}/${GEMINI_CREDENTIAL_FILENAME}`;
346
+ }
347
+
348
+ export function getGeminiCliEnvPath(customPath?: string): string {
349
+ if (customPath) return customPath;
350
+ const home = process.env['HOME'] ?? process.env['USERPROFILE'] ?? '';
351
+ return `${home}/${GEMINI_DIR}/${GEMINI_ENV_FILENAME}`;
352
+ }