sqlite-integration.spec.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { afterEach, describe, expect, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import { mkdtemp, rm } from 'node:fs/promises'
  4. import { tmpdir } from 'node:os'
  5. import { join } from 'node:path'
  6. import type { Agent } from '@deepseek-ai/dsh-agent'
  7. import { CallId } from '@deepseek-ai/dsh-llm'
  8. import SessionStore, {
  9. SESSION_FORMAT_VERSION,
  10. SessionId,
  11. type Session,
  12. } from '@deepseek-ai/dsh-session'
  13. import SessionPersistenceJsonl from '@deepseek-ai/dsh-session-persistence-jsonl'
  14. import SessionQuerySqlite from '@deepseek-ai/dsh-session-query-sqlite'
  15. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  16. import ToolRegistry from '@deepseek-ai/dsh-tools'
  17. import * as ToolSessionQuery from '@deepseek-ai/dsh-tool-session-query'
  18. const temporaryDirectories: string[] = []
  19. const contexts: Context[] = []
  20. afterEach(async () => {
  21. for (const ctx of contexts.splice(0)) await ctx.fiber.dispose()
  22. for (const directory of temporaryDirectories.splice(0)) {
  23. await rm(directory, { recursive: true, force: true })
  24. }
  25. })
  26. function fakeAgent(session: Session): Agent {
  27. return { id: session.id, session } as unknown as Agent
  28. }
  29. describe('tool-session-query with the real SQLite provider', () => {
  30. it('searches live prior-step history and a persisted same-workspace log', async () => {
  31. const root = await mkdtemp(join(tmpdir(), 'dsh-tool-session-query-'))
  32. temporaryDirectories.push(root)
  33. const ctx = new Context()
  34. contexts.push(ctx)
  35. await ctx.plugin(SessionStore)
  36. await ctx.plugin(SystemPrompt)
  37. await ctx.plugin(ToolRegistry)
  38. await ctx.plugin(SessionPersistenceJsonl, { root, compression: 'none' })
  39. await ctx.plugin(SessionQuerySqlite, { path: join(root, 'session-query.db') })
  40. await ctx.plugin(ToolSessionQuery)
  41. const persisted = SessionId('persisted')
  42. await ctx.sessionPersistence.create({
  43. version: SESSION_FORMAT_VERSION,
  44. id: persisted,
  45. createdAt: 1,
  46. cwd: '/work',
  47. })
  48. await ctx.sessionPersistence.append(persisted, [{
  49. type: 'user/message',
  50. seq: 0,
  51. time: 2,
  52. data: {
  53. content: [{ type: 'text', text: 'persisted integration needle' }],
  54. source: { kind: 'user' },
  55. },
  56. surfaceOp: 'append',
  57. }])
  58. const caller = ctx.sessions.create(SessionId('caller'), {
  59. meta: { createdAt: 10, cwd: '/work' },
  60. })
  61. caller.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  62. caller.append(
  63. 'user/message',
  64. { content: [{ type: 'text', text: 'live integration needle' }], source: { kind: 'user' } },
  65. { surfaceOp: 'append' },
  66. )
  67. caller.append('step/start', { turn: 1, step: 1 })
  68. let call = 0
  69. const execute = (name: string, args: unknown) => ctx.tools.execute({
  70. name,
  71. arguments: args,
  72. callId: CallId(`integration-${++call}`),
  73. signal: new AbortController().signal,
  74. agent: fakeAgent(caller),
  75. })
  76. const sessions = await execute('session_search', { query: 'persisted integration needle' })
  77. expect(sessions.isError).toBe(false)
  78. expect(sessions.content.map(block => block.type === 'text' ? block.text : '').join('\n'))
  79. .toContain('Session persisted')
  80. const persistedEvents = await execute('session_event_search', {
  81. session_id: persisted,
  82. query: 'persisted integration needle',
  83. })
  84. expect(persistedEvents.isError).toBe(false)
  85. expect(persistedEvents.content.map(block => block.type === 'text' ? block.text : '').join('\n'))
  86. .toContain('seq 0')
  87. const liveEvents = await execute('session_event_search', { query: 'live integration needle' })
  88. expect(liveEvents.isError).toBe(false)
  89. expect(liveEvents.content.map(block => block.type === 'text' ? block.text : '').join('\n'))
  90. .toContain('seq 1')
  91. })
  92. })