Building a Plugin-Based AI Agent Framework: A Guide to Writing Your Own Plugin
This article explores development practices for building plugins in a modular AI Agent framework. It covers the "everything is a plugin" design philosophy, troubleshooting common editor errors, clarifying core TypeScript configuration concepts, understanding the build process, and navigating the differences between source code and build artifacts. Readers will also learn about workspace mechanisms, the complete lifecycle from publication to runtime module resolution, and profile management, providing a comprehensive mental model for plugin development.
1. Core Philosophy: Everything is a Plugin
Modern plugin-based agent frameworks typically follow a simple but thorough design philosophy: everything is a plugin. There is no “privileged core” in the framework that requires patching. Capabilities such as model adaptation, tool invocation, session logging, and the agent’s main loop all exist as plugins, combined through configuration.
The most common misunderstanding for beginners involves packages that look like the “framework base.” Take a typical “base package” as an example. Its name sounds like an imported underlying library, but it is actually a composition package. Its package.json declares a bundle type and uses a patch-like configuration file to insert dozens of plugin lines into an empty configuration tree, typically including:
- Scheduled tasks and hot-reload plugins;
- LLM capability abstraction and specific model adapters;
- Session event logging and memory storage;
- Agent interface and default main loop driver;
- Tool registry and system prompt assembly;
- Subprocess and shell execution capabilities;
- Sandbox and approval policies;
- File system capabilities and access policies;
- User settings and credential management;
- Sub-agents and workflow orchestration.
In other words, this “base package” is not an API library to be called, but a configuration manifest stating “I want to mount these plugins.” Making the base a plugin means the base itself can be replaced, trimmed, or reorganized. The framework’s extension points are completely uniform.
2. Your First Plugin: Troubleshooting Editor Errors
When beginners follow a tutorial to write their first plugin, the most common issue is red squiggles in the editor: types not found, or module resolution failed. Do not rush to doubt your code; in most cases, it is an engineering configuration issue. Troubleshooting recommendations in order:
- Confirm dependencies are correctly installed in the workspace, not in an isolated directory;
- Check if
moduleResolutionintsconfigmatches the module type; - Ensure the editor uses the project’s own TypeScript version and configuration, not the global version;
- Restart the language service to clear cache interference.
Feeding the complete error message to an AI assistant can often quickly pinpoint that it is a resolution strategy issue rather than a business code problem.
3. Clarifying Core tsconfig Concepts
Several frequently confused points in plugin development:
- module vs moduleResolution: The former determines what module syntax the compiled output uses, while the latter determines how the compiler finds module declarations. The two must coordinate. For example, if the output uses ESM, the resolution strategy should usually be
bundlerornode16series. - paths: These are only compile-time aliases and do not take effect at runtime; if the runtime environment does not do the same mapping, you must ensure aliases are correctly handled during bundling.
- types and typeRoots: These determine the loading scope of global types. Configuring these too broadly or too narrowly will lead to the strange phenomenon of “editor showing errors but compilation succeeding.”
4. Build System: What Does pnpm build Actually Do?
When executing the build command, three things usually happen: first, old artifacts are cleaned; second, the TypeScript compiler performs type checking and transpilation; finally, the bundler merges the plugin and its dependencies into distributable artifacts. Understanding the significance of this step is crucial: what you publish to others is the “artifact,” not your source code directory.
5. Source Startup vs. Build Artifacts: Two Execution Paths
During development, source startup is recommended: running directly with .ts files as entry points, combined with hot-reload, so changes take effect immediately. For production or distribution, the build artifact path is used: loading the bundled .js files. The module resolution behavior of these two paths differs, which is the root cause of the “it works locally, but throws errors when installed elsewhere” issue.
6. Workspace Mechanism
In a monorepo, linkWorkspacePackages allows local packages to reference each other via symbolic links; the workspace: protocol is replaced with the real version number during publication. Understanding this is key to understanding why everything works fine during local debugging, but versions mismatch after release.
7. Complete Link of Publication and Runtime Module Resolution
After publication, when the host environment loads your plugin, the resolution link is: the exports field in the entry package.json → the corresponding artifact file → the artifact internally resolves its dependencies. A missing link in any step (such as missing type declarations or un-declared sub-paths in exports) will manifest as a runtime error.
8. Profile Mechanism and Summary
The profile directory under the user’s home directory stores a complete snapshot of a specific plugin combination and configuration, acting like a “configuration profile” that can be switched between different scenarios.
To summarize this into a single mental model: Plugin-based framework = uniform extension points + configuration-driven composition + standard engineering pipeline. Mastering the main thread of “editor resolution → engineering configuration → build → publication → runtime resolution” equips you with all the key aspects of plugin development.
