MCPcopy Index your code
hub / github.com/docker/docker-agent / chatCompletion

Method chatCompletion

pkg/chatserver/server.go:474–502  ·  view source on GitHub ↗

chatCompletion runs the agent to completion and replies with one non-streaming OpenAI ChatCompletion object. It returns the agent run error (nil on success) so the caller can decide whether to commit the conversation; the HTTP response — success or error envelope — is always written here.

(c echo.Context, rt runtime.Runtime, sess *session.Session, model string)

Source from the content-addressed store, hash-verified

472// conversation; the HTTP response — success or error envelope — is always
473// written here.
474func (s *server) chatCompletion(c echo.Context, rt runtime.Runtime, sess *session.Session, model string) error {
475 var toolCalls []ToolCallReference
476 emit := agentEmit{
477 onToolCall: func(tc ToolCallReference) {
478 toolCalls = append(toolCalls, tc)
479 },
480 }
481 if err := runAgentLoop(c.Request().Context(), rt, sess, emit); err != nil {
482 _ = writeError(c, http.StatusInternalServerError, fmt.Sprintf("agent execution failed: %v", err))
483 return err
484 }
485
486 return c.JSON(http.StatusOK, ChatCompletionResponse{
487 ID: newChatID(),
488 Object: "chat.completion",
489 Created: time.Now().Unix(),
490 Model: model,
491 Choices: []ChatCompletionChoice{{
492 Index: 0,
493 Message: ChatCompletionMessage{
494 Role: "assistant",
495 Content: sess.GetLastAssistantMessageContent(),
496 ToolCalls: toolCalls,
497 },
498 FinishReason: "stop",
499 }},
500 Usage: sessionUsage(sess),
501 })
502}
503
504// streamChatCompletion runs the agent and streams its response back to the
505// client as Server-Sent Events in OpenAI's chat.completion.chunk format.

Callers 1

handleChatCompletionsMethod · 0.95

Calls 7

runAgentLoopFunction · 0.85
newChatIDFunction · 0.85
sessionUsageFunction · 0.85
ContextMethod · 0.80
NowMethod · 0.80
writeErrorFunction · 0.70

Tested by

no test coverage detected