feat: agent resources upgrade
This commit is contained in:
1 parent
659b7497fb
commit
ee323fe660
6 files changed
+163
-64
No files matched your search
+25
-10
@@ -36,8 +36,9 @@ class Agent:
|
|||||||
|
|
||||||
# should be in expert init?
|
# should be in expert init?
|
||||||
self.tools = []
|
self.tools = []
|
||||||
|
self.remotes = []
|
||||||
for resource in self.resources:
|
for resource in self.resources:
|
||||||
if resource.kind != 'tool': continue
|
if resource.kind == 'tool':
|
||||||
if resource.value.get('type') == 'native' and 'name' in resource.value:
|
if resource.value.get('type') == 'native' and 'name' in resource.value:
|
||||||
tool_cls = next((t for t in self.app.tool_classes if t.tool_name() == resource.value['name']), None)
|
tool_cls = next((t for t in self.app.tool_classes if t.tool_name() == resource.value['name']), None)
|
||||||
if tool_cls:
|
if tool_cls:
|
||||||
@@ -45,6 +46,15 @@ class Agent:
|
|||||||
spec = tool.get_spec().clone()
|
spec = tool.get_spec().clone()
|
||||||
spec.name = resource.key
|
spec.name = resource.key
|
||||||
self.tools.append(spec)
|
self.tools.append(spec)
|
||||||
|
elif resource.kind == 'remote':
|
||||||
|
value = resource.value or {}
|
||||||
|
if isinstance(value, str):
|
||||||
|
value = {'prefix': value}
|
||||||
|
self.remotes.append({
|
||||||
|
'name': resource.key,
|
||||||
|
'prefix': value.get('prefix'),
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
def _set_basic_info(self, id, name, user_id):
|
def _set_basic_info(self, id, name, user_id):
|
||||||
self.id = id
|
self.id = id
|
||||||
@@ -205,16 +215,21 @@ class Agent:
|
|||||||
if r.kind == kind and (key is None or r.key == key)]
|
if r.kind == kind and (key is None or r.key == key)]
|
||||||
|
|
||||||
def get_tools(self, run=None):
|
def get_tools(self, run=None):
|
||||||
"""Collect the tools available to this agent.
|
remotes = list(self.remotes)
|
||||||
|
|
||||||
Returns the agent-configured tools (derived from its resources) plus
|
|
||||||
any remote tools referenced by the run input. When a run is given,
|
|
||||||
remote names in ``run.input['remotes']`` are resolved and appended.
|
|
||||||
"""
|
|
||||||
tools = list(self.tools)
|
|
||||||
if run is not None and 'remotes' in run.input:
|
if run is not None and 'remotes' in run.input:
|
||||||
tools += self.app.getRemoteTools(self.user_id, run.input['remotes'])
|
remotes += list(run.input['remotes'])
|
||||||
return tools
|
|
||||||
|
tools = list(self.tools) + self.app.getRemoteTools(self.user_id, remotes)
|
||||||
|
|
||||||
|
# Dedupe by tool name (agent-configured vs run-input vs native collisions)
|
||||||
|
seen, result = set(), []
|
||||||
|
for tool in tools:
|
||||||
|
if tool.name in seen:
|
||||||
|
continue
|
||||||
|
seen.add(tool.name)
|
||||||
|
result.append(tool)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
class DebugAgent(Agent):
|
class DebugAgent(Agent):
|
||||||
|
|||||||
@@ -126,13 +126,21 @@ class AgentMixin:
|
|||||||
|
|
||||||
async def save_agent_resource(self, agent_id, kind, key, value):
|
async def save_agent_resource(self, agent_id, kind, key, value):
|
||||||
await db.AgentResource.update_or_create(
|
await db.AgentResource.update_or_create(
|
||||||
|
{'value': value},
|
||||||
agent_id=agent_id,
|
agent_id=agent_id,
|
||||||
kind=kind,
|
kind=kind,
|
||||||
key=key,
|
key=key,
|
||||||
value=value,
|
|
||||||
)
|
)
|
||||||
print(f"Saved agent resource: {agent_id} - {kind} - {key}")
|
print(f"Saved agent resource: {agent_id} - {kind} - {key}")
|
||||||
|
|
||||||
|
async def delete_agent_resource(self, agent_id, kind, key):
|
||||||
|
await db.AgentResource.filter(
|
||||||
|
agent_id=agent_id,
|
||||||
|
kind=kind,
|
||||||
|
key=key,
|
||||||
|
).delete()
|
||||||
|
print(f"Deleted agent resource: {agent_id} - {kind} - {key}")
|
||||||
|
|
||||||
# --- Control & Actions ---
|
# --- Control & Actions ---
|
||||||
|
|
||||||
async def get_agent_control(self, agent_id):
|
async def get_agent_control(self, agent_id):
|
||||||
|
|||||||
@@ -36,9 +36,9 @@ def format_tools(tools):
|
|||||||
if isinstance(t, dict):
|
if isinstance(t, dict):
|
||||||
specs.append(t)
|
specs.append(t)
|
||||||
elif isinstance(t, ToolSpec):
|
elif isinstance(t, ToolSpec):
|
||||||
specs.append(t.format_openai())
|
specs.append(t.to_openai_dict())
|
||||||
elif isinstance(t, Tool):
|
elif isinstance(t, Tool):
|
||||||
specs.append(t.get_spec().format_openai())
|
specs.append(t.get_spec().to_openai_dict())
|
||||||
return specs
|
return specs
|
||||||
|
|
||||||
def format_openai(messages):
|
def format_openai(messages):
|
||||||
|
|||||||
@@ -150,6 +150,10 @@ def register(server, app, auth_manager):
|
|||||||
await app.save_agent_resource(await agent_id_from_body(), body['kind'], body['key'], body['value'])
|
await app.save_agent_resource(await agent_id_from_body(), body['kind'], body['key'], body['value'])
|
||||||
await send({'message': "resource saved", 'nonce': nonce})
|
await send({'message': "resource saved", 'nonce': nonce})
|
||||||
|
|
||||||
|
elif action == "delete-agent-resource":
|
||||||
|
await app.delete_agent_resource(await agent_id_from_body(), body['kind'], body['key'])
|
||||||
|
await send({'message': "resource deleted", 'nonce': nonce})
|
||||||
|
|
||||||
elif action == "get-agent-runs":
|
elif action == "get-agent-runs":
|
||||||
await send({
|
await send({
|
||||||
'runs': await app.get_agent_runs(agent_id=await agent_id_from_body(allow_none=True), user_id=user_id),
|
'runs': await app.get_agent_runs(agent_id=await agent_id_from_body(allow_none=True), user_id=user_id),
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { ref, computed, watch } from 'vue'
|
import { ref, computed, watch } from 'vue'
|
||||||
|
import * as ui from '../ui'
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
/** JSON schema object describing the input structure */
|
/** JSON schema object describing the input structure */
|
||||||
@@ -55,6 +56,17 @@
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function addField(field) {
|
||||||
|
let key = await ui.input("Enter new property name");
|
||||||
|
if (!key) return;
|
||||||
|
|
||||||
|
// Never mutate the field object in place.
|
||||||
|
currentValue.value = {
|
||||||
|
...(currentValue.value || {}),
|
||||||
|
[key]: null
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const computedFields = computed(() => {
|
const computedFields = computed(() => {
|
||||||
let fields = [];
|
let fields = [];
|
||||||
let skip = {};
|
let skip = {};
|
||||||
|
|||||||
+111
-51
@@ -10,6 +10,8 @@
|
|||||||
import * as backend from '../backend';
|
import * as backend from '../backend';
|
||||||
import * as ui from '../ui';
|
import * as ui from '../ui';
|
||||||
|
|
||||||
|
import * as iconoir from '@iconoir/vue';
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
id: {
|
id: {
|
||||||
type: String,
|
type: String,
|
||||||
@@ -32,8 +34,7 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
// UI State
|
// UI State
|
||||||
const addingTool = ref(null);
|
const targetResource = ref(null);
|
||||||
const addingResource = ref(null);
|
|
||||||
|
|
||||||
// === State Management === //
|
// === State Management === //
|
||||||
|
|
||||||
@@ -92,8 +93,7 @@
|
|||||||
|
|
||||||
// Replace with a backend call
|
// Replace with a backend call
|
||||||
backend.sendAction('get-agent-resources', {agentname}).then(res => {
|
backend.sendAction('get-agent-resources', {agentname}).then(res => {
|
||||||
tools.value = res.resources.filter(r => r.kind == 'tool');
|
resources.value = res.resources;
|
||||||
resources.value = res.resources.filter(r => !['tool', 'config'].includes(r.kind));
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,34 +237,66 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function saveAddingTool() {
|
async function updateResourceKey () {
|
||||||
let resource = {
|
let newKey = await ui.input("Enter new resource key");
|
||||||
kind: 'tool',
|
if (!newKey || newKey == targetResource.value.key) return;
|
||||||
key: addingTool.value.name,
|
|
||||||
value: JSON.parse(addingTool.value.value),
|
|
||||||
};
|
|
||||||
addingTool.value = null;
|
|
||||||
|
|
||||||
let res = await backend.sendAction("save-agent-resource", {
|
// Guard collisions, backend won't
|
||||||
agentId: agent.value.id,
|
for (let r of resources.value) {
|
||||||
...resource
|
if (r.kind == targetResource.value.kind && r.key == newKey) {
|
||||||
});
|
ui.alert(`Conflict: ${targetResource.value.kind} '${newKey}'`);
|
||||||
tools.value.push(resource);
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function saveAddingResource() {
|
// Keys can't be updated directly, we move the resource
|
||||||
let resource = {
|
await backend.sendAction("delete-agent-resource", {
|
||||||
kind: addingResource.value.kind,
|
agentId: agent.value.id,
|
||||||
key: addingResource.value.key,
|
...targetResource.value
|
||||||
value: JSON.parse(addingResource.value.value),
|
});
|
||||||
};
|
|
||||||
addingResource.value = null;
|
|
||||||
|
|
||||||
|
targetResource.value.key = newKey;
|
||||||
|
await backend.sendAction("save-agent-resource", {
|
||||||
|
agentId: agent.value.id,
|
||||||
|
...targetResource.value,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function saveResource () {
|
||||||
let res = await backend.sendAction("save-agent-resource", {
|
let res = await backend.sendAction("save-agent-resource", {
|
||||||
agentId: agent.value.id,
|
agentId: agent.value.id,
|
||||||
...resource
|
...targetResource.value
|
||||||
});
|
});
|
||||||
resources.value.push(resource);
|
|
||||||
|
ui.alert(`Saved ${targetResource.value.kind} '${targetResource.value.key}'`);
|
||||||
|
|
||||||
|
if (targetResource.value.isNew) {
|
||||||
|
delete targetResource.value.isNew;
|
||||||
|
resources.value.push(targetResource.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function deleteResource () {
|
||||||
|
if (!await ui.confirm(`Delete ${targetResource.value.kind} '${targetResource.value.key}'?`)) return;
|
||||||
|
|
||||||
|
let res = await backend.sendAction("delete-agent-resource", {
|
||||||
|
agentId: agent.value.id,
|
||||||
|
...targetResource.value
|
||||||
|
});
|
||||||
|
|
||||||
|
let index = resources.value.indexOf(targetResource.value);
|
||||||
|
if (index >= 0) resources.value.splice(index, 1);
|
||||||
|
targetResource.value = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createResource () {
|
||||||
|
let kind = await ui.input("Enter resource kind");
|
||||||
|
if (!kind) return ui.alert('Cancel new resource');
|
||||||
|
|
||||||
|
let key = await ui.input("Enter resource key");
|
||||||
|
if (!kind) return ui.alert('Cancel new resource');
|
||||||
|
|
||||||
|
targetResource.value = {kind, key, value: {}, isNew: true};
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -327,36 +359,31 @@
|
|||||||
<JsonEditor :schema="config.schema" v-model="config.object" />
|
<JsonEditor :schema="config.schema" v-model="config.object" />
|
||||||
<button type="submit" @click="saveConfig()">Save</button>
|
<button type="submit" @click="saveConfig()">Save</button>
|
||||||
</section>
|
</section>
|
||||||
<section>
|
|
||||||
<h3>Tools</h3>
|
|
||||||
<div v-for="(resource, index) in tools">
|
|
||||||
<div class="label">{{ resource.key }}</div>
|
|
||||||
<pre>{{ JSON.stringify(resource.value, null, 2) }}</pre>
|
|
||||||
</div>
|
|
||||||
<section v-if="addingTool">
|
|
||||||
<input v-model="addingTool.name" placeholder="name"><br>
|
|
||||||
<textarea v-model="addingTool.value" placeholder="{}"></textarea>
|
|
||||||
<br>
|
|
||||||
<button @click="saveAddingTool()">Save</button>
|
|
||||||
<button @click="addingTool=null">Cancel</button>
|
|
||||||
</section>
|
|
||||||
<div v-else><button @click="addingTool={}">Add Tool</button></div>
|
|
||||||
</section>
|
|
||||||
<section>
|
<section>
|
||||||
<h3>Resources</h3>
|
<h3>Resources</h3>
|
||||||
<div v-for="resource in resources">
|
<div class="row">
|
||||||
<div class="label">{{ resource.kind }}:{{ resource.key }}</div>
|
<section class="resource-detail column" v-if="targetResource">
|
||||||
<pre>{{ JSON.stringify(resource.value, null, 2) }}</pre>
|
<div>
|
||||||
|
<span class="resource-type">{{ targetResource.kind }}</span>
|
||||||
|
{{ targetResource.key }}
|
||||||
</div>
|
</div>
|
||||||
<section v-if="addingResource">
|
<JsonEditor :schema="{type:'object'}" v-model="targetResource.value" />
|
||||||
<input v-model="addingResource.kind" placeholder="kind/domain"><br>
|
|
||||||
<input v-model="addingResource.key" placeholder="key"><br>
|
|
||||||
<textarea v-model="addingResource.value" placeholder="value"></textarea>
|
|
||||||
<br>
|
|
||||||
<button @click="saveAddingResource()">Save</button>
|
|
||||||
<button @click="addingResource=null">Cancel</button>
|
|
||||||
</section>
|
</section>
|
||||||
<div v-else><button @click="addingResource={}">Add Resource</button></div>
|
<section class="resource-table">
|
||||||
|
<div class="resource-entry" v-for="resource in resources" @click="targetResource=resource">
|
||||||
|
<span class="label">{{ resource.kind }}</span> {{ resource.key }}
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<template v-if="targetResource">
|
||||||
|
<button @click="updateResourceKey">Update Key</button>
|
||||||
|
<button @click="deleteResource">Delete</button>
|
||||||
|
<button @click="saveResource">Save</button>
|
||||||
|
<button @click="targetResource=null">Cancel</button>
|
||||||
|
</template>
|
||||||
|
<button v-else @click="createResource">Add Resource</button>
|
||||||
|
</div>
|
||||||
</section>
|
</section>
|
||||||
</main>
|
</main>
|
||||||
</template>
|
</template>
|
||||||
@@ -398,4 +425,37 @@
|
|||||||
resize: vertical;
|
resize: vertical;
|
||||||
height: 5em;
|
height: 5em;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.resource-table {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(auto-fill, minmax(6em, 1fr));
|
||||||
|
height: fit-content;
|
||||||
|
flex: 1;
|
||||||
|
|
||||||
|
/*align-items: center;
|
||||||
|
gap: 0.5rem;
|
||||||
|
padding: 0.5rem 0.75rem;
|
||||||
|
border-bottom: 1px solid var(--color-border);*/
|
||||||
|
}
|
||||||
|
.resource-entry {
|
||||||
|
border: 1px solid var(--color-border);
|
||||||
|
border-radius: var(--border-radius);
|
||||||
|
background-color: var(--color-surface-strong);
|
||||||
|
padding: .4rem .6rem;
|
||||||
|
width: max-content;
|
||||||
|
}
|
||||||
|
.resource-entry:hover {
|
||||||
|
border-color: var(--color-highlight);
|
||||||
|
}
|
||||||
|
|
||||||
|
.resource-type {
|
||||||
|
font-family: monospace;
|
||||||
|
border: 1px solid var(--color-border);
|
||||||
|
border-radius: 3px;
|
||||||
|
padding: .1rem .3rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* style fix */
|
||||||
|
.row > section { margin-left: 0; }
|
||||||
|
.row > section:last-child { margin-right: 0; }
|
||||||
</style>
|
</style>
|
||||||
Reference in new issue
Block a user