@polka-codes/core 0.4.8 → 0.4.10

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 +104 -29
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -3069,6 +3069,7 @@ class AnthropicService extends AiServiceBase {
3069
3069
  async* sendImpl(systemPrompt, messages) {
3070
3070
  let stream;
3071
3071
  const modelId = this.model.id;
3072
+ const cacheControl = this.#options.enableCache ? { type: "ephemeral" } : undefined;
3072
3073
  switch (modelId) {
3073
3074
  case "claude-3-5-sonnet-20241022":
3074
3075
  case "claude-3-5-haiku-20241022":
@@ -3090,7 +3091,7 @@ class AnthropicService extends AiServiceBase {
3090
3091
  {
3091
3092
  text: systemPrompt,
3092
3093
  type: "text",
3093
- cache_control: { type: "ephemeral" }
3094
+ cache_control: cacheControl
3094
3095
  }
3095
3096
  ],
3096
3097
  messages: messages.map((message, index) => {
@@ -3101,36 +3102,18 @@ class AnthropicService extends AiServiceBase {
3101
3102
  {
3102
3103
  type: "text",
3103
3104
  text: message.content,
3104
- cache_control: {
3105
- type: "ephemeral"
3106
- }
3105
+ cache_control: cacheControl
3107
3106
  }
3108
3107
  ] : message.content.map((content, contentIndex) => contentIndex === message.content.length - 1 ? {
3109
3108
  ...content,
3110
- cache_control: {
3111
- type: "ephemeral"
3112
- }
3109
+ cache_control: cacheControl
3113
3110
  } : content)
3114
3111
  };
3115
3112
  }
3116
3113
  return message;
3117
3114
  }),
3118
3115
  stream: true
3119
- }, (() => {
3120
- switch (modelId) {
3121
- case "claude-3-5-sonnet-20241022":
3122
- case "claude-3-5-haiku-20241022":
3123
- case "claude-3-opus-20240229":
3124
- case "claude-3-haiku-20240307":
3125
- return {
3126
- headers: {
3127
- "anthropic-beta": "prompt-caching-2024-07-31"
3128
- }
3129
- };
3130
- default:
3131
- return;
3132
- }
3133
- })());
3116
+ });
3134
3117
  break;
3135
3118
  }
3136
3119
  default: {
@@ -8024,6 +8007,7 @@ class OllamaService extends AiServiceBase {
8024
8007
  class OpenRouterService extends AiServiceBase {
8025
8008
  #client;
8026
8009
  #apiKey;
8010
+ #options;
8027
8011
  model;
8028
8012
  constructor(options) {
8029
8013
  super(options.usageMeter);
@@ -8042,6 +8026,7 @@ class OpenRouterService extends AiServiceBase {
8042
8026
  "X-Title": "Polka Codes"
8043
8027
  }
8044
8028
  });
8029
+ this.#options = options;
8045
8030
  this.model = {
8046
8031
  id: options.model,
8047
8032
  info: {}
@@ -8052,6 +8037,7 @@ class OpenRouterService extends AiServiceBase {
8052
8037
  { role: "system", content: systemPrompt },
8053
8038
  ...convertToOpenAiMessages(messages)
8054
8039
  ];
8040
+ const cacheControl = this.#options.enableCache ? { type: "ephemeral" } : undefined;
8055
8041
  switch (this.model.id) {
8056
8042
  case "anthropic/claude-3.5-sonnet":
8057
8043
  case "anthropic/claude-3.5-sonnet:beta":
@@ -8071,7 +8057,7 @@ class OpenRouterService extends AiServiceBase {
8071
8057
  {
8072
8058
  type: "text",
8073
8059
  text: systemPrompt,
8074
- cache_control: { type: "ephemeral" }
8060
+ cache_control: cacheControl
8075
8061
  }
8076
8062
  ]
8077
8063
  };
@@ -8086,7 +8072,7 @@ class OpenRouterService extends AiServiceBase {
8086
8072
  lastTextPart = { type: "text", text: "..." };
8087
8073
  msg.content.push(lastTextPart);
8088
8074
  }
8089
- lastTextPart.cache_control = { type: "ephemeral" };
8075
+ lastTextPart.cache_control = cacheControl;
8090
8076
  }
8091
8077
  }
8092
8078
  break;
@@ -9396,7 +9382,15 @@ var responsePrompts = {
9396
9382
  <tool_result>
9397
9383
  ${result}
9398
9384
  </tool_result>
9399
- </tool_response>`
9385
+ </tool_response>`,
9386
+ commandResult: (command, exitCode, stdout, stderr) => `<command>${command}</command>
9387
+ <command_exit_code>${exitCode}</command_exit_code>
9388
+ <command_stdout>
9389
+ ${stdout}
9390
+ </command_stdout>
9391
+ <command_stderr>
9392
+ ${stderr}
9393
+ </command_stderr>`
9400
9394
  };
9401
9395
 
9402
9396
  // src/Agent/AgentBase.ts
@@ -9580,6 +9574,10 @@ ${agents}`;
9580
9574
  canRetry: false
9581
9575
  };
9582
9576
  }
9577
+ const resp = await this.onBeforeInvokeTool(name, args);
9578
+ if (resp) {
9579
+ return resp;
9580
+ }
9583
9581
  return await handler13(this.config.provider, args);
9584
9582
  } catch (error) {
9585
9583
  return {
@@ -9651,9 +9649,13 @@ class AnalyzerAgent extends AgentBase {
9651
9649
  toolNamePrefix,
9652
9650
  provider: options.provider,
9653
9651
  interactive: options.interactive,
9654
- agents: options.agents
9652
+ agents: options.agents,
9653
+ scripts: options.scripts
9655
9654
  });
9656
9655
  }
9656
+ onBeforeInvokeTool() {
9657
+ return Promise.resolve(undefined);
9658
+ }
9657
9659
  }
9658
9660
  var analyzerAgentInfo = {
9659
9661
  name: "analyzer",
@@ -9745,9 +9747,13 @@ class ArchitectAgent extends AgentBase {
9745
9747
  toolNamePrefix,
9746
9748
  provider: options.provider,
9747
9749
  interactive: options.interactive,
9748
- agents: options.agents
9750
+ agents: options.agents,
9751
+ scripts: options.scripts
9749
9752
  });
9750
9753
  }
9754
+ onBeforeInvokeTool() {
9755
+ return Promise.resolve(undefined);
9756
+ }
9751
9757
  }
9752
9758
  var architectAgentInfo = {
9753
9759
  name: "architect",
@@ -9888,9 +9894,55 @@ class CoderAgent extends AgentBase {
9888
9894
  toolNamePrefix,
9889
9895
  provider: options.provider,
9890
9896
  interactive: options.interactive,
9891
- agents: options.agents
9897
+ agents: options.agents,
9898
+ scripts: options.scripts
9892
9899
  });
9893
9900
  }
9901
+ async onBeforeInvokeTool(name, args) {
9902
+ const executeCommand = this.config.provider.executeCommand;
9903
+ if (!executeCommand) {
9904
+ return;
9905
+ }
9906
+ const format = this.config.scripts?.format;
9907
+ const formatCommand = typeof format === "string" ? format : format?.command;
9908
+ if (formatCommand) {
9909
+ try {
9910
+ await executeCommand(formatCommand, false);
9911
+ } catch (error) {
9912
+ console.warn(`Failed to format code using command: ${formatCommand}`, error);
9913
+ }
9914
+ }
9915
+ const check = this.config.scripts?.check;
9916
+ const checkCommand = typeof check === "string" ? check : check?.command;
9917
+ if (checkCommand) {
9918
+ try {
9919
+ const { exitCode, stdout, stderr } = await executeCommand(checkCommand, false);
9920
+ if (exitCode !== 0) {
9921
+ return {
9922
+ type: "Reply" /* Reply */,
9923
+ message: responsePrompts.commandResult(checkCommand, exitCode, stdout, stderr)
9924
+ };
9925
+ }
9926
+ } catch (error) {
9927
+ console.warn(`Failed to check code using command: ${checkCommand}`, error);
9928
+ }
9929
+ }
9930
+ const test = this.config.scripts?.test;
9931
+ const testCommand = typeof test === "string" ? test : test?.command;
9932
+ if (testCommand) {
9933
+ try {
9934
+ const { exitCode, stdout, stderr } = await executeCommand(testCommand, false);
9935
+ if (exitCode !== 0) {
9936
+ return {
9937
+ type: "Reply" /* Reply */,
9938
+ message: responsePrompts.commandResult(testCommand, exitCode, stdout, stderr)
9939
+ };
9940
+ }
9941
+ } catch (error) {
9942
+ console.warn(`Failed to test code using command: ${testCommand}`, error);
9943
+ }
9944
+ }
9945
+ }
9894
9946
  }
9895
9947
  var coderAgentInfo = {
9896
9948
  name: "coder",
@@ -10130,10 +10182,16 @@ var prompt3 = `You are an analyzer agent responsible for examining project files
10130
10182
  - Code style tools and rules
10131
10183
  - Project structure and conventions
10132
10184
  - Common development workflows
10185
+ - .gitignore patterns and sensitive/generated files
10133
10186
 
10134
10187
  2. Generate a YAML configuration that captures:
10135
- - scripts section based on package.json scripts and CI workflows
10188
+ - scripts section based on package.json scripts and CI workflows. If applicable, generate following scripts:
10189
+ - format: Format the code using a code formatter tool
10190
+ - check: Check the code for errors using a linter tool
10191
+ - test: Run tests using a test runner tool
10192
+ - include other relevant scripts based on project conventions, tools, and patterns
10136
10193
  - rules section based on project conventions, tools, and patterns
10194
+ - excludeFiles section for common sensitive/generated files
10137
10195
 
10138
10196
  3. Use tool_attempt_completion to return the final configuration in this format:
10139
10197
 
@@ -10151,6 +10209,16 @@ rules:
10151
10209
  - "Use \`bun\` as package manager"
10152
10210
  - "Write tests using bun:test with snapshots"
10153
10211
  - "Follow Biome code style"
10212
+
10213
+ excludeFiles:
10214
+ # Sensitive files
10215
+ - ".env"
10216
+ - ".env.*"
10217
+ - ".npmrc"
10218
+ # Generated files
10219
+ - "package-lock.json"
10220
+ - "yarn.lock"
10221
+ - "pnpm-lock.yaml"
10154
10222
  </tool_parameter_result>
10155
10223
  </tool_attempt_completion>
10156
10224
 
@@ -10160,6 +10228,13 @@ Focus on:
10160
10228
  - Code style and linting rules
10161
10229
  - File organization and naming conventions
10162
10230
  - Build and development workflows
10231
+ - Sensitive files that should not be committed:
10232
+ - Environment files (.env*)
10233
+ - Configuration files with potential secrets
10234
+ - Generated files to exclude:
10235
+ - Lock files from package managers
10236
+ - Build artifacts and outputs
10237
+ - Generated file that are not ignored by .gitignore
10163
10238
 
10164
10239
  The configuration should accurately reflect the project's structure, tools, and conventions.
10165
10240
  `;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polka-codes/core",
3
- "version": "0.4.8",
3
+ "version": "0.4.10",
4
4
  "license": "AGPL-3.0",
5
5
  "type": "module",
6
6
  "exports": {