server

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2026 License: MIT Imports: 31 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultPort = 4567

DefaultPort 是 dashboard server 的預設監聽 port,為 CLI(`4x live`)與桌面殼 (macOS Swift、Windows/Linux Tauri)啟動時的預設猜測值之單一事實來源。 Swift(dashboard/macos/Sources/main.swift)與 Rust(dashboard/tauri/src-tauri/src/main.rs) 各自維護一份等值字面常量(無法跨語言直接讀取此常量),由 internal/server/port_sync_test.go 的一致性測試守護三者不漂移;異動此值須同步更新該兩處字面常量。

Variables

View Source
var Version = "dev"

Version 是目前 CLI binary 的版本號,由 main 在啟動時透過 ldflags 注入。

Functions

func DefaultRecentProjectsPath

func DefaultRecentProjectsPath() (string, error)

DefaultRecentProjectsPath 回傳 ~/.4x/recent-projects.json

func ListenForMulti added in v0.1.2

func ListenForMulti(port int) (net.Listener, error)

ListenForMulti 在指定 port 建立 TCP listener,port 為 0 時由 OS 自動分配可用 port。

func NewMultiMux

func NewMultiMux(reg *ProjectRegistry, recentPath string) http.Handler

NewMultiMux 建立支援多專案的 HTTP handler

func NewMux

func NewMux(resolver WorkspaceResolver) http.Handler

NewMux 建立 dashboard 的 HTTP handler。每個需要資料的 handler 透過傳入的 resolver 解析該請求對應的 workspace/process manager/batch manager,使路由表只定義一次, 由 singleResolver(單一專案)或 multiResolver(多專案)共用。

func SaveRecentProjects

func SaveRecentProjects(path string, rp *RecentProjects) error

SaveRecentProjects 寫入 recent-projects.json,自動建立父目錄

func ServeMulti added in v0.1.2

func ServeMulti(ctx context.Context, reg *ProjectRegistry, ln net.Listener, recentPath string) (int, error)

ServeMulti 在已建立的 listener 上啟動 dashboard server。

func Start

func Start(ws *protocol.CachedWorkspace, pm *ProcessManager, port int) error

Start 啟動 dashboard web server。

func StartMulti

func StartMulti(ctx context.Context, reg *ProjectRegistry, port int, recentPath string) error

StartMulti 啟動多專案 dashboard server,ctx 取消時 graceful shutdown。

func StartMultiOnListener added in v0.1.2

func StartMultiOnListener(ctx context.Context, reg *ProjectRegistry, port int, recentPath string) (int, error)

StartMultiOnListener 在已建立的 listener 上啟動 server(由 ListenForMulti 取得)。 回傳實際使用的 port。若傳入 port 而非 listener,使用 StartMulti。

Types

type BatchWorkspace added in v0.3.6

type BatchWorkspace interface {
	DotDir() string
	WriteBatchStop() error
	ReadBatchPID() (int, error)
	ClearBatchPID() error
}

BatchWorkspace 是 BatchManager 依賴的最小 workspace 介面。 *protocol.Workspace 已滿足此介面。

type Cmd added in v0.3.6

type Cmd interface {
	SetDir(dir string)
	StdoutPipe() (io.ReadCloser, error)
	StderrPipe() (io.ReadCloser, error)
	Start() error
	Wait() error
	Pid() int
	Signal(sig os.Signal) error
	Kill() error
}

Cmd 抽出子程序需要的最小操作介面(設定工作目錄、輸出管線、啟動、等待、訊號), 讓 ProcessManager/BatchManager 依賴介面而非具體 *exec.Cmd,測試可注入假實作而不需真的產生子程序。

type Launcher added in v0.3.6

type Launcher interface {
	Command(name string, args ...string) Cmd
}

Launcher 依 name/args 建立一個尚未啟動的 Cmd。

type ProcessManager

type ProcessManager struct {
	// contains filtered or unexported fields
}

ProcessManager 管理 4x run subprocess 的生命週期。

func NewProcessManager

func NewProcessManager(ws *protocol.Workspace, maxParallel int, binName string) *ProcessManager

NewProcessManager 建立 ProcessManager,binName 通常是 "4x",測試可替換成假 command。

func (*ProcessManager) List

func (pm *ProcessManager) List() []RunInfo

List 回傳所有執行中的 run。

func (*ProcessManager) SetMaxParallel

func (pm *ProcessManager) SetMaxParallel(n int)

SetMaxParallel 更新最大併發數,供設定變更後即時生效。

func (*ProcessManager) Shutdown

func (pm *ProcessManager) Shutdown()

Shutdown 終止所有仍在執行中的 subprocess。

func (*ProcessManager) Start

func (pm *ProcessManager) Start(featureID, runner string, maxRounds int, profile string, overrides []phaseOverrideReq) (*RunInfo, error)

Start 啟動一個 4x run subprocess。profile 與 overrides 為本次 run 的 pipeline profile 與 per-phase 臨時覆寫(皆可為空),會轉成對應 CLI args 傳給子程序、不寫回 settings.json。

func (*ProcessManager) Stop

func (pm *ProcessManager) Stop(id string) error

Stop 終止指定的 run(SIGTERM → 5 秒 → SIGKILL)。

type ProcessWorkspace added in v0.3.6

type ProcessWorkspace interface {
	AppendEvent(featureID string, evt protocol.Event) error
	ReadState(featureID string) (protocol.State, error)
	WriteState(featureID string, s protocol.State) error
}

ProcessWorkspace 是 ProcessManager 依賴的最小 workspace 介面,只涵蓋啟動/監控子程序需要的方法, 讓測試可注入假 workspace 而不需完整 *protocol.Workspace。*protocol.Workspace 已滿足此介面。

type ProjectEntry

type ProjectEntry struct {
	Path       string    `json:"path"`
	LastOpened time.Time `json:"lastOpened"`
}

ProjectEntry 記錄一個最近開過的專案

type ProjectListItem

type ProjectListItem struct {
	ID        string `json:"id"`
	Name      string `json:"name"`
	Path      string `json:"path"`
	TaskCount int    `json:"taskCount"`
}

ProjectListItem 是 GET /api/projects 的回應項目

type ProjectRegistry

type ProjectRegistry struct {
	// contains filtered or unexported fields
}

ProjectRegistry 管理多個 workspace 的 in-memory 註冊表

func NewProjectRegistry

func NewProjectRegistry() *ProjectRegistry

NewProjectRegistry 建立空的 registry

func (*ProjectRegistry) Add

Add 註冊一個 workspace,回傳分配的 project ID(base name,重名時加數字後綴)

func (*ProjectRegistry) Count

func (r *ProjectRegistry) Count() int

Count 回傳已註冊專案數量,不做 disk I/O

func (*ProjectRegistry) Get

Get 取得指定 ID 的 workspace

func (*ProjectRegistry) List

func (r *ProjectRegistry) List() []ProjectListItem

List 回傳所有已註冊專案的資訊

func (*ProjectRegistry) Remove

func (r *ProjectRegistry) Remove(id string) bool

Remove 從 registry 移除指定 ID

func (*ProjectRegistry) ShutdownAll

func (r *ProjectRegistry) ShutdownAll()

ShutdownAll 終止所有專案的 subprocess

type RecentProjects

type RecentProjects struct {
	Projects []ProjectEntry `json:"projects"`
}

RecentProjects 管理最近開過的專案列表(LRU 順序)

func LoadRecentProjects

func LoadRecentProjects(path string) (*RecentProjects, error)

LoadRecentProjects 讀取 recent-projects.json,檔案不存在時回傳空列表

func (*RecentProjects) Remove

func (rp *RecentProjects) Remove(path string)

Remove 從列表移除指定路徑

func (*RecentProjects) Touch

func (rp *RecentProjects) Touch(path string)

Touch 將路徑加到最前面(若已存在則移到最前),超過上限時淘汰最舊

type RunInfo

type RunInfo struct {
	ID        string    `json:"id"`
	FeatureID string    `json:"featureId"`
	Runner    string    `json:"runner"`
	Profile   string    `json:"profile,omitempty"`
	Pid       int       `json:"pid,omitempty"`
	Cmd       Cmd       `json:"-"`
	StartTime time.Time `json:"startTime"`
	// contains filtered or unexported fields
}

RunInfo 描述一個執行中的 4x run subprocess。

type WorkspaceResolver

type WorkspaceResolver func(r *http.Request) (*protocol.CachedWorkspace, *ProcessManager, error)

WorkspaceResolver 從 HTTP request 解析出該請求對應的 workspace 與 process manager。 讓 NewMux 的路由表只定義一次:單一專案模式用 singleResolver 回傳固定實例,多專案模式用 multiResolver 依 URL prefix(經外層 mux 注入 context)或 compat 邏輯查找對應專案。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL