macp-sdk-typescript 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +73 -0
- package/LICENSE +201 -0
- package/README.md +307 -0
- package/dist/agent/dispatcher.d.ts +16 -0
- package/dist/agent/dispatcher.js +61 -0
- package/dist/agent/index.d.ts +6 -0
- package/dist/agent/index.js +25 -0
- package/dist/agent/participant.d.ts +38 -0
- package/dist/agent/participant.js +221 -0
- package/dist/agent/runner.d.ts +18 -0
- package/dist/agent/runner.js +84 -0
- package/dist/agent/strategies.d.ts +40 -0
- package/dist/agent/strategies.js +106 -0
- package/dist/agent/transports.d.ts +35 -0
- package/dist/agent/transports.js +125 -0
- package/dist/agent/types.d.ts +72 -0
- package/dist/agent/types.js +2 -0
- package/dist/auth.d.ts +52 -0
- package/dist/auth.js +112 -0
- package/dist/client.d.ts +171 -0
- package/dist/client.js +384 -0
- package/dist/constants.d.ts +26 -0
- package/dist/constants.js +29 -0
- package/dist/decision.d.ts +65 -0
- package/dist/decision.js +148 -0
- package/dist/envelope.d.ts +50 -0
- package/dist/envelope.js +97 -0
- package/dist/errors.d.ts +44 -0
- package/dist/errors.js +114 -0
- package/dist/handoff.d.ts +73 -0
- package/dist/handoff.js +160 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +57 -0
- package/dist/policy.d.ts +76 -0
- package/dist/policy.js +118 -0
- package/dist/projections/decision.d.ts +49 -0
- package/dist/projections/decision.js +132 -0
- package/dist/projections/handoff.d.ts +28 -0
- package/dist/projections/handoff.js +111 -0
- package/dist/projections/proposal.d.ts +40 -0
- package/dist/projections/proposal.js +123 -0
- package/dist/projections/quorum.d.ts +36 -0
- package/dist/projections/quorum.js +107 -0
- package/dist/projections/task.d.ts +54 -0
- package/dist/projections/task.js +127 -0
- package/dist/projections.d.ts +5 -0
- package/dist/projections.js +21 -0
- package/dist/proposal.d.ts +69 -0
- package/dist/proposal.js +155 -0
- package/dist/proto-registry.d.ts +12 -0
- package/dist/proto-registry.js +165 -0
- package/dist/quorum.d.ts +65 -0
- package/dist/quorum.js +144 -0
- package/dist/retry.d.ts +14 -0
- package/dist/retry.js +48 -0
- package/dist/task.d.ts +73 -0
- package/dist/task.js +166 -0
- package/dist/types.d.ts +263 -0
- package/dist/types.js +2 -0
- package/dist/validation.d.ts +17 -0
- package/dist/validation.js +92 -0
- package/dist/watchers.d.ts +47 -0
- package/dist/watchers.js +176 -0
- package/package.json +72 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { CommitmentPayload, Envelope, Root, SessionStartPayload } from './types';
|
|
2
|
+
export declare function newSessionId(): string;
|
|
3
|
+
export declare function newMessageId(): string;
|
|
4
|
+
export declare function newCommitmentId(): string;
|
|
5
|
+
export declare function nowUnixMs(): number;
|
|
6
|
+
export declare function encodeContext(context?: Buffer | string | Record<string, unknown>): Buffer;
|
|
7
|
+
export declare function buildSessionStartPayload(input: {
|
|
8
|
+
intent: string;
|
|
9
|
+
participants: string[];
|
|
10
|
+
ttlMs: number;
|
|
11
|
+
modeVersion?: string;
|
|
12
|
+
configurationVersion?: string;
|
|
13
|
+
policyVersion?: string;
|
|
14
|
+
context?: Buffer | string | Record<string, unknown>;
|
|
15
|
+
roots?: Root[];
|
|
16
|
+
}): SessionStartPayload;
|
|
17
|
+
export declare function inferOutcomePositive(action: string): boolean;
|
|
18
|
+
export declare function buildCommitmentPayload(input: {
|
|
19
|
+
action: string;
|
|
20
|
+
authorityScope: string;
|
|
21
|
+
reason: string;
|
|
22
|
+
commitmentId?: string;
|
|
23
|
+
modeVersion?: string;
|
|
24
|
+
configurationVersion?: string;
|
|
25
|
+
policyVersion?: string;
|
|
26
|
+
outcomePositive?: boolean;
|
|
27
|
+
}): CommitmentPayload;
|
|
28
|
+
export declare function buildRoot(uri: string, name?: string): Root;
|
|
29
|
+
/**
|
|
30
|
+
* Type-erasure helper for protobuf payload encoders.
|
|
31
|
+
*
|
|
32
|
+
* `ProtoRegistry.encodeKnownPayload(mode, messageType, value)` accepts a
|
|
33
|
+
* `Record<string, unknown>` because it does reflective field lookup. Our
|
|
34
|
+
* mode-specific payload interfaces (e.g. `DecisionProposalPayload`) carry
|
|
35
|
+
* stricter field types, so every call site otherwise ends up with
|
|
36
|
+
* `input as unknown as Record<string, unknown>`. Centralising the coercion
|
|
37
|
+
* keeps the `as unknown as` explanation in one place — the helper never
|
|
38
|
+
* narrows, it only erases.
|
|
39
|
+
*/
|
|
40
|
+
export declare function toProtoPayload<T extends object>(input: T): Record<string, unknown>;
|
|
41
|
+
export declare function buildEnvelope(input: {
|
|
42
|
+
mode: string;
|
|
43
|
+
messageType: string;
|
|
44
|
+
sessionId: string;
|
|
45
|
+
payload: Buffer;
|
|
46
|
+
sender?: string;
|
|
47
|
+
messageId?: string;
|
|
48
|
+
macpVersion?: string;
|
|
49
|
+
timestampUnixMs?: string | number;
|
|
50
|
+
}): Envelope;
|
package/dist/envelope.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.newSessionId = newSessionId;
|
|
4
|
+
exports.newMessageId = newMessageId;
|
|
5
|
+
exports.newCommitmentId = newCommitmentId;
|
|
6
|
+
exports.nowUnixMs = nowUnixMs;
|
|
7
|
+
exports.encodeContext = encodeContext;
|
|
8
|
+
exports.buildSessionStartPayload = buildSessionStartPayload;
|
|
9
|
+
exports.inferOutcomePositive = inferOutcomePositive;
|
|
10
|
+
exports.buildCommitmentPayload = buildCommitmentPayload;
|
|
11
|
+
exports.buildRoot = buildRoot;
|
|
12
|
+
exports.toProtoPayload = toProtoPayload;
|
|
13
|
+
exports.buildEnvelope = buildEnvelope;
|
|
14
|
+
const node_crypto_1 = require("node:crypto");
|
|
15
|
+
const constants_1 = require("./constants");
|
|
16
|
+
function newSessionId() {
|
|
17
|
+
return (0, node_crypto_1.randomUUID)();
|
|
18
|
+
}
|
|
19
|
+
function newMessageId() {
|
|
20
|
+
return (0, node_crypto_1.randomUUID)();
|
|
21
|
+
}
|
|
22
|
+
function newCommitmentId() {
|
|
23
|
+
return (0, node_crypto_1.randomUUID)();
|
|
24
|
+
}
|
|
25
|
+
function nowUnixMs() {
|
|
26
|
+
return Date.now();
|
|
27
|
+
}
|
|
28
|
+
function encodeContext(context) {
|
|
29
|
+
if (!context)
|
|
30
|
+
return Buffer.alloc(0);
|
|
31
|
+
if (Buffer.isBuffer(context))
|
|
32
|
+
return context;
|
|
33
|
+
if (typeof context === 'string')
|
|
34
|
+
return Buffer.from(context, 'utf8');
|
|
35
|
+
return Buffer.from(JSON.stringify(context), 'utf8');
|
|
36
|
+
}
|
|
37
|
+
function buildSessionStartPayload(input) {
|
|
38
|
+
return {
|
|
39
|
+
intent: input.intent,
|
|
40
|
+
participants: input.participants,
|
|
41
|
+
modeVersion: input.modeVersion ?? constants_1.DEFAULT_MODE_VERSION,
|
|
42
|
+
configurationVersion: input.configurationVersion ?? constants_1.DEFAULT_CONFIGURATION_VERSION,
|
|
43
|
+
policyVersion: input.policyVersion ?? constants_1.DEFAULT_POLICY_VERSION,
|
|
44
|
+
ttlMs: input.ttlMs,
|
|
45
|
+
context: encodeContext(input.context),
|
|
46
|
+
roots: input.roots ?? [],
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
const NEGATIVE_SUFFIXES = ['rejected', 'failed', 'declined'];
|
|
50
|
+
function inferOutcomePositive(action) {
|
|
51
|
+
const lower = action.toLowerCase();
|
|
52
|
+
if (NEGATIVE_SUFFIXES.some((s) => lower.endsWith(s)))
|
|
53
|
+
return false;
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
function buildCommitmentPayload(input) {
|
|
57
|
+
return {
|
|
58
|
+
commitmentId: input.commitmentId ?? newCommitmentId(),
|
|
59
|
+
action: input.action,
|
|
60
|
+
authorityScope: input.authorityScope,
|
|
61
|
+
reason: input.reason,
|
|
62
|
+
modeVersion: input.modeVersion ?? constants_1.DEFAULT_MODE_VERSION,
|
|
63
|
+
configurationVersion: input.configurationVersion ?? constants_1.DEFAULT_CONFIGURATION_VERSION,
|
|
64
|
+
policyVersion: input.policyVersion ?? constants_1.DEFAULT_POLICY_VERSION,
|
|
65
|
+
outcomePositive: input.outcomePositive ?? inferOutcomePositive(input.action),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function buildRoot(uri, name = '') {
|
|
69
|
+
return { uri, name };
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Type-erasure helper for protobuf payload encoders.
|
|
73
|
+
*
|
|
74
|
+
* `ProtoRegistry.encodeKnownPayload(mode, messageType, value)` accepts a
|
|
75
|
+
* `Record<string, unknown>` because it does reflective field lookup. Our
|
|
76
|
+
* mode-specific payload interfaces (e.g. `DecisionProposalPayload`) carry
|
|
77
|
+
* stricter field types, so every call site otherwise ends up with
|
|
78
|
+
* `input as unknown as Record<string, unknown>`. Centralising the coercion
|
|
79
|
+
* keeps the `as unknown as` explanation in one place — the helper never
|
|
80
|
+
* narrows, it only erases.
|
|
81
|
+
*/
|
|
82
|
+
function toProtoPayload(input) {
|
|
83
|
+
return input;
|
|
84
|
+
}
|
|
85
|
+
function buildEnvelope(input) {
|
|
86
|
+
const ts = input.timestampUnixMs ?? nowUnixMs();
|
|
87
|
+
return {
|
|
88
|
+
macpVersion: input.macpVersion ?? constants_1.MACP_VERSION,
|
|
89
|
+
mode: input.mode,
|
|
90
|
+
messageType: input.messageType,
|
|
91
|
+
messageId: input.messageId ?? newMessageId(),
|
|
92
|
+
sessionId: input.sessionId,
|
|
93
|
+
sender: input.sender ?? '',
|
|
94
|
+
timestampUnixMs: String(ts),
|
|
95
|
+
payload: input.payload,
|
|
96
|
+
};
|
|
97
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { Ack } from './types';
|
|
2
|
+
export declare class MacpSdkError extends Error {
|
|
3
|
+
constructor(message: string);
|
|
4
|
+
}
|
|
5
|
+
export declare class MacpTransportError extends MacpSdkError {
|
|
6
|
+
constructor(message: string);
|
|
7
|
+
}
|
|
8
|
+
export declare class MacpAckError extends MacpSdkError {
|
|
9
|
+
readonly ack: Ack;
|
|
10
|
+
/** Optional gRPC trailing metadata for extracting additional error details. */
|
|
11
|
+
readonly grpcMetadata?: Array<{
|
|
12
|
+
key: string;
|
|
13
|
+
value: string | Buffer;
|
|
14
|
+
}>;
|
|
15
|
+
constructor(ack: Ack, grpcMetadata?: Array<{
|
|
16
|
+
key: string;
|
|
17
|
+
value: string | Buffer;
|
|
18
|
+
}>);
|
|
19
|
+
/** Extract structured denial reasons from the ACK error details and/or gRPC trailing metadata. */
|
|
20
|
+
get reasons(): string[];
|
|
21
|
+
private _parseAckReasons;
|
|
22
|
+
private _parseGrpcMetadataReasons;
|
|
23
|
+
}
|
|
24
|
+
export declare class MacpSessionError extends MacpSdkError {
|
|
25
|
+
constructor(message: string);
|
|
26
|
+
}
|
|
27
|
+
export declare class MacpTimeoutError extends MacpTransportError {
|
|
28
|
+
constructor(message: string);
|
|
29
|
+
}
|
|
30
|
+
export declare class MacpRetryError extends MacpTransportError {
|
|
31
|
+
constructor(message: string);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Raised when an explicit `sender` supplied to a mode helper conflicts with
|
|
35
|
+
* the authenticated identity declared on {@link AuthConfig.expectedSender}.
|
|
36
|
+
*
|
|
37
|
+
* Surfaced by the SDK rather than the runtime so agents fail fast before
|
|
38
|
+
* forging an envelope the runtime would reject (RFC-MACP-0004 §4).
|
|
39
|
+
*/
|
|
40
|
+
export declare class MacpIdentityMismatchError extends MacpSdkError {
|
|
41
|
+
readonly expectedSender: string;
|
|
42
|
+
readonly actualSender: string;
|
|
43
|
+
constructor(expectedSender: string, actualSender: string);
|
|
44
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MacpIdentityMismatchError = exports.MacpRetryError = exports.MacpTimeoutError = exports.MacpSessionError = exports.MacpAckError = exports.MacpTransportError = exports.MacpSdkError = void 0;
|
|
4
|
+
class MacpSdkError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = 'MacpSdkError';
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.MacpSdkError = MacpSdkError;
|
|
11
|
+
class MacpTransportError extends MacpSdkError {
|
|
12
|
+
constructor(message) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = 'MacpTransportError';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.MacpTransportError = MacpTransportError;
|
|
18
|
+
class MacpAckError extends MacpSdkError {
|
|
19
|
+
ack;
|
|
20
|
+
/** Optional gRPC trailing metadata for extracting additional error details. */
|
|
21
|
+
grpcMetadata;
|
|
22
|
+
constructor(ack, grpcMetadata) {
|
|
23
|
+
super(`${ack.error?.code ?? 'UNKNOWN'}: ${ack.error?.message ?? 'runtime returned nack'}`);
|
|
24
|
+
this.name = 'MacpAckError';
|
|
25
|
+
this.ack = ack;
|
|
26
|
+
this.grpcMetadata = grpcMetadata;
|
|
27
|
+
}
|
|
28
|
+
/** Extract structured denial reasons from the ACK error details and/or gRPC trailing metadata. */
|
|
29
|
+
get reasons() {
|
|
30
|
+
// First try: parse from ack.error.details (matches Python _parse_ack_reasons)
|
|
31
|
+
const fromDetails = this._parseAckReasons();
|
|
32
|
+
if (fromDetails.length > 0)
|
|
33
|
+
return fromDetails;
|
|
34
|
+
// Second try: parse from gRPC trailing metadata (matches Python _parse_grpc_metadata_reasons)
|
|
35
|
+
return this._parseGrpcMetadataReasons();
|
|
36
|
+
}
|
|
37
|
+
_parseAckReasons() {
|
|
38
|
+
if (!this.ack.error?.details)
|
|
39
|
+
return [];
|
|
40
|
+
try {
|
|
41
|
+
const details = this.ack.error.details;
|
|
42
|
+
const raw = Buffer.isBuffer(details) ? details.toString('utf-8') : String(details);
|
|
43
|
+
const parsed = JSON.parse(raw);
|
|
44
|
+
const reasons = parsed.reasons;
|
|
45
|
+
return Array.isArray(reasons) ? reasons : [];
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
_parseGrpcMetadataReasons() {
|
|
52
|
+
if (!this.grpcMetadata)
|
|
53
|
+
return [];
|
|
54
|
+
try {
|
|
55
|
+
for (const item of this.grpcMetadata) {
|
|
56
|
+
if (item.key === 'macp-error-details-bin') {
|
|
57
|
+
const data = Buffer.isBuffer(item.value)
|
|
58
|
+
? item.value.toString('utf-8')
|
|
59
|
+
: typeof item.value === 'string'
|
|
60
|
+
? item.value
|
|
61
|
+
: Buffer.from(item.value).toString('utf-8');
|
|
62
|
+
const parsed = JSON.parse(data);
|
|
63
|
+
const reasons = parsed.reasons;
|
|
64
|
+
return Array.isArray(reasons) ? reasons : [];
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
// Intentionally swallowed — matches Python's bare except
|
|
70
|
+
}
|
|
71
|
+
return [];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
exports.MacpAckError = MacpAckError;
|
|
75
|
+
class MacpSessionError extends MacpSdkError {
|
|
76
|
+
constructor(message) {
|
|
77
|
+
super(message);
|
|
78
|
+
this.name = 'MacpSessionError';
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.MacpSessionError = MacpSessionError;
|
|
82
|
+
class MacpTimeoutError extends MacpTransportError {
|
|
83
|
+
constructor(message) {
|
|
84
|
+
super(message);
|
|
85
|
+
this.name = 'MacpTimeoutError';
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.MacpTimeoutError = MacpTimeoutError;
|
|
89
|
+
class MacpRetryError extends MacpTransportError {
|
|
90
|
+
constructor(message) {
|
|
91
|
+
super(message);
|
|
92
|
+
this.name = 'MacpRetryError';
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
exports.MacpRetryError = MacpRetryError;
|
|
96
|
+
/**
|
|
97
|
+
* Raised when an explicit `sender` supplied to a mode helper conflicts with
|
|
98
|
+
* the authenticated identity declared on {@link AuthConfig.expectedSender}.
|
|
99
|
+
*
|
|
100
|
+
* Surfaced by the SDK rather than the runtime so agents fail fast before
|
|
101
|
+
* forging an envelope the runtime would reject (RFC-MACP-0004 §4).
|
|
102
|
+
*/
|
|
103
|
+
class MacpIdentityMismatchError extends MacpSdkError {
|
|
104
|
+
expectedSender;
|
|
105
|
+
actualSender;
|
|
106
|
+
constructor(expectedSender, actualSender) {
|
|
107
|
+
super(`envelope sender "${actualSender}" does not match the authenticated identity "${expectedSender}"; ` +
|
|
108
|
+
'remove the explicit sender or reconfigure Auth.bearer(token, { expectedSender })');
|
|
109
|
+
this.name = 'MacpIdentityMismatchError';
|
|
110
|
+
this.expectedSender = expectedSender;
|
|
111
|
+
this.actualSender = actualSender;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
exports.MacpIdentityMismatchError = MacpIdentityMismatchError;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { type AuthConfig } from './auth';
|
|
2
|
+
import type { MacpClient, MacpStream } from './client';
|
|
3
|
+
import { HandoffProjection } from './projections/handoff';
|
|
4
|
+
import type { Ack, HandoffAcceptPayload, HandoffContextPayload, HandoffDeclinePayload, HandoffOfferPayload, SessionMetadata } from './types';
|
|
5
|
+
interface HandoffSessionOptions {
|
|
6
|
+
sessionId?: string;
|
|
7
|
+
modeVersion?: string;
|
|
8
|
+
configurationVersion?: string;
|
|
9
|
+
policyVersion?: string;
|
|
10
|
+
auth?: AuthConfig;
|
|
11
|
+
}
|
|
12
|
+
export declare class HandoffSession {
|
|
13
|
+
readonly client: MacpClient;
|
|
14
|
+
readonly sessionId: string;
|
|
15
|
+
readonly modeVersion: string;
|
|
16
|
+
readonly configurationVersion: string;
|
|
17
|
+
readonly policyVersion: string;
|
|
18
|
+
readonly auth?: AuthConfig;
|
|
19
|
+
readonly projection: HandoffProjection;
|
|
20
|
+
constructor(client: MacpClient, options?: HandoffSessionOptions);
|
|
21
|
+
private senderFor;
|
|
22
|
+
private sendAndTrack;
|
|
23
|
+
start(input: {
|
|
24
|
+
intent: string;
|
|
25
|
+
participants: string[];
|
|
26
|
+
ttlMs: number;
|
|
27
|
+
context?: Buffer | string | Record<string, unknown>;
|
|
28
|
+
roots?: {
|
|
29
|
+
uri: string;
|
|
30
|
+
name?: string;
|
|
31
|
+
}[];
|
|
32
|
+
sender?: string;
|
|
33
|
+
}): Promise<Ack>;
|
|
34
|
+
offer(input: HandoffOfferPayload & {
|
|
35
|
+
sender?: string;
|
|
36
|
+
auth?: AuthConfig;
|
|
37
|
+
}): Promise<Ack>;
|
|
38
|
+
addContext(input: HandoffContextPayload & {
|
|
39
|
+
sender?: string;
|
|
40
|
+
auth?: AuthConfig;
|
|
41
|
+
}): Promise<Ack>;
|
|
42
|
+
/**
|
|
43
|
+
* @deprecated Use {@link addContext} instead. Scheduled for removal in 0.4.0.
|
|
44
|
+
* First call logs a one-shot warning to guide migrations without spamming long-lived processes.
|
|
45
|
+
*/
|
|
46
|
+
sendContext(input: HandoffContextPayload & {
|
|
47
|
+
sender?: string;
|
|
48
|
+
auth?: AuthConfig;
|
|
49
|
+
}): Promise<Ack>;
|
|
50
|
+
acceptHandoff(input: HandoffAcceptPayload & {
|
|
51
|
+
sender?: string;
|
|
52
|
+
auth?: AuthConfig;
|
|
53
|
+
}): Promise<Ack>;
|
|
54
|
+
decline(input: HandoffDeclinePayload & {
|
|
55
|
+
sender?: string;
|
|
56
|
+
auth?: AuthConfig;
|
|
57
|
+
}): Promise<Ack>;
|
|
58
|
+
commit(input: {
|
|
59
|
+
action: string;
|
|
60
|
+
authorityScope: string;
|
|
61
|
+
reason: string;
|
|
62
|
+
commitmentId?: string;
|
|
63
|
+
outcomePositive?: boolean;
|
|
64
|
+
sender?: string;
|
|
65
|
+
auth?: AuthConfig;
|
|
66
|
+
}): Promise<Ack>;
|
|
67
|
+
metadata(auth?: AuthConfig): Promise<{
|
|
68
|
+
metadata: SessionMetadata;
|
|
69
|
+
}>;
|
|
70
|
+
cancel(reason?: string, auth?: AuthConfig): Promise<Ack>;
|
|
71
|
+
openStream(auth?: AuthConfig): MacpStream;
|
|
72
|
+
}
|
|
73
|
+
export {};
|
package/dist/handoff.js
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HandoffSession = void 0;
|
|
4
|
+
const auth_1 = require("./auth");
|
|
5
|
+
const constants_1 = require("./constants");
|
|
6
|
+
const envelope_1 = require("./envelope");
|
|
7
|
+
const handoff_1 = require("./projections/handoff");
|
|
8
|
+
const validation_1 = require("./validation");
|
|
9
|
+
let sendContextWarned = false;
|
|
10
|
+
function warnSendContextOnce() {
|
|
11
|
+
if (sendContextWarned)
|
|
12
|
+
return;
|
|
13
|
+
sendContextWarned = true;
|
|
14
|
+
// eslint-disable-next-line no-console
|
|
15
|
+
console.warn('[macp-sdk-typescript] HandoffSession.sendContext() is deprecated and will be removed in 0.4.0. Use addContext() instead.');
|
|
16
|
+
}
|
|
17
|
+
class HandoffSession {
|
|
18
|
+
client;
|
|
19
|
+
sessionId;
|
|
20
|
+
modeVersion;
|
|
21
|
+
configurationVersion;
|
|
22
|
+
policyVersion;
|
|
23
|
+
auth;
|
|
24
|
+
projection = new handoff_1.HandoffProjection();
|
|
25
|
+
constructor(client, options = {}) {
|
|
26
|
+
this.client = client;
|
|
27
|
+
if (options.sessionId)
|
|
28
|
+
(0, validation_1.validateSessionId)(options.sessionId);
|
|
29
|
+
this.sessionId = options.sessionId ?? (0, envelope_1.newSessionId)();
|
|
30
|
+
this.modeVersion = options.modeVersion ?? constants_1.DEFAULT_MODE_VERSION;
|
|
31
|
+
this.configurationVersion = options.configurationVersion ?? constants_1.DEFAULT_CONFIGURATION_VERSION;
|
|
32
|
+
this.policyVersion = options.policyVersion ?? constants_1.DEFAULT_POLICY_VERSION;
|
|
33
|
+
this.auth = options.auth;
|
|
34
|
+
}
|
|
35
|
+
senderFor(sender, auth) {
|
|
36
|
+
const effectiveAuth = auth ?? this.auth ?? this.client.auth;
|
|
37
|
+
(0, auth_1.assertSenderMatchesIdentity)(effectiveAuth, sender);
|
|
38
|
+
return sender ?? (0, auth_1.authSender)(effectiveAuth) ?? '';
|
|
39
|
+
}
|
|
40
|
+
async sendAndTrack(envelope, auth) {
|
|
41
|
+
const ack = await this.client.send(envelope, { auth: auth ?? this.auth });
|
|
42
|
+
if (ack.ok)
|
|
43
|
+
this.projection.applyEnvelope(envelope, this.client.protoRegistry);
|
|
44
|
+
return ack;
|
|
45
|
+
}
|
|
46
|
+
async start(input) {
|
|
47
|
+
(0, validation_1.validateSessionStart)({
|
|
48
|
+
intent: input.intent,
|
|
49
|
+
participants: input.participants,
|
|
50
|
+
ttlMs: input.ttlMs,
|
|
51
|
+
modeVersion: this.modeVersion,
|
|
52
|
+
configurationVersion: this.configurationVersion,
|
|
53
|
+
});
|
|
54
|
+
const payload = (0, envelope_1.buildSessionStartPayload)({
|
|
55
|
+
intent: input.intent,
|
|
56
|
+
participants: input.participants,
|
|
57
|
+
ttlMs: input.ttlMs,
|
|
58
|
+
context: input.context,
|
|
59
|
+
roots: input.roots,
|
|
60
|
+
modeVersion: this.modeVersion,
|
|
61
|
+
configurationVersion: this.configurationVersion,
|
|
62
|
+
policyVersion: this.policyVersion,
|
|
63
|
+
});
|
|
64
|
+
const envelope = (0, envelope_1.buildEnvelope)({
|
|
65
|
+
mode: constants_1.MODE_HANDOFF,
|
|
66
|
+
messageType: 'SessionStart',
|
|
67
|
+
sessionId: this.sessionId,
|
|
68
|
+
sender: this.senderFor(input.sender),
|
|
69
|
+
payload: this.client.protoRegistry.encodeKnownPayload(constants_1.MODE_HANDOFF, 'SessionStart', (0, envelope_1.toProtoPayload)(payload)),
|
|
70
|
+
});
|
|
71
|
+
return this.sendAndTrack(envelope, this.auth);
|
|
72
|
+
}
|
|
73
|
+
async offer(input) {
|
|
74
|
+
(0, validation_1.validateRequiredField)('handoffId', input.handoffId);
|
|
75
|
+
(0, validation_1.validateRequiredField)('targetParticipant', input.targetParticipant);
|
|
76
|
+
const offerInput = { ...input, scope: input.scope ?? '' };
|
|
77
|
+
const envelope = (0, envelope_1.buildEnvelope)({
|
|
78
|
+
mode: constants_1.MODE_HANDOFF,
|
|
79
|
+
messageType: 'HandoffOffer',
|
|
80
|
+
sessionId: this.sessionId,
|
|
81
|
+
sender: this.senderFor(input.sender, input.auth),
|
|
82
|
+
payload: this.client.protoRegistry.encodeKnownPayload(constants_1.MODE_HANDOFF, 'HandoffOffer', (0, envelope_1.toProtoPayload)(offerInput)),
|
|
83
|
+
});
|
|
84
|
+
return this.sendAndTrack(envelope, input.auth);
|
|
85
|
+
}
|
|
86
|
+
async addContext(input) {
|
|
87
|
+
(0, validation_1.validateRequiredField)('handoffId', input.handoffId);
|
|
88
|
+
const envelope = (0, envelope_1.buildEnvelope)({
|
|
89
|
+
mode: constants_1.MODE_HANDOFF,
|
|
90
|
+
messageType: 'HandoffContext',
|
|
91
|
+
sessionId: this.sessionId,
|
|
92
|
+
sender: this.senderFor(input.sender, input.auth),
|
|
93
|
+
payload: this.client.protoRegistry.encodeKnownPayload(constants_1.MODE_HANDOFF, 'HandoffContext', (0, envelope_1.toProtoPayload)(input)),
|
|
94
|
+
});
|
|
95
|
+
return this.sendAndTrack(envelope, input.auth);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* @deprecated Use {@link addContext} instead. Scheduled for removal in 0.4.0.
|
|
99
|
+
* First call logs a one-shot warning to guide migrations without spamming long-lived processes.
|
|
100
|
+
*/
|
|
101
|
+
async sendContext(input) {
|
|
102
|
+
warnSendContextOnce();
|
|
103
|
+
return this.addContext(input);
|
|
104
|
+
}
|
|
105
|
+
async acceptHandoff(input) {
|
|
106
|
+
(0, validation_1.validateRequiredField)('handoffId', input.handoffId);
|
|
107
|
+
const envelope = (0, envelope_1.buildEnvelope)({
|
|
108
|
+
mode: constants_1.MODE_HANDOFF,
|
|
109
|
+
messageType: 'HandoffAccept',
|
|
110
|
+
sessionId: this.sessionId,
|
|
111
|
+
sender: this.senderFor(input.sender, input.auth),
|
|
112
|
+
payload: this.client.protoRegistry.encodeKnownPayload(constants_1.MODE_HANDOFF, 'HandoffAccept', (0, envelope_1.toProtoPayload)(input)),
|
|
113
|
+
});
|
|
114
|
+
return this.sendAndTrack(envelope, input.auth);
|
|
115
|
+
}
|
|
116
|
+
async decline(input) {
|
|
117
|
+
(0, validation_1.validateRequiredField)('handoffId', input.handoffId);
|
|
118
|
+
const envelope = (0, envelope_1.buildEnvelope)({
|
|
119
|
+
mode: constants_1.MODE_HANDOFF,
|
|
120
|
+
messageType: 'HandoffDecline',
|
|
121
|
+
sessionId: this.sessionId,
|
|
122
|
+
sender: this.senderFor(input.sender, input.auth),
|
|
123
|
+
payload: this.client.protoRegistry.encodeKnownPayload(constants_1.MODE_HANDOFF, 'HandoffDecline', (0, envelope_1.toProtoPayload)(input)),
|
|
124
|
+
});
|
|
125
|
+
return this.sendAndTrack(envelope, input.auth);
|
|
126
|
+
}
|
|
127
|
+
async commit(input) {
|
|
128
|
+
const payload = (0, envelope_1.buildCommitmentPayload)({
|
|
129
|
+
action: input.action,
|
|
130
|
+
authorityScope: input.authorityScope,
|
|
131
|
+
reason: input.reason,
|
|
132
|
+
commitmentId: input.commitmentId,
|
|
133
|
+
outcomePositive: input.outcomePositive,
|
|
134
|
+
modeVersion: this.modeVersion,
|
|
135
|
+
configurationVersion: this.configurationVersion,
|
|
136
|
+
policyVersion: this.policyVersion,
|
|
137
|
+
});
|
|
138
|
+
const envelope = (0, envelope_1.buildEnvelope)({
|
|
139
|
+
mode: constants_1.MODE_HANDOFF,
|
|
140
|
+
messageType: 'Commitment',
|
|
141
|
+
sessionId: this.sessionId,
|
|
142
|
+
sender: this.senderFor(input.sender, input.auth),
|
|
143
|
+
payload: this.client.protoRegistry.encodeKnownPayload(constants_1.MODE_HANDOFF, 'Commitment', (0, envelope_1.toProtoPayload)(payload)),
|
|
144
|
+
});
|
|
145
|
+
return this.sendAndTrack(envelope, input.auth);
|
|
146
|
+
}
|
|
147
|
+
metadata(auth) {
|
|
148
|
+
return this.client.getSession(this.sessionId, { auth: auth ?? this.auth });
|
|
149
|
+
}
|
|
150
|
+
async cancel(reason = '', auth) {
|
|
151
|
+
return this.client.cancelSession(this.sessionId, reason, {
|
|
152
|
+
auth: auth ?? this.auth,
|
|
153
|
+
raiseOnNack: true,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
openStream(auth) {
|
|
157
|
+
return this.client.openStream({ auth: auth ?? this.auth });
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
exports.HandoffSession = HandoffSession;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export * from './auth';
|
|
2
|
+
export * from './client';
|
|
3
|
+
export * from './constants';
|
|
4
|
+
export * from './decision';
|
|
5
|
+
export * from './envelope';
|
|
6
|
+
export * from './errors';
|
|
7
|
+
export * from './handoff';
|
|
8
|
+
export * from './policy';
|
|
9
|
+
export * from './projections';
|
|
10
|
+
export * from './proposal';
|
|
11
|
+
export * from './proto-registry';
|
|
12
|
+
export * from './retry';
|
|
13
|
+
export * from './quorum';
|
|
14
|
+
export * from './task';
|
|
15
|
+
export * from './types';
|
|
16
|
+
export * from './validation';
|
|
17
|
+
export * from './watchers';
|
|
18
|
+
export * as agent from './agent';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
19
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
20
|
+
};
|
|
21
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
22
|
+
var ownKeys = function(o) {
|
|
23
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
24
|
+
var ar = [];
|
|
25
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
26
|
+
return ar;
|
|
27
|
+
};
|
|
28
|
+
return ownKeys(o);
|
|
29
|
+
};
|
|
30
|
+
return function (mod) {
|
|
31
|
+
if (mod && mod.__esModule) return mod;
|
|
32
|
+
var result = {};
|
|
33
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
34
|
+
__setModuleDefault(result, mod);
|
|
35
|
+
return result;
|
|
36
|
+
};
|
|
37
|
+
})();
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.agent = void 0;
|
|
40
|
+
__exportStar(require("./auth"), exports);
|
|
41
|
+
__exportStar(require("./client"), exports);
|
|
42
|
+
__exportStar(require("./constants"), exports);
|
|
43
|
+
__exportStar(require("./decision"), exports);
|
|
44
|
+
__exportStar(require("./envelope"), exports);
|
|
45
|
+
__exportStar(require("./errors"), exports);
|
|
46
|
+
__exportStar(require("./handoff"), exports);
|
|
47
|
+
__exportStar(require("./policy"), exports);
|
|
48
|
+
__exportStar(require("./projections"), exports);
|
|
49
|
+
__exportStar(require("./proposal"), exports);
|
|
50
|
+
__exportStar(require("./proto-registry"), exports);
|
|
51
|
+
__exportStar(require("./retry"), exports);
|
|
52
|
+
__exportStar(require("./quorum"), exports);
|
|
53
|
+
__exportStar(require("./task"), exports);
|
|
54
|
+
__exportStar(require("./types"), exports);
|
|
55
|
+
__exportStar(require("./validation"), exports);
|
|
56
|
+
__exportStar(require("./watchers"), exports);
|
|
57
|
+
exports.agent = __importStar(require("./agent"));
|