Files
deepseek-harness/packages/experimental/claude-code-mods/tests/loader-composition.spec.ts
T
Tianyi Cui f1715c67ec refactor(experimental): make mods DSH plugins and drop the persistence change
A mod is now a plugin built with defineMod() around its register(on,
options); the bridge is the ClaudeCodeMods service (ctx.claudeCodeMods)
that keeps the loaded mods and raises their events. The plugin.json /
hooks.json loader, pluginDirs, and dynamic imports are gone.

prompt.submit context becomes text blocks appended to the human's own
prompt message and $.prompt.submit queues a user-sourced message, so the
bridge adds no message source kind: the persistence record and the
regenerated schema are removed and the artifacts match master again.

Engine correctness from the review round: next() is memoized and every
started run beneath is awaited before a hook's answer settles; a rewritten
tool argument skips the hook with a report pointing at the pre-tool input
rewrite proposal; timers belong to the session whose event started them;
$.tool.call injects a tool's deferred contexts; $.clock.sleep honors the
event signal; a built-in tool answered with { result } is validated against
its output schema before it becomes an error-shaped result; hooks on events
this host never raises are reported at load.

Examples: the three mods from the Claude Code blog post under examples/,
Token Weather and its test unchanged, Blast Radius and Replay Theater
completed from the published fragments, run by their own tests through a
claude-code/testing alias over the new test kit ($.ui.mount/find/press).
2026-10-03 01:38:21 +08:00

82 lines
4.1 KiB
TypeScript

/** Real Loader composition: the bridge mounted from a cordis.yml row beside the shipped core plugins. */
import { mkdtemp, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join, resolve } from 'node:path'
import { pathToFileURL } from 'node:url'
import { afterEach, expect, it } from 'vitest'
import { Context } from '@deepseek-ai/cordis'
import Loader from '@deepseek-ai/cordis-plugin-loader'
import Include from '@deepseek-ai/cordis-plugin-include'
import AgentRegistry from '@deepseek-ai/dsh-agent'
import AgentLoop from '@deepseek-ai/dsh-agent-loop'
import CommandRuntime from '@deepseek-ai/dsh-commands'
import LlmRuntime, { createUserMessage } from '@deepseek-ai/dsh-llm'
import SessionStore, { SessionId } from '@deepseek-ai/dsh-session'
import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection'
import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
import ToolRuntime, { defineContentToolFixture } from '@deepseek-ai/dsh-tools'
import * as ClaudeCodeMods from '../src/index.ts'
import { MockAdapter, textResponse, toolCallResponse } from '../../../core/agent-loop/tests/mock-adapter.ts'
const FIXTURES = resolve(import.meta.dirname, 'fixtures')
let ctx: Context | undefined
let root: string | undefined
afterEach(async () => {
await ctx?.fiber.dispose()
if (root !== undefined) await rm(root, { recursive: true, force: true })
ctx = undefined
root = undefined
})
it('loads from cordis.yml, counts the model\'s tool calls, and answers /tally through the composed command registry', async () => {
root = await mkdtemp(join(tmpdir(), 'dsh-cc-mods-composition-'))
const configPath = join(root, 'cordis.yml')
const modules = new Map<string, unknown>([
['@deepseek-ai/dsh-llm', LlmRuntime],
['@deepseek-ai/dsh-session', SessionStore],
['@deepseek-ai/dsh-session-projection', SessionProjectionRegistry],
['@deepseek-ai/dsh-system-prompt', SystemPrompt],
['@deepseek-ai/dsh-tools', ToolRuntime],
['@deepseek-ai/dsh-agent', AgentRegistry],
['@deepseek-ai/dsh-agent-loop', AgentLoop],
['@deepseek-ai/dsh-commands', CommandRuntime],
['@deepseek-ai/dsh-experimental-claude-code-mods', ClaudeCodeMods],
])
// A mod is a plugin like any other: here the tutorial mod's `defineMod` wrapper, mounted by file URL after the bridge.
const firstMod = pathToFileURL(resolve(FIXTURES, 'first-mod.ts')).href
await writeFile(configPath, [
...[...modules.keys()].map(name => `- name: '${name}'`),
`- name: '${firstMod}'`,
' config:',
' greeting: The model made',
].join('\n') + '\n')
const context = ctx = new Context()
context.baseUrl = pathToFileURL(root).href + '/'
await context.plugin(Loader)
context.loader.builtins.include = Include
// Without a Node internal loader the Loader imports bare names through this
// test runner's module graph, which resolves workspace packages to `src`.
context.loader.internal = undefined
await context.loader.create({ name: 'cordis:include', config: { path: pathToFileURL(configPath).href } })
await context.loader.await()
for (const entry of context.loader.entries()) await entry.fiber?.await()
const adapter = new MockAdapter([toolCallResponse('c1', 'echo', { command: 'ls' }), textResponse('listed')])
context.llm.registerAdapter(['mock'], adapter)
context.tools.register(defineContentToolFixture({
name: 'echo', description: 'echo', parameters: { command: { type: 'string' } },
async execute(args) { return [{ type: 'text', text: `ran ${args.command}` }] },
}))
const agent = await context.agentLoop.create(SessionId('composed'), { provider: 'mock', model: 'mock' })
expect(context.commands.list(agent).map(command => command.name)).toEqual(['tally'])
agent.followup(createUserMessage({ content: [{ type: 'text', text: 'list the files here' }], source: { kind: 'user' } }))
await agent.whenIdle()
expect(adapter.requests).toHaveLength(2)
const run = await context.commands.execute(agent, '/tally', [], new AbortController().signal)
expect(run?.result).toEqual({ kind: 'success', text: 'first-mod: The model made 1 tool calls since this mod loaded' })
})