api-proxy-blank.spec.ts 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * The summary blank bit means "conversation not started" (no turn has run),
  3. * not "log empty": standalone plugin events — command lifecycle records,
  4. * plan/mode, permission knob events, session titles — never flip it, so running /plan or /goal on a
  5. * fresh session keeps it list-hidden and reusable, while the first accepted
  6. * prompt's turn/start clears it. The host/session-added frame shares the
  7. * same predicate function (covered by the workspace spec's frame assertion).
  8. */
  9. import { describe, expect, it } from 'vitest'
  10. import { Context } from '@deepseek-ai/cordis'
  11. import AgentRegistry from '@deepseek-ai/dsh-agent'
  12. import type { Agent } from '@deepseek-ai/dsh-agent'
  13. import SessionStore from '@deepseek-ai/dsh-session'
  14. import type { Session } from '@deepseek-ai/dsh-session'
  15. import UserInteractionService from '@deepseek-ai/dsh-user-interaction'
  16. import { CommandId } from '@deepseek-ai/dsh-commands/brand'
  17. // Side-effect type imports: the knob-event SessionEventMap merges.
  18. import type {} from '@deepseek-ai/dsh-permission'
  19. import type {} from '@deepseek-ai/dsh-sandbox-policy'
  20. import type {} from '@deepseek-ai/dsh-user-approval'
  21. import type { ApiProxy, RpcRequest } from '@deepseek-ai/dsh-host-apiproxy/api'
  22. import { RpcId } from '@deepseek-ai/dsh-host-apiproxy/api/rpc'
  23. import { createApiProxy } from '@deepseek-ai/dsh-host-apiproxy'
  24. let nextRpc = 1
  25. function request<P>(payload: P): RpcRequest<P> {
  26. return { rpcId: RpcId(`blank-${String(nextRpc++)}`), payload }
  27. }
  28. async function harness(): Promise<{ ctx: Context; api: ApiProxy; attach: (session: Session) => void }> {
  29. const ctx = new Context()
  30. await ctx.plugin(SessionStore)
  31. await ctx.plugin(UserInteractionService)
  32. await ctx.plugin(AgentRegistry)
  33. return {
  34. ctx,
  35. api: createApiProxy(ctx, { defaultModelSelection: () => ({ provider: 'p', model: 'm' }), cwd: '/tmp' }),
  36. attach: (session) => {
  37. ctx.agents.register({ id: session.id, session, status: 'idle', ctx } as Agent)
  38. },
  39. }
  40. }
  41. /** Append the standalone (non-conversation) event family a fresh session can accumulate. */
  42. function appendStandalone(session: Session): void {
  43. session.append('command/run', {
  44. commandId: CommandId('blank-cmd-1'), name: 'plan', args: '', source: { kind: 'user' },
  45. })
  46. session.append('plan/mode', { active: true })
  47. session.append('command/done', { commandId: CommandId('blank-cmd-1'), kind: 'success', text: 'Plan mode on.' })
  48. session.append('session/title', {
  49. title: 'standalone title', messageSeqs: [], source: { kind: 'fallback' },
  50. })
  51. // The three permission knob events (a /permission switch on a fresh session).
  52. session.append('permission/preset', { preset: 'danger-full-access' })
  53. session.append('sandbox/mode', { mode: 'danger-full-access' })
  54. session.append('approval/policy', { policy: 'never' })
  55. }
  56. async function listBlank(api: ApiProxy, id: string): Promise<boolean | undefined> {
  57. const response = await api.sessions.list(request({}))
  58. if (!response.result.ok) throw new Error('list failed')
  59. return response.result.value.items.find(item => item.sessionId === id)?.blank
  60. }
  61. describe('summary blank = conversation not started', () => {
  62. it('standalone events (command lifecycle, plan/mode, title) keep the session blank', async () => {
  63. const { ctx, api, attach } = await harness()
  64. const session = ctx.sessions.create()
  65. attach(session)
  66. expect(await listBlank(api, session.id)).toBe(true)
  67. appendStandalone(session)
  68. expect(await listBlank(api, session.id)).toBe(true)
  69. })
  70. it('the first turn clears blank', async () => {
  71. const { ctx, api, attach } = await harness()
  72. const session = ctx.sessions.create()
  73. attach(session)
  74. appendStandalone(session)
  75. session.append('turn/start', { turn: 0 })
  76. expect(await listBlank(api, session.id)).toBe(false)
  77. })
  78. })