ststshiuh55/wolf
0
1package main
2
3import (
4 "strings"
5 "time"
6)
7
8// TransformModelID removes vendor prefix (e.g. "anthropic:")
9func TransformModelID(modelID string) string {
10 parts := strings.Split(modelID, ":")
11 return parts[len(parts)-1]
12}
13
14func ToOpenAI(atlasResp AtlassianResponse, modelID string) ChatCompletionResponse {
15
16 var usage ChatCompletionUsage
17 if atlasResp.PlatformAttributes.Model != "" {
18
19 usage = ChatCompletionUsage{
20 PromptTokens: nil,
21 CompletionTokens: nil,
22 TotalTokens: nil,
23 }
24 }
25
26 // Convert choices
27 choices := make([]ChatCompletionChoice, len(atlasResp.ResponsePayload.Choices))
28 for i, choice := range atlasResp.ResponsePayload.Choices {
29 // Extract text content from the first content element
30 var content string
31 if len(choice.Message.Content) > 0 {
32 content = choice.Message.Content[0].Text
33 }
34
35 choices[i] = ChatCompletionChoice{
36 Index: choice.Index,
37 Message: &ChatMessage{
38 Role: choice.Message.Role,
39 Content: content,
40 },
41 FinishReason: choice.FinishReason,
42 }
43 }
44
45 return ChatCompletionResponse{
46 ID: atlasResp.ResponsePayload.ID,
47 Object: "chat.completion",
48 Created: atlasResp.ResponsePayload.Created,
49 Model: modelID,
50 Choices: choices,
51 Usage: usage,
52 }
53}
54
55// ToOpenAIStreamChunk converts Atlassian stream chunk to OpenAI format
56func ToOpenAIStreamChunk(atlasChunk AtlassianStreamChunk, requestedModel string) ChatCompletionStreamResponse {
57 var choices []ChatCompletionChoice
58
59 if len(atlasChunk.ResponsePayload.Choices) > 0 {
60 choice := atlasChunk.ResponsePayload.Choices[0]
61
62 delta := &ChatMessage{}
63
64 // Set role if present
65 if choice.Message.Role != "" {
66 delta.Role = choice.Message.Role
67 }
68
69 // Extract text content
70 if len(choice.Message.Content) > 0 && choice.Message.Content[0].Text != "" {
71 delta.Content = choice.Message.Content[0].Text
72 }
73
74 // Only add choice if there's meaningful content or finish reason
75 if delta.Role != "" || delta.Content != "" || choice.FinishReason != nil {
76 choices = append(choices, ChatCompletionChoice{
77 Index: choice.Index,
78 Delta: delta,
79 FinishReason: choice.FinishReason,
80 })
81 }
82 }
83
84 // Generate ID if not present
85 id := atlasChunk.ResponsePayload.ID
86 if id == "" {
87 id = generateChatCompletionID()
88 }
89
90 // Use created time if present, otherwise current time
91 created := atlasChunk.ResponsePayload.Created
92 if created == 0 {
93 created = time.Now().Unix()
94 }
95
96 return ChatCompletionStreamResponse{
97 ID: id,
98 Object: "chat.completion.chunk",
99 Created: created,
100 Model: requestedModel,
101 Choices: choices,
102 }
103}
104
105// generateChatCompletionID generates a chat completion ID similar to OpenAI format
106func generateChatCompletionID() string {
107 return "chatcmpl-" + string(rune(time.Now().UnixMilli()))
108}
109 