@jaibber/sdk 0.4.0 → 0.4.1
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 +44 -2
- package/dist/cli.js +209 -44
- package/dist/index.cjs +195 -41
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +101 -10
- package/dist/index.d.ts +101 -10
- package/dist/index.js +195 -41
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -80,6 +80,8 @@ const agent = new JaibberAgent({
|
|
|
80
80
|
});
|
|
81
81
|
|
|
82
82
|
// Built-in LLM backends: "anthropic" | "openai" | "google" | "claude-cli"
|
|
83
|
+
// Defaults: claude-sonnet-5 / gpt-4o / gemini-pro-latest — override with `model`,
|
|
84
|
+
// e.g. { apiKey, model: "claude-opus-5" } for the most capable Claude tier.
|
|
83
85
|
agent.useProvider("anthropic", { apiKey: process.env.ANTHROPIC_API_KEY! });
|
|
84
86
|
|
|
85
87
|
// Or handle messages yourself
|
|
@@ -87,8 +89,13 @@ agent.onMessage(async (msg, ctx) => {
|
|
|
87
89
|
// ctx.conversationHistory — "User: ..." / "Assistant (Name): ..." transcript
|
|
88
90
|
await ctx.reply(`Hi ${msg.fromUsername}, you said: ${msg.text}`);
|
|
89
91
|
// or stream: await ctx.stream(() => myLLM.generate(msg.text));
|
|
92
|
+
// If the generator throws, the SDK posts an "Agent error" bubble, resolves
|
|
93
|
+
// with the partial text and sets ctx.error — it does not reject for that.
|
|
90
94
|
});
|
|
91
95
|
|
|
96
|
+
// Failures are emitted as "error" and logged by a default listener. To handle
|
|
97
|
+
// them yourself: agent.off("error", JaibberAgent.defaultErrorListener); agent.on("error", ...)
|
|
98
|
+
|
|
92
99
|
agent.onTask(async (task, ctx) => {
|
|
93
100
|
await ctx.sendMessage("Working on it...");
|
|
94
101
|
await ctx.complete("Task done!");
|
|
@@ -154,7 +161,8 @@ Run `jaibber-agent --help` for the full flag list.
|
|
|
154
161
|
- Real-time streaming via Ably WebSocket
|
|
155
162
|
- @mention routing — only responds when addressed
|
|
156
163
|
- Server-attributed identity (`agents` row, `senderAgentId` on every reply)
|
|
157
|
-
- Task auto-execution (submitted → working → completed/failed), by
|
|
164
|
+
- Task auto-execution (submitted → working → completed/failed), matched by
|
|
165
|
+
agent id (by name only for legacy tasks without one)
|
|
158
166
|
- Conversation context (last 20 messages, including the agent's own replies)
|
|
159
167
|
- Loop prevention for agent-to-agent chains (max depth 3)
|
|
160
168
|
- Heartbeat presence (`lastSeenAt`) every 2 minutes
|
|
@@ -162,7 +170,41 @@ Run `jaibber-agent --help` for the full flag list.
|
|
|
162
170
|
(`agent-control` event)
|
|
163
171
|
- 6 preset templates (`--list-templates`)
|
|
164
172
|
|
|
165
|
-
##
|
|
173
|
+
## Changelog
|
|
174
|
+
|
|
175
|
+
### 0.4.1
|
|
176
|
+
|
|
177
|
+
Bug fixes; no breaking changes.
|
|
178
|
+
|
|
179
|
+
- **Crash on provider error.** A transient provider failure (rate limit,
|
|
180
|
+
Claude CLI exit, network) took the whole headless process down: the error
|
|
181
|
+
was re-thrown into the Ably subscribe callback as an unobserved rejection,
|
|
182
|
+
and `agent.emit("error")` had no listener (a Node `EventEmitter` throws in
|
|
183
|
+
that case). `JaibberAgent` now registers a logging `"error"` listener by
|
|
184
|
+
default (`JaibberAgent.defaultErrorListener`, removable),
|
|
185
|
+
`MessageContext.stream()` resolves with the partial text instead of
|
|
186
|
+
rejecting — the failure is still stored as an `error` bubble and exposed
|
|
187
|
+
as `ctx.error` — nothing can escape an Ably subscribe handler, a dropped
|
|
188
|
+
`chunk` publish is logged instead of aborting the reply, and the CLI logs
|
|
189
|
+
`unhandledRejection` / `uncaughtException` instead of exiting.
|
|
190
|
+
- **Task pickup by id.** A task carrying `assignedAgentId` is picked up only
|
|
191
|
+
by the agent with that id; the name match applies to legacy tasks without
|
|
192
|
+
one. Previously a same-named agent could grab a task assigned to another.
|
|
193
|
+
- **Delivery-failure fallback.** When the final `POST` of a reply fails
|
|
194
|
+
(after the retry) the SDK publishes a client-side `error` event under the
|
|
195
|
+
same message id so remote streaming bubbles close instead of hanging
|
|
196
|
+
(ephemeral — not persisted). `reply()` / `stream()` still throw in that case.
|
|
197
|
+
- **Model defaults.** The Anthropic default is `claude-sonnet-5`
|
|
198
|
+
(`--anthropic-model claude-opus-5` / `model: "claude-opus-5"` for the most
|
|
199
|
+
capable tier). `gpt-4o` and `gemini-pro-latest` are unchanged.
|
|
200
|
+
- **`GET /api/me`.** In API-key mode the agent id and name are resolved from
|
|
201
|
+
`GET /api/me` before Ably connects (`JaibberClient.whoAmI()`,
|
|
202
|
+
`agent.identity`). If the key's agent is named differently from
|
|
203
|
+
`agentName`, the server's name is used (with a warning) — that is the name
|
|
204
|
+
replies are attributed to and that @mentions must match. Servers without
|
|
205
|
+
the endpoint fall back to the Ably clientId as before.
|
|
206
|
+
|
|
207
|
+
### 0.4.0 — breaking changes
|
|
166
208
|
|
|
167
209
|
The server now has a single agent model and derives message attribution from
|
|
168
210
|
the credential. This release aligns the SDK with it.
|
package/dist/cli.js
CHANGED
|
@@ -124,6 +124,22 @@ var JaibberClient = class {
|
|
|
124
124
|
}
|
|
125
125
|
return res;
|
|
126
126
|
}
|
|
127
|
+
// ── Identity ────────────────────────────────────────────────────────
|
|
128
|
+
/**
|
|
129
|
+
* GET /api/me — who the current credential is: the user (session), the
|
|
130
|
+
* agent (agent-bound key) or the org (org key). Resolves `null` when the
|
|
131
|
+
* server predates the endpoint (404).
|
|
132
|
+
*/
|
|
133
|
+
async whoAmI() {
|
|
134
|
+
const res = await fetch(`${this.serverUrl}/api/me`, { headers: this.authHeaders() });
|
|
135
|
+
if (res.status === 404) return null;
|
|
136
|
+
if (!res.ok) {
|
|
137
|
+
const text = await readError(res);
|
|
138
|
+
throw new Error(`GET /api/me failed (${res.status}): ${text}`);
|
|
139
|
+
}
|
|
140
|
+
const json = await res.json();
|
|
141
|
+
return json.data;
|
|
142
|
+
}
|
|
127
143
|
// ── Projects ────────────────────────────────────────────────────────
|
|
128
144
|
/**
|
|
129
145
|
* GET /api/projects — with a session: projects the user is a member of
|
|
@@ -404,26 +420,33 @@ var AblyManager = class {
|
|
|
404
420
|
* `agent-control` events) and enter per-project presence.
|
|
405
421
|
* Returns an unsubscribe function.
|
|
406
422
|
*/
|
|
407
|
-
async subscribeToProject(project, presenceData, onMessage, onTask, onControl) {
|
|
423
|
+
async subscribeToProject(project, presenceData, onMessage, onTask, onControl, onError = (err, event) => console.error(`[sdk] Unhandled error in ${event} handler:`, err)) {
|
|
408
424
|
if (!this.ably) throw new Error("Ably not connected");
|
|
409
425
|
const channel = this.ably.channels.get(project.ablyChannelName);
|
|
410
426
|
this.channels.set(project.ablyChannelName, channel);
|
|
411
427
|
await channel.presence.enter(presenceData);
|
|
412
|
-
const
|
|
413
|
-
|
|
428
|
+
const safely = (event, body) => {
|
|
429
|
+
try {
|
|
430
|
+
Promise.resolve(body()).catch((err) => onError(err, event));
|
|
431
|
+
} catch (err) {
|
|
432
|
+
onError(err, event);
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
const messageHandler = (msg) => safely("message", () => {
|
|
436
|
+
if (!msg.data || typeof msg.data !== "object") return;
|
|
414
437
|
const payload = msg.data;
|
|
415
438
|
payload._connectionId = msg.connectionId ?? void 0;
|
|
416
439
|
payload._clientId = msg.clientId ?? void 0;
|
|
417
|
-
onMessage(payload);
|
|
418
|
-
};
|
|
419
|
-
const taskHandler = (msg) => {
|
|
420
|
-
if (!msg.data) return;
|
|
421
|
-
onTask(msg.data);
|
|
422
|
-
};
|
|
423
|
-
const controlHandler = (msg) => {
|
|
440
|
+
return onMessage(payload);
|
|
441
|
+
});
|
|
442
|
+
const taskHandler = (msg) => safely("task", () => {
|
|
424
443
|
if (!msg.data || typeof msg.data !== "object") return;
|
|
425
|
-
|
|
426
|
-
};
|
|
444
|
+
return onTask(msg.data);
|
|
445
|
+
});
|
|
446
|
+
const controlHandler = (msg) => safely("agent-control", () => {
|
|
447
|
+
if (!msg.data || typeof msg.data !== "object") return;
|
|
448
|
+
return onControl?.(msg.data);
|
|
449
|
+
});
|
|
427
450
|
channel.subscribe("message", messageHandler);
|
|
428
451
|
channel.subscribe("task", taskHandler);
|
|
429
452
|
channel.subscribe("agent-control", controlHandler);
|
|
@@ -502,6 +525,8 @@ var MessageContext = class {
|
|
|
502
525
|
respondingChain;
|
|
503
526
|
chunkBatchMs;
|
|
504
527
|
tracker;
|
|
528
|
+
/** Set by `stream()` when its generator threw. */
|
|
529
|
+
streamError = void 0;
|
|
505
530
|
constructor(opts) {
|
|
506
531
|
this.projectId = opts.projectId;
|
|
507
532
|
this.channelName = opts.channelName;
|
|
@@ -529,10 +554,23 @@ var MessageContext = class {
|
|
|
529
554
|
get chain() {
|
|
530
555
|
return [...this.respondingChain];
|
|
531
556
|
}
|
|
532
|
-
/**
|
|
557
|
+
/**
|
|
558
|
+
* Why `stream()` failed, if it did: the error its generator threw;
|
|
559
|
+
* `undefined` otherwise. The failure has already been stored under this
|
|
560
|
+
* reply's id as an `error` bubble, and `stream()` resolved with the
|
|
561
|
+
* partial text.
|
|
562
|
+
*/
|
|
563
|
+
get error() {
|
|
564
|
+
return this.streamError;
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* One-shot reply: publishes `typing`, then POSTs the final message.
|
|
568
|
+
* Throws if the server rejects it (remote bubbles are closed first — see
|
|
569
|
+
* `deliver`).
|
|
570
|
+
*/
|
|
533
571
|
async reply(text, opts) {
|
|
534
572
|
await this.publishTyping();
|
|
535
|
-
return this.
|
|
573
|
+
return this.deliver(text, "response", this.parentFor(opts));
|
|
536
574
|
}
|
|
537
575
|
/** Replies thread under the answered message unless overridden. */
|
|
538
576
|
parentFor(opts) {
|
|
@@ -543,6 +581,12 @@ var MessageContext = class {
|
|
|
543
581
|
* every chunkBatchMs (default 200ms), then POSTs the final message.
|
|
544
582
|
* Resolves with the full response text.
|
|
545
583
|
*
|
|
584
|
+
* A provider error (the generator throwing) does not reject: the failure
|
|
585
|
+
* is stored under the same id as an `error` bubble, exposed as `error`,
|
|
586
|
+
* and the text streamed so far is returned. Only a delivery failure — the
|
|
587
|
+
* server rejecting the final POST after the retry — rejects, and remote
|
|
588
|
+
* bubbles are closed before it does.
|
|
589
|
+
*
|
|
546
590
|
* Mirrors useAbly.ts chunk batching pattern.
|
|
547
591
|
*/
|
|
548
592
|
async stream(generator, opts) {
|
|
@@ -554,16 +598,20 @@ var MessageContext = class {
|
|
|
554
598
|
if (!chunkBuffer) return;
|
|
555
599
|
const text = chunkBuffer;
|
|
556
600
|
chunkBuffer = "";
|
|
557
|
-
|
|
601
|
+
try {
|
|
602
|
+
await this.publishChunk(text);
|
|
603
|
+
} catch (e) {
|
|
604
|
+
console.warn("[sdk] chunk publish failed:", e instanceof Error ? e.message : e);
|
|
605
|
+
}
|
|
558
606
|
};
|
|
559
607
|
try {
|
|
560
608
|
for await (const chunk of generator()) {
|
|
561
609
|
fullText += chunk;
|
|
562
610
|
chunkBuffer += chunk;
|
|
563
611
|
if (!flushTimer) {
|
|
564
|
-
flushTimer = setTimeout(
|
|
612
|
+
flushTimer = setTimeout(() => {
|
|
565
613
|
flushTimer = null;
|
|
566
|
-
|
|
614
|
+
void flushChunks();
|
|
567
615
|
}, this.chunkBatchMs);
|
|
568
616
|
}
|
|
569
617
|
}
|
|
@@ -571,13 +619,16 @@ var MessageContext = class {
|
|
|
571
619
|
await flushChunks();
|
|
572
620
|
} catch (err) {
|
|
573
621
|
if (flushTimer) clearTimeout(flushTimer);
|
|
622
|
+
this.streamError = err;
|
|
574
623
|
const errText = `Agent error: ${err instanceof Error ? err.message : String(err)}`;
|
|
575
|
-
|
|
624
|
+
try {
|
|
625
|
+
await this.deliver(errText, "error", this.parentFor(opts));
|
|
626
|
+
} catch (e) {
|
|
576
627
|
console.error("[sdk] Failed to persist error message:", e);
|
|
577
|
-
}
|
|
578
|
-
|
|
628
|
+
}
|
|
629
|
+
return fullText;
|
|
579
630
|
}
|
|
580
|
-
await this.
|
|
631
|
+
await this.deliver(fullText, "response", this.parentFor(opts));
|
|
581
632
|
return fullText;
|
|
582
633
|
}
|
|
583
634
|
// ── Internal publish helpers ────────────────────────────────────────
|
|
@@ -606,6 +657,44 @@ var MessageContext = class {
|
|
|
606
657
|
agentId: this.agentId
|
|
607
658
|
});
|
|
608
659
|
}
|
|
660
|
+
/**
|
|
661
|
+
* POST this reply's final message. If the server rejects it (after
|
|
662
|
+
* `persistMessage`'s single retry), every other client is stuck on the
|
|
663
|
+
* streaming bubble our `typing`/`chunk` events opened — the server echo
|
|
664
|
+
* that would close it is never coming. Close them with a client-published
|
|
665
|
+
* `error` event under the same id (ephemeral: the server does not persist
|
|
666
|
+
* it, acceptable for a last resort), then rethrow.
|
|
667
|
+
*/
|
|
668
|
+
async deliver(text, type, parentMessageId) {
|
|
669
|
+
try {
|
|
670
|
+
return await this.persist(this.messageId, text, type, parentMessageId);
|
|
671
|
+
} catch (err) {
|
|
672
|
+
await this.publishDeliveryFailure(
|
|
673
|
+
err instanceof Error ? err.message : String(err),
|
|
674
|
+
parentMessageId
|
|
675
|
+
);
|
|
676
|
+
throw err;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
/** Same wire shape as the desktop's delivery-failure fallback (useAbly.ts deliver()). */
|
|
680
|
+
async publishDeliveryFailure(reason, parentMessageId) {
|
|
681
|
+
try {
|
|
682
|
+
await this.ablyManager.publish(this.channelName, "message", {
|
|
683
|
+
from: this.from,
|
|
684
|
+
fromUsername: this.agentName,
|
|
685
|
+
projectId: this.projectId,
|
|
686
|
+
text: `Delivery failed: ${reason}`,
|
|
687
|
+
messageId: this.messageId,
|
|
688
|
+
type: "error",
|
|
689
|
+
agentName: this.agentName,
|
|
690
|
+
agentId: this.agentId,
|
|
691
|
+
isAgentMessage: true,
|
|
692
|
+
parentMessageId
|
|
693
|
+
});
|
|
694
|
+
} catch (e) {
|
|
695
|
+
console.warn("[sdk] delivery-failure publish failed:", e instanceof Error ? e.message : e);
|
|
696
|
+
}
|
|
697
|
+
}
|
|
609
698
|
/** POST the final message. Throws if the server rejects it. */
|
|
610
699
|
async persist(id, text, type, parentMessageId) {
|
|
611
700
|
this.tracker?.begin(id, this.projectId, text, type, parentMessageId);
|
|
@@ -708,7 +797,7 @@ function escapeRegex(s) {
|
|
|
708
797
|
// src/providers/anthropic.ts
|
|
709
798
|
var ANTHROPIC_API_URL = "https://api.anthropic.com/v1/messages";
|
|
710
799
|
var ANTHROPIC_VERSION = "2024-10-22";
|
|
711
|
-
var DEFAULT_MODEL = "claude-sonnet-
|
|
800
|
+
var DEFAULT_MODEL = "claude-sonnet-5";
|
|
712
801
|
var DEFAULT_MAX_TOKENS = 8192;
|
|
713
802
|
var AnthropicProvider = class {
|
|
714
803
|
apiKey;
|
|
@@ -1102,7 +1191,7 @@ var OWN_MESSAGE_MEMORY = 1e3;
|
|
|
1102
1191
|
var MESSAGE_INDEX_SIZE = 500;
|
|
1103
1192
|
var META_TTL_MS = 5 * 60 * 1e3;
|
|
1104
1193
|
var PLAN_MODE_PREFIX = "You are in PLAN MODE. Analyze the request and provide a detailed plan of what changes you would make, including specific files and code snippets. Do NOT execute any file modifications, write any files, or run any commands that change state. You may read files for context. Format your plan clearly with numbered steps.";
|
|
1105
|
-
var JaibberAgent = class extends EventEmitter {
|
|
1194
|
+
var JaibberAgent = class _JaibberAgent extends EventEmitter {
|
|
1106
1195
|
config;
|
|
1107
1196
|
client;
|
|
1108
1197
|
ablyManager = null;
|
|
@@ -1116,6 +1205,7 @@ var JaibberAgent = class extends EventEmitter {
|
|
|
1116
1205
|
heartbeatTimer = null;
|
|
1117
1206
|
agentId = null;
|
|
1118
1207
|
agentRecord = null;
|
|
1208
|
+
keyIdentity = null;
|
|
1119
1209
|
/** In-memory conversation history per project. */
|
|
1120
1210
|
history = /* @__PURE__ */ new Map();
|
|
1121
1211
|
/** Recent messages per project (id → author/parent) for chain reconstruction. */
|
|
@@ -1139,6 +1229,23 @@ var JaibberAgent = class extends EventEmitter {
|
|
|
1139
1229
|
super();
|
|
1140
1230
|
this.config = config;
|
|
1141
1231
|
this.client = new JaibberClient(config.serverUrl);
|
|
1232
|
+
this.on("error", _JaibberAgent.defaultErrorListener);
|
|
1233
|
+
}
|
|
1234
|
+
/**
|
|
1235
|
+
* Default `"error"` listener, registered by the constructor: logs the error
|
|
1236
|
+
* and keeps the process alive. To handle errors yourself without the log,
|
|
1237
|
+
* remove it: `agent.off("error", JaibberAgent.defaultErrorListener)`.
|
|
1238
|
+
*/
|
|
1239
|
+
static defaultErrorListener = (err) => {
|
|
1240
|
+
console.error("[jaibber-agent] error:", err);
|
|
1241
|
+
};
|
|
1242
|
+
/** Emit `"error"` without ever throwing, even if every listener was removed. */
|
|
1243
|
+
emitError(err) {
|
|
1244
|
+
if (this.listenerCount("error") === 0) {
|
|
1245
|
+
_JaibberAgent.defaultErrorListener(err);
|
|
1246
|
+
return;
|
|
1247
|
+
}
|
|
1248
|
+
this.emit("error", err);
|
|
1142
1249
|
}
|
|
1143
1250
|
// ── Accessors ───────────────────────────────────────────────────────
|
|
1144
1251
|
/** The agent's server-side id (available after `connect()`). */
|
|
@@ -1149,6 +1256,10 @@ var JaibberAgent = class extends EventEmitter {
|
|
|
1149
1256
|
get record() {
|
|
1150
1257
|
return this.agentRecord;
|
|
1151
1258
|
}
|
|
1259
|
+
/** The key's agent identity from `GET /api/me` (API-key mode, on servers that have it). */
|
|
1260
|
+
get identity() {
|
|
1261
|
+
return this.keyIdentity;
|
|
1262
|
+
}
|
|
1152
1263
|
/** The underlying REST client (authenticated after `connect()`). */
|
|
1153
1264
|
get api() {
|
|
1154
1265
|
return this.client;
|
|
@@ -1174,6 +1285,7 @@ var JaibberAgent = class extends EventEmitter {
|
|
|
1174
1285
|
this.client.setApiKey(creds.apiKey);
|
|
1175
1286
|
this.agentId = this.config.agentId ?? null;
|
|
1176
1287
|
console.log(`[sdk] Authenticating with agent API key ${creds.apiKey.slice(0, 11)}\u2026`);
|
|
1288
|
+
await this.resolveKeyIdentity();
|
|
1177
1289
|
} else {
|
|
1178
1290
|
console.log(`[sdk] Logging in as "${creds.username}"...`);
|
|
1179
1291
|
await this.client.login(creds.username, creds.password);
|
|
@@ -1269,7 +1381,8 @@ var JaibberAgent = class extends EventEmitter {
|
|
|
1269
1381
|
presenceData,
|
|
1270
1382
|
(msg) => this.handleIncomingMessage(msg, project),
|
|
1271
1383
|
(data) => this.handleTaskEvent(data, project),
|
|
1272
|
-
(data) => this.handleControlEvent(data, project)
|
|
1384
|
+
(data) => this.handleControlEvent(data, project),
|
|
1385
|
+
(err) => this.emitError(err)
|
|
1273
1386
|
);
|
|
1274
1387
|
this.unsubscribers.push(unsub);
|
|
1275
1388
|
}
|
|
@@ -1288,6 +1401,43 @@ var JaibberAgent = class extends EventEmitter {
|
|
|
1288
1401
|
);
|
|
1289
1402
|
this.emit("connected", this.projects);
|
|
1290
1403
|
}
|
|
1404
|
+
/**
|
|
1405
|
+
* API-key mode: ask the server who the key is (`GET /api/me`) so the agent
|
|
1406
|
+
* id and name are known before Ably connects. Servers that predate the
|
|
1407
|
+
* endpoint answer 404; the id is then read from the Ably token's clientId
|
|
1408
|
+
* (`agent:{id}`) once connected, as in 0.4.0.
|
|
1409
|
+
*/
|
|
1410
|
+
async resolveKeyIdentity() {
|
|
1411
|
+
const me = await this.client.whoAmI();
|
|
1412
|
+
if (me === null) {
|
|
1413
|
+
console.warn(
|
|
1414
|
+
"[sdk] GET /api/me is not available on this server; the agent id will be read from the Ably token."
|
|
1415
|
+
);
|
|
1416
|
+
return;
|
|
1417
|
+
}
|
|
1418
|
+
if (me.type !== "agent") {
|
|
1419
|
+
throw new Error(
|
|
1420
|
+
`The API key is not bound to an agent (GET /api/me reports type "${me.type}"). Mint an agent key with POST /api/me/agents/:id/keys or \`jaibber-agent --create-key\`.`
|
|
1421
|
+
);
|
|
1422
|
+
}
|
|
1423
|
+
if (this.agentId && this.agentId !== me.agentId) {
|
|
1424
|
+
console.warn(
|
|
1425
|
+
`[sdk] Configured agentId ${this.agentId} does not match the key's agent ${me.agentId}; using the key's.`
|
|
1426
|
+
);
|
|
1427
|
+
}
|
|
1428
|
+
this.agentId = me.agentId;
|
|
1429
|
+
this.config.agentId = me.agentId;
|
|
1430
|
+
this.keyIdentity = me;
|
|
1431
|
+
if (me.name && me.name.toLowerCase() !== this.config.agentName.toLowerCase()) {
|
|
1432
|
+
console.warn(
|
|
1433
|
+
`[sdk] agentName "${this.config.agentName}" differs from the key's agent "${me.name}"; using "${me.name}".`
|
|
1434
|
+
);
|
|
1435
|
+
this.config.agentName = me.name;
|
|
1436
|
+
}
|
|
1437
|
+
console.log(
|
|
1438
|
+
`[sdk] Authenticated as agent "${me.name}" (id ${me.agentId}${me.org ? `, org ${me.org.slug}` : ""})`
|
|
1439
|
+
);
|
|
1440
|
+
}
|
|
1291
1441
|
/** Stop heartbeats, leave presence, disconnect Ably. */
|
|
1292
1442
|
async disconnect() {
|
|
1293
1443
|
if (!this.connected) return;
|
|
@@ -1501,8 +1651,7 @@ var JaibberAgent = class extends EventEmitter {
|
|
|
1501
1651
|
try {
|
|
1502
1652
|
await this.respondToMessage(payload, project, depth, chain, executionMode, attachments);
|
|
1503
1653
|
} catch (err) {
|
|
1504
|
-
|
|
1505
|
-
this.emit("error", err);
|
|
1654
|
+
this.emitError(err);
|
|
1506
1655
|
} finally {
|
|
1507
1656
|
this.activeResponses.delete(payload.messageId);
|
|
1508
1657
|
}
|
|
@@ -1556,6 +1705,7 @@ var JaibberAgent = class extends EventEmitter {
|
|
|
1556
1705
|
},
|
|
1557
1706
|
ctx
|
|
1558
1707
|
);
|
|
1708
|
+
if (ctx.error !== void 0) this.emitError(ctx.error);
|
|
1559
1709
|
return;
|
|
1560
1710
|
}
|
|
1561
1711
|
if (this.provider) {
|
|
@@ -1564,6 +1714,10 @@ var JaibberAgent = class extends EventEmitter {
|
|
|
1564
1714
|
await ctx.stream(
|
|
1565
1715
|
() => this.provider.generate(payload.text, systemPrompt, conversationHistory)
|
|
1566
1716
|
);
|
|
1717
|
+
if (ctx.error !== void 0) {
|
|
1718
|
+
this.emitError(ctx.error);
|
|
1719
|
+
return;
|
|
1720
|
+
}
|
|
1567
1721
|
this.captureProviderSession(project.id);
|
|
1568
1722
|
return;
|
|
1569
1723
|
}
|
|
@@ -1612,9 +1766,8 @@ ${instructions2}` : PLAN_MODE_PREFIX;
|
|
|
1612
1766
|
return;
|
|
1613
1767
|
const task = data.task;
|
|
1614
1768
|
if (task.status !== "submitted") return;
|
|
1615
|
-
const
|
|
1616
|
-
|
|
1617
|
-
if (!assignedById && !assignedByName) return;
|
|
1769
|
+
const mine = task.assignedAgentId ? !!this.agentId && task.assignedAgentId === this.agentId : !!task.assignedAgentName && task.assignedAgentName.toLowerCase() === this.config.agentName.toLowerCase();
|
|
1770
|
+
if (!mine) return;
|
|
1618
1771
|
console.log(
|
|
1619
1772
|
`[sdk] Picking up task: "${task.title}" (${task.id})`
|
|
1620
1773
|
);
|
|
@@ -1664,26 +1817,27 @@ ${task.description}` : `Task: ${task.title}`;
|
|
|
1664
1817
|
chunkBatchMs: this.config.chunkBatchMs ?? DEFAULT_CHUNK_BATCH_MS,
|
|
1665
1818
|
tracker: this.tracker
|
|
1666
1819
|
});
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
this.
|
|
1676
|
-
await this.client.updateTask(task.id, { status: "completed" });
|
|
1677
|
-
} catch {
|
|
1820
|
+
await msgCtx.stream(
|
|
1821
|
+
() => this.provider.generate(
|
|
1822
|
+
taskPrompt,
|
|
1823
|
+
systemPrompt,
|
|
1824
|
+
conversationHistory
|
|
1825
|
+
)
|
|
1826
|
+
);
|
|
1827
|
+
if (msgCtx.error !== void 0) {
|
|
1828
|
+
this.emitError(msgCtx.error);
|
|
1678
1829
|
await this.client.updateTask(task.id, { status: "failed" });
|
|
1830
|
+
return;
|
|
1679
1831
|
}
|
|
1832
|
+
this.captureProviderSession(project.id);
|
|
1833
|
+
await this.client.updateTask(task.id, { status: "completed" });
|
|
1680
1834
|
return;
|
|
1681
1835
|
}
|
|
1682
1836
|
console.warn(
|
|
1683
1837
|
"[sdk] No task handler or provider configured. Task left as working."
|
|
1684
1838
|
);
|
|
1685
1839
|
} catch (err) {
|
|
1686
|
-
|
|
1840
|
+
this.emitError(err);
|
|
1687
1841
|
try {
|
|
1688
1842
|
await this.client.updateTask(task.id, { status: "failed" });
|
|
1689
1843
|
} catch {
|
|
@@ -1855,7 +2009,7 @@ function parseArgs(argv) {
|
|
|
1855
2009
|
var args = parseArgs(process.argv.slice(2));
|
|
1856
2010
|
var DEFAULT_SERVER = "https://api.jaibber.com";
|
|
1857
2011
|
var DEFAULTS = {
|
|
1858
|
-
anthropic: "claude-sonnet-
|
|
2012
|
+
anthropic: "claude-sonnet-5",
|
|
1859
2013
|
openai: "gpt-4o",
|
|
1860
2014
|
google: "gemini-pro-latest"
|
|
1861
2015
|
};
|
|
@@ -1884,7 +2038,8 @@ PROVIDER (at least one required \u2014 first match wins):
|
|
|
1884
2038
|
--claude-cli Use local Claude CLI (no API key needed)
|
|
1885
2039
|
|
|
1886
2040
|
MODEL OVERRIDES (optional \u2014 defaults track each provider's recommended model):
|
|
1887
|
-
--anthropic-model <id> Anthropic model (default: ${DEFAULTS.anthropic}
|
|
2041
|
+
--anthropic-model <id> Anthropic model (default: ${DEFAULTS.anthropic};
|
|
2042
|
+
claude-opus-5 for the most capable tier)
|
|
1888
2043
|
--openai-model <id> OpenAI model (default: ${DEFAULTS.openai})
|
|
1889
2044
|
--google-model <id> Gemini model (default: ${DEFAULTS.google})
|
|
1890
2045
|
|
|
@@ -2180,6 +2335,12 @@ WantedBy=default.target
|
|
|
2180
2335
|
}
|
|
2181
2336
|
process.exit(0);
|
|
2182
2337
|
}
|
|
2338
|
+
process.on("unhandledRejection", (reason) => {
|
|
2339
|
+
console.error("[cli] Unhandled promise rejection (continuing):", reason);
|
|
2340
|
+
});
|
|
2341
|
+
process.on("uncaughtException", (err) => {
|
|
2342
|
+
console.error("[cli] Uncaught exception (continuing):", err);
|
|
2343
|
+
});
|
|
2183
2344
|
var credentials = apiKey ? { apiKey } : { username, password };
|
|
2184
2345
|
var agent = new JaibberAgent({
|
|
2185
2346
|
serverUrl,
|
|
@@ -2219,7 +2380,11 @@ async function shutdown() {
|
|
|
2219
2380
|
if (shuttingDown) return;
|
|
2220
2381
|
shuttingDown = true;
|
|
2221
2382
|
console.log("\n[cli] Shutting down...");
|
|
2222
|
-
|
|
2383
|
+
try {
|
|
2384
|
+
await agent.disconnect();
|
|
2385
|
+
} catch (err) {
|
|
2386
|
+
console.error(`[cli] Error during shutdown: ${err instanceof Error ? err.message : err}`);
|
|
2387
|
+
}
|
|
2223
2388
|
process.exit(0);
|
|
2224
2389
|
}
|
|
2225
2390
|
process.on("SIGINT", shutdown);
|