Update tuning scripts and add task creation to sync_vikunja.js
This commit is contained in:
@@ -4,21 +4,29 @@ const path = require('path');
|
||||
// 1. Get arguments
|
||||
const args = process.argv.slice(2);
|
||||
if (args.length < 2) {
|
||||
console.error("Usage: node sync_vikunja.js <task_id> <message_or_commit>");
|
||||
console.error("Usage:");
|
||||
console.error(" node sync_vikunja.js <task_id> <message> # Update existing task");
|
||||
console.error(" node sync_vikunja.js create \"<title>\" \"<message>\" # Create new task");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const taskId = args[0];
|
||||
const commandOrId = args[0];
|
||||
const message = args[1];
|
||||
|
||||
// 2. Load configuration from .env.agent
|
||||
const envPath = path.join(__dirname, '../config/.env.agent');
|
||||
if (!fs.existsSync(envPath)) {
|
||||
console.error("Error: .agent/config/.env.agent file not found. Please create it from the template.");
|
||||
const envPath = path.join(__dirname, '../../.env.agent');
|
||||
const fallbackEnvPath = path.join(__dirname, '../config/.env.agent');
|
||||
|
||||
let envContent = '';
|
||||
if (fs.existsSync(envPath)) {
|
||||
envContent = fs.readFileSync(envPath, 'utf8');
|
||||
} else if (fs.existsSync(fallbackEnvPath)) {
|
||||
envContent = fs.readFileSync(fallbackEnvPath, 'utf8');
|
||||
} else {
|
||||
console.error("Error: .env.agent file not found.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const envContent = fs.readFileSync(envPath, 'utf8');
|
||||
const env = {};
|
||||
envContent.split('\n').forEach(line => {
|
||||
const match = line.match(/^([^#=]+)="?(.*?)"?$/);
|
||||
@@ -29,6 +37,7 @@ envContent.split('\n').forEach(line => {
|
||||
|
||||
const apiUrl = env.VIKUNJA_API_URL;
|
||||
const apiToken = env.VIKUNJA_API_TOKEN;
|
||||
const projectId = env.VIKUNJA_PROJECT_ID || 14;
|
||||
|
||||
if (!apiUrl || !apiToken || apiUrl.includes('[YOUR_')) {
|
||||
console.error("Error: VIKUNJA_API_URL or VIKUNJA_API_TOKEN is not configured correctly in .env.agent.");
|
||||
@@ -40,52 +49,59 @@ if (env.AGENT_OPERATING_MODE === "TEST") {
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// 3. Helper to make API calls using native fetch (Node 18+)
|
||||
async function markTaskDoneAndComment(taskId, message) {
|
||||
const FETCH_OPTS = {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${apiToken}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
};
|
||||
|
||||
async function createTaskAndComment(title, message) {
|
||||
try {
|
||||
console.log(`Connecting to Vikunja API for Task ${taskId}...`);
|
||||
|
||||
// Update task status to done
|
||||
const patchRes = await fetch(`${apiUrl}/tasks/${taskId}`, {
|
||||
method: 'POST', // Vikunja uses POST to task endpoint for updates
|
||||
headers: {
|
||||
'Authorization': `Bearer ${apiToken}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ done: true })
|
||||
});
|
||||
|
||||
if (!patchRes.ok) {
|
||||
throw new Error(`Failed to mark task as done: ${patchRes.statusText}`);
|
||||
}
|
||||
|
||||
console.log(`✅ Task ${taskId} successfully marked as Done.`);
|
||||
|
||||
// Add comment
|
||||
const commentRes = await fetch(`${apiUrl}/tasks/${taskId}/comments`, {
|
||||
console.log(`Creating new task in Project ${projectId}...`);
|
||||
const createRes = await fetch(`${apiUrl}/projects/${projectId}/tasks`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${apiToken}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
text: `[Agent Automator] Phase completed.\nReason/Hash: ${message}`
|
||||
...FETCH_OPTS,
|
||||
body: JSON.stringify({
|
||||
title: title,
|
||||
description: message,
|
||||
done: true
|
||||
})
|
||||
});
|
||||
|
||||
if (!commentRes.ok) {
|
||||
console.error(`Warning: Task marked as done, but failed to attach comment: ${commentRes.statusText}`);
|
||||
} else {
|
||||
console.log("✅ Comment attached successfully.");
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error("❌ Failed to sync with Vikunja:");
|
||||
// Mask the token if it somehow leaks via error message
|
||||
const secureErr = error.message.replace(new RegExp(apiToken, 'g'), "********");
|
||||
console.error(secureErr);
|
||||
if (!createRes.ok) throw new Error(`Create failed: ${createRes.statusText}`);
|
||||
const task = await createRes.json();
|
||||
console.log(`✅ Task created and marked Done! ID: #${task.id}`);
|
||||
} catch (e) {
|
||||
console.error("❌ Failed:", e.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
markTaskDoneAndComment(taskId, message);
|
||||
async function markTaskDoneAndComment(taskId, message) {
|
||||
try {
|
||||
console.log(`Updating Task ${taskId}...`);
|
||||
const patchRes = await fetch(`${apiUrl}/tasks/${taskId}`, {
|
||||
method: 'POST',
|
||||
...FETCH_OPTS,
|
||||
body: JSON.stringify({ done: true })
|
||||
});
|
||||
|
||||
if (!patchRes.ok) throw new Error(`Update failed: ${patchRes.statusText}`);
|
||||
console.log(`✅ Task ${taskId} marked as Done.`);
|
||||
|
||||
await fetch(`${apiUrl}/tasks/${taskId}/comments`, {
|
||||
method: 'PUT', ...FETCH_OPTS, body: JSON.stringify({ text: `[Agent Automator] Phase completed.\nReason/Hash: ${message}` })
|
||||
});
|
||||
console.log("✅ Comment attached.");
|
||||
} catch (e) {
|
||||
console.error("❌ Failed:", e.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (commandOrId === "create") {
|
||||
createTaskAndComment(message, args[2] || "Task fully completed.");
|
||||
} else {
|
||||
markTaskDoneAndComment(commandOrId, message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user