Files
雷电芽衣 34ff4c1ea8 chore(go): audit providers kimi grok qwen letta
Move Kimi, Grok, and Qwen onto managed_runtime.go and shell_command.go;
share the encoded and shell command builders and the quoted Windows
legacy parser with Droid; split managed_changes.go; dedupe Letta's hook
entry filtering and stop swallowing settings read errors.

Claude-Session: https://claude.ai/code/session_01Agoc9bRVE9KL7mmWtey9Vq
2026-09-03 02:16:58 -04:00

137 lines
3.2 KiB
Go

package plugins
import (
"fmt"
"path/filepath"
)
// GrokPlugin manages Grok global user hooks.
type GrokPlugin struct {
rename renameFunc
}
// ManagesGuardRuntime reports that Grok commits its provider guard with the
// audit runtime and user hook document.
func (p *GrokPlugin) ManagesGuardRuntime() bool {
return true
}
// PreflightInstall validates the hook source and runtime identity before any
// directory or file is created.
func (p *GrokPlugin) PreflightInstall(config InstallConfig) error {
document, err := readGrokDocument()
if err != nil {
return err
}
_, _, err = preflightGrokInstallation(config, document)
return err
}
func (p *GrokPlugin) Install(config InstallConfig) error {
document, err := readGrokDocument()
if err != nil {
return err
}
paths, nodePath, err := preflightGrokInstallation(config, document)
if err != nil {
return err
}
guardCommand, err := buildEncodedCommand("Grok", nodePath, paths.guardPath)
if err != nil {
return err
}
auditCommand, err := buildEncodedCommand("Grok", nodePath, paths.auditPath)
if err != nil {
return err
}
hooks, err := removeManagedGrokHooks(document.hooks, "")
if err != nil {
return err
}
for _, item := range []struct{ event, command string }{
{"PreToolUse", guardCommand},
{"PostToolUse", auditCommand},
{"PostToolUseFailure", auditCommand},
} {
hooks[item.event] = append(hooks[item.event], buildGrokGroup(item.command))
}
rendered, err := renderGrokDocument(document, hooks)
if err != nil {
return fmt.Errorf("render Grok user hooks: %w", err)
}
changes, err := prepareGrokInstallationChanges(config, paths, rendered)
if err != nil {
return err
}
if err := writeGrokChanges(
changes,
"Install Grok hooks",
p.rename,
paths.runtimeRoot,
paths.agentDirectory,
filepath.Dir(document.configPath),
); err != nil {
return err
}
fmt.Println(
"Grok Build: global PreToolUse, PostToolUse, and PostToolUseFailure hooks installed.",
)
return nil
}
func (p *GrokPlugin) Uninstall(agentID string) error {
document, err := readGrokDocument()
if err != nil {
return err
}
hooks, err := removeManagedGrokHooks(document.hooks, agentID)
if err != nil {
return err
}
rendered, err := renderGrokDocument(document, hooks)
if err != nil {
return fmt.Errorf("render Grok user hooks: %w", err)
}
change, err := prepareRenderedGrokChange(rendered)
if err != nil {
return err
}
return writeGrokChanges(
[]*fileChange{change},
"Uninstall Grok hooks",
p.rename,
"",
"",
filepath.Dir(document.configPath),
)
}
func (p *GrokPlugin) Status() (PluginStatus, error) {
configPath, pathErr := grokConfigPath()
entry := SupportedAgents[grokAgentKey]
status := PluginStatus{
AgentName: grokAgentKey, DisplayName: entry.Name, ConfigPath: configPath,
}
if pathErr != nil {
return status, pathErr
}
document, err := readGrokDocument()
if err != nil {
return status, err
}
contracts, err := grokRuntimeContracts(document.hooks)
if err != nil {
return status, err
}
status.HookConfigured = len(contracts) > 0
if !status.HookConfigured {
return status, nil
}
status.HookScriptExists, err = grokRuntimeFilesExist(contracts)
if err != nil {
return status, err
}
status.Installed = status.HookScriptExists
return status, nil
}