Skip to content

Commit d57e61f

Browse files
committed
fix: transcripts fetching
1 parent 7396453 commit d57e61f

3 files changed

Lines changed: 35 additions & 11 deletions

File tree

pkg/transcript/fetch-all.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func FetchAll(agentID, startTime, endTime, tag, rang, outputDirectory string) er
2323
return fmt.Errorf("failed to fetch transcript %s: %w", transcriptInformation.ID, err)
2424
}
2525

26-
err = SaveTranscript(transcript, outputDirectory)
26+
err = SaveTranscript(transcript, transcriptInformation.ID, outputDirectory)
2727
if err != nil {
2828
return fmt.Errorf("failed to save transcript %s: %w", transcriptInformation.ID, err)
2929
}
@@ -32,15 +32,15 @@ func FetchAll(agentID, startTime, endTime, tag, rang, outputDirectory string) er
3232
return nil
3333
}
3434

35-
func SaveTranscript(transcript [][]string, outputDirectory string) error {
35+
func SaveTranscript(transcript [][]string, transcriptID, outputDirectory string) error {
3636
// Create output directory if it doesn't exist
3737
if err := os.MkdirAll(outputDirectory, 0755); err != nil {
3838
return fmt.Errorf("failed to create output directory: %w", err)
3939
}
4040

41-
// Generate filename with timestamp
41+
// Generate filename with transcript ID and timestamp
4242
timestamp := time.Now().Format("20060102-150405")
43-
filename := fmt.Sprintf("transcript_%s.csv", timestamp)
43+
filename := fmt.Sprintf("transcript_%s_%s.csv", transcriptID, timestamp)
4444
fullPath := filepath.Join(outputDirectory, filename)
4545
buf := new(bytes.Buffer)
4646
wr := csv.NewWriter(buf)

pkg/transcript/fetch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func Fetch(agentID, transcriptID, outputDirectory string) error {
1313
return fmt.Errorf("failed to fetch transcript %s: %w", transcriptID, err)
1414
}
1515

16-
err = SaveTranscript(transcript, outputDirectory)
16+
err = SaveTranscript(transcript, transcriptID, outputDirectory)
1717
if err != nil {
1818
return fmt.Errorf("failed to save transcript %s: %w", transcriptID, err)
1919
}

pkg/voiceflow/transcript.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ import (
1414
)
1515

1616
func FetchTranscriptInformations(agentID, startTime, endTime, tag, rang string) ([]transcript.TranscriptInformation, error) {
17-
const pageSize = 100
17+
const pageSize = 99 // API max take is exclusive < 100
1818
var allTranscripts []transcript.TranscriptInformation
19+
seen := make(map[string]bool)
1920

2021
reqBody := transcript.SearchTranscriptsRequest{}
2122
if startTime != "" {
@@ -25,8 +26,11 @@ func FetchTranscriptInformations(agentID, startTime, endTime, tag, rang string)
2526
reqBody.EndDate = endTime
2627
}
2728

28-
for skip := 0; ; skip += pageSize {
29-
url := fmt.Sprintf("%s/v1/transcript/project/%s?take=%d&skip=%d&order=DESC", global.GetAnalyticsBaseURL(), agentID, pageSize, skip)
29+
// Use cursor-based pagination with endDate to avoid skip limits.
30+
// Results are ordered DESC (newest first), so we move the endDate
31+
// cursor backwards after each full page.
32+
for {
33+
url := fmt.Sprintf("%s/v1/transcript/project/%s?take=%d&skip=0&order=DESC", global.GetAnalyticsBaseURL(), agentID, pageSize)
3034

3135
bodyBytes, err := json.Marshal(reqBody)
3236
if err != nil {
@@ -63,12 +67,25 @@ func FetchTranscriptInformations(agentID, startTime, endTime, tag, rang string)
6367
return nil, fmt.Errorf("error unmarshalling response: %v", err)
6468
}
6569

66-
allTranscripts = append(allTranscripts, searchResponse.Transcripts...)
70+
if len(searchResponse.Transcripts) == 0 {
71+
break
72+
}
73+
74+
for _, t := range searchResponse.Transcripts {
75+
if !seen[t.ID] {
76+
seen[t.ID] = true
77+
allTranscripts = append(allTranscripts, t)
78+
}
79+
}
6780

6881
// If we got fewer results than the page size, we've reached the end
6982
if len(searchResponse.Transcripts) < pageSize {
7083
break
7184
}
85+
86+
// Use the oldest transcript's createdAt as the new endDate cursor
87+
oldest := searchResponse.Transcripts[len(searchResponse.Transcripts)-1]
88+
reqBody.EndDate = oldest.CreatedAt
7289
}
7390

7491
return allTranscripts, nil
@@ -87,8 +104,15 @@ func FetchTranscriptCSV(agentID, transcriptID string) ([][]string, error) {
87104
message := ""
88105
switch turn.Payload.Type {
89106
case "text":
90-
if tp, err := turn.Payload.GetTextPayload(); err == nil {
91-
message = tp.Message
107+
if turn.Type == "user-text" {
108+
// User text payload is a plain string
109+
if s, ok := turn.Payload.Payload.(string); ok {
110+
message = s
111+
}
112+
} else {
113+
if tp, err := turn.Payload.GetTextPayload(); err == nil {
114+
message = tp.Message
115+
}
92116
}
93117
case "intent":
94118
if ip, err := turn.Payload.GetIntentPayload(); err == nil {

0 commit comments

Comments
 (0)