All checks were successful
ci/woodpecker/push/ci Pipeline was successful
Co-authored-by: Jason Woltje <jason@diversecanvas.com> Co-committed-by: Jason Woltje <jason@diversecanvas.com>
69 lines
1.9 KiB
TypeScript
69 lines
1.9 KiB
TypeScript
import { Injectable } from "@nestjs/common";
|
|
import type { Prisma } from "@prisma/client";
|
|
import { PrismaService } from "../../prisma/prisma.service";
|
|
|
|
@Injectable()
|
|
export class AgentControlService {
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
private toJsonValue(value: Record<string, unknown>): Prisma.InputJsonValue {
|
|
return value as Prisma.InputJsonValue;
|
|
}
|
|
|
|
private async createOperatorAuditLog(
|
|
agentId: string,
|
|
operatorId: string,
|
|
action: "inject" | "pause" | "resume",
|
|
payload: Record<string, unknown>
|
|
): Promise<void> {
|
|
await this.prisma.operatorAuditLog.create({
|
|
data: {
|
|
sessionId: agentId,
|
|
userId: operatorId,
|
|
provider: "internal",
|
|
action,
|
|
metadata: this.toJsonValue({ payload }),
|
|
},
|
|
});
|
|
}
|
|
|
|
async injectMessage(agentId: string, operatorId: string, message: string): Promise<void> {
|
|
const treeEntry = await this.prisma.agentSessionTree.findUnique({
|
|
where: { sessionId: agentId },
|
|
select: { id: true },
|
|
});
|
|
|
|
if (treeEntry) {
|
|
await this.prisma.agentConversationMessage.create({
|
|
data: {
|
|
sessionId: agentId,
|
|
role: "operator",
|
|
content: message,
|
|
provider: "internal",
|
|
metadata: this.toJsonValue({}),
|
|
},
|
|
});
|
|
}
|
|
|
|
await this.createOperatorAuditLog(agentId, operatorId, "inject", { message });
|
|
}
|
|
|
|
async pauseAgent(agentId: string, operatorId: string): Promise<void> {
|
|
await this.prisma.agentSessionTree.updateMany({
|
|
where: { sessionId: agentId },
|
|
data: { status: "paused" },
|
|
});
|
|
|
|
await this.createOperatorAuditLog(agentId, operatorId, "pause", {});
|
|
}
|
|
|
|
async resumeAgent(agentId: string, operatorId: string): Promise<void> {
|
|
await this.prisma.agentSessionTree.updateMany({
|
|
where: { sessionId: agentId },
|
|
data: { status: "running" },
|
|
});
|
|
|
|
await this.createOperatorAuditLog(agentId, operatorId, "resume", {});
|
|
}
|
|
}
|