@neutrome/open-ai-router 0.1.18 → 0.3.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/README.md +8 -4
- package/package.json +3 -2
- package/src/admission/access.ts +14 -0
- package/src/admission/jwt.test.ts +96 -0
- package/src/admission/jwt.ts +221 -0
- package/src/admission/types.ts +14 -0
- package/src/app/factory.test.ts +14 -11
- package/src/app/factory.ts +41 -31
- package/src/app/handlers.ts +5 -9
- package/src/example.test.ts +14 -14
- package/src/example.ts +5 -2
- package/src/index.ts +45 -12
- package/src/model.example.ts +61 -0
- package/src/observer.ts +2 -1
- package/src/router/audit.ts +21 -0
- package/src/router/config.ts +8 -8
- package/src/router/errors.ts +60 -0
- package/src/router/execute.test.ts +12 -12
- package/src/router/execute.ts +47 -32
- package/src/router/execution-runtime.test.ts +327 -0
- package/src/router/execution-runtime.ts +632 -0
- package/src/router/execution-types.ts +80 -0
- package/src/router/index.ts +28 -6
- package/src/router/mcp.ts +2 -2
- package/src/router/resolve.test.ts +10 -10
- package/src/router/resolve.ts +15 -15
- package/src/router/runtime.ts +29 -29
- package/src/router/targets.ts +17 -0
- package/src/{auth → upstream-auth}/config.ts +1 -1
- package/src/{auth → upstream-auth}/env.ts +2 -2
- package/src/{auth → upstream-auth}/proxy.ts +2 -2
- package/src/{auth → upstream-auth}/registry.ts +4 -4
- package/src/{auth → upstream-auth}/types.ts +1 -1
- package/src/worker.ts +2 -2
- package/src/auth/apikey.ts +0 -51
package/src/router/index.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
export { createAuditEvent, createInMemoryAuditSink } from "./audit.ts";
|
|
2
|
+
export { ExecutionError, isExecutionError } from "./errors.ts";
|
|
3
|
+
export { createExecutionRuntime } from "./execution-runtime.ts";
|
|
1
4
|
export { createRouterRuntime } from "./runtime.ts";
|
|
2
5
|
export {
|
|
3
6
|
createFetchProviderInvoker,
|
|
@@ -5,12 +8,14 @@ export {
|
|
|
5
8
|
handleAnthropicMessages,
|
|
6
9
|
handleChatCompletions,
|
|
7
10
|
handleGoogleGenAI,
|
|
11
|
+
parseRequestProgram,
|
|
8
12
|
handleResponses,
|
|
13
|
+
ProviderHttpError,
|
|
9
14
|
resolveRequestTarget,
|
|
10
15
|
RouterError,
|
|
11
|
-
ProviderHttpError,
|
|
12
16
|
} from "./execute.ts";
|
|
13
17
|
export { listConfiguredModels, resolveInvocationTarget, resolveInvocationTargets } from "./resolve.ts";
|
|
18
|
+
export { executionTargetAuditRef, executionTargetId } from "./targets.ts";
|
|
14
19
|
export {
|
|
15
20
|
createRemoteMcpToolSet,
|
|
16
21
|
createStreamableHttpMcpClient,
|
|
@@ -18,23 +23,40 @@ export {
|
|
|
18
23
|
wireToolName,
|
|
19
24
|
} from "./mcp.ts";
|
|
20
25
|
export type {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
ExecutorRouteConfig,
|
|
26
|
+
ModelNamespaceConfig,
|
|
27
|
+
ModelRouteConfig,
|
|
24
28
|
ProviderApiStyle,
|
|
25
29
|
ProviderTargetConfig,
|
|
26
30
|
RemoteMcpServerConfig,
|
|
27
31
|
RemoteMcpToolConfig,
|
|
28
32
|
RouterConfig,
|
|
33
|
+
UpstreamAuthConfig,
|
|
29
34
|
} from "./config.ts";
|
|
30
35
|
export type {
|
|
31
36
|
CreateRouterOptions,
|
|
32
|
-
|
|
33
|
-
|
|
37
|
+
ModelNamespaceRuntime,
|
|
38
|
+
ModelRouteRuntime,
|
|
34
39
|
ProviderRuntime,
|
|
35
40
|
RemoteMcpServerRuntime,
|
|
36
41
|
RouterRuntime,
|
|
37
42
|
} from "./runtime.ts";
|
|
43
|
+
export type {
|
|
44
|
+
AuditEvent,
|
|
45
|
+
AuditEventInput,
|
|
46
|
+
ExecutionErrorDetails,
|
|
47
|
+
ExecutionErrorKind,
|
|
48
|
+
ExecutionErrorStage,
|
|
49
|
+
ExecutionRuntime,
|
|
50
|
+
ExecutionRuntimeOptions,
|
|
51
|
+
ExecutionTarget,
|
|
52
|
+
Executor,
|
|
53
|
+
InvokeOptions,
|
|
54
|
+
ProgramTransform,
|
|
55
|
+
ProviderInvocationContext,
|
|
56
|
+
ProviderInvoker,
|
|
57
|
+
ResolveExecutor,
|
|
58
|
+
ResolveTarget,
|
|
59
|
+
} from "./execution-types.ts";
|
|
38
60
|
export type {
|
|
39
61
|
HandleAnthropicMessagesOptions,
|
|
40
62
|
HandleChatCompletionsOptions,
|
package/src/router/mcp.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
2
2
|
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
3
3
|
import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
|
|
4
|
-
import type {
|
|
4
|
+
import type { ExecutorContext, Tool } from "@neutrome/lilsdk-ts";
|
|
5
5
|
import type { RemoteMcpServerRuntime, RouterRuntime } from "./runtime.ts";
|
|
6
6
|
|
|
7
7
|
export type RemoteMcpToolCallClient = {
|
|
@@ -44,7 +44,7 @@ export function createRemoteMcpToolSet(
|
|
|
44
44
|
name,
|
|
45
45
|
description: snapshot.description || `Remote MCP tool ${server.name}/${snapshot.name}`,
|
|
46
46
|
schema: normalizeSchema(snapshot.inputSchema),
|
|
47
|
-
async execute(args: Record<string, unknown>, ctx:
|
|
47
|
+
async execute(args: Record<string, unknown>, ctx: ExecutorContext) {
|
|
48
48
|
const client = await pool.get(server, ctx.signal);
|
|
49
49
|
const result = await client.callTool(snapshot.name, args);
|
|
50
50
|
return stringifyMcpToolResult(result);
|
|
@@ -13,12 +13,12 @@ describe("router resolution", () => {
|
|
|
13
13
|
exports: ["*"],
|
|
14
14
|
},
|
|
15
15
|
},
|
|
16
|
-
|
|
16
|
+
modelNamespaces: {
|
|
17
17
|
enei: {
|
|
18
18
|
exports: ["enei-*"],
|
|
19
19
|
models: {
|
|
20
|
-
"enei-1": {
|
|
21
|
-
"enei-1-pro": {
|
|
20
|
+
"enei-1": { executorId: "enei-1" },
|
|
21
|
+
"enei-1-pro": { executorId: "enei-1-pro", transforms: ["reasoning:high"] },
|
|
22
22
|
},
|
|
23
23
|
},
|
|
24
24
|
},
|
|
@@ -30,7 +30,7 @@ describe("router resolution", () => {
|
|
|
30
30
|
expect(resolved.namespace).toBe("enei");
|
|
31
31
|
expect(resolved.target).toEqual({
|
|
32
32
|
kind: "executor",
|
|
33
|
-
|
|
33
|
+
executorId: "enei-1",
|
|
34
34
|
alias: "enei-1",
|
|
35
35
|
transforms: ["slwin:8"],
|
|
36
36
|
});
|
|
@@ -46,7 +46,7 @@ describe("router resolution", () => {
|
|
|
46
46
|
exports: ["google/*"],
|
|
47
47
|
},
|
|
48
48
|
},
|
|
49
|
-
|
|
49
|
+
modelNamespaces: {},
|
|
50
50
|
},
|
|
51
51
|
knownSuffixes: ["slwin", "kvtools"],
|
|
52
52
|
});
|
|
@@ -71,7 +71,7 @@ describe("router resolution", () => {
|
|
|
71
71
|
exports: ["openai/gpt-4.1-mini"],
|
|
72
72
|
},
|
|
73
73
|
},
|
|
74
|
-
|
|
74
|
+
modelNamespaces: {
|
|
75
75
|
semantyka: {
|
|
76
76
|
exports: ["enei-*"],
|
|
77
77
|
models: {
|
|
@@ -103,12 +103,12 @@ describe("router resolution", () => {
|
|
|
103
103
|
exports: ["*"],
|
|
104
104
|
},
|
|
105
105
|
},
|
|
106
|
-
|
|
106
|
+
modelNamespaces: {
|
|
107
107
|
enei: {
|
|
108
108
|
exports: ["enei-*"],
|
|
109
109
|
models: {
|
|
110
|
-
"enei-1": {
|
|
111
|
-
"enei-1-pro": {
|
|
110
|
+
"enei-1": { executorId: "enei-1" },
|
|
111
|
+
"enei-1-pro": { executorId: "enei-1-pro" },
|
|
112
112
|
},
|
|
113
113
|
},
|
|
114
114
|
},
|
|
@@ -131,7 +131,7 @@ describe("router resolution", () => {
|
|
|
131
131
|
exports: ["*"],
|
|
132
132
|
},
|
|
133
133
|
},
|
|
134
|
-
|
|
134
|
+
modelNamespaces: {
|
|
135
135
|
default: {
|
|
136
136
|
exports: ["*"],
|
|
137
137
|
models: {
|
package/src/router/resolve.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ExecutionTarget } from "@neutrome/
|
|
1
|
+
import type { ExecutionTarget } from "@neutrome/lilsdk-ts";
|
|
2
2
|
import { parseModelSyntax, type ModelSuffixSpec } from "../util/model-syntax.ts";
|
|
3
3
|
import type { RouterRuntime } from "./runtime.ts";
|
|
4
4
|
|
|
@@ -35,10 +35,10 @@ export function resolveInvocationTargets(
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
const matches: ResolvedTarget[] = [];
|
|
38
|
-
for (const namespace of runtime.
|
|
39
|
-
const
|
|
40
|
-
const route =
|
|
41
|
-
if (route &&
|
|
38
|
+
for (const namespace of runtime.modelNamespaceOrder) {
|
|
39
|
+
const modelNamespace = runtime.modelNamespaces.get(namespace)!;
|
|
40
|
+
const route = modelNamespace.routes.get(parsed.baseModel);
|
|
41
|
+
if (route && modelNamespace.exportsMatcher(parsed.baseModel)) {
|
|
42
42
|
matches.push({
|
|
43
43
|
requestedModel,
|
|
44
44
|
namespace,
|
|
@@ -56,10 +56,10 @@ export function resolveInvocationTargets(
|
|
|
56
56
|
export function listConfiguredModels(runtime: RouterRuntime): string[] {
|
|
57
57
|
const models: string[] = [];
|
|
58
58
|
|
|
59
|
-
for (const namespace of runtime.
|
|
60
|
-
const
|
|
61
|
-
for (const alias of
|
|
62
|
-
if (
|
|
59
|
+
for (const namespace of runtime.modelNamespaceOrder) {
|
|
60
|
+
const modelNamespace = runtime.modelNamespaces.get(namespace)!;
|
|
61
|
+
for (const alias of modelNamespace.routes.keys()) {
|
|
62
|
+
if (modelNamespace.exportsMatcher(alias)) {
|
|
63
63
|
models.push(`${namespace}/${alias}`);
|
|
64
64
|
}
|
|
65
65
|
}
|
|
@@ -76,15 +76,15 @@ function resolveQualified(
|
|
|
76
76
|
suffixes: ModelSuffixSpec[],
|
|
77
77
|
): ResolvedTarget[] {
|
|
78
78
|
const provider = runtime.providers.get(namespace);
|
|
79
|
-
const
|
|
79
|
+
const modelNamespace = runtime.modelNamespaces.get(namespace);
|
|
80
80
|
|
|
81
|
-
if (provider &&
|
|
82
|
-
throw new Error(`Ambiguous namespace \`${namespace}\`: both provider and
|
|
81
|
+
if (provider && modelNamespace) {
|
|
82
|
+
throw new Error(`Ambiguous namespace \`${namespace}\`: both provider and model namespace are registered`);
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
if (
|
|
86
|
-
const route =
|
|
87
|
-
if (!route || !
|
|
85
|
+
if (modelNamespace) {
|
|
86
|
+
const route = modelNamespace.routes.get(model);
|
|
87
|
+
if (!route || !modelNamespace.exportsMatcher(model)) {
|
|
88
88
|
return [];
|
|
89
89
|
}
|
|
90
90
|
return [{
|
package/src/router/runtime.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type { ExecutionTarget } from "@neutrome/
|
|
2
|
-
import type {
|
|
3
|
-
import {
|
|
1
|
+
import type { ExecutionTarget } from "@neutrome/lilsdk-ts";
|
|
2
|
+
import type { UpstreamAuthDriver } from "../upstream-auth/types.ts";
|
|
3
|
+
import { buildUpstreamAuthChain } from "../upstream-auth/registry.ts";
|
|
4
4
|
import { createExportsMatcher } from "../util/glob.ts";
|
|
5
5
|
import type {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
ModelNamespaceConfig,
|
|
7
|
+
ModelRouteConfig,
|
|
8
8
|
ProviderTargetConfig,
|
|
9
9
|
RemoteMcpServerConfig,
|
|
10
10
|
RouterConfig,
|
|
@@ -19,27 +19,27 @@ export type ProviderRuntime = {
|
|
|
19
19
|
exportsMatcher: (model: string) => boolean;
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
-
export type
|
|
22
|
+
export type ModelRouteRuntime = {
|
|
23
23
|
requestedAlias: string;
|
|
24
24
|
target: ExecutionTarget;
|
|
25
25
|
transforms: readonly string[];
|
|
26
26
|
mcps: readonly string[];
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
-
export type
|
|
29
|
+
export type ModelNamespaceRuntime = {
|
|
30
30
|
name: string;
|
|
31
31
|
exportsMatcher: (model: string) => boolean;
|
|
32
|
-
routes: ReadonlyMap<string,
|
|
32
|
+
routes: ReadonlyMap<string, ModelRouteRuntime>;
|
|
33
33
|
};
|
|
34
34
|
|
|
35
35
|
export type RouterRuntime = {
|
|
36
36
|
config: RouterConfig;
|
|
37
37
|
providers: ReadonlyMap<string, ProviderRuntime>;
|
|
38
38
|
providerOrder: readonly string[];
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
modelNamespaces: ReadonlyMap<string, ModelNamespaceRuntime>;
|
|
40
|
+
modelNamespaceOrder: readonly string[];
|
|
41
41
|
mcps: ReadonlyMap<string, RemoteMcpServerRuntime>;
|
|
42
|
-
|
|
42
|
+
upstreamAuthChain: readonly UpstreamAuthDriver[];
|
|
43
43
|
knownSuffixes: ReadonlySet<string>;
|
|
44
44
|
fetchImpl: typeof fetch;
|
|
45
45
|
};
|
|
@@ -52,24 +52,24 @@ export type CreateRouterOptions = {
|
|
|
52
52
|
config: RouterConfig;
|
|
53
53
|
fetchImpl?: typeof fetch;
|
|
54
54
|
knownSuffixes?: string[];
|
|
55
|
-
|
|
55
|
+
upstreamAuthDrivers?: readonly UpstreamAuthDriver[];
|
|
56
56
|
};
|
|
57
57
|
|
|
58
58
|
export function createRouterRuntime(options: CreateRouterOptions): RouterRuntime {
|
|
59
59
|
const providers = new Map<string, ProviderRuntime>();
|
|
60
|
-
const
|
|
60
|
+
const modelNamespaces = new Map<string, ModelNamespaceRuntime>();
|
|
61
61
|
const mcps = new Map<string, RemoteMcpServerRuntime>();
|
|
62
62
|
const providerOrder: string[] = [];
|
|
63
|
-
const
|
|
63
|
+
const modelNamespaceOrder: string[] = [];
|
|
64
64
|
|
|
65
65
|
for (const [name, config] of Object.entries(options.config.providers)) {
|
|
66
66
|
providerOrder.push(name);
|
|
67
67
|
providers.set(name, buildProviderRuntime(name, config));
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
for (const [name, config] of Object.entries(options.config.
|
|
71
|
-
|
|
72
|
-
|
|
70
|
+
for (const [name, config] of Object.entries(options.config.modelNamespaces)) {
|
|
71
|
+
modelNamespaceOrder.push(name);
|
|
72
|
+
modelNamespaces.set(name, buildModelNamespaceRuntime(name, config));
|
|
73
73
|
}
|
|
74
74
|
for (const [name, config] of Object.entries(options.config.mcps ?? {})) {
|
|
75
75
|
mcps.set(name, { name, ...config });
|
|
@@ -79,10 +79,10 @@ export function createRouterRuntime(options: CreateRouterOptions): RouterRuntime
|
|
|
79
79
|
config: options.config,
|
|
80
80
|
providers,
|
|
81
81
|
providerOrder,
|
|
82
|
-
|
|
83
|
-
|
|
82
|
+
modelNamespaces,
|
|
83
|
+
modelNamespaceOrder,
|
|
84
84
|
mcps,
|
|
85
|
-
|
|
85
|
+
upstreamAuthChain: options.upstreamAuthDrivers ?? buildUpstreamAuthChain(options.config.upstreamAuth),
|
|
86
86
|
knownSuffixes: new Set(options.knownSuffixes ?? ["slwin", "kvtools"]),
|
|
87
87
|
fetchImpl: options.fetchImpl ?? globalThis.fetch.bind(globalThis),
|
|
88
88
|
};
|
|
@@ -99,10 +99,10 @@ function buildProviderRuntime(name: string, config: ProviderTargetConfig): Provi
|
|
|
99
99
|
};
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
function
|
|
103
|
-
const routes = new Map<string,
|
|
102
|
+
function buildModelNamespaceRuntime(name: string, config: ModelNamespaceConfig): ModelNamespaceRuntime {
|
|
103
|
+
const routes = new Map<string, ModelRouteRuntime>();
|
|
104
104
|
for (const [alias, route] of Object.entries(config.models)) {
|
|
105
|
-
routes.set(alias,
|
|
105
|
+
routes.set(alias, buildModelRoute(alias, route));
|
|
106
106
|
}
|
|
107
107
|
|
|
108
108
|
return {
|
|
@@ -112,7 +112,7 @@ function buildExecutorRuntime(name: string, config: ExecutorNamespaceConfig): Ex
|
|
|
112
112
|
};
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
-
function
|
|
115
|
+
function buildModelRoute(alias: string, route: ModelRouteConfig): ModelRouteRuntime {
|
|
116
116
|
if ("provider" in route) {
|
|
117
117
|
return {
|
|
118
118
|
requestedAlias: alias,
|
|
@@ -128,12 +128,12 @@ function buildExecutorRoute(alias: string, route: ExecutorRouteConfig): Executor
|
|
|
128
128
|
}
|
|
129
129
|
|
|
130
130
|
return {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
131
|
+
requestedAlias: alias,
|
|
132
|
+
transforms: route.transforms ?? [],
|
|
133
|
+
mcps: route.mcps ?? [],
|
|
134
|
+
target: {
|
|
135
135
|
kind: "executor",
|
|
136
|
-
|
|
136
|
+
executorId: route.executorId,
|
|
137
137
|
alias: route.alias ?? alias,
|
|
138
138
|
transforms: route.transforms ?? [],
|
|
139
139
|
},
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ExecutionTarget } from "./execution-types.ts";
|
|
2
|
+
|
|
3
|
+
export function executionTargetId(target: ExecutionTarget): string {
|
|
4
|
+
return target.kind === "provider"
|
|
5
|
+
? target.provider ? `${target.provider}/${target.model}` : target.model
|
|
6
|
+
: `${target.executorId}/${target.alias}`;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function executionTargetAuditRef(target: ExecutionTarget): {
|
|
10
|
+
kind: "provider" | "executor";
|
|
11
|
+
id: string;
|
|
12
|
+
} {
|
|
13
|
+
return {
|
|
14
|
+
kind: target.kind,
|
|
15
|
+
id: executionTargetId(target),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { AuthResult, TargetAuthContext, UpstreamAuthDriver } from "./types.ts";
|
|
2
2
|
|
|
3
|
-
export function envAuthDriver(suffix = "_API_KEY", prefix = ""):
|
|
3
|
+
export function envAuthDriver(suffix = "_API_KEY", prefix = ""): UpstreamAuthDriver {
|
|
4
4
|
const counters = new Map<string, number>();
|
|
5
5
|
|
|
6
6
|
return {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { AuthResult, RequestContext, TargetAuthContext, UpstreamAuthDriver } from "./types.ts";
|
|
2
2
|
|
|
3
|
-
export function proxyAuthDriver(headers: string[]):
|
|
3
|
+
export function proxyAuthDriver(headers: string[]): UpstreamAuthDriver {
|
|
4
4
|
return {
|
|
5
5
|
name: "proxy",
|
|
6
6
|
collectIncoming(ctx: RequestContext) {
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { UpstreamAuthConfigShape } from "./config.ts";
|
|
2
|
+
import type { UpstreamAuthDriver } from "./types.ts";
|
|
3
3
|
import { proxyAuthDriver } from "./proxy.ts";
|
|
4
4
|
import { envAuthDriver } from "./env.ts";
|
|
5
5
|
|
|
6
|
-
export function
|
|
6
|
+
export function buildUpstreamAuthChain(config?: UpstreamAuthConfigShape): UpstreamAuthDriver[] {
|
|
7
7
|
if (!config) return [proxyAuthDriver(["authorization"]), envAuthDriver()];
|
|
8
8
|
const order = config.order ?? ["proxy", "env"];
|
|
9
|
-
const drivers:
|
|
9
|
+
const drivers: UpstreamAuthDriver[] = [];
|
|
10
10
|
for (const name of order) {
|
|
11
11
|
switch (name) {
|
|
12
12
|
case "proxy":
|
package/src/worker.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { createApp } from "./example.ts";
|
|
|
5
5
|
|
|
6
6
|
const config: RouterConfig = {
|
|
7
7
|
trace: true,
|
|
8
|
-
|
|
8
|
+
upstreamAuth: {
|
|
9
9
|
order: ["proxy", "env"],
|
|
10
10
|
proxy: {
|
|
11
11
|
headers: ["authorization", "x-openairouter-token"],
|
|
@@ -18,7 +18,7 @@ const config: RouterConfig = {
|
|
|
18
18
|
exports: ["*:free"],
|
|
19
19
|
},
|
|
20
20
|
},
|
|
21
|
-
|
|
21
|
+
modelNamespaces: {},
|
|
22
22
|
};
|
|
23
23
|
|
|
24
24
|
let appPromise: Promise<ReturnType<typeof createApp>> | undefined;
|
package/src/auth/apikey.ts
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
export type ApiKeyEntry = {
|
|
2
|
-
tokenHash: string;
|
|
3
|
-
allowedModels: string[] | null;
|
|
4
|
-
allowedNamespaces: string[] | null;
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
export type ApiKeyAuth = {
|
|
8
|
-
authenticate(request: Request): Promise<ApiKeyEntry | null>;
|
|
9
|
-
canAccess(key: ApiKeyEntry, modelId: string): boolean;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export function createApiKeyAuth(keys: readonly ApiKeyEntry[]): ApiKeyAuth {
|
|
13
|
-
return {
|
|
14
|
-
async authenticate(request) {
|
|
15
|
-
const token = readApiToken(request);
|
|
16
|
-
if (!token) return null;
|
|
17
|
-
const hash = await sha256Hex(token);
|
|
18
|
-
return keys.find((k) => k.tokenHash === hash) ?? null;
|
|
19
|
-
},
|
|
20
|
-
|
|
21
|
-
canAccess(key, modelId) {
|
|
22
|
-
const hasModelRestriction = key.allowedModels && key.allowedModels.length > 0;
|
|
23
|
-
const hasNamespaceRestriction = key.allowedNamespaces && key.allowedNamespaces.length > 0;
|
|
24
|
-
if (!hasModelRestriction && !hasNamespaceRestriction) return true;
|
|
25
|
-
if (hasModelRestriction && key.allowedModels!.includes(modelId)) return true;
|
|
26
|
-
const ns = modelId.split("/")[0] ?? "";
|
|
27
|
-
return !!(hasNamespaceRestriction && key.allowedNamespaces!.includes(ns));
|
|
28
|
-
},
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function readApiToken(request: Request): string | null {
|
|
33
|
-
const authHeader = request.headers.get("authorization");
|
|
34
|
-
const bearerMatch = authHeader?.match(/^Bearer\s+(.+)$/i);
|
|
35
|
-
if (bearerMatch?.[1]) return bearerMatch[1];
|
|
36
|
-
|
|
37
|
-
const headerToken = request.headers.get("x-api-key")
|
|
38
|
-
?? request.headers.get("x-goog-api-key");
|
|
39
|
-
if (headerToken) return headerToken;
|
|
40
|
-
|
|
41
|
-
const url = new URL(request.url);
|
|
42
|
-
return url.searchParams.get("key") ?? url.searchParams.get("api_key");
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
async function sha256Hex(value: string): Promise<string> {
|
|
46
|
-
const bytes = new TextEncoder().encode(value);
|
|
47
|
-
const digest = await crypto.subtle.digest("SHA-256", bytes);
|
|
48
|
-
return Array.from(new Uint8Array(digest))
|
|
49
|
-
.map((b) => b.toString(16).padStart(2, "0"))
|
|
50
|
-
.join("");
|
|
51
|
-
}
|