Plugin Architecture in Practice: A Guide to Five Plugin Forms

    This article explains how to think about plugin development in terms of "forms," a classification that answers who triggers a piece of logic, what it exposes, and what it depends on. It walks through a shared three-step loading skeleton, then details five plugin forms—minimal loader, model tool, event listener, configuration-driven, and service provider—with TypeScript and YAML examples. You will also find selection guidance and common pitfalls, such as relative paths, uncleaned listeners, vague tool descriptions, missing config defaults, and overly broad service interfaces.

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

    In a plugin system, the most common difficulty for developers is not knowing how to write code, but not knowing what to register. A single extension framework often provides several mechanisms at once—events, tools, configuration, and services. Without a clear classification, code quickly degrades into a pile that is hard to maintain.

    Classifying plugins by form is essentially answering three questions: who triggers this logic, what does it expose, and what does it depend on. Around these three questions, common extensions can be abstracted into five forms: minimal loader, capability injection, event listener, configuration-driven, and service provider. They are not mutually exclusive—a mature plugin is often a combination of several forms—but at the entry stage, mastering them one by one is more efficient than writing them all mixed together.

    2. The Common Skeleton: Three Steps to a Working Load Path

    All five forms share the same directory structure and startup method; the only difference is what gets registered in the source code. This consistency is the foundation of a maintainable plugin system.

    Step one: create a standalone directory. Under the checkout root, create scratch-plugin/src/ to hold the plugin source, and place an overlay configuration file alongside it. Directory layout:

    1. deepseek-harness/ # checkout root
    2. └── scratch-plugin/
    3. ├── src/my-plugin.ts # all five examples live here
    4. └── cordis.yml # overlay configuration

    Step two: write the overlay configuration. Use - insert in the configuration to append a node, where id is the instance identifier and name is the absolute path to 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 overlay mode so the configuration is merged into the main configuration tree. After startup, the web interface is available and the plugin is mounted into the plugin tree.

    Two engineering details deserve emphasis. First, paths generally must be absolute, because the overlay only contributes configuration and does not change the profile directory the loader uses when resolving modules; relative paths are easily resolved to the wrong location. Second, if you do not see the expected logs after startup, check the path and build artifacts first rather than piling on more features—the load path is the foundation for every form that follows.

    3. Form One: The Minimal Plugin, Verifying the Environment and Load Path

    The minimal plugin does only one thing: print a line of log output at load time. Its value is decoupling “is the environment ready” from “is the business logic correct.”

    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. }

    There are two conventions here: name identifies the instance in the plugin tree, and apply is the load entry point that receives the context object. The expected result is the corresponding log appearing in the terminal at startup. If you do not see this line, the problem is in the environment or assembly stage and should be resolved first, before continuing development.

    4. Form Two: Model Tools, Handing Capabilities to the Model

    When you want the model to actively use your capability, you need to declare a dependency on the context and register a tool. The core is two actions: declare the dependency on tool capability via inject: ['tools'], then use defineTool to describe the tool’s name, parameter schema, and handler function.

    1. export const inject = ['tools']
    2. export function apply(ctx: Context) {
    3. ctx.tools.defineTool({
    4. name: 'query_order',
    5. description: 'Query order status by order ID',
    6. parameters: { /* JSON Schema */ },
    7. handler: async (args) => ({ /* structured result */ })
    8. })
    9. }

    There are three engineering points: the description should target the model rather than humans, stating clearly when the tool applies and when it does not; the parameter schema must be strict to avoid inputs the model generates that cannot be validated; and the return value should be structured and serializable so the model can continue reasoning. Tools are the form most likely to “look usable but actually be hard to use,” and description quality often affects results more than implementation quality.

    5. Form Three: Event Listening, Inserting Logic at Framework Moments

    The event form is used to insert logic at specific moments in the framework, such as startup completion, session creation, and before or after tool calls. The difficulty is not listening but cleanup: manually registered listeners that are not released will fire repeatedly when the plugin reloads.

    Using the ctx.effect mechanism provided by the context, you can bind side effects to the plugin lifecycle and reclaim them automatically when the plugin unloads:

    1. export function apply(ctx: Context) {
    2. ctx.effect(() => {
    3. const off = ctx.on('session/create', (s) => { /* ... */ })
    4. return () => off() // return the cleanup function
    5. })
    6. }

    This “register and let the framework manage it” style moves resource management from the developer’s memory to framework constraints, and is a key practice for avoiding leaks in long-running processes.

    6. Form Four: With Configuration, Letting Users Change Plugin Behavior

    The configuration form solves the problem of “same code, different behavior.” The approach is to export the configuration type and a schema description, let the host handle validation and injection, and have the plugin internally consume only the validated configuration object.

    1. export interface Config { level: 'debug' | 'info'; retries: number }
    2. export const schema = { /* describe field types and default values */ }
    3. export function apply(ctx: Context, config: Config) {
    4. // use config.level / config.retries
    5. }

    The principles are: default values must make the plugin usable with zero configuration; validation should be pushed to the host to avoid scattered checks at runtime; and configuration names should remain stable, with a compatibility path provided when changes occur—otherwise the cost of upgrades for users accumulates quickly.

    7. Form Five: Providing a Service, Letting Other Plugins Reuse You

    The service form turns a plugin from a “consumer” into a “provider.” Typically you implement a service class and attach it to the context, and other plugins obtain it through dependency injection:

    1. export class OrderService { /* ... */ }
    2. export const inject = ['services']
    3. export function apply(ctx: Context) {
    4. ctx.set('orderService', new OrderService())
    5. }

    After the dependent declares inject, it can use the service; if it is not ready, the framework defers loading, avoiding initialization-order coupling. In design, keep the service interface narrow and stable, and leave volatile details inside the implementation.

    8. Selection Guidance and Common Pitfalls

    Selection can be summed up in one rule: choose form one if you only need to be loaded; form two if you want the model to call you; form three if you need to hook a moment; form four if behavior must be variable; form five if you need to be reused. Common pitfalls include: writing a relative path that causes load failure, uncleaned listeners causing repeated execution, vague tool descriptions causing call failures, missing configuration defaults, and overly broad service interfaces triggering cascading changes. It is recommended to add forms one layer at a time, verifying load and unload behavior after each layer before moving to the next—this keeps the cost of locating problems lowest.