Skip to content
Merged
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
75 changes: 75 additions & 0 deletions src/index.runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,81 @@ describe('OpenCodeRulesPlugin', () => {
expect(result.system).toContain('Rule Content');
});

it('should consolidate array system messages into single string', async () => {
const { testDir, globalRulesDir } = getTestDirs();
writeFileSync(path.join(globalRulesDir, 'rule.md'), '# My Rule');
process.env.XDG_CONFIG_HOME = path.join(testDir, '.config');

const {
default: { server: plugin },
} = await import('./index.js');
const mockInput = createMockPluginInput({ testDir });

const hooks = await plugin(
mockInput as unknown as Parameters<typeof plugin>[0]
);
const systemTransform = hooks['experimental.chat.system.transform'] as (
input: unknown,
output: { system: string[] }
) => Promise<{ system: string }>;
const result = await systemTransform(
{},
{ system: ['First message.', 'Second message.'] }
);

expect(typeof result.system).toBe('string');
expect(result.system).toContain('First message.');
expect(result.system).toContain('Second message.');
expect(result.system).toContain('My Rule');
});

it('should handle empty array system messages', async () => {
const { testDir, globalRulesDir } = getTestDirs();
writeFileSync(path.join(globalRulesDir, 'rule.md'), '# My Rule');
process.env.XDG_CONFIG_HOME = path.join(testDir, '.config');

const {
default: { server: plugin },
} = await import('./index.js');
const mockInput = createMockPluginInput({ testDir });

const hooks = await plugin(
mockInput as unknown as Parameters<typeof plugin>[0]
);
const systemTransform = hooks['experimental.chat.system.transform'] as (
input: unknown,
output: { system: string[] }
) => Promise<{ system: string }>;
const result = await systemTransform({}, { system: [] });

expect(typeof result.system).toBe('string');
expect(result.system).toContain('My Rule');
});

it('should handle single-element array system message', async () => {
const { testDir, globalRulesDir } = getTestDirs();
writeFileSync(path.join(globalRulesDir, 'rule.md'), '# My Rule');
process.env.XDG_CONFIG_HOME = path.join(testDir, '.config');

const {
default: { server: plugin },
} = await import('./index.js');
const mockInput = createMockPluginInput({ testDir });

const hooks = await plugin(
mockInput as unknown as Parameters<typeof plugin>[0]
);
const systemTransform = hooks['experimental.chat.system.transform'] as (
input: unknown,
output: { system: string[] }
) => Promise<{ system: string }>;
const result = await systemTransform({}, { system: ['Only message.'] });

expect(typeof result.system).toBe('string');
expect(result.system).toContain('Only message.');
expect(result.system).toContain('My Rule');
});

it('should not modify messages in messages.transform hook', async () => {
const { testDir, globalRulesDir } = getTestDirs();
writeFileSync(path.join(globalRulesDir, 'rule.md'), '# Rule');
Expand Down
7 changes: 5 additions & 2 deletions src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ interface SystemTransformInput {
}

interface SystemTransformOutput {
system?: string | string[];
system?: string;
}

interface OpenCodeRulesRuntimeOptions {
Expand Down Expand Up @@ -324,7 +324,10 @@ export class OpenCodeRulesRuntime {
}

if (Array.isArray(output.system)) {
output.system.push(combinedSystem);
output.system =
output.system.join('\n\n') +
(output.system.length > 0 ? '\n\n' : '') +
combinedSystem;
} else {
output.system = output.system
? `${output.system}\n\n${combinedSystem}`
Expand Down
Loading