@aspruyt/xfg 8.4.0 → 8.5.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/dist/ai/change-describer.js +1 -0
- package/dist/ai/client-factory.d.ts +1 -1
- package/dist/ai/client-factory.js +66 -2
- package/dist/config/types.d.ts +2 -0
- package/dist/config/validators/ai-validator.js +6 -2
- package/dist/shared/credential-env.d.ts +7 -1
- package/dist/shared/credential-env.js +21 -2
- package/package.json +1 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { AiProviderOptions, FetchFn, IAiClient } from "./types.js";
|
|
2
|
-
export declare function createAiClient(
|
|
2
|
+
export declare function createAiClient(configured: AiProviderOptions, env: Record<string, string | undefined>, fetch: FetchFn, path?: string): IAiClient;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ValidationError } from "../shared/errors.js";
|
|
2
|
-
import { assertAiHeadersEnvAllowed, assertAiKeyEnvAllowed, defaultAiKeyEnv, isDefaultAiBaseUrl, } from "../shared/credential-env.js";
|
|
2
|
+
import { assertAiBaseUrlEnvAllowed, assertAiHeadersEnvAllowed, assertAiKeyEnvAllowed, defaultAiKeyEnv, isDefaultAiBaseUrl, } from "../shared/credential-env.js";
|
|
3
3
|
import { AnthropicClient, DEFAULT_ANTHROPIC_MODEL, } from "./anthropic-client.js";
|
|
4
4
|
import { DEFAULT_OPENAI_BASE_URL, OpenAICompatibleClient, } from "./openai-compatible-client.js";
|
|
5
5
|
/** The default key env only applies to the provider's own API. */
|
|
@@ -25,6 +25,59 @@ function readApiKey(env, keyEnv) {
|
|
|
25
25
|
}
|
|
26
26
|
return value;
|
|
27
27
|
}
|
|
28
|
+
function resolveBaseUrl(options, env, path) {
|
|
29
|
+
assertAiBaseUrlEnvAllowed(options.baseUrlEnv, options.baseUrl, path);
|
|
30
|
+
if (options.baseUrlEnv === undefined)
|
|
31
|
+
return options.baseUrl;
|
|
32
|
+
const value = readEnv(env, options.baseUrlEnv);
|
|
33
|
+
if (!value) {
|
|
34
|
+
throw new ValidationError(`${options.baseUrlEnv} is not set (required for ${path}.baseUrlEnv)`);
|
|
35
|
+
}
|
|
36
|
+
if (!isHttpUrl(value)) {
|
|
37
|
+
throw new ValidationError(`${options.baseUrlEnv} does not hold an http(s) URL (used for ${path}.baseUrlEnv)`);
|
|
38
|
+
}
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
// Userinfo is refused because undici rejects it and echoes the whole URL in its error.
|
|
42
|
+
function isHttpUrl(value) {
|
|
43
|
+
try {
|
|
44
|
+
const { protocol, username, password } = new URL(value);
|
|
45
|
+
if (username || password)
|
|
46
|
+
return false;
|
|
47
|
+
return protocol === "https:" || protocol === "http:";
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function escapeRegExp(value) {
|
|
54
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, String.raw `\$&`);
|
|
55
|
+
}
|
|
56
|
+
/** Replaces the resolved host in error text with the env var name that supplied it. */
|
|
57
|
+
class HostRedactingClient {
|
|
58
|
+
inner;
|
|
59
|
+
envName;
|
|
60
|
+
pattern;
|
|
61
|
+
constructor(inner, baseUrl, envName) {
|
|
62
|
+
this.inner = inner;
|
|
63
|
+
this.envName = envName;
|
|
64
|
+
const { host, hostname } = new URL(baseUrl);
|
|
65
|
+
const hosts = [...new Set([host, hostname])].sort((a, b) => b.length - a.length);
|
|
66
|
+
this.pattern = new RegExp(hosts.map(escapeRegExp).join("|"), "gi");
|
|
67
|
+
}
|
|
68
|
+
async complete(system, user, schema) {
|
|
69
|
+
try {
|
|
70
|
+
return await this.inner.complete(system, user, schema);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (!(error instanceof Error))
|
|
74
|
+
throw error;
|
|
75
|
+
const redacted = new Error(error.message.replace(this.pattern, () => this.envName));
|
|
76
|
+
// Keep fields such as retryAfter that withRetry reads.
|
|
77
|
+
throw Object.assign(redacted, { ...error });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
28
81
|
function resolveHeaders(options, env, path) {
|
|
29
82
|
const headers = {};
|
|
30
83
|
for (const [name, envName] of Object.entries(options.headersEnv ?? {})) {
|
|
@@ -39,7 +92,7 @@ function resolveHeaders(options, env, path) {
|
|
|
39
92
|
}
|
|
40
93
|
return headers;
|
|
41
94
|
}
|
|
42
|
-
|
|
95
|
+
function buildClient(options, env, fetch, path) {
|
|
43
96
|
assertAiKeyEnvAllowed(options.provider, options.apiKeyEnv, options.baseUrl, path);
|
|
44
97
|
assertAiHeadersEnvAllowed(options.headersEnv, path);
|
|
45
98
|
const keyEnv = keyEnvFor(options);
|
|
@@ -75,3 +128,14 @@ export function createAiClient(options, env, fetch, path = "prOptions.ai") {
|
|
|
75
128
|
fetch,
|
|
76
129
|
});
|
|
77
130
|
}
|
|
131
|
+
export function createAiClient(configured, env, fetch, path = "prOptions.ai") {
|
|
132
|
+
const options = {
|
|
133
|
+
...configured,
|
|
134
|
+
baseUrl: resolveBaseUrl(configured, env, path),
|
|
135
|
+
};
|
|
136
|
+
const client = buildClient(options, env, fetch, path);
|
|
137
|
+
if (options.baseUrlEnv === undefined || options.baseUrl === undefined) {
|
|
138
|
+
return client;
|
|
139
|
+
}
|
|
140
|
+
return new HostRedactingClient(client, options.baseUrl, options.baseUrlEnv);
|
|
141
|
+
}
|
package/dist/config/types.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ export interface AiProviderConfig {
|
|
|
6
6
|
provider?: AiProvider;
|
|
7
7
|
model?: string;
|
|
8
8
|
baseUrl?: string;
|
|
9
|
+
/** Name of the env var that holds the base URL; exclusive with `baseUrl` */
|
|
10
|
+
baseUrlEnv?: string;
|
|
9
11
|
apiKeyEnv?: string;
|
|
10
12
|
/** Extra request headers: header name to the env var that holds its value */
|
|
11
13
|
headersEnv?: Record<string, string>;
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { isPlainObject } from "../../shared/type-guards.js";
|
|
2
2
|
import { ValidationError } from "../../shared/errors.js";
|
|
3
|
-
import { assertAiHeadersEnvAllowed, assertAiKeyEnvAllowed, } from "../../shared/credential-env.js";
|
|
3
|
+
import { assertAiBaseUrlEnvAllowed, assertAiHeadersEnvAllowed, assertAiKeyEnvAllowed, } from "../../shared/credential-env.js";
|
|
4
4
|
const VALID_PROVIDERS = ["anthropic", "openai"];
|
|
5
5
|
const STRING_FIELDS = ["model", "baseUrl", "apiKeyEnv", "prompt"];
|
|
6
6
|
const PROVIDER_KEYS = [
|
|
7
7
|
"provider",
|
|
8
8
|
"maxDiffChars",
|
|
9
9
|
"headersEnv",
|
|
10
|
+
"baseUrlEnv",
|
|
10
11
|
...STRING_FIELDS,
|
|
11
12
|
];
|
|
12
13
|
const PRIMARY_KEYS = new Set([...PROVIDER_KEYS, "fallback"]);
|
|
@@ -41,7 +42,10 @@ function validateProvider(ai, path, knownKeys) {
|
|
|
41
42
|
throw new ValidationError(`${path}.${field} must be a non-empty string`);
|
|
42
43
|
}
|
|
43
44
|
}
|
|
44
|
-
|
|
45
|
+
assertAiBaseUrlEnvAllowed(ai.baseUrlEnv, ai.baseUrl, path);
|
|
46
|
+
assertAiKeyEnvAllowed(provider ?? "anthropic", ai.apiKeyEnv, ai.baseUrl, path,
|
|
47
|
+
// A URL read from the env is unknown here, so it never counts as the provider's own API.
|
|
48
|
+
ai.baseUrlEnv !== undefined);
|
|
45
49
|
assertAiHeadersEnvAllowed(ai.headersEnv, path);
|
|
46
50
|
if (provider === "openai" && ai.model === undefined) {
|
|
47
51
|
throw new ValidationError(`${path}.model is required when provider is 'openai'`);
|
|
@@ -7,9 +7,15 @@ export declare function isDefaultAiBaseUrl(provider: string, baseUrl: string | u
|
|
|
7
7
|
* A credential name may only feed `prOptions.ai` when it is the provider's
|
|
8
8
|
* own key going to the provider's own API.
|
|
9
9
|
*/
|
|
10
|
-
export declare function assertAiKeyEnvAllowed(provider: string, apiKeyEnv: string | undefined, baseUrl: string | undefined, path?: string): void;
|
|
10
|
+
export declare function assertAiKeyEnvAllowed(provider: string, apiKeyEnv: string | undefined, baseUrl: string | undefined, path?: string, baseUrlFromEnv?: boolean): void;
|
|
11
11
|
/**
|
|
12
12
|
* `headersEnv` maps header names to env var names. A value is never accepted
|
|
13
13
|
* inline, and the env var may not be a credential xfg uses elsewhere.
|
|
14
14
|
*/
|
|
15
15
|
export declare function assertAiHeadersEnvAllowed(headersEnv: unknown, path?: string): void;
|
|
16
|
+
/**
|
|
17
|
+
* `baseUrlEnv` names the env var that holds the base URL. A URL is never
|
|
18
|
+
* accepted inline, the two forms are exclusive, and the env var may not be a
|
|
19
|
+
* credential xfg uses elsewhere.
|
|
20
|
+
*/
|
|
21
|
+
export declare function assertAiBaseUrlEnvAllowed(baseUrlEnv: unknown, baseUrl: unknown, path?: string): void;
|
|
@@ -40,11 +40,11 @@ export function isDefaultAiBaseUrl(provider, baseUrl) {
|
|
|
40
40
|
* A credential name may only feed `prOptions.ai` when it is the provider's
|
|
41
41
|
* own key going to the provider's own API.
|
|
42
42
|
*/
|
|
43
|
-
export function assertAiKeyEnvAllowed(provider, apiKeyEnv, baseUrl, path = "prOptions.ai") {
|
|
43
|
+
export function assertAiKeyEnvAllowed(provider, apiKeyEnv, baseUrl, path = "prOptions.ai", baseUrlFromEnv = false) {
|
|
44
44
|
if (apiKeyEnv === undefined || !isCredentialEnvName(apiKeyEnv))
|
|
45
45
|
return;
|
|
46
46
|
const ownKey = apiKeyEnv.toUpperCase() === defaultAiKeyEnv(provider);
|
|
47
|
-
if (ownKey && isDefaultAiBaseUrl(provider, baseUrl))
|
|
47
|
+
if (ownKey && !baseUrlFromEnv && isDefaultAiBaseUrl(provider, baseUrl))
|
|
48
48
|
return;
|
|
49
49
|
throw new ValidationError(`${path}.apiKeyEnv '${apiKeyEnv}' is a credential xfg uses elsewhere; ` +
|
|
50
50
|
`export the key under a dedicated name for this provider and baseUrl.`);
|
|
@@ -91,3 +91,22 @@ export function assertAiHeadersEnvAllowed(headersEnv, path = "prOptions.ai") {
|
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* `baseUrlEnv` names the env var that holds the base URL. A URL is never
|
|
96
|
+
* accepted inline, the two forms are exclusive, and the env var may not be a
|
|
97
|
+
* credential xfg uses elsewhere.
|
|
98
|
+
*/
|
|
99
|
+
export function assertAiBaseUrlEnvAllowed(baseUrlEnv, baseUrl, path = "prOptions.ai") {
|
|
100
|
+
if (baseUrlEnv === undefined)
|
|
101
|
+
return;
|
|
102
|
+
if (baseUrl !== undefined) {
|
|
103
|
+
throw new ValidationError(`${path}: baseUrl and baseUrlEnv are mutually exclusive`);
|
|
104
|
+
}
|
|
105
|
+
if (typeof baseUrlEnv !== "string" || !ENV_NAME.test(baseUrlEnv)) {
|
|
106
|
+
throw new ValidationError(`${path}.baseUrlEnv must be an env var name, not a value`);
|
|
107
|
+
}
|
|
108
|
+
if (isCredentialEnvName(baseUrlEnv)) {
|
|
109
|
+
throw new ValidationError(`${path}.baseUrlEnv '${baseUrlEnv}' is a credential xfg uses elsewhere; ` +
|
|
110
|
+
`export the URL under a dedicated name.`);
|
|
111
|
+
}
|
|
112
|
+
}
|
package/package.json
CHANGED