@botconnector/bccli 0.5.0 → 0.5.1
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 +21 -203
- package/{legacy/dist/chunk-XP5MTJN2.js → dist/chunk-6YRTPXGY.js} +1 -1
- package/{legacy/dist → dist}/cli.js +22 -4
- package/dist/onboarding.d.ts +6 -0
- package/{legacy/dist/serve-HUQNVOSP.js → dist/serve-R2IHP26A.js} +1 -1
- package/{legacy/dist/update-5V425FIK.js → dist/update-XC3B6SJC.js} +1 -1
- package/package.json +51 -47
- package/THIRD_PARTY_NOTICES.md +0 -9
- package/branding.mjs +0 -347
- package/cli.mjs +0 -128
- package/core.mjs +0 -260
- package/credentials.mjs +0 -52
- package/gateway.mjs +0 -2
- package/legacy/LICENSE +0 -21
- package/legacy/package.json +0 -77
- package/legacy-compat.mjs +0 -42
- package/model-command.mjs +0 -61
- package/provider-cli.mjs +0 -76
- package/providers.mjs +0 -179
- package/proxy.mjs +0 -242
- package/startup-menu.mjs +0 -121
- /package/{legacy/README.en.md → README.en.md} +0 -0
- /package/{legacy/README.md → README.md} +0 -0
- /package/{legacy/dist → dist}/abort.d.ts +0 -0
- /package/{legacy/dist → dist}/acp-HWHF25KU.js +0 -0
- /package/{legacy/dist → dist}/acp.d.ts +0 -0
- /package/{legacy/dist → dist}/agent.d.ts +0 -0
- /package/{legacy/dist → dist}/agents.d.ts +0 -0
- /package/{legacy/dist → dist}/args-XA77XUMD.js +0 -0
- /package/{legacy/dist → dist}/args.d.ts +0 -0
- /package/{legacy/dist → dist}/budget.d.ts +0 -0
- /package/{legacy/dist → dist}/checkpoints.d.ts +0 -0
- /package/{legacy/dist → dist}/chunk-2IP5F3EK.js +0 -0
- /package/{legacy/dist → dist}/chunk-5CVHTYBQ.js +0 -0
- /package/{legacy/dist → dist}/chunk-6LO6YRDY.js +0 -0
- /package/{legacy/dist → dist}/chunk-6PJRWKVI.js +0 -0
- /package/{legacy/dist → dist}/chunk-7OTFZK4B.js +0 -0
- /package/{legacy/dist → dist}/chunk-7U7CMKKI.js +0 -0
- /package/{legacy/dist → dist}/chunk-A3NBWIEQ.js +0 -0
- /package/{legacy/dist → dist}/chunk-FY543EQW.js +0 -0
- /package/{legacy/dist → dist}/chunk-IDZLHEVL.js +0 -0
- /package/{legacy/dist → dist}/chunk-IF5RNXBI.js +0 -0
- /package/{legacy/dist → dist}/chunk-M5T4AM4I.js +0 -0
- /package/{legacy/dist → dist}/chunk-NGFWBVHD.js +0 -0
- /package/{legacy/dist → dist}/chunk-OCEQ2TRJ.js +0 -0
- /package/{legacy/dist → dist}/chunk-OK2USAXX.js +0 -0
- /package/{legacy/dist → dist}/chunk-PSO7R5AD.js +0 -0
- /package/{legacy/dist → dist}/chunk-RLSQAZBT.js +0 -0
- /package/{legacy/dist → dist}/chunk-VPFCIUC6.js +0 -0
- /package/{legacy/dist → dist}/chunk-YGBTNDPK.js +0 -0
- /package/{legacy/dist → dist}/chunk-YYTFJUO2.js +0 -0
- /package/{legacy/dist → dist}/chunk-ZXWRLN7E.js +0 -0
- /package/{legacy/dist → dist}/cli.d.ts +0 -0
- /package/{legacy/dist → dist}/clipboardImage.d.ts +0 -0
- /package/{legacy/dist → dist}/commands.d.ts +0 -0
- /package/{legacy/dist → dist}/completion-IRVEVW6H.js +0 -0
- /package/{legacy/dist → dist}/completion.d.ts +0 -0
- /package/{legacy/dist → dist}/config-OVWCUVY7.js +0 -0
- /package/{legacy/dist → dist}/config-S3CZUIBB.js +0 -0
- /package/{legacy/dist → dist}/config.d.ts +0 -0
- /package/{legacy/dist → dist}/context.d.ts +0 -0
- /package/{legacy/dist → dist}/contextWindow.d.ts +0 -0
- /package/{legacy/dist → dist}/diff.d.ts +0 -0
- /package/{legacy/dist → dist}/exit.d.ts +0 -0
- /package/{legacy/dist → dist}/extensions.d.ts +0 -0
- /package/{legacy/dist → dist}/ghBotCli-FYKJDJOD.js +0 -0
- /package/{legacy/dist → dist}/ghBotCli.d.ts +0 -0
- /package/{legacy/dist → dist}/ghBotDispatch.d.ts +0 -0
- /package/{legacy/dist → dist}/ghBotRunner.d.ts +0 -0
- /package/{legacy/dist → dist}/ghBotServe.d.ts +0 -0
- /package/{legacy/dist → dist}/ghBotWebhook.d.ts +0 -0
- /package/{legacy/dist → dist}/hooks.d.ts +0 -0
- /package/{legacy/dist → dist}/i18n/id.d.ts +0 -0
- /package/{legacy/dist → dist}/i18n.d.ts +0 -0
- /package/{legacy/dist → dist}/integrationCli-GY73S2K7.js +0 -0
- /package/{legacy/dist → dist}/integrationCli.d.ts +0 -0
- /package/{legacy/dist → dist}/keybinds.d.ts +0 -0
- /package/{legacy/dist → dist}/login-KSN63EM4.js +0 -0
- /package/{legacy/dist → dist}/login.d.ts +0 -0
- /package/{legacy/dist → dist}/lsp.d.ts +0 -0
- /package/{legacy/dist → dist}/mcp/catalog.d.ts +0 -0
- /package/{legacy/dist → dist}/mcp/config.d.ts +0 -0
- /package/{legacy/dist → dist}/mcp/manager.d.ts +0 -0
- /package/{legacy/dist → dist}/mcp/oauth.d.ts +0 -0
- /package/{legacy/dist → dist}/mcpCli-VWSCCDWT.js +0 -0
- /package/{legacy/dist → dist}/mcpCli.d.ts +0 -0
- /package/{legacy/dist → dist}/mcpServer/serve.d.ts +0 -0
- /package/{legacy/dist → dist}/mcpServer/server.d.ts +0 -0
- /package/{legacy/dist → dist}/models.d.ts +0 -0
- /package/{legacy/dist → dist}/modelsCli-4Q6KFWV2.js +0 -0
- /package/{legacy/dist → dist}/modelsCli.d.ts +0 -0
- /package/{legacy/dist → dist}/npmRunner-LU43GM3W.js +0 -0
- /package/{legacy/dist → dist}/npmRunner.d.ts +0 -0
- /package/{legacy/dist → dist}/oauthLogin-JWXQZTMZ.js +0 -0
- /package/{legacy/dist → dist}/oauthLogin.d.ts +0 -0
- /package/{legacy/dist → dist}/permissions.d.ts +0 -0
- /package/{legacy/dist → dist}/pluginSpec.d.ts +0 -0
- /package/{legacy/dist → dist}/plugins.d.ts +0 -0
- /package/{legacy/dist → dist}/pluginsCli-7XWDLJEL.js +0 -0
- /package/{legacy/dist → dist}/pluginsCli.d.ts +0 -0
- /package/{legacy/dist → dist}/prCli-FH7N26D4.js +0 -0
- /package/{legacy/dist → dist}/prCli.d.ts +0 -0
- /package/{legacy/dist → dist}/presets.d.ts +0 -0
- /package/{legacy/dist → dist}/print.d.ts +0 -0
- /package/{legacy/dist → dist}/provider.d.ts +0 -0
- /package/{legacy/dist → dist}/providerCli-YMG4G56R.js +0 -0
- /package/{legacy/dist → dist}/providerCli.d.ts +0 -0
- /package/{legacy/dist → dist}/providers.d.ts +0 -0
- /package/{legacy/dist → dist}/reasoning.d.ts +0 -0
- /package/{legacy/dist → dist}/repomap.d.ts +0 -0
- /package/{legacy/dist → dist}/sdk.d.ts +0 -0
- /package/{legacy/dist → dist}/sdk.js +0 -0
- /package/{legacy/dist → dist}/serve-HSFF6MGB.js +0 -0
- /package/{legacy/dist → dist}/serve.d.ts +0 -0
- /package/{legacy/dist → dist}/serveHeartbeat.d.ts +0 -0
- /package/{legacy/dist → dist}/serveProtocol.d.ts +0 -0
- /package/{legacy/dist → dist}/session-YM36TGU7.js +0 -0
- /package/{legacy/dist → dist}/session.d.ts +0 -0
- /package/{legacy/dist → dist}/setup-3YB6EKYX.js +0 -0
- /package/{legacy/dist → dist}/setup.d.ts +0 -0
- /package/{legacy/dist → dist}/slash/copy.d.ts +0 -0
- /package/{legacy/dist → dist}/slash/diff.d.ts +0 -0
- /package/{legacy/dist → dist}/slash/doctor.d.ts +0 -0
- /package/{legacy/dist → dist}/slash/export.d.ts +0 -0
- /package/{legacy/dist → dist}/slash/info.d.ts +0 -0
- /package/{legacy/dist → dist}/slash/memory.d.ts +0 -0
- /package/{legacy/dist → dist}/statsCli-I4TSDU3X.js +0 -0
- /package/{legacy/dist → dist}/statsCli.d.ts +0 -0
- /package/{legacy/dist → dist}/textToolCalls.d.ts +0 -0
- /package/{legacy/dist → dist}/thinking.d.ts +0 -0
- /package/{legacy/dist → dist}/tokenBudget.d.ts +0 -0
- /package/{legacy/dist → dist}/tools/bash.d.ts +0 -0
- /package/{legacy/dist → dist}/tools/diagnostics.d.ts +0 -0
- /package/{legacy/dist → dist}/tools/edit.d.ts +0 -0
- /package/{legacy/dist → dist}/tools/fetch.d.ts +0 -0
- /package/{legacy/dist → dist}/tools/glob.d.ts +0 -0
- /package/{legacy/dist → dist}/tools/globSafety.d.ts +0 -0
- /package/{legacy/dist → dist}/tools/grep.d.ts +0 -0
- /package/{legacy/dist → dist}/tools/ignore.d.ts +0 -0
- /package/{legacy/dist → dist}/tools/index.d.ts +0 -0
- /package/{legacy/dist → dist}/tools/memory.d.ts +0 -0
- /package/{legacy/dist → dist}/tools/paths.d.ts +0 -0
- /package/{legacy/dist → dist}/tools/plan.d.ts +0 -0
- /package/{legacy/dist → dist}/tools/read.d.ts +0 -0
- /package/{legacy/dist → dist}/tools/repomap.d.ts +0 -0
- /package/{legacy/dist → dist}/tools/sandbox.d.ts +0 -0
- /package/{legacy/dist → dist}/tools/shell.d.ts +0 -0
- /package/{legacy/dist → dist}/tools/skill.d.ts +0 -0
- /package/{legacy/dist → dist}/tools/task.d.ts +0 -0
- /package/{legacy/dist → dist}/tools/todo.d.ts +0 -0
- /package/{legacy/dist → dist}/tools/types.d.ts +0 -0
- /package/{legacy/dist → dist}/tools/websearch.d.ts +0 -0
- /package/{legacy/dist → dist}/tools/write.d.ts +0 -0
- /package/{legacy/dist → dist}/transferCli-3B4LMOYI.js +0 -0
- /package/{legacy/dist → dist}/transferCli.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/App.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/DiffView.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/EditableText.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/LinePrompt.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/ListPicker.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/Markdown.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/McpMenu.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/ModelPicker.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/PermissionPrompt.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/PlanApproval.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/PromptInput.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/ProviderMenu.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/Spinner.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/StatusBar.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/TodoList.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/ToolBlock.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/complete.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/index.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/lineEdit.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/liveCap.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/markdownLayout.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/replay.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/stdinFilter.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/table.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/theme.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/thinkingMouse.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/transcript.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/tty.d.ts +0 -0
- /package/{legacy/dist → dist}/ui/vim.d.ts +0 -0
- /package/{legacy/dist → dist}/ui-TAZMRXRI.js +0 -0
- /package/{legacy/dist → dist}/update.d.ts +0 -0
- /package/{legacy/dist → dist}/verify.d.ts +0 -0
- /package/{legacy/dist → dist}/version.d.ts +0 -0
- /package/{legacy/dist → dist}/webBudget.d.ts +0 -0
- /package/{legacy/dist → dist}/worktree.d.ts +0 -0
package/providers.mjs
DELETED
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
// BCCLI Preview provider keys: explicit OpenAI-compatible presets only.
|
|
2
|
-
// Windows stores provider API keys as per-user DPAPI encrypted blobs. The
|
|
3
|
-
// upstream Qwen process receives only an ephemeral loopback proxy token.
|
|
4
|
-
import { randomBytes } from 'node:crypto'
|
|
5
|
-
import { spawnSync } from 'node:child_process'
|
|
6
|
-
import { existsSync, lstatSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs'
|
|
7
|
-
import { homedir } from 'node:os'
|
|
8
|
-
import { isAbsolute, join } from 'node:path'
|
|
9
|
-
import { botConnectorCredentialStatus, botConnectorKey } from './credentials.mjs'
|
|
10
|
-
import { GATEWAY } from './gateway.mjs'
|
|
11
|
-
|
|
12
|
-
export const PROVIDERS = Object.freeze({
|
|
13
|
-
botconnector: Object.freeze({ name: 'BotConnector Cloud', endpoint: GATEWAY }),
|
|
14
|
-
openai: Object.freeze({ name: 'OpenAI API', endpoint: 'https://api.openai.com/v1', env: 'OPENAI_API_KEY' }),
|
|
15
|
-
openrouter: Object.freeze({ name: 'OpenRouter', endpoint: 'https://openrouter.ai/api/v1', env: 'OPENROUTER_API_KEY' }),
|
|
16
|
-
deepseek: Object.freeze({ name: 'DeepSeek', endpoint: 'https://api.deepseek.com/v1', env: 'DEEPSEEK_API_KEY' }),
|
|
17
|
-
groq: Object.freeze({ name: 'Groq', endpoint: 'https://api.groq.com/openai/v1', env: 'GROQ_API_KEY' }),
|
|
18
|
-
xai: Object.freeze({ name: 'xAI', endpoint: 'https://api.x.ai/v1', env: 'XAI_API_KEY' }),
|
|
19
|
-
})
|
|
20
|
-
export const ALLOWED_PROVIDER_ENDPOINTS = Object.freeze(Object.values(PROVIDERS).map(p => p.endpoint))
|
|
21
|
-
const ID = /^[a-z][a-z0-9-]{0,31}$/
|
|
22
|
-
const MAX_BLOB_BYTES = 64 * 1024
|
|
23
|
-
const ENC_SCRIPT = '$ErrorActionPreference="Stop"; $plain=[Console]::In.ReadToEnd().Trim(); if(-not $plain){exit 1}; $sec=ConvertTo-SecureString -String $plain -AsPlainText -Force; [Console]::Out.Write((ConvertFrom-SecureString -SecureString $sec))'
|
|
24
|
-
const DEC_SCRIPT = '$ErrorActionPreference="Stop"; $encoded=[Console]::In.ReadToEnd().Trim(); $sec=ConvertTo-SecureString -String $encoded; $ptr=[Runtime.InteropServices.Marshal]::SecureStringToBSTR($sec); try{[Console]::Out.Write([Runtime.InteropServices.Marshal]::PtrToStringBSTR($ptr))}finally{[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ptr)}'
|
|
25
|
-
|
|
26
|
-
export function providerRoot({ home = homedir(), env = process.env } = {}) {
|
|
27
|
-
const path = env.BCCLI_PREVIEW_HOME || join(home, '.bccli-preview')
|
|
28
|
-
if (!isAbsolute(path)) throw Error('BCCLI_PREVIEW_HOME must be absolute.')
|
|
29
|
-
return path
|
|
30
|
-
}
|
|
31
|
-
export function providerDefinition(id) {
|
|
32
|
-
if (!Object.hasOwn(PROVIDERS, id)) throw Error('Unknown provider. Choose from: ' + Object.keys(PROVIDERS).join(', '))
|
|
33
|
-
return PROVIDERS[id]
|
|
34
|
-
}
|
|
35
|
-
function ownerDirectory(root, { create = false } = {}) {
|
|
36
|
-
if (create) mkdirSync(root, { recursive: true, mode: 0o700 })
|
|
37
|
-
if (!existsSync(root)) return false
|
|
38
|
-
const info = lstatSync(root)
|
|
39
|
-
if (!info.isDirectory() || info.isSymbolicLink()) throw Error('Preview credential directory is not a safe regular directory.')
|
|
40
|
-
return true
|
|
41
|
-
}
|
|
42
|
-
function keyPath(root, id) {
|
|
43
|
-
if (!ID.test(id)) throw Error('Provider key storage requires a supported provider.')
|
|
44
|
-
return join(root, 'provider-' + id + '.dpapi')
|
|
45
|
-
}
|
|
46
|
-
function dpapi(script, input, { platform = process.platform, runner = spawnSync } = {}) {
|
|
47
|
-
if (platform !== 'win32') throw Error('Persisting provider API keys currently requires Windows DPAPI. Use the provider environment variable on other systems.')
|
|
48
|
-
const result = runner('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', script], {
|
|
49
|
-
input, encoding: 'utf8', timeout: 15000, maxBuffer: MAX_BLOB_BYTES, windowsHide: true,
|
|
50
|
-
env: { ...process.env, PSModulePath: process.env.PSModulePath },
|
|
51
|
-
})
|
|
52
|
-
if (result.error || result.status !== 0 || !result.stdout?.trim()) throw Error('Windows protected credential operation failed; no key was printed.')
|
|
53
|
-
return result.stdout.trim()
|
|
54
|
-
}
|
|
55
|
-
export function saveProviderKey(id, key, { root = providerRoot(), platform = process.platform, runner = spawnSync } = {}) {
|
|
56
|
-
providerDefinition(id)
|
|
57
|
-
if (typeof key !== 'string' || !key.trim() || key.length > 8192 || /[\r\n]/.test(key)) throw Error('API key cannot be empty, multiline, or oversized.')
|
|
58
|
-
if (platform !== 'win32') throw Error('On non-Windows systems, use the provider API key environment variable.')
|
|
59
|
-
ownerDirectory(root, { create: true })
|
|
60
|
-
const ciphertext = dpapi(ENC_SCRIPT, key.trim(), { platform, runner })
|
|
61
|
-
if (ciphertext.length > MAX_BLOB_BYTES || !/^[0-9a-f]+$/i.test(ciphertext)) throw Error('Windows credential encryption returned an invalid format.')
|
|
62
|
-
const path = keyPath(root, id)
|
|
63
|
-
if (existsSync(path) && (!lstatSync(path).isFile() || lstatSync(path).isSymbolicLink())) throw Error('Unsafe provider credentials path.')
|
|
64
|
-
const temporary = path + '.' + randomBytes(8).toString('hex') + '.tmp'
|
|
65
|
-
try {
|
|
66
|
-
writeFileSync(temporary, ciphertext + '\n', { encoding: 'utf8', flag: 'wx', mode: 0o600 })
|
|
67
|
-
renameSync(temporary, path)
|
|
68
|
-
} finally { rmSync(temporary, { force: true }) }
|
|
69
|
-
}
|
|
70
|
-
export function storedProviderKey(id, { root = providerRoot(), platform = process.platform, runner = spawnSync } = {}) {
|
|
71
|
-
providerDefinition(id)
|
|
72
|
-
if (!ownerDirectory(root)) throw Error('Provider not configured. Run provider add.')
|
|
73
|
-
const path = keyPath(root, id)
|
|
74
|
-
let info
|
|
75
|
-
try { info = lstatSync(path) }
|
|
76
|
-
catch { throw Error('Provider not configured. Run provider add.') }
|
|
77
|
-
if (!info.isFile() || info.isSymbolicLink() || info.size > MAX_BLOB_BYTES) throw Error('Unsafe provider credentials file.')
|
|
78
|
-
const cipher = readFileSync(path, 'utf8')
|
|
79
|
-
if (!/^[0-9a-f]+\s*$/i.test(cipher)) throw Error('Provider credentials are invalid.')
|
|
80
|
-
return dpapi(DEC_SCRIPT, cipher, { platform, runner })
|
|
81
|
-
}
|
|
82
|
-
export function isConfigured(id, { env = process.env, root = providerRoot({env}) } = {}) {
|
|
83
|
-
providerDefinition(id)
|
|
84
|
-
if (id === 'botconnector') {
|
|
85
|
-
if (env.BOTCONNECTOR_API_KEY?.trim()) return true
|
|
86
|
-
try {
|
|
87
|
-
if (ownerDirectory(root)) {
|
|
88
|
-
const info = lstatSync(keyPath(root, id))
|
|
89
|
-
if (info.isFile() && !info.isSymbolicLink() && info.size <= MAX_BLOB_BYTES) return true
|
|
90
|
-
}
|
|
91
|
-
} catch {}
|
|
92
|
-
return botConnectorCredentialStatus({ env }).configured
|
|
93
|
-
}
|
|
94
|
-
if (env[PROVIDERS[id].env]?.trim()) return true
|
|
95
|
-
try { return ownerDirectory(root) && lstatSync(keyPath(root, id)).isFile() && !lstatSync(keyPath(root, id)).isSymbolicLink() }
|
|
96
|
-
catch { return false }
|
|
97
|
-
}
|
|
98
|
-
export function selectedProvider({ env = process.env, root = providerRoot({env}) } = {}) {
|
|
99
|
-
if (!ownerDirectory(root)) return 'botconnector'
|
|
100
|
-
const path = join(root, 'active-provider')
|
|
101
|
-
if (!existsSync(path)) return 'botconnector'
|
|
102
|
-
const info = lstatSync(path)
|
|
103
|
-
if (!info.isFile() || info.isSymbolicLink() || info.size > 128) throw Error('Unsafe provider selection file.')
|
|
104
|
-
const id = readFileSync(path, 'utf8').trim()
|
|
105
|
-
providerDefinition(id)
|
|
106
|
-
return id
|
|
107
|
-
}
|
|
108
|
-
// Model IDs are public selections, never credentials. Keep one per provider
|
|
109
|
-
// so a returning user goes directly to chat without repeating the model picker.
|
|
110
|
-
const MODEL_ID = /^[a-zA-Z0-9][a-zA-Z0-9._:/+-]{0,255}$/
|
|
111
|
-
export function selectedModel(id, { env = process.env, root = providerRoot({ env }) } = {}) {
|
|
112
|
-
providerDefinition(id)
|
|
113
|
-
if (!ownerDirectory(root)) return undefined
|
|
114
|
-
const path = join(root, 'active-model-' + id)
|
|
115
|
-
if (!existsSync(path)) return undefined
|
|
116
|
-
const info = lstatSync(path)
|
|
117
|
-
if (!info.isFile() || info.isSymbolicLink() || info.size > 300) throw Error('Unsafe model selection file.')
|
|
118
|
-
const value = readFileSync(path, 'utf8').trim()
|
|
119
|
-
if (!MODEL_ID.test(value)) throw Error('Invalid saved model selection.')
|
|
120
|
-
return value
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export function saveSelectedModel(id, model, { env = process.env, root = providerRoot({ env }) } = {}) {
|
|
124
|
-
providerDefinition(id)
|
|
125
|
-
if (!MODEL_ID.test(model)) throw Error('Invalid model ID.')
|
|
126
|
-
ownerDirectory(root, { create: true })
|
|
127
|
-
const path = join(root, 'active-model-' + id)
|
|
128
|
-
if (existsSync(path) && (!lstatSync(path).isFile() || lstatSync(path).isSymbolicLink())) {
|
|
129
|
-
throw Error('Unsafe model selection path.')
|
|
130
|
-
}
|
|
131
|
-
const tmp = path + '.' + randomBytes(8).toString('hex') + '.tmp'
|
|
132
|
-
try { writeFileSync(tmp, model + '\n', { flag: 'wx', encoding: 'utf8', mode: 0o600 }); renameSync(tmp, path) }
|
|
133
|
-
finally { rmSync(tmp, { force: true }) }
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
export function useProvider(id, { env = process.env, root = providerRoot({env}) } = {}) {
|
|
137
|
-
providerDefinition(id)
|
|
138
|
-
if (!isConfigured(id, { env, root })) throw Error('Provider is not configured. Use: provider add ' + id + ' (or its API key environment variable).')
|
|
139
|
-
ownerDirectory(root, { create: true })
|
|
140
|
-
const path = join(root, 'active-provider')
|
|
141
|
-
if (existsSync(path) && (!lstatSync(path).isFile() || lstatSync(path).isSymbolicLink())) throw Error('Unsafe provider selection path.')
|
|
142
|
-
const tmp = path + '.' + randomBytes(8).toString('hex') + '.tmp'
|
|
143
|
-
try { writeFileSync(tmp, id + '\n', { flag: 'wx', encoding: 'utf8', mode: 0o600 }); renameSync(tmp, path) }
|
|
144
|
-
finally { rmSync(tmp, { force: true }) }
|
|
145
|
-
}
|
|
146
|
-
export function removeProvider(id, { env = process.env, root = providerRoot({env}) } = {}) {
|
|
147
|
-
providerDefinition(id)
|
|
148
|
-
if (!ownerDirectory(root)) return
|
|
149
|
-
const path = keyPath(root, id)
|
|
150
|
-
if (existsSync(path)) {
|
|
151
|
-
if (!lstatSync(path).isFile() || lstatSync(path).isSymbolicLink()) throw Error('Unsafe provider credential path.')
|
|
152
|
-
rmSync(path)
|
|
153
|
-
}
|
|
154
|
-
if (selectedProvider({env,root}) === id) {
|
|
155
|
-
const active = join(root, 'active-provider')
|
|
156
|
-
rmSync(active, {force:true})
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
export function resolveProvider(id, { env = process.env, root = providerRoot({env}) } = {}) {
|
|
160
|
-
const info = providerDefinition(id)
|
|
161
|
-
if (id === 'botconnector') {
|
|
162
|
-
// Saved keys take precedence; legacy credentials are read only as fallback.
|
|
163
|
-
try { return { id, ...info, key: storedProviderKey(id, { root }) } }
|
|
164
|
-
catch (error) {
|
|
165
|
-
if (error?.message !== 'Provider not configured. Run provider add.') throw error
|
|
166
|
-
return { id, ...info, key: botConnectorKey({ env }) }
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
// An explicit key saved through "provider add" must win over stale environment
|
|
170
|
-
// variables. Never silently fall back if the encrypted file is corrupted.
|
|
171
|
-
let key
|
|
172
|
-
try { key = storedProviderKey(id, {root}) }
|
|
173
|
-
catch (error) {
|
|
174
|
-
if (error?.message !== 'Provider not configured. Run provider add.') throw error
|
|
175
|
-
key = env[info.env]?.trim()
|
|
176
|
-
}
|
|
177
|
-
if (!key) throw Error('Provider not configured. Run provider add ' + id + ' or set ' + info.env + '.')
|
|
178
|
-
return { id, ...info, key }
|
|
179
|
-
}
|
package/proxy.mjs
DELETED
|
@@ -1,242 +0,0 @@
|
|
|
1
|
-
import { randomBytes, timingSafeEqual } from 'node:crypto'
|
|
2
|
-
import { createServer } from 'node:http'
|
|
3
|
-
import { GATEWAY } from './gateway.mjs'
|
|
4
|
-
import { ALLOWED_PROVIDER_ENDPOINTS } from './providers.mjs'
|
|
5
|
-
|
|
6
|
-
// This proxy lives only for one Qwen Code session. Real provider keys remain in
|
|
7
|
-
// the launcher process; MCP and bash subprocesses can see only a short-lived
|
|
8
|
-
// local bearer token. The proxy cannot be redirected to arbitrary hosts.
|
|
9
|
-
const ALLOWED_PATHS = new Set(['/v1/models', '/v1/chat/completions', '/v1/responses'])
|
|
10
|
-
const MAX_REQUEST_BYTES = 16 * 1024 * 1024
|
|
11
|
-
|
|
12
|
-
// Qwen 0.26.3 produces get_goal without parameters and read_file with
|
|
13
|
-
// optional JSON-schema type unions such as ["integer", "null"]. Strict
|
|
14
|
-
// OpenAI-compatible providers reject both formats while parsing tools.
|
|
15
|
-
// Normalize only the incompatible pieces, preserving all coding tools.
|
|
16
|
-
function normalizeOptionalNullableSchema(schema) {
|
|
17
|
-
if (!schema || typeof schema !== 'object' || Array.isArray(schema)) return schema
|
|
18
|
-
let out = schema
|
|
19
|
-
if (schema.properties && typeof schema.properties === 'object' && !Array.isArray(schema.properties)) {
|
|
20
|
-
const required = new Set(Array.isArray(schema.required) ? schema.required : [])
|
|
21
|
-
let properties = schema.properties
|
|
22
|
-
for (const [name, field] of Object.entries(schema.properties)) {
|
|
23
|
-
if (!field || typeof field !== 'object' || Array.isArray(field)) continue
|
|
24
|
-
let candidate = field
|
|
25
|
-
// The optional absence of a field already represents null; dropping
|
|
26
|
-
// "null" from this one optional JSON-Schema union is safe. Do not rewrite
|
|
27
|
-
// required nullable fields or multi-type unions (fail closed upstream).
|
|
28
|
-
if (!required.has(name) && Array.isArray(field.type) && field.type.length === 2
|
|
29
|
-
&& field.type.includes('null')) {
|
|
30
|
-
const primitive = field.type.find((type) => type !== 'null')
|
|
31
|
-
if (['string', 'number', 'integer', 'boolean', 'object', 'array'].includes(primitive)) {
|
|
32
|
-
candidate = { ...field, type: primitive }
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
candidate = normalizeOptionalNullableSchema(candidate)
|
|
36
|
-
if (candidate !== field) {
|
|
37
|
-
if (properties === schema.properties) properties = { ...properties }
|
|
38
|
-
properties[name] = candidate
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
if (properties !== schema.properties) out = { ...out, properties }
|
|
42
|
-
}
|
|
43
|
-
if (schema.items && typeof schema.items === 'object' && !Array.isArray(schema.items)) {
|
|
44
|
-
const items = normalizeOptionalNullableSchema(schema.items)
|
|
45
|
-
if (items !== schema.items) out = { ...out, items }
|
|
46
|
-
}
|
|
47
|
-
return out
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function normalizeZeroArgFunctionTools(requestBody) {
|
|
51
|
-
if (!requestBody || typeof requestBody !== 'object' || !Array.isArray(requestBody.tools)) return requestBody
|
|
52
|
-
let changed = false
|
|
53
|
-
const tools = requestBody.tools.map((tool) => {
|
|
54
|
-
const fn = tool?.type === 'function' ? tool.function : null
|
|
55
|
-
if (!fn || typeof fn.name !== 'string') return tool
|
|
56
|
-
const parameters = fn.parameters == null
|
|
57
|
-
? { type: 'object', properties: {} }
|
|
58
|
-
: normalizeOptionalNullableSchema(fn.parameters)
|
|
59
|
-
if (parameters === fn.parameters) return tool
|
|
60
|
-
changed = true
|
|
61
|
-
return { ...tool, function: { ...fn, parameters } }
|
|
62
|
-
})
|
|
63
|
-
return changed ? { ...requestBody, tools } : requestBody
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// The model can be switched with /model without restarting the coding agent.
|
|
67
|
-
// Annotate each request with the ACTUAL model chosen by Qwen Config, rather
|
|
68
|
-
// than retaining a stale --append-system-prompt model from CLI startup.
|
|
69
|
-
export function attachRuntimeModelContext(payload) {
|
|
70
|
-
if (!payload || typeof payload !== 'object' || !Array.isArray(payload.messages)) return payload
|
|
71
|
-
const model = payload.model
|
|
72
|
-
if (typeof model !== 'string' || !/^[a-zA-Z0-9][a-zA-Z0-9._:/+-]{0,255}$/.test(model)) return payload
|
|
73
|
-
const index = payload.messages.findIndex(item => item?.role === 'system' && typeof item.content === 'string')
|
|
74
|
-
if (index === -1) return payload
|
|
75
|
-
const runtime = '\n\nRuntime metadata (not model instructions): active_model_id=' + model + '; client_app=BCCLI; transport=local-proxy.'
|
|
76
|
-
const messages = [...payload.messages]
|
|
77
|
-
messages[index] = { ...messages[index], content: messages[index].content + runtime }
|
|
78
|
-
return { ...payload, messages }
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Qwen 0.26.3 sends max_tokens=64000 for many model IDs. Several Gateway
|
|
82
|
-
// upstream routes reject that value (e.g. GLM Flash). Bound the requested
|
|
83
|
-
// output conservatively for coding-agent interoperability; never increase it.
|
|
84
|
-
// This does not change the context window or input-token budget.
|
|
85
|
-
const DEFAULT_OUTPUT_TOKEN_CEILING = 4096
|
|
86
|
-
export function boundChatOutputTokens(payload) {
|
|
87
|
-
if (!payload || typeof payload !== 'object') return payload
|
|
88
|
-
const legacy = payload.max_tokens
|
|
89
|
-
const modern = payload.max_completion_tokens
|
|
90
|
-
const valid = (value) => Number.isInteger(value) && value > 0
|
|
91
|
-
|
|
92
|
-
// Real Gateway's exact Kimi route rejects legacy max_tokens. Keep parity
|
|
93
|
-
// with PAYG validation for positive decimal numeric strings, without
|
|
94
|
-
// changing the output-ceiling policy for any other model.
|
|
95
|
-
if (payload.model === 'kimi-k2.7-code') {
|
|
96
|
-
const parsed = (value) => {
|
|
97
|
-
if (typeof value !== 'number' && !(typeof value === 'string' && /^[0-9]+$/.test(value))) return null
|
|
98
|
-
const n = Number(value)
|
|
99
|
-
return Number.isSafeInteger(n) && n > 0 ? n : null
|
|
100
|
-
}
|
|
101
|
-
const legacyValue = parsed(legacy)
|
|
102
|
-
const modernValue = parsed(modern)
|
|
103
|
-
if (legacyValue != null) {
|
|
104
|
-
const requested = modernValue != null ? Math.min(legacyValue, modernValue) : legacyValue
|
|
105
|
-
const { max_tokens: _ignored, ...rest } = payload
|
|
106
|
-
return { ...rest, max_completion_tokens: Math.min(requested, DEFAULT_OUTPUT_TOKEN_CEILING) }
|
|
107
|
-
}
|
|
108
|
-
if (modernValue != null && (typeof modern !== 'number' || modernValue > DEFAULT_OUTPUT_TOKEN_CEILING)) {
|
|
109
|
-
return { ...payload, max_completion_tokens: Math.min(modernValue, DEFAULT_OUTPUT_TOKEN_CEILING) }
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
if (valid(legacy) && legacy > DEFAULT_OUTPUT_TOKEN_CEILING) {
|
|
113
|
-
return { ...payload, max_tokens: DEFAULT_OUTPUT_TOKEN_CEILING }
|
|
114
|
-
}
|
|
115
|
-
if (valid(modern) && modern > DEFAULT_OUTPUT_TOKEN_CEILING) {
|
|
116
|
-
return { ...payload, max_completion_tokens: DEFAULT_OUTPUT_TOKEN_CEILING }
|
|
117
|
-
}
|
|
118
|
-
return payload
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function waitForDrainOrClose(res) {
|
|
122
|
-
if (res.destroyed) return Promise.resolve()
|
|
123
|
-
return new Promise((resolve) => {
|
|
124
|
-
const done = () => {
|
|
125
|
-
res.off('drain', done)
|
|
126
|
-
res.off('close', done)
|
|
127
|
-
resolve()
|
|
128
|
-
}
|
|
129
|
-
res.once('drain', done)
|
|
130
|
-
res.once('close', done)
|
|
131
|
-
})
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function matchesToken(auth, token) {
|
|
135
|
-
if (typeof auth !== 'string') return false
|
|
136
|
-
const actual = Buffer.from(auth)
|
|
137
|
-
const expected = Buffer.from('Bearer ' + token)
|
|
138
|
-
return actual.length === expected.length && timingSafeEqual(actual, expected)
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
export async function startGatewayProxy({ key, endpoint = GATEWAY, token = randomBytes(32).toString('hex'), fetchImpl = fetch } = {}) {
|
|
142
|
-
if (!ALLOWED_PROVIDER_ENDPOINTS.includes(endpoint)) throw Error('Unapproved provider endpoint; local proxy refused to start.')
|
|
143
|
-
if (typeof key !== 'string' || !key.trim()) throw Error('Gateway key is required for local proxy.')
|
|
144
|
-
if (typeof token !== 'string' || token.length < 6 || token === key) throw Error('Invalid or non-isolated proxy token.')
|
|
145
|
-
|
|
146
|
-
const server = createServer(async (req, res) => {
|
|
147
|
-
const fail = (status, body) => {
|
|
148
|
-
if (res.headersSent || res.destroyed) return
|
|
149
|
-
res.writeHead(status, { 'content-type': 'application/json', 'cache-control': 'no-store' })
|
|
150
|
-
res.end(JSON.stringify({ error: body }))
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
if (!matchesToken(req.headers.authorization, token)) {
|
|
154
|
-
fail(401, 'Local session token is required.')
|
|
155
|
-
return
|
|
156
|
-
}
|
|
157
|
-
if (!ALLOWED_PATHS.has(req.url ?? '')) {
|
|
158
|
-
fail(404, 'Unsupported local gateway route.')
|
|
159
|
-
return
|
|
160
|
-
}
|
|
161
|
-
const method = req.url === '/v1/models' ? 'GET' : 'POST'
|
|
162
|
-
if (req.method !== method) {
|
|
163
|
-
fail(405, 'Unsupported request method.')
|
|
164
|
-
return
|
|
165
|
-
}
|
|
166
|
-
if (req.headers['content-length'] && Number(req.headers['content-length']) > MAX_REQUEST_BYTES) {
|
|
167
|
-
fail(413, 'Request too large.')
|
|
168
|
-
return
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
const abort = new AbortController()
|
|
172
|
-
const onClose = () => { if (!res.writableEnded) abort.abort() }
|
|
173
|
-
res.once('close', onClose)
|
|
174
|
-
try {
|
|
175
|
-
let body
|
|
176
|
-
if (method === 'POST') {
|
|
177
|
-
const buffers = []
|
|
178
|
-
let bytes = 0
|
|
179
|
-
for await (const chunk of req) {
|
|
180
|
-
bytes += chunk.length
|
|
181
|
-
if (bytes > MAX_REQUEST_BYTES) {
|
|
182
|
-
fail(413, 'Request too large.')
|
|
183
|
-
return
|
|
184
|
-
}
|
|
185
|
-
buffers.push(chunk)
|
|
186
|
-
}
|
|
187
|
-
body = Buffer.concat(buffers)
|
|
188
|
-
if (req.url === '/v1/chat/completions') {
|
|
189
|
-
let parsed
|
|
190
|
-
try { parsed = JSON.parse(body.toString('utf8')) }
|
|
191
|
-
catch { fail(400, 'Invalid JSON request.'); return }
|
|
192
|
-
const normalized = boundChatOutputTokens(attachRuntimeModelContext(normalizeZeroArgFunctionTools(parsed)))
|
|
193
|
-
if (normalized !== parsed) body = Buffer.from(JSON.stringify(normalized))
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
const upstream = await fetchImpl(endpoint + req.url.slice(3), {
|
|
197
|
-
method,
|
|
198
|
-
headers: {
|
|
199
|
-
authorization: 'Bearer ' + key,
|
|
200
|
-
accept: 'application/json, text/event-stream',
|
|
201
|
-
...(method === 'POST' ? { 'content-type': 'application/json' } : {}),
|
|
202
|
-
},
|
|
203
|
-
body,
|
|
204
|
-
redirect: 'error',
|
|
205
|
-
signal: abort.signal,
|
|
206
|
-
})
|
|
207
|
-
res.writeHead(upstream.status, {
|
|
208
|
-
'content-type': upstream.headers.get('content-type') ?? 'application/json',
|
|
209
|
-
'cache-control': 'no-store',
|
|
210
|
-
})
|
|
211
|
-
if (upstream.body) {
|
|
212
|
-
for await (const chunk of upstream.body) {
|
|
213
|
-
if (res.destroyed) break
|
|
214
|
-
if (!res.write(chunk)) await waitForDrainOrClose(res)
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
res.end()
|
|
218
|
-
} catch {
|
|
219
|
-
if (res.headersSent) res.destroy()
|
|
220
|
-
else fail(502, 'Provider connection failed.')
|
|
221
|
-
} finally {
|
|
222
|
-
res.off('close', onClose)
|
|
223
|
-
}
|
|
224
|
-
})
|
|
225
|
-
|
|
226
|
-
await new Promise((resolve, reject) => {
|
|
227
|
-
const onError = (error) => { server.off('listening', onListening); reject(error) }
|
|
228
|
-
const onListening = () => { server.off('error', onError); resolve() }
|
|
229
|
-
server.once('error', onError)
|
|
230
|
-
server.once('listening', onListening)
|
|
231
|
-
server.listen(0, '127.0.0.1')
|
|
232
|
-
})
|
|
233
|
-
const port = server.address()?.port
|
|
234
|
-
return {
|
|
235
|
-
token,
|
|
236
|
-
baseUrl: 'http://127.0.0.1:' + port + '/v1',
|
|
237
|
-
async close() {
|
|
238
|
-
server.closeAllConnections()
|
|
239
|
-
await new Promise((resolve, reject) => server.close((error) => error ? reject(error) : resolve()))
|
|
240
|
-
},
|
|
241
|
-
}
|
|
242
|
-
}
|
package/startup-menu.mjs
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
// In-terminal BCCLI first-run / provider-switch menu.
|
|
2
|
-
// This runs in the parent process BEFORE the coding agent is launched; real
|
|
3
|
-
// API keys never enter Qwen's process or any coding/MCP subprocess.
|
|
4
|
-
import { createInterface } from 'node:readline/promises'
|
|
5
|
-
import { PROVIDERS, isConfigured, resolveProvider, selectedProvider, selectedModel, saveSelectedModel, saveProviderKey, useProvider } from './providers.mjs'
|
|
6
|
-
import { hiddenApiKey } from './provider-cli.mjs'
|
|
7
|
-
import { DEFAULT_MODEL, discoverModels, selectModel } from './core.mjs'
|
|
8
|
-
|
|
9
|
-
export const PROVIDER_SWITCH_EXIT = 51
|
|
10
|
-
|
|
11
|
-
// Returning users go straight to chat with the selected provider and model.
|
|
12
|
-
// Eligibility is still verified against the live provider's available models.
|
|
13
|
-
export async function autoSelectStartup({
|
|
14
|
-
providerId = selectedProvider(),
|
|
15
|
-
configured = isConfigured,
|
|
16
|
-
remembered = selectedModel,
|
|
17
|
-
resolve = resolveProvider,
|
|
18
|
-
list = discoverModels,
|
|
19
|
-
defaultModel = DEFAULT_MODEL,
|
|
20
|
-
env = process.env,
|
|
21
|
-
} = {}) {
|
|
22
|
-
if (!configured(providerId)) return undefined
|
|
23
|
-
const requested = providerId === 'botconnector'
|
|
24
|
-
? (env.BOTCONNECTOR_MODEL_ID || remembered(providerId) || defaultModel)
|
|
25
|
-
: remembered(providerId)
|
|
26
|
-
if (!requested) return undefined
|
|
27
|
-
const profile = resolve(providerId)
|
|
28
|
-
const models = await list({ key: profile.key, endpoint: profile.endpoint })
|
|
29
|
-
if (!models.some(item => item.id === requested)) return undefined
|
|
30
|
-
return { providerId, model: selectModel(models, requested), key: profile.key, endpoint: profile.endpoint }
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export async function terminalQuestion(label, { input = process.stdin, output = process.stdout } = {}) {
|
|
34
|
-
const rl = createInterface({ input, output, terminal: !!input.isTTY })
|
|
35
|
-
try { return (await rl.question(label)).trim() }
|
|
36
|
-
finally { rl.close() }
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function selectedName(answer, choices, defaultValue) {
|
|
40
|
-
if (!answer) return defaultValue
|
|
41
|
-
const clean = answer.replace(/!$/, '').trim().toLowerCase()
|
|
42
|
-
if (/^[0-9]+$/.test(clean)) return choices[Number(clean) - 1]
|
|
43
|
-
return choices.includes(clean) ? clean : undefined
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export async function startupMenu({
|
|
47
|
-
ask = terminalQuestion,
|
|
48
|
-
askSecret = hiddenApiKey,
|
|
49
|
-
out = value => console.log(value),
|
|
50
|
-
currentProvider = selectedProvider,
|
|
51
|
-
configured = isConfigured,
|
|
52
|
-
saveKey = saveProviderKey,
|
|
53
|
-
use = useProvider,
|
|
54
|
-
remembered = selectedModel,
|
|
55
|
-
saveModel = saveSelectedModel,
|
|
56
|
-
resolve = resolveProvider,
|
|
57
|
-
list = discoverModels,
|
|
58
|
-
defaultModel = DEFAULT_MODEL,
|
|
59
|
-
} = {}) {
|
|
60
|
-
const ids = Object.keys(PROVIDERS)
|
|
61
|
-
const active = currentProvider()
|
|
62
|
-
out('')
|
|
63
|
-
out('BCCLI | Provider dan model')
|
|
64
|
-
out('Pilih provider. Masukkan tanda ! setelah angka untuk mengganti API key yang sudah tersimpan.')
|
|
65
|
-
ids.forEach((id, idx) => {
|
|
66
|
-
out(' ' + (idx + 1) + '. ' + PROVIDERS[id].name +
|
|
67
|
-
(configured(id) ? ' [key tersedia]' : ' [perlu API key]') +
|
|
68
|
-
(id === active ? ' [aktif]' : ''))
|
|
69
|
-
})
|
|
70
|
-
let providerId
|
|
71
|
-
let replace
|
|
72
|
-
for (;;) {
|
|
73
|
-
const value = await ask('Provider (angka/nama; Enter = aktif; Ctrl+C untuk batal): ')
|
|
74
|
-
providerId = selectedName(value, ids, active)
|
|
75
|
-
if (!providerId) { out('Pilihan provider tidak dikenal.'); continue }
|
|
76
|
-
replace = value.endsWith('!')
|
|
77
|
-
break
|
|
78
|
-
}
|
|
79
|
-
let pendingKey
|
|
80
|
-
if (replace || !configured(providerId)) {
|
|
81
|
-
out('Masukkan API key ' + PROVIDERS[providerId].name + ' (input tersembunyi).')
|
|
82
|
-
pendingKey = await askSecret()
|
|
83
|
-
if (!pendingKey) throw Error('Tidak ada API key; provider tidak diubah.')
|
|
84
|
-
}
|
|
85
|
-
if (!pendingKey && !configured(providerId)) throw Error('Provider ' + providerId + ' belum memiliki API key. Tidak ada permintaan AI.')
|
|
86
|
-
// Validate before saving so a bad replacement never destroys a working key.
|
|
87
|
-
const profile = pendingKey
|
|
88
|
-
? { id: providerId, key: pendingKey, endpoint: PROVIDERS[providerId].endpoint }
|
|
89
|
-
: resolve(providerId)
|
|
90
|
-
const models = await list({ key: profile.key, endpoint: profile.endpoint })
|
|
91
|
-
if (!models.length) throw Error('Tidak ada model untuk provider ini.')
|
|
92
|
-
const all = [...models].sort((a, b) => a.id.localeCompare(b.id))
|
|
93
|
-
const previous = remembered(providerId)
|
|
94
|
-
const preferred = all.some(m => m.id === previous) ? previous :
|
|
95
|
-
providerId === 'botconnector' && all.some(m => m.id === defaultModel) ? defaultModel : undefined
|
|
96
|
-
let filtered = all
|
|
97
|
-
for (;;) {
|
|
98
|
-
out('')
|
|
99
|
-
out('Model ' + PROVIDERS[providerId].name + ' (' + filtered.length + ' dari ' + all.length + ' model):')
|
|
100
|
-
filtered.slice(0, 14).forEach((m, i) => out(' ' + (i + 1) + '. ' + m.id + (m.id === preferred ? ' [default]' : '')))
|
|
101
|
-
if (filtered.length > 14) out(' ... ketik ?kata untuk mencari model.')
|
|
102
|
-
const value = await ask('Model (nomor/ID/?cari' + (preferred ? '/Enter = ' + preferred : '') + '): ')
|
|
103
|
-
if (value.startsWith('?')) {
|
|
104
|
-
const term = value.slice(1).trim().toLowerCase()
|
|
105
|
-
filtered = term ? all.filter(m => m.id.toLowerCase().includes(term) || m.name?.toLowerCase().includes(term)) : all
|
|
106
|
-
if (!filtered.length) { out('Tidak ada model yang cocok.'); filtered = all }
|
|
107
|
-
continue
|
|
108
|
-
}
|
|
109
|
-
const candidate = !value ? preferred : /^[0-9]+$/.test(value) ? filtered[Number(value) - 1]?.id : value
|
|
110
|
-
if (!candidate || !all.some(m => m.id === candidate)) { out('Pilih nomor atau ID model yang tampil.'); continue }
|
|
111
|
-
selectModel(all, candidate)
|
|
112
|
-
// Save only after authenticated model discovery and selection.
|
|
113
|
-
if (pendingKey) {
|
|
114
|
-
saveKey(providerId, pendingKey)
|
|
115
|
-
out('API key tersimpan terlindungi. Tidak ada permintaan chat AI.')
|
|
116
|
-
}
|
|
117
|
-
use(providerId)
|
|
118
|
-
saveModel(providerId, candidate)
|
|
119
|
-
return { providerId, model: candidate, key: profile.key, endpoint: profile.endpoint }
|
|
120
|
-
}
|
|
121
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|