MCPcopy
hub / github.com/docker/docker-agent / PrepareUserMessage

Function PrepareUserMessage

pkg/cli/runner.go:347–374  ·  view source on GitHub ↗

PrepareUserMessage resolves commands, parses /attach directives, and creates a user message with optional image attachment. This is the common flow for both TUI and CLI modes. PrepareUserMessage resolves commands, parses /attach directives, and creates a user message with optional image attachment.

(ctx context.Context, rt runtime.Runtime, userInput, globalAttachPath string)

Source from the content-addressed store, hash-verified

345// pass the attachment path to session.Session.AddAttachedFile so sub-agents
346// inherit the file context.
347func PrepareUserMessage(ctx context.Context, rt runtime.Runtime, userInput, globalAttachPath string) (*session.Message, string, error) {
348 // Resolve any /command to its prompt text BEFORE switching agents.
349 // This ensures the command is looked up in the original agent's command table.
350 resolvedContent := runtime.ResolveCommand(ctx, rt, userInput)
351
352 // Switch the active agent if the /command targets a sub-agent.
353 // This must happen before the message is added to the session so the
354 // next runtime turn runs on the right agent.
355 if cmd, _, ok := runtime.LookupCommand(ctx, rt, userInput); ok && cmd.Agent != "" {
356 if err := rt.SetCurrentAgent(ctx, cmd.Agent); err != nil {
357 slog.WarnContext(ctx, "Failed to switch agent for /command", "agent", cmd.Agent, "error", err)
358 return nil, "", fmt.Errorf("switch agent %q: %w", cmd.Agent, err)
359 }
360 // Agent-only command with no trailing args: switch but send no message.
361 if resolvedContent == "" {
362 return nil, "", nil
363 }
364 }
365
366 // Parse for /attach commands in the message
367 messageText, attachPath := ParseAttachCommand(resolvedContent)
368
369 // Use either the per-message attachment or the global one
370 finalAttachPath := cmp.Or(attachPath, globalAttachPath)
371
372 msg, attachedPath := CreateUserMessageWithAttachment(ctx, messageText, finalAttachPath)
373 return msg, attachedPath, nil
374}
375
376func ParseAttachCommand(userInput string) (messageText, attachPath string) {
377 lines := strings.Split(userInput, "\n")

Calls 5

ResolveCommandFunction · 0.92
LookupCommandFunction · 0.92
ParseAttachCommandFunction · 0.85
SetCurrentAgentMethod · 0.65