-
Notifications
You must be signed in to change notification settings - Fork 11.7k
feat(sdk): initial package bootstrap for SDK #18861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+451
−15
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d0ca053
feat(sdk): initial package bootstrap for SDK
mbleigh bf9d04f
Merge branch 'main' into sdk-01-bootstrap
mbleigh 5412b9d
fix(sdk): add missing src/index.ts export file
mbleigh 07ad7a0
fix(sdk): handle JSON string tool arguments and add TODO for AbortSignal
mbleigh 78aa0ac
Merge branch 'main' into sdk-01-bootstrap
mbleigh 5045cad
fix(sdk): enforce tool execution confirmation in GeminiCliAgent and r…
mbleigh eb4ce11
Merge branch 'main' into sdk-01-bootstrap
mbleigh 4c2d2a7
refactor(sdk): use core tool scheduler in GeminiCliAgent
mbleigh b1c781d
refactor(sdk): use core tool scheduler and unified auth detection
mbleigh 7689bff
Merge branch 'sdk-01-bootstrap' of github.com:google-gemini/gemini-cl…
mbleigh 7ff0c57
Merge branch 'sdk-01-bootstrap' of github.com:google-gemini/gemini-cl…
mbleigh 0b38f12
Merge branch 'sdk-01-bootstrap' of github.com:google-gemini/gemini-cl…
mbleigh c7d1c78
Merge branch 'main' into sdk-01-bootstrap
mbleigh ea29786
fix lint
mbleigh 746f0a8
Merge branch 'sdk-01-bootstrap' of github.com:google-gemini/gemini-cl…
mbleigh f6b3943
fix format
mbleigh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # @google/gemini-cli-sdk | ||
|
|
||
| The Gemini CLI SDK provides a programmatic interface to interact with Gemini | ||
| models and tools. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| npm install @google/gemini-cli-sdk | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ```typescript | ||
| import { GeminiCliAgent } from '@google/gemini-cli-sdk'; | ||
|
|
||
| async function main() { | ||
| const agent = new GeminiCliAgent({ | ||
| instructions: 'You are a helpful assistant.', | ||
| }); | ||
|
|
||
| const controller = new AbortController(); | ||
| const signal = controller.signal; | ||
|
|
||
| // Stream responses from the agent | ||
| const stream = agent.sendStream('Why is the sky blue?', signal); | ||
|
|
||
| for await (const chunk of stream) { | ||
| if (chunk.type === 'content') { | ||
| process.stdout.write(chunk.value.text || ''); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| main().catch(console.error); | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { GeminiCliAgent, tool, z } from '../src/index.js'; | ||
|
|
||
| async function main() { | ||
| const myTool = tool( | ||
| { | ||
| name: 'add', | ||
| description: 'Add two numbers.', | ||
| inputSchema: z.object({ | ||
| a: z.number().describe('the first number'), | ||
| b: z.number().describe('the second number'), | ||
| }), | ||
| }, | ||
| async ({ a, b }) => { | ||
| console.log(`Tool 'add' called with a=${a}, b=${b}`); | ||
| return { result: a + b }; | ||
| }, | ||
| ); | ||
|
|
||
| const agent = new GeminiCliAgent({ | ||
| instructions: 'Make sure to always talk like a pirate.', | ||
| tools: [myTool], | ||
| }); | ||
|
|
||
| console.log("Sending prompt: 'add 5 + 6'"); | ||
| for await (const chunk of agent.sendStream( | ||
| 'add 5 + 6 and tell me a story involving the result', | ||
| )) { | ||
| console.log(JSON.stringify(chunk, null, 2)); | ||
| } | ||
| } | ||
|
|
||
| main().catch(console.error); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| export * from './src/index.js'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| { | ||
| "name": "@google/gemini-cli-sdk", | ||
| "version": "0.29.0-nightly.20260203.71f46f116", | ||
| "description": "Gemini CLI SDK", | ||
| "license": "Apache-2.0", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/google-gemini/gemini-cli.git" | ||
| }, | ||
| "type": "module", | ||
| "main": "dist/index.js", | ||
| "types": "dist/index.d.ts", | ||
| "scripts": { | ||
| "build": "node ../../scripts/build_package.js", | ||
| "lint": "eslint . --ext .ts,.tsx", | ||
| "format": "prettier --write .", | ||
| "test": "vitest run", | ||
| "test:ci": "vitest run", | ||
| "typecheck": "tsc --noEmit" | ||
| }, | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "dependencies": { | ||
| "@google/gemini-cli-core": "file:../core", | ||
| "zod": "^3.23.8", | ||
| "zod-to-json-schema": "^3.23.1" | ||
| }, | ||
| "devDependencies": { | ||
| "typescript": "^5.3.3", | ||
| "vitest": "^3.1.1" | ||
| }, | ||
| "engines": { | ||
| "node": ">=20" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { | ||
| Config, | ||
| type ConfigParameters, | ||
| PREVIEW_GEMINI_MODEL_AUTO, | ||
| GeminiEventType, | ||
| type ToolCallRequestInfo, | ||
| type ServerGeminiStreamEvent, | ||
| type GeminiClient, | ||
| scheduleAgentTools, | ||
| getAuthTypeFromEnv, | ||
| AuthType, | ||
| } from '@google/gemini-cli-core'; | ||
|
|
||
| import { type Tool, SdkTool, type z } from './tool.js'; | ||
|
|
||
| export interface GeminiCliAgentOptions { | ||
| instructions: string; | ||
| tools?: Array<Tool<z.ZodType>>; | ||
| model?: string; | ||
| cwd?: string; | ||
| debug?: boolean; | ||
| } | ||
|
|
||
| export class GeminiCliAgent { | ||
| private readonly config: Config; | ||
| private readonly tools: Array<Tool<z.ZodType>>; | ||
|
|
||
| constructor(options: GeminiCliAgentOptions) { | ||
| const cwd = options.cwd || process.cwd(); | ||
| this.tools = options.tools || []; | ||
|
|
||
| const configParams: ConfigParameters = { | ||
| sessionId: `sdk-${Date.now()}`, | ||
| targetDir: cwd, | ||
| cwd, | ||
| debugMode: options.debug ?? false, | ||
| model: options.model || PREVIEW_GEMINI_MODEL_AUTO, | ||
| userMemory: options.instructions, | ||
mbleigh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Minimal config | ||
| enableHooks: false, | ||
| mcpEnabled: false, | ||
| extensionsEnabled: false, | ||
| }; | ||
|
|
||
| this.config = new Config(configParams); | ||
| } | ||
|
|
||
| async *sendStream( | ||
| prompt: string, | ||
| signal?: AbortSignal, | ||
| ): AsyncGenerator<ServerGeminiStreamEvent> { | ||
| // Lazy initialization of auth and client | ||
| if (!this.config.getContentGenerator()) { | ||
mbleigh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const authType = getAuthTypeFromEnv() || AuthType.COMPUTE_ADC; | ||
|
|
||
| await this.config.refreshAuth(authType); | ||
| await this.config.initialize(); | ||
|
|
||
| // Register tools now that registry exists | ||
| const registry = this.config.getToolRegistry(); | ||
| const messageBus = this.config.getMessageBus(); | ||
|
|
||
| for (const toolDef of this.tools) { | ||
| const sdkTool = new SdkTool(toolDef, messageBus); | ||
| registry.registerTool(sdkTool); | ||
| } | ||
| } | ||
|
|
||
| const client = this.config.getGeminiClient(); | ||
|
|
||
| let request: Parameters<GeminiClient['sendMessageStream']>[0] = [ | ||
| { text: prompt }, | ||
| ]; | ||
| const abortSignal = signal ?? new AbortController().signal; | ||
| const sessionId = this.config.getSessionId(); | ||
|
|
||
| while (true) { | ||
mbleigh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // sendMessageStream returns AsyncGenerator<ServerGeminiStreamEvent, Turn> | ||
| const stream = client.sendMessageStream(request, abortSignal, sessionId); | ||
|
|
||
| const toolCallsToSchedule: ToolCallRequestInfo[] = []; | ||
|
|
||
| for await (const event of stream) { | ||
| yield event; | ||
| if (event.type === GeminiEventType.ToolCallRequest) { | ||
| const toolCall = event.value; | ||
| let args = toolCall.args; | ||
| if (typeof args === 'string') { | ||
| args = JSON.parse(args); | ||
| } | ||
| toolCallsToSchedule.push({ | ||
| ...toolCall, | ||
| args, | ||
| isClientInitiated: false, | ||
| prompt_id: sessionId, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| if (toolCallsToSchedule.length === 0) { | ||
| break; | ||
| } | ||
|
|
||
| const completedCalls = await scheduleAgentTools( | ||
| this.config, | ||
| toolCallsToSchedule, | ||
| { | ||
| schedulerId: sessionId, | ||
| toolRegistry: this.config.getToolRegistry(), | ||
| signal: abortSignal, | ||
| }, | ||
| ); | ||
|
|
||
| const functionResponses = completedCalls.flatMap( | ||
| (call) => call.response.responseParts, | ||
| ); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion | ||
| request = functionResponses as unknown as Parameters< | ||
| GeminiClient['sendMessageStream'] | ||
| >[0]; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| export * from './agent.js'; | ||
| export * from './tool.js'; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.