MCPcopy
hub / github.com/wavetermdev/waveterm / RunOpenAIChatStep

Function RunOpenAIChatStep

pkg/aiusechat/openai/openai-backend.go:463–578  ·  view source on GitHub ↗
(
	ctx context.Context,
	sse *sse.SSEHandlerCh,
	chatOpts uctypes.WaveChatOpts,
	cont *uctypes.WaveContinueResponse,
)

Source from the content-addressed store, hash-verified

461}
462
463func RunOpenAIChatStep(
464 ctx context.Context,
465 sse *sse.SSEHandlerCh,
466 chatOpts uctypes.WaveChatOpts,
467 cont *uctypes.WaveContinueResponse,
468) (*uctypes.WaveStopReason, []*OpenAIChatMessage, *uctypes.RateLimitInfo, error) {
469 if sse == nil {
470 return nil, nil, nil, errors.New("sse handler is nil")
471 }
472
473 // Get chat from store
474 chat := chatstore.DefaultChatStore.Get(chatOpts.ChatId)
475 if chat == nil {
476 return nil, nil, nil, fmt.Errorf("chat not found: %s", chatOpts.ChatId)
477 }
478
479 // Validate that chatOpts.Config match the chat's stored configuration
480 if chat.APIType != chatOpts.Config.APIType {
481 return nil, nil, nil, fmt.Errorf("API type mismatch: chat has %s, chatOpts has %s", chat.APIType, chatOpts.Config.APIType)
482 }
483 if !uctypes.AreModelsCompatible(chat.APIType, chat.Model, chatOpts.Config.Model) {
484 return nil, nil, nil, fmt.Errorf("model mismatch: chat has %s, chatOpts has %s", chat.Model, chatOpts.Config.Model)
485 }
486 if chat.APIVersion != chatOpts.Config.APIVersion {
487 return nil, nil, nil, fmt.Errorf("API version mismatch: chat has %s, chatOpts has %s", chat.APIVersion, chatOpts.Config.APIVersion)
488 }
489
490 // Context with timeout if provided.
491 if chatOpts.Config.TimeoutMs > 0 {
492 var cancel context.CancelFunc
493 ctx, cancel = context.WithTimeout(ctx, time.Duration(chatOpts.Config.TimeoutMs)*time.Millisecond)
494 defer cancel()
495 }
496
497 // Validate continuation if provided
498 if cont != nil {
499 if !uctypes.AreModelsCompatible(chat.APIType, chatOpts.Config.Model, cont.Model) {
500 return nil, nil, nil, fmt.Errorf("cannot continue with a different model, model:%q, cont-model:%q", chatOpts.Config.Model, cont.Model)
501 }
502 }
503
504 // Convert GenAIMessages to input objects (OpenAIMessage or OpenAIFunctionCallInput)
505 var inputs []any
506 for _, genMsg := range chat.NativeMessages {
507 // Cast to OpenAIChatMessage
508 chatMsg, ok := genMsg.(*OpenAIChatMessage)
509 if !ok {
510 return nil, nil, nil, fmt.Errorf("expected OpenAIChatMessage, got %T", genMsg)
511 }
512
513 // Convert to appropriate input type based on what's populated
514 if chatMsg.Message != nil {
515 // Clean message to remove preview URLs
516 cleanedMsg := chatMsg.Message.cleanAndCopy()
517 inputs = append(inputs, *cleanedMsg)
518 } else if chatMsg.FunctionCall != nil {
519 cleanedFunctionCall := chatMsg.FunctionCall.clean()
520 inputs = append(inputs, *cleanedFunctionCall)

Callers 1

RunChatStepMethod · 0.92

Calls 12

AreModelsCompatibleFunction · 0.92
MakeHTTPClientFunction · 0.92
ParseRateLimitHeaderFunction · 0.92
buildOpenAIHTTPRequestFunction · 0.85
parseOpenAIHTTPErrorFunction · 0.85
cleanAndCopyMethod · 0.80
SetupSSEMethod · 0.80
sanitizeHostnameInErrorFunction · 0.70
CloseMethod · 0.65
GetMethod · 0.45
cleanMethod · 0.45

Tested by

no test coverage detected