api-proxy-rename.spec.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * sessions.rename delegation through the composed SessionTitleService. The
  3. * agent factory is a structural stub whose createAgent forwards seed/meta into
  4. * the real SessionStore, and whose resume never runs (every source here is
  5. * already attached). Cold-session resolution is the shared `agentFor` path —
  6. * api-proxy-cold.spec.ts owns the resume evidence for every unary that rides
  7. * it, rename included.
  8. */
  9. import { describe, expect, it } from 'vitest'
  10. import { Context } from 'cordis'
  11. import SessionStore from '@deepseek-ai/dsh-session'
  12. import AgentRegistry from '@deepseek-ai/dsh-agent'
  13. import type { Agent, AgentHandle, CreateAgentOptions } from '@deepseek-ai/dsh-agent'
  14. import { createUserMessage } from '@deepseek-ai/dsh-llm'
  15. import SessionTitleService from '@deepseek-ai/dsh-session-title'
  16. import UserInteractionService from '@deepseek-ai/dsh-user-interaction'
  17. import type { Session, SessionId } from '@deepseek-ai/dsh-session'
  18. import type { RpcRequest } from '@deepseek-ai/dsh-host-apiproxy/api/rpc'
  19. import { RpcId } from '@deepseek-ai/dsh-host-apiproxy/api/rpc'
  20. import { createApiProxy } from '@deepseek-ai/dsh-host-apiproxy'
  21. const sid = (id: string): SessionId => id as SessionId
  22. let nextRpc = 1
  23. function request<P>(payload: P): RpcRequest<P> {
  24. return { rpcId: RpcId(`fr-${String(nextRpc++)}`), payload }
  25. }
  26. async function composed(withTitles = true): Promise<Context> {
  27. const ctx = new Context()
  28. await ctx.plugin(SessionStore)
  29. await ctx.plugin(AgentRegistry)
  30. await ctx.plugin(UserInteractionService)
  31. if (withTitles) {
  32. await ctx.plugin(SessionTitleService, { fallbackMaxWords: 5, fallbackMaxBytes: 40, maxTitleBytes: 40 })
  33. }
  34. // Store-backed structural factory: create builds the session with the
  35. // forwarded seed/meta (the store validates the balanced prefix) and
  36. // registers an idle agent stub over it.
  37. ctx.agents.setFactory({
  38. createAgent: (ownerCtx: Context, options: CreateAgentOptions): Promise<AgentHandle> => {
  39. const session = ctx.sessions.create(options.sessionId, {
  40. ...options.seed === undefined ? {} : { seed: [...options.seed] },
  41. ...options.meta === undefined ? {} : { meta: options.meta },
  42. })
  43. const agent = { id: session.id, session, status: 'idle', ctx: ownerCtx } as Agent
  44. ctx.agents.register(agent)
  45. return Promise.resolve({ agent, dispose: () => Promise.resolve() })
  46. },
  47. resume: () => Promise.reject(new Error('resume must not run: every source is attached')),
  48. })
  49. return ctx
  50. }
  51. /** Register one live agent whose log holds `turns` completed turns. */
  52. function liveAgent(ctx: Context, id: string, turns: number): Session {
  53. const session = ctx.sessions.create(sid(id), { meta: { cwd: '/proj' } })
  54. for (let turn = 1; turn <= turns; turn++) {
  55. session.append('turn/start', { turn })
  56. session.append('user/message', createUserMessage({
  57. content: [{ type: 'text', text: `prompt ${String(turn)}` }],
  58. source: { kind: 'user' },
  59. }), { surfaceOp: 'append' })
  60. session.append('turn/end', { turn, reason: { kind: 'completed' } })
  61. }
  62. ctx.agents.register({ id: session.id, session, status: 'idle', ctx } as Agent)
  63. return session
  64. }
  65. const api = (ctx: Context) => createApiProxy(ctx, { provider: 'p', model: 'm', cwd: '/tmp', workspaceRoot: '/tmp' })
  66. describe('sessions.rename', () => {
  67. it('accepts through the composed title service: normalized user-source event, echoed seq', async () => {
  68. const ctx = await composed()
  69. const source = liveAgent(ctx, 'session-rename', 1)
  70. const renamed = await api(ctx).sessions.rename(request({ sessionId: source.id, title: ' new name ' }))
  71. expect(renamed.result.ok).toBe(true)
  72. if (!renamed.result.ok) return
  73. expect(renamed.result.value.title).toBe('new name')
  74. const event = source.events.findLast(item => item.type === 'session/title')
  75. expect(event?.seq).toBe(renamed.result.value.seq)
  76. expect(event?.data).toMatchObject({ title: 'new name', source: { kind: 'user' } })
  77. })
  78. it('maps only an empty-normalizing title to title-invalid, with a presentable message', async () => {
  79. const ctx = await composed()
  80. const source = liveAgent(ctx, 'session-rename-bad', 1)
  81. // U+200B passes a client-side trim gate but normalizes to empty host-side.
  82. const response = await api(ctx).sessions.rename(request({ sessionId: source.id, title: ' ​ ' }))
  83. expect(response.result.ok).toBe(false)
  84. if (!response.result.ok) {
  85. expect(response.result.error).toMatchObject({
  86. code: 'title-invalid',
  87. details: { sessionId: source.id },
  88. })
  89. // The message renders verbatim in the rename dialog's alert.
  90. expect(response.result.error.message).toBe('session title must contain visible characters')
  91. }
  92. })
  93. it('maps a non-validation rename failure (stale session object) to internal, not title-invalid', async () => {
  94. const ctx = await composed()
  95. // The registered agent holds a session object from another store: the
  96. // title service's liveness check throws a plain Error, which must not
  97. // read as the user's fault.
  98. const foreign = await composed(false)
  99. const stale = liveAgent(foreign, 'session-rename-stale', 1)
  100. ctx.agents.register({ id: stale.id, session: stale, status: 'idle', ctx } as Agent)
  101. const response = await api(ctx).sessions.rename(request({ sessionId: stale.id, title: 'name' }))
  102. expect(response.result.ok).toBe(false)
  103. if (!response.result.ok) expect(response.result.error.code).toBe('internal')
  104. })
  105. it('answers internal when the composition mounts no session-title service', async () => {
  106. const ctx = await composed(false)
  107. const source = liveAgent(ctx, 'session-no-titles', 1)
  108. const response = await api(ctx).sessions.rename(request({ sessionId: source.id, title: 'name' }))
  109. expect(response.result.ok).toBe(false)
  110. if (!response.result.ok) {
  111. expect(response.result.error.code).toBe('internal')
  112. expect(response.result.error.message).toMatch(/mounts no session-title service/)
  113. }
  114. })
  115. })