api-proxy-config.spec.ts 35 KB

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