botinabox 0.2.0 → 0.2.2

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,98 @@
1
+ import {
2
+ parseDiscordEvent
3
+ } from "../../chunk-DLJKZD3Q.js";
4
+
5
+ // src/channels/discord/outbound.ts
6
+ var DISCORD_MAX_LENGTH = 2e3;
7
+ function chunkForDiscord(text) {
8
+ if (text.length <= DISCORD_MAX_LENGTH) return [text];
9
+ const chunks = [];
10
+ let remaining = text;
11
+ while (remaining.length > DISCORD_MAX_LENGTH) {
12
+ const slice = remaining.slice(0, DISCORD_MAX_LENGTH);
13
+ const lastSpace = slice.lastIndexOf(" ");
14
+ if (lastSpace > 0) {
15
+ chunks.push(remaining.slice(0, lastSpace));
16
+ remaining = remaining.slice(lastSpace + 1);
17
+ } else {
18
+ chunks.push(remaining.slice(0, DISCORD_MAX_LENGTH));
19
+ remaining = remaining.slice(DISCORD_MAX_LENGTH);
20
+ }
21
+ }
22
+ if (remaining.length > 0) chunks.push(remaining);
23
+ return chunks;
24
+ }
25
+ function formatForDiscord(text) {
26
+ return text;
27
+ }
28
+
29
+ // src/channels/discord/adapter.ts
30
+ var DiscordAdapter = class {
31
+ id = "discord";
32
+ meta = {
33
+ displayName: "Discord",
34
+ icon: "https://discord.com/favicon.ico",
35
+ homepage: "https://discord.com"
36
+ };
37
+ capabilities = {
38
+ chatTypes: ["direct", "group", "channel"],
39
+ threads: true,
40
+ reactions: true,
41
+ editing: true,
42
+ media: true,
43
+ polls: false,
44
+ maxTextLength: 2e3,
45
+ formattingMode: "markdown"
46
+ };
47
+ onMessage;
48
+ connected = false;
49
+ config = null;
50
+ client;
51
+ constructor(client) {
52
+ this.client = client ?? null;
53
+ }
54
+ async connect(config) {
55
+ this.config = config;
56
+ this.connected = true;
57
+ }
58
+ async disconnect() {
59
+ this.connected = false;
60
+ this.config = null;
61
+ }
62
+ async healthCheck() {
63
+ return { ok: this.connected };
64
+ }
65
+ async send(target, payload) {
66
+ if (!this.connected) {
67
+ return { success: false, error: "Not connected" };
68
+ }
69
+ const text = formatForDiscord(payload.text);
70
+ if (this.client) {
71
+ try {
72
+ const result = await this.client.sendMessage(target.peerId, text);
73
+ return { success: true, messageId: result.id };
74
+ } catch (err) {
75
+ return { success: false, error: String(err) };
76
+ }
77
+ }
78
+ return { success: true };
79
+ }
80
+ /** Simulate receiving an inbound message (for testing/webhooks). */
81
+ async receive(event) {
82
+ if (this.onMessage) {
83
+ const { parseDiscordEvent: parseDiscordEvent2 } = await import("../../inbound-SNEMBLGA.js");
84
+ const msg = parseDiscordEvent2(event);
85
+ await this.onMessage(msg);
86
+ }
87
+ }
88
+ };
89
+ function createDiscordAdapter(client) {
90
+ return new DiscordAdapter(client);
91
+ }
92
+ export {
93
+ DiscordAdapter,
94
+ chunkForDiscord,
95
+ createDiscordAdapter as default,
96
+ formatForDiscord,
97
+ parseDiscordEvent
98
+ };
@@ -0,0 +1,80 @@
1
+ import {
2
+ parseSlackEvent
3
+ } from "../../chunk-QLA6YOFN.js";
4
+
5
+ // src/channels/slack/outbound.ts
6
+ function formatForSlack(text) {
7
+ let result = text.replace(/\*\*(.+?)\*\*/gs, "*$1*");
8
+ result = result.replace(/__(.+?)__/gs, "*$1*");
9
+ return result;
10
+ }
11
+
12
+ // src/channels/slack/adapter.ts
13
+ var SlackAdapter = class {
14
+ id = "slack";
15
+ meta = {
16
+ displayName: "Slack",
17
+ icon: "https://slack.com/favicon.ico",
18
+ homepage: "https://slack.com"
19
+ };
20
+ capabilities = {
21
+ chatTypes: ["direct", "group", "channel"],
22
+ threads: true,
23
+ reactions: true,
24
+ editing: true,
25
+ media: true,
26
+ polls: false,
27
+ maxTextLength: 4e4,
28
+ formattingMode: "mrkdwn"
29
+ };
30
+ onMessage;
31
+ connected = false;
32
+ config = null;
33
+ client;
34
+ constructor(client) {
35
+ this.client = client ?? null;
36
+ }
37
+ async connect(config) {
38
+ this.config = config;
39
+ this.connected = true;
40
+ }
41
+ async disconnect() {
42
+ this.connected = false;
43
+ this.config = null;
44
+ }
45
+ async healthCheck() {
46
+ return { ok: this.connected };
47
+ }
48
+ async send(target, payload) {
49
+ if (!this.connected) {
50
+ return { success: false, error: "Not connected" };
51
+ }
52
+ const text = formatForSlack(payload.text);
53
+ if (this.client) {
54
+ try {
55
+ const result = await this.client.postMessage(target.peerId, text, target.threadId);
56
+ return { success: result.ok, messageId: result.ts };
57
+ } catch (err) {
58
+ return { success: false, error: String(err) };
59
+ }
60
+ }
61
+ return { success: true };
62
+ }
63
+ /** Simulate receiving an inbound message (for testing/webhooks). */
64
+ async receive(event) {
65
+ if (this.onMessage) {
66
+ const { parseSlackEvent: parseSlackEvent2 } = await import("../../inbound-AFOHYNUY.js");
67
+ const msg = parseSlackEvent2(event);
68
+ await this.onMessage(msg);
69
+ }
70
+ }
71
+ };
72
+ function createSlackAdapter(client) {
73
+ return new SlackAdapter(client);
74
+ }
75
+ export {
76
+ SlackAdapter,
77
+ createSlackAdapter as default,
78
+ formatForSlack,
79
+ parseSlackEvent
80
+ };
@@ -0,0 +1,178 @@
1
+ // src/channels/webhook/server.ts
2
+ import { createServer } from "http";
3
+
4
+ // src/channels/webhook/hmac.ts
5
+ import { createHmac, timingSafeEqual } from "crypto";
6
+ function verifyHmac(body, secret, signature) {
7
+ const expected = createHmac("sha256", secret).update(body, "utf8").digest("hex");
8
+ const provided = signature.startsWith("sha256=") ? signature.slice(7) : signature;
9
+ if (expected.length !== provided.length) return false;
10
+ try {
11
+ return timingSafeEqual(Buffer.from(expected, "hex"), Buffer.from(provided, "hex"));
12
+ } catch {
13
+ return false;
14
+ }
15
+ }
16
+
17
+ // src/channels/webhook/server.ts
18
+ var WebhookServer = class {
19
+ server = null;
20
+ port;
21
+ secret;
22
+ onMessage;
23
+ constructor(opts) {
24
+ this.port = opts.port ?? 3200;
25
+ this.secret = opts.secret;
26
+ this.onMessage = opts.onMessage;
27
+ }
28
+ start() {
29
+ return new Promise((resolve) => {
30
+ this.server = createServer((req, res) => {
31
+ void this.handleRequest(req, res);
32
+ });
33
+ this.server.listen(this.port, () => resolve());
34
+ });
35
+ }
36
+ stop() {
37
+ return new Promise((resolve, reject) => {
38
+ if (!this.server) {
39
+ resolve();
40
+ return;
41
+ }
42
+ this.server.close((err) => {
43
+ if (err) reject(err);
44
+ else resolve();
45
+ });
46
+ });
47
+ }
48
+ async handleRequest(req, res) {
49
+ const url = req.url ?? "/";
50
+ const method = req.method ?? "GET";
51
+ if (method !== "POST" || url !== "/webhook/inbound") {
52
+ res.writeHead(404, { "Content-Type": "application/json" });
53
+ res.end(JSON.stringify({ error: "Not found" }));
54
+ return;
55
+ }
56
+ let body = "";
57
+ for await (const chunk of req) {
58
+ body += chunk;
59
+ }
60
+ if (this.secret) {
61
+ const sig = req.headers["x-webhook-signature"];
62
+ if (!sig || !verifyHmac(body, this.secret, sig)) {
63
+ res.writeHead(401, { "Content-Type": "application/json" });
64
+ res.end(JSON.stringify({ error: "Invalid signature" }));
65
+ return;
66
+ }
67
+ }
68
+ let parsed;
69
+ try {
70
+ parsed = JSON.parse(body);
71
+ } catch {
72
+ res.writeHead(400, { "Content-Type": "application/json" });
73
+ res.end(JSON.stringify({ error: "Invalid JSON" }));
74
+ return;
75
+ }
76
+ const msg = {
77
+ id: parsed["id"] ?? `webhook-${Date.now()}`,
78
+ channel: "webhook",
79
+ from: parsed["from"] ?? "unknown",
80
+ body: parsed["text"] ?? "",
81
+ threadId: parsed["threadId"],
82
+ receivedAt: (/* @__PURE__ */ new Date()).toISOString(),
83
+ raw: parsed
84
+ };
85
+ try {
86
+ await this.onMessage(msg);
87
+ res.writeHead(200, { "Content-Type": "application/json" });
88
+ res.end(JSON.stringify({ ok: true }));
89
+ } catch (err) {
90
+ res.writeHead(500, { "Content-Type": "application/json" });
91
+ res.end(JSON.stringify({ error: String(err) }));
92
+ }
93
+ }
94
+ };
95
+
96
+ // src/channels/webhook/adapter.ts
97
+ var WebhookAdapter = class {
98
+ id = "webhook";
99
+ meta = {
100
+ displayName: "Webhook",
101
+ homepage: "https://example.com"
102
+ };
103
+ capabilities = {
104
+ chatTypes: ["direct"],
105
+ threads: false,
106
+ reactions: false,
107
+ editing: false,
108
+ media: false,
109
+ polls: false,
110
+ maxTextLength: 65535,
111
+ formattingMode: "plain"
112
+ };
113
+ onMessage;
114
+ connected = false;
115
+ config = null;
116
+ webhookServer = null;
117
+ async connect(config) {
118
+ this.config = config;
119
+ this.connected = true;
120
+ if (this.config.port) {
121
+ this.webhookServer = new WebhookServer({
122
+ port: this.config.port,
123
+ secret: this.config.secret,
124
+ onMessage: async (msg) => {
125
+ if (this.onMessage) await this.onMessage(msg);
126
+ }
127
+ });
128
+ await this.webhookServer.start();
129
+ }
130
+ }
131
+ async disconnect() {
132
+ if (this.webhookServer) {
133
+ await this.webhookServer.stop();
134
+ this.webhookServer = null;
135
+ }
136
+ this.connected = false;
137
+ this.config = null;
138
+ }
139
+ async healthCheck() {
140
+ return { ok: this.connected };
141
+ }
142
+ async send(target, payload) {
143
+ if (!this.connected) {
144
+ return { success: false, error: "Not connected" };
145
+ }
146
+ const callbackUrl = this.config?.callbackUrl;
147
+ if (!callbackUrl) {
148
+ return { success: true };
149
+ }
150
+ try {
151
+ const body = JSON.stringify({
152
+ to: target.peerId,
153
+ threadId: target.threadId,
154
+ text: payload.text
155
+ });
156
+ const response = await fetch(callbackUrl, {
157
+ method: "POST",
158
+ headers: { "Content-Type": "application/json" },
159
+ body
160
+ });
161
+ if (response.ok) {
162
+ return { success: true };
163
+ }
164
+ return { success: false, error: `HTTP ${response.status}` };
165
+ } catch (err) {
166
+ return { success: false, error: String(err) };
167
+ }
168
+ }
169
+ };
170
+ function createWebhookAdapter() {
171
+ return new WebhookAdapter();
172
+ }
173
+ export {
174
+ WebhookAdapter,
175
+ WebhookServer,
176
+ createWebhookAdapter as default,
177
+ verifyHmac
178
+ };
@@ -0,0 +1,22 @@
1
+ // src/channels/discord/inbound.ts
2
+ function parseDiscordEvent(event) {
3
+ const id = event.id ?? `discord-${Date.now()}`;
4
+ const channel = event.channel_id ?? "unknown";
5
+ const from = event.author?.id ?? "unknown";
6
+ const body = event.content ?? "";
7
+ const replyToId = event.message_reference?.message_id;
8
+ const receivedAt = event.timestamp ?? (/* @__PURE__ */ new Date()).toISOString();
9
+ return {
10
+ id,
11
+ channel,
12
+ from,
13
+ body,
14
+ replyToId,
15
+ receivedAt,
16
+ raw: event
17
+ };
18
+ }
19
+
20
+ export {
21
+ parseDiscordEvent
22
+ };
@@ -0,0 +1,22 @@
1
+ // src/channels/slack/inbound.ts
2
+ function parseSlackEvent(event) {
3
+ const id = event.client_msg_id ?? event.ts ?? event.event_ts ?? `slack-${Date.now()}`;
4
+ const channel = event.channel ?? "unknown";
5
+ const from = event.user ?? "unknown";
6
+ const body = event.text ?? "";
7
+ const threadId = event.thread_ts !== void 0 ? event.thread_ts : void 0;
8
+ const receivedAt = event.ts ? new Date(parseFloat(event.ts) * 1e3).toISOString() : (/* @__PURE__ */ new Date()).toISOString();
9
+ return {
10
+ id,
11
+ channel,
12
+ from,
13
+ body,
14
+ threadId,
15
+ receivedAt,
16
+ raw: event
17
+ };
18
+ }
19
+
20
+ export {
21
+ parseSlackEvent
22
+ };
@@ -0,0 +1,6 @@
1
+ import {
2
+ parseSlackEvent
3
+ } from "./chunk-QLA6YOFN.js";
4
+ export {
5
+ parseSlackEvent
6
+ };
@@ -0,0 +1,6 @@
1
+ import {
2
+ parseDiscordEvent
3
+ } from "./chunk-DLJKZD3Q.js";
4
+ export {
5
+ parseDiscordEvent
6
+ };
package/dist/index.d.ts CHANGED
@@ -970,6 +970,20 @@ declare const CORE_MIGRATIONS: Array<{
970
970
  sql: string;
971
971
  }>;
972
972
 
973
+ /**
974
+ * Define default entity context rendering for botinabox core tables.
975
+ * Call after defineCoreTables() and before or after init().
976
+ *
977
+ * Renders:
978
+ * - agents/ — per-agent context (AGENT.md, PROJECTS.md if agent_project exists)
979
+ * - users/ — per-user context (USER.md) — protected
980
+ * - skills/ — per-skill context (SKILL.md)
981
+ *
982
+ * Apps can override by calling db.defineEntityContext() with the same table name
983
+ * BEFORE calling defineCoreEntityContexts().
984
+ */
985
+ declare function defineCoreEntityContexts(db: DataStore): void;
986
+
973
987
  interface SanitizerOptions {
974
988
  fieldLengthLimits?: Record<string, number>;
975
989
  truncateSuffix?: string;
@@ -1357,4 +1371,4 @@ declare class SecretStore {
1357
1371
  private _toMeta;
1358
1372
  }
1359
1373
 
1360
- export { AGENT_STATUSES, type AgentConfig, type AgentDefinition, type AgentFilter, type AgentRecord, AgentRegistry, type AgentStatus, ApiExecutionAdapter, AuditEmitter, type AuditEvent, BackupManager, type BotConfig, type BudgetCheck, type BudgetConfig, BudgetController, CORE_MIGRATIONS, ChannelAdapter, ChannelRegistry, ChannelRegistryError, ChatMessage, ChatSessionManager, CliExecutionAdapter, type ColumnValidator, ColumnValidatorImpl, type ConfigLoadError, type ConfigLoadResult, DEFAULTS, DEFAULT_CONFIG, type DataConfig, DataStore, DataStoreError, EVENTS, type EntityColumnDef, type EntityConfig, type EntityContextDef, type EntityFileSpec, type EntitySource, type ExecutionAdapter, type Filter, HealthStatus, HeartbeatScheduler, HookBus, type HookHandler, type HookOptions, type HookRegistration, InboundMessage, LLMProvider, MAX_CHAIN_DEPTH, MessagePipeline, type ModelConfig, ModelInfo, ModelRouter, NdjsonLogger, NotificationQueue, type PackageMigration, type PackageUpdate, type PkLookup, ProviderRegistry, type QueryOptions, RUN_STATUSES, type RelationDef, type RenderConfig, ResolvedModel, type RetryPolicy, type Row, type RunContext, RunManager, type RunResult, type RunStatus, type SanitizerOptions, type SchemaError, type SecretInput, type SecretMeta, SecretStore, type SecurityConfig, type SeedItem, SessionKey, SessionManager, type SqliteAdapter, type StepRef, TASK_STATUSES, type TableDefinition, type TableInfoRow, type TaskDefinition, TaskQueue, type TaskRecord, type TaskStatus, TokenUsage, type Unsubscribe, UpdateChecker, type UpdateConfig, UpdateManager, type UpdateManifest, type User, type UserInput, UserRegistry, WakeupQueue, type WorkflowConfigEntry, type WorkflowDefinition$1 as WorkflowDefinition, WorkflowEngine, type WorkflowRunRecord, type WorkflowRunStatus, type WorkflowStep$1 as WorkflowStep, type WorkflowStepConfig, type WorkflowTrigger, _resetConfig, areDependenciesMet, buildAgentBindings, buildChainOrigin, checkAllowlist, checkChainDepth, checkMentionGate, chunkText, classifyUpdate, compareVersions, createConfigRevision, defineCoreTables, detectCycle, discoverChannels, discoverProviders, formatText, getConfig, initConfig, interpolate, interpolateEnv, loadConfig, parseVersion, runPackageMigrations, sanitize, topologicalSort, validateConfig };
1374
+ export { AGENT_STATUSES, type AgentConfig, type AgentDefinition, type AgentFilter, type AgentRecord, AgentRegistry, type AgentStatus, ApiExecutionAdapter, AuditEmitter, type AuditEvent, BackupManager, type BotConfig, type BudgetCheck, type BudgetConfig, BudgetController, CORE_MIGRATIONS, ChannelAdapter, ChannelRegistry, ChannelRegistryError, ChatMessage, ChatSessionManager, CliExecutionAdapter, type ColumnValidator, ColumnValidatorImpl, type ConfigLoadError, type ConfigLoadResult, DEFAULTS, DEFAULT_CONFIG, type DataConfig, DataStore, DataStoreError, EVENTS, type EntityColumnDef, type EntityConfig, type EntityContextDef, type EntityFileSpec, type EntitySource, type ExecutionAdapter, type Filter, HealthStatus, HeartbeatScheduler, HookBus, type HookHandler, type HookOptions, type HookRegistration, InboundMessage, LLMProvider, MAX_CHAIN_DEPTH, MessagePipeline, type ModelConfig, ModelInfo, ModelRouter, NdjsonLogger, NotificationQueue, type PackageMigration, type PackageUpdate, type PkLookup, ProviderRegistry, type QueryOptions, RUN_STATUSES, type RelationDef, type RenderConfig, ResolvedModel, type RetryPolicy, type Row, type RunContext, RunManager, type RunResult, type RunStatus, type SanitizerOptions, type SchemaError, type SecretInput, type SecretMeta, SecretStore, type SecurityConfig, type SeedItem, SessionKey, SessionManager, type SqliteAdapter, type StepRef, TASK_STATUSES, type TableDefinition, type TableInfoRow, type TaskDefinition, TaskQueue, type TaskRecord, type TaskStatus, TokenUsage, type Unsubscribe, UpdateChecker, type UpdateConfig, UpdateManager, type UpdateManifest, type User, type UserInput, UserRegistry, WakeupQueue, type WorkflowConfigEntry, type WorkflowDefinition$1 as WorkflowDefinition, WorkflowEngine, type WorkflowRunRecord, type WorkflowRunStatus, type WorkflowStep$1 as WorkflowStep, type WorkflowStepConfig, type WorkflowTrigger, _resetConfig, areDependenciesMet, buildAgentBindings, buildChainOrigin, checkAllowlist, checkChainDepth, checkMentionGate, chunkText, classifyUpdate, compareVersions, createConfigRevision, defineCoreEntityContexts, defineCoreTables, detectCycle, discoverChannels, discoverProviders, formatText, getConfig, initConfig, interpolate, interpolateEnv, loadConfig, parseVersion, runPackageMigrations, sanitize, topologicalSort, validateConfig };