agent-lyra 0.1.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/LICENSE +21 -0
- package/README.md +36 -0
- package/package.json +34 -0
- package/src/gated/hostedAgents.js +56 -0
- package/src/gated/index.js +5 -0
- package/src/index.d.ts +2 -0
- package/src/index.js +2 -0
- package/src/standalone/index.js +13 -0
- package/src/standalone/orchestration.js +59 -0
- package/src/standalone/rag.js +113 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lyra
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# agent-lyra
|
|
2
|
+
|
|
3
|
+
Two-layer Lyra SDK package:
|
|
4
|
+
|
|
5
|
+
- Standalone layer: local orchestration primitives and RAG utilities with no API key.
|
|
6
|
+
- Gated layer: requires a Lyra API key and enables hosted vertical agents for HR, Sales, Support, Finance, Coding, Legal, News, and Markets.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install agent-lyra
|
|
12
|
+
# or
|
|
13
|
+
pnpm add agent-lyra
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Installation has zero side effects:
|
|
17
|
+
|
|
18
|
+
- no files/folders created
|
|
19
|
+
- no network calls by package code
|
|
20
|
+
- no postinstall hooks
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
import { standalone, gated } from "agent-lyra";
|
|
26
|
+
|
|
27
|
+
const workflow = standalone.createLinearWorkflow(
|
|
28
|
+
async () => "collect",
|
|
29
|
+
async (ctx) => `${ctx.step_1}:analyze`,
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
const result = await standalone.runWorkflow(workflow);
|
|
33
|
+
|
|
34
|
+
const client = gated.createHostedAgentClient({ apiKey: process.env.LYRA_API_KEY });
|
|
35
|
+
const output = await client.invoke("sales", { prompt: "Summarize latest pipeline risk." });
|
|
36
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "agent-lyra",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Two-layer Lyra SDK with standalone local primitives and gated hosted agents.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"types": "./src/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/index.js",
|
|
10
|
+
"./standalone": "./src/standalone/index.js",
|
|
11
|
+
"./gated": "./src/gated/index.js"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"src",
|
|
15
|
+
"README.md",
|
|
16
|
+
"LICENSE"
|
|
17
|
+
],
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "node --test"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"lyra",
|
|
24
|
+
"orchestration",
|
|
25
|
+
"rag",
|
|
26
|
+
"agents",
|
|
27
|
+
"sdk"
|
|
28
|
+
],
|
|
29
|
+
"author": "",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export const LYRA_VERTICALS = [
|
|
2
|
+
"hr",
|
|
3
|
+
"sales",
|
|
4
|
+
"support",
|
|
5
|
+
"finance",
|
|
6
|
+
"coding",
|
|
7
|
+
"legal",
|
|
8
|
+
"news",
|
|
9
|
+
"markets",
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
const DEFAULT_BASE_URL = "https://api.lyra.ai/v1";
|
|
13
|
+
|
|
14
|
+
export class HostedAgentClient {
|
|
15
|
+
constructor({ apiKey, baseUrl = DEFAULT_BASE_URL, fetchImpl = fetch } = {}) {
|
|
16
|
+
if (!apiKey || typeof apiKey !== "string") {
|
|
17
|
+
throw new Error("A Lyra API key is required for gated features.");
|
|
18
|
+
}
|
|
19
|
+
if (typeof fetchImpl !== "function") {
|
|
20
|
+
throw new Error("fetchImpl must be a function.");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
this.apiKey = apiKey;
|
|
24
|
+
this.baseUrl = baseUrl;
|
|
25
|
+
this.fetchImpl = fetchImpl;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
listVerticals() {
|
|
29
|
+
return [...LYRA_VERTICALS];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async invoke(vertical, payload = {}) {
|
|
33
|
+
if (!LYRA_VERTICALS.includes(vertical)) {
|
|
34
|
+
throw new Error(`Unsupported vertical: ${vertical}`);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const response = await this.fetchImpl(`${this.baseUrl}/agents/${vertical}/invoke`, {
|
|
38
|
+
method: "POST",
|
|
39
|
+
headers: {
|
|
40
|
+
"Content-Type": "application/json",
|
|
41
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
42
|
+
},
|
|
43
|
+
body: JSON.stringify(payload),
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
if (!response.ok) {
|
|
47
|
+
throw new Error(`Lyra API request failed with status ${response.status}.`);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return response.json();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function createHostedAgentClient(options) {
|
|
55
|
+
return new HostedAgentClient(options);
|
|
56
|
+
}
|
package/src/index.d.ts
ADDED
package/src/index.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export function createTask(id, handler) {
|
|
2
|
+
if (!id || typeof id !== "string") {
|
|
3
|
+
throw new Error("Task id must be a non-empty string.");
|
|
4
|
+
}
|
|
5
|
+
if (typeof handler !== "function") {
|
|
6
|
+
throw new Error("Task handler must be a function.");
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
return { id, handler };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function createWorkflow(tasks = []) {
|
|
13
|
+
const map = new Map();
|
|
14
|
+
|
|
15
|
+
for (const task of tasks) {
|
|
16
|
+
if (!task?.id || typeof task.handler !== "function") {
|
|
17
|
+
throw new Error("Each task must contain id and handler.");
|
|
18
|
+
}
|
|
19
|
+
map.set(task.id, task.handler);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
tasks: map,
|
|
24
|
+
add(task) {
|
|
25
|
+
if (!task?.id || typeof task.handler !== "function") {
|
|
26
|
+
throw new Error("Each task must contain id and handler.");
|
|
27
|
+
}
|
|
28
|
+
map.set(task.id, task.handler);
|
|
29
|
+
return this;
|
|
30
|
+
},
|
|
31
|
+
list() {
|
|
32
|
+
return [...map.keys()];
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function runWorkflow(workflow, initialContext = {}) {
|
|
38
|
+
if (!workflow?.tasks || !(workflow.tasks instanceof Map)) {
|
|
39
|
+
throw new Error("workflow must come from createWorkflow().");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const context = { ...initialContext };
|
|
43
|
+
|
|
44
|
+
for (const [taskId, handler] of workflow.tasks) {
|
|
45
|
+
context[taskId] = await handler(context);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return context;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function createLinearWorkflow(...handlers) {
|
|
52
|
+
const workflow = createWorkflow();
|
|
53
|
+
|
|
54
|
+
handlers.forEach((handler, index) => {
|
|
55
|
+
workflow.add(createTask(`step_${index + 1}`, handler));
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
return workflow;
|
|
59
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
export function chunkText(text, { chunkSize = 500, overlap = 50 } = {}) {
|
|
2
|
+
if (typeof text !== "string") {
|
|
3
|
+
throw new Error("text must be a string");
|
|
4
|
+
}
|
|
5
|
+
if (chunkSize <= 0) {
|
|
6
|
+
throw new Error("chunkSize must be > 0");
|
|
7
|
+
}
|
|
8
|
+
if (overlap < 0 || overlap >= chunkSize) {
|
|
9
|
+
throw new Error("overlap must be >= 0 and < chunkSize");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const chunks = [];
|
|
13
|
+
let start = 0;
|
|
14
|
+
|
|
15
|
+
while (start < text.length) {
|
|
16
|
+
const end = Math.min(start + chunkSize, text.length);
|
|
17
|
+
chunks.push(text.slice(start, end));
|
|
18
|
+
if (end === text.length) {
|
|
19
|
+
break;
|
|
20
|
+
}
|
|
21
|
+
start = end - overlap;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return chunks;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function cosineSimilarity(a, b) {
|
|
28
|
+
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) {
|
|
29
|
+
throw new Error("Vectors must be arrays with equal length.");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let dot = 0;
|
|
33
|
+
let normA = 0;
|
|
34
|
+
let normB = 0;
|
|
35
|
+
|
|
36
|
+
for (let i = 0; i < a.length; i += 1) {
|
|
37
|
+
dot += a[i] * b[i];
|
|
38
|
+
normA += a[i] * a[i];
|
|
39
|
+
normB += b[i] * b[i];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (normA === 0 || normB === 0) {
|
|
43
|
+
return 0;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return dot / (Math.sqrt(normA) * Math.sqrt(normB));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function createInMemoryVectorStore(embedder) {
|
|
50
|
+
if (typeof embedder !== "function") {
|
|
51
|
+
throw new Error("embedder must be a function");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const docs = [];
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
async add({ id, text, metadata = {} }) {
|
|
58
|
+
if (!id || typeof text !== "string") {
|
|
59
|
+
throw new Error("Document requires id and text.");
|
|
60
|
+
}
|
|
61
|
+
const vector = await embedder(text);
|
|
62
|
+
docs.push({ id, text, metadata, vector });
|
|
63
|
+
return id;
|
|
64
|
+
},
|
|
65
|
+
async search(query, { topK = 5 } = {}) {
|
|
66
|
+
const queryVector = await embedder(query);
|
|
67
|
+
return docs
|
|
68
|
+
.map((doc) => ({
|
|
69
|
+
...doc,
|
|
70
|
+
score: cosineSimilarity(queryVector, doc.vector),
|
|
71
|
+
}))
|
|
72
|
+
.sort((left, right) => right.score - left.score)
|
|
73
|
+
.slice(0, topK);
|
|
74
|
+
},
|
|
75
|
+
size() {
|
|
76
|
+
return docs.length;
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function createKeywordRetriever(documents = []) {
|
|
82
|
+
const docs = [...documents];
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
add(document) {
|
|
86
|
+
if (!document?.id || typeof document.text !== "string") {
|
|
87
|
+
throw new Error("Document requires id and text.");
|
|
88
|
+
}
|
|
89
|
+
docs.push(document);
|
|
90
|
+
return document.id;
|
|
91
|
+
},
|
|
92
|
+
search(query, { topK = 5 } = {}) {
|
|
93
|
+
const terms = query.toLowerCase().split(/\s+/).filter(Boolean);
|
|
94
|
+
return docs
|
|
95
|
+
.map((doc) => {
|
|
96
|
+
const body = doc.text.toLowerCase();
|
|
97
|
+
let hits = 0;
|
|
98
|
+
for (const term of terms) {
|
|
99
|
+
if (body.includes(term)) {
|
|
100
|
+
hits += 1;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return { ...doc, score: hits };
|
|
104
|
+
})
|
|
105
|
+
.filter((doc) => doc.score > 0)
|
|
106
|
+
.sort((left, right) => right.score - left.score)
|
|
107
|
+
.slice(0, topK);
|
|
108
|
+
},
|
|
109
|
+
size() {
|
|
110
|
+
return docs.length;
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|