MCPcopy Create free account
hub / github.com/astercloud/aster / handleChat

Method handleChat

server/handlers/websocket.go:224–434  ·  view source on GitHub ↗

handleChat handles chat messages

(wsConn *WebSocketConnection, payload map[string]any)

Source from the content-addressed store, hash-verified

222
223// handleChat handles chat messages
224func (h *WebSocketHandler) handleChat(wsConn *WebSocketConnection, payload map[string]any) {
225 // Reset read deadline to prevent timeout during long-running LLM requests
226 _ = wsConn.Conn.SetReadDeadline(time.Now().Add(5 * time.Minute))
227
228 // Extract parameters
229 templateID, _ := payload["template_id"].(string)
230 if templateID == "" {
231 templateID = "chat"
232 }
233
234 input, _ := payload["input"].(string)
235 if input == "" {
236 h.sendError(wsConn, "missing_input", "Input message is required")
237 return
238 }
239
240 var ag *agent.Agent
241 var err error
242
243 // Reuse existing agent if available, otherwise create new one
244 if wsConn.Agent != nil {
245 ag = wsConn.Agent
246 logging.Info(wsConn.ctx, "websocket.agent.reused", map[string]any{
247 "agent_id": ag.ID(),
248 })
249 } else {
250 // Create agent configuration
251 cfg := &types.AgentConfig{
252 TemplateID: templateID,
253 }
254
255 // Handle model config if provided
256 if modelConfigData, ok := payload["model_config"].(map[string]any); ok {
257 modelConfig := &types.ModelConfig{}
258 if provider, ok := modelConfigData["provider"].(string); ok {
259 modelConfig.Provider = provider
260 }
261 if model, ok := modelConfigData["model"].(string); ok {
262 modelConfig.Model = model
263 }
264 if apiKey, ok := modelConfigData["api_key"].(string); ok {
265 modelConfig.APIKey = apiKey
266 }
267
268 // Fill API key from environment if missing
269 if modelConfig.APIKey == "" && modelConfig.Provider != "" {
270 var envKey string
271 switch modelConfig.Provider {
272 case "deepseek":
273 envKey = os.Getenv("DEEPSEEK_API_KEY")
274 case "anthropic":
275 envKey = os.Getenv("ANTHROPIC_API_KEY")
276 case "openai":
277 envKey = os.Getenv("OPENAI_API_KEY")
278 default:
279 envKey = os.Getenv(strings.ToUpper(modelConfig.Provider) + "_API_KEY")
280 }
281 if envKey != "" {

Callers 1

handleMessageMethod · 0.95

Calls 15

sendErrorMethod · 0.95
IDMethod · 0.95
SubscribeMethod · 0.95
UnsubscribeMethod · 0.95
sendMessageMethod · 0.95
StreamMethod · 0.95
InfoFunction · 0.92
CreateFunction · 0.92
ErrorFunction · 0.92
RecvMethod · 0.80
AddMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected