@asterai/cli 0.3.1 → 0.4.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 CHANGED
@@ -20,7 +20,7 @@ $ npm install -g @asterai/cli
20
20
  $ asterai COMMAND
21
21
  running command...
22
22
  $ asterai (--version)
23
- @asterai/cli/0.3.1 linux-x64 node-v20.12.2
23
+ @asterai/cli/0.4.0 linux-x64 node-v20.12.2
24
24
  $ asterai --help [COMMAND]
25
25
  USAGE
26
26
  $ asterai COMMAND
@@ -37,6 +37,7 @@ USAGE
37
37
  * [`asterai deploy [INPUT]`](#asterai-deploy-input)
38
38
  * [`asterai help [COMMAND]`](#asterai-help-command)
39
39
  * [`asterai init [OUTDIR]`](#asterai-init-outdir)
40
+ * [`asterai query`](#asterai-query)
40
41
 
41
42
  ## `asterai auth KEY`
42
43
 
@@ -53,7 +54,7 @@ EXAMPLES
53
54
  $ asterai auth
54
55
  ```
55
56
 
56
- _See code: [src/commands/auth.ts](https://github.com/asterai-io/asterai-sdk/blob/v0.3.1/src/commands/auth.ts)_
57
+ _See code: [src/commands/auth.ts](https://github.com/asterai-io/asterai-sdk/blob/v0.4.0/src/commands/auth.ts)_
57
58
 
58
59
  ## `asterai build [INPUT]`
59
60
 
@@ -73,7 +74,7 @@ EXAMPLES
73
74
  $ asterai build
74
75
  ```
75
76
 
76
- _See code: [src/commands/build.ts](https://github.com/asterai-io/asterai-sdk/blob/v0.3.1/src/commands/build.ts)_
77
+ _See code: [src/commands/build.ts](https://github.com/asterai-io/asterai-sdk/blob/v0.4.0/src/commands/build.ts)_
77
78
 
78
79
  ## `asterai codegen`
79
80
 
@@ -97,7 +98,7 @@ EXAMPLES
97
98
  $ asterai codegen
98
99
  ```
99
100
 
100
- _See code: [src/commands/codegen.ts](https://github.com/asterai-io/asterai-sdk/blob/v0.3.1/src/commands/codegen.ts)_
101
+ _See code: [src/commands/codegen.ts](https://github.com/asterai-io/asterai-sdk/blob/v0.4.0/src/commands/codegen.ts)_
101
102
 
102
103
  ## `asterai deploy [INPUT]`
103
104
 
@@ -120,7 +121,7 @@ EXAMPLES
120
121
  $ asterai deploy --app 66a46b12-b1a7-4b72-a64a-0e4fe21902b6
121
122
  ```
122
123
 
123
- _See code: [src/commands/deploy.ts](https://github.com/asterai-io/asterai-sdk/blob/v0.3.1/src/commands/deploy.ts)_
124
+ _See code: [src/commands/deploy.ts](https://github.com/asterai-io/asterai-sdk/blob/v0.4.0/src/commands/deploy.ts)_
124
125
 
125
126
  ## `asterai help [COMMAND]`
126
127
 
@@ -157,5 +158,28 @@ EXAMPLES
157
158
  $ asterai init project-name
158
159
  ```
159
160
 
160
- _See code: [src/commands/init.ts](https://github.com/asterai-io/asterai-sdk/blob/v0.3.1/src/commands/init.ts)_
161
+ _See code: [src/commands/init.ts](https://github.com/asterai-io/asterai-sdk/blob/v0.4.0/src/commands/init.ts)_
162
+
163
+ ## `asterai query`
164
+
165
+ query an asterai app interactively
166
+
167
+ ```
168
+ USAGE
169
+ $ asterai query -a <value> -k <value> [-s] [-e <value>]
170
+
171
+ FLAGS
172
+ -a, --app=<value> (required)
173
+ -e, --endpoint=<value> [default: https://api.asterai.io]
174
+ -k, --key=<value> (required) app query key
175
+ -s, --staging
176
+
177
+ DESCRIPTION
178
+ query an asterai app interactively
179
+
180
+ EXAMPLES
181
+ $ asterai query
182
+ ```
183
+
184
+ _See code: [src/commands/query.ts](https://github.com/asterai-io/asterai-sdk/blob/v0.4.0/src/commands/query.ts)_
161
185
  <!-- commandsstop -->
@@ -60,9 +60,9 @@ export const codegen = (flags) => {
60
60
  if (flags.appId && flags.language) {
61
61
  return generateClientTypings(outDir, flags.language, flags.appId, flags.staging ?? false);
62
62
  }
63
- return generateAssemblyScriptPluginTypings(outDir, baseDir, manifestPath);
63
+ return generateAssemblyScriptPluginTypings(flags, baseDir);
64
64
  };
65
- const generateAssemblyScriptPluginTypings = async (outDir, baseDir, manifestPath) => {
65
+ const generateAssemblyScriptPluginTypings = async (flags, baseDir) => {
66
66
  const absoluteAsProtoGenPath = path.join(baseDir, AS_PROTO_GEN_PATH);
67
67
  try {
68
68
  execSync("protoc --version");
@@ -77,7 +77,7 @@ const generateAssemblyScriptPluginTypings = async (outDir, baseDir, manifestPath
77
77
  execSync("protoc " +
78
78
  `--plugin='protoc-gen-as=${absoluteAsProtoGenPath}' ` +
79
79
  `--experimental_allow_proto3_optional ` +
80
- `--as_out='./${outDir}' ./${manifestPath}`);
80
+ `--as_out='./${flags.outputDir}' ./${flags.manifest}`);
81
81
  }
82
82
  catch (e) {
83
83
  console.error("Failed to generate protobuf types:", e);
@@ -96,32 +96,43 @@ const deleteOldGeneratedFiles = (outDir) => {
96
96
  };
97
97
  const generateClientTypings = async (outDir, language, appId, shouldUseStaging) => {
98
98
  const manifestsResponse = await downloadEnabledPluginsManifests(appId, shouldUseStaging);
99
- fs.writeFileSync(path.join(outDir, "manifests.json"), JSON.stringify(manifestsResponse, null, 2));
100
- console.log("manifests.json generated successfully.");
99
+ const asteraiProto = await fetchAsteraiProto();
100
+ const aggregatedManifest = aggregateManifests(manifestsResponse.manifests, asteraiProto);
101
+ const appPrefix = `app.${appId}`;
102
+ fs.writeFileSync(path.join(outDir, `${appPrefix}.proto`), aggregatedManifest.content);
101
103
  if (language === "ts") {
102
- const aggregatedManifestPath = aggregateManifests(manifestsResponse.manifests);
104
+ console.log("generating TypeScript typings for plugin manifest...");
105
+ const jsOutput = `${appPrefix}.js`;
106
+ const dTsOutput = `${appPrefix}.d.ts`;
103
107
  execSync(`
104
- npx -p protobufjs-cli pbjs -t static --no-service ${aggregatedManifestPath} -o ${path.join(outDir, "plugins.asterai.js")}
108
+ npx -p protobufjs-cli pbjs -t static --no-service ${aggregatedManifest.filePath} -o ${path.join(outDir, jsOutput)}
105
109
  `);
106
110
  execSync(`
107
- npx -p protobufjs-cli pbts -o ${path.join(outDir, "plugins.asterai.d.ts")} ${path.join(outDir, "plugins.asterai.js")}
111
+ npx -p protobufjs-cli pbts -o ${path.join(outDir, dTsOutput)} ${path.join(outDir, jsOutput)}
108
112
  `);
109
- fs.unlinkSync(aggregatedManifestPath);
113
+ fs.unlinkSync(aggregatedManifest.filePath);
110
114
  console.log("Typings generated successfully.");
111
115
  }
116
+ console.log("done.");
112
117
  };
113
- const aggregateManifests = (manifests) => {
114
- let aggregatedManifest = `
115
- syntax = 'proto3';
116
- \n
117
- `;
118
+ const fetchAsteraiProto = () => {
119
+ // TODO: fetch this from the local file system instead.
120
+ return axios
121
+ .get("https://unpkg.com/@asterai/sdk@latest/protobuf/asterai.proto")
122
+ .then(r => r.data);
123
+ };
124
+ const aggregateManifests = (manifests, asteraiProto) => {
125
+ let aggregatedManifest = `${asteraiProto}\n`;
118
126
  const osTmpDir = os.tmpdir();
119
127
  const aggregatedManifestPath = path.join(osTmpDir, "plugins.asterai.proto");
120
128
  for (const manifest of manifests) {
121
129
  aggregatedManifest += `${manifest.proto}\n`;
122
130
  }
123
131
  fs.writeFileSync(aggregatedManifestPath, aggregatedManifest);
124
- return aggregatedManifestPath;
132
+ return {
133
+ content: aggregatedManifest,
134
+ filePath: aggregatedManifestPath,
135
+ };
125
136
  };
126
137
  const downloadEnabledPluginsManifests = async (appId, shouldUseStaging) => {
127
138
  const baseUrl = shouldUseStaging
@@ -0,0 +1,13 @@
1
+ import { Command } from "@oclif/core";
2
+ export default class Query extends Command {
3
+ static args: {};
4
+ static description: string;
5
+ static examples: string[];
6
+ static flags: {
7
+ app: import("@oclif/core/lib/interfaces/parser.js").OptionFlag<string, import("@oclif/core/lib/interfaces/parser.js").CustomOptions>;
8
+ key: import("@oclif/core/lib/interfaces/parser.js").OptionFlag<string, import("@oclif/core/lib/interfaces/parser.js").CustomOptions>;
9
+ staging: import("@oclif/core/lib/interfaces/parser.js").BooleanFlag<boolean>;
10
+ endpoint: import("@oclif/core/lib/interfaces/parser.js").OptionFlag<string, import("@oclif/core/lib/interfaces/parser.js").CustomOptions>;
11
+ };
12
+ run(): Promise<void>;
13
+ }
@@ -0,0 +1,93 @@
1
+ import { Flags, Command } from "@oclif/core";
2
+ import readline from "node:readline";
3
+ import { AsteraiClient } from "@asterai/client";
4
+ import { v4 as uuidv4 } from "uuid";
5
+ const ANSI_COLORS = {
6
+ reset: "\x1b[0m",
7
+ bold: "\u001b[1m",
8
+ };
9
+ const USER_PREFIX = `${ANSI_COLORS.bold}user: ${ANSI_COLORS.reset}`;
10
+ const ASSISTANT_PREFIX = `${ANSI_COLORS.bold}assistant: ${ANSI_COLORS.reset}`;
11
+ const PRODUCTION_BASE_URL = "https://api.asterai.io";
12
+ const STAGING_BASE_URL = "https://staging.api.asterai.io";
13
+ export default class Query extends Command {
14
+ static args = {};
15
+ static description = "query an asterai app interactively";
16
+ static examples = [`<%= config.bin %> <%= command.id %>`];
17
+ static flags = {
18
+ app: Flags.string({
19
+ char: "a",
20
+ required: true,
21
+ }),
22
+ key: Flags.string({
23
+ char: "k",
24
+ required: true,
25
+ description: "app query key",
26
+ }),
27
+ staging: Flags.boolean({
28
+ char: "s",
29
+ }),
30
+ endpoint: Flags.string({
31
+ char: "e",
32
+ default: PRODUCTION_BASE_URL,
33
+ }),
34
+ };
35
+ async run() {
36
+ console.clear();
37
+ const { flags } = await this.parse(Query);
38
+ let output = "";
39
+ const addToOutput = (v) => {
40
+ output += v;
41
+ };
42
+ const apiBaseUrl = flags.staging ? STAGING_BASE_URL : flags.endpoint;
43
+ const client = new AsteraiClient({
44
+ appId: flags.app,
45
+ queryKey: flags.key,
46
+ apiBaseUrl,
47
+ });
48
+ const conversationId = uuidv4();
49
+ // Configure STDIN for when raw mode is enabled.
50
+ process.stdin.setEncoding("utf8");
51
+ process.stdin.on("data", key => {
52
+ if (key.toString() === "\u0003") {
53
+ process.stdout.write("\n");
54
+ process.exit();
55
+ }
56
+ });
57
+ const getUserInput = async () => {
58
+ addToOutput(USER_PREFIX);
59
+ const rl = readline.createInterface({
60
+ input: process.stdin,
61
+ output: process.stdout,
62
+ });
63
+ const input = await new Promise(resolve => rl.question(USER_PREFIX, i => resolve(i)));
64
+ rl.close();
65
+ // Enable raw mode to prevent STDIN from echoing in STDOUT.
66
+ process.stdin.setRawMode(true);
67
+ addToOutput(`${input}\r\n${ASSISTANT_PREFIX}`);
68
+ console.clear();
69
+ process.stdout.write(output);
70
+ const query = {
71
+ query: input,
72
+ conversationId,
73
+ };
74
+ const response = await client.query(query);
75
+ response.onToken(token => {
76
+ addToOutput(token);
77
+ process.stdout.write(token);
78
+ });
79
+ return new Promise(resolve => {
80
+ response.onEnd(() => {
81
+ addToOutput("\n");
82
+ process.stdout.write("\n");
83
+ // Disable raw mode to prepare for next user input.
84
+ process.stdin.setRawMode(false);
85
+ resolve(undefined);
86
+ });
87
+ });
88
+ };
89
+ while (true) {
90
+ await getUserInput();
91
+ }
92
+ }
93
+ }
@@ -223,7 +223,62 @@
223
223
  "commands",
224
224
  "init.js"
225
225
  ]
226
+ },
227
+ "query": {
228
+ "aliases": [],
229
+ "args": {},
230
+ "description": "query an asterai app interactively",
231
+ "examples": [
232
+ "<%= config.bin %> <%= command.id %>"
233
+ ],
234
+ "flags": {
235
+ "app": {
236
+ "char": "a",
237
+ "name": "app",
238
+ "required": true,
239
+ "hasDynamicHelp": false,
240
+ "multiple": false,
241
+ "type": "option"
242
+ },
243
+ "key": {
244
+ "char": "k",
245
+ "description": "app query key",
246
+ "name": "key",
247
+ "required": true,
248
+ "hasDynamicHelp": false,
249
+ "multiple": false,
250
+ "type": "option"
251
+ },
252
+ "staging": {
253
+ "char": "s",
254
+ "name": "staging",
255
+ "allowNo": false,
256
+ "type": "boolean"
257
+ },
258
+ "endpoint": {
259
+ "char": "e",
260
+ "name": "endpoint",
261
+ "default": "https://api.asterai.io",
262
+ "hasDynamicHelp": false,
263
+ "multiple": false,
264
+ "type": "option"
265
+ }
266
+ },
267
+ "hasDynamicHelp": false,
268
+ "hiddenAliases": [],
269
+ "id": "query",
270
+ "pluginAlias": "@asterai/cli",
271
+ "pluginName": "@asterai/cli",
272
+ "pluginType": "core",
273
+ "strict": true,
274
+ "enableJsonFlag": false,
275
+ "isESM": true,
276
+ "relativePath": [
277
+ "dist",
278
+ "commands",
279
+ "query.js"
280
+ ]
226
281
  }
227
282
  },
228
- "version": "0.3.1"
283
+ "version": "0.4.0"
229
284
  }
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.3.1",
4
+ "version": "0.4.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",
@@ -51,6 +51,7 @@
51
51
  "types": "dist/index.d.ts",
52
52
  "dependencies": {
53
53
  "@asterai/as-proto-gen": "^1.4.1",
54
+ "@asterai/client": "^0.1.1",
54
55
  "@oclif/core": "^3",
55
56
  "@oclif/plugin-help": "^6",
56
57
  "@oclif/plugin-plugins": "^5",
@@ -59,7 +60,8 @@
59
60
  "form-data": "^4.0.0",
60
61
  "mustache": "^4.2.0",
61
62
  "protobufjs": "^7.4.0",
62
- "protobufjs-cli": "^1.1.3"
63
+ "protobufjs-cli": "^1.1.3",
64
+ "uuid": "^11.0.2"
63
65
  },
64
66
  "devDependencies": {
65
67
  "@oclif/prettier-config": "^0.2.1",