@southwind-ai/config 0.1.2 → 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/index.ts +1 -0
- package/package.json +2 -1
- package/src/ai-openai-compatible.ts +131 -0
package/index.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@southwind-ai/config",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
},
|
|
9
9
|
"files": [
|
|
10
10
|
"index.ts",
|
|
11
|
+
"src/ai-openai-compatible.ts",
|
|
11
12
|
"src/**/*.ts",
|
|
12
13
|
"!src/**/*.test.ts"
|
|
13
14
|
],
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { isIP } from "node:net";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { defineModuleConfig } from "./definition.js";
|
|
4
|
+
import {
|
|
5
|
+
loadModuleConfigSync,
|
|
6
|
+
strictInteger,
|
|
7
|
+
type ConfigEnvironment,
|
|
8
|
+
} from "./environment-loader.js";
|
|
9
|
+
import { EnvironmentSecretResolver } from "./secrets.js";
|
|
10
|
+
|
|
11
|
+
export interface OpenAiCompatibleDeploymentConfig {
|
|
12
|
+
baseUrl: string;
|
|
13
|
+
model: string;
|
|
14
|
+
apiKey?: string;
|
|
15
|
+
connectTimeoutMs: number;
|
|
16
|
+
requestTimeoutMs: number;
|
|
17
|
+
maxResponseBytes: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const deploymentSchema: z.ZodType<OpenAiCompatibleDeploymentConfig> =
|
|
21
|
+
z.strictObject({
|
|
22
|
+
baseUrl: z.string().transform(normalizeOpenAiCompatibleBaseUrl),
|
|
23
|
+
model: z.string().trim().min(1).max(200),
|
|
24
|
+
apiKey: z.string().trim().min(1).optional(),
|
|
25
|
+
connectTimeoutMs: z.number().int().min(100).max(30_000),
|
|
26
|
+
requestTimeoutMs: z.number().int().min(1_000).max(300_000),
|
|
27
|
+
maxResponseBytes: z
|
|
28
|
+
.number()
|
|
29
|
+
.int()
|
|
30
|
+
.min(1_024)
|
|
31
|
+
.max(16 * 1_024 * 1_024),
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
export const openAiCompatibleDeploymentConfigDefinition =
|
|
35
|
+
defineModuleConfig<OpenAiCompatibleDeploymentConfig>({
|
|
36
|
+
key: "ai.openai-compatible",
|
|
37
|
+
label: "OpenAI-compatible 模型部署",
|
|
38
|
+
ownedEnvironmentPrefixes: ["QWEN_", "AI_PROVIDER_"],
|
|
39
|
+
schema: deploymentSchema,
|
|
40
|
+
environment: [
|
|
41
|
+
{ key: "QWEN_API_URL", path: "baseUrl" },
|
|
42
|
+
{ key: "QWEN_MODEL", path: "model" },
|
|
43
|
+
{
|
|
44
|
+
key: "QWEN_API_KEY",
|
|
45
|
+
path: "apiKey",
|
|
46
|
+
secret: { provider: "env" },
|
|
47
|
+
},
|
|
48
|
+
integer("AI_PROVIDER_CONNECT_TIMEOUT_MS", "connectTimeoutMs", 5_000),
|
|
49
|
+
integer("AI_PROVIDER_REQUEST_TIMEOUT_MS", "requestTimeoutMs", 60_000),
|
|
50
|
+
integer(
|
|
51
|
+
"AI_PROVIDER_MAX_RESPONSE_BYTES",
|
|
52
|
+
"maxResponseBytes",
|
|
53
|
+
2 * 1_024 * 1_024,
|
|
54
|
+
),
|
|
55
|
+
],
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
export function loadOpenAiCompatibleDeploymentConfig(
|
|
59
|
+
environment: ConfigEnvironment = process.env,
|
|
60
|
+
): OpenAiCompatibleDeploymentConfig {
|
|
61
|
+
return loadModuleConfigSync(
|
|
62
|
+
openAiCompatibleDeploymentConfigDefinition,
|
|
63
|
+
environment,
|
|
64
|
+
new EnvironmentSecretResolver(environment),
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function normalizeOpenAiCompatibleBaseUrl(raw: string): string {
|
|
69
|
+
let url: URL;
|
|
70
|
+
try {
|
|
71
|
+
url = new URL(raw.trim());
|
|
72
|
+
} catch {
|
|
73
|
+
throw new Error("OpenAI-compatible Endpoint 无效");
|
|
74
|
+
}
|
|
75
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
76
|
+
throw new Error("OpenAI-compatible Endpoint 只允许 HTTP 或 HTTPS");
|
|
77
|
+
}
|
|
78
|
+
if (url.username || url.password || url.search || url.hash) {
|
|
79
|
+
throw new Error("OpenAI-compatible Endpoint 不允许凭据、查询串或片段");
|
|
80
|
+
}
|
|
81
|
+
const hostname = url.hostname.toLowerCase();
|
|
82
|
+
if (isCloudMetadataHost(hostname)) {
|
|
83
|
+
throw new Error("OpenAI-compatible Endpoint 禁止访问云 metadata");
|
|
84
|
+
}
|
|
85
|
+
if (url.protocol === "http:" && !isPrivateHost(hostname)) {
|
|
86
|
+
throw new Error("公网 OpenAI-compatible Endpoint 必须使用 HTTPS");
|
|
87
|
+
}
|
|
88
|
+
url.pathname = url.pathname.replace(/\/+$/, "");
|
|
89
|
+
return url.toString().replace(/\/$/, "");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function integer(key: string, path: string, defaultValue: number) {
|
|
93
|
+
return { key, path, defaultValue, decode: strictInteger };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function isPrivateHost(hostname: string): boolean {
|
|
97
|
+
if (
|
|
98
|
+
hostname === "localhost" ||
|
|
99
|
+
hostname.endsWith(".localhost") ||
|
|
100
|
+
hostname.endsWith(".internal") ||
|
|
101
|
+
hostname.endsWith(".local") ||
|
|
102
|
+
(!hostname.includes(".") && isIP(hostname) === 0)
|
|
103
|
+
) {
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
if (isIP(hostname) === 4) {
|
|
107
|
+
const [first = 0, second = 0] = hostname.split(".").map(Number);
|
|
108
|
+
return (
|
|
109
|
+
first === 10 ||
|
|
110
|
+
first === 127 ||
|
|
111
|
+
(first === 172 && second >= 16 && second <= 31) ||
|
|
112
|
+
(first === 192 && second === 168)
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
if (isIP(hostname) === 6) {
|
|
116
|
+
return (
|
|
117
|
+
hostname === "::1" ||
|
|
118
|
+
hostname.startsWith("fc") ||
|
|
119
|
+
hostname.startsWith("fd")
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function isCloudMetadataHost(hostname: string): boolean {
|
|
126
|
+
return (
|
|
127
|
+
hostname === "169.254.169.254" ||
|
|
128
|
+
hostname === "metadata.google.internal" ||
|
|
129
|
+
hostname === "metadata.azure.internal"
|
|
130
|
+
);
|
|
131
|
+
}
|