opencode-morphllm 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,239 +0,0 @@
1
- import { describe, it, expect, vi } from 'bun:test';
2
- vi.mock('@morphllm/morphsdk', () => ({
3
- MorphClient: vi.fn().mockImplementation(() => ({
4
- routers: {
5
- raw: {
6
- classify: vi.fn(),
7
- },
8
- },
9
- })),
10
- }));
11
- vi.mock('../shared/config', () => ({
12
- API_KEY: 'sk-test-api-key-123',
13
- MORPH_MODEL_EASY: 'easy/easy',
14
- MORPH_MODEL_MEDIUM: 'medium/medium',
15
- MORPH_MODEL_HARD: 'hard/hard',
16
- MORPH_MODEL_DEFAULT: 'default/default',
17
- MORPH_ROUTER_PROMPT_CACHING_AWARE: false,
18
- MORPH_ROUTER_ENABLED: true,
19
- }));
20
- import { createModelRouterHook, extractPromptText } from './router';
21
- describe('router.ts', () => {
22
- describe('extractPromptText', () => {
23
- it('should extract text from parts', () => {
24
- const parts = [
25
- { type: 'text', text: 'Hello' },
26
- { type: 'text', text: 'World' },
27
- ];
28
- expect(extractPromptText(parts)).toBe('Hello World');
29
- });
30
- it('should handle empty parts', () => {
31
- const parts = [];
32
- expect(extractPromptText(parts)).toBe('');
33
- });
34
- it('should filter out non-text parts', () => {
35
- const parts = [
36
- { type: 'text', text: 'Hello' },
37
- { type: 'other', text: 'Ignore' },
38
- { type: 'text', text: 'World' },
39
- ];
40
- expect(extractPromptText(parts)).toBe('Hello World');
41
- });
42
- it('should handle parts with no text', () => {
43
- const parts = [
44
- { type: 'text', text: 'Hello' },
45
- { type: 'text' },
46
- { type: 'text', text: 'World' },
47
- ];
48
- expect(extractPromptText(parts)).toBe('Hello World');
49
- });
50
- it('should handle all non-text parts', () => {
51
- const parts = [
52
- { type: 'image', data: 'base64...' },
53
- { type: 'tool_use', name: 'bash' },
54
- ];
55
- expect(extractPromptText(parts)).toBe('');
56
- });
57
- });
58
- describe('createModelRouterHook', () => {
59
- it('should return a chat.message hook', () => {
60
- const hook = createModelRouterHook();
61
- expect('chat.message' in hook).toBe(true);
62
- expect(typeof hook['chat.message']).toBe('function');
63
- });
64
- it('should call the classifier with the correct input', async () => {
65
- const hook = createModelRouterHook();
66
- const classify = vi.fn().mockResolvedValue({ difficulty: 'easy' });
67
- const input = {
68
- sessionID: '123',
69
- classify,
70
- };
71
- const output = {
72
- message: {},
73
- parts: [{ type: 'text', text: 'test prompt' }],
74
- };
75
- await hook['chat.message'](input, output);
76
- expect(classify).toHaveBeenCalledWith({ input: 'test prompt' });
77
- });
78
- it('should assign the correct model based on difficulty', async () => {
79
- const hook = createModelRouterHook();
80
- const classify = vi.fn().mockResolvedValue({ difficulty: 'hard' });
81
- const input = {
82
- sessionID: '123',
83
- classify,
84
- };
85
- const output = {
86
- message: {},
87
- parts: [{ type: 'text', text: 'test prompt' }],
88
- };
89
- await hook['chat.message'](input, output);
90
- expect(input).toHaveProperty('model');
91
- expect(input.model.providerID).toBe('hard');
92
- expect(input.model.modelID).toBe('hard');
93
- });
94
- it('should use the default model if no difficulty is returned', async () => {
95
- const hook = createModelRouterHook();
96
- const classify = vi.fn().mockResolvedValue({});
97
- const input = {
98
- sessionID: '123',
99
- classify,
100
- };
101
- const output = {
102
- message: {},
103
- parts: [{ type: 'text', text: 'test prompt' }],
104
- };
105
- await hook['chat.message'](input, output);
106
- expect(input).toHaveProperty('model');
107
- expect(input.model.providerID).toBe('default');
108
- expect(input.model.modelID).toBe('default');
109
- });
110
- it('should use default model when router returns nothing', async () => {
111
- const hook = createModelRouterHook();
112
- const classify = vi.fn().mockResolvedValue({});
113
- const input = {
114
- sessionID: '123',
115
- classify,
116
- };
117
- const output = {
118
- message: {},
119
- parts: [{ type: 'text', text: 'test prompt' }],
120
- };
121
- await hook['chat.message'](input, output);
122
- expect(input).toHaveProperty('model');
123
- expect(input.model.providerID).toBe('default');
124
- expect(input.model.modelID).toBe('default');
125
- });
126
- it('should handle classifier throwing an error gracefully', async () => {
127
- const hook = createModelRouterHook();
128
- const classify = vi.fn().mockRejectedValue(new Error('API Error'));
129
- const input = {
130
- sessionID: '123',
131
- classify,
132
- };
133
- const output = {
134
- message: {},
135
- parts: [{ type: 'text', text: 'test prompt' }],
136
- };
137
- await expect(hook['chat.message'](input, output)).rejects.toThrow('API Error');
138
- });
139
- it('should handle classifier returning null', async () => {
140
- const hook = createModelRouterHook();
141
- const classify = vi.fn().mockResolvedValue(null);
142
- const input = {
143
- sessionID: '123',
144
- classify,
145
- };
146
- const output = {
147
- message: {},
148
- parts: [{ type: 'text', text: 'test prompt' }],
149
- };
150
- await hook['chat.message'](input, output);
151
- expect(input.model.providerID).toBe('default');
152
- expect(input.model.modelID).toBe('default');
153
- });
154
- it('should handle classifier returning undefined difficulty', async () => {
155
- const hook = createModelRouterHook();
156
- const classify = vi.fn().mockResolvedValue({ difficulty: undefined });
157
- const input = {
158
- sessionID: '123',
159
- classify,
160
- };
161
- const output = {
162
- message: {},
163
- parts: [{ type: 'text', text: 'test prompt' }],
164
- };
165
- await hook['chat.message'](input, output);
166
- expect(input.model.providerID).toBe('default');
167
- expect(input.model.modelID).toBe('default');
168
- });
169
- it('should default to default model for invalid difficulty values', async () => {
170
- const hook = createModelRouterHook();
171
- const classify = vi.fn().mockResolvedValue({ difficulty: 'ultra-hard' });
172
- const input = {
173
- sessionID: '123',
174
- classify,
175
- };
176
- const output = {
177
- message: {},
178
- parts: [{ type: 'text', text: 'test prompt' }],
179
- };
180
- await hook['chat.message'](input, output);
181
- expect(input.model.providerID).toBe('default');
182
- expect(input.model.modelID).toBe('default');
183
- });
184
- it('should default to default model for empty string difficulty', async () => {
185
- const hook = createModelRouterHook();
186
- const classify = vi.fn().mockResolvedValue({ difficulty: '' });
187
- const input = {
188
- sessionID: '123',
189
- classify,
190
- };
191
- const output = {
192
- message: {},
193
- parts: [{ type: 'text', text: 'test prompt' }],
194
- };
195
- await hook['chat.message'](input, output);
196
- expect(input.model.providerID).toBe('default');
197
- expect(input.model.modelID).toBe('default');
198
- });
199
- it('should handle case-insensitive difficulty matching', async () => {
200
- const hook = createModelRouterHook();
201
- const classify = vi.fn().mockResolvedValue({ difficulty: 'HARD' });
202
- const input = {
203
- sessionID: '123',
204
- classify,
205
- };
206
- const output = {
207
- message: {},
208
- parts: [{ type: 'text', text: 'test prompt' }],
209
- };
210
- await hook['chat.message'](input, output);
211
- expect(input.model.providerID).toBe('hard');
212
- expect(input.model.modelID).toBe('hard');
213
- });
214
- it('should route all messages when MORPH_ROUTER_PROMPT_CACHING_AWARE is disabled', async () => {
215
- const hook = createModelRouterHook();
216
- const classify = vi
217
- .fn()
218
- .mockResolvedValueOnce({ difficulty: 'hard' })
219
- .mockResolvedValueOnce({ difficulty: 'easy' });
220
- const sessionID = 'session-456';
221
- const input1 = { sessionID, classify };
222
- const output1 = {
223
- message: {},
224
- parts: [{ type: 'text', text: 'first message' }],
225
- };
226
- await hook['chat.message'](input1, output1);
227
- expect(classify).toHaveBeenCalledTimes(1);
228
- expect(input1.model.providerID).toBe('hard');
229
- const input2 = { sessionID, classify };
230
- const output2 = {
231
- message: {},
232
- parts: [{ type: 'text', text: 'second message' }],
233
- };
234
- await hook['chat.message'](input2, output2);
235
- expect(classify).toHaveBeenCalledTimes(2);
236
- expect(input2.model.providerID).toBe('easy');
237
- });
238
- });
239
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,201 +0,0 @@
1
- import { describe, it, expect, beforeEach, beforeAll, afterAll, } from 'bun:test';
2
- import { existsSync, writeFileSync, rmSync, mkdirSync, } from 'node:fs';
3
- import { join } from 'node:path';
4
- import { tmpdir } from 'node:os';
5
- import { env } from 'node:process';
6
- // Use a real temporary directory for testing - no mocking needed
7
- const mockConfigDir = join(tmpdir(), 'mock-morph-config-test');
8
- const mockMorphJson = join(mockConfigDir, 'morph.json');
9
- const mockMorphJsonc = join(mockConfigDir, 'morph.jsonc');
10
- // Save original environment
11
- let originalEnv;
12
- import { getMorphPluginConfigPath, loadMorphPluginConfig, loadMorphPluginConfigWithProjectOverride, } from './config';
13
- describe('config.ts', () => {
14
- beforeAll(() => {
15
- // Save original environment and set custom config dir
16
- originalEnv = {
17
- OPENCODE_CONFIG_DIR: env.OPENCODE_CONFIG_DIR,
18
- };
19
- env.OPENCODE_CONFIG_DIR = mockConfigDir;
20
- // Create mock config directory
21
- if (!existsSync(mockConfigDir)) {
22
- mkdirSync(mockConfigDir, { recursive: true });
23
- }
24
- });
25
- afterAll(() => {
26
- // Restore original environment
27
- if (originalEnv.OPENCODE_CONFIG_DIR !== undefined) {
28
- env.OPENCODE_CONFIG_DIR = originalEnv.OPENCODE_CONFIG_DIR;
29
- }
30
- else {
31
- delete env.OPENCODE_CONFIG_DIR;
32
- }
33
- // Cleanup
34
- try {
35
- if (existsSync(mockMorphJson))
36
- rmSync(mockMorphJson);
37
- if (existsSync(mockMorphJsonc))
38
- rmSync(mockMorphJsonc);
39
- if (existsSync(mockConfigDir))
40
- rmSync(mockConfigDir, { recursive: true });
41
- }
42
- catch {
43
- // Ignore cleanup errors
44
- }
45
- });
46
- beforeEach(() => {
47
- // Clean up any existing config files before each test
48
- try {
49
- if (existsSync(mockMorphJson))
50
- rmSync(mockMorphJson);
51
- if (existsSync(mockMorphJsonc))
52
- rmSync(mockMorphJsonc);
53
- }
54
- catch {
55
- // Ignore cleanup errors
56
- }
57
- });
58
- describe('getMorphPluginConfigPath', () => {
59
- it('should return path to morph.json in config directory', () => {
60
- const path = getMorphPluginConfigPath();
61
- expect(path).toContain('morph.json');
62
- expect(path).toContain(mockConfigDir);
63
- });
64
- });
65
- describe('loadMorphPluginConfig', () => {
66
- it('should return null when no config files exist', () => {
67
- const result = loadMorphPluginConfig();
68
- expect(result).toBeNull();
69
- });
70
- it('should load config from .jsonc file', () => {
71
- const content = `{
72
- "MORPH_API_KEY": "test-key-123",
73
- "MORPH_ROUTER_ENABLED": false
74
- }`;
75
- writeFileSync(mockMorphJsonc, content);
76
- const result = loadMorphPluginConfig();
77
- expect(result).not.toBeNull();
78
- expect(result?.MORPH_API_KEY).toBe('test-key-123');
79
- expect(result?.MORPH_ROUTER_ENABLED).toBe(false);
80
- });
81
- it('should load config from .json file', () => {
82
- const content = JSON.stringify({
83
- MORPH_API_KEY: 'json-key-456',
84
- MORPH_MODEL_EASY: 'provider/model-easy',
85
- });
86
- writeFileSync(mockMorphJson, content);
87
- const result = loadMorphPluginConfig();
88
- expect(result).not.toBeNull();
89
- expect(result?.MORPH_API_KEY).toBe('json-key-456');
90
- expect(result?.MORPH_MODEL_EASY).toBe('provider/model-easy');
91
- });
92
- it('should prefer .jsonc over .json when both exist', () => {
93
- const jsoncContent = '{"MORPH_API_KEY": "from-jsonc"}';
94
- const jsonContent = '{"MORPH_API_KEY": "from-json"}';
95
- writeFileSync(mockMorphJsonc, jsoncContent);
96
- writeFileSync(mockMorphJson, jsonContent);
97
- const result = loadMorphPluginConfig();
98
- expect(result?.MORPH_API_KEY).toBe('from-jsonc');
99
- });
100
- it('should return null for invalid .jsonc content', () => {
101
- writeFileSync(mockMorphJsonc, 'invalid json content');
102
- const result = loadMorphPluginConfig();
103
- expect(result).toBeNull();
104
- });
105
- it('should return null for invalid .json content', () => {
106
- writeFileSync(mockMorphJson, 'invalid json content');
107
- const result = loadMorphPluginConfig();
108
- expect(result).toBeNull();
109
- });
110
- it('should handle all MORPH config fields', () => {
111
- const content = JSON.stringify({
112
- MORPH_API_KEY: 'api-key',
113
- MORPH_ROUTER_ENABLED: true,
114
- MORPH_MODEL_EASY: 'easy/provider/model',
115
- MORPH_MODEL_MEDIUM: 'medium/provider/model',
116
- MORPH_MODEL_HARD: 'hard/provider/model',
117
- MORPH_MODEL_DEFAULT: 'default/provider/model',
118
- });
119
- writeFileSync(mockMorphJson, content);
120
- const result = loadMorphPluginConfig();
121
- expect(result).toEqual({
122
- MORPH_API_KEY: 'api-key',
123
- MORPH_ROUTER_ENABLED: true,
124
- MORPH_MODEL_EASY: 'easy/provider/model',
125
- MORPH_MODEL_MEDIUM: 'medium/provider/model',
126
- MORPH_MODEL_HARD: 'hard/provider/model',
127
- MORPH_MODEL_DEFAULT: 'default/provider/model',
128
- });
129
- });
130
- it('should handle nested MORPH_ROUTER_CONFIGS', () => {
131
- const content = JSON.stringify({
132
- MORPH_API_KEY: 'api-key',
133
- MORPH_ROUTER_CONFIGS: {
134
- MORPH_ROUTER_ENABLED: true,
135
- MORPH_MODEL_EASY: 'easy/provider/model',
136
- MORPH_MODEL_MEDIUM: 'medium/provider/model',
137
- MORPH_MODEL_HARD: 'hard/provider/model',
138
- MORPH_MODEL_DEFAULT: 'default/provider/model',
139
- },
140
- });
141
- writeFileSync(mockMorphJson, content);
142
- const result = loadMorphPluginConfig();
143
- expect(result?.MORPH_API_KEY).toBe('api-key');
144
- expect(result?.MORPH_ROUTER_CONFIGS).toEqual({
145
- MORPH_ROUTER_ENABLED: true,
146
- MORPH_MODEL_EASY: 'easy/provider/model',
147
- MORPH_MODEL_MEDIUM: 'medium/provider/model',
148
- MORPH_MODEL_HARD: 'hard/provider/model',
149
- MORPH_MODEL_DEFAULT: 'default/provider/model',
150
- });
151
- });
152
- });
153
- describe('loadMorphPluginConfigWithProjectOverride', () => {
154
- it('should return empty object when no config exists', () => {
155
- const result = loadMorphPluginConfigWithProjectOverride('/non-existent-path');
156
- expect(result).toEqual({});
157
- });
158
- it('should merge user config with project config', () => {
159
- // Create user config
160
- writeFileSync(mockMorphJson, JSON.stringify({
161
- MORPH_API_KEY: 'user-api-key',
162
- MORPH_MODEL_EASY: 'user-easy-model',
163
- }));
164
- // Create project config directory
165
- const projectDir = join(tmpdir(), 'test-project');
166
- const projectConfigDir = join(projectDir, '.opencode');
167
- const projectConfigPath = join(projectConfigDir, 'morph.json');
168
- mkdirSync(projectConfigDir, { recursive: true });
169
- writeFileSync(projectConfigPath, JSON.stringify({
170
- MORPH_API_KEY: 'project-api-key',
171
- MORPH_MODEL_HARD: 'project-hard-model',
172
- }));
173
- const result = loadMorphPluginConfigWithProjectOverride(projectDir);
174
- // Project config should override user config
175
- expect(result.MORPH_API_KEY).toBe('project-api-key');
176
- expect(result.MORPH_MODEL_EASY).toBe('user-easy-model');
177
- expect(result.MORPH_MODEL_HARD).toBe('project-hard-model');
178
- });
179
- it('should handle project .jsonc config', () => {
180
- // Create user config
181
- writeFileSync(mockMorphJson, JSON.stringify({
182
- MORPH_API_KEY: 'user-key',
183
- }));
184
- // Create project config with .jsonc
185
- const projectDir = join(tmpdir(), 'test-project2');
186
- const projectConfigDir = join(projectDir, '.opencode');
187
- const projectConfigPath = join(projectConfigDir, 'morph.jsonc');
188
- mkdirSync(projectConfigDir, { recursive: true });
189
- const jsoncContent = `
190
- // Project config with comments
191
- {
192
- "MORPH_MODEL_MEDIUM": "project-medium-model"
193
- }
194
- `;
195
- writeFileSync(projectConfigPath, jsoncContent);
196
- const result = loadMorphPluginConfigWithProjectOverride(projectDir);
197
- expect(result.MORPH_API_KEY).toBe('user-key');
198
- expect(result.MORPH_MODEL_MEDIUM).toBe('project-medium-model');
199
- });
200
- });
201
- });
@@ -1 +0,0 @@
1
- export {};