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

Function RunAIChat

pkg/aiusechat/usechat.go:389–493  ·  view source on GitHub ↗
(ctx context.Context, sseHandler *sse.SSEHandlerCh, backend UseChatBackend, chatOpts uctypes.WaveChatOpts)

Source from the content-addressed store, hash-verified

387}
388
389func RunAIChat(ctx context.Context, sseHandler *sse.SSEHandlerCh, backend UseChatBackend, chatOpts uctypes.WaveChatOpts) (*uctypes.AIMetrics, error) {
390 if !activeChats.SetUnless(chatOpts.ChatId, true) {
391 return nil, fmt.Errorf("chat %s is already running", chatOpts.ChatId)
392 }
393 defer activeChats.Delete(chatOpts.ChatId)
394
395 stepNum := chatstore.DefaultChatStore.CountUserMessages(chatOpts.ChatId)
396 aiProvider := chatOpts.Config.Provider
397 if aiProvider == "" {
398 aiProvider = uctypes.AIProvider_Custom
399 }
400 isLocal := isLocalEndpoint(chatOpts.Config.Endpoint)
401 metrics := &uctypes.AIMetrics{
402 ChatId: chatOpts.ChatId,
403 StepNum: stepNum,
404 Usage: uctypes.AIUsage{
405 APIType: chatOpts.Config.APIType,
406 Model: chatOpts.Config.Model,
407 },
408 WidgetAccess: chatOpts.WidgetAccess,
409 ToolDetail: make(map[string]int),
410 ThinkingLevel: chatOpts.Config.ThinkingLevel,
411 AIMode: chatOpts.Config.AIMode,
412 AIProvider: aiProvider,
413 IsLocal: isLocal,
414 }
415 firstStep := true
416 var cont *uctypes.WaveContinueResponse
417 for {
418 if chatOpts.TabStateGenerator != nil {
419 tabState, tabTools, tabId, tabErr := chatOpts.TabStateGenerator()
420 if tabErr == nil {
421 chatOpts.TabState = tabState
422 chatOpts.TabTools = tabTools
423 chatOpts.TabId = tabId
424 }
425 }
426 if chatOpts.BuilderAppGenerator != nil {
427 appGoFile, appStaticFiles, platformInfo, appErr := chatOpts.BuilderAppGenerator()
428 if appErr == nil {
429 chatOpts.AppGoFile = appGoFile
430 chatOpts.AppStaticFiles = appStaticFiles
431 chatOpts.PlatformInfo = platformInfo
432 }
433 }
434 stopReason, rtnMessages, err := runAIChatStep(ctx, sseHandler, backend, chatOpts, cont)
435 metrics.RequestCount++
436 if chatOpts.Config.IsWaveProxy() {
437 metrics.ProxyReqCount++
438 if chatOpts.Config.IsPremiumModel() {
439 metrics.PremiumReqCount++
440 }
441 }
442 if stopReason != nil {
443 logutil.DevPrintf("stopreason: %s (%s) (%s) (%s)\n", stopReason.Kind, stopReason.ErrorText, stopReason.ErrorType, stopReason.RawReason)
444 }
445 if len(rtnMessages) > 0 {
446 usage := getUsage(rtnMessages)

Callers 1

WaveAIPostMessageWrapFunction · 0.85

Calls 14

DevPrintfFunction · 0.92
isLocalEndpointFunction · 0.85
runAIChatStepFunction · 0.85
getUsageFunction · 0.85
processAllToolCallsFunction · 0.85
SetUnlessMethod · 0.80
CountUserMessagesMethod · 0.80
IsWaveProxyMethod · 0.80
IsPremiumModelMethod · 0.80
AiMsgErrorMethod · 0.80
AiMsgFinishMethod · 0.80
PostMessageMethod · 0.80

Tested by

no test coverage detected