recoder-a2a-sdk 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 (59) hide show
  1. package/dist/audit-helpers.d.mts +209 -0
  2. package/dist/audit-helpers.d.ts +209 -0
  3. package/dist/audit-helpers.js +905 -0
  4. package/dist/audit-helpers.js.map +1 -0
  5. package/dist/audit-helpers.mjs +34 -0
  6. package/dist/audit-helpers.mjs.map +1 -0
  7. package/dist/chunk-7GY5FFBI.mjs +117 -0
  8. package/dist/chunk-7GY5FFBI.mjs.map +1 -0
  9. package/dist/chunk-AGD6G5N6.mjs +141 -0
  10. package/dist/chunk-AGD6G5N6.mjs.map +1 -0
  11. package/dist/chunk-BWCRJDUB.mjs +357 -0
  12. package/dist/chunk-BWCRJDUB.mjs.map +1 -0
  13. package/dist/chunk-DPFH2JIC.mjs +141 -0
  14. package/dist/chunk-DPFH2JIC.mjs.map +1 -0
  15. package/dist/chunk-EP2GHC3R.mjs +116 -0
  16. package/dist/chunk-EP2GHC3R.mjs.map +1 -0
  17. package/dist/chunk-JOZQDX7A.mjs +359 -0
  18. package/dist/chunk-JOZQDX7A.mjs.map +1 -0
  19. package/dist/chunk-MAA7JZ2Q.mjs +142 -0
  20. package/dist/chunk-MAA7JZ2Q.mjs.map +1 -0
  21. package/dist/chunk-QVOOBAUA.mjs +140 -0
  22. package/dist/chunk-QVOOBAUA.mjs.map +1 -0
  23. package/dist/chunk-T5GUBIFZ.mjs +358 -0
  24. package/dist/chunk-T5GUBIFZ.mjs.map +1 -0
  25. package/dist/chunk-WLNW7Y2H.mjs +636 -0
  26. package/dist/chunk-WLNW7Y2H.mjs.map +1 -0
  27. package/dist/chunk-XSELW6DU.mjs +356 -0
  28. package/dist/chunk-XSELW6DU.mjs.map +1 -0
  29. package/dist/envelope-builders.d.mts +189 -0
  30. package/dist/envelope-builders.d.ts +189 -0
  31. package/dist/envelope-builders.js +652 -0
  32. package/dist/envelope-builders.js.map +1 -0
  33. package/dist/envelope-builders.mjs +20 -0
  34. package/dist/envelope-builders.mjs.map +1 -0
  35. package/dist/error-mapping.d.mts +132 -0
  36. package/dist/error-mapping.d.ts +132 -0
  37. package/dist/error-mapping.js +642 -0
  38. package/dist/error-mapping.js.map +1 -0
  39. package/dist/error-mapping.mjs +16 -0
  40. package/dist/error-mapping.mjs.map +1 -0
  41. package/dist/index.d.mts +7 -0
  42. package/dist/index.d.ts +7 -0
  43. package/dist/index.js +1798 -0
  44. package/dist/index.js.map +1 -0
  45. package/dist/index.mjs +153 -0
  46. package/dist/index.mjs.map +1 -0
  47. package/dist/state-mapping.d.mts +139 -0
  48. package/dist/state-mapping.d.ts +139 -0
  49. package/dist/state-mapping.js +641 -0
  50. package/dist/state-mapping.js.map +1 -0
  51. package/dist/state-mapping.mjs +28 -0
  52. package/dist/state-mapping.mjs.map +1 -0
  53. package/dist/validation.d.mts +117 -0
  54. package/dist/validation.d.ts +117 -0
  55. package/dist/validation.js +850 -0
  56. package/dist/validation.js.map +1 -0
  57. package/dist/validation.mjs +22 -0
  58. package/dist/validation.mjs.map +1 -0
  59. package/package.json +18 -0
package/dist/index.js ADDED
@@ -0,0 +1,1798 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ A2AErrorCategoryEnum: () => A2AErrorCategoryEnum,
24
+ A2AErrorCodeEnum: () => A2AErrorCodeEnum,
25
+ A2AErrorSeverityEnum: () => A2AErrorSeverityEnum,
26
+ A2AStructuredError: () => A2AStructuredError,
27
+ A2A_ERROR_CODE_REGISTRY: () => A2A_ERROR_CODE_REGISTRY,
28
+ ALLOWED_STATE_TRANSITIONS: () => ALLOWED_STATE_TRANSITIONS,
29
+ AUDIT_EVENT_TYPES: () => AUDIT_EVENT_TYPES,
30
+ AUDIT_EVENT_VERSION: () => AUDIT_EVENT_VERSION,
31
+ AuditActorKindEnum: () => AuditActorKindEnum,
32
+ AuditEventTypeEnum: () => AuditEventTypeEnum,
33
+ AuditSeverityEnum: () => AuditSeverityEnum,
34
+ AuditTargetKindEnum: () => AuditTargetKindEnum,
35
+ TASK_ENVELOPE_VERSION: () => TASK_ENVELOPE_VERSION,
36
+ TASK_STATES: () => TASK_STATES,
37
+ TERMINAL_TASK_STATES: () => TERMINAL_TASK_STATES,
38
+ TaskStateEnum: () => TaskStateEnum,
39
+ a2aErrorToTaskErrorInfo: () => a2aErrorToTaskErrorInfo,
40
+ assertAuditEvent: () => assertAuditEvent,
41
+ assertTaskEnvelope: () => assertTaskEnvelope,
42
+ buildA2AError: () => buildA2AError,
43
+ buildClaudeEnvelope: () => buildClaudeEnvelope,
44
+ buildCodexEnvelope: () => buildCodexEnvelope,
45
+ cancelEnvelope: () => cancelEnvelope,
46
+ canonicalToClaudeState: () => canonicalToClaudeState,
47
+ canonicalToCodexState: () => canonicalToCodexState,
48
+ canonicalToConnectorState: () => canonicalToConnectorState,
49
+ checkConnectorTransition: () => checkConnectorTransition,
50
+ completeEnvelope: () => completeEnvelope,
51
+ createAuditEvent: () => createAuditEvent,
52
+ createTaskEnvelope: () => createTaskEnvelope,
53
+ emitAuditForTransition: () => emitAuditForTransition,
54
+ emitConnectorAttached: () => emitConnectorAttached,
55
+ emitConnectorError: () => emitConnectorError,
56
+ emitConnectorHealth: () => emitConnectorHealth,
57
+ emitPolicyApproved: () => emitPolicyApproved,
58
+ emitPolicyChecked: () => emitPolicyChecked,
59
+ emitPolicyDenied: () => emitPolicyDenied,
60
+ emitTaskCancelled: () => emitTaskCancelled,
61
+ emitTaskCompleted: () => emitTaskCompleted,
62
+ emitTaskFailed: () => emitTaskFailed,
63
+ emitTaskInputRequired: () => emitTaskInputRequired,
64
+ emitTaskProgress: () => emitTaskProgress,
65
+ emitTaskStarted: () => emitTaskStarted,
66
+ emitTaskSubmitted: () => emitTaskSubmitted,
67
+ failEnvelope: () => failEnvelope,
68
+ getErrorCodeMeta: () => getErrorCodeMeta,
69
+ isA2AErrorCode: () => isA2AErrorCode,
70
+ isConnectorTerminalState: () => isConnectorTerminalState,
71
+ isTerminalState: () => isTerminalState,
72
+ isValidA2AError: () => isValidA2AError,
73
+ isValidAuditEvent: () => isValidAuditEvent,
74
+ isValidTaskEnvelope: () => isValidTaskEnvelope,
75
+ isValidTransition: () => isValidTransition,
76
+ mapClaudeError: () => mapClaudeError,
77
+ mapClaudeState: () => mapClaudeState,
78
+ mapClaudeStateStrict: () => mapClaudeStateStrict,
79
+ mapCodexError: () => mapCodexError,
80
+ mapCodexState: () => mapCodexState,
81
+ mapCodexStateStrict: () => mapCodexStateStrict,
82
+ mapConnectorError: () => mapConnectorError,
83
+ mapConnectorState: () => mapConnectorState,
84
+ mapConnectorStateStrict: () => mapConnectorStateStrict,
85
+ mapHttpError: () => mapHttpError,
86
+ startEnvelope: () => startEnvelope,
87
+ transitionTaskEnvelope: () => transitionTaskEnvelope,
88
+ validateA2AError: () => validateA2AError,
89
+ validateAuditEvent: () => validateAuditEvent,
90
+ validateTaskEnvelope: () => validateTaskEnvelope,
91
+ wrapUnknownError: () => wrapUnknownError
92
+ });
93
+ module.exports = __toCommonJS(index_exports);
94
+
95
+ // ../../shared/src/a2a/schemas/task-envelope.ts
96
+ var TASK_ENVELOPE_VERSION = "1.0.0";
97
+ var TaskStateEnum = {
98
+ /** Task has been created and is awaiting execution. */
99
+ Submitted: "submitted",
100
+ /** Task is actively being processed by the assignee agent. */
101
+ Working: "working",
102
+ /** The assignee needs additional input before it can continue. */
103
+ InputRequired: "input-required",
104
+ /** Task finished successfully. */
105
+ Completed: "completed",
106
+ /** Task terminated with an error. */
107
+ Failed: "failed",
108
+ /** Task was explicitly cancelled by a requester or operator. */
109
+ Cancelled: "cancelled"
110
+ };
111
+ var TASK_STATES = [
112
+ TaskStateEnum.Submitted,
113
+ TaskStateEnum.Working,
114
+ TaskStateEnum.InputRequired,
115
+ TaskStateEnum.Completed,
116
+ TaskStateEnum.Failed,
117
+ TaskStateEnum.Cancelled
118
+ ];
119
+ var TERMINAL_TASK_STATES = /* @__PURE__ */ new Set([
120
+ TaskStateEnum.Completed,
121
+ TaskStateEnum.Failed,
122
+ TaskStateEnum.Cancelled
123
+ ]);
124
+ function isTerminalState(state) {
125
+ return TERMINAL_TASK_STATES.has(state);
126
+ }
127
+ function createTaskEnvelope(input) {
128
+ const now = input.now ?? (/* @__PURE__ */ new Date()).toISOString();
129
+ return {
130
+ envelope_version: TASK_ENVELOPE_VERSION,
131
+ id: input.id,
132
+ workspace_id: input.workspace_id,
133
+ thread_id: input.thread_id,
134
+ trace_id: input.trace_id,
135
+ parent_id: input.parent_id,
136
+ state: input.initial_state ?? TaskStateEnum.Submitted,
137
+ history: [],
138
+ requester: input.requester,
139
+ assignee: input.assignee,
140
+ connector: input.connector,
141
+ payload: input.payload,
142
+ created_at: now,
143
+ updated_at: now,
144
+ metadata: input.metadata,
145
+ tags: input.tags
146
+ };
147
+ }
148
+ var ALLOWED_STATE_TRANSITIONS = {
149
+ [TaskStateEnum.Submitted]: [TaskStateEnum.Working, TaskStateEnum.Cancelled, TaskStateEnum.Failed],
150
+ [TaskStateEnum.Working]: [
151
+ TaskStateEnum.Completed,
152
+ TaskStateEnum.Failed,
153
+ TaskStateEnum.Cancelled,
154
+ TaskStateEnum.InputRequired
155
+ ],
156
+ [TaskStateEnum.InputRequired]: [TaskStateEnum.Working, TaskStateEnum.Cancelled, TaskStateEnum.Failed],
157
+ [TaskStateEnum.Completed]: [],
158
+ [TaskStateEnum.Failed]: [],
159
+ [TaskStateEnum.Cancelled]: []
160
+ };
161
+ function isValidTransition(from, to) {
162
+ return (ALLOWED_STATE_TRANSITIONS[from] ?? []).includes(to);
163
+ }
164
+ function transitionTaskEnvelope(envelope, to, options) {
165
+ if (!isValidTransition(envelope.state, to)) {
166
+ throw new Error(
167
+ `Invalid task state transition: ${envelope.state} -> ${to} (task ${envelope.id})`
168
+ );
169
+ }
170
+ const now = options?.now ?? (/* @__PURE__ */ new Date()).toISOString();
171
+ const transition = {
172
+ from: envelope.state,
173
+ to,
174
+ transitioned_at: now,
175
+ reason: options?.reason,
176
+ triggered_by: options?.triggered_by
177
+ };
178
+ return {
179
+ ...envelope,
180
+ state: to,
181
+ updated_at: now,
182
+ history: [...envelope.history ?? [], transition]
183
+ };
184
+ }
185
+
186
+ // ../../shared/src/a2a/schemas/audit-event.ts
187
+ var AUDIT_EVENT_VERSION = "1.0.0";
188
+ var AuditEventTypeEnum = {
189
+ // -- task lifecycle ---------------------------------------------------------
190
+ /** A new task was submitted. */
191
+ TaskSubmitted: "task.submitted",
192
+ /** Task execution started. */
193
+ TaskStarted: "task.started",
194
+ /** Task made progress (intermediate update). */
195
+ TaskProgress: "task.progress",
196
+ /** Assignee agent needs additional input. */
197
+ TaskInputRequired: "task.input_required",
198
+ /** Task completed successfully. */
199
+ TaskCompleted: "task.completed",
200
+ /** Task failed with an error. */
201
+ TaskFailed: "task.failed",
202
+ /** Task was cancelled. */
203
+ TaskCancelled: "task.cancelled",
204
+ // -- policy decisions -------------------------------------------------------
205
+ /** A policy check was performed. */
206
+ PolicyChecked: "policy.checked",
207
+ /** A policy action was approved (auto or prompt). */
208
+ PolicyApproved: "policy.approved",
209
+ /** A policy action was denied. */
210
+ PolicyDenied: "policy.denied",
211
+ // -- connector events -------------------------------------------------------
212
+ /** A connector was attached to an agent. */
213
+ ConnectorAttached: "connector.attached",
214
+ /** A connector was detached from an agent. */
215
+ ConnectorDetached: "connector.detached",
216
+ /** A connector emitted a diagnostic or health event. */
217
+ ConnectorHealth: "connector.health",
218
+ /** A connector encountered an error. */
219
+ ConnectorError: "connector.error",
220
+ // -- agent registry ---------------------------------------------------------
221
+ /** An agent was registered. */
222
+ AgentRegistered: "agent.registered",
223
+ /** An agent was unregistered. */
224
+ AgentUnregistered: "agent.unregistered",
225
+ /** An agent's capabilities were updated. */
226
+ AgentUpdated: "agent.updated",
227
+ // -- collaboration ----------------------------------------------------------
228
+ /** A collaboration channel was created. */
229
+ ChannelCreated: "collaboration.channel_created",
230
+ /** A collaboration thread was created. */
231
+ ThreadCreated: "collaboration.thread_created",
232
+ /** A message was sent in a collaboration thread. */
233
+ MessageSent: "collaboration.message_sent",
234
+ // -- system -----------------------------------------------------------------
235
+ /** A system-level configuration change. */
236
+ SystemConfig: "system.config",
237
+ /** A system-level error or warning. */
238
+ SystemError: "system.error"
239
+ };
240
+ var AUDIT_EVENT_TYPES = Object.values(AuditEventTypeEnum);
241
+ var AuditActorKindEnum = {
242
+ /** An AI agent. */
243
+ Agent: "agent",
244
+ /** A human user. */
245
+ User: "user",
246
+ /** An internal system service (e.g. policy engine, scheduler). */
247
+ System: "system",
248
+ /** A connector/adapter. */
249
+ Connector: "connector"
250
+ };
251
+ var AuditTargetKindEnum = {
252
+ Task: "task",
253
+ Agent: "agent",
254
+ Connector: "connector",
255
+ Channel: "channel",
256
+ Thread: "thread",
257
+ Message: "message",
258
+ Policy: "policy",
259
+ Workspace: "workspace",
260
+ System: "system"
261
+ };
262
+ var AuditSeverityEnum = {
263
+ /** Informational -- normal operation. */
264
+ Info: "info",
265
+ /** Warning -- something unexpected but not an error. */
266
+ Warning: "warning",
267
+ /** Error -- an operation failed. */
268
+ Error: "error",
269
+ /** Critical -- a severe failure requiring immediate attention. */
270
+ Critical: "critical"
271
+ };
272
+ function createAuditEvent(input) {
273
+ return {
274
+ schema_version: AUDIT_EVENT_VERSION,
275
+ id: input.id,
276
+ event_type: input.event_type,
277
+ severity: input.severity,
278
+ actor: input.actor,
279
+ target: input.target,
280
+ trace_id: input.trace_id,
281
+ parent_event_id: input.parent_event_id,
282
+ workspace_id: input.workspace_id,
283
+ thread_id: input.thread_id,
284
+ task_id: input.task_id,
285
+ timestamp: input.now ?? (/* @__PURE__ */ new Date()).toISOString(),
286
+ duration_ms: input.duration_ms,
287
+ policy: input.policy,
288
+ metadata: input.metadata,
289
+ tags: input.tags
290
+ };
291
+ }
292
+
293
+ // ../../shared/src/a2a/schemas/error-taxonomy.ts
294
+ var A2AErrorSeverityEnum = {
295
+ /** Low impact -- informational or gracefully handled. */
296
+ Low: "low",
297
+ /** Medium impact -- degraded functionality but system continues. */
298
+ Medium: "medium",
299
+ /** High impact -- a key operation failed and needs attention. */
300
+ High: "high",
301
+ /** Critical -- system stability at risk, immediate action required. */
302
+ Critical: "critical"
303
+ };
304
+ var A2AErrorCategoryEnum = {
305
+ /** Errors in the envelope or message format (validation, parsing). */
306
+ Envelope: "envelope",
307
+ /** Errors in the connector/adapter layer. */
308
+ Connector: "connector",
309
+ /** Errors in the policy/approval plane. */
310
+ Policy: "policy",
311
+ /** Errors in the task orchestration layer. */
312
+ Task: "task",
313
+ /** Errors in the agent registry. */
314
+ Registry: "registry",
315
+ /** Errors in the collaboration service. */
316
+ Collaboration: "collaboration",
317
+ /** Transport/network-level errors. */
318
+ Transport: "transport",
319
+ /** Authentication and authorization errors. */
320
+ Auth: "auth",
321
+ /** Rate-limiting and quota errors. */
322
+ RateLimit: "rate_limit",
323
+ /** Internal / unexpected errors. */
324
+ Internal: "internal"
325
+ };
326
+ var A2AErrorCodeEnum = {
327
+ // -- envelope ---------------------------------------------------------------
328
+ /** The envelope failed structural validation. */
329
+ InvalidEnvelope: "INVALID_ENVELOPE",
330
+ /** The envelope version is unsupported. */
331
+ UnsupportedEnvelopeVersion: "UNSUPPORTED_ENVELOPE_VERSION",
332
+ /** A required field is missing from the envelope. */
333
+ MissingEnvelopeField: "MISSING_ENVELOPE_FIELD",
334
+ /** The payload kind is unknown or malformed. */
335
+ InvalidPayload: "INVALID_PAYLOAD",
336
+ /** Compatibility alias used by older gateway/workspace routes. */
337
+ InvalidParams: "INVALID_ENVELOPE",
338
+ // -- connector --------------------------------------------------------------
339
+ /** No connector is attached for the target agent. */
340
+ ConnectorNotFound: "CONNECTOR_NOT_FOUND",
341
+ /** The connector timed out during task execution. */
342
+ ConnectorTimeout: "CONNECTOR_TIMEOUT",
343
+ /** The connector returned an unexpected error. */
344
+ ConnectorError: "CONNECTOR_ERROR",
345
+ /** The connector is temporarily unavailable. */
346
+ ConnectorUnavailable: "CONNECTOR_UNAVAILABLE",
347
+ /** Protocol mismatch between connector and remote agent. */
348
+ ProtocolMismatch: "PROTOCOL_MISMATCH",
349
+ // -- policy -----------------------------------------------------------------
350
+ /** The policy plane denied the requested action. */
351
+ PolicyDenied: "POLICY_DENIED",
352
+ /** No prompt handler is configured for the policy rule. */
353
+ PolicyHandlerMissing: "POLICY_HANDLER_MISSING",
354
+ /** The policy evaluation timed out. */
355
+ PolicyTimeout: "POLICY_TIMEOUT",
356
+ // -- task -------------------------------------------------------------------
357
+ /** The task was not found. */
358
+ TaskNotFound: "TASK_NOT_FOUND",
359
+ /** An invalid state transition was attempted. */
360
+ InvalidStateTransition: "INVALID_STATE_TRANSITION",
361
+ /** The task has already reached a terminal state. */
362
+ TaskAlreadyTerminal: "TASK_ALREADY_TERMINAL",
363
+ /** The task failed during execution. */
364
+ TaskExecutionFailed: "TASK_EXECUTION_FAILED",
365
+ /** The task was cancelled. */
366
+ TaskCancelled: "TASK_CANCELLED",
367
+ // -- registry ---------------------------------------------------------------
368
+ /** The agent was not found in the registry. */
369
+ AgentNotFound: "AGENT_NOT_FOUND",
370
+ /** The agent is already registered. */
371
+ AgentAlreadyRegistered: "AGENT_ALREADY_REGISTERED",
372
+ /** The agent does not have the requested capability. */
373
+ CapabilityNotFound: "CAPABILITY_NOT_FOUND",
374
+ // -- collaboration ----------------------------------------------------------
375
+ /** The channel was not found. */
376
+ ChannelNotFound: "CHANNEL_NOT_FOUND",
377
+ /** The thread was not found. */
378
+ ThreadNotFound: "THREAD_NOT_FOUND",
379
+ /** The message was not found. */
380
+ MessageNotFound: "MESSAGE_NOT_FOUND",
381
+ /** The sender is not a member of the target channel. */
382
+ NotChannelMember: "NOT_CHANNEL_MEMBER",
383
+ // -- transport --------------------------------------------------------------
384
+ /** A network request failed. */
385
+ NetworkError: "NETWORK_ERROR",
386
+ /** The remote endpoint returned an unexpected status. */
387
+ UnexpectedStatus: "UNEXPECTED_STATUS",
388
+ /** The request or response body could not be parsed. */
389
+ SerializationError: "SERIALIZATION_ERROR",
390
+ // -- auth -------------------------------------------------------------------
391
+ /** Authentication failed (invalid or missing credentials). */
392
+ AuthenticationFailed: "AUTHENTICATION_FAILED",
393
+ /** The caller is not authorized for the requested operation. */
394
+ AuthorizationDenied: "AUTHORIZATION_DENIED",
395
+ /** The access token has expired. */
396
+ TokenExpired: "TOKEN_EXPIRED",
397
+ // -- rate limit -------------------------------------------------------------
398
+ /** The caller has exceeded the rate limit. */
399
+ RateLimitExceeded: "RATE_LIMIT_EXCEEDED",
400
+ /** The caller has exceeded their usage quota. */
401
+ QuotaExceeded: "QUOTA_EXCEEDED",
402
+ // -- internal ---------------------------------------------------------------
403
+ /** An unexpected internal error occurred. */
404
+ InternalError: "INTERNAL_ERROR",
405
+ /** A configuration error was detected. */
406
+ ConfigurationError: "CONFIGURATION_ERROR"
407
+ };
408
+ var A2A_ERROR_CODE_REGISTRY = {
409
+ // envelope
410
+ [A2AErrorCodeEnum.InvalidEnvelope]: {
411
+ code: A2AErrorCodeEnum.InvalidEnvelope,
412
+ category: A2AErrorCategoryEnum.Envelope,
413
+ severity: A2AErrorSeverityEnum.Medium,
414
+ retryable: false,
415
+ description: "The envelope failed structural validation."
416
+ },
417
+ [A2AErrorCodeEnum.UnsupportedEnvelopeVersion]: {
418
+ code: A2AErrorCodeEnum.UnsupportedEnvelopeVersion,
419
+ category: A2AErrorCategoryEnum.Envelope,
420
+ severity: A2AErrorSeverityEnum.Medium,
421
+ retryable: false,
422
+ description: "The envelope version is not supported by this service."
423
+ },
424
+ [A2AErrorCodeEnum.MissingEnvelopeField]: {
425
+ code: A2AErrorCodeEnum.MissingEnvelopeField,
426
+ category: A2AErrorCategoryEnum.Envelope,
427
+ severity: A2AErrorSeverityEnum.Medium,
428
+ retryable: false,
429
+ description: "A required field is missing from the envelope."
430
+ },
431
+ [A2AErrorCodeEnum.InvalidPayload]: {
432
+ code: A2AErrorCodeEnum.InvalidPayload,
433
+ category: A2AErrorCategoryEnum.Envelope,
434
+ severity: A2AErrorSeverityEnum.Medium,
435
+ retryable: false,
436
+ description: "The payload kind is unknown or malformed."
437
+ },
438
+ // connector
439
+ [A2AErrorCodeEnum.ConnectorNotFound]: {
440
+ code: A2AErrorCodeEnum.ConnectorNotFound,
441
+ category: A2AErrorCategoryEnum.Connector,
442
+ severity: A2AErrorSeverityEnum.High,
443
+ retryable: false,
444
+ description: "No connector is attached for the target agent."
445
+ },
446
+ [A2AErrorCodeEnum.ConnectorTimeout]: {
447
+ code: A2AErrorCodeEnum.ConnectorTimeout,
448
+ category: A2AErrorCategoryEnum.Connector,
449
+ severity: A2AErrorSeverityEnum.High,
450
+ retryable: true,
451
+ description: "The connector timed out during task execution."
452
+ },
453
+ [A2AErrorCodeEnum.ConnectorError]: {
454
+ code: A2AErrorCodeEnum.ConnectorError,
455
+ category: A2AErrorCategoryEnum.Connector,
456
+ severity: A2AErrorSeverityEnum.High,
457
+ retryable: true,
458
+ description: "The connector returned an unexpected error."
459
+ },
460
+ [A2AErrorCodeEnum.ConnectorUnavailable]: {
461
+ code: A2AErrorCodeEnum.ConnectorUnavailable,
462
+ category: A2AErrorCategoryEnum.Connector,
463
+ severity: A2AErrorSeverityEnum.High,
464
+ retryable: true,
465
+ description: "The connector is temporarily unavailable."
466
+ },
467
+ [A2AErrorCodeEnum.ProtocolMismatch]: {
468
+ code: A2AErrorCodeEnum.ProtocolMismatch,
469
+ category: A2AErrorCategoryEnum.Connector,
470
+ severity: A2AErrorSeverityEnum.High,
471
+ retryable: false,
472
+ description: "Protocol mismatch between connector and remote agent."
473
+ },
474
+ // policy
475
+ [A2AErrorCodeEnum.PolicyDenied]: {
476
+ code: A2AErrorCodeEnum.PolicyDenied,
477
+ category: A2AErrorCategoryEnum.Policy,
478
+ severity: A2AErrorSeverityEnum.Medium,
479
+ retryable: false,
480
+ description: "The policy plane denied the requested action."
481
+ },
482
+ [A2AErrorCodeEnum.PolicyHandlerMissing]: {
483
+ code: A2AErrorCodeEnum.PolicyHandlerMissing,
484
+ category: A2AErrorCategoryEnum.Policy,
485
+ severity: A2AErrorSeverityEnum.Medium,
486
+ retryable: false,
487
+ description: "No prompt handler is configured for the policy rule."
488
+ },
489
+ [A2AErrorCodeEnum.PolicyTimeout]: {
490
+ code: A2AErrorCodeEnum.PolicyTimeout,
491
+ category: A2AErrorCategoryEnum.Policy,
492
+ severity: A2AErrorSeverityEnum.High,
493
+ retryable: true,
494
+ description: "The policy evaluation timed out."
495
+ },
496
+ // task
497
+ [A2AErrorCodeEnum.TaskNotFound]: {
498
+ code: A2AErrorCodeEnum.TaskNotFound,
499
+ category: A2AErrorCategoryEnum.Task,
500
+ severity: A2AErrorSeverityEnum.Medium,
501
+ retryable: false,
502
+ description: "The task was not found."
503
+ },
504
+ [A2AErrorCodeEnum.InvalidStateTransition]: {
505
+ code: A2AErrorCodeEnum.InvalidStateTransition,
506
+ category: A2AErrorCategoryEnum.Task,
507
+ severity: A2AErrorSeverityEnum.Medium,
508
+ retryable: false,
509
+ description: "An invalid state transition was attempted."
510
+ },
511
+ [A2AErrorCodeEnum.TaskAlreadyTerminal]: {
512
+ code: A2AErrorCodeEnum.TaskAlreadyTerminal,
513
+ category: A2AErrorCategoryEnum.Task,
514
+ severity: A2AErrorSeverityEnum.Low,
515
+ retryable: false,
516
+ description: "The task has already reached a terminal state."
517
+ },
518
+ [A2AErrorCodeEnum.TaskExecutionFailed]: {
519
+ code: A2AErrorCodeEnum.TaskExecutionFailed,
520
+ category: A2AErrorCategoryEnum.Task,
521
+ severity: A2AErrorSeverityEnum.High,
522
+ retryable: true,
523
+ description: "The task failed during execution."
524
+ },
525
+ [A2AErrorCodeEnum.TaskCancelled]: {
526
+ code: A2AErrorCodeEnum.TaskCancelled,
527
+ category: A2AErrorCategoryEnum.Task,
528
+ severity: A2AErrorSeverityEnum.Low,
529
+ retryable: false,
530
+ description: "The task was cancelled."
531
+ },
532
+ // registry
533
+ [A2AErrorCodeEnum.AgentNotFound]: {
534
+ code: A2AErrorCodeEnum.AgentNotFound,
535
+ category: A2AErrorCategoryEnum.Registry,
536
+ severity: A2AErrorSeverityEnum.Medium,
537
+ retryable: false,
538
+ description: "The agent was not found in the registry."
539
+ },
540
+ [A2AErrorCodeEnum.AgentAlreadyRegistered]: {
541
+ code: A2AErrorCodeEnum.AgentAlreadyRegistered,
542
+ category: A2AErrorCategoryEnum.Registry,
543
+ severity: A2AErrorSeverityEnum.Low,
544
+ retryable: false,
545
+ description: "The agent is already registered."
546
+ },
547
+ [A2AErrorCodeEnum.CapabilityNotFound]: {
548
+ code: A2AErrorCodeEnum.CapabilityNotFound,
549
+ category: A2AErrorCategoryEnum.Registry,
550
+ severity: A2AErrorSeverityEnum.Medium,
551
+ retryable: false,
552
+ description: "The agent does not have the requested capability."
553
+ },
554
+ // collaboration
555
+ [A2AErrorCodeEnum.ChannelNotFound]: {
556
+ code: A2AErrorCodeEnum.ChannelNotFound,
557
+ category: A2AErrorCategoryEnum.Collaboration,
558
+ severity: A2AErrorSeverityEnum.Medium,
559
+ retryable: false,
560
+ description: "The channel was not found."
561
+ },
562
+ [A2AErrorCodeEnum.ThreadNotFound]: {
563
+ code: A2AErrorCodeEnum.ThreadNotFound,
564
+ category: A2AErrorCategoryEnum.Collaboration,
565
+ severity: A2AErrorSeverityEnum.Medium,
566
+ retryable: false,
567
+ description: "The thread was not found."
568
+ },
569
+ [A2AErrorCodeEnum.MessageNotFound]: {
570
+ code: A2AErrorCodeEnum.MessageNotFound,
571
+ category: A2AErrorCategoryEnum.Collaboration,
572
+ severity: A2AErrorSeverityEnum.Medium,
573
+ retryable: false,
574
+ description: "The message was not found."
575
+ },
576
+ [A2AErrorCodeEnum.NotChannelMember]: {
577
+ code: A2AErrorCodeEnum.NotChannelMember,
578
+ category: A2AErrorCategoryEnum.Collaboration,
579
+ severity: A2AErrorSeverityEnum.Medium,
580
+ retryable: false,
581
+ description: "The sender is not a member of the target channel."
582
+ },
583
+ // transport
584
+ [A2AErrorCodeEnum.NetworkError]: {
585
+ code: A2AErrorCodeEnum.NetworkError,
586
+ category: A2AErrorCategoryEnum.Transport,
587
+ severity: A2AErrorSeverityEnum.High,
588
+ retryable: true,
589
+ description: "A network request failed."
590
+ },
591
+ [A2AErrorCodeEnum.UnexpectedStatus]: {
592
+ code: A2AErrorCodeEnum.UnexpectedStatus,
593
+ category: A2AErrorCategoryEnum.Transport,
594
+ severity: A2AErrorSeverityEnum.Medium,
595
+ retryable: true,
596
+ description: "The remote endpoint returned an unexpected status."
597
+ },
598
+ [A2AErrorCodeEnum.SerializationError]: {
599
+ code: A2AErrorCodeEnum.SerializationError,
600
+ category: A2AErrorCategoryEnum.Transport,
601
+ severity: A2AErrorSeverityEnum.Medium,
602
+ retryable: false,
603
+ description: "The request or response body could not be parsed."
604
+ },
605
+ // auth
606
+ [A2AErrorCodeEnum.AuthenticationFailed]: {
607
+ code: A2AErrorCodeEnum.AuthenticationFailed,
608
+ category: A2AErrorCategoryEnum.Auth,
609
+ severity: A2AErrorSeverityEnum.High,
610
+ retryable: false,
611
+ description: "Authentication failed (invalid or missing credentials)."
612
+ },
613
+ [A2AErrorCodeEnum.AuthorizationDenied]: {
614
+ code: A2AErrorCodeEnum.AuthorizationDenied,
615
+ category: A2AErrorCategoryEnum.Auth,
616
+ severity: A2AErrorSeverityEnum.Medium,
617
+ retryable: false,
618
+ description: "The caller is not authorized for the requested operation."
619
+ },
620
+ [A2AErrorCodeEnum.TokenExpired]: {
621
+ code: A2AErrorCodeEnum.TokenExpired,
622
+ category: A2AErrorCategoryEnum.Auth,
623
+ severity: A2AErrorSeverityEnum.Medium,
624
+ retryable: true,
625
+ description: "The access token has expired."
626
+ },
627
+ // rate limit
628
+ [A2AErrorCodeEnum.RateLimitExceeded]: {
629
+ code: A2AErrorCodeEnum.RateLimitExceeded,
630
+ category: A2AErrorCategoryEnum.RateLimit,
631
+ severity: A2AErrorSeverityEnum.Medium,
632
+ retryable: true,
633
+ description: "The caller has exceeded the rate limit."
634
+ },
635
+ [A2AErrorCodeEnum.QuotaExceeded]: {
636
+ code: A2AErrorCodeEnum.QuotaExceeded,
637
+ category: A2AErrorCategoryEnum.RateLimit,
638
+ severity: A2AErrorSeverityEnum.Medium,
639
+ retryable: false,
640
+ description: "The caller has exceeded their usage quota."
641
+ },
642
+ // internal
643
+ [A2AErrorCodeEnum.InternalError]: {
644
+ code: A2AErrorCodeEnum.InternalError,
645
+ category: A2AErrorCategoryEnum.Internal,
646
+ severity: A2AErrorSeverityEnum.Critical,
647
+ retryable: false,
648
+ description: "An unexpected internal error occurred."
649
+ },
650
+ [A2AErrorCodeEnum.ConfigurationError]: {
651
+ code: A2AErrorCodeEnum.ConfigurationError,
652
+ category: A2AErrorCategoryEnum.Internal,
653
+ severity: A2AErrorSeverityEnum.High,
654
+ retryable: false,
655
+ description: "A configuration error was detected."
656
+ }
657
+ };
658
+ function buildA2AError(input) {
659
+ const meta = A2A_ERROR_CODE_REGISTRY[input.code];
660
+ return {
661
+ code: input.code,
662
+ category: meta.category,
663
+ message: input.message,
664
+ severity: input.severity ?? meta.severity,
665
+ retryable: input.retryable ?? meta.retryable,
666
+ details: input.details,
667
+ timestamp: input.now ?? (/* @__PURE__ */ new Date()).toISOString(),
668
+ trace_id: input.trace_id
669
+ };
670
+ }
671
+ function getErrorCodeMeta(code) {
672
+ return A2A_ERROR_CODE_REGISTRY[code];
673
+ }
674
+ function isA2AErrorCode(code) {
675
+ return code in A2A_ERROR_CODE_REGISTRY;
676
+ }
677
+ var A2AStructuredError = class _A2AStructuredError extends Error {
678
+ /** The structured error payload. */
679
+ a2aError;
680
+ constructor(a2aError) {
681
+ super(`[${a2aError.code}] ${a2aError.message}`);
682
+ this.name = "A2AStructuredError";
683
+ this.a2aError = a2aError;
684
+ Object.setPrototypeOf(this, _A2AStructuredError.prototype);
685
+ }
686
+ /** Convenience getter for the error code. */
687
+ get code() {
688
+ return this.a2aError.code;
689
+ }
690
+ /** Convenience getter for the error category. */
691
+ get category() {
692
+ return this.a2aError.category;
693
+ }
694
+ /** Convenience getter for the severity. */
695
+ get severity() {
696
+ return this.a2aError.severity;
697
+ }
698
+ /** Whether this error is retryable. */
699
+ get retryable() {
700
+ return this.a2aError.retryable;
701
+ }
702
+ };
703
+
704
+ // src/state-mapping.ts
705
+ var CODEX_TO_CANONICAL = {
706
+ queued: TaskStateEnum.Submitted,
707
+ running: TaskStateEnum.Working,
708
+ waiting_for_input: TaskStateEnum.InputRequired,
709
+ completed: TaskStateEnum.Completed,
710
+ failed: TaskStateEnum.Failed,
711
+ error: TaskStateEnum.Failed,
712
+ cancelled: TaskStateEnum.Cancelled,
713
+ timeout: TaskStateEnum.Failed
714
+ };
715
+ var CANONICAL_TO_CODEX = {
716
+ [TaskStateEnum.Submitted]: "queued",
717
+ [TaskStateEnum.Working]: "running",
718
+ [TaskStateEnum.InputRequired]: "waiting_for_input",
719
+ [TaskStateEnum.Completed]: "completed",
720
+ [TaskStateEnum.Failed]: "failed",
721
+ [TaskStateEnum.Cancelled]: "cancelled"
722
+ };
723
+ function mapCodexState(nativeState) {
724
+ return CODEX_TO_CANONICAL[nativeState];
725
+ }
726
+ function mapCodexStateStrict(nativeState) {
727
+ const mapped = mapCodexState(nativeState);
728
+ if (mapped === void 0) {
729
+ throw new Error(`Unknown Codex state: "${nativeState}"`);
730
+ }
731
+ return mapped;
732
+ }
733
+ function canonicalToCodexState(canonicalState) {
734
+ return CANONICAL_TO_CODEX[canonicalState];
735
+ }
736
+ var CLAUDE_TO_CANONICAL = {
737
+ pending: TaskStateEnum.Submitted,
738
+ streaming: TaskStateEnum.Working,
739
+ processing: TaskStateEnum.Working,
740
+ waiting_for_approval: TaskStateEnum.InputRequired,
741
+ waiting_for_input: TaskStateEnum.InputRequired,
742
+ done: TaskStateEnum.Completed,
743
+ completed: TaskStateEnum.Completed,
744
+ error: TaskStateEnum.Failed,
745
+ failed: TaskStateEnum.Failed,
746
+ aborted: TaskStateEnum.Cancelled,
747
+ cancelled: TaskStateEnum.Cancelled,
748
+ timeout: TaskStateEnum.Failed
749
+ };
750
+ var CANONICAL_TO_CLAUDE = {
751
+ [TaskStateEnum.Submitted]: "pending",
752
+ [TaskStateEnum.Working]: "streaming",
753
+ [TaskStateEnum.InputRequired]: "waiting_for_input",
754
+ [TaskStateEnum.Completed]: "done",
755
+ [TaskStateEnum.Failed]: "error",
756
+ [TaskStateEnum.Cancelled]: "aborted"
757
+ };
758
+ function mapClaudeState(nativeState) {
759
+ return CLAUDE_TO_CANONICAL[nativeState];
760
+ }
761
+ function mapClaudeStateStrict(nativeState) {
762
+ const mapped = mapClaudeState(nativeState);
763
+ if (mapped === void 0) {
764
+ throw new Error(`Unknown Claude Code state: "${nativeState}"`);
765
+ }
766
+ return mapped;
767
+ }
768
+ function canonicalToClaudeState(canonicalState) {
769
+ return CANONICAL_TO_CLAUDE[canonicalState];
770
+ }
771
+ function mapConnectorState(platform, nativeState) {
772
+ switch (platform) {
773
+ case "codex":
774
+ return mapCodexState(nativeState);
775
+ case "claude_code":
776
+ return mapClaudeState(nativeState);
777
+ default:
778
+ return void 0;
779
+ }
780
+ }
781
+ function mapConnectorStateStrict(platform, nativeState) {
782
+ const mapped = mapConnectorState(platform, nativeState);
783
+ if (mapped === void 0) {
784
+ throw new Error(
785
+ `Cannot map state "${nativeState}" for platform "${platform}"`
786
+ );
787
+ }
788
+ return mapped;
789
+ }
790
+ function canonicalToConnectorState(platform, canonicalState) {
791
+ switch (platform) {
792
+ case "codex":
793
+ return canonicalToCodexState(canonicalState);
794
+ case "claude_code":
795
+ return canonicalToClaudeState(canonicalState);
796
+ default:
797
+ return void 0;
798
+ }
799
+ }
800
+ function checkConnectorTransition(currentCanonical, platform, newNativeState) {
801
+ const mappedState = mapConnectorState(platform, newNativeState);
802
+ if (mappedState === void 0) {
803
+ return {
804
+ valid: false,
805
+ mappedState: void 0,
806
+ error: `Unknown ${platform} state: "${newNativeState}"`
807
+ };
808
+ }
809
+ if (mappedState === currentCanonical) {
810
+ return { valid: true, mappedState };
811
+ }
812
+ if (!isValidTransition(currentCanonical, mappedState)) {
813
+ return {
814
+ valid: false,
815
+ mappedState,
816
+ error: `Invalid transition: ${currentCanonical} -> ${mappedState} (from ${platform} state "${newNativeState}")`
817
+ };
818
+ }
819
+ return { valid: true, mappedState };
820
+ }
821
+ function isConnectorTerminalState(platform, nativeState) {
822
+ const mapped = mapConnectorState(platform, nativeState);
823
+ return mapped !== void 0 && isTerminalState(mapped);
824
+ }
825
+
826
+ // src/error-mapping.ts
827
+ var CODEX_ERROR_MAP = {
828
+ timeout: A2AErrorCodeEnum.ConnectorTimeout,
829
+ rate_limit: A2AErrorCodeEnum.RateLimitExceeded,
830
+ quota_exceeded: A2AErrorCodeEnum.QuotaExceeded,
831
+ auth_error: A2AErrorCodeEnum.AuthenticationFailed,
832
+ connection_error: A2AErrorCodeEnum.NetworkError,
833
+ invalid_request: A2AErrorCodeEnum.InvalidPayload,
834
+ execution_error: A2AErrorCodeEnum.TaskExecutionFailed,
835
+ model_error: A2AErrorCodeEnum.ConnectorError,
836
+ internal_error: A2AErrorCodeEnum.InternalError,
837
+ cancelled: A2AErrorCodeEnum.TaskCancelled
838
+ };
839
+ function mapCodexError(input) {
840
+ const code = CODEX_ERROR_MAP[input.type] ?? A2AErrorCodeEnum.ConnectorError;
841
+ return buildA2AError({
842
+ code,
843
+ message: input.message,
844
+ details: {
845
+ ...input.details,
846
+ connector_platform: "codex",
847
+ original_error_type: input.type,
848
+ ...input.statusCode !== void 0 ? { status_code: input.statusCode } : {}
849
+ },
850
+ trace_id: input.traceId
851
+ });
852
+ }
853
+ var CLAUDE_ERROR_MAP = {
854
+ overloaded: A2AErrorCodeEnum.ConnectorUnavailable,
855
+ rate_limit: A2AErrorCodeEnum.RateLimitExceeded,
856
+ authentication_error: A2AErrorCodeEnum.AuthenticationFailed,
857
+ permission_error: A2AErrorCodeEnum.AuthorizationDenied,
858
+ invalid_request: A2AErrorCodeEnum.InvalidPayload,
859
+ api_error: A2AErrorCodeEnum.ConnectorError,
860
+ timeout: A2AErrorCodeEnum.ConnectorTimeout,
861
+ connection_error: A2AErrorCodeEnum.NetworkError,
862
+ tool_execution_error: A2AErrorCodeEnum.TaskExecutionFailed,
863
+ context_length_exceeded: A2AErrorCodeEnum.QuotaExceeded,
864
+ cancelled: A2AErrorCodeEnum.TaskCancelled,
865
+ aborted: A2AErrorCodeEnum.TaskCancelled
866
+ };
867
+ function mapClaudeError(input) {
868
+ const code = CLAUDE_ERROR_MAP[input.type] ?? A2AErrorCodeEnum.ConnectorError;
869
+ return buildA2AError({
870
+ code,
871
+ message: input.message,
872
+ details: {
873
+ ...input.details,
874
+ connector_platform: "claude_code",
875
+ original_error_type: input.type,
876
+ ...input.statusCode !== void 0 ? { status_code: input.statusCode } : {}
877
+ },
878
+ trace_id: input.traceId
879
+ });
880
+ }
881
+ var HTTP_STATUS_MAP = {
882
+ 400: A2AErrorCodeEnum.InvalidPayload,
883
+ 401: A2AErrorCodeEnum.AuthenticationFailed,
884
+ 403: A2AErrorCodeEnum.AuthorizationDenied,
885
+ 404: A2AErrorCodeEnum.ConnectorNotFound,
886
+ 408: A2AErrorCodeEnum.ConnectorTimeout,
887
+ 409: A2AErrorCodeEnum.InvalidStateTransition,
888
+ 422: A2AErrorCodeEnum.InvalidEnvelope,
889
+ 429: A2AErrorCodeEnum.RateLimitExceeded,
890
+ 500: A2AErrorCodeEnum.InternalError,
891
+ 502: A2AErrorCodeEnum.NetworkError,
892
+ 503: A2AErrorCodeEnum.ConnectorUnavailable,
893
+ 504: A2AErrorCodeEnum.ConnectorTimeout
894
+ };
895
+ function mapHttpError(statusCode, message, options) {
896
+ const code = HTTP_STATUS_MAP[statusCode] ?? A2AErrorCodeEnum.UnexpectedStatus;
897
+ return buildA2AError({
898
+ code,
899
+ message,
900
+ details: {
901
+ ...options?.details,
902
+ http_status: statusCode,
903
+ ...options?.platform ? { connector_platform: options.platform } : {}
904
+ },
905
+ trace_id: options?.traceId
906
+ });
907
+ }
908
+ function mapConnectorError(input) {
909
+ switch (input.platform) {
910
+ case "codex":
911
+ return mapCodexError(input);
912
+ case "claude_code":
913
+ return mapClaudeError(input);
914
+ default:
915
+ if (input.statusCode !== void 0) {
916
+ return mapHttpError(input.statusCode, input.message, {
917
+ traceId: input.traceId,
918
+ details: { ...input.details, original_error_type: input.type },
919
+ platform: input.platform
920
+ });
921
+ }
922
+ return buildA2AError({
923
+ code: A2AErrorCodeEnum.ConnectorError,
924
+ message: input.message,
925
+ details: {
926
+ ...input.details,
927
+ connector_platform: input.platform,
928
+ original_error_type: input.type
929
+ },
930
+ trace_id: input.traceId
931
+ });
932
+ }
933
+ }
934
+ function wrapUnknownError(err, options) {
935
+ const message = err instanceof Error ? err.message : typeof err === "string" ? err : "Unknown error";
936
+ const details = {};
937
+ if (options?.platform) {
938
+ details.connector_platform = options.platform;
939
+ }
940
+ if (err instanceof Error) {
941
+ details.error_name = err.name;
942
+ if (err.stack) {
943
+ details.stack_preview = err.stack.split("\n").slice(0, 3).join("\n");
944
+ }
945
+ }
946
+ return buildA2AError({
947
+ code: A2AErrorCodeEnum.InternalError,
948
+ message,
949
+ details,
950
+ trace_id: options?.traceId
951
+ });
952
+ }
953
+
954
+ // src/envelope-builders.ts
955
+ function buildCodexEnvelope(input, options) {
956
+ const connectorMeta = {
957
+ ...options?.connector_metadata
958
+ };
959
+ if (options?.model) connectorMeta.model = options.model;
960
+ if (options?.endpoint) connectorMeta.endpoint = options.endpoint;
961
+ const connector = {
962
+ platform: "codex",
963
+ protocol: "jsonrpc",
964
+ connector_id: options?.connector_id ?? "codex-v1",
965
+ name: "Codex Adapter",
966
+ metadata: Object.keys(connectorMeta).length > 0 ? connectorMeta : void 0
967
+ };
968
+ const assignee = input.assignee ?? {
969
+ agent_id: `codex-agent-${input.workspace_id}`,
970
+ platform: "codex",
971
+ name: "Codex Agent"
972
+ };
973
+ return createTaskEnvelope({
974
+ id: input.id,
975
+ workspace_id: input.workspace_id,
976
+ thread_id: input.thread_id,
977
+ trace_id: input.trace_id,
978
+ parent_id: input.parent_id,
979
+ requester: input.requester,
980
+ assignee,
981
+ connector,
982
+ payload: input.payload,
983
+ metadata: input.metadata,
984
+ tags: input.tags,
985
+ initial_state: input.initial_state,
986
+ now: input.now
987
+ });
988
+ }
989
+ function buildClaudeEnvelope(input, options) {
990
+ const connectorMeta = {
991
+ ...options?.connector_metadata
992
+ };
993
+ if (options?.model) connectorMeta.model = options.model;
994
+ if (options?.endpoint) connectorMeta.endpoint = options.endpoint;
995
+ if (options?.max_tokens !== void 0) connectorMeta.max_tokens = options.max_tokens;
996
+ const connector = {
997
+ platform: "claude_code",
998
+ protocol: "jsonrpc",
999
+ connector_id: options?.connector_id ?? "claude-code-v1",
1000
+ name: "Claude Code Adapter",
1001
+ metadata: Object.keys(connectorMeta).length > 0 ? connectorMeta : void 0
1002
+ };
1003
+ const assignee = input.assignee ?? {
1004
+ agent_id: `claude-agent-${input.workspace_id}`,
1005
+ platform: "claude_code",
1006
+ name: "Claude Code Agent"
1007
+ };
1008
+ return createTaskEnvelope({
1009
+ id: input.id,
1010
+ workspace_id: input.workspace_id,
1011
+ thread_id: input.thread_id,
1012
+ trace_id: input.trace_id,
1013
+ parent_id: input.parent_id,
1014
+ requester: input.requester,
1015
+ assignee,
1016
+ connector,
1017
+ payload: input.payload,
1018
+ metadata: input.metadata,
1019
+ tags: input.tags,
1020
+ initial_state: input.initial_state,
1021
+ now: input.now
1022
+ });
1023
+ }
1024
+ function completeEnvelope(envelope, result, options) {
1025
+ const transitioned = transitionTaskEnvelope(envelope, TaskStateEnum.Completed, options);
1026
+ return {
1027
+ ...transitioned,
1028
+ result
1029
+ };
1030
+ }
1031
+ function failEnvelope(envelope, error, options) {
1032
+ const transitioned = transitionTaskEnvelope(envelope, TaskStateEnum.Failed, options);
1033
+ return {
1034
+ ...transitioned,
1035
+ error
1036
+ };
1037
+ }
1038
+ function cancelEnvelope(envelope, options) {
1039
+ return transitionTaskEnvelope(envelope, TaskStateEnum.Cancelled, options);
1040
+ }
1041
+ function startEnvelope(envelope, options) {
1042
+ return transitionTaskEnvelope(envelope, TaskStateEnum.Working, options);
1043
+ }
1044
+ function a2aErrorToTaskErrorInfo(a2aError) {
1045
+ return {
1046
+ code: a2aError.code,
1047
+ category: a2aError.category,
1048
+ message: a2aError.message,
1049
+ severity: a2aError.severity,
1050
+ retryable: a2aError.retryable,
1051
+ details: a2aError.details
1052
+ };
1053
+ }
1054
+
1055
+ // src/audit-helpers.ts
1056
+ function agentActor(agentId, platform, name) {
1057
+ return {
1058
+ kind: AuditActorKindEnum.Agent,
1059
+ id: agentId,
1060
+ platform,
1061
+ name
1062
+ };
1063
+ }
1064
+ function connectorActor(connectorId, platform, name) {
1065
+ return {
1066
+ kind: AuditActorKindEnum.Connector,
1067
+ id: connectorId,
1068
+ platform,
1069
+ name
1070
+ };
1071
+ }
1072
+ function systemActor(systemId = "control-plane") {
1073
+ return {
1074
+ kind: AuditActorKindEnum.System,
1075
+ id: systemId,
1076
+ name: "A2A Control Plane"
1077
+ };
1078
+ }
1079
+ function taskTarget(taskId) {
1080
+ return {
1081
+ kind: AuditTargetKindEnum.Task,
1082
+ id: taskId
1083
+ };
1084
+ }
1085
+ function connectorTarget(connectorId, name) {
1086
+ return {
1087
+ kind: AuditTargetKindEnum.Connector,
1088
+ id: connectorId,
1089
+ name
1090
+ };
1091
+ }
1092
+ function emitTaskSubmitted(input) {
1093
+ return createAuditEvent({
1094
+ id: input.id,
1095
+ event_type: AuditEventTypeEnum.TaskSubmitted,
1096
+ severity: AuditSeverityEnum.Info,
1097
+ actor: agentActor(input.actor_agent_id, input.actor_platform, input.actor_name),
1098
+ target: taskTarget(input.task_id),
1099
+ trace_id: input.trace_id,
1100
+ workspace_id: input.workspace_id,
1101
+ thread_id: input.thread_id,
1102
+ task_id: input.task_id,
1103
+ parent_event_id: input.parent_event_id,
1104
+ metadata: input.metadata,
1105
+ tags: input.tags,
1106
+ now: input.now
1107
+ });
1108
+ }
1109
+ function emitTaskStarted(input) {
1110
+ return createAuditEvent({
1111
+ id: input.id,
1112
+ event_type: AuditEventTypeEnum.TaskStarted,
1113
+ severity: AuditSeverityEnum.Info,
1114
+ actor: agentActor(input.actor_agent_id, input.actor_platform, input.actor_name),
1115
+ target: taskTarget(input.task_id),
1116
+ trace_id: input.trace_id,
1117
+ workspace_id: input.workspace_id,
1118
+ thread_id: input.thread_id,
1119
+ task_id: input.task_id,
1120
+ parent_event_id: input.parent_event_id,
1121
+ metadata: input.metadata,
1122
+ tags: input.tags,
1123
+ now: input.now
1124
+ });
1125
+ }
1126
+ function emitTaskProgress(input) {
1127
+ const metadata = { ...input.metadata };
1128
+ if (input.progress_pct !== void 0) {
1129
+ metadata.progress_pct = input.progress_pct;
1130
+ }
1131
+ return createAuditEvent({
1132
+ id: input.id,
1133
+ event_type: AuditEventTypeEnum.TaskProgress,
1134
+ severity: AuditSeverityEnum.Info,
1135
+ actor: agentActor(input.actor_agent_id, input.actor_platform, input.actor_name),
1136
+ target: taskTarget(input.task_id),
1137
+ trace_id: input.trace_id,
1138
+ workspace_id: input.workspace_id,
1139
+ thread_id: input.thread_id,
1140
+ task_id: input.task_id,
1141
+ parent_event_id: input.parent_event_id,
1142
+ metadata,
1143
+ tags: input.tags,
1144
+ now: input.now
1145
+ });
1146
+ }
1147
+ function emitTaskInputRequired(input) {
1148
+ return createAuditEvent({
1149
+ id: input.id,
1150
+ event_type: AuditEventTypeEnum.TaskInputRequired,
1151
+ severity: AuditSeverityEnum.Info,
1152
+ actor: agentActor(input.actor_agent_id, input.actor_platform, input.actor_name),
1153
+ target: taskTarget(input.task_id),
1154
+ trace_id: input.trace_id,
1155
+ workspace_id: input.workspace_id,
1156
+ thread_id: input.thread_id,
1157
+ task_id: input.task_id,
1158
+ parent_event_id: input.parent_event_id,
1159
+ metadata: input.metadata,
1160
+ tags: input.tags,
1161
+ now: input.now
1162
+ });
1163
+ }
1164
+ function emitTaskCompleted(input) {
1165
+ return createAuditEvent({
1166
+ id: input.id,
1167
+ event_type: AuditEventTypeEnum.TaskCompleted,
1168
+ severity: AuditSeverityEnum.Info,
1169
+ actor: agentActor(input.actor_agent_id, input.actor_platform, input.actor_name),
1170
+ target: taskTarget(input.task_id),
1171
+ trace_id: input.trace_id,
1172
+ workspace_id: input.workspace_id,
1173
+ thread_id: input.thread_id,
1174
+ task_id: input.task_id,
1175
+ parent_event_id: input.parent_event_id,
1176
+ duration_ms: input.duration_ms,
1177
+ metadata: input.metadata,
1178
+ tags: input.tags,
1179
+ now: input.now
1180
+ });
1181
+ }
1182
+ function emitTaskFailed(input) {
1183
+ const metadata = { ...input.metadata };
1184
+ if (input.error_code) metadata.error_code = input.error_code;
1185
+ if (input.error_message) metadata.error_message = input.error_message;
1186
+ return createAuditEvent({
1187
+ id: input.id,
1188
+ event_type: AuditEventTypeEnum.TaskFailed,
1189
+ severity: AuditSeverityEnum.Error,
1190
+ actor: agentActor(input.actor_agent_id, input.actor_platform, input.actor_name),
1191
+ target: taskTarget(input.task_id),
1192
+ trace_id: input.trace_id,
1193
+ workspace_id: input.workspace_id,
1194
+ thread_id: input.thread_id,
1195
+ task_id: input.task_id,
1196
+ parent_event_id: input.parent_event_id,
1197
+ duration_ms: input.duration_ms,
1198
+ metadata,
1199
+ tags: input.tags,
1200
+ now: input.now
1201
+ });
1202
+ }
1203
+ function emitTaskCancelled(input) {
1204
+ const metadata = { ...input.metadata };
1205
+ if (input.reason) metadata.cancellation_reason = input.reason;
1206
+ return createAuditEvent({
1207
+ id: input.id,
1208
+ event_type: AuditEventTypeEnum.TaskCancelled,
1209
+ severity: AuditSeverityEnum.Info,
1210
+ actor: agentActor(input.actor_agent_id, input.actor_platform, input.actor_name),
1211
+ target: taskTarget(input.task_id),
1212
+ trace_id: input.trace_id,
1213
+ workspace_id: input.workspace_id,
1214
+ thread_id: input.thread_id,
1215
+ task_id: input.task_id,
1216
+ parent_event_id: input.parent_event_id,
1217
+ metadata,
1218
+ tags: input.tags,
1219
+ now: input.now
1220
+ });
1221
+ }
1222
+ function emitConnectorAttached(input) {
1223
+ return createAuditEvent({
1224
+ id: input.id,
1225
+ event_type: AuditEventTypeEnum.ConnectorAttached,
1226
+ severity: AuditSeverityEnum.Info,
1227
+ actor: systemActor(),
1228
+ target: connectorTarget(input.connector_id, input.connector_name),
1229
+ trace_id: input.trace_id,
1230
+ workspace_id: input.workspace_id,
1231
+ thread_id: input.thread_id,
1232
+ task_id: input.task_id,
1233
+ parent_event_id: input.parent_event_id,
1234
+ metadata: {
1235
+ ...input.metadata,
1236
+ connector_platform: input.connector_platform
1237
+ },
1238
+ tags: input.tags,
1239
+ now: input.now
1240
+ });
1241
+ }
1242
+ function emitConnectorHealth(input) {
1243
+ return createAuditEvent({
1244
+ id: input.id,
1245
+ event_type: AuditEventTypeEnum.ConnectorHealth,
1246
+ severity: input.healthy ? AuditSeverityEnum.Info : AuditSeverityEnum.Warning,
1247
+ actor: connectorActor(input.connector_id, input.connector_platform, input.connector_name),
1248
+ target: connectorTarget(input.connector_id, input.connector_name),
1249
+ trace_id: input.trace_id,
1250
+ workspace_id: input.workspace_id,
1251
+ thread_id: input.thread_id,
1252
+ task_id: input.task_id,
1253
+ parent_event_id: input.parent_event_id,
1254
+ metadata: {
1255
+ ...input.metadata,
1256
+ healthy: input.healthy,
1257
+ ...input.latency_ms !== void 0 ? { latency_ms: input.latency_ms } : {}
1258
+ },
1259
+ tags: input.tags,
1260
+ now: input.now
1261
+ });
1262
+ }
1263
+ function emitConnectorError(input) {
1264
+ return createAuditEvent({
1265
+ id: input.id,
1266
+ event_type: AuditEventTypeEnum.ConnectorError,
1267
+ severity: AuditSeverityEnum.Error,
1268
+ actor: connectorActor(input.connector_id, input.connector_platform, input.connector_name),
1269
+ target: connectorTarget(input.connector_id, input.connector_name),
1270
+ trace_id: input.trace_id,
1271
+ workspace_id: input.workspace_id,
1272
+ thread_id: input.thread_id,
1273
+ task_id: input.task_id,
1274
+ parent_event_id: input.parent_event_id,
1275
+ metadata: {
1276
+ ...input.metadata,
1277
+ ...input.error_code ? { error_code: input.error_code } : {},
1278
+ error_message: input.error_message
1279
+ },
1280
+ tags: input.tags,
1281
+ now: input.now
1282
+ });
1283
+ }
1284
+ function emitPolicyChecked(input) {
1285
+ const policyContext = {
1286
+ action: input.action,
1287
+ rule: input.rule,
1288
+ approved: input.approved,
1289
+ reason: input.reason,
1290
+ agent_id: input.agent_id
1291
+ };
1292
+ return createAuditEvent({
1293
+ id: input.id,
1294
+ event_type: AuditEventTypeEnum.PolicyChecked,
1295
+ severity: AuditSeverityEnum.Info,
1296
+ actor: systemActor("policy-engine"),
1297
+ target: taskTarget(input.task_id),
1298
+ trace_id: input.trace_id,
1299
+ workspace_id: input.workspace_id,
1300
+ thread_id: input.thread_id,
1301
+ task_id: input.task_id,
1302
+ parent_event_id: input.parent_event_id,
1303
+ duration_ms: input.duration_ms,
1304
+ policy: policyContext,
1305
+ metadata: input.metadata,
1306
+ tags: input.tags,
1307
+ now: input.now
1308
+ });
1309
+ }
1310
+ function emitPolicyApproved(input) {
1311
+ return createAuditEvent({
1312
+ id: input.id,
1313
+ event_type: AuditEventTypeEnum.PolicyApproved,
1314
+ severity: AuditSeverityEnum.Info,
1315
+ actor: systemActor("policy-engine"),
1316
+ target: taskTarget(input.task_id),
1317
+ trace_id: input.trace_id,
1318
+ workspace_id: input.workspace_id,
1319
+ thread_id: input.thread_id,
1320
+ task_id: input.task_id,
1321
+ parent_event_id: input.parent_event_id,
1322
+ duration_ms: input.duration_ms,
1323
+ policy: {
1324
+ action: input.action,
1325
+ rule: input.rule,
1326
+ approved: true,
1327
+ reason: input.reason,
1328
+ agent_id: input.agent_id
1329
+ },
1330
+ metadata: input.metadata,
1331
+ tags: input.tags,
1332
+ now: input.now
1333
+ });
1334
+ }
1335
+ function emitPolicyDenied(input) {
1336
+ return createAuditEvent({
1337
+ id: input.id,
1338
+ event_type: AuditEventTypeEnum.PolicyDenied,
1339
+ severity: AuditSeverityEnum.Warning,
1340
+ actor: systemActor("policy-engine"),
1341
+ target: taskTarget(input.task_id),
1342
+ trace_id: input.trace_id,
1343
+ workspace_id: input.workspace_id,
1344
+ thread_id: input.thread_id,
1345
+ task_id: input.task_id,
1346
+ parent_event_id: input.parent_event_id,
1347
+ duration_ms: input.duration_ms,
1348
+ policy: {
1349
+ action: input.action,
1350
+ rule: input.rule,
1351
+ approved: false,
1352
+ reason: input.reason,
1353
+ agent_id: input.agent_id
1354
+ },
1355
+ metadata: input.metadata,
1356
+ tags: input.tags,
1357
+ now: input.now
1358
+ });
1359
+ }
1360
+ var STATE_TO_EVENT_TYPE = {
1361
+ submitted: AuditEventTypeEnum.TaskSubmitted,
1362
+ working: AuditEventTypeEnum.TaskStarted,
1363
+ "input-required": AuditEventTypeEnum.TaskInputRequired,
1364
+ completed: AuditEventTypeEnum.TaskCompleted,
1365
+ failed: AuditEventTypeEnum.TaskFailed,
1366
+ cancelled: AuditEventTypeEnum.TaskCancelled
1367
+ };
1368
+ function emitAuditForTransition(input, newState) {
1369
+ const eventType = STATE_TO_EVENT_TYPE[newState] ?? AuditEventTypeEnum.TaskProgress;
1370
+ const severity = newState === "failed" ? AuditSeverityEnum.Error : newState === "cancelled" ? AuditSeverityEnum.Warning : AuditSeverityEnum.Info;
1371
+ return createAuditEvent({
1372
+ id: input.id,
1373
+ event_type: eventType,
1374
+ severity,
1375
+ actor: agentActor(input.actor_agent_id, input.actor_platform, input.actor_name),
1376
+ target: taskTarget(input.task_id),
1377
+ trace_id: input.trace_id,
1378
+ workspace_id: input.workspace_id,
1379
+ thread_id: input.thread_id,
1380
+ task_id: input.task_id,
1381
+ parent_event_id: input.parent_event_id,
1382
+ duration_ms: input.duration_ms,
1383
+ metadata: input.metadata,
1384
+ tags: input.tags,
1385
+ now: input.now
1386
+ });
1387
+ }
1388
+
1389
+ // src/validation.ts
1390
+ function isRecord(value) {
1391
+ return typeof value === "object" && value !== null && !Array.isArray(value);
1392
+ }
1393
+ function isNonEmptyString(value) {
1394
+ return typeof value === "string" && value.trim().length > 0;
1395
+ }
1396
+ function isValidISODate(value) {
1397
+ return typeof value === "string" && !Number.isNaN(Date.parse(value));
1398
+ }
1399
+ var VALID_PLATFORMS = /* @__PURE__ */ new Set([
1400
+ "codex",
1401
+ "claude_code",
1402
+ "recoder_web",
1403
+ "recoder_pm",
1404
+ "recoder_agency",
1405
+ "recoder_backend",
1406
+ "recoder_desktop",
1407
+ "custom"
1408
+ ]);
1409
+ var VALID_PROTOCOLS = /* @__PURE__ */ new Set(["a2a", "mcp", "jsonrpc", "rest", "file_git"]);
1410
+ var VALID_TASK_STATES = new Set(TASK_STATES);
1411
+ var VALID_AUDIT_EVENT_TYPES = new Set(AUDIT_EVENT_TYPES);
1412
+ var VALID_ACTOR_KINDS = /* @__PURE__ */ new Set(["agent", "user", "system", "connector"]);
1413
+ var VALID_TARGET_KINDS = /* @__PURE__ */ new Set([
1414
+ "task",
1415
+ "agent",
1416
+ "connector",
1417
+ "channel",
1418
+ "thread",
1419
+ "message",
1420
+ "policy",
1421
+ "workspace",
1422
+ "system"
1423
+ ]);
1424
+ var VALID_SEVERITIES = /* @__PURE__ */ new Set(["low", "medium", "high", "critical"]);
1425
+ var VALID_AUDIT_SEVERITIES = /* @__PURE__ */ new Set(["info", "warning", "error", "critical"]);
1426
+ function validateAgentRef(value, path, issues) {
1427
+ if (!isRecord(value)) {
1428
+ issues.push({ path, message: "must be an object" });
1429
+ return;
1430
+ }
1431
+ if (!isNonEmptyString(value.agent_id)) {
1432
+ issues.push({ path: `${path}.agent_id`, message: "must be a non-empty string" });
1433
+ }
1434
+ if (!isNonEmptyString(value.platform) || !VALID_PLATFORMS.has(value.platform)) {
1435
+ issues.push({
1436
+ path: `${path}.platform`,
1437
+ message: `must be one of: ${[...VALID_PLATFORMS].join(", ")}`
1438
+ });
1439
+ }
1440
+ }
1441
+ function validateConnectorInfo(value, path, issues) {
1442
+ if (!isRecord(value)) {
1443
+ issues.push({ path, message: "must be an object" });
1444
+ return;
1445
+ }
1446
+ if (!isNonEmptyString(value.platform) || !VALID_PLATFORMS.has(value.platform)) {
1447
+ issues.push({
1448
+ path: `${path}.platform`,
1449
+ message: `must be one of: ${[...VALID_PLATFORMS].join(", ")}`
1450
+ });
1451
+ }
1452
+ if (!isNonEmptyString(value.protocol) || !VALID_PROTOCOLS.has(value.protocol)) {
1453
+ issues.push({
1454
+ path: `${path}.protocol`,
1455
+ message: `must be one of: ${[...VALID_PROTOCOLS].join(", ")}`
1456
+ });
1457
+ }
1458
+ if (!isNonEmptyString(value.connector_id)) {
1459
+ issues.push({
1460
+ path: `${path}.connector_id`,
1461
+ message: "must be a non-empty string"
1462
+ });
1463
+ }
1464
+ }
1465
+ function validatePayloadItem(value, path, issues) {
1466
+ if (!isRecord(value)) {
1467
+ issues.push({ path, message: "must be an object" });
1468
+ return;
1469
+ }
1470
+ const validKinds = /* @__PURE__ */ new Set(["text", "json", "command", "artifact"]);
1471
+ if (!isNonEmptyString(value.kind) || !validKinds.has(value.kind)) {
1472
+ issues.push({
1473
+ path: `${path}.kind`,
1474
+ message: `must be one of: ${[...validKinds].join(", ")}`
1475
+ });
1476
+ return;
1477
+ }
1478
+ switch (value.kind) {
1479
+ case "text":
1480
+ if (!isNonEmptyString(value.text)) {
1481
+ issues.push({ path: `${path}.text`, message: "must be a non-empty string" });
1482
+ }
1483
+ break;
1484
+ case "json":
1485
+ if (!isRecord(value.data)) {
1486
+ issues.push({ path: `${path}.data`, message: "must be an object" });
1487
+ }
1488
+ break;
1489
+ case "command":
1490
+ if (!isNonEmptyString(value.command)) {
1491
+ issues.push({ path: `${path}.command`, message: "must be a non-empty string" });
1492
+ }
1493
+ break;
1494
+ case "artifact":
1495
+ if (!isNonEmptyString(value.mimeType) && !isNonEmptyString(value.mime_type)) {
1496
+ issues.push({ path: `${path}.mimeType`, message: "must be a non-empty string" });
1497
+ }
1498
+ if (!isNonEmptyString(value.content)) {
1499
+ issues.push({ path: `${path}.content`, message: "must be a non-empty string" });
1500
+ }
1501
+ break;
1502
+ }
1503
+ }
1504
+ function validateTaskEnvelope(value, options) {
1505
+ const issues = [];
1506
+ if (!isRecord(value)) {
1507
+ return { valid: false, issues: [{ path: "$", message: "must be an object" }] };
1508
+ }
1509
+ if (!isNonEmptyString(value.envelope_version)) {
1510
+ issues.push({ path: "envelope_version", message: "must be a non-empty string" });
1511
+ } else if (options?.strictVersion && value.envelope_version !== TASK_ENVELOPE_VERSION) {
1512
+ issues.push({
1513
+ path: "envelope_version",
1514
+ message: `must equal "${TASK_ENVELOPE_VERSION}"`
1515
+ });
1516
+ }
1517
+ if (!isNonEmptyString(value.id)) {
1518
+ issues.push({ path: "id", message: "must be a non-empty string" });
1519
+ }
1520
+ if (!isNonEmptyString(value.workspace_id)) {
1521
+ issues.push({ path: "workspace_id", message: "must be a non-empty string" });
1522
+ }
1523
+ if (!isNonEmptyString(value.thread_id)) {
1524
+ issues.push({ path: "thread_id", message: "must be a non-empty string" });
1525
+ }
1526
+ if (!isNonEmptyString(value.trace_id)) {
1527
+ issues.push({ path: "trace_id", message: "must be a non-empty string" });
1528
+ }
1529
+ if (!isNonEmptyString(value.state) || !VALID_TASK_STATES.has(value.state)) {
1530
+ issues.push({
1531
+ path: "state",
1532
+ message: `must be one of: ${[...VALID_TASK_STATES].join(", ")}`
1533
+ });
1534
+ }
1535
+ validateAgentRef(value.requester, "requester", issues);
1536
+ validateAgentRef(value.assignee, "assignee", issues);
1537
+ validateConnectorInfo(value.connector, "connector", issues);
1538
+ if (value.payload === void 0 || value.payload === null) {
1539
+ issues.push({ path: "payload", message: "is required" });
1540
+ } else if (Array.isArray(value.payload)) {
1541
+ value.payload.forEach(
1542
+ (item, i) => validatePayloadItem(item, `payload[${i}]`, issues)
1543
+ );
1544
+ } else {
1545
+ validatePayloadItem(value.payload, "payload", issues);
1546
+ }
1547
+ if (!isValidISODate(value.created_at)) {
1548
+ issues.push({ path: "created_at", message: "must be a valid ISO-8601 timestamp" });
1549
+ }
1550
+ if (!isValidISODate(value.updated_at)) {
1551
+ issues.push({ path: "updated_at", message: "must be a valid ISO-8601 timestamp" });
1552
+ }
1553
+ if (value.error !== void 0) {
1554
+ if (!isRecord(value.error)) {
1555
+ issues.push({ path: "error", message: "must be an object when present" });
1556
+ } else {
1557
+ if (!isNonEmptyString(value.error.code)) {
1558
+ issues.push({ path: "error.code", message: "must be a non-empty string" });
1559
+ }
1560
+ if (!isNonEmptyString(value.error.category)) {
1561
+ issues.push({ path: "error.category", message: "must be a non-empty string" });
1562
+ }
1563
+ if (!isNonEmptyString(value.error.message)) {
1564
+ issues.push({ path: "error.message", message: "must be a non-empty string" });
1565
+ }
1566
+ if (!isNonEmptyString(value.error.severity) || !VALID_SEVERITIES.has(value.error.severity)) {
1567
+ issues.push({
1568
+ path: "error.severity",
1569
+ message: `must be one of: ${[...VALID_SEVERITIES].join(", ")}`
1570
+ });
1571
+ }
1572
+ if (typeof value.error.retryable !== "boolean") {
1573
+ issues.push({ path: "error.retryable", message: "must be a boolean" });
1574
+ }
1575
+ }
1576
+ }
1577
+ return { valid: issues.length === 0, issues };
1578
+ }
1579
+ function isValidTaskEnvelope(value) {
1580
+ return validateTaskEnvelope(value).valid;
1581
+ }
1582
+ function assertTaskEnvelope(value) {
1583
+ const result = validateTaskEnvelope(value);
1584
+ if (!result.valid) {
1585
+ const summary = result.issues.map((i) => ` ${i.path}: ${i.message}`).join("\n");
1586
+ throw new Error(`Invalid TaskEnvelope:
1587
+ ${summary}`);
1588
+ }
1589
+ }
1590
+ function validateAuditActor(value, path, issues) {
1591
+ if (!isRecord(value)) {
1592
+ issues.push({ path, message: "must be an object" });
1593
+ return;
1594
+ }
1595
+ if (!isNonEmptyString(value.kind) || !VALID_ACTOR_KINDS.has(value.kind)) {
1596
+ issues.push({
1597
+ path: `${path}.kind`,
1598
+ message: `must be one of: ${[...VALID_ACTOR_KINDS].join(", ")}`
1599
+ });
1600
+ }
1601
+ if (!isNonEmptyString(value.id)) {
1602
+ issues.push({ path: `${path}.id`, message: "must be a non-empty string" });
1603
+ }
1604
+ }
1605
+ function validateAuditTarget(value, path, issues) {
1606
+ if (!isRecord(value)) {
1607
+ issues.push({ path, message: "must be an object" });
1608
+ return;
1609
+ }
1610
+ if (!isNonEmptyString(value.kind) || !VALID_TARGET_KINDS.has(value.kind)) {
1611
+ issues.push({
1612
+ path: `${path}.kind`,
1613
+ message: `must be one of: ${[...VALID_TARGET_KINDS].join(", ")}`
1614
+ });
1615
+ }
1616
+ if (!isNonEmptyString(value.id)) {
1617
+ issues.push({ path: `${path}.id`, message: "must be a non-empty string" });
1618
+ }
1619
+ }
1620
+ function validateAuditEvent(value, options) {
1621
+ const issues = [];
1622
+ if (!isRecord(value)) {
1623
+ return { valid: false, issues: [{ path: "$", message: "must be an object" }] };
1624
+ }
1625
+ if (!isNonEmptyString(value.schema_version)) {
1626
+ issues.push({ path: "schema_version", message: "must be a non-empty string" });
1627
+ } else if (options?.strictVersion && value.schema_version !== AUDIT_EVENT_VERSION) {
1628
+ issues.push({
1629
+ path: "schema_version",
1630
+ message: `must equal "${AUDIT_EVENT_VERSION}"`
1631
+ });
1632
+ }
1633
+ if (!isNonEmptyString(value.id)) {
1634
+ issues.push({ path: "id", message: "must be a non-empty string" });
1635
+ }
1636
+ if (!isNonEmptyString(value.event_type) || !VALID_AUDIT_EVENT_TYPES.has(value.event_type)) {
1637
+ issues.push({
1638
+ path: "event_type",
1639
+ message: `must be one of the canonical audit event types`
1640
+ });
1641
+ }
1642
+ if (value.severity !== void 0 && (!isNonEmptyString(value.severity) || !VALID_AUDIT_SEVERITIES.has(value.severity))) {
1643
+ issues.push({
1644
+ path: "severity",
1645
+ message: `must be one of: ${[...VALID_AUDIT_SEVERITIES].join(", ")}`
1646
+ });
1647
+ }
1648
+ validateAuditActor(value.actor, "actor", issues);
1649
+ validateAuditTarget(value.target, "target", issues);
1650
+ if (!isNonEmptyString(value.trace_id)) {
1651
+ issues.push({ path: "trace_id", message: "must be a non-empty string" });
1652
+ }
1653
+ if (!isNonEmptyString(value.workspace_id)) {
1654
+ issues.push({ path: "workspace_id", message: "must be a non-empty string" });
1655
+ }
1656
+ if (!isValidISODate(value.timestamp)) {
1657
+ issues.push({ path: "timestamp", message: "must be a valid ISO-8601 timestamp" });
1658
+ }
1659
+ if (value.policy !== void 0) {
1660
+ if (!isRecord(value.policy)) {
1661
+ issues.push({ path: "policy", message: "must be an object when present" });
1662
+ } else {
1663
+ if (!isNonEmptyString(value.policy.action)) {
1664
+ issues.push({ path: "policy.action", message: "must be a non-empty string" });
1665
+ }
1666
+ if (!isNonEmptyString(value.policy.rule)) {
1667
+ issues.push({ path: "policy.rule", message: "must be a non-empty string" });
1668
+ }
1669
+ if (typeof value.policy.approved !== "boolean") {
1670
+ issues.push({ path: "policy.approved", message: "must be a boolean" });
1671
+ }
1672
+ if (!isNonEmptyString(value.policy.reason)) {
1673
+ issues.push({ path: "policy.reason", message: "must be a non-empty string" });
1674
+ }
1675
+ if (!isNonEmptyString(value.policy.agent_id)) {
1676
+ issues.push({ path: "policy.agent_id", message: "must be a non-empty string" });
1677
+ }
1678
+ }
1679
+ }
1680
+ return { valid: issues.length === 0, issues };
1681
+ }
1682
+ function isValidAuditEvent(value) {
1683
+ return validateAuditEvent(value).valid;
1684
+ }
1685
+ function assertAuditEvent(value) {
1686
+ const result = validateAuditEvent(value);
1687
+ if (!result.valid) {
1688
+ const summary = result.issues.map((i) => ` ${i.path}: ${i.message}`).join("\n");
1689
+ throw new Error(`Invalid AuditEvent:
1690
+ ${summary}`);
1691
+ }
1692
+ }
1693
+ function validateA2AError(value) {
1694
+ const issues = [];
1695
+ if (!isRecord(value)) {
1696
+ return { valid: false, issues: [{ path: "$", message: "must be an object" }] };
1697
+ }
1698
+ if (!isNonEmptyString(value.code)) {
1699
+ issues.push({ path: "code", message: "must be a non-empty string" });
1700
+ } else if (!isA2AErrorCode(value.code)) {
1701
+ issues.push({
1702
+ path: "code",
1703
+ message: "must be a recognized A2A error code"
1704
+ });
1705
+ }
1706
+ if (!isNonEmptyString(value.category)) {
1707
+ issues.push({ path: "category", message: "must be a non-empty string" });
1708
+ }
1709
+ if (!isNonEmptyString(value.message)) {
1710
+ issues.push({ path: "message", message: "must be a non-empty string" });
1711
+ }
1712
+ if (!isNonEmptyString(value.severity) || !VALID_SEVERITIES.has(value.severity)) {
1713
+ issues.push({
1714
+ path: "severity",
1715
+ message: `must be one of: ${[...VALID_SEVERITIES].join(", ")}`
1716
+ });
1717
+ }
1718
+ if (typeof value.retryable !== "boolean") {
1719
+ issues.push({ path: "retryable", message: "must be a boolean" });
1720
+ }
1721
+ return { valid: issues.length === 0, issues };
1722
+ }
1723
+ function isValidA2AError(value) {
1724
+ return validateA2AError(value).valid;
1725
+ }
1726
+ // Annotate the CommonJS export names for ESM import in node:
1727
+ 0 && (module.exports = {
1728
+ A2AErrorCategoryEnum,
1729
+ A2AErrorCodeEnum,
1730
+ A2AErrorSeverityEnum,
1731
+ A2AStructuredError,
1732
+ A2A_ERROR_CODE_REGISTRY,
1733
+ ALLOWED_STATE_TRANSITIONS,
1734
+ AUDIT_EVENT_TYPES,
1735
+ AUDIT_EVENT_VERSION,
1736
+ AuditActorKindEnum,
1737
+ AuditEventTypeEnum,
1738
+ AuditSeverityEnum,
1739
+ AuditTargetKindEnum,
1740
+ TASK_ENVELOPE_VERSION,
1741
+ TASK_STATES,
1742
+ TERMINAL_TASK_STATES,
1743
+ TaskStateEnum,
1744
+ a2aErrorToTaskErrorInfo,
1745
+ assertAuditEvent,
1746
+ assertTaskEnvelope,
1747
+ buildA2AError,
1748
+ buildClaudeEnvelope,
1749
+ buildCodexEnvelope,
1750
+ cancelEnvelope,
1751
+ canonicalToClaudeState,
1752
+ canonicalToCodexState,
1753
+ canonicalToConnectorState,
1754
+ checkConnectorTransition,
1755
+ completeEnvelope,
1756
+ createAuditEvent,
1757
+ createTaskEnvelope,
1758
+ emitAuditForTransition,
1759
+ emitConnectorAttached,
1760
+ emitConnectorError,
1761
+ emitConnectorHealth,
1762
+ emitPolicyApproved,
1763
+ emitPolicyChecked,
1764
+ emitPolicyDenied,
1765
+ emitTaskCancelled,
1766
+ emitTaskCompleted,
1767
+ emitTaskFailed,
1768
+ emitTaskInputRequired,
1769
+ emitTaskProgress,
1770
+ emitTaskStarted,
1771
+ emitTaskSubmitted,
1772
+ failEnvelope,
1773
+ getErrorCodeMeta,
1774
+ isA2AErrorCode,
1775
+ isConnectorTerminalState,
1776
+ isTerminalState,
1777
+ isValidA2AError,
1778
+ isValidAuditEvent,
1779
+ isValidTaskEnvelope,
1780
+ isValidTransition,
1781
+ mapClaudeError,
1782
+ mapClaudeState,
1783
+ mapClaudeStateStrict,
1784
+ mapCodexError,
1785
+ mapCodexState,
1786
+ mapCodexStateStrict,
1787
+ mapConnectorError,
1788
+ mapConnectorState,
1789
+ mapConnectorStateStrict,
1790
+ mapHttpError,
1791
+ startEnvelope,
1792
+ transitionTaskEnvelope,
1793
+ validateA2AError,
1794
+ validateAuditEvent,
1795
+ validateTaskEnvelope,
1796
+ wrapUnknownError
1797
+ });
1798
+ //# sourceMappingURL=index.js.map