Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions agents-run-api/src/__tests__/agents/Agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ describe('Agent Integration with SystemPromptBuilder', () => {
usageGuidelines: 'Use this tool when appropriate for the task at hand.',
},
],
policies: [],
dataComponents: [],
artifacts: [],
artifactComponents: [],
Expand All @@ -431,6 +432,7 @@ describe('Agent Integration with SystemPromptBuilder', () => {
expect(systemPromptBuilder.buildSystemPrompt).toHaveBeenCalledWith({
corePrompt: `You are a helpful test agent that can search databases and assist users.`,
prompt: undefined,
policies: [],
tools: [],
dataComponents: [],
artifacts: [],
Expand All @@ -454,6 +456,7 @@ describe('Agent Integration with SystemPromptBuilder', () => {
expect(systemPromptBuilder.buildSystemPrompt).toHaveBeenCalledWith({
corePrompt: `You are a helpful test agent that can search databases and assist users.`,
prompt: undefined,
policies: [],
tools: [],
dataComponents: [],
artifacts: [],
Expand Down Expand Up @@ -489,6 +492,7 @@ describe('Agent Integration with SystemPromptBuilder', () => {
corePrompt: `You are a helpful test agent that can search databases and assist users.`,
prompt: undefined,
tools: [], // Empty tools array since availableTools is undefined
policies: [],
dataComponents: [],
artifacts: [],
artifactComponents: [],
Expand Down
22 changes: 22 additions & 0 deletions agents-run-api/src/__tests__/agents/SystemPromptBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,28 @@ describe('SystemPromptBuilder', () => {
expect(result).toContain('Second tool');
});

test('should include policies section in order when provided', () => {
const config: SystemPromptV1 = {
corePrompt: 'You are a policy-aware assistant.',
tools: [],
dataComponents: [],
artifacts: [],
isThinkingPreparation: false,
policies: [
{ name: 'Second Policy', content: 'Second content', index: 2 },
{ name: 'First Policy', content: 'First content', index: 1 },
],
};

const result = builder.buildSystemPrompt(config);
expect(result).toContain('<policies>');
expect(result).toContain('First Policy');
expect(result).toContain('Second Policy');
expect(result).toContain('First content');
expect(result).toContain('Second content');
expect(result.indexOf('First Policy')).toBeLessThan(result.indexOf('Second Policy'));
});

test('should handle tools with complex parameter schemas', () => {
const mockTool = createMockMcpTool('complex-server', [
{
Expand Down
3 changes: 3 additions & 0 deletions agents-run-api/src/agents/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
ModelFactory,
type ModelSettings,
type Models,
type SubAgentPolicyWithIndex,
parseEmbeddedJson,
type SubAgentStopWhen,
TemplateEngine,
Expand Down Expand Up @@ -133,6 +134,7 @@ export type AgentConfig = {
}>;
contextConfigId?: string;
dataComponents?: DataComponentApiInsert[];
policies?: SubAgentPolicyWithIndex[];
artifactComponents?: ArtifactComponentApiInsert[];
conversationHistoryConfig?: AgentConversationHistoryConfig;
models?: Models;
Expand Down Expand Up @@ -1695,6 +1697,7 @@ export class Agent {
const config: SystemPromptV1 = {
corePrompt: processedPrompt,
prompt,
policies: this.config.policies || [],
tools: toolDefinitions,
dataComponents: componentDataComponents,
artifacts: referenceArtifacts,
Expand Down
16 changes: 16 additions & 0 deletions agents-run-api/src/agents/generateTaskHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getArtifactComponentsForAgent,
getDataComponentsForAgent,
getExternalAgentsForSubAgent,
getPoliciesForSubAgents,
getRelatedAgentsForAgent,
getSubAgentById,
getTeamAgentsForSubAgent,
Expand Down Expand Up @@ -77,6 +78,7 @@ export const createTaskHandler = (
toolsForAgent,
dataComponents,
artifactComponents,
policiesForSubAgents,
] = await Promise.all([
getRelatedAgentsForAgent(dbClient)({
scopes: {
Expand Down Expand Up @@ -126,6 +128,14 @@ export const createTaskHandler = (
subAgentId: config.subAgentId,
},
}),
getPoliciesForSubAgents(dbClient)({
scopes: {
tenantId: config.tenantId,
projectId: config.projectId,
agentId: config.agentId,
},
subAgentIds: [config.subAgentId],
}),
]);

const enhancedInternalRelations = await Promise.all(
Expand Down Expand Up @@ -282,6 +292,11 @@ export const createTaskHandler = (
})
)) ?? [];

const policies =
policiesForSubAgents
?.filter((policy) => policy.subAgentId === config.subAgentId)
.sort((a, b) => (a.index ?? 0) - (b.index ?? 0)) || [];

const agent = new Agent(
{
id: config.subAgentId,
Expand All @@ -296,6 +311,7 @@ export const createTaskHandler = (
prompt,
models: models || undefined,
stopWhen: stopWhen || undefined,
policies,
subAgentRelations: enhancedInternalRelations.map((relation) => ({
id: relation.id,
tenantId: config.tenantId,
Expand Down
11 changes: 11 additions & 0 deletions agents-run-api/src/agents/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,20 @@ export interface VersionConfig<TConfig> {
assemble(templates: Map<string, string>, config: TConfig): string;
}

export interface PolicyData {
id?: string;
subAgentPolicyId?: string;
name: string;
description?: string | null;
content: string;
metadata?: Record<string, unknown> | null;
index?: number;
}

export interface SystemPromptV1 {
corePrompt: string; // Just the agent's prompt string
prompt?: string; // Agent-level context and instructions
policies?: PolicyData[];
artifacts: Artifact[];
tools: ToolData[]; // Support both formats
dataComponents: DataComponentApiInsert[];
Expand Down
34 changes: 33 additions & 1 deletion agents-run-api/src/agents/versions/v1/Phase1Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import artifactTemplate from '../../../../templates/v1/shared/artifact.xml?raw';
import artifactRetrievalGuidance from '../../../../templates/v1/shared/artifact-retrieval-guidance.xml?raw';

import { getLogger } from '../../../logger';
import type { SystemPromptV1, ToolData, VersionConfig } from '../../types';
import type { PolicyData, SystemPromptV1, ToolData, VersionConfig } from '../../types';

const _logger = getLogger('Phase1Config');

Expand Down Expand Up @@ -88,6 +88,9 @@ export class Phase1Config implements VersionConfig<SystemPromptV1> {
const agentContextSection = this.generateAgentContextSection(config.prompt);
systemPrompt = systemPrompt.replace('{{AGENT_CONTEXT_SECTION}}', agentContextSection);

const policiesSection = this.generatePoliciesSection(config.policies);
systemPrompt = systemPrompt.replace('{{POLICIES_SECTION}}', policiesSection);

const rawToolData = this.isToolDataArray(config.tools)
? config.tools
: Phase1Config.convertMcpToolsToToolData(config.tools as McpTool[]);
Expand Down Expand Up @@ -158,6 +161,35 @@ export class Phase1Config implements VersionConfig<SystemPromptV1> {
return thinkingPreparationTemplate;
}

private generatePoliciesSection(policies?: PolicyData[]): string {
if (!policies || policies.length === 0) {
return '';
}

const sortedPolicies = [...policies].sort(
(a, b) => (a.index ?? 0) - (b.index ?? 0)
);

const policyEntries = sortedPolicies
.map((policy) => {
const description = policy.description ? `<description>${policy.description}</description>` : '';
return `
<policy>
<name>${policy.name}</name>
${description}
<content>
${policy.content}
</content>
</policy>`;
})
.join('\n');

return `
<policies>
${policyEntries}
</policies>`;
}

private generateTransferInstructions(hasTransferRelations?: boolean): string {
if (!hasTransferRelations) {
return '';
Expand Down
4 changes: 3 additions & 1 deletion agents-run-api/templates/v1/phase1/system-prompt.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

{{AGENT_CONTEXT_SECTION}}

{{POLICIES_SECTION}}

{{ARTIFACTS_SECTION}}
{{TOOLS_SECTION}}

Expand Down Expand Up @@ -57,4 +59,4 @@
- Cite tool results when applicable
- Maintain conversational flow while being informative
</response_format>
</system_message>
</system_message>
Loading
Loading