Files
Memoh/agent/src/index.ts
T
2026-02-08 01:02:04 +08:00

62 lines
1.6 KiB
TypeScript

import { Elysia } from "elysia";
import { chatModule } from "./modules/chat";
import { corsMiddleware } from "./middlewares/cors";
import { errorMiddleware } from "./middlewares/error";
import { loadConfig } from "./config";
import { join } from "path";
const config = loadConfig("../config.toml");
export const getBraveConfig = () => {
return {
apiKey: config.brave.api_key ?? "",
baseUrl: config.brave.base_url ?? "https://api.search.brave.com/res/v1/",
}
}
export const getBaseUrl = () => {
let baseUrl = "";
if (!baseUrl) {
baseUrl = "http://127.0.0.1";
}
if (
typeof config.server.addr === "string" &&
config.server.addr.startsWith(":")
) {
baseUrl = `http://127.0.0.1${config.server.addr}`;
}
return baseUrl;
};
export type AuthFetcher = (
url: string,
options?: RequestInit,
) => Promise<Response>;
export const createAuthFetcher = (bearer: string | undefined): AuthFetcher => {
return async (url: string, options?: RequestInit) => {
const requestOptions = options ?? {};
const headers = new Headers(requestOptions.headers || {});
if (bearer) {
headers.set("Authorization", `Bearer ${bearer}`);
}
return await fetch(join(getBaseUrl(), url), {
...requestOptions,
headers,
});
};
};
const app = new Elysia()
.use(corsMiddleware)
.use(errorMiddleware)
.use(chatModule)
.listen({
port: config.agent_gateway.port ?? 8081,
hostname: config.agent_gateway.host ?? "127.0.0.1",
});
console.log(
`Agent Gateway is running at ${app.server?.hostname}:${app.server?.port}`,
);