@hilbras/sdk 0.9.3 → 0.9.4
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 +1 -1
- package/dist/adapters/anthropic.d.ts.map +1 -1
- package/dist/adapters/anthropic.js.map +1 -1
- package/dist/adapters/google-genai.d.ts.map +1 -1
- package/dist/adapters/openai.d.ts.map +1 -1
- package/dist/adapters/openai.js.map +1 -1
- package/dist/adapters/text-tool-call-parser.js +2 -2
- package/dist/adapters/text-tool-call-parser.js.map +1 -1
- package/dist/client/client.d.ts +28 -0
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.js +78 -53
- package/dist/client/client.js.map +1 -1
- package/dist/client/pipeline.d.ts +134 -0
- package/dist/client/pipeline.d.ts.map +1 -0
- package/dist/client/pipeline.js +219 -0
- package/dist/client/pipeline.js.map +1 -0
- package/dist/config/config.d.ts +7 -0
- package/dist/config/config.d.ts.map +1 -1
- package/dist/config/config.js +53 -4
- package/dist/config/config.js.map +1 -1
- package/dist/config/provider-config.d.ts +44 -0
- package/dist/config/provider-config.d.ts.map +1 -0
- package/dist/config/provider-config.js +15 -0
- package/dist/config/provider-config.js.map +1 -0
- package/dist/config/schema.d.ts +11 -7
- package/dist/config/schema.d.ts.map +1 -1
- package/dist/config/schema.js +5 -0
- package/dist/config/schema.js.map +1 -1
- package/dist/middleware/middleware.d.ts.map +1 -1
- package/dist/output/structured.d.ts.map +1 -1
- package/dist/output/structured.js +0 -19
- package/dist/output/structured.js.map +1 -1
- package/dist/reliability/degradation.d.ts +1 -1
- package/dist/reliability/degradation.d.ts.map +1 -1
- package/dist/tokens/prompt-cache.js +1 -1
- package/dist/tokens/prompt-cache.js.map +1 -1
- package/dist/transport/fetch.d.ts +18 -1
- package/dist/transport/fetch.d.ts.map +1 -1
- package/dist/transport/fetch.js +36 -11
- package/dist/transport/fetch.js.map +1 -1
- package/dist/types/execution.d.ts.map +1 -1
- package/dist/types/providers.d.ts +6 -17
- package/dist/types/providers.d.ts.map +1 -1
- package/dist/types/providers.js +5 -0
- package/dist/types/providers.js.map +1 -1
- package/package.json +3 -2
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hilbras/sdk — RequestPipeline (v0.10.0 PR-4)
|
|
3
|
+
*
|
|
4
|
+
* The shared reliability lifecycle for a *single attempt*. Used by
|
|
5
|
+
* `HilbrasClient.stream()` and `HilbrasClient.complete()`.
|
|
6
|
+
*
|
|
7
|
+
* Before PR-4, both methods had near-duplicate code for: provider
|
|
8
|
+
* resolution, circuit-breaker check, retry config, timeout signal,
|
|
9
|
+
* budget reservation, budget settle/release, and request.completed /
|
|
10
|
+
* request.failed event emission. The pipeline extracts those concerns
|
|
11
|
+
* for a single attempt. The retry loop and fallback loop stay in the
|
|
12
|
+
* client (where the per-stream consumption semantics live), but they
|
|
13
|
+
* now call `pipeline.runOnce(...)` for each attempt — so a single
|
|
14
|
+
* attempt is one place in the code, not two.
|
|
15
|
+
*
|
|
16
|
+
* Internal-only: not exported from `src/index.ts`.
|
|
17
|
+
*/
|
|
18
|
+
import type { AIProvider } from "../types/adapter.js";
|
|
19
|
+
import type { HookEvent } from "../types/observability.js";
|
|
20
|
+
import type { Message } from "../types/messages.js";
|
|
21
|
+
import type { StreamChunk } from "../types/streams.js";
|
|
22
|
+
import type { ResolvedPolicy } from "../types/policy.js";
|
|
23
|
+
import type { ProviderConfig } from "../types/providers.js";
|
|
24
|
+
import type { ExecutionPolicy } from "../types/policy.js";
|
|
25
|
+
import { type CircuitBreaker } from "../reliability/circuit-breaker.js";
|
|
26
|
+
import { createRetryConfig } from "../reliability/retry.js";
|
|
27
|
+
import type { BudgetTracker } from "../cost/tracker.js";
|
|
28
|
+
/** Per-attempt result for a streaming run. */
|
|
29
|
+
export interface StreamAttemptResult {
|
|
30
|
+
/** Total input tokens reported by the usage chunk, if any. */
|
|
31
|
+
inputTokens?: number;
|
|
32
|
+
/** Total output tokens reported by the usage chunk, if any. */
|
|
33
|
+
outputTokens?: number;
|
|
34
|
+
}
|
|
35
|
+
/** Streaming executor signature. */
|
|
36
|
+
export type StreamExecutor = (args: {
|
|
37
|
+
signal: AbortSignal | undefined;
|
|
38
|
+
providerName: string;
|
|
39
|
+
modelId: string;
|
|
40
|
+
messages: Message[];
|
|
41
|
+
temperature: number | undefined;
|
|
42
|
+
maxTokens: number | undefined;
|
|
43
|
+
tools: import("../types/tools.js").Tool[] | undefined;
|
|
44
|
+
extra: Record<string, unknown> | undefined;
|
|
45
|
+
}) => Promise<{
|
|
46
|
+
chunks: AsyncGenerator<StreamChunk>;
|
|
47
|
+
result: StreamAttemptResult;
|
|
48
|
+
}>;
|
|
49
|
+
/** Non-streaming executor signature. */
|
|
50
|
+
export type CompleteExecutor<T> = (args: {
|
|
51
|
+
signal: AbortSignal | undefined;
|
|
52
|
+
providerName: string;
|
|
53
|
+
modelId: string;
|
|
54
|
+
messages: Message[];
|
|
55
|
+
temperature: number | undefined;
|
|
56
|
+
maxTokens: number | undefined;
|
|
57
|
+
tools: import("../types/tools.js").Tool[] | undefined;
|
|
58
|
+
extra: Record<string, unknown> | undefined;
|
|
59
|
+
}) => Promise<T>;
|
|
60
|
+
/** Result of a successful streaming run. */
|
|
61
|
+
export interface PipelineStreamSuccess {
|
|
62
|
+
ok: true;
|
|
63
|
+
attempts: number;
|
|
64
|
+
inputTokens?: number;
|
|
65
|
+
outputTokens?: number;
|
|
66
|
+
providerName: string;
|
|
67
|
+
modelId: string;
|
|
68
|
+
}
|
|
69
|
+
/** A streaming-executor error that the host should classify (retry/fallback). */
|
|
70
|
+
export interface PipelineStreamFailure {
|
|
71
|
+
ok: false;
|
|
72
|
+
err: unknown;
|
|
73
|
+
}
|
|
74
|
+
export type PipelineStreamResult = PipelineStreamSuccess | PipelineStreamFailure;
|
|
75
|
+
/** Same shape for non-streaming. */
|
|
76
|
+
export type PipelineCompleteResult<T> = {
|
|
77
|
+
ok: true;
|
|
78
|
+
attempts: number;
|
|
79
|
+
value: T;
|
|
80
|
+
providerName: string;
|
|
81
|
+
modelId: string;
|
|
82
|
+
} | {
|
|
83
|
+
ok: false;
|
|
84
|
+
err: unknown;
|
|
85
|
+
};
|
|
86
|
+
/** What the pipeline needs from the host to do its job. */
|
|
87
|
+
export interface PipelineContext {
|
|
88
|
+
requestId: string;
|
|
89
|
+
startTime: number;
|
|
90
|
+
providerConfig: ProviderConfig;
|
|
91
|
+
modelId: string;
|
|
92
|
+
resolved: ResolvedPolicy;
|
|
93
|
+
adapter: AIProvider;
|
|
94
|
+
circuitBreaker: CircuitBreaker | undefined;
|
|
95
|
+
retryConfig: ReturnType<typeof createRetryConfig>;
|
|
96
|
+
signal: AbortSignal | undefined;
|
|
97
|
+
messages: Message[];
|
|
98
|
+
emit: (event: HookEvent) => void;
|
|
99
|
+
budgetTracker: BudgetTracker;
|
|
100
|
+
/** When true (default), the streaming variant settles the budget on the
|
|
101
|
+
* first usage chunk. When false, the budget is settled at the end-of-stream
|
|
102
|
+
* using the estimated cost. */
|
|
103
|
+
settleOnUsage: boolean;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Build a `PipelineContext` for a given client request. The host
|
|
107
|
+
* (HilbrasClient.stream / complete) supplies the resolved fields.
|
|
108
|
+
*/
|
|
109
|
+
export interface BuildContextArgs {
|
|
110
|
+
requestId: string;
|
|
111
|
+
startTime: number;
|
|
112
|
+
providerConfig: ProviderConfig;
|
|
113
|
+
modelId: string;
|
|
114
|
+
policy: ExecutionPolicy | undefined;
|
|
115
|
+
defaultPolicy: ExecutionPolicy | undefined;
|
|
116
|
+
adapter: AIProvider;
|
|
117
|
+
messages: Message[];
|
|
118
|
+
signal: AbortSignal | undefined;
|
|
119
|
+
emit: (event: HookEvent) => void;
|
|
120
|
+
budgetTracker: BudgetTracker;
|
|
121
|
+
settleOnUsage: boolean;
|
|
122
|
+
}
|
|
123
|
+
export declare function buildPipelineContext(args: BuildContextArgs): PipelineContext;
|
|
124
|
+
/**
|
|
125
|
+
* Run a single streaming attempt through the full pipeline (reservation,
|
|
126
|
+
* executor, budget settle, hook emission). Returns a result object the
|
|
127
|
+
* host can use to decide retry/fallback.
|
|
128
|
+
*/
|
|
129
|
+
export declare function runStreamAttempt(ctx: PipelineContext, executor: StreamExecutor, attempt: number): Promise<PipelineStreamResult>;
|
|
130
|
+
/** Run a single non-streaming attempt through the full pipeline. */
|
|
131
|
+
export declare function runCompleteAttempt<T>(ctx: PipelineContext, executor: CompleteExecutor<T>, attempt: number): Promise<PipelineCompleteResult<T>>;
|
|
132
|
+
/** Record a final-failure event for a request that exhausted retries and fallbacks. */
|
|
133
|
+
export declare function recordFailure(ctx: PipelineContext, err: unknown, attempt: number): void;
|
|
134
|
+
//# sourceMappingURL=pipeline.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pipeline.d.ts","sourceRoot":"","sources":["../../src/client/pipeline.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAGH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAE1D,OAAO,EAA6B,KAAK,cAAc,EAAE,MAAM,mCAAmC,CAAC;AACnG,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE5D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAGxD,8CAA8C;AAC9C,MAAM,WAAW,mBAAmB;IA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C;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,cAAc,EAAE,cAAc,CAAC;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,eAAe,GAAG,SAAS,CAAC;IACpC,aAAa,EAAE,eAAe,GAAG,SAAS,CAAC;IAC3C,OAAO,EAAE,UAAU,CAAC;IACpB,QAAQ,EAAE,OAAO,EAAE,CAAC;IACpB,MAAM,EAAE,WAAW,GAAG,SAAS,CAAC;IAChC,IAAI,EAAE,CAAC,KAAK,EAAE,SAAS,KAAK,IAAI,CAAC;IACjC,aAAa,EAAE,aAAa,CAAC;IAC7B,aAAa,EAAE,OAAO,CAAC;CACxB;AAED,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,gBAAgB,GAAG,eAAe,CA6C5E;AAED;;;;GAIG;AACH,wBAAsB,gBAAgB,CACpC,GAAG,EAAE,eAAe,EACpB,QAAQ,EAAE,cAAc,EACxB,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,oBAAoB,CAAC,CAmF/B;AAED,oEAAoE;AACpE,wBAAsB,kBAAkB,CAAC,CAAC,EACxC,GAAG,EAAE,eAAe,EACpB,QAAQ,EAAE,gBAAgB,CAAC,CAAC,CAAC,EAC7B,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,sBAAsB,CAAC,CAAC,CAAC,CAAC,CAyDpC;AAED,uFAAuF;AACvF,wBAAgB,aAAa,CAC3B,GAAG,EAAE,eAAe,EACpB,GAAG,EAAE,OAAO,EACZ,OAAO,EAAE,MAAM,GACd,IAAI,CAaN"}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hilbras/sdk — RequestPipeline (v0.10.0 PR-4)
|
|
3
|
+
*
|
|
4
|
+
* The shared reliability lifecycle for a *single attempt*. Used by
|
|
5
|
+
* `HilbrasClient.stream()` and `HilbrasClient.complete()`.
|
|
6
|
+
*
|
|
7
|
+
* Before PR-4, both methods had near-duplicate code for: provider
|
|
8
|
+
* resolution, circuit-breaker check, retry config, timeout signal,
|
|
9
|
+
* budget reservation, budget settle/release, and request.completed /
|
|
10
|
+
* request.failed event emission. The pipeline extracts those concerns
|
|
11
|
+
* for a single attempt. The retry loop and fallback loop stay in the
|
|
12
|
+
* client (where the per-stream consumption semantics live), but they
|
|
13
|
+
* now call `pipeline.runOnce(...)` for each attempt — so a single
|
|
14
|
+
* attempt is one place in the code, not two.
|
|
15
|
+
*
|
|
16
|
+
* Internal-only: not exported from `src/index.ts`.
|
|
17
|
+
*/
|
|
18
|
+
import { ConfigurationError, CircuitBreakerOpenError } from "../errors/index.js";
|
|
19
|
+
import { resolvePolicy } from "../reliability/presets.js";
|
|
20
|
+
import { getCircuitBreakerRegistry } from "../reliability/circuit-breaker.js";
|
|
21
|
+
import { createRetryConfig } from "../reliability/retry.js";
|
|
22
|
+
import { createTimeoutSignal } from "../reliability/timeout.js";
|
|
23
|
+
import { estimateTokens } from "../tokens/counter.js";
|
|
24
|
+
export function buildPipelineContext(args) {
|
|
25
|
+
const resolved = resolvePolicy(args.policy ?? args.defaultPolicy);
|
|
26
|
+
let circuitBreaker;
|
|
27
|
+
if (resolved.circuitBreaker.enabled) {
|
|
28
|
+
circuitBreaker = getCircuitBreakerRegistry().getOrCreate(args.providerConfig.name, {
|
|
29
|
+
failureThreshold: resolved.circuitBreaker.failureThreshold,
|
|
30
|
+
successThreshold: resolved.circuitBreaker.successThreshold,
|
|
31
|
+
timeoutMs: resolved.circuitBreaker.timeoutMs,
|
|
32
|
+
halfOpenMaxCalls: resolved.circuitBreaker.halfOpenMaxCalls,
|
|
33
|
+
});
|
|
34
|
+
if (!circuitBreaker.isAvailable()) {
|
|
35
|
+
args.emit({
|
|
36
|
+
type: "circuit_breaker.open",
|
|
37
|
+
requestId: args.requestId,
|
|
38
|
+
timestamp: performance.now(),
|
|
39
|
+
provider: args.providerConfig.name,
|
|
40
|
+
});
|
|
41
|
+
throw new CircuitBreakerOpenError(args.providerConfig.name);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const retryConfig = createRetryConfig({
|
|
45
|
+
maxRetries: resolved.retry.maxRetries,
|
|
46
|
+
retryableStatuses: resolved.retry.retryableStatuses,
|
|
47
|
+
retryableNetworkErrors: resolved.retry.retryableNetworkErrors,
|
|
48
|
+
});
|
|
49
|
+
const timeoutMs = resolved.timeout.requestTimeoutMs || args.providerConfig.timeout;
|
|
50
|
+
const signal = timeoutMs
|
|
51
|
+
? createTimeoutSignal({ requestTimeoutMs: timeoutMs }, args.signal)
|
|
52
|
+
: args.signal;
|
|
53
|
+
return {
|
|
54
|
+
requestId: args.requestId,
|
|
55
|
+
startTime: args.startTime,
|
|
56
|
+
providerConfig: args.providerConfig,
|
|
57
|
+
modelId: args.modelId,
|
|
58
|
+
resolved,
|
|
59
|
+
adapter: args.adapter,
|
|
60
|
+
circuitBreaker,
|
|
61
|
+
retryConfig,
|
|
62
|
+
signal,
|
|
63
|
+
messages: args.messages,
|
|
64
|
+
emit: args.emit,
|
|
65
|
+
budgetTracker: args.budgetTracker,
|
|
66
|
+
settleOnUsage: args.settleOnUsage,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Run a single streaming attempt through the full pipeline (reservation,
|
|
71
|
+
* executor, budget settle, hook emission). Returns a result object the
|
|
72
|
+
* host can use to decide retry/fallback.
|
|
73
|
+
*/
|
|
74
|
+
export async function runStreamAttempt(ctx, executor, attempt) {
|
|
75
|
+
const { requestId, startTime, providerConfig, modelId, signal, messages, emit, budgetTracker, settleOnUsage } = ctx;
|
|
76
|
+
const estimatedCost = budgetTracker.estimate(modelId, providerConfig.name, estimateTokens(messages.map((m) => m.content ?? "").join("")), 0);
|
|
77
|
+
const initialReservation = budgetTracker.reserve(requestId, estimatedCost);
|
|
78
|
+
if (!initialReservation) {
|
|
79
|
+
throw new ConfigurationError(`Budget reservation rejected — estimated cost $${estimatedCost.toFixed(4)} would exceed budget`);
|
|
80
|
+
}
|
|
81
|
+
let reservationActive = true;
|
|
82
|
+
let usageSettled = false;
|
|
83
|
+
let totalIn;
|
|
84
|
+
let totalOut;
|
|
85
|
+
try {
|
|
86
|
+
const { chunks, result } = await executor({
|
|
87
|
+
signal,
|
|
88
|
+
providerName: providerConfig.name,
|
|
89
|
+
modelId,
|
|
90
|
+
messages,
|
|
91
|
+
temperature: undefined,
|
|
92
|
+
maxTokens: undefined,
|
|
93
|
+
tools: undefined,
|
|
94
|
+
extra: undefined,
|
|
95
|
+
});
|
|
96
|
+
for await (const chunk of chunks) {
|
|
97
|
+
if (chunk.type === "usage") {
|
|
98
|
+
const inT = chunk.inputTokens;
|
|
99
|
+
const outT = chunk.outputTokens;
|
|
100
|
+
totalIn = inT;
|
|
101
|
+
totalOut = outT;
|
|
102
|
+
if (reservationActive && !usageSettled && settleOnUsage) {
|
|
103
|
+
const actualCost = budgetTracker.estimate(modelId, providerConfig.name, inT ?? 0, outT ?? 0);
|
|
104
|
+
budgetTracker.settle(requestId, actualCost, { provider: providerConfig.name, model: modelId, phase: "execute" });
|
|
105
|
+
reservationActive = false;
|
|
106
|
+
usageSettled = true;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (result.inputTokens !== undefined)
|
|
111
|
+
totalIn = result.inputTokens;
|
|
112
|
+
if (result.outputTokens !== undefined)
|
|
113
|
+
totalOut = result.outputTokens;
|
|
114
|
+
ctx.circuitBreaker?.recordSuccess();
|
|
115
|
+
if (reservationActive) {
|
|
116
|
+
// No usage chunk arrived; settle at the estimate.
|
|
117
|
+
budgetTracker.settle(requestId, estimatedCost, { provider: providerConfig.name, model: modelId, phase: "execute" });
|
|
118
|
+
reservationActive = false;
|
|
119
|
+
}
|
|
120
|
+
emit({
|
|
121
|
+
type: "request.completed",
|
|
122
|
+
requestId,
|
|
123
|
+
timestamp: performance.now(),
|
|
124
|
+
provider: providerConfig.name,
|
|
125
|
+
model: modelId,
|
|
126
|
+
durationMs: performance.now() - startTime,
|
|
127
|
+
attempts: attempt + 1,
|
|
128
|
+
inputTokens: totalIn,
|
|
129
|
+
outputTokens: totalOut,
|
|
130
|
+
structuredOutput: false,
|
|
131
|
+
});
|
|
132
|
+
return { ok: true, attempts: attempt + 1, inputTokens: totalIn, outputTokens: totalOut, providerName: providerConfig.name, modelId };
|
|
133
|
+
}
|
|
134
|
+
catch (err) {
|
|
135
|
+
if (reservationActive) {
|
|
136
|
+
budgetTracker.release(requestId);
|
|
137
|
+
reservationActive = false;
|
|
138
|
+
}
|
|
139
|
+
return { ok: false, err };
|
|
140
|
+
}
|
|
141
|
+
finally {
|
|
142
|
+
// Defensive: in case neither the success nor the catch branch ran
|
|
143
|
+
// (e.g. an exception thrown between them), make sure the reservation
|
|
144
|
+
// is released. Idempotent.
|
|
145
|
+
if (reservationActive) {
|
|
146
|
+
budgetTracker.release(requestId);
|
|
147
|
+
reservationActive = false;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/** Run a single non-streaming attempt through the full pipeline. */
|
|
152
|
+
export async function runCompleteAttempt(ctx, executor, attempt) {
|
|
153
|
+
const { requestId, startTime, providerConfig, modelId, signal, messages, emit, budgetTracker } = ctx;
|
|
154
|
+
const estimatedCost = budgetTracker.estimate(modelId, providerConfig.name, estimateTokens(messages.map((m) => m.content ?? "").join("")), 0);
|
|
155
|
+
const initialReservation = budgetTracker.reserve(requestId, estimatedCost);
|
|
156
|
+
if (!initialReservation) {
|
|
157
|
+
throw new ConfigurationError(`Budget reservation rejected — estimated cost $${estimatedCost.toFixed(4)} would exceed budget`);
|
|
158
|
+
}
|
|
159
|
+
let reservationActive = true;
|
|
160
|
+
try {
|
|
161
|
+
const value = await executor({
|
|
162
|
+
signal,
|
|
163
|
+
providerName: providerConfig.name,
|
|
164
|
+
modelId,
|
|
165
|
+
messages,
|
|
166
|
+
temperature: undefined,
|
|
167
|
+
maxTokens: undefined,
|
|
168
|
+
tools: undefined,
|
|
169
|
+
extra: undefined,
|
|
170
|
+
});
|
|
171
|
+
ctx.circuitBreaker?.recordSuccess();
|
|
172
|
+
if (reservationActive) {
|
|
173
|
+
budgetTracker.settle(requestId, estimatedCost, { provider: providerConfig.name, model: modelId, phase: "execute" });
|
|
174
|
+
reservationActive = false;
|
|
175
|
+
}
|
|
176
|
+
emit({
|
|
177
|
+
type: "request.completed",
|
|
178
|
+
requestId,
|
|
179
|
+
timestamp: performance.now(),
|
|
180
|
+
provider: providerConfig.name,
|
|
181
|
+
model: modelId,
|
|
182
|
+
durationMs: performance.now() - startTime,
|
|
183
|
+
attempts: attempt + 1,
|
|
184
|
+
inputTokens: undefined,
|
|
185
|
+
outputTokens: undefined,
|
|
186
|
+
structuredOutput: false,
|
|
187
|
+
});
|
|
188
|
+
return { ok: true, attempts: attempt + 1, value, providerName: providerConfig.name, modelId };
|
|
189
|
+
}
|
|
190
|
+
catch (err) {
|
|
191
|
+
if (reservationActive) {
|
|
192
|
+
budgetTracker.release(requestId);
|
|
193
|
+
reservationActive = false;
|
|
194
|
+
}
|
|
195
|
+
return { ok: false, err };
|
|
196
|
+
}
|
|
197
|
+
finally {
|
|
198
|
+
if (reservationActive) {
|
|
199
|
+
budgetTracker.release(requestId);
|
|
200
|
+
reservationActive = false;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/** Record a final-failure event for a request that exhausted retries and fallbacks. */
|
|
205
|
+
export function recordFailure(ctx, err, attempt) {
|
|
206
|
+
const { requestId, startTime, providerConfig, modelId, emit } = ctx;
|
|
207
|
+
ctx.circuitBreaker?.recordFailure(err instanceof Error ? err : undefined);
|
|
208
|
+
emit({
|
|
209
|
+
type: "request.failed",
|
|
210
|
+
requestId,
|
|
211
|
+
timestamp: performance.now(),
|
|
212
|
+
provider: providerConfig.name,
|
|
213
|
+
model: modelId,
|
|
214
|
+
durationMs: performance.now() - startTime,
|
|
215
|
+
attempts: attempt + 1,
|
|
216
|
+
error: err instanceof Error ? err.message : String(err),
|
|
217
|
+
});
|
|
218
|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,iBAAiB,EAAE,CAAC;YACtB,aAAa,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;YACjC,iBAAiB,GAAG,KAAK,CAAC;QAC5B,CAAC;QACD,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC;IAC5B,CAAC;YAAS,CAAC;QACT,IAAI,iBAAiB,EAAE,CAAC;YACtB,aAAa,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;YACjC,iBAAiB,GAAG,KAAK,CAAC;QAC5B,CAAC;IACH,CAAC;AACH,CAAC;AAED,uFAAuF;AACvF,MAAM,UAAU,aAAa,CAC3B,GAAoB,EACpB,GAAY,EACZ,OAAe;IAEf,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,cAAc,EAAE,OAAO,EAAE,IAAI,EAAE,GAAG,GAAG,CAAC;IACpE,GAAG,CAAC,cAAc,EAAE,aAAa,CAAC,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;IAC1E,IAAI,CAAC;QACH,IAAI,EAAE,gBAAgB;QACtB,SAAS;QACT,SAAS,EAAE,WAAW,CAAC,GAAG,EAAE;QAC5B,QAAQ,EAAE,cAAc,CAAC,IAAI;QAC7B,KAAK,EAAE,OAAO;QACd,UAAU,EAAE,WAAW,CAAC,GAAG,EAAE,GAAG,SAAS;QACzC,QAAQ,EAAE,OAAO,GAAG,CAAC;QACrB,KAAK,EAAE,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC;KACxD,CAAC,CAAC;AACL,CAAC"}
|
package/dist/config/config.d.ts
CHANGED
|
@@ -3,6 +3,13 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Loads configuration from multiple sources with precedence:
|
|
5
5
|
* Runtime overrides > Environment variables > File config > Defaults
|
|
6
|
+
*
|
|
7
|
+
* The env loader maps the legacy `HILBRAS_PROVIDER_*` variables to the
|
|
8
|
+
* canonical `ProviderConfig` shape (introduced in v0.10.0) and runs every
|
|
9
|
+
* `baseUrl` through `validateBaseUrl` so an env-var-based SSRF bypass is
|
|
10
|
+
* not possible. The API key is captured for redacted error messages; it
|
|
11
|
+
* is *not* stored as a plain string on the loaded config — the loader
|
|
12
|
+
* forces it into the `authentication` field which is the public contract.
|
|
6
13
|
*/
|
|
7
14
|
import type { SDKConfig } from "./schema.js";
|
|
8
15
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/config/config.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/config/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAoG7C;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,OAAO,CAAC,EAAE;IACnC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,OAAO,CAAC,SAAS,CAAC,CAAC;CAChC,GAAG,SAAS,CAmBZ;AAED,oEAAoE;AACpE,wBAAgB,YAAY,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC,SAAS,CAAC,GAAG,SAAS,CAEtE;AAED,0EAA0E;AAC1E,wBAAgB,cAAc,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,GAAG,IAAI,CAsB/D"}
|
package/dist/config/config.js
CHANGED
|
@@ -3,11 +3,40 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Loads configuration from multiple sources with precedence:
|
|
5
5
|
* Runtime overrides > Environment variables > File config > Defaults
|
|
6
|
+
*
|
|
7
|
+
* The env loader maps the legacy `HILBRAS_PROVIDER_*` variables to the
|
|
8
|
+
* canonical `ProviderConfig` shape (introduced in v0.10.0) and runs every
|
|
9
|
+
* `baseUrl` through `validateBaseUrl` so an env-var-based SSRF bypass is
|
|
10
|
+
* not possible. The API key is captured for redacted error messages; it
|
|
11
|
+
* is *not* stored as a plain string on the loaded config — the loader
|
|
12
|
+
* forces it into the `authentication` field which is the public contract.
|
|
6
13
|
*/
|
|
7
14
|
import { DEFAULT_CONFIG } from "./schema.js";
|
|
15
|
+
import { validateBaseUrl } from "../security/url-guard.js";
|
|
16
|
+
import { redact } from "../logging/logger.js";
|
|
8
17
|
import { readFileSync } from "node:fs";
|
|
9
18
|
/** Environment variable prefix for SDK config */
|
|
10
19
|
const ENV_PREFIX = "HILBRAS_";
|
|
20
|
+
/**
|
|
21
|
+
* Heuristic adapter detection from a baseUrl. Used when the env loader
|
|
22
|
+
* builds a provider from `HILBRAS_PROVIDER_URL`/`HILBRAS_PROVIDER_FORMAT`
|
|
23
|
+
* and the user has not specified an adapter explicitly. Matches the
|
|
24
|
+
* simplest substrings; if no match, defaults to `openai`.
|
|
25
|
+
*/
|
|
26
|
+
function inferAdapter(baseUrl) {
|
|
27
|
+
const lower = baseUrl.toLowerCase();
|
|
28
|
+
if (lower.includes("anthropic"))
|
|
29
|
+
return "anthropic";
|
|
30
|
+
if (lower.includes("googleapis") || lower.includes("generativelanguage"))
|
|
31
|
+
return "google-genai";
|
|
32
|
+
if (lower.includes("azure"))
|
|
33
|
+
return "azure";
|
|
34
|
+
if (lower.includes("groq"))
|
|
35
|
+
return "groq";
|
|
36
|
+
if (lower.includes("localhost") || lower.includes("127.0.0.1") || lower.includes("[::1]"))
|
|
37
|
+
return "ollama";
|
|
38
|
+
return "openai";
|
|
39
|
+
}
|
|
11
40
|
/** Parse environment variables into SDKConfig fields */
|
|
12
41
|
function loadFromEnv() {
|
|
13
42
|
const env = process.env;
|
|
@@ -30,17 +59,31 @@ function loadFromEnv() {
|
|
|
30
59
|
config.maxRetries = parseInt(env[`${ENV_PREFIX}MAX_RETRIES`] ?? "", 10);
|
|
31
60
|
if (env[`${ENV_PREFIX}PROMPT_CACHING`])
|
|
32
61
|
config.promptCaching = env[`${ENV_PREFIX}PROMPT_CACHING`] === "true";
|
|
33
|
-
// Provider config from env
|
|
62
|
+
// Provider config from env. v0.10.0: the loader now produces the canonical
|
|
63
|
+
// `ProviderConfig` shape. We also redact the API key for any log that
|
|
64
|
+
// would surface it.
|
|
34
65
|
const providerUrl = env[`${ENV_PREFIX}PROVIDER_URL`];
|
|
35
66
|
const providerKey = env[`${ENV_PREFIX}PROVIDER_KEY`];
|
|
36
67
|
const providerName = env[`${ENV_PREFIX}PROVIDER_NAME`];
|
|
37
|
-
const providerFormat = env[`${ENV_PREFIX}PROVIDER_FORMAT`];
|
|
38
68
|
if (providerUrl && providerKey) {
|
|
69
|
+
// Validate the URL through the v0.9.3 SSRF guard. We deliberately
|
|
70
|
+
// allow insecure here because the env loader is the documented
|
|
71
|
+
// way to point at a local Ollama (`http://localhost:11434`).
|
|
72
|
+
// Loopback/private-range access is gated by the global
|
|
73
|
+
// `allowInsecureUrls` / `allowPrivateNetwork` flags on HilbrasClient,
|
|
74
|
+
// not on the loader.
|
|
75
|
+
const guard = validateBaseUrl(providerUrl, { allowInsecure: true });
|
|
76
|
+
if (!guard.ok) {
|
|
77
|
+
throw new Error(`HILBRAS_PROVIDER_URL rejected by SSRF guard: ${guard.reason}`);
|
|
78
|
+
}
|
|
79
|
+
const inferredAdapter = inferAdapter(providerUrl);
|
|
39
80
|
config.providers = [{
|
|
40
81
|
name: providerName || "default",
|
|
41
82
|
baseUrl: providerUrl,
|
|
42
|
-
apiKey: providerKey,
|
|
43
|
-
|
|
83
|
+
authentication: { type: "bearer", apiKey: redact(providerKey) },
|
|
84
|
+
models: [],
|
|
85
|
+
adapter: inferredAdapter,
|
|
86
|
+
allowInsecure: providerUrl.startsWith("http://"),
|
|
44
87
|
}];
|
|
45
88
|
config.defaultProvider = providerName || "default";
|
|
46
89
|
}
|
|
@@ -116,6 +159,12 @@ export function validateConfig(config) {
|
|
|
116
159
|
if (!["none", "error", "info", "debug"].includes(config.logLevel)) {
|
|
117
160
|
return `Invalid logLevel: ${config.logLevel}`;
|
|
118
161
|
}
|
|
162
|
+
// v0.10.0: also validate every provider's baseUrl.
|
|
163
|
+
for (const p of config.providers) {
|
|
164
|
+
const guard = validateBaseUrl(p.baseUrl, { allowInsecure: !!p.allowInsecure });
|
|
165
|
+
if (!guard.ok)
|
|
166
|
+
return `Provider ${p.name}: ${guard.reason}`;
|
|
167
|
+
}
|
|
119
168
|
return null;
|
|
120
169
|
}
|
|
121
170
|
//# sourceMappingURL=config.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config/config.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE7C,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEvC,iDAAiD;AACjD,MAAM,UAAU,GAAG,UAAU,CAAC;AAE9B;;;;;GAKG;AACH,SAAS,YAAY,CAAC,OAAe;IACnC,MAAM,KAAK,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IACpC,IAAI,KAAK,CAAC,QAAQ,CAAC,WAAW,CAAC;QAAE,OAAO,WAAW,CAAC;IACpD,IAAI,KAAK,CAAC,QAAQ,CAAC,YAAY,CAAC,IAAI,KAAK,CAAC,QAAQ,CAAC,oBAAoB,CAAC;QAAE,OAAO,cAAc,CAAC;IAChG,IAAI,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC;QAAE,OAAO,OAAO,CAAC;IAC5C,IAAI,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC;QAAE,OAAO,MAAM,CAAC;IAC1C,IAAI,KAAK,CAAC,QAAQ,CAAC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,CAAC,UAAU,EAAE,CAAC;IACrE,CAAC;IACD,IAAI,CAAC,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC;QAClE,OAAO,qBAAqB,MAAM,CAAC,QAAQ,EAAE,CAAC;IAChD,CAAC;IACD,mDAAmD;IACnD,KAAK,MAAM,CAAC,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;QACjC,MAAM,KAAK,GAAG,eAAe,CAAC,CAAC,CAAC,OAAO,EAAE,EAAE,aAAa,EAAE,CAAC,CAAC,CAAC,CAAC,aAAa,EAAE,CAAC,CAAC;QAC/E,IAAI,CAAC,KAAK,CAAC,EAAE;YAAE,OAAO,YAAY,CAAC,CAAC,IAAI,KAAK,KAAK,CAAC,MAAM,EAAE,CAAC;IAC9D,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hilbras/sdk — Canonical ProviderConfig
|
|
3
|
+
*
|
|
4
|
+
* This is the single source of truth for the per-provider configuration shape.
|
|
5
|
+
* `src/types/providers.ts` re-exports from here so existing imports of
|
|
6
|
+
* `ProviderConfig` from `@hilbras/sdk` and `@hilbras/sdk/types` keep working.
|
|
7
|
+
*
|
|
8
|
+
* The legacy `format: "openai" | "anthropic" | "google-genai"` field and the
|
|
9
|
+
* flat `apiKey: string` field (which used to live in `src/config/schema.ts`)
|
|
10
|
+
* are gone. Callers should use the structured `authentication` field
|
|
11
|
+
* (`{ type: "bearer", apiKey }` or `{ type: "header", name, value }`) and the
|
|
12
|
+
* `adapter` field.
|
|
13
|
+
*/
|
|
14
|
+
import type { Model } from "../types/models.js";
|
|
15
|
+
import type { AdapterName, Authentication } from "../types/providers.js";
|
|
16
|
+
/**
|
|
17
|
+
* Per-provider configuration.
|
|
18
|
+
*
|
|
19
|
+
* The `baseUrl` is validated through `validateBaseUrl` before the provider is
|
|
20
|
+
* registered. Use `allowInsecure: true` to allow `http://` for local Ollama
|
|
21
|
+
* and similar.
|
|
22
|
+
*/
|
|
23
|
+
export interface ProviderConfig {
|
|
24
|
+
/** Display name used as the key in `client.addProvider` / `client.removeProvider`. */
|
|
25
|
+
name: string;
|
|
26
|
+
/** Provider endpoint. Validated against `validateBaseUrl`. */
|
|
27
|
+
baseUrl: string;
|
|
28
|
+
/** Authentication: bearer, header, or none. */
|
|
29
|
+
authentication: Authentication;
|
|
30
|
+
/** Models exposed by this provider. */
|
|
31
|
+
models: Model[];
|
|
32
|
+
/** Which built-in or custom adapter to use. */
|
|
33
|
+
adapter: AdapterName;
|
|
34
|
+
/** Request timeout in ms; falls back to policy timeout. */
|
|
35
|
+
timeout?: number;
|
|
36
|
+
/** Custom HTTP headers to add to every request. */
|
|
37
|
+
extraHeaders?: Record<string, string>;
|
|
38
|
+
/**
|
|
39
|
+
* Allow non-https baseUrl. Required for local Ollama
|
|
40
|
+
* (`http://localhost:11434`) and similar. Defaults to false.
|
|
41
|
+
*/
|
|
42
|
+
allowInsecure?: boolean;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=provider-config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-config.d.ts","sourceRoot":"","sources":["../../src/config/provider-config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAEzE;;;;;;GAMG;AACH,MAAM,WAAW,cAAc;IAC7B,sFAAsF;IACtF,IAAI,EAAE,MAAM,CAAC;IACb,8DAA8D;IAC9D,OAAO,EAAE,MAAM,CAAC;IAChB,+CAA+C;IAC/C,cAAc,EAAE,cAAc,CAAC;IAC/B,uCAAuC;IACvC,MAAM,EAAE,KAAK,EAAE,CAAC;IAChB,+CAA+C;IAC/C,OAAO,EAAE,WAAW,CAAC;IACrB,2DAA2D;IAC3D,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,mDAAmD;IACnD,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACtC;;;OAGG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hilbras/sdk — Canonical ProviderConfig
|
|
3
|
+
*
|
|
4
|
+
* This is the single source of truth for the per-provider configuration shape.
|
|
5
|
+
* `src/types/providers.ts` re-exports from here so existing imports of
|
|
6
|
+
* `ProviderConfig` from `@hilbras/sdk` and `@hilbras/sdk/types` keep working.
|
|
7
|
+
*
|
|
8
|
+
* The legacy `format: "openai" | "anthropic" | "google-genai"` field and the
|
|
9
|
+
* flat `apiKey: string` field (which used to live in `src/config/schema.ts`)
|
|
10
|
+
* are gone. Callers should use the structured `authentication` field
|
|
11
|
+
* (`{ type: "bearer", apiKey }` or `{ type: "header", name, value }`) and the
|
|
12
|
+
* `adapter` field.
|
|
13
|
+
*/
|
|
14
|
+
export {};
|
|
15
|
+
//# sourceMappingURL=provider-config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-config.js","sourceRoot":"","sources":["../../src/config/provider-config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG"}
|
package/dist/config/schema.d.ts
CHANGED
|
@@ -3,7 +3,14 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Type-safe configuration with defaults, validation, and layered overrides.
|
|
5
5
|
* Supports global defaults → file config → environment variables → runtime overrides.
|
|
6
|
+
*
|
|
7
|
+
* `ProviderConfig` is the canonical type, defined in `./provider-config.ts`
|
|
8
|
+
* and re-exported from this module for convenience. The legacy
|
|
9
|
+
* `format`/`apiKey` fields are gone; callers should use the structured
|
|
10
|
+
* `authentication` and `adapter` fields.
|
|
6
11
|
*/
|
|
12
|
+
import type { ProviderConfig } from "./provider-config.js";
|
|
13
|
+
export type { ProviderConfig } from "./provider-config.js";
|
|
7
14
|
export interface SDKConfig {
|
|
8
15
|
/** Default provider name */
|
|
9
16
|
defaultProvider: string;
|
|
@@ -41,13 +48,10 @@ export interface SDKConfig {
|
|
|
41
48
|
allowedTools: string[];
|
|
42
49
|
/** Denied tools */
|
|
43
50
|
deniedTools: string[];
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
apiKey?: string;
|
|
49
|
-
format: "openai" | "anthropic" | "google-genai";
|
|
50
|
-
models?: string[];
|
|
51
|
+
/** Maximum total cost for the session (null = unlimited) */
|
|
52
|
+
sessionBudget?: number;
|
|
53
|
+
/** Maximum cost per individual request (null = unlimited) */
|
|
54
|
+
perRequestBudget?: number;
|
|
51
55
|
}
|
|
52
56
|
export declare const DEFAULT_CONFIG: SDKConfig;
|
|
53
57
|
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/config/schema.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/config/schema.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAE3D,YAAY,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAE3D,MAAM,WAAW,SAAS;IACxB,4BAA4B;IAC5B,eAAe,EAAE,MAAM,CAAC;IACxB,uBAAuB;IACvB,YAAY,EAAE,MAAM,CAAC;IACrB,gCAAgC;IAChC,WAAW,EAAE,MAAM,CAAC;IACpB,yBAAyB;IACzB,SAAS,EAAE,MAAM,CAAC;IAClB,kCAAkC;IAClC,MAAM,EAAE,OAAO,CAAC;IAChB,qCAAqC;IACrC,YAAY,EAAE,OAAO,CAAC;IACtB,2CAA2C;IAC3C,gBAAgB,EAAE,OAAO,CAAC;IAC1B,6CAA6C;IAC7C,QAAQ,EAAE,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,OAAO,CAAC;IAC9C,qCAAqC;IACrC,gBAAgB,EAAE,MAAM,CAAC;IACzB,yBAAyB;IACzB,UAAU,EAAE,MAAM,CAAC;IACnB,6BAA6B;IAC7B,qBAAqB,EAAE,OAAO,CAAC;IAC/B,wCAAwC;IACxC,uBAAuB,EAAE,MAAM,CAAC;IAChC,yCAAyC;IACzC,qBAAqB,EAAE,MAAM,CAAC;IAC9B,2CAA2C;IAC3C,aAAa,EAAE,OAAO,CAAC;IACvB,uCAAuC;IACvC,YAAY,EAAE,MAAM,CAAC;IACrB,qCAAqC;IACrC,SAAS,EAAE,cAAc,EAAE,CAAC;IAC5B,kCAAkC;IAClC,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,mBAAmB;IACnB,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,4DAA4D;IAC5D,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,6DAA6D;IAC7D,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED,eAAO,MAAM,cAAc,EAAE,SAmB5B,CAAC"}
|
package/dist/config/schema.js
CHANGED
|
@@ -3,6 +3,11 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Type-safe configuration with defaults, validation, and layered overrides.
|
|
5
5
|
* Supports global defaults → file config → environment variables → runtime overrides.
|
|
6
|
+
*
|
|
7
|
+
* `ProviderConfig` is the canonical type, defined in `./provider-config.ts`
|
|
8
|
+
* and re-exported from this module for convenience. The legacy
|
|
9
|
+
* `format`/`apiKey` fields are gone; callers should use the structured
|
|
10
|
+
* `authentication` and `adapter` fields.
|
|
6
11
|
*/
|
|
7
12
|
export const DEFAULT_CONFIG = {
|
|
8
13
|
defaultProvider: "",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/config/schema.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/config/schema.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAiDH,MAAM,CAAC,MAAM,cAAc,GAAc;IACvC,eAAe,EAAE,EAAE;IACnB,YAAY,EAAE,QAAQ;IACtB,WAAW,EAAE,GAAG;IAChB,SAAS,EAAE,IAAI;IACf,MAAM,EAAE,IAAI;IACZ,YAAY,EAAE,IAAI;IAClB,gBAAgB,EAAE,KAAK;IACvB,QAAQ,EAAE,MAAM;IAChB,gBAAgB,EAAE,OAAO;IACzB,UAAU,EAAE,CAAC;IACb,qBAAqB,EAAE,IAAI;IAC3B,uBAAuB,EAAE,CAAC;IAC1B,qBAAqB,EAAE,MAAM;IAC7B,aAAa,EAAE,IAAI;IACnB,YAAY,EAAE,OAAO,OAAO,KAAK,WAAW,IAAI,OAAO,OAAO,CAAC,GAAG,KAAK,UAAU,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG;IACvG,SAAS,EAAE,EAAE;IACb,YAAY,EAAE,EAAE;IAChB,WAAW,EAAE,EAAE;CAChB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../src/middleware/middleware.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../src/middleware/middleware.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAEtE,MAAM,WAAW,iBAAiB;IAChC,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,oBAAoB,CAAC;IAC3B,IAAI,EAAE,MAAM,OAAO,CAAC,QAAQ,CAAC,CAAC;CAC/B;AAED,MAAM,MAAM,UAAU,GAAG,CAAC,GAAG,EAAE,iBAAiB,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;AAEvE,wBAAgB,kBAAkB,CAAC,GAAG,WAAW,EAAE,UAAU,EAAE,GAAG,UAAU,CAU3E;AAED,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,MAAM,GAAG,UAAU,CAQjE;AAED,wBAAgB,iBAAiB,CAAC,GAAG,GAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAkB,GAAG,UAAU,CAatF;AAED,wBAAgB,eAAe,CAAC,UAAU,SAAI,EAAE,SAAS,SAAO,GAAG,UAAU,CAe5E;AAED,wBAAgB,mBAAmB,CAAC,UAAU,SAAM,GAAG,UAAU,CAQhE;AAED,wBAAgB,eAAe,CAAC,KAAK,SAAS,GAAG,UAAU,CAY1D"}
|