@neutrome/open-ai-router 0.1.1 → 0.1.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neutrome/open-ai-router",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/index.ts"
@@ -14,7 +14,7 @@
14
14
  "deploy": "wrangler deploy"
15
15
  },
16
16
  "dependencies": {
17
- "@neutrome/lil-engine": "0.1.1",
17
+ "@neutrome/lil-engine": "0.1.2",
18
18
  "hono": "^4.12.18"
19
19
  },
20
20
  "devDependencies": {
@@ -0,0 +1,4 @@
1
+ allowBuilds:
2
+ esbuild: true
3
+ sharp: true
4
+ workerd: true
package/src/auth/env.ts CHANGED
@@ -1,12 +1,14 @@
1
1
  import type { AuthDriver, TargetAuthContext, AuthResult } from "./types.ts";
2
2
 
3
3
  export function envAuthDriver(suffix = "_API_KEY", prefix = ""): AuthDriver {
4
+ const counters = new Map<string, number>();
5
+
4
6
  return {
5
7
  name: "env",
6
8
  collectTarget(ctx: TargetAuthContext): AuthResult | null {
7
- const key =
8
- ctx.env[`${prefix}${ctx.providerName.toUpperCase()}${suffix}`];
9
- if (typeof key !== "string" || !key) return null;
9
+ const base = `${prefix}${ctx.providerName.toUpperCase()}`;
10
+ const key = resolveKey(ctx.env, base, suffix, counters);
11
+ if (!key) return null;
10
12
  return {
11
13
  headers: { authorization: `Bearer ${key}` },
12
14
  source: "env",
@@ -14,3 +16,26 @@ export function envAuthDriver(suffix = "_API_KEY", prefix = ""): AuthDriver {
14
16
  },
15
17
  };
16
18
  }
19
+
20
+ function resolveKey(
21
+ env: Record<string, unknown>,
22
+ base: string,
23
+ suffix: string,
24
+ counters: Map<string, number>,
25
+ ): string | null {
26
+ const nValue = env[`${base}${suffix}S_N`];
27
+ if (nValue) {
28
+ const n = typeof nValue === "string" ? parseInt(nValue, 10) : typeof nValue === "number" ? nValue : 0;
29
+ if (n > 0) {
30
+ const prev = counters.get(base) ?? 0;
31
+ const index = (prev % n) + 1;
32
+ counters.set(base, prev + 1);
33
+ const key = env[`${base}${suffix}_${index}`];
34
+ if (typeof key === "string" && key) return key;
35
+ }
36
+ }
37
+
38
+ const single = env[`${base}${suffix}`];
39
+ if (typeof single === "string" && single) return single;
40
+ return null;
41
+ }