fs-tools.e2e.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { createUserMessage } from '@deepseek-ai/dsh-llm'
  2. import { mkdtemp, readFile, rm } from 'node:fs/promises'
  3. import { tmpdir } from 'node:os'
  4. import { join } from 'node:path'
  5. import { afterEach, describe, expect, it } from 'vitest'
  6. import type { Context } from 'cordis'
  7. import { SessionId } from '@deepseek-ai/dsh-session'
  8. import { fsHarness, waitForIdle } from './harness.ts'
  9. /** Key-gated smoke for a real model driving the local read/write/edit tools. */
  10. let ctx: Context | undefined
  11. let workdir: string | undefined
  12. afterEach(async () => {
  13. await ctx?.fiber.dispose()
  14. ctx = undefined
  15. if (workdir !== undefined) await rm(workdir, { recursive: true, force: true })
  16. workdir = undefined
  17. })
  18. const SYSTEM = 'You are a coding assistant. Use the write tool to create files, the read tool to inspect '
  19. + 'them, and the edit tool for literal replacements. Read a file before editing it. Keep replies terse.'
  20. describe.skipIf(!process.env.DEEPSEEK_API_KEY)('fs tools with-key smoke', () => {
  21. it('creates, reads, then edits a file — verified on disk', async () => {
  22. workdir = await mkdtemp(join(tmpdir(), 'dsh-fs-e2e-'))
  23. ctx = await fsHarness(workdir, SYSTEM)
  24. // agentLoop.create prepares a session with no cwd, so the provider default
  25. // (config.cwd = workdir) is the workspace.
  26. const agent = ctx.agentLoop.create(SessionId('fs-e2e'), { provider: 'deepseek-official', model: 'deepseek-v4-flash' })
  27. agent.followup(createUserMessage({
  28. content: [{ type: 'text', text:
  29. 'Create a file named note.txt containing exactly the line: status: draft. '
  30. + 'Then read it back, then edit it to replace the literal word draft with final. '
  31. + 'Tell me when done.' }], source: { kind: 'user' } }))
  32. await waitForIdle(ctx, agent)
  33. // Assert the filesystem effect independently of the model response.
  34. const content = await readFile(join(workdir, 'note.txt'), 'utf8')
  35. expect(content).toContain('status: final')
  36. expect(content).not.toContain('draft')
  37. // The log records real read/write/edit tool calls (not bash).
  38. const calls = [...agent.session.events].filter(e => e.type === 'tool/call').map(e => e.data.name)
  39. expect(calls).toContain('write')
  40. expect(calls).toContain('read')
  41. expect(calls).toContain('edit')
  42. }, 180_000)
  43. it('resolves a relative path against the per-session cwd (factory meta.cwd)', async () => {
  44. // config.cwd is the harness workdir, but the agent's SESSION cwd is a
  45. // different dir; the write must land in the SESSION dir, proving the tool
  46. // passes the per-session cwd (not the backend default).
  47. const configDir = await mkdtemp(join(tmpdir(), 'dsh-fs-e2e-cfg-'))
  48. workdir = configDir
  49. const sessionDir = await mkdtemp(join(tmpdir(), 'dsh-fs-e2e-session-'))
  50. try {
  51. ctx = await fsHarness(configDir, SYSTEM)
  52. const handle = await ctx.agents.create({
  53. sessionId: SessionId(`fs-e2e-cwd-${Date.now()}`),
  54. meta: { cwd: sessionDir },
  55. agentOptions: { provider: 'deepseek-official', model: 'deepseek-v4-flash' },
  56. })
  57. handle.agent.followup(createUserMessage({
  58. content: [{ type: 'text', text:
  59. 'Use the write tool to create a file named where.txt containing exactly the line: here. Tell me when done.' }], source: { kind: 'user' } }))
  60. await waitForIdle(ctx, handle.agent)
  61. // The file is in the SESSION dir, not the config dir.
  62. expect(await readFile(join(sessionDir, 'where.txt'), 'utf8')).toContain('here')
  63. await expect(readFile(join(configDir, 'where.txt'), 'utf8')).rejects.toMatchObject({ code: 'ENOENT' })
  64. } finally {
  65. await rm(sessionDir, { recursive: true, force: true })
  66. }
  67. }, 180_000)
  68. })