Building an Agent Plugin: From Troubleshooting to Publishing and Installation
This article examines common failure points in agent plugin development—entry points, build artifacts, local linking, and configuration merging—and provides practical TypeScript, npm, and pnpm examples for troubleshooting editor errors, building artifacts, setting up workspace links, and running pre-publish checks.
Introduction
When writing a plugin, developers often run into several common problems: the editor reports that a module cannot be found, the entry file is missing after a build, the code runs locally but cannot be resolved after publishing, or changes to user configuration do not take effect. These issues usually come from four areas: entry points, build artifacts, linking, and configuration merging.
This article uses plugin development as a thread to break down these four areas one by one, with configuration examples and check commands that can be compared directly against your own project. The examples are based on common TypeScript, npm, and pnpm toolchains. For field names and commands specific to a framework, consult the documentation for that framework.
1. First, Understand the Plugin’s Role
In a plugin-based agent framework, capabilities such as model invocation, tools, session records, memory storage, the agent main loop, sandbox approval, the filesystem, and subagents are usually provided as plugins. The framework itself loads these plugins according to configuration; it does not provide a fixed, unchanging core.
Many packages with base or common in their names do most of their work at the configuration layer. Their entry file often only declares which plugins to mount, and installation writes a batch of plugin entries into a configuration file. Calling their functions is usually not the primary usage.
Once you understand this, you know where to look during troubleshooting. If a capability is not taking effect, first check the plugin entries in the configuration list rather than digging through low-level functions.
2. Minimal Plugin Directory Structure
A plugin directory usually contains the following files:
my-plugin/├── package.json # Name, version, entry point, dependencies├── tsconfig.json # Type checking and compilation config├── src/│ └── index.ts # Plugin entry; exports plugin instance or init function├── templates/ # Optional: prompts, templates, and other resources├── LICENSE└── README.md
When the editor opens the project, it reads the entry field and type declarations in package.json. If the entry points to dist/index.js and the dist directory does not exist yet, the editor reports that the module cannot be found.
3. Three Causes of Editor Errors
Common errors usually come from the following three situations:
- The build artifact referenced by the entry path does not exist.
- The type declaration file is missing, so external users cannot get exported types.
- A path alias is not declared in the configuration, so the editor cannot map
@internal/footo the actual directory.
During troubleshooting, first confirm which mode the project is in. During source development, the entry usually points to src. For published artifacts, the entry points to dist. Mixing entry points from the two modes is the most common cause of errors.
4. Key tsconfig Fields
Here is an example tsconfig.json for a plugin project:
{"compilerOptions": {"target": "ES2022","module": "NodeNext","moduleResolution": "NodeNext","rootDir": "src","outDir": "dist","declaration": true,"declarationMap": true,"sourceMap": true,"strict": true,"baseUrl": ".","paths": {"@internal/*": ["src/internal/*"]}},"include": ["src"]}
The fields work as follows:
rootDirandoutDirdetermine where source is read from and where output is written.moduleandmoduleResolutiondetermine the rules used to resolve modules.declarationgenerates.d.tsfiles. External users rely on them for type hints. Without it, external users usually see exported content asany.sourceMaplets runtime errors map back to source line numbers.paths, together withbaseUrl, defines path aliases.pathsonly affects type checking and compile-time resolution; import paths in the output are not rewritten. At runtime, a bundler or loader still needs to perform the same mapping. Otherwise, the editor can jump to definitions, but the runtime reports that the module cannot be found.
5. Build: From Source to Artifact
The entry point and scripts in package.json can be configured like this:
{"name": "my-plugin","version": "0.1.0","main": "dist/index.js","types": "dist/index.d.ts","files": ["dist", "templates", "README.md", "LICENSE"],"scripts": {"clean": "rm -rf dist","build": "npm run clean && tsc -p tsconfig.json","typecheck": "tsc -p tsconfig.json --noEmit"}}
After the build completes, check three things in order:
npm run build # Run the buildls dist # Confirm index.js and index.d.ts were generatednode --input-type=module -e "import('./dist/index.js').then(() => console.log('entry loadable'))"
The third command actually loads the entry. If the plugin depends on a framework runtime package that is not installed yet, a load failure is expected; install it in the workspace first.
Non-code resources such as templates and prompts are not copied automatically by tsc. Copy them explicitly in the build script, or include them in the published content through the files field. Also, do not hardcode absolute paths in the build configuration. Otherwise, resource paths will break after the artifacts are copied to another machine.
6. Source Mode vs. Artifact Mode
A plugin has two runtime paths, each with different characteristics:
- Source mode: the loader compiles
srcat runtime. Code changes take effect immediately, and editor type checking is more precise. - Artifact mode: the loader reads
distdirectly. No type checking is performed, startup is faster, and dependencies are provided by the installation environment.
The most error-prone part of these two paths is inconsistent entry points. To avoid this, declare both entry points in the exports field of package.json:
{"exports": {".": {"types": "./dist/index.d.ts","development": "./src/index.ts","import": "./dist/index.js","default": "./dist/index.js"}}}
The development condition only takes effect when the loader is started with the corresponding condition parameter, such as --conditions=development. Check the documentation for the loader you use for the exact parameter name. types is placed first so that type tools read the declaration file first.
7. Local Workspace Linking
When multiple plugins are in the same repository, dependencies between them can be resolved through workspace links. Taking pnpm as an example, first declare the workspace scope in the repository root:
# pnpm-workspace.yamlpackages:- "plugins/*"- "apps/*"
Then write the workspace protocol in the depending package’s package.json:
{"dependencies": {"plugin-storage-local": "workspace:*"}}
After running pnpm install, pnpm creates a symbolic link in node_modules pointing to plugins/storage-local. You can confirm whether the link is correct with the following commands:
ls -l node_modules/plugin-storage-localpnpm ls plugin-storage-local
npm workspaces work similarly, but it uses "workspaces": ["plugins/*"] to declare the scope. Dependency versions are usually written as * or a concrete version number, and the workspace: protocol is not supported.
When using workspace links, note the following:
- If the link target points to source code and the loader does not compile source, runtime will fail.
- The package name in the dependency declaration must match the
namefield of the depended-on package. - The depended-on package must be built first. When running
pnpm -r build, pnpm builds packages in dependency order. - If multiple versions of the same package exist, the resolver may choose the wrong version. Use
pnpm lsto check for duplicate versions.
8. Pre-Publish Checks
Before publishing a plugin, complete the following checks:
- Confirm that the entry fields point to the final artifacts, and that the artifacts were generated from the latest source.
- Run the package preflight command to inspect the files that will be uploaded:
npm pack --dry-run
The output file list should include dist/, templates/, package.json, README.md, and LICENSE. It should not include unrelated content such as src/ or test code. For .map files, decide according to project needs. If you want users to see source line numbers, keep them. Otherwise, exclude them in files.
- Check dependency declarations.
pnpm publishreplaces theworkspace:protocol with real version numbers, butnpm publishdoes not recognize this protocol. When publishing with npm, you need to manually replace the dependency declarations with real version numbers.
