feat: support project copilot customization

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
David Kaya
2026-03-28 17:29:37 +01:00
co-authored by Copilot
parent f53907755a
commit 75b9ff667a
20 changed files with 1354 additions and 16 deletions
@@ -0,0 +1,244 @@
import { describe, expect, mock, test } from 'bun:test';
import type { RunTurnCommand } from '@shared/contracts/sidecar';
import type { PatternDefinition } from '@shared/domain/pattern';
import type { ProjectRecord } from '@shared/domain/project';
import type { SessionRecord } from '@shared/domain/session';
import { createWorkspaceSeed, type WorkspaceState } from '@shared/domain/workspace';
const TIMESTAMP = '2026-03-28T00:00:00.000Z';
mock.module('electron', () => {
const electronMock = {
app: {
isPackaged: false,
getAppPath: () => 'C:\\workspace\\personal\\repositories\\aryx',
getPath: () => 'C:\\workspace\\personal\\repositories\\aryx\\tests\\fixtures',
},
dialog: {
showOpenDialog: async () => ({ canceled: true, filePaths: [] }),
},
shell: {
openPath: async () => '',
},
};
return {
...electronMock,
default: electronMock,
};
});
mock.module('keytar', () => ({
default: {
getPassword: async () => null,
setPassword: async () => undefined,
deletePassword: async () => false,
},
}));
const { AryxAppService } = await import('@main/AryxAppService');
function createProject(overrides?: Partial<ProjectRecord>): ProjectRecord {
return {
id: 'project-alpha',
name: 'alpha',
path: 'C:\\workspace\\alpha',
addedAt: TIMESTAMP,
...overrides,
};
}
function createSession(projectId: string, patternId: string, overrides?: Partial<SessionRecord>): SessionRecord {
return {
id: 'session-alpha',
projectId,
patternId,
title: 'Alpha session',
createdAt: TIMESTAMP,
updatedAt: TIMESTAMP,
status: 'idle',
messages: [],
runs: [],
...overrides,
};
}
function createService(
workspace: WorkspaceState,
pattern: PatternDefinition,
options?: {
captureRunTurn?: (command: RunTurnCommand) => void;
},
): InstanceType<typeof AryxAppService> {
const service = new AryxAppService();
const internals = service as unknown as Record<string, unknown>;
internals.loadWorkspace = async () => workspace;
internals.persistAndBroadcast = async (nextWorkspace: WorkspaceState) => nextWorkspace;
internals.buildEffectivePattern = async () => pattern;
internals.awaitFinalResponseApproval = async () => undefined;
internals.finalizeTurn = () => undefined;
internals.emitSessionEvent = () => undefined;
internals.pruneUnavailableApprovalTools = async () => false;
internals.pruneUnavailableSessionToolingSelections = () => false;
(
service as unknown as {
sidecar: {
runTurn: (command: RunTurnCommand) => Promise<[]>;
resolveApproval: () => Promise<void>;
};
}
).sidecar = {
runTurn: async (command) => {
options?.captureRunTurn?.(command);
return [];
},
resolveApproval: async () => undefined,
};
return service;
}
describe('AryxAppService project customization', () => {
test('sendSessionMessage injects project instructions and enabled project agent profiles', async () => {
const workspace = createWorkspaceSeed();
const basePattern = workspace.patterns.find((candidate) => candidate.mode === 'single');
if (!basePattern) {
throw new Error('Expected a single-agent pattern in the workspace seed.');
}
const pattern: PatternDefinition = {
...basePattern,
agents: [
{
...basePattern.agents[0]!,
copilot: {
customAgents: [
{
name: 'reviewer',
prompt: 'Built-in reviewer prompt.',
},
],
},
},
],
};
const project = createProject({
customization: {
instructions: [
{
id: 'instruction-repo',
sourcePath: '.github\\copilot-instructions.md',
content: 'Use TypeScript.',
},
{
id: 'instruction-agents',
sourcePath: 'AGENTS.md',
content: 'Prefer focused tests.',
},
],
agentProfiles: [
{
id: 'agent-reviewer',
name: 'reviewer',
prompt: 'Project reviewer prompt.',
sourcePath: '.github\\agents\\reviewer.agent.md',
enabled: true,
},
{
id: 'agent-readme',
name: 'readme-specialist',
description: 'Documentation specialist',
tools: ['read', 'edit'],
prompt: 'Focus on README improvements.',
sourcePath: '.github\\agents\\readme-specialist.agent.md',
enabled: true,
},
{
id: 'agent-disabled',
name: 'disabled-specialist',
prompt: 'Should never be injected.',
sourcePath: '.github\\agents\\disabled-specialist.agent.md',
enabled: false,
},
],
promptFiles: [],
lastScannedAt: TIMESTAMP,
},
});
const session = createSession(project.id, pattern.id);
workspace.projects = [project];
workspace.sessions = [session];
workspace.selectedProjectId = project.id;
workspace.selectedPatternId = pattern.id;
workspace.selectedSessionId = session.id;
let command: RunTurnCommand | undefined;
const service = createService(workspace, pattern, {
captureRunTurn: (capturedCommand) => {
command = capturedCommand;
},
});
await service.sendSessionMessage(session.id, 'Use the repository guidance.');
expect(command?.projectInstructions).toBe('Use TypeScript.\n\nPrefer focused tests.');
expect(command?.pattern.agents[0]?.copilot?.customAgents).toEqual([
{
name: 'reviewer',
prompt: 'Built-in reviewer prompt.',
},
{
name: 'readme-specialist',
description: 'Documentation specialist',
tools: ['read', 'edit'],
prompt: 'Focus on README improvements.',
infer: undefined,
},
]);
});
test('setProjectAgentProfileEnabled persists the updated enabled state', async () => {
const workspace = createWorkspaceSeed();
const pattern = workspace.patterns.find((candidate) => candidate.mode === 'single');
if (!pattern) {
throw new Error('Expected a single-agent pattern in the workspace seed.');
}
const project = createProject({
customization: {
instructions: [],
agentProfiles: [
{
id: 'agent-readme',
name: 'readme-specialist',
prompt: 'Focus on docs.',
sourcePath: '.github\\agents\\readme-specialist.agent.md',
enabled: true,
},
],
promptFiles: [],
lastScannedAt: TIMESTAMP,
},
});
const session = createSession(project.id, pattern.id);
workspace.projects = [project];
workspace.sessions = [session];
const service = createService(workspace, pattern);
const updated = await service.setProjectAgentProfileEnabled(project.id, 'agent-readme', false);
expect(updated.projects[0]?.customization?.agentProfiles).toEqual([
{
id: 'agent-readme',
name: 'readme-specialist',
prompt: 'Focus on docs.',
sourcePath: '.github\\agents\\readme-specialist.agent.md',
enabled: false,
},
]);
});
});
+160
View File
@@ -0,0 +1,160 @@
import { afterEach, describe, expect, test } from 'bun:test';
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { ProjectCustomizationScanner } from '@main/services/customizationScanner';
const temporaryPaths: string[] = [];
afterEach(async () => {
await Promise.all(
temporaryPaths.splice(0).map((path) => rm(path, { recursive: true, force: true })),
);
});
async function createTempDirectory(): Promise<string> {
const directory = await mkdtemp(join(tmpdir(), 'aryx-customization-scanner-'));
temporaryPaths.push(directory);
return directory;
}
describe('ProjectCustomizationScanner', () => {
test('discovers project instructions, custom agents, and prompt files', async () => {
const projectPath = await createTempDirectory();
await mkdir(join(projectPath, '.github', 'agents'), { recursive: true });
await mkdir(join(projectPath, '.github', 'prompts'), { recursive: true });
await writeFile(
join(projectPath, '.github', 'copilot-instructions.md'),
'# Repo instructions\nUse TypeScript.\n',
'utf8',
);
await writeFile(
join(projectPath, 'AGENTS.md'),
'# Agent guidance\nPrefer clear tests.\n',
'utf8',
);
await writeFile(
join(projectPath, '.github', 'agents', 'readme-specialist.agent.md'),
`---
name: readme-specialist
description: README specialist
tools:
- read
- edit
infer: true
mcp-servers:
docs-mcp:
type: local
command: node
args:
- docs-server.js
---
Focus on repository documentation only.
`,
'utf8',
);
await writeFile(
join(projectPath, '.github', 'prompts', 'explain-code.prompt.md'),
`---
agent: agent
description: Generate a clear explanation
---
Explain the following code:
\${input:code:Paste your code here}
Audience: \${input:audience:Who is this for?}
`,
'utf8',
);
const scanned = await new ProjectCustomizationScanner().scanProject(projectPath);
expect(scanned.instructions).toEqual([
{
id: expect.any(String),
sourcePath: '.github\\copilot-instructions.md',
content: '# Repo instructions\nUse TypeScript.',
},
{
id: expect.any(String),
sourcePath: 'AGENTS.md',
content: '# Agent guidance\nPrefer clear tests.',
},
]);
expect(scanned.agentProfiles).toEqual([
{
id: expect.any(String),
name: 'readme-specialist',
description: 'README specialist',
tools: ['read', 'edit'],
prompt: 'Focus on repository documentation only.',
infer: true,
mcpServers: {
'docs-mcp': {
args: ['docs-server.js'],
command: 'node',
type: 'local',
},
},
sourcePath: '.github\\agents\\readme-specialist.agent.md',
enabled: true,
},
]);
expect(scanned.promptFiles).toEqual([
{
id: expect.any(String),
name: 'explain-code',
description: 'Generate a clear explanation',
agent: 'agent',
template: 'Explain the following code:\n${input:code:Paste your code here}\nAudience: ${input:audience:Who is this for?}',
variables: [
{ name: 'code', placeholder: 'Paste your code here' },
{ name: 'audience', placeholder: 'Who is this for?' },
],
sourcePath: '.github\\prompts\\explain-code.prompt.md',
},
]);
expect(scanned.lastScannedAt).toEqual(expect.any(String));
});
test('retains the previous parsed agent profile when frontmatter becomes malformed', async () => {
const projectPath = await createTempDirectory();
await mkdir(join(projectPath, '.github', 'agents'), { recursive: true });
const filePath = join(projectPath, '.github', 'agents', 'reviewer.agent.md');
await writeFile(
filePath,
`---
name: reviewer
description: Review specialist
tools: [read, search]
---
Review code changes carefully.
`,
'utf8',
);
const scanner = new ProjectCustomizationScanner();
const firstScan = await scanner.scanProject(projectPath);
const previousState = {
...firstScan,
agentProfiles: firstScan.agentProfiles.map((profile) => ({ ...profile, enabled: false })),
};
await writeFile(
filePath,
`---
name: [reviewer
description: broken yaml
---
This should not replace the previous state.
`,
'utf8',
);
const secondScan = await scanner.scanProject(projectPath, previousState);
expect(secondScan.agentProfiles).toEqual(previousState.agentProfiles);
});
});