@polka-codes/cli 0.7.13 → 0.7.15

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/index.js +39 -14
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -24629,7 +24629,7 @@ var {
24629
24629
  Help
24630
24630
  } = import__.default;
24631
24631
  // package.json
24632
- var version = "0.7.13";
24632
+ var version = "0.7.15";
24633
24633
 
24634
24634
  // ../core/src/AiService/AiServiceBase.ts
24635
24635
  class AiServiceBase {
@@ -24638,6 +24638,7 @@ class AiServiceBase {
24638
24638
  this.usageMeter = usageMeter;
24639
24639
  }
24640
24640
  async* send(systemPrompt, messages) {
24641
+ this.usageMeter.checkLimit();
24641
24642
  this.usageMeter.incrementMessageCount();
24642
24643
  const stream = this.sendImpl(systemPrompt, messages);
24643
24644
  for await (const chunk of stream) {
@@ -24650,6 +24651,7 @@ class AiServiceBase {
24650
24651
  }
24651
24652
  }
24652
24653
  async request(systemPrompt, messages) {
24654
+ this.usageMeter.checkLimit();
24653
24655
  this.usageMeter.incrementMessageCount();
24654
24656
  const stream = this.sendImpl(systemPrompt, messages);
24655
24657
  const usage = {
@@ -33344,6 +33346,16 @@ class UsageMeter {
33344
33346
  }
33345
33347
  this.#usage.totalCost += usage.totalCost ?? 0;
33346
33348
  }
33349
+ setUsage(usage, messageCount) {
33350
+ this.#usage.inputTokens = usage.inputTokens ?? this.#usage.inputTokens;
33351
+ this.#usage.outputTokens = usage.outputTokens ?? this.#usage.outputTokens;
33352
+ this.#usage.cacheWriteTokens = usage.cacheWriteTokens ?? this.#usage.cacheWriteTokens;
33353
+ this.#usage.cacheReadTokens = usage.cacheReadTokens ?? this.#usage.cacheReadTokens;
33354
+ this.#usage.totalCost = usage.totalCost ?? this.#usage.totalCost;
33355
+ if (messageCount !== undefined) {
33356
+ this.#messageCount = messageCount;
33357
+ }
33358
+ }
33347
33359
  incrementMessageCount(count = 1) {
33348
33360
  this.#messageCount += count;
33349
33361
  }
@@ -33352,12 +33364,20 @@ class UsageMeter {
33352
33364
  const cost = this.#usage.totalCost >= this.maxCost;
33353
33365
  return {
33354
33366
  messageCount,
33367
+ maxMessageCount: this.maxMessageCount,
33355
33368
  cost,
33369
+ maxCost: this.maxCost,
33356
33370
  result: messageCount || cost
33357
33371
  };
33358
33372
  }
33373
+ checkLimit() {
33374
+ const result = this.isLimitExceeded();
33375
+ if (result.result) {
33376
+ throw new Error(`Usage limit exceeded. Message count: ${result.messageCount}/${result.maxMessageCount}, cost: ${result.cost}/${result.maxCost}`);
33377
+ }
33378
+ }
33359
33379
  get usage() {
33360
- return { ...this.#usage };
33380
+ return { ...this.#usage, messageCount: this.#messageCount };
33361
33381
  }
33362
33382
  printUsage() {
33363
33383
  const { inputTokens, outputTokens, cacheReadTokens, cacheWriteTokens } = this.#usage;
@@ -34674,10 +34694,10 @@ class AgentBase {
34674
34694
  ai;
34675
34695
  config;
34676
34696
  handlers;
34677
- messages;
34697
+ #messages;
34678
34698
  constructor(name2, ai, config, messages = []) {
34679
34699
  this.ai = ai;
34680
- this.messages = messages;
34700
+ this.#messages = messages;
34681
34701
  if (config.agents && config.agents.length > 0) {
34682
34702
  const agents = agentsPrompt(config.agents, name2);
34683
34703
  config.systemPrompt += `
@@ -34690,6 +34710,9 @@ ${agents}`;
34690
34710
  }
34691
34711
  this.handlers = handlers;
34692
34712
  }
34713
+ get messages() {
34714
+ return this.#messages;
34715
+ }
34693
34716
  async#callback(event) {
34694
34717
  await this.config.callback?.(event);
34695
34718
  }
@@ -34697,17 +34720,19 @@ ${agents}`;
34697
34720
  this.#callback({ kind: "StartTask" /* StartTask */, agent: this, systemPrompt: this.config.systemPrompt });
34698
34721
  return await this.#processLoop(prompt);
34699
34722
  }
34700
- async step(promp) {
34701
- if (this.messages.length === 0) {
34702
- this.#callback({ kind: "StartTask" /* StartTask */, agent: this, systemPrompt: this.config.systemPrompt });
34723
+ async step(promp, messages) {
34724
+ if (messages) {
34725
+ this.#messages = messages;
34703
34726
  }
34704
- if (this.ai.usageMeter.isLimitExceeded().result) {
34705
- this.#callback({ kind: "UsageExceeded" /* UsageExceeded */, agent: this });
34706
- return { type: "UsageExceeded" };
34727
+ if (this.#messages.length === 0) {
34728
+ this.#callback({ kind: "StartTask" /* StartTask */, agent: this, systemPrompt: this.config.systemPrompt });
34707
34729
  }
34708
34730
  return await this.#request(promp);
34709
34731
  }
34710
- async handleStepResponse(response) {
34732
+ async handleStepResponse(response, messages) {
34733
+ if (messages) {
34734
+ this.#messages = messages;
34735
+ }
34711
34736
  return this.#handleResponse(response);
34712
34737
  }
34713
34738
  async#processLoop(userMessage) {
@@ -34734,7 +34759,7 @@ ${agents}`;
34734
34759
  throw new Error("userMessage is missing");
34735
34760
  }
34736
34761
  await this.#callback({ kind: "StartRequest" /* StartRequest */, agent: this, userMessage });
34737
- this.messages.push({
34762
+ this.#messages.push({
34738
34763
  role: "user",
34739
34764
  content: userMessage
34740
34765
  });
@@ -34742,7 +34767,7 @@ ${agents}`;
34742
34767
  const retryCount = 5;
34743
34768
  for (let i3 = 0;i3 < retryCount; i3++) {
34744
34769
  currentAssistantMessage = "";
34745
- const stream = this.ai.send(this.config.systemPrompt, this.messages);
34770
+ const stream = this.ai.send(this.config.systemPrompt, this.#messages);
34746
34771
  try {
34747
34772
  for await (const chunk of stream) {
34748
34773
  switch (chunk.type) {
@@ -34769,7 +34794,7 @@ ${agents}`;
34769
34794
  if (!currentAssistantMessage) {
34770
34795
  throw new Error("No assistant message received");
34771
34796
  }
34772
- this.messages.push({
34797
+ this.#messages.push({
34773
34798
  role: "assistant",
34774
34799
  content: currentAssistantMessage
34775
34800
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polka-codes/cli",
3
- "version": "0.7.13",
3
+ "version": "0.7.15",
4
4
  "license": "AGPL-3.0",
5
5
  "author": "github@polka.codes",
6
6
  "type": "module",