Skip to content

Commit 1d94bf2

Browse files
committed
create middleware integration
1 parent cc8fffe commit 1d94bf2

File tree

1 file changed

+126
-1
lines changed

1 file changed

+126
-1
lines changed

typescript-sdk/create-integration.ts

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,132 @@ if (!kebabCaseRegex.test(integrationName)) {
5757

5858
if (hasMiddleware) {
5959
console.log(`Creating middleware-based integration: ${integrationName}`);
60-
// TODO: Implement middleware integration creation
60+
61+
const { execSync } = require("child_process");
62+
const path = require("path");
63+
const fs = require("fs");
64+
65+
const integrationsDir = path.join(__dirname, "integrations");
66+
const sourceDir = path.join(integrationsDir, "middleware-starter");
67+
const targetDir = path.join(integrationsDir, integrationName);
68+
69+
// Check if source directory exists
70+
if (!fs.existsSync(sourceDir)) {
71+
console.error(`Error: Template directory not found: ${sourceDir}`);
72+
process.exit(1);
73+
}
74+
75+
// Check if target directory already exists
76+
if (fs.existsSync(targetDir)) {
77+
console.error(`Error: Integration directory already exists: ${targetDir}`);
78+
process.exit(1);
79+
}
80+
81+
try {
82+
console.log(
83+
`Copying template from integrations/middleware-starter to integrations/${integrationName}...`,
84+
);
85+
execSync(`cp -r "${sourceDir}" "${targetDir}"`, { stdio: "inherit" });
86+
console.log(`✓ Created integration at integrations/${integrationName}`);
87+
88+
// Update package.json
89+
const packageJsonPath = path.join(targetDir, "package.json");
90+
console.log(`Updating package.json...`);
91+
92+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
93+
packageJson.name = `@ag-ui/${integrationName}`;
94+
95+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + "\n");
96+
console.log(`✓ Updated package name to @ag-ui/${integrationName}`);
97+
98+
// Convert kebab-case to PascalCase
99+
const pascalCaseName = integrationName
100+
.split("-")
101+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
102+
.join("");
103+
const agentClassName = `${pascalCaseName}Agent`;
104+
105+
// Update src/index.ts
106+
const indexPath = path.join(targetDir, "src", "index.ts");
107+
console.log(`Updating src/index.ts...`);
108+
109+
let indexContent = fs.readFileSync(indexPath, "utf8");
110+
indexContent = indexContent.replace(
111+
/MiddlewareStarterAgent/g,
112+
agentClassName
113+
);
114+
115+
fs.writeFileSync(indexPath, indexContent);
116+
console.log(`✓ Updated class name to ${agentClassName}`);
117+
118+
// Update apps/dojo/src/menu.ts
119+
const menuPath = path.join(__dirname, "apps", "dojo", "src", "menu.ts");
120+
console.log(`Updating apps/dojo/src/menu.ts...`);
121+
122+
let menuContent = fs.readFileSync(menuPath, "utf8");
123+
124+
const newIntegration = ` {
125+
id: "${integrationName}",
126+
name: "${pascalCaseName}",
127+
features: ["agentic_chat"],
128+
},\n`;
129+
130+
// Find the menuIntegrations array and prepend the new integration
131+
menuContent = menuContent.replace(
132+
/(export const menuIntegrations: MenuIntegrationConfig\[\] = \[\n)/,
133+
`$1${newIntegration}`
134+
);
135+
136+
fs.writeFileSync(menuPath, menuContent);
137+
console.log(`✓ Registered integration in dojo menu`);
138+
139+
// Update apps/dojo/src/agents.ts
140+
const agentsPath = path.join(__dirname, "apps", "dojo", "src", "agents.ts");
141+
console.log(`Updating apps/dojo/src/agents.ts...`);
142+
143+
let agentsContent = fs.readFileSync(agentsPath, "utf8");
144+
145+
// Add import statement at the top
146+
const importStatement = `import { ${agentClassName} } from "@ag-ui/${integrationName}";\n`;
147+
agentsContent = importStatement + agentsContent;
148+
149+
const newAgentIntegration = ` {
150+
id: "${integrationName}",
151+
agents: async () => {
152+
return {
153+
agentic_chat: new ${agentClassName}(),
154+
}
155+
},
156+
},\n`;
157+
158+
// Find the agentsIntegrations array and prepend the new integration
159+
agentsContent = agentsContent.replace(
160+
/(export const agentsIntegrations: AgentIntegrationConfig\[\] = \[\n)/,
161+
`$1${newAgentIntegration}`
162+
);
163+
164+
fs.writeFileSync(agentsPath, agentsContent);
165+
console.log(`✓ Registered agent in dojo agents`);
166+
167+
// Update apps/dojo/package.json
168+
const dojoPackageJsonPath = path.join(__dirname, "apps", "dojo", "package.json");
169+
console.log(`Updating apps/dojo/package.json...`);
170+
171+
const dojoPackageJson = JSON.parse(fs.readFileSync(dojoPackageJsonPath, "utf8"));
172+
173+
// Add the new integration as a dependency at the beginning
174+
const newDependencies = {
175+
[`@ag-ui/${integrationName}`]: "workspace:*",
176+
...dojoPackageJson.dependencies
177+
};
178+
dojoPackageJson.dependencies = newDependencies;
179+
180+
fs.writeFileSync(dojoPackageJsonPath, JSON.stringify(dojoPackageJson, null, 2) + "\n");
181+
console.log(`✓ Added @ag-ui/${integrationName} to dojo dependencies`);
182+
} catch (error) {
183+
console.error("Error creating integration:", error);
184+
process.exit(1);
185+
}
61186
}
62187

63188
if (hasServer) {

0 commit comments

Comments
 (0)