api-proxy-config.spec.ts 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /**
  2. * Settings/credentials/llm RPC domains and their host-stream frames over
  3. * createApiProxy: layered redacted describe, write-path rejection mapping,
  4. * value-free credential views, the directory/live-route merge, and the three
  5. * invalidation frames (settings/credentials/models changed).
  6. */
  7. import { describe, expect, it, vi } from 'vitest'
  8. import { Context } from '@deepseek-ai/cordis'
  9. import z from '@deepseek-ai/schemastery'
  10. import AgentRegistry from '@deepseek-ai/dsh-agent'
  11. import SessionStore from '@deepseek-ai/dsh-session'
  12. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  13. import ToolRegistry from '@deepseek-ai/dsh-tools'
  14. import UserInteractionService from '@deepseek-ai/dsh-user-interaction'
  15. import LlmService, { LlmAdapter } from '@deepseek-ai/dsh-llm'
  16. import type { GenerateOptions, LlmModelInfo, LlmProviderInfo, StreamChunk } from '@deepseek-ai/dsh-llm'
  17. import { Settings, settingsNamespace } from '@deepseek-ai/dsh-settings'
  18. import type { SettingsNamespace } from '@deepseek-ai/dsh-settings'
  19. import { Credentials } from '@deepseek-ai/dsh-credentials'
  20. import type { CredentialInfo, CredentialRef, ResolvedCredential } from '@deepseek-ai/dsh-credentials'
  21. import type { HostFrame } from '../src/api/index.ts'
  22. import type { RpcRequest, RpcResponse } from '../src/api/rpc.ts'
  23. import { RpcId } from '../src/api/rpc.ts'
  24. import { AGENT_DEFAULT_MODEL_SETTINGS_NAMESPACE } from '@deepseek-ai/dsh-agent-default-model'
  25. import { createApiProxy } from '../src/api-proxy.ts'
  26. const DEFAULTS = { defaultModelSelection: () => ({ provider: 'p', model: 'm' }), cwd: '/tmp' }
  27. let nextRpc = 1
  28. function request<P>(payload: P): RpcRequest<P> {
  29. return { rpcId: RpcId(`req-${String(nextRpc++)}`), payload }
  30. }
  31. function expectOk<T>(response: RpcResponse<T>): T {
  32. expect(response.result.ok).toBe(true)
  33. if (!response.result.ok) throw new Error('unreachable')
  34. return response.result.value
  35. }
  36. function expectErr<T>(response: RpcResponse<T>): { code: string; message: string; details: unknown } {
  37. expect(response.result.ok).toBe(false)
  38. if (response.result.ok) throw new Error('unreachable')
  39. return response.result.error
  40. }
  41. /** In-memory settings provider: the Service Definition base class owns all tested behavior. */
  42. class MemorySettings extends Settings {
  43. doc: Record<string, unknown>
  44. constructor(ctx: ConstructorParameters<typeof Settings>[0], options?: {
  45. doc?: Record<string, unknown>
  46. readOnly?: boolean
  47. documentPath?: string
  48. preparedPath?: string
  49. }) {
  50. super(ctx)
  51. this.doc = structuredClone(options?.doc ?? {})
  52. this.readOnly = options?.readOnly ?? false
  53. this.path = options?.documentPath
  54. this.preparedPath = options?.preparedPath
  55. }
  56. private readonly readOnly: boolean
  57. private readonly path: string | undefined
  58. private readonly preparedPath: string | undefined
  59. get writable(): boolean {
  60. return !this.readOnly
  61. }
  62. override get documentPath(): string | undefined {
  63. return this.path
  64. }
  65. override prepareDocument(): Promise<string | undefined> {
  66. return Promise.resolve(this.preparedPath ?? this.documentPath)
  67. }
  68. protected load(): Promise<Record<string, unknown>> {
  69. return Promise.resolve(structuredClone(this.doc))
  70. }
  71. protected persist(ns: SettingsNamespace, section: Record<string, unknown>): Promise<void> {
  72. this.doc[ns] = structuredClone(section)
  73. return Promise.resolve()
  74. }
  75. }
  76. /** In-memory credential provider with an env-shadow double for the rejection path. */
  77. class MemoryCredentials extends Credentials {
  78. private readonly values = new Map<string, string>()
  79. constructor(ctx: ConstructorParameters<typeof Credentials>[0], options?: { shadowed?: string[] }) {
  80. super(ctx)
  81. this.shadowed = new Set(options?.shadowed ?? [])
  82. }
  83. private readonly shadowed: Set<string>
  84. resolve(ref: CredentialRef): Promise<ResolvedCredential | undefined> {
  85. if (this.shadowed.has(ref)) return Promise.resolve({ value: 'from-env', source: 'env' })
  86. const value = this.values.get(ref)
  87. return Promise.resolve(value === undefined ? undefined : { value, source: 'file' })
  88. }
  89. describe(ref: CredentialRef): Promise<CredentialInfo> {
  90. if (this.shadowed.has(ref)) return Promise.resolve({ configured: true, source: 'env', writable: false })
  91. const configured = this.values.has(ref)
  92. return Promise.resolve({ configured, ...configured ? { source: 'file' } : {}, writable: true })
  93. }
  94. set(ref: CredentialRef, value: string): Promise<void> {
  95. if (this.shadowed.has(ref)) {
  96. return Promise.reject(new Error(`credentials: ${ref} is shadowed by the read-only environment`))
  97. }
  98. this.values.set(ref, value)
  99. this.ctx.emit('credentials/updated', ref)
  100. return Promise.resolve()
  101. }
  102. unset(ref: CredentialRef): Promise<void> {
  103. if (this.shadowed.has(ref)) {
  104. return Promise.reject(new Error(`credentials: ${ref} is shadowed by the read-only environment`))
  105. }
  106. this.values.delete(ref)
  107. this.ctx.emit('credentials/updated', ref)
  108. return Promise.resolve()
  109. }
  110. }
  111. /** Catalog-serving adapter stub for the llm.models path. */
  112. class CatalogAdapter extends LlmAdapter {
  113. constructor(private readonly name: string, private readonly models: readonly string[]) {
  114. super()
  115. }
  116. override providerInfo(provider: string): LlmProviderInfo {
  117. return { id: provider, name: this.name }
  118. }
  119. override listModels(provider: string): Promise<readonly LlmModelInfo[]> {
  120. return Promise.resolve(this.models.map(id => ({ provider, id, name: id })))
  121. }
  122. async * stream(_options: GenerateOptions): AsyncIterable<StreamChunk> {
  123. throw new Error('not exercised')
  124. }
  125. }
  126. class BrokenCatalogAdapter extends CatalogAdapter {
  127. override listModels(): Promise<readonly LlmModelInfo[]> {
  128. return Promise.reject(new Error('catalog backend down'))
  129. }
  130. }
  131. const NS = settingsNamespace('llm-deepseek')
  132. const AdapterConfig = z.object({
  133. apiKey: z.string().role('secret'),
  134. apiKeyEnv: z.string().default('DEEPSEEK_API_KEY'),
  135. baseURL: z.string(),
  136. })
  137. async function harness(options?: {
  138. settings?: false | {
  139. doc?: Record<string, unknown>
  140. readOnly?: boolean
  141. documentPath?: string
  142. preparedPath?: string
  143. }
  144. credentials?: false | { shadowed?: string[] }
  145. /** Skip the directory registration to exercise a namespace the proxy does not expose. */
  146. configurableProviders?: false
  147. }): Promise<Context> {
  148. const ctx = new Context()
  149. await ctx.plugin(SessionStore)
  150. await ctx.plugin(SystemPrompt, { persona: '' })
  151. await ctx.plugin(ToolRegistry)
  152. await ctx.plugin(UserInteractionService)
  153. await ctx.plugin(AgentRegistry)
  154. await ctx.plugin(LlmService)
  155. if (options?.settings !== false) await ctx.plugin(MemorySettings, options?.settings)
  156. if (options?.credentials !== false) await ctx.plugin(MemoryCredentials, options?.credentials)
  157. // Model-provider namespaces plus the explicit Web preference and product
  158. // onboarding allowlists are the proxy's complete settings surface.
  159. if (options?.configurableProviders !== false) {
  160. ctx.llm.registerConfigurableProviders([
  161. { provider: 'deepseek-official', displayName: 'DeepSeek', settingsNs: 'llm-deepseek', settingsPath: [] },
  162. ])
  163. }
  164. // Host-stream opener reads the committed-workspace baseline; the stub
  165. // suffices — the real workspace composition is api-proxy-workspace.spec's.
  166. ctx.provide('workspace', { list: () => [] } as never)
  167. return ctx
  168. }
  169. /** Drain `count` host frames matching `types`, then abort the stream. */
  170. async function collectHost(
  171. api: ReturnType<typeof createApiProxy>,
  172. types: string[],
  173. count: number,
  174. run: () => Promise<void>,
  175. ): Promise<HostFrame[]> {
  176. const abort = new AbortController()
  177. const frames: HostFrame[] = []
  178. const stream = api.events.host(request({}), abort.signal)
  179. const consume = (async () => {
  180. for await (const frame of stream) {
  181. if (!types.includes(frame.payload.type)) continue
  182. frames.push(frame.payload)
  183. if (frames.length >= count) abort.abort()
  184. }
  185. })()
  186. await run()
  187. await consume
  188. return frames
  189. }
  190. describe('settings domain', () => {
  191. it('reports an actionable error when no settings provider is mounted', async () => {
  192. const ctx = await harness({ settings: false })
  193. const api = createApiProxy(ctx, DEFAULTS)
  194. const error = expectErr(await api.settings.describe(request({})))
  195. expect(error.code).toBe('internal')
  196. expect(error.message).toContain('dsh-settings-local')
  197. })
  198. it('describes layered redacted namespaces with their secret slots', async () => {
  199. const ctx = await harness({ settings: {
  200. doc: { 'llm-deepseek': { apiKey: 'user-secret', baseURL: 'https://user' } },
  201. documentPath: '/tmp/custom-settings.yaml',
  202. } })
  203. ctx.settings.register(NS, AdapterConfig, { base: { baseURL: 'https://base' } })
  204. const api = createApiProxy(ctx, DEFAULTS)
  205. const value = expectOk(await api.settings.describe(request({})))
  206. expect(value.writable).toBe(true)
  207. expect(value.hasDocument).toBe(true)
  208. expect(value.namespaces).toHaveLength(1)
  209. const view = value.namespaces[0]!
  210. expect(view.ns).toBe('llm-deepseek')
  211. expect(view.applies).toBe('live')
  212. expect((view.schema as { refs?: unknown }).refs).toBeDefined()
  213. expect(view.value).toEqual({ apiKeyEnv: 'DEEPSEEK_API_KEY', baseURL: 'https://user' })
  214. expect(view.base).toEqual({ baseURL: 'https://base' })
  215. expect(view.user).toEqual({ baseURL: 'https://user' })
  216. expect(view.secrets).toEqual([{ path: ['apiKey'], set: true }])
  217. expect(JSON.stringify(value)).not.toContain('user-secret')
  218. })
  219. it('opens the provider-resolved document without accepting a browser path', async () => {
  220. const ctx = await harness({ settings: {
  221. documentPath: '/tmp/described-settings.yaml',
  222. preparedPath: '/tmp/custom-settings.yaml',
  223. } })
  224. const opened: string[] = []
  225. const api = createApiProxy(ctx, {
  226. ...DEFAULTS,
  227. openTextFile: (path) => {
  228. opened.push(path)
  229. return Promise.resolve()
  230. },
  231. })
  232. expect(expectOk(await api.settings.openDocument(request({}), new AbortController().signal)))
  233. .toEqual({ opened: true })
  234. expect(opened).toEqual(['/tmp/custom-settings.yaml'])
  235. })
  236. it('refuses to open settings when the provider has no local document', async () => {
  237. const ctx = await harness()
  238. const api = createApiProxy(ctx, DEFAULTS)
  239. expect(expectOk(await api.settings.describe(request({}))).hasDocument).toBe(false)
  240. const error = expectErr(await api.settings.openDocument(request({}), new AbortController().signal))
  241. expect(error.code).toBe('internal')
  242. expect(error.message).toContain('no local document')
  243. })
  244. it('does not prepare or open a settings document after cancellation', async () => {
  245. const ctx = await harness({ settings: { documentPath: '/tmp/settings.yaml' } })
  246. const opened: string[] = []
  247. const api = createApiProxy(ctx, {
  248. ...DEFAULTS,
  249. openTextFile: (path) => {
  250. opened.push(path)
  251. return Promise.resolve()
  252. },
  253. })
  254. const prepare = vi.spyOn(ctx.settings, 'prepareDocument')
  255. const cancelled = new AbortController()
  256. cancelled.abort()
  257. expect(expectErr(await api.settings.openDocument(request({}), cancelled.signal)).code)
  258. .toBe('cancelled')
  259. expect(prepare).not.toHaveBeenCalled()
  260. const pending = Promise.withResolvers<string | undefined>()
  261. prepare.mockReturnValueOnce(pending.promise)
  262. const duringPrepare = new AbortController()
  263. const opening = api.settings.openDocument(request({}), duringPrepare.signal)
  264. await vi.waitFor(() => { expect(prepare).toHaveBeenCalledOnce() })
  265. duringPrepare.abort()
  266. pending.resolve('/tmp/settings.yaml')
  267. expect(expectErr(await opening).code).toBe('cancelled')
  268. expect(opened).toEqual([])
  269. })
  270. it('serves model-provider and explicitly allowlisted Web namespaces only', async () => {
  271. // The settings seam is general: any plugin may register a namespace for
  272. // its own configuration. The Web configuration plane remains opt-in, so a
  273. // future internal plugin cannot become remotely configurable just by
  274. // registering; locale, permission, conversation, theme, and the product
  275. // onboarding namespace are intentionally admitted by this surface.
  276. const ctx = await harness()
  277. ctx.settings.register(NS, AdapterConfig)
  278. ctx.settings.register(settingsNamespace('some-other-plugin'), z.object({ secretPath: z.string() }))
  279. ctx.settings.register(settingsNamespace('permission'), z.object({
  280. defaultPreset: z.union(['read-only', 'workspace-write']).required(),
  281. }), {
  282. base: { defaultPreset: 'read-only' },
  283. })
  284. ctx.settings.register(settingsNamespace('ui-theme'), z.object({
  285. preference: z.union(['light', 'dark', 'system']).default('system'),
  286. }))
  287. ctx.settings.register(settingsNamespace('locale'), z.object({
  288. preference: z.union(['zh', 'en']).required(false),
  289. }))
  290. ctx.settings.register(settingsNamespace('ui-conversation'), z.object({
  291. busyEnter: z.union(['queue', 'steer']).default('queue'),
  292. }))
  293. const api = createApiProxy(ctx, DEFAULTS)
  294. const value = expectOk(await api.settings.describe(request({})))
  295. expect(value.namespaces.map(view => view.ns)).toEqual([
  296. 'llm-deepseek', 'permission', 'ui-theme', 'locale', 'ui-conversation',
  297. ])
  298. const permission = expectOk(await api.settings.mutate(request({
  299. ns: 'permission',
  300. ops: [{ op: 'set', path: ['defaultPreset'], value: 'workspace-write' }],
  301. })))
  302. expect(permission.value).toEqual({ defaultPreset: 'workspace-write' })
  303. const theme = expectOk(await api.settings.mutate(request({
  304. ns: 'ui-theme',
  305. ops: [{ op: 'set', path: ['preference'], value: 'dark' }],
  306. })))
  307. expect(theme.value).toEqual({ preference: 'dark' })
  308. const locale = expectOk(await api.settings.mutate(request({
  309. ns: 'locale',
  310. ops: [{ op: 'set', path: ['preference'], value: 'en' }],
  311. })))
  312. expect(locale.value).toEqual({ preference: 'en' })
  313. const conversation = expectOk(await api.settings.mutate(request({
  314. ns: 'ui-conversation',
  315. ops: [{ op: 'set', path: ['busyEnter'], value: 'steer' }],
  316. })))
  317. expect(conversation.value).toEqual({ busyEnter: 'steer' })
  318. for (const response of [
  319. await api.settings.update(request({ ns: 'some-other-plugin', patch: { secretPath: '/etc/shadow' } })),
  320. await api.settings.replace(request({ ns: 'some-other-plugin', section: {} })),
  321. ]) {
  322. const error = expectErr(response)
  323. expect(error.code).toBe('settings-not-exposed')
  324. expect(error.details).toEqual({ ns: 'some-other-plugin' })
  325. }
  326. // The write never reached the seam.
  327. expect(ctx.settings.describe().find(d => String(d.ns) === 'some-other-plugin')?.value).toEqual({})
  328. })
  329. it('serves product preference namespaces without invalidating the model catalog', async () => {
  330. const ctx = await harness()
  331. ctx.settings.register(settingsNamespace('ui-onboarding'), z.object({ welcomeNoticeVersion: z.string() }))
  332. ctx.settings.register(settingsNamespace('ui-theme'), z.object({
  333. preference: z.union(['light', 'dark', 'system']).default('system'),
  334. }))
  335. const api = createApiProxy(ctx, DEFAULTS)
  336. expect(expectOk(await api.settings.describe(request({}))).namespaces.map(view => view.ns))
  337. .toEqual(['ui-onboarding', 'ui-theme'])
  338. const frames = await collectHost(api, ['host/settings-changed'], 2, async () => {
  339. expectOk(await api.settings.mutate(request({
  340. ns: 'ui-onboarding',
  341. ops: [{ op: 'set', path: ['welcomeNoticeVersion'], value: 'v1' }],
  342. })))
  343. expectOk(await api.settings.mutate(request({
  344. ns: 'ui-theme',
  345. ops: [{ op: 'set', path: ['preference'], value: 'dark' }],
  346. })))
  347. })
  348. expect(frames).toEqual([
  349. { type: 'host/settings-changed', ns: 'ui-onboarding' },
  350. { type: 'host/settings-changed', ns: 'ui-theme' },
  351. ])
  352. })
  353. it('serves the agent-preset namespace, so a browser preset picker can persist its choice', async () => {
  354. const ctx = await harness()
  355. ctx.settings.register(settingsNamespace('agent-presets'), z.object({ default: z.string() }))
  356. const api = createApiProxy(ctx, DEFAULTS)
  357. expectOk(await api.settings.update(request({ ns: 'agent-presets', patch: { default: 'minimal' } })))
  358. // Both browser surfaces that offer the choice — the General row and the
  359. // management section — write the default through `settings.update`, so a
  360. // namespace outside this boundary makes the picker move and then silently
  361. // forget, which is worse than refusing the control.
  362. expect(ctx.settings.describe().find(view => String(view.ns) === 'agent-presets')?.value)
  363. .toEqual({ default: 'minimal' })
  364. })
  365. it('refuses even a model-provider namespace once its directory entry is gone', async () => {
  366. const ctx = await harness({ configurableProviders: false })
  367. ctx.settings.register(NS, AdapterConfig)
  368. const api = createApiProxy(ctx, DEFAULTS)
  369. expect(expectOk(await api.settings.describe(request({}))).namespaces).toEqual([])
  370. expect(expectErr(await api.settings.update(request({ ns: 'llm-deepseek', patch: { baseURL: 'https://x' } }))).code)
  371. .toBe('settings-not-exposed')
  372. })
  373. it('invalidates the model catalog when a provider namespace changes, and broadcasts a raw-only change', async () => {
  374. // Editing `models` changes no route, so llm/adapters-updated never fires
  375. // and an open model picker would keep serving the stale catalog. Storing
  376. // an override equal to the resolved value emits nothing on
  377. // settings/updated, so another tab would never learn the field became
  378. // overridden.
  379. const ctx = await harness()
  380. ctx.settings.register(NS, AdapterConfig, { base: { baseURL: 'https://base' } })
  381. const api = createApiProxy(ctx, DEFAULTS)
  382. const frames = await collectHost(api, ['host/settings-changed', 'host/models-changed'], 2, async () => {
  383. await api.settings.update(request({ ns: 'llm-deepseek', patch: { baseURL: 'https://base' } }))
  384. })
  385. expect(frames).toEqual([
  386. { type: 'host/settings-changed', ns: 'llm-deepseek' },
  387. { type: 'host/models-changed' },
  388. ])
  389. // The resolved value never moved: base already said https://base.
  390. expect(expectOk(await api.settings.describe(request({}))).namespaces[0]!.value)
  391. .toEqual({ apiKeyEnv: 'DEEPSEEK_API_KEY', baseURL: 'https://base' })
  392. })
  393. it('broadcasts a permission change without invalidating the model catalog', async () => {
  394. const ctx = await harness()
  395. const permission = ctx.settings.register(settingsNamespace('permission'), z.object({
  396. defaultPreset: z.union(['read-only', 'workspace-write']).required(),
  397. }), {
  398. base: { defaultPreset: 'read-only' },
  399. })
  400. const api = createApiProxy(ctx, DEFAULTS)
  401. const frames = await collectHost(api, ['host/settings-changed', 'host/models-changed'], 1, async () => {
  402. await permission.update({ defaultPreset: 'workspace-write' })
  403. })
  404. expect(frames).toEqual([{ type: 'host/settings-changed', ns: 'permission' }])
  405. })
  406. it('invalidates the model catalog when the Agent default selection changes', async () => {
  407. const ctx = await harness()
  408. const defaultModel = ctx.settings.register(AGENT_DEFAULT_MODEL_SETTINGS_NAMESPACE, z.object({
  409. provider: z.string().required(),
  410. model: z.string().required(),
  411. }), { base: { provider: 'deepseek-official', model: 'deepseek-v4-flash' } })
  412. const api = createApiProxy(ctx, DEFAULTS)
  413. // The shared section names the selection every blank session resolves to,
  414. // so an externally edited default — another tab, a
  415. // hand-edited settings.yaml — has to reach an open selector as well.
  416. const frames = await collectHost(api, ['host/settings-changed', 'host/models-changed'], 2, async () => {
  417. await defaultModel.replace({ provider: 'deepseek-official', model: 'deepseek-reasoner' })
  418. })
  419. expect(frames).toEqual([
  420. { type: 'host/settings-changed', ns: 'agent-default-model' },
  421. { type: 'host/models-changed' },
  422. ])
  423. })
  424. it('maps a stale expectedRevision to settings-conflict carrying both revisions', async () => {
  425. const ctx = await harness()
  426. ctx.settings.register(NS, AdapterConfig)
  427. const api = createApiProxy(ctx, DEFAULTS)
  428. const opened = expectOk(await api.settings.describe(request({}))).namespaces[0]!.revision
  429. expect(expectOk(await api.settings.update(request({ ns: 'llm-deepseek', patch: { baseURL: 'https://first' }, expectedRevision: opened })))
  430. .revision).toBe(opened + 1)
  431. const error = expectErr(await api.settings.update(request({ ns: 'llm-deepseek', patch: { baseURL: 'https://second' }, expectedRevision: opened })))
  432. expect(error.code).toBe('settings-conflict')
  433. expect(error.details).toEqual({ ns: 'llm-deepseek', expected: opened, actual: opened + 1 })
  434. // The refused write changed nothing.
  435. expect(expectOk(await api.settings.describe(request({}))).namespaces[0]!.user).toEqual({ baseURL: 'https://first' })
  436. })
  437. it('updates the user layer, answers with the new redacted view, and broadcasts the frame', async () => {
  438. const ctx = await harness()
  439. ctx.settings.register(NS, AdapterConfig, { base: { baseURL: 'https://base' } })
  440. const api = createApiProxy(ctx, DEFAULTS)
  441. const frames = await collectHost(api, ['host/settings-changed'], 1, async () => {
  442. const view = expectOk(await api.settings.update(request({ ns: 'llm-deepseek', patch: { apiKey: 'sk-new', baseURL: 'https://next' } })))
  443. expect(view.value).toEqual({ apiKeyEnv: 'DEEPSEEK_API_KEY', baseURL: 'https://next' })
  444. expect(view.user).toEqual({ baseURL: 'https://next' })
  445. expect(view.secrets).toEqual([{ path: ['apiKey'], set: true }])
  446. expect(JSON.stringify(view)).not.toContain('sk-new')
  447. })
  448. expect(frames).toEqual([{ type: 'host/settings-changed', ns: 'llm-deepseek' }])
  449. })
  450. it('replace resets the user layer wholesale', async () => {
  451. const ctx = await harness({ settings: { doc: { 'llm-deepseek': { baseURL: 'https://user' } } } })
  452. ctx.settings.register(NS, AdapterConfig)
  453. const api = createApiProxy(ctx, DEFAULTS)
  454. const view = expectOk(await api.settings.replace(request({ ns: 'llm-deepseek', section: {} })))
  455. expect(view.value).toEqual({ apiKeyEnv: 'DEEPSEEK_API_KEY' })
  456. expect(view.user).toEqual({})
  457. })
  458. it.each([
  459. ['an invalid namespace name', 'Not A Namespace', {}],
  460. ['a schema-invalid patch', 'llm-deepseek', { baseURL: 42 }],
  461. ])('rejects %s as settings-rejected', async (_case, ns, patch) => {
  462. const ctx = await harness()
  463. ctx.settings.register(NS, AdapterConfig)
  464. const api = createApiProxy(ctx, DEFAULTS)
  465. const error = expectErr(await api.settings.update(request({ ns, patch })))
  466. expect(error.code).toBe('settings-rejected')
  467. expect(error.details).toEqual({ ns })
  468. })
  469. it('answers an unregistered namespace exactly like an unexposed one', async () => {
  470. // Deliberately indistinguishable: separating "does not exist" from
  471. // "exists but is not yours to configure" would let a caller enumerate the
  472. // registered namespaces one probe at a time.
  473. const ctx = await harness()
  474. ctx.settings.register(NS, AdapterConfig)
  475. ctx.settings.register(settingsNamespace('some-other-plugin'), z.object({ secretPath: z.string() }))
  476. const api = createApiProxy(ctx, DEFAULTS)
  477. const unknown = expectErr(await api.settings.update(request({ ns: 'unknown-ns', patch: {} })))
  478. const unexposed = expectErr(await api.settings.update(request({ ns: 'some-other-plugin', patch: {} })))
  479. expect(unknown.code).toBe('settings-not-exposed')
  480. expect(unexposed.code).toBe(unknown.code)
  481. expect(unexposed.message.replace('some-other-plugin', 'unknown-ns')).toBe(unknown.message)
  482. })
  483. it('maps a read-only provider refusal onto the same rejection', async () => {
  484. const ctx = await harness({ settings: { readOnly: true } })
  485. ctx.settings.register(NS, AdapterConfig)
  486. const api = createApiProxy(ctx, DEFAULTS)
  487. const value = expectOk(await api.settings.describe(request({})))
  488. expect(value.writable).toBe(false)
  489. const error = expectErr(await api.settings.update(request({ ns: 'llm-deepseek', patch: {} })))
  490. expect(error.code).toBe('settings-rejected')
  491. expect(error.message).toContain('read-only')
  492. })
  493. })
  494. describe('credentials domain', () => {
  495. it('reports an actionable error when no credential provider is mounted', async () => {
  496. const ctx = await harness({ credentials: false })
  497. const api = createApiProxy(ctx, DEFAULTS)
  498. const error = expectErr(await api.credentials.describe(request({ refs: ['A'] })))
  499. expect(error.code).toBe('internal')
  500. expect(error.message).toContain('dsh-credentials-local')
  501. })
  502. it('describes value-free views and flips state through set/unset with frames', async () => {
  503. const ctx = await harness()
  504. const api = createApiProxy(ctx, DEFAULTS)
  505. const before = expectOk(await api.credentials.describe(request({ refs: ['OPENAI_API_KEY'] })))
  506. expect(before.credentials).toEqual({ OPENAI_API_KEY: { configured: false, writable: true } })
  507. const frames = await collectHost(api, ['host/credentials-changed'], 2, async () => {
  508. expectOk(await api.credentials.set(request({ ref: 'OPENAI_API_KEY', value: 'sk-secret' })))
  509. const after = expectOk(await api.credentials.describe(request({ refs: ['OPENAI_API_KEY'] })))
  510. expect(after.credentials).toEqual({ OPENAI_API_KEY: { configured: true, source: 'file', writable: true } })
  511. expect(JSON.stringify(after)).not.toContain('sk-secret')
  512. expectOk(await api.credentials.unset(request({ ref: 'OPENAI_API_KEY' })))
  513. })
  514. expect(frames).toEqual([
  515. { type: 'host/credentials-changed', ref: 'OPENAI_API_KEY' },
  516. { type: 'host/credentials-changed', ref: 'OPENAI_API_KEY' },
  517. ])
  518. })
  519. it('maps a shadowed write onto credential-rejected for set and unset alike', async () => {
  520. const ctx = await harness({ credentials: { shadowed: ['DEEPSEEK_API_KEY'] } })
  521. const api = createApiProxy(ctx, DEFAULTS)
  522. const described = expectOk(await api.credentials.describe(request({ refs: ['DEEPSEEK_API_KEY'] })))
  523. expect(described.credentials['DEEPSEEK_API_KEY']).toEqual({ configured: true, source: 'env', writable: false })
  524. const setError = expectErr(await api.credentials.set(request({ ref: 'DEEPSEEK_API_KEY', value: 'x' })))
  525. expect(setError.code).toBe('credential-rejected')
  526. expect(setError.details).toEqual({ ref: 'DEEPSEEK_API_KEY' })
  527. const unsetError = expectErr(await api.credentials.unset(request({ ref: 'DEEPSEEK_API_KEY' })))
  528. expect(unsetError.code).toBe('credential-rejected')
  529. })
  530. })
  531. describe('llm domain', () => {
  532. it('merges the configurable directory with live routes and appends undeclared ones', async () => {
  533. const ctx = await harness({ configurableProviders: false })
  534. ctx.llm.registerConfigurableProviders([
  535. { provider: 'deepseek-official', displayName: 'DeepSeek', settingsNs: 'llm-deepseek', settingsPath: [] },
  536. { provider: 'openai', displayName: 'openai', settingsNs: 'llm-pi-ai', settingsPath: ['providers', 'openai'] },
  537. ])
  538. ctx.llm.registerAdapter(['deepseek-official'], new CatalogAdapter('DeepSeek', ['deepseek-v4-flash']))
  539. ctx.llm.registerAdapter(['undeclared'], new CatalogAdapter('Undeclared', ['u-1']))
  540. // Only one namespace can answer an interrogation, so the flag follows the
  541. // entry's namespace rather than being assumed for every row.
  542. ctx.llm.registerModelDiscovery('llm-pi-ai', () => Promise.resolve([]))
  543. const api = createApiProxy(ctx, DEFAULTS)
  544. const value = expectOk(await api.llm.providers(request({})))
  545. expect(value.providers).toEqual([
  546. { provider: 'deepseek-official', displayName: 'DeepSeek', settingsNs: 'llm-deepseek', settingsPath: [], active: true },
  547. { provider: 'openai', displayName: 'openai', settingsNs: 'llm-pi-ai', settingsPath: ['providers', 'openai'], active: false },
  548. // An undeclared live route has no settings address, so nothing can be
  549. // interrogated on its behalf either.
  550. { provider: 'undeclared', displayName: 'Undeclared', settingsNs: '', settingsPath: [], active: true },
  551. ])
  552. })
  553. it('serves the host-scoped catalog with per-provider failures contained', async () => {
  554. const ctx = await harness()
  555. ctx.llm.registerAdapter(['deepseek-official'], new CatalogAdapter('DeepSeek', ['deepseek-v4-flash', 'deepseek-v4-pro']))
  556. ctx.llm.registerAdapter(['broken'], new BrokenCatalogAdapter('Broken', []))
  557. const api = createApiProxy(ctx, DEFAULTS)
  558. const value = expectOk(await api.llm.models(request({})))
  559. expect(value.groups).toEqual([{
  560. id: 'deepseek-official',
  561. name: 'DeepSeek',
  562. models: [
  563. { id: 'deepseek-v4-flash', name: 'deepseek-v4-flash' },
  564. { id: 'deepseek-v4-pro', name: 'deepseek-v4-pro' },
  565. ],
  566. }])
  567. expect(value.failures).toEqual([{ id: 'broken', name: 'Broken', message: 'catalog backend down' }])
  568. })
  569. it('broadcasts host/models-changed at every topology commit point', async () => {
  570. const ctx = await harness()
  571. const api = createApiProxy(ctx, DEFAULTS)
  572. const frames = await collectHost(api, ['host/models-changed'], 2, async () => {
  573. const dispose = ctx.llm.registerAdapter(['deepseek-official'], new CatalogAdapter('DeepSeek', []))
  574. dispose()
  575. return Promise.resolve()
  576. })
  577. expect(frames).toEqual([{ type: 'host/models-changed' }, { type: 'host/models-changed' }])
  578. })
  579. })
  580. describe('llm.discoverModels', () => {
  581. it('carries a draft to its namespace and returns candidates without storing anything', async () => {
  582. const ctx = await harness()
  583. const seen: unknown[] = []
  584. ctx.llm.registerModelDiscovery('llm-pi-ai', (probe) => {
  585. seen.push({ baseURL: probe.baseURL, api: probe.api, apiKey: probe.apiKey })
  586. return Promise.resolve([
  587. { id: 'acme-large', name: 'Acme Large', contextWindow: 65_536, maxTokens: 4096 },
  588. { id: 'acme-small' },
  589. ])
  590. })
  591. const api = createApiProxy(ctx, DEFAULTS)
  592. const value = expectOk(await api.llm.discoverModels(request({
  593. settingsNs: 'llm-pi-ai',
  594. baseURL: 'https://gateway.acme.example/v1',
  595. api: 'openai-completions',
  596. apiKey: 'probe-key',
  597. })))
  598. expect(value.models).toEqual([
  599. { id: 'acme-large', name: 'Acme Large', contextWindow: 65_536, maxTokens: 4096 },
  600. { id: 'acme-small' },
  601. ])
  602. expect(seen).toEqual([{
  603. baseURL: 'https://gateway.acme.example/v1',
  604. api: 'openai-completions',
  605. apiKey: 'probe-key',
  606. }])
  607. // Interrogating a draft is a read: no namespace gained a section, and no
  608. // credential reference was written.
  609. expect(expectOk(await api.settings.describe(request({}))).namespaces.map(view => view.ns))
  610. .not.toContain('llm-pi-ai')
  611. })
  612. it('carries the route being edited so an adapter can answer from its own registry', async () => {
  613. const ctx = await harness()
  614. let probe: unknown
  615. ctx.llm.registerModelDiscovery('llm-pi-ai', (request_) => {
  616. probe = request_
  617. return Promise.resolve([{ id: 'from-registry', contextWindow: 65_536, maxTokens: 4096 }])
  618. })
  619. const api = createApiProxy(ctx, DEFAULTS)
  620. const value = expectOk(await api.llm.discoverModels(request({
  621. settingsNs: 'llm-pi-ai',
  622. provider: 'deepseek',
  623. })))
  624. // No endpoint at all: a route the adapter already describes needs none.
  625. expect(probe).toEqual({ provider: 'deepseek' })
  626. expect(value.models).toEqual([{ id: 'from-registry', contextWindow: 65_536, maxTokens: 4096 }])
  627. })
  628. it('omits a credential and protocol the draft does not name', async () => {
  629. const ctx = await harness()
  630. let probe: unknown
  631. ctx.llm.registerModelDiscovery('llm-pi-ai', (request_) => {
  632. probe = request_
  633. return Promise.resolve([])
  634. })
  635. const api = createApiProxy(ctx, DEFAULTS)
  636. expectOk(await api.llm.discoverModels(request({
  637. settingsNs: 'llm-pi-ai',
  638. baseURL: 'https://gateway.acme.example/v1',
  639. })))
  640. // Absent fields stay absent rather than crossing as explicit undefined:
  641. // the adapter distinguishes "no protocol named" from "protocol undefined".
  642. expect(probe).toEqual({ baseURL: 'https://gateway.acme.example/v1' })
  643. })
  644. it('reports a failed interrogation as the form\'s next move, naming no credential', async () => {
  645. const ctx = await harness()
  646. ctx.llm.registerModelDiscovery('llm-pi-ai', () =>
  647. Promise.reject(new Error('https://gateway.acme.example/v1/models answered 401; check the API key')))
  648. const api = createApiProxy(ctx, DEFAULTS)
  649. const error = expectErr(await api.llm.discoverModels(request({
  650. settingsNs: 'llm-pi-ai',
  651. baseURL: 'https://gateway.acme.example/v1',
  652. apiKey: 'wrong',
  653. })))
  654. expect(error.code).toBe('model-discovery-failed')
  655. expect(error.message).toContain('answered 401; check the API key')
  656. expect(error.details).toEqual({ settingsNs: 'llm-pi-ai', baseURL: 'https://gateway.acme.example/v1' })
  657. expect(JSON.stringify(error)).not.toContain('wrong')
  658. })
  659. it('reports a namespace no adapter family serves', async () => {
  660. const ctx = await harness()
  661. const api = createApiProxy(ctx, DEFAULTS)
  662. const error = expectErr(await api.llm.discoverModels(request({
  663. settingsNs: 'llm-deepseek',
  664. baseURL: 'https://api.deepseek.com',
  665. })))
  666. expect(error.code).toBe('model-discovery-failed')
  667. expect(error.message).toContain('no model discovery is registered')
  668. })
  669. })