Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 70 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ This template demonstrates how to build an AI coding assistant that can work wit
- **Live Development Monitoring**: Watch directory changes and monitor development workflows
- **Command Execution**: Run shell commands, install packages, and manage dependencies
- **Memory System**: Persistent conversation memory with semantic recall and working memory
- **Personalization Layer**: Configure agent identity, preferences, and a catalog of repeatable workflows
- **Desktop Launcher**: Electron-based desktop application for starting/stopping the agent without the terminal
- **Development Workflows**: Professional development patterns with build automation

## Prerequisites
Expand All @@ -29,7 +31,8 @@ This template demonstrates how to build an AI coding assistant that can work wit
```bash
git clone https://github.com/mastra-ai/template-coding-agent.git
cd template-coding-agent
pnpm install
npm install
# or: pnpm install
```

2. **Set up environment variables:**
Expand All @@ -44,12 +47,27 @@ This template demonstrates how to build an AI coding assistant that can work wit
OPENAI_API_KEY="your-openai-api-key-here"
```

3. **Start the development server:**
3. **Check your environment:**

```bash
pnpm run dev
npm run doctor
```

The doctor script highlights missing dependencies or API keys before you launch the agent.

4. **Start the agent (pick one):**

- **CLI:** `npm run dev` for hot-reload development or `npm run start` for production mode.
- **Desktop App:** `npm run desktop` to open the Electron launcher. Provide your API keys once and start/stop the agent with buttons. Output streaming and status updates are surfaced in the UI.

5. **Package a desktop build (optional):**

```bash
npm run desktop:package
```

This produces a platform-specific build in the `dist/` directory using `electron-builder`.

## Architecture

### Core Components
Expand All @@ -63,6 +81,7 @@ The main agent with comprehensive development capabilities:
- **File Operations**: Complete CRUD operations for files and directories
- **Development Monitoring**: Watches for changes and monitors workflows
- **Memory Integration**: Maintains conversation context and project history
- **Personal Profile**: Dynamically incorporates preferences defined in `personal-config.ts`

#### **E2B Tools** (`src/mastra/tools/e2b.ts`)

Expand Down Expand Up @@ -98,6 +117,7 @@ Complete toolkit for sandbox interaction:
**Development Workflow:**

- `runCommand` - Execute shell commands, build scripts, package management
- `runCustomCommand` - Trigger user-defined workflows stored in `personal-config.ts`

### Memory System

Expand All @@ -117,22 +137,56 @@ E2B_API_KEY=your_e2b_api_key_here
OPENAI_API_KEY=your_openai_api_key_here
```

### Customization
### Personalization & Custom Commands

You can customize the agent behavior by modifying the instructions in `src/mastra/agents/coding-agent.ts`:
Fine-tune the assistant without editing core agent code by updating `src/mastra/agents/personal-config.ts`.

```typescript
export const codingAgent = new Agent({
name: 'Coding Agent',
instructions: `
// Customize agent instructions here
// Focus on specific languages, frameworks, or development patterns
`,
model: openai('gpt-4.1'),
// ... other configuration
});
export const personalAgentConfig = {
name: 'My Personal Coding Companion',
tagline: 'A persistent AI partner tailored to your workflows.',
mission: 'Describe the long-term goals for the agent.',
preferences: {
languages: ['TypeScript', 'Python'],
technologies: ['Node.js', 'React'],
developmentStyle: ['Prefer TDD', 'Document architectural decisions'],
},
memoryStrategy: {
longTermSummary: 'What should be remembered across sessions.',
workingMemoryGuidelines: ['Snapshot progress at the end of each session.'],
},
customCommands: [
{
id: 'start-dev',
label: 'Start Dev Server',
description: 'Launch the development server inside the sandbox.',
command: 'pnpm run dev',
},
],
};
```

Key capabilities provided by the configuration file:

- **Agent identity** — control tone, mission, and project focus.
- **Preference sets** — inform the agent about favored languages, tools, and delivery style.
- **Memory strategy** — guide how long-term and short-term context should be stored.
- **Custom commands** — register reusable shell commands that can be triggered with the `runCustomCommand` tool using a `commandId`.

Feel free to extend the configuration with additional custom commands or memory options; the agent automatically reads the updated details on startup.

### Desktop Launcher

The Electron desktop app wraps the same Mastra agent so you can run it without the terminal:

1. Install dependencies and run `npm run doctor` to verify prerequisites.
2. Launch the desktop shell with `npm run desktop`.
3. Enter your API keys once—the app stores them locally using the operating system's application storage.
4. Start or stop the agent with a single click. Live logs and status updates stream into the UI so you can monitor the session in real time.
5. Package a distributable build with `npm run desktop:package` when you are ready to share it across machines.

The launcher proxies your chosen mode (`mastra dev` for development or `mastra start` for production) and forwards environment variables so the agent runs exactly as it does via the CLI.

## Common Issues

### "E2B_API_KEY is not set"
Expand Down Expand Up @@ -171,8 +225,10 @@ export const codingAgent = new Agent({
src/mastra/
agents/
coding-agent.ts # Main coding agent with development capabilities
personal-config.ts # User-editable persona, memory, and command catalog
tools/
e2b.ts # Complete E2B sandbox interaction toolkit
custom-commands.ts # Tool for invoking reusable workflows
index.ts # Mastra configuration with storage and logging
```

Expand Down
180 changes: 180 additions & 0 deletions desktop/electron-main.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import { app, BrowserWindow, ipcMain } from 'electron';
import { spawn } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import path from 'node:path';
import process from 'node:process';
import { existsSync } from 'node:fs';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const projectRoot = path.resolve(__dirname, '..');

let mainWindow;
let agentProcess = null;
let currentMode = null;

const sendStatus = status => {
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send('agent:status', status);
}
};

const sendOutput = payload => {
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send('agent:output', payload);
}
};

const resolveRendererPath = () => {
const rendererUrl = new URL('./renderer/index.html', import.meta.url);
return fileURLToPath(rendererUrl);
};

const resolvePreloadPath = () => {
const preloadUrl = new URL('./preload.mjs', import.meta.url);
return fileURLToPath(preloadUrl);
};

const createWindow = () => {
mainWindow = new BrowserWindow({
width: 1100,
height: 720,
title: 'Coding Agent Desktop',
webPreferences: {
preload: resolvePreloadPath(),
},
});

mainWindow.loadFile(resolveRendererPath());

mainWindow.on('closed', () => {
mainWindow = null;
});
};

const startAgentProcess = (mode, extraEnv) => {
if (agentProcess) {
return { ok: false, message: 'The agent is already running.' };
}

const mastraBinary = path.join(
projectRoot,
'node_modules',
'.bin',
process.platform === 'win32' ? 'mastra.cmd' : 'mastra',
);

if (!existsSync(mastraBinary)) {
return {
ok: false,
message:
'Could not locate the Mastra CLI. Install dependencies first with "npm install" or "pnpm install".',
};
}

const commandArgs = mode === 'dev' ? ['dev'] : ['start'];

const spawnOptions = {
cwd: projectRoot,
env: { ...process.env, ...extraEnv },
shell: process.platform === 'win32',
};

try {
agentProcess = spawn(mastraBinary, commandArgs, spawnOptions);
} catch (error) {
return { ok: false, message: `Failed to launch Mastra: ${error.message}` };
}

currentMode = mode;
sendStatus({ state: 'running', mode });

const forward = (type, data) => {
sendOutput({ type, data: data.toString(), timestamp: Date.now() });
};

agentProcess.stdout?.on('data', chunk => forward('stdout', chunk));
agentProcess.stderr?.on('data', chunk => forward('stderr', chunk));

agentProcess.on('exit', code => {
sendOutput({ type: 'event', data: `Agent exited with code ${code ?? 'unknown'}`, timestamp: Date.now() });
currentMode = null;
sendStatus({ state: 'stopped' });
agentProcess = null;
});

agentProcess.on('error', error => {
sendOutput({ type: 'error', data: error.message, timestamp: Date.now() });
currentMode = null;
sendStatus({ state: 'stopped' });
agentProcess = null;
});

return { ok: true };
};

const stopAgentProcess = () => {
if (!agentProcess) {
return { ok: false, message: 'The agent is not running.' };
}

sendStatus({ state: 'stopping', mode: currentMode });

if (process.platform === 'win32') {
agentProcess.kill();
} else {
agentProcess.kill('SIGINT');
setTimeout(() => agentProcess?.kill('SIGKILL'), 4000);
}

return { ok: true };
};

app.whenReady().then(() => {
createWindow();

app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});

app.on('before-quit', () => {
if (agentProcess) {
stopAgentProcess();
}
});

ipcMain.handle('agent:start', async (_event, payload) => {
const { mode, env } = payload ?? {};
if (!mode || (mode !== 'dev' && mode !== 'start')) {
return { ok: false, message: 'Invalid mode. Choose either "dev" or "start".' };
}

const result = startAgentProcess(mode, env ?? {});
if (!result.ok) {
sendStatus({ state: 'stopped' });
}
return result;
});

ipcMain.handle('agent:stop', async () => {
const result = stopAgentProcess();
if (!result.ok) {
return result;
}
return { ok: true };
});

ipcMain.handle('agent:status', async () => {
if (!agentProcess) {
return { state: 'stopped' };
}
return { state: 'running', mode: currentMode };
});
17 changes: 17 additions & 0 deletions desktop/preload.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { contextBridge, ipcRenderer } from 'electron';

contextBridge.exposeInMainWorld('codingAgentDesktop', {
startAgent: options => ipcRenderer.invoke('agent:start', options),
stopAgent: () => ipcRenderer.invoke('agent:stop'),
getStatus: () => ipcRenderer.invoke('agent:status'),
onOutput: callback => {
const listener = (_event, payload) => callback(payload);
ipcRenderer.on('agent:output', listener);
return () => ipcRenderer.removeListener('agent:output', listener);
},
onStatus: callback => {
const listener = (_event, payload) => callback(payload);
ipcRenderer.on('agent:status', listener);
return () => ipcRenderer.removeListener('agent:status', listener);
},
});
Loading