Skip to content

Commit 85088e7

Browse files
committed
更新 readme
1 parent d5043fc commit 85088e7

File tree

6 files changed

+103
-52
lines changed

6 files changed

+103
-52
lines changed

README-zh.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,23 @@ $ cd waroot && wa run -target=wasi examples/prime
167167

168168
运行的结果和英文语法的示例相同。
169169

170+
## 例子:Chrome本地AI
171+
172+
Chrome builtin Gemini Nano Demo:
173+
174+
```wa
175+
import "ai"
176+
177+
func main {
178+
ai.RequestSession(func(session: ai.Session){
179+
session.PromptAsync("Who are you?", func(res: string) {
180+
println(res)
181+
})
182+
})
183+
}
184+
```
185+
186+
170187
更多例子 [waroot/examples](waroot/examples)
171188

172189
## 贡献者名单

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,22 @@ Print prime numbers up to 30:
168168

169169
Output is the same as the previous example.
170170

171+
## Example: Chrome Native AI
172+
173+
Chrome builtin Gemini Nano Demo:
174+
175+
```wa
176+
import "ai"
177+
178+
func main {
179+
ai.RequestSession(func(session: ai.Session){
180+
session.PromptAsync("Who are you?", func(res: string) {
181+
println(res)
182+
})
183+
})
184+
}
185+
```
186+
171187
More examples [waroot/examples](waroot/examples)
172188

173189
## Contributors

waroot/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- 预定义常量统一用大写字母, 并增加 `__COLUMN__` 常量
88
- `wa build``wa run` 增加 `-optimize` 优化参数, 优化后输出体积大约为原来的 1/3 到 1/4
99
- 优化 `wa run` 默认行为, 只有 wasi 默认命令行执行
10+
- 实验性增加 Chrome 内置 AI 支持
1011
- v0.16.0 (2024-09-16)
1112
- “国产语言论坛”被动关闭: ~~https://zh-lang.osanswer.net~~
1213
- 增加 `__package__`/`__FILE__`/`__LINE__`/`__func__` 预定义常量, 表示当前位置信息

waroot/examples/hello-ai/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ func main {
1212
session.PromptAsync("Who are you?", func(res: string) {
1313
println(res)
1414
})
15-
} , "")
15+
})
1616
}
17-
``
17+
```
1818

1919
然后本地命令行环境执行`wa run`, 然后在打开的页面的开发者控制台可以看到以下输出:
2020

waroot/examples/hello-ai/src/main.wa

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ func main {
77
session.PromptAsync("Who are you?", func(res: string) {
88
println(res)
99
})
10-
} , "")
10+
})
1111
}

waroot/src/ai/ai.wa

Lines changed: 66 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import "js"
32

43
global asyncTasks: js.TaskQueue
@@ -7,93 +6,111 @@ global asyncTasks: js.TaskQueue
76

87
// Session 对象,不能使用该类型直接声明值,需通过 RequestSession 创建
98
#wa:need-constructor
10-
type Session struct {
11-
_extobj: js.ExtObj
9+
type Session :struct {
10+
_extobj: js.ExtObj
1211
}
1312

1413
// 获取 Session,异步
15-
#wa:generic RequestSession_Handler
14+
#wa:generic RequestSession_Handler RequestSessionSimple RequestSession_Handler_Simple
1615
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, "")
2228
}
23-
type SessionRequestor interface {
24-
OnRequested(session: Session)
29+
30+
type SessionRequestor :interface {
31+
OnRequested(session: Session)
2532
}
2633

2734
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, "")
3348
}
34-
type SessionRequestHandler func(session: Session)
49+
50+
type SessionRequestHandler :func(session: Session)
3551

3652
#wa:import aiproxy request_session
3753
func jsAiRequestSession(tid: int, option: string)
3854

3955
#wa:export ai.onSessionRequested
4056
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)
4460

45-
switch h := handler.(type) {
46-
case SessionRequestor:
47-
h.OnRequested(session)
61+
switch h := handler.(type) {
62+
case SessionRequestor:
63+
h.OnRequested(session)
4864

49-
case SessionRequestHandler:
50-
h(session)
51-
}
65+
case SessionRequestHandler:
66+
h(session)
67+
}
5268

53-
asyncTasks.Free(tid)
69+
asyncTasks.Free(tid)
5470
}
5571

5672
//---------------------------------------------------------------
5773

5874
// 异步 prompt
5975
#wa:generic PromptAsync_Handler
6076
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)
6682
}
67-
type Prompter interface {
68-
OnPrompted(result: string)
83+
84+
type Prompter :interface {
85+
OnPrompted(result: string)
6986
}
7087

7188
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)
7794
}
78-
type PromptHandler func(result: string)
95+
96+
type PromptHandler :func(result: string)
7997

8098
#wa:import aiproxy prompt
8199
func jsPromptAsync(tid: int, session: js.Handle, key: string)
82100

83101
#wa:export ai.onPrompted
84102
func onPrompted(tid: int, result: string) {
85-
_, handler := asyncTasks.Get(tid)
103+
_, handler := asyncTasks.Get(tid)
86104

87-
switch h := handler.(type) {
88-
case Prompter:
89-
h.OnPrompted(result)
105+
switch h := handler.(type) {
106+
case Prompter:
107+
h.OnPrompted(result)
90108

91-
case PromptHandler:
92-
h(result)
93-
}
109+
case PromptHandler:
110+
h(result)
111+
}
94112

95-
asyncTasks.Free(tid)
113+
asyncTasks.Free(tid)
96114
}
97115

98116
//---------------------------------------------------------------
99-

0 commit comments

Comments
 (0)