agent-lyra 0.8.0 → 0.10.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.
Files changed (126) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +301 -291
  3. package/bin/agent-lyra.js +51 -51
  4. package/bin/lyra-oss-preview.js +16 -16
  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 +115 -72
  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 +35 -31
  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 +31 -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 -64
  29. package/src/coding/detect-stack.mjs +110 -110
  30. package/src/coding/export.js +133 -133
  31. package/src/coding/setup.js +175 -175
  32. package/src/coding/skills/lyra-android/SKILL.md +37 -37
  33. package/src/coding/skills/lyra-angular/SKILL.md +37 -37
  34. package/src/coding/skills/lyra-cucumber/SKILL.md +37 -37
  35. package/src/coding/skills/lyra-dbt/SKILL.md +37 -37
  36. package/src/coding/skills/lyra-django/SKILL.md +37 -37
  37. package/src/coding/skills/lyra-docker/SKILL.md +37 -37
  38. package/src/coding/skills/lyra-dotnet/SKILL.md +37 -37
  39. package/src/coding/skills/lyra-express/SKILL.md +37 -37
  40. package/src/coding/skills/lyra-fastapi/SKILL.md +37 -37
  41. package/src/coding/skills/lyra-flutter/SKILL.md +37 -37
  42. package/src/coding/skills/lyra-github-actions/SKILL.md +37 -37
  43. package/src/coding/skills/lyra-gitlab-ci/SKILL.md +37 -37
  44. package/src/coding/skills/lyra-go/SKILL.md +37 -37
  45. package/src/coding/skills/lyra-ios/SKILL.md +37 -37
  46. package/src/coding/skills/lyra-kubernetes/SKILL.md +37 -37
  47. package/src/coding/skills/lyra-mongodb/SKILL.md +37 -37
  48. package/src/coding/skills/lyra-mysql/SKILL.md +37 -37
  49. package/src/coding/skills/lyra-nextjs/SKILL.md +37 -37
  50. package/src/coding/skills/lyra-node/SKILL.md +37 -37
  51. package/src/coding/skills/lyra-playwright/SKILL.md +37 -37
  52. package/src/coding/skills/lyra-postgresql/SKILL.md +37 -37
  53. package/src/coding/skills/lyra-python/SKILL.md +37 -37
  54. package/src/coding/skills/lyra-react/SKILL.md +37 -37
  55. package/src/coding/skills/lyra-react-native/SKILL.md +37 -37
  56. package/src/coding/skills/lyra-redis/SKILL.md +37 -37
  57. package/src/coding/skills/lyra-rust/SKILL.md +37 -37
  58. package/src/coding/skills/lyra-spark/SKILL.md +37 -37
  59. package/src/coding/skills/lyra-spring-boot/SKILL.md +37 -37
  60. package/src/coding/skills/lyra-svelte/SKILL.md +37 -37
  61. package/src/coding/skills/lyra-terraform/SKILL.md +37 -37
  62. package/src/coding/skills/lyra-typescript/SKILL.md +37 -37
  63. package/src/coding/skills/lyra-vue/SKILL.md +37 -37
  64. package/src/framework/agent.js +181 -181
  65. package/src/framework/mda.js +65 -65
  66. package/src/framework/run.py +111 -111
  67. package/src/gated/hostedAgents.js +56 -56
  68. package/src/gated/index.d.ts +25 -25
  69. package/src/gated/index.js +5 -5
  70. package/src/index.d.ts +2 -2
  71. package/src/index.js +2 -2
  72. package/src/init.js +960 -960
  73. package/src/intelligence/graphsnip-provider.d.ts +5 -5
  74. package/src/intelligence/graphsnip-provider.js +57 -57
  75. package/src/intelligence/lifecycle.js +25 -25
  76. package/src/memory/LICENSE +21 -21
  77. package/src/memory/README.md +5 -5
  78. package/src/memory/UPSTREAM.json +24 -24
  79. package/src/memory/bridge-cli.js +16 -16
  80. package/src/memory/bridge.js +18 -18
  81. package/src/memory/cli.js +21 -21
  82. package/src/memory/cloud-sync-actions.js +61 -61
  83. package/src/memory/contract.json +45 -45
  84. package/src/memory/index.d.ts +50 -50
  85. package/src/memory/index.js +7 -7
  86. package/src/memory/lifecycle.js +29 -29
  87. package/src/memory/local-cli.js +34 -34
  88. package/src/memory/local-store.js +209 -209
  89. package/src/memory/package.json +33 -33
  90. package/src/memory/proposals.js +90 -90
  91. package/src/memory/skills-cli.js +26 -26
  92. package/src/memory/standalone.d.ts +50 -50
  93. package/src/memory/standalone.js +1 -1
  94. package/src/memory/sync.js +89 -89
  95. package/src/memory/validation.js +25 -25
  96. package/src/onboardingAgents.js +532 -532
  97. package/src/promptTemplates.js +178 -178
  98. package/src/resource-files.js +18 -18
  99. package/src/runtime/contracts.d.ts +47 -47
  100. package/src/runtime/local-cli.js +161 -161
  101. package/src/runtime/mcp-stdio.js +111 -111
  102. package/src/runtime/orchestration.js +59 -59
  103. package/src/runtime/run-agent.js +54 -54
  104. package/src/standalone/index.d.ts +45 -45
  105. package/src/standalone/index.js +13 -13
  106. package/src/standalone/init.js +12 -12
  107. package/src/standalone/orchestration.js +6 -6
  108. package/src/standalone/rag.js +113 -113
  109. package/src/studio/agent-ui.css +43 -43
  110. package/src/studio/agent-ui.js +32 -32
  111. package/src/studio/browser.js +542 -345
  112. package/src/studio/export.js +83 -86
  113. package/src/studio/index.html +74 -37
  114. package/src/studio/memory-actions.js +37 -37
  115. package/src/studio/memory-panel.js +200 -200
  116. package/src/studio/project.js +308 -240
  117. package/src/studio/pull-agent.js +72 -72
  118. package/src/studio/push.js +54 -54
  119. package/src/studio/python-agent.js +47 -47
  120. package/src/studio/python-agent.py +25 -25
  121. package/src/studio/server.js +164 -150
  122. package/src/studio/session.js +83 -83
  123. package/src/studio/studio.css +257 -10
  124. package/src/studio/sync.js +42 -42
  125. package/src/studio/workflow-editor.js +240 -0
  126. package/src/studio/workflow-sync.js +229 -157
@@ -1,181 +1,181 @@
1
- import { execFile } from "node:child_process";
2
- import { createHash, randomUUID } from "node:crypto";
3
- import { lstat, mkdir, readFile, rename, unlink, writeFile } from "node:fs/promises";
4
- import { join } from "node:path";
5
- import { fileURLToPath } from "node:url";
6
- import { runManagedDeepAgent } from "./mda.js";
7
- import { openStore } from "../runtime/local-cli.js";
8
- import { readMode, readSdkKey, requireCloudMode } from "../studio/session.js";
9
-
10
- const uuid = /^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/;
11
- const entry = /^[A-Za-z_][A-Za-z0-9_]*(?:\/[A-Za-z_][A-Za-z0-9_]*)*\.py:[A-Za-z_][A-Za-z0-9_]*$/;
12
- const mdaEntry = /^[A-Za-z_][A-Za-z0-9_-]*(?:\/[A-Za-z_][A-Za-z0-9_-]*)*\.ts:[A-Za-z_][A-Za-z0-9_]*$/;
13
- const assistantIdPattern = /^[A-Za-z0-9][A-Za-z0-9_-]{0,119}$/;
14
- const missing = error => {
15
- if (error.code !== "ENOENT") throw error;
16
- return null;
17
- };
18
-
19
- export function frameworkDescriptor(value) {
20
- const mda = value?.framework === "managed-deepagents";
21
- if (value?.schemaVersion !== 1 || !["langchain", "langgraph", "google-adk", "openai-agents", "custom", "managed-deepagents"].includes(value.framework) ||
22
- typeof value.id !== "string" || value.id.length !== 36 || !uuid.test(value.id) || typeof value.entry !== "string" ||
23
- value.entry.length > 240 || !(mda ? mdaEntry : entry).test(value.entry) || typeof value.name !== "string" ||
24
- !value.name.trim() || value.name.length > 120 || /[\x00-\x1f\x7f]/.test(value.name)) return null;
25
- const { input = "text", output = "text" } = value;
26
- const kinds = value.framework === "custom" ? ["text", "json"] : ["text"];
27
- if (!kinds.includes(input) || !kinds.includes(output)) return null;
28
- if (mda && (typeof value.assistantId !== "string" || !assistantIdPattern.test(value.assistantId))) return null;
29
- return {
30
- schemaVersion: 1, id: value.id, name: value.name.trim(), framework: value.framework,
31
- entry: value.entry, input, output, ...(mda ? { assistantId: value.assistantId } : {}),
32
- };
33
- }
34
-
35
- async function directory(path, create) {
36
- if (create) await mkdir(path).catch(error => { if (error.code !== "EEXIST") throw error; });
37
- if (!(await lstat(path)).isDirectory()) throw new Error("Use regular project folders for agent registrations.");
38
- return path;
39
- }
40
-
41
- async function registrationDirectory(root, folder, create) {
42
- await directory(root, false);
43
- return directory(join(await directory(join(root, ".lyra"), create), folder), create);
44
- }
45
-
46
- export async function readFrameworkAgent(root, id) {
47
- if (typeof id !== "string" || id.length !== 36 || !uuid.test(id)) return null;
48
- const path = join(await directory(join(await registrationDirectory(root, "agents", false), "coding"), false), id + ".json");
49
- const stat = await lstat(path).catch(missing);
50
- if (!stat?.isFile() || stat.size > 8_192) return null;
51
- const value = frameworkDescriptor(JSON.parse((await readFile(path, "utf8")).replace(/^\uFEFF/, "")));
52
- return value?.id === id ? value : null;
53
- }
54
-
55
- async function sourceFile(root, value) {
56
- if (typeof value?.entry !== "string" || !(value.framework === "managed-deepagents" ? mdaEntry : entry).test(value.entry)) throw new Error("Invalid local framework entry point.");
57
- const [file] = value.entry.split(":");
58
- const parts = file.split("/");
59
- let folder = root;
60
- await directory(folder, false);
61
- for (const part of parts.slice(0, -1)) folder = await directory(join(folder, part), false);
62
- const path = join(folder, parts.at(-1));
63
- if (!(await lstat(path)).isFile()) throw new Error("The agent entry must be a regular source file in this project.");
64
- return path;
65
- }
66
-
67
- export async function discoverFrameworkAgents(root) {
68
- const path = join(root, "langgraph.json");
69
- const stat = await lstat(path).catch(missing);
70
- if (!stat?.isFile() || stat.size > 65_536) return [];
71
- const { graphs } = JSON.parse((await readFile(path, "utf8")).replace(/^\uFEFF/, ""));
72
- return Object.entries(graphs ?? {}).filter(([name, value]) =>
73
- name.trim() && name.length <= 120 && !/[\x00-\x1f\x7f]/.test(name) &&
74
- typeof value === "string" && value.length <= 240 && entry.test(value.replace(/^\.\//, "")))
75
- .map(([name, value]) => ({ name, framework: "langgraph", entry: value.replace(/^\.\//, "") }));
76
- }
77
-
78
- export function publicRegistration(agent) {
79
- const { id, name, framework, input = "text", output = "text" } = agent;
80
- return { schemaVersion: 1, id, name, framework, input, output, execution: "local" };
81
- }
82
-
83
- export async function syncFrameworkAgent(root, id, confirmation, { request = fetch, key } = {}) {
84
- requireCloudMode(await readMode(root));
85
- const agent = await readFrameworkAgent(root, id);
86
- if (!agent) throw new Error("Register this agent before syncing.");
87
- const payload = publicRegistration(agent);
88
- const fingerprint = createHash("sha256").update(JSON.stringify(payload)).digest("hex");
89
- if (confirmation === undefined) return { payload, fingerprint };
90
- if (confirmation !== fingerprint) throw new Error("Registration changed. Preview it before syncing.");
91
- key ??= await readSdkKey(root);
92
- if (!key) throw new Error("Connect to Cloud with an API key that has resource sync permission.");
93
- const path = join(await registrationDirectory(root, "framework-links", true), id + ".json");
94
- const stat = await lstat(path).catch(missing);
95
- if (stat && !stat.isFile()) throw new Error("Agent sync link must be a regular file.");
96
- const prior = stat ? JSON.parse(await readFile(path, "utf8")) : null;
97
- const baseRevision = prior?.revision ?? 0;
98
- if (!Number.isSafeInteger(baseRevision) || baseRevision < 0) throw new Error("Invalid local sync revision.");
99
- const response = await request("https://api.agentlyra.in/api/sdk/v1/framework-agents/" + id, {
100
- method: "PUT", redirect: "error", signal: AbortSignal.timeout(15_000),
101
- headers: { Authorization: "Bearer " + key, "Content-Type": "application/json" },
102
- body: JSON.stringify({ ...payload, baseRevision }),
103
- });
104
- if (response.status === 409) throw new Error("Cloud has a newer registration. Review the project link before retrying.");
105
- if (!response.ok) throw new Error("Could not sync the registration. Check your Cloud resource sync permission.");
106
- const result = await response.json();
107
- if (result.id !== id || !Number.isSafeInteger(result.revision) || result.revision < 1) throw new Error("Invalid Cloud registration response.");
108
- const temp = path + "." + randomUUID() + ".tmp";
109
- try {
110
- await writeFile(temp, JSON.stringify({ revision: result.revision }), { flag: "wx" });
111
- await rename(temp, path);
112
- } finally { await unlink(temp).catch(missing); }
113
- return result;
114
- }
115
-
116
- export async function runFrameworkAgent(root, agent, input, traceId) {
117
- const path = await sourceFile(root, agent);
118
- if (agent.framework === "managed-deepagents") return runManagedDeepAgent(agent, input, traceId);
119
- const python = process.env.LYRA_PYTHON || (process.platform === "win32" ? "py.exe" : "python3");
120
- const runner = fileURLToPath(new URL("./run.py", import.meta.url));
121
- return new Promise((resolve, reject) => {
122
- const child = execFile(python, [runner, path, agent.entry.split(":")[1], traceId, agent.framework, agent.output], {
123
- cwd: root, timeout: 60_000, maxBuffer: 1024 * 1024, windowsHide: true,
124
- }, (error, stdout) => {
125
- if (error) {
126
- if (error.code === 20 || error.code === 21) {
127
- const failure = new Error(error.code === 20 ? "Install the framework in the selected Python environment." : "Framework agent returned an invalid output.");
128
- failure.code = error.code === 20 ? "FRAMEWORK_DEPENDENCY" : "FRAMEWORK_OUTPUT";
129
- return reject(failure);
130
- }
131
- const failed = new Error(error.killed ? "Framework agent timed out." : "Framework run failed. Check the entry point and Python environment.");
132
- if (error.killed) failed.code = "FRAMEWORK_TIMEOUT";
133
- return reject(failed);
134
- }
135
- try {
136
- const { output } = JSON.parse(stdout);
137
- if (typeof output !== "string") throw new Error();
138
- resolve(output);
139
- } catch { reject(new Error("Framework agent returned an invalid text response.")); }
140
- });
141
- child.stdin.on("error", () => {});
142
- child.stdin.end(JSON.stringify({ input }));
143
- });
144
- }
145
-
146
- export async function runFrameworkCli(args, output = process.stdout, root = process.cwd()) {
147
- const [action, value, confirm] = args;
148
- if (!action || action === "--help" || value === "--help") {
149
- output.write("Usage: agent-lyra agent discover | register <descriptor.json> | sync <id> [--yes] | trace <run-id>\n");
150
- return;
151
- }
152
- if (action === "discover" && !value) {
153
- output.write(JSON.stringify(await discoverFrameworkAgents(root), null, 2) + "\n");
154
- return;
155
- }
156
- if (action === "register" && value && !confirm && args.length === 2) {
157
- const source = JSON.parse((await readFile(value, "utf8")).replace(/^\uFEFF/, ""));
158
- const agent = frameworkDescriptor({ ...source, schemaVersion: 1, id: source.id ?? randomUUID() });
159
- if (!agent) throw new Error("Provide a supported framework, name, and source entry. For managed-deepagents use relative/file.ts:export and provide assistantId.");
160
- await sourceFile(root, agent);
161
- const path = join(await directory(join(await registrationDirectory(root, "agents", true), "coding"), true), agent.id + ".json");
162
- await writeFile(path, JSON.stringify(agent, null, 2) + "\n", { flag: "wx" });
163
- output.write(JSON.stringify({ id: agent.id, name: agent.name, file: ".lyra/agents/coding/" + agent.id + ".json" }) + "\n");
164
- return;
165
- }
166
- if (action === "trace" && typeof value === "string" && value.length === 36 && uuid.test(value) && args.length === 2) {
167
- const store = await openStore(root);
168
- try {
169
- const events = await store.list(value);
170
- if (!events[0]?.data.framework) throw new Error("No framework run with that ID in this project.");
171
- output.write(JSON.stringify(events, null, 2) + "\n");
172
- } finally { store.close(); }
173
- return;
174
- }
175
- if (action === "sync" && value && (!confirm || confirm === "--yes") && args.length <= 3) {
176
- const preview = await syncFrameworkAgent(root, value);
177
- output.write(JSON.stringify(confirm ? await syncFrameworkAgent(root, value, preview.fingerprint) : preview.payload, null, 2) + "\n");
178
- return;
179
- }
180
- throw new Error("Use agent-lyra agent --help.");
181
- }
1
+ import { execFile } from "node:child_process";
2
+ import { createHash, randomUUID } from "node:crypto";
3
+ import { lstat, mkdir, readFile, rename, unlink, writeFile } from "node:fs/promises";
4
+ import { join } from "node:path";
5
+ import { fileURLToPath } from "node:url";
6
+ import { runManagedDeepAgent } from "./mda.js";
7
+ import { openStore } from "../runtime/local-cli.js";
8
+ import { readMode, readSdkKey, requireCloudMode } from "../studio/session.js";
9
+
10
+ const uuid = /^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/;
11
+ const entry = /^[A-Za-z_][A-Za-z0-9_]*(?:\/[A-Za-z_][A-Za-z0-9_]*)*\.py:[A-Za-z_][A-Za-z0-9_]*$/;
12
+ const mdaEntry = /^[A-Za-z_][A-Za-z0-9_-]*(?:\/[A-Za-z_][A-Za-z0-9_-]*)*\.ts:[A-Za-z_][A-Za-z0-9_]*$/;
13
+ const assistantIdPattern = /^[A-Za-z0-9][A-Za-z0-9_-]{0,119}$/;
14
+ const missing = error => {
15
+ if (error.code !== "ENOENT") throw error;
16
+ return null;
17
+ };
18
+
19
+ export function frameworkDescriptor(value) {
20
+ const mda = value?.framework === "managed-deepagents";
21
+ if (value?.schemaVersion !== 1 || !["langchain", "langgraph", "google-adk", "openai-agents", "custom", "managed-deepagents"].includes(value.framework) ||
22
+ typeof value.id !== "string" || value.id.length !== 36 || !uuid.test(value.id) || typeof value.entry !== "string" ||
23
+ value.entry.length > 240 || !(mda ? mdaEntry : entry).test(value.entry) || typeof value.name !== "string" ||
24
+ !value.name.trim() || value.name.length > 120 || /[\x00-\x1f\x7f]/.test(value.name)) return null;
25
+ const { input = "text", output = "text" } = value;
26
+ const kinds = value.framework === "custom" ? ["text", "json"] : ["text"];
27
+ if (!kinds.includes(input) || !kinds.includes(output)) return null;
28
+ if (mda && (typeof value.assistantId !== "string" || !assistantIdPattern.test(value.assistantId))) return null;
29
+ return {
30
+ schemaVersion: 1, id: value.id, name: value.name.trim(), framework: value.framework,
31
+ entry: value.entry, input, output, ...(mda ? { assistantId: value.assistantId } : {}),
32
+ };
33
+ }
34
+
35
+ async function directory(path, create) {
36
+ if (create) await mkdir(path).catch(error => { if (error.code !== "EEXIST") throw error; });
37
+ if (!(await lstat(path)).isDirectory()) throw new Error("Use regular project folders for agent registrations.");
38
+ return path;
39
+ }
40
+
41
+ async function registrationDirectory(root, folder, create) {
42
+ await directory(root, false);
43
+ return directory(join(await directory(join(root, ".lyra"), create), folder), create);
44
+ }
45
+
46
+ export async function readFrameworkAgent(root, id) {
47
+ if (typeof id !== "string" || id.length !== 36 || !uuid.test(id)) return null;
48
+ const path = join(await directory(join(await registrationDirectory(root, "agents", false), "coding"), false), id + ".json");
49
+ const stat = await lstat(path).catch(missing);
50
+ if (!stat?.isFile() || stat.size > 8_192) return null;
51
+ const value = frameworkDescriptor(JSON.parse((await readFile(path, "utf8")).replace(/^\uFEFF/, "")));
52
+ return value?.id === id ? value : null;
53
+ }
54
+
55
+ async function sourceFile(root, value) {
56
+ if (typeof value?.entry !== "string" || !(value.framework === "managed-deepagents" ? mdaEntry : entry).test(value.entry)) throw new Error("Invalid local framework entry point.");
57
+ const [file] = value.entry.split(":");
58
+ const parts = file.split("/");
59
+ let folder = root;
60
+ await directory(folder, false);
61
+ for (const part of parts.slice(0, -1)) folder = await directory(join(folder, part), false);
62
+ const path = join(folder, parts.at(-1));
63
+ if (!(await lstat(path)).isFile()) throw new Error("The agent entry must be a regular source file in this project.");
64
+ return path;
65
+ }
66
+
67
+ export async function discoverFrameworkAgents(root) {
68
+ const path = join(root, "langgraph.json");
69
+ const stat = await lstat(path).catch(missing);
70
+ if (!stat?.isFile() || stat.size > 65_536) return [];
71
+ const { graphs } = JSON.parse((await readFile(path, "utf8")).replace(/^\uFEFF/, ""));
72
+ return Object.entries(graphs ?? {}).filter(([name, value]) =>
73
+ name.trim() && name.length <= 120 && !/[\x00-\x1f\x7f]/.test(name) &&
74
+ typeof value === "string" && value.length <= 240 && entry.test(value.replace(/^\.\//, "")))
75
+ .map(([name, value]) => ({ name, framework: "langgraph", entry: value.replace(/^\.\//, "") }));
76
+ }
77
+
78
+ export function publicRegistration(agent) {
79
+ const { id, name, framework, input = "text", output = "text" } = agent;
80
+ return { schemaVersion: 1, id, name, framework, input, output, execution: "local" };
81
+ }
82
+
83
+ export async function syncFrameworkAgent(root, id, confirmation, { request = fetch, key } = {}) {
84
+ requireCloudMode(await readMode(root));
85
+ const agent = await readFrameworkAgent(root, id);
86
+ if (!agent) throw new Error("Register this agent before syncing.");
87
+ const payload = publicRegistration(agent);
88
+ const fingerprint = createHash("sha256").update(JSON.stringify(payload)).digest("hex");
89
+ if (confirmation === undefined) return { payload, fingerprint };
90
+ if (confirmation !== fingerprint) throw new Error("Registration changed. Preview it before syncing.");
91
+ key ??= await readSdkKey(root);
92
+ if (!key) throw new Error("Connect to Cloud with an API key that has resource sync permission.");
93
+ const path = join(await registrationDirectory(root, "framework-links", true), id + ".json");
94
+ const stat = await lstat(path).catch(missing);
95
+ if (stat && !stat.isFile()) throw new Error("Agent sync link must be a regular file.");
96
+ const prior = stat ? JSON.parse(await readFile(path, "utf8")) : null;
97
+ const baseRevision = prior?.revision ?? 0;
98
+ if (!Number.isSafeInteger(baseRevision) || baseRevision < 0) throw new Error("Invalid local sync revision.");
99
+ const response = await request("https://api.agentlyra.in/api/sdk/v1/framework-agents/" + id, {
100
+ method: "PUT", redirect: "error", signal: AbortSignal.timeout(15_000),
101
+ headers: { Authorization: "Bearer " + key, "Content-Type": "application/json" },
102
+ body: JSON.stringify({ ...payload, baseRevision }),
103
+ });
104
+ if (response.status === 409) throw new Error("Cloud has a newer registration. Review the project link before retrying.");
105
+ if (!response.ok) throw new Error("Could not sync the registration. Check your Cloud resource sync permission.");
106
+ const result = await response.json();
107
+ if (result.id !== id || !Number.isSafeInteger(result.revision) || result.revision < 1) throw new Error("Invalid Cloud registration response.");
108
+ const temp = path + "." + randomUUID() + ".tmp";
109
+ try {
110
+ await writeFile(temp, JSON.stringify({ revision: result.revision }), { flag: "wx" });
111
+ await rename(temp, path);
112
+ } finally { await unlink(temp).catch(missing); }
113
+ return result;
114
+ }
115
+
116
+ export async function runFrameworkAgent(root, agent, input, traceId) {
117
+ const path = await sourceFile(root, agent);
118
+ if (agent.framework === "managed-deepagents") return runManagedDeepAgent(agent, input, traceId);
119
+ const python = process.env.LYRA_PYTHON || (process.platform === "win32" ? "py.exe" : "python3");
120
+ const runner = fileURLToPath(new URL("./run.py", import.meta.url));
121
+ return new Promise((resolve, reject) => {
122
+ const child = execFile(python, [runner, path, agent.entry.split(":")[1], traceId, agent.framework, agent.output], {
123
+ cwd: root, timeout: 60_000, maxBuffer: 1024 * 1024, windowsHide: true,
124
+ }, (error, stdout) => {
125
+ if (error) {
126
+ if (error.code === 20 || error.code === 21) {
127
+ const failure = new Error(error.code === 20 ? "Install the framework in the selected Python environment." : "Framework agent returned an invalid output.");
128
+ failure.code = error.code === 20 ? "FRAMEWORK_DEPENDENCY" : "FRAMEWORK_OUTPUT";
129
+ return reject(failure);
130
+ }
131
+ const failed = new Error(error.killed ? "Framework agent timed out." : "Framework run failed. Check the entry point and Python environment.");
132
+ if (error.killed) failed.code = "FRAMEWORK_TIMEOUT";
133
+ return reject(failed);
134
+ }
135
+ try {
136
+ const { output } = JSON.parse(stdout);
137
+ if (typeof output !== "string") throw new Error();
138
+ resolve(output);
139
+ } catch { reject(new Error("Framework agent returned an invalid text response.")); }
140
+ });
141
+ child.stdin.on("error", () => {});
142
+ child.stdin.end(JSON.stringify({ input }));
143
+ });
144
+ }
145
+
146
+ export async function runFrameworkCli(args, output = process.stdout, root = process.cwd()) {
147
+ const [action, value, confirm] = args;
148
+ if (!action || action === "--help" || value === "--help") {
149
+ output.write("Usage: agent-lyra agent discover | register <descriptor.json> | sync <id> [--yes] | trace <run-id>\n");
150
+ return;
151
+ }
152
+ if (action === "discover" && !value) {
153
+ output.write(JSON.stringify(await discoverFrameworkAgents(root), null, 2) + "\n");
154
+ return;
155
+ }
156
+ if (action === "register" && value && !confirm && args.length === 2) {
157
+ const source = JSON.parse((await readFile(value, "utf8")).replace(/^\uFEFF/, ""));
158
+ const agent = frameworkDescriptor({ ...source, schemaVersion: 1, id: source.id ?? randomUUID() });
159
+ if (!agent) throw new Error("Provide a supported framework, name, and source entry. For managed-deepagents use relative/file.ts:export and provide assistantId.");
160
+ await sourceFile(root, agent);
161
+ const path = join(await directory(join(await registrationDirectory(root, "agents", true), "coding"), true), agent.id + ".json");
162
+ await writeFile(path, JSON.stringify(agent, null, 2) + "\n", { flag: "wx" });
163
+ output.write(JSON.stringify({ id: agent.id, name: agent.name, file: ".lyra/agents/coding/" + agent.id + ".json" }) + "\n");
164
+ return;
165
+ }
166
+ if (action === "trace" && typeof value === "string" && value.length === 36 && uuid.test(value) && args.length === 2) {
167
+ const store = await openStore(root);
168
+ try {
169
+ const events = await store.list(value);
170
+ if (!events[0]?.data.framework) throw new Error("No framework run with that ID in this project.");
171
+ output.write(JSON.stringify(events, null, 2) + "\n");
172
+ } finally { store.close(); }
173
+ return;
174
+ }
175
+ if (action === "sync" && value && (!confirm || confirm === "--yes") && args.length <= 3) {
176
+ const preview = await syncFrameworkAgent(root, value);
177
+ output.write(JSON.stringify(confirm ? await syncFrameworkAgent(root, value, preview.fingerprint) : preview.payload, null, 2) + "\n");
178
+ return;
179
+ }
180
+ throw new Error("Use agent-lyra agent --help.");
181
+ }
@@ -1,65 +1,65 @@
1
- const MAX_REPLY_BYTES = 256 * 1024;
2
-
3
- function failure(message, code = "FRAMEWORK_ENDPOINT") {
4
- return Object.assign(new Error(message), { code });
5
- }
6
-
7
- export async function runManagedDeepAgent(agent, input, traceId) {
8
- const port = Number(process.env.LYRA_MDA_PORT || 2024);
9
- if (!Number.isInteger(port) || port < 1024 || port > 65535) {
10
- throw failure("Set LYRA_MDA_PORT to the port printed by mda dev.");
11
- }
12
- const key = process.env.LYRA_MDA_API_KEY;
13
- let response;
14
- try {
15
- response = await fetch("http://127.0.0.1:" + port + "/runs/wait", {
16
- method: "POST",
17
- redirect: "error",
18
- signal: AbortSignal.timeout(60_000),
19
- headers: { "Content-Type": "application/json", ...(key ? { "x-api-key": key } : {}) },
20
- body: JSON.stringify({
21
- assistant_id: agent.assistantId,
22
- input: { messages: [{ role: "user", content: input }] },
23
- metadata: { lyra_trace_id: traceId },
24
- }),
25
- });
26
- } catch (error) {
27
- if (error.name === "TimeoutError") throw failure("Managed Deep Agent timed out.", "FRAMEWORK_TIMEOUT");
28
- throw failure("Could not reach the Managed Deep Agent. Start mda dev on the configured port.");
29
- }
30
- if (!response.ok || !response.body) {
31
- await response.body?.cancel();
32
- if ([401, 403].includes(response.status)) {
33
- throw failure("The local MDA server requires authorization. Set LYRA_MDA_API_KEY for that server.");
34
- }
35
- if (response.status === 404) throw failure("Check the assistant ID and port printed by mda dev.");
36
- throw failure("The MDA server rejected the run. Check its local logs.");
37
- }
38
- let data;
39
- try {
40
- const chunks = [];
41
- let size = 0;
42
- for await (const chunk of response.body) {
43
- size += chunk.byteLength;
44
- if (size > MAX_REPLY_BYTES) throw new Error();
45
- chunks.push(chunk);
46
- }
47
- data = JSON.parse(Buffer.concat(chunks).toString("utf8"));
48
- } catch (error) {
49
- if (error.name === "AbortError" || error.name === "TimeoutError") {
50
- throw failure("Managed Deep Agent timed out.", "FRAMEWORK_TIMEOUT");
51
- }
52
- throw failure("Managed Deep Agent returned an unreadable response.", "FRAMEWORK_OUTPUT");
53
- }
54
- if (data?.__error__) throw failure("The MDA run failed. Check its local logs.");
55
- if (data?.__interrupt__?.length) throw failure("This MDA run paused for approval. Continue it in the MDA interface.");
56
- const reply = Array.isArray(data?.messages) ? data.messages.at(-1) : null;
57
- const content = reply?.content;
58
- const text = typeof content === "string" ? content :
59
- Array.isArray(content) ? content.filter(block => block?.type === "text" && typeof block.text === "string")
60
- .map(block => block.text).join("\n") : "";
61
- if (!["assistant", "ai"].includes(reply?.role ?? reply?.type) || !text.trim() || text.length > 65_536) {
62
- throw failure("Managed Deep Agent returned no final text.", "FRAMEWORK_OUTPUT");
63
- }
64
- return text;
65
- }
1
+ const MAX_REPLY_BYTES = 256 * 1024;
2
+
3
+ function failure(message, code = "FRAMEWORK_ENDPOINT") {
4
+ return Object.assign(new Error(message), { code });
5
+ }
6
+
7
+ export async function runManagedDeepAgent(agent, input, traceId) {
8
+ const port = Number(process.env.LYRA_MDA_PORT || 2024);
9
+ if (!Number.isInteger(port) || port < 1024 || port > 65535) {
10
+ throw failure("Set LYRA_MDA_PORT to the port printed by mda dev.");
11
+ }
12
+ const key = process.env.LYRA_MDA_API_KEY;
13
+ let response;
14
+ try {
15
+ response = await fetch("http://127.0.0.1:" + port + "/runs/wait", {
16
+ method: "POST",
17
+ redirect: "error",
18
+ signal: AbortSignal.timeout(60_000),
19
+ headers: { "Content-Type": "application/json", ...(key ? { "x-api-key": key } : {}) },
20
+ body: JSON.stringify({
21
+ assistant_id: agent.assistantId,
22
+ input: { messages: [{ role: "user", content: input }] },
23
+ metadata: { lyra_trace_id: traceId },
24
+ }),
25
+ });
26
+ } catch (error) {
27
+ if (error.name === "TimeoutError") throw failure("Managed Deep Agent timed out.", "FRAMEWORK_TIMEOUT");
28
+ throw failure("Could not reach the Managed Deep Agent. Start mda dev on the configured port.");
29
+ }
30
+ if (!response.ok || !response.body) {
31
+ await response.body?.cancel();
32
+ if ([401, 403].includes(response.status)) {
33
+ throw failure("The local MDA server requires authorization. Set LYRA_MDA_API_KEY for that server.");
34
+ }
35
+ if (response.status === 404) throw failure("Check the assistant ID and port printed by mda dev.");
36
+ throw failure("The MDA server rejected the run. Check its local logs.");
37
+ }
38
+ let data;
39
+ try {
40
+ const chunks = [];
41
+ let size = 0;
42
+ for await (const chunk of response.body) {
43
+ size += chunk.byteLength;
44
+ if (size > MAX_REPLY_BYTES) throw new Error();
45
+ chunks.push(chunk);
46
+ }
47
+ data = JSON.parse(Buffer.concat(chunks).toString("utf8"));
48
+ } catch (error) {
49
+ if (error.name === "AbortError" || error.name === "TimeoutError") {
50
+ throw failure("Managed Deep Agent timed out.", "FRAMEWORK_TIMEOUT");
51
+ }
52
+ throw failure("Managed Deep Agent returned an unreadable response.", "FRAMEWORK_OUTPUT");
53
+ }
54
+ if (data?.__error__) throw failure("The MDA run failed. Check its local logs.");
55
+ if (data?.__interrupt__?.length) throw failure("This MDA run paused for approval. Continue it in the MDA interface.");
56
+ const reply = Array.isArray(data?.messages) ? data.messages.at(-1) : null;
57
+ const content = reply?.content;
58
+ const text = typeof content === "string" ? content :
59
+ Array.isArray(content) ? content.filter(block => block?.type === "text" && typeof block.text === "string")
60
+ .map(block => block.text).join("\n") : "";
61
+ if (!["assistant", "ai"].includes(reply?.role ?? reply?.type) || !text.trim() || text.length > 65_536) {
62
+ throw failure("Managed Deep Agent returned no final text.", "FRAMEWORK_OUTPUT");
63
+ }
64
+ return text;
65
+ }