@atalk/sdk 0.1.0-alpha.10 → 0.1.0-alpha.12

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.
@@ -0,0 +1,329 @@
1
+ import { decryptMandateTerms, type AppendWorkroomEvent, type AttachmentDescriptor, type MandateUseDecision, type MandateUseRequest, type PublicPeer, type SignedMandate, type SignedMandateCommitment, type SignedMandateRevocation, type SignedWorkroomApprovalDecision, type SignedWorkroomReceipt, type Workroom, type WorkroomContentPayload, type WorkroomDescriptor, type WorkroomEncryptedEnvelope, type WorkroomEventMembershipSnapshot, type WorkroomMember, type WorkroomRoutingMatch, type WorkroomThread } from "@atalk/protocol";
2
+ import type { AgentCredentials } from "./credential-store.js";
3
+ import type { AgentAttachmentFileInput, AgentAttachmentInput, AttachmentTransferOptions } from "./agent.js";
4
+ import type { AgentRuntimeState, WorkroomEventFailureState } from "./runtime-state-store.js";
5
+ interface WorkroomMembership extends WorkroomMember {
6
+ id: string;
7
+ workroomId: string;
8
+ addedByPeerId: string;
9
+ idempotencyKey: string;
10
+ }
11
+ interface WorkroomRecord extends Workroom {
12
+ currentKeyEpoch: number;
13
+ descriptorHash: string;
14
+ idempotencyKey: string;
15
+ }
16
+ interface WorkroomThreadRecord extends WorkroomThread {
17
+ headerHash?: string;
18
+ idempotencyKey: string;
19
+ }
20
+ interface WorkroomMandateRecord {
21
+ mandateId: string;
22
+ revision: number;
23
+ workroomId?: string;
24
+ principalPeerId: string;
25
+ actorPeerId: string;
26
+ issuedByPeerId: string;
27
+ validFrom: string;
28
+ validUntil: string;
29
+ termsHash: string;
30
+ encryptedTermsHash: string;
31
+ encryptedTermsEnvelope: Parameters<typeof decryptMandateTerms>[0]["envelope"];
32
+ signedCommitment: SignedMandateCommitment;
33
+ issuerSigningPublicKey: string;
34
+ }
35
+ interface WorkroomMandateView {
36
+ mandate: WorkroomMandateRecord;
37
+ revocation?: {
38
+ signedRevocation: SignedMandateRevocation;
39
+ revokerSigningPublicKey: string;
40
+ };
41
+ }
42
+ interface WorkroomApprovalDecisionRecord {
43
+ signedDecision: SignedWorkroomApprovalDecision;
44
+ signingPublicKey: string;
45
+ }
46
+ interface WorkroomApprovalView {
47
+ requestId: string;
48
+ workroomId: string;
49
+ sourceEventId: string;
50
+ requestedByPeerId: string;
51
+ requiredApprovals: number;
52
+ eligiblePeerIds: string[];
53
+ requestCiphertextHash: string;
54
+ requestEnvelope: WorkroomEncryptedEnvelope;
55
+ expiresAt?: string;
56
+ status: "pending" | "approved" | "rejected" | "expired";
57
+ decisions: WorkroomApprovalDecisionRecord[];
58
+ }
59
+ export interface WorkroomDetail {
60
+ workroom: WorkroomRecord;
61
+ /** Locally verified and decrypted; never returned in plaintext by the relay. */
62
+ descriptor: WorkroomDescriptor;
63
+ membership: WorkroomMembership;
64
+ members: Array<{
65
+ membership: WorkroomMembership;
66
+ peer: PublicPeer | null;
67
+ }>;
68
+ threads: WorkroomThreadRecord[];
69
+ latestMandates: WorkroomMandateView[];
70
+ approvals: WorkroomApprovalView[];
71
+ latestReceiptHash: string | null;
72
+ events?: WorkroomEventRecord[];
73
+ nextAfterSequence?: number | null;
74
+ }
75
+ interface WorkroomEventRecord {
76
+ sequence: number;
77
+ event: AppendWorkroomEvent;
78
+ membershipSnapshot?: WorkroomEventMembershipSnapshot[];
79
+ projection?: WorkroomEventProjection;
80
+ }
81
+ export interface DecryptedWorkroomEvent extends WorkroomEventRecord {
82
+ actor: PublicPeer;
83
+ content: WorkroomContentPayload;
84
+ /** Exact autonomous context derived after canonical routing validation. */
85
+ routing: WorkroomRoutingMatch;
86
+ /** Compatibility alias for routing.directedToMe. */
87
+ directedToMe: boolean;
88
+ }
89
+ export type WorkroomEventProjection = {
90
+ kind: "plan";
91
+ id: string;
92
+ version: number;
93
+ } | {
94
+ kind: "artifact_version";
95
+ id: string;
96
+ artifactId: string;
97
+ artifactVersion: number;
98
+ attachmentIds: string[];
99
+ } | {
100
+ kind: "deliverable";
101
+ id: string;
102
+ artifactId: string;
103
+ artifactVersionId: string;
104
+ } | {
105
+ kind: "cost";
106
+ id: string;
107
+ } | {
108
+ kind: "approval_request";
109
+ id: string;
110
+ requiredApprovals: number;
111
+ eligiblePeerIds: string[];
112
+ expiresAt?: string;
113
+ };
114
+ export interface WorkroomPublishOptions {
115
+ eventId?: string;
116
+ idempotencyKey?: string;
117
+ projection?: WorkroomEventProjection;
118
+ maxAttempts?: number;
119
+ }
120
+ export interface WorkroomPollOptions {
121
+ limit?: number;
122
+ signal?: AbortSignal;
123
+ maxAttempts?: number;
124
+ /** Processing failures are retried durably before the event is quarantined. */
125
+ maxEventFailures?: number;
126
+ /** Observability hook; cursor/dead-letter state is persisted before invocation. */
127
+ onEventQuarantined?: (failure: WorkroomEventFailureState) => void | Promise<void>;
128
+ }
129
+ export interface WorkroomEventPage {
130
+ events: DecryptedWorkroomEvent[];
131
+ nextAfterSequence: number | null;
132
+ }
133
+ export interface MandatedActionInput {
134
+ workroomId: string;
135
+ threadId: string;
136
+ /** UUID used to make approval requests, effects and receipts retry-safe. */
137
+ operationId: string;
138
+ mandateId?: string;
139
+ action: string;
140
+ rationale?: string;
141
+ summary?: string;
142
+ target?: {
143
+ type: string;
144
+ label: string;
145
+ reference?: string;
146
+ };
147
+ effect?: string;
148
+ financialImpact?: {
149
+ currency: string;
150
+ amountMinor: number;
151
+ kind: "exact" | "maximum";
152
+ };
153
+ dataCategories?: string[];
154
+ participantPeerIds?: string[];
155
+ tool?: MandateUseRequest["tool"];
156
+ dataAccesses?: MandateUseRequest["dataAccesses"];
157
+ spend?: MandateUseRequest["spend"];
158
+ spendUsedMinorByLimit?: MandateUseRequest["spendUsedMinorByLimit"];
159
+ volumeUsed?: MandateUseRequest["volumeUsed"];
160
+ volumeDelta?: MandateUseRequest["volumeDelta"];
161
+ delegationDepth?: number;
162
+ principalApprovedDelegation?: boolean;
163
+ metEndConditionIds?: string[];
164
+ }
165
+ export interface MandatedPublicationInput extends Omit<MandatedActionInput, "action"> {
166
+ payload: WorkroomContentPayload;
167
+ publish?: WorkroomPublishOptions;
168
+ }
169
+ export interface MandatedFilePublicationInput extends Omit<MandatedActionInput, "action" | "volumeDelta"> {
170
+ path: string;
171
+ name?: string;
172
+ mimeType?: string;
173
+ title?: string;
174
+ description?: string;
175
+ artifactType?: string;
176
+ artifactId?: string;
177
+ artifactVersion?: number;
178
+ mentions?: Extract<WorkroomContentPayload, {
179
+ kind: "artifact_version";
180
+ }>["mentions"];
181
+ transfer?: AttachmentTransferOptions;
182
+ }
183
+ export type MandateGuardResult = {
184
+ status: "permitted";
185
+ mandate: SignedMandate;
186
+ } | {
187
+ status: "requires_approval";
188
+ decision: Extract<MandateUseDecision, {
189
+ status: "requires_approval";
190
+ }>;
191
+ requestIds: string[];
192
+ } | {
193
+ status: "denied";
194
+ decision: Extract<MandateUseDecision, {
195
+ status: "denied";
196
+ }>;
197
+ };
198
+ export interface MandatedEffectResult<T> {
199
+ value: T;
200
+ costs?: Array<Extract<WorkroomContentPayload, {
201
+ kind: "cost";
202
+ }>>;
203
+ }
204
+ export type MandatedExecutionResult<T> = {
205
+ status: "executed";
206
+ value: T;
207
+ receipt: SignedWorkroomReceipt;
208
+ } | Exclude<MandateGuardResult, {
209
+ status: "permitted";
210
+ }>;
211
+ interface WorkroomClientDependencies {
212
+ request<T>(path: string, init?: RequestInit): Promise<T>;
213
+ credentials(): AgentCredentials;
214
+ runtimeState(): AgentRuntimeState;
215
+ mutateRuntimeState(mutator: (state: AgentRuntimeState) => void): Promise<void>;
216
+ uploadPart(scope: {
217
+ workroomId: string;
218
+ }, id: string, bytes: Uint8Array, transfer?: AttachmentTransferOptions): Promise<void>;
219
+ deletePart(id: string): Promise<void>;
220
+ downloadAttachment(descriptor: AttachmentDescriptor): Promise<Uint8Array>;
221
+ downloadAttachmentTo(descriptor: AttachmentDescriptor, path: string, transfer?: AttachmentTransferOptions): Promise<string>;
222
+ }
223
+ /** E2EE task client. The relay sees signed ciphertext and routing projections only. */
224
+ export declare class WorkroomClient {
225
+ private readonly dependencies;
226
+ private executionTail;
227
+ constructor(dependencies: WorkroomClientDependencies);
228
+ list(cursor?: string, limit?: number): Promise<{
229
+ workrooms: WorkroomDetail[];
230
+ nextCursor: string | null;
231
+ }>;
232
+ get(workroomId: string, afterSequence?: number, eventLimit?: number): Promise<WorkroomDetail>;
233
+ /**
234
+ * Polls from the last durably acknowledged sequence and invokes the agent
235
+ * handler only for events explicitly directed to this identity. Undirected
236
+ * events are still authenticated and consumed so shared Task conversation
237
+ * traffic cannot accidentally start a model turn. A handler failure does not
238
+ * advance the cursor, so the same directed event is retried after restart.
239
+ */
240
+ poll(workroomId: string, handler: (event: DecryptedWorkroomEvent) => void | Promise<void>, options?: WorkroomPollOptions): Promise<number>;
241
+ /**
242
+ * Reads every authenticated event without advancing the autonomous handler
243
+ * cursor. This is the explicit operator/audit surface; never use it as a
244
+ * model-trigger loop because it includes traffic addressed to other agents.
245
+ */
246
+ readAuditEvents(workroomId: string, afterSequence?: number, limit?: number, signal?: AbortSignal): Promise<WorkroomEventPage>;
247
+ /** Durable processing failures that were skipped so later Task work can continue. */
248
+ listQuarantinedEvents(workroomId?: string): WorkroomEventFailureState[];
249
+ /** Long-polls using the durable cursor until aborted. */
250
+ watch(workroomId: string, handler: (event: DecryptedWorkroomEvent) => void | Promise<void>, options?: WorkroomPollOptions & {
251
+ intervalMs?: number;
252
+ }): Promise<void>;
253
+ publish(workroomId: string, threadId: string, candidate: WorkroomContentPayload, options?: WorkroomPublishOptions): Promise<WorkroomEventRecord>;
254
+ message(workroomId: string, threadId: string, body: string, mentions?: Extract<WorkroomContentPayload, {
255
+ kind: "message";
256
+ }>["mentions"]): Promise<WorkroomEventRecord>;
257
+ activity(workroomId: string, threadId: string, activityType: string, summary: string, mentions?: Extract<WorkroomContentPayload, {
258
+ kind: "activity";
259
+ }>["mentions"]): Promise<WorkroomEventRecord>;
260
+ plan(workroomId: string, threadId: string, payload: Omit<Extract<WorkroomContentPayload, {
261
+ kind: "plan";
262
+ }>, "version" | "kind">, options?: WorkroomPublishOptions): Promise<WorkroomEventRecord>;
263
+ artifactVersion(workroomId: string, threadId: string, payload: Omit<Extract<WorkroomContentPayload, {
264
+ kind: "artifact_version";
265
+ }>, "version" | "kind">, options?: WorkroomPublishOptions): Promise<WorkroomEventRecord>;
266
+ deliverable(workroomId: string, threadId: string, payload: Omit<Extract<WorkroomContentPayload, {
267
+ kind: "deliverable";
268
+ }>, "version" | "kind">, options?: WorkroomPublishOptions): Promise<WorkroomEventRecord>;
269
+ /**
270
+ * Preferred publication boundary for agent runtimes. It derives the same
271
+ * action names as the app, checks the current signed mandate twice, and
272
+ * records durable counters plus a signed receipt after publication.
273
+ */
274
+ publishMandated(input: MandatedPublicationInput): Promise<MandatedExecutionResult<WorkroomEventRecord>>;
275
+ /** Permission-aware local decryption for files supplied to an external agent. */
276
+ downloadAttachmentToMandated(input: Omit<MandatedActionInput, "action"> & {
277
+ descriptor: AttachmentDescriptor;
278
+ path: string;
279
+ transfer?: AttachmentTransferOptions;
280
+ }): Promise<MandatedExecutionResult<string>>;
281
+ /** Guard, upload and publish one encrypted Task file as a single runtime operation. */
282
+ submitFileMandated(input: MandatedFilePublicationInput): Promise<MandatedExecutionResult<{
283
+ descriptor: AttachmentDescriptor;
284
+ artifactId: string;
285
+ artifactVersion: number;
286
+ artifactVersionId: string;
287
+ record: WorkroomEventRecord;
288
+ }>>;
289
+ uploadAttachment(input: AgentAttachmentInput & {
290
+ workroomId: string;
291
+ }): Promise<AttachmentDescriptor>;
292
+ uploadAttachmentFile(input: AgentAttachmentFileInput & {
293
+ workroomId: string;
294
+ }): Promise<AttachmentDescriptor>;
295
+ downloadAttachment(descriptor: AttachmentDescriptor): Promise<Uint8Array>;
296
+ downloadAttachmentTo(descriptor: AttachmentDescriptor, path: string, transfer?: AttachmentTransferOptions): Promise<string>;
297
+ guardMandateUse(input: MandatedActionInput, createApproval?: boolean): Promise<MandateGuardResult>;
298
+ /**
299
+ * Revalidates immediately before the callback. Callbacks must use operationId
300
+ * as their external idempotency key so a crash cannot duplicate side effects.
301
+ */
302
+ executeMandatedAction<T>(input: MandatedActionInput, effect: (context: {
303
+ operationId: string;
304
+ mandate: SignedMandate;
305
+ }) => Promise<MandatedEffectResult<T>>): Promise<MandatedExecutionResult<T>>;
306
+ private openDetail;
307
+ private decryptEvent;
308
+ private readEventPage;
309
+ private readRawEventPage;
310
+ private quarantineLegacyEvent;
311
+ private recordEventFailure;
312
+ private clearEventFailure;
313
+ private commitEventCursor;
314
+ private openMandate;
315
+ private verifiedApprovals;
316
+ private ensureApprovalRequests;
317
+ private appendExecutionReceipt;
318
+ private withDurableUsage;
319
+ private recordDurableUsage;
320
+ private serializedExecution;
321
+ private retry;
322
+ }
323
+ /** @internal Approval identity is bound to the complete proposed operation, not only its action name. */
324
+ export declare function approvalRequestId(input: MandatedActionInput, signed: SignedMandate, thresholdId: string): string;
325
+ /** Stable action identifiers emitted by the app's agent-permission editor. */
326
+ export declare function defaultWorkroomAction(kind: WorkroomContentPayload["kind"]): string;
327
+ /** Returns why autonomous execution must stop, independent of encrypted mandate terms. */
328
+ export declare function workroomStopReason(workroom: Pick<Workroom, "status" | "deadline">, now?: number): "completed" | "cancelled" | "expired" | "deadline" | undefined;
329
+ export {};