agent-lyra 0.6.2 → 0.6.3

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.
Files changed (108) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +435 -435
  3. package/bin/agent-lyra.js +49 -49
  4. package/bin/lyra-oss-preview.js +16 -14
  5. package/package.json +60 -60
  6. package/src/app-builder/export.js +109 -109
  7. package/src/app-builder/gateway.js +62 -62
  8. package/src/app-builder/init.js +52 -52
  9. package/src/app-builder/manifest-contract.js +46 -46
  10. package/src/app-builder/native-adapter.js +29 -29
  11. package/src/app-builder/preview.js +22 -22
  12. package/src/app-builder/publish.js +14 -14
  13. package/src/app-builder/saved-app.js +70 -70
  14. package/src/app-builder/saved-preview-browser.js +33 -33
  15. package/src/app-builder/saved-preview.html +24 -24
  16. package/src/app-builder/saved-preview.js +61 -61
  17. package/src/app-builder/support-manifest.js +50 -50
  18. package/src/app-builder/support-template/manifest.json +55 -55
  19. package/src/app-builder/support-template/support-preview.html +56 -56
  20. package/src/app-builder/support-template/support-preview.js +37 -37
  21. package/src/app-builder/support-template/support-preview.mjs +67 -67
  22. package/src/app-builder/support-template/support-workflow.mjs +36 -36
  23. package/src/app-builder/use.js +37 -37
  24. package/src/app-builder/validate.js +21 -21
  25. package/src/app-builder/value-schema.js +37 -37
  26. package/src/catalog.js +147 -147
  27. package/src/coding/catalog.js +56 -56
  28. package/src/coding/cloud-catalog.js +64 -44
  29. package/src/coding/detect-stack.mjs +110 -110
  30. package/src/coding/export.js +130 -130
  31. package/src/coding/setup.js +175 -174
  32. package/src/coding/skills/lyra-android/SKILL.md +37 -0
  33. package/src/coding/skills/lyra-angular/SKILL.md +37 -0
  34. package/src/coding/skills/lyra-cucumber/SKILL.md +37 -0
  35. package/src/coding/skills/lyra-dbt/SKILL.md +37 -0
  36. package/src/coding/skills/lyra-django/SKILL.md +37 -0
  37. package/src/coding/skills/lyra-docker/SKILL.md +37 -0
  38. package/src/coding/skills/lyra-dotnet/SKILL.md +37 -0
  39. package/src/coding/skills/lyra-express/SKILL.md +37 -0
  40. package/src/coding/skills/lyra-fastapi/SKILL.md +37 -0
  41. package/src/coding/skills/lyra-flutter/SKILL.md +37 -0
  42. package/src/coding/skills/lyra-github-actions/SKILL.md +37 -0
  43. package/src/coding/skills/lyra-gitlab-ci/SKILL.md +37 -0
  44. package/src/coding/skills/lyra-go/SKILL.md +37 -0
  45. package/src/coding/skills/lyra-ios/SKILL.md +37 -0
  46. package/src/coding/skills/lyra-kubernetes/SKILL.md +37 -0
  47. package/src/coding/skills/lyra-mongodb/SKILL.md +37 -0
  48. package/src/coding/skills/lyra-mysql/SKILL.md +37 -0
  49. package/src/coding/skills/lyra-nextjs/SKILL.md +37 -0
  50. package/src/coding/skills/lyra-node/SKILL.md +37 -0
  51. package/src/coding/skills/lyra-playwright/SKILL.md +37 -0
  52. package/src/coding/skills/lyra-postgresql/SKILL.md +37 -0
  53. package/src/coding/skills/lyra-python/SKILL.md +37 -0
  54. package/src/coding/skills/lyra-react/SKILL.md +37 -0
  55. package/src/coding/skills/lyra-react-native/SKILL.md +37 -0
  56. package/src/coding/skills/lyra-redis/SKILL.md +37 -0
  57. package/src/coding/skills/lyra-rust/SKILL.md +37 -0
  58. package/src/coding/skills/lyra-spark/SKILL.md +37 -0
  59. package/src/coding/skills/lyra-spring-boot/SKILL.md +37 -0
  60. package/src/coding/skills/lyra-svelte/SKILL.md +37 -0
  61. package/src/coding/skills/lyra-terraform/SKILL.md +37 -0
  62. package/src/coding/skills/lyra-typescript/SKILL.md +37 -0
  63. package/src/coding/skills/lyra-vue/SKILL.md +37 -0
  64. package/src/gated/hostedAgents.js +56 -56
  65. package/src/gated/index.d.ts +25 -25
  66. package/src/gated/index.js +5 -5
  67. package/src/index.d.ts +2 -2
  68. package/src/index.js +2 -2
  69. package/src/init.js +965 -965
  70. package/src/memory/cli.js +21 -21
  71. package/src/memory/contract.json +28 -28
  72. package/src/memory/index.d.ts +49 -49
  73. package/src/memory/index.js +259 -259
  74. package/src/memory/lifecycle.js +29 -29
  75. package/src/memory/local-cli.js +34 -34
  76. package/src/memory/proposals.js +84 -84
  77. package/src/memory/skills-cli.js +26 -26
  78. package/src/memory/sync.js +89 -89
  79. package/src/memory/validation.js +25 -25
  80. package/src/onboardingAgents.js +532 -532
  81. package/src/promptTemplates.js +178 -178
  82. package/src/runtime/contracts.d.ts +47 -47
  83. package/src/runtime/local-cli.js +160 -160
  84. package/src/runtime/mcp-stdio.js +111 -111
  85. package/src/runtime/orchestration.js +59 -59
  86. package/src/runtime/run-agent.js +43 -43
  87. package/src/standalone/index.d.ts +45 -45
  88. package/src/standalone/index.js +13 -13
  89. package/src/standalone/init.js +12 -0
  90. package/src/standalone/orchestration.js +6 -6
  91. package/src/standalone/rag.js +113 -113
  92. package/src/studio/agent-ui.css +43 -43
  93. package/src/studio/agent-ui.js +32 -32
  94. package/src/studio/browser.js +331 -331
  95. package/src/studio/export.js +80 -80
  96. package/src/studio/index.html +37 -37
  97. package/src/studio/memory-actions.js +37 -37
  98. package/src/studio/memory-panel.js +200 -200
  99. package/src/studio/project.js +216 -216
  100. package/src/studio/pull-agent.js +72 -72
  101. package/src/studio/push.js +54 -54
  102. package/src/studio/python-agent.js +47 -47
  103. package/src/studio/python-agent.py +25 -25
  104. package/src/studio/server.js +143 -143
  105. package/src/studio/session.js +83 -83
  106. package/src/studio/studio.css +10 -10
  107. package/src/studio/sync.js +42 -42
  108. package/src/studio/workflow-sync.js +157 -157
@@ -1,83 +1,83 @@
1
- import { randomUUID } from "node:crypto";
2
- import { lstat, mkdir, readFile, rename, unlink, writeFile } from "node:fs/promises";
3
- import { homedir } from "node:os";
4
- import { join } from "node:path";
5
- import { createInterface } from "node:readline/promises";
6
- import { Writable } from "node:stream";
7
-
8
- const validKey = key => typeof key === "string" && /^lyra_sdk_[a-f0-9]{64}$/.test(key);
9
- const account = () => join(homedir(), ".agent-lyra");
10
- const workspace = root => join(root, ".lyra");
11
-
12
- async function readJson(path) {
13
- try {
14
- if (!(await lstat(path)).isFile()) throw new Error("Lyra settings must be a regular file.");
15
- return JSON.parse((await readFile(path, "utf8")).replace(/^\uFEFF/, ""));
16
- } catch (error) {
17
- if (error.code === "ENOENT") return {};
18
- throw error;
19
- }
20
- }
21
-
22
- async function saveJson(directory, name, data) {
23
- await mkdir(directory, { recursive: true, mode: 0o700 });
24
- if (!(await lstat(directory)).isDirectory()) throw new Error("Lyra settings must use a regular directory.");
25
- const target = join(directory, name);
26
- const temporary = target + "." + randomUUID() + ".tmp";
27
- await writeFile(temporary, JSON.stringify(data, null, 2) + "\n", { flag: "wx", mode: 0o600 });
28
- try {
29
- await rename(temporary, target);
30
- } finally {
31
- await unlink(temporary).catch(() => {});
32
- }
33
- }
34
-
35
- export async function readMode(root) {
36
- const { mode } = await readJson(join(workspace(root), "session.json"));
37
- return ["gated", "standalone"].includes(mode) ? mode : "automatic";
38
- }
39
-
40
- export async function readSdkKey(root, { home = account() } = {}) {
41
- const env = process.env.LYRA_API_KEY;
42
- if (validKey(env)) return env;
43
- const saved = (await readJson(join(home, "credentials.json"))).LYRA_API_KEY;
44
- if (validKey(saved)) return saved;
45
- const workspaceKey = (await readJson(join(workspace(root), "credentials.json"))).LYRA_API_KEY;
46
- return validKey(workspaceKey) ? workspaceKey : undefined;
47
- }
48
-
49
- async function promptKey() {
50
- if (!process.stdin.isTTY || !process.stdout.isTTY) {
51
- throw new Error("Run this command in your IDE terminal to enter an AgentLyra API key, or supply LYRA_API_KEY in the environment.");
52
- }
53
- const silent = new Writable({ write(_chunk, _encoding, done) { done(); } });
54
- const prompt = createInterface({ input: process.stdin, output: silent, terminal: true, historySize: 0 });
55
- const cancel = new AbortController();
56
- prompt.once("SIGINT", () => cancel.abort());
57
- process.stdout.write("Paste your AgentLyra API key and press Enter (input hidden): ");
58
- try {
59
- return (await prompt.question("", { signal: cancel.signal })).trim();
60
- } finally {
61
- prompt.close();
62
- process.stdout.write("\n");
63
- }
64
- }
65
-
66
- export async function switchMode(root, mode, args, { output = process.stdout, prompt = promptKey, home = account() } = {}) {
67
- const replace = mode === "gated" && args.length === 1 && args[0] === "--replace-key";
68
- if (args.length && !replace) throw new Error("Usage: agent-lyra work-locally | agent-lyra connect-cloud [--replace-key]");
69
- if (mode === "gated" && (replace || !(await readSdkKey(root, { home })))) {
70
- output.write("Generate an AgentLyra API key at https://console.agentlyra.in/mcp-servers\n");
71
- const key = await prompt();
72
- if (!validKey(key)) throw new Error("Enter an AgentLyra API key beginning with lyra_sdk_.");
73
- await saveJson(home, "credentials.json", { LYRA_API_KEY: key });
74
- }
75
- await saveJson(workspace(root), "session.json", { mode });
76
- output.write(mode === "gated"
77
- ? "Connected to Cloud. Open Studio and choose Pull from Cloud.\n"
78
- : "Working locally. Studio Cloud actions are paused.\n");
79
- }
80
-
81
- export function requireCloudMode(mode) {
82
- if (mode === "standalone") throw new Error("You’re working locally. Run connect-cloud in your project terminal to use Cloud resources.");
83
- }
1
+ import { randomUUID } from "node:crypto";
2
+ import { lstat, mkdir, readFile, rename, unlink, writeFile } from "node:fs/promises";
3
+ import { homedir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { createInterface } from "node:readline/promises";
6
+ import { Writable } from "node:stream";
7
+
8
+ const validKey = key => typeof key === "string" && /^lyra_sdk_[a-f0-9]{64}$/.test(key);
9
+ const account = () => join(homedir(), ".agent-lyra");
10
+ const workspace = root => join(root, ".lyra");
11
+
12
+ async function readJson(path) {
13
+ try {
14
+ if (!(await lstat(path)).isFile()) throw new Error("Lyra settings must be a regular file.");
15
+ return JSON.parse((await readFile(path, "utf8")).replace(/^\uFEFF/, ""));
16
+ } catch (error) {
17
+ if (error.code === "ENOENT") return {};
18
+ throw error;
19
+ }
20
+ }
21
+
22
+ async function saveJson(directory, name, data) {
23
+ await mkdir(directory, { recursive: true, mode: 0o700 });
24
+ if (!(await lstat(directory)).isDirectory()) throw new Error("Lyra settings must use a regular directory.");
25
+ const target = join(directory, name);
26
+ const temporary = target + "." + randomUUID() + ".tmp";
27
+ await writeFile(temporary, JSON.stringify(data, null, 2) + "\n", { flag: "wx", mode: 0o600 });
28
+ try {
29
+ await rename(temporary, target);
30
+ } finally {
31
+ await unlink(temporary).catch(() => {});
32
+ }
33
+ }
34
+
35
+ export async function readMode(root) {
36
+ const { mode } = await readJson(join(workspace(root), "session.json"));
37
+ return ["gated", "standalone"].includes(mode) ? mode : "automatic";
38
+ }
39
+
40
+ export async function readSdkKey(root, { home = account() } = {}) {
41
+ const env = process.env.LYRA_API_KEY;
42
+ if (validKey(env)) return env;
43
+ const saved = (await readJson(join(home, "credentials.json"))).LYRA_API_KEY;
44
+ if (validKey(saved)) return saved;
45
+ const workspaceKey = (await readJson(join(workspace(root), "credentials.json"))).LYRA_API_KEY;
46
+ return validKey(workspaceKey) ? workspaceKey : undefined;
47
+ }
48
+
49
+ async function promptKey() {
50
+ if (!process.stdin.isTTY || !process.stdout.isTTY) {
51
+ throw new Error("Run this command in your IDE terminal to enter an AgentLyra API key, or supply LYRA_API_KEY in the environment.");
52
+ }
53
+ const silent = new Writable({ write(_chunk, _encoding, done) { done(); } });
54
+ const prompt = createInterface({ input: process.stdin, output: silent, terminal: true, historySize: 0 });
55
+ const cancel = new AbortController();
56
+ prompt.once("SIGINT", () => cancel.abort());
57
+ process.stdout.write("Paste your AgentLyra API key and press Enter (input hidden): ");
58
+ try {
59
+ return (await prompt.question("", { signal: cancel.signal })).trim();
60
+ } finally {
61
+ prompt.close();
62
+ process.stdout.write("\n");
63
+ }
64
+ }
65
+
66
+ export async function switchMode(root, mode, args, { output = process.stdout, prompt = promptKey, home = account() } = {}) {
67
+ const replace = mode === "gated" && args.length === 1 && args[0] === "--replace-key";
68
+ if (args.length && !replace) throw new Error("Usage: agent-lyra work-locally | agent-lyra connect-cloud [--replace-key]");
69
+ if (mode === "gated" && (replace || !(await readSdkKey(root, { home })))) {
70
+ output.write("Generate an AgentLyra API key at https://console.agentlyra.in/mcp-servers\n");
71
+ const key = await prompt();
72
+ if (!validKey(key)) throw new Error("Enter an AgentLyra API key beginning with lyra_sdk_.");
73
+ await saveJson(home, "credentials.json", { LYRA_API_KEY: key });
74
+ }
75
+ await saveJson(workspace(root), "session.json", { mode });
76
+ output.write(mode === "gated"
77
+ ? "Connected to Cloud. Open Studio and choose Pull from Cloud.\n"
78
+ : "Working locally. Studio Cloud actions are paused.\n");
79
+ }
80
+
81
+ export function requireCloudMode(mode) {
82
+ if (mode === "standalone") throw new Error("You’re working locally. Run connect-cloud in your project terminal to use Cloud resources.");
83
+ }
@@ -1,10 +1,10 @@
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
- #memory-panel{width:min(48rem,100vw)}
4
- #memory-panel details{margin:1rem 0}
5
- #memory-panel pre{white-space:pre-wrap;overflow-wrap:anywhere}
6
- [hidden]{display:none!important}
7
- .exported > header,.exported main > aside,.exported main > section > p{display:none}
8
- .exported main{display:block;max-width:52rem;margin:1rem auto}
9
- .exported #canvas{border:0;background:transparent;padding:0;min-height:0}
10
- .exported .node{border:0;padding:0;margin:1rem 0}
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
+ #memory-panel{width:min(48rem,100vw)}
4
+ #memory-panel details{margin:1rem 0}
5
+ #memory-panel pre{white-space:pre-wrap;overflow-wrap:anywhere}
6
+ [hidden]{display:none!important}
7
+ .exported > header,.exported main > aside,.exported main > section > p{display:none}
8
+ .exported main{display:block;max-width:52rem;margin:1rem auto}
9
+ .exported #canvas{border:0;background:transparent;padding:0;min-height:0}
10
+ .exported .node{border:0;padding:0;margin:1rem 0}
@@ -1,42 +1,42 @@
1
- import { lstat, mkdir, writeFile } from "node:fs/promises";
2
- import { readMode, readSdkKey, requireCloudMode } from "./session.js";
3
- import { join } from "node:path";
4
-
5
- export async function pullStudio(root, { key, request = fetch } = {}) {
6
- requireCloudMode(await readMode(root));
7
- key ??= await readSdkKey(root);
8
- if (!/^lyra_sdk_[a-f0-9]{64}$/.test(key ?? "")) throw new Error("Run connect-cloud to enter your SDK key, then pull again.");
9
- const response = await request("https://api.agentlyra.in/api/sdk/v1/studio/catalog", {
10
- headers: { Authorization: "Bearer " + key }, redirect: "error", signal: AbortSignal.timeout(20_000),
11
- });
12
- if (!response.ok) throw new Error("Cloud pull failed (HTTP " + response.status + ").");
13
- const catalog = await response.json();
14
- const count = { added: 0, preserved: 0 };
15
- const base = join(root, ".lyra");
16
- await mkdir(base, { recursive: true });
17
- if (!(await lstat(base)).isDirectory()) throw new Error(".lyra must be a regular directory.");
18
-
19
- for (const [kind, rows] of Object.entries({ agents: catalog.agents, workflows: catalog.workflows, teams: catalog.teams, skills: catalog.skills ?? [] })) {
20
- if (!Array.isArray(rows) || rows.length > 100) throw new Error("Unexpected Cloud catalog response.");
21
- const folder = join(base, kind);
22
- await mkdir(folder, { recursive: true });
23
- if (!(await lstat(folder)).isDirectory()) throw new Error("Catalog paths must be regular directories.");
24
- for (const row of rows) {
25
- if (typeof row?.id !== "string" || !/^[a-f0-9]{24}$/i.test(row.id) || typeof row.name !== "string") {
26
- throw new Error("Unexpected Cloud catalog entry.");
27
- }
28
- const name = "cloud-" + row.id + (kind === "agents" ? ".md" : ".json");
29
- const content = kind === "agents"
30
- ? "---\nid: " + row.id + "\nname: " + JSON.stringify(row.name) + "\nmode: cloud\n---\n"
31
- : JSON.stringify(row, null, 2) + "\n";
32
- try {
33
- await writeFile(join(folder, name), content, { flag: "wx" });
34
- count.added++;
35
- } catch (error) {
36
- if (error.code !== "EEXIST") throw error;
37
- count.preserved++;
38
- }
39
- }
40
- }
41
- return count;
42
- }
1
+ import { lstat, mkdir, writeFile } from "node:fs/promises";
2
+ import { readMode, readSdkKey, requireCloudMode } from "./session.js";
3
+ import { join } from "node:path";
4
+
5
+ export async function pullStudio(root, { key, request = fetch } = {}) {
6
+ requireCloudMode(await readMode(root));
7
+ key ??= await readSdkKey(root);
8
+ if (!/^lyra_sdk_[a-f0-9]{64}$/.test(key ?? "")) throw new Error("Run connect-cloud to enter your SDK key, then pull again.");
9
+ const response = await request("https://api.agentlyra.in/api/sdk/v1/studio/catalog", {
10
+ headers: { Authorization: "Bearer " + key }, redirect: "error", signal: AbortSignal.timeout(20_000),
11
+ });
12
+ if (!response.ok) throw new Error("Cloud pull failed (HTTP " + response.status + ").");
13
+ const catalog = await response.json();
14
+ const count = { added: 0, preserved: 0 };
15
+ const base = join(root, ".lyra");
16
+ await mkdir(base, { recursive: true });
17
+ if (!(await lstat(base)).isDirectory()) throw new Error(".lyra must be a regular directory.");
18
+
19
+ for (const [kind, rows] of Object.entries({ agents: catalog.agents, workflows: catalog.workflows, teams: catalog.teams, skills: catalog.skills ?? [] })) {
20
+ if (!Array.isArray(rows) || rows.length > 100) throw new Error("Unexpected Cloud catalog response.");
21
+ const folder = join(base, kind);
22
+ await mkdir(folder, { recursive: true });
23
+ if (!(await lstat(folder)).isDirectory()) throw new Error("Catalog paths must be regular directories.");
24
+ for (const row of rows) {
25
+ if (typeof row?.id !== "string" || !/^[a-f0-9]{24}$/i.test(row.id) || typeof row.name !== "string") {
26
+ throw new Error("Unexpected Cloud catalog entry.");
27
+ }
28
+ const name = "cloud-" + row.id + (kind === "agents" ? ".md" : ".json");
29
+ const content = kind === "agents"
30
+ ? "---\nid: " + row.id + "\nname: " + JSON.stringify(row.name) + "\nmode: cloud\n---\n"
31
+ : JSON.stringify(row, null, 2) + "\n";
32
+ try {
33
+ await writeFile(join(folder, name), content, { flag: "wx" });
34
+ count.added++;
35
+ } catch (error) {
36
+ if (error.code !== "EEXIST") throw error;
37
+ count.preserved++;
38
+ }
39
+ }
40
+ }
41
+ return count;
42
+ }
@@ -1,157 +1,157 @@
1
- import { createHash, randomUUID } from "node:crypto";
2
- import { lstat, mkdir, readFile, readdir, rename, unlink, writeFile } from "node:fs/promises";
3
- import { join, resolve } from "node:path";
4
- import { readMode, readSdkKey, requireCloudMode } from "./session.js";
5
-
6
- const safeId = /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,95}$/;
7
- const objectId = /^[a-f0-9]{24}$/i;
8
- const digest = /^[a-f0-9]{64}$/;
9
- const hash = value => createHash("sha256").update(value).digest("hex");
10
-
11
- async function json(path) {
12
- if (!(await lstat(path)).isFile()) throw new Error("Use a regular definition or link file.");
13
- const content = await readFile(path, "utf8");
14
- return { content, value: JSON.parse(content.replace(/^\uFEFF/, "")) };
15
- }
16
-
17
- async function replace(path, content) {
18
- const temporary = path + "." + randomUUID() + ".tmp";
19
- await writeFile(temporary, content, { flag: "wx", mode: 0o600 });
20
- try { await rename(temporary, path); }
21
- finally { await unlink(temporary).catch(() => {}); }
22
- }
23
-
24
- async function cloudAgentId(base, target) {
25
- let agentId = typeof target === "string" && target.startsWith("cloud:") ? target.slice(6) : "";
26
- if (typeof target === "string" && target.startsWith("local:") && safeId.test(target.slice(6))) {
27
- const folder = join(base, "agent-links");
28
- try {
29
- if (!(await lstat(folder)).isDirectory()) throw new Error();
30
- const linked = (await json(join(folder, target.slice(6) + ".json"))).value;
31
- agentId = linked.cloudId;
32
- } catch {
33
- throw new Error("Push agent " + target.slice(6) + " to Cloud before syncing this definition.");
34
- }
35
- }
36
- if (typeof agentId !== "string" || !objectId.test(agentId)) throw new Error("Use an agent linked to Cloud.");
37
- return agentId.toLowerCase();
38
- }
39
-
40
- async function cloudDefinition(base, workflow) {
41
- if (typeof workflow.name !== "string" || !workflow.name.trim() || (workflow.description != null && typeof workflow.description !== "string") ||
42
- !Array.isArray(workflow.steps) || workflow.steps.length < 2 || workflow.steps.length > 8) {
43
- throw new Error("Use a workflow name, optional text description, and two to eight steps.");
44
- }
45
- const steps = [];
46
- for (const item of workflow.steps) {
47
- if (typeof item?.task !== "string") throw new Error("Each workflow step needs a task.");
48
- const agentId = await cloudAgentId(base, item.agentId);
49
- steps.push({ agentId, task: item.task.trim(), input: item.input ?? null, output: item.output ?? null });
50
- }
51
- return { name: workflow.name.trim(), description: workflow.description?.trim() || "Sequential workflow from Studio.", steps };
52
- }
53
-
54
- async function cloudTeam(base, team) {
55
- if (typeof team.name !== "string" || !team.name.trim() ||
56
- (team.description != null && typeof team.description !== "string") ||
57
- !Array.isArray(team.agentIds) || team.agentIds.length > 30) {
58
- throw new Error("Use a team name, an optional description, and up to 30 agent assignments.");
59
- }
60
- const ids = await Promise.all(team.agentIds.map(agent => cloudAgentId(base, agent)));
61
- return { name: team.name.trim(), description: team.description?.trim() ?? "",
62
- agentIds: [...new Set(ids)].sort() };
63
- }
64
-
65
- function cloudSkill(skill) {
66
- if (typeof skill.name !== "string" || !skill.name.trim() || skill.name.length > 64 ||
67
- typeof skill.description !== "string" || !skill.description.trim() || skill.description.length > 1024 ||
68
- typeof skill.body !== "string" || !skill.body.trim() || skill.body.length > 16000) {
69
- throw new Error("Use a text skill with a name, description, and instructions.");
70
- }
71
- return { name: skill.name.trim(), description: skill.description.trim(), body: skill.body.trim() };
72
- }
73
-
74
- async function applyRemote(resource, base, value, local, remote) {
75
- if (resource === "skill") return { ...value, ...remote };
76
- if (resource === "team") {
77
- const byCloud = new Map(await Promise.all(value.agentIds.map(async id => [await cloudAgentId(base, id), id])));
78
- return { ...value, ...remote, agentIds: remote.agentIds.map(id => byCloud.get(id) ?? "cloud:" + id) };
79
- }
80
- const byCloud = new Map(local.steps.map((step, index) => [step.agentId, value.steps[index].agentId]));
81
- return { ...value, ...remote,
82
- steps: remote.steps.map(step => ({ ...step, agentId: byCloud.get(step.agentId) ?? "cloud:" + step.agentId })) };
83
- }
84
-
85
- export function syncWorkflow(root, id, action, options) {
86
- return syncResource("workflow", root, id, action, options);
87
- }
88
-
89
- export function syncTeam(root, id, action, options) {
90
- return syncResource("team", root, id, action, options);
91
- }
92
-
93
- export function syncSkill(root, id, action, options) {
94
- return syncResource("skill", root, id, action, options);
95
- }
96
-
97
- const cloudDefinitions = { workflow: cloudDefinition, team: cloudTeam, skill: (_base, skill) => cloudSkill(skill) };
98
-
99
- async function syncResource(resource, root, id, action, { key, request = fetch } = {}) {
100
- requireCloudMode(await readMode(root));
101
- if (typeof id !== "string" || !safeId.test(id)) throw new Error("Choose a local " + resource + " ID.");
102
- const base = join(root, ".lyra");
103
- const folder = join(base, resource + "s");
104
- for (const path of [base, folder]) {
105
- if (!(await lstat(path).catch(() => null))?.isDirectory()) throw new Error("Use a regular .lyra/" + resource + "s folder.");
106
- }
107
- const matches = [];
108
- for (const entry of await readdir(folder, { withFileTypes: true })) {
109
- if (!entry.isFile() || !entry.name.endsWith(".json")) continue;
110
- const path = join(folder, entry.name);
111
- let file;
112
- try { file = await json(path); }
113
- catch (error) { if (error instanceof SyntaxError) continue; throw error; }
114
- if (file.value?.id === id && file.value.source !== "cloud") matches.push({ ...file, path });
115
- }
116
- if (matches.length !== 1) throw new Error("Choose a unique local " + resource + ".");
117
- const [{ path, content, value }] = matches;
118
- const local = await cloudDefinitions[resource](base, value);
119
- const localRevision = hash(JSON.stringify(local));
120
- const links = join(base, resource + "-links");
121
- await mkdir(links, { recursive: true });
122
- if (!(await lstat(links)).isDirectory()) throw new Error("Cloud links must use a regular directory.");
123
- const metadata = join(links, id + ".json");
124
- let link;
125
- try {
126
- link = (await json(metadata)).value;
127
- if (!digest.test(link?.linkId ?? "") || !digest.test(link?.revision ?? "") || !objectId.test(link?.cloudId ?? "")) {
128
- throw new Error("Invalid Cloud " + resource + " link.");
129
- }
130
- } catch (error) {
131
- if (error.code !== "ENOENT") throw error;
132
- }
133
- if (action === "pull" && !link) throw new Error("Push this " + resource + " once to link it to Cloud.");
134
- key ??= await readSdkKey(root);
135
- if (!key) throw new Error("Use Connect to Cloud with a Cloud sync key before syncing.");
136
- const linkId = link?.linkId ?? hash(resolve(root) + "\0" + resource + ":" + id);
137
- const push = action === "push";
138
- const response = await request("https://api.agentlyra.in/api/sdk/v1/studio/" + resource + "s/" + linkId, {
139
- method: push ? "PUT" : "GET",
140
- headers: { Authorization: "Bearer " + key, "Content-Type": "application/json" },
141
- body: push ? JSON.stringify({ [resource]: local, revision: link?.revision }) : undefined,
142
- redirect: "error", signal: AbortSignal.timeout(20_000),
143
- });
144
- const result = await response.json();
145
- if (!response.ok) throw new Error(result.error || "Could not sync the Cloud " + resource + ".");
146
- if (!objectId.test(result.id ?? "") || !digest.test(result.revision ?? "") ||
147
- (link && result.id !== link.cloudId)) throw new Error("The Cloud " + resource + " link no longer matches.");
148
- if (!push && result.revision !== link.revision && result.revision !== localRevision) {
149
- if (localRevision !== link.revision) throw new Error("Both copies changed. Review their edits before syncing.");
150
- if (!result[resource] || hash(JSON.stringify(result[resource])) !== result.revision) throw new Error("Invalid Cloud response.");
151
- const next = await applyRemote(resource, base, value, local, result[resource]);
152
- if ((await readFile(path, "utf8")) !== content) throw new Error("The local " + resource + " changed during sync. Try again.");
153
- await replace(path, JSON.stringify(next, null, 2) + "\n");
154
- }
155
- await replace(metadata, JSON.stringify({ linkId, cloudId: result.id, revision: result.revision }) + "\n");
156
- return { name: push ? local.name : result[resource].name, created: response.status === 201, updated: !push && result.revision !== localRevision && result.revision !== link.revision };
157
- }
1
+ import { createHash, randomUUID } from "node:crypto";
2
+ import { lstat, mkdir, readFile, readdir, rename, unlink, writeFile } from "node:fs/promises";
3
+ import { join, resolve } from "node:path";
4
+ import { readMode, readSdkKey, requireCloudMode } from "./session.js";
5
+
6
+ const safeId = /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,95}$/;
7
+ const objectId = /^[a-f0-9]{24}$/i;
8
+ const digest = /^[a-f0-9]{64}$/;
9
+ const hash = value => createHash("sha256").update(value).digest("hex");
10
+
11
+ async function json(path) {
12
+ if (!(await lstat(path)).isFile()) throw new Error("Use a regular definition or link file.");
13
+ const content = await readFile(path, "utf8");
14
+ return { content, value: JSON.parse(content.replace(/^\uFEFF/, "")) };
15
+ }
16
+
17
+ async function replace(path, content) {
18
+ const temporary = path + "." + randomUUID() + ".tmp";
19
+ await writeFile(temporary, content, { flag: "wx", mode: 0o600 });
20
+ try { await rename(temporary, path); }
21
+ finally { await unlink(temporary).catch(() => {}); }
22
+ }
23
+
24
+ async function cloudAgentId(base, target) {
25
+ let agentId = typeof target === "string" && target.startsWith("cloud:") ? target.slice(6) : "";
26
+ if (typeof target === "string" && target.startsWith("local:") && safeId.test(target.slice(6))) {
27
+ const folder = join(base, "agent-links");
28
+ try {
29
+ if (!(await lstat(folder)).isDirectory()) throw new Error();
30
+ const linked = (await json(join(folder, target.slice(6) + ".json"))).value;
31
+ agentId = linked.cloudId;
32
+ } catch {
33
+ throw new Error("Push agent " + target.slice(6) + " to Cloud before syncing this definition.");
34
+ }
35
+ }
36
+ if (typeof agentId !== "string" || !objectId.test(agentId)) throw new Error("Use an agent linked to Cloud.");
37
+ return agentId.toLowerCase();
38
+ }
39
+
40
+ async function cloudDefinition(base, workflow) {
41
+ if (typeof workflow.name !== "string" || !workflow.name.trim() || (workflow.description != null && typeof workflow.description !== "string") ||
42
+ !Array.isArray(workflow.steps) || workflow.steps.length < 2 || workflow.steps.length > 8) {
43
+ throw new Error("Use a workflow name, optional text description, and two to eight steps.");
44
+ }
45
+ const steps = [];
46
+ for (const item of workflow.steps) {
47
+ if (typeof item?.task !== "string") throw new Error("Each workflow step needs a task.");
48
+ const agentId = await cloudAgentId(base, item.agentId);
49
+ steps.push({ agentId, task: item.task.trim(), input: item.input ?? null, output: item.output ?? null });
50
+ }
51
+ return { name: workflow.name.trim(), description: workflow.description?.trim() || "Sequential workflow from Studio.", steps };
52
+ }
53
+
54
+ async function cloudTeam(base, team) {
55
+ if (typeof team.name !== "string" || !team.name.trim() ||
56
+ (team.description != null && typeof team.description !== "string") ||
57
+ !Array.isArray(team.agentIds) || team.agentIds.length > 30) {
58
+ throw new Error("Use a team name, an optional description, and up to 30 agent assignments.");
59
+ }
60
+ const ids = await Promise.all(team.agentIds.map(agent => cloudAgentId(base, agent)));
61
+ return { name: team.name.trim(), description: team.description?.trim() ?? "",
62
+ agentIds: [...new Set(ids)].sort() };
63
+ }
64
+
65
+ function cloudSkill(skill) {
66
+ if (typeof skill.name !== "string" || !skill.name.trim() || skill.name.length > 64 ||
67
+ typeof skill.description !== "string" || !skill.description.trim() || skill.description.length > 1024 ||
68
+ typeof skill.body !== "string" || !skill.body.trim() || skill.body.length > 16000) {
69
+ throw new Error("Use a text skill with a name, description, and instructions.");
70
+ }
71
+ return { name: skill.name.trim(), description: skill.description.trim(), body: skill.body.trim() };
72
+ }
73
+
74
+ async function applyRemote(resource, base, value, local, remote) {
75
+ if (resource === "skill") return { ...value, ...remote };
76
+ if (resource === "team") {
77
+ const byCloud = new Map(await Promise.all(value.agentIds.map(async id => [await cloudAgentId(base, id), id])));
78
+ return { ...value, ...remote, agentIds: remote.agentIds.map(id => byCloud.get(id) ?? "cloud:" + id) };
79
+ }
80
+ const byCloud = new Map(local.steps.map((step, index) => [step.agentId, value.steps[index].agentId]));
81
+ return { ...value, ...remote,
82
+ steps: remote.steps.map(step => ({ ...step, agentId: byCloud.get(step.agentId) ?? "cloud:" + step.agentId })) };
83
+ }
84
+
85
+ export function syncWorkflow(root, id, action, options) {
86
+ return syncResource("workflow", root, id, action, options);
87
+ }
88
+
89
+ export function syncTeam(root, id, action, options) {
90
+ return syncResource("team", root, id, action, options);
91
+ }
92
+
93
+ export function syncSkill(root, id, action, options) {
94
+ return syncResource("skill", root, id, action, options);
95
+ }
96
+
97
+ const cloudDefinitions = { workflow: cloudDefinition, team: cloudTeam, skill: (_base, skill) => cloudSkill(skill) };
98
+
99
+ async function syncResource(resource, root, id, action, { key, request = fetch } = {}) {
100
+ requireCloudMode(await readMode(root));
101
+ if (typeof id !== "string" || !safeId.test(id)) throw new Error("Choose a local " + resource + " ID.");
102
+ const base = join(root, ".lyra");
103
+ const folder = join(base, resource + "s");
104
+ for (const path of [base, folder]) {
105
+ if (!(await lstat(path).catch(() => null))?.isDirectory()) throw new Error("Use a regular .lyra/" + resource + "s folder.");
106
+ }
107
+ const matches = [];
108
+ for (const entry of await readdir(folder, { withFileTypes: true })) {
109
+ if (!entry.isFile() || !entry.name.endsWith(".json")) continue;
110
+ const path = join(folder, entry.name);
111
+ let file;
112
+ try { file = await json(path); }
113
+ catch (error) { if (error instanceof SyntaxError) continue; throw error; }
114
+ if (file.value?.id === id && file.value.source !== "cloud") matches.push({ ...file, path });
115
+ }
116
+ if (matches.length !== 1) throw new Error("Choose a unique local " + resource + ".");
117
+ const [{ path, content, value }] = matches;
118
+ const local = await cloudDefinitions[resource](base, value);
119
+ const localRevision = hash(JSON.stringify(local));
120
+ const links = join(base, resource + "-links");
121
+ await mkdir(links, { recursive: true });
122
+ if (!(await lstat(links)).isDirectory()) throw new Error("Cloud links must use a regular directory.");
123
+ const metadata = join(links, id + ".json");
124
+ let link;
125
+ try {
126
+ link = (await json(metadata)).value;
127
+ if (!digest.test(link?.linkId ?? "") || !digest.test(link?.revision ?? "") || !objectId.test(link?.cloudId ?? "")) {
128
+ throw new Error("Invalid Cloud " + resource + " link.");
129
+ }
130
+ } catch (error) {
131
+ if (error.code !== "ENOENT") throw error;
132
+ }
133
+ if (action === "pull" && !link) throw new Error("Push this " + resource + " once to link it to Cloud.");
134
+ key ??= await readSdkKey(root);
135
+ if (!key) throw new Error("Use Connect to Cloud with a Cloud sync key before syncing.");
136
+ const linkId = link?.linkId ?? hash(resolve(root) + "\0" + resource + ":" + id);
137
+ const push = action === "push";
138
+ const response = await request("https://api.agentlyra.in/api/sdk/v1/studio/" + resource + "s/" + linkId, {
139
+ method: push ? "PUT" : "GET",
140
+ headers: { Authorization: "Bearer " + key, "Content-Type": "application/json" },
141
+ body: push ? JSON.stringify({ [resource]: local, revision: link?.revision }) : undefined,
142
+ redirect: "error", signal: AbortSignal.timeout(20_000),
143
+ });
144
+ const result = await response.json();
145
+ if (!response.ok) throw new Error(result.error || "Could not sync the Cloud " + resource + ".");
146
+ if (!objectId.test(result.id ?? "") || !digest.test(result.revision ?? "") ||
147
+ (link && result.id !== link.cloudId)) throw new Error("The Cloud " + resource + " link no longer matches.");
148
+ if (!push && result.revision !== link.revision && result.revision !== localRevision) {
149
+ if (localRevision !== link.revision) throw new Error("Both copies changed. Review their edits before syncing.");
150
+ if (!result[resource] || hash(JSON.stringify(result[resource])) !== result.revision) throw new Error("Invalid Cloud response.");
151
+ const next = await applyRemote(resource, base, value, local, result[resource]);
152
+ if ((await readFile(path, "utf8")) !== content) throw new Error("The local " + resource + " changed during sync. Try again.");
153
+ await replace(path, JSON.stringify(next, null, 2) + "\n");
154
+ }
155
+ await replace(metadata, JSON.stringify({ linkId, cloudId: result.id, revision: result.revision }) + "\n");
156
+ return { name: push ? local.name : result[resource].name, created: response.status === 201, updated: !push && result.revision !== localRevision && result.revision !== link.revision };
157
+ }