Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
feat: add scripts for generating new agents and tools
Brad Barbine4f9dba3c212
diff --git a/README.md b/README.mdindex 50b1f54906..e957b9c744 100644--- a/README.md+++ b/README.md@@ -228,7 +228,31 @@ After installation you should be able run the apps locally.| Mailpit | [http://localhost:54324](http://localhost:54324) || Edge Functions | [http://localhost:54321/functions/v1/<function-name>](http://localhost:54321/functions/v1/<function-name>) |-### Notes+### Commands++To add an edge function++```bash+$ npm run db:function:new <name>+```++To add a database migration++```bash+$ npm run db:migrate:new <name>+```++To add an AI agent++```bash+$ npm run agent:new <name>+```++To add an AI tool++```bash+$ npm run tool:new <name>+```To kill the database containers in a non-recoverable way, you can run:diff --git a/apps/erp/app/components/Chat/ChatStatusIndicators.tsx b/apps/erp/app/components/Chat/ChatStatusIndicators.tsxindex 280add6b0e..a7ec188e91 100644--- a/apps/erp/app/components/Chat/ChatStatusIndicators.tsx+++ b/apps/erp/app/components/Chat/ChatStatusIndicators.tsx@@ -1,9 +1,9 @@import { AnimatedStatus } from "./AnimatedStatus";-import { getStatusMessage, getToolMessage } from "./lib/agent";+import { getStatusMessage } from "./lib/agent";import type { AgentStatus } from "./lib/types";import { Loader } from "./Loader";-import { getToolIcon } from "./ToolCallIndicator";+import { getToolIcon, getToolMessage } from "./ToolCallIndicator";interface ChatStatusIndicatorsProps {agentStatus: AgentStatus | null;@@ -17,7 +17,7 @@ export function ChatStatusIndicators({status,}: ChatStatusIndicatorsProps) {const statusMessage = getStatusMessage(agentStatus);- const toolMessage = getToolMessage(currentToolCall);+ const toolMessage = currentToolCall ? getToolMessage(currentToolCall) : null;// Always prioritize tool message over agent status when a tool is runningconst displayMessage = toolMessage || statusMessage;diff --git a/apps/erp/app/components/Chat/ToolCallIndicator.tsx b/apps/erp/app/components/Chat/ToolCallIndicator.tsxindex caf59d6755..10b12ad9aa 100644--- a/apps/erp/app/components/Chat/ToolCallIndicator.tsx+++ b/apps/erp/app/components/Chat/ToolCallIndicator.tsx@@ -1,13 +1,8 @@import { cn, TextShimmer } from "@carbon/react";import type { IconType } from "react-icons";+import { LuBrain } from "react-icons/lu";+import { toolConfigs as toolDisplayConfig } from "~/routes/api+/ai+/chat+/tools/config";-import { toolConfigs as toolDisplayConfig } from "~/routes/api+/ai+/chat+/config";--export interface ToolConfig {- name: string;- icon: IconType;- description?: string;-}export type SupportedToolName = keyof typeof toolDisplayConfig;export interface ToolCallIndicatorProps {@@ -16,9 +11,25 @@ export interface ToolCallIndicatorProps {}export function getToolIcon(toolName: SupportedToolName): IconType | null {+ if (toolName === "handoff_to_agent") {+ return LuBrain;+ }+ if (toolName === "updateWorkingMemory") {+ return LuBrain;+ }return toolDisplayConfig[toolName]?.icon ?? null;}+export function getToolMessage(toolName: SupportedToolName): string | null {+ if (toolName === "handoff_to_agent") {+ return "Connecting you with the right specialist...";+ }+ if (toolName === "updateWorkingMemory") {+ return "Updating working memory...";+ }+ return toolDisplayConfig[toolName]?.message ?? null;+}+export function ToolCallIndicator({toolName,className,diff --git a/apps/erp/app/components/Chat/lib/agent.tsx b/apps/erp/app/components/Chat/lib/agent.tsxindex 1b1fa136fc..c9c3bde101 100644--- a/apps/erp/app/components/Chat/lib/agent.tsx+++ b/apps/erp/app/components/Chat/lib/agent.tsx@@ -1,3 +1,4 @@+import { agentConfigs } from "~/routes/api+/ai+/chat+/agents/config";import type { AgentStatus } from "./types";// Generate user-friendly status messages@@ -13,41 +14,9 @@ export const getStatusMessage = (status?: AgentStatus | null) => {}if (state === "executing") {- const messages: Record<AgentStatus["agent"], string> = {- triage: "Thinking...",-- general: "Searching the web...",- purchasing: "Calling the purchasing agent...",- parts: "Calling the parts agent...",- suppliers: "Calling the suppliers agent...",- };-- return messages[agent];+ const agentConfig = agentConfigs[agent];+ return agentConfig?.executingMessage || "Processing...";}return null;};--// Generate user-friendly tool messages-export const getToolMessage = (toolName: string | null) => {- if (!toolName) return null;-- const toolMessages: Record<string, string> = {- // Reports tools- createPurchaseOrder: "Creating a purchase order...",- getPart: "Searching for a part...",- getSupplier: "Searching for a supplier...",- getSupplierForParts: "Searching for suppliers for parts...",-- // Research tools- webSearch: "Searching the web...",-- // Memory tools- updateWorkingMemory: "Updating working memory...",-- // Handoff tools- handoff_to_agent: "Connecting you with the right specialist...",- };-- return toolMessages[toolName];-};diff --git a/apps/erp/app/routes/api+/ai+/chat+/agents/config.ts b/apps/erp/app/routes/api+/ai+/chat+/agents/config.tsnew file mode 100644index 0000000000..d8be3356fa--- /dev/null+++ b/apps/erp/app/routes/api+/ai+/chat+/agents/config.ts@@ -0,0 +1,16 @@+import type { AgentConfig } from "./shared/tools";++// Dynamically import all agent configs+const agentModules = import.meta.glob("./*-agent.ts", {+ eager: true,+}) as Record<string, { config: AgentConfig }>;++// Export agent configs as a record keyed by agent name+export const agentConfigs: Record<string, AgentConfig> = Object.values(+ agentModules+).reduce((acc, module) => {+ if (module.config) {+ acc[module.config.name] = module.config;+ }+ return acc;+}, {} as Record<string, AgentConfig>);diff --git a/apps/erp/app/routes/api+/ai+/chat+/agents/orchestration-agent.ts b/apps/erp/app/routes/api+/ai+/chat+/agents/orchestration-agent.tsindex 565ca6dfb2..5b4d00485f 100644--- a/apps/erp/app/routes/api+/ai+/chat+/agents/orchestration-agent.ts+++ b/apps/erp/app/routes/api+/ai+/chat+/agents/orchestration-agent.ts@@ -3,6 +3,14 @@ import { purchasingAgent } from "./purchasing-agent";import { searchAgent } from "./search-agent";import { createAgent } from "./shared/agent";import { formatContextForLLM } from "./shared/prompts";+import type { AgentConfig } from "./shared/tools";++export const config: AgentConfig = {+ name: "triage",+ displayName: "Triage Agent",+ description: "Routes user requests to the appropriate specialist",+ executingMessage: "Thinking...",+};export const orchestrationAgent = createAgent({name: "triage",diff --git a/apps/erp/app/routes/api+/ai+/chat+/agents/purchasing-agent.ts b/apps/erp/app/routes/api+/ai+/chat+/agents/purchasing-agent.tsindex 4557c1694d..b92a7c5ed9 100644--- a/apps/erp/app/routes/api+/ai+/chat+/agents/purchasing-agent.ts+++ b/apps/erp/app/routes/api+/ai+/chat+/agents/purchasing-agent.ts@@ -5,6 +5,14 @@ import { getSupplierTool } from "../tools/get-supplier";import { getSupplierForPartsTool } from "../tools/get-supplier-for-parts";import { createAgent } from "./shared/agent";import { COMMON_AGENT_RULES, formatContextForLLM } from "./shared/prompts";+import type { AgentConfig } from "./shared/tools";++export const config: AgentConfig = {+ name: "purchasing",+ displayName: "Purchasing Agent",+ description: "Creates purchase orders and gets quotes from suppliers",+ executingMessage: "Calling the purchasing agent...",+};export const purchasingAgent = createAgent({name: "purchasing",diff --git a/apps/erp/app/routes/api+/ai+/chat+/agents/search-agent.ts b/apps/erp/app/routes/api+/ai+/chat+/agents/search-agent.tsindex b166aa6986..a2721ee261 100644--- a/apps/erp/app/routes/api+/ai+/chat+/agents/search-agent.ts+++ b/apps/erp/app/routes/api+/ai+/chat+/agents/search-agent.ts@@ -3,6 +3,14 @@ import { webSearchTool } from "../tools/web-search";import { purchasingAgent } from "./purchasing-agent";import { createAgent } from "./shared/agent";import { COMMON_AGENT_RULES, formatContextForLLM } from "./shared/prompts";+import type { AgentConfig } from "./shared/tools";++export const config: AgentConfig = {+ name: "general",+ displayName: "General Agent",+ description: "Handles general questions and web searches",+ executingMessage: "Searching the web...",+};export const searchAgent = createAgent({name: "general",diff --git a/apps/erp/app/routes/api+/ai+/chat+/agents/shared/tools.ts b/apps/erp/app/routes/api+/ai+/chat+/agents/shared/tools.tsindex 2b85ed2ae0..b3d804a485 100644--- a/apps/erp/app/routes/api+/ai+/chat+/agents/shared/tools.ts+++ b/apps/erp/app/routes/api+/ai+/chat+/agents/shared/tools.ts@@ -3,5 +3,13 @@ import type { IconType } from "react-icons";export interface ToolConfig {name: string;icon: IconType;- displayText?: string;+ displayText: string;+ message: string;+}++export interface AgentConfig {+ name: string;+ displayName: string;+ description: string;+ executingMessage: string;}diff --git a/apps/erp/app/routes/api+/ai+/chat+/config.ts b/apps/erp/app/routes/api+/ai+/chat+/tools/config.tssimilarity index 82%rename from apps/erp/app/routes/api+/ai+/chat+/config.tsrename to apps/erp/app/routes/api+/ai+/chat+/tools/config.tsindex 153d49c0f4..dbe39b796c 100644--- a/apps/erp/app/routes/api+/ai+/chat+/config.ts+++ b/apps/erp/app/routes/api+/ai+/chat+/tools/config.ts@@ -1,4 +1,4 @@-import type { ToolConfig } from "./agents/shared/tools";+import type { ToolConfig } from "../agents/shared/tools";// Dynamically import all tool configsconst toolModules = import.meta.glob("./tools/*/index.ts", {@@ -13,27 +13,29 @@ const directToolModules = import.meta.glob("./tools/*.ts", {// Combine both patterns and create the config objectexport const toolConfigs: Record<string,- Pick<ToolConfig, "icon" | "displayText">+ Pick<ToolConfig, "icon" | "displayText" | "message">> = {...Object.values(toolModules).reduce((acc, module) => {if (module.config) {acc[module.config.name] = {icon: module.config.icon,displayText: module.config.displayText,+ message: module.config.message,};}return acc;- }, {} as Record<string, Pick<ToolConfig, "icon" | "displayText">>),+ }, {} as Record<string, Pick<ToolConfig, "icon" | "displayText" | "message">>),...Object.values(directToolModules).reduce((acc, module) => {if (module.config) {acc[module.config.name] = {icon: module.config.icon,displayText: module.config.displayText,+ message: module.config.message,};}return acc;- }, {} as Record<string, Pick<ToolConfig, "icon" | "displayText">>),+ }, {} as Record<string, Pick<ToolConfig, "icon" | "displayText" | "message">>),};console.log(toolConfigs);diff --git a/apps/erp/app/routes/api+/ai+/chat+/tools/create-purchase-order.ts b/apps/erp/app/routes/api+/ai+/chat+/tools/create-purchase-order.tsindex 4612155ce6..b592c52784 100644--- a/apps/erp/app/routes/api+/ai+/chat+/tools/create-purchase-order.ts+++ b/apps/erp/app/routes/api+/ai+/chat+/tools/create-purchase-order.ts@@ -20,6 +20,7 @@ export const config: ToolConfig = {name: "createPurchaseOrder",icon: LuShoppingCart,displayText: "Creating a Purchase Order",+ message: "Creating a purchase order...",};export const createPurchaseOrderSchema = z.object({diff --git a/apps/erp/app/routes/api+/ai+/chat+/tools/get-part.ts b/apps/erp/app/routes/api+/ai+/chat+/tools/get-part.tsindex 1fc0363b68..3597caa10b 100644--- a/apps/erp/app/routes/api+/ai+/chat+/tools/get-part.ts+++ b/apps/erp/app/routes/api+/ai+/chat+/tools/get-part.ts@@ -9,6 +9,7 @@ export const config: ToolConfig = {name: "getPart",icon: LuSearch,displayText: "Getting Part",+ message: "Searching for a part...",};export const getPartSchema = zdiff --git a/apps/erp/app/routes/api+/ai+/chat+/tools/get-supplier-for-parts.ts b/apps/erp/app/routes/api+/ai+/chat+/tools/get-supplier-for-parts.tsindex f5e23d6235..4193184f8a 100644--- a/apps/erp/app/routes/api+/ai+/chat+/tools/get-supplier-for-parts.ts+++ b/apps/erp/app/routes/api+/ai+/chat+/tools/get-supplier-for-parts.ts@@ -12,6 +12,7 @@ export const config: ToolConfig = {name: "getSupplierForParts",icon: LuSearch,displayText: "Getting Supplier for Parts",+ message: "Searching for suppliers for parts...",};export const getSupplierForPartsSchema = z.object({diff --git a/apps/erp/app/routes/api+/ai+/chat+/tools/get-supplier.ts b/apps/erp/app/routes/api+/ai+/chat+/tools/get-supplier.tsindex 73e50fd399..e224d51023 100644--- a/apps/erp/app/routes/api+/ai+/chat+/tools/get-supplier.ts+++ b/apps/erp/app/routes/api+/ai+/chat+/tools/get-supplier.ts@@ -12,6 +12,7 @@ export const config: ToolConfig = {name: "getSupplier",icon: LuSearch,displayText: "Getting Supplier",+ message: "Searching for a supplier...",};export const getSupplierSchema = zdiff --git a/apps/erp/app/routes/api+/ai+/chat+/tools/web-search.ts b/apps/erp/app/routes/api+/ai+/chat+/tools/web-search.tsindex fa748364f1..d20b046907 100644--- a/apps/erp/app/routes/api+/ai+/chat+/tools/web-search.ts+++ b/apps/erp/app/routes/api+/ai+/chat+/tools/web-search.ts@@ -10,6 +10,7 @@ export const config: ToolConfig = {name: "webSearch",icon: LuGlobe,displayText: "Searching the Web",+ message: "Searching the web...",};// Static tool that uses context at execution time via experimental_contextdiff --git a/package.json b/package.jsonindex 0f29f74689..75f44a2c35 100755--- a/package.json+++ b/package.json@@ -68,6 +68,8 @@"prepare": "npx husky install","setup": "turbo run setup","test": "turbo run test",+ "tool:new": "tsx scripts/create-tool.ts",+ "agent:new": "tsx scripts/create-agent.ts","types": "turbo run types"},"workspaces": [diff --git a/scripts/create-agent.ts b/scripts/create-agent.tsnew file mode 100644index 0000000000..94b8f7e33c--- /dev/null+++ b/scripts/create-agent.ts@@ -0,0 +1,96 @@+import { existsSync, writeFileSync } from "fs";+import { join } from "path";++const agentName = process.argv[2];++if (!agentName) {+ console.error("Error: Agent name is required");+ console.log("Usage: npm run agent:new <agentName>");+ console.log("Example: npm run agent:new inventory");+ process.exit(1);+}++// Convert agentName to kebab-case for file name+const fileName = agentName.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();++const AGENTS_DIR = join(+ process.cwd(),+ "apps",+ "erp",+ "app",+ "routes",+ "api+",+ "ai+",+ "chat+",+ "agents"+);++if (!existsSync(AGENTS_DIR)) {+ console.error(`Error: Agents directory not found at ${AGENTS_DIR}`);+ process.exit(1);+}++const agentFilePath = join(AGENTS_DIR, `${fileName}-agent.ts`);++if (existsSync(agentFilePath)) {+ console.error(`Error: Agent file already exists at ${agentFilePath}`);+ process.exit(1);+}++// Convert to PascalCase for display name+const displayName =+ agentName.charAt(0).toUpperCase() ++ agentName.slice(1).replace(/([A-Z])/g, " $1");++const agentTemplate = `import { openai } from "@ai-sdk/openai";+import { createAgent } from "./shared/agent";+import { COMMON_AGENT_RULES, formatContextForLLM } from "./shared/prompts";+import type { AgentConfig } from "./shared/tools";++export const config: AgentConfig = {+ name: "${agentName}",+ displayName: "${displayName} Agent",+ description: "Description of what this agent does",+ executingMessage: "Calling the ${agentName} agent...",+};++export const ${agentName}Agent = createAgent({+ name: "${agentName}",+ model: openai("gpt-4o"),+ temperature: 0.5,+ instructions: (ctx) => \`You are a ${agentName} specialist for \${ctx.companyName}.++<background-data>+\${formatContextForLLM(ctx)}+</background-data>++\${COMMON_AGENT_RULES}++<capabilities>+- Add your agent capabilities here+</capabilities>\`,+ tools: {+ // Add your tools here+ // exampleTool: exampleToolTool,+ },+ handoffs: [],+ maxTurns: 5,+});+`;++try {+ writeFileSync(agentFilePath, agentTemplate, "utf-8");+ console.log(`✅ Successfully created agent at ${agentFilePath}`);+ console.log(`\nNext steps:`);+ console.log(`1. Edit ${agentFilePath} to implement your agent logic`);+ console.log(`2. Add tools to the tools object`);+ console.log(`3. Update the instructions and capabilities`);+ console.log(`4. Configure handoffs to other agents if needed`);+ console.log(`5. Add the agent to the AgentStatus type in app/components/Chat/lib/types.ts`);+ console.log(+ `6. Import and add the agent to handoffs in orchestration-agent.ts if needed`+ );+} catch (error) {+ console.error(`Failed to create agent file:`, error);+ process.exit(1);+}diff --git a/scripts/create-tool.ts b/scripts/create-tool.tsnew file mode 100644index 0000000000..ffc3d37133--- /dev/null+++ b/scripts/create-tool.ts@@ -0,0 +1,83 @@+import { existsSync, writeFileSync } from "fs";+import { join } from "path";++const toolName = process.argv[2];++if (!toolName) {+ console.error("Error: Tool name is required");+ console.log("Usage: npm run tool:new <toolName>");+ console.log("Example: npm run tool:new getPart");+ process.exit(1);+}++// Convert toolName to kebab-case for file name+const fileName = toolName.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();++const TOOLS_DIR = join(+ process.cwd(),+ "apps",+ "erp",+ "app",+ "routes",+ "api+",+ "ai+",+ "chat+",+ "tools"+);++if (!existsSync(TOOLS_DIR)) {+ console.error(`Error: Tools directory not found at ${TOOLS_DIR}`);+ process.exit(1);+}++const toolFilePath = join(TOOLS_DIR, `${fileName}.ts`);++if (existsSync(toolFilePath)) {+ console.error(`Error: Tool file already exists at ${toolFilePath}`);+ process.exit(1);+}++const toolTemplate = `import { tool } from "ai";+import { LuSearch } from "react-icons/lu";+import { z } from "zod";+import type { ToolConfig } from "../agents/shared/tools";+import type { ChatContext } from "../agents/shared/context";++export const config: ToolConfig = {+ name: "${toolName}",+ icon: LuSearch,+ displayText: "Processing ${toolName}",+ message: "Processing ${toolName.toLowerCase()}...",+};++export const ${toolName}Schema = z.object({+ // Add your schema properties here+});++export const ${toolName}Tool = tool({+ description: "Description of what this tool does",+ inputSchema: ${toolName}Schema,+ execute: async function (args, executionOptions) {+ const context = executionOptions.experimental_context as ChatContext;++ // TODO: Implement tool logic here++ return {+ message: "Hello from ${toolName}!",+ };+ },+});+`;++try {+ writeFileSync(toolFilePath, toolTemplate, "utf-8");+ console.log(`✅ Successfully created tool at ${toolFilePath}`);+ console.log(`\nNext steps:`);+ console.log(`1. Edit ${toolFilePath} to implement your tool logic`);+ console.log(`2. Update the schema in ${toolName}Schema`);+ console.log(`3. Update the description and execute function`);+ console.log(`4. Change the icon if needed (from react-icons/lu)`);+} catch (error) {+ console.error(`Failed to create tool file:`, error);+ process.exit(1);+}