* feat(agent): add MCP integration Signed-off-by: mudler <mudler@localai.io> * Update core/agent/mcp.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Hook MCP Server configuration to creation and setting mask * Allow to specify a bearer token * Small fixups --------- Signed-off-by: mudler <mudler@localai.io> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
879 lines
41 KiB
HTML
879 lines
41 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<title>Agent settings {{.Name}}</title>
|
|
{{template "views/partials/header"}}
|
|
</head>
|
|
<body>
|
|
{{template "views/partials/menu"}}
|
|
|
|
<!-- Toast notification container -->
|
|
<div id="toast" class="toast">
|
|
<span id="toast-message"></span>
|
|
</div>
|
|
|
|
<div class="container">
|
|
<header class="text-center mb-8">
|
|
<h1 class="text-3xl md:text-5xl font-bold">Agent settings - {{.Name}}</h1>
|
|
</header>
|
|
|
|
<div class="max-w-4xl mx-auto">
|
|
<div class="section-box">
|
|
<h2>Agent Control</h2>
|
|
<div class="button-container">
|
|
<button
|
|
class="action-btn toggle-btn"
|
|
data-agent="{{.Name}}"
|
|
data-active="{{.Status}}">
|
|
{{if .Status}}
|
|
<i class="fas fa-pause"></i> Pause Agent
|
|
{{else}}
|
|
<i class="fas fa-play"></i> Start Agent
|
|
{{end}}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- New Agent Configuration Form Section -->
|
|
<div class="section-box">
|
|
<h2>Edit Agent Configuration</h2>
|
|
<form id="edit-agent-form">
|
|
<input type="hidden" name="name" id="name" value="{{.Name}}">
|
|
|
|
<div id="connectorsSection">
|
|
<!-- Connectors will be added here dynamically -->
|
|
</div>
|
|
|
|
<div class="button-container">
|
|
<button type="button" id="addConnectorButton" class="action-btn">
|
|
<i class="fas fa-plus-circle"></i> Add Connector
|
|
</button>
|
|
</div>
|
|
|
|
<div class="mb-4" id="action_box">
|
|
<!-- Actions will be added here dynamically -->
|
|
</div>
|
|
|
|
<div class="button-container">
|
|
<button id="action_button" type="button" class="action-btn">
|
|
<i class="fas fa-plus-circle"></i> Add Action
|
|
</button>
|
|
</div>
|
|
|
|
<div id="mcpSection">
|
|
<!-- Connectors will be added here dynamically -->
|
|
</div>
|
|
|
|
<div class="button-container">
|
|
<button type="button" id="addMCPButton" class="action-btn">
|
|
<i class="fas fa-plus-circle"></i> Add MCP Server
|
|
</button>
|
|
</div>
|
|
|
|
|
|
<div class="mb-4" id="dynamic_box">
|
|
<!-- Dynamic prompts will be added here dynamically -->
|
|
</div>
|
|
|
|
<div class="button-container">
|
|
<button id="dynamic_button" type="button" class="action-btn">
|
|
<i class="fas fa-plus-circle"></i> Add Dynamic Prompt
|
|
</button>
|
|
</div>
|
|
|
|
<div class="mb-4">
|
|
<label for="hud" class="checkbox-label">
|
|
<span class="checkbox-custom">
|
|
<input type="checkbox" name="hud" id="hud">
|
|
<span class="checkmark"></span>
|
|
</span>
|
|
HUD
|
|
</label>
|
|
</div>
|
|
|
|
<div class="mb-4">
|
|
<label for="enable_kb" class="checkbox-label">
|
|
<span class="checkbox-custom">
|
|
<input type="checkbox" name="enable_kb" id="enable_kb">
|
|
<span class="checkmark"></span>
|
|
</span>
|
|
Enable Knowledge Base
|
|
</label>
|
|
</div>
|
|
|
|
<div class="mb-4">
|
|
<label for="enable_reasoning" class="checkbox-label">
|
|
<span class="checkbox-custom">
|
|
<input type="checkbox" name="enable_reasoning" id="enable_reasoning">
|
|
<span class="checkmark"></span>
|
|
</span>
|
|
Enable Reasoning
|
|
</label>
|
|
</div>
|
|
|
|
<div class="mb-4">
|
|
<label for="kb_results">Knowledge Base Results</label>
|
|
<input type="number" name="kb_results" id="kb_results" placeholder="3">
|
|
</div>
|
|
|
|
<div class="mb-4">
|
|
<label for="standalone_job" class="checkbox-label">
|
|
<span class="checkbox-custom">
|
|
<input type="checkbox" name="standalone_job" id="standalone_job">
|
|
<span class="checkmark"></span>
|
|
</span>
|
|
Standalone Job
|
|
</label>
|
|
</div>
|
|
|
|
<div class="mb-4">
|
|
<label for="initiate_conversations" class="checkbox-label">
|
|
<span class="checkbox-custom">
|
|
<input type="checkbox" name="initiate_conversations" id="initiate_conversations">
|
|
<span class="checkmark"></span>
|
|
</span>
|
|
Initiate Conversations
|
|
</label>
|
|
</div>
|
|
|
|
<div class="mb-4">
|
|
<label for="can_stop_itself" class="checkbox-label">
|
|
<span class="checkbox-custom">
|
|
<input type="checkbox" name="can_stop_itself" id="can_stop_itself">
|
|
<span class="checkmark"></span>
|
|
</span>
|
|
Can Stop Itself
|
|
</label>
|
|
</div>
|
|
|
|
<div class="mb-4">
|
|
<label for="random_identity" class="checkbox-label">
|
|
<span class="checkbox-custom">
|
|
<input type="checkbox" name="random_identity" id="random_identity">
|
|
<span class="checkmark"></span>
|
|
</span>
|
|
Random Identity
|
|
</label>
|
|
</div>
|
|
|
|
<div class="mb-4">
|
|
<label for="long_term_memory" class="checkbox-label">
|
|
<span class="checkbox-custom">
|
|
<input type="checkbox" name="long_term_memory" id="long_term_memory">
|
|
<span class="checkmark"></span>
|
|
</span>
|
|
Long Term Memory
|
|
</label>
|
|
</div>
|
|
|
|
<div class="mb-4">
|
|
<label for="summary_long_term_memory" class="checkbox-label">
|
|
<span class="checkbox-custom">
|
|
<input type="checkbox" name="summary_long_term_memory" id="summary_long_term_memory">
|
|
<span class="checkmark"></span>
|
|
</span>
|
|
Long Term Memory (Summarize!)
|
|
</label>
|
|
</div>
|
|
|
|
<div class="mb-4">
|
|
<label for="identity_guidance">Identity Guidance</label>
|
|
<textarea name="identity_guidance" id="identity_guidance" placeholder="Identity Guidance"></textarea>
|
|
</div>
|
|
|
|
<div class="mb-4">
|
|
<label for="periodic_runs">Periodic Runs</label>
|
|
<input type="text" name="periodic_runs" id="periodic_runs" placeholder="Periodic Runs">
|
|
</div>
|
|
|
|
<div class="mb-4">
|
|
<label for="model">Model </label>
|
|
<input type="text" name="model" id="model" placeholder="Model name">
|
|
</div>
|
|
|
|
<div class="mb-4">
|
|
<label for="multimodal_model">Multimodal Model </label>
|
|
<input type="text" name="multimodal_model" id="multimodal_model" placeholder="Model name">
|
|
</div>
|
|
|
|
<div class="mb-4">
|
|
<label for="permanent_goal">Permanent Goal</label>
|
|
<textarea name="permanent_goal" id="permanent_goal" placeholder="Permanent goal"></textarea>
|
|
</div>
|
|
|
|
<div class="mb-4">
|
|
<label for="system_prompt">System Prompt</label>
|
|
<textarea name="system_prompt" id="system_prompt" placeholder="System prompt"></textarea>
|
|
</div>
|
|
|
|
<button type="submit" id="update-button" class="action-btn">
|
|
<i class="fas fa-save"></i> Update Agent
|
|
</button>
|
|
</form>
|
|
</div>
|
|
|
|
<div class="section-box">
|
|
<h2>Export Data</h2>
|
|
<p class="mb-4">Export your agent configuration for backup or transfer.</p>
|
|
<button
|
|
class="action-btn"
|
|
onclick="window.location.href='/settings/export/{{.Name}}'">
|
|
<i class="fas fa-file-export"></i> Export Configuration
|
|
</button>
|
|
</div>
|
|
|
|
<div class="section-box">
|
|
<h2>Danger Zone</h2>
|
|
<p class="mb-4">Permanently delete this agent and all associated data. This action cannot be undone.</p>
|
|
<button
|
|
class="action-btn"
|
|
style="background: linear-gradient(135deg, #ff4545, var(--secondary)); color: white;"
|
|
hx-delete="/delete/{{.Name}}"
|
|
hx-swap="none"
|
|
data-action="delete"
|
|
data-agent="{{.Name}}">
|
|
<i class="fas fa-trash-alt"></i> Delete Agent
|
|
</button>
|
|
</div>
|
|
|
|
<div class="user-info">
|
|
<span>Agent: {{.Name}}</span>
|
|
<span class="timestamp">Last modified: <span id="current-date"></span></span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
const actions = `{{ range .Actions }}<option value="{{.}}">{{.}}</option>{{ end }}`;
|
|
const connectors = `{{ range .Connectors }}<option value="{{.}}">{{.}}</option>{{ end }}`;
|
|
const promptBlocks = `{{ range .PromptBlocks }}<option value="{{.}}">{{.}}</option>{{ end }}`;
|
|
let agentConfig = null;
|
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
// Load agent configuration when page loads
|
|
loadAgentConfig();
|
|
|
|
// Add event listener for delete button
|
|
document.querySelectorAll('[data-action="delete"]').forEach(button => {
|
|
button.addEventListener('htmx:afterRequest', function(event) {
|
|
handleActionResponse(event, this);
|
|
});
|
|
});
|
|
|
|
// Handle toggle button
|
|
const toggleButton = document.querySelector('.toggle-btn');
|
|
if (toggleButton) {
|
|
toggleButton.addEventListener('click', function() {
|
|
const agent = this.getAttribute('data-agent');
|
|
const isActive = this.getAttribute('data-active') === "true";
|
|
const endpoint = isActive ? `/pause/${agent}` : `/start/${agent}`;
|
|
|
|
// Add animation
|
|
this.style.animation = 'pulse 0.5s';
|
|
|
|
// Create a new XMLHttpRequest
|
|
const xhr = new XMLHttpRequest();
|
|
xhr.open('PUT', endpoint);
|
|
xhr.setRequestHeader('Content-Type', 'application/json');
|
|
|
|
xhr.onload = () => {
|
|
// Clear animation
|
|
this.style.animation = '';
|
|
|
|
if (xhr.status === 200) {
|
|
try {
|
|
const response = JSON.parse(xhr.responseText);
|
|
|
|
if (response.status === "ok") {
|
|
// Toggle the button state
|
|
const newState = !isActive;
|
|
this.setAttribute('data-active', newState.toString());
|
|
|
|
// Update button text and icon
|
|
if (newState) {
|
|
this.innerHTML = '<i class="fas fa-pause"></i> Pause Agent';
|
|
} else {
|
|
this.innerHTML = '<i class="fas fa-play"></i> Start Agent';
|
|
}
|
|
|
|
// Show success toast
|
|
const action = isActive ? 'pause' : 'start';
|
|
showToast(`Agent "${agent}" ${action}ed successfully`, 'success');
|
|
|
|
} else if (response.error) {
|
|
// Show error toast
|
|
showToast(`Error: ${response.error}`, 'error');
|
|
}
|
|
} catch (e) {
|
|
// Handle parsing error
|
|
showToast("Invalid response format", 'error');
|
|
console.error("Error parsing response:", e);
|
|
}
|
|
} else {
|
|
// Handle HTTP error
|
|
showToast(`Server error: ${xhr.status}`, 'error');
|
|
}
|
|
};
|
|
|
|
xhr.onerror = () => {
|
|
// Clear animation
|
|
this.style.animation = '';
|
|
showToast("Network error occurred", 'error');
|
|
console.error("Network error occurred");
|
|
};
|
|
|
|
// Send the request
|
|
xhr.send(JSON.stringify({}));
|
|
});
|
|
}
|
|
|
|
// Set current date for timestamp
|
|
const now = new Date();
|
|
document.getElementById('current-date').textContent = now.toISOString().split('T')[0];
|
|
|
|
// Add connector functionality
|
|
document.getElementById('addConnectorButton').addEventListener('click', function() {
|
|
const connectorsSection = document.getElementById('connectorsSection');
|
|
const newConnectorIndex = connectorsSection.getElementsByClassName('connector').length;
|
|
const newConnectorHTML = `
|
|
<div class="connector mb-4 section-box" style="margin-top: 15px; padding: 15px;">
|
|
<h2>Connector ${newConnectorIndex + 1}</h2>
|
|
<div class="mb-4">
|
|
<label for="connectorType${newConnectorIndex}">Connector Type</label>
|
|
<select name="connectors[${newConnectorIndex}].type" id="connectorType${newConnectorIndex}">
|
|
${connectors}
|
|
</select>
|
|
</div>
|
|
<div class="mb-4">
|
|
<label for="connectorConfig${newConnectorIndex}">Connector Config (JSON)</label>
|
|
<textarea id="connectorConfig${newConnectorIndex}" name="connectors[${newConnectorIndex}].config" placeholder='{"token":"sk-mg3.."}'>{}</textarea>
|
|
</div>
|
|
</div>
|
|
`;
|
|
|
|
connectorsSection.insertAdjacentHTML('beforeend', newConnectorHTML);
|
|
});
|
|
|
|
// Add MCP functionality
|
|
document.getElementById('addMCPButton').addEventListener('click', function() {
|
|
const mcpSection = document.getElementById('mcpSection');
|
|
const newMCPIndex = mcpSection.getElementsByClassName('mcp_server').length;
|
|
const newMCPHTML = `
|
|
<div class="mcp_server mb-4 section-box" style="margin-top: 15px; padding: 15px;">
|
|
<h2>MCP Server ${newMCPIndex + 1}</h2>
|
|
<div class="mb-4">
|
|
<label for="mcpURL${newMCPIndex}">MCP Server URL</label>
|
|
<input type="text" id="mcpURL${newMCPIndex}" name="mcp_servers[${newMCPIndex}].url" placeholder='https://...'>
|
|
</div>
|
|
<div class="mb-4">
|
|
<label for="mcpToken${newMCPIndex}">Bearer Token</label>
|
|
<input type="text" id="mcpToken${newMCPIndex}" name="mcp_servers[${newMCPIndex}].token" placeholder='Bearer token'>
|
|
</div>
|
|
</div>
|
|
`;
|
|
|
|
mcpSection.insertAdjacentHTML('beforeend', newMCPHTML);
|
|
});
|
|
|
|
|
|
// Add action functionality
|
|
document.getElementById('action_button').addEventListener('click', function() {
|
|
const actionsSection = document.getElementById('action_box');
|
|
const ii = actionsSection.getElementsByClassName('action').length;
|
|
|
|
const newActionHTML = `
|
|
<div class="action mb-4 section-box" style="margin-top: 15px; padding: 15px;">
|
|
<h2>Action ${ii + 1}</h2>
|
|
<div class="mb-4">
|
|
<label for="actionsName${ii}">Action</label>
|
|
<select name="actions[${ii}].name" id="actionsName${ii}">
|
|
${actions}
|
|
</select>
|
|
</div>
|
|
<div class="mb-4">
|
|
<label for="actionsConfig${ii}">Action Config (JSON)</label>
|
|
<textarea id="actionsConfig${ii}" name="actions[${ii}].config" placeholder='{"results":"5"}'>{}</textarea>
|
|
</div>
|
|
</div>
|
|
`;
|
|
|
|
actionsSection.insertAdjacentHTML('beforeend', newActionHTML);
|
|
});
|
|
|
|
// Add dynamic prompt functionality
|
|
document.getElementById('dynamic_button').addEventListener('click', function() {
|
|
const promptsSection = document.getElementById('dynamic_box');
|
|
const ii = promptsSection.getElementsByClassName('promptBlock').length;
|
|
|
|
const newPromptHTML = `
|
|
<div class="promptBlock mb-4 section-box" style="margin-top: 15px; padding: 15px;">
|
|
<h2>Prompt Block ${ii + 1}</h2>
|
|
<div class="mb-4">
|
|
<label for="promptName${ii}">Block Prompt</label>
|
|
<select name="promptblocks[${ii}].name" id="promptName${ii}">
|
|
${promptBlocks}
|
|
</select>
|
|
</div>
|
|
<div class="mb-4">
|
|
<label for="promptConfig${ii}">Prompt Config (JSON)</label>
|
|
<textarea id="promptConfig${ii}" name="promptblocks[${ii}].config" placeholder='{"results":"5"}'>{}</textarea>
|
|
</div>
|
|
</div>
|
|
`;
|
|
|
|
promptsSection.insertAdjacentHTML('beforeend', newPromptHTML);
|
|
});
|
|
|
|
// Form submission handling
|
|
const form = document.getElementById('edit-agent-form');
|
|
form.addEventListener('submit', function(e) {
|
|
e.preventDefault();
|
|
|
|
// Show a loading state
|
|
const updateButton = document.getElementById('update-button');
|
|
const originalButtonText = updateButton.innerHTML;
|
|
updateButton.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Updating...';
|
|
updateButton.disabled = true;
|
|
|
|
// Build a structured data object
|
|
const formData = new FormData(form);
|
|
const jsonData = {};
|
|
|
|
// Process basic form fields
|
|
for (const [key, value] of formData.entries()) {
|
|
// Skip the array fields (connectors, actions, promptblocks) as they'll be processed separately
|
|
if (!key.includes('[') && !key.includes('].')) {
|
|
// Handle checkboxes
|
|
if (value === 'on') {
|
|
jsonData[key] = true;
|
|
}
|
|
// Handle numeric fields - specifically kb_results
|
|
else if (key === 'kb_results') {
|
|
// Convert to integer or default to 3 if empty
|
|
jsonData[key] = value ? parseInt(value, 10) : 3;
|
|
|
|
// Check if the parse was successful
|
|
if (isNaN(jsonData[key])) {
|
|
showToast('Knowledge Base Results must be a number', 'error');
|
|
updateButton.innerHTML = originalButtonText;
|
|
updateButton.disabled = false;
|
|
return; // Stop form submission
|
|
}
|
|
}
|
|
// Handle other numeric fields if needed
|
|
else if (key === 'periodic_runs' && value) {
|
|
// Try to parse as number if it looks like one
|
|
const numValue = parseInt(value, 10);
|
|
if (!isNaN(numValue) && String(numValue) === value) {
|
|
jsonData[key] = numValue;
|
|
} else {
|
|
jsonData[key] = value;
|
|
}
|
|
}
|
|
else {
|
|
jsonData[key] = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Process connectors - KEEP CONFIG AS STRING
|
|
const connectors = [];
|
|
const connectorsElements = document.getElementsByClassName('connector');
|
|
for (let i = 0; i < connectorsElements.length; i++) {
|
|
const typeField = document.getElementById(`connectorType${i}`);
|
|
const configField = document.getElementById(`connectorConfig${i}`);
|
|
|
|
if (typeField && configField) {
|
|
try {
|
|
// Validate JSON but send as string
|
|
const configValue = configField.value.trim() || '{}';
|
|
// Parse to validate but don't use the parsed object
|
|
JSON.parse(configValue);
|
|
|
|
connectors.push({
|
|
type: typeField.value,
|
|
config: configValue // Send the raw string, not parsed JSON
|
|
});
|
|
} catch (err) {
|
|
console.error(`Error parsing connector ${i} config:`, err);
|
|
showToast(`Error in connector ${i+1} configuration: Invalid JSON`, 'error');
|
|
|
|
updateButton.innerHTML = originalButtonText;
|
|
updateButton.disabled = false;
|
|
return; // Stop form submission
|
|
}
|
|
}
|
|
}
|
|
jsonData.connectors = connectors;
|
|
|
|
// MCP Servers - updated to handle URL and token
|
|
const mcpServers = [];
|
|
const mcpServerElements = document.getElementsByClassName('mcp_server');
|
|
for (let i = 0; i < mcpServerElements.length; i++) {
|
|
const urlField = document.getElementById(`mcpURL${i}`);
|
|
const tokenField = document.getElementById(`mcpToken${i}`);
|
|
|
|
if (urlField && urlField.value.trim()) {
|
|
mcpServers.push({
|
|
url: urlField.value.trim(),
|
|
token: tokenField ? tokenField.value.trim() : ''
|
|
});
|
|
}
|
|
}
|
|
jsonData.mcp_servers = mcpServers;
|
|
|
|
// Process actions - KEEP CONFIG AS STRING
|
|
const actions = [];
|
|
const actionElements = document.getElementsByClassName('action');
|
|
for (let i = 0; i < actionElements.length; i++) {
|
|
const nameField = document.getElementById(`actionsName${i}`);
|
|
const configField = document.getElementById(`actionsConfig${i}`);
|
|
|
|
if (nameField && configField) {
|
|
try {
|
|
// Validate JSON but send as string
|
|
const configValue = configField.value.trim() || '{}';
|
|
// Parse to validate but don't use the parsed object
|
|
JSON.parse(configValue);
|
|
|
|
actions.push({
|
|
name: nameField.value,
|
|
config: configValue // Send the raw string, not parsed JSON
|
|
});
|
|
} catch (err) {
|
|
console.error(`Error parsing action ${i} config:`, err);
|
|
showToast(`Error in action ${i+1} configuration: Invalid JSON`, 'error');
|
|
|
|
updateButton.innerHTML = originalButtonText;
|
|
updateButton.disabled = false;
|
|
return; // Stop form submission
|
|
}
|
|
}
|
|
}
|
|
jsonData.actions = actions;
|
|
|
|
// Process prompt blocks - KEEP CONFIG AS STRING
|
|
const promptBlocks = [];
|
|
const promptBlockElements = document.getElementsByClassName('promptBlock');
|
|
for (let i = 0; i < promptBlockElements.length; i++) {
|
|
const nameField = document.getElementById(`promptName${i}`);
|
|
const configField = document.getElementById(`promptConfig${i}`);
|
|
|
|
if (nameField && configField) {
|
|
try {
|
|
// Validate JSON but send as string
|
|
const configValue = configField.value.trim() || '{}';
|
|
// Parse to validate but don't use the parsed object
|
|
JSON.parse(configValue);
|
|
|
|
promptBlocks.push({
|
|
name: nameField.value,
|
|
config: configValue // Send the raw string, not parsed JSON
|
|
});
|
|
} catch (err) {
|
|
console.error(`Error parsing prompt block ${i} config:`, err);
|
|
showToast(`Error in prompt block ${i+1} configuration: Invalid JSON`, 'error');
|
|
|
|
updateButton.innerHTML = originalButtonText;
|
|
updateButton.disabled = false;
|
|
return; // Stop form submission
|
|
}
|
|
}
|
|
}
|
|
jsonData.promptblocks = promptBlocks;
|
|
|
|
console.log('Sending data:', jsonData);
|
|
|
|
// Send the structured data as JSON
|
|
fetch(`/api/agent/${jsonData.name}/config`, {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(jsonData)
|
|
})
|
|
.then(response => {
|
|
if (!response.ok) {
|
|
return response.json().then(err => {
|
|
throw new Error(err.error || `Server error: ${response.status}`);
|
|
});
|
|
}
|
|
return response.json();
|
|
})
|
|
.then(data => {
|
|
// Restore button state
|
|
updateButton.innerHTML = originalButtonText;
|
|
updateButton.disabled = false;
|
|
|
|
if (data.status === "ok") {
|
|
// Show success toast
|
|
showToast('Agent updated successfully!', 'success');
|
|
|
|
// Reload agent config to get updated values
|
|
setTimeout(() => {
|
|
loadAgentConfig();
|
|
}, 500);
|
|
} else if (data.error) {
|
|
// Show error toast
|
|
showToast('Error: ' + data.error, 'error');
|
|
} else {
|
|
// Handle unexpected response format
|
|
showToast('Unexpected response format', 'error');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
// Handle network or other errors
|
|
showToast('Error: ' + error.message, 'error');
|
|
console.error('Update error:', error);
|
|
|
|
// Restore button state
|
|
updateButton.innerHTML = originalButtonText;
|
|
updateButton.disabled = false;
|
|
});
|
|
});
|
|
});
|
|
|
|
// Function to handle API responses for delete action
|
|
function handleActionResponse(event, button) {
|
|
const xhr = event.detail.xhr;
|
|
const action = button.getAttribute('data-action');
|
|
const agent = button.getAttribute('data-agent');
|
|
|
|
if (xhr.status === 200) {
|
|
try {
|
|
const response = JSON.parse(xhr.responseText);
|
|
|
|
if (response.status === "ok") {
|
|
// Action successful
|
|
let message = "";
|
|
|
|
switch(action) {
|
|
case 'delete':
|
|
message = `Agent "${agent}" deleted successfully`;
|
|
// Redirect to agent list page after short delay for delete
|
|
setTimeout(() => {
|
|
window.location.href = "/agents";
|
|
}, 2000);
|
|
break;
|
|
default:
|
|
message = "Operation completed successfully";
|
|
}
|
|
|
|
// Show success message
|
|
showToast(message, 'success');
|
|
|
|
} else if (response.error) {
|
|
// Show error message
|
|
showToast(`Error: ${response.error}`, 'error');
|
|
}
|
|
} catch (e) {
|
|
// Handle JSON parsing error
|
|
showToast("Invalid response format", 'error');
|
|
}
|
|
} else {
|
|
// Handle HTTP error
|
|
showToast(`Server error: ${xhr.status}`, 'error');
|
|
}
|
|
}
|
|
|
|
// Load agent configuration from server
|
|
function loadAgentConfig() {
|
|
const agentName = document.getElementById('name').value;
|
|
fetch(`/api/agent/${agentName}/config`)
|
|
.then(response => {
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to load agent config: ${response.status}`);
|
|
}
|
|
return response.json();
|
|
})
|
|
.then(data => {
|
|
agentConfig = data;
|
|
populateFormWithConfig(data);
|
|
showToast('Agent configuration loaded', 'success');
|
|
})
|
|
.catch(error => {
|
|
console.error('Error loading agent config:', error);
|
|
showToast('Error loading agent configuration: ' + error.message, 'error');
|
|
});
|
|
}
|
|
|
|
|
|
function populateFormWithConfig(config) {
|
|
// Clear existing dynamic sections
|
|
document.getElementById('connectorsSection').innerHTML = '';
|
|
document.getElementById('mcpSection').innerHTML = '';
|
|
document.getElementById('action_box').innerHTML = '';
|
|
document.getElementById('dynamic_box').innerHTML = '';
|
|
|
|
// Populate simple fields
|
|
document.getElementById('hud').checked = config.hud || false;
|
|
document.getElementById('enable_kb').checked = config.enable_kb || false;
|
|
document.getElementById('enable_reasoning').checked = config.enable_reasoning || false;
|
|
document.getElementById('kb_results').value = config.kb_results || '';
|
|
document.getElementById('standalone_job').checked = config.standalone_job || false;
|
|
document.getElementById('initiate_conversations').checked = config.initiate_conversations || false;
|
|
document.getElementById('can_stop_itself').checked = config.can_stop_itself || false;
|
|
document.getElementById('random_identity').checked = config.random_identity || false;
|
|
document.getElementById('long_term_memory').checked = config.long_term_memory || false;
|
|
document.getElementById('summary_long_term_memory').checked = config.summary_long_term_memory || false;
|
|
document.getElementById('identity_guidance').value = config.identity_guidance || '';
|
|
document.getElementById('periodic_runs').value = config.periodic_runs || '';
|
|
document.getElementById('model').value = config.model || '';
|
|
document.getElementById('multimodal_model').value = config.multimodal_model || '';
|
|
document.getElementById('permanent_goal').value = config.permanent_goal || '';
|
|
document.getElementById('system_prompt').value = config.system_prompt || '';
|
|
|
|
// Populate connectors
|
|
if (config.connectors && Array.isArray(config.connectors)) {
|
|
config.connectors.forEach((connector, index) => {
|
|
// Add connector section
|
|
document.getElementById('addConnectorButton').click();
|
|
|
|
// Find the added connector elements
|
|
const connectorType = document.getElementById(`connectorType${index}`);
|
|
const connectorConfig = document.getElementById(`connectorConfig${index}`);
|
|
|
|
// Set values
|
|
if (connectorType) {
|
|
setSelectValue(connectorType, connector.type);
|
|
}
|
|
|
|
if (connectorConfig) {
|
|
// Format the config value
|
|
formatConfigValue(connectorConfig, connector.config);
|
|
}
|
|
});
|
|
}
|
|
|
|
// Populate MCP servers
|
|
if (config.mcp_servers && Array.isArray(config.mcp_servers)) {
|
|
config.mcp_servers.forEach((server, index) => {
|
|
// Add MCP server section
|
|
document.getElementById('addMCPButton').click();
|
|
|
|
// Find the added MCP server elements
|
|
const mcpURL = document.getElementById(`mcpURL${index}`);
|
|
const mcpToken = document.getElementById(`mcpToken${index}`);
|
|
|
|
// Set values
|
|
if (mcpURL) {
|
|
// If server is a string (old format), use it as URL
|
|
if (typeof server === 'string') {
|
|
mcpURL.value = server;
|
|
}
|
|
// If server is an object (new format), extract URL
|
|
else if (typeof server === 'object' && server !== null) {
|
|
mcpURL.value = server.url || '';
|
|
|
|
if (mcpToken && server.token) {
|
|
mcpToken.value = server.token;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
// Populate actions
|
|
if (config.actions && Array.isArray(config.actions)) {
|
|
config.actions.forEach((action, index) => {
|
|
// Add action section
|
|
document.getElementById('action_button').click();
|
|
|
|
// Find the added action elements
|
|
const actionName = document.getElementById(`actionsName${index}`);
|
|
const actionConfig = document.getElementById(`actionsConfig${index}`);
|
|
|
|
// Set values
|
|
if (actionName) {
|
|
setSelectValue(actionName, action.name);
|
|
}
|
|
|
|
if (actionConfig) {
|
|
// Format the config value
|
|
formatConfigValue(actionConfig, action.config);
|
|
}
|
|
});
|
|
}
|
|
|
|
// Populate prompt blocks
|
|
if (config.promptblocks && Array.isArray(config.promptblocks)) {
|
|
config.promptblocks.forEach((block, index) => {
|
|
// Add prompt block section
|
|
document.getElementById('dynamic_button').click();
|
|
|
|
// Find the added prompt block elements
|
|
const promptName = document.getElementById(`promptName${index}`);
|
|
const promptConfig = document.getElementById(`promptConfig${index}`);
|
|
|
|
// Set values
|
|
if (promptName) {
|
|
setSelectValue(promptName, block.name);
|
|
}
|
|
|
|
if (promptConfig) {
|
|
// Format the config value
|
|
formatConfigValue(promptConfig, block.config);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
// Helper function to format config values
|
|
function formatConfigValue(configElement, configValue) {
|
|
// If it's a string (already stringified JSON), try to parse it first
|
|
if (typeof configValue === 'string') {
|
|
try {
|
|
const parsed = JSON.parse(configValue);
|
|
configElement.value = JSON.stringify(parsed, null, 2);
|
|
} catch (e) {
|
|
console.warn("Failed to parse config JSON string:", e);
|
|
configElement.value = configValue; // Keep as is if parsing fails
|
|
}
|
|
} else if (configValue !== undefined && configValue !== null) {
|
|
// Direct object, just stringify with formatting
|
|
configElement.value = JSON.stringify(configValue, null, 2);
|
|
} else {
|
|
// Default to empty object
|
|
configElement.value = '{}';
|
|
}
|
|
}
|
|
|
|
// Helper function to set select value (with fallback if option doesn't exist)
|
|
function setSelectValue(selectElement, value) {
|
|
// Check if the option exists
|
|
const optionExists = Array.from(selectElement.options).some(option => option.value === value);
|
|
|
|
if (optionExists) {
|
|
selectElement.value = value;
|
|
} else if (value) {
|
|
// If value is provided but option doesn't exist, create a new option
|
|
const newOption = document.createElement('option');
|
|
newOption.value = value;
|
|
newOption.text = value + ' (custom)';
|
|
selectElement.add(newOption);
|
|
selectElement.value = value;
|
|
}
|
|
}
|
|
|
|
</script>
|
|
|
|
<style>
|
|
@keyframes pulse {
|
|
0% { transform: scale(1); }
|
|
50% { transform: scale(1.05); }
|
|
100% { transform: scale(1); }
|
|
}
|
|
|
|
.checkbox-label {
|
|
display: flex;
|
|
align-items: center;
|
|
cursor: pointer;
|
|
margin-bottom: 10px;
|
|
}
|
|
|
|
.checkbox-label .checkbox-custom {
|
|
margin-right: 10px;
|
|
}
|
|
</style>
|
|
</body>
|
|
</html> |