replicas-engine 0.1.166 → 0.1.167

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.
Files changed (2) hide show
  1. package/dist/src/index.js +51 -1
  2. package/package.json +1 -1
package/dist/src/index.js CHANGED
@@ -20,6 +20,7 @@ import { readFileSync } from "fs";
20
20
 
21
21
  // ../shared/src/event.ts
22
22
  var CODEX_QUOTA_STATUS_EVENT_TYPE = "codex-quota-status";
23
+ var CONTEXT_USAGE_EVENT_TYPE = "context-usage";
23
24
 
24
25
  // ../shared/src/pricing.ts
25
26
  var PLANS = {
@@ -285,7 +286,7 @@ function parseReplicasConfigString(content, filename) {
285
286
  }
286
287
 
287
288
  // ../shared/src/engine/environment.ts
288
- var DAYTONA_SNAPSHOT_ID = "13-05-2026-royal-york-v12";
289
+ var DAYTONA_SNAPSHOT_ID = "13-05-2026-royal-york-v13";
289
290
 
290
291
  // ../shared/src/engine/types.ts
291
292
  var DEFAULT_CHAT_TITLES = {
@@ -2616,6 +2617,15 @@ var CodingAgentManager = class {
2616
2617
  await this.initialized;
2617
2618
  return this.messageQueue.enqueue(request);
2618
2619
  }
2620
+ emitContextUsage(payload) {
2621
+ const event = {
2622
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
2623
+ type: CONTEXT_USAGE_EVENT_TYPE,
2624
+ payload: { ...payload }
2625
+ };
2626
+ this.onEvent(event);
2627
+ return event;
2628
+ }
2619
2629
  buildCombinedInstructions(customInstructions) {
2620
2630
  const startHooksInstruction = this.getStartHooksInstruction();
2621
2631
  const repositorySystemPromptInstruction = this.getRepositorySystemPromptInstruction();
@@ -2926,6 +2936,7 @@ var ClaudeManager = class _ClaudeManager extends CodingAgentManager {
2926
2936
  }
2927
2937
  }
2928
2938
  if (msg.type === "result") {
2939
+ await this.recordContextUsage(response);
2929
2940
  this.activePromptStream?.close();
2930
2941
  break;
2931
2942
  }
@@ -2947,6 +2958,45 @@ var ClaudeManager = class _ClaudeManager extends CodingAgentManager {
2947
2958
  }
2948
2959
  }
2949
2960
  }
2961
+ getContextUsageProvider() {
2962
+ return this.systemPromptOverride ? "relay" : "claude";
2963
+ }
2964
+ buildContextUsagePayload(usage) {
2965
+ const maxTokens = Number.isFinite(usage.maxTokens) ? usage.maxTokens : null;
2966
+ const percentage = Number.isFinite(usage.percentage) ? usage.percentage : maxTokens && maxTokens > 0 ? usage.totalTokens / maxTokens * 100 : 0;
2967
+ return {
2968
+ provider: this.getContextUsageProvider(),
2969
+ source: "claude_context",
2970
+ model: usage.model,
2971
+ totalTokens: usage.totalTokens,
2972
+ maxTokens,
2973
+ rawMaxTokens: Number.isFinite(usage.rawMaxTokens) ? usage.rawMaxTokens : maxTokens,
2974
+ percentage,
2975
+ categories: usage.categories.map((category) => ({
2976
+ name: category.name,
2977
+ tokens: category.tokens,
2978
+ percentage: maxTokens && maxTokens > 0 ? category.tokens / maxTokens * 100 : 0,
2979
+ color: category.color,
2980
+ ...category.isDeferred !== void 0 ? { isDeferred: category.isDeferred } : {}
2981
+ })),
2982
+ apiUsage: usage.apiUsage ? {
2983
+ inputTokens: usage.apiUsage.input_tokens,
2984
+ outputTokens: usage.apiUsage.output_tokens,
2985
+ cacheCreationInputTokens: usage.apiUsage.cache_creation_input_tokens,
2986
+ cacheReadInputTokens: usage.apiUsage.cache_read_input_tokens
2987
+ } : null,
2988
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
2989
+ };
2990
+ }
2991
+ async recordContextUsage(response) {
2992
+ try {
2993
+ const usage = await response.getContextUsage();
2994
+ const event = this.emitContextUsage(this.buildContextUsagePayload(usage));
2995
+ await appendFile4(this.historyFile, JSON.stringify(event) + "\n", "utf-8");
2996
+ } catch (error) {
2997
+ console.warn("[ClaudeManager] Failed to record context usage:", error instanceof Error ? error.message : error);
2998
+ }
2999
+ }
2950
3000
  /**
2951
3001
  * Determines if an error is an OAuth authentication failure from the Claude SDK.
2952
3002
  * Known patterns:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "replicas-engine",
3
- "version": "0.1.166",
3
+ "version": "0.1.167",
4
4
  "description": "Lightweight API server for Replicas workspaces",
5
5
  "type": "module",
6
6
  "main": "dist/src/index.js",