macp-sdk-typescript 0.2.2 → 0.4.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 (47) hide show
  1. package/CHANGELOG.md +176 -1
  2. package/README.md +49 -6
  3. package/dist/agent/cancel-callback.d.ts +23 -0
  4. package/dist/agent/cancel-callback.js +108 -0
  5. package/dist/agent/index.d.ts +1 -0
  6. package/dist/agent/index.js +1 -0
  7. package/dist/agent/participant.d.ts +39 -0
  8. package/dist/agent/participant.js +114 -45
  9. package/dist/agent/runner.d.ts +13 -0
  10. package/dist/agent/runner.js +34 -0
  11. package/dist/agent/strategies.d.ts +2 -0
  12. package/dist/agent/strategies.js +8 -0
  13. package/dist/agent/transports.js +5 -0
  14. package/dist/base-session.d.ts +75 -0
  15. package/dist/base-session.js +127 -0
  16. package/dist/client.d.ts +54 -0
  17. package/dist/client.js +130 -9
  18. package/dist/constants.d.ts +21 -16
  19. package/dist/constants.js +24 -17
  20. package/dist/decision.d.ts +10 -0
  21. package/dist/decision.js +20 -0
  22. package/dist/envelope.d.ts +38 -1
  23. package/dist/envelope.js +53 -1
  24. package/dist/errors.d.ts +17 -4
  25. package/dist/errors.js +19 -10
  26. package/dist/handoff.d.ts +10 -8
  27. package/dist/handoff.js +20 -16
  28. package/dist/index.d.ts +3 -1
  29. package/dist/index.js +10 -2
  30. package/dist/logging.d.ts +21 -0
  31. package/dist/logging.js +64 -0
  32. package/dist/policy.d.ts +47 -42
  33. package/dist/policy.js +5 -6
  34. package/dist/projections/base.d.ts +20 -0
  35. package/dist/projections/base.js +37 -0
  36. package/dist/projections.d.ts +1 -0
  37. package/dist/projections.js +1 -0
  38. package/dist/proposal.d.ts +10 -0
  39. package/dist/proposal.js +20 -0
  40. package/dist/quorum.d.ts +10 -0
  41. package/dist/quorum.js +20 -0
  42. package/dist/task.d.ts +14 -4
  43. package/dist/task.js +24 -4
  44. package/dist/types.d.ts +30 -3
  45. package/dist/watchers.d.ts +37 -2
  46. package/dist/watchers.js +65 -5
  47. package/package.json +2 -2
package/dist/types.d.ts CHANGED
@@ -19,13 +19,24 @@ export interface MacpErrorShape {
19
19
  messageId?: string;
20
20
  details?: Buffer;
21
21
  }
22
+ /**
23
+ * Lifecycle state of a session. Mirrors the `SessionState` enum in
24
+ * `macp/v1/envelope.proto`.
25
+ *
26
+ * `SESSION_STATE_SUSPENDED` is a non-terminal pause (TTL is banked while
27
+ * suspended and restored on resume); `SESSION_STATE_CANCELLED` is the terminal
28
+ * state for an explicit `CancelSession` — distinct from `SESSION_STATE_EXPIRED`
29
+ * (TTL / deterministic runtime policy) so consumers can tell explicit
30
+ * cancellation from expiry.
31
+ */
32
+ export type SessionState = 'SESSION_STATE_UNSPECIFIED' | 'SESSION_STATE_OPEN' | 'SESSION_STATE_RESOLVED' | 'SESSION_STATE_EXPIRED' | 'SESSION_STATE_SUSPENDED' | 'SESSION_STATE_CANCELLED';
22
33
  export interface Ack {
23
34
  ok?: boolean;
24
35
  duplicate?: boolean;
25
36
  messageId?: string;
26
37
  sessionId?: string;
27
38
  acceptedAtUnixMs?: string;
28
- sessionState?: string;
39
+ sessionState?: SessionState;
29
40
  error?: MacpErrorShape;
30
41
  }
31
42
  export interface ParticipantActivity {
@@ -36,7 +47,7 @@ export interface ParticipantActivity {
36
47
  export interface SessionMetadata {
37
48
  sessionId?: string;
38
49
  mode?: string;
39
- state?: string;
50
+ state?: SessionState;
40
51
  startedAtUnixMs?: string;
41
52
  expiresAtUnixMs?: string;
42
53
  modeVersion?: string;
@@ -99,6 +110,15 @@ export interface SessionStartPayload {
99
110
  extensions?: Record<string, Buffer>;
100
111
  roots?: Root[];
101
112
  }
113
+ /**
114
+ * Reference to a specific accepted commitment, used for cross-session
115
+ * supersession (RFC-MACP-0001 §7.3). Mirrors `macp.v1.CommitmentRef`.
116
+ */
117
+ export interface CommitmentRef {
118
+ sessionId: string;
119
+ /** Content hash of the superseded `CommitmentPayload`. */
120
+ commitmentHash: string;
121
+ }
102
122
  export interface CommitmentPayload {
103
123
  commitmentId: string;
104
124
  action: string;
@@ -108,6 +128,13 @@ export interface CommitmentPayload {
108
128
  policyVersion?: string;
109
129
  configurationVersion: string;
110
130
  outcomePositive?: boolean;
131
+ /**
132
+ * Cross-session supersession (RFC-MACP-0001 §7.3): when present, this
133
+ * commitment supersedes a prior commitment identified by
134
+ * `{sessionId, commitmentHash}`. The superseding commitment lives in a NEW
135
+ * session — a RESOLVED session accepts no further messages.
136
+ */
137
+ supersedes?: CommitmentRef;
111
138
  }
112
139
  export interface SignalPayload {
113
140
  signalType: string;
@@ -264,7 +291,7 @@ export interface RegistryChanged {
264
291
  export interface RootsChanged {
265
292
  observedAtUnixMs: string;
266
293
  }
267
- export type SessionLifecycleEventType = 'EVENT_TYPE_UNSPECIFIED' | 'EVENT_TYPE_CREATED' | 'EVENT_TYPE_RESOLVED' | 'EVENT_TYPE_EXPIRED';
294
+ export type SessionLifecycleEventType = 'EVENT_TYPE_UNSPECIFIED' | 'EVENT_TYPE_CREATED' | 'EVENT_TYPE_RESOLVED' | 'EVENT_TYPE_EXPIRED' | 'EVENT_TYPE_SUSPENDED' | 'EVENT_TYPE_RESUMED' | 'EVENT_TYPE_CANCELLED';
268
295
  export interface SessionLifecycleEvent {
269
296
  eventType: SessionLifecycleEventType;
270
297
  session: SessionMetadata;
@@ -1,6 +1,28 @@
1
1
  import type { AuthConfig } from './auth';
2
2
  import type { MacpClient } from './client';
3
- import type { Envelope, PolicyDescriptor, RegistryChanged, RootsChanged, SessionLifecycleEvent } from './types';
3
+ import type { Envelope, PolicyDescriptor, RegistryChanged, RootsChanged, SessionLifecycleEvent, SessionLifecycleEventType } from './types';
4
+ /**
5
+ * The lifecycle event types after which a session emits no further events.
6
+ * `EVENT_TYPE_CANCELLED` (proto 0.1.3) is terminal alongside `RESOLVED` and
7
+ * `EXPIRED`; `SUSPENDED` / `RESUMED` are non-terminal. Parity with python-sdk
8
+ * `SessionLifecycle.is_terminal`.
9
+ */
10
+ export declare const TERMINAL_SESSION_LIFECYCLE_EVENT_TYPES: readonly SessionLifecycleEventType[];
11
+ type LifecycleEventOrType = SessionLifecycleEvent | SessionLifecycleEventType;
12
+ /**
13
+ * Predicates over a {@link SessionLifecycleEvent} (or a bare
14
+ * {@link SessionLifecycleEventType}), mirroring python-sdk's `SessionLifecycle`
15
+ * properties so consumers can classify lifecycle events without re-deriving the
16
+ * proto enum strings. {@link isTerminalSessionLifecycleEvent} deliberately
17
+ * includes `EVENT_TYPE_CANCELLED`.
18
+ */
19
+ export declare const isSessionCreated: (e: LifecycleEventOrType) => boolean;
20
+ export declare const isSessionResolved: (e: LifecycleEventOrType) => boolean;
21
+ export declare const isSessionExpired: (e: LifecycleEventOrType) => boolean;
22
+ export declare const isSessionCancelled: (e: LifecycleEventOrType) => boolean;
23
+ export declare const isSessionSuspended: (e: LifecycleEventOrType) => boolean;
24
+ export declare const isSessionResumed: (e: LifecycleEventOrType) => boolean;
25
+ export declare const isTerminalSessionLifecycleEvent: (e: LifecycleEventOrType) => boolean;
4
26
  export declare class ModeRegistryWatcher {
5
27
  private readonly client;
6
28
  private readonly auth?;
@@ -48,10 +70,23 @@ export declare class PolicyWatcher {
48
70
  export declare class SessionLifecycleWatcher {
49
71
  private readonly client;
50
72
  private readonly auth?;
73
+ private deprecatedEventsWarned;
74
+ private deprecatedNextEventWarned;
51
75
  constructor(client: MacpClient, options?: {
52
76
  auth?: AuthConfig;
53
77
  });
54
- events(signal?: AbortSignal): AsyncGenerator<SessionLifecycleEvent, void, void>;
78
+ changes(signal?: AbortSignal): AsyncGenerator<SessionLifecycleEvent, void, void>;
55
79
  watch(handler: (event: SessionLifecycleEvent) => void | Promise<void>): Promise<void>;
80
+ nextChange(): Promise<SessionLifecycleEvent>;
81
+ /**
82
+ * @deprecated Use {@link changes} — renamed for parity with python-sdk's
83
+ * `SessionLifecycleWatcher.changes()` and the uniform `changes()` name
84
+ * used by all other watchers in both SDKs. Will be removed in 0.5.0.
85
+ */
86
+ events(signal?: AbortSignal): AsyncGenerator<SessionLifecycleEvent, void, void>;
87
+ /**
88
+ * @deprecated Use {@link nextChange}. Will be removed in 0.5.0.
89
+ */
56
90
  nextEvent(): Promise<SessionLifecycleEvent>;
57
91
  }
92
+ export {};
package/dist/watchers.js CHANGED
@@ -1,6 +1,42 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SessionLifecycleWatcher = exports.PolicyWatcher = exports.SignalWatcher = exports.RootsWatcher = exports.ModeRegistryWatcher = void 0;
3
+ exports.SessionLifecycleWatcher = exports.PolicyWatcher = exports.SignalWatcher = exports.RootsWatcher = exports.ModeRegistryWatcher = exports.isTerminalSessionLifecycleEvent = exports.isSessionResumed = exports.isSessionSuspended = exports.isSessionCancelled = exports.isSessionExpired = exports.isSessionResolved = exports.isSessionCreated = exports.TERMINAL_SESSION_LIFECYCLE_EVENT_TYPES = void 0;
4
+ const logging_1 = require("./logging");
5
+ /**
6
+ * The lifecycle event types after which a session emits no further events.
7
+ * `EVENT_TYPE_CANCELLED` (proto 0.1.3) is terminal alongside `RESOLVED` and
8
+ * `EXPIRED`; `SUSPENDED` / `RESUMED` are non-terminal. Parity with python-sdk
9
+ * `SessionLifecycle.is_terminal`.
10
+ */
11
+ exports.TERMINAL_SESSION_LIFECYCLE_EVENT_TYPES = [
12
+ 'EVENT_TYPE_RESOLVED',
13
+ 'EVENT_TYPE_EXPIRED',
14
+ 'EVENT_TYPE_CANCELLED',
15
+ ];
16
+ function lifecycleEventType(e) {
17
+ return typeof e === 'string' ? e : e.eventType;
18
+ }
19
+ /**
20
+ * Predicates over a {@link SessionLifecycleEvent} (or a bare
21
+ * {@link SessionLifecycleEventType}), mirroring python-sdk's `SessionLifecycle`
22
+ * properties so consumers can classify lifecycle events without re-deriving the
23
+ * proto enum strings. {@link isTerminalSessionLifecycleEvent} deliberately
24
+ * includes `EVENT_TYPE_CANCELLED`.
25
+ */
26
+ const isSessionCreated = (e) => lifecycleEventType(e) === 'EVENT_TYPE_CREATED';
27
+ exports.isSessionCreated = isSessionCreated;
28
+ const isSessionResolved = (e) => lifecycleEventType(e) === 'EVENT_TYPE_RESOLVED';
29
+ exports.isSessionResolved = isSessionResolved;
30
+ const isSessionExpired = (e) => lifecycleEventType(e) === 'EVENT_TYPE_EXPIRED';
31
+ exports.isSessionExpired = isSessionExpired;
32
+ const isSessionCancelled = (e) => lifecycleEventType(e) === 'EVENT_TYPE_CANCELLED';
33
+ exports.isSessionCancelled = isSessionCancelled;
34
+ const isSessionSuspended = (e) => lifecycleEventType(e) === 'EVENT_TYPE_SUSPENDED';
35
+ exports.isSessionSuspended = isSessionSuspended;
36
+ const isSessionResumed = (e) => lifecycleEventType(e) === 'EVENT_TYPE_RESUMED';
37
+ exports.isSessionResumed = isSessionResumed;
38
+ const isTerminalSessionLifecycleEvent = (e) => exports.TERMINAL_SESSION_LIFECYCLE_EVENT_TYPES.includes(lifecycleEventType(e));
39
+ exports.isTerminalSessionLifecycleEvent = isTerminalSessionLifecycleEvent;
4
40
  function serverStreamToAsyncGenerator(stream) {
5
41
  const queue = [];
6
42
  let resolve = null;
@@ -177,11 +213,13 @@ exports.PolicyWatcher = PolicyWatcher;
177
213
  class SessionLifecycleWatcher {
178
214
  client;
179
215
  auth;
216
+ deprecatedEventsWarned = false;
217
+ deprecatedNextEventWarned = false;
180
218
  constructor(client, options) {
181
219
  this.client = client;
182
220
  this.auth = options?.auth;
183
221
  }
184
- async *events(signal) {
222
+ async *changes(signal) {
185
223
  const stream = this.client.watchSessions(this.auth);
186
224
  if (signal) {
187
225
  signal.addEventListener('abort', () => stream.cancel(), { once: true });
@@ -193,17 +231,39 @@ class SessionLifecycleWatcher {
193
231
  }
194
232
  }
195
233
  async watch(handler) {
196
- for await (const event of this.events()) {
234
+ for await (const event of this.changes()) {
197
235
  await handler(event);
198
236
  }
199
237
  }
200
- async nextEvent() {
201
- const gen = this.events();
238
+ async nextChange() {
239
+ const gen = this.changes();
202
240
  const result = await gen.next();
203
241
  await gen.return(undefined);
204
242
  if (result.done)
205
243
  throw new Error('stream ended before receiving a session lifecycle event');
206
244
  return result.value;
207
245
  }
246
+ /**
247
+ * @deprecated Use {@link changes} — renamed for parity with python-sdk's
248
+ * `SessionLifecycleWatcher.changes()` and the uniform `changes()` name
249
+ * used by all other watchers in both SDKs. Will be removed in 0.5.0.
250
+ */
251
+ async *events(signal) {
252
+ if (!this.deprecatedEventsWarned) {
253
+ this.deprecatedEventsWarned = true;
254
+ logging_1.logger.warn('[deprecated] SessionLifecycleWatcher.events() — use changes() instead');
255
+ }
256
+ yield* this.changes(signal);
257
+ }
258
+ /**
259
+ * @deprecated Use {@link nextChange}. Will be removed in 0.5.0.
260
+ */
261
+ async nextEvent() {
262
+ if (!this.deprecatedNextEventWarned) {
263
+ this.deprecatedNextEventWarned = true;
264
+ logging_1.logger.warn('[deprecated] SessionLifecycleWatcher.nextEvent() — use nextChange() instead');
265
+ }
266
+ return this.nextChange();
267
+ }
208
268
  }
209
269
  exports.SessionLifecycleWatcher = SessionLifecycleWatcher;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "macp-sdk-typescript",
3
- "version": "0.2.2",
3
+ "version": "0.4.0",
4
4
  "description": "TypeScript SDK for the MACP Rust runtime",
5
5
  "license": "Apache-2.0",
6
6
  "type": "commonjs",
@@ -55,7 +55,7 @@
55
55
  "dependencies": {
56
56
  "@grpc/grpc-js": "^1.14.3",
57
57
  "@grpc/proto-loader": "^0.8.0",
58
- "@multiagentcoordinationprotocol/proto": "^0.1.1",
58
+ "@multiagentcoordinationprotocol/proto": "^0.1.3",
59
59
  "protobufjs": "^8.0.0"
60
60
  },
61
61
  "devDependencies": {