@polka-codes/runner 0.8.24 → 0.8.26

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 +91 -22
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -32748,7 +32748,7 @@ var {
32748
32748
  Help
32749
32749
  } = import__.default;
32750
32750
  // package.json
32751
- var version = "0.8.24";
32751
+ var version = "0.8.26";
32752
32752
 
32753
32753
  // src/runner.ts
32754
32754
  import { execSync } from "node:child_process";
@@ -32763,14 +32763,19 @@ import { join as join2 } from "node:path";
32763
32763
  class AiServiceBase {
32764
32764
  usageMeter;
32765
32765
  options;
32766
+ #abortController;
32766
32767
  constructor(options) {
32767
32768
  this.options = options;
32768
32769
  this.usageMeter = options.usageMeter;
32769
32770
  }
32771
+ abort() {
32772
+ this.#abortController?.abort();
32773
+ }
32770
32774
  async* send(systemPrompt, messages) {
32771
32775
  this.usageMeter.checkLimit();
32772
32776
  this.usageMeter.incrementMessageCount();
32773
- const stream = this.sendImpl(systemPrompt, messages);
32777
+ this.#abortController = new AbortController;
32778
+ const stream = this.sendImpl(systemPrompt, messages, this.#abortController.signal);
32774
32779
  for await (const chunk of stream) {
32775
32780
  switch (chunk.type) {
32776
32781
  case "usage":
@@ -32783,7 +32788,8 @@ class AiServiceBase {
32783
32788
  async request(systemPrompt, messages) {
32784
32789
  this.usageMeter.checkLimit();
32785
32790
  this.usageMeter.incrementMessageCount();
32786
- const stream = this.sendImpl(systemPrompt, messages);
32791
+ this.#abortController = new AbortController;
32792
+ const stream = this.sendImpl(systemPrompt, messages, this.#abortController.signal);
32787
32793
  const usage = {
32788
32794
  inputTokens: 0,
32789
32795
  outputTokens: 0,
@@ -36288,7 +36294,7 @@ class AnthropicService extends AiServiceBase {
36288
36294
  info: anthropicModels[id] ?? anthropicModels[anthropicDefaultModelId]
36289
36295
  };
36290
36296
  }
36291
- async* sendImpl(systemPrompt, messages) {
36297
+ async* sendImpl(systemPrompt, messages, signal) {
36292
36298
  let stream;
36293
36299
  const modelId = this.model.id;
36294
36300
  const cacheControl = this.#options.enableCache ? { type: "ephemeral" } : undefined;
@@ -36346,7 +36352,7 @@ class AnthropicService extends AiServiceBase {
36346
36352
  return message;
36347
36353
  }),
36348
36354
  stream: true
36349
- });
36355
+ }, { signal });
36350
36356
  break;
36351
36357
  }
36352
36358
  default: {
@@ -36357,7 +36363,7 @@ class AnthropicService extends AiServiceBase {
36357
36363
  system: [{ text: systemPrompt, type: "text" }],
36358
36364
  messages,
36359
36365
  stream: true
36360
- });
36366
+ }, { signal });
36361
36367
  break;
36362
36368
  }
36363
36369
  }
@@ -41715,7 +41721,7 @@ class DeepSeekService extends AiServiceBase {
41715
41721
  info: deepSeekModels[id] ?? deepSeekModels[deepSeekDefaultModelId]
41716
41722
  };
41717
41723
  }
41718
- async* sendImpl(systemPrompt, messages) {
41724
+ async* sendImpl(systemPrompt, messages, signal) {
41719
41725
  const openAiMessages = [
41720
41726
  { role: "system", content: systemPrompt },
41721
41727
  ...convertToOpenAiMessages(messages)
@@ -41727,7 +41733,7 @@ class DeepSeekService extends AiServiceBase {
41727
41733
  temperature: 0,
41728
41734
  stream: true,
41729
41735
  stream_options: { include_usage: true }
41730
- });
41736
+ }, { signal });
41731
41737
  for await (const chunk of stream) {
41732
41738
  const delta = chunk.choices[0]?.delta;
41733
41739
  if (delta?.reasoning_content) {
@@ -41771,7 +41777,7 @@ class OllamaService extends AiServiceBase {
41771
41777
  info: openAiModelInfoSaneDefaults
41772
41778
  };
41773
41779
  }
41774
- async* sendImpl(systemPrompt, messages) {
41780
+ async* sendImpl(systemPrompt, messages, signal) {
41775
41781
  const openAiMessages = [
41776
41782
  { role: "system", content: systemPrompt },
41777
41783
  ...convertToOpenAiMessages(messages)
@@ -41781,7 +41787,7 @@ class OllamaService extends AiServiceBase {
41781
41787
  messages: openAiMessages,
41782
41788
  temperature: 0,
41783
41789
  stream: true
41784
- });
41790
+ }, { signal });
41785
41791
  for await (const chunk of stream) {
41786
41792
  const delta = chunk.choices[0]?.delta;
41787
41793
  if (delta?.content) {
@@ -41828,7 +41834,7 @@ class OpenRouterService extends AiServiceBase {
41828
41834
  this.#modelProviderInfo = data.data;
41829
41835
  });
41830
41836
  }
41831
- async* sendImpl(systemPrompt, messages) {
41837
+ async* sendImpl(systemPrompt, messages, signal) {
41832
41838
  const openAiMessages = [
41833
41839
  { role: "system", content: systemPrompt },
41834
41840
  ...convertToOpenAiMessages(messages)
@@ -41888,7 +41894,7 @@ class OpenRouterService extends AiServiceBase {
41888
41894
  transforms: shouldApplyMiddleOutTransform ? ["middle-out"] : undefined,
41889
41895
  include_reasoning: true,
41890
41896
  ...reasoning
41891
- });
41897
+ }, { signal });
41892
41898
  let genId;
41893
41899
  for await (const chunk of stream) {
41894
41900
  if ("error" in chunk) {
@@ -42142,7 +42148,7 @@ var getArray = (args, name, defaultValue) => {
42142
42148
  return [ret];
42143
42149
  };
42144
42150
  // ../core/src/tools/utils/replaceInFile.ts
42145
- var replaceInFile = async (fileContent, diff) => {
42151
+ var replaceInFile = (fileContent, diff) => {
42146
42152
  const blockPattern = /<<<<<+ SEARCH>?\s*\r?\n([\s\S]*?)\r?\n=======[ \t]*\r?\n([\s\S]*?)\r?\n?>>>>>+ REPLACE/g;
42147
42153
  const blocks = [];
42148
42154
  for (let match = blockPattern.exec(diff);match !== null; match = blockPattern.exec(diff)) {
@@ -42187,14 +42193,32 @@ var replaceInFile = async (fileContent, diff) => {
42187
42193
  const startPos = endPos - strippedSearch.length;
42188
42194
  return content.slice(0, startPos) + replace + content.slice(endPos);
42189
42195
  }
42190
- throw new Error(`Could not find the following text in file:
42191
- ${search}`);
42196
+ return null;
42192
42197
  };
42193
42198
  let updatedFile = fileContent;
42199
+ let appliedCount = 0;
42200
+ const totalCount = blocks.length;
42194
42201
  for (const { search, replace } of blocks) {
42195
- updatedFile = findAndReplace(updatedFile, search, replace);
42202
+ const result = findAndReplace(updatedFile, search, replace);
42203
+ if (result !== null) {
42204
+ updatedFile = result;
42205
+ appliedCount++;
42206
+ }
42196
42207
  }
42197
- return updatedFile;
42208
+ let status;
42209
+ if (appliedCount === 0) {
42210
+ status = "no_diff_applied";
42211
+ } else if (appliedCount < totalCount) {
42212
+ status = "some_diff_applied";
42213
+ } else {
42214
+ status = "all_diff_applied";
42215
+ }
42216
+ return {
42217
+ content: updatedFile,
42218
+ status,
42219
+ appliedCount,
42220
+ totalCount
42221
+ };
42198
42222
  };
42199
42223
  // ../core/src/tools/askFollowupQuestion.ts
42200
42224
  var toolInfo = {
@@ -42889,14 +42913,30 @@ var handler8 = async (provider, args) => {
42889
42913
  if (fileContent == null) {
42890
42914
  return {
42891
42915
  type: "Error" /* Error */,
42892
- message: `<error><replace_in_file_path>${path}</replace_in_file_path><error_message>File not found</error_message></error>`
42916
+ message: `<replace_in_file_result path="${path}" status="failed" message="File not found" />`
42917
+ };
42918
+ }
42919
+ const result = replaceInFile(fileContent, diff);
42920
+ if (result.status === "no_diff_applied") {
42921
+ return {
42922
+ type: "Error" /* Error */,
42923
+ message: `<replace_in_file_result path="${path}" status="failed" message="Unable to apply changes">
42924
+ <file_content path="${path}">${fileContent}</file_content>
42925
+ </replace_in_file_result>`
42926
+ };
42927
+ }
42928
+ await provider.writeFile(path, result.content);
42929
+ if (result.status === "some_diff_applied") {
42930
+ return {
42931
+ type: "Reply" /* Reply */,
42932
+ message: `<replace_in_file_result path="${path}" status="some_diff_applied" applied_count="${result.appliedCount}" total_count="${result.totalCount}">
42933
+ <file_content path="${path}">${result.content}</file_content>
42934
+ </replace_in_file_result>`
42893
42935
  };
42894
42936
  }
42895
- const result = await replaceInFile(fileContent, diff);
42896
- await provider.writeFile(path, result);
42897
42937
  return {
42898
42938
  type: "Reply" /* Reply */,
42899
- message: `<replace_in_file_path>${path}</replace_in_file_path>`
42939
+ message: `<replace_in_file_result path="${path}" status="all_diff_applied" />`
42900
42940
  };
42901
42941
  };
42902
42942
  var isAvailable8 = (provider) => {
@@ -43938,6 +43978,7 @@ class AgentBase {
43938
43978
  handlers;
43939
43979
  #messages = [];
43940
43980
  #policies;
43981
+ #aborted = false;
43941
43982
  constructor(name, ai, config) {
43942
43983
  this.ai = ai;
43943
43984
  if (config.agents && config.agents.length > 0) {
@@ -43969,6 +44010,10 @@ ${instance.prompt}`;
43969
44010
  this.config = config;
43970
44011
  this.#policies = policies;
43971
44012
  }
44013
+ abort() {
44014
+ this.#aborted = true;
44015
+ this.ai.abort();
44016
+ }
43972
44017
  get parameters() {
43973
44018
  return this.ai.options.parameters;
43974
44019
  }
@@ -43997,11 +44042,17 @@ ${instance.prompt}`;
43997
44042
  async#processLoop(userMessage) {
43998
44043
  let nextRequest = userMessage;
43999
44044
  while (true) {
44045
+ if (this.#aborted) {
44046
+ return { type: "Aborted" };
44047
+ }
44000
44048
  if (this.ai.usageMeter.isLimitExceeded().result) {
44001
44049
  this.#callback({ kind: "UsageExceeded" /* UsageExceeded */, agent: this });
44002
44050
  return { type: "UsageExceeded" };
44003
44051
  }
44004
44052
  const response = await this.#request(nextRequest);
44053
+ if (this.#aborted) {
44054
+ return { type: "Aborted" };
44055
+ }
44005
44056
  const resp = await this.#handleResponse(response);
44006
44057
  if (resp.type === "exit") {
44007
44058
  this.#callback({ kind: "EndTask" /* EndTask */, agent: this, exitReason: resp.reason });
@@ -44048,14 +44099,23 @@ ${instance.prompt}`;
44048
44099
  }
44049
44100
  }
44050
44101
  } catch (error) {
44102
+ if (error instanceof Error && error.name === "AbortError") {
44103
+ break;
44104
+ }
44051
44105
  console.error("Error in stream:", error);
44052
44106
  }
44053
44107
  if (currentAssistantMessage) {
44054
44108
  break;
44055
44109
  }
44110
+ if (this.#aborted) {
44111
+ break;
44112
+ }
44056
44113
  console.debug(`Retrying request ${i2 + 1} of ${retryCount}`);
44057
44114
  }
44058
44115
  if (!currentAssistantMessage) {
44116
+ if (this.#aborted) {
44117
+ return [];
44118
+ }
44059
44119
  throw new Error("No assistant message received");
44060
44120
  }
44061
44121
  this.#messages.push({
@@ -44376,6 +44436,7 @@ class MultiAgent {
44376
44436
  case "Delegate" /* Delegate */:
44377
44437
  console.warn("Unexpected exit reason", delegateResult);
44378
44438
  break;
44439
+ case "Aborted":
44379
44440
  case "Interrupted" /* Interrupted */:
44380
44441
  return delegateResult;
44381
44442
  case "Exit" /* Exit */:
@@ -44383,6 +44444,7 @@ class MultiAgent {
44383
44444
  }
44384
44445
  return delegateResult;
44385
44446
  }
44447
+ case "Aborted":
44386
44448
  case "Interrupted" /* Interrupted */:
44387
44449
  case "Exit" /* Exit */:
44388
44450
  this.#agents.pop();
@@ -44415,6 +44477,11 @@ class MultiAgent {
44415
44477
  get hasActiveAgent() {
44416
44478
  return this.#agents.length > 0;
44417
44479
  }
44480
+ abort() {
44481
+ if (this.hasActiveAgent) {
44482
+ this.#agents[this.#agents.length - 1].abort();
44483
+ }
44484
+ }
44418
44485
  }
44419
44486
  // ../core/node_modules/zod/lib/index.mjs
44420
44487
  var util;
@@ -54363,6 +54430,8 @@ var getProvider = (agentName, config2, options = {}) => {
54363
54430
  }
54364
54431
  return provider2;
54365
54432
  };
54433
+ // ../cli-shared/src/utils/eventHandler.ts
54434
+ var toolCallStats = new Map;
54366
54435
  // ../../node_modules/ws/wrapper.mjs
54367
54436
  var import_stream = __toESM(require_stream(), 1);
54368
54437
  var import_receiver = __toESM(require_receiver(), 1);
@@ -58807,7 +58876,7 @@ class Runner {
58807
58876
  path,
58808
58877
  content
58809
58878
  });
58810
- console.log(`Sent content for file: ${path}`);
58879
+ console.log(`Sent content for file: ${path}, size: ${content.length}`);
58811
58880
  } else {
58812
58881
  console.error(`Path is not a file or directory: ${path}`);
58813
58882
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polka-codes/runner",
3
- "version": "0.8.24",
3
+ "version": "0.8.26",
4
4
  "license": "AGPL-3.0",
5
5
  "author": "github@polka.codes",
6
6
  "type": "module",