@sw4rm/js-sdk 0.4.0 → 0.6.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/README.md +178 -1
- package/dist/cjs/index.cjs +2819 -292
- package/dist/esm/index.js +2736 -284
- package/dist/types/agentConfig.d.ts +245 -0
- package/dist/types/audit.d.ts +214 -0
- package/dist/types/clients/handoff.d.ts +44 -1
- package/dist/types/clients/negotiationRoom.d.ts +81 -5
- package/dist/types/clients/negotiationRoomStore.d.ts +155 -0
- package/dist/types/clients/workflow.d.ts +15 -0
- package/dist/types/constants/index.d.ts +100 -0
- package/dist/types/index.d.ts +14 -5
- package/dist/types/internal/baseClient.d.ts +6 -0
- package/dist/types/internal/envelope.d.ts +16 -0
- package/dist/types/internal/errorMapping.d.ts +116 -0
- package/dist/types/internal/worktreeState.d.ts +60 -0
- package/dist/types/llm/anthropic.d.ts +83 -0
- package/dist/types/llm/client.d.ts +107 -0
- package/dist/types/llm/factory.d.ts +69 -0
- package/dist/types/llm/groq.d.ts +79 -0
- package/dist/types/llm/index.d.ts +45 -0
- package/dist/types/llm/mock.d.ts +89 -0
- package/dist/types/llm/rateLimiter.d.ts +101 -0
- package/dist/types/persistentActivityBuffer.d.ts +94 -0
- package/dist/types/runtime/cancellation.d.ts +41 -0
- package/dist/types/runtime/delegation.d.ts +20 -0
- package/dist/types/runtime/gateway.d.ts +80 -0
- package/package.json +4 -2
- package/protos/activity.proto +24 -0
- package/protos/common.proto +141 -0
- package/protos/connector.proto +29 -0
- package/protos/handoff.proto +105 -0
- package/protos/hitl.proto +23 -0
- package/protos/logging.proto +20 -0
- package/protos/negotiation.proto +57 -0
- package/protos/negotiation_room.proto +220 -0
- package/protos/policy.proto +55 -0
- package/protos/reasoning.proto +41 -0
- package/protos/registry.proto +47 -0
- package/protos/router.proto +16 -0
- package/protos/scheduler.proto +52 -0
- package/protos/scheduler_policy.proto +36 -0
- package/protos/tool.proto +47 -0
- package/protos/workflow.proto +116 -0
- package/protos/worktree.proto +33 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Negotiation Room storage backend abstraction.
|
|
3
|
+
*
|
|
4
|
+
* This module provides a pluggable storage layer for NegotiationRoomClient,
|
|
5
|
+
* enabling shared state across multiple client instances.
|
|
6
|
+
*
|
|
7
|
+
* The key insight is that NegotiationRoomClient previously used instance-local
|
|
8
|
+
* storage (private Maps), which broke multi-instance deployments. By extracting
|
|
9
|
+
* storage into a separate abstraction that can be shared, we enable:
|
|
10
|
+
*
|
|
11
|
+
* 1. Multiple client instances sharing the same storage
|
|
12
|
+
* 2. Producer/Critic/Coordinator agents in different processes sharing state
|
|
13
|
+
* 3. Future gRPC integration where the server becomes the source of truth
|
|
14
|
+
*
|
|
15
|
+
* For persistent storage backends (JSON file, Redis, PostgreSQL), see the
|
|
16
|
+
* colony/stores module which provides optional persistence implementations.
|
|
17
|
+
*
|
|
18
|
+
* Based on the PolicyStore pattern established in policyStore.ts.
|
|
19
|
+
*/
|
|
20
|
+
import { NegotiationProposal, NegotiationVote, NegotiationDecision } from './negotiationRoom.js';
|
|
21
|
+
/**
|
|
22
|
+
* Error thrown when a negotiation room store operation fails.
|
|
23
|
+
*/
|
|
24
|
+
export declare class NegotiationRoomStoreError extends Error {
|
|
25
|
+
constructor(message: string);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Interface defining the contract for negotiation room storage backends.
|
|
29
|
+
*
|
|
30
|
+
* All storage implementations must support these operations for proposals,
|
|
31
|
+
* votes, and decisions. This follows the same pattern as PolicyStore,
|
|
32
|
+
* enabling pluggable backends ranging from in-memory to file-based to gRPC-based.
|
|
33
|
+
*/
|
|
34
|
+
export interface NegotiationRoomStore {
|
|
35
|
+
/**
|
|
36
|
+
* Check if a proposal exists for the given artifact_id.
|
|
37
|
+
*
|
|
38
|
+
* @param artifactId - Unique identifier for the artifact
|
|
39
|
+
* @returns True if a proposal exists, false otherwise
|
|
40
|
+
*/
|
|
41
|
+
hasProposal(artifactId: string): Promise<boolean>;
|
|
42
|
+
/**
|
|
43
|
+
* Retrieve a proposal by artifact_id.
|
|
44
|
+
*
|
|
45
|
+
* @param artifactId - Unique identifier for the artifact
|
|
46
|
+
* @returns The proposal if found, null otherwise
|
|
47
|
+
*/
|
|
48
|
+
getProposal(artifactId: string): Promise<NegotiationProposal | null>;
|
|
49
|
+
/**
|
|
50
|
+
* Save a proposal to storage.
|
|
51
|
+
*
|
|
52
|
+
* @param proposal - The proposal to save
|
|
53
|
+
* @throws NegotiationRoomStoreError if a proposal with the same artifact_id already exists
|
|
54
|
+
*/
|
|
55
|
+
saveProposal(proposal: NegotiationProposal): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* List all proposals, optionally filtered by negotiation room.
|
|
58
|
+
*
|
|
59
|
+
* @param negotiationRoomId - If provided, only return proposals for this room
|
|
60
|
+
* @returns List of proposals matching the filter criteria
|
|
61
|
+
*/
|
|
62
|
+
listProposals(negotiationRoomId?: string): Promise<NegotiationProposal[]>;
|
|
63
|
+
/**
|
|
64
|
+
* Retrieve all votes for a specific artifact.
|
|
65
|
+
*
|
|
66
|
+
* @param artifactId - The identifier of the artifact
|
|
67
|
+
* @returns List of all votes for the artifact (empty array if no votes)
|
|
68
|
+
*/
|
|
69
|
+
getVotes(artifactId: string): Promise<NegotiationVote[]>;
|
|
70
|
+
/**
|
|
71
|
+
* Add a vote for an artifact.
|
|
72
|
+
*
|
|
73
|
+
* @param vote - The vote to add
|
|
74
|
+
* @throws NegotiationRoomStoreError if the critic has already voted for this artifact
|
|
75
|
+
*/
|
|
76
|
+
addVote(vote: NegotiationVote): Promise<void>;
|
|
77
|
+
/**
|
|
78
|
+
* Retrieve the decision for an artifact.
|
|
79
|
+
*
|
|
80
|
+
* @param artifactId - The identifier of the artifact
|
|
81
|
+
* @returns The decision if available, null otherwise
|
|
82
|
+
*/
|
|
83
|
+
getDecision(artifactId: string): Promise<NegotiationDecision | null>;
|
|
84
|
+
/**
|
|
85
|
+
* Save a decision for an artifact.
|
|
86
|
+
*
|
|
87
|
+
* @param decision - The decision to save
|
|
88
|
+
* @throws NegotiationRoomStoreError if a decision already exists for this artifact
|
|
89
|
+
*/
|
|
90
|
+
saveDecision(decision: NegotiationDecision): Promise<void>;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* In-memory implementation of NegotiationRoomStore.
|
|
94
|
+
*
|
|
95
|
+
* Stores proposals, votes, and decisions in memory. Data is lost when the
|
|
96
|
+
* process terminates. Suitable for testing and single-process deployments.
|
|
97
|
+
*
|
|
98
|
+
* Thread-safety: JavaScript is single-threaded, but async operations are
|
|
99
|
+
* properly sequenced.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* // Create a single shared store
|
|
104
|
+
* const sharedStore = new InMemoryNegotiationRoomStore();
|
|
105
|
+
*
|
|
106
|
+
* // Pass to multiple clients
|
|
107
|
+
* const producerClient = new NegotiationRoomClient({ store: sharedStore });
|
|
108
|
+
* const criticClient = new NegotiationRoomClient({ store: sharedStore });
|
|
109
|
+
* const coordinatorClient = new NegotiationRoomClient({ store: sharedStore });
|
|
110
|
+
*
|
|
111
|
+
* // Now all clients share the same state
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
export declare class InMemoryNegotiationRoomStore implements NegotiationRoomStore {
|
|
115
|
+
private proposals;
|
|
116
|
+
private votes;
|
|
117
|
+
private decisions;
|
|
118
|
+
hasProposal(artifactId: string): Promise<boolean>;
|
|
119
|
+
getProposal(artifactId: string): Promise<NegotiationProposal | null>;
|
|
120
|
+
saveProposal(proposal: NegotiationProposal): Promise<void>;
|
|
121
|
+
listProposals(negotiationRoomId?: string): Promise<NegotiationProposal[]>;
|
|
122
|
+
getVotes(artifactId: string): Promise<NegotiationVote[]>;
|
|
123
|
+
addVote(vote: NegotiationVote): Promise<void>;
|
|
124
|
+
getDecision(artifactId: string): Promise<NegotiationDecision | null>;
|
|
125
|
+
saveDecision(decision: NegotiationDecision): Promise<void>;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Get the default shared in-memory store.
|
|
129
|
+
*
|
|
130
|
+
* This provides a convenient way to share state across multiple
|
|
131
|
+
* NegotiationRoomClient instances within the same process without
|
|
132
|
+
* explicitly passing a store instance.
|
|
133
|
+
*
|
|
134
|
+
* @returns The default shared InMemoryNegotiationRoomStore instance
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* // All clients automatically share the same store
|
|
139
|
+
* const client1 = new NegotiationRoomClient(); // Uses default store
|
|
140
|
+
* const client2 = new NegotiationRoomClient(); // Uses same default store
|
|
141
|
+
*
|
|
142
|
+
* // Equivalent to:
|
|
143
|
+
* const store = getDefaultStore();
|
|
144
|
+
* const client1 = new NegotiationRoomClient({ store });
|
|
145
|
+
* const client2 = new NegotiationRoomClient({ store });
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
export declare function getDefaultStore(): InMemoryNegotiationRoomStore;
|
|
149
|
+
/**
|
|
150
|
+
* Reset the default store (primarily for testing).
|
|
151
|
+
*
|
|
152
|
+
* This clears the default store singleton, causing a new store
|
|
153
|
+
* to be created on the next call to getDefaultStore().
|
|
154
|
+
*/
|
|
155
|
+
export declare function resetDefaultStore(): void;
|
|
@@ -290,6 +290,21 @@ export declare class WorkflowClient {
|
|
|
290
290
|
* Check if a workflow instance is complete and update its status.
|
|
291
291
|
*/
|
|
292
292
|
private checkWorkflowCompletion;
|
|
293
|
+
/**
|
|
294
|
+
* Resume a workflow by marking a node as completed and advancing dependents.
|
|
295
|
+
*
|
|
296
|
+
* Implements the ResumeWorkflow RPC (spec §17.7 MUST). Marks the specified
|
|
297
|
+
* node as COMPLETED, updates workflow data if provided, then transitions
|
|
298
|
+
* dependent nodes from PENDING to READY when all their dependencies are met.
|
|
299
|
+
*
|
|
300
|
+
* @param instanceId - The ID of the workflow instance
|
|
301
|
+
* @param nodeId - The ID of the node to mark as completed
|
|
302
|
+
* @param workflowData - Optional updated workflow data (JSON string)
|
|
303
|
+
* @param metadata - Optional metadata for the resume operation
|
|
304
|
+
* @returns The updated workflow instance
|
|
305
|
+
* @throws WorkflowValidationError if the instance or node does not exist
|
|
306
|
+
*/
|
|
307
|
+
resumeWorkflow(instanceId: string, nodeId: string, workflowData?: string, metadata?: Record<string, string>): Promise<WorkflowInstance>;
|
|
293
308
|
/**
|
|
294
309
|
* Update the shared workflow data.
|
|
295
310
|
*
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
export { ErrorCode } from '../internal/errorMapping.js';
|
|
2
|
+
export { MessageType } from '../internal/envelope.js';
|
|
3
|
+
/**
|
|
4
|
+
* CommunicationClass constants (spec section 7.3).
|
|
5
|
+
*/
|
|
6
|
+
export declare enum CommunicationClass {
|
|
7
|
+
COMM_CLASS_UNSPECIFIED = 0,
|
|
8
|
+
PRIVILEGED = 1,
|
|
9
|
+
STANDARD = 2,
|
|
10
|
+
BULK = 3
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* DebateIntensity constants (spec section 17).
|
|
14
|
+
*/
|
|
15
|
+
export declare enum DebateIntensity {
|
|
16
|
+
DEBATE_INTENSITY_UNSPECIFIED = 0,
|
|
17
|
+
LOWEST = 1,
|
|
18
|
+
LOW = 2,
|
|
19
|
+
MEDIUM = 3,
|
|
20
|
+
HIGH = 4,
|
|
21
|
+
HIGHEST = 5
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* HitlReasonType constants (spec section 15).
|
|
25
|
+
*/
|
|
26
|
+
export declare enum HitlReasonType {
|
|
27
|
+
HITL_REASON_UNSPECIFIED = 0,
|
|
28
|
+
CONFLICT = 1,
|
|
29
|
+
SECURITY_APPROVAL = 2,
|
|
30
|
+
TASK_ESCALATION = 3,
|
|
31
|
+
MANUAL_OVERRIDE = 4,
|
|
32
|
+
WORKTREE_OVERRIDE = 5,
|
|
33
|
+
DEBATE_DEADLOCK = 6,
|
|
34
|
+
TOOL_PRIVILEGE_ESCALATION = 7,
|
|
35
|
+
CONNECTOR_APPROVAL = 8
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* AgentState constants (spec section 8).
|
|
39
|
+
* Re-exported from agentState.ts for convenience.
|
|
40
|
+
*/
|
|
41
|
+
export { AgentState } from '../runtime/agentState.js';
|
|
42
|
+
/**
|
|
43
|
+
* AckStage constants.
|
|
44
|
+
*/
|
|
45
|
+
export declare enum AckStage {
|
|
46
|
+
ACK_STAGE_UNSPECIFIED = 0,
|
|
47
|
+
RECEIVED = 1,
|
|
48
|
+
READ = 2,
|
|
49
|
+
FULFILLED = 3,
|
|
50
|
+
REJECTED = 4,
|
|
51
|
+
FAILED = 5,
|
|
52
|
+
TIMED_OUT = 6
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* EnvelopeState lifecycle constants.
|
|
56
|
+
*
|
|
57
|
+
* Tracks the lifecycle of an envelope through the system:
|
|
58
|
+
* SENT -> RECEIVED -> READ -> FULFILLED/REJECTED/FAILED/TIMED_OUT
|
|
59
|
+
*/
|
|
60
|
+
export declare enum EnvelopeState {
|
|
61
|
+
ENVELOPE_STATE_UNSPECIFIED = 0,
|
|
62
|
+
SENT = 1,
|
|
63
|
+
RECEIVED = 2,
|
|
64
|
+
READ = 3,
|
|
65
|
+
FULFILLED = 4,
|
|
66
|
+
REJECTED = 5,
|
|
67
|
+
FAILED = 6,
|
|
68
|
+
TIMED_OUT = 7
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* WorktreeState constants (spec section 16).
|
|
72
|
+
*/
|
|
73
|
+
export declare enum WorktreeStateEnum {
|
|
74
|
+
WORKTREE_STATE_UNSPECIFIED = 0,
|
|
75
|
+
UNBOUND = 1,
|
|
76
|
+
BOUND_HOME = 2,
|
|
77
|
+
SWITCH_PENDING = 3,
|
|
78
|
+
BOUND_NON_HOME = 4,
|
|
79
|
+
BIND_FAILED = 5
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Default timeout for acknowledgement in milliseconds.
|
|
83
|
+
*/
|
|
84
|
+
export declare const DEFAULT_ACK_TIMEOUT_MS = 10000;
|
|
85
|
+
/**
|
|
86
|
+
* Deduplication window in seconds.
|
|
87
|
+
*/
|
|
88
|
+
export declare const DEDUP_WINDOW_S = 3600;
|
|
89
|
+
/**
|
|
90
|
+
* Check if an envelope state is terminal (no further transitions expected).
|
|
91
|
+
*
|
|
92
|
+
* Terminal states: FULFILLED, REJECTED, FAILED, TIMED_OUT
|
|
93
|
+
*/
|
|
94
|
+
export declare function isTerminalEnvelopeState(state: EnvelopeState): boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Update the state of an envelope object.
|
|
97
|
+
*/
|
|
98
|
+
export declare function updateEnvelopeState<T extends {
|
|
99
|
+
state?: number;
|
|
100
|
+
}>(envelope: T, newState: EnvelopeState): T;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare const version = "0.
|
|
1
|
+
export declare const version = "0.6.0";
|
|
2
2
|
export * from './clients/router.js';
|
|
3
3
|
export * from './clients/scheduler.js';
|
|
4
4
|
export * from './clients/schedulerPolicy.js';
|
|
@@ -12,32 +12,40 @@ export * from './clients/reasoning.js';
|
|
|
12
12
|
export * from './clients/connector.js';
|
|
13
13
|
export * from './clients/registry.js';
|
|
14
14
|
export * from './clients/negotiationRoom.js';
|
|
15
|
+
export * from './clients/negotiationRoomStore.js';
|
|
15
16
|
export * from './clients/handoff.js';
|
|
16
17
|
export * from './clients/workflow.js';
|
|
17
18
|
export { buildEnvelope, type EnvelopeBuilt, type EnvelopeInput, nowTimestamp, type Timestamp, } from './internal/envelope.js';
|
|
18
19
|
export * from './internal/errorMapping.js';
|
|
19
|
-
export
|
|
20
|
+
export { ClientOptions, BaseClient, } from './internal/baseClient.js';
|
|
20
21
|
export * from './internal/time.js';
|
|
21
22
|
export * from './internal/interceptors.js';
|
|
22
23
|
export * from './internal/worktreeState.js';
|
|
23
24
|
export * from './internal/worktreePolicy.js';
|
|
24
25
|
export * from './internal/runtime/activityBuffer.js';
|
|
25
|
-
export
|
|
26
|
-
export
|
|
26
|
+
export { AckState, ACKLifecycleManager, } from './internal/runtime/ackLifecycle.js';
|
|
27
|
+
export { EnvelopeLike, MessageProcessor, } from './internal/runtime/messageProcessor.js';
|
|
27
28
|
export * from './runtime/ackHelpers.js';
|
|
28
29
|
export * from './runtime/activitySync.js';
|
|
29
30
|
export * from './runtime/streams.js';
|
|
30
31
|
export * from './runtime/negotiationEvents.js';
|
|
32
|
+
export * from './runtime/delegation.js';
|
|
33
|
+
export * from './runtime/cancellation.js';
|
|
34
|
+
export * from './runtime/gateway.js';
|
|
31
35
|
export * from './persistence/persistence.js';
|
|
32
36
|
export * from './runtime/voting.js';
|
|
33
37
|
export * from './runtime/policyStore.js';
|
|
34
|
-
export
|
|
38
|
+
export { AgentState, isValidTransition, getValidTransitions, type AgentLifecycleHooks, AgentStateMachine, } from './runtime/agentState.js';
|
|
35
39
|
export * from './internal/ids.js';
|
|
36
40
|
export * from './internal/idempotency.js';
|
|
37
41
|
export * from './runtime/persistenceAdapter.js';
|
|
38
42
|
export * from './internal/errorMapper.js';
|
|
39
43
|
export * from './internal/ack.js';
|
|
40
44
|
export * from './internal/control.js';
|
|
45
|
+
export { CommunicationClass, DebateIntensity, HitlReasonType, AckStage, EnvelopeState, WorktreeStateEnum, DEFAULT_ACK_TIMEOUT_MS, DEDUP_WINDOW_S, isTerminalEnvelopeState, updateEnvelopeState, MessageType, } from './constants/index.js';
|
|
46
|
+
export * from './audit.js';
|
|
47
|
+
export * from './agentConfig.js';
|
|
48
|
+
export { type EnvelopeRecord, PersistentActivityBuffer, } from './persistentActivityBuffer.js';
|
|
41
49
|
export * from './secrets/types.js';
|
|
42
50
|
export * from './secrets/errors.js';
|
|
43
51
|
export * from './secrets/backend.js';
|
|
@@ -45,3 +53,4 @@ export * from './secrets/resolver.js';
|
|
|
45
53
|
export * from './secrets/backends/file.js';
|
|
46
54
|
export * from './secrets/backends/keyring.js';
|
|
47
55
|
export * from './secrets/factory.js';
|
|
56
|
+
export * from './llm/index.js';
|
|
@@ -25,6 +25,12 @@ export declare class BaseClient {
|
|
|
25
25
|
protected readonly interceptors: InterceptorChain;
|
|
26
26
|
protected readonly errorMapper: ErrorCodeMapper;
|
|
27
27
|
constructor(opts: ClientOptions);
|
|
28
|
+
/**
|
|
29
|
+
* Resolves the protos directory, checking multiple locations:
|
|
30
|
+
* 1. npm package: ./protos relative to package root
|
|
31
|
+
* 2. Monorepo dev: ../../../../protos relative to this file
|
|
32
|
+
*/
|
|
33
|
+
private resolveProtosDir;
|
|
28
34
|
protected channelCredentials(): grpc.ChannelCredentials;
|
|
29
35
|
protected metadata(base?: grpc.Metadata): grpc.Metadata;
|
|
30
36
|
protected deadlineFromNow(): Date;
|
|
@@ -30,6 +30,10 @@ export interface EnvelopeInput {
|
|
|
30
30
|
hlc_timestamp?: string;
|
|
31
31
|
ttl_ms?: number;
|
|
32
32
|
timestamp?: Date;
|
|
33
|
+
state?: number;
|
|
34
|
+
effective_policy_id?: string;
|
|
35
|
+
audit_proof?: Uint8Array;
|
|
36
|
+
audit_policy_id?: string;
|
|
33
37
|
}
|
|
34
38
|
export interface EnvelopeBuilt {
|
|
35
39
|
message_id: string;
|
|
@@ -47,7 +51,19 @@ export interface EnvelopeBuilt {
|
|
|
47
51
|
ttl_ms?: number;
|
|
48
52
|
timestamp: Timestamp;
|
|
49
53
|
payload?: Uint8Array;
|
|
54
|
+
state: number;
|
|
55
|
+
effective_policy_id: string;
|
|
56
|
+
audit_proof: Uint8Array;
|
|
57
|
+
audit_policy_id: string;
|
|
50
58
|
}
|
|
51
59
|
export declare function nowTimestamp(date?: Date): Timestamp;
|
|
60
|
+
/**
|
|
61
|
+
* Generate a stub HLC timestamp in the canonical format `HLC:<wall>:<logical>:<node>`.
|
|
62
|
+
*
|
|
63
|
+
* Per spec section 4, the HLC format combines wall-clock time (Unix microseconds),
|
|
64
|
+
* a logical counter (always 0 in this stub), and a node identifier (hostname).
|
|
65
|
+
* Full HLC implementations should increment the logical counter on concurrent
|
|
66
|
+
* events within the same microsecond.
|
|
67
|
+
*/
|
|
52
68
|
export declare function nowHlcStub(date?: Date): string;
|
|
53
69
|
export declare function buildEnvelope(input: EnvelopeInput): EnvelopeBuilt;
|
|
@@ -13,6 +13,10 @@ export declare enum ErrorCode {
|
|
|
13
13
|
PARTIAL_DELIVERY = 11,
|
|
14
14
|
FORCED_PREEMPTION = 12,
|
|
15
15
|
TTL_EXPIRED = 13,
|
|
16
|
+
DUPLICATE_DETECTED = 14,
|
|
17
|
+
ALREADY_IN_PROGRESS = 15,
|
|
18
|
+
OVERLOADED = 16,
|
|
19
|
+
REDIRECT = 20,
|
|
16
20
|
INTERNAL_ERROR = 99
|
|
17
21
|
}
|
|
18
22
|
export declare class Sw4rmError extends Error {
|
|
@@ -24,4 +28,116 @@ export declare class Sw4rmError extends Error {
|
|
|
24
28
|
details?: string;
|
|
25
29
|
});
|
|
26
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Thrown when input validation fails.
|
|
33
|
+
* Default error code: VALIDATION_ERROR
|
|
34
|
+
*/
|
|
35
|
+
export declare class ValidationError extends Sw4rmError {
|
|
36
|
+
constructor(message: string, code?: ErrorCode, opts?: {
|
|
37
|
+
grpcStatus?: number;
|
|
38
|
+
details?: string;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Thrown when an operation times out.
|
|
43
|
+
* Default error code: ACK_TIMEOUT
|
|
44
|
+
*/
|
|
45
|
+
export declare class TimeoutError extends Sw4rmError {
|
|
46
|
+
constructor(message: string, code?: ErrorCode, opts?: {
|
|
47
|
+
grpcStatus?: number;
|
|
48
|
+
details?: string;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Thrown when an invalid state transition is attempted.
|
|
53
|
+
* Default error code: INTERNAL_ERROR
|
|
54
|
+
*/
|
|
55
|
+
export declare class StateTransitionError extends Sw4rmError {
|
|
56
|
+
constructor(message: string, code?: ErrorCode, opts?: {
|
|
57
|
+
grpcStatus?: number;
|
|
58
|
+
details?: string;
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Thrown when negotiation fails.
|
|
63
|
+
* Default error code: INTERNAL_ERROR
|
|
64
|
+
*/
|
|
65
|
+
export declare class NegotiationError extends Sw4rmError {
|
|
66
|
+
constructor(message: string, code?: ErrorCode, opts?: {
|
|
67
|
+
grpcStatus?: number;
|
|
68
|
+
details?: string;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Thrown when the activity buffer is full.
|
|
73
|
+
* Default error code: BUFFER_FULL
|
|
74
|
+
*/
|
|
75
|
+
export declare class BufferFullError extends Sw4rmError {
|
|
76
|
+
constructor(message: string, code?: ErrorCode, opts?: {
|
|
77
|
+
grpcStatus?: number;
|
|
78
|
+
details?: string;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Thrown when no route to destination exists.
|
|
83
|
+
* Default error code: NO_ROUTE
|
|
84
|
+
*/
|
|
85
|
+
export declare class RouteError extends Sw4rmError {
|
|
86
|
+
constructor(message: string, code?: ErrorCode, opts?: {
|
|
87
|
+
grpcStatus?: number;
|
|
88
|
+
details?: string;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Thrown when permission is denied.
|
|
93
|
+
* Default error code: PERMISSION_DENIED
|
|
94
|
+
*/
|
|
95
|
+
export declare class PermissionError extends Sw4rmError {
|
|
96
|
+
constructor(message: string, code?: ErrorCode, opts?: {
|
|
97
|
+
grpcStatus?: number;
|
|
98
|
+
details?: string;
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Thrown when a duplicate request is detected.
|
|
103
|
+
* Default error code: DUPLICATE_DETECTED
|
|
104
|
+
*/
|
|
105
|
+
export declare class DuplicateDetectedError extends Sw4rmError {
|
|
106
|
+
constructor(message: string, code?: ErrorCode, opts?: {
|
|
107
|
+
grpcStatus?: number;
|
|
108
|
+
details?: string;
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Thrown when an agent is forcibly preempted by the scheduler.
|
|
113
|
+
* Default error code: FORCED_PREEMPTION
|
|
114
|
+
*/
|
|
115
|
+
export declare class PreemptionError extends Sw4rmError {
|
|
116
|
+
readonly agentId: string;
|
|
117
|
+
readonly reason: string;
|
|
118
|
+
constructor(agentId: string, reason: string, code?: ErrorCode, opts?: {
|
|
119
|
+
grpcStatus?: number;
|
|
120
|
+
details?: string;
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Thrown when a worktree operation fails (bind, switch, etc.).
|
|
125
|
+
* Default error code: INTERNAL_ERROR
|
|
126
|
+
*/
|
|
127
|
+
export declare class WorktreeError extends Sw4rmError {
|
|
128
|
+
constructor(message: string, code?: ErrorCode, opts?: {
|
|
129
|
+
grpcStatus?: number;
|
|
130
|
+
details?: string;
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Thrown when an action violates a configured policy.
|
|
135
|
+
* Default error code: PERMISSION_DENIED
|
|
136
|
+
*/
|
|
137
|
+
export declare class PolicyViolationError extends Sw4rmError {
|
|
138
|
+
constructor(message: string, code?: ErrorCode, opts?: {
|
|
139
|
+
grpcStatus?: number;
|
|
140
|
+
details?: string;
|
|
141
|
+
});
|
|
142
|
+
}
|
|
27
143
|
export declare function mapGrpcStatusToErrorCode(status: number): ErrorCode;
|
|
@@ -1,14 +1,74 @@
|
|
|
1
1
|
export type WorktreeState = 'UNBOUND' | 'BOUND_HOME' | 'SWITCH_PENDING' | 'BOUND_NON_HOME' | 'BIND_FAILED';
|
|
2
|
+
/**
|
|
3
|
+
* Check if a worktree state transition is valid.
|
|
4
|
+
*/
|
|
5
|
+
export declare function isValidWorktreeTransition(from: WorktreeState, to: WorktreeState): boolean;
|
|
6
|
+
export declare class WorktreeTransitionError extends Error {
|
|
7
|
+
readonly fromState: WorktreeState;
|
|
8
|
+
readonly toState: WorktreeState;
|
|
9
|
+
constructor(from: WorktreeState, to: WorktreeState);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Persistent worktree state machine with full spec §16 support.
|
|
13
|
+
*
|
|
14
|
+
* Supports SWITCH_PENDING approval flow with TTL on BOUND_NON_HOME.
|
|
15
|
+
*/
|
|
2
16
|
export declare class PersistentWorktreeState {
|
|
3
17
|
private state;
|
|
4
18
|
private repoId?;
|
|
5
19
|
private worktreeId?;
|
|
20
|
+
private homeRepoId?;
|
|
21
|
+
private homeWorktreeId?;
|
|
22
|
+
private switchTtlMs?;
|
|
23
|
+
private switchTimer?;
|
|
24
|
+
/**
|
|
25
|
+
* Transition to a new state with validation.
|
|
26
|
+
* @throws WorktreeTransitionError if transition is invalid
|
|
27
|
+
*/
|
|
28
|
+
private transitionTo;
|
|
29
|
+
/**
|
|
30
|
+
* Bind to a home worktree (UNBOUND -> BOUND_HOME or BIND_FAILED -> BOUND_HOME).
|
|
31
|
+
*/
|
|
6
32
|
bind(repoId: string, worktreeId: string): void;
|
|
33
|
+
/**
|
|
34
|
+
* Handle bind failure (UNBOUND -> BIND_FAILED).
|
|
35
|
+
*/
|
|
36
|
+
bindFailed(): void;
|
|
37
|
+
/**
|
|
38
|
+
* Unbind from current worktree.
|
|
39
|
+
*/
|
|
7
40
|
unbind(): void;
|
|
41
|
+
/**
|
|
42
|
+
* Request switch to a non-home worktree (BOUND_HOME -> SWITCH_PENDING).
|
|
43
|
+
* Requires approval or rejection before taking effect.
|
|
44
|
+
*/
|
|
45
|
+
requestSwitch(targetRepoId: string, targetWorktreeId: string): void;
|
|
46
|
+
/**
|
|
47
|
+
* Approve a pending switch (SWITCH_PENDING -> BOUND_NON_HOME).
|
|
48
|
+
* @param ttlMs - Time-to-live in milliseconds before auto-reverting to BOUND_HOME
|
|
49
|
+
*/
|
|
50
|
+
approveSwitch(ttlMs?: number): void;
|
|
51
|
+
/**
|
|
52
|
+
* Reject a pending switch (SWITCH_PENDING -> BOUND_HOME).
|
|
53
|
+
*/
|
|
54
|
+
rejectSwitch(): void;
|
|
55
|
+
/**
|
|
56
|
+
* Revert from non-home to home worktree (BOUND_NON_HOME -> BOUND_HOME).
|
|
57
|
+
* Called on TTL expiry or explicit revoke.
|
|
58
|
+
*/
|
|
59
|
+
revertToHome(): void;
|
|
60
|
+
/**
|
|
61
|
+
* Reset from BIND_FAILED to UNBOUND for retry.
|
|
62
|
+
*/
|
|
63
|
+
resetFromFailed(): void;
|
|
64
|
+
private clearSwitchTimer;
|
|
8
65
|
setState(state: WorktreeState): void;
|
|
9
66
|
current(): {
|
|
10
67
|
state: WorktreeState;
|
|
11
68
|
repo_id: string | undefined;
|
|
12
69
|
worktree_id: string | undefined;
|
|
70
|
+
home_repo_id: string | undefined;
|
|
71
|
+
home_worktree_id: string | undefined;
|
|
72
|
+
switch_ttl_ms: number | undefined;
|
|
13
73
|
};
|
|
14
74
|
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Anthropic LLM client for SW4RM agents.
|
|
3
|
+
*
|
|
4
|
+
* Thin wrapper around the Anthropic Messages REST API using native `fetch()`.
|
|
5
|
+
* Handles system-message normalization (Anthropic uses a top-level `system`
|
|
6
|
+
* param, not an in-band system message).
|
|
7
|
+
*
|
|
8
|
+
* Credentials (in order):
|
|
9
|
+
* 1. `apiKey` constructor parameter
|
|
10
|
+
* 2. `ANTHROPIC_API_KEY` environment variable
|
|
11
|
+
* 3. `~/.anthropic` file (plain text, one line)
|
|
12
|
+
*
|
|
13
|
+
* @module llm/anthropic
|
|
14
|
+
*/
|
|
15
|
+
import type { LlmClient, LlmResponse, QueryOptions } from './client.js';
|
|
16
|
+
/** Options for constructing an AnthropicClient. */
|
|
17
|
+
export interface AnthropicClientOptions {
|
|
18
|
+
/** API key override (takes precedence over env / dotfile). */
|
|
19
|
+
apiKey?: string;
|
|
20
|
+
/** Default model to use for queries. */
|
|
21
|
+
defaultModel?: string;
|
|
22
|
+
/** Request timeout in milliseconds. */
|
|
23
|
+
timeoutMs?: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* LLM client for the Anthropic Messages API.
|
|
27
|
+
*
|
|
28
|
+
* Credentials are resolved from (in order):
|
|
29
|
+
* 1. `apiKey` constructor option
|
|
30
|
+
* 2. `ANTHROPIC_API_KEY` environment variable
|
|
31
|
+
* 3. `~/.anthropic` file (plain text, one line)
|
|
32
|
+
*
|
|
33
|
+
* Environment variables:
|
|
34
|
+
* - `ANTHROPIC_API_KEY`: API key override
|
|
35
|
+
* - `ANTHROPIC_DEFAULT_MODEL`: Default model override
|
|
36
|
+
*/
|
|
37
|
+
export declare class AnthropicClient implements LlmClient {
|
|
38
|
+
/** The resolved API key. */
|
|
39
|
+
readonly apiKey: string;
|
|
40
|
+
/** The default model used when none is specified per-call. */
|
|
41
|
+
readonly defaultModel: string;
|
|
42
|
+
private readonly timeoutMs;
|
|
43
|
+
private readonly rateLimiter;
|
|
44
|
+
constructor(opts?: AnthropicClientOptions);
|
|
45
|
+
/**
|
|
46
|
+
* Load API key from ~/.anthropic file.
|
|
47
|
+
*
|
|
48
|
+
* @returns The key string, or null if the file does not exist.
|
|
49
|
+
*/
|
|
50
|
+
static loadKeyFile(): string | null;
|
|
51
|
+
private estimateTokens;
|
|
52
|
+
/**
|
|
53
|
+
* Map an HTTP error response to the appropriate LlmError subclass.
|
|
54
|
+
*
|
|
55
|
+
* @param status - HTTP status code.
|
|
56
|
+
* @param body - Parsed response body (if available).
|
|
57
|
+
* @param message - Raw error message.
|
|
58
|
+
*/
|
|
59
|
+
private mapError;
|
|
60
|
+
/**
|
|
61
|
+
* Send a query to Anthropic and get a complete response.
|
|
62
|
+
*
|
|
63
|
+
* System prompts are sent via the top-level `system` parameter rather
|
|
64
|
+
* than as a system message in the messages array, per the Anthropic API
|
|
65
|
+
* specification.
|
|
66
|
+
*
|
|
67
|
+
* @param prompt - The user prompt/query.
|
|
68
|
+
* @param opts - Optional query configuration.
|
|
69
|
+
* @returns LlmResponse with generated content and metadata.
|
|
70
|
+
*/
|
|
71
|
+
query(prompt: string, opts?: QueryOptions): Promise<LlmResponse>;
|
|
72
|
+
/**
|
|
73
|
+
* Stream a query response chunk by chunk.
|
|
74
|
+
*
|
|
75
|
+
* Uses Server-Sent Events (SSE) streaming from the Anthropic API.
|
|
76
|
+
* System prompts are sent via the top-level `system` parameter.
|
|
77
|
+
*
|
|
78
|
+
* @param prompt - The user prompt/query.
|
|
79
|
+
* @param opts - Optional query configuration.
|
|
80
|
+
* @yields Text chunks as they arrive from the API.
|
|
81
|
+
*/
|
|
82
|
+
streamQuery(prompt: string, opts?: QueryOptions): AsyncGenerator<string>;
|
|
83
|
+
}
|