ai-remote 0.4.14 → 0.4.16

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,51 @@
1
+ // src/cli/ipc.ts
2
+ import net from "node:net";
3
+ import { StringDecoder } from "node:string_decoder";
4
+ function readMessages(socket, onMessage) {
5
+ const decoder = new StringDecoder("utf8");
6
+ let buffer = "";
7
+ socket.on("data", (chunk) => {
8
+ buffer += decoder.write(chunk);
9
+ for (; ; ) {
10
+ const newline = buffer.indexOf("\n");
11
+ if (newline === -1) return;
12
+ const line = buffer.slice(0, newline);
13
+ buffer = buffer.slice(newline + 1);
14
+ if (!line.trim()) continue;
15
+ try {
16
+ onMessage(JSON.parse(line));
17
+ } catch {
18
+ }
19
+ }
20
+ });
21
+ }
22
+ var writeMessage = (socket, value) => {
23
+ socket.write(`${JSON.stringify(value)}
24
+ `);
25
+ };
26
+ function request(path, op, args = {}, timeoutMs = 12e4) {
27
+ return new Promise((resolve, reject) => {
28
+ const socket = net.connect(path);
29
+ let settled = false;
30
+ const finish = (error, response) => {
31
+ if (settled) return;
32
+ settled = true;
33
+ clearTimeout(timer);
34
+ socket.destroy();
35
+ if (error) reject(error);
36
+ else resolve(response);
37
+ };
38
+ const timer = setTimeout(() => finish(new Error(`The session did not answer "${op}" in time.`)), timeoutMs);
39
+ socket.on("connect", () => writeMessage(socket, { id: 1, op, args }));
40
+ socket.on("error", (error) => {
41
+ finish(Object.assign(error, { notRunning: error.code === "ENOENT" || error.code === "ECONNREFUSED" }));
42
+ });
43
+ readMessages(socket, (message) => finish(null, message));
44
+ });
45
+ }
46
+
47
+ export {
48
+ readMessages,
49
+ writeMessage,
50
+ request
51
+ };
@@ -0,0 +1,201 @@
1
+ // src/protocols/ssh/wire.ts
2
+ var textEncoder = new TextEncoder();
3
+ var textDecoder = new TextDecoder();
4
+ function encodeUtf8(text) {
5
+ return textEncoder.encode(text);
6
+ }
7
+ function decodeUtf8(bytes) {
8
+ return textDecoder.decode(bytes);
9
+ }
10
+ function concatBytes(...parts) {
11
+ const total = parts.reduce((sum, part) => sum + part.length, 0);
12
+ const out = new Uint8Array(total);
13
+ let offset = 0;
14
+ for (const part of parts) {
15
+ out.set(part, offset);
16
+ offset += part.length;
17
+ }
18
+ return out;
19
+ }
20
+ function toBase64(bytes) {
21
+ let binary = "";
22
+ for (const byte of bytes) binary += String.fromCharCode(byte);
23
+ return btoa(binary);
24
+ }
25
+ function fromBase64(text) {
26
+ const binary = atob(text);
27
+ const bytes = new Uint8Array(binary.length);
28
+ for (let index = 0; index < binary.length; index++) bytes[index] = binary.charCodeAt(index);
29
+ return bytes;
30
+ }
31
+ function toBase64Url(bytes) {
32
+ return toBase64(bytes).replaceAll("+", "-").replaceAll("/", "_").replaceAll("=", "");
33
+ }
34
+ var SshWriter = class {
35
+ bytes;
36
+ view;
37
+ offset = 0;
38
+ constructor(capacity = 256) {
39
+ this.bytes = new Uint8Array(new ArrayBuffer(capacity));
40
+ this.view = new DataView(this.bytes.buffer);
41
+ }
42
+ #ensure(extra) {
43
+ if (this.offset + extra <= this.bytes.length) return;
44
+ let capacity = Math.max(this.bytes.length * 2, 64);
45
+ while (capacity < this.offset + extra) capacity *= 2;
46
+ const grown = new Uint8Array(new ArrayBuffer(capacity));
47
+ grown.set(this.bytes.subarray(0, this.offset));
48
+ this.bytes = grown;
49
+ this.view = new DataView(grown.buffer);
50
+ }
51
+ u8(value) {
52
+ this.#ensure(1);
53
+ this.view.setUint8(this.offset, value);
54
+ this.offset += 1;
55
+ return this;
56
+ }
57
+ boolean(value) {
58
+ return this.u8(value ? 1 : 0);
59
+ }
60
+ u32(value) {
61
+ this.#ensure(4);
62
+ this.view.setUint32(this.offset, value >>> 0, false);
63
+ this.offset += 4;
64
+ return this;
65
+ }
66
+ /**
67
+ * A 64-bit unsigned integer, written from a JavaScript number.
68
+ *
69
+ * SFTP measures files and offsets in these. Written as two 32-bit halves
70
+ * rather than through a BigInt, because the values are byte counts that come
71
+ * from and go back to `number` at every other layer, and converting twice per
72
+ * chunk to satisfy a type is work with nothing to show for it. Above
73
+ * `Number.MAX_SAFE_INTEGER` -- eight petabytes -- this is wrong, and so is
74
+ * every other size in this codebase.
75
+ */
76
+ u64(value) {
77
+ const whole = Math.floor(value);
78
+ return this.u32(Math.floor(whole / 4294967296)).u32(whole % 4294967296);
79
+ }
80
+ raw(bytes) {
81
+ this.#ensure(bytes.length);
82
+ this.bytes.set(bytes, this.offset);
83
+ this.offset += bytes.length;
84
+ return this;
85
+ }
86
+ /** A length-prefixed string. Text is encoded as UTF-8. */
87
+ string(value) {
88
+ const bytes = typeof value === "string" ? encodeUtf8(value) : value;
89
+ return this.u32(bytes.length).raw(bytes);
90
+ }
91
+ nameList(names) {
92
+ return this.string(names.join(","));
93
+ }
94
+ /**
95
+ * An mpint: two's complement, big endian, with no leading zero bytes except
96
+ * the one that keeps a positive number from looking negative.
97
+ */
98
+ mpint(bytes) {
99
+ let start = 0;
100
+ while (start < bytes.length && bytes[start] === 0) start++;
101
+ const magnitude = bytes.subarray(start);
102
+ if (magnitude.length === 0) return this.u32(0);
103
+ if ((magnitude[0] ?? 0) & 128) {
104
+ this.u32(magnitude.length + 1).u8(0);
105
+ return this.raw(magnitude);
106
+ }
107
+ return this.u32(magnitude.length).raw(magnitude);
108
+ }
109
+ get length() {
110
+ return this.offset;
111
+ }
112
+ take() {
113
+ return this.bytes.slice(0, this.offset);
114
+ }
115
+ };
116
+ var SshReader = class {
117
+ bytes;
118
+ view;
119
+ offset;
120
+ constructor(bytes, offset = 0) {
121
+ this.bytes = bytes;
122
+ this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
123
+ this.offset = offset;
124
+ }
125
+ get remaining() {
126
+ return this.bytes.length - this.offset;
127
+ }
128
+ #need(count) {
129
+ if (count < 0 || this.remaining < count) {
130
+ throw new Error(`SSH: truncated packet (needed ${count} bytes, ${this.remaining} left)`);
131
+ }
132
+ }
133
+ u8() {
134
+ this.#need(1);
135
+ return this.view.getUint8(this.offset++);
136
+ }
137
+ boolean() {
138
+ return this.u8() !== 0;
139
+ }
140
+ u32() {
141
+ this.#need(4);
142
+ const value = this.view.getUint32(this.offset, false);
143
+ this.offset += 4;
144
+ return value;
145
+ }
146
+ /** A 64-bit unsigned integer as a number; see SshWriter#u64 on the range. */
147
+ u64() {
148
+ return this.u32() * 4294967296 + this.u32();
149
+ }
150
+ raw(count) {
151
+ this.#need(count);
152
+ const slice = this.bytes.subarray(this.offset, this.offset + count);
153
+ this.offset += count;
154
+ return slice;
155
+ }
156
+ /** The bytes of a length-prefixed string. */
157
+ stringBytes() {
158
+ return this.raw(this.u32());
159
+ }
160
+ string() {
161
+ return decodeUtf8(this.stringBytes());
162
+ }
163
+ nameList() {
164
+ const value = this.string();
165
+ return value ? value.split(",") : [];
166
+ }
167
+ /** An mpint's magnitude, with the sign byte removed. */
168
+ mpint() {
169
+ const bytes = this.stringBytes();
170
+ let start = 0;
171
+ while (start < bytes.length && bytes[start] === 0) start++;
172
+ return bytes.subarray(start);
173
+ }
174
+ skip(count) {
175
+ this.#need(count);
176
+ this.offset += count;
177
+ return this;
178
+ }
179
+ rest() {
180
+ return this.bytes.subarray(this.offset);
181
+ }
182
+ };
183
+ function padStart(bytes, width) {
184
+ if (bytes.length === width) return bytes;
185
+ if (bytes.length > width) return bytes.subarray(bytes.length - width);
186
+ const out = new Uint8Array(width);
187
+ out.set(bytes, width - bytes.length);
188
+ return out;
189
+ }
190
+
191
+ export {
192
+ encodeUtf8,
193
+ decodeUtf8,
194
+ concatBytes,
195
+ toBase64,
196
+ fromBase64,
197
+ toBase64Url,
198
+ SshWriter,
199
+ SshReader,
200
+ padStart
201
+ };