@kl-c/matrixos 0.1.40 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Per-agent token budget (cost guardrails).
3
+ *
4
+ * Tracks cumulative tokens consumed per agent and triggers an action when
5
+ * the configured budget is exceeded. Inspired by Sprint B9 (cost-alerter)
6
+ * but per-agent instead of global.
7
+ *
8
+ * Defaults: tank=50k, morpheus=200k, general=100k, unlimited for others.
9
+ * Action on overrun: warn (log) by default; can be set to "throttle" (block
10
+ * the call) or "throw" (reject the call) via the action field.
11
+ */
12
+ export type BudgetAction = "warn" | "throttle" | "throw";
13
+ export type BudgetConfig = {
14
+ tokens: number;
15
+ action: BudgetAction;
16
+ };
17
+ export declare function setBudget(agent: string, config: BudgetConfig): void;
18
+ export declare function recordTokens(agent: string, tokens: number): BudgetStatus;
19
+ export declare function getUsed(agent: string): number;
20
+ export declare function resetBudget(agent?: string): void;
21
+ export type BudgetStatus = {
22
+ used: number;
23
+ limit: number;
24
+ percent: number;
25
+ action: BudgetAction | null;
26
+ over: boolean;
27
+ };
28
+ /** Convenience: throw if action=throw and budget is over. Returns the status either way. */
29
+ export declare function enforceBudget(agent: string, tokens: number): BudgetStatus;
@@ -0,0 +1,42 @@
1
+ /**
2
+ * matrixos adopt [telegram]
3
+ *
4
+ * Interactive adoption flow: asks the user for credentials, ingests them
5
+ * via the same .klc-gateway.env file used by `gateway set-token`, and
6
+ * validates the token against the platform API.
7
+ *
8
+ * Why a dedicated command and not just `gateway set-token`:
9
+ * - `set-token` is the low-level "write one token" primitive
10
+ * - `adopt` is the high-level "configure a channel end-to-end" UX
11
+ * (token + chat id + optional validation)
12
+ * - `adopt` is what a user re-runs when they want to fix or migrate
13
+ * their setup
14
+ *
15
+ * The slash command `/adopt` in Telegram dispatches to this same flow
16
+ * (see gateway-handler.ts).
17
+ *
18
+ * Token is NEVER hardcoded. Always prompted (masked) or read from --token.
19
+ */
20
+ export interface AdoptOptions {
21
+ readonly channel?: "telegram";
22
+ readonly envPath?: string;
23
+ readonly token?: string;
24
+ readonly allowedChatId?: string;
25
+ readonly nonInteractive?: boolean;
26
+ /** Mock the validator (for tests). Returns true if the token is valid. */
27
+ readonly validate?: (token: string) => Promise<boolean>;
28
+ }
29
+ export interface AdoptResult {
30
+ readonly ok: boolean;
31
+ readonly channel: string;
32
+ readonly tokenWritten: boolean;
33
+ readonly allowedChatId?: string;
34
+ readonly validated: boolean;
35
+ readonly message: string;
36
+ /** Never the actual token — only a boolean and a length. */
37
+ readonly tokenLength?: number;
38
+ }
39
+ export declare function upsertEnvFile(path: string, updates: Record<string, string>): void;
40
+ /** Default Telegram validator: hits getMe. Returns true on 200. */
41
+ export declare function defaultTelegramValidate(token: string): Promise<boolean>;
42
+ export declare function runAdopt(opts?: AdoptOptions): Promise<AdoptResult>;
@@ -0,0 +1,39 @@
1
+ /**
2
+ * matrixos gateway start [telegram]
3
+ *
4
+ * Boots the MaTrixOS gateway: loads `.klc-gateway.env`, builds a
5
+ * GatewayConfig, instantiates the requested channel adapter, registers it
6
+ * with the MatrixGateway, sets a message handler that shells out to
7
+ * `matrixos run`, and starts long-polling.
8
+ *
9
+ * Token handling: the token is read from `.klc-gateway.env` (chmod 600)
10
+ * via `loadGatewayEnv()`. NEVER hardcoded. Set it with
11
+ * `matrixos gateway set-token telegram --token "<token>"` or
12
+ * `matrixos gateway set-token telegram -i` (masked interactive prompt).
13
+ *
14
+ * The file `.klc-gateway.env` is gitignored (created by `set-token`).
15
+ */
16
+ import { MatrixGateway, TelegramAdapter, type GatewayConfig, type TelegramChannelConfig } from "@matrixos/matrix-gateway-core";
17
+ export interface GatewayStartOptions {
18
+ readonly channel?: "telegram" | "discord";
19
+ readonly envPath?: string;
20
+ readonly command?: string;
21
+ readonly args?: string[];
22
+ readonly timeoutMs?: number;
23
+ /** Mock-friendly: inject a built gateway instead of constructing one. */
24
+ readonly gatewayFactory?: (config: GatewayConfig) => MatrixGateway;
25
+ readonly adapterFactory?: (config: TelegramChannelConfig) => TelegramAdapter;
26
+ }
27
+ export interface GatewayStartResult {
28
+ readonly ok: boolean;
29
+ readonly channel: string;
30
+ readonly pid: number;
31
+ readonly envLoaded: boolean;
32
+ readonly tokenPresent: boolean;
33
+ readonly message: string;
34
+ }
35
+ /** Parse a .klc-gateway.env file (KEY=value, no shell quoting) without dotenv dep. */
36
+ export declare function loadGatewayEnv(path?: string): Record<string, string>;
37
+ export declare function buildTelegramConfig(env: Record<string, string>): TelegramChannelConfig;
38
+ export declare function buildGatewayConfig(channel: TelegramChannelConfig): GatewayConfig;
39
+ export declare function runGatewayStart(opts?: GatewayStartOptions): Promise<GatewayStartResult>;