|
| 1 | +package bridge |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "time" |
| 9 | + |
| 10 | + "ccgo/internal/commands" |
| 11 | + "ccgo/internal/contracts" |
| 12 | + "ccgo/internal/platform" |
| 13 | +) |
| 14 | + |
| 15 | +const manifestFileName = "bridge-manifest.json" |
| 16 | + |
| 17 | +type Manifest struct { |
| 18 | + SessionID contracts.ID `json:"session_id,omitempty"` |
| 19 | + WorkingDirectory string `json:"working_directory,omitempty"` |
| 20 | + GeneratedAt string `json:"generated_at"` |
| 21 | + Commands []Command `json:"commands,omitempty"` |
| 22 | +} |
| 23 | + |
| 24 | +type Command struct { |
| 25 | + Name string `json:"name"` |
| 26 | + DisplayName string `json:"display_name,omitempty"` |
| 27 | + Type contracts.CommandType `json:"type"` |
| 28 | + Source contracts.CommandSource `json:"source,omitempty"` |
| 29 | + LoadedFrom string `json:"loaded_from,omitempty"` |
| 30 | + Aliases []string `json:"aliases,omitempty"` |
| 31 | + ArgumentHint string `json:"argument_hint,omitempty"` |
| 32 | + SupportsNonInteractive bool `json:"supports_non_interactive,omitempty"` |
| 33 | + Immediate bool `json:"immediate,omitempty"` |
| 34 | +} |
| 35 | + |
| 36 | +func SessionManifestPath(sessionPath string, sessionID contracts.ID) string { |
| 37 | + if sessionPath == "" || sessionID == "" { |
| 38 | + return "" |
| 39 | + } |
| 40 | + return filepath.Join(filepath.Dir(sessionPath), string(sessionID), manifestFileName) |
| 41 | +} |
| 42 | + |
| 43 | +func BuildManifest(sessionID contracts.ID, cwd string, registry commands.Registry) Manifest { |
| 44 | + manifest := Manifest{ |
| 45 | + SessionID: sessionID, |
| 46 | + WorkingDirectory: cwd, |
| 47 | + GeneratedAt: time.Now().UTC().Format(time.RFC3339Nano), |
| 48 | + } |
| 49 | + for _, command := range registry.Visible() { |
| 50 | + if !commands.IsBridgeSafeCommand(command) { |
| 51 | + continue |
| 52 | + } |
| 53 | + manifest.Commands = append(manifest.Commands, Command{ |
| 54 | + Name: command.Name, |
| 55 | + DisplayName: command.DisplayName, |
| 56 | + Type: command.Type, |
| 57 | + Source: command.Source, |
| 58 | + LoadedFrom: command.LoadedFrom, |
| 59 | + Aliases: append([]string(nil), command.Aliases...), |
| 60 | + ArgumentHint: command.ArgumentHint, |
| 61 | + SupportsNonInteractive: command.SupportsNonInteractive, |
| 62 | + Immediate: command.Immediate, |
| 63 | + }) |
| 64 | + } |
| 65 | + return manifest |
| 66 | +} |
| 67 | + |
| 68 | +func BuildManifestFromSettings(sessionID contracts.ID, cwd string, settings contracts.Settings) Manifest { |
| 69 | + return BuildManifest(sessionID, cwd, commands.Load(commands.Options{CWD: cwd, Settings: settings})) |
| 70 | +} |
| 71 | + |
| 72 | +func WriteManifest(path string, manifest Manifest) error { |
| 73 | + if path == "" { |
| 74 | + return os.ErrInvalid |
| 75 | + } |
| 76 | + if manifest.GeneratedAt == "" { |
| 77 | + manifest.GeneratedAt = time.Now().UTC().Format(time.RFC3339Nano) |
| 78 | + } |
| 79 | + data, err := json.MarshalIndent(manifest, "", " ") |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + data = append(data, '\n') |
| 84 | + return platform.AtomicWriteFile(path, data, 0o644) |
| 85 | +} |
| 86 | + |
| 87 | +func LoadManifest(path string) (Manifest, error) { |
| 88 | + if path == "" { |
| 89 | + return Manifest{}, os.ErrInvalid |
| 90 | + } |
| 91 | + data, err := os.ReadFile(path) |
| 92 | + if errors.Is(err, os.ErrNotExist) { |
| 93 | + return Manifest{}, nil |
| 94 | + } |
| 95 | + if err != nil { |
| 96 | + return Manifest{}, err |
| 97 | + } |
| 98 | + var manifest Manifest |
| 99 | + if err := json.Unmarshal(data, &manifest); err != nil { |
| 100 | + return Manifest{}, err |
| 101 | + } |
| 102 | + return manifest, nil |
| 103 | +} |
0 commit comments