spill.spec.ts 4.4 KB

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