Skip to content
Open
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
52 changes: 46 additions & 6 deletions apps/server/src/services/EventRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import {
type InferenceProvider,
type InferenceRequest,
type InferenceResponse,
type InferenceToolDescriptor,
type LocalInferenceDriver,
type KvCacheRef,
type KvCacheScope,
Expand Down Expand Up @@ -778,12 +779,26 @@ class EventRuntimeService {
private createCanonicalReActAgentRuntime(): ReActAgentRuntime {
return {
async reason(context) {
const toolRefs = context.agent.toolRefs ?? [];
const tools: InferenceToolDescriptor[] = toolRefs
.map((toolRef) => {
const descriptor = getToolManager().describeTool(toolRef);
if (!descriptor) return null;
return {
id: descriptor.id ?? toolRef,
name: descriptor.name ?? toolRef,
description: descriptor.description ?? toolRef,
inputSchema: descriptor.inputSchema as Record<string, unknown>,
} as InferenceToolDescriptor;
})
.filter((tool): tool is InferenceToolDescriptor => tool !== null);
return {
runId: context.runId,
stepId: context.stepId,
sessionId: context.memoryScope?.sessionId,
agentId: context.agent.id,
modelAlias: context.agent.modelAlias,
...(tools.length === 0 ? {} : { tools }),
input: {
instructions: context.agent.systemInstructions,
messages: context.messages,
Expand Down Expand Up @@ -892,6 +907,8 @@ class EventRuntimeService {
const contractSnapshotRef =
request.context.contractSnapshotRef ??
(await this.ensureRunToolSnapshot(request.context.runId));
const contractSnapshot = await this.toolSnapshotStore.get(contractSnapshotRef);
const effectiveCapabilities = contractSnapshot?.effectiveCapabilities;
return this.toolRunner.run({
...request,
toolId,
Expand All @@ -900,6 +917,9 @@ class EventRuntimeService {
userId,
sessionId,
contractSnapshotRef,
...(effectiveCapabilities === undefined
? {}
: { capabilitySnapshotRef: contractSnapshotRef }),
principal: request.context.principal ?? {
id: userId,
principalId: userId,
Expand Down Expand Up @@ -1916,14 +1936,28 @@ class EventRuntimeService {
: [];
const availableToolIds = spec.toolRefs ?? input.options?.tools?.map((tool) => tool.name) ?? [];
const capabilityMetadata = asRecord(spec.metadata);
const allowedMCPServerIds = Array.from(
new Set(
availableToolIds
.map((toolRef) => {
const descriptor = getToolManager().describeTool(toolRef);
return descriptor?.serverId ?? descriptor?.capabilityId?.split('.')[0];
})
.filter((serverId): serverId is string => Boolean(serverId)),
),
);
const effectiveCapabilities = createEffectiveAgentCapabilitySnapshot({
runId: input.runId,
agentId: id,
principalId: userId,
tenantId: stringValue(asRecord(input.metadata)?.tenantId),
domainId: runContext.domainPackId,
agent: capabilityConstraint(capabilityMetadata, availableToolIds, 'agent.policy'),
domain: capabilityConstraint(workflowState, availableToolIds, 'domain.policy'),
agent: capabilityConstraint(capabilityMetadata, availableToolIds, 'agent.policy', {
allowedMCPServerIds,
}),
domain: capabilityConstraint(workflowState, availableToolIds, 'domain.policy', {
allowedMCPServerIds,
}),
activeSkills,
});
this.runCapabilitySnapshots.set(input.runId, effectiveCapabilities);
Expand Down Expand Up @@ -1954,9 +1988,11 @@ class EventRuntimeService {
input.options?.model ??
this.resolveChatModel().model,
systemInstructions,
promptResolution,
...(promptResolution === undefined ? {} : { promptResolution }),
activeSkills,
toolRefs: spec.toolRefs ?? input.options?.tools?.map((tool) => tool.name),
...(spec.toolRefs ?? input.options?.tools?.map((tool) => tool.name)
? { toolRefs: spec.toolRefs ?? input.options?.tools?.map((tool) => tool.name) }
: {}),
};
}

Expand Down Expand Up @@ -4875,7 +4911,8 @@ function stringList(input: unknown): string[] | undefined {
function capabilityConstraint(
source: Record<string, unknown> | undefined,
fallbackToolIds: string[],
defaultPolicyRef: string
defaultPolicyRef: string,
extras: { allowedMCPServerIds?: string[] } = {},
): EffectiveAgentCapabilitySnapshotInput['agent'] {
const memory = stringValue(source?.memoryAccess);
const sideEffect = stringValue(source?.maximumSideEffectLevel);
Expand All @@ -4894,7 +4931,10 @@ function capabilityConstraint(
return {
allowedToolIds:
stringList(source?.allowedToolIds) ?? stringList(source?.allowedTools) ?? fallbackToolIds,
allowedMCPServerIds: stringList(source?.allowedMCPServerIds),
allowedMCPServerIds:
stringList(source?.allowedMCPServerIds)?.length
? (stringList(source?.allowedMCPServerIds) as string[])
: extras.allowedMCPServerIds ?? [],
memoryAccess,
allowedExecutionProfiles: stringList(source?.allowedExecutionProfiles) ?? [],
maximumSideEffectLevel,
Expand Down
8 changes: 4 additions & 4 deletions packages/inference/src/agent-prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ export class AgentPromptRegistry {
templateContentHash: spec.contentHash!,
scope: spec.scope ?? 'global',
trustLevel: spec.trustLevel ?? 'reviewed',
ownerId: spec.ownerId,
tenantId: spec.tenantId,
provenance: spec.provenance,
metadata: spec.metadata,
...(spec.ownerId === undefined ? {} : { ownerId: spec.ownerId }),
...(spec.tenantId === undefined ? {} : { tenantId: spec.tenantId }),
...(spec.provenance === undefined ? {} : { provenance: spec.provenance }),
...(spec.metadata === undefined ? {} : { metadata: spec.metadata }),
});
}
return {
Expand Down
Loading