-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathgemini.plugin.ts
More file actions
176 lines (155 loc) · 5.44 KB
/
gemini.plugin.ts
File metadata and controls
176 lines (155 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import type { AgentMetadata, HookTransformer } from '../../core/types.js';
import { BaseAgentAdapter } from '../../core/BaseAgentAdapter.js';
import { GeminiHookTransformer } from './gemini.hook-transformer.js';
import { GeminiSessionAdapter } from './gemini.session-adapter.js';
import type { SessionAdapter } from '../../core/session/BaseSessionAdapter.js';
import { GeminiExtensionInstaller } from './gemini.extension-installer.js';
import type { BaseExtensionInstaller } from '../../core/extension/BaseExtensionInstaller.js';
// Define metadata first (used by both lifecycle and analytics)
const metadata = {
name: 'gemini',
displayName: 'Gemini CLI',
description: 'Google Gemini CLI - AI coding assistant',
npmPackage: '@google/gemini-cli',
cliCommand: 'gemini',
envMapping: {
baseUrl: ['GOOGLE_GEMINI_BASE_URL', 'GEMINI_BASE_URL'],
apiKey: ['GEMINI_API_KEY', 'GOOGLE_API_KEY'],
model: ['GEMINI_MODEL']
},
supportedProviders: ['ai-run-sso', 'litellm', 'bearer-auth'],
blockedModelPatterns: [/^claude/i, /^gpt/i], // Gemini models only
recommendedModels: ['gemini-3-pro'],
ssoConfig: {
enabled: true,
clientType: 'codemie-gemini'
},
flagMappings: {
'--task': {
type: 'flag' as const,
target: '-p'
}
},
// Data paths (used by lifecycle hooks and analytics)
dataPaths: {
home: '.gemini',
settings: 'settings.json'
},
// MCP configuration paths for Gemini CLI
// - User: ~/.gemini/settings.json → mcpServers (available across all projects)
// - Project: .gemini/settings.json → mcpServers (project-specific)
// Note: Gemini doesn't have a "local" scope like Claude
mcpConfig: {
project: {
path: '.gemini/settings.json',
jsonPath: 'mcpServers'
},
user: {
path: '~/.gemini/settings.json',
jsonPath: 'mcpServers'
}
},
// Hook configuration: event name mapping
hookConfig: {
/**
* Map Gemini event names to internal event names
* Based on Gemini CLI documentation: https://geminicli.com/docs/hooks/
*
* Supported mappings (map to existing 6 internal events):
* - SessionStart → SessionStart (direct)
* - SessionEnd → SessionEnd (direct)
* - PreCompress → PreCompact (Gemini's name for context compression)
* - AfterAgent → Stop (Gemini's AfterAgent = Claude's Stop)
* - Notification → PermissionRequest (Gemini's notification system)
*
* Unsupported events (silently ignored by router):
* - BeforeModel, AfterModel, BeforeToolSelection, BeforeTool, AfterTool
*/
eventNameMapping: {
// Direct mappings (same name)
'SessionStart': 'SessionStart',
'SessionEnd': 'SessionEnd',
// Renamed mappings
'PreCompress': 'PreCompact', // Gemini's PreCompress = Claude's PreCompact
'AfterAgent': 'Stop', // Gemini's AfterAgent = Claude's Stop
'BeforeAgent': 'UserPromptSubmit', // Gemini's BeforeAgent = Claude's UserPromptSubmit
'Notification': 'PermissionRequest' // Gemini's Notification = Claude's PermissionRequest
}
}
};
/**
* Gemini CLI Plugin Metadata
*/
export const GeminiPluginMetadata: AgentMetadata = {
...metadata,
// Lifecycle hook to ensure settings file exists
// Uses BaseAgentAdapter methods for cross-platform file operations
lifecycle: {
enrichArgs: (args, config) => {
// Gemini CLI uses -m flag for model selection
const hasModelArg = args.some((arg, idx) =>
(arg === '-m' || arg === '--model') && idx < args.length - 1
);
if (!hasModelArg && config.model) {
return ['-m', config.model, ...args];
}
return args;
},
beforeRun: async function(this: BaseAgentAdapter, env: NodeJS.ProcessEnv) {
// Ensure .gemini directory exists (uses base method)
await this.ensureDirectory(this.resolveDataPath());
// Ensure settings.json exists with default content (uses base method)
await this.ensureJsonFile(
this.resolveDataPath(metadata.dataPaths.settings),
{
security: {
auth: {
selectedType: 'gemini-api-key'
}
},
tools: {
enableHooks: true
}
}
);
return env;
}
}
};
/**
* Gemini CLI Adapter
*/
export class GeminiPlugin extends BaseAgentAdapter {
private hookTransformer: HookTransformer;
private sessionAdapter: SessionAdapter;
private extensionInstaller: BaseExtensionInstaller;
constructor() {
super(GeminiPluginMetadata);
// Initialize hook transformer for Gemini-specific payload transformation
this.hookTransformer = new GeminiHookTransformer();
// Initialize session adapter for unified session sync
this.sessionAdapter = new GeminiSessionAdapter(GeminiPluginMetadata);
// Initialize extension installer with metadata (agent name from metadata)
this.extensionInstaller = new GeminiExtensionInstaller(GeminiPluginMetadata);
}
/**
* Get hook transformer for this agent
* Transforms Gemini hook events to internal format
*/
getHookTransformer(): HookTransformer {
return this.hookTransformer;
}
/**
* Get session adapter for this agent (used by unified session sync)
*/
getSessionAdapter(): SessionAdapter {
return this.sessionAdapter;
}
/**
* Get extension installer for this agent
* Returns installer to handle extension installation
*/
getExtensionInstaller(): BaseExtensionInstaller {
return this.extensionInstaller;
}
}