@usecontextlayer/cli 0.16.2 → 0.18.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 +4 -4
- package/dist/auth-BL9AxZGb.mjs +1214 -0
- package/dist/auth-BL9AxZGb.mjs.map +1 -0
- package/dist/auth.d.mts +17 -2
- package/dist/auth.d.mts.map +1 -1
- package/dist/auth.mjs +1 -2
- package/dist/cli.mjs +173 -217
- package/dist/cli.mjs.map +1 -1
- package/dist/database-codegen-CxDtyvam.mjs +24 -0
- package/dist/database-codegen-CxDtyvam.mjs.map +1 -0
- package/dist/database-dialect-BLWWgYGo.mjs +56 -0
- package/dist/database-dialect-BLWWgYGo.mjs.map +1 -0
- package/dist/database.d.mts +35 -0
- package/dist/database.d.mts.map +1 -0
- package/dist/database.mjs +32 -0
- package/dist/database.mjs.map +1 -0
- package/dist/databases-SvX3IgqC.mjs +23 -0
- package/dist/databases-SvX3IgqC.mjs.map +1 -0
- package/dist/env-C_tllJ4u.mjs +98 -0
- package/dist/env-C_tllJ4u.mjs.map +1 -0
- package/dist/local-databases-BeXOeSs-.mjs +53 -0
- package/dist/local-databases-BeXOeSs-.mjs.map +1 -0
- package/dist/local-databases-C5qUZcUH.mjs +1 -0
- package/dist/{resources--XCCMeP6.mjs → resources-0GJALfam.mjs} +28 -180
- package/dist/resources-0GJALfam.mjs.map +1 -0
- package/dist/{auth-DOsE8GwJ.mjs → schemas-D9UReYiT.mjs} +37 -1212
- package/dist/schemas-D9UReYiT.mjs.map +1 -0
- package/dist/schemas-DeaNWrS-.d.mts +2213 -0
- package/dist/schemas-DeaNWrS-.d.mts.map +1 -0
- package/dist/template.tar.gz +0 -0
- package/dist/vite.d.mts +8 -4333
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +16 -67
- package/dist/vite.mjs.map +1 -1
- package/package.json +12 -7
- package/dist/auth-DOsE8GwJ.mjs.map +0 -1
- package/dist/resources--XCCMeP6.mjs.map +0 -1
- package/dist/rolldown-runtime-EAsCxrDo.mjs +0 -14
- package/dist/template/AGENTS.md +0 -13
- package/dist/template/README.md +0 -46
- package/dist/template/app/root.tsx +0 -66
- package/dist/template/app/routes/home.tsx +0 -25
- package/dist/template/databases/updates/drizzle.config.ts +0 -8
- package/dist/template/databases/updates/migrations/0000_create_updates.sql +0 -6
- package/dist/template/databases/updates/migrations/meta/0000_snapshot.json +0 -14
- package/dist/template/databases/updates/migrations/meta/_journal.json +0 -13
- package/dist/template/databases/updates/migrations/migrations.js +0 -10
- package/dist/template/databases/updates/schema.ts +0 -8
- package/dist/template/public/slate.json +0 -4
- package/dist/template/vite.config.ts +0 -9
- package/dist/template/workers/app.ts +0 -8
- package/dist/template/workers/local-resources/index.ts +0 -6
- package/dist/template/workers/local-resources/wrangler.jsonc +0 -19
|
@@ -1,21 +1,23 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as boolean, c as literal, d as record, g as uuid, o as discriminatedUnion, p as string, u as object } from "./schemas-D9UReYiT.mjs";
|
|
2
|
+
import { c as resourcesSchema, i as collectSlateResources, o as declarationSchema, r as readLocalSecret } from "./env-C_tllJ4u.mjs";
|
|
2
3
|
import { glob, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
3
|
-
import { constants } from "node:os";
|
|
4
4
|
import path, { join, resolve } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
5
6
|
import { EventEmitter, addAbortListener, on, once, setMaxListeners } from "node:events";
|
|
6
7
|
import { ChildProcess, execFile, spawn, spawnSync } from "node:child_process";
|
|
7
8
|
import fs, { appendFileSync, closeSync, createReadStream, createWriteStream, openSync, readFileSync, readSync, statSync, writeFileSync } from "node:fs";
|
|
8
9
|
import process$1, { execArgv, execPath, hrtime, platform } from "node:process";
|
|
9
10
|
import { aborted, callbackify, debuglog, inspect, promisify, stripVTControlCharacters } from "node:util";
|
|
10
|
-
import { fileURLToPath } from "node:url";
|
|
11
11
|
import { StringDecoder } from "node:string_decoder";
|
|
12
12
|
import tty from "node:tty";
|
|
13
13
|
import { scheduler, setImmediate, setTimeout } from "node:timers/promises";
|
|
14
|
+
import { constants } from "node:os";
|
|
14
15
|
import { serialize } from "node:v8";
|
|
15
16
|
import { Buffer } from "node:buffer";
|
|
16
17
|
import { finished } from "node:stream/promises";
|
|
17
18
|
import { Duplex, PassThrough, Readable, Transform, Writable, getDefaultHighWaterMark } from "node:stream";
|
|
18
19
|
import path$1 from "node:path/win32";
|
|
20
|
+
import { createHash } from "node:crypto";
|
|
19
21
|
//#region ../../node_modules/.pnpm/is-plain-obj@4.1.0/node_modules/is-plain-obj/index.js
|
|
20
22
|
function isPlainObject(value) {
|
|
21
23
|
if (typeof value !== "object" || value === null) return false;
|
|
@@ -6350,132 +6352,6 @@ createExeca(mapNode);
|
|
|
6350
6352
|
createExeca(mapScriptAsync, {}, deepScriptOptions, setScriptSync);
|
|
6351
6353
|
const { sendMessage, getOneMessage, getEachMessage, getCancelSignal } = getIpcExport();
|
|
6352
6354
|
//#endregion
|
|
6353
|
-
//#region ../shared/dist/databases.mjs
|
|
6354
|
-
const databaseJournalSchema = object({ entries: array(object({
|
|
6355
|
-
breakpoints: boolean(),
|
|
6356
|
-
idx: number().int().nonnegative(),
|
|
6357
|
-
tag: string().min(1),
|
|
6358
|
-
when: number()
|
|
6359
|
-
})) });
|
|
6360
|
-
const databaseMigrationsSchema = object({
|
|
6361
|
-
journal: databaseJournalSchema,
|
|
6362
|
-
migrations: record(string(), string())
|
|
6363
|
-
});
|
|
6364
|
-
async function readDatabaseMigrations(slug, read) {
|
|
6365
|
-
const directory = `databases/${slug}/migrations`;
|
|
6366
|
-
const contents = await read(`${directory}/meta/_journal.json`);
|
|
6367
|
-
if (contents === null) return {
|
|
6368
|
-
journal: { entries: [] },
|
|
6369
|
-
migrations: {}
|
|
6370
|
-
};
|
|
6371
|
-
const journal = databaseJournalSchema.parse(JSON.parse(contents));
|
|
6372
|
-
const migrations = await Promise.all(journal.entries.map(async (entry) => {
|
|
6373
|
-
const sql = await read(`${directory}/${entry.tag}.sql`);
|
|
6374
|
-
if (sql === null) throw new Error(`Missing database migration: ${slug}/${entry.tag}.sql`);
|
|
6375
|
-
return [`m${entry.idx.toString().padStart(4, "0")}`, sql];
|
|
6376
|
-
}));
|
|
6377
|
-
return {
|
|
6378
|
-
journal,
|
|
6379
|
-
migrations: Object.fromEntries(migrations)
|
|
6380
|
-
};
|
|
6381
|
-
}
|
|
6382
|
-
const sqlValueSchema = union([
|
|
6383
|
-
string(),
|
|
6384
|
-
number(),
|
|
6385
|
-
_null()
|
|
6386
|
-
]);
|
|
6387
|
-
const databaseQuerySchema = strictObject({
|
|
6388
|
-
params: array(sqlValueSchema).default([]),
|
|
6389
|
-
slug: string().min(1),
|
|
6390
|
-
sql: string().min(1)
|
|
6391
|
-
});
|
|
6392
|
-
const databaseResultSchema = object({
|
|
6393
|
-
columns: array(string()),
|
|
6394
|
-
rows: array(array(sqlValueSchema)),
|
|
6395
|
-
rowsRead: number(),
|
|
6396
|
-
rowsWritten: number()
|
|
6397
|
-
});
|
|
6398
|
-
//#endregion
|
|
6399
|
-
//#region ../shared/dist/declaration.mjs
|
|
6400
|
-
const credentialModeSchema = _enum(["viewer", "owner"]);
|
|
6401
|
-
const connectionRequirementSchema = strictObject({
|
|
6402
|
-
mode: credentialModeSchema,
|
|
6403
|
-
toolkit: string().min(1)
|
|
6404
|
-
});
|
|
6405
|
-
const secretNameSchema = string().regex(/^[A-Z_][A-Z0-9_]*$/);
|
|
6406
|
-
const secretRequirementSchema = strictObject({ mode: credentialModeSchema });
|
|
6407
|
-
const databaseRequirementSchema = strictObject({ mode: credentialModeSchema });
|
|
6408
|
-
const databaseSlugSchema = string().min(1).regex(/^(?!\.{1,2}$)[^/\\]+$/);
|
|
6409
|
-
const declarationSchema = strictObject({
|
|
6410
|
-
connections: record(string().min(1), connectionRequirementSchema),
|
|
6411
|
-
databases: record(databaseSlugSchema, databaseRequirementSchema).default({}),
|
|
6412
|
-
secrets: record(secretNameSchema, secretRequirementSchema).default({})
|
|
6413
|
-
});
|
|
6414
|
-
strictObject({
|
|
6415
|
-
agents: record(string().min(1), declarationSchema),
|
|
6416
|
-
app: declarationSchema
|
|
6417
|
-
});
|
|
6418
|
-
const resourceConsumerSchema = discriminatedUnion("type", [strictObject({ type: literal("app") }), strictObject({
|
|
6419
|
-
name: string().min(1),
|
|
6420
|
-
type: literal("agent")
|
|
6421
|
-
})]);
|
|
6422
|
-
const resourcesSchema = declarationSchema.extend({
|
|
6423
|
-
connections: record(string().min(1), connectionRequirementSchema.extend({ consumers: array(resourceConsumerSchema) })),
|
|
6424
|
-
databases: record(databaseSlugSchema, databaseRequirementSchema.extend({ consumers: array(resourceConsumerSchema) })),
|
|
6425
|
-
secrets: record(secretNameSchema, secretRequirementSchema.extend({ consumers: array(resourceConsumerSchema) }))
|
|
6426
|
-
});
|
|
6427
|
-
function collectSlateResources(slateDeclarations) {
|
|
6428
|
-
const declarations = [{
|
|
6429
|
-
consumer: { type: "app" },
|
|
6430
|
-
declaration: slateDeclarations.app
|
|
6431
|
-
}, ...Object.entries(slateDeclarations.agents).map(([name, declaration]) => ({
|
|
6432
|
-
consumer: {
|
|
6433
|
-
name,
|
|
6434
|
-
type: "agent"
|
|
6435
|
-
},
|
|
6436
|
-
declaration
|
|
6437
|
-
}))];
|
|
6438
|
-
return {
|
|
6439
|
-
connections: collectRequirements("connections", declarations, (declaration) => declaration.connections),
|
|
6440
|
-
databases: collectRequirements("databases", declarations, (declaration) => declaration.databases),
|
|
6441
|
-
secrets: collectRequirements("secrets", declarations, (declaration) => declaration.secrets)
|
|
6442
|
-
};
|
|
6443
|
-
}
|
|
6444
|
-
function resourcesForConsumer(resources, consumer) {
|
|
6445
|
-
return {
|
|
6446
|
-
connections: filterRequirements(resources.connections, consumer),
|
|
6447
|
-
databases: filterRequirements(resources.databases, consumer),
|
|
6448
|
-
secrets: filterRequirements(resources.secrets, consumer)
|
|
6449
|
-
};
|
|
6450
|
-
}
|
|
6451
|
-
function filterRequirements(requirements, consumer) {
|
|
6452
|
-
return Object.fromEntries(Object.entries(requirements).filter(([, requirement]) => requirement.consumers.some((declared) => declared.type === consumer.type && (consumer.type === "app" || declared.type === "agent" && declared.name === consumer.name))));
|
|
6453
|
-
}
|
|
6454
|
-
function collectRequirements(category, declarations, select) {
|
|
6455
|
-
const resources = /* @__PURE__ */ new Map();
|
|
6456
|
-
for (const { consumer, declaration } of declarations) for (const [key, requirement] of Object.entries(select(declaration))) {
|
|
6457
|
-
const existing = resources.get(key);
|
|
6458
|
-
if (!existing) {
|
|
6459
|
-
resources.set(key, {
|
|
6460
|
-
...requirement,
|
|
6461
|
-
consumers: [consumer]
|
|
6462
|
-
});
|
|
6463
|
-
continue;
|
|
6464
|
-
}
|
|
6465
|
-
if (existing.mode !== requirement.mode || existing.toolkit !== requirement.toolkit) throw new ZodError([{
|
|
6466
|
-
code: "custom",
|
|
6467
|
-
message: `Conflicting ${category} requirement "${key}": declarations must agree on mode${category === "connections" ? " and toolkit" : ""}`,
|
|
6468
|
-
path: [
|
|
6469
|
-
...consumer.type === "app" ? ["app"] : ["agents", consumer.name],
|
|
6470
|
-
category,
|
|
6471
|
-
key
|
|
6472
|
-
]
|
|
6473
|
-
}]);
|
|
6474
|
-
existing.consumers.push(consumer);
|
|
6475
|
-
}
|
|
6476
|
-
return Object.fromEntries(resources);
|
|
6477
|
-
}
|
|
6478
|
-
//#endregion
|
|
6479
6355
|
//#region ../shared/dist/slate-usage.mjs
|
|
6480
6356
|
const ownerSchema = discriminatedUnion("type", [object({
|
|
6481
6357
|
id: string().min(1),
|
|
@@ -6493,52 +6369,30 @@ object({
|
|
|
6493
6369
|
const noCommitSchema = object({ reason: literal("no-commit") });
|
|
6494
6370
|
const slateResourceConfigurationSchema = resourcesSchema.extend({
|
|
6495
6371
|
connections: record(resourcesSchema.shape.connections.keyType, resourcesSchema.shape.connections.valueType.extend({
|
|
6496
|
-
|
|
6372
|
+
account_id: string().nullable(),
|
|
6497
6373
|
owner: ownerSchema
|
|
6498
6374
|
})),
|
|
6499
6375
|
databases: record(resourcesSchema.shape.databases.keyType, resourcesSchema.shape.databases.valueType.extend({
|
|
6500
6376
|
instance_name: string().min(1),
|
|
6501
6377
|
owner: ownerSchema
|
|
6502
6378
|
})),
|
|
6503
|
-
secrets: record(resourcesSchema.shape.secrets.keyType, resourcesSchema.shape.secrets.valueType.extend({
|
|
6379
|
+
secrets: record(resourcesSchema.shape.secrets.keyType, discriminatedUnion("mode", [resourcesSchema.shape.secrets.valueType.extend({
|
|
6380
|
+
mode: literal("owner"),
|
|
6381
|
+
owner: ownerSchema
|
|
6382
|
+
}), resourcesSchema.shape.secrets.valueType.extend({
|
|
6383
|
+
mode: literal("viewer"),
|
|
6384
|
+
owner: ownerSchema,
|
|
6385
|
+
supplied: boolean()
|
|
6386
|
+
})]))
|
|
6504
6387
|
});
|
|
6505
6388
|
object({ resources: slateResourceConfigurationSchema });
|
|
6506
6389
|
const localResourceConfigurationSchema = resourcesSchema.extend({
|
|
6507
|
-
connections: record(resourcesSchema.shape.connections.keyType, resourcesSchema.shape.connections.valueType.extend({
|
|
6508
|
-
databases: record(resourcesSchema.shape.databases.keyType, resourcesSchema.shape.databases.valueType.extend({
|
|
6509
|
-
instance_name: string().min(1),
|
|
6510
|
-
migrations: databaseMigrationsSchema
|
|
6511
|
-
})),
|
|
6390
|
+
connections: record(resourcesSchema.shape.connections.keyType, resourcesSchema.shape.connections.valueType.extend({ account_id: string().min(1).nullable() })),
|
|
6391
|
+
databases: record(resourcesSchema.shape.databases.keyType, resourcesSchema.shape.databases.valueType.extend({ instance_name: string().min(1) })),
|
|
6512
6392
|
secrets: record(resourcesSchema.shape.secrets.keyType, resourcesSchema.shape.secrets.valueType.extend({ value: string().nullable() }))
|
|
6513
6393
|
});
|
|
6514
6394
|
//#endregion
|
|
6515
|
-
//#region env.ts
|
|
6516
|
-
function readLocalSecret(name) {
|
|
6517
|
-
return process.env[name] ?? null;
|
|
6518
|
-
}
|
|
6519
|
-
const env = object({
|
|
6520
|
-
CTX_AUTH_CLIENT_ID: string().min(1).default("QBFOYTTCdjpEiXwgDNnovDPoJBYAfRpF"),
|
|
6521
|
-
CTX_AUTH_ISSUER: url().default("https://www.usecontextlayer.com/api/auth"),
|
|
6522
|
-
CTX_PLATFORM_URL: url().default("https://platform.usecontextlayer.com")
|
|
6523
|
-
}).parse(process.env);
|
|
6524
|
-
//#endregion
|
|
6525
6395
|
//#region slate.ts
|
|
6526
|
-
async function resolveSlateTarget(target) {
|
|
6527
|
-
return target === "local" ? getLocalSlateId() : target;
|
|
6528
|
-
}
|
|
6529
|
-
async function joinSlate(target) {
|
|
6530
|
-
const slateTarget = await resolveSlateTarget(target);
|
|
6531
|
-
const response = await fetch(new URL(`/api/slates/${encodeURIComponent(slateTarget)}/join`, env.CTX_PLATFORM_URL), {
|
|
6532
|
-
headers: { authorization: `Bearer ${await getAccessToken(env)}` },
|
|
6533
|
-
method: "POST"
|
|
6534
|
-
});
|
|
6535
|
-
if (!response.ok) throw new Error(`Slate join failed: HTTP ${response.status}`);
|
|
6536
|
-
const member = object({
|
|
6537
|
-
slate_id: uuid(),
|
|
6538
|
-
user_id: string().min(1)
|
|
6539
|
-
}).parse(await response.json());
|
|
6540
|
-
console.log(JSON.stringify(member));
|
|
6541
|
-
}
|
|
6542
6396
|
async function getLocalSlateId() {
|
|
6543
6397
|
const { stdout: remote } = await execa("git", [
|
|
6544
6398
|
"remote",
|
|
@@ -6577,43 +6431,37 @@ async function readLocalConnections(directory) {
|
|
|
6577
6431
|
const contents = await readOptionalFile(resolve(directory, ".contextlayer/connections.json"));
|
|
6578
6432
|
return localConnectionsSchema.parse(contents === null ? {} : JSON.parse(contents));
|
|
6579
6433
|
}
|
|
6580
|
-
|
|
6581
|
-
|
|
6582
|
-
const ignorePath = resolve(directory, ".gitignore");
|
|
6583
|
-
const contents = await readOptionalFile(ignorePath) ?? "";
|
|
6584
|
-
if (!contents.split(/\r?\n/).includes("/.contextlayer/")) await writeFile(ignorePath, `${contents}${contents && !contents.endsWith("\n") ? "\n" : ""}/.contextlayer/\n`);
|
|
6434
|
+
function getLocalResourcesWorkerName() {
|
|
6435
|
+
return `ctx-local-resources-${createHash("sha256").update(process.cwd()).digest("hex").slice(0, 16)}`;
|
|
6585
6436
|
}
|
|
6586
6437
|
async function prepareLocalResourceConfiguration(directory = process.cwd()) {
|
|
6587
6438
|
const resources = await readLocalResources(directory);
|
|
6588
6439
|
const assignments = new Map(Object.entries(await readLocalConnections(directory)));
|
|
6589
|
-
|
|
6440
|
+
return localResourceConfigurationSchema.parse({
|
|
6590
6441
|
connections: Object.fromEntries(Object.entries(resources.connections).map(([slug, requirement]) => [slug, {
|
|
6591
6442
|
...requirement,
|
|
6592
|
-
|
|
6443
|
+
account_id: assignments.get(slug) ?? null
|
|
6593
6444
|
}])),
|
|
6594
|
-
databases: Object.fromEntries(
|
|
6445
|
+
databases: Object.fromEntries(Object.entries(resources.databases).map(([slug, requirement]) => [slug, {
|
|
6595
6446
|
...requirement,
|
|
6596
|
-
instance_name: slug
|
|
6597
|
-
|
|
6598
|
-
}]))),
|
|
6447
|
+
instance_name: slug
|
|
6448
|
+
}])),
|
|
6599
6449
|
secrets: Object.fromEntries(Object.entries(resources.secrets).map(([name, requirement]) => [name, {
|
|
6600
6450
|
...requirement,
|
|
6601
6451
|
value: readLocalSecret(name)
|
|
6602
6452
|
}]))
|
|
6603
6453
|
});
|
|
6604
|
-
await prepareLocalDirectory(directory);
|
|
6605
|
-
return configuration;
|
|
6606
6454
|
}
|
|
6607
|
-
async function saveLocalConnection(slug,
|
|
6608
|
-
const assignment = localConnectionsSchema.parse({ [slug]:
|
|
6455
|
+
async function saveLocalConnection(slug, accountId, directory = process.cwd()) {
|
|
6456
|
+
const assignment = localConnectionsSchema.parse({ [slug]: accountId });
|
|
6609
6457
|
const connections = {
|
|
6610
6458
|
...await readLocalConnections(directory),
|
|
6611
6459
|
...assignment
|
|
6612
6460
|
};
|
|
6613
|
-
await
|
|
6461
|
+
await mkdir(resolve(directory, ".contextlayer"), { recursive: true });
|
|
6614
6462
|
await writeFile(resolve(directory, ".contextlayer/connections.json"), `${JSON.stringify(connections, null, 2)}\n`);
|
|
6615
6463
|
}
|
|
6616
6464
|
//#endregion
|
|
6617
|
-
export {
|
|
6465
|
+
export { noCommitSchema as a, execa as c, getLocalSlateId as i, prepareLocalResourceConfiguration as n, slateResourceConfigurationSchema as o, saveLocalConnection as r, ownerSchema as s, getLocalResourcesWorkerName as t };
|
|
6618
6466
|
|
|
6619
|
-
//# sourceMappingURL=resources
|
|
6467
|
+
//# sourceMappingURL=resources-0GJALfam.mjs.map
|