агенты: добавить поиск инструментов по каталогу
This commit is contained in:
+192
-20
@@ -14,6 +14,7 @@ function mapAgent(agent) {
|
||||
raw_status: agent.status,
|
||||
operation_count: agent.operation_count || 0,
|
||||
operation_ids: agent.operation_ids || [],
|
||||
tool_selection_policy: agent.tool_selection_policy || { mode: 'direct', groups: [], search: { max_results: 8 } },
|
||||
key_count: agent.key_count || 0,
|
||||
calls_today: agent.calls_today || 0,
|
||||
created_at: agent.created_at,
|
||||
@@ -66,10 +67,18 @@ document.addEventListener('alpine:init', function() {
|
||||
description: '',
|
||||
status: 'published',
|
||||
selectedOps: [],
|
||||
accessMode: 'direct',
|
||||
groups: [],
|
||||
searchMaxResults: 8,
|
||||
},
|
||||
|
||||
opSearch: '',
|
||||
slugManuallyEdited: false,
|
||||
searchPreviewQuery: '',
|
||||
searchPreviewGroup: '',
|
||||
searchPreviewItems: [],
|
||||
searchPreviewLoading: false,
|
||||
searchPreviewRan: false,
|
||||
|
||||
async init() {
|
||||
var self = this;
|
||||
@@ -205,7 +214,7 @@ document.addEventListener('alpine:init', function() {
|
||||
get agentToolFindings() {
|
||||
var findings = [];
|
||||
var selected = this.selectedOperations;
|
||||
if (selected.length > 8) {
|
||||
if (selected.length > 8 && this.form.accessMode === 'direct') {
|
||||
findings.push(this.tKey('agents.drawer.finding.too_many_tools'));
|
||||
}
|
||||
|
||||
@@ -242,13 +251,18 @@ document.addEventListener('alpine:init', function() {
|
||||
description: '',
|
||||
status: 'published',
|
||||
selectedOps: [],
|
||||
accessMode: 'direct',
|
||||
groups: [],
|
||||
searchMaxResults: 8,
|
||||
};
|
||||
this.opSearch = '';
|
||||
this.slugManuallyEdited = false;
|
||||
this.resetSearchPreview();
|
||||
this.drawerOpen = true;
|
||||
},
|
||||
|
||||
openEdit(agent) {
|
||||
var policy = agent.tool_selection_policy || {};
|
||||
this.drawerMode = 'edit';
|
||||
this.editingId = agent.id;
|
||||
this.form = {
|
||||
@@ -257,9 +271,22 @@ document.addEventListener('alpine:init', function() {
|
||||
description: agent.description,
|
||||
status: agent.raw_status || agent.status || 'draft',
|
||||
selectedOps: [].concat(agent.operation_ids || []),
|
||||
accessMode: policy.mode === 'search' ? 'search' : 'direct',
|
||||
groups: (policy.groups || []).map(function(group) {
|
||||
return {
|
||||
id: group.id || '',
|
||||
name: group.name || '',
|
||||
description: group.description || '',
|
||||
tool_names: [].concat(group.tool_names || []),
|
||||
};
|
||||
}),
|
||||
searchMaxResults: policy.search && policy.search.max_results
|
||||
? policy.search.max_results
|
||||
: 8,
|
||||
};
|
||||
this.opSearch = '';
|
||||
this.slugManuallyEdited = true;
|
||||
this.resetSearchPreview();
|
||||
this.drawerOpen = true;
|
||||
},
|
||||
|
||||
@@ -289,13 +316,170 @@ document.addEventListener('alpine:init', function() {
|
||||
this.form.selectedOps.push(operationId);
|
||||
} else {
|
||||
this.form.selectedOps.splice(index, 1);
|
||||
var operation = this.operations.find(function(item) { return item.id === operationId; });
|
||||
if (operation) {
|
||||
this.form.groups.forEach(function(group) {
|
||||
group.tool_names = group.tool_names.filter(function(name) {
|
||||
return name !== operation.name;
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
this.resetSearchPreview();
|
||||
},
|
||||
|
||||
isOpSelected(operationId) {
|
||||
return this.form.selectedOps.includes(operationId);
|
||||
},
|
||||
|
||||
clearSelectedOperations() {
|
||||
this.form.selectedOps = [];
|
||||
this.form.groups.forEach(function(group) { group.tool_names = []; });
|
||||
this.resetSearchPreview();
|
||||
},
|
||||
|
||||
setAccessMode(mode) {
|
||||
this.form.accessMode = mode === 'search' ? 'search' : 'direct';
|
||||
this.resetSearchPreview();
|
||||
},
|
||||
|
||||
addToolGroup() {
|
||||
this.form.groups.push({ id: '', name: '', description: '', tool_names: [] });
|
||||
this.resetSearchPreview();
|
||||
},
|
||||
|
||||
removeToolGroup(index) {
|
||||
this.form.groups.splice(index, 1);
|
||||
this.resetSearchPreview();
|
||||
},
|
||||
|
||||
onToolGroupName(index, value) {
|
||||
var group = this.form.groups[index];
|
||||
if (!group) return;
|
||||
var previousSlug = this.slugifyGroupName(group.name);
|
||||
group.name = value;
|
||||
if (!group.id || group.id === previousSlug) {
|
||||
group.id = this.slugifyGroupName(value);
|
||||
}
|
||||
this.resetSearchPreview();
|
||||
},
|
||||
|
||||
onToolGroupId(index, value) {
|
||||
var group = this.form.groups[index];
|
||||
if (!group) return;
|
||||
group.id = this.slugifyGroupName(value);
|
||||
this.resetSearchPreview();
|
||||
},
|
||||
|
||||
slugifyGroupName(value) {
|
||||
return String(value || '')
|
||||
.toLowerCase()
|
||||
.replace(/\s+/g, '-')
|
||||
.replace(/[^a-z0-9-]/g, '')
|
||||
.replace(/-+/g, '-')
|
||||
.replace(/^-|-$/g, '');
|
||||
},
|
||||
|
||||
toggleToolGroup(groupIndex, toolName) {
|
||||
var group = this.form.groups[groupIndex];
|
||||
if (!group) return;
|
||||
var index = group.tool_names.indexOf(toolName);
|
||||
if (index === -1) group.tool_names.push(toolName);
|
||||
else group.tool_names.splice(index, 1);
|
||||
this.resetSearchPreview();
|
||||
},
|
||||
|
||||
toolInGroup(groupIndex, toolName) {
|
||||
var group = this.form.groups[groupIndex];
|
||||
return Boolean(group && group.tool_names.includes(toolName));
|
||||
},
|
||||
|
||||
toolSelectionPolicy() {
|
||||
return {
|
||||
mode: this.form.accessMode,
|
||||
groups: this.form.accessMode === 'search'
|
||||
? this.form.groups.map(function(group) {
|
||||
return {
|
||||
id: group.id.trim(),
|
||||
name: group.name.trim(),
|
||||
description: group.description.trim(),
|
||||
tool_names: [].concat(group.tool_names || []),
|
||||
};
|
||||
})
|
||||
: [],
|
||||
search: {
|
||||
max_results: Math.max(1, Math.min(20, Number(this.form.searchMaxResults) || 8)),
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
get catalogConfigValid() {
|
||||
if (this.form.accessMode !== 'search') return true;
|
||||
var ids = [];
|
||||
for (var index = 0; index < this.form.groups.length; index += 1) {
|
||||
var group = this.form.groups[index];
|
||||
if (!group.id.trim() || !group.name.trim() || !group.description.trim()) return false;
|
||||
if (ids.includes(group.id.trim())) return false;
|
||||
ids.push(group.id.trim());
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
||||
agentBindings() {
|
||||
var self = this;
|
||||
return this.form.selectedOps.map(function(operationId) {
|
||||
var operation = self.operations.find(function(item) { return item.id === operationId; });
|
||||
return {
|
||||
operation_id: operationId,
|
||||
operation_version: operation && operation.latest_published_version
|
||||
? operation.latest_published_version
|
||||
: operation && operation.current_draft_version
|
||||
? operation.current_draft_version
|
||||
: 1,
|
||||
tool_name: operation ? operation.name : operationId,
|
||||
tool_title: operation ? (operation.display_name || operation.name) : operationId,
|
||||
tool_description_override: null,
|
||||
enabled: true,
|
||||
};
|
||||
});
|
||||
},
|
||||
|
||||
resetSearchPreview() {
|
||||
this.searchPreviewItems = [];
|
||||
this.searchPreviewRan = false;
|
||||
},
|
||||
|
||||
async previewToolSearch() {
|
||||
if (
|
||||
this.searchPreviewLoading
|
||||
|| !this.workspaceId
|
||||
|| !this.searchPreviewQuery.trim()
|
||||
|| this.form.accessMode !== 'search'
|
||||
) return;
|
||||
this.searchPreviewLoading = true;
|
||||
this.searchPreviewRan = false;
|
||||
try {
|
||||
var response = await window.CrankApi.previewAgentToolSearch(this.workspaceId, {
|
||||
query: this.searchPreviewQuery.trim(),
|
||||
group_ids: this.searchPreviewGroup ? [this.searchPreviewGroup] : [],
|
||||
bindings: this.agentBindings(),
|
||||
tool_selection_policy: this.toolSelectionPolicy(),
|
||||
});
|
||||
this.searchPreviewItems = response.items || [];
|
||||
this.searchPreviewRan = true;
|
||||
} catch (error) {
|
||||
this.searchPreviewItems = [];
|
||||
if (window.CrankUi) {
|
||||
window.CrankUi.error(
|
||||
error.message || this.tKey('agents.drawer.search.preview_error'),
|
||||
this.tKey('agents.drawer.search.preview_error_title')
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
this.searchPreviewLoading = false;
|
||||
}
|
||||
},
|
||||
|
||||
operationsLookSimilar(left, right) {
|
||||
var leftTokens = this.operationTokens(left);
|
||||
var rightTokens = this.operationTokens(right);
|
||||
@@ -345,7 +529,7 @@ document.addEventListener('alpine:init', function() {
|
||||
display_name: this.form.display_name,
|
||||
description: this.form.description,
|
||||
instructions: {},
|
||||
tool_selection_policy: {},
|
||||
tool_selection_policy: this.toolSelectionPolicy(),
|
||||
});
|
||||
agentId = created.agent_id;
|
||||
currentVersion = created.version || 1;
|
||||
@@ -359,27 +543,15 @@ document.addEventListener('alpine:init', function() {
|
||||
currentVersion = agent.current_draft_version || 1;
|
||||
}
|
||||
|
||||
await window.CrankApi.saveAgentBindings(
|
||||
var savedVersion = await window.CrankApi.saveAgentBindings(
|
||||
this.workspaceId,
|
||||
agentId,
|
||||
this.form.selectedOps.map(function(operationId) {
|
||||
var operation = self.operations.find(function(item) {
|
||||
return item.id === operationId;
|
||||
});
|
||||
return {
|
||||
operation_id: operationId,
|
||||
operation_version: operation && operation.latest_published_version
|
||||
? operation.latest_published_version
|
||||
: operation && operation.current_draft_version
|
||||
? operation.current_draft_version
|
||||
: 1,
|
||||
tool_name: operation ? operation.name : operationId,
|
||||
tool_title: operation ? (operation.display_name || operation.name) : operationId,
|
||||
tool_description_override: null,
|
||||
enabled: true,
|
||||
};
|
||||
}),
|
||||
{
|
||||
bindings: this.agentBindings(),
|
||||
tool_selection_policy: this.toolSelectionPolicy(),
|
||||
},
|
||||
);
|
||||
currentVersion = savedVersion.version || currentVersion;
|
||||
|
||||
if (this.form.status === 'published') {
|
||||
await window.CrankApi.publishAgent(this.workspaceId, agentId, {
|
||||
|
||||
Reference in New Issue
Block a user