browser-plugin.spec.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**
  2. * ui-model browser half on a real cordis Context with fake command/slots/
  3. * connection faces and real session scopes: the plugin mounts ModelService
  4. * as `models`, the /model contribution and the conversation.input.model
  5. * seat both register, and BOTH entries resolve the SAME per-session
  6. * directory through the service — a selection submitted through the seat's
  7. * inject face is the current the popup's next options pass marks active
  8. * (and the reverse), the one-shared-state contract of the dual entry.
  9. * Scope disposal drops the directory (HMR safety).
  10. */
  11. import { Context } from 'cordis'
  12. import { describe, expect, it } from 'vitest'
  13. import { createScope } from '@deepseek-ai/dsh-client-runtime/client'
  14. import type { SessionId } from '@deepseek-ai/dsh-client-runtime/client'
  15. import { LocaleService } from '@deepseek-ai/dsh-client-locale/client'
  16. import type { ModelTarget } from '@deepseek-ai/dsh-client-connection/client'
  17. import type { CommandContribution, SelectOption } from '@deepseek-ai/dsh-client-ui-command/client'
  18. import type { ModelSelectInjected } from '../src/client/slots.ts'
  19. import { apply, inject } from '../src/client/index.ts'
  20. const sid = (k: string): SessionId => k as SessionId
  21. const GROUPS = [{
  22. id: 'deepseek-official',
  23. name: 'DeepSeek',
  24. models: [
  25. {
  26. id: 'deepseek-v4-flash',
  27. name: 'DeepSeek-V4-Flash',
  28. reasoning: {
  29. efforts: [
  30. { id: 'off', name: 'Off' },
  31. { id: 'high', name: 'High' },
  32. { id: 'max', name: 'Max' },
  33. ],
  34. defaultEffort: 'high',
  35. },
  36. },
  37. {
  38. id: 'deepseek-v4-pro',
  39. name: 'DeepSeek-V4-Pro',
  40. reasoning: {
  41. efforts: [
  42. { id: 'off', name: 'Off' },
  43. { id: 'high', name: 'High' },
  44. { id: 'max', name: 'Max' },
  45. ],
  46. defaultEffort: 'high',
  47. },
  48. },
  49. ],
  50. }]
  51. /** Boot the plugin over fake faces + a stateful fake host (current moves on selectModel). */
  52. async function bench() {
  53. const ctx = new Context()
  54. let current: ModelTarget = { provider: 'deepseek-official', model: 'deepseek-v4-flash' }
  55. const calls = { models: 0, select: 0 }
  56. ctx.provide('connection', { api: { sessions: {
  57. models: () => {
  58. calls.models += 1
  59. return Promise.resolve({ result: { ok: true as const, value: { current, groups: GROUPS, failures: [] } } })
  60. },
  61. selectModel: (payload: { provider: string; model: string; reasoningEffort?: string }) => {
  62. calls.select += 1
  63. current = {
  64. provider: payload.provider,
  65. model: payload.model,
  66. ...payload.reasoningEffort === undefined
  67. ? {}
  68. : { reasoningEffort: payload.reasoningEffort },
  69. }
  70. return Promise.resolve({ result: { ok: true as const, value: { selected: current } } })
  71. },
  72. } } })
  73. let contribution: CommandContribution | undefined
  74. ctx.provide('command', {
  75. register(c: CommandContribution) {
  76. contribution = c
  77. return () => { contribution = undefined }
  78. },
  79. })
  80. const seats = new Map<string, {
  81. inject: ((sessionId: SessionId) => ModelSelectInjected) | undefined
  82. locale: string | undefined
  83. }>()
  84. ctx.provide('slots', {
  85. register(options: { name: string; locale?: string; inject?: (sessionId: SessionId) => ModelSelectInjected }) {
  86. seats.set(options.name, { inject: options.inject, locale: options.locale })
  87. return () => { seats.delete(options.name) }
  88. },
  89. })
  90. ctx.provide('conversation', {})
  91. ctx.provide('locale', new LocaleService(ctx))
  92. const scopes = new Map<SessionId, Context>()
  93. const addressed = new Set<SessionId>()
  94. ctx.provide('sessions', {
  95. scope: (id: SessionId) => scopes.get(id),
  96. subagentAddress: (id: SessionId) => addressed.has(id)
  97. ? { parentSessionId: sid('parent'), childSessionId: id, mode: 'continuable' as const }
  98. : undefined,
  99. })
  100. const fiber = ctx.plugin({ inject: [...inject], apply })
  101. await fiber.await()
  102. await ctx.plugin(function probe() {}).await()
  103. const mint = (key: string) => {
  104. const handle = createScope(ctx, sid(key))
  105. scopes.set(sid(key), handle.ctx)
  106. return handle
  107. }
  108. return {
  109. ctx, fiber, mint, calls,
  110. contribution: () => contribution!,
  111. seat: () => seats.get('conversation.input.model')!,
  112. hostCurrent: () => current,
  113. setHostCurrent: (target: ModelTarget) => { current = target },
  114. address: (id: SessionId) => { addressed.add(id) },
  115. }
  116. }
  117. const projection = (id: string) => ({ sessionId: sid(id) })
  118. describe('ui-model dual entry', () => {
  119. it('registers the /model contribution and the composer model seat', async () => {
  120. const b = await bench()
  121. expect(b.contribution().name).toBe('model')
  122. expect(b.contribution().ui.kind).toBe('popupSelect')
  123. expect(b.seat().inject).toBeTypeOf('function')
  124. // Copy rides the standard locale seat.
  125. expect(b.seat().locale).toBe('model')
  126. })
  127. it('popup options mark the host current active with the provider group in the detail', async () => {
  128. const b = await bench()
  129. b.mint('s1')
  130. const options = await b.contribution().ui.options(projection('s1'), new AbortController().signal)
  131. expect(options.map((o: SelectOption) => o.label)).toEqual(['DeepSeek-V4-Flash', 'DeepSeek-V4-Pro'])
  132. expect(options[0]).toMatchObject({ active: true, detail: 'DeepSeek' })
  133. expect(options[1]?.active).toBeUndefined()
  134. })
  135. it('a seat selection is the current the popup marks active next — one shared state', async () => {
  136. const b = await bench()
  137. b.mint('s1')
  138. const seatFace = b.seat().inject!(sid('s1'))
  139. // Switch through the SEAT entry.
  140. expect(await seatFace.select({
  141. provider: 'deepseek-official',
  142. model: 'deepseek-v4-pro',
  143. reasoningEffort: 'max',
  144. })).toBe(true)
  145. expect(b.hostCurrent()).toEqual({
  146. provider: 'deepseek-official',
  147. model: 'deepseek-v4-pro',
  148. reasoningEffort: 'max',
  149. })
  150. expect(seatFace.directory.getSnapshot().current).toEqual({
  151. provider: 'deepseek-official',
  152. model: 'deepseek-v4-pro',
  153. reasoningEffort: 'max',
  154. })
  155. // The POPUP's next options pass reflects it without a seat-side reload.
  156. const options = await b.contribution().ui.options(projection('s1'), new AbortController().signal)
  157. expect(options.find((o: SelectOption) => o.label === 'DeepSeek-V4-Pro')).toMatchObject({ active: true })
  158. })
  159. it('a popup selection lands on the seat store — the reverse direction of the same state', async () => {
  160. const b = await bench()
  161. b.mint('s1')
  162. const seatFace = b.seat().inject!(sid('s1'))
  163. const options = await b.contribution().ui.options(projection('s1'), new AbortController().signal)
  164. const pro = options.find((o: SelectOption) => o.label === 'DeepSeek-V4-Pro')!
  165. await b.contribution().ui.onSelect(pro, projection('s1'))
  166. expect(seatFace.directory.getSnapshot().current).toEqual({
  167. provider: 'deepseek-official',
  168. model: 'deepseek-v4-pro',
  169. reasoningEffort: 'high',
  170. })
  171. })
  172. it('both entries share one directory instance per session, isolated across sessions', async () => {
  173. const b = await bench()
  174. b.mint('a')
  175. b.mint('b')
  176. const faceA = b.seat().inject!(sid('a'))
  177. const faceA2 = b.seat().inject!(sid('a'))
  178. const faceB = b.seat().inject!(sid('b'))
  179. expect(faceA.directory).toBe(faceA2.directory)
  180. expect(faceA.directory).not.toBe(faceB.directory)
  181. // The service face resolves the same instance the seat inject handed out.
  182. expect(b.ctx.models.directoryFor(sid('a')).store).toBe(faceA.directory)
  183. })
  184. it('drops an unconsumed local selection and restores the Host target after reconnect', async () => {
  185. const b = await bench()
  186. b.mint('s1')
  187. const face = b.seat().inject!(sid('s1'))
  188. await face.select({ provider: 'deepseek-official', model: 'deepseek-v4-pro' })
  189. b.setHostCurrent({ provider: 'deepseek-official', model: 'deepseek-v4-flash' })
  190. b.ctx.emit('connection/reset')
  191. expect(face.directory.getSnapshot()).toMatchObject({ current: null, status: 'loading' })
  192. await Promise.resolve()
  193. expect(face.directory.getSnapshot()).toMatchObject({
  194. current: { provider: 'deepseek-official', model: 'deepseek-v4-flash' },
  195. status: 'ready',
  196. })
  197. })
  198. it('scope disposal drops the directory; a reborn scope gets a fresh one', async () => {
  199. const b = await bench()
  200. const first = b.mint('s1')
  201. const face1 = b.seat().inject!(sid('s1'))
  202. await first.fiber.dispose()
  203. b.mint('s1')
  204. const face2 = b.seat().inject!(sid('s1'))
  205. expect(face2.directory).not.toBe(face1.directory)
  206. })
  207. it('an unknown session fails loud at the seat inject', async () => {
  208. const b = await bench()
  209. expect(() => b.seat().inject!(sid('ghost'))).toThrow(/resolved no scope/)
  210. })
  211. it('withholds both model entries from addressed subagent sessions without Agent-bound RPCs', async () => {
  212. const b = await bench()
  213. b.mint('child')
  214. b.address(sid('child'))
  215. expect(b.contribution().available(projection('child'))).toBe(false)
  216. await expect(b.contribution().ui.options(
  217. projection('child'),
  218. new AbortController().signal,
  219. )).rejects.toThrow(/unavailable for addressed subagent/)
  220. const face = b.seat().inject!(sid('child'))
  221. expect(face.available).toBe(false)
  222. face.load()
  223. await expect(face.select({ provider: 'deepseek', model: 'deepseek-v4-pro' })).resolves.toBe(false)
  224. await expect(b.ctx.models.directoryFor(sid('child')).load())
  225. .rejects.toThrow(/unavailable for addressed subagent/)
  226. await expect(b.ctx.models.directoryFor(sid('child')).select({
  227. provider: 'deepseek',
  228. model: 'deepseek-v4-pro',
  229. })).rejects.toThrow(/unavailable for addressed subagent/)
  230. b.ctx.emit('connection/reset')
  231. await Promise.resolve()
  232. expect(b.calls).toEqual({ models: 0, select: 0 })
  233. })
  234. })