-
-
Notifications
You must be signed in to change notification settings - Fork 953
Expand file tree
/
Copy pathopenai-convertmessage.go
More file actions
573 lines (512 loc) · 17.5 KB
/
openai-convertmessage.go
File metadata and controls
573 lines (512 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// Copyright 2025, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package openai
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"strings"
"github.com/google/uuid"
"github.com/wavetermdev/waveterm/pkg/aiusechat/aiutil"
"github.com/wavetermdev/waveterm/pkg/aiusechat/uctypes"
"github.com/wavetermdev/waveterm/pkg/wavebase"
)
const (
OpenAIDefaultAPIVersion = "2024-12-31"
OpenAIDefaultMaxTokens = 4096
// "medium" verbosity is more widely supported across models than "low"
OpenAIDefaultVerbosity = "medium"
)
// convertContentBlockToParts converts a single content block to UIMessageParts
func convertContentBlockToParts(block OpenAIMessageContent, role string) []uctypes.UIMessagePart {
var parts []uctypes.UIMessagePart
switch block.Type {
case "input_text", "output_text":
if found, part := aiutil.ConvertDataUserFile(block.Text); found {
if part != nil {
parts = append(parts, *part)
}
} else {
parts = append(parts, uctypes.UIMessagePart{
Type: "text",
Text: block.Text,
})
}
case "input_image":
if role == "user" {
parts = append(parts, uctypes.UIMessagePart{
Type: "data-userfile",
Data: uctypes.UIMessageDataUserFile{
FileName: block.Filename,
MimeType: "image/*",
PreviewUrl: block.PreviewUrl,
},
})
}
case "input_file":
if role == "user" {
parts = append(parts, uctypes.UIMessagePart{
Type: "data-userfile",
Data: uctypes.UIMessageDataUserFile{
FileName: block.Filename,
MimeType: "application/pdf",
PreviewUrl: block.PreviewUrl,
},
})
}
}
return parts
}
// appendToLastUserMessage appends a text block to the last user message in the inputs slice
func appendToLastUserMessage(inputs []any, text string) {
for i := len(inputs) - 1; i >= 0; i-- {
if msg, ok := inputs[i].(OpenAIMessage); ok && msg.Role == "user" {
block := OpenAIMessageContent{
Type: "input_text",
Text: text,
}
msg.Content = append(msg.Content, block)
inputs[i] = msg
break
}
}
}
// ---------- OpenAI Request Types ----------
type StreamOptionsType struct {
IncludeObfuscation bool `json:"include_obfuscation"`
}
type ReasoningType struct {
Effort string `json:"effort,omitempty"` // "minimal", "low", "medium", "high"
Summary string `json:"summary,omitempty"` // "auto", "concise", "detailed"
}
type TextType struct {
Format interface{} `json:"format,omitempty"` // Format object, e.g. {"type": "text"}, {"type": "json_object"}, {"type": "json_schema"}
Verbosity string `json:"verbosity,omitempty"` // "low", "medium", "high"
}
type PromptType struct {
ID string `json:"id"`
Variables map[string]interface{} `json:"variables,omitempty"`
Version string `json:"version,omitempty"`
}
type OpenAIRequest struct {
Background bool `json:"background,omitempty"`
Conversation string `json:"conversation,omitempty"`
Include []string `json:"include,omitempty"`
Input []any `json:"input,omitempty"` // either OpenAIMessage or OpenAIFunctionCallInput
Instructions string `json:"instructions,omitempty"`
MaxOutputTokens int `json:"max_output_tokens,omitempty"`
MaxToolCalls int `json:"max_tool_calls,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
Model string `json:"model,omitempty"`
ParallelToolCalls bool `json:"parallel_tool_calls,omitempty"`
PreviousResponseID string `json:"previous_response_id,omitempty"`
Prompt *PromptType `json:"prompt,omitempty"`
PromptCacheKey string `json:"prompt_cache_key,omitempty"`
Reasoning *ReasoningType `json:"reasoning,omitempty"`
SafetyIdentifier string `json:"safety_identifier,omitempty"`
ServiceTier string `json:"service_tier,omitempty"` // "auto", "default", "flex", "priority"
Store bool `json:"store,omitempty"`
Stream bool `json:"stream,omitempty"`
StreamOptions *StreamOptionsType `json:"stream_options,omitempty"`
Temperature float64 `json:"temperature,omitempty"`
Text *TextType `json:"text,omitempty"`
ToolChoice interface{} `json:"tool_choice,omitempty"` // "none", "auto", "required", or object
Tools []OpenAIRequestTool `json:"tools,omitempty"`
TopLogprobs int `json:"top_logprobs,omitempty"`
TopP float64 `json:"top_p,omitempty"`
Truncation string `json:"truncation,omitempty"` // "auto", "disabled"
}
type OpenAIRequestTool struct {
Type string `json:"type"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Parameters any `json:"parameters,omitempty"`
Strict bool `json:"strict,omitempty"`
}
// ConvertToolDefinitionToOpenAI converts a generic ToolDefinition to OpenAI format
func ConvertToolDefinitionToOpenAI(tool uctypes.ToolDefinition) OpenAIRequestTool {
return OpenAIRequestTool{
Name: tool.Name,
Description: tool.Description,
Parameters: tool.InputSchema,
Strict: tool.Strict,
Type: "function",
}
}
func debugPrintReq(req *OpenAIRequest, endpoint string) {
if !wavebase.IsDevMode() {
return
}
if endpoint != uctypes.DefaultAIEndpoint {
log.Printf("endpoint: %s\n", endpoint)
}
var toolNames []string
for _, tool := range req.Tools {
toolNames = append(toolNames, tool.Name)
}
modelInfo := req.Model
var details []string
if req.Reasoning != nil && req.Reasoning.Effort != "" {
details = append(details, fmt.Sprintf("reasoning: %s", req.Reasoning.Effort))
}
if req.MaxOutputTokens > 0 {
details = append(details, fmt.Sprintf("max_tokens: %d", req.MaxOutputTokens))
}
if len(details) > 0 {
log.Printf("model %s (%s)\n", modelInfo, strings.Join(details, ", "))
} else {
log.Printf("model %s\n", modelInfo)
}
if len(toolNames) > 0 {
log.Printf("tools: %s\n", strings.Join(toolNames, ","))
}
log.Printf("inputs (%d):", len(req.Input))
for idx, input := range req.Input {
debugPrintInput(idx, input)
}
}
// buildOpenAIHTTPRequest creates a complete HTTP request for the OpenAI API
func buildOpenAIHTTPRequest(ctx context.Context, inputs []any, chatOpts uctypes.WaveChatOpts, cont *uctypes.WaveContinueResponse) (*http.Request, error) {
opts := chatOpts.Config
// If continuing from premium rate limit, downgrade to default model and medium thinking
// (medium is more widely supported than low across different models)
if cont != nil && cont.ContinueFromKind == uctypes.StopKindPremiumRateLimit {
opts.Model = uctypes.DefaultOpenAIModel
opts.ThinkingLevel = uctypes.ThinkingLevelMedium
}
if opts.Model == "" {
return nil, errors.New("ai:model is required")
}
if chatOpts.ClientId == "" {
return nil, errors.New("chatOpts.ClientId is required")
}
// Set defaults
endpoint := opts.Endpoint
if endpoint == "" {
return nil, errors.New("ai:endpoint is required")
}
maxTokens := opts.MaxTokens
if maxTokens <= 0 {
maxTokens = OpenAIDefaultMaxTokens
}
// injected data
if chatOpts.TabState != "" {
appendToLastUserMessage(inputs, chatOpts.TabState)
}
if chatOpts.PlatformInfo != "" {
appendToLastUserMessage(inputs, "<PlatformInfo>\n"+chatOpts.PlatformInfo+"\n</PlatformInfo>")
}
if chatOpts.AppStaticFiles != "" {
appendToLastUserMessage(inputs, "<CurrentAppStaticFiles>\n"+chatOpts.AppStaticFiles+"\n</CurrentAppStaticFiles>")
}
if chatOpts.AppGoFile != "" {
appendToLastUserMessage(inputs, "<CurrentAppGoFile>\n"+chatOpts.AppGoFile+"\n</CurrentAppGoFile>")
}
// Build request body
// Use configured verbosity, or fall back to default constant
verbosity := opts.Verbosity
if verbosity == "" {
verbosity = OpenAIDefaultVerbosity
}
reqBody := &OpenAIRequest{
Model: opts.Model,
Input: inputs,
Stream: true,
StreamOptions: &StreamOptionsType{IncludeObfuscation: false},
MaxOutputTokens: maxTokens,
Text: &TextType{Verbosity: verbosity},
}
// Add system prompt as instructions if provided
if len(chatOpts.SystemPrompt) > 0 {
reqBody.Instructions = strings.Join(chatOpts.SystemPrompt, "\n")
}
// Add tools if provided
if len(chatOpts.Tools) > 0 {
tools := make([]OpenAIRequestTool, len(chatOpts.Tools))
for i, tool := range chatOpts.Tools {
tools[i] = ConvertToolDefinitionToOpenAI(tool)
}
reqBody.Tools = tools
}
for _, tool := range chatOpts.TabTools {
convertedTool := ConvertToolDefinitionToOpenAI(tool)
reqBody.Tools = append(reqBody.Tools, convertedTool)
}
// Add native web search tool if enabled
if chatOpts.AllowNativeWebSearch {
webSearchTool := OpenAIRequestTool{
Type: "web_search",
}
reqBody.Tools = append(reqBody.Tools, webSearchTool)
}
// Set reasoning based on thinking level from config
if opts.ThinkingLevel != "" {
reqBody.Reasoning = &ReasoningType{
Effort: opts.ThinkingLevel,
}
if opts.Model == "gpt-5" || opts.Model == "gpt-5.1" {
reqBody.Reasoning.Summary = "auto"
}
}
debugPrintReq(reqBody, endpoint)
// Encode request body
buf, err := aiutil.JsonEncodeRequestBody(reqBody)
if err != nil {
return nil, err
}
// Create HTTP request
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, &buf)
if err != nil {
return nil, err
}
// Set headers
req.Header.Set("Content-Type", "application/json")
// Azure OpenAI uses "api-key" header instead of "Authorization: Bearer"
if opts.Provider == uctypes.AIProvider_Azure || opts.Provider == uctypes.AIProvider_AzureLegacy {
req.Header.Set("api-key", opts.APIToken)
} else {
req.Header.Set("Authorization", "Bearer "+opts.APIToken)
}
req.Header.Set("Accept", "text/event-stream")
// Only send Wave-specific headers when using Wave provider
if opts.Provider == uctypes.AIProvider_Wave {
if chatOpts.ClientId != "" {
req.Header.Set("X-Wave-ClientId", chatOpts.ClientId)
}
if chatOpts.ChatId != "" {
req.Header.Set("X-Wave-ChatId", chatOpts.ChatId)
}
req.Header.Set("X-Wave-Version", wavebase.WaveVersion)
req.Header.Set("X-Wave-APIType", uctypes.APIType_OpenAIResponses)
req.Header.Set("X-Wave-RequestType", chatOpts.GetWaveRequestType())
}
return req, nil
}
// convertFileAIMessagePart converts a file AIMessagePart to OpenAI format
func convertFileAIMessagePart(part uctypes.AIMessagePart) (*OpenAIMessageContent, error) {
if part.Type != uctypes.AIMessagePartTypeFile {
return nil, fmt.Errorf("convertFileAIMessagePart expects 'file' type, got '%s'", part.Type)
}
if part.MimeType == "" {
return nil, fmt.Errorf("file part missing mimetype")
}
// Handle different file types
switch {
case strings.HasPrefix(part.MimeType, "image/"):
imageUrl, err := aiutil.ExtractImageUrl(part.Data, part.URL, part.MimeType)
if err != nil {
return nil, err
}
return &OpenAIMessageContent{
Type: "input_image",
ImageUrl: imageUrl,
Filename: part.FileName,
PreviewUrl: part.PreviewUrl,
}, nil
case part.MimeType == "application/pdf":
// Handle PDFs - OpenAI only supports base64 data for PDFs, not URLs
if len(part.Data) == 0 {
if part.URL != "" {
return nil, fmt.Errorf("dropping PDF with URL (must be fetched and converted to base64 data)")
}
return nil, fmt.Errorf("PDF file part missing data")
}
// Convert raw data to base64
base64Data := base64.StdEncoding.EncodeToString(part.Data)
return &OpenAIMessageContent{
Type: "input_file",
Filename: part.FileName, // Optional filename
FileData: base64Data,
PreviewUrl: part.PreviewUrl,
}, nil
case part.MimeType == "text/plain":
textData, err := aiutil.ExtractTextData(part.Data, part.URL)
if err != nil {
return nil, err
}
formattedText := aiutil.FormatAttachedTextFile(part.FileName, textData)
return &OpenAIMessageContent{
Type: "input_text",
Text: formattedText,
}, nil
case part.MimeType == "directory":
var jsonContent string
if len(part.Data) > 0 {
jsonContent = string(part.Data)
} else {
return nil, fmt.Errorf("directory listing part missing data")
}
formattedText := aiutil.FormatAttachedDirectoryListing(part.FileName, jsonContent)
return &OpenAIMessageContent{
Type: "input_text",
Text: formattedText,
}, nil
default:
return nil, fmt.Errorf("dropping file with unsupported mimetype '%s' (OpenAI supports images, PDFs, text/plain, and directories)", part.MimeType)
}
}
// ConvertAIMessageToOpenAIChatMessage converts an AIMessage to OpenAIChatMessage
// These messages are ALWAYS role "user"
// Handles text parts, images, PDFs, and text/plain files
func ConvertAIMessageToOpenAIChatMessage(aiMsg uctypes.AIMessage) (*OpenAIChatMessage, error) {
if err := aiMsg.Validate(); err != nil {
return nil, fmt.Errorf("invalid AIMessage: %w", err)
}
var contentBlocks []OpenAIMessageContent
for i, part := range aiMsg.Parts {
switch part.Type {
case uctypes.AIMessagePartTypeText:
if part.Text == "" {
return nil, fmt.Errorf("part %d: text type requires non-empty text field", i)
}
contentBlocks = append(contentBlocks, OpenAIMessageContent{
Type: "input_text",
Text: part.Text,
})
case uctypes.AIMessagePartTypeFile:
block, err := convertFileAIMessagePart(part)
if err != nil {
log.Printf("openai: %v", err)
continue
}
contentBlocks = append(contentBlocks, *block)
default:
// Drop unknown part types
log.Printf("openai: dropping unknown part type '%s'", part.Type)
continue
}
}
return &OpenAIChatMessage{
MessageId: aiMsg.MessageId,
Message: &OpenAIMessage{
Role: "user",
Content: contentBlocks,
},
}, nil
}
// ConvertToolResultsToOpenAIChatMessage converts AIToolResult slice to OpenAIChatMessage slice
func ConvertToolResultsToOpenAIChatMessage(toolResults []uctypes.AIToolResult) ([]*OpenAIChatMessage, error) {
if len(toolResults) == 0 {
return nil, errors.New("toolResults cannot be empty")
}
var messages []*OpenAIChatMessage
for _, result := range toolResults {
if result.ToolUseID == "" {
return nil, fmt.Errorf("tool result missing ToolUseID")
}
// Create the function call output with result data
var outputData any
if result.ErrorText != "" {
// Marshal error output to string
errorOutput := OpenAIFunctionCallErrorOutput{
Ok: "false",
Error: result.ErrorText,
}
errorBytes, err := json.Marshal(errorOutput)
if err != nil {
return nil, fmt.Errorf("failed to marshal error output: %w", err)
}
outputData = string(errorBytes)
} else {
// Check if text looks like an image data URL
if strings.HasPrefix(result.Text, "data:image/") {
// Convert to output array with input_image type
outputData = []OpenAIMessageContent{
{
Type: "input_image",
ImageUrl: result.Text,
},
}
} else {
// Use text result for success
outputData = result.Text
}
}
functionCallOutput := &OpenAIFunctionCallOutputInput{
Type: "function_call_output",
CallId: result.ToolUseID,
Output: outputData,
}
messages = append(messages, &OpenAIChatMessage{
MessageId: uuid.New().String(),
FunctionCallOutput: functionCallOutput,
})
}
return messages, nil
}
// convertToUIMessage converts an OpenAIChatMessage to a UIMessage
func (m *OpenAIChatMessage) convertToUIMessage() *uctypes.UIMessage {
var parts []uctypes.UIMessagePart
var role string
// Handle different message types
if m.Message != nil {
role = m.Message.Role
for _, block := range m.Message.Content {
blockParts := convertContentBlockToParts(block, role)
parts = append(parts, blockParts...)
}
} else if m.FunctionCall != nil {
// Handle function call input
role = "assistant"
if m.FunctionCall.ToolUseData != nil {
parts = append(parts, uctypes.UIMessagePart{
Type: "data-tooluse",
ID: m.FunctionCall.CallId,
Data: *m.FunctionCall.ToolUseData,
})
}
} else if m.FunctionCallOutput != nil {
// FunctionCallOutput messages are not converted to UIMessage
return nil
}
if len(parts) == 0 {
return nil
}
return &uctypes.UIMessage{
ID: m.MessageId,
Role: role,
Parts: parts,
}
}
// ConvertAIChatToUIChat converts an AIChat to a UIChat for OpenAI
func ConvertAIChatToUIChat(aiChat uctypes.AIChat) (*uctypes.UIChat, error) {
if aiChat.APIType != uctypes.APIType_OpenAIResponses {
return nil, fmt.Errorf("APIType must be '%s', got '%s'", uctypes.APIType_OpenAIResponses, aiChat.APIType)
}
uiMessages := make([]uctypes.UIMessage, 0, len(aiChat.NativeMessages))
for i, nativeMsg := range aiChat.NativeMessages {
openaiMsg, ok := nativeMsg.(*OpenAIChatMessage)
if !ok {
return nil, fmt.Errorf("message %d: expected *OpenAIChatMessage, got %T", i, nativeMsg)
}
uiMsg := openaiMsg.convertToUIMessage()
if uiMsg != nil {
uiMessages = append(uiMessages, *uiMsg)
}
}
return &uctypes.UIChat{
ChatId: aiChat.ChatId,
APIType: aiChat.APIType,
Model: aiChat.Model,
APIVersion: aiChat.APIVersion,
Messages: uiMessages,
}, nil
}
// GetFunctionCallInputByToolCallId returns the OpenAIFunctionCallInput associated with the given ToolCallId,
// or nil if not found in the AIChat
func GetFunctionCallInputByToolCallId(aiChat uctypes.AIChat, toolCallId string) *OpenAIFunctionCallInput {
for _, nativeMsg := range aiChat.NativeMessages {
openaiMsg, ok := nativeMsg.(*OpenAIChatMessage)
if !ok {
continue
}
if openaiMsg.FunctionCall != nil && openaiMsg.FunctionCall.CallId == toolCallId {
return openaiMsg.FunctionCall
}
}
return nil
}