Skip to content

Commit

Permalink
feat: improve @llamaindex/env (#787)
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 authored May 3, 2024
1 parent 2fe2b81 commit 5596e31
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/itchy-rice-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"llamaindex": patch
"@llamaindex/env": patch
---

feat: improve `@llamaindex/env`
5 changes: 4 additions & 1 deletion packages/core/jsr.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"version": "0.3.4",
"exports": "./src/index.ts",
"imports": {
"@llamaindex/env": "jsr:@llamaindex/[email protected]"
"@llamaindex/env": "jsr:@llamaindex/[email protected]"
},
"publish": {
"include": ["LICENSE", "README.md", "src/**/*.ts"]
}
}
3 changes: 3 additions & 0 deletions packages/env/jsr.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
"exports": {
".": "./src/index.ts",
"./type": "./src/type.ts"
},
"publish": {
"include": ["LICENSE", "README.md", "src/**/*.ts"]
}
}
13 changes: 12 additions & 1 deletion packages/env/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/env",
"description": "environment wrapper",
"description": "environment wrapper, supports all JS environment including node, deno, bun, edge runtime, and cloudflare worker",
"version": "0.1.0",
"type": "module",
"types": "dist/type/index.d.ts",
Expand Down Expand Up @@ -74,5 +74,16 @@
"@aws-crypto/sha256-js": "^5.2.0",
"pathe": "^1.1.2",
"readable-stream": "^4.5.2"
},
"peerDependenciesMeta": {
"@aws-crypto/sha256-js": {
"optional": true
},
"pathe": {
"optional": true
},
"readable-stream": {
"optional": true
}
}
}
13 changes: 13 additions & 0 deletions packages/env/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/**
* This module is under Node.js environment.
* It provides a set of APIs to interact with the file system, streams, and other Node.js built-in modules.
*
* Use this under "node" condition,
*
* For example:
* ```shell
* node -e "const env = require('@llamaindex/env');"
* ```
*
* @module
*/
import { ok } from "node:assert";
import { createHash, randomUUID } from "node:crypto";
import fs from "node:fs/promises";
Expand Down
7 changes: 7 additions & 0 deletions packages/env/src/index.workerd.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* This module is under Cloudflare Workers environment.
*
* Most of Node.js APIs are not available in Cloudflare Workers environment.
*
* @module
*/
import { INTERNAL_ENV } from "./utils.js";

export * from "./index.polyfill.js";
Expand Down
24 changes: 24 additions & 0 deletions packages/env/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
// DO NOT EXPOSE THIS VARIABLE TO PUBLIC, IT IS USED INTERNALLY FOR CLOUDFLARE WORKER
export const INTERNAL_ENV: Record<string, string> = {};

/**
* Set environment variables before using llamaindex, because some LLM need to access API key before running.
*
* You have to set the environment variables in Cloudflare Worker environment,
* because it doesn't have any global environment variables.
*
* @example
* ```ts
* export default {
* async fetch(
* request: Request,
* env: Env,
* ctx: ExecutionContext,
* ): Promise<Response> {
* const { setEnvs } = await import("@llamaindex/env");
* setEnvs(env);
* // ...
* return new Response("Hello, World!");
* },
* };
* ```
*
* @param envs Environment variables
*/
export function setEnvs(envs: object): void {
Object.assign(INTERNAL_ENV, envs);
}
Expand Down
9 changes: 8 additions & 1 deletion scripts/check-minor-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ if (minorVersion !== expectedMinorVersion) {
process.exit(1);
}

const packages = ["core", "env"];
const packages = ["env", "core"];
const envPackageJson = JSON.parse(
fs.readFileSync("./packages/env/package.json", "utf8"),
);
for (const pkg of packages) {
const packageJson = JSON.parse(
fs.readFileSync(`./packages/${pkg}/package.json`, "utf8"),
Expand All @@ -36,6 +39,10 @@ for (const pkg of packages) {
);

jsrJson.version = packageJson.version;
if (pkg === "core") {
jsrJson.imports["@llamaindex/env"] =
`jsr:@llamaindex/env@${envPackageJson.version}`;
}

fs.writeFileSync(
`./packages/${pkg}/jsr.json`,
Expand Down

0 comments on commit 5596e31

Please sign in to comment.