feat: add llamaindex bindling (#41)

This commit is contained in:
Alex Yang
2025-04-17 14:26:40 -07:00
committed by GitHub
parent f84872f0fc
commit 4d5ffb9545
22 changed files with 434 additions and 43 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@llama-flow/llamaindex": patch
---
feat: add llamaindex binding
+1 -1
View File
@@ -31,4 +31,4 @@ jobs:
- name: Install dependencies
run: pnpm install
- name: Run tests
run: pnpm run -r test
run: pnpx turbo run test
+3
View File
@@ -137,3 +137,6 @@ packages/core/middleware
packages/core/util
packages/core/stream
packages/core/README.md
lib
.turbo
+1
View File
@@ -1 +1,2 @@
pnpm-lock.yaml
lib
+6 -1
View File
@@ -10,15 +10,20 @@
]
},
"scripts": {
"typecheck": "tsc -b --verbose",
"lint:fix": "prettier . --write",
"publish": "pnpm -r run build && changeset publish",
"prepare": "husky"
},
"devDependencies": {
"@changesets/cli": "^2.29.0",
"@edge-runtime/vm": "^5.0.0",
"happy-dom": "^17.4.4",
"husky": "^9.1.7",
"lint-staged": "^15.5.0",
"prettier": "^3.5.3",
"vite-tsconfig-paths": "^5.1.4"
"turbo": "^2.5.0",
"vite-tsconfig-paths": "^5.1.4",
"vitest": "^3.0.8"
}
}
+4 -3
View File
@@ -73,10 +73,8 @@
"prepublishOnly": "cp ../../README.md ./README.md"
},
"devDependencies": {
"@edge-runtime/vm": "^5.0.0",
"@modelcontextprotocol/sdk": "^1.7.0",
"@types/node": "^22.13.10",
"happy-dom": "^17.4.4",
"hono": "^4.7.4",
"next": "^15.2.2",
"p-retry": "^6.2.1",
@@ -84,7 +82,6 @@
"stream-chain": "^3.4.0",
"tsup": "^8.4.0",
"typescript": "^5.8.2",
"vitest": "^3.0.8",
"zod": "^3.24.2"
},
"peerDependencies": {
@@ -112,6 +109,10 @@
"optional": true
}
},
"repository": {
"type": "git",
"url": "https://github.com/run-llama/llama-flow.git"
},
"publishConfig": {
"access": "public"
}
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.browser.json",
"compilerOptions": {
"composite": false
},
"include": ["./src"]
}
+2 -1
View File
@@ -1,7 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"composite": true,
"outDir": "./lib/browser",
"tsBuildInfoFile": "./lib/browser/.tsbuildinfo",
"target": "es2015",
"paths": {
"@llama-flow/core": ["./src/core/index.ts"],
+8 -2
View File
@@ -1,7 +1,13 @@
{
"extends": "./tsconfig.browser.json",
"compilerOptions": {
"composite": true
"outDir": "./lib/browser-test",
"tsBuildInfoFile": "./lib/browser-test/.tsbuildinfo"
},
"include": ["./src", "./tests", "./vitest.config.ts"]
"include": ["./src", "./tests", "./vitest.config.ts"],
"references": [
{
"path": "./tsconfig.browser.json"
}
]
}
+7
View File
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"composite": false
},
"include": ["./src"]
}
+2 -23
View File
@@ -1,21 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": ".",
"target": "es2022",
"module": "esnext",
"moduleResolution": "bundler",
"verbatimModuleSyntax": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"noUncheckedIndexedAccess": true,
"strictNullChecks": true,
"exactOptionalPropertyTypes": true,
"strict": true,
"skipLibCheck": true,
"stripInternal": true,
"outDir": "./lib",
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"lib": ["ES2022", "DOM", "DOM.Iterable", "DOM.AsyncIterable"],
"paths": {
"@llama-flow/core": ["./src/core/index.ts"],
"@llama-flow/core/async-context": ["./src/async-context/index.ts"],
@@ -25,13 +12,5 @@
"@llama-flow/core/stream/*": ["./src/stream/*.ts"]
}
},
"include": ["./src"],
"references": [
{
"path": "./tsconfig.test.json"
},
{
"path": "./tsconfig.browser.json"
}
]
"include": ["./src"]
}
+8 -2
View File
@@ -1,7 +1,13 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"composite": true
"outDir": "./lib/test/browser",
"tsBuildInfoFile": "./lib/test/.tsbuildinfo"
},
"include": ["./src", "./tests", "./vitest.config.ts"]
"include": ["./src", "./tests", "./vitest.config.ts"],
"references": [
{
"path": "./tsconfig.json"
}
]
}
+7 -1
View File
@@ -7,6 +7,7 @@ export default defineConfig([
outDir: "dist",
format: ["cjs", "esm"],
external: ["@llama-flow/core/async-context"],
tsconfig: "./tsconfig.build.json",
dts: true,
sourcemap: true,
},
@@ -17,7 +18,7 @@ export default defineConfig([
outExtension: () => ({
js: ".browser.js",
}),
tsconfig: "./tsconfig.browser.json",
tsconfig: "./tsconfig.browser.build.json",
platform: "browser",
format: ["esm"],
minify: true,
@@ -28,6 +29,7 @@ export default defineConfig([
entry: ["src/async-context/*.ts"],
outDir: "async-context",
format: ["cjs", "esm"],
tsconfig: "./tsconfig.build.json",
dts: true,
sourcemap: true,
},
@@ -42,6 +44,7 @@ export default defineConfig([
"@llama-flow/core",
"@llama-flow/core/async-context",
],
tsconfig: "./tsconfig.build.json",
dts: true,
sourcemap: true,
},
@@ -51,6 +54,7 @@ export default defineConfig([
outDir: "middleware",
external: ["@llama-flow/core", "@llama-flow/core/async-context"],
format: ["esm"],
tsconfig: "./tsconfig.build.json",
dts: true,
sourcemap: true,
},
@@ -60,6 +64,7 @@ export default defineConfig([
outDir: "util",
format: ["esm"],
external: ["@llama-flow/core", "@llama-flow/core/async-context"],
tsconfig: "./tsconfig.build.json",
dts: true,
sourcemap: true,
},
@@ -69,6 +74,7 @@ export default defineConfig([
outDir: "stream",
format: ["esm"],
external: ["@llama-flow/core", "@llama-flow/core/async-context"],
tsconfig: "./tsconfig.build.json",
dts: true,
sourcemap: true,
},
+24
View File
@@ -0,0 +1,24 @@
{
"name": "@llama-flow/llamaindex",
"version": "0.0.1",
"type": "module",
"description": "LlamaIndex binding for llama-flow",
"scripts": {
"test": "vitest run"
},
"files": [
"dist",
"README.md",
"LICENSE"
],
"dependencies": {
"@llama-flow/core": "workspace:*"
},
"repository": {
"type": "git",
"url": "https://github.com/run-llama/llama-flow.git"
},
"publishConfig": {
"access": "public"
}
}
+69
View File
@@ -0,0 +1,69 @@
import {
createWorkflow,
type Handler,
type WorkflowEvent,
workflowEvent,
type WorkflowEventData,
getContext,
} from "@llama-flow/core";
import { until } from "@llama-flow/core/stream/until";
import { collect } from "@llama-flow/core/stream/consumer";
import { withStore } from "@llama-flow/core/middleware/store";
export { workflowEvent } from "@llama-flow/core";
export type StepContext<T = unknown> = ReturnType<typeof getContext> & {
get data(): T;
};
export type StepHandler<
ContextData,
Inputs extends WorkflowEvent<any>[],
Outputs extends WorkflowEventData<any>[],
> = (
context: StepContext<ContextData>,
...args: Parameters<Handler<Inputs, Outputs[number]>>
) => ReturnType<Handler<Inputs, Outputs[number]>>;
export const startEvent = workflowEvent<any, "llamaindex-start">({
debugLabel: "llamaindex-start",
});
export const stopEvent = workflowEvent<any, "llamaindex-stop">({
debugLabel: "llamaindex-stop",
});
export class Workflow<ContextData, Start, Stop> {
#workflow = withStore((data: ContextData) => data, createWorkflow());
addStep<AcceptEvents extends WorkflowEvent<any>[]>(
parameters: {
inputs: AcceptEvents;
},
handler: (
context: StepContext<ContextData>,
...args: Parameters<Handler<AcceptEvents, WorkflowEventData<any> | void>>
) => ReturnType<Handler<AcceptEvents, WorkflowEventData<any> | void>>,
) {
this.#workflow.handle(parameters.inputs, (...events) => {
const context = getContext();
const contextData = this.#workflow.getStore();
return handler(
{
...context,
get data(): ContextData {
return contextData;
},
},
...events,
);
});
}
async run(start: Start, context?: ContextData): Promise<Stop> {
const { sendEvent, stream } = this.#workflow.createContext(context!);
sendEvent(startEvent.with(start));
const events = await collect(until(stream, stopEvent));
return events.at(-1)!.data;
}
}
+107
View File
@@ -0,0 +1,107 @@
import { describe, expect, test } from "vitest";
import { startEvent, stopEvent, Workflow, workflowEvent } from "../src";
describe("workflow basic", () => {
test("basic usage", async () => {
const workflow = new Workflow<
{
foo: string;
bar: number;
},
string,
string
>();
workflow.addStep(
{
inputs: [startEvent],
},
async ({ data }, start) => {
expect(start.data).toBe("start");
expect(data.bar).toBe(42);
expect(data.foo).toBe("foo");
return stopEvent.with("stopped");
},
);
const result = await workflow.run("start", {
foo: "foo",
bar: 42,
});
expect(result).toBe("stopped");
});
test("sendEvent", async () => {
const workflow = new Workflow<
{
foo: string;
bar: number;
},
string,
string
>();
workflow.addStep(
{
inputs: [startEvent],
},
async ({ data, sendEvent }, start) => {
expect(start.data).toBe("start");
expect(data.bar).toBe(42);
expect(data.foo).toBe("foo");
sendEvent(stopEvent.with("stopped"));
},
);
const result = await workflow.run("start", {
foo: "foo",
bar: 42,
});
expect(result).toBe("stopped");
});
test("sendEvent with merge", async () => {
const workflow = new Workflow<
{
foo: string;
bar: number;
},
string,
string
>();
const aEvent = workflowEvent<number>();
const bEvent = workflowEvent<number>();
workflow.addStep(
{
inputs: [startEvent],
},
async ({ data, sendEvent }, start) => {
expect(start.data).toBe("start");
expect(data.bar).toBe(42);
expect(data.foo).toBe("foo");
sendEvent(aEvent.with(1));
setTimeout(() => {
sendEvent(bEvent.with(2));
}, 100);
},
);
workflow.addStep(
{
inputs: [aEvent, bEvent],
},
async ({ data }, a, b) => {
expect(a.data).toBe(1);
expect(b.data).toBe(2);
return stopEvent.with("stopped");
},
);
const result = await workflow.run("start", {
foo: "foo",
bar: 42,
});
expect(result).toBe("stopped");
});
});
+13
View File
@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./lib",
"tsBuildInfoFile": "./lib/.tsbuildinfo"
},
"include": ["./src"],
"references": [
{
"path": "./tsconfig.test.json"
}
]
}
+8
View File
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./lib/test/browser",
"tsBuildInfoFile": "./lib/test/.tsbuildinfo"
},
"include": ["./src", "./tests", "./vitest.config.ts"]
}
+20
View File
@@ -0,0 +1,20 @@
import { defineConfig } from "vitest/config";
import tsconfigPaths from "vite-tsconfig-paths";
export default defineConfig({
test: {
workspace: [
{
plugins: [
tsconfigPaths({
projects: ["./tsconfig.test.json"],
}),
],
test: {
name: "Node.js",
environment: "node",
},
},
],
},
});
+79 -9
View File
@@ -11,6 +11,12 @@ importers:
'@changesets/cli':
specifier: ^2.29.0
version: 2.29.0
'@edge-runtime/vm':
specifier: ^5.0.0
version: 5.0.0
happy-dom:
specifier: ^17.4.4
version: 17.4.4
husky:
specifier: ^9.1.7
version: 9.1.7
@@ -20,9 +26,15 @@ importers:
prettier:
specifier: ^3.5.3
version: 3.5.3
turbo:
specifier: ^2.5.0
version: 2.5.0
vite-tsconfig-paths:
specifier: ^5.1.4
version: 5.1.4(typescript@5.8.2)(vite@6.2.2(@types/node@22.13.10)(tsx@4.19.3)(yaml@2.7.1))
vitest:
specifier: ^3.0.8
version: 3.0.8(@edge-runtime/vm@5.0.0)(@types/node@22.13.10)(happy-dom@17.4.4)(tsx@4.19.3)(yaml@2.7.1)
demo:
dependencies:
@@ -115,18 +127,12 @@ importers:
packages/core:
devDependencies:
'@edge-runtime/vm':
specifier: ^5.0.0
version: 5.0.0
'@modelcontextprotocol/sdk':
specifier: ^1.7.0
version: 1.7.0
'@types/node':
specifier: ^22.13.10
version: 22.13.10
happy-dom:
specifier: ^17.4.4
version: 17.4.4
hono:
specifier: ^4.7.4
version: 4.7.4
@@ -148,13 +154,16 @@ importers:
typescript:
specifier: ^5.8.2
version: 5.8.2
vitest:
specifier: ^3.0.8
version: 3.0.8(@edge-runtime/vm@5.0.0)(@types/node@22.13.10)(happy-dom@17.4.4)(tsx@4.19.3)(yaml@2.7.1)
zod:
specifier: ^3.24.2
version: 3.24.2
packages/llamaindex:
dependencies:
'@llama-flow/core':
specifier: workspace:*
version: link:../core
packages:
'@babel/runtime@7.27.0':
@@ -2402,6 +2411,40 @@ packages:
engines: {node: '>=18.0.0'}
hasBin: true
turbo-darwin-64@2.5.0:
resolution: {integrity: sha512-fP1hhI9zY8hv0idym3hAaXdPi80TLovmGmgZFocVAykFtOxF+GlfIgM/l4iLAV9ObIO4SUXPVWHeBZQQ+Hpjag==}
cpu: [x64]
os: [darwin]
turbo-darwin-arm64@2.5.0:
resolution: {integrity: sha512-p9sYq7kXH7qeJwIQE86cOWv/xNqvow846l6c/qWc26Ib1ci5W7V0sI5thsrP3eH+VA0d+SHalTKg5SQXgNQBWA==}
cpu: [arm64]
os: [darwin]
turbo-linux-64@2.5.0:
resolution: {integrity: sha512-1iEln2GWiF3iPPPS1HQJT6ZCFXynJPd89gs9SkggH2EJsj3eRUSVMmMC8y6d7bBbhBFsiGGazwFIYrI12zs6uQ==}
cpu: [x64]
os: [linux]
turbo-linux-arm64@2.5.0:
resolution: {integrity: sha512-bKBcbvuQHmsX116KcxHJuAcppiiBOfivOObh2O5aXNER6mce7YDDQJy00xQQNp1DhEfcSV2uOsvb3O3nN2cbcA==}
cpu: [arm64]
os: [linux]
turbo-windows-64@2.5.0:
resolution: {integrity: sha512-9BCo8oQ7BO7J0K913Czbc3tw8QwLqn2nTe4E47k6aVYkM12ASTScweXPTuaPFP5iYXAT6z5Dsniw704Ixa5eGg==}
cpu: [x64]
os: [win32]
turbo-windows-arm64@2.5.0:
resolution: {integrity: sha512-OUHCV+ueXa3UzfZ4co/ueIHgeq9B2K48pZwIxKSm5VaLVuv8M13MhM7unukW09g++dpdrrE1w4IOVgxKZ0/exg==}
cpu: [arm64]
os: [win32]
turbo@2.5.0:
resolution: {integrity: sha512-PvSRruOsitjy6qdqwIIyolv99+fEn57gP6gn4zhsHTEcCYgXPhv6BAxzAjleS8XKpo+Y582vTTA9nuqYDmbRuA==}
hasBin: true
type-is@2.0.0:
resolution: {integrity: sha512-gd0sGezQYCbWSbkZr75mln4YBidWUN60+devscpLF5mtRDUpiaTvKpBNrdaCvel1NdR2k6vclXybU5fBd2i+nw==}
engines: {node: '>= 0.6'}
@@ -4700,6 +4743,33 @@ snapshots:
optionalDependencies:
fsevents: 2.3.3
turbo-darwin-64@2.5.0:
optional: true
turbo-darwin-arm64@2.5.0:
optional: true
turbo-linux-64@2.5.0:
optional: true
turbo-linux-arm64@2.5.0:
optional: true
turbo-windows-64@2.5.0:
optional: true
turbo-windows-arm64@2.5.0:
optional: true
turbo@2.5.0:
optionalDependencies:
turbo-darwin-64: 2.5.0
turbo-darwin-arm64: 2.5.0
turbo-linux-64: 2.5.0
turbo-linux-arm64: 2.5.0
turbo-windows-64: 2.5.0
turbo-windows-arm64: 2.5.0
type-is@2.0.0:
dependencies:
content-type: 1.0.5
+38
View File
@@ -0,0 +1,38 @@
{
"compilerOptions": {
"rootDir": ".",
"composite": true,
"target": "es2022",
"module": "esnext",
"moduleResolution": "bundler",
"verbatimModuleSyntax": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"noUncheckedIndexedAccess": true,
"strictNullChecks": true,
"exactOptionalPropertyTypes": true,
"strict": true,
"skipLibCheck": true,
"stripInternal": true,
"lib": ["ES2022", "DOM", "DOM.Iterable", "DOM.AsyncIterable"]
},
"files": [],
"include": [],
"references": [
{
"path": "./packages/core/tsconfig.test.json"
},
{
"path": "./packages/core/tsconfig.browser.test.json"
},
{
"path": "./packages/core/tsconfig.browser.json"
},
{
"path": "./packages/core/tsconfig.json"
},
{
"path": "./packages/llamaindex/tsconfig.json"
}
]
}
+15
View File
@@ -0,0 +1,15 @@
{
"$schema": "https://turbo.build/schema.json",
"tasks": {
"build": {
"outputs": ["dist/**"]
},
"test": {
"dependsOn": ["^build"]
},
"dev": {
"persistent": true,
"cache": false
}
}
}