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/README.md +129 -0
- package/dist/chunk-Y4MC6YDW.mjs +135 -0
- package/dist/index.d.mts +511 -0
- package/dist/index.d.ts +511 -0
- package/dist/index.js +1696 -0
- package/dist/index.mjs +1480 -0
- package/dist/types-ADTG4FSI.mjs +46 -0
- package/package.json +60 -0
- package/src/auth.ts +293 -0
- package/src/backend.ts +615 -0
- package/src/client.ts +230 -0
- package/src/exceptions.ts +289 -0
- package/src/index.ts +148 -0
- package/src/session.ts +380 -0
- package/src/tools.ts +127 -0
- package/src/types.ts +352 -0
package/README.md
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# GeminiSDK TypeScript
|
|
2
|
+
|
|
3
|
+
TypeScript SDK for Google Gemini Code Assist API - similar to GitHub Copilot SDK.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install geminisdk
|
|
9
|
+
# or
|
|
10
|
+
yarn add geminisdk
|
|
11
|
+
# or
|
|
12
|
+
pnpm add geminisdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Prerequisites
|
|
16
|
+
|
|
17
|
+
Before using this SDK, you need to authenticate with Gemini CLI:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install -g @google/gemini-cli
|
|
21
|
+
gemini auth login
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { GeminiClient, EventType } from 'geminisdk';
|
|
28
|
+
|
|
29
|
+
async function main() {
|
|
30
|
+
const client = new GeminiClient();
|
|
31
|
+
|
|
32
|
+
const session = await client.createSession({
|
|
33
|
+
model: 'gemini-2.5-pro',
|
|
34
|
+
streaming: true,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
session.on((event) => {
|
|
38
|
+
if (event.type === EventType.ASSISTANT_MESSAGE_DELTA) {
|
|
39
|
+
process.stdout.write((event.data as any).deltaContent);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
await session.send({ prompt: 'What is TypeScript?' });
|
|
44
|
+
await client.close();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
main();
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Features
|
|
51
|
+
|
|
52
|
+
- 🔐 **OAuth Authentication** - Seamless auth using Gemini CLI credentials
|
|
53
|
+
- 🌊 **Streaming Responses** - Real-time streaming with SSE
|
|
54
|
+
- 🛠️ **Tool Calling** - Define and use custom tools
|
|
55
|
+
- 💬 **Session Management** - Manage conversation state
|
|
56
|
+
- 🧠 **Thinking/Reasoning** - Support for model reasoning
|
|
57
|
+
|
|
58
|
+
## API Reference
|
|
59
|
+
|
|
60
|
+
### GeminiClient
|
|
61
|
+
|
|
62
|
+
Main client for interacting with the API.
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
const client = new GeminiClient({
|
|
66
|
+
timeout: 720000, // Request timeout
|
|
67
|
+
autoRefresh: true, // Auto-refresh tokens
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
await client.start();
|
|
71
|
+
const session = await client.createSession({ model: 'gemini-2.5-pro' });
|
|
72
|
+
await client.close();
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### GeminiSession
|
|
76
|
+
|
|
77
|
+
Manages conversation sessions.
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
const session = await client.createSession({
|
|
81
|
+
model: 'gemini-2.5-pro',
|
|
82
|
+
streaming: true,
|
|
83
|
+
systemMessage: 'You are a helpful assistant.',
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// Subscribe to events
|
|
87
|
+
session.on((event) => {
|
|
88
|
+
console.log(event.type, event.data);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Send message
|
|
92
|
+
await session.send({ prompt: 'Hello!' });
|
|
93
|
+
|
|
94
|
+
// Or send and wait
|
|
95
|
+
const response = await session.sendAndWait({ prompt: 'Hello!' });
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Tools
|
|
99
|
+
|
|
100
|
+
Define custom tools for the model to use.
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
import { defineTool, createTool } from 'geminisdk';
|
|
104
|
+
|
|
105
|
+
const weatherTool = defineTool(
|
|
106
|
+
{
|
|
107
|
+
name: 'get_weather',
|
|
108
|
+
description: 'Get current weather',
|
|
109
|
+
parameters: {
|
|
110
|
+
type: 'object',
|
|
111
|
+
properties: {
|
|
112
|
+
city: { type: 'string', description: 'City name' },
|
|
113
|
+
},
|
|
114
|
+
required: ['city'],
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
async (args) => {
|
|
118
|
+
return `Weather in ${args.city}: Sunny, 72°F`;
|
|
119
|
+
}
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
const session = await client.createSession({
|
|
123
|
+
tools: [weatherTool],
|
|
124
|
+
});
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## License
|
|
128
|
+
|
|
129
|
+
MIT
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// src/types.ts
|
|
2
|
+
var Role = /* @__PURE__ */ ((Role2) => {
|
|
3
|
+
Role2["USER"] = "user";
|
|
4
|
+
Role2["ASSISTANT"] = "assistant";
|
|
5
|
+
Role2["SYSTEM"] = "system";
|
|
6
|
+
return Role2;
|
|
7
|
+
})(Role || {});
|
|
8
|
+
var EventType = /* @__PURE__ */ ((EventType2) => {
|
|
9
|
+
EventType2["SESSION_CREATED"] = "session.created";
|
|
10
|
+
EventType2["SESSION_IDLE"] = "session.idle";
|
|
11
|
+
EventType2["SESSION_ERROR"] = "session.error";
|
|
12
|
+
EventType2["ASSISTANT_MESSAGE"] = "assistant.message";
|
|
13
|
+
EventType2["ASSISTANT_MESSAGE_DELTA"] = "assistant.message_delta";
|
|
14
|
+
EventType2["ASSISTANT_REASONING"] = "assistant.reasoning";
|
|
15
|
+
EventType2["ASSISTANT_REASONING_DELTA"] = "assistant.reasoning_delta";
|
|
16
|
+
EventType2["TOOL_CALL"] = "tool.call";
|
|
17
|
+
EventType2["TOOL_RESULT"] = "tool.result";
|
|
18
|
+
return EventType2;
|
|
19
|
+
})(EventType || {});
|
|
20
|
+
var GEMINI_OAUTH_REDIRECT_URI = "http://localhost:45289";
|
|
21
|
+
var GEMINI_OAUTH_BASE_URL = "https://accounts.google.com";
|
|
22
|
+
var GEMINI_OAUTH_TOKEN_ENDPOINT = `${GEMINI_OAUTH_BASE_URL}/o/oauth2/token`;
|
|
23
|
+
var GEMINI_OAUTH_AUTH_ENDPOINT = `${GEMINI_OAUTH_BASE_URL}/o/oauth2/v2/auth`;
|
|
24
|
+
var GEMINI_OAUTH_CLIENT_ID = "681255809395-oo8ft2oprdrnp9e3aqf6av3hmdib135j.apps.googleusercontent.com";
|
|
25
|
+
var GEMINI_OAUTH_CLIENT_SECRET = "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";
|
|
26
|
+
var GEMINI_OAUTH_SCOPES = [
|
|
27
|
+
"https://www.googleapis.com/auth/cloud-platform",
|
|
28
|
+
"https://www.googleapis.com/auth/userinfo.email",
|
|
29
|
+
"https://www.googleapis.com/auth/userinfo.profile"
|
|
30
|
+
];
|
|
31
|
+
var GEMINI_CODE_ASSIST_ENDPOINT = "https://cloudcode-pa.googleapis.com";
|
|
32
|
+
var GEMINI_CODE_ASSIST_API_VERSION = "v1internal";
|
|
33
|
+
var GEMINI_DIR = ".gemini";
|
|
34
|
+
var GEMINI_CREDENTIAL_FILENAME = "oauth_creds.json";
|
|
35
|
+
var GEMINI_ENV_FILENAME = ".env";
|
|
36
|
+
var TOKEN_REFRESH_BUFFER_MS = 5 * 60 * 1e3;
|
|
37
|
+
var HTTP_OK = 200;
|
|
38
|
+
var HTTP_UNAUTHORIZED = 401;
|
|
39
|
+
var HTTP_FORBIDDEN = 403;
|
|
40
|
+
var GEMINI_CLI_MODELS = {
|
|
41
|
+
"gemini-3-pro-preview": {
|
|
42
|
+
id: "gemini-3-pro-preview",
|
|
43
|
+
name: "Gemini 3 Pro Preview",
|
|
44
|
+
contextWindow: 1e6,
|
|
45
|
+
maxOutput: 65536,
|
|
46
|
+
inputPrice: 0,
|
|
47
|
+
outputPrice: 0,
|
|
48
|
+
supportsNativeTools: true,
|
|
49
|
+
supportsThinking: true
|
|
50
|
+
},
|
|
51
|
+
"gemini-3-flash-preview": {
|
|
52
|
+
id: "gemini-3-flash-preview",
|
|
53
|
+
name: "Gemini 3 Flash Preview",
|
|
54
|
+
contextWindow: 1e6,
|
|
55
|
+
maxOutput: 65536,
|
|
56
|
+
inputPrice: 0,
|
|
57
|
+
outputPrice: 0,
|
|
58
|
+
supportsNativeTools: true,
|
|
59
|
+
supportsThinking: true
|
|
60
|
+
},
|
|
61
|
+
"gemini-2.5-pro": {
|
|
62
|
+
id: "gemini-2.5-pro",
|
|
63
|
+
name: "Gemini 2.5 Pro",
|
|
64
|
+
contextWindow: 1048576,
|
|
65
|
+
maxOutput: 65536,
|
|
66
|
+
inputPrice: 0,
|
|
67
|
+
outputPrice: 0,
|
|
68
|
+
supportsNativeTools: true,
|
|
69
|
+
supportsThinking: true
|
|
70
|
+
},
|
|
71
|
+
"gemini-2.5-flash": {
|
|
72
|
+
id: "gemini-2.5-flash",
|
|
73
|
+
name: "Gemini 2.5 Flash",
|
|
74
|
+
contextWindow: 1048576,
|
|
75
|
+
maxOutput: 65536,
|
|
76
|
+
inputPrice: 0,
|
|
77
|
+
outputPrice: 0,
|
|
78
|
+
supportsNativeTools: true,
|
|
79
|
+
supportsThinking: true
|
|
80
|
+
},
|
|
81
|
+
"gemini-2.5-flash-lite": {
|
|
82
|
+
id: "gemini-2.5-flash-lite",
|
|
83
|
+
name: "Gemini 2.5 Flash Lite",
|
|
84
|
+
contextWindow: 1e6,
|
|
85
|
+
maxOutput: 32768,
|
|
86
|
+
inputPrice: 0,
|
|
87
|
+
outputPrice: 0,
|
|
88
|
+
supportsNativeTools: true,
|
|
89
|
+
supportsThinking: false
|
|
90
|
+
},
|
|
91
|
+
auto: {
|
|
92
|
+
id: "auto",
|
|
93
|
+
name: "Auto (Default)",
|
|
94
|
+
contextWindow: 1048576,
|
|
95
|
+
maxOutput: 65536,
|
|
96
|
+
inputPrice: 0,
|
|
97
|
+
outputPrice: 0,
|
|
98
|
+
supportsNativeTools: true,
|
|
99
|
+
supportsThinking: true
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
function getGeminiCliCredentialPath(customPath) {
|
|
103
|
+
if (customPath) return customPath;
|
|
104
|
+
const home = process.env["HOME"] ?? process.env["USERPROFILE"] ?? "";
|
|
105
|
+
return `${home}/${GEMINI_DIR}/${GEMINI_CREDENTIAL_FILENAME}`;
|
|
106
|
+
}
|
|
107
|
+
function getGeminiCliEnvPath(customPath) {
|
|
108
|
+
if (customPath) return customPath;
|
|
109
|
+
const home = process.env["HOME"] ?? process.env["USERPROFILE"] ?? "";
|
|
110
|
+
return `${home}/${GEMINI_DIR}/${GEMINI_ENV_FILENAME}`;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export {
|
|
114
|
+
Role,
|
|
115
|
+
EventType,
|
|
116
|
+
GEMINI_OAUTH_REDIRECT_URI,
|
|
117
|
+
GEMINI_OAUTH_BASE_URL,
|
|
118
|
+
GEMINI_OAUTH_TOKEN_ENDPOINT,
|
|
119
|
+
GEMINI_OAUTH_AUTH_ENDPOINT,
|
|
120
|
+
GEMINI_OAUTH_CLIENT_ID,
|
|
121
|
+
GEMINI_OAUTH_CLIENT_SECRET,
|
|
122
|
+
GEMINI_OAUTH_SCOPES,
|
|
123
|
+
GEMINI_CODE_ASSIST_ENDPOINT,
|
|
124
|
+
GEMINI_CODE_ASSIST_API_VERSION,
|
|
125
|
+
GEMINI_DIR,
|
|
126
|
+
GEMINI_CREDENTIAL_FILENAME,
|
|
127
|
+
GEMINI_ENV_FILENAME,
|
|
128
|
+
TOKEN_REFRESH_BUFFER_MS,
|
|
129
|
+
HTTP_OK,
|
|
130
|
+
HTTP_UNAUTHORIZED,
|
|
131
|
+
HTTP_FORBIDDEN,
|
|
132
|
+
GEMINI_CLI_MODELS,
|
|
133
|
+
getGeminiCliCredentialPath,
|
|
134
|
+
getGeminiCliEnvPath
|
|
135
|
+
};
|