File size: 9,538 Bytes
bd749dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Package translator provides types and functions for converting chat requests and responses between different schemas.
// This file contains shared state utilities for streaming response translators.
package translator

import (
	"fmt"
	"strings"
	"sync/atomic"
	"time"
)

// StreamState provides common state fields for streaming response translators.
// Embed this struct into translator-specific state types.
type StreamState struct {
	// ResponseID is the unique identifier for the response.
	ResponseID string
	// CreatedAt is the Unix timestamp when the response was created.
	CreatedAt int64
	// Model is the name of the model being used.
	Model string
	// SequenceNum is used for ordered events (e.g., OpenAI Responses API).
	SequenceNum int
	// FinishReason is the mapped finish reason in target format.
	FinishReason string
	// NativeFinishReason is the original finish reason from the source.
	NativeFinishReason string
	// InputTokens tracks the number of input tokens used.
	InputTokens int64
	// OutputTokens tracks the number of output tokens used.
	OutputTokens int64
	// UsageSeen indicates whether usage data has been processed.
	UsageSeen bool
}

// Reset clears the state for reuse.
func (s *StreamState) Reset() {
	s.ResponseID = ""
	s.CreatedAt = 0
	s.Model = ""
	s.SequenceNum = 0
	s.FinishReason = ""
	s.NativeFinishReason = ""
	s.InputTokens = 0
	s.OutputTokens = 0
	s.UsageSeen = false
}

// NextSequence returns the next sequence number for event ordering.
func (s *StreamState) NextSequence() int {
	s.SequenceNum++
	return s.SequenceNum
}

// EnsureTimestamp sets CreatedAt to current time if not already set.
func (s *StreamState) EnsureTimestamp() int64 {
	if s.CreatedAt == 0 {
		s.CreatedAt = time.Now().Unix()
	}
	return s.CreatedAt
}

// ToolCallState tracks the state of a single tool/function call being accumulated.
type ToolCallState struct {
	// ID is the unique identifier for this tool call.
	ID string
	// Name is the name of the tool/function.
	Name string
	// Index is the position of this tool call in the sequence.
	Index int
	// Arguments accumulates the JSON arguments for this tool call.
	Arguments strings.Builder
	// Complete indicates whether this tool call has been fully received.
	Complete bool
}

// GetArguments returns the accumulated arguments, defaulting to "{}" if empty.
func (t *ToolCallState) GetArguments() string {
	args := t.Arguments.String()
	if args == "" {
		return "{}"
	}
	return args
}

// WriteArguments appends partial argument JSON.
func (t *ToolCallState) WriteArguments(partial string) {
	t.Arguments.WriteString(partial)
}

// ToolCallAccumulator manages multiple concurrent tool calls indexed by position.
type ToolCallAccumulator struct {
	// calls maps index -> tool call state.
	calls map[int]*ToolCallState
	// ordered tracks the order of indices for deterministic iteration.
	ordered []int
	// nextIndex tracks the next available index for new calls.
	nextIndex int
}

// NewToolCallAccumulator creates a new accumulator.
func NewToolCallAccumulator() *ToolCallAccumulator {
	return &ToolCallAccumulator{
		calls:     make(map[int]*ToolCallState),
		ordered:   make([]int, 0),
		nextIndex: 0,
	}
}

// Get retrieves a tool call by index, returning nil if not found.
func (t *ToolCallAccumulator) Get(index int) *ToolCallState {
	return t.calls[index]
}

// GetOrCreate retrieves or creates a tool call at the given index.
func (t *ToolCallAccumulator) GetOrCreate(index int) *ToolCallState {
	if tc, exists := t.calls[index]; exists {
		return tc
	}
	tc := &ToolCallState{Index: index}
	t.calls[index] = tc
	t.ordered = append(t.ordered, index)
	return tc
}

// CreateNext creates a new tool call at the next available index.
func (t *ToolCallAccumulator) CreateNext(id, name string) *ToolCallState {
	tc := &ToolCallState{
		ID:    id,
		Name:  name,
		Index: t.nextIndex,
	}
	t.calls[t.nextIndex] = tc
	t.ordered = append(t.ordered, t.nextIndex)
	t.nextIndex++
	return tc
}

// MarkComplete marks a tool call as complete and returns it.
func (t *ToolCallAccumulator) MarkComplete(index int) *ToolCallState {
	if tc, exists := t.calls[index]; exists {
		tc.Complete = true
		return tc
	}
	return nil
}

// Remove deletes a tool call from the accumulator (cleanup after processing).
func (t *ToolCallAccumulator) Remove(index int) {
	delete(t.calls, index)
	// Rebuild ordered slice
	newOrdered := make([]int, 0, len(t.calls))
	for _, idx := range t.ordered {
		if idx != index {
			newOrdered = append(newOrdered, idx)
		}
	}
	t.ordered = newOrdered
}

// HasAny returns true if any tool calls are being tracked.
func (t *ToolCallAccumulator) HasAny() bool {
	return len(t.calls) > 0
}

// Count returns the number of tracked tool calls.
func (t *ToolCallAccumulator) Count() int {
	return len(t.calls)
}

// GetOrdered returns tool calls in the order they were created.
func (t *ToolCallAccumulator) GetOrdered() []*ToolCallState {
	result := make([]*ToolCallState, 0, len(t.ordered))
	for _, idx := range t.ordered {
		if tc, exists := t.calls[idx]; exists {
			result = append(result, tc)
		}
	}
	return result
}

// Reset clears all accumulated state.
func (t *ToolCallAccumulator) Reset() {
	t.calls = make(map[int]*ToolCallState)
	t.ordered = make([]int, 0)
	t.nextIndex = 0
}

// ContentBuffer accumulates text and reasoning content.
type ContentBuffer struct {
	// Text accumulates the main text content.
	Text strings.Builder
	// Reasoning accumulates reasoning/thinking content.
	Reasoning strings.Builder
}

// Reset clears the buffers.
func (c *ContentBuffer) Reset() {
	c.Text.Reset()
	c.Reasoning.Reset()
}

// HasContent returns true if any content has been accumulated.
func (c *ContentBuffer) HasContent() bool {
	return c.Text.Len() > 0 || c.Reasoning.Len() > 0
}

// StreamingState combines all common state for streaming translators.
type StreamingState struct {
	StreamState
	// ToolCalls tracks accumulated tool calls.
	ToolCalls *ToolCallAccumulator
	// Content accumulates text and reasoning content.
	Content ContentBuffer
	// InTextBlock indicates whether currently processing a text block.
	InTextBlock bool
	// InToolBlock indicates whether currently processing a tool block.
	InToolBlock bool
	// InReasoningBlock indicates whether currently processing a reasoning block.
	InReasoningBlock bool
	// CurrentMessageID is the ID of the current message being processed.
	CurrentMessageID string
	// CurrentToolCallID is the ID of the current tool call being processed.
	CurrentToolCallID string
	// CurrentReasoningID is the ID of the current reasoning block being processed.
	CurrentReasoningID string
}

// NewStreamingState creates a fully initialized streaming state.
func NewStreamingState() *StreamingState {
	return &StreamingState{
		ToolCalls: NewToolCallAccumulator(),
	}
}

// Reset clears all state for reuse.
func (s *StreamingState) Reset() {
	s.StreamState.Reset()
	s.ToolCalls.Reset()
	s.Content.Reset()
	s.InTextBlock = false
	s.InToolBlock = false
	s.InReasoningBlock = false
	s.CurrentMessageID = ""
	s.CurrentToolCallID = ""
	s.CurrentReasoningID = ""
}

// FinishReasonMapper provides bidirectional finish reason mapping.
type FinishReasonMapper struct {
	toOpenAI   map[string]string
	fromOpenAI map[string]string
}

// NewFinishReasonMapper creates a mapper with standard OpenAI mappings.
func NewFinishReasonMapper() *FinishReasonMapper {
	return &FinishReasonMapper{
		toOpenAI: map[string]string{
			// Anthropic/Claude
			"end_turn":      "stop",
			"tool_use":      "tool_calls",
			"max_tokens":    "length",
			"stop_sequence": "stop",
			// Gemini
			"STOP":       "stop",
			"MAX_TOKENS": "length",
			"SAFETY":     "content_filter",
			"RECITATION": "content_filter",
			"OTHER":      "stop",
			// Codex
			"completed": "stop",
		},
		fromOpenAI: map[string]string{
			"stop":           "end_turn",
			"tool_calls":     "tool_use",
			"length":         "max_tokens",
			"content_filter": "SAFETY",
		},
	}
}

// ToOpenAI converts a provider-specific finish reason to OpenAI format.
func (m *FinishReasonMapper) ToOpenAI(reason string) string {
	if mapped, ok := m.toOpenAI[reason]; ok {
		return mapped
	}
	return "stop" // default
}

// FromOpenAI converts an OpenAI finish reason to provider-specific format.
func (m *FinishReasonMapper) FromOpenAI(reason string) string {
	if mapped, ok := m.fromOpenAI[reason]; ok {
		return mapped
	}
	return "STOP" // default
}

// RegisterMapping adds or overrides a mapping.
func (m *FinishReasonMapper) RegisterMapping(providerReason, openAIReason string) {
	m.toOpenAI[providerReason] = openAIReason
	m.fromOpenAI[openAIReason] = providerReason
}

// DefaultFinishReasonMapper is the package-level default mapper.
var DefaultFinishReasonMapper = NewFinishReasonMapper()

// Process-wide unique ID generators.
var (
	responseIDCounter     uint64
	functionCallIDCounter uint64
)

// GenerateResponseID creates a unique response ID with optional prefix.
func GenerateResponseID(prefix string) string {
	counter := atomic.AddUint64(&responseIDCounter, 1)
	if prefix != "" {
		return fmt.Sprintf("%s_%x_%d", prefix, time.Now().UnixNano(), counter)
	}
	return fmt.Sprintf("resp_%x_%d", time.Now().UnixNano(), counter)
}

// GenerateFunctionCallID creates a unique function call ID.
func GenerateFunctionCallID(name string) string {
	counter := atomic.AddUint64(&functionCallIDCounter, 1)
	if name != "" {
		return fmt.Sprintf("call_%s_%d_%d", name, time.Now().UnixNano(), counter)
	}
	return fmt.Sprintf("call_%d_%d", time.Now().UnixNano(), counter)
}