agent-lyra 0.3.2 → 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.
- package/README.md +67 -14
- package/bin/agent-lyra.js +4 -1
- package/package.json +1 -1
- package/src/app-builder/export.js +109 -59
- package/src/app-builder/init.js +52 -23
- package/src/app-builder/preview.js +22 -14
- package/src/app-builder/saved-app.js +70 -0
- package/src/app-builder/saved-preview-browser.js +33 -0
- package/src/app-builder/saved-preview.html +24 -0
- package/src/app-builder/saved-preview.js +61 -0
- package/src/app-builder/validate.js +64 -65
- package/src/runtime/local-cli.js +2 -2
- package/src/studio/agent-ui.css +43 -0
- package/src/studio/agent-ui.js +32 -0
- package/src/studio/browser.js +289 -0
- package/src/studio/export.js +79 -0
- package/src/studio/index.html +16 -0
- package/src/studio/project.js +190 -0
- package/src/studio/python-agent.js +47 -0
- package/src/studio/python-agent.py +25 -0
- package/src/studio/server.js +102 -0
- package/src/studio/studio.css +7 -0
- package/src/studio/sync.js +39 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { createServer } from "node:http";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { createSavedAgentClient, runSavedApp, validateSavedApp } from "./saved-app.js";
|
|
4
|
+
|
|
5
|
+
export async function startSavedPreview(manifest, { port = Number(process.env.LYRA_PREVIEW_PORT ?? 4173), client } = {}) {
|
|
6
|
+
validateSavedApp(manifest);
|
|
7
|
+
client ??= createSavedAgentClient();
|
|
8
|
+
if (!Number.isInteger(port) || port < 0 || port > 65535) throw new Error("Invalid preview port.");
|
|
9
|
+
const [html, script] = await Promise.all(["saved-preview.html", "saved-preview-browser.js"].map(
|
|
10
|
+
name => readFile(new URL("./" + name, import.meta.url)),
|
|
11
|
+
));
|
|
12
|
+
const server = createServer(async (req, res) => {
|
|
13
|
+
const send = (status, body, type = "application/json; charset=utf-8") => {
|
|
14
|
+
res.writeHead(status, {
|
|
15
|
+
"Content-Type": type, "Cache-Control": "no-store", "X-Content-Type-Options": "nosniff",
|
|
16
|
+
"Content-Security-Policy": "default-src 'none'; script-src 'self'; style-src 'unsafe-inline'; connect-src 'self'; frame-ancestors 'none'",
|
|
17
|
+
});
|
|
18
|
+
res.end(type.startsWith("application/json") ? JSON.stringify(body) : body);
|
|
19
|
+
};
|
|
20
|
+
const authority = "127.0.0.1:" + server.address().port;
|
|
21
|
+
if (req.headers.host !== authority) return send(403, { error: "Use the loopback address." });
|
|
22
|
+
if (req.method === "GET" && req.url === "/") return send(200, html, "text/html; charset=utf-8");
|
|
23
|
+
if (req.method === "GET" && req.url === "/preview.js") return send(200, script, "text/javascript; charset=utf-8");
|
|
24
|
+
if (req.method === "GET" && req.url === "/api/app") return send(200, { name: manifest.application.name });
|
|
25
|
+
if (req.method !== "POST" || req.url !== "/api/run") return send(404, { error: "Not found." });
|
|
26
|
+
if (req.headers.origin !== "http://" + authority) return send(403, { error: "Use the application page to run agents." });
|
|
27
|
+
if (req.headers["content-type"]?.split(";")[0] !== "application/json") return send(415, { error: "Use application/json." });
|
|
28
|
+
|
|
29
|
+
const chunks = [];
|
|
30
|
+
let size = 0;
|
|
31
|
+
let input;
|
|
32
|
+
try {
|
|
33
|
+
for await (const chunk of req) {
|
|
34
|
+
size += chunk.length;
|
|
35
|
+
if (size > 24_576) return send(413, { error: "Input is too large." });
|
|
36
|
+
chunks.push(chunk);
|
|
37
|
+
}
|
|
38
|
+
input = JSON.parse(Buffer.concat(chunks).toString("utf8"))?.input;
|
|
39
|
+
} catch {
|
|
40
|
+
return send(400, { error: "Invalid request." });
|
|
41
|
+
}
|
|
42
|
+
if (typeof input !== "string" || !input.trim() || input.length > 4000) {
|
|
43
|
+
return send(400, { error: "Enter between 1 and 4,000 characters." });
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
send(200, await runSavedApp(manifest, input, client));
|
|
47
|
+
} catch (error) {
|
|
48
|
+
send(502, { error: error.message });
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
server.requestTimeout = 10_000;
|
|
53
|
+
await new Promise((resolve, reject) => {
|
|
54
|
+
server.once("error", reject);
|
|
55
|
+
server.listen(port, "127.0.0.1", resolve);
|
|
56
|
+
}).catch(error => {
|
|
57
|
+
if (error.code === "EADDRINUSE") throw new Error("Preview port is busy. Set LYRA_PREVIEW_PORT=0 to pick a free port.");
|
|
58
|
+
throw error;
|
|
59
|
+
});
|
|
60
|
+
return { server, url: "http://127.0.0.1:" + server.address().port + "/" };
|
|
61
|
+
}
|
|
@@ -1,65 +1,64 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { assertValueSchema } from "./value-schema.js";
|
|
3
|
-
|
|
4
|
-
const modes = new Set(["in-process", "local-process", "remote"]);
|
|
5
|
-
const identifier = /^[a-z][a-z0-9-]*$/;
|
|
6
|
-
const isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
7
|
-
const isIdentifier = (value) => typeof value === "string" && identifier.test(value);
|
|
8
|
-
|
|
9
|
-
export function validateAppManifest(manifest) {
|
|
10
|
-
if (!isRecord(manifest) || manifest.version !== "1.0") {
|
|
11
|
-
throw new Error('Application manifest requires version "1.0".');
|
|
12
|
-
}
|
|
13
|
-
const { application, capabilities, actions } = manifest;
|
|
14
|
-
if (!isRecord(application) || !isIdentifier(application.id) ||
|
|
15
|
-
typeof application.name !== "string" || !application.name.trim()) {
|
|
16
|
-
throw new Error("Application requires a valid id and a name.");
|
|
17
|
-
}
|
|
18
|
-
if (!Array.isArray(capabilities) || !Array.isArray(actions)) {
|
|
19
|
-
throw new Error("Capabilities and actions must be arrays.");
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const capabilityIds = new Set();
|
|
23
|
-
for (const capability of capabilities) {
|
|
24
|
-
if (!isRecord(capability) || !isIdentifier(capability.id) || !isIdentifier(capability.adapterId) ||
|
|
25
|
-
!modes.has(capability.executionMode)) {
|
|
26
|
-
throw new Error("Each capability requires an id, adapterId, and supported executionMode.");
|
|
27
|
-
}
|
|
28
|
-
if (capabilityIds.has(capability.id)) throw new Error(`Duplicate capability: ${capability.id}.`);
|
|
29
|
-
for (const key of ["inputSchema", "outputSchema"]) {
|
|
30
|
-
if (Object.hasOwn(capability, key)) assertValueSchema(capability[key], `${capability.id} ${key}`);
|
|
31
|
-
}
|
|
32
|
-
capabilityIds.add(capability.id);
|
|
33
|
-
}
|
|
34
|
-
const actionIds = new Set();
|
|
35
|
-
for (const action of actions) {
|
|
36
|
-
if (!isRecord(action) || !isIdentifier(action.id) || !isIdentifier(action.capabilityId)) {
|
|
37
|
-
throw new Error("Each action requires an id and capabilityId.");
|
|
38
|
-
}
|
|
39
|
-
if (actionIds.has(action.id)) throw new Error(`Duplicate action: ${action.id}.`);
|
|
40
|
-
if (!capabilityIds.has(action.capabilityId)) {
|
|
41
|
-
throw new Error(`Action ${action.id} refers to unknown capability ${action.capabilityId}.`);
|
|
42
|
-
}
|
|
43
|
-
actionIds.add(action.id);
|
|
44
|
-
}
|
|
45
|
-
return { id: application.id, capabilityCount: capabilityIds.size, actionCount: actionIds.size };
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export async function runAppValidation(args, output = process.stdout) {
|
|
49
|
-
if (args.length === 1 && args[0] === "--help") {
|
|
50
|
-
output.write("Usage: agent-lyra app validate <manifest.json>\n");
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
if (args.length !== 1 || args[0].startsWith("-")) {
|
|
54
|
-
throw new Error("Provide a manifest path. See agent-lyra app validate --help.");
|
|
55
|
-
}
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
1
|
+
import { readAppManifest, SAVED_VERSION, validateSavedApp } from "./saved-app.js";
|
|
2
|
+
import { assertValueSchema } from "./value-schema.js";
|
|
3
|
+
|
|
4
|
+
const modes = new Set(["in-process", "local-process", "remote"]);
|
|
5
|
+
const identifier = /^[a-z][a-z0-9-]*$/;
|
|
6
|
+
const isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
7
|
+
const isIdentifier = (value) => typeof value === "string" && identifier.test(value);
|
|
8
|
+
|
|
9
|
+
export function validateAppManifest(manifest) {
|
|
10
|
+
if (!isRecord(manifest) || manifest.version !== "1.0") {
|
|
11
|
+
throw new Error('Application manifest requires version "1.0".');
|
|
12
|
+
}
|
|
13
|
+
const { application, capabilities, actions } = manifest;
|
|
14
|
+
if (!isRecord(application) || !isIdentifier(application.id) ||
|
|
15
|
+
typeof application.name !== "string" || !application.name.trim()) {
|
|
16
|
+
throw new Error("Application requires a valid id and a name.");
|
|
17
|
+
}
|
|
18
|
+
if (!Array.isArray(capabilities) || !Array.isArray(actions)) {
|
|
19
|
+
throw new Error("Capabilities and actions must be arrays.");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const capabilityIds = new Set();
|
|
23
|
+
for (const capability of capabilities) {
|
|
24
|
+
if (!isRecord(capability) || !isIdentifier(capability.id) || !isIdentifier(capability.adapterId) ||
|
|
25
|
+
!modes.has(capability.executionMode)) {
|
|
26
|
+
throw new Error("Each capability requires an id, adapterId, and supported executionMode.");
|
|
27
|
+
}
|
|
28
|
+
if (capabilityIds.has(capability.id)) throw new Error(`Duplicate capability: ${capability.id}.`);
|
|
29
|
+
for (const key of ["inputSchema", "outputSchema"]) {
|
|
30
|
+
if (Object.hasOwn(capability, key)) assertValueSchema(capability[key], `${capability.id} ${key}`);
|
|
31
|
+
}
|
|
32
|
+
capabilityIds.add(capability.id);
|
|
33
|
+
}
|
|
34
|
+
const actionIds = new Set();
|
|
35
|
+
for (const action of actions) {
|
|
36
|
+
if (!isRecord(action) || !isIdentifier(action.id) || !isIdentifier(action.capabilityId)) {
|
|
37
|
+
throw new Error("Each action requires an id and capabilityId.");
|
|
38
|
+
}
|
|
39
|
+
if (actionIds.has(action.id)) throw new Error(`Duplicate action: ${action.id}.`);
|
|
40
|
+
if (!capabilityIds.has(action.capabilityId)) {
|
|
41
|
+
throw new Error(`Action ${action.id} refers to unknown capability ${action.capabilityId}.`);
|
|
42
|
+
}
|
|
43
|
+
actionIds.add(action.id);
|
|
44
|
+
}
|
|
45
|
+
return { id: application.id, capabilityCount: capabilityIds.size, actionCount: actionIds.size };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function runAppValidation(args, output = process.stdout) {
|
|
49
|
+
if (args.length === 1 && args[0] === "--help") {
|
|
50
|
+
output.write("Usage: agent-lyra app validate <manifest.json>\n");
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (args.length !== 1 || args[0].startsWith("-")) {
|
|
54
|
+
throw new Error("Provide a manifest path. See agent-lyra app validate --help.");
|
|
55
|
+
}
|
|
56
|
+
const manifest = await readAppManifest(args[0]);
|
|
57
|
+
if (manifest?.version === SAVED_VERSION) {
|
|
58
|
+
validateSavedApp(manifest);
|
|
59
|
+
output.write("Valid app " + manifest.application.id + " (2 saved agents).\n");
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const { id, capabilityCount, actionCount } = validateAppManifest(manifest);
|
|
63
|
+
output.write(`Valid app ${id} (${capabilityCount} capabilities, ${actionCount} actions).\n`);
|
|
64
|
+
}
|
package/src/runtime/local-cli.js
CHANGED
|
@@ -3,7 +3,7 @@ import { join } from "node:path";
|
|
|
3
3
|
import { parseArgs } from "node:util";
|
|
4
4
|
import { runAgent } from "./run-agent.js";
|
|
5
5
|
|
|
6
|
-
async function openStore(cwd) {
|
|
6
|
+
export async function openStore(cwd) {
|
|
7
7
|
const { DatabaseSync } = await import("node:sqlite").catch(() => {
|
|
8
8
|
throw new Error("Local agent runs require Node.js 22.13 or newer.");
|
|
9
9
|
});
|
|
@@ -58,7 +58,7 @@ async function openStore(cwd) {
|
|
|
58
58
|
};
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
function createOpenAIProvider(apiKey, fetchRequest) {
|
|
61
|
+
export function createOpenAIProvider(apiKey, fetchRequest) {
|
|
62
62
|
return {
|
|
63
63
|
async complete(request) {
|
|
64
64
|
const response = await fetchRequest("https://api.openai.com/v1/chat/completions", {
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
.lyra-run-card {
|
|
2
|
+
display: flex;
|
|
3
|
+
flex-direction: column;
|
|
4
|
+
gap: 8px;
|
|
5
|
+
min-width: 0;
|
|
6
|
+
padding: 14px;
|
|
7
|
+
border: 1px solid var(--lyra-color-border, #cbdcd1);
|
|
8
|
+
border-top: 3px solid var(--lyra-color-primary, #174b38);
|
|
9
|
+
border-radius: 12px;
|
|
10
|
+
background: var(--lyra-color-surface, #fff);
|
|
11
|
+
color: var(--lyra-color-text, #173328);
|
|
12
|
+
}
|
|
13
|
+
.lyra-run-card-clickable { min-height: 350px; padding: 20px; cursor: pointer; }
|
|
14
|
+
.lyra-run-card-heading { display: grid; gap: 3px; }
|
|
15
|
+
.lyra-run-card-source { font-size: 11px; color: var(--lyra-color-text-muted, #47675a); }
|
|
16
|
+
.lyra-run-card-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: auto; }
|
|
17
|
+
.lyra-run-card-actions .lyra-run-action {
|
|
18
|
+
display: inline-flex;
|
|
19
|
+
align-items: center;
|
|
20
|
+
justify-content: center;
|
|
21
|
+
gap: 8px;
|
|
22
|
+
min-height: 36px;
|
|
23
|
+
padding: 0 12px;
|
|
24
|
+
border: 0;
|
|
25
|
+
border-radius: 8px;
|
|
26
|
+
background: var(--lyra-color-primary, #174b38);
|
|
27
|
+
color: var(--app-on-accent, #fff);
|
|
28
|
+
font: inherit;
|
|
29
|
+
font-size: 12px;
|
|
30
|
+
font-weight: 600;
|
|
31
|
+
cursor: pointer;
|
|
32
|
+
}
|
|
33
|
+
.lyra-run-card-clickable .lyra-run-action { flex: 1; min-height: 40px; }
|
|
34
|
+
.lyra-run-action:hover:not(:disabled) { filter: brightness(.94); }
|
|
35
|
+
.lyra-run-action:disabled { opacity: .5; cursor: default; }
|
|
36
|
+
.lyra-run-header { display: flex; align-items: center; gap: 10px; }
|
|
37
|
+
.lyra-run-heading { flex: 1; min-width: 0; }
|
|
38
|
+
.lyra-run-heading-line { display: flex; align-items: center; gap: 8px; }
|
|
39
|
+
.lyra-run-header .lyra-run-title { font-size: 14px; font-weight: 650; line-height: 1.3; margin: 0; }
|
|
40
|
+
.lyra-run-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
41
|
+
.lyra-run-header .lyra-run-card-source { flex-shrink: 0; }
|
|
42
|
+
.lyra-run-close { display: inline-flex; align-items: center; justify-content: center; min-height: 32px; cursor: pointer; }
|
|
43
|
+
.rd-header .lyra-run-close { width: 32px; border: 0; border-radius: 8px; background: transparent; color: var(--lyra-color-text-muted); }
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export function renderAgentCard(h, { name, source, onRun, onOpen, header, details, extraAction, runIcon }) {
|
|
2
|
+
return h("article", { className: "lyra-run-card" + (onOpen ? " lyra-run-card-clickable" : ""), onClick: onOpen },
|
|
3
|
+
header ?? h("div", { className: "lyra-run-card-heading" },
|
|
4
|
+
h("span", { className: "lyra-run-card-source" }, source),
|
|
5
|
+
h("strong", { className: "lyra-run-title", title: name }, name)),
|
|
6
|
+
details,
|
|
7
|
+
h("div", { className: "lyra-run-card-actions" },
|
|
8
|
+
h("button", {
|
|
9
|
+
type: "button", className: "lyra-run-action", disabled: !onRun,
|
|
10
|
+
onClick: event => { event.stopPropagation(); onRun?.(); },
|
|
11
|
+
}, runIcon, "Run now"),
|
|
12
|
+
extraAction));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function renderRunHeader(h, { title, kind, icon, details, actions, onClose, closeIcon, closeId, className, style }) {
|
|
16
|
+
return h("header", { className: ["lyra-run-header", className].filter(Boolean).join(" "), style },
|
|
17
|
+
icon,
|
|
18
|
+
h("div", { className: "lyra-run-heading" },
|
|
19
|
+
title && h("div", { className: "lyra-run-heading-line" },
|
|
20
|
+
h("h2", { className: "lyra-run-title", id: closeId ? "run-title" : undefined }, title),
|
|
21
|
+
kind && h("span", { className: "lyra-run-card-source" }, kind)),
|
|
22
|
+
details),
|
|
23
|
+
actions,
|
|
24
|
+
onClose && renderRunClose(h, { onClose, closeIcon, closeId }));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function renderRunClose(h, { onClose, closeIcon, closeId }) {
|
|
28
|
+
return h("button", {
|
|
29
|
+
type: "button", className: "lyra-run-close", id: closeId,
|
|
30
|
+
onClick: onClose, title: "Close run drawer", "aria-label": "Close run drawer",
|
|
31
|
+
}, closeIcon ?? "Close");
|
|
32
|
+
}
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { renderAgentCard, renderRunHeader } from "./agent-ui.js";
|
|
2
|
+
|
|
3
|
+
const $ = selector => document.querySelector(selector);
|
|
4
|
+
const element = (tag, text) => Object.assign(document.createElement(tag), text === undefined ? {} : { textContent: text });
|
|
5
|
+
function dom(tag, props, ...children) {
|
|
6
|
+
const node = document.createElement(tag);
|
|
7
|
+
for (const [key, value] of Object.entries(props)) {
|
|
8
|
+
if (value == null) continue;
|
|
9
|
+
if (key === "onClick") node.onclick = value;
|
|
10
|
+
else if (key === "aria-label") node.setAttribute(key, value);
|
|
11
|
+
else node[key] = value;
|
|
12
|
+
}
|
|
13
|
+
node.append(...children.filter(child => child != null && child !== false && child !== ""));
|
|
14
|
+
return node;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const agentSources = { local: "Local agent", cloud: "Cloud agent", python: "Python agent" };
|
|
18
|
+
const names = { heading:"Heading", text:"Text", input:"Text input", textarea:"Text area", button:"Run button", "agent-card":"Agent card", output:"Output" };
|
|
19
|
+
let project = { layout: [], agents: [], workflows: [], teams: [], models: [] };
|
|
20
|
+
let selected, pending;
|
|
21
|
+
let dirty = false;
|
|
22
|
+
const values = new Map();
|
|
23
|
+
const outputs = new Map();
|
|
24
|
+
|
|
25
|
+
function button(text, click) {
|
|
26
|
+
const control = element("button", text);
|
|
27
|
+
control.type = "button";
|
|
28
|
+
control.onclick = click;
|
|
29
|
+
return control;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function api(path, data) {
|
|
33
|
+
const response = await fetch("/api/" + path, data === undefined ? {} : {
|
|
34
|
+
method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data),
|
|
35
|
+
});
|
|
36
|
+
const payload = await response.json();
|
|
37
|
+
if (!response.ok) throw new Error(payload.error);
|
|
38
|
+
return payload;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function announce(message) { $("#status").textContent = message; }
|
|
42
|
+
function change() { dirty = true; renderCanvas(); renderInspector(); }
|
|
43
|
+
|
|
44
|
+
function add(kind) {
|
|
45
|
+
if (!Object.hasOwn(names, kind) || project.layout.length >= 60) return;
|
|
46
|
+
const node = { id: crypto.randomUUID(), kind, label: names[kind] };
|
|
47
|
+
if (["button", "agent-card"].includes(kind)) node.targets = [];
|
|
48
|
+
project.layout.push(node);
|
|
49
|
+
selected = node.id;
|
|
50
|
+
change();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function move(node, offset) {
|
|
54
|
+
const index = project.layout.indexOf(node);
|
|
55
|
+
const next = index + offset;
|
|
56
|
+
if (next < 0 || next >= project.layout.length) return;
|
|
57
|
+
[project.layout[index], project.layout[next]] = [project.layout[next], node];
|
|
58
|
+
change();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function renderCanvas() {
|
|
62
|
+
const canvas = $("#canvas");
|
|
63
|
+
canvas.replaceChildren();
|
|
64
|
+
if (!project.layout.length) canvas.append(element("p", "Your canvas is empty."));
|
|
65
|
+
for (const node of project.layout) {
|
|
66
|
+
const card = element("div");
|
|
67
|
+
card.className = "node" + (node.id === selected ? " selected" : "");
|
|
68
|
+
const select = button("Edit " + names[node.kind], () => { selected = node.id; renderCanvas(); renderInspector(); });
|
|
69
|
+
if (!project.exported) card.append(select);
|
|
70
|
+
if (node.kind === "heading" || node.kind === "text") card.append(element(node.kind === "heading" ? "h2" : "p", node.label));
|
|
71
|
+
if (["input", "textarea"].includes(node.kind)) {
|
|
72
|
+
const id = "component-" + node.id;
|
|
73
|
+
const label = element("label", node.label);
|
|
74
|
+
label.htmlFor = id;
|
|
75
|
+
const input = element(node.kind);
|
|
76
|
+
input.id = id;
|
|
77
|
+
input.maxLength = 4000;
|
|
78
|
+
input.value = values.get(node.id) ?? "";
|
|
79
|
+
input.oninput = () => values.set(node.id, input.value);
|
|
80
|
+
card.append(label, input);
|
|
81
|
+
}
|
|
82
|
+
if (node.kind === "output") card.append(element("h2", node.label), element("pre", outputs.get(node.id) ?? "Run a workflow to see a result."));
|
|
83
|
+
if (node.kind === "agent-card") {
|
|
84
|
+
const agent = project.agents.find(item => item.id === node.targets[0]);
|
|
85
|
+
card.append(renderAgentCard(dom, {
|
|
86
|
+
name: node.label, source: agent ? agentSources[agent.source] : "Choose an agent in the inspector",
|
|
87
|
+
onRun: agent ? () => openRun(node.targets, values.get(node.sourceId) ?? "", node.outputId, node.id) : undefined,
|
|
88
|
+
}));
|
|
89
|
+
}
|
|
90
|
+
if (node.kind === "button") {
|
|
91
|
+
const trigger = button(node.label, () => {
|
|
92
|
+
if (!node.targets.length) return announce("Assign an agent or workflow in the inspector first.");
|
|
93
|
+
openRun(node.targets, values.get(node.sourceId) ?? "", node.outputId, node.id);
|
|
94
|
+
});
|
|
95
|
+
if (!node.targets.length) trigger.title = "Assign a target in the inspector";
|
|
96
|
+
trigger.className = "primary";
|
|
97
|
+
card.append(trigger);
|
|
98
|
+
}
|
|
99
|
+
canvas.append(card);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function addSelect(parent, label, choices, value, onChange) {
|
|
104
|
+
const wrap = element("label", label);
|
|
105
|
+
const select = element("select");
|
|
106
|
+
for (const [id, title] of choices) select.add(new Option(title, id));
|
|
107
|
+
select.value = value ?? "";
|
|
108
|
+
select.onchange = () => onChange(select.value);
|
|
109
|
+
wrap.append(select);
|
|
110
|
+
parent.append(wrap);
|
|
111
|
+
return select;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function labelFor(id) {
|
|
115
|
+
return project.agents.find(agent => agent.id === id)?.name ??
|
|
116
|
+
project.workflows.find(workflow => "workflow:" + workflow.id === id)?.name ?? id;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function renderInspector() {
|
|
120
|
+
const box = $("#inspector");
|
|
121
|
+
box.replaceChildren();
|
|
122
|
+
const node = project.layout.find(item => item.id === selected);
|
|
123
|
+
if (!node) { box.append(element("p", "Select a component to configure it.")); return; }
|
|
124
|
+
|
|
125
|
+
const label = element("label", "Label");
|
|
126
|
+
const field = element("input");
|
|
127
|
+
field.value = node.label;
|
|
128
|
+
field.maxLength = 160;
|
|
129
|
+
field.oninput = () => { node.label = field.value; dirty = true; renderCanvas(); };
|
|
130
|
+
label.append(field);
|
|
131
|
+
const actions = element("div");
|
|
132
|
+
actions.className = "actions";
|
|
133
|
+
actions.append(button("Move up", () => move(node, -1)), button("Move down", () => move(node, 1)), button("Remove", () => {
|
|
134
|
+
project.layout = project.layout.filter(item => item !== node);
|
|
135
|
+
for (const item of project.layout) {
|
|
136
|
+
if (item.sourceId === node.id) item.sourceId = "";
|
|
137
|
+
if (item.outputId === node.id) item.outputId = "";
|
|
138
|
+
}
|
|
139
|
+
selected = null;
|
|
140
|
+
change();
|
|
141
|
+
}));
|
|
142
|
+
box.append(label, actions);
|
|
143
|
+
if (!["button", "agent-card"].includes(node.kind)) return;
|
|
144
|
+
|
|
145
|
+
const fields = project.layout.filter(item => ["input", "textarea"].includes(item.kind));
|
|
146
|
+
const results = project.layout.filter(item => item.kind === "output");
|
|
147
|
+
addSelect(box, "Input component", [["", "Enter in run drawer"], ...fields.map(item => [item.id, item.label])], node.sourceId, id => {
|
|
148
|
+
node.sourceId = id; dirty = true;
|
|
149
|
+
});
|
|
150
|
+
addSelect(box, "Output component", [["", "Show in run drawer"], ...results.map(item => [item.id, item.label])], node.outputId, id => {
|
|
151
|
+
node.outputId = id; dirty = true;
|
|
152
|
+
});
|
|
153
|
+
if (node.kind === "agent-card") {
|
|
154
|
+
addSelect(box, "Agent", [["", "Choose an agent"], ...project.agents.map(agent => [agent.id, agent.name])], node.targets[0], id => {
|
|
155
|
+
if (node.label === names["agent-card"] || node.label === labelFor(node.targets[0])) {
|
|
156
|
+
node.label = id ? labelFor(id) : names["agent-card"];
|
|
157
|
+
}
|
|
158
|
+
node.targets = id ? [id] : [];
|
|
159
|
+
change();
|
|
160
|
+
});
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
box.append(element("h2", "Run order"));
|
|
164
|
+
node.targets.forEach((target, index) => {
|
|
165
|
+
const row = element("div", labelFor(target));
|
|
166
|
+
row.className = "binding";
|
|
167
|
+
row.append(button("Remove", () => { node.targets.splice(index, 1); change(); }));
|
|
168
|
+
box.append(row);
|
|
169
|
+
});
|
|
170
|
+
const options = [
|
|
171
|
+
...project.agents.map(agent => [agent.id, agent.name]),
|
|
172
|
+
...project.workflows.filter(item => item.runnable).map(item => ["workflow:" + item.id, item.name + " (workflow)"]),
|
|
173
|
+
];
|
|
174
|
+
addSelect(box, "Add next step", [["", "Choose agent or workflow"], ...options], "", id => {
|
|
175
|
+
if (id && node.targets.length < 8) { node.targets.push(id); change(); }
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function renderCatalog() {
|
|
180
|
+
for (const [id, rows] of [["agents", project.agents], ["workflows", project.workflows], ["teams", project.teams]]) {
|
|
181
|
+
const section = $("#" + id);
|
|
182
|
+
section.replaceChildren();
|
|
183
|
+
if (!rows.length) section.append(element("p", "None in this project."));
|
|
184
|
+
for (const row of rows) {
|
|
185
|
+
if (id === "agents") {
|
|
186
|
+
section.append(renderAgentCard(dom, { name: row.name, source: agentSources[row.source], onRun: () => openRun([row.id]) }));
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
const card = element("article");
|
|
190
|
+
card.className = "agent-card";
|
|
191
|
+
card.append(element("strong", row.name));
|
|
192
|
+
if (id === "workflows") card.append(row.runnable
|
|
193
|
+
? button("Run now", () => openRun(["workflow:" + row.id]))
|
|
194
|
+
: element("span", "Cloud workflow requires the console"));
|
|
195
|
+
if (id === "teams") card.append(element("span", "Cloud team"));
|
|
196
|
+
section.append(card);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function openRun(targets, input = "", outputId = "", buttonId) {
|
|
202
|
+
pending = { targets, outputId, buttonId };
|
|
203
|
+
$("#run-header").replaceChildren(renderRunHeader(dom, {
|
|
204
|
+
title: targets.map(labelFor).join(" → "), kind: targets.length > 1 || targets[0].startsWith("workflow:") ? "Workflow" : "Agent",
|
|
205
|
+
closeId: "close-run", onClose: () => $("#drawer").close(),
|
|
206
|
+
}));
|
|
207
|
+
$("#prompt").value = input;
|
|
208
|
+
$("#run-result").textContent = "";
|
|
209
|
+
$("#run-status").textContent = "";
|
|
210
|
+
const picker = $("#model");
|
|
211
|
+
picker.replaceChildren(...project.models.map(model => new Option(model, model)));
|
|
212
|
+
const needsLocalModel = targets.some(id => id.startsWith("local:"));
|
|
213
|
+
picker.hidden = !needsLocalModel;
|
|
214
|
+
document.querySelector('label[for="model"]').hidden = !needsLocalModel;
|
|
215
|
+
$("#drawer").showModal();
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
$("#runner").onsubmit = async event => {
|
|
219
|
+
event.preventDefault();
|
|
220
|
+
$("#run").disabled = true;
|
|
221
|
+
$("#close-run").disabled = true;
|
|
222
|
+
$("#run-status").textContent = "Running workflow…";
|
|
223
|
+
try {
|
|
224
|
+
const { answers } = await api("run", { targets: pending.targets, buttonId: pending.buttonId, input: $("#prompt").value, model: $("#model").value });
|
|
225
|
+
const text = answers.map(({ agent, output }) => agent + "\n" + output).join("\n\n");
|
|
226
|
+
$("#run-result").textContent = text;
|
|
227
|
+
if (pending.outputId) outputs.set(pending.outputId, answers.at(-1).output);
|
|
228
|
+
renderCanvas();
|
|
229
|
+
$("#run-status").textContent = "Run completed.";
|
|
230
|
+
} catch (error) {
|
|
231
|
+
$("#run-status").textContent = error.message;
|
|
232
|
+
} finally {
|
|
233
|
+
$("#run").disabled = false;
|
|
234
|
+
$("#close-run").disabled = false;
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
$("#drawer").oncancel = event => { if ($("#run").disabled) event.preventDefault(); };
|
|
238
|
+
|
|
239
|
+
async function load({ catalogOnly = false } = {}) {
|
|
240
|
+
const next = await api("project");
|
|
241
|
+
if (catalogOnly) {
|
|
242
|
+
for (const key of ["agents", "workflows", "teams", "models"]) project[key] = next[key];
|
|
243
|
+
} else {
|
|
244
|
+
project = next;
|
|
245
|
+
dirty = false;
|
|
246
|
+
}
|
|
247
|
+
$("#project-name").textContent = project.name;
|
|
248
|
+
if (project.exported) {
|
|
249
|
+
document.body.classList.add("exported");
|
|
250
|
+
document.title = project.name;
|
|
251
|
+
$("main h1").textContent = project.name;
|
|
252
|
+
} else {
|
|
253
|
+
renderCatalog();
|
|
254
|
+
renderInspector();
|
|
255
|
+
}
|
|
256
|
+
renderCanvas();
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
for (const [kind, title] of Object.entries(names)) {
|
|
260
|
+
const item = button(title, () => add(kind));
|
|
261
|
+
item.draggable = true;
|
|
262
|
+
item.ondragstart = event => event.dataTransfer.setData("text/lyra-component", kind);
|
|
263
|
+
$("#palette").append(item);
|
|
264
|
+
}
|
|
265
|
+
$("#canvas").ondragover = event => { if (event.dataTransfer.types.includes("text/lyra-component")) event.preventDefault(); };
|
|
266
|
+
$("#canvas").ondrop = event => { event.preventDefault(); add(event.dataTransfer.getData("text/lyra-component")); };
|
|
267
|
+
$("#refresh").onclick = () => load({ catalogOnly: dirty }).then(() => announce("Project refreshed.")).catch(error => announce(error.message));
|
|
268
|
+
$("#pull").onclick = async () => {
|
|
269
|
+
$("#pull").disabled = true;
|
|
270
|
+
try {
|
|
271
|
+
const result = await api("pull", {});
|
|
272
|
+
await load({ catalogOnly: true });
|
|
273
|
+
announce(result.added + " added. " + result.preserved + " existing files preserved.");
|
|
274
|
+
} catch (error) { announce(error.message); }
|
|
275
|
+
finally { $("#pull").disabled = false; }
|
|
276
|
+
};
|
|
277
|
+
$("#save").onclick = async () => {
|
|
278
|
+
$("#save").disabled = true;
|
|
279
|
+
const snapshot = JSON.stringify(project.layout);
|
|
280
|
+
try {
|
|
281
|
+
const result = await api("save", { layout: JSON.parse(snapshot), revision: project.revision });
|
|
282
|
+
project.revision = result.revision;
|
|
283
|
+
dirty = snapshot !== JSON.stringify(project.layout);
|
|
284
|
+
announce(dirty ? "Saved. You have newer canvas edits." : "Saved to manifest.json.");
|
|
285
|
+
} catch (error) { announce(error.message); }
|
|
286
|
+
finally { $("#save").disabled = false; }
|
|
287
|
+
};
|
|
288
|
+
window.addEventListener("beforeunload", event => { if (dirty) { event.preventDefault(); event.returnValue = ""; } });
|
|
289
|
+
load().catch(error => announce(error.message));
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
2
|
+
import { pythonSource } from "./python-agent.js";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { readCatalog, resolveSequence, validateLayout } from "./project.js";
|
|
5
|
+
|
|
6
|
+
const runtime = [
|
|
7
|
+
"studio/server.js", "studio/project.js", "studio/sync.js", "studio/index.html",
|
|
8
|
+
"studio/browser.js", "studio/studio.css", "studio/agent-ui.js", "studio/agent-ui.css", "app-builder/saved-app.js",
|
|
9
|
+
"runtime/local-cli.js", "runtime/run-agent.js", "studio/python-agent.js", "studio/python-agent.py",
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
export async function exportStudioApp(app, root, directory) {
|
|
13
|
+
const layout = validateLayout(app.studio.layout);
|
|
14
|
+
if (!layout.length) throw new Error("Add components to the Studio canvas and save before exporting.");
|
|
15
|
+
const catalog = await readCatalog(root);
|
|
16
|
+
const agents = new Map();
|
|
17
|
+
const workflows = new Map();
|
|
18
|
+
const buttons = layout.filter(node => ["button", "agent-card"].includes(node.kind));
|
|
19
|
+
for (const button of buttons) {
|
|
20
|
+
if ((button.sourceId && !layout.some(node => node.id === button.sourceId && ["input", "textarea"].includes(node.kind))) ||
|
|
21
|
+
(button.outputId && !layout.some(node => node.id === button.outputId && node.kind === "output"))) {
|
|
22
|
+
throw new Error("Fix the input or output binding for " + button.label + " in Studio.");
|
|
23
|
+
}
|
|
24
|
+
let sequence;
|
|
25
|
+
try {
|
|
26
|
+
sequence = resolveSequence(catalog, button.targets);
|
|
27
|
+
if (button.kind === "agent-card" && !catalog.agents.has(button.targets[0])) {
|
|
28
|
+
throw new Error("Choose one agent for this card.");
|
|
29
|
+
}
|
|
30
|
+
} catch (error) {
|
|
31
|
+
throw new Error("Fix the run targets for " + button.label + ": " + error.message);
|
|
32
|
+
}
|
|
33
|
+
for (const { agentId } of sequence) agents.set(agentId, catalog.agents.get(agentId));
|
|
34
|
+
for (const target of button.targets.filter(id => id.startsWith("workflow:"))) {
|
|
35
|
+
const workflow = catalog.workflows.find(item => "workflow:" + item.id === target);
|
|
36
|
+
workflows.set(workflow.id, {
|
|
37
|
+
id: workflow.id, name: workflow.name, runnable: true,
|
|
38
|
+
steps: workflow.steps.map(({ agentId, task, input, output }) => ({ agentId, task, input, output })),
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const name = typeof app.application?.name === "string" ? app.application.name : "Lyra app";
|
|
43
|
+
const files = await Promise.all(runtime.map(async file => [
|
|
44
|
+
"runtime/" + file, await readFile(new URL("../" + file, import.meta.url)),
|
|
45
|
+
]));
|
|
46
|
+
const pythonAgents = [...agents.values()].filter(agent => agent.source === "python");
|
|
47
|
+
const pythonFiles = new Map();
|
|
48
|
+
for (const agent of pythonAgents) {
|
|
49
|
+
if (pythonFiles.has(agent.file)) continue;
|
|
50
|
+
const path = await pythonSource(root, agent, false);
|
|
51
|
+
pythonFiles.set(agent.file, await readFile(path));
|
|
52
|
+
}
|
|
53
|
+
for (const [filename, source] of pythonFiles) files.push(["python-agents/" + filename, source]);
|
|
54
|
+
const pythonNotes = pythonAgents.length
|
|
55
|
+
? "\nInstall the Python SDK and any agent dependencies in your Python environment. Set LYRA_PYTHON to its interpreter if needed. The python-agents folder contains the source of the bound agents.\n"
|
|
56
|
+
: "";
|
|
57
|
+
files.push(
|
|
58
|
+
["LICENSE", await readFile(new URL("../../LICENSE", import.meta.url))],
|
|
59
|
+
["package.json", { name: "lyra-app", version: "1.0.0", private: true, type: "module", engines: { node: ">=22.13.0" }, scripts: { start: "node server.js" } }],
|
|
60
|
+
["manifest.json", { version: "3.0", application: { id: "lyra-app", name }, studio: { layout } }],
|
|
61
|
+
["catalog.json", { agents: [...agents.values()], workflows: [...workflows.values()] }],
|
|
62
|
+
[".gitignore", ".lyra/\n.env\n"],
|
|
63
|
+
["README.md", "# Run the exported Lyra app\n\nUse Node.js 22.13 or newer. Run `npm start` and open the loopback URL printed in the terminal.\n\nLocal agents require `OPENAI_API_KEY`. Cloud agents require `LYRA_API_KEY`, plus an OpenAI key configured in Cloud Models. Set credentials in the environment. The export includes system prompts for referenced local agents. The server is intended for local use.\n" + pythonNotes],
|
|
64
|
+
["server.js", 'import { readFile } from "node:fs/promises";\nimport { fileURLToPath } from "node:url";\nimport { startStudio } from "./runtime/studio/server.js";\n\nconst snapshot = JSON.parse(await readFile(new URL("./catalog.json", import.meta.url), "utf8"));\nconst catalog = { agents: new Map(snapshot.agents.map(agent => [agent.id, agent])), workflows: snapshot.workflows, teams: [] };\nconst { url } = await startStudio(fileURLToPath(new URL(".", import.meta.url)), { appCatalog: catalog });\nconsole.log("Lyra app: " + url);\n'],
|
|
65
|
+
);
|
|
66
|
+
try {
|
|
67
|
+
await mkdir(directory);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
if (error.code === "EEXIST") throw new Error("Choose a new directory. Existing files were preserved.");
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
for (const [file, contents] of files) {
|
|
73
|
+
const path = join(directory, file);
|
|
74
|
+
await mkdir(dirname(path), { recursive: true });
|
|
75
|
+
await writeFile(path, typeof contents === "string" || Buffer.isBuffer(contents)
|
|
76
|
+
? contents
|
|
77
|
+
: JSON.stringify(contents, null, 2) + "\n", { flag: "wx" });
|
|
78
|
+
}
|
|
79
|
+
}
|