fs-tools.e2e.ts 3.5 KB

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