@moltium/core 0.1.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,564 @@
1
+ import * as express_serve_static_core from 'express-serve-static-core';
2
+ import { Router } from 'express';
3
+ import winston from 'winston';
4
+
5
+ interface MoltbookConfig {
6
+ enabled: boolean;
7
+ apiKey: string;
8
+ baseUrl?: string;
9
+ postFrequency?: 'realtime' | 'hourly' | 'daily';
10
+ engagementRate?: 'low' | 'medium' | 'high';
11
+ autoReply?: boolean;
12
+ replyFilter?: (post: any) => boolean;
13
+ }
14
+ interface TwitterConfig {
15
+ enabled: boolean;
16
+ credentials: {
17
+ apiKey: string;
18
+ apiSecret: string;
19
+ accessToken: string;
20
+ accessSecret: string;
21
+ bearerToken?: string;
22
+ };
23
+ postFrequency?: 'realtime' | 'hourly' | 'daily';
24
+ replyToMentions?: boolean;
25
+ autoRetweet?: boolean;
26
+ maxTweetsPerDay?: number;
27
+ contentStrategy?: {
28
+ threadTopics?: string[];
29
+ hashtagStrategy?: string;
30
+ mentionBehavior?: string;
31
+ };
32
+ }
33
+ interface SocialPlatformConfig {
34
+ enabled: boolean;
35
+ [key: string]: unknown;
36
+ }
37
+ type SocialConfig = {
38
+ moltbook?: MoltbookConfig;
39
+ twitter?: TwitterConfig;
40
+ } & Record<string, SocialPlatformConfig | MoltbookConfig | TwitterConfig | undefined>;
41
+ interface BehaviorTrigger {
42
+ condition: (context: any) => boolean;
43
+ action: string;
44
+ params?: Record<string, unknown>;
45
+ }
46
+ interface RedisConfig {
47
+ url: string;
48
+ keyPrefix?: string;
49
+ ttl?: number;
50
+ }
51
+ interface PostgresConfig {
52
+ url: string;
53
+ schema?: string;
54
+ tableName?: string;
55
+ }
56
+ interface Plugin {
57
+ name: string;
58
+ version: string;
59
+ init?: (agent: any) => Promise<void>;
60
+ destroy?: () => Promise<void>;
61
+ }
62
+ interface AgentConfig {
63
+ name: string;
64
+ type?: string;
65
+ personality: {
66
+ traits: string[];
67
+ bio: string;
68
+ [key: string]: unknown;
69
+ };
70
+ llm: {
71
+ provider: 'anthropic' | 'openai';
72
+ model: string;
73
+ apiKey: string;
74
+ temperature?: number;
75
+ maxTokens?: number;
76
+ systemPrompt?: string;
77
+ };
78
+ social: SocialConfig;
79
+ behaviors: {
80
+ autonomous: boolean;
81
+ decisionMaking: 'llm-driven' | 'rule-based' | 'hybrid';
82
+ actionsPerHour?: number | ((context: any) => number);
83
+ sleepSchedule?: {
84
+ start: number;
85
+ end: number;
86
+ timezone?: string;
87
+ };
88
+ triggers?: BehaviorTrigger[];
89
+ };
90
+ memory: {
91
+ type: 'memory' | 'redis' | 'postgres' | 'hybrid';
92
+ retention?: string;
93
+ redis?: RedisConfig;
94
+ postgres?: PostgresConfig;
95
+ };
96
+ actions: string[];
97
+ customActions?: Action[];
98
+ webhooks?: {
99
+ onAction?: string | ((action: any, result: any) => Promise<void>);
100
+ onError?: string;
101
+ };
102
+ plugins?: Plugin[];
103
+ }
104
+ interface ActionContext {
105
+ agent: any;
106
+ memory: any;
107
+ llm: any;
108
+ parameters: Record<string, unknown>;
109
+ }
110
+ interface ActionResult {
111
+ success: boolean;
112
+ data?: unknown;
113
+ error?: string;
114
+ }
115
+ interface Action {
116
+ name: string;
117
+ description: string;
118
+ parameters?: Record<string, unknown>;
119
+ execute: (context: ActionContext) => Promise<ActionResult>;
120
+ }
121
+ interface Decision {
122
+ action: string;
123
+ reasoning: string;
124
+ parameters: Record<string, unknown>;
125
+ }
126
+ interface Experience {
127
+ type: string;
128
+ action: string;
129
+ result: ActionResult;
130
+ timestamp: Date;
131
+ context?: Record<string, unknown>;
132
+ }
133
+ interface DeploymentConfig {
134
+ platform: 'railway' | 'render' | 'aws' | 'digitalocean' | 'custom';
135
+ region?: string;
136
+ instanceType?: string;
137
+ autoScale?: boolean;
138
+ customConfig?: CustomDeploymentConfig;
139
+ }
140
+ interface DeploymentResult {
141
+ success: boolean;
142
+ url?: string;
143
+ deploymentId?: string;
144
+ logs?: string[];
145
+ error?: string;
146
+ }
147
+ interface DeploymentStatus {
148
+ state: 'running' | 'stopped' | 'deploying' | 'failed' | 'unknown';
149
+ url?: string;
150
+ uptime?: number;
151
+ lastDeployed?: Date;
152
+ }
153
+ interface CustomDeploymentConfig {
154
+ deploymentMethod: 'docker' | 'ssh' | 'cli' | 'api';
155
+ buildCommand: string;
156
+ startCommand: string;
157
+ environmentVariables?: Record<string, string>;
158
+ healthCheckUrl?: string;
159
+ port?: number;
160
+ docker?: CustomDockerConfig;
161
+ ssh?: CustomSSHConfig;
162
+ cli?: CustomCLIConfig;
163
+ api?: CustomAPIConfig;
164
+ }
165
+ interface CustomDockerConfig {
166
+ registry: string;
167
+ imageName: string;
168
+ tag?: string;
169
+ dockerfile?: string;
170
+ buildArgs?: Record<string, string>;
171
+ registryAuth?: {
172
+ username: string;
173
+ password: string;
174
+ };
175
+ hostUrl: string;
176
+ containerPort: number;
177
+ hostPort: number;
178
+ }
179
+ interface CustomSSHConfig {
180
+ host: string;
181
+ port: number;
182
+ username: string;
183
+ authMethod: 'password' | 'privateKey';
184
+ password?: string;
185
+ privateKeyPath?: string;
186
+ deploymentPath: string;
187
+ preDeployCommands?: string[];
188
+ postDeployCommands?: string[];
189
+ }
190
+ interface CustomCLIConfig {
191
+ cliName: string;
192
+ installCommand?: string;
193
+ loginCommand?: string;
194
+ deployCommand: string;
195
+ statusCommand?: string;
196
+ logsCommand?: string;
197
+ credentials?: Record<string, string>;
198
+ }
199
+ interface CustomAPIConfig {
200
+ baseUrl: string;
201
+ authType: 'bearer' | 'apiKey' | 'basic' | 'custom';
202
+ authToken?: string;
203
+ apiKey?: string;
204
+ headers?: Record<string, string>;
205
+ endpoints: {
206
+ deploy: string;
207
+ status?: string;
208
+ logs?: string;
209
+ rollback?: string;
210
+ };
211
+ requestMethod?: 'POST' | 'PUT';
212
+ payloadTemplate?: Record<string, unknown>;
213
+ }
214
+ interface PostResult {
215
+ id: string;
216
+ url?: string;
217
+ timestamp: Date;
218
+ }
219
+ interface ReplyResult {
220
+ id: string;
221
+ parentId: string;
222
+ url?: string;
223
+ timestamp: Date;
224
+ }
225
+ interface Mention {
226
+ id: string;
227
+ authorId: string;
228
+ authorName: string;
229
+ content: string;
230
+ timestamp: Date;
231
+ platform: string;
232
+ }
233
+ interface Profile {
234
+ id: string;
235
+ name: string;
236
+ bio?: string;
237
+ followers?: number;
238
+ following?: number;
239
+ platform: string;
240
+ }
241
+
242
+ interface LifecycleHooks {
243
+ onInit?: (agent: Agent) => Promise<void>;
244
+ onStart?: (agent: Agent) => Promise<void>;
245
+ onTick?: (agent: Agent) => Promise<void>;
246
+ onShutdown?: (agent: Agent) => Promise<void>;
247
+ onError?: (error: Error, agent: Agent) => Promise<void>;
248
+ }
249
+ type LifecycleEvent = keyof LifecycleHooks;
250
+
251
+ declare class ActionRegistry {
252
+ private actions;
253
+ register(action: Action): void;
254
+ unregister(name: string): void;
255
+ get(name: string): Action | undefined;
256
+ has(name: string): boolean;
257
+ list(): Action[];
258
+ listNames(): string[];
259
+ }
260
+
261
+ declare abstract class Memory {
262
+ abstract remember(key: string, value: unknown, ttl?: number): Promise<void>;
263
+ abstract recall(key: string): Promise<unknown>;
264
+ abstract forget(key: string): Promise<void>;
265
+ abstract store(key: string, value: unknown): Promise<void>;
266
+ abstract retrieve(key: string): Promise<unknown>;
267
+ abstract delete(key: string): Promise<void>;
268
+ abstract addExperience(experience: Experience): Promise<void>;
269
+ abstract queryExperiences(query: string, limit?: number): Promise<Experience[]>;
270
+ abstract clear(): Promise<void>;
271
+ }
272
+
273
+ interface GenerateOptions {
274
+ temperature?: number;
275
+ maxTokens?: number;
276
+ stopSequences?: string[];
277
+ systemPrompt?: string;
278
+ }
279
+ interface Message {
280
+ role: 'system' | 'user' | 'assistant';
281
+ content: string;
282
+ }
283
+ interface StructuredSchema {
284
+ type: string;
285
+ properties?: Record<string, unknown>;
286
+ required?: string[];
287
+ }
288
+ declare abstract class LLMProvider {
289
+ abstract name: string;
290
+ abstract generateText(prompt: string, options?: GenerateOptions): Promise<string>;
291
+ abstract generateStructured<T>(prompt: string, schema: StructuredSchema, options?: GenerateOptions): Promise<T>;
292
+ abstract chat(messages: Message[], options?: GenerateOptions): Promise<Message>;
293
+ abstract streamGenerate(prompt: string, options?: GenerateOptions): AsyncGenerator<string>;
294
+ }
295
+
296
+ interface FeedOptions {
297
+ limit?: number;
298
+ since?: Date;
299
+ cursor?: string;
300
+ }
301
+ interface TimelineOptions extends FeedOptions {
302
+ includeReplies?: boolean;
303
+ includeRetweets?: boolean;
304
+ }
305
+ interface Post {
306
+ id: string;
307
+ authorId: string;
308
+ authorName: string;
309
+ content: string;
310
+ timestamp: Date;
311
+ platform: string;
312
+ likes?: number;
313
+ replies?: number;
314
+ reposts?: number;
315
+ mentions?: string[];
316
+ url?: string;
317
+ }
318
+
319
+ declare abstract class SocialAdapter {
320
+ abstract platform: string;
321
+ abstract post(content: string): Promise<PostResult>;
322
+ abstract reply(id: string, content: string): Promise<ReplyResult>;
323
+ abstract like(id: string): Promise<void>;
324
+ abstract follow(userId: string): Promise<void>;
325
+ abstract getMentions(): Promise<Mention[]>;
326
+ abstract getFeed(options?: FeedOptions): Promise<Post[]>;
327
+ abstract getProfile(userId: string): Promise<Profile>;
328
+ abstract connect(): Promise<void>;
329
+ abstract disconnect(): Promise<void>;
330
+ }
331
+
332
+ type AgentState = 'idle' | 'initializing' | 'running' | 'stopping' | 'stopped' | 'error';
333
+ declare class Agent {
334
+ readonly config: AgentConfig;
335
+ private state;
336
+ private llm;
337
+ private memory_;
338
+ private scheduler;
339
+ private actionRegistry;
340
+ private actionHandler;
341
+ private hooks;
342
+ private tickTimer?;
343
+ private systemPrompt;
344
+ socialAdapters: Record<string, SocialAdapter>;
345
+ constructor(config: AgentConfig, hooks?: LifecycleHooks);
346
+ get name(): string;
347
+ getState(): AgentState;
348
+ getMemory(): Memory;
349
+ getLLM(): LLMProvider;
350
+ getActionRegistry(): ActionRegistry;
351
+ setSystemPrompt(prompt: string): void;
352
+ appendSystemPrompt(additional: string): void;
353
+ getSystemPrompt(): string;
354
+ setHooks(hooks: LifecycleHooks): void;
355
+ mergeHooks(hooks: Partial<LifecycleHooks>): void;
356
+ setMemory(memory: Memory): void;
357
+ preloadMemory(entries: Record<string, unknown>): Promise<void>;
358
+ registerAction(action: Action): void;
359
+ init(): Promise<void>;
360
+ start(): Promise<void>;
361
+ stop(): Promise<void>;
362
+ think(): Promise<Decision>;
363
+ act(actionName: string, parameters?: Record<string, unknown>): Promise<ActionResult>;
364
+ learn(experience: Experience): Promise<void>;
365
+ tick(): Promise<void>;
366
+ registerMarkdownSkills(skills: Array<{
367
+ name: string;
368
+ description: string;
369
+ }>): void;
370
+ private initLLM;
371
+ private registerActions;
372
+ private initSocialAdapters;
373
+ private startAutonomousLoop;
374
+ private isSleeping;
375
+ private gatherContext;
376
+ private fireWebhook;
377
+ }
378
+
379
+ interface ScheduledJob {
380
+ name: string;
381
+ intervalMs: number;
382
+ handler: () => Promise<void>;
383
+ lastRun?: number;
384
+ }
385
+ declare class Scheduler {
386
+ private jobs;
387
+ private timers;
388
+ private running;
389
+ addJob(job: ScheduledJob): void;
390
+ removeJob(name: string): void;
391
+ start(): void;
392
+ stop(): void;
393
+ isRunning(): boolean;
394
+ getJobs(): ScheduledJob[];
395
+ }
396
+
397
+ type ConfigType = 'code' | 'markdown';
398
+ declare class ConfigLoader {
399
+ private markdownParser;
400
+ load(directory: string): Promise<{
401
+ config: AgentConfig;
402
+ type: ConfigType;
403
+ }>;
404
+ loadCode(filePath: string): Promise<AgentConfig>;
405
+ loadMarkdown(filePath: string): Promise<AgentConfig>;
406
+ }
407
+
408
+ declare class MarkdownParser {
409
+ parse(markdown: string): Partial<AgentConfig>;
410
+ parseSkills(markdown: string): Array<{
411
+ name: string;
412
+ description: string;
413
+ }>;
414
+ private parseSections;
415
+ private findSection;
416
+ private parseIdentity;
417
+ private parseSocial;
418
+ private parseBehaviors;
419
+ private parseMemory;
420
+ private parseSleepSchedule;
421
+ private parseKeyValueLines;
422
+ private toSnakeCase;
423
+ }
424
+
425
+ declare function validateConfig(config: unknown): AgentConfig;
426
+
427
+ declare class AnthropicProvider extends LLMProvider {
428
+ name: string;
429
+ private client;
430
+ private model;
431
+ constructor(apiKey: string, model?: string);
432
+ generateText(prompt: string, options?: GenerateOptions): Promise<string>;
433
+ generateStructured<T>(prompt: string, schema: StructuredSchema, options?: GenerateOptions): Promise<T>;
434
+ chat(messages: Message[], options?: GenerateOptions): Promise<Message>;
435
+ streamGenerate(prompt: string, options?: GenerateOptions): AsyncGenerator<string>;
436
+ }
437
+
438
+ declare class OpenAIProvider extends LLMProvider {
439
+ name: string;
440
+ private client;
441
+ private model;
442
+ constructor(apiKey: string, model?: string);
443
+ generateText(prompt: string, options?: GenerateOptions): Promise<string>;
444
+ generateStructured<T>(prompt: string, schema: StructuredSchema, options?: GenerateOptions): Promise<T>;
445
+ chat(messages: Message[], options?: GenerateOptions): Promise<Message>;
446
+ streamGenerate(prompt: string, options?: GenerateOptions): AsyncGenerator<string>;
447
+ }
448
+
449
+ declare function buildSystemPrompt(config: AgentConfig): string;
450
+ declare function buildDecisionPrompt(context: Record<string, unknown>, availableActions: Array<{
451
+ name: string;
452
+ description: string;
453
+ }>): string;
454
+ declare function buildSkillPrompt(skillName: string, skillDescription: string, parameters: Record<string, unknown>): string;
455
+
456
+ declare class ShortTermMemory extends Memory {
457
+ private cache;
458
+ private store_;
459
+ private experiences;
460
+ remember(key: string, value: unknown, ttl?: number): Promise<void>;
461
+ recall(key: string): Promise<unknown>;
462
+ forget(key: string): Promise<void>;
463
+ store(key: string, value: unknown): Promise<void>;
464
+ retrieve(key: string): Promise<unknown>;
465
+ delete(key: string): Promise<void>;
466
+ addExperience(experience: Experience): Promise<void>;
467
+ queryExperiences(query: string, limit?: number): Promise<Experience[]>;
468
+ clear(): Promise<void>;
469
+ }
470
+
471
+ declare class LongTermMemory extends Memory {
472
+ private redisConfig?;
473
+ private postgresConfig?;
474
+ private redis;
475
+ private pg;
476
+ private initialized;
477
+ constructor(options?: {
478
+ redis?: RedisConfig;
479
+ postgres?: PostgresConfig;
480
+ });
481
+ init(): Promise<void>;
482
+ remember(key: string, value: unknown, ttl?: number): Promise<void>;
483
+ recall(key: string): Promise<unknown>;
484
+ forget(key: string): Promise<void>;
485
+ store(key: string, value: unknown): Promise<void>;
486
+ retrieve(key: string): Promise<unknown>;
487
+ delete(key: string): Promise<void>;
488
+ addExperience(experience: Experience): Promise<void>;
489
+ queryExperiences(query: string, limit?: number): Promise<Experience[]>;
490
+ clear(): Promise<void>;
491
+ disconnect(): Promise<void>;
492
+ ensureSchema(): Promise<void>;
493
+ private initRedis;
494
+ private initPostgres;
495
+ private ensureRedis;
496
+ private ensurePostgres;
497
+ private redisKey;
498
+ private pgTable;
499
+ private pgExperienceTable;
500
+ private sanitizeName;
501
+ }
502
+
503
+ declare class MoltbookAdapter extends SocialAdapter {
504
+ platform: string;
505
+ private client;
506
+ private config;
507
+ constructor(config: MoltbookConfig);
508
+ connect(): Promise<void>;
509
+ disconnect(): Promise<void>;
510
+ post(content: string): Promise<PostResult>;
511
+ reply(postId: string, content: string): Promise<ReplyResult>;
512
+ like(postId: string): Promise<void>;
513
+ follow(userId: string): Promise<void>;
514
+ getMentions(): Promise<Mention[]>;
515
+ getFeed(options?: FeedOptions): Promise<Post[]>;
516
+ getProfile(userId: string): Promise<Profile>;
517
+ }
518
+
519
+ declare class TwitterAdapter extends SocialAdapter {
520
+ platform: string;
521
+ private config;
522
+ private client;
523
+ constructor(config: TwitterConfig);
524
+ connect(): Promise<void>;
525
+ disconnect(): Promise<void>;
526
+ post(content: string): Promise<PostResult>;
527
+ reply(tweetId: string, content: string): Promise<ReplyResult>;
528
+ like(tweetId: string): Promise<void>;
529
+ follow(userId: string): Promise<void>;
530
+ getMentions(): Promise<Mention[]>;
531
+ getFeed(options?: FeedOptions): Promise<Post[]>;
532
+ getProfile(userId: string): Promise<Profile>;
533
+ }
534
+
535
+ declare class ActionHandler {
536
+ private registry;
537
+ constructor(registry: ActionRegistry);
538
+ execute(actionName: string, context: ActionContext): Promise<ActionResult>;
539
+ getAvailableActions(): Array<{
540
+ name: string;
541
+ description: string;
542
+ }>;
543
+ }
544
+
545
+ /**
546
+ * Creates an Action from a markdown skill description.
547
+ * The skill's behavior is interpreted by the LLM at runtime.
548
+ */
549
+ declare function createMarkdownAction(name: string, description: string, llmProvider: LLMProvider): Action;
550
+
551
+ declare const builtInActions: Action[];
552
+
553
+ interface ServerOptions {
554
+ port?: number;
555
+ host?: string;
556
+ }
557
+ declare function createApp(agent: Agent): express_serve_static_core.Express;
558
+ declare function startServer(agent: Agent, options?: ServerOptions): Promise<void>;
559
+
560
+ declare function createRoutes(agent: Agent): Router;
561
+
562
+ declare function createLogger(label: string): winston.Logger;
563
+
564
+ export { type Action, type ActionContext, ActionHandler, ActionRegistry, type ActionResult, Agent, type AgentConfig, type AgentState, AnthropicProvider, type BehaviorTrigger, ConfigLoader, type ConfigType, type CustomAPIConfig, type CustomCLIConfig, type CustomDeploymentConfig, type CustomDockerConfig, type CustomSSHConfig, type Decision, type DeploymentConfig, type DeploymentResult, type DeploymentStatus, type Experience, type FeedOptions, type GenerateOptions, LLMProvider, type LifecycleEvent, type LifecycleHooks, LongTermMemory, MarkdownParser, Memory, type Mention, type Message, MoltbookAdapter, type MoltbookConfig, OpenAIProvider, type Plugin, type Post, type PostResult, type PostgresConfig, type Profile, type RedisConfig, type ReplyResult, type ScheduledJob, Scheduler, ShortTermMemory, SocialAdapter, type SocialConfig, type SocialPlatformConfig, type StructuredSchema, type TimelineOptions, TwitterAdapter, type TwitterConfig, buildDecisionPrompt, buildSkillPrompt, buildSystemPrompt, builtInActions, createApp, createLogger, createMarkdownAction, createRoutes, startServer, validateConfig };