Plugin Architecture in Practice: A Guide to Five Plugin Forms

    This guide explains how to think about plugin development in terms of five distinct forms: validation, capability, response, configuration, and service. It walks through a shared project skeleton that gets the loading chain working in three steps, then covers each form with code examples and engineering caveats, including absolute path requirements, tool registration, automatic cleanup via effects, schema-driven configuration, and dependency injection. The closing section summarizes practical recommendations and common pitfalls for building maintainable, testable plugins.

    1. Why Plugin Development Needs a “Form-Based” Mindset

    The core value of a plugin architecture is decoupling the stability of the host program from its extensibility. The host only defines extension points and the lifecycle; specific capabilities are mounted by plugins on demand. In practice, however, developers often make one mistake: cramming every requirement into a single plugin, so that the plugin registers tools, listens to events, and parses configuration all at once. The result is hard to reuse and hard to test.

    A more reasonable approach is to classify plugins by “form” first. A form refers to the role a plugin plays in the host, not its business content. A common industry breakdown abstracts into five categories:

    • Validation: only confirms whether the loading chain works;
    • Capability: exposes callable tools to the model or host;
    • Response: inserts logic at specific moments in the framework;
    • Configuration: lets users adjust behavior without changing source code;
    • Service: packages its own capability for consumption by other plugins.

    These five forms are not mutually exclusive; a mature plugin is often a stack of several forms. But breaking them down one form at a time in early development can significantly reduce debugging costs.

    2. Common Skeleton: Three Steps to Get the Loading Chain Working

    All five forms share the same directory structure and startup method; the only difference is what the source code registers. The benefit of this design is that switching examples requires no change to the run command.

    Step one: create a dedicated directory. Under the checkout root, create scratch-plugin/src/. All example source code goes into my-plugin.ts, with an overlay config file cordis.yml placed in the same directory.

    Step two: write the overlay config. The config does only one thing: use - insert to insert a plugin node into the plugin tree, where id is the node identifier and name points to the absolute path of the plugin source.

    1. - insert:
    2. - id: demo
    3. name: '/absolute/path/to/scratch-plugin/src/my-plugin.ts'

    Step three: start the host in patch mode and pass in the overlay config. The expected result is that the UI comes up normally and the plugin is attached to the plugin tree.

    There is one high-frequency pitfall that must be emphasized here: the path must be an absolute path. The overlay only contributes config and does not change the profile directory used by the loader when resolving module paths. So relative paths often resolve to unexpected locations, showing up as “the plugin seemingly loads successfully but logs don’t print.” The recommended troubleshooting order is: first confirm the path form, then confirm the build artifact exists, and finally check whether the corresponding node appears in the plugin tree.

    3. Form One: Minimal Plugin to Validate the Environment and Loading Chain

    A minimal plugin has only an apply function and a name export. name identifies the plugin; apply runs when the plugin is loaded and receives a context object as its argument.

    1. import type { Context } from '@deepseek-ai/cordis'
    2. export const name = 'hello-plugin'
    3. export function apply(ctx: Context) {
    4. console.log('[hello-plugin] plugin loaded!')
    5. }

    Its value is not in functionality but in acting as a “chain probe.” After startup, the terminal should print the corresponding log; if you don’t see it, don’t continue developing further—fix the path and build issues first. In practice, it’s worth making this a team convention: the first version of any new plugin is a minimal plugin, and capabilities are layered on only after it passes.

    4. Form Two: Model Tools That Let the Model Call Your Capability

    A tool plugin declares its dependency via inject: ['tools'], then uses defineTool to register a callable unit with a name, description, and parameter schema. The quality of the description text directly determines how accurately the model selects the tool, so write clearly “what it does, when to use it, what the parameters mean,” and avoid vague wording.

    1. export const inject = ['tools']
    2. export function apply(ctx: Context) {
    3. ctx.tools.defineTool({
    4. name: 'query_status',
    5. description: 'Query the current status of a specified resource; use when real-time status is needed',
    6. parameters: { /* parameter schema */ },
    7. async execute(args) { /* business logic */ }
    8. })
    9. }

    Three engineering points to note: first, parameter validation should be done at the entry point to avoid passing illegal input downstream; second, tool execution should have timeout and error-return conventions so the model can retry or reroute accordingly; third, keep tool names semantically stable and don’t rename them arbitrarily between versions.

    5. Form Three: Event Listening and Automatic Cleanup

    A response plugin focuses on “doing the right thing at the right time.” Registering side effects via ctx.effect allows cleanup logic to run automatically when the plugin is unloaded, avoiding leaks of listeners, timers, and connections.

    1. export function apply(ctx: Context) {
    2. ctx.effect(() => {
    3. const timer = setInterval(() => {}, 1000)
    4. return () => clearInterval(timer)
    5. })
    6. }

    The return value of effect is the cleanup function—this is the unified convention for lifecycle management. Any operation that consumes resources should be wrapped in effect. This ensures the host environment stays clean when a plugin is hot-reloaded or removed, without manually tracking the release point of every resource.

    6. Form Four: Plugins with Configuration

    A configuration plugin externalizes variable behavior. The approach is to export a config type and a corresponding schema, with the host responsible for validation and injection. When designing config, three principles are recommended: keep field names flat and semantically clear; provide reasonable defaults for each field; constrain value ranges so errors are caught at startup rather than at runtime.

    1. export interface Config { level: 'debug' | 'info' | 'warn' }
    2. export const schema = { /* field constraints and defaults */ }
    3. export function apply(ctx: Context, config: Config) { /* use config */ }

    The payoff of configuration is operability: the same codebase can adapt to different environments without rebuilding.

    7. Form Five: Providing a Service for Other Plugins to Reuse

    A service plugin packages its capability as a service class and declares dependencies via inject; other plugins declare a dependency on the same service to obtain an instance. This is essentially dependency injection: the host handles instantiation and assembly, and plugins depend only on contracts, not implementations.

    Practical advice: keep service interfaces stable while internal implementations evolve freely; handle the case where “the dependency is not ready yet” when calling a service; avoid circular dependencies between services, and introduce an intermediate layer to decouple when necessary.

    8. Implementation Advice and Common Pitfalls

    First, advance in small steps by form, with each step independently verifiable. Second, unify the directory and startup method to lower switching costs. Third, stick to absolute paths and build artifact checks. Fourth, bring all resource acquisition under a unified cleanup mechanism. Fifth, define config and service contracts first, then implement. Sixth, prepare a minimal verification case for each plugin to form a regression baseline.

    In summary, the difficulty of plugin development lies not in syntax but in form selection and lifecycle discipline. Understand the five forms thoroughly, and the vast majority of extension needs will find a clear landing point.