agent.spec.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { describe, expect, it, vi } from 'vitest'
  2. import { Context } from 'cordis'
  3. import AgentRegistry, { type Agent } from '@deepseek-ai/dsh-agent'
  4. import AgentLoop from '@deepseek-ai/dsh-agent-loop'
  5. import LlmService from '@deepseek-ai/dsh-llm'
  6. import SessionStore, { SessionId } from '@deepseek-ai/dsh-session'
  7. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  8. import ToolRegistry from '@deepseek-ai/dsh-tools'
  9. import { MockAdapter, textResponse } from './mock-adapter.ts'
  10. async function harness(adapter: MockAdapter): Promise<Context> {
  11. const ctx = new Context()
  12. await ctx.plugin(LlmService)
  13. await ctx.plugin(SessionStore)
  14. await ctx.plugin(SystemPrompt)
  15. await ctx.plugin(ToolRegistry)
  16. await ctx.plugin(AgentRegistry)
  17. await ctx.plugin(AgentLoop, { agents: [] })
  18. ctx.llm.registerAdapter(['mock'], adapter)
  19. return ctx
  20. }
  21. function send(agent: Agent, text: string): void {
  22. agent.followup({ content: [{ type: 'text', text }], source: { kind: 'user' } })
  23. }
  24. describe('Agent', () => {
  25. it('idle inject() appends context without opening a turn or requesting a flush', async () => {
  26. const adapter = new MockAdapter([textResponse('ok')])
  27. const ctx = await harness(adapter)
  28. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  29. let flushes = 0
  30. ctx.on('session/flush', () => { flushes += 1 })
  31. agent.inject({ content: [{ type: 'text', text: 'context' }], source: { kind: 'plugin', plugin: 'p' } })
  32. expect(agent.session.events.map(event => event.type)).toEqual(['user/message'])
  33. expect(agent.status).toBe('idle')
  34. expect(adapter.requests).toHaveLength(0)
  35. await agent.whenIdle()
  36. expect(flushes).toBe(0)
  37. })
  38. it('inject() preserves an explicitly empty plugin source', async () => {
  39. const ctx = await harness(new MockAdapter([textResponse('ok')]))
  40. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  41. agent.inject({ content: [{ type: 'text', text: 'empty plugin source' }], source: { kind: 'plugin', plugin: '' } })
  42. const injected = agent.session.events.at(-1)
  43. expect(injected?.type === 'user/message' && injected.data.source)
  44. .toEqual({ kind: 'plugin', plugin: '' })
  45. })
  46. it('idle inject() rejects invalid input before append', async () => {
  47. const ctx = await harness(new MockAdapter([textResponse('ok')]))
  48. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  49. expect(() => {
  50. agent.inject({ content: [{ type: 'text', text: 'x', bad: 1n } as never], source: { kind: 'plugin', plugin: 'p' } })
  51. }).toThrow(/non-JSON-serializable/)
  52. expect(agent.session.events).toHaveLength(0)
  53. })
  54. it('steer() while idle becomes a woken prompt turn', async () => {
  55. const adapter = new MockAdapter([textResponse('ok')])
  56. const ctx = await harness(adapter)
  57. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  58. agent.steer({ content: [{ type: 'text', text: 'steer idle' }], source: { kind: 'plugin', plugin: 'test' } })
  59. await agent.whenIdle()
  60. expect(agent.session.events.some(event => event.type === 'user/message')).toBe(true)
  61. expect(adapter.requests).toHaveLength(1)
  62. })
  63. it('emits one running and idle transition for one completed turn', async () => {
  64. const ctx = await harness(new MockAdapter([textResponse('ok')]))
  65. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  66. const statuses: string[] = []
  67. ctx.on('agent/status', (subject, status) => {
  68. if (subject === agent) statuses.push(status)
  69. })
  70. send(agent, 'hi')
  71. await agent.whenIdle()
  72. expect(statuses).toEqual(['running', 'idle'])
  73. })
  74. it('whenIdle() resolves immediately without active work', async () => {
  75. const ctx = await harness(new MockAdapter([textResponse('ok')]))
  76. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  77. await agent.whenIdle()
  78. expect(agent.status).toBe('idle')
  79. })
  80. it('whenIdle() waits for active work until explicit cancellation', async () => {
  81. const ctx = await harness(new MockAdapter(['hang']))
  82. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  83. send(agent, 'queued')
  84. let settled = false
  85. const idle = agent.whenIdle().then(() => { settled = true })
  86. await Promise.resolve()
  87. expect(settled).toBe(false)
  88. agent.cancel({ kind: 'user' })
  89. await idle
  90. expect(agent.status).toBe('idle')
  91. })
  92. it('contains a throwing status listener on both transitions', async () => {
  93. const ctx = await harness(new MockAdapter([textResponse('ok')]))
  94. const warn = vi.spyOn(ctx.logger, 'warn').mockImplementation(() => undefined)
  95. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  96. ctx.on('agent/status', (_subject, status) => {
  97. throw new Error(`bad ${status} listener`)
  98. })
  99. send(agent, 'go')
  100. await agent.whenIdle()
  101. expect(agent.status).toBe('idle')
  102. expect(warn).toHaveBeenCalledWith(
  103. expect.stringContaining('agent event "agent/status" listener threw'),
  104. )
  105. })
  106. })