@asterai/cli 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -58
- package/dist/commands/deploy.d.ts +4 -6
- package/dist/commands/deploy.js +41 -64
- package/dist/commands/init.d.ts +8 -1
- package/dist/commands/init.js +37 -5
- package/dist/commands/pkg.d.ts +27 -0
- package/dist/commands/pkg.js +98 -0
- package/dist/const.d.ts +2 -0
- package/dist/const.js +2 -0
- package/init/rust/Cargo.toml +25 -0
- package/init/rust/build.sh +5 -0
- package/init/rust/deploy.sh +7 -0
- package/init/rust/plugin.wit +10 -0
- package/init/rust/src/bindings.rs +685 -0
- package/init/rust/src/lib.rs +16 -0
- package/init/typescript/package.json +26 -0
- package/init/typescript/plugin.ts +11 -0
- package/init/typescript/plugin.wit +12 -0
- package/init/typescript/tsconfig.json +11 -0
- package/oclif.manifest.json +79 -115
- package/package.json +4 -2
- package/asterai-init-plugin/package.json +0 -23
- package/asterai-init-plugin/plugin.asterai.proto +0 -22
- package/asterai-init-plugin/plugin.ts +0 -35
- package/asterai-init-plugin/tsconfig.json +0 -4
- package/dist/commands/build.d.ts +0 -23
- package/dist/commands/build.js +0 -139
- package/dist/commands/codegen.d.ts +0 -22
- package/dist/commands/codegen.js +0 -149
- package/dist/compile.d.ts +0 -7
- package/dist/compile.js +0 -25
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
use crate::bindings::asterai::host::api;
|
|
2
|
+
use crate::bindings::exports::your_username::greeter::greeter::Guest;
|
|
3
|
+
|
|
4
|
+
#[allow(warnings)]
|
|
5
|
+
mod bindings;
|
|
6
|
+
|
|
7
|
+
struct Component;
|
|
8
|
+
|
|
9
|
+
impl Guest for Component {
|
|
10
|
+
fn greet(name: String) {
|
|
11
|
+
let greeting = format!("hello {name}");
|
|
12
|
+
api::send_response_to_agent(&greeting);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
bindings::export!(Component with_types_in bindings);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "plugin",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"author": "",
|
|
6
|
+
"license": "UNLICENSED",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"auth": "asterai auth",
|
|
10
|
+
"gen:pkg": "mkdirp build && asterai pkg -o build/package.wasm -w build/package.wit",
|
|
11
|
+
"gen:types": "jco guest-types build/package.wit -n plugin -o generated/",
|
|
12
|
+
"compile": "tsc",
|
|
13
|
+
"componentize": "jco componentize build/plugin.js -w build/package.wit -n plugin -o build/plugin.wasm",
|
|
14
|
+
"build": "run-s gen:pkg gen:types compile componentize",
|
|
15
|
+
"deploy": "asterai deploy"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@asterai/cli": "latest",
|
|
19
|
+
"@asterai/sdk": "latest",
|
|
20
|
+
"@bytecodealliance/jco": "^1.9.1",
|
|
21
|
+
"@types/node": "^22.13.4",
|
|
22
|
+
"mkdirp": "^3.0.1",
|
|
23
|
+
"npm-run-all": "^4.1.5",
|
|
24
|
+
"typescript": "^5.7.3"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { BinaryOperationInput } from "./generated/plugin";
|
|
2
|
+
import * as asterai from "asterai:host/api@0.1.0";
|
|
3
|
+
|
|
4
|
+
export const add = (input: BinaryOperationInput): number => {
|
|
5
|
+
const result = input.a + input.b;
|
|
6
|
+
// Send the calculation result to the agent.
|
|
7
|
+
asterai.sendResponseToAgent(`the result is ${result}`);
|
|
8
|
+
// This result is not seen by the agent, but it can be consumed by
|
|
9
|
+
// other plugins calling this function.
|
|
10
|
+
return result;
|
|
11
|
+
};
|
package/oclif.manifest.json
CHANGED
|
@@ -28,24 +28,49 @@
|
|
|
28
28
|
"auth.js"
|
|
29
29
|
]
|
|
30
30
|
},
|
|
31
|
-
"
|
|
31
|
+
"deploy": {
|
|
32
32
|
"aliases": [],
|
|
33
|
-
"args": {
|
|
34
|
-
|
|
35
|
-
"default": "plugin.ts",
|
|
36
|
-
"name": "input"
|
|
37
|
-
}
|
|
38
|
-
},
|
|
39
|
-
"description": "compiles the plugin",
|
|
33
|
+
"args": {},
|
|
34
|
+
"description": "uploads a plugin to asterai",
|
|
40
35
|
"examples": [
|
|
41
|
-
"<%= config.bin %> <%= command.id %>"
|
|
36
|
+
"<%= config.bin %> <%= command.id %> --app 66a46b12-b1a7-4b72-a64a-0e4fe21902b6"
|
|
42
37
|
],
|
|
43
38
|
"flags": {
|
|
44
|
-
"
|
|
45
|
-
"char": "
|
|
46
|
-
"description": "
|
|
47
|
-
"name": "
|
|
48
|
-
"
|
|
39
|
+
"agent": {
|
|
40
|
+
"char": "a",
|
|
41
|
+
"description": "agent ID to immediately activate this plugin for",
|
|
42
|
+
"name": "agent",
|
|
43
|
+
"required": false,
|
|
44
|
+
"hasDynamicHelp": false,
|
|
45
|
+
"multiple": false,
|
|
46
|
+
"type": "option"
|
|
47
|
+
},
|
|
48
|
+
"endpoint": {
|
|
49
|
+
"char": "e",
|
|
50
|
+
"name": "endpoint",
|
|
51
|
+
"default": "https://api.asterai.io",
|
|
52
|
+
"hasDynamicHelp": false,
|
|
53
|
+
"multiple": false,
|
|
54
|
+
"type": "option"
|
|
55
|
+
},
|
|
56
|
+
"staging": {
|
|
57
|
+
"char": "s",
|
|
58
|
+
"name": "staging",
|
|
59
|
+
"allowNo": false,
|
|
60
|
+
"type": "boolean"
|
|
61
|
+
},
|
|
62
|
+
"plugin": {
|
|
63
|
+
"description": "plugin WASM path",
|
|
64
|
+
"name": "plugin",
|
|
65
|
+
"default": "plugin.wasm",
|
|
66
|
+
"hasDynamicHelp": false,
|
|
67
|
+
"multiple": false,
|
|
68
|
+
"type": "option"
|
|
69
|
+
},
|
|
70
|
+
"pkg": {
|
|
71
|
+
"description": "package WASM path",
|
|
72
|
+
"name": "pkg",
|
|
73
|
+
"default": "package.wasm",
|
|
49
74
|
"hasDynamicHelp": false,
|
|
50
75
|
"multiple": false,
|
|
51
76
|
"type": "option"
|
|
@@ -53,7 +78,7 @@
|
|
|
53
78
|
},
|
|
54
79
|
"hasDynamicHelp": false,
|
|
55
80
|
"hiddenAliases": [],
|
|
56
|
-
"id": "
|
|
81
|
+
"id": "deploy",
|
|
57
82
|
"pluginAlias": "@asterai/cli",
|
|
58
83
|
"pluginName": "@asterai/cli",
|
|
59
84
|
"pluginType": "core",
|
|
@@ -63,66 +88,38 @@
|
|
|
63
88
|
"relativePath": [
|
|
64
89
|
"dist",
|
|
65
90
|
"commands",
|
|
66
|
-
"
|
|
91
|
+
"deploy.js"
|
|
67
92
|
]
|
|
68
93
|
},
|
|
69
|
-
"
|
|
94
|
+
"init": {
|
|
70
95
|
"aliases": [],
|
|
71
|
-
"args": {
|
|
72
|
-
|
|
96
|
+
"args": {
|
|
97
|
+
"outDir": {
|
|
98
|
+
"default": "plugin",
|
|
99
|
+
"name": "outDir"
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
"description": "Initialise a new plugin project",
|
|
73
103
|
"examples": [
|
|
74
|
-
"<%= config.bin %> <%= command.id %>"
|
|
104
|
+
"<%= config.bin %> <%= command.id %> project-name"
|
|
75
105
|
],
|
|
76
106
|
"flags": {
|
|
77
|
-
"
|
|
78
|
-
"
|
|
79
|
-
"
|
|
80
|
-
"
|
|
81
|
-
"
|
|
82
|
-
"hasDynamicHelp": false,
|
|
83
|
-
"multiple": false,
|
|
84
|
-
"type": "option"
|
|
85
|
-
},
|
|
86
|
-
"outputDir": {
|
|
87
|
-
"char": "o",
|
|
88
|
-
"description": "output directory",
|
|
89
|
-
"name": "outputDir",
|
|
90
|
-
"default": "generated",
|
|
91
|
-
"hasDynamicHelp": false,
|
|
92
|
-
"multiple": false,
|
|
93
|
-
"type": "option"
|
|
94
|
-
},
|
|
95
|
-
"appId": {
|
|
96
|
-
"char": "a",
|
|
97
|
-
"description": "app id",
|
|
98
|
-
"name": "appId",
|
|
99
|
-
"required": false,
|
|
100
|
-
"hasDynamicHelp": false,
|
|
101
|
-
"multiple": false,
|
|
102
|
-
"type": "option"
|
|
103
|
-
},
|
|
104
|
-
"language": {
|
|
105
|
-
"char": "l",
|
|
106
|
-
"description": "language of generated typings",
|
|
107
|
-
"name": "language",
|
|
108
|
-
"required": false,
|
|
109
|
-
"default": "js",
|
|
110
|
-
"hasDynamicHelp": false,
|
|
111
|
-
"multiple": false,
|
|
112
|
-
"type": "option"
|
|
107
|
+
"typescript": {
|
|
108
|
+
"description": "init a the plugin project in typescript",
|
|
109
|
+
"name": "typescript",
|
|
110
|
+
"allowNo": false,
|
|
111
|
+
"type": "boolean"
|
|
113
112
|
},
|
|
114
|
-
"
|
|
115
|
-
"
|
|
116
|
-
"
|
|
117
|
-
"name": "staging",
|
|
118
|
-
"required": false,
|
|
113
|
+
"rust": {
|
|
114
|
+
"description": "init a the plugin project in rust",
|
|
115
|
+
"name": "rust",
|
|
119
116
|
"allowNo": false,
|
|
120
117
|
"type": "boolean"
|
|
121
118
|
}
|
|
122
119
|
},
|
|
123
120
|
"hasDynamicHelp": false,
|
|
124
121
|
"hiddenAliases": [],
|
|
125
|
-
"id": "
|
|
122
|
+
"id": "init",
|
|
126
123
|
"pluginAlias": "@asterai/cli",
|
|
127
124
|
"pluginName": "@asterai/cli",
|
|
128
125
|
"pluginType": "core",
|
|
@@ -132,36 +129,37 @@
|
|
|
132
129
|
"relativePath": [
|
|
133
130
|
"dist",
|
|
134
131
|
"commands",
|
|
135
|
-
"
|
|
132
|
+
"init.js"
|
|
136
133
|
]
|
|
137
134
|
},
|
|
138
|
-
"
|
|
135
|
+
"pkg": {
|
|
139
136
|
"aliases": [],
|
|
140
137
|
"args": {
|
|
141
138
|
"input": {
|
|
142
|
-
"default": "plugin.
|
|
139
|
+
"default": "plugin.wit",
|
|
140
|
+
"description": "path to the plugin's WIT file",
|
|
143
141
|
"name": "input"
|
|
144
142
|
}
|
|
145
143
|
},
|
|
146
|
-
"description": "
|
|
144
|
+
"description": "bundles the WIT into a binary WASM package",
|
|
147
145
|
"examples": [
|
|
148
|
-
"<%= config.bin %> <%= command.id %>
|
|
146
|
+
"<%= config.bin %> <%= command.id %>"
|
|
149
147
|
],
|
|
150
148
|
"flags": {
|
|
151
|
-
"
|
|
152
|
-
"char": "
|
|
153
|
-
"description": "
|
|
154
|
-
"name": "
|
|
155
|
-
"
|
|
149
|
+
"output": {
|
|
150
|
+
"char": "o",
|
|
151
|
+
"description": "output file name for the binary WASM package",
|
|
152
|
+
"name": "output",
|
|
153
|
+
"default": "package.wasm",
|
|
156
154
|
"hasDynamicHelp": false,
|
|
157
155
|
"multiple": false,
|
|
158
156
|
"type": "option"
|
|
159
157
|
},
|
|
160
|
-
"
|
|
161
|
-
"char": "
|
|
162
|
-
"description": "
|
|
163
|
-
"name": "
|
|
164
|
-
"default": "
|
|
158
|
+
"wit": {
|
|
159
|
+
"char": "w",
|
|
160
|
+
"description": "output package converted to the WIT format",
|
|
161
|
+
"name": "wit",
|
|
162
|
+
"default": "package.wit",
|
|
165
163
|
"hasDynamicHelp": false,
|
|
166
164
|
"multiple": false,
|
|
167
165
|
"type": "option"
|
|
@@ -169,21 +167,15 @@
|
|
|
169
167
|
"endpoint": {
|
|
170
168
|
"char": "e",
|
|
171
169
|
"name": "endpoint",
|
|
172
|
-
"default": "https://api.asterai.io
|
|
170
|
+
"default": "https://api.asterai.io",
|
|
173
171
|
"hasDynamicHelp": false,
|
|
174
172
|
"multiple": false,
|
|
175
173
|
"type": "option"
|
|
176
|
-
},
|
|
177
|
-
"staging": {
|
|
178
|
-
"char": "s",
|
|
179
|
-
"name": "staging",
|
|
180
|
-
"allowNo": false,
|
|
181
|
-
"type": "boolean"
|
|
182
174
|
}
|
|
183
175
|
},
|
|
184
176
|
"hasDynamicHelp": false,
|
|
185
177
|
"hiddenAliases": [],
|
|
186
|
-
"id": "
|
|
178
|
+
"id": "pkg",
|
|
187
179
|
"pluginAlias": "@asterai/cli",
|
|
188
180
|
"pluginName": "@asterai/cli",
|
|
189
181
|
"pluginType": "core",
|
|
@@ -193,35 +185,7 @@
|
|
|
193
185
|
"relativePath": [
|
|
194
186
|
"dist",
|
|
195
187
|
"commands",
|
|
196
|
-
"
|
|
197
|
-
]
|
|
198
|
-
},
|
|
199
|
-
"init": {
|
|
200
|
-
"aliases": [],
|
|
201
|
-
"args": {
|
|
202
|
-
"outDir": {
|
|
203
|
-
"default": "plugin",
|
|
204
|
-
"name": "outDir"
|
|
205
|
-
}
|
|
206
|
-
},
|
|
207
|
-
"description": "Initialise a new plugin project",
|
|
208
|
-
"examples": [
|
|
209
|
-
"<%= config.bin %> <%= command.id %> project-name"
|
|
210
|
-
],
|
|
211
|
-
"flags": {},
|
|
212
|
-
"hasDynamicHelp": false,
|
|
213
|
-
"hiddenAliases": [],
|
|
214
|
-
"id": "init",
|
|
215
|
-
"pluginAlias": "@asterai/cli",
|
|
216
|
-
"pluginName": "@asterai/cli",
|
|
217
|
-
"pluginType": "core",
|
|
218
|
-
"strict": true,
|
|
219
|
-
"enableJsonFlag": false,
|
|
220
|
-
"isESM": true,
|
|
221
|
-
"relativePath": [
|
|
222
|
-
"dist",
|
|
223
|
-
"commands",
|
|
224
|
-
"init.js"
|
|
188
|
+
"pkg.js"
|
|
225
189
|
]
|
|
226
190
|
},
|
|
227
191
|
"query": {
|
|
@@ -280,5 +244,5 @@
|
|
|
280
244
|
]
|
|
281
245
|
}
|
|
282
246
|
},
|
|
283
|
-
"version": "0.
|
|
247
|
+
"version": "0.6.0"
|
|
284
248
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@asterai/cli",
|
|
3
3
|
"description": "CLI for building and deploying asterai plugins",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.6.0",
|
|
5
5
|
"author": "asterai <support@asterai.io>",
|
|
6
6
|
"repository": "asterai-io/asterai-sdk",
|
|
7
7
|
"homepage": "https://github.com/asterai-io/asterai-sdk",
|
|
@@ -36,7 +36,9 @@
|
|
|
36
36
|
"/bin",
|
|
37
37
|
"/dist",
|
|
38
38
|
"/oclif.manifest.json",
|
|
39
|
-
"/
|
|
39
|
+
"/init",
|
|
40
|
+
"!/init/rust/target",
|
|
41
|
+
"!/init/rust/Cargo.lock"
|
|
40
42
|
],
|
|
41
43
|
"oclif": {
|
|
42
44
|
"bin": "asterai",
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "plugin",
|
|
3
|
-
"version": "1.0.0",
|
|
4
|
-
"description": "",
|
|
5
|
-
"author": "asterai",
|
|
6
|
-
"license": "MIT",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"auth": "asterai auth",
|
|
9
|
-
"codegen": "asterai codegen",
|
|
10
|
-
"build": "asterai build",
|
|
11
|
-
"deploy": "asterai deploy"
|
|
12
|
-
},
|
|
13
|
-
"type": "module",
|
|
14
|
-
"devDependencies": {
|
|
15
|
-
"@asterai/cli": "latest",
|
|
16
|
-
"@asterai/sdk": "latest",
|
|
17
|
-
"@asterai/as-proto": "latest",
|
|
18
|
-
"@asterai/as-proto-gen": "latest"
|
|
19
|
-
},
|
|
20
|
-
"peerDependencies": {
|
|
21
|
-
"assemblyscript": "0.27.27"
|
|
22
|
-
}
|
|
23
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
syntax = "proto3";
|
|
2
|
-
import "node_modules/@asterai/sdk/protobuf/asterai.proto";
|
|
3
|
-
|
|
4
|
-
service Math {
|
|
5
|
-
rpc processQuery(PluginContext) returns (ProcessQueryOutput);
|
|
6
|
-
rpc add(BinaryOperationInput) returns (CalculationOutput);
|
|
7
|
-
rpc mul(BinaryOperationInput) returns (CalculationOutput);
|
|
8
|
-
rpc div(BinaryOperationInput) returns (CalculationOutput);
|
|
9
|
-
rpc pow(BinaryOperationInput) returns (CalculationOutput);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
message ProcessQueryOutput {}
|
|
13
|
-
|
|
14
|
-
message BinaryOperationInput {
|
|
15
|
-
PluginContext context = 1;
|
|
16
|
-
double a = 2;
|
|
17
|
-
double b = 3;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
message CalculationOutput {
|
|
21
|
-
string system_message = 1;
|
|
22
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { BinaryOperationInput } from "./generated/BinaryOperationInput";
|
|
2
|
-
import { CalculationOutput } from "./generated/CalculationOutput";
|
|
3
|
-
import { PluginContext } from "./generated/PluginContext";
|
|
4
|
-
import { ProcessQueryOutput } from "./generated/ProcessQueryOutput";
|
|
5
|
-
import { Log } from "@asterai/sdk";
|
|
6
|
-
|
|
7
|
-
export function processQuery(input: PluginContext): ProcessQueryOutput {
|
|
8
|
-
Log.info(`math plugin received a query: ${input.query.content}`);
|
|
9
|
-
// This doesn't return any data, but protobuf requires functions
|
|
10
|
-
// to always have one input and one output exactly to ensure
|
|
11
|
-
// backward compatibility.
|
|
12
|
-
return new ProcessQueryOutput();
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function add(input: BinaryOperationInput): CalculationOutput {
|
|
16
|
-
const result = input.a + input.b;
|
|
17
|
-
// CalculationOutput returns a system message.
|
|
18
|
-
// The `system_message` field is sent to the LLM.
|
|
19
|
-
return new CalculationOutput(`the result is ${result}`);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function mul(input: BinaryOperationInput): CalculationOutput {
|
|
23
|
-
const result = input.a * input.b;
|
|
24
|
-
return new CalculationOutput(`the result is ${result}`);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function div(input: BinaryOperationInput): CalculationOutput {
|
|
28
|
-
const result = input.a / input.b;
|
|
29
|
-
return new CalculationOutput(`the result is ${result}`);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function pow(input: BinaryOperationInput): CalculationOutput {
|
|
33
|
-
const result = input.a ** input.b;
|
|
34
|
-
return new CalculationOutput(`the result is ${result}`);
|
|
35
|
-
}
|
package/dist/commands/build.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { Command } from "@oclif/core";
|
|
2
|
-
export type BuildArgs = {
|
|
3
|
-
input: string;
|
|
4
|
-
};
|
|
5
|
-
export type BuildFlags = {
|
|
6
|
-
manifest: string;
|
|
7
|
-
};
|
|
8
|
-
export type BuildOutput = {
|
|
9
|
-
outputFile: string;
|
|
10
|
-
manifestPath: string;
|
|
11
|
-
};
|
|
12
|
-
export default class Build extends Command {
|
|
13
|
-
static args: {
|
|
14
|
-
input: import("@oclif/core/lib/interfaces/parser.js").Arg<string, Record<string, unknown>>;
|
|
15
|
-
};
|
|
16
|
-
static description: string;
|
|
17
|
-
static examples: string[];
|
|
18
|
-
static flags: {
|
|
19
|
-
manifest: import("@oclif/core/lib/interfaces/parser.js").OptionFlag<string, import("@oclif/core/lib/interfaces/parser.js").CustomOptions>;
|
|
20
|
-
};
|
|
21
|
-
run(): Promise<void>;
|
|
22
|
-
}
|
|
23
|
-
export declare const build: (args: BuildArgs, flags: BuildFlags) => Promise<BuildOutput>;
|
package/dist/commands/build.js
DELETED
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
import { Args, Command, Flags } from "@oclif/core";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import fs from "fs";
|
|
4
|
-
import { compile } from "../compile.js";
|
|
5
|
-
import Mustache from "mustache";
|
|
6
|
-
import protobuf from "protobufjs";
|
|
7
|
-
import { mergeProtoImports } from "./deploy.js";
|
|
8
|
-
export default class Build extends Command {
|
|
9
|
-
static args = {
|
|
10
|
-
input: Args.string({
|
|
11
|
-
default: "plugin.ts",
|
|
12
|
-
}),
|
|
13
|
-
};
|
|
14
|
-
static description = "compiles the plugin";
|
|
15
|
-
static examples = [`<%= config.bin %> <%= command.id %>`];
|
|
16
|
-
static flags = {
|
|
17
|
-
manifest: Flags.string({
|
|
18
|
-
char: "m",
|
|
19
|
-
description: "manifest path",
|
|
20
|
-
default: "plugin.asterai.proto",
|
|
21
|
-
}),
|
|
22
|
-
};
|
|
23
|
-
async run() {
|
|
24
|
-
const { args, flags } = await this.parse(Build);
|
|
25
|
-
await build(args, flags);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
export const build = async (args, flags) => {
|
|
29
|
-
const manifestPath = path.resolve(flags.manifest);
|
|
30
|
-
const inputFile = path.resolve(args.input);
|
|
31
|
-
if (!fs.existsSync(inputFile)) {
|
|
32
|
-
throw new Error(`input file not found (${args.input})`);
|
|
33
|
-
}
|
|
34
|
-
const inputFileName = path.parse(inputFile).name;
|
|
35
|
-
const baseDir = path.dirname(manifestPath);
|
|
36
|
-
const outDir = path.join(baseDir, "build");
|
|
37
|
-
const outputFile = path.join(outDir, `${inputFileName}.wasm`);
|
|
38
|
-
const libsDir = path.join(baseDir, "node_modules");
|
|
39
|
-
if (!fs.existsSync(libsDir)) {
|
|
40
|
-
throw new Error("no node_modules found in the plugin directory");
|
|
41
|
-
}
|
|
42
|
-
const proto = fs.readFileSync(manifestPath, { encoding: "utf8" });
|
|
43
|
-
const functionDescriptors = getPluginFunctionDescriptors(proto, manifestPath);
|
|
44
|
-
const inputFileContent = fs.readFileSync(inputFile, { encoding: "utf8" });
|
|
45
|
-
assertPluginCodeHasAllFunctionsFromManifest(inputFileContent, functionDescriptors);
|
|
46
|
-
const entryPointCode = generateEntryPointCode(functionDescriptors);
|
|
47
|
-
const mergedPluginCode = mergeInputPluginCodeWithEntrypoint(inputFileContent, entryPointCode);
|
|
48
|
-
const mergedTempFilePath = writeMergedPluginCodeTempFile(mergedPluginCode, path.parse(inputFile).dir, inputFileName);
|
|
49
|
-
const globalFile = path.join(libsDir, "@asterai/sdk/global.ts");
|
|
50
|
-
const options = {
|
|
51
|
-
inputFiles: [mergedTempFilePath, globalFile],
|
|
52
|
-
baseDir,
|
|
53
|
-
outputFile,
|
|
54
|
-
libs: libsDir,
|
|
55
|
-
};
|
|
56
|
-
try {
|
|
57
|
-
await compile(options);
|
|
58
|
-
}
|
|
59
|
-
finally {
|
|
60
|
-
fs.unlinkSync(mergedTempFilePath);
|
|
61
|
-
}
|
|
62
|
-
return {
|
|
63
|
-
manifestPath,
|
|
64
|
-
outputFile,
|
|
65
|
-
};
|
|
66
|
-
};
|
|
67
|
-
const getPluginFunctionDescriptors = (proto, protoPath) => {
|
|
68
|
-
const protoMerged = mergeProtoImports(proto, protoPath, false, false);
|
|
69
|
-
const result = protobuf.parse(protoMerged);
|
|
70
|
-
const namespace = result.root.resolveAll();
|
|
71
|
-
const objects = namespace.nestedArray;
|
|
72
|
-
const serviceObject = objects.find(o => o.methods !== undefined);
|
|
73
|
-
if (!serviceObject) {
|
|
74
|
-
throw new Error("no service found in plugin manifest");
|
|
75
|
-
}
|
|
76
|
-
const service = serviceObject;
|
|
77
|
-
return service.methodsArray.map(m => ({
|
|
78
|
-
functionName: m.name,
|
|
79
|
-
inputType: m.requestType,
|
|
80
|
-
outputType: m.responseType,
|
|
81
|
-
}));
|
|
82
|
-
};
|
|
83
|
-
const generateEntryPointCode = (functionDescriptors) => {
|
|
84
|
-
const importsCode = `
|
|
85
|
-
import { Protobuf } from "@asterai/as-proto/assembly";
|
|
86
|
-
import { readBufferFromPtr, writeBufferToPr } from "@asterai/sdk/buffer";
|
|
87
|
-
`;
|
|
88
|
-
let code = "\n// generated plugin entry points\n\n";
|
|
89
|
-
for (const functionDescriptor of functionDescriptors) {
|
|
90
|
-
const template = `
|
|
91
|
-
export function {{func}}_entry_point(ptr: u32): u32 {
|
|
92
|
-
const inputBuffer = readBufferFromPtr(ptr);
|
|
93
|
-
const input = Protobuf.decode<{{inpt}}>(
|
|
94
|
-
inputBuffer,
|
|
95
|
-
{{inpt}}.decode,
|
|
96
|
-
);
|
|
97
|
-
const output = {{func}}(input);
|
|
98
|
-
const outputBuffer = Protobuf.encode<{{outp}}>(
|
|
99
|
-
output,
|
|
100
|
-
{{outp}}.encode,
|
|
101
|
-
);
|
|
102
|
-
return writeBufferToPr(outputBuffer);
|
|
103
|
-
}
|
|
104
|
-
`;
|
|
105
|
-
const view = {
|
|
106
|
-
func: functionDescriptor.functionName,
|
|
107
|
-
inpt: functionDescriptor.inputType,
|
|
108
|
-
outp: functionDescriptor.outputType,
|
|
109
|
-
};
|
|
110
|
-
const functionEntryPoint = Mustache.render(template, view);
|
|
111
|
-
code = `${code}\n${functionEntryPoint}`;
|
|
112
|
-
}
|
|
113
|
-
return {
|
|
114
|
-
importsCode,
|
|
115
|
-
entryPointsCode: code,
|
|
116
|
-
};
|
|
117
|
-
};
|
|
118
|
-
const mergeInputPluginCodeWithEntrypoint = (pluginCode, entryPoint) => `${entryPoint.importsCode}\n${pluginCode}\n${entryPoint.entryPointsCode}`;
|
|
119
|
-
const writeMergedPluginCodeTempFile = (source, inputFileDir, inputFileName) => {
|
|
120
|
-
const tempFileName = `.entrypoint.${inputFileName}.ts`;
|
|
121
|
-
const tempFilePath = path.join(inputFileDir, tempFileName);
|
|
122
|
-
fs.writeFileSync(tempFilePath, source, { encoding: "utf8" });
|
|
123
|
-
return tempFilePath;
|
|
124
|
-
};
|
|
125
|
-
/**
|
|
126
|
-
* Throw an error if the plugin WASM source is missing a function from
|
|
127
|
-
* the manifest.
|
|
128
|
-
* The only purpose of this function is to let the user know about
|
|
129
|
-
* the issue in a direct way.
|
|
130
|
-
*/
|
|
131
|
-
const assertPluginCodeHasAllFunctionsFromManifest = (source, functionDescriptors) => {
|
|
132
|
-
for (const functionDescriptor of functionDescriptors) {
|
|
133
|
-
const includesFunction = source.includes(`function ${functionDescriptor.functionName}`);
|
|
134
|
-
if (!includesFunction) {
|
|
135
|
-
throw new Error(`function "${functionDescriptor.functionName}" was defined in plugin ` +
|
|
136
|
-
"manifest (.proto file) but is missing from plugin code");
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
};
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { Command } from "@oclif/core";
|
|
2
|
-
export type CodegenFlags = {
|
|
3
|
-
manifest: string;
|
|
4
|
-
outputDir: string;
|
|
5
|
-
appId?: string;
|
|
6
|
-
language?: string;
|
|
7
|
-
staging?: boolean;
|
|
8
|
-
};
|
|
9
|
-
export default class Codegen extends Command {
|
|
10
|
-
static args: {};
|
|
11
|
-
static description: string;
|
|
12
|
-
static examples: string[];
|
|
13
|
-
static flags: {
|
|
14
|
-
manifest: import("@oclif/core/lib/interfaces/parser.js").OptionFlag<string, import("@oclif/core/lib/interfaces/parser.js").CustomOptions>;
|
|
15
|
-
outputDir: import("@oclif/core/lib/interfaces/parser.js").OptionFlag<string, import("@oclif/core/lib/interfaces/parser.js").CustomOptions>;
|
|
16
|
-
appId: import("@oclif/core/lib/interfaces/parser.js").OptionFlag<string | undefined, import("@oclif/core/lib/interfaces/parser.js").CustomOptions>;
|
|
17
|
-
language: import("@oclif/core/lib/interfaces/parser.js").OptionFlag<string, import("@oclif/core/lib/interfaces/parser.js").CustomOptions>;
|
|
18
|
-
staging: import("@oclif/core/lib/interfaces/parser.js").BooleanFlag<boolean>;
|
|
19
|
-
};
|
|
20
|
-
run(): Promise<void>;
|
|
21
|
-
}
|
|
22
|
-
export declare const codegen: (flags: CodegenFlags) => Promise<void>;
|