@schuttdev/gigai 0.1.0-beta.1 → 0.1.0-beta.10

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,288 @@
1
+ #!/usr/bin/env node
2
+
3
+ // ../shared/dist/index.mjs
4
+ import { randomBytes, createCipheriv, createDecipheriv } from "crypto";
5
+ import { z } from "zod";
6
+ var ALGORITHM = "aes-256-gcm";
7
+ var IV_LENGTH = 12;
8
+ var TAG_LENGTH = 16;
9
+ function encrypt(payload, key) {
10
+ const keyBuffer = Buffer.from(key, "hex");
11
+ if (keyBuffer.length !== 32) {
12
+ throw new Error("Encryption key must be 32 bytes (64 hex chars)");
13
+ }
14
+ const iv = randomBytes(IV_LENGTH);
15
+ const cipher = createCipheriv(ALGORITHM, keyBuffer, iv, {
16
+ authTagLength: TAG_LENGTH
17
+ });
18
+ const plaintext = JSON.stringify(payload);
19
+ const encrypted = Buffer.concat([
20
+ cipher.update(plaintext, "utf8"),
21
+ cipher.final()
22
+ ]);
23
+ const tag = cipher.getAuthTag();
24
+ return {
25
+ iv: iv.toString("base64"),
26
+ ciphertext: encrypted.toString("base64"),
27
+ tag: tag.toString("base64")
28
+ };
29
+ }
30
+ function decrypt(encrypted, key) {
31
+ const keyBuffer = Buffer.from(key, "hex");
32
+ if (keyBuffer.length !== 32) {
33
+ throw new Error("Encryption key must be 32 bytes (64 hex chars)");
34
+ }
35
+ const iv = Buffer.from(encrypted.iv, "base64");
36
+ const ciphertext = Buffer.from(encrypted.ciphertext, "base64");
37
+ const tag = Buffer.from(encrypted.tag, "base64");
38
+ const decipher = createDecipheriv(ALGORITHM, keyBuffer, iv, {
39
+ authTagLength: TAG_LENGTH
40
+ });
41
+ decipher.setAuthTag(tag);
42
+ const decrypted = Buffer.concat([
43
+ decipher.update(ciphertext),
44
+ decipher.final()
45
+ ]);
46
+ return JSON.parse(decrypted.toString("utf8"));
47
+ }
48
+ function generateEncryptionKey() {
49
+ return randomBytes(32).toString("hex");
50
+ }
51
+ var ErrorCode = /* @__PURE__ */ ((ErrorCode2) => {
52
+ ErrorCode2["PAIRING_EXPIRED"] = "PAIRING_EXPIRED";
53
+ ErrorCode2["PAIRING_INVALID"] = "PAIRING_INVALID";
54
+ ErrorCode2["PAIRING_USED"] = "PAIRING_USED";
55
+ ErrorCode2["TOKEN_INVALID"] = "TOKEN_INVALID";
56
+ ErrorCode2["TOKEN_DECRYPT_FAILED"] = "TOKEN_DECRYPT_FAILED";
57
+ ErrorCode2["ORG_MISMATCH"] = "ORG_MISMATCH";
58
+ ErrorCode2["SESSION_EXPIRED"] = "SESSION_EXPIRED";
59
+ ErrorCode2["SESSION_INVALID"] = "SESSION_INVALID";
60
+ ErrorCode2["AUTH_REQUIRED"] = "AUTH_REQUIRED";
61
+ ErrorCode2["TOOL_NOT_FOUND"] = "TOOL_NOT_FOUND";
62
+ ErrorCode2["EXEC_TIMEOUT"] = "EXEC_TIMEOUT";
63
+ ErrorCode2["EXEC_FAILED"] = "EXEC_FAILED";
64
+ ErrorCode2["HTTPS_REQUIRED"] = "HTTPS_REQUIRED";
65
+ ErrorCode2["RATE_LIMITED"] = "RATE_LIMITED";
66
+ ErrorCode2["VALIDATION_ERROR"] = "VALIDATION_ERROR";
67
+ ErrorCode2["INTERNAL_ERROR"] = "INTERNAL_ERROR";
68
+ ErrorCode2["MCP_ERROR"] = "MCP_ERROR";
69
+ ErrorCode2["MCP_NOT_CONNECTED"] = "MCP_NOT_CONNECTED";
70
+ ErrorCode2["TRANSFER_NOT_FOUND"] = "TRANSFER_NOT_FOUND";
71
+ ErrorCode2["TRANSFER_EXPIRED"] = "TRANSFER_EXPIRED";
72
+ ErrorCode2["PATH_NOT_ALLOWED"] = "PATH_NOT_ALLOWED";
73
+ ErrorCode2["COMMAND_NOT_ALLOWED"] = "COMMAND_NOT_ALLOWED";
74
+ return ErrorCode2;
75
+ })(ErrorCode || {});
76
+ var STATUS_CODES = {
77
+ [
78
+ "PAIRING_EXPIRED"
79
+ /* PAIRING_EXPIRED */
80
+ ]: 410,
81
+ [
82
+ "PAIRING_INVALID"
83
+ /* PAIRING_INVALID */
84
+ ]: 400,
85
+ [
86
+ "PAIRING_USED"
87
+ /* PAIRING_USED */
88
+ ]: 409,
89
+ [
90
+ "TOKEN_INVALID"
91
+ /* TOKEN_INVALID */
92
+ ]: 401,
93
+ [
94
+ "TOKEN_DECRYPT_FAILED"
95
+ /* TOKEN_DECRYPT_FAILED */
96
+ ]: 401,
97
+ [
98
+ "ORG_MISMATCH"
99
+ /* ORG_MISMATCH */
100
+ ]: 403,
101
+ [
102
+ "SESSION_EXPIRED"
103
+ /* SESSION_EXPIRED */
104
+ ]: 401,
105
+ [
106
+ "SESSION_INVALID"
107
+ /* SESSION_INVALID */
108
+ ]: 401,
109
+ [
110
+ "AUTH_REQUIRED"
111
+ /* AUTH_REQUIRED */
112
+ ]: 401,
113
+ [
114
+ "TOOL_NOT_FOUND"
115
+ /* TOOL_NOT_FOUND */
116
+ ]: 404,
117
+ [
118
+ "EXEC_TIMEOUT"
119
+ /* EXEC_TIMEOUT */
120
+ ]: 408,
121
+ [
122
+ "EXEC_FAILED"
123
+ /* EXEC_FAILED */
124
+ ]: 500,
125
+ [
126
+ "HTTPS_REQUIRED"
127
+ /* HTTPS_REQUIRED */
128
+ ]: 403,
129
+ [
130
+ "RATE_LIMITED"
131
+ /* RATE_LIMITED */
132
+ ]: 429,
133
+ [
134
+ "VALIDATION_ERROR"
135
+ /* VALIDATION_ERROR */
136
+ ]: 400,
137
+ [
138
+ "INTERNAL_ERROR"
139
+ /* INTERNAL_ERROR */
140
+ ]: 500,
141
+ [
142
+ "MCP_ERROR"
143
+ /* MCP_ERROR */
144
+ ]: 502,
145
+ [
146
+ "MCP_NOT_CONNECTED"
147
+ /* MCP_NOT_CONNECTED */
148
+ ]: 503,
149
+ [
150
+ "TRANSFER_NOT_FOUND"
151
+ /* TRANSFER_NOT_FOUND */
152
+ ]: 404,
153
+ [
154
+ "TRANSFER_EXPIRED"
155
+ /* TRANSFER_EXPIRED */
156
+ ]: 410,
157
+ [
158
+ "PATH_NOT_ALLOWED"
159
+ /* PATH_NOT_ALLOWED */
160
+ ]: 403,
161
+ [
162
+ "COMMAND_NOT_ALLOWED"
163
+ /* COMMAND_NOT_ALLOWED */
164
+ ]: 403
165
+ };
166
+ var GigaiError = class extends Error {
167
+ code;
168
+ statusCode;
169
+ details;
170
+ constructor(code, message, details) {
171
+ super(message);
172
+ this.name = "GigaiError";
173
+ this.code = code;
174
+ this.statusCode = STATUS_CODES[code];
175
+ this.details = details;
176
+ }
177
+ toJSON() {
178
+ return {
179
+ error: {
180
+ code: this.code,
181
+ message: this.message,
182
+ ...this.details !== void 0 && { details: this.details }
183
+ }
184
+ };
185
+ }
186
+ };
187
+ var TailscaleHttpsConfigSchema = z.object({
188
+ provider: z.literal("tailscale"),
189
+ funnelPort: z.number().optional()
190
+ });
191
+ var CloudflareHttpsConfigSchema = z.object({
192
+ provider: z.literal("cloudflare"),
193
+ tunnelName: z.string(),
194
+ domain: z.string().optional()
195
+ });
196
+ var LetsEncryptHttpsConfigSchema = z.object({
197
+ provider: z.literal("letsencrypt"),
198
+ domain: z.string(),
199
+ email: z.string().email(),
200
+ certPath: z.string().optional(),
201
+ keyPath: z.string().optional()
202
+ });
203
+ var ManualHttpsConfigSchema = z.object({
204
+ provider: z.literal("manual"),
205
+ certPath: z.string(),
206
+ keyPath: z.string()
207
+ });
208
+ var HttpsConfigSchema = z.discriminatedUnion("provider", [
209
+ TailscaleHttpsConfigSchema,
210
+ CloudflareHttpsConfigSchema,
211
+ LetsEncryptHttpsConfigSchema,
212
+ ManualHttpsConfigSchema
213
+ ]);
214
+ var CliToolConfigSchema = z.object({
215
+ type: z.literal("cli"),
216
+ name: z.string(),
217
+ command: z.string(),
218
+ args: z.array(z.string()).optional(),
219
+ description: z.string(),
220
+ timeout: z.number().optional(),
221
+ cwd: z.string().optional(),
222
+ env: z.record(z.string()).optional()
223
+ });
224
+ var McpToolConfigSchema = z.object({
225
+ type: z.literal("mcp"),
226
+ name: z.string(),
227
+ command: z.string(),
228
+ args: z.array(z.string()).optional(),
229
+ description: z.string(),
230
+ env: z.record(z.string()).optional()
231
+ });
232
+ var ScriptToolConfigSchema = z.object({
233
+ type: z.literal("script"),
234
+ name: z.string(),
235
+ path: z.string(),
236
+ description: z.string(),
237
+ timeout: z.number().optional(),
238
+ interpreter: z.string().optional()
239
+ });
240
+ var BuiltinToolConfigSchema = z.object({
241
+ type: z.literal("builtin"),
242
+ name: z.string(),
243
+ builtin: z.enum(["filesystem", "shell"]),
244
+ description: z.string(),
245
+ config: z.record(z.unknown()).optional()
246
+ });
247
+ var ToolConfigSchema = z.discriminatedUnion("type", [
248
+ CliToolConfigSchema,
249
+ McpToolConfigSchema,
250
+ ScriptToolConfigSchema,
251
+ BuiltinToolConfigSchema
252
+ ]);
253
+ var AuthConfigSchema = z.object({
254
+ encryptionKey: z.string().length(64),
255
+ pairingTtlSeconds: z.number().default(300),
256
+ sessionTtlSeconds: z.number().default(14400)
257
+ });
258
+ var ServerConfigSchema = z.object({
259
+ port: z.number().default(7443),
260
+ host: z.string().default("0.0.0.0"),
261
+ https: HttpsConfigSchema.optional()
262
+ });
263
+ var GigaiConfigSchema = z.object({
264
+ server: ServerConfigSchema,
265
+ auth: AuthConfigSchema,
266
+ tools: z.array(ToolConfigSchema).default([])
267
+ });
268
+ function decodeJWTPayload(token) {
269
+ const parts = token.split(".");
270
+ if (parts.length !== 3) {
271
+ throw new Error("Invalid JWT format: expected 3 segments");
272
+ }
273
+ const payload = parts[1];
274
+ const base64 = payload.replace(/-/g, "+").replace(/_/g, "/");
275
+ const padded = base64 + "=".repeat((4 - base64.length % 4) % 4);
276
+ const decoded = Buffer.from(padded, "base64").toString("utf8");
277
+ return JSON.parse(decoded);
278
+ }
279
+
280
+ export {
281
+ encrypt,
282
+ decrypt,
283
+ generateEncryptionKey,
284
+ ErrorCode,
285
+ GigaiError,
286
+ GigaiConfigSchema,
287
+ decodeJWTPayload
288
+ };
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ ErrorCode,
4
+ GigaiError,
5
+ encrypt
6
+ } from "./chunk-4XUWD3DZ.js";
7
+
8
+ // ../server/dist/chunk-54TEF6CS.mjs
9
+ import { nanoid } from "nanoid";
10
+ var PAIRING_CODE_LENGTH = 8;
11
+ var PAIRING_CODE_CHARS = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ";
12
+ function generatePairingCode(store, ttlSeconds) {
13
+ let code = "";
14
+ const bytes = nanoid(PAIRING_CODE_LENGTH);
15
+ for (let i = 0; i < PAIRING_CODE_LENGTH; i++) {
16
+ code += PAIRING_CODE_CHARS[bytes.charCodeAt(i) % PAIRING_CODE_CHARS.length];
17
+ }
18
+ store.addPairingCode(code, ttlSeconds);
19
+ return code;
20
+ }
21
+ function validateAndPair(store, code, orgUuid, encryptionKey, serverFingerprint) {
22
+ const entry = store.getPairingCode(code.toUpperCase());
23
+ if (!entry) {
24
+ throw new GigaiError(ErrorCode.PAIRING_INVALID, "Invalid pairing code");
25
+ }
26
+ if (entry.used) {
27
+ throw new GigaiError(ErrorCode.PAIRING_USED, "Pairing code already used");
28
+ }
29
+ if (entry.expiresAt < Date.now()) {
30
+ throw new GigaiError(ErrorCode.PAIRING_EXPIRED, "Pairing code expired");
31
+ }
32
+ store.markPairingCodeUsed(code.toUpperCase());
33
+ return encrypt(
34
+ { orgUuid, serverFingerprint, createdAt: Date.now() },
35
+ encryptionKey
36
+ );
37
+ }
38
+
39
+ export {
40
+ generatePairingCode,
41
+ validateAndPair
42
+ };
@@ -0,0 +1,82 @@
1
+ #!/usr/bin/env node
2
+
3
+ // ../server/dist/chunk-KF32K7J3.mjs
4
+ import { nanoid } from "nanoid";
5
+ var AuthStore = class {
6
+ pairingCodes = /* @__PURE__ */ new Map();
7
+ sessions = /* @__PURE__ */ new Map();
8
+ boundOrgs = /* @__PURE__ */ new Map();
9
+ // serverFingerprint -> orgUuid
10
+ cleanupInterval;
11
+ constructor() {
12
+ this.cleanupInterval = setInterval(() => this.cleanup(), 6e4);
13
+ }
14
+ // Pairing codes
15
+ addPairingCode(code, ttlSeconds) {
16
+ const entry = {
17
+ code,
18
+ expiresAt: Date.now() + ttlSeconds * 1e3,
19
+ used: false
20
+ };
21
+ this.pairingCodes.set(code, entry);
22
+ return entry;
23
+ }
24
+ getPairingCode(code) {
25
+ return this.pairingCodes.get(code);
26
+ }
27
+ markPairingCodeUsed(code) {
28
+ const entry = this.pairingCodes.get(code);
29
+ if (entry) {
30
+ entry.used = true;
31
+ }
32
+ }
33
+ // Sessions
34
+ createSession(orgUuid, ttlSeconds) {
35
+ const session = {
36
+ id: nanoid(32),
37
+ orgUuid,
38
+ token: nanoid(48),
39
+ expiresAt: Date.now() + ttlSeconds * 1e3,
40
+ lastActivity: Date.now()
41
+ };
42
+ this.sessions.set(session.token, session);
43
+ return session;
44
+ }
45
+ getSession(token) {
46
+ const session = this.sessions.get(token);
47
+ if (session) {
48
+ session.lastActivity = Date.now();
49
+ }
50
+ return session;
51
+ }
52
+ // Org binding (bind-on-first-use for wildcard tokens)
53
+ bindOrg(fingerprint, orgUuid) {
54
+ this.boundOrgs.set(fingerprint, orgUuid);
55
+ }
56
+ getBoundOrg(fingerprint) {
57
+ return this.boundOrgs.get(fingerprint);
58
+ }
59
+ // Cleanup
60
+ cleanup() {
61
+ const now = Date.now();
62
+ for (const [key, code] of this.pairingCodes) {
63
+ if (code.expiresAt < now) {
64
+ this.pairingCodes.delete(key);
65
+ }
66
+ }
67
+ for (const [key, session] of this.sessions) {
68
+ if (session.expiresAt < now) {
69
+ this.sessions.delete(key);
70
+ }
71
+ }
72
+ }
73
+ destroy() {
74
+ clearInterval(this.cleanupInterval);
75
+ this.pairingCodes.clear();
76
+ this.sessions.clear();
77
+ }
78
+ };
79
+
80
+ export {
81
+ AuthStore
82
+ };