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)
| 472 | // conversation; the HTTP response — success or error envelope — is always |
| 473 | // written here. |
| 474 | func (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. |
no test coverage detected