@clawcash/forge 0.1.7 → 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.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- import { Application, Router, Request, Response, NextFunction } from 'express';
2
-
3
1
  type SchemaFieldType = "string" | "url" | "number" | "boolean";
4
2
  type InputSchema = Record<string, SchemaFieldType>;
5
3
  type OutputSchema = Record<string, SchemaFieldType>;
@@ -32,69 +30,6 @@ interface DefinedAgentService<TInput extends Record<string, unknown> = Record<st
32
30
  readonly __forge: true;
33
31
  path: string;
34
32
  }
35
- interface FacilitatorAuthHeaders {
36
- verify: Record<string, string>;
37
- settle: Record<string, string>;
38
- supported: Record<string, string>;
39
- bazaar?: Record<string, string>;
40
- }
41
- interface ForgePayToSource {
42
- owner: string;
43
- repo: string;
44
- /** Forge web base URL. Optional — uses FORGE_URL env when set. */
45
- baseUrl?: string;
46
- }
47
- interface MountOptions {
48
- /**
49
- * Recipient wallet. Prefer omitting and letting `forge` + baked fallback
50
- * resolve automatically so merchants never set PAY_TO on their host.
51
- * Still honored as override when set (or via process.env.PAY_TO).
52
- */
53
- payTo?: string;
54
- /**
55
- * Baked-in Forge wallet from codegen. Used when live Forge fetch is
56
- * unavailable. Required unless skipPayment or payTo is set.
57
- */
58
- payToFallback?: string;
59
- /**
60
- * Auto-resolve payTo from Forge public API (no merchant env required).
61
- */
62
- forge?: ForgePayToSource;
63
- /**
64
- * Facilitator URL. Defaults to XPay (`https://facilitator.xpay.sh`) —
65
- * Base mainnet, no API keys. Override with CDP or others if needed.
66
- */
67
- facilitatorUrl?: string;
68
- /** Optional auth header factory (e.g. CDP JWT). Not required for XPay. */
69
- createAuthHeaders?: () => Promise<FacilitatorAuthHeaders>;
70
- /** Path prefix for agent routes. Defaults to /agent */
71
- basePath?: string;
72
- /** Skip x402 (local dev only). Defaults to false. */
73
- skipPayment?: boolean;
74
- /**
75
- * Optional gateway heartbeat after mount.
76
- * Prefer env: GATEWAY_URL, FORGE_HANDLE, FORGE_API_KEY, PUBLIC_BASE_URL.
77
- */
78
- gateway?: {
79
- url?: string;
80
- handle?: string;
81
- /** @deprecated Prefer FORGE_API_KEY env — per-project key from Forge dashboard. */
82
- registerSecret?: string;
83
- /** Merchant public base URL for fulfillment. */
84
- publicBaseUrl?: string;
85
- };
86
- /**
87
- * Path prefix for private fulfillment (gateway → merchant after x402).
88
- * Defaults to /forge/fulfill. Not end-user / x402 routes.
89
- */
90
- fulfillPath?: string;
91
- }
92
- interface SkillMeta {
93
- name: string;
94
- description: string;
95
- baseUrl: string;
96
- homepage?: string;
97
- }
98
33
 
99
34
  declare function waitUntil<T>(options: WaitUntilOptions<T>): Promise<T>;
100
35
  declare function createContext(): AgentServiceContext;
@@ -102,59 +37,82 @@ declare function createContext(): AgentServiceContext;
102
37
  declare function toKebabCase(name: string): string;
103
38
  declare function defineAgentService<TInput extends Record<string, unknown> = Record<string, unknown>, TOutput extends Record<string, unknown> = Record<string, unknown>>(config: AgentServiceConfig<TInput, TOutput>): DefinedAgentService<TInput, TOutput>;
104
39
 
105
- /**
106
- * Mount Forge agent services on an Express app.
107
- * - GET /forge/health, GET /forge/status, GET /.well-known/clawcash.json — liveliness
108
- * - POST {basePath}/{kebab-name} — optional public x402 agent surface
109
- * - POST /forge/fulfill/{kebab-name} private gateway fulfillment after payment
110
- *
111
- * payTo resolves automatically: PAY_TO env (optional) → Forge API → baked fallback.
112
- */
113
- declare function mountAgentServices(app: Application, services: DefinedAgentService[], options: MountOptions): Promise<Router>;
114
- declare function createForgeRouter(services: DefinedAgentService[], options: MountOptions): (app: Application) => Promise<Router>;
40
+ interface ForgeWorkerOptions {
41
+ services: DefinedAgentService[];
42
+ /** Merchant handle. Defaults to FORGE_HANDLE env. */
43
+ handle?: string;
44
+ /** Per-project key from the Forge dashboard. Defaults to FORGE_API_KEY env. */
45
+ apiKey?: string;
46
+ /** Defaults to https://gateway.clawca.sh (or GATEWAY_URL env). */
47
+ gatewayUrl?: string;
48
+ /** Shown in gateway job rows. Defaults to hostname-pid. */
49
+ workerId?: string;
50
+ /** Long-poll window per claim request. Defaults to 20s (gateway caps at 25s). */
51
+ waitMs?: number;
52
+ onError?: (error: Error) => void;
53
+ }
54
+ interface ForgeWorkerHandle {
55
+ stop: () => void;
56
+ readonly running: boolean;
57
+ }
58
+ declare function forgeWorker(options: ForgeWorkerOptions): ForgeWorkerHandle;
115
59
 
116
60
  /**
117
- * Generate a SKILL.md document for agents from defined Forge services.
61
+ * Web-standard fetch handler mount Forge fulfillment + liveliness on any
62
+ * runtime that speaks (Request) => Response: Next.js route handlers, Hono,
63
+ * Fastify (via adapter), Bun, Deno, plain node:http (via adapter).
64
+ *
65
+ * This surface is for gateway-push (fulfillment.mode=sdk) merchants and
66
+ * deploy probes. Payment lives on the gateway; prefer forgeWorker() when the
67
+ * merchant can run an outbound worker instead.
68
+ *
69
+ * Routes handled (anything else returns null from tryHandleForgeRequest, or
70
+ * 404 from the plain handler):
71
+ * - GET /forge/health
72
+ * - GET /forge/status
73
+ * - GET /.well-known/clawcash.json
74
+ * - POST {fulfillPath}/{action-path} (requires FORGE_API_KEY auth)
118
75
  */
119
- declare function generateSkillMarkdown(services: DefinedAgentService[], meta: SkillMeta): string;
120
-
121
- declare function isZeroAddress(address: string | undefined | null): boolean;
76
+ interface ForgeHandlerOptions {
77
+ /** Merchant handle — FORGE_HANDLE env when omitted. */
78
+ handle?: string;
79
+ /** Per-project key. FORGE_API_KEY env when omitted. */
80
+ apiKey?: string;
81
+ /** Defaults to /forge/fulfill. */
82
+ fulfillPath?: string;
83
+ /** Reported in /forge/status. Optional. */
84
+ publicBaseUrl?: string;
85
+ /** Reported in /forge/status. Defaults to GATEWAY_URL env or the ClawCash gateway. */
86
+ gatewayUrl?: string;
87
+ }
88
+ type ForgeFetchHandler = (request: Request) => Promise<Response>;
122
89
  /**
123
- * Resolve merchant payTo with minimal host config:
124
- * 1. `PAY_TO` env (optional override)
125
- * 2. Live address from Forge public API (when reachable)
126
- * 3. Baked-in fallback from the Forge PR
90
+ * Returns a Response when the request targets a Forge route, else null so
91
+ * the host app can fall through to its own routing.
127
92
  */
128
- declare function resolvePayTo(opts: {
129
- /** Baked-in address from Forge codegen (required fallback). */
130
- fallback: string;
131
- /** Optional explicit override (usually process.env.PAY_TO). */
132
- payTo?: string;
133
- forge?: ForgePayToSource;
134
- /** Fetch timeout ms. Defaults to 2500. */
135
- timeoutMs?: number;
136
- }): Promise<string>;
93
+ declare function createForgeRequestMatcher(services: DefinedAgentService[], options?: ForgeHandlerOptions): (request: Request) => Promise<Response | null>;
94
+ /** Like createForgeRequestMatcher, but 404s instead of returning null. */
95
+ declare function createForgeHandler(services: DefinedAgentService[], options?: ForgeHandlerOptions): ForgeFetchHandler;
137
96
 
138
97
  /**
139
- * Notify ClawCash gateway of this merchant's live public base URL.
140
- * Full action registration still happens from Forge "Go live".
98
+ * Framework-free input validation shared by every execution surface
99
+ * (Express mount, fetch handler, worker).
141
100
  */
142
- declare function heartbeatGateway(opts: {
143
- gatewayUrl: string;
144
- handle: string;
145
- baseUrl: string;
146
- registerSecret: string;
147
- }): Promise<void>;
101
+ type ValidationResult = {
102
+ ok: true;
103
+ value: Record<string, unknown>;
104
+ } | {
105
+ ok: false;
106
+ error: string;
107
+ };
108
+ declare function validateInput(body: unknown, schema: InputSchema): ValidationResult;
109
+ declare function checkType(key: string, value: unknown, type: SchemaFieldType): string | null;
148
110
 
149
111
  /**
150
- * Merchant-side Forge surface status — public probes for deploy / go-live checks.
112
+ * Merchant-side Forge surface status — public probes for deploy / go-live
113
+ * checks when running the fetch handler. Worker-mode merchants report
114
+ * liveness by polling the gateway instead.
151
115
  */
152
- type ForgeHeartbeatState = {
153
- attempted: boolean;
154
- ok: boolean | null;
155
- at: string | null;
156
- error: string | null;
157
- };
158
116
  type ForgeSurfaceStatus = {
159
117
  ok: boolean;
160
118
  forge: true;
@@ -162,42 +120,29 @@ type ForgeSurfaceStatus = {
162
120
  handle: string | null;
163
121
  publicBaseUrl: string | null;
164
122
  gatewayUrl: string | null;
165
- /** True when FORGE_API_KEY is set so /forge/fulfill is mounted. */
123
+ /** True when FORGE_API_KEY is set so fulfillment is accepted. */
166
124
  fulfillMounted: boolean;
167
125
  fulfillPath: string;
168
- agentBasePath: string;
169
- skipPayment: boolean;
170
126
  actions: Array<{
171
127
  name: string;
172
128
  path: string;
173
129
  price: string;
174
130
  }>;
175
- /** Env + fulfill ready — merchant can accept gateway fulfillment. */
131
+ /** Key + actions present — merchant can accept gateway fulfillment. */
176
132
  ready: boolean;
177
- heartbeat: ForgeHeartbeatState;
178
133
  endpoints: {
179
134
  health: string;
180
135
  status: string;
181
136
  wellKnown: string;
182
137
  fulfill: string;
183
- agent: string;
184
138
  };
185
139
  };
186
- declare function recordHeartbeatResult(result: {
187
- ok: true;
188
- } | {
189
- ok: false;
190
- error: string;
191
- }): void;
192
- declare function getLastHeartbeat(): ForgeHeartbeatState;
193
140
  declare function buildForgeSurfaceStatus(opts: {
194
141
  handle: string | null;
195
142
  publicBaseUrl: string | null;
196
143
  gatewayUrl: string | null;
197
144
  fulfillMounted: boolean;
198
145
  fulfillPath: string;
199
- agentBasePath: string;
200
- skipPayment: boolean;
201
146
  actions: Array<{
202
147
  name: string;
203
148
  path: string;
@@ -215,20 +160,19 @@ declare function buildWellKnownDocument(status: ForgeSurfaceStatus): {
215
160
  declare function forgeSdkVersion(): string;
216
161
 
217
162
  /**
218
- * Per-project Forge API key for gateway → merchant fulfillment.
219
- * Merchants set FORGE_API_KEY from the Forge dashboard (never the shared
220
- * GATEWAY_REGISTER_SECRET that is Forge↔gateway platform auth only).
163
+ * Per-project Forge API key helpers framework-free.
164
+ *
165
+ * Merchants set FORGE_API_KEY from the Forge dashboard. The worker uses it
166
+ * to claim jobs from the gateway; the fetch handler uses it to gate
167
+ * gateway-push fulfillment; execute() can use forgeGatewayLoopbackHeaders()
168
+ * so the merchant's own API middleware can recognize fulfillment traffic.
221
169
  *
222
170
  * GATEWAY_REGISTER_SECRET is accepted as a deprecated fallback for older deploys.
223
171
  */
224
172
  declare function getForgeApiKey(explicit?: string | null): string | null;
225
- /** @deprecated Use getForgeApiKey */
226
- declare function getForgeGatewaySecret(explicit?: string | null): string | null;
227
- declare function isForgeGatewayRequest(req: Request, secret?: string | null): boolean;
228
- /** Synthetic user id for DB rows created by gateway-driven fulfillment. */
173
+ /** Synthetic user id for DB rows created by fulfillment traffic. */
229
174
  declare const FORGE_GATEWAY_USER_ID = "forge_gateway_agent";
230
- declare function requireForgeGateway(secret?: string | null): (req: Request, res: Response, next: NextFunction) => void;
231
175
  /** Headers merchant execute() should send on loopback calls during fulfillment. */
232
176
  declare function forgeGatewayLoopbackHeaders(secret?: string | null): Record<string, string>;
233
177
 
234
- export { type AgentServiceConfig, type AgentServiceContext, type DefinedAgentService, FORGE_GATEWAY_USER_ID, type ForgeHeartbeatState, type ForgePayToSource, type ForgeSurfaceStatus, type InputSchema, type MountOptions, type OutputSchema, type PaymentConfig, type SchemaFieldType, type SkillMeta, type WaitUntilOptions, buildForgeSurfaceStatus, buildWellKnownDocument, createContext, createForgeRouter, defineAgentService, forgeGatewayLoopbackHeaders, forgeSdkVersion, generateSkillMarkdown, getForgeApiKey, getForgeGatewaySecret, getLastHeartbeat, heartbeatGateway, isForgeGatewayRequest, isZeroAddress, mountAgentServices, recordHeartbeatResult, requireForgeGateway, resolvePayTo, toKebabCase, waitUntil };
178
+ export { type AgentServiceConfig, type AgentServiceContext, type DefinedAgentService, FORGE_GATEWAY_USER_ID, type ForgeFetchHandler, type ForgeHandlerOptions, type ForgeSurfaceStatus, type ForgeWorkerHandle, type ForgeWorkerOptions, type InputSchema, type OutputSchema, type PaymentConfig, type SchemaFieldType, type ValidationResult, type WaitUntilOptions, buildForgeSurfaceStatus, buildWellKnownDocument, checkType, createContext, createForgeHandler, createForgeRequestMatcher, defineAgentService, forgeGatewayLoopbackHeaders, forgeSdkVersion, forgeWorker, getForgeApiKey, toKebabCase, validateInput, waitUntil };