From 6427e70c0759e41c0f995e8bc5eb4de1715f704c Mon Sep 17 00:00:00 2001 From: Hugo Dutka Date: Wed, 4 Jun 2025 14:22:46 +0200 Subject: [PATCH 1/2] make the terminal width and height configurable --- cmd/server/server.go | 19 ++++++++++++++++++- lib/httpapi/setup.go | 19 +++++++++++++------ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/cmd/server/server.go b/cmd/server/server.go index 7feadf1..58d0964 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -23,6 +23,8 @@ var ( port int printOpenAPI bool chatBasePath string + termWidth uint16 + termHeight uint16 ) type AgentType = msgfmt.AgentType @@ -78,11 +80,24 @@ func runServer(ctx context.Context, logger *slog.Logger, argsToPass []string) er if err != nil { return xerrors.Errorf("failed to parse agent type: %w", err) } + + if termWidth < 10 { + return xerrors.Errorf("term width must be at least 10") + } + if termHeight < 10 { + return xerrors.Errorf("term height must be at least 10") + } + var process *termexec.Process if printOpenAPI { process = nil } else { - process, err = httpapi.SetupProcess(ctx, agent, argsToPass[1:]...) + process, err = httpapi.SetupProcess(ctx, httpapi.SetupProcessConfig{ + Program: agent, + ProgramArgs: argsToPass[1:], + TerminalWidth: termWidth, + TerminalHeight: termHeight, + }) if err != nil { return xerrors.Errorf("failed to setup process: %w", err) } @@ -139,4 +154,6 @@ func init() { ServerCmd.Flags().IntVarP(&port, "port", "p", 3284, "Port to run the server on") ServerCmd.Flags().BoolVarP(&printOpenAPI, "print-openapi", "P", false, "Print the OpenAPI schema to stdout and exit") ServerCmd.Flags().StringVarP(&chatBasePath, "chat-base-path", "c", "/chat", "Base path for assets and routes used in the static files of the chat interface") + ServerCmd.Flags().Uint16VarP(&termWidth, "term-width", "W", 80, "Width of the emulated terminal") + ServerCmd.Flags().Uint16VarP(&termHeight, "term-height", "H", 1000, "Height of the emulated terminal") } diff --git a/lib/httpapi/setup.go b/lib/httpapi/setup.go index 298a800..8328991 100644 --- a/lib/httpapi/setup.go +++ b/lib/httpapi/setup.go @@ -13,16 +13,23 @@ import ( "github.com/coder/agentapi/lib/termexec" ) -func SetupProcess(ctx context.Context, program string, programArgs ...string) (*termexec.Process, error) { +type SetupProcessConfig struct { + Program string + ProgramArgs []string + TerminalWidth uint16 + TerminalHeight uint16 +} + +func SetupProcess(ctx context.Context, config SetupProcessConfig) (*termexec.Process, error) { logger := logctx.From(ctx) - logger.Info(fmt.Sprintf("Running: %s %s", program, strings.Join(programArgs, " "))) + logger.Info(fmt.Sprintf("Running: %s %s", config.Program, strings.Join(config.ProgramArgs, " "))) process, err := termexec.StartProcess(ctx, termexec.StartProcessConfig{ - Program: program, - Args: programArgs, - TerminalWidth: 80, - TerminalHeight: 1000, + Program: config.Program, + Args: config.ProgramArgs, + TerminalWidth: config.TerminalWidth, + TerminalHeight: config.TerminalHeight, }) if err != nil { logger.Error(fmt.Sprintf("Error starting process: %v", err)) From efcc8ce0eea64f7751607ef3ff744f33a28600b0 Mon Sep 17 00:00:00 2001 From: Hugo Dutka Date: Wed, 4 Jun 2025 14:24:56 +0200 Subject: [PATCH 2/2] update docs --- lib/httpapi/events.go | 2 +- lib/httpapi/models.go | 2 +- openapi.json | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/httpapi/events.go b/lib/httpapi/events.go index ccca17d..1e6281d 100644 --- a/lib/httpapi/events.go +++ b/lib/httpapi/events.go @@ -39,7 +39,7 @@ func (a AgentStatus) Schema(r huma.Registry) *huma.Schema { type MessageUpdateBody struct { Id int `json:"id" doc:"Unique identifier for the message. This identifier also represents the order of the message in the conversation history."` Role st.ConversationRole `json:"role" doc:"Role of the message author"` - Message string `json:"message" doc:"Message content. The message is formatted as it appears in the agent's terminal session, meaning it consists of lines of text with 80 characters per line."` + Message string `json:"message" doc:"Message content. The message is formatted as it appears in the agent's terminal session, meaning that, by default, it consists of lines of text with 80 characters per line."` Time time.Time `json:"time" doc:"Timestamp of the message"` } diff --git a/lib/httpapi/models.go b/lib/httpapi/models.go index 6e33dfd..8f969f9 100644 --- a/lib/httpapi/models.go +++ b/lib/httpapi/models.go @@ -27,7 +27,7 @@ func (m MessageType) Schema(r huma.Registry) *huma.Schema { // Message represents a message type Message struct { Id int `json:"id" doc:"Unique identifier for the message. This identifier also represents the order of the message in the conversation history."` - Content string `json:"content" example:"Hello world" doc:"Message content. The message is formatted as it appears in the agent's terminal session, meaning it consists of lines of text with 80 characters per line."` + Content string `json:"content" example:"Hello world" doc:"Message content. The message is formatted as it appears in the agent's terminal session, meaning that, by default, it consists of lines of text with 80 characters per line."` Role st.ConversationRole `json:"role" doc:"Role of the message author"` Time time.Time `json:"time" doc:"Timestamp of the message"` } diff --git a/openapi.json b/openapi.json index 79495e2..f4e2e88 100644 --- a/openapi.json +++ b/openapi.json @@ -108,7 +108,7 @@ "additionalProperties": false, "properties": { "content": { - "description": "Message content. The message is formatted as it appears in the agent's terminal session, meaning it consists of lines of text with 80 characters per line.", + "description": "Message content. The message is formatted as it appears in the agent's terminal session, meaning that, by default, it consists of lines of text with 80 characters per line.", "examples": [ "Hello world" ], @@ -209,7 +209,7 @@ "type": "integer" }, "message": { - "description": "Message content. The message is formatted as it appears in the agent's terminal session, meaning it consists of lines of text with 80 characters per line.", + "description": "Message content. The message is formatted as it appears in the agent's terminal session, meaning that, by default, it consists of lines of text with 80 characters per line.", "type": "string" }, "role": {