1
-
2
1
import "js"
3
2
4
3
global asyncTasks: js.TaskQueue
@@ -7,93 +6,111 @@ global asyncTasks: js.TaskQueue
7
6
8
7
// Session 对象,不能使用该类型直接声明值,需通过 RequestSession 创建
9
8
#wa:need-constructor
10
- type Session struct {
11
- _extobj: js.ExtObj
9
+ type Session : struct {
10
+ _extobj: js.ExtObj
12
11
}
13
12
14
13
// 获取 Session,异步
15
- #wa:generic RequestSession_Handler
14
+ #wa:generic RequestSession_Handler RequestSessionSimple RequestSession_Handler_Simple
16
15
func RequestSession(requestor: SessionRequestor, option: string) {
17
- if requestor == nil {
18
- panic("SessionRequestor can't be nil.")
19
- }
20
- tid := asyncTasks.Alloc(nil, requestor)
21
- jsAiRequestSession(tid, option)
16
+ if requestor == nil {
17
+ panic("SessionRequestor can't be nil.")
18
+ }
19
+ tid := asyncTasks.Alloc(nil, requestor)
20
+ jsAiRequestSession(tid, option)
21
+ }
22
+ func RequestSessionSimple(requestor: SessionRequestor) {
23
+ if requestor == nil {
24
+ panic("SessionRequestor can't be nil.")
25
+ }
26
+ tid := asyncTasks.Alloc(nil, requestor)
27
+ jsAiRequestSession(tid, "")
22
28
}
23
- type SessionRequestor interface {
24
- OnRequested(session: Session)
29
+
30
+ type SessionRequestor :interface {
31
+ OnRequested(session: Session)
25
32
}
26
33
27
34
func RequestSession_Handler(handler: SessionRequestHandler, option: string) {
28
- if handler == nil {
29
- panic("SessionRequestHandler can't be nil.")
30
- }
31
- tid := asyncTasks.Alloc(nil, handler)
32
- jsAiRequestSession(tid, option)
35
+ if handler == nil {
36
+ panic("SessionRequestHandler can't be nil.")
37
+ }
38
+ tid := asyncTasks.Alloc(nil, handler)
39
+ jsAiRequestSession(tid, option)
40
+ }
41
+
42
+ func RequestSession_Handler_Simple(handler: SessionRequestHandler) {
43
+ if handler == nil {
44
+ panic("SessionRequestHandler can't be nil.")
45
+ }
46
+ tid := asyncTasks.Alloc(nil, handler)
47
+ jsAiRequestSession(tid, "")
33
48
}
34
- type SessionRequestHandler func(session: Session)
49
+
50
+ type SessionRequestHandler :func(session: Session)
35
51
36
52
#wa:import aiproxy request_session
37
53
func jsAiRequestSession(tid: int, option: string)
38
54
39
55
#wa:export ai.onSessionRequested
40
56
func onSessionRequested(tid: int, sh: js.Handle) {
41
- _, handler := asyncTasks.Get(tid)
42
- session: Session
43
- session._extobj = js.WrapExtObj(sh)
57
+ _, handler := asyncTasks.Get(tid)
58
+ session: Session
59
+ session._extobj = js.WrapExtObj(sh)
44
60
45
- switch h := handler.(type) {
46
- case SessionRequestor:
47
- h.OnRequested(session)
61
+ switch h := handler.(type) {
62
+ case SessionRequestor:
63
+ h.OnRequested(session)
48
64
49
- case SessionRequestHandler:
50
- h(session)
51
- }
65
+ case SessionRequestHandler:
66
+ h(session)
67
+ }
52
68
53
- asyncTasks.Free(tid)
69
+ asyncTasks.Free(tid)
54
70
}
55
71
56
72
//---------------------------------------------------------------
57
73
58
74
// 异步 prompt
59
75
#wa:generic PromptAsync_Handler
60
76
func Session.PromptAsync(key: string, prompter: Prompter) {
61
- if prompter == nil {
62
- panic("Prompter can't be nil.")
63
- }
64
- tid := asyncTasks.Alloc(this, prompter)
65
- jsPromptAsync(tid, this._extobj.GetHandle(), key)
77
+ if prompter == nil {
78
+ panic("Prompter can't be nil.")
79
+ }
80
+ tid := asyncTasks.Alloc(this, prompter)
81
+ jsPromptAsync(tid, this._extobj.GetHandle(), key)
66
82
}
67
- type Prompter interface {
68
- OnPrompted(result: string)
83
+
84
+ type Prompter :interface {
85
+ OnPrompted(result: string)
69
86
}
70
87
71
88
func Session.PromptAsync_Handler(key: string, handler: PromptHandler) {
72
- if handler == nil {
73
- panic("BufferMapHandler can't be nil.")
74
- }
75
- tid := asyncTasks.Alloc(this, handler)
76
- jsPromptAsync(tid, this._extobj.GetHandle(), key)
89
+ if handler == nil {
90
+ panic("BufferMapHandler can't be nil.")
91
+ }
92
+ tid := asyncTasks.Alloc(this, handler)
93
+ jsPromptAsync(tid, this._extobj.GetHandle(), key)
77
94
}
78
- type PromptHandler func(result: string)
95
+
96
+ type PromptHandler :func(result: string)
79
97
80
98
#wa:import aiproxy prompt
81
99
func jsPromptAsync(tid: int, session: js.Handle, key: string)
82
100
83
101
#wa:export ai.onPrompted
84
102
func onPrompted(tid: int, result: string) {
85
- _, handler := asyncTasks.Get(tid)
103
+ _, handler := asyncTasks.Get(tid)
86
104
87
- switch h := handler.(type) {
88
- case Prompter:
89
- h.OnPrompted(result)
105
+ switch h := handler.(type) {
106
+ case Prompter:
107
+ h.OnPrompted(result)
90
108
91
- case PromptHandler:
92
- h(result)
93
- }
109
+ case PromptHandler:
110
+ h(result)
111
+ }
94
112
95
- asyncTasks.Free(tid)
113
+ asyncTasks.Free(tid)
96
114
}
97
115
98
116
//---------------------------------------------------------------
99
-
0 commit comments