web

package
v0.6.4 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2026 License: MIT Imports: 42 Imported by: 0

Documentation

Overview

Package web implements the jcode web server and API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Engine added in v0.6.4

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

Engine is the per-task run state of the web server — one independent top-level session (a "task"). It holds exactly the fields that were Server singletons in the single-active design: the agent, its conversation history, recorder, per-task token tracker, approval axis, working dir, and event handler.

The refactor toward concurrent tasks proceeds in stages:

  • INC-3 (this): the fields move out of Server into Engine, Server embeds one bootstrap *Engine, and field promotion keeps every existing s.<field> reference compiling and behaving identically (concurrency stays 1).
  • INC-5: handlers resolve an Engine by task_id (Server.tasks) instead of relying on the promoted bootstrap.
  • INC-7+: the global run gate is removed so multiple Engines run at once; `running` becomes the per-task busy flag and each Engine gets its own ctx.

Kernel types (session.Recorder, tools.Env, runner.ApprovalState, handler.WebHandler, model.TokenUsage) are reused unchanged — parallelism is achieved by instantiating one set of them per Engine, never by editing them.

type EngineConfig added in v0.6.4

type EngineConfig struct {
	TaskID         string
	Pwd            string
	Mode           string
	ProviderName   string
	ModelName      string
	Agent          *adk.ChatModelAgent
	Env            *tools.Env
	TodoStore      *tools.TodoStore
	Recorder       *session.Recorder
	TokenUsage     *model.TokenUsage
	ApprovalState  *runner.ApprovalState
	Handler        *handler.WebHandler
	EventHandler   handler.AgentEventHandler
	BreakdownFn    func() usage.ContextBreakdown
	CreateAgent    func(providerName, modelName string) (*adk.ChatModelAgent, error)
	RebuildForMode func(planMode bool) (*adk.ChatModelAgent, error)
}

EngineConfig carries the per-task pieces a factory (command.buildWebTask) produces for one task. The web package owns Engine's unexported fields, so the command package hands them over through this exported struct and the server assembles the Engine via newEngine.

type Server

type Server struct {
	// Engine is the bootstrap/active task's run state. Its fields (agent, history,
	// recorder, pwd, env, tokenUsage, approvalState, handler, …) are PROMOTED onto
	// Server, so existing s.<field> accesses resolve to s.Engine.<field> while
	// there is a single active task. Per-task routing (Server.tasks) supersedes
	// this promotion in a later increment. Always non-nil after NewServer.
	*Engine
	// contains filtered or unexported fields
}

Server is the jcode web server.

func NewServer

func NewServer(cfg *ServerConfig) *Server

NewServer creates a new web server.

func (*Server) CloseAllEngines added in v0.6.4

func (s *Server) CloseAllEngines()

CloseAllEngines tears down every live engine. Called on server shutdown.

func (*Server) Handler

func (s *Server) Handler() *handler.WebHandler

Handler returns the underlying WebHandler for external wiring (e.g. approval routing).

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Start starts the web server. Blocks until context is cancelled.

func (*Server) SubmitMessage added in v0.1.1

func (s *Server) SubmitMessage(message, source string) bool

SubmitMessage submits a message for agent processing from an external source (e.g. WeChat inbound message). Returns false if the agent is busy.

type ServerConfig

type ServerConfig struct {
	Port               int
	Host               string
	OpenBrowser        bool
	Pwd                string
	Version            string
	Agent              *adk.ChatModelAgent
	CreateAgent        func(providerName, modelName string) (*adk.ChatModelAgent, error)
	RebuildForMode     func(planMode bool) (*adk.ChatModelAgent, error)
	NewEngine          func(taskID, pwd, mode string) (*EngineConfig, error)                                             // factory for new concurrent task engines (local)
	NewRemoteEngine    func(taskID string, executor tools.RemoteExecutor, remotePwd, mode string) (*EngineConfig, error) // remote sibling of NewEngine (SSH or Docker)
	InitialMode        string                                                                                            // unified startup mode string ("approval"/"plan"/"full_access")
	TodoStore          *tools.TodoStore
	Recorder           *session.Recorder
	Tracer             *telemetry.LangfuseTracer
	Env                *tools.Env
	ProviderName       string
	ModelName          string
	Config             *config.Config
	Registry           *model.ModelRegistry
	ApprovalState      *runner.ApprovalState
	SkillLoader        *skills.Loader
	ReloadMCP          func(servers map[string]*config.MCPServer) ([]tools.MCPStatus, error) // optional: hot-reload MCP tools
	InitialMCPStatuses []tools.MCPStatus                                                     // statuses from the startup MCP load
	WechatClient       channel.Channel                                                       // optional WeChat channel
	WebHandler         *handler.WebHandler                                                   // optional: pre-created handler for sharing with tools
	EventHandler       handler.AgentEventHandler                                             // optional: handler for runner (e.g. NotifyingHandler)
	NeedsSetup         bool                                                                  // true when no providers are configured (setup mode)
	TokenUsage         *model.TokenUsage                                                     // optional: shared token tracker (created when nil)
	ContextBreakdownFn func() usage.ContextBreakdown                                         // optional: live per-task context breakdown
}

ServerConfig holds the configuration for creating a new Server.

type WSBroker

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

WSBroker manages WebSocket client connections and broadcasts events.

func NewWSBroker

func NewWSBroker() *WSBroker

NewWSBroker creates a new WebSocket broker.

func (*WSBroker) Broadcast

func (b *WSBroker) Broadcast(event WSEvent)

Broadcast sends an event to all connected WebSocket clients.

func (*WSBroker) ClientCount

func (b *WSBroker) ClientCount() int

ClientCount returns the number of connected clients.

func (*WSBroker) Close

func (b *WSBroker) Close()

Close shuts down the broker.

func (*WSBroker) Register

func (b *WSBroker) Register(conn *websocket.Conn) (uint64, *WSClient, func())

Register adds a new WebSocket client and returns an ID + unsubscribe func.

type WSClient

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

WSClient represents a connected WebSocket client.

type WSEvent

type WSEvent struct {
	Type string `json:"type"`
	// TaskID tags the event with the task (engine) it came from so the client can
	// route it to the right task view, and so the broker can deliver it only to
	// clients subscribed to that task. Empty for global/server-wide events
	// (mcp_changed, model_changed, pong, …), which every client receives.
	TaskID string `json:"task_id,omitempty"`
	Data   any    `json:"data,omitempty"`
}

WSEvent is a WebSocket message envelope.

type WSIncoming

type WSIncoming struct {
	Type string          `json:"type"`
	Data json.RawMessage `json:"data,omitempty"`
}

WSIncoming represents a message from the client over WebSocket.

Jump to

Keyboard shortcuts

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