@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
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# SW4RM JavaScript SDK
|
|
2
2
|
|
|
3
|
-
Reference JavaScript SDK for the SW4RM Agentic Protocol. This is one of
|
|
3
|
+
Reference JavaScript SDK for the SW4RM Agentic Protocol. This is one of five SDKs in this repository (Python, Rust, JavaScript, Elixir, Common Lisp). 🚧 Under development: initial implementation includes a basic RegistryClient and core utilities.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -66,6 +66,7 @@ await client.deregisterAgent('my-agent', 'Done');
|
|
|
66
66
|
|
|
67
67
|
- ✅ Base gRPC client infrastructure
|
|
68
68
|
- ✅ RegistryClient (agent registration, heartbeat, deregistration)
|
|
69
|
+
- ✅ LLM clients (Groq, Anthropic, Mock) with adaptive rate limiting
|
|
69
70
|
- ✅ TypeScript type definitions
|
|
70
71
|
- ✅ Unit tests
|
|
71
72
|
- ⏳ Additional service clients (planned)
|
|
@@ -145,15 +146,191 @@ await persist.load();
|
|
|
145
146
|
persist.startAutosave();
|
|
146
147
|
```
|
|
147
148
|
|
|
149
|
+
## LLM Client
|
|
150
|
+
|
|
151
|
+
The SDK includes a provider-agnostic LLM client abstraction. Agents can query
|
|
152
|
+
language models through a unified interface without coupling to a specific vendor.
|
|
153
|
+
|
|
154
|
+
### Import
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
import {
|
|
158
|
+
createLlmClient,
|
|
159
|
+
GroqClient,
|
|
160
|
+
AnthropicClient,
|
|
161
|
+
MockLlmClient,
|
|
162
|
+
} from '@sw4rm/js-sdk';
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Factory
|
|
166
|
+
|
|
167
|
+
The `createLlmClient` factory selects a provider based on explicit options or
|
|
168
|
+
environment variables. When nothing is specified it defaults to the mock client,
|
|
169
|
+
so tests never hit a real API.
|
|
170
|
+
|
|
171
|
+
```ts
|
|
172
|
+
// Auto-detect from LLM_CLIENT_TYPE env var (default: "mock")
|
|
173
|
+
const client = createLlmClient();
|
|
174
|
+
|
|
175
|
+
// Explicit provider
|
|
176
|
+
const groq = createLlmClient({ clientType: 'groq' });
|
|
177
|
+
const claude = createLlmClient({
|
|
178
|
+
clientType: 'anthropic',
|
|
179
|
+
model: 'claude-sonnet-4-20250514',
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// Override API key and timeout
|
|
183
|
+
const custom = createLlmClient({
|
|
184
|
+
clientType: 'groq',
|
|
185
|
+
apiKey: 'gsk_...',
|
|
186
|
+
timeoutMs: 60_000,
|
|
187
|
+
});
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### Credential resolution
|
|
191
|
+
|
|
192
|
+
Each provider resolves its API key in this order:
|
|
193
|
+
|
|
194
|
+
1. `apiKey` constructor / factory parameter
|
|
195
|
+
2. Environment variable (`GROQ_API_KEY` or `ANTHROPIC_API_KEY`)
|
|
196
|
+
3. Dotfile in the home directory (`~/.groq` or `~/.anthropic`, plain text, one line)
|
|
197
|
+
|
|
198
|
+
If none of these are set the constructor throws `LlmAuthenticationError`.
|
|
199
|
+
|
|
200
|
+
### Basic query
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
import { createLlmClient } from '@sw4rm/js-sdk';
|
|
204
|
+
|
|
205
|
+
const client = createLlmClient({ clientType: 'groq' });
|
|
206
|
+
|
|
207
|
+
const response = await client.query(
|
|
208
|
+
'Analyze this task and suggest next steps.',
|
|
209
|
+
{
|
|
210
|
+
systemPrompt: 'You are a helpful task-analysis agent.',
|
|
211
|
+
maxTokens: 2048,
|
|
212
|
+
temperature: 0.7,
|
|
213
|
+
},
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
console.log(response.content); // generated text
|
|
217
|
+
console.log(response.model); // e.g. "llama-3.3-70b-versatile"
|
|
218
|
+
console.log(response.usage); // { input_tokens, output_tokens }
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### Streaming
|
|
222
|
+
|
|
223
|
+
`streamQuery` returns an `AsyncGenerator<string>` that yields text chunks as
|
|
224
|
+
they arrive over SSE.
|
|
225
|
+
|
|
226
|
+
```ts
|
|
227
|
+
const client = createLlmClient({ clientType: 'anthropic' });
|
|
228
|
+
|
|
229
|
+
for await (const chunk of client.streamQuery('Write a status report.', {
|
|
230
|
+
systemPrompt: 'You are a concise technical writer.',
|
|
231
|
+
})) {
|
|
232
|
+
process.stdout.write(chunk);
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### Mock client for testing
|
|
237
|
+
|
|
238
|
+
`MockLlmClient` never makes network calls. It records every query so tests
|
|
239
|
+
can assert on prompts, token counts, and call order.
|
|
240
|
+
|
|
241
|
+
```ts
|
|
242
|
+
import { MockLlmClient } from '@sw4rm/js-sdk';
|
|
243
|
+
|
|
244
|
+
const mock = new MockLlmClient({
|
|
245
|
+
responses: ['First canned answer', 'Second canned answer'],
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
const r1 = await mock.query('Hello');
|
|
249
|
+
console.log(r1.content); // "First canned answer"
|
|
250
|
+
console.log(mock.callCount); // 1
|
|
251
|
+
|
|
252
|
+
// Custom generator
|
|
253
|
+
const mock2 = new MockLlmClient({
|
|
254
|
+
responseGenerator: (prompt) => `Echo: ${prompt}`,
|
|
255
|
+
});
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### Rate limiting
|
|
259
|
+
|
|
260
|
+
All LLM clients share a process-wide token-bucket rate limiter. It is enabled
|
|
261
|
+
by default and adapts automatically:
|
|
262
|
+
|
|
263
|
+
- On **HTTP 429** the budget is reduced by a configurable factor (default 0.7x).
|
|
264
|
+
- After a cooldown period and enough consecutive successes the budget recovers.
|
|
265
|
+
- Callers block in `acquire()` until tokens are available; a timeout prevents
|
|
266
|
+
indefinite waits.
|
|
267
|
+
|
|
268
|
+
No application code is needed -- rate limiting is built into every `query` and
|
|
269
|
+
`streamQuery` call.
|
|
270
|
+
|
|
271
|
+
### Error hierarchy
|
|
272
|
+
|
|
273
|
+
All LLM errors extend `LlmError`:
|
|
274
|
+
|
|
275
|
+
| Class | Trigger |
|
|
276
|
+
|---|---|
|
|
277
|
+
| `LlmAuthenticationError` | Invalid / missing API key, billing errors |
|
|
278
|
+
| `LlmRateLimitError` | HTTP 429 from the provider |
|
|
279
|
+
| `LlmTimeoutError` | Request exceeded timeout |
|
|
280
|
+
| `LlmContextLengthError` | Prompt exceeds model context window |
|
|
281
|
+
|
|
282
|
+
```ts
|
|
283
|
+
import { LlmRateLimitError } from '@sw4rm/js-sdk';
|
|
284
|
+
|
|
285
|
+
try {
|
|
286
|
+
await client.query('...');
|
|
287
|
+
} catch (err) {
|
|
288
|
+
if (err instanceof LlmRateLimitError) {
|
|
289
|
+
// The rate limiter already reduced its budget; retry after a delay
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
### Environment variables
|
|
295
|
+
|
|
296
|
+
| Variable | Default | Description |
|
|
297
|
+
|---|---|---|
|
|
298
|
+
| `LLM_CLIENT_TYPE` | `mock` | Provider for the factory: `groq`, `anthropic`, or `mock` |
|
|
299
|
+
| `LLM_DEFAULT_MODEL` | per-provider | Override the default model for any provider |
|
|
300
|
+
| `GROQ_API_KEY` | -- | Groq API key |
|
|
301
|
+
| `GROQ_DEFAULT_MODEL` | `llama-3.3-70b-versatile` | Default model for the Groq client |
|
|
302
|
+
| `ANTHROPIC_API_KEY` | -- | Anthropic API key |
|
|
303
|
+
| `ANTHROPIC_DEFAULT_MODEL` | `claude-sonnet-4-20250514` | Default model for the Anthropic client |
|
|
304
|
+
| `LLM_RATE_LIMIT_ENABLED` | `1` | Set to `0` to disable the rate limiter |
|
|
305
|
+
| `LLM_RATE_LIMIT_TOKENS_PER_MIN` | `250000` | Token budget per minute |
|
|
306
|
+
| `LLM_RATE_LIMIT_ADAPTIVE` | `1` | Enable adaptive throttling on 429 |
|
|
307
|
+
| `LLM_RATE_LIMIT_REDUCTION_FACTOR` | `0.7` | Budget multiplier after a 429 |
|
|
308
|
+
| `LLM_RATE_LIMIT_RECOVERY_FACTOR` | `1.1` | Budget multiplier during recovery |
|
|
309
|
+
| `LLM_RATE_LIMIT_COOLDOWN_SECONDS` | `30` | Seconds to wait before recovery begins |
|
|
310
|
+
| `LLM_RATE_LIMIT_RECOVERY_SUCCESS_THRESHOLD` | `20` | Consecutive successes needed for recovery |
|
|
311
|
+
|
|
148
312
|
## Spec compliance
|
|
149
313
|
|
|
150
314
|
- Envelope, ACK lifecycle, Scheduler (priority/Duration), Worktree, HITL, Negotiation, Reasoning, Connector, Logging clients implemented.
|
|
151
315
|
- Streaming resilience and interceptor hooks included by default.
|
|
152
316
|
- JSON persistence for ActivityBuffer and ACK states.
|
|
153
317
|
|
|
318
|
+
## Operational Contracts
|
|
319
|
+
|
|
320
|
+
For production deployments, see the **[Operational Contracts](../docs/OPERATIONAL_CONTRACTS.md)** documentation, which defines:
|
|
321
|
+
|
|
322
|
+
- Connection timeouts and keep-alive settings
|
|
323
|
+
- Retry policies and error handling
|
|
324
|
+
- Data consistency guarantees
|
|
325
|
+
- Idempotency contracts
|
|
326
|
+
- State persistence guarantees
|
|
327
|
+
|
|
328
|
+
These are protocol-level contracts that all SW4RM SDKs honor.
|
|
329
|
+
|
|
154
330
|
## Links
|
|
155
331
|
|
|
156
332
|
- Top-level README (overview and API): `../../README.md`
|
|
157
333
|
- Quickstart for running local services: `../../QUICKSTART.md`
|
|
334
|
+
- Operational Contracts: `../docs/OPERATIONAL_CONTRACTS.md`
|
|
158
335
|
- Python SDK: `../py_sdk/README.md`
|
|
159
336
|
- Rust SDK: `../rust_sdk/README.md`
|