File size: 11,086 Bytes
f0634fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | import { beforeEach, describe, expect, it } from 'vitest';
import { z } from 'zod';
import {
type CollectionToken,
type CollectionView,
} from '#/_base/di/collection';
import { ScopeUnits } from '#/_base/di/fiber';
import { createDecorator, ScopeActivation } from '#/_base/di/instantiation';
import { type InstantiationService } from '#/_base/di/instantiationService';
import {
_clearScopedRegistryForTests,
getScopedServiceDescriptors,
registerScopedService,
type Scope,
} from '#/_base/di/scope';
import { Service } from '#/_base/di/service';
import { createScopedTestHost } from '#/_base/di/test';
import { AgentProfileContribution } from '#/app/agentProfileCatalog/agentProfileContribution';
import { ConfigSectionContribution } from '#/app/config/configSectionContributions';
import { IFeatureManager } from '#/app/feature/featureManager';
import { FeatureManagerService } from '#/app/feature/featureManagerService';
import { LifecycleScope } from '#/app/scopes';
import { AgentToolContribution } from '#/agent/toolRegistry/toolContribution';
import { Feature } from '#/features/feature';
import { IFeatureAssemblyService } from '#/features/featureAssembly';
import { FeatureAssemblyService } from '#/features/featureAssemblyService';
import {
AgentModel,
AgentModelContribution,
defineAgentModel,
SessionModelContribution,
type SessionModelDefinition,
} from '#/state/agentModel';
import {
_clearFeatureRecipesForTests,
registerFeature,
} from '#/features/featureRegistry';
import type { AgentTool, ToolExecution } from '#/tool/toolContract';
interface IGreeter {
readonly _serviceBrand: undefined;
greet(): string;
}
const IGreeter = createDecorator<IGreeter>('test-feature-greeter');
class GreeterService extends Service implements IGreeter {
declare readonly _serviceBrand: undefined;
greet(): string {
return 'hi';
}
}
interface ITestTool extends AgentTool {}
const ITestTool = createDecorator<ITestTool>('test-feature-tool');
class TestTool implements ITestTool {
declare readonly _serviceBrand: undefined;
readonly name = 'TestTool';
readonly description = 'test tool';
readonly parameters = {};
resolveExecution(): ToolExecution {
return {
approvalRule: this.name,
execute: async () => ({ output: '' }),
};
}
}
const TestConfigSchema = {
'~standard': {
validate: (value: unknown) => ({ value }),
},
} as never;
function collectionViewOf<T>(scope: Scope, token: CollectionToken<T>): CollectionView<T> {
return (scope.instantiation as InstantiationService).fiberHost.collectionView(token);
}
describe('Feature — built-in capability assembly (src/features)', () => {
beforeEach(() => {
_clearScopedRegistryForTests();
_clearFeatureRecipesForTests();
registerScopedService(
LifecycleScope.App,
IFeatureManager,
FeatureManagerService,
ScopeActivation.OnScopeCreated,
'feature',
);
registerScopedService(
LifecycleScope.App,
IFeatureAssemblyService,
FeatureAssemblyService,
ScopeActivation.OnScopeCreated,
'features',
);
});
it('assembles a registered feature and materializes its contributions per Agent scope', async () => {
const disposed: string[] = [];
class TestFeature extends Feature {
static override readonly name = 'test-feature';
constructor() {
super();
this.contributeConfig('testFeatureSection', TestConfigSchema, { defaultValue: false });
this.contributeAgentService(IGreeter, GreeterService);
this.contributeTool(ITestTool, TestTool, { name: 'TestTool' });
this.contributeProfiles([{ name: 'test-profile' } as never]);
this.onDispose(() => disposed.push('test-feature'));
}
}
registerFeature(TestFeature);
const host = createScopedTestHost();
const manager = host.app.accessor.get(IFeatureManager);
expect(manager.units()).toHaveLength(1);
expect(manager.units()[0]!.name).toBe('test-feature');
const configView = collectionViewOf(host.app, ConfigSectionContribution);
expect(configView.items.map((item) => item.domain)).toContain('testFeatureSection');
const profileView = collectionViewOf(host.app, AgentProfileContribution);
expect(profileView.items).toHaveLength(1);
expect(profileView.items[0]!.sourceId).toBe('feature:test-feature');
const agentOne = host.child(LifecycleScope.Agent, 'agent-1');
const agentTwo = host.child(LifecycleScope.Agent, 'agent-2');
expect(agentOne.accessor.get(IGreeter).greet()).toBe('hi');
expect(agentTwo.accessor.get(IGreeter).greet()).toBe('hi');
expect(agentOne.accessor.get(IGreeter)).not.toBe(agentTwo.accessor.get(IGreeter));
const toolView = collectionViewOf(agentOne, AgentToolContribution);
expect(toolView.items).toHaveLength(1);
expect(toolView.items[0]!.options.name).toBe('TestTool');
expect(agentOne.accessor.get(ITestTool).name).toBe('TestTool');
await manager.unprovideUnit('test-feature');
await host.app.instantiation.cascade.whenIdle();
await new Promise((resolve) => setTimeout(resolve, 0));
expect(manager.units()).toHaveLength(0);
expect(disposed).toEqual(['test-feature']);
expect(configView.items.map((item) => item.domain)).not.toContain('testFeatureSection');
expect(profileView.items).toHaveLength(0);
expect(toolView.items).toHaveLength(0);
expect(() => agentOne.accessor.get(IGreeter)).toThrow();
expect(() => agentOne.accessor.get(ITestTool)).toThrow();
host.dispose();
});
it('registers model definitions and retracts them on unload', async () => {
const sessionModel: SessionModelDefinition<number> = {
id: 'test-feature.session-model',
state: { initial: () => 0, schema: z.custom<number>() },
events: [],
undoable: false,
};
const agentModel = defineAgentModel({
id: 'test-feature.agent-model',
model: class extends AgentModel<number> {},
state: { initial: () => 0, schema: z.custom<number>() },
events: [],
});
class DomainFeature extends Feature {
static override readonly name = 'domain-definitions';
constructor() {
super();
this.contributeSessionModel(sessionModel);
this.contributeAgentModel(agentModel);
}
}
class ReplacementFeature extends Feature {
static override readonly name = 'replacement-definitions';
constructor() {
super();
this.contributeAgentModel(agentModel);
}
}
registerFeature(DomainFeature);
const host = createScopedTestHost();
const manager = host.app.accessor.get(IFeatureManager);
const views = [
collectionViewOf(host.app, SessionModelContribution),
collectionViewOf(host.app, AgentModelContribution),
];
expect(views.map((view) => view.items)).toEqual([
[sessionModel],
[agentModel],
]);
expect(() => manager.provideUnit(ReplacementFeature)).toThrow(
"Agent model 'test-feature.agent-model' already has an active provider",
);
await manager.unprovideUnit('domain-definitions');
await host.app.instantiation.cascade.whenIdle();
expect(views.every((view) => view.items.length === 0)).toBe(true);
expect(() => manager.provideUnit(ReplacementFeature)).not.toThrow();
host.dispose();
});
it('rejects duplicate service contributions until the provider unloads', async () => {
class FirstFeature extends Feature {
static override readonly name = 'first-feature';
constructor() {
super();
this.contributeAgentService(IGreeter, GreeterService);
}
}
class SecondFeature extends Feature {
static override readonly name = 'second-feature';
constructor() {
super();
this.contributeAgentService(IGreeter, GreeterService);
}
}
registerFeature(FirstFeature);
const host = createScopedTestHost();
const manager = host.app.accessor.get(IFeatureManager);
const agent = host.child(LifecycleScope.Agent, 'agent-1');
const original = agent.accessor.get(IGreeter);
expect(() => manager.provideUnit(SecondFeature)).toThrow(
/Service test-feature-greeter is already contributed at scope agent/,
);
expect(manager.units().map((unit) => unit.name)).toEqual(['first-feature']);
expect(
manager
.contributedServices()
.filter((entry) => entry.scope === LifecycleScope.Agent && entry.id === IGreeter),
).toHaveLength(1);
expect(collectionViewOf(host.app, ScopeUnits(LifecycleScope.Agent)).items).toHaveLength(1);
expect(agent.accessor.get(IGreeter)).toBe(original);
await manager.unprovideUnit('first-feature');
await host.app.instantiation.cascade.whenIdle();
expect(() => manager.provideUnit(SecondFeature)).not.toThrow();
expect(manager.units().map((unit) => unit.name)).toEqual(['second-feature']);
expect(agent.accessor.get(IGreeter).greet()).toBe('hi');
host.dispose();
});
it('isolates equal service contributions between App roots', async () => {
class SharedFeature extends Feature {
static override readonly name = 'shared-feature';
constructor() {
super();
this.contributeAgentService(IGreeter, GreeterService);
}
}
registerFeature(SharedFeature);
const first = createScopedTestHost();
const second = createScopedTestHost();
const firstManager = first.app.accessor.get(IFeatureManager);
const secondManager = second.app.accessor.get(IFeatureManager);
const firstAgent = first.child(LifecycleScope.Agent, 'agent-1');
const secondAgent = second.child(LifecycleScope.Agent, 'agent-1');
expect(firstManager.contributedServices()).toHaveLength(1);
expect(secondManager.contributedServices()).toHaveLength(1);
expect(firstAgent.accessor.get(IGreeter)).not.toBe(secondAgent.accessor.get(IGreeter));
await firstManager.unprovideUnit('shared-feature');
await first.app.instantiation.cascade.whenIdle();
expect(firstManager.contributedServices()).toHaveLength(0);
expect(secondManager.contributedServices()).toHaveLength(1);
expect(() => firstAgent.accessor.get(IGreeter)).toThrow();
expect(secondAgent.accessor.get(IGreeter).greet()).toBe('hi');
first.dispose();
second.dispose();
});
it('materializes a per-scope class recipe contributed through contribute()', () => {
class SoloAgentUnit extends Service {
static override readonly name = 'solo-feature/agent';
constructor() {
super();
this.provide(IGreeter, GreeterService);
}
}
class SoloFeature extends Feature {
static override readonly name = 'solo-feature';
constructor() {
super();
this.contribute(ScopeUnits(LifecycleScope.Agent), SoloAgentUnit);
}
}
registerFeature(SoloFeature);
const host = createScopedTestHost();
const agent = host.child(LifecycleScope.Agent, 'agent-1');
expect(agent.accessor.get(IGreeter).greet()).toBe('hi');
host.dispose();
});
});
|