@antseed/cli 0.1.0 → 0.1.2
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.
- package/LICENSE +674 -0
- package/README.md +4 -2
- package/dist/cli/commands/dashboard.js +1 -1
- package/dist/cli/commands/dashboard.js.map +1 -1
- package/dist/cli/commands/seed.js +1 -1
- package/dist/cli/commands/seed.js.map +1 -1
- package/dist/cli/index.js +2 -2
- package/dist/cli/index.js.map +1 -1
- package/dist/plugins/registry.js +4 -4
- package/dist/plugins/registry.js.map +1 -1
- package/dist/proxy/buyer-proxy.d.ts.map +1 -1
- package/dist/proxy/buyer-proxy.js +141 -27
- package/dist/proxy/buyer-proxy.js.map +1 -1
- package/package.json +19 -17
- package/.env.example +0 -15
- package/src/cli/commands/balance.ts +0 -77
- package/src/cli/commands/browse.ts +0 -113
- package/src/cli/commands/config.ts +0 -271
- package/src/cli/commands/connect.test.ts +0 -69
- package/src/cli/commands/connect.ts +0 -342
- package/src/cli/commands/dashboard.ts +0 -59
- package/src/cli/commands/deposit.ts +0 -61
- package/src/cli/commands/dev.ts +0 -107
- package/src/cli/commands/init.ts +0 -99
- package/src/cli/commands/plugin-create.test.ts +0 -60
- package/src/cli/commands/plugin-create.ts +0 -230
- package/src/cli/commands/plugin.test.ts +0 -55
- package/src/cli/commands/plugin.ts +0 -295
- package/src/cli/commands/profile.ts +0 -95
- package/src/cli/commands/seed.test.ts +0 -70
- package/src/cli/commands/seed.ts +0 -447
- package/src/cli/commands/status.ts +0 -73
- package/src/cli/commands/types.ts +0 -56
- package/src/cli/commands/withdraw.ts +0 -61
- package/src/cli/formatters.ts +0 -64
- package/src/cli/index.ts +0 -46
- package/src/cli/shutdown.ts +0 -38
- package/src/config/defaults.ts +0 -49
- package/src/config/effective.test.ts +0 -80
- package/src/config/effective.ts +0 -119
- package/src/config/loader.test.ts +0 -95
- package/src/config/loader.ts +0 -251
- package/src/config/types.ts +0 -139
- package/src/config/validation.ts +0 -78
- package/src/env/load-env.ts +0 -20
- package/src/plugins/loader.ts +0 -96
- package/src/plugins/manager.ts +0 -66
- package/src/plugins/registry.ts +0 -45
- package/src/proxy/buyer-proxy.ts +0 -604
- package/src/status/node-status.ts +0 -105
- package/tsconfig.json +0 -9
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert/strict'
|
|
2
|
-
import test from 'node:test'
|
|
3
|
-
import { mkdtemp, readFile, rm } from 'node:fs/promises'
|
|
4
|
-
import { join } from 'node:path'
|
|
5
|
-
import { tmpdir } from 'node:os'
|
|
6
|
-
import { scaffoldPlugin } from './plugin-create.js'
|
|
7
|
-
|
|
8
|
-
test('scaffoldPlugin creates provider project structure', async () => {
|
|
9
|
-
const dir = await mkdtemp(join(tmpdir(), 'antseed-test-'))
|
|
10
|
-
try {
|
|
11
|
-
await scaffoldPlugin(dir, {
|
|
12
|
-
name: 'test-provider',
|
|
13
|
-
type: 'provider',
|
|
14
|
-
displayName: 'Test Provider',
|
|
15
|
-
description: 'A test provider plugin',
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
const pkg = JSON.parse(await readFile(join(dir, 'package.json'), 'utf-8')) as { name: string }
|
|
19
|
-
assert.equal(pkg.name, '@antseed/provider-test-provider')
|
|
20
|
-
|
|
21
|
-
const tsconfig = await readFile(join(dir, 'tsconfig.json'), 'utf-8')
|
|
22
|
-
assert.ok(tsconfig.includes('NodeNext'))
|
|
23
|
-
|
|
24
|
-
const index = await readFile(join(dir, 'src', 'index.ts'), 'utf-8')
|
|
25
|
-
assert.ok(index.includes("type: 'provider'"))
|
|
26
|
-
assert.ok(index.includes('AntseedProviderPlugin'))
|
|
27
|
-
|
|
28
|
-
const provider = await readFile(join(dir, 'src', 'provider.ts'), 'utf-8')
|
|
29
|
-
assert.ok(provider.includes('createProvider'))
|
|
30
|
-
|
|
31
|
-
const testFile = await readFile(join(dir, 'src', 'index.test.ts'), 'utf-8')
|
|
32
|
-
assert.ok(testFile.includes("'provider'"))
|
|
33
|
-
|
|
34
|
-
const readme = await readFile(join(dir, 'README.md'), 'utf-8')
|
|
35
|
-
assert.ok(readme.includes('test-provider'))
|
|
36
|
-
} finally {
|
|
37
|
-
await rm(dir, { recursive: true, force: true })
|
|
38
|
-
}
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
test('scaffoldPlugin creates router project structure', async () => {
|
|
42
|
-
const dir = await mkdtemp(join(tmpdir(), 'antseed-test-'))
|
|
43
|
-
try {
|
|
44
|
-
await scaffoldPlugin(dir, {
|
|
45
|
-
name: 'test-router',
|
|
46
|
-
type: 'router',
|
|
47
|
-
displayName: 'Test Router',
|
|
48
|
-
description: 'A test router plugin',
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
const index = await readFile(join(dir, 'src', 'index.ts'), 'utf-8')
|
|
52
|
-
assert.ok(index.includes("type: 'router'"))
|
|
53
|
-
assert.ok(index.includes('AntseedRouterPlugin'))
|
|
54
|
-
|
|
55
|
-
const router = await readFile(join(dir, 'src', 'router.ts'), 'utf-8')
|
|
56
|
-
assert.ok(router.includes('createRouter'))
|
|
57
|
-
} finally {
|
|
58
|
-
await rm(dir, { recursive: true, force: true })
|
|
59
|
-
}
|
|
60
|
-
})
|
|
@@ -1,230 +0,0 @@
|
|
|
1
|
-
import type { Command } from 'commander'
|
|
2
|
-
import chalk from 'chalk'
|
|
3
|
-
import { createInterface } from 'node:readline/promises'
|
|
4
|
-
import { mkdir, writeFile } from 'node:fs/promises'
|
|
5
|
-
import { join } from 'node:path'
|
|
6
|
-
|
|
7
|
-
interface PluginScaffoldOptions {
|
|
8
|
-
name: string
|
|
9
|
-
type: 'provider' | 'router'
|
|
10
|
-
displayName: string
|
|
11
|
-
description: string
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function generatePackageJson(opts: PluginScaffoldOptions): string {
|
|
15
|
-
return JSON.stringify(
|
|
16
|
-
{
|
|
17
|
-
name: `@antseed/${opts.type}-${opts.name}`,
|
|
18
|
-
version: '0.1.0',
|
|
19
|
-
description: opts.description,
|
|
20
|
-
type: 'module',
|
|
21
|
-
main: 'dist/index.js',
|
|
22
|
-
types: 'dist/index.d.ts',
|
|
23
|
-
scripts: {
|
|
24
|
-
build: 'tsc',
|
|
25
|
-
test: 'node --test dist/**/*.test.js',
|
|
26
|
-
prepublishOnly: 'npm run build',
|
|
27
|
-
},
|
|
28
|
-
keywords: ['antseed', 'plugin', opts.type],
|
|
29
|
-
peerDependencies: {
|
|
30
|
-
'@antseed/node': '>=0.1.0',
|
|
31
|
-
},
|
|
32
|
-
devDependencies: {
|
|
33
|
-
'@antseed/node': 'workspace:*',
|
|
34
|
-
'@types/node': '^20.11.0',
|
|
35
|
-
typescript: '^5.3.0',
|
|
36
|
-
},
|
|
37
|
-
},
|
|
38
|
-
null,
|
|
39
|
-
2,
|
|
40
|
-
)
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function generateTsconfig(): string {
|
|
44
|
-
return JSON.stringify(
|
|
45
|
-
{
|
|
46
|
-
compilerOptions: {
|
|
47
|
-
target: 'ES2022',
|
|
48
|
-
module: 'NodeNext',
|
|
49
|
-
moduleResolution: 'NodeNext',
|
|
50
|
-
outDir: 'dist',
|
|
51
|
-
rootDir: 'src',
|
|
52
|
-
strict: true,
|
|
53
|
-
esModuleInterop: true,
|
|
54
|
-
skipLibCheck: true,
|
|
55
|
-
declaration: true,
|
|
56
|
-
sourceMap: true,
|
|
57
|
-
},
|
|
58
|
-
include: ['src/**/*.ts'],
|
|
59
|
-
exclude: ['node_modules', 'dist'],
|
|
60
|
-
},
|
|
61
|
-
null,
|
|
62
|
-
2,
|
|
63
|
-
)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function generateIndexTs(opts: PluginScaffoldOptions): string {
|
|
67
|
-
const importType = opts.type === 'provider' ? 'AntseedProviderPlugin' : 'AntseedRouterPlugin'
|
|
68
|
-
const methodImport = opts.type === 'provider'
|
|
69
|
-
? `import { createProvider } from './${opts.type}.js'`
|
|
70
|
-
: `import { createRouter } from './${opts.type}.js'`
|
|
71
|
-
const method = opts.type === 'provider' ? 'createProvider' : 'createRouter'
|
|
72
|
-
|
|
73
|
-
return `import type { ${importType}, ConfigField } from '@antseed/node'
|
|
74
|
-
${methodImport}
|
|
75
|
-
|
|
76
|
-
const configSchema: ConfigField[] = [
|
|
77
|
-
// Define your plugin's configuration fields here
|
|
78
|
-
// { key: 'API_KEY', label: 'API Key', type: 'secret', required: true, description: 'Your API key' },
|
|
79
|
-
]
|
|
80
|
-
|
|
81
|
-
const plugin: ${importType} = {
|
|
82
|
-
name: '${opts.name}',
|
|
83
|
-
displayName: '${opts.displayName}',
|
|
84
|
-
version: '0.1.0',
|
|
85
|
-
description: '${opts.description}',
|
|
86
|
-
type: '${opts.type}',
|
|
87
|
-
configSchema,
|
|
88
|
-
${method},
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export default plugin
|
|
92
|
-
`
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function generateProviderTs(opts: PluginScaffoldOptions): string {
|
|
96
|
-
return `import type { Provider, SerializedHttpRequest, SerializedHttpResponse } from '@antseed/node'
|
|
97
|
-
|
|
98
|
-
export function createProvider(config: Record<string, string>): Provider {
|
|
99
|
-
return {
|
|
100
|
-
name: '${opts.name}',
|
|
101
|
-
models: [],
|
|
102
|
-
pricing: {
|
|
103
|
-
defaults: { inputUsdPerMillion: 0, outputUsdPerMillion: 0 },
|
|
104
|
-
},
|
|
105
|
-
maxConcurrency: 1,
|
|
106
|
-
async handleRequest(req: SerializedHttpRequest): Promise<SerializedHttpResponse> {
|
|
107
|
-
// Implement your provider logic here
|
|
108
|
-
throw new Error('Not implemented')
|
|
109
|
-
},
|
|
110
|
-
getCapacity() {
|
|
111
|
-
return { current: 0, max: 1 }
|
|
112
|
-
},
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
`
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function generateRouterTs(_opts: PluginScaffoldOptions): string {
|
|
119
|
-
return `import type { Router, PeerInfo, SerializedHttpRequest } from '@antseed/node'
|
|
120
|
-
|
|
121
|
-
export function createRouter(config: Record<string, string>): Router {
|
|
122
|
-
return {
|
|
123
|
-
selectPeer(req: SerializedHttpRequest, peers: PeerInfo[]): PeerInfo | null {
|
|
124
|
-
// Implement your router logic here
|
|
125
|
-
return peers[0] ?? null
|
|
126
|
-
},
|
|
127
|
-
onResult(peer: PeerInfo, result: { success: boolean; latencyMs: number; tokens: number }): void {
|
|
128
|
-
// Track peer performance here
|
|
129
|
-
},
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
`
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
function generateTestTs(opts: PluginScaffoldOptions): string {
|
|
136
|
-
return `import assert from 'node:assert/strict'
|
|
137
|
-
import test from 'node:test'
|
|
138
|
-
|
|
139
|
-
test('${opts.name} plugin exports valid manifest', async () => {
|
|
140
|
-
const { default: plugin } = await import('./index.js')
|
|
141
|
-
assert.equal(plugin.type, '${opts.type}')
|
|
142
|
-
assert.equal(plugin.name, '${opts.name}')
|
|
143
|
-
assert.ok(plugin.displayName)
|
|
144
|
-
assert.ok(plugin.version)
|
|
145
|
-
assert.ok(Array.isArray(plugin.configSchema))
|
|
146
|
-
})
|
|
147
|
-
`
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
function generateReadme(opts: PluginScaffoldOptions): string {
|
|
151
|
-
return `# antseed-${opts.type}-${opts.name}
|
|
152
|
-
|
|
153
|
-
${opts.description}
|
|
154
|
-
|
|
155
|
-
## Installation
|
|
156
|
-
|
|
157
|
-
\`\`\`bash
|
|
158
|
-
antseed plugin add antseed-${opts.type}-${opts.name}
|
|
159
|
-
\`\`\`
|
|
160
|
-
|
|
161
|
-
## Configuration
|
|
162
|
-
|
|
163
|
-
Configure via interactive prompt:
|
|
164
|
-
|
|
165
|
-
\`\`\`bash
|
|
166
|
-
antseed plugin config <instance-id>
|
|
167
|
-
\`\`\`
|
|
168
|
-
|
|
169
|
-
## Development
|
|
170
|
-
|
|
171
|
-
\`\`\`bash
|
|
172
|
-
npm install
|
|
173
|
-
npm run build
|
|
174
|
-
npm test
|
|
175
|
-
\`\`\`
|
|
176
|
-
`
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
export async function scaffoldPlugin(dir: string, opts: PluginScaffoldOptions): Promise<void> {
|
|
180
|
-
await mkdir(join(dir, 'src'), { recursive: true })
|
|
181
|
-
await writeFile(join(dir, 'package.json'), generatePackageJson(opts), 'utf-8')
|
|
182
|
-
await writeFile(join(dir, 'tsconfig.json'), generateTsconfig(), 'utf-8')
|
|
183
|
-
await writeFile(join(dir, 'src', 'index.ts'), generateIndexTs(opts), 'utf-8')
|
|
184
|
-
|
|
185
|
-
if (opts.type === 'provider') {
|
|
186
|
-
await writeFile(join(dir, 'src', 'provider.ts'), generateProviderTs(opts), 'utf-8')
|
|
187
|
-
} else {
|
|
188
|
-
await writeFile(join(dir, 'src', 'router.ts'), generateRouterTs(opts), 'utf-8')
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
await writeFile(join(dir, 'src', 'index.test.ts'), generateTestTs(opts), 'utf-8')
|
|
192
|
-
await writeFile(join(dir, 'README.md'), generateReadme(opts), 'utf-8')
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
export function registerPluginCreateCommand(pluginCmd: Command): void {
|
|
196
|
-
pluginCmd
|
|
197
|
-
.command('create')
|
|
198
|
-
.description('Scaffold a new plugin project')
|
|
199
|
-
.action(async () => {
|
|
200
|
-
const rl = createInterface({ input: process.stdin, output: process.stdout })
|
|
201
|
-
try {
|
|
202
|
-
const name = (await rl.question('Plugin name (lowercase, no spaces): ')).trim()
|
|
203
|
-
if (!name || !/^[a-z][a-z0-9-]*$/.test(name)) {
|
|
204
|
-
console.log(chalk.red('Invalid plugin name. Use lowercase letters, numbers, and hyphens.'))
|
|
205
|
-
process.exit(1)
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
const typeAnswer = (await rl.question('Plugin type (provider/router): ')).trim()
|
|
209
|
-
if (typeAnswer !== 'provider' && typeAnswer !== 'router') {
|
|
210
|
-
console.log(chalk.red('Plugin type must be "provider" or "router".'))
|
|
211
|
-
process.exit(1)
|
|
212
|
-
}
|
|
213
|
-
const type = typeAnswer as 'provider' | 'router'
|
|
214
|
-
|
|
215
|
-
const displayName = (await rl.question('Display name: ')).trim() || name
|
|
216
|
-
const description = (await rl.question('Description: ')).trim() || `Antseed ${type} plugin`
|
|
217
|
-
|
|
218
|
-
const dir = join(process.cwd(), `antseed-${type}-${name}`)
|
|
219
|
-
await scaffoldPlugin(dir, { name, type, displayName, description })
|
|
220
|
-
|
|
221
|
-
console.log(chalk.green(`\nScaffolded plugin at: ${dir}`))
|
|
222
|
-
console.log(chalk.dim('\nNext steps:'))
|
|
223
|
-
console.log(chalk.dim(` cd antseed-${type}-${name}`))
|
|
224
|
-
console.log(chalk.dim(' npm install'))
|
|
225
|
-
console.log(chalk.dim(' npm run build'))
|
|
226
|
-
} finally {
|
|
227
|
-
rl.close()
|
|
228
|
-
}
|
|
229
|
-
})
|
|
230
|
-
}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert/strict'
|
|
2
|
-
import test from 'node:test'
|
|
3
|
-
import { TRUSTED_PLUGINS } from '../../plugins/registry.js'
|
|
4
|
-
import { buildPluginConfig, LEGACY_PACKAGE_MAP } from '../../plugins/loader.js'
|
|
5
|
-
|
|
6
|
-
test('TRUSTED_PLUGINS contains 6 official plugins', () => {
|
|
7
|
-
assert.equal(TRUSTED_PLUGINS.length, 6)
|
|
8
|
-
const names = TRUSTED_PLUGINS.map(p => p.name)
|
|
9
|
-
assert.ok(names.includes('anthropic'))
|
|
10
|
-
assert.ok(names.includes('claude-code'))
|
|
11
|
-
assert.ok(names.includes('openrouter'))
|
|
12
|
-
assert.ok(names.includes('local-llm'))
|
|
13
|
-
assert.ok(names.includes('local-proxy'))
|
|
14
|
-
assert.ok(names.includes('local-chat'))
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
test('TRUSTED_PLUGINS all have scoped package names', () => {
|
|
18
|
-
for (const plugin of TRUSTED_PLUGINS) {
|
|
19
|
-
assert.ok(plugin.package.startsWith('@antseed/'), `${plugin.name} package should be scoped`)
|
|
20
|
-
}
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
test('buildPluginConfig uses priority: instanceConfig < env < overrides', () => {
|
|
24
|
-
const keys = [
|
|
25
|
-
{ key: 'API_KEY', label: 'API Key', type: 'string' as const },
|
|
26
|
-
{ key: 'MODEL', label: 'Model', type: 'string' as const },
|
|
27
|
-
]
|
|
28
|
-
const instanceConfig = { API_KEY: 'from-instance', MODEL: 'from-instance' }
|
|
29
|
-
const original = process.env['API_KEY']
|
|
30
|
-
process.env['API_KEY'] = 'from-env'
|
|
31
|
-
try {
|
|
32
|
-
const result = buildPluginConfig(keys, { API_KEY: 'from-override' }, instanceConfig)
|
|
33
|
-
assert.equal(result['API_KEY'], 'from-override')
|
|
34
|
-
assert.equal(result['MODEL'], 'from-instance')
|
|
35
|
-
} finally {
|
|
36
|
-
if (original === undefined) {
|
|
37
|
-
delete process.env['API_KEY']
|
|
38
|
-
} else {
|
|
39
|
-
process.env['API_KEY'] = original
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
test('buildPluginConfig works without instanceConfig (backwards compatible)', () => {
|
|
45
|
-
const keys = [
|
|
46
|
-
{ key: 'TEST_KEY_COMPAT', label: 'Test', type: 'string' as const },
|
|
47
|
-
]
|
|
48
|
-
const result = buildPluginConfig(keys, { TEST_KEY_COMPAT: 'override' })
|
|
49
|
-
assert.equal(result['TEST_KEY_COMPAT'], 'override')
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
test('LEGACY_PACKAGE_MAP maps old names to scoped names', () => {
|
|
53
|
-
assert.equal(LEGACY_PACKAGE_MAP['antseed-provider-anthropic'], '@antseed/provider-claude-code')
|
|
54
|
-
assert.equal(LEGACY_PACKAGE_MAP['antseed-router-claude-code'], '@antseed/router-local-proxy')
|
|
55
|
-
})
|
|
@@ -1,295 +0,0 @@
|
|
|
1
|
-
import type { Command } from 'commander'
|
|
2
|
-
import chalk from 'chalk'
|
|
3
|
-
import ora from 'ora'
|
|
4
|
-
import { createInterface } from 'node:readline/promises'
|
|
5
|
-
import { join } from 'node:path'
|
|
6
|
-
import { homedir } from 'node:os'
|
|
7
|
-
import { installPlugin, removePlugin, listInstalledPlugins, getPluginsDir } from '../../plugins/manager.js'
|
|
8
|
-
import { TRUSTED_PLUGINS } from '../../plugins/registry.js'
|
|
9
|
-
import {
|
|
10
|
-
addInstance,
|
|
11
|
-
removeInstance,
|
|
12
|
-
getInstance,
|
|
13
|
-
getInstances,
|
|
14
|
-
updateInstanceConfig,
|
|
15
|
-
loadPluginModule,
|
|
16
|
-
} from '@antseed/node'
|
|
17
|
-
import type { ConfigField, AntseedPlugin } from '@antseed/node'
|
|
18
|
-
import { registerPluginCreateCommand } from './plugin-create.js'
|
|
19
|
-
|
|
20
|
-
const CONFIG_PATH = join(homedir(), '.antseed', 'config.json')
|
|
21
|
-
|
|
22
|
-
function generateInstanceId(pluginName: string): string {
|
|
23
|
-
const suffix = Math.random().toString(36).slice(2, 8)
|
|
24
|
-
return `${pluginName}-${suffix}`
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function getConfigSchema(plugin: AntseedPlugin): ConfigField[] {
|
|
28
|
-
return plugin.configSchema ?? plugin.configKeys ?? []
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function maskSecret(value: string): string {
|
|
32
|
-
if (value.length <= 4) return '****'
|
|
33
|
-
return '****' + value.slice(-4)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
async function promptForFields(
|
|
37
|
-
rl: ReturnType<typeof createInterface>,
|
|
38
|
-
schema: ConfigField[],
|
|
39
|
-
existingConfig?: Record<string, unknown>,
|
|
40
|
-
): Promise<Record<string, unknown>> {
|
|
41
|
-
const config: Record<string, unknown> = {}
|
|
42
|
-
for (const field of schema) {
|
|
43
|
-
const defaultVal = existingConfig?.[field.key] ?? field.default
|
|
44
|
-
const defaultHint = field.type === 'secret' && typeof defaultVal === 'string' && defaultVal
|
|
45
|
-
? ` [${maskSecret(defaultVal)}]`
|
|
46
|
-
: defaultVal !== undefined
|
|
47
|
-
? ` [${String(defaultVal)}]`
|
|
48
|
-
: ''
|
|
49
|
-
const requiredHint = field.required ? ' (required)' : ''
|
|
50
|
-
const desc = field.description ? ` — ${field.description}` : ''
|
|
51
|
-
const prompt = ` ${field.label}${requiredHint}${desc}${defaultHint}: `
|
|
52
|
-
|
|
53
|
-
let answer: string
|
|
54
|
-
if (field.type === 'secret') {
|
|
55
|
-
process.stdout.write(prompt)
|
|
56
|
-
const stdin = process.stdin
|
|
57
|
-
const wasRaw = stdin.isRaw
|
|
58
|
-
if (stdin.isTTY) stdin.setRawMode(true)
|
|
59
|
-
let secret = ''
|
|
60
|
-
answer = await new Promise<string>((resolve) => {
|
|
61
|
-
const onData = (chunk: Buffer) => {
|
|
62
|
-
const ch = chunk.toString()
|
|
63
|
-
if (ch === '\n' || ch === '\r') {
|
|
64
|
-
stdin.removeListener('data', onData)
|
|
65
|
-
if (stdin.isTTY && wasRaw !== undefined) stdin.setRawMode(wasRaw)
|
|
66
|
-
process.stdout.write('\n')
|
|
67
|
-
resolve(secret)
|
|
68
|
-
} else if (ch === '\u007f' || ch === '\b') {
|
|
69
|
-
secret = secret.slice(0, -1)
|
|
70
|
-
} else if (ch === '\u0003') {
|
|
71
|
-
stdin.removeListener('data', onData)
|
|
72
|
-
if (stdin.isTTY && wasRaw !== undefined) stdin.setRawMode(wasRaw)
|
|
73
|
-
process.exit(0)
|
|
74
|
-
} else {
|
|
75
|
-
secret += ch
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
stdin.on('data', onData)
|
|
79
|
-
})
|
|
80
|
-
} else {
|
|
81
|
-
answer = await rl.question(prompt)
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const trimmed = answer.trim()
|
|
85
|
-
if (trimmed) {
|
|
86
|
-
if (field.type === 'number') {
|
|
87
|
-
config[field.key] = Number(trimmed)
|
|
88
|
-
} else if (field.type === 'boolean') {
|
|
89
|
-
config[field.key] = trimmed === 'true' || trimmed === 'yes' || trimmed === '1'
|
|
90
|
-
} else {
|
|
91
|
-
config[field.key] = trimmed
|
|
92
|
-
}
|
|
93
|
-
} else if (defaultVal !== undefined) {
|
|
94
|
-
config[field.key] = defaultVal
|
|
95
|
-
} else if (field.required) {
|
|
96
|
-
console.log(chalk.red(` ${field.label} is required.`))
|
|
97
|
-
// Re-prompt by recursing on just this field
|
|
98
|
-
const retry = await promptForFields(rl, [field], existingConfig)
|
|
99
|
-
config[field.key] = retry[field.key]!
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return config
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export function registerPluginCommand(program: Command): void {
|
|
106
|
-
const pluginCmd = program
|
|
107
|
-
.command('plugin')
|
|
108
|
-
.description('Manage antseed plugins')
|
|
109
|
-
|
|
110
|
-
// Task 1: plugin add with interactive config
|
|
111
|
-
pluginCmd
|
|
112
|
-
.command('add <package>')
|
|
113
|
-
.description('Install a plugin from npm and configure an instance')
|
|
114
|
-
.action(async (packageName: string) => {
|
|
115
|
-
const spinner = ora(`Installing ${packageName}...`).start()
|
|
116
|
-
try {
|
|
117
|
-
await installPlugin(packageName)
|
|
118
|
-
spinner.succeed(chalk.green(`Installed ${packageName}`))
|
|
119
|
-
} catch (err) {
|
|
120
|
-
spinner.fail(chalk.red(`Failed to install ${packageName}: ${(err as Error).message}`))
|
|
121
|
-
process.exit(1)
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// Load plugin module to read configSchema
|
|
125
|
-
let plugin: AntseedPlugin
|
|
126
|
-
try {
|
|
127
|
-
plugin = await loadPluginModule(packageName, getPluginsDir())
|
|
128
|
-
} catch (err) {
|
|
129
|
-
console.log(chalk.yellow(`\nInstalled but could not load plugin module: ${(err as Error).message}`))
|
|
130
|
-
return
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const schema = getConfigSchema(plugin)
|
|
134
|
-
if (schema.length === 0) {
|
|
135
|
-
console.log(chalk.dim('\nPlugin has no configuration fields.'))
|
|
136
|
-
const instanceId = generateInstanceId(plugin.name)
|
|
137
|
-
await addInstance(CONFIG_PATH, {
|
|
138
|
-
id: instanceId,
|
|
139
|
-
package: packageName,
|
|
140
|
-
type: plugin.type,
|
|
141
|
-
config: {},
|
|
142
|
-
enabled: true,
|
|
143
|
-
createdAt: new Date().toISOString(),
|
|
144
|
-
}, schema)
|
|
145
|
-
console.log(chalk.green(`\nCreated instance: ${instanceId}`))
|
|
146
|
-
return
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
console.log(chalk.bold(`\nConfigure ${plugin.displayName}:\n`))
|
|
150
|
-
const rl = createInterface({ input: process.stdin, output: process.stdout })
|
|
151
|
-
try {
|
|
152
|
-
const config = await promptForFields(rl, schema)
|
|
153
|
-
const instanceId = generateInstanceId(plugin.name)
|
|
154
|
-
await addInstance(CONFIG_PATH, {
|
|
155
|
-
id: instanceId,
|
|
156
|
-
package: packageName,
|
|
157
|
-
type: plugin.type,
|
|
158
|
-
config,
|
|
159
|
-
enabled: true,
|
|
160
|
-
createdAt: new Date().toISOString(),
|
|
161
|
-
}, schema)
|
|
162
|
-
console.log(chalk.green(`\nCreated instance: ${instanceId}`))
|
|
163
|
-
} finally {
|
|
164
|
-
rl.close()
|
|
165
|
-
}
|
|
166
|
-
})
|
|
167
|
-
|
|
168
|
-
// Task 2: plugin config command
|
|
169
|
-
pluginCmd
|
|
170
|
-
.command('config <id>')
|
|
171
|
-
.description('Update configuration for a plugin instance')
|
|
172
|
-
.action(async (instanceId: string) => {
|
|
173
|
-
const instance = await getInstance(CONFIG_PATH, instanceId)
|
|
174
|
-
if (!instance) {
|
|
175
|
-
console.log(chalk.red(`Instance "${instanceId}" not found.`))
|
|
176
|
-
process.exit(1)
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
let plugin: AntseedPlugin
|
|
180
|
-
try {
|
|
181
|
-
plugin = await loadPluginModule(instance.package, getPluginsDir())
|
|
182
|
-
} catch (err) {
|
|
183
|
-
console.log(chalk.red(`Could not load plugin: ${(err as Error).message}`))
|
|
184
|
-
process.exit(1)
|
|
185
|
-
return // unreachable but satisfies TS
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
const schema = getConfigSchema(plugin)
|
|
189
|
-
if (schema.length === 0) {
|
|
190
|
-
console.log(chalk.dim('Plugin has no configuration fields.'))
|
|
191
|
-
return
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
console.log(chalk.bold(`\nConfigure ${plugin.displayName} (${instanceId}):\n`))
|
|
195
|
-
|
|
196
|
-
// Show current values
|
|
197
|
-
for (const field of schema) {
|
|
198
|
-
const current = instance.config[field.key]
|
|
199
|
-
if (current !== undefined) {
|
|
200
|
-
const display = field.type === 'secret' ? maskSecret(String(current)) : String(current)
|
|
201
|
-
console.log(chalk.dim(` Current ${field.label}: ${display}`))
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
console.log('')
|
|
205
|
-
|
|
206
|
-
const rl = createInterface({ input: process.stdin, output: process.stdout })
|
|
207
|
-
try {
|
|
208
|
-
const newConfig = await promptForFields(rl, schema, instance.config)
|
|
209
|
-
await updateInstanceConfig(CONFIG_PATH, instanceId, newConfig, schema)
|
|
210
|
-
console.log(chalk.green(`\nUpdated instance: ${instanceId}`))
|
|
211
|
-
} finally {
|
|
212
|
-
rl.close()
|
|
213
|
-
}
|
|
214
|
-
})
|
|
215
|
-
|
|
216
|
-
// Task 5: plugin remove with instance handling
|
|
217
|
-
pluginCmd
|
|
218
|
-
.command('remove <name>')
|
|
219
|
-
.description('Remove a plugin instance or uninstall a package')
|
|
220
|
-
.action(async (name: string) => {
|
|
221
|
-
// Check if name matches an instance ID
|
|
222
|
-
const instance = await getInstance(CONFIG_PATH, name)
|
|
223
|
-
if (instance) {
|
|
224
|
-
const rl = createInterface({ input: process.stdin, output: process.stdout })
|
|
225
|
-
try {
|
|
226
|
-
const confirm = await rl.question(`Remove instance "${name}"? (y/N): `)
|
|
227
|
-
if (confirm.trim().toLowerCase() !== 'y') {
|
|
228
|
-
console.log(chalk.dim('Cancelled.'))
|
|
229
|
-
return
|
|
230
|
-
}
|
|
231
|
-
await removeInstance(CONFIG_PATH, name)
|
|
232
|
-
console.log(chalk.green(`Removed instance: ${name}`))
|
|
233
|
-
|
|
234
|
-
const uninstall = await rl.question(`Also uninstall npm package "${instance.package}"? (y/N): `)
|
|
235
|
-
if (uninstall.trim().toLowerCase() === 'y') {
|
|
236
|
-
const spinner = ora(`Removing ${instance.package}...`).start()
|
|
237
|
-
try {
|
|
238
|
-
await removePlugin(instance.package)
|
|
239
|
-
spinner.succeed(chalk.green(`Uninstalled ${instance.package}`))
|
|
240
|
-
} catch (err) {
|
|
241
|
-
spinner.fail(chalk.red(`Failed to uninstall: ${(err as Error).message}`))
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
} finally {
|
|
245
|
-
rl.close()
|
|
246
|
-
}
|
|
247
|
-
return
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
// Fall through: treat as npm package name
|
|
251
|
-
const spinner = ora(`Removing ${name}...`).start()
|
|
252
|
-
try {
|
|
253
|
-
await removePlugin(name)
|
|
254
|
-
spinner.succeed(chalk.green(`Removed ${name}`))
|
|
255
|
-
} catch (err) {
|
|
256
|
-
spinner.fail(chalk.red(`Failed to remove ${name}: ${(err as Error).message}`))
|
|
257
|
-
process.exit(1)
|
|
258
|
-
}
|
|
259
|
-
})
|
|
260
|
-
|
|
261
|
-
// Task 4: plugin list with instances
|
|
262
|
-
pluginCmd
|
|
263
|
-
.command('list')
|
|
264
|
-
.description('List installed plugins and configured instances')
|
|
265
|
-
.action(async () => {
|
|
266
|
-
const installed = await listInstalledPlugins()
|
|
267
|
-
const trusted = new Set(TRUSTED_PLUGINS.map((p) => p.package))
|
|
268
|
-
|
|
269
|
-
if (installed.length === 0) {
|
|
270
|
-
console.log(chalk.dim('No plugins installed. Run: antseed init'))
|
|
271
|
-
} else {
|
|
272
|
-
console.log(chalk.bold('\nInstalled plugins:\n'))
|
|
273
|
-
for (const plugin of installed) {
|
|
274
|
-
const isTrusted = trusted.has(plugin.package)
|
|
275
|
-
const badge = isTrusted ? chalk.green(' [trusted]') : ''
|
|
276
|
-
console.log(` ${chalk.cyan(plugin.package)} ${chalk.dim(plugin.version)}${badge}`)
|
|
277
|
-
}
|
|
278
|
-
console.log('')
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
// Show configured instances
|
|
282
|
-
const instances = await getInstances(CONFIG_PATH)
|
|
283
|
-
if (instances.length > 0) {
|
|
284
|
-
console.log(chalk.bold('Configured instances:\n'))
|
|
285
|
-
for (const inst of instances) {
|
|
286
|
-
const status = inst.enabled === false ? chalk.red(' [disabled]') : chalk.green(' [enabled]')
|
|
287
|
-
console.log(` ${chalk.cyan(inst.id)} ${chalk.dim(inst.package)}${status}`)
|
|
288
|
-
}
|
|
289
|
-
console.log('')
|
|
290
|
-
}
|
|
291
|
-
})
|
|
292
|
-
|
|
293
|
-
// Register plugin create subcommand (Task 3)
|
|
294
|
-
registerPluginCreateCommand(pluginCmd)
|
|
295
|
-
}
|