spill.spec.ts 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * Showcase integration: the real `web_fetch` tool + the real spill stack
  3. * (`dsh-spill-local` backend + `dsh-spill-policy`), exercised through
  4. * `ctx.tools.execute()`. Proves the Agent Note's default local-backend path — a large
  5. * formatted fetch result is automatically retained and spilled with NO
  6. * tool-specific spill code, and the model-facing text changes ONLY by the
  7. * deliberate spill notice (the full formatted result lands in the spill file).
  8. */
  9. import { afterEach, beforeEach, describe, expect, it } from 'vitest'
  10. import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http'
  11. import { AddressInfo } from 'node:net'
  12. import { mkdtempSync, readFileSync, rmSync } from 'node:fs'
  13. import { tmpdir } from 'node:os'
  14. import { join } from 'node:path'
  15. import { Context } from 'cordis'
  16. import { CallId } from '@deepseek-ai/dsh-llm'
  17. import { SessionId } from '@deepseek-ai/dsh-session'
  18. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  19. import ToolRegistry from '@deepseek-ai/dsh-tools'
  20. import type { ToolExecution } from '@deepseek-ai/dsh-tools'
  21. const testToolSignal = new AbortController().signal
  22. import WebService from '@deepseek-ai/dsh-web'
  23. import * as WebFetchLocal from '@deepseek-ai/dsh-web-fetch-local'
  24. import LocalSpillStore from '@deepseek-ai/dsh-spill-local'
  25. import * as SpillPolicy from '@deepseek-ai/dsh-spill-policy'
  26. import * as ToolWeb from '@deepseek-ai/dsh-tool-web'
  27. type Handler = (req: IncomingMessage, res: ServerResponse) => void
  28. let server: Server
  29. let base: string
  30. let handler: Handler
  31. let spillRoot: string
  32. let ctx: Context
  33. const BODY = 'X'.repeat(4000) // formatted result is well over the policy cap
  34. const MAX_INLINE_BYTES = 1000 // leaves room for a head/tail preview beside the notice
  35. beforeEach(async () => {
  36. handler = (_req, res) => { res.writeHead(200, { 'content-type': 'text/plain' }); res.end(BODY) }
  37. server = createServer((req, res) => { handler(req, res) })
  38. await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve))
  39. base = `http://127.0.0.1:${(server.address() as AddressInfo).port}`
  40. spillRoot = mkdtempSync(join(tmpdir(), 'dsh-spill-web-'))
  41. ctx = new Context()
  42. await ctx.plugin(SystemPrompt)
  43. await ctx.plugin(ToolRegistry)
  44. await ctx.plugin(WebService, { fetchProvider: WebFetchLocal.LOCAL_FETCH_PROVIDER_ID })
  45. // Provider cap generous so the tool returns a large formatted result; the
  46. // policy cap is what triggers the spill (the Agent Note's separation of concerns).
  47. await ctx.plugin(WebFetchLocal, { maxBodyChars: 500_000 })
  48. await ctx.plugin(LocalSpillStore, { root: spillRoot })
  49. await ctx.plugin(SpillPolicy, { maxInlineBytes: MAX_INLINE_BYTES })
  50. await ctx.plugin(ToolWeb)
  51. })
  52. afterEach(async () => {
  53. await new Promise<void>(resolve => server.close(() => { resolve() }))
  54. rmSync(spillRoot, { recursive: true, force: true })
  55. })
  56. /** A web_fetch call carrying a session owner (so the policy can scope the spill). */
  57. function fetchCall(): Promise<{ isError: boolean; content: { type: string; text?: string }[] }> {
  58. const agent = { session: { header: { id: SessionId('web-sess') } } }
  59. const exec = { callId: CallId('call-1'), name: 'web_fetch', arguments: { url: base }, agent, signal: testToolSignal } as unknown as ToolExecution
  60. return ctx.tools.execute(exec)
  61. }
  62. describe('web_fetch spill showcase', () => {
  63. it('spills a large formatted result and returns a preview + spill locator', async () => {
  64. const out = await fetchCall()
  65. expect(out.isError).toBe(false)
  66. const text = out.content.map(b => b.text).join('')
  67. // Model-facing text is a preview + notice within the cap, NOT the full body.
  68. expect(text.length).toBeLessThan(BODY.length)
  69. expect(Buffer.byteLength(text, 'utf8')).toBeLessThanOrEqual(MAX_INLINE_BYTES)
  70. expect(text).toContain(`Fetched ${base}`) // the head of the formatted result survives
  71. expect(text).toContain('Full formatted result stored at:')
  72. expect(text).toContain('Use read with offset/limit, or grep this path')
  73. // The spill file holds the FULL formatted result the tool returned.
  74. const match = /stored at: (\S+?)\. Use read/.exec(text)
  75. expect(match).not.toBeNull()
  76. const spillPath = match![1]!
  77. const saved = readFileSync(spillPath, 'utf8')
  78. // The provider cap was generous, so the tool did not truncate: the spill file
  79. // holds the full formatted result (header + the complete body), far larger
  80. // than the model-facing preview.
  81. expect(saved).toContain('(HTTP 200)')
  82. expect(saved).toContain(BODY)
  83. expect(saved.length).toBeGreaterThan(text.length)
  84. })
  85. })