@agentforge-io/core 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.
Files changed (151) hide show
  1. package/dist/adapters/billing/billing-adapter.interface.d.ts +41 -0
  2. package/dist/adapters/billing/billing-adapter.interface.js +5 -0
  3. package/dist/adapters/billing/stripe/stripe.adapter.d.ts +30 -0
  4. package/dist/adapters/billing/stripe/stripe.adapter.js +122 -0
  5. package/dist/adapters/email/email-adapter.interface.d.ts +25 -0
  6. package/dist/adapters/email/email-adapter.interface.js +6 -0
  7. package/dist/adapters/email/noop.adapter.d.ts +10 -0
  8. package/dist/adapters/email/noop.adapter.js +15 -0
  9. package/dist/adapters/email/resend.adapter.d.ts +8 -0
  10. package/dist/adapters/email/resend.adapter.js +39 -0
  11. package/dist/adapters/job-queue/in-memory.d.ts +43 -0
  12. package/dist/adapters/job-queue/in-memory.js +154 -0
  13. package/dist/adapters/job-queue/job-queue.types.d.ts +76 -0
  14. package/dist/adapters/job-queue/job-queue.types.js +5 -0
  15. package/dist/adapters/prepared-stream/prepared-stream.types.d.ts +23 -0
  16. package/dist/adapters/prepared-stream/prepared-stream.types.js +5 -0
  17. package/dist/adapters/rate-limiter/in-memory.d.ts +19 -0
  18. package/dist/adapters/rate-limiter/in-memory.js +63 -0
  19. package/dist/adapters/rate-limiter/rate-limiter.types.d.ts +42 -0
  20. package/dist/adapters/rate-limiter/rate-limiter.types.js +5 -0
  21. package/dist/adapters/rate-limiter/redis.d.ts +31 -0
  22. package/dist/adapters/rate-limiter/redis.js +47 -0
  23. package/dist/adapters/upload/noop.adapter.d.ts +9 -0
  24. package/dist/adapters/upload/noop.adapter.js +14 -0
  25. package/dist/adapters/upload/s3.adapter.d.ts +38 -0
  26. package/dist/adapters/upload/s3.adapter.js +69 -0
  27. package/dist/adapters/upload/upload-adapter.interface.d.ts +37 -0
  28. package/dist/adapters/upload/upload-adapter.interface.js +15 -0
  29. package/dist/ai/index.d.ts +15 -0
  30. package/dist/ai/index.js +43 -0
  31. package/dist/billing/index.d.ts +12 -0
  32. package/dist/billing/index.js +28 -0
  33. package/dist/constants.d.ts +3 -0
  34. package/dist/constants.js +8 -0
  35. package/dist/domain/agent.d.ts +59 -0
  36. package/dist/domain/agent.js +2 -0
  37. package/dist/domain/api-key.d.ts +28 -0
  38. package/dist/domain/api-key.js +2 -0
  39. package/dist/domain/auth-identity.d.ts +10 -0
  40. package/dist/domain/auth-identity.js +2 -0
  41. package/dist/domain/chat-token.d.ts +39 -0
  42. package/dist/domain/chat-token.js +2 -0
  43. package/dist/domain/connector-auth.d.ts +42 -0
  44. package/dist/domain/connector-auth.js +2 -0
  45. package/dist/domain/connector.d.ts +52 -0
  46. package/dist/domain/connector.js +2 -0
  47. package/dist/domain/conversation.d.ts +26 -0
  48. package/dist/domain/conversation.js +2 -0
  49. package/dist/domain/email-token.d.ts +11 -0
  50. package/dist/domain/email-token.js +2 -0
  51. package/dist/domain/external-user.d.ts +23 -0
  52. package/dist/domain/external-user.js +2 -0
  53. package/dist/domain/index.d.ts +5 -0
  54. package/dist/domain/index.js +24 -0
  55. package/dist/domain/mcp-server.d.ts +33 -0
  56. package/dist/domain/mcp-server.js +2 -0
  57. package/dist/domain/plan.d.ts +20 -0
  58. package/dist/domain/plan.js +2 -0
  59. package/dist/domain/platform-secret.d.ts +24 -0
  60. package/dist/domain/platform-secret.js +8 -0
  61. package/dist/domain/refresh-token.d.ts +15 -0
  62. package/dist/domain/refresh-token.js +2 -0
  63. package/dist/domain/subscription.d.ts +21 -0
  64. package/dist/domain/subscription.js +2 -0
  65. package/dist/domain/tenant.d.ts +21 -0
  66. package/dist/domain/tenant.js +2 -0
  67. package/dist/domain/usage-record.d.ts +15 -0
  68. package/dist/domain/usage-record.js +2 -0
  69. package/dist/domain/user.d.ts +43 -0
  70. package/dist/domain/user.js +2 -0
  71. package/dist/factory.d.ts +68 -0
  72. package/dist/factory.js +56 -0
  73. package/dist/index.d.ts +14 -0
  74. package/dist/index.js +59 -0
  75. package/dist/repositories/in-memory.d.ts +30 -0
  76. package/dist/repositories/in-memory.js +82 -0
  77. package/dist/repositories/index.d.ts +67 -0
  78. package/dist/repositories/index.js +16 -0
  79. package/dist/services/agent-config.service.d.ts +45 -0
  80. package/dist/services/agent-config.service.js +114 -0
  81. package/dist/services/agent-job.worker.d.ts +32 -0
  82. package/dist/services/agent-job.worker.js +97 -0
  83. package/dist/services/agent-runner.service.d.ts +35 -0
  84. package/dist/services/agent-runner.service.js +224 -0
  85. package/dist/services/agent.service.d.ts +171 -0
  86. package/dist/services/agent.service.js +329 -0
  87. package/dist/services/api-key.service.d.ts +41 -0
  88. package/dist/services/api-key.service.js +80 -0
  89. package/dist/services/auth.service.d.ts +133 -0
  90. package/dist/services/auth.service.js +411 -0
  91. package/dist/services/billing.service.d.ts +67 -0
  92. package/dist/services/billing.service.js +254 -0
  93. package/dist/services/chat-token.service.d.ts +29 -0
  94. package/dist/services/chat-token.service.js +113 -0
  95. package/dist/services/connector-registry.service.d.ts +156 -0
  96. package/dist/services/connector-registry.service.js +278 -0
  97. package/dist/services/conversation.service.d.ts +47 -0
  98. package/dist/services/conversation.service.js +101 -0
  99. package/dist/services/email-templates.d.ts +18 -0
  100. package/dist/services/email-templates.js +39 -0
  101. package/dist/services/email.service.d.ts +26 -0
  102. package/dist/services/email.service.js +42 -0
  103. package/dist/services/errors.d.ts +7 -0
  104. package/dist/services/errors.js +27 -0
  105. package/dist/services/in-memory-prepared-stream.store.d.ts +13 -0
  106. package/dist/services/in-memory-prepared-stream.store.js +35 -0
  107. package/dist/services/index.d.ts +13 -0
  108. package/dist/services/index.js +40 -0
  109. package/dist/services/mcp-client.service.d.ts +64 -0
  110. package/dist/services/mcp-client.service.js +157 -0
  111. package/dist/services/mcp-server.service.d.ts +44 -0
  112. package/dist/services/mcp-server.service.js +147 -0
  113. package/dist/services/oauth.service.d.ts +73 -0
  114. package/dist/services/oauth.service.js +174 -0
  115. package/dist/services/oauth2.service.d.ts +57 -0
  116. package/dist/services/oauth2.service.js +82 -0
  117. package/dist/services/orchestrator.service.d.ts +45 -0
  118. package/dist/services/orchestrator.service.js +180 -0
  119. package/dist/services/plan.service.d.ts +54 -0
  120. package/dist/services/plan.service.js +120 -0
  121. package/dist/services/prepared-stream.service.d.ts +23 -0
  122. package/dist/services/prepared-stream.service.js +43 -0
  123. package/dist/services/refresh-token.service.d.ts +38 -0
  124. package/dist/services/refresh-token.service.js +73 -0
  125. package/dist/services/secrets/crypto.d.ts +37 -0
  126. package/dist/services/secrets/crypto.js +110 -0
  127. package/dist/services/secrets/known-keys.d.ts +38 -0
  128. package/dist/services/secrets/known-keys.js +50 -0
  129. package/dist/services/secrets.service.d.ts +91 -0
  130. package/dist/services/secrets.service.js +193 -0
  131. package/dist/services/tenant-billing.service.d.ts +121 -0
  132. package/dist/services/tenant-billing.service.js +290 -0
  133. package/dist/services/tenant.service.d.ts +54 -0
  134. package/dist/services/tenant.service.js +96 -0
  135. package/dist/services/tool-registry.service.d.ts +42 -0
  136. package/dist/services/tool-registry.service.js +101 -0
  137. package/dist/services/upload.service.d.ts +37 -0
  138. package/dist/services/upload.service.js +84 -0
  139. package/dist/services/usage.service.d.ts +34 -0
  140. package/dist/services/usage.service.js +108 -0
  141. package/dist/types/agent.types.d.ts +160 -0
  142. package/dist/types/agent.types.js +2 -0
  143. package/dist/types/billing.types.d.ts +82 -0
  144. package/dist/types/billing.types.js +3 -0
  145. package/dist/types/config.types.d.ts +127 -0
  146. package/dist/types/config.types.js +9 -0
  147. package/dist/types/hooks.d.ts +85 -0
  148. package/dist/types/hooks.js +2 -0
  149. package/dist/types/index.d.ts +3 -0
  150. package/dist/types/index.js +19 -0
  151. package/package.json +36 -0
@@ -0,0 +1,82 @@
1
+ export type SubscriptionStatus = 'active' | 'trialing' | 'past_due' | 'canceled' | 'unpaid' | 'incomplete' | 'paused';
2
+ export type PaymentStatus = 'succeeded' | 'failed' | 'pending' | 'refunded';
3
+ export interface UserSubscription {
4
+ id: string;
5
+ userId: string;
6
+ planId: string;
7
+ status: SubscriptionStatus;
8
+ /** Provider-specific subscription ID (e.g., Stripe sub_xxx) */
9
+ providerSubscriptionId?: string;
10
+ /** Provider-specific customer ID (e.g., Stripe cus_xxx) */
11
+ providerCustomerId?: string;
12
+ currentPeriodStart: Date;
13
+ currentPeriodEnd: Date;
14
+ trialEnd?: Date;
15
+ cancelAtPeriodEnd: boolean;
16
+ createdAt: Date;
17
+ updatedAt: Date;
18
+ }
19
+ export interface UsageSummary {
20
+ userId: string;
21
+ planId: string;
22
+ periodStart: Date;
23
+ periodEnd: Date;
24
+ requestsUsed: number;
25
+ tokensUsed: number;
26
+ creditsUsed?: number;
27
+ requestsLimit: number;
28
+ tokensLimit: number;
29
+ creditsBalance?: number;
30
+ percentUsed: {
31
+ requests: number;
32
+ tokens: number;
33
+ };
34
+ isOverLimit: boolean;
35
+ }
36
+ export interface UsageRecord {
37
+ id: string;
38
+ userId: string;
39
+ conversationId: string;
40
+ messageId: string;
41
+ agentId: string;
42
+ inputTokens: number;
43
+ outputTokens: number;
44
+ totalTokens: number;
45
+ requestCount: number;
46
+ creditsConsumed?: number;
47
+ costInCents?: number;
48
+ billingPeriod: string;
49
+ recordedAt: Date;
50
+ }
51
+ export interface CheckoutResult {
52
+ sessionId: string;
53
+ url: string;
54
+ planId: string;
55
+ userId: string;
56
+ }
57
+ export interface PortalResult {
58
+ url: string;
59
+ }
60
+ export interface BillingOverviewResult {
61
+ subscription: UserSubscription | null;
62
+ usage: UsageSummary;
63
+ plan: {
64
+ id: string;
65
+ name: string;
66
+ features: string[];
67
+ limits: {
68
+ requestsPerMonth: number;
69
+ tokensPerMonth: number;
70
+ };
71
+ };
72
+ invoiceHistory?: InvoiceRecord[];
73
+ }
74
+ export interface InvoiceRecord {
75
+ id: string;
76
+ amount: number;
77
+ currency: string;
78
+ status: string;
79
+ createdAt: Date;
80
+ pdfUrl?: string;
81
+ hostedUrl?: string;
82
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ // ─── Billing Runtime Types ────────────────────────────────────────────────────
3
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,127 @@
1
+ export interface AgentForgeConfig {
2
+ /** Anthropic API configuration */
3
+ anthropic: AnthropicConfig;
4
+ /** PostgreSQL database configuration */
5
+ database: DatabaseConfig;
6
+ /** Redis configuration for BullMQ queue */
7
+ redis?: RedisConfig;
8
+ /** Agent definitions */
9
+ agents: AgentDefinition[];
10
+ /** Queue configuration */
11
+ queue?: QueueConfig;
12
+ }
13
+ export interface AnthropicConfig {
14
+ apiKey: string;
15
+ /** Default model to use across all agents (can be overridden per agent) */
16
+ defaultModel?: string;
17
+ /** Default max tokens per response */
18
+ defaultMaxTokens?: number;
19
+ /** Base URL override (useful for proxies) */
20
+ baseURL?: string;
21
+ }
22
+ export interface DatabaseConfig {
23
+ url: string;
24
+ /** Run migrations on startup (default: true) */
25
+ autoMigrate?: boolean;
26
+ /** SSL configuration */
27
+ ssl?: boolean | {
28
+ rejectUnauthorized: boolean;
29
+ };
30
+ /** Connection pool size */
31
+ poolSize?: number;
32
+ /** Log SQL queries (default: false) */
33
+ logging?: boolean;
34
+ }
35
+ export interface RedisConfig {
36
+ /**
37
+ * Connection URL — `redis://[:password@]host:port[/db]` or `rediss://...`.
38
+ * Plain `host:port` is also accepted (we'll prepend `redis://`).
39
+ * If provided, takes precedence over the structured fields below.
40
+ */
41
+ url?: string;
42
+ host?: string;
43
+ port?: number;
44
+ password?: string;
45
+ db?: number;
46
+ tls?: boolean;
47
+ }
48
+ export interface QueueConfig {
49
+ /** Enable background job queue (requires Redis) */
50
+ enabled: boolean;
51
+ /** Max concurrent jobs per worker */
52
+ concurrency?: number;
53
+ /** Default job options */
54
+ defaultJobOptions?: {
55
+ attempts?: number;
56
+ backoff?: {
57
+ type: 'exponential' | 'fixed';
58
+ delay: number;
59
+ };
60
+ removeOnComplete?: boolean | number;
61
+ removeOnFail?: boolean | number;
62
+ };
63
+ }
64
+ export interface JwtConfig {
65
+ secret: string;
66
+ expiresIn?: string;
67
+ }
68
+ export interface AgentDefinition {
69
+ /** Unique identifier for the agent */
70
+ id: string;
71
+ /** Human-readable name */
72
+ name: string;
73
+ /** Agent description (shown to users) */
74
+ description?: string;
75
+ /** Claude model to use */
76
+ model?: string;
77
+ /** System prompt */
78
+ systemPrompt: string;
79
+ /** Max tokens per response */
80
+ maxTokens?: number;
81
+ /** Temperature (0-1) */
82
+ temperature?: number;
83
+ /** Top-p sampling */
84
+ topP?: number;
85
+ /** Tools available to this agent */
86
+ tools?: string[];
87
+ /** MCP servers this agent can connect to */
88
+ mcpServers?: McpServerConfig[];
89
+ /** Whether this agent can spawn subagents */
90
+ canOrchestrate?: boolean;
91
+ /** Subagents this orchestrator can delegate to */
92
+ subAgents?: string[];
93
+ /** Restrict to specific plans */
94
+ requiredPlan?: string[];
95
+ /** Custom metadata */
96
+ metadata?: Record<string, unknown>;
97
+ }
98
+ /**
99
+ * Configuration for an MCP server the runtime should connect to.
100
+ *
101
+ * v1 supports remote MCP servers only (HTTP/SSE). Stdio transport is
102
+ * deliberately out of scope — it requires spawning a subprocess and managing
103
+ * its lifecycle, which is harder to deploy in serverless / container setups.
104
+ *
105
+ * The `name` is the human-friendly identifier the admin UI shows. It also
106
+ * prefixes every tool the server exposes (`<name>__<tool>`) so multiple MCPs
107
+ * can register tools with overlapping names without colliding in the
108
+ * ToolRegistry.
109
+ */
110
+ export interface McpServerConfig {
111
+ /** Stable identifier — also used as a tool-name prefix. */
112
+ name: string;
113
+ /**
114
+ * Wire protocol. `http` = Streamable HTTP (current standard); `sse` =
115
+ * legacy Server-Sent Events transport, still common in older servers.
116
+ * Stdio not supported in v1.
117
+ */
118
+ transport: 'http' | 'sse';
119
+ /** Base URL of the MCP server (the `/mcp` or `/sse` endpoint). */
120
+ url: string;
121
+ /**
122
+ * Static headers attached to every outbound request — typically used for
123
+ * `Authorization: Bearer <token>` or vendor-specific API keys. Resolve
124
+ * secrets via the host's SecretsService before passing them here.
125
+ */
126
+ headers?: Record<string, string>;
127
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ // ─── AgentForge Configuration Types ──────────────────────────────────────────
3
+ //
4
+ // The runtime SDK's config carries only what the agent loop needs to run:
5
+ // Anthropic credentials, DB connection, Redis (optional), the agent catalog,
6
+ // and queue settings. Auth, billing, email, secrets — those live in the host
7
+ // and are NEVER part of AgentForgeConfig. The host wires them through its
8
+ // own modules (e.g. AuthModule reads ConfigService, AUTH_CONFIG token, etc.).
9
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Host-supplied hooks for observing SDK runtime events.
3
+ *
4
+ * The control plane (server/host) registers these to react to what the
5
+ * AI runtime does — record usage, append audit logs, push to analytics.
6
+ *
7
+ * Failures inside a hook are logged and swallowed; they do NOT interrupt
8
+ * the agent. The SDK is a runtime, not a guard.
9
+ *
10
+ * Phase 1: observation only. No abort, no payload mutation.
11
+ */
12
+ export interface SdkHooks {
13
+ /**
14
+ * Fired once per agent turn (user message → final agent reply) with
15
+ * the aggregated token usage of that turn. Multiple internal LLM
16
+ * calls inside one turn are summed before dispatch.
17
+ *
18
+ * Typical host handler: persist a UsageRecord, kick off async
19
+ * rate-limit checks, emit to analytics.
20
+ */
21
+ onUsage?: (event: UsageEvent) => Promise<void>;
22
+ /**
23
+ * Fired when an agent turn completes — successfully, with an error,
24
+ * or aborted (e.g. client SSE disconnect). Useful for audit trails,
25
+ * latency metrics, "last activity" stamping.
26
+ */
27
+ onTurnComplete?: (event: TurnCompleteEvent) => Promise<void>;
28
+ /**
29
+ * Fired once per tool invocation by an agent. Sub-agent delegations
30
+ * are surfaced as nested `onTurnComplete` events, not as tool calls.
31
+ */
32
+ onToolCall?: (event: ToolCallEvent) => Promise<void>;
33
+ }
34
+ /** Payload for `onUsage`. */
35
+ export interface UsageEvent {
36
+ version: 1;
37
+ /** Stable id for this turn (1:1 with an agent reply). */
38
+ turnId: string;
39
+ conversationId: string;
40
+ /** Agent slug or uuid. */
41
+ agentId: string;
42
+ /** If the host is multi-tenant, the tenant that owns the turn. */
43
+ tenantId?: string;
44
+ /** End-user that triggered the turn (host-internal). */
45
+ userId?: string;
46
+ provider: 'anthropic' | 'openai' | 'google' | 'other';
47
+ modelId: string;
48
+ /** Sum across all LLM calls inside the turn. */
49
+ inputTokens: number;
50
+ outputTokens: number;
51
+ /** Wall-clock duration of the turn in ms. */
52
+ durationMs: number;
53
+ /** Server-side timestamp when the turn ended. */
54
+ at: Date;
55
+ }
56
+ /** Payload for `onTurnComplete`. */
57
+ export interface TurnCompleteEvent {
58
+ version: 1;
59
+ turnId: string;
60
+ conversationId: string;
61
+ agentId: string;
62
+ tenantId?: string;
63
+ userId?: string;
64
+ status: 'ok' | 'error' | 'aborted';
65
+ errorMessage?: string;
66
+ /** Count of tool calls executed during this turn (0..N). */
67
+ toolCallCount: number;
68
+ durationMs: number;
69
+ at: Date;
70
+ }
71
+ /** Payload for `onToolCall`. */
72
+ export interface ToolCallEvent {
73
+ version: 1;
74
+ turnId: string;
75
+ conversationId: string;
76
+ agentId: string;
77
+ tenantId?: string;
78
+ toolName: string;
79
+ /** Raw args the tool was invoked with — useful for debugging/audit. */
80
+ args: Record<string, unknown>;
81
+ /** Set when the tool execution threw. */
82
+ error?: string;
83
+ durationMs: number;
84
+ at: Date;
85
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ export * from './agent.types';
2
+ export * from './config.types';
3
+ export * from './hooks';
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./agent.types"), exports);
18
+ __exportStar(require("./config.types"), exports);
19
+ __exportStar(require("./hooks"), exports);
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@agentforge-io/core",
3
+ "version": "0.2.0",
4
+ "description": "Framework-free AI runtime SDK. Owns: agent loop (Anthropic), conversations, tools, streaming, agent-job queue, SdkHooks. Identity, billing, infra (email/uploads/secrets) live in the host's modules — not here.",
5
+ "license": "MIT",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ },
13
+ "./ai": {
14
+ "types": "./dist/ai/index.d.ts",
15
+ "default": "./dist/ai/index.js"
16
+ }
17
+ },
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "scripts": {
22
+ "build": "tsc -p tsconfig.build.json",
23
+ "build:watch": "tsc -p tsconfig.build.json --watch",
24
+ "clean": "rm -rf dist *.tgz",
25
+ "test": "node --test --import tsx --test-reporter=spec tests/*.test.ts"
26
+ },
27
+ "dependencies": {
28
+ "@anthropic-ai/sdk": "^0.32.0",
29
+ "@modelcontextprotocol/sdk": "^1.29.0"
30
+ },
31
+ "devDependencies": {
32
+ "@types/node": "^20.0.0",
33
+ "tsx": "^4.19.0",
34
+ "typescript": "^5.0.0"
35
+ }
36
+ }