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

Method sendMessage

lib/screentracker/pty_conversation.go:404–433  ·  view source on GitHub ↗

sendMessage sends a message to the agent. It acquires and releases c.lock around the parts that access shared state, but releases it during writeStabilize to avoid blocking the snapshot loop.

(ctx context.Context, messageParts ...MessagePart)

Source from the content-addressed store, hash-verified

402// around the parts that access shared state, but releases it during
403// writeStabilize to avoid blocking the snapshot loop.
404func (c *PTYConversation) sendMessage(ctx context.Context, messageParts ...MessagePart) error {
405 message := buildStringFromMessageParts(messageParts)
406
407 c.lock.Lock()
408 screenBeforeMessage := c.cfg.AgentIO.ReadScreen()
409 now := c.cfg.Clock.Now()
410 c.updateLastAgentMessageLocked(screenBeforeMessage, now)
411 c.writingMessage = true
412 c.lock.Unlock()
413
414 if err := c.writeStabilize(ctx, messageParts...); err != nil {
415 c.lock.Lock()
416 defer c.lock.Unlock()
417 c.writingMessage = false
418 return xerrors.Errorf("failed to send message: %w", err)
419 }
420
421 c.lock.Lock()
422 c.screenBeforeLastUserMessage = screenBeforeMessage
423 c.messages = append(c.messages, ConversationMessage{
424 Id: len(c.messages),
425 Message: message,
426 Role: ConversationRoleUser,
427 Time: now,
428 })
429 c.userSentMessageAfterLoadState = true
430 c.writingMessage = false
431 c.lock.Unlock()
432 return nil
433}
434
435// writeStabilize writes messageParts to the PTY and waits for
436// the agent to process them. It operates in two phases:

Callers 1

StartMethod · 0.95

Calls 4

writeStabilizeMethod · 0.95
ReadScreenMethod · 0.65

Tested by

no test coverage detected