Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions services/ai-gateway/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const RawConfig = Config.all({
port: Config.int("AI_GATEWAY_LISTEN_PORT").pipe(
Config.withDefault(8787),
),
idleTimeoutSeconds: Config.int("AI_GATEWAY_IDLE_TIMEOUT_SECONDS").pipe(
Config.withDefault(255),
),
gracefulShutdownMillis: Config.int(
"AI_GATEWAY_GRACEFUL_SHUTDOWN_MILLIS",
).pipe(Config.withDefault(20_000)),
Expand Down Expand Up @@ -84,6 +87,7 @@ export interface AIGatewayConfig {
readonly stateDirectory: string;
readonly hostname: string;
readonly port: number;
readonly idleTimeoutSeconds: number;
readonly gracefulShutdownMillis: number;
readonly clientAuthenticationMode: "shared_token" | "workload_identity";
readonly clientToken: Redacted.Redacted<string>;
Expand Down Expand Up @@ -134,6 +138,7 @@ export const loadAIGatewayConfig = Effect.fn(
stateDirectory,
hostname: raw.hostname,
port: raw.port,
idleTimeoutSeconds: raw.idleTimeoutSeconds,
gracefulShutdownMillis: raw.gracefulShutdownMillis,
clientAuthenticationMode,
clientToken: raw.clientToken,
Expand Down Expand Up @@ -194,6 +199,7 @@ function validConfiguration(
validSecret(operatorToken) &&
validSecret(openAIApiKey) &&
validPositiveInteger(raw.port, 65_535) &&
validNonNegativeInteger(raw.idleTimeoutSeconds, 255) &&
validPositiveInteger(raw.gracefulShutdownMillis) &&
validPositiveInteger(raw.heartbeatMillis) &&
validPositiveInteger(raw.maximumUsageEventBytes) &&
Expand All @@ -215,6 +221,10 @@ function validPositiveInteger(value: number, maximum?: number): boolean {
(maximum === undefined || value <= maximum);
}

function validNonNegativeInteger(value: number, maximum: number): boolean {
return Number.isSafeInteger(value) && value >= 0 && value <= maximum;
}

function validString(value: string, maximum: number): boolean {
return value.length > 0 && value.length <= maximum && value === value.trim();
}
Expand Down
1 change: 1 addition & 0 deletions services/ai-gateway/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ function makeAIGatewayRuntimeLive(
Layer.provide(BunHttpServer.layer({
hostname: serveConfig.hostname,
port: serveConfig.port,
idleTimeout: serveConfig.idleTimeoutSeconds,
gracefulShutdownTimeout: serveConfig.gracefulShutdownMillis,
})),
);
Expand Down
13 changes: 11 additions & 2 deletions services/ai-gateway/tests/cli.effect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function configuration(
stateDirectory: "/state",
hostname: "0.0.0.0",
port: 8787,
idleTimeoutSeconds: 255,
gracefulShutdownMillis: 20_000,
clientAuthenticationMode: "shared_token",
clientToken: Redacted.make("fleet-secret"),
Expand Down Expand Up @@ -71,6 +72,7 @@ const makeHarness = Effect.fn("test.aiGateway.makeCliHarness")(function*() {
const served = yield* Ref.make<ReadonlyArray<{
readonly hostname: string;
readonly port: number;
readonly idleTimeoutSeconds: number;
readonly authentication: string;
}>>([]);
const statusTokens = yield* Ref.make<ReadonlyArray<string>>([]);
Expand Down Expand Up @@ -117,6 +119,7 @@ const makeHarness = Effect.fn("test.aiGateway.makeCliHarness")(function*() {
{
hostname: config.hostname,
port: config.port,
idleTimeoutSeconds: config.idleTimeoutSeconds,
authentication: config.authentication.kind,
},
]),
Expand Down Expand Up @@ -172,17 +175,22 @@ describe("AI Gateway Effect CLI", () => {
]);
}));

it.effect("passes the explicit native bind address to the Effect runtime", () =>
it.effect("passes HTTP server settings to the Effect runtime", () =>
Effect.gen(function*() {
const harness = yield* makeHarness();
const exitCode = yield* runAIGatewayCli(
["serve"],
configuration({ hostname: "127.0.0.2", port: 9876 }),
configuration({
hostname: "127.0.0.2",
port: 9876,
idleTimeoutSeconds: 120,
}),
).pipe(Effect.provide(harness.layer));
assert.strictEqual(exitCode, 0);
assert.deepStrictEqual(yield* Ref.get(harness.served), [{
hostname: "127.0.0.2",
port: 9876,
idleTimeoutSeconds: 120,
authentication: "shared_token",
}]);
}));
Expand All @@ -202,6 +210,7 @@ describe("AI Gateway Effect CLI", () => {
assert.deepStrictEqual(yield* Ref.get(harness.served), [{
hostname: "0.0.0.0",
port: 8787,
idleTimeoutSeconds: 255,
authentication: "workload_identity",
}]);
}));
Expand Down
22 changes: 22 additions & 0 deletions services/ai-gateway/tests/config.effect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe("AI Gateway Effect configuration", () => {
);
assert.strictEqual(config.hostname, "0.0.0.0");
assert.strictEqual(config.port, 8787);
assert.strictEqual(config.idleTimeoutSeconds, 255);
assert.strictEqual(String(config.clientToken), "<redacted>");
assert.strictEqual(String(config.openAIApiKey), "<redacted>");
assert.strictEqual(Redacted.value(config.clientToken), "fleet-secret");
Expand All @@ -52,6 +53,25 @@ describe("AI Gateway Effect configuration", () => {
);
}));

it.effect("loads an explicit Bun HTTP idle timeout", () =>
Effect.gen(function*() {
const configured = yield* loadAIGatewayConfig().pipe(
Effect.provide(environment({
HOME: "/home/agentos",
AI_GATEWAY_IDLE_TIMEOUT_SECONDS: "120",
})),
);
const disabled = yield* loadAIGatewayConfig().pipe(
Effect.provide(environment({
HOME: "/home/agentos",
AI_GATEWAY_IDLE_TIMEOUT_SECONDS: "0",
})),
);

assert.strictEqual(configured.idleTimeoutSeconds, 120);
assert.strictEqual(disabled.idleTimeoutSeconds, 0);
}));

it.effect("rejects missing shared auth and malformed runtime bounds", () =>
Effect.gen(function*() {
const missingToken = yield* loadAIGatewayConfig().pipe(
Expand All @@ -64,6 +84,8 @@ describe("AI Gateway Effect configuration", () => {
const invalidEnvironments: ReadonlyArray<Readonly<Record<string, string>>> = [
{ AI_GATEWAY_LISTEN_PORT: "0" },
{ AI_GATEWAY_LISTEN_PORT: "65536" },
{ AI_GATEWAY_IDLE_TIMEOUT_SECONDS: "-1" },
{ AI_GATEWAY_IDLE_TIMEOUT_SECONDS: "256" },
{ AI_GATEWAY_HEARTBEAT_MILLIS: "0" },
{ AGENTOS_PROVIDER_BUDGET_SETTLEMENT_TIMEOUT_MILLIS: "0" },
];
Expand Down