mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-01-11 19:56:44 +00:00
feat(ai-agent): Update AI Agent API routes and remove deprecated ingest feature
This commit is contained in:
parent
1a2acbf12d
commit
19f8dc8b19
7 changed files with 55 additions and 114 deletions
|
|
@ -8,20 +8,10 @@ if (!process.env["ONEUPTIME_URL"]) {
|
|||
process.exit();
|
||||
}
|
||||
|
||||
export let ONEUPTIME_URL: URL = URL.fromString(
|
||||
export const ONEUPTIME_URL: URL = URL.fromString(
|
||||
process.env["ONEUPTIME_URL"] || "https://oneuptime.com",
|
||||
);
|
||||
|
||||
// If the URL does not have the ai-agent-ingest path, add it.
|
||||
if (
|
||||
!ONEUPTIME_URL.toString().endsWith("ai-agent-ingest") &&
|
||||
!ONEUPTIME_URL.toString().endsWith("ai-agent-ingest/")
|
||||
) {
|
||||
ONEUPTIME_URL = URL.fromString(
|
||||
ONEUPTIME_URL.addRoute("/ai-agent-ingest").toString(),
|
||||
);
|
||||
}
|
||||
|
||||
export const AI_AGENT_ID: ObjectID | null = process.env["AI_AGENT_ID"]
|
||||
? new ObjectID(process.env["AI_AGENT_ID"])
|
||||
: null;
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ const InitJob: VoidFunction = (): void => {
|
|||
|
||||
const aliveUrl: URL = URL.fromString(
|
||||
ONEUPTIME_URL.toString(),
|
||||
).addRoute("/alive");
|
||||
).addRoute("/api/ai-agent/alive");
|
||||
|
||||
const result: HTTPResponse<JSONObject> = await API.post({
|
||||
url: aliveUrl,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { ONEUPTIME_URL, AI_AGENT_ID, AI_AGENT_KEY } from "../Config";
|
||||
import AIAgentAPIRequest from "../Utils/AIAgentAPIRequest";
|
||||
import HTTPResponse from "Common/Types/API/HTTPResponse";
|
||||
import URL from "Common/Types/API/URL";
|
||||
import { JSONObject } from "Common/Types/JSON";
|
||||
|
|
@ -44,7 +43,7 @@ export default class Register {
|
|||
|
||||
const aliveUrl: URL = URL.fromString(
|
||||
ONEUPTIME_URL.toString(),
|
||||
).addRoute("/alive");
|
||||
).addRoute("/api/ai-agent/alive");
|
||||
|
||||
logger.debug("Registering AI Agent...");
|
||||
logger.debug("Sending request to: " + aliveUrl.toString());
|
||||
|
|
|
|||
|
|
@ -1,82 +0,0 @@
|
|||
import BadDataException from "Common/Types/Exception/BadDataException";
|
||||
import { JSONObject } from "Common/Types/JSON";
|
||||
import ObjectID from "Common/Types/ObjectID";
|
||||
import AIAgentService from "Common/Server/Services/AIAgentService";
|
||||
import Express, {
|
||||
ExpressRequest,
|
||||
ExpressResponse,
|
||||
ExpressRouter,
|
||||
NextFunction,
|
||||
} from "Common/Server/Utils/Express";
|
||||
import Response from "Common/Server/Utils/Response";
|
||||
import AIAgent from "Common/Models/DatabaseModels/AIAgent";
|
||||
|
||||
const router: ExpressRouter = Express.getRouter();
|
||||
|
||||
// Middleware to authorize AI Agent requests
|
||||
async function isAuthorizedAIAgentMiddleware(
|
||||
req: ExpressRequest,
|
||||
res: ExpressResponse,
|
||||
next: NextFunction,
|
||||
): Promise<void> {
|
||||
const data: JSONObject = req.body;
|
||||
|
||||
if (!data["aiAgentId"] || !data["aiAgentKey"]) {
|
||||
return Response.sendErrorResponse(
|
||||
req,
|
||||
res,
|
||||
new BadDataException("aiAgentId or aiAgentKey is missing"),
|
||||
);
|
||||
}
|
||||
|
||||
const aiAgentId: ObjectID = new ObjectID(data["aiAgentId"] as string);
|
||||
|
||||
const aiAgentKey: string = data["aiAgentKey"] as string;
|
||||
|
||||
const aiAgent: AIAgent | null = await AIAgentService.findOneBy({
|
||||
query: {
|
||||
_id: aiAgentId.toString(),
|
||||
secretKey: aiAgentKey,
|
||||
},
|
||||
select: {
|
||||
_id: true,
|
||||
},
|
||||
props: {
|
||||
isRoot: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!aiAgent) {
|
||||
return Response.sendErrorResponse(
|
||||
req,
|
||||
res,
|
||||
new BadDataException("Invalid AI Agent ID or AI Agent Key"),
|
||||
);
|
||||
}
|
||||
|
||||
// Update last alive
|
||||
await AIAgentService.updateLastAlive(aiAgentId);
|
||||
|
||||
return next();
|
||||
}
|
||||
|
||||
router.post(
|
||||
"/alive",
|
||||
isAuthorizedAIAgentMiddleware,
|
||||
async (
|
||||
req: ExpressRequest,
|
||||
res: ExpressResponse,
|
||||
next: NextFunction,
|
||||
): Promise<void> => {
|
||||
try {
|
||||
// Update last alive in AI Agent and return success response.
|
||||
// The middleware already updates lastAlive, so we just return success.
|
||||
|
||||
return Response.sendEmptySuccessResponse(req, res);
|
||||
} catch (err) {
|
||||
return next(err);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
export default router;
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
import AIAgentIngestAPI from "./API/AIAgentIngest";
|
||||
import FeatureSet from "Common/Server/Types/FeatureSet";
|
||||
import Express, { ExpressApplication } from "Common/Server/Utils/Express";
|
||||
|
||||
const AIAgentIngestFeatureSet: FeatureSet = {
|
||||
init: async (): Promise<void> => {
|
||||
const app: ExpressApplication = Express.getExpressApp();
|
||||
|
||||
const APP_NAME: string = "ai-agent-ingest";
|
||||
|
||||
// Mount the AI Agent ingest API routes
|
||||
app.use([`/${APP_NAME}`, "/"], AIAgentIngestAPI);
|
||||
},
|
||||
};
|
||||
|
||||
export default AIAgentIngestFeatureSet;
|
||||
|
|
@ -2,7 +2,6 @@ import BaseAPIRoutes from "./FeatureSet/BaseAPI/Index";
|
|||
// import FeatureSets.
|
||||
import IdentityRoutes from "./FeatureSet/Identity/Index";
|
||||
import NotificationRoutes from "./FeatureSet/Notification/Index";
|
||||
import AIAgentIngestRoutes from "./FeatureSet/AIAgentIngest/Index";
|
||||
import { PromiseVoidFunction } from "Common/Types/FunctionTypes";
|
||||
import { ClickhouseAppInstance } from "Common/Server/Infrastructure/ClickhouseDatabase";
|
||||
import PostgresAppInstance from "Common/Server/Infrastructure/PostgresDatabase";
|
||||
|
|
@ -95,7 +94,6 @@ const init: PromiseVoidFunction = async (): Promise<void> => {
|
|||
await IdentityRoutes.init();
|
||||
await NotificationRoutes.init();
|
||||
await BaseAPIRoutes.init();
|
||||
await AIAgentIngestRoutes.init();
|
||||
|
||||
// Add default routes to the app
|
||||
await App.addDefaultRoutes();
|
||||
|
|
|
|||
|
|
@ -12,11 +12,63 @@ import BaseAPI from "./BaseAPI";
|
|||
import LIMIT_MAX from "../../Types/Database/LimitMax";
|
||||
import PositiveNumber from "../../Types/PositiveNumber";
|
||||
import AIAgent from "../../Models/DatabaseModels/AIAgent";
|
||||
import BadDataException from "../../Types/Exception/BadDataException";
|
||||
import { JSONObject } from "../../Types/JSON";
|
||||
import ObjectID from "../../Types/ObjectID";
|
||||
|
||||
export default class AIAgentAPI extends BaseAPI<AIAgent, AIAgentServiceType> {
|
||||
public constructor() {
|
||||
super(AIAgent, AIAgentService);
|
||||
|
||||
// Alive endpoint for AI Agent heartbeat
|
||||
this.router.post(
|
||||
`${new this.entityType().getCrudApiPath()?.toString()}/alive`,
|
||||
async (req: ExpressRequest, res: ExpressResponse, next: NextFunction) => {
|
||||
try {
|
||||
const data: JSONObject = req.body;
|
||||
|
||||
if (!data["aiAgentId"] || !data["aiAgentKey"]) {
|
||||
return Response.sendErrorResponse(
|
||||
req,
|
||||
res,
|
||||
new BadDataException("aiAgentId or aiAgentKey is missing"),
|
||||
);
|
||||
}
|
||||
|
||||
const aiAgentId: ObjectID = new ObjectID(data["aiAgentId"] as string);
|
||||
const aiAgentKey: string = data["aiAgentKey"] as string;
|
||||
|
||||
const aiAgent: AIAgent | null = await AIAgentService.findOneBy({
|
||||
query: {
|
||||
_id: aiAgentId.toString(),
|
||||
secretKey: aiAgentKey,
|
||||
},
|
||||
select: {
|
||||
_id: true,
|
||||
},
|
||||
props: {
|
||||
isRoot: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!aiAgent) {
|
||||
return Response.sendErrorResponse(
|
||||
req,
|
||||
res,
|
||||
new BadDataException("Invalid AI Agent ID or AI Agent Key"),
|
||||
);
|
||||
}
|
||||
|
||||
// Update last alive
|
||||
await AIAgentService.updateLastAlive(aiAgentId);
|
||||
|
||||
return Response.sendEmptySuccessResponse(req, res);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
this.router.post(
|
||||
`${new this.entityType().getCrudApiPath()?.toString()}/global-ai-agents`,
|
||||
UserMiddleware.getUserMiddleware,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue