Kharat Bot Docs

KAL modules & IMPORT

Sharing code between scripts with IMPORT: what modules may contain, how resolution works and every M-error.

IMPORT pulls constants and functions out of a named module so several scripts can share one implementation.

Syntax#

text
IMPORT { symbol1, symbol2 } FROM "module_name";
  • Exactly one line, at the top of the file, before the AUTOMATION block.
  • Up to 32 imports per script.
  • Module names are letters, digits, - and _.

What a module contains#

A module is KAL source whose AUTOMATION block holds only CONSTANT and FUNCTION declarations β€” no triggers, no actions at module level, no nested imports:

kal (fragment)
// inside module "pricing_lib"
AUTOMATION pricing_lib {
    CONSTANT TAX_RATE = 1.08;

    FUNCTION with_tax(amount AS NUMBER) AS NUMBER {
        RETURN amount * TAX_RATE;
    }
}

Using an import#

kal (fragment)
KAL VERSION 2.0;
IMPORT { with_tax, TAX_RATE } FROM "pricing_lib";
AUTOMATION checkout {
    WHEN COMMAND total (price AS NUMBER) {
        REPLY "With tax: " + ("" + with_tax(price));
    }
}

(This example is shown as a fragment because modules must exist in your server's module registry for the import to resolve β€” an unregistered module is a compile error, not a runtime surprise.)

How resolution works#

  • Modules are registered per server. An import resolves against your server's module library only β€” there is no global library, no file system and no URL fetching.
  • Missing module β†’ M001_MODULE_NOT_FOUND. Missing symbol β†’ M003_EXPORT_NOT_FOUND.
  • There is currently no dashboard page for uploading modules; the library is managed by the platform. Marketplace packages that need shared code declare it in their package rather than reaching into other servers' registries.

Limits#

RuleValue
Imports per script32
Modules per server256
Module source size200,000 bytes
Transitive importsNot allowed (a module cannot import)

The M-error family#

CodeMeaning
M001_MODULE_NOT_FOUNDNo such module in this server's registry
M002_CIRCULAR_MODULE_DEPENDENCYImport cycle (defensive; imports cannot nest)
M003_EXPORT_NOT_FOUNDThe module does not export that symbol
M004_DUPLICATE_IMPORTSame symbol imported twice
M005_INVALID_EXPORTExport is not a CONSTANT or FUNCTION
M006_INVALID_MODULE_IDENTIFIERBad module name
M007_GUILD_REQUIREDCompiled without a server context (tooling)
M008_MODULE_SYNTAX_ERRORThe module source itself does not compile
M009_MODULE_IMPORTS_UNSUPPORTEDA module tried to import
M010_MALFORMED_IMPORTThe IMPORT line is malformed
M011_IMPORT_LIMIT_EXCEEDEDMore than 32 imports