All skills
medusajs avatar

/creating-internal-agents

@597f4a0 official
by Medusamedusajs/medusa-agent-skills225 stars
29

Use when building an internal admin-facing AI agent in a Medusa project. These agents are operated by merchants and store operators — not customers. Covers data models, module service, agent runtime (tools, system prompt, streamText), streaming API routes (NDJSON), and admin UI chat extensions. Load for any internal agent type: store operations assistant, product audit, cohort analysis, customer service tooling for support staff, etc. Do NOT use for customer-facing agents (storefront chatbots, buyer-side assistants).

  • 8 files
  • 46.7 KB
  • Updated 3 months ago
  • GitHub

Use this Skill: https://skilld.dev/gh/medusajs/medusa-agent-skills/creating-internal-agents

This session only. Nothing lands on disk.

referencedata-models.md

≈589 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Data Models

AgentSession and AgentMessage are shared infrastructure — one set of tables serves every agent in the project. An agent_type field on the session distinguishes which agent owns it, so a customer service agent and a product audit agent both write to the same tables without colliding.

Do not create separate session/message models per agent. Add agent_type and reuse these models.

Models

// src/modules/agent/models/session.ts
import { model } from "@medusajs/framework/utils"
import { AgentMessage } from "./message"

export const AgentSession = model.define("agent_session", {
  id: model.id({ prefix: "sess" }).primaryKey(),
  agent_type: model.text(),                // e.g. "customer-service", "product-audit"
  title: model.text().nullable(),          // first 72 chars of opening message
  created_by_id: model.text(),             // actor_id from auth context
  messages: model.hasMany(() => AgentMessage),
})
// src/modules/agent/models/message.ts
import { model } from "@medusajs/framework/utils"
import { AgentSession } from "./session"

export const AgentMessage = model.define("agent_message", {
  id: model.id({ prefix: "msg" }).primaryKey(),
  agent_session: model.belongsTo(() => AgentSession, { mappedBy: "messages" }),
  role: model.enum(["user", "assistant"]),
  content: model.text(),
})

Key Rules

  • agent_type — set this to a stable, lowercase slug when creating a session. Each agent's API route passes its own value. Use it to filter sessions in the list endpoint so each agent only sees its own history.
  • model.id({ prefix: "..." }) — generates a prefixed ID (e.g. sess_01JABCD…).
  • hasMany / belongsTo — always define both sides. The mappedBy value must match the field name on the parent.
  • nullable() — title is set lazily from the first message; it can be null at creation.

Migrations

After adding or changing models, run:

npx medusa db:generate agent   # matches the module resolve path in medusa-config.ts
npx medusa db:migrate

CRITICAL: The name passed to db:generate must match how the module is resolved in medusa-config.ts.

Exporting Models

// Imported directly in service.ts
import { AgentSession } from "./models/session"
import { AgentMessage } from "./models/message"

Source: SKILL.md on GitHub

No third-party reports yet.

Signed by skilld at 597f4a0. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 days ago.

Activeupdated 3 months ago

README badge

README badge for medusajs/medusa-agent-skills/creating-internal-agents