inventory.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import { afterEach, describe, expect, it } from 'vitest'
  2. import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises'
  3. import { tmpdir } from 'node:os'
  4. import { join } from 'node:path'
  5. import { pathToFileURL } from 'node:url'
  6. import { Context } from '@deepseek-ai/cordis'
  7. import Loader from '@deepseek-ai/cordis-plugin-loader'
  8. import Include from '@deepseek-ai/cordis-plugin-include'
  9. import AgentRegistry, { type Agent } from '@deepseek-ai/dsh-agent'
  10. import { SessionId } from '@deepseek-ai/dsh-session'
  11. import { createScope } from '@deepseek-ai/dsh-scope'
  12. import AgentPresets, { mountPreset } from '@deepseek-ai/dsh-agent-presets'
  13. import DeepSeekLlmApiExtensionRegistry from '@deepseek-ai/dsh-deepseek-llm-api-extensions'
  14. import * as PluginInventory from '../src/index.ts'
  15. const contexts: Context[] = []
  16. const roots: string[] = []
  17. const SIGNAL = new AbortController().signal
  18. afterEach(async () => {
  19. await Promise.all(contexts.splice(0).map(ctx => ctx.fiber.dispose()))
  20. await Promise.all(roots.splice(0).map(root => rm(root, { recursive: true, force: true })))
  21. })
  22. async function packagePlugin(
  23. root: string,
  24. dir: string,
  25. manifest: object,
  26. source = 'export default () => {}\n',
  27. ): Promise<string> {
  28. const packageDir = join(root, dir)
  29. await mkdir(packageDir, { recursive: true })
  30. await writeFile(join(packageDir, 'package.json'), `${JSON.stringify({ type: 'module', ...manifest })}\n`)
  31. await writeFile(join(packageDir, 'plugin.mjs'), source)
  32. return `./${dir}/plugin.mjs`
  33. }
  34. async function harness(enabled?: boolean): Promise<{ ctx: Context; root: string; disposeInventory: () => Promise<void> }> {
  35. const root = await mkdtemp(join(tmpdir(), 'dsh-plugin-packages-'))
  36. roots.push(root)
  37. const ctx = new Context()
  38. contexts.push(ctx)
  39. ctx.baseUrl = pathToFileURL(join(root, 'cordis.yml')).href
  40. await ctx.plugin(Loader)
  41. ctx.loader.builtins.include = Include
  42. await ctx.plugin(AgentRegistry)
  43. await ctx.plugin(AgentPresets, { default: 'fixture', roots: [], includeShippedRoot: false, includeUserRoot: false })
  44. await ctx.plugin(DeepSeekLlmApiExtensionRegistry)
  45. const inventory = enabled === undefined
  46. ? ctx.plugin(PluginInventory)
  47. : ctx.plugin(PluginInventory, { enabled })
  48. await inventory
  49. return { ctx, root, disposeInventory: () => inventory.dispose() }
  50. }
  51. describe('DeepSeek plugin package inventory', () => {
  52. it('contributes by default and can be explicitly disabled', async () => {
  53. const defaultHarness = await harness()
  54. const defaultFields = await defaultHarness.ctx.deepseekLlmApiExtensions.prepare({
  55. body: { messages: [] }, signal: SIGNAL,
  56. })
  57. expect(defaultFields.fields).toHaveProperty('dsh_plugin_packages')
  58. const disabledHarness = await harness(false)
  59. const disabledFields = await disabledHarness.ctx.deepseekLlmApiExtensions.prepare({
  60. body: { messages: [] }, signal: SIGNAL,
  61. })
  62. expect(disabledFields.fields).not.toHaveProperty('dsh_plugin_packages')
  63. })
  64. it('reports active package versions once, retains parallel versions, and excludes inactive or loose entries', async () => {
  65. const { ctx, root } = await harness()
  66. const oneA = await packagePlugin(root, 'one-a', { name: 'one', version: '1.0.0' })
  67. const oneB = await packagePlugin(root, 'one-b', { name: 'one', version: '2.0.0' })
  68. const disabled = await packagePlugin(root, 'disabled', { name: 'disabled', version: '1.0.0' })
  69. await mkdir(join(root, 'loose'), { recursive: true })
  70. await writeFile(join(root, 'loose/plugin.mjs'), 'export default () => {}\n')
  71. await ctx.loader.create({ name: oneA })
  72. await ctx.loader.create({ name: oneA })
  73. await ctx.loader.create({ name: oneB })
  74. await ctx.loader.create({ name: disabled, disabled: true })
  75. await ctx.loader.create({ name: './loose/plugin.mjs' })
  76. const prepared = await ctx.deepseekLlmApiExtensions.prepare({ body: { messages: [] }, signal: SIGNAL })
  77. expect(prepared.fields.dsh_plugin_packages).toEqual({
  78. version: 1,
  79. packages: [
  80. { name: 'one', version: '1.0.0' },
  81. { name: 'one', version: '2.0.0' },
  82. ],
  83. })
  84. })
  85. it('fails request preparation for an active package with malformed identity metadata', async () => {
  86. const { ctx, root } = await harness()
  87. const bad = await packagePlugin(root, 'bad', { name: 'bad' })
  88. await ctx.loader.create({ name: bad })
  89. await expect(ctx.deepseekLlmApiExtensions.prepare({ body: { messages: [] }, signal: SIGNAL }))
  90. .rejects.toThrow(/must declare non-empty name and version/)
  91. })
  92. it('omits a loose ESM module whose nearest manifest only marks the module type', async () => {
  93. const { ctx, root } = await harness()
  94. const marker = await packagePlugin(root, 'marker-only', {})
  95. await ctx.loader.create({ name: marker })
  96. await expect(ctx.deepseekLlmApiExtensions.prepare({ body: { messages: [] }, signal: SIGNAL }))
  97. .resolves.toMatchObject({ fields: { dsh_plugin_packages: { version: 1, packages: [] } } })
  98. })
  99. it('uses the host inventory when a request has no matching or joined live agent', async () => {
  100. const { ctx, root } = await harness()
  101. const plugin = await packagePlugin(root, 'host-only', { name: 'host-only', version: '3.0.0' })
  102. await ctx.loader.create({ name: plugin })
  103. const missing = await ctx.deepseekLlmApiExtensions.prepare({ body: { messages: [] }, signal: SIGNAL, sessionId: 'missing' })
  104. expect(missing.fields.dsh_plugin_packages?.packages).toEqual([{ name: 'host-only', version: '3.0.0' }])
  105. const id = SessionId('bare-agent')
  106. const agentScope = createScope(ctx, {})
  107. ctx.agents.register({ id, ctx: agentScope.ctx, session: { id } } as unknown as Agent)
  108. const bare = await ctx.deepseekLlmApiExtensions.prepare({ body: { messages: [] }, signal: SIGNAL, sessionId: id })
  109. expect(bare.fields.dsh_plugin_packages?.packages).toEqual([{ name: 'host-only', version: '3.0.0' }])
  110. })
  111. it('resolves scoped and unscoped bare subpaths, absolute/file modules, and skips URL or Cordis modules', async () => {
  112. const { ctx, root } = await harness()
  113. await packagePlugin(root, 'node_modules/plain-package', { name: 'plain-package', version: '1.0.0' })
  114. await packagePlugin(root, 'node_modules/@scope/scoped-package', { name: '@scope/scoped-package', version: '2.0.0' })
  115. await packagePlugin(root, 'absolute-package', { name: 'absolute-package', version: '3.0.0' })
  116. const absolute = join(root, 'absolute-package/plugin.mjs')
  117. const internal = ctx.loader.internal
  118. ctx.loader.internal = {
  119. version: 'v2',
  120. import: async (specifier: string, ...args: unknown[]) => {
  121. if (specifier === 'https://plugins.example/test.mjs') return { default: () => {} }
  122. // Node ESM on Windows requires a file URL; retain the raw Loader name for package attribution.
  123. const portableSpecifier = specifier === absolute ? pathToFileURL(specifier).href : specifier
  124. return await (internal as never as { import(specifier: string, ...args: unknown[]): Promise<unknown> })
  125. .import(portableSpecifier, ...args)
  126. },
  127. } as unknown as NonNullable<typeof ctx.loader.internal>
  128. await ctx.loader.create({ name: 'plain-package/plugin.mjs' })
  129. await ctx.loader.create({ name: '@scope/scoped-package/plugin.mjs' })
  130. await ctx.loader.create({ name: absolute })
  131. await ctx.loader.create({ name: pathToFileURL(absolute).href })
  132. ctx.loader.builtins.noop = () => {}
  133. await ctx.loader.create({ name: 'cordis:noop' })
  134. await ctx.loader.create({ name: 'https://plugins.example/test.mjs' })
  135. const prepared = await ctx.deepseekLlmApiExtensions.prepare({ body: { messages: [] }, signal: SIGNAL })
  136. expect(prepared.fields.dsh_plugin_packages?.packages).toEqual([
  137. { name: '@scope/scoped-package', version: '2.0.0' },
  138. { name: 'absolute-package', version: '3.0.0' },
  139. { name: 'plain-package', version: '1.0.0' },
  140. ])
  141. })
  142. it('fails when a Loader-resolved bare entry has no package manifest', async () => {
  143. const { ctx } = await harness()
  144. ctx.loader.internal = {
  145. version: 'v2',
  146. import: async () => ({ default: () => {} }),
  147. } as unknown as NonNullable<typeof ctx.loader.internal>
  148. await ctx.loader.create({ name: 'missing-package' })
  149. await expect(ctx.deepseekLlmApiExtensions.prepare({ body: { messages: [] }, signal: SIGNAL }))
  150. .rejects.toThrow(/cannot resolve active package/)
  151. })
  152. it('supports a direct embedding whose context has no base URL', async () => {
  153. const ctx = new Context()
  154. contexts.push(ctx)
  155. await ctx.plugin(Loader)
  156. await ctx.plugin(AgentRegistry)
  157. await ctx.plugin(DeepSeekLlmApiExtensionRegistry)
  158. await ctx.plugin(PluginInventory)
  159. const prepared = await ctx.deepseekLlmApiExtensions.prepare({ body: { messages: [] }, signal: SIGNAL })
  160. expect(prepared.fields.dsh_plugin_packages).toEqual({ version: 1, packages: [] })
  161. })
  162. it('uses each ordinary Loader tree base for conflicting bare package versions', async () => {
  163. const { ctx, root } = await harness()
  164. await packagePlugin(root, 'node_modules/versioned-plugin', {
  165. name: 'versioned-plugin', version: '1.0.0',
  166. })
  167. const nestedRoot = join(root, 'nested')
  168. await packagePlugin(nestedRoot, 'node_modules/versioned-plugin', {
  169. name: 'versioned-plugin', version: '2.0.0',
  170. })
  171. const composition = join(nestedRoot, 'cordis.yml')
  172. await writeFile(composition, '- id: nested\n name: versioned-plugin/plugin.mjs\n')
  173. await ctx.loader.create({ name: 'versioned-plugin/plugin.mjs' })
  174. await ctx.loader.create({ name: 'cordis:include', config: { path: pathToFileURL(composition).href } })
  175. const prepared = await ctx.deepseekLlmApiExtensions.prepare({ body: { messages: [] }, signal: SIGNAL })
  176. expect(prepared.fields.dsh_plugin_packages?.packages).toEqual([
  177. { name: 'versioned-plugin', version: '1.0.0' },
  178. { name: 'versioned-plugin', version: '2.0.0' },
  179. ])
  180. })
  181. it('mirrors the standing preset bare-package override instead of its local node_modules', async () => {
  182. const { ctx, root } = await harness()
  183. await packagePlugin(root, 'node_modules/preset-only', { name: 'preset-only', version: '4.0.0' })
  184. const presetDir = join(root, 'preset')
  185. await mkdir(presetDir, { recursive: true })
  186. await packagePlugin(presetDir, 'node_modules/preset-only', { name: 'preset-only', version: '9.0.0' })
  187. const composition = join(presetDir, 'agent.cordis.yml')
  188. await writeFile(composition, '- id: preset-only\n name: preset-only/plugin.mjs\n')
  189. const standingKey = {}
  190. const standing = createScope(ctx, standingKey)
  191. await mountPreset(standing.ctx, { id: 'fixture', trust: 'user', path: composition })
  192. const agentKey = {}
  193. const agentScope = createScope(ctx, agentKey, { parent: standingKey })
  194. const id = SessionId('preset-agent')
  195. const agent = { id, ctx: agentScope.ctx, session: { id } } as unknown as Agent
  196. ctx.agents.register(agent)
  197. const prepared = await ctx.deepseekLlmApiExtensions.prepare({ body: { messages: [] }, signal: SIGNAL, sessionId: id })
  198. expect(prepared.fields.dsh_plugin_packages?.packages).toEqual([{ name: 'preset-only', version: '4.0.0' }])
  199. })
  200. it('withdraws the inventory field when the contributing plugin reloads', async () => {
  201. const { ctx, disposeInventory } = await harness()
  202. expect((await ctx.deepseekLlmApiExtensions.prepare({ body: { messages: [] }, signal: SIGNAL })).fields)
  203. .toHaveProperty('dsh_plugin_packages')
  204. await disposeInventory()
  205. expect((await ctx.deepseekLlmApiExtensions.prepare({ body: { messages: [] }, signal: SIGNAL })).fields)
  206. .not.toHaveProperty('dsh_plugin_packages')
  207. })
  208. })