bots-garden/sidekickspublic Fork
⑂ v0.1.0
Commits
⬇ Clone ▾
git clone https://git.rickub.com/bots-garden/sidekicks.git
git clone ssh://git@rickub.com/bots-garden/sidekicks.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP
🛟 Updated. 8ef4097 · on v0.1.0 · k33g · 25d ago
main.go · 143 lines · 5.4 KBGo Blame HistoryRaw
  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
// Copyright (c) 2026 Philippe Charrière — Sidekicks Source-Available License 1.0, see LICENSE

// Command sidekicks is the Sidekicks desktop application: it reads the
// configuration, binds the services of internal/app to the Svelte interface
// and opens the window. The main package sits at the repository root because
// the build Taskfiles generated by Wails build "." (ADR 0003, amendment).
//
//	sidekicks [--config path/to/config.yaml]
package main

import (
	"context"
	"embed"
	"flag"
	"fmt"
	"log"
	"os"
	"time"

	"git.rickub.com/bots-garden/sidekicks/internal/app"
	"git.rickub.com/bots-garden/sidekicks/internal/channels"
	"git.rickub.com/bots-garden/sidekicks/internal/config"
	"git.rickub.com/bots-garden/sidekicks/internal/events"
	"git.rickub.com/bots-garden/sidekicks/internal/prefs"
	"git.rickub.com/bots-garden/sidekicks/internal/shellpath"
	"git.rickub.com/bots-garden/sidekicks/internal/workspace"
	"github.com/wailsapp/wails/v3/pkg/application"
)

//go:embed all:frontend/dist
var assets embed.FS

// titleBarInset must match --titlebar-inset in frontend/src/style.css: the
// height of the area covered by the macOS traffic lights.
const titleBarInset = 38

func init() {
	// Registering the payload types generates typed TypeScript events.
	application.RegisterEvent[events.ConfigChangedPayload](events.ConfigChanged)
	application.RegisterEvent[events.AgentStatePayload](events.AgentState)
	application.RegisterEvent[events.AgentLogPayload](events.AgentLog)
	application.RegisterEvent[channels.ListPayload](events.ChannelsChanged)
	application.RegisterEvent[channels.MessagePayload](events.MessageUpdated)
	application.RegisterEvent[channels.DeltaPayload](events.MessageDelta)
	application.RegisterEvent[channels.TurnPayload](events.TurnChanged)
	application.RegisterEvent[channels.Permission](events.PermissionRequested)
	application.RegisterEvent[channels.PermissionResolvedPayload](events.PermissionResolved)
	application.RegisterEvent[channels.UsagePayload](events.UsageChanged)
	application.RegisterEvent[channels.CommandsPayload](events.CommandsChanged)
	application.RegisterEvent[events.WorkspaceChangedPayload](events.WorkspaceChanged)
	application.RegisterEvent[workspace.OutputPayload](events.TerminalOutput)
	application.RegisterEvent[workspace.ExitPayload](events.TerminalExit)
}

func main() {
	configPath := flag.String("config", "", "configuration file (default: $"+config.EnvironmentVariable+", then the default location)")
	flag.Parse()

	// Before the configuration is read: its warnings say whether each
	// agent's program can be found, and the agents inherit this PATH.
	shellCtx, shellCancel := context.WithTimeout(context.Background(), 5*time.Second)
	if err := shellpath.Apply(shellCtx); err != nil {
		log.Printf("sidekicks: the PATH of your shell could not be read (%v); programs are looked up in %s", err, os.Getenv("PATH"))
	}
	shellCancel()

	var preferences *prefs.Store
	if path, err := prefs.DefaultPath(); err == nil {
		preferences = prefs.NewStore(path)
	}
	lastOpened := ""
	if preferences != nil {
		lastOpened = preferences.Load().LastOpened
	}
	location, err := config.Locate(*configPath, os.Getenv, lastOpened)
	if err != nil {
		fmt.Fprintln(os.Stderr, "sidekicks:", err)
		os.Exit(1)
	}

	// The file dialog is used once the application exists. Events sent while
	// core is created (declared channels) have nobody to reach yet: the
	// interface reads the initial state from the services.
	var wailsApp *application.App
	emitter := events.EmitterFunc(func(name string, payload any) {
		if wailsApp != nil {
			wailsApp.Event.Emit(name, payload)
		}
	})
	pickFile := func(folder string) (string, error) {
		return wailsApp.Dialog.OpenFile().
			SetTitle("Open a Sidekicks configuration").
			SetDirectory(folder).
			AddFilter("YAML configuration", "*.yaml;*.yml").
			PromptForSingleSelection()
	}
	pickFolder := func(folder string) (string, error) {
		return wailsApp.Dialog.OpenFile().
			SetTitle("Choose the agent's folder").
			SetDirectory(folder).
			CanChooseFiles(false).
			CanChooseDirectories(true).
			CanCreateDirectories(true).
			PromptForSingleSelection()
	}
	core := app.New(location, app.Options{Emitter: emitter, Preferences: preferences, PickFile: pickFile, PickFolder: pickFolder})
	ctx, cancel := context.WithCancel(context.Background())

	wailsApp = application.New(application.Options{
		Name:        "Sidekicks",
		Description: "Converse with AI agents in channels",
		Services: []application.Service{
			application.NewService(&app.AgentService{Core: core}),
			application.NewService(&app.ConfigService{Core: core}),
			application.NewService(&app.ChannelService{Core: core}),
			application.NewService(&app.WorkspaceService{Core: core}),
			application.NewService(&app.TerminalService{Core: core}),
		},
		Assets: application.AssetOptions{Handler: application.AssetFileServerFS(assets)},
		Mac:    application.MacOptions{ApplicationShouldTerminateAfterLastWindowClosed: true},
		OnShutdown: func() {
			cancel()
			core.Shutdown(context.Background())
		},
	})
	wailsApp.Window.NewWithOptions(application.WebviewWindowOptions{
		Title:  "Sidekicks",
		Width:  1280,
		Height: 800,
		Mac: application.MacWindow{
			InvisibleTitleBarHeight: titleBarInset,
			Backdrop:                application.MacBackdropTranslucent,
			TitleBar:                application.MacTitleBarHiddenInset,
		},
		BackgroundColour: application.NewRGB(24, 25, 29),
		URL:              "/",
	})

	go core.Run(ctx)
	if err := wailsApp.Run(); err != nil {
		log.Fatal(err)
	}
}