agent-lyra 0.1.3 → 0.2.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.
@@ -1,59 +1,6 @@
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
- }
1
+ export {
2
+ createWorkflow,
3
+ runWorkflow,
4
+ createTask,
5
+ createLinearWorkflow,
6
+ } from "../runtime/orchestration.js";
@@ -1,113 +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
- }
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
+ }