File size: 1,666 Bytes
87337b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 *
 * Agora Real Time Engagement
 * Created by Wei Hu in 2022-10.
 * Copyright (c) 2024 Agora IO. All rights reserved.
 *
 */
// Note that this is just an example extension written in the GO programming
// language, so the package name does not equal to the containing directory
// name. However, it is not common in Go.
package extension

import (
	"fmt"

	"ten_framework/ten"
)

const (
	textDataTextField  = "text"
	textDataFinalField = "is_final"

	cmdNameFlush = "flush"
)

type interruptDetectorExtension struct {
	ten.DefaultExtension
}

func newExtension(name string) ten.Extension {
	return &interruptDetectorExtension{}
}

// OnData receives data from ten graph.
// current supported data:
//   - name: text_data
//     example:
//     {name: text_data, properties: {text: "hello", is_final: false}
func (p *interruptDetectorExtension) OnData(
	tenEnv ten.TenEnv,
	data ten.Data,
) {
	text, err := data.GetPropertyString(textDataTextField)
	if err != nil {
		tenEnv.LogWarn(fmt.Sprintf("OnData GetProperty %s error: %v", textDataTextField, err))
		return
	}

	final, err := data.GetPropertyBool(textDataFinalField)
	if err != nil {
		tenEnv.LogWarn(fmt.Sprintf("OnData GetProperty %s error: %v", textDataFinalField, err))
		return
	}

	tenEnv.LogDebug(fmt.Sprintf("OnData %s: %s %s: %t", textDataTextField, text, textDataFinalField, final))

	if final || len(text) >= 2 {
		flushCmd, _ := ten.NewCmd(cmdNameFlush)
		tenEnv.SendCmd(flushCmd, nil)

		tenEnv.LogInfo(fmt.Sprintf("sent cmd: %s", cmdNameFlush))
	}
}

func init() {
	// Register addon
	ten.RegisterAddonAsExtension(
		"interrupt_detector",
		ten.NewDefaultExtensionAddon(newExtension),
	)
}