middleware.js utility functions, cross-file references, runtime scheduled commands, and group management methods. Public plugins should prefer the explicitly listed functions and avoid relying on unpublished internal actions.
importModule(module, versionOrOptions?, manager?)
Ensure an npm dependency is available, then return the result of require(module).
string
required
Module name used by
require(...).string | object
A string means version. An object can contain
package, version, and manager.string
Real package name. Use this when the package name differs from the import name, such as subpath imports or aliased packages.
string
Required version.
string
Package manager name. Usually you do not need to pass this; the runtime handles it by default.
importJs(target)
importJs(...) is a global function in the ES5 / JavaScript runtime. It references other .js files in the plugin workspace, or files from another plugin in the same JavaScript runtime family. It eventually returns module.exports from the target file, similar to require(resolvedPath).
target format
source_script_id / namespace matching rules:
- The
specterbefore the colon is not a folder name. The runtime matches it against the target plugin author, plugin name, marketplace author, plugin ID prefix, and marketplace source script ID (source_script_id) prefix. - For marketplace plugins,
source_script_idusually looks likespecter:hook. The runtime uses the prefix before the colon, soimportJs('specter:hook.js')can locate that source plugin. - If both a marketplace plugin and a local plugin match the same namespace, the marketplace plugin wins. If no marketplace match exists, local plugins are considered.
File resolution behavior
- The path after the colon is the file path inside the target plugin. For example,
specter:lib/hookresolves tolib/hook.jsin the target plugin. .jsis added automatically when no extension exists.- The target file is copied into the current runtime workspace, and the runtime only loads from the workspace.
- Escaping the workspace is prohibited. For example, if resolution lands outside the workspace, it reports
importJs target is outside runtime workspace. - A missing target reports
importJs target not found. If no cross-plugin namespace match exists, the runtime continues as a normal current-workspace file lookup and then reports not found if still missing. - Only the same runtime family can reference each other:
es5can only referencees5;nodejs/typescriptcan reference each other; they cannot cross to Python or other runtimes. - If the namespace matches a plugin but the runtime family is incompatible, it reports runtime mismatch directly instead of falling back to mixed use.
System and utility functions
Cron
Cron manages runtime scheduled commands. It is different from manifest-level cron:
//[cron: ...]: triggers the current plugin on a fixed expression.new Cron(): dynamically adds, removes, updates, and lists runtime scheduled commands while a plugin runs.
RuntimeCronItem example:
A new run triggered by runtime cron has source
runtime_api, and its parameters include runtime_cron_id and cmd. If you only need to run the current plugin on a fixed schedule, prefer manifest cron.Group management
These methods depend on current IM adapter support. Unsupported adapters may return errors or have no effect. The JS helper returns the runtime result, so you canawait it and inspect the adapter response.
Next steps
Manifest cron
See how to declare fixed scheduled triggers.
Proactive push
See push account selection in cron / fake contexts.