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
package/README.md
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
# AgentLyra SDK
|
|
2
2
|
|
|
3
|
-
Build
|
|
3
|
+
Build apps from your saved Lyra agents, run agents with your own OpenAI key, and inspect saved runs from your IDE. The Node.js package includes a command-line tool and typed JavaScript APIs for composing workflows.
|
|
4
4
|
|
|
5
5
|
[AgentLyra homepage](https://agentlyra.in/) · [CLI reference](https://agentlyra.in/#sdk-commands) · [Cloud console](https://console.agentlyra.in/)
|
|
6
6
|
|
|
7
|
+
## Choose an SDK mode
|
|
8
|
+
|
|
9
|
+
| Mode | Where work runs | What you need |
|
|
10
|
+
| --- | --- | --- |
|
|
11
|
+
| **Standalone** | In your own Node.js process. Use `agent-lyra/standalone` for local workflows and retrieval, or run your own local workflows. | No AgentLyra account or key. Model-backed CLI runs use your own OpenAI key. |
|
|
12
|
+
| **Gated** | On a compatible hosted backend. `agent-lyra/gated` calls the backend's `/agents/<vertical>/invoke` endpoint. | A Lyra API key for that backend and its base URL. For AgentLyra Cloud, use a dedicated SDK key and the base URL https://api.agentlyra.in/api/sdk/v1. Cloud SDK calls also require an OpenAI key in Models settings. |
|
|
13
|
+
|
|
14
|
+
The App Builder below uses a dedicated SDK key to run saved agents in AgentLyra Cloud. Standalone workflows remain available through the SDK and CLI.
|
|
15
|
+
|
|
7
16
|
## Install
|
|
8
17
|
|
|
9
18
|
Requires **Node.js 22.13 or newer**. From your project directory:
|
|
@@ -14,30 +23,34 @@ npm install agent-lyra
|
|
|
14
23
|
|
|
15
24
|
Use `pnpm add agent-lyra` if your project uses pnpm. Run the CLI with `npx agent-lyra` after installation.
|
|
16
25
|
|
|
17
|
-
##
|
|
26
|
+
## Build an app from your Lyra agents
|
|
27
|
+
|
|
28
|
+
Create two enabled text agents in AgentLyra Console. In [API key settings](https://console.agentlyra.in/settings/lyra-api-key), choose **Generate SDK key** and set it as `LYRA_API_KEY` in your terminal environment. Add your own OpenAI key in Console → Models. Keep both keys out of source control.
|
|
18
29
|
|
|
19
30
|
```bash
|
|
20
31
|
npx agent-lyra app init
|
|
21
|
-
npx agent-lyra app
|
|
32
|
+
npx agent-lyra app validate manifest.json
|
|
33
|
+
npx agent-lyra app preview manifest.json
|
|
22
34
|
```
|
|
23
35
|
|
|
24
|
-
Open the
|
|
36
|
+
Choose the agents in the order they should run. The CLI creates one `manifest.json` in your project. Open the URL printed by the preview and enter some text. The first agent runs on that input, then the second receives the input and first result. The browser shows both outputs. This version runs each saved agent's instructions as text-only; connected tools and skills are outside this workflow.
|
|
25
37
|
|
|
26
|
-
|
|
38
|
+
Stop the preview or open a new terminal, then export into a new folder:
|
|
27
39
|
|
|
28
40
|
```bash
|
|
29
|
-
npx agent-lyra app
|
|
30
|
-
|
|
31
|
-
cd support-app
|
|
41
|
+
npx agent-lyra app export manifest.json ./my-app
|
|
42
|
+
cd my-app
|
|
32
43
|
npm start
|
|
33
44
|
```
|
|
34
45
|
|
|
46
|
+
The export contains editable HTML, a Node.js server, the runtime, and the app manifest. Keep `LYRA_API_KEY` in the server environment. The exported server binds to loopback and the two agents continue to run in Cloud.
|
|
47
|
+
|
|
35
48
|
## Choose the workflow you need
|
|
36
49
|
|
|
37
50
|
| I want to… | Start here |
|
|
38
51
|
| --- | --- |
|
|
39
52
|
| Set up a repo with starter agent definitions | `npx agent-lyra init` |
|
|
40
|
-
| Build and export
|
|
53
|
+
| Build and export an app from your own saved agents | [Build an app from your Lyra agents](#build-an-app-from-your-lyra-agents) |
|
|
41
54
|
| Run a model-backed agent from my terminal | [Run an agent](#run-an-agent) |
|
|
42
55
|
| Inspect saved runs inside my IDE | [Connect your IDE](#connect-your-ide) |
|
|
43
56
|
| Share or import an app | [Use the Marketplace](#use-the-marketplace) |
|
|
@@ -67,6 +80,46 @@ The MCP server reads saved runs from that project.
|
|
|
67
80
|
|
|
68
81
|
Generate a shareable listing preview with `npx agent-lyra app publish .lyra/app/manifest.json --dry-run`. Review it before submitting the listing in the cloud. Browse apps publicly, then sign in to import an editable private copy. CLI imports require an existing Lyra session supplied through `LYRA_SESSION_TOKEN`. [Marketplace Apps](https://agentlyra.in/marketplace/apps)
|
|
69
82
|
|
|
83
|
+
## Local Studio
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
npx agent-lyra studio
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Open the printed loopback URL. Drag a heading, text, input, text area, run button, or output onto the canvas. You can also add components by clicking them. Select a run button in the inspector, bind an input and output, and add agents or compatible workflows in the order they should run. **Save layout** writes the layout into your root `manifest.json`.
|
|
90
|
+
|
|
91
|
+
Studio reads standalone Markdown agents from `.lyra/agents/`. Each needs an `id`, a `name`, and a `## System Prompt` section. To run a local agent, set `OPENAI_API_KEY` and choose a model in the Run now drawer. Override the model list with a comma-separated `LYRA_STUDIO_MODELS` environment variable if needed.
|
|
92
|
+
|
|
93
|
+
Set `LYRA_API_KEY` to an AgentLyra SDK key, then select **Pull from Cloud** or run `npx agent-lyra studio pull`. The pull saves Cloud agent references, workflows, and team metadata into `.lyra/agents/`, `.lyra/workflows/`, and `.lyra/teams/`. Existing files are preserved. Studio can run compatible sequential text workflows. Other workflows and teams appear as reference cards. Cloud agents use the OpenAI key configured in Cloud Models.
|
|
94
|
+
|
|
95
|
+
Add an **Agent card** to let users run one agent directly from the app. Choose the agent in the Inspector and optionally bind an input and an output. Use a **Run button** for a sequence of agents or workflows.
|
|
96
|
+
|
|
97
|
+
Save the canvas in Studio and export it with `npx agent-lyra app export manifest.json ./my-app`. Open the new folder and run `npm start` to launch the app locally. The export keeps the component layout and Run button bindings. It includes the prompts of referenced local agents and the IDs of referenced Cloud agents. Supply your API keys through environment variables when starting the app.
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
### Run a Python skill from the canvas
|
|
101
|
+
|
|
102
|
+
Install the Python SDK in your environment. Create `.lyra/agents/draft.py`:
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
from lyra.standalone import Agent, Skill
|
|
106
|
+
|
|
107
|
+
def draft(text):
|
|
108
|
+
return f"Draft: {text}"
|
|
109
|
+
|
|
110
|
+
agent = Agent("draft", [Skill("draft-note", draft)])
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Register the skill in `.lyra/agents/draft.json`:
|
|
114
|
+
|
|
115
|
+
```json
|
|
116
|
+
{"id":"draft","name":"Draft a note","runtime":"python","file":"draft.py","skill":"draft-note"}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Restart Studio or click **Reload agents**. Select **Draft a note** on an Agent card, or add it to a Run button's execution order. Studio passes the input as a string, along with any previous step result. Skills may return strings or JSON-serializable values. The Python CLI uses its own interpreter. If you launch Studio from npm or start an exported app, set `LYRA_PYTHON` to your virtual environment's Python executable when needed.
|
|
120
|
+
|
|
121
|
+
Export includes the Python files bound to the canvas. Install the Python SDK and any dependencies in the environment running the export. If an agent imports other project files, add those files to the exported project and make them importable in that environment. Run Python agents from projects whose source you trust.
|
|
122
|
+
|
|
70
123
|
## CLI reference
|
|
71
124
|
|
|
72
125
|
Prefix each command with `npx agent-lyra`. Angle brackets indicate required arguments. Square brackets indicate optional arguments.
|
|
@@ -82,10 +135,10 @@ Prefix each command with `npx agent-lyra`. Angle brackets indicate required argu
|
|
|
82
135
|
|
|
83
136
|
| Command | What it does |
|
|
84
137
|
| --- | --- |
|
|
85
|
-
| `app init [manifest.json]` |
|
|
138
|
+
| `app init [manifest.json]` | Select two saved Lyra agents. Defaults to `manifest.json` in your project. |
|
|
86
139
|
| `app validate <manifest.json>` | Check the app manifest before previewing or exporting. |
|
|
87
|
-
| `app preview <manifest.json>` |
|
|
88
|
-
| `app export <manifest.json> <new-directory>` | Export
|
|
140
|
+
| `app preview <manifest.json>` | Preview the app in your browser on loopback. Set `LYRA_PREVIEW_PORT` to change its port. |
|
|
141
|
+
| `app export <manifest.json> <new-directory>` | Export editable app source into a new directory. |
|
|
89
142
|
| `app publish <manifest.json> --dry-run` | Print the proposed Marketplace payload for review. Submit the listing in the cloud. |
|
|
90
143
|
| `app use <listing-id> [new-manifest.json]` | Import a published app and save its manifest. Requires `LYRA_SESSION_TOKEN`. |
|
|
91
144
|
|
|
@@ -96,7 +149,7 @@ Prefix each command with `npx agent-lyra`. Angle brackets indicate required argu
|
|
|
96
149
|
| `agent run --model <model> --prompt <text>` | Run a local agent with an OpenAI model. Requires `OPENAI_API_KEY`. |
|
|
97
150
|
| `team run draft-review --model <model> --prompt <text>` | Run the built-in writer and reviewer team. Requires `OPENAI_API_KEY`. |
|
|
98
151
|
| `run status <run-id>` | Read the latest event for a run from local SQLite history. |
|
|
99
|
-
| `mcp serve --dir <absolute-project-directory>` | Expose saved runs to an IDE over read-only stdio MCP. |
|
|
152
|
+
| `mcp serve --dir <absolute-project-directory>` | Expose saved runs to an IDE over read-only stdio MCP. |
|
|
100
153
|
|
|
101
154
|
## Use the library from JavaScript
|
|
102
155
|
|
|
@@ -115,6 +168,6 @@ The standalone entry point also includes retrieval utilities. The gated entry po
|
|
|
115
168
|
|
|
116
169
|
## Current scope
|
|
117
170
|
|
|
118
|
-
The App Builder
|
|
171
|
+
The App Builder supports two saved text agents in sequence. Publishing saved-agent app manifests to Marketplace is outside the current release. OpenAI-backed agent runs use your own provider key. Cloud accounts and AgentLyra API keys are managed in the [AgentLyra console](https://console.agentlyra.in/).
|
|
119
172
|
|
|
120
173
|
MIT licensed.
|
package/bin/agent-lyra.js
CHANGED
|
@@ -10,8 +10,11 @@ import { runAppPreview } from "../src/app-builder/preview.js";
|
|
|
10
10
|
import { runAppPublish } from "../src/app-builder/publish.js";
|
|
11
11
|
import { runAppUse } from "../src/app-builder/use.js";
|
|
12
12
|
|
|
13
|
+
import { runStudio } from "../src/studio/server.js";
|
|
14
|
+
|
|
13
15
|
async function main() {
|
|
14
|
-
const argv = process.argv.slice(2);
|
|
16
|
+
const argv = process.argv.slice(2);
|
|
17
|
+
if (argv[0] === "studio") return runStudio(argv.slice(1));
|
|
15
18
|
if (argv[0] === "app" && argv[1] === "use") return runAppUse(argv.slice(2));
|
|
16
19
|
if (argv[0] === "app" && argv[1] === "init") return runAppInit(argv.slice(2));
|
|
17
20
|
if (argv[0] === "app" && argv[1] === "preview") return runAppPreview(argv.slice(2));
|
package/package.json
CHANGED
|
@@ -1,59 +1,109 @@
|
|
|
1
|
-
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
2
|
-
import { dirname, join, resolve } from "node:path";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
"runtime/app-builder/support-template/support-preview.
|
|
9
|
-
"runtime/app-builder/support-template/support-
|
|
10
|
-
"runtime/app-builder/
|
|
11
|
-
"runtime/app-builder/
|
|
12
|
-
"runtime/app-builder/
|
|
13
|
-
"runtime/app-builder/
|
|
14
|
-
"runtime/
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
files.push(["
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
await
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
1
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, join, resolve } from "node:path";
|
|
3
|
+
import { readAppManifest, SAVED_VERSION, validateSavedApp } from "./saved-app.js";
|
|
4
|
+
import { exportStudioApp } from "../studio/export.js";
|
|
5
|
+
import { readSupportManifest } from "./support-manifest.js";
|
|
6
|
+
|
|
7
|
+
const assets = {
|
|
8
|
+
"runtime/app-builder/support-template/support-preview.mjs": "./support-template/support-preview.mjs",
|
|
9
|
+
"runtime/app-builder/support-template/support-preview.html": "./support-template/support-preview.html",
|
|
10
|
+
"runtime/app-builder/support-template/support-preview.js": "./support-template/support-preview.js",
|
|
11
|
+
"runtime/app-builder/support-template/support-workflow.mjs": "./support-template/support-workflow.mjs",
|
|
12
|
+
"runtime/app-builder/gateway.js": "./gateway.js",
|
|
13
|
+
"runtime/app-builder/validate.js": "./validate.js",
|
|
14
|
+
"runtime/app-builder/saved-app.js": "./saved-app.js",
|
|
15
|
+
"runtime/app-builder/value-schema.js": "./value-schema.js",
|
|
16
|
+
"runtime/app-builder/native-adapter.js": "./native-adapter.js",
|
|
17
|
+
"runtime/runtime/run-agent.js": "../runtime/run-agent.js",
|
|
18
|
+
LICENSE: "../../LICENSE",
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export async function runAppExport(args, output = process.stdout) {
|
|
22
|
+
if (args.length === 1 && args[0] === "--help") {
|
|
23
|
+
output.write("Usage: agent-lyra app export <manifest.json> <new-directory>\n");
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (args.length !== 2 || args.some((arg) => arg.startsWith("-"))) {
|
|
27
|
+
throw new Error("Provide a manifest and new output directory. See agent-lyra app export --help.");
|
|
28
|
+
}
|
|
29
|
+
const app = await readAppManifest(args[0]);
|
|
30
|
+
if (Array.isArray(app?.studio?.layout)) {
|
|
31
|
+
const directory = resolve(args[1]);
|
|
32
|
+
await exportStudioApp(app, dirname(resolve(args[0])), directory);
|
|
33
|
+
output.write("Exported Studio app to " + directory + "\n");
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (app?.version === SAVED_VERSION) {
|
|
37
|
+
await exportSavedApp(validateSavedApp(app), resolve(args[1]));
|
|
38
|
+
output.write("Exported " + app.application.id + " to " + resolve(args[1]) + "\n");
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const manifest = await readSupportManifest(args[0]);
|
|
42
|
+
const directory = resolve(args[1]);
|
|
43
|
+
const files = await Promise.all(Object.entries(assets).map(async ([name, sourcePath]) =>
|
|
44
|
+
[name, await readFile(new URL(sourcePath, import.meta.url))]));
|
|
45
|
+
const manifestReader = 'const manifest = JSON.parse(await readFile(new URL("./example-manifest.json", import.meta.url), "utf8"));\n';
|
|
46
|
+
files.push(["example-manifest.json", JSON.stringify(manifest, null, 2) + "\n"]);
|
|
47
|
+
files.push(["support-preview.mjs",
|
|
48
|
+
'import { readFile } from "node:fs/promises";\n' +
|
|
49
|
+
'import { startSupportPreview } from "./runtime/app-builder/support-template/support-preview.mjs";\n' +
|
|
50
|
+
manifestReader + "await startSupportPreview(manifest);\n"]);
|
|
51
|
+
files.push(["support-example.mjs",
|
|
52
|
+
'import { readFile } from "node:fs/promises";\n' +
|
|
53
|
+
'import { createSupportWorkflow } from "./runtime/app-builder/support-template/support-workflow.mjs";\n' +
|
|
54
|
+
manifestReader +
|
|
55
|
+
'console.log(JSON.stringify(await createSupportWorkflow(manifest)(process.argv[2] ?? "Invoice question"), null, 2));\n']);
|
|
56
|
+
files.push(["package.json", JSON.stringify({
|
|
57
|
+
name: manifest.application.id, version: "1.0.0", private: true, type: "module",
|
|
58
|
+
engines: { node: ">=22.13.0" },
|
|
59
|
+
scripts: { start: "node support-preview.mjs", demo: "node support-example.mjs" },
|
|
60
|
+
}, null, 2) + "\n"]);
|
|
61
|
+
files.push(["README.md", "# Support app\n\nRun `npm start` and open http://127.0.0.1:4173/. Run `npm run demo` for the terminal workflow.\n\nThe bundled classifier and provider are deterministic. The app binds to loopback and does not persist or send tickets.\n"]);
|
|
62
|
+
try {
|
|
63
|
+
await mkdir(directory);
|
|
64
|
+
} catch (error) {
|
|
65
|
+
if (error.code === "EEXIST") throw new Error("Choose a new directory. Existing files were preserved.");
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
for (const [name, contents] of files) {
|
|
69
|
+
const target = join(directory, name);
|
|
70
|
+
await mkdir(dirname(target), { recursive: true });
|
|
71
|
+
await writeFile(target, contents, { flag: "wx" });
|
|
72
|
+
}
|
|
73
|
+
output.write(`Exported ${manifest.application.id} to ${directory}\n`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function exportSavedApp(manifest, directory) {
|
|
77
|
+
const runtime = ["saved-app.js", "saved-preview.js", "saved-preview-browser.js", "saved-preview.html"];
|
|
78
|
+
const files = await Promise.all(runtime.map(async name => [
|
|
79
|
+
name, await readFile(new URL("./" + name, import.meta.url)),
|
|
80
|
+
]));
|
|
81
|
+
const project = {
|
|
82
|
+
name: manifest.application.id, version: "1.0.0", private: true, type: "module",
|
|
83
|
+
engines: { node: ">=22.13.0" }, scripts: { start: "node server.js" },
|
|
84
|
+
};
|
|
85
|
+
const safeManifest = {
|
|
86
|
+
version: SAVED_VERSION,
|
|
87
|
+
application: { id: manifest.application.id, name: manifest.application.name },
|
|
88
|
+
workflow: { steps: manifest.workflow.steps.map(({ agentId }) => ({ agentId })) },
|
|
89
|
+
};
|
|
90
|
+
files.push(["package.json", JSON.stringify(project, null, 2) + "\n"]);
|
|
91
|
+
files.push(["manifest.json", JSON.stringify(safeManifest, null, 2) + "\n"]);
|
|
92
|
+
files.push(["server.js",
|
|
93
|
+
'import { readAppManifest } from "./saved-app.js";\n' +
|
|
94
|
+
'import { startSavedPreview } from "./saved-preview.js";\n' +
|
|
95
|
+
'const { url } = await startSavedPreview(await readAppManifest(new URL("./manifest.json", import.meta.url)));\n' +
|
|
96
|
+
'console.log("App preview: " + url);\n']);
|
|
97
|
+
files.push(["README.md", "# Run this Lyra app\n\nSet LYRA_API_KEY to your AgentLyra SDK key, then run npm start. Open the loopback URL. Your selected agents run in AgentLyra Cloud. Set LYRA_PREVIEW_PORT=0 to pick a free port. The local server does not provide authentication for public hosting.\n"]);
|
|
98
|
+
files.push(["LICENSE", await readFile(new URL("../../LICENSE", import.meta.url))]);
|
|
99
|
+
|
|
100
|
+
try {
|
|
101
|
+
await mkdir(directory);
|
|
102
|
+
} catch (error) {
|
|
103
|
+
if (error.code === "EEXIST") throw new Error("Choose a new directory. Existing files were preserved.");
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
for (const [name, contents] of files) {
|
|
107
|
+
await writeFile(join(directory, name), contents, { flag: "wx" });
|
|
108
|
+
}
|
|
109
|
+
}
|
package/src/app-builder/init.js
CHANGED
|
@@ -1,23 +1,52 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { dirname, resolve } from "node:path";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
1
|
+
import { access, mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, resolve } from "node:path";
|
|
3
|
+
import { createInterface } from "node:readline/promises";
|
|
4
|
+
import { createSavedAgentClient, SAVED_VERSION, validateSavedApp } from "./saved-app.js";
|
|
5
|
+
|
|
6
|
+
export async function runAppInit(args, output = process.stdout, {
|
|
7
|
+
openTerminal = () => createInterface({ input: process.stdin, output }),
|
|
8
|
+
listAgents = async () => {
|
|
9
|
+
if (!process.stdin.isTTY) throw new Error("Run app init from an interactive terminal to choose your agents.");
|
|
10
|
+
return createSavedAgentClient().list();
|
|
11
|
+
},
|
|
12
|
+
} = {}) {
|
|
13
|
+
if (args.length === 1 && args[0] === "--help") {
|
|
14
|
+
output.write("Usage: agent-lyra app init [manifest.json]\nSelect two saved agents. Requires LYRA_API_KEY. Default: manifest.json\n");
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
if (args.length > 1 || args[0]?.startsWith("-")) {
|
|
18
|
+
throw new Error("Provide at most one manifest path. See agent-lyra app init --help.");
|
|
19
|
+
}
|
|
20
|
+
const target = resolve(args[0] ?? "manifest.json");
|
|
21
|
+
try {
|
|
22
|
+
await access(target);
|
|
23
|
+
throw new Error("Manifest already exists at " + target + ". Choose a new path.");
|
|
24
|
+
} catch (error) {
|
|
25
|
+
if (error.code !== "ENOENT") throw error;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const agents = await listAgents();
|
|
29
|
+
if (agents.length < 2) throw new Error("Create at least two enabled agents in AgentLyra Console first.");
|
|
30
|
+
for (const [index, agent] of agents.entries()) {
|
|
31
|
+
const name = String(agent.name).replace(/[\x00-\x1f\x7f]/g, " ");
|
|
32
|
+
output.write((index + 1) + ". " + name + "\n");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const terminal = openTerminal();
|
|
36
|
+
try {
|
|
37
|
+
const name = (await terminal.question("Application name: ")).trim();
|
|
38
|
+
const first = agents[Number(await terminal.question("First agent number: ")) - 1];
|
|
39
|
+
const second = agents[Number(await terminal.question("Second agent number: ")) - 1];
|
|
40
|
+
const id = name.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
|
|
41
|
+
const manifest = validateSavedApp({
|
|
42
|
+
version: SAVED_VERSION,
|
|
43
|
+
application: { id: id && /^[a-z]/.test(id) ? id.slice(0, 80) : "lyra-app", name },
|
|
44
|
+
workflow: { steps: [{ agentId: first?.id }, { agentId: second?.id }] },
|
|
45
|
+
});
|
|
46
|
+
await mkdir(dirname(target), { recursive: true });
|
|
47
|
+
await writeFile(target, JSON.stringify(manifest, null, 2) + "\n", { flag: "wx" });
|
|
48
|
+
output.write("Created " + target + "\n");
|
|
49
|
+
} finally {
|
|
50
|
+
terminal.close();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -1,14 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import { readAppManifest, SAVED_VERSION } from "./saved-app.js";
|
|
2
|
+
import { startSavedPreview } from "./saved-preview.js";
|
|
3
|
+
import { readSupportManifest } from "./support-manifest.js";
|
|
4
|
+
import { startSupportPreview } from "./support-template/support-preview.mjs";
|
|
5
|
+
|
|
6
|
+
export async function runAppPreview(args, output = process.stdout) {
|
|
7
|
+
if (args.length === 1 && args[0] === "--help") {
|
|
8
|
+
output.write("Usage: agent-lyra app preview <manifest.json>\nSet LYRA_PREVIEW_PORT to override the loopback port.\n");
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
if (args.length !== 1 || args[0].startsWith("-")) {
|
|
12
|
+
throw new Error("Provide a manifest path. See agent-lyra app preview --help.");
|
|
13
|
+
}
|
|
14
|
+
const app = await readAppManifest(args[0]);
|
|
15
|
+
if (app?.version === SAVED_VERSION) {
|
|
16
|
+
const { url } = await startSavedPreview(app);
|
|
17
|
+
output.write("App preview: " + url + "\n");
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const manifest = await readSupportManifest(args[0]);
|
|
21
|
+
await startSupportPreview(manifest);
|
|
22
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
|
|
3
|
+
export const SAVED_VERSION = "2.0";
|
|
4
|
+
const idPattern = /^[a-f0-9]{24}$/i;
|
|
5
|
+
const appIdPattern = /^[a-z][a-z0-9-]{0,79}$/;
|
|
6
|
+
|
|
7
|
+
export function validateSavedApp(manifest) {
|
|
8
|
+
if (manifest?.version !== SAVED_VERSION || !appIdPattern.test(manifest.application?.id ?? "") ||
|
|
9
|
+
typeof manifest.application.name !== "string" || !manifest.application.name.trim() ||
|
|
10
|
+
manifest.application.name.length > 120) {
|
|
11
|
+
throw new Error('Version "2.0" requires an application ID and name.');
|
|
12
|
+
}
|
|
13
|
+
const { steps } = manifest.workflow ?? {};
|
|
14
|
+
if (!Array.isArray(steps) || steps.length !== 2 ||
|
|
15
|
+
!steps.every(step => typeof step?.agentId === "string" && idPattern.test(step.agentId)) ||
|
|
16
|
+
steps[0].agentId.toLowerCase() === steps[1].agentId.toLowerCase()) {
|
|
17
|
+
throw new Error("A workflow requires two distinct saved Lyra agent IDs.");
|
|
18
|
+
}
|
|
19
|
+
return manifest;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function readAppManifest(path) {
|
|
23
|
+
const source = await readFile(path, "utf8");
|
|
24
|
+
try {
|
|
25
|
+
return JSON.parse(source.replace(/^\uFEFF/, ""));
|
|
26
|
+
} catch {
|
|
27
|
+
throw new Error("Application manifest is not valid JSON.");
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function createSavedAgentClient({ key = process.env.LYRA_API_KEY, fetchRequest = fetch } = {}) {
|
|
32
|
+
if (!/^lyra_sdk_[a-f0-9]{64}$/.test(key ?? "")) {
|
|
33
|
+
throw new Error("Set LYRA_API_KEY to a dedicated SDK key from AgentLyra Console.");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function request(path, prompt) {
|
|
37
|
+
const response = await fetchRequest("https://api.agentlyra.in/api/sdk/v1/saved-agents" + path, {
|
|
38
|
+
method: prompt === undefined ? "GET" : "POST",
|
|
39
|
+
headers: { Authorization: "Bearer " + key, "Content-Type": "application/json" },
|
|
40
|
+
body: prompt === undefined ? undefined : JSON.stringify({ prompt }),
|
|
41
|
+
redirect: "error",
|
|
42
|
+
signal: AbortSignal.timeout(65_000),
|
|
43
|
+
});
|
|
44
|
+
if (!response.ok) {
|
|
45
|
+
if (response.status === 401) throw new Error("The SDK key is invalid or revoked.");
|
|
46
|
+
if (response.status === 403) throw new Error("Add your OpenAI key in AgentLyra Console → Models.");
|
|
47
|
+
if (response.status === 404) throw new Error("A selected agent is unavailable to this SDK key.");
|
|
48
|
+
throw new Error("AgentLyra Cloud request failed (HTTP " + response.status + ").");
|
|
49
|
+
}
|
|
50
|
+
return response.json();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
list: async () => (await request("")).agents,
|
|
55
|
+
invoke: async (id, prompt) => (await request("/" + encodeURIComponent(id) + "/invoke", prompt)).output,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export async function runSavedApp(manifest, input, client = createSavedAgentClient()) {
|
|
60
|
+
const [first, second] = validateSavedApp(manifest).workflow.steps;
|
|
61
|
+
if (typeof input !== "string" || !input.trim() || input.length > 4000) {
|
|
62
|
+
throw new Error("Enter between 1 and 4,000 characters.");
|
|
63
|
+
}
|
|
64
|
+
const firstOutput = await client.invoke(first.agentId, input.trim());
|
|
65
|
+
const output = await client.invoke(second.agentId, JSON.stringify({
|
|
66
|
+
input: input.trim(),
|
|
67
|
+
previousAgentOutput: firstOutput,
|
|
68
|
+
}).slice(0, 12_000));
|
|
69
|
+
return { firstOutput, output };
|
|
70
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const form = document.querySelector("form");
|
|
2
|
+
const button = form.querySelector("button");
|
|
3
|
+
const status = document.querySelector("#status");
|
|
4
|
+
const result = document.querySelector("#result");
|
|
5
|
+
|
|
6
|
+
fetch("/api/app").then(response => response.json()).then(({ name }) => {
|
|
7
|
+
document.title = name;
|
|
8
|
+
document.querySelector("h1").textContent = name;
|
|
9
|
+
}).catch(() => { status.textContent = "Could not load application details."; });
|
|
10
|
+
|
|
11
|
+
form.addEventListener("submit", async event => {
|
|
12
|
+
event.preventDefault();
|
|
13
|
+
button.disabled = true;
|
|
14
|
+
result.hidden = true;
|
|
15
|
+
status.textContent = "Running both agents…";
|
|
16
|
+
try {
|
|
17
|
+
const response = await fetch("/api/run", {
|
|
18
|
+
method: "POST",
|
|
19
|
+
headers: { "Content-Type": "application/json" },
|
|
20
|
+
body: JSON.stringify({ input: document.querySelector("#input").value }),
|
|
21
|
+
});
|
|
22
|
+
const data = await response.json();
|
|
23
|
+
if (!response.ok) throw new Error(data.error);
|
|
24
|
+
document.querySelector("#output").textContent = data.output;
|
|
25
|
+
document.querySelector("#first").textContent = data.firstOutput;
|
|
26
|
+
result.hidden = false;
|
|
27
|
+
status.textContent = "Workflow completed.";
|
|
28
|
+
} catch (error) {
|
|
29
|
+
status.textContent = error.message;
|
|
30
|
+
} finally {
|
|
31
|
+
button.disabled = false;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<title>Lyra application</title>
|
|
7
|
+
<style>
|
|
8
|
+
body { background:#f6f9f7;color:#152c23;font:1rem/1.6 system-ui,sans-serif;margin:0 }
|
|
9
|
+
main { max-width:50rem;margin:4rem auto;padding:0 1.25rem }
|
|
10
|
+
label,textarea,button { display:block;font:inherit }
|
|
11
|
+
textarea { box-sizing:border-box;width:100%;min-height:8rem;margin:.45rem 0 1rem;padding:.75rem;border:1px solid #8ea99c;border-radius:.5rem }
|
|
12
|
+
button { background:#174b38;color:white;border:0;border-radius:.5rem;padding:.6rem 1rem;cursor:pointer }
|
|
13
|
+
button:disabled { opacity:.65;cursor:wait }
|
|
14
|
+
pre { white-space:pre-wrap;overflow-wrap:anywhere;background:white;padding:1rem;border:1px solid #cfddd5;border-radius:.5rem;font:inherit }
|
|
15
|
+
</style>
|
|
16
|
+
<script src="/preview.js" defer></script>
|
|
17
|
+
</head>
|
|
18
|
+
<body><main>
|
|
19
|
+
<h1>Lyra application</h1>
|
|
20
|
+
<form><label for="input">Workflow input</label><textarea id="input" required maxlength="4000"></textarea><button>Run workflow</button></form>
|
|
21
|
+
<p role="status" id="status"></p>
|
|
22
|
+
<section hidden id="result"><h2>Final result</h2><pre id="output"></pre><details><summary>First agent output</summary><pre id="first"></pre></details></section>
|
|
23
|
+
</main></body>
|
|
24
|
+
</html>
|