@cleocode/lafs-protocol 0.5.0 → 1.0.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 (38) hide show
  1. package/LICENSE +0 -0
  2. package/README.md +0 -0
  3. package/dist/examples/discovery-server.d.ts +8 -0
  4. package/dist/examples/discovery-server.js +216 -0
  5. package/dist/examples/mcp-lafs-client.d.ts +10 -0
  6. package/dist/examples/mcp-lafs-client.js +427 -0
  7. package/dist/examples/mcp-lafs-server.d.ts +10 -0
  8. package/dist/examples/mcp-lafs-server.js +358 -0
  9. package/dist/schemas/v1/envelope.schema.json +0 -0
  10. package/dist/schemas/v1/error-registry.json +0 -0
  11. package/dist/src/budgetEnforcement.d.ts +84 -0
  12. package/dist/src/budgetEnforcement.js +328 -0
  13. package/dist/src/cli.d.ts +0 -0
  14. package/dist/src/cli.js +0 -0
  15. package/dist/src/conformance.d.ts +0 -0
  16. package/dist/src/conformance.js +0 -0
  17. package/dist/src/discovery.d.ts +127 -0
  18. package/dist/src/discovery.js +304 -0
  19. package/dist/src/errorRegistry.d.ts +0 -0
  20. package/dist/src/errorRegistry.js +0 -0
  21. package/dist/src/flagSemantics.d.ts +0 -0
  22. package/dist/src/flagSemantics.js +0 -0
  23. package/dist/src/index.d.ts +4 -0
  24. package/dist/src/index.js +4 -0
  25. package/dist/src/mcpAdapter.d.ts +28 -0
  26. package/dist/src/mcpAdapter.js +281 -0
  27. package/dist/src/tokenEstimator.d.ts +87 -0
  28. package/dist/src/tokenEstimator.js +238 -0
  29. package/dist/src/types.d.ts +25 -0
  30. package/dist/src/types.js +0 -0
  31. package/dist/src/validateEnvelope.d.ts +0 -0
  32. package/dist/src/validateEnvelope.js +0 -0
  33. package/lafs.md +164 -0
  34. package/package.json +8 -3
  35. package/schemas/v1/context-ledger.schema.json +0 -0
  36. package/schemas/v1/discovery.schema.json +132 -0
  37. package/schemas/v1/envelope.schema.json +0 -0
  38. package/schemas/v1/error-registry.json +0 -0
@@ -0,0 +1,328 @@
1
+ /**
2
+ * LAFS Budget Enforcement
3
+ *
4
+ * Middleware for enforcing MVI (Minimal Viable Interface) token budgets on LAFS envelopes.
5
+ * Provides budget checking, truncation, and error generation for exceeded budgets.
6
+ */
7
+ import { TokenEstimator } from "./tokenEstimator.js";
8
+ /**
9
+ * Budget exceeded error code from LAFS error registry
10
+ */
11
+ const BUDGET_EXCEEDED_CODE = "E_MVI_BUDGET_EXCEEDED";
12
+ /**
13
+ * Default category for budget exceeded errors
14
+ */
15
+ const BUDGET_ERROR_CATEGORY = "VALIDATION";
16
+ /**
17
+ * Create a budget exceeded error object
18
+ */
19
+ function createBudgetExceededError(estimated, budget) {
20
+ return {
21
+ code: BUDGET_EXCEEDED_CODE,
22
+ message: `Response exceeds declared MVI budget: estimated ${estimated} tokens, budget ${budget} tokens`,
23
+ category: BUDGET_ERROR_CATEGORY,
24
+ retryable: false,
25
+ retryAfterMs: null,
26
+ details: {
27
+ estimatedTokens: estimated,
28
+ budgetTokens: budget,
29
+ exceededBy: estimated - budget,
30
+ exceededByPercent: Math.round(((estimated - budget) / budget) * 100),
31
+ },
32
+ };
33
+ }
34
+ /**
35
+ * Truncate a result to fit within budget.
36
+ * Returns the truncated result and whether truncation occurred.
37
+ */
38
+ function truncateResult(result, targetTokens, estimator) {
39
+ if (result === null) {
40
+ return { result: null, wasTruncated: false };
41
+ }
42
+ const currentEstimate = estimator.estimate(result);
43
+ // If already within budget, no truncation needed
44
+ if (currentEstimate <= targetTokens) {
45
+ return { result, wasTruncated: false };
46
+ }
47
+ // Calculate target size (conservative: assume 10% overhead)
48
+ const targetChars = Math.floor(targetTokens * 4 * 0.9);
49
+ if (Array.isArray(result)) {
50
+ return truncateArray(result, targetChars, targetTokens, estimator);
51
+ }
52
+ return truncateObject(result, targetChars, targetTokens, estimator);
53
+ }
54
+ /**
55
+ * Truncate an array to fit within budget.
56
+ */
57
+ function truncateArray(arr, targetChars, targetTokens, estimator) {
58
+ if (arr.length === 0) {
59
+ return { result: arr, wasTruncated: false };
60
+ }
61
+ // Binary search to find how many items fit
62
+ let left = 0;
63
+ let right = arr.length;
64
+ let bestFit = 0;
65
+ while (left <= right) {
66
+ const mid = Math.floor((left + right) / 2);
67
+ const subset = arr.slice(0, mid);
68
+ const estimate = estimator.estimate(subset);
69
+ if (estimate <= targetTokens) {
70
+ bestFit = mid;
71
+ left = mid + 1;
72
+ }
73
+ else {
74
+ right = mid - 1;
75
+ }
76
+ }
77
+ // If we can fit all items, no truncation needed
78
+ if (bestFit >= arr.length) {
79
+ return { result: arr, wasTruncated: false };
80
+ }
81
+ // Create truncated result
82
+ const truncated = arr.slice(0, bestFit);
83
+ // If we couldn't fit any items, return minimal response
84
+ if (bestFit === 0 && arr.length > 0) {
85
+ return {
86
+ result: [{ _truncated: true, reason: "budget_exceeded" }],
87
+ wasTruncated: true
88
+ };
89
+ }
90
+ // Add truncation indicator to last element if it's an object
91
+ if (bestFit > 0 && typeof truncated[bestFit - 1] === 'object' && truncated[bestFit - 1] !== null) {
92
+ const lastItem = truncated[bestFit - 1];
93
+ truncated[bestFit - 1] = {
94
+ ...lastItem,
95
+ _truncated: true,
96
+ remainingItems: arr.length - bestFit,
97
+ };
98
+ }
99
+ return { result: truncated, wasTruncated: true };
100
+ }
101
+ /**
102
+ * Truncate an object to fit within budget.
103
+ */
104
+ function truncateObject(obj, targetChars, targetTokens, estimator) {
105
+ const keys = Object.keys(obj);
106
+ if (keys.length === 0) {
107
+ return { result: obj, wasTruncated: false };
108
+ }
109
+ // Try to fit as many top-level properties as possible
110
+ let left = 0;
111
+ let right = keys.length;
112
+ let bestFit = 0;
113
+ while (left <= right) {
114
+ const mid = Math.floor((left + right) / 2);
115
+ const subsetKeys = keys.slice(0, mid);
116
+ const subset = {};
117
+ for (const key of subsetKeys) {
118
+ subset[key] = obj[key];
119
+ }
120
+ const estimate = estimator.estimate(subset);
121
+ if (estimate <= targetTokens) {
122
+ bestFit = mid;
123
+ left = mid + 1;
124
+ }
125
+ else {
126
+ right = mid - 1;
127
+ }
128
+ }
129
+ // If we can fit all properties, no truncation needed
130
+ if (bestFit >= keys.length) {
131
+ return { result: obj, wasTruncated: false };
132
+ }
133
+ // Create truncated result
134
+ const subsetKeys = keys.slice(0, bestFit);
135
+ const truncated = {};
136
+ for (const key of subsetKeys) {
137
+ truncated[key] = obj[key];
138
+ }
139
+ // If we couldn't fit any properties, return minimal response
140
+ if (bestFit === 0) {
141
+ return {
142
+ result: { _truncated: true, reason: "budget_exceeded" },
143
+ wasTruncated: true
144
+ };
145
+ }
146
+ // Add truncation metadata
147
+ truncated._truncated = true;
148
+ truncated._truncatedFields = keys.slice(bestFit);
149
+ return { result: truncated, wasTruncated: true };
150
+ }
151
+ /**
152
+ * Apply budget enforcement to an envelope.
153
+ *
154
+ * @param envelope - The LAFS envelope to check
155
+ * @param budget - Maximum allowed tokens
156
+ * @param options - Budget enforcement options
157
+ * @returns Enforce result with potentially modified envelope
158
+ */
159
+ export function applyBudgetEnforcement(envelope, budget, options = {}) {
160
+ const { truncateOnExceed = false, onBudgetExceeded } = options;
161
+ const estimator = new TokenEstimator();
162
+ // Estimate the result payload
163
+ const estimatedTokens = estimator.estimate(envelope.result);
164
+ // Add estimate to metadata
165
+ const tokenEstimate = {
166
+ estimated: estimatedTokens,
167
+ };
168
+ // Check if within budget
169
+ const withinBudget = estimatedTokens <= budget;
170
+ // If within budget, just add the estimate to metadata
171
+ if (withinBudget) {
172
+ return {
173
+ envelope: {
174
+ ...envelope,
175
+ _meta: {
176
+ ...envelope._meta,
177
+ _tokenEstimate: tokenEstimate,
178
+ },
179
+ },
180
+ withinBudget: true,
181
+ estimatedTokens,
182
+ budget,
183
+ truncated: false,
184
+ };
185
+ }
186
+ // Budget exceeded - call callback if provided
187
+ if (onBudgetExceeded) {
188
+ onBudgetExceeded(estimatedTokens, budget);
189
+ }
190
+ // If truncation is enabled, try to truncate
191
+ if (truncateOnExceed) {
192
+ const { result, wasTruncated } = truncateResult(envelope.result, budget, estimator);
193
+ const truncatedEstimate = estimator.estimate(result);
194
+ if (truncatedEstimate <= budget) {
195
+ return {
196
+ envelope: {
197
+ ...envelope,
198
+ result,
199
+ _meta: {
200
+ ...envelope._meta,
201
+ _tokenEstimate: {
202
+ estimated: truncatedEstimate,
203
+ truncated: true,
204
+ originalEstimate: estimatedTokens,
205
+ },
206
+ },
207
+ },
208
+ withinBudget: true,
209
+ estimatedTokens: truncatedEstimate,
210
+ budget,
211
+ truncated: true,
212
+ };
213
+ }
214
+ }
215
+ // Return budget exceeded error
216
+ return {
217
+ envelope: {
218
+ ...envelope,
219
+ success: false,
220
+ result: null,
221
+ error: createBudgetExceededError(estimatedTokens, budget),
222
+ _meta: {
223
+ ...envelope._meta,
224
+ _tokenEstimate: tokenEstimate,
225
+ },
226
+ },
227
+ withinBudget: false,
228
+ estimatedTokens,
229
+ budget,
230
+ truncated: false,
231
+ };
232
+ }
233
+ /**
234
+ * Create a budget enforcement middleware function.
235
+ *
236
+ * @param budget - Maximum allowed tokens for response
237
+ * @param options - Budget enforcement options
238
+ * @returns Middleware function that enforces budget
239
+ *
240
+ * @example
241
+ * ```typescript
242
+ * const middleware = withBudget(1000, { truncateOnExceed: true });
243
+ * const result = await middleware(envelope, async () => nextEnvelope);
244
+ * ```
245
+ */
246
+ export function withBudget(budget, options = {}) {
247
+ return async (envelope, next) => {
248
+ // Execute next middleware/handler
249
+ const result = await next();
250
+ // Apply budget enforcement to the result
251
+ const enforcement = applyBudgetEnforcement(result, budget, options);
252
+ return enforcement.envelope;
253
+ };
254
+ }
255
+ /**
256
+ * Check if an envelope has exceeded its budget without modifying it.
257
+ *
258
+ * @param envelope - The LAFS envelope to check
259
+ * @param budget - Maximum allowed tokens
260
+ * @returns Budget check result
261
+ */
262
+ export function checkBudget(envelope, budget) {
263
+ const estimator = new TokenEstimator();
264
+ const estimated = estimator.estimate(envelope.result);
265
+ return {
266
+ exceeded: estimated > budget,
267
+ estimated,
268
+ remaining: Math.max(0, budget - estimated),
269
+ };
270
+ }
271
+ /**
272
+ * Synchronous version of withBudget for non-async contexts.
273
+ *
274
+ * @param budget - Maximum allowed tokens for response
275
+ * @param options - Budget enforcement options
276
+ * @returns Middleware function that enforces budget synchronously
277
+ */
278
+ export function withBudgetSync(budget, options = {}) {
279
+ return (envelope, next) => {
280
+ const result = next();
281
+ const enforcement = applyBudgetEnforcement(result, budget, options);
282
+ return enforcement.envelope;
283
+ };
284
+ }
285
+ /**
286
+ * Higher-order function that wraps a handler with budget enforcement.
287
+ *
288
+ * @param handler - The handler function to wrap
289
+ * @param budget - Maximum allowed tokens
290
+ * @param options - Budget enforcement options
291
+ * @returns Wrapped handler with budget enforcement
292
+ *
293
+ * @example
294
+ * ```typescript
295
+ * const myHandler = async (request: Request) => ({ success: true, result: { data } });
296
+ * const budgetedHandler = wrapWithBudget(myHandler, 1000, { truncateOnExceed: true });
297
+ * const result = await budgetedHandler(request);
298
+ * ```
299
+ */
300
+ export function wrapWithBudget(handler, budget, options = {}) {
301
+ return async (...args) => {
302
+ const result = await handler(...args);
303
+ const enforcement = applyBudgetEnforcement(result, budget, options);
304
+ return enforcement.envelope;
305
+ };
306
+ }
307
+ /**
308
+ * Compose multiple middleware functions into a single middleware.
309
+ * Middleware is executed in order (left to right).
310
+ */
311
+ export function composeMiddleware(...middlewares) {
312
+ return async (envelope, next) => {
313
+ let index = 0;
314
+ async function dispatch(i) {
315
+ if (i >= middlewares.length) {
316
+ return next();
317
+ }
318
+ const middleware = middlewares[i];
319
+ if (!middleware) {
320
+ return dispatch(i + 1);
321
+ }
322
+ return middleware(envelope, () => dispatch(i + 1));
323
+ }
324
+ return dispatch(0);
325
+ };
326
+ }
327
+ export { TokenEstimator };
328
+ export { BUDGET_EXCEEDED_CODE };
package/dist/src/cli.d.ts CHANGED
File without changes
package/dist/src/cli.js CHANGED
File without changes
File without changes
File without changes
@@ -0,0 +1,127 @@
1
+ /**
2
+ * LAFS Agent Discovery - Express/Fastify Middleware
3
+ * Serves discovery document at /.well-known/lafs.json
4
+ */
5
+ import type { RequestHandler } from "express";
6
+ /**
7
+ * Capability definition for service advertisement
8
+ */
9
+ export interface Capability {
10
+ name: string;
11
+ version: string;
12
+ description?: string;
13
+ operations: string[];
14
+ optional?: boolean;
15
+ }
16
+ /**
17
+ * Service configuration for discovery document
18
+ */
19
+ export interface ServiceConfig {
20
+ name: string;
21
+ version: string;
22
+ description?: string;
23
+ }
24
+ /**
25
+ * Endpoint configuration for discovery document
26
+ */
27
+ export interface EndpointConfig {
28
+ envelope: string;
29
+ context?: string;
30
+ discovery: string;
31
+ }
32
+ /**
33
+ * Complete discovery document served at /.well-known/lafs.json
34
+ */
35
+ export interface DiscoveryDocument {
36
+ $schema: string;
37
+ lafs_version: string;
38
+ service: ServiceConfig;
39
+ capabilities: Capability[];
40
+ endpoints: EndpointConfig;
41
+ }
42
+ /**
43
+ * Configuration for the discovery middleware
44
+ */
45
+ export interface DiscoveryConfig {
46
+ /** Service information */
47
+ service: ServiceConfig;
48
+ /** List of capabilities this service provides */
49
+ capabilities: Capability[];
50
+ /** Endpoint URLs - can be relative paths or absolute URLs */
51
+ endpoints: {
52
+ /** URL for envelope submission endpoint */
53
+ envelope: string;
54
+ /** Optional URL for context ledger endpoint */
55
+ context?: string;
56
+ /** URL for this discovery document (usually auto-detected) */
57
+ discovery?: string;
58
+ };
59
+ /** Cache duration in seconds (default: 3600) */
60
+ cacheMaxAge?: number;
61
+ /** LAFS protocol version (default: "1.0.0") */
62
+ lafsVersion?: string;
63
+ /** Schema URL override */
64
+ schemaUrl?: string;
65
+ /** Base URL for constructing absolute URLs */
66
+ baseUrl?: string;
67
+ /** Optional custom headers to include in response */
68
+ headers?: Record<string, string>;
69
+ }
70
+ /**
71
+ * Discovery middleware options
72
+ */
73
+ export interface DiscoveryMiddlewareOptions {
74
+ /** Path to serve discovery document (default: /.well-known/lafs.json) */
75
+ path?: string;
76
+ /** Enable HEAD requests (default: true) */
77
+ enableHead?: boolean;
78
+ /** Enable ETag caching (default: true) */
79
+ enableEtag?: boolean;
80
+ }
81
+ /**
82
+ * Create Express middleware for serving LAFS discovery document
83
+ *
84
+ * @param config - Discovery configuration
85
+ * @param options - Middleware options
86
+ * @returns Express RequestHandler
87
+ *
88
+ * @example
89
+ * ```typescript
90
+ * import express from "express";
91
+ * import { discoveryMiddleware } from "./discovery.js";
92
+ *
93
+ * const app = express();
94
+ *
95
+ * app.use(discoveryMiddleware({
96
+ * service: {
97
+ * name: "my-lafs-service",
98
+ * version: "1.0.0",
99
+ * description: "A LAFS-compliant API service"
100
+ * },
101
+ * capabilities: [
102
+ * {
103
+ * name: "envelope-processor",
104
+ * version: "1.0.0",
105
+ * operations: ["process", "validate"],
106
+ * description: "Process LAFS envelopes"
107
+ * }
108
+ * ],
109
+ * endpoints: {
110
+ * envelope: "/api/v1/envelope",
111
+ * context: "/api/v1/context"
112
+ * }
113
+ * }));
114
+ * ```
115
+ */
116
+ export declare function discoveryMiddleware(config: DiscoveryConfig, options?: DiscoveryMiddlewareOptions): RequestHandler;
117
+ /**
118
+ * Fastify plugin for LAFS discovery (for Fastify users)
119
+ *
120
+ * @param fastify - Fastify instance
121
+ * @param options - Plugin options
122
+ */
123
+ export declare function discoveryFastifyPlugin(fastify: unknown, options: {
124
+ config: DiscoveryConfig;
125
+ path?: string;
126
+ }): Promise<void>;
127
+ export default discoveryMiddleware;