@essentialai/cogent-bridge
Version:
Cogent Bridge — cross-agent comms for Claude Code, OpenAI Codex and Slack. Codex users: install with `curl -fsSL https://cogent.tools/install.sh | sh`, then start with `cogent-codex` for real-time peer wake.
193 lines (177 loc) • 7.06 kB
JavaScript
/**
* The env block the wizard writes into a project's `.mcp.json`.
* Cloud is the zero-config default (COGENT_ENDPOINT defaults to the free relay),
* so cloud adds nothing extra. Local mode sets the COGENT_LOCAL=1 opt-out so the
* bridge uses on-machine file state and never contacts a relay — for offline /
* air-gapped / local-LLM setups. The wizard writes this so the user never
* hand-edits a config file.
*/
function cogentEnv(mode) {
const env = {
COGENT_LOG_LEVEL: "info",
COGENT_TIMEOUT_MS: "300000",
};
if (mode === "local")
env.COGENT_LOCAL = "1";
return env;
}
export function mcpJsonContent(npxPath, mode = "cloud") {
const config = {
mcpServers: {
"cogent": {
command: npxPath,
args: ["-y", "@essentialai/cogent-bridge"],
env: cogentEnv(mode),
},
},
};
return JSON.stringify(config, null, 2) + "\n";
}
export function mcpJsonMerge(existing, npxPath, mode = "cloud") {
const parsed = JSON.parse(existing);
if (!parsed.mcpServers)
parsed.mcpServers = {};
parsed.mcpServers["cogent"] = {
command: npxPath,
args: ["-y", "@essentialai/cogent-bridge"],
env: cogentEnv(mode),
};
return JSON.stringify(parsed, null, 2) + "\n";
}
export function claudeMdBridgeSection(peerId, label, otherPeerId) {
return `
## Cogent Bridge Protocol
You are participating in a cross-team collaboration via Cogent Bridge.
Your peer identity: **peerId=\`${peerId}\`**, **label=\`${label}\`**
### On Session Start
1. Find your Claude Code session ID:
\`\`\`bash
ls -t "\${CLAUDE_CONFIG_DIR:-$HOME/.claude}"/projects/$(pwd | sed 's/[^a-zA-Z0-9-]/-/g')/*.jsonl 2>/dev/null | head -1 | xargs -I{} basename {} .jsonl
\`\`\`
2. Register yourself on the bridge:
- Use \`cogent_register_peer\` with peerId="${peerId}", label="${label}"
- Set sessionId to the UUID from step 1
- Set cwd to your project working directory (\`pwd\`)
### When You Receive Messages
- Messages from other peers arrive with a \`[Cogent Bridge message from ...]\` header
- Your entire response is automatically relayed back to the sender
- Do NOT use \`cogent_send_message\` to reply — just answer directly and normally
- Read carefully, investigate the issue, and respond with specifics
### Sending Messages
- Use \`cogent_send_message\` to initiate conversations with other peers (e.g. toPeerId="${otherPeerId}")
- Be specific about what you changed when reporting fixes
- Include file names and line numbers in your responses
`;
}
export function demoServerJs() {
return `// server.js
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.json());
const users = [
{ id: 1, name: 'Alice', email: 'alice@example.com', role: 'admin' },
{ id: 2, name: 'Bob', email: 'bob@example.com', role: 'user' },
{ id: 3, name: 'Charlie', email: 'charlie@example.com', role: 'user' },
];
// GET /api/users - works fine
app.get('/api/users', (req, res) => {
res.json({ success: true, data: users });
});
// GET /api/users/:id - BUG: returns 200 with wrong shape when user not found
app.get('/api/users/:id', (req, res) => {
const id = parseInt(req.params.id);
const user = users.find(u => u.id === id);
// BUG 1: No 404 handling - returns 200 with { success: true, data: undefined }
// BUG 2: When user IS found, wraps in extra nested object
res.json({ success: true, data: { user: { record: user } } });
});
// POST /api/users - works fine
app.post('/api/users', (req, res) => {
const { name, email, role } = req.body;
const newUser = { id: users.length + 1, name, email, role: role || 'user' };
users.push(newUser);
res.status(201).json({ success: true, data: newUser });
});
app.listen(3001, () => console.log('API running on http://localhost:3001'));
`;
}
export function demoAppJs() {
// Using regular string concatenation to avoid template literal escaping issues.
// The output JavaScript uses backtick template literals with ${} interpolation.
const lines = [
"// app.js",
"const axios = require('axios');",
"const API_BASE = 'http://localhost:3001';",
"",
"async function listUsers() {",
" const res = await axios.get(`${API_BASE}/api/users`);",
" return res.data.data;",
"}",
"",
"async function getUser(id) {",
" const res = await axios.get(`${API_BASE}/api/users/${id}`);",
" // Frontend expects: { success: true, data: { id, name, email, role } }",
" // But gets: { success: true, data: { user: { record: {...} } } }",
" return res.data.data;",
"}",
"",
"async function main() {",
" console.log('--- List Users ---');",
" const users = await listUsers();",
" console.log(users);",
"",
" console.log('\\n--- Get User 1 ---');",
" const user = await getUser(1);",
" console.log('User name:', user.name); // undefined! Bug: extra nesting",
" console.log('User email:', user.email); // undefined!",
" console.log('Raw response:', JSON.stringify(user));",
"",
" console.log('\\n--- Get User 999 (not found) ---');",
" const missing = await getUser(999);",
" console.log('Missing user:', JSON.stringify(missing)); // No 404, gets 200 with undefined",
"}",
"",
"main().catch(console.error);",
"",
];
return lines.join("\n");
}
export function demoPackageJson(name, deps) {
const pkg = {
name,
version: "1.0.0",
private: true,
dependencies: deps,
};
return JSON.stringify(pkg, null, 2) + "\n";
}
export function demoBackendClaudeMd() {
return `# Cogent Backend - API Server
You are the Backend development team working on an Express.js API server.
## Project
- Express.js REST API running on port 3001
- Entry point: server.js
${claudeMdBridgeSection("backend", "Cogent_Backend", "frontend")}
### Bug Fix Protocol
- When you receive a bug report, read server.js and investigate
- Fix the issue, then notify the reporter with details of what changed
- Include file names and line numbers in your response
- After fixing, tell the other peer exactly how to test the fix
`;
}
export function demoFrontendClaudeMd() {
return `# Cogent Frontend - Web Client
You are the Frontend development team consuming the Backend API.
## Project
- Node.js client consuming REST API at http://localhost:3001
- Entry point: app.js
${claudeMdBridgeSection("frontend", "Cogent_Frontend", "backend")}
### Bug Reporting Protocol
- When reporting bugs, include: endpoint, expected behavior, actual behavior, and reproduction steps
- When confirming fixes, actually run \`node app.js\` and report results
- If the fix works, confirm success. If not, describe what's still broken.
`;
}
//# sourceMappingURL=templates.js.map