opencode-morphllm 0.0.5 → 0.0.7

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.
Files changed (39) hide show
  1. package/.gitleaks.toml +6 -0
  2. package/.husky/pre-commit +3 -1
  3. package/.prettierignore +1 -0
  4. package/.prettierrc +1 -1
  5. package/README.md +21 -2
  6. package/bun.lock +23 -0
  7. package/bunfig.toml +4 -0
  8. package/dist/index.js +13 -14
  9. package/dist/morph/mcps.js +15 -0
  10. package/dist/morph/mcps.test.d.ts +1 -0
  11. package/dist/morph/mcps.test.js +39 -0
  12. package/dist/morph/router.d.ts +22 -0
  13. package/dist/morph/router.js +65 -0
  14. package/dist/morph/router.test.d.ts +1 -0
  15. package/dist/morph/router.test.js +239 -0
  16. package/dist/shared/config.d.ts +29 -0
  17. package/dist/shared/config.js +80 -0
  18. package/dist/shared/config.test.d.ts +1 -0
  19. package/dist/shared/config.test.js +201 -0
  20. package/dist/shared/opencode-config-dir.d.ts +18 -8
  21. package/dist/shared/opencode-config-dir.js +93 -47
  22. package/dist/shared/opencode-config-dir.test.d.ts +1 -0
  23. package/dist/shared/opencode-config-dir.test.js +310 -0
  24. package/package.json +3 -2
  25. package/src/index.ts +3 -4
  26. package/src/morph/mcps.test.ts +51 -0
  27. package/src/{mcps.ts → morph/mcps.ts} +1 -1
  28. package/src/morph/router.test.ts +267 -0
  29. package/src/{router.ts → morph/router.ts} +29 -3
  30. package/src/shared/config.test.ts +257 -0
  31. package/src/{config.ts → shared/config.ts} +30 -6
  32. package/src/shared/opencode-config-dir.test.ts +404 -0
  33. package/src/shared/opencode-config-dir.ts +90 -11
  34. package/dist/config.d.ts +0 -20
  35. package/dist/config.js +0 -71
  36. package/dist/mcps.js +0 -15
  37. package/dist/router.d.ts +0 -27
  38. package/dist/router.js +0 -51
  39. /package/dist/{mcps.d.ts → morph/mcps.d.ts} +0 -0
@@ -0,0 +1,80 @@
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { getOpenCodeConfigDir } from './opencode-config-dir';
4
+ const MORPH_PLUGIN_NAME = 'morph';
5
+ export function getMorphPluginConfigPath() {
6
+ const configDir = getOpenCodeConfigDir({ binary: 'opencode' });
7
+ return join(configDir, `${MORPH_PLUGIN_NAME}.json`);
8
+ }
9
+ function parseJsonc(content) {
10
+ try {
11
+ // A simple JSONC parser that removes comments
12
+ const cleanedContent = content.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, '');
13
+ return JSON.parse(cleanedContent);
14
+ }
15
+ catch {
16
+ return null;
17
+ }
18
+ }
19
+ export function loadMorphPluginConfig() {
20
+ const jsoncPath = getMorphPluginConfigPath().replace('.json', '.jsonc');
21
+ const jsonPath = getMorphPluginConfigPath();
22
+ if (existsSync(jsoncPath)) {
23
+ try {
24
+ const content = readFileSync(jsoncPath, 'utf-8');
25
+ return parseJsonc(content);
26
+ }
27
+ catch {
28
+ return null;
29
+ }
30
+ }
31
+ if (existsSync(jsonPath)) {
32
+ try {
33
+ const content = readFileSync(jsonPath, 'utf-8');
34
+ return JSON.parse(content);
35
+ }
36
+ catch {
37
+ return null;
38
+ }
39
+ }
40
+ return null;
41
+ }
42
+ export function loadMorphPluginConfigWithProjectOverride(projectDir = process.cwd()) {
43
+ const userConfig = loadMorphPluginConfig() ?? {};
44
+ const projectBasePath = join(projectDir, '.opencode', MORPH_PLUGIN_NAME);
45
+ const projectJsoncPath = `${projectBasePath}.jsonc`;
46
+ const projectJsonPath = `${projectBasePath}.json`;
47
+ let projectConfig = {};
48
+ if (existsSync(projectJsoncPath)) {
49
+ try {
50
+ const content = readFileSync(projectJsoncPath, 'utf-8');
51
+ projectConfig = parseJsonc(content) ?? {};
52
+ }
53
+ catch {
54
+ // Ignore parse errors
55
+ }
56
+ }
57
+ else if (existsSync(projectJsonPath)) {
58
+ try {
59
+ const content = readFileSync(projectJsonPath, 'utf-8');
60
+ projectConfig = JSON.parse(content);
61
+ }
62
+ catch {
63
+ // Ignore parse errors
64
+ }
65
+ }
66
+ return { ...userConfig, ...projectConfig };
67
+ }
68
+ const config = loadMorphPluginConfigWithProjectOverride();
69
+ const routerConfigs = config.MORPH_ROUTER_CONFIGS || {};
70
+ export const API_KEY = config.MORPH_API_KEY || '';
71
+ export const MORPH_MODEL_EASY = routerConfigs.MORPH_MODEL_EASY || config.MORPH_MODEL_EASY || '';
72
+ export const MORPH_MODEL_MEDIUM = routerConfigs.MORPH_MODEL_MEDIUM || config.MORPH_MODEL_MEDIUM || '';
73
+ export const MORPH_MODEL_HARD = routerConfigs.MORPH_MODEL_HARD || config.MORPH_MODEL_HARD || '';
74
+ export const MORPH_MODEL_DEFAULT = routerConfigs.MORPH_MODEL_DEFAULT ||
75
+ config.MORPH_MODEL_DEFAULT ||
76
+ MORPH_MODEL_MEDIUM;
77
+ export const MORPH_ROUTER_ENABLED = routerConfigs.MORPH_ROUTER_ENABLED ?? config.MORPH_ROUTER_ENABLED ?? true;
78
+ export const MORPH_ROUTER_PROMPT_CACHING_AWARE = routerConfigs.MORPH_ROUTER_PROMPT_CACHING_AWARE ??
79
+ config.MORPH_ROUTER_PROMPT_CACHING_AWARE ??
80
+ false;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,201 @@
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,9 +1,19 @@
1
- interface OpenCodeConfigDirOptions {
2
- binary: 'opencode' | 'opencode-desktop';
3
- version?: string | null;
4
- checkExisting?: boolean;
1
+ export type OpenCodeBinaryType = 'opencode' | 'opencode-desktop';
2
+ export interface OpenCodeConfigDirOptions {
3
+ binary: OpenCodeBinaryType;
4
+ version?: string | null;
5
+ checkExisting?: boolean;
5
6
  }
6
- export declare function getOpenCodeConfigDir(
7
- options: OpenCodeConfigDirOptions
8
- ): string;
9
- export {};
7
+ export interface OpenCodeConfigPaths {
8
+ configDir: string;
9
+ configJson: string;
10
+ configJsonc: string;
11
+ packageJson: string;
12
+ omoConfig: string;
13
+ }
14
+ export declare const TAURI_APP_IDENTIFIER = "ai.opencode.desktop";
15
+ export declare const TAURI_APP_IDENTIFIER_DEV = "ai.opencode.desktop.dev";
16
+ export declare function isDevBuild(version: string | null | undefined): boolean;
17
+ export declare function getOpenCodeConfigDir(options: OpenCodeConfigDirOptions): string;
18
+ export declare function getOpenCodeConfigPaths(options: OpenCodeConfigDirOptions): OpenCodeConfigPaths;
19
+ export declare function detectExistingConfigDir(binary: OpenCodeBinaryType, version?: string | null): string | null;
@@ -1,56 +1,102 @@
1
1
  import { existsSync } from 'node:fs';
2
2
  import { homedir } from 'node:os';
3
3
  import { join, resolve } from 'node:path';
4
+ export const TAURI_APP_IDENTIFIER = 'ai.opencode.desktop';
5
+ export const TAURI_APP_IDENTIFIER_DEV = 'ai.opencode.desktop.dev';
6
+ export function isDevBuild(version) {
7
+ if (!version)
8
+ return false;
9
+ return version.includes('-dev') || version.includes('.dev');
10
+ }
4
11
  function getTauriConfigDir(identifier) {
5
- const platform = process.platform;
6
- switch (platform) {
7
- case 'darwin':
8
- return join(homedir(), 'Library', 'Application Support', identifier);
9
- case 'win32': {
10
- const appData =
11
- process.env.APPDATA || join(homedir(), 'AppData', 'Roaming');
12
- return join(appData, identifier);
13
- }
14
- case 'linux':
15
- default: {
16
- const xdgConfig =
17
- process.env.XDG_CONFIG_HOME || join(homedir(), '.config');
18
- return join(xdgConfig, identifier);
19
- }
20
- }
12
+ const platform = process.platform;
13
+ switch (platform) {
14
+ case 'darwin':
15
+ return join(homedir(), 'Library', 'Application Support', identifier);
16
+ case 'win32': {
17
+ const appData = process.env.APPDATA || join(homedir(), 'AppData', 'Roaming');
18
+ return join(appData, identifier);
19
+ }
20
+ case 'linux':
21
+ default: {
22
+ const xdgConfig = process.env.XDG_CONFIG_HOME || join(homedir(), '.config');
23
+ return join(xdgConfig, identifier);
24
+ }
25
+ }
21
26
  }
22
27
  function getCliConfigDir() {
23
- const envConfigDir = process.env.OPENCODE_CONFIG_DIR?.trim();
24
- if (envConfigDir) {
25
- return resolve(envConfigDir);
26
- }
27
- if (process.platform === 'win32') {
28
- const crossPlatformDir = join(homedir(), '.config', 'opencode');
29
- const crossPlatformConfig = join(crossPlatformDir, 'opencode.json');
30
- if (existsSync(crossPlatformConfig)) {
31
- return crossPlatformDir;
32
- }
33
- const appData =
34
- process.env.APPDATA || join(homedir(), 'AppData', 'Roaming');
35
- const appdataDir = join(appData, 'opencode');
36
- const appdataConfig = join(appdataDir, 'opencode.json');
37
- if (existsSync(appdataConfig)) {
38
- return appdataDir;
39
- }
40
- return crossPlatformDir;
41
- }
42
- const xdgConfig = process.env.XDG_CONFIG_HOME || join(homedir(), '.config');
43
- return join(xdgConfig, 'opencode');
28
+ const envConfigDir = process.env.OPENCODE_CONFIG_DIR?.trim();
29
+ if (envConfigDir) {
30
+ return resolve(envConfigDir);
31
+ }
32
+ if (process.platform === 'win32') {
33
+ const crossPlatformDir = join(homedir(), '.config', 'opencode');
34
+ const crossPlatformConfig = join(crossPlatformDir, 'opencode.json');
35
+ if (existsSync(crossPlatformConfig)) {
36
+ return crossPlatformDir;
37
+ }
38
+ const appData = process.env.APPDATA || join(homedir(), 'AppData', 'Roaming');
39
+ const appdataDir = join(appData, 'opencode');
40
+ const appdataConfig = join(appdataDir, 'opencode.json');
41
+ if (existsSync(appdataConfig)) {
42
+ return appdataDir;
43
+ }
44
+ return crossPlatformDir;
45
+ }
46
+ const xdgConfig = process.env.XDG_CONFIG_HOME || join(homedir(), '.config');
47
+ return join(xdgConfig, 'opencode');
44
48
  }
45
49
  export function getOpenCodeConfigDir(options) {
46
- if (options.binary === 'opencode-desktop') {
47
- const version = options.version;
48
- const isDev =
49
- !!version && (version.includes('-dev') || version.includes('.dev'));
50
- const identifier = isDev
51
- ? 'ai.opencode.desktop.dev'
52
- : 'ai.opencode.desktop';
53
- return getTauriConfigDir(identifier);
54
- }
55
- return getCliConfigDir();
50
+ const { binary, version, checkExisting = true } = options;
51
+ if (binary === 'opencode') {
52
+ return getCliConfigDir();
53
+ }
54
+ const identifier = isDevBuild(version)
55
+ ? TAURI_APP_IDENTIFIER_DEV
56
+ : TAURI_APP_IDENTIFIER;
57
+ const tauriDir = getTauriConfigDir(identifier);
58
+ if (checkExisting) {
59
+ const legacyDir = getCliConfigDir();
60
+ const legacyConfig = join(legacyDir, 'opencode.json');
61
+ const legacyConfigC = join(legacyDir, 'opencode.jsonc');
62
+ if (existsSync(legacyConfig) || existsSync(legacyConfigC)) {
63
+ return legacyDir;
64
+ }
65
+ }
66
+ return tauriDir;
67
+ }
68
+ export function getOpenCodeConfigPaths(options) {
69
+ const configDir = getOpenCodeConfigDir(options);
70
+ return {
71
+ configDir,
72
+ configJson: join(configDir, 'opencode.json'),
73
+ configJsonc: join(configDir, 'opencode.jsonc'),
74
+ packageJson: join(configDir, 'package.json'),
75
+ omoConfig: join(configDir, 'oh-my-opencode.json'),
76
+ };
77
+ }
78
+ export function detectExistingConfigDir(binary, version) {
79
+ const locations = [];
80
+ const envConfigDir = process.env.OPENCODE_CONFIG_DIR?.trim();
81
+ if (envConfigDir) {
82
+ locations.push(resolve(envConfigDir));
83
+ }
84
+ if (binary === 'opencode-desktop') {
85
+ const identifier = isDevBuild(version)
86
+ ? TAURI_APP_IDENTIFIER_DEV
87
+ : TAURI_APP_IDENTIFIER;
88
+ locations.push(getTauriConfigDir(identifier));
89
+ if (isDevBuild(version)) {
90
+ locations.push(getTauriConfigDir(TAURI_APP_IDENTIFIER));
91
+ }
92
+ }
93
+ locations.push(getCliConfigDir());
94
+ for (const dir of locations) {
95
+ const configJson = join(dir, 'opencode.json');
96
+ const configJsonc = join(dir, 'opencode.jsonc');
97
+ if (existsSync(configJson) || existsSync(configJsonc)) {
98
+ return dir;
99
+ }
100
+ }
101
+ return null;
56
102
  }
@@ -0,0 +1 @@
1
+ export {};