@rakazo/cli 0.0.0-stage → 0.1.1
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/LICENSE +178 -0
- package/README.md +42 -2
- package/package.json +22 -4
- package/rakazo.js +336 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the
|
|
13
|
+
copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other
|
|
16
|
+
entities that control, are controlled by, or are under common control with
|
|
17
|
+
that entity. For the purposes of this definition, "control" means (i) the
|
|
18
|
+
power, direct or indirect, to cause the direction or management of such
|
|
19
|
+
entity, whether by contract or otherwise, or (ii) ownership of fifty percent
|
|
20
|
+
(50%) or more of the outstanding shares, or (iii) beneficial ownership of
|
|
21
|
+
such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
24
|
+
permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation source, and
|
|
28
|
+
configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical transformation or
|
|
31
|
+
translation of a Source form, including but not limited to compiled object
|
|
32
|
+
code, generated documentation, and conversions to other media types.
|
|
33
|
+
|
|
34
|
+
"Work" shall mean the work of authorship, whether in Source or Object form,
|
|
35
|
+
made available under the License, as indicated by a copyright notice that is
|
|
36
|
+
included in or attached to the work (an example is provided in the Appendix
|
|
37
|
+
below).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object form,
|
|
40
|
+
that is based on (or derived from) the Work and for which the editorial
|
|
41
|
+
revisions, annotations, elaborations, or other modifications represent, as a
|
|
42
|
+
whole, an original work of authorship. For the purposes of this License,
|
|
43
|
+
Derivative Works shall not include works that remain separable from, or
|
|
44
|
+
merely link (or bind by name) to the interfaces of, the Work and Derivative
|
|
45
|
+
Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean any work of authorship, including the original
|
|
48
|
+
version of the Work and any modifications or additions to that Work or
|
|
49
|
+
Derivative Works thereof, that is intentionally submitted to Licensor for
|
|
50
|
+
inclusion in the Work by the copyright owner or by an individual or Legal
|
|
51
|
+
Entity authorized to submit on behalf of the copyright owner. For the
|
|
52
|
+
purposes of this definition, "submitted" means any form of electronic,
|
|
53
|
+
verbal, or written communication sent to the Licensor or its representatives,
|
|
54
|
+
including but not limited to communication on electronic mailing lists,
|
|
55
|
+
source code control systems, and issue tracking systems that are managed by,
|
|
56
|
+
or on behalf of, the Licensor for the purpose of discussing and improving
|
|
57
|
+
the Work, but excluding communication that is conspicuously marked or
|
|
58
|
+
otherwise designated in writing by the copyright owner as "Not a Contribution."
|
|
59
|
+
|
|
60
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on
|
|
61
|
+
behalf of whom a Contribution has been received by Licensor and subsequently
|
|
62
|
+
incorporated within the Work.
|
|
63
|
+
|
|
64
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this
|
|
65
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
66
|
+
non-exclusive, no-charge, royalty-free, irrevocable copyright license to
|
|
67
|
+
reproduce, prepare Derivative Works of, publicly display, publicly perform,
|
|
68
|
+
sublicense, and distribute the Work and such Derivative Works in Source or
|
|
69
|
+
Object form.
|
|
70
|
+
|
|
71
|
+
3. Grant of Patent License. Subject to the terms and conditions of this
|
|
72
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
73
|
+
non-exclusive, no-charge, royalty-free, irrevocable (except as stated in
|
|
74
|
+
this section) patent license to make, have made, use, offer to sell, sell,
|
|
75
|
+
import, and otherwise transfer the Work, where such license applies only to
|
|
76
|
+
those patent claims licensable by such Contributor that are necessarily
|
|
77
|
+
infringed by their Contribution(s) alone or by combination of their
|
|
78
|
+
Contribution(s) with the Work to which such Contribution(s) was submitted.
|
|
79
|
+
If You institute patent litigation against any entity (including a
|
|
80
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work or a
|
|
81
|
+
Contribution incorporated within the Work constitutes direct or contributory
|
|
82
|
+
patent infringement, then any patent licenses granted to You under this
|
|
83
|
+
License for that Work shall terminate as of the date such litigation is
|
|
84
|
+
filed.
|
|
85
|
+
|
|
86
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or
|
|
87
|
+
Derivative Works thereof in any medium, with or without modifications, and
|
|
88
|
+
in Source or Object form, provided that You meet the following conditions:
|
|
89
|
+
|
|
90
|
+
(a) You must give any other recipients of the Work or Derivative Works a
|
|
91
|
+
copy of this License; and
|
|
92
|
+
|
|
93
|
+
(b) You must cause any modified files to carry prominent notices stating
|
|
94
|
+
that You changed the files; and
|
|
95
|
+
|
|
96
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
97
|
+
distribute, all copyright, patent, trademark, and attribution notices from
|
|
98
|
+
the Source form of the Work, excluding those notices that do not pertain to
|
|
99
|
+
any part of the Derivative Works; and
|
|
100
|
+
|
|
101
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution,
|
|
102
|
+
then any Derivative Works that You distribute must include a readable copy
|
|
103
|
+
of the attribution notices contained within such NOTICE file, excluding
|
|
104
|
+
those notices that do not pertain to any part of the Derivative Works, in at
|
|
105
|
+
least one of the following places: within a NOTICE text file distributed as
|
|
106
|
+
part of the Derivative Works; within the Source form or documentation, if
|
|
107
|
+
provided along with the Derivative Works; or, within a display generated by
|
|
108
|
+
the Derivative Works, if and wherever such third-party notices normally
|
|
109
|
+
appear. The contents of the NOTICE file are for informational purposes only
|
|
110
|
+
and do not modify the License. You may add Your own attribution notices
|
|
111
|
+
within Derivative Works that You distribute, alongside or as an addendum to
|
|
112
|
+
the NOTICE text from the Work, provided that such additional attribution
|
|
113
|
+
notices cannot be construed as modifying the License.
|
|
114
|
+
|
|
115
|
+
You may add Your own copyright statement to Your modifications and may
|
|
116
|
+
provide additional or different license terms and conditions for use,
|
|
117
|
+
reproduction, or distribution of Your modifications, or for any such
|
|
118
|
+
Derivative Works as a whole, provided Your use, reproduction, and
|
|
119
|
+
distribution of the Work otherwise complies with the conditions stated in
|
|
120
|
+
this License.
|
|
121
|
+
|
|
122
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any
|
|
123
|
+
Contribution intentionally submitted for inclusion in the Work by You to the
|
|
124
|
+
Licensor shall be under the terms and conditions of this License, without
|
|
125
|
+
any additional terms or conditions. Notwithstanding the above, nothing
|
|
126
|
+
herein shall supersede or modify the terms of any separate license agreement
|
|
127
|
+
you may have executed with Licensor regarding such Contributions.
|
|
128
|
+
|
|
129
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
130
|
+
names, trademarks, service marks, or product names of the Licensor, except
|
|
131
|
+
as required for reasonable and customary use in describing the origin of the
|
|
132
|
+
Work and reproducing the content of the NOTICE file.
|
|
133
|
+
|
|
134
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to
|
|
135
|
+
in writing, Licensor provides the Work (and each Contributor provides its
|
|
136
|
+
Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
137
|
+
KIND, either express or implied, including, without limitation, any
|
|
138
|
+
warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or
|
|
139
|
+
FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining
|
|
140
|
+
the appropriateness of using or redistributing the Work and assume any risks
|
|
141
|
+
associated with Your exercise of permissions under this License.
|
|
142
|
+
|
|
143
|
+
8. Limitation of Liability. In no event and under no legal theory, whether
|
|
144
|
+
in tort (including negligence), contract, or otherwise, unless required by
|
|
145
|
+
applicable law (such as deliberate and grossly negligent acts) or agreed to
|
|
146
|
+
in writing, shall any Contributor be liable to You for damages, including
|
|
147
|
+
any direct, indirect, special, incidental, or consequential damages of any
|
|
148
|
+
character arising as a result of this License or out of the use or inability
|
|
149
|
+
to use the Work (including but not limited to damages for loss of goodwill,
|
|
150
|
+
work stoppage, computer failure or malfunction, or any and all other
|
|
151
|
+
commercial damages or losses), even if such Contributor has been advised of
|
|
152
|
+
the possibility of such damages.
|
|
153
|
+
|
|
154
|
+
9. Accepting Warranty or Additional Liability. While redistributing the
|
|
155
|
+
Work or Derivative Works thereof, You may choose to offer, and charge a fee
|
|
156
|
+
for, acceptance of support, warranty, indemnity, or other liability
|
|
157
|
+
obligations and/or rights consistent with this License. However, in
|
|
158
|
+
accepting such obligations, You may act only on Your own behalf and on Your
|
|
159
|
+
sole responsibility, not on behalf of any other Contributor, and only if
|
|
160
|
+
You agree to indemnify, defend, and hold each Contributor harmless for any
|
|
161
|
+
liability incurred by, or claims asserted against, such Contributor by
|
|
162
|
+
reason of your accepting any such warranty or additional liability.
|
|
163
|
+
|
|
164
|
+
END OF TERMS AND CONDITIONS
|
|
165
|
+
|
|
166
|
+
Copyright 2026 Rakazo contributors
|
|
167
|
+
|
|
168
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
169
|
+
you may not use this file except in compliance with the License.
|
|
170
|
+
You may obtain a copy of the License at
|
|
171
|
+
|
|
172
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
173
|
+
|
|
174
|
+
Unless required by applicable law or agreed to in writing, software
|
|
175
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
176
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
177
|
+
See the License for the specific language governing permissions and
|
|
178
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,43 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Rakazo CLI
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Chat with your Rakazo bots and delegate tasks from a terminal or script.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
Requires Node.js 24 or later.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install --global @rakazo/cli
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Connect to Rakazo
|
|
14
|
+
|
|
15
|
+
In Rakazo, open **Settings → API keys** and create a key. Run `rakazo login`
|
|
16
|
+
and enter your Rakazo server URL and API key when prompted.
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
rakazo login
|
|
20
|
+
rakazo bots
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Chat and delegate tasks
|
|
24
|
+
|
|
25
|
+
Use a bot's exact name or the ID shown by `rakazo bots`. The examples use a bot
|
|
26
|
+
named `Research`.
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
rakazo chat Research "Summarize this week's release notes"
|
|
30
|
+
rakazo task create Research "Compare these three vendors and report back"
|
|
31
|
+
rakazo task get TASK_ID
|
|
32
|
+
rakazo task wait TASK_ID
|
|
33
|
+
rakazo task send TASK_ID "Include pricing in the comparison"
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Replace `TASK_ID` with the ID printed by `rakazo task create`. Add `--wait` to
|
|
37
|
+
`task create` or `task send` to wait in the same command. Add `--json` to bot,
|
|
38
|
+
chat, or task commands for machine-readable output. For scripts, set
|
|
39
|
+
`RAKAZO_URL` and `RAKAZO_API_KEY` to override the saved login.
|
|
40
|
+
|
|
41
|
+
Run `rakazo --help` for command options. See the
|
|
42
|
+
[Rakazo API documentation](https://github.com/elie222/rakazo/blob/main/docs/bot-api.md)
|
|
43
|
+
for more on authentication, tasks, and callbacks.
|
package/package.json
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rakazo/cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Hand work to your Rakazo bots from a terminal or script.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/elie222/rakazo.git",
|
|
10
|
+
"directory": "apps/cli"
|
|
11
|
+
},
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=24"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"bin": {
|
|
19
|
+
"rakazo": "./rakazo.js"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"rakazo.js"
|
|
23
|
+
]
|
|
24
|
+
}
|
package/rakazo.js
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/main.ts
|
|
4
|
+
import { stdin, stdout } from "node:process";
|
|
5
|
+
import { createInterface } from "node:readline/promises";
|
|
6
|
+
import { parseArgs } from "node:util";
|
|
7
|
+
|
|
8
|
+
// src/sse.ts
|
|
9
|
+
async function* readEvents(body) {
|
|
10
|
+
const decoder = new TextDecoder();
|
|
11
|
+
let buffer = "";
|
|
12
|
+
let event = "message";
|
|
13
|
+
let data = [];
|
|
14
|
+
for await (const chunk of body) {
|
|
15
|
+
buffer += decoder.decode(chunk, { stream: true });
|
|
16
|
+
let newline = buffer.indexOf("\n");
|
|
17
|
+
while (newline !== -1) {
|
|
18
|
+
const line = buffer.slice(0, newline).replace(/\r$/, "");
|
|
19
|
+
buffer = buffer.slice(newline + 1);
|
|
20
|
+
newline = buffer.indexOf("\n");
|
|
21
|
+
if (line === "") {
|
|
22
|
+
if (data.length) yield { event, data: data.join("\n") };
|
|
23
|
+
event = "message";
|
|
24
|
+
data = [];
|
|
25
|
+
} else if (line.startsWith("data:")) data.push(line.slice(5).replace(/^ /, ""));
|
|
26
|
+
else if (line.startsWith("event:")) event = line.slice(6).trim();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// src/client.ts
|
|
32
|
+
var ApiError = class extends Error {
|
|
33
|
+
constructor(status, message) {
|
|
34
|
+
super(message);
|
|
35
|
+
this.status = status;
|
|
36
|
+
}
|
|
37
|
+
status;
|
|
38
|
+
};
|
|
39
|
+
function assertSecureUrl(url) {
|
|
40
|
+
const parsed = URL.parse(url);
|
|
41
|
+
if (!parsed) throw new Error(`Not a URL: ${url}`);
|
|
42
|
+
const local = ["localhost", "127.0.0.1", "[::1]"].includes(parsed.hostname);
|
|
43
|
+
if (parsed.protocol !== "https:" && !(parsed.protocol === "http:" && local))
|
|
44
|
+
throw new Error("Use an https:// address; only this machine may use http://");
|
|
45
|
+
}
|
|
46
|
+
function createClient(config, fetchImpl = fetch) {
|
|
47
|
+
assertSecureUrl(config.url);
|
|
48
|
+
const base = `${config.url}/api/v1`;
|
|
49
|
+
async function request(method, route, body, stream = false) {
|
|
50
|
+
const response = await fetchImpl(`${base}${route}`, {
|
|
51
|
+
method,
|
|
52
|
+
// A redirect could carry the key or the message to another host.
|
|
53
|
+
redirect: "error",
|
|
54
|
+
headers: {
|
|
55
|
+
authorization: `Bearer ${config.key}`,
|
|
56
|
+
// Says it is a stream, so a server shutting down closes it at once rather than waiting.
|
|
57
|
+
...stream ? { accept: "text/event-stream" } : {},
|
|
58
|
+
...body === void 0 ? {} : { "content-type": "application/json" }
|
|
59
|
+
},
|
|
60
|
+
body: body === void 0 ? void 0 : JSON.stringify(body)
|
|
61
|
+
});
|
|
62
|
+
if (!response.ok) {
|
|
63
|
+
const payload = await response.json().catch(() => null);
|
|
64
|
+
throw new ApiError(response.status, payload?.error?.message ?? response.statusText);
|
|
65
|
+
}
|
|
66
|
+
return response;
|
|
67
|
+
}
|
|
68
|
+
const json = async (method, route, body) => await (await request(method, route, body)).json();
|
|
69
|
+
return {
|
|
70
|
+
bots: async () => (await json("GET", "/models")).data,
|
|
71
|
+
createTask: (input) => json("POST", "/tasks", input),
|
|
72
|
+
task: (id) => json("GET", `/tasks/${encodeURIComponent(id)}`),
|
|
73
|
+
sendToTask: (id, input) => json("POST", `/tasks/${encodeURIComponent(id)}/messages`, { input }),
|
|
74
|
+
/** The task each time it changes, until it settles. */
|
|
75
|
+
async *followTask(id) {
|
|
76
|
+
const response = await request(
|
|
77
|
+
"GET",
|
|
78
|
+
`/tasks/${encodeURIComponent(id)}/events`,
|
|
79
|
+
void 0,
|
|
80
|
+
true
|
|
81
|
+
);
|
|
82
|
+
if (!response.body) return;
|
|
83
|
+
for await (const event of readEvents(response.body)) {
|
|
84
|
+
if (event.event === "task") yield JSON.parse(event.data);
|
|
85
|
+
if (event.event === "error") {
|
|
86
|
+
const { error } = JSON.parse(event.data);
|
|
87
|
+
throw new ApiError(error?.status ?? 500, error?.message ?? "The stream stopped");
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
/** A chat turn's reply as it arrives. */
|
|
92
|
+
async *chat(bot, message) {
|
|
93
|
+
const response = await request(
|
|
94
|
+
"POST",
|
|
95
|
+
"/chat/completions",
|
|
96
|
+
{ model: bot, stream: true, messages: [{ role: "user", content: message }] },
|
|
97
|
+
true
|
|
98
|
+
);
|
|
99
|
+
if (!response.body) return;
|
|
100
|
+
for await (const event of readEvents(response.body)) {
|
|
101
|
+
if (event.data === "[DONE]") return;
|
|
102
|
+
const chunk = JSON.parse(event.data);
|
|
103
|
+
if (chunk.error) throw new ApiError(500, chunk.error.message ?? "The stream stopped");
|
|
104
|
+
const content = chunk.choices?.[0]?.delta?.content;
|
|
105
|
+
if (content) yield content;
|
|
106
|
+
if (chunk.choices?.[0]?.finish_reason === "length")
|
|
107
|
+
throw new ApiError(504, "The bot is still working; follow the task for the rest");
|
|
108
|
+
}
|
|
109
|
+
throw new ApiError(502, "The reply stopped before it finished");
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// src/config.ts
|
|
115
|
+
import { chmod, mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
116
|
+
import { homedir } from "node:os";
|
|
117
|
+
import path from "node:path";
|
|
118
|
+
function configPath(env = process.env) {
|
|
119
|
+
const base = env.XDG_CONFIG_HOME || path.join(homedir(), ".config");
|
|
120
|
+
return path.join(base, "rakazo", "config.json");
|
|
121
|
+
}
|
|
122
|
+
async function loadConfig(env = process.env) {
|
|
123
|
+
const fromEnv = (url2, key2) => ({ url: url2.replace(/\/+$/, ""), key: key2 });
|
|
124
|
+
if (env.RAKAZO_URL && env.RAKAZO_API_KEY) return fromEnv(env.RAKAZO_URL, env.RAKAZO_API_KEY);
|
|
125
|
+
let saved = {};
|
|
126
|
+
try {
|
|
127
|
+
saved = JSON.parse(await readFile(configPath(env), "utf8"));
|
|
128
|
+
} catch (error) {
|
|
129
|
+
if (error.code !== "ENOENT") throw error;
|
|
130
|
+
}
|
|
131
|
+
const url = env.RAKAZO_URL || saved.url;
|
|
132
|
+
const sameServer = !env.RAKAZO_URL || env.RAKAZO_URL.replace(/\/+$/, "") === saved.url;
|
|
133
|
+
const key = env.RAKAZO_API_KEY || (sameServer ? saved.key : void 0);
|
|
134
|
+
return url && key ? { url: url.replace(/\/+$/, ""), key } : null;
|
|
135
|
+
}
|
|
136
|
+
async function saveConfig(config, env = process.env) {
|
|
137
|
+
const file = configPath(env);
|
|
138
|
+
await mkdir(path.dirname(file), { recursive: true, mode: 448 });
|
|
139
|
+
await chmod(path.dirname(file), 448);
|
|
140
|
+
const fresh = `${file}.${process.pid}.tmp`;
|
|
141
|
+
await writeFile(fresh, `${JSON.stringify(config, null, 2)}
|
|
142
|
+
`, { mode: 384, flag: "wx" });
|
|
143
|
+
await rename(fresh, file);
|
|
144
|
+
return file;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// ../../packages/core/src/primitives/async.ts
|
|
148
|
+
async function abortableDelay(delayMs, signal) {
|
|
149
|
+
if (signal?.aborted) return;
|
|
150
|
+
await new Promise((resolve) => {
|
|
151
|
+
const finish = () => {
|
|
152
|
+
clearTimeout(timer);
|
|
153
|
+
signal?.removeEventListener("abort", finish);
|
|
154
|
+
resolve();
|
|
155
|
+
};
|
|
156
|
+
const timer = setTimeout(finish, delayMs);
|
|
157
|
+
signal?.addEventListener("abort", finish, { once: true });
|
|
158
|
+
if (signal?.aborted) finish();
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// src/wait.ts
|
|
163
|
+
var RECONNECT_DELAYS_MS = [1e3, 2e3, 4e3, 8e3, 16e3];
|
|
164
|
+
async function waitFor(client, task, delay = abortableDelay) {
|
|
165
|
+
let last = task;
|
|
166
|
+
for (let quickDrops = 0; last.status === "working"; ) {
|
|
167
|
+
const before = last.updated_at;
|
|
168
|
+
const openedAt = Date.now();
|
|
169
|
+
try {
|
|
170
|
+
for await (const next of client.followTask(task.id)) last = next;
|
|
171
|
+
} catch (error) {
|
|
172
|
+
if (error instanceof ApiError) throw error;
|
|
173
|
+
process.stderr.write(
|
|
174
|
+
`Reconnecting: ${error instanceof Error ? error.message : String(error)}
|
|
175
|
+
`
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
const quick = Date.now() - openedAt < 3e4 && last.updated_at === before;
|
|
179
|
+
quickDrops = quick ? quickDrops + 1 : 0;
|
|
180
|
+
if (last.status === "working" && quickDrops > 0) {
|
|
181
|
+
const wait = RECONNECT_DELAYS_MS[quickDrops - 1];
|
|
182
|
+
if (wait === void 0)
|
|
183
|
+
throw new Error(`Lost the connection while task ${task.id} was still working`);
|
|
184
|
+
await delay(wait);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return last;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// src/main.ts
|
|
191
|
+
var USAGE = `Usage: rakazo <command>
|
|
192
|
+
|
|
193
|
+
login [--url <server>] [--key <api key>] Save the server and API key
|
|
194
|
+
bots List the bots you can use
|
|
195
|
+
chat <bot> <message> Send a message and print the reply as it arrives
|
|
196
|
+
task create <bot> <message> [--wait] [--callback <https url>]
|
|
197
|
+
task get <task id>
|
|
198
|
+
task wait <task id> Wait until the task finishes or asks something
|
|
199
|
+
task send <task id> <message> [--wait]
|
|
200
|
+
|
|
201
|
+
<bot> is a bot's id or exact name. RAKAZO_URL and RAKAZO_API_KEY override saved login.
|
|
202
|
+
`;
|
|
203
|
+
function printTask(task, json) {
|
|
204
|
+
if (json) {
|
|
205
|
+
stdout.write(`${JSON.stringify(task, null, 2)}
|
|
206
|
+
`);
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
stdout.write(`${task.id} ${task.status}
|
|
210
|
+
`);
|
|
211
|
+
if (task.output) stdout.write(`
|
|
212
|
+
${task.output}
|
|
213
|
+
`);
|
|
214
|
+
if (task.question) stdout.write(`
|
|
215
|
+
${task.question}
|
|
216
|
+
`);
|
|
217
|
+
}
|
|
218
|
+
var ended = (task) => task.status === "failed" || task.status === "cancelled";
|
|
219
|
+
async function ask(question, hidden = false) {
|
|
220
|
+
const prompt = createInterface({ input: stdin, output: stdout, terminal: stdin.isTTY });
|
|
221
|
+
if (hidden) {
|
|
222
|
+
stdout.write(question);
|
|
223
|
+
const echo = prompt._writeToOutput;
|
|
224
|
+
prompt._writeToOutput = (text) => {
|
|
225
|
+
if (text.includes("\n")) echo.call(prompt, "\n");
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
try {
|
|
229
|
+
return (await prompt.question(hidden ? "" : question)).trim();
|
|
230
|
+
} finally {
|
|
231
|
+
prompt.close();
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
async function main(argv) {
|
|
235
|
+
const { values, positionals } = parseArgs({
|
|
236
|
+
args: argv,
|
|
237
|
+
allowPositionals: true,
|
|
238
|
+
options: {
|
|
239
|
+
url: { type: "string" },
|
|
240
|
+
key: { type: "string" },
|
|
241
|
+
wait: { type: "boolean", default: false },
|
|
242
|
+
callback: { type: "string" },
|
|
243
|
+
json: { type: "boolean", default: false },
|
|
244
|
+
help: { type: "boolean", short: "h", default: false }
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
const [command, ...rest] = positionals;
|
|
248
|
+
if (!command || values.help) {
|
|
249
|
+
stdout.write(USAGE);
|
|
250
|
+
return command || values.help ? 0 : 1;
|
|
251
|
+
}
|
|
252
|
+
if (command === "login") {
|
|
253
|
+
const url = values.url ?? await ask("Rakazo URL: ");
|
|
254
|
+
const key = values.key ?? await ask("API key: ", true);
|
|
255
|
+
if (!url || !key) throw new Error("Both a URL and an API key are needed");
|
|
256
|
+
const config2 = { url: url.replace(/\/+$/, ""), key };
|
|
257
|
+
const bots = await createClient(config2).bots();
|
|
258
|
+
const file = await saveConfig(config2);
|
|
259
|
+
stdout.write(
|
|
260
|
+
`Saved to ${file}. ${bots.length} bot${bots.length === 1 ? "" : "s"} available.
|
|
261
|
+
`
|
|
262
|
+
);
|
|
263
|
+
return 0;
|
|
264
|
+
}
|
|
265
|
+
const config = await loadConfig();
|
|
266
|
+
if (!config) throw new Error("Run `rakazo login` or set RAKAZO_URL and RAKAZO_API_KEY");
|
|
267
|
+
const client = createClient(config);
|
|
268
|
+
const [sub, first, ...words] = rest;
|
|
269
|
+
if (command === "bots") {
|
|
270
|
+
const bots = await client.bots();
|
|
271
|
+
if (values.json) stdout.write(`${JSON.stringify(bots, null, 2)}
|
|
272
|
+
`);
|
|
273
|
+
else for (const bot of bots) stdout.write(`${bot.name} (${bot.owned_by}) ${bot.id}
|
|
274
|
+
`);
|
|
275
|
+
return 0;
|
|
276
|
+
}
|
|
277
|
+
if (command === "chat") {
|
|
278
|
+
const [bot, ...message] = rest;
|
|
279
|
+
if (!bot || message.length === 0) throw new Error("Usage: rakazo chat <bot> <message>");
|
|
280
|
+
let reply = "";
|
|
281
|
+
for await (const text of client.chat(bot, message.join(" "))) {
|
|
282
|
+
if (values.json) reply += text;
|
|
283
|
+
else stdout.write(text);
|
|
284
|
+
}
|
|
285
|
+
stdout.write(values.json ? `${JSON.stringify({ reply })}
|
|
286
|
+
` : "\n");
|
|
287
|
+
return 0;
|
|
288
|
+
}
|
|
289
|
+
if (command === "task") {
|
|
290
|
+
if (sub === "create" && first && words.length) {
|
|
291
|
+
const created = await client.createTask({
|
|
292
|
+
bot: first,
|
|
293
|
+
input: words.join(" "),
|
|
294
|
+
callback_url: values.callback
|
|
295
|
+
});
|
|
296
|
+
const { callback_secret: secret } = created;
|
|
297
|
+
if (secret && !values.json) stdout.write(`Callback secret: ${secret}
|
|
298
|
+
`);
|
|
299
|
+
const task = values.wait ? await waitFor(client, created) : created;
|
|
300
|
+
printTask(secret ? { ...task, callback_secret: secret } : task, values.json);
|
|
301
|
+
return ended(task) ? 1 : 0;
|
|
302
|
+
}
|
|
303
|
+
if (sub === "get" && first) {
|
|
304
|
+
const task = await client.task(first);
|
|
305
|
+
printTask(task, values.json);
|
|
306
|
+
return ended(task) ? 1 : 0;
|
|
307
|
+
}
|
|
308
|
+
if (sub === "wait" && first) {
|
|
309
|
+
const task = await waitFor(client, await client.task(first));
|
|
310
|
+
printTask(task, values.json);
|
|
311
|
+
return ended(task) ? 1 : 0;
|
|
312
|
+
}
|
|
313
|
+
if (sub === "send" && first && words.length) {
|
|
314
|
+
let task = await client.sendToTask(first, words.join(" "));
|
|
315
|
+
if (values.wait) task = await waitFor(client, task);
|
|
316
|
+
printTask(task, values.json);
|
|
317
|
+
return ended(task) ? 1 : 0;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
stdout.write(USAGE);
|
|
321
|
+
return 1;
|
|
322
|
+
}
|
|
323
|
+
main(process.argv.slice(2)).then(
|
|
324
|
+
(code) => {
|
|
325
|
+
process.exitCode = code;
|
|
326
|
+
},
|
|
327
|
+
(error) => {
|
|
328
|
+
const message = error instanceof ApiError ? `${error.message} (${error.status})` : error instanceof Error ? error.message : String(error);
|
|
329
|
+
process.stderr.write(`rakazo: ${message}
|
|
330
|
+
`);
|
|
331
|
+
process.exitCode = 1;
|
|
332
|
+
}
|
|
333
|
+
);
|
|
334
|
+
export {
|
|
335
|
+
main
|
|
336
|
+
};
|