agent-lyra 0.3.1 → 0.6.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.
@@ -0,0 +1,102 @@
1
+ import { createServer } from "node:http";
2
+ import { readFile } from "node:fs/promises";
3
+ import { readCatalog, readLayout, runSelection, saveLayout } from "./project.js";
4
+ import { pullStudio } from "./sync.js";
5
+
6
+ export async function startStudio(root, { port = 0, run = runSelection, pull = pullStudio, appCatalog } = {}) {
7
+ const assets = new Map();
8
+ for (const [route, name, mime] of [
9
+ ["/agent-ui.js", "agent-ui.js", "text/javascript"], ["/agent-ui.css", "agent-ui.css", "text/css"],
10
+ ["/", "index.html", "text/html"], ["/studio.js", "browser.js", "text/javascript"], ["/studio.css", "studio.css", "text/css"],
11
+ ]) assets.set(route, [await readFile(new URL("./" + name, import.meta.url)), mime]);
12
+
13
+ let running = false;
14
+ let writing = false;
15
+ const server = createServer(async (req, res) => {
16
+ const send = (status, data, type = "application/json") => {
17
+ res.writeHead(status, {
18
+ "Content-Type": type + "; charset=utf-8", "Cache-Control": "no-store", "X-Content-Type-Options": "nosniff",
19
+ "Content-Security-Policy": "default-src 'none'; script-src 'self'; style-src 'self'; connect-src 'self'; frame-ancestors 'none'",
20
+ });
21
+ res.end(type === "application/json" ? JSON.stringify(data) : data);
22
+ };
23
+ const host = "127.0.0.1:" + server.address().port;
24
+ if (req.headers.host !== host) return send(403, { error: "Use the Studio loopback URL." });
25
+ if (req.method === "GET" && assets.has(req.url)) {
26
+ const [data, mime] = assets.get(req.url);
27
+ return send(200, data, mime);
28
+ }
29
+ try {
30
+ if (req.method === "GET" && req.url === "/api/project") {
31
+ const catalog = appCatalog ?? await readCatalog(root);
32
+ return send(200, {
33
+ ...await readLayout(root),
34
+ exported: Boolean(appCatalog),
35
+ agents: [...catalog.agents.values()].map(({ id, name, source }) => ({ id, name, source })),
36
+ workflows: catalog.workflows.map(({ id, name, runnable }) => ({ id, name, runnable })),
37
+ teams: catalog.teams.map(({ id, name }) => ({ id, name })),
38
+ models: (process.env.LYRA_STUDIO_MODELS || "gpt-4o-mini").split(",").map(value => value.trim()).filter(Boolean),
39
+ });
40
+ }
41
+ if (req.method !== "POST" || !["/api/save", "/api/run", "/api/pull"].includes(req.url)) {
42
+ return send(404, { error: "Not found." });
43
+ }
44
+ if (appCatalog && req.url !== "/api/run") return send(404, { error: "Not found." });
45
+ if (req.headers.origin !== "http://" + host) return send(403, { error: "Use the Studio page to submit requests." });
46
+ if (req.headers["content-type"]?.split(";")[0] !== "application/json") return send(415, { error: "Use application/json." });
47
+ const chunks = [];
48
+ let size = 0;
49
+ for await (const chunk of req) {
50
+ size += chunk.length;
51
+ if (size > 65_536) return send(413, { error: "Studio request is too large." });
52
+ chunks.push(chunk);
53
+ }
54
+ const body = JSON.parse(Buffer.concat(chunks).toString("utf8"));
55
+ if (req.url === "/api/run") {
56
+ if (running) return send(409, { error: "A Studio run is already in progress." });
57
+ running = true;
58
+ try {
59
+ if (appCatalog) {
60
+ const { layout } = await readLayout(root);
61
+ const button = layout.find(node => ["button", "agent-card"].includes(node.kind) && node.id === body.buttonId);
62
+ if (!button) return send(400, { error: "Choose a run button from the app." });
63
+ body.targets = button.targets;
64
+ }
65
+ return send(200, await run(root, body, appCatalog));
66
+ } finally { running = false; }
67
+ }
68
+ if (writing) return send(409, { error: "Studio is already saving or pulling files." });
69
+ writing = true;
70
+ try {
71
+ return send(200, req.url === "/api/save"
72
+ ? await saveLayout(root, body.layout, body.revision)
73
+ : await pull(root));
74
+ } finally { writing = false; }
75
+ } catch (error) {
76
+ const message = error.code ? "Studio could not read or write the project." : error.message;
77
+ return send(400, { error: message });
78
+ }
79
+ });
80
+
81
+ server.requestTimeout = 10_000;
82
+ await new Promise((resolve, reject) => {
83
+ server.once("error", reject);
84
+ server.listen(port, "127.0.0.1", resolve);
85
+ });
86
+ return { server, url: "http://127.0.0.1:" + server.address().port + "/" };
87
+ }
88
+
89
+ export async function runStudio(args, output = process.stdout) {
90
+ if (args[0] === "--help") {
91
+ output.write("Usage: agent-lyra studio | agent-lyra studio pull\n");
92
+ return;
93
+ }
94
+ if (args.length === 1 && args[0] === "pull") {
95
+ const { added, preserved } = await pullStudio(process.cwd());
96
+ output.write("Cloud pull: " + added + " added, " + preserved + " existing preserved.\n");
97
+ return;
98
+ }
99
+ if (args.length) throw new Error("See agent-lyra studio --help.");
100
+ const { url } = await startStudio(process.cwd());
101
+ output.write("AgentLyra Studio: " + url + "\n");
102
+ }
@@ -0,0 +1,7 @@
1
+ *{box-sizing:border-box}body{margin:0;background:#f6f8f6;color:#173328;font:0.95rem/1.5 system-ui,sans-serif}header{display:flex;align-items:center;justify-content:space-between;gap:.8rem;padding:1rem 1.25rem;border-bottom:1px solid #d9e4dd;background:#fff}header strong{white-space:nowrap}h1,h2,p{margin:.2rem 0 .65rem}h1{font-size:1.45rem}h2{font-size:1rem}main{display:grid;grid-template-columns:minmax(13rem,1fr) minmax(18rem,2.6fr) minmax(14rem,1.1fr);gap:1rem;padding:0 1.25rem 1rem}main>aside,main>section{min-width:0;padding:1rem;border:1px solid #d9e4dd;border-radius:.65rem;background:#fff}button,input,select,textarea{font:inherit}button{border:1px solid #b4cabe;border-radius:.35rem;background:#fff;color:#174b38;padding:.35rem .65rem;cursor:pointer}button:hover{background:#e6f0e9}.primary{background:#174b38;color:#fff;border-color:#174b38}.primary:hover{background:#246449}button:disabled{opacity:.5;cursor:wait}input,textarea,select{display:block;padding:.45rem;border:1px solid #a8c0b1;border-radius:.35rem;background:#fff;color:#173328;width:100%;margin-bottom:.65rem}label{display:block;margin-top:.65rem}#status{min-height:1.5rem;padding:0 1.25rem}#palette{display:grid;grid-template-columns:1fr 1fr;gap:.4rem;margin-bottom:1.15rem}.agent-card,.node{padding:.6rem;margin:.5rem 0;border:1px solid #cbdcd1;border-radius:.45rem}.agent-card span{display:block;color:#47675a;font-size:.8rem}.agent-card button{margin-top:.35rem}#canvas{min-height:18rem;border:1px dashed #8aa99a;border-radius:.6rem;background:#f9fcfa;padding:.65rem}.node{background:#fff}.node.selected{border-color:#2f7658;outline:1px solid #2f7658}.node>button:first-child{margin-bottom:.35rem}.actions{display:flex;flex-wrap:wrap;gap:.35rem}.binding{margin:.35rem 0;display:flex;justify-content:space-between;gap:.35rem}pre{white-space:pre-wrap;overflow-wrap:anywhere;background:#eff5f1;padding:.6rem;border-radius:.35rem}dialog{border:0;margin:0 0 0 auto;height:100%;max-height:100%;width:min(29rem,100%);max-width:100%;padding:1rem;color:#173328}dialog::backdrop{background:#203a3266}dialog header{padding:0 0 .5rem;margin-bottom:1rem}@media(max-width:860px){main{grid-template-columns:1fr 2fr}main>aside:last-child{grid-column:1/-1}}@media(max-width:600px){main{grid-template-columns:1fr}header{flex-wrap:wrap}main>aside:last-child{grid-column:auto}}
2
+
3
+ [hidden]{display:none!important}
4
+ .exported > header,.exported main > aside,.exported main > section > p{display:none}
5
+ .exported main{display:block;max-width:52rem;margin:1rem auto}
6
+ .exported #canvas{border:0;background:transparent;padding:0;min-height:0}
7
+ .exported .node{border:0;padding:0;margin:1rem 0}
@@ -0,0 +1,39 @@
1
+ import { lstat, mkdir, writeFile } from "node:fs/promises";
2
+ import { join } from "node:path";
3
+
4
+ export async function pullStudio(root, { key = process.env.LYRA_API_KEY, request = fetch } = {}) {
5
+ if (!/^lyra_sdk_[a-f0-9]{64}$/.test(key ?? "")) throw new Error("Set LYRA_API_KEY to your AgentLyra SDK key.");
6
+ const response = await request("https://api.agentlyra.in/api/sdk/v1/studio/catalog", {
7
+ headers: { Authorization: "Bearer " + key }, redirect: "error", signal: AbortSignal.timeout(20_000),
8
+ });
9
+ if (!response.ok) throw new Error("Cloud pull failed (HTTP " + response.status + ").");
10
+ const catalog = await response.json();
11
+ const count = { added: 0, preserved: 0 };
12
+ const base = join(root, ".lyra");
13
+ await mkdir(base, { recursive: true });
14
+ if (!(await lstat(base)).isDirectory()) throw new Error(".lyra must be a regular directory.");
15
+
16
+ for (const [kind, rows] of Object.entries({ agents: catalog.agents, workflows: catalog.workflows, teams: catalog.teams })) {
17
+ if (!Array.isArray(rows) || rows.length > 100) throw new Error("Unexpected Cloud catalog response.");
18
+ const folder = join(base, kind);
19
+ await mkdir(folder, { recursive: true });
20
+ if (!(await lstat(folder)).isDirectory()) throw new Error("Catalog paths must be regular directories.");
21
+ for (const row of rows) {
22
+ if (typeof row?.id !== "string" || !/^[a-f0-9]{24}$/i.test(row.id) || typeof row.name !== "string") {
23
+ throw new Error("Unexpected Cloud catalog entry.");
24
+ }
25
+ const name = "cloud-" + row.id + (kind === "agents" ? ".md" : ".json");
26
+ const content = kind === "agents"
27
+ ? "---\nid: " + row.id + "\nname: " + JSON.stringify(row.name) + "\nmode: cloud\n---\n"
28
+ : JSON.stringify(row, null, 2) + "\n";
29
+ try {
30
+ await writeFile(join(folder, name), content, { flag: "wx" });
31
+ count.added++;
32
+ } catch (error) {
33
+ if (error.code !== "EEXIST") throw error;
34
+ count.preserved++;
35
+ }
36
+ }
37
+ }
38
+ return count;
39
+ }