-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattempt_trace.go
More file actions
81 lines (69 loc) · 2.08 KB
/
Copy pathattempt_trace.go
File metadata and controls
81 lines (69 loc) · 2.08 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
package wormhole
import (
"context"
"time"
)
// AttemptPhase describes where a provider/model attempt is in its lifecycle.
type AttemptPhase string
const (
AttemptStarted AttemptPhase = "started"
AttemptSuccess AttemptPhase = "success"
AttemptError AttemptPhase = "error"
)
// AttemptEvent describes one provider/model attempt made by a request builder.
type AttemptEvent struct {
Operation string
Phase AttemptPhase
Provider string
Model string
Attempt int
Fallback bool
Stream bool
Error error
Time time.Time
}
// AttemptTraceFunc receives best-effort attempt events.
type AttemptTraceFunc func(context.Context, AttemptEvent)
func (p *Wormhole) emitAttempt(ctx context.Context, event AttemptEvent) {
if p == nil || p.config.AttemptTrace == nil {
return
}
if event.Time.IsZero() {
event.Time = time.Now()
}
p.config.AttemptTrace(ctx, event)
}
// --- Stream Lifecycle Trace ---
// StreamEventType identifies the kind of stream lifecycle event.
type StreamEventType string
const (
// StreamStarted is emitted when a streaming request is initiated.
StreamStarted StreamEventType = "started"
// StreamChunk is emitted for each chunk received (optional, controlled by config).
StreamChunk StreamEventType = "chunk"
// StreamEnded is the terminal event emitted exactly once when a stream completes normally.
StreamEnded StreamEventType = "ended"
// StreamError is the terminal event emitted exactly once when a stream fails.
StreamError StreamEventType = "error"
)
// StreamEvent describes one stream lifecycle event.
type StreamEvent struct {
Type StreamEventType
Provider string
Model string
Attempt int
Error error
Time time.Time
}
// StreamTraceFunc receives stream lifecycle events.
// Terminal events (StreamEnded, StreamError) are emitted exactly once.
type StreamTraceFunc func(context.Context, StreamEvent)
func (p *Wormhole) emitStreamEvent(ctx context.Context, event StreamEvent) {
if p == nil || p.config.StreamTrace == nil {
return
}
if event.Time.IsZero() {
event.Time = time.Now()
}
p.config.StreamTrace(ctx, event)
}