MCPcopy Create free account
hub / github.com/coder/agentapi / runServer

Function runServer

cmd/server/server.go:80–295  ·  view source on GitHub ↗
(ctx context.Context, logger *slog.Logger, argsToPass []string)

Source from the content-addressed store, hash-verified

78}
79
80func runServer(ctx context.Context, logger *slog.Logger, argsToPass []string) error {
81 agent := argsToPass[0]
82 agentTypeValue := viper.GetString(FlagType)
83 agentType, err := parseAgentType(agent, agentTypeValue)
84 if err != nil {
85 return xerrors.Errorf("failed to parse agent type: %w", err)
86 }
87
88 termWidth := viper.GetUint16(FlagTermWidth)
89 termHeight := viper.GetUint16(FlagTermHeight)
90
91 if termWidth < 10 {
92 return xerrors.Errorf("term width must be at least 10")
93 }
94 if termHeight < 10 {
95 return xerrors.Errorf("term height must be at least 10")
96 }
97
98 // Read stdin if it's piped, to be used as initial prompt
99 initialPrompt := viper.GetString(FlagInitialPrompt)
100 if initialPrompt == "" {
101 if !isatty.IsTerminal(os.Stdin.Fd()) {
102 if stdinData, err := io.ReadAll(os.Stdin); err != nil {
103 return xerrors.Errorf("failed to read stdin: %w", err)
104 } else if len(stdinData) > 0 {
105 initialPrompt = string(stdinData)
106 logger.Info("Read initial prompt from stdin", "bytes", len(stdinData))
107 }
108 }
109 }
110
111 // Get the variables related to state management
112 stateFile := viper.GetString(FlagStateFile)
113 loadState := false
114 saveState := false
115
116 // Validate state file configuration
117 if stateFile != "" {
118 if !viper.IsSet(FlagLoadState) {
119 loadState = true
120 } else {
121 loadState = viper.GetBool(FlagLoadState)
122 }
123
124 if !viper.IsSet(FlagSaveState) {
125 saveState = true
126 } else {
127 saveState = viper.GetBool(FlagSaveState)
128 }
129 } else {
130 if viper.IsSet(FlagLoadState) && viper.GetBool(FlagLoadState) {
131 return xerrors.Errorf("--load-state requires --state-file to be set")
132 }
133 if viper.IsSet(FlagSaveState) && viper.GetBool(FlagSaveState) {
134 return xerrors.Errorf("--save-state requires --state-file to be set")
135 }
136 }
137

Callers 1

CreateServerCmdFunction · 0.85

Calls 15

GetOpenAPIMethod · 0.95
WaitMethod · 0.95
ReadScreenMethod · 0.95
StopMethod · 0.95
StartMethod · 0.95
SaveStateMethod · 0.95
CloseMethod · 0.95
SetupACPFunction · 0.92
SetupProcessFunction · 0.92
NewServerFunction · 0.92
TransportTypeAlias · 0.92
parseAgentTypeFunction · 0.85

Tested by

no test coverage detected