-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.go
147 lines (127 loc) · 3.48 KB
/
extension.go
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
/**
*
* Agora Real Time Engagement
* Created by Wei Hu in 2022-10.
* Copyright (c) 2024 Agora IO. All rights reserved.
*
*/
package extension
import (
"chat_transcriber/pb"
"fmt"
"log/slog"
"time"
"ten_framework/ten"
"google.golang.org/protobuf/proto"
)
const (
textDataTextField = "text"
textDataFinalField = "is_final"
textDataStreamIdField = "stream_id"
textDataEndOfSegmentField = "end_of_segment"
)
var (
logTag = slog.String("extension", "CHAT_TRANSCRIBER_EXTENSION")
)
type chatTranscriberExtension struct {
ten.DefaultExtension
cachedTextMap map[uint32]string // record the cached text data for each stream id
}
func newExtension(name string) ten.Extension {
return &chatTranscriberExtension{
cachedTextMap: make(map[uint32]string),
}
}
// OnData receives data from ten graph.
// current suppotend data:
// - name: text_data
// example:
// {"name": "text_data", "properties": {"text": "hello", "is_final": true, "stream_id": 123, "end_of_segment": true}}
func (p *chatTranscriberExtension) OnData(
tenEnv ten.TenEnv,
data ten.Data,
) {
// Get the text data from data.
text, err := data.GetPropertyString(textDataTextField)
if err != nil {
slog.Warn(fmt.Sprintf("OnData GetProperty %s error: %v", textDataTextField, err), logTag)
return
}
// Get the 'is_final' flag from data which indicates whether the text is final,
// otherwise it could be overwritten by the next text.
final, err := data.GetPropertyBool(textDataFinalField)
if err != nil {
slog.Warn(fmt.Sprintf("OnData GetProperty %s error: %v", textDataFinalField, err), logTag)
return
}
// Get the stream id from data.
var streamId uint32
if id, err := data.GetPropertyUint32(textDataStreamIdField); err == nil {
streamId = id
}
// Get the 'end_of_segment' flag from data which indicates whether a line break is needed.
endOfSegment, err := data.GetPropertyBool(textDataEndOfSegmentField)
if err != nil {
slog.Warn(fmt.Sprintf("OnData GetProperty %s error: %v", textDataEndOfSegmentField, err), logTag)
return
}
slog.Debug(fmt.Sprintf(
"OnData %s: %s %s: %t %s: %d %s: %t",
textDataTextField,
text,
textDataFinalField,
final,
textDataStreamIdField,
streamId,
textDataEndOfSegmentField,
endOfSegment), logTag)
// We cache all final text data and append the non-final text data to the cached data
// until the end of the segment.
if endOfSegment {
if cachedText, ok := p.cachedTextMap[streamId]; ok {
text = cachedText + text
delete(p.cachedTextMap, streamId)
}
} else {
if final {
if cachedText, ok := p.cachedTextMap[streamId]; ok {
text = cachedText + text
p.cachedTextMap[streamId] = text
} else {
p.cachedTextMap[streamId] = text
}
}
}
pb := pb.Text{
Uid: int32(streamId),
DataType: "transcribe",
Texttime: time.Now().UnixMilli(),
Words: []*pb.Word{
{
Text: text,
IsFinal: endOfSegment,
},
},
}
pbData, err := proto.Marshal(&pb)
if err != nil {
slog.Warn(fmt.Sprintf("OnData Marshal error: %v", err), logTag)
return
}
// convert the origin text data to the protobuf data and send it to the graph.
tenData, err := ten.NewData("data")
tenData.SetPropertyBytes("data", pbData)
if err != nil {
slog.Warn(fmt.Sprintf("OnData NewData error: %v", err), logTag)
return
}
tenEnv.SendData(tenData)
}
func init() {
slog.Info("chat_transcriber extension init", logTag)
// Register addon
ten.RegisterAddonAsExtension(
"chat_transcriber",
ten.NewDefaultExtensionAddon(newExtension),
)
}