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

Function WaveAIPostMessageWrap

pkg/aiusechat/usechat.go:549–591  ·  view source on GitHub ↗
(ctx context.Context, sseHandler *sse.SSEHandlerCh, message *uctypes.AIMessage, chatOpts uctypes.WaveChatOpts)

Source from the content-addressed store, hash-verified

547}
548
549func WaveAIPostMessageWrap(ctx context.Context, sseHandler *sse.SSEHandlerCh, message *uctypes.AIMessage, chatOpts uctypes.WaveChatOpts) error {
550 startTime := time.Now()
551
552 // Convert AIMessage to native chat message using backend
553 backend, err := GetBackendByAPIType(chatOpts.Config.APIType)
554 if err != nil {
555 return err
556 }
557 convertedMessage, err := backend.ConvertAIMessageToNativeChatMessage(*message)
558 if err != nil {
559 return fmt.Errorf("message conversion failed: %w", err)
560 }
561
562 // Post message to chat store
563 if err := chatstore.DefaultChatStore.PostMessage(chatOpts.ChatId, &chatOpts.Config, convertedMessage); err != nil {
564 return fmt.Errorf("failed to store message: %w", err)
565 }
566
567 metrics, err := RunAIChat(ctx, sseHandler, backend, chatOpts)
568 if metrics != nil {
569 metrics.RequestDuration = int(time.Since(startTime).Milliseconds())
570 for _, part := range message.Parts {
571 if part.Type == uctypes.AIMessagePartTypeText {
572 metrics.TextLen += len(part.Text)
573 } else if part.Type == uctypes.AIMessagePartTypeFile {
574 mimeType := strings.ToLower(part.MimeType)
575 if strings.HasPrefix(mimeType, "image/") {
576 metrics.ImageCount++
577 } else if mimeType == "application/pdf" {
578 metrics.PDFCount++
579 } else {
580 metrics.TextDocCount++
581 }
582 }
583 }
584 log.Printf("WaveAI call metrics: requests=%d tools=%d premium=%d proxy=%d images=%d pdfs=%d textdocs=%d textlen=%d duration=%dms error=%v\n",
585 metrics.RequestCount, metrics.ToolUseCount, metrics.PremiumReqCount, metrics.ProxyReqCount,
586 metrics.ImageCount, metrics.PDFCount, metrics.TextDocCount, metrics.TextLen, metrics.RequestDuration, metrics.HadError)
587
588 sendAIMetricsTelemetry(ctx, metrics)
589 }
590 return err
591}
592
593func sendAIMetricsTelemetry(ctx context.Context, metrics *uctypes.AIMetrics) {
594 event := telemetrydata.MakeTEvent("waveai:post", telemetrydata.TEventProps{

Callers 7

testOpenAIFunction · 0.92
testOpenAICompFunction · 0.92
testOpenRouterFunction · 0.92
testNanoGPTFunction · 0.92
testAnthropicFunction · 0.92
testGeminiFunction · 0.92
WaveAIPostMessageHandlerFunction · 0.85

Calls 5

GetBackendByAPITypeFunction · 0.85
RunAIChatFunction · 0.85
sendAIMetricsTelemetryFunction · 0.85
PostMessageMethod · 0.80

Tested by

no test coverage detected