@cleocode/lafs 2026.3.74 → 2026.4.3

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 (143) hide show
  1. package/LICENSE +0 -0
  2. package/README.md +97 -68
  3. package/dist/schemas/v1/agent-card.schema.json +230 -0
  4. package/dist/schemas/v1/conformance-profiles.json +0 -0
  5. package/dist/schemas/v1/context-ledger.schema.json +70 -0
  6. package/dist/schemas/v1/discovery.schema.json +132 -0
  7. package/dist/schemas/v1/envelope.schema.json +0 -0
  8. package/dist/schemas/v1/error-registry.json +0 -0
  9. package/dist/src/a2a/bindings/grpc.d.ts +118 -11
  10. package/dist/src/a2a/bindings/grpc.d.ts.map +1 -0
  11. package/dist/src/a2a/bindings/grpc.js +80 -8
  12. package/dist/src/a2a/bindings/grpc.js.map +1 -0
  13. package/dist/src/a2a/bindings/http.d.ts +131 -15
  14. package/dist/src/a2a/bindings/http.d.ts.map +1 -0
  15. package/dist/src/a2a/bindings/http.js +101 -14
  16. package/dist/src/a2a/bindings/http.js.map +1 -0
  17. package/dist/src/a2a/bindings/index.d.ts +83 -9
  18. package/dist/src/a2a/bindings/index.d.ts.map +1 -0
  19. package/dist/src/a2a/bindings/index.js +74 -6
  20. package/dist/src/a2a/bindings/index.js.map +1 -0
  21. package/dist/src/a2a/bindings/jsonrpc.d.ts +194 -9
  22. package/dist/src/a2a/bindings/jsonrpc.d.ts.map +1 -0
  23. package/dist/src/a2a/bindings/jsonrpc.js +155 -10
  24. package/dist/src/a2a/bindings/jsonrpc.js.map +1 -0
  25. package/dist/src/a2a/bridge.d.ts +237 -44
  26. package/dist/src/a2a/bridge.d.ts.map +1 -0
  27. package/dist/src/a2a/bridge.js +187 -48
  28. package/dist/src/a2a/bridge.js.map +1 -0
  29. package/dist/src/a2a/extensions.d.ts +222 -12
  30. package/dist/src/a2a/extensions.d.ts.map +1 -0
  31. package/dist/src/a2a/extensions.js +178 -13
  32. package/dist/src/a2a/extensions.js.map +1 -0
  33. package/dist/src/a2a/index.d.ts +10 -7
  34. package/dist/src/a2a/index.d.ts.map +1 -0
  35. package/dist/src/a2a/index.js +24 -27
  36. package/dist/src/a2a/index.js.map +1 -0
  37. package/dist/src/a2a/streaming.d.ts +276 -3
  38. package/dist/src/a2a/streaming.d.ts.map +1 -0
  39. package/dist/src/a2a/streaming.js +255 -11
  40. package/dist/src/a2a/streaming.js.map +1 -0
  41. package/dist/src/a2a/task-lifecycle.d.ts +341 -20
  42. package/dist/src/a2a/task-lifecycle.d.ts.map +1 -0
  43. package/dist/src/a2a/task-lifecycle.js +327 -26
  44. package/dist/src/a2a/task-lifecycle.js.map +1 -0
  45. package/dist/src/budgetEnforcement.d.ts +93 -20
  46. package/dist/src/budgetEnforcement.d.ts.map +1 -0
  47. package/dist/src/budgetEnforcement.js +146 -31
  48. package/dist/src/budgetEnforcement.js.map +1 -0
  49. package/dist/src/circuit-breaker/index.d.ts +260 -10
  50. package/dist/src/circuit-breaker/index.d.ts.map +1 -0
  51. package/dist/src/circuit-breaker/index.js +226 -14
  52. package/dist/src/circuit-breaker/index.js.map +1 -0
  53. package/dist/src/cli.d.ts +1 -0
  54. package/dist/src/cli.d.ts.map +1 -0
  55. package/dist/src/cli.js +12 -11
  56. package/dist/src/cli.js.map +1 -0
  57. package/dist/src/compliance.d.ts +180 -3
  58. package/dist/src/compliance.d.ts.map +1 -0
  59. package/dist/src/compliance.js +114 -13
  60. package/dist/src/compliance.js.map +1 -0
  61. package/dist/src/conformance.d.ts +55 -2
  62. package/dist/src/conformance.d.ts.map +1 -0
  63. package/dist/src/conformance.js +124 -76
  64. package/dist/src/conformance.js.map +1 -0
  65. package/dist/src/conformanceProfiles.d.ts +68 -1
  66. package/dist/src/conformanceProfiles.d.ts.map +1 -0
  67. package/dist/src/conformanceProfiles.js +53 -1
  68. package/dist/src/conformanceProfiles.js.map +1 -0
  69. package/dist/src/deprecationRegistry.d.ts +82 -1
  70. package/dist/src/deprecationRegistry.d.ts.map +1 -0
  71. package/dist/src/deprecationRegistry.js +58 -7
  72. package/dist/src/deprecationRegistry.js.map +1 -0
  73. package/dist/src/discovery.d.ts +347 -65
  74. package/dist/src/discovery.d.ts.map +1 -0
  75. package/dist/src/discovery.js +130 -72
  76. package/dist/src/discovery.js.map +1 -0
  77. package/dist/src/envelope.d.ts +262 -9
  78. package/dist/src/envelope.d.ts.map +1 -0
  79. package/dist/src/envelope.js +179 -15
  80. package/dist/src/envelope.js.map +1 -0
  81. package/dist/src/errorRegistry.d.ts +163 -3
  82. package/dist/src/errorRegistry.d.ts.map +1 -0
  83. package/dist/src/errorRegistry.js +119 -3
  84. package/dist/src/errorRegistry.js.map +1 -0
  85. package/dist/src/fieldExtraction.d.ts +128 -27
  86. package/dist/src/fieldExtraction.d.ts.map +1 -0
  87. package/dist/src/fieldExtraction.js +100 -27
  88. package/dist/src/fieldExtraction.js.map +1 -0
  89. package/dist/src/flagResolver.d.ts +77 -10
  90. package/dist/src/flagResolver.d.ts.map +1 -0
  91. package/dist/src/flagResolver.js +22 -5
  92. package/dist/src/flagResolver.js.map +1 -0
  93. package/dist/src/flagSemantics.d.ts +80 -4
  94. package/dist/src/flagSemantics.d.ts.map +1 -0
  95. package/dist/src/flagSemantics.js +78 -11
  96. package/dist/src/flagSemantics.js.map +1 -0
  97. package/dist/src/health/index.d.ts +103 -9
  98. package/dist/src/health/index.d.ts.map +1 -0
  99. package/dist/src/health/index.js +75 -26
  100. package/dist/src/health/index.js.map +1 -0
  101. package/dist/src/index.d.ts +34 -23
  102. package/dist/src/index.d.ts.map +1 -0
  103. package/dist/src/index.js +40 -28
  104. package/dist/src/index.js.map +1 -0
  105. package/dist/src/mviProjection.d.ts +43 -6
  106. package/dist/src/mviProjection.d.ts.map +1 -0
  107. package/dist/src/mviProjection.js +32 -5
  108. package/dist/src/mviProjection.js.map +1 -0
  109. package/dist/src/native-loader.d.ts +49 -0
  110. package/dist/src/native-loader.d.ts.map +1 -0
  111. package/dist/src/native-loader.js +56 -0
  112. package/dist/src/native-loader.js.map +1 -0
  113. package/dist/src/problemDetails.d.ts +71 -4
  114. package/dist/src/problemDetails.d.ts.map +1 -0
  115. package/dist/src/problemDetails.js +27 -3
  116. package/dist/src/problemDetails.js.map +1 -0
  117. package/dist/src/shutdown/index.d.ts +103 -9
  118. package/dist/src/shutdown/index.d.ts.map +1 -0
  119. package/dist/src/shutdown/index.js +78 -12
  120. package/dist/src/shutdown/index.js.map +1 -0
  121. package/dist/src/tokenEstimator.d.ts +98 -11
  122. package/dist/src/tokenEstimator.d.ts.map +1 -0
  123. package/dist/src/tokenEstimator.js +91 -13
  124. package/dist/src/tokenEstimator.js.map +1 -0
  125. package/dist/src/types.d.ts +477 -11
  126. package/dist/src/types.d.ts.map +1 -0
  127. package/dist/src/types.js +76 -2
  128. package/dist/src/types.js.map +1 -0
  129. package/dist/src/validateEnvelope.d.ts +61 -2
  130. package/dist/src/validateEnvelope.d.ts.map +1 -0
  131. package/dist/src/validateEnvelope.js +81 -14
  132. package/dist/src/validateEnvelope.js.map +1 -0
  133. package/dist/tsconfig.build.tsbuildinfo +1 -0
  134. package/lafs.md +3 -4
  135. package/package.json +14 -12
  136. package/schemas/v1/agent-card.schema.json +0 -0
  137. package/schemas/v1/conformance-profiles.json +0 -0
  138. package/schemas/v1/context-ledger.schema.json +0 -0
  139. package/schemas/v1/discovery.schema.json +0 -0
  140. package/schemas/v1/envelope.schema.json +0 -0
  141. package/schemas/v1/error-registry.json +0 -0
  142. package/dist/src/mcpAdapter.d.ts +0 -28
  143. package/dist/src/mcpAdapter.js +0 -281
@@ -1,281 +0,0 @@
1
- import { randomUUID } from "node:crypto";
2
- /**
3
- * Extract result from MCP content array
4
- * Attempts to parse JSON content, falls back to text representation
5
- */
6
- function extractResultFromContent(content) {
7
- if (!content || content.length === 0) {
8
- return null;
9
- }
10
- // Combine all text content
11
- const textParts = [];
12
- const otherContent = [];
13
- for (const item of content) {
14
- if (item.type === "text" && item.text) {
15
- textParts.push(item.text);
16
- }
17
- else {
18
- otherContent.push(item);
19
- }
20
- }
21
- // Try to parse as JSON if single text item looks like JSON
22
- if (textParts.length === 1) {
23
- const text = textParts[0]?.trim() ?? "";
24
- if (text.startsWith("{") || text.startsWith("[")) {
25
- try {
26
- const parsed = JSON.parse(text);
27
- if (typeof parsed === "object" && parsed !== null) {
28
- return parsed;
29
- }
30
- }
31
- catch {
32
- // Not valid JSON, treat as text
33
- }
34
- }
35
- }
36
- // Combine into result object
37
- const result = {};
38
- if (textParts.length > 0) {
39
- result.text = textParts.length === 1 ? textParts[0] : textParts.join("\n");
40
- }
41
- if (otherContent.length > 0) {
42
- result.content = otherContent;
43
- }
44
- return result;
45
- }
46
- /**
47
- * Estimate token count from content
48
- * Rough estimation: ~4 characters per token
49
- */
50
- function estimateTokens(content) {
51
- if (!content)
52
- return 0;
53
- const jsonString = JSON.stringify(content);
54
- return Math.ceil(jsonString.length / 4);
55
- }
56
- /**
57
- * Truncate content to fit within budget
58
- */
59
- function truncateToBudget(content, budget) {
60
- if (!content)
61
- return { result: null, truncated: false, originalEstimate: 0 };
62
- const originalEstimate = estimateTokens(content);
63
- if (originalEstimate <= budget) {
64
- return { result: content, truncated: false, originalEstimate };
65
- }
66
- // Calculate truncation ratio
67
- const ratio = budget / originalEstimate;
68
- const jsonString = JSON.stringify(content);
69
- const targetLength = Math.floor(jsonString.length * ratio);
70
- // Truncate the string and try to make it valid JSON
71
- let truncated = jsonString.slice(0, targetLength);
72
- // Close any open structures
73
- const openBraces = (truncated.match(/\{/g) || []).length - (truncated.match(/\}/g) || []).length;
74
- const openBrackets = (truncated.match(/\[/g) || []).length - (truncated.match(/\]/g) || []).length;
75
- truncated += "}".repeat(Math.max(0, openBraces));
76
- truncated += "]".repeat(Math.max(0, openBrackets));
77
- try {
78
- const parsed = JSON.parse(truncated);
79
- // Add truncation notice
80
- if (typeof parsed === "object" && parsed !== null) {
81
- parsed["_truncated"] = true;
82
- parsed["_originalTokens"] = originalEstimate;
83
- parsed["_budget"] = budget;
84
- }
85
- return { result: parsed, truncated: true, originalEstimate };
86
- }
87
- catch {
88
- // If parsing fails, return minimal result
89
- return {
90
- result: {
91
- _truncated: true,
92
- _error: "Content truncated due to budget constraints",
93
- _originalTokens: originalEstimate,
94
- _budget: budget,
95
- },
96
- truncated: true,
97
- originalEstimate,
98
- };
99
- }
100
- }
101
- /**
102
- * Convert MCP error to LAFS error format
103
- */
104
- function convertMCPErrorToLAFS(mcpResult, operation) {
105
- const content = mcpResult.content;
106
- const errorText = content
107
- .filter((item) => item.type === "text" && typeof item.text === "string")
108
- .map((item) => item.text)
109
- .join("\n");
110
- // Determine error category based on error text
111
- let category = "INTERNAL";
112
- let code = "E_MCP_INTERNAL_ERROR";
113
- let retryable = false;
114
- let retryAfterMs = null;
115
- const errorLower = errorText.toLowerCase();
116
- if (errorLower.includes("not found") || errorLower.includes("doesn't exist") || errorLower.includes("does not exist")) {
117
- category = "NOT_FOUND";
118
- code = "E_MCP_NOT_FOUND";
119
- }
120
- else if (errorLower.includes("rate limit") || errorLower.includes("too many requests")) {
121
- category = "RATE_LIMIT";
122
- code = "E_MCP_RATE_LIMIT";
123
- retryable = true;
124
- retryAfterMs = 60000; // 1 minute default
125
- }
126
- else if (errorLower.includes("auth") || errorLower.includes("unauthorized") || errorLower.includes("forbidden")) {
127
- category = "AUTH";
128
- code = "E_MCP_AUTH_ERROR";
129
- }
130
- else if (errorLower.includes("permission") || errorLower.includes("access denied")) {
131
- category = "PERMISSION";
132
- code = "E_MCP_PERMISSION_DENIED";
133
- }
134
- else if (errorLower.includes("validation") || errorLower.includes("invalid")) {
135
- category = "VALIDATION";
136
- code = "E_MCP_VALIDATION_ERROR";
137
- }
138
- else if (errorLower.includes("timeout") || errorLower.includes("transient")) {
139
- category = "TRANSIENT";
140
- code = "E_MCP_TRANSIENT_ERROR";
141
- retryable = true;
142
- retryAfterMs = 5000; // 5 seconds
143
- }
144
- return {
145
- code,
146
- message: errorText || "MCP tool execution failed",
147
- category,
148
- retryable,
149
- retryAfterMs,
150
- details: {
151
- operation,
152
- mcpError: true,
153
- contentTypes: content.map((c) => c.type),
154
- },
155
- };
156
- }
157
- /**
158
- * Wrap MCP tool result in LAFS envelope
159
- *
160
- * @param mcpResult - The raw MCP CallToolResult
161
- * @param operation - The operation name (tool name)
162
- * @param budget - Optional token budget for response
163
- * @returns LAFS-compliant envelope
164
- */
165
- export function wrapMCPResult(mcpResult, operation, budget) {
166
- const requestId = randomUUID();
167
- const timestamp = new Date().toISOString();
168
- // Build base meta
169
- const meta = {
170
- specVersion: "1.0.0",
171
- schemaVersion: "1.0.0",
172
- timestamp,
173
- operation,
174
- requestId,
175
- transport: "sdk",
176
- strict: true,
177
- mvi: "standard",
178
- contextVersion: 1,
179
- };
180
- // Handle MCP error
181
- if (mcpResult.isError) {
182
- const error = convertMCPErrorToLAFS(mcpResult, operation);
183
- return {
184
- $schema: "https://lafs.dev/schemas/v1/envelope.schema.json",
185
- _meta: meta,
186
- success: false,
187
- result: null,
188
- error,
189
- };
190
- }
191
- // Extract result from MCP content
192
- let result = extractResultFromContent(mcpResult.content);
193
- let truncated = false;
194
- let originalEstimate = 0;
195
- let extensions;
196
- // Apply budget enforcement if specified
197
- if (budget !== undefined && budget > 0) {
198
- const budgetResult = truncateToBudget(result, budget);
199
- result = budgetResult.result;
200
- truncated = budgetResult.truncated;
201
- originalEstimate = budgetResult.originalEstimate;
202
- // Put token estimate in extensions to comply with strict schema
203
- extensions = {
204
- "x-mcp-token-estimate": {
205
- estimated: truncated ? budget : originalEstimate,
206
- truncated,
207
- originalEstimate: truncated ? originalEstimate : undefined,
208
- },
209
- };
210
- }
211
- return {
212
- $schema: "https://lafs.dev/schemas/v1/envelope.schema.json",
213
- _meta: meta,
214
- success: true,
215
- result,
216
- error: null,
217
- _extensions: extensions,
218
- };
219
- }
220
- /**
221
- * Create a LAFS error envelope for MCP adapter errors
222
- *
223
- * @param message - Error message
224
- * @param operation - The operation being performed
225
- * @param category - Error category
226
- * @returns LAFS error envelope
227
- */
228
- export function createAdapterErrorEnvelope(message, operation, category = "INTERNAL") {
229
- const requestId = randomUUID();
230
- const timestamp = new Date().toISOString();
231
- const error = {
232
- code: "E_MCP_ADAPTER_ERROR",
233
- message,
234
- category,
235
- retryable: category === "TRANSIENT" || category === "RATE_LIMIT",
236
- retryAfterMs: category === "RATE_LIMIT" ? 60000 : category === "TRANSIENT" ? 5000 : null,
237
- details: {
238
- operation,
239
- adapterError: true,
240
- },
241
- };
242
- return {
243
- $schema: "https://lafs.dev/schemas/v1/envelope.schema.json",
244
- _meta: {
245
- specVersion: "1.0.0",
246
- schemaVersion: "1.0.0",
247
- timestamp,
248
- operation,
249
- requestId,
250
- transport: "sdk",
251
- strict: true,
252
- mvi: "standard",
253
- contextVersion: 1,
254
- },
255
- success: false,
256
- result: null,
257
- error,
258
- };
259
- }
260
- /**
261
- * Type guard to check if content is TextContent
262
- */
263
- export function isTextContent(content) {
264
- return (typeof content === "object" &&
265
- content !== null &&
266
- "type" in content &&
267
- content.type === "text" &&
268
- "text" in content &&
269
- typeof content.text === "string");
270
- }
271
- /**
272
- * Parse MCP text content as JSON if possible
273
- */
274
- export function parseMCPTextContent(content) {
275
- try {
276
- return JSON.parse(content.text);
277
- }
278
- catch {
279
- return content.text;
280
- }
281
- }