assistant-ui 0.0.124 → 0.0.125

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.
@@ -0,0 +1,150 @@
1
+ import { validateCloudUrl } from "./cloud-url";
2
+
3
+ export const CLOUD_CLI_RUN_PATH = "/api/cli/run";
4
+
5
+ export type CloudCliAction = { type: string; [key: string]: unknown };
6
+
7
+ export type CloudCliResponse = {
8
+ stdout?: string;
9
+ stderr?: string;
10
+ exitCode: number;
11
+ actions?: CloudCliAction[];
12
+ };
13
+
14
+ const SAFE = /^[A-Za-z0-9_@%+=:,./-]+$/;
15
+
16
+ export const joinCloudArgs = (args: readonly string[]): string =>
17
+ args
18
+ .map((arg) => (SAFE.test(arg) ? arg : `'${arg.replaceAll("'", `'\\''`)}'`))
19
+ .join(" ");
20
+
21
+ export const parseCloudCliResponse = (
22
+ value: unknown,
23
+ ): CloudCliResponse | null => {
24
+ if (!value || typeof value !== "object") return null;
25
+ const body = value as Record<string, unknown>;
26
+ const exitCode = body["exitCode"];
27
+ if (
28
+ typeof exitCode !== "number" ||
29
+ !Number.isInteger(exitCode) ||
30
+ exitCode < 0 ||
31
+ exitCode > 255
32
+ ) {
33
+ return null;
34
+ }
35
+ for (const key of ["stdout", "stderr"]) {
36
+ if (body[key] !== undefined && typeof body[key] !== "string") return null;
37
+ }
38
+ const actions = body["actions"];
39
+ if (
40
+ actions !== undefined &&
41
+ !(
42
+ Array.isArray(actions) &&
43
+ actions.every(
44
+ (action) =>
45
+ !!action &&
46
+ typeof action === "object" &&
47
+ typeof (action as CloudCliAction).type === "string",
48
+ )
49
+ )
50
+ ) {
51
+ return null;
52
+ }
53
+ return body as CloudCliResponse;
54
+ };
55
+
56
+ const cliVersion: string =
57
+ typeof __AUI_PACKAGE_VERSION__ === "string"
58
+ ? __AUI_PACKAGE_VERSION__
59
+ : "0.0.0";
60
+
61
+ export const sendCloudCommand = async (
62
+ cloudUrl: string,
63
+ args: readonly string[],
64
+ accessToken: string | null,
65
+ ): Promise<CloudCliResponse> => {
66
+ const url = `${validateCloudUrl(cloudUrl)}${CLOUD_CLI_RUN_PATH}`;
67
+ let response: Response;
68
+ try {
69
+ response = await fetch(url, {
70
+ method: "POST",
71
+ headers: {
72
+ "Content-Type": "application/json",
73
+ ...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}),
74
+ },
75
+ body: JSON.stringify({
76
+ command: joinCloudArgs(args),
77
+ client: {
78
+ name: "assistant-ui",
79
+ version: cliVersion,
80
+ platform: `${process.platform}-${process.arch}`,
81
+ },
82
+ }),
83
+ redirect: "error",
84
+ signal: AbortSignal.timeout(60_000),
85
+ });
86
+ } catch (error) {
87
+ const cause = (error as Error).cause as NodeJS.ErrnoException | undefined;
88
+ throw new Error(
89
+ `Could not reach ${url}: ${cause?.message || cause?.code || (error as Error).message}`,
90
+ );
91
+ }
92
+ const body = parseCloudCliResponse(await response.json().catch(() => null));
93
+ if (!body) {
94
+ throw new Error(
95
+ `Unexpected response from ${url} (${response.status} ${response.statusText}).`,
96
+ );
97
+ }
98
+ return body;
99
+ };
100
+
101
+ // A response naming an action this client lacks fails before printing, so the
102
+ // server can roll a new action out behind the client version it is sent.
103
+ const ACTION_HANDLERS: Record<
104
+ string,
105
+ (action: CloudCliAction) => Promise<number>
106
+ > = {};
107
+
108
+ export const runCloudCommand = async (
109
+ args: readonly string[],
110
+ deps: {
111
+ cloudUrl: string;
112
+ accessToken: () => Promise<string | null>;
113
+ stdout: (text: string) => void;
114
+ stderr: (text: string) => void;
115
+ send?: typeof sendCloudCommand;
116
+ },
117
+ ): Promise<number> => {
118
+ let response: CloudCliResponse;
119
+ try {
120
+ response = await (deps.send ?? sendCloudCommand)(
121
+ deps.cloudUrl,
122
+ args,
123
+ await deps.accessToken().catch((error: unknown) => {
124
+ deps.stderr(
125
+ `${error instanceof Error ? error.message : String(error)}\n`,
126
+ );
127
+ return null;
128
+ }),
129
+ );
130
+ } catch (error) {
131
+ deps.stderr(`${error instanceof Error ? error.message : String(error)}\n`);
132
+ return 1;
133
+ }
134
+ const unknown = response.actions?.find(
135
+ (action) => !Object.hasOwn(ACTION_HANDLERS, action.type),
136
+ );
137
+ if (unknown) {
138
+ deps.stderr(
139
+ `This command needs a newer assistant-ui (unsupported action "${unknown.type}"). Update the CLI and try again.\n`,
140
+ );
141
+ return 1;
142
+ }
143
+ if (response.stdout) deps.stdout(response.stdout);
144
+ if (response.stderr) deps.stderr(response.stderr);
145
+ for (const action of response.actions ?? []) {
146
+ const code = await ACTION_HANDLERS[action.type]!(action);
147
+ if (code !== 0) return code;
148
+ }
149
+ return response.exitCode;
150
+ };