Skip to content

Commit aff33f5

Browse files
committed
feat: implement automatic backend terminal command execution
- Add dyad-run-backend-terminal-cmd tag parser to handle backend commands - Update all backend framework system prompts (Django, FastAPI, Flask, Node.js) to use dyad tags instead of showing commands in chat - Implement backend terminal command processing in response_processor.ts - Commands are now automatically executed in backend/ directory instead of shown in chat interface - Remove old terminal command listings from system prompts - Maintain TypeScript compilation compatibility
1 parent f0facc9 commit aff33f5

File tree

3 files changed

+92
-107
lines changed

3 files changed

+92
-107
lines changed

src/ipc/processors/response_processor.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import {
2626
getDyadDeleteTags,
2727
getDyadAddDependencyTags,
2828
getDyadExecuteSqlTags,
29+
getDyadRunBackendTerminalCmdTags,
2930
} from "../utils/dyad_tag_parser";
31+
import { runShellCommand } from "../utils/runShellCommand";
3032
import { storeDbTimestampAtCurrentVersion } from "../utils/neon_timestamp_utils";
3133

3234
import { FileUploadsState } from "../utils/file_uploads_state";
@@ -119,6 +121,7 @@ export async function processFullResponseActions(
119121
const dyadExecuteSqlQueries = chatWithApp.app.supabaseProjectId
120122
? getDyadExecuteSqlTags(fullResponse)
121123
: [];
124+
const dyadRunBackendTerminalCmdTags = getDyadRunBackendTerminalCmdTags(fullResponse);
122125

123126
const message = await db.query.messages.findFirst({
124127
where: and(
@@ -166,7 +169,38 @@ export async function processFullResponseActions(
166169
}
167170
}
168171
logger.log(`Executed ${dyadExecuteSqlQueries.length} SQL queries`);
169-
}
172+
}
173+
174+
// Handle backend terminal command tags
175+
if (dyadRunBackendTerminalCmdTags.length > 0) {
176+
for (const cmdTag of dyadRunBackendTerminalCmdTags) {
177+
try {
178+
const backendPath = path.join(appPath, "backend");
179+
const cwd = cmdTag.cwd ? path.join(backendPath, cmdTag.cwd) : backendPath;
180+
181+
logger.log(`Executing backend terminal command: ${cmdTag.command} in ${cwd}`);
182+
183+
const result = await runShellCommand(`cd "${cwd}" && ${cmdTag.command}`);
184+
185+
if (result === null) {
186+
errors.push({
187+
message: `Backend terminal command failed: ${cmdTag.description || cmdTag.command}`,
188+
error: `Command execution failed in ${cwd}`,
189+
});
190+
} else {
191+
logger.log(`Backend terminal command succeeded: ${cmdTag.description || cmdTag.command}`);
192+
// Add success message to app output
193+
// Note: We don't add to writtenFiles since these are not file changes
194+
}
195+
} catch (error) {
196+
errors.push({
197+
message: `Backend terminal command failed: ${cmdTag.description || cmdTag.command}`,
198+
error: error,
199+
});
200+
}
201+
}
202+
logger.log(`Executed ${dyadRunBackendTerminalCmdTags.length} backend terminal commands`);
203+
}
170204

171205
// Handle add dependency tags
172206
if (dyadAddDependencyPackages.length > 0) {

src/ipc/utils/dyad_tag_parser.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,32 @@ export function getDyadCommandTags(fullResponse: string): string[] {
137137

138138
return commands;
139139
}
140+
141+
export function getDyadRunBackendTerminalCmdTags(fullResponse: string): {
142+
command: string;
143+
cwd?: string;
144+
description?: string;
145+
}[] {
146+
const dyadRunBackendTerminalCmdRegex =
147+
/<dyad-run-backend-terminal-cmd([^>]*)>([\s\S]*?)<\/dyad-run-backend-terminal-cmd>/g;
148+
const cwdRegex = /cwd="([^"]+)"/;
149+
const descriptionRegex = /description="([^"]+)"/;
150+
151+
let match;
152+
const commands: { command: string; cwd?: string; description?: string }[] = [];
153+
154+
while ((match = dyadRunBackendTerminalCmdRegex.exec(fullResponse)) !== null) {
155+
const attributesString = match[1];
156+
const command = match[2].trim();
157+
158+
const cwdMatch = cwdRegex.exec(attributesString);
159+
const descriptionMatch = descriptionRegex.exec(attributesString);
160+
161+
const cwd = cwdMatch?.[1];
162+
const description = descriptionMatch?.[1];
163+
164+
commands.push({ command, cwd, description });
165+
}
166+
167+
return commands;
168+
}

src/prompts/system_prompt.ts

Lines changed: 28 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,12 @@ When working with Django applications:
181181
- Use <read_file> to examine existing Django files (models.py, views.py, urls.py, settings.py)
182182
- Use <search_replace> for precise edits to Django code
183183
- Use <write_to_file> for creating new Django apps, models, views, etc.
184-
- Use <run_terminal_cmd> for Django management commands like:
185-
- \`python backend/manage.py makemigrations\` - Create database migrations
186-
- \`python backend/manage.py migrate\` - Apply database migrations
187-
- \`python backend/manage.py runserver\` - Start development server
188-
- \`python backend/manage.py startapp <app_name>\` - Create new Django app
189-
- \`pip install -r backend/requirements.txt\` - Install Python dependencies
184+
- Use <dyad-run-backend-terminal-cmd> to execute Django management commands automatically in the backend terminal:
185+
- <dyad-run-backend-terminal-cmd description="Install Python dependencies">pip install -r requirements.txt</dyad-run-backend-terminal-cmd>
186+
- <dyad-run-backend-terminal-cmd description="Create database migrations">python manage.py makemigrations</dyad-run-backend-terminal-cmd>
187+
- <dyad-run-backend-terminal-cmd description="Apply database migrations">python manage.py migrate</dyad-run-backend-terminal-cmd>
188+
- <dyad-run-backend-terminal-cmd description="Start development server">python manage.py runserver 8000</dyad-run-backend-terminal-cmd>
189+
- <dyad-run-backend-terminal-cmd description="Create new Django app">python manage.py startapp <app_name></dyad-run-backend-terminal-cmd>
190190
- Use <grep_search> to find patterns across Django codebase
191191
192192
Always explain what you're doing and why, then use the appropriate tools to implement Django solutions. When setting up Django projects, use terminal commands to run migrations and start servers.
@@ -205,22 +205,7 @@ When working with Django applications:
205205
206206
# Django Terminal Commands
207207
208-
Run these commands in the backend terminal after navigating to the backend directory:
209-
210-
**Initial Setup:**
211-
- Install dependencies: \`pip install -r requirements.txt\`
212-
- Run database migrations: \`python manage.py migrate\`
213-
- Create superuser: \`python manage.py createsuperuser\`
214-
215-
**Development Workflow:**
216-
- Start development server: \`python manage.py runserver 8000\`
217-
- Create new migrations: \`python manage.py makemigrations\`
218-
- Apply migrations: \`python manage.py migrate\`
219-
- Create new Django app: \`python manage.py startapp <app_name>\`
220-
221-
**Production Preparation:**
222-
- Collect static files: \`python manage.py collectstatic\`
223-
- Run tests: \`python manage.py test\`
208+
When you need to execute Django commands, use the <dyad-run-backend-terminal-cmd> tags above. The system will automatically run these commands in the backend terminal for you. Do not show commands in chat - use the dyad tags instead.
224209
225210
# Django Best Practices
226211
@@ -244,11 +229,11 @@ When working with FastAPI applications:
244229
- Use <read_file> to examine existing FastAPI files (main.py, routes, schemas, models)
245230
- Use <search_replace> for precise edits to FastAPI code
246231
- Use <write_to_file> for creating new routes, schemas, models, etc.
247-
- Use <run_terminal_cmd> for FastAPI development commands like:
248-
- \`pip install -r backend/requirements.txt\` - Install Python dependencies
249-
- \`uvicorn backend.main:app --reload --host 0.0.0.0 --port 8000\` - Start FastAPI server
250-
- \`alembic revision --autogenerate -m "message"\` - Create database migrations (if using Alembic)
251-
- \`alembic upgrade head\` - Apply database migrations
232+
- Use <dyad-run-backend-terminal-cmd> to execute FastAPI development commands automatically in the backend terminal:
233+
- <dyad-run-backend-terminal-cmd description="Install Python dependencies">pip install -r requirements.txt</dyad-run-backend-terminal-cmd>
234+
- <dyad-run-backend-terminal-cmd description="Start FastAPI server">uvicorn main:app --reload --host 0.0.0.0 --port 8000</dyad-run-backend-terminal-cmd>
235+
- <dyad-run-backend-terminal-cmd description="Create database migrations">alembic revision --autogenerate -m "migration message"</dyad-run-backend-terminal-cmd>
236+
- <dyad-run-backend-terminal-cmd description="Apply database migrations">alembic upgrade head</dyad-run-backend-terminal-cmd>
252237
- Use <grep_search> to find patterns across FastAPI codebase
253238
254239
Always explain what you're doing and why, then use the appropriate tools to implement FastAPI solutions. When setting up FastAPI projects, use terminal commands to install dependencies and start servers.
@@ -266,25 +251,7 @@ When working with FastAPI applications:
266251
267252
# FastAPI Terminal Commands
268253
269-
Run these commands in the backend terminal after navigating to the backend directory:
270-
271-
**Initial Setup:**
272-
- Install dependencies: \`pip install -r requirements.txt\`
273-
- Start development server: \`uvicorn main:app --reload --host 0.0.0.0 --port 8000\`
274-
275-
**Development Workflow:**
276-
- Run with hot reload: \`uvicorn main:app --reload\`
277-
- Run on specific port: \`uvicorn main:app --host 0.0.0.0 --port 8000\`
278-
- Run production server: \`uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4\`
279-
280-
**API Documentation:**
281-
- View interactive docs: Navigate to \`http://localhost:8000/docs\`
282-
- View alternative docs: Navigate to \`http://localhost:8000/redoc\`
283-
284-
**Testing:**
285-
- Run tests: \`pytest\`
286-
- Run with coverage: \`pytest --cov=.\`
287-
- Run specific test: \`pytest tests/test_file.py\`
254+
When you need to execute FastAPI commands, use the <dyad-run-backend-terminal-cmd> tags above. The system will automatically run these commands in the backend terminal for you. Do not show commands in chat - use the dyad tags instead.
288255
289256
# FastAPI Best Practices
290257
@@ -309,13 +276,13 @@ When working with Flask applications:
309276
- Use <read_file> to examine existing Flask files (app.py, routes, models, templates)
310277
- Use <search_replace> for precise edits to Flask code
311278
- Use <write_to_file> for creating new routes, models, templates, etc.
312-
- Use <run_terminal_cmd> for Flask development commands like:
313-
- \`pip install -r backend/requirements.txt\` - Install Python dependencies
314-
- \`python backend/app.py\` - Start Flask development server
315-
- \`flask run\` - Alternative way to start Flask server (with FLASK_APP set)
316-
- \`flask db init\` - Initialize Flask-Migrate (if using Flask-SQLAlchemy)
317-
- \`flask db migrate\` - Create database migrations
318-
- \`flask db upgrade\` - Apply database migrations
279+
- Use <dyad-run-backend-terminal-cmd> to execute Flask development commands automatically in the backend terminal:
280+
- <dyad-run-backend-terminal-cmd description="Install Python dependencies">pip install -r requirements.txt</dyad-run-backend-terminal-cmd>
281+
- <dyad-run-backend-terminal-cmd description="Start Flask development server">python app.py</dyad-run-backend-terminal-cmd>
282+
- <dyad-run-backend-terminal-cmd description="Alternative Flask start">flask run --host=0.0.0.0 --port=5000</dyad-run-backend-terminal-cmd>
283+
- <dyad-run-backend-terminal-cmd description="Initialize Flask-Migrate">flask db init</dyad-run-backend-terminal-cmd>
284+
- <dyad-run-backend-terminal-cmd description="Create database migrations">flask db migrate -m "migration message"</dyad-run-backend-terminal-cmd>
285+
- <dyad-run-backend-terminal-cmd description="Apply database migrations">flask db upgrade</dyad-run-backend-terminal-cmd>
319286
- Use <grep_search> to find patterns across Flask codebase
320287
321288
Always explain what you're doing and why, then use the appropriate tools to implement Flask solutions. When setting up Flask projects, use terminal commands to install dependencies and start servers.
@@ -333,27 +300,7 @@ When working with Flask applications:
333300
334301
# Flask Terminal Commands
335302
336-
Run these commands in the backend terminal after navigating to the backend directory:
337-
338-
**Initial Setup:**
339-
- Install dependencies: \`pip install -r requirements.txt\`
340-
- Start development server: \`python app.py\`
341-
- Alternative start: \`flask run --host=0.0.0.0 --port=5000\`
342-
343-
**Database Management (if using Flask-Migrate):**
344-
- Initialize migrations: \`flask db init\`
345-
- Create migration: \`flask db migrate -m "migration message"\`
346-
- Apply migrations: \`flask db upgrade\`
347-
- Rollback migration: \`flask db downgrade\`
348-
349-
**Development Workflow:**
350-
- Run with debugger: \`FLASK_ENV=development flask run\`
351-
- Run on different port: \`flask run --port=8000\`
352-
- Use Flask CLI: \`export FLASK_APP=app.py && flask run\`
353-
354-
**Testing:**
355-
- Run tests: \`python -m pytest\`
356-
- Run with Flask test client: \`python -c "from app import app; app.test_client()"\`
303+
When you need to execute Flask commands, use the <dyad-run-backend-terminal-cmd> tags above. The system will automatically run these commands in the backend terminal for you. Do not show commands in chat - use the dyad tags instead.
357304
358305
# Flask Best Practices
359306
@@ -378,12 +325,12 @@ When working with Node.js applications:
378325
- Use <read_file> to examine existing Node.js files (server.js, routes, models, controllers)
379326
- Use <search_replace> for precise edits to Node.js code
380327
- Use <write_to_file> for creating new routes, models, controllers, middleware, etc.
381-
- Use <run_terminal_cmd> for Node.js development commands like:
382-
- \`npm install\` - Install Node.js dependencies
383-
- \`npm start\` - Start production server
384-
- \`npm run dev\` - Start development server with auto-reload
385-
- \`node backend/server.js\` - Run Node.js server directly
386-
- \`npx nodemon backend/server.js\` - Run with auto-restart on changes
328+
- Use <dyad-run-backend-terminal-cmd> to execute Node.js development commands automatically in the backend terminal:
329+
- <dyad-run-backend-terminal-cmd description="Install Node.js dependencies">npm install</dyad-run-backend-terminal-cmd>
330+
- <dyad-run-backend-terminal-cmd description="Start production server">npm start</dyad-run-backend-terminal-cmd>
331+
- <dyad-run-backend-terminal-cmd description="Start development server">npm run dev</dyad-run-backend-terminal-cmd>
332+
- <dyad-run-backend-terminal-cmd description="Run server directly">node server.js</dyad-run-backend-terminal-cmd>
333+
- <dyad-run-backend-terminal-cmd description="Run with nodemon">npx nodemon server.js</dyad-run-backend-terminal-cmd>
387334
- Use <grep_search> to find patterns across Node.js codebase
388335
389336
Always explain what you're doing and why, then use the appropriate tools to implement Node.js solutions. When setting up Node.js projects, use terminal commands to install dependencies and start servers.
@@ -401,32 +348,7 @@ When working with Node.js applications:
401348
402349
# Node.js Terminal Commands
403350
404-
Run these commands in the backend terminal after navigating to the backend directory:
405-
406-
**Initial Setup:**
407-
- Install dependencies: \`npm install\`
408-
- Start development server: \`npm run dev\` or \`nodemon server.js\`
409-
- Start production server: \`npm start\` or \`node server.js\`
410-
411-
**Development Workflow:**
412-
- Install new package: \`npm install <package-name>\`
413-
- Install dev dependency: \`npm install --save-dev <package-name>\`
414-
- Run custom scripts: \`npm run <script-name>\`
415-
- Check scripts: \`npm run\`
416-
417-
**Database Management (MongoDB example):**
418-
- Start MongoDB: \`mongod\` (if running locally)
419-
- Connect to MongoDB: Use MongoDB Compass or CLI tools
420-
421-
**Testing:**
422-
- Run all tests: \`npm test\`
423-
- Run tests in watch mode: \`npm run test:watch\`
424-
- Run specific test: \`npm test -- --testNamePattern="test name"\`
425-
426-
**Build and Deployment:**
427-
- Build for production: \`npm run build\` (if applicable)
428-
- Lint code: \`npm run lint\`
429-
- Format code: \`npm run format\`
351+
When you need to execute Node.js commands, use the <dyad-run-backend-terminal-cmd> tags above. The system will automatically run these commands in the backend terminal for you. Do not show commands in chat - use the dyad tags instead.
430352
431353
# Node.js Best Practices
432354

0 commit comments

Comments
 (0)