@usecontextlayer/cli 0.15.0 → 0.16.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 +10 -8
- package/dist/{auth-CMrKVwsH.mjs → auth-DOsE8GwJ.mjs} +2 -2
- package/dist/{auth-CMrKVwsH.mjs.map → auth-DOsE8GwJ.mjs.map} +1 -1
- package/dist/auth.mjs +1 -1
- package/dist/cli.mjs +57 -57
- package/dist/cli.mjs.map +1 -1
- package/dist/{resources-5FTDu19i.mjs → resources-B-EYHG1j.mjs} +77 -77
- package/dist/resources-B-EYHG1j.mjs.map +1 -0
- package/dist/template/AGENTS.md +6 -6
- package/dist/template/README.md +14 -12
- package/dist/template/app/root.tsx +7 -7
- package/dist/template/app/routes/home.tsx +8 -7
- package/dist/template/workers/app.ts +2 -2
- package/dist/template/workers/local-resources/index.ts +6 -1
- package/dist/template/workers/local-resources/wrangler.jsonc +2 -2
- package/dist/vite.d.mts +19 -19
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +15 -15
- package/dist/vite.mjs.map +1 -1
- package/package.json +3 -3
- package/dist/resources-5FTDu19i.mjs.map +0 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { C as ZodError, S as uuid, _ as strictObject, c as array, f as literal, g as record, h as object, l as boolean, m as number, n as getAccessToken, o as _enum, s as _null, u as discriminatedUnion, v as string, x as url, y as union } from "./auth-
|
|
1
|
+
import { C as ZodError, S as uuid, _ as strictObject, c as array, f as literal, g as record, h as object, l as boolean, m as number, n as getAccessToken, o as _enum, s as _null, u as discriminatedUnion, v as string, x as url, y as union } from "./auth-DOsE8GwJ.mjs";
|
|
2
2
|
import { glob, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
3
3
|
import { constants } from "node:os";
|
|
4
4
|
import path, { join, resolve } from "node:path";
|
|
@@ -6396,7 +6396,7 @@ const databaseResultSchema = object({
|
|
|
6396
6396
|
rowsWritten: number()
|
|
6397
6397
|
});
|
|
6398
6398
|
//#endregion
|
|
6399
|
-
//#region ../shared/dist/
|
|
6399
|
+
//#region ../shared/dist/declaration.mjs
|
|
6400
6400
|
const credentialModeSchema = _enum(["viewer", "owner"]);
|
|
6401
6401
|
const connectionRequirementSchema = strictObject({
|
|
6402
6402
|
mode: credentialModeSchema,
|
|
@@ -6405,40 +6405,40 @@ const connectionRequirementSchema = strictObject({
|
|
|
6405
6405
|
const secretNameSchema = string().regex(/^[A-Z_][A-Z0-9_]*$/);
|
|
6406
6406
|
const secretRequirementSchema = strictObject({ mode: credentialModeSchema });
|
|
6407
6407
|
const databaseRequirementSchema = strictObject({ mode: credentialModeSchema });
|
|
6408
|
-
const
|
|
6409
|
-
const
|
|
6408
|
+
const databaseSlugSchema = string().min(1).regex(/^(?!\.{1,2}$)[^/\\]+$/);
|
|
6409
|
+
const declarationSchema = strictObject({
|
|
6410
6410
|
connections: record(string().min(1), connectionRequirementSchema),
|
|
6411
|
-
databases: record(
|
|
6411
|
+
databases: record(databaseSlugSchema, databaseRequirementSchema).default({}),
|
|
6412
6412
|
secrets: record(secretNameSchema, secretRequirementSchema).default({})
|
|
6413
6413
|
});
|
|
6414
6414
|
strictObject({
|
|
6415
|
-
agents: record(string().min(1),
|
|
6416
|
-
app:
|
|
6415
|
+
agents: record(string().min(1), declarationSchema),
|
|
6416
|
+
app: declarationSchema
|
|
6417
6417
|
});
|
|
6418
6418
|
const resourceConsumerSchema = discriminatedUnion("type", [strictObject({ type: literal("app") }), strictObject({
|
|
6419
6419
|
name: string().min(1),
|
|
6420
6420
|
type: literal("agent")
|
|
6421
6421
|
})]);
|
|
6422
|
-
const resourcesSchema =
|
|
6422
|
+
const resourcesSchema = declarationSchema.extend({
|
|
6423
6423
|
connections: record(string().min(1), connectionRequirementSchema.extend({ consumers: array(resourceConsumerSchema) })),
|
|
6424
|
-
databases: record(
|
|
6424
|
+
databases: record(databaseSlugSchema, databaseRequirementSchema.extend({ consumers: array(resourceConsumerSchema) })),
|
|
6425
6425
|
secrets: record(secretNameSchema, secretRequirementSchema.extend({ consumers: array(resourceConsumerSchema) }))
|
|
6426
6426
|
});
|
|
6427
|
-
function
|
|
6427
|
+
function collectSlateResources(slateDeclarations) {
|
|
6428
6428
|
const declarations = [{
|
|
6429
6429
|
consumer: { type: "app" },
|
|
6430
|
-
|
|
6431
|
-
}, ...Object.entries(
|
|
6430
|
+
declaration: slateDeclarations.app
|
|
6431
|
+
}, ...Object.entries(slateDeclarations.agents).map(([name, declaration]) => ({
|
|
6432
6432
|
consumer: {
|
|
6433
6433
|
name,
|
|
6434
6434
|
type: "agent"
|
|
6435
6435
|
},
|
|
6436
|
-
|
|
6436
|
+
declaration
|
|
6437
6437
|
}))];
|
|
6438
6438
|
return {
|
|
6439
|
-
connections: collectRequirements("connections", declarations, (
|
|
6440
|
-
databases: collectRequirements("databases", declarations, (
|
|
6441
|
-
secrets: collectRequirements("secrets", declarations, (
|
|
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
6442
|
};
|
|
6443
6443
|
}
|
|
6444
6444
|
function resourcesForConsumer(resources, consumer) {
|
|
@@ -6453,10 +6453,10 @@ function filterRequirements(requirements, consumer) {
|
|
|
6453
6453
|
}
|
|
6454
6454
|
function collectRequirements(category, declarations, select) {
|
|
6455
6455
|
const resources = /* @__PURE__ */ new Map();
|
|
6456
|
-
for (const { consumer,
|
|
6457
|
-
const existing = resources.get(
|
|
6456
|
+
for (const { consumer, declaration } of declarations) for (const [key, requirement] of Object.entries(select(declaration))) {
|
|
6457
|
+
const existing = resources.get(key);
|
|
6458
6458
|
if (!existing) {
|
|
6459
|
-
resources.set(
|
|
6459
|
+
resources.set(key, {
|
|
6460
6460
|
...requirement,
|
|
6461
6461
|
consumers: [consumer]
|
|
6462
6462
|
});
|
|
@@ -6464,11 +6464,11 @@ function collectRequirements(category, declarations, select) {
|
|
|
6464
6464
|
}
|
|
6465
6465
|
if (existing.mode !== requirement.mode || existing.toolkit !== requirement.toolkit) throw new ZodError([{
|
|
6466
6466
|
code: "custom",
|
|
6467
|
-
message: `Conflicting ${category} requirement "${
|
|
6467
|
+
message: `Conflicting ${category} requirement "${key}": declarations must agree on mode${category === "connections" ? " and toolkit" : ""}`,
|
|
6468
6468
|
path: [
|
|
6469
6469
|
...consumer.type === "app" ? ["app"] : ["agents", consumer.name],
|
|
6470
6470
|
category,
|
|
6471
|
-
|
|
6471
|
+
key
|
|
6472
6472
|
]
|
|
6473
6473
|
}]);
|
|
6474
6474
|
existing.consumers.push(consumer);
|
|
@@ -6476,6 +6476,41 @@ function collectRequirements(category, declarations, select) {
|
|
|
6476
6476
|
return Object.fromEntries(resources);
|
|
6477
6477
|
}
|
|
6478
6478
|
//#endregion
|
|
6479
|
+
//#region ../shared/dist/slate-usage.mjs
|
|
6480
|
+
const ownerSchema = discriminatedUnion("type", [object({
|
|
6481
|
+
id: string().min(1),
|
|
6482
|
+
type: literal("personal")
|
|
6483
|
+
}), object({
|
|
6484
|
+
id: string().min(1),
|
|
6485
|
+
type: literal("organization")
|
|
6486
|
+
})]);
|
|
6487
|
+
object({
|
|
6488
|
+
id: uuid(),
|
|
6489
|
+
owner: ownerSchema
|
|
6490
|
+
});
|
|
6491
|
+
//#endregion
|
|
6492
|
+
//#region ../shared/dist/slate-resources.mjs
|
|
6493
|
+
const slateResourceConfigurationSchema = resourcesSchema.extend({
|
|
6494
|
+
connections: record(resourcesSchema.shape.connections.keyType, resourcesSchema.shape.connections.valueType.extend({
|
|
6495
|
+
connection_id: string().nullable(),
|
|
6496
|
+
owner: ownerSchema
|
|
6497
|
+
})),
|
|
6498
|
+
databases: record(resourcesSchema.shape.databases.keyType, resourcesSchema.shape.databases.valueType.extend({
|
|
6499
|
+
instance_name: string().min(1),
|
|
6500
|
+
owner: ownerSchema
|
|
6501
|
+
})),
|
|
6502
|
+
secrets: record(resourcesSchema.shape.secrets.keyType, resourcesSchema.shape.secrets.valueType.extend({ owner: ownerSchema }))
|
|
6503
|
+
});
|
|
6504
|
+
object({ resources: slateResourceConfigurationSchema });
|
|
6505
|
+
const localResourceConfigurationSchema = resourcesSchema.extend({
|
|
6506
|
+
connections: record(resourcesSchema.shape.connections.keyType, resourcesSchema.shape.connections.valueType.extend({ connection_id: string().min(1).nullable() })),
|
|
6507
|
+
databases: record(resourcesSchema.shape.databases.keyType, resourcesSchema.shape.databases.valueType.extend({
|
|
6508
|
+
instance_name: string().min(1),
|
|
6509
|
+
migrations: databaseMigrationsSchema
|
|
6510
|
+
})),
|
|
6511
|
+
secrets: record(resourcesSchema.shape.secrets.keyType, resourcesSchema.shape.secrets.valueType.extend({ value: string().nullable() }))
|
|
6512
|
+
});
|
|
6513
|
+
//#endregion
|
|
6479
6514
|
//#region env.ts
|
|
6480
6515
|
function readLocalSecret(name) {
|
|
6481
6516
|
return process.env[name] ?? null;
|
|
@@ -6483,27 +6518,27 @@ function readLocalSecret(name) {
|
|
|
6483
6518
|
const env = object({
|
|
6484
6519
|
CTX_AUTH_CLIENT_ID: string().min(1).default("QBFOYTTCdjpEiXwgDNnovDPoJBYAfRpF"),
|
|
6485
6520
|
CTX_AUTH_ISSUER: url().default("https://www.usecontextlayer.com/api/auth"),
|
|
6486
|
-
CTX_PLATFORM_URL: url().default("https://
|
|
6521
|
+
CTX_PLATFORM_URL: url().default("https://platform.usecontextlayer.com")
|
|
6487
6522
|
}).parse(process.env);
|
|
6488
6523
|
//#endregion
|
|
6489
|
-
//#region
|
|
6490
|
-
async function
|
|
6491
|
-
return target === "local" ?
|
|
6524
|
+
//#region slate.ts
|
|
6525
|
+
async function resolveSlateTarget(target) {
|
|
6526
|
+
return target === "local" ? getLocalSlateId() : target;
|
|
6492
6527
|
}
|
|
6493
|
-
async function
|
|
6494
|
-
const
|
|
6495
|
-
const response = await fetch(new URL(`/api/
|
|
6528
|
+
async function joinSlate(target) {
|
|
6529
|
+
const slateTarget = await resolveSlateTarget(target);
|
|
6530
|
+
const response = await fetch(new URL(`/api/slates/${encodeURIComponent(slateTarget)}/join`, env.CTX_PLATFORM_URL), {
|
|
6496
6531
|
headers: { authorization: `Bearer ${await getAccessToken(env)}` },
|
|
6497
6532
|
method: "POST"
|
|
6498
6533
|
});
|
|
6499
|
-
if (!response.ok) throw new Error(`
|
|
6534
|
+
if (!response.ok) throw new Error(`Slate join failed: HTTP ${response.status}`);
|
|
6500
6535
|
const member = object({
|
|
6501
|
-
|
|
6536
|
+
slate_id: uuid(),
|
|
6502
6537
|
user_id: string().min(1)
|
|
6503
6538
|
}).parse(await response.json());
|
|
6504
6539
|
console.log(JSON.stringify(member));
|
|
6505
6540
|
}
|
|
6506
|
-
async function
|
|
6541
|
+
async function getLocalSlateId() {
|
|
6507
6542
|
const { stdout: remote } = await execa("git", [
|
|
6508
6543
|
"remote",
|
|
6509
6544
|
"get-url",
|
|
@@ -6512,56 +6547,21 @@ async function getLocalAppId() {
|
|
|
6512
6547
|
return uuid().parse(new URL(remote).pathname.replace(/^\/git\//, ""));
|
|
6513
6548
|
}
|
|
6514
6549
|
async function readLocalResources(directory = process.cwd()) {
|
|
6515
|
-
const app = await
|
|
6550
|
+
const app = await readLocalDeclaration(join(directory, "public/slate.json"));
|
|
6516
6551
|
const agents = {};
|
|
6517
6552
|
for await (const entry of glob("agents/*", {
|
|
6518
6553
|
cwd: directory,
|
|
6519
6554
|
withFileTypes: true
|
|
6520
|
-
})) if (entry.isDirectory()) agents[entry.name] = await
|
|
6521
|
-
return
|
|
6555
|
+
})) if (entry.isDirectory()) agents[entry.name] = await readLocalDeclaration(join(directory, "agents", entry.name, "slate.json"));
|
|
6556
|
+
return collectSlateResources({
|
|
6522
6557
|
agents,
|
|
6523
6558
|
app
|
|
6524
6559
|
});
|
|
6525
6560
|
}
|
|
6526
|
-
async function
|
|
6527
|
-
return
|
|
6561
|
+
async function readLocalDeclaration(path) {
|
|
6562
|
+
return declarationSchema.parse(JSON.parse(await readFile(path, "utf8")));
|
|
6528
6563
|
}
|
|
6529
6564
|
//#endregion
|
|
6530
|
-
//#region ../shared/dist/app-usage.mjs
|
|
6531
|
-
const ownerSchema = discriminatedUnion("type", [object({
|
|
6532
|
-
id: string().min(1),
|
|
6533
|
-
type: literal("user")
|
|
6534
|
-
}), object({
|
|
6535
|
-
id: string().min(1),
|
|
6536
|
-
type: literal("organization")
|
|
6537
|
-
})]);
|
|
6538
|
-
object({
|
|
6539
|
-
id: uuid(),
|
|
6540
|
-
owner: ownerSchema
|
|
6541
|
-
});
|
|
6542
|
-
//#endregion
|
|
6543
|
-
//#region ../shared/dist/app-resources.mjs
|
|
6544
|
-
const appResourceConfigurationSchema = resourcesSchema.extend({
|
|
6545
|
-
connections: record(resourcesSchema.shape.connections.keyType, resourcesSchema.shape.connections.valueType.extend({
|
|
6546
|
-
connection_id: string().nullable(),
|
|
6547
|
-
owner: ownerSchema
|
|
6548
|
-
})),
|
|
6549
|
-
databases: record(resourcesSchema.shape.databases.keyType, resourcesSchema.shape.databases.valueType.extend({
|
|
6550
|
-
instance_name: string().min(1),
|
|
6551
|
-
owner: ownerSchema
|
|
6552
|
-
})),
|
|
6553
|
-
secrets: record(resourcesSchema.shape.secrets.keyType, resourcesSchema.shape.secrets.valueType.extend({ owner: ownerSchema }))
|
|
6554
|
-
});
|
|
6555
|
-
object({ resources: appResourceConfigurationSchema });
|
|
6556
|
-
const localResourceConfigurationSchema = resourcesSchema.extend({
|
|
6557
|
-
connections: record(resourcesSchema.shape.connections.keyType, resourcesSchema.shape.connections.valueType.extend({ connection_id: string().min(1).nullable() })),
|
|
6558
|
-
databases: record(resourcesSchema.shape.databases.keyType, resourcesSchema.shape.databases.valueType.extend({
|
|
6559
|
-
instance_name: string().min(1),
|
|
6560
|
-
migrations: databaseMigrationsSchema
|
|
6561
|
-
})),
|
|
6562
|
-
secrets: record(resourcesSchema.shape.secrets.keyType, resourcesSchema.shape.secrets.valueType.extend({ value: string().nullable() }))
|
|
6563
|
-
});
|
|
6564
|
-
//#endregion
|
|
6565
6565
|
//#region resources.ts
|
|
6566
6566
|
const localConnectionsSchema = record(resourcesSchema.shape.connections.keyType, string().min(1));
|
|
6567
6567
|
async function readOptionalFile(path) {
|
|
@@ -6584,11 +6584,11 @@ async function prepareLocalDirectory(directory) {
|
|
|
6584
6584
|
}
|
|
6585
6585
|
async function prepareLocalResourceConfiguration(directory = process.cwd()) {
|
|
6586
6586
|
const resources = await readLocalResources(directory);
|
|
6587
|
-
const
|
|
6587
|
+
const assignments = new Map(Object.entries(await readLocalConnections(directory)));
|
|
6588
6588
|
const configuration = localResourceConfigurationSchema.parse({
|
|
6589
6589
|
connections: Object.fromEntries(Object.entries(resources.connections).map(([slug, requirement]) => [slug, {
|
|
6590
6590
|
...requirement,
|
|
6591
|
-
connection_id:
|
|
6591
|
+
connection_id: assignments.get(slug) ?? null
|
|
6592
6592
|
}])),
|
|
6593
6593
|
databases: Object.fromEntries(await Promise.all(Object.entries(resources.databases).map(async ([slug, requirement]) => [slug, {
|
|
6594
6594
|
...requirement,
|
|
@@ -6604,15 +6604,15 @@ async function prepareLocalResourceConfiguration(directory = process.cwd()) {
|
|
|
6604
6604
|
return configuration;
|
|
6605
6605
|
}
|
|
6606
6606
|
async function saveLocalConnection(slug, connectionId, directory = process.cwd()) {
|
|
6607
|
-
const
|
|
6607
|
+
const assignment = localConnectionsSchema.parse({ [slug]: connectionId });
|
|
6608
6608
|
const connections = {
|
|
6609
6609
|
...await readLocalConnections(directory),
|
|
6610
|
-
...
|
|
6610
|
+
...assignment
|
|
6611
6611
|
};
|
|
6612
6612
|
await prepareLocalDirectory(directory);
|
|
6613
6613
|
await writeFile(resolve(directory, ".contextlayer/connections.json"), `${JSON.stringify(connections, null, 2)}\n`);
|
|
6614
6614
|
}
|
|
6615
6615
|
//#endregion
|
|
6616
|
-
export {
|
|
6616
|
+
export { env as a, resourcesForConsumer as c, databaseResultSchema as d, execa as f, joinSlate as i, secretNameSchema as l, saveLocalConnection as n, slateResourceConfigurationSchema as o, getLocalSlateId as r, ownerSchema as s, prepareLocalResourceConfiguration as t, databaseQuerySchema as u };
|
|
6617
6617
|
|
|
6618
|
-
//# sourceMappingURL=resources-
|
|
6618
|
+
//# sourceMappingURL=resources-B-EYHG1j.mjs.map
|