@aexol/spectral 0.2.19 → 0.2.21

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.
@@ -724,6 +724,7 @@ export class PiBridge {
724
724
  e.type === "tool_result");
725
725
  if (!finalContent && !hasMeaningfulEvent) {
726
726
  console.debug("[pi-bridge] skipping empty intermediate message");
727
+ this.opts.onAssistantMessageSkipped?.(messageId);
727
728
  return;
728
729
  }
729
730
  const eventsJsonl = wireEvents.map((e) => JSON.stringify(e)).join("\n");
@@ -833,8 +834,17 @@ export class PiBridge {
833
834
  // Emit token usage for this assistant message. pi provides token
834
835
  // counts via ev.message.usage; cost is computed from the model's
835
836
  // configured pricing (or null when unavailable).
837
+ //
838
+ // Skip zero-total-usage events — they happen when a turn is
839
+ // cancelled before the provider starts streaming, and emitting
840
+ // inputTokens:0 would mislead the UI into showing 0% context window
841
+ // usage after reconnect.
836
842
  const usage = ev.message.usage;
837
- if (usage) {
843
+ const totalTokens = (usage?.input ?? 0) +
844
+ (usage?.output ?? 0) +
845
+ (usage?.cacheRead ?? 0) +
846
+ (usage?.cacheWrite ?? 0);
847
+ if (usage && totalTokens > 0) {
838
848
  const usageEvent = {
839
849
  type: "token_usage",
840
850
  messageId,
@@ -843,7 +853,7 @@ export class PiBridge {
843
853
  outputTokens: usage.output ?? 0,
844
854
  cacheReadTokens: usage.cacheRead ?? 0,
845
855
  cacheWriteTokens: usage.cacheWrite ?? 0,
846
- totalTokens: usage.totalTokens ?? 0,
856
+ totalTokens: usage.totalTokens ?? totalTokens,
847
857
  cost: usage.cost?.total ?? null,
848
858
  },
849
859
  };
@@ -195,6 +195,9 @@ export class SessionStreamManager {
195
195
  console.error(`[spectral] error: failed to persist assistant message: ${err instanceof Error ? err.message : String(err)}`);
196
196
  }
197
197
  },
198
+ onAssistantMessageSkipped: (messageId) => {
199
+ this.store.deleteMessage(messageId);
200
+ },
198
201
  onError: (err) => {
199
202
  console.error(`[spectral] error: pi bridge error: ${err.message}`);
200
203
  },
@@ -499,6 +502,9 @@ export class SessionStreamManager {
499
502
  console.error(`[spectral] error: failed to persist assistant message: ${err instanceof Error ? err.message : String(err)}`);
500
503
  }
501
504
  },
505
+ onAssistantMessageSkipped: (messageId) => {
506
+ this.store.deleteMessage(messageId);
507
+ },
502
508
  onError: (err) => {
503
509
  console.error(`[spectral] error: pi bridge error: ${err.message}`);
504
510
  },
@@ -147,6 +147,7 @@ export class SessionStore {
147
147
  stmtDeleteSession;
148
148
  stmtListMessages;
149
149
  stmtAppendMessage;
150
+ stmtDeleteMessage;
150
151
  stmtTouchSession;
151
152
  stmtRenameSession;
152
153
  stmtSessionWithCount;
@@ -265,6 +266,7 @@ export class SessionStore {
265
266
  FROM messages WHERE session_id = ? ORDER BY created_at ASC, id ASC`);
266
267
  this.stmtAppendMessage = this.db.prepare(`INSERT OR REPLACE INTO messages (id, session_id, role, content, events_jsonl, images_json, created_at)
267
268
  VALUES (?, ?, ?, ?, ?, ?, ?)`);
269
+ this.stmtDeleteMessage = this.db.prepare(`DELETE FROM messages WHERE id = ?`);
268
270
  this.stmtTouchSession = this.db.prepare(`UPDATE sessions SET updated_at = ? WHERE id = ?`);
269
271
  this.stmtRenameSession = this.db.prepare(`UPDATE sessions SET title = ?, updated_at = ? WHERE id = ?`);
270
272
  this.stmtSessionWithCount = this.db.prepare(`
@@ -435,6 +437,10 @@ export class SessionStore {
435
437
  * Append a message and bump the session's updated_at in one transaction.
436
438
  * Throws if the session doesn't exist (FK violation).
437
439
  */
440
+ /** Delete a message by id. No-op if the message doesn't exist. */
441
+ deleteMessage(id) {
442
+ this.stmtDeleteMessage.run(id);
443
+ }
438
444
  appendMessage(sessionId, msg) {
439
445
  const id = msg.id ?? randomUUID();
440
446
  const createdAt = msg.createdAt ?? Date.now();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aexol/spectral",
3
- "version": "0.2.19",
3
+ "version": "0.2.21",
4
4
  "description": "Always-on coding agent for Aexol — branded pi wrapper with relay-based browser access.",
5
5
  "type": "module",
6
6
  "private": false,