-
Notifications
You must be signed in to change notification settings - Fork 1
Add Gemini CLI render adapter with explicit agents opt-in #22
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
Open
kelos-bot
wants to merge
1
commit into
main
Choose a base branch
from
kanon-task-7
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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,147 @@ | ||
| package core | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "path/filepath" | ||
| ) | ||
|
|
||
| type geminiAdapter struct{} | ||
|
|
||
| func (geminiAdapter) Name() string { | ||
| return AgentGemini | ||
| } | ||
|
|
||
| func (geminiAdapter) Render(cfg *Config, opts TargetOptions) ([]RenderedFile, error) { | ||
| var files []RenderedFile | ||
| targetRoot := filepath.Join(opts.UserHome, ".gemini") | ||
| instructionPath := filepath.Join(targetRoot, "GEMINI.md") | ||
| skillRoot := filepath.Join(targetRoot, "skills") | ||
| if opts.Project != "" { | ||
| targetRoot = filepath.Join(opts.Project, ".gemini") | ||
| instructionPath = filepath.Join(opts.Project, "GEMINI.md") | ||
| skillRoot = filepath.Join(targetRoot, "skills") | ||
| } | ||
|
|
||
| instructions, err := readInstruction(opts.KanonHome, cfg.Instructions.Files) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if len(instructions) > 0 { | ||
| files = append(files, RenderedFile{ | ||
| Agent: AgentGemini, | ||
| Path: instructionPath, | ||
| Content: instructions, | ||
| Mode: 0o644, | ||
| }) | ||
| } | ||
|
|
||
| settings := map[string]any{} | ||
| if servers := geminiMCPServers(cfg); len(servers) > 0 { | ||
| settings["mcpServers"] = servers | ||
| } | ||
| hooks, err := geminiHooks(cfg) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if len(hooks) > 0 { | ||
| settings["hooks"] = hooks | ||
| } | ||
| if len(settings) > 0 { | ||
| data, err := renderJSON(settings) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| files = append(files, RenderedFile{ | ||
| Agent: AgentGemini, | ||
| Path: filepath.Join(targetRoot, "settings.json"), | ||
| Content: data, | ||
| Mode: 0o644, | ||
| }) | ||
| } | ||
|
|
||
| skills, err := renderSkills(cfg, opts, AgentGemini, skillRoot) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| files = append(files, skills...) | ||
| return files, nil | ||
| } | ||
|
|
||
| // Import is not yet supported for Gemini CLI. The adapter is render-only for | ||
| // now (see issue #7); lifting GEMINI.md + settings.json back into the neutral | ||
| // config is tracked as later work. It returns an empty result so that an | ||
| // explicit `kanon import --agent gemini` is a harmless no-op rather than an | ||
| // error. | ||
| func (geminiAdapter) Import(opts ImportOptions) (*ImportResult, error) { | ||
| return &ImportResult{ | ||
| Config: &Config{Version: 1}, | ||
| Files: map[string][]byte{}, | ||
| }, nil | ||
| } | ||
|
|
||
| func geminiHooks(cfg *Config) (map[string]any, error) { | ||
| grouped := map[string]any{} | ||
| for _, hook := range cfg.Hooks { | ||
| if !HasTarget(hook.Targets, AgentGemini) { | ||
| continue | ||
| } | ||
| event, item, err := geminiHookItem(hook) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| grouped[event] = appendAny(grouped[event], item) | ||
| } | ||
| return grouped, nil | ||
| } | ||
|
|
||
| func geminiHookItem(hook Hook) (string, map[string]any, error) { | ||
| event := hook.Event | ||
| if event == "" { | ||
| event = hook.Name | ||
| } | ||
| geminiEvent, ok := geminiHookEvent(event) | ||
| if !ok { | ||
| return "", nil, fmt.Errorf("hook %q targets gemini with unsupported event %q", hook.Name, event) | ||
| } | ||
| if hook.Type != "" && hook.Type != "command" { | ||
| return "", nil, fmt.Errorf("hook %q targets gemini with unsupported type %q", hook.Name, hook.Type) | ||
| } | ||
| if hook.Command == "" { | ||
| return "", nil, fmt.Errorf("hook %q targets gemini and requires command", hook.Name) | ||
| } | ||
| if len(hook.Args) > 0 { | ||
| return "", nil, fmt.Errorf("hook %q targets gemini: args are not supported; include arguments in command", hook.Name) | ||
| } | ||
| if hook.Async { | ||
| return "", nil, fmt.Errorf("hook %q targets gemini: async hooks are not supported", hook.Name) | ||
| } | ||
| handler := map[string]any{ | ||
| "name": hook.Name, | ||
| "type": "command", | ||
| "command": hook.Command, | ||
| } | ||
| if hook.Timeout > 0 { | ||
| handler["timeout"] = hook.Timeout | ||
| } | ||
| item := map[string]any{"hooks": []any{handler}} | ||
| if hook.Matcher != "" { | ||
| item["matcher"] = hook.Matcher | ||
| } | ||
| return geminiEvent, item, nil | ||
| } | ||
|
|
||
| func geminiHookEvent(event string) (string, bool) { | ||
| switch event { | ||
| case "PreToolUse": | ||
| return "BeforeTool", true | ||
| case "PostToolUse": | ||
| return "AfterTool", true | ||
| case "BeforeTool", "AfterTool", | ||
| "BeforeAgent", "AfterAgent", | ||
| "BeforeModel", "BeforeToolSelection", "AfterModel", | ||
| "SessionStart", "SessionEnd", "Notification", "PreCompress": | ||
| return event, true | ||
| default: | ||
| return "", false | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Please give this rendered file a merge strategy before shipping Gemini apply support.
.gemini/settings.jsonis the same file Gemini CLI uses for unrelated settings like UI, general, security, privacy, andhooksConfig; with the defaultFileMergeReplacehere, an opted-in user who already has that file will lose those settings when Kanon writes onlymcpServersandhooks. This also contradicts the README's co-owned-config contract, so Kanon should preserve unrelated top-level fields and non-rendered MCP servers (or fail) instead of replacing the whole file.