agent-lyra 0.6.0 → 0.6.2

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 (47) hide show
  1. package/README.md +435 -173
  2. package/bin/agent-lyra.js +21 -3
  3. package/bin/lyra-oss-preview.js +14 -0
  4. package/package.json +60 -56
  5. package/src/app-builder/export.js +109 -109
  6. package/src/app-builder/init.js +52 -52
  7. package/src/app-builder/manifest-contract.js +46 -0
  8. package/src/app-builder/preview.js +22 -22
  9. package/src/app-builder/saved-app.js +70 -70
  10. package/src/app-builder/saved-preview-browser.js +33 -33
  11. package/src/app-builder/saved-preview.html +24 -24
  12. package/src/app-builder/saved-preview.js +61 -61
  13. package/src/app-builder/validate.js +21 -64
  14. package/src/coding/catalog.js +56 -0
  15. package/src/coding/cloud-catalog.js +44 -0
  16. package/src/coding/detect-stack.mjs +110 -0
  17. package/src/coding/export.js +130 -0
  18. package/src/coding/setup.js +174 -0
  19. package/src/init.js +32 -4
  20. package/src/memory/cli.js +21 -0
  21. package/src/memory/contract.json +28 -0
  22. package/src/memory/index.d.ts +49 -0
  23. package/src/memory/index.js +259 -0
  24. package/src/memory/lifecycle.js +29 -0
  25. package/src/memory/local-cli.js +34 -0
  26. package/src/memory/proposals.js +84 -0
  27. package/src/memory/skills-cli.js +26 -0
  28. package/src/memory/sync.js +89 -0
  29. package/src/memory/validation.js +25 -0
  30. package/src/runtime/local-cli.js +2 -1
  31. package/src/studio/agent-ui.css +43 -43
  32. package/src/studio/agent-ui.js +32 -32
  33. package/src/studio/browser.js +331 -289
  34. package/src/studio/export.js +80 -79
  35. package/src/studio/index.html +37 -16
  36. package/src/studio/memory-actions.js +37 -0
  37. package/src/studio/memory-panel.js +200 -0
  38. package/src/studio/project.js +216 -190
  39. package/src/studio/pull-agent.js +72 -0
  40. package/src/studio/push.js +54 -0
  41. package/src/studio/python-agent.js +47 -47
  42. package/src/studio/python-agent.py +25 -25
  43. package/src/studio/server.js +143 -102
  44. package/src/studio/session.js +83 -0
  45. package/src/studio/studio.css +10 -7
  46. package/src/studio/sync.js +42 -39
  47. package/src/studio/workflow-sync.js +157 -0
@@ -0,0 +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
+ }