Skip to content

Commit 4ab0976

Browse files
[Agent Builder] agent execution - cleanup (elastic#253052)
## Summary Some cleanup and refactoring around the agent execution's code and APIs - expose agent registry from server-side plugin start contract (as we were doing for tools) - remove `AgentServiceStart.execute` - was a passthrough to `Runner.runAgent` just introducing another layer of dependencies. - improve contract mocks - use own tool prefix for attachment tools - do not show link to tool definition in reasoning panel for "internal" tools (filestore, attachments) - expose execution service from server-side contract - remove skill registration API from start contract (for now) --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
1 parent a1beb31 commit 4ab0976

28 files changed

Lines changed: 178 additions & 142 deletions

File tree

x-pack/platform/packages/shared/agent-builder/agent-builder-common/base/namespaces.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const internalNamespaces = {
1313
platformCore: 'platform.core',
1414
platformDashboard: 'platform.dashboard',
1515
filestore: 'filestore',
16+
attachments: 'attachments',
1617
observability: 'observability',
1718
security: 'security',
1819
} as const;
@@ -22,6 +23,7 @@ export const internalNamespaces = {
2223
*/
2324
export const protectedNamespaces: string[] = [
2425
internalNamespaces.platformCore,
26+
internalNamespaces.attachments,
2527
internalNamespaces.filestore,
2628
internalNamespaces.observability,
2729
internalNamespaces.platformDashboard, // Owned by dashboard_agent plugin

x-pack/platform/packages/shared/agent-builder/agent-builder-common/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export {
1616
type ToolDefinition,
1717
type ToolDefinitionWithSchema,
1818
platformCoreTools,
19+
attachmentTools,
1920
filestoreTools,
2021
defaultAgentToolIds,
2122
editableToolTypes,

x-pack/platform/packages/shared/agent-builder/agent-builder-common/tools/constants.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,32 @@ export const platformCoreTools = {
3030
productDocumentation: platformCoreTool('product_documentation'),
3131
cases: platformCoreTool('cases'),
3232
integrationKnowledge: platformCoreTool('integration_knowledge'),
33-
// Attachment tools
34-
attachmentRead: platformCoreTool('attachment_read'),
35-
attachmentUpdate: platformCoreTool('attachment_update'),
36-
attachmentAdd: platformCoreTool('attachment_add'),
37-
attachmentList: platformCoreTool('attachment_list'),
38-
attachmentDiff: platformCoreTool('attachment_diff'),
3933
} as const;
4034

35+
export const attachmentTools = {
36+
read: `${internalNamespaces.attachments}.read`,
37+
update: `${internalNamespaces.attachments}.update`,
38+
add: `${internalNamespaces.attachments}.add`,
39+
list: `${internalNamespaces.attachments}.list`,
40+
diff: `${internalNamespaces.attachments}.diff`,
41+
};
42+
4143
export const filestoreTools = {
4244
read: `${internalNamespaces.filestore}.read`,
4345
ls: `${internalNamespaces.filestore}.ls`,
4446
grep: `${internalNamespaces.filestore}.grep`,
4547
glob: `${internalNamespaces.filestore}.glob`,
4648
};
4749

50+
export const isAttachmentTool = (toolName: string) =>
51+
Object.values(attachmentTools).includes(toolName);
52+
53+
export const isFilestoreTool = (toolName: string) =>
54+
Object.values(filestoreTools).includes(toolName);
55+
56+
export const isInternalTool = (toolName: string) =>
57+
isAttachmentTool(toolName) || isFilestoreTool(toolName);
58+
4859
/**
4960
* List of tool types which can be created / edited by a user.
5061
*/

x-pack/platform/packages/shared/agent-builder/agent-builder-common/tools/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ export { ToolType, type ToolDefinition, type ToolDefinitionWithSchema } from './
99
export { isReservedToolId, validateToolId, toolIdRegexp, toolIdMaxLength } from './tool_ids';
1010
export {
1111
platformCoreTools,
12+
attachmentTools,
1213
filestoreTools,
1314
activeToolsCountWarningThreshold,
1415
defaultAgentToolIds,
1516
editableToolTypes,
17+
isInternalTool,
18+
isAttachmentTool,
19+
isFilestoreTool,
1620
} from './constants';
1721
export {
1822
type ByIdsToolSelection,

x-pack/platform/plugins/shared/agent_builder/public/application/components/conversations/conversation_rounds/round_thinking/steps/tool_call_display.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
*/
77

88
import type { ToolCallStep } from '@kbn/agent-builder-common/chat/conversation';
9+
import { isInternalTool } from '@kbn/agent-builder-common/tools';
910
import type { ReactNode } from 'react';
1011
import React from 'react';
11-
import { EuiLink, EuiText } from '@elastic/eui';
12+
import { EuiLink, EuiText, EuiCode } from '@elastic/eui';
1213
import { FormattedMessage } from '@kbn/i18n-react';
1314
import { i18n } from '@kbn/i18n';
1415
import { useNavigation } from '../../../../../hooks/use_navigation';
@@ -44,7 +45,9 @@ export const ToolCallDisplay: React.FC<ToolCallDisplayProps> = ({ step, icon, te
4445
id="xpack.agentBuilder.thinking.toolCallThinkingItem"
4546
defaultMessage="Calling tool {tool}"
4647
values={{
47-
tool: (
48+
tool: isInternalTool(toolId) ? (
49+
<EuiCode>{toolId}</EuiCode>
50+
) : (
4851
<code>
4952
<EuiLink
5053
href={toolHref}

x-pack/platform/plugins/shared/agent_builder/server/mocks.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77

88
import type { AgentBuilderPluginSetup, AgentBuilderPluginStart } from './types';
99
import { createMockedExecutableTool, createToolRegistryMock } from './test_utils/tools';
10+
import { createMockedAgentRegistry } from './test_utils/agents';
1011
import { createFormatContextMock } from './test_utils/attachments';
1112
import { createToolHandlerContextMock } from './test_utils/runner';
1213

1314
export type { ToolHandlerContextMock } from './test_utils/runner';
1415

15-
const createSetupContractMock = (): jest.Mocked<AgentBuilderPluginSetup> => {
16+
export type AgentBuilderPluginSetupMock = jest.MockedObjectDeep<AgentBuilderPluginSetup>;
17+
18+
const createSetupContractMock = (): AgentBuilderPluginSetupMock => {
1619
return {
1720
agents: {
1821
register: jest.fn(),
@@ -32,18 +35,21 @@ const createSetupContractMock = (): jest.Mocked<AgentBuilderPluginSetup> => {
3235
};
3336
};
3437

35-
const createStartContractMock = (): jest.Mocked<AgentBuilderPluginStart> => {
38+
export type AgentBuilderPluginStartMock = jest.MockedObjectDeep<AgentBuilderPluginStart>;
39+
40+
const createStartContractMock = (): AgentBuilderPluginStartMock => {
3641
return {
3742
agents: {
3843
runAgent: jest.fn(),
44+
getRegistry: jest.fn().mockImplementation(() => createMockedAgentRegistry()),
3945
},
4046
tools: {
4147
execute: jest.fn(),
4248
getRegistry: jest.fn().mockImplementation(() => createToolRegistryMock()),
4349
},
44-
skills: {
45-
register: jest.fn(),
46-
unregister: jest.fn(),
50+
execution: {
51+
executeAgent: jest.fn(),
52+
getExecution: jest.fn(),
4753
},
4854
};
4955
};

x-pack/platform/plugins/shared/agent_builder/server/plugin.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,23 +169,25 @@ export class AgentBuilderPlugin
169169
analyticsService: this.analyticsService,
170170
});
171171

172-
const { tools, agents, skills, runnerFactory } = startServices;
172+
const { tools, agents, runnerFactory, execution } = startServices;
173173
const runner = runnerFactory.getRunner();
174174

175175
if (this.home) {
176176
registerSampleData(this.home, this.logger);
177177
}
178+
178179
return {
179180
agents: {
180-
runAgent: agents.execute.bind(agents),
181+
getRegistry: ({ request }) => agents.getRegistry({ request }),
182+
runAgent: runner.runAgent.bind(runner),
181183
},
182184
tools: {
183185
getRegistry: ({ request }) => tools.getRegistry({ request }),
184186
execute: runner.runTool.bind(runner),
185187
},
186-
skills: {
187-
register: skills.registerSkill.bind(skills),
188-
unregister: skills.unregisterSkill.bind(skills),
188+
execution: {
189+
executeAgent: execution.executeAgent.bind(execution),
190+
getExecution: execution.getExecution.bind(execution),
189191
},
190192
};
191193
}

x-pack/platform/plugins/shared/agent_builder/server/routes/attachments.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {
1212
ConversationRound,
1313
VersionedAttachment,
1414
} from '@kbn/agent-builder-common';
15-
import { ConversationRoundStatus } from '@kbn/agent-builder-common';
15+
import { ConversationRoundStatus, attachmentTools } from '@kbn/agent-builder-common';
1616
import { ConversationRoundStepType } from '@kbn/agent-builder-common';
1717
import { ToolResultType } from '@kbn/agent-builder-common/tools/tool_result';
1818
import { registerAttachmentRoutes } from './attachments';
@@ -571,7 +571,7 @@ describe('Attachment Routes', () => {
571571
{
572572
type: ConversationRoundStepType.toolCall,
573573
tool_call_id: 'tc-1',
574-
tool_id: 'platform.core.attachment_read',
574+
tool_id: attachmentTools.read,
575575
params: { attachment_id: 'att-1' },
576576
results: [
577577
{

x-pack/platform/plugins/shared/agent_builder/server/routes/attachments.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import path from 'path';
99
import { schema } from '@kbn/config-schema';
1010
import type { ConversationRound, ToolCallStep } from '@kbn/agent-builder-common';
11-
import { isToolCallStep } from '@kbn/agent-builder-common';
11+
import { isToolCallStep, attachmentTools } from '@kbn/agent-builder-common';
1212
import { createAttachmentStateManager } from '@kbn/agent-builder-server/attachments';
1313
import { ATTACHMENT_REF_ACTOR } from '@kbn/agent-builder-common/attachments';
1414
import type { RouteDependencies } from './types';
@@ -32,11 +32,7 @@ function isAttachmentReferencedInRounds(
3232
attachmentId: string,
3333
rounds: ConversationRound[]
3434
): boolean {
35-
const attachmentToolIds = [
36-
'platform.core.attachment_read',
37-
'platform.core.attachment_update',
38-
'platform.core.attachment_diff',
39-
];
35+
const attachmentToolIds = [attachmentTools.read, attachmentTools.update, attachmentTools.diff];
4036

4137
for (const round of rounds) {
4238
for (const step of round.steps) {

x-pack/platform/plugins/shared/agent_builder/server/services/agents/agents_service.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ const createClientMock = createClient as jest.MockedFunction<typeof createClient
3333
const runToolRefCleanupMock = runToolRefCleanup as jest.MockedFunction<typeof runToolRefCleanup>;
3434

3535
const createStartDeps = (): AgentsServiceStartDeps => ({
36-
getRunner: () => ({ runAgent: jest.fn() } as any),
3736
security: securityServiceMock.createStart(),
3837
elasticsearch: elasticsearchServiceMock.createStart(),
3938
uiSettings: uiSettingsServiceMock.createStartContract(),

0 commit comments

Comments
 (0)