AkurAI Build
Menu

popagent

public

Latest change da13a7bebe63bf4b2693180d2d4850aabeaa0807 - Add autonomous evolution and self-healing by AkurAI Build

import { createSkill } from "@mastra/core/skills";
import { storage } from "./storage";
import type { AgentSkill, AgentSkillInput } from "./api-types";
import { initializeAgentRoleStorage } from "./agent-settings";


export class AgentSkillStore {
  readonly storage = storage;

  init(): Promise<void> {
    return initializeAgentRoleStorage();
  }

  async list(agentId: string, enabled?: boolean): Promise<AgentSkill[]> {
    await this.init();
    return this.storage.db.any<AgentSkill>(`
      SELECT id, agent_id AS "agentId", name, description, instructions, "references",
             source_urls AS "sourceUrls", enabled, user_invocable AS "userInvocable",
             evolution_managed AS "evolutionManaged",
             created_at::text AS "createdAt", updated_at::text AS "updatedAt"
      FROM popagent_agent_skills
      WHERE agent_id = $1 AND ($2::boolean IS NULL OR enabled = $2)
      ORDER BY name
    `, [agentId, enabled ?? null]);
  }

  async create(agentId: string, input: AgentSkillInput): Promise<AgentSkill> {
    await this.init();
    return this.storage.db.one<AgentSkill>(`
      INSERT INTO popagent_agent_skills
        (id, agent_id, name, description, instructions, "references", enabled,
         user_invocable, evolution_managed)
      VALUES ($1, $2, $3, $4, $5, $6::jsonb, $7, $8, FALSE)
      RETURNING id, agent_id AS "agentId", name, description, instructions, "references",
                source_urls AS "sourceUrls", enabled, user_invocable AS "userInvocable",
                evolution_managed AS "evolutionManaged",
                created_at::text AS "createdAt", updated_at::text AS "updatedAt"
    `, [crypto.randomUUID(), agentId, input.name, input.description, input.instructions, JSON.stringify(input.references), input.enabled, input.userInvocable]);
  }

  async update(agentId: string, id: string, input: AgentSkillInput): Promise<AgentSkill | undefined> {
    await this.init();
    return (await this.storage.db.oneOrNone<AgentSkill>(`
      UPDATE popagent_agent_skills
      SET name = $3, description = $4, instructions = $5, "references" = $6::jsonb,
          enabled = $7, user_invocable = $8, evolution_managed = FALSE, updated_at = NOW()
      WHERE agent_id = $1 AND id = $2
      RETURNING id, agent_id AS "agentId", name, description, instructions, "references",
                source_urls AS "sourceUrls", enabled, user_invocable AS "userInvocable",
                evolution_managed AS "evolutionManaged",
                created_at::text AS "createdAt", updated_at::text AS "updatedAt"
    `, [agentId, id, input.name, input.description, input.instructions, JSON.stringify(input.references), input.enabled, input.userInvocable])) ?? undefined;
  }

  async delete(agentId: string, id: string): Promise<boolean> {
    await this.init();
    return Boolean(await this.storage.db.oneOrNone<{ id: string }>(
      "DELETE FROM popagent_agent_skills WHERE agent_id = $1 AND id = $2 RETURNING id",
      [agentId, id],
    ));
  }

  async resolve(agentId: string) {
    return (await this.list(agentId, true)).map((skill) => createSkill({
      name: skill.name,
      description: skill.description,
      instructions: skill.instructions,
      references: skill.references,
      "user-invocable": skill.userInvocable,
    }));
  }
}

export const agentSkills = new AgentSkillStore();